Commit Graph
43 Commits
Author SHA1 Message Date
147d077aa7 feat(inference): unify and optimize WebGPU inference pipeline (#1622)
## Problem

The WebGPU inference pipeline had several structural issues:

1. **No unified protocol** — Kokoro TTS, Whisper ASR, and Background
Removal workers each used their own ad-hoc message formats. Adding a new
model meant reinventing worker communication from scratch.
2. **Infrastructure existed but was disconnected** —
`GPUResourceCoordinator`, `LoadQueue`, `InferenceWorkerManager`, and
`protocol.ts` were all implemented but had zero consumers. The adapters
duplicated the same lifecycle/timeout/mutex patterns independently.
3. **Performance gaps** — Kokoro only offered fp32 on WebGPU (no fp16),
Whisper warm-up compiled shaders for 187.5s of dummy audio, audio
transfer went through unnecessary WAV blob encode/decode, and
`listVoices` reloaded the model every time.
4. **Silent failures** — Whisper worker's `generate()` had no try-catch;
errors were swallowed and the main thread waited until timeout.
5. **No graceful degradation** — Whisper and Background Removal workers
hardcoded `device: 'webgpu'` with no WASM fallback.
6. **No observability** — Only Kokoro had performance tracing. No
adapter reported status to `useInferenceStatus`. No cache management UI
existed.
7. **Dead code accumulation** — Old `KokoroWorkerManager` (232 lines),
legacy Whisper message types, and scattered duplicate constants.

## Changes

### Phase 0 — Critical Performance & Bugs
- Add `fp16-webgpu` dtype for Kokoro TTS (~2x inference speed on
supported GPUs)
- Fix Whisper warm-up tensor from `[1, 128, 3000]` → `[1, 128, 1]`
(minimal shader compilation)
- Fix Whisper worker silent error bug (add try-catch to `generate()` and
`load()`)

### Phase 1 — Data Transfer & Caching
- Switch Kokoro audio to Float32Array transferable (skip WAV blob encode
in worker, lightweight WAV encode on main thread)
- Cache `listVoices` results (skip redundant model reload when adapter
state is `ready`)
- Normalize progress reporting to 0-100 across all adapters,
differentiate `warmup` phase

### Phase 2 — Protocol Unification & Infrastructure
- Migrate all 3 workers + 3 adapters to unified `protocol.ts` message
types (`load-model`, `run-inference`, `model-ready`, `inference-result`,
`progress`, `error`)
- Wire `GPUResourceCoordinator` into all adapters (VRAM allocation
tracking, LRU ordering, memory pressure events)
- Wire `LoadQueue` into all adapters (priority-based sequential model
loading: TTS=10 > ASR=5 > BG_REMOVAL=1)
- Add `coordinator.ts` global singleton for GPU coordinator + load queue
- Add WebGPU detection + WASM fallback in Whisper and Background Removal
workers

### Phase 3 — Error Recovery & Observability
- Add restart logic with exponential backoff to Whisper adapter
(matching Kokoro's existing pattern)
- Integrate `classifyError()` (OOM / DEVICE_LOST / TIMEOUT
classification) in Whisper adapter
- Extend `defaultPerfTracer` to Whisper `transcribe()` and Background
Removal `processImage()`
- Wire `useInferenceStatus` into all 3 adapters (downloading → ready →
terminated lifecycle)

### Phase 4 — Tests
- Add unit tests for `AsyncMutex` (4 tests), `LoadQueue` (4 tests),
`GPUResourceCoordinator` (7 tests) — all 15 passing

### Phase 5 — Cleanup & Features
- Delete old `KokoroWorkerManager` (232 lines, zero consumers)
- Delete orphaned `libs/workers/types.ts` (old Whisper message types)
- Clean up `workers/kokoro/types.ts` (remove legacy message types, keep
domain types)
- Create centralized `constants.ts` (MODEL_IDS, MODEL_NAMES, TIMEOUTS,
MAX_RESTARTS)
- Remove hardcoded WebGPU check from background-removal devtools pages
(worker auto-detects)
- Add `useModelPreload` composable for generic idle-time preloading
- Add `useInferencePreload` composable that reads provider config and
preloads configured local models
- Wire preloading into both `stage-web` and `stage-tamagotchi` App.vue
(Kokoro TTS preloads 3s after init)
- Add `ModelCacheManager.vue` settings component (cache size display,
per-model status, clear cache)
- Document GPU Device isolation architecture in protocol.ts

## After This PR

- All inference workers speak the same protocol → adding a new model
adapter is straightforward
- GPU memory is tracked across all models with automatic pressure
warnings at 80%/95% of VRAM budget
- Models load sequentially via priority queue → no bandwidth/VRAM
contention
- Workers auto-detect WebGPU and fall back to WASM → works on browsers
without WebGPU
- Kokoro TTS preloads during idle time → "instant" first use for
configured users
- All adapters auto-restart on worker crashes (max 3 attempts,
exponential backoff)
- 15 unit tests cover core infrastructure (mutex, queue, coordinator)
- Zero dead code remains in the inference pipeline

## Test Plan

- [x] `pnpm exec vitest run packages/stage-ui/src/libs/inference/` — 15
tests pass
- [x] `pnpm -F @proj-airi/stage-ui exec tsc --noEmit` — no TypeScript
errors
- [x] `pnpm lint:fix` — no lint errors in changed files
- [ ] Manual: verify Kokoro TTS works with fp16-webgpu on a supported
browser
- [ ] Manual: verify Whisper ASR loads and transcribes correctly
- [ ] Manual: verify Background Removal works in devtools page
- [ ] Manual: verify preloading triggers in console (`[Preload] Loading
kokoro-...`)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-04-13 23:19:27 +08:00
Neko Ayaka b18158bce2 chore(computer-use-mcp): remove all hardcoded file path 2026-04-11 05:13:55 +08:00
Neko Ayaka b95d84fba7 perf(stage-tamagotchi): better fallback, matrix smoking test 2026-04-08 04:16:13 +08:00
Richard Pinedo 99be852fe4 feat(stage-tamagotchi,stage-ui,server-sdk,server-runtime): zero-trust websocket authentication (#1582) 2026-04-07 04:05:35 +08:00
jensenhuangfan babdb0e6e7 fix(stage-tamagotchi): harden cross-platform auto-updater flow, diagnostics, logs, and cache cleanup (#1566) 2026-04-06 01:48:02 +08:00
Neko Ayaka ec9436a173 chore(ci): incorrect env passing in GitHub Actions 2026-03-30 03:09:02 +08:00
Neko Ayaka 42dace246a chore(ci): filename renaming still using old legacy file 2026-03-27 17:17:58 +08:00
Neko Ayaka 7b7fbba840 chore(ci): filename change for windows artifact regenerate 2026-03-27 15:55:21 +08:00
Neko Ayaka 9caf6a4966 chore(ci): should directly import config 2026-03-27 15:32:46 +08:00
Neko Ayaka 5532356d13 chore(ci): incorrect manifest path resolution for Windows signed artifact 2026-03-23 21:38:23 +08:00
Neko Ayaka 101ee76a5c chore(ci): incorrect merge latest.yml for macOS 2026-03-23 21:38:23 +08:00
Neko Ayaka 0cd68568a6 fix(ci): re-calculate Windows artifact hash
Authored-by-agent: Codex <267193182+codex@users.noreply.github.com>
2026-03-23 19:20:28 +08:00
Lovehsigure_520andautofix-ci[bot] e952fe779e chore: switch zh-CN desktop download links to GitHub releases (#1442)
* chore: use GitHub release links for zh-CN desktop downloads

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-03-21 11:47:17 +08:00
Neko Ayaka 211c3d72d8 chore(ci): added readme update workflow 2026-03-10 18:20:22 +08:00
Neko Ayaka 0024366d88 chore(ci): added pnpm util, fix macos merge issue 2026-01-11 04:43:50 +08:00
Neko Ayaka 08f4dbb799 chore(ci): should be able to correctly merge macos yaml 2026-01-11 03:50:37 +08:00
Neko Ayaka b6b0bf2e9b chore(ci): windows auto updater file not uploaded & failed to merge macos files 2026-01-08 17:14:31 +08:00
Neko Ayaka 531a426b10 chore(ci): upload auto-update needed files & merge latest-mac.yml
Close #900
2026-01-08 15:52:02 +08:00
藍+85CD 1d9371351b chore(stage-tamagotchi): fix lint warn 2025-11-02 12:54:04 +08:00
Neko Ayaka 3a13c654d3 chore(ci): improve rename and bundle name of flatpak 2025-10-31 17:55:50 +08:00
Neko Ayaka 6b7d498dc8 fix(ci): deb uses amd64 for arch name 2025-10-20 21:19:04 +08:00
Neko Ayaka de15c9d15c fix(ci): rpm uses aarch64 & x86_64 as convention, which fails the name matching 2025-10-20 15:31:08 +08:00
Neko Ayaka cf585ccba0 fix(ci): incorrect version used for renaming artifacts 2025-10-20 13:18:04 +08:00
Neko Ayaka ad786013b0 feat(ci): support to run release-tamagotchi.yml workflow for deb and rpm files 2025-10-20 13:00:30 +08:00
藍+85CD 4a1a33f9b6 chore(deps): replace execa w/ tinyexec (#665) 2025-10-18 19:12:54 +08:00
Sline d744bec754 refactor: start websocket server by server-runtime (#634) 2025-10-04 21:56:30 +08:00
Neko Ayaka 25ead776b9 chore(ci): x86_64 not correctly passed as target 2025-10-04 17:05:41 +08:00
Neko Ayaka 2bf0ccf897 chore(ci): fix naming issue 2025-10-04 03:02:14 +08:00
Sline 55f0c027d2 fix: start tamagotchi (#630) 2025-10-04 02:49:49 +08:00
Neko Ayaka bbbecd8410 chore(ci): fix arch string incorrect 2025-10-04 02:30:28 +08:00
Neko Ayaka a0d212297f chore(ci): fix name 2025-10-04 02:05:37 +08:00
Neko Ayaka a866c5a544 chore(ci): fix artifacts 2025-10-04 01:45:36 +08:00
Neko Ayaka b5a2fb350c refactor(stage-tamagotchi): remove tauri based tamagotchi 2025-10-03 16:07:14 +08:00
b190122bf1 feat(stage-ui,stage-web): Implement Comprehensive Module Settings Management with Frontend UI and Backend WebSocket Integration (#617)
---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Neko Ayaka <neko@ayaka.moe>
2025-10-01 01:11:12 +08:00
Neko Ayaka d721c9c1e3 fix(ci): git tag describe will fail when doing nightly and manual build 2025-07-19 23:11:18 +08:00
Neko Ayaka d434c9486f chore(ci): improve scripts 2025-07-19 20:47:05 +08:00
Neko Ayaka 3e665b25a7 fix(ci): build will have artifact conflicts 2025-07-19 02:23:56 +08:00
Neko Ayaka d69a8e0e02 fix(ci): rename artifacts 2025-07-17 11:23:36 +08:00
Neko fd314058b6 feat(ci): support nightly build (#256) 2025-07-05 17:20:22 +08:00
Neko Ayaka 8ee87f9e70 style(*): stronger import sort 2025-06-30 02:23:40 +08:00
LemonNeko dc28a59d8a fix(stage-tamagotchi): product name changed 2025-06-09 09:21:47 +08:00
LemonNeko 148f27532b release: v0.4.26-beta.3 2025-05-01 19:32:24 +08:00
LemonNeko 15fa1fcfbe refactor(stage-tamagotchi): remove stage electron (#115)
* refactor(stage-tamagotchi): remove stage electron

* fix(stage-tamagotchi): path
2025-04-05 18:04:13 +08:00