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. A model can be provided as a JSON file (format: see below), DNNF file (format: see dnnf.md) or created dynamically at runtime. A model can also be exported to both JSON and DNNF.

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.json.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_json(&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_json("sample_model.json")
input = [-3.5987e-01, -4.6701e-01, 1.6226e+00, 5.3634e-02]
output = model.forward_pass(input)

JSON format

Models can be stored in a JSON format. This is simply an object. The object has two attributes: input_size and layers. input_size is the size of the input that the model accepts. layers is an array of Layers. Each layer has a type attribute that describes the layer type and a size attribute describing the size of the layer (the number of outgoing values). A layer type can be one of the following:

  • linear
  • layer_norm
  • elu
  • leaky_relu
  • 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",
    "size": 3,
    "weight": [
        [0.1, 0.2],
        [0.4, 0.4],
        [0.5, 0.6]
    ],
    "bias": [0.1, 0.2, 0.3]
}

Layer Normalization

This is an example for a layer normalization layer.

{
    "type": "layer_norm",
    "size": 3,
    "weight": [0.1, 0.2, 0.3],
    "bias": [0.1, 0.2, 0.3],
    "eps": 0.00001
}

eps is an optional attribute with a default value of 1e-5.

ELU

This is an example for an ELU layer.

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

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

LeakyReLU

This is an example for an ELU layer.

{
    "type": "leaky_relu",
    "size": 3,
    "negative_slope": 0.1
}

negative_slope is an optional attribute with a default value of 0.01.

ReLU

This is the representation of a ReLU layer.

{
    "type": "relu",
    "size": 3
}

ReLU6

This is the representation of a ReLU6 layer.

{
    "type": "relu6",
    "size": 3
}

Sigmoid

This is the representation of a sigmoid layer.

{
    "type": "sigmoid",
    "size": 3
}

Tanh

This is the representation of a Tanh layer.

{
    "type": "tanh",
    "size": 3
}

Clip

This is an example for a clipping layer.

