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 Mlir2ncnn

From Leeroopedia
Revision as of 16:49, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tencent_Ncnn_Mlir2ncnn.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Overview

The main converter tool that reads a TensorFlow MLIR file and produces ncnn .param and .bin model files, enabling deployment of TensorFlow 2.x models with the ncnn inference framework.

Description

mlir2ncnn follows a multi-phase conversion pipeline. In the main function, it initializes an MLIR context with the Standard, TensorFlow, and NCNN dialects, parses the input .mlir source file, and runs an NCNN optimization pass (pattern rewriting from TF ops to ncnn ops).

It then extracts the main function's basic block and iterates over all operations in three passes:

  1. A reference-counting pass that identifies weight nodes (tf.Const), tracks blob names, and counts how many times each value is referenced
  2. A weight-reduction pass that decrements reference counts for weights consumed inline by operations like ncnn.KerasConv2D, tf.Conv2D, tf.MatMul, tf.Reshape, etc.
  3. A code-generation pass that writes the ncnn param file (layer type, name, input/output counts, and layer-specific parameters) and binary weight file

Helper functions at the top (get_attr_s, get_attr_b, get_attr_i, get_attr_f, get_attr_ai, get_attr_af and their get_operation_attr_* variants) extract typed attribute values from MLIR operations.

The converter handles both TensorFlow-native operations (e.g., tf.Conv2D, tf.DepthwiseConv2dNative, tf.MatMul, tf.BiasAdd, tf.Relu, tf.Sigmoid, tf.Softmax, tf.Mean, tf.ConcatV2, tf.StridedSlice, tf.Pad, tf.Reshape, tf.ResizeBilinear, tf.ResizeNearestNeighbor) and ncnn-specific fused operations (e.g., ncnn.KerasConv2D, ncnn.KerasDense, ncnn.KerasBatchNorm, ncnn.InstanceNormAffine, ncnn.BinaryOp).

Weight data is written in ncnn's expected memory layout, including h-w-c to c-h-w transposition for 3D tensors. Split layers are automatically inserted for values referenced by multiple consumers.

Usage

Use this tool when you need to convert TensorFlow 2.x models (exported as MLIR) to ncnn format. This provides the official, maintained route for TensorFlow model conversion, complementing the other model converters (Caffe, ONNX, MXNet, Darknet) in the ncnn toolchain.

Code Reference

Source Location

Signature

static std::string get_mlir_value_uniq_id(const mlir::Value& value);
static std::string get_attr_s(const mlir::Attribute& attr);
static int get_attr_b(const mlir::Attribute& attr);
static int get_attr_i(const mlir::Attribute& attr);
static float get_attr_f(const mlir::Attribute& attr);

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

Import

// CLI tool - no import needed
// Usage: ./mlir2ncnn [mlir] [ncnnparam] [ncnnbin]

I/O Contract

Inputs

Name Type Required Description
mlir file path (string) Yes Path to the TensorFlow MLIR (.mlir) source file
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 with transposed weight data

Usage Examples

Basic Conversion

./mlir2ncnn model.mlir model.param model.bin

Default Output Names

./mlir2ncnn model.mlir
# Produces ncnn.param and ncnn.bin in the current directory

Related Pages

Page Connections

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