diff --git a/docs/Dockerfile b/docs/Dockerfile
index 7cfa6cd514..4acde4e659 100644
--- a/docs/Dockerfile
+++ b/docs/Dockerfile
@@ -25,4 +25,4 @@ RUN npm install npm@9.8.1 -g && \
RUN python3 -m pip install --no-cache-dir --upgrade pip
RUN python3 -m pip install --no-cache-dir git+https://github.com/huggingface/doc-builder.git
RUN git clone $clone_url && cd optimum-intel && git checkout $commit_sha
-RUN python3 -m pip install --no-cache-dir ./optimum-intel[neural-compressor,openvino,nncf,quality]
+RUN python3 -m pip install --no-cache-dir ./optimum-intel[neural-compressor,openvino,diffusers,quality]
diff --git a/docs/source/_toctree.yml b/docs/source/_toctree.yml
index 1fd3fe6b71..7053a17ef2 100644
--- a/docs/source/_toctree.yml
+++ b/docs/source/_toctree.yml
@@ -22,6 +22,13 @@
title: Supported Models
- local: openvino/reference
title: Reference
+ - sections:
+ - local: openvino/tutorials/notebooks
+ title: Notebooks
+ - local: openvino/tutorials/diffusers
+ title: Generate images with Diffusion models
+ title: Tutorials
+ isExpanded: false
title: OpenVINO
title: Optimum Intel
isExpanded: false
diff --git a/docs/source/openvino/export.mdx b/docs/source/openvino/export.mdx
index 8cffd06121..eed980076d 100644
--- a/docs/source/openvino/export.mdx
+++ b/docs/source/openvino/export.mdx
@@ -14,25 +14,15 @@ specific language governing permissions and limitations under the License.
To export your model to the [OpenVINO IR](https://docs.openvino.ai/2024/documentation/openvino-ir-format.html) format with the CLI :
```bash
-optimum-cli export openvino --model gpt2 ov_model/
+optimum-cli export openvino --model meta-llama/Meta-Llama-3-8B ov_model/
```
The model argument can either be the model ID of a model hosted on the [Hub](https://huggingface.co/models) or a path to a model hosted locally. For local models, you need to specify the task for which the model should be loaded before export, among the list of the [supported tasks](https://huggingface.co/docs/optimum/main/en/exporters/task_manager).
-
```bash
-optimum-cli export openvino --model local_model_dir --task text-generation-with-past ov_model/
+optimum-cli export openvino --model local_llama --task text-generation-with-past ov_model/
```
-The `-with-past` suffix enable the re-use of past keys and values. This allows to avoid recomputing the same intermediate activations during the generation. to export the model without, you will need to remove this suffix.
-
-| With K-V cache | Without K-V cache |
-|------------------------------------------|--------------------------------------|
-| `text-generation-with-past` | `text-generation` |
-| `text2text-generation-with-past` | `text2text-generation` |
-| `automatic-speech-recognition-with-past` | `automatic-speech-recognition` |
-
-
Check out the help for more options:
```bash
@@ -97,7 +87,7 @@ Optional arguments:
You can also apply fp16, 8-bit or 4-bit weight-only quantization on the Linear, Convolutional and Embedding layers when exporting your model by setting `--weight-format` to respectively `fp16`, `int8` or `int4`:
```bash
-optimum-cli export openvino --model gpt2 --weight-format int8 ov_model/
+optimum-cli export openvino --model meta-llama/Meta-Llama-3-8B --weight-format int8 ov_model/
```
For more information on the quantization parameters checkout the [documentation](inference#weight-only-quantization)
@@ -109,6 +99,33 @@ Models larger than 1 billion parameters are exported to the OpenVINO format with
+
+### Decoder models
+
+For models with a decoder, we enable the re-use of past keys and values by default. This allows to avoid recomputing the same intermediate activations at each generation step. To export the model without, you will need to remove the `-with-past` suffix when specifying the task.
+
+| With K-V cache | Without K-V cache |
+|------------------------------------------|--------------------------------------|
+| `text-generation-with-past` | `text-generation` |
+| `text2text-generation-with-past` | `text2text-generation` |
+| `automatic-speech-recognition-with-past` | `automatic-speech-recognition` |
+
+
+### Diffusion models
+
+When Stable Diffusion models are exported to the OpenVINO format, they are decomposed into different components that are later combined during inference:
+
+* Text encoder(s)
+* U-Net
+* VAE encoder
+* VAE decoder
+
+To export your Stable Diffusion XL model to the OpenVINO IR format with the CLI you can do as follows:
+
+```bash
+optimum-cli export openvino --model stabilityai/stable-diffusion-xl-base-1.0 ov_sdxl/
+```
+
## When loading your model
You can also load your PyTorch checkpoint and convert it to the OpenVINO format on-the-fly, by setting `export=True` when loading your model.
@@ -121,7 +138,7 @@ To easily save the resulting model, you can use the `save_pretrained()` method,
+ from optimum.intel import OVModelForCausalLM
from transformers import AutoTokenizer
- model_id = "gpt2"
+ model_id = "meta-llama/Meta-Llama-3-8B"
- model = AutoModelForCausalLM.from_pretrained(model_id)
+ model = OVModelForCausalLM.from_pretrained(model_id, export=True)
tokenizer = AutoTokenizer.from_pretrained(model_id)
@@ -137,7 +154,7 @@ To easily save the resulting model, you can use the `save_pretrained()` method,
from transformers import AutoModelForCausalLM
from optimum.exporters.openvino import export_from_model
-model = AutoModelForCausalLM.from_pretrained("gpt2")
+model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B")
export_from_model(model, output="ov_model", task="text-generation-with-past")
```
diff --git a/docs/source/openvino/inference.mdx b/docs/source/openvino/inference.mdx
index 822d6e2f99..0f2e30af47 100644
--- a/docs/source/openvino/inference.mdx
+++ b/docs/source/openvino/inference.mdx
@@ -11,9 +11,12 @@ specific language governing permissions and limitations under the License.
Optimum Intel can be used to load optimized models from the [Hub](https://huggingface.co/models?library=openvino&sort=downloads) and create pipelines to run inference with OpenVINO Runtime on a variety of Intel processors ([see](https://docs.openvino.ai/2024/about-openvino/compatibility-and-support/supported-devices.html) the full list of supported devices)
-## Transformers models
-Once [your model was exported](export), you can load it by replacing the `AutoModelForXxx` class with the corresponding `OVModelForXxx` class.
+## Loading
+
+### Transformers models
+
+Once [your model was exported](export), you can load it by replacing the `AutoModelForXxx` class with the corresponding `OVModelForXxx`.
```diff
- from transformers import AutoModelForCausalLM
@@ -22,403 +25,157 @@ Once [your model was exported](export), you can load it by replacing the `AutoMo
model_id = "helenai/gpt2-ov"
- model = AutoModelForCausalLM.from_pretrained(model_id)
+ # here the model was already exported so no need to set export=True
+ model = OVModelForCausalLM.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
results = pipe("He's a dreadful magician and")
```
-See the [reference documentation](reference) for more information about parameters, and examples for different tasks.
-
As shown in the table below, each task is associated with a class enabling to automatically load your model.
-| Task | Auto Class |
+| Auto Class | Task |
|--------------------------------------|--------------------------------------|
-| `text-classification` | `OVModelForSequenceClassification` |
-| `token-classification` | `OVModelForTokenClassification` |
-| `question-answering` | `OVModelForQuestionAnswering` |
-| `audio-classification` | `OVModelForAudioClassification` |
-| `image-classification` | `OVModelForImageClassification` |
-| `feature-extraction` | `OVModelForFeatureExtraction` |
-| `fill-mask` | `OVModelForMaskedLM` |
-| `image-classification` | `OVModelForImageClassification` |
-| `audio-classification` | `OVModelForAudioClassification` |
-| `text-generation-with-past` | `OVModelForCausalLM` |
-| `text2text-generation-with-past` | `OVModelForSeq2SeqLM` |
-| `automatic-speech-recognition` | `OVModelForSpeechSeq2Seq` |
-| `image-to-text` | `OVModelForVision2Seq` |
-
-
-### Weight-only quantization
-
-You can also apply fp16, 8-bit or 4-bit weight compression on the Linear, Convolutional and Embedding layers when loading your model to reduce the memory footprint and inference latency.
-
-For more information on the quantization parameters checkout the [documentation](optimziation#weight-only-quantization).
+| `OVModelForSequenceClassification` | `text-classification` |
+| `OVModelForTokenClassification` | `token-classification` |
+| `OVModelForQuestionAnswering` | `question-answering` |
+| `OVModelForAudioClassification` | `audio-classification` |
+| `OVModelForImageClassification` | `image-classification` |
+| `OVModelForFeatureExtraction` | `feature-extraction` |
+| `OVModelForMaskedLM` | `fill-mask` |
+| `OVModelForImageClassification` | `image-classification` |
+| `OVModelForAudioClassification` | `audio-classification` |
+| `OVModelForCausalLM` | `text-generation-with-past` |
+| `OVModelForSeq2SeqLM` | `text2text-generation-with-past` |
+| `OVModelForSpeechSeq2Seq` | `automatic-speech-recognition` |
+| `OVModelForVision2Seq` | `image-to-text` |
+
+
+### Diffusers models
-
+Make sure you have 🤗 Diffusers installed. To install `diffusers`:
-If not specified, `load_in_8bit` will be set to `True` by default when models larger than 1 billion parameters are exported to the OpenVINO format (with `export=True`). You can disable it with `load_in_8bit=False`.
+```bash
+pip install optimum[diffusers]
+```
-
+```diff
+- from diffusers import StableDiffusionPipeline
++ from optimum.intel import OVStableDiffusionPipeline
-It's also possible to apply quantization on both weights and activations using the `OVQuantizer`, more information in the [documentation](optimization#static-quantization).
+ model_id = "echarlaix/stable-diffusion-v1-5-openvino"
+- pipeline = StableDiffusionPipeline.from_pretrained(model_id)
++ pipeline = OVStableDiffusionPipeline.from_pretrained(model_id)
+ prompt = "sailing ship in storm by Rembrandt"
+ images = pipeline(prompt).images
+```
-### Static shape
-By default, `OVModelForXxx` support dynamic shapes, enabling inputs of every shapes. To speed up inference, static shapes can be enabled by giving the desired inputs shapes.
+As shown in the table below, each task is associated with a class enabling to automatically load your model.
-```python
-# Fix the batch size to 1 and the sequence length to 9
-model.reshape(1, 9)
-# Compile the model before the first inference
-model.compile()
-```
+| Auto Class | Task |
+|--------------------------------------|--------------------------------------|
+| `OVStableDiffusionPipeline` | `text-to-image` |
+| `OVStableDiffusionImg2ImgPipeline` | `image-to-image` |
+| `OVStableDiffusionInpaintPipeline` | `inpaint` |
+| `OVStableDiffusionXLPipeline` | `text-to-image` |
+| `OVStableDiffusionXLImg2ImgPipeline` | `image-to-image` |
+| `OVLatentConsistencyModelPipeline` | `text-to-image` |
-When fixing the shapes with the `reshape()` method, inference cannot be performed with an input of a different shape. When instantiating your pipeline, you can specify the maximum total input sequence length after tokenization in order for shorter sequences to be padded and for longer sequences to be truncated.
-```python
-from datasets import load_dataset
-from transformers import AutoTokenizer, pipeline
-from evaluate import evaluator
-from optimum.intel import OVModelForQuestionAnswering
+See the [reference documentation](reference) for more information about parameters, and examples for different tasks.
-model_id = "distilbert-base-cased-distilled-squad"
-model = OVModelForQuestionAnswering.from_pretrained(model_id, export=True)
-model.reshape(1, 384)
-tokenizer = AutoTokenizer.from_pretrained(model_id)
-eval_dataset = load_dataset("squad", split="validation").select(range(50))
-task_evaluator = evaluator("question-answering")
-qa_pipe = pipeline(
- "question-answering",
- model=model,
- tokenizer=tokenizer,
- max_seq_len=384,
- padding="max_length",
- truncation=True,
-)
-metric = task_evaluator.compute(model_or_pipeline=qa_pipe, data=eval_dataset, metric="squad")
-```
-### Compilation
+## Compilation
-By default the model will be compiled when instantiating our `OVModel`. In the case where the model is reshaped or placed to another device, the model will need to be recompiled again, which will happen by default before the first inference (thus inflating the latency of the first inference). To avoid an unnecessary compilation, you can disable the first compilation by setting `compile=False`. The model can be compiled before the first inference with `model.compile()`.
+By default the model will be compiled when instantiating an `OVModel`. In the case where the model is reshaped or placed to another device, the model will need to be recompiled again, which will happen by default before the first inference (thus inflating the latency of the first inference). To avoid an unnecessary compilation, you can disable the first compilation by setting `compile=False`.
```python
-from optimum.intel import OVModelForSequenceClassification
+from optimum.intel import OVModelForQuestionAnswering
-model_id = "distilbert-base-uncased-finetuned-sst-2-english"
+model_id = "distilbert/distilbert-base-cased-distilled-squad"
# Load the model and disable the model compilation
-model = OVModelForSequenceClassification.from_pretrained(model_id, export=True, compile=False)
-# Reshape to a static sequence length of 128
-model.reshape(1,128)
-# Compile the model before the first inference
-model.compile()
+model = OVModelForQuestionAnswering.from_pretrained(model_id, compile=False)
```
To run inference on Intel integrated or discrete GPU, use `.to("gpu")`. On GPU, models run in FP16 precision by default. (See [OpenVINO documentation](https://docs.openvino.ai/2024/get-started/configurations/configurations-intel-gpu.html) about installing drivers for GPU inference).
```python
-# Static shapes speed up inference
-model.reshape(1, 9)
model.to("gpu")
-# Compile the model before the first inference
-model.compile()
-```
-
-### Configuration
-
-
-It is possible to pass an `ov_config` parameter to `from_pretrained()` with custom OpenVINO configuration values. This can be used for example to enable full precision inference on devices where FP16 or BF16 inference precision is used by default.
-
-
-```python
-model = OVModelForSequenceClassification.from_pretrained(model_id, ov_config={"INFERENCE_PRECISION_HINT":"f32"})
-```
-
-Optimum Intel leverages OpenVINO's model caching to speed up model compiling on GPU. By default a `model_cache` directory is created in the model's directory in the [Hugging Face Hub cache](https://huggingface.co/docs/huggingface_hub/main/en/guides/manage-cache). To override this, use the ov_config parameter and set `CACHE_DIR` to a different value. To disable model caching on GPU, set `CACHE_DIR` to an empty string.
-
-```python
-model = OVModelForSequenceClassification.from_pretrained(model_id, device="GPU", ov_config={"PERFORMANCE_HINT": "LATENCY", "CACHE_DIR":""})
-```
-
-### Sequence-to-sequence models
-
-Sequence-to-sequence (Seq2Seq) models, that generate a new sequence from an input, can also be used when running inference with OpenVINO. When Seq2Seq models are exported to the OpenVINO IR, they are decomposed into two parts : the encoder and the "decoder" (which actually consists of the decoder with the language modeling head), that are later combined during inference.
-To speed up sequential decoding, a cache with pre-computed key/values hidden-states will be used by default. An additional model component will be exported: the "decoder" with pre-computed key/values as one of its inputs. This specific export comes from the fact that during the first pass, the decoder has no pre-computed key/values hidden-states, while during the rest of the generation past key/values will be used to speed up sequential decoding. To disable this cache, set `use_cache=False` in the `from_pretrained()` method.
-
-Here is an example on how you can run inference for a translation task using a T5 model and then export it to OpenVINO IR:
-
-
-```python
-from transformers import AutoTokenizer, pipeline
-from optimum.intel import OVModelForSeq2SeqLM
-
-model_id = "t5-small"
-model = OVModelForSeq2SeqLM.from_pretrained(model_id, export=True)
-tokenizer = AutoTokenizer.from_pretrained(model_id)
-translation_pipe = pipeline("translation_en_to_fr", model=model, tokenizer=tokenizer)
-text = "He never went out without a book under his arm, and he often came back with two."
-result = translation_pipe(text)
-
-# Save the exported model
-save_directory = "openvino_t5"
-model.save_pretrained(save_directory)
-tokenizer.save_pretrained(save_directory)
-
-[{'translation_text': "Il n'est jamais sorti sans un livre sous son bras, et il est souvent revenu avec deux."}]
-```
-
-## Diffusers models
-
-Make sure you have 🤗 Diffusers installed. To install `diffusers`:
-
-```bash
-pip install optimum[diffusers]
```
-### Stable Diffusion
-
-Stable Diffusion models can also be used when running inference with OpenVINO. When Stable Diffusion models
-are exported to the OpenVINO format, they are decomposed into different components that are later combined during inference:
-- The text encoder
-- The U-NET
-- The VAE encoder
-- The VAE decoder
-
-| Task | Auto Class |
-|--------------------------------------|--------------------------------------|
-| `text-to-image` | `OVStableDiffusionPipeline` |
-| `image-to-image` | `OVStableDiffusionImg2ImgPipeline` |
-| `inpaint` | `OVStableDiffusionInpaintPipeline` |
-
-
-#### Text-to-Image
-Here is an example of how you can load an OpenVINO Stable Diffusion model and run inference using OpenVINO Runtime:
+The model can be compiled:
```python
-from optimum.intel import OVStableDiffusionPipeline
-
-model_id = "echarlaix/stable-diffusion-v1-5-openvino"
-pipeline = OVStableDiffusionPipeline.from_pretrained(model_id)
-prompt = "sailing ship in storm by Rembrandt"
-images = pipeline(prompt).images
+model.compile()
```
-To load your PyTorch model and convert it to OpenVINO on the fly, you can set `export=True`.
-
-```python
-model_id = "runwayml/stable-diffusion-v1-5"
-pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=True)
-# Don't forget to save the exported model
-pipeline.save_pretrained("openvino-sd-v1-5")
-```
+## Static shape
-To further speed up inference, the model can be statically reshaped :
+By default, dynamic shapes are supported, enabling inference for inputs of every shape. To speed up inference, static shapes can be enabled by giving the desired input shapes with [.reshape()](reference#optimum.intel.OVBaseModel.reshape).
```python
-# Define the shapes related to the inputs and desired outputs
-batch_size = 1
-num_images_per_prompt = 1
-height = 512
-width = 512
-
-# Statically reshape the model
-pipeline.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images_per_prompt)
-# Compile the model before the first inference
-pipeline.compile()
-
-# Run inference
-images = pipeline(prompt, height=height, width=width, num_images_per_prompt=num_images_per_prompt).images
+# Fix the batch size to 1 and the sequence length to 40
+batch_size, seq_len = 1, 40
+model.reshape(batch_size, seq_len)
```
-In case you want to change any parameters such as the outputs height or width, you'll need to statically reshape your model once again.
-
-
-
-
-
-#### Text-to-Image with Textual Inversion
-Here is an example of how you can load an OpenVINO Stable Diffusion model with pre-trained textual inversion embeddings and run inference using OpenVINO Runtime:
-
-
-First, you can run original pipeline without textual inversion
-```python
-from optimum.intel import OVStableDiffusionPipeline
-import numpy as np
-
-model_id = "echarlaix/stable-diffusion-v1-5-openvino"
-prompt = "A back-pack"
-# Set a random seed for better comparison
-np.random.seed(42)
-
-pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=False, compile=False)
-pipeline.compile()
-image1 = pipeline(prompt, num_inference_steps=50).images[0]
-image1.save("stable_diffusion_v1_5_without_textual_inversion.png")
-```
+When fixing the shapes with the `reshape()` method, inference cannot be performed with an input of a different shape.
-Then, you can load [sd-concepts-library/cat-toy](https://huggingface.co/sd-concepts-library/cat-toy) textual inversion embedding and run pipeline with same prompt again
```python
-# Reset stable diffusion pipeline
-pipeline.clear_requests()
-# Load textual inversion into stable diffusion pipeline
-pipeline.load_textual_inversion("sd-concepts-library/cat-toy", "")
+from transformers import AutoTokenizer
+from optimum.intel import OVModelForQuestionAnswering
+model_id = "distilbert/distilbert-base-cased-distilled-squad"
+model = OVModelForQuestionAnswering.from_pretrained(model_id, compile=False)
+tokenizer = AutoTokenizer.from_pretrained(model_id)
+batch_size, seq_len = 1, 40
+model.reshape(batch_size, seq_len)
# Compile the model before the first inference
-pipeline.compile()
-image2 = pipeline(prompt, num_inference_steps=50).images[0]
-image2.save("stable_diffusion_v1_5_with_textual_inversion.png")
-```
-The left image shows the generation result of original stable diffusion v1.5, the right image shows the generation result of stable diffusion v1.5 with textual inversion.
-
-| | |
-|---|---|
-| ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/textual_inversion/stable_diffusion_v1_5_without_textual_inversion.png) | ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/textual_inversion/stable_diffusion_v1_5_with_textual_inversion.png) |
-
-
-#### Image-to-Image
-
-```python
-import requests
-import torch
-from PIL import Image
-from io import BytesIO
-from optimum.intel import OVStableDiffusionImg2ImgPipeline
-
-model_id = "runwayml/stable-diffusion-v1-5"
-pipeline = OVStableDiffusionImg2ImgPipeline.from_pretrained(model_id, export=True)
-
-url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
-response = requests.get(url)
-init_image = Image.open(BytesIO(response.content)).convert("RGB")
-init_image = init_image.resize((768, 512))
-prompt = "A fantasy landscape, trending on artstation"
-image = pipeline(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images[0]
-image.save("fantasy_landscape.png")
-```
-
-### Stable Diffusion XL
-
-| Task | Auto Class |
-|--------------------------------------|--------------------------------------|
-| `text-to-image` | `OVStableDiffusionXLPipeline` |
-| `image-to-image` | `OVStableDiffusionXLImg2ImgPipeline` |
-
-
-#### Text-to-Image
-
-Here is an example of how you can load a SDXL OpenVINO model from [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) and run inference using OpenVINO Runtime:
+model.compile()
-```python
-from optimum.intel import OVStableDiffusionXLPipeline
+question = "Which name is also used to describe the Amazon rainforest ?"
+context = "The Amazon rainforest, also known as Amazonia or the Amazon Jungle"
+tokens = tokenizer(question, context, max_length=seq_len, padding="max_length", return_tensors="np")
-model_id = "stabilityai/stable-diffusion-xl-base-1.0"
-base = OVStableDiffusionXLPipeline.from_pretrained(model_id)
-prompt = "train station by Caspar David Friedrich"
-image = base(prompt).images[0]
-image.save("train_station.png")
+outputs = model(**tokens)
```
-| | |
-|---|---|
-| ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/sd_xl/train_station_friedrich.png) | ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/sd_xl/train_station_friedrich_2.png) |
-
-#### Text-to-Image with Textual Inversion
-
-Here is an example of how you can load an SDXL OpenVINO model from [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) with pre-trained textual inversion embeddings and run inference using OpenVINO Runtime:
-
-
-First, you can run original pipeline without textual inversion
-```python
-from optimum.intel import OVStableDiffusionXLPipeline
-import numpy as np
-
-model_id = "stabilityai/stable-diffusion-xl-base-1.0"
-prompt = "charturnerv2, multiple views of the same character in the same outfit, a character turnaround of a beautiful woman wearing a red jacket and black shirt, best quality, intricate details."
-# Set a random seed for better comparison
-np.random.seed(112)
-
-base = OVStableDiffusionXLPipeline.from_pretrained(model_id, export=False, compile=False)
-base.compile()
-image1 = base(prompt, num_inference_steps=50).images[0]
-image1.save("sdxl_without_textual_inversion.png")
-```
+For models that handle images, you can also specify the `height` and `width` when reshaping your model:
-Then, you can load [charturnerv2](https://civitai.com/models/3036/charturner-character-turnaround-helper-for-15-and-21) textual inversion embedding and run pipeline with same prompt again
```python
-# Reset stable diffusion pipeline
-base.clear_requests()
-
-# Load textual inversion into stable diffusion pipeline
-base.load_textual_inversion("./charturnerv2.pt", "charturnerv2")
-
-# Compile the model before the first inference
-base.compile()
-image2 = base(prompt, num_inference_steps=50).images[0]
-image2.save("sdxl_with_textual_inversion.png")
-
-The left image shows the generation result of the original SDXL base 1.0, the right image shows the generation result of SDXL base 1.0 with textual inversion.
+batch_size, num_images, height, width = 1, 1, 512, 512
+pipeline.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images)
+images = pipeline(prompt, height=height, width=width, num_images_per_prompt=num_images).images
```
-| | |
-|---|---|
-| ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/textual_inversion/sdxl_without_textual_inversion.png) | ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/textual_inversion/sdxl_with_textual_inversion.png) |
-
+## Configuration
-#### Image-to-Image
-
-Here is an example of how you can load a PyTorch SDXL model, convert it to OpenVINO on-the-fly and run inference using OpenVINO Runtime for *image-to-image*:
+The `ov_config` parameter allow to provide custom OpenVINO configuration values. This can be used for example to enable full precision inference on devices where FP16 or BF16 inference precision is used by default.
```python
-from optimum.intel import OVStableDiffusionXLImg2ImgPipeline
-from diffusers.utils import load_image
-
-model_id = "stabilityai/stable-diffusion-xl-refiner-1.0"
-pipeline = OVStableDiffusionXLImg2ImgPipeline.from_pretrained(model_id, export=True)
-
-url = "https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/sd_xl/castle_friedrich.png"
-image = load_image(url).convert("RGB")
-prompt = "medieval castle by Caspar David Friedrich"
-image = pipeline(prompt, image=image).images[0]
-# Don't forget to save your OpenVINO model so that you can load it without exporting it with `export=True`
-pipeline.save_pretrained("openvino-sd-xl-refiner-1.0")
+ov_config = {"INFERENCE_PRECISION_HINT": "f32"}
+model = OVModelForSequenceClassification.from_pretrained(model_id, ov_config=ov_config)
```
-
-#### Refining the image output
-
-The image can be refined by making use of a model like [stabilityai/stable-diffusion-xl-refiner-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0). In this case, you only have to output the latents from the base model.
-
+Optimum Intel leverages OpenVINO's model caching to speed up model compiling on GPU. By default a `model_cache` directory is created in the model's directory in the [Hugging Face Hub cache](https://huggingface.co/docs/huggingface_hub/main/en/guides/manage-cache). To override this, use the ov_config parameter and set `CACHE_DIR` to a different value. To disable model caching on GPU, set `CACHE_DIR` to an empty string.
```python
-from optimum.intel import OVStableDiffusionXLImg2ImgPipeline
-
-model_id = "stabilityai/stable-diffusion-xl-refiner-1.0"
-refiner = OVStableDiffusionXLImg2ImgPipeline.from_pretrained(model_id, export=True)
-
-image = base(prompt=prompt, output_type="latent").images[0]
-image = refiner(prompt=prompt, image=image[None, :]).images[0]
+ov_config = {"CACHE_DIR": ""}
+model = OVModelForSequenceClassification.from_pretrained(model_id, device="gpu", ov_config=ov_config)
```
+## Weight quantization
-### Latent Consistency Models
-
-
-| Task | Auto Class |
-|--------------------------------------|--------------------------------------|
-| `text-to-image` | `OVLatentConsistencyModelPipeline` |
+You can also apply fp16, 8-bit or 4-bit weight compression on the Linear, Convolutional and Embedding layers when loading your model to reduce the memory footprint and inference latency.
+For more information on the quantization parameters checkout the [documentation](optimziation#weight-only-quantization).
-#### Text-to-Image
+
-Here is an example of how you can load a Latent Consistency Models (LCMs) from [SimianLuo/LCM_Dreamshaper_v7](https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7) and run inference using OpenVINO :
+If not specified, `load_in_8bit` will be set to `True` by default when models larger than 1 billion parameters are exported to the OpenVINO format (with `export=True`). You can disable it with `load_in_8bit=False`.
-```python
-from optimum.intel import OVLatentConsistencyModelPipeline
+
-model_id = "SimianLuo/LCM_Dreamshaper_v7"
-pipeline = OVLatentConsistencyModelPipeline.from_pretrained(model_id, export=True)
-prompt = "sailing ship in storm by Leonardo da Vinci"
-images = pipeline(prompt, num_inference_steps=4, guidance_scale=8.0).images
-```
+It's also possible to apply quantization on both weights and activations using the [`OVQuantizer`](optimization#static-quantization).
diff --git a/docs/source/openvino/models.mdx b/docs/source/openvino/models.mdx
index 523928b8de..83acd37efc 100644
--- a/docs/source/openvino/models.mdx
+++ b/docs/source/openvino/models.mdx
@@ -7,6 +7,8 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o
specific language governing permissions and limitations under the License.
-->
+# Supported models
+
🤗 Optimum handles the export of models to OpenVINO in the `exporters.openvino` module. It provides classes, functions, and a command line interface to perform the export easily.
Here is the list of the supported architectures :
diff --git a/docs/source/openvino/reference.mdx b/docs/source/openvino/reference.mdx
index 32385eae00..c5043d877a 100644
--- a/docs/source/openvino/reference.mdx
+++ b/docs/source/openvino/reference.mdx
@@ -16,6 +16,12 @@ limitations under the License.
# Models
+## Generic model classes
+
+[[autodoc]] openvino.modeling_base.OVBaseModel
+ - _from_pretrained
+ - reshape
+
## Natural Language Processing
The following classes are available for the following natural language processing tasks.
@@ -106,6 +112,7 @@ The following classes are available for the following multimodal tasks.
[[autodoc]] openvino.modeling_seq2seq.OVModelForPix2Struct
- forward
+
## Custom Tasks
### OVModelForCustomTasks
@@ -119,8 +126,40 @@ The following classes are available for the following multimodal tasks.
- forward
-# Quantization
+## Text-to-image
+
+### OVStableDiffusionPipeline
+
+[[autodoc]] openvino.modeling_diffusion.OVStableDiffusionPipeline
+ - forward
+
+### OVStableDiffusionXLPipeline
+
+[[autodoc]] openvino.modeling_diffusion.OVStableDiffusionXLPipeline
+ - forward
+
+### OVLatentConsistencyModelPipeline
+
+[[autodoc]] openvino.modeling_diffusion.OVLatentConsistencyModelPipeline
+ - forward
-### OVQuantizer
-[[autodoc]] openvino.quantization.OVQuantizer
+## Image-to-image
+
+### OVStableDiffusionImg2ImgPipeline
+
+[[autodoc]] openvino.modeling_diffusion.OVStableDiffusionImg2ImgPipeline
+ - forward
+
+### OVStableDiffusionXLImg2ImgPipeline
+
+[[autodoc]] openvino.modeling_diffusion.OVStableDiffusionXLImg2ImgPipeline
+ - forward
+
+## Inpainting
+
+### OVStableDiffusionInpaintPipeline
+
+[[autodoc]] openvino.modeling_diffusion.OVStableDiffusionInpaintPipeline
+ - forward
+
diff --git a/docs/source/openvino/tutorials/diffusers.mdx b/docs/source/openvino/tutorials/diffusers.mdx
new file mode 100644
index 0000000000..dad09420b1
--- /dev/null
+++ b/docs/source/openvino/tutorials/diffusers.mdx
@@ -0,0 +1,250 @@
+
+
+# Generate images with Diffusion models
+
+## Stable Diffusion
+
+Stable Diffusion models can also be used when running inference with OpenVINO. When Stable Diffusion models
+are exported to the OpenVINO format, they are decomposed into different components that are later combined during inference:
+- The text encoder
+- The U-NET
+- The VAE encoder
+- The VAE decoder
+
+| Task | Auto Class |
+|--------------------------------------|--------------------------------------|
+| `text-to-image` | `OVStableDiffusionPipeline` |
+| `image-to-image` | `OVStableDiffusionImg2ImgPipeline` |
+| `inpaint` | `OVStableDiffusionInpaintPipeline` |
+
+
+### Text-to-Image
+Here is an example of how you can load an OpenVINO Stable Diffusion model and run inference using OpenVINO Runtime:
+
+```python
+from optimum.intel import OVStableDiffusionPipeline
+
+model_id = "echarlaix/stable-diffusion-v1-5-openvino"
+pipeline = OVStableDiffusionPipeline.from_pretrained(model_id)
+prompt = "sailing ship in storm by Rembrandt"
+images = pipeline(prompt).images
+```
+
+To load your PyTorch model and convert it to OpenVINO on the fly, you can set `export=True`.
+
+```python
+model_id = "runwayml/stable-diffusion-v1-5"
+pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=True)
+# Don't forget to save the exported model
+pipeline.save_pretrained("openvino-sd-v1-5")
+```
+
+To further speed up inference, the model can be statically reshaped :
+
+```python
+# Define the shapes related to the inputs and desired outputs
+batch_size = 1
+num_images_per_prompt = 1
+height = 512
+width = 512
+
+# Statically reshape the model
+pipeline.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images_per_prompt)
+# Compile the model before the first inference
+pipeline.compile()
+
+# Run inference
+images = pipeline(prompt, height=height, width=width, num_images_per_prompt=num_images_per_prompt).images
+```
+
+In case you want to change any parameters such as the outputs height or width, you'll need to statically reshape your model once again.
+
+
+
+
+
+### Text-to-Image with Textual Inversion
+Here is an example of how you can load an OpenVINO Stable Diffusion model with pre-trained textual inversion embeddings and run inference using OpenVINO Runtime:
+
+
+First, you can run original pipeline without textual inversion
+```python
+from optimum.intel import OVStableDiffusionPipeline
+import numpy as np
+
+model_id = "echarlaix/stable-diffusion-v1-5-openvino"
+prompt = "A back-pack"
+# Set a random seed for better comparison
+np.random.seed(42)
+
+pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=False, compile=False)
+pipeline.compile()
+image1 = pipeline(prompt, num_inference_steps=50).images[0]
+image1.save("stable_diffusion_v1_5_without_textual_inversion.png")
+```
+
+Then, you can load [sd-concepts-library/cat-toy](https://huggingface.co/sd-concepts-library/cat-toy) textual inversion embedding and run pipeline with same prompt again
+```python
+# Reset stable diffusion pipeline
+pipeline.clear_requests()
+
+# Load textual inversion into stable diffusion pipeline
+pipeline.load_textual_inversion("sd-concepts-library/cat-toy", "")
+
+# Compile the model before the first inference
+pipeline.compile()
+image2 = pipeline(prompt, num_inference_steps=50).images[0]
+image2.save("stable_diffusion_v1_5_with_textual_inversion.png")
+```
+The left image shows the generation result of original stable diffusion v1.5, the right image shows the generation result of stable diffusion v1.5 with textual inversion.
+
+| | |
+|---|---|
+| ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/textual_inversion/stable_diffusion_v1_5_without_textual_inversion.png) | ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/textual_inversion/stable_diffusion_v1_5_with_textual_inversion.png) |
+
+
+### Image-to-Image
+
+```python
+import requests
+import torch
+from PIL import Image
+from io import BytesIO
+from optimum.intel import OVStableDiffusionImg2ImgPipeline
+
+model_id = "runwayml/stable-diffusion-v1-5"
+pipeline = OVStableDiffusionImg2ImgPipeline.from_pretrained(model_id, export=True)
+
+url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
+response = requests.get(url)
+init_image = Image.open(BytesIO(response.content)).convert("RGB")
+init_image = init_image.resize((768, 512))
+prompt = "A fantasy landscape, trending on artstation"
+image = pipeline(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images[0]
+image.save("fantasy_landscape.png")
+```
+
+## Stable Diffusion XL
+
+| Task | Auto Class |
+|--------------------------------------|--------------------------------------|
+| `text-to-image` | `OVStableDiffusionXLPipeline` |
+| `image-to-image` | `OVStableDiffusionXLImg2ImgPipeline` |
+
+
+### Text-to-Image
+
+Here is an example of how you can load a SDXL OpenVINO model from [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) and run inference using OpenVINO Runtime:
+
+```python
+from optimum.intel import OVStableDiffusionXLPipeline
+
+model_id = "stabilityai/stable-diffusion-xl-base-1.0"
+base = OVStableDiffusionXLPipeline.from_pretrained(model_id)
+prompt = "train station by Caspar David Friedrich"
+image = base(prompt).images[0]
+image.save("train_station.png")
+```
+
+| | |
+|---|---|
+| ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/sd_xl/train_station_friedrich.png) | ![](https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/sd_xl/train_station_friedrich_2.png) |
+
+### Text-to-Image with Textual Inversion
+
+Here is an example of how you can load an SDXL OpenVINO model from [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) with pre-trained textual inversion embeddings and run inference using OpenVINO Runtime:
+
+
+First, you can run original pipeline without textual inversion
+```python
+from optimum.intel import OVStableDiffusionXLPipeline
+import numpy as np
+
+model_id = "stabilityai/stable-diffusion-xl-base-1.0"
+prompt = "charturnerv2, multiple views of the same character in the same outfit, a character turnaround wearing a red jacket and black shirt, best quality, intricate details."
+# Set a random seed for better comparison
+np.random.seed(112)
+
+base = OVStableDiffusionXLPipeline.from_pretrained(model_id, export=False, compile=False)
+base.compile()
+image1 = base(prompt, num_inference_steps=50).images[0]
+image1.save("sdxl_without_textual_inversion.png")
+```
+
+Then, you can load [charturnerv2](https://civitai.com/models/3036/charturner-character-turnaround-helper-for-15-and-21) textual inversion embedding and run pipeline with same prompt again
+```python
+# Reset stable diffusion pipeline
+base.clear_requests()
+
+# Load textual inversion into stable diffusion pipeline
+base.load_textual_inversion("./charturnerv2.pt", "charturnerv2")
+
+# Compile the model before the first inference
+base.compile()
+image2 = base(prompt, num_inference_steps=50).images[0]
+image2.save("sdxl_with_textual_inversion.png")
+```
+
+### Image-to-Image
+
+Here is an example of how you can load a PyTorch SDXL model, convert it to OpenVINO on-the-fly and run inference using OpenVINO Runtime for *image-to-image*:
+
+```python
+from optimum.intel import OVStableDiffusionXLImg2ImgPipeline
+from diffusers.utils import load_image
+
+model_id = "stabilityai/stable-diffusion-xl-refiner-1.0"
+pipeline = OVStableDiffusionXLImg2ImgPipeline.from_pretrained(model_id, export=True)
+
+url = "https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/sd_xl/castle_friedrich.png"
+image = load_image(url).convert("RGB")
+prompt = "medieval castle by Caspar David Friedrich"
+image = pipeline(prompt, image=image).images[0]
+# Don't forget to save your OpenVINO model so that you can load it without exporting it with `export=True`
+pipeline.save_pretrained("openvino-sd-xl-refiner-1.0")
+```
+
+
+### Refining the image output
+
+The image can be refined by making use of a model like [stabilityai/stable-diffusion-xl-refiner-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0). In this case, you only have to output the latents from the base model.
+
+
+```python
+from optimum.intel import OVStableDiffusionXLImg2ImgPipeline
+
+model_id = "stabilityai/stable-diffusion-xl-refiner-1.0"
+refiner = OVStableDiffusionXLImg2ImgPipeline.from_pretrained(model_id, export=True)
+
+image = base(prompt=prompt, output_type="latent").images[0]
+image = refiner(prompt=prompt, image=image[None, :]).images[0]
+```
+
+
+## Latent Consistency Models
+
+
+| Task | Auto Class |
+|--------------------------------------|--------------------------------------|
+| `text-to-image` | `OVLatentConsistencyModelPipeline` |
+
+
+### Text-to-Image
+
+Here is an example of how you can load a Latent Consistency Model (LCM) from [SimianLuo/LCM_Dreamshaper_v7](https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7) and run inference using OpenVINO :
+
+```python
+from optimum.intel import OVLatentConsistencyModelPipeline
+
+model_id = "SimianLuo/LCM_Dreamshaper_v7"
+pipeline = OVLatentConsistencyModelPipeline.from_pretrained(model_id, export=True)
+prompt = "sailing ship in storm by Leonardo da Vinci"
+images = pipeline(prompt, num_inference_steps=4, guidance_scale=8.0).images
+```
diff --git a/docs/source/openvino/tutorials/notebooks.mdx b/docs/source/openvino/tutorials/notebooks.mdx
new file mode 100644
index 0000000000..3608e95c92
--- /dev/null
+++ b/docs/source/openvino/tutorials/notebooks.mdx
@@ -0,0 +1,26 @@
+
+
+# Notebooks
+
+## Inference
+
+| Notebook | Description | | |
+|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|------:|
+| [How to run inference with the OpenVINO](https://github.com/huggingface/optimum-intel/blob/main/notebooks/openvino/optimum_openvino_inference.ipynb) | Explains how to export your model to OpenVINO and to run inference with OpenVINO Runtime on various tasks | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/optimum-intel/blob/main/notebooks/openvino/optimum_openvino_inference.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/optimum-intel/blob/main/notebooks/openvino/optimum_openvino_inference.ipynb) |
+
+## Quantization
+
+| Notebook | Description | | |
+|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|------:|
+| [How to quantize a question answering model with OpenVINO NNCF](https://github.com/huggingface/optimum-intel/blob/main/notebooks/openvino/question_answering_quantization.ipynb) | Show how to apply post-training quantization on a question answering model using [NNCF](https://github.com/openvinotoolkit/nncf) | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/optimum-intel/blob/main/notebooks/openvino/question_answering_quantization.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/optimum-intel/blob/main/notebooks/openvino/question_answering_quantization.ipynb) |
+| [How to quantize Stable Diffusion model with OpenVINO NNCF](https://github.com/huggingface/optimum-intel/blob/main/notebooks/openvino/stable_diffusion_hybrid_quantization.ipynb) | Show how to apply post-training hybrid quantization on a Stable Diffusion model using [NNCF](https://github.com/openvinotoolkit/nncf) | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/optimum-intel/blob/main/notebooks/openvino/stable_diffusion_hybrid_quantization.ipynb)| [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/optimum-intel/blob/main/notebooks/openvino/stable_diffusion_hybrid_quantization.ipynb)|
+| [Compare outputs of a quantized Stable Diffusion model with its full-precision counterpart](https://github.com/huggingface/optimum-intel/blob/main/notebooks/openvino/stable_diffusion_optimization.ipynb) | Show how to load and compare outputs from two Stable Diffusion models with different precision | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/optimum-intel/blob/main/notebooks/openvino/stable_diffusion_optimization.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/optimum-intel/blob/main/notebooks/openvino/stable_diffusion_optimization.ipynb) |
+
+