Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn Onnx2ncnn

From Leeroopedia


Knowledge Sources
Domains Model_Conversion, ONNX_Framework
Last Updated 2026-02-09 19:00 GMT

Overview

Converts ONNX model files (.onnx) into ncnn's native .param and .bin format by deserializing the protobuf-encoded ONNX graph and translating each operator to its ncnn equivalent.

Description

onnx2ncnn is the primary (now legacy) converter for deploying ONNX models with ncnn, covering a large number of ONNX operators across its 6000+ lines of translation logic. The tool now prints a recommendation to use PNNX (PyTorch Neural Network eXchange) for more accurate and elegant conversion results.

The converter uses Google Protocol Buffers to parse the ONNX ModelProto. The read_proto_from_binary function deserializes the .onnx file with a large byte limit (INT_MAX) via SetTotalBytesLimit to handle large models.

Helper functions extract typed attributes from ONNX nodes:

  • get_node_attr_ai - extracts integer arrays from node attributes
  • set_node_attr_ai - sets integer arrays on node attributes
  • get_node_attr_af - extracts float arrays from node attributes
  • get_node_attr_i - extracts single integer values
  • get_node_attr_f - extracts single float values
  • get_node_attr_s - extracts string values
  • get_node_attr_tensor - extracts tensor (weight) values

The main conversion loop iterates over all graph nodes and maps ONNX operators to ncnn layers. Supported operators include but are not limited to: Conv, ConvTranspose, BatchNormalization, MaxPool, AveragePool, GlobalAveragePool, Relu, LeakyRelu, Sigmoid, Tanh, Softmax, Gemm, MatMul, Flatten, Reshape, Transpose, Concat, Slice, Gather, Squeeze, Unsqueeze, Resize, Upsample, Pad, ReduceMean, ReduceSum, Clip, Add, Sub, Mul, Div, Pow, Sqrt, Exp, Log, Abs, Neg, and many more.

Weight tensors are converted from ONNX's format to ncnn's expected memory layout during the translation process.

Usage

Use this tool when you have a pre-existing ONNX model (.onnx file) that you need to deploy with ncnn. Note that for PyTorch-derived models, the recommended path is now PNNX (pip3 install pnnx), which provides more accurate conversion. onnx2ncnn remains useful for models from other frameworks that export to ONNX format.

Code Reference

Source Location

Signature

static bool read_proto_from_binary(const char* filepath, onnx::ModelProto* message);

static std::vector<int> get_node_attr_ai(const onnx::NodeProto& node, const char* key);
static void set_node_attr_ai(onnx::NodeProto& node, const char* key, const std::vector<int>& value);
static std::vector<float> get_node_attr_af(const onnx::NodeProto& node, const char* key);
static int get_node_attr_i(const onnx::NodeProto& node, const char* key, int def = 0);
static float get_node_attr_f(const onnx::NodeProto& node, const char* key, float def = 0.f);
static std::string get_node_attr_s(const onnx::NodeProto& node, const char* key);
static onnx::TensorProto get_node_attr_tensor(const onnx::NodeProto& node, const char* key);

int main(int argc, char** argv);
// Usage: onnx2ncnn [onnxpb] [ncnnparam] [ncnnbin]

Import

// CLI tool - no import needed
// Usage: ./onnx2ncnn [onnxpb] [ncnnparam] [ncnnbin]

I/O Contract

Inputs

Name Type Required Description
onnxpb file path (string) Yes Path to the ONNX model file (.onnx protobuf)
ncnnparam file path (string) No Output .param file path (defaults to "ncnn.param")
ncnnbin file path (string) No Output .bin file path (defaults to "ncnn.bin")

Outputs

Name Type Description
ncnn.param text file ncnn parameter file with magic number 7767517 and layer definitions
ncnn.bin binary file ncnn binary weight file containing converted weight data

Usage Examples

Basic Conversion

./onnx2ncnn model.onnx model.param model.bin

Default Output Names

./onnx2ncnn model.onnx
# Produces ncnn.param and ncnn.bin in the current directory

Recommended Alternative (PNNX)

# For PyTorch models, PNNX is now recommended over onnx2ncnn
pip3 install pnnx
pnnx model.onnx

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment