Hybrid Belief-State Network: a contradiction-aware hybrid architecture for compact language models. Pure PyTorch implementation targeting a single RTX 5060 8GB (also runs on CPU).
Embedding
-> n_ssm_layers x MambaBlock (selective SSM, belief state h)
-> DialecticalGate (contradiction index c, alpha = beta + (1-beta)*c)
-> n_attention_layers x AttentionBlock (precise retrieval)
-> final norm -> tied LM head
- SelectiveSSM: diagonal SSM with input-dependent B, C, dt; causal depthwise conv; chunked parallel scan for training and an exact recurrent step mode for generation.
- DialecticalGate: computes a per-token contradiction index from the belief state and modulates how strongly new evidence updates the state.
- Composite loss:
L = L_text + aux_weight * L_contradiction + energy_weight * E, whereE = ||h||^2is the state energy.
python -m venv .venv
.\.venv\Scripts\pip install -e ".[dev]".\.venv\Scripts\python -m src.scripts.train --config config/base.yaml --epochs 50Options: --device cpu|cuda, --n-samples, --n-corpus, --epochs.
The tokenizer is trained automatically on the built-in synthetic Russian
corpus and saved to data/tokenizer.json; checkpoints go to
checkpoints/hbs_final.pt.
.\.venv\Scripts\python -m src.scripts.generate `
--checkpoint checkpoints/hbs_final.pt `
--question "Какая трава?" `
--evidence "трава была зелёная." "трава оказалась жёлтая." `
--temperature 0.8 --max-new-tokens 32Prints the answer and the mean contradiction index over generated tokens.
.\.venv\Scripts\python -m pytest tests -qtests/test_model.py includes a step/sequence consistency test that
verifies recurrent generation exactly reproduces teacher-forced forward
passes.
src/hbs/tokenizer— BPE tokenizer and synthetic corpussrc/hbs/model— Mamba blocks, attention, dialectical gate, HBSNetsrc/hbs/dataset— synthetic contradiction dataset and collatesrc/hbs/training— composite loss and training loopsrc/hbs/inference— autoregressive generatorsrc/scripts— CLI entry points (train,generate)config/base.yaml— model and training hyperparameters