SUMMARY

ONNX Runtime Raku Bindings - Summary

What Works

Standalone Scripts (āœ… WORKING)

  • onnx-working.raku - Basic functionality demonstration

  • mnist-final.raku - Complete MNIST inference example

  • Both scripts work perfectly and can load models and run inference

Why Modules Don't Work

Raku's module precompilation system has a bug when:

  1. Modules use NativeCall to load native libraries

  2. Function pointers are obtained at runtime (not direct exports)

  3. These function pointers are called during module initialization

This causes the precompilation process to hang indefinitely.

Attempted Solutions

1. Traditional Module Approach (āŒ FAILED)

  • Created various module architectures (Simple, Direct, Lazy, etc.)

  • All hang during precompilation when trying to initialize ONNX Runtime

2. Server-Client Approach (āš ļø PARTIAL)

  • Created onnx-runtime-server.raku - A standalone server that handles ONNX operations

  • Created wrapper modules (ONNX::Wrapper, ONNX::Runtime) to communicate with server

  • Works in principle but has complexity issues with process management

3. Script Generation Approach (āš ļø PARTIAL)

  • Created ONNX::ScriptWrapper that generates temporary scripts

  • Avoids precompilation by running standalone scripts

  • Works but has overhead and complexity

Recommendations

For production use:

  1. Use the standalone scripts directly - They work perfectly

  2. Wait for Raku to fix the precompilation bug - This needs to be reported upstream

  3. Consider creating a C wrapper library - With direct function exports instead of function pointers

Key Learning

The ONNX Runtime C API's design (function pointer table via OrtGetApiBase()) is fundamentally incompatible with Raku's current module precompilation system. This is a Raku bug, not an issue with our implementation.

Files Overview

Working Scripts:
ā”œā”€ā”€ onnx-working.raku          # Basic ONNX Runtime demo
ā”œā”€ā”€ mnist-final.raku           # Complete MNIST inference
└── onnx-runtime-server.raku   # Server for wrapper approach

Module Attempts:
ā”œā”€ā”€ lib/
│   ā”œā”€ā”€ ONNX/
│   │   ā”œā”€ā”€ Simple.rakumod     # Minimal module (exports types only)
│   │   ā”œā”€ā”€ Wrapper.rakumod    # Async server wrapper
│   │   ā”œā”€ā”€ Runtime.rakumod    # Sync server wrapper
│   │   └── ScriptWrapper.rakumod # Script generation wrapper
│   └── ONNX/Runtime/
│       ā”œā”€ā”€ API/Simple.rakumod # Various architecture attempts
│       ā”œā”€ā”€ Direct.rakumod
│       ā”œā”€ā”€ Lazy.rakumod
│       └── ... (many more attempts)

Documentation:
ā”œā”€ā”€ README-ONNX-Runtime.md     # User documentation
ā”œā”€ā”€ README-precompilation-issue.md # Technical details of the bug
└── SUMMARY.md                 # This file

Next Steps

  1. Report the bug to Raku developers with our minimal test cases

  2. Use the working scripts for any immediate needs

  3. Consider alternative approaches like creating a simplified C wrapper library

ONNX::Runtime v0.0.6

Native Raku bindings for ONNX Runtime - run ONNX models directly in Raku

Authors

  • Danslav Slavenskoj

License

MIT

Dependencies

Test Dependencies

Provides

  • ONNX::Runtime
  • ONNX::Runtime::API
  • ONNX::Runtime::Types

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.