Skip to main content

Running multilayer perceptrons on the CPU

Project description

dnnets

This is a lightweight and dependency-free library to run multilayer perceptrons on the CPU. Given a model and an input, it will compute the output of a forward pass. Nothing more. The computation of a single pass runs in a single thread. Generally, the library tries to make a good trade-off between simplicity and performance. The model can be provided as a JSON file (format: see below) or created dynamically at runtime.

Note: in this library, activation functions are also modeled as layers.

Installation

To build and install the shared library along with the header file and a pkg-config file to /usr/local (or another location of your choice), use the (default) install target:

zig build install -Doptimize=ReleaseFast
zig build install -Doptimize=ReleaseFast -p /usr/local

To automaticallly build a Python wheel with the provided build script, you need to have Hatch installed. For bundling the Python bindings together with the shared library into a wheel, use build-wheel-with-lib:

zig build build-wheel-with-lib -Doptimize=ReleaseFast

If you only want to include the bindings without the shared library, use build-wheel:

zig build build-wheel

In both cases, you will find the generated wheel in zig-out/dist.

If you'd like to use your own frontend for building the Python wheel, you can use the prepare-wheel-with-lib and prepare-wheel targets instead:

zig build prepare-wheel-with-lib -Doptimize=ReleaseFast
# or
zig build prepare-wheel

You can then navigate to zig-out/wheel-builds/<your target> and invoke the frontend of your choice there. Note that hatchling is still used as the backend. <your-target> is either any if the wheel does not contain the shared library or a combination of <CPU architecture>-<OS>-<ABI> otherwise.

If you need to cross compile/build the Python wheel, you can use build-all-wheels which builds the wheels for all available targets:

zig build build-all-wheels -Doptimize=ReleaseFast

Using from Zig

This package exposes a module called dnnets that you can use in your own Zig code. Simply add this package to the dependencies in your build.zig.zon. In your build file, you can add the module as an import to your own module with something similar to this:

const dnnets_pkg = b.dependency("dnnets", .{});
const dnnets_mod = dnnets_pkg.module("dnnets");
exe_mod.addImport("dnnets", dnnets_mod);

The API is available as an online documentation at https://hannesbraun.net/share/doc/dnnets/zig

This is a small example of its usage:

var model = try dnnets.load(allocator, "sample_model.json");
defer model.deinit();
var input = [_]f32{ -3.5987e-01, -4.6701e-01, 1.6226e+00, 5.3634e-02 };
const output = model.getOutputBuffer(); // The output buffer will be reused.
model.forwardPass(&input);

C interface

dnnets has a C-compatible ABI. Documentation for the C interface is available in the header at include/dnnets.h.

If you don't like browsing the header file directly, there's also an online version of this documentation available: https://hannesbraun.net/share/doc/dnnets/c

This is the example provided above adjusted for C (without error handling):

dnnets_init(); // Initialize library

struct dnnets_model *model;
dnnets_load(&model, "sample_model.json");

const float input[] = { -3.5987e-01, -4.6701e-01, 1.6226e+00, 5.3634e-02 };
const float* out_buf = dnnets_get_output_buffer(model);
const unsigned int out_len = dnnets_get_output_len(model);
dnnets_forward_pass(model, input, 4);

dnnets_free_model(model);

dnnets_deinit(); // Deinitialize library

Python bindings

dnnets provides Python bindings. You can install the package (including the native library) via the PyPI:

pip install dnnets

You can find a documentation of the Python API here: https://hannesbraun.net/share/doc/dnnets/python

The Python bindings can optionally be used together with NumPy. This feature can be enabled using dnnets.enable_numpy(). Then, all weights, biases and inputs need to be NumPy arrays. Outputs will then also be NumPy arrays.

Here's a quick example:

import dnnets

model = dnnets.load("sample_model.json")
input = [-3.5987e-01, -4.6701e-01, 1.6226e+00, 5.3634e-02]
output = model.forward_pass(input)

JSON format

Models are stored in a JSON format. This is simply an array of layers. Each layer has a type attribute that describes the layer type. A layer type can be one of the following:

  • linear
  • layer_norm
  • elu
  • relu
  • relu6
  • sigmoid
  • tanh
  • clip

You can also have a view at sample_model.json to see how this looks as a whole.

Linear

This is an example for a linear layer where the previous layer has 2 outputs and this layer has 3 outputs.

{
    "type": "linear",
    "weights": [
        [0.1, 0.2],
        [0.4, 0.4],
        [0.5, 0.6]
    ],
    "biases": [0.1, 0.2, 0.3]
}

Layer Normalization

This is an example for a layer normalization layer.

{
    "type": "layer_norm",
    "weights": [0.1, 0.2, 0.3],
    "biases": [0.1, 0.2, 0.3],
    "eps": 0.00001,
}

ELU

This is an example for an ELU layer.

{
    "type": "elu",
    "alpha": 0.9,
}

alpha is an optional attribute with a default value of 1.0.

ReLU

This is the representation of a ReLU layer.

{
    "type": "relu",
}

ReLU6

This is the representation of a ReLU6 layer.

{
    "type": "relu6",
}

Sigmoid

This is the representation of a sigmoid layer.

{
    "type": "sigmoid",
}

Tanh

This is the representation of a Tanh layer.

{
    "type": "tanh",
}

Clip

This is an example for a clipping layer.

{
    "type": "clip",
    "min": -3.0,
    "max": 3.0,
}

License

This library is licensed under the terms of the GNU Lesser General Public License (version 3 only) as published by the Free Software Foundation. For more information, see LICENSE.

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

dnnets-0.1.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distributions

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

dnnets-0.1.1-py3-none-win_arm64.whl (205.0 kB view details)

Uploaded Python 3Windows ARM64

dnnets-0.1.1-py3-none-win_amd64.whl (223.4 kB view details)

Uploaded Python 3Windows x86-64

dnnets-0.1.1-py3-none-win32.whl (241.5 kB view details)

Uploaded Python 3Windows x86

