char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
867229003
char/aliceai-t5-moe
1// 2// MIT license 3// Copyright (C) 2025 Codeplay Software Ltd. 4// Copyright (C) 2025 Intel Corporation 5// SPDX-License-Identifier: MIT 6// 7 8// 9// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 10// See https://llvm.org/LICENSE.txt for license information. 11// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 12// 13 14#ifndef GGML_SYCL_QUANTS_HPP 15#define GGML_SYCL_QUANTS_HPP 16 17#include <utility> 18 19#include "ggml-common.h" 20#include "ggml.h" 21 22namespace ggml_sycl_reordered { 23 24// The reordered block moves quants (qs) and scales(d) to two 25// uniform regions of memory that is contiguous in the same tensor. 26// What this means is that instead of having: 27// [d0, qs0] [d1, qs1] [d2, qs2] ... [dN, qsN] 28// We have: 29// [qs0, qs1, qs2, ..., qsN] [d0, d1, d2, ..., dN] 30// 31// Notes: out-of-bounds qs will run into d values 32// Alignment relies on the allocated size of qs 33 34template < ggml_type type > struct block_q_t ; 35 36// qk number of weights / quants in a block 37// qr number of weights in a byte (described as 'before dequantization') 38// for quantization types that has low and high bits split, qr is calculated with 39// using the lower bits, e.g for Q6 quants QR6 is 2 40// qi number of 32 bit integers needed to represent all the quants from a block (`qs` field) 41// See ggml-common.h to see how these are calculated 42template <> struct block_q_t < GGML_TYPE_Q4_0 > { 43struct traits { 44static constexpr uint32_t qk = QK4_0 ; 45static constexpr uint32_t qi = QI4_0 ; 46static constexpr uint32_t qr = QR4_0 ; 47static constexpr uint32_t vdr_mmvq = 2 ; 48 }; 49 50static constexpr std::pair < int ,int > get_block_offset (const int block_index ,const int /* nblocks */ ) { 51return {block_index * (QK4_0 /QR4_0 ),0 }; 52 } 53 54static constexpr std::pair < int ,int > get_d_offset (int nrows ,int ncols ,const int block_index ) { 55return { (ncols /QR4_0 * nrows )+ block_index * sizeof (ggml_half ),0 }; 56 } 57 58static constexpr int block_to_q8_1_ratio () {return traits::qk /QK8_1 ; } 59}; 60 61template <> struct block_q_t < GGML_TYPE_Q2_K > { 62struct traits { 63static constexpr uint32_t qk = QK_K ; 64static constexpr uint32_t qi = QI2_K ; 65static constexpr uint32_t qr = QR2_K ; 66static constexpr uint32_t vdr_mmvq = 1 ; 67 }; 68 69// Reordered layout: [qs (QK_K/4 per block)] [scales (QK_K/16 per block)] [dm] 70static constexpr std::pair < int ,int > get_block_offset (const int block_index ,const int /* n_blocks */ ) { 71return {block_index * (QK_K /4 ),0 }; 72 } 73 74static constexpr std::pair < int ,int > get_d_offset (int nrows ,int ncols ,const int block_index ) { 75auto nblocks = (nrows * (ncols /QK_K )); 76auto total_qs_bytes = nblocks * (QK_K /4 ); 77return {total_qs_bytes + block_index * (QK_K /16 ), 78total_qs_bytes + nblocks * (QK_K /16 )+ block_index * sizeof (ggml_half2 ) }; 79 } 80 81static constexpr int block_to_q8_1_ratio () {return traits::qk /QK8_1 ; } 82}; 83 84template <> struct block_q_t < GGML_TYPE_Q3_K > { 85struct traits { 86static constexpr uint32_t qk = QK_K ; 87static constexpr uint32_t qi = QI3_K ; 88static constexpr uint32_t qr = QR3_K ; 89static constexpr uint32_t vdr_mmvq = 1 ; 90 }; 91 92// Reordered layout: [qs (QK_K/4 per block)] [hmask (QK_K/8 per block)] [scales] [d] 93static constexpr std::pair < int ,int > get_block_offset (const int block_index ,const int n_blocks ) { 94auto qs_offset = block_index * (QK_K /4 ); 95auto hmask_offset = n_blocks * (QK_K /4 )+ block_index * (QK_K /8 ); 96return {qs_offset ,hmask_offset }; 97 } 98 99static constexpr std::pair < int ,int > get_d_offset (int nrows ,int ncols ,const int block_index ) { 100auto nblocks = (nrows * (ncols /QK_K )); 101auto total_qs_bytes = nblocks * (QK_K /4 )+ nblocks * (QK_K /8 ); 102return {total_qs_bytes + block_index * 12 , 103total_qs_bytes + nblocks * 12 + block_index * sizeof (ggml_half ) }; 104 } 105 106static constexpr int block_to_q8_1_ratio () {return traits::qk /QK8_1 ; } 107}; 108 109template <> struct block_q_t < GGML_TYPE_Q4_K > { 110struct traits { 111static constexpr uint32_t qk = QK_K ; 112static constexpr uint32_t qi = QI4_K ; 113static constexpr uint32_t qr = QR4_K ; 114static constexpr uint32_t vdr_mmvq = 2 ; 115 }; 116 117static constexpr std::pair < int ,int > get_block_offset (const int block_index ,const int /* nblocks */ ) { 118return {block_index * (traits::qk / traits::qr ),0 }; 119 } 120 121static constexpr std::pair < int ,int > get_d_offset (int nrows ,int ncols ,const int block_index ) { 122auto nblocks = (nrows * (ncols /QK_K )); 123return {nblocks * (QK_K /2 )+ (block_index * K_SCALE_SIZE ), 124 (nblocks * QK_K /2 )+ (nblocks * K_SCALE_SIZE )+ (block_index * sizeof (ggml_half2 )) }; 125 } 126 127static constexpr int block_to_q8_1_ratio () {return traits::qk /QK8_1 ; } 128}; 129 130template <> struct block_q_t < GGML_TYPE_Q5_K > { 131struct traits { 132static constexpr uint32_t qk = QK_K ; 133static constexpr uint32_t qi = QI5_K ; 134static constexpr uint32_t qr = QR5_K ; 135static constexpr uint32_t vdr_mmvq = 2 ; 136 }; 137 138// Reordered layout: [qs (QK_K/2 per block)] [qh (QK_K/8 per block)] [scales] [dm] 139static constexpr std::pair < int ,int > get_block_offset (const int block_index ,const int n_blocks ) { 140auto qs_offset = block_index * (QK_K /2 ); 141auto qh_offset = n_blocks * (QK_K /2 )+ block_index * (QK_K /8 ); 142return {qs_offset ,qh_offset }; 143 } 144 145static constexpr std::pair < int ,int > get_d_offset (int nrows ,int ncols ,const int block_index ) { 146auto nblocks = (nrows * (ncols /QK_K )); 147auto total_qs_bytes = nblocks * (QK_K /2 )+ nblocks * (QK_K /8 ); 148return {total_qs_bytes + block_index * K_SCALE_SIZE , 149total_qs_bytes + nblocks * K_SCALE_SIZE + block_index * sizeof (ggml_half2 ) }; 150 } 151 152static constexpr int block_to_q8_1_ratio () {return traits::qk /QK8_1 ; } 153}; 154 155template <> struct block_q_t < GGML_TYPE_Q6_K > { 156struct traits { 157static constexpr uint32_t qk = QK_K ; 158static constexpr uint32_t qi = QI6_K ; 159static constexpr uint32_t qr = QR6_K ; 160static constexpr uint32_t vdr_mmvq = 1 ; 161 }; 162 163static constexpr std::pair < int ,int > get_block_offset (const int block_index ,const int n_blocks ) { 164auto low_bits_index = block_index * (QK_K /QR6_K ); 165// the index of high bits it's after all low bits 166auto high_bits_index = n_blocks * (QK_K /2 )+ (block_index * (QK_K /4 )); 167return {low_bits_index ,high_bits_index }; 168 } 169 170static constexpr std::pair < int ,int > get_d_offset (int nrows ,int ncols ,const int block_index ) { 171auto nblocks = (nrows * (ncols /QK_K )); 172auto total_qs_bytes = nblocks * (QK_K /2 )+ nblocks * (QK_K /4 ); 173auto block_scales = total_qs_bytes + block_index * (QK_K /16 ); 174auto sb_scale = total_qs_bytes + nblocks * (QK_K /16 )+ block_index * sizeof (ggml_half ); 175return {block_scales ,sb_scale }; 176 } 177 178static constexpr int block_to_q8_1_ratio () {return traits::qk /QK8_1 ; } 179}; 180 181template <> struct block_q_t < GGML_TYPE_Q8_0 > { 182struct traits { 183static constexpr uint32_t qk = QK8_0 ;// 32 184static constexpr uint32_t qi = QI8_0 ;// 8 185static constexpr uint32_t qr = QR8_0 ;// 1 186static constexpr uint32_t vdr_mmvq = 4 ; 187 }; 188 189// Q8_0 reorder layout: [qs0|qs1|...|qsN][d0|d1|...|dN] 190// Each block has 32 int8 weights (32 bytes) followed by all scales 191static constexpr std::pair < int ,int > get_block_offset (const int block_index ,const int /* nblocks */ ) { 192return {block_index * QK8_0 ,0 }; 193 } 194 195static constexpr std::pair < int ,int > get_d_offset (int nrows ,int ncols ,const int block_index ) { 196return { (ncols * nrows )+ block_index * sizeof (ggml_half ),0 }; 197 } 198 199static constexpr int block_to_q8_1_ratio () {return traits::qk /QK8_1 ; }// 1 200}; 201 202}// namespace ggml_sycl_reordered 203 204#endif // GGML_SYCL_QUANTS_HPP