Updated examples
This commit is contained in:
parent
63e220a8a4
commit
4fddf74ca7
11 changed files with 408 additions and 405 deletions
|
|
@ -20,61 +20,62 @@ void KomputeModelML::train(std::vector<float> yData, std::vector<float> xIData,
|
|||
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;
|
||||
|
||||
{
|
||||
mgr.rebuild(params);
|
||||
std::shared_ptr<kp::Tensor> xI = mgr.tensor(xIData);
|
||||
std::shared_ptr<kp::Tensor> xJ = mgr.tensor(xJData);
|
||||
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence();
|
||||
std::shared_ptr<kp::Tensor> y = mgr.tensor(yData);
|
||||
|
||||
// Record op algo base
|
||||
sq->begin();
|
||||
std::shared_ptr<kp::Tensor> wIn = mgr.tensor({ 0.001, 0.001 });
|
||||
std::shared_ptr<kp::Tensor> wOutI = mgr.tensor(zerosData);
|
||||
std::shared_ptr<kp::Tensor> wOutJ = mgr.tensor(zerosData);
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
std::shared_ptr<kp::Tensor> bIn = mgr.tensor({ 0 });
|
||||
std::shared_ptr<kp::Tensor> bOut = mgr.tensor(zerosData);
|
||||
|
||||
// Newer versions of Android are able to use shaderc to read raw string
|
||||
sq->record<kp::OpAlgoCreate>(
|
||||
params, kp::Shader::compile_source(LR_SHADER));
|
||||
std::shared_ptr<kp::Tensor> lOut = mgr.tensor(zerosData);
|
||||
|
||||
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { xI, xJ, y,
|
||||
wIn, wOutI, wOutJ,
|
||||
bIn, bOut, lOut };
|
||||
|
||||
sq->end();
|
||||
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));
|
||||
|
||||
// Iterate across all expected iterations
|
||||
for (size_t i = 0; i < ITERATIONS; i++) {
|
||||
std::shared_ptr<kp::Algorithm> algo =
|
||||
mgr.algorithm(params, spirv, kp::Workgroup({ 5 }), kp::Constants({ 5.0 }));
|
||||
|
||||
sq->eval();
|
||||
mgr.sequence()->eval<kp::OpTensorSyncDevice>(params);
|
||||
|
||||
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];
|
||||
}
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence()
|
||||
->record<kp::OpTensorSyncDevice>({ wIn, bIn })
|
||||
->record<kp::OpAlgoDispatch>(algo)
|
||||
->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
// 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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->mWeights = kp::Tensor(wIn->data());
|
||||
this->mBias = kp::Tensor(bIn->data());
|
||||
KP_LOG_INFO("RESULT: <<<<<<<<<<<<<<<<<<<");
|
||||
KP_LOG_INFO("{}", wIn->data()[0]);
|
||||
KP_LOG_INFO("{}", wIn->data()[1]);
|
||||
KP_LOG_INFO("{}", bIn->data()[0]);
|
||||
|
||||
this->mWeights = wIn;
|
||||
this->mBias = bIn;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<float> KomputeModelML::predict(std::vector<float> xI, std::vector<float> xJ) {
|
||||
|
|
@ -88,9 +89,9 @@ 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.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
|
||||
float var = result > 0 ? 1 : 0;
|
||||
|
|
@ -103,13 +104,13 @@ std::vector<float> KomputeModelML::predict(std::vector<float> xI, std::vector<fl
|
|||
std::vector<float> KomputeModelML::get_params() {
|
||||
std::vector<float> retVector;
|
||||
|
||||
if(this->mWeights.size() + this->mBias.size() == 0) {
|
||||
if(this->mWeights->size() + this->mBias->size() == 0) {
|
||||
return retVector;
|
||||
}
|
||||
|
||||
retVector.push_back(this->mWeights.data()[0]);
|
||||
retVector.push_back(this->mWeights.data()[1]);
|
||||
retVector.push_back(this->mBias.data()[0]);
|
||||
retVector.push_back(this->mWeights->data()[0]);
|
||||
retVector.push_back(this->mWeights->data()[1]);
|
||||
retVector.push_back(this->mBias->data()[0]);
|
||||
retVector.push_back(99.0);
|
||||
|
||||
return retVector;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include "kompute/Kompute.hpp"
|
||||
|
||||
|
|
@ -20,8 +21,8 @@ public:
|
|||
std::vector<float> get_params();
|
||||
|
||||
private:
|
||||
kp::Tensor mWeights;
|
||||
kp::Tensor mBias;
|
||||
std::shared_ptr<kp::Tensor> mWeights;
|
||||
std::shared_ptr<kp::Tensor> mBias;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -37,11 +37,14 @@ int main()
|
|||
}
|
||||
)");
|
||||
|
||||
mgr.evalOpDefault<kp::OpAlgoCreate>(
|
||||
{ tensorInA, tensorInB, tensorOut },
|
||||
kp::Shader::compile_source(shader));
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { tensorInA, tensorInB, tensorOut };
|
||||
|
||||
mgr.evalOpDefault<kp::OpTensorSyncLocal>({tensorOut});
|
||||
std::shared_ptr<kp::Algorithm> algo = mgr.algorithm(params, kp::Shader::compile_source(shader));
|
||||
|
||||
mgr.sequence()
|
||||
->record<kp::OpTensorSyncDevice>(params)
|
||||
->record<kp::OpAlgoDispatch>(algo)
|
||||
->record<kp::OpTensorSyncLocal>(params);
|
||||
|
||||
// prints "Output { 0 4 12 }"
|
||||
std::cout<< "Output: { ";
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ void KomputeSummatorNode::_init() {
|
|||
std::cout << "CALLING INIT" << std::endl;
|
||||
this->mPrimaryTensor = this->mManager.tensor({ 0.0 });
|
||||
this->mSecondaryTensor = this->mManager.tensor({ 0.0 });
|
||||
this->mSequence = this->mManager.sequence("AdditionSeq");
|
||||
this->mSequence = this->mManager.sequence();
|
||||
|
||||
// We now record the steps in the sequence
|
||||
if (std::shared_ptr<kp::Sequence> sq = this->mSequence)
|
||||
|
|
@ -51,7 +51,11 @@ void KomputeSummatorNode::_init() {
|
|||
}
|
||||
)");
|
||||
|
||||
sq->begin();
|
||||
std::shared_ptr<kp::Algorithm> algo =
|
||||
mgr.algorithm(
|
||||
{ this->mPrimaryTensor, this->mSecondaryTensor },
|
||||
kp::Shader::compile_source(shader));
|
||||
|
||||
|
||||
// First we ensure secondary tensor loads to GPU
|
||||
// No need to sync the primary tensor as it should not be changed
|
||||
|
|
@ -59,15 +63,12 @@ void KomputeSummatorNode::_init() {
|
|||
{ this->mSecondaryTensor });
|
||||
|
||||
// Then we run the operation with both tensors
|
||||
sq->record<kp::OpAlgoCreate>(
|
||||
{ this->mPrimaryTensor, this->mSecondaryTensor },
|
||||
kp::Shader::compile_source(shader));
|
||||
sq->record<kp::OpAlgoDispatch>(algo)
|
||||
|
||||
// We map the result back to local
|
||||
sq->record<kp::OpTensorSyncLocal>(
|
||||
{ this->mPrimaryTensor });
|
||||
|
||||
sq->end();
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Sequence pointer no longer available");
|
||||
|
|
|
|||
|
|
@ -29,54 +29,41 @@ void KomputeModelMLNode::train(Array yArr, Array xIArr, Array xJArr) {
|
|||
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;
|
||||
|
||||
mgr.rebuild(params);
|
||||
std::shared_ptr<kp::Tensor> xI = mgr.tensor(xIData);
|
||||
std::shared_ptr<kp::Tensor> xJ = mgr.tensor(xJData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> y = mgr.tensor(yData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> wIn = mgr.tensor({ 0.001, 0.001 });
|
||||
std::shared_ptr<kp::Tensor> wOutI = mgr.tensor(zerosData);
|
||||
std::shared_ptr<kp::Tensor> wOutJ = mgr.tensor(zerosData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> bIn = mgr.tensor({ 0 });
|
||||
std::shared_ptr<kp::Tensor> bOut = mgr.tensor(zerosData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> lOut = mgr.tensor(zerosData);
|
||||
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { xI, xJ, y,
|
||||
wIn, wOutI, wOutJ,
|
||||
bIn, bOut, lOut };
|
||||
|
||||
{
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence();
|
||||
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));
|
||||
|
||||
// Record op algo base
|
||||
sq->begin();
|
||||
std::shared_ptr<kp::Algorithm> algo = mgr.algorithm(params, spirv);
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
mgr.sequence()->eval<kp::OpTensorSyncDevice>(params);
|
||||
|
||||
#ifdef KOMPUTE_ANDROID_SHADER_FROM_STRING
|
||||
// Newer versions of Android are able to use shaderc to read raw string
|
||||
sq->record<kp::OpAlgoCreate>(
|
||||
params, std::vector<char>(LR_SHADER.begin(), LR_SHADER.end()));
|
||||
#else
|
||||
// Older versions of Android require the SPIRV binary directly
|
||||
sq->record<kp::OpAlgoCreate>(
|
||||
params, std::vector<char>(
|
||||
kp::shader_data::shaders_glsl_logisticregression_comp_spv,
|
||||
kp::shader_data::shaders_glsl_logisticregression_comp_spv
|
||||
+ kp::shader_data::shaders_glsl_logisticregression_comp_spv_len
|
||||
));
|
||||
#endif
|
||||
|
||||
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
sq->end();
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence()
|
||||
->record<kp::OpTensorSyncDevice>({ wIn, bIn })
|
||||
->record<kp::OpAlgoDispatch>(algo)
|
||||
->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
// Iterate across all expected iterations
|
||||
for (size_t i = 0; i < ITERATIONS; i++) {
|
||||
|
|
@ -90,15 +77,15 @@ void KomputeModelMLNode::train(Array yArr, Array xIArr, Array xJArr) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
KP_LOG_INFO("RESULT: <<<<<<<<<<<<<<<<<<<");
|
||||
KP_LOG_INFO(wIn->data()[0]);
|
||||
KP_LOG_INFO(wIn->data()[1]);
|
||||
KP_LOG_INFO(bIn->data()[0]);
|
||||
|
||||
this->mWeights = kp::Tensor(wIn->data());
|
||||
this->mBias = kp::Tensor(bIn->data());
|
||||
}
|
||||
|
||||
KP_LOG_INFO("RESULT: <<<<<<<<<<<<<<<<<<<");
|
||||
KP_LOG_INFO(wIn->data()[0]);
|
||||
KP_LOG_INFO(wIn->data()[1]);
|
||||
KP_LOG_INFO(bIn->data()[0]);
|
||||
|
||||
this->mWeights = kp::Tensor(wIn->data());
|
||||
this->mBias = kp::Tensor(bIn->data());
|
||||
}
|
||||
|
||||
Array KomputeModelMLNode::predict(Array xI, Array xJ) {
|
||||
|
|
|
|||
|
|
@ -33,54 +33,41 @@ void KomputeModelML::train(Array yArr, Array xIArr, Array xJArr) {
|
|||
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;
|
||||
|
||||
std::shared_ptr<kp::Tensor> xI = mgr.tensor(xIData);
|
||||
std::shared_ptr<kp::Tensor> xJ = mgr.tensor(xJData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> y = mgr.tensor(yData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> wIn = mgr.tensor({ 0.001, 0.001 });
|
||||
std::shared_ptr<kp::Tensor> wOutI = mgr.tensor(zerosData);
|
||||
std::shared_ptr<kp::Tensor> wOutJ = mgr.tensor(zerosData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> bIn = mgr.tensor({ 0 });
|
||||
std::shared_ptr<kp::Tensor> bOut = mgr.tensor(zerosData);
|
||||
|
||||
std::shared_ptr<kp::Tensor> lOut = mgr.tensor(zerosData);
|
||||
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { xI, xJ, y,
|
||||
wIn, wOutI, wOutJ,
|
||||
bIn, bOut, lOut };
|
||||
|
||||
{
|
||||
mgr.rebuild(params);
|
||||
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));
|
||||
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence();
|
||||
std::shared_ptr<kp::Algorithm> algo = mgr.algorithm(params, spirv);
|
||||
|
||||
// Record op algo base
|
||||
sq->begin();
|
||||
mgr.sequence()->eval<kp::OpTensorSyncDevice>(params);
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
|
||||
#ifdef KOMPUTE_ANDROID_SHADER_FROM_STRING
|
||||
// Newer versions of Android are able to use shaderc to read raw string
|
||||
sq->record<kp::OpAlgoCreate>(
|
||||
params, std::vector<char>(LR_SHADER.begin(), LR_SHADER.end()));
|
||||
#else
|
||||
// Older versions of Android require the SPIRV binary directly
|
||||
sq->record<kp::OpAlgoCreate>(
|
||||
params, std::vector<char>(
|
||||
kp::shader_data::shaders_glsl_logisticregression_comp_spv,
|
||||
kp::shader_data::shaders_glsl_logisticregression_comp_spv
|
||||
+ kp::shader_data::shaders_glsl_logisticregression_comp_spv_len
|
||||
));
|
||||
#endif
|
||||
|
||||
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
sq->end();
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence()
|
||||
->record<kp::OpTensorSyncDevice>({ wIn, bIn })
|
||||
->record<kp::OpAlgoDispatch>(algo)
|
||||
->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
// Iterate across all expected iterations
|
||||
for (size_t i = 0; i < ITERATIONS; i++) {
|
||||
|
|
@ -94,15 +81,15 @@ void KomputeModelML::train(Array yArr, Array xIArr, Array xJArr) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
KP_LOG_INFO("RESULT: <<<<<<<<<<<<<<<<<<<");
|
||||
KP_LOG_INFO(wIn->data()[0]);
|
||||
KP_LOG_INFO(wIn->data()[1]);
|
||||
KP_LOG_INFO(bIn->data()[0]);
|
||||
|
||||
this->mWeights = wIn;
|
||||
this->mBias = bIn;
|
||||
}
|
||||
|
||||
KP_LOG_INFO("RESULT: <<<<<<<<<<<<<<<<<<<");
|
||||
KP_LOG_INFO(wIn->data()[0]);
|
||||
KP_LOG_INFO(wIn->data()[1]);
|
||||
KP_LOG_INFO(bIn->data()[0]);
|
||||
|
||||
this->mWeights = kp::Tensor(wIn->data());
|
||||
this->mBias = kp::Tensor(bIn->data());
|
||||
}
|
||||
|
||||
Array KomputeModelML::predict(Array xI, Array xJ) {
|
||||
|
|
@ -116,9 +103,9 @@ Array KomputeModelML::predict(Array xI, Array xJ) {
|
|||
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;
|
||||
|
|
@ -131,15 +118,15 @@ Array KomputeModelML::predict(Array xI, Array xJ) {
|
|||
Array KomputeModelML::get_params() {
|
||||
Array retArray;
|
||||
|
||||
KP_LOG_INFO(this->mWeights.size() + this->mBias.size());
|
||||
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;
|
||||
}
|
||||
|
||||
retArray.push_back(this->mWeights.data()[0]);
|
||||
retArray.push_back(this->mWeights.data()[1]);
|
||||
retArray.push_back(this->mBias.data()[0]);
|
||||
retArray.push_back(this->mWeights->data()[0]);
|
||||
retArray.push_back(this->mWeights->data()[1]);
|
||||
retArray.push_back(this->mBias->data()[0]);
|
||||
retArray.push_back(99.0);
|
||||
|
||||
return retArray;
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ public:
|
|||
static void _register_methods();
|
||||
|
||||
private:
|
||||
kp::Tensor mWeights;
|
||||
kp::Tensor mBias;
|
||||
std::shared_ptr<kp::Tensor> mWeights;
|
||||
std::shared_ptr<kp::Tensor> mBias;
|
||||
};
|
||||
|
||||
static std::string LR_SHADER = R"(
|
||||
|
|
|
|||
|
|
@ -15,44 +15,39 @@ int main()
|
|||
uint32_t ITERATIONS = 100;
|
||||
float learningRate = 0.1;
|
||||
|
||||
std::shared_ptr<kp::Tensor> xI{ new kp::Tensor({ 0, 1, 1, 1, 1 }) };
|
||||
std::shared_ptr<kp::Tensor> xJ{ new kp::Tensor({ 0, 0, 0, 1, 1 }) };
|
||||
kp::Manager mgr;
|
||||
|
||||
std::shared_ptr<kp::Tensor> y{ new kp::Tensor({ 0, 0, 0, 1, 1 }) };
|
||||
std::shared_ptr<kp::Tensor> xI = mgr.tensor({ 0, 1, 1, 1, 1 });
|
||||
std::shared_ptr<kp::Tensor> xJ = mgr.tensor({ 0, 0, 0, 1, 1 });
|
||||
|
||||
std::shared_ptr<kp::Tensor> wIn{ new kp::Tensor({ 0.001, 0.001 }) };
|
||||
std::shared_ptr<kp::Tensor> wOutI{ new kp::Tensor({ 0, 0, 0, 0, 0 }) };
|
||||
std::shared_ptr<kp::Tensor> wOutJ{ new kp::Tensor({ 0, 0, 0, 0, 0 }) };
|
||||
std::shared_ptr<kp::Tensor> y = mgr.tensor({ 0, 0, 0, 1, 1 });
|
||||
|
||||
std::shared_ptr<kp::Tensor> bIn{ new kp::Tensor({ 0 }) };
|
||||
std::shared_ptr<kp::Tensor> bOut{ new kp::Tensor({ 0, 0, 0, 0, 0 }) };
|
||||
std::shared_ptr<kp::Tensor> wIn = mgr.tensor({ 0.001, 0.001 });
|
||||
std::shared_ptr<kp::Tensor> wOutI = mgr.tensor({ 0, 0, 0, 0, 0 });
|
||||
std::shared_ptr<kp::Tensor> wOutJ = mgr.tensor({ 0, 0, 0, 0, 0 });
|
||||
|
||||
std::shared_ptr<kp::Tensor> lOut{ new kp::Tensor({ 0, 0, 0, 0, 0 }) };
|
||||
std::shared_ptr<kp::Tensor> bIn = mgr.tensor({ 0 });
|
||||
std::shared_ptr<kp::Tensor> bOut = mgr.tensor({ 0, 0, 0, 0, 0 });
|
||||
|
||||
std::shared_ptr<kp::Tensor> lOut = mgr.tensor({ 0, 0, 0, 0, 0 });
|
||||
|
||||
std::vector<std::shared_ptr<kp::Tensor>> params = { xI, xJ, y,
|
||||
wIn, wOutI, wOutJ,
|
||||
bIn, bOut, lOut };
|
||||
|
||||
kp::Manager mgr;
|
||||
|
||||
mgr.rebuild(params);
|
||||
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence();
|
||||
|
||||
// Record op algo base
|
||||
sq->begin();
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
|
||||
sq->record<kp::OpAlgoCreate>(
|
||||
params, std::vector<uint32_t>(
|
||||
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)));
|
||||
+ kp::shader_data::shaders_glsl_logisticregression_comp_spv_len));
|
||||
|
||||
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
std::shared_ptr<kp::Algorithm> algo = mgr.algorithm(params, spirv);
|
||||
|
||||
sq->end();
|
||||
mgr.sequence()->eval<kp::OpTensorSyncDevice>(params);
|
||||
|
||||
std::shared_ptr<kp::Sequence> sq = mgr.sequence()
|
||||
->record<kp::OpTensorSyncDevice>({ wIn, bIn })
|
||||
->record<kp::OpAlgoDispatch>(algo)
|
||||
->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
// Iterate across all expected iterations
|
||||
for (size_t i = 0; i < ITERATIONS; i++) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue