recursive-adaptive-network 0.3.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.
- recursive_adaptive_network-0.3.0/LICENSE +21 -0
- recursive_adaptive_network-0.3.0/MANIFEST.in +17 -0
- recursive_adaptive_network-0.3.0/PKG-INFO +269 -0
- recursive_adaptive_network-0.3.0/README.md +239 -0
- recursive_adaptive_network-0.3.0/csrc/binding.cpp +158 -0
- recursive_adaptive_network-0.3.0/csrc/fp32_gemv.h +170 -0
- recursive_adaptive_network-0.3.0/csrc/int8_gemm.h +240 -0
- recursive_adaptive_network-0.3.0/csrc/ran_step.h +492 -0
- recursive_adaptive_network-0.3.0/csrc/ran_step_binding.cpp +271 -0
- recursive_adaptive_network-0.3.0/pyproject.toml +70 -0
- recursive_adaptive_network-0.3.0/setup.cfg +4 -0
- recursive_adaptive_network-0.3.0/setup.py +162 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/__init__.py +101 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/api.py +195 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/cli/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/cli/main.py +330 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/configuration_ran.py +156 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/core/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/core/nn.py +228 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/core/tensor.py +382 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/data/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/data/dataset.py +63 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/distributed/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/framework/__init__.py +35 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/framework/agent.py +109 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/framework/memory/__init__.py +110 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/framework/skills/__init__.py +83 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/framework/skills/builtin.py +66 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/framework/tools/__init__.py +105 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/framework/tools/builtin.py +74 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/hub/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/inference/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/inference/fast_forward.py +472 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/inference/ran_step_native.py +306 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/legacy_format.py +65 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/modeling_memory.py +189 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/modeling_ran.py +225 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/modeling_reasoning.py +129 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/modeling_routing.py +36 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/modeling_state_engine.py +96 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/modeling_utils.py +92 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/optimization/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/optimization/losses.py +29 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/optimization/optimizer.py +71 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base/config.json +16 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base/metadata.json +12 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base/tokenizer.json +1 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base/weights.pkl +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base_int8/config.json +16 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base_int8/metadata.json +6 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base_int8/tokenizer.json +1 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/base_int8/weights_int8.pkl +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/corpus_id.txt +180 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/pretrained_models/train_base_model.py +95 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/py.typed +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/quantization/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/quantization/int8.py +178 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/quantization/int8_native.py +161 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/tokenization_ran.py +115 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/training/__init__.py +0 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/training/hardware.py +102 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network/training/trainer.py +149 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network.egg-info/PKG-INFO +269 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network.egg-info/SOURCES.txt +74 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network.egg-info/dependency_links.txt +1 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network.egg-info/entry_points.txt +2 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network.egg-info/requires.txt +4 -0
- recursive_adaptive_network-0.3.0/src/recursive_adaptive_network.egg-info/top_level.txt +1 -0
- recursive_adaptive_network-0.3.0/tests/test_components.py +112 -0
- recursive_adaptive_network-0.3.0/tests/test_fast_forward_matches_autograd.py +215 -0
- recursive_adaptive_network-0.3.0/tests/test_framework.py +144 -0
- recursive_adaptive_network-0.3.0/tests/test_int8_gemm_native.py +112 -0
- recursive_adaptive_network-0.3.0/tests/test_model_and_training.py +444 -0
- recursive_adaptive_network-0.3.0/tests/test_ran_step_native.py +170 -0
- recursive_adaptive_network-0.3.0/tests/test_tensor.py +80 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ZeroBoy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Ensures the C++ source for the optional _ran_native extension
|
|
2
|
+
# (see setup.py) is included in source distributions (sdist), not
|
|
3
|
+
# just the compiled .so in wheels. Without this, `pip install` from
|
|
4
|
+
# an sdist (e.g. on a platform with no matching prebuilt wheel) would
|
|
5
|
+
# have nothing to compile, silently losing the native extension even
|
|
6
|
+
# on a machine that DOES have a working compiler.
|
|
7
|
+
recursive-include csrc *.h *.cpp
|
|
8
|
+
include setup.py
|
|
9
|
+
|
|
10
|
+
# Bundled demo model (previously shipped only in the separate
|
|
11
|
+
# ran-framework package's pretrained_models/ folder -- now part of
|
|
12
|
+
# this package, see recursive_adaptive_network/pretrained_models/ and
|
|
13
|
+
# RAN.from_pretrained("bundled-base") / ("bundled-base-int8") in
|
|
14
|
+
# api.py). Needed here in addition to [tool.setuptools.package-data]
|
|
15
|
+
# in pyproject.toml so these non-.py files are included in sdists too,
|
|
16
|
+
# not just wheels built directly from this checkout.
|
|
17
|
+
recursive-include src/recursive_adaptive_network/pretrained_models *.json *.pkl *.txt *.py
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: recursive-adaptive-network
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: RAN: a from-scratch, non-Transformer, CPU-first neural architecture for people without GPUs -- now bundled with an extensible Agent/Tools/Memory/Skills framework (previously the separate ran-framework package).
|
|
5
|
+
Author: ZeroBoy
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/REPLACE_ME/recursive-adaptive-network
|
|
8
|
+
Project-URL: Repository, https://github.com/REPLACE_ME/recursive-adaptive-network
|
|
9
|
+
Project-URL: Documentation, https://github.com/REPLACE_ME/recursive-adaptive-network/blob/main/docs/ARCHITECTURE.md
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/REPLACE_ME/recursive-adaptive-network/issues
|
|
11
|
+
Keywords: artificial intelligence,deep learning,neural network,cpu inference,low resource,recurrent neural network,adaptive computation
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Classifier: Operating System :: OS Independent
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: numpy>=1.24
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# RAN — Recursive Adaptive Network
|
|
32
|
+
|
|
33
|
+
**"AI pintar tidak boleh hanya bisa dibuat oleh orang yang memiliki GPU mahal."**
|
|
34
|
+
|
|
35
|
+
RAN is a from-scratch neural architecture and training framework built
|
|
36
|
+
as an alternative to the Transformer — no self-attention, no QKV — for
|
|
37
|
+
people whose only computer is a 2-4 core CPU laptop with 4-8GB of RAM
|
|
38
|
+
and no GPU.
|
|
39
|
+
|
|
40
|
+
Published as a single `recursive-adaptive-network` package on PyPI,
|
|
41
|
+
structured the same way `transformers` is: a `configuration_ran.py`
|
|
42
|
+
(hyperparameters only) separated from `modeling_ran.py` (the actual
|
|
43
|
+
architecture), a `RANConfig`/`RANModel` pair, and
|
|
44
|
+
`save_pretrained()`/`from_pretrained()` for a directory-based model
|
|
45
|
+
format.
|
|
46
|
+
|
|
47
|
+
**As of v0.2, this single package also bundles the Agent/Tools/Memory/Skills
|
|
48
|
+
framework** (`recursive_adaptive_network.framework`) that previously
|
|
49
|
+
shipped as a separate `ran-framework` package — `pip install
|
|
50
|
+
recursive-adaptive-network` now gives you both in one install, no
|
|
51
|
+
second `pip install` step needed. A small pretrained demo model is
|
|
52
|
+
bundled too (`RAN.from_pretrained("bundled-base")`), so you can try the
|
|
53
|
+
Agent immediately without training anything yourself first.
|
|
54
|
+
|
|
55
|
+
## Install
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install recursive-adaptive-network
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
That's it — the only hard dependency is NumPy. No PyTorch, no CUDA.
|
|
62
|
+
(Until this is actually published to PyPI, see "Building and publishing
|
|
63
|
+
this package yourself" below to build and install it locally.)
|
|
64
|
+
|
|
65
|
+
## Quickstart: the Agent framework
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from recursive_adaptive_network import RAN, Agent
|
|
69
|
+
from recursive_adaptive_network.framework.tools import ToolRegistry
|
|
70
|
+
from recursive_adaptive_network.framework.tools.builtin import ALL_BUILTIN_TOOLS
|
|
71
|
+
from recursive_adaptive_network.framework.skills import SkillRegistry
|
|
72
|
+
from recursive_adaptive_network.framework.skills.builtin import ALL_BUILTIN_SKILLS
|
|
73
|
+
|
|
74
|
+
model = RAN.from_pretrained("bundled-base") # small demo model, bundled in this package
|
|
75
|
+
|
|
76
|
+
tools = ToolRegistry()
|
|
77
|
+
for t in ALL_BUILTIN_TOOLS:
|
|
78
|
+
tools.register(t)
|
|
79
|
+
skills = SkillRegistry()
|
|
80
|
+
for s in ALL_BUILTIN_SKILLS:
|
|
81
|
+
skills.register(s)
|
|
82
|
+
|
|
83
|
+
agent = Agent(model, tools=tools, skills=skills)
|
|
84
|
+
print(agent.respond("halo"))
|
|
85
|
+
print(agent.respond("jam berapa sekarang?"))
|
|
86
|
+
print(agent.respond("hitung 12 * (3 + 4)"))
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Swap `RAN.from_pretrained("bundled-base")` for your own trained model
|
|
90
|
+
(`RAN.from_pretrained("my_model_dir")`, or `RAN(parameters="10m")` +
|
|
91
|
+
`.train(...)`) once you're past trying the framework out — the bundled
|
|
92
|
+
demo model is intentionally small (see docs/BASE_MODEL.md) and not
|
|
93
|
+
meant for real use.
|
|
94
|
+
|
|
95
|
+
## Status: v0.2 (early, honest)
|
|
96
|
+
|
|
97
|
+
This is a real, runnable implementation, not a pseudocode sketch. It
|
|
98
|
+
has been tested end to end: forward pass, backward pass, gradient
|
|
99
|
+
flow through every learnable component, an overfitting sanity check
|
|
100
|
+
(loss goes down), checkpoint save/resume, a `save_pretrained`/
|
|
101
|
+
`from_pretrained` round-trip, and the Agent framework's tools/memory/
|
|
102
|
+
skills wiring. All of that is covered by the automated test suite in
|
|
103
|
+
`tests/` (70 tests, run with `pytest tests/`).
|
|
104
|
+
|
|
105
|
+
**What v0.2 is NOT (yet):**
|
|
106
|
+
- **Benchmarked against a Transformer, and the result favors the
|
|
107
|
+
Transformer at this scale.** `benchmarks/ran_vs_transformer.py`
|
|
108
|
+
compares RAN against a parameter-matched from-scratch Transformer
|
|
109
|
+
decoder (both on the same Tensor/autograd engine, so implementation
|
|
110
|
+
quality isn't a confound). On this sandbox's 1-core CPU: the
|
|
111
|
+
|
|
112
|
+
Transformer is **15-23x faster**, uses **~4x less memory**, and
|
|
113
|
+
reaches **lower training loss** after the same steps on the same
|
|
114
|
+
corpus. Profiling traces the gap to Python-level overhead in RAN's
|
|
115
|
+
per-token loop (hundreds of thousands of function calls for a
|
|
116
|
+
64-token sequence), not to the arithmetic itself — see
|
|
117
|
+
`docs/ROADMAP.md` for the full numbers and what would need to change
|
|
118
|
+
(a compiled/vectorized inner loop) before re-testing this. **No
|
|
119
|
+
"RAN is more efficient than X" claim is made anywhere in this
|
|
120
|
+
repo** — if anything, the one real measurement done so far points
|
|
121
|
+
the other way, at this scale, in this implementation.
|
|
122
|
+
- Not trained on any real corpus at a meaningful scale. The example
|
|
123
|
+
training run in `examples/` uses a few sentences and will not
|
|
124
|
+
produce coherent text — it exists to prove the pipeline works, not
|
|
125
|
+
to demonstrate language ability.
|
|
126
|
+
- No C++ backend yet (pure Python + NumPy). INT8 quantization exists
|
|
127
|
+
for checkpoint-size reduction only (`quantization/int8.py`, ~3.9x
|
|
128
|
+
smaller files, no speed/RAM benefit — see its module docstring for
|
|
129
|
+
why). No distributed CPU training, no Model Hub. See
|
|
130
|
+
`docs/ROADMAP.md` for what's built vs. planned, mapped to the
|
|
131
|
+
original spec.
|
|
132
|
+
- No PyTorch/CUDA dependency at all in v0.1 — every learnable op runs
|
|
133
|
+
on a small NumPy-based autograd engine (`core/tensor.py`), which is
|
|
134
|
+
actually a feature here: the "runs without a GPU" promise holds
|
|
135
|
+
from the very first line of code, on machines where even installing
|
|
136
|
+
a GPU-capable PyTorch wheel is impractical.
|
|
137
|
+
|
|
138
|
+
## Why not just run a Transformer on CPU?
|
|
139
|
+
|
|
140
|
+
Self-attention is O(n²) in sequence length. RAN instead maintains a
|
|
141
|
+
fixed-size recurrent **State Engine** (O(1) per token), a **learned,
|
|
142
|
+
multi-tier Adaptive Neural Memory** instead of a growing KV-cache, an
|
|
143
|
+
**Importance Gate** that decides how much compute/memory a piece of
|
|
144
|
+
information deserves, and a **Recursive Reasoning Core** with adaptive
|
|
145
|
+
halting so a simple prompt costs less compute than a complex one.
|
|
146
|
+
None of these use QKV attention. See `docs/ARCHITECTURE.md` for the
|
|
147
|
+
full pipeline and the reasoning behind each piece.
|
|
148
|
+
|
|
149
|
+
## Quickstart
|
|
150
|
+
|
|
151
|
+
### Command line
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
ran doctor # check what your hardware can handle
|
|
155
|
+
ran init my_project && cd my_project
|
|
156
|
+
|
|
157
|
+
ran tokenizer train --data data/corpus.txt --vocab-size 4096 --out tokenizer/tokenizer.json
|
|
158
|
+
ran model create --parameters 10m --tokenizer tokenizer/tokenizer.json --out model
|
|
159
|
+
ran train --model model --data data/corpus.txt --steps 500
|
|
160
|
+
ran run --model model --prompt "Halo dunia"
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### High-level Python API
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
from recursive_adaptive_network import RAN
|
|
167
|
+
|
|
168
|
+
model = RAN(parameters="10m")
|
|
169
|
+
model.train_tokenizer(open("corpus.txt").read())
|
|
170
|
+
model.train(dataset_path="corpus.txt", steps=500)
|
|
171
|
+
model.save_pretrained("my_model")
|
|
172
|
+
|
|
173
|
+
model2 = RAN.from_pretrained("my_model")
|
|
174
|
+
print(model2.generate("Halo dunia"))
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Direct architecture access (HuggingFace-style)
|
|
178
|
+
|
|
179
|
+
For custom training loops, research, or composing RAN's components
|
|
180
|
+
into something else — config and model are separate classes, the same
|
|
181
|
+
way `AutoConfig`/`AutoModel` are in `transformers`:
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from recursive_adaptive_network import RANConfig, RANModel
|
|
185
|
+
import numpy as np
|
|
186
|
+
|
|
187
|
+
config = RANConfig(vocab_size=4096, hidden_dim=128, state_dim=128,
|
|
188
|
+
memory_slot_dim=64, max_reasoning_steps=4)
|
|
189
|
+
model = RANModel(config)
|
|
190
|
+
|
|
191
|
+
tokens = np.random.randint(0, 4096, size=(2, 16)) # (batch, seq_len)
|
|
192
|
+
logits_list, final_state, info = model.forward_sequence(tokens)
|
|
193
|
+
|
|
194
|
+
model.config.save_pretrained("my_config") # config.json only
|
|
195
|
+
config2 = RANConfig.from_pretrained("my_config")
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Repository layout
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
src/recursive_adaptive_network/
|
|
202
|
+
├── configuration_ran.py RANConfig — hyperparameters only, no layers
|
|
203
|
+
├── modeling_ran.py RANModel, RANForCausalLM — the architecture
|
|
204
|
+
├── modeling_state_engine.py State Engine (recurrent, replaces attention)
|
|
205
|
+
├── modeling_memory.py Adaptive Neural Memory (4 tiers)
|
|
206
|
+
├── modeling_routing.py Importance Gate
|
|
207
|
+
├── modeling_reasoning.py Recursive Reasoning Core (adaptive halting)
|
|
208
|
+
├── modeling_utils.py save_pretrained() / from_pretrained()
|
|
209
|
+
├── tokenization_ran.py Byte-level BPE tokenizer
|
|
210
|
+
├── legacy_format.py backward-compat with the old single-file .ran format
|
|
211
|
+
├── api.py RAN — high-level convenience wrapper
|
|
212
|
+
├── core/ Tensor autograd engine, nn.Module base
|
|
213
|
+
├── data/ Streaming, low-RAM dataset loader
|
|
214
|
+
├── training/ CPU-first Trainer, hardware detection
|
|
215
|
+
├── optimization/ Adam optimizer, loss functions
|
|
216
|
+
├── quantization/ (not yet implemented)
|
|
217
|
+
├── inference/ (not yet implemented beyond RAN.generate())
|
|
218
|
+
├── distributed/ (not yet implemented)
|
|
219
|
+
├── hub/ (not yet implemented)
|
|
220
|
+
└── cli/ `ran` command-line tool
|
|
221
|
+
tests/ 36 automated tests
|
|
222
|
+
benchmarks/ Honest, uncompared performance measurements
|
|
223
|
+
docs/ Architecture notes and roadmap
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Building and publishing this package yourself
|
|
227
|
+
|
|
228
|
+
This repo is ready to build and upload, but actually publishing to
|
|
229
|
+
PyPI requires *your* PyPI account and API token — that step isn't
|
|
230
|
+
something anyone else can do on your behalf.
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
pip install build twine
|
|
234
|
+
python -m build # produces dist/*.whl and dist/*.tar.gz
|
|
235
|
+
python -m twine check dist/* # validate metadata before uploading
|
|
236
|
+
|
|
237
|
+
# test on TestPyPI first (recommended)
|
|
238
|
+
python -m twine upload --repository testpypi dist/*
|
|
239
|
+
pip install --index-url https://test.pypi.org/simple/ recursive-adaptive-network
|
|
240
|
+
|
|
241
|
+
# then the real thing
|
|
242
|
+
python -m twine upload dist/*
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
You'll need a PyPI account (https://pypi.org/account/register/) and an
|
|
246
|
+
API token (Account Settings -> API tokens) — `twine upload` will
|
|
247
|
+
prompt for credentials, or read them from `~/.pypirc`. Before your
|
|
248
|
+
first real upload, double check:
|
|
249
|
+
- the package name `recursive-adaptive-network` is still unclaimed on
|
|
250
|
+
PyPI (verified unclaimed as of this writing, but names can be taken
|
|
251
|
+
at any time)
|
|
252
|
+
- `pyproject.toml`'s `[project.urls]` section — currently placeholder
|
|
253
|
+
GitHub URLs (`REPLACE_ME`) that should point at your actual repo
|
|
254
|
+
before publishing
|
|
255
|
+
- the version number in `pyproject.toml`, since PyPI does not allow
|
|
256
|
+
re-uploading the same version number even if you delete a release
|
|
257
|
+
|
|
258
|
+
## A note on how this was built
|
|
259
|
+
|
|
260
|
+
Every architectural claim in this repo — "gradients flow through the
|
|
261
|
+
memory write gates", "loss decreases during training", "the model
|
|
262
|
+
generalizes across model sizes" — was checked by actually running the
|
|
263
|
+
code, not assumed from the design. Real bugs were caught and fixed
|
|
264
|
+
this way during development: memory writes that silently broke the
|
|
265
|
+
autograd graph, a recursive backward pass that crashed on long
|
|
266
|
+
sequences, and an exploding-gradient issue from an unbounded
|
|
267
|
+
backward-graph across training batches. See `docs/ROADMAP.md` for
|
|
268
|
+
details and `tests/` for the regression tests that now guard against
|
|
269
|
+
them recurring.
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# RAN — Recursive Adaptive Network
|
|
2
|
+
|
|
3
|
+
**"AI pintar tidak boleh hanya bisa dibuat oleh orang yang memiliki GPU mahal."**
|
|
4
|
+
|
|
5
|
+
RAN is a from-scratch neural architecture and training framework built
|
|
6
|
+
as an alternative to the Transformer — no self-attention, no QKV — for
|
|
7
|
+
people whose only computer is a 2-4 core CPU laptop with 4-8GB of RAM
|
|
8
|
+
and no GPU.
|
|
9
|
+
|
|
10
|
+
Published as a single `recursive-adaptive-network` package on PyPI,
|
|
11
|
+
structured the same way `transformers` is: a `configuration_ran.py`
|
|
12
|
+
(hyperparameters only) separated from `modeling_ran.py` (the actual
|
|
13
|
+
architecture), a `RANConfig`/`RANModel` pair, and
|
|
14
|
+
`save_pretrained()`/`from_pretrained()` for a directory-based model
|
|
15
|
+
format.
|
|
16
|
+
|
|
17
|
+
**As of v0.2, this single package also bundles the Agent/Tools/Memory/Skills
|
|
18
|
+
framework** (`recursive_adaptive_network.framework`) that previously
|
|
19
|
+
shipped as a separate `ran-framework` package — `pip install
|
|
20
|
+
recursive-adaptive-network` now gives you both in one install, no
|
|
21
|
+
second `pip install` step needed. A small pretrained demo model is
|
|
22
|
+
bundled too (`RAN.from_pretrained("bundled-base")`), so you can try the
|
|
23
|
+
Agent immediately without training anything yourself first.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install recursive-adaptive-network
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
That's it — the only hard dependency is NumPy. No PyTorch, no CUDA.
|
|
32
|
+
(Until this is actually published to PyPI, see "Building and publishing
|
|
33
|
+
this package yourself" below to build and install it locally.)
|
|
34
|
+
|
|
35
|
+
## Quickstart: the Agent framework
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from recursive_adaptive_network import RAN, Agent
|
|
39
|
+
from recursive_adaptive_network.framework.tools import ToolRegistry
|
|
40
|
+
from recursive_adaptive_network.framework.tools.builtin import ALL_BUILTIN_TOOLS
|
|
41
|
+
from recursive_adaptive_network.framework.skills import SkillRegistry
|
|
42
|
+
from recursive_adaptive_network.framework.skills.builtin import ALL_BUILTIN_SKILLS
|
|
43
|
+
|
|
44
|
+
model = RAN.from_pretrained("bundled-base") # small demo model, bundled in this package
|
|
45
|
+
|
|
46
|
+
tools = ToolRegistry()
|
|
47
|
+
for t in ALL_BUILTIN_TOOLS:
|
|
48
|
+
tools.register(t)
|
|
49
|
+
skills = SkillRegistry()
|
|
50
|
+
for s in ALL_BUILTIN_SKILLS:
|
|
51
|
+
skills.register(s)
|
|
52
|
+
|
|
53
|
+
agent = Agent(model, tools=tools, skills=skills)
|
|
54
|
+
print(agent.respond("halo"))
|
|
55
|
+
print(agent.respond("jam berapa sekarang?"))
|
|
56
|
+
print(agent.respond("hitung 12 * (3 + 4)"))
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Swap `RAN.from_pretrained("bundled-base")` for your own trained model
|
|
60
|
+
(`RAN.from_pretrained("my_model_dir")`, or `RAN(parameters="10m")` +
|
|
61
|
+
`.train(...)`) once you're past trying the framework out — the bundled
|
|
62
|
+
demo model is intentionally small (see docs/BASE_MODEL.md) and not
|
|
63
|
+
meant for real use.
|
|
64
|
+
|
|
65
|
+
## Status: v0.2 (early, honest)
|
|
66
|
+
|
|
67
|
+
This is a real, runnable implementation, not a pseudocode sketch. It
|
|
68
|
+
has been tested end to end: forward pass, backward pass, gradient
|
|
69
|
+
flow through every learnable component, an overfitting sanity check
|
|
70
|
+
(loss goes down), checkpoint save/resume, a `save_pretrained`/
|
|
71
|
+
`from_pretrained` round-trip, and the Agent framework's tools/memory/
|
|
72
|
+
skills wiring. All of that is covered by the automated test suite in
|
|
73
|
+
`tests/` (70 tests, run with `pytest tests/`).
|
|
74
|
+
|
|
75
|
+
**What v0.2 is NOT (yet):**
|
|
76
|
+
- **Benchmarked against a Transformer, and the result favors the
|
|
77
|
+
Transformer at this scale.** `benchmarks/ran_vs_transformer.py`
|
|
78
|
+
compares RAN against a parameter-matched from-scratch Transformer
|
|
79
|
+
decoder (both on the same Tensor/autograd engine, so implementation
|
|
80
|
+
quality isn't a confound). On this sandbox's 1-core CPU: the
|
|
81
|
+
|
|
82
|
+
Transformer is **15-23x faster**, uses **~4x less memory**, and
|
|
83
|
+
reaches **lower training loss** after the same steps on the same
|
|
84
|
+
corpus. Profiling traces the gap to Python-level overhead in RAN's
|
|
85
|
+
per-token loop (hundreds of thousands of function calls for a
|
|
86
|
+
64-token sequence), not to the arithmetic itself — see
|
|
87
|
+
`docs/ROADMAP.md` for the full numbers and what would need to change
|
|
88
|
+
(a compiled/vectorized inner loop) before re-testing this. **No
|
|
89
|
+
"RAN is more efficient than X" claim is made anywhere in this
|
|
90
|
+
repo** — if anything, the one real measurement done so far points
|
|
91
|
+
the other way, at this scale, in this implementation.
|
|
92
|
+
- Not trained on any real corpus at a meaningful scale. The example
|
|
93
|
+
training run in `examples/` uses a few sentences and will not
|
|
94
|
+
produce coherent text — it exists to prove the pipeline works, not
|
|
95
|
+
to demonstrate language ability.
|
|
96
|
+
- No C++ backend yet (pure Python + NumPy). INT8 quantization exists
|
|
97
|
+
for checkpoint-size reduction only (`quantization/int8.py`, ~3.9x
|
|
98
|
+
smaller files, no speed/RAM benefit — see its module docstring for
|
|
99
|
+
why). No distributed CPU training, no Model Hub. See
|
|
100
|
+
`docs/ROADMAP.md` for what's built vs. planned, mapped to the
|
|
101
|
+
original spec.
|
|
102
|
+
- No PyTorch/CUDA dependency at all in v0.1 — every learnable op runs
|
|
103
|
+
on a small NumPy-based autograd engine (`core/tensor.py`), which is
|
|
104
|
+
actually a feature here: the "runs without a GPU" promise holds
|
|
105
|
+
from the very first line of code, on machines where even installing
|
|
106
|
+
a GPU-capable PyTorch wheel is impractical.
|
|
107
|
+
|
|
108
|
+
## Why not just run a Transformer on CPU?
|
|
109
|
+
|
|
110
|
+
Self-attention is O(n²) in sequence length. RAN instead maintains a
|
|
111
|
+
fixed-size recurrent **State Engine** (O(1) per token), a **learned,
|
|
112
|
+
multi-tier Adaptive Neural Memory** instead of a growing KV-cache, an
|
|
113
|
+
**Importance Gate** that decides how much compute/memory a piece of
|
|
114
|
+
information deserves, and a **Recursive Reasoning Core** with adaptive
|
|
115
|
+
halting so a simple prompt costs less compute than a complex one.
|
|
116
|
+
None of these use QKV attention. See `docs/ARCHITECTURE.md` for the
|
|
117
|
+
full pipeline and the reasoning behind each piece.
|
|
118
|
+
|
|
119
|
+
## Quickstart
|
|
120
|
+
|
|
121
|
+
### Command line
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
ran doctor # check what your hardware can handle
|
|
125
|
+
ran init my_project && cd my_project
|
|
126
|
+
|
|
127
|
+
ran tokenizer train --data data/corpus.txt --vocab-size 4096 --out tokenizer/tokenizer.json
|
|
128
|
+
ran model create --parameters 10m --tokenizer tokenizer/tokenizer.json --out model
|
|
129
|
+
ran train --model model --data data/corpus.txt --steps 500
|
|
130
|
+
ran run --model model --prompt "Halo dunia"
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### High-level Python API
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from recursive_adaptive_network import RAN
|
|
137
|
+
|
|
138
|
+
model = RAN(parameters="10m")
|
|
139
|
+
model.train_tokenizer(open("corpus.txt").read())
|
|
140
|
+
model.train(dataset_path="corpus.txt", steps=500)
|
|
141
|
+
model.save_pretrained("my_model")
|
|
142
|
+
|
|
143
|
+
model2 = RAN.from_pretrained("my_model")
|
|
144
|
+
print(model2.generate("Halo dunia"))
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Direct architecture access (HuggingFace-style)
|
|
148
|
+
|
|
149
|
+
For custom training loops, research, or composing RAN's components
|
|
150
|
+
into something else — config and model are separate classes, the same
|
|
151
|
+
way `AutoConfig`/`AutoModel` are in `transformers`:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
from recursive_adaptive_network import RANConfig, RANModel
|
|
155
|
+
import numpy as np
|
|
156
|
+
|
|
157
|
+
config = RANConfig(vocab_size=4096, hidden_dim=128, state_dim=128,
|
|
158
|
+
memory_slot_dim=64, max_reasoning_steps=4)
|
|
159
|
+
model = RANModel(config)
|
|
160
|
+
|
|
161
|
+
tokens = np.random.randint(0, 4096, size=(2, 16)) # (batch, seq_len)
|
|
162
|
+
logits_list, final_state, info = model.forward_sequence(tokens)
|
|
163
|
+
|
|
164
|
+
model.config.save_pretrained("my_config") # config.json only
|
|
165
|
+
config2 = RANConfig.from_pretrained("my_config")
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Repository layout
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
src/recursive_adaptive_network/
|
|
172
|
+
├── configuration_ran.py RANConfig — hyperparameters only, no layers
|
|
173
|
+
├── modeling_ran.py RANModel, RANForCausalLM — the architecture
|
|
174
|
+
├── modeling_state_engine.py State Engine (recurrent, replaces attention)
|
|
175
|
+
├── modeling_memory.py Adaptive Neural Memory (4 tiers)
|
|
176
|
+
├── modeling_routing.py Importance Gate
|
|
177
|
+
├── modeling_reasoning.py Recursive Reasoning Core (adaptive halting)
|
|
178
|
+
├── modeling_utils.py save_pretrained() / from_pretrained()
|
|
179
|
+
├── tokenization_ran.py Byte-level BPE tokenizer
|
|
180
|
+
├── legacy_format.py backward-compat with the old single-file .ran format
|
|
181
|
+
├── api.py RAN — high-level convenience wrapper
|
|
182
|
+
├── core/ Tensor autograd engine, nn.Module base
|
|
183
|
+
├── data/ Streaming, low-RAM dataset loader
|
|
184
|
+
├── training/ CPU-first Trainer, hardware detection
|
|
185
|
+
├── optimization/ Adam optimizer, loss functions
|
|
186
|
+
├── quantization/ (not yet implemented)
|
|
187
|
+
├── inference/ (not yet implemented beyond RAN.generate())
|
|
188
|
+
├── distributed/ (not yet implemented)
|
|
189
|
+
├── hub/ (not yet implemented)
|
|
190
|
+
└── cli/ `ran` command-line tool
|
|
191
|
+
tests/ 36 automated tests
|
|
192
|
+
benchmarks/ Honest, uncompared performance measurements
|
|
193
|
+
docs/ Architecture notes and roadmap
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Building and publishing this package yourself
|
|
197
|
+
|
|
198
|
+
This repo is ready to build and upload, but actually publishing to
|
|
199
|
+
PyPI requires *your* PyPI account and API token — that step isn't
|
|
200
|
+
something anyone else can do on your behalf.
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
pip install build twine
|
|
204
|
+
python -m build # produces dist/*.whl and dist/*.tar.gz
|
|
205
|
+
python -m twine check dist/* # validate metadata before uploading
|
|
206
|
+
|
|
207
|
+
# test on TestPyPI first (recommended)
|
|
208
|
+
python -m twine upload --repository testpypi dist/*
|
|
209
|
+
pip install --index-url https://test.pypi.org/simple/ recursive-adaptive-network
|
|
210
|
+
|
|
211
|
+
# then the real thing
|
|
212
|
+
python -m twine upload dist/*
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
You'll need a PyPI account (https://pypi.org/account/register/) and an
|
|
216
|
+
API token (Account Settings -> API tokens) — `twine upload` will
|
|
217
|
+
prompt for credentials, or read them from `~/.pypirc`. Before your
|
|
218
|
+
first real upload, double check:
|
|
219
|
+
- the package name `recursive-adaptive-network` is still unclaimed on
|
|
220
|
+
PyPI (verified unclaimed as of this writing, but names can be taken
|
|
221
|
+
at any time)
|
|
222
|
+
- `pyproject.toml`'s `[project.urls]` section — currently placeholder
|
|
223
|
+
GitHub URLs (`REPLACE_ME`) that should point at your actual repo
|
|
224
|
+
before publishing
|
|
225
|
+
- the version number in `pyproject.toml`, since PyPI does not allow
|
|
226
|
+
re-uploading the same version number even if you delete a release
|
|
227
|
+
|
|
228
|
+
## A note on how this was built
|
|
229
|
+
|
|
230
|
+
Every architectural claim in this repo — "gradients flow through the
|
|
231
|
+
memory write gates", "loss decreases during training", "the model
|
|
232
|
+
generalizes across model sizes" — was checked by actually running the
|
|
233
|
+
code, not assumed from the design. Real bugs were caught and fixed
|
|
234
|
+
this way during development: memory writes that silently broke the
|
|
235
|
+
autograd graph, a recursive backward pass that crashed on long
|
|
236
|
+
sequences, and an exploding-gradient issue from an unbounded
|
|
237
|
+
backward-graph across training batches. See `docs/ROADMAP.md` for
|
|
238
|
+
details and `tests/` for the regression tests that now guard against
|
|
239
|
+
them recurring.
|