okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5b e93b90250 into f-own-intd4eac2ca3

main
3.8 KiB60 linesraw
1// src/image/dit/qi21_kernels.hpp - the Qwen-Image-2.1 DiT's element-wise and attention kernels (src/image/dit/
2// qi21_kernels.cu), launched on a caller's stream.  Moved from the sd.cpp patch tools/image/sdcpp_native/qi21-native.cu
3// (same kernels, same launch shapes; that patch keeps its own copy for the test builds until the sd-cli hook changes),
4// plus the steps ggml still did for the native runner: the timestep sinusoid, SiLU, GELU (tanh form), the zero-centred
5// RMSNorm of txt_in.  Every launcher returns 0, or 1 for an unsupported shape, 2 for a launch error.
6#pragma once
7
8#include <cstddef>
9#include <cstdint>
10
11namespace strata::image::qi21 {
12
13// x += h * tanh(g) (when h), then out = LayerNorm(x) * (1 + m) (when out); no affine, eps.  T rows of C (C <= 8192).
14int res_ln_mod(void* stream, float* x, const float* h, const float* g, float* out, const float* m, int T, int C, float eps);
15
16// per head of T tokens: q, k RMSNorm * wq | wk, RoPE from the per-token table pe ([T][64][4]), q -> qo head-major
17// [H][q_tstride][128] (f16 when q_f16, else f32), k, v -> f16 head-major [H][kv_tstride][128] at row kv_off.
18// q, k, v rows have stride ld floats.  H % 8 == 0, ld % 4 == 0.
19int qkv_post(void* stream, const float* q, const float* k, const float* v, int ld, const float* wq, const float* wk,
20             const float* pe, void* qo, int q_f16, int q_tstride, void* ko, void* vo, int kv_tstride, int kv_off, int T,
21             int H, float eps);
22
23// out[T][F] = up * silu(gate), gu = [T][gate F | up F]
24int swiglu(void* stream, const float* gu, float* out, int T, int F);
25
26// fp32 attention, q f32 head-major, k / v f16 head-major, out token-major [Lq][H*128]; causal: query i sees keys
27// 0..i (the text prefix); otherwise all Lk keys (the exact reference path).  Lk * 4 bytes of shared memory <= 48 KiB.
28int attn_exact(void* stream, const float* q, int q_tstride, const void* k, const void* v, int kv_tstride, float* out,
29               int H, int Lq, int Lk, int causal);
30
31// 1 when this device runs attn_fast (NVIDIA sm_80+: mma.sync; HIP gfx11: WMMA; gfx12 unless STRATA_QI_GFX12_ATTN=0)
32int has_fast_attn(int device);
33
34// flash attention for the image tokens: q f16 head-major, k / v f16 head-major, out f32 token-major
35int attn_fast(void* stream, const void* q, int q_tstride, const void* k, const void* v, int kv_tstride, float* out, int H,
36              int nq, int nkv);
37
38// the same with the output as f16 bits (f16_from_f32 of the float the f32 version stores: the bytes dit_gemm's
39// conversion pass made of that float), for the kernels whose output only feeds a GEMM
40int res_ln_mod_f16(void* stream, float* x, const float* h, const float* g, uint16_t* out, const float* m, int T, int C,
41                   float eps);
42int swiglu_f16(void* stream, const float* gu, uint16_t* out, int T, int F);
43int attn_exact_f16(void* stream, const float* q, int q_tstride, const void* k, const void* v, int kv_tstride,
44                   uint16_t* out, int H, int Lq, int Lk, int causal);
45int attn_fast_f16(void* stream, const void* q, int q_tstride, const void* k, const void* v, int kv_tstride, uint16_t* out,
46                  int H, int nq, int nkv);
47
48// emb[n][dim] = [cos(t * f_j) | sin(t * f_j)], f_j = exp(-ln(max_period) * j / (dim / 2))  (ggml_timestep_embedding)
49int timestep_embedding(void* stream, const float* t, float* emb, int n, int dim, float max_period);
50
51// x = x / (1 + exp(-x)) in place
52int silu(void* stream, float* x, int64_t n);
53
54// x = 0.5 x (1 + tanh(sqrt(2 / pi) x (1 + 0.044715 x^2))) in place (ggml's GELU)
55int gelu_tanh(void* stream, float* x, int64_t n);
56
57// out[r] = x[r] / sqrt(mean(x[r]^2) + eps) * (w + 1), T rows of C (txt_in's zero-centred RMSNorm)
58int rms_norm_w1(void* stream, const float* x, const float* w, float* out, int T, int C, float eps);
59
60}  // namespace strata::image::qi21