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
4 changes: 4 additions & 0 deletions engine/src/poly/gemm_small_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ int main() {
int bad = 0;
for (int n_out : {32, 64, 100, 128, 256})
for (int rows : {32, 37, 1024, 2048}) bad += check(h, 2048, n_out, rows, rng);
for (int rows : {37, 1024, 2048, 8192}) { // Inkling 2 Mini's controllers' down matrices and its router
bad += check(h, 4608, 64, rows, rng);
bad += check(h, 2304, 128, rows, rng);
}
bad += check(h, 2000, 64, 45, rng); // inputs that end inside a step
bad += check(h, 2000, 256, 45, rng); // and of gemm_tile_kernel's
cublasDestroy(h);
Expand Down
19 changes: 19 additions & 0 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,11 +940,30 @@ void Inkling2::control(const float* cur, const float* msg, const Ctrl& c, float*
const int outs[2] = {n_embd_, n_embd_};
gemv_set(w, x, y, outs, 2, rank_, n, st_);
} else {
#if defined(STRATA_USE_HIP)
// a prompt's: lin_pair()'s two hipblasSgemm calls and the gate in one launch, the same bits
if (up_gate_ok(c, n) && ink_ctrl_up_gate(cur, msg, ch_, c.gain.p, c.forget.p, out, n_embd_, rank_, n, st_,
c.gain.type == kTypeBF16))
return;
#endif
lin_pair(c.gain, c.forget, ch_, gain_, forget_, n, nullptr);
}
ink_gate(cur, msg, gain_, forget_, out, (size_t) n * n_embd_, st_);
}

#if defined(STRATA_USE_HIP)
// Where lin_pair() takes a controller's gain and forget through gemm_f32()'s hipblasSgemm (a prompt batch of F32, or
// of BF16 widened into wf_; more than the 256 outputs gemm_f32() keeps for its own kernels), whose bits
// ink_ctrl_up_gate() has.
bool Inkling2::up_gate_ok(const Ctrl& c, int n) const {
const auto blas = [&](const Dev& d) {
return d.scale == 1.0f && d.n_in == rank_ && d.n_out == n_embd_ &&
(d.type == kTypeF32 || (d.type == kTypeBF16 && wf_ && (size_t) d.n_in * d.n_out <= bf16_max_));
};
return n >= kStageRows && n_embd_ > 256 && c.gain.type == c.forget.type && blas(c.gain) && blas(c.forget);
}
#endif

// A later pass's FFN controller for one token (control() on mixf_), its gate in the launch of the experts' input
// norm and quantization (native_add_rms_norm_q8_1()'s): the same bytes. False, with nothing launched, where
// control() and that norm do it. `row`: the token's row of mixf_, msg, xn_ and xq_ (step()'s rows, one at a time).
Expand Down
58 changes: 58 additions & 0 deletions engine/src/poly/inkling2.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,64 @@ void ink_gate_set(const float* const* cur, const float* const* msg, const float*
launched("ink_gate");
}

// ink_ctrl_up_gate(): a prompt's gain and forget products of a controller and its gate in one launch. A thread has
// one output (value o of a row) and its two weight rows in registers, and goes through kUpTokens tokens whose ch rows
// the block holds. Each product is one fmaf after the other over the rank in its order from 0, as hipblasSgemm's
// kernel for these shapes does it (the same bits on gfx1201 under ROCm 7.2 and 10.2; inkling2_test's ctrl_up_gate
// checks it against lin()'s launches), then ink_gate_kernel's arithmetic on them.
constexpr int kUpThreads = 256, kUpTokens = 32, kUpRank = 64;

template <typename W>
__global__ void __launch_bounds__(kUpThreads) ink_ctrl_up_gate_kernel(const float* cur, const float* msg,
const float* __restrict__ ch,
const W* __restrict__ gw,
const W* __restrict__ fw, float* out,
int n_embd, int rows) {
__shared__ __align__(16) float sc[kUpTokens][kUpRank];
const int o = blockIdx.x * kUpThreads + threadIdx.x, t0 = blockIdx.y * kUpTokens;
const int nt = min(kUpTokens, rows - t0);
for (int i = threadIdx.x; i < kUpTokens * kUpRank; i += kUpThreads) {
const int t = i / kUpRank;
sc[t][i % kUpRank] = t < nt ? ch[size_t(t0 + t) * kUpRank + i % kUpRank] : 0.0f;
}
__syncthreads();
if (o >= n_embd) return;
float g[kUpRank], f[kUpRank];
#pragma unroll
for (int k = 0; k < kUpRank; ++k) {
g[k] = wval(gw, size_t(o) * kUpRank + k);
f[k] = wval(fw, size_t(o) * kUpRank + k);
}
for (int t = 0; t < nt; ++t) {
float a = 0.0f, b = 0.0f;
#pragma unroll
for (int k = 0; k < kUpRank; k += 4) {
const float4 x = *reinterpret_cast<const float4*>(&sc[t][k]);
a = fmaf(x.x, g[k], a), b = fmaf(x.x, f[k], b);
a = fmaf(x.y, g[k + 1], a), b = fmaf(x.y, f[k + 1], b);
a = fmaf(x.z, g[k + 2], a), b = fmaf(x.z, f[k + 2], b);
a = fmaf(x.w, g[k + 3], a), b = fmaf(x.w, f[k + 3], b);
}
const size_t i = size_t(t0 + t) * n_embd + o;
const float c = cur[i];
out[i] = c - tanhf(b) * c + tanhf(a) * msg[i];
}
}

