Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Alibaba MNN Model Inspection

From Leeroopedia


Field Value
Principle Name Model_Inspection
Category Model_Conversion_Pipeline
Description Inspecting and validating MNN model structure and metadata
Applies To Post-conversion validation and debugging

Overview

After converting a model to MNN's .mnn format, it is important to inspect the model's structure and metadata to confirm that the conversion produced a valid and expected result. Model inspection allows developers to verify input/output specifications, check tensor dimensions, review operator composition, and examine model version information before deploying the model to a device.

Theory: Post-Conversion Validation

Model conversion is a complex process involving operator mapping, graph optimization, and format serialization. Even when numerical verification passes (comparing output values), structural inspection provides a complementary validation layer that catches a different class of issues:

What Inspection Reveals

  • Input specification -- Names, shapes, data types, and dimension formats (NCHW vs NHWC) of all model inputs. This is critical for ensuring the application feeds data in the correct format.
  • Output specification -- Names and types of all model outputs, allowing the application to correctly interpret inference results.
  • Dimension format -- The model's default dimension format (NCHW, NHWC, or NC4HW4). Mismatched formats between the model and application code are a common source of incorrect results.
  • Model version -- The MNN version used to produce the model. Older models (pre-2.0.0) may not support all features of newer MNN runtimes.
  • Metadata -- Custom key-value metadata pairs embedded in the model (e.g., model origin, training configuration, business code).
  • Full graph structure -- When exported to JSON, the complete operator graph is visible, including all operator types, parameters, tensor connections, and sub-graphs.

Why Inspection Matters for Deployment

  • Shape mismatch detection -- A model exported with fixed shapes may have unexpected input dimensions after optimization. Inspection catches this before the model is shipped.
  • Format verification -- Confirming that keepInputFormat was correctly applied and inputs are in the expected format.
  • Operator audit -- Reviewing the operator list ensures no unsupported or unexpected operators were introduced during conversion.
  • Size analysis -- JSON export allows inspection of weight sizes and parameter counts to verify that quantization or FP16 conversion was applied correctly.
  • Version compatibility -- Checking the model version against the target device's MNN runtime version prevents deployment of incompatible models.

Inspection Methods

Quick Metadata Dump

The simplest inspection method: dump model metadata (inputs, outputs, version, format) to the console. This uses MNN's Module::load() and Module::getInfo() APIs internally to parse the model and extract structural information.

Full JSON Export

For deep inspection, the entire MNN model can be exported to a human-readable JSON representation. This JSON contains:

  • All operators with their types and parameters
  • Tensor names and connections
  • Sub-graphs (for models with control flow)
  • Optionally, weight data (can be excluded for readability with large models)

The JSON export uses FlatBuffers' built-in FlatBufferToString() function to convert the binary format back to a structured text representation.

Selective Weight Exclusion

For large models, the JSON export supports different levels of detail:

  • Full export (default) -- Includes all data including weights
  • Structure only (flag > 3) -- Excludes convolution weights, biases, blob data, MatMul weights, PReLU slopes, and Extra op info. This makes the JSON manageable for structural review.
  • Separate sub-graph dump (flag > 4) -- Additionally dumps each sub-graph to a separate file for models with complex control flow

Relationship to Other Validation Steps

Model inspection is complementary to numerical verification:

Aspect Numerical Verification Model Inspection
What it checks Output correctness Structural correctness
When errors are caught Operator implementation bugs Shape mismatches, format errors, missing ops
Requirements Test data + reference outputs Only the MNN model file
Speed Slower (requires inference) Fast (metadata parsing only)
Debugging depth Value-level errors Architecture-level issues

Both methods should be part of a complete conversion validation pipeline.

Related Pages

Page Connections

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