Skip to main content

A compact machine learning runtime for developers who want to understand, control, and optimize the full stack.

Project description

Stargazers Forks Issues GitHub Actions Workflow Status


Magnetron Logo

magnetron

A compact machine learning runtime for developers who want to understand, control, and optimize the full stack.
Native C core, modern Python API, no runtime dependencies, no bloat.

Documentation »

Qwen3 Inference Example · Autoencoder Training Example · GPT-2 Inference Example


About

Magnetron is a machine learning runtime built from scratch in C, with a small modern Python interface for usability.
It implements its own tensor system, operator set, autograd engine, and execution model - without relying on large external frameworks.

The goal is simple:

Keep the stack small enough to understand and be hackable, but powerful enough to run real models.

This makes Magnetron useful in two situations:

  • when you want full control over execution and memory
  • when you want a clean base for experimentation or new ideas

Why Magnetron?

Magnetron is not trying to compete with PyTorch on ecosystem or feature count.

Instead, it optimizes for a different axis:

Magnetron PyTorch
Small, inspectable core Large, layered system
Explicit execution Implicit / abstracted
Minimal dependencies Heavy runtime
Easy to modify kernels Harder to reason about backend
Good for research & systems work Good for production & scale

If you want to:

  • understand how your model actually runs
  • experiment with kernels, memory layouts, or execution
  • port ML workloads to unusual hardware

Magnetron gives you a much shorter path.


Architecture Overview

Magnetron is built as a single, cohesive runtime, not a collection of loosely coupled libraries.

  • Tensor system
    Owns dtype, shape, strides, and memory – supports a full view system with a view solver, enabling complex slicing, reshaping, and broadcasting semantics similar to PyTorch while remaining explicit and predictable.

  • Execution model
    Eager execution with a dynamic autograd graph (reverse-mode), constructed per forward pass and traversed during backward.

  • Operator backend
    Central dispatch layer mapping high-level operations to architecture-specific kernel implementations.

  • CPU backend
    Multi-dispatch design with compile-time optimized kernels for a wide range of microarchitectures (Intel, AMD Zen1–Zen5, ARM).
    At runtime, CPUID-based detection selects the most optimal kernel path automatically.
    Supports multiple SIMD ISAs and extensions, including SSE (1–4), AVX, AVX2, FMA, AVX-512, AVX-512-BF16, AVX-512-FP16, F16C and ARM NEON, combined with multithreaded execution.

  • CUDA backend (in progress)
    Kernel layer is implemented - Memory management, execution pipeline, and integration are actively being completed.

  • Serialization
    Native .mag format designed for zero-copy, memory-mapped loading, enabling fast startup and efficient large model handling.
    Conversion tools are provided to import weights from external formats.

  • Backend extensibility
    The architecture is intentionally clean and modular, making it straightforward to introduce new backends or target additional hardware platforms.

The system is intentionally kept tight and explicit, so each layer is understandable, controllable, and replaceable without hidden complexity.


Highlights

  • Practical, not just educational
    Capable of running modern LLM inference (e.g. Qwen3 in BF16), not just toy models.

  • Small, controllable ML runtime
    Designed to stay inspectable end-to-end - no hidden execution layers or opaque backends.

  • True ownership of execution
    You can reason about memory layout, kernel dispatch, and graph behavior without abstraction barriers.

  • Hardware-aware by design
    Not a generic backend wrapper - kernels and execution are written with specific ISAs and microarchitectures in mind.

  • Zero-copy model loading
    Memory-mapped .mag format enables fast startup and efficient handling of large models.

  • Built for experimentation
    Easy to modify operators, add kernels, or prototype new execution strategies.

  • Minimal runtime surface
    Native extension with no required Python dependencies - easy to deploy and embed.


Example Models

End-to-end demos live under examples/.

Path Description
examples/qwen3/ Qwen3 transformer inference in bfloat16 with tokenizer integration, .mag weights, CLI chat, and HTTP/streaming API.
examples/gpt2/ GPT-2 causal language model inference with KV cache, token streaming, and configurable generation.
examples/ae/ Convolutional autoencoder with training loop and reconstruction visualization.
examples/linear_regression/ Simple 1D regression with SGD and loss tracking.
examples/xor/ Minimal MLP demonstrating autograd and optimization.

Operator Cheat Sheet

Magnetron provides a compact but expressive operator set covering:

  • elementwise operations (add, mul, div, ...)
  • reductions (sum, mean, ...)
  • tensor transformations (view, reshape, permute, ...)
  • neural building blocks (matmul, softmax, layernorm, ...)
  • type casting and memory views

A full reference of operators, data types, and semantics is available here:

Magnetron Cheat Sheet


Installation

Magnetron is available on PyPI.

Make sure you are inside a Python virtual environment.

pip install magnetron

or with uv:

uv pip install magnetron

Local Development

Clone the repository and install locally:

git clone --recursive https://github.com/MarioSieg/magnetron
cd magnetron
uv pip install . -v

For C/C++ development, open the project root (containing CMakeLists.txt) in an IDE such as CLion.


Quick start

from magnetron import Tensor, nn, optim

