Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/backend_gpu/include/math/broadcast_add.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <cstdint>

bool tensor_broadcast_add_op_cuda(tensor_t *t, float *d_a, float *d_b, float *d_res);
4 changes: 4 additions & 0 deletions src/backend_gpu/include/math/mean.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <cstdint>

bool tensor_mean_op_cuda(tensor_t *t, float *d_a, float *d_res);
4 changes: 4 additions & 0 deletions src/backend_gpu/include/math/relu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <cstdint>

bool tensor_relu_op_cuda(tensor_t *t, float *d_a, float *d_res);
4 changes: 4 additions & 0 deletions src/backend_gpu/include/math/scalar_mul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <cstdint>

bool tensor_scalar_mul_op_cuda(tensor_t *t, float *d_a, float *d_b, float *d_res);
4 changes: 4 additions & 0 deletions src/backend_gpu/include/math/square.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <cstdint>

bool tensor_square_op_cuda(tensor_t *t, float *d_a, float *d_res);
4 changes: 4 additions & 0 deletions src/backend_gpu/include/math/sub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <cstdint>

bool tensor_sub_op_cuda(tensor_t *t, float *d_a, float *d_b, float *d_res);
31 changes: 31 additions & 0 deletions src/backend_gpu/math/broadcast_add.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "internal_header.h"
#include <cuda_runtime.h>

__global__
static void broadcast_add_kernel(const float *a, const float *bias,
float *out,
uint32_t rows, uint32_t cols) {
uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
uint32_t total = rows * cols;
uint32_t stride = blockDim.x * gridDim.x;
for (; idx < total; idx += stride) {
uint32_t col = idx % cols;
out[idx] = a[idx] + bias[col];
}
}

bool tensor_broadcast_add_op_cuda(tensor_t *t, float *d_a, float *d_b,
float *d_res) {
uint32_t rows = t->dims[0];
uint32_t cols = t->dims[1];
uint32_t total = rows * cols;
int block = 256;
int grid = ((int)total + block - 1) / block;
broadcast_add_kernel<<<grid, block>>>(d_a, d_b, d_res, rows, cols);
cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
debug("CUDA BroadcastAdd Kernel Failed: %s\n", cudaGetErrorString(err));
return false;
}
return true;
}
77 changes: 77 additions & 0 deletions src/backend_gpu/math/mean.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "internal_header.h"
#include <cuda_runtime.h>

#define MEAN_BLOCK 256

__global__
static void reduce_sum_k(const float *in, float *partial, uint32_t n) {
extern __shared__ float smem[];
uint32_t tid = threadIdx.x;
uint32_t i = blockIdx.x * blockDim.x + tid;
smem[tid] = (i < n) ? in[i] : 0.f;
__syncthreads();
for (uint32_t s = blockDim.x >> 1; s > 0; s >>= 1) {
if (tid < s) smem[tid] += smem[tid + s];
__syncthreads();
}
if (tid == 0) partial[blockIdx.x] = smem[0];
}


__global__
static void divide_scalar_k(float *val, uint32_t n) {
/* Single-thread, runs with <<<1,1>>> */
val[0] /= (float)n;
}


bool tensor_mean_op_cuda(tensor_t *t, float *d_a, float *d_res) {
uint32_t n = t->nvalues;
int block = MEAN_BLOCK;
int grid = ((int)n + block - 1) / block;

// Allocate partial-sum buffer
float *d_partial = nullptr;
cudaError_t alloc_err = cudaMalloc(&d_partial, (size_t)grid * sizeof(float));
if (alloc_err != cudaSuccess) {
debug("CUDA Mean: failed to alloc partial buffer: %s\n",
cudaGetErrorString(alloc_err));
return false;
}

/* Pass 1 */
reduce_sum_k<<<grid, block, (size_t)block * sizeof(float)>>>(d_a, d_partial, n);

/* Pass 2 — reduce the partial sums */
int grid2 = ((int)grid + block - 1) / block;
float *d_partial2 = nullptr;
cudaMalloc(&d_partial2, (size_t)grid2 * sizeof(float));
reduce_sum_k<<<grid2, block, (size_t)block * sizeof(float)>>>(
d_partial, d_partial2, (uint32_t)grid);
// If there are still more elements, keep reducing
while (grid2 > 1) {
int grid3 = (grid2 + block - 1) / block;
float *d_tmp = nullptr;
cudaMalloc(&d_tmp, (size_t)grid3 * sizeof(float));
reduce_sum_k<<<grid3, block, (size_t)block * sizeof(float)>>>(
d_partial2, d_tmp, (uint32_t)grid2);
cudaFree(d_partial2);
d_partial2 = d_tmp;
grid2 = grid3;
}

// Copy scalar result to d_res[0] and divide
cudaMemcpy(d_res, d_partial2, sizeof(float), cudaMemcpyDeviceToDevice);
divide_scalar_k<<<1, 1>>>(d_res, n);

cudaFree(d_partial);
cudaFree(d_partial2);

cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
debug("CUDA Mean Kernel Failed: %s\n", cudaGetErrorString(err));
return false;
}
return true;
}

22 changes: 22 additions & 0 deletions src/backend_gpu/math/relu.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "internal_header.h"
#include <cuda_runtime.h>

__global__
static void relu_kernel(const float *a, float *out, uint32_t n) {
uint32_t i = blockIdx.x * blockDim.x + threadIdx.x;
uint32_t stride = blockDim.x * gridDim.x;
for (; i < n; i += stride)
out[i] = (a[i] > 0.f) ? a[i] : 0.f;
}

bool tensor_relu_op_cuda(tensor_t *t, float *d_a, float *d_res) {
int block = 256;
int grid = ((int)t->nvalues + block - 1) / block;
relu_kernel<<<grid, block>>>(d_a, d_res, t->nvalues);
cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
debug("CUDA ReLU Kernel Failed: %s\n", cudaGetErrorString(err));
return false;
}
return true;
}
26 changes: 26 additions & 0 deletions src/backend_gpu/math/scalar_mul.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "internal_header.h"
#include <cuda_runtime.h>

__global__
static void scalar_mul_kernel(const float *a, float *out,
float scalar, uint32_t n) {
uint32_t i = blockIdx.x * blockDim.x + threadIdx.x;
uint32_t stride = blockDim.x * gridDim.x;
for (; i < n; i += stride)
out[i] = a[i] * scalar;
}

bool tensor_scalar_mul_op_cuda(tensor_t *t, float *d_a,
[[maybe_unused]] float *d_b,
float *d_res) {
float s = ((float *)t->b->data)[0];
int block = 256;
int grid = ((int)t->nvalues + block - 1) / block;
scalar_mul_kernel<<<grid, block>>>(d_a, d_res, s, t->nvalues);
cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
debug("CUDA ScalarMul Kernel Failed: %s\n", cudaGetErrorString(err));
return false;
}
return true;
}
22 changes: 22 additions & 0 deletions src/backend_gpu/math/square.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "internal_header.h"
#include <cuda_runtime.h>

__global__
static void square_kernel(const float *a, float *out, uint32_t n) {
uint32_t i = blockIdx.x * blockDim.x + threadIdx.x;
uint32_t stride = blockDim.x * gridDim.x;
for (; i < n; i += stride)
out[i] = a[i] * a[i];
}

bool tensor_square_op_cuda(tensor_t *t, float *d_a, float *d_res) {
int block = 256;
int grid = ((int)t->nvalues + block - 1) / block;
square_kernel<<<grid, block>>>(d_a, d_res, t->nvalues);
cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
debug("CUDA Square Kernel Failed: %s\n", cudaGetErrorString(err));
return false;
}
return true;
}
22 changes: 22 additions & 0 deletions src/backend_gpu/math/sub.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "internal_header.h"
#include <cuda_runtime.h>

__global__
static void sub_kernel(const float *a, const float *b, float *out, uint32_t n) {
uint32_t i = blockIdx.x * blockDim.x + threadIdx.x;
uint32_t stride = blockDim.x * gridDim.x;
for (; i < n; i += stride)
out[i] = a[i] - b[i];
}

