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.

Principle:Tencent Ncnn Layer Abstraction

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


Knowledge Sources
Domains Software_Architecture, Neural_Network_Inference
Last Updated 2026-02-09 19:00 GMT

Overview

A polymorphic base class for all neural network operators that defines a uniform lifecycle and inference interface, extended through a plugin architecture of factory-registered subclasses.

Description

Layer abstraction is a design pattern in which every computational operator in a neural network (convolution, pooling, activation, normalization, etc.) inherits from a single base class that declares a standard set of virtual methods: loading parameters from a dictionary, loading weights from a binary stream, creating/destroying execution pipelines, and performing forward inference. The base class also carries declarative capability flags (one_blob_only, support_inplace, support_vulkan, support_packing, support_fp16_storage, support_int8_storage) that the runtime inspects to choose optimal data paths.

The inference interface is overloaded: a layer may accept a single tensor or a vector of tensors, and may operate out-of-place (producing new output tensors) or in-place (modifying input tensors directly). GPU-accelerated variants add parallel virtual methods that operate on device-side tensor types.

New operator types are registered through a factory pattern: a compile-time or runtime registry maps type indices and type name strings to creator/destroyer function pointers. This allows the framework to instantiate any layer by name when parsing a model file, and enables users to register custom layers or override built-in implementations without modifying framework source code.

Supporting this abstraction are auxiliary data structures: a Blob class that represents named data edges in the graph (linking a producer layer to a consumer layer), a ParamDict class that provides type-safe indexed parameter storage (int, float, array, string keyed by integer ID up to 32 slots), and an expression evaluator that resolves dynamic shape expressions at runtime.

Usage

Apply this principle when designing an extensible inference engine where new operators must be added frequently without altering the core graph executor. The uniform interface ensures that the scheduler can treat all operators identically during graph traversal, memory planning, and execution.

Theoretical Basis

Layer lifecycle:

1. Factory creates Layer subclass from type index or name
2. load_param(ParamDict)   -- parse operator-specific parameters
3. load_model(ModelBin)    -- read weight data from binary stream
4. create_pipeline(Option) -- allocate resources, compile GPU shaders
5. forward(bottom, top)    -- execute inference (possibly multiple times)
6. destroy_pipeline(Option)-- release resources
7. ~Layer()                -- destructor

Polymorphic forward interface:

class Layer {
    // Multi-blob forward
    virtual int forward(const std::vector<Mat>& bottom_blobs,
                        std::vector<Mat>& top_blobs,
                        const Option& opt) const;
    // Single-blob forward
    virtual int forward(const Mat& bottom_blob,
                        Mat& top_blob,
                        const Option& opt) const;
    // In-place variants
    virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs,
                                const Option& opt) const;
    virtual int forward_inplace(Mat& bottom_top_blob,
                                const Option& opt) const;
};

Factory registration pattern:

// Creator function pointer type
typedef Layer* (*layer_creator_func)(void*);

// Registry entry
struct layer_registry_entry {
    const char* name;           // e.g. "Convolution"
    layer_creator_func creator; // factory function
};

// Macro for defining a creator
#define DEFINE_LAYER_CREATOR(name) \
    Layer* name##_layer_creator(void*) { return new name; }

Related Pages

Implemented By

Page Connections

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