///|
/// Minimal `flash` library example (CPU: wasm f32x4 SIMD / native scalar).
///
/// Shows the 4D contract — q [B,H,Sq,D], k/v [B,HKV,Skv,DV] -> [B,H,Sq,DV] —
/// with GQA (`hkv < h`) plus a bottom-right causal mask, and checks the tiled
/// kernel against the naive reference that ships in the same package.
///
/// Run: moon run examples/attn_cpu --target wasm --release
///|
/// Deterministic pseudo-random value in [-1, 1) (example stand-in for real data).
fn rand_val(i : Int, j : Int, k : Int) -> Float {
(Float::from_int((i * 37 + j * 91 + k * 13 + 11) % 29) - 14.0F) / 14.0F
}
///|
/// Build a [b, h, rows, cols] tensor filled with the deterministic pattern.
fn make_tensor(
b : Int,
h : Int,
rows : Int,
cols : Int,
k : Int,
) -> @nparray.NpArray {
let data = FixedArray::make(b * h * rows * cols, 0.0F)
for i in 0..<(b * h * rows) {
for j in 0.. max_diff {
max_diff = diff
}
}
let dims = out.value().dims
println(
"out dims = [\{dims[0]}, \{dims[1]}, \{dims[2]}, \{dims[3]}] (expect [b,h,Sq,DV])",
)
println("out[0] = \{a[0]}")
println("naive[0] = \{r[0]}")
println("max |flash - naive| = \{max_diff}")
}