mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
metal: the owner's strata_dual_coda_loop_v1 Loop file in the looped window (attn / mlp LoRAs, dual-coda mixer, anchor entry, </think>-gated coda), --loop-coda, q4_tf_probe loop flags
This commit is contained in:
1 parent
acf4556c7d
commit
6b595a9fb1
5 files changed
+186
-7
No files matched your search
@@ -39,13 +39,19 @@ bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0, const float* con
|
||||
const std::function<bool(const float*, int64_t, int64_t, std::string&)>& on_chunk, std::string& err);
|
||||
} // namespace strata::metal_engine
|
||||
|
||||
namespace strata::metal_engine {
|
||||
void loop_adapter_set(const char* path, int passes); // loop.inc
|
||||
void loop_coda_set(const char* sel); // loop.inc
|
||||
} // namespace strata::metal_engine
|
||||
|
||||
namespace {
|
||||
struct Job { std::string id; int64_t off = 0; int rows = 0; std::string path; };
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) { std::fprintf(stderr, "usage: q4_tf_probe <model-metal.gguf> --ple F --cells N --out DIR --job ID:OFF:ROWS:IDS.txt ...\n"); return 2; }
|
||||
std::string ple_path, outdir = ".";
|
||||
std::string ple_path, outdir = ".", loop_path, loop_coda = "universal";
|
||||
int loop_passes = 2;
|
||||
int64_t cells = 8192;
|
||||
int K = 256, bench_n = 0;
|
||||
std::vector<Job> jobs;
|
||||
@@ -56,6 +62,9 @@ int main(int argc, char** argv) {
|
||||
else if (a == "--out") outdir = argv[i + 1];
|
||||
else if (a == "--topk") K = std::atoi(argv[i + 1]);
|
||||
else if (a == "--bench") bench_n = std::atoi(argv[i + 1]);
|
||||
else if (a == "--loop-adapter") loop_path = argv[i + 1];
|
||||
else if (a == "--loop-passes") loop_passes = std::atoi(argv[i + 1]);
|
||||
else if (a == "--loop-coda") loop_coda = argv[i + 1];
|
||||
else if (a == "--job") {
|
||||
Job j;
|
||||
std::string s = argv[i + 1];
|
||||
@@ -70,6 +79,10 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
setenv("STRATA_METAL_STREAM", "0", 1);
|
||||
strata::metal_engine::set_model_shard(argv[1]);
|
||||
if (!loop_path.empty()) { // the looped core (Qwen4-35B-A3B-Loop.safetensors)
|
||||
strata::metal_engine::loop_coda_set(loop_coda.c_str());
|
||||
strata::metal_engine::loop_adapter_set(loop_path.c_str(), loop_passes);
|
||||
}
|
||||
strata::core::SessionState ss;
|
||||
std::string err;
|
||||
strata::kernels::PleTable table;
|
||||
|
||||
@@ -26,7 +26,8 @@ const char* const kLoopSuffix[kLoopT] = {"linear_attn.in_proj_qkv", "linear_attn
|
||||
"linear_attn.in_proj_b", "linear_attn.out_proj", "self_attn.q_proj",
|
||||
"self_attn.k_proj", "self_attn.v_proj", "self_attn.o_proj", "mlp.gate",
|
||||
"mlp.shared_expert.gate_proj", "mlp.shared_expert.up_proj",
|
||||
"mlp.shared_expert.down_proj"};
|
||||
"mlp.shared_expert.down_proj", "(dual attn)", "(dual mlp)"};
|
||||
std::string g_loop_coda = "universal"; // --loop-coda (strata_dual_coda_loop_v1 only): universal | compact
|
||||
|
||||
// a number after "key": in the JSON text (the checkpoint's flat loop_config); false when absent or not a number
|
||||
bool loop_json_num(const std::string& s, const char* key, double& v) {
|
||||
@@ -57,8 +58,16 @@ void loop_adapter_set(const char* path, int passes) {
|
||||
g_loop_passes = passes;
|
||||
}
|
||||
bool loop_adapter_wanted() { return !g_loop_path.empty(); }
|
||||
void loop_coda_set(const char* sel) { g_loop_coda = sel ? sel : "universal"; }
|
||||
|
||||
struct Window::LoopA {
|
||||
// strata_dual_coda_loop_v1 (Qwen4-35B-A3B-Loop.safetensors, trainer 03_train_dual_coda_loop.py): attn / mlp LoRAs (scale 0.25) on the
|
||||
// block outputs, mixer R = x + sigmoid(gate) * up(silu(down(rms(m_prev - x) * (1 + norm)))), the entry of an extra pass is the anchor,
|
||||
// the coda LoRA delta is scaled by cd_omega[layer] from the first </think> on
|
||||
bool dual = false;
|
||||
std::vector<float> cd; // meta.cd_omega, one per coda layer
|
||||
mtl::Buffer flag; // [MAXT] the window rows' after-</think> flags (1 / 0)
|
||||
int64_t close_pos = INT64_MAX; // the position of the first </think> token of the sequence in the KV (INT64_MAX: none yet)
|
||||
int T = 1, core_start = 0, core_end = 0, coda = 0, t_max = kLoopDefTMax;
|
||||
float alpha = kLoopDefAlpha;
|
||||
bool per_pass = true;
|
||||
@@ -113,6 +122,12 @@ bool Window::loop_open(std::string& err) {
|
||||
if (!loop_json_str_is(js, "mode", "gated_workspace")) { err = "loop adapter: only mode gated_workspace is implemented"; return false; }
|
||||
}
|
||||
}
|
||||
L.dual = sf.tensors.count("meta.cd_omega") != 0;
|
||||
if (L.dual) { // the owner's file: core L12..27, coda L28..39 (its metadata says the same), at most T = 3 (core_lora.pass_1 / pass_2)
|
||||
L.core_start = 12; L.core_end = 28; L.coda = 12;
|
||||
if (L.T > 3) { err = "loop adapter: the dual-coda file supports --loop-passes 2 or 3"; return false; }
|
||||
if (g_loop_coda != "universal" && g_loop_coda != "compact") { err = "loop adapter: --loop-coda is universal or compact"; return false; }
|
||||
}
|
||||
if (L.core_start < 1 || L.core_end <= L.core_start || L.core_end > G_NL) { err = "loop adapter: core range outside the model"; return false; }
|
||||
L.coda = std::max(0, std::min(L.coda, G_NL - L.core_end));
|
||||
for (int l = L.core_start; l < L.core_end; ++l) (l % G_QI == G_QI - 1 ? L.nqc : L.ngc)++;
|
||||
@@ -155,6 +170,58 @@ bool Window::loop_open(std::string& err) {
|
||||
const size_t w = v.size(), pi = (size_t) clamp_slot(p_idx, ns), ri = (size_t) clamp_slot(rem_idx, ns);
|
||||
for (size_t j = 0; j < w; ++j) v[j] += P[pi * w + j] + R[ri * w + j];
|
||||
};
|
||||
if (L.dual) {
|
||||
const int core_len = L.core_end - L.core_start;
|
||||
std::vector<float> zeros((size_t) HD, 0.0f);
|
||||
const size_t zero_off = put(zeros);
|
||||
for (int i = 0; i < core_len; ++i) {
|
||||
const std::string pf = "mixers." + std::to_string(L.core_start + i) + ".";
|
||||
std::vector<float> w1, ma, mb, gt;
|
||||
std::vector<int64_t> sa;
|
||||
if (!need(pf + "norm.weight", w1, HD)) return false;
|
||||
if (!get(pf + "mix_a.weight", ma, &sa) || sa.size() != 2 || sa[1] != HD || sa[0] > kLoopMaxRank) {
|
||||
err = "loop adapter: " + pf + "mix_a.weight missing or of the wrong shape"; return false;
|
||||
}
|
||||
L.mixer_rank = (int) sa[0];
|
||||
if (!need(pf + "mix_b.weight", mb, HD * L.mixer_rank) || !need(pf + "gate.weight", gt, HD)) return false;
|
||||
for (float& v : w1) v += 1.0f; // rms_norm_f32(delta, 1 + norm)
|
||||
for (float& v : gt) v = 1.0f / (1.0f + std::exp(-v)); // g = sigmoid(gate)
|
||||
LoopA::Mix m;
|
||||
m.norm = put(w1); m.a = put(ma); m.b = put(mb);
|
||||
const size_t go = put(gt);
|
||||
for (int p = 1; p < L.T; ++p) { m.g.push_back(go); m.dk.push_back(zero_off); }
|
||||
L.mix.push_back(std::move(m));
|
||||
}
|
||||
auto pair = [&](const std::string& an, const std::string& bn, int pass, int l, int t) -> bool {
|
||||
std::vector<float> A, B;
|
||||
std::vector<int64_t> sa, sb;
|
||||
if (!get(an, A, &sa) || !get(bn, B, &sb) || sa.size() != 2 || sb.size() != 2 || sa[0] != sb[1] || sa[0] > kLoopMaxRank) {
|
||||
if (err.empty()) err = "loop adapter: missing or bad LoRA pair " + an;
|
||||
return false;
|
||||
}
|
||||
for (float& v : B) v *= 0.25f; // the trainer's apply_lora scale
|
||||
LoopA::Lora x;
|
||||
x.a = put(A); x.b = put(B);
|
||||
x.in = (uint32_t) sa[1]; x.out = (uint32_t) sb[0]; x.rank = (uint32_t) sa[0];
|
||||
L.lora[LoopA::key(pass, l, t)] = x;
|
||||
++L.hooks;
|
||||
return true;
|
||||
};
|
||||
const char* kinds[2] = {"attn", "mlp"};
|
||||
for (int p = 1; p < L.T; ++p)
|
||||
for (int i = 0; i < core_len; ++i)
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
const std::string b = "core_lora.pass_" + std::to_string(p) + "." + std::to_string(L.core_start + i) + "." + kinds[k];
|
||||
if (!pair(b + "_a.weight", b + "_b.weight", p, L.core_start + i, kAttn + k)) return false;
|
||||
}
|
||||
for (int j = 0; j < L.coda; ++j)
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
const std::string b = "coda_lora." + g_loop_coda + "." + std::to_string(L.core_end + j) + "." + kinds[k];
|
||||
if (!pair(b + "_a.weight", b + "_b.weight", 0, L.core_end + j, kAttn + k)) return false;
|
||||
}
|
||||
if (!need("meta.cd_omega", L.cd, L.coda)) return false;
|
||||
for (float& v : L.cd) v = std::min(1.0f, std::max(0.0f, v));
|
||||
} else {
|
||||
// 1. the workspace entry
|
||||
{
|
||||
std::vector<float> nsw, naw, ea, eb, gsb, gmb;
|
||||
@@ -233,13 +300,14 @@ bool Window::loop_open(std::string& err) {
|
||||
for (int j = 0; j < L.coda; ++j)
|
||||
for (int t = 0; t < kLoopT; ++t)
|
||||
if (!add_lora("coda." + std::to_string(j) + "." + kLoopSuffix[t], 0, L.core_end + j, t, -1, -1)) return false;
|
||||
} // (not the dual-coda file)
|
||||
// the LoRA shapes against this model's projections (a different model's adapter stops here, not in a kernel)
|
||||
const uint32_t want_out[kLoopT] = {(uint32_t) G_CONV, (uint32_t) G_ZV, (uint32_t) G_HV, (uint32_t) G_HV, (uint32_t) N,
|
||||
(uint32_t) G_QFULL, (uint32_t) G_KV, (uint32_t) G_KV, (uint32_t) N, (uint32_t) nexp_,
|
||||
(uint32_t) NFF, (uint32_t) NFF, (uint32_t) N};
|
||||
(uint32_t) NFF, (uint32_t) NFF, (uint32_t) N, (uint32_t) N, (uint32_t) N};
|
||||
const uint32_t want_in[kLoopT] = {(uint32_t) N, (uint32_t) N, (uint32_t) N, (uint32_t) N, (uint32_t) G_ZV, (uint32_t) N,
|
||||
(uint32_t) N, (uint32_t) N, (uint32_t) G_QOUT, (uint32_t) N, (uint32_t) N, (uint32_t) N,
|
||||
(uint32_t) NFF};
|
||||
(uint32_t) NFF, (uint32_t) N, (uint32_t) N};
|
||||
for (const auto& [k, x] : L.lora) {
|
||||
const int t = (int) (k % kLoopT);
|
||||
if (x.in != want_in[t] || x.out != want_out[t]) {
|
||||
@@ -253,6 +321,8 @@ bool Window::loop_open(std::string& err) {
|
||||
const size_t rows = (size_t) MAXT * HD * 4;
|
||||
L.anchor = cx_->alloc(rows); L.mprev = cx_->alloc(rows); L.ns = cx_->alloc(rows); L.na = cx_->alloc(rows);
|
||||
L.rms = cx_->alloc((size_t) 4 * MAXT * HC); L.tmp = cx_->alloc((size_t) 4 * MAXT * kLoopMaxRank);
|
||||
L.flag = cx_->alloc((size_t) 4 * MAXT);
|
||||
std::memset(L.flag.ptr, 0, (size_t) 4 * MAXT);
|
||||
L.lib = cx_->compile(strata_metal::lib_loop(engine_msl_dir()).files, strata_metal::lib_loop(engine_msl_dir()).prelude);
|
||||
if (L.lib < 0) { err = "loop adapter: loop.metal did not compile"; return false; }
|
||||
std::fprintf(stderr, "strata metal: loop adapter %s: T=%d core=[%d,%d) coda=%d hooks=%d entry rank %d mixer rank %d (%.1f MB)\n",
|
||||
@@ -306,11 +376,30 @@ void Window::loop_lora(std::vector<mtl::Dispatch>& ds, int l, int t, const mtl::
|
||||
d1.grid[0] = q->rank; d1.grid[1] = (uint32_t) ntok; d1.tg[0] = 256;
|
||||
nb_ = false; // after the projection that wrote y (and whatever wrote x)
|
||||
push(ds, d1);
|
||||
d2.p = P(lp_->lib, "loop_lora_b", {}, "loop_lora_b");
|
||||
d2.args = {mtl::Arg::B(lp_->tmp), mtl::Arg::B(lp_->W, q->b), mtl::Arg::B(y, y_off), mtl::Arg::V(a)};
|
||||
if (lp_->dual && l >= lp_->core_end && (size_t) (l - lp_->core_end) < lp_->cd.size()) { // the coda: the delta times cd_omega after </think>
|
||||
const float cd = lp_->cd[(size_t) (l - lp_->core_end)];
|
||||
d2.p = P(lp_->lib, "loop_lora_b_gated", {}, "loop_lora_b_gated");
|
||||
d2.args = {mtl::Arg::B(lp_->tmp), mtl::Arg::B(lp_->W, q->b), mtl::Arg::B(y, y_off), mtl::Arg::V(a), mtl::Arg::B(lp_->flag), mtl::Arg::V(cd)};
|
||||
} else {
|
||||
d2.p = P(lp_->lib, "loop_lora_b", {}, "loop_lora_b");
|
||||
d2.args = {mtl::Arg::B(lp_->tmp), mtl::Arg::B(lp_->W, q->b), mtl::Arg::B(y, y_off), mtl::Arg::V(a)};
|
||||
}
|
||||
d2.grid[0] = (q->out + 255) / 256; d2.grid[1] = (uint32_t) ntok; d2.tg[0] = 256;
|
||||
push(ds, d2);
|
||||
}
|
||||
// the window rows' after-</think> flags (strata_dual_coda_loop_v1): row t is 1 from the first </think> token of the sequence on. The sequence's
|
||||
// first </think> position is kept across windows; a window starting at or before it (a rewind, a new prompt) forgets it first.
|
||||
void Window::loop_flags(const int32_t* tokens, int Tn, int64_t pos0) {
|
||||
if (!lp_ || !lp_->dual) return;
|
||||
LoopA& L = *lp_;
|
||||
constexpr int32_t kThinkEnd = 248069;
|
||||
if (pos0 == 0 || pos0 <= L.close_pos) L.close_pos = INT64_MAX;
|
||||
float* f = L.flag.as<float>();
|
||||
for (int t = 0; t < Tn; ++t) {
|
||||
if (L.close_pos == INT64_MAX && tokens[t] == kThinkEnd) L.close_pos = pos0 + t;
|
||||
f[t] = pos0 + t >= L.close_pos ? 1.0f : 0.0f;
|
||||
}
|
||||
}
|
||||
bool Window::loop_has(int l, int t) const { return lp_ && lp_->find(pass_, l, t) != nullptr; }
|
||||
|
||||
// R_ with the last sublayer's pending write applied (gr_norm with apply; its xn_ output unused), as the PLE block does;
|
||||
@@ -347,6 +436,10 @@ void Window::loop_gnorm(std::vector<mtl::Dispatch>& ds, const mtl::Buffer& x, si
|
||||
// R_ = WorkspaceEntry(anchor, m_prev) for extra pass p (1..T-1)
|
||||
void Window::loop_entry(std::vector<mtl::Dispatch>& ds, int p, int ntok) {
|
||||
LoopA& L = *lp_;
|
||||
if (L.dual) { // every extra pass restarts from the core's input state (the trainer's R_entries[12])
|
||||
loop_copy(ds, L.anchor, R_, ntok);
|
||||
return;
|
||||
}
|
||||
struct { uint32_t n_embd, hc, hc_dim, rank; float eps; } a{N, HC, (uint32_t) (HC * N), (uint32_t) L.entry_rank, eps_};
|
||||
nb_ = false;
|
||||
loop_gnorm(ds, L.mprev, L.we_ns, L.ns, false, ntok);
|
||||
@@ -368,6 +461,32 @@ void Window::loop_mixer(std::vector<mtl::Dispatch>& ds, int p, int step, int nto
|
||||
LoopA& L = *lp_;
|
||||
const LoopA::Mix& m = L.mix[(size_t) step];
|
||||
struct { uint32_t n_embd, hc, hc_dim, rank; float eps; } a{N, HC, (uint32_t) (HC * N), (uint32_t) L.mixer_rank, eps_};
|
||||
if (L.dual) { // R = x + sigmoid(gate) * up(silu(down(rms(m_prev - x) * (1 + norm)))), eps 1e-6
|
||||
struct { uint32_t n_embd, hc, hc_dim, rank; float eps; } da{N, HC, (uint32_t) (HC * N), (uint32_t) L.mixer_rank, 1e-6f};
|
||||
nb_ = false;
|
||||
mtl::Dispatch n1;
|
||||
n1.p = P(L.lib, "loop_dual_norm", {}, "loop_dual_norm");
|
||||
n1.args = {mtl::Arg::B(R_), mtl::Arg::B(L.mprev), mtl::Arg::B(L.W, m.norm), mtl::Arg::B(L.ns), mtl::Arg::V(da)};
|
||||
n1.grid[0] = 1; n1.grid[1] = (uint32_t) ntok; n1.tg[0] = 256;
|
||||
push(ds, n1);
|
||||
mtl::Dispatch d1, ds1, d2;
|
||||
struct { uint32_t in_dim, out_dim, rank, x_stride, y_stride; } la{(uint32_t) (HC * N), 0, (uint32_t) L.mixer_rank, (uint32_t) (HC * N), 0};
|
||||
d1.p = P(L.lib, "loop_lora_a", {}, "loop_lora_a");
|
||||
d1.args = {mtl::Arg::B(L.ns), mtl::Arg::B(L.W, m.a), mtl::Arg::B(L.tmp), mtl::Arg::V(la)};
|
||||
d1.grid[0] = (uint32_t) L.mixer_rank; d1.grid[1] = (uint32_t) ntok; d1.tg[0] = 256;
|
||||
push(ds, d1);
|
||||
const uint32_t nsil = (uint32_t) (ntok * L.mixer_rank);
|
||||
ds1.p = P(L.lib, "loop_silu", {}, "loop_silu");
|
||||
ds1.args = {mtl::Arg::B(L.tmp), mtl::Arg::V(nsil)};
|
||||
ds1.grid[0] = (nsil + 255) / 256; ds1.tg[0] = 256;
|
||||
push(ds, ds1);
|
||||
d2.p = P(L.lib, "loop_mix_comb", {}, "loop_mix_comb");
|
||||
d2.args = {mtl::Arg::B(R_), mtl::Arg::B(L.tmp), mtl::Arg::B(L.W, m.b), mtl::Arg::B(L.W, m.g[(size_t) p - 1]),
|
||||
mtl::Arg::B(L.W, m.dk[(size_t) p - 1]), mtl::Arg::B(R_), mtl::Arg::V(a)};
|
||||
d2.grid[0] = (uint32_t) (HC * N + 255) / 256; d2.grid[1] = (uint32_t) ntok; d2.tg[0] = 256;
|
||||
push(ds, d2);
|
||||
return;
|
||||
}
|
||||
nb_ = false;
|
||||
loop_gnorm(ds, L.mprev, m.norm, L.ns, false, ntok);
|
||||
mtl::Dispatch d1, d2;
|
||||
|
||||
@@ -113,6 +113,43 @@ kernel void loop_mix_comb(device const float* x [[buffer(0)]], device const floa
|
||||
R[idx] = xv + dk[j] * xv + g[j] * d;
|
||||
}
|
||||
|
||||
// ---- strata_dual_coda_loop_v1 (the owner's Qwen4-35B-A3B-Loop.safetensors; trainer 03_train_dual_coda_loop.py) ----
|
||||
// the mixer's input: dn[t, j] = (m[t, j] - x[t, j]) * rsqrt(mean_j (m - x)^2 + eps) * w1[j] (ONE RMS over hc_dim, w1 = 1 + norm)
|
||||
// grid (1, n_tok)
|
||||
kernel void loop_dual_norm(device const float* x [[buffer(0)]], device const float* m [[buffer(1)]], device const float* w1 [[buffer(2)]],
|
||||
device float* dn [[buffer(3)]], constant HcArgs& p [[buffer(4)]],
|
||||
uint2 tg [[threadgroup_position_in_grid]], uint tid [[thread_index_in_threadgroup]]) {
|
||||
threadgroup float sm[8];
|
||||
const uint t = tg.y;
|
||||
const size_t base = (size_t) t * p.hc_dim;
|
||||
float s = 0.0f;
|
||||
for (uint k = tid; k < p.hc_dim; k += LTG) { const float d = m[base + k] - x[base + k]; s += d * d; }
|
||||
s = tg_sum(s, sm, tid);
|
||||
const float inv = rsqrt(s / (float) p.hc_dim + p.eps);
|
||||
for (uint k = tid; k < p.hc_dim; k += LTG) dn[base + k] = (m[base + k] - x[base + k]) * inv * w1[k];
|
||||
}
|
||||
|
||||
// v[i] = silu(v[i]) grid (ceil(n / 256))
|
||||
kernel void loop_silu(device float* v [[buffer(0)]], constant uint& n [[buffer(1)]],
|
||||
uint tg [[threadgroup_position_in_grid]], uint tid [[thread_index_in_threadgroup]]) {
|
||||
const uint i = tg * LTG + tid;
|
||||
if (i < n) { const float a = v[i]; v[i] = a / (1.0f + exp(-a)); }
|
||||
}
|
||||
|
||||
// y[t, o] += s_t * sum_r tmp[t, r] * B_eff[o, r], s_t = (flag[t] > 0.5 ? cd : 1) (the coda LoRA after </think>)
|
||||
// grid (ceil(out / 256), n_tok)
|
||||
kernel void loop_lora_b_gated(device const float* tmp [[buffer(0)]], device const float* b [[buffer(1)]], device float* y [[buffer(2)]],
|
||||
constant LoraArgs& p [[buffer(3)]], device const float* flag [[buffer(4)]], constant float& cd [[buffer(5)]],
|
||||
uint2 tg [[threadgroup_position_in_grid]], uint tid [[thread_index_in_threadgroup]]) {
|
||||
const uint o = tg.x * LTG + tid, t = tg.y;
|
||||
if (o >= p.out_dim) return;
|
||||
device const float* tr = tmp + t * p.rank;
|
||||
device const float* br = b + (size_t) o * p.rank;
|
||||
float acc = 0.0f;
|
||||
for (uint r = 0; r < p.rank; ++r) acc += tr[r] * br[r];
|
||||
y[(size_t) t * p.y_stride + o] += acc * (flag[t] > 0.5f ? cd : 1.0f);
|
||||
}
|
||||
|
||||
// dst[i] = src[i] grid (ceil(n / 256))
|
||||
kernel void loop_copy(device const float* src [[buffer(0)]], device float* dst [[buffer(1)]], constant uint& n [[buffer(2)]],
|
||||
uint tg [[threadgroup_position_in_grid]], uint tid [[thread_index_in_threadgroup]]) {
|
||||
|
||||
@@ -246,8 +246,9 @@ const int32_t* g_mrope_host = nullptr;
|
||||
const int32_t* g_mrope_dev = nullptr;
|
||||
|
||||
// the looped core's LoRA targets (loop.inc): the projections a pass's LoRA adds to
|
||||
enum LoopT { kGdnQkv, kGdnZ, kGdnA, kGdnB, kGdnOut, kQsaQ, kQsaK, kQsaV, kQsaO, kMoeGate, kShGate, kShUp, kShDown, kLoopT };
|
||||
enum LoopT { kGdnQkv, kGdnZ, kGdnA, kGdnB, kGdnOut, kQsaQ, kQsaK, kQsaV, kQsaO, kMoeGate, kShGate, kShUp, kShDown, kAttn, kMlp, kLoopT }; // kAttn / kMlp: strata_dual_coda_loop_v1
|
||||
bool loop_adapter_wanted(); // loop.inc: --loop-adapter given
|
||||
void loop_coda_set(const char* sel); // loop.inc: --loop-coda universal|compact
|
||||
|
||||
class Window {
|
||||
public:
|
||||
@@ -427,6 +428,7 @@ private:
|
||||
int loop_extra_qsa() const; // the looped core's extra QSA slots (0 when stock)
|
||||
bool loop_tap_here(int l) const; // DFlash2 taps: a core layer's on its last pass, the others' on pass 0
|
||||
bool loop_has(int l, int t) const;
|
||||
void loop_flags(const int32_t* tokens, int Tn, int64_t pos0); // the dual-coda file: the rows' after-</think> flags
|
||||
void loop_lora(std::vector<mtl::Dispatch>& ds, int l, int t, const mtl::Buffer& x, uint32_t xs, const mtl::Buffer& y,
|
||||
size_t y_off, uint32_t ys, int ntok);
|
||||
void loop_materialize(std::vector<mtl::Dispatch>& ds, int l, int ntok);
|
||||
@@ -1328,6 +1330,7 @@ void Window::layer(std::vector<mtl::Dispatch>& ds, int l, int ntok, int64_t pos0
|
||||
if (!fuse_) xprep(ds, yg_, XY_, G_ZV);
|
||||
gemv(ds, T(b + "ssm_out.weight"), XY_, bo_, N, ntok);
|
||||
loop_lora(ds, l, kGdnOut, XY_.b, G_ZV, bo_, 0, N, ntok);
|
||||
loop_lora(ds, l, kAttn, XA_.b, N, bo_, 0, N, ntok); // (strata_dual_coda_loop_v1: the block input to the block output)
|
||||
}
|
||||
} else {
|
||||
const int qi = loop_qi(l);
|
||||
@@ -1418,6 +1421,7 @@ void Window::layer(std::vector<mtl::Dispatch>& ds, int l, int ntok, int64_t pos0
|
||||
if (rec_) rec_->push_back({(int) ds.size() - 1, 1});
|
||||
gemv(ds, T(b + "attn_output.weight"), XQ_, bo_, N, ntok);
|
||||
loop_lora(ds, l, kQsaO, XQ_.b, G_QOUT, bo_, 0, N, ntok);
|
||||
loop_lora(ds, l, kAttn, XA_.b, N, bo_, 0, N, ntok);
|
||||
}
|
||||
// ---- the MoE
|
||||
gr_read(ds, b + "hc_ffn", true, XF_, false, ntok);
|
||||
@@ -1619,6 +1623,7 @@ void Window::layer(std::vector<mtl::Dispatch>& ds, int l, int ntok, int64_t pos0
|
||||
d.tg[0] = 256;
|
||||
push(ds, d);
|
||||
}
|
||||
loop_lora(ds, l, kMlp, XF_.b, N, bo_, 0, N, ntok); // (strata_dual_coda_loop_v1: the MoE input to the MoE output)
|
||||
if (taps_on_ && tap_slot_[l] >= 0 && loop_tap_here(l)) tap_dispatch(ds, l, R_, bo_, inj_[cur_inj_], tap_dec_, ntok); // DFlash2: the layers completed output (dflash2.inc)
|
||||
}
|
||||
|
||||
@@ -1688,6 +1693,7 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t
|
||||
mtp_src_ = nullptr;
|
||||
if (Tn < 1 || Tn > MAXT || (head && Tn > max_t_)) { err = "metal window: window size out of range"; return false; }
|
||||
if (pos0 + Tn + 1 >= max_cells_) { err = "metal window: the window runs past the context"; return false; }
|
||||
loop_flags(tokens, Tn, pos0); // (the dual-coda loop: the rows' after-</think> flags; nothing otherwise)
|
||||
const double t0 = now_ms();
|
||||
// ---- the embeddings (CPU dequant of the token rows straight into the shared buffer), broadcast to the streams
|
||||
const gguf::Tensor& te = T("token_embd.weight");
|
||||
|
||||
@@ -205,6 +205,7 @@ namespace strata::metal_engine {
|
||||
void set_model_shard(const char* shard1); // metal/src/engine/window.cpp
|
||||
void set_batch_slots(int n); // metal/src/engine/window.cpp: how many --batch slots the window keeps state for
|
||||
void loop_adapter_set(const char* path, int passes); // metal/src/engine/loop.inc: --loop-adapter
|
||||
void loop_coda_set(const char* sel); // metal/src/engine/loop.inc: --loop-coda universal|compact
|
||||
// batch slot b <-> the main sequence: the running state (unless kv_only) and the first `upto` cells of K/V
|
||||
bool window_slot_copy_main(bool into_slot, int slot, int64_t upto, bool kv_only, std::string& err);
|
||||
std::string draft_vocab_default(); // metal/src/engine/window.cpp: the packaged default draft subset (data/draft_vocab.bin)
|
||||
@@ -1092,6 +1093,7 @@ struct Options {
|
||||
/// prompt again, the v0.1.2 behaviour). One is the GDN recurrence of the 36 layers, the QSA indexer tails and
|
||||
/// the PLE history (~118 MB of host RAM); the KV cache itself is positional and stays where it is.
|
||||
int prompt_cache = 6;
|
||||
std::string loop_coda = "universal"; ///< --loop-coda universal|compact (the dual-coda file)
|
||||
std::string loop_adapter; ///< --loop-adapter FILE: Looped Qwen3.8-Flash-Next recurrent-core adapter (.safetensors + sibling .json), Metal
|
||||
int loop_passes = 0; ///< --loop-passes T (0: the window's default, 2)
|
||||
int64_t conversation_cache_mib = -1; // host RAM for independent conversations; -1: not given, --serve sizes it from the free RAM (#3493), 0: off
|
||||
@@ -4199,6 +4201,7 @@ int main(int argc, char** argv) {
|
||||
else if (a == "--session-save-reclaim") o.session_save_reclaim = true;
|
||||
else if (a == "--prompt-cache") o.prompt_cache = std::max(0, std::atoi(next("--prompt-cache")));
|
||||
else if (a == "--loop-adapter") o.loop_adapter = next("--loop-adapter");
|
||||
else if (a == "--loop-coda") o.loop_coda = next("--loop-coda");
|
||||
else if (a == "--loop-passes") o.loop_passes = std::atoi(next("--loop-passes"));
|
||||
else if (a == "--conversation-cache-swap-block-mib") {
|
||||
const std::string value = next(a.c_str());
|
||||
@@ -5480,6 +5483,7 @@ int main(int argc, char** argv) {
|
||||
// the looped core's extra K/V and GDN slots live only in the window: a parked conversation would come back without them
|
||||
if (o.conversation_cache_mib != 0) std::fprintf(stderr, "strata generate: --loop-adapter: conversation cache off\n");
|
||||
o.conversation_cache_mib = 0;
|
||||
strata::metal_engine::loop_coda_set(o.loop_coda.c_str());
|
||||
strata::metal_engine::loop_adapter_set(o.loop_adapter.c_str(), o.loop_passes);
|
||||
}
|
||||
o.mmap_experts = true; // ... experts included: no resident copy of them (the GGUF shards in place)
|
||||
|
||||
Reference in new issue
Block a user