Skip to content

npu: Moe trusts the expert metadata's rows/cols, so a transposed pair (the ggml [cols, rows] mistake) loads silently and hands a kernel K and N swapped #1152

Closed

Description

@bong-water-water-bong

What

onebit::npu::Moe reads rows, cols and tiles per routed expert stack out of __metadata__.moe and hands them
unvalidated to every consumer (Moe::expert() returns them as ExpertChunks::rows/.cols, which is where a kernel's
K and N come from). The constructor validates the expert count, the byte count, the offsets and their coverage of the
tensor — but never the geometry those three fields describe, so a metadata block whose rows/cols are transposed
loads silently and gives the kernel a swapped contraction.

This is the failure class #1109/#1110 are about (a container that decodes as noise with no error), and the class the
class comment already advertises as covered: "Throws when … that metadata and the tensor shapes disagree". It does not,
for rows/cols.

Why a transpose is the plausible mistake, and why nothing else catches it

The routed stacks come from ggml [cols, rows, experts]; the container tensor is [experts*rows/32, cols/256, 5120].
A converter that keeps ggml's field order swaps K and N and touches no byte count — offsets, bytes and coverage are
all identical. On this model it is also invisible in tiles, because gate/up are 768 x 2048 and down is
2048 x 768, and (rows/32)*(cols/256) is 192 either way (24x8 vs 64x3). Only the tensor's own shape tells them
apart: [3072, 8, 5120] vs [8192, 3, 5120].

It matters concretely for the parked MoE decode work: the design turns on down_proj being K=768 -> N=2048 while
gate/up are K=2048 -> N=768, so a transposed down is a GEMM contracting over the wrong axis.

Reproducer

tests/npu_moe_test.cpp's crafted harness already writes the tensor shape and the metadata from the same rows/cols,
so it could not express this. Two crafted cases (metadata rows/cols transposed; metadata rows doubled) against the
constructor, on origin/main c2bfe96c:

$ cmake --build build --target npu_moe_test && ./build/npu_moe_test
ok   a byte count that is not whole chunks is refused
ok   an expert count that disagrees with the tensor is refused
ok   a directory with no MoE metadata is refused
FAIL rows/cols transposed in the metadata is refused (tiles alone cannot see it)
FAIL a tile count that disagrees with rows x cols is refused
FAIL

Both Moe constructions succeed, i.e. both directories load and would be served.

Fix

Validate the geometry against the tensor's own shape at construction, where the rest of the metadata is already checked:
rows % 32 == 0, cols % 256 == 0, tiles == (rows/32)*(cols/256), and shape == {experts*rows/32, cols/256, 5120}.

Bar

Same reproducer: both cases refused with a message naming the cause; the crafted matrix's existing cases unchanged; the
real container still loads and the C++ router still picks the reference's experts on both frozen gate oracles.

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