-
Notifications
You must be signed in to change notification settings - Fork 20.4k
CUDA: loop over ne2*ne3 in case it overflows #19538
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,31 +8,34 @@ | |
| template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t> | ||
| static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, | ||
| const int64_t ne00, const int64_t ne01, const int64_t ne02, | ||
| const int64_t s01, const int64_t s02, const int64_t s03) { | ||
| const int64_t ne0203, const int64_t s01, const int64_t s02, const int64_t s03) { | ||
| const int64_t i00 = 2 * (int64_t(blockDim.x)*blockIdx.x + threadIdx.x); | ||
|
|
||
| if (i00 >= ne00) { | ||
| return; | ||
| } | ||
|
|
||
| const int64_t i01 = blockIdx.y; | ||
| const int64_t i02 = blockIdx.z % ne02; | ||
| const int64_t i03 = blockIdx.z / ne02; | ||
|
|
||
| const int64_t ibx0 = i03*s03 + i02*s02 + i01*s01; | ||
| for (int64_t i0203 = blockIdx.z; i0203 < ne0203; i0203 += gridDim.z) { | ||
| const int64_t i02 = i0203 % ne02; | ||
| const int64_t i03 = i0203 / ne02; | ||
|
|
||
| const int64_t ib = ibx0 + i00/qk; // block index | ||
| const int64_t iqs = (i00%qk)/qr; // quant index | ||
| const int64_t iybs = i00 - i00%qk; // y block start index | ||
| const int64_t y_offset = qr == 1 ? 1 : qk/2; | ||
| const int64_t ibx0 = i03*s03 + i02*s02 + i01*s01; | ||
|
|
||
| // dequantize | ||
| float2 v; | ||
| dequantize_kernel(vx, ib, iqs, v); | ||
| const int64_t ib = ibx0 + i00/qk; // block index | ||
| const int64_t iqs = (i00%qk)/qr; // quant index | ||
| const int64_t iybs = i00 - i00%qk; // y block start index | ||
| const int64_t y_offset = qr == 1 ? 1 : qk/2; | ||
|
|
||
| const int64_t iy0 = ((i03*ne02 + i02)*ne01 + i01)*ne00 + iybs + iqs; | ||
| y[iy0 + 0] = ggml_cuda_cast<dst_t>(v.x); | ||
| y[iy0 + y_offset] = ggml_cuda_cast<dst_t>(v.y); | ||
| // dequantize | ||
| float2 v; | ||
| dequantize_kernel(vx, ib, iqs, v); | ||
|
|
||
| const int64_t iy0 = ((i03*ne02 + i02)*ne01 + i01)*ne00 + iybs + iqs; | ||
| y[iy0 + 0] = ggml_cuda_cast<dst_t>(v.x); | ||
| y[iy0 + y_offset] = ggml_cuda_cast<dst_t>(v.y); | ||
| } | ||
| } | ||
|
|
||
| template <bool need_check> | ||
|
|
@@ -485,9 +488,10 @@ template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t> | |
| static void dequantize_block_cuda(const void * vx, dst_t * y, | ||
| const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03, | ||
| const int64_t s01, const int64_t s02, const int64_t s03, cudaStream_t stream) { | ||
| const dim3 num_blocks((ne00 + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE), ne01, ne02*ne03); | ||
| const int64_t ne0203 = ne02*ne03; | ||
| const dim3 num_blocks((ne00 + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE), ne01, (int)std::min(ne0203, (int64_t)65535)); | ||
| dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>> | ||
| (vx, y, ne00, ne01, ne02, s01, s02, s03); | ||
| (vx, y, ne00, ne01, ne02, ne0203, s01, s02, s03); | ||
| } | ||
|
|
||
| template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t> | ||
|
|
@@ -613,31 +617,35 @@ static void dequantize_row_mxfp4_cuda(const void * vx, dst_t * y, const int64_t | |
| template <typename src_t, typename dst_t> | ||
| static __global__ void convert_unary( | ||
| const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t ne00, const int64_t ne01, const int64_t ne02, | ||
| const int64_t s01, const int64_t s02, const int64_t s03) { | ||
| const int64_t ne0203, const int64_t s01, const int64_t s02, const int64_t s03) { | ||
| const int64_t i00 = (int64_t)blockDim.x*blockIdx.x + threadIdx.x; | ||
|
|
||
| if (i00 >= ne00) { | ||
| return; | ||
| } | ||
|
|
||
| const int64_t i01 = blockIdx.y; | ||
| const int64_t i02 = blockIdx.z % ne02; | ||
| const int64_t i03 = blockIdx.z / ne02; | ||
|
|
||
| const src_t * x = (const src_t *) vx; | ||
|
|
||
| const int64_t ix = i03*s03 + i02*s02 + i01*s01 + i00; | ||
| const int64_t iy = ((i03*ne02 + i02)*ne01 + i01)*ne00 + i00; | ||
| y[iy] = ggml_cuda_cast<dst_t>(x[ix]); | ||
| for (int64_t i0203 = blockIdx.z; i0203 < ne0203; i0203 += gridDim.z) { | ||
| const int64_t i02 = i0203 % ne02; | ||
| const int64_t i03 = i0203 / ne02; | ||
|
Contributor
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. Same as above. |
||
|
|
||
| const int64_t ix = i03*s03 + i02*s02 + i01*s01 + i00; | ||
| const int64_t iy = ((i03*ne02 + i02)*ne01 + i01)*ne00 + i00; | ||
| y[iy] = ggml_cuda_cast<dst_t>(x[ix]); | ||
| } | ||
| } | ||
|
|
||
| template <typename src_t, typename dst_t> | ||
| static void convert_unary_cuda(const void * vx, dst_t * y, | ||
| const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03, | ||
| const int64_t s01, const int64_t s02, const int64_t s03, cudaStream_t stream) { | ||
| const dim3 num_blocks((ne00 + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE, ne01, ne02*ne03); | ||
| const int64_t ne0203 = ne02*ne03; | ||
| const dim3 num_blocks((ne00 + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE, ne01, (int)std::min(ne0203, (int64_t)65535)); | ||
| convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>> | ||
| (vx, y, ne00, ne01, ne02, s01, s02, s03); | ||
| (vx, y, ne00, ne01, ne02, ne0203, s01, s02, s03); | ||
| } | ||
|
|
||
| template <typename src_t, typename dst_t> | ||
|
|
||
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.
Preferably use
fastdivhere.