moon_onnx

    A pure MoonBit ONNX inference runtime for js, wasm-gc and native

    onnx
    inference
    machine-learning
    neural-network
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    17 hours ago
    Downloads
    8

    #moon_onnx

    A general-purpose ONNX inference runtime written in pure MoonBit. It reads standard .onnx files and has no external dependencies, and the same code runs on the js, wasm-gc and native backends.

    • Includes its own protobuf decoder, tensors with NumPy broadcasting, graph execution, and 46 operators.
    • Runs real image-classification models: MNIST, SqueezeNet 1.1, MobileNetV2 and ResNet-18. On all three backends the outputs agree with onnxruntime within tolerance and the top-5 classes are identical.
    • Operator coverage is measured with the official ONNX backend tests (onnx v1.22.0). All 317 in-scope node tests pass, and all 118 out-of-scope tests (unsupported dtypes, training mode, and so on) are correctly rejected at load time. All 69 official pytorch-converted tests pass as well.
    • Anything unsupported (operator, operator version, attribute, data type) is reported while the model is loading, naming the node. The runtime never silently computes a wrong result.

    #How it differs from existing MoonBit projects

    ProjectWhat it isHow moon_onnx differs
    HuLunTunTao/moon-ortBindings to the official ONNX Runtime C library; native onlymoon_onnx is pure MoonBit with no external runtime library, and runs on js, wasm-gc and native
    OJII3/onnxruntime_webBindings to onnxruntime-web; browser onlySame as above; no JavaScript or WASM runtime to ship alongside
    QUHONGYE-gif/modelhubContains a pure-MoonBit ONNX executor with 11 operators (Gemm, MatMul, Add, Relu, ...) for small fully-connected models46 operators including convolution, pooling, normalization and shape operators. Runs CNNs such as ResNet-18. Handles operator versions from opset 1 to 25, and is measured against the official backend tests
    soehi/moonlm, majikxu/laya, ...Inference hand-written for one specific model architectureReads any ONNX file whose operators are in the supported set

    #Usage

    let session = @runtime.Session::from_bytes(model_bytes) // raises if unsupported
    for spec in session.inputs() {
    println(spec.to_string()) // e.g. "data: float32[N,3,224,224]"
    }
    let input = @tensor.Tensor::f32([1, 3, 224, 224], pixels) // or @tensor.from_proto_bytes(pb)
    let outputs = session.run([("data", input)]) // [(name, Tensor)] in graph order

    Errors are @graph.OnnxError: DecodeFailed (malformed protobuf, with the byte offset), Unsupported / InvalidModel (at load time, naming the node), InvalidInput (wrong name, dtype, rank or dimension) and RuntimeFailure (naming the node), for example:

    unsupported: node #0 'erf_node' (Erf): operator 'Erf' is not supported unsupported: node #0 'bn' (BatchNormalization-15): BatchNormalization with training_mode=1 is not supported invalid input: input 'Input3' dimension 2 must be 28, got 27 (shape [1,1,27,28]) decode error: protobuf decode error at byte 3 (model.graph): length-delimited field of 9 bytes exceeds remaining 2 bytes

    #Browser demo

    https://sayoi7799.github.io/moon_onnx/: handwritten-digit recognition (MNIST) and image classification (SqueezeNet 1.1, ImageNet labels). The runtime is compiled to wasm-gc (a 190 KB module) and everything is computed in the page; nothing is uploaded, and onnxruntime-web is not used. It needs a browser with WebAssembly GC (Chrome 119+, Firefox 120+, Safari 18.2+). SqueezeNet takes about 230 ms per image there.

    web/demo exposes three functions (load_model, describe, run_model) with number-only signatures. Bytes and floats are pulled from JavaScript through imported callbacks, so no GC objects cross the boundary. web/moon_onnx.js wraps them:

    import { createRuntime, topK } from "./moon_onnx.js"; const rt = await createRuntime(await (await fetch("demo.wasm")).arrayBuffer()); const model = rt.load(new Uint8Array(await (await fetch("mnist-12.onnx")).arrayBuffer())); const scores = rt.run(model, pixels /* Float32Array */, [1, 1, 28, 28]);

    Build and serve locally (scripts/fetch_models.py first): bash scripts/build_web.sh && python -m http.server -d _site 8000. node web/smoke_test.mjs checks the wasm-gc build against onnxruntime's MNIST output. The Pages workflow runs it before every deploy.

    #Command-line example

    moon run cmd/onnx_run --target native --release -- .cache/models/resnet18-v1-7.onnx examples/chelsea.ppm --repeat 5

    model .cache/models/resnet18-v1-7.onnx (opset 8, 69 nodes, producer '') operators Add-7 Conv-1 Gemm-7 Relu-6 Flatten-1 MaxPool-8 GlobalAveragePool-1 BatchNormalization-7 input data: float32[N,3,224,224] output resnetv15_dense0_fwd: float32[N,1000] tensor float32[1,3,224,224] from examples/chelsea.ppm top-5: #1 class 285 score 15.851795 p=55.9% #2 class 281 score 15.005581 p=23.98% ... load 118 ms infer 1092.2 ms average over 5 runs, best 1083 ms

    INPUT can be a binary PPM or BMP image, which is resized and normalized according to the model's input shape, or a serialized TensorProto (.pb), which is fed to the model unchanged. Options: --labels FILE (one class name per line), --repeat N, --top K, --invert (for MNIST-style models when the image has a dark digit on light paper). To convert other image formats: python -c "from PIL import Image; Image.open('a.jpg').convert('RGB').save('a.ppm')".

    #Supported operators

    Versions are listed as since_version. The version used by a node is the largest since_version that is ≤ the model's opset. Versions older than the ones listed here use legacy semantics and are rejected at load time.

    GroupOperators (oldest supported version)Types
    Convolution / poolingConv (1): groups, depthwise, dilations, auto_pad, 1-D/2-D/3-D · MaxPool (1): ceil_mode, dilations, Indices output · AveragePool (1): count_include_pad, ceil_mode · GlobalAveragePool (1)float32 (MaxPool also uint8)
    Normalization / activationBatchNormalization (6, inference only) · Relu (6) · LeakyRelu (6) · Clip (6) · Sigmoid (6) · Tanh (6) · Softmax (1) · LogSoftmax (1) · Dropout (7, inference; training mode only with ratio 0)float32/float64; Relu, Clip and Neg also accept integers
    Linear algebraGemm (6) · MatMul (1)float32
    Element-wiseAdd, Sub, Mul, Div (6, NumPy broadcasting, including opset-6 broadcast/axis) · Pow (7) · Sqrt, Exp, Log, Neg, Abs (6) · Min, Max (6)float32, float64, int32, int64, uint8
    ShapeReshape (5) · Flatten (1) · Transpose (1) · Concat (4) · Squeeze, Unsqueeze (1) · Slice (1) · Gather (1) · Shape (1) · Cast (6) · Pad (2: constant/reflect/edge/wrap) · Identity (1) · Constant (1) · ConstantOfShape (9) · Expand (8)all 6 MVP types
    ReductionReduceMean, ReduceSum, ReduceMax (1, axes as attribute or input) · ArgMax (1)numeric (ReduceMax also bool)

    Tensor element types: float32, float64, int32, int64, uint8, bool. Not supported (all rejected at load time): control flow (If/Loop/Scan), sequences and maps, quantized operators, string tensors, float16 and other dtypes, external data files, training mode, and non-default operator domains.

    #Official ONNX backend tests

    Source: onnx/backend/test/data/node at onnx v1.22.0, the last release that commits this data. A test is in MVP scope when all of its types and operators are supported; this label comes from the test's contents (see scripts/gen_node_tests.py), not from our results. An out-of-scope test counts as correctly rejected only when loading or running fails with a "not supported" error. Function-expanded tests (*_expanded) are counted under the operator they expand. Expected outputs are the official .pb files, compared with the official runner's tolerance (rtol 1e-3, atol 1e-7). Results are identical on js, wasm-gc and native.

    operatorofficial testsin MVP scopepassedcorrectly rejected
    Abs1110
    Add8335
    ArgMax1616160
    AveragePool2020200
    BatchNormalization4222
    Cast602258
    Clip24101014
    Concat1212120
    Constant1110
    ConstantOfShape3330
    Conv6660
    Div10555
    Dropout12884
    Exp2220
    Expand2220
    Flatten9990
    Gather4440
    Gemm1111110
    GlobalAveragePool2220
    Identity3112
    LeakyRelu6333
    Log2220
    LogSoftmax2121210
    MatMul7770
    Max14886
    MaxPool1919190
    Min14886
    Mul9445
    Neg2220
    Pad6660
    Pow1210102
    ReduceMax1010100
    ReduceMean8880
    ReduceSum1212120
    Relu2111
    Reshape1010100
    Shape1111110
    Sigmoid2220
    Slice8880
    Softmax2121210
    Sqrt2220
    Squeeze2220
    Sub9445
    Tanh2220
    Transpose7770
    Unsqueeze7770
    total435317317118

    pytorch-converted tests (onnx/backend/test/data/pytorch-converted): the 69 tests that use only MVP operators. They cover Conv1d/2d/3d, grouped, depthwise, dilated and strided convolutions, as well as pooling, padding and BatchNorm.

    defining operatorofficial testsin MVP scopepassedcorrectly rejected
    Abs1110
    AveragePool7770
    BatchNormalization5550
    Conv2626260
    Exp1110
    Gather2220
    Gemm1110
    LeakyRelu2220
    LogSoftmax3330
    MaxPool8880
    Neg1110
    Pad4440
    Relu1110
    Sigmoid1110
    Softmax3330
    Tanh1110
    Transpose2220
    total6969690

    #Models

    ModelFile (ONNX Model Zoo)opsetProducerLicense
    MNISTmnist-12.onnx12CNTK 2.5.1MIT
    SqueezeNet 1.1squeezenet1.1-7.onnx7MXNetApache-2.0
    MobileNetV2mobilenetv2-12.onnx12PyTorch 1.8Apache-2.0
    ResNet-18 v1resnet18-v1-7.onnx8MXNetApache-2.0

    Agreement with onnxruntime 1.31.1 (CPU, 1 thread, graph optimizations disabled), with 5 inputs per model. The tolerance is |ours − ort| ≤ 1e-4 + 1e-3·|ort|, and the top-5 classes must match exactly, in the same order. All four models give identical results on js, wasm-gc and native:

    ModelInputsTop-1 sameTop-5 same (incl. order)Max abs errorMax rel error
    mnist-1255/55/51.5e-3 (logits are on the order of 10³)2.5e-6
    squeezenet1.1-755/55/52.3e-57.7e-6
    mobilenetv2-1255/55/51.5e-55.2e-4
    resnet18-v1-755/55/51.1e-54.7e-4

    For comparison, onnxruntime's own optimized and unoptimized outputs for these inputs differ by up to 1.5e-3 (MNIST) and 1.3e-5 (ImageNet models).

    Why this tolerance: a float32 accumulation over about 20 layers picks up about 1e-5 relative error from summation order alone (onnxruntime's MLAS kernels use FMA and a different blocking). 1e-3 relative / 1e-4 absolute is two orders of magnitude above that, yet far below any change that could reorder classes. The top-5 check enforces that directly.

    #Speed

    Single thread, release builds, one 224×224 image (MNIST: one 28×28 image), averaged over 5 runs on a Windows 11 laptop:

    ModelGMACsnativewasm-gcjs (Node 24)onnxruntime 1.31.1 (1 thread, MLAS)
    mnist-120.00050.8 ms1.4 ms8.6 ms< 0.1 ms
    squeezenet1.1-70.35236 ms214 ms802 ms5.0 ms
    mobilenetv2-120.30211 ms208 ms841 ms5.8 ms
    resnet18-v1-71.81092 ms950 ms3178 ms26.3 ms

    Timer resolution is 1 ms, so the MNIST times are only approximate. All three backends predict the same class for the example image (285, chelsea). Reproduce with bash scripts/bench.sh 5 and python scripts/bench_ort.py.

    Convolution uses im2col followed by a cache-blocked float32 GEMM (i-k-j loop order). 1×1 convolutions skip im2col, and depthwise convolutions use a direct loop. There is no SIMD or multithreading, which explains most of the gap to onnxruntime. Raising performance is future work; the MVP prioritizes correctness.

    #Architecture

    PackageRole
    protoProtobuf wire-format decoder and ONNX messages (Model/Graph/Node/Attribute/Tensor/ValueInfo)
    tensorTensor type (one contiguous buffer per dtype), shapes, strides, broadcasting, TensorProto → Tensor
    opsOperator kernels and the registry: version resolution, attribute and type validation at load time
    graphModel loading, topological sort, Constant folding, liveness-based release, execution
    runtimePublic Session API
    visionImage decoding (PPM/BMP), resize/crop, ImageNet normalization, top-k (used by the examples)
    fileioFile reading for tests and the CLI (native/wasm-gc: moonbitlang/x/fs; js: Node fs)
    cmd/onnx_runCommand-line classifier
    web/demowasm-gc build with a number-only JS interface (browser demo)
    tests/node, tests/models, tests/edgeOfficial backend tests, whole-model tests, edge cases

    #Tests

    moon test --target js|wasm-gc|native runs:

    1. protobuf / tensor: decoder output compared with the onnx Python package for mnist-12, official test files, a hand-built model covering every attribute and storage form, truncated prefixes and malformed messages; TensorProto conversion compared with onnx.numpy_helper; broadcasting compared with NumPy.
    2. operators: all official backend node tests and pytorch-converted tests, as above.
    3. models: the four models against onnxruntime. mnist-12 is committed; the others are skipped with a message until scripts/fetch_models.py has downloaded them. CI downloads them and fails if any test is skipped.
    4. cross-backend: each backend is checked against the same onnxruntime/ONNX reference values.
    5. edge cases: truncated and corrupted model files (every prefix up to 2 KB), unsupported operators, domains, attributes, dtypes and opsets, cycles and undefined values, input dtype/rank/dimension/symbolic-dimension mismatches, runtime errors that name the node, and empty tensors.

    Test data is embedded as .mbt files where practical, so it runs on every backend. The larger models are read from disk at test time; fileio makes this work on wasm-gc as well, through moonrun.

    #Regenerating the test data

    All expected values come from the official ONNX test data and from onnxruntime; none are written by hand. Versions are pinned in scripts/requirements.txt (onnx 1.22.0, onnxruntime 1.31.1, numpy 2.3.5, Python 3.12).

    py -3.12 -m venv .cache/venv .cache/venv/Scripts/python -m pip install -r scripts/requirements.txt .cache/venv/Scripts/python scripts/fetch_test_data.py # onnx v1.22.0 backend tests (git SHA-1 verified) .cache/venv/Scripts/python scripts/fetch_models.py # Model Zoo models + sample data (sha256 verified) .cache/venv/Scripts/python scripts/gen_proto_fixtures.py # proto/fixtures_wbtest.mbt .cache/venv/Scripts/python scripts/gen_tensor_fixtures.py .cache/venv/Scripts/python scripts/gen_op_schemas.py # ops/schemas.mbt from onnx.defs .cache/venv/Scripts/python scripts/gen_node_tests.py # tests/node/data_*_wbtest.mbt .cache/venv/Scripts/python scripts/gen_model_tests.py # testdata/expected, tests/models/manifest_wbtest.mbt .cache/venv/Scripts/python scripts/gen_edge_fixtures.py bash scripts/bench.sh 5 && .cache/venv/Scripts/python scripts/bench_ort.py

    On Linux/macOS, use .cache/venv/bin/python instead.

    #Sources and licenses

    moon_onnx is licensed under Apache-2.0 (see LICENSE). It is an independent implementation in MoonBit. Only the specifications and observable behavior were consulted:

    • ONNX specification and operator documentation and onnx.proto (Apache-2.0) define the semantics. ops/schemas.mbt is generated from the onnx.defs operator schemas (names, versions, attribute lists).
    • onnxruntime (MIT) behavior is used as the reference for model outputs. None of its code is used.

    Third-party data in this repository or downloaded by the scripts:

    DataSourceLicense
    tests/node/data_*_wbtest.mbt (embedded unchanged)onnx v1.22.0 onnx/backend/test/data/{node,pytorch-converted}Apache-2.0
    testdata/models/mnist-12.onnx; downloaded squeezenet1.1-7, mobilenetv2-12, resnet18-v1-7 and their sample dataONNX Model Zoo commit c5cae0fMIT (MNIST), Apache-2.0 (others)
    testdata/images/*.pb, examples/chelsea.ppm, examples/coffee.bmpscikit-image sample data: astronaut and rocket (public domain), chelsea and coffee (CC0)public domain / CC0
    testdata/mnist/digit_*.pb, examples/digit3.ppmdrawn with PIL's default font by scripts/gen_model_tests.py(generated)
    testdata/expected/outputs of onnxruntime 1.31.1(generated)
    web/synset.txt (ImageNet class names)ONNX Model Zoo validated/vision/classification/synset.txt, commit c5cae0fApache-2.0
    web/samples/*.jpgscikit-image sample data (astronaut, rocket: public domain; chelsea, coffee: CC0), downscaledpublic domain / CC0