Skip to main content

krnl

krnl is a lightweight, educational machine learning library written in Python on top of NumPy, with experimental OpenCL support for device-aware tensor computation

The project intentionally stays small, readable, and easy to follow rather than being production ready. docs

Highlights

  • Tensor-based autograd engine
  • NumPy-backed CPU execution
  • Experimental OpenCL backend for GPU-style computation
  • Basic neural network building blocks
  • Optimizer implementations for training
  • Common loss functions for regression and learning tasks

Installation

Create and activate a virtual environment:

python -m venv .venv
source .venv/bin/activate

Install the package:

pip install krnl

Optional OpenCL setup

To use the OpenCL backend, make sure your system has a compatible OpenCL runtime and drivers installed.

Quick start

import numpy as np

from krnl import nn
from krnl import Tensor


def main():
    dx = []
    dy = []

    for i in range(10):
        dx.append(float(i))
        dy.append(float(i * 2 + 1))

    model = nn.Linear(
        in_features=1,
        out_features=1,
        bias=True,
    )

    criterion = nn.loss.MSELoss()

    optimizer = nn.optim.MuonAdamAux(
        model._parameters.values(),
        learning_rate=0.001,
    )

    weight = model._parameters["weight"]
    bias = model._parameters["bias"]

    print("Initial weight:", weight.data, "| Initial bias:", bias.data)

    for epoch in range(100):
        total_loss = 0.0

        for x_value, y_value in zip(dx, dy):
            optimizer.zero_grad()

            x = Tensor([[x_value]])
            target = Tensor([[y_value]])

            prediction = model(x)
            loss = criterion(prediction, target)
            loss.backward()
            optimizer.step()

            total_loss += float(np.asarray(loss.data).reshape(-1)[0])

        if epoch % 10 == 0:
            average_loss = total_loss / len(dx)
            print(
                f"epoch={epoch:03d}, "
                f"loss={average_loss:.6f}, "
                f"weight={weight.data}, "
                f"bias={bias.data}"
            )

    print("\nFinal predictions:")

    for x_value, expected_value in zip(dx, dy):
        x = Tensor([[x_value]])
        prediction = model(x)
        print(
            f"x={x_value:.1f}, "
            f"predicted={prediction.data}, "
            f"expected={expected_value:.1f}"
        )


if __name__ == "__main__":
    main()

This example builds a small computational graph, computes a scalar loss, and backpropagates gradients through the graph.

Core concepts

Tensor

The Tensor class is the foundation of the project. It stores numeric data, tracks computation history, and supports differentiation through custom arithmetic and autograd operations.

Key features include:

  • scalar and array arithmetic
  • forward-pass tracking
  • backward gradient propagation
  • device awareness for CPU/GPU execution
  • support for differentiable operations used in model training

Device abstraction

The Device abstraction allows the library to reason about whether computation should happen on CPU or a GPU-like backend. This keeps the design simple while making it easier to experiment with accelerators and custom execution paths.

OpenCL backend

The OpenCL path uses PyOpenCL to move arrays to an accelerator, execute simple kernels, and support basic tensor operations. It is useful for experimentation and for understanding how low-level device execution fits into a training pipeline.

Neural network utilities

The library includes a minimal nn module with building blocks for simple models.

Module and Parameter

Module is the base class for neural network components. It provides a simple interface for registering trainable parameters.

Parameter wraps a tensor and marks it as trainable so that it participates in gradient-based optimization.

Layers

The library includes a few basic layer types:

  • Linear: a dense layer implementing y = xW^T + b
  • ReLU: elementwise rectified linear activation
  • Sigmoid: elementwise sigmoid activation

Example:

from nn.model_utils import Linear, ReLU
from backends.backends.tensor import Tensor

layer1 = Linear(in_features=4, out_features=8)
activation = ReLU()

x = Tensor([[1.0, 2.0, 3.0, 4.0]])
y = activation(layer1(x))

Loss functions

The project includes a small set of regression-style loss implementations in nn.

MSELoss

Mean squared error:

(predicted - target) ** 2

This computes the squared error across the prediction and is commonly used for regression tasks.

MAELoss

Mean absolute error:

abs(predicted - target)

This is useful when you want the average magnitude of the prediction error without squaring it.

HuberLoss

A smooth piecewise loss that behaves like squared error for small residuals and absolute error for larger residuals. It is often used to reduce sensitivity to outliers.

Example:

