@@ -108,6 +108,20 @@ void quantize_row_tq2_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy,
108108 quantize_row_tq2_0_ref (x , y , k );
109109}
110110
111+ // ====================== TurboQuant (TBQ) quantization ========================
112+
113+ void quantize_row_tbq3_0 (const float * GGML_RESTRICT x , void * GGML_RESTRICT vy , int64_t k ) {
114+ assert (k % QK_K == 0 );
115+ block_tbq3_0 * GGML_RESTRICT y = vy ;
116+ quantize_row_tbq3_0_ref (x , y , k );
117+ }
118+
119+ void quantize_row_tbq4_0 (const float * GGML_RESTRICT x , void * GGML_RESTRICT vy , int64_t k ) {
120+ assert (k % QK_K == 0 );
121+ block_tbq4_0 * GGML_RESTRICT y = vy ;
122+ quantize_row_tbq4_0_ref (x , y , k );
123+ }
124+
111125//===================================== Q8_K ==============================================
112126
113127void quantize_row_q8_K_generic (const float * GGML_RESTRICT x , void * GGML_RESTRICT y , int64_t k ) {
@@ -456,6 +470,89 @@ void ggml_vec_dot_tq2_0_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs,
456470 * s = sumf ;
457471}
458472
473+ // ====================== TurboQuant vec_dot (dequant-then-dot) ====================
474+ //
475+ // These implementations fully dequantize the TBQ row (including inverse rotation),
476+ // then compute the dot product with the Q8_K row. The inverse rotation is O(d²).
477+ //
478+ // TODO: A faster approach would rotate the activation into the codebook domain
479+ // (Q^T · activation) and dot directly with codebook entries, avoiding inverse rotation.
480+ // This would require the rotation matrix to be accessible here.
481+
482+ #if defined(__STDC_VERSION__ ) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__ )
483+ #define TURBOQ_VD_TL _Thread_local
484+ #elif defined(__GNUC__ ) || defined(__clang__ )
485+ #define TURBOQ_VD_TL __thread
486+ #elif defined(_MSC_VER )
487+ #define TURBOQ_VD_TL __declspec(thread)
488+ #else
489+ #define TURBOQ_VD_TL
490+ #endif
491+
492+ static TURBOQ_VD_TL float * tbq_vd_buf = NULL ;
493+ static TURBOQ_VD_TL int64_t tbq_vd_buf_size = 0 ;
494+
495+ static float * tbq_vd_get_scratch (int64_t n ) {
496+ if (n > tbq_vd_buf_size ) {
497+ free (tbq_vd_buf );
498+ tbq_vd_buf = (float * )malloc (n * sizeof (float ));
499+ tbq_vd_buf_size = n ;
500+ }
501+ return tbq_vd_buf ;
502+ }
503+
504+ void ggml_vec_dot_tbq3_0_q8_K_generic (int n , float * GGML_RESTRICT s , size_t bs , const void * GGML_RESTRICT vx , size_t bx , const void * GGML_RESTRICT vy , size_t by , int nrc ) {
505+ assert (nrc == 1 );
506+ UNUSED (nrc );
507+ UNUSED (bx );
508+ UNUSED (by );
509+ UNUSED (bs );
510+
511+ float * tmp = tbq_vd_get_scratch (n );
512+ dequantize_row_tbq3_0 ((const block_tbq3_0 * )vx , tmp , n );
513+
514+ const block_q8_K * GGML_RESTRICT y = vy ;
515+ const int nb = n / QK_K ;
516+
517+ float sumf = 0.0f ;
518+ int64_t idx = 0 ;
519+ for (int i = 0 ; i < nb ; i ++ ) {
520+ const float d = y [i ].d ;
521+ for (int j = 0 ; j < QK_K ; j ++ ) {
522+ sumf += tmp [idx ] * (d * y [i ].qs [j ]);
523+ idx ++ ;
524+ }
525+ }
526+
527+ * s = sumf ;
528+ }
529+
530+ void ggml_vec_dot_tbq4_0_q8_K_generic (int n , float * GGML_RESTRICT s , size_t bs , const void * GGML_RESTRICT vx , size_t bx , const void * GGML_RESTRICT vy , size_t by , int nrc ) {
531+ assert (nrc == 1 );
532+ UNUSED (nrc );
533+ UNUSED (bx );
534+ UNUSED (by );
535+ UNUSED (bs );
536+
537+ float * tmp = tbq_vd_get_scratch (n );
538+ dequantize_row_tbq4_0 ((const block_tbq4_0 * )vx , tmp , n );
539+
540+ const block_q8_K * GGML_RESTRICT y = vy ;
541+ const int nb = n / QK_K ;
542+
543+ float sumf = 0.0f ;
544+ int64_t idx = 0 ;
545+ for (int i = 0 ; i < nb ; i ++ ) {
546+ const float d = y [i ].d ;
547+ for (int j = 0 ; j < QK_K ; j ++ ) {
548+ sumf += tmp [idx ] * (d * y [i ].qs [j ]);
549+ idx ++ ;
550+ }
551+ }
552+
553+ * s = sumf ;
554+ }
555+
459556void ggml_vec_dot_q2_K_q8_K_generic (int n , float * GGML_RESTRICT s , size_t bs , const void * GGML_RESTRICT vx , size_t bx , const void * GGML_RESTRICT vy , size_t by , int nrc ) {
460557 assert (nrc == 1 );
461558 UNUSED (nrc );
0 commit comments