diff --git a/notebooks/convert-to-openvino/convert-to-openvino.ipynb b/notebooks/convert-to-openvino/convert-to-openvino.ipynb index d557df798c5..fb8e1b174ee 100644 --- a/notebooks/convert-to-openvino/convert-to-openvino.ipynb +++ b/notebooks/convert-to-openvino/convert-to-openvino.ipynb @@ -66,63 +66,9 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "usage: ovc INPUT_MODEL... [-h] [--output_model OUTPUT_MODEL]\n", - " [--compress_to_fp16 [True | False]] [--version] [--input INPUT]\n", - " [--output OUTPUT] [--extension EXTENSION] [--verbose]\n", - "\n", - "positional arguments:\n", - " INPUT_MODEL Input model file(s) from TensorFlow, ONNX,\n", - " PaddlePaddle. Use openvino.convert_model in Python to\n", - " convert models from PyTorch.\n", - "\n", - "optional arguments:\n", - " -h, --help show this help message and exit\n", - " --output_model OUTPUT_MODEL\n", - " This parameter is used to name output .xml/.bin files\n", - " with converted model.\n", - " --compress_to_fp16 [True | False]\n", - " Compress weights in output OpenVINO model to FP16. To\n", - " turn off compression use \"--compress_to_fp16=False\"\n", - " command line parameter. Default value is True.\n", - " --version Print ovc version and exit.\n", - " --input INPUT Information of model input required for model\n", - " conversion. This is a comma separated list with\n", - " optional input names and shapes. The order of inputs\n", - " in converted model will match the order of specified\n", - " inputs. The shape is specified as comma-separated\n", - " list. Example, to set `input_1` input with shape\n", - " [1,100] and `sequence_len` input with shape [1,?]:\n", - " \"input_1[1,100],sequence_len[1,?]\", where \"?\" is a\n", - " dynamic dimension, which means that such a dimension\n", - " can be specified later in the runtime. If the\n", - " dimension is set as an integer (like 100 in [1,100]),\n", - " such a dimension is not supposed to be changed later,\n", - " during a model conversion it is treated as a static\n", - " value. Example with unnamed inputs: \"[1,100],[1,?]\".\n", - " --output OUTPUT One or more comma-separated model outputs to be\n", - " preserved in the converted model. Other outputs are\n", - " removed. If `output` parameter is not specified then\n", - " all outputs from the original model are preserved. Do\n", - " not add :0 to the names for TensorFlow. The order of\n", - " outputs in the converted model is the same as the\n", - " order of specified names. Example: ovc model.onnx\n", - " output=out_1,out_2\n", - " --extension EXTENSION\n", - " Paths or a comma-separated list of paths to libraries\n", - " (.so or .dll) with extensions. To disable all\n", - " extensions including those that are placed at the\n", - " default location, pass an empty string.\n", - " --verbose Print detailed information about conversion.\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# OVC CLI tool parameters description\n", "\n", @@ -145,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -171,7 +117,7 @@ "outputs": [], "source": [ "from transformers import AutoModelForSequenceClassification, AutoTokenizer\n", - "from transformers.onnx import export, FeaturesManager\n", + "import torch\n", "\n", "ONNX_NLP_MODEL_PATH = MODEL_DIRECTORY_PATH / \"distilbert.onnx\"\n", "\n", @@ -180,19 +126,14 @@ "# initialize tokenizer\n", "tokenizer = AutoTokenizer.from_pretrained(\"distilbert-base-uncased-finetuned-sst-2-english\")\n", "\n", - "# get model onnx config function for output feature format sequence-classification\n", - "model_kind, model_onnx_config = FeaturesManager.check_supported_model_or_raise(hf_model, feature=\"sequence-classification\")\n", - "# fill onnx config based on pytorch model config\n", - "onnx_config = model_onnx_config(hf_model.config)\n", - "\n", - "# export to onnx format\n", - "export(\n", - " preprocessor=tokenizer,\n", - " model=hf_model,\n", - " config=onnx_config,\n", - " opset=onnx_config.default_onnx_opset,\n", - " output=ONNX_NLP_MODEL_PATH,\n", - ")" + "if not ONNX_NLP_MODEL_PATH.exists():\n", + " inputs = tokenizer(\"Hi, how are you?\", return_tensors=\"pt\")\n", + " input_names = list(inputs.keys())\n", + " dynamic_axes = {input_name: {0: \"batch_size\", 1: \"seq_length\"} for input_name in input_names}\n", + " torch.onnx.export(\n", + " hf_model, args=dict(inputs), input_names=input_names, output_names=[\"logits\"], dynamic_axes=dynamic_axes, f=ONNX_NLP_MODEL_PATH, opset_version=14\n", + " )\n", + " print(f\"ONNX model exported to {ONNX_NLP_MODEL_PATH}\")" ] }, { @@ -227,17 +168,9 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ONNX model exported to model/resnet.onnx\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import torch\n", "import warnings\n", @@ -266,17 +199,9 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "model/distilbert.onnx\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import openvino as ov\n", "\n", @@ -290,30 +215,9 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", - "To disable this warning, you can either:\n", - "\t- Avoid using `tokenizers` before the fork if possible\n", - "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[ INFO ] Generated IR will be compressed to FP16. If you get lower accuracy, please consider disabling compression by removing argument \"compress_to_fp16\" or set it to false \"compress_to_fp16=False\".\n", - "Find more information about compression to FP16 at https://docs.openvino.ai/2024/openvino-workflow/model-preparation/conversion-parameters.html\n", - "[ SUCCESS ] XML file: model/distilbert.xml\n", - "[ SUCCESS ] BIN file: model/distilbert.bin\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "! ovc model/distilbert.onnx --output_model model/distilbert.xml" ] @@ -333,7 +237,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -344,30 +248,9 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", - "To disable this warning, you can either:\n", - "\t- Avoid using `tokenizers` before the fork if possible\n", - "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[ INFO ] Generated IR will be compressed to FP16. If you get lower accuracy, please consider disabling compression by removing argument \"compress_to_fp16\" or set it to false \"compress_to_fp16=False\".\n", - "Find more information about compression to FP16 at https://docs.openvino.ai/2024/openvino-workflow/model-preparation/conversion-parameters.html\n", - "[ SUCCESS ] XML file: model/distilbert.xml\n", - "[ SUCCESS ] BIN file: model/distilbert.bin\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "! ovc model/distilbert.onnx --input input_ids[1,128],attention_mask[1,128] --output_model model/distilbert.xml" ] @@ -382,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -393,30 +276,9 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", - "To disable this warning, you can either:\n", - "\t- Avoid using `tokenizers` before the fork if possible\n", - "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[ INFO ] Generated IR will be compressed to FP16. If you get lower accuracy, please consider disabling compression by removing argument \"compress_to_fp16\" or set it to false \"compress_to_fp16=False\".\n", - "Find more information about compression to FP16 at https://docs.openvino.ai/2024/openvino-workflow/model-preparation/conversion-parameters.html\n", - "[ SUCCESS ] XML file: model/distilbert.xml\n", - "[ SUCCESS ] BIN file: model/distilbert.bin\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "! ovc model/distilbert.onnx --input \"input_ids[1,?],attention_mask[1,?]\" --output_model model/distilbert.xml" ] @@ -431,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -451,30 +313,9 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", - "To disable this warning, you can either:\n", - "\t- Avoid using `tokenizers` before the fork if possible\n", - "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[ INFO ] Generated IR will be compressed to FP16. If you get lower accuracy, please consider disabling compression by removing argument \"compress_to_fp16\" or set it to false \"compress_to_fp16=False\".\n", - "Find more information about compression to FP16 at https://docs.openvino.ai/2024/openvino-workflow/model-preparation/conversion-parameters.html\n", - "[ SUCCESS ] XML file: model/distilbert.xml\n", - "[ SUCCESS ] BIN file: model/distilbert.bin\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "! ovc model/distilbert.onnx --input input_ids[1,10..128],attention_mask[1,10..128] --output_model model/distilbert.xml" ] @@ -492,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -504,28 +345,9 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", - "To disable this warning, you can either:\n", - "\t- Avoid using `tokenizers` before the fork if possible\n", - "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[ SUCCESS ] XML file: model/distilbert.xml\n", - "[ SUCCESS ] BIN file: model/distilbert.bin\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "! ovc model/distilbert.onnx --output_model model/distilbert.xml --compress_to_fp16=False" ] @@ -557,7 +379,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -601,7 +423,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -643,7 +465,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -687,7 +509,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -736,7 +558,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -778,7 +600,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -792,7 +614,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.11.4" }, "openvino_notebooks": { "imageUrl": "",