Skip to main content

⚡ MarkovGPU

Massive Scale Markov Models on Consumer Hardware

image

Run million-state HMMs on your laptop GPU.
No CUDA required • Hybrid CPU/GPU Backend • Production Ready

PyPI version Python 3.9+ License: MIT Build Status


🌟 The Engine for Stochastic Intelligence

MarkovGPU is a high-performance probabilistic modeling library built for speed. It breaks the "NVIDIA Monopoly" by using OpenCL to accelerate Hidden Markov Models (HMM) and Markov Chains on any GPU—including AMD Radeon, Intel Arc, and Apple Silicon.

It doesn't just run; it thinks. The Smart Hybrid Backend automatically routes small tasks to the CPU (NumPy) and massive workloads to the GPU, giving you optimal performance at every scale.


🚀 Core Superpowers

Feature Magic Behind It
⚡ Hardware Agnostic Built on OpenCL — runs on AMD, Intel, NVIDIA, and Apple M1/M2/M3 chips.
🧠 Smart Hybrid Backend Auto-detects problem size ($N$). Uses NumPy for speed on small data, GPU for massive throughput.
📉 Log-Space Stability Implements Log-Sum-Exp kernels to prevent underflow on long time-series (1M+ steps).
🕵️ Viterbi Decoding Finds the "Hidden Truth" in noisy data (e.g., market regimes, DNA sequences) in milliseconds.
🎓 Unsupervised Learning Baum-Welch (EM) algorithm trains models directly on the GPU, learning rules from raw data.
📦 Zero-Config Install pip install markovgpu-rane. No driver hell. No CUDA toolkit nightmares.

🏗️ Architecture: The Hybrid Pipeline

graph LR
    A[User Code] -->|Request Fit/Predict| B{Smart Dispatcher}
    B -->|Small N < 64| C["CPU Engine
    (NumPy AVX2)"]
    B -->|Large N >= 64| D["GPU Engine
    (OpenCL Kernels)"]
    C --> E[Result]
    D --> E
    subgraph GPU_Acceleration[GPU Acceleration]
    D --> F[Matrix Multiply]
    D --> G[Log-Sum-Exp]
    D --> H[Parallel Viterbi]
    end

The library handles the hardware. You handle the math.

⚡ Performance: Benchmarks

Task: Viterbi Decoding (64 Hidden States, 5000 Days of Data).
Hardware: AMD Ryzen 680M (Integrated Graphics).

Engine Execution Time Speedup
🐢 CPU (NumPy Optimized) 5.06s 1x
🚀 GPU (MarkovGPU) 0.82s 6.2x

⚙️ Quick Start in 30 Seconds

Installation

# Production
pip install markovgpu-rane

# Or for local development
uv pip install markovgpu-rane

1. Market Regime Detection (Viterbi)

Identify hidden "Bull" vs. "Bear" markets from noisy stock returns.

import numpy as np
from markovgpu import MarkovEngine

# 1. Setup the Rules (Transition Matrix)
# "Bull markets tend to stay Bullish (95%)"
trans_mat = np.array([[0.95, 0.05], 
                      [0.10, 0.90]], dtype=np.float32)

# 2. Feed the Data (Observation Likelihoods)
# Shape: (1000 Days, 2 States)
obs_probs = np.random.rand(1000, 2).astype(np.float32) 

# 3. Ignite the Engine
engine = MarkovEngine()
predicted_states = engine.decode_regime(trans_mat, obs_probs)

print("Detected Regimes:", predicted_states) 
# Output: [0, 0, 0, 1, 1, 1, 0 ...]

2. Unsupervised Learning (Baum-Welch)

Train the AI to discover the hidden rules from raw data.

# The engine learns the Transition Matrix automatically
learned_matrix = engine.fit(
    obs_probs, 
    n_states=2, 
    n_iters=100, 
    tolerance=1e-4
)

print("Discovered Rules:")
print(learned_matrix)

🔬 Technical Brilliance

1. The Log-Sum-Exp Kernel

Standard HMMs crash on long sequences because probabilities like $0.9^{1000}$ vanish to zero. We solved this by rewriting the entire GPU kernel in Log-Space:

// Actual OpenCL Kernel snippet
float log_add(float log_a, float log_b) {
    float max_val = max(log_a, log_b);
    return max_val + log1p(exp(min(log_a, log_b) - max_val));
}

→ Result: You can process sequences of infinite length without numerical collapse.

2. Parallel Viterbi

Instead of a slow Python loop, we launch $N$ threads (one per state) for every time step on the GPU, calculating the optimal path in parallel.


🛠️ Project Structure

markovgpu/
├── src/markovgpu/
│   ├── backend.py       # The Brain (Smart Dispatcher)
│   ├── kernels.cl       # The Muscle (OpenCL C Code)
│   └── __init__.py
├── tests/               # Unit Tests
├── pyproject.toml       # Modern Packaging Config
└── README.md

🌱 Contributing

We welcome forks, issues, and PRs!

git clone https://github.com/wizardwithcodehazard/markov.git
cd markov
uv sync --dev
uv run pytest

📄 License

MIT License — Free to use, modify, and ship in commercial products.

MarkovGPU doesn’t just crunch numbers.

It discovers the hidden structure of reality.

Made with 🧡 by Sahil Rane

Metadata

Release files for markovgpu-rane 0.4.2

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

Source distribution (sdist)

Source distribution for markovgpu-rane 0.4.2
File Size Uploaded
markovgpu_rane-0.4.2.tar.gz 88.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for markovgpu-rane 0.4.2
File Interpreter ABI Platform
markovgpu_rane-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 97.6 kB

Release files / markovgpu_rane-0.4.2.tar.gz

Download URL markovgpu_rane-0.4.2.tar.gz
Size 88.6 kB
Tags Source
SHA-256 checksum
How to use checksums
62f5054a7d3ab830da46173a23d7cd3e34d14f6dd7d487a39dba3a9c5bfc0596
BLAKE2b-256 checksum
How to use checksums
6773248b9f420e56cb0d9ce2c488b7302d8e6ed464b4a4fa7615eebbf4e540e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 4, 2026.

Transparency log

Release files / markovgpu_rane-0.4.2-py3-none-any.whl

Download URL markovgpu_rane-0.4.2-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac3ff24998bd62bd17542ba4732fefdf1b3fbd0e5865c3291e9b4dda66d8da7f
BLAKE2b-256 checksum
How to use checksums
df1ea1815dd992d82dbd35c8f25968e73e07ba4efe0befe1feb10aedad910a80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 4, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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