├── .clang-format ├── .daq_pm └── configs │ ├── all_v7 │ ├── arm64-v8a │ ├── arm64-v8a-wo-test │ ├── bconv_test │ ├── bconv_test_x86 │ ├── benchmark │ ├── benchmark_macro │ ├── benchmark_v7 │ ├── bgemm_test │ ├── bgemm_test_v7 │ ├── bgemm_test_x86 │ ├── bitpack_test │ ├── converter │ ├── converter_python │ ├── fconv_test │ ├── gemm_test │ ├── im2col_test │ ├── jni │ ├── net_test │ ├── net_test_debug │ ├── net_test_v7 │ ├── net_test_x86 │ ├── pad_test │ ├── run_net │ ├── run_net_debug │ ├── run_net_new_bitpack │ └── run_net_x86 ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── README_CN.md ├── benchmark ├── CMakeLists.txt ├── benchmark.cpp └── benchmark_single_model.cpp ├── binaries ├── CMakeLists.txt └── run.cpp ├── ci ├── adb_push_and_run.sh ├── android_aar │ ├── .gitignore │ ├── build.gradle │ ├── dabnn │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ └── me │ │ │ │ └── daquexian │ │ │ │ └── dabnn │ │ │ │ └── Net.java │ │ │ └── res │ │ │ └── values │ │ │ └── strings.xml │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle ├── appimage │ ├── onnx2bnn.desktop │ └── onnx2bnn.png ├── build_aar.sh ├── build_appimage.sh ├── build_dabnn.sh ├── build_dabnn_v7.sh ├── build_onnx2bnn.sh ├── dabnn_build_and_test.yml ├── download_and_convert_models.sh ├── get_cores.sh ├── onnx2bnn_build.yml ├── start_android_emulator.sh ├── start_android_emulator_v7.sh ├── template_dabnn_run_test.yml ├── template_onnx2bnn_github_release.yml └── template_onnx2bnn_publish_artifacts.yml ├── cmake ├── benchmark.cmake ├── common.cmake ├── flatbuffers.cmake ├── glog.cmake ├── gtest.cmake ├── onnx.cmake ├── protobuf.cmake ├── system.cmake └── utils.cmake ├── common ├── Shaper.cpp ├── Shaper.h ├── StrKeyMap.h ├── argh.h ├── baseline.h ├── common_bitpack.h ├── dab.fbs ├── dab_generated.h ├── flatbuffers_helper.h ├── helper.h ├── log_helper.h └── macros.h ├── dabnn ├── CMakeLists.txt ├── allocator.cpp ├── allocator.h ├── bconv.h ├── bgemm.h ├── bitpack.h ├── fconv.h ├── fused_binarize_im2col.h ├── im2col.h ├── jni │ ├── CMakeLists.txt │ ├── JavaWrapper.cpp │ ├── jni_handle.h │ ├── net_for_jni.cpp │ └── net_for_jni.h ├── layer.cpp ├── layer.h ├── layers │ ├── Add.cpp │ ├── Add.h │ ├── Affine.cpp │ ├── Affine.h │ ├── AvePool.cpp │ ├── AvePool.h │ ├── BinConv.cpp │ ├── BinConv.h │ ├── Binarize.cpp │ ├── Binarize.h │ ├── Concat.cpp │ ├── Concat.h │ ├── FloatConv.cpp │ ├── FloatConv.h │ ├── MaxPool.cpp │ ├── MaxPool.h │ ├── PRelu.cpp │ ├── PRelu.h │ ├── Pad.cpp │ ├── Pad.h │ ├── Relu.cpp │ ├── Relu.h │ ├── Shuffle.cpp │ ├── Shuffle.h │ ├── Split.cpp │ └── Split.h ├── mat.h ├── net.cpp ├── net.h └── pad.h ├── docs ├── bconv.md ├── bconv.md.in ├── bconv_CN.md ├── benchmark_and_comparison.md ├── build.md ├── model_conversion.md ├── onnx2bnn.md ├── onnx2bnn_CN.md ├── overall.md ├── overall_CN.md └── svgs │ ├── 09e963a9a257d451169d317f04f4cf59.svg │ ├── 0df921cda6b933526ab3fb2c8ba713c0.svg │ ├── 15e03f3c82848a46865db186cb4c1092.svg │ ├── 1cb45f0e1e422f5a042ce0dc8710ed27.svg │ ├── 22aefc0b275701a94e3684ede71e1cbf.svg │ ├── 252b59b1233ed40f0396e2cd369f514d.svg │ ├── 25bbbd23c3609fee3f26aa5f809dbe2e.svg │ ├── 2c08f38f094ac03aea56779378242468.svg │ ├── 2e32e0141d372413f25c35045d246695.svg │ ├── 2e67a96431b169a7b134a2ab4c5f3457.svg │ ├── 3b5fe08410dc2e357ad56d5e09c013c5.svg │ ├── 4723cf14b1da3a0da99410e67984882d.svg │ ├── 484ca82711fa06cc47440002b35c5a66.svg │ ├── 4fd661cfefdf4318d1aa35fb483796b2.svg │ ├── 5615b81594cc5f5f54f6c86a17443fea.svg │ ├── 68d27da8ea3f60dda13e915b722c2c25.svg │ ├── 7adcdcafe095c28283fc5a319a9b6cdb.svg │ ├── 7ceafe91152205b4b4d2b3a8b1565477.svg │ ├── 81299da238f63ff881f8365a2a3b638a.svg │ ├── 8217ed3c32a785f0b5aad4055f432ad8.svg │ ├── 873e1472d3de67a29299013cd3eebb39.svg │ ├── 88cf5350b4c645c31edaa0cbba3ee5f9.svg │ ├── 8edf0a665654dc211972f609f97cb684.svg │ ├── 9034606aa4dd18758a6889347abf0302.svg │ ├── 904d8a3dfde39f4fb05df9337f05b65f.svg │ ├── 9998129ab540f7bc0985032e06e974ed.svg │ ├── a6b6654f6dbe55b7fa2c8f5104fb8370.svg │ ├── ab03e97f653c3b2963d6a503b2a9719b.svg │ ├── bdbf342b57819773421273d508dba586.svg │ ├── c745b9b57c145ec5577b82542b2df546.svg │ ├── c82ef99e46a995ca2c9e5865a66d022f.svg │ ├── cc1dbbcd450fb3182ca125d94560c60d.svg │ ├── cc6323a2493d6ca0f60a01cf3df23307.svg │ ├── cfeb1241a5083b5a03c00504e02f69b3.svg │ ├── d0740c8f4fc4e3563ada4e53f43a81a1.svg │ ├── d0b09e58d8b197fff6fc95ea3bca20fe.svg │ ├── d236438ccc18393a004614a9ddc04188.svg │ ├── d4e2df111ef06f71a9c4108cb9542aff.svg │ ├── d6328eaebbcd5c358f426dbea4bdbf70.svg │ ├── dad09b192ab596ebe6643c184b041b78.svg │ ├── dc61d515b6f36dadf6ab7371698a9ef1.svg │ ├── eb7ee640b8ff98c0068ed4d9ec3baf60.svg │ ├── f0fa7d7a09a30703b30ba8aae9c1c1b5.svg │ ├── f29d99803e443e4e6e87180539b3197f.svg │ ├── f3d9f6f447d13bcef7127ff6c98710a3.svg │ ├── f5feb9f32839cb69ccdf8b0838d8c7cb.svg │ ├── f6128a2d469857252e8e52385e7a00c5.svg │ ├── f8b4daba6c4183a3c1000ebb2d64de5f.svg │ ├── f9c4988898e7f532b9f826a75014ed3c.svg │ ├── fb97d38bcc19230b0acd442e17db879c.svg │ └── ff7cbf533a4e41019c689366004849fb.svg ├── images ├── comparison_cn.png └── comparison_en.png ├── tests ├── CMakeLists.txt ├── bconv_test.cpp ├── bgemm_test.cpp ├── bitpack_test.cpp ├── fconv_test.cpp ├── im2col_test.cpp └── net_test.cpp ├── third_party └── eigen │ └── Eigen │ ├── CMakeLists.txt │ ├── Cholesky │ ├── CholmodSupport │ ├── Core │ ├── Dense │ ├── Eigen │ ├── Eigenvalues │ ├── Geometry │ ├── Householder │ ├── IterativeLinearSolvers │ ├── Jacobi │ ├── LU │ ├── MetisSupport │ ├── OrderingMethods │ ├── PaStiXSupport │ ├── PardisoSupport │ ├── QR │ ├── QtAlignedMalloc │ ├── SPQRSupport │ ├── SVD │ ├── Sparse │ ├── SparseCholesky │ ├── SparseCore │ ├── SparseLU │ ├── SparseQR │ ├── StdDeque │ ├── StdList │ ├── StdVector │ ├── SuperLUSupport │ ├── UmfPackSupport │ └── src │ ├── Cholesky │ ├── LDLT.h │ ├── LLT.h │ └── LLT_LAPACKE.h │ ├── CholmodSupport │ └── CholmodSupport.h │ ├── Core │ ├── Array.h │ ├── ArrayBase.h │ ├── ArrayWrapper.h │ ├── Assign.h │ ├── AssignEvaluator.h │ ├── Assign_MKL.h │ ├── BandMatrix.h │ ├── Block.h │ ├── BooleanRedux.h │ ├── CommaInitializer.h │ ├── ConditionEstimator.h │ ├── CoreEvaluators.h │ ├── CoreIterators.h │ ├── CwiseBinaryOp.h │ ├── CwiseNullaryOp.h │ ├── CwiseTernaryOp.h │ ├── CwiseUnaryOp.h │ ├── CwiseUnaryView.h │ ├── DenseBase.h │ ├── DenseCoeffsBase.h │ ├── DenseStorage.h │ ├── Diagonal.h │ ├── DiagonalMatrix.h │ ├── DiagonalProduct.h │ ├── Dot.h │ ├── EigenBase.h │ ├── ForceAlignedAccess.h │ ├── Fuzzy.h │ ├── GeneralProduct.h │ ├── GenericPacketMath.h │ ├── GlobalFunctions.h │ ├── IO.h │ ├── Inverse.h │ ├── Map.h │ ├── MapBase.h │ ├── MathFunctions.h │ ├── MathFunctionsImpl.h │ ├── Matrix.h │ ├── MatrixBase.h │ ├── NestByValue.h │ ├── NoAlias.h │ ├── NumTraits.h │ ├── PermutationMatrix.h │ ├── PlainObjectBase.h │ ├── Product.h │ ├── ProductEvaluators.h │ ├── Random.h │ ├── Redux.h │ ├── Ref.h │ ├── Replicate.h │ ├── ReturnByValue.h │ ├── Reverse.h │ ├── Select.h │ ├── SelfAdjointView.h │ ├── SelfCwiseBinaryOp.h │ ├── Solve.h │ ├── SolveTriangular.h │ ├── SolverBase.h │ ├── StableNorm.h │ ├── Stride.h │ ├── Swap.h │ ├── Transpose.h │ ├── Transpositions.h │ ├── TriangularMatrix.h │ ├── VectorBlock.h │ ├── VectorwiseOp.h │ ├── Visitor.h │ ├── arch │ │ ├── AVX │ │ │ ├── Complex.h │ │ │ ├── MathFunctions.h │ │ │ ├── PacketMath.h │ │ │ └── TypeCasting.h │ │ ├── AVX512 │ │ │ ├── MathFunctions.h │ │ │ └── PacketMath.h │ │ ├── AltiVec │ │ │ ├── Complex.h │ │ │ ├── MathFunctions.h │ │ │ └── PacketMath.h │ │ ├── CUDA │ │ │ ├── Complex.h │ │ │ ├── Half.h │ │ │ ├── MathFunctions.h │ │ │ ├── PacketMath.h │ │ │ ├── PacketMathHalf.h │ │ │ └── TypeCasting.h │ │ ├── Default │ │ │ ├── ConjHelper.h │ │ │ └── Settings.h │ │ ├── NEON │ │ │ ├── Complex.h │ │ │ ├── MathFunctions.h │ │ │ └── PacketMath.h │ │ ├── SSE │ │ │ ├── Complex.h │ │ │ ├── MathFunctions.h │ │ │ ├── PacketMath.h │ │ │ └── TypeCasting.h │ │ └── ZVector │ │ │ ├── Complex.h │ │ │ ├── MathFunctions.h │ │ │ └── PacketMath.h │ ├── functors │ │ ├── AssignmentFunctors.h │ │ ├── BinaryFunctors.h │ │ ├── NullaryFunctors.h │ │ ├── StlFunctors.h │ │ ├── TernaryFunctors.h │ │ └── UnaryFunctors.h │ ├── products │ │ ├── GeneralBlockPanelKernel.h │ │ ├── GeneralMatrixMatrix.h │ │ ├── GeneralMatrixMatrixTriangular.h │ │ ├── GeneralMatrixMatrixTriangular_BLAS.h │ │ ├── GeneralMatrixMatrix_BLAS.h │ │ ├── GeneralMatrixVector.h │ │ ├── GeneralMatrixVector_BLAS.h │ │ ├── Parallelizer.h │ │ ├── SelfadjointMatrixMatrix.h │ │ ├── SelfadjointMatrixMatrix_BLAS.h │ │ ├── SelfadjointMatrixVector.h │ │ ├── SelfadjointMatrixVector_BLAS.h │ │ ├── SelfadjointProduct.h │ │ ├── SelfadjointRank2Update.h │ │ ├── TriangularMatrixMatrix.h │ │ ├── TriangularMatrixMatrix_BLAS.h │ │ ├── TriangularMatrixVector.h │ │ ├── TriangularMatrixVector_BLAS.h │ │ ├── TriangularSolverMatrix.h │ │ ├── TriangularSolverMatrix_BLAS.h │ │ └── TriangularSolverVector.h │ └── util │ │ ├── BlasUtil.h │ │ ├── Constants.h │ │ ├── DisableStupidWarnings.h │ │ ├── ForwardDeclarations.h │ │ ├── MKL_support.h │ │ ├── Macros.h │ │ ├── Memory.h │ │ ├── Meta.h │ │ ├── NonMPL2.h │ │ ├── ReenableStupidWarnings.h │ │ ├── StaticAssert.h │ │ └── XprHelper.h │ ├── Eigenvalues │ ├── ComplexEigenSolver.h │ ├── ComplexSchur.h │ ├── ComplexSchur_LAPACKE.h │ ├── EigenSolver.h │ ├── GeneralizedEigenSolver.h │ ├── GeneralizedSelfAdjointEigenSolver.h │ ├── HessenbergDecomposition.h │ ├── MatrixBaseEigenvalues.h │ ├── RealQZ.h │ ├── RealSchur.h │ ├── RealSchur_LAPACKE.h │ ├── SelfAdjointEigenSolver.h │ ├── SelfAdjointEigenSolver_LAPACKE.h │ └── Tridiagonalization.h │ ├── Geometry │ ├── AlignedBox.h │ ├── AngleAxis.h │ ├── EulerAngles.h │ ├── Homogeneous.h │ ├── Hyperplane.h │ ├── OrthoMethods.h │ ├── ParametrizedLine.h │ ├── Quaternion.h │ ├── Rotation2D.h │ ├── RotationBase.h │ ├── Scaling.h │ ├── Transform.h │ ├── Translation.h │ ├── Umeyama.h │ └── arch │ │ └── Geometry_SSE.h │ ├── Householder │ ├── BlockHouseholder.h │ ├── Householder.h │ └── HouseholderSequence.h │ ├── IterativeLinearSolvers │ ├── BasicPreconditioners.h │ ├── BiCGSTAB.h │ ├── ConjugateGradient.h │ ├── IncompleteCholesky.h │ ├── IncompleteLUT.h │ ├── IterativeSolverBase.h │ ├── LeastSquareConjugateGradient.h │ └── SolveWithGuess.h │ ├── Jacobi │ └── Jacobi.h │ ├── LU │ ├── Determinant.h │ ├── FullPivLU.h │ ├── InverseImpl.h │ ├── PartialPivLU.h │ ├── PartialPivLU_LAPACKE.h │ └── arch │ │ └── Inverse_SSE.h │ ├── MetisSupport │ └── MetisSupport.h │ ├── OrderingMethods │ ├── Amd.h │ ├── Eigen_Colamd.h │ └── Ordering.h │ ├── PaStiXSupport │ └── PaStiXSupport.h │ ├── PardisoSupport │ └── PardisoSupport.h │ ├── QR │ ├── ColPivHouseholderQR.h │ ├── ColPivHouseholderQR_LAPACKE.h │ ├── CompleteOrthogonalDecomposition.h │ ├── FullPivHouseholderQR.h │ ├── HouseholderQR.h │ └── HouseholderQR_LAPACKE.h │ ├── SPQRSupport │ └── SuiteSparseQRSupport.h │ ├── SVD │ ├── BDCSVD.h │ ├── JacobiSVD.h │ ├── JacobiSVD_LAPACKE.h │ ├── SVDBase.h │ └── UpperBidiagonalization.h │ ├── SparseCholesky │ ├── SimplicialCholesky.h │ └── SimplicialCholesky_impl.h │ ├── SparseCore │ ├── AmbiVector.h │ ├── CompressedStorage.h │ ├── ConservativeSparseSparseProduct.h │ ├── MappedSparseMatrix.h │ ├── SparseAssign.h │ ├── SparseBlock.h │ ├── SparseColEtree.h │ ├── SparseCompressedBase.h │ ├── SparseCwiseBinaryOp.h │ ├── SparseCwiseUnaryOp.h │ ├── SparseDenseProduct.h │ ├── SparseDiagonalProduct.h │ ├── SparseDot.h │ ├── SparseFuzzy.h │ ├── SparseMap.h │ ├── SparseMatrix.h │ ├── SparseMatrixBase.h │ ├── SparsePermutation.h │ ├── SparseProduct.h │ ├── SparseRedux.h │ ├── SparseRef.h │ ├── SparseSelfAdjointView.h │ ├── SparseSolverBase.h │ ├── SparseSparseProductWithPruning.h │ ├── SparseTranspose.h │ ├── SparseTriangularView.h │ ├── SparseUtil.h │ ├── SparseVector.h │ ├── SparseView.h │ └── TriangularSolver.h │ ├── SparseLU │ ├── SparseLU.h │ ├── SparseLUImpl.h │ ├── SparseLU_Memory.h │ ├── SparseLU_Structs.h │ ├── SparseLU_SupernodalMatrix.h │ ├── SparseLU_Utils.h │ ├── SparseLU_column_bmod.h │ ├── SparseLU_column_dfs.h │ ├── SparseLU_copy_to_ucol.h │ ├── SparseLU_gemm_kernel.h │ ├── SparseLU_heap_relax_snode.h │ ├── SparseLU_kernel_bmod.h │ ├── SparseLU_panel_bmod.h │ ├── SparseLU_panel_dfs.h │ ├── SparseLU_pivotL.h │ ├── SparseLU_pruneL.h │ └── SparseLU_relax_snode.h │ ├── SparseQR │ └── SparseQR.h │ ├── StlSupport │ ├── StdDeque.h │ ├── StdList.h │ ├── StdVector.h │ └── details.h │ ├── SuperLUSupport │ └── SuperLUSupport.h │ ├── UmfPackSupport │ └── UmfPackSupport.h │ ├── misc │ ├── Image.h │ ├── Kernel.h │ ├── RealSvd2x2.h │ ├── blas.h │ ├── lapack.h │ ├── lapacke.h │ └── lapacke_mangling.h │ └── plugins │ ├── ArrayCwiseBinaryOps.h │ ├── ArrayCwiseUnaryOps.h │ ├── BlockMethods.h │ ├── CommonCwiseBinaryOps.h │ ├── CommonCwiseUnaryOps.h │ ├── MatrixCwiseBinaryOps.h │ └── MatrixCwiseUnaryOps.h └── tools ├── CMakeLists.txt ├── onnx2bnn ├── CMakeLists.txt ├── NodeAttrHelper.cpp ├── NodeAttrHelper.h ├── OnnxConverter.cpp ├── OnnxConverter.h ├── onnx2bnn.cpp ├── optional.h └── pywrapper.cpp └── quick_benchmark.py /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | IndentWidth: 4 3 | -------------------------------------------------------------------------------- /.daq_pm/configs/all_v7: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_v7 5 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-21 -DANDROID_ABI="armeabi-v7a with NEON" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -GNinja 6 | -------------------------------------------------------------------------------- /.daq_pm/configs/arm64-v8a: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build 5 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release 6 | binary ~/adb_push_and_run.sh tests/fconv_test 7 | -------------------------------------------------------------------------------- /.daq_pm/configs/arm64-v8a-wo-test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_arm64_no_test 5 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release-DBUILD_TEST=OFF -DBUILD_BENCHMARK=OFF 6 | binary ~/adb_push_and_run.sh binaries/run /sdcard/model.daq 7 | -------------------------------------------------------------------------------- /.daq_pm/configs/bconv_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build 5 | target bconv_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON 7 | binary ~/adb_push_and_run.sh tests/bconv_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/bconv_test_x86: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_test_x86 5 | target bconv_test 6 | cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBNN_BUILD_MAIN_LIB=ON -GNinja 7 | binary tests/bconv_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/benchmark: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_release 5 | build_type Release 6 | target dabnn_benchmark 7 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release 8 | binary ~/adb_push_and_run.sh benchmark/dabnn_benchmark 9 | -------------------------------------------------------------------------------- /.daq_pm/configs/benchmark_macro: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_benchmark_macro 5 | build_type Release 6 | target run 7 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -DBNN_NET_BENCHMARK=ON 8 | binary ~/adb_push_and_run.sh binaries/run /data/local/tmp/model_imagenet.daq 188 && adb pull /data/local/tmp/mat.txt 9 | -------------------------------------------------------------------------------- /.daq_pm/configs/benchmark_v7: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_release_v7 5 | build_type Release 6 | target dabnn_benchmark 7 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-21 -DANDROID_ABI="armeabi-v7a with NEON" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release 8 | binary ~/adb_push_and_run.sh benchmark/dabnn_benchmark 9 | -------------------------------------------------------------------------------- /.daq_pm/configs/bgemm_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_test 5 | target bgemm_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -GNinja 7 | binary ~/adb_push_and_run.sh tests/bgemm_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/bgemm_test_v7: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_test_v7 5 | target bgemm_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-25 -DANDROID_ABI="armeabi-v7a with NEON" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -GNinja 7 | binary ~/adb_push_and_run.sh tests/bgemm_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/bgemm_test_x86: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_test_x86 5 | target bgemm_test 6 | cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBNN_BUILD_MAIN_LIB=ON -GNinja 7 | binary tests/bgemm_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/bitpack_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_test 5 | target bitpack_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -GNinja 7 | binary ~/adb_push_and_run.sh tests/bitpack_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/converter: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_x86 5 | cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON 6 | binary tools/onnx2bnn/onnx2bnn ~/repos/bnn.pytorch/model_imagenet_24g2.onnx br24g2.daq && adb push br24g2.daq /data/local/tmp 7 | -------------------------------------------------------------------------------- /.daq_pm/configs/converter_python: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_x86_python 5 | cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBNN_BUILD_PYTHON=ON 6 | -------------------------------------------------------------------------------- /.daq_pm/configs/fconv_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build 5 | target fconv_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release 7 | binary ~/adb_push_and_run.sh tests/fconv_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/gemm_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build 5 | target gemm_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release 7 | binary ~/adb_push_and_run.sh tests/gemm_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/im2col_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build 5 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release 6 | binary ~/adb_push_and_run.sh tests/im2col_test 7 | -------------------------------------------------------------------------------- /.daq_pm/configs/jni: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_jni 5 | cmake_options -DCMAKE_SYSTEM_NAME=Android -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DCMAKE_ANDROID_STL_TYPE=c++_static -DANDROID_PLATFORM=android-28 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBNN_BUILD_TEST=OFF -DBNN_BUILD_BENCHMARK=OFF -DCMAKE_BUILD_TYPE=Release -DBNN_BUILD_JNI=ON 6 | -------------------------------------------------------------------------------- /.daq_pm/configs/net_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_net_test 5 | target net_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-21 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -GNinja 7 | binary ~/adb_push_and_run.sh tests/net_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/net_test_debug: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_net_test_debug 5 | target net_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug 7 | binary ~/adb_push_and_run.sh tests/net_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/net_test_v7: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_test_v7 5 | target net_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-21 -DANDROID_ABI="armeabi-v7a with NEON" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -GNinja 7 | binary ~/adb_push_and_run.sh tests/net_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/net_test_x86: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_net_test_x86 5 | target net_test 6 | cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -DBNN_BUILD_MAIN_LIB=ON -GNinja 7 | binary tests/net_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/pad_test: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build 5 | target pad_test 6 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release 7 | binary ~/adb_push_and_run.sh tests/pad_test 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/run_net: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_arm64_no_test 5 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -DBNN_BUILD_TEST=OFF -DBNN_BUILD_BENCHMARK=OFF 6 | binary ~/adb_push_and_run.sh binaries/run /data/local/tmp/br24g2.daq 242 && adb pull /data/local/tmp/mat.txt 7 | -------------------------------------------------------------------------------- /.daq_pm/configs/run_net_debug: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_arm64_no_test_debug 5 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug -DBNN_BUILD_TEST=OFF -DBNN_BUILD_BENCHMARK=OFF -DBNN_CHECK_CONSISTENCY=ON 6 | # binary ~/adb_push_and_run.sh binaries/run /data/local/tmp/model_imagenet.dab 144 145 7 | binary ~/adb_push_and_run.sh binaries/run /data/local/tmp/simple5.dab output --v=6 8 | -------------------------------------------------------------------------------- /.daq_pm/configs/run_net_new_bitpack: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_arm64_no_test 5 | cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -DBNN_BUILD_TEST=OFF -DBNN_BUILD_BENCHMARK=OFF 6 | binary ~/adb_push_and_run.sh binaries/run /data/local/tmp/br24g2.daq 242 && adb pull /data/local/tmp/mat.txt 7 | -------------------------------------------------------------------------------- /.daq_pm/configs/run_net_x86: -------------------------------------------------------------------------------- 1 | # It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim) 2 | name binary-nn 3 | type cpp 4 | build_dir build_main_x86 5 | cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug -DBNN_BUILD_TEST=OFF -DBNN_BUILD_BENCHMARK=OFF -DBNN_BUILD_MAIN_LIB=ON 6 | target run 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | cmake-build-*/ 2 | .idea/ 3 | *.swp 4 | *.swo 5 | build/ 6 | .ccls-cache/ 7 | compile_commands.json 8 | build-*/ 9 | build_*/ 10 | .daq_pm/status/ 11 | .setuptools-cmake-build/ 12 | .eggs/ 13 | tools/onnx2bnn/python/dist/ 14 | tools/onnx2bnn/python/onnx2bnn.egg-info/ 15 | .clangd/ 16 | .build*/ 17 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/benchmark"] 2 | path = third_party/benchmark 3 | url = https://github.com/google/benchmark 4 | [submodule "third_party/onnx"] 5 | path = third_party/onnx 6 | url = https://github.com/daquexian/onnx 7 | [submodule "third_party/glog"] 8 | path = third_party/glog 9 | url = https://github.com/google/glog 10 | [submodule "third_party/googletest"] 11 | path = third_party/googletest 12 | url = https://github.com/google/googletest 13 | [submodule "third_party/protobuf"] 14 | path = third_party/protobuf 15 | url = https://github.com/protocolbuffers/protobuf 16 | [submodule "third_party/flatbuffers"] 17 | path = third_party/flatbuffers 18 | url = https://github.com/google/flatbuffers 19 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | cmake_minimum_required(VERSION 3.9) 4 | project(dabnn CXX) 5 | 6 | message(STATUS "CMake Version: ${CMAKE_VERSION}") 7 | 8 | if (NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "") 9 | message(STATUS "No build type, set to Release") 10 | set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE) 11 | endif() 12 | message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") 13 | 14 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 15 | 16 | option(BNN_BUILD_JNI "Build jni" OFF) 17 | option(BNN_BUILD_TEST "Build test" ON) 18 | option(BNN_BUILD_BENCHMARK "Build benchmark" ON) 19 | option(BNN_NET_BENCHMARK "define BNN_BENCHMARK" OFF) 20 | option(BNN_CHECK_CONSISTENCY "Make some layers non inplace to check results" OFF) 21 | option(BNN_SYSTEM_PROTOBUF "Use system protobuf to build onnx2bnn" ON) 22 | option(BNN_BUILD_PYTHON "Build onnx2bnn python interface" OFF) 23 | option(BNN_USE_MSVC_STATIC_RUNTIME "Link onnx2bnn to msvc static runtime" ON) 24 | 25 | message(STATUS "Target architecture: ${CMAKE_SYSTEM_PROCESSOR}") 26 | if (NOT DEFINED BNN_BUILD_MAIN_LIB) 27 | if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR 28 | CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a") 29 | set(BNN_BUILD_MAIN_LIB ON) 30 | else() 31 | set(BNN_BUILD_MAIN_LIB OFF) 32 | endif() 33 | endif() 34 | message(STATUS "BNN_BUILD_MAIN_LIB: ${BNN_BUILD_MAIN_LIB}") 35 | 36 | include(cmake/utils.cmake) 37 | bnn_add_msvc_runtime_flag() 38 | 39 | include(cmake/common.cmake) 40 | include(cmake/system.cmake) 41 | 42 | include(cmake/glog.cmake) 43 | configure_glog() 44 | 45 | include(cmake/flatbuffers.cmake) 46 | configure_flatbuffers() 47 | 48 | add_compile_options("-DEIGEN_MPL2_ONLY") 49 | if (${BNN_NET_BENCHMARK}) 50 | add_compile_options("-DBNN_BENCHMARK") 51 | endif() 52 | 53 | if (BNN_BUILD_MAIN_LIB) 54 | set(CMAKE_CXX_STANDARD 17) 55 | 56 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 57 | 58 | if (${BNN_BUILD_TEST}) 59 | include(cmake/gtest.cmake) 60 | configure_gtest() 61 | endif() 62 | 63 | add_subdirectory(dabnn) 64 | 65 | add_subdirectory(binaries) 66 | if (BNN_BUILD_TEST) 67 | add_subdirectory(tests) 68 | endif() 69 | if (BNN_BUILD_BENCHMARK) 70 | include(cmake/benchmark.cmake) 71 | configure_benchmark() 72 | add_subdirectory(benchmark) 73 | endif() 74 | else() 75 | set(CMAKE_CXX_STANDARD 11) 76 | 77 | if (${BNN_BUILD_PYTHON}) 78 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) 79 | add_subdirectory(third_party/pybind11) 80 | endif() 81 | 82 | include(cmake/protobuf.cmake) 83 | if (${BNN_SYSTEM_PROTOBUF}) 84 | find_package(Protobuf) 85 | endif() 86 | if (NOT Protobuf_FOUND) 87 | message(STATUS "BNN_SYSTEM_PROTOBUF is ON but protobuf is not found, using the packed protobuf..") 88 | set(BNN_SYSTEM_PROTOBUF OFF CACHE BOOL "Use system protobuf when building onnx2daq" FORCE) 89 | endif() 90 | 91 | include(cmake/onnx.cmake) 92 | configure_onnx() 93 | 94 | add_subdirectory(tools) 95 | endif() 96 | -------------------------------------------------------------------------------- /benchmark/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | add_executable(dabnn_benchmark benchmark.cpp) 4 | target_link_libraries(dabnn_benchmark 5 | dabnn 6 | benchmark_main) 7 | 8 | add_executable(benchmark_single_model benchmark_single_model.cpp) 9 | target_link_libraries(benchmark_single_model 10 | dabnn 11 | benchmark_main) 12 | -------------------------------------------------------------------------------- /benchmark/benchmark_single_model.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | static void BM_single_model(benchmark::State &state, std::string model_path) { 8 | float input[999999]; 9 | 10 | auto net = bnn::Net::create(); 11 | net->optimize = true; 12 | net->run_fconv = true; 13 | net->strict = true; 14 | net->read(model_path); 15 | 16 | for (auto _ : state) { 17 | net->run(input); 18 | } 19 | } 20 | 21 | int main(int argc, char **argv) { 22 | benchmark::RegisterBenchmark("single_model", BM_single_model, argv[1]); 23 | benchmark::Initialize(&argc, argv); 24 | benchmark::RunSpecifiedBenchmarks(); 25 | } 26 | -------------------------------------------------------------------------------- /binaries/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(run run.cpp) 2 | target_link_libraries(run 3 | dabnn) 4 | -------------------------------------------------------------------------------- /binaries/run.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char **argv) { 13 | argh::parser cmdl(argc, argv); 14 | google::InitGoogleLogging(argv[0]); 15 | cmdl("v", 1) >> FLAGS_v; 16 | FLAGS_alsologtostderr = true; 17 | // FLAGS_logbuflevel = -1; 18 | 19 | float input[3 * 224 * 224]; 20 | FORZ(i, 3 * 224 * 224) { input[i] = 1; } 21 | 22 | auto net1 = bnn::Net::create(); 23 | net1->optimize = true; 24 | net1->run_fconv = true; 25 | net1->strict = true; 26 | net1->read(argv[1]); 27 | FORZ(i, 0) { net1->run(input); } 28 | const int N = 1; 29 | using Clock = std::chrono::steady_clock; 30 | const auto t1 = Clock::now(); 31 | FORZ(i, N) { 32 | LOG(INFO) << "------"; 33 | net1->run(input); 34 | } 35 | const auto t2 = Clock::now(); 36 | 37 | for (int i = 2; i < cmdl.size(); i++) { 38 | css blob_name = argv[i]; 39 | LOG(INFO) << "Fetching blob: " << blob_name; 40 | const auto &blob1 = net1->get_blob(blob_name); 41 | LOG(INFO) << static_cast(blob1->data)[0]; 42 | if (blob1->data_type == bnn::DataType::Float) { 43 | blob1->dump("/data/local/tmp/mat_" + blob_name + ".txt"); 44 | } 45 | FORZ(j, std::min(static_cast(blob1->total()), 10)) { 46 | if (blob1->data_type == bnn::DataType::Float) { 47 | LOG(INFO) << blob_name << ": " << static_cast(blob1->data)[j]; 48 | } else { 49 | LOG(INFO) << blob_name << ": " << binrep(static_cast(blob1->data) + j, 64, true); 50 | } 51 | } 52 | } 53 | #ifdef BNN_BENCHMARK 54 | net1->print_time(); 55 | #endif 56 | } 57 | -------------------------------------------------------------------------------- /ci/adb_push_and_run.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | # echo "${@:2}" 4 | adb push $1 /data/local/tmp/`basename $1` && adb shell "data/local/tmp/`basename $1` ${@:2}" 5 | -------------------------------------------------------------------------------- /ci/android_aar/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | build/ 8 | 9 | openCVLibrary320/build/ 10 | /captures 11 | .externalNativeBuild 12 | 13 | .idea/ 14 | 15 | *.swp 16 | *.swo 17 | 18 | libdabnn-jni.so 19 | -------------------------------------------------------------------------------- /ci/android_aar/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.4.0' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /ci/android_aar/dabnn/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /ci/android_aar/dabnn/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release' // must be applied after your artifact generating plugin (eg. java / com.android.library) 3 | 4 | static def generateVersionCode() { 5 | def result = "git rev-list HEAD --count".execute().text.trim() //unix 6 | if(result.empty) result = "PowerShell -Command git rev-list HEAD --count".execute().text.trim() //windows 7 | if(result.empty) throw new RuntimeException("Could not generate versioncode on this platform? Cmd output: ${result.text}") 8 | return result.toInteger() 9 | } 10 | 11 | buildscript { 12 | repositories { 13 | jcenter() 14 | } 15 | dependencies { 16 | classpath 'com.novoda:bintray-release:0.9.1' 17 | } 18 | } 19 | 20 | android { 21 | compileSdkVersion 28 22 | 23 | 24 | defaultConfig { 25 | minSdkVersion 21 26 | targetSdkVersion 28 27 | versionCode generateVersionCode() 28 | versionName "0.1" 29 | } 30 | 31 | buildTypes { 32 | release { 33 | minifyEnabled false 34 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 35 | } 36 | } 37 | 38 | } 39 | 40 | publish { 41 | userOrg = 'daquexian566' 42 | groupId = 'me.daquexian' 43 | artifactId = 'dabnn' 44 | publishVersion = '0.1.0' 45 | desc = 'dabnn is an accelerated binary neural networks inference framework for mobile platform' 46 | website = 'https://github.com/JDAI-CV/dabnn' 47 | } 48 | 49 | -------------------------------------------------------------------------------- /ci/android_aar/dabnn/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /ci/android_aar/dabnn/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /ci/android_aar/dabnn/src/main/java/me/daquexian/dabnn/Net.java: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | package me.daquexian.dabnn; 4 | 5 | import android.content.res.AssetManager; 6 | 7 | public class Net { 8 | static { 9 | System.loadLibrary("dabnn-jni"); 10 | } 11 | public Net() { 12 | initHandle(); 13 | } 14 | @Override 15 | protected void finalize() throws Throwable { 16 | dispose(); 17 | super.finalize(); 18 | } 19 | private long nativeHandle; 20 | private native void initHandle(); 21 | public native Net readFile(String filename); 22 | public native Net readAsset(AssetManager assetManager, String filename); 23 | public native float[] getBlob(String blobName); 24 | public native void predict(float[] input); 25 | public native void dispose(); 26 | } 27 | -------------------------------------------------------------------------------- /ci/android_aar/dabnn/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | dabnn 3 | 4 | -------------------------------------------------------------------------------- /ci/android_aar/gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Thu May 10 20:44:55 HKT 2018 16 | org.gradle.jvmargs=-Xmx1536m 17 | -------------------------------------------------------------------------------- /ci/android_aar/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JDAI-CV/dabnn/d93aa950788319d92ade545eb7fe7ac6f1589fd1/ci/android_aar/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /ci/android_aar/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Apr 25 14:12:44 HKT 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip 7 | -------------------------------------------------------------------------------- /ci/android_aar/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /ci/android_aar/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':openCVLibrary340', ':dabnn' 2 | -------------------------------------------------------------------------------- /ci/appimage/onnx2bnn.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Name=onnx2bnn 3 | Exec=onnx2bnn 4 | Icon=onnx2bnn 5 | Type=Application 6 | Terminal=true 7 | Categories=Development; 8 | -------------------------------------------------------------------------------- /ci/appimage/onnx2bnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JDAI-CV/dabnn/d93aa950788319d92ade545eb7fe7ac6f1589fd1/ci/appimage/onnx2bnn.png -------------------------------------------------------------------------------- /ci/build_aar.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | set -e 4 | 5 | nproc=$(ci/get_cores.sh) 6 | if [[ "$OSTYPE" == "darwin"* ]]; then 7 | echo "The system is Mac OS X, alias sed to gsed" 8 | export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH" 9 | echo "Output of sed -v:" 10 | sed --version 11 | fi 12 | 13 | MY_ANDROID_HOME="${ANDROID_HOME:-$HOME/Android/Sdk}" 14 | MY_ANDROID_NDK_HOME="${ANDROID_NDK_HOME:-$MY_ANDROID_HOME/ndk-bundle}" 15 | JNI_BUILD_DIR=build_jni_tmp 16 | rm -rf ${JNI_BUILD_DIR} && mkdir ${JNI_BUILD_DIR} && pushd ${JNI_BUILD_DIR} 17 | cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_TOOLCHAIN_FILE=${MY_ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake -DANDROID_CPP_FEATURES=exceptions -DANDROID_PLATFORM=android-21 -DANDROID_ABI=arm64-v8a -DBNN_BUILD_JNI=ON -DBNN_BUILD_TEST=OFF -DBNN_BUILD_BENCHMARK=OFF .. 18 | cmake --build . -- -j$nproc 19 | popd 20 | mkdir -p ci/android_aar/dabnn/src/main/jniLibs/arm64-v8a 21 | cp ${JNI_BUILD_DIR}/dabnn/jni/libdabnn-jni.so ci/android_aar/dabnn/src/main/jniLibs/arm64-v8a/ 22 | 23 | # Increase version code and update version name 24 | 25 | echo "Build source branch: $BUILD_SOURCEBRANCH" 26 | 27 | if (($# == 0)); then 28 | if [[ $BUILD_SOURCEBRANCH == refs/tags/v* ]]; then 29 | ver=`echo $BUILD_SOURCEBRANCH | cut -c 12-` 30 | else 31 | echo "HEAD is not tagged as a version, skip deploy aar" 32 | exit 0 33 | fi 34 | elif (( $# == 1 )); then 35 | ver=$1 36 | fi 37 | echo "ver=$ver" 38 | 39 | sed -i -E "s/versionName .+/versionName \"v$ver\"/" ci/android_aar/dabnn/build.gradle 40 | sed -i -E "s/publishVersion = .+/publishVersion = \'$ver\'/" ci/android_aar/dabnn/build.gradle 41 | 42 | cat ci/android_aar/dabnn/build.gradle 43 | 44 | pushd ci/android_aar 45 | ANDROID_HOME=$MY_ANDROID_HOME ./gradlew clean build 46 | 47 | # Publishing is only for myself 48 | if [[ -z $BINTRAY_KEY ]]; then 49 | echo "BINTRAY_KEY is not set, skip bintray upload" 50 | else 51 | echo "Publishing.." 52 | ANDROID_HOME=$MY_ANDROID_HOME ./gradlew bintrayUpload -PbintrayUser=daquexian566 -PbintrayKey=$BINTRAY_KEY -PdryRun=false 53 | fi 54 | popd 55 | -------------------------------------------------------------------------------- /ci/build_appimage.sh: -------------------------------------------------------------------------------- 1 | wget https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage 2 | wget https://github.com/linuxdeploy/linuxdeploy-plugin-appimage/releases/download/continuous/linuxdeploy-plugin-appimage-x86_64.AppImage 3 | 4 | chmod +x linuxdeploy-*.AppImage 5 | mkdir -p ci/appimage/appdir/usr/bin 6 | cp build_onnx2bnn/tools/onnx2bnn/onnx2bnn ci/appimage/appdir/usr/bin/ 7 | ./linuxdeploy-x86_64.AppImage --appdir ci/appimage/appdir -d ci/appimage/onnx2bnn.desktop -i ci/appimage/onnx2bnn.png --output appimage 8 | mv `ls onnx2bnn-*.AppImage` onnx2bnn.AppImage 9 | -------------------------------------------------------------------------------- /ci/build_dabnn.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | set -e 3 | 4 | echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'ndk-bundle' 5 | nproc=$(ci/get_cores.sh) 6 | 7 | mkdir build_dabnn && cd build_dabnn 8 | cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_HOME/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release .. 9 | cmake --build . -- -j$nproc 10 | cd - 11 | -------------------------------------------------------------------------------- /ci/build_dabnn_v7.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | set -e 3 | 4 | echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'ndk-bundle' 5 | nproc=$(ci/get_cores.sh) 6 | 7 | mkdir build_dabnn && cd build_dabnn 8 | cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_HOME/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-21 -DANDROID_ABI="armeabi-v7a with NEON" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release .. 9 | cmake --build . -- -j$nproc 10 | cd - 11 | 12 | -------------------------------------------------------------------------------- /ci/build_onnx2bnn.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | set -e 3 | 4 | mkdir build_onnx2bnn && cd build_onnx2bnn 5 | cmake .. 6 | cmake --build . 7 | cd - 8 | -------------------------------------------------------------------------------- /ci/dabnn_build_and_test.yml: -------------------------------------------------------------------------------- 1 | trigger: 2 | branches: 3 | include: 4 | - master 5 | tags: 6 | include: 7 | - v* 8 | paths: 9 | include: 10 | - '*' 11 | exclude: 12 | - README.md 13 | - docs/* 14 | pr: 15 | branches: 16 | include: 17 | - '*' 18 | paths: 19 | include: 20 | - '*' 21 | exclude: 22 | - README.md 23 | - docs/* 24 | 25 | jobs: 26 | - job: aarch64 27 | pool: 28 | vmImage: 'macOS-10.14' 29 | steps: 30 | - checkout: self 31 | submodules: true 32 | - bash: brew install watch gnu-sed 33 | displayName: Install watch and gnu-sed 34 | - bash: ci/build_dabnn.sh 35 | displayName: Build dabnn 36 | - bash: ci/start_android_emulator.sh 37 | displayName: Start Android Emulator 38 | - template: template_dabnn_run_test.yml 39 | - bash: ci/build_aar.sh 40 | env: 41 | BINTRAY_KEY: $(bintrayKey) 42 | displayName: Build and Publish AAR package 43 | - job: armv7a_with_NEON 44 | pool: 45 | vmImage: 'macOS-10.14' 46 | steps: 47 | - checkout: self 48 | submodules: true 49 | - bash: brew install watch gnu-sed 50 | displayName: Install watch and gnu-sed 51 | - bash: ci/build_dabnn_v7.sh 52 | displayName: Build dabnn 53 | - bash: ci/start_android_emulator_v7.sh 54 | displayName: Start Android Emulator 55 | - template: template_dabnn_run_test.yml 56 | -------------------------------------------------------------------------------- /ci/download_and_convert_models.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | wget "https://drive.google.com/uc?export=download&id=1Xp3HB51H6Nhl6e555ieJubVutQake5sR" -O model_imagenet.onnx 4 | ./build_onnx2bnn/tools/onnx2bnn/onnx2bnn model_imagenet.onnx model_imagenet.dab --aggressive --verbose 5 | adb push model_imagenet.dab /data/local/tmp 6 | wget "https://drive.google.com/uc?export=download&id=1zu48CFptAGZ91IDCBPJSPM0bxDuPm9HS" -O model_imagenet_stem.onnx 7 | ./build_onnx2bnn/tools/onnx2bnn/onnx2bnn model_imagenet_stem.onnx model_imagenet_stem.dab --aggressive --verbose 8 | adb push model_imagenet_stem.dab /data/local/tmp/ 9 | -------------------------------------------------------------------------------- /ci/get_cores.sh: -------------------------------------------------------------------------------- 1 | if [[ "$OSTYPE" == "drawin*" ]]; then 2 | echo $(sysctl -n hw.physicalcpu) 3 | else 4 | echo $(nproc) 5 | fi 6 | -------------------------------------------------------------------------------- /ci/onnx2bnn_build.yml: -------------------------------------------------------------------------------- 1 | trigger: 2 | branches: 3 | include: 4 | - master 5 | tags: 6 | include: 7 | - v* 8 | paths: 9 | include: 10 | - '*' 11 | exclude: 12 | - README.md 13 | - docs/* 14 | # not trigger onnx2bnn build when only dabnn is edited 15 | - dabnn/* 16 | pr: 17 | branches: 18 | include: 19 | - '*' 20 | paths: 21 | include: 22 | - '*' 23 | exclude: 24 | - README.md 25 | - docs/* 26 | # not trigger onnx2bnn build when only dabnn is edited 27 | - dabnn/* 28 | 29 | jobs: 30 | - job: LinuxAppImage 31 | pool: 32 | vmImage: 'ubuntu-16.04' 33 | steps: 34 | - checkout: self 35 | submodules: true 36 | - script: sudo apt install -y protobuf-compiler libprotobuf-dev 37 | displayName: Install protobuf 38 | - bash: ci/build_onnx2bnn.sh 39 | displayName: Build 40 | - bash: ci/build_appimage.sh 41 | displayName: Build AppImage 42 | - task: CopyFiles@2 43 | inputs: 44 | contents: 'onnx2bnn.AppImage' 45 | targetFolder: $(Build.ArtifactStagingDirectory) 46 | - template: template_onnx2bnn_publish_artifacts.yml 47 | - template: template_onnx2bnn_github_release.yml 48 | - job: Windows 49 | pool: 50 | vmImage: 'vs2017-win2016' 51 | steps: 52 | - checkout: self 53 | submodules: true 54 | - bash: ci/build_onnx2bnn.sh 55 | displayName: Build 56 | - bash: ls build_onnx2bnn 57 | - task: CopyFiles@2 58 | inputs: 59 | sourceFolder: 'build_onnx2bnn\tools\onnx2bnn\Debug\' 60 | contents: 'onnx2bnn.exe' 61 | targetFolder: $(Build.ArtifactStagingDirectory) 62 | - template: template_onnx2bnn_publish_artifacts.yml 63 | - template: template_onnx2bnn_github_release.yml 64 | - job: macOS 65 | pool: 66 | vmImage: 'macOS-10.14' 67 | steps: 68 | - checkout: self 69 | submodules: true 70 | - bash: ci/build_onnx2bnn.sh 71 | displayName: Build 72 | - script: 'cp build_onnx2bnn/tools/onnx2bnn/onnx2bnn build_onnx2bnn/tools/onnx2bnn/onnx2bnn-macos' 73 | displayName: 'Rename onnx2bnn' 74 | - task: CopyFiles@2 75 | inputs: 76 | sourceFolder: 'build_onnx2bnn/tools/onnx2bnn' 77 | contents: 'onnx2bnn-macos' 78 | targetFolder: $(Build.ArtifactStagingDirectory) 79 | - template: template_onnx2bnn_publish_artifacts.yml 80 | - template: template_onnx2bnn_github_release.yml 81 | -------------------------------------------------------------------------------- /ci/start_android_emulator.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | set -e 3 | 4 | export TERM=xterm 5 | 6 | echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-25;google_apis;arm64-v8a' 7 | 8 | echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n android_emulator -k 'system-images;android-25;google_apis;arm64-v8a' --force 9 | 10 | echo "Starting emulator" 11 | 12 | # Start emulator in background 13 | nohup $ANDROID_HOME/emulator/emulator -avd android_emulator -no-snapshot -no-audio & 14 | 15 | # start server in advance, so that the result of watch will only change when device gets online 16 | $ANDROID_HOME/platform-tools/adb start-server 17 | 18 | watch -g -n 1 '$ANDROID_HOME/platform-tools/adb devices | grep -c device$' 19 | 20 | echo "Emulator is online" 21 | 22 | $ANDROID_HOME/platform-tools/adb devices 23 | 24 | echo "Emulator started" 25 | -------------------------------------------------------------------------------- /ci/start_android_emulator_v7.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | set -e 3 | 4 | export TERM=xterm 5 | 6 | echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-25;google_apis;armeabi-v7a' 7 | 8 | echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n android_emulator -k 'system-images;android-25;google_apis;armeabi-v7a' --force 9 | 10 | echo "Starting emulator" 11 | 12 | # Start emulator in background 13 | nohup $ANDROID_HOME/emulator/emulator -avd android_emulator -no-snapshot -no-audio & 14 | 15 | # start server in advance, so that the result of watch will only change when device gets online 16 | $ANDROID_HOME/platform-tools/adb start-server 17 | 18 | watch -g -n 1 '$ANDROID_HOME/platform-tools/adb devices | grep -c device$' 19 | 20 | echo "Emulator is online" 21 | 22 | $ANDROID_HOME/platform-tools/adb devices 23 | 24 | echo "Emulator started" 25 | 26 | -------------------------------------------------------------------------------- /ci/template_dabnn_run_test.yml: -------------------------------------------------------------------------------- 1 | steps: 2 | - bash: ci/adb_push_and_run.sh build_dabnn/tests/bconv_test 3 | displayName: Binary Conv Test 4 | - bash: ci/adb_push_and_run.sh build_dabnn/tests/bgemm_test 5 | displayName: Binary Gemm Test 6 | - bash: ci/build_onnx2bnn.sh 7 | displayName: Build onnx2bnn 8 | - bash: ci/download_and_convert_models.sh 9 | displayName: Download and Convert Models 10 | - bash: ci/adb_push_and_run.sh build_dabnn/tests/net_test 11 | displayName: Model Test 12 | -------------------------------------------------------------------------------- /ci/template_onnx2bnn_github_release.yml: -------------------------------------------------------------------------------- 1 | steps: 2 | - task: GitHubRelease@0 3 | condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v')) 4 | inputs: 5 | gitHubConnection: 'dabnn release' 6 | repositoryName: '$(Build.Repository.Name)' 7 | action: 'edit' 8 | tag: '$(Build.SourceBranchName)' 9 | target: '$(Build.SourceVersion)' 10 | assets: '$(Build.ArtifactStagingDirectory)/*' 11 | assetUploadMode: 'replace' 12 | 13 | -------------------------------------------------------------------------------- /ci/template_onnx2bnn_publish_artifacts.yml: -------------------------------------------------------------------------------- 1 | steps: 2 | - task: PublishBuildArtifacts@1 3 | inputs: 4 | pathtoPublish: $(Build.ArtifactStagingDirectory) 5 | artifactName: onnx2bnn 6 | 7 | -------------------------------------------------------------------------------- /cmake/benchmark.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | macro(configure_benchmark) 4 | # option(BENCHMARK_DOWNLOAD_DEPENDENCIES "" ON) 5 | if (CMAKE_SYSTEM_NAME STREQUAL "Android") 6 | option(BENCHMARK_USE_LIBCXX "" ON) 7 | endif() 8 | option(BENCHMARK_ENABLE_TESTING "" OFF) 9 | set(HAVE_STD_REGEX 0) 10 | set(HAVE_POSIX_REGEX 0) 11 | set(HAVE_STEADY_CLOCK 0) 12 | add_subdirectory(third_party/benchmark) 13 | endmacro() 14 | -------------------------------------------------------------------------------- /cmake/common.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | function(treat_warnings_as_errors target) 4 | if(MSVC) 5 | target_compile_options(${target} PRIVATE "/W4" "/WX") 6 | else() 7 | target_compile_options(${target} PRIVATE "-Wall" "-Wextra" "-Werror") 8 | endif() 9 | endfunction() 10 | -------------------------------------------------------------------------------- /cmake/flatbuffers.cmake: -------------------------------------------------------------------------------- 1 | function(configure_flatbuffers) 2 | option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." OFF) 3 | option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" OFF) 4 | option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" 5 | OFF) 6 | option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library" 7 | ON) 8 | add_subdirectory(third_party/flatbuffers) 9 | endfunction() 10 | 11 | -------------------------------------------------------------------------------- /cmake/glog.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | macro(configure_glog) 4 | message(STATUS "Configureing glog...") 5 | option(BUILD_TESTING "" OFF) 6 | option(WITH_GFLAGS "" OFF) 7 | add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/glog) 8 | endmacro() 9 | -------------------------------------------------------------------------------- /cmake/gtest.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | macro(configure_gtest) 4 | message(STATUS "Configuring gtest...") 5 | option(BUILD_GMOCK "Builds the googlemock subproject" OFF) 6 | option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" OFF) 7 | add_subdirectory(third_party/googletest) 8 | endmacro() 9 | -------------------------------------------------------------------------------- /cmake/onnx.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | macro(configure_onnx) 4 | if (NOT ${BNN_SYSTEM_PROTOBUF}) 5 | include(${PROJECT_SOURCE_DIR}/cmake/protobuf.cmake) 6 | configure_protobuf() 7 | endif() 8 | 9 | message(STATUS "Configuring onnx...") 10 | set(BNN_ONNX_NAMESPACE onnx_bnn) 11 | if (MSVC) 12 | set(ONNX_CMAKELISTS ${PROJECT_SOURCE_DIR}/third_party/onnx/CMakeLists.txt) 13 | file(READ ${ONNX_CMAKELISTS} content) 14 | # Treating warnings as errors breaks ci, we have no other way to opt-out 15 | string( 16 | REPLACE 17 | "/WX" 18 | "" 19 | content 20 | "${content}" 21 | ) 22 | file(WRITE ${ONNX_CMAKELISTS} "${content}") 23 | endif() 24 | set(ONNX_USE_MSVC_STATIC_RUNTIME ${BNN_USE_MSVC_STATIC_RUNTIME}) 25 | set(ONNX_NAMESPACE ${BNN_ONNX_NAMESPACE} CACHE STRING "onnx namespace") 26 | add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/onnx) 27 | # Since https://github.com/onnx/onnx/pull/1318 is merged, we don't need to set it manually 28 | # target_compile_definitions(onnx 29 | # PUBLIC 30 | # -DONNX_NAMESPACE=${BNN_ONNX_NAMESPACE}) 31 | endmacro() 32 | -------------------------------------------------------------------------------- /cmake/protobuf.cmake: -------------------------------------------------------------------------------- 1 | macro(alias_protobuf) 2 | add_library(protobuf::libprotoc ALIAS libprotoc) 3 | add_library(protobuf::libprotobuf ALIAS libprotobuf) 4 | add_library(protobuf::libprotobuf-lite ALIAS libprotobuf-lite) 5 | endmacro() 6 | 7 | macro(configure_protobuf) 8 | message(STATUS "Configuring protobuf...") 9 | option(protobuf_BUILD_TESTS "" OFF) 10 | option(protobuf_BUILD_EXAMPLES "" OFF) 11 | option(protobuf_BUILD_SHARED_LIBS "" OFF) 12 | option(protobuf_BUILD_PROTOC_BINARIES "" ON) 13 | add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/protobuf/cmake) 14 | alias_protobuf() 15 | endmacro() 16 | 17 | -------------------------------------------------------------------------------- /cmake/system.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | if ((NOT CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME) OR 4 | (NOT CMAKE_SYSTEM_PROCESSOR STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR)) 5 | set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 6 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 7 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 8 | set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 9 | endif() 10 | -------------------------------------------------------------------------------- /cmake/utils.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | # Add MSVC RunTime Flag 4 | function(bnn_add_msvc_runtime_flag) 5 | if (MSVC) 6 | if(${BNN_USE_MSVC_STATIC_RUNTIME}) 7 | if(${CMAKE_BUILD_TYPE} MATCHES "Debug") 8 | add_compile_options(/MTd) 9 | else() 10 | add_compile_options(/MT) 11 | endif() 12 | else() 13 | if(${CMAKE_BUILD_TYPE} MATCHES "Debug") 14 | add_compile_options(/MDd) 15 | else() 16 | add_compile_options(/MD) 17 | endif() 18 | endif() 19 | endif() 20 | endfunction() 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /common/StrKeyMap.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by daquexian on 8/20/18. 3 | // 4 | 5 | #ifndef DNNLIBRARY_DNN_MAP_H 6 | #define DNNLIBRARY_DNN_MAP_H 7 | 8 | #include 9 | #include 10 | 11 | /** 12 | * std::map whose key is std::string, so that we can print the key when a 13 | * std::out_of_range is thrown 14 | * @tparam V the value typename 15 | */ 16 | template 17 | class StrKeyMap { 18 | private: 19 | using map_t = std::map; 20 | map_t map_; 21 | 22 | public: 23 | inline V &operator[](const std::string &key) { 24 | return map_[key]; 25 | } 26 | inline const V &at(const std::string &key) const { 27 | try { 28 | return map_.at(key); 29 | } catch (const std::out_of_range &e) { 30 | throw std::out_of_range("Key " + key + " not found."); 31 | } 32 | } 33 | // This is "const decltype(as_const(map_).begin()) begin() const" 34 | const typename map_t::const_iterator begin() const { 35 | return map_.begin(); 36 | } 37 | const typename map_t::const_iterator end() const { 38 | return map_.end(); 39 | } 40 | void clear() { 41 | map_.clear(); 42 | } 43 | const typename map_t::const_iterator find(const std::string &key) const { 44 | return map_.find(key); 45 | } 46 | size_t size() const { 47 | return map_.size(); 48 | } 49 | bool has(const std::string &key) const { 50 | return map_.find(key) != map_.end(); 51 | } 52 | 53 | void insert(const std::pair &p) { 54 | map_.insert(p); 55 | } 56 | }; 57 | 58 | #endif // DNNLIBRARY_DNN_MAP_H 59 | -------------------------------------------------------------------------------- /common/log_helper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef DNN_LOG_HELPER_H 4 | #define DNN_LOG_HELPER_H 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace bnn { 11 | template 12 | std::ostream& operator<<(std::ostream& output, std::vector const& values) { 13 | output << "["; 14 | for (size_t i = 0; i < values.size(); i++) { 15 | output << values[i]; 16 | if (i != values.size() - 1) { 17 | output << ", "; 18 | } 19 | } 20 | output << "]"; 21 | return output; 22 | } 23 | } // namespace bnn 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /common/macros.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_MACROS_H 4 | #define BNN_MACROS_H 5 | 6 | #define BNN_LATEST_MODEL_VERSION 2 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /dabnn/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | add_library(dabnn 4 | allocator.cpp 5 | allocator.h 6 | mat.h 7 | bconv.h 8 | bitpack.h 9 | net.cpp 10 | im2col.h 11 | fconv.h 12 | layers/FloatConv.cpp 13 | layers/FloatConv.h 14 | layers/BinConv.cpp 15 | layers/BinConv.h 16 | layers/MaxPool.cpp 17 | layers/MaxPool.h 18 | layers/Affine.cpp 19 | layers/Affine.h 20 | layers/AvePool.cpp 21 | layers/AvePool.h 22 | layers/Add.cpp 23 | layers/Add.h 24 | layers/Concat.cpp 25 | layers/Concat.h 26 | layers/Relu.cpp 27 | layers/Relu.h 28 | layers/Binarize.cpp 29 | layers/Binarize.h 30 | layers/Shuffle.cpp 31 | layers/Shuffle.h 32 | layers/Split.cpp 33 | layers/Split.h 34 | layers/PRelu.cpp 35 | layers/PRelu.h 36 | layer.h 37 | layer.cpp 38 | ${PROJECT_SOURCE_DIR}/common/Shaper.cpp 39 | ${PROJECT_SOURCE_DIR}/common/Shaper.h 40 | ) 41 | target_include_directories(dabnn 42 | PUBLIC 43 | ${CMAKE_CURRENT_BINARY_DIR} 44 | ${PROJECT_SOURCE_DIR} 45 | ) 46 | target_include_directories(dabnn 47 | SYSTEM 48 | PUBLIC 49 | ${PROJECT_SOURCE_DIR}/third_party/eigen 50 | ) 51 | target_link_libraries(dabnn 52 | glog::glog 53 | flatbuffers 54 | ) 55 | 56 | if (${BNN_BUILD_JNI}) 57 | add_subdirectory(jni) 58 | endif() 59 | 60 | if (${BNN_CHECK_CONSISTENCY}) 61 | target_compile_definitions( 62 | dabnn 63 | PRIVATE 64 | BNN_CHECK_CONSISTENCY 65 | ) 66 | endif() 67 | 68 | treat_warnings_as_errors(dabnn) 69 | -------------------------------------------------------------------------------- /dabnn/fused_binarize_im2col.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | namespace bnn { 6 | inline void fused_binarize_im2col(const Mat &im, const int kernel_h, 7 | const int kernel_w, const int pad_h, 8 | const int pad_w, const int stride_h, 9 | const int stride_w, const int dilation_h, 10 | const int dilation_w, Mat &col) { 11 | BNN_ASSERT(im.data_type == DataType::Float, "Input of fused_binarize_im2col should be float"); 12 | BNN_ASSERT(col.data_type == DataType::Bit, "Output of fused_binarize_im2col should be bit"); 13 | 14 | BNN_ASSERT(kernel_h * kernel_w * im.c < 60000, 15 | "kernel_h * kernel_w * im.c must be smaller than 60000"); 16 | 17 | const int output_h = 18 | (im.h + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1; 19 | const int output_w = 20 | (im.w + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1; 21 | 22 | // TODO: More elegant way 23 | static char buf[2400000]; 24 | 25 | 26 | char *data_col = static_cast(col); 27 | int input_y = 0; 28 | FORZ(output_y, output_h) { 29 | int input_x = 0; 30 | FORZ(output_x, output_w) { 31 | char *buf_ptr = buf; 32 | FORZ(kh, kernel_h) { 33 | int y = input_y - pad_h + kh * dilation_h; 34 | const char *data_im = static_cast(im.data) + 35 | y * im.w * im.c * im.elemsize; 36 | FORZ(kw, kernel_w) { 37 | int x = input_x - pad_w + kw * dilation_w; 38 | if (y < 0 || y >= im.h || x < 0 || x >= im.w) { 39 | memset(buf_ptr, 0, im.c * im.elemsize); 40 | } else { 41 | memcpy(buf_ptr, data_im + x * im.c * im.elemsize, 42 | im.c * im.elemsize); 43 | } 44 | buf_ptr += im.c * im.elemsize; 45 | } 46 | } 47 | 48 | BNN_ASSERT(im.elemsize == 4, ""); 49 | // len: the number of elements in one column 50 | const size_t len = (buf_ptr - buf) / im.elemsize; 51 | const size_t len_aligned_128 = (len + 127) / 128 * 128; 52 | // pad the buffer so that its length aligns to 128 53 | memset(buf_ptr, 0, (len_aligned_128 - len) * im.elemsize); 54 | 55 | auto *fbuf = reinterpret_cast(buf); 56 | pack_64(fbuf, data_col, len_aligned_128); 57 | 58 | // `len_aligned_128` is the number of appended __bits__ in 59 | // mat `col`, so divide here 60 | const auto tmp = len_aligned_128 / 8; 61 | 62 | data_col += tmp; 63 | 64 | input_x += stride_w; 65 | } 66 | input_y += stride_h; 67 | } 68 | } 69 | } // namespace bnn 70 | -------------------------------------------------------------------------------- /dabnn/im2col.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_IM2COL_HPP 4 | #define BNN_IM2COL_HPP 5 | 6 | #include 7 | 8 | #include 9 | #include "mat.h" 10 | 11 | namespace bnn { 12 | 13 | // Modified from caffe 14 | inline void im2col(const Mat &im, const int kernel_h, const int kernel_w, 15 | const int pad_h, const int pad_w, const int stride_h, 16 | const int stride_w, const int dilation_h, 17 | const int dilation_w, Mat &col) { 18 | const int output_h = 19 | (im.h + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1; 20 | const int output_w = 21 | (im.w + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1; 22 | 23 | char *data_col = static_cast(col); 24 | int input_y = 0; 25 | FORZ(output_y, output_h) { 26 | int input_x = 0; 27 | FORZ(output_x, output_w) { 28 | FORZ(kh, kernel_h) { 29 | int y = input_y - pad_h + kh * dilation_h; 30 | const char *data_im = static_cast(im.data) + 31 | y * im.w * im.c * im.elemsize; 32 | FORZ(kw, kernel_w) { 33 | int x = input_x - pad_w + kw * dilation_w; 34 | if (y < 0 || y >= im.h || x < 0 || x >= im.w) { 35 | memset(data_col, 0, im.c * im.elemsize); 36 | } else { 37 | memcpy(data_col, data_im + x * im.c * im.elemsize, 38 | im.c * im.elemsize); 39 | } 40 | data_col += im.c * im.elemsize; 41 | } 42 | } 43 | input_x += stride_w; 44 | } 45 | input_y += stride_h; 46 | } 47 | } 48 | 49 | } // namespace bnn 50 | 51 | #endif /* BNN_IM2COL_HPP */ 52 | -------------------------------------------------------------------------------- /dabnn/jni/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | add_library(dabnn-jni 4 | SHARED 5 | jni_handle.h 6 | net_for_jni.h 7 | JavaWrapper.cpp 8 | net_for_jni.cpp 9 | ) 10 | 11 | find_library( 12 | android-lib 13 | android 14 | ) 15 | 16 | target_link_libraries( 17 | dabnn-jni 18 | dabnn 19 | ${android-lib} 20 | ) 21 | 22 | treat_warnings_as_errors(dabnn-jni) 23 | -------------------------------------------------------------------------------- /dabnn/jni/jni_handle.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef _HANDLE_H_INCLUDED_ 4 | #define _HANDLE_H_INCLUDED_ 5 | 6 | #include 7 | 8 | jfieldID getHandleField(JNIEnv *env, jobject obj) { 9 | jclass c = env->GetObjectClass(obj); 10 | // J is the type signature for long: 11 | return env->GetFieldID(c, "nativeHandle", "J"); 12 | } 13 | 14 | template 15 | T *getHandle(JNIEnv *env, jobject obj) { 16 | jlong handle = env->GetLongField(obj, getHandleField(env, obj)); 17 | return reinterpret_cast(handle); 18 | } 19 | 20 | void setHandle(JNIEnv *env, jobject obj, void *t) { 21 | jlong handle = reinterpret_cast(t); 22 | env->SetLongField(obj, getHandleField(env, obj), handle); 23 | } 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /dabnn/jni/net_for_jni.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "net_for_jni.h" 4 | 5 | namespace bnn {} 6 | -------------------------------------------------------------------------------- /dabnn/jni/net_for_jni.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_NET_FOR_JNI_H 4 | #define BNN_NET_FOR_JNI_H 5 | 6 | #include 7 | 8 | #include 9 | 10 | namespace bnn { 11 | class NetJNI { 12 | public: 13 | NetJNI() : net_(Net::create()){}; 14 | const std::shared_ptr net_; 15 | AAsset* asset; 16 | }; 17 | } // namespace bnn 18 | 19 | #endif /* BNN_NET_FOR_JNI_H */ 20 | -------------------------------------------------------------------------------- /dabnn/layer.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "layer.h" 4 | 5 | #include 6 | 7 | #include "net.h" 8 | 9 | namespace bnn { 10 | Layer::~Layer() {} 11 | 12 | Layer::MatCP Layer::mat(const std::string &name) const { 13 | return net_.lock()->get_blob(name); 14 | } 15 | 16 | void Layer::forward() { 17 | #ifdef BNN_BENCHMARK 18 | const auto start = std::chrono::system_clock::now(); 19 | #endif 20 | forward_impl(); 21 | #ifdef BNN_BENCHMARK 22 | const auto end = std::chrono::system_clock::now(); 23 | const auto elapsed_time_double = 24 | 1. * 25 | std::chrono::duration_cast(end - start) 26 | .count() / 27 | 1000; 28 | const auto elapsed_time = std::to_string(elapsed_time_double) + "ms"; 29 | 30 | net_.lock()->layer_time_[type_] += elapsed_time_double; 31 | #endif 32 | } 33 | 34 | std::string Layer::to_str() const { return ""; } 35 | 36 | } // namespace bnn 37 | -------------------------------------------------------------------------------- /dabnn/layer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_LAYER_H 4 | #define BNN_LAYER_H 5 | 6 | #include 7 | #include 8 | #include 9 | #include "mat.h" 10 | 11 | namespace bnn { 12 | 13 | class Net; 14 | 15 | class Layer { 16 | protected: 17 | // handy alias 18 | using NetCP = const std::weak_ptr; 19 | using MatCP = const std::shared_ptr; 20 | using MatP = std::shared_ptr; 21 | MatCP mat(const std::string &name) const; 22 | 23 | public: 24 | Layer(NetCP net, const std::string &name, const std::string &type) 25 | : net_(net), name_(name), type_(type) {} 26 | // virtual destructor 27 | virtual ~Layer(); 28 | 29 | NetCP net_; 30 | 31 | void forward(); 32 | virtual void forward_impl() const = 0; 33 | virtual std::string to_str() const; 34 | 35 | // layer name 36 | std::string name_; 37 | // layer type name 38 | std::string type_; 39 | }; 40 | 41 | } // namespace bnn 42 | 43 | #endif // BNN_LAYER_H 44 | -------------------------------------------------------------------------------- /dabnn/layers/Add.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Add.h" 4 | 5 | namespace bnn { 6 | 7 | inline void add_inplace(bnn::Mat &a, const bnn::Mat &b) { 8 | FORZ(n, a.n) { 9 | FORZ(h, a.h) { 10 | auto *a_ptr = a.point(n, h, 0); 11 | const auto *b_ptr = b.point(n, h, 0); 12 | FORZ(w, a.w) { 13 | FORZ(c, a.c) { *a_ptr++ += *b_ptr++; } 14 | } 15 | } 16 | } 17 | } 18 | 19 | inline void add(const bnn::Mat &a, const bnn::Mat &b, bnn::Mat &c) { 20 | FORZ(n, a.n) { 21 | FORZ(h, a.h) { 22 | const auto *a_ptr = a.point(n, h, 0); 23 | const auto *b_ptr = b.point(n, h, 0); 24 | auto *c_ptr = c.point(n, h, 0); 25 | FORZ(w, a.w) { 26 | FORZ(c, a.c) { *c_ptr++ = *a_ptr++ + *b_ptr++; } 27 | } 28 | } 29 | } 30 | } 31 | 32 | void Add::forward_impl() const { 33 | #ifdef BNN_CHECK_CONSISTENCY 34 | add(*input1_mat, *input2_mat, *output_mat); 35 | #else 36 | add_inplace(*input1_mat, *input2_mat); 37 | #endif 38 | } 39 | 40 | } // namespace bnn 41 | -------------------------------------------------------------------------------- /dabnn/layers/Add.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_ADD_H 4 | #define BNN_ADD_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Add : public Layer { 10 | public: 11 | MatCP input1_mat; 12 | MatCP input2_mat; 13 | 14 | #ifdef BNN_CHECK_CONSISTENCY 15 | MatCP output_mat; 16 | 17 | Add(NetCP net, const std::string &name, css input1, css input2, css output) 18 | : Layer(net, name, "Add"), 19 | input1_mat(mat(input1)), 20 | input2_mat(mat(input2)), 21 | output_mat(mat(output)) {} 22 | #else 23 | Add(NetCP net, const std::string &name, css input1, css input2) 24 | : Layer(net, name, "Add"), 25 | input1_mat(mat(input1)), 26 | input2_mat(mat(input2)) {} 27 | #endif 28 | virtual void forward_impl() const; 29 | ~Add() {} 30 | }; 31 | } // namespace bnn 32 | 33 | #endif /* BNN_ADD_H */ 34 | -------------------------------------------------------------------------------- /dabnn/layers/Affine.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Affine.h" 4 | 5 | namespace bnn { 6 | 7 | /** 8 | * per channel affine, x = a * x + b 9 | */ 10 | inline void affine_inplace(bnn::Mat &data, const bnn::Mat &a, 11 | const bnn::Mat &b) { 12 | FORZ(n, data.n) { 13 | FORZ(h, data.h) { 14 | auto ptr = data.point(n, h, 0); 15 | FORZ(w, data.w) { 16 | FORZ(c, data.c) { 17 | *ptr = a[c] * *ptr + b[c]; 18 | ptr++; 19 | } 20 | } 21 | } 22 | } 23 | } 24 | 25 | inline void affine(const bnn::Mat &data, const bnn::Mat &a, const bnn::Mat &b, 26 | bnn::Mat &output) { 27 | FORZ(n, data.n) { 28 | FORZ(h, data.h) { 29 | const auto *ptr = data.point(n, h, 0); 30 | auto output_ptr = output.point(n, h, 0); 31 | FORZ(w, data.w) { 32 | FORZ(c, data.c) { 33 | *output_ptr = a[c] * *ptr + b[c]; 34 | ptr++; 35 | output_ptr++; 36 | } 37 | } 38 | } 39 | } 40 | } 41 | 42 | void Affine::forward_impl() const { 43 | #ifdef BNN_CHECK_CONSISTENCY 44 | affine(*data_mat, *a_mat, *b_mat, *output_mat); 45 | #else 46 | affine_inplace(*data_mat, *a_mat, *b_mat); 47 | #endif 48 | } 49 | 50 | } // namespace bnn 51 | -------------------------------------------------------------------------------- /dabnn/layers/Affine.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_AFFINE_H 4 | #define BNN_AFFINE_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Affine : public Layer { 10 | public: 11 | MatCP data_mat; 12 | MatCP a_mat; 13 | MatCP b_mat; 14 | 15 | #ifdef BNN_CHECK_CONSISTENCY 16 | MatCP output_mat; 17 | 18 | Affine(NetCP net, const std::string &name, css data, css a, css b, 19 | css output) 20 | : Layer(net, name, "Affine"), 21 | data_mat(mat(data)), 22 | a_mat(mat(a)), 23 | b_mat(mat(b)), 24 | output_mat(mat(output)) {} 25 | #else 26 | Affine(NetCP net, const std::string &name, css data, css a, css b) 27 | : Layer(net, name, "Affine"), 28 | data_mat(mat(data)), 29 | a_mat(mat(a)), 30 | b_mat(mat(b)) {} 31 | #endif 32 | virtual void forward_impl() const; 33 | }; 34 | } // namespace bnn 35 | 36 | #endif /* BNN_AFFINE_H */ 37 | -------------------------------------------------------------------------------- /dabnn/layers/AvePool.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_AVEPOOL_H 4 | #define BNN_AVEPOOL_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class AvePool : public Layer { 10 | public: 11 | MatCP input_mat; 12 | std::shared_ptr padded_mat; 13 | MatCP output_mat; 14 | int kernel_h; 15 | int kernel_w; 16 | int pad_h; 17 | int pad_w; 18 | int stride_h; 19 | int stride_w; 20 | 21 | AvePool(NetCP net, const std::string &name, css input, css output, 22 | int kernel_h, int kernel_w, int pad_h, int pad_w, int stride_h, 23 | int stride_w); 24 | virtual void forward_impl() const; 25 | }; 26 | } // namespace bnn 27 | 28 | #endif /* BNN_AVEPOOL_H */ 29 | -------------------------------------------------------------------------------- /dabnn/layers/BinConv.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_BINCONV_H 4 | #define BNN_BINCONV_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class BinConv : public Layer { 10 | public: 11 | MatCP input_mat; 12 | MatP binarized_mat; 13 | MatP padded_mat; 14 | MatP col_mat; 15 | MatCP weight_mat; 16 | MatP transposed_weight_mat; 17 | MatCP output_mat; 18 | const int pad_h; 19 | const int pad_w; 20 | const int stride_h; 21 | const int stride_w; 22 | 23 | BinConv(NetCP net, const std::string &name, css input, css weight, 24 | css output, int pad_h, int pad_w, int stride_h, int stride_w); 25 | virtual void forward_impl() const; 26 | virtual std::string to_str() const; 27 | 28 | private: 29 | enum Method { 30 | DIRECT_CONV = 0, 31 | BGEMM, 32 | BCONV_NAIVE, 33 | BGEMM_NAIVE 34 | }; 35 | bool direct_conv_compatible() const; 36 | bool gemm_compatible() const; 37 | Method method() const; 38 | }; 39 | } // namespace bnn 40 | 41 | #endif /* BNN_BINCONV_H */ 42 | -------------------------------------------------------------------------------- /dabnn/layers/Binarize.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Binarize.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace bnn { 9 | void Binarize::forward_impl() const { pack_mat(*input_mat, *output_mat); } 10 | 11 | } // namespace bnn 12 | -------------------------------------------------------------------------------- /dabnn/layers/Binarize.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_BINARIZE_H 4 | #define BNN_BINARIZE_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Binarize : public Layer { 10 | public: 11 | MatCP input_mat; 12 | MatCP output_mat; 13 | 14 | Binarize(NetCP net, const std::string &name, css &input, css &output) 15 | : Layer(net, name, "Binarize"), 16 | input_mat(mat(input)), 17 | output_mat(mat(output)) {} 18 | virtual void forward_impl() const; 19 | }; 20 | } // namespace bnn 21 | 22 | #endif /* BNN_BINARIZE_H */ 23 | -------------------------------------------------------------------------------- /dabnn/layers/Concat.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Concat.h" 4 | 5 | namespace bnn { 6 | 7 | void Concat::forward_impl() const { 8 | const auto *in_ptr1 = static_cast(*input1_mat); 9 | const auto *in_ptr2 = static_cast(*input2_mat); 10 | auto *out_ptr = static_cast(*output_mat); 11 | 12 | FORZ(h, input1_mat->h) { 13 | FORZ(w, input1_mat->w) { 14 | memcpy(out_ptr, in_ptr1, input1_mat->c * sizeof(float)); 15 | out_ptr += input1_mat->c; 16 | in_ptr1 += input1_mat->c; 17 | memcpy(out_ptr, in_ptr2, input2_mat->c * sizeof(float)); 18 | out_ptr += input2_mat->c; 19 | in_ptr2 += input2_mat->c; 20 | } 21 | } 22 | } 23 | 24 | } // namespace bnn 25 | -------------------------------------------------------------------------------- /dabnn/layers/Concat.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_CONCAT_H 4 | #define BNN_CONCAT_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Concat : public Layer { 10 | public: 11 | MatCP input1_mat; 12 | MatCP input2_mat; 13 | MatCP output_mat; 14 | 15 | Concat(NetCP net, const std::string &name, css input1, css input2, 16 | css output) 17 | : Layer(net, name, "Concat"), 18 | input1_mat(mat(input1)), 19 | input2_mat(mat(input2)), 20 | output_mat(mat(output)) { 21 | BNN_ASSERT(input1_mat->c == input2_mat->c, 22 | "channels of two inputs should equal"); 23 | BNN_ASSERT(input1_mat->data_type == DataType::Float, 24 | "input1 data type should be float"); 25 | BNN_ASSERT(input2_mat->data_type == DataType::Float, 26 | "input2 data type should be float"); 27 | } 28 | virtual void forward_impl() const; 29 | }; 30 | } // namespace bnn 31 | 32 | #endif /* BNN_CONCAT_H */ 33 | -------------------------------------------------------------------------------- /dabnn/layers/FloatConv.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "FloatConv.h" 4 | 5 | #include 6 | 7 | namespace bnn { 8 | 9 | void FloatConv::forward_impl() const { 10 | if (bias_mat == nullptr) { 11 | fconv(*input_mat, *weight_mat, weight_mat->h, weight_mat->w, pad_h, 12 | pad_w, stride_h, stride_w, dilation, dilation, output_mat->c, 13 | *output_mat); 14 | } else { 15 | fconv(*input_mat, *weight_mat, *bias_mat, weight_mat->h, weight_mat->w, 16 | pad_h, pad_w, stride_h, stride_w, dilation, dilation, 17 | output_mat->c, *output_mat); 18 | } 19 | } 20 | 21 | std::string FloatConv::to_str() const { 22 | std::stringstream ss; 23 | ss << "input_h: " << std::to_string(input_mat->h) 24 | << ", input_w: " << std::to_string(input_mat->w) 25 | << ", input_c: " << std::to_string(input_mat->c) 26 | << ", weight_h: " << std::to_string(weight_mat->h) 27 | << ", weight_w: " << std::to_string(weight_mat->w) 28 | << ", weight_n: " << std::to_string(weight_mat->n); 29 | 30 | return ss.str(); 31 | } 32 | 33 | } // namespace bnn 34 | -------------------------------------------------------------------------------- /dabnn/layers/FloatConv.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_FLOATCONV_H 4 | #define BNN_FLOATCONV_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class FloatConv : public Layer { 10 | public: 11 | MatCP input_mat; 12 | MatCP weight_mat; 13 | MatCP bias_mat; 14 | MatCP output_mat; 15 | const int pad_h; 16 | const int pad_w; 17 | const int stride_h; 18 | const int stride_w; 19 | const int dilation; 20 | 21 | FloatConv(NetCP net, const std::string &name, css input, css weight, 22 | css output, int pad_h, int pad_w, int stride_h, int stride_w, 23 | int dilation) 24 | : Layer(net, name, "Float Conv"), 25 | input_mat(mat(input)), 26 | weight_mat(mat(weight)), 27 | output_mat(mat(output)), 28 | pad_h(pad_h), 29 | pad_w(pad_w), 30 | stride_h(stride_h), 31 | stride_w(stride_w), 32 | dilation(dilation) {} 33 | 34 | FloatConv(NetCP net, const std::string &name, css input, css weight, 35 | css bias, css output, int pad_h, int pad_w, int stride_h, 36 | int stride_w, int dilation) 37 | : Layer(net, name, "Float Conv"), 38 | input_mat(mat(input)), 39 | weight_mat(mat(weight)), 40 | bias_mat(mat(bias)), 41 | output_mat(mat(output)), 42 | pad_h(pad_h), 43 | pad_w(pad_w), 44 | stride_h(stride_h), 45 | stride_w(stride_w), 46 | dilation(dilation) {} 47 | 48 | virtual void forward_impl() const; 49 | virtual std::string to_str() const; 50 | }; 51 | } // namespace bnn 52 | 53 | #endif /* BNN_FLOATCONV_H */ 54 | -------------------------------------------------------------------------------- /dabnn/layers/MaxPool.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_MAXPOOL_H 4 | #define BNN_MAXPOOL_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class MaxPool : public Layer { 10 | public: 11 | MatCP input_mat; 12 | std::shared_ptr padded_mat; 13 | MatCP output_mat; 14 | int kernel_h; 15 | int kernel_w; 16 | int pad_h; 17 | int pad_w; 18 | int stride_h; 19 | int stride_w; 20 | 21 | MaxPool(NetCP net, const std::string &name, css input, css output, 22 | int kernel_h, int kernel_w, int pad_h, int pad_w, int stride_h, 23 | int stride_w); 24 | virtual void forward_impl() const; 25 | virtual std::string to_str() const; 26 | }; 27 | } // namespace bnn 28 | 29 | #endif /* BNN_MAXPOOL_H */ 30 | -------------------------------------------------------------------------------- /dabnn/layers/PRelu.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "PRelu.h" 4 | 5 | namespace bnn { 6 | void PRelu::forward_impl() const { 7 | BNN_ASSERT(slope_mat->total() == 1 || 8 | slope_mat->total() == static_cast(data_mat->c), 9 | "slope must have size 1 or input.channels"); 10 | float *ptr = static_cast(*data_mat); 11 | float *slope_ptr = static_cast(*slope_mat); 12 | if (slope_mat->total() == 1) { 13 | const auto slope = *slope_ptr; 14 | FORZ(i, data_mat->total()) { 15 | if (*ptr < 0) { 16 | *ptr = (*ptr) * slope; 17 | } 18 | ptr++; 19 | } 20 | } else if (slope_mat->total() == static_cast(data_mat->c)) { 21 | const auto nhw = data_mat->n * data_mat->h * data_mat->w; 22 | FORZ(i, nhw) { 23 | FORZ(j, data_mat->c) { 24 | if (*ptr < 0) { 25 | *ptr = (*ptr) * slope_ptr[j]; 26 | } 27 | ptr++; 28 | } 29 | } 30 | } 31 | } 32 | } // namespace bnn 33 | -------------------------------------------------------------------------------- /dabnn/layers/PRelu.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_PRELU_H 4 | #define BNN_PRELU_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class PRelu : public Layer { 10 | public: 11 | MatCP data_mat; 12 | MatCP slope_mat; 13 | 14 | PRelu(NetCP net, const std::string &name, css data, css slope) 15 | : Layer(net, name, "PRelu"), data_mat(mat(data)), slope_mat(mat(slope)) {} 16 | virtual void forward_impl() const; 17 | }; 18 | } // namespace bnn 19 | 20 | #endif /* BNN_PRELU_H */ 21 | 22 | -------------------------------------------------------------------------------- /dabnn/layers/Pad.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Pad.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace bnn { 9 | void Pad::forward_impl() const { pad(*input_mat, pad_h, pad_w, *output_mat); } 10 | 11 | } // namespace bnn 12 | -------------------------------------------------------------------------------- /dabnn/layers/Pad.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_PAD_H 4 | #define BNN_PAD_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Pad : public Layer { 10 | public: 11 | MatCP input_mat; 12 | MatCP output_mat; 13 | int pad_h; 14 | int pad_w; 15 | float val; 16 | 17 | Pad(NetCP net, const std::string &name, css &input, int pad_h, int pad_w, 18 | float val, css &output) 19 | : Layer(net, name, "Pad"), 20 | input_mat(mat(input)), 21 | output_mat(mat(output)), 22 | pad_h(pad_h), 23 | pad_w(pad_w), 24 | val(val) {} 25 | virtual void forward_impl() const; 26 | }; 27 | } // namespace bnn 28 | 29 | #endif /* BNN_PAD_H */ 30 | -------------------------------------------------------------------------------- /dabnn/layers/Relu.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Relu.h" 4 | 5 | #if __ARM_NEON 6 | #include 7 | #endif // __ARM_NEON 8 | 9 | namespace bnn { 10 | void Relu::forward_impl() const { 11 | #if __ARM_NEON 12 | float32x4_t _zero = vdupq_n_f32(0.f); 13 | float *ptr = static_cast(*data_mat); 14 | FORZ(i, data_mat->total() / 4) { 15 | float32x4_t _p = vld1q_f32(ptr); 16 | _p = vmaxq_f32(_p, _zero); 17 | vst1q_f32(ptr, _p); 18 | 19 | ptr += 4; 20 | } 21 | #else 22 | float *ptr = static_cast(*data_mat); 23 | FORZ(i, data_mat->total()) { 24 | *ptr = std::max(*ptr, 0.f); 25 | ptr++; 26 | } 27 | #endif // __ARM_NEON 28 | } 29 | } // namespace bnn 30 | -------------------------------------------------------------------------------- /dabnn/layers/Relu.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_RELU_H 4 | #define BNN_RELU_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Relu : public Layer { 10 | public: 11 | MatCP data_mat; 12 | 13 | Relu(NetCP net, const std::string &name, css data) 14 | : Layer(net, name, "Relu"), data_mat(mat(data)) {} 15 | virtual void forward_impl() const; 16 | }; 17 | } // namespace bnn 18 | 19 | #endif /* BNN_RELU_H */ 20 | -------------------------------------------------------------------------------- /dabnn/layers/Shuffle.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Shuffle.h" 4 | 5 | namespace bnn { 6 | void Shuffle::forward_impl() const { 7 | const auto c = data_mat->elem_c; 8 | const auto n = data_mat->total() / data_mat->c; 9 | if (c == 128) { 10 | VLOG(5) << "128 channels shuffle"; 11 | auto *ptr = static_cast(data_mat->data); 12 | FORZ(_, n) { 13 | const auto tmp = *(ptr + 1); 14 | *(ptr + 1) = *(ptr + 2); 15 | *(ptr + 2) = tmp; 16 | ptr += 4; 17 | } 18 | } else if (c == 256) { 19 | VLOG(5) << "256 channels shuffle"; 20 | auto *ptr = static_cast(data_mat->data); 21 | FORZ(_, n) { 22 | const auto tmp = *(ptr + 1); 23 | *(ptr + 1) = *(ptr + 2); 24 | *(ptr + 2) = tmp; 25 | ptr += 4; 26 | } 27 | } else if (c == 512) { 28 | VLOG(5) << "512 channels shuffle"; 29 | auto *ptr = static_cast(data_mat->data); 30 | FORZ(_, n) { 31 | const auto tmp1 = *(ptr + 2); 32 | const auto tmp2 = *(ptr + 3); 33 | *(ptr + 2) = *(ptr + 4); 34 | *(ptr + 3) = *(ptr + 5); 35 | *(ptr + 4) = tmp1; 36 | *(ptr + 5) = tmp2; 37 | ptr += 8; 38 | } 39 | } 40 | } 41 | } // namespace bnn 42 | -------------------------------------------------------------------------------- /dabnn/layers/Shuffle.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_SHUFFLE_H 4 | #define BNN_SHUFFLE_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Shuffle : public Layer { 10 | public: 11 | MatCP data_mat; 12 | 13 | Shuffle(NetCP net, css &name, css &data) 14 | : Layer(net, name, "Shuffle"), data_mat(mat(data)) { 15 | BNN_ASSERT(data_mat->data_type == DataType::Bit, 16 | "Shuffle only supports bit mat"); 17 | BNN_ASSERT(data_mat->elem_c % 128 == 0 && data_mat->elem_c <= 512, 18 | data_mat->elem_c); 19 | } 20 | virtual void forward_impl() const; 21 | }; 22 | } // namespace bnn 23 | 24 | #endif /* BNN_SHUFFLE_H */ 25 | -------------------------------------------------------------------------------- /dabnn/layers/Split.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include "Split.h" 4 | 5 | namespace bnn { 6 | void Split::forward_impl() const { 7 | const auto c = input_mat->c; 8 | const auto c_per_output = c / 2; 9 | auto *ptr = static_cast(input_mat->data); 10 | auto *ptr1 = static_cast(output_mat1->data); 11 | auto *ptr2 = static_cast(output_mat2->data); 12 | const auto nn = input_mat->total() / c; 13 | // PNT(nn); 14 | FORZ(_, nn) { 15 | memcpy(ptr1, ptr, c_per_output * sizeof(uint64_t)); 16 | ptr1 += c_per_output; 17 | ptr += c_per_output; 18 | memcpy(ptr2, ptr, c_per_output * sizeof(uint64_t)); 19 | ptr2 += c_per_output; 20 | ptr += c_per_output; 21 | } 22 | } 23 | } // namespace bnn 24 | -------------------------------------------------------------------------------- /dabnn/layers/Split.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_SPLIT_H 4 | #define BNN_SPLIT_H 5 | 6 | #include 7 | 8 | namespace bnn { 9 | class Split : public Layer { 10 | public: 11 | MatCP input_mat; 12 | MatCP output_mat1; 13 | MatCP output_mat2; 14 | 15 | Split(NetCP net, css &name, css &input, css &output1, css &output2) 16 | : Layer(net, name, "Split"), 17 | input_mat(mat(input)), 18 | output_mat1(mat(output1)), 19 | output_mat2(mat(output2)) { 20 | BNN_ASSERT(input_mat->data_type == DataType::Bit, 21 | "Split only supports bit mat"); 22 | BNN_ASSERT(input_mat->elem_c % 128 == 0 && input_mat->elem_c <= 512, 23 | input_mat->elem_c); 24 | } 25 | virtual void forward_impl() const; 26 | }; 27 | } // namespace bnn 28 | 29 | #endif /* BNN_SPLIT_H */ 30 | -------------------------------------------------------------------------------- /dabnn/net.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #ifndef BNN_NET_H 4 | #define BNN_NET_H 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include "layer.h" 19 | #include "mat.h" 20 | 21 | namespace bnn { 22 | class Net : public std::enable_shared_from_this { 23 | private: 24 | #ifdef BNN_BENCHMARK 25 | std::map layer_time_; 26 | #endif 27 | StrKeyMap> mat_map_; 28 | Shaper shaper; 29 | void add_mat(const std::string &name, std::shared_ptr mat); 30 | // The lifecycle of float_bufs_ is the same as Net object 31 | std::vector>> float_bufs_; 32 | std::vector> layers; 33 | 34 | std::string input_name_; 35 | 36 | std::weak_ptr get_weak(); 37 | 38 | void read_impl(const void *ptr); 39 | 40 | Net() = default; 41 | 42 | friend class Layer; 43 | friend class BinConv; 44 | friend class AvePool; 45 | friend class MaxPool; 46 | friend class FloatConv; 47 | friend class Affine; 48 | friend class Add; 49 | 50 | public: 51 | void read(const std::string &path); 52 | void read_buf(const void *ptr); 53 | void prepare(); 54 | void run(void *input); 55 | static std::shared_ptr create(); 56 | const flatbnn::Model *model_; 57 | 58 | std::shared_ptr get_blob(const std::string &name); 59 | bool optimize = true; 60 | bool run_fconv = true; 61 | bool strict = true; 62 | 63 | #ifdef BNN_BENCHMARK 64 | void print_time(); 65 | #endif 66 | }; 67 | } // namespace bnn 68 | 69 | #endif /* BNN_NET_H */ 70 | -------------------------------------------------------------------------------- /dabnn/pad.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include 4 | #include "mat.h" 5 | 6 | namespace bnn { 7 | inline void pad(const bnn::Mat &input, const int pad_h, const int pad_w, 8 | bnn::Mat &output, float val = 0.f) { 9 | BNN_ASSERT(input.data_type == output.data_type, 10 | "Input and output data_type is not the same"); 11 | if (output.data_type == bnn::DataType::Bit) { 12 | BNN_ASSERT(val == 0.f, "val cannot be set for DataType::Bit"); 13 | // 0 is 000000000....000 in binary, and take the role of -1 in xnor 14 | output.fill(0); 15 | FORZ(h, input.h) { 16 | auto *out_p = output.point(h + pad_h, pad_w); 17 | const auto *input_p = input.point(h, 0); 18 | memcpy(out_p, input_p, input.w * input.c * input.elemsize); 19 | } 20 | } else if (output.data_type == bnn::DataType::Float) { 21 | output.fill(val); 22 | FORZ(h, input.h) { 23 | auto *out_p = output.point(h + pad_h, pad_w); 24 | const auto *input_p = input.point(h, 0); 25 | memcpy(out_p, input_p, input.w * input.c * input.elemsize); 26 | } 27 | } else { 28 | BNN_ASSERT(false, "Unknown data_type"); 29 | } 30 | } 31 | } // namespace bnn 32 | -------------------------------------------------------------------------------- /docs/bconv_CN.md: -------------------------------------------------------------------------------- 1 | ## Bit-packing 2 | Bit-packing 在 `Binarize` 层进行,是指将 N 个 32 位的 float/integer,根据和 0 的大小关系,二值化为 N 个 bit (即 0 或 1),并打包成一个 N-bit 的整体,例如对 128 个浮点数进行 bit-packing 之后,就会产生一个 128-bit 的操作数。做了这一步,后续才可以进行位运算 xnor/xor。 3 | 4 | Bit-packing 的具体实现在 5 | 6 | * https://github.com/JDAI-CV/dabnn/blob/master/dabnn/bitpack.h#L29 (高优化版,针对 128 和以上 channel 数的 tensor) 7 | * https://github.com/JDAI-CV/dabnn/blob/master/dabnn/bitpack.h#L228 (低优化版,针对 128 channel 以下的 tensor) 8 | 9 | 高优化版和低优化版的性能差距在 4 倍左右。在高优化版中,bit-packing 算法直接利用 IEEE 754 float 和 int32 的符号位,而不需要把每一个数都和 0 比较,并使用了 SIMD 指令加速这一算法。值得一提的是,使用 SIMD 指令进行 bit-packing 后,输出的 N-bit 操作数的 N 个 bit 和 N 个输入不是按顺序对应的,但只要 xnor/xor 的两个操作数的每个 bit 一一对应,就不会对运算产生任何影响,因此,在适用高优化 bit-packing 的场景下,我们会对 weight 进行重排,使它的每个 bit 和 input 的每个 bit 一一对应,这一步的具体代码在 https://github.com/JDAI-CV/dabnn/blob/master/dabnn/net.cpp#L82。 10 | 11 | 卷积实现有很多种办法,dabnn 提供了如下两种优化实现。 12 | 13 | ## BGEMM 14 | 15 | GEMM 是实现浮点卷积的通用方法。它要求先用 [im2col](https://github.com/JDAI-CV/dabnn/blob/master/dabnn/im2col.h) 重排输入,经过 im2col 之后,卷积即可被表示为矩阵和矩阵的乘法,即 GEMM。GEMM 的加速方法在 CNN 火热起来之前,就已经得到了深入的研究。不过在二值卷积中,不能利用已有的 GEMM 库,因为它们是为 double、float 或 integer 准备的,因此 dabnn 实现了 BGEMM (Binary GEMM)。它的优点是性能不低,实现方便,一套 GEMM 代码即可处理所有的情况。 16 | 17 | BGEMM 的具体实现在 https://github.com/JDAI-CV/dabnn/blob/master/dabnn/bgemm.h。 18 | 19 | ## Binary Direct Convolution 20 | 21 | 然而 BGEMM 在 ARM 设备上并不高效,因为二值乘-加操作中,加法需要两步 - bitcount 和普通的加法。Bitcount 用来得到一个 N-bit 操作数中有多少 bit 是 1。在 ARMv8 设备上,bitcount 需要两条指令,ARMv7 设备上需要更多条指令。这大大限制了 BGEMM 的速度。因此 dabnn 提出了直接卷积的方法,称为 Binary Direct Convolution (BDC),它是指直接按照卷积的定义来计算卷积。在 BDC 中,通过一个简单的变换,大部分 bitcount 指令会被消除。它的优点是性能比 BGEMM 更高,但不能像 BGEMM 一样用一套代码覆盖所有的情况。 22 | 23 | 关于 BDC 如何消除大部分 bitcount 指令在 [bconv.md](bconv.md) 中有详细的说明。 24 | 25 | BDC 的具体实现在 https://github.com/JDAI-CV/dabnn/blob/master/dabnn/bconv.h。 26 | -------------------------------------------------------------------------------- /docs/benchmark_and_comparison.md: -------------------------------------------------------------------------------- 1 | ## Benchmark and Comparison 2 | 3 | Benchmark result on Google Pixel 1 (single thread): 4 | 5 | ``` 6 | 2019-05-06 10:36:48 7 | Running data/local/tmp/dabnn_benchmark 8 | Run on (4 X 1593.6 MHz CPU s) 9 | ***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead. 10 | -------------------------------------------------------------------- 11 | Benchmark Time CPU Iterations 12 | -------------------------------------------------------------------- 13 | dabnn_5x5_256 3661928 ns 3638192 ns 191 <--- input: 14*14*256, kernel: 256*5*5*256, output: 14*14*256, padding: 2 14 | dabnn_3x3_64 1306391 ns 1281553 ns 546 <--- input: 56*56*64, kernel: 64*3*3*64, output: 56*56*64, padding: 1 15 | dabnn_3x3_128 958388 ns 954754 ns 735 <--- input: 28*28*128, kernel: 128*3*3*128, output: 28*28*128, padding: 1 16 | dabnn_3x3_256 975123 ns 969810 ns 691 <--- input: 14*14*256, kernel: 256*3*3*256, output: 14*14*256, padding: 1 17 | dabnn_3x3_256_s2 268310 ns 267712 ns 2618 <--- input: 14*14*256, kernel: 256*3*3*256, output: 7*7*256, padding: 1, stride: 2 18 | dabnn_3x3_512 1281832 ns 1253921 ns 588 <--- input: 7* 7*512, kernel: 512*3*3*512, output: 7* 7*512, padding: 1 19 | dabnn_bireal18_imagenet 61920154 ns 61339185 ns 10 <--- Bi-Real Net 18, 56.4% top-1 on ImageNet 20 | dabnn_bireal18_imagenet_stem 43294019 ns 41401923 ns 14 <--- Bi-Real Net 18 with stem module (The network structure is described in detail in [our paper](https://arxiv.org/abs/1908.05858)), 56.4% top-1 on ImageNet 21 | ``` 22 | 23 | The following is the comparison between our dabnn and [Caffe](http://caffe.berkeleyvision.org) (full precision), [TensorFlow Lite](https://www.tensorflow.org/lite) (full precision) and [BMXNet](https://github.com/hpi-xnor/BMXNet) (binary). We surprisingly observe that BMXNet is even slower than the full precision TensorFlow Lite. It suggests that the potential of binary neural networks is far from exploited until our dabnn is published. 24 | 25 | ![Comparison](/images/comparison_en.png) 26 | 27 | 28 | -------------------------------------------------------------------------------- /docs/build.md: -------------------------------------------------------------------------------- 1 | ## Build 2 | 3 | We use CMake build system like most C++ projects. 4 | 5 | ### Clone the project 6 | 7 | Clone dabnn recursively 8 | 9 | ```bash 10 | git clone --recursive https://github.com/JDAI-CV/dabnn 11 | ``` 12 | 13 | ### Build dabnn 14 | 15 | Cross-compiling for ARMv8 Android: 16 | 17 | 1. Download and unzip Android NDK from https://developer.android.com/ndk/downloads. 18 | 19 | 2. Run cmake with the toolchain file, which determine the proper compiling toolchains. **If this step fails, please check whether the toolchain file really exists on the path you set.** 20 | 21 | ```bash 22 | mkdir build-dabnn 23 | cd build-dabnn 24 | cmake -DCMAKE_TOOLCHAIN_FILE=the_path_to_android_ndk/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DCMAKE_BUILD_TYPE=Release .. 25 | ``` 26 | 27 | 3. Build 28 | 29 | ```bash 30 | cmake --build . 31 | ``` 32 | 33 | For ARMv7, just replace `-DANDROID_ABI=arm64-v8a` with `-DANDROID_ABI=armeabi-v7a` in step 2. 34 | 35 | For non-Android ARM devices, use the proper toolchain file for your device instead of the Android NDK toolchain file, or compile natively on your ARM device. 36 | 37 | For non-ARM devices, only the unoptimized code will work. If you still want to build dabnn for non-ARM devices, pass `-DBNN_BUILD_MAIN_LIB=ON` in step 2. 38 | 39 | ### Build onnx2bnn 40 | 41 | On non-ARM devices, just run cmake and build the project directly. 42 | 43 | 1. Run cmake 44 | 45 | ```bash 46 | mkdir build-onnx2bnn 47 | cd build-onnx2bnn 48 | cmake .. 49 | ``` 50 | 51 | 2. Build 52 | 53 | ```bash 54 | cmake --build . 55 | ``` 56 | 57 | On ARM devices, pass `-DBNN_BUILD_MAIN_LIB=OFF` in step 1. 58 | -------------------------------------------------------------------------------- /docs/model_conversion.md: -------------------------------------------------------------------------------- 1 | ## If you want to benchmark an existing full-precision network structure 2 | 3 | If you just want to benchmark the latency of a BNN instead of deploying it, you can determine which convolutions in the input ONNX model are binary by passing "--binary-list filename" command-line argument. Each line of the file is the **output name** of a convolution. 4 | 5 | For example, you have a full-precision model named "model.onnx". In the model, you want three convolutions, whose outputs are "34", "36", "55" respectively, to be binary convolutions, and test how fast the model with three binary convolutions will be. In this case, you should first create a text file with 6 | 7 | > 34 8 | > 9 | > 36 10 | > 11 | > 55 12 | 13 | After creating the text file (Let's assume the file is named "my_binary_convs"), you can convert the model by 14 | 15 | ```bash 16 | ./onnx2bnn model.onnx model.dab --binary-list my_binary_convs 17 | ``` 18 | 19 | Once the command finishes, you will get a BNN model named model.dab. 20 | 21 | ## If you want to train and export a dabnn-compatible ONNX model 22 | 23 | If you want to train and deploy a BNN on real device, the following instructions are what you needed. 24 | 25 | Binary convolutions are not supported natively by training frameworks (e.g., TensorFlow, PyTorch, MXNet). To implement correct and dabnn-compatible binary convolutions by self, there is something needed attention: 26 | 27 | 1. The input of binary convolutions should only be +1/-1, but the padding value of convolution is 0. 28 | 29 | 2. PyTorch doesn't support export ONNX sign operator until PyTorch 1.2. 30 | 31 | Therefore, we provide a ["standard" PyTorch implementation](https://gist.github.com/daquexian/7db1e7f1e0a92ab13ac1ad028233a9eb) which is compatible with dabnn and produces a correct result. The implementations TensorFlow, MXNet and other training frameworks should be similar. 32 | 33 | #### How does dabnn recognize binary convolutions in ONNX model 34 | 35 | The converter `onnx2bnn` has three mode in terms of how it recognizes binary convolutions: 36 | 37 | * Aggressive (default). In this mode, onnx2bnn will mark all convolutions whose weights consist of only +1 or -1 as binary convolutions. The aggressive mode is for the existing BNN models which do not have the correct padding value (-1 rather than 0). Note: The output of the generated dabnn model is different from that of the ONNX model since the padding value is 0 instead of -1. 38 | * Moderate. This mode is for our "standard" implementation -- A Conv operator with binary weight and following a -1 Pad operator. 39 | * Strict. In this mode, onnx2bnn only recognizes the following natural and correct "pattern" of binary convolutions: A Conv operator, whose input is got from a Sign op and a Pad op (the order doesn't matter), and weight is got from a Sign op. 40 | 41 | For now "Aggressive" is the default mode. To enable moderate or strict mode, pass "--moderate" or "--strict" command-line argument to onnx2bnn. 42 | -------------------------------------------------------------------------------- /docs/onnx2bnn.md: -------------------------------------------------------------------------------- 1 | ## About ONNX 2 | 3 | [ONNX](http://onnx.ai) (Open Neural Network Exchange) is an open format which is greatly supported or officially integrated by [many frameworks and tools](http://onnx.ai/supported-tools). 4 | 5 | ## How onnx2bnn converts the models 6 | 7 | 1. Recognizing binary convolutions, whose weights will be bit-packed. The developers of dabnn added several [optimizer](https://github.com/onnx/onnx/blob/master/docs/Optimizer.md) to ONNX in order to recognize binary convolutions. The details is in dabnn_*.h of https://github.com/daquexian/onnx/tree/optimizer_for_bnn/onnx/optimizer/passes. For bit-packing, please check out [this documentation](bconv.md) 8 | 9 | 2. Update the weight and bias of BN layers following binary conv layers. Since -1 in binary convs is represented by a unset bit (i.e., 0), and bitcount returns the number of set bits (i.e., 1) in a N-bit operand, a correction is needed to get the correct result of binary convs. Specifically, denote a as an N-bit operand, b as the number of set bits in a, c as the number of unset bits in a, the result we want is 10 | 11 | > b - c = b - (N - b) = 2 * b - N = 2 * bitcount(a) - N 12 | 13 | It is an affine transform of bitcount(a), so we accordingly update the weight and bias of the corresponding BN layers. 14 | 15 | The details is in https://github.com/JDAI-CV/dabnn/blob/master/tools/onnx2bnn/OnnxConverter.cpp#L522. 16 | 17 | 3. Other layers are converted as usual. 18 | 19 | ## Notes (Need Attention) 20 | 21 | There are some notes for model conversion. 22 | 23 | 1. Binary convolutions are custom operations in training frameworks (e.g., TensorFlow, PyTorch), so the implementations are various. Unfortunately, the most existing implementations of binary convs are not correct. For example, they always pad 0 to their input, while the input should only be +1 or -1. The developers of dabnn provide [a standard implementation of binary convs in PyTorch](https://gist.github.com/daquexian/7db1e7f1e0a92ab13ac1ad028233a9eb). We advise trainers of BNNs to use this implementation, or implement binary convs in their own training frameworks according to this implementation. 24 | 25 | 2. onnx2bnn has multiple recognizing levels. It can even recognize the incorrect binary convs described above (the result will be incorrect though). Please check out [this documentation](https://github.com/JDAI-CV/dabnn/wiki/Train,-export-and-convert-a-dabnn-model) for details. 26 | 27 | 3. `group` is not supported for now. 28 | -------------------------------------------------------------------------------- /docs/onnx2bnn_CN.md: -------------------------------------------------------------------------------- 1 | ## 关于 ONNX 2 | 3 | ONNX (Open Neural Network Exchange) 是一个独立于训练框架的模型格式,[众多框架和工具](http://onnx.ai/supported-tools) 支持 ONNX 格式。 4 | 5 | ## 模型转换流程 6 | 7 | 1. 识别二值卷积,对二值卷积的 weight 进行 bit-packing。dabnn 开发者给 onnx 增加了多个 optimizer,用来识别二值卷积,具体实现可参考 https://github.com/daquexian/onnx/tree/optimizer_for_bnn/onnx/optimizer/passes 中的 dabnn_*.h。关于 bit-packing 可以参考 [这篇文档](bconv_CN.md); 8 | 9 | 2. 修改紧跟着二值卷积的 BN 层的权重。因为 bit 只有 1 和 0 两个值,所以二值卷积中的 -1 被用 0 表示,bitcount 可以得到一个 N-bit 操作数中,值为 1 的 bit 的数量,这忽略了 -1 的存在。具体来说,设 a 为一个 N-bit 操作数,b 是 a 中值为 1 的 bit 数量,c 是 a 中值为 0 的 bit 数量(即 -1 的数量) 10 | 11 | 在计算卷积时,我们应该得到的值是 12 | 13 | > b - c = b - (N - b) = 2 * b - N = 2 * bitcount(a) - N 14 | 15 | 这个值可以经过一个对 bitcount(a) 的线性变换得到,因此我们将这个变换融合进二值卷积之后的 BN 层之中。 16 | 17 | 具体实现在 https://github.com/JDAI-CV/dabnn/blob/master/tools/onnx2bnn/OnnxConverter.cpp#L530。 18 | 19 | 3. 其他 Layer 正常处理。 20 | 21 | ## 注意事项(必看) 22 | 23 | 模型转换过程中有些规则或限制需要额外说明。 24 | 25 | 1. 二值卷积是自定义操作,因此可能存在多种实现,网上存在的大部分二值卷积自定义实现是不完全正确的,例如它们用 0 进行 pad,而忽略了二值卷积的输入只能有 +1 和 -1。dabnn 开发者提供了一个[标准的二值卷积 PyTorch 实现](https://gist.github.com/daquexian/7db1e7f1e0a92ab13ac1ad028233a9eb),我们建议所有二值网络的训练者使用这个实现,或是按照这个实现来在他们用的训练框架中自行实现二值卷积; 26 | 27 | 2. onnx2bnn 有多种针对二值卷积的识别模式,例如会根据卷积的权重(是否为 +1/-1)识别、根据 Sign operator 识别,在用户选择 aggressive 模式时,甚至可以识别上一条所述的非正确的二值卷积(但在运算时仍会以 -1 而不是 0 来 pad,因此会导致结果不完全一致)。具体请看 [这篇文档](https://github.com/JDAI-CV/dabnn/wiki/Train,-export-and-convert-a-dabnn-model); 28 | 29 | 3. 目前暂时不支持 `group` 参数。 30 | -------------------------------------------------------------------------------- /docs/overall.md: -------------------------------------------------------------------------------- 1 | ## Background 2 | 3 | Binary Neural Networks is proposed in [Binary Neural Networks](https://arxiv.org/abs/1602.02830) and [XNOR-Net](https://arxiv.org/abs/1603.05279). In the following papers, [Bi-Real Net](https://arxiv.org/abs/1808.00278) presented some new training method in order to improve the performance, [BENN](https://arxiv.org/abs/1806.07550) leverages emsemble on BNNs. 4 | 5 | BNNs can save 10X+ memory, and several times as fast as float NNs. What's more, it theoretically [saves 10X energy](https://camo.githubusercontent.com/e725038be60ce4bb698b22480603b636a92beeaf/687474703a2f2f66696c652e656c656366616e732e636f6d2f776562312f4d30302f35352f37392f7049594241467373565f5341504f63534141435742546f6d6531633033392e706e67), so the battery life of devices will be expanded a lot. 6 | 7 | ## Some notes 8 | 9 | 1. The BNN models can be trained by any frameworks which support ONNX. Note that binary convs are custom operations, please check out [onnx2bnn.md](docs/onnx2bnn.md) for how to make the model comptabile with dabnn. 10 | 11 | 2. For the implementation of binary convolutions, please check out [bconv.md](bconv.md). 12 | -------------------------------------------------------------------------------- /docs/overall_CN.md: -------------------------------------------------------------------------------- 1 | ## 背景和发展方向 2 | 3 | 二值网络比较年轻,最初的两篇文章是 2016 年的 [Binary Neural Networks](https://arxiv.org/abs/1602.02830) 和 [XNOR-Net](https://arxiv.org/abs/1603.05279)。后续的工作中,[Bi-Real Net](https://arxiv.org/abs/1808.00278) 提出了一些精度提升方法,[BENN](https://arxiv.org/abs/1806.07550v2) 用 ensemble 方法进一步提升了 BNN 在分类任务上的表现,结果甚至超过单精度浮点模型。 4 | 5 | 但是从移动端工程应用的角度来看,定点网络可以节省数十倍的内存、提升数倍推理速度,同时降低十倍以上能耗。这意味着原本设备充电一次只能用一个小时,现在理论上可以用十小时以上。能耗相关可参见[相关测试](https://camo.githubusercontent.com/e725038be60ce4bb698b22480603b636a92beeaf/687474703a2f2f66696c652e656c656366616e732e636f6d2f776562312f4d30302f35352f37392f7049594241467373565f5341504f63534141435742546f6d6531633033392e706e67)。 6 | 7 | 综合算法和工程来看,部分二值网络实用意义和竞争优势可能在以下两点: 8 | 9 | 1. 与已量产设备融合。嵌入式设备在设计过程中,为了节约成本往往会做成“刚好够用”的状态。二值卷积在计算过程中既不需要大量的 SIMD 乘法操作,内存需求也远低于 8bit 模型,对原有系统干扰极小; 10 | 2. 在分类任务中以混合精度的方式替换已有方法。 11 | 12 | 卷积曾出现过很多变种,但是其中大部分已被历史淘汰。BNN 要想避免此命运,最简单的方法莫过于尽快落在某个产品或项目上,证明自己的价值。 13 | 14 | 15 | ## 软件架构 16 | 在使用流程和软件结构方面,dabnn 和已开源的推理库(如 [ncnn](https://github.com/Tencent/ncnn)、[Tengine](https://github.com/OAID/Tengine)、[FeatherCNN](https://github.com/Tencent/FeatherCNN) 等)差距不大: 17 | 18 | 1. 模型训练可使用任意一种可以导出 ONNX 模型的框架,但需要注意的是,二值卷积是自定义操作,为了让模型中二值卷积可以被 dabnn 正确识别,请看 [onnx2bnn_CN.md](onnx2bnn_CN.md)。 19 | 2. 部署模型前需要把 onnx 格式转换成 dabnn 内部格式。在转换过程中,会把二值卷积的权重转换为 1-bit (而不是默认的 32-bit),大大减小模型文件的体积。流程和**注意事项**可参照 [onnx2bnn_CN.md](onnx2bnn_CN.md); 20 | 3. 二值卷积实现请查阅 [bconv_CN.md](bconv_CN.md) 21 | -------------------------------------------------------------------------------- /docs/svgs/09e963a9a257d451169d317f04f4cf59.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/svgs/4fd661cfefdf4318d1aa35fb483796b2.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/svgs/bdbf342b57819773421273d508dba586.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/svgs/c745b9b57c145ec5577b82542b2df546.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/svgs/d0b09e58d8b197fff6fc95ea3bca20fe.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/svgs/f6128a2d469857252e8e52385e7a00c5.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/svgs/f9c4988898e7f532b9f826a75014ed3c.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/svgs/ff7cbf533a4e41019c689366004849fb.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /images/comparison_cn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JDAI-CV/dabnn/d93aa950788319d92ade545eb7fe7ac6f1589fd1/images/comparison_cn.png -------------------------------------------------------------------------------- /images/comparison_en.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JDAI-CV/dabnn/d93aa950788319d92ade545eb7fe7ac6f1589fd1/images/comparison_en.png -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | add_executable(bconv_test bconv_test.cpp) 4 | target_link_libraries(bconv_test dabnn gtest_main) 5 | add_test(NAME bconv_test COMMAND bconv_test) 6 | 7 | add_executable(bitpack_test bitpack_test.cpp) 8 | target_link_libraries(bitpack_test dabnn gtest_main) 9 | add_test(NAME bitpack_test COMMAND bitpack_test) 10 | 11 | add_executable(im2col_test im2col_test.cpp) 12 | target_link_libraries(im2col_test dabnn gtest_main) 13 | add_test(NAME im2col_test COMMAND im2col_test) 14 | 15 | add_executable(fconv_test fconv_test.cpp) 16 | target_link_libraries(fconv_test dabnn gtest_main) 17 | add_test(NAME fconv_test COMMAND fconv_test) 18 | 19 | add_executable(bgemm_test bgemm_test.cpp) 20 | target_link_libraries(bgemm_test dabnn gtest_main) 21 | add_test(NAME bgemm_test COMMAND bgemm_test) 22 | 23 | add_executable(net_test net_test.cpp) 24 | target_link_libraries(net_test dabnn gtest_main) 25 | add_test(NAME net_test COMMAND net_test) 26 | 27 | -------------------------------------------------------------------------------- /tests/bitpack_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #ifdef __aarch64__ 13 | TEST(bitpack, pack_mat_128) { 14 | const size_t AHEIGHT = 64; 15 | const size_t AWIDTH = 64; 16 | const size_t CHANNEL = 256; 17 | const size_t ALEN = AHEIGHT * AWIDTH * CHANNEL; 18 | float a_data[ALEN]; 19 | fill_rand_float(a_data, ALEN); 20 | 21 | const bnn::Mat a(AHEIGHT, AWIDTH, CHANNEL, a_data, bnn::DataType::Float); 22 | bnn::Mat a_binary(AHEIGHT, AWIDTH, CHANNEL, bnn::DataType::Bit); 23 | bnn::Mat expected(AHEIGHT, AWIDTH, CHANNEL, bnn::DataType::Bit); 24 | pack_mat_128_opt(a, a_binary); 25 | 26 | baseline_pack_mat(a, expected); 27 | 28 | FORZS(i, a_binary.total(), 2) { 29 | // LOG(INFO) << i; 30 | ASSERT_EQ(bitcount(*(static_cast(a_binary) + i)) + 31 | bitcount(*(static_cast(a_binary) + i + 1)), 32 | bitcount(*(static_cast(expected) + i)) + 33 | bitcount(*(static_cast(expected) + i + 1))); 34 | } 35 | } 36 | #endif // __aarch64__ 37 | 38 | TEST(bitpack, pack_mat_64) { 39 | const size_t AHEIGHT = 64; 40 | const size_t AWIDTH = 64; 41 | const size_t CHANNEL = 256; 42 | const size_t ALEN = AHEIGHT * AWIDTH * CHANNEL; 43 | float a_data[ALEN]; 44 | fill_rand_float(a_data, ALEN); 45 | 46 | const bnn::Mat a(AHEIGHT, AWIDTH, CHANNEL, a_data, bnn::DataType::Float); 47 | bnn::Mat a_binary(AHEIGHT, AWIDTH, CHANNEL, bnn::DataType::Bit); 48 | bnn::Mat expected(AHEIGHT, AWIDTH, CHANNEL, bnn::DataType::Bit); 49 | pack_mat_64(a, a_binary); 50 | 51 | baseline_pack_mat(a, expected); 52 | 53 | FORZS(i, a_binary.total(), 2) { 54 | // LOG(INFO) << i; 55 | ASSERT_EQ(bitcount(*(static_cast(a_binary) + i)) + 56 | bitcount(*(static_cast(a_binary) + i + 1)), 57 | bitcount(*(static_cast(expected) + i)) + 58 | bitcount(*(static_cast(expected) + i + 1))); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/im2col_test.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | namespace bnn { 12 | 13 | TEST(im2col, im2col) { 14 | const size_t len = 32; 15 | float data[len]; 16 | for (size_t i = 0; i < len; i++) { 17 | data[i] = i; 18 | } 19 | Mat im(4, 4, 2, data, DataType::Float); 20 | Mat col(288, DataType::Float); 21 | im2col(im, 3, 3, 1, 1, 1, 1, 1, 1, col); 22 | float data_col_expected[]{ 23 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 8, 9, 10, 11, 24 | 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 25 | 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 26 | 0, 0, 0, 0, 0, 0, 4, 5, 6, 7, 0, 0, 12, 13, 14, 15, 0, 0, 27 | 0, 0, 0, 1, 2, 3, 0, 0, 8, 9, 10, 11, 0, 0, 16, 17, 18, 19, 28 | 0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 16, 17, 18, 19, 20, 21, 29 | 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 30 | 4, 5, 6, 7, 0, 0, 12, 13, 14, 15, 0, 0, 20, 21, 22, 23, 0, 0, 31 | 0, 0, 8, 9, 10, 11, 0, 0, 16, 17, 18, 19, 0, 0, 24, 25, 26, 27, 32 | 8, 9, 10, 11, 12, 13, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 33 | 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 23, 26, 27, 28, 29, 30, 31, 34 | 12, 13, 14, 15, 0, 0, 20, 21, 22, 23, 0, 0, 28, 29, 30, 31, 0, 0, 35 | 0, 0, 16, 17, 18, 19, 0, 0, 24, 25, 26, 27, 0, 0, 0, 0, 0, 0, 36 | 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 0, 0, 0, 0, 0, 0, 37 | 18, 19, 20, 21, 22, 23, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 38 | 20, 21, 22, 23, 0, 0, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0}; 39 | 40 | Mat col_expected(288, data_col_expected, DataType::Float); 41 | ASSERT_EQ(col.flatten(), col_expected.flatten()); 42 | } 43 | 44 | } // namespace bnn 45 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(RegexUtils) 2 | test_escape_string_as_regex() 3 | 4 | file(GLOB Eigen_directory_files "*") 5 | 6 | escape_string_as_regex(ESCAPED_CMAKE_CURRENT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") 7 | 8 | foreach(f ${Eigen_directory_files}) 9 | if(NOT f MATCHES "\\.txt" AND NOT f MATCHES "${ESCAPED_CMAKE_CURRENT_SOURCE_DIR}/[.].+" AND NOT f MATCHES "${ESCAPED_CMAKE_CURRENT_SOURCE_DIR}/src") 10 | list(APPEND Eigen_directory_files_to_install ${f}) 11 | endif() 12 | endforeach(f ${Eigen_directory_files}) 13 | 14 | install(FILES 15 | ${Eigen_directory_files_to_install} 16 | DESTINATION ${INCLUDE_INSTALL_DIR}/Eigen COMPONENT Devel 17 | ) 18 | 19 | install(DIRECTORY src DESTINATION ${INCLUDE_INSTALL_DIR}/Eigen COMPONENT Devel FILES_MATCHING PATTERN "*.h") 20 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Cholesky: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_CHOLESKY_MODULE_H 9 | #define EIGEN_CHOLESKY_MODULE_H 10 | 11 | #include "Core" 12 | #include "Jacobi" 13 | 14 | #include "src/Core/util/DisableStupidWarnings.h" 15 | 16 | /** \defgroup Cholesky_Module Cholesky module 17 | * 18 | * 19 | * 20 | * This module provides two variants of the Cholesky decomposition for selfadjoint (hermitian) matrices. 21 | * Those decompositions are also accessible via the following methods: 22 | * - MatrixBase::llt() 23 | * - MatrixBase::ldlt() 24 | * - SelfAdjointView::llt() 25 | * - SelfAdjointView::ldlt() 26 | * 27 | * \code 28 | * #include 29 | * \endcode 30 | */ 31 | 32 | #include "src/Cholesky/LLT.h" 33 | #include "src/Cholesky/LDLT.h" 34 | #ifdef EIGEN_USE_LAPACKE 35 | #ifdef EIGEN_USE_MKL 36 | #include "mkl_lapacke.h" 37 | #else 38 | #include "src/misc/lapacke.h" 39 | #endif 40 | #include "src/Cholesky/LLT_LAPACKE.h" 41 | #endif 42 | 43 | #include "src/Core/util/ReenableStupidWarnings.h" 44 | 45 | #endif // EIGEN_CHOLESKY_MODULE_H 46 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 47 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/CholmodSupport: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_CHOLMODSUPPORT_MODULE_H 9 | #define EIGEN_CHOLMODSUPPORT_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | extern "C" { 16 | #include 17 | } 18 | 19 | /** \ingroup Support_modules 20 | * \defgroup CholmodSupport_Module CholmodSupport module 21 | * 22 | * This module provides an interface to the Cholmod library which is part of the suitesparse package. 23 | * It provides the two following main factorization classes: 24 | * - class CholmodSupernodalLLT: a supernodal LLT Cholesky factorization. 25 | * - class CholmodDecomposiiton: a general L(D)LT Cholesky factorization with automatic or explicit runtime selection of the underlying factorization method (supernodal or simplicial). 26 | * 27 | * For the sake of completeness, this module also propose the two following classes: 28 | * - class CholmodSimplicialLLT 29 | * - class CholmodSimplicialLDLT 30 | * Note that these classes does not bring any particular advantage compared to the built-in 31 | * SimplicialLLT and SimplicialLDLT factorization classes. 32 | * 33 | * \code 34 | * #include 35 | * \endcode 36 | * 37 | * In order to use this module, the cholmod headers must be accessible from the include paths, and your binary must be linked to the cholmod library and its dependencies. 38 | * The dependencies depend on how cholmod has been compiled. 39 | * For a cmake based project, you can use our FindCholmod.cmake module to help you in this task. 40 | * 41 | */ 42 | 43 | #include "src/CholmodSupport/CholmodSupport.h" 44 | 45 | #include "src/Core/util/ReenableStupidWarnings.h" 46 | 47 | #endif // EIGEN_CHOLMODSUPPORT_MODULE_H 48 | 49 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Dense: -------------------------------------------------------------------------------- 1 | #include "Core" 2 | #include "LU" 3 | #include "Cholesky" 4 | #include "QR" 5 | #include "SVD" 6 | #include "Geometry" 7 | #include "Eigenvalues" 8 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Eigen: -------------------------------------------------------------------------------- 1 | #include "Dense" 2 | #include "Sparse" 3 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Eigenvalues: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_EIGENVALUES_MODULE_H 9 | #define EIGEN_EIGENVALUES_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | #include "Cholesky" 16 | #include "Jacobi" 17 | #include "Householder" 18 | #include "LU" 19 | #include "Geometry" 20 | 21 | /** \defgroup Eigenvalues_Module Eigenvalues module 22 | * 23 | * 24 | * 25 | * This module mainly provides various eigenvalue solvers. 26 | * This module also provides some MatrixBase methods, including: 27 | * - MatrixBase::eigenvalues(), 28 | * - MatrixBase::operatorNorm() 29 | * 30 | * \code 31 | * #include 32 | * \endcode 33 | */ 34 | 35 | #include "src/misc/RealSvd2x2.h" 36 | #include "src/Eigenvalues/Tridiagonalization.h" 37 | #include "src/Eigenvalues/RealSchur.h" 38 | #include "src/Eigenvalues/EigenSolver.h" 39 | #include "src/Eigenvalues/SelfAdjointEigenSolver.h" 40 | #include "src/Eigenvalues/GeneralizedSelfAdjointEigenSolver.h" 41 | #include "src/Eigenvalues/HessenbergDecomposition.h" 42 | #include "src/Eigenvalues/ComplexSchur.h" 43 | #include "src/Eigenvalues/ComplexEigenSolver.h" 44 | #include "src/Eigenvalues/RealQZ.h" 45 | #include "src/Eigenvalues/GeneralizedEigenSolver.h" 46 | #include "src/Eigenvalues/MatrixBaseEigenvalues.h" 47 | #ifdef EIGEN_USE_LAPACKE 48 | #ifdef EIGEN_USE_MKL 49 | #include "mkl_lapacke.h" 50 | #else 51 | #include "src/misc/lapacke.h" 52 | #endif 53 | #include "src/Eigenvalues/RealSchur_LAPACKE.h" 54 | #include "src/Eigenvalues/ComplexSchur_LAPACKE.h" 55 | #include "src/Eigenvalues/SelfAdjointEigenSolver_LAPACKE.h" 56 | #endif 57 | 58 | #include "src/Core/util/ReenableStupidWarnings.h" 59 | 60 | #endif // EIGEN_EIGENVALUES_MODULE_H 61 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 62 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Geometry: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_GEOMETRY_MODULE_H 9 | #define EIGEN_GEOMETRY_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | #include "SVD" 16 | #include "LU" 17 | #include 18 | 19 | /** \defgroup Geometry_Module Geometry module 20 | * 21 | * This module provides support for: 22 | * - fixed-size homogeneous transformations 23 | * - translation, scaling, 2D and 3D rotations 24 | * - \link Quaternion quaternions \endlink 25 | * - cross products (\ref MatrixBase::cross, \ref MatrixBase::cross3) 26 | * - orthognal vector generation (\ref MatrixBase::unitOrthogonal) 27 | * - some linear components: \link ParametrizedLine parametrized-lines \endlink and \link Hyperplane hyperplanes \endlink 28 | * - \link AlignedBox axis aligned bounding boxes \endlink 29 | * - \link umeyama least-square transformation fitting \endlink 30 | * 31 | * \code 32 | * #include 33 | * \endcode 34 | */ 35 | 36 | #include "src/Geometry/OrthoMethods.h" 37 | #include "src/Geometry/EulerAngles.h" 38 | 39 | #include "src/Geometry/Homogeneous.h" 40 | #include "src/Geometry/RotationBase.h" 41 | #include "src/Geometry/Rotation2D.h" 42 | #include "src/Geometry/Quaternion.h" 43 | #include "src/Geometry/AngleAxis.h" 44 | #include "src/Geometry/Transform.h" 45 | #include "src/Geometry/Translation.h" 46 | #include "src/Geometry/Scaling.h" 47 | #include "src/Geometry/Hyperplane.h" 48 | #include "src/Geometry/ParametrizedLine.h" 49 | #include "src/Geometry/AlignedBox.h" 50 | #include "src/Geometry/Umeyama.h" 51 | 52 | // Use the SSE optimized version whenever possible. At the moment the 53 | // SSE version doesn't compile when AVX is enabled 54 | #if defined EIGEN_VECTORIZE_SSE && !defined EIGEN_VECTORIZE_AVX 55 | #include "src/Geometry/arch/Geometry_SSE.h" 56 | #endif 57 | 58 | #include "src/Core/util/ReenableStupidWarnings.h" 59 | 60 | #endif // EIGEN_GEOMETRY_MODULE_H 61 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 62 | 63 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Householder: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_HOUSEHOLDER_MODULE_H 9 | #define EIGEN_HOUSEHOLDER_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | /** \defgroup Householder_Module Householder module 16 | * This module provides Householder transformations. 17 | * 18 | * \code 19 | * #include 20 | * \endcode 21 | */ 22 | 23 | #include "src/Householder/Householder.h" 24 | #include "src/Householder/HouseholderSequence.h" 25 | #include "src/Householder/BlockHouseholder.h" 26 | 27 | #include "src/Core/util/ReenableStupidWarnings.h" 28 | 29 | #endif // EIGEN_HOUSEHOLDER_MODULE_H 30 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 31 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/IterativeLinearSolvers: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_ITERATIVELINEARSOLVERS_MODULE_H 9 | #define EIGEN_ITERATIVELINEARSOLVERS_MODULE_H 10 | 11 | #include "SparseCore" 12 | #include "OrderingMethods" 13 | 14 | #include "src/Core/util/DisableStupidWarnings.h" 15 | 16 | /** 17 | * \defgroup IterativeLinearSolvers_Module IterativeLinearSolvers module 18 | * 19 | * This module currently provides iterative methods to solve problems of the form \c A \c x = \c b, where \c A is a squared matrix, usually very large and sparse. 20 | * Those solvers are accessible via the following classes: 21 | * - ConjugateGradient for selfadjoint (hermitian) matrices, 22 | * - LeastSquaresConjugateGradient for rectangular least-square problems, 23 | * - BiCGSTAB for general square matrices. 24 | * 25 | * These iterative solvers are associated with some preconditioners: 26 | * - IdentityPreconditioner - not really useful 27 | * - DiagonalPreconditioner - also called Jacobi preconditioner, work very well on diagonal dominant matrices. 28 | * - IncompleteLUT - incomplete LU factorization with dual thresholding 29 | * 30 | * Such problems can also be solved using the direct sparse decomposition modules: SparseCholesky, CholmodSupport, UmfPackSupport, SuperLUSupport. 31 | * 32 | \code 33 | #include 34 | \endcode 35 | */ 36 | 37 | #include "src/IterativeLinearSolvers/SolveWithGuess.h" 38 | #include "src/IterativeLinearSolvers/IterativeSolverBase.h" 39 | #include "src/IterativeLinearSolvers/BasicPreconditioners.h" 40 | #include "src/IterativeLinearSolvers/ConjugateGradient.h" 41 | #include "src/IterativeLinearSolvers/LeastSquareConjugateGradient.h" 42 | #include "src/IterativeLinearSolvers/BiCGSTAB.h" 43 | #include "src/IterativeLinearSolvers/IncompleteLUT.h" 44 | #include "src/IterativeLinearSolvers/IncompleteCholesky.h" 45 | 46 | #include "src/Core/util/ReenableStupidWarnings.h" 47 | 48 | #endif // EIGEN_ITERATIVELINEARSOLVERS_MODULE_H 49 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Jacobi: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_JACOBI_MODULE_H 9 | #define EIGEN_JACOBI_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | /** \defgroup Jacobi_Module Jacobi module 16 | * This module provides Jacobi and Givens rotations. 17 | * 18 | * \code 19 | * #include 20 | * \endcode 21 | * 22 | * In addition to listed classes, it defines the two following MatrixBase methods to apply a Jacobi or Givens rotation: 23 | * - MatrixBase::applyOnTheLeft() 24 | * - MatrixBase::applyOnTheRight(). 25 | */ 26 | 27 | #include "src/Jacobi/Jacobi.h" 28 | 29 | #include "src/Core/util/ReenableStupidWarnings.h" 30 | 31 | #endif // EIGEN_JACOBI_MODULE_H 32 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 33 | 34 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/LU: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_LU_MODULE_H 9 | #define EIGEN_LU_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | /** \defgroup LU_Module LU module 16 | * This module includes %LU decomposition and related notions such as matrix inversion and determinant. 17 | * This module defines the following MatrixBase methods: 18 | * - MatrixBase::inverse() 19 | * - MatrixBase::determinant() 20 | * 21 | * \code 22 | * #include 23 | * \endcode 24 | */ 25 | 26 | #include "src/misc/Kernel.h" 27 | #include "src/misc/Image.h" 28 | #include "src/LU/FullPivLU.h" 29 | #include "src/LU/PartialPivLU.h" 30 | #ifdef EIGEN_USE_LAPACKE 31 | #ifdef EIGEN_USE_MKL 32 | #include "mkl_lapacke.h" 33 | #else 34 | #include "src/misc/lapacke.h" 35 | #endif 36 | #include "src/LU/PartialPivLU_LAPACKE.h" 37 | #endif 38 | #include "src/LU/Determinant.h" 39 | #include "src/LU/InverseImpl.h" 40 | 41 | // Use the SSE optimized version whenever possible. At the moment the 42 | // SSE version doesn't compile when AVX is enabled 43 | #if defined EIGEN_VECTORIZE_SSE && !defined EIGEN_VECTORIZE_AVX 44 | #include "src/LU/arch/Inverse_SSE.h" 45 | #endif 46 | 47 | #include "src/Core/util/ReenableStupidWarnings.h" 48 | 49 | #endif // EIGEN_LU_MODULE_H 50 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 51 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/MetisSupport: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_METISSUPPORT_MODULE_H 9 | #define EIGEN_METISSUPPORT_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | extern "C" { 16 | #include 17 | } 18 | 19 | 20 | /** \ingroup Support_modules 21 | * \defgroup MetisSupport_Module MetisSupport module 22 | * 23 | * \code 24 | * #include 25 | * \endcode 26 | * This module defines an interface to the METIS reordering package (http://glaros.dtc.umn.edu/gkhome/views/metis). 27 | * It can be used just as any other built-in method as explained in \link OrderingMethods_Module here. \endlink 28 | */ 29 | 30 | 31 | #include "src/MetisSupport/MetisSupport.h" 32 | 33 | #include "src/Core/util/ReenableStupidWarnings.h" 34 | 35 | #endif // EIGEN_METISSUPPORT_MODULE_H 36 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/OrderingMethods: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_ORDERINGMETHODS_MODULE_H 9 | #define EIGEN_ORDERINGMETHODS_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | /** 16 | * \defgroup OrderingMethods_Module OrderingMethods module 17 | * 18 | * This module is currently for internal use only 19 | * 20 | * It defines various built-in and external ordering methods for sparse matrices. 21 | * They are typically used to reduce the number of elements during 22 | * the sparse matrix decomposition (LLT, LU, QR). 23 | * Precisely, in a preprocessing step, a permutation matrix P is computed using 24 | * those ordering methods and applied to the columns of the matrix. 25 | * Using for instance the sparse Cholesky decomposition, it is expected that 26 | * the nonzeros elements in LLT(A*P) will be much smaller than that in LLT(A). 27 | * 28 | * 29 | * Usage : 30 | * \code 31 | * #include 32 | * \endcode 33 | * 34 | * A simple usage is as a template parameter in the sparse decomposition classes : 35 | * 36 | * \code 37 | * SparseLU > solver; 38 | * \endcode 39 | * 40 | * \code 41 | * SparseQR > solver; 42 | * \endcode 43 | * 44 | * It is possible as well to call directly a particular ordering method for your own purpose, 45 | * \code 46 | * AMDOrdering ordering; 47 | * PermutationMatrix perm; 48 | * SparseMatrix A; 49 | * //Fill the matrix ... 50 | * 51 | * ordering(A, perm); // Call AMD 52 | * \endcode 53 | * 54 | * \note Some of these methods (like AMD or METIS), need the sparsity pattern 55 | * of the input matrix to be symmetric. When the matrix is structurally unsymmetric, 56 | * Eigen computes internally the pattern of \f$A^T*A\f$ before calling the method. 57 | * If your matrix is already symmetric (at leat in structure), you can avoid that 58 | * by calling the method with a SelfAdjointView type. 59 | * 60 | * \code 61 | * // Call the ordering on the pattern of the lower triangular matrix A 62 | * ordering(A.selfadjointView(), perm); 63 | * \endcode 64 | */ 65 | 66 | #ifndef EIGEN_MPL2_ONLY 67 | #include "src/OrderingMethods/Amd.h" 68 | #endif 69 | 70 | #include "src/OrderingMethods/Ordering.h" 71 | #include "src/Core/util/ReenableStupidWarnings.h" 72 | 73 | #endif // EIGEN_ORDERINGMETHODS_MODULE_H 74 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/PaStiXSupport: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_PASTIXSUPPORT_MODULE_H 9 | #define EIGEN_PASTIXSUPPORT_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | extern "C" { 16 | #include 17 | #include 18 | } 19 | 20 | #ifdef complex 21 | #undef complex 22 | #endif 23 | 24 | /** \ingroup Support_modules 25 | * \defgroup PaStiXSupport_Module PaStiXSupport module 26 | * 27 | * This module provides an interface to the PaSTiX library. 28 | * PaSTiX is a general \b supernodal, \b parallel and \b opensource sparse solver. 29 | * It provides the two following main factorization classes: 30 | * - class PastixLLT : a supernodal, parallel LLt Cholesky factorization. 31 | * - class PastixLDLT: a supernodal, parallel LDLt Cholesky factorization. 32 | * - class PastixLU : a supernodal, parallel LU factorization (optimized for a symmetric pattern). 33 | * 34 | * \code 35 | * #include 36 | * \endcode 37 | * 38 | * In order to use this module, the PaSTiX headers must be accessible from the include paths, and your binary must be linked to the PaSTiX library and its dependencies. 39 | * The dependencies depend on how PaSTiX has been compiled. 40 | * For a cmake based project, you can use our FindPaSTiX.cmake module to help you in this task. 41 | * 42 | */ 43 | 44 | #include "src/PaStiXSupport/PaStiXSupport.h" 45 | 46 | #include "src/Core/util/ReenableStupidWarnings.h" 47 | 48 | #endif // EIGEN_PASTIXSUPPORT_MODULE_H 49 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/PardisoSupport: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_PARDISOSUPPORT_MODULE_H 9 | #define EIGEN_PARDISOSUPPORT_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | #include 16 | 17 | /** \ingroup Support_modules 18 | * \defgroup PardisoSupport_Module PardisoSupport module 19 | * 20 | * This module brings support for the Intel(R) MKL PARDISO direct sparse solvers. 21 | * 22 | * \code 23 | * #include 24 | * \endcode 25 | * 26 | * In order to use this module, the MKL headers must be accessible from the include paths, and your binary must be linked to the MKL library and its dependencies. 27 | * See this \ref TopicUsingIntelMKL "page" for more information on MKL-Eigen integration. 28 | * 29 | */ 30 | 31 | #include "src/PardisoSupport/PardisoSupport.h" 32 | 33 | #include "src/Core/util/ReenableStupidWarnings.h" 34 | 35 | #endif // EIGEN_PARDISOSUPPORT_MODULE_H 36 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/QR: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_QR_MODULE_H 9 | #define EIGEN_QR_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | #include "Cholesky" 16 | #include "Jacobi" 17 | #include "Householder" 18 | 19 | /** \defgroup QR_Module QR module 20 | * 21 | * 22 | * 23 | * This module provides various QR decompositions 24 | * This module also provides some MatrixBase methods, including: 25 | * - MatrixBase::householderQr() 26 | * - MatrixBase::colPivHouseholderQr() 27 | * - MatrixBase::fullPivHouseholderQr() 28 | * 29 | * \code 30 | * #include 31 | * \endcode 32 | */ 33 | 34 | #include "src/QR/HouseholderQR.h" 35 | #include "src/QR/FullPivHouseholderQR.h" 36 | #include "src/QR/ColPivHouseholderQR.h" 37 | #include "src/QR/CompleteOrthogonalDecomposition.h" 38 | #ifdef EIGEN_USE_LAPACKE 39 | #ifdef EIGEN_USE_MKL 40 | #include "mkl_lapacke.h" 41 | #else 42 | #include "src/misc/lapacke.h" 43 | #endif 44 | #include "src/QR/HouseholderQR_LAPACKE.h" 45 | #include "src/QR/ColPivHouseholderQR_LAPACKE.h" 46 | #endif 47 | 48 | #include "src/Core/util/ReenableStupidWarnings.h" 49 | 50 | #endif // EIGEN_QR_MODULE_H 51 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 52 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/QtAlignedMalloc: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_QTMALLOC_MODULE_H 9 | #define EIGEN_QTMALLOC_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #if (!EIGEN_MALLOC_ALREADY_ALIGNED) 14 | 15 | #include "src/Core/util/DisableStupidWarnings.h" 16 | 17 | void *qMalloc(std::size_t size) 18 | { 19 | return Eigen::internal::aligned_malloc(size); 20 | } 21 | 22 | void qFree(void *ptr) 23 | { 24 | Eigen::internal::aligned_free(ptr); 25 | } 26 | 27 | void *qRealloc(void *ptr, std::size_t size) 28 | { 29 | void* newPtr = Eigen::internal::aligned_malloc(size); 30 | std::memcpy(newPtr, ptr, size); 31 | Eigen::internal::aligned_free(ptr); 32 | return newPtr; 33 | } 34 | 35 | #include "src/Core/util/ReenableStupidWarnings.h" 36 | 37 | #endif 38 | 39 | #endif // EIGEN_QTMALLOC_MODULE_H 40 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 41 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/SPQRSupport: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_SPQRSUPPORT_MODULE_H 9 | #define EIGEN_SPQRSUPPORT_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | #include "SuiteSparseQR.hpp" 16 | 17 | /** \ingroup Support_modules 18 | * \defgroup SPQRSupport_Module SuiteSparseQR module 19 | * 20 | * This module provides an interface to the SPQR library, which is part of the suitesparse package. 21 | * 22 | * \code 23 | * #include 24 | * \endcode 25 | * 26 | * In order to use this module, the SPQR headers must be accessible from the include paths, and your binary must be linked to the SPQR library and its dependencies (Cholmod, AMD, COLAMD,...). 27 | * For a cmake based project, you can use our FindSPQR.cmake and FindCholmod.Cmake modules 28 | * 29 | */ 30 | 31 | #include "src/CholmodSupport/CholmodSupport.h" 32 | #include "src/SPQRSupport/SuiteSparseQRSupport.h" 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/SVD: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_SVD_MODULE_H 9 | #define EIGEN_SVD_MODULE_H 10 | 11 | #include "QR" 12 | #include "Householder" 13 | #include "Jacobi" 14 | 15 | #include "src/Core/util/DisableStupidWarnings.h" 16 | 17 | /** \defgroup SVD_Module SVD module 18 | * 19 | * 20 | * 21 | * This module provides SVD decomposition for matrices (both real and complex). 22 | * Two decomposition algorithms are provided: 23 | * - JacobiSVD implementing two-sided Jacobi iterations is numerically very accurate, fast for small matrices, but very slow for larger ones. 24 | * - BDCSVD implementing a recursive divide & conquer strategy on top of an upper-bidiagonalization which remains fast for large problems. 25 | * These decompositions are accessible via the respective classes and following MatrixBase methods: 26 | * - MatrixBase::jacobiSvd() 27 | * - MatrixBase::bdcSvd() 28 | * 29 | * \code 30 | * #include 31 | * \endcode 32 | */ 33 | 34 | #include "src/misc/RealSvd2x2.h" 35 | #include "src/SVD/UpperBidiagonalization.h" 36 | #include "src/SVD/SVDBase.h" 37 | #include "src/SVD/JacobiSVD.h" 38 | #include "src/SVD/BDCSVD.h" 39 | #if defined(EIGEN_USE_LAPACKE) && !defined(EIGEN_USE_LAPACKE_STRICT) 40 | #ifdef EIGEN_USE_MKL 41 | #include "mkl_lapacke.h" 42 | #else 43 | #include "src/misc/lapacke.h" 44 | #endif 45 | #include "src/SVD/JacobiSVD_LAPACKE.h" 46 | #endif 47 | 48 | #include "src/Core/util/ReenableStupidWarnings.h" 49 | 50 | #endif // EIGEN_SVD_MODULE_H 51 | /* vim: set filetype=cpp et sw=2 ts=2 ai: */ 52 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/Sparse: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_SPARSE_MODULE_H 9 | #define EIGEN_SPARSE_MODULE_H 10 | 11 | /** \defgroup Sparse_Module Sparse meta-module 12 | * 13 | * Meta-module including all related modules: 14 | * - \ref SparseCore_Module 15 | * - \ref OrderingMethods_Module 16 | * - \ref SparseCholesky_Module 17 | * - \ref SparseLU_Module 18 | * - \ref SparseQR_Module 19 | * - \ref IterativeLinearSolvers_Module 20 | * 21 | \code 22 | #include 23 | \endcode 24 | */ 25 | 26 | #include "SparseCore" 27 | #include "OrderingMethods" 28 | #ifndef EIGEN_MPL2_ONLY 29 | #include "SparseCholesky" 30 | #endif 31 | #include "SparseLU" 32 | #include "SparseQR" 33 | #include "IterativeLinearSolvers" 34 | 35 | #endif // EIGEN_SPARSE_MODULE_H 36 | 37 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/SparseCholesky: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2008-2013 Gael Guennebaud 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_SPARSECHOLESKY_MODULE_H 11 | #define EIGEN_SPARSECHOLESKY_MODULE_H 12 | 13 | #include "SparseCore" 14 | #include "OrderingMethods" 15 | 16 | #include "src/Core/util/DisableStupidWarnings.h" 17 | 18 | /** 19 | * \defgroup SparseCholesky_Module SparseCholesky module 20 | * 21 | * This module currently provides two variants of the direct sparse Cholesky decomposition for selfadjoint (hermitian) matrices. 22 | * Those decompositions are accessible via the following classes: 23 | * - SimplicialLLt, 24 | * - SimplicialLDLt 25 | * 26 | * Such problems can also be solved using the ConjugateGradient solver from the IterativeLinearSolvers module. 27 | * 28 | * \code 29 | * #include 30 | * \endcode 31 | */ 32 | 33 | #ifdef EIGEN_MPL2_ONLY 34 | #error The SparseCholesky module has nothing to offer in MPL2 only mode 35 | #endif 36 | 37 | #include "src/SparseCholesky/SimplicialCholesky.h" 38 | 39 | #ifndef EIGEN_MPL2_ONLY 40 | #include "src/SparseCholesky/SimplicialCholesky_impl.h" 41 | #endif 42 | 43 | #include "src/Core/util/ReenableStupidWarnings.h" 44 | 45 | #endif // EIGEN_SPARSECHOLESKY_MODULE_H 46 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/SparseCore: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_SPARSECORE_MODULE_H 9 | #define EIGEN_SPARSECORE_MODULE_H 10 | 11 | #include "Core" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | /** 22 | * \defgroup SparseCore_Module SparseCore module 23 | * 24 | * This module provides a sparse matrix representation, and basic associated matrix manipulations 25 | * and operations. 26 | * 27 | * See the \ref TutorialSparse "Sparse tutorial" 28 | * 29 | * \code 30 | * #include 31 | * \endcode 32 | * 33 | * This module depends on: Core. 34 | */ 35 | 36 | #include "src/SparseCore/SparseUtil.h" 37 | #include "src/SparseCore/SparseMatrixBase.h" 38 | #include "src/SparseCore/SparseAssign.h" 39 | #include "src/SparseCore/CompressedStorage.h" 40 | #include "src/SparseCore/AmbiVector.h" 41 | #include "src/SparseCore/SparseCompressedBase.h" 42 | #include "src/SparseCore/SparseMatrix.h" 43 | #include "src/SparseCore/SparseMap.h" 44 | #include "src/SparseCore/MappedSparseMatrix.h" 45 | #include "src/SparseCore/SparseVector.h" 46 | #include "src/SparseCore/SparseRef.h" 47 | #include "src/SparseCore/SparseCwiseUnaryOp.h" 48 | #include "src/SparseCore/SparseCwiseBinaryOp.h" 49 | #include "src/SparseCore/SparseTranspose.h" 50 | #include "src/SparseCore/SparseBlock.h" 51 | #include "src/SparseCore/SparseDot.h" 52 | #include "src/SparseCore/SparseRedux.h" 53 | #include "src/SparseCore/SparseView.h" 54 | #include "src/SparseCore/SparseDiagonalProduct.h" 55 | #include "src/SparseCore/ConservativeSparseSparseProduct.h" 56 | #include "src/SparseCore/SparseSparseProductWithPruning.h" 57 | #include "src/SparseCore/SparseProduct.h" 58 | #include "src/SparseCore/SparseDenseProduct.h" 59 | #include "src/SparseCore/SparseSelfAdjointView.h" 60 | #include "src/SparseCore/SparseTriangularView.h" 61 | #include "src/SparseCore/TriangularSolver.h" 62 | #include "src/SparseCore/SparsePermutation.h" 63 | #include "src/SparseCore/SparseFuzzy.h" 64 | #include "src/SparseCore/SparseSolverBase.h" 65 | 66 | #include "src/Core/util/ReenableStupidWarnings.h" 67 | 68 | #endif // EIGEN_SPARSECORE_MODULE_H 69 | 70 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/SparseLU: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2012 Désiré Nuentsa-Wakam 5 | // Copyright (C) 2012 Gael Guennebaud 6 | // 7 | // This Source Code Form is subject to the terms of the Mozilla 8 | // Public License v. 2.0. If a copy of the MPL was not distributed 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 | 11 | #ifndef EIGEN_SPARSELU_MODULE_H 12 | #define EIGEN_SPARSELU_MODULE_H 13 | 14 | #include "SparseCore" 15 | 16 | /** 17 | * \defgroup SparseLU_Module SparseLU module 18 | * This module defines a supernodal factorization of general sparse matrices. 19 | * The code is fully optimized for supernode-panel updates with specialized kernels. 20 | * Please, see the documentation of the SparseLU class for more details. 21 | */ 22 | 23 | // Ordering interface 24 | #include "OrderingMethods" 25 | 26 | #include "src/SparseLU/SparseLU_gemm_kernel.h" 27 | 28 | #include "src/SparseLU/SparseLU_Structs.h" 29 | #include "src/SparseLU/SparseLU_SupernodalMatrix.h" 30 | #include "src/SparseLU/SparseLUImpl.h" 31 | #include "src/SparseCore/SparseColEtree.h" 32 | #include "src/SparseLU/SparseLU_Memory.h" 33 | #include "src/SparseLU/SparseLU_heap_relax_snode.h" 34 | #include "src/SparseLU/SparseLU_relax_snode.h" 35 | #include "src/SparseLU/SparseLU_pivotL.h" 36 | #include "src/SparseLU/SparseLU_panel_dfs.h" 37 | #include "src/SparseLU/SparseLU_kernel_bmod.h" 38 | #include "src/SparseLU/SparseLU_panel_bmod.h" 39 | #include "src/SparseLU/SparseLU_column_dfs.h" 40 | #include "src/SparseLU/SparseLU_column_bmod.h" 41 | #include "src/SparseLU/SparseLU_copy_to_ucol.h" 42 | #include "src/SparseLU/SparseLU_pruneL.h" 43 | #include "src/SparseLU/SparseLU_Utils.h" 44 | #include "src/SparseLU/SparseLU.h" 45 | 46 | #endif // EIGEN_SPARSELU_MODULE_H 47 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/SparseQR: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_SPARSEQR_MODULE_H 9 | #define EIGEN_SPARSEQR_MODULE_H 10 | 11 | #include "SparseCore" 12 | #include "OrderingMethods" 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | /** \defgroup SparseQR_Module SparseQR module 16 | * \brief Provides QR decomposition for sparse matrices 17 | * 18 | * This module provides a simplicial version of the left-looking Sparse QR decomposition. 19 | * The columns of the input matrix should be reordered to limit the fill-in during the 20 | * decomposition. Built-in methods (COLAMD, AMD) or external methods (METIS) can be used to this end. 21 | * See the \link OrderingMethods_Module OrderingMethods\endlink module for the list 22 | * of built-in and external ordering methods. 23 | * 24 | * \code 25 | * #include 26 | * \endcode 27 | * 28 | * 29 | */ 30 | 31 | #include "OrderingMethods" 32 | #include "src/SparseCore/SparseColEtree.h" 33 | #include "src/SparseQR/SparseQR.h" 34 | 35 | #include "src/Core/util/ReenableStupidWarnings.h" 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/StdDeque: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2009 Gael Guennebaud 5 | // Copyright (C) 2009 Hauke Heibel 6 | // 7 | // This Source Code Form is subject to the terms of the Mozilla 8 | // Public License v. 2.0. If a copy of the MPL was not distributed 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 | 11 | #ifndef EIGEN_STDDEQUE_MODULE_H 12 | #define EIGEN_STDDEQUE_MODULE_H 13 | 14 | #include "Core" 15 | #include 16 | 17 | #if EIGEN_COMP_MSVC && EIGEN_OS_WIN64 && (EIGEN_MAX_STATIC_ALIGN_BYTES<=16) /* MSVC auto aligns up to 16 bytes in 64 bit builds */ 18 | 19 | #define EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(...) 20 | 21 | #else 22 | 23 | #include "src/StlSupport/StdDeque.h" 24 | 25 | #endif 26 | 27 | #endif // EIGEN_STDDEQUE_MODULE_H 28 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/StdList: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2009 Hauke Heibel 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_STDLIST_MODULE_H 11 | #define EIGEN_STDLIST_MODULE_H 12 | 13 | #include "Core" 14 | #include 15 | 16 | #if EIGEN_COMP_MSVC && EIGEN_OS_WIN64 && (EIGEN_MAX_STATIC_ALIGN_BYTES<=16) /* MSVC auto aligns up to 16 bytes in 64 bit builds */ 17 | 18 | #define EIGEN_DEFINE_STL_LIST_SPECIALIZATION(...) 19 | 20 | #else 21 | 22 | #include "src/StlSupport/StdList.h" 23 | 24 | #endif 25 | 26 | #endif // EIGEN_STDLIST_MODULE_H 27 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/StdVector: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2009 Gael Guennebaud 5 | // Copyright (C) 2009 Hauke Heibel 6 | // 7 | // This Source Code Form is subject to the terms of the Mozilla 8 | // Public License v. 2.0. If a copy of the MPL was not distributed 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 | 11 | #ifndef EIGEN_STDVECTOR_MODULE_H 12 | #define EIGEN_STDVECTOR_MODULE_H 13 | 14 | #include "Core" 15 | #include 16 | 17 | #if EIGEN_COMP_MSVC && EIGEN_OS_WIN64 && (EIGEN_MAX_STATIC_ALIGN_BYTES<=16) /* MSVC auto aligns up to 16 bytes in 64 bit builds */ 18 | 19 | #define EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(...) 20 | 21 | #else 22 | 23 | #include "src/StlSupport/StdVector.h" 24 | 25 | #endif 26 | 27 | #endif // EIGEN_STDVECTOR_MODULE_H 28 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/SuperLUSupport: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_SUPERLUSUPPORT_MODULE_H 9 | #define EIGEN_SUPERLUSUPPORT_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | #ifdef EMPTY 16 | #define EIGEN_EMPTY_WAS_ALREADY_DEFINED 17 | #endif 18 | 19 | typedef int int_t; 20 | #include 21 | #include 22 | #include 23 | 24 | // slu_util.h defines a preprocessor token named EMPTY which is really polluting, 25 | // so we remove it in favor of a SUPERLU_EMPTY token. 26 | // If EMPTY was already defined then we don't undef it. 27 | 28 | #if defined(EIGEN_EMPTY_WAS_ALREADY_DEFINED) 29 | # undef EIGEN_EMPTY_WAS_ALREADY_DEFINED 30 | #elif defined(EMPTY) 31 | # undef EMPTY 32 | #endif 33 | 34 | #define SUPERLU_EMPTY (-1) 35 | 36 | namespace Eigen { struct SluMatrix; } 37 | 38 | /** \ingroup Support_modules 39 | * \defgroup SuperLUSupport_Module SuperLUSupport module 40 | * 41 | * This module provides an interface to the SuperLU library. 42 | * It provides the following factorization class: 43 | * - class SuperLU: a supernodal sequential LU factorization. 44 | * - class SuperILU: a supernodal sequential incomplete LU factorization (to be used as a preconditioner for iterative methods). 45 | * 46 | * \warning This wrapper requires at least versions 4.0 of SuperLU. The 3.x versions are not supported. 47 | * 48 | * \warning When including this module, you have to use SUPERLU_EMPTY instead of EMPTY which is no longer defined because it is too polluting. 49 | * 50 | * \code 51 | * #include 52 | * \endcode 53 | * 54 | * In order to use this module, the superlu headers must be accessible from the include paths, and your binary must be linked to the superlu library and its dependencies. 55 | * The dependencies depend on how superlu has been compiled. 56 | * For a cmake based project, you can use our FindSuperLU.cmake module to help you in this task. 57 | * 58 | */ 59 | 60 | #include "src/SuperLUSupport/SuperLUSupport.h" 61 | 62 | #include "src/Core/util/ReenableStupidWarnings.h" 63 | 64 | #endif // EIGEN_SUPERLUSUPPORT_MODULE_H 65 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/UmfPackSupport: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // This Source Code Form is subject to the terms of the Mozilla 5 | // Public License v. 2.0. If a copy of the MPL was not distributed 6 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | #ifndef EIGEN_UMFPACKSUPPORT_MODULE_H 9 | #define EIGEN_UMFPACKSUPPORT_MODULE_H 10 | 11 | #include "SparseCore" 12 | 13 | #include "src/Core/util/DisableStupidWarnings.h" 14 | 15 | extern "C" { 16 | #include 17 | } 18 | 19 | /** \ingroup Support_modules 20 | * \defgroup UmfPackSupport_Module UmfPackSupport module 21 | * 22 | * This module provides an interface to the UmfPack library which is part of the suitesparse package. 23 | * It provides the following factorization class: 24 | * - class UmfPackLU: a multifrontal sequential LU factorization. 25 | * 26 | * \code 27 | * #include 28 | * \endcode 29 | * 30 | * In order to use this module, the umfpack headers must be accessible from the include paths, and your binary must be linked to the umfpack library and its dependencies. 31 | * The dependencies depend on how umfpack has been compiled. 32 | * For a cmake based project, you can use our FindUmfPack.cmake module to help you in this task. 33 | * 34 | */ 35 | 36 | #include "src/UmfPackSupport/UmfPackSupport.h" 37 | 38 | #include "src/Core/util/ReenableStupidWarnings.h" 39 | 40 | #endif // EIGEN_UMFPACKSUPPORT_MODULE_H 41 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/Assign.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2007 Michael Olbrich 5 | // Copyright (C) 2006-2010 Benoit Jacob 6 | // Copyright (C) 2008 Gael Guennebaud 7 | // 8 | // This Source Code Form is subject to the terms of the Mozilla 9 | // Public License v. 2.0. If a copy of the MPL was not distributed 10 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 11 | 12 | #ifndef EIGEN_ASSIGN_H 13 | #define EIGEN_ASSIGN_H 14 | 15 | namespace Eigen { 16 | 17 | template 18 | template 19 | EIGEN_STRONG_INLINE Derived& DenseBase 20 | ::lazyAssign(const DenseBase& other) 21 | { 22 | enum{ 23 | SameType = internal::is_same::value 24 | }; 25 | 26 | EIGEN_STATIC_ASSERT_LVALUE(Derived) 27 | EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Derived,OtherDerived) 28 | EIGEN_STATIC_ASSERT(SameType,YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY) 29 | 30 | eigen_assert(rows() == other.rows() && cols() == other.cols()); 31 | internal::call_assignment_no_alias(derived(),other.derived()); 32 | 33 | return derived(); 34 | } 35 | 36 | template 37 | template 38 | EIGEN_DEVICE_FUNC 39 | EIGEN_STRONG_INLINE Derived& DenseBase::operator=(const DenseBase& other) 40 | { 41 | internal::call_assignment(derived(), other.derived()); 42 | return derived(); 43 | } 44 | 45 | template 46 | EIGEN_DEVICE_FUNC 47 | EIGEN_STRONG_INLINE Derived& DenseBase::operator=(const DenseBase& other) 48 | { 49 | internal::call_assignment(derived(), other.derived()); 50 | return derived(); 51 | } 52 | 53 | template 54 | EIGEN_DEVICE_FUNC 55 | EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const MatrixBase& other) 56 | { 57 | internal::call_assignment(derived(), other.derived()); 58 | return derived(); 59 | } 60 | 61 | template 62 | template 63 | EIGEN_DEVICE_FUNC 64 | EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const DenseBase& other) 65 | { 66 | internal::call_assignment(derived(), other.derived()); 67 | return derived(); 68 | } 69 | 70 | template 71 | template 72 | EIGEN_DEVICE_FUNC 73 | EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const EigenBase& other) 74 | { 75 | internal::call_assignment(derived(), other.derived()); 76 | return derived(); 77 | } 78 | 79 | template 80 | template 81 | EIGEN_DEVICE_FUNC 82 | EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const ReturnByValue& other) 83 | { 84 | other.derived().evalTo(derived()); 85 | return derived(); 86 | } 87 | 88 | } // end namespace Eigen 89 | 90 | #endif // EIGEN_ASSIGN_H 91 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/DiagonalProduct.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2008 Gael Guennebaud 5 | // Copyright (C) 2007-2009 Benoit Jacob 6 | // 7 | // This Source Code Form is subject to the terms of the Mozilla 8 | // Public License v. 2.0. If a copy of the MPL was not distributed 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 | 11 | #ifndef EIGEN_DIAGONALPRODUCT_H 12 | #define EIGEN_DIAGONALPRODUCT_H 13 | 14 | namespace Eigen { 15 | 16 | /** \returns the diagonal matrix product of \c *this by the diagonal matrix \a diagonal. 17 | */ 18 | template 19 | template 20 | inline const Product 21 | MatrixBase::operator*(const DiagonalBase &a_diagonal) const 22 | { 23 | return Product(derived(),a_diagonal.derived()); 24 | } 25 | 26 | } // end namespace Eigen 27 | 28 | #endif // EIGEN_DIAGONALPRODUCT_H 29 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/SelfCwiseBinaryOp.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2009-2010 Gael Guennebaud 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_SELFCWISEBINARYOP_H 11 | #define EIGEN_SELFCWISEBINARYOP_H 12 | 13 | namespace Eigen { 14 | 15 | // TODO generalize the scalar type of 'other' 16 | 17 | template 18 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase::operator*=(const Scalar& other) 19 | { 20 | internal::call_assignment(this->derived(), PlainObject::Constant(rows(),cols(),other), internal::mul_assign_op()); 21 | return derived(); 22 | } 23 | 24 | template 25 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& ArrayBase::operator+=(const Scalar& other) 26 | { 27 | internal::call_assignment(this->derived(), PlainObject::Constant(rows(),cols(),other), internal::add_assign_op()); 28 | return derived(); 29 | } 30 | 31 | template 32 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& ArrayBase::operator-=(const Scalar& other) 33 | { 34 | internal::call_assignment(this->derived(), PlainObject::Constant(rows(),cols(),other), internal::sub_assign_op()); 35 | return derived(); 36 | } 37 | 38 | template 39 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase::operator/=(const Scalar& other) 40 | { 41 | internal::call_assignment(this->derived(), PlainObject::Constant(rows(),cols(),other), internal::div_assign_op()); 42 | return derived(); 43 | } 44 | 45 | } // end namespace Eigen 46 | 47 | #endif // EIGEN_SELFCWISEBINARYOP_H 48 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/Swap.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2006-2008 Benoit Jacob 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_SWAP_H 11 | #define EIGEN_SWAP_H 12 | 13 | namespace Eigen { 14 | 15 | namespace internal { 16 | 17 | // Overload default assignPacket behavior for swapping them 18 | template 19 | class generic_dense_assignment_kernel, Specialized> 20 | : public generic_dense_assignment_kernel, BuiltIn> 21 | { 22 | protected: 23 | typedef generic_dense_assignment_kernel, BuiltIn> Base; 24 | using Base::m_dst; 25 | using Base::m_src; 26 | using Base::m_functor; 27 | 28 | public: 29 | typedef typename Base::Scalar Scalar; 30 | typedef typename Base::DstXprType DstXprType; 31 | typedef swap_assign_op Functor; 32 | 33 | EIGEN_DEVICE_FUNC generic_dense_assignment_kernel(DstEvaluatorTypeT &dst, const SrcEvaluatorTypeT &src, const Functor &func, DstXprType& dstExpr) 34 | : Base(dst, src, func, dstExpr) 35 | {} 36 | 37 | template 38 | void assignPacket(Index row, Index col) 39 | { 40 | PacketType tmp = m_src.template packet(row,col); 41 | const_cast(m_src).template writePacket(row,col, m_dst.template packet(row,col)); 42 | m_dst.template writePacket(row,col,tmp); 43 | } 44 | 45 | template 46 | void assignPacket(Index index) 47 | { 48 | PacketType tmp = m_src.template packet(index); 49 | const_cast(m_src).template writePacket(index, m_dst.template packet(index)); 50 | m_dst.template writePacket(index,tmp); 51 | } 52 | 53 | // TODO find a simple way not to have to copy/paste this function from generic_dense_assignment_kernel, by simple I mean no CRTP (Gael) 54 | template 55 | void assignPacketByOuterInner(Index outer, Index inner) 56 | { 57 | Index row = Base::rowIndexByOuterInner(outer, inner); 58 | Index col = Base::colIndexByOuterInner(outer, inner); 59 | assignPacket(row, col); 60 | } 61 | }; 62 | 63 | } // namespace internal 64 | 65 | } // end namespace Eigen 66 | 67 | #endif // EIGEN_SWAP_H 68 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/arch/AVX/TypeCasting.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2015 Benoit Steiner 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_TYPE_CASTING_AVX_H 11 | #define EIGEN_TYPE_CASTING_AVX_H 12 | 13 | namespace Eigen { 14 | 15 | namespace internal { 16 | 17 | // For now we use SSE to handle integers, so we can't use AVX instructions to cast 18 | // from int to float 19 | template <> 20 | struct type_casting_traits { 21 | enum { 22 | VectorizedCast = 0, 23 | SrcCoeffRatio = 1, 24 | TgtCoeffRatio = 1 25 | }; 26 | }; 27 | 28 | template <> 29 | struct type_casting_traits { 30 | enum { 31 | VectorizedCast = 0, 32 | SrcCoeffRatio = 1, 33 | TgtCoeffRatio = 1 34 | }; 35 | }; 36 | 37 | 38 | 39 | template<> EIGEN_STRONG_INLINE Packet8i pcast(const Packet8f& a) { 40 | return _mm256_cvtps_epi32(a); 41 | } 42 | 43 | template<> EIGEN_STRONG_INLINE Packet8f pcast(const Packet8i& a) { 44 | return _mm256_cvtepi32_ps(a); 45 | } 46 | 47 | } // end namespace internal 48 | 49 | } // end namespace Eigen 50 | 51 | #endif // EIGEN_TYPE_CASTING_AVX_H 52 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/arch/CUDA/MathFunctions.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2014 Benoit Steiner 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_MATH_FUNCTIONS_CUDA_H 11 | #define EIGEN_MATH_FUNCTIONS_CUDA_H 12 | 13 | namespace Eigen { 14 | 15 | namespace internal { 16 | 17 | // Make sure this is only available when targeting a GPU: we don't want to 18 | // introduce conflicts between these packet_traits definitions and the ones 19 | // we'll use on the host side (SSE, AVX, ...) 20 | #if defined(__CUDACC__) && defined(EIGEN_USE_GPU) 21 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 22 | float4 plog(const float4& a) 23 | { 24 | return make_float4(logf(a.x), logf(a.y), logf(a.z), logf(a.w)); 25 | } 26 | 27 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 28 | double2 plog(const double2& a) 29 | { 30 | using ::log; 31 | return make_double2(log(a.x), log(a.y)); 32 | } 33 | 34 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 35 | float4 plog1p(const float4& a) 36 | { 37 | return make_float4(log1pf(a.x), log1pf(a.y), log1pf(a.z), log1pf(a.w)); 38 | } 39 | 40 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 41 | double2 plog1p(const double2& a) 42 | { 43 | return make_double2(log1p(a.x), log1p(a.y)); 44 | } 45 | 46 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 47 | float4 pexp(const float4& a) 48 | { 49 | return make_float4(expf(a.x), expf(a.y), expf(a.z), expf(a.w)); 50 | } 51 | 52 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 53 | double2 pexp(const double2& a) 54 | { 55 | using ::exp; 56 | return make_double2(exp(a.x), exp(a.y)); 57 | } 58 | 59 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 60 | float4 psqrt(const float4& a) 61 | { 62 | return make_float4(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z), sqrtf(a.w)); 63 | } 64 | 65 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 66 | double2 psqrt(const double2& a) 67 | { 68 | using ::sqrt; 69 | return make_double2(sqrt(a.x), sqrt(a.y)); 70 | } 71 | 72 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 73 | float4 prsqrt(const float4& a) 74 | { 75 | return make_float4(rsqrtf(a.x), rsqrtf(a.y), rsqrtf(a.z), rsqrtf(a.w)); 76 | } 77 | 78 | template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 79 | double2 prsqrt(const double2& a) 80 | { 81 | return make_double2(rsqrt(a.x), rsqrt(a.y)); 82 | } 83 | 84 | 85 | #endif 86 | 87 | } // end namespace internal 88 | 89 | } // end namespace Eigen 90 | 91 | #endif // EIGEN_MATH_FUNCTIONS_CUDA_H 92 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/arch/Default/ConjHelper.h: -------------------------------------------------------------------------------- 1 | 2 | // This file is part of Eigen, a lightweight C++ template library 3 | // for linear algebra. 4 | // 5 | // Copyright (C) 2017 Gael Guennebaud 6 | // 7 | // This Source Code Form is subject to the terms of the Mozilla 8 | // Public License v. 2.0. If a copy of the MPL was not distributed 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 | 11 | #ifndef EIGEN_ARCH_CONJ_HELPER_H 12 | #define EIGEN_ARCH_CONJ_HELPER_H 13 | 14 | #define EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(PACKET_CPLX, PACKET_REAL) \ 15 | template<> struct conj_helper { \ 16 | EIGEN_STRONG_INLINE PACKET_CPLX pmadd(const PACKET_REAL& x, const PACKET_CPLX& y, const PACKET_CPLX& c) const \ 17 | { return padd(c, pmul(x,y)); } \ 18 | EIGEN_STRONG_INLINE PACKET_CPLX pmul(const PACKET_REAL& x, const PACKET_CPLX& y) const \ 19 | { return PACKET_CPLX(Eigen::internal::pmul(x, y.v)); } \ 20 | }; \ 21 | \ 22 | template<> struct conj_helper { \ 23 | EIGEN_STRONG_INLINE PACKET_CPLX pmadd(const PACKET_CPLX& x, const PACKET_REAL& y, const PACKET_CPLX& c) const \ 24 | { return padd(c, pmul(x,y)); } \ 25 | EIGEN_STRONG_INLINE PACKET_CPLX pmul(const PACKET_CPLX& x, const PACKET_REAL& y) const \ 26 | { return PACKET_CPLX(Eigen::internal::pmul(x.v, y)); } \ 27 | }; 28 | 29 | #endif // EIGEN_ARCH_CONJ_HELPER_H 30 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/arch/Default/Settings.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2008-2010 Gael Guennebaud 5 | // Copyright (C) 2006-2008 Benoit Jacob 6 | // 7 | // This Source Code Form is subject to the terms of the Mozilla 8 | // Public License v. 2.0. If a copy of the MPL was not distributed 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 | 11 | 12 | /* All the parameters defined in this file can be specialized in the 13 | * architecture specific files, and/or by the user. 14 | * More to come... */ 15 | 16 | #ifndef EIGEN_DEFAULT_SETTINGS_H 17 | #define EIGEN_DEFAULT_SETTINGS_H 18 | 19 | /** Defines the maximal loop size to enable meta unrolling of loops. 20 | * Note that the value here is expressed in Eigen's own notion of "number of FLOPS", 21 | * it does not correspond to the number of iterations or the number of instructions 22 | */ 23 | #ifndef EIGEN_UNROLLING_LIMIT 24 | #define EIGEN_UNROLLING_LIMIT 100 25 | #endif 26 | 27 | /** Defines the threshold between a "small" and a "large" matrix. 28 | * This threshold is mainly used to select the proper product implementation. 29 | */ 30 | #ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 31 | #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8 32 | #endif 33 | 34 | /** Defines the maximal width of the blocks used in the triangular product and solver 35 | * for vectors (level 2 blas xTRMV and xTRSV). The default is 8. 36 | */ 37 | #ifndef EIGEN_TUNE_TRIANGULAR_PANEL_WIDTH 38 | #define EIGEN_TUNE_TRIANGULAR_PANEL_WIDTH 8 39 | #endif 40 | 41 | 42 | /** Defines the default number of registers available for that architecture. 43 | * Currently it must be 8 or 16. Other values will fail. 44 | */ 45 | #ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS 46 | #define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS 8 47 | #endif 48 | 49 | #endif // EIGEN_DEFAULT_SETTINGS_H 50 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/arch/SSE/TypeCasting.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2015 Benoit Steiner 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_TYPE_CASTING_SSE_H 11 | #define EIGEN_TYPE_CASTING_SSE_H 12 | 13 | namespace Eigen { 14 | 15 | namespace internal { 16 | 17 | #ifndef EIGEN_VECTORIZE_AVX 18 | template <> 19 | struct type_casting_traits { 20 | enum { 21 | VectorizedCast = 1, 22 | SrcCoeffRatio = 1, 23 | TgtCoeffRatio = 1 24 | }; 25 | }; 26 | 27 | template <> 28 | struct type_casting_traits { 29 | enum { 30 | VectorizedCast = 1, 31 | SrcCoeffRatio = 1, 32 | TgtCoeffRatio = 1 33 | }; 34 | }; 35 | 36 | template <> 37 | struct type_casting_traits { 38 | enum { 39 | VectorizedCast = 1, 40 | SrcCoeffRatio = 2, 41 | TgtCoeffRatio = 1 42 | }; 43 | }; 44 | 45 | template <> 46 | struct type_casting_traits { 47 | enum { 48 | VectorizedCast = 1, 49 | SrcCoeffRatio = 1, 50 | TgtCoeffRatio = 2 51 | }; 52 | }; 53 | #endif 54 | 55 | template<> EIGEN_STRONG_INLINE Packet4i pcast(const Packet4f& a) { 56 | return _mm_cvttps_epi32(a); 57 | } 58 | 59 | template<> EIGEN_STRONG_INLINE Packet4f pcast(const Packet4i& a) { 60 | return _mm_cvtepi32_ps(a); 61 | } 62 | 63 | template<> EIGEN_STRONG_INLINE Packet4f pcast(const Packet2d& a, const Packet2d& b) { 64 | return _mm_shuffle_ps(_mm_cvtpd_ps(a), _mm_cvtpd_ps(b), (1 << 2) | (1 << 6)); 65 | } 66 | 67 | template<> EIGEN_STRONG_INLINE Packet2d pcast(const Packet4f& a) { 68 | // Simply discard the second half of the input 69 | return _mm_cvtps_pd(a); 70 | } 71 | 72 | 73 | } // end namespace internal 74 | 75 | } // end namespace Eigen 76 | 77 | #endif // EIGEN_TYPE_CASTING_SSE_H 78 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/functors/TernaryFunctors.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2016 Eugene Brevdo 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_TERNARY_FUNCTORS_H 11 | #define EIGEN_TERNARY_FUNCTORS_H 12 | 13 | namespace Eigen { 14 | 15 | namespace internal { 16 | 17 | //---------- associative ternary functors ---------- 18 | 19 | 20 | 21 | } // end namespace internal 22 | 23 | } // end namespace Eigen 24 | 25 | #endif // EIGEN_TERNARY_FUNCTORS_H 26 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/util/NonMPL2.h: -------------------------------------------------------------------------------- 1 | #ifdef EIGEN_MPL2_ONLY 2 | #error Including non-MPL2 code in EIGEN_MPL2_ONLY mode 3 | #endif 4 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/Core/util/ReenableStupidWarnings.h: -------------------------------------------------------------------------------- 1 | #ifdef EIGEN_WARNINGS_DISABLED 2 | #undef EIGEN_WARNINGS_DISABLED 3 | 4 | #ifndef EIGEN_PERMANENTLY_DISABLE_STUPID_WARNINGS 5 | #ifdef _MSC_VER 6 | #pragma warning( pop ) 7 | #elif defined __INTEL_COMPILER 8 | #pragma warning pop 9 | #elif defined __clang__ 10 | #pragma clang diagnostic pop 11 | #elif defined __GNUC__ && __GNUC__>=6 12 | #pragma GCC diagnostic pop 13 | #endif 14 | 15 | #if defined __NVCC__ 16 | // Don't reenable the diagnostic messages, as it turns out these messages need 17 | // to be disabled at the point of the template instantiation (i.e the user code) 18 | // otherwise they'll be triggered by nvcc. 19 | // #pragma diag_default code_is_unreachable 20 | // #pragma diag_default initialization_not_reachable 21 | // #pragma diag_default 2651 22 | // #pragma diag_default 2653 23 | #endif 24 | 25 | #endif 26 | 27 | #endif // EIGEN_WARNINGS_DISABLED 28 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/SparseCore/MappedSparseMatrix.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2008-2014 Gael Guennebaud 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_MAPPED_SPARSEMATRIX_H 11 | #define EIGEN_MAPPED_SPARSEMATRIX_H 12 | 13 | namespace Eigen { 14 | 15 | /** \deprecated Use Map > 16 | * \class MappedSparseMatrix 17 | * 18 | * \brief Sparse matrix 19 | * 20 | * \param _Scalar the scalar type, i.e. the type of the coefficients 21 | * 22 | * See http://www.netlib.org/linalg/html_templates/node91.html for details on the storage scheme. 23 | * 24 | */ 25 | namespace internal { 26 | template 27 | struct traits > : traits > 28 | {}; 29 | } // end namespace internal 30 | 31 | template 32 | class MappedSparseMatrix 33 | : public Map > 34 | { 35 | typedef Map > Base; 36 | 37 | public: 38 | 39 | typedef typename Base::StorageIndex StorageIndex; 40 | typedef typename Base::Scalar Scalar; 41 | 42 | inline MappedSparseMatrix(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr, Scalar* valuePtr, StorageIndex* innerNonZeroPtr = 0) 43 | : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZeroPtr) 44 | {} 45 | 46 | /** Empty destructor */ 47 | inline ~MappedSparseMatrix() {} 48 | }; 49 | 50 | namespace internal { 51 | 52 | template 53 | struct evaluator > 54 | : evaluator > > 55 | { 56 | typedef MappedSparseMatrix<_Scalar,_Options,_StorageIndex> XprType; 57 | typedef evaluator > Base; 58 | 59 | evaluator() : Base() {} 60 | explicit evaluator(const XprType &mat) : Base(mat) {} 61 | }; 62 | 63 | } 64 | 65 | } // end namespace Eigen 66 | 67 | #endif // EIGEN_MAPPED_SPARSEMATRIX_H 68 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/SparseCore/SparseFuzzy.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2008-2014 Gael Guennebaud 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_SPARSE_FUZZY_H 11 | #define EIGEN_SPARSE_FUZZY_H 12 | 13 | namespace Eigen { 14 | 15 | template 16 | template 17 | bool SparseMatrixBase::isApprox(const SparseMatrixBase& other, const RealScalar &prec) const 18 | { 19 | const typename internal::nested_eval::type actualA(derived()); 20 | typename internal::conditional::type, 22 | const PlainObject>::type actualB(other.derived()); 23 | 24 | return (actualA - actualB).squaredNorm() <= prec * prec * numext::mini(actualA.squaredNorm(), actualB.squaredNorm()); 25 | } 26 | 27 | } // end namespace Eigen 28 | 29 | #endif // EIGEN_SPARSE_FUZZY_H 30 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/SparseCore/SparseRedux.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2008-2014 Gael Guennebaud 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_SPARSEREDUX_H 11 | #define EIGEN_SPARSEREDUX_H 12 | 13 | namespace Eigen { 14 | 15 | template 16 | typename internal::traits::Scalar 17 | SparseMatrixBase::sum() const 18 | { 19 | eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix"); 20 | Scalar res(0); 21 | internal::evaluator thisEval(derived()); 22 | for (Index j=0; j::InnerIterator iter(thisEval,j); iter; ++iter) 24 | res += iter.value(); 25 | return res; 26 | } 27 | 28 | template 29 | typename internal::traits >::Scalar 30 | SparseMatrix<_Scalar,_Options,_Index>::sum() const 31 | { 32 | eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix"); 33 | if(this->isCompressed()) 34 | return Matrix::Map(m_data.valuePtr(), m_data.size()).sum(); 35 | else 36 | return Base::sum(); 37 | } 38 | 39 | template 40 | typename internal::traits >::Scalar 41 | SparseVector<_Scalar,_Options,_Index>::sum() const 42 | { 43 | eigen_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix"); 44 | return Matrix::Map(m_data.valuePtr(), m_data.size()).sum(); 45 | } 46 | 47 | } // end namespace Eigen 48 | 49 | #endif // EIGEN_SPARSEREDUX_H 50 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/SparseLU/SparseLU_Utils.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2012 Désiré Nuentsa-Wakam 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | 11 | #ifndef EIGEN_SPARSELU_UTILS_H 12 | #define EIGEN_SPARSELU_UTILS_H 13 | 14 | namespace Eigen { 15 | namespace internal { 16 | 17 | /** 18 | * \brief Count Nonzero elements in the factors 19 | */ 20 | template 21 | void SparseLUImpl::countnz(const Index n, Index& nnzL, Index& nnzU, GlobalLU_t& glu) 22 | { 23 | nnzL = 0; 24 | nnzU = (glu.xusub)(n); 25 | Index nsuper = (glu.supno)(n); 26 | Index jlen; 27 | Index i, j, fsupc; 28 | if (n <= 0 ) return; 29 | // For each supernode 30 | for (i = 0; i <= nsuper; i++) 31 | { 32 | fsupc = glu.xsup(i); 33 | jlen = glu.xlsub(fsupc+1) - glu.xlsub(fsupc); 34 | 35 | for (j = fsupc; j < glu.xsup(i+1); j++) 36 | { 37 | nnzL += jlen; 38 | nnzU += j - fsupc + 1; 39 | jlen--; 40 | } 41 | } 42 | } 43 | 44 | /** 45 | * \brief Fix up the data storage lsub for L-subscripts. 46 | * 47 | * It removes the subscripts sets for structural pruning, 48 | * and applies permutation to the remaining subscripts 49 | * 50 | */ 51 | template 52 | void SparseLUImpl::fixupL(const Index n, const IndexVector& perm_r, GlobalLU_t& glu) 53 | { 54 | Index fsupc, i, j, k, jstart; 55 | 56 | StorageIndex nextl = 0; 57 | Index nsuper = (glu.supno)(n); 58 | 59 | // For each supernode 60 | for (i = 0; i <= nsuper; i++) 61 | { 62 | fsupc = glu.xsup(i); 63 | jstart = glu.xlsub(fsupc); 64 | glu.xlsub(fsupc) = nextl; 65 | for (j = jstart; j < glu.xlsub(fsupc + 1); j++) 66 | { 67 | glu.lsub(nextl) = perm_r(glu.lsub(j)); // Now indexed into P*A 68 | nextl++; 69 | } 70 | for (k = fsupc+1; k < glu.xsup(i+1); k++) 71 | glu.xlsub(k) = nextl; // other columns in supernode i 72 | } 73 | 74 | glu.xlsub(n) = nextl; 75 | } 76 | 77 | } // end namespace internal 78 | 79 | } // end namespace Eigen 80 | #endif // EIGEN_SPARSELU_UTILS_H 81 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/misc/Kernel.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2009 Benoit Jacob 5 | // 6 | // This Source Code Form is subject to the terms of the Mozilla 7 | // Public License v. 2.0. If a copy of the MPL was not distributed 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | 10 | #ifndef EIGEN_MISC_KERNEL_H 11 | #define EIGEN_MISC_KERNEL_H 12 | 13 | namespace Eigen { 14 | 15 | namespace internal { 16 | 17 | /** \class kernel_retval_base 18 | * 19 | */ 20 | template 21 | struct traits > 22 | { 23 | typedef typename DecompositionType::MatrixType MatrixType; 24 | typedef Matrix< 25 | typename MatrixType::Scalar, 26 | MatrixType::ColsAtCompileTime, // the number of rows in the "kernel matrix" 27 | // is the number of cols of the original matrix 28 | // so that the product "matrix * kernel = zero" makes sense 29 | Dynamic, // we don't know at compile-time the dimension of the kernel 30 | MatrixType::Options, 31 | MatrixType::MaxColsAtCompileTime, // see explanation for 2nd template parameter 32 | MatrixType::MaxColsAtCompileTime // the kernel is a subspace of the domain space, 33 | // whose dimension is the number of columns of the original matrix 34 | > ReturnType; 35 | }; 36 | 37 | template struct kernel_retval_base 38 | : public ReturnByValue > 39 | { 40 | typedef _DecompositionType DecompositionType; 41 | typedef ReturnByValue Base; 42 | 43 | explicit kernel_retval_base(const DecompositionType& dec) 44 | : m_dec(dec), 45 | m_rank(dec.rank()), 46 | m_cols(m_rank==dec.cols() ? 1 : dec.cols() - m_rank) 47 | {} 48 | 49 | inline Index rows() const { return m_dec.cols(); } 50 | inline Index cols() const { return m_cols; } 51 | inline Index rank() const { return m_rank; } 52 | inline const DecompositionType& dec() const { return m_dec; } 53 | 54 | template inline void evalTo(Dest& dst) const 55 | { 56 | static_cast*>(this)->evalTo(dst); 57 | } 58 | 59 | protected: 60 | const DecompositionType& m_dec; 61 | Index m_rank, m_cols; 62 | }; 63 | 64 | } // end namespace internal 65 | 66 | #define EIGEN_MAKE_KERNEL_HELPERS(DecompositionType) \ 67 | typedef typename DecompositionType::MatrixType MatrixType; \ 68 | typedef typename MatrixType::Scalar Scalar; \ 69 | typedef typename MatrixType::RealScalar RealScalar; \ 70 | typedef Eigen::internal::kernel_retval_base Base; \ 71 | using Base::dec; \ 72 | using Base::rank; \ 73 | using Base::rows; \ 74 | using Base::cols; \ 75 | kernel_retval(const DecompositionType& dec) : Base(dec) {} 76 | 77 | } // end namespace Eigen 78 | 79 | #endif // EIGEN_MISC_KERNEL_H 80 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/misc/RealSvd2x2.h: -------------------------------------------------------------------------------- 1 | // This file is part of Eigen, a lightweight C++ template library 2 | // for linear algebra. 3 | // 4 | // Copyright (C) 2009-2010 Benoit Jacob 5 | // Copyright (C) 2013-2016 Gael Guennebaud 6 | // 7 | // This Source Code Form is subject to the terms of the Mozilla 8 | // Public License v. 2.0. If a copy of the MPL was not distributed 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 | 11 | #ifndef EIGEN_REALSVD2X2_H 12 | #define EIGEN_REALSVD2X2_H 13 | 14 | namespace Eigen { 15 | 16 | namespace internal { 17 | 18 | template 19 | void real_2x2_jacobi_svd(const MatrixType& matrix, Index p, Index q, 20 | JacobiRotation *j_left, 21 | JacobiRotation *j_right) 22 | { 23 | using std::sqrt; 24 | using std::abs; 25 | Matrix m; 26 | m << numext::real(matrix.coeff(p,p)), numext::real(matrix.coeff(p,q)), 27 | numext::real(matrix.coeff(q,p)), numext::real(matrix.coeff(q,q)); 28 | JacobiRotation rot1; 29 | RealScalar t = m.coeff(0,0) + m.coeff(1,1); 30 | RealScalar d = m.coeff(1,0) - m.coeff(0,1); 31 | 32 | if(abs(d) < (std::numeric_limits::min)()) 33 | { 34 | rot1.s() = RealScalar(0); 35 | rot1.c() = RealScalar(1); 36 | } 37 | else 38 | { 39 | // If d!=0, then t/d cannot overflow because the magnitude of the 40 | // entries forming d are not too small compared to the ones forming t. 41 | RealScalar u = t / d; 42 | RealScalar tmp = sqrt(RealScalar(1) + numext::abs2(u)); 43 | rot1.s() = RealScalar(1) / tmp; 44 | rot1.c() = u / tmp; 45 | } 46 | m.applyOnTheLeft(0,1,rot1); 47 | j_right->makeJacobi(m,0,1); 48 | *j_left = rot1 * j_right->transpose(); 49 | } 50 | 51 | } // end namespace internal 52 | 53 | } // end namespace Eigen 54 | 55 | #endif // EIGEN_REALSVD2X2_H 56 | -------------------------------------------------------------------------------- /third_party/eigen/Eigen/src/misc/lapacke_mangling.h: -------------------------------------------------------------------------------- 1 | #ifndef LAPACK_HEADER_INCLUDED 2 | #define LAPACK_HEADER_INCLUDED 3 | 4 | #ifndef LAPACK_GLOBAL 5 | #if defined(LAPACK_GLOBAL_PATTERN_LC) || defined(ADD_) 6 | #define LAPACK_GLOBAL(lcname,UCNAME) lcname##_ 7 | #elif defined(LAPACK_GLOBAL_PATTERN_UC) || defined(UPPER) 8 | #define LAPACK_GLOBAL(lcname,UCNAME) UCNAME 9 | #elif defined(LAPACK_GLOBAL_PATTERN_MC) || defined(NOCHANGE) 10 | #define LAPACK_GLOBAL(lcname,UCNAME) lcname 11 | #else 12 | #define LAPACK_GLOBAL(lcname,UCNAME) lcname##_ 13 | #endif 14 | #endif 15 | 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /tools/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | add_subdirectory(onnx2bnn) 4 | -------------------------------------------------------------------------------- /tools/onnx2bnn/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 JD.com Inc. JD AI 2 | 3 | include(${PROJECT_SOURCE_DIR}/cmake/utils.cmake) 4 | 5 | add_library(onnx2bnn 6 | ${CMAKE_CURRENT_SOURCE_DIR}/OnnxConverter.cpp 7 | ${CMAKE_CURRENT_SOURCE_DIR}/OnnxConverter.h 8 | ${CMAKE_CURRENT_SOURCE_DIR}/NodeAttrHelper.cpp 9 | ${CMAKE_CURRENT_SOURCE_DIR}/NodeAttrHelper.h 10 | ${PROJECT_SOURCE_DIR}/common/StrKeyMap.h 11 | ${PROJECT_SOURCE_DIR}/common/Shaper.h 12 | ${PROJECT_SOURCE_DIR}/common/Shaper.cpp 13 | ) 14 | target_link_libraries(onnx2bnn 15 | glog::glog 16 | onnx 17 | flatbuffers 18 | ) 19 | if (CMAKE_VERSION VERSION_LESS "3.9.0") 20 | target_link_libraries(onnx2bnn 21 | # ${PROTOBUF_LIBRARIES} and ${PROTOBUF_INCLUDE_DIRS} is for cmake 3.5 22 | ${PROTOBUF_LIBRARIES} ${Protobuf_LIBRARIES}) 23 | target_include_directories(onnx2bnn 24 | PUBLIC 25 | ${PROTOBUF_INCLUDE_DIRS} ${Protobuf_INCLUDE_DIRS}) 26 | else() 27 | target_link_libraries(onnx2bnn 28 | protobuf::libprotobuf 29 | ) 30 | endif() 31 | 32 | target_include_directories(onnx2bnn 33 | PUBLIC 34 | ${PROJECT_SOURCE_DIR} 35 | ${CMAKE_CURRENT_BINARY_DIR} 36 | ) 37 | 38 | add_executable(onnx2bnn-bin 39 | ${CMAKE_CURRENT_SOURCE_DIR}/onnx2bnn.cpp 40 | ) 41 | target_link_libraries(onnx2bnn-bin 42 | onnx2bnn) 43 | set_target_properties(onnx2bnn-bin PROPERTIES OUTPUT_NAME "onnx2bnn") 44 | if (${BNN_SYSTEM_PROTOBUF}) 45 | treat_warnings_as_errors(onnx2bnn) 46 | treat_warnings_as_errors(onnx2bnn-bin) 47 | endif() 48 | 49 | if (${BNN_BUILD_PYTHON}) 50 | pybind11_add_module(_onnx2bnn 51 | ${CMAKE_CURRENT_SOURCE_DIR}/pywrapper.cpp) 52 | 53 | target_link_libraries(_onnx2bnn 54 | PUBLIC 55 | protobuf::libprotobuf 56 | glog::glog 57 | onnx 58 | onnx2bnn 59 | pybind11::pybind11 60 | ) 61 | endif() 62 | -------------------------------------------------------------------------------- /tools/onnx2bnn/NodeAttrHelper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 JD.com Inc. JD AI 2 | 3 | // 4 | // Created by daquexian on 8/3/18. 5 | // 6 | 7 | #ifndef PROJECT_NODE_H 8 | #define PROJECT_NODE_H 9 | 10 | #include 11 | #include 12 | 13 | /** 14 | * Wrapping onnx::NodeProto for retrieving attribute values 15 | */ 16 | class NodeAttrHelper { 17 | public: 18 | NodeAttrHelper(ONNX_NAMESPACE::NodeProto proto); 19 | 20 | float get(const std::string &key, float def_val) const; 21 | int get(const std::string &key, int def_val) const; 22 | std::vector get(const std::string &key, 23 | std::vector def_val) const; 24 | std::vector get(const std::string &key, 25 | std::vector def_val) const; 26 | std::string get(const std::string &key, std::string def_val) const; 27 | 28 | bool has_attr(const std::string &key) const; 29 | 30 | private: 31 | const ONNX_NAMESPACE::NodeProto node_; 32 | }; 33 | 34 | #endif // PROJECT_ATTRIBUTE_H 35 | -------------------------------------------------------------------------------- /tools/onnx2bnn/pywrapper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "OnnxConverter.h" 4 | 5 | namespace py = pybind11; 6 | 7 | void convert(const std::string &model_str, const std::string &filepath, 8 | const std::string &level_str) { 9 | using namespace bnn; 10 | ONNX_NAMESPACE::ModelProto model_proto; 11 | bool ret = model_proto.ParseFromString(model_str); 12 | if (!ret) { 13 | throw std::invalid_argument("Read protobuf string failed"); 14 | } 15 | 16 | OnnxConverter::Level level = OnnxConverter::Level::kModerate; 17 | 18 | if (level_str == "moderate") { 19 | level = OnnxConverter::Level::kModerate; 20 | } else if (level_str == "strict") { 21 | level = OnnxConverter::Level::kStrict; 22 | } else if (level_str == "aggressive") { 23 | level = OnnxConverter::Level::kAggressive; 24 | } else { 25 | throw std::invalid_argument( 26 | "Level can only be moderate, strict or aggressive"); 27 | } 28 | OnnxConverter converter; 29 | converter.Convert(model_proto, filepath, level); 30 | google::protobuf::ShutdownProtobufLibrary(); 31 | } 32 | 33 | PYBIND11_MODULE(_onnx2bnn, m) { m.def("convert", &convert, ""); } 34 | -------------------------------------------------------------------------------- /tools/quick_benchmark.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import argparse 4 | import inspect 5 | import subprocess 6 | import os 7 | from pathlib import Path 8 | 9 | parser = argparse.ArgumentParser() 10 | parser.add_argument('--onnx', type=str, required=True) 11 | args, others = parser.parse_known_args() 12 | 13 | filename = inspect.getframeinfo(inspect.currentframe()).filename 14 | base_dir = Path(filename).resolve().parent.parent 15 | android_ndk = Path(os.getenv('ANDROID_NDK', default='')).resolve() 16 | onnx_model = Path(args.onnx).resolve() 17 | dabnn_build_dir = base_dir/'.build_dabnn_release' 18 | onnx2bnn_build_dir = base_dir/'.build_onnx2bnn_release' 19 | temp_dab_model = onnx2bnn_build_dir/'dabnn_quick_benchmark.dab' 20 | quick_benchmark_bin = dabnn_build_dir/'benchmark'/'benchmark_single_model' 21 | os.makedirs(dabnn_build_dir, exist_ok=True) 22 | os.makedirs(onnx2bnn_build_dir, exist_ok=True) 23 | print("Build dabnn..") 24 | subprocess.check_call('cmake -DCMAKE_TOOLCHAIN_FILE={}/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a ..'.format(android_ndk/'build'/'cmake'/'android.toolchain.cmake'), cwd=dabnn_build_dir, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 25 | 26 | subprocess.check_call('cmake --build .', cwd=dabnn_build_dir, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 27 | 28 | print("Build onnx2bnn..") 29 | subprocess.check_call('cmake ..', cwd=onnx2bnn_build_dir, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 30 | 31 | subprocess.check_call('cmake --build .', cwd=onnx2bnn_build_dir, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 32 | 33 | print("Generating daBNN model..") 34 | subprocess.check_call('./tools/onnx2bnn/onnx2bnn {} {} {}'.format(' '.join(others), onnx_model, temp_dab_model), cwd=onnx2bnn_build_dir, shell=True) 35 | print("Pushing daBNN model..") 36 | subprocess.check_call('adb push {} /data/local/tmp/'.format(temp_dab_model), cwd=onnx2bnn_build_dir, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 37 | subprocess.check_call('adb push {} /data/local/tmp/'.format(quick_benchmark_bin), cwd=dabnn_build_dir, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 38 | print("Benchmarking..") 39 | subprocess.check_call('adb shell /data/local/tmp/{} /data/local/tmp/{}'.format(quick_benchmark_bin.name, temp_dab_model.name), shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 40 | subprocess.check_call('adb shell rm /data/local/tmp/{} /data/local/tmp/{}'.format(quick_benchmark_bin.name, temp_dab_model.name), shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 41 | --------------------------------------------------------------------------------