Skip to main content

A lightweight, high-performance tensor operations library.

Project description

MiniTensor Logo

A lightweight, high-performance tensor operations library with automatic differentiation, inspired by PyTorch and powered by Rust backend.


Python 3.10+ rustc 1.85+ Test Linux Test Windows Test MacOS Lints License DOI

Features

  • High Performance: Rust backend for maximum speed and memory efficiency
  • Python-Friendly: Familiar PyTorch-like API for easy adoption
  • Neural Networks: Complete neural network layers and optimizers
  • NumPy Integration: Seamless interoperability with NumPy arrays
  • Automatic Differentiation: Built-in gradient computation for training
  • Extensible: Modular design for easy customization and extension

Quick Start

Installation

From PyPi:

pip install minitensor

From Source:

# Clone the repository
git clone https://github.com/neuralsorcerer/minitensor.git
cd minitensor

# Quick install with make (Linux/macOS)
make install

# Or manually with maturin
pip install maturin[patchelf]
maturin develop --release

# Optional: editable install with pip (debug build by default)
pip install -e .

Note: pip install -e . builds a debug version by default; pass --config-settings=--release for a release build.

Using the install script (Linux/macOS/Windows):

bash install.sh

Common options:

bash install.sh --no-venv          # Use current Python env (no virtualenv)
bash install.sh --venv .myvenv     # Create/use a specific venv directory
bash install.sh --debug            # Debug build (default is --release)
bash install.sh --python /usr/bin/python3.12   # Use a specific Python

The script ensures Python 3.10+, sets up a virtual environment by default, installs Rust (via rustup if needed), installs maturin (with patchelf on Linux), builds MiniTensor, and verifies the installation.

Basic Usage

import minitensor as mt
from minitensor import nn, optim

# Create tensors
x = mt.randn(32, 784)  # Batch of 32 samples
y = mt.zeros(32, 10)   # Target labels

# Build a neural network
model = nn.Sequential([
    nn.DenseLayer(784, 128),
    nn.ReLU(),
    nn.DenseLayer(128, 10)
])

# Set up training
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(0.001, betas=(0.9, 0.999), epsilon=1e-8)

print(f"Model: {model}")
print(f"Input shape: {x.shape}")

Documentation

Core Components

Tensors

import minitensor as mt
import numpy as np

# Create tensors
x = mt.zeros(3, 4)          # Zeros
y = mt.ones(3, 4)           # Ones
z = mt.randn(2, 2)          # Random normal
np_array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
w = mt.from_numpy(np_array) # From NumPy

# Operations
result = x + y                      # Element-wise addition
product = x.matmul(y.T)             # Matrix multiplication
mean_val = x.mean()                 # Reduction operations
max_val = x.max()                   # -inf for empty or all-NaN tensors
min_vals, min_idx = x.min(dim=1)    # Returns values & indices; empty dims yield (inf, 0)

Neural Networks

from minitensor import nn

# Layers
dense = nn.DenseLayer(10, 5)        # Dense layer (fully connected)
conv = nn.Conv2d(3, 16, 3)          # 2D convolution
bn = nn.BatchNorm1d(128)            # Batch normalization
dropout = nn.Dropout(0.5)           # Dropout regularization

# Activations
relu = nn.ReLU()                    # ReLU activation
sigmoid = nn.Sigmoid()              # Sigmoid activation
tanh = nn.Tanh()                    # Tanh activation
gelu = nn.GELU()                    # GELU activation

# Loss functions
mse = nn.MSELoss()                  # Mean squared error
ce = nn.CrossEntropyLoss()          # Cross entropy
bce = nn.BCELoss()                  # Binary cross entropy

Optimizers

from minitensor import optim

# Optimizers
sgd = optim.SGD(0.01, 0.9, 0.0, False)                      # SGD with momentum
adam = optim.Adam(0.001, betas=(0.9, 0.999), epsilon=1e-8)  # Adam optimizer
rmsprop = optim.RMSprop(0.01, 0.99, 1e-8, 0.0, 0.0)         # RMSprop optimizer

Architecture

Minitensor is built with a modular architecture:

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Python API    │    │   PyO3 Bindings  │    │   Rust Engine   │
│                 │<-->│                  │<-->│                 │
│ • Tensor        │    │ • Type Safety    │    │ • Performance   │
│ • nn.Module     │    │ • Memory Mgmt    │    │ • Autograd      │
│ • Optimizers    │    │ • Error Handling │    │ • SIMD/GPU      │
└─────────────────┘    └──────────────────┘    └─────────────────┘

Components

  • Engine: High-performance Rust backend with SIMD optimizations
  • Bindings: PyO3-based Python bindings for seamless interop
  • Python API: Familiar PyTorch-like interface for ease of use

Examples

Simple Neural Network

import minitensor as mt
from minitensor import nn, optim

# Create a simple classifier
model = nn.Sequential([
    nn.DenseLayer(784, 128),
    nn.ReLU(),
    nn.DenseLayer(128, 10),
])

# Initialize model
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(0.001, betas=(0.9, 0.999), epsilon=1e-8)

Training Loop

import minitensor as mt
from minitensor import nn, optim

# Synthetic data: y = 3x + 0.5
x = mt.randn(64, 1)
y = 3 * x + 0.5

# Model, loss, optimizer
model = nn.DenseLayer(1, 1)
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)

for epoch in range(100):
    pred = model(x)
    loss = criterion(pred, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if (epoch + 1) % 20 == 0:
        loss_val = float(loss.numpy().ravel()[0])
        print(f"Epoch {epoch+1:03d} | Loss: {loss_val:.4f}")

Code Style

  • Rust: Follow rustfmt and clippy recommendations
  • Python: Use black for formatting and mypy for type checking

Performance

Minitensor is designed for performance:

  • Memory Efficient: Zero-copy operations where possible
  • SIMD Optimized: Vectorized operations for maximum throughput
  • GPU Ready: CUDA and Metal backend support (coming soon)
  • Parallel: Multi-threaded operations for large tensors

Citation

If you use minitensor in your work and wish to refer to it, please use the following BibTeX entry.

@software{minitensor2025,
  author = {Soumyadip Sarkar},
  title = {MiniTensor: A Lightweight, High-Performance Tensor Operations Library},
  url = {http://github.com/neuralsorcerer/minitensor},
  year = {2025},
}

License

This project is licensed under the Apache License - see the LICENSE file for details.

Acknowledgments

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

minitensor-0.1.1.tar.gz (271.8 kB view details)

Uploaded Source

Built Distribution

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

minitensor-0.1.1-cp312-cp312-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file minitensor-0.1.1.tar.gz.

File metadata

  • Download URL: minitensor-0.1.1.tar.gz
  • Upload date:
  • Size: 271.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for minitensor-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2085bd3b5e3edc2887f76b257a7214b2dc0d7e45360b85f7e90da04a00ae0c29
MD5 fe665eb8ef3fb4a00776e3f2c5f23312
BLAKE2b-256 59c9c4e6737ed2b46ffaf48f58186b2c49c905f742bf3d0c2a4db3d1d2ec1130

See more details on using hashes here.

File details

Details for the file minitensor-0.1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: minitensor-0.1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for minitensor-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6230ba5342c871e4a693583d9bd4b3ceba7ac099a60cd9a2ae0d50ce54b73fc0
MD5 71fa892bf1b820baef1b50bc1652c344
BLAKE2b-256 1e7ce6d68d0e4a68878de32ec19cb0bcf78a32f9f8f1a1c843b7619128dd5561

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