bool tensor_sub_op_cuda(tensor_t *t, float *d_a, float *d_b, float *d_res) {
int block = 256;
int grid = ((int)t->nvalues + block - 1) / block;
sub_kernel<<<grid, block>>>(d_a, d_b, d_res, t->nvalues);
cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
debug("CUDA Sub Kernel Failed: %s\n", cudaGetErrorString(err));
return false;
}
return true;
}
69 changes: 49 additions & 20 deletions src/core/tensor/tensor.cu
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ tensor_t *tensor_dtype_create(tensor_pool_t *pool, tensor_dtype_t dtype, uint32_
t->device = device_type::CPU;
t->grad_compute = grad_status;
t->is_transposed = false;
t->grad = NULL;
// Return success
return t;
}
Expand Down Expand Up @@ -190,7 +191,8 @@ bool tensor_evaluate(tensor_pool_t *pool, tensor_t *t, [[maybe_unused]]float *d
/*************************************************************/
/*************************************************************/
// TODO: Implement GPU module
bool tensor_evaluate_GPU([[maybe_unused]] tensor_pool_t *pool, [[maybe_unused]]tensor_t *t, [[maybe_unused]]float *d_a, [[maybe_unused]]float *d_b, [[maybe_unused]]float *d_res) {
bool tensor_evaluate_GPU(tensor_pool_t *pool, tensor_t *t,
float *d_a, float *d_b, float *d_res) {
assert(pool != NULL);
assert(t != NULL);
bool success = false;
Expand All @@ -201,37 +203,64 @@ bool tensor_evaluate_GPU([[maybe_unused]] tensor_pool_t *pool, [[maybe_unused]]t
break;
case tensor_op_t::CAST:
break;
// TODO: Implement here
case tensor_op_t::ADD:
assert(t->a != NULL);
assert(t->b != NULL);
assert(t->a->dtype == t->b->dtype);
assert(t->a != NULL && t->b != NULL);
success = tensor_add_op_cuda(t, d_a, d_b, d_res);
break;
case tensor_op_t::MUL_MATRIX:
case tensor_op_t::SUB:
assert(t->a != NULL && t->b != NULL);
success = tensor_sub_op_cuda(t, d_a, d_b, d_res);
break;
case tensor_op_t::RELU:
assert(t->a != NULL);
assert(t->b != NULL);
assert(t->a->dtype == t->b->dtype);
success = tensor_relu_op_cuda(t, d_a, d_res);
break;
case tensor_op_t::SQUARE:
assert(t->a != NULL);
success = tensor_square_op_cuda(t, d_a, d_res);
break;
case tensor_op_t::MEAN:
assert(t->a != NULL);
success = tensor_mean_op_cuda(t, d_a, d_res);
break;
case tensor_op_t::MUL_MATRIX:
assert(t->a != NULL && t->b != NULL);
success = tensor_mul_op_cuda(t, d_a, d_b, d_res);
break;
default:
// We are having a stopgap where if we have not written that op on GPU then CPU will handle it
std::cout << "OP NOT AVAILABLE FOR GPU SWITCHING TO CPU\n";
case tensor_op_t::NAIVE_MATRIX_MUL:
assert(t->a != NULL && t->b != NULL);
success = tensor_mul_op_cuda(t, d_a, d_b, d_res);
break;
case tensor_op_t::MUL_SCALAR:
assert(t->a != NULL && t->b != NULL);
success = tensor_scalar_mul_op_cuda(t, d_a, d_b, d_res);
break;
case tensor_op_t::BROADCAST_ADD:
assert(t->a != NULL && t->b != NULL);
success = tensor_broadcast_add_op_cuda(t, d_a, d_b, d_res);
break;
case tensor_op_t::TRANSPOSE:
// Transpose on GPU: fall back to CPU with a single round-trip.
// Transpose is an O(n) copy the overhead is acceptable.
// Cause don't know how to do transpose on GPU lol
if (t->a != nullptr && d_a != nullptr) {
cudaMemcpy(t->a->data, d_a, t->a->nvalues * sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(t->a->data, d_a,
t->a->nvalues * sizeof(float), cudaMemcpyDeviceToHost);
}
if (t->b != nullptr && d_b != nullptr) {
cudaMemcpy(t->b->data, d_b, t->b->nvalues * sizeof(float), cudaMemcpyDeviceToHost);
}
success = tensor_evaluate(pool, t, d_a, d_b, d_res);
success = tensor_tranpose_op_matrix(pool, t);
if (success && d_res != nullptr) {
cudaMemcpy(d_res, t->data, t->nvalues * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_res, t->data,
t->nvalues * sizeof(float), cudaMemcpyHostToDevice);
}
}
break;
default:
debug("tensor_evaluate_GPU: unhandled op=%d\n", (int)t->op);
success = false;
}
if (success) {
debug("tensor_evaluate_GPU: success\n");
debug("tensor_evaluate_GPU: success op=%d\n", (int)t->op);
} else {
debug("tensor_evaluate_GPU: FUBAR\n");
debug("tensor_evaluate_GPU: FUBAR op=%d\n", (int)t->op);
}
return success;
}
Expand Down
6 changes: 6 additions & 0 deletions src/internal_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@

#include "backend_gpu/include/math/add.h"
#include "backend_gpu/include/math/matmul.h"
#include "backend_gpu/include/math/sub.h"
#include "backend_gpu/include/math/relu.h"
#include "backend_gpu/include/math/square.h"
#include "backend_gpu/include/math/mean.h"
#include "backend_gpu/include/math/scalar_mul.h"
#include "backend_gpu/include/math/broadcast_add.h"
// clang-format on
Loading