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
+100
-152
@@ -1,42 +1,37 @@
|
||||
/* ref_ple.cpp -- the gate of Qwen4-Exp's Per-Layer Embedding: everything between the n-gram
|
||||
* lookup's two projections and the dilated convolution.
|
||||
*
|
||||
* ============================== WHAT IT IS ==============================
|
||||
* ============================== What it is ==============================
|
||||
*
|
||||
* PLE mixes a hashed n-gram embedding into the residual stream, and the mixing coefficient is a
|
||||
* DOT PRODUCT between the embedding and the stream -- one scalar per hyper-connection stream, per
|
||||
* token. Written out, with `n` the model width and `hc` the number of streams:
|
||||
* PLE mixes a hashed n-gram embedding into the residual stream with a mixing coefficient that is a
|
||||
* dot product between the embedding and the stream: one scalar per hyper-connection stream per
|
||||
* token. With `n` the model width and `hc` the number of streams:
|
||||
*
|
||||
* K[c] = grouped_rmsnorm(k)[c] * (w_key[c] + wadd) k is key_proj(embedding)
|
||||
* Q[c] = grouped_rmsnorm(q)[c] * (w_query[c] + wadd) q is the INCOMING hc stream
|
||||
* Q[c] = grouped_rmsnorm(q)[c] * (w_query[c] + wadd) q is the incoming hc stream
|
||||
* s[c] = sum_d K[c][d] * Q[c][d] / sqrt(n)
|
||||
* s[c] = sign(s[c]) * sqrt(max(|s[c]|, gate_eps)) <- the signed square root
|
||||
* gv[c] = sigmoid(s[c]) * v v is value_proj(embedding), ONE row
|
||||
* gv[c] = sigmoid(s[c]) * v v is value_proj(embedding), one row
|
||||
* gvn = grouped_rmsnorm(gv) * (w_conv + wadd)
|
||||
*
|
||||
* and the caller finishes with `out = gv + silu(dilated_conv(gvn))`. BOTH gv AND gvn leave this op
|
||||
* because the convolution reads the NORMED copy and the residual add reads the UN-normed one --
|
||||
* two different tensors, and recomputing either outside would be a second definition of the norm.
|
||||
* and the caller finishes with `out = gv + silu(dilated_conv(gvn))`. Both gv and gvn are outputs:
|
||||
* the convolution reads the normed copy and the residual add the un-normed one.
|
||||
*
|
||||
* ============================== THE SIGNED SQUARE ROOT ==============================
|
||||
* ============================== The signed square root ==============================
|
||||
*
|
||||
* `gate.abs().clamp_min(1e-6).sqrt() * gate.sign()` is not a monotone rescaling of the dot product
|
||||
* and it is not decoration: it compresses a score whose magnitude spans orders of magnitude into
|
||||
* something sigmoid can resolve, and it KEEPS THE SIGN, so a stream whose embedding opposes the
|
||||
* residual gets a gate below a half rather than a gate of zero.
|
||||
* `gate.abs().clamp_min(1e-6).sqrt() * gate.sign()` compresses a score spanning orders of magnitude
|
||||
* into a range sigmoid can resolve and keeps the sign, so a stream whose embedding opposes the
|
||||
* residual gets a gate below a half.
|
||||
*
|
||||
* THE CLAMP IS ON THE MAGNITUDE AND THE SIGN IS TAKEN FROM THE ORIGINAL, which matters at exactly
|
||||
* one value: torch's `sign(0)` is 0, so a dot product of exactly zero comes out as zero and not as
|
||||
* sqrt(gate_eps). Clamping first and then re-signing the clamped value would give 1e-3 there.
|
||||
* Zero is not a measure-zero curiosity in a quantised model -- an fp8 activation row can produce
|
||||
* it -- so the order is written out rather than left to the arithmetic.
|
||||
* The clamp applies to the magnitude and the sign comes from the original value. torch's `sign(0)`
|
||||
* is 0, so an exactly-zero dot stays zero rather than becoming sqrt(gate_eps) = 1e-3; an fp8
|
||||
* activation row can produce exact zero.
|
||||
*
|
||||
* ============================== ONE OP AND NOT SIX ==============================
|
||||
* ============================== One op, not six ==============================
|
||||
*
|
||||
* Three grouped norms, a reduction, a scalar nonlinearity and a broadcast multiply would be six
|
||||
* launches over tensors that are all [M, hc*n] -- 10240 wide at this model's shape. Every launch
|
||||
* costs a fixed floor and every one of those ops is memory-bound, so the fused form is the only one
|
||||
* worth writing. `hc_read` is the same argument at the same width.
|
||||
* memory-bound launches over [M, hc*n] tensors (10240 wide at this model's shape). `hc_read` is
|
||||
* fused for the same reason.
|
||||
*/
|
||||
#include "ref_common.h"
|
||||
#include "ref_ops.h"
|
||||
@@ -47,18 +42,8 @@ using namespace ref;
|
||||
|
||||
namespace {
|
||||
|
||||
/* A value narrowed to a tensor's own dtype and read back -- the same helper ref_hc.cpp carries and
|
||||
* for the same reason. The reference model's arithmetic is NOT f32 throughout: torch's grouped norm
|
||||
* returns `.type_as(x)`, so everything downstream of a norm reads a bf16 value. Rounding once, here,
|
||||
* is what lets a fused kernel match this to a tolerance that means something. */
|
||||
inline float narrow(uint32_t dtype, float v) {
|
||||
uint32_t buf[2] = {0, 0};
|
||||
st_dt(dtype, buf, 0, v);
|
||||
return ld_dt(dtype, buf, 0);
|
||||
}
|
||||
|
||||
/* One group's reciprocal RMS: the rsqrt is per `n`-wide stream while the gain below it spans all
|
||||
* hc*n. That asymmetry is the whole of what `group_size` means on Qwen4ExpTextRMSNorm. */
|
||||
/* One group's reciprocal RMS: the rsqrt is per `n`-wide stream while the gain spans all hc*n
|
||||
* (what `group_size` means on Qwen4ExpTextRMSNorm). */
|
||||
inline float group_rsqrt(const RadTensor* x, int64_t base, int64_t stride, int64_t n, float eps) {
|
||||
double acc = 0;
|
||||
for (int64_t d = 0; d < n; ++d) {
|
||||
@@ -72,13 +57,13 @@ inline float group_rsqrt(const RadTensor* x, int64_t base, int64_t stride, int64
|
||||
|
||||
/* PLE's gate.
|
||||
*
|
||||
* k [M][hc*n] key_proj(embedding), UN-normed
|
||||
* q [M][hc*n] the incoming hyper-connection stream, UN-normed
|
||||
* v [M][n] value_proj(embedding) -- ONE row, gated differently per stream
|
||||
* k [M][hc*n] key_proj(embedding), un-normed
|
||||
* q [M][hc*n] the incoming hyper-connection stream, un-normed
|
||||
* v [M][n] value_proj(embedding): one row, gated differently per stream
|
||||
* w_key [hc*n] w
|
||||
* w_query [hc*n] w
|
||||
* w_conv [hc*n] w
|
||||
* gv [M][hc*n] out, UN-normed: what the residual add reads
|
||||
* gv [M][hc*n] out, un-normed: what the residual add reads
|
||||
* gvn [M][hc*n] out, normed: what the convolution reads
|
||||
*/
|
||||
int ref_ple_gate(const RadArgs* a, RadStream) {
|
||||
@@ -95,13 +80,11 @@ int ref_ple_gate(const RadArgs* a, RadStream) {
|
||||
|
||||
const int64_t n = p_int(a, "n", 0);
|
||||
const int64_t hc = p_int(a, "hc", 0);
|
||||
/* `v` is [M, n] and it is the call; `M` is the band. See band_rows in ref_common.h. */
|
||||
/* Rows come from `v` [M, n]; `M` is the band (see band_rows in ref_common.h). */
|
||||
const int64_t M = n > 0 ? band_rows(numel(v) / n, p_int(a, "M", 0)) : 0;
|
||||
const float eps = p_f32(a, "eps", 1e-6f);
|
||||
const float wadd = p_f32(a, "wadd", 0.0f);
|
||||
/* The floor the gate's magnitude is clamped to before the square root. Named rather than
|
||||
* written into the expression: it is a model constant, and a constant a reader cannot see is
|
||||
* one a kernel can disagree with in silence. */
|
||||
/* The floor the gate's magnitude is clamped to before the square root; a model constant. */
|
||||
const float geps = p_f32(a, "gate_eps", 1e-6f);
|
||||
if (M <= 0 || n <= 0 || hc <= 0) return RAD_E_SHAPE;
|
||||
const int64_t hn = hc * n;
|
||||
@@ -113,49 +96,46 @@ int ref_ple_gate(const RadArgs* a, RadStream) {
|
||||
const int64_t wks = laststride(wk), wqs = laststride(wq), wcs = laststride(wc);
|
||||
const int64_t gls = laststride(gv), gnls = laststride(gvn);
|
||||
|
||||
#pragma omp parallel for schedule(static)
|
||||
REF_PAR_FOR(M * hn * 4)
|
||||
for (int64_t m = 0; m < M; ++m) {
|
||||
const int64_t kb = rowoff(k, m, hn), qb = rowoff(q, m, hn), vb = rowoff(v, m, n);
|
||||
const int64_t gb = rowoff(gv, m, hn), nb = rowoff(gvn, m, hn);
|
||||
|
||||
for (int64_t c = 0; c < hc; ++c) {
|
||||
/* The two normed streams, and the dot between them. Neither is materialised: every
|
||||
* element is used exactly once here, and at 2560 wide per stream a buffer would be the
|
||||
* only allocation in the op. */
|
||||
/* The two normed streams and their dot, not materialised since each element is used
|
||||
* once. Each norm is rounded to its input's dtype: upstream's grouped norm returns
|
||||
* `.type_as(x)`. */
|
||||
const float rk = group_rsqrt(k, kb + c * n * kls, kls, n, eps);
|
||||
const float rq = group_rsqrt(q, qb + c * n * qls, qls, n, eps);
|
||||
double dot = 0;
|
||||
for (int64_t d = 0; d < n; ++d) {
|
||||
const int64_t j = c * n + d;
|
||||
const float kk = narrow(k->dtype, ldt(k, kb + j * kls) * rk *
|
||||
(ldt(wk, j * wks) + wadd));
|
||||
const float qq = narrow(q->dtype, ldt(q, qb + j * qls) * rq *
|
||||
(ldt(wq, j * wqs) + wadd));
|
||||
const float kk = round_dt(k->dtype, ldt(k, kb + j * kls) * rk *
|
||||
(ldt(wk, j * wks) + wadd));
|
||||
const float qq = round_dt(q->dtype, ldt(q, qb + j * qls) * rq *
|
||||
(ldt(wq, j * wqs) + wadd));
|
||||
dot += (double)kk * (double)qq;
|
||||
}
|
||||
float s = (float)(dot / std::sqrt((double)n));
|
||||
|
||||
/* THE SIGNED SQUARE ROOT, in the reference's own order: the magnitude is clamped, the
|
||||
* root is taken, and the SIGN COMES FROM THE ORIGINAL. torch's sign(0) is 0, so a dot
|
||||
* of exactly zero stays zero instead of becoming sqrt(gate_eps). */
|
||||
/* Signed square root in upstream's order: clamp the magnitude, take the root, sign
|
||||
* from the original (sign(0) = 0). */
|
||||
const float mag = std::sqrt(std::fabs(s) < geps ? geps : std::fabs(s));
|
||||
s = s > 0.0f ? mag : (s < 0.0f ? -mag : 0.0f);
|
||||
|
||||
const float g = act_sigmoid(s);
|
||||
for (int64_t d = 0; d < n; ++d)
|
||||
stt(gv, gb + (c * n + d) * gls, narrow(gv->dtype, g * ldt(v, vb + d * vls)));
|
||||
stt(gv, gb + (c * n + d) * gls, round_dt(gv->dtype, g * ldt(v, vb + d * vls)));
|
||||
}
|
||||
|
||||
/* And the third norm, over what was just written -- READ BACK, not carried in registers,
|
||||
* because `gv` has been narrowed to its own dtype and the convolution's input is a norm of
|
||||
* THOSE bytes. A fused kernel that normed the f32 it still held would disagree in the last
|
||||
* bits -- the same epilogue-fold rule the GDN out-projection follows. */
|
||||
/* The third norm reads `gv` back after it was narrowed, since the convolution's input is a
|
||||
* norm of the stored bytes (the GDN out-projection's epilogue-fold rule). */
|
||||
for (int64_t c = 0; c < hc; ++c) {
|
||||
const float r = group_rsqrt(gv, gb + c * n * gls, gls, n, eps);
|
||||
for (int64_t d = 0; d < n; ++d) {
|
||||
const int64_t j = c * n + d;
|
||||
stt(gvn, nb + j * gnls,
|
||||
narrow(gvn->dtype, ldt(gv, gb + j * gls) * r * (ldt(wc, j * wcs) + wadd)));
|
||||
round_dt(gvn->dtype, ldt(gv, gb + j * gls) * r * (ldt(wc, j * wcs) + wadd)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -163,65 +143,55 @@ int ref_ple_gate(const RadArgs* a, RadStream) {
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ ple_conv */
|
||||
/* PLE's depthwise convolution: width 4 at DILATION 3, silu AFTER it, and the residual add folded
|
||||
* in. That is the last piece of the layer -- `out = gv + silu(conv(gvn))`.
|
||||
/* PLE's depthwise convolution: width 4 at dilation 3, silu after it, and the residual add folded
|
||||
* in; the layer's last piece, `out = gv + silu(conv(gvn))`.
|
||||
*
|
||||
* ============================== THE DILATION IS THE WHOLE DIFFERENCE ==============================
|
||||
* ============================== Dilation ==============================
|
||||
*
|
||||
* A dilated causal convolution reads `width` taps spaced `dilation` apart:
|
||||
*
|
||||
* y[t] = silu( sum_j w[c][j] * x[t - (width-1-j)*dilation] )
|
||||
*
|
||||
* so at Qwen4-Exp's width 4 and dilation 3 the taps are 9, 6, 3 and 0 timesteps back, and the
|
||||
* history a sequence has to carry is (width-1)*dilation = NINE, not three. Every off-by-one in a
|
||||
* convolution cache is a model that generates fluently from a slightly wrong context, so the
|
||||
* indexing is written once here and mirrors gdn_conv_prep's exactly: state slot `i` holds the
|
||||
* value at relative position `i - state_len`, and a read at `src < 0` becomes `cs[slot][c][src +
|
||||
* state_len]`.
|
||||
* so at Qwen4-Exp's width 4 and dilation 3 the taps are 9, 6, 3 and 0 timesteps back and the
|
||||
* carried history is (width-1)*dilation = 9, not 3. The indexing mirrors gdn_conv_prep's: state
|
||||
* slot `i` holds relative position `i - state_len`, and a read at `src < 0` becomes
|
||||
* `cs[slot][c][src + state_len]`.
|
||||
*
|
||||
* The reference reaches the same place by a different route -- it pads by `state_len`, slices the
|
||||
* last `state_len + seq_len`, and convolves with no padding -- and that pad-and-slice is a no-op
|
||||
* on a warm cache. What it exists for is the COLD one, where the window in front of the first
|
||||
* token must be zeros.
|
||||
* Upstream pads by `state_len`, slices the last `state_len + seq_len` and convolves unpadded; on a
|
||||
* warm cache that is a no-op, and on a cold one the window before the first token is zeros.
|
||||
*
|
||||
* ============================== ONE OP WHERE THE GDN HAS TWO ==============================
|
||||
* ============================== One op where the GDN has two ==============================
|
||||
*
|
||||
* gdn_conv_prep and gdn_conv_update are separate because the work AROUND their convolutions
|
||||
* differs -- one splits a fused projection and takes an l2 norm, the other does not. PLE's
|
||||
* convolution is the same arithmetic at prefill and at decode, so it is one op, and it takes the
|
||||
* union of the two contracts: `has_init` says whether the slot holds a history (prefill's
|
||||
* question) and `num_accepted` shifts the read offset within a deeper window (decode's).
|
||||
* gdn_conv_prep and gdn_conv_update differ in the work around the convolution. PLE's convolution is
|
||||
* the same at prefill and decode, so one op takes both contracts: `has_init` says whether the slot
|
||||
* holds history, and `num_accepted` shifts the read offset within a deeper window.
|
||||
*
|
||||
* `num_accepted` ABSENT MEANS EVERY TOKEN COMMITS, which is what a prefill chunk is: the read is at
|
||||
* offset zero and nothing this step ran can be rejected. gdn_conv_update makes it required and
|
||||
* argues that an optional rollback contract is not a contract; the difference here is that this op
|
||||
* serves prefill too, where there is no previous step to have rejected anything. The state must
|
||||
* still be sized (width-1)*dilation + n_spec by the manager, and a window too shallow for the
|
||||
* offset asked for is refused by shape rather than clamped.
|
||||
* `num_accepted` absent means every token commits (a prefill chunk): the read is at offset zero.
|
||||
* Unlike gdn_conv_update it is optional because this op also serves prefill. The state must still
|
||||
* be sized (width-1)*dilation + n_spec, and a window too shallow for the requested offset is
|
||||
* refused, not clamped.
|
||||
*
|
||||
* ============================== WHAT THE WINDOW HOLDS AFTER A STEP ==============================
|
||||
* ============================== The window after a step ==============================
|
||||
*
|
||||
* The next step reads at ITS `num_accepted - 1`, and what that offset must find is the `hist`
|
||||
* values ending at the last token this step COMMITTED. Which token that is depends on the kind of
|
||||
* step, so the presence of `num_accepted` picks the layout:
|
||||
* The next step reads at its `num_accepted - 1` and must find the `hist` values ending at the last
|
||||
* token this step committed. The presence of `num_accepted` picks the layout:
|
||||
*
|
||||
* PRESENT -- a decode step of `slen` tokens, any of which past the first a verify may reject.
|
||||
* present -- a decode step of `slen` tokens, any past the first rejectable by verify.
|
||||
* The window is rewritten the way gdn_conv_update rewrites its own: slot i holds position
|
||||
* i + 1 of (the window this step read ++ this step's inputs), so the old history shifted down
|
||||
* by one and then token t at slot hist - 1 + t. A next step that kept k of these tokens reads
|
||||
* at k - 1 and finds the history ending at token k - 1. It needs hist - 1 + slen slots, which
|
||||
* is the manager's hist + n_spec for a step of 1 + n_spec tokens.
|
||||
*
|
||||
* ABSENT -- every token commits, so the next step reads at offset zero (its `num_accepted` is
|
||||
* 1) and the window is the last `hist` values of the step, there.
|
||||
* absent -- every token commits, so the next step reads at offset zero (`num_accepted` 1) and
|
||||
* the window is the step's last `hist` values.
|
||||
*
|
||||
* The two agree when the step has one token. Writing the tail at offset zero after a verify of
|
||||
* several would hand a step that accepted only the first of them a history ending at a rejected
|
||||
* draft, and one that accepted two a slot nothing wrote.
|
||||
* The two agree for a one-token step. After a multi-token verify, the absent layout would give a
|
||||
* step that accepted only the first token a history ending at a rejected draft.
|
||||
*/
|
||||
/* THE DEPTHWISE CAUSAL CONVOLUTION AND ITS ROLLING WINDOW, shared by `ple_conv` (dilated, silu,
|
||||
* an optional residual) and `causal_conv1d` (LFM2's short convolution: undilated, no activation,
|
||||
* no residual). One body, so the two ops cannot disagree about the window's layout. */
|
||||
/* The depthwise causal convolution and its rolling window, shared by `ple_conv` (dilated, silu,
|
||||
* optional residual) and `causal_conv1d` (LFM2's short convolution: undilated, no activation, no
|
||||
* residual), so the two agree on the window layout. */
|
||||
struct ConvArgs {
|
||||
const RadTensor *x, *w, *cs, *cu, *resid, *sidx, *hini, *nacc, *y;
|
||||
bool silu;
|
||||
@@ -234,13 +204,12 @@ static int depthwise_causal_conv(const RadArgs* a, const ConvArgs& o) {
|
||||
|
||||
const int64_t n = p_int(a, "n", 0);
|
||||
const int64_t wid = p_int(a, "width", 0);
|
||||
/* `x` is [M, n] and it is the call; `M` is the band. See band_rows in ref_common.h. */
|
||||
/* Rows come from `x` [M, n]; `M` is the band (see band_rows in ref_common.h). */
|
||||
const int64_t M = n > 0 ? band_rows(numel(x) / n, p_int(a, "M", 0)) : 0;
|
||||
const int64_t dil = p_int(a, "dilation", 1);
|
||||
if (M <= 0 || n <= 0 || wid <= 0 || dil <= 0) return RAD_E_SHAPE;
|
||||
const int64_t hist_len = (wid - 1) * dil;
|
||||
/* The read window lives in a stack frame. Nine at this model's shape; the bound is stated so a
|
||||
* caller with a wider one is refused rather than overrunning it. */
|
||||
/* The read window lives in a stack frame (9 at this model's shape); wider is refused. */
|
||||
enum { kHistMax = 64 };
|
||||
if (hist_len > kHistMax) return RAD_E_UNSUPPORTED;
|
||||
|
||||
@@ -252,15 +221,13 @@ static int depthwise_causal_conv(const RadArgs* a, const ConvArgs& o) {
|
||||
const int64_t state_len = cs->rank >= 3 ? cs->shape[cs->rank - 1] : hist_len;
|
||||
if (cs->data && state_len < hist_len) return RAD_E_SHAPE;
|
||||
|
||||
/* Checked for every sequence BEFORE any of them is written: a state too shallow for the offset
|
||||
* asked for, or for the window a decode step leaves behind, is a caller bug whose symptom
|
||||
* would otherwise be a silently truncated history. */
|
||||
/* Check every sequence before writing any: a state too shallow for the requested offset or for
|
||||
* a decode step's resulting window is a caller bug that would silently truncate history. */
|
||||
for (int64_t s = 0; s < N; ++s) {
|
||||
const int64_t off = nacc ? ld_int(nacc->dtype, nacc->data, offlin(nacc, s)) - 1 : 0;
|
||||
if (off < 0) return RAD_E_INVAL;
|
||||
if (off + hist_len > state_len) return RAD_E_SHAPE;
|
||||
const int64_t slen = ld_int(cu->dtype, cu->data, offlin(cu, s + 1)) -
|
||||
ld_int(cu->dtype, cu->data, offlin(cu, s));
|
||||
const int64_t slen = cu_at(cu, s + 1) - cu_at(cu, s);
|
||||
if (nacc && cs->data && hist_len > 0 && slen > 0 && hist_len - 1 + slen > state_len)
|
||||
return RAD_E_SHAPE;
|
||||
}
|
||||
@@ -270,27 +237,31 @@ static int depthwise_causal_conv(const RadArgs* a, const ConvArgs& o) {
|
||||
const int64_t rrow = resid ? rowoff(resid, 1, n) : 0;
|
||||
const int64_t rcol = resid ? laststride(resid) : 0;
|
||||
const int64_t wrow = rowoff(w, 1, wid), wcol = laststride(w);
|
||||
/* [n_slots, n, state_len], tight, the layout gdn's conv state already uses. */
|
||||
const int64_t cs_slot = n * state_len, cs_ch = state_len;
|
||||
/* [n_slots, n, state_len], as gdn's conv state; a flattened one is read as the tight layout. */
|
||||
const bool cs3 = cs->rank >= 3;
|
||||
const int64_t cs_slot = cs3 ? cs->stride[cs->rank - 3] : n * state_len;
|
||||
const int64_t cs_ch = cs3 ? cs->stride[cs->rank - 2] : state_len;
|
||||
const int64_t cs_t = cs3 ? cs->stride[cs->rank - 1] : 1;
|
||||
|
||||
#pragma omp parallel for schedule(static)
|
||||
REF_PAR_FOR(M * n * wid)
|
||||
for (int64_t sc = 0; sc < N * n; ++sc) {
|
||||
const int64_t s = sc / n, c = sc % n;
|
||||
const int64_t bos = ld_int(cu->dtype, cu->data, offlin(cu, s));
|
||||
const int64_t slen = ld_int(cu->dtype, cu->data, offlin(cu, s + 1)) - bos;
|
||||
const int64_t bos = cu_at(cu, s);
|
||||
const int64_t slen = cu_at(cu, s + 1) - bos;
|
||||
if (slen <= 0) continue;
|
||||
const int64_t slot = sidx ? ld_int(sidx->dtype, sidx->data, offlin(sidx, s)) : s;
|
||||
if (slot < 0) continue;
|
||||
/* A zero `has_init` means the slot holds no history: the window in front of the first
|
||||
* token is ZEROS and not whatever the slot last held. Without it a fresh sequence landing
|
||||
* on a recycled slot convolves the previous sequence's tail into its first nine tokens --
|
||||
* fluent output with the wrong prefix. */
|
||||
/* A zero `has_init` means the slot holds no history: the window before the first token
|
||||
* is zeros, not a recycled slot's previous contents. */
|
||||
const bool warm = !hini || ld_int(hini->dtype, hini->data, offlin(hini, s)) != 0;
|
||||
const int64_t off = nacc ? ld_int(nacc->dtype, nacc->data, offlin(nacc, s)) - 1 : 0;
|
||||
const int64_t base = slot * cs_slot + c * cs_ch;
|
||||
|
||||
/* Read whole before anything is written back, since a step longer than the window
|
||||
* overwrites history it still reads. */
|
||||
float hist[kHistMax];
|
||||
for (int64_t i = 0; i < hist_len; ++i)
|
||||
hist[i] = (warm && cs->data) ? ldt(cs, slot * cs_slot + c * cs_ch + off + i) : 0.0f;
|
||||
hist[i] = (warm && cs->data) ? ldt(cs, base + (off + i) * cs_t) : 0.0f;
|
||||
|
||||
for (int64_t t = 0; t < slen; ++t) {
|
||||
float acc = 0.0f;
|
||||
@@ -303,50 +274,27 @@ static int depthwise_causal_conv(const RadArgs* a, const ConvArgs& o) {
|
||||
const float r = resid ? ldt(resid, (bos + t) * rrow + c * rcol) : 0.0f;
|
||||
stt(y, (bos + t) * yrow + c * ycol, r + (o.silu ? act_silu(acc) : acc));
|
||||
}
|
||||
}
|
||||
|
||||
/* The window this step leaves behind, written after every read is done -- a sequence longer
|
||||
* than the window overwrites history it was still reading otherwise. The layout is the one
|
||||
* the header describes: shifted by one with every input of the step behind it when
|
||||
* `num_accepted` is present, the last `hist` values at offset zero when it is not. */
|
||||
if (cs->data && hist_len > 0) {
|
||||
#pragma omp parallel for schedule(static)
|
||||
for (int64_t sc = 0; sc < N * n; ++sc) {
|
||||
const int64_t s = sc / n, c = sc % n;
|
||||
const int64_t bos = ld_int(cu->dtype, cu->data, offlin(cu, s));
|
||||
const int64_t slen = ld_int(cu->dtype, cu->data, offlin(cu, s + 1)) - bos;
|
||||
if (slen <= 0) continue;
|
||||
const int64_t slot = sidx ? ld_int(sidx->dtype, sidx->data, offlin(sidx, s)) : s;
|
||||
if (slot < 0) continue;
|
||||
const bool warm = !hini || ld_int(hini->dtype, hini->data, offlin(hini, s)) != 0;
|
||||
const int64_t off = nacc ? ld_int(nacc->dtype, nacc->data, offlin(nacc, s)) - 1 : 0;
|
||||
const int64_t base = slot * cs_slot + c * cs_ch;
|
||||
|
||||
float win[kHistMax];
|
||||
for (int64_t i = 0; i < hist_len; ++i)
|
||||
win[i] = warm ? ldt(cs, base + off + i) : 0.0f;
|
||||
|
||||
if (nacc) {
|
||||
for (int64_t i = 0; i + 1 < hist_len; ++i) stt(cs, base + i, win[i + 1]);
|
||||
for (int64_t t = 0; t < slen; ++t)
|
||||
stt(cs, base + hist_len - 1 + t, ldt(x, (bos + t) * xrow + c * xcol));
|
||||
} else {
|
||||
float keep[kHistMax];
|
||||
for (int64_t i = 0; i < hist_len; ++i) {
|
||||
const int64_t src = slen - hist_len + i;
|
||||
keep[i] = src >= 0 ? ldt(x, (bos + src) * xrow + c * xcol)
|
||||
: win[src + hist_len];
|
||||
}
|
||||
for (int64_t i = 0; i < hist_len; ++i) stt(cs, base + i, keep[i]);
|
||||
/* The window after the step, in the layout described above ple_conv. */
|
||||
if (!cs->data || hist_len == 0) continue;
|
||||
if (nacc) {
|
||||
for (int64_t i = 0; i + 1 < hist_len; ++i) stt(cs, base + i * cs_t, hist[i + 1]);
|
||||
for (int64_t t = 0; t < slen; ++t)
|
||||
stt(cs, base + (hist_len - 1 + t) * cs_t, ldt(x, (bos + t) * xrow + c * xcol));
|
||||
} else {
|
||||
float keep[kHistMax];
|
||||
for (int64_t i = 0; i < hist_len; ++i) {
|
||||
const int64_t src = slen - hist_len + i;
|
||||
keep[i] = src >= 0 ? ldt(x, (bos + src) * xrow + c * xcol) : hist[src + hist_len];
|
||||
}
|
||||
for (int64_t i = 0; i < hist_len; ++i) stt(cs, base + i * cs_t, keep[i]);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int ref_ple_conv(const RadArgs* a, RadStream) {
|
||||
/* NINE positions, four of them optional, so the count is checked and the optionals are tested
|
||||
* one by one -- `have()` would refuse the op for an absent operand that is allowed to be. */
|
||||
/* Nine positions, four optional; `have()` would refuse an absent optional operand. */
|
||||
if (a->n_t < 9) return RAD_E_INVAL;
|
||||
return depthwise_causal_conv(a, { t_in(a, 0), t_in(a, 1), t_in(a, 2), t_in(a, 3), t_in(a, 4),
|
||||
t_in(a, 5), t_in(a, 6), t_in(a, 7), t_in(a, 8), true });
|
||||
|
||||
Reference in new issue
Block a user