Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(macos): use the arm64-native ONNX Runtime slice for sherpa-onnx on Apple silicon (#2116)
Zeljaa32541ce2ac8a
diff --git a/scripts/download-sherpa-onnx.js b/scripts/download-sherpa-onnx.jsindex ceab4b0a7..0f47b68d8 100644--- a/scripts/download-sherpa-onnx.js+++ b/scripts/download-sherpa-onnx.js@@ -2,6 +2,7 @@const fs = require("fs");const path = require("path");const { execFileSync } = require("child_process");+const crypto = require("crypto");const {cleanupFiles,downloadFile,@@ -30,6 +31,18 @@ const GITHUB_RELEASE_URL = `https://github.com/k2-fsa/sherpa-onnx/releases/downlconst WINDOWS_ONNXRUNTIME_UPSTREAM_NAME = "onnxruntime.dll";const WINDOWS_ONNXRUNTIME_PRIVATE_NAME = "ow-onnxrt.dll";+// sherpa-onnx's macOS archives bundle a universal2 libonnxruntime whose arm64+// slice runs INT8 models ~3x slower than the arm64-only build of the same+// version from the same maintainer (the zip sherpa-onnx's own+// cmake/onnxruntime-osx-arm64.cmake pins). On macOS hosts we swap that slice+// in and keep the x86_64 slice, so the file stays universal2.+const MACOS_ARM64_ONNXRUNTIME = {+ url: "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.27.0/onnxruntime-osx-arm64-1.27.0.zip",+ sha256: "5f05b2653eb0af852dab46c85778f0e012c514ed2e71a6dbb9c9550434b0a514",+ libraryName: "libonnxruntime.1.27.0.dylib",+ marker: "arm64-1.27.0", // recorded in the install marker so older installs re-extract+};+// Binary configurations for each platform// Note: macOS uses universal2 builds that work on both arm64 and x64const BINARIES = {@@ -81,12 +94,47 @@ const BIN_DIR = path.join(__dirname, "..", "resources", "bin");const VERSIONED_LIB_PATTERN = /^(lib.+?)\.(\d+\.\d+\.\d+)\.(dylib|so|dll)$/;const REQUIRED_MACOS_ARCHITECTURES = ["x86_64", "arm64"];+// Both macOS targets install the same libonnxruntime file; lipo needs a macOS host.+function isMacosHostTarget(platformArch) {+ return process.platform === "darwin" && platformArch.startsWith("darwin");+}+// Upstream 1.13.4 ships an invalid arm64 signature on libonnxruntime; dyld SIGKILLs unsigned loads.function adhocSign(filePath, platformArch) {if (process.platform !== "darwin" || !platformArch.startsWith("darwin")) return;execFileSync("codesign", ["--force", "--sign", "-", filePath], { stdio: "ignore" });}+async function replaceMacosArm64OnnxRuntime(libraryPath, platformArch) {+ const { url, sha256, libraryName } = MACOS_ARM64_ONNXRUNTIME;+ if (path.basename(libraryPath) !== libraryName) {+ throw new Error(+ `sherpa-onnx ships ${path.basename(libraryPath)}; update MACOS_ARM64_ONNXRUNTIME to match`+ );+ }+ const zipPath = `${libraryPath}.arm64.zip`;+ const extractDir = `${libraryPath}.arm64`;+ const x86Path = `${libraryPath}.x86_64`;+ try {+ console.log(` ${platformArch}: Downloading arm64 ONNX Runtime from ${url}`);+ await downloadFile(url, zipPath);+ const actual = crypto.createHash("sha256").update(fs.readFileSync(zipPath)).digest("hex");+ if (actual !== sha256) throw new Error(`arm64 ONNX Runtime sha256 mismatch: ${actual}`);+ execFileSync("unzip", ["-q", "-o", zipPath, "-d", extractDir], { stdio: "ignore" });+ const arm64Path = findLibrariesInDir(extractDir, "*.dylib").find(+ (file) => path.basename(file) === libraryName+ );+ if (!arm64Path) throw new Error(`${libraryName} missing from arm64 ONNX Runtime zip`);+ execFileSync("lipo", ["-thin", "x86_64", libraryPath, "-output", x86Path]);+ execFileSync("lipo", ["-create", x86Path, arm64Path, "-output", libraryPath]);+ console.log(` ${platformArch}: Replaced arm64 slice of ${libraryName}`);+ } finally {+ for (const file of [zipPath, extractDir, x86Path]) {+ fs.rmSync(file, { recursive: true, force: true });+ }+ }+}+function getDownloadUrl(archiveName) {return `${GITHUB_RELEASE_URL}/${archiveName}`;}@@ -250,8 +298,12 @@ function isCompleteInstall(markerPath, binaryPaths, { platformArch, binDir = BINif (marker.libraries.some((lib) => !fs.existsSync(path.join(binDir, lib)))) return false;// A win32 marker without this field predates the rename: the exes on disk// still import onnxruntime.dll and must be re-extracted.+ if (platformArch.startsWith("win32")) {+ return marker.onnxRuntime === WINDOWS_ONNXRUNTIME_PRIVATE_NAME;+ }+ // A macOS marker without this field still holds the slow universal2 slice.return (- !platformArch.startsWith("win32") || marker.onnxRuntime === WINDOWS_ONNXRUNTIME_PRIVATE_NAME+ !isMacosHostTarget(platformArch) || marker.onnxRuntime === MACOS_ARM64_ONNXRUNTIME.marker);} catch {return false;@@ -325,6 +377,9 @@ async function downloadBinary(platformArch, config, isForce = false) {fs.rmSync(destPath, { force: true });fs.copyFileSync(libPath, destPath);setExecutable(destPath);+ if (isMacosHostTarget(platformArch) && versionMatch?.[1] === "libonnxruntime") {+ await replaceMacosArm64OnnxRuntime(destPath, platformArch);+ }adhocSign(destPath, platformArch);copiedLibraries.push(libName);console.log(` ${platformArch}: Copied library ${libName}`);@@ -364,6 +419,7 @@ async function downloadBinary(platformArch, config, isForce = false) {version: SHERPA_ONNX_VERSION,libraries: shippedLibraries,...(isWindowsTarget ? { onnxRuntime: WINDOWS_ONNXRUNTIME_PRIVATE_NAME } : {}),+ ...(isMacosHostTarget(platformArch) ? { onnxRuntime: MACOS_ARM64_ONNXRUNTIME.marker } : {}),}));return true;@@ -443,6 +499,7 @@ async function main() {// Export config for potential importsmodule.exports = {SHERPA_ONNX_VERSION,+ MACOS_ARM64_ONNXRUNTIME,BINARIES,BIN_DIR,WINDOWS_ONNXRUNTIME_PRIVATE_NAME,diff --git a/test/scripts/downloadSherpaOnnx.test.js b/test/scripts/downloadSherpaOnnx.test.jsindex b8d647984..fb655b844 100644--- a/test/scripts/downloadSherpaOnnx.test.js+++ b/test/scripts/downloadSherpaOnnx.test.js@@ -10,6 +10,7 @@ const { listImportedModules } = require("../../scripts/lib/pe-imports");const { buildPeImage } = require("../helpers/harness/peFixture");const {BINARIES,+ MACOS_ARM64_ONNXRUNTIME,SHERPA_ONNX_VERSION,WINDOWS_ONNXRUNTIME_PRIVATE_NAME,WINDOWS_ONNXRUNTIME_UPSTREAM_NAME,@@ -181,18 +182,43 @@ test("a win32 marker written before the rename is not a complete install", (t) =assert.equal(isCompleteInstall(marker, [exe], options), true);});-test("non-Windows markers do not need the onnxRuntime field", (t) => {+test("Linux markers do not need the onnxRuntime field", (t) => {const dir = makeBinDir(t);- const binary = path.join(dir, "sherpa-onnx-ws-darwin-arm64");+ const binary = path.join(dir, "sherpa-onnx-ws-linux-x64");fs.writeFileSync(binary, "");- const marker = path.join(dir, ".sherpa-onnx-darwin-arm64.json");+ const marker = path.join(dir, ".sherpa-onnx-linux-x64.json");fs.writeFileSync(marker, JSON.stringify({ version: SHERPA_ONNX_VERSION, libraries: [] }));assert.equal(- isCompleteInstall(marker, [binary], { platformArch: "darwin-arm64", binDir: dir }),+ isCompleteInstall(marker, [binary], { platformArch: "linux-x64", binDir: dir }),true);});+test(+ "a macOS marker written before the arm64 ONNX Runtime slice is not a complete install",+ { skip: process.platform !== "darwin" && "the slice is only replaced on macOS hosts" },+ (t) => {+ const dir = makeBinDir(t);+ const binary = path.join(dir, "sherpa-onnx-ws-darwin-arm64");+ fs.writeFileSync(binary, "");+ const marker = path.join(dir, ".sherpa-onnx-darwin-arm64.json");+ const options = { platformArch: "darwin-arm64", binDir: dir };++ fs.writeFileSync(marker, JSON.stringify({ version: SHERPA_ONNX_VERSION, libraries: [] }));+ assert.equal(isCompleteInstall(marker, [binary], options), false);++ fs.writeFileSync(+ marker,+ JSON.stringify({+ version: SHERPA_ONNX_VERSION,+ libraries: [],+ onnxRuntime: MACOS_ARM64_ONNXRUNTIME.marker,+ })+ );+ assert.equal(isCompleteInstall(marker, [binary], options), true);+ }+);+test("a failed automatic Windows repair stays incomplete and retries DLL patching", async (t) => {const root = makeBinDir(t);const binDir = path.join(root, "resources", "bin");