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

Uploaded Python 3Windows ARM64

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

Uploaded Python 3Windows x86-64

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

Uploaded Python 3Windows x86

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

Uploaded Python 3musllinux: musl 1.2+ x86-64

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

Uploaded Python 3musllinux: musl 1.2+ s390x

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

Uploaded Python 3musllinux: musl 1.2+ riscv64

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

Uploaded Python 3musllinux: musl 1.2+ ppc64le

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

Uploaded Python 3musllinux: musl 1.2+ i686

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

Uploaded Python 3musllinux: musl 1.2+ ARMv7l

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

Uploaded Python 3musllinux: musl 1.2+ ARM64

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

Uploaded Python 3manylinux: glibc 2.35+ x86-64

dnnets-0.1.0-py3-none-manylinux_2_35_s390x.whl (794.3 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ s390x

dnnets-0.1.0-py3-none-manylinux_2_35_riscv64.whl (669.4 kB view details)

Uploaded Python 3manylinux: glibc 2.35+ riscv64

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

Uploaded Python 3manylinux: glibc 2.35+ ppc64le

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

Uploaded Python 3manylinux: glibc 2.35+ i686

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

Uploaded Python 3manylinux: glibc 2.35+ ARMv7l

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

Uploaded Python 3manylinux: glibc 2.35+ ARM64

dnnets-0.1.0-py3-none-macosx_11_0_arm64.whl (162.6 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

dnnets-0.1.0-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.0.tar.gz.

File metadata

  • Download URL: dnnets-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 c11a4daedd9b459eb144ea48120667245bba441c5f8070a983a2c5796e335d07
MD5 c39b2e3eb4eb843660e88b6b06b4e8a6
BLAKE2b-256 a9cd2e1201ad41ee2e7d941aaa6a2778e785744fc6706345445f65a156261f39

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dnnets-0.1.0-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.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 76e757e7238d1d3c0de6efbaba638cd61dced45db920675b31d61497d1979aca
MD5 bbdde12d035da188a4a5da27c430f29c
BLAKE2b-256 da4b32405d3caee3c873968daffa60ed1876939102f549c62830f55ef85b95e1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dnnets-0.1.0-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.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 82f903eed6cb2e8c34c55fcf4fefd840f0df1f46fc5bc4ced2f9de77736e3245
MD5 caf85fc0572dc1d6428b1c971b7453b9
BLAKE2b-256 6c58061434db1e7b33c14a6c934b0ad89a0217e1ada91b563b4156d241c00e62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dnnets-0.1.0-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.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 ccaa14150dce002f61172c2476b7ef7d37da4924e5ea68821e639f9ca58d7465
MD5 98327b333fea410136d29d04804537f1
BLAKE2b-256 f9ab028d35e090f0d819838831a9db7c2ee5fe3a181e0a843d9a301ad43a7f1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 bd672cfbc0b9944b61ecc02d551dfb7612b27f40730da760d91cc660492b7fc6
MD5 2011a167b427a69b8d7d21bd5983a691
BLAKE2b-256 8cc1aad3b589c23ae81d19de1fb47c8c18e51006d601e773dde5eb0fb61bce39

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dnnets-0.1.0-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.0-py3-none-musllinux_1_2_s390x.whl
Algorithm Hash digest
SHA256 85b195bcefaad44089513099087ec947f2a189059feb4462224dc3645d9e4f75
MD5 6b232e12d0fb057c878eaaa9f5708c06
BLAKE2b-256 397606f626d901f004a7276c11282d68c8497bfd8c8194b8cf3dfa32bae68112

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-musllinux_1_2_riscv64.whl
Algorithm Hash digest
SHA256 a8798f70b86ae6e5c67ef94710cfd54d6ead5f5c1b8cfbcb5e3496ecce2cc55b
MD5 a7acd0973f1916e011dda57812f48d44
BLAKE2b-256 4280981ecebd46b306dea0ddeb27b67c2cdf965b96093fd889efc33b2264ff1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 25a8857a01ba34b5eccca75922d731719dfe0ab53aa162aaf3b33f75bbde3a46
MD5 0ce8acd2a2a07b797a4d5b8c1ec01c2f
BLAKE2b-256 541b2b5e26612d594deba540a9f2424ce9586834bff8a368d877cead44ad9029

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dnnets-0.1.0-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.0-py3-none-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 15132b31c09073fa7157d4cdecd95453faa0d088638cbbb267dbe59d962d63d5
MD5 7546d359ff6c981d4c1dad7537199d95
BLAKE2b-256 1a41b4b7e5220dc9fd2cf55a0f6f07b831e218069f98899225f80a5f365db4a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 f81ebdc2a4d82073e2ef8b1166f59aa90e6c6864e74f2bfa66e7b0743dd5afc8
MD5 dfdb3cc250281b391003bbc3a1e5daf5
BLAKE2b-256 59eef0b3f6f2e3e95ea1474df477db11169d2b0e4225d10791f7578dba1c5511

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 31cee70218786f607e0181134253b49d430d6793b8a93f4152769a714b17ebd0
MD5 2b36ba9e994241f8dec751d84ac4abd3
BLAKE2b-256 7d00bae08eae9151b3ef17a1b4d5d38eec178e9475c4a4a04335a18206bed08e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 bc616c6708e8cf870fd021879dfa51c57e559845c9b20e72c00d5cc84384c424
MD5 450152e58e4adf2a0f7c6453dac59929
BLAKE2b-256 4c10573faab9dd2a84e6000884756dbb43ef67cae855f2e0d61c2894139b5854

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-manylinux_2_35_s390x.whl
Algorithm Hash digest
SHA256 6619b237cd608c562d483e88bdbb50ed02080bc0602b865734ab9b74fcc01490
MD5 0521f69ce7a3542c1711b446786f2444
BLAKE2b-256 d54570d6429513a9a03ff753c879e3675ce2f263b2a947dbc258a3fee19fef99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-manylinux_2_35_riscv64.whl
Algorithm Hash digest
SHA256 2a9815452303301903a6cb7c5851023337d17587f6c2d48b1e5f569ee762280c
MD5 ff3ec833843ae60d57e609fcf1bc9865
BLAKE2b-256 373fce15a3f79b3df668990ca1eeab5801cb6bd2e0dabf22aa604745e4deb3bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-manylinux_2_35_ppc64le.whl
Algorithm Hash digest
SHA256 bb0fb9fab776f45acc9d0898764c234add8c227460509c1c3fe1df629e5e21d0
MD5 75687fb9ccf445fc8cd90b754246c035
BLAKE2b-256 25b07e754f6bbe789c6592bf1bc2a14820a81782f65ce0e80ef502b60f936454

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dnnets-0.1.0-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.0-py3-none-manylinux_2_35_i686.whl
Algorithm Hash digest
SHA256 1f124135e9a1b83f2e639c949b0f9b92d6ea5114ed845a769a00c938b9cbedc6
MD5 25b06015af63dcd7c4c41d04758d0634
BLAKE2b-256 4af233d2252a28022c0b3c3f312a657fdbf9b9e9a7740e4714e0b9f8e5a67284

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-manylinux_2_35_armv7l.whl
Algorithm Hash digest
SHA256 2c3fe7ca847f8cd3ae050dde34765d91cd181a6f0112c6299fa8aa519ba7cc76
MD5 5b069a31489ed519a518e821914d096e
BLAKE2b-256 d1fa3bc676d480062403f4a603f129ff40b147174077f32321dac4bc1f45df77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 f598fb3d85bfc9e76a619b66c80dc7771bb4f11e3820d524b06a4f59f30615fe
MD5 3682c88d5caf3b150d61ca9f5e8dc59d
BLAKE2b-256 9aaa4dd3d5ca3704f9ca5d1b294e18f5ac26ac3644b2e7364ad13d4b7da55bbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 abd567f5eb7eafdfc330483c6dfff1c3ea2fc740c108070cd4d486f42df0e0e5
MD5 d48f3d52addf97ef682dc5bbe2916dc8
BLAKE2b-256 dbeb03a566e77c8be99452d60f59d29599cc3041108778c37a242ef16a0f8107

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dnnets-0.1.0-py3-none-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 df35786d7e0074744bca3a47358d48266fd34e1c149de70031b6683312883910
MD5 eef0730e5e4a9b29a60e1844ba21ef9c
BLAKE2b-256 fe76abc46e32904abeaec797111953f1950546fd8442bc92c84f6d4d6c556065

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