Skip to content

Commit

Permalink
Merge branch 'master' into doc
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisonooo committed May 20, 2019
2 parents 1f9f677 + 08660e3 commit d401219
Show file tree
Hide file tree
Showing 22 changed files with 523 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .daq_pm/configs/converter_python
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim)
name binary-nn
type cpp
build_dir build_x86_python
cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBNN_BUILD_PYTHON=ON
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ compile_commands.json
build-*/
build_*/
.daq_pm/status/
.setuptools-cmake-build/
.eggs/
tools/onnx2bnn/python/dist/
tools/onnx2bnn/python/onnx2bnn.egg-info/
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@
[submodule "third_party/googletest"]
path = third_party/googletest
url = https://github.com/google/googletest
[submodule "third_party/protobuf"]
path = third_party/protobuf
url = https://github.com/protocolbuffers/protobuf
[submodule "third_party/pybind11"]
path = third_party/pybind11
url = https://github.com/pybind/pybind11
23 changes: 22 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2019 JD.com Inc. JD AI

cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.9)
project(dabnn CXX)

message(STATUS "CMake Version: ${CMAKE_VERSION}")
Expand All @@ -18,13 +18,19 @@ option(BNN_BUILD_TEST "Build test" ON)
option(BNN_BUILD_BENCHMARK "Build benchmark" ON)
option(BNN_NET_BENCHMARK "define BNN_BENCHMARK" OFF)
option(BNN_CHECK_CONSISTENCY "Make some layers non inplace to check results" OFF)
option(BNN_SYSTEM_PROTOBUF "Use system protobuf to build onnx2bnn" ON)
option(BNN_BUILD_PYTHON "Build onnx2bnn python interface" OFF)
option(BNN_USE_MSVC_STATIC_RUNTIME "Link onnx2bnn to msvc static runtime" ON)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Android")
set(BNN_BUILD_ANDROID ON)
else()
set(BNN_BUILD_ANDROID OFF)
endif()

include(cmake/utils.cmake)
bnn_add_msvc_runtime_flag()

include(cmake/common.cmake)
include(cmake/system.cmake)

Expand Down Expand Up @@ -62,7 +68,22 @@ if (${BNN_BUILD_ANDROID})
else()
set(CMAKE_CXX_STANDARD 11)

if (${BNN_BUILD_PYTHON})
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(third_party/pybind11)
endif()

include(cmake/protobuf.cmake)
if (${BNN_SYSTEM_PROTOBUF})
find_package(Protobuf)
endif()
if (NOT Protobuf_FOUND)
message(STATUS "BNN_SYSTEM_PROTOBUF is ON but protobuf is not found, using the packed protobuf..")
set(BNN_SYSTEM_PROTOBUF OFF CACHE BOOL "Use system protobuf when building onnx2daq" FORCE)
endif()

include(cmake/onnx.cmake)
configure_onnx()

add_subdirectory(tools)
endif()
5 changes: 2 additions & 3 deletions ci/build_onnx2bnn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
set -e

