Archived
Use F32 draft logits for the DSpark Markov walk
Score the walk on F32 head logits, reject non-I32 anchors in libref, and assert the real proposal path. Near-tie policy vs the reference is still open.
This commit is contained in:
1 parent
445bf8e0d9
commit
9ca20a1fba
14 files changed
+284
-56
No files matched your search
@@ -1,6 +1,6 @@
|
||||
// Full-vocabulary DSpark Markov greedy walk. Unlike dflash_select's candidate-lattice walk, this
|
||||
// operation applies the learned bigram bias to every BF16 draft-head logit and greedily selects
|
||||
// the largest biased score. The pinned llama.cpp graph uses F32 base logits and may differ.
|
||||
// operation applies the learned bigram bias to every draft-head logit and greedily selects
|
||||
// the largest biased score. Separate entry points accept BF16 or F32 base logits.
|
||||
#include <hip/hip_runtime.h>
|
||||
#include <climits>
|
||||
#include "r4d_gdn_args.h"
|
||||
@@ -26,8 +26,14 @@ bool rows_fit(long long rows, long long pitch, long long width, int element_byte
|
||||
// One CTA scans 64 vocabulary rows, eight at a time (one warp per row). The rank reduction is
|
||||
// parallel across the warp and codebook loads remain contiguous. Each CTA writes its exact local
|
||||
// argmax; a second kernel reduces those tile winners before the next dependent position starts.
|
||||
template<typename Logit>
|
||||
__device__ float load_logit(Logit x);
|
||||
template<> __device__ float load_logit<r4d_u16>(r4d_u16 x) { return r4d_bf2f(x); }
|
||||
template<> __device__ float load_logit<float>(float x) { return x; }
|
||||
|
||||
template<typename Logit>
|
||||
__global__ __launch_bounds__(kThreads) void markov_score_tiles(
|
||||
const r4d_u16* __restrict__ logits, long logits_pitch,
|
||||
const Logit* __restrict__ logits, long logits_pitch,
|
||||
const int* __restrict__ anchor, long anchor_stride,
|
||||
const r4d_u16* __restrict__ pred, const r4d_u16* __restrict__ succ,
|
||||
float* __restrict__ tile_scores, long scores_pitch,
|
||||
@@ -62,8 +68,8 @@ __global__ __launch_bounds__(kThreads) void markov_score_tiles(
|
||||
dot += s_pred[r] * r4d_bf2f(succ[(long)token * rank + r]);
|
||||
dot = r4d_wave_sum(dot);
|
||||
if (lane == 0) {
|
||||
const float score = r4d_bf2f(logits[(long)seq * steps * logits_pitch +
|
||||
(long)step * logits_pitch + token]) + dot;
|
||||
const float score = load_logit(logits[(long)seq * steps * logits_pitch +
|
||||
(long)step * logits_pitch + token]) + dot;
|
||||
if (score > warp_best || (score == warp_best && token < warp_token)) {
|
||||
warp_best = score;
|
||||
warp_token = token;
|
||||
@@ -129,13 +135,14 @@ __global__ __launch_bounds__(kThreads) void markov_reduce_tiles(
|
||||
if (tid == 0) out[(long)seq * out_pitch + step] = s_token[0];
|
||||
}
|
||||
|
||||
int markov_emit(const r4d_u16* logits, long logits_pitch, const int* anchor, long anchor_stride,
|
||||
template<typename Logit>
|
||||
int markov_emit(const Logit* logits, long logits_pitch, const int* anchor, long anchor_stride,
|
||||
const r4d_u16* pred, const r4d_u16* succ, float* tile_scores, long scores_pitch,
|
||||
int* tile_tokens, long tokens_pitch, int* out, long out_pitch,
|
||||
int sequences, int steps, int rank, int vocab,
|
||||
int n_tiles, RadStream stream) {
|
||||
for (int step = 0; step < steps; ++step) {
|
||||
hipLaunchKernelGGL(markov_score_tiles, dim3(n_tiles, sequences), dim3(kThreads), 0,
|
||||
hipLaunchKernelGGL((markov_score_tiles<Logit>), dim3(n_tiles, sequences), dim3(kThreads), 0,
|
||||
(hipStream_t)stream, logits, logits_pitch, anchor, anchor_stride, pred,
|
||||
succ, tile_scores, scores_pitch, tile_tokens, tokens_pitch, out, out_pitch,
|
||||
step, steps, rank, vocab, n_tiles);
|
||||
@@ -148,7 +155,8 @@ int markov_emit(const r4d_u16* logits, long logits_pitch, const int* anchor, lon
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" int r4d_rad_dflash_markov_greedy(const RadArgs* a, RadStream stream) {
|
||||
template<typename Logit>
|
||||
static int markov_launch(const RadArgs* a, RadStream stream, uint32_t logits_dtype) {
|
||||
constexpr int LOGITS = 0, ANCHOR = 1, PRED = 2, SUCC = 3;
|
||||
constexpr int TILE_SCORE = 4, TILE_TOKEN = 5, OUT = 6;
|
||||
const RadTensor* logits = r4d_opt(a, LOGITS);
|
||||
@@ -177,9 +185,9 @@ extern "C" int r4d_rad_dflash_markov_greedy(const RadArgs* a, RadStream stream)
|
||||
// blockIdx.y is limited to 65535, and all kernel loop indices are int.
|
||||
if (sequences <= 0 || sequences > 65535 || sequences > LLONG_MAX / steps ||
|
||||
(sequences - 1) > (LLONG_MAX - 1) / anchor_stride ||
|
||||
!rows_fit(sequences, out_pitch, steps, sizeof(int)) || logits->dtype != RAD_BF16 ||
|
||||
!rows_fit(sequences, out_pitch, steps, sizeof(int)) || logits->dtype != logits_dtype ||
|
||||
logits->rank != 2 || logits->shape[0] < sequences * steps || logits->shape[1] < vocab ||
|
||||
!rows_fit(sequences * steps, logits_pitch, vocab, sizeof(r4d_u16)))
|
||||
!rows_fit(sequences * steps, logits_pitch, vocab, sizeof(Logit)))
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
if (anchor->dtype != RAD_I32 || !r4d_contig(anchor) ||
|
||||
@@ -197,7 +205,7 @@ extern "C" int r4d_rad_dflash_markov_greedy(const RadArgs* a, RadStream stream)
|
||||
!rows_fit(sequences, r4d_row_pitch(tokens), n_tiles, sizeof(int)))
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
return R4D_RAW(markov_emit((const r4d_u16*)logits->data, (long)logits_pitch,
|
||||
return R4D_RAW(markov_emit((const Logit*)logits->data, (long)logits_pitch,
|
||||
(const int*)anchor->data, (long)anchor_stride,
|
||||
(const r4d_u16*)pred->data, (const r4d_u16*)succ->data,
|
||||
(float*)scores->data, (long)r4d_row_pitch(scores),
|
||||
@@ -205,3 +213,11 @@ extern "C" int r4d_rad_dflash_markov_greedy(const RadArgs* a, RadStream stream)
|
||||
(long)out_pitch, (int)sequences, (int)steps, (int)rank,
|
||||
(int)vocab, (int)n_tiles, stream));
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_dflash_markov_greedy(const RadArgs* a, RadStream stream) {
|
||||
return markov_launch<r4d_u16>(a, stream, RAD_BF16);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_dflash_markov_greedy_f32(const RadArgs* a, RadStream stream) {
|
||||
return markov_launch<float>(a, stream, RAD_F32);
|
||||
}
|
||||
@@ -198,6 +198,7 @@ R4D_ATTN_ANCHOR_DECL(512, 1) R4D_ATTN_ANCHOR_DECL(512, 2) R4D_ATTN_ANCHOR_DECL(5
|
||||
R4D_ATTN_ANCHOR_DECL(512, 8)
|
||||
#undef R4D_ATTN_ANCHOR_DECL
|
||||
int r4d_rad_dflash_markov_greedy(const RadArgs*, RadStream);
|
||||
int r4d_rad_dflash_markov_greedy_f32(const RadArgs*, RadStream);
|
||||
/* attn_paged_qrow_tree (docs/OPS.md): the h256/gqa2 split-KV decode and the windowed kernels with a
|
||||
* tree verify's mask from operand 8, `spec_parent`. */
|
||||
int r4d_rad_attn_qrow_tree_decode_h256_gqa2_fp8 (const RadArgs*, RadStream);
|
||||
@@ -1012,6 +1013,7 @@ int r4d_shape_qsa_work (const RadParam*, int, int operand, RadOpdDesc*
|
||||
int r4d_shape_dflash_conv (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_dflash_select (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_dflash_markov (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_dflash_markov_f32(const RadParam*, int, int operand, RadOpdDesc*);
|
||||
|
||||
/* -- declared fusions (r4d_fuse.cpp, pure host) --
|
||||
*
|
||||
|
||||
@@ -3399,6 +3399,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
"DSpark's exact greedy Markov walk: add the full-vocabulary bilinear bias to each "
|
||||
"draft-head logit, choose the greatest token (ties to the lower id), then use it as the "
|
||||
"next position's predecessor."),
|
||||
SCHEMA("dflash_markov_greedy_f32", pDflashMarkov, oDflashMarkov,
|
||||
"DSpark full-vocabulary greedy walk with F32 base logits and BF16 codebooks."),
|
||||
SCHEMA("dflash_tree", pDflashTree, oDflashTree,
|
||||
"dflash_select's lattice and edge scores grown best-first into a tree of `nodes` nodes: "
|
||||
"a taken node's children are the valid candidates at the next position, each with log "
|
||||
@@ -9606,6 +9608,15 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
|
||||
nullptr, nullptr, r4d_rad_dflash_markov_greedy, nullptr, nullptr, nullptr,
|
||||
r4d_shape_dflash_markov },
|
||||
|
||||
{ "dflash_markov_greedy_f32", "dflash_markov_greedy_f32", "dflash",
|
||||
"Full-vocabulary DSpark greedy walk with F32 base logits",
|
||||
"steps >= 1, rank a multiple of 32 and <= 512, 64 vocabulary ids per scratch tile",
|
||||
"f32 logits, bf16 codebooks, i32 anchor, f32/i32 tile winners, i32 tokens out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cDflashMarkov), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_dflash_markov_greedy_f32, nullptr, nullptr, nullptr,
|
||||
r4d_shape_dflash_markov_f32 },
|
||||
|
||||
{ "dflash_select_bf16", "dflash_select", "dflash",
|
||||
"DFlash2 candidate selector: the low-rank edge scores and the greedy walk over them, one "
|
||||
"workgroup a sequence and no launch between steps",
|
||||
|
||||
@@ -2225,3 +2225,10 @@ extern "C" int r4d_shape_dflash_markov(const RadParam* p, int n_p, int operand,
|
||||
};
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_dflash_markov_f32(const RadParam* p, int n_p, int operand,
|
||||
RadOpdDesc* out) {
|
||||
const int rc = r4d_shape_dflash_markov(p, n_p, operand, out);
|
||||
if (rc == RAD_OK && operand == 0) out->dtype = RAD_F32;
|
||||
return rc;
|
||||
}
|
||||
Reference in new issue
Block a user