x = Tensor([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
y = Tensor([[0.0], [1.0], [1.0], [0.0]])

model = nn.Sequential(
    nn.Linear(2, 2),
    nn.Tanh(),
    nn.Linear(2, 1),
    nn.Tanh(),
)

optimizer = optim.SGD(model.parameters(), lr=1e-1)
criterion = nn.MSELoss()

for epoch in range(2000):
    y_hat = model(x)
    loss = criterion(y_hat, y)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

    if epoch % 100 == 0:
        print(f"Epoch {epoch:4d} | Loss {loss.item():.6f}")

y_hat = model(x)
for i in range(x.shape[0]):
    print(f"Expected: {y[i].item():.1f}, Predicted: {y_hat[i].item():.4f}")

Roadmap

  • 🚧 CUDA backend
    Finish memory model, execution pipeline, and stabilize for production use.

  • 🚧 Multi-GPU execution
    Introduce scalable execution across multiple devices.

  • 🚧 New CPU architectures
    Support for LoongArch and RISC-V.

  • 🧪 JIT compilation
    Custom SSA-based IR with register allocation and target-specific instruction emission.


History

Magnetron started in 2024 as a personal project to understand how machine learning frameworks work internally: tensor storage, operator dispatch, autograd, and inference execution. What began as a learning project gradually evolved into a full runtime with its own tensor engine, native snapshot format, SIMD-specialized CPU backend, and support for running modern models such as Qwen3 in BF16. Today, Magnetron is developed both as a practical inference/runtime system and as a research platform for experimenting with new backends, execution strategies, and low-level ML systems ideas.


License

(c) 2026 Mario Sieg - mario.sieg.64@gmail.com
Distributed under the Apache 2 License.
Developed in Berlin, Germany.


Similar Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magnetron-0.1.7.tar.gz (7.7 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

magnetron-0.1.7-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

magnetron-0.1.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

magnetron-0.1.7-cp313-cp313-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

magnetron-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

magnetron-0.1.7-cp312-cp312-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

magnetron-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

magnetron-0.1.7-cp311-cp311-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

magnetron-0.1.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

magnetron-0.1.7-cp310-cp310-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file magnetron-0.1.7.tar.gz.

File metadata

  • Download URL: magnetron-0.1.7.tar.gz
  • Upload date:
  • Size: 7.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.13

File hashes

Hashes for magnetron-0.1.7.tar.gz
Algorithm Hash digest
SHA256 25de13aaff62f587215efda3effd77c355988a4b47f7cb1acb56e3cc27c578ec
MD5 8e9867ca6bfee8f6d0815a0991466586
BLAKE2b-256 6e527ab8ee33850f020967d0fbdf1578e27c35c812599165fad4be471967ff39

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4077558ccb3a91f5efd634b6c67420fd2ba98895a85f24eaa097c46917b98851
MD5 ccf44814530ec657d1bea19c5bd5544c
BLAKE2b-256 3074ed845458f4bccfc11aae0f71d90c2509de91ee1e4952e1e6be05faaef219

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 df70bd2bbfdc109ec09908fae2341e3a6c5c8ca8998c1255aac5115b19a4a9f2
MD5 ed6e00b31ba7a43e78257dce085f719b
BLAKE2b-256 48d188ad554d7ca1b5c40644b3e8e14ed62e230a2f2aa60e4b573135754c9479

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c996dbebef1fa479821a1a2b86b34fb269deaf84c8b4ce208d0d0b68d768ddb0
MD5 f43a780f6b689424b44e1b95cd552bcc
BLAKE2b-256 96d574af67ccaf57c58ab3b5c552af05ca3b0896992cc435ed1679c356a059b1

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9ac4e163819398b5691a66ff570f571392078a669eecc341748b7612cb00ff88
MD5 a8d40c89c233b61682fe31dfaa6a7818
BLAKE2b-256 61e97d741c13262c3ff51d1223d502a5d70a62705b7265138ef7e74218abdcb4

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f9f957b704b0961f3f54e3de8027690c8eb72a0d80ca1b6e8c5cb10610a81260
MD5 339e3e085a626dbe6716823d46960807
BLAKE2b-256 3863fb7cdc0f998489361982ffd71c8131ac1c0775e205dc9325ca1fe82a9ac2

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1dc786c1e87f477bb251faefce22ef1194e8e07f0c18d0e1015116a16efb01b1
MD5 d9309d2cdf568e3f3503cbcc006bf885
BLAKE2b-256 fdccdd8891dcce2594aa475cb8f52ab6ad2b5382538a42ef4db0e6634a270b3b

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f7800aefc3c8816d734c0475df8147503b24342278cc138d42fa493e602ce48d
MD5 6e9fe3a1fd3d9ea0a5a7b9f327ce34f0
BLAKE2b-256 b06f8171f1da1d1e6c176c82b66b44604a84fee627c857923657418463b7dd68

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cc9bb5f8e18bcd5b69c4224139869790e3ed9d8d59757235c4df0217d57e0c20
MD5 b6f94d9441f8a25a14149f1ba119100c
BLAKE2b-256 250e01c7e9fc99ff4ac1699b1d588c00ffcd9b4e4710311e483b1d832a7b336a

See more details on using hashes here.

File details

Details for the file magnetron-0.1.7-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for magnetron-0.1.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 89264e8504637f25d2cd0e67be7a82a165e734dc3b5ed1d90478e5d1789e070e
MD5 b167a8ab6c52958418ff40b13eeaf802
BLAKE2b-256 fd008249bd8596df573b3f11b61177630fd22d5f9916f6ffa8ebb14c1399ae27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page