Skip to main content

Optimum Habana is the interface between the Hugging Face Transformers and Diffusers libraries and Habana's Gaudi processor (HPU). It provides a set of tools enabling easy model loading, training and inference on single- and multi-HPU settings for different downstream tasks.

Project description

Optimum Habana

🤗 Optimum Habana is the interface between the 🤗 Transformers and Diffusers libraries and Habana's Gaudi processor (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 models and tasks with only few changes.

What is a Habana Processing Unit (HPU)?

HPUs offer fast model training and inference as well as a great price-performance ratio. Check out this blog post about BERT pre-training and this article benchmarking Habana Gaudi2 versus Nvidia A100 GPUs for concrete examples. If you are not familiar with HPUs and would like to know more about them, we recommend you take a look at our conceptual guide.

Install

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 DeepSpeed on HPUs, you also need to run the following command:

pip install git+https://github.com/HabanaAI/DeepSpeed.git@1.11.0

Optimum Habana is a fast-moving project, and you may want to install it from source:

pip install git+https://github.com/huggingface/optimum-habana.git

Last but not least, don't forget to install the requirements for every example:

cd <example-folder>
pip install -r requirements.txt

How to use it?

Quick Start

🤗 Optimum Habana was designed with one goal in mind: to make training and inference straightforward for any 🤗 Transformers and 🤗 Diffusers user while leveraging the complete power of Gaudi processors.

Transformers Interface

There are two main classes one needs to know:

  • GaudiTrainer: the trainer class that takes care of compiling (lazy or eager mode) and distributing the model to run on HPUs, and performing training and evaluation.
  • GaudiConfig: the class that enables to configure Habana 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 Gaudi 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 (Gaudi configurations are stored in model repositories) or a path to a local Gaudi configuration file (you can see here how to write your own).

Diffusers Interface

You can generate images from prompts using Stable Diffusion on Gaudi using the GaudiStableDiffusionPipeline class and the [GaudiDDIMScheduler] 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 = "runwayml/stable-diffusion-v1-5"

- 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 = generator(
    ["An image of a squirrel in Picasso style"],
    num_images_per_prompt=16,
+   batch_size=4,
)

Documentation

Check out the documentation of Optimum Habana for more advanced usage.

Validated Models

The following model architectures, tasks and device distributions have been validated for 🤗 Optimum Habana:

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:
  • text classification
  • question answering
  • language modeling
  • RoBERTa :heavy_check_mark: :heavy_check_mark:
  • question answering
  • language modeling
  • ALBERT :heavy_check_mark: :heavy_check_mark:
  • question answering
  • language modeling
  • DistilBERT :heavy_check_mark: :heavy_check_mark:
  • question answering
  • language modeling
  • GPT2 :heavy_check_mark: :heavy_check_mark:
  • language modeling
  • text generation
  • BLOOM(Z) :x:
  • DeepSpeed
  • text generation
  • StarCoder :x:
  • Single card
  • text generation
  • GPT-J
  • DeepSpeed
  • Single card
  • DeepSpeed
  • language modeling
  • text generation
  • GPT-NeoX
  • DeepSpeed
  • DeepSpeed
  • language modeling
  • text generation
  • OPT :x:
  • DeepSpeed
  • text generation
  • Llama 2
  • DeepSpeed
  • LoRA
  • DeepSpeed
  • LoRA
  • language modeling
  • text generation
  • StableLM :x:
  • Single card
  • text generation
  • Falcon :x:
  • Single card
  • text generation
  • CodeGen :x:
  • Single card
  • text generation
  • T5 :heavy_check_mark: :heavy_check_mark:
  • summarization
  • translation
  • question answering
  • ViT :heavy_check_mark: :heavy_check_mark:
  • image classification
  • Swin :heavy_check_mark: :heavy_check_mark:
  • image classification
  • Wav2Vec2 :heavy_check_mark: :heavy_check_mark:
  • audio classification
  • speech recognition
  • CLIP :heavy_check_mark: :heavy_check_mark:
  • contrastive image-text training
  • BridgeTower :heavy_check_mark: :heavy_check_mark:
  • contrastive image-text training
  • ESMFold :x:
  • Single card
  • protein folding
    • Diffusers:
    Architecture Training Inference Tasks
    Stable Diffusion :x:
  • Single card
  • text-to-image generation
  • LDM3D :x:
  • Single card
  • text-to-image generation
  • Other models and tasks supported by the 🤗 Transformers and 🤗 Diffusers library may also work. You can refer to this section for using them with 🤗 Optimum Habana. Besides, this page explains how to modify any example from the 🤗 Transformers library to make it work with 🤗 Optimum Habana.

    If you find any issues while using those, please open an issue or a pull request.

    Gaudi Setup

    Please refer to Habana Gaudi's official installation guide.

    Tests should be run in a Docker container based on Habana Docker images.

    The current version has been validated for SynapseAI 1.11.

    Development

    Check the contributor guide for instructions.

    Project details


    Download files

    Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

    Source Distribution

    optimum-habana-1.7.2.tar.gz (187.9 kB view details)

    Uploaded Source

    Built Distribution

    If you're not sure about the file name format, learn more about wheel file names.

    optimum_habana-1.7.2-py3-none-any.whl (201.4 kB view details)

    Uploaded Python 3

    File details

    Details for the file optimum-habana-1.7.2.tar.gz.

    File metadata

    • Download URL: optimum-habana-1.7.2.tar.gz
    • Upload date:
    • Size: 187.9 kB
    • Tags: Source
    • Uploaded using Trusted Publishing? No
    • Uploaded via: twine/4.0.2 CPython/3.8.10

    File hashes

    Hashes for optimum-habana-1.7.2.tar.gz
    Algorithm Hash digest
    SHA256 25c52686b1fb15bfe4b6032d44f40f0674107fddc20cada0bc9b54ecbf635847
    MD5 2afc82d34385a5efbe9f26d7cb719ade
    BLAKE2b-256 3f67bf4bd524903e04efff458edf7d08b6877c3fcac1d2a210c03d99e58ddf5b

    See more details on using hashes here.

    File details

    Details for the file optimum_habana-1.7.2-py3-none-any.whl.

    File metadata

    • Download URL: optimum_habana-1.7.2-py3-none-any.whl
    • Upload date:
    • Size: 201.4 kB
    • Tags: Python 3
    • Uploaded using Trusted Publishing? No
    • Uploaded via: twine/4.0.2 CPython/3.8.10

    File hashes

    Hashes for optimum_habana-1.7.2-py3-none-any.whl
    Algorithm Hash digest
    SHA256 809a782b6e7783499da95d74950860ecbc3ce19547d88f2c2bef745e5836a16f
    MD5 84fb9c953aeb72b94fc2213e399f4311
    BLAKE2b-256 ab60dc2cf858e8134a18c036dea17ab671f3fee38919aaef34294f8a6190f3e7

    See more details on using hashes here.

    Supported by

    AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page