Update README.md
Browse files
README.md
CHANGED
|
@@ -1,199 +1,252 @@
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
| 2 |
library_name: transformers
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
---
|
| 5 |
|
| 6 |
-
#
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
-
|
| 20 |
-
- **Developed by:** [More Information Needed]
|
| 21 |
-
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
-
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
-
- **Model type:** [More Information Needed]
|
| 24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
-
|
| 28 |
-
### Model Sources [optional]
|
| 29 |
-
|
| 30 |
-
<!-- Provide the basic links for the model. -->
|
| 31 |
-
|
| 32 |
-
- **Repository:** [More Information Needed]
|
| 33 |
-
- **Paper [optional]:** [More Information Needed]
|
| 34 |
-
- **Demo [optional]:** [More Information Needed]
|
| 35 |
-
|
| 36 |
-
## Uses
|
| 37 |
-
|
| 38 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
-
|
| 40 |
-
### Direct Use
|
| 41 |
-
|
| 42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
|
| 76 |
## Training Details
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
-
|
| 82 |
-
[More Information Needed]
|
| 83 |
|
| 84 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#### Training Hyperparameters
|
| 94 |
-
|
| 95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
-
|
| 97 |
-
#### Speeds, Sizes, Times [optional]
|
| 98 |
-
|
| 99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
-
|
| 101 |
-
[More Information Needed]
|
| 102 |
-
|
| 103 |
-
## Evaluation
|
| 104 |
-
|
| 105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
-
|
| 107 |
-
### Testing Data, Factors & Metrics
|
| 108 |
-
|
| 109 |
-
#### Testing Data
|
| 110 |
-
|
| 111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
-
|
| 113 |
-
[More Information Needed]
|
| 114 |
-
|
| 115 |
-
#### Factors
|
| 116 |
-
|
| 117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
-
|
| 119 |
-
[More Information Needed]
|
| 120 |
-
|
| 121 |
-
#### Metrics
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
|
|
|
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
[More Information Needed]
|
| 158 |
|
| 159 |
-
|
| 160 |
|
| 161 |
-
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
|
| 167 |
-
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
| 180 |
|
| 181 |
-
|
| 182 |
|
| 183 |
-
|
| 184 |
|
| 185 |
-
|
| 186 |
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
## More Information [optional]
|
| 190 |
|
| 191 |
-
|
| 192 |
|
| 193 |
-
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
[More Information Needed]
|
|
|
|
| 1 |
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: apache-2.0
|
| 5 |
library_name: transformers
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
tags:
|
| 8 |
+
- mixture-of-attentions
|
| 9 |
+
- distance-attention
|
| 10 |
+
- metric-attention
|
| 11 |
+
- mqa
|
| 12 |
+
- hyperffn
|
| 13 |
+
- router-gating
|
| 14 |
---
|
| 15 |
|
| 16 |
+
# MoAMetricLM-185M — Mixture of Attentions (MoA)
|
| 17 |
|
| 18 |
+
**A geometry-aware Transformer with a mixture of attention mechanisms and metric-based routing.**
|
| 19 |
+
**Parameters:** ~185M | **Type:** Causal LM (decoder-only) | **KV cache:** not yet implemented
|
| 20 |
|
| 21 |
+
---
|
| 22 |
|
| 23 |
+
## Model Index
|
| 24 |
|
| 25 |
+
- **Model ID:** `your-hf-username/MoAMetricLM-185M`
|
| 26 |
+
- **Task:** text generation (`text-generation`)
|
| 27 |
+
- **Library:** 🤗 Transformers
|
| 28 |
+
- **License:** Apache-2.0 (change here & add LICENSE file if different)
|
| 29 |
+
- **Datasets (examples used in dev runs):**
|
| 30 |
+
- `WeMake/Intelligent-Content-Understanding` (+ another 250k-token dataset)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
---
|
| 33 |
|
| 34 |
+
## Overview
|
| 35 |
|
| 36 |
+
**MoA** replaces standard dot-product attention with **metric-based attention** and blends multiple attentional biases using a **token-wise router** and **feature/router gates**.
|
| 37 |
|
| 38 |
+
**Heads per block**
|
| 39 |
+
- **LocalConvHead** — depthwise separable 1D conv (local context).
|
| 40 |
+
- **Metric Multi-Head Attention (MetricMHAttention)** — attention via negative distances in learned head subspaces (L2 / cosine / diagonal-Mahalanobis), with per-head **origin** \(o_h\) and **radius** \(r_h\) enabling **ball pruning**.
|
| 41 |
+
- **Metric MQA** — multi-query attention with shared K/V in the same metric space (efficiency).
|
| 42 |
+
- **ChannelMixHead** — per-token MLP for channel interactions.
|
| 43 |
|
| 44 |
+
**FFN**
|
| 45 |
+
- **HyperFFN** (multi-branch): SwiGLU MLP path, separable-conv path, and low-rank path, combined via a token-wise branch router and optional feature gates. LayerScale + DropPath for stability.
|
| 46 |
|
| 47 |
+
**Regularization (optional)**
|
| 48 |
+
- **Triangle-inequality (TI) penalty** on sampled triples to encourage true-metric behavior.
|
| 49 |
|
| 50 |
+
**Design goals:** geometric consistency, diverse inductive biases, structured efficiency, and full HF compatibility.
|
| 51 |
|
| 52 |
+
---
|
| 53 |
|
| 54 |
+
## What’s different from a standard Transformer?
|
| 55 |
|
| 56 |
+
- **Distance-based attention (softmin over distances)** instead of dot product:
|
| 57 |
+
\[
|
| 58 |
+
\text{attn}(i,j)\ \propto\ \exp\!\big(-\alpha_h\ \|q_i-k_j\|^2\big)
|
| 59 |
+
\]
|
| 60 |
+
with per-head sharpness \(\alpha_h\). Cosine / diag-Mahalanobis variants supported.
|
| 61 |
+
- **Per-head origins & radii** define balls for principled sparsity (**ball pruning**).
|
| 62 |
+
- **Mixture of attentions** (conv / metric MHA / metric MQA / channel MLP) blended by a **token-wise router**, with **feature gates** (FiLM-like) and **router-bias gates** for up/down control.
|
| 63 |
+
- **Up/Down projections** (SwiGLU-style) inside heads to expand/contract the value stream.
|
| 64 |
+
- **HyperFFN** provides non-lazy capacity with token-wise branch routing.
|
| 65 |
|
| 66 |
+
---
|
| 67 |
|
| 68 |
+
## Intended Use & Limitations
|
| 69 |
|
| 70 |
+
**Intended use:** research on geometry-aware attention, structured sparsity, and mixtures of attentional biases; small-scale experimentation and ablations.
|
| 71 |
|
| 72 |
+
**Limitations:**
|
| 73 |
+
- Dev runs used small token budgets; this is **not** a general-purpose LM.
|
| 74 |
+
- **No KV cache** yet → generation cost scales with context length.
|
| 75 |
+
- No alignment/safety tuning; outputs may be biased or inaccurate.
|
| 76 |
|
| 77 |
+
**Out-of-scope:** high-stakes applications (medical/legal/etc.) without further training, evaluation, and safeguards.
|
| 78 |
|
| 79 |
+
---
|
| 80 |
|
| 81 |
## Training Details
|
| 82 |
|
| 83 |
+
**Hardware:** CPU (Intel; no CUDA)
|
| 84 |
+
**Precision:** FP32
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
### Latest run (v0.2)
|
| 87 |
+
- **Tokens:** ~500,000 (two datasets, ~250k each)
|
| 88 |
+
- **Wall-time:** ~20 minutes (~**417 toks/s** overall)
|
| 89 |
+
- **Tokenizer:** GPT-2 (`gpt2`)
|
| 90 |
+
- **Learning rate:** **5e-4** (AdamW)
|
| 91 |
+
- **Batch / Seq:** batch_size=4, sequence length ≤512
|
| 92 |
+
- **Final train loss:** **≈ 0.30**
|
| 93 |
|
| 94 |
+
### Prior run (v0.1)
|
| 95 |
+
- **Tokens:** ~196k
|
| 96 |
+
- **Wall-time:** ~14 minutes
|
| 97 |
+
- **Final avg loss:** ≈ 0.417 (min batch ≈ 0.193)
|
| 98 |
|
| 99 |
+
**Stability aids:** safe softmax (subtract max), PreNorm, LayerScale (≈1e-4), DropPath (optional), label masking (`-100` on padding).
|
| 100 |
|
| 101 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
## Configuration (example)
|
| 104 |
+
|
| 105 |
+
```json
|
| 106 |
+
{
|
| 107 |
+
"model_type": "moa_metric",
|
| 108 |
+
"vocab_size": 50257,
|
| 109 |
+
"dim": 768,
|
| 110 |
+
"num_layers": 12,
|
| 111 |
+
"attn_heads": 8,
|
| 112 |
+
"mqa_q_heads": 8,
|
| 113 |
+
"mixer_hidden": 3072,
|
| 114 |
+
"ffn_hidden": 3072,
|
| 115 |
+
"metric": "l2", // "l2" | "cosine" | "maha_diag"
|
| 116 |
+
"alpha_init": 1.0,
|
| 117 |
+
"learn_alpha": true,
|
| 118 |
+
"use_balls": true,
|
| 119 |
+
"radius_init": 3.0,
|
| 120 |
+
"learn_radius": true,
|
| 121 |
+
"origin_init_scale": 0.0,
|
| 122 |
+
"maha_init": 1.0,
|
| 123 |
+
"ti_reg_weight": 0.0,
|
| 124 |
+
"ti_reg_samples": 0,
|
| 125 |
+
"router_hidden": 128,
|
| 126 |
+
"router_dropout": 0.1,
|
| 127 |
+
"router_temperature": 1.0,
|
| 128 |
+
"attn_drop": 0.1,
|
| 129 |
+
"proj_drop": 0.1,
|
| 130 |
+
"drop_path": 0.0,
|
| 131 |
+
"max_position_embeddings": 2048,
|
| 132 |
+
"pad_token_id": 50256,
|
| 133 |
+
"bos_token_id": 50256,
|
| 134 |
+
"eos_token_id": 50256
|
| 135 |
+
}
|
| 136 |
|
| 137 |
+
If you use gpt2 tokenizer, set pad_token = eos_token and ensure vocab_size/eos/pad match the tokenizer.
|
| 138 |
|
| 139 |
+
⸻
|
| 140 |
|
| 141 |
+
Usage
|
| 142 |
|
| 143 |
+
Inference
|
| 144 |
|
| 145 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 146 |
|
| 147 |
+
model_id = "your-hf-username/MoAMetricLM-185M"
|
| 148 |
+
tok = AutoTokenizer.from_pretrained(model_id)
|
| 149 |
+
if tok.pad_token is None:
|
| 150 |
+
tok.pad_token = tok.eos_token
|
| 151 |
|
| 152 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 153 |
|
| 154 |
+
prompt = "Explain metric-based attention in simple terms:"
|
| 155 |
+
inputs = tok(prompt, return_tensors="pt")
|
| 156 |
+
gen = model.generate(**inputs, max_new_tokens=128, do_sample=False)
|
| 157 |
+
print(tok.decode(gen[0], skip_special_tokens=True))
|
| 158 |
|
| 159 |
+
KV cache: not yet implemented; generation recomputes full context at each step.
|
| 160 |
|
| 161 |
+
Training (custom loop sketch)
|
| 162 |
|
| 163 |
+
from transformers import AutoTokenizer, DataCollatorForLanguageModeling
|
| 164 |
+
from torch.utils.data import DataLoader
|
| 165 |
+
import torch, torch.nn.functional as F
|
| 166 |
|
| 167 |
+
tok = AutoTokenizer.from_pretrained("gpt2")
|
| 168 |
+
if tok.pad_token is None:
|
| 169 |
+
tok.pad_token = tok.eos_token
|
| 170 |
|
| 171 |
+
def collate(examples):
|
| 172 |
+
batch = tok([e["text"] for e in examples], padding="max_length",
|
| 173 |
+
truncation=True, max_length=512, return_tensors="pt")
|
| 174 |
+
labels = batch["input_ids"].clone()
|
| 175 |
+
labels[batch["attention_mask"] == 0] = -100
|
| 176 |
+
batch["labels"] = labels
|
| 177 |
+
return batch
|
| 178 |
|
| 179 |
+
# dataset = ... (load HF dataset with a 'text' field)
|
| 180 |
+
# loader = DataLoader(dataset, batch_size=4, shuffle=True, collate_fn=collate)
|
| 181 |
+
# model = AutoModelForCausalLM.from_pretrained(model_id) # or initialize config & model
|
| 182 |
|
| 183 |
+
# optimizer = torch.optim.AdamW(model.parameters(), lr=5e-4, betas=(0.9,0.95), weight_decay=0.01)
|
| 184 |
+
# for batch in loader:
|
| 185 |
+
# out = model(**batch)
|
| 186 |
+
# out.loss.backward()
|
| 187 |
+
# torch.nn.utils.clip_grad_norm_(model.parameters(), 1.2)
|
| 188 |
+
# optimizer.step(); optimizer.zero_grad()
|
| 189 |
|
|
|
|
| 190 |
|
| 191 |
+
⸻
|
| 192 |
|
| 193 |
+
Evaluation
|
| 194 |
|
| 195 |
+
For meaningful comparisons, run:
|
| 196 |
+
• Validation perplexity on a held-out split.
|
| 197 |
+
• Ablations at fixed token budgets:
|
| 198 |
+
• L2 vs cosine vs diag-Mahalanobis
|
| 199 |
+
• With vs without ball pruning
|
| 200 |
+
• With vs without HyperFFN branch router/gates
|
| 201 |
+
• With vs without TI regularizer
|
| 202 |
|
| 203 |
+
Please share results via Issues/PRs.
|
| 204 |
|
| 205 |
+
⸻
|
| 206 |
|
| 207 |
+
Efficiency Notes
|
| 208 |
+
• Ball pruning: masks keys outside per-head radius → structured sparsity.
|
| 209 |
+
• MQA: shared K/V reduce projection cost while retaining diversity via multi-query heads.
|
| 210 |
+
• HyperFFN: token-wise branch router (optional top-k) to avoid paying for all branches equally.
|
| 211 |
+
• CPU tips: set OMP_NUM_THREADS/MKL_NUM_THREADS to core count; use pad_token = eos_token.
|
| 212 |
|
| 213 |
+
Roadmap: metric-aware KV cache for long contexts; kernelized distance approximations (e.g., RFF) for sub-quadratic regimes; quantization & mixed precision exploration.
|
| 214 |
|
| 215 |
+
⸻
|
| 216 |
|
| 217 |
+
Safety, Bias & Risks
|
| 218 |
+
• May produce biased, offensive, or factually incorrect outputs.
|
| 219 |
+
• No safety/alignment training included.
|
| 220 |
+
• Do not deploy in high-stakes contexts without additional safeguards.
|
| 221 |
|
| 222 |
+
⸻
|
| 223 |
|
| 224 |
+
License
|
| 225 |
|
| 226 |
+
Apache-2.0 (update if different).
|
| 227 |
|
| 228 |
+
⸻
|
| 229 |
|
| 230 |
+
Citation
|
| 231 |
|
| 232 |
+
@misc{moametriclm185m,
|
| 233 |
+
title = {MoAMetricLM-185M: A Geometry-Aware Mixture-of-Attentions Language Model},
|
| 234 |
+
author = {Colca, Roy Shawn and collaborators},
|
| 235 |
+
year = {2025},
|
| 236 |
+
url = {https://huggingface.co/your-hf-username/MoAMetricLM-185M}
|
| 237 |
+
}
|
| 238 |
|
|
|
|
| 239 |
|
| 240 |
+
⸻
|
| 241 |
|
| 242 |
+
Changelog
|
| 243 |
+
• v0.2 (2025-09-20) — 500k-token CPU run, GPT-2 tokenizer, LR=5e-4, final loss ≈ 0.30.
|
| 244 |
+
• v0.1 (2025-09-20) — initial public release: metric heads, MQA, ball pruning, HyperFFN, router & gates; HF-compatible; no KV cache.
|
| 245 |
|
| 246 |
+
⸻
|
| 247 |
|
| 248 |
+
Maintainers
|
| 249 |
+
• Author: reaper (Convergent Intelligence LLC)
|
| 250 |
+
• Contact: add preferred contact
|
| 251 |
+
• Issues: HF model hub issues tab
|
| 252 |
|
|
|