Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:zap: Speed up ARMv7 Docker ONNX builds

Daniel1459d77a708d
diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml
index 7d39481c57..5b71702005 100644
--- a/.github/workflows/dockerimage.yml
+++ b/.github/workflows/dockerimage.yml
@@ -25,6 +25,8 @@ concurrency:
env:
IMAGE_REPOSITORY: b3log/siyuan
+ # ARMv7 的 QEMU 冷编译耗时较长,在四核运行器上使用全部核心。
+ ARMV7_ONNX_BUILD_PARALLEL: 4
permissions:
contents: read
@@ -187,7 +189,10 @@ jobs:
PLATFORM_ID: ${{ matrix.id }}
CACHE_IMAGE: ${{ env.IMAGE_REPOSITORY }}:buildcache-release-v2-${{ matrix.id }}
run: |
+ ONNX_PARALLEL=2
+ if [ "$PLATFORM_ID" = "armv7" ]; then ONNX_PARALLEL="$ARMV7_ONNX_BUILD_PARALLEL"; fi
docker buildx build --push --platform "$PLATFORM" --provenance=mode=min \
+ --build-arg "ONNX_BUILD_PARALLEL=$ONNX_PARALLEL" \
--cache-from "type=registry,ref=$CACHE_IMAGE" \
--cache-to "type=registry,ref=$CACHE_IMAGE,mode=max" \
--metadata-file "$RUNNER_TEMP/docker-metadata.json" --progress plain \
@@ -294,8 +299,12 @@ jobs:
- name: Verify platform ONNX cache reuse and invalidation
env:
PLATFORM: ${{ matrix.platform }}
+ PLATFORM_ID: ${{ matrix.id }}
run: |
+ ONNX_PARALLEL=2
+ if [ "$PLATFORM_ID" = "armv7" ]; then ONNX_PARALLEL="$ARMV7_ONNX_BUILD_PARALLEL"; fi
python3 scripts/check-docker-cache.py --platform "$PLATFORM" \
+ --onnx-parallel "$ONNX_PARALLEL" \
--dockerfile Dockerfile --cache-repository "$IMAGE_REPOSITORY" \
--records "$RUNNER_TEMP/docker-cache-acceptance"
diff --git a/Dockerfile b/Dockerfile
index 3ce1e5f54f..0d831d40a4 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -103,8 +103,10 @@ patch --fuzz=0 -p1 <<'EOPATCH'
#include <vector>
EOPATCH
EORUN
+ARG ONNX_BUILD_PARALLEL=2
+# 并行度只影响编译层,允许发布环境按可用资源调整。
RUN python3 /onnxruntime/tools/ci_build/build.py --build_dir /onnxruntime/build \
- --config Release --update --build --parallel 2 --build_shared_lib \
+ --config Release --update --build --parallel ${ONNX_BUILD_PARALLEL} --build_shared_lib \
--skip_tests --allow_running_as_root --compile_no_warning_as_error \
--cmake_extra_defines onnxruntime_BUILD_UNIT_TESTS=OFF \
&& grep -q '^onnxruntime_BUILD_UNIT_TESTS:BOOL=OFF$' /onnxruntime/build/Release/CMakeCache.txt \
diff --git a/scripts/check-docker-cache.py b/scripts/check-docker-cache.py
index 068df9f2e9..74a993444a 100644
--- a/scripts/check-docker-cache.py
+++ b/scripts/check-docker-cache.py
@@ -47,7 +47,7 @@ def onnx_steps(log, expected=PLATFORMS):
return result
-def build_case(context, cache, directory, name, *, import_cache, revision=None, platforms=PLATFORMS):
+def build_case(context, cache, directory, name, *, import_cache, revision=None, platforms=PLATFORMS, onnx_parallel=2):
builder = "siyuan-cache-check-" + uuid.uuid4().hex
started = time.monotonic()
log_path = directory / (name + ".log")
@@ -79,6 +79,7 @@ def build_case(context, cache, directory, name, *, import_cache, revision=None,
monitor.start()
command = ["docker", "buildx", "build", "--builder", builder, "--target", "onnx-build",
"--platform", ",".join(sorted(platforms)), "--progress", "plain", "--output", "type=cacheonly",
+ "--build-arg", "ONNX_BUILD_PARALLEL=" + str(onnx_parallel),
"--cache-to", "type=registry,ref=" + cache + ",mode=max"]
if import_cache:
command += ["--cache-from", "type=registry,ref=" + cache]
@@ -106,13 +107,17 @@ def main():
parser.add_argument("--cache-repository", required=True)
parser.add_argument("--records", type=Path, required=True)
parser.add_argument("--platform", choices=sorted(PLATFORMS))
+ parser.add_argument("--onnx-parallel", type=int, default=2)
args = parser.parse_args()
+ if args.onnx_parallel < 1:
+ parser.error("ONNX parallelism must be a positive integer")
if not re.fullmatch(r"[a-z0-9][a-z0-9./_-]*", args.cache_repository):
parser.error("Expected a registry repository without a tag or digest")
args.records.mkdir(parents=True, exist_ok=False)
platforms = {args.platform} if args.platform else PLATFORMS
cache = args.cache_repository + ":buildcache-check-" + uuid.uuid4().hex
- report = {"cache": cache, "platforms": sorted(platforms), "status": "running", "cases": [],
+ report = {"cache": cache, "platforms": sorted(platforms), "onnx_parallel": args.onnx_parallel,
+ "status": "running", "cases": [],
"dockerfile_sha256": hashlib.sha256(args.dockerfile.read_bytes()).hexdigest()}
try:
with tempfile.TemporaryDirectory(prefix="siyuan-onnx-cache-") as directory:
@@ -126,7 +131,7 @@ def main():
(context / "app/cache-probe.txt").write_text("changed application source\n", encoding="utf-8")
revision = uuid.uuid4().hex if name == "onnx-input-change" else None
result = build_case(context, cache, args.records, name, import_cache=imported, revision=revision,
- platforms=platforms)
+ platforms=platforms, onnx_parallel=args.onnx_parallel)
report["cases"].append(result)
expected = name in ("warm", "application-change")
if any(cached != expected for cached in result["cached"].values()):
diff --git a/scripts/test_docker_release.py b/scripts/test_docker_release.py
index 92b734c219..83fe3bfce0 100644
--- a/scripts/test_docker_release.py
+++ b/scripts/test_docker_release.py
@@ -501,22 +501,60 @@ class CacheTests(unittest.TestCase):
with self.assertRaises(ValueError):
cache.onnx_steps(line, {"linux/arm64"})
- def test_selected_platform_is_used_for_all_four_cache_acceptance_cases(self):
+ def test_selected_platform_and_parallelism_are_used_for_all_four_cache_acceptance_cases(self):
def build(context, cache_ref, directory, name, *, platforms, **kwargs):
- self.assertEqual(platforms, {"linux/arm64"})
- return {"cached": {"linux/arm64": name in ("warm", "application-change")}}
+ self.assertEqual(platforms, {"linux/arm/v7"})
+ self.assertEqual(kwargs["onnx_parallel"], 4)
+ return {"cached": {"linux/arm/v7": name in ("warm", "application-change")}}
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
dockerfile = root / "Dockerfile"
dockerfile.write_text("FROM alpine AS onnx-build\n", encoding="utf-8")
with patch("sys.argv", ["check-docker-cache.py", "--dockerfile", str(dockerfile),
"--cache-repository", "b3log/siyuan", "--records", str(root / "records"),
- "--platform", "linux/arm64"]), patch.object(cache, "build_case", side_effect=build) as run:
+ "--platform", "linux/arm/v7", "--onnx-parallel", "4"]), \
+ patch.object(cache, "build_case", side_effect=build) as run:
cache.main()
self.assertEqual(run.call_count, 4)
value = json.loads(root.joinpath("records/report.json").read_text(encoding="utf-8"))
self.assertEqual(value["status"], "passed")
- self.assertEqual(value["platforms"], ["linux/arm64"])
+ self.assertEqual(value["platforms"], ["linux/arm/v7"])
+ self.assertEqual(value["onnx_parallel"], 4)
+
+ def test_build_uses_selected_parallelism_and_retains_cache_inputs(self):
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory)
+ commands = []
+
+ def command(arguments, **kwargs):
+ commands.append(arguments)
+ if arguments[:3] == ["docker", "buildx", "build"]:
+ kwargs["stdout"].write(
+ "#1 [linux/arm/v7 onnx-build 5/5] RUN python3 /onnxruntime/tools/ci_build/build.py\n#1 CACHED\n")
+ return subprocess.CompletedProcess(arguments, 0, '{"layers": []}', "")
+
+ with patch.object(cache.subprocess, "run", side_effect=command), patch.object(cache.threading, "Thread"):
+ value = cache.build_case(root, "b3log/siyuan:cache", root, "warm", import_cache=True,
+ platforms={"linux/arm/v7"}, onnx_parallel=4, revision="changed")
+ build = next(arguments for arguments in commands if arguments[:3] == ["docker", "buildx", "build"])
+ self.assertIn("ONNX_BUILD_PARALLEL=4", build)
+ self.assertIn("ONNX_TOOLCHAIN_REV=changed", build)
+ self.assertIn("linux/arm/v7", build)
+ self.assertIn("--cache-from", build)
+ self.assertIn("--cache-to", build)
+ self.assertEqual(value["cached"], {"linux/arm/v7": True})
+
+ def test_invalid_parallelism_stops_before_creating_records_or_running_docker(self):
+ with tempfile.TemporaryDirectory() as directory:
+ records = Path(directory) / "records"
+ for count in ("0", "-1", "invalid"):
+ with self.subTest(count=count), patch("sys.argv", ["check-docker-cache.py", "--dockerfile", "Dockerfile",
+ "--cache-repository", "b3log/siyuan", "--records", str(records), "--onnx-parallel", count]), \
+ patch.object(cache, "build_case") as build, contextlib.redirect_stderr(io.StringIO()):
+ with self.assertRaises(SystemExit):
+ cache.main()
+ build.assert_not_called()
+ self.assertFalse(records.exists())
class SourceDownloadTests(unittest.TestCase):