vigneshwar234 commited on
Commit
2c83537
·
verified ·
1 Parent(s): dc8b3dd

Update model card: TMT v3 comprehensive documentation

Browse files
Files changed (1) hide show
  1. README.md +132 -364
README.md CHANGED
@@ -1,418 +1,186 @@
1
  ---
2
- language:
3
- - en
4
  license: mit
5
- library_name: pytorch
6
  tags:
7
- - text-generation
8
- - pytorch
9
- - transformers
10
- - graph-neural-network
11
- - research
12
- - novel-architecture
13
- - efficient-transformer
14
- - sparse-attention
15
- - adaptive-computation
16
- - dynamic-graph
17
- - early-exit
18
- - temporal-decay
19
- - mesh-attention
20
- - language-model
21
- - causal-lm
22
- - preprint
23
- - arxiv
24
- - wikitext
25
- - nlp
26
- - attention
27
  datasets:
28
- - wikitext
29
- - roneneldan/TinyStories
30
- - vigneshwar234/TMT-Benchmarks
31
  metrics:
32
- - perplexity
33
- pipeline_tag: text-generation
34
  model-index:
35
- - name: TemporalMesh-Transformer
36
- results:
37
- - task:
38
- type: text-generation
39
- name: Text Generation
40
- dataset:
41
- name: WikiText-2
42
- type: wikitext
43
- metrics:
44
- - type: perplexity
45
- value: 29.4
46
- name: Perplexity — Full TMT (120M params)
47
- - task:
48
- type: text-generation
49
- name: Text Generation (Ablation)
50
- dataset:
51
- name: WikiText-2
52
- type: wikitext
53
- metrics:
54
- - type: perplexity
55
- value: 42.1
56
- name: Perplexity — Vanilla Transformer Baseline
57
- - type: perplexity
58
- value: 37.8
59
- name: Perplexity — Mesh Attention Only
60
- - type: perplexity
61
- value: 39.6
62
- name: Perplexity — Adaptive Exit Only
63
- - type: perplexity
64
- value: 29.4
65
- name: Perplexity — Full TMT (all 3 innovations)
66
  ---
67
 
68
- # 🚀 TemporalMesh Transformer (TMT)
69
 
70
- [![Paper](https://img.shields.io/badge/📄_Paper-Zenodo_Preprint-blue?style=for-the-badge)](https://zenodo.org/records/20287390)
71
- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.20287197.svg)](https://doi.org/10.5281/zenodo.20287197)
72
- [![GitHub](https://img.shields.io/badge/💻_GitHub-TemporalMesh-black?style=for-the-badge)](https://github.com/vignesh2027/TemporalMesh-Transformer)
73
- [![Demo](https://img.shields.io/badge/🚀_Live_Demo-HF_Space-green?style=for-the-badge)](https://huggingface.co/spaces/vigneshwar234/TemporalMesh-Transformer-Demo)
74
- [![License](https://img.shields.io/badge/License-MIT-green?style=flat-square)](https://github.com/vignesh2027/TemporalMesh-Transformer/blob/main/LICENSE)
75
- [![Tests](https://img.shields.io/badge/Tests-201_passing-brightgreen?style=flat-square)](https://github.com/vignesh2027/TemporalMesh-Transformer/actions)
76
- [![Python](https://img.shields.io/badge/Python-3.10%2B-blue?style=flat-square)](https://python.org)
77
- [![PyTorch](https://img.shields.io/badge/PyTorch-2.2%2B-orange?style=flat-square)](https://pytorch.org)
78
-
79
- > 📄 **[TemporalMesh Transformer: Dynamic Graph Attention with Temporal Decay and Adaptive Depth Routing](https://zenodo.org/records/20287390)**
80
- > **Author:** Vigneshwar LK · **DOI:** [10.5281/zenodo.20287197](https://doi.org/10.5281/zenodo.20287197) · **Published:** May 2026 · **Preprint (Open Access)**
81
-
82
- ---
83
-
84
- ## 🔥 Key Results
85
-
86
- **Full TMT achieves 30.2% lower perplexity than vanilla transformer while using only 48% of the compute — a 2.1× efficiency gain.**
87
-
88
- | Configuration | Mesh | Decay | Exit | Val PPL ↓ | Avg Layers | Rel Compute ↓ | Params |
89
- |---|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
90
- | Vanilla Transformer | ✗ | ✗ | ✗ | 42.1 | 12.0 | 1.00× | 120M |
91
- | Mesh Attention Only | ✓ | ✗ | ✗ | 37.8 | 12.0 | 0.62× | 120M |
92
- | Temporal Decay Only | ✗ | ✓ | ✗ | 40.3 | 12.0 | 0.98× | 120M |
93
- | Adaptive Exit Only | ✗ | ✗ | ✓ | 39.6 | 5.8 | 0.51× | 120M |
94
- | Mesh + Decay | ✓ | ✓ | ✗ | 34.2 | 12.0 | 0.61× | 120M |
95
- | Mesh + Exit | ✓ | ✗ | ✓ | 35.1 | 5.7 | 0.50× | 120M |
96
- | Decay + Exit | ✗ | ✓ | ✓ | 37.0 | 5.9 | 0.50× | 120M |
97
- | **Full TMT (all 3)** | ✓ | ✓ | ✓ | **29.4** | **5.5** | **0.48×** | 120M |
98
-
99
- > **Full TMT achieves PPL 29.4 vs Vanilla 42.1 — a 30.2% perplexity reduction while using only 48% of the compute (2.1× efficiency gain).**
100
-
101
- The results show clear synergy: each innovation provides independent benefit, dual combinations improve further, and combining all three yields disproportionate gains that exceed what ablations predict from linear composition.
102
 
103
  ---
104
 
105
- ## What Makes TMT Different
106
 
107
- Every transformer since Vaswani et al. (2017) makes the same three assumptions:
108
 
109
- 1. **Static topology** every token attends to every other token (O(S²) complexity)
110
- 2. **Fixed depth** — every token passes through every layer regardless of difficulty
111
- 3. **Time-agnostic position** positional encoding treats all positions equivalently without semantic distance weighting
112
-
113
- TMT breaks all three simultaneously in a single unified forward pass.
114
-
115
- | Property | GPT / BERT | Graph Transformer | Early Exit | MoE | **TMT** |
116
- |---|:---:|:---:|:---:|:---:|:---:|
117
- | Dynamic graph topology | ✗ | Partial | ✗ | ✗ | **✓** |
118
- | Per-token adaptive depth | ✗ | ✗ | ✓ | ✗ | **✓** |
119
- | Semantic temporal decay | ✗ | ✗ | ✗ | ✗ | **✓** |
120
- | Persistent memory anchors | ✗ | ✗ | ✗ | ✗ | **✓** |
121
- | Dual-stream FFN | ✗ | ✗ | ✗ | Partial | **✓** |
122
- | Joint training of all innovations | — | — | — | — | **✓** |
123
-
124
- No prior architecture combines all five of these properties. TMT is the first.
125
 
126
  ---
127
 
128
- ## 🔬 The Three Innovations
129
-
130
- ### Innovation 1 — Mesh Attention (Dynamic Graph Topology)
131
-
132
- **Problem:** Standard self-attention computes O(S²) attention scores regardless of semantic relevance. Most attended pairs carry negligible information weight.
133
-
134
- **Solution:** Before each layer, TMT recomputes the full token graph using cosine similarity of current representations, then retains only the top-k nearest neighbours per token. This gives an O(S·k) sparse graph that concentrates attention capacity where it matters most.
135
-
136
- **Formal definition:**
137
 
138
- ```
139
- sim(i, j) = x_i · x_j / (‖x_i‖ · ‖x_j‖)
140
-
141
- Neighbours(i) = top-k_{j i} sim(i, j)
142
-
143
- edge_weight(i, j) = sim(i, j) for j Neighbours(i), else 0
144
- ```
145
-
146
- **Pseudocode:**
147
-
148
- ```python
149
- # MeshBuilder.forward — runs once per layer
150
- x_norm = F.normalize(x_flat, p=2, dim=-1) # (B*S, D)
151
- sim = x_norm @ x_norm.T # (B*S, B*S)
152
- sim.fill_diagonal_(float('-inf')) # no self-loops
153
- topk_vals, topk_idx = sim.topk(k, dim=-1) # (B*S, k)
154
- edge_index = build_coo(topk_idx) # (2, B*S*k)
155
- edge_weight = topk_vals.flatten() # (B*S*k,)
156
- ```
157
 
158
- **Key insight:** The graph is rebuilt every layer using updated representations. A token that was semantically distant in layer 0 may become a critical neighbour in layer 6 once context is established. Static graphs (as in prior graph transformers) cannot capture this dynamic topology evolution.
159
-
160
- **Result:** Mesh attention alone reduces compute to 0.62× while dropping PPL from 42.1 to 37.8 — an 11.2% improvement.
161
 
162
  ---
163
 
164
- ### Innovation 2 Temporal Semantic Decay
165
-
166
- **Problem:** Standard positional encodings (sinusoidal, RoPE, ALiBi) encode position but not semantic distance. A token at position 500 is not necessarily semantically "farther" from position 0 than position 5 is — but the model has no mechanism to express this distinction prior to attention.
167
-
168
- **Solution:** TMT learns per-dimension decay weights applied to token embeddings before attention. Tokens at later positions are attenuated by a learned sigmoid function of their normalized position. This allows the model to express "this semantic content fades with distance" without any recurrence.
169
-
170
- **Formal definition:**
171
 
172
  ```
173
- t_s = s / (S - 1) # normalized position ∈ [0, 1]
174
- decay(s, d) = σ(−t_s · w_decay[d]) # per-dim sigmoid decay
175
- x̃[s, d] = x[s, d] · decay(s, d)
 
 
 
 
 
 
176
  ```
177
 
178
- Where `w_decay ∈ ℝ^D` is a learned parameter vector initialized to `decay_rate` (default 0.1).
179
-
180
- **Key insight:** The decay is per-dimension, not a single scalar. This lets the model learn which embedding dimensions should decay quickly (syntactic surface features) vs slowly (deep semantic content). The decay scalars are carried through all subsequent layers so downstream components can condition on temporal distance.
181
-
182
- **Result:** Temporal decay alone yields PPL 40.3 vs vanilla 42.1 modest alone, but provides critical signal when combined with mesh attention (Mesh+Decay: 34.2 PPL, an 18.8% improvement over vanilla).
 
 
183
 
184
  ---
185
 
186
- ### Innovation 3 — Adaptive Depth Routing (Early Exit)
187
-
188
- **Problem:** All transformer tokens process through all N layers identically. Common words ("the", "a", punctuation) require far less computation than rare entities, complex reasoning steps, or ambiguous references. Uniform depth wastes compute on easy tokens.
189
-
190
- **Solution:** After each layer, a single linear gate projects each token's representation to a confidence scalar. Tokens exceeding the threshold have their representations frozen and skip remaining layers entirely. The gate is trained with an auxiliary loss that encourages decisiveness.
191
-
192
- **Formal definition:**
193
-
194
- ```
195
- confidence(s) = σ(W_gate · h_s + b_gate) # scalar per token
196
- exit(s) = confidence(s) > threshold
197
-
198
- if exit(s):
199
- h_s stays unchanged for all subsequent layers
200
- ```
201
-
202
- **Auxiliary loss:**
203
 
 
 
 
 
204
  ```
205
- L_gate = −E[|confidence − 0.5|] # reward decisiveness
206
- L_total = L_CE + 0.1 · L_gate
207
- ```
208
-
209
- The coefficient 0.1 keeps the auxiliary loss from dominating the language modelling objective while still driving the gate to be decisive (push toward 0 or 1, not linger at 0.5).
210
-
211
- **Pseudocode:**
212
 
213
  ```python
214
- exit_mask = torch.zeros(B, S, dtype=torch.bool)
215
- for layer in self.layers:
216
- x_frozen = x.clone()
217
- x = layer.attn(x) + layer.ffn(x) # standard compute
218
- confidence = sigmoid(layer.gate_proj(x)) # (B, S)
219
- newly_exited = (~exit_mask) & (confidence > threshold)
220
- exit_mask = exit_mask | newly_exited
221
- # Freeze exited tokens — carry representation unchanged
222
- x = torch.where(exit_mask.unsqueeze(-1), x_frozen, x)
223
- ```
224
-
225
- **Key insight:** The exit decision is fully learned from data, not hand-crafted per token type. On WikiText-2, the model learns to exit common tokens early and keep complex tokens active through all 12 layers. Average exit layer drops to 5.5/12 — saving 54% of layer computations.
226
-
227
- **Result:** Adaptive exit alone gives PPL 39.6 with avg 5.8 layers (0.51× compute). Combined with all three innovations, achieves 5.5 avg layers (0.48× compute) and PPL 29.4.
228
-
229
- ---
230
-
231
- ## 🏗️ Architecture
232
-
 
 
 
233
  ```
234
- Input token IDs (B, S)
235
-
236
-
237
- ┌───────────────────────────────────┐
238
- │ Token Embedding │ (B, S) → (B, S, D)
239
- │ + Temporal Position Encoder │ RoPE + learned decay scalars
240
- │ → decay_scalars (B, S, D) │ per-dim sigmoid decay
241
- └───────────────────────────────────┘
242
-
243
-
244
- ┌───────────────────────────────────┐
245
- │ Mesh Builder │ Dynamic kNN graph
246
- │ x_flat (B*S, D) → edge_index │ O(S·k) edges per batch item
247
- │ + edge_weight │ Cosine similarity weights
248
- └───────────────────────────────────┘
249
-
250
- ▼ (repeated N times — graph rebuilt each iteration)
251
- ┌───────────────────────────────────┐
252
- │ TMT Layer i │
253
- │ │
254
- │ ┌─────────────────────────────┐ │
255
- │ │ LayerNorm → Mesh Attention │ │ Sparse graph attention
256
- │ │ + decay_scalars weighting │ │ (B, S, D) → (B, S, D)
257
- │ └─────────────────────────────┘ │
258
- │ ↓ + residual │
259
- │ ┌─────────────────────────────┐ │
260
- │ │ LayerNorm → Dual Stream │ │ Two parallel FFN streams
261
- │ │ FFN │ │ merged by gated fusion
262
- │ └─────────────────────────────┘ │
263
- │ ↓ + residual │
264
- │ ┌─────────────────────────────┐ │
265
- │ │ Exit Gate │ │ confidence = σ(W·h)
266
- │ │ if conf > threshold: │ │ Freeze token, skip future layers
267
- │ │ freeze token │ │ exit_mask updated monotonically
268
- │ └─────────────────────────────┘ │
269
- │ ↓ │
270
- │ ┌─────────────────────────────┐ │
271
- │ │ LayerNorm → Memory Anchor │ │ Cross-attn to M persistent
272
- │ │ Cross-Attention │ │ key-value memory vectors
273
- │ └─────────────────────────────┘ │
274
- │ ↓ + residual │
275
- │ Rebuild mesh graph │ Updated for next layer
276
- └───────────────────────────────────┘
277
-
278
-
279
- ┌───────────────────────────────────┐
280
- │ Final LayerNorm → Output Proj │ (B, S, D) → (B, S, V)
281
- │ (weight-tied with embedding) │ Parameter-efficient
282
- └───────────────────────────────────┘
283
-
284
-
285
- TMTOutput dataclass
286
- ├── logits (B, S, V) — next-token logits
287
- ├── exit_masks [N × (B, S)] — per-layer bool exit decisions
288
- ├── confidences [N × (B, S)] — gate confidence ∈ [0, 1]
289
- ├── graph_edges (2, E), (E,) — final dynamic graph
290
- ├── memory_state (M, D) — persistent memory anchors
291
- └── decay_scalars (B, S, D) — temporal decay weights
292
- ```
293
-
294
- ---
295
-
296
- ## 📈 Ablation Study Results
297
-
298
- Complete ablation from the [TMT-Benchmarks](https://huggingface.co/datasets/vigneshwar234/TMT-Benchmarks) dataset (`ablation_reference` split). All runs use identical training setup, same random seed, same 120M parameter budget.
299
-
300
- | # | Configuration | Mesh | Decay | Exit | Val PPL ↓ | Avg Layers | Rel Compute | Params |
301
- |---|---|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
302
- | 1 | Vanilla Transformer | ✗ | ✗ | ✗ | 42.1 | 12.0 | 1.00× | 120M |
303
- | 2 | Mesh Attention Only | ✓ | ✗ | ✗ | 37.8 | 12.0 | 0.62× | 120M |
304
- | 3 | Temporal Decay Only | ✗ | ✓ | ✗ | 40.3 | 12.0 | 0.98× | 120M |
305
- | 4 | Adaptive Exit Only | ✗ | ✗ | ✓ | 39.6 | 5.8 | 0.51× | 120M |
306
- | 5 | Mesh + Decay | ✓ | ✓ | ✗ | 34.2 | 12.0 | 0.61× | 120M |
307
- | 6 | Mesh + Exit | ✓ | ✗ | ✓ | 35.1 | 5.7 | 0.50× | 120M |
308
- | 7 | Decay + Exit | ✗ | ✓ | ✓ | 37.0 | 5.9 | 0.50× | 120M |
309
- | **8** | **Full TMT (all 3)** | **✓** | **✓** | **✓** | **29.4** | **5.5** | **0.48×** | **120M** |
310
-
311
- **Key findings:**
312
- - Every single innovation beats vanilla (rows 2–4 all improve over row 1)
313
- - Dual combinations are super-additive — synergy appears even at two innovations
314
- - Full TMT achieves **30.2% PPL reduction** with only **48% of compute** vs vanilla
315
- - Average token exits at layer 5.5 out of 12 — more than half of layers are skipped
316
- - The 2.1× efficiency gain makes TMT suitable for inference-critical deployments
317
-
318
- ---
319
-
320
- ## 🔗 Related Resources
321
-
322
- | Resource | Link |
323
- |---|---|
324
- | 📄 Paper (Zenodo Preprint) | https://zenodo.org/records/20287390 |
325
- | 🔖 DOI | https://doi.org/10.5281/zenodo.20287197 |
326
- | 💻 GitHub Repository | https://github.com/vignesh2027/TemporalMesh-Transformer |
327
- | 🤗 This Model (HuggingFace) | https://huggingface.co/vigneshwar234/TemporalMesh-Transformer |
328
- | 📊 Benchmark Dataset | https://huggingface.co/datasets/vigneshwar234/TMT-Benchmarks |
329
- | 🚀 Live Demo Space | https://huggingface.co/spaces/vigneshwar234/TemporalMesh-Transformer-Demo |
330
- | 🌐 Docs (GitHub Pages) | https://vignesh2027.github.io/TemporalMesh-Transformer/ |
331
 
332
  ---
333
 
334
- ## 📦 Repository Structure
335
-
336
- ```
337
- TemporalMesh-Transformer/
338
- ├── tmt/
339
- │ ├── model/
340
- │ │ ├── config.py # TMTConfig dataclass — all hyperparameters
341
- │ │ ├── model.py # TMTModel — full forward pass + TMTOutput
342
- │ │ ├── mesh.py # MeshBuilder — dynamic kNN graph construction
343
- │ │ ├── attention.py # MeshAttention — sparse graph attention
344
- │ │ ├── embedding.py # TokenEmbedding + TemporalPositionEncoder
345
- │ │ ├── exit_gate.py # ExitGate — per-token adaptive depth routing
346
- │ │ ├── ffn.py # DualStreamFFN — dual parallel feed-forward
347
- │ │ ├── layers.py # TMTLayer — full single layer assembly
348
- │ │ └── memory.py # MemoryAnchorCross — persistent KV memory
349
- │ ├── training/
350
- │ │ ├── loss.py # compute_loss — CE + auxiliary gate loss
351
- │ │ ├── trainer.py # training loop utilities
352
- │ │ └── scheduler.py # learning rate scheduling
353
- │ ├── data/
354
- │ │ ├── dataset.py # WikiText-2 / TinyStories loading
355
- │ │ └── tokenizer.py # tokenizer utilities
356
- │ └── experiments/
357
- │ ├── 01_baseline.ipynb # Vanilla transformer baseline
358
- │ ├── 02_mesh_only.ipynb # Mesh-only ablation
359
- │ ├── 03_full_tmt.ipynb # Full TMT training
360
- │ └── 04_compare.ipynb # Side-by-side comparison
361
- ├── tests/
362
- │ ├── test_benchmarks.py # 25 benchmark validation tests (NEW)
363
- │ ├── test_forward.py # End-to-end forward pass tests
364
- │ ├── test_shapes.py # Tensor shape contracts
365
- │ ├── test_config.py # Config validation
366
- │ ├── test_training.py # Training loop tests
367
- │ ├── test_integration.py # Integration tests
368
- │ ├── test_edge_cases.py # Edge and boundary conditions
369
- │ ├── test_generation.py # Text generation tests
370
- │ ├── test_dataset.py # Dataset loading tests
371
- │ └── test_reprs.py # __repr__ and string tests
372
- ├── paper/
373
- │ └── TemporalMesh_Transformer_2026.pdf
374
- ├── docs/
375
- │ └── index.html # GitHub Pages docs
376
- ├── pyproject.toml
377
- ├── requirements.txt
378
- └── README.md
379
  ```
380
 
381
  ---
382
 
383
- ## 💻 Hardware Requirements
384
 
385
- | Task | Minimum | Recommended |
386
- |---|---|---|
387
- | Install + run all 201+ tests | Any CPU, 4GB RAM | — |
388
- | Small config training (d=128, L=4) | CPU, 8GB RAM | CPU, 16GB RAM |
389
- | Full config training (d=512, L=12, 120M) | GPU 8GB VRAM | GPU 24GB VRAM (A100/H100/4090) |
390
- | Inference (batch=1, seq=1024) | CPU | GPU 8GB |
391
- | WikiText-2 full training run | GPU 16GB | GPU with DDP |
 
 
392
 
393
- The architecture is fully CPU-runnable for research and testing. All 201+ tests pass on CPU without any GPU required.
394
 
395
  ---
396
 
397
- ## 📖 Citation
398
-
399
- If you use TMT in your research, please cite:
400
 
401
  ```bibtex
402
- @article{vigneshwar2026temporalmesh,
403
- title = {TemporalMesh Transformer: Dynamic Graph Attention with Temporal Decay and Adaptive Depth Routing},
404
- author = {LK, Vigneshwar},
405
- journal = {Zenodo Preprint},
406
- year = {2026},
407
- doi = {10.5281/zenodo.20287197},
408
- url = {https://zenodo.org/records/20287390}
409
  }
410
  ```
411
 
412
  ---
413
 
414
- ## 📜 License
415
-
416
- MIT License — see [LICENSE](https://github.com/vignesh2027/TemporalMesh-Transformer/blob/main/LICENSE) for details.
417
 
418
- Copyright (c) 2026 Vigneshwar LK
 
1
  ---
2
+ language: en
 
3
  license: mit
 
4
  tags:
5
+ - transformers
6
+ - language-model
7
+ - graph-attention
8
+ - adaptive-depth
9
+ - temporal-decay
10
+ - efficient-llm
11
+ - pytorch
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  datasets:
13
+ - wikitext
14
+ - c4
 
15
  metrics:
16
+ - perplexity
 
17
  model-index:
18
+ - name: TemporalMesh-Transformer
19
+ results:
20
+ - task:
21
+ type: text-generation
22
+ dataset:
23
+ name: WikiText-2
24
+ type: wikitext
25
+ metrics:
26
+ - type: perplexity
27
+ value: 29.4
28
+ name: Validation Perplexity
29
+ - task:
30
+ type: text-generation
31
+ dataset:
32
+ name: WikiText-103
33
+ type: wikitext
34
+ metrics:
35
+ - type: perplexity
36
+ value: 36.1
37
+ name: Validation Perplexity
 
 
 
 
 
 
 
 
 
 
 
38
  ---
39
 
40
+ # TemporalMesh Transformer (TMT v3)
41
 
42
+ **Author:** Vigneshwar LK
43
+ **Paper:** [DOI 10.5281/zenodo.20287197](https://zenodo.org/records/20287390)
44
+ **Code:** [github.com/vignesh2027/TemporalMesh-Transformer](https://github.com/vignesh2027/TemporalMesh-Transformer)
45
+ **Live Demo:** [HuggingFace Space](https://huggingface.co/spaces/vigneshwar234/TemporalMesh-Transformer-Demo)
46
+ **Benchmarks:** [TMT-Benchmarks Dataset](https://huggingface.co/datasets/vigneshwar234/TMT-Benchmarks)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  ---
49
 
50
+ ## What is TMT?
51
 
52
+ TMT is a novel PyTorch transformer architecture that simultaneously resolves three fundamental inefficiencies in standard transformers:
53
 
54
+ | Problem | Standard Transformer | TMT Solution |
55
+ |---|---|---|
56
+ | Quadratic attention cost | $O(S^2)$ per layer | **Mesh Attention**: $O(S \cdot k)$ dynamic $k$NN graph |
57
+ | Static attention topology | Fixed fully-connected | **Dynamic graph** rebuilt per-layer from cosine similarity |
58
+ | Uniform token compute | All tokens use all $N$ layers | **Adaptive Depth Routing**: exit gate per token, avg 5.8/12 layers |
59
+ | Flat positional encoding | Position only | **Temporal Decay**: learned multiplicative semantic attenuation |
60
+ | No cross-sequence memory | Stateless | **EMA Memory Anchors**: 16 persistent fast-weight vectors |
 
 
 
 
 
 
 
 
 
61
 
62
  ---
63
 
64
+ ## Results
 
 
 
 
 
 
 
 
65
 
66
+ | Model | WikiText-2 PPL ↓ | WikiText-103 PPL ↓ | LongBench ↑ | Compute |
67
+ |---|---|---|---|---|
68
+ | Vanilla Transformer | 42.1 | 51.3 | 41.2 | 100% |
69
+ | Longformer | 39.6 | 47.2 | 49.8 | 62% |
70
+ | Mamba | 31.8 | 38.4 | 51.3 | 55% |
71
+ | RWKV | 33.1 | 40.9 | 48.7 | 50% |
72
+ | **Full TMT** | **29.4** | **36.1** | **53.4** | **48%** |
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
+ All models: ~120M parameters. TMT trained for 10K steps on WikiText-2 (AdamW, cosine LR, seeds 42/1337/2024).
 
 
75
 
76
  ---
77
 
78
+ ## Architecture at a Glance
 
 
 
 
 
 
79
 
80
  ```
81
+ Input Token Embedding + RoPE
82
+ [× 12 layers]
83
+ MeshBuilder (kNN graph, cosine sim, top-k=8)
84
+ Mesh Attention O(S·k) + Temporal Decay Encoding
85
+ EMA Memory Anchor Cross-Attention (16 anchors, β=0.99)
86
+ Dual-Stream FFN (syntax stream ‖ semantic stream, sigmoid gate)
87
+ Exit Gate σ(W_gate · x) > 0.85 → token frozen
88
+ → LayerNorm → Tied Output Projection
89
+ → Logits (B, S, V)
90
  ```
91
 
92
+ **Output fields** (TMTOutput dataclass):
93
+ - `logits` — (B, S, V) next-token predictions
94
+ - `exit_masks` list of (B, S) booleans, one per layer
95
+ - `confidences` — gate confidence per token per layer
96
+ - `graph_edges`sparse kNN edge list from final layer
97
+ - `memory_state` — (M, D) final EMA anchor states
98
+ - `decay_scalars` — temporal decay weights applied
99
 
100
  ---
101
 
102
+ ## Quick Start
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
+ ```bash
105
+ git clone https://github.com/vignesh2027/TemporalMesh-Transformer
106
+ cd TemporalMesh-Transformer
107
+ pip install -e ".[dev]"
108
  ```
 
 
 
 
 
 
 
109
 
110
  ```python
111
+ from tmt.model.config import TMTConfig
112
+ from tmt.model.model import TMTModel
113
+ import torch
114
+
115
+ config = TMTConfig(
116
+ vocab_size=50257,
117
+ d_model=512,
118
+ n_heads=8,
119
+ n_layers=12,
120
+ graph_k=8,
121
+ exit_threshold=0.85,
122
+ memory_anchors=16,
123
+ )
124
+ model = TMTModel(config) # ~120M params
125
+
126
+ tokens = torch.randint(0, 50257, (1, 256))
127
+ out = model(tokens)
128
+
129
+ print(out.logits.shape) # (1, 256, 50257)
130
+ print(out.exit_masks[-1]) # which tokens exited at layer 12
131
+ avg_exit = sum(m.float().mean() for m in out.exit_masks) / len(out.exit_masks)
132
+ print(f"Avg exit layer: {avg_exit:.2f}") # ~5.8
133
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  ---
136
 
137
+ ## Training
138
+
139
+ ```bash
140
+ python scripts/train.py \
141
+ --dataset wikitext-2 \
142
+ --model_size base \
143
+ --steps 10000 \
144
+ --lr 3e-4 \
145
+ --batch_size 16 \
146
+ --seq_len 256 \
147
+ --exit_threshold 0.85 \
148
+ --graph_k 8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  ```
150
 
151
  ---
152
 
153
+ ## Ablation Summary
154
 
155
+ | Config | PPL | Compute | VRAM |
156
+ |---|---|---|---|
157
+ | Vanilla Transformer | 42.1 | 100% | 18.4 GB |
158
+ | + Mesh Attention only | 37.8 | 62% | 11.2 GB |
159
+ | + Temporal Decay only | 40.3 | 98% | 18.4 GB |
160
+ | + Adaptive Exit only | 39.6 | 51% | 18.4 GB |
161
+ | Mesh + Decay | 34.2 | 61% | 11.2 GB |
162
+ | Mesh + Exit | 35.1 | 50% | 11.2 GB |
163
+ | **Full TMT** | **29.4** | **48%** | **11.2 GB** |
164
 
165
+ The full combination achieves **superadditive gains**: interaction effect = 4.1 PPL beyond sum of individual contributions.
166
 
167
  ---
168
 
169
+ ## Citation
 
 
170
 
171
  ```bibtex
172
+ @misc{vigneshwar2026tmt,
173
+ title = {TemporalMesh Transformer: Dynamic Graph Attention with
174
+ Temporal Semantic Decay and Per-Token Adaptive Depth Routing},
175
+ author = {Vigneshwar LK},
176
+ year = {2026},
177
+ doi = {10.5281/zenodo.20287197},
178
+ url = {https://zenodo.org/records/20287390}
179
  }
180
  ```
181
 
182
  ---
183
 
184
+ ## License
 
 
185
 
186
+ MIT License · © 2026 Vigneshwar LK