diff --git a/sycl/include/strata/core/verify.hpp b/sycl/include/strata/core/verify.hpp index 540323ac..8e371f56 100644 --- a/sycl/include/strata/core/verify.hpp +++ b/sycl/include/strata/core/verify.hpp @@ -281,6 +281,7 @@ private: std::map, uint64_t> bm_used_; ///< last use of each captured layout (LRU, only with a graph limit) uint64_t bm_tick_ = 0; size_t batch_graph_limit_ = 0; ///< 0: keep every captured batch graph (0.1.39); N: LRU-evict beyond N layouts + int prof_S_ = 0; ///< rows of the batch window being profiled (0: a plain window) int last_rows_[8] = {}; ///< the slots of the last batch window's rows std::vector bkey(const int* rows, int S, int hbase) const { // #871: the doorbell variant has its own graphs std::vector k = batch_key(rows, S, hbase); diff --git a/sycl/src/core/verify.cpp b/sycl/src/core/verify.cpp index 2811b5a0..c0f2f5e6 100644 --- a/sycl/src/core/verify.cpp +++ b/sycl/src/core/verify.cpp @@ -1068,6 +1068,24 @@ bool Verifier::record_window(int T, dpct::queue_ptr cs, std::string &err) { native_mmvq(wg->native_type, wg->native_data, xq_, z_ + (size_t) tb * ZV, (int) N, (int) ZV, n, cs); stamp(l, 5, grp); // the recurrence from the untouched state over tokens [0, te); outputs only for this group's + static const bool gdn_groups_on = [] { // STRATA_BATCH_GDN_GROUPS=1, all slots in one launch + const char* e = std::getenv("STRATA_BATCH_GDN_GROUPS"); + return e != nullptr && std::atoi(e) != 0; + }(); + if (batch_rec_ && gdn_groups_on && te - tb <= 8) { + GdnGroups gg; + for (int t = tb; t < te;) { + const int first = t; + while (t < te && brow_[t] == brow_[first]) ++t; + SessionState& sx = slot_ss(first); + gg.state[gg.n] = sx.gdn_state + (size_t) (gi - sx.gdn_ord0) * gdn_floats; + gg.first[gg.n] = first; + gg.ntok[gg.n] = t - first; + ++gg.n; + } + gdn_step_norm_groups(gg, hb, (int) C, gate, beta, z_, (const float*) wnm->data, EPS, y_, (int) HK, + (int) HV, false, cs); + } else if (batch_rec_) { // each slot's recurrence over its own proposed-token group for (int t = tb; t < te;) { const int first = t; @@ -1257,6 +1275,34 @@ bool Verifier::record_window(int T, dpct::queue_ptr cs, std::string &err) { } } stamp(l, 10, grp); + // STRATA_BATCH_ATTN_ROWS=1: the window's rows in ONE launch per step (scores with each row's own indexer + // keys, the top-k over all rows, attention with each row's own pools): the same per-row arithmetic. + // Not with KV streaming (qsa_kv_resolve has work per slot then). + static const bool attn_rows = [] { + const char* e = std::getenv("STRATA_BATCH_ATTN_ROWS"); + return e != nullptr && std::atoi(e) != 0; + }(); + bool rows_ok = batch_rec_ && attn_rows && te - tb <= kAttnRowsMax; + for (int t = tb; rows_ok && t < te; ++t) rows_ok = slot_ss(t).qsa_states[qi].kv_mode != 1; + if (rows_ok) { + const int n_rows = te - tb; + QsaScoreRows sr; + QsaAttnPoolsRows pr; + sr.n = pr.n = n_rows; + for (int t = tb; t < te; ++t) { + const QsaState& sx = slot_ss(t).qsa_states[qi]; + sr.pooled[t - tb] = sx.idx_pooled; + sr.dead[t - tb] = sx.idx_dead; + pr.p[t - tb] = qsa_attn_pools(sx); + } + qsa_block_scores_rows(sr, qidx_ + tb * IQ * ID, step_ + tb * kStepCount, n_rows, max_blocks_, s, + scores_ + (size_t) tb * max_blocks_, cs); + qsa_block_topk(scores_ + (size_t) tb * max_blocks_, step_ + tb * kStepCount, n_rows, max_blocks_, cap_, + s, sel_ + (size_t) tb * cap_, cs); + qsa_decode_attn_batch_rows(qcur_ + tb * NH * HD, pr, sel_ + (size_t) tb * cap_, step_ + tb * kStepCount, + cap_, s, attn_scratch_ + (size_t) tb * attn_scratch_floats_, + attn_ + tb * NH * HD, n_rows, cs); + } else if (batch_rec_) { // each row selects and attends over its own slot's K/V for (int t = tb; t < te; ++t) { const QsaState& sx = slot_ss(t).qsa_states[qi]; @@ -2010,6 +2056,17 @@ void Verifier::collect_profile() { cs_->memcpy(prof_h_.data(), prof_, prof_h_.size() * 8).wait(); } accumulate_profile(prof_h_.data()); + if (prof_S_ > 0) { // a batch window: its table (run() prints only its own windows) + static const long long every = [] { + const char* e = std::getenv("STRATA_VERIFY_PROFILE_EVERY"); + return e ? std::atoll(e) : 0LL; + }(); + if (every > 0 && prof_windows_ >= every) { + std::fprintf(stderr, "verify stages batch window of %d rows (ms/window over %lld, GPU clock):%s\n", prof_S_, + (long long) prof_windows_, profile_report().c_str()); + std::fflush(stderr); + } + } } void Verifier::accumulate_profile(const unsigned long long* stamps) { @@ -2049,6 +2106,7 @@ void Verifier::accumulate_profile(const unsigned long long* stamps) { bool Verifier::run(int T, const int32_t *tokens, int64_t pos0, PoolMultiFn pool, void *user, int32_t *out, std::string &err) try { using namespace strata::kernels; + prof_S_ = 0; const OnDevice on_device(device_); last_batch_ = false; if (T < 1 || T > max_t_) { err = "verify: window size out of range"; return false; } @@ -2968,6 +3026,13 @@ bool Verifier::capture_commit_batch(const int *rows, int S, int hbase, if (!is_qsa_layer(g, l)) { const WeightRef* wnm = need(v, "ssm_norm.weight", err); if (!wnm) { ok = false; break; } + static const bool gdn_groups_on = [] { // STRATA_BATCH_GDN_GROUPS=1: the one-row groups in one launch + const char* e = std::getenv("STRATA_BATCH_GDN_GROUPS"); + return e != nullptr && std::atoi(e) != 0; + }(); + bool one_row = S <= 8; + for (int t = 1; one_row && t < S; ++t) one_row = rows[t] != rows[t - 1]; + GdnGroups gg; for (int t = 0; t < S;) { const int first = t; while (t < S && rows[t] == rows[first]) ++t; @@ -2977,6 +3042,14 @@ bool Verifier::capture_commit_batch(const int *rows, int S, int hbase, float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size; gdn_conv_commit(conv, qkv_L_ + (size_t) gdn_index * MT * C + (size_t) first * C, (int) C, keep, cs_); + if (gdn_groups_on && one_row) { + gg.state[gg.n] = state; + gg.keep[gg.n] = keep; + gg.first[gg.n] = first; + gg.ntok[gg.n] = 1; + ++gg.n; + continue; + } gdn_step_norm_multi(state, h_L_ + (size_t) gdn_index * MT * C + (size_t) first * C, (int) C, gate_L_ + (size_t) gdn_index * MT * HV + (size_t) first * HV, beta_L_ + (size_t) gdn_index * MT * HV + (size_t) first * HV, @@ -2985,6 +3058,11 @@ bool Verifier::capture_commit_batch(const int *rows, int S, int hbase, (int) HV, t - first, keep, cs_, t - first > 1 ? t - first : 0); // a 1-row group keeps the 0.1.39 kernel } + if (gg.n > 0) + gdn_step_norm_groups(gg, h_L_ + (size_t) gdn_index * MT * C, (int) C, + gate_L_ + (size_t) gdn_index * MT * HV, beta_L_ + (size_t) gdn_index * MT * HV, + z_, (const float*) wnm->data, EPS, y_dummy_, (int) g.ssm_k_heads, (int) HV, true, + cs_); ++gdn_index; } else { const WeightRef* wikn = need(v, "indexer.k_norm.weight", err); @@ -3181,6 +3259,7 @@ bool Verifier::run_slot_rows(const int *rows, int S, const int32_t *tokens, using namespace strata::kernels; const OnDevice on_device(device_); const ModelGeometry& g = *g_; + prof_S_ = S; if (!stage_batch(rows, S, 0, tokens, pos, err)) return false; const dpct::err0 le = DPCT_CHECK_ERROR((cs_)->ext_oneapi_graph(*exec_bm_[bkey(rows, S, 0)])); diff --git a/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp b/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp index 18abd96a..debe6462 100644 --- a/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp +++ b/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp @@ -122,9 +122,11 @@ attn_chunk_kernel(const float *__restrict__ q, QsaAttnPools p, const int32_t *__restrict__ step, int n_kv_heads, int page_size, float scale, float *__restrict__ part_acc, float *__restrict__ part_m, float *__restrict__ part_l, - int n_chunks, int cap = 0, long long scratch_stride = 0) { + int n_chunks, int cap = 0, long long scratch_stride = 0, + QsaAttnPoolsRows prow = {}) { // batched form: query blockIdx.z, with its own q row, selection, step and scratch auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); + if (prow.n > 0) p = prow.p[item_ct1.get_group(0)]; // a batch window's query attends over its own slot's pools q += (size_t)item_ct1.get_group(0) * (size_t)(n_kv_heads * G) * HD; ids += (size_t)item_ct1.get_group(0) * (size_t)cap; step += (size_t)item_ct1.get_group(0) * kStepCount; @@ -863,6 +865,71 @@ void qsa_decode_attn_batch(const float* q, const QsaAttnPools& pools, const int3 const dpct::err0 e = 0; } +namespace { +template +void attn_rows_chunks(dpct::queue_ptr st, const dpct::dim3& grid, const float* q, const QsaAttnPools& pools, + const int32_t* ids, const int32_t* steps, int n_kv_heads, int page_size, float scale, + float* part_acc, float* part_m, float* part_l, int n_chunks, int cap, long long stride, + const QsaAttnPoolsRows& rows) { + auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; + dpct::has_capability_or_fail(st->get_device(), {sycl::aspect::fp16}); + const QsaAttnPools p0 = pools; + const QsaAttnPoolsRows rr = rows; + st->parallel_for>>( + sycl::nd_range<3>(grid * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, + [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { + attn_chunk_kernel(q, p0, ids, steps, n_kv_heads, page_size, scale, part_acc, part_m, part_l, + n_chunks, cap, stride, rr); + }); +} +} // namespace + +void qsa_decode_attn_batch_rows(const float* q, const QsaAttnPoolsRows& rows, const int32_t* ids, const int32_t* steps, + int64_t cap, const QsaShapes& s, float* scratch, float* attn, int64_t n_q, void* stream) { + if (n_q <= 0) return; + const QsaAttnPools& pools = rows.p[0]; + if (rows.n != n_q || n_q > kAttnRowsMax || s.head_dim != HD || s.n_head != (int64_t) G * s.n_head_kv || cap <= 0 || + !scratch || !ids || !steps) { + std::fprintf(stderr, "qsa_decode_attn_batch_rows: unsupported geometry or missing buffers\n"); + std::exit(1); + } + static const int rs_on = [] { const char* v = std::getenv("STRATA_ATTN_RS"); return v ? std::atoi(v) : 2; }(); + static const int lane_cell_env = [] { + const char* v = std::getenv("STRATA_ATTN_LANECELL"); + return v != nullptr && v[0] != '\0' ? std::atoi(v) : -1; + }(); + const long long stride = (long long) qsa_decode_attn_scratch_floats(cap, s); + if (rs_on != 2 || lane_cell_env == 1) { // a non-default per-row kernel is selected: the per-row calls + for (int64_t z = 0; z < n_q; ++z) + qsa_decode_attn_batch(q + (size_t) z * s.n_head * HD, rows.p[z], ids + (size_t) z * cap, + steps + (size_t) z * kStepCount, cap, s, scratch + (size_t) z * stride, + attn + (size_t) z * s.n_head * HD, 1, stream); + return; + } + const int kv_mode = pools.k_q4 != nullptr ? 2 : (pools.k_q != nullptr && pools.v_q4 != nullptr ? 3 + : (pools.k_q != nullptr ? 1 : 0)); + const int n_chunks = (int) ((cap + CHUNK - 1) / CHUNK); + float* part_acc = scratch; + float* part_m = scratch + (size_t) n_chunks * s.n_head * HD; + float* part_l = part_m + (size_t) n_chunks * s.n_head; + const float scale = 1.0f / sqrtf((float) HD); + const dpct::dim3 grid((unsigned)n_chunks, (unsigned)s.n_head_kv, (unsigned)n_q); + dpct::queue_ptr st = strata::q_of(stream); + if (kv_mode == 3) attn_rows_chunks<3>(st, grid, q, pools, ids, steps, (int)s.n_head_kv, (int)s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int)cap, stride, rows); + else if (kv_mode == 2) attn_rows_chunks<2>(st, grid, q, pools, ids, steps, (int)s.n_head_kv, (int)s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int)cap, stride, rows); + else if (kv_mode == 1) attn_rows_chunks<1>(st, grid, q, pools, ids, steps, (int)s.n_head_kv, (int)s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int)cap, stride, rows); + else attn_rows_chunks<0>(st, grid, q, pools, ids, steps, (int)s.n_head_kv, (int)s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int)cap, stride, rows); + { + auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; + st->parallel_for>( + sycl::nd_range<3>(sycl::range(1, (unsigned)n_q, (unsigned)s.n_head) * sycl::range(1, 1, HD), + sycl::range(1, 1, HD)), + exp_props, [=](sycl::nd_item<3> item_ct1) { + attn_merge_kernel(part_acc, part_m, part_l, n_chunks, attn, stride); + }); + } +} + uint64_t qsa_decode_attn_scratch_floats(int64_t cap, const QsaShapes& s) { const int64_t chunks = (cap + CHUNK - 1) / CHUNK; return (uint64_t) chunks * (uint64_t) s.n_head * (HD + 2) + 64; diff --git a/sycl/src/kernels/cuda/qsa_select.dp.cpp b/sycl/src/kernels/cuda/qsa_select.dp.cpp index adafc898..3bd1c099 100644 --- a/sycl/src/kernels/cuda/qsa_select.dp.cpp +++ b/sycl/src/kernels/cuda/qsa_select.dp.cpp @@ -1127,7 +1127,7 @@ register pressure. __dpct_inline__ void block_scores_multi_kernel( const float *__restrict__ pooled, const float *__restrict__ dead, const float *__restrict__ q_idx, const int32_t *__restrict__ steps, int nq, - int64_t max_blocks, float *__restrict__ out, bool early_exit) { + int64_t max_blocks, float *__restrict__ out, bool early_exit, QsaScoreRows srow = {}) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &qs = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[MQ * IDX_HEADS * IDX_DIM]>( @@ -1171,14 +1171,18 @@ auto &qs = *sycl::ext::oneapi::group_local_memory_for_overwrite< for (int64_t b = (int64_t)item_ct1.get_group(2) * SCORE_WARPS + (item_ct1.get_local_id(2) >> 5); b <= top && b < max_blocks; b += wstride) { - const sycl::float4 kp = *reinterpret_cast( + // srow.n > 0: each query reads its own sequence's keys; else one set of keys for every query + const bool own = srow.n > 0; + const sycl::float4 kp = own ? sycl::float4{} : *reinterpret_cast( pooled + b * IDX_DIM + lane * 4); - const sycl::float4 kd = + const sycl::float4 kd = own ? sycl::float4{} : *reinterpret_cast(dead + lane * 4); for (int qi = 0; qi < nq; ++qi) { const int64_t n_bid = s_nbid[qi]; if (b > n_bid) continue; - const sycl::float4 k4 = (b == n_bid) ? kd : kp; + const sycl::float4 k4 = !own ? ((b == n_bid) ? kd : kp) + : (b == n_bid) ? *reinterpret_cast(srow.dead[qi] + lane * 4) + : *reinterpret_cast(srow.pooled[qi] + b * IDX_DIM + lane * 4); const float* q = qs + qi * IDX_HEADS * IDX_DIM + lane * 4; float score = 0.0f; #pragma unroll @@ -1566,6 +1570,27 @@ void block_topk_cluster_kernel(const float *__restrict__ scores, #endif // !__HIPCC__ } // namespace +void qsa_block_scores_rows(const QsaScoreRows& rows, const float* q_idx, const int32_t* steps, int64_t nq, + int64_t max_blocks, const QsaShapes& s, float* scores, void* stream) { + if (nq <= 0) return; + if (s.idx_dim != IDX_DIM || s.idx_n_head != IDX_HEADS || s.idx_block != R || nq > MQ || rows.n != nq) { + std::fprintf(stderr, "qsa_block_scores_rows: unsupported geometry\n"); + std::exit(1); + } + static const bool early = [] { const char* v = std::getenv("STRATA_QSA_EARLY_EXIT"); return v == nullptr || std::atoi(v) != 0; }(); + const QsaScoreRows srow = rows; + auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; + strata::q_of(stream)->submit([&](sycl::handler &cgh) { + auto early_ct7 = early; + cgh.parallel_for>( + sycl::nd_range<3>(sycl::range(1, 1, 256) * sycl::range(1, 1, SCORE_WARPS * 32), + sycl::range(1, 1, SCORE_WARPS * 32)), + exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { + block_scores_multi_kernel(nullptr, nullptr, q_idx, steps, (int)nq, max_blocks, scores, early_ct7, srow); + }); + }); +} + void qsa_block_scores(const float* pooled, const float* dead, const float* q_idx, const int32_t* steps, int64_t nq, int64_t max_blocks, const QsaShapes& s, float* scores, void* stream, int64_t active_blocks) { if (nq <= 0) return; diff --git a/sycl/src/kernels/cuda/verify_kernels.dp.cpp b/sycl/src/kernels/cuda/verify_kernels.dp.cpp index 229b0f8b..4c397ebc 100644 --- a/sycl/src/kernels/cuda/verify_kernels.dp.cpp +++ b/sycl/src/kernels/cuda/verify_kernels.dp.cpp @@ -350,8 +350,19 @@ __dpct_inline__ void gdn_step_norm_multi_kernel( const float *__restrict__ z, const float *__restrict__ gamma, float eps, float *__restrict__ y, int h_k, int h_v, int T, const int32_t *__restrict__ n_keep, int t_out_begin, - GdnQ81 *__restrict__ xq) { + GdnQ81 *__restrict__ xq, GdnGroups grp = {}) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); + if (grp.n > 0) { // group get_group(1) - its slot's state and its rows of the window's buffers + const int gi = item_ct1.get_group(1), f = grp.first[gi]; + state = grp.state[gi]; + hbuf += (size_t)f * C; + gate += (size_t)f * h_v; + beta += (size_t)f * h_v; + z += (size_t)f * S * h_v; + y += (size_t)f * S * h_v; + T = grp.ntok[gi]; + n_keep = grp.keep[gi]; + } auto &sk = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &sq = @@ -1830,6 +1841,37 @@ void gdn_ab_multi(const float* x, const uint16_t* w_alpha, const uint16_t* w_bet check("gdn_ab_multi"); } +void gdn_step_norm_groups(const GdnGroups& groups, const float* h, int conv_channels, const float* gate, const float* beta, + const float* z, const float* gamma, float eps, float* y, int h_k, int h_v, bool commit, + void* stream) { + if (groups.n < 1 || groups.n > 8 || !h || !gate || !beta || !z || !gamma || !y || h_k <= 0 || h_v % h_k) { + std::fprintf(stderr, "gdn_step_norm_groups: invalid arguments\n"); + std::exit(1); + } + for (int i = 0; i < groups.n; ++i) + if (!groups.state[i] || groups.ntok[i] < 1 || groups.ntok[i] > kVerifyMaxT || (commit && !groups.keep[i])) { + std::fprintf(stderr, "gdn_step_norm_groups: invalid group %d\n", i); + std::exit(1); + } + const dpct::dim3 g1((unsigned)h_v, (unsigned)groups.n), b1(S, RG); + const GdnGroups grp = groups; + auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; + // the kernel gdn_step_norm_multi picks for these calls: a window's rows (all outputs, no keep) or a commit of + // one-row groups (keep, t_out_begin 0) + if (commit) + strata::q_of(stream)->parallel_for>>( + sycl::nd_range<3>(g1 * b1, b1), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { + gdn_step_norm_multi_kernel(nullptr, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, + h_v, 0, nullptr, 0, nullptr, grp); + }); + else + strata::q_of(stream)->parallel_for>>( + sycl::nd_range<3>(g1 * b1, b1), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { + gdn_step_norm_multi_kernel(nullptr, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, + h_v, 0, nullptr, 0, nullptr, grp); + }); +} + void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const float* gate, const float* beta, const float* z, const float* gamma, float eps, float* y, int h_k, int h_v, int n_tok, const int32_t* n_keep, void* stream, int t_out_begin, void* xq_out) {