Skip to content

Commit

Permalink
Merge branch 'main' into test-sentence-transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
echarlaix committed May 29, 2024
2 parents 9b6fb5f + bfd0767 commit 1dc6f4a
Show file tree
Hide file tree
Showing 63 changed files with 3,580 additions and 707 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/test_inc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ jobs:
python -m pip install --upgrade pip
pip install cmake
pip install py-cpuinfo
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cpu
pip install .[neural-compressor,diffusers,tests]
pip install intel-extension-for-transformers
pip install intel-extension-for-transformers==1.4.1
pip install peft
- name: Test with Pytest
Expand All @@ -42,7 +43,7 @@ jobs:
- name: Test IPEX
run: |
pip uninstall -y intel-extension-for-transformers
pip install torch==2.1.0 torchaudio==2.1.0 torchvision==0.16 --extra-index-url https://download.pytorch.org/whl/cpu
pip install intel-extension-for-pytorch==2.1.100
pip install torch==2.3.0 torchaudio==2.3.0 torchvision==0.18 --extra-index-url https://download.pytorch.org/whl/cpu
pip install intel-extension-for-pytorch==2.3.0
pytest tests/neural_compressor/test_ipex.py
1 change: 1 addition & 0 deletions .github/workflows/test_ipex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install torch torchaudio torchvision --extra-index-url https://download.pytorch.org/whl/cpu
pip install .[ipex,tests]
- name: Test with Pytest
run: |
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/test_offline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Offline usage / Python - Test

on:
push:
branches: [main]
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
build:
strategy:
fail-fast: false
matrix:
python-version: [3.9]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install .[tests,openvino]
- name: Test
run: |
HF_HOME=/tmp/ huggingface-cli download hf-internal-testing/tiny-random-gpt2
HF_HOME=/tmp/ HF_HUB_OFFLINE=1 optimum-cli export openvino --model hf-internal-testing/tiny-random-gpt2 gpt2_openvino --task text-generation
huggingface-cli download hf-internal-testing/tiny-random-gpt2
HF_HUB_OFFLINE=1 optimum-cli export openvino --model hf-internal-testing/tiny-random-gpt2 gpt2_openvino --task text-generation
pytest tests/openvino/test_modeling.py -k "test_load_from_hub" -s -vvvvv
HF_HUB_OFFLINE=1 pytest tests/openvino/test_modeling.py -k "test_load_from_hub" -s -vvvvv
20 changes: 10 additions & 10 deletions .github/workflows/test_openvino_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
Expand All @@ -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
python -m pytest examples/openvino/test_examples.py
4 changes: 2 additions & 2 deletions .github/workflows/test_openvino_notebooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions examples/neural_compressor/language-modeling/run_clm.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@
from transformers.utils.versions import require_version

from optimum.intel.neural_compressor import INCModelForCausalLM, INCQuantizer, INCTrainer
from optimum.intel.utils.import_utils import (
INTEL_EXTENSION_FOR_TRANSFORMERS_IMPORT_ERROR,
is_intel_extension_for_transformers_available,
)
from optimum.intel.utils.import_utils import ITREX_IMPORT_ERROR, is_itrex_available


if is_intel_extension_for_transformers_available():
if is_itrex_available():
from intel_extension_for_transformers.transformers.utils.config import GPTQConfig, RtnConfig

os.environ["CUDA_VISIBLE_DEVICES"] = ""
Expand Down Expand Up @@ -658,8 +655,8 @@ def compute_metrics(eval_preds):
else:
recipes = {}
if optim_args.quantization_approach == "weight_only":
if not is_intel_extension_for_transformers_available():
raise ImportError(INTEL_EXTENSION_FOR_TRANSFORMERS_IMPORT_ERROR.format("WeightOnly quantization"))
if not is_itrex_available():
raise ImportError(ITREX_IMPORT_ERROR.format("WeightOnly quantization"))
if optim_args.apply_pruning or optim_args.apply_distillation:
raise ValueError("Weight only quantization and pruning or distillation cannot be combined.")

Expand Down
3 changes: 2 additions & 1 deletion examples/openvino/audio-classification/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
datasets>=1.14.0
evaluate
librosa
torchaudio
torchaudio
accelerate
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
1 change: 1 addition & 0 deletions examples/openvino/image-classification/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ datasets >= 1.8.0
torch >= 1.9.0
torchvision>=0.6.0
evaluate
accelerate
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
1 change: 1 addition & 0 deletions examples/openvino/question-answering/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
datasets >= 1.8.0
torch >= 1.9.0
evaluate
accelerate
2 changes: 1 addition & 1 deletion examples/openvino/question-answering/run_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/openvino/question-answering/trainer_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion examples/openvino/text-classification/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ scipy
scikit-learn
protobuf
torch >= 1.3
evaluate
evaluate
accelerate
2 changes: 1 addition & 1 deletion examples/openvino/text-classification/run_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions notebooks/openvino/optimum_openvino_inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions notebooks/openvino/question_answering_quantization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion notebooks/openvino/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
optimum-intel[openvino, nncf]
optimum-intel[openvino]
datasets
evaluate[evaluator]
ipywidgets
Expand Down
14 changes: 10 additions & 4 deletions notebooks/openvino/stable_diffusion_hybrid_quantization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"import transformers\n",
"from pathlib import Path\n",
"from openvino.runtime import Core\n",
"from optimum.intel import OVStableDiffusionPipeline, OVWeightQuantizationConfig\n",
"from optimum.intel import OVConfig, OVQuantizer, OVStableDiffusionPipeline, OVWeightQuantizationConfig\n",
"from optimum.intel.openvino.configuration import OVQuantizationMethod\n",
"\n",
"transformers.logging.set_verbosity_error()\n",
"datasets.logging.set_verbosity_error()"
Expand Down Expand Up @@ -198,9 +199,14 @@
},
"outputs": [],
"source": [
"quantization_config = OVWeightQuantizationConfig(bits=8, dataset=calibration_dataset, num_samples=NUM_SAMPLES)\n",
"int8_pipe = OVStableDiffusionPipeline.from_pretrained(model_id=MODEL_ID, export=True, quantization_config=quantization_config)\n",
"int8_pipe.save_pretrained(int8_model_path)"
"int8_pipe = OVStableDiffusionPipeline.from_pretrained(model_id=MODEL_ID, export=True)\n",
"quantization_config = OVWeightQuantizationConfig(bits=8, num_samples=NUM_SAMPLES, quant_method=OVQuantizationMethod.HYBRID)\n",
"quantizer = OVQuantizer(int8_pipe)\n",
"quantizer.quantize(\n",
" ov_config=OVConfig(quantization_config=quantization_config),\n",
" calibration_dataset=calibration_dataset,\n",
" save_directory=int8_model_path\n",
")"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion notebooks/openvino/stable_diffusion_optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
Loading

0 comments on commit 1dc6f4a

Please sign in to comment.