Fixed building Kompute for Android
Signed-off-by: Fabian Sauter <sauter.fabian@mailbox.org>
This commit is contained in:
parent
3d6c5af302
commit
6506aba466
19 changed files with 158 additions and 1558 deletions
|
|
@ -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 {
|
||||
|
|
@ -39,6 +37,7 @@ android {
|
|||
externalNativeBuild {
|
||||
cmake {
|
||||
path 'src/main/cpp/CMakeLists.txt'
|
||||
version '3.22.2'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -62,6 +61,7 @@ android {
|
|||
// armeabi-v7a, arm64-v8a, x86, x86_64
|
||||
}
|
||||
}
|
||||
namespace 'com.ethicalml.kompute'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<?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"
|
||||
|
|
@ -9,7 +8,8 @@
|
|||
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" />
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
|
||||
#include "KomputeModelML.hpp"
|
||||
#include "my_shader.hpp"
|
||||
#include <kompute/Kompute.hpp>
|
||||
|
||||
|
||||
KomputeModelML::KomputeModelML() {}
|
||||
|
||||
|
|
@ -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++) {
|
||||
|
|
@ -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);
|
||||
|
||||
|
|
@ -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());
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,8 @@
|
|||
|
||||
#ifndef KOMPUTEMODELML_HPP
|
||||
#define KOMPUTEMODELML_HPP
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "kompute/Kompute.hpp"
|
||||
|
||||
class KomputeModelML
|
||||
{
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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}>)
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
#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);
|
||||
}
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.3.72'
|
||||
ext.kotlin_version = '1.6.20'
|
||||
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:4.0.0'
|
||||
classpath 'com.android.tools.build:gradle:7.3.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
|||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue