Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

metal: hc_split ported - the hyper-connection family is complete on the Mac

Niko12214735e381ad10
diff --git a/metal/include/metal_compat/ds4_metal_host.h b/metal/include/metal_compat/ds4_metal_host.h
index c9c47fe39..fba08b9af 100644
--- a/metal/include/metal_compat/ds4_metal_host.h
+++ b/metal/include/metal_compat/ds4_metal_host.h
@@ -32,6 +32,10 @@ struct FinishIn {
uint8_t* pdst;
long long pdst_stride;
};
+// ds4_kernels.cu:1908 hc_split (the sinkhorn normalisation)
+bool hc_split(const float* mixes, const float* scale, const float* base, int hc, int iters, float hc_eps, int T,
+ float* pre, float* post, float* comb);
+
// ds4_kernels.cu:1901 hc_mix
bool hc_mix(const float* h, const float* fn, int hcdim, int n_mix, int T, float eps, float* mixes);
diff --git a/metal/src/engine/ds4_finish.cpp b/metal/src/engine/ds4_finish.cpp
index 654bb9738..37a5fe52e 100644
--- a/metal/src/engine/ds4_finish.cpp
+++ b/metal/src/engine/ds4_finish.cpp
@@ -198,4 +198,46 @@ bool hc_mix(const float* h, const float* fn, int hcdim, int n_mix, int T, float
return true;
}
+
+// ds4_kernels.cu:1908 - hc_split: the sinkhorn normalisation (scalar path; see the MSL comment)
+bool hc_split(const float* mixes, const float* scale, const float* base, int hc, int iters, float hc_eps, int T,
+ float* pre, float* post, float* comb) {
+ if (!ensure()) return false;
+ void* hm = strata::gpu::host_view((void*) mixes);
+ void* hp = strata::gpu::host_view((void*) pre);
+ void* ho = strata::gpu::host_view((void*) post);
+ void* hc_ = strata::gpu::host_view((void*) comb);
+ if (!hm || !hp || !ho || !hc_) { std::fprintf(stderr, "dsv41 metal: hc_split has no host view\n"); return false; }
+ const size_t page = 16384;
+ auto wrap = [&](void* host, size_t bytes) {
+ return g_cx->wrap((void*) (((uintptr_t) host) & ~(uintptr_t) (page - 1)), ((bytes + page - 1) / page) * page);
+ };
+ auto off = [&](void* host) { return (size_t) ((uintptr_t) host - (((uintptr_t) host) & ~(uintptr_t) (page - 1))); };
+ mtl::Buffer bm = wrap(hm, (size_t) T * 32 * 4 + 65536);
+ mtl::Buffer bp = wrap(hp, (size_t) T * 8 * 4 + 65536);
+ mtl::Buffer bo = wrap(ho, (size_t) T * 8 * 4 + 65536);
+ mtl::Buffer bc = wrap(hc_, (size_t) T * 64 * 4 + 65536);
+ if (!bm.handle || !bp.handle || !bo.handle || !bc.handle) return false;
+ // scale and base come from the engine like every other operand: reach them through host_view, not as raw host pointers
+ void* hs = strata::gpu::host_view((void*) scale);
+ void* hb = strata::gpu::host_view((void*) base);
+ if (!hs || !hb) { std::fprintf(stderr, "dsv41 metal: hc_split scale/base have no host view\n"); return false; }
+ const size_t nbase = (size_t) (2 * hc + hc * hc);
+ mtl::Buffer bs = wrap(hs, 256);
+ mtl::Buffer bb = wrap(hb, (nbase * 4 + 65535) / 65536 * 65536 + 65536);
+ if (!bs.handle || !bb.handle) return false;
+ mtl::Pipeline pl;
+ try { pl = g_cx->pipeline(g_lib, "ds4_hc_split"); } catch (...) { return false; }
+ mtl::Dispatch d;
+ d.p = &pl;
+ d.args = {mtl::Arg::B(bm, off(hm)), mtl::Arg::B(bs, off(hs)), mtl::Arg::B(bb, off(hb)), mtl::Arg::B(bp, off(hp)),
+ mtl::Arg::B(bo, off(ho)), mtl::Arg::B(bc, off(hc_)), mtl::Arg::V((unsigned) hc),
+ mtl::Arg::V((unsigned) iters), mtl::Arg::V((float) hc_eps)};
+ d.grid[0] = (uint32_t) T;
+ d.grid[1] = d.grid[2] = 1;
+ d.tg[0] = 32; d.tg[1] = d.tg[2] = 1;
+ g_cx->run({d});
+ return true;
+}
+
} // namespace strata::dsv41::metal_host
diff --git a/metal/src/engine/msl/ds4_init.metal b/metal/src/engine/msl/ds4_init.metal
index e0013e729..6e7094c90 100644
--- a/metal/src/engine/msl/ds4_init.metal
+++ b/metal/src/engine/msl/ds4_init.metal
@@ -173,3 +173,69 @@ kernel void ds4_hc_mix(device const float* h [[buffer(0)]], device const float*
ss = ds4_block_sum(ss, red, lane, w);
if (tid == 0) mixes[(size_t) t * 32u + m] = acc * rsqrt(ss / float(hcdim) + eps);
}
+
+// ds4_kernels.cu:268 hc_split_kernel - the sinkhorn normalisation of the hyper-connection mix.
+// This is the SCALAR path of the CUDA kernel (lane 0 of the threadgroup does the token, a local 8x8 matrix), applied for every
+// hc including 4: it computes the same mathematics as the warp-lane version, only without the shuffles. Correctness first -
+// the lane-parallel hc == 4 version can be added behind a profile.
+kernel void ds4_hc_split(device const float* mixes [[buffer(0)]], device const float* scale [[buffer(1)]],
+ device const float* base [[buffer(2)]], device float* pre [[buffer(3)]],
+ device float* post [[buffer(4)]], device float* comb [[buffer(5)]],
+ constant uint& hc [[buffer(6)]], constant uint& iters [[buffer(7)]],
+ constant float& eps [[buffer(8)]], uint b [[threadgroup_position_in_grid]],
+ uint lane [[thread_index_in_threadgroup]]) {
+ if (lane != 0u) return;
+ const device float* mx = mixes + (size_t) b * 32u;
+ device float* pr = pre + (size_t) b * 8u;
+ device float* po = post + (size_t) b * 8u;
+ device float* cb = comb + (size_t) b * 64u;
+ for (uint i = 0; i < 8u; ++i) {
+ if (i >= hc) break;
+ pr[i] = 1.0f / (1.0f + exp(-(mx[i] * scale[0] + base[i]))) + eps;
+ po[i] = 2.0f / (1.0f + exp(-(mx[hc + i] * scale[1] + base[hc + i])));
+ }
+ float c[8][8];
+ for (uint j = 0; j < 8u; ++j) {
+ if (j >= hc) break;
+ float m = -3.402823466e+38f;
+ for (uint k = 0; k < 8u; ++k) {
+ if (k >= hc) break;
+ c[j][k] = mx[2u * hc + j * hc + k] * scale[2] + base[2u * hc + j * hc + k];
+ m = fmax(m, c[j][k]);
+ }
+ float s = 0.0f;
+ for (uint k = 0; k < 8u; ++k) {
+ if (k >= hc) break;
+ c[j][k] = exp(c[j][k] - m);
+ s += c[j][k];
+ }
+ for (uint k = 0; k < 8u; ++k) {
+ if (k >= hc) break;
+ c[j][k] = c[j][k] / s + eps;
+ }
+ }
+ for (uint k = 0; k < 8u; ++k) {
+ if (k >= hc) break;
+ float s = 0.0f;
+ for (uint j = 0; j < 8u; ++j) if (j < hc) s += c[j][k];
+ for (uint j = 0; j < 8u; ++j) if (j < hc) c[j][k] /= s + eps;
+ }
+ for (uint it = 1; it < iters; ++it) {
+ for (uint j = 0; j < 8u; ++j) {
+ if (j >= hc) break;
+ float s = 0.0f;
+ for (uint k = 0; k < 8u; ++k) if (k < hc) s += c[j][k];
+ for (uint k = 0; k < 8u; ++k) if (k < hc) c[j][k] /= s + eps;
+ }
+ for (uint k = 0; k < 8u; ++k) {
+ if (k >= hc) break;
+ float s = 0.0f;
+ for (uint j = 0; j < 8u; ++j) if (j < hc) s += c[j][k];
+ for (uint j = 0; j < 8u; ++j) if (j < hc) c[j][k] /= s + eps;
+ }
+ }
+ for (uint j = 0; j < 8u; ++j) {
+ if (j >= hc) break;
+ for (uint k = 0; k < 8u; ++k) { if (k >= hc) break; cb[j * hc + k] = c[j][k]; }
+ }
+}
diff --git a/src/dsv41/ds4_kernels.cu b/src/dsv41/ds4_kernels.cu
index 342ed98e1..e387ae1f4 100644
--- a/src/dsv41/ds4_kernels.cu
+++ b/src/dsv41/ds4_kernels.cu
@@ -1905,6 +1905,9 @@ void hc_mix(const float* h, const float* fn, int hcdim, int n_mix, int T, float
}
void hc_split(const float* mixes, const float* scale, const float* base, int hc, int iters, float hc_eps, int T,
float* pre, float* post, float* comb, cudaStream_t s) {
+#if defined(STRATA_METAL)
+ if (strata::dsv41::metal_host::hc_split(mixes, scale, base, hc, iters, hc_eps, T, pre, post, comb)) return;
+#endif
strata_ds4_launch(hc_split_kernel, T, 32, 0, s, mixes, scale, base, hc, iters, hc_eps, pre, post, comb);
}
void hc_pre_norm(const float* h, const float* mix, const float* w, int dim, int hc, int T, float eps, float* x,