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.2.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.2-py3-none-win_arm64.whl (306.0 kB view details)

Uploaded Python 3Windows ARM64

dnnets-0.2.2-py3-none-win_amd64.whl (319.8 kB view details)

Uploaded Python 3Windows x86-64

dnnets-0.2.2-py3-none-win32.whl (346.3 kB view details)

Uploaded Python 3Windows x86

dnnets-0.2.2-py3-none-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

dnnets-0.2.2-py3-none-musllinux_1_2_s390x.whl (1.5 MB view details)

Uploaded Python 3musllinux: musl 1.2+ s390x

dnnets-0.2.2-py3-none-musllinux_1_2_riscv64.whl (1.6 MB view details)

Uploaded Python 3musllinux: musl 1.2+ riscv64

dnnets-0.2.2-py3-none-musllinux_1_2_ppc64le.whl (1.2 MB view details)

Uploaded Python 3musllinux: musl 1.2+ ppc64le

dnnets-0.2.2-py3-none-musllinux_1_2_i686.whl (1.2 MB view details)

Uploaded Python 3musllinux: musl 1.2+ i686

dnnets-0.2.2-py3-none-musllinux_1_2_armv7l.whl (1.2 MB view details)

Uploaded Python 3musllinux: musl 1.2+ ARMv7l

