okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
508e6999a
main
1// Strata addition (third_party/strata-ggml/UPSTREAM.md): the MMQ tile loader of the trellis types (TQ2_T, TQK6, TQK7), appended to ggml-cuda's 2// mmq-load-tiles.cuh here (an #include at its end; cmake/strata_ggml_tq_overlay.cmake does the same for a STRATA_GGML_DIR build; the agentionai fork has 3// it natively). The function is agentionai/llama.cpp's (ggml/src/ggml-cuda/mmq-load-tiles.cuh, MIT, the ggml authors, 4// written by Laurent Zuijdwijk), unchanged; the other lines of this patch only route the trellis types to it the way 5// IQ2_XS is routed (same Q8_0_16 tile layout, same dot kernels, same configs). 6#pragma once 7#include "tq.cuh" 8 9// Trellis types (TQ2_T, TQK6, TQK7): a 128-weight block is TQ_LANES lanes of 16 weights. Each lane 10// decodes its 4 trellis steps and requantizes them to int8 with one scale per 16 weights, filling 11// the Q8_0_16 tile layout (as IQ2_XS). One MMQ_ITER_K slice of a row is 2 blocks = 16 lanes. 12template <ggml_type type, int J, bool fallback> static __device__ __forceinline__ void ggml_cuda_mmq_load_tiles_tq( 13 const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) { 14 // ggml_cuda_mmq_get_util_funcs names this loader for every type; only trellis types reach it. 15 constexpr ggml_type tq_type = ggml_cuda_type_is_tq(type) ? type : GGML_TYPE_TQ2_T; 16 typedef typename tq_block_info<tq_type>::block_t block_t; 17 constexpr int warp_size = ggml_cuda_get_physical_warp_size(); 18 constexpr int nwarps = ggml_cuda_mmq_get_nthreads(type, J, fallback) / warp_size; 19 constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback); 20 constexpr int sram_stride = ggml_cuda_mmq_get_sram_stride(type, J, fallback); 21 22#if defined(AMD_MFMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 23 int * x_qs = (int *) x_tile; 24 float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2); 25#else 26 constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(type, I); 27 int * x_qs = (int *) x_tile; 28 float * x_df = (float *) (x_qs + txs.qs); 29#endif // defined(AMD_MFMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 30 31 static_assert(MMQ_ITER_K % QK_TQK == 0, "bad MMQ_ITER_K"); 32 constexpr int threads_per_row = (MMQ_ITER_K / QK_TQK) * TQ_LANES; // 16 lanes x 16 weights = 256 33 static_assert(warp_size % threads_per_row == 0, "bad warp size"); 34 constexpr int nrows = warp_size / threads_per_row; 35 const int kqsx = threadIdx.x % threads_per_row; 36 const int kbx = kqsx / TQ_LANES; 37 const int lane = kqsx % TQ_LANES; 38 39#pragma unroll 40 for (int i0 = 0; i0 < I; i0 += nwarps * nrows) { 41 int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row; 42 43 if (fallback) { 44 i = min(i, i_max); 45 } 46 47 const block_t * bxi = (const block_t *) x + kbx0 + i*stride + kbx; 48 49 int q[TQ_STEPS_LANE]; 50 const float dq = tq_lane_to_q8<tq_type>(bxi, lane, q); 51 52#pragma unroll 53 for (int k = 0; k < TQ_STEPS_LANE; ++k) { 54#if defined(AMD_MFMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 55 x_qs[i*sram_stride + 4*kqsx + k] = q[k]; 56#else 57 x_qs[i*(2*MMQ_TILE_NE_K + 1) + 4*kqsx + k] = q[k]; 58#endif // defined(AMD_MFMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 59 } 60 61#if defined(AMD_MFMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 62 x_df[i*sram_stride + kqsx] = dq; 63#else 64 x_df[i*(2*MMQ_TILE_NE_K*2/QI8_0) + i/(QI8_0/4) + kqsx] = dq; 65#endif // defined(AMD_MFMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 66 } 67}