Skip to main content

Design, train and compile neural networks optimized specifically for FPGAs.

Project description

ElasticAI.creator

Design, train and compile neural networks optimized specifically for FPGAs. Obtaining a final model is typically a three stage process.

  • design and train it using the layers provided in the elasticai.creator.nn package.
  • translate the model to a target representation, e.g. VHDL
  • compile the intermediate representation with a third party tool, e.g. Xilinx Vivado (TM)

This version currently only supports parts of VHDL as target representations.

The project is part of the elastic ai ecosystem developed by the Embedded Systems Department of the University Duisburg-Essen. For more details checkout the slides at researchgate.

Table of contents

Users Guide

Install

You can install the ElasticAI.creator as a dependency using pip:

python3 -m pip install elasticai-creator

Minimal Example

The following example shows how to use the ElasticAI.creator to define and translate a machine learning model to VHDL. It will save the generated VHDL code to a directory called build_dir.

from elasticai.creator.nn import Sequential
from elasticai.creator.nn.fixed_point import Linear, HardSigmoid
from elasticai.creator.file_generation.on_disk_path import OnDiskPath


def main() -> None:
    # Define a model
    model = Sequential(
        Linear(in_features=10, out_features=2, bias=True, total_bits=16, frac_bits=8),
        HardSigmoid(total_bits=16, frac_bits=8),
    )

    # Train the model
    run_training(model)

    # Save the VHDL code of the trained model
    destination = OnDiskPath("build_dir")
    design = model.create_design("my_model")
    design.save_to(destination)


if __name__ == "__main__":
    main()

General Limitations

By now we only support Sequential models for our translations.

Structure of the Project

The structure of the project is as follows. The creator folder includes all main concepts of our project, especially the qtorch implementation which is our implementation of quantized PyTorch layer. It also includes the supported target representations, like the subfolder nn is for the translation to vhdl. Additionally, we have unit and integration tests in the tests folder.

Developers Guide

Install Dev Dependencies

pip install poetry
poetry install
poetry shell
pre-commit install
npm install --save-dev @commitlint/{config-conventional,cli}

# Optional:
sudo apt install ghdl

Project Structure

All packages and modules fall into one of five main categories and can thus be found in the corresponding package

  • nn: trainable modules that can be translated to vhdl to build a hardware accelerator
  • base_modules: (non-public) shared functionality and data structures, that we use to create our neural network software modules
  • vhdl: (non-public) shared code that we use to represent and generate vhdl code
  • file_generation: provide a very restricted api to generate files or file subtrees under a given root node and defines a basic template api and datastructure, compatible with file_generation

Glossary

  • fxp/Fxp: prefix for fixed point
  • flp/Flp: prefix for floating point
  • x: parameter input tensor for layer with single input tensor
  • y: output value/tensor for layer with single output
  • _bits: suffix to denote the number of bits, e.g. total_bits, frac_bits, in python context
  • _width: suffix to denote the number of bits used for a data bus in vhdl, e.g. total_width, frac_width
  • MathOperations/operations: definition of how to perform mathematical operations (quantization, addition, matrix multiplication, ...)

Conventional Commit Rules

We use conventional commits (see here). The following commit types are allowed. The message scope is optional.

Commit Types
feat
fix
docs
style
refactor
revert
chore
wip
perf

Tests

Our implementation is tested with unit and integration. You can run one explicit test with the following statement:

python3 -m pytest ./tests/path/to/specific/test.py

If you want to run all tests, give the path to the tests:

python3 -m pytest ./tests

If you want to add more tests please refer to the Test Guidelines in the following.

Test Style Guidelines

File IO

In general try to avoid interaction with the filesystem. In most cases instead of writing to or reading from a file you can use a StringIO object or a StringReader. If you absolutely have to create files, be sure to use pythons tempfile module and cleanup after the tests. In most cases you can use the InMemoryPath class to write files to the RAM instead of writing them to the hard disc (especially for testing the generated VHDL files of a certain layer).

Directory structure and file names

Files containing tests for a python module should be located in a test directory for the sake of separation of concerns. Each file in the test directory should contain tests for one and only one class/function defined in the module. Files containing tests should be named according to the rubric test_<class_name>.py. Next, if needed for more specific tests define a class. Then subclass it. It avoids introducing the category of bugs associated with copying and pasting code for reuse. This class should be named similarly to the file name. There's a category of bugs that appear if the initialization parameters defined at the top of the test file are directly used: some tests require the initialization parameters to be changed slightly. Its possible to define a parameter and have it change in memory as a result of a test. Subsequent tests will therefore throw errors. Each class contains methods that implement a test. These methods are named according to the rubric test_<name>_<condition>

Unit tests

In those tests each functionality of each function in the module is tested, it is the entry point when adding new functions. It assures that the function behaves correctly independently of others. Each test has to be fast, so use of heavier libraries is discouraged. The input used is the minimal one needed to obtain a reproducible output. Dependencies should be replaced with mocks as needed.

Integration Tests

Here the functions' behaviour with other modules is tested. In this repository each integration function is in the correspondent folder. Then the integration with a single class of the target, or the minimum amount of classes for a functionality, is tested in each separated file.

System tests

Those tests will use every component of the system, comprising multiple classes. Those tests include expected use cases and unexpected or stress tests.

Adding new functionalities and tests required

When adding new functions to an existing module, add unit tests in the correspondent file in the same order of the module, if a new module is created a new file should be created. When a bug is solved created the respective regression test to ensure that it will not return. Proceed similarly with integration tests. Creating a new file if a functionality completely different from the others is created e.g. support for a new layer. System tests are added if support for a new library is added.

Updating tests

If new functionalities are changed or removed the tests are expected to reflect that, generally the ordering is unit tests -> integration tests-> system tests. Also, unit tests that change the dependencies should be checked, since this system is fairly small the internal dependencies are not always mocked.

references: https://jrsmith3.github.io/python-testing-style-guidelines.html

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elasticai_creator-0.59.2.tar.gz (42.8 kB view details)

Uploaded Source

Built Distribution

elasticai_creator-0.59.2-py3-none-any.whl (76.2 kB view details)

Uploaded Python 3

File details

Details for the file elasticai_creator-0.59.2.tar.gz.

File metadata

  • Download URL: elasticai_creator-0.59.2.tar.gz
  • Upload date:
  • Size: 42.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/42.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.0.6 tqdm/4.66.1 importlib-metadata/6.8.0 keyring/24.2.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.11.5

File hashes

Hashes for elasticai_creator-0.59.2.tar.gz
Algorithm Hash digest
SHA256 f0554e100576f38e677041141854a7b83ec7a6de8aec6e37a3fbc78b5d53bed6
MD5 2cd0e5131f3c42ca954550645adf9891
BLAKE2b-256 8f50f96a801c1a3dad0532573758be8887f8ab4f9e98860a9b54c593ea3e4b29

See more details on using hashes here.

File details

Details for the file elasticai_creator-0.59.2-py3-none-any.whl.

File metadata

  • Download URL: elasticai_creator-0.59.2-py3-none-any.whl
  • Upload date:
  • Size: 76.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/42.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.0.6 tqdm/4.66.1 importlib-metadata/6.8.0 keyring/24.2.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.11.5

File hashes

Hashes for elasticai_creator-0.59.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3ca067e0619f877b585dbbb5653ac501bdc5ddd6856cd52c02fb59338aefddd2
MD5 b7a0183d9afc73d763b3ba1424ecf11b
BLAKE2b-256 0c8bfe148318f028deb490225a0ff5e748a7535410fa4a59d15349f77886f5c3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page