Skip to main content

Thoughtformula 🧠

Release candidate 0.3.1: Bounded inference, deterministic sampling and resumable pretraining used by Thoughtform. Published versions and main development are tracked separately.

Pure-Python generative Transformer architecture featuring FlashAttention online softmax tiling, vLLM-style Paged KV-Cache, BitNet b1.58 ternary quantization, Grouped Query Attention (GQA), Rotary Positional Embeddings (RoPE), Sparse Mixture of Experts (MoE SwiGLU), LoRA continual learning, Kahneman-Tversky Optimization (KTO), Medusa speculative decoding, persistent binary index (.tfidx), and continuous batching. Built exclusively with Python standard library built-ins.

Tests Python Dependencies Version


Key Features

  • Standard Library Autograd (FlatTensor): 1D contiguous float64 training buffer with symmetric Int8 inference quantization, BitNet b1.58 ternary quantization, and reverse-mode automatic differentiation.
  • FlashAttention Online Softmax Tiling: Dao et al. streaming online softmax eliminates $T \times T$ attention matrix materialization, maintaining $O(N)$ memory across long context windows.
  • vLLM-Style Paged KV-Cache (PagedKVCache): Fixed-size physical memory pages (e.g., 16 tokens/block), block allocator, and page tables supporting non-contiguous allocation and $O(1)$ prompt prefix sharing.
  • BitNet b1.58 Ternary Quantization: Weights in ${-1, 0, 1}$ with scale $\gamma = \text{mean}(|W|)$, 2-bit bitpacking (4 weights/byte), and addition-only matrix multiplication.
  • Kahneman-Tversky Optimization (KTO): Unpaired binary human preference alignment (q :: a ++ and q :: a --) using prospect theory utility loss and frozen base Int8 weights as reference.
  • Interactive Streaming Token Generator: Real-time typewriter output streaming via generate_stream(), generate_speculative_stream(), and generate_medusa_stream().
  • Medusa Multi-Head Speculative Drafter: Lightweight auxiliary heads predicting multiple future tokens simultaneously directly from the final hidden state without extra transformer layers.
  • Persistent Binary Index Format (.tfidx): High-performance binary container persisting precomputed dense embeddings and term inverted postings for large-scale hybrid RAG retrieval.
  • Continuous Batching Engine (ContinuousBatchScheduler): Synchronous iteration-level continuous batcher packing active requests into unified forward passes, amortizing parameter loading across concurrent sessions.
  • C-Speed Matrix Multiplication: Accelerated with compiled C math.sumprod over contiguous transposed weight slices (5x-12x pure-Python speedup).
  • Grouped Query Attention (GQA) & Precomputed RoPE: Memory-efficient query/key-value head grouping with precomputed cos_cache / sin_cache trigonometric lookup tables.
  • Sparse Mixture of Experts (MoE): 4 SwiGLU experts with true sparse token dispatch and Switch Transformer auxiliary load-balancing loss preventing expert collapse.
  • Zero-Memory Direct Preference Optimization (DPO): Align model responses (q :: good || bad) using frozen Int8 base weights as reference policy $\pi_{\text{ref}}$ with 0 extra RAM.
  • Decoupled Weight Decay (AdamW): Bounded weight magnitudes preventing Int8 scale drift.
  • Lossless Speculative Decoding: Leviathan et al. (2023) rejection sampling with N-gram proposal distributions and Int8 KV-cache rollback.
  • Hybrid Dense + Lexical RAG: Okapi BM25 combined with mean-pooled tok_emb dense embeddings via Reciprocal Rank Fusion (RRF).
  • Binary .mgpt Container: Python standard library zipfile binary container eliminating 33% Base64 storage overhead.
  • Built-in Gradient Checker: Finite-difference calculus test harness (/test) verifying all analytical gradients to $< 10^{-10}$.

Quickstart

# Install via pip
pip install thoughtformula

# Run CLI directly
thoughtformula --benchmark

# Or run interactive typewriter REPL
thoughtformula

Run from Source

git clone https://github.com/wispsy/thoughtformula.git
cd thoughtformula

# Run speculative decoding benchmark
PYTHONPATH=src python3 -m thoughtformula.cli --benchmark

# Run interactive typewriter REPL
PYTHONPATH=src python3 -m thoughtformula.cli

