Skip to main content

Aakaar

Aakaar is a custom, standalone deep learning library built from the ground up using Python, C++, and raw CUDA. It implements N-dimensional tensors, a broadcasting-aware reverse-mode autograd engine, and a small set of neural network building blocks — all without relying on PyTorch, TensorFlow, or any other heavy external framework.

Core Architecture

Aakaar's Tensor is a custom C++ object that can live directly in GPU VRAM or in host memory, exposed to Python via pybind11. Tensors carry their own shape and strides, so operations like slicing, transposing, and reshaping return lightweight zero-copy views into the same underlying memory wherever possible — data only moves when you explicitly ask for it via .to_numpy() or .to(device).

Every differentiable operation records itself into a dynamic computation graph (grad_fn), which .backward() walks in reverse topological order to compute gradients — the same fundamental design as PyTorch's autograd, built independently from scratch.

Current capabilities

Tensors

  • N-dimensional tensors on CPU or CUDA, with real shape/stride tracking
  • Zero-copy views: slicing (t[1:3, 2:4], negative indices, step slicing), .transpose(), .T (full axis reversal), .view() / .reshape()
  • .contiguous() to materialize a view when an operation requires dense memory
  • from_numpy() to load real data in; .to_numpy() to get it back out
  • .to(device) / .to_device() to move tensors between CPU and CUDA

Autograd

  • requires_grad, .grad, .backward() with correct gradient accumulation across branching (diamond) graphs
  • retain_graph support for reusing a graph across multiple backward passes
  • no_grad() context manager and .detach() for inference / parameter-update code that shouldn't be tracked
  • Broadcasting-aware gradients for every elementwise and matmul operation, verified against numerical (finite-difference) gradients, not just symbolic derivation

Operations

  • Elementwise: +, -, *, / (tensor-tensor and tensor-scalar, with full broadcasting), unary negation
  • matmul() / @: N-dimensional, batched, with broadcasting batch dimensions on both forward and backward
  • Reductions: sum(dim=...), sum() (full reduction), max(dim=...) (with correct argmax-routed gradients)
  • Activations: relu, sigmoid, tanh, leaky_relu — all with float4-vectorized CUDA kernels and an alignment-safe scalar fallback
  • exp(), log()
  • softmax() (numerically stable, max-subtraction based) and cross_entropy_from_probs()

Neural network building blocks (aakaar.nn, aakaar.optim)

  • nn.Linear — a fully-connected layer with standard uniform initialization
  • optim.SGD — gradient descent optimizer using in-place parameter updates (copy_()) so parameter objects keep their identity across training steps
  • zero_grad_all() for clearing gradients across a parameter list

Automatic CPU fallback

  • If no CUDA toolkit is available at install time, Aakaar builds a CPU-only extension automatically. device="cpu" works everywhere; device="cuda" raises a clear error on CPU-only builds instead of failing to install.

Installation

pip install aakaar

Prebuilt wheels are available for Windows (Python 3.10–3.14, with CUDA support). On other platforms, pip builds Aakaar from source — this requires a C++ compiler (e.g. g++) for CPU-only support, and additionally the NVIDIA CUDA Toolkit (nvcc) for GPU acceleration. If no CUDA toolkit is found at install time, Aakaar automatically builds a CPU-only extension.

Quick start: tensors and autograd

import aakaar
import numpy as np

# Load real data
x = aakaar.from_numpy(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), requires_grad=True)

# Standard ops, all differentiable
y = (x * 2 + 1).sum()
y.backward()
print(x.grad.to_numpy())  # [[2. 2.] [2. 2.]]

# Zero-copy slicing and views
big = aakaar.rand((10, 10), device="cpu")
view = big[1:5, 1:5]
print(view.is_contiguous())  # False — it's a strided view, no data copied

# Move to GPU
gpu_tensor = x.to("cuda")

Training a small neural network

import aakaar
from aakaar.nn import Linear
from aakaar.optim import SGD
import numpy as np

# Synthetic data: y = sin(x)
N = 64
x_np = np.linspace(-3, 3, N).reshape(N, 1).astype(np.float32)
y_np = np.sin(x_np).astype(np.float32)
x = aakaar.from_numpy(x_np)
y = aakaar.from_numpy(y_np)

fc1 = Linear(1, 16)
fc2 = Linear(16, 1)

def forward(x):
    h = fc1(x).tanh()
    return fc2(h)

def mse_loss(pred, target):
    diff = pred - target
    return (diff * diff).sum() / pred.size

