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 Ncnnmerge

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


Knowledge Sources
Domains Model_Utilities, Model_Serialization
Last Updated 2026-02-09 19:00 GMT

Overview

Command-line tool that merges multiple ncnn model pairs (param + bin files) into a single combined param file and a single combined bin file, enabling multi-model inference through a unified model.

Description

ncnnmerge takes pairs of param/bin file paths as arguments, with the final two arguments being the output param and bin paths. It requires at least 3 model pairs (minimum 7 arguments).

The tool consists of two core functions:

copy_param reads each input .param file, validates the ncnn magic number (7767517), extracts layer and blob counts, and rewrites each layer's definition with a namespace prefix derived from the input filename (e.g., model1/layer_name). All layer names, bottom blob names, and top blob names are prefixed with this namespace to prevent naming collisions between merged models. Layer-specific parameter strings are copied verbatim.

copy_bin performs a straightforward binary copy of each .bin file by reading in 4096-byte chunks and appending them to the output binary file.

The main function first writes a placeholder header line (with padding spaces for the layer/blob counts), accumulates the total layer and blob counts across all models, then seeks back to the beginning of the output param file to overwrite the placeholder with the actual totals. The output param file starts with the ncnn magic number 7767517 followed by the combined layer count and blob count.

Usage

Use this tool when you need to combine independently trained models into a single deployment unit. This is useful for applications that need to run multiple models (e.g., detection + classification pipelines) and want to simplify model management by loading a single merged model file. The namespace prefixing ensures that blob and layer names remain unique across the merged models.

Code Reference

Source Location

Signature

static int copy_param(const char* parampath, FILE* outparamfp,
                      int* total_layer_count, int* total_blob_count);

static int copy_bin(const char* binpath, FILE* outbinfp);

int main(int argc, char** argv);
// Usage: ncnnmerge [param1] [bin1] [param2] [bin2] ... [outparam] [outbin]

Import

// CLI tool - no import needed
// Usage: ./ncnnmerge [param1] [bin1] [param2] [bin2] ... [outparam] [outbin]

I/O Contract

Inputs

Name Type Required Description
param1 file path (string) Yes First model's .param file path
bin1 file path (string) Yes First model's .bin file path
param2 file path (string) Yes Second model's .param file path
bin2 file path (string) Yes Second model's .bin file path
... file path pairs No Additional model param/bin pairs
outparam file path (string) Yes Output merged .param file path (last two args)
outbin file path (string) Yes Output merged .bin file path (last two args)

Outputs

Name Type Description
outparam text file Merged ncnn parameter file with namespaced layer/blob names and combined counts
outbin binary file Concatenated binary weight data from all input models

Usage Examples

Merge Two Models

./ncnnmerge detect.param detect.bin classify.param classify.bin merged.param merged.bin

Merge Three Models

./ncnnmerge face_detect.param face_detect.bin \
           landmark.param landmark.bin \
           recognize.param recognize.bin \
           merged.param merged.bin

Related Pages

Page Connections

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