mkdir build_onnx2bnn && cd build_onnx2bnn
# azure pipeline image aliases cmake to their cmake 3.12
/usr/bin/cmake ..
/usr/bin/cmake --build . -- -j$(nproc)
cmake ..
cmake --build . -- -j$(nproc)
cd -
5 changes: 5 additions & 0 deletions ci/dabnn_build_and_test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
trigger:
branches:
include:
- refs/heads/*
- refs/tags/*
pool:
vmImage: 'macOS-10.14'
steps:
Expand Down
106 changes: 81 additions & 25 deletions ci/onnx2bnn_build.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,82 @@
pool:
vmImage: 'ubuntu-16.04'
steps:
- checkout: self
submodules: true
- script: sudo apt install -y cmake protobuf-compiler libprotobuf-dev
displayName: Install cmake 3.5.1 and protobuf
- bash: ci/build_onnx2bnn.sh
displayName: Build
- bash: ci/build_appimage.sh
displayName: Build AppImage
- task: CopyFiles@2
inputs:
contents: 'onnx2bnn.AppImage'
trigger:
branches:
include:
- refs/heads/*
- refs/tags/*
jobs:
- job: Linux_AppImage
pool:
vmImage: 'ubuntu-16.04'
steps:
- checkout: self
submodules: true
- script: sudo apt install -y protobuf-compiler libprotobuf-dev
displayName: Install protobuf
- bash: ci/build_onnx2bnn.sh
displayName: Build
- bash: ci/build_appimage.sh
displayName: Build AppImage
- task: CopyFiles@2
inputs:
contents: 'onnx2bnn.AppImage'
targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: $(Build.ArtifactStagingDirectory)
artifactName: onnx2bnn AppImage
- task: GitHubRelease@0
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
inputs:
gitHubConnection: 'dabnn release'
repositoryName: '$(Build.Repository.Name)'
action: 'edit'
tag: '$(Build.SourceBranchName)'
target: '$(Build.SourceVersion)'
assets: '$(Build.ArtifactStagingDirectory)/*'
assetUploadMode: 'replace'
- job: Windows_Python_Package
pool:
vmImage: 'vs2017-win2016'
steps:
- checkout: self
submodules: true
- task: UsePythonVersion@0
inputs:
versionSpec: '3.7'
addToPath: true
architecture: 'x64'
- template: template_onnx2bnn_build_python.yml
- task: UsePythonVersion@0
inputs:
versionSpec: '3.6'
addToPath: true
architecture: 'x64'
- template: template_onnx2bnn_build_python.yml
- task: UsePythonVersion@0
inputs:
versionSpec: '3.5'
addToPath: true
architecture: 'x64'
- template: template_onnx2bnn_build_python.yml
- task: CopyFiles@2
inputs:
sourceFolder: '.setuptools-cmake-build\tools\onnx2bnn\Release\'
contents: 'onnx2bnn.exe'
targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: $(Build.ArtifactStagingDirectory)
artifactName: onnx2bnn AppImage
- task: GitHubRelease@0
inputs:
gitHubConnection: 'dabnn release'
repositoryName: '$(Build.Repository.Name)'
action: 'create'
target: '$(Build.SourceVersion)'
assets: '$(Build.ArtifactStagingDirectory)/onnx2bnn.AppImage'
- task: GitHubRelease@0
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
inputs:
gitHubConnection: 'dabnn release'
repositoryName: '$(Build.Repository.Name)'
action: 'edit'
tag: '$(Build.SourceBranchName)'
target: '$(Build.SourceVersion)'
assets: '$(Build.ArtifactStagingDirectory)/*'
assetUploadMode: 'replace'
- script: python -m twine upload dist/* --verbose
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
displayName: Upload wheel to PyPI
workingDirectory: tools/onnx2bnn/python/
env:
TWINE_USERNAME: $(twineUsername)
TWINE_PASSWORD: $(twinePassword)
6 changes: 6 additions & 0 deletions ci/template_onnx2bnn_build_python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
steps:
- script: python -m pip install --user --upgrade setuptools wheel twine
displayName: Install setuptools, wheel and twine
- script: python setup.py sdist bdist_wheel
workingDirectory: tools/onnx2bnn/python/
displayName: Build onnx2bnn python package
20 changes: 19 additions & 1 deletion cmake/onnx.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
# Copyright 2019 JD.com Inc. JD AI

macro(configure_onnx)
if (NOT ${BNN_SYSTEM_PROTOBUF})
include(${PROJECT_SOURCE_DIR}/cmake/protobuf.cmake)
configure_protobuf()
endif()

message(STATUS "Configuring onnx...")
set(BNN_ONNX_NAMESPACE onnx_bnn)

if (MSVC)
set(ONNX_CMAKELISTS ${PROJECT_SOURCE_DIR}/third_party/onnx/CMakeLists.txt)
file(READ ${ONNX_CMAKELISTS} content)
# Treating warnings as errors breaks ci, we have no other way to opt-out
string(
REPLACE
"/WX"
""
content
"${content}"
)
file(WRITE ${ONNX_CMAKELISTS} "${content}")
endif()
set(ONNX_USE_MSVC_STATIC_RUNTIME ${BNN_USE_MSVC_STATIC_RUNTIME})
set(ONNX_NAMESPACE ${BNN_ONNX_NAMESPACE} CACHE STRING "onnx namespace")
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/onnx)
# Since https://github.com/onnx/onnx/pull/1318 is merged, we don't need to set it manually
Expand Down
16 changes: 16 additions & 0 deletions cmake/protobuf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
macro(alias_protobuf)
add_library(protobuf::libprotoc ALIAS libprotoc)
add_library(protobuf::libprotobuf ALIAS libprotobuf)
add_library(protobuf::libprotobuf-lite ALIAS libprotobuf-lite)
endmacro()

macro(configure_protobuf)
message(STATUS "Configuring protobuf...")
option(protobuf_BUILD_TESTS "" OFF)
option(protobuf_BUILD_EXAMPLES "" OFF)
option(protobuf_BUILD_SHARED_LIBS "" OFF)
option(protobuf_BUILD_PROTOC_BINARIES "" ON)
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/protobuf/cmake)
alias_protobuf()
endmacro()

23 changes: 23 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2019 JD.com Inc. JD AI

# Add MSVC RunTime Flag
function(bnn_add_msvc_runtime_flag)
if (MSVC)
if(${BNN_USE_MSVC_STATIC_RUNTIME})
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
add_compile_options(/MTd)
else()
add_compile_options(/MT)
endif()
else()
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
add_compile_options(/MDd)
else()
add_compile_options(/MD)
endif()
endif()
endif()
endfunction()



2 changes: 1 addition & 1 deletion common/baseline.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <bitset>

#include <common/helper.h>
#include <common/common_bitpack.h>
#include <dabnn/bitpack.h>
#include <dabnn/mat.h>

inline int bitcount(uint64_t x) {
Expand Down
34 changes: 7 additions & 27 deletions common/common_bitpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
#include <cstdint>

#include <common/helper.h>
#include <dabnn/mat.h>

inline void pack_128_fallback(const float *float_ptr, void *binary_ptr,
size_t size) {
uint64_t *ui64_ptr = static_cast<uint64_t *>(binary_ptr);
const size_t UNIT_LEN = 64;
std::bitset<UNIT_LEN> bits1;
std::bitset<UNIT_LEN> bits2;
static_assert(std::is_same<decltype(bits1.to_ulong()), uint64_t>::value,
"bits.to_ulong() must return uint64_t");
static_assert(sizeof(decltype(bits1.to_ullong())) * CHAR_BIT == 64,
"bits.to_ullong() must return a 64-bit element");

FORZS(j, size, 128) {
FORZS(i, 128, 4) {
Expand All @@ -27,8 +26,8 @@ inline void pack_128_fallback(const float *float_ptr, void *binary_ptr,
bits2[t] = (*(float_ptr + j + i + 2) > 0);
bits2[t + 32] = (*(float_ptr + j + i + 3) > 0);
}
*ui64_ptr++ = bits1.to_ulong();
*ui64_ptr++ = bits2.to_ulong();
*ui64_ptr++ = bits1.to_ullong();
*ui64_ptr++ = bits2.to_ullong();
}
}

Expand All @@ -38,9 +37,9 @@ inline void pack_64_bitset(const float *fptr, uint64_t *buf) {
for (size_t i = 0; i < UNIT_LEN; i++) {
bits[i] = (*(fptr + i) > 0);
}
static_assert(std::is_same<decltype(bits.to_ulong()), uint64_t>::value,
"bits.to_ulong() must return uint64_t");
*buf = bits.to_ulong();
static_assert(sizeof(decltype(bits.to_ullong())) * CHAR_BIT == 64,
"bits.to_ullong() must return a 64-bit element");
*buf = bits.to_ullong();
}

inline void pack_64_bitfield(const float *fptr, uint64_t *buf) {
Expand Down Expand Up @@ -184,23 +183,4 @@ inline void pack_64_bitfield(const float *fptr, uint64_t *buf) {
*buf = u.u64;
}

inline void pack_mat_64(const bnn::Mat &float_mat, bnn::Mat &binary_mat) {
BNN_ASSERT(
float_mat.w * float_mat.c > 0 && float_mat.w * float_mat.c % 64 == 0,
float_mat.w * float_mat.c);
BNN_ASSERT(float_mat.c / 64 == binary_mat.c && float_mat.c % 64 == 0, "");

FORZ(n, float_mat.n) {
FORZ(h, float_mat.h) {
auto *fptr = float_mat.point<float>(n, h, 0);
auto *bptr = binary_mat.point<uint64_t>(n, h, 0);
FORZ(i, float_mat.w * float_mat.c / 64) {
pack_64_bitfield(fptr, bptr);
fptr += 64;
bptr++;
}
}
}
}

#endif /* COMMON_BITPACK_H */
19 changes: 19 additions & 0 deletions dabnn/bitpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ inline void pack_mat_128(const bnn::Mat &float_mat, bnn::Mat &binary_mat) {
float_mat.total());
}

inline void pack_mat_64(const bnn::Mat &float_mat, bnn::Mat &binary_mat) {
BNN_ASSERT(
float_mat.w * float_mat.c > 0 && float_mat.w * float_mat.c % 64 == 0,
float_mat.w * float_mat.c);
BNN_ASSERT(float_mat.c / 64 == binary_mat.c && float_mat.c % 64 == 0, "");

FORZ(n, float_mat.n) {
FORZ(h, float_mat.h) {
auto *fptr = float_mat.point<float>(n, h, 0);
auto *bptr = binary_mat.point<uint64_t>(n, h, 0);
FORZ(i, float_mat.w * float_mat.c / 64) {
pack_64_bitfield(fptr, bptr);
fptr += 64;
bptr++;
}
}
}
}

inline void pack_mat(const bnn::Mat &float_mat, bnn::Mat &binary_mat) {
BNN_ASSERT(float_mat.c % 64 == 0, float_mat.c);
if (float_mat.c % 128 == 0) {
Expand Down
1 change: 1 addition & 0 deletions third_party/protobuf
Submodule protobuf added at 6973c3
1 change: 1 addition & 0 deletions third_party/pybind11
Submodule pybind11 added at 97784d
Loading

0 comments on commit d401219

Please sign in to comment.