Archived
radiance 1.0.11
- The n-gram embedding table (Qwen3.8-Flash-Next's per-layer embedding) can be gathered on the card. libr4d gains a row for embed_lookup_q whose output is the host gather's to the bit, and --ngram-placement uses it: ram keeps a pinned copy in RAM that the cards read in place over the link, so a step has no host work in it; vram puts the whole table on every card that holds it; disk reads it from the container on the host, as before. auto takes the first of the three that holds the table and prints why it passed over the others. Without a device kernel for the gather, ram is the locked copy the host gather reads and vram is refused, as in 1.0.10. - The pinned copy is not charged to RLIMIT_MEMLOCK; the host gather's locked copy still is, so --ulimit memlock=-1 matters only on a run without the cards. - The idea and a first kernel for the device gather came from Code000.
This commit is contained in:
1 parent
e466fa07e2
commit
469441d90f
24 files changed
+848
-247
No files matched your search
@@ -113,6 +113,63 @@ __global__ __launch_bounds__(kThreads) void embed_lookup_kernel(
|
||||
for (uint32_t i = threadIdx.x; i < n_embd; i += kThreads) dst[i] = src[i];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- embed_lookup_q
|
||||
// ref_embed_lookup_q. x[m] = bf16(wte[id] * scale), from a table stored as E4M3 codes with ONE
|
||||
// scale (Q) or as plain bf16, where an absent scale is one. This is the n-gram table's gather
|
||||
// (arch/common/rad_block_ple.h) wherever --ngram-placement lets a card read the table: in VRAM, or
|
||||
// in pinned host memory the card reads in place over the link.
|
||||
//
|
||||
// A THREAD A QUAD OF ENTRIES, NOT A BLOCK A ROW. A row of the n-gram table is 160 entries, so a
|
||||
// block a row leaves most of its threads idle and the grid is as deep as the chunk's row count.
|
||||
// Flattened, consecutive threads take consecutive quads, and a row's 40 threads read its 160
|
||||
// contiguous bytes. V = 1 is the same walk an entry at a time, for a width or a pitch the quad's
|
||||
// 4- and 8-byte accesses would misalign.
|
||||
//
|
||||
// THE VALUE libavx's LOOKUP TABLE HOLDS, to the bit: a code decodes to f32 exactly
|
||||
// (v_cvt_pk_f32_fp8, or the software form beside it in r4d_fp8_wmma.h), the product with the
|
||||
// scale rounds once in f32, and f32_to_bf16 rounds to nearest even. An id outside [0, n_vocab)
|
||||
// after the offset writes a zero row (the divergence note at the top of this file).
|
||||
template <uint32_t V, bool Q>
|
||||
__global__ __launch_bounds__(kThreads) void embed_lookup_q_kernel(
|
||||
const int32_t* __restrict__ tok, const void* __restrict__ wte, const void* __restrict__ sc,
|
||||
uint32_t sc_f32, uint16_t* __restrict__ x, uint32_t M, uint32_t n_embd, int32_t n_vocab,
|
||||
int32_t vocab_off, uint32_t x_ld) {
|
||||
const uint32_t per_row = n_embd / V;
|
||||
const uint64_t g = (uint64_t)blockIdx.x * kThreads + threadIdx.x;
|
||||
if (g >= (uint64_t)M * per_row) return;
|
||||
const uint32_t m = (uint32_t)(g / per_row), j = (uint32_t)(g % per_row) * V;
|
||||
const int32_t id = tok[m] - vocab_off;
|
||||
uint16_t* __restrict__ dst = x + (size_t)m * x_ld + j;
|
||||
if (id < 0 || id >= n_vocab) {
|
||||
if constexpr (V == 4) *(uint2*)dst = make_uint2(0u, 0u);
|
||||
else dst[0] = 0;
|
||||
return;
|
||||
}
|
||||
const float s = !sc ? 1.0f : sc_f32 ? *(const float*)sc : bf16_to_f32(*(const uint16_t*)sc);
|
||||
const size_t at = (size_t)id * n_embd + j;
|
||||
float v[V];
|
||||
if constexpr (Q && V == 4) {
|
||||
const uint32_t q = *(const uint32_t*)((const uint8_t*)wte + at);
|
||||
const v2f lo = r4d_fp8::e4m3_pk<false>(q), hi = r4d_fp8::e4m3_pk<true>(q);
|
||||
v[0] = lo.x; v[1] = lo.y; v[2] = hi.x; v[3] = hi.y;
|
||||
} else if constexpr (Q) {
|
||||
v[0] = r4d_fp8::e4m3(((const uint8_t*)wte)[at]);
|
||||
} else if constexpr (V == 4) {
|
||||
const uint2 b = *(const uint2*)((const uint16_t*)wte + at);
|
||||
v[0] = bf16_to_f32((uint16_t)b.x); v[1] = bf16_to_f32((uint16_t)(b.x >> 16));
|
||||
v[2] = bf16_to_f32((uint16_t)b.y); v[3] = bf16_to_f32((uint16_t)(b.y >> 16));
|
||||
} else {
|
||||
v[0] = bf16_to_f32(((const uint16_t*)wte)[at]);
|
||||
}
|
||||
if constexpr (V == 4) {
|
||||
*(uint2*)dst = make_uint2(
|
||||
(uint32_t)f32_to_bf16(v[0] * s) | ((uint32_t)f32_to_bf16(v[1] * s) << 16),
|
||||
(uint32_t)f32_to_bf16(v[2] * s) | ((uint32_t)f32_to_bf16(v[3] * s) << 16));
|
||||
} else {
|
||||
dst[0] = f32_to_bf16(v[0] * s);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- gather_rows
|
||||
// ref_gather_rows. Same shape as the embedding gather and the same negative-index rule, but the
|
||||
// source row pitch is the caller's rather than n: a gather off a padded activation buffer is what
|
||||
@@ -535,6 +592,25 @@ extern "C" int r4d_embed_lookup_bf16(long tok, long wte, long x, int M, int n_em
|
||||
return embed_lookup_bf16_emit(tok, wte, x, M, n_embd, n_vocab, vocab_off, x_ld, sk);
|
||||
}
|
||||
|
||||
/* The quad walk when the width, the pitch and both bases allow its 4- and 8-byte accesses (a bf16
|
||||
* table's quad is 8 bytes), an entry a thread otherwise. */
|
||||
static int embed_lookup_q_emit(const int32_t* tok, const void* wte, bool q, const void* sc,
|
||||
bool sc_f32, uint16_t* x, long long M, int n_embd, int n_vocab,
|
||||
int vocab_off, long long x_ld, const R4dSink& sk) {
|
||||
const bool quad = n_embd % 4 == 0 && x_ld % 4 == 0 && r4d_aligned(x, 8) &&
|
||||
r4d_aligned(wte, q ? 4 : 8);
|
||||
const uint64_t threads = (uint64_t)M * (uint64_t)(quad ? n_embd / 4 : n_embd);
|
||||
const dim3 grid((uint32_t)((threads + kThreads - 1) / kThreads));
|
||||
#define R4D_ELQ(V, Q) \
|
||||
R4D_EMIT(sk, (embed_lookup_q_kernel<V, Q>), grid, dim3(kThreads), 0, tok, wte, sc, \
|
||||
(uint32_t)(sc_f32 ? 1u : 0u), x, (uint32_t)M, (uint32_t)n_embd, (int32_t)n_vocab, \
|
||||
(int32_t)vocab_off, (uint32_t)x_ld)
|
||||
const int rc = quad ? (q ? R4D_ELQ(4, true) : R4D_ELQ(4, false))
|
||||
: (q ? R4D_ELQ(1, true) : R4D_ELQ(1, false));
|
||||
#undef R4D_ELQ
|
||||
return rc == RAD_OK ? 0 : -4;
|
||||
}
|
||||
|
||||
static int gather_rows_bf16_emit(long x, long idx, long y, int M, int n, int src_rows,
|
||||
int x_ld, int y_ld, const R4dSink& sk) {
|
||||
if (!x || !idx || !y) return -1;
|
||||
@@ -893,6 +969,46 @@ static int rad_embed_lookup_body(const RadArgs* a, const R4dSink& sk) {
|
||||
}
|
||||
R4D_ENTRY(r4d_rad_embed_lookup, rad_embed_lookup_body)
|
||||
|
||||
/* ------------------------------------------------------------------ embed_lookup_q */
|
||||
|
||||
/* Operand 4, `ahead`, is not read. It carries the ids of the prompt after this step for a host
|
||||
* reader that fetches table rows from a file before they are wanted; a card reads its table where
|
||||
* it already is, so there is nothing to fetch. */
|
||||
enum { ELQ_TOK = 0, ELQ_WTE, ELQ_SCALE, ELQ_X };
|
||||
|
||||
static int rad_embed_lookup_q_body(const RadArgs* a, const R4dSink& sk) {
|
||||
const RadTensor* tok = r4d_opt(a, ELQ_TOK);
|
||||
const RadTensor* wte = r4d_opt(a, ELQ_WTE);
|
||||
const RadTensor* sc = r4d_opt(a, ELQ_SCALE);
|
||||
const RadTensor* x = r4d_opt(a, ELQ_X);
|
||||
if (!tok || !wte || !x) return R4D_NO(RAD_E_INVAL);
|
||||
R4D_NEED(dt_i32(tok));
|
||||
R4D_NEED(dt_bf16(x));
|
||||
const bool q = wte->dtype == RAD_F8E4M3;
|
||||
if (!q && wte->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
|
||||
if (sc && sc->dtype != RAD_BF16 && sc->dtype != RAD_F32) return R4D_NO(RAD_E_DTYPE);
|
||||
if (sc && r4d_numel(sc) < 1) return R4D_NO(RAD_E_SHAPE);
|
||||
if (!r4d_contig(tok) || !r4d_contig(wte)) return R4D_NO(RAD_E_STRIDE);
|
||||
|
||||
const long long n_embd = r4d_geti_or(a, "n_embd", r4d_dim(wte, (int)wte->rank - 1));
|
||||
const long long n_vocab = r4d_geti_or(a, "n_vocab", r4d_dim(wte, (int)wte->rank - 2));
|
||||
const long long voff = r4d_geti_or(a, "vocab_offset", 0);
|
||||
if (n_embd <= 0 || n_vocab <= 0 || voff < 0) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r4d_numel(wte) < n_vocab * n_embd) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
long long M = 0;
|
||||
const long long x_ld = r4d_rows_pitch(x, n_embd, &M);
|
||||
if (x_ld < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (M <= 0 || r4d_numel(tok) < M) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
return embed_lookup_q_emit((const int32_t*)r4d_addr(tok), (const void*)r4d_addr(wte), q,
|
||||
sc ? (const void*)r4d_addr(sc) : nullptr,
|
||||
sc && sc->dtype == RAD_F32, (uint16_t*)r4d_addr(x), M,
|
||||
(int)n_embd, (int)n_vocab, (int)voff, x_ld, sk) == 0
|
||||
? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
R4D_ENTRY(r4d_rad_embed_lookup_q, rad_embed_lookup_q_body)
|
||||
|
||||
/* ------------------------------------------------------------------ gather_rows */
|
||||
|
||||
enum { GR_X = 0, GR_IDX, GR_Y };
|
||||
|
||||
Reference in new issue
Block a user