# OpenVINO

🤗 [Optimum](https://github.com/huggingface/optimum-intel) cung cấp các pipeline Stable Diffusion tương thích với OpenVINO để thực hiện suy luận trên nhiều loại bộ xử lý Intel khác nhau (xem [danh sách đầy đủ](https://docs.openvino.ai/latest/openvino_docs_OV_UG_supported_plugins_Supported_Devices.html) các thiết bị được hỗ trợ).

Bạn cần cài đặt 🤗 Optimum Intel với tùy chọn `--upgrade-strategy eager` để đảm bảo [`optimum-intel`](https://github.com/huggingface/optimum-intel) đang sử dụng phiên bản mới nhất:

```bash
pip install --upgrade-strategy eager optimum["openvino"]
```

Hướng dẫn này sẽ cho bạn thấy cách sử dụng các pipeline Stable Diffusion và Stable Diffusion XL (SDXL) với OpenVINO.

## Stable Diffusion

Để tải và chạy suy luận, hãy sử dụng `OVStableDiffusionPipeline`. Nếu bạn muốn tải một mô hình PyTorch và chuyển đổi nó sang định dạng OpenVINO ngay lập tức, hãy đặt `export=True`:

```python
from optimum.intel import OVStableDiffusionPipeline

model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=True)
prompt = "sailing ship in storm by Rembrandt"
image = pipeline(prompt).images[0]

# Don't forget to save the exported model
pipeline.save_pretrained("openvino-sd-v1-5")
```

Để tăng tốc suy luận hơn nữa, hãy định hình lại mô hình một cách tĩnh. Nếu bạn thay đổi bất kỳ tham số nào như chiều cao hoặc chiều rộng của đầu ra, bạn sẽ cần định hình lại mô hình của mình một cách tĩnh một lần nữa.

```python
# Define the shapes related to the inputs and desired outputs
batch_size, num_images, height, width = 1, 1, 512, 512

# Statically reshape the model
pipeline.reshape(batch_size, height, width, num_images)
# Compile the model before inference
pipeline.compile()

image = pipeline(
    prompt,
    height=height,
    width=width,
    num_images_per_prompt=num_images,
).images[0]
```

    

Bạn có thể tìm thêm các ví dụ trong [tài liệu](https://huggingface.co/docs/optimum/intel/inference#stable-diffusion) của 🤗 Optimum, và Stable Diffusion được hỗ trợ cho text-to-image, image-to-image và inpainting.

## Stable Diffusion XL

Để tải và chạy suy luận với SDXL, hãy sử dụng `OVStableDiffusionXLPipeline`:

```python
from optimum.intel import OVStableDiffusionXLPipeline

model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipeline = OVStableDiffusionXLPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Rembrandt"
image = pipeline(prompt).images[0]
```

Để tăng tốc suy luận hơn nữa, hãy [định hình lại tĩnh](#stable-diffusion) mô hình như đã trình bày trong phần Stable Diffusion.

Bạn có thể tìm thêm các ví dụ trong [tài liệu](https://huggingface.co/docs/optimum/intel/inference#stable-diffusion-xl) của 🤗 Optimum, và việc chạy SDXL trên OpenVINO được hỗ trợ cho cả text-to-image và image-to-image.

