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++
Images (I2): NVIDIA mma.sync flash attention for the native runner, fused q/k/v GEMM
Niko12215a30c2c4e8b7
diff --git a/tools/image/sdcpp_native/qi21-native.cu b/tools/image/sdcpp_native/qi21-native.cuindex de00d55d3..b263d1e0b 100644--- a/tools/image/sdcpp_native/qi21-native.cu+++ b/tools/image/sdcpp_native/qi21-native.cu@@ -36,6 +36,52 @@ __device__ __forceinline__ float qi_block_sum(float v, float* red) {return red[0];}+// x += h * tg (tg = tanh(gate), precomputed once per step), then out = LN(x) * (1 + m); float4, 256 threads, C % 1024 == 0+template <bool RES, bool LN>+__global__ void __launch_bounds__(256) k_res_ln_mod4(float* __restrict__ x, const float* __restrict__ h,+ const float* __restrict__ tg, float* __restrict__ out,+ const float* __restrict__ m, int C, float eps) {+ __shared__ float red[32];+ const size_t row = blockIdx.x;+ float4* xr = reinterpret_cast<float4*>(x + row * C);+ const float4* hr = reinterpret_cast<const float4*>(h + row * C);+ const float4* t4 = reinterpret_cast<const float4*>(tg);+ float4 v[8];+ int n = 0;+ float s = 0.f;+ for (int c = threadIdx.x; c < C / 4; c += blockDim.x) {+ float4 a = xr[c];+ if (RES) {+ const float4 hh = hr[c], gg = t4[c];+ a.x += hh.x * gg.x; a.y += hh.y * gg.y; a.z += hh.z * gg.z; a.w += hh.w * gg.w;+ xr[c] = a;+ }+ v[n++] = a;+ s += (a.x + a.y) + (a.z + a.w);+ }+ if (!LN) return;+ const float mean = qi_block_sum(s, red) / C;+ float q = 0.f;+ for (int i = 0; i < n; ++i) {+ const float dx = v[i].x - mean, dy = v[i].y - mean, dz = v[i].z - mean, dw = v[i].w - mean;+ q += (dx * dx + dy * dy) + (dz * dz + dw * dw);+ }+ const float inv = rsqrtf(qi_block_sum(q, red) / C + eps);+ const float4* m4 = reinterpret_cast<const float4*>(m);+ float4* o4 = reinterpret_cast<float4*>(out + row * C);+ n = 0;+ for (int c = threadIdx.x; c < C / 4; c += blockDim.x) {+ const float4 mm = m4[c], a = v[n++];+ o4[c] = make_float4((a.x - mean) * inv * (1.f + mm.x), (a.y - mean) * inv * (1.f + mm.y),+ (a.z - mean) * inv * (1.f + mm.z), (a.w - mean) * inv * (1.f + mm.w));+ }+}++__global__ void k_tanh(const float* __restrict__ in, float* __restrict__ out, int n) {+ const int i = blockIdx.x * blockDim.x + threadIdx.x;+ if (i < n) out[i] = tanhf(in[i]);+}+template <bool RES, bool LN>__global__ void __launch_bounds__(256) k_res_ln_mod(float* __restrict__ x, const float* __restrict__ h,const float* __restrict__ g, float* __restrict__ out,@@ -325,6 +371,173 @@ __global__ void __launch_bounds__(FA_NW * 32) k_attn_wmma(const _Float16* __rest}#endif // GGML_USE_HIP+#if !defined(GGML_USE_HIP)+// NVIDIA (sm_80+): mma.sync m16n8k16 f16 -> f32 flash attention. 8 warps x 16 queries per block, 64-key tiles of K+// and V (row-major) double-buffered with cp.async, ldmatrix (.trans for V) fragments, fp32 online softmax, P in fp16.+// q fp16 head-major [H][q_tstride][128], k/v fp16 head-major [H][kv_tstride][128], out fp32 token-major [nq][H*128].+constexpr int MF_NW = 8, MF_BQ = MF_NW * 16, MF_BKV = 64, MF_LD = QI_D + 8;+constexpr size_t MF_SMEM = (size_t) 4 * MF_BKV * MF_LD * sizeof(half);++__device__ __forceinline__ void qi_mma16816(float* c, const uint32_t* a, const uint32_t* b) {+#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800+ asm volatile("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%0,%1,%2,%3};\n"+ : "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3])+ : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b[0]), "r"(b[1]));+#else+ (void) c; (void) a; (void) b;+ __trap();+#endif+}+__device__ __forceinline__ uint32_t qi_pack2(float x, float y) {+ half2 h = __floats2half2_rn(x, y);+ return *reinterpret_cast<uint32_t*>(&h);+}+__device__ __forceinline__ void qi_ldsm_x4(uint32_t* r, const half* p, bool trans) {+#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800+ const uint32_t a = (uint32_t) __cvta_generic_to_shared(p);+ if (trans)+ asm volatile("ldmatrix.sync.aligned.m8n8.x4.trans.shared.b16 {%0,%1,%2,%3}, [%4];\n"+ : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3]) : "r"(a));+ else+ asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n"+ : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3]) : "r"(a));+#else+ (void) r; (void) p; (void) trans;+#endif+}+__device__ __forceinline__ void qi_cp16(half* dst, const half* src, bool ok) {+#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800+ const uint32_t a = (uint32_t) __cvta_generic_to_shared(dst);+ const int n = ok ? 16 : 0;+ asm volatile("cp.async.cg.shared.global [%0], [%1], 16, %2;\n" :: "r"(a), "l"(src), "r"(n));+#else+ (void) dst; (void) src; (void) ok;+#endif+}++__global__ void __launch_bounds__(MF_NW * 32) k_attn_mma(const half* __restrict__ Q, int q_tstride,+ const half* __restrict__ K, const half* __restrict__ V,+ int kv_tstride, float* __restrict__ O, int H, int nq, int nkv,+ float scale_log2) {+#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800+ extern __shared__ __align__(16) half qi_smem[];+ const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;+ const int r = lane >> 2, c2 = (lane & 3) * 2, lr = lane & 7, lq = lane >> 3;+ const int head = blockIdx.y, q0 = blockIdx.x * MF_BQ + warp * 16;+ const half* Qh = Q + (size_t) head * q_tstride * QI_D;+ const half* Kh = K + (size_t) head * kv_tstride * QI_D;+ const half* Vh = V + (size_t) head * kv_tstride * QI_D;+ auto tile = [&](int st, int kv) { return qi_smem + ((st * 2 + kv) * MF_BKV) * MF_LD; };+ auto load = [&](int st, int k0) {+ for (int e = tid * 8; e < MF_BKV * QI_D; e += MF_NW * 32 * 8) {+ const int rr = e / QI_D, cc = e % QI_D, key = k0 + rr;+ const bool ok = key < nkv;+ const size_t off = (size_t) (ok ? key : 0) * QI_D + cc;+ qi_cp16(tile(st, 0) + rr * MF_LD + cc, Kh + off, ok);+ qi_cp16(tile(st, 1) + rr * MF_LD + cc, Vh + off, ok);+ }+ asm volatile("cp.async.commit_group;\n");+ };+ uint32_t qa[QI_D / 16][4];+ {+ const int r0 = min(q0 + r, nq - 1), r1 = min(q0 + r + 8, nq - 1);+ for (int kc = 0; kc < QI_D / 16; ++kc) {+ qa[kc][0] = *reinterpret_cast<const uint32_t*>(Qh + (size_t) r0 * QI_D + kc * 16 + c2);+ qa[kc][1] = *reinterpret_cast<const uint32_t*>(Qh + (size_t) r1 * QI_D + kc * 16 + c2);+ qa[kc][2] = *reinterpret_cast<const uint32_t*>(Qh + (size_t) r0 * QI_D + kc * 16 + c2 + 8);+ qa[kc][3] = *reinterpret_cast<const uint32_t*>(Qh + (size_t) r1 * QI_D + kc * 16 + c2 + 8);+ }+ }+ float o[QI_D / 8][4];+ for (int n = 0; n < QI_D / 8; ++n) o[n][0] = o[n][1] = o[n][2] = o[n][3] = 0.f;+ float m0 = -INFINITY, m1 = -INFINITY, l0 = 0.f, l1 = 0.f;+ const int ntiles = (nkv + MF_BKV - 1) / MF_BKV;+ load(0, 0);+ for (int it = 0; it < ntiles; ++it) {+ const int st = it & 1, k0 = it * MF_BKV;+ if (it + 1 < ntiles) {+ load(st ^ 1, k0 + MF_BKV);+ asm volatile("cp.async.wait_group 1;\n");+ } else {+ asm volatile("cp.async.wait_group 0;\n");+ }+ __syncthreads();+ const half* sK = tile(st, 0);+ const half* sV = tile(st, 1);+ float s[MF_BKV / 8][4];+ for (int t = 0; t < MF_BKV / 8; ++t) {+ s[t][0] = s[t][1] = s[t][2] = s[t][3] = 0.f;+ for (int kc = 0; kc < QI_D / 16; kc += 2) {+ uint32_t b[4];+ qi_ldsm_x4(b, sK + (t * 8 + lr) * MF_LD + kc * 16 + lq * 8, false);+ qi_mma16816(s[t], qa[kc], b);+ qi_mma16816(s[t], qa[kc + 1], b + 2);+ }+ }+ float mx0 = -INFINITY, mx1 = -INFINITY;+ for (int t = 0; t < MF_BKV / 8; ++t)+ for (int j = 0; j < 2; ++j) {+ const bool ok = k0 + t * 8 + c2 + j < nkv;+ s[t][j] = ok ? s[t][j] * scale_log2 : -INFINITY;+ s[t][2 + j] = ok ? s[t][2 + j] * scale_log2 : -INFINITY;+ mx0 = fmaxf(mx0, s[t][j]);+ mx1 = fmaxf(mx1, s[t][2 + j]);+ }+ for (int off = 1; off < 4; off <<= 1) {+ mx0 = fmaxf(mx0, __shfl_xor_sync(0xffffffff, mx0, off));+ mx1 = fmaxf(mx1, __shfl_xor_sync(0xffffffff, mx1, off));+ }+ const float mn0 = fmaxf(m0, mx0), mn1 = fmaxf(m1, mx1);+ const float a0 = exp2f(m0 - mn0), a1 = exp2f(m1 - mn1);+ float sum0 = 0.f, sum1 = 0.f;+ for (int t = 0; t < MF_BKV / 8; ++t)+ for (int j = 0; j < 2; ++j) {+ s[t][j] = exp2f(s[t][j] - mn0);+ s[t][2 + j] = exp2f(s[t][2 + j] - mn1);+ sum0 += s[t][j];+ sum1 += s[t][2 + j];+ }+ for (int off = 1; off < 4; off <<= 1) {+ sum0 += __shfl_xor_sync(0xffffffff, sum0, off);+ sum1 += __shfl_xor_sync(0xffffffff, sum1, off);+ }+ l0 = l0 * a0 + sum0;+ l1 = l1 * a1 + sum1;+ m0 = mn0;+ m1 = mn1;+ for (int n = 0; n < QI_D / 8; ++n) {+ o[n][0] *= a0; o[n][1] *= a0; o[n][2] *= a1; o[n][3] *= a1;+ }+ for (int kc = 0; kc < MF_BKV / 16; ++kc) {+ uint32_t pa[4];+ pa[0] = qi_pack2(s[2 * kc][0], s[2 * kc][1]);+ pa[1] = qi_pack2(s[2 * kc][2], s[2 * kc][3]);+ pa[2] = qi_pack2(s[2 * kc + 1][0], s[2 * kc + 1][1]);+ pa[3] = qi_pack2(s[2 * kc + 1][2], s[2 * kc + 1][3]);+ for (int n = 0; n < QI_D / 8; n += 2) {+ uint32_t b[4];+ qi_ldsm_x4(b, sV + (kc * 16 + (lq & 1) * 8 + lr) * MF_LD + n * 8 + (lq >> 1) * 8, true);+ qi_mma16816(o[n], pa, b);+ qi_mma16816(o[n + 1], pa, b + 2);+ }+ }+ __syncthreads();+ }+ const int row0 = q0 + r, row1 = q0 + r + 8;+ for (int n = 0; n < QI_D / 8; ++n) {+ if (row0 < nq)+ *reinterpret_cast<float2*>(O + (size_t) row0 * H * QI_D + head * QI_D + n * 8 + c2) =+ make_float2(o[n][0] / l0, o[n][1] / l0);+ if (row1 < nq)+ *reinterpret_cast<float2*>(O + (size_t) row1 * H * QI_D + head * QI_D + n * 8 + c2) =+ make_float2(o[n][2] / l1, o[n][3] / l1);+ }+#else+ __trap();+#endif+}+#endif // !GGML_USE_HIP+inline cudaStream_t qi_s(void* s) { return static_cast<cudaStream_t>(s); }} // namespace@@ -345,11 +558,31 @@ int qi21_has_wmma_attn(ggml_backend_t backend) {const char* g12 = getenv("STRATA_QI_GFX12_ATTN");return GGML_CUDA_CC_IS_RDNA4(cc) && g12 != nullptr && g12[0] == '1' ? 1 : 0;#else- (void) backend;- return 0;+ // NVIDIA Ampere or newer: the mma.sync kernel (STRATA_QI_GGML_ATTN=1 in the runner keeps ggml's)+ const int dev = static_cast<ggml_backend_cuda_context*>(backend->context)->device;+ return GGML_CUDA_CC_IS_NVIDIA(ggml_cuda_info().devices[dev].cc) &&+ ggml_cuda_info().devices[dev].cc >= GGML_CUDA_CC_AMPERE ? 1 : 0;#endif}+int qi21_tanh(void* stream, const float* in, float* out, int n) {+ k_tanh<<<(n + 255) / 256, 256, 0, qi_s(stream)>>>(in, out, n);+ return cudaGetLastError() == cudaSuccess ? 0 : 2;+}++// g: the gate row as tanh(gate) already when tg != 0 (the float4 kernel), the raw gate otherwise+int qi21_res_ln_mod2(void* stream, float* x, const float* h, const float* tg, float* out, const float* m, int T, int C,+ float eps) {+ if (C % 1024 != 0 || C > 8 * 1024) return 1;+ if (h != nullptr && out != nullptr)+ k_res_ln_mod4<true, true><<<T, 256, 0, qi_s(stream)>>>(x, h, tg, out, m, C, eps);+ else if (h != nullptr)+ k_res_ln_mod4<true, false><<<T, 256, 0, qi_s(stream)>>>(x, h, tg, out, m, C, eps);+ else+ k_res_ln_mod4<false, true><<<T, 256, 0, qi_s(stream)>>>(x, h, tg, out, m, C, eps);+ return cudaGetLastError() == cudaSuccess ? 0 : 2;+}+int qi21_res_ln_mod(void* stream, float* x, const float* h, const float* g, float* out, const float* m, int T, int C,float eps) {if (C > 32 * 256) return 1;@@ -425,9 +658,17 @@ int qi21_attn_wmma(void* stream, const void* q, const void* qlo, int q_tstride,nq, nkv, sl);return cudaGetLastError() == cudaSuccess ? 0 : 2;#else- (void) stream; (void) q; (void) qlo; (void) q_tstride; (void) k; (void) v; (void) kv_tstride; (void) out; (void) H; (void) nq;- (void) nkv;- return 1;+ if (qlo != nullptr) return 1; // no hi/lo split on this path+ static bool attr = false;+ if (!attr) {+ if (cudaFuncSetAttribute(k_attn_mma, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) MF_SMEM) != cudaSuccess)+ return 2;+ attr = true;+ }+ k_attn_mma<<<dim3((nq + MF_BQ - 1) / MF_BQ, H), MF_NW * 32, MF_SMEM, qi_s(stream)>>>(+ static_cast<const half*>(q), q_tstride, static_cast<const half*>(k), static_cast<const half*>(v), kv_tstride, out,+ H, nq, nkv, (float) (1.0 / sqrt((double) QI_D) * 1.4426950408889634));+ return cudaGetLastError() == cudaSuccess ? 0 : 2;#endif}diff --git a/tools/image/sdcpp_native/qwen_image_2_1_native.hpp b/tools/image/sdcpp_native/qwen_image_2_1_native.hppindex 9740a1534..ef845a772 100644--- a/tools/image/sdcpp_native/qwen_image_2_1_native.hpp+++ b/tools/image/sdcpp_native/qwen_image_2_1_native.hpp@@ -34,6 +34,9 @@ int qi21_qkv_post(void* stream, const float* q, const float* k, const float* v,const float* pe, void* qo, void* qlo, int q_f16, int q_tstride, void* ko, void* vo, int kv_tstride,int kv_off, int T, int H, float eps);int qi21_swiglu(void* stream, const float* gu, float* out, int T, int F);+int qi21_tanh(void* stream, const float* in, float* out, int n);+int qi21_res_ln_mod2(void* stream, float* x, const float* h, const float* tg, float* out, const float* m, int T, int C,+ float eps);int qi21_attn_causal(void* stream, const float* q, int q_tstride, const void* k, const void* v, int kv_tstride,float* out, int H, int Lq, int Lk, int causal);int qi21_attn_wmma(void* stream, const void* q, const void* qlo, int q_tstride, const void* k, const void* v,@@ -53,6 +56,8 @@ namespace Qwen {bool said_why = false;bool qsplit = false;bool exact_attn = false;+ bool fast_ln = true;+ bool fuse_qkv = true;// weights (our own copy: the stock runner's params are never loaded, they load lazily on first use)ggml_context* wctx = nullptr;@@ -64,7 +69,7 @@ namespace Qwen {ggml_backend_buffer_t abuf = nullptr;int64_t t_cap = 0, l_cap = 0;ggml_tensor *x, *h, *q, *k, *v, *att, *gu, *mlp, *q16, *kw, *vw, *pe_img, *pe_txt, *latent, *outp, *timein,- *mod, *scale_out, *ctx_in, *pk, *pv;+ *mod, *gates, *scale_out, *ctx_in, *pk, *pv;// the prefix cache: per layer K/V of the text tokens (fp16, head-major), for one conditionuint64_t prefix_key = 0;@@ -79,6 +84,8 @@ namespace Qwen {timing = getenv("STRATA_QI_TIMING") != nullptr;qsplit = getenv("STRATA_QI_QSPLIT") != nullptr; // measured: no accuracy gain, +7% step timeexact_attn = getenv("STRATA_QI_EXACT_ATTN") != nullptr;+ fuse_qkv = getenv("STRATA_QI_NO_QKV_FUSE") == nullptr;+ fast_ln = getenv("STRATA_QI_FAST_LN") != nullptr; // measured: no gain on the 3060 (2.10 vs 2.10 s/step)gbuf.resize(64 << 20);}@@ -117,12 +124,43 @@ namespace Qwen {if (g == nullptr) return false;const int64_t n = gguf_get_n_tensors(g);wctx = ggml_init({(size_t) (n + 8) * ggml_tensor_overhead(), nullptr, true});- std::vector<std::pair<ggml_tensor*, size_t>> plan;+ struct Piece { ggml_tensor* t; size_t file_off, dst_off; };+ std::vector<Piece> plan;std::map<ggml_tensor*, ggml_type> widened;const size_t base = gguf_get_data_offset(g);+ // q, k and v of a block in one tensor when they have the same type (one GEMM, one activation quantize):+ // rows stay rows, so the three weights are simply laid one after the other+ auto qkv_name = [](int b, const char* w) {+ return "model.diffusion_model.transformer_blocks." + std::to_string(b) + ".attn.to_" + w + ".weight";+ };+ std::map<std::string, std::pair<ggml_tensor*, size_t>> fused_part; // q/k/v name -> (fused, offset)+ if (fuse_qkv) {+ for (int b = 0; b < config.num_layers; ++b) {+ ggml_tensor* tq = ggml_get_tensor(meta, qkv_name(b, "q").c_str());+ ggml_tensor* tk = ggml_get_tensor(meta, qkv_name(b, "k").c_str());+ ggml_tensor* tv = ggml_get_tensor(meta, qkv_name(b, "v").c_str());+ if (!tq || !tk || !tv || tq->type != tk->type || tq->type != tv->type || tq->ne[0] != tk->ne[0] ||+ tq->ne[1] != tk->ne[1] || tq->ne[1] != tv->ne[1])+ continue;+ ggml_tensor* f = ggml_new_tensor_2d(wctx, tq->type, tq->ne[0], 3 * tq->ne[1]);+ const std::string fname = "model.diffusion_model.transformer_blocks." + std::to_string(b) ++ ".attn.qkv.weight";+ ggml_set_name(f, fname.c_str());+ W[fname] = f;+ const size_t nb = ggml_nbytes(tq);+ fused_part[qkv_name(b, "q")] = {f, 0};+ fused_part[qkv_name(b, "k")] = {f, nb};+ fused_part[qkv_name(b, "v")] = {f, 2 * nb};+ }+ }for (int64_t i = 0; i < n; ++i) {const char* name = gguf_get_tensor_name(g, i);if (strncmp(name, "model.diffusion_model.", 22) != 0) continue;+ auto fp = fused_part.find(name);+ if (fp != fused_part.end()) {+ plan.push_back({fp->second.first, base + gguf_get_tensor_offset(g, i), fp->second.second});+ continue;+ }ggml_tensor* src = ggml_get_tensor(meta, name);// a 1-D norm weight in BF16 / F16 is kept as F32 (elementwise ops on it then work on every backend)const bool widen = ggml_n_dims(src) == 1 && src->type != GGML_TYPE_F32;@@ -130,7 +168,7 @@ namespace Qwen {if (widen) widened[t] = src->type;ggml_set_name(t, name);W[name] = t;- plan.push_back({t, base + gguf_get_tensor_offset(g, i)});+ plan.push_back({t, base + gguf_get_tensor_offset(g, i), 0});}wbuf = ggml_backend_alloc_ctx_tensors(wctx, runtime_backend);if (wbuf == nullptr) {@@ -141,11 +179,12 @@ namespace Qwen {FILE* f = fopen(dit_path.c_str(), "rb");std::vector<char> stage(64 << 20);bool ok = f != nullptr;- for (auto& [t, off] : plan) {+ for (auto& [t, off, dst_off] : plan) {if (!ok) break;auto wi = widened.find(t);- const size_t nb = wi == widened.end() ? ggml_nbytes(t)- : ggml_row_size(wi->second, t->ne[0]);+ const bool part = std::string(ggml_get_name(t)).find(".attn.qkv.") != std::string::npos;+ const size_t nb = part ? ggml_nbytes(t) / 3+ : wi == widened.end() ? ggml_nbytes(t) : ggml_row_size(wi->second, t->ne[0]);#ifdef _WIN32ok = _fseeki64(f, (long long) off, SEEK_SET) == 0;#else@@ -164,7 +203,7 @@ namespace Qwen {for (size_t done = 0; ok && done < nb;) {const size_t c = std::min(stage.size(), nb - done);ok = fread(stage.data(), 1, c, f) == c;- if (ok) ggml_backend_tensor_set(t, stage.data(), done, c);+ if (ok) ggml_backend_tensor_set(t, stage.data(), dst_off + done, c);done += c;}}@@ -174,7 +213,10 @@ namespace Qwen {if (!ok) return false;for (int i = 0; i < config.num_layers; ++i) {const std::string b = "transformer_blocks." + std::to_string(i) + ".";- for (const char* s : {"attn.to_q.weight", "attn.to_k.weight", "attn.to_v.weight", "attn.to_out.0.weight",+ if (w(b + "attn.qkv.weight") == nullptr &&+ (!w(b + "attn.to_q.weight") || !w(b + "attn.to_k.weight") || !w(b + "attn.to_v.weight")))+ return false;+ for (const char* s : {"attn.to_out.0.weight","attn.norm_q.weight", "attn.norm_k.weight", "img_mlp.gate_up.weight","img_mlp.out.weight"}) {if (w(b + s) == nullptr) return false;@@ -190,7 +232,7 @@ namespace Qwen {}LOG_INFO("Qwen Image 2.1 native: %zu tensors, %.2f GB on %s%s", W.size(),ggml_backend_buffer_get_size(wbuf) / 1e9, ggml_backend_name(runtime_backend),- wmma_attn ? " (WMMA attention)" : "");+ wmma_attn ? " (Strata attention)" : "");return true;}@@ -220,6 +262,7 @@ namespace Qwen {outp = ggml_new_tensor_2d(actx, GGML_TYPE_F32, config.out_channels, t_cap);timein = ggml_new_tensor_1d(actx, GGML_TYPE_F32, 2);mod = ggml_new_tensor_2d(actx, GGML_TYPE_F32, 4 * C, 2);+ gates = ggml_new_tensor_2d(actx, GGML_TYPE_F32, 4 * C, 2); // tanh(mod), the gate rows usedscale_out = ggml_new_tensor_1d(actx, GGML_TYPE_F32, C);ctx_in = ggml_new_tensor_2d(actx, GGML_TYPE_F32, config.context_dim, std::max<int64_t>(l_cap, 1));pk = ggml_new_tensor_3d(actx, GGML_TYPE_F16, D * H, std::max<int64_t>(l_cap, 1), config.num_layers);@@ -279,6 +322,13 @@ namespace Qwen {void* s = qi21_stream(runtime_backend);const float* m = (const float*) mod->data + mrow * 4 * C;const float *m0 = m, *m1 = m + C, *m2 = m + 2 * C, *m3 = m + 3 * C;+ const float* tgr = (const float*) gates->data + mrow * 4 * C; // tanh of the same row+ const float *t1 = tgr + C, *t3 = tgr + 3 * C;+ auto res_ln = [&](float* xx, const float* hh, const float* graw, const float* tgate, float* out,+ const float* mm) {+ return fast_ln ? qi21_res_ln_mod2(s, xx, hh, tgate, out, mm, (int) n, (int) C, 1e-6f)+ : qi21_res_ln_mod(s, xx, hh, graw, out, mm, (int) n, (int) C, 1e-6f);+ };const bool prefix = mrow == 1;const int64_t kvt = prefix ? n : L + n; // keys per head in the work K/Vconst bool use_wmma = !prefix && wmma_attn && !exact_attn;@@ -288,18 +338,29 @@ namespace Qwen {const size_t qlo_off = (size_t) H * n * D * sizeof(ggml_fp16_t);float* X = (float*) x->data;float* Hh = (float*) h->data;- int rc = qi21_res_ln_mod(s, X, nullptr, nullptr, Hh, m0, (int) n, (int) C, 1e-6f);+ int rc = res_ln(X, nullptr, nullptr, nullptr, Hh, m0);for (int i = 0; i < config.num_layers && rc == 0; ++i) {const std::string b = "transformer_blocks." + std::to_string(i) + ".";+ ggml_tensor* wqkv = w(b + "attn.qkv.weight");+ const float *qs = (const float*) q->data, *ks = (const float*) k->data, *vs = (const float*) v->data;+ int ldq = (int) C;{G g = begin();- gemm(g, w(b + "attn.to_q.weight"), h, q, n);- gemm(g, w(b + "attn.to_k.weight"), h, k, n);- gemm(g, w(b + "attn.to_v.weight"), h, v, n);+ if (wqkv != nullptr) { // [3C x n] into gu (free here), q | k | v per token+ gemm(g, wqkv, h, gu, n);+ qs = (const float*) gu->data;+ ks = qs + C;+ vs = qs + 2 * C;+ ldq = (int) (3 * C);+ } else {+ gemm(g, w(b + "attn.to_q.weight"), h, q, n);+ gemm(g, w(b + "attn.to_k.weight"), h, k, n);+ gemm(g, w(b + "attn.to_v.weight"), h, v, n);+ }run(g, {});}const int64_t off = prefix ? 0 : L;- rc = qi21_qkv_post(s, (const float*) q->data, (const float*) k->data, (const float*) v->data, (int) C,+ rc = qi21_qkv_post(s, qs, ks, vs, ldq,(const float*) w(b + "attn.norm_q.weight")->data,(const float*) w(b + "attn.norm_k.weight")->data, (const float*) pe->data, q16->data,use_wmma && qsplit ? (char*) q16->data + qlo_off : nullptr, use_wmma ? 1 : 0, (int) n, kw->data, vw->data, (int) kvt, (int) off, (int) n, (int) H,@@ -360,7 +421,7 @@ namespace Qwen {gemm(g, w(b + "attn.to_out.0.weight"), att, h, n);run(g, {});}- rc = qi21_res_ln_mod(s, X, Hh, m1, Hh, m2, (int) n, (int) C, 1e-6f);+ rc = res_ln(X, Hh, m1, t1, Hh, m2);if (rc) break;{G g = begin();@@ -375,7 +436,7 @@ namespace Qwen {run(g, {});}const bool last = i + 1 == config.num_layers;- rc = qi21_res_ln_mod(s, X, Hh, m3, last ? nullptr : Hh, m0, (int) n, (int) C, 1e-6f);+ rc = res_ln(X, Hh, m3, t3, last ? nullptr : Hh, m0);}if (rc) LOG_WARN("Qwen Image 2.1 native: a kernel failed (%d)", rc);return rc == 0;@@ -460,6 +521,11 @@ namespace Qwen {bind(sc, scale_out);run(g, {md, sc});}+ if (qi21_tanh(qi21_stream(runtime_backend), (const float*) mod->data, (float*) gates->data,+ (int) (8 * C)) != 0) {+ fail("the gate kernel failed");+ return QwenImage21Runner::compute(n_threads, inputs);+ }// the text prefix: once per conditionuint64_t key = extra != nullptr ? extra->prefix_id : 0;{