dnnets-0.1.1-py3-none-musllinux_1_2_x86_64.whl (569.2 kB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

dnnets-0.1.1-py3-none-musllinux_1_2_s390x.whl (793.6 kB view details)

Uploaded Python 3musllinux: musl 1.2+ s390x

dnnets-0.1.1-py3-none-musllinux_1_2_riscv64.whl (668.8 kB view details)

Uploaded Python 3musllinux: musl 1.2+ riscv64

dnnets-0.1.1-py3-none-musllinux_1_2_ppc64le.whl (542.3 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ppc64le

dnnets-0.1.1-py3-none-musllinux_1_2_i686.whl (785.0 kB view details)

Uploaded Python 3musllinux: musl 1.2+ i686

dnnets-0.1.1-py3-none-musllinux_1_2_armv7l.whl (812.8 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARMv7l

dnnets-0.1.1-py3-none-musllinux_1_2_aarch64.whl (559.4 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

dnnets-0.1.1-py3-none-manylinux_2_35_x86_64.whl (569.3 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ x86-64

dnnets-0.1.1-py3-none-manylinux_2_35_s390x.whl (794.2 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ s390x

dnnets-0.1.1-py3-none-manylinux_2_35_riscv64.whl (669.3 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ riscv64

dnnets-0.1.1-py3-none-manylinux_2_35_ppc64le.whl (542.8 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ ppc64le

dnnets-0.1.1-py3-none-manylinux_2_35_i686.whl (585.8 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ i686

dnnets-0.1.1-py3-none-manylinux_2_35_armv7l.whl (814.1 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ ARMv7l

dnnets-0.1.1-py3-none-manylinux_2_35_aarch64.whl (560.4 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ ARM64

dnnets-0.1.1-py3-none-macosx_11_0_arm64.whl (162.5 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

dnnets-0.1.1-py3-none-macosx_10_7_x86_64.whl (174.2 kB view details)

Uploaded Python 3macOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: dnnets-0.1.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dnnets-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c689cc139c28aaa1f27e18f78d3386b53b63fc43c7389dd3fb5f48d2d4bfbe85
MD5 ae400410725e1f8921d0e83c3229a61d
BLAKE2b-256 190925ebaf91d84fd63027d16f4bedfe37cceab6b39a6db31f84fb94ad7ebda5

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-win_arm64.whl.

File metadata

  • Download URL: dnnets-0.1.1-py3-none-win_arm64.whl
  • Upload date:
  • Size: 205.0 kB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dnnets-0.1.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 2646ffab2b66d2de9fcb0ee90308b01abbbfa9cda44941574e480363f78c83f8
MD5 60cfafca1b1ac84d28b22c76d46ad4f1
BLAKE2b-256 d7a41d18b2f2f5037133bcba53729816722888556959b5e70bf9d28a79289d91

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: dnnets-0.1.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 223.4 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dnnets-0.1.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 fa7a5dedbd40bb7ccdad8fb80ec01e079a90410c6bf8e5aebc969c939afb14e3
MD5 0eef004e8d728bd9d9128a48e8999460
BLAKE2b-256 58159cefb7d0c01ecb768a7921959c49feb291d2e1a688ee1d45604ce735f7c4

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-win32.whl.

File metadata

  • Download URL: dnnets-0.1.1-py3-none-win32.whl
  • Upload date:
  • Size: 241.5 kB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dnnets-0.1.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 3c346d39d7aa10bc580b5f3d9ce2ebbc8271e623f9ba99fd2a6eb550832e6c06
MD5 0d9f72d24c16e5cb5daaaef7ab4c1510
BLAKE2b-256 d7b55e5e197ad84ff706d22a7fd101afdcbdfef540147e4b2fe1f8405fceed11

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 bdf42cf7853dff3144d660edeb03fb939a1225952f0f81d5692ea0a1a85ab74d
MD5 7ddef9c20ac896de7d6020e68727476a
BLAKE2b-256 52748d996b001a455397eb4ca54575ecedb8783020f7c9d59dabd3c360b4e37f

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-musllinux_1_2_s390x.whl.

File metadata

  • Download URL: dnnets-0.1.1-py3-none-musllinux_1_2_s390x.whl
  • Upload date:
  • Size: 793.6 kB
  • Tags: Python 3, musllinux: musl 1.2+ s390x
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dnnets-0.1.1-py3-none-musllinux_1_2_s390x.whl
Algorithm Hash digest
SHA256 e74cd6fa557dd4f22a8887c37fe23b7bbdcbd89a4ad51302fdcce6d91f133f59
MD5 e0d7e4eb6ded9b90a5931588362d6cde
BLAKE2b-256 aa425093e7117d344bbd101b3d273eecfe7e700fc314b4b05f860bd5eb563704

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-musllinux_1_2_riscv64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-musllinux_1_2_riscv64.whl
Algorithm Hash digest
SHA256 2c84ffbb75b9107a470fdee6dda25ca742fff3eb51fd2f7038cb9837ed75cd70
MD5 ab460494db69b74f6fb9afe6bfe0880d
BLAKE2b-256 8a5cc377804e69f717628cc8dfb4622b431ecc546a892ce76bc1c2113b397ca6

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-musllinux_1_2_ppc64le.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 5bb0f0b45a161aa43392e08b40041d9c12f518968ae9c5fe4b33cfd9b7ae2963
MD5 371d15b058553c4e43a147e1605ea24b
BLAKE2b-256 bf272f17bc8f91fdefede5323b15078ba3fa227f53da004858c347582f42e27c

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-musllinux_1_2_i686.whl.

File metadata

  • Download URL: dnnets-0.1.1-py3-none-musllinux_1_2_i686.whl
  • Upload date:
  • Size: 785.0 kB
  • Tags: Python 3, musllinux: musl 1.2+ i686
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dnnets-0.1.1-py3-none-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 233296b01bb9b3385d270dcd2557c64f85539c461915370623603954b384e542
MD5 7c49d789555fdb16745ae1f9698ad7ca
BLAKE2b-256 1cf2a60fe9a7e30674506c57488a16c6f6f21b013fa2385037eca8808d5d62a0

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 f7a52b2dbee51b9ebdc894737387513ca661fbec345aa441cfc26df1c34ed765
MD5 76c432b3e9a11bed0e7bd320bb96098a
BLAKE2b-256 115223118f24c768d445db8eebaad0f6ffe36e947fb15b4391a95f3601dfe4c9

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2f4f9bf75c36cdbbaa0c4684196df13ed423d57e6e9a06ec9ebc02459e318cd7
MD5 3dacd207d7e6c7b546488cc67dc86300
BLAKE2b-256 cca9e15e4efa96d1e97b60a0a65f408607a767a1664aa45a81618a5877865dbc

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 7c81e74072f15bf136cefc9a1d27ceda4f11c855299bd235cb2c30ac83e6bff9
MD5 77aaa7eba3226ae3847a44260c5e70f3
BLAKE2b-256 163bdfff18a9b61fd7752e84e1f551e73ca364e69ea27495f476e1ac73fa9539

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-manylinux_2_35_s390x.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-manylinux_2_35_s390x.whl
Algorithm Hash digest
SHA256 9e2b396590d76713591d662b4a84b685e9897ba117078b3c7bcaf5ff6c8d3cfd
MD5 aa431abd05cad341747db8a9da3fe888
BLAKE2b-256 7beced8dcb8b624cb78d41f193e27db7f8d5defc8372e10189eeac430e7552b0

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-manylinux_2_35_riscv64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-manylinux_2_35_riscv64.whl
Algorithm Hash digest
SHA256 7d2080ee18f909420e893ed7b1b5da45e2924a80a098a1718a966abde02a9737
MD5 1e0058f32339caa1d75c003ad7c60116
BLAKE2b-256 d4f05bc5094c791ad0d68f4e5463bc258f7a3e1df4e6a5df7d8902cbd0c9f004

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-manylinux_2_35_ppc64le.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-manylinux_2_35_ppc64le.whl
Algorithm Hash digest
SHA256 9844e1850e88f112181e65a135cb41b969704d0f2d21300b39766d06ff007ec0
MD5 8f3fbcda04e143b7f853c62c11723159
BLAKE2b-256 c814ea5a867d3d3cb25e71c8ed04182b262b4443d239ad879677d039aa234875

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-manylinux_2_35_i686.whl.

File metadata

  • Download URL: dnnets-0.1.1-py3-none-manylinux_2_35_i686.whl
  • Upload date:
  • Size: 585.8 kB
  • Tags: Python 3, manylinux: glibc 2.35+ i686
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for dnnets-0.1.1-py3-none-manylinux_2_35_i686.whl
Algorithm Hash digest
SHA256 7c28ffd46e2697b8315a5f9b2ce99778d4f18298fde1321b9817bd74eab5401b
MD5 4a8cd9aee1b0bd00015f8f1bfaa7c0fc
BLAKE2b-256 7db8637b889f2d444ff652275c2d3794fe0f02d1bd8e67cee1d9eedb2810a529

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-manylinux_2_35_armv7l.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-manylinux_2_35_armv7l.whl
Algorithm Hash digest
SHA256 a0e914b4b9df971a6516614038a4e1a99b02ba82356de9fcf880c42ddf5d61e4
MD5 a02cfdcd39bec5f8fa46500d2c1239b1
BLAKE2b-256 9f2223db8117e2657d8568693f11f5f9ef90650dec94ac00f3ad4ef0b515050f

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 cd1c7527a78627b533c2884ff866f59e28047321b1eb74234ddb87883af7d573
MD5 e0a421bac8d45f6587fcfe635f11f5e2
BLAKE2b-256 bd4fbf5d404bd5e870636263b3e8f8dc0f8ea4292747a1f7dbf1c4488d95b3cb

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 54c857af7aef1bd8d02f84a9e3ae7a8f38d10fd55d4b05ee6c94b0e9d5388800
MD5 d0143957efa5fac05d2ced263bca286f
BLAKE2b-256 bc52aac804349e88355044cbee4bf2e2a560270fee6de0d083832c9bcefb67c4

See more details on using hashes here.

File details

Details for the file dnnets-0.1.1-py3-none-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for dnnets-0.1.1-py3-none-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 921062251f6590fbf49151ab3b5a61470c009b94cc5945d29ca92eb7dec6c9b7
MD5 bccdaae2b2e17faeecfe310c8d208e83
BLAKE2b-256 e7879167fac2f81cecb48dfd6ff21047e18a2a743fec991477e5ea22f7b50536

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