Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,17 @@ jobs:
id: checkout
uses: actions/checkout@v4

- name: CPU fingerprint
id: cpu
# GGML_NATIVE (the default) builds with -march=native, and ccache 4.5
# hashes that flag literally, so key the cache on the runner's CPU
# features to never reuse objects built for a different CPU
run: echo "fingerprint=$(grep -E '^(model name|flags|CPU part|Features)' /proc/cpuinfo | sort -u | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT"

- name: ccache
uses: ggml-org/ccache-action@v1.2.16
with:
key: ubuntu-24-cmake-vulkan
key: ubuntu-24-cmake-vulkan-${{ steps.cpu.outputs.fingerprint }}
evict-old-files: 1d

- name: Dependencies
Expand Down Expand Up @@ -456,10 +463,17 @@ jobs:
id: checkout
uses: actions/checkout@v4

- name: CPU fingerprint
id: cpu
# GGML_NATIVE (the default) builds with -march=native, and ccache 4.5
# hashes that flag literally, so key the cache on the runner's CPU
# features to never reuse objects built for a different CPU
run: echo "fingerprint=$(grep -E '^(model name|flags|CPU part|Features)' /proc/cpuinfo | sort -u | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT"

- name: ccache
uses: ggml-org/ccache-action@v1.2.16
with:
key: ubuntu-22-cmake-webgpu
key: ubuntu-22-cmake-webgpu-${{ steps.cpu.outputs.fingerprint }}
evict-old-files: 1d

- name: Vulkan SDK Dependencies
Expand Down
19 changes: 13 additions & 6 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ x(t+1) = (1-α) * x(t) + α * tanh(W_res * x(t) + W_in * u(t))
- `src/llama-hparams.h` - ESN hyperparameters (reservoir_size, spectral_radius, etc.)
- `src/llama-model.cpp` - Model loading and forward pass (`llm_build_esn`)
- `docs/esn.md` - Comprehensive ESN documentation
- `src/llama-esn.{h,cpp}` - Online readout adaptation (`llama_esn_*` API)
- `tests/test-esn.cpp` - ESN architecture tests
- `tests/test-esn-online.cpp` - Online adaptation tests (ridge/RLS/SGD, reset)
- `scripts/esn_training.py` - Python utility for ESN training and GGUF creation

### ESN Hyperparameters
Expand All @@ -37,7 +39,8 @@ x(t+1) = (1-α) * x(t) + α * tanh(W_res * x(t) + W_in * u(t))
| `esn_feedback_scaling` | 0.0 | Output-to-reservoir feedback (0 = disabled) |
| `esn_noise_level` | 0.0 | Noise injection for regularization |
| `esn_activation_type` | 0 | Activation: 0=tanh, 1=sigmoid, 2=leaky_relu |
| `esn_bidirectional` | false | Not supported (inference is forward-only) |
| `esn_bidirectional` | false | Not supported; the loader refuses it |
| `esn_output_temperature` | 0 | Scale W_out was fit to (T·one-hot); 0 = not recorded |

### Tensor Layout

Expand Down Expand Up @@ -103,16 +106,16 @@ assert(esn_arch == LLM_ARCH_ESN);

// Test tensor info
const llm_tensor_info & info = llm_tensor_info_for(LLM_TENSOR_ESN_INPUT_WEIGHTS);
assert(info.layer == LLM_TENSOR_LAYER_INPUT);
assert(info.layer == LLM_TENSOR_LAYER_REPEATING);
```

## Training ESN Models

Use the Python training utility:

```bash
# Basic training
python scripts/esn_training.py --reservoir-size 1024 --output model.gguf
# Basic training (--train-data is required unless --init-only)
python scripts/esn_training.py --reservoir-size 1024 --train-data corpus.txt --output model.gguf

# With custom config
python scripts/esn_training.py --config config.json
Expand All @@ -131,6 +134,10 @@ python scripts/esn_training.py --reservoir-size 8192 --reg-grid 1,10,100,1000 \
of computing W_in·E + biases + feedback. The CPU `tanh`/`sigmoid` are
branch-free so they vectorize (`tests/test-esn-activations.cpp` checks them).

`--save-online-metadata` enables the runtime `llama_esn_*` online readout
adaptation for the exported model; least-squares readouts also record
`esn.output_temperature`, which the C++ ridge/RLS updates need.

Batch training holds out `--val-fraction` (default 10%) of the sequences to
pick λ from `--reg-grid` and to fit a softmax temperature that is folded into
`W_out` (disable with `--no-calibrate`).
Expand Down Expand Up @@ -164,8 +171,8 @@ ESN is recognized as recurrent via `llm_arch_is_recurrent()`. To add similar:

Current limitations and planned improvements:

1. **Training Support** - Ridge regression training (see `scripts/esn_training.py`)
2. **Hierarchical ESNs** - Multi-layer reservoir architectures
1. **Infer-train loop in CLI/server** - The `llama_esn_*` API exists; no tool drives it yet
2. **Hierarchical ESNs** - Multi-layer reservoir architectures (keys reserved; refused at load)
3. **Adaptive Parameters** - Dynamic leaking rate and spectral radius
4. **Sparse Operations** - Optimized sparse matrix ops for reservoir

Expand Down
29 changes: 15 additions & 14 deletions docs/esllm-roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,26 @@ Proprioceptive signals are routed through the `llama_esn_feedback_event` API and
| **Bounded latency** | Online train step is O(R²) and must complete in < 1 ms for R ≤ 4096 on CPU |
| **Stable reservoir dynamics** | Spectral radius is validated at load time to be in (0, 1) |
| **Non-destructive continual updates** | Only `W_out` is adapted; `W_res`, `W_in` are read-only after model load |
| **Reproducible checkpoints** | State + readout weights can be serialised via `llama_state_*` APIs at any point |
| **Safe online updates** | All updates are bounded by L2 regularisation; a drift detector can trigger rollback |
| **Reversible updates** | The model's `W_out` is never modified; `llama_esn_reset_adaptation` returns a context to it (the adapted readout is not yet part of `llama_state_*` data) |
| **Regularized online updates** | Ridge and RLS updates are anchored to the checkpoint by λ‖W − W₀‖²; drift detection is Phase 8 |

### 1.3 ESN/ESLLM Capability Matrix

| Capability | Current Status | Target (ESLLM) |
|-----------|---------------|----------------|
| Single-layer ESN inference | ✅ Implemented | ✅ Keep |
| Batch ridge regression training | ✅ Python utility | ✅ Keep |
| Online RLS adaptation | ✅ Python utility | ✅ Runtime (C++) |
| Online SGD adaptation | ✅ Python utility | ✅ Runtime (C++) |
| Online ridge / RLS / SGD readout adaptation | ✅ Python utility and C++ runtime (`llama_esn_update_readout`) | ✅ Keep |
| Parameter validation at load | ✅ Implemented | ✅ Keep |
| Proprioceptive feedback API | ✅ Header-defined stubs | 🔄 Full implementation |
| Synchronous infer-train loop | ❌ Not yet | 🎯 Phase 4 |
| Hierarchical reservoir (DTE) | ❌ Config only | 🎯 Phase 6 |
| Proprioceptive feedback API | ✅ Implemented (`src/llama-esn.cpp`, `tests/test-esn-online.cpp`) | ✅ Keep |
| Synchronous infer-train loop in CLI/server | ❌ Not yet (the API supports it) | 🎯 Phase 4 |
| Hierarchical reservoir (DTE) | ❌ Keys reserved; models that set them are refused at load | 🎯 Phase 6 |
| ESN–Transformer hybrid | ❌ Not yet | 🎯 Phase 7 |
| Drift detection + rollback | ❌ Not yet | 🎯 Phase 8 |
| Online learning GGUF metadata | ✅ Implemented | ✅ Keep |
| Safety/policy gates | ❌ Not yet | 🎯 Phase 8 |
| Research benchmark suite | ❌ Not yet | 🎯 Phase 9 |
| Full API surface in llama.h | ✅ Stubs implemented | 🔄 Phase 10 |
| Full API surface in llama.h | ✅ Implemented for online adaptation | 🔄 Phase 10 |

