agentleFS
Hugging FaceKnown publisherhuggingface/optimum-neuron/optimum/neuron/cache/AGENTS.md

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…

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
  1. Very specific:

    All defined in optimum/commands/neuron/cache.py:

    line 78

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

Cache Subsystem Agent Guide

Module Overview

FilePurposeNeuronx required?
cleanup.pyLocal compile cache inspection and cleanup (get_local_cache_status, cleanup_local_cache)No
hub_cache.pyHF Hub cache proxy, monkey-patching, registry, sync with retryYes (runtime)
entries/cache_entry.pyModelCacheEntry dataclass — serialization, arch digest, hashNo
entries/single_model.py, entries/multi_model.pyConcrete entry typesNo
training.py, traced.pyLegacy cache wrappers for training and traced model pathsYes
optimum_neuron_cc_wrapper.pyCompiler wrapper hooksYes

CLI commands live in optimum/commands/neuron/cache.py (not in this directory).

Local Compile Cache

Path Resolution

  1. cache_dir kwarg (explicit)
  2. NEURON_COMPILE_CACHE_URL env var (local path or S3 URL)
  3. 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

StateneffdoneloglockAction
Successyesyes——Keep
Failed——yes—Poisoned — remove to allow recompilation
Locked———yesActive compilation in progress
Stale lock———yesOrphaned 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:

  1. Calls cleanup_local_cache() to remove failed entries and stale locks (avoid syncing poison)
  2. Creates CompileCacheHfProxy pointing at the Hub repo
  3. Calls proxy.synchronize() which uses upload_folder() with parent_commit for optimistic concurrency
  4. 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:

CommandDescription
cache createCreate a Hub cache repo
cache setSet active Hub cache repo locally
cache synchronizeUpload local cache to Hub (cleans up first)
cache lookupSearch Hub cache for compiled models
cache statusShow local cache entry counts, sizes, compiler versions
cache cleanupRemove 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 URL
  • create_compile_cache(cache_url) — factory for CompileCacheFs or CompileCacheS3
  • cache.get_hlos() → (hlos, locked, done, failed) — categorized HLO paths
  • cache.clear_locks() — remove all .lock files
  • cache.clean() — shutil.rmtree the cache

Testing

CPU-only unit tests (no Neuron hardware):

  • tests/decoder/test_cache_cleanup.py — tests for cleanup.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 — runs test_cache.py on inf2 hardware

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.