-
Notifications
You must be signed in to change notification settings - Fork 926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: cuda implementation for ggml_conv_transpose_1d
#854
Open
balisujohn
wants to merge
11
commits into
ggerganov:master
Choose a base branch
from
balisujohn:dev-conv-transpose-1d-cuda
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+840
−1
Open
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
70de8b7
conv transpose 1d passing test for 1d input and kernel
balisujohn f6883de
working for different input and output channel counts, added test for…
balisujohn f35d3ec
initial draft appears to work with stride other than 1
balisujohn 53a4fcf
working with all old and new conv1d tests
balisujohn f3bb758
added a test for large tensors
balisujohn 7eff0ab
removed use cuda hardcoding
balisujohn 152e04e
restored test-conv-transpose.c
balisujohn 5d39cd4
removed unused arugments, and fixed bug where test failure would caus…
balisujohn 2e7445e
fixed accumulator bug
balisujohn ed3b788
added test to test-backend-ops
balisujohn da3d0d1
fixed mistake
balisujohn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "conv-transpose-1d.cuh" | ||
|
||
static __global__ void conv_transpose_1d_kernel( | ||
const int s0, const int p0, const int d0, const int output_size, | ||
const int src0_ne0, const int src0_ne1, const int src0_ne2, const int src0_ne3, | ||
const int src1_ne0, const int src1_ne1, const int src1_ne2, const int src1_ne3, | ||
const int dst_ne0, const int dst_ne1, const int dst_ne2, const int dst_ne3, | ||
const float * src0, const float * src1, float * dst) { | ||
int global_index = threadIdx.x + blockIdx.x * blockDim.x; | ||
if (global_index >= output_size) { | ||
return; | ||
} | ||
|
||
int out_index = global_index / dst_ne0; | ||
|
||
int accumulator = 0; | ||
|
||
for (int c = 0; c < src0_ne2; c++) | ||
{ | ||
|
||
int idx = global_index % dst_ne0; | ||
|
||
int kernel_offset = (src0_ne0 * src0_ne1 * c) + (out_index * src0_ne0); | ||
int input_offset = src1_ne0 * c; | ||
|
||
int initial_weight_idx = idx > src0_ne0 -1 ? src0_ne0-1 : idx; | ||
|
||
for (int i = 0; i < src1_ne0; i++) | ||
{ | ||
if (!(idx >= i*s0 && idx < i*s0 + src0_ne0)) | ||
{ | ||
continue; | ||
} | ||
int weight_idx = idx - i*s0; | ||
|
||
|
||
int kernel_weight = src0[kernel_offset + weight_idx]; | ||
int input_value = src1[input_offset+i]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
accumulator += kernel_weight * input_value; | ||
} | ||
} | ||
dst[global_index] = accumulator; | ||
} | ||
|
||
static void conv_transpose_1d_f32_f32_cuda( | ||
const int s0, const int p0, const int d0, const int output_size, | ||
const int src0_ne0, const int src0_ne1, const int src0_ne2, const int src0_ne3, | ||
const int src1_ne0, const int src1_ne1, const int src1_ne2, const int src1_ne3, | ||
const int dst_ne0, const int dst_ne1, const int dst_ne2, const int dst_ne3, | ||
const float * src0, const float * src1, float * dst, | ||
cudaStream_t stream) { | ||
|
||
const int num_blocks = (output_size + CUDA_CONV_TRANPOSE_1D_BLOCK_SIZE - 1) / CUDA_CONV_TRANPOSE_1D_BLOCK_SIZE; | ||
conv_transpose_1d_kernel<<<num_blocks,CUDA_CONV_TRANPOSE_1D_BLOCK_SIZE, 0, stream>>>(s0,p0,d0,output_size, | ||
src0_ne0, src0_ne1, src0_ne2, src0_ne3, | ||
src1_ne0, src1_ne1, src1_ne2, src1_ne3, | ||
dst_ne0, dst_ne1, dst_ne2, dst_ne3, | ||
src0,src1, dst); | ||
} | ||
|
||
void ggml_cuda_op_conv_transpose_1d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { | ||
const ggml_tensor * src0 = dst->src[0]; | ||
const float * src0_d = (const float *)src0->data; | ||
|
||
const ggml_tensor * src1 = dst->src[1]; | ||
const float * src1_d = (const float *)src1->data; | ||
|
||
float * dst_d = (float *)dst->data; | ||
cudaStream_t stream = ctx.stream(); | ||
|
||
GGML_ASSERT(src0->type == GGML_TYPE_F32); | ||
GGML_ASSERT( dst->type == GGML_TYPE_F32); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also assert contiguous |
||
|
||
const int32_t * opts = (const int32_t *)dst->op_params; | ||
|
||
const int s0 = dst->op_params[0]; | ||
const int p0 = 0;//opts[3]; | ||
const int d0 = 1;//opts[4]; | ||
|
||
const int64_t kernel_size = ggml_nelements(src0); | ||
const int64_t input_size = ggml_nelements(src1); | ||
const int64_t output_size = ggml_nelements(dst); | ||
|
||
|
||
conv_transpose_1d_f32_f32_cuda( s0,p0,d0,output_size, | ||
src0->ne[0],src0->ne[1],src0->ne[2],src0->ne[3], | ||
src1->ne[0],src1->ne[1],src1->ne[2],src1->ne[3], | ||
dst->ne[0],dst->ne[1],dst->ne[2],dst->ne[3], | ||
src0_d, src1_d, dst_d, stream); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "common.cuh" | ||
|
||
#define CUDA_CONV_TRANPOSE_1D_BLOCK_SIZE 256 | ||
|
||
void ggml_cuda_op_conv_transpose_1d(ggml_backend_cuda_context & ctx, ggml_tensor * dst); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int
->float
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that seems to fix the issue I was experiencing; it's bizarre that the tests still passed even in cuda mode with the types accidentally set to
int
Thanks so much!