Skip to content

[WebNN EP] Fix MatMulNBits when K is not a multiple of block_size - #31152

Merged
fdwr merged 1 commit into
microsoft:mainfrom
Honry:matmulnbits-pads-K
Jul 31, 2026
Merged

[WebNN EP] Fix MatMulNBits when K is not a multiple of block_size#31152
fdwr merged 1 commit into
microsoft:mainfrom
Honry:matmulnbits-pads-K

Conversation

@Honry

@Honry Honry commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Description

MatMulNBits stores B as [N, n_blocks_per_col, blob_size] with n_blocks_per_col = ceil(K / block_size), so the dequantized weights span n_blocks_per_col * block_size (padded_K) columns, which is >= K. The previous code reshaped the dequantized operand directly to [N, K], an invalid element-count mismatch whenever K % block_size != 0.

Motivation and Context

Reshape to [N, padded_K] first, then slice off the padding columns to obtain exactly [N, K] before transpose and matmul.

MatMulNBits stores B as [N, n_blocks_per_col, blob_size] with
n_blocks_per_col = ceil(K / block_size), so the dequantized weights span
n_blocks_per_col * block_size (padded_K) columns, which is >= K. The
previous code reshaped the dequantized operand directly to [N, K], an
invalid element-count mismatch whenever K % block_size != 0.

Reshape to [N, padded_K] first, then slice off the padding columns to
obtain exactly [N, K] before transpose and matmul.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@Honry

Honry commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

@fdwr, PTAL, thanks!

@fdwr fdwr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@fdwr
fdwr merged commit 38dee6c into microsoft:main Jul 31, 2026
86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants