Added .clang-format file and formatted everything
Signed-off-by: Fabian Sauter <sauter.fabian@mailbox.org>
This commit is contained in:
parent
f731f2e55c
commit
24cd307042
47 changed files with 5157 additions and 4354 deletions
|
|
@ -12,17 +12,16 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
|
||||
// Includes the Jni utilities for Android to be able to create the
|
||||
// relevant bindings for java, including JNIEXPORT, JNICALL , and
|
||||
// Includes the Jni utilities for Android to be able to create the
|
||||
// relevant bindings for java, including JNIEXPORT, JNICALL , and
|
||||
// other "j-variables".
|
||||
#include <jni.h>
|
||||
|
||||
// The ML class exposing the Kompute ML workflow for training and
|
||||
// The ML class exposing the Kompute ML workflow for training and
|
||||
// prediction of inference data.
|
||||
#include "KomputeModelML.hpp"
|
||||
|
||||
// Allows us to use the C++ sleep function to wait when loading the
|
||||
// Allows us to use the C++ sleep function to wait when loading the
|
||||
// Vulkan library in android
|
||||
#include <unistd.h>
|
||||
|
||||
|
|
@ -30,86 +29,92 @@
|
|||
#define KOMPUTE_VK_INIT_RETRIES 5
|
||||
#endif
|
||||
|
||||
static std::vector<float> jfloatArrayToVector(JNIEnv *env, const jfloatArray & fromArray) {
|
||||
float *inCArray = env->GetFloatArrayElements(fromArray, NULL);
|
||||
if (NULL == inCArray) return std::vector<float>();
|
||||
static std::vector<float>
|
||||
jfloatArrayToVector(JNIEnv* env, const jfloatArray& fromArray)
|
||||
{
|
||||
float* inCArray = env->GetFloatArrayElements(fromArray, NULL);
|
||||
if (NULL == inCArray)
|
||||
return std::vector<float>();
|
||||
int32_t length = env->GetArrayLength(fromArray);
|
||||
|
||||
std::vector<float> outVector(inCArray, inCArray + length);
|
||||
return outVector;
|
||||
}
|
||||
|
||||
static jfloatArray vectorToJFloatArray(JNIEnv *env, const std::vector<float> & fromVector) {
|
||||
static jfloatArray
|
||||
vectorToJFloatArray(JNIEnv* env, const std::vector<float>& fromVector)
|
||||
{
|
||||
jfloatArray ret = env->NewFloatArray(fromVector.size());
|
||||
if (NULL == ret) return NULL;
|
||||
if (NULL == ret)
|
||||
return NULL;
|
||||
env->SetFloatArrayRegion(ret, 0, fromVector.size(), fromVector.data());
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_initVulkan(JNIEnv *env, jobject thiz) {
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_initVulkan(JNIEnv* env, jobject thiz)
|
||||
{
|
||||
|
||||
KP_LOG_INFO("Initialising vulkan");
|
||||
KP_LOG_INFO("Initialising vulkan");
|
||||
|
||||
uint32_t totalRetries = 0;
|
||||
uint32_t totalRetries = 0;
|
||||
|
||||
while (totalRetries < KOMPUTE_VK_INIT_RETRIES) {
|
||||
KP_LOG_INFO("VULKAN LOAD TRY NUMBER: %u", totalRetries);
|
||||
if(InitVulkan()) {
|
||||
break;
|
||||
while (totalRetries < KOMPUTE_VK_INIT_RETRIES) {
|
||||
KP_LOG_INFO("VULKAN LOAD TRY NUMBER: %u", totalRetries);
|
||||
if (InitVulkan()) {
|
||||
break;
|
||||
}
|
||||
sleep(1);
|
||||
totalRetries++;
|
||||
}
|
||||
sleep(1);
|
||||
totalRetries++;
|
||||
|
||||
return totalRetries < KOMPUTE_VK_INIT_RETRIES;
|
||||
}
|
||||
|
||||
return totalRetries < KOMPUTE_VK_INIT_RETRIES;
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_kompute(
|
||||
JNIEnv *env,
|
||||
jobject thiz,
|
||||
jfloatArray xiJFloatArr,
|
||||
jfloatArray xjJFloatArr,
|
||||
jfloatArray yJFloatArr) {
|
||||
|
||||
KP_LOG_INFO("Creating manager");
|
||||
|
||||
std::vector<float> xiVector = jfloatArrayToVector(env, xiJFloatArr);
|
||||
std::vector<float> xjVector = jfloatArrayToVector(env, xjJFloatArr);
|
||||
std::vector<float> yVector = jfloatArrayToVector(env, yJFloatArr);
|
||||
|
||||
KomputeModelML kml;
|
||||
kml.train(yVector, xiVector, xjVector);
|
||||
|
||||
std::vector<float> pred = kml.predict(xiVector, xjVector);
|
||||
|
||||
return vectorToJFloatArray(env, pred);
|
||||
}
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_komputeParams(
|
||||
JNIEnv *env,
|
||||
jobject thiz,
|
||||
jfloatArray xiJFloatArr,
|
||||
jfloatArray xjJFloatArr,
|
||||
jfloatArray yJFloatArr) {
|
||||
|
||||
KP_LOG_INFO("Creating manager");
|
||||
|
||||
std::vector<float> xiVector = jfloatArrayToVector(env, xiJFloatArr);
|
||||
std::vector<float> xjVector = jfloatArrayToVector(env, xjJFloatArr);
|
||||
std::vector<float> yVector = jfloatArrayToVector(env, yJFloatArr);
|
||||
|
||||
KomputeModelML kml;
|
||||
kml.train(yVector, xiVector, xjVector);
|
||||
|
||||
std::vector<float> params = kml.get_params();
|
||||
|
||||
return vectorToJFloatArray(env, params);
|
||||
}
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_kompute(JNIEnv* env,
|
||||
jobject thiz,
|
||||
jfloatArray xiJFloatArr,
|
||||
jfloatArray xjJFloatArr,
|
||||
jfloatArray yJFloatArr)
|
||||
{
|
||||
|
||||
KP_LOG_INFO("Creating manager");
|
||||
|
||||
std::vector<float> xiVector = jfloatArrayToVector(env, xiJFloatArr);
|
||||
std::vector<float> xjVector = jfloatArrayToVector(env, xjJFloatArr);
|
||||
std::vector<float> yVector = jfloatArrayToVector(env, yJFloatArr);
|
||||
|
||||
KomputeModelML kml;
|
||||
kml.train(yVector, xiVector, xjVector);
|
||||
|
||||
std::vector<float> pred = kml.predict(xiVector, xjVector);
|
||||
|
||||
return vectorToJFloatArray(env, pred);
|
||||
}
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_komputeParams(JNIEnv* env,
|
||||
jobject thiz,
|
||||
jfloatArray xiJFloatArr,
|
||||
jfloatArray xjJFloatArr,
|
||||
jfloatArray yJFloatArr)
|
||||
{
|
||||
|
||||
KP_LOG_INFO("Creating manager");
|
||||
|
||||
std::vector<float> xiVector = jfloatArrayToVector(env, xiJFloatArr);
|
||||
std::vector<float> xjVector = jfloatArrayToVector(env, xjJFloatArr);
|
||||
std::vector<float> yVector = jfloatArrayToVector(env, yJFloatArr);
|
||||
|
||||
KomputeModelML kml;
|
||||
kml.train(yVector, xiVector, xjVector);
|
||||
|
||||
std::vector<float> params = kml.get_params();
|
||||
|
||||
return vectorToJFloatArray(env, params);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
43
examples/android/android-simple/app/src/main/cpp/KomputeModelML.cpp
Executable file → Normal file
43
examples/android/android-simple/app/src/main/cpp/KomputeModelML.cpp
Executable file → Normal file
|
|
@ -1,15 +1,15 @@
|
|||
|
||||
#include "KomputeModelML.hpp"
|
||||
|
||||
KomputeModelML::KomputeModelML() {
|
||||
KomputeModelML::KomputeModelML() {}
|
||||
|
||||
}
|
||||
KomputeModelML::~KomputeModelML() {}
|
||||
|
||||
KomputeModelML::~KomputeModelML() {
|
||||
|
||||
}
|
||||
|
||||
void KomputeModelML::train(std::vector<float> yData, std::vector<float> xIData, std::vector<float> xJData) {
|
||||
void
|
||||
KomputeModelML::train(std::vector<float> yData,
|
||||
std::vector<float> xIData,
|
||||
std::vector<float> xJData)
|
||||
{
|
||||
|
||||
std::vector<float> zerosData;
|
||||
|
||||
|
|
@ -42,17 +42,19 @@ void KomputeModelML::train(std::vector<float> yData, std::vector<float> xIData,
|
|||
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));
|
||||
|
||||
(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));
|
||||
|
||||
std::shared_ptr<kp::Algorithm> algorithm = mgr.algorithm(
|
||||
params, spirv, kp::Workgroup({ 5 }), std::vector<float>({ 5.0 }));
|
||||
params, spirv, kp::Workgroup({ 5 }), std::vector<float>({ 5.0 }));
|
||||
|
||||
mgr.sequence()->eval<kp::OpTensorSyncDevice>(params);
|
||||
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence()
|
||||
std::shared_ptr<kp::Sequence> sq =
|
||||
mgr.sequence()
|
||||
->record<kp::OpTensorSyncDevice>({ wIn, bIn })
|
||||
->record<kp::OpAlgoDispatch>(algorithm)
|
||||
->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
|
@ -79,7 +81,9 @@ void KomputeModelML::train(std::vector<float> yData, std::vector<float> xIData,
|
|||
}
|
||||
}
|
||||
|
||||
std::vector<float> KomputeModelML::predict(std::vector<float> xI, std::vector<float> xJ) {
|
||||
std::vector<float>
|
||||
KomputeModelML::predict(std::vector<float> xI, std::vector<float> xJ)
|
||||
{
|
||||
|
||||
KP_LOG_INFO("Running prediction inference");
|
||||
|
||||
|
|
@ -93,9 +97,8 @@ std::vector<float> KomputeModelML::predict(std::vector<float> xI, std::vector<fl
|
|||
for (size_t i = 0; i < xI.size(); i++) {
|
||||
float xIVal = xI[i];
|
||||
float xJVal = xJ[i];
|
||||
float result = (xIVal * this->mWeights[0]
|
||||
+ xJVal * this->mWeights[1]
|
||||
+ this->mBias[0]);
|
||||
float result = (xIVal * this->mWeights[0] + xJVal * this->mWeights[1] +
|
||||
this->mBias[0]);
|
||||
|
||||
// Instead of using sigmoid we'll just return full numbers
|
||||
float var = result > 0 ? 1 : 0;
|
||||
|
|
@ -107,13 +110,15 @@ std::vector<float> KomputeModelML::predict(std::vector<float> xI, std::vector<fl
|
|||
return retVector;
|
||||
}
|
||||
|
||||
std::vector<float> KomputeModelML::get_params() {
|
||||
std::vector<float>
|
||||
KomputeModelML::get_params()
|
||||
{
|
||||
|
||||
KP_LOG_INFO("Displaying results");
|
||||
|
||||
std::vector<float> retVector;
|
||||
|
||||
if(this->mWeights.size() + this->mBias.size() == 0) {
|
||||
if (this->mWeights.size() + this->mBias.size() == 0) {
|
||||
return retVector;
|
||||
}
|
||||
|
||||
|
|
|
|||
18
examples/android/android-simple/app/src/main/cpp/KomputeModelML.hpp
Executable file → Normal file
18
examples/android/android-simple/app/src/main/cpp/KomputeModelML.hpp
Executable file → Normal file
|
|
@ -2,28 +2,30 @@
|
|||
#ifndef KOMPUTEMODELML_HPP
|
||||
#define KOMPUTEMODELML_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "kompute/Kompute.hpp"
|
||||
|
||||
class KomputeModelML {
|
||||
class KomputeModelML
|
||||
{
|
||||
|
||||
public:
|
||||
public:
|
||||
KomputeModelML();
|
||||
virtual ~KomputeModelML();
|
||||
|
||||
void train(std::vector<float> yData, std::vector<float> xIData, std::vector<float> xJData);
|
||||
void train(std::vector<float> yData,
|
||||
std::vector<float> xIData,
|
||||
std::vector<float> xJData);
|
||||
|
||||
std::vector<float> predict(std::vector<float> xI, std::vector<float> xJ);
|
||||
|
||||
std::vector<float> get_params();
|
||||
|
||||
private:
|
||||
private:
|
||||
std::vector<float> mWeights;
|
||||
std::vector<float> mBias;
|
||||
|
||||
};
|
||||
|
||||
static std::string LR_SHADER = R"(
|
||||
|
|
@ -83,4 +85,4 @@ void main() {
|
|||
}
|
||||
)";
|
||||
|
||||
#endif //ANDROID_SIMPLE_KOMPUTEMODELML_HPP
|
||||
#endif // ANDROID_SIMPLE_KOMPUTEMODELML_HPP
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue