Skip to content

[CoreAI] Add root and PTE cache clearing - #23395

Draft
metascroy wants to merge 1 commit into
coreai-v2/06-pte-inspectfrom
coreai-v2/07-cache-clear
Draft

metascroy wants to merge 1 commit into
coreai-v2/06-pte-inspectfrom
coreai-v2/07-cache-clear

Conversation

@metascroy

@metascroy metascroy commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Adds public APIs to clear Core AI cache state, either for a whole assets root or for the models referenced by one PTE (runtime/coreai_cache.{h,mm}).

  • APIs:
    • clear_cache(coreai_assets_dir) clears every keyed entry in an explicit assets root.
    • clear_cache_for_pte(loader, dir) and clear_cache_for_pte(pte_path, dir) clear only the entries for the current platform and architecture that the PTE's Core AI delegates reference. They find those delegates with inspect_coreai_pte, so no Program/Method is constructed and no assets are materialized. Every selected manifest is validated before any key is cleared. A null directory selects the backend's default root.
  • Per key: evict the SDK entry, remove staging, then remove the bookmark. The bookmark is kept until staging removal succeeds so a failed clear can be retried. SDK errors other than confirmed absence preserve the key's files. Keys are processed in sorted order and the first error is returned. Missing roots and entries are no-ops.
  • Everything under a keyed staging/<key> directory belongs to the backend. Outside keyed entries, PTEs, other files, lock files and unkeyed temporaries are never removed.
  • Callers must unload affected models and stop concurrent loads, inference and maintenance until these calls return; per-key locks are not a whole-root barrier.
  • Supporting changes: keyed staging removal in coreai_storage, bookmark inventory/removal in coreai_bookmarks, and SDK eviction plus per-key clearing in coreai_load_coordinator.
  • Tests: cache-clearing and eviction suites in coreai_host_test; coreai_runtime_smoke also links every public cache-clearing overload.
  • README: "Clearing cached assets".

No changes outside backends/apple/coreai.

Stack: 9 of 9, based on #23394. Review only this PR's commit.

Test plan: built locally for macOS 27.0; ctest -N lists coreai_host_test, coreai_swift_bridge_test and coreai_runtime_smoke, which the Core AI workflow runs on the macOS 27 runner. lintrunner is clean.

@pytorch-bot

pytorch-bot Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23395

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 25e467e with merge base 0b3d26d (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 3, 2026
@metascroy
metascroy force-pushed the coreai-v2/06-pte-inspect branch from 7094d5e to f69015e Compare October 4, 2026 19:48
@metascroy
metascroy force-pushed the coreai-v2/07-cache-clear branch from 4ab2733 to d83126b Compare October 4, 2026 19:48
Summary:
Add public APIs to clear Core AI cache state, either for a whole assets root
or for the models referenced by one PTE.

- `runtime/coreai_cache.{h,mm}`:
  - `clear_cache(coreai_assets_dir)` clears every keyed entry in an explicit
    assets root.
  - `clear_cache_for_pte(loader, dir)` and `clear_cache_for_pte(pte_path, dir)`
    clear only the entries for the current platform and architecture that the
    PTE's Core AI delegates reference. They find those delegates with
    `inspect_coreai_pte`, so no Program/Method is constructed and no Core AI
    assets are materialized. Every selected manifest is validated before any
    key is cleared. A null directory selects the backend's default root.
- Per key, clearing preflights the staged source tree, evicts the SDK entry,
  removes staging, then removes the bookmark. The bookmark is kept until
  staging removal succeeds so a failed clear can be retried. SDK errors other
  than confirmed absence preserve the key's files. Keys are processed in
  sorted order and the first error is returned. Each removal syncs the
  directory it changed. Missing roots and entries are no-ops.
- Everything under a keyed `staging/<key>` directory belongs to the backend
  and is removed with that key. Outside keyed entries, PTEs, other files, lock
  files and unkeyed temporaries are never removed.
- Supporting changes: descriptor-relative keyed staging removal in
  `coreai_storage`, bookmark inventory and removal in `coreai_bookmarks`, and
  SDK eviction plus per-key clearing in `coreai_load_coordinator`.
- Tests: cache-clearing and eviction suites in `coreai_host_test`.
  `coreai_runtime_smoke` also links every public cache-clearing overload.
- README: "Clearing cached assets".

Callers must unload affected models and stop concurrent loads, inference and
maintenance until these calls return. Per-key locks are not a whole-root
barrier.

No changes outside `backends/apple/coreai`.

Test Plan:
```
# Local: configure and build only
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_OSX_DEPLOYMENT_TARGET=27.0 -DEXECUTORCH_BUILD_COREAI=ON \
  -DEXECUTORCH_BUILD_TESTS=ON -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON
cmake --build build --target backends/apple/coreai/all
# CI (macOS 27 runner, .github/workflows/coreai.yml)
ctest --test-dir build/backends/apple/coreai --output-on-failure --no-tests=error
```
CTest runs `coreai_host_test` (adding the cache-clearing and eviction suites),
`coreai_swift_bridge_test` and `coreai_runtime_smoke`.

This branch was successfully deployed

1 active deployment
cadence — 25e467eb Deployed Oct 5, 2026 by metascroy via hifi-op-test / hifi4 #31513
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant