11#include <cuda_runtime.h>
33 const long long per =
static_cast<long long>(
T) *
nb;
36 if (
gw >= 2 *
per)
return;
39 const int t =
static_cast<int>(
w /
nb),
b =
static_cast<int>(
w -
static_cast<long long>(
t) *
nb);
55 for (
int o = 16;
o > 0;
o >>= 1) {
64 const int qi =
static_cast<int>(q);
75 const size_t n =
static_cast<size_t>(
T) *
row;
77 if (
i >= 2 *
n)
return;
78 const bool isv =
i >=
n;
79 const size_t e =
isv ?
i -
n :
i;
96 }
else if (
F ==
kQ8) {
103 const unsigned u =
__ldg(
reinterpret_cast<const unsigned short*
>(
D +
at / 2));
108 __fmaf_rn(
static_cast<float>((
u >> 8) & 15u), d,
m),
109 __fmaf_rn(
static_cast<float>((
u >> 12) & 15u), d,
m));
125 const int L =
dpos ? *
dpos + 1 : seq_len;
134 const int n =
k1 -
k0;
138 float*
s =
reinterpret_cast<float*
>(
q4 +
d4n);
151 for (
int j = ql;
j <
d4n;
j += 4) {
193 for (
int i = g;
i <
n;
i +=
G) {
194 const float w =
s[
i];
204 const float*
pacc =
reinterpret_cast<const float*
>(
pacc4);
214 const int L =
dpos ? *
dpos + 1 : seq_len;
224 const int r4 =
row / 4;
225 const size_t per =
static_cast<size_t>(
n) *
r4;
227 if (
i >= 2 *
per)
return;
230 const size_t pos =
w /
r4;
231 const int e =
static_cast<int>(
w -
pos *
r4) * 4;
251 const size_t total = 2 *
static_cast<size_t>(
n) * (
row / 4);
264 const size_t total = 2 *
static_cast<size_t>(
T) *
row;
269 const unsigned grid =
static_cast<unsigned>((
threads + 127) / 128);
275 write_block_kernel<kQ8><<<
grid, 128, 0,
st>>>(
kd,
ks,
vd,
vs,
k,
v,
T,
row,
start_pos,
dpos);
277 write_block_kernel<kQ4><<<
grid, 128, 0,
st>>>(
kd,
ks,
vd,
vs,
k,
v,
T,
row,
start_pos,
dpos);
279 throw std::runtime_error(
"kv_quant write: format " + std::to_string(
f));
292 throw std::runtime_error(
"kv_quant decode: shape not supported (heads " + std::to_string(
hq) +
"/" +
293 std::to_string(
hkv) +
", head_dim " + std::to_string(
hd) +
", max_len " +
294 std::to_string(max_len) +
")");
296 case kF16:
decode_launch<kF16>(q,
Kd,
Ks,
Vd,
Vs,
out,
hq,
hkv,
hd,
scale, max_len, seq_len,
part,
st,
dpos);
break;
297 case kQ8:
decode_launch<kQ8>(q,
Kd,
Ks,
Vd,
Vs,
out,
hq,
hkv,
hd,
scale, max_len, seq_len,
part,
st,
dpos);
break;
298 case kQ4:
decode_launch<kQ4>(q,
Kd,
Ks,
Vd,
Vs,
out,
hq,
hkv,
hd,
scale, max_len, seq_len,
part,
st,
dpos);
break;
299 default:
throw std::runtime_error(
"kv_quant decode: format " + std::to_string(
f));
310 default:
throw std::runtime_error(
"kv_quant dequant: format " + std::to_string(
f));
void write_cuda(int f, void *Kd, void *Ks, void *Vd, void *Vs, const float *k, const float *v, int T, int row, int start_pos, cudaStream_t st, const int *dpos)
size_t partial_floats(int hq, int hd, int max_len)
void decode_cuda(int f, const float *q, const void *Kd, const void *Ks, const void *Vd, const void *Vs, float *out, int hq, int hkv, int hd, float scale, int max_len, int seq_len, float *part, cudaStream_t st, const int *dpos)
void dequant_rows_cuda(int f, const void *Kd, const void *Ks, const void *Vd, const void *Vs, float *K, float *V, int n, int row, cudaStream_t st)
__host__ bool tiled_shape_ok(int heads, int head_dim, int max_len, int heads_max, int min_chunk, int max_chunks)
__host__ size_t tiled_shm_bytes(int head_dim, int max_len, int min_chunk, int max_chunks)
constexpr int kTiledThreads
constexpr int kTiledMaxChunks
__host__ __device__ int tiled_chunk(int L, int min_chunk, int max_chunks)
constexpr int kTiledMinChunk
__device__ __forceinline__ void tiled_merge_block(const float *__restrict__ base, float *__restrict__ out_h, int L, int head_dim, int min_chunk, int max_chunks)
__host__ __device__ int tiled_grid_chunks(int max_len, int min_chunk, int max_chunks)