|
| 1 | +package sk.ainet.exec.kernel |
| 2 | + |
| 3 | +import kotlinx.cinterop.ExperimentalForeignApi |
| 4 | +import kotlinx.cinterop.addressOf |
| 5 | +import kotlinx.cinterop.reinterpret |
| 6 | +import kotlinx.cinterop.usePinned |
| 7 | +import sk.ainet.backend.api.kernel.Fp32MatmulKernel |
| 8 | +import sk.ainet.backend.api.kernel.KernelProvider |
| 9 | +import sk.ainet.backend.api.kernel.KernelRegistry |
| 10 | +import sk.ainet.backend.api.kernel.Q4KMatmulKernel |
| 11 | +import sk.ainet.backend.api.kernel.Q4_0MatmulKernel |
| 12 | +import sk.ainet.backend.api.kernel.Q5KMatmulKernel |
| 13 | +import sk.ainet.backend.api.kernel.Q8_0MatmulKernel |
| 14 | +import sk.ainet.kernels.cinterop.skainet_q4_0_matmul |
| 15 | +import sk.ainet.kernels.cinterop.skainet_q4k_matmul |
| 16 | +import sk.ainet.kernels.cinterop.skainet_q8_0_matmul |
| 17 | + |
| 18 | +/** |
| 19 | + * Kotlin/Native [KernelProvider] backed by the hand-written C kernels via |
| 20 | + * cinterop (static archive `libskainet_kernels.a`) — the K/N analogue of the |
| 21 | + * JVM `NativeKernelProvider` (FFM). Priority 100, above the commonMain scalar |
| 22 | + * reference (0). On `linuxArm64` the linked archive carries the NEON paths. |
| 23 | + * |
| 24 | + * **Registration is manual on K/N** (no `ServiceLoader`): a consumer calls |
| 25 | + * [installNativeKernels] once at startup. [Q5KMatmulKernel] (the FunctionGemma |
| 26 | + * Q5_K_M hot path) plus Q4_K / Q8_0 / Q4_0 are wired; the rest cascade to the |
| 27 | + * scalar provider. |
| 28 | + */ |
| 29 | +@OptIn(ExperimentalForeignApi::class) |
| 30 | +public object NativeKnKernelProvider : KernelProvider { |
| 31 | + override val name: String = "native-cinterop" |
| 32 | + override val priority: Int = 100 |
| 33 | + |
| 34 | + // Statically linked — the symbols are always present once the binary links |
| 35 | + // libskainet_kernels.a, so the provider is unconditionally available. |
| 36 | + override fun isAvailable(): Boolean = true |
| 37 | + |
| 38 | + // Abstract on KernelProvider (no default) — no native FP32 SGEMM wrapper yet. |
| 39 | + override fun matmulFp32(): Fp32MatmulKernel? = null |
| 40 | + |
| 41 | + override fun matmulQ5K(): Q5KMatmulKernel = NativeKnQ5KMatmulKernel |
| 42 | + override fun matmulQ4K(): Q4KMatmulKernel = NativeKnQ4KMatmulKernel |
| 43 | + override fun matmulQ8_0(): Q8_0MatmulKernel = NativeKnQ8_0MatmulKernel |
| 44 | + override fun matmulQ4_0(): Q4_0MatmulKernel = NativeKnQ4_0MatmulKernel |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Register [NativeKnKernelProvider] in the process-wide [KernelRegistry]. Idempotent |
| 49 | + * (re-registering the same instance is a no-op). Call once at startup before any |
| 50 | + * `ops.matmul` on quantized weights. |
| 51 | + * |
| 52 | + * For quant types without a C kernel (e.g. Q6_K) also register the commonMain |
| 53 | + * `ScalarKernelProvider` (from `skainet-backend-cpu`) as the fallback — it lives |
| 54 | + * in a different module, so the consumer wires it: |
| 55 | + * `KernelRegistry.register(ScalarKernelProvider)`. |
| 56 | + */ |
| 57 | +public fun installNativeKernels() { |
| 58 | + KernelRegistry.register(NativeKnKernelProvider) |
| 59 | +} |
| 60 | + |
| 61 | +@OptIn(ExperimentalForeignApi::class) |
| 62 | +public object NativeKnQ4KMatmulKernel : Q4KMatmulKernel { |
| 63 | + private const val BLOCK_SIZE = 256 |
| 64 | + override fun matmul( |
| 65 | + input: FloatArray, inputOffset: Int, |
| 66 | + weight: ByteArray, weightByteOffset: Int, |
| 67 | + inputDim: Int, outputDim: Int, |
| 68 | + output: FloatArray, outputOffset: Int, |
| 69 | + ) { |
| 70 | + require(inputDim % BLOCK_SIZE == 0) { |
| 71 | + "NativeKnQ4KMatmulKernel: inputDim must be a multiple of $BLOCK_SIZE; got $inputDim" |
| 72 | + } |
| 73 | + if (outputDim == 0 || inputDim == 0) return |
| 74 | + input.usePinned { i -> weight.usePinned { w -> output.usePinned { o -> |
| 75 | + skainet_q4k_matmul( |
| 76 | + i.addressOf(0), inputOffset, |
| 77 | + w.addressOf(0).reinterpret(), weightByteOffset, |
| 78 | + inputDim, outputDim, |
| 79 | + o.addressOf(0), outputOffset, |
| 80 | + ) |
| 81 | + } } } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +@OptIn(ExperimentalForeignApi::class) |
| 86 | +public object NativeKnQ8_0MatmulKernel : Q8_0MatmulKernel { |
| 87 | + private const val BLOCK_SIZE = 32 |
| 88 | + override fun matmul( |
| 89 | + input: FloatArray, inputOffset: Int, |
| 90 | + weight: ByteArray, weightByteOffset: Int, |
| 91 | + inputDim: Int, outputDim: Int, |
| 92 | + output: FloatArray, outputOffset: Int, |
| 93 | + ) { |
| 94 | + require(inputDim % BLOCK_SIZE == 0) { |
| 95 | + "NativeKnQ8_0MatmulKernel: inputDim must be a multiple of $BLOCK_SIZE; got $inputDim" |
| 96 | + } |
| 97 | + if (outputDim == 0 || inputDim == 0) return |
| 98 | + input.usePinned { i -> weight.usePinned { w -> output.usePinned { o -> |
| 99 | + skainet_q8_0_matmul( |
| 100 | + i.addressOf(0), inputOffset, |
| 101 | + w.addressOf(0).reinterpret(), weightByteOffset, |
| 102 | + inputDim, outputDim, |
| 103 | + o.addressOf(0), outputOffset, |
| 104 | + ) |
| 105 | + } } } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +@OptIn(ExperimentalForeignApi::class) |
| 110 | +public object NativeKnQ4_0MatmulKernel : Q4_0MatmulKernel { |
| 111 | + private const val BLOCK_SIZE = 32 |
| 112 | + override fun matmul( |
| 113 | + input: FloatArray, inputOffset: Int, |
| 114 | + weight: ByteArray, weightByteOffset: Int, |
| 115 | + inputDim: Int, outputDim: Int, |
| 116 | + output: FloatArray, outputOffset: Int, |
| 117 | + ) { |
| 118 | + require(inputDim % BLOCK_SIZE == 0) { |
| 119 | + "NativeKnQ4_0MatmulKernel: inputDim must be a multiple of $BLOCK_SIZE; got $inputDim" |
| 120 | + } |
| 121 | + if (outputDim == 0 || inputDim == 0) return |
| 122 | + input.usePinned { i -> weight.usePinned { w -> output.usePinned { o -> |
| 123 | + skainet_q4_0_matmul( |
| 124 | + i.addressOf(0), inputOffset, |
| 125 | + w.addressOf(0).reinterpret(), weightByteOffset, |
| 126 | + inputDim, outputDim, |
| 127 | + o.addressOf(0), outputOffset, |
| 128 | + ) |
| 129 | + } } } |
| 130 | + } |
| 131 | +} |
0 commit comments