Text Generation
Transformers
Safetensors
English
llama_kda
llama
pretraining
linear-attention
kimi-delta-attention
custom_code
Instructions to use Mercity/pretrain-kda-1b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Mercity/pretrain-kda-1b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Mercity/pretrain-kda-1b", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Mercity/pretrain-kda-1b", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Mercity/pretrain-kda-1b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Mercity/pretrain-kda-1b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Mercity/pretrain-kda-1b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Mercity/pretrain-kda-1b
- SGLang
How to use Mercity/pretrain-kda-1b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Mercity/pretrain-kda-1b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Mercity/pretrain-kda-1b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Mercity/pretrain-kda-1b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Mercity/pretrain-kda-1b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Mercity/pretrain-kda-1b with Docker Model Runner:
docker model run hf.co/Mercity/pretrain-kda-1b
Add model card
Browse files
README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
library_name: transformers
|
| 5 |
+
pipeline_tag: text-generation
|
| 6 |
+
datasets:
|
| 7 |
+
- HuggingFaceFW/fineweb
|
| 8 |
+
tags:
|
| 9 |
+
- llama
|
| 10 |
+
- pretraining
|
| 11 |
+
- linear-attention
|
| 12 |
+
- kimi-delta-attention
|
| 13 |
+
- custom_code
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# Llama 1B with Kimi Delta Attention, 6B tokens
|
| 17 |
+
|
| 18 |
+
A dense ~1B-parameter Llama 3-style decoder where one attention layer in every four is Kimi Delta Attention (KDA), a linear-attention layer from [Kimi Linear](https://arxiv.org/abs/2510.26692). It is otherwise identical to the [QK-norm baseline](https://huggingface.co/Mercity/pretrain-baseline-qknorm) and was trained on the same 6B FineWeb tokens.
|
| 19 |
+
|
| 20 |
+
This is a base model. It is not instruction-tuned or safety-tuned.
|
| 21 |
+
|
| 22 |
+
## Results
|
| 23 |
+
|
| 24 |
+
### Training
|
| 25 |
+
|
| 26 |
+
| Metric | Value | vs. baseline |
|
| 27 |
+
| --- | ---: | ---: |
|
| 28 |
+
| Final train loss (step 3,053) | 2.5634 | −0.0064 |
|
| 29 |
+
| Final eval loss (step 3,000) | 2.5848 | −0.0059 |
|
| 30 |
+
| Final grad norm (step 3,053) | 0.0514 | +0.0060 |
|
| 31 |
+
| Peak grad norm after step 200 | 0.559 | +0.020 |
|
| 32 |
+
| Tokens / steps | 6B / 3,053 | same |
|
| 33 |
+
|
| 34 |
+
### Zero-shot benchmarks
|
| 35 |
+
|
| 36 |
+
Scores from `lm-eval` on each task's full split. Shared-9 is the unweighted mean of the nine tasks.
|
| 37 |
+
|
| 38 |
+
| Benchmark | Metric | Score | vs. baseline |
|
| 39 |
+
| --- | --- | ---: | ---: |
|
| 40 |
+
| HellaSwag | acc_norm | 39.65 | +0.67 |
|
| 41 |
+
| WinoGrande | acc | 51.46 | −0.16 |
|
| 42 |
+
| ARC-Easy | acc_norm | 39.60 | −0.55 |
|
| 43 |
+
| ARC-Challenge | acc_norm | 24.06 | +0.43 |
|
| 44 |
+
| PIQA | acc_norm | 67.79 | +1.20 |
|
| 45 |
+
| OpenBookQA | acc_norm | 27.60 | −1.40 |
|
| 46 |
+
| CommonsenseQA | acc | 20.07 | +0.25 |
|
| 47 |
+
| SciQ | acc_norm | 63.80 | +0.30 |
|
| 48 |
+
| LAMBADA | acc | 38.29 | +0.54 |
|
| 49 |
+
| **Shared-9 average** | | **41.37** | **+0.14** |
|
| 50 |
+
| Shared-9 average, 4-bit NF4 | | 40.64 | −0.18 |
|
| 51 |
+
|
| 52 |
+
The +0.14 Shared-9 gap is within single-seed noise, so treat KDA as matching the baseline, not beating it.
|
| 53 |
+
|
| 54 |
+
## Usage
|
| 55 |
+
|
| 56 |
+
The architecture class ships with the checkpoint, so load it with `trust_remote_code=True`. The KDA layers need [flash-linear-attention](https://github.com/fla-org/flash-linear-attention) and a CUDA GPU (its kernels are written in Triton). Tested with `transformers==5.8.0` and `flash-linear-attention==0.5.2`.
|
| 57 |
+
|
| 58 |
+
```bash
|
| 59 |
+
pip install "transformers==5.8.0" "flash-linear-attention==0.5.2"
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
import torch
|
| 64 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 65 |
+
|
| 66 |
+
repo = "Mercity/pretrain-kda-1b"
|
| 67 |
+
|
| 68 |
+
tokenizer = AutoTokenizer.from_pretrained(repo)
|
| 69 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 70 |
+
repo,
|
| 71 |
+
trust_remote_code=True,
|
| 72 |
+
torch_dtype=torch.bfloat16,
|
| 73 |
+
device_map="cuda",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
inputs = tokenizer("The capital of France is", return_tensors="pt").to(model.device)
|
| 77 |
+
# use_cache=False is required: the KDA layers keep no recurrent state between
|
| 78 |
+
# decoding steps, so cached generation would feed them one token at a time.
|
| 79 |
+
output = model.generate(**inputs, max_new_tokens=32, do_sample=False, use_cache=False)
|
| 80 |
+
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
Generation without the cache re-reads the full sequence at every step, so it is slow for long outputs. Scoring text in one forward pass has no such cost:
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
batch = tokenizer("FineWeb is a large web-text dataset.", return_tensors="pt").to(model.device)
|
| 87 |
+
with torch.no_grad():
|
| 88 |
+
loss = model(**batch, labels=batch["input_ids"]).loss
|
| 89 |
+
print(f"loss={loss.item():.3f} ppl={loss.exp().item():.1f}")
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
The first forward pass on a new machine spends about 90 seconds compiling the KDA kernels.
|
| 93 |
+
|
| 94 |
+
## Model details
|
| 95 |
+
|
| 96 |
+
| Setting | Value |
|
| 97 |
+
| --- | --- |
|
| 98 |
+
| Architecture | `LlamaKDA` (Llama 3-style dense decoder, hybrid KDA + softmax attention) |
|
| 99 |
+
| Total parameters | 1.056B |
|
| 100 |
+
| Layers | 32: 24 GQA + 8 KDA |
|
| 101 |
+
| KDA layer positions | 0, 4, 8, 12, 16, 20, 24, 28 |
|
| 102 |
+
| Hidden size | 1,536 |
|
| 103 |
+
| Intermediate size (SwiGLU) | 5,120 |
|
| 104 |
+
| Attention heads / KV heads | 12 / 6 (GQA layers) |
|
| 105 |
+
| QK normalization | On (GQA layers) |
|
| 106 |
+
| Max sequence length | 8,192 |
|
| 107 |
+
| Tokenizer | Llama 2, 32,000 tokens |
|
| 108 |
+
| Embeddings | Tied input and output |
|
| 109 |
+
|
| 110 |
+
## Training
|
| 111 |
+
|
| 112 |
+
| Setting | Value |
|
| 113 |
+
| --- | --- |
|
| 114 |
+
| Data | FineWeb `sample-10BT`, packed 8,192-token sequences |
|
| 115 |
+
| Tokens / steps | 6B / 3,053 |
|
| 116 |
+
| Batch | 10 per device × 24 gradient accumulation (~1.97M tokens per step) |
|
| 117 |
+
| Optimizer | Muon (LR 0.02, momentum 0.95, 5 Newton-Schulz steps, WD 0.1) + AdamW (LR 3e-4, β 0.9/0.95, WD 0.1) |
|
| 118 |
+
| Schedule | Cosine, 150 warmup steps |
|
| 119 |
+
| Hardware | 1 × NVIDIA B200, ~18 hours |
|
| 120 |
+
| Stack | TorchTitan, FlashAttention 4, Liger kernels, flash-linear-attention |
|
| 121 |
+
|
| 122 |
+
## Related checkpoints
|
| 123 |
+
|
| 124 |
+
| Model | Change from the baseline | Shared-9 |
|
| 125 |
+
| --- | --- | ---: |
|
| 126 |
+
| [Baseline (QK-norm)](https://huggingface.co/Mercity/pretrain-baseline-qknorm) | Reference model | 41.23 |
|
| 127 |
+
| [N-gram 25%](https://huggingface.co/Mercity/pretrain-longcat-ngram-25pct) | ~25% of parameters moved into LongCat n-gram tables, 23 layers | 40.56 |
|
| 128 |
+
| [N-gram 50%](https://huggingface.co/Mercity/pretrain-longcat-ngram-50pct) | ~48% of parameters moved into LongCat n-gram tables, 16 layers | 39.54 |
|
| 129 |
+
|
| 130 |
+
## Limitations
|
| 131 |
+
|
| 132 |
+
Trained on 6B English web tokens only, a small budget for a 1B model. Benchmark scores are single-seed. KDA inference needs a CUDA GPU and does not support cached generation through `transformers`. The model will repeat or make up facts and has had no alignment training.
|