From 16c56f29e15072beae037ebef9c7e585d2de3f4f Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sun, 4 Oct 2020 16:19:43 +0100 Subject: [PATCH] Added lr shader and removed old ml shader --- shaders/glsl/logisticregression.comp | 54 ++++++++++++++++++ shaders/glsl/machinelearning.comp | 84 ---------------------------- 2 files changed, 54 insertions(+), 84 deletions(-) create mode 100644 shaders/glsl/logisticregression.comp delete mode 100644 shaders/glsl/machinelearning.comp diff --git a/shaders/glsl/logisticregression.comp b/shaders/glsl/logisticregression.comp new file mode 100644 index 000000000..ff25cef79 --- /dev/null +++ b/shaders/glsl/logisticregression.comp @@ -0,0 +1,54 @@ +#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); +} diff --git a/shaders/glsl/machinelearning.comp b/shaders/glsl/machinelearning.comp deleted file mode 100644 index 461b156cf..000000000 --- a/shaders/glsl/machinelearning.comp +++ /dev/null @@ -1,84 +0,0 @@ -#version 450 - -// This variable is set by the pipeline -layout (constant_id = 0) const uint BUFFER_ELEMENTS = 32; - -layout(binding = 0) buffer Pos { - uint values[ ]; -}; - -layout (local_size_x = 4, local_size_y = 1, local_size_z = 1) in; - -float learningRate = 0.01; - -uint fibonacci(uint n) { - if(n <= 1){ - return n; - } - uint curr = 1; - uint prev = 1; - for(uint i = 2; i < n; ++i) { - uint temp = curr; - curr += prev; - prev = temp; - } - return curr; -} - -float sigmoid(float z) { - return 1.0 / (1.0 + exp(-z)); -} - -float forwardProp(float x, float w, float b) { - float z = dot(w, x) + b; - float yHat = sigmoid(z); - return yHat; -} - -void backwardProp(float x, float yHat, float y, float m, out float w, out float b) { - float dZ = yHat - y; - float dW = (1/m) * x * dZ; - float dB = (1/m) * dZ; - w -= (learningRate * dW); - b -= (learningRate * dB); -} - -float calculateLoss(float yHat, float y) { - return -(y * log(yHat) + (1.0 - y) * log(1.0 - yHat)); -} - -shared uint sharedTotal[1]; - -void main() -{ - uint index = gl_GlobalInvocationID.x; - if (index >= BUFFER_ELEMENTS) - return; - -// float m = int(BUFFER_ELEMENTS); -// float w = 0.001; -// float b = 0.0; -// -// float x = values[index]; -// -// float yHat = forwardProp(x, w, b); -// float loss = calculateLoss(yHat, x); -// -// backwardProp(x, yHat, x, m, w, b); -// -// values[index] = fibonacci(values[index]); - - sharedTotal[0] = 0; - - barrier(); - memoryBarrierShared(); - - atomicAdd(sharedTotal[0], values[index]); - - barrier(); - memoryBarrierShared(); - - values[index] = int(sharedTotal[0]); -} - -