Commit 2af135d
feat(native-cpu): native FFM FP32 SGEMM kernel (PR 5 of 5)
Final PR of the staged native-FFM rollout per
docs/.../perf/native-ffm-plan.adoc. Wires a real FP32 matmul into
the existing matmulFp32() SPI accessor on NativeKernelProvider; the
KernelRegistry now hands out the native kernel ahead of Panama
Vector for FP32 SGEMM on hosts where libskainet_kernels resolves.
Native side (native/):
- src/fp32_matmul.c implements skainet_fp32_matmul as row-major
C(m,n) = A(m,k) * B(k,n) with stride support. Iteration order is
i-p-j (outer product into rows of C); the inner `c[j] += a*b[j]`
loop streams two contiguous arrays and auto-vectorizes cleanly
under -O3 -ffast-math into vfmadd231ps on x86_64 / fmla on AArch64.
Caller contract matches the SPI: zero-then-accumulate ensures C is
fully overwritten, k=0 zeros the block, m=0||n=0 is a no-op.
- include/skainet_kernels.h declares the new export. CMakeLists adds
fp32_matmul.c alongside the smoke and Q4_K sources.
Kotlin side (src/jvmMain):
- NativeFp32MatmulKernel implements Fp32MatmulKernel via FFM downcall
(12-arg FunctionDescriptor: 3 ADDRESS + 9 JAVA_INT). Heap arrays
are copied into Arena.ofConfined off-heap segments sized to the
reach (offset + last-row stride * rows) so non-contiguous strides
Just Work without per-row staging.
- NativeKernelProvider.matmulFp32() now returns
NativeFp32MatmulKernel when the lib loads; cascades to Panama
otherwise. NativeKernelProvider's class kdoc is updated to mark
PR 5 as the cursor for matmulFp32.
Tests (src/jvmTest):
- NativeFp32MatmulKernelParityTest — 10 cases mirroring the Panama
fixture (small contiguous, random aligned, non-aligned k tail,
strided sub-block, irregular sizes, LLM-typical 256², zero-m,
zero-k, negative-dim rejection) plus a provider-handout assertion.
Tolerance scaled with k matching the Panama-vs-Scalar bar
(1e-5 * k); 5e-5 * k at 256² to absorb -ffast-math reassociation.
- Q4KMatmulMicrobenchTest grows a bench_fp32_native_vs_panama case
at 256³ / 512³ / 1024³, gated by -Dskainet.runBench=true.
- NativeFfmPipelineTest's stub-flip assertion now expects both
matmulFp32() and matmulQ4K() to return non-null kernels.
Microbench numbers (Linux x86_64, JDK 21.0.10, gcc 13.3 -O3
-ffast-math; warmup=5, samples=9, median µs):
shape native panama ratio
256³ 1976 3492 1.77×
512³ 17048 26882 1.58×
1024³ 142463 220710 1.55×
Honest read: FP32 wins are more modest than Q4_K (Q4_K was 4–6×).
Why: Panama's FP32 path has tile-blocking + B-pack (transposed)
+ parallelChunks across all cores; Q4_K's Panama path doesn't
do parallelChunks-style decomposition. Native single-threaded scalar
C still wins everywhere measured because the JVM's per-call
overhead and parallelChunks dispatch overhead are nontrivial at
these shapes. Hand-tuned cache-blocking + threading would push the
native FP32 path further but is perf-tuning, not correctness.
Verification (linux-x86_64, JDK 21.0.10):
- :skainet-backends:skainet-backend-native-cpu:jvmTest — 27/27
(3 pipeline + 5 Q4_K heap-parity + 7 Q4_K memseg-parity + 10 FP32
parity + 2 microbench-gated)
- :skainet-backends:skainet-backend-cpu:jvmTest — 218/218 (no regression)
- 3 native symbols exported: skainet_smoke_double, skainet_q4k_matmul,
skainet_fp32_matmul
Out of scope (deferred):
- Native Q6_K and Q8_0 matmul. Both need new SPI accessors
(Q6KMatmulKernel, Q8MatmulKernel) and the existing Panama provider
also needs to expose its internal Q6_K / Q8_0 paths through the
new SPI. That arc is a separate plan.
- Cache-blocking + B-tile packing + parallelChunks-style threading
for the native FP32 path. Profile-driven; the current scalar C
path already wins everywhere measured.
- Maven Central native classifier publishing / fat-JAR aggregation
(still deferred from PR 4).
Rollout state: PR 1 (scaffolding), PR 2 (Q4_K matmul), PR 3 (Q4_K
MemSeg zero-copy), PR 4 (cross-arch CI matrix), PR 5 (this commit,
FP32 matmul). The 5-PR plan from native-ffm-plan.adoc is now
complete in its core scope; the optional Q6_K / Q8_0 additions and
the publishing infrastructure are future work.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 87a5730 commit 2af135d
8 files changed
Lines changed: 410 additions & 6 deletions
File tree
- skainet-backends/skainet-backend-native-cpu
- native
- include
- src
- src
- jvmMain/kotlin/sk/ainet/exec/kernel
- jvmTest/kotlin/sk/ainet/exec/kernel
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
63 | 79 | | |
64 | 80 | | |
65 | 81 | | |
| |||
Lines changed: 61 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
Lines changed: 113 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
Lines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
| 39 | + | |
| 40 | + | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
44 | | - | |
| 43 | + | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
0 commit comments