Runtime Options and Cache#
This page covers the Core AI backend’s load-time options and how it caches specialized models. See the overview for building the runtime and loading a .pte.
Options#
The Core AI backend accepts these options when a model is loaded. All are optional.
Option |
Type |
Default |
Effect |
|---|---|---|---|
|
string |
|
Where the backend keeps its bookmarks to Core AI’s cached models. See Cache. |
|
string |
|
Which compute units Core AI may use. See Compute units. |
|
bool |
|
Optimize for input shapes that change between calls. See Frequent reshapes. |
|
bool |
|
Load a multi-method model once and share it across its methods. See Sharing a multi-method model. |
Options are grouped by backend; Core AI’s are read from the CoreAIBackend entry. In C++, pass them to Module::load through a LoadBackendOptionsMap:
#include <executorch/extension/module/module.h>
#include <executorch/runtime/backend/options.h>
using executorch::extension::Module;
using namespace executorch::runtime;
Error load_model(Module& module) {
BackendOptions<2> options;
ET_CHECK_OK_OR_RETURN_ERROR(
options.set_option("coreai_compute_unit", "prefer_neural_engine"));
ET_CHECK_OK_OR_RETURN_ERROR(
options.set_option("coreai_expect_frequent_reshapes", true));
LoadBackendOptionsMap map;
ET_CHECK_OK_OR_RETURN_ERROR(map.set_options("CoreAIBackend", options.view()));
return module.load(map);
}
Here module is a Module constructed from the .pte path. String values are limited to 255 bytes.
In Python, pass them to load_program:
program = Runtime.get().load_program(
"mv2_coreai.pte",
backend_options={"CoreAIBackend": {"coreai_compute_unit": "cpu_only"}},
)
Options apply to the model being loaded, so different models in one app can use different options.
Compute Units#
coreai_compute_unit chooses which compute units Core AI may run the model on. Each value maps to one of the configurations the Core AI SDK provides:
Value |
Allowed compute units |
Core AI |
|---|---|---|
|
CPU, GPU, Neural Engine |
|
|
CPU |
|
|
CPU, GPU, Neural Engine, preferring the CPU |
|
|
CPU, GPU, Neural Engine, preferring the GPU |
|
|
CPU, GPU, Neural Engine, preferring the Neural Engine |
|
cpu_onlyis the only restriction. Theprefer_*values make Core AI run as much of the model as it can on that unit, and fall back to the other units for operations the preferred unit can’t run.There are no other combinations. The SDK doesn’t offer, for example, CPU and Neural Engine only.
An unknown value fails the load with InvalidArgument. Each value is specialized and cached separately.
Frequent Reshapes#
Set coreai_expect_frequent_reshapes for models with dynamic inputs whose shapes change from call to call. It sets Core AI’s SpecializationOptions.expectFrequentReshapes, in addition to the compute unit chosen above. Each value is specialized and cached separately.
For ahead-of-time compiled models, also set the matching export option, AOTCompileConfig(expect_frequent_reshapes=True).
Cache#
Specialization can take much longer than the rest of a load, so its result is cached. For example, a small model that took 118 ms to load the first time took 7 ms afterwards. Two directories are involved:
Core AI’s model cache |
The backend’s bookmark directory |
|
|---|---|---|
Holds |
The specialized models |
One bookmark per specialized model, plus lock files |
Managed by |
Core AI |
The ExecuTorch backend |
Location |
Chosen by Core AI. Currently |
|
Configurable |
No |
Yes, with |
On a model’s first load, Core AI specializes it and stores the result in its own model cache. Core AI then returns a bookmark, a small handle that identifies the model in its cache, and the backend saves it in the bookmark directory. Later loads, including from new processes, pass the bookmark back to Core AI to restore the specialized model without specializing it again.
coreai_cache_dir only moves the bookmarks: Core AI always stores specialized models in its own cache, and ExecuTorch can’t change where. Don’t modify Core AI’s cache directly; use clear_cache to remove models from it.
Each combination of model, platform, device architecture, coreai_compute_unit and coreai_expect_frequent_reshapes is specialized and cached separately. The methods of a multi-method export share one entry.
The backend lets the system purge Core AI’s model cache under storage pressure. The next load of a purged model specializes it again, which needs the model data from the .pte, so keep the .pte (and any external data files) available.
Bookmark Directory#
The bookmark directory defaults to executorch_coreai in the app’s Caches directory. Besides bookmarks and lock files, it briefly holds a copy of a model’s Core AI bundle, extracted from the .pte, while that model is being specialized. To use another directory, set coreai_cache_dir:
Use an absolute path, on a local APFS volume, that only this backend uses.
Don’t rename, replace or delete the directory while models are loading, running or being cleared.
If you load a model with a custom directory, pass the same directory when clearing its cache.
The backend excludes the directory from backups. It creates new directories with NSFileProtectionCompleteUntilFirstUserAuthentication, so on iOS the cache stays usable while the device is locked, once it has been unlocked after starting up.
Clearing the Cache#
The backend provides C++ functions to remove cached models, declared in executorch/backends/apple/coreai/runtime/coreai_cache.h:
namespace executorch::backends::coreai {
runtime::Error clear_cache(const char* coreai_cache_dir = nullptr);
runtime::Error clear_cache_for_pte(
runtime::DataLoader& pte, const char* coreai_cache_dir = nullptr);
runtime::Error clear_cache_for_pte(
const char* pte_path, const char* coreai_cache_dir = nullptr);
}
clear_cacheremoves every model that has a bookmark in the bookmark directory.clear_cache_for_pteremoves the models for one.pte, for every device, architecture and option combination they were loaded with.
Both use each bookmark to remove the specialized model from Core AI’s model cache, then delete the bookmark. They only reach models with a bookmark in the given directory. Without a directory argument, they use the default bookmark directory. They run synchronously, and require that nothing else is using the affected models: unload them first, and make sure no process loads, runs or clears Core AI models in that cache directory at the same time. The next load of a cleared model specializes it again.