diff --git a/.gitignore b/.gitignore index 04afbed00..6cf78c31b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ packages/onnx/native/ # Internal working files: kept on the machine, not published. /CLEANUP.md /docs/dev/*-handoff.md +packages/onnxruntime/ diff --git a/bin/publish-onnxruntime b/bin/publish-onnxruntime new file mode 100755 index 000000000..2d660b2fa --- /dev/null +++ b/bin/publish-onnxruntime @@ -0,0 +1,200 @@ +#!/usr/bin/env node +// Builds the five @wirecat/onnxruntime- packages that `models download` takes the native ONNX Runtime from, +// and with --publish publishes the ones npm does not have yet. Microsoft's GitHub releases went away on 17.11.2026, +// so cli-core depends on no Microsoft hosting at run time: the libraries are taken once, here, from Microsoft's NuGet +// package (Linux, macOS) and onnxruntime-node's npm tarball (Windows: its binding needs the DirectML build of +// onnxruntime.dll, export ordinal 4, which the NuGet CPU build lacks). Every input and every packed tarball is +// checked against the sha256 below, which src/embeddings/onnx-runtime.ts pins too: a packed tarball that differs +// (another npm version packs differently) is not published. +// +// bin/publish-onnxruntime build and pack into packages/onnxruntime/ +// bin/publish-onnxruntime --publish the same, then publish; the npm token comes from the keyring +// (secret-tool lookup service npm account leemour) or NPM_TOKEN +import { execFileSync } from "node:child_process" +import { createHash } from "node:crypto" +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs" +import { dirname, join } from "node:path" +import { fileURLToPath } from "node:url" +import { unzipSync } from "fflate" + +const VERSION = "1.30.0" +const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..") +const OUT = join(ROOT, "packages", "onnxruntime") +const SOURCES = join(OUT, "sources") + +const NUGET = { + url: + `https://api.nuget.org/v3-flatcontainer/microsoft.ml.onnxruntime/${VERSION}` + + `/microsoft.ml.onnxruntime.${VERSION}.nupkg`, + file: join(SOURCES, `microsoft.ml.onnxruntime.${VERSION}.nupkg`), + sha256: "30df119bd9164eaa187666d93147448fdd4a9a1a3caf83f1505daf7af2d1c5d2", +} +const NPM = { + url: `https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-${VERSION}.tgz`, + file: join(SOURCES, `onnxruntime-node-${VERSION}.tgz`), + sha256: "6e3390d6b783e7be946fad629292799da28d0b42f84856e50d2c1b0383291e75", +} + +const TARGETS = [ + { + target: "linux-x64", + os: "linux", + cpu: "x64", + from: NUGET, + member: "runtimes/linux-x64/native/libonnxruntime.so", + library: "libonnxruntime.so.1", + sha256: "245a6f8c38127551057a1cd1ffd59f0a186a227ade4f3492dea2494eb565542e", + tarball: "9c12e88ae495ae04e28802e52c2524a7a9c31aa715a67eeb636c85efcb08e5d9", + }, + { + target: "linux-arm64", + os: "linux", + cpu: "arm64", + from: NUGET, + member: "runtimes/linux-arm64/native/libonnxruntime.so", + library: "libonnxruntime.so.1", + sha256: "64e903a43a041240fd6bcffe0ac6d4fea47ef87bf24b9d097801bd00a9612a4b", + tarball: "4ec23a63f0e7f49edb63590f4bc8b0e8526bff5b3a2b19d768112f37dc50d09c", + }, + { + target: "darwin-arm64", + os: "darwin", + cpu: "arm64", + from: NUGET, + member: "runtimes/osx-arm64/native/libonnxruntime.dylib", + library: "libonnxruntime.1.dylib", + sha256: "bffaa6ef856dba2c26f09c7c0e7018fd1562625f3485292c7ad28b83748c6879", + tarball: "0e543a584a048b301ff96b81af4b001288e048577ce0f0b4a5a9c2df97979615", + }, + { + target: "win32-x64", + os: "win32", + cpu: "x64", + from: NPM, + member: "package/bin/napi-v6/win32/x64/onnxruntime.dll", + library: "onnxruntime.dll", + sha256: "508c362f5673483dd3a086379c392795b2e42d10d5e6f3f90ebd7ac21c97af67", + tarball: "55924c8e4c3f975d131e943712c5fac6e5423fb2b0c356c96230f2ad18dc2792", + }, + { + target: "win32-arm64", + os: "win32", + cpu: "arm64", + from: NPM, + member: "package/bin/napi-v6/win32/arm64/onnxruntime.dll", + library: "onnxruntime.dll", + sha256: "6c3a2abf5c6aca11c48a0d338a91af309f2266f25ce2e4084e6a87bbcb0fbfd0", + tarball: "77e43743e580d6641e49b7cdeed57fd11f21a8cf27a186dd33a3db817b3ff7f2", + }, +] + +const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex") +const fail = (message) => { + console.error(message) + process.exit(1) +} + +const fetched = async ({ url, file, sha256: expected }) => { + if (!existsSync(file)) { + console.log(`downloading ${url}`) + const response = await fetch(url) + if (!response.ok) fail(`${url}: HTTP ${response.status}`) + writeFileSync(file, Buffer.from(await response.arrayBuffer())) + } + const bytes = readFileSync(file) + if (sha256(bytes) !== expected) fail(`${file} is not the file expected (sha256 ${sha256(bytes)}); delete it to retry`) + return bytes +} + +const tarMember = (archive, member) => + execFileSync("tar", ["-xzOf", archive, member], { maxBuffer: 256 * 1024 * 1024 }) + +const readme = ({ target, from, member, library, sha256: expected }) => `# @wirecat/onnxruntime-${target} + +ONNX Runtime ${VERSION}'s native library for ${target}, as \`${library}\`, for +[@wirecat/cli-core](https://www.npmjs.com/package/@wirecat/cli-core): \`models download\` fetches this package's +tarball and checks the library's sha256 before it uses it. Not meant to be installed on its own. + +- Source: ${from.url} +- Source sha256: \`${from.sha256}\` +- Member: \`${member}\` +- Library sha256: \`${expected}\` + +ONNX Runtime is © Microsoft Corporation, under the MIT licence in \`LICENSE\`; \`ThirdPartyNotices.txt\` lists the +code it includes. Both are copied from the NuGet package \`Microsoft.ML.OnnxRuntime\` ${VERSION}. +` + +mkdirSync(SOURCES, { recursive: true }) +const nuget = unzipSync(await fetched(NUGET), { + filter: ({ name }) => name === "LICENSE" || name === "ThirdPartyNotices.txt" || name.startsWith("runtimes/"), +}) +await fetched(NPM) + +const packed = [] +for (const spec of TARGETS) { + const name = `@wirecat/onnxruntime-${spec.target}` + const folder = join(OUT, spec.target) + rmSync(folder, { recursive: true, force: true }) + mkdirSync(folder, { recursive: true }) + const library = spec.from === NUGET ? nuget[spec.member] : tarMember(NPM.file, spec.member) + if (!library || sha256(library) !== spec.sha256) fail(`${spec.member} is not the library expected`) + writeFileSync(join(folder, spec.library), library, { mode: 0o755 }) + writeFileSync(join(folder, "LICENSE"), nuget.LICENSE) + writeFileSync(join(folder, "ThirdPartyNotices.txt"), nuget["ThirdPartyNotices.txt"]) + writeFileSync(join(folder, "README.md"), readme(spec)) + const manifest = { + name, + version: VERSION, + description: `ONNX Runtime ${VERSION}'s native library for ${spec.target}, for @wirecat/cli-core`, + license: "MIT", + repository: { type: "git", url: "git+https://github.com/WireCatLabs/cli-core.git", directory: "bin" }, + os: [spec.os], + cpu: [spec.cpu], + files: [spec.library, "LICENSE", "ThirdPartyNotices.txt", "README.md"], + } + writeFileSync(join(folder, "package.json"), `${JSON.stringify(manifest, null, 2)}\n`) + const [{ filename }] = JSON.parse( + execFileSync("npm", ["pack", "--json", "--pack-destination", OUT], { cwd: folder, encoding: "utf8" }), + ) + const tarball = join(OUT, filename) + const actual = sha256(readFileSync(tarball)) + console.log(`${name}@${VERSION}: ${tarball}, ${readFileSync(tarball).length} bytes, sha256 ${actual}`) + if (spec.tarball && actual !== spec.tarball) fail(`${filename} packed differently from the pinned ${spec.tarball}`) + packed.push({ name, tarball, pinned: Boolean(spec.tarball) }) +} + +if (!process.argv.includes("--publish")) process.exit(0) +if (packed.some(({ pinned }) => !pinned)) fail("pin every tarball's sha256 above before publishing") + +let token = process.env.NPM_TOKEN +if (!token) { + try { + token = execFileSync("secret-tool", ["lookup", "service", "npm", "account", "leemour"], { + encoding: "utf8", + stdio: ["ignore", "pipe", "ignore"], + }).trim() + } catch {} +} +if (!token) fail("no npm token: export NPM_TOKEN, or: secret-tool store --label=npm service npm account leemour") +const npmrc = join(OUT, ".npmrc") +// The config names the variable, not the value: the token stays in this process's environment. +writeFileSync(npmrc, "//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n", { mode: 0o600 }) +try { + for (const { name, tarball } of packed) { + let published = false + try { + execFileSync("npm", ["view", `${name}@${VERSION}`, "version"], { stdio: "ignore" }) + published = true + } catch {} + if (published) { + console.log(`${name}@${VERSION} is already on npm`) + continue + } + execFileSync("npm", ["publish", tarball, "--access", "public", "--userconfig", npmrc], { + stdio: "inherit", + env: { ...process.env, NPM_TOKEN: token }, + }) + } +} finally { + rmSync(npmrc, { force: true }) +} diff --git a/changes/fix-ort-native-source.md b/changes/fix-ort-native-source.md new file mode 100644 index 000000000..2c9b6f03a --- /dev/null +++ b/changes/fix-ort-native-source.md @@ -0,0 +1,10 @@ +### Fixed + +- **`models download` takes the native ONNX Runtime from WireCat's own npm packages.** Microsoft's GitHub repository, + and every release download in it, answers 404 since 17.11.2026, so the download failed on Linux and macOS and the + models ran through WebAssembly. The library now comes from `@wirecat/onnxruntime-` on npm (linux-x64, + linux-arm64, darwin-arm64, win32-x64, win32-arm64), 10–13 MB each, with the same sha256 check of the library. No + download depends on Microsoft's hosting any more. `bin/publish-onnxruntime` builds the packages once from + Microsoft's NuGet package `Microsoft.ML.OnnxRuntime` 1.30.0, and on Windows from `onnxruntime-node`'s npm tarball as + before. The Linux and Windows libraries are byte for byte the ones used so far. The macOS one is NuGet's build of + the same version, with a new sha256. Windows downloads 11 MB instead of the 113 MB `onnxruntime-node` tarball. diff --git a/packages/onnx/README.md b/packages/onnx/README.md index 29d77c787..83229bbc5 100644 --- a/packages/onnx/README.md +++ b/packages/onnx/README.md @@ -6,9 +6,10 @@ about 15 MB instead of the 145 MB package that also carries every browser build. - **Native**, 2–3× faster, on Linux x64 and arm64 (glibc), macOS arm64 and Windows x64 and arm64: `onnxruntime-node`'s JavaScript and the 0.3–0.4 MB binding of each of those systems, without its libraries. - `@wirecat/cli-core` downloads the library beside a copy of the binding, which loads it from its own folder: from Microsoft's release of - the same version, and on Windows from `onnxruntime-node`'s own npm tarball, because the Windows binding needs the - DirectML build of `onnxruntime.dll` (export ordinal 4) that Microsoft's CPU release does not carry. + `@wirecat/cli-core` downloads the library beside a copy of the binding, which loads it from its own folder: from + WireCat's `@wirecat/onnxruntime-` package of the same version, built by `bin/publish-onnxruntime` from + Microsoft's NuGet package, and on Windows from `onnxruntime-node`'s own npm tarball, because the Windows binding + needs the DirectML build of `onnxruntime.dll` (export ordinal 4) that Microsoft's CPU build does not carry. ```js import { env, InferenceSession, Tensor } from "@wirecat/cli-core-onnx" // WebAssembly diff --git a/packages/onnx/probe-native.mjs b/packages/onnx/probe-native.mjs index 3da49e185..c6090244d 100644 --- a/packages/onnx/probe-native.mjs +++ b/packages/onnx/probe-native.mjs @@ -1,6 +1,6 @@ // Runs the one-node model through the native binding of this system, beside the ONNX Runtime library -// @wirecat/cli-core downloads — from Microsoft's release, or on Windows from onnxruntime-node's npm tarball, whose -// DirectML build of onnxruntime.dll the binding needs: `node probe-native.mjs` and `bun probe-native.mjs`. +// @wirecat/cli-core downloads from WireCat's @wirecat/onnxruntime- package on npm (bin/publish-onnxruntime): +// `node probe-native.mjs` and `bun probe-native.mjs`. import { execFileSync } from "node:child_process" import { createHash } from "node:crypto" import { copyFileSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs" @@ -8,49 +8,24 @@ import { tmpdir } from "node:os" import { join } from "node:path" import { nativeBindingFor, ONNX_RUNTIME_VERSION, openNative } from "@wirecat/cli-core-onnx" -const release = `https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_RUNTIME_VERSION}` -const npm = "https://registry.npmjs.org/onnxruntime-node/-" const archives = { - "linux-x64": [ - "onnxruntime-linux-x64-1.30.0.tgz", - "a5ed5a3cac51fbb2e90da632ae43d19212faaa20e76484e62bcb7c23ddb3b3fd", - "onnxruntime-linux-x64-1.30.0/lib/libonnxruntime.so.1.30.0", - "libonnxruntime.so.1", - ], - "linux-arm64": [ - "onnxruntime-linux-aarch64-1.30.0.tgz", - "e16a27a8ed330bbc698df7330b0cf56e722f354e3bcc92118682c74ef3c3e3da", - "onnxruntime-linux-aarch64-1.30.0/lib/libonnxruntime.so.1.30.0", - "libonnxruntime.so.1", - ], - "darwin-arm64": [ - "onnxruntime-osx-arm64-1.30.0.tgz", - "6ebb5062a934537c352937821f9fe9718e7de1a2db1122a93dd363ffd53a7012", - "./onnxruntime-osx-arm64-1.30.0/lib/libonnxruntime.1.30.0.dylib", - "libonnxruntime.1.dylib", - ], - "win32-x64": [ - "onnxruntime-node-1.30.0.tgz", - "6e3390d6b783e7be946fad629292799da28d0b42f84856e50d2c1b0383291e75", - "package/bin/napi-v6/win32/x64/onnxruntime.dll", - "onnxruntime.dll", - ], - "win32-arm64": [ - "onnxruntime-node-1.30.0.tgz", - "6e3390d6b783e7be946fad629292799da28d0b42f84856e50d2c1b0383291e75", - "package/bin/napi-v6/win32/arm64/onnxruntime.dll", - "onnxruntime.dll", - ], + "linux-x64": ["9c12e88ae495ae04e28802e52c2524a7a9c31aa715a67eeb636c85efcb08e5d9", "libonnxruntime.so.1"], + "linux-arm64": ["4ec23a63f0e7f49edb63590f4bc8b0e8526bff5b3a2b19d768112f37dc50d09c", "libonnxruntime.so.1"], + "darwin-arm64": ["0e543a584a048b301ff96b81af4b001288e048577ce0f0b4a5a9c2df97979615", "libonnxruntime.1.dylib"], + "win32-x64": ["55924c8e4c3f975d131e943712c5fac6e5423fb2b0c356c96230f2ad18dc2792", "onnxruntime.dll"], + "win32-arm64": ["77e43743e580d6641e49b7cdeed57fd11f21a8cf27a186dd33a3db817b3ff7f2", "onnxruntime.dll"], } const target = `${process.platform}-${process.arch}` -const [name, sha256, member, library] = archives[target] ?? [] -if (!name) throw new Error(`no ONNX Runtime archive for ${target}`) +const [sha256, library] = archives[target] ?? [] +if (!sha256) throw new Error(`no ONNX Runtime package for ${target}`) +const name = `onnxruntime-${target}-${ONNX_RUNTIME_VERSION}.tgz` +const member = `package/${library}` const binding = nativeBindingFor() if (!binding) throw new Error(`the package carries no binding for ${target}`) const work = mkdtempSync(join(tmpdir(), "onnx-native-")) const archive = Buffer.from( - await (await fetch(`${process.platform === "win32" ? npm : release}/${name}`)).arrayBuffer(), + await (await fetch(`https://registry.npmjs.org/@wirecat/onnxruntime-${target}/-/${name}`)).arrayBuffer(), ) const actual = createHash("sha256").update(archive).digest("hex") if (actual !== sha256) throw new Error(`${name} is not the file expected (sha256 ${actual})`) diff --git a/src/embeddings/onnx-runtime.ts b/src/embeddings/onnx-runtime.ts index 5c4227190..5f0571212 100644 --- a/src/embeddings/onnx-runtime.ts +++ b/src/embeddings/onnx-runtime.ts @@ -25,83 +25,72 @@ export interface NativeRuntime { readonly library: { name: string; sha256: string; bytes: number } } -const release = (name: string, sha256: string, bytes: number): ModelFile => ({ - name, - url: `https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_RUNTIME_VERSION}/${name}`, - sha256, - bytes, -}) +const REGISTRY = "https://registry.npmjs.org" /** - * onnxruntime-node's own npm tarball, for Windows: its binding imports the DirectML build of onnxruntime.dll by - * ordinal, which Microsoft's CPU release lacks; DirectML.dll is loaded only when used, so the DLL alone runs on CPU. + * WireCat's own package of this system's library, on npm: Microsoft's GitHub releases went away on 17.11.2026, so no + * download depends on Microsoft's hosting. `bin/publish-onnxruntime` builds them from Microsoft's NuGet package, and + * on Windows from onnxruntime-node's npm tarball: its binding imports the DirectML build of onnxruntime.dll by + * ordinal, which Microsoft's CPU build lacks; DirectML.dll is loaded only when used, so the DLL alone runs on CPU. */ -const npmTarball: ModelFile = { - name: `onnxruntime-node-${ONNX_RUNTIME_VERSION}.tgz`, - url: `https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-${ONNX_RUNTIME_VERSION}.tgz`, - sha256: "6e3390d6b783e7be946fad629292799da28d0b42f84856e50d2c1b0383291e75", - bytes: 113_507_888, -} +const wirecat = ( + target: string, + sha256: string, + bytes: number, + library: NativeRuntime["library"], +): Omit => ({ + archive: { + name: `onnxruntime-${target}-${ONNX_RUNTIME_VERSION}.tgz`, + url: `${REGISTRY}/@wirecat/onnxruntime-${target}/-/onnxruntime-${target}-${ONNX_RUNTIME_VERSION}.tgz`, + sha256, + bytes, + }, + member: `package/${library.name}`, + library, +}) /** The systems the native-runtime workflow runs natively; elsewhere, and on musl, the models run through WebAssembly. */ const NATIVE: Record> = { - "linux-x64": { - archive: release( - "onnxruntime-linux-x64-1.30.0.tgz", - "a5ed5a3cac51fbb2e90da632ae43d19212faaa20e76484e62bcb7c23ddb3b3fd", - 11_306_877, - ), - member: "onnxruntime-linux-x64-1.30.0/lib/libonnxruntime.so.1.30.0", - library: { - name: "libonnxruntime.so.1", - sha256: "245a6f8c38127551057a1cd1ffd59f0a186a227ade4f3492dea2494eb565542e", - bytes: 28_985_152, - }, - }, - "linux-arm64": { - archive: release( - "onnxruntime-linux-aarch64-1.30.0.tgz", - "e16a27a8ed330bbc698df7330b0cf56e722f354e3bcc92118682c74ef3c3e3da", - 10_269_495, - ), - member: "onnxruntime-linux-aarch64-1.30.0/lib/libonnxruntime.so.1.30.0", - library: { + "linux-x64": wirecat("linux-x64", "9c12e88ae495ae04e28802e52c2524a7a9c31aa715a67eeb636c85efcb08e5d9", 11_305_384, { + name: "libonnxruntime.so.1", + sha256: "245a6f8c38127551057a1cd1ffd59f0a186a227ade4f3492dea2494eb565542e", + bytes: 28_985_152, + }), + "linux-arm64": wirecat( + "linux-arm64", + "4ec23a63f0e7f49edb63590f4bc8b0e8526bff5b3a2b19d768112f37dc50d09c", + 10_386_392, + { name: "libonnxruntime.so.1", sha256: "64e903a43a041240fd6bcffe0ac6d4fea47ef87bf24b9d097801bd00a9612a4b", bytes: 25_135_496, }, - }, - "darwin-arm64": { - archive: release( - "onnxruntime-osx-arm64-1.30.0.tgz", - "6ebb5062a934537c352937821f9fe9718e7de1a2db1122a93dd363ffd53a7012", - 42_373_116, - ), - member: "onnxruntime-osx-arm64-1.30.0/lib/libonnxruntime.1.30.0.dylib", - library: { + ), + "darwin-arm64": wirecat( + "darwin-arm64", + "0e543a584a048b301ff96b81af4b001288e048577ce0f0b4a5a9c2df97979615", + 12_921_423, + { name: "libonnxruntime.1.dylib", - sha256: "bcc9110f9d638a119de2db7afb3ba9a1da8085f0cb3401e1c48ae1caf450b6fa", + sha256: "bffaa6ef856dba2c26f09c7c0e7018fd1562625f3485292c7ad28b83748c6879", bytes: 43_879_424, }, - }, - "win32-x64": { - archive: npmTarball, - member: "package/bin/napi-v6/win32/x64/onnxruntime.dll", - library: { - name: "onnxruntime.dll", - sha256: "508c362f5673483dd3a086379c392795b2e42d10d5e6f3f90ebd7ac21c97af67", - bytes: 28_754_232, - }, - }, - "win32-arm64": { - archive: npmTarball, - member: "package/bin/napi-v6/win32/arm64/onnxruntime.dll", - library: { + ), + "win32-x64": wirecat("win32-x64", "55924c8e4c3f975d131e943712c5fac6e5423fb2b0c356c96230f2ad18dc2792", 10_652_326, { + name: "onnxruntime.dll", + sha256: "508c362f5673483dd3a086379c392795b2e42d10d5e6f3f90ebd7ac21c97af67", + bytes: 28_754_232, + }), + "win32-arm64": wirecat( + "win32-arm64", + "77e43743e580d6641e49b7cdeed57fd11f21a8cf27a186dd33a3db817b3ff7f2", + 10_778_591, + { name: "onnxruntime.dll", sha256: "6c3a2abf5c6aca11c48a0d338a91af309f2266f25ce2e4084e6a87bbcb0fbfd0", bytes: 29_815_136, }, - }, + ), } const onMusl = (): boolean => { @@ -143,7 +132,7 @@ const isInstalled = (runtime: NativeRuntime, folder: string): boolean => sizeOf(join(folder, runtime.library.name)) === runtime.library.bytes && sizeOf(join(folder, BINDING)) !== undefined /** - * The library this system's binding links to, out of Microsoft's release archive, checked against its own sha256 + * The library this system's binding links to, out of WireCat's npm package of it, checked against its own sha256 * and placed beside a copy of the binding, which loads it from its own folder. Only from `models download`: a search * never downloads. `undefined` when this system runs models through WebAssembly only. */ @@ -166,9 +155,7 @@ export const installNativeRuntime = async ({ const folder = runtimeFolder(runtime, env) if (isInstalled(runtime, folder)) return folder - progress( - `ONNX Runtime ${ONNX_RUNTIME_VERSION} for ${runtime.target}: ${megabytes(runtime.archive.bytes)} from Microsoft`, - ) + progress(`ONNX Runtime ${ONNX_RUNTIME_VERSION} for ${runtime.target}: ${megabytes(runtime.archive.bytes)} from npm`) const parent = sharedModelsDirectory("runtime", env) mkdirSync(parent, { recursive: true, mode: 0o700 }) const staging = mkdtempSync(join(parent, ".onnxruntime-"))