Skip to content

fix: guard quantiseAndSend against zero-length sequence and hidden dim#78

Merged
devlux76 merged 2 commits intoclaude/wasm-marshalling-performance-DwBi0from
copilot/sub-pr-77
Mar 21, 2026
Merged

fix: guard quantiseAndSend against zero-length sequence and hidden dim#78
devlux76 merged 2 commits intoclaude/wasm-marshalling-performance-DwBi0from
copilot/sub-pr-77

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 21, 2026

quantiseAndSend() had no input validation before calling into the WASM kernel. With seqLen=0, the kernel computes lastTokenPos = seqLen - 1 = -1, which wraps to a large address in WASM linear memory — an invalid read.

Changes

  • src/worker.ts — Added early-return guards at the top of quantiseAndSend() for both seqLen < 1 and hiddenDim < 1, logging a warning and skipping the kernel call:
if (seqLen < 1) {
  workerLog('warn', 'quantiseAndSend: seqLen < 1; skipping Q² quantisation', { seqLen });
  return;
}
if (hiddenDim < 1) {
  workerLog('warn', 'quantiseAndSend: hiddenDim < 1; skipping Q² quantisation', { hiddenDim });
  return;
}

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI changed the title [WIP] Move Q² WASM kernel from main thread to worker fix: guard quantiseAndSend against zero-length sequence and hidden dim Mar 21, 2026
Copilot AI requested a review from devlux76 March 21, 2026 06:31
@devlux76 devlux76 marked this pull request as ready for review March 21, 2026 06:33
Copilot AI review requested due to automatic review settings March 21, 2026 06:33
@devlux76 devlux76 merged commit 5df9569 into claude/wasm-marshalling-performance-DwBi0 Mar 21, 2026
@devlux76 devlux76 deleted the copilot/sub-pr-77 branch March 21, 2026 06:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds input validation to quantiseAndSend() in the worker to avoid unsafe/invalid WASM kernel calls when the embedding shape is degenerate (e.g., zero-length sequences).

Changes:

  • Add early-return guard for seqLen < 1 with a warning log.
  • Add early-return guard for hiddenDim < 1 with a warning log.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/worker.ts
Comment on lines +284 to +291
if (seqLen < 1) {
workerLog('warn', 'quantiseAndSend: seqLen < 1; skipping Q² quantisation', { seqLen });
return;
}
if (hiddenDim < 1) {
workerLog('warn', 'quantiseAndSend: hiddenDim < 1; skipping Q² quantisation', { hiddenDim });
return;
}
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

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

The new guards prevent the seqLen=0/hiddenDim=0 trap, but quantiseAndSend still doesn’t validate other documented kernel preconditions (e.g., n must be a power of 2 and ≤ 16,384, and the buffer must contain at least the last-token row). Without these checks it’s still possible to call into the WASM kernel with unsupported dimensions or an undersized buffer, which can lead to traps or incorrect fingerprints. Consider extending the early validation to enforce the kernel’s n constraints (and, if applicable, the expected byteLength for the given dtype) before copying/calling into WASM.

Copilot uses AI. Check for mistakes.
Comment thread src/worker.ts
Comment on lines +284 to +291
if (seqLen < 1) {
workerLog('warn', 'quantiseAndSend: seqLen < 1; skipping Q² quantisation', { seqLen });
return;
}
if (hiddenDim < 1) {
workerLog('warn', 'quantiseAndSend: hiddenDim < 1; skipping Q² quantisation', { hiddenDim });
return;
}
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

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

This change introduces new early-return behavior for invalid seqLen/hiddenDim, but there’s no unit test asserting that the Q² kernel is not invoked in these cases. Since the repo already has worker unit tests, consider adding a test that triggers the embedding extraction path with seqLen=0 and/or hiddenDim=0 and verifies getKernel/quantise are not called (and that the worker continues without error).

Copilot uses AI. Check for mistakes.
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.

3 participants