Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/automation.just
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Offline automation and reporting recipes; no native build inputs.

inference-contracts:
@PYTHONPATH=scripts "{{ automation_python }}" scripts/tests/inference_build.py
@PYTHONPATH=scripts "{{ automation_python }}" scripts/tests/inference_contracts.py
@PYTHONPATH=scripts "{{ automation_python }}" scripts/tests/jy_read_recovery.py
@PYTHONPATH=scripts "{{ automation_python }}" scripts/tests/inference_markdown.py
Expand Down
36 changes: 29 additions & 7 deletions scripts/inference-build.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ def cache_key(inputs):
return hashlib.sha256(data.encode("utf-8")).hexdigest()


def native_inputs():
# Bind Cargo's bundled archives and compiler outputs to the same source recipe.
# Git tree entries include paths and blob identities, including removed patches.
paths = ["llama_cpp", "Justfile", "crates/skippy-ffi", "scripts/build-llama.sh",
"scripts/prepare-llama.sh", "scripts/prepare-whisper.sh", "scripts/lib/cuda-toolkit.sh",
"scripts/inference-build.py"]
return hashlib.sha256(capture(["git", "ls-tree", "-r", "HEAD", "--", *paths]).encode()).hexdigest()


def dependency_key(inputs):
return cache_key({name: value for name, value in inputs.items() if name != "source"})


