feat: checkpoint distributed gguf runtime stories
This commit is contained in:
187
packages/node/native/scripts/build_llama_worker.sh
Normal file
187
packages/node/native/scripts/build_llama_worker.sh
Normal file
@@ -0,0 +1,187 @@
|
||||
#!/usr/bin/env bash
|
||||
# Apply the numbered llama.cpp patch stack and build the worker scaffold.
|
||||
#
|
||||
# Default flow:
|
||||
# 1. Fetch the pinned llama.cpp source into a build directory if needed.
|
||||
# 2. Verify the checkout matches the pinned commit.
|
||||
# 3. Check/apply the numbered patch stack from packages/node/native/llama/.
|
||||
# 4. Compile and build the standalone worker scaffold.
|
||||
# 5. Copy upstream LICENSE/AUTHORS notices into the staging directory.
|
||||
#
|
||||
# This script is intentionally model-free and does not contact any inference
|
||||
# endpoint. It is a source/build reproducibility check.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
NATIVE_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
||||
LLAMA_ROOT="${NATIVE_ROOT}/llama"
|
||||
UPSTREAM_COMMIT="$(tr -d '\n\r' < "${LLAMA_ROOT}/UPSTREAM_COMMIT")"
|
||||
UPSTREAM_REPOSITORY="$(tr -d '\n\r' < "${LLAMA_ROOT}/UPSTREAM_REPOSITORY")"
|
||||
PATCH_DIR="${LLAMA_ROOT}/patches"
|
||||
DEFAULT_SOURCE_DIR="${NATIVE_ROOT}/build/llama.cpp-src"
|
||||
DEFAULT_BUILD_DIR="${NATIVE_ROOT}/build/llama-worker"
|
||||
SOURCE_DIR="${DEFAULT_SOURCE_DIR}"
|
||||
BUILD_DIR="${DEFAULT_BUILD_DIR}"
|
||||
WORKTREE_DIR=""
|
||||
FETCH=1
|
||||
CXX_BIN="${CXX:-}"
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: build_llama_worker.sh [--source-dir PATH] [--build-dir PATH] [--no-fetch]
|
||||
|
||||
Builds the project-owned worker scaffold from a pinned llama.cpp checkout.
|
||||
EOF
|
||||
}
|
||||
|
||||
fail() {
|
||||
echo "error: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
while (($#)); do
|
||||
case "$1" in
|
||||
--source-dir)
|
||||
SOURCE_DIR="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--build-dir)
|
||||
BUILD_DIR="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--no-fetch)
|
||||
FETCH=0
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
fail "unknown argument: $1"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ -n "${SOURCE_DIR}" ]] || fail "source dir is empty"
|
||||
[[ -n "${BUILD_DIR}" ]] || fail "build dir is empty"
|
||||
|
||||
checkout_commit() {
|
||||
if [[ -f "${SOURCE_DIR}/.meshnet-upstream-commit" ]]; then
|
||||
tr -d '\n\r' < "${SOURCE_DIR}/.meshnet-upstream-commit"
|
||||
return 0
|
||||
fi
|
||||
if git -C "${SOURCE_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
git -C "${SOURCE_DIR}" rev-parse HEAD
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
ensure_source() {
|
||||
if [[ -d "${SOURCE_DIR}" ]]; then
|
||||
return 0
|
||||
fi
|
||||
if [[ "${FETCH}" -ne 1 ]]; then
|
||||
fail "source dir ${SOURCE_DIR} does not exist and --no-fetch was set"
|
||||
fi
|
||||
|
||||
mkdir -p "${SOURCE_DIR}"
|
||||
git clone --quiet "${UPSTREAM_REPOSITORY}" "${SOURCE_DIR}" || fail "unable to clone ${UPSTREAM_REPOSITORY}"
|
||||
git -C "${SOURCE_DIR}" checkout --quiet "${UPSTREAM_COMMIT}" || fail "unable to checkout ${UPSTREAM_COMMIT}"
|
||||
printf '%s\n' "${UPSTREAM_COMMIT}" > "${SOURCE_DIR}/.meshnet-upstream-commit"
|
||||
printf '%s\n' "${UPSTREAM_REPOSITORY}" > "${SOURCE_DIR}/.meshnet-upstream-repository"
|
||||
}
|
||||
|
||||
verify_assumptions() {
|
||||
local observed_commit
|
||||
observed_commit="$(checkout_commit)" || fail "source tree does not expose a commit pin; write ${SOURCE_DIR}/.meshnet-upstream-commit or use a git checkout"
|
||||
if [[ "${observed_commit}" != "${UPSTREAM_COMMIT}" ]]; then
|
||||
fail "llama.cpp pin mismatch: expected ${UPSTREAM_COMMIT}, got ${observed_commit}"
|
||||
fi
|
||||
|
||||
for required in LICENSE AUTHORS CMakeLists.txt; do
|
||||
[[ -e "${SOURCE_DIR}/${required}" ]] || fail "missing upstream assumption file: ${required}"
|
||||
done
|
||||
}
|
||||
|
||||
apply_patches() {
|
||||
shopt -s nullglob
|
||||
local patches=("${PATCH_DIR}"/*.patch)
|
||||
shopt -u nullglob
|
||||
if ((${#patches[@]} == 0)); then
|
||||
fail "no patch files found in ${PATCH_DIR}"
|
||||
fi
|
||||
|
||||
for patch in "${patches[@]}"; do
|
||||
git -C "${SOURCE_DIR}" apply --check "${patch}" || fail "patch check failed: $(basename "${patch}")"
|
||||
done
|
||||
for patch in "${patches[@]}"; do
|
||||
git -C "${SOURCE_DIR}" apply "${patch}" || fail "patch apply failed: $(basename "${patch}")"
|
||||
done
|
||||
}
|
||||
|
||||
build_worker() {
|
||||
rm -rf "${BUILD_DIR}"
|
||||
mkdir -p "${BUILD_DIR}"
|
||||
WORKTREE_DIR="${BUILD_DIR}/llama.cpp-worktree"
|
||||
rm -rf "${WORKTREE_DIR}"
|
||||
mkdir -p "${WORKTREE_DIR}"
|
||||
cp -a "${SOURCE_DIR}/." "${WORKTREE_DIR}/"
|
||||
if [[ -f "${SOURCE_DIR}/.meshnet-upstream-commit" ]]; then
|
||||
cp "${SOURCE_DIR}/.meshnet-upstream-commit" "${WORKTREE_DIR}/.meshnet-upstream-commit"
|
||||
fi
|
||||
if [[ -f "${SOURCE_DIR}/.meshnet-upstream-repository" ]]; then
|
||||
cp "${SOURCE_DIR}/.meshnet-upstream-repository" "${WORKTREE_DIR}/.meshnet-upstream-repository"
|
||||
fi
|
||||
|
||||
SOURCE_DIR="${WORKTREE_DIR}"
|
||||
apply_patches
|
||||
|
||||
local worker_dir="${SOURCE_DIR}/examples/meshnet-worker"
|
||||
cp "${LLAMA_ROOT}/templates/meshnet_worker.cpp" "${worker_dir}/meshnet_worker.cpp"
|
||||
cat > "${worker_dir}/version.h" <<EOF
|
||||
#pragma once
|
||||
|
||||
#define MESHNET_LLAMA_UPSTREAM_COMMIT "${UPSTREAM_COMMIT}"
|
||||
#define MESHNET_LLAMA_PATCHSET_VERSION "0001"
|
||||
EOF
|
||||
|
||||
local compiler=""
|
||||
if [[ -n "${CXX_BIN}" ]] && command -v "${CXX_BIN}" >/dev/null 2>&1; then
|
||||
compiler="${CXX_BIN}"
|
||||
elif command -v g++ >/dev/null 2>&1; then
|
||||
compiler="g++"
|
||||
elif command -v c++ >/dev/null 2>&1; then
|
||||
compiler="c++"
|
||||
elif command -v clang++ >/dev/null 2>&1; then
|
||||
compiler="clang++"
|
||||
else
|
||||
fail "no C++ compiler found (need g++, c++, clang++, or $CXX)"
|
||||
fi
|
||||
|
||||
"${compiler}" -std=c++17 -O2 -Wall -Wextra \
|
||||
-I "${worker_dir}" \
|
||||
-o "${BUILD_DIR}/meshnet_worker" \
|
||||
"${worker_dir}/meshnet_worker.cpp"
|
||||
}
|
||||
|
||||
stage_notices() {
|
||||
local notice_dir="${BUILD_DIR}/upstream-notices"
|
||||
mkdir -p "${notice_dir}"
|
||||
cp "${SOURCE_DIR}/LICENSE" "${notice_dir}/LICENSE"
|
||||
cp "${SOURCE_DIR}/AUTHORS" "${notice_dir}/AUTHORS"
|
||||
printf '%s\n' "${UPSTREAM_COMMIT}" > "${notice_dir}/UPSTREAM_COMMIT"
|
||||
printf '%s\n' "${UPSTREAM_REPOSITORY}" > "${notice_dir}/UPSTREAM_REPOSITORY"
|
||||
}
|
||||
|
||||
main() {
|
||||
ensure_source
|
||||
verify_assumptions
|
||||
build_worker
|
||||
stage_notices
|
||||
"${BUILD_DIR}/meshnet_worker" --smoke
|
||||
echo "build ok: ${BUILD_DIR}/meshnet_worker"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
43
packages/node/native/scripts/generate_cpp.sh
Normal file
43
packages/node/native/scripts/generate_cpp.sh
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
# Reproducibly generate the C++ Shard-protocol stubs from the schema.
|
||||
#
|
||||
# Produces message stubs (protoc --cpp_out) always, and gRPC C++ service stubs
|
||||
# (protoc --grpc_out with grpc_cpp_plugin) when the plugin is available. The
|
||||
# round-trip test needs only the message stubs; gRPC service stubs are for the
|
||||
# standalone C++ worker (DGR-008).
|
||||
#
|
||||
# Requirements: protoc (>=3.16). Optional: grpc_cpp_plugin for --grpc_out.
|
||||
#
|
||||
# Usage:
|
||||
# packages/node/native/scripts/generate_cpp.sh
|
||||
# Output: packages/node/native/build/cpp-gen/ (gitignored via build/).
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
NATIVE_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
||||
PROTO_DIR="${NATIVE_ROOT}/proto"
|
||||
PROTO_FILE="${PROTO_DIR}/shard_runtime.proto"
|
||||
OUT_DIR="${NATIVE_ROOT}/build/cpp-gen"
|
||||
|
||||
if ! command -v protoc >/dev/null 2>&1; then
|
||||
echo "error: protoc not found on PATH (install protobuf-compiler)." >&2
|
||||
exit 3
|
||||
fi
|
||||
|
||||
mkdir -p "${OUT_DIR}"
|
||||
|
||||
echo "generating C++ message stubs -> ${OUT_DIR}"
|
||||
protoc --proto_path="${PROTO_DIR}" --cpp_out="${OUT_DIR}" "${PROTO_FILE}"
|
||||
|
||||
if command -v grpc_cpp_plugin >/dev/null 2>&1; then
|
||||
echo "generating C++ gRPC service stubs -> ${OUT_DIR}"
|
||||
protoc --proto_path="${PROTO_DIR}" \
|
||||
--grpc_out="${OUT_DIR}" \
|
||||
--plugin=protoc-gen-grpc="$(command -v grpc_cpp_plugin)" \
|
||||
"${PROTO_FILE}"
|
||||
else
|
||||
echo "note: grpc_cpp_plugin not found; skipped --grpc_out (message stubs only)." >&2
|
||||
fi
|
||||
|
||||
echo "done:"
|
||||
ls -1 "${OUT_DIR}"
|
||||
76
packages/node/native/scripts/generate_python.py
Normal file
76
packages/node/native/scripts/generate_python.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Reproducibly generate the Python Shard-protocol stubs from the schema.
|
||||
|
||||
This is the documented, no-manual-copy generation entry point referenced by
|
||||
``evidence/DGR-002/README.md``. It runs the pinned ``grpc_tools.protoc`` with the
|
||||
same flags ``meshnet_node.native_protocol.generate()`` uses on demand, but is
|
||||
kept self-contained (it does not import ``meshnet_node``) so it works regardless
|
||||
of which checkout the editable install points at.
|
||||
|
||||
Usage (from the project .venv):
|
||||
|
||||
python packages/node/native/scripts/generate_python.py
|
||||
|
||||
Output: ``packages/node/native/build/python/shard_runtime_pb2{,_grpc}.py``
|
||||
(``build/`` is gitignored).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
_NATIVE_ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
PROTO_DIR = _NATIVE_ROOT / "proto"
|
||||
PROTO_FILE = PROTO_DIR / "shard_runtime.proto"
|
||||
GEN_DIR = _NATIVE_ROOT / "build" / "python"
|
||||
|
||||
|
||||
def _well_known_include() -> str | None:
|
||||
try:
|
||||
import grpc_tools
|
||||
|
||||
candidate = pathlib.Path(grpc_tools.__file__).parent / "_proto"
|
||||
return str(candidate) if candidate.is_dir() else None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if not PROTO_FILE.exists():
|
||||
print(f"schema not found: {PROTO_FILE}", file=sys.stderr)
|
||||
return 2
|
||||
try:
|
||||
from grpc_tools import protoc
|
||||
except ImportError:
|
||||
print(
|
||||
"grpc_tools is required (pip install grpcio-tools); it is present in "
|
||||
"the project .venv.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 3
|
||||
|
||||
GEN_DIR.mkdir(parents=True, exist_ok=True)
|
||||
well_known = _well_known_include()
|
||||
args = [
|
||||
"grpc_tools.protoc",
|
||||
f"-I{PROTO_DIR}",
|
||||
*([f"-I{well_known}"] if well_known else []),
|
||||
f"--python_out={GEN_DIR}",
|
||||
f"--grpc_python_out={GEN_DIR}",
|
||||
PROTO_FILE.name,
|
||||
]
|
||||
rc = protoc.main(args)
|
||||
if rc != 0:
|
||||
print(f"grpc_tools.protoc exited with status {rc}", file=sys.stderr)
|
||||
return rc
|
||||
|
||||
print(f"generated Python stubs into: {GEN_DIR}")
|
||||
for name in ("shard_runtime_pb2.py", "shard_runtime_pb2_grpc.py"):
|
||||
target = GEN_DIR / name
|
||||
print(f" {name}: {'ok' if target.exists() else 'MISSING'}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user