Skip to content

Commit

Permalink
Fixed building Kompute for Android
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Sauter <[email protected]>
  • Loading branch information
COM8 committed Oct 27, 2022
1 parent 3d6c5af commit 6506aba
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 1,558 deletions.
2 changes: 0 additions & 2 deletions .ccls
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@
-I./external/spdlog/include/
-I./external/fmt/include/
-I./src/include/
-I./single_include/
-I./vk_ndk_wrapper_include/
-I./test/compiled_shaders_include/
-I./test/utils/
34 changes: 21 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,33 @@ include(cmake/deprecation_warnings.cmake)
# Dependencies
# ####################################################
include(cmake/vulkan_shader_compiler.cmake)
include(FetchContent)
include(cmake/check_vulkan_version.cmake)

if(KOMPUTE_OPT_USE_BUILT_IN_VULKAN_HEADER)
FetchContent_Declare(vulkan_header GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Headers.git
GIT_TAG ${KOMPUTE_OPT_BUILT_IN_VULKAN_HEADER_TAG}) # Source: https://github.com/KhronosGroup/Vulkan-Headers/tags
FetchContent_MakeAvailable(vulkan_header)

if(NOT KOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK)
# Ensure the driver supports this Vulkan version
check_vulkan_version(INCLUDE_DIR "${vulkan_header_SOURCE_DIR}/include")
endif()
endif()
include(FetchContent)

# Vulkan Header
# We don't import Vulkan library if Android build as it is built dynamically
# Otherwise it is expected that the Vulkan SDK and dependencies are installed
# Has to happen AFTER using the build-in Vulkan headers to prevent multiple targets with the name Vulkan::Headers
if(NOT KOMPUTE_OPT_ANDROID_BUILD)
if(KOMPUTE_OPT_ANDROID_BUILD)
add_library(vulkanAndroid INTERFACE)
set(VULKAN_INCLUDE_DIR ${ANDROID_NDK}/sources/third_party/vulkan/src/include)
target_sources(vulkanAndroid INTERFACE ${VULKAN_INCLUDE_DIR}/vulkan/vulkan.hpp)
target_include_directories(vulkanAndroid INTERFACE ${VULKAN_INCLUDE_DIR})

target_compile_definitions(vulkanAndroid INTERFACE VK_NO_PROTOTYPES=1)
target_compile_definitions(vulkanAndroid INTERFACE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1)
else()
if(KOMPUTE_OPT_USE_BUILT_IN_VULKAN_HEADER)
FetchContent_Declare(vulkan_header GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Headers.git
GIT_TAG ${KOMPUTE_OPT_BUILT_IN_VULKAN_HEADER_TAG}) # Source: https://github.com/KhronosGroup/Vulkan-Headers/tags
FetchContent_MakeAvailable(vulkan_header)

if(NOT KOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK)
# Ensure the driver supports this Vulkan version
check_vulkan_version(INCLUDE_DIR "${vulkan_header_SOURCE_DIR}/include")
endif()
endif()

find_package(Vulkan REQUIRED)

if(Vulkan_FOUND AND NOT TARGET Vulkan::Headers)
Expand Down
22 changes: 11 additions & 11 deletions examples/android/android-simple/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
ndkVersion '21.2.6472646'
compileSdkVersion 33
ndkVersion '25.1.8937393'

defaultConfig {
applicationId "com.ethicalml.kompute"
minSdkVersion 23
targetSdkVersion 29
minSdkVersion 26
targetSdkVersion 33
versionCode = 1
versionName = "0.0.1"
externalNativeBuild {
cmake {
abiFilters "armeabi-v7a", 'arm64-v8a', 'x86', 'x86_64'
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_STL=c++_static',
'-DKOMPUTE_OPT_ANDROID_BUILD=ON',
'-DKOMPUTE_OPT_INSTALL=OFF',
'-DKOMPUTE_OPT_USE_SPDLOG=OFF',
'-DKOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS=ON',
'-DCMAKE_CXX_FLAGS=-DKOMPUTE_VK_API_MINOR_VERSION=0'
'-DANDROID_STL=c++_static'
}
}
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}

}

buildFeatures {
Expand All @@ -39,6 +37,7 @@ android {
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
version '3.22.2'
}
}