def source_inputs(sha):
environment = {
name: value for name, value in os.environ.items()
Expand All @@ -30,7 +43,10 @@ def source_inputs(sha):
}
}
return {
"schema": 1, "source": sha, "os": platform.system().lower(), "arch": platform.machine(),
"schema": 2, "native_inputs_sha256": native_inputs(), "source": sha,
"os": platform.system().lower(), "arch": platform.machine(),
"compiler_extra_files": {path: digest(Path(path)) for path in
filter(None, os.environ.get("SCCACHE_EXTRAFILES", "").split(os.pathsep))},
"cargo_lock_sha256": digest(Path("Cargo.lock")), "rust": capture(["rustc", "-vV"]),
"cuda": capture(["nvcc", "--version"]), "cxx": capture(["c++", "--version"]),
"cmake": capture(["cmake", "--version"]), "backend": "cuda", "architectures": "121",
Expand All @@ -45,15 +61,21 @@ def install_binaries(directory):


def build(sha, root, inputs, key, cached):
dependency_key = cache_key({name: value for name, value in inputs.items() if name != "source"})
cargo_root = root.parent / "cargo-v1"
dependencies = dependency_key(inputs)
cargo_root = root.parent / "cargo-v2"
cargo_root.mkdir(parents=True, exist_ok=True)
with lock(cargo_root / f"{dependency_key}.lock"):
cargo_target = cargo_root / dependency_key
with lock(cargo_root / f"{dependencies}.lock"):
cargo_target = cargo_root / dependencies
native_identity = Path("target/inference-native-inputs.json").resolve()
native_identity.parent.mkdir(parents=True, exist_ok=True)
write_json(native_identity, {"schema": 2, "native_inputs_sha256": inputs["native_inputs_sha256"]})
extra_files = [*filter(None, os.environ.get("SCCACHE_EXTRAFILES", "").split(os.pathsep)),
str(native_identity)]
environment = {
"CARGO_TARGET_DIR": str(cargo_target), "SCCACHE_DIR": str(root.parent / "compiler"),
"SCCACHE_BASEDIRS": str(Path.cwd()), "CARGO_INCREMENTAL": "0", "SCCACHE_SERVER_PORT": "4227",
"SCCACHE_CACHE_SIZE": "20G", "CARGO_PROFILE_DEV_DEBUG": "0",
"SCCACHE_EXTRAFILES": os.pathsep.join(extra_files),
}
sccache = shutil.which("sccache")
if sccache:
Expand All @@ -66,7 +88,7 @@ def build(sha, root, inputs, key, cached):
command(["just", "inference-build-uncached"], overrides=environment, timeout=7200)
install_binaries(cargo_target / "debug")
build_id = identity(sha)
build_id.update(cache_key=key, build_inputs=inputs, cache_hit=False)
build_id.update(cache_key=key, dependency_cache_key=dependencies, build_inputs=inputs, cache_hit=False)
write_json(BUILD_IDENTITY, build_id)
with tempfile.TemporaryDirectory(dir=root, prefix="publish-") as tmp:
stage = Path(tmp) / "entry"
Expand All @@ -86,7 +108,7 @@ def main():
command(["git", "diff", "--exit-code", "HEAD"], timeout=30)
inputs = source_inputs(sha)
key = cache_key(inputs)
root = Path(os.environ["HOME"]) / ".cache/skippy/inference-build-v1"
root = Path(os.environ["HOME"]) / ".cache/skippy/inference-build-v2"
root.mkdir(parents=True, exist_ok=True)
cached = root / key
with lock(root / f"{key}.lock"):
Expand Down
59 changes: 59 additions & 0 deletions scripts/tests/inference_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Offline regression tests for native recipe transitions in JY build caches."""

import os
import unittest
from contextlib import chdir
from pathlib import Path
from runpy import run_path
from tempfile import TemporaryDirectory
from unittest.mock import patch


BUILD = run_path(str(Path(__file__).resolve().parents[1] / "inference-build.py"))


class NativeCacheTests(unittest.TestCase):
def test_native_transition_isolates_dependencies_but_rust_edits_reuse_them(self):
old = {"schema": 2, "source": "a" * 40, "native_inputs_sha256": "1" * 64}
new = dict(old, source="b" * 40, native_inputs_sha256="2" * 64)
rust_edit = dict(new, source="c" * 40)
self.assertNotEqual(BUILD["dependency_key"](old), BUILD["dependency_key"](new))
self.assertEqual(BUILD["dependency_key"](new), BUILD["dependency_key"](rust_edit))
self.assertEqual(BUILD["dependency_key"](old), BUILD["dependency_key"](dict(old)))
self.assertNotEqual(BUILD["cache_key"](new), BUILD["cache_key"](rust_edit))

def test_patch_removal_and_native_build_changes_invalidate_identity(self):
native_inputs = BUILD["native_inputs"]
with patch.dict(native_inputs.__globals__, capture=lambda args: "100644 blob old patch\n"):
old = native_inputs()
with patch.dict(native_inputs.__globals__, capture=lambda args: "100644 blob new patch\n"):
self.assertNotEqual(old, native_inputs())
with patch.dict(native_inputs.__globals__, capture=lambda args: ""):
self.assertNotEqual(old, native_inputs())

def test_build_hashes_native_identity_in_compiler_cache_and_retains_extra_files(self):
build = BUILD["build"]
calls = []
inputs = {"schema": 2, "source": "a" * 40, "native_inputs_sha256": "1" * 64}
def command(args, overrides, timeout):
calls.append((args, overrides))
# The command helper merges overrides into the complete inherited environment.
self.assertEqual(os.environ["PROVIDER_CONFIGURATION"], "retained")
with TemporaryDirectory() as tmp, chdir(tmp), patch.dict(os.environ, {
"SCCACHE_EXTRAFILES": "/existing/input", "PROVIDER_CONFIGURATION": "retained",
}), patch.dict(build.__globals__, command=command, install_binaries=lambda _: None,
identity=lambda _: {}), \
patch("shutil.which", return_value="/bin/sccache"), \
patch("shutil.copy"), patch("pathlib.Path.rename"):
root = Path(tmp) / "inference-build-v2"
root.mkdir()
build(inputs["source"], root, inputs, "key", root / "key")
compiler = next(env for args, env in calls if args == ["just", "inference-build-uncached"])
self.assertIn("cargo-v2", compiler["CARGO_TARGET_DIR"])
self.assertEqual(compiler["SCCACHE_EXTRAFILES"].split(os.pathsep)[0], "/existing/input")
self.assertTrue(compiler["SCCACHE_EXTRAFILES"].endswith("target/inference-native-inputs.json"))
self.assertNotIn("SCCACHE_RECACHE", compiler)


if __name__ == "__main__":
unittest.main()
Loading