Skip to content

FFM row-major tier: serve mapped packed weights fast on the JVM (MemSeg, follow-up to #1189) #1191

Description

@michalharakal

Follow-up to #1189/#1190, which made WeightResidency.MAPPED serve Q4_K/Q6_K payloads from file-backed pages with zero heap bytes — verified on Android through the JNI direct-buffer kernels (JniMappedKernelPack).

On the desktop JVM the same loader path now emits BufferPackedTensorData, but no fast kernel serves the BLOCKED_ROW_MAJOR × off-heap key there: dispatch falls back to the decoding reference kernel (correct, very slow). The pieces already exist:

  • skainet_q4k_matmul_rm / skainet_q6k_matmul_rm are in the shared native library (FFM binds them by name — RowMajorMatmulParityTest already does exactly that in jvmTest).
  • NativeQ4KMemSegMatmulKernel shows the MemSeg downcall pattern (feed-order symbol today, no dispatch consumer).
  • MemorySegment.ofBuffer(directByteBuffer) bridges MappedBufferStorage/DirectBufferStorage.buffer() into FFM with no copy.

Sketch

  1. NativeQ4KMatmulRmKernel / NativeQ6KMatmulRmKernel (native-cpu jvmMain): downcalls to the _rm symbols taking the weight as a MemorySegment.
  2. A jvmMain view-kernel sibling of JniBufferPackedMatmulKernel that unwraps mapped/direct storage via MemorySegment.ofBuffer and registers under the same exact keys (matmul(FP32 dense contiguous × FP32/Q4_K|Q6_K blocked_row_major)), installed alongside KernelPacks.install(NativeKernelProvider).
  3. Parity test vs the JNI-verified semantics: row-major FFM output must be bit-identical to the feed-order kernel on permuted bytes (same oracle as RowMajorMatmulParityTest).

Why it matters

Desktop JVM inference of mapped models currently pays reference-kernel cost for exactly the tensors #1189 made cheap on Android; with this, MAPPED becomes the best default on both tiers, and StorageCapabilities.mappedServableEncodings stays truthful for the JVM without qualification.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions