nhge 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nhge-0.1.0/PKG-INFO +200 -0
- nhge-0.1.0/README.md +170 -0
- nhge-0.1.0/nhge/__init__.py +51 -0
- nhge-0.1.0/nhge/demo.py +162 -0
- nhge-0.1.0/nhge/nhge_inference.py +200 -0
- nhge-0.1.0/nhge/nhge_model.py +417 -0
- nhge-0.1.0/nhge/nhge_tokenizer.py +167 -0
- nhge-0.1.0/nhge/nhge_trainer.py +312 -0
- nhge-0.1.0/nhge.egg-info/PKG-INFO +200 -0
- nhge-0.1.0/nhge.egg-info/SOURCES.txt +13 -0
- nhge-0.1.0/nhge.egg-info/dependency_links.txt +1 -0
- nhge-0.1.0/nhge.egg-info/requires.txt +8 -0
- nhge-0.1.0/nhge.egg-info/top_level.txt +1 -0
- nhge-0.1.0/pyproject.toml +68 -0
- nhge-0.1.0/setup.cfg +4 -0
nhge-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nhge
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Neuro-Harmonic Graph Engine — iterative harmonic resonance ML architecture
|
|
5
|
+
Author: NHGE Project
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mwala400/nhge
|
|
8
|
+
Project-URL: Repository, https://github.com/mwala400/nhge
|
|
9
|
+
Project-URL: Issues, https://github.com/mwala400/nhge/issues
|
|
10
|
+
Keywords: machine learning,deep learning,graph neural network,harmonic,transformer alternative,NLP,NHGE
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
Requires-Dist: torch>=2.0.0
|
|
24
|
+
Requires-Dist: numpy>=1.24.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
28
|
+
Requires-Dist: black; extra == "dev"
|
|
29
|
+
Requires-Dist: ruff; extra == "dev"
|
|
30
|
+
|
|
31
|
+
# Neuro-Harmonic Graph Engine (NHGE)
|
|
32
|
+
#IDEA INITIATED BY SIR H.A.Mwala (Full name HEKIMA A. MWALA)
|
|
33
|
+
#TANZANIAN
|
|
34
|
+
A novel machine learning architecture that replaces transformer parallelism
|
|
35
|
+
with **iterative harmonic resonance** over a dynamic graph structure.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Key Idea
|
|
40
|
+
|
|
41
|
+
Transformers compute attention in one parallel pass over all tokens simultaneously.
|
|
42
|
+
**NHGE iterates** — tokens are graph nodes that resonate with each other, updating
|
|
43
|
+
their states step by step until the graph reaches harmonic convergence.
|
|
44
|
+
|
|
45
|
+
| Property | Transformer | NHGE |
|
|
46
|
+
|---------------------|------------------------------|-----------------------------------|
|
|
47
|
+
| Processing | Parallel attention | Iterative harmonic resonance |
|
|
48
|
+
| Depth | Fixed layers | Dynamic (stops at convergence) |
|
|
49
|
+
| Edge weights | Attention scores | Similarity × phase coherence |
|
|
50
|
+
| Memory | O(N²) attention matrix | O(N²) but iterative, not stacked |
|
|
51
|
+
| Simple inputs | Same cost as complex | Converges faster → cheaper |
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Architecture
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
Input tokens
|
|
59
|
+
↓
|
|
60
|
+
[Embedding + positional + phase initialisation]
|
|
61
|
+
↓
|
|
62
|
+
┌─────────────────────────────────────────┐
|
|
63
|
+
│ Harmonic iteration loop (max T steps) │
|
|
64
|
+
│ │
|
|
65
|
+
│ HarmonicEdgeLayer │
|
|
66
|
+
│ edge_w = softmax(Q·K/√d) × cos(Δθ) │
|
|
67
|
+
│ │
|
|
68
|
+
│ HarmonicNodeUpdate │
|
|
69
|
+
│ h_v ← h_v + FFN(h_v + Σ w·h_u) │
|
|
70
|
+
│ │
|
|
71
|
+
│ PhaseUpdate │
|
|
72
|
+
│ θ ← θ + α·tanh(W·h)·π │
|
|
73
|
+
│ │
|
|
74
|
+
│ Convergence: ||h_t − h_{t−1}|| < ε │
|
|
75
|
+
└─────────────────────────────────────────┘
|
|
76
|
+
↓
|
|
77
|
+
[Graph readout: mean / CLS / attention pool]
|
|
78
|
+
↓
|
|
79
|
+
Output logits
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Files
|
|
85
|
+
|
|
86
|
+
| File | Purpose |
|
|
87
|
+
|----------------------|-------------------------------------------------|
|
|
88
|
+
| `nhge_model.py` | Core NHGE architecture (all layers + full model)|
|
|
89
|
+
| `nhge_tokenizer.py` | Word / char / subword tokenizer |
|
|
90
|
+
| `nhge_trainer.py` | Training engine with warmup LR, AMP, checkpoints|
|
|
91
|
+
| `nhge_inference.py` | Generation, classification, embeddings |
|
|
92
|
+
| `demo.py` | Runnable end-to-end demo (no GPU needed) |
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Quick start
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from nhge_model import nhge_small
|
|
100
|
+
from nhge_tokenizer import NHGETokenizer
|
|
101
|
+
from nhge_inference import NHGEInference
|
|
102
|
+
|
|
103
|
+
# 1. Build tokenizer
|
|
104
|
+
tok = NHGETokenizer(mode="word")
|
|
105
|
+
tok.build_vocab(your_texts, min_freq=2)
|
|
106
|
+
|
|
107
|
+
# 2. Build model
|
|
108
|
+
model = nhge_small(vocab_size=tok.vocab_size, num_classes=2)
|
|
109
|
+
|
|
110
|
+
# 3. Inference
|
|
111
|
+
inf = NHGEInference(model, tok, device="cuda")
|
|
112
|
+
results = inf.classify(["Your input text here"], label_names=["neg", "pos"])
|
|
113
|
+
|
|
114
|
+
# 4. Generate
|
|
115
|
+
text = inf.generate("The harmonic graph", max_new_tokens=50)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Training
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from nhge_trainer import NHGETrainer, TokenDataset
|
|
124
|
+
from torch.utils.data import DataLoader
|
|
125
|
+
|
|
126
|
+
dataset = TokenDataset(encoded_tokens, labels=class_labels, max_len=128)
|
|
127
|
+
loader = DataLoader(dataset, batch_size=32, shuffle=True)
|
|
128
|
+
|
|
129
|
+
trainer = NHGETrainer(model, loader, val_loader=val_loader,
|
|
130
|
+
task="cls", lr=3e-4, device="cuda")
|
|
131
|
+
history = trainer.train(epochs=20)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Model sizes
|
|
137
|
+
|
|
138
|
+
| Name | d_model | n_heads | n_layers | max_iter | ~Params |
|
|
139
|
+
|---------------|---------|---------|----------|----------|---------|
|
|
140
|
+
| `nhge_small` | 128 | 4 | 2 | 6 | ~3M |
|
|
141
|
+
| `nhge_base` | 512 | 8 | 4 | 8 | ~85M |
|
|
142
|
+
| `nhge_large` | 1024 | 16 | 6 | 10 | ~340M |
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Requirements
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
torch >= 2.0
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
No other dependencies required for core functionality.
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## Run the demo
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
python demo.py
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Theory
|
|
165
|
+
|
|
166
|
+
The harmonic edge weight combines two signals:
|
|
167
|
+
1. **Semantic similarity** — scaled dot product of Q and K projections
|
|
168
|
+
2. **Phase coherence** — cos(θ_i − θ_j) between learned phase angles
|
|
169
|
+
|
|
170
|
+
Nodes in phase amplify each other's signals. Out-of-phase nodes cancel.
|
|
171
|
+
This mimics oscillatory dynamics in biological neural networks, where
|
|
172
|
+
synchronised firing encodes binding of related concepts.
|
|
173
|
+
|
|
174
|
+
The phase angles are updated each iteration via a damped gradient:
|
|
175
|
+
```
|
|
176
|
+
θ ← θ + α · tanh(W·h) · π
|
|
177
|
+
```
|
|
178
|
+
This allows the network to "tune" its resonance frequencies as information
|
|
179
|
+
propagates — a form of learned synchronisation.
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## Licence
|
|
184
|
+
|
|
185
|
+
MIT — use freely, extend openly, credit the NHGE project.
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
MIT License
|
|
189
|
+
|
|
190
|
+
Copyright (c) 2026 H.A. Mwala
|
|
191
|
+
|
|
192
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
193
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
194
|
+
in the Software without restriction, including without limitation the rights
|
|
195
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
196
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
197
|
+
furnished to do so, subject to the following conditions:
|
|
198
|
+
|
|
199
|
+
The above copyright notice and this permission notice shall be included in all
|
|
200
|
+
copies or substantial portions of the Software.
|
nhge-0.1.0/README.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# Neuro-Harmonic Graph Engine (NHGE)
|
|
2
|
+
#IDEA INITIATED BY SIR H.A.Mwala (Full name HEKIMA A. MWALA)
|
|
3
|
+
#TANZANIAN
|
|
4
|
+
A novel machine learning architecture that replaces transformer parallelism
|
|
5
|
+
with **iterative harmonic resonance** over a dynamic graph structure.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Key Idea
|
|
10
|
+
|
|
11
|
+
Transformers compute attention in one parallel pass over all tokens simultaneously.
|
|
12
|
+
**NHGE iterates** — tokens are graph nodes that resonate with each other, updating
|
|
13
|
+
their states step by step until the graph reaches harmonic convergence.
|
|
14
|
+
|
|
15
|
+
| Property | Transformer | NHGE |
|
|
16
|
+
|---------------------|------------------------------|-----------------------------------|
|
|
17
|
+
| Processing | Parallel attention | Iterative harmonic resonance |
|
|
18
|
+
| Depth | Fixed layers | Dynamic (stops at convergence) |
|
|
19
|
+
| Edge weights | Attention scores | Similarity × phase coherence |
|
|
20
|
+
| Memory | O(N²) attention matrix | O(N²) but iterative, not stacked |
|
|
21
|
+
| Simple inputs | Same cost as complex | Converges faster → cheaper |
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Architecture
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
Input tokens
|
|
29
|
+
↓
|
|
30
|
+
[Embedding + positional + phase initialisation]
|
|
31
|
+
↓
|
|
32
|
+
┌─────────────────────────────────────────┐
|
|
33
|
+
│ Harmonic iteration loop (max T steps) │
|
|
34
|
+
│ │
|
|
35
|
+
│ HarmonicEdgeLayer │
|
|
36
|
+
│ edge_w = softmax(Q·K/√d) × cos(Δθ) │
|
|
37
|
+
│ │
|
|
38
|
+
│ HarmonicNodeUpdate │
|
|
39
|
+
│ h_v ← h_v + FFN(h_v + Σ w·h_u) │
|
|
40
|
+
│ │
|
|
41
|
+
│ PhaseUpdate │
|
|
42
|
+
│ θ ← θ + α·tanh(W·h)·π │
|
|
43
|
+
│ │
|
|
44
|
+
│ Convergence: ||h_t − h_{t−1}|| < ε │
|
|
45
|
+
└─────────────────────────────────────────┘
|
|
46
|
+
↓
|
|
47
|
+
[Graph readout: mean / CLS / attention pool]
|
|
48
|
+
↓
|
|
49
|
+
Output logits
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Files
|
|
55
|
+
|
|
56
|
+
| File | Purpose |
|
|
57
|
+
|----------------------|-------------------------------------------------|
|
|
58
|
+
| `nhge_model.py` | Core NHGE architecture (all layers + full model)|
|
|
59
|
+
| `nhge_tokenizer.py` | Word / char / subword tokenizer |
|
|
60
|
+
| `nhge_trainer.py` | Training engine with warmup LR, AMP, checkpoints|
|
|
61
|
+
| `nhge_inference.py` | Generation, classification, embeddings |
|
|
62
|
+
| `demo.py` | Runnable end-to-end demo (no GPU needed) |
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Quick start
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from nhge_model import nhge_small
|
|
70
|
+
from nhge_tokenizer import NHGETokenizer
|
|
71
|
+
from nhge_inference import NHGEInference
|
|
72
|
+
|
|
73
|
+
# 1. Build tokenizer
|
|
74
|
+
tok = NHGETokenizer(mode="word")
|
|
75
|
+
tok.build_vocab(your_texts, min_freq=2)
|
|
76
|
+
|
|
77
|
+
# 2. Build model
|
|
78
|
+
model = nhge_small(vocab_size=tok.vocab_size, num_classes=2)
|
|
79
|
+
|
|
80
|
+
# 3. Inference
|
|
81
|
+
inf = NHGEInference(model, tok, device="cuda")
|
|
82
|
+
results = inf.classify(["Your input text here"], label_names=["neg", "pos"])
|
|
83
|
+
|
|
84
|
+
# 4. Generate
|
|
85
|
+
text = inf.generate("The harmonic graph", max_new_tokens=50)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Training
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from nhge_trainer import NHGETrainer, TokenDataset
|
|
94
|
+
from torch.utils.data import DataLoader
|
|
95
|
+
|
|
96
|
+
dataset = TokenDataset(encoded_tokens, labels=class_labels, max_len=128)
|
|
97
|
+
loader = DataLoader(dataset, batch_size=32, shuffle=True)
|
|
98
|
+
|
|
99
|
+
trainer = NHGETrainer(model, loader, val_loader=val_loader,
|
|
100
|
+
task="cls", lr=3e-4, device="cuda")
|
|
101
|
+
history = trainer.train(epochs=20)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Model sizes
|
|
107
|
+
|
|
108
|
+
| Name | d_model | n_heads | n_layers | max_iter | ~Params |
|
|
109
|
+
|---------------|---------|---------|----------|----------|---------|
|
|
110
|
+
| `nhge_small` | 128 | 4 | 2 | 6 | ~3M |
|
|
111
|
+
| `nhge_base` | 512 | 8 | 4 | 8 | ~85M |
|
|
112
|
+
| `nhge_large` | 1024 | 16 | 6 | 10 | ~340M |
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## Requirements
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
torch >= 2.0
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
No other dependencies required for core functionality.
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Run the demo
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
python demo.py
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## Theory
|
|
135
|
+
|
|
136
|
+
The harmonic edge weight combines two signals:
|
|
137
|
+
1. **Semantic similarity** — scaled dot product of Q and K projections
|
|
138
|
+
2. **Phase coherence** — cos(θ_i − θ_j) between learned phase angles
|
|
139
|
+
|
|
140
|
+
Nodes in phase amplify each other's signals. Out-of-phase nodes cancel.
|
|
141
|
+
This mimics oscillatory dynamics in biological neural networks, where
|
|
142
|
+
synchronised firing encodes binding of related concepts.
|
|
143
|
+
|
|
144
|
+
The phase angles are updated each iteration via a damped gradient:
|
|
145
|
+
```
|
|
146
|
+
θ ← θ + α · tanh(W·h) · π
|
|
147
|
+
```
|
|
148
|
+
This allows the network to "tune" its resonance frequencies as information
|
|
149
|
+
propagates — a form of learned synchronisation.
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Licence
|
|
154
|
+
|
|
155
|
+
MIT — use freely, extend openly, credit the NHGE project.
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
MIT License
|
|
159
|
+
|
|
160
|
+
Copyright (c) 2026 H.A. Mwala
|
|
161
|
+
|
|
162
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
163
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
164
|
+
in the Software without restriction, including without limitation the rights
|
|
165
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
166
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
167
|
+
furnished to do so, subject to the following conditions:
|
|
168
|
+
|
|
169
|
+
The above copyright notice and this permission notice shall be included in all
|
|
170
|
+
copies or substantial portions of the Software.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""
|
|
2
|
+
NHGE — Neuro-Harmonic Graph Engine
|
|
3
|
+
====================================
|
|
4
|
+
A next-generation ML architecture that replaces transformer parallelism
|
|
5
|
+
with iterative harmonic resonance over a dynamic graph structure.
|
|
6
|
+
|
|
7
|
+
Quick start:
|
|
8
|
+
from nhge import NHGE, nhge_small, nhge_base, nhge_large
|
|
9
|
+
from nhge import NHGETokenizer, NHGETrainer, NHGEInference
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.0"
|
|
13
|
+
__author__ = "NHGE Project"
|
|
14
|
+
__license__ = "MIT"
|
|
15
|
+
|
|
16
|
+
from nhge.nhge_model import (
|
|
17
|
+
NHGE,
|
|
18
|
+
NHGEBlock,
|
|
19
|
+
HarmonicEdgeLayer,
|
|
20
|
+
HarmonicNodeUpdate,
|
|
21
|
+
PhaseUpdate,
|
|
22
|
+
nhge_small,
|
|
23
|
+
nhge_base,
|
|
24
|
+
nhge_large,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
from nhge.nhge_tokenizer import NHGETokenizer
|
|
28
|
+
|
|
29
|
+
from nhge.nhge_trainer import NHGETrainer, TokenDataset, WarmupCosineScheduler
|
|
30
|
+
|
|
31
|
+
from nhge.nhge_inference import NHGEInference
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
# Model
|
|
35
|
+
"NHGE",
|
|
36
|
+
"NHGEBlock",
|
|
37
|
+
"HarmonicEdgeLayer",
|
|
38
|
+
"HarmonicNodeUpdate",
|
|
39
|
+
"PhaseUpdate",
|
|
40
|
+
"nhge_small",
|
|
41
|
+
"nhge_base",
|
|
42
|
+
"nhge_large",
|
|
43
|
+
# Tokenizer
|
|
44
|
+
"NHGETokenizer",
|
|
45
|
+
# Training
|
|
46
|
+
"NHGETrainer",
|
|
47
|
+
"TokenDataset",
|
|
48
|
+
"WarmupCosineScheduler",
|
|
49
|
+
# Inference
|
|
50
|
+
"NHGEInference",
|
|
51
|
+
]
|
nhge-0.1.0/nhge/demo.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""
|
|
2
|
+
NHGE Quick-Start Demo
|
|
3
|
+
======================
|
|
4
|
+
Run this file to see NHGE working end-to-end:
|
|
5
|
+
1. Build a tiny vocabulary from sample text
|
|
6
|
+
2. Construct an NHGE-small model
|
|
7
|
+
3. Do a forward pass and inspect convergence behaviour
|
|
8
|
+
4. Run a mock classification task
|
|
9
|
+
5. Print harmonic state diagnostics
|
|
10
|
+
|
|
11
|
+
No GPU required — runs on CPU in seconds.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import torch
|
|
15
|
+
import sys
|
|
16
|
+
import os
|
|
17
|
+
|
|
18
|
+
# Allow imports from the same directory
|
|
19
|
+
sys.path.insert(0, os.path.dirname(__file__))
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
from nhge.nhge_model import NHGE, nhge_small
|
|
23
|
+
from nhge.nhge_tokenizer import NHGETokenizer
|
|
24
|
+
from nhge.nhge_inference import NHGEInference
|
|
25
|
+
except ImportError:
|
|
26
|
+
from nhge_model import NHGE, nhge_small
|
|
27
|
+
from nhge_tokenizer import NHGETokenizer
|
|
28
|
+
from nhge_inference import NHGEInference
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# ------------------------------------------------------------------
|
|
32
|
+
# 1. Sample corpus
|
|
33
|
+
# ------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
CORPUS = [
|
|
36
|
+
"the neuro harmonic graph engine processes tokens as graph nodes",
|
|
37
|
+
"harmonic resonance allows information to propagate iteratively",
|
|
38
|
+
"unlike transformers nhge does not require parallel attention",
|
|
39
|
+
"each node updates its state based on neighbouring node phases",
|
|
40
|
+
"convergence is detected dynamically reducing unnecessary computation",
|
|
41
|
+
"the model adapts the number of iterations to input complexity",
|
|
42
|
+
"simple inputs converge quickly complex ones require more iterations",
|
|
43
|
+
"phase alignment between nodes encodes semantic similarity",
|
|
44
|
+
"the harmonic edge weight combines similarity and phase coherence",
|
|
45
|
+
"this architecture is an advancement over the transformer model",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
LABELS = [0, 0, 1, 0, 0, 0, 0, 0, 0, 1] # 0=architecture, 1=comparison
|
|
49
|
+
LABEL_NAMES = ["architecture", "comparison"]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# ------------------------------------------------------------------
|
|
53
|
+
# 2. Build tokenizer
|
|
54
|
+
# ------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
print("=" * 60)
|
|
57
|
+
print("NEURO-HARMONIC GRAPH ENGINE — Demo")
|
|
58
|
+
print("=" * 60)
|
|
59
|
+
print()
|
|
60
|
+
|
|
61
|
+
tok = NHGETokenizer(mode="word")
|
|
62
|
+
tok.build_vocab(CORPUS, min_freq=1, max_vocab=500)
|
|
63
|
+
print(f"Vocab size: {tok.vocab_size}")
|
|
64
|
+
print()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# ------------------------------------------------------------------
|
|
68
|
+
# 3. Build NHGE-small
|
|
69
|
+
# ------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
model = nhge_small(
|
|
72
|
+
vocab_size=tok.vocab_size,
|
|
73
|
+
num_classes=2, # binary classification demo
|
|
74
|
+
readout="attention",
|
|
75
|
+
epsilon=1e-4,
|
|
76
|
+
dropout=0.0, # no dropout for inference demo
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
|
80
|
+
print(f"Model parameters: {total_params:,}")
|
|
81
|
+
print()
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# ------------------------------------------------------------------
|
|
85
|
+
# 4. Single forward pass — inspect convergence
|
|
86
|
+
# ------------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
print("--- Single forward pass ---")
|
|
89
|
+
enc = tok.batch_encode(CORPUS[:3], max_length=24, add_cls=True)
|
|
90
|
+
ids = torch.tensor(enc["input_ids"], dtype=torch.long)
|
|
91
|
+
mask = torch.tensor(enc["attention_mask"], dtype=torch.bool)
|
|
92
|
+
|
|
93
|
+
with torch.no_grad():
|
|
94
|
+
out = model(ids, mask, lm_mode=False, return_iterations=True)
|
|
95
|
+
|
|
96
|
+
print(f"Logits shape : {out['logits'].shape}")
|
|
97
|
+
print(f"Iterations ran: {out['iterations']}")
|
|
98
|
+
print(f"Deltas per iter: {[f'{d:.5f}' for d in out.get('deltas', [])]}")
|
|
99
|
+
print()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# ------------------------------------------------------------------
|
|
103
|
+
# 5. Classification demo
|
|
104
|
+
# ------------------------------------------------------------------
|
|
105
|
+
|
|
106
|
+
print("--- Classification demo ---")
|
|
107
|
+
inf = NHGEInference(model, tok, device="cpu")
|
|
108
|
+
|
|
109
|
+
results = inf.classify(
|
|
110
|
+
["harmonic resonance drives the graph update",
|
|
111
|
+
"nhge replaces the transformer in language models"],
|
|
112
|
+
max_length=24,
|
|
113
|
+
label_names=LABEL_NAMES,
|
|
114
|
+
)
|
|
115
|
+
for i, r in enumerate(results):
|
|
116
|
+
probs = [f"{p:.3f}" for p in r["scores"]]
|
|
117
|
+
print(f" [{i}] pred={r['name']:<14} scores={probs} iters={r['iters']}")
|
|
118
|
+
print()
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
# ------------------------------------------------------------------
|
|
122
|
+
# 6. Harmonic state introspection
|
|
123
|
+
# ------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
print("--- Harmonic state introspection ---")
|
|
126
|
+
state = inf.harmonic_state(
|
|
127
|
+
"phase alignment between nodes encodes semantic similarity",
|
|
128
|
+
max_length=16,
|
|
129
|
+
)
|
|
130
|
+
print(f" Tokens : {state['tokens']}")
|
|
131
|
+
print(f" Iterations: {state['iterations']}")
|
|
132
|
+
print(f" Deltas : {[f'{d:.5f}' for d in state['deltas']]}")
|
|
133
|
+
print(f" Node norms: {[f'{n:.3f}' for n in state['h_norm']]}")
|
|
134
|
+
print()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# ------------------------------------------------------------------
|
|
138
|
+
# 7. Embedding similarity
|
|
139
|
+
# ------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
print("--- Embedding similarity ---")
|
|
142
|
+
embs = inf.embed(
|
|
143
|
+
["harmonic graph iteration",
|
|
144
|
+
"neuro harmonic resonance",
|
|
145
|
+
"unrelated random words here"],
|
|
146
|
+
max_length=12,
|
|
147
|
+
)
|
|
148
|
+
def cosine(a, b):
|
|
149
|
+
return (a @ b / (a.norm() * b.norm())).item()
|
|
150
|
+
|
|
151
|
+
print(f" sim(0,1) = {cosine(embs[0], embs[1]):.4f} ← expect higher (related)")
|
|
152
|
+
print(f" sim(0,2) = {cosine(embs[0], embs[2]):.4f} ← expect lower (unrelated)")
|
|
153
|
+
print()
|
|
154
|
+
|
|
155
|
+
print("=" * 60)
|
|
156
|
+
print("NHGE demo complete — model is working correctly.")
|
|
157
|
+
print("Next steps:")
|
|
158
|
+
print(" 1. Build a real tokenizer with nhge_tokenizer.NHGETokenizer.build_vocab()")
|
|
159
|
+
print(" 2. Prepare DataLoaders using nhge_trainer.TokenDataset")
|
|
160
|
+
print(" 3. Train with nhge_trainer.NHGETrainer(model, train_loader, ...)")
|
|
161
|
+
print(" 4. Generate text with nhge_inference.NHGEInference.generate()")
|
|
162
|
+
print("=" * 60)
|