Updated to add logistic regression godot example
This commit is contained in:
parent
63426281fe
commit
9308b83af4
50 changed files with 101078 additions and 0 deletions
|
|
@ -0,0 +1,15 @@
|
|||
#include "KomputeSummator.hpp"
|
||||
|
||||
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
|
||||
godot::Godot::gdnative_init(o);
|
||||
}
|
||||
|
||||
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
|
||||
godot::Godot::gdnative_terminate(o);
|
||||
}
|
||||
|
||||
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
|
||||
godot::Godot::nativescript_init(handle);
|
||||
|
||||
godot::register_class<godot::KomputeSummator>();
|
||||
}
|
||||
|
|
@ -0,0 +1,142 @@
|
|||
/* summator.cpp */
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "KomputeSummator.hpp"
|
||||
|
||||
namespace godot {
|
||||
|
||||
KomputeSummator::KomputeSummator() {
|
||||
std::cout << "CALLING CONSTRUCTOR" << std::endl;
|
||||
this->_init();
|
||||
}
|
||||
|
||||
void KomputeSummator::train(Array yArr, Array xIArr, Array xJArr) {
|
||||
|
||||
assert(y.size() == xI.size());
|
||||
assert(xI.size() == xJ.size());
|
||||
|
||||
std::vector<float> yData;
|
||||
std::vector<float> xIData;
|
||||
std::vector<float> xJData;
|
||||
std::vector<float> zerosData;
|
||||
|
||||
for (size_t i = 0; i < yArr.size(); i++) {
|
||||
yData.push_back(yArr[i]);
|
||||
xIData.push_back(xIArr[i]);
|
||||
xJData.push_back(xJArr[i]);
|
||||
zerosData.push_back(0);
|
||||
}
|
||||
|
||||
uint32_t ITERATIONS = 100;
|
||||
float learningRate = 0.1;
|
||||
|
||||
std::shared_ptr<kp::Tensor> xI{ new kp::Tensor(xIData) };
|
||||
std::shared_ptr<kp::Tensor> xJ{ new kp::Tensor(xJData) };
|
||||
|
||||
std::shared_ptr<kp::Tensor> y{ new kp::Tensor(yData) };
|
||||
|
||||
std::shared_ptr<kp::Tensor> wIn{ new kp::Tensor({ 0.001, 0.001 }) };
|
||||
std::shared_ptr<kp::Tensor> wOutI{ new kp::Tensor(zerosData) };
|
||||
std::shared_ptr<kp::Tensor> wOutJ{ new kp::Tensor(zerosData) };
|
||||
|
||||
std::shared_ptr<kp::Tensor> bIn{ new kp::Tensor({ 0 }) };
|
||||
std::shared_ptr<kp::Tensor> bOut{ new kp::Tensor(zerosData) };
|
||||
|
||||
std::shared_ptr<kp::Tensor> lOut{ new kp::Tensor(zerosData) };
|
||||
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { xI, xJ, y,
|
||||
wIn, wOutI, wOutJ,
|
||||
bIn, bOut, lOut };
|
||||
|
||||
{
|
||||
kp::Manager mgr;
|
||||
|
||||
if (std::shared_ptr<kp::Sequence> sq =
|
||||
mgr.getOrCreateManagedSequence("createTensors").lock()) {
|
||||
|
||||
sq->begin();
|
||||
|
||||
sq->record<kp::OpTensorCreate>(params);
|
||||
|
||||
sq->end();
|
||||
sq->eval();
|
||||
|
||||
// Record op algo base
|
||||
sq->begin();
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
|
||||
sq->record<kp::OpAlgoBase<>>(
|
||||
params, std::vector<char>(LR_SHADER.begin(), LR_SHADER.end()));
|
||||
|
||||
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
sq->end();
|
||||
|
||||
// Iterate across all expected iterations
|
||||
for (size_t i = 0; i < ITERATIONS; i++) {
|
||||
|
||||
sq->eval();
|
||||
|
||||
for (size_t j = 0; j < bOut->size(); j++) {
|
||||
wIn->data()[0] -= learningRate * wOutI->data()[j];
|
||||
wIn->data()[1] -= learningRate * wOutJ->data()[j];
|
||||
bIn->data()[0] -= learningRate * bOut->data()[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INFO("RESULT: <<<<<<<<<<<<<<<<<<<");
|
||||
SPDLOG_INFO(wIn->data()[0]);
|
||||
SPDLOG_INFO(wIn->data()[1]);
|
||||
SPDLOG_INFO(bIn->data()[0]);
|
||||
|
||||
this->mWeights = kp::Tensor(wIn->data());
|
||||
this->mBias = kp::Tensor(bIn->data());
|
||||
}
|
||||
|
||||
Array KomputeSummator::predict(Array xI, Array xJ) {
|
||||
assert(xI.size() == xJ.size());
|
||||
|
||||
Array retArray;
|
||||
|
||||
// We run the inference in the CPU for simplicity
|
||||
// BUt you can also implement the inference on GPU
|
||||
// GPU implementation would speed up minibatching
|
||||
for (size_t i = 0; i < xI.size(); i++) {
|
||||
float xIVal = xI[i];
|
||||
float xJVal = xJ[i];
|
||||
float result = (xIVal * this->mWeights.data()[0]
|
||||
+ xJVal * this->mWeights.data()[1]
|
||||
+ this->mBias.data()[0]);
|
||||
|
||||
// Instead of using sigmoid we'll just return full numbers
|
||||
Variant var = result > 0 ? 1 : 0;
|
||||
retArray.push_back(var);
|
||||
}
|
||||
|
||||
return retArray;
|
||||
}
|
||||
|
||||
void KomputeSummator::_init() {
|
||||
std::cout << "CALLING INIT" << std::endl;
|
||||
}
|
||||
|
||||
void KomputeSummator::_process(float delta) {
|
||||
|
||||
}
|
||||
|
||||
void KomputeSummator::_register_methods() {
|
||||
register_method((char *)"_process", &KomputeSummator::_process);
|
||||
register_method((char *)"_init", &KomputeSummator::_init);
|
||||
|
||||
register_method((char *)"train", &KomputeSummator::train);
|
||||
register_method((char *)"predict", &KomputeSummator::predict);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
#pragma once
|
||||
|
||||
#include <Godot.hpp>
|
||||
#include <Node2D.hpp>
|
||||
#include <Array.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "kompute/Kompute.hpp"
|
||||
|
||||
namespace godot {
|
||||
class KomputeSummator : public Node2D {
|
||||
private:
|
||||
GODOT_CLASS(KomputeSummator, Node2D);
|
||||
|
||||
public:
|
||||
KomputeSummator();
|
||||
|
||||
void train(Array y, Array xI, Array xJ);
|
||||
Array predict(Array xI, Array xJ);
|
||||
|
||||
void _process(float delta);
|
||||
void _init();
|
||||
|
||||
static void _register_methods();
|
||||
|
||||
private:
|
||||
kp::Tensor mWeights;
|
||||
kp::Tensor 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);
|
||||
}
|
||||
)";
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue