Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 10 min read How-to mediumAdept

Pre-Train BERT with Hugging Face Transformers and Habana Gaudi

BERT-base can be pre-trained from scratch with masked-language modeling on an AWS DL1 instance (Habana Gaudi), using Hugging Face Transformers, Datasets and Optimum Habana. The workflow merges BookCorpus and Wikipedia, trains a 32k-vocab tokenizer, and packs the text into 512-token chunks on a CPU instance. Training then runs data-parallel across 8 HPUs by swapping Trainer for GaudiTrainer, launched remotely with EC2 Remote Runner. 100k steps at global batch 256 took about 12.5 hours, which projects a full 1M-step pre-training to roughly $1,650. That is about 25% cheaper than a DeepSpeed V100 baseline estimated at around $2,075.

Assumes: Hugging Face TransformersBERT and masked language modelingAWS EC2 basics

Related posts

  • Getting Started with Transformers on Habana Gaudi

    Hugging Face mediumAdept

    Fine-tuning BERT for text classification on Habana Gaudi accelerators takes a few steps on AWS: launch an EC2 DL1 instance (8 Gaudi processors) from the Habana Deep Learning AMI, raise EBS storage to 50GB, and pull the Habana PyTorch container. From there, Optimum Habana's run_glue.py example fine-tunes bert-large-uncased-whole-word-masking on GLUE MRPC using a Gaudi config pulled from the Hugging Face Hub. The 3-epoch job finished in about 2 minutes 12 seconds with an eval F1 of about 0.90, and a Spot Instance cut the hourly price from $13.11 to $3.93, roughly 70% off.

  • Text-Generation Pipeline on Intel® Gaudi® 2 AI Accelerator

    Hugging Face mediumAdept

    Optimum Habana's custom GaudiTextGenerationPipeline runs Llama 2 text generation (7b, 13b, 70b) on Intel Gaudi 2 with a few lines of code, handling pre- and post-processing end to end. Setup covers gated-model access, pinned versions of optimum-habana and Habana's DeepSpeed fork, and CLI runs with HPU graphs, KV cache and sampling parameters. Llama-2-70b uses DeepSpeed across 8 devices. The pipeline can also be embedded in Python scripts or wrapped by LangChain's HuggingFacePipeline, validated only against LangChain 0.0.191.

  • 1 Billion Classifications

    Hugging Face mediumAdept

    Processing 1B classifications or embeddings on a single-GPU baseline costs about $254 for DistilBERT sentiment classification and $409 for ModernBERT-based gte embeddings on an NVIDIA L4 ($0.8/hr), but roughly $44,500 for ColQwen2 vision embeddings. The method serves models with Infinity on Hugging Face Inference Endpoints and load-tests them with k6's shared-iterations executor. It sweeps batch size and virtual users using an exponential-then-binary search with a 2% improvement threshold, then scales cost horizontally by adding replicas. The best configurations were batch 64 / 448 VUs for classification, 32 / 256 for embedding and 4 / 4 for vision. The cheap T4 lost to the L4, partly because Flash Attention 2 requires newer GPUs.

  • How to train a Language Model with Megatron-LM

    Hugging Face hardMaster

    Megatron-LM, NVIDIA's GPU-optimized framework for pre-training transformers, can train a GPT2-style model end to end: environment setup in an NGC PyTorch container, preprocessing data into tokenized binary mmap files, launching distributed training, and converting checkpoints to Hugging Face Transformers. Its speed comes from a DataLoader that pre-computes shuffled sequence indexes across epochs and from fused CUDA kernels, including Apex's fused AdamW, which cut memory movement. A 110M-parameter CodeParrot model trains in about 12 hours on 8 GPUs with data parallelism, and tensor and pipeline parallelism are available for larger models. The extra preprocessing and conversion overhead makes it worth using mainly for pre-training or extended fine-tuning rather than short fine-tunes.

  • Fast Inference on Large Language Models: BLOOMZ on Habana Gaudi2 Accelerator

    Hugging Face mediumAdept

    BLOOMZ (176B parameters, 352 GB in 16-bit) runs inference on a single 8-HPU Habana Gaudi2 server using Optimum Habana with Habana's DeepSpeed-inference fork, HPU graphs and a KV cache. In batch-1 greedy generation of 100 tokens, Gaudi2 is 1.42x faster than an 8x A100 80GB setup on the 176B model (3.1 s vs 4.4 s) and 2.89x faster on BLOOMZ-7B. Unlike A100, Gaudi2 benefits from model parallelism on the 7B model. First-gen Gaudi (AWS DL1, about $13/hr) offers better price-performance than A100 for the 7B checkpoint, and the post includes commands to reproduce the benchmark or run generation over any Hugging Face dataset.

The quest for software engineering knowledge.

Look:
Layout: