optimum-neuron / cache
AGENTS.md270 starsChanged 10 months agoApache-2.0View on GitHub
CLI commands live in optimum/commands/neuron/cache.py (not in this directory). Failed entries permanently block recompilation of the same graph. This is the primary reason cleanup_local_cache() exists. The Hub cache integration works by replacing libneuronxla.create_compile_cache at runtime: CompileCacheHfProxy wraps a local CompileCacheFs (or CompileCacheS3) and adds Hub lookup/download as a fallback. It delegates all writes to the local cache — Hub upload only happens via explicit synchronize(). The patching utility is in optimum/neuron/utils/patching.py. synchronize_hub_cache() uploads the local cache to a HF Hub…
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository32 other files it gives its agents.
- AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/backend/modules/attention/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/gemma3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/granite/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/llama4/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/llama/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/mixtral/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/phi3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/qwen2/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/qwen3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/qwen3_moe/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/smollm3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/smolvlm/AGENTS.mdAGENTS.md
- optimum/neuron/vllm/AGENTS.mdAGENTS.md
- tests/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- optimum/neuron/cache/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/gemma3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/granite/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/llama4/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/llama/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/mixtral/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/phi3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/qwen2/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/qwen3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/qwen3_moe/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/smollm3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/smolvlm/CLAUDE.mdCLAUDE.md
- optimum/neuron/vllm/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
The file
We split this AGENTS.md into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 78
All defined in optimum/commands/neuron/cache.py:
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Cache Subsystem Agent Guide
Module Overview
| File | Purpose | Neuronx required? |
cleanup.py | Local compile cache inspection and cleanup (get_local_cache_status, cleanup_local_cache) | No |
hub_cache.py | HF Hub cache proxy, monkey-patching, registry, sync with retry | Yes (runtime) |
entries/cache_entry.py | ModelCacheEntry dataclass — serialization, arch digest, hash | No |
entries/single_model.py, entries/multi_model.py | Concrete entry types | No |
training.py, traced.py | Legacy cache wrappers for training and traced model paths | Yes |
optimum_neuron_cc_wrapper.py | Compiler wrapper hooks | Yes |
CLI commands live in optimum/commands/neuron/cache.py (not in this directory).
Local Compile Cache
Path Resolution
cache_dirkwarg (explicit)NEURON_COMPILE_CACHE_URLenv var (local path or S3 URL)- Default:
/var/tmp/neuron-compile-cache
Directory Layout
/var/tmp/neuron-compile-cache/
neuronxcc-<compiler_version>/
MODULE_<hlo_hash>+<flags_hash>/
model.hlo_module.pb # serialized HLO graph
compile_flags.json # compiler flags
model.neff # compiled binary (success only)
model.done # empty marker (success only)
model.log # error log (failure only)
model.hlo_module.pb.lock # lock file (active compilation only)Entry States
| State | neff | done | log | lock | Action |
| Success | yes | yes | — | — | Keep |
| Failed | — | — | yes | — | Poisoned — remove to allow recompilation |
| Locked | — | — | — | yes | Active compilation in progress |
| Stale lock | — | — | — | yes | Orphaned from crash — remove (if no neuronx-cc running) |
| Empty | — | — | — | — | HLO uploaded, never compiled — harmless |
Failed entries permanently block recompilation of the same graph. This is the primary reason cleanup_local_cache() exists.
libneuronxla Monkey-Patching
The Hub cache integration works by replacing libneuronxla.create_compile_cache at runtime:
# hub_cache.py — hub_neuronx_cache() context manager
patch_everywhere("create_compile_cache", hf_create_compile_cache, "libneuronxla")
try:
yield # all compilations during this block use CompileCacheHfProxy
finally:
patch_everywhere("create_compile_cache", create_compile_cache, "libneuronxla")CompileCacheHfProxy wraps a local CompileCacheFs (or CompileCacheS3) and adds Hub lookup/download as a fallback. It delegates all writes to the local cache — Hub upload only happens via explicit synchronize().
The patching utility is in optimum/neuron/utils/patching.py.
Hub Cache Sync
synchronize_hub_cache() uploads the local cache to a HF Hub repo:
- Calls
cleanup_local_cache()to remove failed entries and stale locks (avoid syncing poison) - Creates
CompileCacheHfProxypointing at the Hub repo - Calls
proxy.synchronize()which usesupload_folder()withparent_commitfor optimistic concurrency - On HTTP 412 conflict: exponential backoff + jitter, up to 5 retries (constants:
_SYNC_MAX_RETRIES,_SYNC_BASE_WAIT_SECS,_SYNC_MAX_WAIT_SECS)
Registry Structure
Cache entries are indexed under 0_REGISTRY/{optimum_version}/{arch_digest}/{hash}.json with symlink aliases at 0_REGISTRY/{optimum_version}/{model_type}/{org}/{model}/{hash}.json.
CLI Commands
All defined in optimum/commands/neuron/cache.py:
| Command | Description |
cache create | Create a Hub cache repo |
cache set | Set active Hub cache repo locally |
cache synchronize | Upload local cache to Hub (cleans up first) |
cache lookup | Search Hub cache for compiled models |
cache status | Show local cache entry counts, sizes, compiler versions |
cache cleanup | Remove failed/locked/empty entries from local cache |
cleanup flags: --all (include empty), --old-versions, --wipe, --dry-run, --cache_dir.
libneuronxla APIs
Used from libneuronxla.neuron_cc_cache:
CacheUrl.get_cache_url(cache_dir=None)— resolve cache URLcreate_compile_cache(cache_url)— factory forCompileCacheFsorCompileCacheS3cache.get_hlos()→(hlos, locked, done, failed)— categorized HLO pathscache.clear_locks()— remove all.lockfilescache.clean()—shutil.rmtreethe cache
Testing
CPU-only unit tests (no Neuron hardware):
tests/decoder/test_cache_cleanup.py— tests forcleanup.py(local cache status/cleanup)tests/decoder/test_cache_sync_retry.py— tests for Hub sync retry logic
Integration tests (require Neuron hardware + Hub access):
tests/decoder/test_cache.py— end-to-end Hub cache sync: compile, sync, verify registry entries
CI:
.github/workflows/test_cpu_compilation.yml— runs CPU-only cache unit tests.github/workflows/test_inf2_llm.yml— runstest_cache.pyon inf2 hardware
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

