Skip to content

Persistent Incremental Csrc Build Cache

Overview

vLLM Ascend builds native third-party libraries and many AscendC custom operators. An exact final-artifact cache is fast when it matches, but any source change invalidates the complete artifact. The persistent incremental csrc cache adds a second level that reuses individual compiler actions across CI jobs, workspaces, and repeated local source builds.

The two cache levels have different roles:

L0 exact final artifact
        |
        | miss
        v
L1 persistent action entries
        |
        v
ordinary source build

The source build remains authoritative. L1 only replaces an equivalent action with previously verified artifacts.

Goals and non-goals

Goals:

  • reuse native build work across CI jobs;
  • reuse native build work across repeated local source builds without remote transport;
  • invalidate only actions whose semantic inputs changed;
  • reuse entries across equivalent checkout and build roots;
  • persist local entries through the repository's cache transport;
  • publish artifacts safely under concurrent builds; and
  • degrade to an ordinary build when an optional cache layer is unavailable.

Non-goals:

  • replacing compiler dependency tracking;
  • replacing the L0 final-artifact cache;
  • sharing file locks across physical nodes;
  • requiring historical source snapshots to contain the cache engine; or
  • migrating older cache schemas in place.

Architecture

workflow
  |
  +-- restore L1 snapshot
  |
  +-- CMake adapter
  |     |
  |     +-- action identity
  |     +-- entry lookup/validation
  |     +-- restore or compile
  |     +-- local entry save
  |     `-- custom-operator publication
  |
  `-- publish changed L1 snapshot (writers only)

build_cache.py owns local action identity, entries, locking, restoration, and publication. CMake declares semantic inputs and invokes the engine. Composite actions own persistent snapshot keys and OBS transport. Workflows only decide where to restore and which trusted build roles may publish.

The two levels have separate correctness boundaries. L0 is the outer exact final-artifact snapshot: a hit can skip the native build only when its full artifact key matches. L1 is an outer persistent snapshot of the local action cache. Restoring an L1 snapshot only makes candidate entries available; the inner engine still validates each manifest, action key, schema, artifact model, and artifact contents before accepting an entry. An L1 hit therefore never overrides inner correctness, and an L1 miss never changes the correctness of a normal source build.

The existing image-build L0 key uses architecture, base-image tag, and the tracked csrc hash. Because an L0 hit skips compilation and therefore skips L1 entry validation, that key assumes a fixed compiler-image registry and immutable tags. Changing the registry or republishing a tag with different contents requires a new L0 key namespace or the full image identity in the key; an L1 identity check cannot make an already accepted L0 artifact safe. This PR does not change image-build L0 behavior or add L1 to Docker builds.

Build flow

Cold build

L0 miss
  -> L1 miss
  -> normal source build starts
  -> action MISS
  -> compiler runs
  -> verified local entry save
  -> .updated marker
  -> writer publishes L1
  -> optional L0 publication

Warm build

L0 miss
  -> compatible L1 snapshot restored
  -> .updated marker reset
  -> normal source build starts
  -> action identity calculated
  -> entry validated
  -> action HIT
  -> artifacts restored
  -> compiler action skipped

An all-HIT build does not recreate .updated, so it does not publish another unique remote snapshot.

Cache identity

Each action key is the canonical hash of three independent identities:

prepared_input_hash
    what is compiled

recipe_hash
    how it is compiled

compiler_environment_hash
    which toolchain compiles it

For custom operators, operator_text_hash provides the operator namespace. It does not replace any of the three action-key components.

The entry manifest stores the hashes and the exact artifact model used by the key. A HIT requires the expected schema, domain, action key, artifact model, and verified artifact content.

Prepared-input contract

Prepared inputs include all compiler-visible semantic content, including:

  • generated operator sources;
  • dependent generated sources;
  • shared kernel sources;
  • compiler-visible recipe files; and
  • shared compatibility headers such as cann_compat.h.

Physical checkout and build roots are not semantic identity. UTF-8 text may therefore normalize explicitly declared roots. A root may be normalized only when every semantic object referenced through it is independently covered by the action identity.

For example, a generated adapter can contain:

-include /temporary/root/csrc/common/include/cann_compat.h

The temporary root is normalized, while cann_compat.h content is hashed as a prepared input. Moving the workspace remains a HIT; changing the header is a MISS.

The normalization contract is deliberately narrow:

  • UTF-8 text replaces only explicit normalize roots at path-component boundaries;
  • unrelated backslashes in source code, escapes, and regular expressions remain byte-sensitive;
  • binary inputs remain raw-byte-sensitive;
  • paths outside explicit roots remain sensitive; and
  • symlink identity and resolved semantic content both participate in hashing.

An unreadable explicit semantic input is never silently omitted from identity.

Recipe and compiler environment

The recipe identity covers compiler commands, recipe files, and explicit recipe values after the same root normalization contract. --set-env compiler overrides also participate, using their exact values: an override may point to an input not otherwise covered by the prepared-input identity.

The custom-operator command sets its generated ASCEND_CUSTOM_OPP_PATH through env, so the existing command normalization handles its declared OPP root. Operator host/tiling sources, including the definitions that generate OPP configuration, remain in the operator namespace. The rebuilt tiling library is not hashed again: its log strings can embed a different checkout path even when the sources are identical. With matching source and toolchain inputs, moving the entire csrc/build_cache to another checkout can restore kernel artifacts into an empty build directory. Host libraries still follow the normal build.

Custom-operator callers exclude README, Markdown/reStructuredText and docs/ from both prepared inputs and the operator namespace. They also exclude tests/ when ENABLE_TEST and BENCHMARK are both off. Test builds retain their test-source identity; kernel, host/tiling and shared compiler sources remain identity-sensitive.

The compiler-environment identity covers the host platform, selected compiler profile, tool version output, CANN metadata, and explicit environment values. Absolute compiler installation paths are not semantic when the reported tool identity is equal.

CMake integration contract

vllm_ascend_build_cache_command() is a correctness-sensitive boundary, not only a command wrapper. A caller that changes a compiler-visible input must update the matching identity group in the same change:

  • PREPARED_INPUT describes source, generated, dependent, and shared content;
  • RECIPE_FILE and RECIPE_VALUE describe how that content is built;
  • SET_ENV changes the compiler process environment and must remain in recipe identity whenever its value can affect the output;
  • ENVIRONMENT_FILE, ENVIRONMENT_VALUE, and ENVIRONMENT_TOOL describe the compiler/toolkit environment; and
  • NORMALIZE_PATH removes only a physical location whose referenced semantic content is already covered by one of those identity groups.

EXCLUDE applies consistently to prepared inputs and the operator namespace.

Removing or omitting an identity argument can cause a false HIT. Adding a non-semantic physical path can cause avoidable cross-workspace misses. Changes to the custom-operator generation inputs or the protobuf ExternalProject_Add() command therefore require a cache-identity review.

Schema and snapshot compatibility

Entry manifests use SCHEMA_VERSION = 4. Schema 4 introduced safe textual prepared-input normalization and complete semantic coverage for normalized paths. Older entries naturally miss because the persistent key contains the schema and entry validation rejects a different schema. There is no in-place migration: the first schema-4 build is cold and later builds are warm.

Schema 4 is the first production namespace for this cache. Pre-release local or validation snapshots written by earlier schema-4 candidates are not production compatible and must remain isolated from the production namespace. Any future change that can map an old semantic input to a different meaning requires a schema or identity revision rather than reusing those entries.

PUBLISH_STATE_SCHEMA is separate because build-tree artifact ownership is a different format from persistent entry identity.

The snapshot key command has one compatibility model:

schema
+ architecture
+ canonical SOC
+ explicit compiler-image identity for a nested container build
  or CANN metadata + runtime OS/libc identity for a direct build

The restore action adds the tracked csrc hash and a unique publication suffix. Both producer and consumer keys therefore come from the same implementation. An explicit compiler image takes precedence over the outer runner environment, so a Docker build is keyed by the environment that actually compiles csrc. Direct builds include the runtime OS/libc identity to keep host-built artifacts from crossing incompatible system-header or ABI boundaries.

Prepare, restore, and save actions

prepare_csrc_l1_restore.py is the shared key-preparation helper. It checks that the tested source root contains the cache engine, derives the tracked csrc hash when the caller did not provide one, computes the snapshot key, and exports the cache directory and event-log paths. Unsupported or unidentifiable source snapshots return supported=false so the caller can continue with a normal build.

