---
title: "Pre-Train BERT with Hugging Face Transformers and Habana Gaudi"
description: "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."
url: "https://swe.quest/articles/pre-train-bert-with-hugging-face-transformers-and-habana-gaudi-81eabeeaee082cc4"
canonical: "https://huggingface.co/blog/pretraining-bert"
source: "Hugging Face"
published: "2022-08-22"
difficulty: "medium"
kind: "How-to"
reading_minutes: 10
tags: ["model-training","llms","hardware","cost-optimization","python"]
---

# Pre-Train BERT with Hugging Face Transformers and Habana Gaudi

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-08-22, 10 min read, How-to. Original: [https://huggingface.co/blog/pretraining-bert](https://huggingface.co/blog/pretraining-bert)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Hardware](https://swe.quest/tags/hardware.md) (Infrastructure)
- [Cost optimization](https://swe.quest/tags/cost-optimization.md) (Reliability & operations)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Hugging Face Transformers
- BERT and masked language modeling
- AWS EC2 basics ([easy posts on Cloud platforms](https://swe.quest/tags/cloud.md?difficulty=easy))

## Keywords

BERT, Habana Gaudi, Optimum Habana, Hugging Face Transformers, AWS DL1, GaudiTrainer, DeepSpeed

## Related posts

- [Getting Started with Transformers on Habana Gaudi](https://swe.quest/articles/getting-started-with-transformers-on-habana-gaudi-edc0b7eeb1b053a5.md): Hugging Face · 2022-04-26 · Medium · 4 min read · Model training, Hardware, Cloud platforms
  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.
- [Text-Generation Pipeline on Intel® Gaudi® 2 AI Accelerator](https://swe.quest/articles/text-generation-pipeline-on-intel-gaudi-2-ai-accelerator-08fe7776346f5b95.md): Hugging Face · 2024-02-29 · Medium · 6 min read · Inference & serving, LLMs, Hardware, Python
  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.
- [1 Billion Classifications](https://swe.quest/articles/1-billion-classifications-90168b62de6ffa1b.md): Hugging Face · 2025-02-13 · Medium · 17 min read · Inference & serving, Cost optimization, Performance engineering, Machine learning
  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.
- [How to train a Language Model with Megatron-LM](https://swe.quest/articles/how-to-train-a-language-model-with-megatron-lm-30e46ab3423562c4.md): Hugging Face · 2022-09-07 · Hard · 7 min read · Model training, LLMs, Python
  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.
- [Fast Inference on Large Language Models: BLOOMZ on Habana Gaudi2 Accelerator](https://swe.quest/articles/fast-inference-on-large-language-models-bloomz-on-habana-gaudi2-accelerator-b18b18c9b1b881f4.md): Hugging Face · 2023-03-28 · Medium · 8 min read · Inference & serving, LLMs, Hardware, Performance engineering
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
