From 33fc7b76422ed592cab598cc2c825a78e5e26c22 Mon Sep 17 00:00:00 2001 From: Helena Kloosterman Date: Thu, 25 Apr 2024 14:02:36 +0200 Subject: [PATCH] Update imports in examples/notebooks (#687) * Update imports in examples `from optimum.intel.openvino import ...` -> `from optimum.intel import ...` * Update workflow to run example tests * Get compression from kwargs --- .github/workflows/test_openvino_examples.yml | 20 ++++++++-------- .github/workflows/test_openvino_notebooks.yml | 4 ++-- .../audio-classification/requirements.txt | 3 ++- .../run_audio_classification.py | 2 +- .../image-classification/requirements.txt | 1 + .../run_image_classification.py | 2 +- .../question-answering/requirements.txt | 1 + .../openvino/question-answering/run_qa.py | 2 +- .../openvino/question-answering/trainer_qa.py | 2 +- .../text-classification/requirements.txt | 3 ++- .../openvino/text-classification/run_glue.py | 2 +- .../openvino/optimum_openvino_inference.ipynb | 24 +++++++++---------- .../question_answering_quantization.ipynb | 4 ++-- notebooks/openvino/requirements.txt | 2 +- .../stable_diffusion_optimization.ipynb | 2 +- optimum/intel/openvino/configuration.py | 4 +++- optimum/intel/openvino/modeling_seq2seq.py | 4 ++-- optimum/intel/openvino/quantization.py | 4 ++-- 18 files changed, 46 insertions(+), 40 deletions(-) diff --git a/.github/workflows/test_openvino_examples.yml b/.github/workflows/test_openvino_examples.yml index 61b411c967..747afa31b5 100644 --- a/.github/workflows/test_openvino_examples.yml +++ b/.github/workflows/test_openvino_examples.yml @@ -7,11 +7,11 @@ on: push: paths: - '.github/workflows/test_openvino_examples.yml' - - 'examples/openvino/*' + - 'examples/openvino/**' pull_request: paths: - '.github/workflows/test_openvino_examples.yml' - - 'examples/openvino/*' + - 'examples/openvino/**' concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -22,9 +22,9 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.8", "3.10"] + python-version: ["3.8", "3.11"] - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 @@ -35,12 +35,12 @@ jobs: - name: Install dependencies run: | - pip install optimum[openvino] jstyleson nncf pytest - pip install -r examples/openvino/audio-classification/requirements.txt - pip install -r examples/openvino/image-classification/requirements.txt - pip install -r examples/openvino/question-answering/requirements.txt - pip install -r examples/openvino/text-classification/requirements.txt + pip install .[openvino] jstyleson pytest + pip install -r examples/openvino/audio-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu + pip install -r examples/openvino/image-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu + pip install -r examples/openvino/question-answering/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu + pip install -r examples/openvino/text-classification/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu - name: Test examples run: | - python -m pytest examples/openvino/test_examples.py \ No newline at end of file + python -m pytest examples/openvino/test_examples.py diff --git a/.github/workflows/test_openvino_notebooks.yml b/.github/workflows/test_openvino_notebooks.yml index 7b037d0565..ed77077e87 100644 --- a/.github/workflows/test_openvino_notebooks.yml +++ b/.github/workflows/test_openvino_notebooks.yml @@ -23,9 +23,9 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.8", "3.10"] + python-version: ["3.8", "3.11"] - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 diff --git a/examples/openvino/audio-classification/requirements.txt b/examples/openvino/audio-classification/requirements.txt index 60c66d8091..df77b9298b 100644 --- a/examples/openvino/audio-classification/requirements.txt +++ b/examples/openvino/audio-classification/requirements.txt @@ -1,4 +1,5 @@ datasets>=1.14.0 evaluate librosa -torchaudio \ No newline at end of file +torchaudio +accelerate diff --git a/examples/openvino/audio-classification/run_audio_classification.py b/examples/openvino/audio-classification/run_audio_classification.py index b8df86a575..30b95c1739 100644 --- a/examples/openvino/audio-classification/run_audio_classification.py +++ b/examples/openvino/audio-classification/run_audio_classification.py @@ -35,7 +35,7 @@ from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version -from optimum.intel.openvino import OVConfig, OVTrainer, OVTrainingArguments +from optimum.intel import OVConfig, OVTrainer, OVTrainingArguments logger = logging.getLogger(__name__) diff --git a/examples/openvino/image-classification/requirements.txt b/examples/openvino/image-classification/requirements.txt index c52a5f399b..a55c46ca4c 100644 --- a/examples/openvino/image-classification/requirements.txt +++ b/examples/openvino/image-classification/requirements.txt @@ -2,3 +2,4 @@ datasets >= 1.8.0 torch >= 1.9.0 torchvision>=0.6.0 evaluate +accelerate diff --git a/examples/openvino/image-classification/run_image_classification.py b/examples/openvino/image-classification/run_image_classification.py index 5f98d95cb5..04c2984d8b 100644 --- a/examples/openvino/image-classification/run_image_classification.py +++ b/examples/openvino/image-classification/run_image_classification.py @@ -52,7 +52,7 @@ from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version -from optimum.intel.openvino import OVConfig, OVTrainer, OVTrainingArguments +from optimum.intel import OVConfig, OVTrainer, OVTrainingArguments logger = logging.getLogger(__name__) diff --git a/examples/openvino/question-answering/requirements.txt b/examples/openvino/question-answering/requirements.txt index 3bd58b158b..0bb12723c2 100644 --- a/examples/openvino/question-answering/requirements.txt +++ b/examples/openvino/question-answering/requirements.txt @@ -1,3 +1,4 @@ datasets >= 1.8.0 torch >= 1.9.0 evaluate +accelerate diff --git a/examples/openvino/question-answering/run_qa.py b/examples/openvino/question-answering/run_qa.py index a86c7fb6d7..261fa839c9 100644 --- a/examples/openvino/question-answering/run_qa.py +++ b/examples/openvino/question-answering/run_qa.py @@ -49,7 +49,7 @@ from transformers.utils.versions import require_version from utils_qa import postprocess_qa_predictions -from optimum.intel.openvino import OVConfig, OVTrainingArguments +from optimum.intel import OVConfig, OVTrainingArguments # Will error if the minimal version of Transformers is not installed. Remove at your own risks. diff --git a/examples/openvino/question-answering/trainer_qa.py b/examples/openvino/question-answering/trainer_qa.py index bda91f99b5..c10466060b 100644 --- a/examples/openvino/question-answering/trainer_qa.py +++ b/examples/openvino/question-answering/trainer_qa.py @@ -20,7 +20,7 @@ import torch.nn.functional as F from transformers.trainer_utils import PredictionOutput -from optimum.intel.openvino.trainer import OVTrainer +from optimum.intel import OVTrainer class QuestionAnsweringOVTrainer(OVTrainer): diff --git a/examples/openvino/text-classification/requirements.txt b/examples/openvino/text-classification/requirements.txt index 95655f80ec..660e820c3c 100644 --- a/examples/openvino/text-classification/requirements.txt +++ b/examples/openvino/text-classification/requirements.txt @@ -4,4 +4,5 @@ scipy scikit-learn protobuf torch >= 1.3 -evaluate \ No newline at end of file +evaluate +accelerate diff --git a/examples/openvino/text-classification/run_glue.py b/examples/openvino/text-classification/run_glue.py index 002f67232c..66670de77e 100644 --- a/examples/openvino/text-classification/run_glue.py +++ b/examples/openvino/text-classification/run_glue.py @@ -46,7 +46,7 @@ from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version -from optimum.intel.openvino import OVConfig, OVTrainer, OVTrainingArguments +from optimum.intel import OVConfig, OVTrainer, OVTrainingArguments # Will error if the minimal version of Transformers is not installed. Remove at your own risks. diff --git a/notebooks/openvino/optimum_openvino_inference.ipynb b/notebooks/openvino/optimum_openvino_inference.ipynb index b94238d358..dcd7dc866f 100644 --- a/notebooks/openvino/optimum_openvino_inference.ipynb +++ b/notebooks/openvino/optimum_openvino_inference.ipynb @@ -76,7 +76,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForQuestionAnswering\n", + "from optimum.intel import OVModelForQuestionAnswering\n", "\n", "# Load PyTorch model from the Hub and export to OpenVINO in the background\n", "model = OVModelForQuestionAnswering.from_pretrained(\"distilbert-base-uncased-distilled-squad\", export=True)\n", @@ -182,7 +182,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForQuestionAnswering\n", + "from optimum.intel import OVModelForQuestionAnswering\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model = OVModelForQuestionAnswering.from_pretrained(\"distilbert-base-uncased-distilled-squad-ov-fp32\")\n", @@ -240,7 +240,7 @@ ], "source": [ "import torch\n", - "from optimum.intel.openvino import OVModelForQuestionAnswering\n", + "from optimum.intel import OVModelForQuestionAnswering\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model = OVModelForQuestionAnswering.from_pretrained(\"distilbert-base-uncased-distilled-squad-ov-fp32\")\n", @@ -324,7 +324,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForQuestionAnswering\n", + "from optimum.intel import OVModelForQuestionAnswering\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model = OVModelForQuestionAnswering.from_pretrained(\n", @@ -529,7 +529,7 @@ ], "source": [ "from IPython.display import Audio\n", - "from optimum.intel.openvino import OVModelForAudioClassification\n", + "from optimum.intel import OVModelForAudioClassification\n", "from transformers import AutoFeatureExtractor, pipeline\n", "from datasets import load_dataset\n", "\n", @@ -638,7 +638,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForCausalLM\n", + "from optimum.intel import OVModelForCausalLM\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model_id = \"helenai/gpt2-ov\"\n", @@ -704,7 +704,7 @@ ], "source": [ "from IPython.display import Image\n", - "from optimum.intel.openvino import OVModelForImageClassification\n", + "from optimum.intel import OVModelForImageClassification\n", "from transformers import AutoImageProcessor, pipeline\n", "\n", "model_id = \"helenai/microsoft-swin-tiny-patch4-window7-224-ov\"\n", @@ -766,7 +766,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForMaskedLM\n", + "from optimum.intel import OVModelForMaskedLM\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model_id = \"helenai/bert-base-uncased-ov\"\n", @@ -835,7 +835,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForQuestionAnswering\n", + "from optimum.intel import OVModelForQuestionAnswering\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "# Load the model and tokenizer saved in Part 1 of this notebook. Or use the line below to load them from the hub\n", @@ -890,7 +890,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForSeq2SeqLM\n", + "from optimum.intel import OVModelForSeq2SeqLM\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model_id = \"helenai/t5-small-ov\"\n", @@ -998,7 +998,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForSequenceClassification\n", + "from optimum.intel import OVModelForSequenceClassification\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model_id = \"helenai/papluca-xlm-roberta-base-language-detection-ov\"\n", @@ -1047,7 +1047,7 @@ } ], "source": [ - "from optimum.intel.openvino import OVModelForTokenClassification\n", + "from optimum.intel import OVModelForTokenClassification\n", "from transformers import AutoTokenizer, pipeline\n", "\n", "model_id = \"helenai/dslim-bert-base-NER-ov-fp32\"\n", diff --git a/notebooks/openvino/question_answering_quantization.ipynb b/notebooks/openvino/question_answering_quantization.ipynb index 196e3ba6a7..2481c9b904 100644 --- a/notebooks/openvino/question_answering_quantization.ipynb +++ b/notebooks/openvino/question_answering_quantization.ipynb @@ -51,7 +51,7 @@ "import transformers\n", "from evaluate import evaluator\n", "from openvino.runtime import Core\n", - "from optimum.intel.openvino import OVModelForQuestionAnswering, OVQuantizer, OVQuantizationConfig, OVConfig\n", + "from optimum.intel import OVModelForQuestionAnswering, OVQuantizer, OVQuantizationConfig, OVConfig\n", "from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline\n", "\n", "transformers.logging.set_verbosity_error()\n", @@ -286,7 +286,7 @@ "**NOTE:** if you notice very low accuracy after post-training quantization, it is likely caused by an overflow issue which affects processors that do not contain VNNI (Vector Neural Network Instruction). NNCF has an `overflow_fix` option to address this. It will effectively use 7-bits for quantizing instead of 8-bits to prevent the overflow. To use this option, modify the code in the next cell to add an explicit quantization configuration, and set `overflow_fix` to `\"enable\"`:\n", "\n", "```\n", - "from optimum.intel.openvino import OVConfig, OVQuantizationConfig\n", + "from optimum.intel import OVConfig, OVQuantizationConfig\n", "\n", "ov_config = OVConfig(quantization_config=OVQuantizationConfig(overflow_fix=\"enable\")\n", "quantizer = OVQuantizer.from_pretrained(model)\n", diff --git a/notebooks/openvino/requirements.txt b/notebooks/openvino/requirements.txt index 3432e949e9..bb7a517cff 100644 --- a/notebooks/openvino/requirements.txt +++ b/notebooks/openvino/requirements.txt @@ -1,4 +1,4 @@ -optimum-intel[openvino, nncf] +optimum-intel[openvino] datasets evaluate[evaluator] ipywidgets diff --git a/notebooks/openvino/stable_diffusion_optimization.ipynb b/notebooks/openvino/stable_diffusion_optimization.ipynb index 6c79bc5df0..f2297b2151 100644 --- a/notebooks/openvino/stable_diffusion_optimization.ipynb +++ b/notebooks/openvino/stable_diffusion_optimization.ipynb @@ -14,7 +14,7 @@ "metadata": {}, "outputs": [], "source": [ - "from optimum.intel.openvino import OVStableDiffusionPipeline\n", + "from optimum.intel import OVStableDiffusionPipeline\n", "from diffusers.training_utils import set_seed\n", "from IPython.display import display" ] diff --git a/optimum/intel/openvino/configuration.py b/optimum/intel/openvino/configuration.py index 739b5640a6..398e1e0b50 100644 --- a/optimum/intel/openvino/configuration.py +++ b/optimum/intel/openvino/configuration.py @@ -310,7 +310,9 @@ def __init__( if isinstance(quantization_config, dict): quantization_config = self._quantization_config_from_dict(quantization_config) self.quantization_config = quantization_config - self.compression = None # A field for backward-compatability of training-time compression parameters + self.compression = kwargs.get( + "compression", None + ) # A field for backward-compatability of training-time compression parameters bits = self.quantization_config.bits if self.quantization_config else None self.dtype = "int" + str(bits) if isinstance(bits, int) else dtype diff --git a/optimum/intel/openvino/modeling_seq2seq.py b/optimum/intel/openvino/modeling_seq2seq.py index 0959a8c2f9..6d72dc7b0e 100644 --- a/optimum/intel/openvino/modeling_seq2seq.py +++ b/optimum/intel/openvino/modeling_seq2seq.py @@ -224,7 +224,7 @@ ```python >>> from transformers import {processor_class} - >>> from optimum.intel.openvino import {model_class} + >>> from optimum.intel import {model_class} >>> from datasets import load_dataset >>> processor = {processor_class}.from_pretrained("{checkpoint}") @@ -241,7 +241,7 @@ ```python >>> from transformers import {processor_class}, pipeline - >>> from optimum.intel.openvino import {model_class} + >>> from optimum.intel import {model_class} >>> from datasets import load_dataset >>> processor = {processor_class}.from_pretrained("{checkpoint}") diff --git a/optimum/intel/openvino/quantization.py b/optimum/intel/openvino/quantization.py index 124b0366c1..ddd0cdec75 100644 --- a/optimum/intel/openvino/quantization.py +++ b/optimum/intel/openvino/quantization.py @@ -235,7 +235,7 @@ def quantize( Examples: ```python - >>> from optimum.intel.openvino import OVQuantizer, OVModelForCausalLM + >>> from optimum.intel import OVQuantizer, OVModelForCausalLM >>> from transformers import AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-3b") >>> quantizer = OVQuantizer.from_pretrained(model, task="text-generation") @@ -245,7 +245,7 @@ def quantize( ``` ```python - >>> from optimum.intel.openvino import OVQuantizer, OVModelForSequenceClassification + >>> from optimum.intel import OVQuantizer, OVModelForSequenceClassification >>> from transformers import AutoModelForSequenceClassification >>> model = OVModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english", export=True) >>> # or