Skip to main content

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

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.9.tar.gz (7.9 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.9-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (17.6 MB view details)

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

magnetron-0.1.9-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (17.6 MB view details)

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

magnetron-0.1.9-cp313-cp313-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

magnetron-0.1.9-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (17.6 MB view details)

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

magnetron-0.1.9-cp312-cp312-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

magnetron-0.1.9-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (17.6 MB view details)

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

magnetron-0.1.9-cp311-cp311-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

magnetron-0.1.9-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (17.6 MB view details)

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

magnetron-0.1.9-cp310-cp310-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for magnetron-0.1.9.tar.gz
Algorithm Hash digest
SHA256 95b40bc66b7ea3e0a07efb829322284a919fd70a2c57a385f9362ddc58d6811a
MD5 da91e05094b14ddeb5549164a7411763
BLAKE2b-256 0174efeb04e8a756e182087c9651fb312e23ae67e8a9a3915afe1265443c97db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 131f3d423cdaf31bff8d90d8d71ff2f1968d3f19f74320b21dc3ae946c3d533d
MD5 c8d5add55a862bc73870678458cb1027
BLAKE2b-256 a6cd5a0296b10347425c0fa13d3acc4ddf3f62b3a916b6cf0b6123e09d0434ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5292a8153ba93f02e39bbf4cc86d2518f64c97310f0b0bd0d7548ad5f71932ea
MD5 2ce5de3eb7b1f911e99b5a96fd96a909
BLAKE2b-256 ecee111ccf55e32583f75d22de75e969b4365772248509134c49fbdab756e0c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6f2bd91d6f63fba09e5219e67c9563085605a11c6f156b2eb04198042c42767a
MD5 4570b42379837427cd0182f523ab36e2
BLAKE2b-256 b95292acefdc10fdda2f42702b148fa6174afd48e39e03a6aa37a42ff3fd9296

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8c46ef358983a363d7c349e358bd56317263281f0b183226bf6b94a4bbcbeb2d
MD5 99a9bbc8b6d32ac87d63cbe1d0442cc6
BLAKE2b-256 e7e12894f9bc5f1d9a72a0708d3169e457ea9c9667d63186f533c580dd3899f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 440c6ff71f17488f48baaee0d6fe3ba2cee589f8afce1c781624086963576640
MD5 62a4bc456ced84a373b7e6b451655c1f
BLAKE2b-256 ed2c1ed88be2723417646e844f1f40fdb0c772d5d40f4786508c329668451877

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9690eff917fd14946a5243b0f92094ce1f5826f295e703a96edae7a9537b470a
MD5 ea05b19a16001bec925193f07656dc37
BLAKE2b-256 850a64fe4cef001b059f3ee6a75f35a5316fef41c46126132e27efe672aec438

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8266bf002485c36698a8aed25e5b7558fe6f72d09e41d989c3668d1a2d3cbea2
MD5 6c746b49f6e45f397165e63449d2d4f3
BLAKE2b-256 f519f8b00d4ba414fe9ba36ba043433452b7f18056ff92b045727f41714cff0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3bc13e1542a93376286bc92065d57ae1b400c89e42f7d33b9b846323eaa29223
MD5 67bde9ae70c65bee3db1abc6eceb5899
BLAKE2b-256 bf9589a192996cb8434db959a5382ae0e29361dc9e18c439a7465bce49b7dc1a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magnetron-0.1.9-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0a57ad732acac81362c5d590c6dffbef3de6b6d3423347497c7a6d068554f62c
MD5 3458403ee272b0757c4bfe54198c2714
BLAKE2b-256 66f5097723bfdfa18e36cfa74e84928d9c981cbb7e9b0c76ae1a4f6bfb339315

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.0

10 files

This release

0.1.9 This release

10 files

0.1.8

10 files

0.1.7

10 files

0.1.6

10 files

0.1.5

10 files

0.1.4

7 files

0.1.3

7 files

0.1.2

7 files

0.1.0

7 files

0.0.0

2 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