onnxruntime_web

    MoonBit bindings for ONNX Runtime Web

    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    6 days ago
    Downloads
    24

    #onnxruntime-web-mbt

    MoonBit bindings for ONNX Runtime Web, based on the companion-module pattern used by three-mbt. The package targets MoonBit's JavaScript backend and keeps inference, tensor storage, and execution on the ONNX Runtime side.

    #Supported API

    • Create float32 tensors and inspect their data and dimensions.
    • Load an ONNX model from a URL or file path with InferenceSession::create.
    • Select execution providers, including WebGPU, when creating a session.
    • Inspect input and output names, run inference, and release the session.

    The current binding intentionally covers the common float32 inference path. Other tensor types, general session options, and fetch selection are not exposed yet. WebGPU requires browser support and only accelerates operators supported by its execution provider.

    #Install

    Add the MoonBit package and its JavaScript companion to your project:

    moon add OJII3/onnxruntime_web pnpm add onnxruntime-web @ojii3/onnxruntime-web-mbt@link:./.mooncakes/OJII3/onnxruntime_web/js

    Add the import to the MoonBit package that uses the binding:

    import { "OJII3/onnxruntime_web", }

    Build for JavaScript with moon build --target js. The consumer must bundle or otherwise resolve the onnxruntime-web npm package and the companion package.

    #Example

    async fn main {
    let session = @onnxruntime_web.InferenceSession::create(
    "./model.onnx",
    execution_providers=["webgpu"],
    )
    let input = @onnxruntime_web.Tensor::float32([1.0, 2.0, 3.0, 4.0], [1, 4])
    let outputs = session.run([("input", input)])
    let scores = outputs[0].float32_data()
    session.release()
    }

    Inference methods are async; call them from an async MoonBit function. run returns tensors in the order of session.output_names().

    #Development

    nix develop moon info moon fmt moon build --target js

    The npm companion lives in js/adapter.js. It imports ONNX Runtime Web and adapts the module's API to the small FFI surface in src/onnxruntime.mbt.

    #Release

    Update the versions in moon.mod and js/package.json together, then push the matching v<version> tag. The release workflow checks the tag and package versions, runs the MoonBit checks and build, publishes the module and its JavaScript companion together to Mooncakes, then creates a GitHub Release with generated notes.

    Configure these repository Actions secrets before publishing:

    • MOONCAKES_TOKEN: Mooncakes API token. The workflow writes it to ~/.moon/credentials.json.

    InferenceSession

    #external
    pub type InferenceSession

    InferenceSession::create

    async fn InferenceSession::create(model_path : String, execution_providers? : FixedArray[String]) -> InferenceSession

    InferenceSession::input_names

    fn InferenceSession::input_names(self : InferenceSession) -> FixedArray[String]

    InferenceSession::output_names

    fn InferenceSession::output_names(self : InferenceSession) -> FixedArray[String]

    InferenceSession::release

    async fn InferenceSession::release(self : InferenceSession) -> Unit

    InferenceSession::run

    async fn InferenceSession::run(self : InferenceSession, feeds : FixedArray[(String, Tensor)]) -> FixedArray[Tensor]

    Tensor

    #external
    pub type Tensor

    Tensor::dims

    fn Tensor::dims(self : Tensor) -> FixedArray[Int]

    Tensor::float32

    fn Tensor::float32(data : FixedArray[Double], dims : FixedArray[Int]) -> Tensor

    Tensor::float32_data

    fn Tensor::float32_data(self : Tensor) -> FixedArray[Double]

    Source Files

    Powered by MoonBit

    Site sourceReport issueModulesSkillsBuild queueStatistics

    © 2026 mooncakes.io