---
title: "Accelerate BERT inference with Hugging Face Transformers and AWS Inferentia"
description: "DistilBERT can be compiled to AWS Neuron with torch.neuron.trace and served on an Inferentia-backed SageMaker real-time endpoint, using a custom inference.py that pins one Neuron core per HTTP worker."
url: "https://swe.quest/articles/accelerate-bert-inference-with-hugging-face-transformers-and-aws-inferentia-a06f31ecbe8356b6"
canonical: "https://huggingface.co/blog/bert-inferentia-sagemaker"
source: "Hugging Face"
published: "2022-03-16"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["inference","hardware","cloud","python"]
---

# Accelerate BERT inference with Hugging Face Transformers and AWS Inferentia

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-03-16, 8 min read, How-to. Original: [https://huggingface.co/blog/bert-inferentia-sagemaker](https://huggingface.co/blog/bert-inferentia-sagemaker)

## Summary

DistilBERT can be compiled to AWS Neuron with torch.neuron.trace and served on an Inferentia-backed SageMaker real-time endpoint, using a custom inference.py that pins one Neuron core per HTTP worker. Because the Neuron SDK lacked dynamic shape support, inputs must be padded to a fixed sequence length (128 here) chosen at compile time. On an ml.inf1.xlarge the endpoint averaged 5-6 ms model latency per request. That is faster than CPU, and running four model copies across the chip's four Neuron cores gives higher throughput than GPUs. The recommendation is to move BERT-like encoder workloads to Inferentia when that latency is acceptable.

Written by swe.quest, not by the author.

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

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [Hardware](https://swe.quest/tags/hardware.md) (Infrastructure)
- [Cloud platforms](https://swe.quest/tags/cloud.md) (Infrastructure)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Hugging Face Transformers
- PyTorch TorchScript tracing ([easy posts on Observability](https://swe.quest/tags/observability.md?difficulty=easy))
- Amazon SageMaker basics

## Keywords

AWS Inferentia, AWS Neuron SDK, Amazon SageMaker, Hugging Face Transformers, DistilBERT, torch-neuron, BERT

## Related posts

- [Accelerating Hugging Face Transformers with AWS Inferentia2](https://swe.quest/articles/accelerating-hugging-face-transformers-with-aws-inferentia2-732d2daf15bc9874.md): Hugging Face · 2023-04-17 · Easy · 5 min read · Inference & serving, Hardware, Performance engineering, Cloud platforms
  Hugging Face benchmarked popular Transformer models on AWS Inferentia2 (inf2.xlarge) against Inferentia1 (inf1.2xlarge) and an NVIDIA A10G GPU (g5.2xlarge), running 144 p95-latency experiments at batch size 1 across six architectures and sequence lengths from 8 to 512.
- [Accelerating PyTorch Transformers with Intel Sapphire Rapids - part 2](https://swe.quest/articles/accelerating-pytorch-transformers-with-intel-sapphire-rapids-part-2-c41ef6f6c233e349.md): Hugging Face · 2023-02-06 · Medium · 6 min read · Inference & serving, Hardware, Performance engineering, Python
  Intel Sapphire Rapids CPUs with AMX instructions, combined with Hugging Face Optimum Intel running in bfloat16 with JIT compilation, cut p99 inference latency for DistilBERT, BERT and RoBERTa text classification by roughly 3x compared with vanilla Transformers on Ice Lake.
- [Make your llama generation time fly with AWS Inferentia2](https://swe.quest/articles/make-your-llama-generation-time-fly-with-aws-inferentia2-bc8b5e1981a590c0.md): Hugging Face · 2023-11-07 · Medium · 7 min read · Inference & serving, LLMs, Hardware, Cloud platforms, Performance engineering
  optimum-neuron can now compile and export Llama 2 models for text generation on AWS Inferentia2, using static input shapes (batch size, 2048 sequence length that also bounds the KV cache) and a configurable core count and fp16 precision.
- [Hugging Face Text Generation Inference available for AWS Inferentia2](https://swe.quest/articles/hugging-face-text-generation-inference-available-for-aws-inferentia2-4404accc08bd48f1.md): Hugging Face · 2024-02-01 · Medium · 7 min read · Inference & serving, LLMs, Cloud platforms, Python
  Text Generation Inference (TGI) is now generally available on AWS Inferentia2 through Amazon SageMaker, offering an alternative to GPUs for serving open LLMs such as Llama and Mistral.
- [How to deploy and fine-tune DeepSeek models on AWS](https://swe.quest/articles/how-to-deploy-and-fine-tune-deepseek-models-on-aws-e64f345de2597515.md): Hugging Face · 2025-01-30 · Medium · 8 min read · Inference & serving, Cloud platforms, LLMs, Python
  DeepSeek-R1's distilled Llama and Qwen models can be deployed on AWS through Hugging Face Inference Endpoints, Bedrock Marketplace, SageMaker with Hugging Face LLM DLCs (TGI v3) on GPUs or Neuron, and EC2 Inferentia via the Hugging Face Neuron AMI.

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)
