Building standalone NNPACK with CMake.

NNPACK is a widely used CPU based neural network acceleration library. It is fast a sleek library. But building it from scratch is hard and often won’t work (even following the READEME).

Building NNPACK

On the offical READEME file said NNPACK can be build with confu and ninja. But that often fail and and is annoying to setup. Fortunately CMake is included in the project and it handles and installs required libraries in the project folder. Use it! and here is how

git clone
mkdir build
cmake ..
make -j4

then if you want to install NNPACK to your system

sudo make install


By default NNPACK is built as a .a file. Which is statically linked. If you want a shared version. add -DBUILD_SHARED_LIBS=on to the cmake command to build .so files.


Let’s write some basic NNPACK code to make sure it actually works.
(Note: Setting threadpool to nullptr in NNPACK means to run in single thread mode)
(Note: Yeah, nnp_fully_connected_inference is really multiplying a vector with a transposed matrix with a glorified name. So bias is not taken into account here.)

#include <iostream>
#include <vector>

#include <nnpack.h>

using namespace std;

ostream& operator <<(ostream& os, const std::vector<float>& vec)
    for(auto v : vec)
        os << v << " ";
    return os;

int main()
    pthreadpool_t threadpool = nullptr;//nullptr means single thread
    std::vector<float> in = {0,1,2,3};
    std::vector<float> kernel = {1,1,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    std::vector<float> res(5);

    auto init_status = nnp_initialize();
    if(init_status != nnp_status_success)
        cout << "NNPACK initialization failed: error code" << init_status << endl;

    auto status = nnp_fully_connected_inference(in.size(), kernel.size()
        , &in[0], &kernel[0], &res[0], threadpool);
    if(status != nnp_status_success)
        cout << "NNPACK executing failed: error code" << status << endl;

    cout << res << endl;
    return 0;

Compile it with

c++ main.cpp -o main -lnnapck -pthread -O3

If it gives you the output 6 0 1 2 3. Congrats! you just wrote you first NNPACK program.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Powered by

Up ↑

%d bloggers like this: