moon-tensor

    MoonBit neural network inference operators for small, portable deployments.

    moonbit
    tensor
    neural-network
    inference
    wasm
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    3 months ago
    Downloads
    32

    No dependencies

    #moon-tensor

    MoonBit neural network inference operators for small, portable deployments.

    moon-tensor aims to be the first neural network inference operator library in the MoonBit ecosystem. It focuses on simple, readable, dependency-light kernels that compile cleanly to WebAssembly, JavaScript, and native targets.

    #Features

    • GEMM: row-major matrix multiplication
    • Conv1D: no-padding, stride-1 1D convolution
    • Linear: fully connected layer with bias
    • Activations: ReLU, Sigmoid, GELU
    • Softmax: Softmax and LogSoftmax
    • Normalization: LayerNorm and RMSNorm
    • Pooling: MaxPool1d and AvgPool1d
    • Utilities: max absolute difference for numerical checks

    #Quick Start

    Add moon-tensor as a dependency in your MoonBit project, then import it from the package that uses the operators.

    import {
    "Ankaluoer/moon-tensor" @tensor,
    }

    Example:

    ///|
    fn main {
    let input = [1.0, 2.0, 3.0]
    let activated = @tensor.relu(input)
    println(activated)

    let logits = [1.0, 2.0, 3.0]
    let probs = @tensor.softmax(logits)
    println(probs)
    }

    Run tests:

    moon test

    Build for WebAssembly GC:

    moon build --target wasm-gc

    #Size Comparison

    Runtime or libraryApproximate artifact size
    moon-tensor wasm-gc demo10 KB
    ORT-Web25 MB
    TensorFlow.js core-style bundles300-500 KB

    The goal is not to replace full ML runtimes. moon-tensor targets tiny inference paths where a small set of predictable operators is enough.

    #Roadmap

    • Quantized kernels for smaller models and faster edge inference
    • More inference operators, including Conv2D and attention building blocks
    • Keyword spotting demo for an end-to-end small model workflow
    • More backend checks across wasm-gc, wasm, js, and native targets

    #License

    Apache-2.0

    avg_pool1d

    fn avg_pool1d(x : Array[Double], channels : Int, length : Int, kernel_size : Int, stride : Int) -> Array[Double]

    AvgPool1d over input shape (channels, length). Flat layout: x[c * length + l]

    conv1d

    fn conv1d(input : Array[Double], kernel : Array[Double], c_in : Int, l : Int, c_out : Int, k : Int) -> Array[Double]

    Naive 1D convolution (no padding, stride=1).

    Input shape: (C_in, L) flat layout: input[c * L + l] Kernel shape: (C_out, C_in, K) flat layout: kernel[o * C_in * K + c * K + k] Output shape: (C_out, L - K + 1)

    gelu

    fn gelu(x : Array[Double]) -> Array[Double]

    GELU activation using tanh approximation. Output shape is the same as input shape.

    gemm

    fn gemm(a : Array[Double], b : Array[Double], m : Int, k : Int, n : Int) -> Array[Double]

    Naive GEMM: C = A @ B A: (M, K) row-major, B: (K, N) row-major, C: (M, N) row-major

    layer_norm

    fn layer_norm(x : Array[Double], gamma : Array[Double], beta : Array[Double], eps : Double) -> Array[Double]

    LayerNorm over a 1D array. x, gamma, beta shape: (features)

    linear

    fn linear(input : Array[Double], weight : Array[Double], bias : Array[Double], batch : Int, in_features : Int, out_features : Int) -> Array[Double]

    Linear fully-connected operator: output = input @ weight^T + bias Input shape: (batch, in_features) row-major Weight shape: (out_features, in_features) row-major Bias shape: (out_features) Output shape: (batch, out_features) row-major

    log_softmax

    fn log_softmax(x : Array[Double]) -> Array[Double]

    LogSoftmax over a 1D array using numerically stable max subtraction. Output shape is the same as input shape.

    max_abs_diff

    fn max_abs_diff(x : Array[Double], y : Array[Double]) -> Double

    Compute max absolute difference between two arrays. Used for numerical correctness verification.

    max_pool1d

    fn max_pool1d(x : Array[Double], channels : Int, length : Int, kernel_size : Int, stride : Int) -> Array[Double]

    MaxPool1d over input shape (channels, length). Flat layout: x[c * length + l]

    relu

    fn relu(x : Array[Double]) -> Array[Double]

    ReLU activation. Output shape is the same as input shape.

    rms_norm

    fn rms_norm(x : Array[Double], gamma : Array[Double], eps : Double) -> Array[Double]

    RMSNorm over a 1D array. x, gamma shape: (features)

    sigmoid

    fn sigmoid(x : Array[Double]) -> Array[Double]

    Sigmoid activation. Output shape is the same as input shape.

    softmax

    fn softmax(x : Array[Double]) -> Array[Double]

    Softmax over a 1D array using numerically stable max subtraction. Output shape is the same as input shape.