Added .clang-format file and formatted everything

Signed-off-by: Fabian Sauter <sauter.fabian@mailbox.org>
This commit is contained in:
Fabian Sauter 2022-05-02 15:11:40 +02:00
parent f731f2e55c
commit 24cd307042
47 changed files with 5157 additions and 4354 deletions

View file

@ -4,12 +4,15 @@
#include "KomputeModelMLNode.h"
KomputeModelMLNode::KomputeModelMLNode() {
KomputeModelMLNode::KomputeModelMLNode()
{
std::cout << "CALLING CONSTRUCTOR" << std::endl;
this->_init();
}
void KomputeModelMLNode::train(Array yArr, Array xIArr, Array xJArr) {
void
KomputeModelMLNode::train(Array yArr, Array xIArr, Array xJArr)
{
assert(yArr.size() == xIArr.size());
assert(xIArr.size() == xJArr.size());
@ -52,15 +55,19 @@ void KomputeModelMLNode::train(Array yArr, Array xIArr, Array xJArr) {
{
std::vector<uint32_t> spirv(
(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> algo = mgr.algorithm(params, spirv);
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>(algo)
->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
@ -88,20 +95,22 @@ void KomputeModelMLNode::train(Array yArr, Array xIArr, Array xJArr) {
}
}
Array KomputeModelMLNode::predict(Array xI, Array xJ) {
Array
KomputeModelMLNode::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
// 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]);
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;
@ -111,12 +120,14 @@ Array KomputeModelMLNode::predict(Array xI, Array xJ) {
return retArray;
}
Array KomputeModelMLNode::get_params() {
Array
KomputeModelMLNode::get_params()
{
Array retArray;
KP_LOG_INFO(this->mWeights.size() + this->mBias.size());
if(this->mWeights.size() + this->mBias.size() == 0) {
if (this->mWeights.size() + this->mBias.size() == 0) {
return retArray;
}
@ -128,20 +139,27 @@ Array KomputeModelMLNode::get_params() {
return retArray;
}
void KomputeModelMLNode::_init() {
void
KomputeModelMLNode::_init()
{
std::cout << "CALLING INIT" << std::endl;
}
void KomputeModelMLNode::_process(float delta) {
void
KomputeModelMLNode::_process(float delta)
{}
}
void KomputeModelMLNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("_process", "delta"), &KomputeModelMLNode::_process);
void
KomputeModelMLNode::_bind_methods()
{
ClassDB::bind_method(D_METHOD("_process", "delta"),
&KomputeModelMLNode::_process);
ClassDB::bind_method(D_METHOD("_init"), &KomputeModelMLNode::_init);
ClassDB::bind_method(D_METHOD("train", "yArr", "xIArr", "xJArr"), &KomputeModelMLNode::train);
ClassDB::bind_method(D_METHOD("predict", "xI", "xJ"), &KomputeModelMLNode::predict);
ClassDB::bind_method(D_METHOD("get_params"), &KomputeModelMLNode::get_params);
ClassDB::bind_method(D_METHOD("train", "yArr", "xIArr", "xJArr"),
&KomputeModelMLNode::train);
ClassDB::bind_method(D_METHOD("predict", "xI", "xJ"),
&KomputeModelMLNode::predict);
ClassDB::bind_method(D_METHOD("get_params"),
&KomputeModelMLNode::get_params);
}

View file

@ -6,10 +6,11 @@
#include "scene/main/node.h"
class KomputeModelMLNode : public Node {
class KomputeModelMLNode : public Node
{
GDCLASS(KomputeModelMLNode, Node);
public:
public:
KomputeModelMLNode();
void train(Array y, Array xI, Array xJ);
@ -21,10 +22,10 @@ public:
void _process(float delta);
void _init();
protected:
protected:
static void _bind_methods();
private:
private:
kp::Tensor mWeights;
kp::Tensor mBias;
};
@ -85,4 +86,3 @@ void main() {
lout[idx] = calculateLoss(yHat, yCurr);
}
)";

View file

@ -2,13 +2,17 @@
#include "register_types.h"
#include "core/class_db.h"
#include "KomputeModelMLNode.h"
#include "core/class_db.h"
void register_kompute_model_ml_types() {
void
register_kompute_model_ml_types()
{
ClassDB::register_class<KomputeModelMLNode>();
}
void unregister_kompute_model_ml_types() {
// Nothing to do here in this example.
void
unregister_kompute_model_ml_types()
{
// Nothing to do here in this example.
}

View file

@ -1,6 +1,8 @@
/* register_types.h */
#pragma once
void register_kompute_model_ml_types();
void unregister_kompute_model_ml_types();
void
register_kompute_model_ml_types();
void
unregister_kompute_model_ml_types();
/* yes, the word in the middle must be the same as the module folder name */