<p align="center">
<img src="readme_logo.png" />
</p>
# Optimum Intel
🤗 Optimum Intel is the interface between the 🤗 Transformers and Diffusers libraries and the different tools and libraries provided by Intel to accelerate end-to-end pipelines on Intel architectures.
[Intel Extension for PyTorch](https://intel.github.io/intel-extension-for-pytorch/#introduction) is an open-source library which provides optimizations for both eager mode and graph mode, however, compared to eager mode, graph mode in PyTorch* normally yields better performance from optimization techniques, such as operation fusion.
Intel [Neural Compressor](https://www.intel.com/content/www/us/en/developer/tools/oneapi/neural-compressor.html) is an open-source library enabling the usage of the most popular compression techniques such as quantization, pruning and knowledge distillation. It supports automatic accuracy-driven tuning strategies in order for users to easily generate quantized model. The users can easily apply static, dynamic and aware-training quantization approaches while giving an expected accuracy criteria. It also supports different weight pruning techniques enabling the creation of pruned model giving a predefined sparsity target.
[OpenVINO](https://docs.openvino.ai) is an open-source toolkit that enables high performance inference capabilities for Intel CPUs, GPUs, and special DL inference accelerators ([see](https://docs.openvino.ai/2024/about-openvino/compatibility-and-support/supported-devices.html) the full list of supported devices). It is supplied with a set of tools to optimize your models with compression techniques such as quantization, pruning and knowledge distillation. Optimum Intel provides a simple interface to optimize your Transformers and Diffusers models, convert them to the OpenVINO Intermediate Representation (IR) format and run inference using OpenVINO Runtime.
## Installation
To install the latest release of 🤗 Optimum Intel with the corresponding required dependencies, you can use `pip` as follows:
| Accelerator | Installation |
|:-----------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------|
| [Intel Neural Compressor](https://www.intel.com/content/www/us/en/developer/tools/oneapi/neural-compressor.html) | `pip install --upgrade --upgrade-strategy eager "optimum[neural-compressor]"` |
| [OpenVINO](https://docs.openvino.ai) | `pip install --upgrade --upgrade-strategy eager "optimum[openvino]"` |
| [Intel Extension for PyTorch](https://intel.github.io/intel-extension-for-pytorch/#introduction) | `pip install --upgrade --upgrade-strategy eager "optimum[ipex]"` |
The `--upgrade-strategy eager` option is needed to ensure `optimum-intel` is upgraded to the latest version.
We recommend creating a [virtual environment](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment) and upgrading
pip with `python -m pip install --upgrade pip`.
Optimum Intel is a fast-moving project, and you may want to install from source with the following command:
```bash
python -m pip install git+https://github.com/huggingface/optimum-intel.git
```
or to install from source including dependencies:
```bash
python -m pip install "optimum-intel[extras]"@git+https://github.com/huggingface/optimum-intel.git
```
where `extras` can be one or more of `ipex`, `neural-compressor`, `openvino`, `nncf`.
# Quick tour
## Neural Compressor
Dynamic quantization can be used through the Optimum command-line interface:
```bash
optimum-cli inc quantize --model distilbert-base-cased-distilled-squad --output ./quantized_distilbert
```
Note that quantization is currently only supported for CPUs (only CPU backends are available), so we will not be utilizing GPUs / CUDA in this example.
To load a quantized model hosted locally or on the 🤗 hub, you can do as follows :
```python
from optimum.intel import INCModelForSequenceClassification
model_id = "Intel/distilbert-base-uncased-finetuned-sst-2-english-int8-dynamic"
model = INCModelForSequenceClassification.from_pretrained(model_id)
```
You can load many more quantized models hosted on the hub under the Intel organization [`here`](https://huggingface.co/Intel).
For more details on the supported compression techniques, please refer to the [documentation](https://huggingface.co/docs/optimum/main/en/intel/optimization_inc).
## OpenVINO
Below are examples of how to use OpenVINO and its [NNCF](https://docs.openvino.ai/2024/openvino-workflow/model-optimization-guide/compressing-models-during-training.html) framework to accelerate inference.
#### Export:
It is also possible to export your model to the [OpenVINO IR](https://docs.openvino.ai/2024/documentation/openvino-ir-format.html) format with the CLI :
```plain
optimum-cli export openvino --model gpt2 ov_model
```
You can also apply 8-bit weight-only quantization when exporting your model : the model linear, embedding and convolution weights will be quantized to INT8, the activations will be kept in floating point precision.
```plain
optimum-cli export openvino --model gpt2 --weight-format int8 ov_model
```
Quantization in hybrid mode can be applied to Stable Diffusion pipeline during model export. This involves applying hybrid post-training quantization to the UNet model and weight-only quantization for the rest of the pipeline components. In the hybrid mode, weights in MatMul and Embedding layers are quantized, as well as activations of other layers.
```plain
optimum-cli export openvino --model stabilityai/stable-diffusion-2-1 --dataset conceptual_captions --weight-format int8 ov_model
```
To apply quantization on both weights and activations, you can find more information in the [documentation](https://huggingface.co/docs/optimum/main/en/intel/optimization_ov).
#### Inference:
To load a model and run inference with OpenVINO Runtime, you can just replace your `AutoModelForXxx` class with the corresponding `OVModelForXxx` class.
```diff
- from transformers import AutoModelForSeq2SeqLM
+ from optimum.intel import OVModelForSeq2SeqLM
from transformers import AutoTokenizer, pipeline
model_id = "echarlaix/t5-small-openvino"
- model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
+ model = OVModelForSeq2SeqLM.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("translation_en_to_fr", model=model, tokenizer=tokenizer)
results = pipe("He never went out without a book under his arm, and he often came back with two.")
[{'translation_text': "Il n'est jamais sorti sans un livre sous son bras, et il est souvent revenu avec deux."}]
```
If you want to load a PyTorch checkpoint, set `export=True` to convert your model to the OpenVINO IR.
```python
from optimum.intel import OVModelForCausalLM
model = OVModelForCausalLM.from_pretrained("gpt2", export=True)
model.save_pretrained("./ov_model")
```
#### Post-training static quantization:
Post-training static quantization introduces an additional calibration step where data is fed through the network in order to compute the activations quantization parameters. Here is an example on how to apply static quantization on a fine-tuned DistilBERT.
```python
from functools import partial
from optimum.intel import OVQuantizer, OVModelForSequenceClassification, OVConfig, OVQuantizationConfig
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_id = "distilbert-base-uncased-finetuned-sst-2-english"
model = OVModelForSequenceClassification.from_pretrained(model_id, export=True)
tokenizer = AutoTokenizer.from_pretrained(model_id)
def preprocess_fn(examples, tokenizer):
return tokenizer(
examples["sentence"], padding=True, truncation=True, max_length=128
)
quantizer = OVQuantizer.from_pretrained(model)
calibration_dataset = quantizer.get_calibration_dataset(
"glue",
dataset_config_name="sst2",
preprocess_function=partial(preprocess_fn, tokenizer=tokenizer),
num_samples=100,
dataset_split="train",
preprocess_batch=True,
)
# The directory where the quantized model will be saved
save_dir = "nncf_results"
# Apply static quantization and save the resulting model in the OpenVINO IR format
ov_config = OVConfig(quantization_config=OVQuantizationConfig())
quantizer.quantize(ov_config=ov_config, calibration_dataset=calibration_dataset, save_directory=save_dir)
# Load the quantized model
optimized_model = OVModelForSequenceClassification.from_pretrained(save_dir)
```
## IPEX
To load your IPEX model, you can just replace your `AutoModelForXxx` class with the corresponding `IPEXModelForXxx` class. You can set `export=True` to load a PyTorch checkpoint, export your model via TorchScript and apply IPEX optimizations : both operators optimization (replaced with customized IPEX operators) and graph-level optimization (like operators fusion) will be applied on your model.
```diff
from transformers import AutoTokenizer, pipeline
- from transformers import AutoModelForCausalLM
+ from optimum.intel import IPEXModelForCausalLM
model_id = "gpt2"
- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16)
+ model = IPEXModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, export=True)
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
results = pipe("He's a dreadful magician and")
```
For more details, please refer to the [documentation](https://intel.github.io/intel-extension-for-pytorch/#introduction).
## Running the examples
Check out the [`examples`](https://github.com/huggingface/optimum-intel/tree/main/examples) and [`notebooks`](https://github.com/huggingface/optimum-intel/tree/main/notebooks) directory to see how 🤗 Optimum Intel can be used to optimize models and accelerate inference.
Do not forget to install requirements for every example:
```
cd <example-folder>
pip install -r requirements.txt
```
## Gaudi
To train your model on [Intel Gaudi AI Accelerators (HPU)](https://docs.habana.ai/en/latest/index.html), check out [Optimum Habana](https://github.com/huggingface/optimum-habana) which provides a set of tools enabling easy model loading, training and inference on single- and multi-HPU settings for different downstream tasks. After training your model, feel free to submit it to the Intel [leaderboard](https://huggingface.co/spaces/Intel/powered_by_intel_llm_leaderboard) which is designed to evaluate, score, and rank open-source LLMs that have been pre-trained or fine-tuned on Intel Hardwares. Models submitted to the leaderboard will be evaluated on the Intel Developer Cloud. The evaluation platform consists of Gaudi Accelerators and Xeon CPUs running benchmarks from the Eleuther AI Language Model Evaluation Harness.
Raw data
{
"_id": null,
"home_page": "https://www.intel.com",
"name": "optimum-intel",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "transformers, quantization, pruning, knowledge distillation, optimization, training",
"author": "HuggingFace Inc. Special Ops Team",
"author_email": "hardware@huggingface.co",
"download_url": "https://files.pythonhosted.org/packages/28/5c/4696003eb93e0586f6ec92259f41667b3d45e87e28239b529bd84863e6be/optimum-intel-1.20.1.tar.gz",
"platform": null,
"description": "<p align=\"center\">\n <img src=\"readme_logo.png\" />\n</p>\n\n# Optimum Intel\n\n\ud83e\udd17 Optimum Intel is the interface between the \ud83e\udd17 Transformers and Diffusers libraries and the different tools and libraries provided by Intel to accelerate end-to-end pipelines on Intel architectures.\n\n[Intel Extension for PyTorch](https://intel.github.io/intel-extension-for-pytorch/#introduction) is an open-source library which provides optimizations for both eager mode and graph mode, however, compared to eager mode, graph mode in PyTorch* normally yields better performance from optimization techniques, such as operation fusion.\n\nIntel [Neural Compressor](https://www.intel.com/content/www/us/en/developer/tools/oneapi/neural-compressor.html) is an open-source library enabling the usage of the most popular compression techniques such as quantization, pruning and knowledge distillation. It supports automatic accuracy-driven tuning strategies in order for users to easily generate quantized model. The users can easily apply static, dynamic and aware-training quantization approaches while giving an expected accuracy criteria. It also supports different weight pruning techniques enabling the creation of pruned model giving a predefined sparsity target.\n\n[OpenVINO](https://docs.openvino.ai) is an open-source toolkit that enables high performance inference capabilities for Intel CPUs, GPUs, and special DL inference accelerators ([see](https://docs.openvino.ai/2024/about-openvino/compatibility-and-support/supported-devices.html) the full list of supported devices). It is supplied with a set of tools to optimize your models with compression techniques such as quantization, pruning and knowledge distillation. Optimum Intel provides a simple interface to optimize your Transformers and Diffusers models, convert them to the OpenVINO Intermediate Representation (IR) format and run inference using OpenVINO Runtime.\n\n\n## Installation\n\nTo install the latest release of \ud83e\udd17 Optimum Intel with the corresponding required dependencies, you can use `pip` as follows:\n\n| Accelerator | Installation |\n|:-----------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------|\n| [Intel Neural Compressor](https://www.intel.com/content/www/us/en/developer/tools/oneapi/neural-compressor.html) | `pip install --upgrade --upgrade-strategy eager \"optimum[neural-compressor]\"` |\n| [OpenVINO](https://docs.openvino.ai) | `pip install --upgrade --upgrade-strategy eager \"optimum[openvino]\"` |\n| [Intel Extension for PyTorch](https://intel.github.io/intel-extension-for-pytorch/#introduction) | `pip install --upgrade --upgrade-strategy eager \"optimum[ipex]\"` |\n\nThe `--upgrade-strategy eager` option is needed to ensure `optimum-intel` is upgraded to the latest version.\n\nWe recommend creating a [virtual environment](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment) and upgrading\npip with `python -m pip install --upgrade pip`.\n\nOptimum Intel is a fast-moving project, and you may want to install from source with the following command:\n\n```bash\npython -m pip install git+https://github.com/huggingface/optimum-intel.git\n```\n\nor to install from source including dependencies:\n\n```bash\npython -m pip install \"optimum-intel[extras]\"@git+https://github.com/huggingface/optimum-intel.git\n```\n\nwhere `extras` can be one or more of `ipex`, `neural-compressor`, `openvino`, `nncf`.\n\n# Quick tour\n\n## Neural Compressor\n\nDynamic quantization can be used through the Optimum command-line interface:\n\n```bash\noptimum-cli inc quantize --model distilbert-base-cased-distilled-squad --output ./quantized_distilbert\n```\nNote that quantization is currently only supported for CPUs (only CPU backends are available), so we will not be utilizing GPUs / CUDA in this example.\n\nTo load a quantized model hosted locally or on the \ud83e\udd17 hub, you can do as follows :\n```python\nfrom optimum.intel import INCModelForSequenceClassification\n\nmodel_id = \"Intel/distilbert-base-uncased-finetuned-sst-2-english-int8-dynamic\"\nmodel = INCModelForSequenceClassification.from_pretrained(model_id)\n```\n\nYou can load many more quantized models hosted on the hub under the Intel organization [`here`](https://huggingface.co/Intel).\n\nFor more details on the supported compression techniques, please refer to the [documentation](https://huggingface.co/docs/optimum/main/en/intel/optimization_inc).\n\n\n## OpenVINO\n\nBelow are examples of how to use OpenVINO and its [NNCF](https://docs.openvino.ai/2024/openvino-workflow/model-optimization-guide/compressing-models-during-training.html) framework to accelerate inference.\n\n#### Export:\n\nIt is also possible to export your model to the [OpenVINO IR](https://docs.openvino.ai/2024/documentation/openvino-ir-format.html) format with the CLI :\n\n```plain\noptimum-cli export openvino --model gpt2 ov_model\n```\n\nYou can also apply 8-bit weight-only quantization when exporting your model : the model linear, embedding and convolution weights will be quantized to INT8, the activations will be kept in floating point precision.\n\n```plain\noptimum-cli export openvino --model gpt2 --weight-format int8 ov_model\n```\n\nQuantization in hybrid mode can be applied to Stable Diffusion pipeline during model export. This involves applying hybrid post-training quantization to the UNet model and weight-only quantization for the rest of the pipeline components. In the hybrid mode, weights in MatMul and Embedding layers are quantized, as well as activations of other layers.\n\n```plain\noptimum-cli export openvino --model stabilityai/stable-diffusion-2-1 --dataset conceptual_captions --weight-format int8 ov_model\n```\n\nTo apply quantization on both weights and activations, you can find more information in the [documentation](https://huggingface.co/docs/optimum/main/en/intel/optimization_ov).\n\n#### Inference:\n\nTo load a model and run inference with OpenVINO Runtime, you can just replace your `AutoModelForXxx` class with the corresponding `OVModelForXxx` class.\n\n\n```diff\n- from transformers import AutoModelForSeq2SeqLM\n+ from optimum.intel import OVModelForSeq2SeqLM\n from transformers import AutoTokenizer, pipeline\n\n model_id = \"echarlaix/t5-small-openvino\"\n- model = AutoModelForSeq2SeqLM.from_pretrained(model_id)\n+ model = OVModelForSeq2SeqLM.from_pretrained(model_id)\n tokenizer = AutoTokenizer.from_pretrained(model_id)\n pipe = pipeline(\"translation_en_to_fr\", model=model, tokenizer=tokenizer)\n results = pipe(\"He never went out without a book under his arm, and he often came back with two.\")\n\n [{'translation_text': \"Il n'est jamais sorti sans un livre sous son bras, et il est souvent revenu avec deux.\"}]\n```\n\nIf you want to load a PyTorch checkpoint, set `export=True` to convert your model to the OpenVINO IR.\n\n```python\nfrom optimum.intel import OVModelForCausalLM\n\nmodel = OVModelForCausalLM.from_pretrained(\"gpt2\", export=True)\nmodel.save_pretrained(\"./ov_model\")\n```\n\n\n#### Post-training static quantization:\n\nPost-training static quantization introduces an additional calibration step where data is fed through the network in order to compute the activations quantization parameters. Here is an example on how to apply static quantization on a fine-tuned DistilBERT.\n\n```python\nfrom functools import partial\nfrom optimum.intel import OVQuantizer, OVModelForSequenceClassification, OVConfig, OVQuantizationConfig\nfrom transformers import AutoTokenizer, AutoModelForSequenceClassification\n\nmodel_id = \"distilbert-base-uncased-finetuned-sst-2-english\"\nmodel = OVModelForSequenceClassification.from_pretrained(model_id, export=True)\ntokenizer = AutoTokenizer.from_pretrained(model_id)\ndef preprocess_fn(examples, tokenizer):\n return tokenizer(\n examples[\"sentence\"], padding=True, truncation=True, max_length=128\n )\n\nquantizer = OVQuantizer.from_pretrained(model)\ncalibration_dataset = quantizer.get_calibration_dataset(\n \"glue\",\n dataset_config_name=\"sst2\",\n preprocess_function=partial(preprocess_fn, tokenizer=tokenizer),\n num_samples=100,\n dataset_split=\"train\",\n preprocess_batch=True,\n)\n# The directory where the quantized model will be saved\nsave_dir = \"nncf_results\"\n# Apply static quantization and save the resulting model in the OpenVINO IR format\nov_config = OVConfig(quantization_config=OVQuantizationConfig())\nquantizer.quantize(ov_config=ov_config, calibration_dataset=calibration_dataset, save_directory=save_dir)\n# Load the quantized model\noptimized_model = OVModelForSequenceClassification.from_pretrained(save_dir)\n```\n\n\n## IPEX\nTo load your IPEX model, you can just replace your `AutoModelForXxx` class with the corresponding `IPEXModelForXxx` class. You can set `export=True` to load a PyTorch checkpoint, export your model via TorchScript and apply IPEX optimizations : both operators optimization (replaced with customized IPEX operators) and graph-level optimization (like operators fusion) will be applied on your model.\n```diff\n from transformers import AutoTokenizer, pipeline\n- from transformers import AutoModelForCausalLM\n+ from optimum.intel import IPEXModelForCausalLM\n\n\n model_id = \"gpt2\"\n- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16)\n+ model = IPEXModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, export=True)\n tokenizer = AutoTokenizer.from_pretrained(model_id)\n pipe = pipeline(\"text-generation\", model=model, tokenizer=tokenizer)\n results = pipe(\"He's a dreadful magician and\")\n```\n\nFor more details, please refer to the [documentation](https://intel.github.io/intel-extension-for-pytorch/#introduction).\n\n\n## Running the examples\n\nCheck out the [`examples`](https://github.com/huggingface/optimum-intel/tree/main/examples) and [`notebooks`](https://github.com/huggingface/optimum-intel/tree/main/notebooks) directory to see how \ud83e\udd17 Optimum Intel can be used to optimize models and accelerate inference.\n\nDo not forget to install requirements for every example:\n\n```\ncd <example-folder>\npip install -r requirements.txt\n```\n\n\n## Gaudi\n\nTo train your model on [Intel Gaudi AI Accelerators (HPU)](https://docs.habana.ai/en/latest/index.html), check out [Optimum Habana](https://github.com/huggingface/optimum-habana) which provides a set of tools enabling easy model loading, training and inference on single- and multi-HPU settings for different downstream tasks. After training your model, feel free to submit it to the Intel [leaderboard](https://huggingface.co/spaces/Intel/powered_by_intel_llm_leaderboard) which is designed to evaluate, score, and rank open-source LLMs that have been pre-trained or fine-tuned on Intel Hardwares. Models submitted to the leaderboard will be evaluated on the Intel Developer Cloud. The evaluation platform consists of Gaudi Accelerators and Xeon CPUs running benchmarks from the Eleuther AI Language Model Evaluation Harness.\n",
"bugtrack_url": null,
"license": "Apache",
"summary": "Optimum Library is an extension of the Hugging Face Transformers library, providing a framework to integrate third-party libraries from Hardware Partners and interface with their specific functionality.",
"version": "1.20.1",
"project_urls": {
"Homepage": "https://www.intel.com"
},
"split_keywords": [
"transformers",
" quantization",
" pruning",
" knowledge distillation",
" optimization",
" training"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4e5b0feccc562231cf6b8be5bee49c9a1f5bd8e4a52b30a664ce694b13bc933e",
"md5": "7ea23623e35da2f7bec329d58e294eaf",
"sha256": "fafb7d1afa48bf4fbeef51af0ce748ce7c132aff223d925333e2a58e1fa7cd3e"
},
"downloads": -1,
"filename": "optimum_intel-1.20.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7ea23623e35da2f7bec329d58e294eaf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 258484,
"upload_time": "2024-10-29T23:51:49",
"upload_time_iso_8601": "2024-10-29T23:51:49.768588Z",
"url": "https://files.pythonhosted.org/packages/4e/5b/0feccc562231cf6b8be5bee49c9a1f5bd8e4a52b30a664ce694b13bc933e/optimum_intel-1.20.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "285c4696003eb93e0586f6ec92259f41667b3d45e87e28239b529bd84863e6be",
"md5": "b0fae90fba1b594c2dddbfc33e8b94bc",
"sha256": "993d75300c10d6e38c77c02889fb5472fae5faba273b3cddc336b5a44c159ccd"
},
"downloads": -1,
"filename": "optimum-intel-1.20.1.tar.gz",
"has_sig": false,
"md5_digest": "b0fae90fba1b594c2dddbfc33e8b94bc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 217888,
"upload_time": "2024-10-29T23:51:52",
"upload_time_iso_8601": "2024-10-29T23:51:52.190554Z",
"url": "https://files.pythonhosted.org/packages/28/5c/4696003eb93e0586f6ec92259f41667b3d45e87e28239b529bd84863e6be/optimum-intel-1.20.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-29 23:51:52",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "optimum-intel"
}