---
title: "Towards Encrypted Large Language Models with FHE"
description: "A single attention head of GPT2's first multi-head attention layer can run on encrypted data using TFHE via Zama's Concrete-Python and Concrete-ML, with the rest of the model executing in plaintext on the client."
url: "https://swe.quest/articles/towards-encrypted-large-language-models-with-fhe-6e22ac5193234b25"
canonical: "https://huggingface.co/blog/encrypted-llm"
source: "Hugging Face"
published: "2023-08-02"
difficulty: "hard"
kind: "How-to"
reading_minutes: 7
tags: ["cryptography","privacy","inference","llms"]
---

# Towards Encrypted Large Language Models with FHE

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-08-02, 7 min read, How-to. Original: [https://huggingface.co/blog/encrypted-llm](https://huggingface.co/blog/encrypted-llm)

## Summary

A single attention head of GPT2's first multi-head attention layer can run on encrypted data using TFHE via Zama's Concrete-Python and Concrete-ML, with the rest of the model executing in plaintext on the client. Weights and activations are post-training quantized to integers, nonlinearities become Programmable Bootstrapping table lookups, and the Hugging Face transformers forward pass is overridden with a quantized SingleHeadAttention module. Four-bit quantization keeps about 96% of original top-k accuracy on roughly 80 sentences, and the compiled circuit needs 8-bit width. A 6-token sequence costs 11,622 PBS operations, which takes minutes on CPU; the authors expect future FHE ASICs to bring that under 100 ms.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Cryptography](https://swe.quest/tags/cryptography.md) (Security)
- [Privacy](https://swe.quest/tags/privacy.md) (Security)
- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- transformer attention
- homomorphic encryption basics ([easy posts on Cryptography](https://swe.quest/tags/cryptography.md?difficulty=easy))
- model quantization ([easy posts on Inference & serving](https://swe.quest/tags/inference.md?difficulty=easy))

## Keywords

FHE, TFHE, Concrete-ML, Concrete-Python, GPT2, Hugging Face Transformers, Programmable Bootstrapping, Zama

## Related posts

- [Running Privacy-Preserving Inferences on Hugging Face Endpoints](https://swe.quest/articles/running-privacy-preserving-inferences-on-hugging-face-endpoints-de0383d315f32496.md): Hugging Face · 2024-04-16 · Medium · 8 min read · Privacy, Inference & serving, Cryptography, Machine learning, Python
  Zama's Concrete ML models, compiled for Fully Homomorphic Encryption (FHE), can be deployed as Hugging Face Inference Endpoints using custom inference handlers.
- [Sentiment Analysis on Encrypted Data with Homomorphic Encryption](https://swe.quest/articles/sentiment-analysis-on-encrypted-data-with-homomorphic-encryption-62bd5104a9f4964a.md): Hugging Face · 2022-11-17 · Medium · 8 min read · Privacy, Cryptography, Machine learning, Inference & serving, Python
  Sentiment classification can run on encrypted text by computing a transformer (RoBERTa) hidden representation on the client, then running a quantized XGBoost classifier under fully homomorphic encryption with Concrete-ML.
- [Making LLMs lighter with AutoGPTQ and transformers](https://swe.quest/articles/making-llms-lighter-with-autogptq-and-transformers-6a1ce27ee3288303.md): Hugging Face · 2023-08-23 · Medium · 10 min read · Inference & serving, LLMs, Performance engineering, Hardware
  AutoGPTQ is now integrated into Hugging Face Transformers (via Optimum), letting users quantize and run LLMs at 8, 4, 3 or 2 bits with the GPTQ post-training method.
- [Intel and Hugging Face Partner to Democratize Machine Learning Hardware Acceleration](https://swe.quest/articles/intel-and-hugging-face-partner-to-democratize-machine-learning-hardware-0de59c0019e6704c.md): Hugging Face · 2022-06-15 · Medium · 7 min read · Inference & serving, Machine learning, Performance engineering, Hardware
  Intel has joined Hugging Face's Hardware Partner Program, and the two are collaborating on Optimum Intel, an open-source library built on Intel Neural Compressor that applies quantization, pruning and distillation to Transformer models on Xeon CPUs.
- [Smaller is better: Q8-Chat, an efficient generative AI experience on Xeon](https://swe.quest/articles/smaller-is-better-q8-chat-an-efficient-generative-ai-experience-on-xeon-67e9bd0a15445b38.md): Hugging Face · 2023-05-16 · Easy · 5 min read · Inference & serving, LLMs, Hardware
  SmoothQuant enables accurate 8-bit (INT8) post-training quantization of LLMs by mathematically shifting activation outliers into the weights, which makes Transformer layers quantization-friendly.

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)
