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
16 changes: 16 additions & 0 deletions engine/include/strata/poly/kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ struct ExpertBell {
/// this one, or 0 when there is none). One block. CUDA only.
void bell_ring(const void* const* slots, const int32_t* ids, const float* w, int n_used, int skip_from, int layer,
const float* x, float* hx, int n, ExpertBell* bell, uint32_t* seq, void* stream);
/// bell_ring()'s arguments besides the picks, for route_ring(); uses: count_picks()'s counts (null: none).
struct BellRing {
const void* const* slots;
int32_t* uses;
int skip_from, layer;
const float* x;
float* hx;
int n;
ExpertBell* bell;
uint32_t* seq;
};
/// route_rows() of one token into ids and w, count_picks() of its picks into r.uses and bell_ring() of them with
/// r's arguments, in one launch: the same bits as the three. CUDA only.
void route_ring(const float* logits, int n_expert, int n_used, int gate, float in_scale, const float* bias,
const float* escale, bool norm, float w_scale, int32_t* ids, float* w, const BellRing& r,
void* stream);
/// Where seq[1] is a ring: wait until the bell's ack is that ring (or abort is set), then acc += hy (n floats).
void bell_add(const ExpertBell* bell, const uint32_t* seq, const float* hy, float* acc, int n, void* stream);
/// copy2() with a third run. CUDA and HIP only.
Expand Down
31 changes: 21 additions & 10 deletions engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5582,20 +5582,31 @@ bool Engine::card_moe(int n) {
// on the host, or its formats have no kernel for picks on the card.
bool Engine::moe_card(Layer& L, int il, int n) {
if (L.card == 0) return false;
if (!kernels::native_mmvq_ids_supported((int) L.gu_type) || !kernels::native_mmvq_ids_supported((int) L.u_type) ||
!kernels::native_mmvq_ids_supported((int) L.d_type) || !route_card(il, n, d_ids_, d_wts_)) {
const int rr = n * n_used_;
const void* const* table = d_slots_ + (size_t) il * n_expert_;
[[maybe_unused]] const bool rings = bell_ && n == 1;
[[maybe_unused]] bool rung = false; // route_card_ring() has counted the picks and rung the bell
const bool kernels_for_picks = kernels::native_mmvq_ids_supported((int) L.gu_type) &&
kernels::native_mmvq_ids_supported((int) L.u_type) &&
kernels::native_mmvq_ids_supported((int) L.d_type);
#if !defined(STRATA_GPU_METAL)
// the doorbell: the misses to the host, a token's place under --tail-skip's
const BellRing ring{table, rings && !block_pass_ ? d_use_ + (size_t) il * n_expert_ : nullptr,
opt_.tail_skip > 0 ? opt_.tail_skip : n_used_, il, xn_, h_x_, n_embd_, h_bell_, d_bell_seq_};
rung = kernels_for_picks && rings && route_card_ring(il, ring);
#endif
if (!kernels_for_picks || (!rung && !route_card(il, n, d_ids_, d_wts_))) {
L.card = 0;
return false;
}
L.card = 1;
const int rr = n * n_used_;
const void* const* table = d_slots_ + (size_t) il * n_expert_;
[[maybe_unused]] const bool rings = bell_ && n == 1;
#if !defined(STRATA_GPU_METAL)
if (rings) { // the doorbell: the misses to the host, a token's place under --tail-skip's
if (!block_pass_) count_picks(d_ids_, rr, n_expert_, d_use_ + (size_t) il * n_expert_, st_);
bell_ring(table, d_ids_, d_wts_, n_used_, opt_.tail_skip > 0 ? opt_.tail_skip : n_used_, il, xn_, h_x_, n_embd_,
h_bell_, d_bell_seq_, st_);
if (rings) {
if (!rung) {
if (ring.uses) count_picks(d_ids_, rr, n_expert_, ring.uses, st_);
bell_ring(table, d_ids_, d_wts_, n_used_, ring.skip_from, il, xn_, h_x_, n_embd_, h_bell_, d_bell_seq_,
st_);
}
table = d_zslots_ + (size_t) il * n_expert_;
} else if (!all_cached()) { // card_far_: the experts it lacks, from the mirror into the staging buffer first
far_plan(table, d_far_ + (size_t) il * n_expert_, d_ids_, rr, n_expert_, stage_, ahead_stride_, d_tab_,
Expand Down Expand Up @@ -5628,7 +5639,7 @@ bool Engine::moe_card(Layer& L, int il, int n) {
if (moe_sum_later(n)) moe_left_ = true;
else moe_sum(d_, d_wts_, fo_, n_embd_, n_used_, n, st_);
#if !defined(STRATA_GPU_METAL)
if (rings) bell_add(h_bell_, d_bell_seq_, h_cpu_, fo_, n_embd_, st_); // (bells(): no sum later then)
if (rings && !moe_left_) bell_add(h_bell_, d_bell_seq_, h_cpu_, fo_, n_embd_, st_); // (later: the family's)
#endif
return true;
}
Expand Down
11 changes: 10 additions & 1 deletion engine/src/poly/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,23 @@ class Engine {
(void) il, (void) n, (void) ids, (void) w;
return false;
}
#if !defined(STRATA_GPU_METAL)
/// route_card() of one token into d_ids_ and d_wts_ with the doorbell's count_picks() and bell_ring() of its
/// picks (route_ring()), in one launch. False, with nothing launched: the three launches instead.
virtual bool route_card_ring(int il, const BellRing& ring) {
(void) il, (void) ring;
return false;
}
#endif
/// The shared expert, into fo_. It runs on the GPU while the host routes.
virtual void moe_shared(const Layer& L, int il, int n) = 0;
/// False: run() launches moe_shared() as it is where it replays a layer's other launches (Layer::shared stays
/// empty), for a family whose shared expert is a launch or two that a graph launch costs the host more than.
virtual bool shared_recorded() const { return true; }
/// True: moe_card() leaves the routed experts' sum (d_ weighted by d_wts_, onto fo_) of a call of n tokens to the
/// family, which folds it into what follows (Inkling 2's layer_tail()), and sets moe_left_. The weights are then
/// still to be multiplied by moe_left_scale_[id] where that is set (expert_weights()'s product).
/// still to be multiplied by moe_left_scale_[id] where that is set (expert_weights()'s product), and with the
/// doorbell (bell_ and one token) the host's sum is then the family's to add too, as bell_add() adds it.
virtual bool moe_sum_later(int n) const {
(void) n;
return false;
Expand Down
23 changes: 21 additions & 2 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,14 @@ void Inkling2::layer_tail(Layer&, int il, int n) {
void Inkling2::tail_rows(int il, int n) {
const Slot& S = slots_[(size_t) slot_of(il, pass_of(il))];
if (n == 1 && std::exchange(moe_left_, false)) { // moe_card() left the experts' sum here
#if !defined(STRATA_GPU_METAL)
if (bell_) { // and the doorbell's
ink_moe_bell_conv_add(d_, d_wts_, moe_left_scale_, d_ids_, n_used_, fo_, h_bell_, d_bell_seq_, h_cpu_,
past_f(run_slot(il, 0)), fp(blocks_[(size_t) il].f_conv), fc_, r_, 1.f, n_embd_,
taps_, st_);
return;
}
#endif
ink_moe_conv_add(d_, d_wts_, moe_left_scale_, d_ids_, n_used_, fo_, past_f(run_slot(il, 0)),
fp(blocks_[(size_t) il].f_conv), fc_, r_, 1.f, n_embd_, taps_, st_);
return;
Expand Down Expand Up @@ -1710,14 +1718,25 @@ void Inkling2::route_by(const float* logits, const float* bias, int* ids, float*
}
}

bool Inkling2::route_card(int il, int n, int32_t* ids, float* w) {
const float* Inkling2::card_bias(int il, int n) const {
const Block& B = blocks_[(size_t) il];
const float* bias = B.probs_b_dev.p ? fp(B.probs_b_dev) : nullptr;
// a few tokens (moe_card()) with --cache-bias pick by slots_moved()'s bias; a prompt batch as before
if (cache_b_ && n < kStageRows) bias = cache_b_ + (size_t) il * n_expert_;
route_rows(rl_, n_expert_, n_used_, n, 1, 1.f, bias, nullptr, true, w_scale_, ids, w, st_);
return bias;
}

bool Inkling2::route_card(int il, int n, int32_t* ids, float* w) {
route_rows(rl_, n_expert_, n_used_, n, 1, 1.f, card_bias(il, n), nullptr, true, w_scale_, ids, w, st_);
return true;
}

#if !defined(STRATA_GPU_METAL)
bool Inkling2::route_card_ring(int il, const BellRing& ring) {
route_ring(rl_, n_expert_, n_used_, 1, 1.f, card_bias(il, 1), nullptr, true, w_scale_, d_ids_, d_wts_, ring, st_);
return true;
}
#endif

// No shared expert: the routed experts' sum starts from nothing (where layer_tail() sums them, it starts there).
void Inkling2::moe_shared(const Layer&, int, int n) {
Expand Down
42 changes: 36 additions & 6 deletions engine/src/poly/inkling2.cu
Original file line number Diff line number Diff line change
Expand Up @@ -901,20 +901,39 @@ __global__ void ink_conv_add_rows_kernel(const float* x, InkStateRows state, con
norm, c);
}

// BELL: ink_moe_bell_conv_add()'s, bell_add_kernel's wait and sum between the experts' sum and the convolution
template <bool BELL>
__global__ void ink_moe_conv_add_kernel(const float* x, const float* w, const float* __restrict__ scale,
const int* ids, int n_used,
float* fo, float* state, const float* __restrict__ cw, float* __restrict__ out,
float* acc, float a, int channels, int taps, float norm) {
float* acc, float a, int channels, int taps, float norm,
const ExpertBell* bell, const uint32_t* seq, const float* hy) {
pdl_trigger(); // PDL (pdl.hpp): the launch after this one may start
pdl_wait();
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= channels) return;
if (!BELL && i >= channels) return;
float s = 0.0f;
for (int k = 0; k < n_used; ++k) {
for (int k = 0; k < n_used && i < channels; ++k) {
const float wk = scale ? w[k] * scale[ids[k]] : w[k]; // expert_weights_kernel's product
s += wk * x[size_t(k) * channels + i];
}
fo[i] = 0.0f + s; // moe_sum() onto the zeros fo was
float f = 0.0f + s; // moe_sum() onto the zeros fo was
if (BELL) {
const uint32_t r = seq[1];
if (r != 0) {
if (threadIdx.x == 0)
while (bell->ack != r && !bell->abort) {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700
__nanosleep(500);
#endif
}
__syncthreads();
__threadfence_system();
if (i < channels) f = f + *(volatile const float*) (hy + i);
}
if (i >= channels) return;
}
fo[i] = f;
const float y = conv_value(fo, state, cw, nullptr, channels, 1, taps, 0, i, norm);
out[i] = y;
conv_state_at(fo, state, channels, taps, 1, i);
Expand Down Expand Up @@ -3001,11 +3020,22 @@ void ink_moe_conv_add(const float* x, const float* w, const float* scale, const
void* stream) {
if (taps < 2 || taps > 8) throw std::invalid_argument("ink_moe_conv_add: 2 to 8 taps");
const float norm = 1.0f / std::sqrt((float) (taps + 1));
launch_pdl(ink_moe_conv_add_kernel, dim3(grid((size_t) channels, 256)), dim3(256), 0, cs(stream), x, w, scale, ids,
n_used, fo, state, cw, out, acc, a, channels, taps, norm);
launch_pdl(ink_moe_conv_add_kernel<false>, dim3(grid((size_t) channels, 256)), dim3(256), 0, cs(stream), x, w,
scale, ids, n_used, fo, state, cw, out, acc, a, channels, taps, norm, (const ExpertBell*) nullptr,
(const uint32_t*) nullptr, (const float*) nullptr);
launched("ink_moe_conv_add");
}

void ink_moe_bell_conv_add(const float* x, const float* w, const float* scale, const int* ids, int n_used, float* fo,
const ExpertBell* bell, const uint32_t* seq, const float* hy, float* state, const float* cw,
float* out, float* acc, float a, int channels, int taps, void* stream) {
if (taps < 2 || taps > 8) throw std::invalid_argument("ink_moe_bell_conv_add: 2 to 8 taps");
const float norm = 1.0f / std::sqrt((float) (taps + 1));
launch_pdl(ink_moe_conv_add_kernel<true>, dim3(grid((size_t) channels, 256)), dim3(256), 0, cs(stream), x, w,
scale, ids, n_used, fo, state, cw, out, acc, a, channels, taps, norm, bell, seq, hy);
launched("ink_moe_bell_conv_add");
}

#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
Expand Down
11 changes: 8 additions & 3 deletions engine/src/poly/inkling2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class Inkling2 final : public Engine {
float* tmp) override;
void slots_moved() override;
bool route_card(int il, int n, int32_t* ids, float* w) override;
#if !defined(STRATA_GPU_METAL)
bool route_card_ring(int il, const BellRing& ring) override;
#endif
void moe_shared(const Layer& L, int il, int n) override;

private:
Expand Down Expand Up @@ -140,9 +143,9 @@ class Inkling2 final : public Engine {
bool control_ffn(const float* msg, const Ctrl& c, const Block& B, int row = 0);
void guess_block(int32_t token) override;
bool own_blocks() const override { return ds_file_ != nullptr; }
// only where moe_card() and layer_tail() are recorded together (whole_rec_), and not with the doorbell, whose
// experts' sum comes in after the card's
bool moe_sum_later(int n) const override { return n == 1 && whole_rec_ && !bell_; }
// only where moe_card() and layer_tail() are recorded together (whole_rec_); with the doorbell, layer_tail()
// adds the host's sum too
bool moe_sum_later(int n) const override { return n == 1 && whole_rec_; }
bool bells() const override { return true; }
#if defined(STRATA_USE_HIP)
// the shared expert is one memset (moe_shared()), and a graph launch a layer for it cost an RX 9070 XT's host
Expand Down Expand Up @@ -186,6 +189,8 @@ class Inkling2 final : public Engine {
float w_scale_ = 0;
float* cache_b_ = nullptr; ///< --cache-bias: each layer's pick bias for the card (slots_moved())
void route_by(const float* logits, const float* bias, int* ids, float* w, float* probs) const;
/// route_card()'s bias for layer il: the router's own, or for a few tokens with --cache-bias slots_moved()'s.
const float* card_bias(int il, int n) const;
void cached_bias(const Layer& L, int il, float* b) const;
uint32_t attn_type_ = 0; ///< --attn-requant's type for the attention's matrices; 0: the file's
bool unsloth_ = false; ///< an "inkling2" file: block_count counts applications, the loop's names are Unsloth's
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 @@ -99,6 +99,12 @@ void ink_conv_add_rows(const float* x, const InkStateRows& state, const float* w
void ink_moe_conv_add(const float* x, const float* w, const float* scale, const int* ids, int n_used, float* fo,
float* state, const float* cw, float* out, float* acc, float a, int channels, int taps,
void* stream);
/// ink_moe_conv_add() with the doorbell's sum between the two: where seq[1] is a ring, the experts' sum waits for
/// the bell's answer and gets hy added (bell_add()'s), then the convolution. The same bits as moe_sum(), bell_add()
/// and ink_conv_add() in turn. CUDA only.
void ink_moe_bell_conv_add(const float* x, const float* w, const float* scale, const int* ids, int n_used, float* fo,
const ExpertBell* bell, const uint32_t* seq, const float* hy, float* state, const float* cw,
float* out, float* acc, float a, int channels, int taps, void* stream);

/// Each head's `dim` values, times scale (null: none; [dim] a head), brought down to an L2 norm of at most `thr`
/// (the norm has 1e-6 added), then times tau = 1 + alpha * log(max(1, (pos + row + 1) / floor)) when alpha > 0.
Expand Down
Loading
Loading