Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Caffe2 ARM ComputeLibrary integration #2015

Merged
merged 10 commits into from
Feb 24, 2018

Conversation

jerryzh168
Copy link
Contributor

No description provided.

@facebook-github-bot
Copy link
Contributor

@jerryzh168 has updated the pull request. View: changes

@@ -1,5 +1,8 @@
add_subdirectory(ios)
add_subdirectory(opengl)
if (USE_ARM_COMPUTE AND ANDROID)
add_subdirectory(arm-compute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert to spaces

@@ -0,0 +1,11 @@
if(USE_ARM_COMPUTE AND ANDROID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brew install scons
```

use the build\_android.sh:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which word? build_android.sh? that is probably for markdown..


CAFFE_DEFINE_REGISTRY(GLOperatorRegistry, OperatorBase, const OperatorDef &,
Workspace *);
CAFFE_REGISTER_DEVICE_TYPE(DeviceType::OPENGL, GLOperatorRegistry);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably collides with our OpenGL ES 3.0 backend

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is OpenGL ES 3.0 backend removed already?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, its still in the source tree, and potentially even being built.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change it to something else? I think DeviceType::OPENGL is a pre-defined device type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use a different device type, similar to how we use different device type for MKL-DNN (which, albeit runs on CPU, keeps tensors in a non-standard layout)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @hlu1 to check if this can conflict with OpenGL ES 3.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#ifdef CAFFE2_OPENGL_BACKEND
#error Can only build one OpenGL backend at a time.
#else
#define CAFFE2_OPENGL_BACKEND
#endif

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenGL ES 3.0 backend does not use DeviceType. It prefixes operator names with OpenGL instead

CMakeLists.txt Outdated
@@ -25,6 +25,7 @@ cmake_dependent_option(
"NOT BUILD_SHARED_LIBS" OFF)
option(BUILD_OBSERVERS "Build performance observers/loggers in caffe2/share/observers directory" OFF)
option(BUILD_TEST "Build C++ test binaries (need gtest and gbenchmark)" ON)
option(USE_ARM_COMPUTE "Use ARM Compute Library" ON)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets turn this off by default, as it has a scons dependency at the moment

.gitmodules Outdated
@@ -61,6 +61,9 @@
[submodule "third_party/python-six"]
path = third_party/python-six
url = https://github.com/benjaminp/six.git
[submodule "third_party/ComputeLibrary"]
path = third_party/ComputeLibrary
url = https://github.com/ARM-software/ComputeLibrary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.git for consistency


# Disable unused dependencies
CMAKE_ARGS+=("-DUSE_CUDA=OFF")
CMAKE_ARGS+=("-DUSE_GFLAGS=OFF")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add this back?

# Use android-cmake to build Android project from CMake.
CMAKE_ARGS+=("-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake")

# Don't build artifacts we don't need
CMAKE_ARGS+=("-DBUILD_TEST=OFF")
CMAKE_ARGS+=("-DBUILD_TEST=ON")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is on by default, either way the comment above doesn't make sense anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we turn on the test by default?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better keep it off. When needed, tests can be built with scripts/build_android.sh -DBUILD_TEST=ON

CMAKE_ARGS+=($@)
USE_ARM_COMPUTE=false
for arg in ${CMAKE_ARGS[@]};do
if [ $arg == "-DUSE_ARM_COMPUTE=ON" ];then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a couple other ways to set things "true" in cmake, it might be good to check for all of those

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-DUSE_ARM_COMPUTE=1? what else?


cmake "$CAFFE2_ROOT" \
-DCMAKE_INSTALL_PREFIX=../install \
-DCMAKE_BUILD_TYPE=Release \
"${CMAKE_ARGS[@]}"
-G Ninja \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be a default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some changes got lost, will revert to the master

else
cmake --build . -- "-j$(nproc)"
fi
ninja
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets leave this bit

@@ -10,6 +10,7 @@ set(Caffe2_GPU_SRCS)
# files for CPU and GPU tests respectively.
set(Caffe2_CPU_TEST_SRCS)
set(Caffe2_GPU_TEST_SRCS)
set(Caffe2_MOBILE_TEST_SRCS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need Caffe2_MOBILE_TEST_SRCS? IIUC, Caffe2_GPU_TEST_SRCS exist because it is built with a different compiler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that we can build only those tests when we build on mobile? I'm not sure, @bwasti what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Yangqing as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, we can chuck everything into CPU_TEST_SRCS. semantically, we could rename them to CC_SRCS and NVCC_SRCS huh?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, CC_SRCS + NVCC_SRCS would make more sense


cmake "$CAFFE2_ROOT" \
-DCMAKE_INSTALL_PREFIX=../install \
-DCMAKE_BUILD_TYPE=Release \
"${CMAKE_ARGS[@]}"
-G Ninja \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of switching to ninja, but lets leave it for a separate PR

@Maratyszcza
Copy link
Contributor

Could you keep changes to CMakeLists to a minimum?

@facebook-github-bot
Copy link
Contributor

@jerryzh168 has updated the pull request. View: changes

@jerryzh168
Copy link
Contributor Author

@Maratyszcza @bwasti could you review again? it would be great if we can merge this today

@facebook-github-bot
Copy link
Contributor

@jerryzh168 has updated the pull request. View: changes

@@ -169,7 +182,7 @@ if (BUILD_TEST)
${test_name} ${Caffe2_MAIN_LIBS} ${Caffe2_DEPENDENCY_LIBS}
gtest_main)
endif()
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.0)
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.0 AND NOT ANDROID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change? The sources still use cxx_range_for when building for android. target_compile_features tells CMake to set appropriate flags. Without it, compiler may default to c++98.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target_compile_features is not recognized before for some reason.. resolved now.

@jerryzh168
Copy link
Contributor Author

remaining issue device_type and Caffe2_MOBILE_TEST_SRCS, but I think we can modify these in another diff as long as it doesn't break the ci?

@facebook-github-bot
Copy link
Contributor

@jerryzh168 has updated the pull request. View: changes

CMakeLists.txt Outdated
@@ -25,6 +25,7 @@ cmake_dependent_option(
"NOT BUILD_SHARED_LIBS" OFF)
option(BUILD_OBSERVERS "Build performance observers/loggers in caffe2/share/observers directory" OFF)
option(BUILD_TEST "Build C++ test binaries (need gtest and gbenchmark)" ON)
option(USE_ARM_COMPUTE "Use ARM Compute Library" OFF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is ambiguous: users may think they have to enable it for any ARM builds. Please change to USE_ACL, USE_ARMCL, or USE_ARM_COMPUTE_LIBRARY

CMAKE_ARGS+=("-DBUILD_BINARY=OFF")
CMAKE_ARGS+=("-DBUILD_PYTHON=OFF")
CMAKE_ARGS+=("-DBUILD_SHARED_LIBS=OFF")

if $USE_ARM_COMPUTE;then
CMAKE_ARGS+=("-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line have the same effect as -DANDROID_TOOLCHAIN=gcc. See https://android.googlesource.com/platform/ndk/+/master/build/cmake/android.toolchain.cmake#103

@@ -103,7 +115,7 @@ fi
cmake "$CAFFE2_ROOT" \
-DCMAKE_INSTALL_PREFIX=../install \
-DCMAKE_BUILD_TYPE=Release \
"${CMAKE_ARGS[@]}"
"${CMAKE_ARGS[@]}" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert?

@@ -52,6 +52,13 @@ CMAKE_ARGS=()
# target architecture and need an exact version match.
CMAKE_ARGS+=("-DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=$CAFFE2_ROOT/build_host_protoc/bin/protoc")

CMAKE_ARGS+=($@)
USE_ARM_COMPUTE=false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this snippet, its not needed

set (ARM_COMPUTE_CORE_LIB "${CMAKE_CURRENT_BINARY_DIR}/libarm_compute_core.a")
set (ARM_COMPUTE_LIBS ${ARM_COMPUTE_LIB} ${ARM_COMPUTE_CORE_LIB})

add_custom_command(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom command doesn't have DEPENDS, so it won't rebuild the sources e.g. after a submodule update. Hopefully, ARM CL would just add CMakeLists, and we don't have to monkey-patch it.

@@ -464,6 +501,14 @@ if (USE_METAL)
endif()
endif()


if (USE_ARM_COMPUTE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be above the code that imports arm compute library targets from Scons. The motivation is to check only if (USE_ARM_COMPUTE), so if we need to enable this backend on other platforms, we need to patch only one snippet.

Copy link
Contributor

@Maratyszcza Maratyszcza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do the following before merging:

  • Remove Caffe2_MOBILE_TEST_SRCS, use Caffe2_CPU_TEST_SRCS
  • Remove code that sets and uses USE_ARM_COMPUTE from scripts/build_android.sh
  • Replace if (USE_ARM_COMPUTE AND ANDROID) with if (USE_ARM_COMPUTE). Only the snippet that disables ARMCL on non-Android platforms should check the platform.
  • Rename USE_ARM_COMPUTE option to something less ambiguous. Current name may suggest that this option is required for all builds for ARM.
  • Check that is builds with ARM Compute Library for ARMv7 and ARM64, and with -DBUILD_TEST=ON as well.

I didn't look much at C++ code, assume @bwasti understands it.

namespace caffe2 {

typedef half_float::half half;
typedef half DataType;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to check if float16 is supported on device?

@facebook-github-bot
Copy link
Contributor

@jerryzh168 has updated the pull request. View: changes

Copy link
Contributor

@bwasti bwasti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename the PR to be a bit more informative?

@bwasti bwasti changed the title Caffe2 acl Caffe2 ARM ComputeLibrary integration Feb 24, 2018
@facebook-github-bot
Copy link
Contributor

@jerryzh168 has updated the pull request. View: changes

@jerryzh168 jerryzh168 merged commit f543d91 into facebookarchive:master Feb 24, 2018
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerryzh168 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants