Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
// (copy of foreign_quant/fq_kq_item.cuh without the Q5_K item) Q4_K / Q6_K v2 "items" (one thread, one aligned wide load pair = 4 consecutive v1 sub-chunk values), shared by the dense
// rows GEMV (fq_gemv_kq.cu) and the grouped MoE gate+up (fq_moe_kq.cu). Include after fq_gemv.cuh, inside no namespace.
#pragma once
#include <cstdint>
#include <cstring>
#include <hip/hip_fp16.h>
namespace {
__device__ __forceinline__ float fq_h2f_lo(uint32_t w)
{
uint16_t lo = (uint16_t)(w & 0xFFFFu);
__half h;
memcpy(&h, &lo, 2);
return __half2float(h);
}
__device__ __forceinline__ uint32_t fq_ld_u16x2(const uint8_t* __restrict__ p) // p is 2-byte aligned
{
const uint16_t* q = reinterpret_cast<const uint16_t*>(p);
return (uint32_t)q[0] | ((uint32_t)q[1] << 16);
}
// Q4_K item: 256-block b = it >> 2, group g = it & 3 (qs bytes 32g .. 32g+31: low nibbles = sub-block 2g,
// high nibbles = sub-block 2g+1). dst[qo] = v1 chunk value c = 16 b + 4 g + qo, qo = 0..3.
__device__ __forceinline__ void fq_kq_item_q4_k(const uint8_t* __restrict__ wr, const uint8_t* __restrict__ aq,
int it, float* __restrict__ dst)
{
const int b = it >> 2, g = it & 3;
const uint8_t* blk = wr + (size_t)b * FQ_Q4_K_BYTES; // 144 B: 16-byte aligned rows
const uint32_t hd = *reinterpret_cast<const uint32_t*>(blk); // d | dmin
const uint32_t* sp = reinterpret_cast<const uint32_t*>(blk + 4);
const uint32_t sw0 = sp[0], sw1 = sp[1], sw2 = sp[2];
const uint4 va = *reinterpret_cast<const uint4*>(blk + 16 + 32 * g);
const uint4 vb = *reinterpret_cast<const uint4*>(blk + 16 + 32 * g + 16);
const int32_t* a0 = reinterpret_cast<const int32_t*>(aq + (size_t)(b * 8 + 2 * g) * FQ_Q8_1_BYTES);
const int32_t* a1 = a0 + FQ_Q8_1_BYTES / 4;
const float dm0 = fq_h2f_lo(hd);
const float dm1 = fq_h2f_lo(hd >> 16);
const float d8[2] = {fq_h2f_lo((uint32_t)a0[0]), fq_h2f_lo((uint32_t)a1[0])};
// scales / mins of sub-blocks 2g, 2g+1 (verbatim fq_q4k_scales on the three u16 words it reads)
const int sh = 16 * (g & 1);
const uint32_t s0 = (sw0 >> sh) & 0xFFFFu, s2 = (sw1 >> sh) & 0xFFFFu, s4 = (sw2 >> sh) & 0xFFFFu;
const uint32_t hi = (g >= 2) ? 0xFFFFFFFFu : 0u;
const uint16_t aux0 = (uint16_t)(((s0 & 0x3F3Fu) & ~hi) | ((((s4 >> 0) & 0x0F0Fu) | ((s0 & 0xC0C0u) >> 2)) & hi));
const uint16_t aux1 = (uint16_t)(((s2 & 0x3F3Fu) & ~hi) | ((((s4 >> 4) & 0x0F0Fu) | ((s2 & 0xC0C0u) >> 2)) & hi));
const int sc[2] = {(int)(aux0 & 0xFFu), (int)((aux0 >> 8) & 0xFFu)};
const int mn[2] = {(int)(aux1 & 0xFFu), (int)((aux1 >> 8) & 0xFFu)};
const uint32_t w0[4] = {va.x, va.y, va.z, va.w};
const uint32_t w1[4] = {vb.x, vb.y, vb.z, vb.w};
#pragma unroll
for (int qo = 0; qo < 4; qo++)
{
float sumf_d = 0.0f, sumf_m = 0.0f;
#pragma unroll
for (int i = 0; i < 2; i++)
{
const int32_t* a = i ? a1 : a0;
const uint32_t v0i = (w0[qo] >> (4 * i)) & 0x0F0F0F0Fu;
const uint32_t v1i = (w1[qo] >> (4 * i)) & 0x0F0F0F0Fu;
const int32_t u0 = a[1 + qo], u1 = a[5 + qo];
int dot1 = fq_dp4a((int32_t)v0i, u0, 0);
dot1 = fq_dp4a((int32_t)v1i, u1, dot1);
int dot2 = fq_dp4a(0x01010101, u0, 0);
dot2 = fq_dp4a(0x01010101, u1, dot2);
sumf_d = __fadd_rn(sumf_d, __fmul_rn(d8[i], (float)(dot1 * sc[i])));
sumf_m = __fadd_rn(sumf_m, __fmul_rn(d8[i], (float)(dot2 * mn[i])));
}
dst[qo] = __fadd_rn(__fmul_rn(dm0, sumf_d), -__fmul_rn(dm1, sumf_m));
}
}
// Q6_K item: 256-block b = it >> 3, lane p = it & 7; the four v1 chunks iqs = p + 8 q (q = 0..3) of that block,
// c = 32 b + p + 8 q. dst[q] = value of chunk q.
__device__ __forceinline__ void fq_kq_item_q6_k(const uint8_t* __restrict__ wr, const uint8_t* __restrict__ aq,
int it, float* __restrict__ dst)
{
const int b = it >> 3, p = it & 7;
const uint8_t* blk = wr + (size_t)b * FQ_Q6_K_BYTES; // 210 B: 2-byte aligned only
uint32_t vl[4];
#pragma unroll
for (int q = 0; q < 4; q++)
vl[q] = fq_ld_u16x2(blk + 4 * p + 32 * q);
const uint32_t vhw[2] = {fq_ld_u16x2(blk + 128 + 4 * p), fq_ld_u16x2(blk + 128 + 32 + 4 * p)};
const int8_t* scl = reinterpret_cast<const int8_t*>(blk + 192);
const float d = fq_h2f_lo((uint32_t)*reinterpret_cast<const uint16_t*>(blk + 208));
#pragma unroll
for (int q = 0; q < 4; q++)
{
const int sc_off = 8 * (q >> 1) + (p >> 2) + 2 * (q & 1);
const int bq8_off = 4 * (q >> 1) + (q & 1);
const uint32_t vh = vhw[q >> 1] >> (2 * (q & 1));
float sumf = 0.0f;
#pragma unroll
for (int i = 0; i < 2; i++)
{
const int scc = (int)scl[sc_off + 4 * i];
const uint32_t vil = (vl[q] >> (4 * i)) & 0x0F0F0F0Fu;
const uint32_t vih = ((vh >> (4 * i)) << 4) & 0x30303030u;
const uint32_t vi = fq_vsub4(vil | vih, 0x20202020u);
const int32_t* a = reinterpret_cast<const int32_t*>(aq + (size_t)(b * 8 + bq8_off + 2 * i) * FQ_Q8_1_BYTES);
const float d8 = fq_h2f_lo((uint32_t)a[0]);
sumf = __fadd_rn(sumf, __fmul_rn(d8, (float)(fq_dp4a((int32_t)vi, a[1 + p], 0) * scc)));
}
dst[q] = __fmul_rn(d, sumf);
}
}
// ---- Q3_K and Q8_0 lane dots (verbatim fq_dot_q3_k_q8 / fq_dot_q8_0_q8 of fq_gemv_hip.cuh: lane tid of nthreads sums sub-chunks c = tid, tid + nthreads, ...) ----
#ifndef FQ_Q3_K_BYTES
#define FQ_Q3_K_BYTES 110
#endif
#ifndef FQ_Q8_0_BYTES
#define FQ_Q8_0_BYTES 34
#endif
__device__ __forceinline__ float gm_dot_q3_k(const uint8_t* __restrict__ wr, const uint8_t* __restrict__ aqb, int K, int tid, int nthreads)
{
const int nsub = K / 16;
float acc = 0.0f;
for (int c = tid; c < nsub; c += nthreads)
{
const int b = c >> 4;
const int iqs = c & 15;
const uint8_t* blk = wr + (size_t)b * FQ_Q3_K_BYTES;
const int bq8_offset = 4 * (iqs / 8);
const int scale_offset = iqs - iqs % 8 + (iqs % 8) / 4;
int32_t vl, vm;
memcpy(&vl, blk + 32 + 4 * iqs, 4);
memcpy(&vm, blk + 4 * (iqs % 8), 4);
const int32_t vh = ~vm >> bq8_offset;
const uint8_t* scales = blk + 96;
__half dh;
memcpy(&dh, blk + 108, 2);
const float d = __half2float(dh);
float sumf = 0.0f;
#pragma unroll
for (int i = 0; i < 4; i++)
{
const int isc = scale_offset + 2 * i;
const int sc_low = (scales[isc % 8] >> (4 * (isc / 8))) & 0xF;
const int sc_high = ((scales[8 + isc % 4] >> (2 * (isc / 4))) & 3) << 4;
const int sc = (sc_low | sc_high) - 32;
const uint32_t vil = ((uint32_t)vl >> (2 * i)) & 0x03030303u;
const uint32_t vih = (uint32_t)((vh >> i) << 2) & 0x04040404u;
const uint32_t vi = fq_vsub4(vil, vih);
const uint8_t* aqb_blk = aqb + (size_t)(b * 8 + bq8_offset + i) * FQ_Q8_1_BYTES;
__half dah;
memcpy(&dah, aqb_blk, 2);
const float d8 = __half2float(dah);
int32_t u;
memcpy(&u, aqb_blk + 4 + 4 * (iqs % 8), 4);
sumf = __fadd_rn(sumf, __fmul_rn(d8, (float)(fq_dp4a((int32_t)vi, u, 0) * sc)));
}
acc = __fadd_rn(acc, __fmul_rn(d, sumf));
}
return acc;
}
__device__ __forceinline__ float gm_dot_q8_0(const uint8_t* __restrict__ wr, const uint8_t* __restrict__ aqb, int K, int tid, int nthreads)
{
const int nchunks = K / 32;
float acc = 0.0f;
for (int c = tid; c < nchunks; c += nthreads)
{
const uint8_t* blk = wr + (size_t)c * FQ_Q8_0_BYTES;
__half dwh;
memcpy(&dwh, blk, 2);
const float dw = __half2float(dwh);
__half dah;
memcpy(&dah, aqb + (size_t)c * FQ_Q8_1_BYTES, 2);
const float da = __half2float(dah);
const int8_t* aq = reinterpret_cast<const int8_t*>(aqb + (size_t)c * FQ_Q8_1_BYTES + 4);
const uint16_t* w16 = reinterpret_cast<const uint16_t*>(blk + 2);
const int32_t* aq32 = reinterpret_cast<const int32_t*>(aq);
int sumi = 0;
#pragma unroll
for (int j2 = 0; j2 < 8; j2++)
{
const int32_t w4 = (int32_t)(w16[j2 * 2] | ((uint32_t)w16[j2 * 2 + 1] << 16));
sumi = fq_dp4a(w4, aq32[j2], sumi);
}
acc = __fadd_rn(acc, __fmul_rn(__fmul_rn(dw, da), (float)sumi));
}
return acc;
}
} // namespace