dnnets-0.2.2-py3-none-musllinux_1_2_aarch64.whl (1.1 MB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

dnnets-0.2.2-py3-none-manylinux_2_35_x86_64.whl (1.2 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ x86-64

dnnets-0.2.2-py3-none-manylinux_2_35_s390x.whl (1.5 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ s390x

dnnets-0.2.2-py3-none-manylinux_2_35_riscv64.whl (1.6 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ riscv64

dnnets-0.2.2-py3-none-manylinux_2_35_ppc64le.whl (1.2 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ ppc64le

dnnets-0.2.2-py3-none-manylinux_2_35_i686.whl (990.0 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ i686

dnnets-0.2.2-py3-none-manylinux_2_35_armv7l.whl (1.2 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ ARMv7l

dnnets-0.2.2-py3-none-manylinux_2_35_aarch64.whl (1.1 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ ARM64

dnnets-0.2.2-py3-none-macosx_13_0_x86_64.whl (236.0 kB view details)

Uploaded Python 3macOS 13.0+ x86-64

dnnets-0.2.2-py3-none-macosx_13_0_arm64.whl (225.7 kB view details)

Uploaded Python 3macOS 13.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.2.tar.gz
Algorithm Hash digest
SHA256 44ae111487156de507f85616784cf4c9c20829c773c21be61f3fb5ca0ba83e8f
MD5 d3e057241f39ffb9f9f8ba5bda6aebee
BLAKE2b-256 c0fdfdc934bd08e26a6503bde3475fbf682584e895dedb5c5c1d084f3ebf5e78

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.2-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 69825cdcc58a028f4a50cf6caa7cada7415bd971c9f9d12fae5ccec8f05573e4
MD5 dd2391c8d8f1e8599f21ee3563e82034
BLAKE2b-256 8bf055a3e77becb60564b03e528e876e6ee50e19c04e059ecb6a0ec6f9e83660

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 935982a64a200cc6f9bc876a3ba647e77a57bd3fcbc27bf1c0553de6abb8fcfb
MD5 00e737010c2c5c67fe3a2f7485b3a3d6
BLAKE2b-256 4122776af27a0355dd79b763f337011bfd453f3167f4e88a51fd0cdc084e9b1b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.2-py3-none-win32.whl
Algorithm Hash digest
SHA256 51d9be7d877a0589fb4206596a603634c30957e93b56c2a3c0dce17017dae556
MD5 e3136281b4befb5b205237b4c72729af
BLAKE2b-256 d88cc98cdf5c0a9a8961db4e13fe9ff4b7c1691bc60cb3ba3d1ca8f99ab8af43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c756cd1fa90100d87d275c36e918dc2893f878d744cb69139e36b7a15ac4ac82
MD5 a397cb4c7df24947ec558c8932a09c03
BLAKE2b-256 b0f4d609b78d1b8a27cfdc867a13ce82e1b41b111cedd6a3f08d4db432584145

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-musllinux_1_2_s390x.whl
Algorithm Hash digest
SHA256 b6a09cd8760526997998cc8d604aafd05c54b7a530dae2a662aaf1dc2acc8f5b
MD5 517b5f9f14e2e97d61c27e7fe28a19a5
BLAKE2b-256 8f9cd5fb25c13b6c48392d7c6a0b9bfa68b841bdd742ea785c2d600e6b0e91f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-musllinux_1_2_riscv64.whl
Algorithm Hash digest
SHA256 bcf88422082f6197cfbc05395b55b2023a511d20271343d8fbad794793aa7384
MD5 feefe382834d4cf6aaa2fb973ee6bfc1
BLAKE2b-256 a1eb952943a0541250e5ffa8ca968d98298421f136ff96fe07423dd5c33b4ed3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 784f6985b645f2515a7b8100f026588258af01a80e75041d19963d75b8f22994
MD5 3a685393731da368ac051bfebb36887d
BLAKE2b-256 a080ad96da060e26b46f345814a036ab8fc203315deac30cf6eb1156e11b1ddb

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.2-py3-none-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 bc9c0ac3d3be56b410f0961741b443d76e5dfcc85f14bd35bca65c328a61d806
MD5 0b0994378c01cefbf9b5f7c9447cd635
BLAKE2b-256 5a2e87894b4091e2e32a60b6c402ee2753f8b02e5fafb50a5627be1e91a4b939

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 3c95b219d599f085fe26d1563766e58d3ba83f2a25694ceca562531c59a00633
MD5 34e099fbb3b1767fbbc75fbf09b4605b
BLAKE2b-256 46c105749b40898a54e6f696f14f7dbfafd567920545ded3ed369d80a343c7e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 bf183f1f365464a5ea83914d3b16b2eb8f4345d198f004f4b9aa60c0a68eee6b
MD5 9586493b5d200d486feb87351255b600
BLAKE2b-256 10e1d7b41b0e097f4ccd294ef89c938436574d00827d4548c7189b92d8a5c6fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 25594a55c990b8f41254bae98f2e5845ce362768afd9e003a64dc4755b7e40a2
MD5 877a34eedb84ccbe7774d47f6916d15e
BLAKE2b-256 c58320d2a5f217d361acfcb2407edd5315213579aa81328ba656b2148979f995

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-manylinux_2_35_s390x.whl
Algorithm Hash digest
SHA256 a865999de8b6e808c55cc5a743ef4d2c9d0dfa15182ded54674340167ccb6701
MD5 c6fe6504926bc5358bed9e83f477c63b
BLAKE2b-256 35c0246d6783d166a0ab57b6f73fb85fa2a9685db933f125df450e3e9fa266d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-manylinux_2_35_riscv64.whl
Algorithm Hash digest
SHA256 5fcca8f1050e7a5d3f28a249857f217983a54c3848cc69c652619c91074865ad
MD5 7f8229bce106542cd0599f576120a809
BLAKE2b-256 205254ca7bcf889e73f373e11e1b0b9a15d5932380329f6320353cacb712042c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-manylinux_2_35_ppc64le.whl
Algorithm Hash digest
SHA256 6d2a9fc2935ffc393c029722bd78cc8c8e2c964e25d8514855e07814d28a57a8
MD5 973fa042106cf16fb6860272e07e8c7f
BLAKE2b-256 818a51c99c8171bc591336f579b5e89a812f66ceda020e6a639532ce2b3f38b9

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dnnets-0.2.2-py3-none-manylinux_2_35_i686.whl
Algorithm Hash digest
SHA256 7af903c1c63a2317d01d7f7dad8394485dcd37ad4e4589edb89e37d97e3958af
MD5 d6cdb6e7cbf373669c3438d29a3d91af
BLAKE2b-256 b98f3d8696c5c0df6909c0f5f9c7f072089ab63c374b5c630cbbd84b724ef9d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-manylinux_2_35_armv7l.whl
Algorithm Hash digest
SHA256 8e9d2bda2f0d4c0b83bc2f84f6bfc9a279fd3362db9caba56ef1258482771ea9
MD5 5630d697be896450a5f569e027c87070
BLAKE2b-256 1d7768efd2356c868b803922d2e97207a271e9205df88960eb3213d76a757887

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 346e57e8014140886acc6a740091f0d4761e5045fb9235bbe8ce3c59f84bb0bf
MD5 3263ccca096d485d0e39fee70d09167d
BLAKE2b-256 f0909762e8204af1fb50b120881e80605983891235a4bd2038e9f1535e3f127c

See more details on using hashes here.

File details

Details for the file dnnets-0.2.2-py3-none-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 3459c2bff978e733a472658377fa4e9a89b3503d7c24cc8a5641f448a7f574f1
MD5 f82b0b5ca348b2c65451c3984ee14d18
BLAKE2b-256 17916cd79c246e2bb42d633b60b168abea2776f4a7cc6b7b8cb3810b7ded01ef

See more details on using hashes here.

File details

Details for the file dnnets-0.2.2-py3-none-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for dnnets-0.2.2-py3-none-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 b30f3a0256799ec606d9bbc2ea7095324d6e9eda8696becd0db2f04989c18067
MD5 2a791bbeca7d5af287ef5b5fd584d438
BLAKE2b-256 2d431edd846aab4875735d36396e7ef93903a0996f0399aedf405999c5633fc5

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