fix(wasm): eliminate UI hang during prompt prefill#30
Merged
Conversation
The browser froze for 5-15s after pressing Enter because the prefill loop (running all prompt tokens through 28 layers) had no yield points. ASYNCIFY sleep only fired during token generation, not prefill. Two fixes: 1. quant.h: add emscripten_sleep(0) every 2 tokens in the prefill loop. Browser can repaint and stays responsive. Only active on __EMSCRIPTEN__ builds — native is unaffected. 2. index.html: double-requestAnimationFrame before WASM call ensures "Thinking..." indicator paints before any blocking starts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
unamedkr
added a commit
that referenced
this pull request
Apr 10, 2026
The emscripten_sleep(0) added to quant.h's prefill loop (PR #30) broke ASYNCIFY for the entire quant_generate call. The call stack during tq_forward() is too deep (matmul → SIMD kernels) for ASYNCIFY to unwind/rewind — it silently fails and the generation callback's sleep stops working too. Fix: remove prefill sleep entirely. The prefill blocks the browser for a few seconds (unavoidable without a step-by-step API), but "Thinking..." is shown before via requestAnimationFrame. Token streaming during generation works again. Also: pthreads removed (PR #32) to avoid pthreads+ASYNCIFY conflict, build.sh now uses single-thread SIMD + ASYNCIFY only. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
unamedkr
added a commit
that referenced
this pull request
Apr 10, 2026
) The emscripten_sleep(0) added to quant.h's prefill loop (PR #30) broke ASYNCIFY for the entire quant_generate call. The call stack during tq_forward() is too deep (matmul → SIMD kernels) for ASYNCIFY to unwind/rewind — it silently fails and the generation callback's sleep stops working too. Fix: remove prefill sleep entirely. The prefill blocks the browser for a few seconds (unavoidable without a step-by-step API), but "Thinking..." is shown before via requestAnimationFrame. Token streaming during generation works again. Also: pthreads removed (PR #32) to avoid pthreads+ASYNCIFY conflict, build.sh now uses single-thread SIMD + ASYNCIFY only. Co-authored-by: Claude Opus 4.6 (1M context) <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.
Problem
Browser freezes 5-15s after pressing Enter. "Thinking..." indicator never shows because the prefill loop blocks the main thread before the browser can paint.
Root cause
The prefill loop (
quant.h:15465) runstq_forward()for each prompt token with zero yield points. ASYNCIFYemscripten_sleep(0)only existed in the token generation callback, not during prefill.Fix
quant.h:emscripten_sleep(0)every 2 tokens during prefill (guarded by__EMSCRIPTEN__— native unaffected)index.html: doublerequestAnimationFramebefore WASM call guarantees "Thinking..." paints first🤖 Generated with Claude Code