Optimize max peak memory usage and add warnings on mobile for higher models

This commit is contained in:
Lucas Winther committed 2026-07-28 07:23:37 +02:00
1 parent f6019b3a42
commit a0ef5548af
10 files changed
+428 -7

No files matched your search

+19
View File
@@ -93,6 +93,25 @@ export function formatModelSize(bytes: number): string {
return `${Math.round(bytes / 1e6)} MB`;
}
/**
* Approximate peak transient memory a browser-engine load needs (D-027): for
* whichever of the encoder/decoder weight files is larger, transformers.js
* buffers the full download into one JS-heap `Uint8Array`, then
* onnxruntime-web mallocs a second full-size copy inside WASM linear memory
* to parse it at session-creation time — a per-file ~2x peak. The
* sequential-session-load patch (`patches/`) keeps the encoder and decoder
* from both being resident at once, so the two files' peaks don't add
* together; only the larger one sets the peak. Approximate — real browser/
* runtime overhead (page baseline, Cache Storage, other tabs) adds more.
*/
export function estimatePeakLoadBytes(model: WhisperModel, dtype: DtypeSpec): number {
const encDtype = typeof dtype === "string" ? dtype : dtype.encoder_model;
const decDtype = typeof dtype === "string" ? dtype : dtype.decoder_model_merged;
const encoderBytes = model.encoderParamsM * 1e6 * BYTES_PER_PARAM[encDtype];
const decoderBytes = model.decoderParamsM * 1e6 * BYTES_PER_PARAM[decDtype];
return Math.round(2 * Math.max(encoderBytes, decoderBytes));
}
// ---------------------------------------------------------------------------
// dtype defaults (community-known-good; see file header)
// ---------------------------------------------------------------------------