8000 GitHub - MarioSieg/magnetron: (WIP) A small but powerful, homemade PyTorch from scratch.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

MarioSieg/magnetron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Contributors Forks Stargazers Issues GitHub Actions Workflow Status


Logo

magnetron

Super minimalistic machine-learning framework.
Explore the docs Β»

View 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

  • N-dimensional, flattened tensors
  • Automatic differentiation on dynamic computation graphs
  • CPU multithreading + SIMD (SSE4, AVX2/AVX512, ARM NEON)
  • PyTorch-like Python API
  • Broadcasting-aware operators with in-place variants
  • High-level neural-net building blocks
  • float32 and float16 datatypes
  • Modern PRNGs (Mersenne Twister, PCG)
  • Clear validation and error messages
  • Custom compressed tensor file formats
  • No C and Python dependencies (except CFFI for the Python wrapper)

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.from_data([[0, 0], [0, 1], [1, 0], [1, 1]])
y = mag.Tensor.from_data([[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:

ScreenShot

Getting Started

To get a local copy up and running follow these simple steps.
Magnetron itself has no Python dependencies except for CFFI to call the C library from Python.
Some examples use matplotlib and numpy for plotting and data generation, but these are not required to use the framework.

Prerequisites

  • Linux, MacOS or Windows
  • A C99 compiler (gcc, clang, msvc)
  • Python 3.6 or higher

Installation

A pip installable package will be provided, as soon as all core features are implemented.

  1. Clone the repo
  2. cd magnetron/python (VENV recommended).
  3. pip install -r requirements.txt Install dependencies for examples.
  4. cd magnetron_framework && bash install_wheel_local.sh && cd ../ Install the Magnetron wheel locally, a pip installable package will be provided in the future.
  5. python examples/simple/xor.py Run the XOR example.

Usage

See the Examples directory which contains various models and training examples.
For usage in C or C++ see the Unit Tests directory.

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

(back to top)

Releases

No releases published

Sponsor this project

 

Packages

No packages published
0