metal: vision - image rows in the prompt, M-RoPE (qsa_prep constant 24), setup's --vision
The prefill hook hands the prompt's image rows (sp.embd_rows) to the Metal prompt paths (prompt_chain.inc, the
streaming prompt path, the 8-row windows), which take an image row in place of the token embedding.
mrope_table_set keeps the host view of the server's (t, h, w) table; every window, prompt chunk and draft round copies
the rows it reads into the window's buffer, bound at index 20 of qsa_prep with function constant 24 (mac-kern
29724e5e) - the window, both prompt paths and the draft layer. The cached window's QsaArgs are patched by the last
inline argument (the table is bound after it). setup_mac.py --vision yes: the pinned projector (SHA-256) and
strata-vision built with ggml-metal; the config gets --vision and the encoder.
missing.cpp: a crash prints its call stack and fault address.
M5 Max, Q2_0, ISTA's projector, a test picture (shapes and 'STRATA 42'): 'STRATA 42' and 'Red circle / Blue square /
Green triangle', the same answers as the HIP engine on the R9700 (p620) with the same picture and files, also
through the config setup writes. Text greedy unchanged with and without --vision (prompt1, story, the 1,662-token
chain prompt). prefill.cpp: the CUDA (P1004) and HIP (p620) objects are byte-IDENTICAL (preprocessor-only).
This commit is contained in:
1 parent
13ed803334
commit
ae02e7db17
7 files changed
+152
-17
No files matched your search
@@ -286,6 +286,38 @@ def engine_args(pack: Path, shards: list, rt: Path, ctx: int, stream: bool = Fal
|
||||
"--mmap-experts"] # what the Metal engine does anyway (the GGUF mapped in place); the server says so
|
||||
|
||||
|
||||
# the image encoder's projector (setup.py's pinned revision): name -> (bytes, SHA-256)
|
||||
VISION_SHA256 = {"mmproj-Qwen3.8-Flash-Next-BF16.gguf":
|
||||
(907543008, "b1a82259702816a5330d7bd7607cd9676b11780e79ff7348c21103ff3ce49bd0")}
|
||||
VISION_EXE = ROOT / "build-vision" / "bin" / "strata-vision"
|
||||
|
||||
|
||||
def build_vision(llama: Path) -> Path:
|
||||
"""strata-vision (tools/vision: llama.cpp's mtmd at the pinned commit), with ggml's Metal backend: the encoder
|
||||
runs on the GPU."""
|
||||
stamp = ROOT / "build-vision" / "BUILD.json"
|
||||
src = source_digest()
|
||||
try:
|
||||
if VISION_EXE.exists() and json.loads(stamp.read_text(encoding="utf-8")).get("source") == src:
|
||||
S.ok(f"image encoder: {VISION_EXE}")
|
||||
return VISION_EXE
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
S.say(" Building the image encoder (1-3 minutes) ...")
|
||||
env = dict(os.environ, PATH=str(Path(sys.executable).parent) + os.pathsep + os.environ.get("PATH", ""))
|
||||
log = ROOT / "build-vision.log"
|
||||
with open(log, "w", encoding="utf-8") as f:
|
||||
for cmd in ([tool("cmake"), "-S", str(ROOT / "tools" / "vision"), "-B", str(ROOT / "build-vision"), "-G", "Ninja",
|
||||
"-DCMAKE_BUILD_TYPE=Release", f"-DCMAKE_MAKE_PROGRAM={tool('ninja')}", f"-DLLAMA_DIR={llama}",
|
||||
"-DSTRATA_VISION_CUDA=OFF", "-DSTRATA_PORTABLE=OFF"],
|
||||
[tool("ninja"), "-C", str(ROOT / "build-vision"), "strata-vision"]):
|
||||
if subprocess.call(cmd, stdout=f, stderr=subprocess.STDOUT, env=env) != 0:
|
||||
S.fail(f"the image encoder did not build (see {log})")
|
||||
stamp.write_text(json.dumps({"source": src}), encoding="utf-8")
|
||||
S.ok(f"image encoder built: {VISION_EXE}")
|
||||
return VISION_EXE
|
||||
|
||||
|
||||
def ple_shard(files: list) -> Path:
|
||||
"""The shard that holds the n-gram table (per_layer_token_embd)."""
|
||||
sys.path.insert(0, str(ROOT / "metal" / "tools"))
|
||||
@@ -356,6 +388,8 @@ def main(argv=None) -> int:
|
||||
ap.add_argument("--check", action="store_true")
|
||||
ap.add_argument("--update", action="store_true")
|
||||
ap.add_argument("--build", action="store_true", help="build the engine again even when it is current")
|
||||
ap.add_argument("--vision", choices=["yes", "no"],
|
||||
help="let the model read images (the image encoder on the GPU, built here; default: asked, no)")
|
||||
a, rest = ap.parse_known_args(argv)
|
||||
if rest:
|
||||
S.warn("not used on a Mac: " + " ".join(rest))
|
||||
@@ -399,6 +433,8 @@ def main(argv=None) -> int:
|
||||
str(names.index(rec) + 1), a.yes)) - 1]
|
||||
for n in model_note(model, info["ram_gb"]):
|
||||
S.warn(n)
|
||||
vision = a.vision or ("yes" if S.ask("Should the model also read images? (the image encoder, ~1 GB more)",
|
||||
["y", "n"], "n", a.yes) == "y" else "no")
|
||||
ctx = a.context or recommend_context(info["ram_gb"])
|
||||
if ctx > TESTED_CONTEXT:
|
||||
S.say(f" context {ctx}: measured on the Mac up to {TESTED_CONTEXT} (needles found at 8K-32K); longer is kept")
|
||||
@@ -442,6 +478,16 @@ def main(argv=None) -> int:
|
||||
S.verify_sha256(s, *shard_sha(s.name))
|
||||
S.ok("model files present")
|
||||
|
||||
mmproj = vexe = None
|
||||
if vision == "yes":
|
||||
mmproj = models_root / fam["mmproj"]
|
||||
if not (mmproj.exists() and S.done(mmproj)):
|
||||
S.download(fam["mmproj_hf"] + fam["mmproj"], mmproj, "vision encoder")
|
||||
sha = VISION_SHA256.get(fam["mmproj"])
|
||||
if sha:
|
||||
S.verify_sha256(mmproj, *sha)
|
||||
vexe = build_vision(llama)
|
||||
|
||||
# ---- 6. the pack and the MTP draft layer
|
||||
S.step(6, "preparing the model for Strata")
|
||||
pack = S.find_in(roots, f"packs/{tag.lower()}") or data / "packs" / tag.lower()
|
||||
@@ -487,6 +533,10 @@ def main(argv=None) -> int:
|
||||
port = a.port or 8080
|
||||
cfg = mac_config(exe, engine_args(pack, shards, rt, ctx, stream), pack, f"{fam['name']}-{model.lower()}", tag, port)
|
||||
cfg["env"] = {"STRATA_METAL_STREAM": "1" if stream else "0"} # the engine follows setup's measured choice
|
||||
if vision == "yes":
|
||||
cfg["args"] += ["--vision"]
|
||||
cfg["vision"] = {"exe": str(vexe), "mmproj": str(mmproj), "model": str(files[0]), "gpu": True,
|
||||
"max_tokens": 1024}
|
||||
if a.host:
|
||||
cfg["host"] = a.host
|
||||
if a.api_key:
|
||||
|
||||
@@ -41,6 +41,8 @@ const Cvec& cvec() {
|
||||
|
||||
// an uncaught exception or abort on the Metal build prints where it came from (macOS gives no core dump by default and
|
||||
// lldb needs a debugging permission a user's session may not have)
|
||||
#include <csignal>
|
||||
#include <signal.h>
|
||||
#include <execinfo.h>
|
||||
#include <exception>
|
||||
#include <unistd.h>
|
||||
@@ -54,4 +56,25 @@ namespace {
|
||||
std::abort();
|
||||
}
|
||||
const bool g_terminate_set = (std::set_terminate(metal_terminate), true);
|
||||
// a crash (SIGSEGV / SIGBUS) prints its call stack too: macOS keeps no report for a command-line process by default
|
||||
void metal_crash(int sig, siginfo_t* si, void*) {
|
||||
void* fr[64];
|
||||
const int n = backtrace(fr, 64);
|
||||
char msg[160];
|
||||
const int m = std::snprintf(msg, sizeof msg, "strata metal: crashed (signal %d at address %p) - the call stack:\n", sig,
|
||||
si ? si->si_addr : nullptr);
|
||||
(void) !write(2, msg, (size_t) m);
|
||||
backtrace_symbols_fd(fr, n, 2);
|
||||
signal(sig, SIG_DFL);
|
||||
raise(sig);
|
||||
}
|
||||
bool set_crash() {
|
||||
struct sigaction sa {};
|
||||
sa.sa_sigaction = metal_crash;
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
sigaction(SIGSEGV, &sa, nullptr);
|
||||
sigaction(SIGBUS, &sa, nullptr);
|
||||
return true;
|
||||
}
|
||||
const bool g_crash_set = set_crash();
|
||||
} // namespace
|
||||
@@ -109,9 +109,11 @@ bool Window::pc_chunk(const int64_t* tokens, int nT, int64_t pos0, std::string&
|
||||
// ---- the rows: the embedding into every stream; the PLE table rows
|
||||
const gguf::Tensor& te = T("token_embd.weight");
|
||||
const size_t rb = ref::row_bytes(te.type, N);
|
||||
mrope_sync(pos0, nT);
|
||||
for (int t = 0; t < nT; ++t) {
|
||||
float* r = b.R.as<float>() + (size_t) t * HC * N;
|
||||
ref::dequant(te.type, (const uint8_t*) W_.ptr + te.offset + (size_t) tokens[t] * rb, r, N);
|
||||
if (const float* img = img_row(pos0 + t)) std::memcpy(r, img, (size_t) N * 4);
|
||||
else ref::dequant(te.type, (const uint8_t*) W_.ptr + te.offset + (size_t) tokens[t] * rb, r, N);
|
||||
for (int c = 1; c < HC; ++c) std::memcpy(r + (size_t) c * N, r, (size_t) N * 4);
|
||||
}
|
||||
if (ple_on_) {
|
||||
@@ -224,13 +226,13 @@ bool Window::pc_chunk(const int64_t* tokens, int nT, int64_t pos0, std::string&
|
||||
gemm(b.mixh, b.Wiq, b.iqr, nT, 512, N);
|
||||
gemm(b.mixh, b.Wik, b.ikr, nT, 128, N);
|
||||
QsaArgs a{(uint32_t) pos0, 0, (uint32_t) max_cells_, (uint32_t) nT, eps_};
|
||||
D(P_(b.lqsa, "qsa_prep"),
|
||||
D(P_(b.lqsa, "qsa_prep", qsa_c(), qsa_k()),
|
||||
{mtl::Arg::B(b.qfull), mtl::Arg::B(b.kcur), mtl::Arg::B(b.vcur), mtl::Arg::B(b.iqr), mtl::Arg::B(b.ikr),
|
||||
mtl::Arg::B(W_, T(bl + "attn_q_norm.weight").offset), mtl::Arg::B(W_, T(bl + "attn_k_norm.weight").offset),
|
||||
mtl::Arg::B(W_, T(bl + "indexer.q_norm.weight").offset), mtl::Arg::B(W_, T(bl + "indexer.k_norm.weight").offset),
|
||||
mtl::Arg::B(cs_), mtl::Arg::B(b.qcur), mtl::Arg::B(b.qidx), mtl::Arg::B(kq_[(size_t) qi]), mtl::Arg::B(vq_[(size_t) qi]),
|
||||
mtl::Arg::B(ksc_[(size_t) qi]), mtl::Arg::B(vsc_[(size_t) qi]), mtl::Arg::B(tail_[(size_t) qi]),
|
||||
mtl::Arg::B(dead_[(size_t) qi]), mtl::Arg::B(pooled_[(size_t) qi]), mtl::Arg::V(a)},
|
||||
mtl::Arg::B(dead_[(size_t) qi]), mtl::Arg::B(pooled_[(size_t) qi]), mtl::Arg::V(a), mtl::Arg::B(mrope_)},
|
||||
33, nT, 256);
|
||||
if (pos0 + nT > PC_QB) { // some row's context is past the budget: the indexer selects
|
||||
if (b.k4)
|
||||
|
||||
@@ -247,7 +247,6 @@ namespace strata::kernels { void moe_hit_grouped_s2(const uint8_t * p0, const in
|
||||
namespace strata::kernels { void moe_hit_grouped_s2_cpu_order(const uint8_t * p0, const int32_t * p1, const int32_t * p2, int64_t p3, int64_t p4, const uint8_t * p5, void * p6, float * p7, void * p8, const float * p9, float * p10) { strata::metal_engine::missing("strata::kernels::moe_hit_grouped_s2_cpu_order"); } }
|
||||
namespace strata::kernels { void moe_hit_grouped_s2_dev(const uint8_t * p0, const int32_t * p1, const int32_t * p2, const int32_t * p3, int64_t p4, int64_t p5, const uint8_t * p6, void * p7, float * p8, void * p9, const float * p10) { strata::metal_engine::missing("strata::kernels::moe_hit_grouped_s2_dev"); } }
|
||||
namespace strata::kernels { void moe_hit_select(const int32_t * p0, const int32_t * p1, int p2, int p3, int32_t * p4, int32_t * p5, int32_t * p6, void * p7) { strata::metal_engine::missing("strata::kernels::moe_hit_select"); } }
|
||||
namespace strata::kernels { void mrope_table_set(const int32_t * p0) { strata::metal_engine::missing("strata::kernels::mrope_table_set"); } }
|
||||
namespace strata::kernels { void mtp_select(const float * p0, int64_t p1, const int32_t * p2, const int32_t * p3, float * p4, int32_t * p5, int32_t * p6, int p7, void * p8, const float * p9, float * p10) { strata::metal_engine::missing("strata::kernels::mtp_select"); } }
|
||||
namespace strata::kernels { bool multi_block_head_ops() { strata::metal_engine::missing("strata::kernels::multi_block_head_ops"); return {}; } }
|
||||
namespace strata::kernels { void native_expert_grouped(const NativeExpertLayout & p0, const unsigned long long * p1, const int32_t * p2, const int32_t * p3, const int32_t * p4, const int32_t * p5, int64_t p6, int64_t p7, const void * p8, void * p9, float * p10, void * p11, int64_t p12) { strata::metal_engine::missing("strata::kernels::native_expert_grouped"); } }
|
||||
|
||||
+64
-11
@@ -91,6 +91,13 @@ struct PromptBufs {
|
||||
Wsg, Wshg, Wshu, Wshd, pemb, pembh, pkey, pval, keyn, qn, gated, normW, Wpk, Wpv;
|
||||
};
|
||||
|
||||
// The vision path (--vision, GENI requests), from the shared host code: the image rows of the prompt being read
|
||||
// (host rows by absolute position, null = embed the token; set by prompt_run for its duration) and the M-RoPE table
|
||||
// (int32 [cells][3] (t, h, w); mrope_table_set: the host view of the device table the server fills per request).
|
||||
const float* const* g_embd_rows = nullptr;
|
||||
const int32_t* g_mrope_host = nullptr;
|
||||
const int32_t* g_mrope_dev = nullptr;
|
||||
|
||||
class Window {
|
||||
public:
|
||||
bool init(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, const std::string& shard1, int max_t,
|
||||
@@ -330,6 +337,18 @@ private:
|
||||
const mtl::Buffer* membp_ = nullptr; // mtp_encode's embedding rows (nullptr: memb_)
|
||||
const mtl::Buffer* mpsrc_ = nullptr; // the drafter's prompt rows (nullptr: R_)
|
||||
bool coop_k4();
|
||||
// the vision path: the embedding row of an absolute position (an image row, or null), the M-RoPE table rows a
|
||||
// window reads copied into mrope_ (qsa_prep's buffer 20, function constant 24)
|
||||
const float* img_row(int64_t pos) const { return g_embd_rows ? g_embd_rows[pos] : nullptr; }
|
||||
mtl::Buffer mrope_;
|
||||
bool mrope_on() const { return g_mrope_host != nullptr; }
|
||||
void mrope_sync(int64_t c0, int64_t n) {
|
||||
if (!mrope_on()) return;
|
||||
const int64_t a = std::max<int64_t>(0, c0 & ~(int64_t) 3), b = std::min<int64_t>(max_cells_, c0 + n);
|
||||
if (b > a) std::memcpy(mrope_.as<int32_t>() + a * 3, g_mrope_host + a * 3, (size_t) (b - a) * 12);
|
||||
}
|
||||
mtl::Constants qsa_c() const { return mrope_on() ? mtl::Constants().b(24, true) : mtl::Constants(); }
|
||||
std::string qsa_k() const { return mrope_on() ? "qsa_prep#mrope" : "qsa_prep"; }
|
||||
int m5_layout_ = -1; // the M5's matmul2d layout (coop_k4), asked once: the M4's tiles differ
|
||||
bool m5_layout() { if (m5_layout_ < 0) m5_layout_ = coop_k4() ? 1 : 0; return m5_layout_ == 1; }
|
||||
bool pc_setup(int C);
|
||||
@@ -391,6 +410,7 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta
|
||||
qkvL_ = A(4 * (size_t) 36 * MAXT * 10240); alL_ = A(4 * (size_t) 36 * MAXT * 48); beL_ = A(4 * (size_t) 36 * MAXT * 48);
|
||||
zL_ = A(4 * (size_t) 36 * MAXT * 6144); ydump_ = A(4 * MAXT * 6144);
|
||||
keyn_ = A(4 * MAXT * 10240); qn_ = A(4 * MAXT * 10240); gated_ = A(4 * MAXT * 10240); normW_ = A(4 * MAXT * 10240);
|
||||
mrope_ = A(12 * (size_t) (MAXC + 16));
|
||||
pleHist_ = A(4 * 9 * 10240);
|
||||
auto act = [&](int n) { Act a; a.n = n; a.b = A(4 * ((size_t) 4 * MAXT * n + (size_t) MAXT * n / 16)); return a; };
|
||||
XA_ = act(N); XF_ = act(N); XY_ = act(6144); XQ_ = act(6144); XH_ = act(NFF); XE_ = act(N);
|
||||
@@ -621,14 +641,14 @@ void Window::layer(std::vector<mtl::Dispatch>& ds, int l, int ntok, int64_t pos0
|
||||
gemv(ds, T(b + "indexer.k_proj.weight"), XA_, ikr_, 128, ntok);
|
||||
QsaArgs a{(uint32_t) pos0, 0, (uint32_t) max_cells_, (uint32_t) ntok, eps_};
|
||||
mtl::Dispatch d;
|
||||
d.p = P(lqsa_, "qsa_prep", {}, "qsa_prep");
|
||||
d.p = P(lqsa_, "qsa_prep", qsa_c(), qsa_k());
|
||||
d.args = {mtl::Arg::B(qfull_), mtl::Arg::B(kcur_), mtl::Arg::B(vcur_), mtl::Arg::B(iqr_), mtl::Arg::B(ikr_),
|
||||
mtl::Arg::B(W_, T(b + "attn_q_norm.weight").offset), mtl::Arg::B(W_, T(b + "attn_k_norm.weight").offset),
|
||||
mtl::Arg::B(W_, T(b + "indexer.q_norm.weight").offset), mtl::Arg::B(W_, T(b + "indexer.k_norm.weight").offset),
|
||||
mtl::Arg::B(cs_), mtl::Arg::B(qcur_), mtl::Arg::B(qidx_), mtl::Arg::B(kq_[(size_t) qi]),
|
||||
mtl::Arg::B(vq_[(size_t) qi]), mtl::Arg::B(ksc_[(size_t) qi]), mtl::Arg::B(vsc_[(size_t) qi]),
|
||||
mtl::Arg::B(tail_[(size_t) qi]), mtl::Arg::B(dead_[(size_t) qi]), mtl::Arg::B(pooled_[(size_t) qi]),
|
||||
mtl::Arg::V(a)};
|
||||
mtl::Arg::V(a), mtl::Arg::B(mrope_)};
|
||||
d.grid[0] = 33;
|
||||
d.grid[1] = ntok;
|
||||
d.tg[0] = 256;
|
||||
@@ -874,7 +894,8 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t
|
||||
const size_t rb = ref::row_bytes(te.type, N);
|
||||
for (int t = 0; t < Tn; ++t) {
|
||||
float* r = R_.as<float>() + (size_t) t * HC * N;
|
||||
ref::dequant(te.type, (const uint8_t*) W_.ptr + te.offset + (size_t) tokens[t] * rb, r, N);
|
||||
if (const float* img = img_row(pos0 + t)) std::memcpy(r, img, (size_t) N * 4);
|
||||
else ref::dequant(te.type, (const uint8_t*) W_.ptr + te.offset + (size_t) tokens[t] * rb, r, N);
|
||||
for (int c = 1; c < HC; ++c) std::memcpy(r + (size_t) c * N, r, (size_t) N * 4);
|
||||
}
|
||||
// ---- the PLE rows (the table read is the shared host code's), projected on the GPU
|
||||
@@ -897,7 +918,9 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t
|
||||
for (int c = 0; c < 2051; ++c) qids_.as<int>()[t * 2051 + c] = c;
|
||||
qdirty_ = false;
|
||||
}
|
||||
const int key = ((((Tn * 2 + (head ? 1 : 0)) * 2 + hp_) * (MAXT + 1)) + pend_fold_) * 2 + (sel ? 1 : 0);
|
||||
const int key = (((((Tn * 2 + (head ? 1 : 0)) * 2 + hp_) * (MAXT + 1)) + pend_fold_) * 2 + (sel ? 1 : 0)) * 2 +
|
||||
(mrope_on() ? 1 : 0);
|
||||
mrope_sync(pos0, Tn);
|
||||
const int fold_n = pend_fold_;
|
||||
pend_fold_ = 0;
|
||||
win_head_ = head;
|
||||
@@ -908,7 +931,11 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t
|
||||
for (const auto& [i, kind] : c.qsa) {
|
||||
mtl::Dispatch& d = c.ds[(size_t) i];
|
||||
QsaArgs a{(uint32_t) pos0, 0, (uint32_t) max_cells_, (uint32_t) (kind == 1 ? MAXT : Tn), eps_};
|
||||
std::memcpy(d.args.back().bytes.data(), &a, sizeof a);
|
||||
// the QsaArgs: the dispatch's last inline argument (qsa_prep has the M-RoPE table bound after it)
|
||||
mtl::Arg* qa = nullptr;
|
||||
for (auto& g : d.args)
|
||||
if (!g.bytes.empty()) qa = &g;
|
||||
std::memcpy(qa->bytes.data(), &a, sizeof a);
|
||||
if (kind == 2) d.grid[0] = (uint32_t) (((pos0 + Tn) / 4 + 1 + 7) / 8);
|
||||
}
|
||||
seg_T_ = Tn;
|
||||
@@ -1574,14 +1601,14 @@ void Window::mtp_encode(std::vector<mtl::Dispatch>& ds, const mtl::Buffer& srcR,
|
||||
QsaArgs qa{(uint32_t) cell0, 0, (uint32_t) max_cells_, (uint32_t) n, eps_};
|
||||
{
|
||||
mtl::Dispatch d;
|
||||
d.p = P(lqsa_, "qsa_prep", {}, "qsa_prep");
|
||||
d.p = P(lqsa_, "qsa_prep", qsa_c(), qsa_k());
|
||||
d.args = {mtl::Arg::B(qfull_), mtl::Arg::B(kcur_), mtl::Arg::B(vcur_), mtl::Arg::B(midx_), mtl::Arg::B(midx_, 512 * 4),
|
||||
mtl::Arg::B(mnorm_, NM(L + "self_attn.q_norm.weight")), mtl::Arg::B(mnorm_, NM(L + "self_attn.k_norm.weight")),
|
||||
mtl::Arg::B(mnorm_, NM(L + "self_attn.indexer.q_layernorm.weight")),
|
||||
mtl::Arg::B(mnorm_, NM(L + "self_attn.indexer.k_layernorm.weight")),
|
||||
mtl::Arg::B(cs_), mtl::Arg::B(qcur_), mtl::Arg::B(qidx_), mtl::Arg::B(mkq_[0]), mtl::Arg::B(mvq_[0]),
|
||||
mtl::Arg::B(mksc_[0]), mtl::Arg::B(mvsc_[0]), mtl::Arg::B(mtail_[0]), mtl::Arg::B(mdead_[0]),
|
||||
mtl::Arg::B(mpooled_[0]), mtl::Arg::V(qa)};
|
||||
mtl::Arg::B(mpooled_[0]), mtl::Arg::V(qa), mtl::Arg::B(mrope_)};
|
||||
d.grid[0] = 33; d.grid[1] = n; d.tg[0] = 256;
|
||||
push(ds, d);
|
||||
}
|
||||
@@ -1699,6 +1726,7 @@ void Window::mtp_encode(std::vector<mtl::Dispatch>& ds, const mtl::Buffer& srcR,
|
||||
// the prompt's K/V: cell cell0 + i pairs the main window's final residual row i with the token at cell0 + i + 1
|
||||
bool Window::mtp_prefill(const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err) {
|
||||
if (!mtp_on_) { err = "metal mtp: not loaded"; return false; }
|
||||
mrope_sync(cell0, n);
|
||||
// every group of MAXT rows in one command buffer: the embedding rows of all of them in mpemb_ (row c + i)
|
||||
if ((int64_t) mpemb_rows_ < n) {
|
||||
if (mpemb_rows_ > 0) cx_->free(mpemb_);
|
||||
@@ -1747,6 +1775,7 @@ bool Window::mtp_draft(int Tn, const int32_t* tokens, int64_t p, int a, int max_
|
||||
float min_p, int* n_drafts, std::string& err) {
|
||||
if (!mtp_on_) { err = "metal mtp: not loaded"; return false; }
|
||||
(void) Tn;
|
||||
mrope_sync(p, a + max_steps + 1);
|
||||
int n = 0;
|
||||
int32_t tok = tokens[a];
|
||||
if (chain_ && max_steps > 0) {
|
||||
@@ -1956,9 +1985,11 @@ bool Window::prompt_chunk(const int32_t* tokens, int Tn, int64_t pos0, std::stri
|
||||
{
|
||||
const gguf::Tensor& te = TN("token_embd.weight");
|
||||
const size_t rb = ref::row_bytes(te.type, N);
|
||||
mrope_sync(pos0, T);
|
||||
for (int t = 0; t < T; ++t) {
|
||||
float* r = p.R.as<float>() + (size_t) t * HC * N;
|
||||
ref::dequant(te.type, (const uint8_t*) W_.ptr + te.offset + (size_t) tokens[t] * rb, r, N);
|
||||
if (const float* img = img_row(pos0 + t)) std::memcpy(r, img, (size_t) N * 4);
|
||||
else ref::dequant(te.type, (const uint8_t*) W_.ptr + te.offset + (size_t) tokens[t] * rb, r, N);
|
||||
for (int c = 1; c < HC; ++c) std::memcpy(r + (size_t) c * N, r, (size_t) N * 4);
|
||||
}
|
||||
}
|
||||
@@ -2077,13 +2108,13 @@ bool Window::prompt_chunk(const int32_t* tokens, int Tn, int64_t pos0, std::stri
|
||||
gemm(p.mixh, p.Wiq, p.iqr, T, 512, N);
|
||||
gemm(p.mixh, p.Wik, p.ikr, T, 128, N);
|
||||
QsaArgs a{(uint32_t) pos0, 0, (uint32_t) max_cells_, (uint32_t) T, eps_};
|
||||
D(PP(p.lqsa, "qsa_prep"),
|
||||
D(PP(p.lqsa, "qsa_prep", qsa_c(), qsa_k()),
|
||||
{mtl::Arg::B(p.qfull), mtl::Arg::B(p.kcur), mtl::Arg::B(p.vcur), mtl::Arg::B(p.iqr), mtl::Arg::B(p.ikr),
|
||||
mtl::Arg::B(W_, TN(b + "attn_q_norm.weight").offset), mtl::Arg::B(W_, TN(b + "attn_k_norm.weight").offset),
|
||||
mtl::Arg::B(W_, TN(b + "indexer.q_norm.weight").offset), mtl::Arg::B(W_, TN(b + "indexer.k_norm.weight").offset),
|
||||
mtl::Arg::B(cs_), mtl::Arg::B(p.qcur), mtl::Arg::B(p.qidx), mtl::Arg::B(kq_[(size_t) qi]), mtl::Arg::B(vq_[(size_t) qi]),
|
||||
mtl::Arg::B(ksc_[(size_t) qi]), mtl::Arg::B(vsc_[(size_t) qi]), mtl::Arg::B(tail_[(size_t) qi]),
|
||||
mtl::Arg::B(dead_[(size_t) qi]), mtl::Arg::B(pooled_[(size_t) qi]), mtl::Arg::V(a)}, 33, T, 256);
|
||||
mtl::Arg::B(dead_[(size_t) qi]), mtl::Arg::B(pooled_[(size_t) qi]), mtl::Arg::V(a), mtl::Arg::B(mrope_)}, 33, T, 256);
|
||||
if (pos0 + T > 2051)
|
||||
D(PP(p.lqsa, "qsa_score_mm"), {mtl::Arg::B(p.qidx), mtl::Arg::B(pooled_[(size_t) qi]), mtl::Arg::B(dead_[(size_t) qi]),
|
||||
mtl::Arg::B(p.scores), mtl::Arg::V(a)},
|
||||
@@ -2368,8 +2399,18 @@ bool mtp_load_pending(Window* w, std::string& err) {
|
||||
}
|
||||
// Prefill::run's Metal side: the prompt read through the window, MAXT rows at a time (no head), each window
|
||||
// committed; on_chunk (the MTP's K/V) after each, with the window's final residual rows in place.
|
||||
bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0,
|
||||
bool prompt_run_rows(const int64_t* tokens, int64_t n, int64_t pos0,
|
||||
const std::function<bool(const float*, int64_t, int64_t, std::string&)>& on_chunk, std::string& err);
|
||||
bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0, const float* const* embd_rows,
|
||||
const std::function<bool(const float*, int64_t, int64_t, std::string&)>& on_chunk, std::string& err) {
|
||||
struct Rows {
|
||||
explicit Rows(const float* const* r) { g_embd_rows = r; }
|
||||
~Rows() { g_embd_rows = nullptr; }
|
||||
} rows(embd_rows);
|
||||
return prompt_run_rows(tokens, n, pos0, on_chunk, err);
|
||||
}
|
||||
bool prompt_run_rows(const int64_t* tokens, int64_t n, int64_t pos0,
|
||||
const std::function<bool(const float*, int64_t, int64_t, std::string&)>& on_chunk, std::string& err) {
|
||||
if (g_window == nullptr) { err = "metal prompt: the decode window is not up yet"; return false; }
|
||||
static const bool windows = std::getenv("STRATA_METAL_PROMPT") && std::string(std::getenv("STRATA_METAL_PROMPT")) == "window";
|
||||
static const int64_t min_rows = std::getenv("STRATA_METAL_PROMPT_MIN") ? std::atoll(std::getenv("STRATA_METAL_PROMPT_MIN")) : 32;
|
||||
@@ -2418,3 +2459,15 @@ bool mtp_draft(int T, const int32_t* tokens, int64_t p, int a, int max_steps, in
|
||||
#include "prompt_chain.inc"
|
||||
|
||||
} // namespace strata::metal_engine
|
||||
namespace strata::gpu { void* host_view(const void* device_pointer); }
|
||||
namespace strata::kernels {
|
||||
void mrope_table_set(const int32_t* device_table) {
|
||||
strata::metal_engine::g_mrope_dev = device_table;
|
||||
strata::metal_engine::g_mrope_host =
|
||||
device_table ? (const int32_t*) strata::gpu::host_view(device_table) : nullptr;
|
||||
}
|
||||
const int32_t* mrope_table() { return strata::metal_engine::g_mrope_dev; }
|
||||
} // namespace strata::kernels
|
||||
namespace strata::metal_engine {
|
||||
|
||||
} // namespace strata::metal_engine
|
||||
@@ -101,7 +101,7 @@ void experts_native(const Batch&, const NativeGeom&, int, int64_t, const void*,
|
||||
// hands over, draft_kv declines (the MTP hook reads the window's rows itself).
|
||||
namespace strata::metal_engine {
|
||||
bool window_ensure(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, int64_t max_cells, std::string& err);
|
||||
bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0,
|
||||
bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0, const float* const* embd_rows,
|
||||
const std::function<bool(const float*, int64_t, int64_t, std::string&)>& on_chunk, std::string& err);
|
||||
} // namespace strata::metal_engine
|
||||
#endif
|
||||
@@ -4266,7 +4266,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
#if defined(STRATA_GPU_METAL)
|
||||
{
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
const bool ok = strata::metal_engine::prompt_run(tokens, n, pos0, on_chunk, err);
|
||||
const bool ok = strata::metal_engine::prompt_run(tokens, n, pos0, embd_rows, on_chunk, err);
|
||||
stats_.tokens += n;
|
||||
stats_.chunks += 1;
|
||||
stats_.ms_total += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
|
||||
|
||||
@@ -99,6 +99,14 @@ class Config(unittest.TestCase):
|
||||
self.assertTrue(cfg["exe"] and isinstance(cfg["args"], list))
|
||||
|
||||
|
||||
class Vision(unittest.TestCase):
|
||||
def test_the_projector_is_pinned_and_the_encoder_builds_with_metal(self):
|
||||
self.assertIn("mmproj-Qwen3.8-Flash-Next-BF16.gguf", M.VISION_SHA256)
|
||||
src = (ROOT / "metal" / "setup_mac.py").read_text(encoding="utf-8")
|
||||
self.assertIn('"-DSTRATA_VISION_CUDA=OFF"', src)
|
||||
self.assertIn('cfg["args"] += ["--vision"]', src)
|
||||
|
||||
|
||||
class HandOver(unittest.TestCase):
|
||||
def test_darwin_hands_the_run_to_setup_mac(self):
|
||||
with mock.patch.object(setup.sys, "platform", "darwin"), \
|
||||
|
||||
Reference in new issue
Block a user