Skip to content

Commit

Permalink
use optimum helper in notebooks (#2473)
Browse files Browse the repository at this point in the history
  • Loading branch information
eaidova authored Oct 25, 2024
1 parent f983fc1 commit 24f90de
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
"outputs": [],
"source": [
"import os\n",
"from pathlib import Path\n",
"import requests\n",
"\n",
"os.environ[\"GIT_CLONE_PROTECTION_ACTIVE\"] = \"false\"\n",
"\n",
Expand All @@ -99,12 +101,17 @@
"%pip install -q \"diffusers>=0.16.1\" \"transformers>=4.33.0\" \"torch>=2.1\" \"nncf>=2.10.0\" \"onnx<1.16.2\" \"gradio>=4.19\" --extra-index-url https://download.pytorch.org/whl/cpu\n",
"%pip install -q \"git+https://github.com/huggingface/optimum-intel.git\"\n",
"\n",
"import requests\n",
"\n",
"r = requests.get(\n",
" url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py\",\n",
")\n",
"open(\"notebook_utils.py\", \"w\").write(r.text)"
"utility_files = [\"notebook_utils.py\", \"cmd_helper.py\"]\n",
"\n",
"for utility in utility_files:\n",
" local_path = Path(utility)\n",
" if not local_path.exists():\n",
" r = requests.get(\n",
" url=f\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/{local_path.name}\",\n",
" )\n",
" with local_path.open(\"w\") as f:\n",
" f.write(r.text)"
]
},
{
Expand Down Expand Up @@ -207,7 +214,7 @@
}
],
"source": [
"from IPython.display import Markdown, display\n",
"from IPython.display import display\n",
"import ipywidgets as widgets\n",
"\n",
"prepare_int4_model = widgets.Checkbox(\n",
Expand Down Expand Up @@ -319,6 +326,7 @@
],
"source": [
"from pathlib import Path\n",
"from cmd_helper import optimum_cli\n",
"\n",
"model_id = \"databricks/dolly-v2-3b\"\n",
"model_path = Path(\"dolly-v2-3b\")\n",
Expand All @@ -331,36 +339,19 @@
"def convert_to_fp16():\n",
" if (fp16_model_dir / \"openvino_model.xml\").exists():\n",
" return\n",
" fp16_model_dir.mkdir(parents=True, exist_ok=True)\n",
" export_command_base = \"optimum-cli export openvino --model {} --task text-generation-with-past --weight-format fp16\".format(model_id)\n",
" export_command = export_command_base + \" \" + str(fp16_model_dir)\n",
" display(Markdown(\"**Export command:**\"))\n",
" display(Markdown(f\"`{export_command}`\"))\n",
" ! $export_command\n",
" optimum_cli(model_id, fp16_model_dir, additional_args={\"weight-format\": \"fp16\"})\n",
"\n",
"\n",
"def convert_to_int8():\n",
" if (int8_model_dir / \"openvino_model.xml\").exists():\n",
" return\n",
" int8_model_dir.mkdir(parents=True, exist_ok=True)\n",
" export_command_base = \"optimum-cli export openvino --model {} --task text-generation-with-past --weight-format int8\".format(model_id)\n",
" export_command = export_command_base + \" \" + str(int8_model_dir)\n",
" display(Markdown(\"**Export command:**\"))\n",
" display(Markdown(f\"`{export_command}`\"))\n",
" ! $export_command\n",
" optimum_cli(model_id, int8_model_dir, additional_args={\"weight-format\": \"int8\"})\n",
"\n",
"\n",
"def convert_to_int4():\n",
" if (int4_model_dir / \"openvino_model.xml\").exists():\n",
" return\n",
" int4_model_dir.mkdir(parents=True, exist_ok=True)\n",
" export_command_base = \"optimum-cli export openvino --model {} --task text-generation-with-past --weight-format int4 --ratio 1.0 --group-size 128\".format(\n",
" model_id\n",
" )\n",
" export_command = export_command_base + \" \" + str(int4_model_dir)\n",
" display(Markdown(\"**Export command:**\"))\n",
" display(Markdown(f\"`{export_command}`\"))\n",
" ! $export_command\n",
" optimum_cli(model_id, int4_model_dir, additional_args={\"weight-format\": \"int4\"})\n",
"\n",
"\n",
"if prepare_fp16_model.value:\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,25 @@
},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import requests\n",
"\n",
"%pip install -q \"torch>=2.1.0\" \"torchvision\" \"torchaudio\" --index-url https://download.pytorch.org/whl/cpu\n",
"%pip install -q \"git+https://github.com/eaidova/optimum-intel.git@ea/minicpmv\"\n",
"%pip install -q \"nncf>=2.13.0\" \"sentencepiece\" \"tokenizers>=0.12.1\" \"transformers>=4.45.0\" \"gradio>=4.36\"\n",
"%pip install -q -U --pre --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly openvino_tokenizers openvino openvino-genai"
"%pip install -q -U --pre --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly openvino-tokenizers openvino openvino-genai\n",
"\n",
"\n",
"utility_files = [\"notebook_utils.py\", \"cmd_helper.py\"]\n",
"\n",
"for utility in utility_files:\n",
" local_path = Path(utility)\n",
" if not local_path.exists():\n",
" r = requests.get(\n",
" url=f\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/{local_path.name}\",\n",
" )\n",
" with local_path.open(\"w\") as f:\n",
" f.write(r.text)"
]
},
{
Expand Down Expand Up @@ -145,13 +160,13 @@
},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from cmd_helper import optimum_cli\n",
"\n",
"model_id = \"llava-hf/llava-1.5-7b-hf\"\n",
"model_path = Path(model_id.split(\"/\")[-1]) / \"FP16\"\n",
"\n",
"if not model_path.exists():\n",
" !optimum-cli export openvino --model {model_id} --weight-format fp16 {model_path}"
" optimum_cli(model_id, model_path, additoonal_args={\"weight-format\": \"fp16\"})"
]
},
{
Expand Down Expand Up @@ -333,13 +348,6 @@
}
],
"source": [
"import requests\n",
"\n",
"r = requests.get(\n",
" url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py\",\n",
")\n",
"open(\"notebook_utils.py\", \"w\").write(r.text)\n",
"\n",
"from notebook_utils import device_widget\n",
"\n",
"device = device_widget(exclude=[\"NPU\"])\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,24 @@
},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import requests\n",
"\n",
"%pip install -q \"torch>=2.1.0\" \"torchvision\" \"torchaudio\" --index-url https://download.pytorch.org/whl/cpu\n",
"%pip install -q \"git+https://github.com/eaidova/optimum-intel.git@ea/minicpmv\"\n",
"%pip install -q \"nncf>=2.13.0\" \"sentencepiece\" \"tokenizers>=0.12.1\" \"transformers>=4.45.0\" \"gradio>=4.36\"\n",
"%pip install -q -U --pre --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly openvino_tokenizers openvino openvino-genai"
"%pip install -q -U --pre --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly openvino-tokenizers openvino openvino-genai\n",
"\n",
"utility_files = [\"notebook_utils.py\", \"cmd_helper.py\"]\n",
"\n",
"for utility in utility_files:\n",
" local_path = Path(utility)\n",
" if not local_path.exists():\n",
" r = requests.get(\n",
" url=f\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/{local_path.name}\",\n",
" )\n",
" with local_path.open(\"w\") as f:\n",
" f.write(r.text)"
]
},
{
Expand Down Expand Up @@ -147,13 +161,13 @@
},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from cmd_helper import optimum_cli\n",
"\n",
"model_id = \"llava-hf/llava-1.5-7b-hf\"\n",
"model_path = Path(model_id.split(\"/\")[-1]) / \"FP16\"\n",
"\n",
"if not model_path.exists():\n",
" !optimum-cli export openvino --model {model_id} --weight-format fp16 {model_path}"
" optimum_cli(model_id, model_path, additoonal_args={\"weight-format\": \"fp16\"})"
]
},
{
Expand Down Expand Up @@ -331,13 +345,6 @@
}
],
"source": [
"import requests\n",
"\n",
"r = requests.get(\n",
" url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py\",\n",
")\n",
"open(\"notebook_utils.py\", \"w\").write(r.text)\n",
"\n",
"from notebook_utils import device_widget\n",
"\n",
"device = device_widget(exclude=[\"NPU\"])\n",
Expand Down
36 changes: 18 additions & 18 deletions notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"outputs": [],
"source": [
"import os\n",
"from pathlib import Path\n",
"import requests\n",
"\n",
"os.environ[\"GIT_CLONE_PROTECTION_ACTIVE\"] = \"false\"\n",
"\n",
Expand All @@ -73,7 +75,18 @@
"\"qwen-agent==0.0.7\" \"transformers>=4.38.1\" \"gradio==4.21.0\", \"modelscope-studio>=0.4.0\" \"langchain>=0.2.3\" \"langchain-community>=0.2.4\" \"wikipedia\"\n",
"%pip install -q --extra-index-url https://download.pytorch.org/whl/cpu \\\n",
"\"git+https://github.com/huggingface/optimum-intel.git\" \\\n",
"\"git+https://github.com/openvinotoolkit/nncf.git\""
"\"git+https://github.com/openvinotoolkit/nncf.git\"\n",
"\n",
"utility_files = [\"notebook_utils.py\", \"cmd_helper.py\"]\n",
"\n",
"for utility in utility_files:\n",
" local_path = Path(utility)\n",
" if not local_path.exists():\n",
" r = requests.get(\n",
" url=f\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/{local_path.name}\",\n",
" )\n",
" with local_path.open(\"w\") as f:\n",
" f.write(r.text)"
]
},
{
Expand Down Expand Up @@ -190,12 +203,13 @@
"outputs": [],
"source": [
"from pathlib import Path\n",
"from cmd_helper import optimum_cli\n",
"\n",
"model_id = \"Qwen/Qwen2-7B-Instruct\"\n",
"model_path = \"Qwen2-7B-Instruct-ov\"\n",
"\n",
"if not Path(model_path).exists():\n",
" !optimum-cli export openvino --model {model_id} --task text-generation-with-past --trust-remote-code --weight-format int4 --ratio 0.72 {model_path}"
" optimum_cli(model_id, model_path, additional_args={\"task\": \"text-generation-with-past\", \"trust-remote-code\": \"\", \"weight-format\": \"int4\", \"ratio\": \"0.72\"})"
]
},
{
Expand Down Expand Up @@ -232,23 +246,9 @@
}
],
"source": [
"import openvino as ov\n",
"import ipywidgets as widgets\n",
"\n",
"core = ov.Core()\n",
"\n",
"support_devices = core.available_devices\n",
"if \"NPU\" in support_devices:\n",
" support_devices.remove(\"NPU\")\n",
"\n",
"device = widgets.Dropdown(\n",
" options=support_devices + [\"AUTO\"],\n",
" value=\"CPU\",\n",
" description=\"Device:\",\n",
" disabled=False,\n",
")\n",
"from notebook_utils import device_widget\n",
"\n",
"device"
"device = device_widget(\"CPU\", [\"NPU\"])"
]
},
{
Expand Down
24 changes: 13 additions & 11 deletions notebooks/llm-agent-react/llm-agent-rag-llamaindex.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@
"source": [
"import os\n",
"import requests\n",
"from pathlib import Path\n",
"\n",
"utility_files = [\"notebook_utils.py\", \"cmd_helper.py\", \"pip_helper.py\"]\n",
"\n",
"r = requests.get(\n",
" url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py\",\n",
")\n",
"open(\"notebook_utils.py\", \"w\").write(r.text)\n",
"\n",
"r = requests.get(\n",
" url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/pip_helper.py\",\n",
")\n",
"open(\"pip_helper.py\", \"w\").write(r.text)\n",
"for utility in utility_files:\n",
" local_path = Path(utility)\n",
" if not local_path.exists():\n",
" r = requests.get(\n",
" url=f\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/{local_path.name}\",\n",
" )\n",
" with local_path.open(\"w\") as f:\n",
" f.write(r.text)\n",
"\n",
"os.environ[\"GIT_CLONE_PROTECTION_ACTIVE\"] = \"false\"\n",
"\n",
Expand Down Expand Up @@ -211,6 +212,7 @@
"source": [
"from pathlib import Path\n",
"import huggingface_hub as hf_hub\n",
"from cmd_helper import optimum_cli\n",
"\n",
"llm_model_path = llm_model_id.value.split(\"/\")[-1]\n",
"repo_name = llm_model_id.value.split(\"/\")[0]\n",
Expand All @@ -219,7 +221,7 @@
" if repo_name == \"OpenVINO\":\n",
" hf_hub.snapshot_download(llm_model_id.value, local_dir=llm_model_path)\n",
" else:\n",
" !optimum-cli export openvino --model {llm_model_id.value} --task text-generation-with-past --trust-remote-code --weight-format int4 --group-size 128 --ratio 0.8 {llm_model_path}"
" !optimum_cli(llm_model_id.value, llm_model_path, additional_args=-{\"task\": \"text-generation-with-past\", \"weight-format\": \"int4\"})"
]
},
{
Expand All @@ -246,7 +248,7 @@
"embedding_model_path = \"bge-small-en-v1.5\"\n",
"\n",
"if not Path(embedding_model_path).exists():\n",
" !optimum-cli export openvino --model {embedding_model_id} --task feature-extraction {embedding_model_path}"
" optimum_cli(embedding_model_id, embedding_model_path, additional_args={\"task\": \"feature-extraction\"})"
]
},
{
Expand Down

0 comments on commit 24f90de

Please sign in to comment.