{
    "type": "clip",
    "size": 3,
    "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.2.0.tar.gz (13.1 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.2.0-py3-none-win_arm64.whl (215.2 kB view details)

Uploaded Python 3Windows ARM64

dnnets-0.2.0-py3-none-win_amd64.whl (233.4 kB view details)

Uploaded Python 3Windows x86-64

dnnets-0.2.0-py3-none-win32.whl (252.9 kB view details)

Uploaded Python 3Windows x86

dnnets-0.2.0-py3-none-musllinux_1_2_x86_64.whl (607.6 kB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

dnnets-0.2.0-py3-none-musllinux_1_2_s390x.whl (898.2 kB view details)

Uploaded Python 3musllinux: musl 1.2+ s390x

dnnets-0.2.0-py3-none-musllinux_1_2_riscv64.whl (731.2 kB view details)

Uploaded Python 3musllinux: musl 1.2+ riscv64

dnnets-0.2.0-py3-none-musllinux_1_2_ppc64le.whl (579.1 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ppc64le

dnnets-0.2.0-py3-none-musllinux_1_2_i686.whl (823.1 kB view details)

Uploaded Python 3musllinux: musl 1.2+ i686

dnnets-0.2.0-py3-none-musllinux_1_2_armv7l.whl (854.2 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARMv7l

dnnets-0.2.0-py3-none-musllinux_1_2_aarch64.whl (596.6 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

dnnets-0.2.0-py3-none-manylinux_2_35_x86_64.whl (607.6 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ x86-64

dnnets-0.2.0-py3-none-manylinux_2_35_s390x.whl (899.0 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ s390x

dnnets-0.2.0-py3-none-manylinux_2_35_riscv64.whl (731.7 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ riscv64

dnnets-0.2.0-py3-none-manylinux_2_35_ppc64le.whl (579.4 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ ppc64le

dnnets-0.2.0-py3-none-manylinux_2_35_i686.whl (621.9 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ i686

dnnets-0.2.0-py3-none-manylinux_2_35_armv7l.whl (854.9 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ ARMv7l

dnnets-0.2.0-py3-none-manylinux_2_35_aarch64.whl (597.8 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ ARM64

dnnets-0.2.0-py3-none-macosx_11_0_arm64.whl (173.4 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

dnnets-0.2.0-py3-none-macosx_10_7_x86_64.whl (185.8 kB view details)

Uploaded Python 3macOS 10.7+ x86-64

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.0.tar.gz
Algorithm Hash digest
SHA256 89d423fc08101f6e612c91e83c6e96b7ed1bcde4c8883fda7532fa2da2a593ac
MD5 8a1de87d6d6f2d97859075554fac6978
BLAKE2b-256 15139a9452a3a06feb69a01a8354fc98d07f0c1f1eba142e4a493ca0ff800c53

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 341de51f1645e7ca12837696d971821397825c7f17341f3733f1665d6248df4b
MD5 4c24bcd2f3a9c0e9b1fa1aa3b34ea11b
BLAKE2b-256 fe04eb3fc533a0c971f085d93d52cd2fb18085c96a788682bf1fa74abba93fd4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 301cea0905856fd84486c186a2dc57e3798faf599454bf1bf67a45879b48fd27
MD5 77e020b571a0fc78b89e26038b78a38d
BLAKE2b-256 3fc69d14b3f8cabc6eeaf860594768e12d5eddda4c85bf991f2087c10d594286

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 38fe6fd51435cbd7bb03a76a2d0ed0226ad2c4a7a052d80fbbc8dc3ce06838d0
MD5 9402591ac566e4146486a8c8752610fb
BLAKE2b-256 6ed83daa9d108369d6f3794c4337843569ea272d1f31b0c906a150c58c6c27c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8758a79f84dc3db8299ed7250dfc98ad279f60983127274547c4a54284b5c55e
MD5 c52c0d012e1f20a942d5365632e42b96
BLAKE2b-256 e4915f4509288f6f69480a6432f3adf38d6cc01648a94c9abf10cbb6d4740f1c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.0-py3-none-musllinux_1_2_s390x.whl
Algorithm Hash digest
SHA256 d08731853a35ba4fabefe07b3bdbb4e693d367a08af32edce8aac0670e0380f6
MD5 f7328f2a80f93aaca1a2bf504456112f
BLAKE2b-256 4bf6239f868fcfb7d36261f364fab69a03e58698f21501a67bdabe140dc360eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-musllinux_1_2_riscv64.whl
Algorithm Hash digest
SHA256 0f2fcbfc321d5471b65d2c278fa922f6bdd54b251381eddaab291558988d61a1
MD5 d3049b83c2a72dc20f7b9b8984dbfd2a
BLAKE2b-256 d33fa5ed7a7bbc2e348304ddf09b3a1bd2d5cbf9b290e47f5118c4a0d6df504a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 7371d028f00f30d16d6cbc04aaf5b5ccfd7f2117c8bbf8398f4324853e95125b
MD5 b69d0fe329ee3a4a3f51de559aa34bf9
BLAKE2b-256 abd6377886a78c00d300e9f58d1802c18033d7de886c2fbec91dfecb9b92fe33

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.0-py3-none-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 c7394194a7dbd6ca3be76da0f51fee1f037caf535d9e67da17556117f2380bd1
MD5 f5bcf7d279dde04707dda61a9bdc09d0
BLAKE2b-256 5bd22df77e2aef1fe5e692f796d83d48ac6b0c2305857a684fffa0292d9efb89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 57176520d8ed4ed739cc678b9661c45a6a77cf3b7c1c8fb01ec370f8ec3a66b1
MD5 7d9d48ad3bf9ce05dbf5249f60ba018b
BLAKE2b-256 db981d35dbf45b023d8ecba3eea80c8934f5c26cd46c5a1a1d31f26dd9232f4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 e9b5b8a7fb51cbe36e887dd747490fe1e3eaa60078ea60567454439637b3a6d2
MD5 84a68031661b2f6f32132b6560ea4276
BLAKE2b-256 7da89a71ab4f8260fb3a17b647c394ac868348a9687fb4f0e5871dfb49068f78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 506ed519073d6063a3533a53c4e4324492a2da05990f7cfb86fd8a76587a5348
MD5 a7d8ee4d93e14f15997658e6c60af941
BLAKE2b-256 dfafd2aa0380e28f60de6e8c554a5674dbc4b6c4e35551f65b06466b4e9f5848

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-manylinux_2_35_s390x.whl
Algorithm Hash digest
SHA256 ee3d1e049ad8f51fa2d06733ec92974e4100299e15d53453cab4667a487ae150
MD5 297feab1eb1724369f37d79d52e7c3a2
BLAKE2b-256 816ee5da62fd659b79e9e49c009708124635cf8668498e8927883c4a825efc7e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-manylinux_2_35_riscv64.whl
Algorithm Hash digest
SHA256 6509ae57d1d32bb172060451d82f1a41cfb0f46e4182e84612b812dd30f73bf0
MD5 ba61928560b55786ebdca9d2dc67d329
BLAKE2b-256 e4539feaebbaa55739256c653902cbbf63dcf148bc623392798c92b7f664c13e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-manylinux_2_35_ppc64le.whl
Algorithm Hash digest
SHA256 528ad7da5826d174bdb8af365db69139b95bd07c7f36ef4b3faea708ab1a57d6
MD5 9d407888545281958e77fe559d410455
BLAKE2b-256 6fb2bc120da1e8159fe4f138af4a78f3ba6ea0420fdc3467d2c311435aa555fc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.0-py3-none-manylinux_2_35_i686.whl
Algorithm Hash digest
SHA256 393be794e43f972a3fd470a238c07da35e7fbe8d5970159b6bb059bf54138814
MD5 a04a648c24cc543ea409a49bb88c3337
BLAKE2b-256 6d4099d877168d5bf09e72293b43fbe6c8c169377db5cbb0c7da716a67a15112

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-manylinux_2_35_armv7l.whl
Algorithm Hash digest
SHA256 9d4e7eb724d9c2778bb5ef3bca478baefc4e4bfe96cc146741a3a62d028285b3
MD5 2686ca35f8a43c29d561d4e51892a54e
BLAKE2b-256 0b6a614e718a2ae751ead79edcd310311286ad36ea63f1bea8ea3401e1d89f7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 20b1cdd8215d3743f96640749dd7fa7c38a5523410a25f4f6d0f925cfc4893be
MD5 4ab089fd190eef0ad0b40e1f83ec18e7
BLAKE2b-256 a1ccbbb883fcc97c96e48713b311bd8d4af1bda85063d7b85b5839af41dc919f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 93029183de1ebb44e12aa9bb744af099d37de90c7297c450fcb572397b1e1114
MD5 ffe48fac098a87d3cd2eb034ccacfc21
BLAKE2b-256 64dd98e7deed1a37b6a0e3670adc24c44eef211a48e787afa3a4ba2024852518

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.0-py3-none-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 7723faf0f9235514195c36df2be40318eebed2d02422bf3c5a9e84b31cfe2a76
MD5 54e17e2869ab35668f53704f796c43e0
BLAKE2b-256 ca9e6357aa15ae23678d845e16788d1370239fd22ab070afdd13050379dc51ae

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