Text Generation
Transformers
Safetensors
English
q
causal-lm
custom-architecture
pretrained
base-model
grouped-query-attention
qk-norm
gated-residuals
tiny-model
custom_code
Instructions to use q-project/Q-50M-Base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use q-project/Q-50M-Base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="q-project/Q-50M-Base", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("q-project/Q-50M-Base", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use q-project/Q-50M-Base with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "q-project/Q-50M-Base" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "q-project/Q-50M-Base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/q-project/Q-50M-Base
- SGLang
How to use q-project/Q-50M-Base 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 "q-project/Q-50M-Base" \ --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": "q-project/Q-50M-Base", "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 "q-project/Q-50M-Base" \ --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": "q-project/Q-50M-Base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use q-project/Q-50M-Base with Docker Model Runner:
docker model run hf.co/q-project/Q-50M-Base
Release Q-50M-Base
Browse files- .ipynb_checkpoints/README-checkpoint.md +298 -0
- LICENSE +202 -0
- Q_Logo.svg +43 -0
- README.md +298 -0
- config.json +41 -0
- configuration_q.py +30 -0
- generation_config.json +12 -0
- model.safetensors +3 -0
- modeling_q.py +119 -0
- tokenizer.json +0 -0
- tokenizer_config.json +10 -0
- training_metrics.json +8 -0
.ipynb_checkpoints/README-checkpoint.md
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
datasets:
|
| 8 |
+
- HuggingFaceFW/fineweb-edu
|
| 9 |
+
tags:
|
| 10 |
+
- causal-lm
|
| 11 |
+
- custom-architecture
|
| 12 |
+
- pretrained
|
| 13 |
+
- base-model
|
| 14 |
+
- grouped-query-attention
|
| 15 |
+
- qk-norm
|
| 16 |
+
- gated-residuals
|
| 17 |
+
- tiny-model
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
<div align="center">
|
| 21 |
+
<img src="Q_Logo.svg" alt="Q Logo" width="220"/>
|
| 22 |
+
<h1>Q-50M-Base</h1>
|
| 23 |
+
<h3>A compact gated decoder language model pretrained from scratch</h3>
|
| 24 |
+
|
| 25 |
+
<p>
|
| 26 |
+
<a href="https://huggingface.co/q-project/Q-50M-Base"><img src="https://img.shields.io/badge/Parameters-50.9M-red?style=for-the-badge&logo=huggingface" alt="Parameters"></a>
|
| 27 |
+
<a href="https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu"><img src="https://img.shields.io/badge/Pretraining-5B%20FineWeb--Edu%20tokens-blue?style=for-the-badge" alt="Training data"></a>
|
| 28 |
+
<a href="LICENSE"><img src="https://img.shields.io/badge/License-Apache%202.0-green?style=for-the-badge" alt="License"></a>
|
| 29 |
+
</p>
|
| 30 |
+
</div>
|
| 31 |
+
|
| 32 |
+
> [!IMPORTANT]
|
| 33 |
+
> **Q-50M-Base is a base language model, not a chat or instruction model.** It is designed for text continuation and as a starting point for fine-tuning. It should not be expected to reliably follow user instructions.
|
| 34 |
+
|
| 35 |
+
## Model summary
|
| 36 |
+
|
| 37 |
+
**Q-50M-Base** is a 50.9M-parameter decoder-only causal language model developed by Q-Project. It was pretrained from scratch on approximately 5 billion tokens from FineWeb-Edu and does not inherit weights from another model.
|
| 38 |
+
|
| 39 |
+
The model keeps the well-tested shape of a modern Transformer decoder while adding three inexpensive mechanisms intended to improve stability and parameter efficiency at small scale:
|
| 40 |
+
|
| 41 |
+
1. **Grouped-query attention (GQA)** with 8 query heads and 2 key/value heads.
|
| 42 |
+
2. **Per-head QK-Norm** before the attention score calculation.
|
| 43 |
+
3. **Content-dependent scalar gates** on both the attention and MLP residual branches.
|
| 44 |
+
|
| 45 |
+
The implementation is built on the Hugging Face Mistral classes for compatibility with the Transformers generation and cache APIs, but the attention and decoder layers are replaced by Q-Project implementations.
|
| 46 |
+
|
| 47 |
+
## Architecture
|
| 48 |
+
|
| 49 |
+
```text
|
| 50 |
+
Input token IDs
|
| 51 |
+
│
|
| 52 |
+
▼
|
| 53 |
+
Tied token embedding (32,768 × 512)
|
| 54 |
+
│
|
| 55 |
+
▼
|
| 56 |
+
┌──────────────────────────────────────────┐
|
| 57 |
+
│ QDecoderLayer × 10 │
|
| 58 |
+
│ │
|
| 59 |
+
│ RMSNorm │
|
| 60 |
+
│ │ │
|
| 61 |
+
│ ▼ │
|
| 62 |
+
│ GQA: 8 query heads / 2 KV heads │
|
| 63 |
+
│ ├─ per-head QK-Norm │
|
| 64 |
+
│ ├─ RoPE positional encoding │
|
| 65 |
+
│ └─ content-dependent scalar output gate │
|
| 66 |
+
│ │ │
|
| 67 |
+
│ └──────────── residual connection │
|
| 68 |
+
│ │
|
| 69 |
+
│ RMSNorm │
|
| 70 |
+
│ │ │
|
| 71 |
+
│ ▼ │
|
| 72 |
+
│ SwiGLU MLP (512 → 1,792 → 512) │
|
| 73 |
+
│ └─ content-dependent scalar output gate │
|
| 74 |
+
│ │ │
|
| 75 |
+
│ └──────────── residual connection │
|
| 76 |
+
└──────────────────────────────────────────┘
|
| 77 |
+
│
|
| 78 |
+
▼
|
| 79 |
+
Final RMSNorm
|
| 80 |
+
│
|
| 81 |
+
▼
|
| 82 |
+
Tied language-model head → next-token logits
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
### Configuration
|
| 86 |
+
|
| 87 |
+
| Parameter | Value |
|
| 88 |
+
|---|---:|
|
| 89 |
+
| Total parameters | 50,878,208 |
|
| 90 |
+
| Vocabulary size | 32,768 |
|
| 91 |
+
| Hidden size | 512 |
|
| 92 |
+
| Decoder layers | 10 |
|
| 93 |
+
| Query attention heads | 8 |
|
| 94 |
+
| Key/value heads | 2 |
|
| 95 |
+
| Attention head dimension | 64 |
|
| 96 |
+
| MLP intermediate size | 1,792 |
|
| 97 |
+
| Maximum context length | 2,048 tokens |
|
| 98 |
+
| Positional encoding | RoPE in every layer |
|
| 99 |
+
| RoPE theta | 10,000 |
|
| 100 |
+
| Normalization | RMSNorm, epsilon `1e-5` |
|
| 101 |
+
| Attention stabilization | Per-head QK-Norm |
|
| 102 |
+
| MLP | SwiGLU / SiLU |
|
| 103 |
+
| Residual branch gates | Scalar, content-dependent, attention + MLP |
|
| 104 |
+
| Embedding / LM head | Tied |
|
| 105 |
+
| Attention dropout | 0.0 |
|
| 106 |
+
| Training attention backend | PyTorch SDPA |
|
| 107 |
+
|
| 108 |
+
The implementation supports selective NoPE layers for architecture experiments. This checkpoint sets `nope_every_n=None`, so **RoPE is active in all ten layers**.
|
| 109 |
+
|
| 110 |
+
## Why this architecture?
|
| 111 |
+
|
| 112 |
+
### A 512-wide, 10-layer decoder
|
| 113 |
+
|
| 114 |
+
At roughly 50M parameters, allocating capacity is a trade-off between vocabulary embeddings, depth, width, and MLP size. Ten 512-dimensional layers give the model enough sequential depth to build progressively richer representations without making every attention and MLP operation too expensive. The 1,792-dimensional SwiGLU MLP provides substantial nonlinear capacity while keeping the full model within the target size.
|
| 115 |
+
|
| 116 |
+
### Tied input and output embeddings
|
| 117 |
+
|
| 118 |
+
With a 32K vocabulary and a hidden size of 512, one embedding matrix contains about 16.8M parameters. Sharing it with the output language-model head avoids a second matrix of the same size. This is especially important in a 50M model: the saved parameters can be used in the decoder layers instead of duplicating lexical storage.
|
| 119 |
+
|
| 120 |
+
### Grouped-query attention
|
| 121 |
+
|
| 122 |
+
The model has 8 query heads but only 2 key/value heads. Four query heads share each key/value head. Compared with standard multi-head attention using 8 independent KV heads, this reduces the size of the KV cache by approximately **4×** while preserving multiple query subspaces.
|
| 123 |
+
|
| 124 |
+
This design was chosen primarily for lower autoregressive inference memory and better generation throughput. The trade-off is reduced key/value diversity compared with full multi-head attention.
|
| 125 |
+
|
| 126 |
+
### Per-head QK-Norm
|
| 127 |
+
|
| 128 |
+
Queries and keys are independently RMS-normalized inside each attention head before attention scores are computed. This limits uncontrolled growth in their magnitudes, makes attention logits less sensitive to activation scale, and improves optimization stability during long pretraining runs.
|
| 129 |
+
|
| 130 |
+
QK-Norm adds very few parameters: only one scale vector for queries and one for keys in each layer.
|
| 131 |
+
|
| 132 |
+
### Content-dependent scalar residual gates
|
| 133 |
+
|
| 134 |
+
Both attention and MLP outputs are modulated before they are added to the residual stream:
|
| 135 |
+
|
| 136 |
+
```text
|
| 137 |
+
gate(x) = 2 × sigmoid(Wx)
|
| 138 |
+
output = gate(x) × branch(x)
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
The gate produces one scalar per token rather than a full hidden-size vector. It therefore lets the model strengthen or suppress an entire residual branch based on the current token representation with minimal parameter and compute overhead.
|
| 142 |
+
|
| 143 |
+
The multiplier of 2 centers the gate near 1 at initialization, so each gated branch begins close to an ordinary Transformer branch instead of being nearly closed. The model can then learn token-dependent deviations during training.
|
| 144 |
+
|
| 145 |
+
### Mistral-compatible foundation
|
| 146 |
+
|
| 147 |
+
Q-50M reuses the surrounding Hugging Face Mistral model interfaces, RMSNorm layout, SwiGLU MLP convention, generation utilities, and cache behavior. This reduces implementation risk and makes the custom architecture easier to use with the Transformers ecosystem. The novel parts remain localized in `QAttention`, `QMLP`, `QDecoderLayer`, and `QConfig`.
|
| 148 |
+
|
| 149 |
+
## Pretraining
|
| 150 |
+
|
| 151 |
+
| Setting | Value |
|
| 152 |
+
|---|---:|
|
| 153 |
+
| Dataset | [FineWeb-Edu](https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu), `sample-10BT` |
|
| 154 |
+
| Training tokens | 5,000,003,584 (~5B) |
|
| 155 |
+
| Training steps | 152,588 |
|
| 156 |
+
| Sequence length | 2,048 |
|
| 157 |
+
| Micro-batch size | 2 sequences |
|
| 158 |
+
| Gradient accumulation | 8 |
|
| 159 |
+
| Effective tokens per optimizer step | 32,768 |
|
| 160 |
+
| Optimizer | Fused AdamW |
|
| 161 |
+
| Peak learning rate | `3e-4` |
|
| 162 |
+
| Adam betas | `(0.9, 0.95)` |
|
| 163 |
+
| Weight decay | `0.1` |
|
| 164 |
+
| LR schedule | Warmup-stable-decay; 1% warmup, final 10% linear decay |
|
| 165 |
+
| Precision | FP16 |
|
| 166 |
+
| Compilation | `torch.compile` |
|
| 167 |
+
| Training hardware | 1× NVIDIA Tesla V100 16GB |
|
| 168 |
+
| Random seed | 2026 |
|
| 169 |
+
|
| 170 |
+
### Tokenizer
|
| 171 |
+
|
| 172 |
+
The model uses a 32,768-entry byte-level BPE tokenizer trained from scratch on FineWeb-Edu text. Byte-level fallback allows arbitrary UTF-8 text to be represented without unknown-character failures, although the model itself was trained primarily on English data.
|
| 173 |
+
|
| 174 |
+
## Evaluation
|
| 175 |
+
|
| 176 |
+
The final checkpoint was evaluated on a fixed FineWeb-Edu validation cache containing 524,288 tokens.
|
| 177 |
+
|
| 178 |
+
| Metric | Value |
|
| 179 |
+
|---|---:|
|
| 180 |
+
| Final training loss | 3.5127 |
|
| 181 |
+
| Validation loss | 3.1974 |
|
| 182 |
+
| Validation perplexity | 24.47 |
|
| 183 |
+
|
| 184 |
+
An observed greedy-generation speed on a Tesla V100 was approximately **36.6 tokens/s**. This is a local smoke-test measurement, not a standardized benchmark; speed depends on prompt length, generated length, precision, backend, and hardware.
|
| 185 |
+
|
| 186 |
+
Perplexity should only be compared directly with models evaluated using the same tokenizer, tokenization pipeline, context construction, and validation data.
|
| 187 |
+
|
| 188 |
+
## Usage
|
| 189 |
+
|
| 190 |
+
```python
|
| 191 |
+
import torch
|
| 192 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 193 |
+
|
| 194 |
+
model_id = "q-project/Q-50M-Base"
|
| 195 |
+
|
| 196 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 197 |
+
model_id,
|
| 198 |
+
trust_remote_code=True,
|
| 199 |
+
)
|
| 200 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 201 |
+
model_id,
|
| 202 |
+
trust_remote_code=True,
|
| 203 |
+
torch_dtype=torch.float16,
|
| 204 |
+
device_map="auto",
|
| 205 |
+
)
|
| 206 |
+
model.eval()
|
| 207 |
+
|
| 208 |
+
prompt = "Artificial intelligence can help people by"
|
| 209 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 210 |
+
|
| 211 |
+
with torch.inference_mode():
|
| 212 |
+
output = model.generate(
|
| 213 |
+
**inputs,
|
| 214 |
+
max_new_tokens=128,
|
| 215 |
+
do_sample=True,
|
| 216 |
+
temperature=0.8,
|
| 217 |
+
top_p=0.95,
|
| 218 |
+
top_k=50,
|
| 219 |
+
repetition_penalty=1.1,
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
| 223 |
+
```
|
| 224 |
+
|
| 225 |
+
`trust_remote_code=True` is required because the repository includes the custom `QConfig` and `QForCausalLM` implementations.
|
| 226 |
+
|
| 227 |
+
### Base-model prompting
|
| 228 |
+
|
| 229 |
+
Use a natural text prefix and let the model continue it. Do not apply a chat template to this checkpoint. For instruction following, use a separately released `Q-50M-Instruct` checkpoint.
|
| 230 |
+
|
| 231 |
+
## Intended use
|
| 232 |
+
|
| 233 |
+
Q-50M-Base is intended for:
|
| 234 |
+
|
| 235 |
+
- research on compact language-model architectures;
|
| 236 |
+
- experimentation with QK-Norm and lightweight residual gating;
|
| 237 |
+
- causal text continuation;
|
| 238 |
+
- educational use and local inference;
|
| 239 |
+
- supervised fine-tuning, instruction tuning, or domain adaptation.
|
| 240 |
+
|
| 241 |
+
It is not intended to be used as a factual authority, safety-critical system, autonomous agent, or production chat assistant without additional evaluation and alignment work.
|
| 242 |
+
|
| 243 |
+
## Limitations
|
| 244 |
+
|
| 245 |
+
- At 50.9M parameters, the model has limited factual knowledge, reasoning depth, and long-range coherence.
|
| 246 |
+
- The pretraining corpus is primarily English; quality in other languages is not established.
|
| 247 |
+
- The model can produce incorrect, biased, repetitive, unsafe, or nonsensical text.
|
| 248 |
+
- This base checkpoint was not instruction-tuned and may ignore questions or commands.
|
| 249 |
+
- A 2,048-token configured context does not imply uniform quality across the entire window.
|
| 250 |
+
- FineWeb-Edu is derived from web data and may contain residual errors or undesirable content despite filtering.
|
| 251 |
+
- The architecture and evaluation should be treated as an experimental research release.
|
| 252 |
+
|
| 253 |
+
Users are responsible for evaluating outputs and adding safeguards appropriate to their application.
|
| 254 |
+
|
| 255 |
+
## Repository files required for release
|
| 256 |
+
|
| 257 |
+
Because this is a custom Transformers architecture, the model repository should include:
|
| 258 |
+
|
| 259 |
+
```text
|
| 260 |
+
README.md
|
| 261 |
+
LICENSE
|
| 262 |
+
Q_Logo.svg
|
| 263 |
+
config.json
|
| 264 |
+
configuration_q.py
|
| 265 |
+
modeling_q.py
|
| 266 |
+
generation_config.json
|
| 267 |
+
model.safetensors
|
| 268 |
+
tokenizer.json
|
| 269 |
+
tokenizer_config.json
|
| 270 |
+
special_tokens_map.json
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
`config.json` must expose the custom classes through `auto_map`, for example:
|
| 274 |
+
|
| 275 |
+
```json
|
| 276 |
+
{
|
| 277 |
+
"auto_map": {
|
| 278 |
+
"AutoConfig": "configuration_q.QConfig",
|
| 279 |
+
"AutoModel": "modeling_q.QModel",
|
| 280 |
+
"AutoModelForCausalLM": "modeling_q.QForCausalLM"
|
| 281 |
+
}
|
| 282 |
+
}
|
| 283 |
+
```
|
| 284 |
+
|
| 285 |
+
## Citation
|
| 286 |
+
|
| 287 |
+
```bibtex
|
| 288 |
+
@misc{q50mbase2026,
|
| 289 |
+
title = {Q-50M-Base: A Compact Gated Decoder Language Model},
|
| 290 |
+
author = {Nikolay Kompanets},
|
| 291 |
+
year = {2026},
|
| 292 |
+
url = {https://huggingface.co/q-project/Q-50M-Base}
|
| 293 |
+
}
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
## Acknowledgements
|
| 297 |
+
|
| 298 |
+
Q-50M-Base uses the Hugging Face Transformers ecosystem and was pretrained on FineWeb-Edu. The model implementation derives its standard decoder components and public interfaces from the Transformers Mistral implementation, with custom Q-Project attention normalization and residual gating modules.
|
LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Apache License
|
| 3 |
+
Version 2.0, January 2004
|
| 4 |
+
http://www.apache.org/licenses/
|
| 5 |
+
|
| 6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 7 |
+
|
| 8 |
+
1. Definitions.
|
| 9 |
+
|
| 10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 12 |
+
|
| 13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 14 |
+
the copyright owner that is granting the License.
|
| 15 |
+
|
| 16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 17 |
+
other entities that control, are controlled by, or are under common
|
| 18 |
+
control with that entity. For the purposes of this definition,
|
| 19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 20 |
+
direction or management of such entity, whether by contract or
|
| 21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 23 |
+
|
| 24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 25 |
+
exercising permissions granted by this License.
|
| 26 |
+
|
| 27 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
+
including but not limited to software source code, documentation
|
| 29 |
+
source, and configuration files.
|
| 30 |
+
|
| 31 |
+
"Object" form shall mean any form resulting from mechanical
|
| 32 |
+
transformation or translation of a Source form, including but
|
| 33 |
+
not limited to compiled object code, generated documentation,
|
| 34 |
+
and conversions to other media types.
|
| 35 |
+
|
| 36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
+
Object form, made available under the License, as indicated by a
|
| 38 |
+
copyright notice that is included in or attached to the work
|
| 39 |
+
(an example is provided in the Appendix below).
|
| 40 |
+
|
| 41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
+
form, that is based on (or derived from) the Work and for which the
|
| 43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
+
of this License, Derivative Works shall not include works that remain
|
| 46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
+
the Work and Derivative Works thereof.
|
| 48 |
+
|
| 49 |
+
"Contribution" shall mean any work of authorship, including
|
| 50 |
+
the original version of the Work and any modifications or additions
|
| 51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
+
means any form of electronic, verbal, or written communication sent
|
| 56 |
+
to the Licensor or its representatives, including but not limited to
|
| 57 |
+
communication on electronic mailing lists, source code control systems,
|
| 58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
+
excluding communication that is conspicuously marked or otherwise
|
| 61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
+
|
| 63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
+
subsequently incorporated within the Work.
|
| 66 |
+
|
| 67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
+
Work and such Derivative Works in Source or Object form.
|
| 73 |
+
|
| 74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
+
(except as stated in this section) patent license to make, have made,
|
| 78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
+
where such license applies only to those patent claims licensable
|
| 80 |
+
by such Contributor that are necessarily infringed by their
|
| 81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
+
institute patent litigation against any entity (including a
|
| 84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
+
or contributory patent infringement, then any patent licenses
|
| 87 |
+
granted to You under this License for that Work shall terminate
|
| 88 |
+
as of the date such litigation is filed.
|
| 89 |
+
|
| 90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
+
modifications, and in Source or Object form, provided that You
|
| 93 |
+
meet the following conditions:
|
| 94 |
+
|
| 95 |
+
(a) You must give any other recipients of the Work or
|
| 96 |
+
Derivative Works a copy of this License; and
|
| 97 |
+
|
| 98 |
+
(b) You must cause any modified files to carry prominent notices
|
| 99 |
+
stating that You changed the files; and
|
| 100 |
+
|
| 101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
+
that You distribute, all copyright, patent, trademark, and
|
| 103 |
+
attribution notices from the Source form of the Work,
|
| 104 |
+
excluding those notices that do not pertain to any part of
|
| 105 |
+
the Derivative Works; and
|
| 106 |
+
|
| 107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
+
distribution, then any Derivative Works that You distribute must
|
| 109 |
+
include a readable copy of the attribution notices contained
|
| 110 |
+
within such NOTICE file, excluding those notices that do not
|
| 111 |
+
pertain to any part of the Derivative Works, in at least one
|
| 112 |
+
of the following places: within a NOTICE text file distributed
|
| 113 |
+
as part of the Derivative Works; within the Source form or
|
| 114 |
+
documentation, if provided along with the Derivative Works; or,
|
| 115 |
+
within a display generated by the Derivative Works, if and
|
| 116 |
+
wherever such third-party notices normally appear. The contents
|
| 117 |
+
of the NOTICE file are for informational purposes only and
|
| 118 |
+
do not modify the License. You may add Your own attribution
|
| 119 |
+
notices within Derivative Works that You distribute, alongside
|
| 120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
+
that such additional attribution notices cannot be construed
|
| 122 |
+
as modifying the License.
|
| 123 |
+
|
| 124 |
+
You may add Your own copyright statement to Your modifications and
|
| 125 |
+
may provide additional or different license terms and conditions
|
| 126 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
+
the conditions stated in this License.
|
| 130 |
+
|
| 131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
+
this License, without any additional terms or conditions.
|
| 135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
+
the terms of any separate license agreement you may have executed
|
| 137 |
+
with Licensor regarding such Contributions.
|
| 138 |
+
|
| 139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
+
except as required for reasonable and customary use in describing the
|
| 142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
+
|
| 144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
+
implied, including, without limitation, any warranties or conditions
|
| 149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
+
appropriateness of using or redistributing the Work and assume any
|
| 152 |
+
risks associated with Your exercise of permissions under this License.
|
| 153 |
+
|
| 154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
+
unless required by applicable law (such as deliberate and grossly
|
| 157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
+
liable to You for damages, including any direct, indirect, special,
|
| 159 |
+
incidental, or consequential damages of any character arising as a
|
| 160 |
+
result of this License or out of the use or inability to use the
|
| 161 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
+
other commercial damages or losses), even if such Contributor
|
| 164 |
+
has been advised of the possibility of such damages.
|
| 165 |
+
|
| 166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
+
or other liability obligations and/or rights consistent with this
|
| 170 |
+
License. However, in accepting such obligations, You may act only
|
| 171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
+
defend, and hold each Contributor harmless for any liability
|
| 174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
+
of your accepting any such warranty or additional liability.
|
| 176 |
+
|
| 177 |
+
END OF TERMS AND CONDITIONS
|
| 178 |
+
|
| 179 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
+
|
| 181 |
+
To apply the Apache License to your work, attach the following
|
| 182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
+
replaced with your own identifying information. (Don't include
|
| 184 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
+
comment syntax for the file format. We also recommend that a
|
| 186 |
+
file or class name and description of purpose be included on the
|
| 187 |
+
same "printed page" as the copyright notice for easier
|
| 188 |
+
identification within third-party archives.
|
| 189 |
+
|
| 190 |
+
Copyright [yyyy] [name of copyright owner]
|
| 191 |
+
|
| 192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
+
you may not use this file except in compliance with the License.
|
| 194 |
+
You may obtain a copy of the License at
|
| 195 |
+
|
| 196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
+
|
| 198 |
+
Unless required by applicable law or agreed to in writing, software
|
| 199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
+
See the License for the specific language governing permissions and
|
| 202 |
+
limitations under the License.
|
Q_Logo.svg
ADDED
|
|
README.md
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
datasets:
|
| 8 |
+
- HuggingFaceFW/fineweb-edu
|
| 9 |
+
tags:
|
| 10 |
+
- causal-lm
|
| 11 |
+
- custom-architecture
|
| 12 |
+
- pretrained
|
| 13 |
+
- base-model
|
| 14 |
+
- grouped-query-attention
|
| 15 |
+
- qk-norm
|
| 16 |
+
- gated-residuals
|
| 17 |
+
- tiny-model
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
<div align="center">
|
| 21 |
+
<img src="Q_Logo.svg" alt="Q Logo" width="220"/>
|
| 22 |
+
<h1>Q-50M-Base</h1>
|
| 23 |
+
<h3>A compact gated decoder language model pretrained from scratch</h3>
|
| 24 |
+
|
| 25 |
+
<p>
|
| 26 |
+
<a href="https://huggingface.co/q-project/Q-50M-Base"><img src="https://img.shields.io/badge/Parameters-50.9M-red?style=for-the-badge&logo=huggingface" alt="Parameters"></a>
|
| 27 |
+
<a href="https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu"><img src="https://img.shields.io/badge/Pretraining-5B%20FineWeb--Edu%20tokens-blue?style=for-the-badge" alt="Training data"></a>
|
| 28 |
+
<a href="LICENSE"><img src="https://img.shields.io/badge/License-Apache%202.0-green?style=for-the-badge" alt="License"></a>
|
| 29 |
+
</p>
|
| 30 |
+
</div>
|
| 31 |
+
|
| 32 |
+
> [!IMPORTANT]
|
| 33 |
+
> **Q-50M-Base is a base language model, not a chat or instruction model.** It is designed for text continuation and as a starting point for fine-tuning. It should not be expected to reliably follow user instructions.
|
| 34 |
+
|
| 35 |
+
## Model summary
|
| 36 |
+
|
| 37 |
+
**Q-50M-Base** is a 50.9M-parameter decoder-only causal language model developed by Q-Project. It was pretrained from scratch on approximately 5 billion tokens from FineWeb-Edu and does not inherit weights from another model.
|
| 38 |
+
|
| 39 |
+
The model keeps the well-tested shape of a modern Transformer decoder while adding three inexpensive mechanisms intended to improve stability and parameter efficiency at small scale:
|
| 40 |
+
|
| 41 |
+
1. **Grouped-query attention (GQA)** with 8 query heads and 2 key/value heads.
|
| 42 |
+
2. **Per-head QK-Norm** before the attention score calculation.
|
| 43 |
+
3. **Content-dependent scalar gates** on both the attention and MLP residual branches.
|
| 44 |
+
|
| 45 |
+
The implementation is built on the Hugging Face Mistral classes for compatibility with the Transformers generation and cache APIs, but the attention and decoder layers are replaced by Q-Project implementations.
|
| 46 |
+
|
| 47 |
+
## Architecture
|
| 48 |
+
|
| 49 |
+
```text
|
| 50 |
+
Input token IDs
|
| 51 |
+
│
|
| 52 |
+
▼
|
| 53 |
+
Tied token embedding (32,768 × 512)
|
| 54 |
+
│
|
| 55 |
+
▼
|
| 56 |
+
┌──────────────────────────────────────────┐
|
| 57 |
+
│ QDecoderLayer × 10 │
|
| 58 |
+
│ │
|
| 59 |
+
│ RMSNorm │
|
| 60 |
+
│ │ │
|
| 61 |
+
│ ▼ │
|
| 62 |
+
│ GQA: 8 query heads / 2 KV heads │
|
| 63 |
+
│ ├─ per-head QK-Norm │
|
| 64 |
+
│ ├─ RoPE positional encoding │
|
| 65 |
+
│ └─ content-dependent scalar output gate │
|
| 66 |
+
│ │ │
|
| 67 |
+
│ └──────────── residual connection │
|
| 68 |
+
│ │
|
| 69 |
+
│ RMSNorm │
|
| 70 |
+
│ │ │
|
| 71 |
+
│ ▼ │
|
| 72 |
+
│ SwiGLU MLP (512 → 1,792 → 512) │
|
| 73 |
+
│ └─ content-dependent scalar output gate │
|
| 74 |
+
│ │ │
|
| 75 |
+
│ └──────────── residual connection │
|
| 76 |
+
└──────────────────────────────────────────┘
|
| 77 |
+
│
|
| 78 |
+
▼
|
| 79 |
+
Final RMSNorm
|
| 80 |
+
│
|
| 81 |
+
▼
|
| 82 |
+
Tied language-model head → next-token logits
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
### Configuration
|
| 86 |
+
|
| 87 |
+
| Parameter | Value |
|
| 88 |
+
|---|---:|
|
| 89 |
+
| Total parameters | 50,878,208 |
|
| 90 |
+
| Vocabulary size | 32,768 |
|
| 91 |
+
| Hidden size | 512 |
|
| 92 |
+
| Decoder layers | 10 |
|
| 93 |
+
| Query attention heads | 8 |
|
| 94 |
+
| Key/value heads | 2 |
|
| 95 |
+
| Attention head dimension | 64 |
|
| 96 |
+
| MLP intermediate size | 1,792 |
|
| 97 |
+
| Maximum context length | 2,048 tokens |
|
| 98 |
+
| Positional encoding | RoPE in every layer |
|
| 99 |
+
| RoPE theta | 10,000 |
|
| 100 |
+
| Normalization | RMSNorm, epsilon `1e-5` |
|
| 101 |
+
| Attention stabilization | Per-head QK-Norm |
|
| 102 |
+
| MLP | SwiGLU / SiLU |
|
| 103 |
+
| Residual branch gates | Scalar, content-dependent, attention + MLP |
|
| 104 |
+
| Embedding / LM head | Tied |
|
| 105 |
+
| Attention dropout | 0.0 |
|
| 106 |
+
| Training attention backend | PyTorch SDPA |
|
| 107 |
+
|
| 108 |
+
The implementation supports selective NoPE layers for architecture experiments. This checkpoint sets `nope_every_n=None`, so **RoPE is active in all ten layers**.
|
| 109 |
+
|
| 110 |
+
## Why this architecture?
|
| 111 |
+
|
| 112 |
+
### A 512-wide, 10-layer decoder
|
| 113 |
+
|
| 114 |
+
At roughly 50M parameters, allocating capacity is a trade-off between vocabulary embeddings, depth, width, and MLP size. Ten 512-dimensional layers give the model enough sequential depth to build progressively richer representations without making every attention and MLP operation too expensive. The 1,792-dimensional SwiGLU MLP provides substantial nonlinear capacity while keeping the full model within the target size.
|
| 115 |
+
|
| 116 |
+
### Tied input and output embeddings
|
| 117 |
+
|
| 118 |
+
With a 32K vocabulary and a hidden size of 512, one embedding matrix contains about 16.8M parameters. Sharing it with the output language-model head avoids a second matrix of the same size. This is especially important in a 50M model: the saved parameters can be used in the decoder layers instead of duplicating lexical storage.
|
| 119 |
+
|
| 120 |
+
### Grouped-query attention
|
| 121 |
+
|
| 122 |
+
The model has 8 query heads but only 2 key/value heads. Four query heads share each key/value head. Compared with standard multi-head attention using 8 independent KV heads, this reduces the size of the KV cache by approximately **4×** while preserving multiple query subspaces.
|
| 123 |
+
|
| 124 |
+
This design was chosen primarily for lower autoregressive inference memory and better generation throughput. The trade-off is reduced key/value diversity compared with full multi-head attention.
|
| 125 |
+
|
| 126 |
+
### Per-head QK-Norm
|
| 127 |
+
|
| 128 |
+
Queries and keys are independently RMS-normalized inside each attention head before attention scores are computed. This limits uncontrolled growth in their magnitudes, makes attention logits less sensitive to activation scale, and improves optimization stability during long pretraining runs.
|
| 129 |
+
|
| 130 |
+
QK-Norm adds very few parameters: only one scale vector for queries and one for keys in each layer.
|
| 131 |
+
|
| 132 |
+
### Content-dependent scalar residual gates
|
| 133 |
+
|
| 134 |
+
Both attention and MLP outputs are modulated before they are added to the residual stream:
|
| 135 |
+
|
| 136 |
+
```text
|
| 137 |
+
gate(x) = 2 × sigmoid(Wx)
|
| 138 |
+
output = gate(x) × branch(x)
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
The gate produces one scalar per token rather than a full hidden-size vector. It therefore lets the model strengthen or suppress an entire residual branch based on the current token representation with minimal parameter and compute overhead.
|
| 142 |
+
|
| 143 |
+
The multiplier of 2 centers the gate near 1 at initialization, so each gated branch begins close to an ordinary Transformer branch instead of being nearly closed. The model can then learn token-dependent deviations during training.
|
| 144 |
+
|
| 145 |
+
### Mistral-compatible foundation
|
| 146 |
+
|
| 147 |
+
Q-50M reuses the surrounding Hugging Face Mistral model interfaces, RMSNorm layout, SwiGLU MLP convention, generation utilities, and cache behavior. This reduces implementation risk and makes the custom architecture easier to use with the Transformers ecosystem. The novel parts remain localized in `QAttention`, `QMLP`, `QDecoderLayer`, and `QConfig`.
|
| 148 |
+
|
| 149 |
+
## Pretraining
|
| 150 |
+
|
| 151 |
+
| Setting | Value |
|
| 152 |
+
|---|---:|
|
| 153 |
+
| Dataset | [FineWeb-Edu](https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu), `sample-10BT` |
|
| 154 |
+
| Training tokens | 5,000,003,584 (~5B) |
|
| 155 |
+
| Training steps | 152,588 |
|
| 156 |
+
| Sequence length | 2,048 |
|
| 157 |
+
| Micro-batch size | 2 sequences |
|
| 158 |
+
| Gradient accumulation | 8 |
|
| 159 |
+
| Effective tokens per optimizer step | 32,768 |
|
| 160 |
+
| Optimizer | Fused AdamW |
|
| 161 |
+
| Peak learning rate | `3e-4` |
|
| 162 |
+
| Adam betas | `(0.9, 0.95)` |
|
| 163 |
+
| Weight decay | `0.1` |
|
| 164 |
+
| LR schedule | Warmup-stable-decay; 1% warmup, final 10% linear decay |
|
| 165 |
+
| Precision | FP16 |
|
| 166 |
+
| Compilation | `torch.compile` |
|
| 167 |
+
| Training hardware | 1× NVIDIA Tesla V100 16GB |
|
| 168 |
+
| Random seed | 2026 |
|
| 169 |
+
|
| 170 |
+
### Tokenizer
|
| 171 |
+
|
| 172 |
+
The model uses a 32,768-entry byte-level BPE tokenizer trained from scratch on FineWeb-Edu text. Byte-level fallback allows arbitrary UTF-8 text to be represented without unknown-character failures, although the model itself was trained primarily on English data.
|
| 173 |
+
|
| 174 |
+
## Evaluation
|
| 175 |
+
|
| 176 |
+
The final checkpoint was evaluated on a fixed FineWeb-Edu validation cache containing 524,288 tokens.
|
| 177 |
+
|
| 178 |
+
| Metric | Value |
|
| 179 |
+
|---|---:|
|
| 180 |
+
| Final training loss | 3.5127 |
|
| 181 |
+
| Validation loss | 3.1974 |
|
| 182 |
+
| Validation perplexity | 24.47 |
|
| 183 |
+
|
| 184 |
+
An observed greedy-generation speed on a Tesla V100 was approximately **36.6 tokens/s**. This is a local smoke-test measurement, not a standardized benchmark; speed depends on prompt length, generated length, precision, backend, and hardware.
|
| 185 |
+
|
| 186 |
+
Perplexity should only be compared directly with models evaluated using the same tokenizer, tokenization pipeline, context construction, and validation data.
|
| 187 |
+
|
| 188 |
+
## Usage
|
| 189 |
+
|
| 190 |
+
```python
|
| 191 |
+
import torch
|
| 192 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 193 |
+
|
| 194 |
+
model_id = "q-project/Q-50M-Base"
|
| 195 |
+
|
| 196 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 197 |
+
model_id,
|
| 198 |
+
trust_remote_code=True,
|
| 199 |
+
)
|
| 200 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 201 |
+
model_id,
|
| 202 |
+
trust_remote_code=True,
|
| 203 |
+
torch_dtype=torch.float16,
|
| 204 |
+
device_map="auto",
|
| 205 |
+
)
|
| 206 |
+
model.eval()
|
| 207 |
+
|
| 208 |
+
prompt = "Artificial intelligence can help people by"
|
| 209 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 210 |
+
|
| 211 |
+
with torch.inference_mode():
|
| 212 |
+
output = model.generate(
|
| 213 |
+
**inputs,
|
| 214 |
+
max_new_tokens=128,
|
| 215 |
+
do_sample=True,
|
| 216 |
+
temperature=0.8,
|
| 217 |
+
top_p=0.95,
|
| 218 |
+
top_k=50,
|
| 219 |
+
repetition_penalty=1.1,
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
| 223 |
+
```
|
| 224 |
+
|
| 225 |
+
`trust_remote_code=True` is required because the repository includes the custom `QConfig` and `QForCausalLM` implementations.
|
| 226 |
+
|
| 227 |
+
### Base-model prompting
|
| 228 |
+
|
| 229 |
+
Use a natural text prefix and let the model continue it. Do not apply a chat template to this checkpoint. For instruction following, use a separately released `Q-50M-Instruct` checkpoint.
|
| 230 |
+
|
| 231 |
+
## Intended use
|
| 232 |
+
|
| 233 |
+
Q-50M-Base is intended for:
|
| 234 |
+
|
| 235 |
+
- research on compact language-model architectures;
|
| 236 |
+
- experimentation with QK-Norm and lightweight residual gating;
|
| 237 |
+
- causal text continuation;
|
| 238 |
+
- educational use and local inference;
|
| 239 |
+
- supervised fine-tuning, instruction tuning, or domain adaptation.
|
| 240 |
+
|
| 241 |
+
It is not intended to be used as a factual authority, safety-critical system, autonomous agent, or production chat assistant without additional evaluation and alignment work.
|
| 242 |
+
|
| 243 |
+
## Limitations
|
| 244 |
+
|
| 245 |
+
- At 50.9M parameters, the model has limited factual knowledge, reasoning depth, and long-range coherence.
|
| 246 |
+
- The pretraining corpus is primarily English; quality in other languages is not established.
|
| 247 |
+
- The model can produce incorrect, biased, repetitive, unsafe, or nonsensical text.
|
| 248 |
+
- This base checkpoint was not instruction-tuned and may ignore questions or commands.
|
| 249 |
+
- A 2,048-token configured context does not imply uniform quality across the entire window.
|
| 250 |
+
- FineWeb-Edu is derived from web data and may contain residual errors or undesirable content despite filtering.
|
| 251 |
+
- The architecture and evaluation should be treated as an experimental research release.
|
| 252 |
+
|
| 253 |
+
Users are responsible for evaluating outputs and adding safeguards appropriate to their application.
|
| 254 |
+
|
| 255 |
+
## Repository files required for release
|
| 256 |
+
|
| 257 |
+
Because this is a custom Transformers architecture, the model repository should include:
|
| 258 |
+
|
| 259 |
+
```text
|
| 260 |
+
README.md
|
| 261 |
+
LICENSE
|
| 262 |
+
Q_Logo.svg
|
| 263 |
+
config.json
|
| 264 |
+
configuration_q.py
|
| 265 |
+
modeling_q.py
|
| 266 |
+
generation_config.json
|
| 267 |
+
model.safetensors
|
| 268 |
+
tokenizer.json
|
| 269 |
+
tokenizer_config.json
|
| 270 |
+
special_tokens_map.json
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
`config.json` must expose the custom classes through `auto_map`, for example:
|
| 274 |
+
|
| 275 |
+
```json
|
| 276 |
+
{
|
| 277 |
+
"auto_map": {
|
| 278 |
+
"AutoConfig": "configuration_q.QConfig",
|
| 279 |
+
"AutoModel": "modeling_q.QModel",
|
| 280 |
+
"AutoModelForCausalLM": "modeling_q.QForCausalLM"
|
| 281 |
+
}
|
| 282 |
+
}
|
| 283 |
+
```
|
| 284 |
+
|
| 285 |
+
## Citation
|
| 286 |
+
|
| 287 |
+
```bibtex
|
| 288 |
+
@misc{q50mbase2026,
|
| 289 |
+
title = {Q-50M-Base: A Compact Gated Decoder Language Model},
|
| 290 |
+
author = {Nikolay Kompanets},
|
| 291 |
+
year = {2026},
|
| 292 |
+
url = {https://huggingface.co/q-project/Q-50M-Base}
|
| 293 |
+
}
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
## Acknowledgements
|
| 297 |
+
|
| 298 |
+
Q-50M-Base uses the Hugging Face Transformers ecosystem and was pretrained on FineWeb-Edu. The model implementation derives its standard decoder components and public interfaces from the Transformers Mistral implementation, with custom Q-Project attention normalization and residual gating modules.
|
config.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"QForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_dropout": 0.0,
|
| 6 |
+
"attention_scalar_gate": true,
|
| 7 |
+
"bos_token_id": 1,
|
| 8 |
+
"dtype": "float32",
|
| 9 |
+
"eos_token_id": 2,
|
| 10 |
+
"gate_multiplier": 2.0,
|
| 11 |
+
"head_dim": 64,
|
| 12 |
+
"hidden_act": "silu",
|
| 13 |
+
"hidden_size": 512,
|
| 14 |
+
"initializer_range": 0.02,
|
| 15 |
+
"intermediate_size": 1792,
|
| 16 |
+
"max_position_embeddings": 2048,
|
| 17 |
+
"mlp_scalar_gate": true,
|
| 18 |
+
"model_type": "q",
|
| 19 |
+
"nope_every_n": null,
|
| 20 |
+
"nope_layers": [],
|
| 21 |
+
"num_attention_heads": 8,
|
| 22 |
+
"num_hidden_layers": 10,
|
| 23 |
+
"num_key_value_heads": 2,
|
| 24 |
+
"pad_token_id": 0,
|
| 25 |
+
"qk_norm": true,
|
| 26 |
+
"rms_norm_eps": 1e-05,
|
| 27 |
+
"rope_parameters": {
|
| 28 |
+
"rope_theta": 10000.0,
|
| 29 |
+
"rope_type": "default"
|
| 30 |
+
},
|
| 31 |
+
"sliding_window": 4096,
|
| 32 |
+
"tie_word_embeddings": true,
|
| 33 |
+
"transformers_version": "5.5.0",
|
| 34 |
+
"use_cache": true,
|
| 35 |
+
"vocab_size": 32768,
|
| 36 |
+
"auto_map": {
|
| 37 |
+
"AutoConfig": "configuration_q.QConfig",
|
| 38 |
+
"AutoModel": "modeling_q.QModel",
|
| 39 |
+
"AutoModelForCausalLM": "modeling_q.QForCausalLM"
|
| 40 |
+
}
|
| 41 |
+
}
|
configuration_q.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.models.mistral.configuration_mistral import MistralConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class QConfig(MistralConfig):
|
| 5 |
+
model_type = "q"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
qk_norm=True,
|
| 10 |
+
attention_scalar_gate=True,
|
| 11 |
+
mlp_scalar_gate=True,
|
| 12 |
+
gate_multiplier=2.0,
|
| 13 |
+
nope_every_n=4,
|
| 14 |
+
nope_layers=None,
|
| 15 |
+
**kwargs,
|
| 16 |
+
):
|
| 17 |
+
super().__init__(**kwargs)
|
| 18 |
+
self.qk_norm = qk_norm
|
| 19 |
+
self.attention_scalar_gate = attention_scalar_gate
|
| 20 |
+
self.mlp_scalar_gate = mlp_scalar_gate
|
| 21 |
+
self.gate_multiplier = gate_multiplier
|
| 22 |
+
self.nope_every_n = nope_every_n
|
| 23 |
+
if nope_layers is not None:
|
| 24 |
+
self.nope_layers = list(nope_layers)
|
| 25 |
+
elif nope_every_n:
|
| 26 |
+
self.nope_layers = [
|
| 27 |
+
i for i in range(self.num_hidden_layers) if (i + 1) % nope_every_n == 0
|
| 28 |
+
]
|
| 29 |
+
else:
|
| 30 |
+
self.nope_layers = []
|
generation_config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": [
|
| 5 |
+
2
|
| 6 |
+
],
|
| 7 |
+
"output_attentions": false,
|
| 8 |
+
"output_hidden_states": false,
|
| 9 |
+
"pad_token_id": 0,
|
| 10 |
+
"transformers_version": "5.5.0",
|
| 11 |
+
"use_cache": false
|
| 12 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e2697d59322647e2eab298479d82c43be0f957ed5f273c90b4240ea86dabd998
|
| 3 |
+
size 203527584
|
modeling_q.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Callable
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS
|
| 6 |
+
from transformers.models.mistral.modeling_mistral import (
|
| 7 |
+
MistralAttention,
|
| 8 |
+
MistralDecoderLayer,
|
| 9 |
+
MistralForCausalLM,
|
| 10 |
+
MistralMLP,
|
| 11 |
+
MistralModel,
|
| 12 |
+
MistralRMSNorm,
|
| 13 |
+
apply_rotary_pos_emb,
|
| 14 |
+
eager_attention_forward,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
from .configuration_q import QConfig
|
| 19 |
+
except ImportError: # запуск сгенерированного файла прямо из каталога ноутбука
|
| 20 |
+
from configuration_q import QConfig
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class QScalarGate(nn.Module):
|
| 24 |
+
def __init__(self, hidden_size, multiplier=2.0):
|
| 25 |
+
super().__init__()
|
| 26 |
+
self.projection = nn.Linear(hidden_size, 1, bias=False)
|
| 27 |
+
self.multiplier = multiplier
|
| 28 |
+
|
| 29 |
+
def forward(self, branch, residual_input):
|
| 30 |
+
return branch * self.multiplier * torch.sigmoid(self.projection(residual_input))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class QMLP(MistralMLP):
|
| 34 |
+
def __init__(self, config):
|
| 35 |
+
super().__init__(config)
|
| 36 |
+
self.output_gate = (
|
| 37 |
+
QScalarGate(config.hidden_size, config.gate_multiplier)
|
| 38 |
+
if config.mlp_scalar_gate else None
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
def forward(self, hidden_states):
|
| 42 |
+
output = super().forward(hidden_states)
|
| 43 |
+
if self.output_gate is not None:
|
| 44 |
+
output = self.output_gate(output, hidden_states)
|
| 45 |
+
return output
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class QAttention(MistralAttention):
|
| 49 |
+
def __init__(self, config, layer_idx):
|
| 50 |
+
super().__init__(config, layer_idx)
|
| 51 |
+
self.use_rope = layer_idx not in config.nope_layers
|
| 52 |
+
self.q_norm = MistralRMSNorm(self.head_dim, eps=config.rms_norm_eps) if config.qk_norm else None
|
| 53 |
+
self.k_norm = MistralRMSNorm(self.head_dim, eps=config.rms_norm_eps) if config.qk_norm else None
|
| 54 |
+
self.output_gate = (
|
| 55 |
+
QScalarGate(config.hidden_size, config.gate_multiplier)
|
| 56 |
+
if config.attention_scalar_gate else None
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def forward(self, hidden_states, position_embeddings, attention_mask, past_key_values=None, **kwargs):
|
| 60 |
+
input_shape = hidden_states.shape[:-1]
|
| 61 |
+
hidden_shape = (*input_shape, -1, self.head_dim)
|
| 62 |
+
query_states = self.q_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 63 |
+
key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 64 |
+
value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 65 |
+
|
| 66 |
+
if self.q_norm is not None:
|
| 67 |
+
query_states = self.q_norm(query_states)
|
| 68 |
+
key_states = self.k_norm(key_states)
|
| 69 |
+
if self.use_rope:
|
| 70 |
+
cos, sin = position_embeddings
|
| 71 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
|
| 72 |
+
|
| 73 |
+
if past_key_values is not None:
|
| 74 |
+
key_states, value_states = past_key_values.update(
|
| 75 |
+
key_states, value_states, self.layer_idx
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
|
| 79 |
+
self.config._attn_implementation, eager_attention_forward
|
| 80 |
+
)
|
| 81 |
+
attn_output, attn_weights = attention_interface(
|
| 82 |
+
self, query_states, key_states, value_states, attention_mask,
|
| 83 |
+
dropout=0.0 if not self.training else self.attention_dropout,
|
| 84 |
+
scaling=self.scaling,
|
| 85 |
+
sliding_window=getattr(self.config, "sliding_window", None),
|
| 86 |
+
**kwargs,
|
| 87 |
+
)
|
| 88 |
+
attn_output = attn_output.reshape(*input_shape, -1).contiguous()
|
| 89 |
+
attn_output = self.o_proj(attn_output)
|
| 90 |
+
if self.output_gate is not None:
|
| 91 |
+
attn_output = self.output_gate(attn_output, hidden_states)
|
| 92 |
+
return attn_output, attn_weights
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class QDecoderLayer(MistralDecoderLayer):
|
| 96 |
+
def __init__(self, config, layer_idx):
|
| 97 |
+
super().__init__(config, layer_idx)
|
| 98 |
+
self.self_attn = QAttention(config, layer_idx)
|
| 99 |
+
self.mlp = QMLP(config)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class QModel(MistralModel):
|
| 103 |
+
config_class = QConfig
|
| 104 |
+
|
| 105 |
+
def __init__(self, config):
|
| 106 |
+
super().__init__(config)
|
| 107 |
+
self.layers = nn.ModuleList(
|
| 108 |
+
[QDecoderLayer(config, i) for i in range(config.num_hidden_layers)]
|
| 109 |
+
)
|
| 110 |
+
self.post_init()
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
class QForCausalLM(MistralForCausalLM):
|
| 114 |
+
config_class = QConfig
|
| 115 |
+
|
| 116 |
+
def __init__(self, config):
|
| 117 |
+
super().__init__(config)
|
| 118 |
+
self.model = QModel(config)
|
| 119 |
+
self.post_init()
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "<s>",
|
| 4 |
+
"eos_token": "</s>",
|
| 5 |
+
"is_local": true,
|
| 6 |
+
"model_max_length": 2048,
|
| 7 |
+
"pad_token": "<pad>",
|
| 8 |
+
"tokenizer_class": "TokenizersBackend",
|
| 9 |
+
"unk_token": "<unk>"
|
| 10 |
+
}
|
training_metrics.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"eval_loss": 3.197364330291748,
|
| 3 |
+
"eval_runtime": 14.2089,
|
| 4 |
+
"eval_samples_per_second": 18.017,
|
| 5 |
+
"eval_steps_per_second": 9.008,
|
| 6 |
+
"epoch": 1.0,
|
| 7 |
+
"perplexity": 24.467955686324988
|
| 8 |
+
}
|