params = fc1.parameters() + fc2.parameters()
opt = SGD(params, lr=0.05)

for epoch in range(300):
    opt.zero_grad()
    loss = mse_loss(forward(x), y)
    loss.backward()
    opt.step()

print(f"final loss: {loss.item():.6f}")

Notes and known limitations

  • matmul requires contiguous tensors; call .contiguous() on sliced/transposed operands first.
  • Elementwise CUDA kernels also require contiguous inputs for their fast vectorized path.
  • matmul backward supports broadcasting batch dimensions, but not yet arbitrary mixed-rank batch shapes beyond standard right-aligned broadcasting rules.
  • Only float32 is currently supported. Support for additional dtypes (float16, float64, int types) is a planned future addition, not yet implemented.
  • This is an actively developed project; APIs may change between minor versions.

Download files

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

Source Distribution

aakaar-0.1.8.tar.gz (28.4 kB view details)

Uploaded Source

Built Distributions

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

aakaar-0.1.8-cp314-cp314-win_amd64.whl (282.7 kB view details)

Uploaded CPython 3.14Windows x86-64

aakaar-0.1.8-cp313-cp313-win_amd64.whl (276.0 kB view details)

Uploaded CPython 3.13Windows x86-64

aakaar-0.1.8-cp312-cp312-win_amd64.whl (276.0 kB view details)

Uploaded CPython 3.12Windows x86-64

aakaar-0.1.8-cp311-cp311-win_amd64.whl (274.6 kB view details)

Uploaded CPython 3.11Windows x86-64

aakaar-0.1.8-cp310-cp310-win_amd64.whl (272.8 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

Details for the file aakaar-0.1.8.tar.gz.

File metadata

  • Download URL: aakaar-0.1.8.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for aakaar-0.1.8.tar.gz
Algorithm Hash digest
SHA256 4948d0a402e594f867302ff51a4d32a6459b35656be3263f5698b81eb94bbae8
MD5 357145f1f3b2a5ff53e366ec68bf6448
BLAKE2b-256 0670dd4a6b328df8bd0b5845a851bde7cf9d29563ebdedc90ad29861ede71375

See more details on using hashes here.

File details

Details for the file aakaar-0.1.8-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: aakaar-0.1.8-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 282.7 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for aakaar-0.1.8-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e50c6589ff32aef7fc03849122000f8c3876828e14d98d0ba8f88539e7052997
MD5 8dbed461e8c21d303292edb9101697dc
BLAKE2b-256 ccc6d2058491a1c42ef9876c9569bc5d35e7add1e163e3ad222f23f9bffb6671

See more details on using hashes here.

File details

Details for the file aakaar-0.1.8-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: aakaar-0.1.8-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 276.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for aakaar-0.1.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 583be232db4aa0ea9a8ab9bf37cc7029d846c4c41e5b351ff4ba3b9255457f06
MD5 ffeb3a7318ac97a8b7259b5e02378aee
BLAKE2b-256 f476a31e5f01bd52a37408fc6da765489ddfff3ceb25d4cc904f3b67ec977684

See more details on using hashes here.

File details

Details for the file aakaar-0.1.8-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: aakaar-0.1.8-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 276.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for aakaar-0.1.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c4c26bfd6f42fe823027f6154e513b36b24a673d8392d6b27dc560f2a5d62361
MD5 70987b8f7f5e8ed6c1112a31209624b3
BLAKE2b-256 534b5054d7a85f1b41c146faf5f2f806b7ee68167c3172330b58cdf8c3354297

See more details on using hashes here.

File details

Details for the file aakaar-0.1.8-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: aakaar-0.1.8-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 274.6 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for aakaar-0.1.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c7e1eda091aa7572c1a2ada96c457430fa3de4f1acd38350f09aead7c136a7d0
MD5 a6a20b159566350d6a2ee8408cdbd016
BLAKE2b-256 f1786c0a918a1125033f16f810d0ab9a9e7dd85263898c35f8885777c7c3e0e5

See more details on using hashes here.

File details

Details for the file aakaar-0.1.8-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: aakaar-0.1.8-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 272.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for aakaar-0.1.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8b6a29b050921e6681deb37af9b33440a5561d6379d582827d7b5252cd8681e4
MD5 9eb1a5512319f275acabd7da127d6e52
BLAKE2b-256 fb97f192608e79510e3d846b342618b3e5b78395a8b8ddca72730ac74adb30da

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