Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Sync with llama.cpp upstream
Justine Tunney133b05e4b26d
diff --git a/build/config.mk b/build/config.mkindex cd1db0ef..a8d15efb 100644--- a/build/config.mk+++ b/build/config.mk@@ -14,8 +14,8 @@ INSTALL = installARFLAGS = rcsDCCFLAGS = -g -O3 -fexceptions-CPPFLAGS_ = -iquote. -mcosmoTARGET_ARCH = -Xx86_64-mssse3+CPPFLAGS_ = -iquote. -mcosmo -DGGML_MULTIPLATFORMTMPDIR = o//tmpIGNORE := $(shell mkdir -p $(TMPDIR))diff --git a/llama.cpp/BUILD.mk b/llama.cpp/BUILD.mkindex c1e10ddc..79557dc5 100644--- a/llama.cpp/BUILD.mk+++ b/llama.cpp/BUILD.mk@@ -24,6 +24,8 @@ include llama.cpp/main/BUILD.mkinclude llama.cpp/quantize/BUILD.mkinclude llama.cpp/perplexity/BUILD.mk+$(LLAMA_CPP_OBJS): private CCFLAGS += -DGGML_MULTIPLATFORM+.PHONY: o/$(MODE)/llama.cppo/$(MODE)/llama.cpp: \o/$(MODE)/llama.cpp/main \diff --git a/llama.cpp/README.llamafile b/llama.cpp/README.llamafileindex b13a9743..873a10e0 100644--- a/llama.cpp/README.llamafile+++ b/llama.cpp/README.llamafile@@ -9,8 +9,8 @@ LICENSEORIGINhttps://github.com/ggerganov/llama.cpp/pull/4406/- b3a7c20b5c035250257d2b62851c379b159c899a- 2024-01-04+ 4f56458d34cb13dcbf69aca650e9bf77d5497e6f+ 2024-01-10LOCAL MODIFICATIONSdiff --git a/llama.cpp/common.cpp b/llama.cpp/common.cppindex 31023844..8026a8fe 100644--- a/llama.cpp/common.cpp+++ b/llama.cpp/common.cpp@@ -235,6 +235,20 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {break;}params.n_ctx = std::stoi(argv[i]);+ } else if (arg == "--grp-attn-n" || arg == "-gan") {+ if (++i >= argc) {+ invalid_param = true;+ break;+ }++ params.grp_attn_n = std::stoi(argv[i]);+ } else if (arg == "--grp-attn-w" || arg == "-gaw") {+ if (++i >= argc) {+ invalid_param = true;+ break;+ }++ params.grp_attn_w = std::stoi(argv[i]);} else if (arg == "--rope-freq-base") {if (++i >= argc) {invalid_param = true;@@ -662,7 +676,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {}std::stringstream ss(argv[i]);llama_token key;- char sign = 0;+ char sign;std::string value_str;try {if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {@@ -817,6 +831,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {printf("\n");printf("options:\n");printf(" -h, --help show this help message and exit\n");+ printf(" --version show version and build info\n");printf(" -i, --interactive run in interactive mode\n");printf(" --interactive-first run in interactive mode and wait for input right away\n");printf(" -ins, --instruct run in instruction mode (use with Alpaca models)\n");@@ -908,6 +923,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {printf(" --numa attempt optimizations that help on some NUMA systems\n");printf(" if run without this previously, it is recommended to drop the system page cache before using this\n");printf(" see https://github.com/ggerganov/llama.cpp/issues/1437\n");+#ifdef LLAMA_SUPPORTS_GPU_OFFLOADprintf(" -ngl N, --n-gpu-layers N\n");printf(" number of layers to store in VRAM\n");printf(" -ngld N, --n-gpu-layers-draft N\n");@@ -915,9 +931,16 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {printf(" -ts SPLIT --tensor-split SPLIT\n");printf(" how to split tensors across multiple GPUs, comma-separated list of proportions, e.g. 3,1\n");printf(" -mg i, --main-gpu i the GPU to use for scratch and small tensors\n");+#ifdef GGML_USE_CUBLASprintf(" -nommq, --no-mul-mat-q\n");printf(" use " GGML_CUBLAS_NAME " instead of custom mul_mat_q " GGML_CUDA_NAME " kernels.\n");printf(" Not recommended since this is both slower and uses more VRAM.\n");+#endif // GGML_USE_CUBLAS+#endif+ printf(" -gan N, --grp-attn-n N\n");+ printf(" group-attention factor (default: %d)\n", params.grp_attn_n);+ printf(" -gaw N, --grp-attn-w N\n");+ printf(" group-attention width (default: %.1f)\n", (double)params.grp_attn_w);printf(" --verbose-prompt print prompt before generation\n");printf(" -dkvc, --dump-kv-cache\n");printf(" verbose print of the KV cache\n");@@ -934,7 +957,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {printf(" -m FNAME, --model FNAME\n");printf(" model path (default: %s)\n", params.model.c_str());printf(" -md FNAME, --model-draft FNAME\n");- printf(" draft model for speculative decoding (default: %s)\n", params.model.c_str());+ printf(" draft model for speculative decoding\n");printf(" -ld LOGDIR, --logdir LOGDIR\n");printf(" path under which to save YAML logs (no logging if unset)\n");printf(" --override-kv KEY=TYPE:VALUE\n");@@ -1327,7 +1350,7 @@ std::string get_sortable_timestamp() {const int64_t ns = std::chrono::duration_cast<std::chrono::nanoseconds>(current_time.time_since_epoch() % 1000000000).count();- char timestamp_ns[21];+ char timestamp_ns[11];snprintf(timestamp_ns, 11, "%09" PRId64, ns);return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);diff --git a/llama.cpp/common.h b/llama.cpp/common.hindex 3f76864c..493986e0 100644--- a/llama.cpp/common.h+++ b/llama.cpp/common.h@@ -74,6 +74,8 @@ struct gpt_params {int32_t main_gpu = 0; // the GPU that is used for scratch and small tensorsfloat tensor_split[LLAMA_MAX_DEVICES] = {0}; // how split tensors should be distributed across GPUsint32_t n_beams = 0; // if non-zero then use beam search of given width.+ int32_t grp_attn_n = 1; // group-attention factor+ int32_t grp_attn_w = 512; // group-attention widthfloat rope_freq_base = 0.0f; // RoPE base frequencyfloat rope_freq_scale = 0.0f; // RoPE frequency scaling factorfloat yarn_ext_factor = -1.0f; // YaRN extrapolation mix factor@@ -254,3 +256,4 @@ void dump_kv_cache_view(const llama_kv_cache_view & view, int row_size = 80);// Dump the KV cache view showing individual sequences in each cell (long output).void dump_kv_cache_view_seqs(const llama_kv_cache_view & view, int row_size = 40);+diff --git a/llama.cpp/ggml-backend-impl.h b/llama.cpp/ggml-backend-impl.hindex 2d62ce3b..b5c0fc90 100644--- a/llama.cpp/ggml-backend-impl.h+++ b/llama.cpp/ggml-backend-impl.h@@ -4,7 +4,6 @@// ggml-backend internal header-#include "ggml.h"#include "ggml-backend.h"#ifdef __cplusplus@@ -17,6 +16,7 @@ extern "C" {// buffer typetypedef void * ggml_backend_buffer_type_context_t;+struct ggml_backend_buffer_type_i {ggml_backend_buffer_t (*GGML_CALL alloc_buffer) (ggml_backend_buffer_type_t buft, size_t size);size_t (*GGML_CALL get_alignment) (ggml_backend_buffer_type_t buft); // tensor alignmentdiff --git a/llama.cpp/ggml-backend.c b/llama.cpp/ggml-backend.cindex 4badd273..9cabc92a 100644--- a/llama.cpp/ggml-backend.c+++ b/llama.cpp/ggml-backend.c@@ -201,11 +201,14 @@ void ggml_backend_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_ggml_backend_synchronize(backend);}-void ggml_backend_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {- backend->iface.graph_compute(backend, cgraph);+bool ggml_backend_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {+ if (!backend->iface.graph_compute(backend, cgraph)) {+ return false;+ }// TODO: optional syncggml_backend_synchronize(backend);+ return true;}bool ggml_backend_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) {@@ -283,7 +286,7 @@ GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void *GGML_UNUSED(user_data);}-static void ggml_backend_registry_init(void) {+GGML_CALL static void ggml_backend_registry_init(void) {static bool initialized = false;if (initialized) {diff --git a/llama.cpp/ggml-backend.h b/llama.cpp/ggml-backend.hindex f70133e9..be6fdd4f 100644--- a/llama.cpp/ggml-backend.h+++ b/llama.cpp/ggml-backend.h@@ -13,7 +13,6 @@ extern "C" {typedef struct ggml_backend_buffer * ggml_backend_buffer_t;typedef struct ggml_backend * ggml_backend_t;typedef void * ggml_backend_graph_plan_t;- struct ggml_backend_api;//// Backend buffer@@ -61,7 +60,7 @@ extern "C" {GGML_API void ggml_backend_graph_plan_free (ggml_backend_t backend, ggml_backend_graph_plan_t plan);GGML_API void ggml_backend_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan);- GGML_API void ggml_backend_graph_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph);+ GGML_API bool ggml_backend_graph_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph);GGML_API bool ggml_backend_supports_op (ggml_backend_t backend, const struct ggml_tensor * op);// tensor copy between different backends@@ -188,6 +187,7 @@ extern "C" {//// dynamic shared object api//+ struct ggml_backend_api;const struct ggml_backend_api *ggml_backend_api(void);#ifdef __cplusplusdiff --git a/llama.cpp/ggml-cuda.cu b/llama.cpp/ggml-cuda.cuindex 129d3d32..4f943153 100644--- a/llama.cpp/ggml-cuda.cu+++ b/llama.cpp/ggml-cuda.cu@@ -276,6 +276,7 @@ GGML_CALL void ggml_cuda_link(const struct ggml_backend_api *backend_api) {g_backend = backend_api;}+#define CC_PASCAL 600#define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products#define CC_VOLTA 700#define CC_OFFSET_AMD 1000000@@ -343,7 +344,7 @@ static __device__ __forceinline__ int __vsubss4(const int a, const int b) {static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {#if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)c = __builtin_amdgcn_sdot4(a, b, c, false);-#elif defined(__gfx1100__)+#elif defined(RDNA3)c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);#elif defined(__gfx1010__) || defined(__gfx900__)int tmp1;@@ -637,6 +638,14 @@ typedef struct {} block_q6_K;static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");+#define QR2_XXS 8+#define QI2_XXS (QK_K / (4*QR2_XXS))+typedef struct {+ half d;+ uint16_t qs[QK_K/8];+} block_iq2_xxs;+static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");+#define WARP_SIZE 32#define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses@@ -708,11 +717,12 @@ static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};struct cuda_device_capabilities {int cc; // compute capability+ size_t smpb; // max. shared memory per blockbool vmm; // virtual memory supportsize_t vmm_granularity; // granularity of virtual memory};-static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, false, 0} };+static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };static void * g_scratch_buffer = nullptr;static size_t g_scratch_size = 0; // disabled by default@@ -747,6 +757,19 @@ static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {return a;}+static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {+#if __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))+ (void) a;+ bad_arch();+#else+#pragma unroll+ for (int mask = 16; mask > 0; mask >>= 1) {+ a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));+ }+ return a;+#endif // __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))+}+static __device__ __forceinline__ float warp_reduce_max(float x) {#pragma unrollfor (int mask = 16; mask > 0; mask >>= 1) {@@ -755,6 +778,19 @@ static __device__ __forceinline__ float warp_reduce_max(float x) {return x;}+static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {+#if __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))+ (void) x;+ bad_arch();+#else+#pragma unroll+ for (int mask = 16; mask > 0; mask >>= 1) {+ x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));+ }+ return x;+#endif // __CUDA_ARCH__ < CC_PASCAL || (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))+}+static __device__ __forceinline__ float op_repeat(const float a, const float b) {return b;GGML_UNUSED(a);@@ -1454,6 +1490,128 @@ static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t#endif}+static const __device__ uint64_t kgrid_iq2xxs[256] = {+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,+ 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,+ 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,+ 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,+ 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,+ 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,+ 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,+ 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,+ 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,+ 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,+ 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,+ 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,+ 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,+ 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,+ 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,+ 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,+ 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,+ 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,+ 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,+ 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,+ 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,+ 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,+ 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,+ 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,+ 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,+ 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,+ 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,+ 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,+ 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,+ 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,+ 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,+ 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,+ 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,+ 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,+ 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,+ 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,+ 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,+ 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,+ 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,+ 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,+ 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,+ 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,+ 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,+ 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,+ 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,+ 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,+ 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,+ 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,+ 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,+ 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,+ 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,+ 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,+ 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,+ 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,+ 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,+ 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,+ 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,+ 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,+ 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,+ 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,+};++static const __device__ uint8_t ksigns_iq2xs[128] = {+ 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,+ 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,+ 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,+ 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,+ 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,+ 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,+ 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,+ 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,+};++static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};++inline bool ggml_cuda_supports_mmq(enum ggml_type type) {+ switch (type) {+ case GGML_TYPE_Q4_0:+ case GGML_TYPE_Q4_1:+ case GGML_TYPE_Q5_0:+ case GGML_TYPE_Q5_1:+ case GGML_TYPE_Q8_0:+ case GGML_TYPE_Q2_K:+ case GGML_TYPE_Q3_K:+ case GGML_TYPE_Q4_K:+ case GGML_TYPE_Q5_K:+ case GGML_TYPE_Q6_K:+ return true;+ default:+ return false;+ }+}++template<typename dst_t>+static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {++ const int i = blockIdx.x;+ const block_iq2_xxs * x = (const block_iq2_xxs *) vx;++ const int tid = threadIdx.x;+#if QK_K == 256+ const int il = tid/8; // 0...3+ const int ib = tid%8; // 0...7+ dst_t * y = yy + i*QK_K + 32*ib + 8*il;+ const uint16_t * q2 = x[i].qs + 4*ib;+ const uint8_t * aux8 = (const uint8_t *)q2;+ const uint8_t * grid = (const uint8_t *)(kgrid_iq2xxs + aux8[il]);+ const uint32_t aux32 = q2[2] | (q2[3] << 16);+ const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;+ const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];+ for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);+#else+ assert(false);+#endif++}+static __global__ void dequantize_mul_mat_vec_q2_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");@@ -2034,14 +2192,6 @@ static __device__ void convert_f16(const void * vx, const int ib, const int iqs,v.y = x[ib + iqs + 1];}-static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){- const float * x = (const float *) vx;-- // automatic half -> float type cast if dfloat == float- v.x = x[ib + iqs + 0];- v.y = x[ib + iqs + 1];-}-static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {const int ix = blockDim.x*blockIdx.x + threadIdx.x;@@ -2145,7 +2295,7 @@ static __global__ void k_get_rows_float(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 int k) {- const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;+ const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);if (i >= k) {return;@@ -2164,6 +2314,19 @@ static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __y[iybs + iqs + y_offset] = v.y;}+template <typename src_t, typename dst_t>+static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {+ const int i = blockDim.x*blockIdx.x + threadIdx.x;++ if (i >= k) {+ return;+ }++ const src_t * x = (src_t *) vx;++ y[i] = x[i];+}+// VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called// MMVQ = mul_mat_vec_q, MMQ = mul_mat_q@@ -3982,6 +4145,55 @@ static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(return vec_dot_q6_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, x_dmf[i * (WARP_SIZE/QI6_K) + i/QI6_K], &y_df[index_y/QI8_1]);}+static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {+#if QK_K == 256+ const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;++#if QR2_XXS == 8+ const int ib32 = iqs;+ const uint16_t * q2 = bq2->qs + 4*ib32;+ const uint8_t * aux8 = (const uint8_t *)q2;+ const int8_t * q8 = bq8_1[ib32].qs;+ uint32_t aux32 = q2[2] | (q2[3] << 16);+ int sumi = 0;+ for (int l = 0; l < 4; ++l) {+ const uint8_t * grid = (const uint8_t *)(kgrid_iq2xxs + aux8[l]);+ const uint8_t signs = ksigns_iq2xs[aux32 & 127];+ for (int j = 0; j < 8; ++j) {+ sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);+ }+ q8 += 8;+ aux32 >>= 7;+ }+ const float d = (float)bq2->d * (0.5f + aux32) * (float)bq8_1[ib32].ds.x * 0.25f;+ return d * sumi;+#else+ // iqs is 0...15+ const int ib32 = iqs/2;+ const int il = iqs%2;+ const uint16_t * q2 = bq2->qs + 4*ib32;+ const uint8_t * aux8 = (const uint8_t *)q2;+ const uint8_t * grid1 = (const uint8_t *)(kgrid_iq2xxs + aux8[2*il+0]);+ const uint8_t * grid2 = (const uint8_t *)(kgrid_iq2xxs + aux8[2*il+1]);+ const uint32_t aux32 = q2[2] | (q2[3] << 16);+ const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * (float)bq8_1[ib32].ds.x * 0.25f;+ const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];+ const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];+ const int8_t * q8 = bq8_1[ib32].qs + 16*il;+ int sumi1 = 0, sumi2 = 0;+ for (int j = 0; j < 8; ++j) {+ sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);+ sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);+ }+ return d * (sumi1 + sumi2);+#endif+#else+ assert(false);+ return 0.f;+#endif+}+template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>static __device__ __forceinline__ void mul_mat_q(@@ -5363,75 +5575,233 @@ static __global__ void diag_mask_inf_f32(const float * x, float * dst, const intdst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;}-static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {+template <bool vals_smem, int ncols_template, int block_size_template, bool need_check>+static __global__ void soft_max_f16(const float * x, const float * y, float * dst, const int ncols_par, const int nrows_y, const float scale) {+#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL+ const int ncols_data = ncols_template == 0 ? ncols_par : ncols_template;+ const int ncols_smem = GGML_PAD(ncols_data, 2*WARP_SIZE)/2;++ const int tid = threadIdx.x;+ const int rowx = blockIdx.x;+ const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension++ const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;++ const int warp_id = threadIdx.x / WARP_SIZE;+ const int lane_id = threadIdx.x % WARP_SIZE;++ extern __shared__ half data_soft_max_f16[];+ half * buf_iw = data_soft_max_f16 + 0; // shared memory buffer for inter-warp communication+ // (shared memory) buffer to cache values between iterations:+ half2 * vals = vals_smem ? (half2 *) (buf_iw + WARP_SIZE) : (half2 *) (dst + rowx*ncols_data);+ // if the buffer is larger than max. shared memory per block, use dst as temp. buffer instead+ // in that case col_smem == col_data must be enforced to avoid race conditions++ half2 max_val = make_half2(-INFINITY, -INFINITY);++#pragma unroll+ for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {+ const int col_data = 2*col0 + 2*WARP_SIZE*warp_id + lane_id;+ const int col_smem = vals_smem ? col0 + tid : col_data;++ const int ix = rowx*ncols_data + col_data;+ const int iy = rowy*ncols_data + col_data;++ half2 val;+ if (need_check && col_data + 0 >= ncols_data) {+ val.x = -INFINITY;+ } else {+ val.x = x[ix + 0]*scale + (y ? y[iy + 0] : 0.0f);+ }+ if (need_check && col_data + WARP_SIZE >= ncols_data) {+ val.y = -INFINITY;+ } else {+ val.y = x[ix + WARP_SIZE]*scale + (y ? y[iy + WARP_SIZE] : 0.0f);+ }+ if (!need_check || col_smem < (vals_smem ? ncols_smem : ncols_data)) {+ vals[col_smem] = val;+ }+ max_val = __hmax2(max_val, val);+ }++ // find the max value in the block+ max_val = warp_reduce_max(max_val);+ if (block_size > WARP_SIZE) {+ if (warp_id == 0) {+ buf_iw[lane_id] = -INFINITY;+ }+ __syncthreads();++ if (lane_id == 0) {+ buf_iw[warp_id] = __hmax(max_val.x, max_val.y);+ }+ __syncthreads();++ max_val = __half2half2(buf_iw[lane_id]);+ max_val = warp_reduce_max(max_val);+ } else {+ max_val = __half2half2(__hmax(max_val.x, max_val.y));+ }++ half2 tmp = make_half2(0.0f, 0.0f); // partial sums++#pragma unroll+ for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {+ const int col_smem = vals_smem ? col0 + tid : 2*col0 + 2*warp_id*WARP_SIZE + lane_id;++ if (ncols_template == 0 && col_smem >= (vals_smem ? ncols_smem : ncols_data)) {+ break;+ }++ const half2 val = h2exp(vals[col_smem] - max_val);++ tmp += val;+ vals[col_smem] = val;+ }++ // find the sum of exps in the block+ tmp = warp_reduce_sum(tmp);+ if (block_size > WARP_SIZE) {+ if (warp_id == 0) {+ buf_iw[lane_id] = 0.0f;+ }+ __syncthreads();++ if (lane_id == 0) {+ buf_iw[warp_id] = tmp.x + tmp.y;+ }+ __syncthreads();++ tmp = __half2half2(buf_iw[lane_id]);+ tmp = warp_reduce_sum(tmp);+ } else {+ tmp = __half2half2(tmp.x + tmp.y);+ }++ const half2 inv_sum = make_half2(1.0f, 1.0f) / tmp;++#pragma unroll+ for (int col0 = 0; col0 < ncols_smem; col0 += block_size) {+ const int col_data = 2*col0 + 2*WARP_SIZE*warp_id + lane_id;+ const int col_smem = vals_smem ? col0 + tid : col_data;++ const int idst = rowx*ncols_data + col_data;+ const half2 result = vals[col_smem] * inv_sum;++ if (need_check && col_data + 0 >= ncols_data) {+ return;+ }+ dst[idst] = result.x;++ if (need_check && col_data + WARP_SIZE >= ncols_data) {+ return;+ }++ dst[idst + WARP_SIZE] = result.y;+ }+#else+ (void) x; (void) y; (void) dst; (void) ncols_par; (void) nrows_y; (void) scale;+ bad_arch();+#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL+}++template <bool vals_smem, int ncols_template, int block_size_template>+static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols_par, const int nrows_y, const float scale) {+ const int ncols = ncols_template == 0 ? ncols_par : ncols_template;+const int tid = threadIdx.x;const int rowx = blockIdx.x;const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension- const int block_size = blockDim.x;+ const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;const int warp_id = threadIdx.x / WARP_SIZE;const int lane_id = threadIdx.x % WARP_SIZE;- __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];+ extern __shared__ float data_soft_max_f32[];+ float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication+ // shared memory buffer to cache values between iterations:+ float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;float max_val = -INFINITY;- for (int col = tid; col < ncols; col += block_size) {+#pragma unroll+ for (int col0 = 0; col0 < ncols; col0 += block_size) {+ const int col = col0 + tid;++ if (ncols_template == 0 && col >= ncols) {+ break;+ }+const int ix = rowx*ncols + col;const int iy = rowy*ncols + col;- max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));++ const float val = x[ix]*scale + (y ? y[iy] : 0.0f);+ vals[col] = val;+ max_val = max(max_val, val);}// find the max value in the blockmax_val = warp_reduce_max(max_val);if (block_size > WARP_SIZE) {if (warp_id == 0) {- buf[lane_id] = -INFINITY;+ buf_iw[lane_id] = -INFINITY;}__syncthreads();if (lane_id == 0) {- buf[warp_id] = max_val;+ buf_iw[warp_id] = max_val;}__syncthreads();- max_val = buf[lane_id];+ max_val = buf_iw[lane_id];max_val = warp_reduce_max(max_val);}- float tmp = 0.f;+ float tmp = 0.0f; // partial sum- for (int col = tid; col < ncols; col += block_size) {- const int ix = rowx*ncols + col;- const int iy = rowy*ncols + col;- const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);+#pragma unroll+ for (int col0 = 0; col0 < ncols; col0 += block_size) {+ const int col = col0 + tid;++ if (ncols_template == 0 && col >= ncols) {+ break;+ }++ const float val = expf(vals[col] - max_val);tmp += val;- dst[ix] = val;+ vals[col] = val;}// find the sum of exps in the blocktmp = warp_reduce_sum(tmp);if (block_size > WARP_SIZE) {if (warp_id == 0) {- buf[lane_id] = 0.f;+ buf_iw[lane_id] = 0.0f;}__syncthreads();if (lane_id == 0) {- buf[warp_id] = tmp;+ buf_iw[warp_id] = tmp;}__syncthreads();- tmp = buf[lane_id];+ tmp = buf_iw[lane_id];tmp = warp_reduce_sum(tmp);}- const float inv_tmp = 1.f / tmp;+ const float inv_sum = 1.0f / tmp;- for (int col = tid; col < ncols; col += block_size) {- const int i = rowx*ncols + col;- dst[i] *= inv_tmp;+#pragma unroll+ for (int col0 = 0; col0 < ncols; col0 += block_size) {+ const int col = col0 + tid;++ if (ncols_template == 0 && col >= ncols) {+ return;+ }++ const int idst = rowx*ncols + col;+ dst[idst] = vals[col] * inv_sum;}}@@ -5771,7 +6141,7 @@ static void quantize_row_q8_1_cuda(const float * x, void * vy, const int kx, contemplate <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {- const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;+ const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);}@@ -5821,6 +6191,18 @@ static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cu#endif}+template<typename dst_t>+static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {+ const int nb = k / QK_K;+ dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);+}++template <typename src_t, typename dst_t>+static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {+ const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;+ convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);+}+static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {switch (type) {case GGML_TYPE_Q4_0:@@ -5843,8 +6225,10 @@ static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {return dequantize_row_q5_K_cuda;case GGML_TYPE_Q6_K:return dequantize_row_q6_K_cuda;+ case GGML_TYPE_IQ2_XXS:+ return dequantize_row_iq2_xxs_cuda;case GGML_TYPE_F32:- return dequantize_block_cuda<1, 1, convert_f32>;+ return convert_unary_cuda<float>;default:return nullptr;}@@ -5872,8 +6256,10 @@ static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {return dequantize_row_q5_K_cuda;case GGML_TYPE_Q6_K:return dequantize_row_q6_K_cuda;+ case GGML_TYPE_IQ2_XXS:+ return dequantize_row_iq2_xxs_cuda;case GGML_TYPE_F16:- return dequantize_block_cuda<1, 1, convert_f16>;+ return convert_unary_cuda<half>;default:return nullptr;}@@ -6066,6 +6452,15 @@ static void mul_mat_vec_q6_K_q8_1_cuda(const void * vx, const void * vy, float *<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);}+static void mul_mat_vec_iq2_xxs_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {+ GGML_ASSERT(ncols % QK_K == 0);+ const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;+ const dim3 block_nums(block_num_y, 1, 1);+ const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);+ mul_mat_vec_q<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>+ <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);+}+static void ggml_mul_mat_q4_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {@@ -6705,12 +7100,90 @@ static void diag_mask_inf_f32_cuda(const float * x, float * dst, const int ncolsdiag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);}+static void soft_max_f16_cuda(const float * x, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, cudaStream_t stream) {+ int nth = WARP_SIZE;+ while (nth < ncols_x/2 && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;+ const dim3 block_dims(nth, 1, 1);+ const dim3 block_nums(nrows_x, 1, 1);+ const size_t shmem = (GGML_PAD(ncols_x, 2*WARP_SIZE) + WARP_SIZE)*sizeof(half);+ static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");+ if (shmem <= g_device_caps[g_main_device].smpb) {+ switch (ncols_x) {+ case 32:+ soft_max_f16<true, 32, 32, true><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 64:+ soft_max_f16<true, 64, 32, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 128:+ soft_max_f16<true, 128, 64, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 256:+ soft_max_f16<true, 256, 128, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 512:+ soft_max_f16<true, 512, 256, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 1024:+ soft_max_f16<true, 1024, 512, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 2048:+ soft_max_f16<true, 2048, 1024, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 4096:+ soft_max_f16<true, 4096, 1024, false><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ default:+ soft_max_f16<true, 0, 0, true><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ }+ } else {+ const size_t shmem_low = WARP_SIZE*sizeof(half);+ soft_max_f16<false, 0, 0, true><<<block_nums, block_dims, shmem_low, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ }+}+static void soft_max_f32_cuda(const float * x, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, cudaStream_t stream) {int nth = WARP_SIZE;while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;const dim3 block_dims(nth, 1, 1);const dim3 block_nums(nrows_x, 1, 1);- soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);+ static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");+ if (shmem < g_device_caps[g_main_device].smpb) {+ switch (ncols_x) {+ case 32:+ soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 64:+ soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 128:+ soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 256:+ soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 512:+ soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 1024:+ soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 2048:+ soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ case 4096:+ soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ default:+ soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ break;+ }+ } else {+ const size_t shmem_low = WARP_SIZE*sizeof(float);+ soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, y, dst, ncols_x, nrows_y, scale);+ }}static void im2col_f32_f16_cuda(const float* x, half* dst,@@ -6961,11 +7434,11 @@ struct cuda_pool_alloc {static bool g_cublas_loaded = false;-GGML_CALL bool ggml_cublas_loaded(void) {+GGML_CALL bool ggml_cublas_loaded() {return g_cublas_loaded;}-GGML_CALL void ggml_init_cublas(void) {+GGML_CALL void ggml_init_cublas() {static bool initialized = false;if (!initialized) {@@ -7027,6 +7500,7 @@ GGML_CALL void ggml_init_cublas(void) {#elseg_device_caps[id].cc = 100*prop.major + 10*prop.minor;#endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)+ g_device_caps[id].smpb = prop.sharedMemPerBlock;}for (int id = 0; id < g_device_count; ++id) {g_tensor_split[id] /= total_vram;@@ -7562,6 +8036,7 @@ static int64_t get_row_rounding(ggml_type type) {case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:return max_compute_capability >= CC_RDNA2 ? 128 : 64;default:GGML_ASSERT(false);@@ -7582,6 +8057,7 @@ static int64_t get_row_rounding(ggml_type type) {case GGML_TYPE_Q3_K:case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:+ case GGML_TYPE_IQ2_XXS:return max_compute_capability >= CC_VOLTA ? 128 : 64;case GGML_TYPE_Q6_K:return 64;@@ -7632,6 +8108,9 @@ static void ggml_cuda_op_mul_mat_vec_q(case GGML_TYPE_Q6_K:mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);break;+ case GGML_TYPE_IQ2_XXS:+ mul_mat_vec_iq2_xxs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);+ break;default:GGML_ASSERT(false);break;@@ -8040,7 +8519,21 @@ static void ggml_cuda_op_soft_max(float scale = 1.0f;memcpy(&scale, dst->op_params, sizeof(float));- soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);+#if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)+ const bool use_f16_soft_max = false;+#else+#ifdef GGML_CUDA_F16+ const bool use_f16_soft_max = true;+#else+ const bool use_f16_soft_max = false;+#endif // GGML_CUDA_F16+#endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)++ if (use_f16_soft_max) {+ soft_max_f16_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);+ } else {+ soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);+ }(void) dst;}@@ -8850,6 +9343,8 @@ static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1#endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)+ use_mul_mat_q = use_mul_mat_q && ggml_cuda_supports_mmq(src0->type);+// debug helpers//printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);//printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);diff --git a/llama.cpp/ggml-metal.m b/llama.cpp/ggml-metal.mindex a8eff058..d682f6db 100644--- a/llama.cpp/ggml-metal.m+++ b/llama.cpp/ggml-metal.m@@ -107,6 +107,7 @@ struct ggml_metal_context {GGML_METAL_DECL_KERNEL(get_rows_q5_K);GGML_METAL_DECL_KERNEL(get_rows_q6_K);GGML_METAL_DECL_KERNEL(get_rows_i32);+ GGML_METAL_DECL_KERNEL(get_rows_iq2_xxs);GGML_METAL_DECL_KERNEL(rms_norm);GGML_METAL_DECL_KERNEL(group_norm);GGML_METAL_DECL_KERNEL(norm);@@ -125,6 +126,7 @@ struct ggml_metal_context {GGML_METAL_DECL_KERNEL(mul_mv_q4_K_f32);GGML_METAL_DECL_KERNEL(mul_mv_q5_K_f32);GGML_METAL_DECL_KERNEL(mul_mv_q6_K_f32);+ GGML_METAL_DECL_KERNEL(mul_mv_iq2_xxs_f32);GGML_METAL_DECL_KERNEL(mul_mv_id_f32_f32);//GGML_METAL_DECL_KERNEL(mul_mv_id_f16_f16);GGML_METAL_DECL_KERNEL(mul_mv_id_f16_f32);@@ -140,6 +142,7 @@ struct ggml_metal_context {GGML_METAL_DECL_KERNEL(mul_mv_id_q4_K_f32);GGML_METAL_DECL_KERNEL(mul_mv_id_q5_K_f32);GGML_METAL_DECL_KERNEL(mul_mv_id_q6_K_f32);+ GGML_METAL_DECL_KERNEL(mul_mv_id_iq2_xxs_f32);GGML_METAL_DECL_KERNEL(mul_mm_f32_f32);GGML_METAL_DECL_KERNEL(mul_mm_f16_f32);GGML_METAL_DECL_KERNEL(mul_mm_q4_0_f32);@@ -152,6 +155,7 @@ struct ggml_metal_context {GGML_METAL_DECL_KERNEL(mul_mm_q4_K_f32);GGML_METAL_DECL_KERNEL(mul_mm_q5_K_f32);GGML_METAL_DECL_KERNEL(mul_mm_q6_K_f32);+ GGML_METAL_DECL_KERNEL(mul_mm_iq2_xxs_f32);GGML_METAL_DECL_KERNEL(mul_mm_id_f32_f32);GGML_METAL_DECL_KERNEL(mul_mm_id_f16_f32);GGML_METAL_DECL_KERNEL(mul_mm_id_q4_0_f32);@@ -164,6 +168,7 @@ struct ggml_metal_context {GGML_METAL_DECL_KERNEL(mul_mm_id_q4_K_f32);GGML_METAL_DECL_KERNEL(mul_mm_id_q5_K_f32);GGML_METAL_DECL_KERNEL(mul_mm_id_q6_K_f32);+ GGML_METAL_DECL_KERNEL(mul_mm_id_iq2_xxs_f32);GGML_METAL_DECL_KERNEL(rope_f32);GGML_METAL_DECL_KERNEL(rope_f16);GGML_METAL_DECL_KERNEL(alibi_f32);@@ -277,14 +282,14 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];#endifNSError * error = nil;- NSString * libPath = [bundle pathForResource:@"ggml" ofType:@"metallib"];+ NSString * libPath = [bundle pathForResource:@"default" ofType:@"metallib"];if (libPath != nil) {// pre-compiled library foundNSURL * libURL = [NSURL fileURLWithPath:libPath];GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [libPath UTF8String]);ctx->library = [ctx->device newLibraryWithURL:libURL error:&error];} else {- GGML_METAL_LOG_INFO("%s: ggml.metallib not found, loading from source\n", __func__);+ GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);NSString * sourcePath;NSString * ggmlMetalPathResources = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];@@ -398,6 +403,7 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {GGML_METAL_ADD_KERNEL(get_rows_q5_K);GGML_METAL_ADD_KERNEL(get_rows_q6_K);GGML_METAL_ADD_KERNEL(get_rows_i32);+ GGML_METAL_ADD_KERNEL(get_rows_iq2_xxs);GGML_METAL_ADD_KERNEL(rms_norm);GGML_METAL_ADD_KERNEL(group_norm);GGML_METAL_ADD_KERNEL(norm);@@ -416,6 +422,7 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {GGML_METAL_ADD_KERNEL(mul_mv_q4_K_f32);GGML_METAL_ADD_KERNEL(mul_mv_q5_K_f32);GGML_METAL_ADD_KERNEL(mul_mv_q6_K_f32);+ GGML_METAL_ADD_KERNEL(mul_mv_iq2_xxs_f32);GGML_METAL_ADD_KERNEL(mul_mv_id_f32_f32);//GGML_METAL_ADD_KERNEL(mul_mv_id_f16_f16);GGML_METAL_ADD_KERNEL(mul_mv_id_f16_f32);@@ -431,6 +438,7 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {GGML_METAL_ADD_KERNEL(mul_mv_id_q4_K_f32);GGML_METAL_ADD_KERNEL(mul_mv_id_q5_K_f32);GGML_METAL_ADD_KERNEL(mul_mv_id_q6_K_f32);+ GGML_METAL_ADD_KERNEL(mul_mv_id_iq2_xxs_f32);if ([ctx->device supportsFamily:MTLGPUFamilyApple7]) {GGML_METAL_ADD_KERNEL(mul_mm_f32_f32);GGML_METAL_ADD_KERNEL(mul_mm_f16_f32);@@ -444,6 +452,7 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {GGML_METAL_ADD_KERNEL(mul_mm_q4_K_f32);GGML_METAL_ADD_KERNEL(mul_mm_q5_K_f32);GGML_METAL_ADD_KERNEL(mul_mm_q6_K_f32);+ GGML_METAL_ADD_KERNEL(mul_mm_iq2_xxs_f32);GGML_METAL_ADD_KERNEL(mul_mm_id_f32_f32);GGML_METAL_ADD_KERNEL(mul_mm_id_f16_f32);GGML_METAL_ADD_KERNEL(mul_mm_id_q4_0_f32);@@ -456,6 +465,7 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {GGML_METAL_ADD_KERNEL(mul_mm_id_q4_K_f32);GGML_METAL_ADD_KERNEL(mul_mm_id_q5_K_f32);GGML_METAL_ADD_KERNEL(mul_mm_id_q6_K_f32);+ GGML_METAL_ADD_KERNEL(mul_mm_id_iq2_xxs_f32);}GGML_METAL_ADD_KERNEL(rope_f32);GGML_METAL_ADD_KERNEL(rope_f16);@@ -521,6 +531,7 @@ void ggml_metal_free(struct ggml_metal_context * ctx) {GGML_METAL_DEL_KERNEL(get_rows_q5_K);GGML_METAL_DEL_KERNEL(get_rows_q6_K);GGML_METAL_DEL_KERNEL(get_rows_i32);+ GGML_METAL_DEL_KERNEL(get_rows_iq2_xxs);GGML_METAL_DEL_KERNEL(rms_norm);GGML_METAL_DEL_KERNEL(group_norm);GGML_METAL_DEL_KERNEL(norm);@@ -539,6 +550,7 @@ void ggml_metal_free(struct ggml_metal_context * ctx) {GGML_METAL_DEL_KERNEL(mul_mv_q4_K_f32);GGML_METAL_DEL_KERNEL(mul_mv_q5_K_f32);GGML_METAL_DEL_KERNEL(mul_mv_q6_K_f32);+ GGML_METAL_DEL_KERNEL(mul_mv_iq2_xxs_f32);GGML_METAL_DEL_KERNEL(mul_mv_id_f32_f32);//GGML_METAL_DEL_KERNEL(mul_mv_id_f16_f16);GGML_METAL_DEL_KERNEL(mul_mv_id_f16_f32);@@ -554,6 +566,7 @@ void ggml_metal_free(struct ggml_metal_context * ctx) {GGML_METAL_DEL_KERNEL(mul_mv_id_q4_K_f32);GGML_METAL_DEL_KERNEL(mul_mv_id_q5_K_f32);GGML_METAL_DEL_KERNEL(mul_mv_id_q6_K_f32);+ GGML_METAL_DEL_KERNEL(mul_mv_id_iq2_xxs_f32);if ([ctx->device supportsFamily:MTLGPUFamilyApple7]) {GGML_METAL_DEL_KERNEL(mul_mm_f32_f32);GGML_METAL_DEL_KERNEL(mul_mm_f16_f32);@@ -567,6 +580,7 @@ void ggml_metal_free(struct ggml_metal_context * ctx) {GGML_METAL_DEL_KERNEL(mul_mm_q4_K_f32);GGML_METAL_DEL_KERNEL(mul_mm_q5_K_f32);GGML_METAL_DEL_KERNEL(mul_mm_q6_K_f32);+ GGML_METAL_DEL_KERNEL(mul_mm_iq2_xxs_f32);GGML_METAL_DEL_KERNEL(mul_mm_id_f32_f32);GGML_METAL_DEL_KERNEL(mul_mm_id_f16_f32);GGML_METAL_DEL_KERNEL(mul_mm_id_q4_0_f32);@@ -579,6 +593,7 @@ void ggml_metal_free(struct ggml_metal_context * ctx) {GGML_METAL_DEL_KERNEL(mul_mm_id_q4_K_f32);GGML_METAL_DEL_KERNEL(mul_mm_id_q5_K_f32);GGML_METAL_DEL_KERNEL(mul_mm_id_q6_K_f32);+ GGML_METAL_DEL_KERNEL(mul_mm_id_iq2_xxs_f32);}GGML_METAL_DEL_KERNEL(rope_f32);GGML_METAL_DEL_KERNEL(rope_f16);@@ -1561,6 +1576,7 @@ bool ggml_metal_graph_compute(case GGML_TYPE_Q4_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q4_K_f32]; break;case GGML_TYPE_Q5_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q5_K_f32]; break;case GGML_TYPE_Q6_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q6_K_f32]; break;+ case GGML_TYPE_IQ2_XXS: [encoder setComputePipelineState:ctx->pipeline_mul_mm_iq2_xxs_f32]; break;default: GGML_ASSERT(false && "MUL MAT-MAT not implemented");}[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];@@ -1673,6 +1689,12 @@ bool ggml_metal_graph_compute(nth1 = 32;[encoder setComputePipelineState:ctx->pipeline_mul_mv_q6_K_f32];} break;+ case GGML_TYPE_IQ2_XXS:+ {+ nth0 = 4;+ nth1 = 16;+ [encoder setComputePipelineState:ctx->pipeline_mul_mv_iq2_xxs_f32];+ } break;default:{GGML_METAL_LOG_ERROR("Asserting on type %d\n", (int)src0t);@@ -1706,9 +1728,14 @@ bool ggml_metal_graph_compute(if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 ||src0t == GGML_TYPE_Q5_0 || src0t == GGML_TYPE_Q5_1 || src0t == GGML_TYPE_Q8_0 ||+ //src0t == GGML_TYPE_IQ2_XXS ||src0t == GGML_TYPE_Q2_K) { // || src0t == GGML_TYPE_Q4_K) {[encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];}+ else if (src0t == GGML_TYPE_IQ2_XXS) {+ [encoder setThreadgroupMemoryLength:(256*8+128) atIndex:0];+ [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];+ }else if (src0t == GGML_TYPE_Q4_K) {[encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];}@@ -1798,6 +1825,7 @@ bool ggml_metal_graph_compute(case GGML_TYPE_Q4_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_id_q4_K_f32]; break;case GGML_TYPE_Q5_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_id_q5_K_f32]; break;case GGML_TYPE_Q6_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_id_q6_K_f32]; break;+ case GGML_TYPE_IQ2_XXS: [encoder setComputePipelineState:ctx->pipeline_mul_mm_id_iq2_xxs_f32]; break;default: GGML_ASSERT(false && "MUL_MAT_ID not implemented");}[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];@@ -1913,6 +1941,12 @@ bool ggml_metal_graph_compute(nth1 = 32;[encoder setComputePipelineState:ctx->pipeline_mul_mv_id_q6_K_f32];} break;+ case GGML_TYPE_IQ2_XXS:+ {+ nth0 = 4;+ nth1 = 16;+ [encoder setComputePipelineState:ctx->pipeline_mul_mv_id_iq2_xxs_f32];+ } break;default:{GGML_METAL_LOG_ERROR("Asserting on type %d\n", (int)src2t);@@ -1962,9 +1996,14 @@ bool ggml_metal_graph_compute(if (src2t == GGML_TYPE_Q4_0 || src2t == GGML_TYPE_Q4_1 ||src2t == GGML_TYPE_Q5_0 || src2t == GGML_TYPE_Q5_1 || src2t == GGML_TYPE_Q8_0 ||+ //src2t == GGML_TYPE_IQ2_XXS ||src2t == GGML_TYPE_Q2_K) { // || src2t == GGML_TYPE_Q4_K) {[encoder dispatchThreadgroups:MTLSizeMake((ne21 + 7)/8, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];}+ else if (src2t == GGML_TYPE_IQ2_XXS) {+ [encoder setThreadgroupMemoryLength:(256*8+128) atIndex:0];+ [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 7)/8, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];+ }else if (src2t == GGML_TYPE_Q4_K) {[encoder dispatchThreadgroups:MTLSizeMake((ne21 + 3)/4, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];}@@ -2002,6 +2041,7 @@ bool ggml_metal_graph_compute(case GGML_TYPE_Q5_K: [encoder setComputePipelineState:ctx->pipeline_get_rows_q5_K]; break;case GGML_TYPE_Q6_K: [encoder setComputePipelineState:ctx->pipeline_get_rows_q6_K]; break;case GGML_TYPE_I32: [encoder setComputePipelineState:ctx->pipeline_get_rows_i32]; break;+ case GGML_TYPE_IQ2_XXS: [encoder setComputePipelineState:ctx->pipeline_get_rows_iq2_xxs]; break;default: GGML_ASSERT(false && "not implemented");}@@ -2773,3 +2813,12 @@ bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];}++GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data); // silence warning++GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data) {+ return ggml_backend_metal_init();++ GGML_UNUSED(params);+ GGML_UNUSED(user_data);+}diff --git a/llama.cpp/ggml-metal.metal b/llama.cpp/ggml-metal.metalindex a7d3f9ef..229efb8b 100644--- a/llama.cpp/ggml-metal.metal+++ b/llama.cpp/ggml-metal.metal@@ -2446,6 +2446,12 @@ typedef struct {} block_q6_K;// 210 bytes / block+typedef struct {+ half d;+ uint16_t qs[QK_K/8];+} block_iq2_xxs;+// 66 bytes / block for QK_K = 256, so 2.0625 bpw+//====================================== dot products =========================void kernel_mul_mv_q2_K_f32_impl(@@ -3468,6 +3474,221 @@ kernel void kernel_mul_mv_q6_K_f32(kernel_mul_mv_q6_K_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, tgpig, tiisg, sgitg);}+// ======================= "True" 2-bit++constexpr constant static uint64_t kgrid_iq2xxs[256] = {+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,+ 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,+ 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,+ 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,+ 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,+ 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,+ 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,+ 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,+ 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,+ 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,+ 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,+ 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,+ 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,+ 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,+ 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,+ 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,+ 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,+ 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,+ 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,+ 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,+ 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,+ 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,+ 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,+ 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,+ 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,+ 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,+ 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,+ 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,+ 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,+ 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,+ 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,+ 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,+ 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,+ 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,+ 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,+ 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,+ 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,+ 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,+ 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,+ 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,+ 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,+ 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,+ 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,+ 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,+ 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,+ 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,+ 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,+ 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,+ 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,+ 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,+ 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,+ 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,+ 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,+ 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,+ 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,+ 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,+ 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,+ 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,+ 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,+ 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,+};++constexpr constant static uint8_t ksigns_iq2xs[128] = {+ 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,+ 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,+ 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,+ 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,+ 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,+ 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,+ 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,+ 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,+};++constexpr constant static uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};++void kernel_mul_mv_iq2_xxs_f32_impl(+ device const void * src0,+ device const float * src1,+ device float * dst,+ constant int64_t & ne00,+ constant int64_t & ne01,+ constant int64_t & ne02,+ constant int64_t & ne10,+ constant int64_t & ne12,+ constant int64_t & ne0,+ constant int64_t & ne1,+ constant uint & r2,+ constant uint & r3,+ threadgroup int8_t * shared_values [[threadgroup(0)]],+ uint3 tgpig[[threadgroup_position_in_grid]],+ uint tiisg[[thread_index_in_simdgroup]],+ uint sgitg[[simdgroup_index_in_threadgroup]]) {++ const int nb = ne00/QK_K;+ const int r0 = tgpig.x;+ const int r1 = tgpig.y;+ const int im = tgpig.z;++ const int first_row = (r0 * N_SIMDGROUP + sgitg) * N_DST;+ const int ib_row = first_row * nb;++ const uint i12 = im%ne12;+ const uint i13 = im/ne12;++ const uint offset0 = (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);++ device const block_iq2_xxs * x = (device const block_iq2_xxs *) src0 + ib_row + offset0;+ device const float * y = (device const float *) src1 + r1*ne10 + im*ne00*ne1;++ float yl[32];+ float sumf[N_DST]={0.f}, all_sum;++ const int nb32 = nb * (QK_K / 32);++ threadgroup uint64_t * values = (threadgroup uint64_t *)shared_values;+ threadgroup uint8_t * shared_signs = (threadgroup uint8_t *)(values + 256);+ {+ int nval = 4;+ int pos = (32*sgitg + tiisg)*nval;+ for (int i = 0; i < nval; ++i) values[pos + i] = kgrid_iq2xxs[pos + i];+ nval = 2;+ pos = (32*sgitg + tiisg)*nval;+ for (int i = 0; i < nval; ++i) shared_signs[pos+i] = ksigns_iq2xs[pos+i];+ threadgroup_barrier(mem_flags::mem_threadgroup);+ }++#if QK_K == 256+ const int ix = tiisg;++ device const float * y4 = y + 32 * ix;++ for (int ib32 = ix; ib32 < nb32; ib32 += 32) {++ for (int i = 0; i < 32; ++i) {+ yl[i] = y4[i];+ }++ const int ibl = ib32 / (QK_K / 32);+ const int ib = ib32 % (QK_K / 32);++ device const block_iq2_xxs * xr = x + ibl;+ device const uint16_t * q2 = xr->qs + 4 * ib;+ device const half * dh = &xr->d;++ for (int row = 0; row < N_DST; row++) {++ const float db = dh[0];+ device const uint8_t * aux8 = (device const uint8_t *)q2;+ const uint32_t aux32 = q2[2] | (q2[3] << 16);+ const float d = db * (0.5f + (aux32 >> 28));++ float sum = 0;+ for (int l = 0; l < 4; ++l) {+ const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(values + aux8[l]);+ const uint8_t signs = shared_signs[(aux32 >> 7*l) & 127];+ for (int j = 0; j < 8; ++j) {+ sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);+ }+ }+ sumf[row] += d * sum;++ dh += nb*sizeof(block_iq2_xxs)/2;+ q2 += nb*sizeof(block_iq2_xxs)/2;+ }++ y4 += 32 * 32;+ }+#else+ // TODO+#endif++ for (int row = 0; row < N_DST; ++row) {+ all_sum = simd_sum(sumf[row]);+ if (tiisg == 0) {+ dst[r1*ne0 + im*ne0*ne1 + first_row + row] = all_sum * 0.25f;+ }+ }+}++[[host_name("kernel_mul_mv_iq2_xxs_f32")]]+kernel void kernel_mul_mv_iq2_xxs_f32(+ device const void * src0,+ device const float * src1,+ device float * dst,+ constant int64_t & ne00,+ constant int64_t & ne01,+ constant int64_t & ne02,+ constant uint64_t & nb00,+ constant uint64_t & nb01,+ constant uint64_t & nb02,+ constant int64_t & ne10,+ constant int64_t & ne11,+ constant int64_t & ne12,+ constant uint64_t & nb10,+ constant uint64_t & nb11,+ constant uint64_t & nb12,+ constant int64_t & ne0,+ constant int64_t & ne1,+ constant uint & r2,+ constant uint & r3,+ threadgroup int8_t * shared_values [[threadgroup(0)]],+ uint3 tgpig[[threadgroup_position_in_grid]],+ uint tiisg[[thread_index_in_simdgroup]],+ uint sgitg[[simdgroup_index_in_threadgroup]]) {++ kernel_mul_mv_iq2_xxs_f32_impl(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3, shared_values, tgpig, tiisg, sgitg);+}+//============================= templates and their specializations =============================// NOTE: this is not dequantizing - we are simply fitting the template@@ -3620,8 +3841,8 @@ void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reguint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2): (scale_2&kmask2) | ((scale_1&kmask1) << 4);- half dl = il<8 ? d_all * (dl_int - 32.h) : d_all * (dl_int / 16.h - 32.h);- const half ml = 4.h * dl;+ float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);+ const float ml = 4.f * dl;il = (il/2) & 3;const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);@@ -3688,7 +3909,7 @@ void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reguint8_t ul = 1 << (il/2);il = il & 3;const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);- const float d = il < 2 ? xb->d : xb->d / 16.h;+ const float d = il < 2 ? xb->d : xb->d / 16.f;const float min = xb->dmin;const float dl = d * sc[0];const float ml = min * sc[1];@@ -3721,17 +3942,17 @@ void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg#if QK_K == 256ql = ql + 64*(il/8) + 32*((il/2)&1) + 16*(il&1);qh = qh + 32*(il/8) + 16*(il&1);- half sc = scales[(il%2) + 2 * ((il/2))];+ float sc = scales[(il%2) + 2 * ((il/2))];il = (il/2) & 3;#elseql = ql + 16 * (il&1);- half sc = scales[il];+ float sc = scales[il];#endifconst uint16_t kmask1 = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3);const uint16_t kmask2 = il>1 ? 0xF0 : 0x0F;- const half coef = il>1 ? 1.f/16.h : 1.h;- const half ml = d_all * sc * 32.h;- const half dl = d_all * sc * coef;+ const float coef = il>1 ? 1.f/16.f : 1.f;+ const float ml = d_all * sc * 32.f;+ const float dl = d_all * sc * coef;for (int i = 0; i < 16; ++i) {const half q = il&1 ? ((ql[i] & kmask2) | ((qh[i] & kmask1) << 2)): ((ql[i] & kmask2) | ((qh[i] & kmask1) << 4));@@ -3739,6 +3960,31 @@ void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg}}+template <typename type4x4>+void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {+ // il is 0...15 for QK_K = 256 => index of block of 32 is il/2+ const float d = xb->d;+ const int ib32 = il/2;+ il = il%2;+ // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16+ // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.+ device const uint16_t * q2 = xb->qs + 4*ib32;+ const uint32_t aux32_g = q2[0] | (q2[1] << 16);+ const uint32_t aux32_s = q2[2] | (q2[3] << 16);+ thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;+ const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;+ constant uint8_t * grid = (constant uint8_t *)(kgrid_iq2xxs + aux8[2*il+0]);+ uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];+ for (int i = 0; i < 8; ++i) {+ reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);+ }+ grid = (constant uint8_t *)(kgrid_iq2xxs + aux8[2*il+1]);+ signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];+ for (int i = 0; i < 8; ++i) {+ reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);+ }+}+template<typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread float4x4 &)>kernel void kernel_get_rows(device const void * src0,@@ -4278,6 +4524,7 @@ template [[host_name("kernel_get_rows_q3_K")]] kernel get_rows_t kernel_get_rowstemplate [[host_name("kernel_get_rows_q4_K")]] kernel get_rows_t kernel_get_rows<block_q4_K, QK_NL, dequantize_q4_K>;template [[host_name("kernel_get_rows_q5_K")]] kernel get_rows_t kernel_get_rows<block_q5_K, QK_NL, dequantize_q5_K>;template [[host_name("kernel_get_rows_q6_K")]] kernel get_rows_t kernel_get_rows<block_q6_K, QK_NL, dequantize_q6_K>;+template [[host_name("kernel_get_rows_iq2_xxs")]] kernel get_rows_t kernel_get_rows<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;//// matrix-matrix multiplication@@ -4314,6 +4561,7 @@ template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mat_mm_t kernel_mul_mm<btemplate [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q4_K, QK_NL, dequantize_q4_K>;template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q5_K, QK_NL, dequantize_q5_K>;template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mat_mm_t kernel_mul_mm<block_q6_K, QK_NL, dequantize_q6_K>;+template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mat_mm_t kernel_mul_mm<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;//// indirect matrix-matrix multiplication@@ -4362,6 +4610,7 @@ template [[host_name("kernel_mul_mm_id_q3_K_f32")]] kernel mat_mm_id_t kernel_mutemplate [[host_name("kernel_mul_mm_id_q4_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q4_K, QK_NL, dequantize_q4_K>;template [[host_name("kernel_mul_mm_id_q5_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q5_K, QK_NL, dequantize_q5_K>;template [[host_name("kernel_mul_mm_id_q6_K_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_q6_K, QK_NL, dequantize_q6_K>;+template [[host_name("kernel_mul_mm_id_iq2_xxs_f32")]] kernel mat_mm_id_t kernel_mul_mm_id<block_iq2_xxs, QK_NL, dequantize_iq2_xxs>;//// matrix-vector multiplication@@ -5134,3 +5383,68 @@ kernel void kernel_mul_mv_id_q6_K_f32(tiisg,sgitg);}++[[host_name("kernel_mul_mv_id_iq2_xxs_f32")]]+kernel void kernel_mul_mv_id_iq2_xxs_f32(+ device const char * ids,+ device const char * src1,+ device float * dst,+ constant uint64_t & nbi1,+ constant int64_t & ne00,+ constant int64_t & ne01,+ constant int64_t & ne02,+ constant uint64_t & nb00,+ constant uint64_t & nb01,+ constant uint64_t & nb02,+ constant int64_t & ne10,+ constant int64_t & ne11,+ constant int64_t & ne12,+ constant int64_t & ne13,+ constant uint64_t & nb10,+ constant uint64_t & nb11,+ constant uint64_t & nb12,+ constant int64_t & ne0,+ constant int64_t & ne1,+ constant uint64_t & nb1,+ constant uint & r2,+ constant uint & r3,+ constant int & idx,+ device const char * src00,+ device const char * src01,+ device const char * src02,+ device const char * src03,+ device const char * src04,+ device const char * src05,+ device const char * src06,+ device const char * src07,+ threadgroup int8_t * shared_values [[threadgroup(0)]],+ uint3 tgpig[[threadgroup_position_in_grid]],+ uint tiitg[[thread_index_in_threadgroup]],+ uint tiisg[[thread_index_in_simdgroup]],+ uint sgitg[[simdgroup_index_in_threadgroup]]) {+ device const char * src0[8] = {src00, src01, src02, src03, src04, src05, src06, src07};++ const int64_t bid = tgpig.z/(ne12*ne13);++ tgpig.z = tgpig.z%(ne12*ne13);++ const int32_t id = ((device int32_t *) (ids + bid*nbi1))[idx];++ kernel_mul_mv_iq2_xxs_f32_impl(+ src0[id],+ (device const float *) (src1 + bid*nb11),+ dst + bid*ne0,+ ne00,+ ne01,+ ne02,+ ne10,+ ne12,+ ne0,+ ne1,+ r2,+ r3,+ shared_values,+ tgpig,+ tiisg,+ sgitg);+}diff --git a/llama.cpp/ggml-quants.c b/llama.cpp/ggml-quants.cindex d282d4c0..4da75520 100644--- a/llama.cpp/ggml-quants.c+++ b/llama.cpp/ggml-quants.c@@ -2374,6 +2374,138 @@ size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t *return (n/QK_K*sizeof(block_q6_K));}+// ====================== "True" 2-bit (de)-quantization++void quantize_row_iq2_xxs_reference(const float * restrict x, block_iq2_xxs * restrict y, int k) {+ (void)x;+ (void)y;+ (void)k;+ assert(k % QK_K == 0);+ //fprintf(stderr, "=========================== %s: not implemented\n", __func__);+}++static const uint64_t iq2xxs_grid[256] = {+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,+ 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,+ 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,+ 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,+ 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,+ 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,+ 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,+ 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,+ 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,+ 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,+ 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,+ 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,+ 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,+ 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,+ 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,+ 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,+ 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,+ 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,+ 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,+ 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,+ 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,+ 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,+ 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,+ 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,+ 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,+ 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,+ 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,+ 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,+ 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,+ 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,+ 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,+ 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,+ 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,+ 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,+ 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,+ 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,+ 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,+ 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,+ 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,+ 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,+ 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,+ 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,+ 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,+ 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,+ 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,+ 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,+ 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,+ 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,+ 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,+ 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,+ 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,+ 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,+ 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,+ 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,+ 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,+ 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,+ 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,+ 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,+ 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,+ 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,+};++static const uint8_t ksigns_iq2xs[128] = {+ 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,+ 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,+ 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,+ 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,+ 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,+ 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,+ 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,+ 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,+};+static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};++void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {+ assert(k % QK_K == 0);+ const int nb = k / QK_K;++ uint32_t aux32[2];+ const uint8_t * aux8 = (const uint8_t *)aux32;++ for (int i = 0; i < nb; i++) {++ const float d = GGML_FP16_TO_FP32(x[i].d);++ for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {+ memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));+ const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;+ for (int l = 0; l < 4; ++l) {+ const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);+ const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];+ for (int j = 0; j < 8; ++j) {+ y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);+ }+ y += 8;+ }+ }+ }+}++void quantize_row_iq2_xxs(const float * restrict x, void * restrict vy, int k) {+ assert(k % QK_K == 0);+ block_iq2_xxs * restrict y = vy;+ quantize_row_iq2_xxs_reference(x, y, k);+}++size_t ggml_quantize_iq2_xxs(const float * src, void * dst, int n, int k, int64_t * hist) {+ assert(k % QK_K == 0);+ (void)hist; // TODO: collect histograms++ for (int j = 0; j < n; j += k) {+ block_iq2_xxs * restrict y = (block_iq2_xxs *)dst + j/QK_K;+ quantize_row_iq2_xxs_reference(src + j, y, k);+ }+ return (n/QK_K*sizeof(block_iq2_xxs));+}+//===================================== Q8_K ==============================================void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {@@ -2396,7 +2528,9 @@ void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrictx += QK_K;continue;}- const float iscale = -128.f/max;+ //const float iscale = -128.f/max;+ // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward+ const float iscale = -127.f/max;for (int j = 0; j < QK_K; ++j) {int v = nearest_int(iscale*x[j]);y[i].qs[j] = MIN(127, v);@@ -2724,6 +2858,7 @@ void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restri#endif#else+#error update mevoid ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {@@ -4403,3 +4538,66 @@ void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restri}#endif++static const int8_t keven_signs_q2xs[1024] = {+ 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,+ 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,+ 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,+ 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,+ 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,+ 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,+ 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,+ 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,+ 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,+ 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,+ 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,+ 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,+ 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,+ 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,+ 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,+ 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,+ 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,+ 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,+ 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,+ 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,+ 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,+ 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,+ 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,+ 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,+ 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,+ 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,+ 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,+ 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,+ 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,+ 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,+ 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,+ 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,+};++#ifdef __x86_64__+#define ggml_vec_dot_iq2_xxs_q8_K ggml_vec_dot_iq2_xxs_q8_K_default+#include "ggml_vec_dot_iq2_xxs_q8_K.inc"+#undef ggml_vec_dot_iq2_xxs_q8_K+#define ggml_vec_dot_iq2_xxs_q8_K __attribute__((__target__("avx2,fma,f16c"))) ggml_vec_dot_iq2_xxs_q8_K_avx2+#ifndef __AVX2__+#define __UNDEF_AVX2__+#define __AVX2__+#endif+#include "ggml_vec_dot_iq2_xxs_q8_K.inc"+#ifdef __UNDEF_AVX2__+#undef __UNDEF_AVX2__+#undef __AVX2__+#endif+#undef ggml_vec_dot_iq2_xxs_q8_K+void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {+ if (X86_HAVE(AVX2) && X86_HAVE(FMA) && X86_HAVE(F16C)) {+ ggml_vec_dot_iq2_xxs_q8_K_avx2(n, s, vx, vy);+ } else {+ ggml_vec_dot_iq2_xxs_q8_K_default(n, s, vx, vy);+ }+}+#else+#define static+#include "ggml_vec_dot_iq2_xxs_q8_K.inc"+#undef static+#endifdiff --git a/llama.cpp/ggml-quants.h b/llama.cpp/ggml-quants.hindex 70c12c27..8dd911d4 100644--- a/llama.cpp/ggml-quants.h+++ b/llama.cpp/ggml-quants.h@@ -70,7 +70,7 @@ static_assert(sizeof(block_q8_1) == 2*sizeof(float) + QK8_1, "wrong q8_1 block s// 2-bit quantization// weight is represented as x = a * q + b// 16 blocks of 16 elements each-// Effectively 2.5625 bits per weight+// Effectively 2.625 bits per weighttypedef struct {uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bitsuint8_t qs[QK_K/4]; // quants@@ -165,6 +165,14 @@ typedef struct {} block_q8_K;static_assert(sizeof(block_q8_K) == sizeof(float) + QK_K + QK_K/16*sizeof(int16_t), "wrong q8_K block size/padding");+// (Almost) "true" 2-bit quantization.+// Due to the need to use blocks as per ggml dsign, it ends up using+// 2.0625 bpw because of the 16-bit scale for each block of 256.+typedef struct {+ ggml_fp16_t d;+ uint16_t qs[QK_K/8];+} block_iq2_xxs;+static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");// Quantizationvoid quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k);@@ -180,6 +188,7 @@ void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrictvoid quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k);void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k);void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k);+void quantize_row_iq2_xxs_reference(const float * restrict x, block_iq2_xxs * restrict y, int k);void quantize_row_q4_0(const float * restrict x, void * restrict y, int k);void quantize_row_q4_1(const float * restrict x, void * restrict y, int k);@@ -194,6 +203,7 @@ void quantize_row_q4_K(const float * restrict x, void * restrict y, int k);void quantize_row_q5_K(const float * restrict x, void * restrict y, int k);void quantize_row_q6_K(const float * restrict x, void * restrict y, int k);void quantize_row_q8_K(const float * restrict x, void * restrict y, int k);+void quantize_row_iq2_xxs(const float * restrict x, void * restrict y, int k);// Dequantizationvoid dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k);@@ -209,6 +219,7 @@ void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, intvoid dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k);void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k);void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k);+void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k);// Dot productvoid ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy);@@ -222,3 +233,4 @@ void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, const void * restrict vx,void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);+void ggml_vec_dot_iq2_xxs_q8_K(int n, float * restrict s, const void * restrict vx, const void * restrict vy);diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.cindex f0dd70af..a54a1e5c 100644--- a/llama.cpp/ggml.c+++ b/llama.cpp/ggml.c@@ -150,12 +150,6 @@ void ggml_print_backtrace(void) {// end of logging block//-static volatile bool is_interrupted;--void ggml_interrupt(bool state) {- is_interrupted = state;-}-#ifdef GGML_USE_ACCELERATE// uncomment to use vDSP for soft max computation// note: not sure if it is actually faster@@ -518,6 +512,17 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {.vec_dot = ggml_vec_dot_q6_K_q8_K,.vec_dot_type = GGML_TYPE_Q8_K,},+ [GGML_TYPE_IQ2_XXS] = {+ .type_name = "iq2_xxs",+ .blck_size = QK_K,+ .type_size = sizeof(block_iq2_xxs),+ .is_quantized = true,+ .to_float = (ggml_to_float_t) dequantize_row_iq2_xxs,+ .from_float = quantize_row_iq2_xxs,+ .from_float_reference = (ggml_from_float_t) quantize_row_iq2_xxs_reference,+ .vec_dot = ggml_vec_dot_iq2_xxs_q8_K,+ .vec_dot_type = GGML_TYPE_Q8_K,+ },[GGML_TYPE_Q8_K] = {.type_name = "q8_K",.blck_size = QK_K,@@ -1804,7 +1809,7 @@ static_assert(sizeof(struct ggml_object)%GGML_MEM_ALIGN == 0, "ggml_object sizestatic_assert(sizeof(struct ggml_tensor)%GGML_MEM_ALIGN == 0, "ggml_tensor size must be a multiple of GGML_MEM_ALIGN");// WARN:-// Mis-confguration can lead to problem that's hard to reason about:+// Mis-configuration can lead to problem that's hard to reason about:// * At best it crash or talks nosense.// * At worst it talks slightly difference but hard to perceive.//@@ -2152,6 +2157,7 @@ enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype) {case GGML_FTYPE_MOSTLY_Q4_K: wtype = GGML_TYPE_Q4_K; break;case GGML_FTYPE_MOSTLY_Q5_K: wtype = GGML_TYPE_Q5_K; break;case GGML_FTYPE_MOSTLY_Q6_K: wtype = GGML_TYPE_Q6_K; break;+ case GGML_FTYPE_MOSTLY_IQ2_XXS: wtype = GGML_TYPE_IQ2_XXS; break;case GGML_FTYPE_UNKNOWN: wtype = GGML_TYPE_COUNT; break;case GGML_FTYPE_MOSTLY_Q4_1_SOME_F16: wtype = GGML_TYPE_COUNT; break;}@@ -7476,6 +7482,7 @@ static void ggml_compute_forward_add(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:{ggml_compute_forward_add_q_f32(params, src0, src1, dst);} break;@@ -7740,6 +7747,7 @@ static void ggml_compute_forward_add1(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:{ggml_compute_forward_add1_q_f32(params, src0, src1, dst);} break;@@ -7854,6 +7862,7 @@ static void ggml_compute_forward_acc(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:default:{GGML_ASSERT(false);@@ -9744,10 +9753,10 @@ static void ggml_compute_forward_group_norm(#if defined(GGML_USE_ACCELERATE) || defined(GGML_USE_OPENBLAS)// helper function to determine if it is better to use BLAS or not// for large matrices, BLAS is faster-static bool ggml_compute_forward_mul_mat_use_blas(- const struct ggml_tensor * src0,- const struct ggml_tensor * src1,- struct ggml_tensor * dst) {+static bool ggml_compute_forward_mul_mat_use_blas(struct ggml_tensor * dst) {+ const struct ggml_tensor * src0 = dst->src[0];+ const struct ggml_tensor * src1 = dst->src[1];+//const int64_t ne00 = src0->ne[0];//const int64_t ne01 = src0->ne[1];@@ -9827,7 +9836,7 @@ static void ggml_compute_forward_mul_mat(#endif#if defined(GGML_USE_ACCELERATE) || defined(GGML_USE_OPENBLAS)- if (ggml_compute_forward_mul_mat_use_blas(src0, src1, dst)) {+ if (ggml_compute_forward_mul_mat_use_blas(dst)) {if (params->ith != 0) {return;}@@ -10495,6 +10504,7 @@ static void ggml_compute_forward_out_prod(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:{ggml_compute_forward_out_prod_q_f32(params, src0, src1, dst);} break;@@ -10669,6 +10679,7 @@ static void ggml_compute_forward_set(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:default:{GGML_ASSERT(false);@@ -10863,6 +10874,7 @@ static void ggml_compute_forward_get_rows(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:{ggml_compute_forward_get_rows_q(params, src0, src1, dst);} break;@@ -11499,6 +11511,7 @@ static void ggml_compute_forward_alibi(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:case GGML_TYPE_Q8_K:case GGML_TYPE_I8:case GGML_TYPE_I16:@@ -11573,6 +11586,7 @@ static void ggml_compute_forward_clamp(case GGML_TYPE_Q4_K:case GGML_TYPE_Q5_K:case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:case GGML_TYPE_Q8_K:case GGML_TYPE_I8:case GGML_TYPE_I16:@@ -16520,11 +16534,12 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {int node_n = -1;- while (!is_interrupted) {+ while (true) {if (cplan->abort_callback && cplan->abort_callback(cplan->abort_callback_data)) {state->shared->node_n += 1;return (thread_ret_t) GGML_EXIT_ABORTED;}+if (atomic_fetch_sub(&state->shared->n_active, 1) == 1) {// all other threads are finished and spinning// do finalize and init here so we don't have synchronize again@@ -16590,14 +16605,18 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {} else {// wait for other threads to finishconst int last = node_n;- while (!is_interrupted) {++ const bool do_yield = last < 0 || cgraph->nodes[last]->op == GGML_OP_MUL_MAT;++ while (true) {// TODO: this sched_yield can have significant impact on the performance - either positive or negative// depending on the workload and the operating system.// since it is not clear what is the best approach, it should potentially become user-configurable// ref: https://github.com/ggerganov/ggml/issues/291-#if defined(GGML_USE_ACCELERATE) || defined(GGML_USE_OPENBLAS)- sched_yield();-#endif+ // UPD: adding the do_yield flag seems to resolve the issue universally+ if (do_yield) {+ sched_yield();+ }node_n = atomic_load(&state->shared->node_n);if (node_n != last) break;@@ -16676,7 +16695,7 @@ struct ggml_cplan ggml_graph_plan(struct ggml_cgraph * cgraph, int n_threads) {} else#endif#if defined(GGML_USE_ACCELERATE) || defined(GGML_USE_OPENBLAS)- if (ggml_compute_forward_mul_mat_use_blas(node->src[0], node->src[1], node)) {+ if (ggml_compute_forward_mul_mat_use_blas(node)) {if (node->src[0]->type != GGML_TYPE_F32) {// here we need memory just for single 2D matrix from src0cur = ggml_type_size(GGML_TYPE_F32)*(node->src[0]->ne[0]*node->src[0]->ne[1]);@@ -18695,6 +18714,12 @@ size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, iblock_q6_K * block = (block_q6_K*)dst + start / QK_K;result = ggml_quantize_q6_K(src + start, block, n, n, hist);} break;+ case GGML_TYPE_IQ2_XXS:+ {+ GGML_ASSERT(start % QK_K == 0);+ block_iq2_xxs * block = (block_iq2_xxs*)dst + start / QK_K;+ result = ggml_quantize_iq2_xxs(src + start, block, n, n, hist);+ } break;case GGML_TYPE_F16:{int elemsize = sizeof(ggml_fp16_t);diff --git a/llama.cpp/ggml.h b/llama.cpp/ggml.hindex e235ff52..16febaf3 100644--- a/llama.cpp/ggml.h+++ b/llama.cpp/ggml.h@@ -175,12 +175,6 @@////-#if defined(_WIN32)-# define GGML_CALL-#else-# define GGML_CALL __attribute__((__ms_abi__))-#endif-#ifdef GGML_SHARED# if defined(_WIN32) && !defined(__MINGW32__)# ifdef GGML_BUILD@@ -195,6 +189,16 @@# define GGML_API#endif+#ifdef GGML_MULTIPLATFORM+# if defined(_WIN32)+# define GGML_CALL+# else+# define GGML_CALL __attribute__((__ms_abi__))+# endif+#else+# define GGML_CALL+#endif+// TODO: support for clang#ifdef __GNUC__# define GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))@@ -348,6 +352,7 @@ extern "C" {GGML_TYPE_Q5_K = 13,GGML_TYPE_Q6_K = 14,GGML_TYPE_Q8_K = 15,+ GGML_TYPE_IQ2_XXS = 16,GGML_TYPE_I8,GGML_TYPE_I16,GGML_TYPE_I32,@@ -382,6 +387,7 @@ extern "C" {GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensorsGGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensorsGGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors+ GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors};// available tensor operations:@@ -2076,6 +2082,7 @@ extern "C" {GGML_API size_t ggml_quantize_q4_K(const float * src, void * dst, int n, int k, int64_t * hist);GGML_API size_t ggml_quantize_q5_K(const float * src, void * dst, int n, int k, int64_t * hist);GGML_API size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist);+ GGML_API size_t ggml_quantize_iq2_xxs(const float * src, void * dst, int n, int k, int64_t * hist);GGML_API size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, int start, int n, int64_t * hist);diff --git a/llama.cpp/ggml_vec_dot_iq2_xxs_q8_K.inc b/llama.cpp/ggml_vec_dot_iq2_xxs_q8_K.incnew file mode 100644index 00000000..4a237314--- /dev/null+++ b/llama.cpp/ggml_vec_dot_iq2_xxs_q8_K.inc@@ -0,0 +1,122 @@+static void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {+ assert(n % QK_K == 0);++ const block_iq2_xxs * restrict x = vx;+ const block_q8_K * restrict y = vy;++ const int nb = n / QK_K;++#if defined(__ARM_NEON)++ const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;++ uint32_t aux32[4];+ const uint8_t * aux8 = (const uint8_t *)aux32;++ ggml_int8x16x4_t q2u;+ ggml_int8x16x4_t q2s;+ ggml_int8x16x4_t q8b;++ float sumf = 0;+ for (int i = 0; i < nb; ++i) {+ const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;+ const uint16_t * restrict q2 = x[i].qs;+ const int8_t * restrict q8 = y[i].qs;+ float sumf1 = 0, sumf2 = 0;+ for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {+ q8b = ggml_vld1q_s8_x4(q8); q8 += 64;+ memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;+ q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));+ q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));+ q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));+ q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));+ q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));+ q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));+ q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));+ q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));+ q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);+ q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);+ q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);+ q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);+ const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]), q2u.val[1], q8b.val[1]);+ const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]), q2u.val[3], q8b.val[3]);+ sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));+ sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));+ }+ sumf += d*(sumf1 + sumf2);+ }+ *s = 0.25f * sumf;++#elif defined(__AVX2__)++ const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;++ uint32_t aux32[4];+ const uint8_t * aux8 = (const uint8_t *)aux32;++ __m256 accumf = _mm256_setzero_ps();+ for (int i = 0; i < nb; ++i) {+ const float d = GGML_FP16_TO_FP32__F16C(x[i].d) * y[i].d;+ const uint16_t * restrict q2 = x[i].qs;+ const int8_t * restrict q8 = y[i].qs;+ __m256i sumi1 = _mm256_setzero_si256();+ __m256i sumi2 = _mm256_setzero_si256();+ for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {+ const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;+ const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;+ memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;+ const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);+ const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);+ const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],+ signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);+ const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],+ signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);+ const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);+ const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);+ const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);+ const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);+ const uint16_t ls1 = aux32[1] >> 28;+ const uint16_t ls2 = aux32[3] >> 28;+ const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));+ const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));+ sumi1 = _mm256_add_epi32(sumi1, p1);+ sumi2 = _mm256_add_epi32(sumi2, p2);+ }++ accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);++ }++ *s = 0.125f * hsum_float_8(accumf);++#else++ uint32_t aux32[2];+ const uint8_t * aux8 = (const uint8_t *)aux32;++ float sumf = 0.f;+ for (int i = 0; i < nb; ++i) {+ const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;+ const uint16_t * restrict q2 = x[i].qs;+ const int8_t * restrict q8 = y[i].qs;+ int32_t bsum = 0;+ for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {+ memcpy(aux32, q2, 2*sizeof(uint32_t));+ q2 += 4;+ const uint32_t ls = 2*(aux32[1] >> 28) + 1;+ int32_t sumi = 0;+ for (int l = 0; l < 4; ++l) {+ const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);+ const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];+ for (int j = 0; j < 8; ++j) {+ sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);+ }+ q8 += 8;+ }+ bsum += sumi * ls;+ }+ sumf += d * bsum;+ }+ *s = 0.125f * sumf;+#endif+}diff --git a/llama.cpp/ggml_vec_dot_q5_K_q8_K_256.inc b/llama.cpp/ggml_vec_dot_q5_K_q8_K_256.incindex bd618e32..b75aad44 100644--- a/llama.cpp/ggml_vec_dot_q5_K_q8_K_256.inc+++ b/llama.cpp/ggml_vec_dot_q5_K_q8_K_256.inc@@ -12,7 +12,6 @@ static void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void *uint32_t utmp[4];-#ifdef __ARM_NEONconst uint8x16_t m4b = vdupq_n_u8(0xf);const uint8x16_t mone = vdupq_n_u8(1);diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cppindex fd71e185..c32d38ae 100644--- a/llama.cpp/llama.cpp+++ b/llama.cpp/llama.cpp@@ -1716,6 +1716,28 @@ static void llama_kv_cache_seq_shift(cache.head = new_head != cache.size ? new_head : 0;}+static void llama_kv_cache_seq_div(+ struct llama_kv_cache & cache,+ llama_seq_id seq_id,+ llama_pos p0,+ llama_pos p1,+ int d) {+ if (p0 < 0) p0 = 0;+ if (p1 < 0) p1 = std::numeric_limits<llama_pos>::max();++ for (uint32_t i = 0; i < cache.size; ++i) {+ if (cache.cells[i].has_seq_id(seq_id) && cache.cells[i].pos >= p0 && cache.cells[i].pos < p1) {+ cache.has_shift = true;++ {+ llama_pos p_old = cache.cells[i].pos;+ cache.cells[i].pos /= d;+ cache.cells[i].delta += cache.cells[i].pos - p_old;+ }+ }+ }+}+//// model loading and saving//@@ -1994,7 +2016,11 @@ struct llama_model_loader {type_max = type;}- // LLAMA_LOG_INFO("%s: - tensor %4d: %32s %-8s [ %s ]\n", __func__, i, name, ggml_type_name(meta->type), llama_format_tensor_shape(meta).c_str());+ // TODO: make runtime configurable+#if 0+ struct ggml_tensor * meta = ggml_get_tensor(ctx_meta, gguf_get_tensor_name(ctx_gguf, i));+ LLAMA_LOG_INFO("%s: - tensor %4d: %32s %-8s [ %s ]\n", __func__, i, ggml_get_name(meta), ggml_type_name(type), llama_format_tensor_shape(meta).c_str());+#endif}switch (type_max) {@@ -2010,6 +2036,7 @@ struct llama_model_loader {case GGML_TYPE_Q4_K: ftype = LLAMA_FTYPE_MOSTLY_Q4_K_M; break;case GGML_TYPE_Q5_K: ftype = LLAMA_FTYPE_MOSTLY_Q5_K_M; break;case GGML_TYPE_Q6_K: ftype = LLAMA_FTYPE_MOSTLY_Q6_K; break;+ case GGML_TYPE_IQ2_XXS: ftype = LLAMA_FTYPE_MOSTLY_IQ2_XXS; break;default:{LLAMA_LOG_WARN("%s: unknown type %s\n", __func__, ggml_type_name(type_max));@@ -2381,6 +2408,7 @@ static std::string llama_model_ftype_name(llama_ftype ftype) {case LLAMA_FTYPE_MOSTLY_Q5_K_S: return "Q5_K - Small";case LLAMA_FTYPE_MOSTLY_Q5_K_M: return "Q5_K - Medium";case LLAMA_FTYPE_MOSTLY_Q6_K: return "Q6_K";+ case LLAMA_FTYPE_MOSTLY_IQ2_XXS:return "IQ2_XSS - 2.0625 bpw";default: return "unknown, may not work";}@@ -4591,7 +4619,6 @@ struct llm_build_context {const int64_t n_embd_head = hparams.n_embd_head_v;const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);struct ggml_tensor * cur;struct ggml_tensor * inpL;@@ -4715,7 +4742,6 @@ struct llm_build_context {const int64_t n_embd_head = hparams.n_embd_head_v;const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);struct ggml_tensor * cur;struct ggml_tensor * pos;@@ -4814,9 +4840,7 @@ struct llm_build_context {struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false);const int64_t n_embd_head = hparams.n_embd_head_v;- const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);const int64_t n_rot = n_embd_head_k / 2;@@ -5028,9 +5052,7 @@ struct llm_build_context {struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false);const int64_t n_embd_head = hparams.n_embd_head_v;- const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);struct ggml_tensor * cur;struct ggml_tensor * inpL;@@ -5123,7 +5145,6 @@ struct llm_build_context {const int64_t n_embd_head = hparams.n_embd_head_v;const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);struct ggml_tensor * cur;struct ggml_tensor * inpL;@@ -5219,7 +5240,6 @@ struct llm_build_context {const int64_t n_embd_head = hparams.n_embd_head_v;const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);struct ggml_tensor * cur;struct ggml_tensor * inpL;@@ -5546,7 +5566,6 @@ struct llm_build_context {const int64_t n_embd_head = hparams.n_embd_head_v;const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);struct ggml_tensor * cur;struct ggml_tensor * attn_norm_output;@@ -5770,7 +5789,6 @@ struct llm_build_context {const int64_t n_embd_head = hparams.n_embd_head_v;const int64_t n_embd_gqa = hparams.n_embd_v_gqa();GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);- GGML_ASSERT(n_embd_gqa == n_embd);struct ggml_tensor * cur;struct ggml_tensor * pos;@@ -8837,6 +8855,7 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const scase LLAMA_FTYPE_MOSTLY_Q5_K_S:case LLAMA_FTYPE_MOSTLY_Q5_K_M: quantized_type = GGML_TYPE_Q5_K; break;case LLAMA_FTYPE_MOSTLY_Q6_K: quantized_type = GGML_TYPE_Q6_K; break;+ case LLAMA_FTYPE_MOSTLY_IQ2_XXS:quantized_type = GGML_TYPE_IQ2_XXS; break;default: throw std::runtime_error(format("invalid output file type %d\n", ftype));}@@ -9964,9 +9983,21 @@ void llama_kv_cache_seq_keep(struct llama_context * ctx, llama_seq_id seq_id) {}void llama_kv_cache_seq_shift(struct llama_context * ctx, llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) {+ if (delta == 0) {+ return;+ }+llama_kv_cache_seq_shift(ctx->kv_self, seq_id, p0, p1, delta);}+void llama_kv_cache_seq_div(struct llama_context * ctx, llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {+ if (d == 1) {+ return;+ }++ llama_kv_cache_seq_div(ctx->kv_self, seq_id, p0, p1, d);+}+// Returns the *maximum* size of the statesize_t llama_get_state_size(const struct llama_context * ctx) {// we don't know size of rng until we actually serialize it. so reserve more than enough memory for its serialized state.diff --git a/llama.cpp/llama.h b/llama.cpp/llama.hindex 88cdae18..669d23cc 100644--- a/llama.cpp/llama.h+++ b/llama.cpp/llama.h@@ -99,6 +99,7 @@ extern "C" {LLAMA_FTYPE_MOSTLY_Q5_K_S = 16, // except 1d tensorsLLAMA_FTYPE_MOSTLY_Q5_K_M = 17, // except 1d tensorsLLAMA_FTYPE_MOSTLY_Q6_K = 18, // except 1d tensors+ LLAMA_FTYPE_MOSTLY_IQ2_XXS = 19, // except 1d tensorsLLAMA_FTYPE_GUESSED = 1024, // not specified in the model file};@@ -480,6 +481,17 @@ extern "C" {llama_pos p1,llama_pos delta);+ // Integer division of the positions by factor of `d > 1`+ // If the KV cache is RoPEd, the KV data is updated accordingly+ // p0 < 0 : [0, p1]+ // p1 < 0 : [p0, inf)+ LLAMA_API void llama_kv_cache_seq_div(+ struct llama_context * ctx,+ llama_seq_id seq_id,+ llama_pos p0,+ llama_pos p1,+ int d);+//// State / sessions//diff --git a/llama.cpp/llava/llava-cli.cpp b/llama.cpp/llava/llava-cli.cppindex b11fd7a1..738ad963 100644--- a/llama.cpp/llava/llava-cli.cpp+++ b/llama.cpp/llava/llava-cli.cpp@@ -264,7 +264,7 @@ int llava_cli(int argc, char ** argv) {auto image_embed = load_image(ctx_llava, ¶ms);if (!image_embed) {- exit(1);+ return 1;}// process the promptdiff --git a/llama.cpp/main/main.1 b/llama.cpp/main/main.1index ab8a21e7..6b05d6c7 100644--- a/llama.cpp/main/main.1+++ b/llama.cpp/main/main.1@@ -487,6 +487,14 @@ Disable KV offload.KV cache data type for K..It Fl ctv Ar TYPE , Fl Fl cache-type-v Ar TYPEKV cache data type for V.+.It Fl gan Ar N , Fl Fl grp-attn-n Ar N+Group-attention factor.+.Pp+Default: 1+.It Fl gaw Ar N , Fl Fl grp-attn-w Ar N+Group-attention width.+.Pp+Default: 512.El.Sh CLI OPTIONSThe following options may be specified whendiff --git a/llama.cpp/main/main.1.asc b/llama.cpp/main/main.1.ascindex 48dec637..c933c374 100644--- a/llama.cpp/main/main.1.asc+++ b/llama.cpp/main/main.1.asc@@ -461,6 +461,16 @@ OOPPTTIIOONNSS--ccttvv _T_Y_P_E, ----ccaacchhee--ttyyppee--vv _T_Y_P_EKV cache data type for V.+ --ggaann _N, ----ggrrpp--aattttnn--nn _N+ Group-attention factor.++ Default: 1++ --ggaaww _N, ----ggrrpp--aattttnn--ww _N+ Group-attention width.++ Default: 512+CCLLII OOPPTTIIOONNSSThe following options may be specified when llllaammaaffiillee is running in ----cclliimode.diff --git a/llama.cpp/main/main.cpp b/llama.cpp/main/main.cppindex 739d2ce1..f9fec061 100644--- a/llama.cpp/main/main.cpp+++ b/llama.cpp/main/main.cpp@@ -477,6 +477,21 @@ int main(int argc, char ** argv) {LOG_TEE("sampling: \n%s\n", llama_sampling_print(sparams).c_str());LOG_TEE("sampling order: \n%s\n", llama_sampling_order_print(sparams).c_str());LOG_TEE("generate: n_ctx = %d, n_batch = %d, n_predict = %d, n_keep = %d\n", n_ctx, params.n_batch, params.n_predict, params.n_keep);++ // group-attention state+ // number of grouped KV tokens so far (used only if params.grp_attn_n > 1)+ int ga_i = 0;++ const int ga_n = params.grp_attn_n;+ const int ga_w = params.grp_attn_w;++ if (ga_n != 1) {+ GGML_ASSERT(ga_n > 0 && "grp_attn_n must be positive"); // NOLINT+ GGML_ASSERT(ga_w % ga_n == 0 && "grp_attn_w must be a multiple of grp_attn_n"); // NOLINT+ //GGML_ASSERT(n_ctx_train % ga_w == 0 && "n_ctx_train must be a multiple of grp_attn_w"); // NOLINT+ //GGML_ASSERT(n_ctx >= n_ctx_train * ga_n && "n_ctx must be at least n_ctx_train * grp_attn_n"); // NOLINT+ LOG_TEE("self-extend: n_ctx_train = %d, grp_attn_n = %d, grp_attn_w = %d\n", n_ctx_train, ga_n, ga_w);+ }LOG_TEE("\n\n");if (params.interactive) {@@ -538,37 +553,61 @@ int main(int argc, char ** argv) {fflush(stdout);}- // infinite text generation via context swapping- // if we run out of context:- // - take the n_keep first tokens from the original prompt (via n_past)- // - take half of the last (n_ctx - n_keep) tokens and recompute the logits in batches- if (n_past + (int) embd.size() + std::max<int>(0, guidance_offset) > n_ctx) {- if (params.n_predict == -2) {- LOG_TEE("\n\n%s: context full and n_predict == -%d => stopping\n", __func__, params.n_predict);- break;- }+ if (ga_n == 1) {+ // infinite text generation via context shifting+ // if we run out of context:+ // - take the n_keep first tokens from the original prompt (via n_past)+ // - take half of the last (n_ctx - n_keep) tokens and recompute the logits in batches+ if (n_past + (int) embd.size() + std::max<int>(0, guidance_offset) > n_ctx) {+ if (params.n_predict == -2) {+ LOG_TEE("\n\n%s: context full and n_predict == -%d => stopping\n", __func__, params.n_predict);+ break;+ }- const int n_left = n_past - params.n_keep - 1;- const int n_discard = n_left/2;+ const int n_left = n_past - params.n_keep - 1;+ const int n_discard = n_left/2;- LOG("context full, swapping: n_past = %d, n_left = %d, n_ctx = %d, n_keep = %d, n_discard = %d\n",- n_past, n_left, n_ctx, params.n_keep, n_discard);+ LOG("context full, swapping: n_past = %d, n_left = %d, n_ctx = %d, n_keep = %d, n_discard = %d\n",+ n_past, n_left, n_ctx, params.n_keep, n_discard);- llama_kv_cache_seq_rm (ctx, 0, params.n_keep + 1 , params.n_keep + n_discard + 1);- llama_kv_cache_seq_shift(ctx, 0, params.n_keep + 1 + n_discard, n_past, -n_discard);+ llama_kv_cache_seq_rm (ctx, 0, params.n_keep + 1 , params.n_keep + n_discard + 1);+ llama_kv_cache_seq_shift(ctx, 0, params.n_keep + 1 + n_discard, n_past, -n_discard);- n_past -= n_discard;+ n_past -= n_discard;- if (ctx_guidance) {- n_past_guidance -= n_discard;+ if (ctx_guidance) {+ n_past_guidance -= n_discard;+ }++ LOG("after swap: n_past = %d, n_past_guidance = %d\n", n_past, n_past_guidance);++ LOG("embd: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, embd).c_str());++ LOG("clear session path\n");+ path_session.clear();}+ } else {+ // context extension via Self-Extend+ while (n_past >= ga_i + ga_w) {+ const int ib = (ga_n*ga_i)/ga_w;+ const int bd = (ga_w/ga_n)*(ga_n - 1);+ const int dd = (ga_w/ga_n) - ib*bd - ga_w;- LOG("after swap: n_past = %d, n_past_guidance = %d\n", n_past, n_past_guidance);+ LOG("\n");+ LOG("shift: [%6d, %6d] + %6d -> [%6d, %6d]\n", ga_i, n_past, ib*bd, ga_i + ib*bd, n_past + ib*bd);+ LOG("div: [%6d, %6d] / %6d -> [%6d, %6d]\n", ga_i + ib*bd, ga_i + ib*bd + ga_w, ga_n, (ga_i + ib*bd)/ga_n, (ga_i + ib*bd + ga_w)/ga_n);+ LOG("shift: [%6d, %6d] + %6d -> [%6d, %6d]\n", ga_i + ib*bd + ga_w, n_past + ib*bd, dd, ga_i + ib*bd + ga_w + dd, n_past + ib*bd + dd);- LOG("embd: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, embd).c_str());+ llama_kv_cache_seq_shift(ctx, 0, ga_i, n_past, ib*bd);+ llama_kv_cache_seq_div (ctx, 0, ga_i + ib*bd, ga_i + ib*bd + ga_w, ga_n);+ llama_kv_cache_seq_shift(ctx, 0, ga_i + ib*bd + ga_w, n_past + ib*bd, dd);- LOG("clear session path\n");- path_session.clear();+ n_past -= bd;++ ga_i += ga_w/ga_n;++ LOG("\nn_past_old = %d, n_past = %d, ga_i = %d\n\n", n_past + bd, n_past, ga_i);+ }}// try to reuse a matching prefix from the loaded session instead of re-eval (via n_past)diff --git a/llama.cpp/perplexity/perplexity.cpp b/llama.cpp/perplexity/perplexity.cppindex 161e8551..b9f9d479 100644--- a/llama.cpp/perplexity/perplexity.cpp+++ b/llama.cpp/perplexity/perplexity.cpp@@ -151,8 +151,7 @@ static results_perplexity perplexity_v2(llama_context * ctx, const gpt_params &// Output: `perplexity: 13.5106 [114/114]`// BOS tokens will be added for each chunk before eval- const bool is_spm = llama_vocab_type(llama_get_model(ctx)) == LLAMA_VOCAB_TYPE_SPM;- const bool add_bos = is_spm;+ const bool add_bos = llama_should_add_bos_token(llama_get_model(ctx));fprintf(stderr, "%s: tokenizing the input ..\n", __func__);@@ -290,8 +289,7 @@ static results_perplexity perplexity(llama_context * ctx, const gpt_params & par// Output: `perplexity: 13.5106 [114/114]`// BOS tokens will be added for each chunk before eval- const bool is_spm = llama_vocab_type(llama_get_model(ctx)) == LLAMA_VOCAB_TYPE_SPM;- const bool add_bos = is_spm;+ const bool add_bos = llama_should_add_bos_token(llama_get_model(ctx));const int n_ctx = llama_n_ctx(ctx);auto tim1 = std::chrono::high_resolution_clock::now();@@ -483,7 +481,7 @@ static void hellaswag_score(llama_context * ctx, const gpt_params & params) {fprintf(stderr, "================================= is_spm = %d\n", is_spm);// This is needed as usual for LLaMA models- const bool add_bos = is_spm;+ const bool add_bos = llama_should_add_bos_token(llama_get_model(ctx));// Number of tasks to use when computing the scoreif ( params.hellaswag_tasks < hs_task_count ) {diff --git a/llama.cpp/quantize/quantize.cpp b/llama.cpp/quantize/quantize.cppindex 0e23f3bc..bb751ebf 100644--- a/llama.cpp/quantize/quantize.cpp+++ b/llama.cpp/quantize/quantize.cpp@@ -41,13 +41,6 @@ static const std::vector<struct quant_option> QUANT_OPTIONS = {};-static bool is_integer_str(const char *s) {- if (*s == '-') ++s;- if (!*s) return false;- while (isdigit(*s)) ++s;- return !*s;-}-static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftype, std::string & ftype_str_out) {std::string ftype_str;@@ -61,7 +54,7 @@ static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftypreturn true;}}- if (is_integer_str(ftype_str.c_str())) {+ try {int ftype_int = std::stoi(ftype_str);for (auto & it : QUANT_OPTIONS) {if (it.ftype == ftype_int) {@@ -71,6 +64,9 @@ static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftyp}}}+ catch (...) {+ // stoi failed+ }return false;}diff --git a/llama.cpp/train.h b/llama.cpp/train.hdeleted file mode 100644index d86c93cc..00000000--- a/llama.cpp/train.h+++ /dev/null@@ -1,231 +0,0 @@-// Various helper functions and utilities for training--#pragma once--#include <string>-#include <random>-#include <vector>--#include "ggml.h"-#include "llama.h"--typedef std::string mt19937_state;--struct train_state {- struct ggml_opt_context * opt;-- uint64_t train_its;- uint64_t train_samples;- uint64_t train_tokens;- uint64_t train_epochs;-- size_t shuffle_samples_hash; // fn, sample_count, *zip(sample_begins, sample_sizes)- mt19937_state shuffle_rng_state_current;- mt19937_state shuffle_rng_state_next;- size_t shuffle_sample_count;- size_t shuffle_next_sample;-};--struct train_params_common {- const char * fn_train_data;- const char * fn_checkpoint_in;- const char * fn_checkpoint_out;- const char * pattern_fn_it;- const char * fn_latest;-- bool print_usage;-- int save_every;-- uint32_t seed;-- int n_ctx;- int n_threads;- int n_batch;- int n_gradient_accumulation;- int n_epochs;- int n_gpu_layers;-- bool custom_n_ctx;-- bool use_flash;- bool use_checkpointing;-- std::string sample_start;- bool include_sample_start;- bool escape;- bool overlapping_samples;- bool fill_with_next_samples;- bool separate_with_eos;- bool separate_with_bos;- bool sample_random_offsets;-- bool force_reshuffle;-- int warmup;- int cos_decay_steps;- float cos_decay_restart;- float cos_decay_min;- bool enable_restart;-- int opt_past;- float opt_delta;- int opt_max_no_improvement;-- int adam_n_iter;- float adam_alpha;- float adam_min_alpha;- float adam_decay;- int adam_decay_min_ndim;- float adam_beta1;- float adam_beta2;- float adam_gclip;- float adam_eps_f;-};--typedef void (*save_train_files_callback)(void * data, struct train_state * train);--struct train_opt_callback_data {- struct train_params_common * params;- struct train_state * train;- save_train_files_callback save_cb;- void * save_data;- struct llama_context * lctx;- int last_save_iter;- llama_token * tokens_data;- size_t tokens_size;- size_t * samples_begin;- size_t * samples_size;- size_t * shuffled_samples_offs;- size_t * shuffled_samples_begin;- size_t * shuffled_samples_size;- size_t samples_count;- struct ggml_tensor * tokens_input;- struct ggml_tensor * target_probs;- int first_iter;- int first_epoch;- int iter_at_last_epoch;- int64_t last_time;- double millis_per_iter;-};--struct train_state * init_train_state();-void free_train_state(struct train_state * state);--struct train_params_common get_default_train_params_common();-void print_common_train_usage(int /*argc*/, char ** argv, const struct train_params_common * params);--bool consume_common_train_arg(int argc, char ** argv, int * idx, struct train_params_common * params, bool * invalid_param);-void finish_processing_train_args(struct train_params_common * params);--struct random_normal_distribution;-struct random_uniform_distribution;--struct random_normal_distribution * init_random_normal_distribution (int seed, float mean, float std, float min, float max);-struct random_uniform_distribution * init_random_uniform_distribution(int seed, float min, float max);--void free_random_normal_distribution (struct random_normal_distribution * rnd);-void free_random_uniform_distribution(struct random_uniform_distribution * rnd);--struct ggml_tensor * randomize_tensor_normal (struct ggml_tensor * tensor, struct random_normal_distribution * rnd);-struct ggml_tensor * randomize_tensor_uniform(struct ggml_tensor * tensor, struct random_uniform_distribution * rnd);--// generate random float in interval [0,1)-float frand();-float frand_normal (struct random_normal_distribution * rnd);-float frand_uniform(struct random_uniform_distribution * rnd);--int clamp (const int v, const int min, const int max);-float fclamp(const float v, const float min, const float max);--void assert_shape_1d(struct ggml_tensor * tensor, int64_t ne0);-void assert_shape_2d(struct ggml_tensor * tensor, int64_t ne0, int64_t ne1);-void assert_shape_3d(struct ggml_tensor * tensor, int64_t ne0, int64_t ne1, int64_t ne2);-void assert_shape_4d(struct ggml_tensor * tensor, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3);--size_t tokenize_file(- struct llama_context * lctx,- const char * filename,- const std::string & sample_start,- bool include_sample_start,- bool overlapping_samples,- unsigned context_length,- std::vector<llama_token> & out_tokens,- std::vector<size_t> & out_samples_begin,- std::vector<size_t> & out_samples_size);--int64_t get_example_targets_batch(- struct llama_context * lctx,- struct ggml_tensor * tokens_input,- struct ggml_tensor * target_probs,- int64_t example_id,- const size_t * samples_offs,- const size_t * samples_begin,- const size_t * samples_size,- size_t samples_count,- const llama_token * train_data,- size_t n_train_data,- bool separate_with_eos,- bool separate_with_bos,- bool fill_with_next_samples,- bool sample_random_offsets);---void mt19937_set_state(std::mt19937& rng, const mt19937_state& rng_state);-mt19937_state mt19937_get_state(const std::mt19937& rng);-mt19937_state mt19937_seed_to_state(unsigned seed);--mt19937_state shuffle_samples(- const mt19937_state & rng_state,- size_t * shuffled_offs,- size_t * shuffled_begins,- size_t * shuffled_sizes,- const size_t * begins,- const size_t * sizes,- size_t count);--size_t hash_combine(size_t h1, size_t h2);--size_t compute_samples_hash(- const char* fn,- const size_t* samples_begin,- const size_t* samples_size,- size_t sample_count);---std::string replace_str(const char * s, const char * needle, const char * replacement);--void print_duration(double milliseconds);--float cosine_decay(- int64_t step,- int64_t decay_steps,- float minimum);--float cosine_decay_restart(- int64_t step,- int64_t decay_steps,- float minimum,- float restart_step_mult);--float learning_schedule(- int64_t step,- int64_t warmup_steps,- int64_t decay_steps,- float learning_rate,- float overall_minimum,- float cos_decay_minimum,- float cos_decay_restart_step_mult,- bool enable_restart);--void copy_tensor_by_name(struct ggml_tensor * dst, struct ggml_context * ctx, const char * name);--void load_opt_context_gguf(struct gguf_context * fctx, struct ggml_context * f_ggml_ctx, struct ggml_opt_context * opt);-void save_opt_context_gguf(struct gguf_context * fctx, struct ggml_opt_context * opt);--bool load_train_state_gguf(struct gguf_context * fctx, struct ggml_context * f_ggml_ctx, struct train_state * train);-void save_train_state_gguf(struct gguf_context * fctx, struct train_state * train);--std::string get_train_filename(const char * filename, const char * pattern_it, const char * latest, int64_t iteration);--void train_opt_callback(void * vdata, int accum_step, float * sched, bool * cancel);diff --git a/llamafile/cuda.bat b/llamafile/cuda.batindex 9eb3c30e..d6936d13 100644--- a/llamafile/cuda.bat+++ b/llamafile/cuda.bat@@ -12,8 +12,9 @@ nvcc -arch=all ^-DNDEBUG ^-DGGML_BUILD=1 ^-DGGML_SHARED=1 ^- -DGGML_CUDA_DMMV_X=32 ^-DGGML_CUDA_MMV_Y=1 ^+ -DGGML_MULTIPLATFORM ^+ -DGGML_CUDA_DMMV_X=32 ^-DK_QUANTS_PER_ITERATION=2 ^-DGGML_CUDA_PEER_MAX_BATCH_SIZE=128 ^-DGGML_USE_TINYBLAS ^diff --git a/llamafile/cuda.c b/llamafile/cuda.cindex 3ce825c6..2021dd5b 100644--- a/llamafile/cuda.c+++ b/llamafile/cuda.c@@ -67,8 +67,9 @@ __static_yoink("llama.cpp/ggml-backend-impl.h");"-DNDEBUG", \"-DGGML_BUILD=1", \"-DGGML_SHARED=1", \- "-DGGML_CUDA_DMMV_X=32", \"-DGGML_CUDA_MMV_Y=1", \+ "-DGGML_MULTIPLATFORM", \+ "-DGGML_CUDA_DMMV_X=32", \"-DK_QUANTS_PER_ITERATION=2", \"-DGGML_CUDA_PEER_MAX_BATCH_SIZE=128", \(FLAG_tinyblas \@@ -476,6 +477,7 @@ static bool CompileAmdWindows(const char *clangxx, const char *dso, const char *"-DGGML_USE_HIPBLAS",(char *)offload_arch,"-DGGML_CUDA_MMV_Y=1",+ "-DGGML_MULTIPLATFORM","-DGGML_CUDA_DMMV_X=32","-Wno-ignored-attributes","-D_CRT_SECURE_NO_WARNINGS",@@ -521,6 +523,7 @@ static bool CompileAmdUnix(const char *dso, const char *src, const char *tmpdso)"-Wno-unused-result","-DGGML_USE_HIPBLAS","-DGGML_CUDA_MMV_Y=1",+ "-DGGML_MULTIPLATFORM","-DGGML_CUDA_DMMV_X=32",ARMS_ONLY("-ffixed-x28"),"-DK_QUANTS_PER_ITERATION=2",diff --git a/llamafile/cuda.sh b/llamafile/cuda.shindex 43efb779..f445c998 100755--- a/llamafile/cuda.sh+++ b/llamafile/cuda.sh@@ -9,8 +9,9 @@-DNDEBUG \-DGGML_BUILD=1 \-DGGML_SHARED=1 \- -DGGML_CUDA_DMMV_X=32 \-DGGML_CUDA_MMV_Y=1 \+ -DGGML_MULTIPLATFORM \+ -DGGML_CUDA_DMMV_X=32 \-DK_QUANTS_PER_ITERATION=2 \-DGGML_CUDA_PEER_MAX_BATCH_SIZE=128 \-DGGML_USE_TINYBLAS \diff --git a/llamafile/metal.c b/llamafile/metal.cindex f4c2cc5b..0faab78f 100644--- a/llamafile/metal.c+++ b/llamafile/metal.c@@ -151,14 +151,15 @@ static bool BuildMetal(const char *dso) {}char *args[] = {"cc",- "-shared",- "-O3","-I.",- "-DTARGET_OS_OSX",- "-DNDEBUG",+ "-O3","-fPIC",+ "-shared","-pthread",+ "-DNDEBUG","-ffixed-x28", // cosmo's tls register+ "-DTARGET_OS_OSX",+ "-DGGML_MULTIPLATFORM",src,"-o", tmpdso,"-framework", "Foundation",diff --git a/llamafile/rocm.sh b/llamafile/rocm.shindex 714d59f0..1e164083 100755--- a/llamafile/rocm.sh+++ b/llamafile/rocm.sh@@ -17,9 +17,10 @@ hipcc \-DGGML_SHARED=1 \-Wno-return-type \-Wno-unused-result \- -DGGML_CUDA_DMMV_X=32 \- -DGGML_CUDA_MMV_Y=1 \-DGGML_USE_HIPBLAS \+ -DGGML_CUDA_MMV_Y=1 \+ -DGGML_MULTIPLATFORM \+ -DGGML_CUDA_DMMV_X=32 \-DK_QUANTS_PER_ITERATION=2 \-DGGML_CUDA_PEER_MAX_BATCH_SIZE=128 \-o ggml-rocm.so \