onnx

    Pure-MoonBit cross-platform ONNX inference engine with typed IR, graph optimizations, and V128 SIMD kernels

    onnx
    inference
    ml
    simd
    v128
    wasm
    native
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    1 hour ago
    Downloads
    2

    #matrixlt/onnx

    纯 MoonBit 编写的跨端 ONNX 推理引擎与统一门面库,组合了 matrixlt/tensor、matrixlt/ml_kernels、matrixlt/onnx_proto 与 matrixlt/nn_ir,无外部 C/C++ 运行时依赖。

    #安装

    moon add matrixlt/onnx

    #子包结构

    • matrixlt/onnx(根包门面 @onnx):重导出 Session、BackendKind、RuntimeError、OpTiming、RunProfile、PoolStats、模型加载与校验入口。
    • matrixlt/onnx/frontend:ONNX OnnxModel → nn_ir::IrGraph Lowering(属性解析、Opset 语义适配与拓扑排序)。
    • matrixlt/onnx/ops:面向 Tensor 的 ONNX 算子实现(逐元素/广播、激活、形状变换、MatMul/Gemm、Conv2D/PackedConvWeight/MaxPool/AveragePool/BatchNorm),底层复用 matrixlt/ml_kernels 与 BufferPool。
    • matrixlt/onnx/session:Session 执行调度引擎:
      • 支持 Scalar 与 Simd(原生 V128)双后端切换;
      • 初始化期自动预打包 MatMul、Gemm(含 trans_b=true)与 Conv 静态权重;
      • 内置别名感知的静态生命周期分析与 BufferPool 内存复用(预热后中间激活与 im2col 工作区 100% 命中);
      • 提供逐算子延迟剖析接口 run_profiled / run_single_profiled 与 RunProfile::format_table()。

    #快速示例

    let session = @onnx.session_from_bytes(model_bytes, backend=@session.Simd)
    let (output, profile) = session.run_single_profiled(input_tensor)
    println(profile.format_table())

    #许可证

    Apache-2.0

    BackendKind

    推理计算后端选择:
    • Scalar:100% 纯 MoonBit 标量参考实现
    • Simd :基于 matrixlt/ml_kernels 的 MoonBit V128 SIMD 向量化内核 + 静态权重预打包缓存

    OnnxModel

    解析并解码完成后的 ONNX 模型对象。 隔离了底层自动生成的 Protobuf 细节,向上层 IR 与推理引擎提供干净接口。

    OpTiming

    单个节点的逐算子耗时剖析记录。

    PoolStats

    缓冲区复用池统计信息,用于监控高频推理场景下的内存复用命中率与 GC 压力。

    RunProfile

    单次推理的完整剖析报告,包含总耗时与每个节点的耗时分布。

    RuntimeError

    推理会话构建与执行阶段可能抛出的结构化错误。

    Session

    matrixlt/onnx 根包门面:对外提供简洁稳定的推理引擎入口。

    Tensor

    using @matrixlt/tensor { type Tensor }

    多维张量核心结构。 采用 (data, offset, shape, strides) 视图模型,使 Transpose / Squeeze / Reshape(连续时) 能够在不复制底层缓冲的前提下零拷贝完成。

    engine_version

    fn engine_version() -> String

    返回当前引擎版本字符串。

    load_f32_file

    fn load_f32_file(path : String, shape : Array[Int]) ->
    Tensor
    raise
    RuntimeError

    从小端序 .f32 文件读取张量。

    load_model_from_bytes

    从内存 .onnx 字节流加载模型元信息与权重。

    load_model_from_file

    从本地文件路径加载 .onnx 模型。

    max_abs_diff_f32

    计算两个 Float32 张量之间的最大绝对误差。

    session_from_bytes

    从内存 .onnx 字节流直接创建推理会话(全平台通用)。

    session_from_file

    从本地 .onnx 文件直接创建推理会话。

    Source Files