feat(backends): TernaryF32GemvNative SPI + exact FP32×b1.58 dispatch pack (#1138) - #1157
Merged
Merged
Conversation
…pack TernaryF32GemvNative is the array-shaped seam over the vendored NeoGPU LUT kernel (#1137), sibling of BitNetGemvNative. TernaryF32KernelPack registers a view kernel under the exact key matmul(FP32 dense × BITNET_B1_58 row-major) — the key KernelDispatch checks before the requantize branch, so installing the pack short-circuits the int8 adapter with zero dispatcher changes, and absence leaves today's behavior untouched (a notice, never a crash; deliberately nothing is registered without the native kernel — the tuned int8 path beats a Kotlin f32 loop as the portable fallback). The view kernel loops the gemv per activation row (prefill included), applies the in-band per-tensor scale the native side never sees, and falls back to the new TernaryF32GemvKernel reference for non-heap storage, strided views, or k % 4 != 0 (the sequential packing crosses byte boundaries between rows then). The reference is pinned against the decoded matmul; the pack contract against a FakeNative; and the real vendored kernel against real dispatch in TernaryF32FfmPackTest — the FFM object now implements the SPI (gemv → gemvPacked) with an install() helper, closing the "FFM has no ternary consumer" gap. Kernel-support matrix intentionally unchanged: pack-registered kernels are not KernelProvider accessors, same as bitnet_gemv. Refs #1138, #1136 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 2 of #1136 — closes #1138. Builds on #1151.
What
TernaryF32GemvNative— array-shaped SPI over the vendored NeoGPU LUT kernel (sibling ofBitNetGemvNative; scale NOT applied,inputDim % 4 == 0).TernaryF32GemvKernel— portable referenceViewKernel: FP32 ×BITNET_B1_58, codes hoisted, in-band scale applied to output. The correctness oracle and in-kernel fallback — deliberately not a dispatch entry.TernaryF32KernelPack.install(native, capabilities, warn)— registersNativeTernaryF32ViewKernelunder the exact keymatmul(FP32 dense contiguous × FP32/BITNET_B1_58 blocked_row_major)(capability-free + capability keys, same two-key pattern asTernaryKernelPacks).KernelDispatch.matmulchecks the exact key before the requantize branch → the pack short-circuits the int8 adapter with zero dispatcher changes;install(null)warns and registers nothing, keeping today's behavior bit-for-bit.k % 4 != 0.gemvPacked) with aninstall()helper — the first ternary FFM consumer.Tests (all green locally, macOS arm64)
TernaryF32GemvKernelTest— reference vs decoded-weight matmul (incl.k % 4 != 0byte-boundary crossing)TernaryF32KernelPackTest— FakeNative: exact key beats requantize path (noAdapterInserted), absence keepsbitnet_gemv/referenceserving, per-row looping, odd-k fallback, capability recorded in keyTernaryF32FfmPackTest— the REAL vendored kernel behind REAL dispatch at BitNet-2B dims (k=2560): serves the key, no adapter, matches the referenceKernel-support matrix intentionally unchanged — pack-registered kernels aren't
KernelProvideraccessors (same asbitnet_gemv).Next: #1139 (JNI + Kotlin/Native bridges), #1140 (I2_S GGUF import).
🤖 Generated with Claude Code