forked from ggml-org/llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 8
q4k mmq optimizations #80
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
Open
liangliangchang
wants to merge
7
commits into
gfx11
Choose a base branch
from
lichang.q4k-opt
base: gfx11
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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.
+282
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2815b3e
HIP: optimize RDNA3.5 Q4_K MMQ pipeline
liangliangchang a06b975
HIP: fix RDNA3.5 Q4_K pipeline integration
liangliangchang c83bdac
HIP: tune Q4_K wave mapping by tile width
liangliangchang bec4a25
HIP: preserve expert-aware Q4_K MMQ tiling
liangliangchang f1cd056
HIP: remove Q4_K MMQ test override
liangliangchang f602551
HIP: extend RDNA3.5 Q4_K prefetch to J128
liangliangchang 96fc67b
HIP: batch RDNA3.5 Q4_K J128 WMMA phases
liangliangchang 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
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
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 |
|---|---|---|
|
|
@@ -180,6 +180,9 @@ struct ggml_cuda_mmq_config { | |
| constexpr __device__ int rows_per_warp() const { | ||
| #if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) | ||
| #if defined(RDNA3_5) | ||
| if (type == GGML_TYPE_Q4_K && J == 128) { | ||
| return 16; | ||
| } | ||
| return J >= 64 && J % 32 == 0 ? 32 : 16; | ||
| #else | ||
| return 16; | ||
|
|
@@ -828,7 +831,7 @@ static constexpr __device__ ggml_cuda_mmq_util_funcs ggml_cuda_mmq_get_util_func | |
| return ggml_cuda_mmq_util_funcs( | ||
| -1, | ||
| ggml_cuda_mmq_load_tiles_q4_K<type, J, fallback>, | ||
| ggml_cuda_mmq_vec_dot_q8_1_q8_1_mma<type, J, fallback>, | ||
| ggml_cuda_mmq_vec_dot_q4_K_q8_1_mma_rdna35<type, J, fallback>, | ||
|
Collaborator
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. It looks like this function is called for all AMD GPUs, not just rdna35? |
||
| ggml_cuda_mmq_write_back_mma<type, J, fallback>); | ||
| case GGML_TYPE_Q5_K: | ||
| return ggml_cuda_mmq_util_funcs( | ||
|
|
@@ -1015,6 +1018,56 @@ static __device__ __forceinline__ void mul_mat_q_process_tile( | |
|
|
||
| constexpr int sz = sizeof(block_q8_1_mmq) / sizeof(int); | ||
|
|
||
| #if defined(RDNA3_5) && defined(AMD_WMMA_AVAILABLE) && !defined(AMD_MFMA_AVAILABLE) | ||
|
Collaborator
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. Doesn't RDNA3_5 imply that WMMA is available and MFMA is not available? |
||
| if constexpr (type == GGML_TYPE_Q4_K && (J == 64 || J == 128)) { | ||
| constexpr int qs_cache_size = I/nwarps; | ||
|
|
||
| __syncthreads(); | ||
| load_tiles(x, tile_x, offset_x + kb0_start, tile_x_max_i, stride_row_x); | ||
| __syncthreads(); | ||
|
|
||
| for (int kb0 = kb0_start; kb0 < kb0_stop; kb0 += blocks_per_iter) { | ||
| const int yk = kb0*qk/ne_block; | ||
| const int * by0 = y + ncols_y*yk*sz; | ||
| const int * by1 = y + ncols_y*(yk + 1)*sz; | ||
|
|
||
| #pragma unroll | ||
| for (int l0 = 0; l0 < J*MMQ_TILE_Y_K; l0 += nwarps*warp_size) { | ||
| const int l = l0 + threadIdx.y*warp_size + threadIdx.x; | ||
| tile_y[l] = by0[l]; | ||
| } | ||
| __syncthreads(); | ||
| vec_dot(tile_x, tile_y, sum, 0); | ||
|
|
||
| __syncthreads(); | ||
| #pragma unroll | ||
| for (int l0 = 0; l0 < J*MMQ_TILE_Y_K; l0 += nwarps*warp_size) { | ||
| const int l = l0 + threadIdx.y*warp_size + threadIdx.x; | ||
| tile_y[l] = by1[l]; | ||
| } | ||
| __syncthreads(); | ||
|
|
||
| int qs_cache[qs_cache_size]; | ||
| int scales_cache[3]; | ||
| half2 dm_cache; | ||
| const int kb0_next = kb0 + blocks_per_iter; | ||
| const bool have_next = kb0_next < kb0_stop; | ||
| if (have_next) { | ||
| ggml_cuda_mmq_prefetch_tiles_q4_K_rdna35<type, J, fallback>( | ||
| x, offset_x + kb0_next, tile_x_max_i, stride_row_x, qs_cache, scales_cache, dm_cache); | ||
| } | ||
|
|
||
| vec_dot(tile_x, tile_y, sum, MMQ_TILE_NE_K); | ||
| __syncthreads(); | ||
|
|
||
| if (have_next) { | ||
| ggml_cuda_mmq_store_tiles_q4_K_rdna35<type, J, fallback>( | ||
| tile_x, qs_cache, scales_cache, dm_cache); | ||
| } | ||
| __syncthreads(); | ||
| } | ||
| } else { | ||
| #endif | ||
| #if defined(RDNA3_5) | ||
| constexpr int tile_y_elems = J*MMQ_TILE_Y_K; | ||
| constexpr int tile_y_load_stride = nwarps*warp_size; | ||
|
|
@@ -1097,6 +1150,9 @@ static __device__ __forceinline__ void mul_mat_q_process_tile( | |
| mmq_hip_tile_barrier<J>(); | ||
| } | ||
| } | ||
| #if defined(RDNA3_5) && defined(AMD_WMMA_AVAILABLE) && !defined(AMD_MFMA_AVAILABLE) | ||
| } | ||
| #endif | ||
|
|
||
| if (fixup) { | ||
| write_back(sum, ids_dst, tmp_fixup + blockIdx.x*(J*I), y_scale, I, I, J); | ||
|
|
@@ -1831,6 +1887,24 @@ void mul_mat_q_switch_J(ggml_backend_cuda_context & ctx, const mmq_args & args, | |
| J_best = J_tuned; | ||
| } | ||
| } | ||
|
|
||
| if constexpr (type == GGML_TYPE_Q4_K) { | ||
| constexpr int q4_k_J_default = 128; | ||
| constexpr int q4_k_J_small = 64; | ||
| constexpr int q4_k_m_small_max = 1024; | ||
| constexpr int q4_k_ncols_pipeline = 128; | ||
| const bool use_q4_k_pipeline = | ||
| args.expert_bounds == nullptr && args.ncols_max == q4_k_ncols_pipeline; | ||
| if (use_q4_k_pipeline) { | ||
| const bool use_small = args.nrows_x <= q4_k_m_small_max; | ||
| const int q4_k_J = use_small ? q4_k_J_small : q4_k_J_default; | ||
| const ggml_cuda_mmq_config config = ggml_cuda_mmq_get_config(type, q4_k_J, fallback, cc); | ||
| if (GGML_CUDA_CC_IS_RDNA3_5(cc) && | ||
| config.type != GGML_TYPE_COUNT && mmq_get_nbytes_shared(config, cc) <= smpbo) { | ||
| J_best = q4_k_J; | ||
| } | ||
| } | ||
| } | ||
| #endif // GGML_USE_HIP | ||
|
|
||
| switch (J_best) { | ||
|
|
||
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.
Why is this fixed to exactly 128?