Renamed
This commit is contained in:
parent
4e3802cb9d
commit
7906406dd1
4 changed files with 0 additions and 0 deletions
40
examples/logistic_regression/CMakeLists.txt
Normal file
40
examples/logistic_regression/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
cmake_minimum_required(VERSION 3.17.0)
|
||||
project(kompute_linear_reg VERSION 0.1.0)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
option(KOMPUTE_OPT_ENABLE_SPDLOG "Extra compile flags for Kompute, see docs for full list" 0)
|
||||
set(KOMPUTE_EXTRA_CXX_FLAGS "" CACHE STRING "Extra compile flags for Kompute, see docs for full list")
|
||||
|
||||
if(KOMPUTE_OPT_ENABLE_SPDLOG)
|
||||
set(KOMPUTE_EXTRA_CXX_FLAGS "${KOMPUTE_EXTRA_CXX_FLAGS} -DKOMPUTE_ENABLE_SPDLOG=1")
|
||||
endif()
|
||||
|
||||
# It is necessary to pass the DEBUG or RELEASE flag accordingly to Kompute
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG=1 ${KOMPUTE_EXTRA_CXX_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DRELEASE=1 ${KOMPUTE_EXTRA_CXX_FLAGS}")
|
||||
|
||||
find_package(kompute REQUIRED)
|
||||
find_package(Vulkan REQUIRED)
|
||||
|
||||
if(KOMPUTE_OPT_ENABLE_SPDLOG)
|
||||
find_package(spdlog REQUIRED)
|
||||
find_package(fmt REQUIRED)
|
||||
endif()
|
||||
|
||||
add_executable(kompute_linear_reg
|
||||
src/Main.cpp)
|
||||
|
||||
target_link_libraries(kompute_linear_reg
|
||||
kompute::kompute
|
||||
Vulkan::Vulkan
|
||||
)
|
||||
|
||||
if(KOMPUTE_OPT_ENABLE_SPDLOG)
|
||||
target_link_libraries(kompute_linear_reg
|
||||
kompute::kompute
|
||||
fmt::fmt
|
||||
spdlog::spdlog
|
||||
)
|
||||
endif()
|
||||
|
||||
74
examples/logistic_regression/README.md
Normal file
74
examples/logistic_regression/README.md
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# Kompute Logistic Regression Example
|
||||
|
||||
This folder contains an end to end Kompute Example that implements logistic regression.
|
||||
|
||||
This example is structured such that you will be able to extend it for your project.
|
||||
|
||||
It contains a cmake build configuration that can be used in your production applications.
|
||||
|
||||
## Pre-requisites
|
||||
|
||||
In order to run this example, you will need the following dependencies:
|
||||
|
||||
* REQUIRED
|
||||
+ Vulkan Kompute library must be accessible
|
||||
+ The Vulkan SDK must be installed
|
||||
* OPTIONAL
|
||||
+ SPDLOG - for logging
|
||||
+ FMT - for text formatting
|
||||
|
||||
We will cover how you can install Vulkan Kompute in the next section.
|
||||
|
||||
For the Vulkan SDK, the simplest way to install it is through [their website](https://vulkan.lunarg.com/sdk/home). You just have to follow the instructions for the relevant platform.
|
||||
|
||||
For the other libraries, because they are optional you can just make sure you build and install Kompute with these disabled (this will be covered in more detail below).
|
||||
|
||||
Alternatively you can use package managers such as vcpkg to help you install them, although to simplify things you can start without the dependencies first.
|
||||
|
||||
## Set Up Vulkan Kompute Dependency
|
||||
|
||||
You have multiple options to set up Vulkan Kompute. The easiest is to perform a local installation.
|
||||
|
||||
For this, you will want to go to the main repo and run the following cmake command, which will configure it without SPDLOG by default.
|
||||
|
||||
```
|
||||
cmake \
|
||||
-Bbuild
|
||||
```
|
||||
|
||||
You can pass the following optional parameters based on your desired configuration:
|
||||
* If you wish to install with spdlog support you just have to pass `-DKOMPUTE_ENABLE_SPDLOG=1`.
|
||||
* If you wish to perform the installation on the local folder instead of in your system you can use `-DCMAKE_INSTALL_PREFIX="build/src/CMakeFiles/Export/"` which will basically ensure that the final files are created in the local directory.
|
||||
* If you are using a package manager such as `vcpkg` make sure you pass the `-DCMAKE_TOOLCHAIN_FILE=` parameter
|
||||
|
||||
Then you can proceed to run the installation:
|
||||
|
||||
* For Windows / Visual Studio you just have to build `INSTALL.vcxproj`
|
||||
* For Linux you can just run the `install` target via `make -C build install`
|
||||
|
||||
You also have the option to build as `Release` or `Debug` - just make sure that you build your example with the same build/debug flags as required.
|
||||
|
||||
## Building the example
|
||||
|
||||
Now that you've set up the dependencies / installation of Vulkan Kompute you can build this example.
|
||||
|
||||
You will notice that it's a standalone project, so you can re-use it for your application.
|
||||
|
||||
To build you just need to run the cmake command in this folder as follows:
|
||||
|
||||
```
|
||||
cmake \
|
||||
-Bbuild
|
||||
```
|
||||
|
||||
Make sure to pass the required flags depending on the configuration above:
|
||||
* If you built with Debug make sure you build your example with Debug as well
|
||||
* If you installed in the local folder, make sure you pass the CMAKE_PREFIX_PATH pointing to the respective folder (e.g. `-DCMAKE_PREFIX_PATH=../../build/src/CMakeFiles/Export/lib/cmake/kompute/` if parent folder is main repo).
|
||||
* If you built Vulkan Kompute with spdlog enabled, make sure to pass `-DKOMPUTE_OPT_ENABLE_SPDLOG=1`
|
||||
* If you are using a package manager such as `vcpkg` make sure you pass the `-DCMAKE_TOOLCHAIN_FILE=` parameter
|
||||
|
||||
Now you just have to build your application as above:
|
||||
|
||||
* For Windows / Visual Studio you just have to build and run `kompute_linear_reg.vcxproj`
|
||||
* For Linux you can just run the `kompute_linear_reg` target via `make -C build kompute_linear_reg`
|
||||
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
#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);
|
||||
}
|
||||
|
||||
|
||||
77
examples/logistic_regression/src/Main.cpp
Executable file
77
examples/logistic_regression/src/Main.cpp
Executable file
|
|
@ -0,0 +1,77 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "kompute/Kompute.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
#if KOMPUTE_ENABLE_SPDLOG
|
||||
spdlog::set_level(
|
||||
static_cast<spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL));
|
||||
#endif
|
||||
|
||||
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 }) };
|
||||
|
||||
std::shared_ptr<kp::Tensor> y{ new kp::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> bIn{ new kp::Tensor({ 0 }) };
|
||||
std::shared_ptr<kp::Tensor> bOut{ new kp::Tensor({ 0, 0, 0, 0, 0 }) };
|
||||
|
||||
std::shared_ptr<kp::Tensor> lOut{ new kp::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;
|
||||
|
||||
std::weak_ptr<kp::Sequence> sqWeakPtr = mgr.getOrCreateManagedSequence("createTensors");
|
||||
std::shared_ptr<kp::Sequence> sq = sqWeakPtr.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, "shaders/glsl/logistic_regression.comp");
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "RESULTS" << std::endl;
|
||||
std::cout << "w1: " << wIn->data()[0] << std::endl;
|
||||
std::cout << "w2: " << wIn->data()[1] << std::endl;
|
||||
std::cout << "b: " << bIn->data()[0] << std::endl;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue