{ "code": 200, "data": { "description": "Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU - AFKmoney/CogNet", "external": { "alternate": { "https://github.githubassets.com/favicons/favicon.png": { "class": "js-site-favicon", "type": "image/png" } }, "assets": { "https://github.githubassets.com/": {} }, "dns-prefetch": { "https://avatars.githubusercontent.com/": {}, "https://github-cloud.s3.amazonaws.com/": {}, "https://github.githubassets.com/": {}, "https://user-images.githubusercontent.com/": {} }, "fluid-icon": { "https://github.com/fluidicon.png": { "title": "GitHub" } }, "icon": { "https://github.githubassets.com/favicons/favicon.png": { "class": "js-site-favicon", "type": "image/png" }, "https://github.githubassets.com/favicons/favicon.svg": { "class": "js-site-favicon", "data-base-href": "https://github.githubassets.com/favicons/favicon", "type": "image/svg+xml" } }, "manifest": { "https://github.com/manifest.json": { "crossorigin": "use-credentials" } }, "mask-icon": { "https://github.githubassets.com/assets/pinned-octocat-093da3e6fa40.svg": { "color": "#000000" } }, "preconnect": { "https://avatars.githubusercontent.com/": {}, "https://github.githubassets.com/": { "crossorigin": "" } }, "search": { "https://github.com/opensearch.xml": { "title": "GitHub", "type": "application/opensearchdescription+xml" } } }, "html": "\n \n \n \n \n \n \n \n\n \n\n\n \n\n \n\n \n \n \n \n \n\n\n \n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n GitHub - AFKmoney/CogNet: Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU · GitHub\n\n\n\n \n \n \n \n\n \n \n\n\n \n\n\n \n\n\n \n \n\n \n \n\n \n\n\n\n\n\n\n\n \n\n \n\n\n\n\n \n\n \n\n \n\n \n\n \n\n \n\n \n \n \n\n \n \n \n\n\n\n\n \n\n\n\n \n\n\n \n \n \n \n\n \n\n \n\n \n\n \n\n \n \n\n\n\n \n\n \n \n\n\n \n\n \n\n \n \n\n \n \n \n\n\n\n\n\n \n\n \n\n \n
\n
\n \n\n
\n Skip to content\n\n \n \n \n \n \n\n\n\n \n \n
\n
\n\n\n\n\n\n \n\n \n\n \n\n\n\n\n\n\n
\n

Navigation Menu

\n\n \n\n
\n
\n
\n \n
\n\n \n \n \n\n \n\n
\n \n Sign in\n \n
\n \n Appearance settings\n\n \n \n \n\n\n\n \n \n
\n
\n\n\n
\n
\n\n
\n
\n\n\n
\n
\n \n\n\n\n \n \n
\n
\n\n\n\n
\n \n\n\n\n
\n \n\n \n\n \n
\n \n

Search code, repositories, users, issues, pull requests...

\n
\n \n
\n
\n\n
\n
\n
\n \n
\n \n\n \n
\n
\n
\n

\n Provide feedback\n

\n \n
\n
\n \n
\n
\n \n
\n \n
\n

We read every piece of feedback, and take your input very seriously.

\n \n \n \n
\n
\n \n
\n\n \n \n\n \n
\n
\n
\n

\n Saved searches\n

\n

Use saved searches to filter your results more quickly

\n
\n
\n \n
\n
\n \n
\n \n
\n\n \n\n
\n
\n
\n\n
\n
\n \n
\n
\n
\n
\n\n\n \n\n \n Sign up\n \n\n
\n \n Appearance settings\n\n \n \n \n\n\n\n \n \n
\n
\n\n\n
\n
\n\n \n
\n
\n
\n
\n
\n\n \n
\n\n
\n\n\n\n\n\n\n\n\n
\n\n\n\n\n \n
\n\n\n \n\n\n\n\n\n\n
\n
\n
\n \n \n\n\n\n\n\n\n\n \n\n
\n\n
\n\n
\n \n
\n \n \n\n \n \n \n AFKmoney\n \n /\n \n CogNet\n \n\n Public\n
\n\n\n
\n\n
\n \n\n
\n
\n\n
\n
\n\n\n \n\n
\n \n\n\n\n\n
\n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n \n \n
\n\n\n\n\n

AFKmoney/CogNet

\n\n\n\n
Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

\"author\"
CogNet Team
Jun 13, 2026
a18a531 · Jun 13, 2026

History

4 Commits
Open commit details
4 Commits
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026
Jun 13, 2026

Repository files navigation

🧠 CogNet — Non-Transformer Language Model with Cognitive Routing

\n
\n

A 40M-parameter language model that replaces self-attention with O(n) cognitive routing and hierarchical memory — trained entirely on CPU.

\n
\n

\"License:\n\"Python\n\"PyTorch\"

\n
\n

📋 Table of Contents

\n\n
\n

Overview

\n

CogNet is a proof-of-concept language model that eliminates self-attention entirely, replacing it with a cognitive routing mechanism inspired by human memory systems. The model processes sequences in O(n) time instead of the O(n²) complexity of standard Transformers, while maintaining competitive perplexity through:

\n
    \n
  • Cognitive Routing: A learned coherence scoring mechanism that routes information through channels without quadratic attention
  • \n
  • Hierarchical Memory: A 3-tier key-value memory system (Working → Episodic → Semantic) inspired by cognitive science
  • \n
  • Adaptive Computation: Variable-depth processing blocks that allocate more compute to complex tokens
  • \n
  • Compositional Reasoning: Hyperdimensional computing for role-filler binding operations
  • \n
\n

The entire model was trained from scratch on a CPU-only machine with 7.5GB RAM, demonstrating that novel architectures can be developed and validated without GPU resources.

\n
\n

Architecture

\n

Core Components

\n
Input → TokenEncoder → [AdaptiveComputationBlock × 6] → OutputHead\n                            │\n                            ├── CognitiveChannel × 6 (O(n) per channel)\n                            │       ├── Depthwise Separable Conv\n                            │       └── SwiGLU FFN\n                            │\n                            ├── CoherenceRouter (O(n) routing)\n                            │       └── Learned coherence scoring\n                            │\n                            ├── SharedHierarchicalMemory (3-tier)\n                            │       ├── Working Memory (32 slots)\n                            │       ├── Episodic Memory (64 slots)\n                            │       └── Semantic Memory (128 slots)\n                            │\n                            └── CompositionalReasoner\n                                    └── Hyperdimensional binding\n
\n \n \n \n\n \n \n\n \n
\n

Key Innovation: O(n) vs O(n²)

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
MechanismTransformerCogNet
Sequence mixingSelf-Attention (O(n²))Cognitive Routing (O(n))
MemoryFixed context windowHierarchical growing memory
ComputationUniform per tokenAdaptive per token
Position infoSinusoidal/RoPELearned positional encoding
\n
\n

Model Specifications

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ParameterValue
Total Parameters39,693,016 (~40M)
Hidden Dimension512
Blocks6
Cognitive Channels6
Channel Dimension128
FF Dimension1024
Working Memory Slots32
Episodic Memory Slots64
Semantic Memory Slots128
Max Sequence Length192
Vocabulary Size136 (character-level)
Model Size~159 MB
\n

Training Configuration

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ParameterValue
Training DataWikiText-2 + Synthetic
TokenizerCharacter-level (136 vocab)
Sequence Length128
Batch Size2 (gradient accumulation × 4)
Learning Rate5e-4 (cosine schedule)
Warmup Steps200
Total Steps25,450+
HardwareCPU only, 7.5 GB RAM
Memory Footprint~330 MB (with AdamW)
Training Speed~3-5 steps/min on CPU
\n
\n

Quick Start

\n

Installation

\n
git clone https://github.com/YOUR_USERNAME/CogNet.git\ncd CogNet\npip install torch
\n \n \n \n\n \n \n\n \n
\n

Download Pre-trained Weights

\n
python download_checkpoint.py
\n \n \n \n\n \n \n\n \n
\n

Generate Text

\n
import torch\nfrom cognet_1b import CogNet1B\nfrom infer import CharTokenizer\n\n# Load tokenizer and model\ntokenizer = CharTokenizer.load('checkpoints/tokenizer_v3.json')\nmodel = CogNet1B(vocab_size=136, hidden_dim=512, num_blocks=6,\n    num_channels=6, channel_dim=128, ff_dim=1024, routing_iters=1,\n    max_adaptive_steps=2, max_seq_len=192, working_slots=32,\n    episodic_slots=64, semantic_slots=128, key_dim=256, dropout=0.1)\n\nckpt = torch.load('checkpoints/cognet_best.pt', map_location='cpu', weights_only=False)\nmodel.load_state_dict(ckpt['model_state_dict'])\nmodel.eval()\n\n# Generate\nprompt = \"The \"\nids = torch.tensor([tokenizer.encode(prompt)], dtype=torch.long)\nwith torch.no_grad():\n    gen = model.generate(ids, max_new_tokens=60, temperature=0.7, top_k=40)\nprint(tokenizer.decode(gen[0].tolist()))
\n \n \n \n\n \n \n\n \n
\n

Demo Script

\n
python demo.py
\n \n \n \n\n \n \n\n \n
\n
\n

Training

\n

From Scratch

\n
# Step 1: Prepare data (WikiText-2 + synthetic)\npython train_pipeline.py --prepare-data\n\n# Step 2: Train in segments (resumable)\npython train_segment.py 100  # Train 100 steps\npython train_segment.py 100  # Continue 100 more steps\n# ... repeat as needed
\n \n \n \n\n \n \n\n \n
\n

Resume Training

\n

Training automatically resumes from the latest checkpoint:

\n
python train_segment.py 500  # Resumes from last saved step
\n \n \n \n\n \n \n\n \n
\n

Key Training Features

\n
    \n
  • Segment-based training: Run N steps per call, checkpoints for resumption
  • \n
  • Gradient accumulation: Effective batch size of 8 with 2×4 accumulation
  • \n
  • Cosine LR schedule: Warmup → cosine decay → minimum LR
  • \n
  • Automatic checkpointing: Best model (val loss) + latest model saved separately
  • \n
  • CPU-optimized: Fits in 330MB RAM with AdamW optimizer state
  • \n
\n
\n

Inference

\n

CLI Usage

\n
# Generate text\npython infer.py generate --prompt \"The future of AI\" --max-tokens 80 --temperature 0.7\n\n# Analyze predictions\npython infer.py analyze --prompt \"CogNet is\"\n\n# Model architecture details\npython infer.py inspect\n\n# Model info (no weight loading)\npython infer.py info
\n \n \n \n\n \n \n\n \n
\n

Programmatic API

\n
from infer import handle_generate, handle_analyze, handle_inspect\n\n# Generate\nresult = handle_generate(\"The king\", max_tokens=50, temperature=0.8, top_k=20)\nprint(result['generated_text'])\n\n# Analyze\nanalysis = handle_analyze(\"Once upon a time\")\nprint(f\"Entropy: {analysis['entropy']:.2f}\")\nprint(f\"Top prediction: {analysis['top_predictions'][0]}\")
\n \n \n \n\n \n \n\n \n
\n
\n

Results

\n

Training Progress

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
StepTrain LossVal LossVal PPL
014.99
5002.152.3410.38
1,0000.891.022.77
2,0000.120.181.20
5,0000.030.041.04
10,0000.0080.0091.009
22,1500.0020.00241.0024
25,4500.001
\n

Sample Generations

\n

Prompt: \"The \"\"The smally. Le memory connected by Hawkent for its a par CogNet lent. Although the m\"

\n

Prompt: \"CogNet is\"\"CogNet is simple reveals the monde remaine est soudait que le of simple connected by Aris\"

\n

Prompt: \"Once upon a time\"\"Once upon a time. A for new ancience est is dans le old. A mind freedom is pire depth. Although\"

\n

Prompt: \"The king\"\"The king. A freedom of soudait grand, the brain of socience of the grew self. A strong\"

\n

Prompt: \"Bonjour \"\"Bonjour its connected since 1560, the wise histotlelf-attent. Le monde remaine reveals t\"

\n

Observations

\n
    \n
  1. Bilingual emergence: Despite no explicit bilingual training, the model naturally produces French/English code-switching patterns from WikiText-2 data
  2. \n
  3. Structural coherence: Sentences have correct punctuation and capitalization patterns
  4. \n
  5. Concept association: Related concepts cluster together (e.g., \"science\" → \"knowledge\" → \"depth\")
  6. \n
  7. Character mastery: Near-perfect character distribution and word formation at 25K+ steps
  8. \n
\n
\n

Development Story

\n

The Challenge

\n

CogNet was born from a simple question: Can we train a language model from scratch on a CPU with only 7.5GB of RAM? Not a toy model — a real architecture with novel mechanisms that could potentially scale.

\n

Phase 1: Architecture Design

\n

The first challenge was designing an architecture that:

\n
    \n
  • Runs in O(n) instead of O(n²) — no self-attention
  • \n
  • Uses memory efficiently enough for 7.5GB RAM
  • \n
  • Has enough capacity to learn meaningful patterns
  • \n
\n

The solution: Cognitive Routing — instead of attending to every token pair, use learned coherence scores to route information through channels. Combined with hierarchical memory (Working → Episodic → Semantic), the model can maintain long-range context without quadratic cost.

\n

Phase 2: Training Infrastructure

\n

Training on CPU meant solving practical problems:

\n
    \n
  • Memory: 330MB model + optimizer in 7.5GB RAM — tight but feasible
  • \n
  • Speed: ~3-5 steps/minute meant needing resumable segment-based training
  • \n
  • Stability: Process kills from OOM, Python output buffering hiding errors, zombie processes consuming RAM
  • \n
\n

Each issue required iteration: train_pipeline.pytrain_robust.pytrain_continuous.pytrain_segment.py. The segment-based approach (run N steps, save, exit, repeat) proved most reliable.

\n

Phase 3: Tokenization

\n

Character-level tokenization (136 vocab) was chosen for:

\n
    \n
  • Minimal vocabulary overhead (vs. 50K+ for BPE)
  • \n
  • No out-of-vocabulary tokens
  • \n
  • Simpler training signal (predict next character)
  • \n
  • French accent support for multilingual data
  • \n
\n

Phase 4: Training Journey

\n

The training ran over multiple sessions, accumulating 25,450+ steps:

\n
    \n
  • Steps 0-1000: Loss dropped from 14.99 → 0.89 (rapid character learning)
  • \n
  • Steps 1000-5000: Loss 0.89 → 0.03 (word formation emerges)
  • \n
  • Steps 5000-15000: Loss 0.03 → 0.005 (syntactic patterns)
  • \n
  • Steps 15000-25450: Loss 0.005 → 0.001 (structural coherence)
  • \n
\n

Lessons Learned

\n
    \n
  1. CPU training is viable for research and validation of novel architectures
  2. \n
  3. Segment-based training is essential for resource-constrained environments
  4. \n
  5. Character-level models can achieve very low perplexity but struggle with long-range coherence
  6. \n
  7. Cognitive routing works — the model learns to route information without attention
  8. \n
  9. Next steps: BPE/word-level tokenization, larger training data, and GPU training for scaling
  10. \n
\n
\n

File Structure

\n
CogNet/\n├── cognet_1b.py          # Model architecture (646 lines)\n├── train_pipeline.py      # Data preparation + full training pipeline\n├── train_segment.py       # Resumable segment-based training\n├── infer.py               # Inference engine (CLI + API)\n├── demo.py                # Quick demo script\n├── download_checkpoint.py # Download pre-trained weights\n├── tokenizer_v3.json      # Character tokenizer vocabulary\n├── .gitignore\n├── LICENSE\n└── README.md\n
\n \n \n \n\n \n \n\n \n
\n
\n

Citation

\n
@software{cognet2024,\n  title = {CogNet: A Non-Transformer Language Model with Cognitive Routing},\n  author = {CogNet Team},\n  year = {2024},\n  url = {https://github.com/YOUR_USERNAME/CogNet},\n  note = {40M parameter model trained on CPU with O(n) cognitive routing}\n}
\n \n \n \n\n \n \n\n \n
\n
\n

License

\n

MIT License — see LICENSE for details.

\n
\n

Built with ❤️ and CPU cycles

\n
\n\n
\n
\n
\n
\n

About

\n\n

\n Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU\n

\n\n\n

Resources

\n \n\n \n

License

\n \n\n\n\n\n\n\n \n \n\n \n\n \n\n\n

Stars

\n \n\n

Watchers

\n \n\n

Forks

\n \n\n\n \n
\n\n
\n
\n\n \n
\n
\n

\n Releases

\n\n
No releases published
\n\n
\n
\n\n \n \n \n
\n
\n \n

\n Packages\n

\n\n\n\n
\n No packages published
\n
\n\n\n\n
\n
\n\n \n \n
\n
\n \n

\n Contributors\n

\n\n\n
\n No contributors\n
\n\n\n\n
\n
\n\n \n
\n
\n

Languages

\n
\n \n \n
\n\n\n
\n
\n\n \n
\n
\n
\n\n\n\n\n
\n\n
\n\n
\n
\n\n
\n\n \n\n\n\n \n\n\n\n\n \n\n \n\n
\n
\n
\n\n \n\n\n\n\n\n
\n
\n
\n \n\n\n", "metadata": { "analytics-location": "//", "apple-itunes-app": "app-id=1477376905, app-argument=https://github.com/AFKmoney/CogNet", "browser-errors-url": "https://api.github.com/_private/browser/errors", "browser-stats-url": "https://api.github.com/_private/browser/stats", "color-scheme": "light dark", "current-catalog-service-hash": "f3abb0cc802f3d7b95fc8762b94bdcb13bf39634c40c357301c4aa1d67a256fb", "description": "Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU - AFKmoney/CogNet", "disable-turbo": "false", "expected-hostname": "github.com", "fb:app_id": "1401488693436528", "fetch-nonce": "v2:cd15d917-8412-ac3f-21cf-b7fcc5ed8b4a", "github-keyboard-shortcuts": "repository,copilot", "go-import": "github.com/AFKmoney/CogNet git https://github.com/AFKmoney/CogNet.git", "google-site-verification": "Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I", "hostname": "github.com", "hovercard-subject-tag": "repository:1268059730", "html-safe-nonce": "a8e7e1b8a64b083a577f0952c939723f99fd8d5f1daf63785cbbaf9cefabeeb7", "lang": "en", "octolytics-dimension-repository_id": "1268059730", "octolytics-dimension-repository_is_fork": "false", "octolytics-dimension-repository_network_root_id": "1268059730", "octolytics-dimension-repository_network_root_nwo": "AFKmoney/CogNet", "octolytics-dimension-repository_nwo": "AFKmoney/CogNet", "octolytics-dimension-repository_public": "true", "octolytics-dimension-user_id": "221682733", "octolytics-dimension-user_login": "AFKmoney", "octolytics-url": "https://collector.github.com/github/collect", "og:description": "Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU - AFKmoney/CogNet", "og:image": "https://opengraph.githubassets.com/19f6fb278b1c9f923ed300875c883557b0e6f0d1876803bf2df9c9e1e093f269/AFKmoney/CogNet", "og:image:alt": "Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU - AFKmoney/CogNet", "og:image:height": "600", "og:image:width": "1200", "og:site_name": "GitHub", "og:title": "GitHub - AFKmoney/CogNet: Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU", "og:type": "object", "og:url": "https://github.com/AFKmoney/CogNet", "release": "3f244c05cbe64c2455314b1780ba7b42e978858d", "request-id": "146B:23EDFB:CC4479A:CEB661D:6A2DA90D", "route-action": "disambiguate", "route-controller": "files", "route-pattern": "/:user_id/:repository", "theme-color": "#1e2327", "turbo-body-classes": "logged-out env-production page-responsive", "turbo-cache-control": [ "no-preview", "no-cache" ], "twitter:card": "summary_large_image", "twitter:description": "Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU - AFKmoney/CogNet", "twitter:image": "https://opengraph.githubassets.com/19f6fb278b1c9f923ed300875c883557b0e6f0d1876803bf2df9c9e1e093f269/AFKmoney/CogNet", "twitter:site": "@github", "twitter:title": "GitHub - AFKmoney/CogNet: Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU", "ui-target": "full", "user-login": "", "viewport": "width=device-width", "visitor-hmac": "67c3f49a7fee563a5c2a8e5ebc78f2565a693a25766ee5833cd3240c3a324c2e", "visitor-payload": "eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiIxNDZCOjIzRURGQjpDQzQ0NzlBOkNFQjY2MUQ6NkEyREE5MEQiLCJ2aXNpdG9yX2lkIjoiNDQ5MTg0MzU5MDY5ODE1MDE1NyIsInJlZ2lvbl9lZGdlIjoic2VhIiwicmVnaW9uX3JlbmRlciI6InNlYSJ9" }, "title": "GitHub - AFKmoney/CogNet: Non-Transformer Language Model with Cognitive Routing — 40M params, O(n) complexity, trained on CPU", "url": "https://github.com/AFKmoney/CogNet", "usage": { "tokens": 175721 } }, "meta": { "usage": { "tokens": 175721 } }, "status": 20000 }