How tgakathunderr/bim-2 works
1. Project shape and runtime boundary
- BIM 2 is a CPU-only, online sequence learning system exposed through a small CLI in main.py.
- The repository is organized around a session loop that learns from each token as it arrives, with optional checkpoint load/save for persistence across runs.
2. Top-level execution flow
- User text enters through interaction/loop.py.
- Text is tokenized in inference/tokenizer.py.
- Tokens are converted into sparse distributed representations by the core spatial pooler.
- Those SDRs flow through the hierarchical cortex for prediction and learning.
- Outputs are decoded back to tokens and returned to the interaction loop.
- Session state can be saved/restored via utils/save_load.py.
3. Core representation layer
- core/spatial_pooler.py is the token-to-SDR boundary.
- Each token maps to a sparse pattern of active columns, giving the model a fixed high-dimensional representation space.
- core/sdr.py likely provides the SDR data structures and set-like operations used throughout the rest of the system.
4. Hierarchical cortex subsystem
- core/sparse_cortex.py and core/hierarchy.py implement the main predictive substrate.
- The architecture is two-layered: a lower sensory layer for local token transitions and an upper abstract layer for slower context.
- The README’s “apical bias” feedback means top-down influence is part of the prediction path, not just a post-processing step.
5. Learning subsystem
- The learning package contains the model’s adaptation rules:
- learning/prediction_error.py computes surprise/error between predicted and actual SDRs.
- learning/sparse_predictive.py holds the Hebbian-style potentiation/depression logic.
- learning/reward.py adds scalar reward-based adjustment for previous/current winners.
- learning/homeostasis.py performs synaptic scaling to prevent runaway permanence growth.
- The key design boundary is that surprise directly scales learning, so updates happen only when prediction was wrong.
6. Concept formation and abstraction
- learning/concepts.py builds the ConceptGraph.
- Repeated abstract L2 patterns crystallize into named concepts, and weighted edges capture co-occurrence relationships.
- This subsystem is separate from the raw cortex: it turns stable internal patterns into graph-based symbolic memory and shallow inference.
7. Interaction and working memory
- interaction/loop.py is the conversational orchestrator and REPL-facing control loop.
- interaction/memory.py stores recent (input SDR, response SDR, surprise) tuples with decay.
- This working memory is the bridge between immediate token-level behavior and longer session context, including correction behavior.
8. Inference boundary
- inference/tokenizer.py is the only explicit text-processing boundary in the repo.
- It enforces a word-level vocabulary and maps text to model tokens and back.
- This matters architecturally because the model does not operate on subword pieces or embeddings; it learns directly over discrete tokens.
9. Persistence and observability
- utils/save_load.py handles full-state checkpointing, which is important because the model is intended to accumulate knowledge over time.
- utils/metrics.py and main.py stats expose system-level measurements.
- utils/visualization.py is the inspection/diagnostic boundary for understanding learned structure and activity patterns.
10. Operational modes
- main.py dispatches at least three workflows:
- interact for online teaching and dialogue,
- stats for inspection,
- benchmark for the fictional NovaCorp knowledge/inference test.
- The benchmark is not a training pipeline separate from the system; it is a scripted exercise of the same online learning stack.
11. Important data flow to capture in a diagram
- Token in → tokenizer → spatial pooler SDR → hierarchical cortex → prediction/error comparison → surprise-scaled learning update → updated permanences/state → token out.
- In parallel, repeated abstract patterns may flow into ConceptGraph, and recent exchanges may flow into working memory.
12. Architectural constraints that shape the system
- No backpropagation, no GPU, no pretraining: learning is local, incremental, and stateful.
- The system’s “knowledge” is therefore distributed across permanence matrices, contextual winners, memory buffers, and concept edges rather than a single trained weight file.
Main entry points: main.py Main · sparse_cortex.py Sparse cortex · loop.py Session loop · sparse_predictive.py Predictive learning
Components 17
Entry & I/O
CLI dispatch and text/token boundaries.
Core Model
Representation, cortex, and hierarchy.
- Spatial pooler · Maps discrete tokens into sparse distributed representations.
core/spatial_pooler.py - SDR · Core sparse distributed representation data structures and operations.
core/sdr.py - Sparse cortex · Main cortical substrate that predicts and learns from sparse activity.
core/sparse_cortex.py - Hierarchy · Coordinates lower sensory and upper abstract layers with top-down influence.
core/hierarchy.py
Learning
Local adaptation, surprise, reward, and concept formation.
- Prediction error · Computes mismatch between predicted and actual SDRs.
learning/prediction_error.py - Predictive learning · Applies local potentiation and depression for sparse predictive learning.
learning/sparse_predictive.py - Reward · Modulates learning using reward signals for previous and current winners.
learning/reward.py - Homeostasis · Constrains permanence growth and keeps learning stable.
learning/homeostasis.py - Concept graph · Forms named concepts and co-occurrence links from repeated abstract patterns.
learning/concepts.py
Interaction
Session loop and working memory.
- Session loop · Orchestrates the turn-by-turn interaction and online teaching cycle.
interaction/loop.py - Working memory · Stores recent input, response, and surprise tuples with decay.
interaction/memory.py
Utilities
Persistence, metrics, and visualization.
- Checkpointing · Saves and restores full session and model state across runs.
utils/save_load.py - Metrics · Computes repository-level measurements for inspection and stats mode.
utils/metrics.py - Visualization · Renders internal structure and activity for debugging and analysis.
utils/visualization.py
Connections 22
- Main → Session loop: interact · Dispatches the interactive online learning workflow.
- Main → Metrics: stats · Routes to system measurement and inspection.
- Main → Visualization: inspect · Can invoke visual diagnostics.
- Main → Checkpointing: checkpoint · Loads or saves session state around runs.
- Session loop → Tokenizer: text · Accepts user text and converts it for model processing.
- Tokenizer → Spatial pooler: tokens · Feeds discrete tokens into the representation boundary.
- Spatial pooler → SDR: encode · Produces sparse distributed representations.
- SDR → Sparse cortex: SDR flow · Provides sparse inputs to the predictive cortex.
- Sparse cortex → Hierarchy: layers · Organizes computation across sensory and abstract layers.
- Hierarchy → Prediction error: compare · Compares predicted and observed activity to form surprise.
- Prediction error → Predictive learning: scale · Surprise gates the magnitude of learning updates.
- Predictive learning → Sparse cortex: update · Adjusts permanences and local predictive state.
- Reward → Predictive learning: modulate · Biases learning using reward information.
- Homeostasis → Predictive learning: stabilize · Normalizes growth to prevent runaway adaptation.
- Hierarchy → Concept graph: abstract patterns · Feeds stable higher-level patterns into concept formation.
- Session loop → Working memory: store turns · Records recent exchanges and surprise for context.
- Working memory → Session loop: context · Provides recent session history back to the interaction loop.
- Checkpointing → Session loop: restore · Rehydrates session state for continued learning.
- Sparse cortex → Checkpointing: state · Persists learned structure and context across runs.
- Metrics → Visualization: report · Feeds measured state into inspection views.
- Config → Main: params · Supplies runtime configuration for CLI execution.
- Config → Sparse cortex: tuning · Shapes core model behavior through parameters.