Neural Casting is a front-end compiler to convert ONNX format (Open Neural Network Exchange) to a specific programming language. Currently, conversion to C code is supported, but the software aims to provide an infrastructure suited to convert ONNX structure to different programming languages and intermediate representations. This makes Neural Casting a suitable entry point for backend compilers. The core of Neural Casting is the use of the Direct Acyclic Graph (DAG) provided by ONNX structure to replace the Abstract Syntax Tree (AST) of the traditional compilers. As a result, the code generation is possible with an exploration algorithm of such data structure, considering the dependencies between the different operators of the encoded neural network.
For the setup of Neural Casting, use the following procedure:
Clone the repository:
git clone git@github.com:alecerio/NeuralCasting.git
cd NeuralCasting
Install the conda environment:
conda env create -f env.yml
You can find env.yml
in the main page of the repository.
conda activate neural_casting
neural_casting
, run the setup file:python setup.py install
sudo apt update
sudo apt install gcc
gcc --version
Inside the workdir directory, create two folders:
Currently, onnx and pytorch models are supported as input and C code as output. The entry point of the compiler is the function run
in neural_cast/compiler.py
.
You must pass as first parameter the configuration of the compiler (e.g. the one you can find in config/config.yaml
), while the following parameters depend on the selected framework.
If framework='pytorch'
, then you have to specify the following parameters:
model
: an instance of the pytoch model.dummy_input
: the dummy input for the model (check that the shape is compatible with the model).params
: the weights of the model.Example:
import os
import yaml
import torch
from neural_cast.compiler import run
# load configuration
curr_file = os.path.abspath(__file__)
curr_path = os.path.dirname(curr_file)
with open(curr_path + '/../../config/config.yaml', 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
# run compiler
run(config, framework='onnx', path=curr_path + '/model.onnx')
# check generated code in the output folder defined in config.yaml
If framework='onnx'
, then you have to specify the following parameters:
path
: the path and the name of the onnx file (including the .onnx extension).Example:
import os
import yaml
import torch
from neural_cast.compiler import run
# load configuration
curr_file = os.path.abspath(__file__)
curr_path = os.path.dirname(curr_file)
with open(curr_path + '/../../config/config.yaml', 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
# run compiler
run(config, framework='onnx', path=curr_path + '/model.onnx')
# check generated code in the output folder defined in config.yaml
There are different tests implemented to ensure the generated code is correct. You can find the list of implemented tests in tests/neural_networks/
:
Go the the repository directory:
cd /path/to/repository/
Ensure the compiler package is up to date:
python setup.py install
Run the tests:
python run_tests.py
The current main goal of the project is to run audio related neural networks on an STM32 board, specifically for de-noising.
Nevertheless, the project aims to provide a general infrastructure and to cover a wide range of use cases. Consequently, there are many oppurtunities of features integration in the future:
NeuralCasting is a project developed by Alessandro Cerioli during his Industrial PhD at Jabra and DTU (Technical University of Denmark) and is part of the European project Convolve. For more information regarding the project or to actively contribute to the development of the repository, use the following contacts:
This project is developed according to apache 2 license (see LICENSE).