Expand All @@ -62,6 +61,7 @@ android {
// armeabi-v7a, arm64-v8a, x86, x86_64
}
}
namespace 'com.ethicalml.kompute'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ethicalml.kompute">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".KomputeJni">
<activity android:name=".KomputeJni"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
37 changes: 16 additions & 21 deletions examples/android/android-simple/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
cmake_minimum_required(VERSION 3.4.1)
cmake_minimum_required(VERSION 3.20)

add_subdirectory(../../../../../../../ ${CMAKE_CURRENT_BINARY_DIR}/kompute_build)
set(CMAKE_CXX_STANDARD 17)

set(VK_ANDROID_INCLUDE_DIR ${ANDROID_NDK}/sources/third_party/vulkan/src/include)
include(FetchContent)
FetchContent_Declare(kompute GIT_REPOSITORY https://github.com/COM8/kompute.git
GIT_TAG b28d4ae0bec1d39003b97574623a52b0b5f7494d) # The commit hash for a dev version before v0.9.0. Replace with the latest from: https://github.com/KomputeProject/kompute/releases
set(KOMPUTE_OPT_ANDROID_BUILD ON)
set(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS ON)
FetchContent_MakeAvailable(kompute)
include_directories(${kompute_SOURCE_DIR}/src/include)

add_library(kompute-jni SHARED
KomputeJniNative.cpp
KomputeModelML.cpp)
# Add to the list, so CMake can later find the code to compile shaders to header files
list(APPEND CMAKE_PREFIX_PATH "${kompute_SOURCE_DIR}/cmake")
add_subdirectory(shader)

include_directories(
${VK_ANDROID_COMMON_DIR}
${VK_ANDROID_INCLUDE_DIR}
../../../../../../../single_include/
../../../../../../../vk_ndk_wrapper_include/)
add_library(kompute-jni SHARED KomputeJniNative.cpp
KomputeModelML.cpp
KomputeModelML.hpp)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 \
-DVK_USE_PLATFORM_ANDROID_KHR=1 \
-DKOMPUTE_DISABLE_VK_DEBUG_LAYERS=1")
target_link_libraries(kompute-jni PRIVATE kompute::kompute shader log android)

target_link_libraries(kompute-jni
# Libraries from kompute build
kompute
kompute_vk_ndk_wrapper
# Libraries from android build
log
android)
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
// Allows us to use the C++ sleep function to wait when loading the
// Vulkan library in android
#include <unistd.h>

#ifndef KOMPUTE_VK_INIT_RETRIES
#define KOMPUTE_VK_INIT_RETRIES 5
#endif
#include <kompute/logger/Logger.hpp>

static std::vector<float>
jfloatArrayToVector(JNIEnv* env, const jfloatArray& fromArray)
Expand Down Expand Up @@ -62,16 +59,17 @@ extern "C"

uint32_t totalRetries = 0;

while (totalRetries < KOMPUTE_VK_INIT_RETRIES) {
/*while (totalRetries < KOMPUTE_VK_INIT_RETRIES) {
KP_LOG_INFO("VULKAN LOAD TRY NUMBER: %u", totalRetries);
if (InitVulkan()) {
break;
}
sleep(1);
totalRetries++;
}
}*/

return totalRetries < KOMPUTE_VK_INIT_RETRIES;
// return totalRetries < KOMPUTE_VK_INIT_RETRIES;
return true;
}

JNIEXPORT jfloatArray JNICALL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

#include "KomputeModelML.hpp"
#include "my_shader.hpp"
#include <kompute/Kompute.hpp>


KomputeModelML::KomputeModelML() {}

Expand All @@ -10,7 +13,6 @@ KomputeModelML::train(std::vector<float> yData,
std::vector<float> xIData,
std::vector<float> xJData)
{

std::vector<float> zerosData;

for (size_t i = 0; i < yData.size(); i++) {
Expand Down Expand Up @@ -41,15 +43,11 @@ KomputeModelML::train(std::vector<float> yData,
wIn, wOutI, wOutJ,
bIn, bOut, lOut };

std::vector<uint32_t> spirv = std::vector<uint32_t>(
(uint32_t*)kp::shader_data::shaders_glsl_logisticregression_comp_spv,
(uint32_t*)(kp::shader_data::
shaders_glsl_logisticregression_comp_spv +
kp::shader_data::
shaders_glsl_logisticregression_comp_spv_len));
const std::vector<uint32_t> shader = std::vector<uint32_t>(
shader::MY_SHADER_COMP_SPV.begin(), shader::MY_SHADER_COMP_SPV.end());

std::shared_ptr<kp::Algorithm> algorithm = mgr.algorithm(
params, spirv, kp::Workgroup({ 5 }), std::vector<float>({ 5.0 }));
params, shader, kp::Workgroup({ 5 }), std::vector<float>({ 5.0 }));

mgr.sequence()->eval<kp::OpTensorSyncDevice>(params);

Expand Down Expand Up @@ -84,7 +82,6 @@ KomputeModelML::train(std::vector<float> yData,
std::vector<float>
KomputeModelML::predict(std::vector<float> xI, std::vector<float> xJ)
{

KP_LOG_INFO("Running prediction inference");

assert(xI.size() == xJ.size());
Expand Down Expand Up @@ -113,7 +110,6 @@ KomputeModelML::predict(std::vector<float> xI, std::vector<float> xJ)
std::vector<float>
KomputeModelML::get_params()
{

KP_LOG_INFO("Displaying results");

std::vector<float> retVector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@

#ifndef KOMPUTEMODELML_HPP
#define KOMPUTEMODELML_HPP
#pragma once

#include <memory>
#include <string>
#include <vector>

#include "kompute/Kompute.hpp"

class KomputeModelML
{

Expand All @@ -27,62 +22,3 @@ class KomputeModelML
std::vector<float> mWeights;
std::vector<float> mBias;
};

static std::string LR_SHADER = R"(
#version 450
layout (constant_id = 0) const uint M = 0;
layout (local_size_x = 1) in;
layout(set = 0, binding = 0) buffer bxi { float xi[]; };
layout(set = 0, binding = 1) buffer bxj { float xj[]; };
layout(set = 0, binding = 2) buffer by { float y[]; };
layout(set = 0, binding = 3) buffer bwin { float win[]; };
layout(set = 0, binding = 4) buffer bwouti { float wouti[]; };
layout(set = 0, binding = 5) buffer bwoutj { float woutj[]; };
layout(set = 0, binding = 6) buffer bbin { float bin[]; };
layout(set = 0, binding = 7) buffer bbout { float bout[]; };
layout(set = 0, binding = 8) buffer blout { float lout[]; };
float m = float(M);
float sigmoid(float z) {
return 1.0 / (1.0 + exp(-z));
}
float inference(vec2 x, vec2 w, float b) {
// Compute the linear mapping function
float z = dot(w, x) + b;
// Calculate the y-hat with sigmoid
float yHat = sigmoid(z);
return yHat;
}
float calculateLoss(float yHat, float y) {
return -(y * log(yHat) + (1.0 - y) * log(1.0 - yHat));
}
void main() {
uint idx = gl_GlobalInvocationID.x;
vec2 wCurr = vec2(win[0], win[1]);
float bCurr = bin[0];
vec2 xCurr = vec2(xi[idx], xj[idx]);
float yCurr = y[idx];
float yHat = inference(xCurr, wCurr, bCurr);
float dZ = yHat - yCurr;
vec2 dW = (1. / m) * xCurr * dZ;
float dB = (1. / m) * dZ;
wouti[idx] = dW.x;
woutj[idx] = dW.y;
bout[idx] = dB;
lout[idx] = calculateLoss(yHat, yCurr);
}
)";

#endif // ANDROID_SIMPLE_KOMPUTEMODELML_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.20)

# To add more shaders simply copy the vulkan_compile_shader command and replace it with your new shader
vulkan_compile_shader(INFILE my_shader.comp
OUTFILE my_shader.hpp
NAMESPACE "shader"
RELATIVE_PATH "${kompute_SOURCE_DIR}/cmake")

# Then add it to the library, so you can access it later in your code
add_library(shader INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/my_shader.hpp"

# "${CMAKE_CURRENT_BINARY_DIR}/my_shader2.hpp"
)

target_include_directories(shader INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
Loading

0 comments on commit 6506aba

Please sign in to comment.