Skip to content

LiteRT GPU program cache grows ~0.5 GB per engine create and never speeds up init #552

Closed

Description

@leehack

Found while re-running #521 on macOS arm64 (M4 Max, macOS 26.6.2), llamadart 6cea70af2, LiteRT-LM 0.17.0-6, Qwen3.5-0.8B INT8, GPU backend.

Observed

  • *_mldrift_program_cache.bin in the LiteRT cache directory grows ~0.5 GB per GPU engine create: 2.5 GB after the cold run, 5.0 GB, then 6.9 GB across the following runs. Total cache directory reached 7.6 GB for one 0.8B model.
  • The cache gives no benefit: shader init stays ~32 s per create, cold or warm.
  • From the 3rd create onward, and on every warm create, the runtime logs DATA_LOSS: Deserialization failed (webgpu/inference_context.cc:187), 10 lines per create. Non-fatal.

Reading (unverified): the serialized program cache fails to deserialize, so every create recompiles and appends a new serialization instead of replacing it.

Impact: unbounded disk growth in the app cache directory for any app that reloads a LiteRT GPU model; severe on mobile.

Not checked: other platforms/GPUs, portable bundle (this was JIT dart run), whether the growth is in the runtime or in how llamadart passes the cache path. If it reproduces with the native C API alone it belongs in leehack/litert-lm-native or upstream.

Acceptance: cache size is bounded across repeated GPU engine creates, and either a warm create is measurably faster or the program cache is not written.

Activity

  1. added
    bug
    blocked-runtime
    priority:P2
    on Sep 21, 2026
  2. leehack commented on Sep 21, 2026

    @leehack
    OwnerAuthor

    Reproduced on macOS arm64, LiteRT-LM 0.17.0-6, Qwen3.5-0.8B INT8, GPU, llamadart 82cbb101: one process, fresh cache dir, one engine at a time, 8-token generation and full dispose between creates.

    Create *_mldrift_program_cache.bin Init DATA_LOSS
    1 (cold) 539,470,408 51.8 s no
    2 1,078,897,224 50.6 s no
    3 1,608,917,872 50.8 s 10 lines
    4 (file deleted first) 539,470,408 47.9 s no
    • Growth happens inside engine_create only; the weight cache stays at 751,917,896 bytes. (Init times are inflated: another GPU run shared the machine.)
    • Create 2 silently misses the cache for the 10 prefill graphs; create 3 logs Deserialization failed: DATA_LOSS (delegate_kernel.cc:808) for them. Deleting the file costs no init time, so the cache gives no benefit for this model.
    • llamadart only passes a fixed directory ($TMPDIR/llamadart_litert_lm on macOS/Android, litert_lm_service.dart _defaultCacheDir; null elsewhere, so the runtime caches next to the model) through litert_lm_engine_settings_set_cache_dir. It never names, opens or deletes cache files, and only one engine is alive at a time. Runtime-side.
    • The C API has no program-cache switch (SetDisableProgramCache / cache_compiled_shaders_only exist in C++ only); the :nocache sentinel also disables the GPU weight cache.
    • Looks model-specific: in a long-lived cache dir the Qwen3.5 program cache is 15 GB while gemma-4-E2B is 44 MB and Qwen3-0.6B 7.8 MB (only Qwen3.5 was run here).
    • No identical upstream report. Closest: MTP + GPU (Metal): mldrift cache files corrupted on 2nd+ initialization (iOS) google-ai-edge/LiteRT-LM#2503 (open; mldrift cache corrupt on 2nd+ init, workaround is deleting the cache files).

    Not checked: other platforms/GPUs, two processes on one directory, a portable bundle.

    Options for llamadart, none implemented: (1) document the cache directory and make it configurable; (2) opt-in prune of *_mldrift_program_cache.bin above a size cap before engine create, logged so the bug stays visible; (3) a C setter for the program-cache switch in leehack/litert-lm-native; (4) an upstream report referencing LiteRT-LM#2503.

  3. leehack commented on Sep 21, 2026

    @leehack
    OwnerAuthor

    Mitigation from #565 verified on real hardware (Apple M4 Max, Metal, main 8cdfb2ad2, public LlamaEngine API, Qwen3.5-0.8B INT8, throwaway liteRtLmCacheDir, six creates in one process):

    Create liteRtLmMaxProgramCacheBytes Program cache before → after Deleted
    1 null (dir missing) → 539,470,408 no
    2 null 539,470,408 → 1,078,897,224 no
    3 600,000,000 1,078,897,224 → 539,470,408 yes, one warning
    4 600,000,000 539,470,408 → 1,069,491,056 no (under cap)
    5 0 1,069,491,056 → 539,470,408 yes, one warning
    6 0, native log level none 539,470,408 → 539,470,408 (new mtime) yes, silently

    The weight cache (751,917,896 bytes) kept its size and mtime throughout; the missing nested directory was created at the lazy engine create; output was identical and coherent in every step. DATA_LOSS appeared only on create 4 (appending to a previously capped file), not on create 2.

    Not exercised on hardware: *.xnnpack_cache preservation (no such file on the GPU path), the prune-failure path, links, other platforms.

  4. added a commit that references this issue on Oct 9, 2026
  5. leehack commented on Oct 14, 2026

    @leehack
    OwnerAuthor

    Re-checked on LiteRT-LM v0.18.0 published bytes for the 0.12.0 validation (#966): not fixed for Qwen3.5-0.8B_int8.litertlm.

    macOS arm64 (M4 Max, Metal through WebGPU), main 29c52578, portable validation bundle, fresh cache directory, six engine creates across two processes (qwen35-litert-gpu run twice):

    Create Program cache (bytes) Create time DATA_LOSS lines
    1 (cold) 539,205,104 48.9 s 0
    2 1,069,010,176 51.9 s 10
    3 1,598,803,200 54.1 s 10
    4 (new process) 2,128,596,224 52.3 s 10
    5 2,658,389,248 48.4 s 10
    6 3,188,182,272 50.1 s 10
    • Every create appends 529.8 MB; the weight cache stays at 751,917,896 bytes.
    • Warm creates are no faster. Two of twelve graphs initialise from the cache; ten log Deserialization failed: DATA_LOSS (ml_drift/webgpu/inference_context.cc:187), now from the second create on.
    • Acceptance is not met. The liteRtLmMaxProgramCacheBytes cap remains the mitigation; it was not re-exercised here, but the file suffix it matches is unchanged.

    Not checked: other platforms, the VL bundle, two processes sharing a directory at once. Not independently reviewed.

  6. leehack commented on Oct 14, 2026

    @leehack
    OwnerAuthor

    Re-checked on v0.18.0 (2026-10-14): unchanged for Qwen3.5 (+530 MB per engine create). The opt-in liteRtLmMaxProgramCacheBytes cap remains the mitigation; whether to set a default cap is an open decision.

  7. leehack commented on Oct 14, 2026

    @leehack
    OwnerAuthor

    Decision (2026-10-14): set a default liteRtLmMaxProgramCacheBytes cap of about 1 GiB per cache directory, overridable to a higher value or unlimited. This changes default behaviour, so it targets the next minor release, not 0.12.x.

  8. leehack commented on Oct 15, 2026

    @leehack
    OwnerAuthor

    Closed with the bounded-cache mitigation shipped in 0.13.0 by #1089 (via #1095): a 1 GiB default program-cache cap per directory, with -1 for unlimited. Warm-create speed and cache growth depend on the LiteRT-LM runtime (google-ai-edge/LiteRT-LM#2503).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocked-runtimebugpriority:P2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions