High-performance, lightweight deep-learning library with a PyTorch like API and GPU support.
Project description
magnetron
Super minimalistic machine-learning framework.
Explore the docs »
View GPT-2 Example
|
Report Bug
|
Request Feature
About
Magnetron is a minimalistic, PyTorch-style machine-learning framework designed for IoT and other resource-limited environments.
The tiny C99 core - wrapped in a modern Python API - gives you dynamic graphs, automatic differentiation and network building blocks without the bloat.
A CUDA backend is also WIP.
Key features
- PyTorch-like Python API
→ Seamless switch for PyTorch users with familiar syntax and behavior - Automatic differentiation on dynamic computation graphs
→ Supports flexible model construction and training workflows - High-level neural-net building blocks
→ Includesnn.Module,Linear,Sequential, and more out of the box - Broadcasting-aware operators with in-place variants
→ Efficient, NumPy-like tensor ops with performance in mind - CPU multithreading + SIMD (SSE4, AVX2/AVX512, ARM NEON)
→ High performance even without a GPU - Multiple datatypes: float32, float16, int32, and boolean
→ Flexibility for both training and quantized inference - Custom compressed tensor file formats
→ Fast serialization & model loading - Modern PRNGs (Mersenne Twister, PCG)
→ Reliable and reproducible randomness - Clear validation and error messages
→ Easier debugging and better developer experience - N-dimensional, flattened tensors
→ Simple internal representation with general support for shapes - No external C or Python dependencies (except CFFI for the Python wrapper)
→ Lightweight and portable – great for embedded or restricted environments
GPT-2 Inference Example
Run the GPT-2 example locally on your machine, purely using Magnetron.
The model data will be downloaded automatically from Hugging Face.
-
Clone and enter the Magnetron repository:
git clone https://github.com/MarioSieg/magnetron && cd magnetron
-
Create and activate a virtual environment:
python3 -m venv .venv && source .venv/bin/activate
-
Install Magnetron
(Make sure CMake and a C compiler are installed – see Prerequisites):pip install . tiktoken
-
Run the GPT-2 inference:
python3 examples/gpt2/gpt2.py "What is the answer to the universe?"
Example Output
Loading gpt2 with config: GPTHParams(block_size=1024, vocab_size=50257, n_layer=12, n_head=12, n_embd=768, dropout=0.0, bias=True)
Parameter count: 124.0M
Generated in: 4.243197377 seconds
What is the answer to the universe?
The answer could be that late in the creation the Sun may even cause protons to decay, possibly by
...
XOR Training Example
A simple XOR neuronal network (MLP) trained with Magnetron. Copy and paste the code below into a file called xor.py and run it with Python.
import magnetron as mag
from magnetron import optim, nn
from matplotlib import pyplot as plt
EPOCHS: int = 2000
# Create the model, optimizer, and loss function
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()
loss_values: list[float] = []
x = mag.Tensor.of([[0, 0], [0, 1], [1, 0], [1, 1]])
y = mag.Tensor.of([[0], [1], [1], [0]])
# Train the model
for epoch in range(EPOCHS):
y_hat = model(x)
loss = criterion(y_hat, y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
loss_values.append(loss.item())
if epoch % 100 == 0:
print(f'Epoch: {epoch}, Loss: {loss.item()}')
# Print the final predictions after the training
print('=== Final Predictions ===')
with mag.no_grad():
y_hat = model(x)
for i in range(x.shape[0]):
print(f'Expected: {y[i]}, Predicted: {y_hat[i]}')
# Plot the loss
plt.figure()
plt.plot(loss_values)
plt.xlabel('Epoch')
plt.ylabel('MSE Loss')
plt.title('Training Loss over Time')
plt.grid(True)
plt.show()
This results in the following output:
Operators
The following table lists all available operators and their properties.
| Mnemonic | Desc | IN | OUT | Params | Flags | Inplace | Backward | Result | Validation | CPU-Parallel | Type |
|---|---|---|---|---|---|---|---|---|---|---|---|
| NOP | no-op | 0 | 0 | N/A | N/A | NO | NO | N/A | NO | NO | NO-OP |
| CLONE | strided copy | 1 | 1 | N/A | N/A | NO | YES | ISOMORPH | YES | NO | Morph |
| VIEW | memory view | 1 | 1 | N/A | N/A | NO | YES | ISOMORPH | YES | NO | Morph |
| TRANSPOSE | 𝑥ᵀ | 1 | 1 | N/A | N/A | NO | YES | TRANSPOSED | YES | NO | Morph |
| PERMUTE | swap axes by index | 1 | 1 | U64 [6] | N/A | NO | NO | PERMUTED | YES | NO | Morph |
| MEAN | (∑𝑥) ∕ 𝑛 | 1 | 1 | N/A | N/A | NO | YES | SCALAR/REDUCED | YES | NO | Reduction |
| MIN | min(𝑥) | 1 | 1 | N/A | N/A | NO | NO | SCALAR/REDUCED | YES | NO | Reduction |
| MAX | max(𝑥) | 1 | 1 | N/A | N/A | NO | NO | SCALAR/REDUCED | YES | NO | Reduction |
| SUM | ∑𝑥 | 1 | 1 | N/A | N/A | NO | YES | SCALAR/REDUCED | YES | NO | Reduction |
| ABS | |𝑥| | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SGN | 𝑥⁄ | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| NEG | −𝑥 | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| LOG | log₁₀(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SQR | 𝑥² | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SQRT | √𝑥 | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SIN | sin(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| COS | cos(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| STEP | 𝐻(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| EXP | 𝑒ˣ | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| FLOOR | ⌊𝑥⌋ | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| CEIL | ⌈𝑥⌉ | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| ROUND | ⟦𝑥⟧ | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SOFTMAX | 𝑒ˣⁱ ∕ ∑𝑒ˣʲ | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SOFTMAX_DV | 𝑑⁄𝑑𝑥 softmax(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SIGMOID | 1 ∕ (1 + 𝑒⁻ˣ) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SIGMOID_DV | 𝑑⁄𝑑𝑥 sigmoid(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| HARD_SIGMOID | max(0, min(1, 0.2×𝑥 + 0.5)) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SILU | 𝑥 ∕ (1 + 𝑒⁻ˣ) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| SILU_DV | 𝑑⁄𝑑𝑥 silu(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| TANH | tanh(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| TANH_DV | 𝑑⁄𝑑𝑥 tanh(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| RELU | max(0, 𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| RELU_DV | 𝑑⁄𝑑𝑥 relu(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| GELU | 0.5×𝑥×(1 + erf(𝑥 ∕ √2)) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| GELU_DV | 𝑑⁄𝑑𝑥 gelu(𝑥) | 1 | 1 | N/A | N/A | YES | YES | ISOMORPH | YES | YES | Unary OP |
| ADD | 𝑥 + 𝑦 | 2 | 1 | N/A | N/A | YES | YES | BROADCASTED | YES | YES | Binary OP |
| SUB | 𝑥 − 𝑦 | 2 | 1 | N/A | N/A | YES | YES | BROADCASTED | YES | YES | Binary OP |
| MUL | 𝑥 ⊙ 𝑦 | 2 | 1 | N/A | N/A | YES | YES | BROADCASTED | YES | YES | Binary OP |
| DIV | 𝑥 ∕ 𝑦 | 2 | 1 | N/A | N/A | YES | YES | BROADCASTED | YES | YES | Binary OP |
| MATMUL | 𝑥𝑦 | 2 | 1 | N/A | N/A | YES | YES | MATRIX | YES | YES | Binary OP |
| REPEAT_BACK | gradient broadcast to repeated shape | 2 | 1 | N/A | N/A | YES | YES | BROADCASTED | YES | NO | Binary OP |
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
License
(c) 2025 Mario "Neo" Sieg. mario.sieg.64@gmail.com
Distributed under the Apache 2 License. See LICENSE.txt for more information.
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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file magnetron-0.1.0.tar.gz.
File metadata
- Download URL: magnetron-0.1.0.tar.gz
- Upload date:
- Size: 8.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ddf3e75f3d5e8dd476c7e708ac3961cd5a113b9241638f4116a4c0c06da5a88
|
|
| MD5 |
9af6bfbb5d01518af7235fa34dfc760e
|
|
| BLAKE2b-256 |
66a3fad188670cc66012a9cf8458ee79395889597284b9d96449bed5d96f5aa7
|
File details
Details for the file magnetron-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: magnetron-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 816.1 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc95524abe757db01daadfcf6764f2cd343e30664fd7c5d1d24213d22996dbbc
|
|
| MD5 |
90841cf2d738a12d54ee19a7f4e29969
|
|
| BLAKE2b-256 |
dacff105ff108381c8f46abb59400c0ceedd52ac45727f94c671bb1bf4aea336
|
File details
Details for the file magnetron-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: magnetron-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 461.7 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
feb78b8844e3158a8bb4d181f94cfd820a364a9d5f2a1614f1242739ca90f19d
|
|
| MD5 |
2b192a962b1055d2506e6e7fedcfe296
|
|
| BLAKE2b-256 |
c281807bae0ba04845aa619f941ac1394350d69a33a3194de955d70dd8548e44
|
File details
Details for the file magnetron-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: magnetron-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 816.1 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34d69547f66c6e9f94f69f4b3285078c7917d674ee003fed60270528cd9e52b5
|
|
| MD5 |
1ae4e3cae9b71286f4f684af703f3a81
|
|
| BLAKE2b-256 |
d40f2a1830050a5921d8e3c07f281fb04a176afcad4be7e48859f5fb22b0526a
|
File details
Details for the file magnetron-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: magnetron-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 461.7 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc16e1a2f6003073cb98b8a7ff053e93654c696d11667e6cc0b8f0d49d9f1406
|
|
| MD5 |
a5c7bcc9e86267fa990798e3b9096670
|
|
| BLAKE2b-256 |
1c273326dd682dfd4da33d4c301715cbda97a5e5cab88eebe01893b75fde6d14
|
File details
Details for the file magnetron-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: magnetron-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 816.1 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a5382a453167aa5e761a0b95270993091724abca4d57c1d6af7f21e883ccfa9
|
|
| MD5 |
d933910bd0571d942714c82d5f26a7de
|
|
| BLAKE2b-256 |
1a6f0057c1632ebad416a2923edf49536abe30a8f4cbb5941b8d0705982b80d0
|
File details
Details for the file magnetron-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: magnetron-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 461.7 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f30be64759b9545d6dbbe6ea875d2dc0db0f3433e327651777271b0011782c68
|
|
| MD5 |
6cf9e764e2c45de19f19b91a515b5509
|
|
| BLAKE2b-256 |
151333641835f268a2604d584fb872368715fe84e8e15101f6071229c9d0c868
|