.github/actions/csrc-l1-restore/action.yaml invokes the helper, restores the keyed outer snapshot with compatibility prefixes, and removes .updated before the build. .github/actions/csrc-l1-save/action.yaml publishes only when the caller supplies a primary key, the local cache contains .updated, and trusted writer credentials are present. The local engine creates .updated only after an entry is newly saved or replaced; an all-hit build consequently does not republish an unchanged snapshot. Restore and save transport failures are availability failures and remain fail-open. A missing or unparsable manifest, or a manifest object that fails schema or artifact validation, is a MISS and is rebuilt. Conflicts that could corrupt shared artifact publication or synchronization fail the build. A JSON manifest with a non-object top level currently raises during validation; it is not covered by the MISS fallback.

Workflow code and tested source are intentionally separate. Persistent action references use uses: $/.github/actions/csrc-l1-restore and uses: $/.github/actions/csrc-l1-save, where $/.github resolves the action implementation from the workflow revision. The caller's repository and source_ref may therefore be a fork or historical snapshot without selecting an incompatible action implementation. Caller inputs still describe the tested checkout: source-root, cache directory, architecture, SOC, toolchain image, and optional csrc hash.

Entry and artifact lifecycle

For each action the engine:

  1. hashes prepared inputs, recipe, and compiler environment;
  2. derives the content-addressed entry path;
  3. acquires the entry and action locks;
  4. validates and restores a matching entry;
  5. otherwise runs the original build command;
  6. discovers and verifies produced artifacts;
  7. atomically saves the local entry;
  8. marks the local L1 snapshot as updated; and
  9. publishes custom-operator artifacts into the shared build output.

The entry manifest is the correctness record. No separate mutable index is required for lookup or invalidation.

Concurrency model

Three lock scopes protect distinct invariants:

  • the entry lock permits one creator for a content-addressed entry;
  • the action lock protects one action's private staging directory; and
  • the publish lock serializes shared custom-operator output publication.

Custom operators compile into private staging directories. Publication tracks artifact ownership and uses atomic replacement, preventing partial output from becoming visible. A conflicting owner for the same relative artifact is a hard error rather than an unsafe overwrite.

Multi-node jobs use a node-scoped cache directory:

/root/.cache/vllm-ascend/csrc-build-cache/<soc>/node-<worker>

The design does not depend on cross-node flock behavior.

Persistent transport and trust

The restore and save composite actions own the fixed OBS transport configuration. The cache engine has no storage API or credential knowledge.

Read access follows the runner's existing OBS authorization. Shared writes require both HW_OBS_AK and HW_OBS_SK; possession of those secrets is the only write-authorization boundary. The save action receives the credentials explicitly from trusted callers and skips publication when either is absent.

Only the central csrc producer publishes L1 in this integration. Image and release-wheel Docker builds retain their existing behavior and do not restore, export, or save persistent L1.

Source-consuming jobs are read-only L1 consumers: they may restore a compatible snapshot before their existing source installation, but they do not publish. Exact-artifact consumers use the central L0 output; an L0 miss continues to the ordinary source build without making persistent L1 transport a prerequisite. Local compilation remains correct but does not create a shared snapshot.

A writer publishes only after a successful build creates or replaces a local entry. OBS restore and save failures remain performance degradations.

Failure behavior

Condition Behavior
Historical source has no cache engine Report unsupported and continue with the ordinary build.
Snapshot environment cannot be fingerprinted Skip L1 restore and continue with the ordinary build.
Snapshot restore fails Continue with an empty local L1.
Entry is absent, unparsable, or has an object manifest that fails validation Compile and replace it.
Manifest is valid JSON with a non-object top level Currently raises during validation; this is an outstanding cache-engine defect.
Entry lock is unavailable Bypass the entry and compile.
Local entry save fails Warn and keep the successful build output.
Snapshot save fails Keep the successful build or verified L0 artifact.
Compiler command fails Fail the build.
Artifact ownership collides Fail rather than overwrite another action's output.
Action or publish synchronization fails Fail when continuing could corrupt shared output.

Operational JSONL events are limited to cache results, warnings, and lock contention. Observability is best effort and never serializes compilation.

Integration patterns

Direct source consumers

Source-consuming jobs restore L1 before their existing source installation and do not publish. Exact-artifact jobs use the canonical producer's L0 output; an L0 miss proceeds through their existing source build without a second direct L1 fallback, except where a caller explicitly enables the optional shared-L1 fallback. Selected-test jobs use that optional fallback after an L0 miss; other L0 consumers, including scheduled upstream E2E, retain their caller- specific source-build policy.

Local source builds

Ordinary local source builds use the same action cache in the repository's ignored csrc/build_cache directory. This is local-only acceleration: no OBS credentials or remote snapshot transport are involved. Set VLLM_ASCEND_BUILD_CACHE_DIR to an absolute path to override this local default. CI uses the same workspace-relative csrc/build_cache transport path, which the restore helper resolves to an absolute directory for builds. Deleting the local directory causes a cold build; it does not change the compiled source or CI's persistent snapshot.

csrc/build.sh keeps two operator_text_hash versions per SOC/operator after a successful csrc build, retaining all actions inside each version. Configure the script-local BUILD_CACHE_MAX_OPERATOR_VERSIONS beside BUILD_CACHE_DIR; nonpositive values disable cleanup. No project-level setting is introduced. HITs and saves refresh recency without deleting versions, so a third version can coexist until the entire csrc build succeeds. Failed builds do not prune.

Cleanup runs at the end of build.sh, after its foreground build commands (including their parallel compilation tasks) have succeeded. No cleanup lock is added; the cache directory must not be used by another concurrent build or prune operation. Cleanup errors never fail the successful build. .updated marks compaction for the next trusted snapshot save. Published outputs are never removed. Schema 4 and action identity are unchanged.

Standalone action or CMake users can explicitly run python3 csrc/scripts/build_cache.py prune --cache-root csrc/build_cache after a successful complete build. --max-operator-versions N changes the default of 2.

This bounds operator source-version history, not disk bytes, third-party entries, action-key variants within a retained version, or historical OBS snapshot counts. Separate concurrent builds must use separate cache directories, or the caller must arrange a maintenance window after all builds finish.

Central producer

The reusable producer restores L1, performs the ordinary source build, verifies final native artifacts, publishes changed L1 state, and then publishes the exact L0 artifact.

All L1 producers and consumers must pass the same literal relative cache-dir: csrc/build_cache to the restore/save actions. runs-on/cache includes that path in its remote cache version: using /tmp on one runner and runner.temp on another separates otherwise compatible keys into different OBS namespaces. The relative archive layout also restores under each consumer's own workspace, not the producer's absolute directory. The producer initially sets VLLM_ASCEND_BUILD_CACHE_DIR=csrc/build_cache. Restore exports its absolute equivalent through GITHUB_ENV for compilation and preserves the original spelling in its cache-dir output. Save consumes that output, not the updated environment variable: using the absolute build directory as the save input would split the OBS namespace again. Restore/save transport failures remain nonfatal; ordinary source compilation is still the fallback. Existing snapshots under the old path namespaces are not migrated, so the first build with the shared path may be cold.

Historical sources

Main2Main and bisect-style builds keep current workflow helpers separate from the selected source tree. A historical tree without build_cache.py reports supported=false and builds normally.

Docker builds

Image and release-wheel workflows are not L1 consumers or writers in this PR. Their existing Docker build, source compilation, and image L0 behavior remain unchanged. A later integration must validate the Docker boundary without embedding persistent L1 in a published image.

Validation summary

Capability Evidence
Action identity, entries, and failure behavior cache-engine unit tests
Concurrent ownership and lock behavior concurrency unit tests
Direct source reuse A2, A3, and 310P cold/warm runs
Selective invalidation operator-local mutation with unrelated HITs
Root-independent semantic identity schema-4 CASE-C regression
Persistent producer cold/warm producer runs

Some exact production callers still require release credentials, caller registration, or multi-node allocation. Structural and component evidence does not replace those caller-specific runtime gates.

Operational debugging

For an unexpected full MISS, compare schema, snapshot compatibility, prepared_input_hash, recipe_hash, and compiler_environment_hash in entry manifests. For broad selective invalidation, inspect the prepared-input set and operator namespace. For historical source, check the restore action's supported output.

Implementation map

Responsibility Source
Cache identity, entries, locking, and publication csrc/scripts/build_cache.py
CMake-to-engine adapter csrc/cmake/build_cache.cmake
Operator semantic inputs csrc/cmake/func.cmake
Third-party semantic inputs csrc/cmake/third_party/ascend_protobuf.cmake
Persistent restore and key orchestration .github/actions/csrc-l1-restore/action.yaml
Persistent changed-snapshot publication .github/actions/csrc-l1-save/action.yaml
Central producer .github/workflows/_build_csrc_cache.yaml