Archived
ops: ssd_update and ple_conv_bias, Mamba-2's scan and convolution
ssd_update is Mamba-2's selective state update, a token at a time, with kda_update's rollback contract: per_candidate, or the anchor form (one committed state and a scratch of per-token records). libref, libavx and a libr4d kernel at a 128-wide state (r4d_ssd.hip, r4d_kda.hip's register layout). ple_conv_bias is ple_conv with a per-channel bias inside the silu, Mamba-2's conv1d, an op of its own so ple_conv's callers keep their operand list. ref_test, avx_check_hand and selftest cases; docs/OPS.md.
This commit is contained in:
1 parent
3b278dcec3
commit
63416510a0
21 files changed
+1362
-11
No files matched your search
@@ -52,6 +52,7 @@ set(AVX_KERNEL_SRC
|
||||
avx_qsa.cpp
|
||||
avx_moe.cpp
|
||||
avx_gdn.cpp
|
||||
avx_ssd.cpp
|
||||
avx_sample.cpp
|
||||
avx_misc.cpp
|
||||
avx_gen.cpp
|
||||
|
||||
@@ -179,6 +179,10 @@ std::vector<Case> build_cases() {
|
||||
add("kda_update", { HI("M", 3), HI("heads", 2), HI("head_k", 32), HI("head_v", 32),
|
||||
HF("lower_bound", -5), HF("scale", 0.1767766952966369),
|
||||
HF("l2_eps", 1e-6), HS("mode", mode), HS("dtype", dt) });
|
||||
/* Nemotron-H's per-rank shape at four ranks is 32 heads x 64 over 2 groups of 128. */
|
||||
for (const char* mode : { "prefill", "verify" })
|
||||
add("ssd_update", { HI("M", 3), HI("heads", 4), HI("head_dim", 16), HI("d_state", 32),
|
||||
HI("groups", 2), HS("mode", mode), HS("dtype", dt) });
|
||||
add("hc_write", { HI("M", 5), HI("n", 128), HI("hc", 4), HS("dtype", dt) });
|
||||
add("hc_project",{ HI("M", 5), HI("n", 128), HI("hc", 4), HF("scale", 1.0),
|
||||
HS("dtype", dt) });
|
||||
|
||||
@@ -630,6 +630,46 @@ void shape_cases(std::vector<Hand>& hs) {
|
||||
.ps("state_form", "anchor");
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
/* Nemotron-H's Mamba-2 under the same two-copy state: a scratch holding three records of
|
||||
* 1 + head_dim + d_state floats (decay, u, B) behind the count. */
|
||||
for (const char* mode : { "prefill", "verify" })
|
||||
for (int accepted : { 1, 2, 3 })
|
||||
for (int cold : { 0, 1 }) {
|
||||
constexpr int64_t P = 16, N = 32, R = 1 + P + N;
|
||||
Hand h("SSD anchor accepted-prefix replay", "ssd_update", 0.01);
|
||||
h.dense(RAD_BF16, { 2, 1, P }, hfill(221, 0.25f))
|
||||
.dense(RAD_BF16, { 2, 1, N }, hfill(222, 0.25f))
|
||||
.dense(RAD_BF16, { 2, 1, N }, hfill(223, 0.25f))
|
||||
.dense(RAD_BF16, { 2, 1 }, hfill(224, 0.25f))
|
||||
.dense(RAD_F32, { 1 }, [](int64_t) { return 0.0f; })
|
||||
.dense(RAD_F32, { 1 }, [](int64_t) { return -1.0f; })
|
||||
.dense(RAD_F32, { 1 }, [](int64_t) { return 0.5f; })
|
||||
.dense(
|
||||
RAD_F32, { 2, 1, P, N },
|
||||
[](int64_t i) {
|
||||
if (i < P * N) return 0.125f;
|
||||
i -= P * N;
|
||||
if (!i) return 3.0f;
|
||||
if (i > 3 * R) return 0.0f;
|
||||
const int64_t at = (i - 1) % R;
|
||||
return at == 0 ? 0.75f : at <= P ? 0.01f : 0.125f;
|
||||
},
|
||||
true)
|
||||
.dense(RAD_I32, { 1, 6 }, [](int64_t i) { return i < 3 ? 0.0f : 1.0f; })
|
||||
.idx({ 0, 2 })
|
||||
.idx({ accepted })
|
||||
.idx({ cold ? 0 : 1 })
|
||||
.dense(RAD_BF16, { 2, 1, P }, hfill(225), true)
|
||||
.pi("M", 2)
|
||||
.pi("heads", 1)
|
||||
.pi("head_dim", P)
|
||||
.pi("d_state", N)
|
||||
.pi("groups", 1)
|
||||
.ps("mode", mode)
|
||||
.ps("dtype", "bf16")
|
||||
.ps("state_form", "anchor");
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
for (const char* mode : { "absorb", "expand" }) {
|
||||
const bool absorb = !std::strcmp(mode, "absorb");
|
||||
const int64_t in = absorb ? 17 : 31, out = absorb ? 31 : 17;
|
||||
|
||||
+2
-2
@@ -73,7 +73,7 @@ int avx_par_force(int threads);
|
||||
X(hc_read) X(hc_write) X(hc_project) X(mtp_enter) \
|
||||
X(layernorm) X(mhc_pre) X(mhc_post) X(mhc_contract) \
|
||||
X(kda_update) X(mhc_pre_partial) X(mhc_pre_finish) \
|
||||
X(swiglu_clamped) \
|
||||
X(swiglu_clamped) X(ssd_update) \
|
||||
X(swiglu_step) \
|
||||
X(router_sigmoid_topk) \
|
||||
X(router_sigmoid_group_topk) \
|
||||
@@ -88,7 +88,7 @@ int avx_par_force(int threads);
|
||||
X(lili_cand) X(lili_attn) X(lili_factor) X(lili_walk) \
|
||||
X(mul) X(silu_mul) X(gelu_mul) X(gelu) \
|
||||
X(silu) X(sigmoid) X(logit_softcap) X(gather_rows) \
|
||||
X(scatter_rows) X(ngram_ids) \
|
||||
X(scatter_rows) X(ngram_ids) X(ple_conv_bias) \
|
||||
X(ple_gate) X(ple_conv) X(cast) X(softmax) \
|
||||
X(scale_rows) X(scale) X(fill) X(act_mul) \
|
||||
X(seg_mean_bf16_sum) \
|
||||
|
||||
+15
-2
@@ -157,11 +157,12 @@ AVX_KERNEL(ple_gate) {
|
||||
struct ConvOps {
|
||||
const RadTensor *x, *w, *cs, *cu, *resid, *sidx, *hini, *nacc, *y;
|
||||
bool silu;
|
||||
const RadTensor* b; /* per-channel bias inside the activation (Mamba-2's conv1d), or null */
|
||||
};
|
||||
|
||||
static int depthwise_causal_conv(const RadArgs* a, const ConvOps& o) {
|
||||
const RadTensor *x = o.x, *w = o.w, *cs = o.cs, *cu = o.cu, *resid = o.resid;
|
||||
const RadTensor *sidx = o.sidx, *hini = o.hini, *nacc = o.nacc, *y = o.y;
|
||||
const RadTensor *sidx = o.sidx, *hini = o.hini, *nacc = o.nacc, *y = o.y, *b = o.b;
|
||||
if (!x || !w || !cs || !cu || !y) return RAD_E_INVAL;
|
||||
|
||||
const int64_t n = p_int(a, "n", 0);
|
||||
@@ -180,6 +181,7 @@ static int depthwise_causal_conv(const RadArgs* a, const ConvOps& o) {
|
||||
if (N <= 0) return RAD_E_SHAPE;
|
||||
if (numel(x) < M * n || numel(y) < M * n || numel(w) < n * wid) return RAD_E_SHAPE;
|
||||
if (resid && numel(resid) < M * n) return RAD_E_SHAPE;
|
||||
if (b && numel(b) != n) return RAD_E_SHAPE;
|
||||
|
||||
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;
|
||||
@@ -222,9 +224,10 @@ static int depthwise_causal_conv(const RadArgs* a, const ConvOps& o) {
|
||||
* on the stack, so `width` needs no bound of its own. */
|
||||
float* tap = scratch_f32((size_t)wid);
|
||||
for (int64_t j = 0; j < wid; ++j) tap[j] = ldt(w, c * wrow + j * wcol);
|
||||
const float bias = b ? ldt(b, c * laststride(b)) : 0.0f;
|
||||
|
||||
for (int64_t t = 0; t < slen; ++t) {
|
||||
float acc = 0.0f;
|
||||
float acc = bias;
|
||||
for (int64_t j = 0; j < wid; ++j) {
|
||||
const int64_t src = t - hist_len + j * dil;
|
||||
const float vv = src >= 0 ? ldt(x, (bos + src) * xrow + c * xcol)
|
||||
@@ -277,6 +280,16 @@ AVX_KERNEL(ple_conv) {
|
||||
rad_arg_in(a, 6), rad_arg_in(a, 7), rad_arg_in(a, 8), true });
|
||||
}
|
||||
|
||||
/* ple_conv's operands with `b`, a per-channel bias inside the silu, before `y`: Mamba-2's
|
||||
* conv1d at dilation one. */
|
||||
AVX_KERNEL(ple_conv_bias) {
|
||||
if (a->n_t < 10 || !rad_arg_in(a, 8)) return RAD_E_INVAL;
|
||||
return depthwise_causal_conv(a, { rad_arg_in(a, 0), rad_arg_in(a, 1), rad_arg_in(a, 2),
|
||||
rad_arg_in(a, 3), rad_arg_in(a, 4), rad_arg_in(a, 5),
|
||||
rad_arg_in(a, 6), rad_arg_in(a, 7), rad_arg_in(a, 9), true,
|
||||
rad_arg_in(a, 8) });
|
||||
}
|
||||
|
||||
/* ple_conv's operands without `resid`, dilation 1, no activation. */
|
||||
AVX_KERNEL(causal_conv1d) {
|
||||
if (a->n_t < 8) return RAD_E_INVAL;
|
||||
|
||||
@@ -87,6 +87,11 @@ static const RadParamSpec pKdaUpdate[] = { P_INT("M"), P_INT("heads"), P_INT("he
|
||||
static const RadOperandSpec oKdaUpdate[] = { OPD("q"), OPD("k"), OPD("v"), OPD("a"), OPD("b"),
|
||||
WGT("A_log"), WGT("dt_bias"), INOUT("state"), OPD("state_idx"), OPD("cu"),
|
||||
OPD_O("num_accepted"), OPD_O("has_init"), OUT("o") };
|
||||
static const RadParamSpec pSsdUpdate[] = { P_INT("M"), P_INT("heads"), P_INT("head_dim"),
|
||||
P_INT("d_state"), P_INT("groups"), P_STR("mode"), P_STR("dtype"), P_STRO("state_form") };
|
||||
static const RadOperandSpec oSsdUpdate[] = { OPD("x"), OPD("B"), OPD("C"), OPD("dt"),
|
||||
WGT("A_log"), WGT("dt_bias"), WGT("D"), INOUT("state"), OPD("state_idx"), OPD("cu"),
|
||||
OPD_O("num_accepted"), OPD_O("has_init"), OUT("o") };
|
||||
|
||||
static const RadParamSpec pNgramIds[] = { P_INT("M"), P_INT("heads"), P_INT("ngram"),
|
||||
P_INT("eos") };
|
||||
@@ -106,6 +111,11 @@ static const RadParamSpec pPleConv[] = { P_INT("M"), P_INT("n"), P_INT("width"
|
||||
static const RadOperandSpec oPleConv[] = { OPD("x"), WGT("w"), INOUT("conv_state"), OPD("cu"),
|
||||
OPD_O("resid"), OPD_O("cache_idx"), OPD_O("has_init"),
|
||||
OPD_O("num_accepted"), OUT("y") };
|
||||
/* ple_conv's operands with `b`, a per-channel bias inside the silu, before `y`. */
|
||||
static const RadOperandSpec oPleConvBias[] = { OPD("x"), WGT("w"), INOUT("conv_state"), OPD("cu"),
|
||||
OPD_O("resid"), OPD_O("cache_idx"),
|
||||
OPD_O("has_init"), OPD_O("num_accepted"), WGT("b"),
|
||||
OUT("y") };
|
||||
static const RadParamSpec pCausalConv[] = { P_INT("M"), P_INT("n"), P_INT("width"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oCausalConv[] = { OPD("x"), WGT("w"), INOUT("conv_state"), OPD("cu"),
|
||||
@@ -977,6 +987,7 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "mhc_post", ARR(pHcWrite), ARR(oMhcPost), "Source-to-destination manifold residual update." },
|
||||
{ "mhc_contract", ARR(pHcWrite), ARR(oMhcContract), "Mean over manifold residual streams." },
|
||||
{ "kda_update", ARR(pKdaUpdate), ARR(oKdaUpdate), "Bounded per-channel KDA with candidate-state rollback." },
|
||||
{ "ssd_update", ARR(pSsdUpdate), ARR(oSsdUpdate), "Mamba-2 scalar-decay state update with candidate-state rollback, per_candidate or anchor." },
|
||||
{ "hc_read", ARR(pHcRead), ARR(oHcRead),
|
||||
"The read half of a gated residual: grouped norm, low-rank gate, stream mix, and optionally the "
|
||||
"block-scaled fp8 form of x and its Hadamard-rotated twin. See docs/OPS.md for the three facts "
|
||||
@@ -1077,6 +1088,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
"PLE gate: three grouped norms, a dot, a signed sqrt." },
|
||||
{ "ple_conv", ARR(pPleConv), ARR(oPleConv),
|
||||
"PLE's dilated depthwise conv, silu and residual add." },
|
||||
{ "ple_conv_bias", ARR(pPleConv), ARR(oPleConvBias),
|
||||
"ple_conv with a per-channel bias inside the silu (Mamba-2's conv1d)." },
|
||||
{ "causal_conv1d", ARR(pCausalConv), ARR(oCausalConv),
|
||||
"depthwise causal conv with ple_conv's window, no activation and no residual (LFM2)." },
|
||||
{ "ngram_ids", ARR(pNgramIds), ARR(oNgramIds), "hashed n-gram row ids for PLE." },
|
||||
@@ -1646,6 +1659,7 @@ SIMPLE("avx_mhc_pre_finish", "mhc_pre_finish", "norm", "manifold gates from proj
|
||||
SIMPLE("avx_mhc_post", "mhc_post", "norm", "manifold residual update", DT_FLOAT, L(mhc_post)),
|
||||
SIMPLE("avx_mhc_contract", "mhc_contract", "norm", "mean of residual streams", DT_FLOAT, L(mhc_contract)),
|
||||
SIMPLE("avx_kda_update", "kda_update", "gdn", "bounded KDA and candidate-state rollback", DT_FLOAT, L(kda_update)),
|
||||
SIMPLE("avx_ssd_update", "ssd_update", "gdn", "Mamba-2 recurrence and candidate-state rollback", DT_FLOAT, L(ssd_update)),
|
||||
ROWL("avx_hc_read", "hc_read", "norm",
|
||||
"gated residual read: grouped norm, low-rank gate, stream mix",
|
||||
DT_FLOAT ", or mixing matrices of E4M3 / int8 codes with a scale plane, joined", L(hc_read),
|
||||
@@ -1747,6 +1761,9 @@ SIMPLE("avx_ple_gate", "ple_gate", "norm", "PLE gate: three grouped norms, a dot
|
||||
DT_FLOAT, L(ple_gate)),
|
||||
SIMPLE("avx_ple_conv", "ple_conv", "norm", "PLE's dilated depthwise conv, silu and residual add",
|
||||
DT_FLOAT, L(ple_conv)),
|
||||
SIMPLE("avx_ple_conv_bias", "ple_conv_bias", "norm",
|
||||
"ple_conv with a per-channel bias inside the silu (Mamba-2's conv1d)", DT_FLOAT,
|
||||
L(ple_conv_bias)),
|
||||
SIMPLE("avx_causal_conv1d", "causal_conv1d", "conv", "depthwise causal conv, no activation",
|
||||
DT_FLOAT, L(causal_conv1d)),
|
||||
SIMPLE("avx_cast", "cast", "elem", "dtype conversion", DT_ANY, L(cast)),
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
/* avx_ssd.cpp -- the Mamba-2 selective state update (ssd_update), libref/ref_ssd.cpp's arithmetic
|
||||
* with the d_state reduction in this ISA level's vectors, in both state forms.
|
||||
*
|
||||
* A token widens its group's B and C rows once; each head then decays and updates its
|
||||
* [head_dim, d_state] state row by row and reads the row against C in the same pass. The anchor
|
||||
* form's replay applies a record with the window's own update (row_axpy is row_update without the
|
||||
* read), so the committed state is this library's per-candidate state bit for bit. */
|
||||
#include "avx_vec.h"
|
||||
#include "avx_common.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
using namespace avx;
|
||||
|
||||
namespace {
|
||||
bool headed(const RadTensor& t, int64_t tokens, int64_t heads, int64_t width) {
|
||||
return t.data && t.shape[0] == tokens &&
|
||||
((t.rank == 3 && t.shape[1] == heads && t.shape[2] == width) ||
|
||||
(t.rank == 2 && t.shape[1] == heads * width));
|
||||
}
|
||||
int64_t off(const RadTensor& t, int64_t token, int64_t head, int64_t d, int64_t width) {
|
||||
return token * t.stride[0] + head * (t.rank == 3 ? t.stride[1] : width * t.stride[1]) +
|
||||
d * laststride(&t);
|
||||
}
|
||||
float softplus(float v) { return v > 20.0f ? v : std::log1p(std::exp(v)); }
|
||||
|
||||
/* s = s * decay + u * b over one state row, as row_update does it, without the read. */
|
||||
void row_axpy(float* s, const float* b, float decay, float u, int64_t n) {
|
||||
const vf vd = vf_set1(decay), vu = vf_set1(u);
|
||||
int64_t d = 0;
|
||||
for (; d + VF_N <= n; d += VF_N)
|
||||
vf_storeu(s + d, vf_fma(vf_loadu(s + d), vd, vf_mul(vu, vf_loadu(b + d))));
|
||||
for (; d < n; ++d) s[d] = s[d] * decay + u * b[d];
|
||||
}
|
||||
int64_t anchor_tokens(int64_t P, int64_t Nst) {
|
||||
const int64_t fit = (P * Nst - 1) / (1 + P + Nst);
|
||||
return fit < 32 ? fit : 32;
|
||||
}
|
||||
|
||||
/* s = s * decay + u * b over one state row, and the row's dot with c. */
|
||||
float row_update(float* s, const float* b, const float* c, float decay, float u, int64_t n) {
|
||||
const vf vd = vf_set1(decay), vu = vf_set1(u);
|
||||
vf acc = vf_zero();
|
||||
int64_t d = 0;
|
||||
for (; d + VF_N <= n; d += VF_N) {
|
||||
const vf r = vf_fma(vf_loadu(s + d), vd, vf_mul(vu, vf_loadu(b + d)));
|
||||
vf_storeu(s + d, r);
|
||||
acc = vf_fma(r, vf_loadu(c + d), acc);
|
||||
}
|
||||
float result = vf_hsum(acc);
|
||||
for (; d < n; ++d) {
|
||||
s[d] = s[d] * decay + u * b[d];
|
||||
result += s[d] * c[d];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AVX_KERNEL(ssd_update) {
|
||||
if (!a || a->n_t < 13) return RAD_E_INVAL;
|
||||
for (int i = 0; i < 10; ++i)
|
||||
if (!rad_arg_in(a, i)) return RAD_E_INVAL;
|
||||
if (!rad_arg_in(a, 12)) return RAD_E_INVAL;
|
||||
const int64_t H = p_int(a, "heads", 0), P = p_int(a, "head_dim", 0);
|
||||
const int64_t Nst = p_int(a, "d_state", 0), G = p_int(a, "groups", 0);
|
||||
const char* mode = p_str(a, "mode", "");
|
||||
const bool verify = !std::strcmp(mode, "verify");
|
||||
if (!verify && std::strcmp(mode, "prefill")) return RAD_E_INVAL;
|
||||
const char* form = p_str(a, "state_form", "per_candidate");
|
||||
const bool anchor = !std::strcmp(form, "anchor");
|
||||
if (!anchor && std::strcmp(form, "per_candidate")) return RAD_E_INVAL;
|
||||
if (H < 1 || P < 1 || Nst < 1 || G < 1 || H % G) return RAD_E_SHAPE;
|
||||
const int64_t rec = 1 + P + Nst, deepest = anchor_tokens(P, Nst);
|
||||
if (anchor && deepest < 1) return RAD_E_SHAPE;
|
||||
const auto &x = a->t[0], &B = a->t[1], &C = a->t[2], &dt = a->t[3];
|
||||
const auto &al = a->t[4], &dtb = a->t[5], &dskip = a->t[6], &s = a->t[7];
|
||||
const auto &idx = a->t[8], &cu = a->t[9], &out = a->t[12];
|
||||
const auto* accepted = rad_arg_in(a, 10);
|
||||
const auto* init = rad_arg_in(a, 11);
|
||||
if (x.rank < 2 || cu.rank != 1 || cu.dtype != RAD_I32 || cu.shape[0] < 2) return RAD_E_SHAPE;
|
||||
const int64_t T = x.shape[0], N = cu.shape[0] - 1, slots = s.shape[0];
|
||||
if (T < 1) return RAD_E_SHAPE;
|
||||
if (!headed(x, T, H, P) || !headed(B, T, G, Nst) || !headed(C, T, G, Nst) ||
|
||||
!headed(dt, T, H, 1) || !headed(out, T, H, P) || numel(&al) != H || numel(&dtb) != H ||
|
||||
numel(&dskip) != H || s.rank != 4 || s.dtype != RAD_F32 || s.shape[1] != H ||
|
||||
s.shape[2] != P || s.shape[3] != Nst || idx.rank != 2 || idx.dtype != RAD_I32 ||
|
||||
idx.shape[0] < N || idx.shape[1] < 1 ||
|
||||
(accepted && (accepted->dtype != RAD_I32 || numel(accepted) < N)) ||
|
||||
(init && (init->dtype != RAD_I32 || numel(init) < N)))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t width = idx.shape[1];
|
||||
for (int64_t seq = 0; seq < N; ++seq) {
|
||||
const int64_t begin = ldi(&cu, seq * cu.stride[0]);
|
||||
const int64_t end = ldi(&cu, (seq + 1) * cu.stride[0]);
|
||||
const int64_t na = accepted ? ldi(accepted, offlin(accepted, seq)) : 1;
|
||||
const int64_t most = anchor ? deepest : width;
|
||||
if (begin < 0 || end < begin || end > T || na < 1 || na > most ||
|
||||
(verify && end - begin > most) || (anchor && width < 2))
|
||||
return RAD_E_SHAPE;
|
||||
for (int64_t j = 0; j < width; ++j)
|
||||
if (ldi(&idx, seq * idx.stride[0] + j * idx.stride[1]) >= slots) return RAD_E_SHAPE;
|
||||
if (anchor) {
|
||||
const int64_t read = ldi(&idx, seq * idx.stride[0]);
|
||||
const int64_t scratch = ldi(&idx, seq * idx.stride[0] + (width / 2) * idx.stride[1]);
|
||||
if (read >= 0 && (scratch < 0 || scratch == read)) return RAD_E_SHAPE;
|
||||
if (verify && read >= 0 && (!init || ldi(init, offlin(init, seq))))
|
||||
for (int64_t h = 0; h < H; ++h) {
|
||||
const float count = ldt(&s, scratch * s.stride[0] + h * s.stride[1]);
|
||||
if (!std::isfinite(count) || count < 0 || count > (float)deepest ||
|
||||
count != std::floor(count) || (count > 0 && na > count))
|
||||
return RAD_E_SHAPE;
|
||||
}
|
||||
}
|
||||
}
|
||||
const int64_t per_group = H / G;
|
||||
AVX_PARALLEL_FOR_IF(N * H * P * Nst >= AVX_PAR_MIN)
|
||||
for (int64_t sh = 0; sh < N * H; ++sh) {
|
||||
const int64_t seq = sh / H, head = sh % H, grp = head / per_group;
|
||||
const int64_t begin = ldi(&cu, seq * cu.stride[0]);
|
||||
const int64_t end = ldi(&cu, (seq + 1) * cu.stride[0]);
|
||||
if (begin == end) continue;
|
||||
const int64_t na = accepted ? ldi(accepted, offlin(accepted, seq)) : 1;
|
||||
const int64_t read =
|
||||
ldi(&idx, seq * idx.stride[0] + (verify && !anchor ? na - 1 : 0) * idx.stride[1]);
|
||||
if (read < 0) {
|
||||
for (int64_t t = begin; t < end; ++t)
|
||||
for (int64_t e = 0; e < P; ++e) stt(&out, off(out, t, head, e, P), 0);
|
||||
continue;
|
||||
}
|
||||
const bool initialized = !init || ldi(init, offlin(init, seq)) != 0;
|
||||
std::vector<float> S((size_t)(P * Nst)), b((size_t)Nst), c((size_t)Nst);
|
||||
const int64_t sb = read * s.stride[0] + head * s.stride[1];
|
||||
for (int64_t e = 0; e < P; ++e)
|
||||
for (int64_t d = 0; d < Nst; ++d)
|
||||
S[e * Nst + d] = initialized ? ldt(&s, sb + e * s.stride[2] + d * s.stride[3]) : 0;
|
||||
const int64_t scratch =
|
||||
anchor ? ldi(&idx, seq * idx.stride[0] + (width / 2) * idx.stride[1]) : 0;
|
||||
auto factor_at = [&](int64_t i) {
|
||||
return scratch * s.stride[0] + head * s.stride[1] + (i / Nst) * s.stride[2] +
|
||||
(i % Nst) * s.stride[3];
|
||||
};
|
||||
if (anchor && verify) {
|
||||
/* The accepted prefix of the previous window, onto the committed state, published. */
|
||||
const int64_t replay = initialized ? (int64_t)ldt(&s, factor_at(0)) : 0;
|
||||
for (int64_t t = 0; t < (replay ? na : 0); ++t) {
|
||||
const int64_t f = 1 + t * rec;
|
||||
const float decay = ldt(&s, factor_at(f));
|
||||
for (int64_t d = 0; d < Nst; ++d) b[(size_t)d] = ldt(&s, factor_at(f + 1 + P + d));
|
||||
for (int64_t e = 0; e < P; ++e)
|
||||
row_axpy(&S[e * Nst], b.data(), decay, ldt(&s, factor_at(f + 1 + e)), Nst);
|
||||
}
|
||||
for (int64_t e = 0; e < P; ++e)
|
||||
for (int64_t d = 0; d < Nst; ++d)
|
||||
stt(&s, sb + e * s.stride[2] + d * s.stride[3], S[e * Nst + d]);
|
||||
}
|
||||
const float A = -std::exp(ldt(&al, head * laststride(&al)));
|
||||
const float bias = ldt(&dtb, head * laststride(&dtb));
|
||||
const float skip = ldt(&dskip, head * laststride(&dskip));
|
||||
for (int64_t t = begin; t < end; ++t) {
|
||||
const float step = softplus(ldt(&dt, off(dt, t, head, 0, 1)) + bias);
|
||||
const float decay = std::exp(step * A);
|
||||
for (int64_t d = 0; d < Nst; ++d) {
|
||||
b[d] = ldt(&B, off(B, t, grp, d, Nst));
|
||||
c[d] = ldt(&C, off(C, t, grp, d, Nst));
|
||||
}
|
||||
const int64_t f = 1 + (t - begin) * rec;
|
||||
if (anchor && verify) {
|
||||
stt(&s, factor_at(f), decay);
|
||||
for (int64_t d = 0; d < Nst; ++d) stt(&s, factor_at(f + 1 + P + d), b[d]);
|
||||
}
|
||||
for (int64_t e = 0; e < P; ++e) {
|
||||
const float xe = ldt(&x, off(x, t, head, e, P));
|
||||
const float u = step * xe;
|
||||
if (anchor && verify) stt(&s, factor_at(f + 1 + e), u);
|
||||
const float r = row_update(&S[e * Nst], b.data(), c.data(), decay, u, Nst);
|
||||
stt(&out, off(out, t, head, e, P), r + skip * xe);
|
||||
}
|
||||
if ((verify && !anchor) || (!verify && t + 1 == end)) {
|
||||
const int64_t write =
|
||||
ldi(&idx, seq * idx.stride[0] + (verify ? t - begin : 0) * idx.stride[1]);
|
||||
if (write >= 0)
|
||||
for (int64_t e = 0; e < P; ++e)
|
||||
for (int64_t d = 0; d < Nst; ++d)
|
||||
stt(&s, write * s.stride[0] + head * s.stride[1] + e * s.stride[2] +
|
||||
d * s.stride[3],
|
||||
S[e * Nst + d]);
|
||||
}
|
||||
}
|
||||
if (anchor) stt(&s, factor_at(0), verify ? (float)(end - begin) : 0.0f);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
Reference in new issue
Block a user