Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void Inkling2::load_geometry() {
one_sequence(false, "on Metal");
#endif
#if defined(STRATA_USE_HIP)
// the _rows kernels are the fused q/k/v and clips kernels HIP does not run (inkling2.cu: clips_apart())
// the _rows kernels are the fused q/k/v and clips kernels HIP does not run (inkling2.cu: clips_hip())
one_sequence(false, "on AMD");
#endif
one_sequence(opt_.draft_model.empty(), "with --draft-model");
Expand Down
101 changes: 64 additions & 37 deletions engine/src/poly/inkling2.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3011,42 +3011,68 @@ void ink_moe_conv_add(const float* x, const float* w, const float* scale, const
}

#if defined(__HIPCC__)
// HIP: ink_clips() as the launches its contract names (ink_clip(), ink_head_rms() and fp16 stores of its jobs), and
// ink_qkv() as ink_conv() of K and V and then those; the fused kernels are not built there. ink_clips_kernel and
// ink_qkv_kernel never finished on an RX 7900 XT (gfx1100) or an RX 9070 XT (gfx1201) under ROCm 7.2.4, the first
// prompt or token of Inkling 2 Mini with them, and the same code built with any change to ink_clips_kernel finished:
// the compiler's code for these two kernels, not their arithmetic. The separate kernels give the same bits
// HIP: ink_clips() in one launch of a kernel of its own, and ink_qkv() as ink_conv_few() of K and V and then that.
// ink_clips_kernel and ink_qkv_kernel never finished on an RX 7900 XT (gfx1100) or an RX 9070 XT (gfx1201) under
// ROCm 7.2.4 (the first prompt or token of Inkling 2 Mini with them), and the same code built with any change to
// ink_clips_kernel finished: the compiler's code for those two, not their arithmetic. This one picks its job by
// comparisons and copies the job's fields out, where ink_clips_kernel indexes the arrays by a counted loop; each block
// is a token's head through the same clip_head() / rms_head() and the same stores, so the bits are those of
// ink_clip(), ink_head_rms() and store_f16() / store_f16_at() / ink_store_ring() / kvq8_store() one at a time
// (poly_inkling2_test's clips and qkv cases compare them).
// A Q8_0 cache's rows (kvq8_store()'s), for clips_apart(): a block a row's head, its cell pos + row (at[0] + row
// with at), % ring when ring > 0.
__global__ void ink_store_q8_kernel(const float* x, uint8_t* cache, int n_kv, int dim, long long pos,
const long long* at, int ring) {
const int row = blockIdx.x / n_kv, h = blockIdx.x % n_kv;
long long cell = (at ? at[0] : pos) + row;
if (ring > 0) cell %= ring;
kvq8_store(x + (size_t(row) * n_kv + h) * dim, cache, cell, n_kv, dim, h);
struct ClipJob {
float* x;
const float* scale;
float thr, alpha, floor_;
int dim, heads;
bool rms;
void* cache;
};

__device__ __forceinline__ ClipJob clip_job(const InkClips& c, int j) {
switch (j) {
case 0: return {c.x[0], c.scale[0], c.thr[0], c.alpha[0], c.floor[0], c.dim[0], c.heads[0], c.rms[0], c.cache[0]};
case 1: return {c.x[1], c.scale[1], c.thr[1], c.alpha[1], c.floor[1], c.dim[1], c.heads[1], c.rms[1], c.cache[1]};
case 2: return {c.x[2], c.scale[2], c.thr[2], c.alpha[2], c.floor[2], c.dim[2], c.heads[2], c.rms[2], c.cache[2]};
default: return {c.x[3], c.scale[3], c.thr[3], c.alpha[3], c.floor[3], c.dim[3], c.heads[3], c.rms[3], c.cache[3]};
}
}

static void clips_apart(const InkClips& c, void* stream) {
if (c.kv8) throw std::logic_error("ink_clips: no FP8 caches under HIP");
for (int j = 0; j < 4; ++j) {
if (c.heads[j] <= 0) continue;
if (c.rms[j])
ink_head_rms(c.x[j], c.dim[j], c.heads[j], c.rows, c.eps, stream);
else
ink_clip(c.x[j], c.scale[j], c.thr[j], c.dim[j], c.heads[j], c.rows, c.pos, c.alpha[j], c.floor[j], stream,
c.at);
if (!c.cache[j]) continue;
const int n = c.heads[j] * c.dim[j];
if (c.kvq8) {
if (c.dim[j] % 32) throw std::invalid_argument("ink_clips: Q8_0 caches need heads of whole 32-value blocks");
ink_store_q8_kernel<<<c.rows * c.heads[j], 128, 0, cs(stream)>>>(c.x[j], (uint8_t*) c.cache[j], c.heads[j],
c.dim[j], c.pos, c.at, c.ring);
} else if (c.at) store_f16_at(c.x[j], c.cache[j], n, c.at, c.rows, c.ring, stream);
else if (c.ring > 0) ink_store_ring(c.x[j], c.cache[j], n, c.pos, c.rows, c.ring, stream);
else store_f16(c.x[j], c.cache[j], n, c.pos, c.rows, stream);
launched("ink_clips' stores");
template <int KV>
__launch_bounds__(128) __global__ void ink_clips_hip_kernel(InkClips c) {
__shared__ float part[128 / WARP];
const int e0 = c.rows * c.heads[0], e1 = e0 + c.rows * c.heads[1], e2 = e1 + c.rows * c.heads[2];
const int bx = (int) blockIdx.x;
const int j = bx < e0 ? 0 : bx < e1 ? 1 : bx < e2 ? 2 : 3;
const int b = bx - (j == 0 ? 0 : j == 1 ? e0 : j == 2 ? e1 : e2);
const ClipJob J = clip_job(c, j);
const int row = b / J.heads, head = b % J.heads;
float* v = J.x + (size_t(row) * J.heads + head) * J.dim;
if (J.rms) rms_head(v, J.dim, c.eps, part);
else
clip_head(v, J.scale ? J.scale + size_t(head) * J.dim : nullptr, J.thr, J.dim, J.alpha, J.floor_,
c.at ? c.at[2 * row] : c.pos + row, part);
if (!J.cache) return; // each thread its own values, as clip_head() and rms_head() left them
long long cell = (c.at ? c.at[0] : c.pos) + row;
if (c.ring > 0) cell %= c.ring;
if (KV == kKvQ8) {
kvq8_store(v, (uint8_t*) J.cache, cell, J.heads, J.dim, head);
return;
}
__half* to = (__half*) J.cache + (size_t) cell * J.heads * J.dim + (size_t) head * J.dim;
for (int i = threadIdx.x; i < J.dim; i += blockDim.x) to[i] = __float2half(v[i]);
}

static void clips_hip(const InkClips& c, void* stream) {
if (c.kv8) throw std::logic_error("ink_clips: no FP8 caches under HIP");
if (c.kvq8)
for (int j = 0; j < 4; ++j)
if (c.cache[j] && c.dim[j] % 32)
throw std::invalid_argument("ink_clips: Q8_0 caches need heads of whole 32-value blocks");
const int blocks = c.rows * (c.heads[0] + c.heads[1] + c.heads[2] + c.heads[3]);
if (blocks <= 0) return; // no heads: nothing to launch (a grid of 0 blocks is an error)
auto* k = c.kvq8 ? ink_clips_hip_kernel<kKvQ8> : ink_clips_hip_kernel<kKvF16>;
k<<<blocks, 128, 0, cs(stream)>>>(c);
launched("ink_clips");
}
#endif

Expand All @@ -3055,9 +3081,10 @@ void ink_qkv(const InkQkv& a, void* stream) {
throw std::invalid_argument("ink_qkv: heads of up to 128 values, 2 to 8 taps");
#if defined(__HIPCC__)
if (a.kv8) throw std::logic_error("ink_qkv: no FP8 caches under HIP");
const int kv = a.n_kv * a.dim;
ink_conv(a.kraw, a.kstate, a.kw, a.kdyn, a.k, kv, a.n_kv, a.taps, 1, stream);
ink_conv(a.vraw, a.vstate, a.vw, a.vdyn, a.v, kv, a.n_kv, a.taps, 1, stream);
InkConv ck, cv; // ink_conv()'s of each, in one launch
ck.x = a.kraw, ck.state = a.kstate, ck.w = a.kw, ck.dyn = a.kdyn, ck.out = a.k;
cv.x = a.vraw, cv.state = a.vstate, cv.w = a.vw, cv.dyn = a.vdyn, cv.out = a.v;
ink_conv_few(ck, cv, a.n_kv * a.dim, a.n_kv, a.taps, 1, stream, 0, 0);
InkClips c;
float* xs[4] = {a.q, a.k, a.v, a.r};
const float* sc[4] = {a.q_scale, nullptr, nullptr, a.r_scale};
Expand All @@ -3078,7 +3105,7 @@ void ink_qkv(const InkQkv& a, void* stream) {
c.ring = a.ring;
c.kv8 = a.kv8;
c.kvq8 = a.kvq8;
clips_apart(c, stream);
clips_hip(c, stream);
#else
QkvArgs g{a, 1.0f / std::sqrt((float) ((a.kdyn ? 2 : 1) * a.taps + 1)),
1.0f / std::sqrt((float) ((a.vdyn ? 2 : 1) * a.taps + 1))};
Expand Down Expand Up @@ -3112,7 +3139,7 @@ void ink_clip(float* x, const float* scale, float thr, int dim, int heads, int r
void ink_clips(const InkClips& c, void* stream) {
if (c.rows <= 0) return;
#if defined(__HIPCC__)
clips_apart(c, stream);
clips_hip(c, stream);
#else
auto* k = c.kvq8 ? ink_clips_kernel<kKvQ8> : c.kv8 ? ink_clips_kernel<kKvFp8> : ink_clips_kernel<kKvF16>;
k<<<c.rows * (c.heads[0] + c.heads[1] + c.heads[2] + c.heads[3]), 128, 0, cs(stream)>>>(c);
Expand Down
20 changes: 20 additions & 0 deletions engine/src/poly/inkling2_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,26 @@ void case_clips() {
ink_clips(st, stream());
same_bits(tag + ": the stores", keys.get(), keys2.get());
same_bits(tag + ": the stores (values)", vals.get(), vals2.get());
if (!kv8) { // the fp16 stores against store_f16() / ink_store_ring() and store_f16_at() of the same rows
const int n = kKv * kDim;
Dev<uint8_t> k3(cache_bytes(false, cells)), v3(cache_bytes(false, cells)), k4(cache_bytes(false, cells)),
v4(cache_bytes(false, cells));
Dev<long long> at(std::vector<long long>{pos, 0});
if (ring) {
ink_store_ring(dk2, (uint16_t*) k3.p, n, pos, rows, ring, stream());
ink_store_ring(dv2, (uint16_t*) v3.p, n, pos, rows, ring, stream());
} else {
store_f16(dk2, (uint16_t*) k3.p, n, pos, rows, stream());
store_f16(dv2, (uint16_t*) v3.p, n, pos, rows, stream());
}
store_f16_at(dk2, (uint16_t*) k4.p, n, at.p, rows, ring, stream());
store_f16_at(dv2, (uint16_t*) v4.p, n, at.p, rows, ring, stream());
const char* plain = ring ? "ink_store_ring()" : "store_f16()";
same_bits(tag + ": K stored = " + plain, keys.get(), k3.get());
same_bits(tag + ": V stored = " + plain, vals.get(), v3.get());
same_bits(tag + ": K stored = store_f16_at()", keys.get(), k4.get());
same_bits(tag + ": V stored = store_f16_at()", vals.get(), v4.get());
}
// the K reference, and the rows where the stores put it
std::vector<double> kref(k.size());
for (size_t h = 0; h < (size_t) rows * kKv; ++h)
Expand Down
Loading