---

Expand Down Expand Up @@ -129,11 +128,11 @@ All of the following are loaded from GGUF metadata and fall back to safe default

```bash
# RLS online training (efficient for sequential adaptation)
python scripts/esn_training.py --reservoir-size 1024 --online --online-mode rls \
python scripts/esn_training.py --reservoir-size 1024 --train-data corpus.txt --online --online-mode rls \
--forgetting-factor 0.999 --save-online-metadata --output esllm.gguf

# SGD online training (standard gradient descent)
python scripts/esn_training.py --reservoir-size 1024 --online --online-mode sgd \
python scripts/esn_training.py --reservoir-size 1024 --train-data corpus.txt --online --online-mode sgd \
--online-lr 1e-4 --weight-decay 1e-5 --output esllm_sgd.gguf
```

Expand All @@ -143,9 +142,11 @@ Key classes:
- `ESN.sgd_update(state, target)` — one online SGD step (O(V·R))
- `ESN.online_train_sequence(...)` — process a complete token sequence online

### 3.3 C++ API Stubs
### 3.3 C++ API

Declared in `include/llama.h`:
Declared in `include/llama.h`, implemented in `src/llama-esn.cpp` (see
`docs/esn.md`, "Online readout adaptation"), tested by
`tests/test-esn-online.cpp`:

```c
struct llama_esn_online_params { ... };
Expand Down Expand Up @@ -334,8 +335,8 @@ When drift exceeds threshold, the monitor:
### 8.2 Rollback Checkpoints

- A "golden" checkpoint (the static GGUF weights) is always held in memory.
- `llama_esn_reset_adaptation()` restores `W_out` to the golden checkpoint weights instantly.
- Periodic automatic checkpoints can be saved with `llama_state_save_file()`.
- `llama_esn_reset_adaptation()` returns a context to the golden checkpoint `W_out` (implemented).
- Periodic checkpoints of the adapted readout need it in `llama_state_*` data (not yet).

### 8.3 Policy Controls

Expand Down
3 changes: 2 additions & 1 deletion docs/esn-completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ sign off on criteria 1–4, 6, 7 by inspection of the file set; criterion

| Requirement | Satisfied by | Evidence |
|---|---|---|
| Hyperparameters load from GGUF | `src/llama-model.cpp` (`llm_load_hparams` ESN branch) | reservoir size, spectral radius, sparsity, leaking rate, input scaling, feedback scaling, noise, activation, bidirectional |
| Hyperparameters load from GGUF | `src/llama-model.cpp` (`llm_load_hparams` ESN branch) | reservoir size, spectral radius, sparsity, leaking rate, input scaling, feedback scaling, noise, activation, output temperature, online learning; bidirectional and hierarchical settings are refused |
| Tensors load with correct shapes | `src/llama-model.cpp` (`llm_load_tensors` ESN branch) | `ggml_mul_mat` semantics respected — `ne[0]` is the contracted axis for each weight |
| Recurrent memory sized correctly | `src/llama-hparams.cpp` | `n_embd_s()` returns `esn_reservoir_size` for `LLM_ARCH_ESN` |

Expand Down Expand Up @@ -140,6 +140,7 @@ and Inference Optimizations" in `docs/esn.md`.
| Arch registry | `tests/test-esn.cpp` (CTest, 8 asserts) |
| C API end-to-end | `tests/test-esn-inference.cpp` (build-only; takes a model path) |
| Numerical correctness vs. baseline | `tests/test-esn-perplexity.cpp` (build-only; takes a model + text) |
| Online readout adaptation (`llama_esn_*`) | `tests/test-esn-online.cpp` (CTest; writes its own models, or takes one) |

**Verified by**: `ctest -R test-esn --verbose` passes; both build-only tests pass on the markdown-trained model.

Expand Down
Loading
Loading