Repository
optimum-habana
Easy and lightning fast training of 🤗 Transformers on Habana Gaudi processor (HPU)
- Architecture
- Training
- BERT
- :heavy_check_mark:
- RoBERTa
- :heavy_check_mark:
- ALBERT
- :heavy_check_mark:
Specs
- ArchitectureTraining
- BERT:heavy_check_mark:
- RoBERTa:heavy_check_mark:
- ALBERT:heavy_check_mark:
- DistilBERT:heavy_check_mark:
- GPT2:heavy_check_mark:
- BLOOM(Z)<ul><li>DeepSpeed</li></ul>
- StarCoder / StarCoder2:heavy_check_mark:
- GPT-J<ul><li>DeepSpeed</li></ul>
- GPT-Neo<ul><li>Single card</li></ul>
- GPT-NeoX<ul><li>DeepSpeed</li></ul>
- OPT<ul><li>DeepSpeed</li></ul>
- StableLM<ul><li>Single card</li></ul>
- Falcon<ul><li>LoRA</li></ul>
- CodeGen<ul><li>Single card</li></ul>
- MPT<ul><li>Single card</li></ul>
- Mistral<ul><li>Single card</li></ul>
- Phi:heavy_check_mark:
- Mixtral<ul><li>Single card</li></ul>
- Persimmon<ul><li>Single card</li></ul>
- Qwen2 / Qwen3<ul><li>Single card</li></ul>
- Qwen2-MoE<ul><li>Single card</li></ul>
- Gemma:heavy_check_mark:
- Gemma2:heavy_check_mark:
- Gemma3:heavy_check_mark:
- XGLM<ul><li>Single card</li></ul>
- Cohere<ul><li>Single card</li></ul>
- T5 / Flan T5:heavy_check_mark:
- BART<ul><li>Single card</li></ul>
- ViT:heavy_check_mark:
- Swin:heavy_check_mark:
- Wav2Vec2:heavy_check_mark:
- Whisper:heavy_check_mark:
- SpeechT5<ul><li>Single card</li></ul>
- CLIP:heavy_check_mark:
- BridgeTower:heavy_check_mark:
- ESMFold<ul><li>Single card</li></ul>
- Blip<ul><li>Single card</li></ul>
- OWLViT<ul><li>Single card</li></ul>
- ClipSeg<ul><li>Single card</li></ul>
README
<a href="https://github.com/huggingface/optimum-habana#gh-light-mode-only">
<img src="https://github.com/huggingface/optimum-habana/blob/main/readme_logo_light.png"/>
</a>
<a href="https://github.com/huggingface/optimum-habana#gh-dark-mode-only">
<img src="https://github.com/huggingface/optimum-habana/blob/main/readme_logo_dark.png"/>
</a>
Optimum for Intel® Gaudi® Accelerators
Optimum for Intel Gaudi - a.k.a. optimum-habana - is the interface between the Transformers and Diffusers libraries and
Intel Gaudi AI Accelerators (HPU). It provides a set of tools enabling easy
model loading, training and inference on single- and multi-HPU settings for different downstream tasks. The list of officially
validated models and tasks is available here. Users can
try other of the thousands of Hugging Face models on Intel Gaudi accelerators and tasks with only few changes.
What are Intel Gaudi AI Accelerators (HPUs)?
HPUs offer fast model training and inference as well as a great price-performance ratio.
Check out this blog post about BLOOM inference and
this post benchmarking Intel Gaudi 2 and NVIDIA A100 GPUs for BridgeTower training
for concrete examples.
Gaudi Setup
Please refer to the Intel Gaudi AI Accelerator official installation guide.
[!NOTE]
Tests should be run in a Docker container based on Intel Gaudi's official images. Instructions to
obtain the latest containers from the Intel Gaudi Vault are available
here.
The current Optimum for Intel Gaudi has been validated with Intel Gaudi v1.22 stack.
Install the library and get example scripts
Option 1: Use the latest stable release
To install the latest stable release of this package
pip install --upgrade-strategy eager optimum[habana]
The --upgrade-strategy eager option is needed to ensure optimum-habana is upgraded to the latest stable release.
To use the example associated with the latest stable release, run:
git clone https://github.com/huggingface/optimum-habana
cd optimum-habana && git checkout v1.19.1
with v1.19.1 being the latest Optimum for Intel Gaudi release version.
Option 2: Use the latest main branch under development
Optimum for Intel Gaudi is a fast-moving project, and you may want to install it from source and get the latest scripts :
pip install git+https://github.com/huggingface/optimum-habana.git
git clone https://github.com/huggingface/optimum-habana
Option 3: Use the transformers_future branch to have the latest changes from Transformers
The transformers_future branch is regularly updated with the latest changes from the main branches of Optimum for Intel Gaudi
and Transformers. This enables you to try out new Transformers features that have not been merged into the main branch yet.
[!WARNING]
Thetransformers_futurebranch may have some regressions or bugs and may be less stable than the main branch.
pip install git+https://github.com/huggingface/optimum-habana.git@transformers_future
git clone -b transformers_future https://github.com/huggingface/optimum-habana
Install Dependencies
To use DeepSpeed on HPUs, you also need to run the following command:
pip install git+https://github.com/HabanaAI/[email protected]
To install the requirements for every example:
cd <example-folder>
pip install -r requirements.txt
How to use it?
Optimum for Intel Gaudi was designed with one goal in mind: to make training and inference straightforward for Transformers
and Diffusers users, while fully leveraging the power of Intel Gaudi AI Accelerators.
Transformers Interface
There are two main classes one needs to know:
-
GaudiTrainer: the trainer class that takes care of
compiling and distributing the model to run on HPUs, and performing training and evaluation. -
GaudiConfig: the class that enables to configure
Gaudi Mixed Precision and to decide whether optimized operators and optimizers should be used or not.
The GaudiTrainer is very similar to the
Transformers Trainer, and adapting a script using the Trainer to
make it work with Intel Gaudi accelerators will mostly consist in simply swapping the Trainer class for the GaudiTrainer one.
That's how most of the example scripts were adapted from their
original counterparts.
Here is an example:
- from transformers import Trainer, TrainingArguments
+ from optimum.habana import GaudiConfig, GaudiTrainer, GaudiTrainingArguments
- training_args = TrainingArguments(
+ training_args = GaudiTrainingArguments(
# training arguments...
+ use_habana=True,
+ use_lazy_mode=True, # whether to use lazy or eager mode
+ gaudi_config_name=path_to_gaudi_config,
)
# A lot of code here
# Initialize our Trainer
- trainer = Trainer(
+ trainer = GaudiTrainer(
model=model,
args=training_args, # Original training arguments.
train_dataset=train_dataset if training_args.do_train else None,
eval_dataset=eval_dataset if training_args.do_eval else None,
compute_metrics=compute_metrics,
tokenizer=tokenizer,
data_collator=data_collator,
)
where gaudi_config_name is the name of a model from the Hub (Intel Gaudi configurations
are stored in model repositories) or a path to a local Intel Gaudi configuration file (you can see
here how to write your own).
Diffusers Interface
You can generate images from prompts using Stable Diffusion on Intel Gaudi using the
GaudiStableDiffusionPipeline class and the
GaudiDDIMScheduler
class which have been both optimized for HPUs. Here is how to use them and the differences with the Diffusers library:
- from diffusers import DDIMScheduler, StableDiffusionPipeline
+ from optimum.habana.diffusers import GaudiDDIMScheduler, GaudiStableDiffusionPipeline
model_name = "CompVis/stable-diffusion-v1-4"
- scheduler = DDIMScheduler.from_pretrained(model_name, subfolder="scheduler")
+ scheduler = GaudiDDIMScheduler.from_pretrained(model_name, subfolder="scheduler")
- pipeline = StableDiffusionPipeline.from_pretrained(
+ pipeline = GaudiStableDiffusionPipeline.from_pretrained(
model_name,
scheduler=scheduler,
+ use_habana=True,
+ use_hpu_graphs=True,
+ gaudi_config="Habana/stable-diffusion",
)
outputs = pipeline(
["An image of a squirrel in Picasso style"],
num_images_per_prompt=16,
+ batch_size=4,
)
Important Note on Pytorch 2.5 Performance Degradation
With the upgrade to PyTorch 2.5, users may experience some performance degradation due to changes in the handling of FP16/BF16 inputs.
The note from PyTorch 2.5 states:
"A naive SDPA math backend, when using FP16/BF16 inputs, can accumulate significant numerical errors due to the usage of low-precision
intermediate buffers. To mitigate this issue, the default behavior now involves upcasting FP16/BF16 inputs to FP32. Computations are performed
in FP32/TF32, and the final FP32 results are then downcasted back to FP16/BF16. This will improve numerical accuracy of the final output for
the math backend with FP16/BF16 inputs, but increases memory usages and may cause the performance regressions in the math backend as computations
shift from FP16/BF16 BMM to FP32/TF32 BMM/Matmul."
For scenarios where reduced-precision reductions are preferred for speed, they can be enabled with the following setting:
torch.backends.cuda.allow_fp16_bf16_reduction_math_sdp(True)
Additionally, the next release of Optimum Habana will include a Gaudi-specific safe_softmax implementation that will also improve performance.
More info:
- https://pytorch.org/docs/stable/notes/numerical_accuracy.html
Documentation
Check out the documentation of Optimum for Intel Gaudi for more advanced usage.
Validated Models
The following model architectures, tasks and device distributions have been validated for Optimum for Intel Gaudi:
[!NOTE]
In the tables below, :heavy_check_mark: means single-card, multi-card and DeepSpeed have all been validated.
Transformers:
| Architecture | Training | Inference | Tasks |
|---|---|---|---|
| BERT | :heavy_check_mark: | :heavy_check_mark: | <ul><li>text classification</li><li>question answering</li><li>language modeling</li><li>text feature extraction</li></ul> |
| RoBERTa | :heavy_check_mark: | :heavy_check_mark: | <ul><li>question answering</li><li>language modeling</li></ul> |
| ALBERT | :heavy_check_mark: | :heavy_check_mark: | <ul><li>question answering</li><li>language modeling</li></ul> |
| DistilBERT | :heavy_check_mark: | :heavy_check_mark: | <ul><li>question answering</li><li>language modeling</li></ul> |
| GPT2 | :heavy_check_mark: | :heavy_check_mark: | <ul><li>language modeling</li><li>text generation</li></ul> |
| BLOOM(Z) | <ul><li>DeepSpeed</li></ul> | <ul><li>text generation</li></ul> | |
| StarCoder / StarCoder2 | :heavy_check_mark: | <ul><li>Single-card</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| GPT-J | <ul><li>DeepSpeed</li></ul> | <ul><li>Single card</li><li>DeepSpeed</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| GPT-Neo | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| GPT-NeoX | <ul><li>DeepSpeed</li></ul> | <ul><li>DeepSpeed</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| OPT | <ul><li>DeepSpeed</li></ul> | <ul><li>text generation</li></ul> | |
| Llama 2 / CodeLlama / Llama 3 / Llama Guard / Granite | :heavy_check_mark: | :heavy_check_mark: | <ul><li>language modeling</li><li>text generation</li><li>question answering</li><li>text classification (Llama Guard)</li></ul> |
| StableLM | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Falcon | <ul><li>LoRA</li></ul> | :heavy_check_mark: | <ul><li>language modeling</li><li>text generation</li></ul> |
| CodeGen | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| MPT | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Mistral | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Phi | :heavy_check_mark: | <ul><li>Single card</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| Mixtral | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Persimmon | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Qwen2 / Qwen3 | <ul><li>Single card</li></ul> | <ul><li>Single card</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| Qwen2-MoE | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Gemma | :heavy_check_mark: | <ul><li>Single card</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| Gemma2 | :heavy_check_mark: | <ul><li>text generation</li></ul> | |
| Gemma3 | :heavy_check_mark: | <ul><li>text generation</li></ul> | |
| XGLM | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Cohere | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| T5 / Flan T5 | :heavy_check_mark: | :heavy_check_mark: | <ul><li>summarization</li><li>translation</li><li>question answering</li></ul> |
| BART | <ul><li>Single card</li></ul> | <ul><li>summarization</li><li>translation</li><li>question answering</li></ul> | |
| ViT | :heavy_check_mark: | :heavy_check_mark: | <ul><li>image classification</li></ul> |
| Swin | :heavy_check_mark: | :heavy_check_mark: | <ul><li>image classification</li></ul> |
| Wav2Vec2 | :heavy_check_mark: | :heavy_check_mark: | <ul><li>audio classification</li><li>speech recognition</li></ul> |
| Whisper | :heavy_check_mark: | :heavy_check_mark: | <ul><li>speech recognition</li></ul> |
| SpeechT5 | <ul><li>Single card</li></ul> | <ul><li>text to speech</li></ul> | |
| CLIP | :heavy_check_mark: | :heavy_check_mark: | <ul><li>contrastive image-text training</li></ul> |
| BridgeTower | :heavy_check_mark: | :heavy_check_mark: | <ul><li>contrastive image-text training</li></ul> |
| ESMFold | <ul><li>Single card</li></ul> | <ul><li>protein folding</li></ul> | |
| Blip | <ul><li>Single card</li></ul> | <ul><li>visual question answering</li><li>image to text</li></ul> | |
| OWLViT | <ul><li>Single card</li></ul> | <ul><li>zero shot object detection</li></ul> | |
| ClipSeg | <ul><li>Single card</li></ul> | <ul><li>object segmentation</li></ul> | |
| Llava / Llava-next / Llava-onevision | <ul><li>Single card</li></ul> | <ul><li>image to text</li></ul> | |
| idefics2 | <ul><li>LoRA</li></ul> | <ul><li>Single card</li></ul> | <ul><li>image to text</li></ul> |
| Paligemma | <ul><li>Single card</li></ul> | <ul><li>image to text</li></ul> | |
| Segment Anything Model | <ul><li>Single card</li></ul> | <ul><li>object segmentation</li></ul> | |
| VideoMAE | <ul><li>Single card</li></ul> | <ul><li>Video classification</li></ul> | |
| TableTransformer | <ul><li>Single card</li></ul> | <ul><li>table object detection </li></ul> | |
| DETR | <ul><li>Single card</li></ul> | <ul><li>object detection</li></ul> | |
| Mllama | <ul><li>LoRA</li></ul> | :heavy_check_mark: | <ul><li>image to text</li></ul> |
| MiniCPM3 | <ul><li>Single card</li></ul> | <ul><li>text generation</li></ul> | |
| Baichuan2 | <ul><li>DeepSpeed</li></ul>. | <ul><li>Single card</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| DeepSeek-V2 | :heavy_check_mark: | :heavy_check_mark: | <ul><li>text generation</li></ul> |
| DeepSeek-V3 / Moonlight | :heavy_check_mark: | <ul><li>text generation</li></ul> | |
| ChatGLM | <ul><li>DeepSpeed</li></ul> | <ul><li>Single card</li></ul> | <ul><li>language modeling</li><li>text generation</li></ul> |
| Qwen2-VL | <ul><li>Single card</li></ul> | <ul><li>image to text</li></ul> | |
| Qwen2.5-VL | <ul><li>Single card</li></ul> | <ul><li>image to text</li></ul> | |
| VideoLLaVA | <ul><li>Single card</li></ul> | <ul><li>Video comprehension</li></ul> | |
| GLM-4V | <ul><li>Single card</li></ul> | <ul><li>image to text</li></ul> | |
| Arctic | <ul><li>DeepSpeed</li></ul> | <ul><li>text generation</li></ul> | |
| GPT-OSS | <ul><li>DeepSpeed</li></ul> | <ul><li>text generation</li></ul> |
</div>
Diffusers:
| Architecture | Training | Inference | Tasks |
|---|---|---|---|
| Stable Diffusion | :heavy_check_mark: | :heavy_check_mark: | <ul><li>text-to-image generation</li><li>image-to-image generation</li></ul> |
| Stable Diffusion XL | :heavy_check_mark: | :heavy_check_mark: | <ul><li>text-to-image generation</li><li>image-to-image generation</li></ul> |
| Stable Diffusion Depth2img | <ul><li>Single card</li></ul> | <ul><li>depth-to-image generation</li></ul> | |
| Stable Diffusion 3 | :heavy_check_mark: | :heavy_check_mark: | <ul><li>text-to-image generation</li></ul> |
| LDM3D | <ul><li>Single card</li></ul> | <ul><li>text-to-image generation</li></ul> | |
| FLUX.1 | <ul><li>LoRA</li></ul> | <ul><li>Single card</li></ul> | <ul><li>text-to-image generation</li><li>image-to-image generation</li></ul> |
| Qwen Image | <ul><li>Single card</li></ul> | <ul><li>text-to-image generation</li></ul> | |
| Text to Video | <ul><li>Single card</li></ul> | <ul><li>text-to-video generation</li></ul> | |
| Image to Video | <ul><li>Single card</li></ul> | <ul><li>image-to-video generation</li></ul> | |
| i2vgen-xl | <ul><li>Single card</li></ul> | <ul><li>image-to-video generation</li></ul> | |
| Wan | :heavy_check_mark: | <ul><li>text-to-video generation</li><li>image-to-video generation</li></ul> |
PyTorch Image Models/TIMM:
| Architecture | Training | Inference | Tasks |
|---|---|---|---|
| FastViT | <ul><li>Single card</li></ul> | <ul><li>image classification</li></ul> |
TRL:
| Architecture | Training | Inference | Tasks |
|---|---|---|---|
| Llama 2 | :heavy_check_mark: | <ul><li>DPO Pipeline</li></ul> | |
| Llama 2 | :heavy_check_mark: | <ul><li>PPO Pipeline</li></ul> | |
| Stable Diffusion | :heavy_check_mark: | <ul><li>DDPO Pipeline</li></ul> |
Other models and tasks supported by the Transformers and Diffusers libraries may also work. You can refer to this section
for using them with Optimum for Intel Gaudi. In addition, this page explains how to modify any
example from the Transformers library to make it work with Optimum for Intel Gaudi.
If you find any issues while using those, please open an issue or a pull request.
After training your model, feel free to submit it to the Intel 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.
The list of validated models through continuous integration tests is posted here
Development
Check the contributor guide for instructions.