Skip to content

Instantly share code, notes, and snippets.

View kketernality's full-sized avatar

Chang-Yi Tsai kketernality

  • Taiwan
View GitHub Profile

Detail behind NCNN's factory pattern

NCNN adopts the factory pattern to create the layers of a nueral network. It's also the way the well-known library Caffe takes. It differs from Caffe in the implementation of the registry table. On one hand, the Caffe registry is populated in runtime as the side effect of initializion of global variable (which is a popular way for library initialization). On the other hand, the NCNN registry is determined in compile time. The registry is generated in a brilliant way using CMake instead of a hand-crafted table. NCNN's approach provides several benefits compared to Caffe's approach.

First, it's suitable for building a static library. When building a static library, the linker will strip any unused global variable to minimize the size of the library. This makes sense but it also strips the global variable which need to be inintialized to insert te layer creator into the registry. Tricky linker flags and related instrutions are required to resolve this issue. By creating

#include <cstdlib>
bool isEven(int n)
{
return n % 2 == 0;
}
// Note the computation order of operator
int computeSumToNumber(int n)
{
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>