Nano-prime Claude Opus 4.5 commited on
Commit
a76994c
·
1 Parent(s): 5a4c75f

Add Complexity model - Llama with Token-Routed MLP

Browse files

- Token-Routed MLP: Routes by token ID, not hidden states
- QK Normalization: Stabilizes attention at scale
- Flash Attention via SDPA
- 100M params, 12 layers, 4 experts

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

README.md CHANGED
@@ -1,3 +1,120 @@
1
  ---
2
  license: cc-by-nc-4.0
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: cc-by-nc-4.0
3
+ language:
4
+ - en
5
+ - fr
6
+ - code
7
+ tags:
8
+ - complexity
9
+ - token-routed-mlp
10
+ - flash-attention
11
+ - causal-lm
12
+ library_name: transformers
13
+ pipeline_tag: text-generation
14
  ---
15
+
16
+ # Complexity Base
17
+
18
+ A Llama-style transformer with architectural improvements for efficiency and performance.
19
+
20
+ ## Architecture: Llama + Improvements
21
+
22
+ Complexity builds on the Llama architecture with three key enhancements:
23
+
24
+ | Component | Llama | Complexity |
25
+ |-----------|-------|------------|
26
+ | **MLP** | Dense FFN | **Token-Routed MLP** (4 experts, 1 active) |
27
+ | **Attention** | Standard | **Flash Attention** via SDPA |
28
+ | **Normalization** | RMSNorm only | RMSNorm + **QK Normalization** |
29
+
30
+ ### Token-Routed MLP
31
+
32
+ Unlike MoE which routes based on hidden states, Token-Routed MLP routes based on **token ID**:
33
+
34
+ ```python
35
+ expert_idx = token_id % num_experts # Deterministic routing
36
+ output = experts[expert_idx](hidden_states)
37
+ ```
38
+
39
+ **Benefits:**
40
+ - No router network overhead
41
+ - Deterministic, reproducible routing
42
+ - 4x parameter efficiency (only 1/4 experts active)
43
+
44
+ ### QK Normalization
45
+
46
+ Stabilizes attention at scale by normalizing Q and K before computing attention scores:
47
+
48
+ ```python
49
+ q = self.q_norm(q)
50
+ k = self.k_norm(k)
51
+ attn = (q @ k.T) / sqrt(d)
52
+ ```
53
+
54
+ ## Model Details
55
+
56
+ - **Parameters**: ~100M
57
+ - **Hidden size**: 768
58
+ - **Layers**: 12
59
+ - **Attention heads**: 12 (KV heads: 4)
60
+ - **Experts**: 4 (1 active per token)
61
+ - **Vocabulary**: 100K tokens
62
+ - **Context**: 2048 tokens
63
+
64
+ ## Installation
65
+
66
+ ```bash
67
+ pip install complexity-model pyllm-inference
68
+ ```
69
+
70
+ ## Usage
71
+
72
+ ### With PyLLM
73
+
74
+ ```bash
75
+ pyllm serve Pacific-Prime/complexity
76
+ ```
77
+
78
+ ### Python API
79
+
80
+ ```python
81
+ from transformers import AutoTokenizer, AutoModelForCausalLM
82
+
83
+ tokenizer = AutoTokenizer.from_pretrained("Pacific-Prime/complexity")
84
+ model = AutoModelForCausalLM.from_pretrained(
85
+ "Pacific-Prime/complexity",
86
+ trust_remote_code=True
87
+ )
88
+
89
+ inputs = tokenizer("def fibonacci(n):", return_tensors="pt")
90
+ outputs = model.generate(**inputs, max_new_tokens=100)
91
+ print(tokenizer.decode(outputs[0]))
92
+ ```
93
+
94
+ ## Comparison with Llama
95
+
96
+ ```
97
+ Llama: embed -> [Attn + FFN] x L -> output
98
+ Complexity: embed -> [Attn + TokenRoutedMLP] x L -> output
99
+ ↑ QK Norm ↑ 4 experts (1 active)
100
+ ```
101
+
102
+ Same parameter count, but:
103
+ - **4x more total MLP parameters** (distributed across experts)
104
+ - **Faster training** (QK norm stabilizes gradients)
105
+ - **Better scaling** (sparse activation)
106
+
107
+ ## License
108
+
109
+ Apache 2.0
110
+
111
+ ## Citation
112
+
113
+ ```bibtex
114
+ @misc{complexity,
115
+ title={Complexity: Token-Routed MLP Transformer},
116
+ author={Pacific Prime},
117
+ year={2025},
118
+ url={https://huggingface.co/Pacific-Prime/complexity}
119
+ }
120
+ ```
config.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ComplexityForCausalLM"
4
+ ],
5
+ "model_type": "complexity",
6
+ "vocab_size": 100000,
7
+ "hidden_size": 768,
8
+ "intermediate_size": 2048,
9
+ "num_hidden_layers": 12,
10
+ "num_attention_heads": 12,
11
+ "num_key_value_heads": 4,
12
+ "max_position_embeddings": 2048,
13
+ "rms_norm_eps": 1e-06,
14
+ "rope_theta": 10000.0,
15
+ "tie_word_embeddings": true,
16
+ "torch_dtype": "float16",
17
+ "transformers_version": "4.36.0",
18
+ "use_token_routed_mlp": true,
19
+ "num_experts": 4,
20
+ "use_qk_norm": true,
21
+ "use_sdpa": true,
22
+ "sliding_window": null
23
+ }
generation_config.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 2,
4
+ "eos_token_id": 0,
5
+ "pad_token_id": 1,
6
+ "max_length": 2048,
7
+ "do_sample": true,
8
+ "temperature": 0.7,
9
+ "top_p": 0.9,
10
+ "top_k": 50,
11
+ "repetition_penalty": 1.1
12
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:416e3e3ed86dde9f1d7460d81e73f10b43604a965654cabbfc30da6037e79b9c
3
+ size 467867760
special_tokens_map.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<|startoftext|>",
3
+ "eos_token": "<|endoftext|>",
4
+ "pad_token": "<|pad|>"
5
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<|endoftext|>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<|pad|>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "<|startoftext|>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ }
27
+ },
28
+ "bos_token": "<|startoftext|>",
29
+ "clean_up_tokenization_spaces": false,
30
+ "eos_token": "<|endoftext|>",
31
+ "extra_special_tokens": {},
32
+ "model_max_length": 1000000000000000019884624838656,
33
+ "pad_token": "<|pad|>",
34
+ "tokenizer_class": "PreTrainedTokenizerFast",
35
+ "unk_token": null
36
+ }