From 24f90dee17f57473b91263b0c9c1fc11fb5706be Mon Sep 17 00:00:00 2001 From: Ekaterina Aidova Date: Fri, 25 Oct 2024 10:28:16 +0400 Subject: [PATCH] use optimum helper in notebooks (#2473) --- .../dolly-2-instruction-following.ipynb | 43 ++++++++----------- .../llava-multimodal-chatbot-genai.ipynb | 28 +++++++----- .../llava-multimodal-chatbot-optimum.ipynb | 27 +++++++----- .../llm-agent-functioncall-qwen.ipynb | 36 ++++++++-------- .../llm-agent-rag-llamaindex.ipynb | 24 ++++++----- 5 files changed, 83 insertions(+), 75 deletions(-) diff --git a/notebooks/dolly-2-instruction-following/dolly-2-instruction-following.ipynb b/notebooks/dolly-2-instruction-following/dolly-2-instruction-following.ipynb index 3e2efebb991..6ecf7aac9fd 100644 --- a/notebooks/dolly-2-instruction-following/dolly-2-instruction-following.ipynb +++ b/notebooks/dolly-2-instruction-following/dolly-2-instruction-following.ipynb @@ -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", @@ -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)" ] }, { @@ -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", @@ -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", @@ -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", diff --git a/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-genai.ipynb b/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-genai.ipynb index 8d6980ade09..f2c82561780 100644 --- a/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-genai.ipynb +++ b/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-genai.ipynb @@ -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)" ] }, { @@ -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\"})" ] }, { @@ -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", diff --git a/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-optimum.ipynb b/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-optimum.ipynb index 3481d94a58a..638a245f281 100644 --- a/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-optimum.ipynb +++ b/notebooks/llava-multimodal-chatbot/llava-multimodal-chatbot-optimum.ipynb @@ -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)" ] }, { @@ -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\"})" ] }, { @@ -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", diff --git a/notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb b/notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb index 7c7e4e9fdd6..781d801aeee 100644 --- a/notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb +++ b/notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb @@ -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", @@ -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)" ] }, { @@ -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\"})" ] }, { @@ -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\"])" ] }, { diff --git a/notebooks/llm-agent-react/llm-agent-rag-llamaindex.ipynb b/notebooks/llm-agent-react/llm-agent-rag-llamaindex.ipynb index 966508df67d..8eb6ba91bca 100644 --- a/notebooks/llm-agent-react/llm-agent-rag-llamaindex.ipynb +++ b/notebooks/llm-agent-react/llm-agent-rag-llamaindex.ipynb @@ -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", @@ -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", @@ -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\"})" ] }, { @@ -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\"})" ] }, {