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
@@ -539,3 +539,36 @@ extern "C" int r4d_rad_unrelayout_mtp_e4m3(const RadParam* p, int n_p, int opera
|
||||
if (rc == RAD_OK) rc = hcrows_enc_layout(g, "r4d.mtp.e4m3.g128", enc, sel, pl, n, &L);
|
||||
return rc != RAD_OK ? rc : hcrows_unrelayout(g, src, src_bytes, pl);
|
||||
}
|
||||
|
||||
/* ================================================================= embed_lookup_q's table
|
||||
*
|
||||
* The gather reads the table AS IT IS, so this hook only answers whether it can: E4M3 codes with
|
||||
* ONE bf16 scale for the whole table (`fp8_e4m3*bf16[*x*]`, the n-gram table's recipe), or the
|
||||
* table as the checkpoint ships it, plain bf16 with no scale. The same two forms libavx's host
|
||||
* gather accepts (avx_layout_ngram), checked the same way, so a model whose table one of them
|
||||
* reads is never refused by the other at declare. */
|
||||
extern "C" int r4d_rad_layout_embed_q(const RadParam* p, int n_p, int operand,
|
||||
const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out) {
|
||||
(void)out;
|
||||
enum { EQ_WTE = 1, EQ_SCALE = 2 };
|
||||
if (operand != EQ_WTE && operand != EQ_SCALE) return RAD_E_UNSUPPORTED;
|
||||
const long long rows = rad_param_geti(p, n_p, "n_vocab", 0);
|
||||
const long long dim = rad_param_geti(p, n_p, "n_embd", 0);
|
||||
if (rows <= 0 || dim <= 0) return RAD_E_SHAPE;
|
||||
if (rad_enc_is(enc, "plain") && enc->n_planes == 1 && enc->plane[0].dtype == RAD_BF16) {
|
||||
if (operand != EQ_WTE) return RAD_E_DTYPE;
|
||||
if (n != 1 || !pl || !r4d_enc::dims(pl[0], rows, dim)) return RAD_E_SHAPE;
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
const RadEncPlane* sc = rad_enc_plane(enc, "scale");
|
||||
if (!rad_enc_is(enc, "affine") || enc->n_planes != 2 || enc->transform[0] ||
|
||||
enc->plane[0].dtype != RAD_F8E4M3 || !sc || sc->dtype != RAD_BF16 || sc->block[0] != 0 ||
|
||||
sc->block[1] != 0)
|
||||
return RAD_E_DTYPE;
|
||||
if (n != 1 || !pl || !sel) return RAD_E_SHAPE;
|
||||
if (!r4d_enc::selects(enc, sel, n, operand == EQ_WTE ? "codes" : "scale")) return RAD_E_SHAPE;
|
||||
if (!r4d_enc::dims(pl[0], operand == EQ_WTE ? rows : 1, operand == EQ_WTE ? dim : 1))
|
||||
return RAD_E_SHAPE;
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
@@ -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 };
|
||||
|
||||
@@ -340,6 +340,8 @@ R4D_HOOKS(moe_w4_mod4)
|
||||
#undef R4D_HOOKS
|
||||
int r4d_rad_layout_fp8_rowmajor(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
const RadTensor*, int, RadLayout*);
|
||||
int r4d_rad_layout_embed_q(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
const RadTensor*, int, RadLayout*);
|
||||
int r4d_rad_layout_moe_fp8(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
const RadTensor*, int, RadLayout*);
|
||||
int r4d_rad_layout_moe_fp8_mod4(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
@@ -364,6 +366,10 @@ int r4d_rad_relayout_mxfp4(const RadParam*, int, int, const RadEncoding*, const
|
||||
* fatal issue, not a slow one. `logits_gemm` is declared with them. */
|
||||
int r4d_rad_embed_lookup (const RadArgs*, RadStream);
|
||||
int r4d_rad_embed_lookup_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
/* The quantised gather (embed_lookup_q): the n-gram table's, where a card reads the table -- in
|
||||
* VRAM, or in pinned host memory over the link. */
|
||||
int r4d_rad_embed_lookup_q (const RadArgs*, RadStream);
|
||||
int r4d_rad_embed_lookup_q_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_ngram_ids (const RadArgs*, RadStream);
|
||||
/* ABI v10: the same dispatch, described. Generated with the launch above by R4D_ENTRY, from one
|
||||
* body, so the two cannot describe different work. */
|
||||
@@ -427,6 +433,7 @@ int r4d_rad_sample_merge_topk(const RadArgs*, RadStream);
|
||||
* Pure host code, called before anything is allocated, so it includes neither r4d.h nor
|
||||
* <hip/hip_runtime.h> -- the same split r4d_rows.cpp and r4d_layout.cpp keep. */
|
||||
int r4d_shape_embed_lookup (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_embed_lookup_q (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_gather_rows (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_gather_rows_u8 (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_scatter_rows_u8(const RadParam*, int, int operand, RadOpdDesc*);
|
||||
|
||||
@@ -1614,6 +1614,16 @@ static const RadOperandSpec oEmbed[] = {
|
||||
{ "wte", RAD_OPD_WEIGHT, 0 },
|
||||
{ "x", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* The quantised gather's: libref's list (ref_registry.cpp), which the loader holds every plugin
|
||||
* serving the op to -- a one-element `scale`, absent for a table stored plain, and `ahead`, the
|
||||
* next prompt's ids for a host reader that fetches rows from a file. */
|
||||
static const RadOperandSpec oEmbedQ[] = {
|
||||
{ "tokens", RAD_OPD_IN, 0 },
|
||||
{ "wte", RAD_OPD_WEIGHT, 0 },
|
||||
{ "scale", RAD_OPD_WEIGHT, 1 },
|
||||
{ "x", RAD_OPD_OUT, 0 },
|
||||
{ "ahead", RAD_OPD_IN, 1 },
|
||||
};
|
||||
/* PLE's hashed n-gram ids. `state` is a rolling window of committed ids -- RAD_KV_CONV's shape and
|
||||
* ple_conv's contract -- except that a cold window is EOS and not zero, because zero is a real
|
||||
* token id. */
|
||||
@@ -1821,6 +1831,11 @@ static const RadOpSchema kSchemas[] = {
|
||||
"[vocab_offset, vocab_offset + n_vocab) -- which is what makes the table shardable "
|
||||
"across the vocabulary, the symmetry lm_head already had. Ids stay GLOBAL; summing the "
|
||||
"ranks reconstructs x, so the caller follows this with an exact all_reduce."),
|
||||
SCHEMA("embed_lookup_q", pEmbed, oEmbedQ,
|
||||
"the same gather from a table quantised with ONE scale for all of it: x[m] = "
|
||||
"wte[id] * scale[0], an absent scale being one. Padding and bounds behave exactly as "
|
||||
"embed_lookup's. `ahead` is the next prompt's ids, for a reader that fetches rows ahead "
|
||||
"of the step; a kernel that reads its table in place ignores it."),
|
||||
SCHEMA("ple_gate", pPleGate, oPleGate,
|
||||
"PLE's gate. K = grouped_rmsnorm(k) * (w_key + wadd) and Q likewise over `q`, one rsqrt "
|
||||
"per n-wide stream and one gain across all hc*n; s[c] = sum_d K[c][d]*Q[c][d] / sqrt(n); "
|
||||
@@ -3137,6 +3152,15 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_embed_lookup, nullptr, nullptr, nullptr,
|
||||
r4d_shape_embed_lookup, nullptr, nullptr, r4d_rad_embed_lookup_describe },
|
||||
|
||||
{ "embed_lookup_q_bf16", "embed_lookup_q", "vocab",
|
||||
"quantised embedding gather, four entries a thread",
|
||||
"any n_embd and n_vocab; a negative or out-of-range id writes a zero row",
|
||||
"i32 tokens, an E4M3 table with a bf16 or f32 scale or a plain bf16 one, bf16 output",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cBf16Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_embed_lookup_q, nullptr, r4d_rad_layout_embed_q, nullptr,
|
||||
r4d_shape_embed_lookup_q, nullptr, nullptr, r4d_rad_embed_lookup_q_describe },
|
||||
|
||||
{ "ple_gate_bf16", "ple_gate", "norm",
|
||||
"PLE's dot-product gate, one workgroup a (row, stream) pair",
|
||||
"n up to 4096 -- every reduction is over one stream's n and lives in a workgroup",
|
||||
|
||||
@@ -9112,6 +9112,119 @@ static void gather_two_rank(const Plugin& pl, const std::vector<int>& devs) {
|
||||
* `tok` behind them. Their ids must be the ids step two computes running [7, 8, 9] against the
|
||||
* window step one left; that window must be the step's own; and the step's own ids must be the
|
||||
* ones a launch without `ahead_ids` writes. The same property ref_test holds libref to. */
|
||||
/* embed_lookup_q against a host gather of the same bytes, through the host side's own conversions.
|
||||
*
|
||||
* THE BAR IS THE BIT, not a tolerance. libavx's gather decodes a code as
|
||||
* rad_f32_to_bf16(rad_fp8e4m3_to_f32(code) * scale), and the device kernel has to produce exactly
|
||||
* that for every code, or one model reads a different n-gram embedding depending on which tier
|
||||
* --ngram-placement put its table on. So the oracle is those two functions, the table holds every
|
||||
* code but the two NaN encodings (no quantiser writes them), and the comparison is of the bytes.
|
||||
*
|
||||
* Five launches: the quad walk and the entry walk (the model's width of 160, and 13); an E4M3
|
||||
* table with a scale and a plain bf16 one without, whose rows come back unchanged; the table in
|
||||
* VRAM and in pinned host memory read over the link, the placement this kernel exists for. The ids
|
||||
* include padding (-1) and one past the table, and both write a zero row. */
|
||||
static void embed_lookup_q_case(const Plugin& pl) {
|
||||
if (!sel("embed_lookup_q_bf16")) return;
|
||||
const char* what = "embed_lookup_q vs a host gather, to the bit";
|
||||
const RadKernelInfo* r = find(pl, "embed_lookup_q_bf16");
|
||||
if (!r) { ok(what, false, "row absent"); return; }
|
||||
|
||||
struct Shape { int64_t ne; bool e4m3; bool host; };
|
||||
const Shape shapes[] = { { 160, true, false }, { 160, true, true }, { 13, true, false },
|
||||
{ 160, false, true }, { 13, false, false } };
|
||||
const int64_t NV = 1031, M = 37;
|
||||
const float scale = rad_bf16_to_f32(rad_f32_to_bf16(1.99317932e-4f));
|
||||
std::vector<void*> pinned;
|
||||
std::string bad;
|
||||
for (const Shape& sh : shapes) {
|
||||
const int64_t es = sh.e4m3 ? 1 : 2;
|
||||
const size_t tbytes = (size_t)(NV * sh.ne * es);
|
||||
std::vector<uint8_t> table(tbytes);
|
||||
uint32_t rs = 0x9e3779b9u;
|
||||
for (int64_t i = 0; i < NV * sh.ne; ++i) {
|
||||
rs = rs * 1664525u + 1013904223u;
|
||||
if (sh.e4m3) {
|
||||
uint8_t c = (uint8_t)((i * 167 + (rs >> 24)) & 0xFF);
|
||||
if ((c & 0x7F) == 0x7F) c ^= 0x01; /* 0x7F and 0xFF are NaN */
|
||||
table[(size_t)i] = c;
|
||||
} else {
|
||||
const uint16_t b = rad_f32_to_bf16((float)((int32_t)(rs >> 9) - (1 << 22)) * 1e-6f);
|
||||
std::memcpy(&table[(size_t)(2 * i)], &b, 2);
|
||||
}
|
||||
}
|
||||
std::vector<int32_t> ids((size_t)M);
|
||||
for (int64_t m = 0; m < M; ++m) ids[(size_t)m] = (int32_t)((m * 131 + 7) % NV);
|
||||
ids[3] = -1;
|
||||
ids[7] = (int32_t)NV;
|
||||
ids[11] = (int32_t)(NV - 1);
|
||||
|
||||
void* d_tab = nullptr;
|
||||
if (sh.host) {
|
||||
void* h = nullptr;
|
||||
if (hipHostMalloc(&h, tbytes, hipHostMallocMapped) != hipSuccess) {
|
||||
ok(what, false, "pinned table allocation failed");
|
||||
return;
|
||||
}
|
||||
pinned.push_back(h);
|
||||
std::memcpy(h, table.data(), tbytes);
|
||||
if (hipHostGetDevicePointer(&d_tab, h, 0) != hipSuccess || !d_tab) {
|
||||
ok(what, false, "no device address for the pinned table");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
d_tab = dalloc(tbytes);
|
||||
st_memcpy(d_tab, table.data(), tbytes, hipMemcpyHostToDevice);
|
||||
}
|
||||
const uint16_t sc_bits = rad_f32_to_bf16(scale);
|
||||
void* d_sc = dalloc(2);
|
||||
st_memcpy(d_sc, &sc_bits, 2, hipMemcpyHostToDevice);
|
||||
void* d_tok = dalloc((size_t)M * 4);
|
||||
st_memcpy(d_tok, ids.data(), (size_t)M * 4, hipMemcpyHostToDevice);
|
||||
void* d_x = dalloc((size_t)(M * sh.ne * 2));
|
||||
st_memset(d_x, 0xFF, (size_t)(M * sh.ne * 2)); /* a zero row has to be written */
|
||||
|
||||
std::vector<RadTensor> ts = { T(d_tok, RAD_I32, { M }),
|
||||
T(d_tab, sh.e4m3 ? RAD_F8E4M3 : RAD_BF16, { NV, sh.ne }),
|
||||
T(sh.e4m3 ? d_sc : nullptr, RAD_BF16, { 1 }),
|
||||
T(d_x, RAD_BF16, { M, sh.ne }) };
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("n_embd", sh.ne), PI("n_vocab", NV),
|
||||
PS("dtype", "bf16") };
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts.data(); a.n_t = (int)ts.size();
|
||||
a.p = ps.data(); a.n_p = (int)ps.size();
|
||||
const int rc = r->launch(&a, nullptr);
|
||||
st_stream_sync(nullptr);
|
||||
std::vector<uint16_t> got((size_t)(M * sh.ne), 0);
|
||||
st_memcpy(got.data(), d_x, got.size() * 2, hipMemcpyDeviceToHost);
|
||||
|
||||
int64_t wrong = 0;
|
||||
for (int64_t m = 0; m < M; ++m) {
|
||||
const int32_t id = ids[(size_t)m];
|
||||
for (int64_t i = 0; i < sh.ne; ++i) {
|
||||
uint16_t want = 0;
|
||||
if (id >= 0 && id < NV) {
|
||||
const size_t at = (size_t)(id * sh.ne + i);
|
||||
if (sh.e4m3) {
|
||||
want = rad_f32_to_bf16(rad_fp8e4m3_to_f32(table[at]) * scale);
|
||||
} else {
|
||||
std::memcpy(&want, &table[2 * at], 2);
|
||||
}
|
||||
}
|
||||
if (got[(size_t)(m * sh.ne + i)] != want) ++wrong;
|
||||
}
|
||||
}
|
||||
if (rc != RAD_OK || wrong)
|
||||
bad += fmt("%s n_embd=%lld %s: rc=%d, %lld of %lld entries differ; ",
|
||||
sh.e4m3 ? "e4m3" : "bf16", (long long)sh.ne,
|
||||
sh.host ? "pinned host table" : "vram table", rc, (long long)wrong,
|
||||
(long long)(M * sh.ne));
|
||||
}
|
||||
for (void* h : pinned) hip_ck(hipHostFree(h), "hipHostFree", __LINE__);
|
||||
ok(what, bad.empty(), bad);
|
||||
}
|
||||
|
||||
static void ngram_ahead_case(const Plugin& pl) {
|
||||
if (!sel("ngram_ids_i32")) return;
|
||||
const RadKernelInfo* r = find(pl, "ngram_ids_i32");
|
||||
@@ -9194,6 +9307,7 @@ static void device_phase(const Plugin& pl) {
|
||||
if (!g_case) std::printf("\ndevice smoke tests (device %d)\n", devs[0]);
|
||||
|
||||
ngram_ahead_case(pl);
|
||||
embed_lookup_q_case(pl);
|
||||
|
||||
/* --- the paged cache's write side --- */
|
||||
kv_store_case(pl, "kv_store_bf16", "bf16");
|
||||
|
||||
@@ -296,6 +296,26 @@ extern "C" int r4d_shape_embed_lookup(const RadParam* p, int n_p, int operand,
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
/* The quantised gather: libref's description (ref_embed_lookup_q_shape), an E4M3 table and a
|
||||
* one-element scale in the activation dtype. `ahead` is not passed: it is ids for a reader that
|
||||
* fetches rows from a file, and the kernel does not read it. */
|
||||
extern "C" int r4d_shape_embed_lookup_q(const RadParam* p, int n_p, int operand,
|
||||
RadOpdDesc* out) {
|
||||
const int64_t M = pi(p, n_p, "M");
|
||||
const int64_t ne = pi(p, n_p, "n_embd");
|
||||
const int64_t nv = pi(p, n_p, "n_vocab");
|
||||
const int64_t vo = pi(p, n_p, "vocab_offset", 0);
|
||||
const uint32_t D = dt_act(p, n_p, "dtype");
|
||||
const RadOpdDesc l[] = {
|
||||
opd_idx(M, 0, vo + nv),
|
||||
opd((uint32_t)RAD_F8E4M3, nv, ne),
|
||||
opd(D, 1),
|
||||
opd(D, M, ne),
|
||||
opd_none(),
|
||||
};
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_gather_rows(const RadParam* p, int n_p, int operand,
|
||||
RadOpdDesc* out) {
|
||||
|
||||
|
||||
Reference in new issue
Block a user