Skip to content

New feature: OnnxPredict algorithm#1488

Open
xaviliz wants to merge 70 commits into
MTG:masterfrom
xaviliz:feat/onnx-predict
Open

New feature: OnnxPredict algorithm#1488
xaviliz wants to merge 70 commits into
MTG:masterfrom
xaviliz:feat/onnx-predict

Conversation

@xaviliz
Copy link
Copy Markdown
Contributor

@xaviliz xaviliz commented Sep 1, 2025

New feature: OnnxPredict algorithm

Feature

It makes additional changes in Essentia library to build ONNX Runtime Inferencing library from the source and to implement a new algorithm OnnxPredict for running ONNX models (.onnx) with multiple IO.

Implementation

  • Provide a new building script for ONNX Runtime Inferencing library.
  • Modify Essentia scripts to link with the onnxruntime dynamic library.
  • Implement new algorithm OnnxPredict to run ONNX models in Essentia.
  • Implement unittests in test_onnxpredict.py

Prerequisites

  • python >= 3.10
  • cmake >= 3.28

Testing

  • Builds successfully with ONNX Runtime v1.22.1 in MacOS
    • ARM64
    • x86_64
  • Builds successfully with ONNX Runtime v1.22.1 in Linux
  • Multiple input inferencing
  • Multiple output inferencing
  • No runtime errors or compatibility issues

How to Test

Tested in onnxruntime-v1.22.1:

  • MacOS with an ARM64 machine with python 3.13.4 and cmake 4.0.2
  • Linux docker with python 3.10.18 and cmake 4.1.0

How to build ONNX Runtime

After installing Essentia dependencies in a virtual environment, install cmake

python3 -m pip install cmake
which cmake

Then we can run the building script:

cd packaging/debian_3rdparty
bash build_onnx.sh

How to build OnnxPredict

In MacOS:

source .env/bin/activate
python3 waf configure --fft=KISS --include-algos=OnnxPredict,Windowing,Spectrum,MelBands,UnaryOperator,TriangularBands,FFT,Magnitude,NoiseAdder,RealAccumulator,FileOutputProxy,FrameCutter --static-dependencies --pkg-config-path=/packaging/debian_3rdparty/lib/pkgconfig --with-onnx --lightweight= --with-python --pythondir=.env/lib/python3.13/site-packages
python3 waf -v && python3 waf install

In Linux:

python3 waf configure --fft=KISS --include-algos=OnnxPredict,Windowing,Spectrum,MelBands,UnaryOperator,TriangularBands,FFT,Magnitude,NoiseAdder,RealAccumulator,FileOutputProxy,FrameCutter --static-dependencies --with-onnx --lightweight= --with-python --pkg-config-path /usr/share/pkgconfig --std=c++14
python3 waf -v && python3 waf install

How to unittest

# prepare essentia audio repo
git clone https://github.com/MTG/essentia-audio.git test/essentia-audio
rm -rf test/audio && mv test/essentia-audio test/audio

# download effnet.onnx model for testing
curl https://essentia.upf.edu/models/feature-extractors/discogs-effnet/discogs-effnet-bsdynamic-1.onnx --output test/models/discogs-effnet-bsdynamic-1.onnx
python3 test/src/unittests/all_tests.py onnxpredict

…hen cuda, metal or open_ml are not compiled in onnxruntime library
… level in ORT.

- Declared as a new string parameter with choices: {disable_all,basic,extended,all}, by default “extended”.
	- https://onnxruntime.ai/docs/performance/model-optimizations/graph-optimizations.html#levels
- Set graph optimization level in ORT Session.
- `test_default_optimization_level()`: Check that the default optimization level is 'extended'.
- `test_set_valid_optimization_levels()`: Check that valid optimization levels can be set without errors.
- `test_set_invalid_optimization_level()`: Check that invalid optimization levels raise an error.
- CUDA tensors now use Ort::MemoryInfo::CreateCuda to allocate GPU memory.
- Metal and CoreML providers continue to use CPU tensors; data is managed internally by the provider.
- Execution provider is auto-selected based on availability and _deviceId.
@xaviliz
Copy link
Copy Markdown
Contributor Author

xaviliz commented Dec 23, 2025

Thank you @palonso, all changes/suggestions have been addressed.
Please when you can revise and give me feedback for the changes.
I think it is almost done.

Before merging it would be nice to:

  • Test the algorithm in Linux with the last changes, but it should work fine.
  • Test it in OSX with METAL and OPEN_ML.
  • Make a brew cask to test the essentia installation with the onnxruntime package
    • I think it was failing because it was builded without some static dependencies flag.

   - Add CUDA flags to build ORT in Linux
   - Copy dynamic library with CUDA providers in the PREFIX
   - Add ORT flags to optimize building and library.
   - Define HAVE_ONNXRUNTIME_CUDA variable in wscript to automatically
     check if CUDA library is available
   - Avoid loading CORE_ML headers from OSX when HAVE_ONNSRUNTIME_COREML
     is not on.
   - Switch to CUDA provider when HAVE_ONNXRUNTIME_CUDA is active.
   - Remove specific CUDA Memory info snippet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants