o
odinpkg.dev
packages / binding / onnx-odin-bindings

onnx-odin-bindings

ONNX-1.17.3_OdinLinux-0.0.2binding

Tool to translate C ONNX headers into Odin.

MIT · updated 2 years ago

Tool for translation C's ONNX header into Odin lang

Tool to translate C ONNX headers into Odin.

Disclamers

  1. The tool was made for self-educational purposes only. Use it at your own risk.
  2. Linux only! At the moment translation for linux only supported. Feel free fork and add any required funcitonality to add Window and Darwin support.
  3. I'm not proud of it from the software perspective, but it was fun to make.

Prerequisites

To translate header into Oding pycparser is used.

Requirements:

  • pycparser

Quick start

pip install -r requirements.txt
just parse-ast

See Justfile for more details.

Step-by-step

  1. Install pycparser
pip install -r requirements.txt
  1. Get ONNX C header of required version (for example, 1.17.3)
wget -O onnxruntime_c_api.h https://raw.githubusercontent.com/microsoft/onnxruntime/v1.17.3/include/onnxruntime/core/session/onnxruntime_c_api.h
  1. Preprocess onnxruntime_c_api.h
gcc -E -std=c99 onnxruntime_c_api.h > onnxruntime_c_api_prep.h
# OR
# cpp onnxruntime_c_api.h > onnxruntime_c_api_prep.h
# OR
# clang -E onnxruntime_c_api.h > onnxruntime_c_api_prep.h
  1. Clean-up preprocessed header file:
perl -pi -e 's/onnxruntime_c_api.h/onnxruntime_c_api_prep.h/g' onnxruntime_c_api_prep.h
perl -i -nle 'print if !/^\#/' onnxruntime_c_api_prep.h 
perl -pi -e 's/__\w+__//g' onnxruntime_c_api_prep.h
perl -pi -e 's/\(\(.+\)\)//g' onnxruntime_c_api_prep.h
perl -pi -e 's/__restrict//g' onnxruntime_c_api_prep.h
  1. Dump and parse AST

5.1 Get ONNX Version

echo ORT_API_VERSION::$(cat onnxruntime_c_api.h | grep "#define ORT_API_VERSION" | sed 's/.* //') > ORT_API_VERSION.version

5.2 Dump AST

python pycparser.c_json.py > dump.json

5.3 Parse AST

python translate_header.py --libonnxruntime-path="/thirdparty/onnxruntime/lib/libonnxruntime.so" --os="Linux" --package="onnx_bindings"

This will generate onnxbinding.odin file with Odin bindings to ONNX API.