bool ink_ctrl_up_gate(const float* cur, const float* msg, const float* ch, const void* gain, const void* forget,
float* out, int n_embd, int rank, int rows, void* stream, bool bf16) {
if (rank != kUpRank || rows <= 0) return false;
const dim3 g(grid((size_t) n_embd, kUpThreads), grid((size_t) rows, kUpTokens));
if (bf16)
ink_ctrl_up_gate_kernel<uint16_t><<<g, kUpThreads, 0, cs(stream)>>>(
cur, msg, ch, (const uint16_t*) gain, (const uint16_t*) forget, out, n_embd, rows);
else
ink_ctrl_up_gate_kernel<float><<<g, kUpThreads, 0, cs(stream)>>>(cur, msg, ch, (const float*) gain,
(const float*) forget, out, n_embd, rows);
launched("ink_ctrl_up_gate");
return true;
}

void ink_gate(const float* cur, const float* msg, const float* gain, const float* forget, float* out, size_t n,
void* stream) {
ink_gate_set(&cur, &msg, &gain, &forget, &out, 1, n, stream);
Expand Down
3 changes: 3 additions & 0 deletions engine/src/poly/inkling2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ class Inkling2 final : public Engine {
/// The stream waits for what side_ has.
void join();
void control(const float* cur, const float* msg, const Ctrl& c, float* out, int n);
#if defined(STRATA_USE_HIP)
bool up_gate_ok(const Ctrl& c, int n) const;
#endif
bool control3(const float* const* cur, float* const* msg, const Ctrl* c, float* const* out, int n);
bool project(const Block& B, const float* const* x, int n, bool quantized = false, bool* extras = nullptr);
bool control_ffn(const float* msg, const Ctrl& c, const Block& B, int row = 0);
Expand Down
6 changes: 6 additions & 0 deletions engine/src/poly/inkling2_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ void ink_control3_gate(const float* const* cur, const float* const* msg, const f
void ink_gate_set(const float* const* cur, const float* const* msg, const float* const* gain,
const float* const* forget, float* const* out, int count, size_t n, void* stream);

/// A prompt's controller after its down product: gain = ch gain_w^T and forget = ch forget_w^T (rank values in, n_embd
/// out, F32 or with bf16 BF16 matrices), then ink_gate() of them, in one launch. Each product is one fmaf after the
/// other in the rank's order: the bits of lin_pair()'s hipblasSgemm on gfx1201 (inkling2_test: ctrl_up_gate). False,
/// with nothing launched, for a rank other than 64. out may be cur.
bool ink_ctrl_up_gate(const float* cur, const float* msg, const float* ch, const void* gain_w, const void* forget_w,
float* out, int n_embd, int rank, int rows, void* stream, bool bf16 = false);
/// out = cur - tanh(forget) * cur + tanh(gain) * msg, n values. out may be cur.
void ink_gate(const float* cur, const float* msg, const float* gain, const float* forget, float* out, size_t n,
void* stream);
Expand Down
62 changes: 62 additions & 0 deletions engine/src/poly/inkling2_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,67 @@ void case_attend_batch() {

// ---- the list ----


// A prompt's controller after its down product: ink_ctrl_up_gate() against lin_pair()'s launches for it (gemm_f32()'s
// two products, after bf16_to_f32() for BF16 matrices, then ink_gate()), byte for byte, at row counts around the
// prompt calls' and in place (out = cur, as the FFN's controller writes it); and against the reference. The bits are
// hipblasSgemm's where gemm_f32() calls it (HIP): CUDA's cuBLAS sums in another order, so there it is checked against
// the reference only.
void case_ctrl_up_gate() {
void* blas = blas_create(stream());
for (const bool bf : {false, true})
for (const auto [rows, embd] : {std::pair{32, kEmbd}, {33, kEmbd}, {100, kEmbd}, {777, kEmbd}, {1024, kEmbd},
{2048, kEmbd}, {3001, kEmbd}, {100, kEmbd + 32}}) {
const auto cur = randv((size_t) rows * embd, 1.0f), msg = randv((size_t) rows * embd, 0.5f),
chv = randv((size_t) rows * kRank, 1.0f);
auto gain = randv((size_t) embd * kRank, 0.1f), forget = randv((size_t) embd * kRank, 0.1f);
std::vector<uint16_t> gb, fb;
if (bf) bf16_of(std::vector<float>(gain), gb, gain), bf16_of(std::vector<float>(forget), fb, forget);
Dev<float> dcur(cur), dmsg(msg), dch(chv), dg(gain), df(forget), gv((size_t) rows * embd),
fv((size_t) rows * embd), out((size_t) rows * embd), out2((size_t) rows * embd), wide(gain.size());
Dev<uint16_t> dgb(bf ? gb : std::vector<uint16_t>(1)), dfb(bf ? fb : std::vector<uint16_t>(1));
if (bf) {
bf16_to_f32(dgb, wide, gain.size(), stream());
gemm_f32(blas, wide, dch, gv, kRank, embd, embd, rows);
bf16_to_f32(dfb, wide, forget.size(), stream());
gemm_f32(blas, wide, dch, fv, kRank, embd, embd, rows);
} else {
gemm_f32(blas, dg, dch, gv, kRank, embd, embd, rows);
gemm_f32(blas, df, dch, fv, kRank, embd, embd, rows);
}
ink_gate(dcur, dmsg, gv, fv, out2, (size_t) rows * embd, stream());
const void* gw = bf ? (const void*) dgb.p : (const void*) dg.p;
const void* fw = bf ? (const void*) dfb.p : (const void*) df.p;
if (!ink_ctrl_up_gate(dcur, dmsg, dch, gw, fw, out, embd, kRank, rows, stream(), bf)) {
++failures;
std::printf("ink_ctrl_up_gate: refused Inkling 2 Mini's shape FAIL\n");
continue;
}
const auto got = out.get();
const std::string what = fmt("ink_ctrl_up_gate %s, %d rows of %d", bf ? "BF16" : "F32", rows, embd);
#if defined(__HIPCC__)
same_bits(what + " = gemm_f32() x2 + ink_gate()", got, out2.get());
#endif
// in place: the FFN's controller writes its cur
ink_ctrl_up_gate(dcur, dmsg, dch, gw, fw, dcur, embd, kRank, rows, stream(), bf);
same_bits(what + ", out = cur", dcur.get(), got);
if (rows > 100) continue;
std::vector<double> ref((size_t) rows * embd);
for (int t = 0; t < rows; ++t)
for (int i = 0; i < embd; ++i) {
double g = 0, f = 0;
for (int k = 0; k < kRank; ++k) {
g += (double) gain[(size_t) i * kRank + k] * chv[(size_t) t * kRank + k];
f += (double) forget[(size_t) i * kRank + k] * chv[(size_t) t * kRank + k];
}
const size_t at = (size_t) t * embd + i;
ref[at] = h_gate(cur[at], msg[at], g, f);
}
compare(what + " against the reference", got, ref, 2e-5);
}
blas_destroy(blas);
}

struct Case {
const char* name;
void (*run)();
Expand All @@ -1439,6 +1500,7 @@ const Case kCases[] = {
{"mix", case_mix},
{"norms", case_norms},
{"controllers", case_controllers},
{"ctrl_up_gate", case_ctrl_up_gate},
{"situ", case_situ},
{"router", case_router},
{"small", case_small},
Expand Down
16 changes: 16 additions & 0 deletions engine/src/poly/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3529,6 +3529,22 @@ void gemm_f32(void* blas, const float* w, const float* x, float* y, int n_in, in
if (groups == 1 && n_out <= 8 * SMALL_OUT) {
cudaStream_t stream = nullptr;
blas_ck(cublasGetStream(h, &stream), "cublasGetStream");
#if defined(STRATA_USE_HIP)
// RDNA: the tiles by the size of the product. The shapes above left an RX 9070 XT's 64 multiprocessors few
// blocks: 1,024 tokens of Inkling 2 Mini's 4,608 values into its controllers' 64 outputs took 243 us with
// <16, 64, 2, 4>, 156 with <32, 32, 2, 2>; its router (2,304 into 128) 146 us with <32, 64, 4, 4>, 120 with
// <64, 64, 4, 4>. Every tile sums an output in the same order: the same bits.
if (n_out > 2 * SMALL_OUT || small_waves(rows, n_out) > 2) {
const long long outs = (long long) rows * n_out;
if (outs <= 65536)
gemm_tile<32, 32, 2, 2>(w, x, y, n_in, n_out, rows, stream);
else if (outs <= 262144 || n_out <= 64)
gemm_tile<64, 64, 4, 4>(w, x, y, n_in, n_out, rows, stream);
else
gemm_tile<64, 128, 4, 4>(w, x, y, n_in, n_out, rows, stream);
return;
}
#endif
if (n_out > 2 * SMALL_OUT)
gemm_tile<32, 64, 4, 4>(w, x, y, n_in, n_out, rows, stream);
else if (small_waves(rows, n_out) > 2)
Expand Down
Loading