REPL Commands & Syntax

Syntax Action
question? RAG retrieval + speculative decoding typewriter output
q :: answer Continual online LoRA adaptation
q :: good || bad Zero-Memory LoRA Direct Preference Optimization (DPO)
q :: answer ++ Kahneman-Tversky Optimization (KTO) Desirable feedback
q :: answer -- Kahneman-Tversky Optimization (KTO) Undesirable feedback
/test Run finite-difference calculus gradient verification
/merge Merge LoRA adapters into base weights
/stats Display model parameter count and cache metrics

Project Structure

thoughtformula/
├── pyproject.toml
├── README.md
├── docs/
│   ├── architecture.md
│   ├── modules.md
│   └── quickstart.md
├── src/
│   └── thoughtformula/
│       ├── __init__.py
│       ├── batching.py
│       ├── cache.py
│       ├── cli.py
│       ├── draft.py
│       ├── gradcheck.py
│       ├── medusa.py
│       ├── micro_gpt_checkpoint.json
│       ├── micro_gpt_v8.py
│       ├── model.py
│       ├── rag.py
│       ├── tensor.py
│       ├── tokenizer.py
│       └── training.py
└── tests/
    ├── test_batching.py
    ├── test_cache.py
    ├── test_draft.py
    ├── test_gradcheck.py
    ├── test_medusa.py
    ├── test_model.py
    ├── test_rag.py
    ├── test_tensor.py
    ├── test_tokenizer.py
    └── test_training.py

Running Tests

All 64 unit tests run using Python's built-in unittest framework with zero external dependencies:

PYTHONPATH=src python3 -m unittest discover tests

License

MIT

Generation request limits

All standard, speculative and Medusa generation methods accept nonnegative integer token budgets, reject invalid vocabulary IDs, and require prompt plus output budget within model.max_seq_len. Zero budget preserves the prompt in nonstream results and yields nothing in streams, without cache allocation, Medusa setup or a forward pass. Empty prompts retain their existing empty return shapes. Sampling and return shapes otherwise remain compatible.

Bounded homegrown training

TrainingConfig, TrainingState, fit and evaluate expose bounded next-token training over the existing autograd/Adam implementation. Callers select token sequences, context/step/time budgets and optional boundary stop/progress callbacks. Evaluation reports token-weighted loss/perplexity and exact denominators.

save_training_checkpoint and load_training_checkpoint use versioned atomic lossless float64 parameter/Adam state for exact fixed-order resume. These bounded passive .tft artifacts are distinct from quantized inference exports, which retain their existing API. Checkpoints preserve model/tokenizer/config/progress and caller metadata; source/data identity remains the application's responsibility. Save at completed optimizer boundaries. reset_optimizer explicitly starts a new specialization trajectory while preserving learned weights. Auxiliary Medusa heads are outside this initial training artifact profile. No data/model download, pickle or implicit promotion occurs.

Metadata

Release files for thoughtformula 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thoughtformula 0.3.1
File Size Uploaded Provenance Actions
thoughtformula-0.3.1.tar.gz 98.8 kB With provenance Details for thoughtformula-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for thoughtformula 0.3.1
File Interpreter ABI Platform Actions
thoughtformula-0.3.1-py3-none-any.whl Python 3 none any Details for thoughtformula-0.3.1-py3-none-any.whl

Total release size: 188.0 kB

Release files / thoughtformula-0.3.1.tar.gz

Download URL thoughtformula-0.3.1.tar.gz
Size 98.8 kB
Tags Source
SHA-256 checksum
How to use checksums
91dccc6796620a8656ac38c3f7f83273eeb87a8004d3b2b83ebd5f026372dcad
BLAKE2b-256 checksum
How to use checksums
055b32c04914f768e489f09a41cd861c63b4e951ac1481ec0a3c8fed585f6b46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / thoughtformula-0.3.1-py3-none-any.whl

Download URL thoughtformula-0.3.1-py3-none-any.whl
Size 89.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6f936bc2d86e090ef7dbdcdd5b67bc58ea04eab01702b932df2c74aa766f6b4
BLAKE2b-256 checksum
How to use checksums
91a5b4b41ebb0b3d4cf7f8440a9b58201220a01366255dcc2b213e53b10d17b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for thoughtformula 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for thoughtformula-0.3.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page