Views
No views yet
HF1BitLLM/Llama3-8B-1.58-100B-tokens: 8.07% ternary ❌1bitLLM/bitnet_b1_58-3B: 2.69% ternary ❌1Total Parameters: 124,439,808
2Ternary Parameters: 119,722,445 (96.22%)
3Non-Ternary: Embeddings + LayerNorm (correct!)pip install torch transformers1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Load model and tokenizer
5model = AutoModelForCausalLM.from_pretrained("Chris4K/bitnet-gpt2-1.58bit")
6tokenizer = AutoTokenizer.from_pretrained("Chris4K/bitnet-gpt2-1.58bit")
7
8# Generate text
9prompt = "The future of AI is"
10inputs = tokenizer(prompt, return_tensors="pt")
11
12outputs = model.generate(
13 **inputs,
14 max_length=50,
15 do_sample=True,
16 temperature=0.7,
17 top_p=0.9
18)
19
20print(tokenizer.decode(outputs[0], skip_special_tokens=True))1import torch
2
3total = 0
4ternary = 0
5
6for name, param in model.named_parameters():
7 if 'weight' in name:
8 flat = param.data.flatten()
9 is_ternary = (
10 torch.isclose(flat, torch.tensor(-1.0), atol=1e-3) |
11 torch.isclose(flat, torch.tensor(0.0), atol=1e-3) |
12 torch.isclose(flat, torch.tensor(1.0), atol=1e-3)
13 )
14 ternary += is_ternary.sum().item()
15 total += len(flat)
16
17print(f"Ternary %: {ternary/total*100:.2f}%")
18# Output: Ternary %: 96.22% ✅1{
2 'model': 'gpt2',
3 'epochs': 3,
4 'batch_size': 16,
5 'learning_rate': 5e-5,
6 'optimizer': 'AdamW',
7 'quantization': 'Ternary {-1, 0, +1}',
8 'gradient_estimator': 'Straight-Through Estimator (STE)'
9}Epoch 1: Val Perplexity = 45316.80, Ternary = 96.22%
Epoch 2: Val Perplexity = TBD, Ternary = TBD
Epoch 3: Val Perplexity = TBD, Ternary = TBD1class BitLinear(nn.Linear):
2 def forward(self, x):
3 w = self.weight
4 scale = 1.0 / (w.abs().mean().clamp(min=1e-5) + 1e-5)
5
6 # Quantize to {-1, 0, +1}
7 w_ternary = (w * scale).round().clamp(-1, 1) / scale
8
9 # Straight-Through Estimator
10 w_quant = w + (w_ternary - w).detach()
11
12 return F.linear(x, w_quant, self.bias)
13
14 def quantize_weights(self):
15 # Project weights to ternary after optimizer step
16 with torch.no_grad():
17 w = self.weight.data
18 scale = 1.0 / (w.abs().mean().clamp(min=1e-5) + 1e-5)
19 w_ternary = (w * scale).round().clamp(-1, 1)
20 self.weight.data = w_ternary / scale1optimizer.step()
2
3# CRITICAL: Enforce ternary constraint
4for module in model.modules():
5 if isinstance(module, BitLinear):
6 module.quantize_weights()pytorch_model.bin - Model weights (150MB)config.json - Model configurationtokenizer.json - Tokenizertraining_stats.json - Training metricsverify_bitnet.py - Verification script| Model | Ternary % | Size | Verified |
|---|---|---|---|
| This Model | 96.22% | 150MB | ✅ |
| HF1BitLLM/Llama3-8B | 8.07% | 3.6GB | ❌ |
| 1bitLLM/bitnet_b1_58-3B | 2.69% | 13.3GB | ❌ |
1@misc{bitnet-gpt2-2026,
2 author = {Chris4K},
3 title = {BitNet GPT-2 1.58-Bit: First Verified Public BitNet Model},
4 year = {2026},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/Chris4K/bitnet-gpt2-1.58bit}
7}1@article{wang2023bitnet,
2 title={BitNet: Scaling 1-bit Transformers for Large Language Models},
3 author={Wang, Hongyu and Ma, Shuming and Dong, Li and Huang, Shaohan and Wang, Huaijie and Ma, Lingxiao and Yang, Fan and Wang, Ruiping and Wu, Yi and Wei, Furu},
4 journal={arXiv preprint arXiv:2310.11453},
5 year={2023}
6}verify_bitnet.py in model files