dx-codegen 1.1.1
A library package code generator
Requires Python: >= 3.8

# __CodeGen Library Package__

The DX Code Generator (CodeGen) is the backend compiler that takes the front-end output onnx file and generates the NPU executable binary file.
CodeGen generates the NPU binary file from two onnx input files: a proprocess onnx file and a model onnx file. The proprocess onnx has the information of the preprocessing ops (Mul, Add, ...). CodeGen performs the NPU compatible feature format changes on the output of the proprocessing. Then, CodeGen process the main model onnx file and generates the NPU binary file. 

## Quick Start
You can run the CodeGen with sample models in `CodeGen/examples`
```
# run all five models without logs
python3 run_models.py --all 
# run a model with log
python3 run_models.py --model model_name
```

You can check the supported model names from the help message
```
python3 run_models.py -h
usage: run_models.py [-h] [--all] [--model MODEL]

optional arguments:
  -h, --help     show this help message and exit
  --all          run all example models
  --model MODEL  Specify a model name to run from the below: model_graph_test (simple mode used in pytest), mv1_v0.25, mobilenetv3s, bisenet_v2,
                 yolo_v5s

```
Support Models are:
- model_graph_test
- mv1_v0.25
- mobilenetv3s
- bisenet_v2
- yolo_v5s

#TODO(Hyunjin): rewrite the below README.md
> ## __Table of content__ <!-- omit in toc -->

- [__CodeGen Package__](#codegen-package)

  - [__Creating the package files__](#creating-the-package-files)

  - [__Configuring metadata__](#configuring-metadata)
  
  - [__Checking a package__](#checking-a-package)
  
  - [__Creating the wheel__](#creating-the-wheel)

- [__Installing the CodeGen wheel file__](#installing-the-codegen-wheel-file)

- [__Editable mode Installing__](#editable-mode-installing)

- [__How to import the package__](#how-to-import-the-package)

- [__How to run the package__](#how-to-run-the-package)

- [__Generating the binary files__](#generating-the-binary-files)

- [__Uninstalling the CodeGen package__](#uninstalling-the-codegen-package)

- [__Uploading package to private package index server__](#uploading-package-to-private-package-index-server)

- [__Installing package from private package index server__](#installing-package-from-private-package-index-server)




> ## __CodeGen Package__
### __Creating the package files__

File structure of the project:

```bash
codegen
├── .vscode
│   ├── launch.json
│   └── settings.json
├── Makefile
├── README.md
├── requirements.txt
├── setup.py
├── examples
│   ├── example.py
│   └── run.sh
└── src
    └── dx_codegen
        ├── __init__.py
        ├── address_base.py
        ├── argparser.py
        ├── codegen.py
        ├── config.py
        ├── constant.py
        ├── device_layer.py
        ├── generator.py
        ├── onnx_data.py
        ├── onnx_layer.py
        ├── onnx_node.py
        ├── onnx_parser.py
        ├── onnx.proto
        ├── onnx_utils.py
        ├── operator_base.py
        ├── operator_data.py
        ├── operator_enum.py
        ├── operator_utils.py
        ├── outputes.py
        ├── partitioner.py
        ├── provisioner.py
        ├── data
        │   ├── __init__.py
        │   ├── GENESIS_M_REG_V0.3_RF_MEM_NPU_2K.pb
        │   ├── GENESIS_M_REG_V0.3_RF_MEM_NPU_4K.pb
        │   └── GENESIS_M_REG_V0.3_RF_MEM_NPU_8K.pb
        ├── data_config
        │   ├── __init__.py
        │   ├── L1_2k.json
        │   ├── M1_4k.json
        │   └── M1_8k.json
        └── outputer
            ├── __init__.py
            ├── npu_mmap.py
            ├── npu_util.py
            ├── out_data.py
            ├── out_utils.py
            └── outputer.py
```


### __Configuring metadata__
setup.py is the build script for setuptools. It tells setuptools about your package (such as the name and version) as well as which code files to include.

```python
import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description    = fh.read()

setuptools.setup(
    name                = "dx-codegen",
    version             = "0.5.1",
    author              = "Example Author",
    author_email        = "hoseung.kim@deepx.co.kr, sunmi.lee@deepx.co.kr, kkh@deepx.co.kr, mdh@deepx.co.kr",
    description         = "A library package code generator",
    long_description    = long_description,
    long_description_content_type = "text/markdown",
    url                 = "https://github.com/KOMOSYS/codegen.git",
    project_urls        = {
        "Bug Tracker" : "https://github.com/KOMOSYS/codegen/issues",
    },
    classifiers         = [
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    package_dir         = {"": "src"},
    packages            = setuptools.find_packages(where="src", include=["*"]),
    package_data        = {
        'dx_codegen' : ['data_config/*.json', 'data/*.pb'], 
    },
    python_requires     = ">= 3.8",
    install_requires    = [
        "numpy               >= 1.19.5",
        "sympy               >= 1.9",
        "nbformat            >= 5.0.7",
        "onnx                >= 1.11.0",
        "importlib-resources >= 5.6.0",
        "dx-rmap             >= 0.0.1",
    ],
)
```



### __Checking a package__
The check command allows you to verify if your package meta-data meet the minimum requirements to build a distribution.

To run it, just call it using your setup.py script. If something is missing, check will display a warning.

##### __Unix/macOS :__
```bash
$ python3 setup.py check
```


### __How to install Python packages with requirements.txt__
If you are managing Python packages (libraries) with pip, you can use the configuration file requirements.txt to install the specified packages with the specified version.

##### __Unix/macOS :__
```bash
$ pip3 install -r requirements.txt
```


### __Creating the wheel__
##### __Unix/macOS :__
```bash
$ python3 setup.py bdist_wheel
```



> ## __Installing the CodeGen wheel file__
You can use pip to install your package.

##### __Unix/macOS :__
```bash
$ pip3 install dist/dx_codegen-0.0.1-py3-none-any.whl 
```



> ## __'Editable' mode Installing__
Pip has the option to do an editable install, meaning you can install a package from a local source.

But instead of copying the package files to some location on your system, pip creates symlinks to the codebase you’re installing it from.

This way, you can work on the project and make changes and be able to directly test those changes without constantly reinstalling new versions of the package.

__There are a number of reasons why you might need this option :__

- When you are creating your own package

- If you want to fix someone else’s package

- If you want to install a so-called bleeding edge version of package, right from its source repository


##### __Unix/macOS :__
```bash
$ pip3 install -e .
```



> ## __How to import the package__
You can test that it was installed correctly by importing the package. Run Python :


##### __Unix/macOS :__
```bash
$ python3
```
and Import the package:

```bash
Python 3.6.9 (default, Dec  8 2021, 21:08:43) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dx_codegen.codegen import Codegen
>>> codegen = Codegen('./model/mv1_best_npu.onnx', './bin/mv1_best/', True, './model/mv1_best_pre.onnx', False, False, False, L1_2k, True)
>>> codegen.run()
>>> quit()
```



> ## __How to run the package__

By running the instruction below, you can see whether the package has been successfully installed or not.

##### __Unix/macOS :__
**Option 1** : This is the **recommended** way to get codegen running correctly.

__python3    main.py    --input  [ONNX File Path]    --out_dir  [Output Directory]    --dump_weight    --pre_onnx  [ONNX File Path]    --fpga    --prefix    --opt    --hw_config  [Config]    --im2col__
```
$ python3  ./examples/example.py    --input  ./model/mv1_best.onnx    --out_dir  ./bin/mv1_best/    --dump_weight    --pre_onnx  ./model/mv1_best_pre.onnx    --hw_config  L1-2k      --im2col
```

**Option 2** : This is the simplest way to get codegen running without any additional options such as hardware-specific configuration or output path location etc. Instead, the default hardware configuration and output path location are used.

__python3    main.py    --input  [ONNX File Path]__
```
$ python3  ./examples/example.py    --input  ./model/mv1_best.onnx
```


> ## __Generating the binary files__

Binary structure:

```bash
codegen
└── bin
    └── mv1_best
        ├── dev
        │   ├── rmap_128.hex
        │   ├── rmap_256.hex
        │   ├── rmap_512.hex
        │   ├── rmap.info.json
        │   ├── rmap.info.txt
        │   ├── rmap.json
        │   ├── rmap.pb
        │   ├── rmap.txt
        │   ├── sequence.txt
        │   └── weight.hex
        ├── rmap.bin
        ├── rmap.info
        └── weight.bin
```



> ## __Uninstalling the Codegen package__
You can use pip to uninstall your package.

##### __Unix/macOS :__
```bash
$ pip3 uninstall dx_codegen
```



> ## __Uploading package to private package index server__
See below link for an explanation of .pypirc.

[__Python Packaging User Guide - The .pypirc file__](https://packaging.python.org/en/latest/specifications/pypirc/)


##### __Unix/macOS :__
```bash
$ vi ~/.pypirc
[distutils]
index-servers =
	dev

[dev]
repository = http://192.168.0.10:3141/yourdevpiusername/dev/
```



> ## __Installing package from private package index server__
See below link for an explanation of pip.conf.

[__pip documentation - Configuration__](https://pip.pypa.io/en/latest/topics/configuration/)


##### __Unix/macOS :__
```bash
$ vi ~/.config/pip/pip.conf
[global]
index-url = http://192.168.0.10:3141/yourdevpiusername/dev/+simple/
trusted-host = 192.168.0.10

[search]
index = http://192.168.0.10:3141/yourdevpiusername/dev/
```

```bash
$ pip3 install dx_codegen
```