Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
+136
-115
@@ -2,32 +2,28 @@
|
||||
*
|
||||
* ===================================================================== WHY THIS FILE EXISTS
|
||||
*
|
||||
* A plugin is a shared object that exports the symbols in rad_abi.h and links NOTHING of
|
||||
* radiance's. That boundary is fixed. What it leaves an author with, though, is the raw structs
|
||||
* and nothing else -- so without this header every plugin arrives independently at the same dozen
|
||||
* helpers: an optional-operand fetch, a parameter-with-default, a contiguity test, a row pitch,
|
||||
* and f32<->bf16, f32<->f16 and f32<->fp8 conversions.
|
||||
* A plugin exports the symbols in rad_abi.h and links nothing of radiance's, so it has only the
|
||||
* raw structs. This header supplies the helpers every plugin would otherwise rewrite: an
|
||||
* optional-operand fetch, parameters with defaults, row pitch, and f32<->bf16/f16/fp8
|
||||
* conversions.
|
||||
*
|
||||
* Two of those are not merely a duplication of effort. The narrowing conversions define what a
|
||||
* result IS: libref's contract is f32 accumulation, IEEE round-to-nearest-even, no FMA and
|
||||
* no reassociation, and a plugin whose bf16 rounding truncates instead of rounding to even does
|
||||
* not reproduce the reference and cannot be checked against it. That convention belongs in the
|
||||
* ABI, stated once, not rediscovered per plugin. And the meaning of an ABSENT OPTIONAL OPERAND --
|
||||
* a null RadTensor.data, not a short operand count -- is a rule you get wrong silently: read the
|
||||
* operand after it and every weight in the model shifts by one.
|
||||
* Two of them are contracts, not conveniences. The narrowing conversions define what a result is:
|
||||
* libref's contract is f32 accumulation, IEEE round-to-nearest-even, no FMA and no reassociation,
|
||||
* and a plugin whose bf16 rounding truncates cannot be checked against the reference. And an
|
||||
* absent optional operand is a null RadTensor.data, not a short operand count; getting that wrong
|
||||
* silently shifts every later operand by one.
|
||||
*
|
||||
* ===================================================================== WHAT IS AND IS NOT HERE
|
||||
*
|
||||
* Here: pure, total, header-only functions over the ABI's own types. Nothing allocates, nothing
|
||||
* has state, nothing calls back into the core, and nothing may ever start.
|
||||
* Here: pure, total, header-only functions over the ABI's own types. Nothing allocates, holds
|
||||
* state or calls back into the core, and nothing may start to.
|
||||
*
|
||||
* Not here: anything that needs the core. A plugin that wants a device allocation, a stream or a
|
||||
* builder handle calls the functions in rad_device.h / rad_builder.h / rad_runtime.h, which the
|
||||
* host exports because it must -- those genuinely cannot be inlined, and the loader's ABI check
|
||||
* is what keeps them honest.
|
||||
* Not here: anything that needs the core. Device allocations, streams and builder handles come
|
||||
* from rad_device.h / rad_builder.h / rad_runtime.h, which the host exports; the loader's ABI
|
||||
* check covers them.
|
||||
*
|
||||
* This header is OPTIONAL. A plugin that includes only rad_abi.h is complete and correct; this
|
||||
* is convenience and one shared definition of the rounding, not a requirement.
|
||||
* This header is optional. A plugin that includes only rad_abi.h is complete; this is
|
||||
* convenience plus one shared definition of the rounding.
|
||||
*
|
||||
* Pure C, like the rest of abi/. See spec.md 2.
|
||||
*/
|
||||
@@ -44,9 +40,8 @@ extern "C" {
|
||||
|
||||
/* ================================================================== parameters
|
||||
*
|
||||
* Two families, because a plugin is asked for things in two shapes. `launch` and `scratch` get a
|
||||
* RadArgs; `layout`, `relayout`, `unrelayout` and `opd_shape` get a bare (const RadParam*, int)
|
||||
* pair with no tensors at all. Both are the same lookup and neither should be written twice. */
|
||||
* Two families: `launch` and `scratch` get a RadArgs; `layout`, `relayout`, `unrelayout` and
|
||||
* `opd_shape` get a bare (const RadParam*, int) pair with no tensors. */
|
||||
|
||||
static inline const RadParam* rad_param_find(const RadParam* p, int n_p, const char* key) {
|
||||
if (!p || !key) return 0;
|
||||
@@ -55,27 +50,20 @@ static inline const RadParam* rad_param_find(const RadParam* p, int n_p, const c
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* An int parameter, or `dflt` if the key is absent or is not an int.
|
||||
/* An int parameter, or `dflt` if the key is absent or not an int.
|
||||
*
|
||||
* A RANGE is deliberately NOT accepted here. A ranged parameter is the one axis a declared op is
|
||||
* bucketed over, and by the time a kernel runs it has been resolved to a value -- so a range
|
||||
* arriving at a launch is a selection bug, and quietly returning its low bound would hide it. */
|
||||
* A range is deliberately not accepted: by the time a kernel runs, the ranged axis has been
|
||||
* resolved to a value, so a range reaching a launch is a selection bug that returning its low
|
||||
* bound would hide. */
|
||||
static inline long long rad_param_geti(const RadParam* p, int n_p, const char* key,
|
||||
long long dflt) {
|
||||
const RadParam* f = rad_param_find(p, n_p, key);
|
||||
return (f && f->kind == RAD_P_INT) ? f->ival : dflt;
|
||||
}
|
||||
|
||||
/* THE SAME PARAMETER READ BY A SHAPE HOOK RATHER THAN BY A LAUNCH, which is the one place a range
|
||||
* is legitimate and must be read as its HIGH bound.
|
||||
*
|
||||
* `opd_shape` is called to size buffers BEFORE selection has collapsed anything: a tool asking
|
||||
* "how big is this operand" over a banded op is describing the whole band, and the arena has to
|
||||
* hold it. The low end fits inside the high end, so ihi is the answer and rad_param_geti's refusal
|
||||
* -- correct for a launch, where a surviving range is a selection bug -- is wrong here.
|
||||
*
|
||||
* Anyone writing a shape hook needs this on the first one they write, which is why it belongs in
|
||||
* the ABI rather than once per plugin. */
|
||||
/* The same read for a shape hook, the one place a range is legitimate; it reads as its high
|
||||
* bound. `opd_shape` sizes buffers before selection has collapsed anything, so over a banded op it
|
||||
* describes the whole band, and the high end covers the low. */
|
||||
static inline long long rad_param_getdim(const RadParam* p, int n_p, const char* key,
|
||||
long long dflt) {
|
||||
const RadParam* f = rad_param_find(p, n_p, key);
|
||||
@@ -96,9 +84,8 @@ static inline double rad_param_getf(const RadParam* p, int n_p, const char* key,
|
||||
return (f && f->kind == RAD_P_F64) ? f->dval : dflt;
|
||||
}
|
||||
|
||||
/* The dtype a parameter names, parsed. RAD_DT_INVALID if the key is absent or unrecognised --
|
||||
* which a kernel should treat as a refusal, because a dtype it cannot name is one it cannot
|
||||
* read. */
|
||||
/* The dtype a parameter names, parsed. RAD_DT_INVALID if the key is absent or unrecognised, which
|
||||
* a kernel should treat as a refusal. */
|
||||
static inline uint32_t rad_param_getdt(const RadParam* p, int n_p, const char* key) {
|
||||
return rad_dtype_parse(rad_param_gets(p, n_p, key, 0));
|
||||
}
|
||||
@@ -121,31 +108,23 @@ static inline double rad_args_getf_or(const RadArgs* a, const char* key, double
|
||||
|
||||
/* ================================================================== operands
|
||||
*
|
||||
* THE ABSENT-OPERAND RULE, WHICH IS THE EASIEST THING IN THIS ABI TO GET WRONG.
|
||||
* The absent-operand rule: an optional operand a call does not pass is a RadTensor whose `data`
|
||||
* is null, not a shorter `n_t`. Operands are positional, so treating absence as a shorter array
|
||||
* would read operand i+1 for operand i and produce wrong numbers without a fault.
|
||||
*
|
||||
* An optional operand a call does not pass is a RadTensor whose `data` is null -- NOT a shorter
|
||||
* `n_t`. The distinction is load-bearing because operands are POSITIONAL: if a plugin treated
|
||||
* absence as "the array is shorter" it would read operand i+1 where operand i was meant, and for
|
||||
* a weight that means every weight after it is off by one. Nothing in the arithmetic would fault;
|
||||
* the model would simply produce wrong numbers.
|
||||
*
|
||||
* So: one entry per schema operand, always, and this returns null for both spellings of absent --
|
||||
* past the end, or present with no data. A caller that needs to tell those apart has a schema
|
||||
* disagreement rather than an absent operand, and should say so. */
|
||||
* There is always one entry per schema operand. This returns null both past the end and for a
|
||||
* null `data`; a caller that sees the former has a schema disagreement, not an absent operand. */
|
||||
static inline const RadTensor* rad_arg_in(const RadArgs* a, int i) {
|
||||
if (!a || !a->t || i < 0 || i >= a->n_t) return 0;
|
||||
return a->t[i].data ? &a->t[i] : 0;
|
||||
}
|
||||
|
||||
|
||||
/* THERE IS NO rad_arg_out, AND THAT IS THE ABI SPEAKING RATHER THAN AN OMISSION. RadArgs::t is a
|
||||
* `const RadTensor*`: the DESCRIPTORS are const, and a kernel writes an output through the
|
||||
* non-const `void* data` inside one. So an output operand is fetched exactly like an input and
|
||||
* the constness a caller sees is the same either way. A separate accessor would have to cast the
|
||||
* descriptor's constness away and would be advertising a promise the ABI does not make. */
|
||||
/* There is deliberately no rad_arg_out. RadArgs::t is a `const RadTensor*`: the descriptors are
|
||||
* const, and a kernel writes an output through the non-const `void* data` inside one, so outputs
|
||||
* are fetched with rad_arg_in too. */
|
||||
|
||||
/* Are the first `n` operands all present? The usual opening line of a launch, where the required
|
||||
* ones are a prefix. */
|
||||
/* Are the first `n` operands all present? For a launch whose required operands are a prefix. */
|
||||
static inline int rad_args_have(const RadArgs* a, int n) {
|
||||
for (int i = 0; i < n; ++i) if (!rad_arg_in(a, i)) return 0;
|
||||
return 1;
|
||||
@@ -154,8 +133,8 @@ static inline int rad_args_have(const RadArgs* a, int n) {
|
||||
/* ================================================================== tensor shape helpers */
|
||||
|
||||
/* [rows..., K] flattened to (M, K): every leading axis is a row count and only the last is
|
||||
* structural. This is what a kernel that owns whole rows -- one workgroup per row -- wants.
|
||||
* Returns 0 on a rank-0 tensor. */
|
||||
* structural, as a row-owning kernel (one workgroup per row) wants. Returns 0 on a rank-0
|
||||
* tensor. */
|
||||
static inline int64_t rad_tensor_rows(const RadTensor* t) {
|
||||
if (!t || t->rank == 0) return 0;
|
||||
int64_t m = 1;
|
||||
@@ -167,36 +146,32 @@ static inline int64_t rad_tensor_lastdim(const RadTensor* t) {
|
||||
return (t && t->rank > 0) ? t->shape[t->rank - 1] : 0;
|
||||
}
|
||||
|
||||
/* The element distance between consecutive rows. For a contiguous tensor this is the last extent;
|
||||
* for a strided one it is the stride of the second-to-last axis, which is the pitch a row-owning
|
||||
* kernel has to honour. Strides are in ELEMENTS, not bytes (rad_types.h). */
|
||||
/* The element distance between consecutive rows: the stride of the second-to-last axis (the last
|
||||
* extent when contiguous). Strides are in elements, not bytes (rad_types.h). */
|
||||
static inline int64_t rad_tensor_row_pitch(const RadTensor* t) {
|
||||
if (!t || t->rank == 0) return 0;
|
||||
if (t->rank == 1) return t->shape[0];
|
||||
return t->stride[t->rank - 2];
|
||||
}
|
||||
|
||||
/* Is `p` aligned to `bytes`? Kernels that issue wide loads need this and the ABI does not promise
|
||||
* it -- alignment is one of the things a constraint row cannot express, so it is checked in the
|
||||
* entry point (rad_types.h: constraints are necessary, not sufficient). */
|
||||
/* Is `p` aligned to `bytes`? The ABI does not promise alignment and a constraint row cannot
|
||||
* express it, so a kernel issuing wide loads checks it in the entry point (rad_types.h). */
|
||||
static inline int rad_ptr_aligned(const void* p, int64_t bytes) {
|
||||
return bytes > 0 && ((uintptr_t)p % (uintptr_t)bytes) == 0;
|
||||
}
|
||||
|
||||
/* ================================================================== narrowing conversions
|
||||
*
|
||||
* THESE DEFINE WHAT A RESULT IS, so they are stated once here rather than per plugin.
|
||||
* These define what a result is, so they are stated once here.
|
||||
*
|
||||
* libref's contract -- and therefore the definition every kernel is checked against -- is
|
||||
* f32 accumulation, IEEE round-to-nearest-ties-to-even, no fused multiply-add and no
|
||||
* reassociation. `-ffp-contract=off` is a CORRECTNESS flag in this project, not a tuning one. A
|
||||
* plugin whose bf16 narrowing truncates instead of rounding to even is off by up to one ulp on
|
||||
* every element, which is not a tolerance question: it means the plugin cannot reproduce the
|
||||
* reference and cannot be checked against it.
|
||||
* libref's contract, which every kernel is checked against, is f32 accumulation, IEEE
|
||||
* round-to-nearest-ties-to-even, no fused multiply-add and no reassociation; `-ffp-contract=off`
|
||||
* is a correctness flag in this project. A plugin whose bf16 narrowing truncates is off by up to
|
||||
* one ulp on every element and cannot be checked against the reference.
|
||||
*
|
||||
* Every one of these is exact in the widening direction and RTNE in the narrowing direction, and
|
||||
* each handles subnormals rather than flushing them -- the hardware decodes subnormals correctly,
|
||||
* and flushing loses the smallest block of every quantised weight. */
|
||||
* Every conversion is exact when widening and RTNE when narrowing, and keeps subnormals rather
|
||||
* than flushing them: the hardware decodes subnormals, and flushing loses the smallest block of
|
||||
* every quantised weight. */
|
||||
|
||||
static inline float rad_bf16_to_f32(uint16_t h) {
|
||||
uint32_t u = (uint32_t)h << 16;
|
||||
@@ -205,10 +180,9 @@ static inline float rad_bf16_to_f32(uint16_t h) {
|
||||
return f;
|
||||
}
|
||||
|
||||
/* The `(u >> 16) & 1` is the tie-break: it adds one more than the halfway point exactly when the
|
||||
* surviving bit is odd, which is what sends a tie up rather than down. Overflow falls out --
|
||||
* 0x7f7fffff plus the rounding carries into 0x7f80, the bf16 infinity -- and a NaN is forced
|
||||
* quiet rather than allowed to round into an infinity. */
|
||||
/* `(u >> 16) & 1` is the tie-break: it adds one more than the halfway point when the surviving
|
||||
* bit is odd, so ties go to even. Overflow carries into 0x7f80, the bf16 infinity. A NaN is forced
|
||||
* quiet so it cannot round into an infinity. */
|
||||
static inline uint16_t rad_f32_to_bf16(float f) {
|
||||
uint32_t u;
|
||||
memcpy(&u, &f, 4);
|
||||
@@ -240,8 +214,8 @@ static inline float rad_f16_to_f32(uint16_t h) {
|
||||
return f;
|
||||
}
|
||||
|
||||
/* RTNE, with subnormals and overflow-to-infinity handled explicitly rather than by leaning on a
|
||||
* hardware convert whose mode a caller could have changed. */
|
||||
/* RTNE, with subnormals and overflow-to-infinity handled explicitly rather than by a hardware
|
||||
* convert whose rounding mode a caller could have changed. */
|
||||
static inline uint16_t rad_f32_to_f16(float f) {
|
||||
uint32_t u;
|
||||
memcpy(&u, &f, 4);
|
||||
@@ -279,9 +253,9 @@ static inline uint16_t rad_f32_to_f16(float f) {
|
||||
}
|
||||
}
|
||||
|
||||
/* OCP fp8 e4m3 ("e4m3fn"): 1-4-3, bias 7, NO infinity, S1111111 is the only NaN, max 448. This is
|
||||
* the KV-cache format and the block-scaled weight format, so its rounding sits on the attention
|
||||
* oracle's critical path and on every fp8 GEMM's. */
|
||||
/* OCP fp8 e4m3 ("e4m3fn"): 1-4-3, bias 7, no infinity, S1111111 is the only NaN, max 448. The
|
||||
* KV-cache and block-scaled weight format, so the attention oracle and every fp8 GEMM depend on
|
||||
* its rounding. */
|
||||
static inline float rad_fp8e4m3_to_f32(uint8_t b) {
|
||||
uint32_t s = (uint32_t)(b >> 7) << 31;
|
||||
uint32_t e = (uint32_t)(b >> 3) & 0xfu;
|
||||
@@ -306,9 +280,8 @@ static inline float rad_fp8e4m3_to_f32(uint8_t b) {
|
||||
return f;
|
||||
}
|
||||
|
||||
/* SATURATES at 448 rather than overflowing. Returning a NaN for a large activation would turn one
|
||||
* outlier into a poison that spreads through the whole row, and every fp8 cast in this project
|
||||
* clamps for that reason. */
|
||||
/* Saturates at 448 rather than overflowing: a NaN for one large activation would spread through
|
||||
* the whole row. Every fp8 cast in this project clamps for that reason. */
|
||||
static inline uint8_t rad_f32_to_fp8e4m3(float f) {
|
||||
uint32_t u;
|
||||
memcpy(&u, &f, 4);
|
||||
@@ -321,10 +294,10 @@ static inline uint8_t rad_f32_to_fp8e4m3(float f) {
|
||||
if (a >= 0x43e00000u) return (uint8_t)(s | 0x7eu); /* >= 448: clamp to the largest */
|
||||
e = (int)(a >> 23) - 127;
|
||||
m = a & 0x7fffffu;
|
||||
/* The smallest subnormal is 2^-9, so everything under HALF of it -- exponent -11 and
|
||||
* below -- rounds to zero. Exponent -10 is [2^-10, 2^-9): its tie point 2^-10 rounds to
|
||||
* the even zero and the rest of it up to 2^-9, which the subnormal branch does with a
|
||||
* 24-bit shift. */
|
||||
/* The smallest subnormal is 2^-9, so everything below half of it (exponent -11 and
|
||||
* below) rounds to zero. Exponent -10 is [2^-10, 2^-9): its tie point 2^-10 rounds to the
|
||||
* even zero and the rest up to 2^-9, which the subnormal branch does with a 24-bit
|
||||
* shift. */
|
||||
if (e < -10) return s;
|
||||
if (e < -6) { /* subnormal fp8 */
|
||||
int sh = -6 - e + 20;
|
||||
@@ -349,9 +322,12 @@ static inline uint8_t rad_f32_to_fp8e4m3(float f) {
|
||||
}
|
||||
}
|
||||
|
||||
/* fp8 e5m2: 1-5-2, bias 15, IEEE-shaped -- it has infinities and NaNs, unlike e4m3fn. It is the
|
||||
* top byte of an f16, so both directions go through the f16 path and the narrowing rounds to even
|
||||
* a SECOND time on the way down. */
|
||||
/* fp8 e5m2: 1-5-2, bias 15, IEEE-shaped (it has infinities and NaNs, unlike e4m3fn). It is the
|
||||
* top byte of an f16, so both directions go through the f16 path.
|
||||
*
|
||||
* Narrowing rounds to f16 and then drops the low byte. Every e5m2 tie point is an f16 value, so
|
||||
* the f16 step can move `f` onto a tie but never across one; when it lands on a tie, `f`'s side
|
||||
* of it decides, and only an exact tie goes to even. That makes the result one RTNE of `f`. */
|
||||
static inline float rad_fp8e5m2_to_f32(uint8_t b) {
|
||||
return rad_f16_to_f32((uint16_t)((uint16_t)b << 8));
|
||||
}
|
||||
@@ -360,19 +336,30 @@ static inline uint8_t rad_f32_to_fp8e5m2(float f) {
|
||||
uint16_t h = rad_f32_to_f16(f);
|
||||
uint16_t lo = (uint16_t)(h & 0xffu);
|
||||
uint16_t q = (uint16_t)(h >> 8);
|
||||
/* A NaN keeps its top payload bits and stays quiet; rounding its low byte could carry it out
|
||||
* of the exponent into a zero of the other sign. */
|
||||
if ((h & 0x7fffu) > 0x7c00u) return (uint8_t)(q | 0x02u);
|
||||
if (lo == 0x80u && (h & 0x7c00u) != 0x7c00u) {
|
||||
/* Magnitudes of non-negative floats order as their bit patterns. */
|
||||
float t = rad_f16_to_f32(h);
|
||||
uint32_t uf, ut;
|
||||
memcpy(&uf, &f, 4);
|
||||
memcpy(&ut, &t, 4);
|
||||
uf &= 0x7fffffffu;
|
||||
ut &= 0x7fffffffu;
|
||||
if (uf != ut) return (uint8_t)(uf > ut ? q + 1u : q);
|
||||
}
|
||||
if (lo > 0x80u || (lo == 0x80u && (q & 1u))) ++q;
|
||||
return (uint8_t)q;
|
||||
}
|
||||
|
||||
/* OCP FP4 e2m1: 1-2-1, bias 1, no infinity and no NaN, so every one of the sixteen codes is a
|
||||
* number and the largest is 6. */
|
||||
/* OCP FP4 e2m1: 1-2-1, bias 1, no infinity or NaN; all sixteen codes are numbers, max 6. */
|
||||
static inline float rad_fp4e2m1_to_f32(uint8_t nib) {
|
||||
static const float mag[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f };
|
||||
return (nib & 8u) ? -mag[nib & 7u] : mag[nib & 7u];
|
||||
}
|
||||
|
||||
/* OCP FP6, both forms. No infinity and no NaN; e2m3 has bias 1 and tops out at 7.5, e3m2 has
|
||||
* bias 3 and tops out at 28. */
|
||||
/* OCP FP6, both forms. No infinity or NaN; e2m3 has bias 1 and max 7.5, e3m2 bias 3 and max 28. */
|
||||
static inline float rad_fp6_to_f32(uint8_t c, int ebits) {
|
||||
const int mbits = 5 - ebits, bias = ebits == 2 ? 1 : 3;
|
||||
const int e = (c >> mbits) & ((1 << ebits) - 1), m = c & ((1 << mbits) - 1);
|
||||
@@ -382,7 +369,7 @@ static inline float rad_fp6_to_f32(uint8_t c, int ebits) {
|
||||
return (c & 0x20) ? -mag : mag;
|
||||
}
|
||||
|
||||
/* THE BIT STREAM, for every element narrower than a byte or not a power of two wide.
|
||||
/* The bit stream, for every element narrower than a byte or not a power of two wide.
|
||||
*
|
||||
* Element `i` of a b-bit row occupies bits [i*b, (i+1)*b) counted from bit 0 of `base`, lowest
|
||||
* first, so it may straddle a byte; the read touches only the bytes the element is in. Widths to
|
||||
@@ -444,13 +431,11 @@ static inline float rad_e8m0_to_f32(uint8_t e) {
|
||||
/* ================================================================== dtype-generic access
|
||||
*
|
||||
* Load or store element `i` of a plane as f32, whatever it is stored as. This is what a host
|
||||
* kernel and a converter both need, and it is the one place the dtype enum and the conversions
|
||||
* above are wired together -- so a plugin that adds support for a dtype adds it once. Returns 0
|
||||
* for a dtype it cannot read, which a caller must treat as a refusal rather than as a value.
|
||||
* kernel and a converter both need, and the one place the dtype enum and the conversions above
|
||||
* meet. Returns 0 for a dtype it cannot read, which a caller must treat as a refusal, not a value.
|
||||
*
|
||||
* A SUB-BYTE `i` COUNTS ELEMENTS FROM `base`, which is right inside one row and wrong across rows
|
||||
* whose element count does not fill their last byte: a row starts on a byte (rad_encoding.h), so
|
||||
* a caller crossing rows steps `base` a row's bytes at a time. */
|
||||
* For sub-byte dtypes `i` counts elements from `base`, which is valid only within one row: rows
|
||||
* start on a byte (rad_encoding.h), so a caller crossing rows steps `base` by a row's bytes. */
|
||||
static inline float rad_load_f32(const void* base, uint32_t dtype, int64_t i) {
|
||||
const uint8_t* b8 = (const uint8_t*)base;
|
||||
int sgn = 0;
|
||||
@@ -482,10 +467,10 @@ static inline float rad_load_f32(const void* base, uint32_t dtype, int64_t i) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Store the raw CODE of element `i` of a sub-byte or byte plane -- the bit pattern, already
|
||||
* chosen, masked to the dtype's width -- leaving its neighbours in the same byte alone. A signed
|
||||
* code is passed as its value (-8..7 for i4) and stored two's complement. The quantisers' half of
|
||||
* rad_load_f32; nothing rounds here. Returns 0 for a dtype wider than 16 bits. */
|
||||
/* Store the raw code of element `i` of a sub-byte or byte plane (the chosen bit pattern, masked to
|
||||
* the dtype's width), leaving neighbours in the same byte alone. A signed code is passed as its
|
||||
* value (-8..7 for i4) and stored two's complement. The quantisers' counterpart to rad_load_f32;
|
||||
* nothing rounds here. Returns 0 for a dtype wider than 16 bits. */
|
||||
static inline int rad_store_code(void* base, uint32_t dtype, int64_t i, int code) {
|
||||
const int bits = rad_dtype_bits(dtype);
|
||||
if (bits < 1 || bits > 16) return 0;
|
||||
@@ -495,6 +480,28 @@ static inline int rad_store_code(void* base, uint32_t dtype, int64_t i, int code
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* `v` rounded to the nearest integer, ties to even, and clamped to [lo, hi]; NaN is 0. Exact and
|
||||
* independent of the rounding mode: a float of magnitude 2^23 or more is already an integer, and
|
||||
* below that its fraction is exact in a double. */
|
||||
static inline double rad_round_even_sat(float v, double lo, double hi) {
|
||||
double d = (double)v, r;
|
||||
if (v != v) return 0.0;
|
||||
if (d >= 8388608.0 || d <= -8388608.0) {
|
||||
r = d;
|
||||
} else {
|
||||
const long long t = (long long)d; /* toward zero */
|
||||
const double f = d - (double)t;
|
||||
long long n = t;
|
||||
if (f > 0.5 || (f == 0.5 && (t & 1))) ++n;
|
||||
else if (f < -0.5 || (f == -0.5 && (t & 1))) --n;
|
||||
r = (double)n;
|
||||
}
|
||||
return r < lo ? lo : (r > hi ? hi : r);
|
||||
}
|
||||
|
||||
/* Store `v` as element `i` of a byte-aligned plane. A float dtype rounds as the narrowings above
|
||||
* do; an integer dtype rounds to nearest, ties to even, and saturates to its range, NaN storing 0.
|
||||
* Sub-byte dtypes are stored with rad_store_code; any other dtype is left unwritten. */
|
||||
static inline void rad_store_f32(void* base, uint32_t dtype, int64_t i, float v) {
|
||||
switch (dtype) {
|
||||
case RAD_F32: ((float*)base)[i] = v; break;
|
||||
@@ -502,12 +509,26 @@ static inline void rad_store_f32(void* base, uint32_t dtype, int64_t i, float v)
|
||||
case RAD_BF16: ((uint16_t*)base)[i] = rad_f32_to_bf16(v); break;
|
||||
case RAD_F8E4M3: ((uint8_t*)base)[i] = rad_f32_to_fp8e4m3(v); break;
|
||||
case RAD_F8E5M2: ((uint8_t*)base)[i] = rad_f32_to_fp8e5m2(v); break;
|
||||
case RAD_I8: ((int8_t*)base)[i] = (int8_t)v; break;
|
||||
case RAD_U8: ((uint8_t*)base)[i] = (uint8_t)v; break;
|
||||
case RAD_I16: ((int16_t*)base)[i] = (int16_t)v; break;
|
||||
case RAD_I32: ((int32_t*)base)[i] = (int32_t)v; break;
|
||||
case RAD_U32: ((uint32_t*)base)[i] = (uint32_t)v; break;
|
||||
case RAD_I64: ((int64_t*)base)[i] = (int64_t)v; break;
|
||||
case RAD_I8: ((int8_t*)base)[i] = (int8_t)rad_round_even_sat(v, -128.0, 127.0); break;
|
||||
case RAD_U8: ((uint8_t*)base)[i] = (uint8_t)rad_round_even_sat(v, 0.0, 255.0); break;
|
||||
case RAD_U16:
|
||||
((uint16_t*)base)[i] = (uint16_t)rad_round_even_sat(v, 0.0, 65535.0);
|
||||
break;
|
||||
case RAD_I16:
|
||||
((int16_t*)base)[i] = (int16_t)rad_round_even_sat(v, -32768.0, 32767.0);
|
||||
break;
|
||||
case RAD_I32:
|
||||
((int32_t*)base)[i] = (int32_t)rad_round_even_sat(v, -2147483648.0, 2147483647.0);
|
||||
break;
|
||||
case RAD_U32:
|
||||
((uint32_t*)base)[i] = (uint32_t)rad_round_even_sat(v, 0.0, 4294967295.0);
|
||||
break;
|
||||
case RAD_I64: {
|
||||
/* 2^63 is the first float past the range, so it is compared, not clamped to. */
|
||||
const double r = rad_round_even_sat(v, -9223372036854775808.0, 9223372036854775808.0);
|
||||
((int64_t*)base)[i] = r >= 9223372036854775808.0 ? INT64_MAX : (int64_t)r;
|
||||
break;
|
||||
}
|
||||
case RAD_BOOL: ((uint8_t*)base)[i] = v != 0.0f ? 1u : 0u; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user