Maratyszcza / NNPACK

Acceleration package for neural networks on multi-core CPUs
BSD 2-Clause "Simplified" License
1.68k stars 316 forks source link

Generating shared library (libnnpack.so) #100

Closed dslate closed 7 years ago

dslate commented 7 years ago

I am trying to build NNPACK for use with torch on an x86_64 system (Linux LC2430HD 4.4.0-96-generic #119-Ubuntu SMP Tue Sep 12 14:59:54 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux). I used these shell commands: rm -rf NNPACK git clone https://github.com/Maratyszcza/NNPACK.git cd NNPACK confu setup python ./configure.py ninja luarocks install https://raw.githubusercontent.com/szagoruyko/nnpack.torch/master/nnpack-scm-1.rockspec

However, the build produced a static library libnnpack.a rather than a shared library libnnpack.so, which as far as I know is necessary for running require 'nnpack' in torch. I noticed that in the github page for szagoruyko/nnpack.torch there is a note under "CREDITS" that says "Thanks to @Maratyszcza for adding the option to generate shared NNPACK library." However, I don't see any option for doing that. After "git clone https://github.com/Maratyszcza/NNPACK.git" the NNPACK/Configure.py file contains: build.static_library("nnpack", nnpack_objects).

I even tried recompiling NNPACK with -fPIC, converting the resulting libnnpack.a to a shared library, and storing the resulting libnnpack.so in the usual place for torch to find, but that didn't work either.

Can anyone tell me how to generate libnnpack.so for use with torch?

Thanks.

edmBernard commented 7 years ago

you can check here : https://github.com/Maratyszcza/NNPACK/issues/56 the last commit that allow shared library is 9c6747d7b80051b40e6f92d6828e2ed997529cd2

git clone --recursive https://github.com/Maratyszcza/NNPACK.git && cd NNPACK && \
    git reset --hard 9c6747d7b80051b40e6f92d6828e2ed997529cd2 && \
    git submodule init && git submodule update --recursive && \
    python ./configure.py --enable-shared && \
    $LIB_DIR/ninja/./ninja
dslate commented 7 years ago

Thanks edmBernard for your help. Using your script I was able to generate libnnpack.so and copy it to the appropriate directory for use by torch. However, require 'nnpack' still could not find libnnpack.so! I finally found what appears to be the problem: at the end of install/share/lua/5.1/nnpack/ffi.lua are the lines: -- local C = ffi.load'nnpack' local C = ffi.load'./libnnpack.so' return C

I don't understand why it is trying to load ./libnnpack.so from the current directory. I changed the code to uncomment the other line: local C = ffi.load'nnpack' -- local C = ffi.load'./libnnpack.so' return C

After that mod require 'nnpath' succeeded. Now I will try to use it to speed up my CPU-based convolutional neural nets.

dslate commented 7 years ago

Sorry, I accidentally hit the wrong button and closed the issue.

Maratyszcza commented 7 years ago

You can use the new CMake configuration scripts to build a PIC or shared library.

umbralada commented 6 years ago

@Maratyszcza and how to enable shared library with cmake? There is no instructions. I have tryed to use cmake -DNNPACK_LIBRARY_TYPE=shared but with no success. The problem is libcpuinfo compilled statically and then i had error:

Linking C shared library libnnpack.so /usr/bin/ld: deps/cpuinfo/libcpuinfo.a(init.c.o): relocation R_X86_64_PC32 against symbol `cpuinfo_is_initialized' can not be used when making a shared object;

Maratyszcza commented 6 years ago

@umbralada Try to add -DCMAKE_POSITION_INDEPENDENT_CODE=ON

umbralada commented 6 years ago

@Maratyszcza this helps to compile libnnpack.so but it useless because all other libs like libcpuinfo.a, libpthreadpool.a and others still compilled statically, there is no .so files ant thus libnnpack.so cannot be loaded.

Maratyszcza commented 6 years ago

@umbralada If other dependencies are compiled statically, they are merged into libnnpack.so when it is linked.

Shivankit99 commented 4 years ago

@Maratyszcza Im trying to compile NNPACK for raspberry pi(64 bit ARM CPU) using cmake But during testing im getting the same error that libnnpack.so could not be opened. Ive tried adding -DCMAKE_POSITION_INDEPENDENT_CODE=ON as well as -DBUILD_SHARED_LIBS=on The build for NNPACK are successful but im unable to create the .so files required Any suggestions on how i should proceed ?

Maratyszcza commented 4 years ago

Try -DNNPACK_LIBRARY_TYPE=shared

Shivankit99 commented 4 years ago

@Maratyszcza Thanks for the reply but that still gives me the same error: ./darknet: error while loading shared libraries: libnnpack.so: cannot open shared object file: No such file or directory this is the exact error in case it helps Also in which folder are these libraries present ?

Maratyszcza commented 4 years ago

The library should be in the build directory

Shivankit99 commented 4 years ago

I found the library, just had one more question. Will building NNPACK with cmake automatically add the libraries and header files to the system environment or does it need to be done explicitly. If so how are we supposed to do it ?

Maratyszcza commented 4 years ago

If you want to install NNPACK libraries & headers in the system, do make install or ninja install

Shivankit99 commented 4 years ago

Thank you so much for the help @Maratyszcza