Downloads · 30 days
32
2% of all-time downloads
123aloo123/BitNet-GPT2-125M-Ternary
BitNet-GPT2-125M-Ternary is a text generation model from 123aloo123. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as mit.
This repository demonstrates the end-to-end conversion, Quantization-Aware Training (QAT), and inference of a 1.58-bit Large Language Model built entirely from scratch. By performing "Model Surgery" on a standard Hugg…
Downloads · 30 days
32
2% of all-time downloads
All-time downloads
1.4K
Public
Parameters
124M
498 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors498 MB · 99%
From the Hugging Face model README
This repository demonstrates the end-to-end conversion, Quantization-Aware Training (QAT), and inference of a 1.58-bit Large Language Model built entirely from scratch. By performing "Model Surgery" on a standard Hugging Face GPT-2 (125M), this project replaces standard 16-bit floating-point linear layers with custom Ternary (-1, 0, 1) BitLinear layers, inspired by Microsoft's The Era of 1-bit LLMs research.
Standard LLMs rely on highly precise decimal weights (FP16 or BF16) which require expensive matrix multiplications. This project implements a BitLinear architecture that restricts weights to ternary values {-1, 0, 1}.
This transition mathematically simplifies inference from complex matrix multiplications to highly efficient, hardware-friendly addition and subtraction, dramatically lowering memory footprint and increasing inference speed.
BitLinear PyTorch Module: A drop-in replacement for nn.Linear featuring dynamic AbsMean scaling.torch.autograd.Function that allows backpropagation to bypass the non-differentiable step function of the ternary quantizer, updating hidden "shadow" FP16 weights during training.Conv1D / Linear layers with BitLinear modules while copying initial shadow weights.BitNetSTEThe magic of this model relies on deceiving the PyTorch computational graph during the backward pass.
[-1, 0, 1].class BitNetSTE(torch.autograd.Function):
@staticmethod
def forward(ctx, W):
gamma = W.abs().mean()
W_quant = torch.clamp(torch.round(W / (gamma + 1e-5)), min=-1, max=1)
return W_quant
@staticmethod
def backward(ctx, grad_output):
return grad_output # The STE "Lie"
pip install torch transformers datasets
Load a standard GPT-2 model and inject the BitLinear layers:
from transformers import AutoModelForCausalLM
from custom_architecture import perform_surgery
model = AutoModelForCausalLM.from_pretrained("gpt2")
perform_surgery(model)
Because the initial transition to ternary weights causes massive precision loss (acting like a lobotomy to the pre-trained model), it must undergo Quantization-Aware Training to recover its reasoning capabilities.
Run the training loop on the WikiText dataset:
python train_bitnet.py
Note: The training script includes an automatic evolution showcase that generates text every 15 minutes, allowing you to monitor the model's recovery of grammar and syntax in real-time.
During a standard 2-hour training run on an NVIDIA T4, the model successfully demonstrated the ability to recover from the initial quantization shock.
8.6477 (Random/Confused)5.9542 (Grammar recovery)Sample Generation (Post-Surgery, Pre-Training):
Prompt: The future of artificial intelligence is Output:
zxq wlp rtb the a of to in...
Sample Generation (After 2 Hours of QAT):
Prompt: The future of artificial intelligence is Output:
the most powerful and influential than any of the most important computer, and the computer is the best known for its...
bitnet.cpp integration) for actual on-device CPU inference speedups.Disclaimer: This is an educational project built to demonstrate the foundational concepts of Quantization-Aware Training and custom PyTorch autograd functions.