from nn import MSELoss, MAELoss, HuberLoss

criterion = MSELoss()
loss = criterion(predictions, targets)

Optimizers

The library includes several optimization strategies in nn.

SGD

Stochastic Gradient Descent updates parameters by moving them in the direction opposite the gradient:

parameter = parameter - learning_rate * gradient

This is the simplest gradient-based optimizer and is useful for understanding the basics of training.

Adam

Adam combines momentum and adaptive learning rates using first and second moment estimates. It is one of the most common modern optimizers and is useful for training neural networks more stably than plain SGD.

Muon

Muon is a momentum-based optimizer with an orthogonalized update direction. It introduces a more structure-aware update strategy and is included as an experimental optimizer in the library.

MuonAdam

This optimizer combines Muon-style orthogonal updates with Adam-like moment estimates, making it a hybrid approach for experimentation and learning.

Example:

from nn.optim import SGD, Adam, Muon, MuonAdam

sgd = SGD(parameters=model_parameters, learning_rate=0.01)
adam = Adam(parameters=model_parameters, learning_rate=0.001)
muon = Muon(parameters=model_parameters, learning_rate=0.02)
muon_adam = MuonAdam(parameters=model_parameters, learning_rate=0.02)

Training workflow

A typical training loop looks like this:

from nn import MSELoss
from nn.optim import SGD

model = ...
criterion = MSELoss()
optimizer = SGD(model._parameters.values(), learning_rate=0.01)

for batch_x, batch_y in data:
    predictions = model(batch_x)
    loss = criterion(predictions, batch_y)

    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

Repository structure

krnl/
├── README.md
├── __init__.py
├── requirements.txt
├── version.txt
├── src/
│   ├── backends/
│   │   ├── GPU_processer.py
│   │   ├── __init__.py
│   │   ├── device.py
│   │   ├── kernels.py
│   │   └── tensor.py
│   ├── nn/
│   │   ├── __init__.py
│   │   ├── loss.py
│   │   ├── model_utils.py
│   │   └── optim.py
│   └── tests/
│       ├── test_opencl_backend.py
│       ├── test_simple_model_implementation.py
│       └── test_tensor.py
└── .gitignore

Running tests

From the project root:

python src/tests/test_tensor.py
python src/tests/test_opencl_backend.py

The OpenCL tests require a valid OpenCL installation and compatible device support.

Project status

krnl is an educational framework for understanding the internals of deep learning systems. It is not intended to replace established libraries such as PyTorch or TensorFlow.

Current focus areas include:

  • tensor arithmetic and autograd
  • gradient-based optimization
  • CPU execution with NumPy
  • GPU/OpenCL experimentation
  • small neural network primitives

Contributing

Contributions are welcome in areas such as:

  • more layer types
  • additional loss functions
  • more optimization algorithms
  • clearer documentation
  • stronger tests
  • device and performance improvements

The project remains intentionally simple and readable so it can serve as a learning tool.

License

This repository does not currently include a license file. If you plan to distribute or publish it publicly, consider adding a license such as MIT or Apache 2.0.

Acknowledgements

This project is built for experimentation and education in tensor math, computational graphs, optimization, and device-aware execution.

Metadata

Release files for krnl 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for krnl 0.6.1
File Size Uploaded Provenance Actions
krnl-0.6.1.tar.gz 25.7 kB Without provenance Details for krnl-0.6.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for krnl 0.6.1
File Interpreter ABI Platform Actions
krnl-0.6.1-py3-none-any.whl Python 3 none any Details for krnl-0.6.1-py3-none-any.whl

Total release size: 47.2 kB

Release files / krnl-0.6.1.tar.gz

Download URL krnl-0.6.1.tar.gz
Size 25.7 kB
Tags Source
SHA-256 checksum
How to use checksums
344f0abb6660972a9c07ecc8a541b06d167319e8bb55d6477b1ac170fa115c92
BLAKE2b-256 checksum
How to use checksums
377850ed0c7b5661fb8e46e6fbeb624168b32f3454a8d0ddfd5d10876cd0e12a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / krnl-0.6.1-py3-none-any.whl

Download URL krnl-0.6.1-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b20f6216fa870303cf16bc1e073a207fd63d55b14d5f66505c4cd940f38bfcf
BLAKE2b-256 checksum
How to use checksums
330f33f7c4595e0ed3d1582aaea05779e64b01ebde6c29b81b13e4ad6ed5b10a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for krnl 0.6.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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