Archived
radiance 1.3.0
- #47: llama-server's request fields, routes and sampler order, and /props (with baKon's model entry: max_model_len, root and capabilities on /v1/models, and /v1/models/:id) - new APIs: Anthropic's Messages API (/v1/messages), OpenAI's Responses API (/v1/responses), llama-server's /completion and /infill, logprobs (OpenAI's logprobs and top_logprobs, up to 20; llama-server's n_probs), OpenAI's legacy `functions`, and X-Api-Key beside the bearer key - #4: --model takes a Hugging Face repo id (owner/name), fetched before startup into ~/.cache/radiance/hf or --download-dir and checked for changes at every start (redxherring) - #46: --tp 3 --tp-wire wht6 serves -- a three-rank two-shot all-reduce with uneven shards and the tiered-int8 wire over it (baKon); scripts/cardclock.py names what holds each card's clock under load (baKon) - CPU-only serving works again; --kernels names the kernel libraries to load; a model the loaded libraries cannot fully serve refuses at startup instead of failing its first request; --vram-kv-mib and --vram-weights-mib are gone (the split is automatic) - --embedding-placement ram is the default, and `auto` is gone - #40: Swift 1.5 recipes; rad-convert reads which shard holds a tensor from the index - #53: the pinned host pool gets huge pages where shmem THP is off; #57: a crash on video with an odd frame width is fixed
This commit is contained in:
1 parent
6a2ee986d6
commit
89cee7ce41
107 files changed
+6849
-1091
No files matched your search
@@ -63,6 +63,9 @@ set(R4D_UNITS
|
||||
# Twelve, head_dim 256 only: Qwen3.8-Flash-Next's QSA layers are 24 over 2. Four lines that
|
||||
# set the ratio and include the unit above, which is the parameterised body.
|
||||
"r4d_attn_paged_h256_gqa12|"
|
||||
# Four, head_dim 256: Qwen3.5-0.8B is 8 over 2 at the 27B's head width. Again the body above
|
||||
# with the ratio set.
|
||||
"r4d_attn_paged_h256_gqa4|"
|
||||
# Six, head_dim 256 and the same 4-token page: the same QSA layers at four ranks, where each KV
|
||||
# head is replicated across a rank pair. Again four lines that set the ratio and include the body.
|
||||
"r4d_attn_paged_h256_gqa6_bs4|"
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ target architecture. They are reported by the `r4d_*_dims()` accessors in
|
||||
| `gated_had_quant_i8` | `silu(a)*b` or `sigmoid(a)*b`, then the same rotation and quantiser |
|
||||
| `gdn_gated_norm_had_quant_i8` | the gdn gated norm with the rotation and quantiser fused onto it |
|
||||
| `qk_norm_rope_gate` | q/k rms-norm, rope and the attention gate in one kernel |
|
||||
| `ar_oneshot_{4,8}rank_exact`, `ar_twoshot_{4,8}rank_exact`, `ar_twoshot_4rank_ti8` | the wider-world all-reduces (TP4/TP8), exact and int8-wire |
|
||||
| `ar_oneshot_{3,4,8}rank_exact`, `ar_twoshot_{3,4,8}rank_exact`, `ar_twoshot_{3,4}rank_ti8`, `ar_gather_3rank` | the wider-world collectives (TP3/TP4/TP8), exact and int8-wire |
|
||||
| `p2p_copy2d`, `p2p_copy2d_kernel`, `p2p_fence` | strided peer device-to-device copy, through the copy engines or a kernel |
|
||||
## Asking the library instead of remembering
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// R4D attention at FOUR queries per KV head, head_dim 256 -- Qwen3.5-0.8B and its siblings that
|
||||
// keep the 27B's head width with fewer heads (8 query heads over 2 KV heads).
|
||||
//
|
||||
// The geometry and the dispatch only, as r4d_attn_paged_h256_gqa12.hip is: r4d_attn_paged_gqa8.hip
|
||||
// is the parameterised body and its kernels are templates on `gqa`. What the ratio changes is the
|
||||
// two bounds, and both are in the ROW table rather than here:
|
||||
//
|
||||
// the decode verify width 64 / gqa = 16, against 8 at gqa 8 and 10 at gqa 6. A workgroup holds
|
||||
// 64 (query, head) rows and carries q_len * gqa of them.
|
||||
// a prefill block's width 384 / gqa = 96 query positions, against 48 at gqa 8.
|
||||
//
|
||||
// NO head_dim 128 LEG. gqa 4 at 128 is the DFlash2 drafter's windowed geometry, which
|
||||
// r4d_attn_paged_h128_gqa4.hip serves; R4D_ATTN_H128 leaves those entry points out of this unit.
|
||||
// The page is the body's default 16.
|
||||
#define R4D_ATTN_GQA 4
|
||||
#define R4D_ATTN_H128 0
|
||||
|
||||
#include "r4d_attn_paged_gqa8.hip"
|
||||
@@ -104,6 +104,13 @@ int r4d_rad_attn_prefill_h128_gqa8_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h128_gqa8_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h128_gqa8_bf16 (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h128_gqa8_scratch(const RadArgs*);
|
||||
/* Four queries a KV head, head_dim 256 only -- Qwen3.5-0.8B. Same body, different
|
||||
* instantiation (r4d_attn_paged_h256_gqa4.hip). */
|
||||
int r4d_rad_attn_prefill_h256_gqa4_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_prefill_h256_gqa4_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa4_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa4_bf16 (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h256_gqa4_scratch(const RadArgs*);
|
||||
/* Twelve queries a KV head, head_dim 256 only -- Qwen3.8-Flash-Next's QSA layers. Same body,
|
||||
* different instantiation; r4d_attn_paged_h256_gqa12.hip is four lines. */
|
||||
int r4d_rad_attn_prefill_h256_gqa12_fp8 (const RadArgs*, RadStream);
|
||||
@@ -452,6 +459,7 @@ int r4d_rad_sample_temp (const RadArgs*, RadStream);
|
||||
int r4d_rad_sample_mask (const RadArgs*, RadStream);
|
||||
int r4d_rad_sample_topk (const RadArgs*, RadStream);
|
||||
int r4d_rad_sample_argmax (const RadArgs*, RadStream);
|
||||
int r4d_rad_sample_logprobs (const RadArgs*, RadStream);
|
||||
int r4d_rad_sample_topp (const RadArgs*, RadStream);
|
||||
int r4d_rad_sample_minp (const RadArgs*, RadStream);
|
||||
int r4d_rad_sample_typical (const RadArgs*, RadStream);
|
||||
@@ -500,6 +508,7 @@ int r4d_shape_sample_temp (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_sample_mask (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_sample_topk (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_sample_argmax (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_sample_logprobs (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_sample_narrow (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_sample_pick (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_sample_chain (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
|
||||
@@ -1907,6 +1907,19 @@ static const RadOperandSpec oArgmax[] = {
|
||||
{ "params", RAD_OPD_IN, 0 },
|
||||
{ "token", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* `stats` is [M, 2 + 2k] f32: max, sum of exp(l - max), then k (id, logit) pairs. `k` is the
|
||||
* deployment's width, `vocab_off` sample_topk's. */
|
||||
static const RadParamSpec pLogprobs[] = {
|
||||
{ "M", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "n_vocab", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "k", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "vocab_off", RAD_P_INT, RAD_OPTIONAL },
|
||||
};
|
||||
static const RadOperandSpec oLogprobs[] = {
|
||||
{ "logits", RAD_OPD_IN, 0 },
|
||||
{ "params", RAD_OPD_IN, 0 },
|
||||
{ "stats", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadOperandSpec oNarrow[] = {
|
||||
{ "cand_idx", RAD_OPD_INOUT, 0 },
|
||||
{ "cand_val", RAD_OPD_INOUT, 0 },
|
||||
@@ -2068,6 +2081,9 @@ static const RadOpSchema kSchemas[] = {
|
||||
"the exact top-k candidate set, descending, ties to the lower token id. The width is "
|
||||
"the buffer's; a row's own top_k may be narrower and leaves holes."),
|
||||
SCHEMA("sample_argmax", pMVocab, oArgmax, "greedy, ties to the lower token id."),
|
||||
SCHEMA("sample_logprobs", pLogprobs, oLogprobs,
|
||||
"for a row with RAD_SP_LOGPROBS: the raw logits' max, sum of exp(l - max) and k largest "
|
||||
"(id, logit) pairs, descending, ties to the lower id. Other rows are not written."),
|
||||
SCHEMA("sample_topp", pMCand, oNarrow,
|
||||
"nucleus: keep the shortest prefix whose cumulative probability reaches top_p, always "
|
||||
"at least one. The candidates are assumed descending, as sample_topk leaves them."),
|
||||
@@ -2434,6 +2450,25 @@ static const RadConstraint cAttnDecodeH256G8Bf16[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 8), C_EQ("block_size", 16),
|
||||
C_GE("causal", 0), C_LE("q_len", 8), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
};
|
||||
/* FOUR QUERIES PER KV HEAD, head_dim 256 (r4d_attn_paged_h256_gqa4.hip) -- Qwen3.5-0.8B, 8 query
|
||||
* heads over 2. `q_len <= 16` is the same `q_len * gqa <= 64` bound, so every legal speculation
|
||||
* depth is a decode launch at this ratio. */
|
||||
static const RadConstraint cAttnPrefillH256G4Fp8[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 4), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
static const RadConstraint cAttnPrefillH256G4Bf16[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 4), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeH256G4Fp8[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 4), C_EQ("block_size", 16),
|
||||
C_GE("causal", 0), C_LE("q_len", 16), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeH256G4Bf16[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 4), C_EQ("block_size", 16),
|
||||
C_GE("causal", 0), C_LE("q_len", 16), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
};
|
||||
/* TWELVE QUERIES PER KV HEAD, head_dim 256 (r4d_attn_paged_h256_gqa12.hip) -- Qwen3.8-Flash-Next's
|
||||
* QSA layers, 24 query heads over 2. `q_len <= 5` is the same `q_len * gqa <= 64` bound the two
|
||||
* ratios above state, and it is TIGHTER than the legal speculation depths: a verify of depth 7 is
|
||||
@@ -3378,6 +3413,7 @@ static const RadConstraint cGridEmbed[] = { C_GE("n", 1), C_GE("side", 1), C_IN(
|
||||
static const RadConstraint cGemmBias[] = { C_GE("M", 1), C_DIV("K", 8), C_IN("dtype", "bf16") };
|
||||
static const RadConstraint cSampleVoc[] = { C_GE("n_vocab", 1) };
|
||||
static const RadConstraint cSampleCand[] = { C_GE("n_cand", 1), C_LE("n_cand", 4096) };
|
||||
static const RadConstraint cSampleLogprobs[] = { C_GE("n_vocab", 1), C_GE("k", 1), C_LE("k", 64) };
|
||||
/* The activation width the column loop vectorises over; M is unbounded -- the kernel chunks rows. */
|
||||
static const RadConstraint cLogitsGemm[] = { C_DIV("n_embd", 8), C_IN("dtype", "bf16") };
|
||||
/* The fp8 head. n_embd on the 128 block the scales are taken over, and NO CAP ON M -- the row
|
||||
@@ -3849,6 +3885,17 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_sample_argmax, nullptr, nullptr, nullptr,
|
||||
r4d_shape_sample_argmax },
|
||||
|
||||
{ "sample_logprobs_f32", "sample_logprobs", "sample",
|
||||
"the raw distribution's summary for a logprobs row: max, sum of exp(l - max), the k largest",
|
||||
"any vocabulary; k <= 64, the LDS the sort runs in. sample_topk's radix select at the op's k, so "
|
||||
"the set and its tie rule are the top-k's; the maximum is its first entry and the sum one more "
|
||||
"pass. A row without RAD_SP_LOGPROBS leaves before reading anything",
|
||||
"f32 logits, f32 stats [M, 2 + 2k] with the ids as floats",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cSampleLogprobs), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_sample_logprobs, nullptr, nullptr, nullptr,
|
||||
r4d_shape_sample_logprobs },
|
||||
|
||||
{ "sample_topp_f32", "sample_topp", "sample",
|
||||
"nucleus narrowing in place",
|
||||
"n_cand <= 4096; the prefix sum is SEQUENTIAL in f32 in candidate order, because a parallel "
|
||||
@@ -4012,6 +4059,44 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_prefill_h256_gqa4_fp8kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 4 queries per kv head, paged block 16, varlen",
|
||||
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
|
||||
ROW(cAttnPrefillH256G4Fp8), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_fp8, nullptr, nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_prefill_h256_gqa4_bf16kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 4 queries per kv head, paged block 16, varlen",
|
||||
"bf16 query, bf16 kv cache, bf16 out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
|
||||
ROW(cAttnPrefillH256G4Bf16), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_bf16, nullptr, nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_decode_h256_gqa4_fp8kv", "attn_paged", "attn",
|
||||
"paged attention, causal or not, split-KV (decode / speculative verify / draft block)",
|
||||
"head_dim 256, 4 queries per kv head, paged block 16, q_len*gqa <= 64, no window",
|
||||
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnDecodeH256G4Fp8), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_decode_h256_gqa4_fp8, r4d_rad_attn_decode_h256_gqa4_scratch,
|
||||
nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_decode_h256_gqa4_bf16kv", "attn_paged", "attn",
|
||||
"paged attention, causal or not, split-KV (decode / speculative verify / draft block)",
|
||||
"head_dim 256, 4 queries per kv head, paged block 16, q_len*gqa <= 64, no window",
|
||||
"bf16 query, bf16 kv cache, bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnDecodeH256G4Bf16), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_decode_h256_gqa4_bf16, r4d_rad_attn_decode_h256_gqa4_scratch,
|
||||
nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_prefill_h256_gqa12_fp8kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 12 queries per kv head, paged block 4, varlen",
|
||||
|
||||
@@ -98,7 +98,8 @@ enum {
|
||||
SP_PENALTY = 1 << 1,
|
||||
SP_MASK = 1 << 2,
|
||||
SP_XTC = 1 << 3,
|
||||
SP_DRY = 1 << 4
|
||||
SP_DRY = 1 << 4,
|
||||
SP_LOGPROBS = 1 << 5
|
||||
};
|
||||
|
||||
__device__ __forceinline__ uint64_t splitmix64(uint64_t z) {
|
||||
@@ -674,6 +675,115 @@ __global__ __launch_bounds__(kTopkThreads) void topk_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- sample_logprobs
|
||||
// ref_sample_logprobs. For a row with RAD_SP_LOGPROBS: [max, sum of exp(l - max), k (id, logit)
|
||||
// pairs], descending, ties to the lower id, ids global (+ vocab_off) and written as floats. A row
|
||||
// without the flag is not written, and its block leaves before reading anything.
|
||||
//
|
||||
// THE k LARGEST ARE topk_kernel's SELECT, at the op's fixed k rather than a row's top_k: the same
|
||||
// radix_select passes, the tie-break pass only when the threshold's tie group is bigger than the
|
||||
// set is owed, and the same packed (key, ~id) bitonic sort, so this set and a top-k candidate set
|
||||
// over the same row agree to the bit and the tie rule is ref's by construction. The maximum is
|
||||
// the first of them, so it costs nothing; the sum is one more pass, accurate expf, folded wave by
|
||||
// wave in a fixed order -- the same row gives the same bits every step.
|
||||
//
|
||||
// k IS BOUNDED BY kMaxLogprobs, the LDS the sort runs in. The engine declares 21 (OpenAI's bound on
|
||||
// top_logprobs, and one more so a row is whole 16-byte words); the shim refuses a wider op rather
|
||||
// than truncating it.
|
||||
constexpr uint32_t kMaxLogprobs = 64;
|
||||
|
||||
__global__ __launch_bounds__(kTopkThreads) void logprobs_kernel(
|
||||
const float* __restrict__ lg, const SP* __restrict__ sp, float* __restrict__ st,
|
||||
uint32_t n_vocab, uint32_t k, uint32_t lg_ld, uint32_t st_ld, int32_t vocab_off) {
|
||||
__shared__ unsigned long long cand[kMaxLogprobs];
|
||||
__shared__ uint32_t hist[258];
|
||||
__shared__ uint32_t scan[256], wt[256 / kWave];
|
||||
__shared__ uint32_t fill;
|
||||
__shared__ float red[kTopkWaves];
|
||||
|
||||
const uint32_t m = blockIdx.x;
|
||||
if (!(sp[m].flags & SP_LOGPROBS)) return;
|
||||
const float* __restrict__ row = lg + (size_t)m * lg_ld;
|
||||
float* __restrict__ out = st + (size_t)m * st_ld;
|
||||
const uint32_t kk = k < n_vocab ? k : n_vocab;
|
||||
|
||||
uint32_t n_eq = 0, dummy = 0, n_tie = 0;
|
||||
const uint32_t T = radix_select<true>(row, n_vocab, 0, kk, hist, scan, wt, &n_eq, &n_tie);
|
||||
const uint32_t idx_cut = (n_tie == n_eq)
|
||||
? 0xffffffffu
|
||||
: radix_select<false>(row, n_vocab, T, n_eq, hist, scan, wt,
|
||||
&dummy, &n_tie);
|
||||
if (threadIdx.x == 0) fill = 0;
|
||||
__syncthreads();
|
||||
auto collect = [&](float f, uint32_t i) {
|
||||
const uint32_t key = fkey(f);
|
||||
if (!(key > T || (key == T && i <= idx_cut))) return;
|
||||
const uint32_t slot = atomicAdd(&fill, 1u);
|
||||
if (slot < kMaxLogprobs) cand[slot] = ((unsigned long long)key << 32) | (unsigned long long)(~i);
|
||||
};
|
||||
const bool v4 = ((uintptr_t)row & 15u) == 0 && (n_vocab & 3u) == 0;
|
||||
if (v4) {
|
||||
const float4* __restrict__ r4 = (const float4*)row;
|
||||
for (uint32_t j = threadIdx.x; j < (n_vocab >> 2); j += kTopkThreads) {
|
||||
const float4 f = r4[j];
|
||||
collect(f.x, 4 * j);
|
||||
collect(f.y, 4 * j + 1);
|
||||
collect(f.z, 4 * j + 2);
|
||||
collect(f.w, 4 * j + 3);
|
||||
}
|
||||
} else {
|
||||
for (uint32_t i = threadIdx.x; i < n_vocab; i += kTopkThreads) collect(row[i], i);
|
||||
}
|
||||
__syncthreads();
|
||||
const uint32_t got = fill < kk ? fill : kk;
|
||||
uint32_t sn = 1;
|
||||
while (sn < got) sn <<= 1;
|
||||
for (uint32_t i = got + threadIdx.x; i < sn; i += kTopkThreads) cand[i] = 0;
|
||||
__syncthreads();
|
||||
for (uint32_t sz = 2; sz <= sn; sz <<= 1) {
|
||||
for (uint32_t stp = sz >> 1; stp > 0; stp >>= 1) {
|
||||
const uint32_t t = threadIdx.x;
|
||||
if (t < sn) {
|
||||
const uint32_t partner = t ^ stp;
|
||||
if (partner > t) {
|
||||
const bool up = (t & sz) == 0; // `up` here means DESCENDING
|
||||
const unsigned long long a = cand[t], b = cand[partner];
|
||||
if (up ? (a < b) : (a > b)) { cand[t] = b; cand[partner] = a; }
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
}
|
||||
|
||||
const float mx = got ? row[~(uint32_t)(cand[0] & 0xffffffffull)] : -INFINITY;
|
||||
float s = 0.0f;
|
||||
if (v4) {
|
||||
const float4* __restrict__ r4 = (const float4*)row;
|
||||
for (uint32_t j = threadIdx.x; j < (n_vocab >> 2); j += kTopkThreads) {
|
||||
const float4 f = r4[j];
|
||||
s += expf(f.x - mx) + expf(f.y - mx) + expf(f.z - mx) + expf(f.w - mx);
|
||||
}
|
||||
} else {
|
||||
for (uint32_t i = threadIdx.x; i < n_vocab; i += kTopkThreads) s += expf(row[i] - mx);
|
||||
}
|
||||
s = r4d_wave_sum(s);
|
||||
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
|
||||
if (lane == 0) red[wave] = s;
|
||||
__syncthreads();
|
||||
if (threadIdx.x == 0) {
|
||||
float sum = 0.0f;
|
||||
for (uint32_t w = 0; w < kTopkWaves; ++w) sum += red[w];
|
||||
out[0] = mx;
|
||||
out[1] = sum;
|
||||
}
|
||||
for (uint32_t r = threadIdx.x; r < k; r += kTopkThreads) {
|
||||
const bool live = r < got;
|
||||
const uint32_t loc = live ? ~(uint32_t)(cand[r] & 0xffffffffull) : 0u;
|
||||
out[2 + 2 * r] = live ? (float)((int32_t)loc + vocab_off) : -1.0f;
|
||||
out[3 + 2 * r] = live ? row[loc] : -INFINITY;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- the narrowing stages
|
||||
//
|
||||
// All four read a fixed-width candidate set in place: index -1 and value -infinity mark a hole, and
|
||||
@@ -1052,6 +1162,20 @@ static int r4d_sample_argmax_f32(long lg, long tok, int M, int n_vocab, int lg_l
|
||||
return hipGetLastError() == hipSuccess ? 0 : -4;
|
||||
}
|
||||
|
||||
static int r4d_sample_logprobs_f32(long lg, long params, long stats, int M, int n_vocab, int k,
|
||||
int lg_ld, int st_ld, int vocab_off, long stream) {
|
||||
if (!lg || !params || !stats) return -1;
|
||||
if (M <= 0 || n_vocab <= 0 || k <= 0) return -2;
|
||||
if ((uint32_t)k > kMaxLogprobs) return -3;
|
||||
if (!lg_ld) lg_ld = n_vocab;
|
||||
if (!st_ld) st_ld = 2 + 2 * k;
|
||||
hipLaunchKernelGGL(logprobs_kernel, dim3((uint32_t)M), dim3(kTopkThreads), 0,
|
||||
(hipStream_t)stream, (const float*)lg, (const SP*)params, (float*)stats,
|
||||
(uint32_t)n_vocab, (uint32_t)k, (uint32_t)lg_ld, (uint32_t)st_ld,
|
||||
(int32_t)vocab_off);
|
||||
return hipGetLastError() == hipSuccess ? 0 : -4;
|
||||
}
|
||||
|
||||
static int r4d_sample_topk_f32(long lg, long params, long cand_idx, long cand_val, long pairs,
|
||||
int M, int n_vocab, int n_cand,
|
||||
int lg_ld, int ci_ld, int cv_ld, int pr_ld, int vocab_off,
|
||||
@@ -1273,6 +1397,28 @@ extern "C" int r4d_rad_sample_argmax(const RadArgs* a, RadStream s) {
|
||||
(long)(uintptr_t)s));
|
||||
}
|
||||
|
||||
/* stats [M, 2 + 2k] f32: max, sum of exp(l - max), then k (global id, logit) pairs. */
|
||||
extern "C" int r4d_rad_sample_logprobs(const RadArgs* a, RadStream s) {
|
||||
const RadTensor* lg;
|
||||
long long M = 0, nv = 0, ld = 0;
|
||||
R4D_NEED(logits_of(a, &lg, &M, &nv, &ld));
|
||||
const RadTensor* pr = r4d_opt(a, V_PARAMS);
|
||||
R4D_NEED(params_ok(pr, M));
|
||||
const RadTensor* st = r4d_opt(a, 2);
|
||||
if (!st) return R4D_NO(RAD_E_INVAL);
|
||||
R4D_NEED(dt_f32(st));
|
||||
const long long k = r4d_pgeti_or(a->p, a->n_p, "k", 0);
|
||||
if (k <= 0 || k > (long long)kMaxLogprobs) return R4D_NO(RAD_E_SHAPE);
|
||||
long long Ms = 0;
|
||||
const long long st_ld = rows_pitch(st, 2 + 2 * k, &Ms);
|
||||
if (st_ld < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (Ms < M) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long voff = r4d_pgeti_or(a->p, a->n_p, "vocab_off", 0);
|
||||
R4D_LAUNCH(r4d_sample_logprobs_f32(r4d_addr(lg), r4d_addr(pr), r4d_addr(st), (int)M, (int)nv,
|
||||
(int)k, (int)ld, (int)st_ld, (int)voff,
|
||||
(long)(uintptr_t)s));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ top-k */
|
||||
|
||||
enum { TK_LOGITS = 0, TK_PARAMS, TK_CI, TK_CV, TK_PAIRS };
|
||||
|
||||
@@ -781,6 +781,20 @@ extern "C" int r4d_shape_sample_argmax(const RadParam* p, int n_p, int operand,
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_sample_logprobs(const RadParam* p, int n_p, int operand,
|
||||
RadOpdDesc* out) {
|
||||
|
||||
const int64_t M = pi(p, n_p, "M");
|
||||
const int64_t nv = pi(p, n_p, "n_vocab");
|
||||
const int64_t k = pi(p, n_p, "k");
|
||||
const RadOpdDesc l[] = {
|
||||
opd((uint32_t)RAD_F32, M, nv),
|
||||
opd_sample_params(M),
|
||||
opd((uint32_t)RAD_F32, M, 2 + 2 * k),
|
||||
};
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
/* top_p, min_p, typical and xtc: the same three operands, narrowed in place. */
|
||||
extern "C" int r4d_shape_sample_narrow(const RadParam* p, int n_p, int operand,
|
||||
RadOpdDesc* out) {
|
||||
|
||||
Reference in new issue
Block a user