@localinference/utils
Advanced tools
| "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/.errors/class.ts | ||
| var LocalInferenceUtilsError = class extends Error { | ||
| constructor(code, message, options) { | ||
| const detail = _nullishCoalesce(message, () => ( code)); | ||
| super(`{@localinference/utils} ${detail}`, options); | ||
| this.code = code; | ||
| this.name = "LocalInferenceUtilsError"; | ||
| } | ||
| }; | ||
| // src/GPUAccelerationSupported/index.ts | ||
| var isNodeJs = () => typeof process !== "undefined" && !!_optionalChain([process, 'access', _ => _.versions, 'optionalAccess', _2 => _2.node]); | ||
| var isDeno = () => !!_optionalChain([globalThis, 'access', _3 => _3.Deno, 'optionalAccess', _4 => _4.version, 'optionalAccess', _5 => _5.deno]); | ||
| var getNavigator = () => globalThis.navigator; | ||
| var hasWebNN = () => typeof _optionalChain([getNavigator, 'call', _6 => _6(), 'optionalAccess', _7 => _7.ml, 'optionalAccess', _8 => _8.createContext]) === "function"; | ||
| var hasWebGPU = () => typeof _optionalChain([getNavigator, 'call', _9 => _9(), 'optionalAccess', _10 => _10.gpu, 'optionalAccess', _11 => _11.requestAdapter]) === "function"; | ||
| var hasWebGLContext = (canvas) => !!(_nullishCoalesce(_nullishCoalesce(_optionalChain([canvas, 'optionalAccess', _12 => _12.getContext, 'optionalCall', _13 => _13("webgl2")]), () => ( _optionalChain([canvas, 'optionalAccess', _14 => _14.getContext, 'optionalCall', _15 => _15("webgl")]))), () => ( _optionalChain([canvas, 'optionalAccess', _16 => _16.getContext, 'optionalCall', _17 => _17("experimental-webgl")])))); | ||
| var hasWebGL = () => { | ||
| try { | ||
| if (typeof OffscreenCanvas !== "undefined") { | ||
| return hasWebGLContext(new OffscreenCanvas(1, 1)); | ||
| } | ||
| if (typeof document !== "undefined") { | ||
| return hasWebGLContext(document.createElement("canvas")); | ||
| } | ||
| } catch (e) { | ||
| return false; | ||
| } | ||
| return false; | ||
| }; | ||
| function GPUAccelerationSupported() { | ||
| if (isNodeJs() || isDeno()) { | ||
| return false; | ||
| } | ||
| return hasWebNN() || hasWebGPU() || hasWebGL(); | ||
| } | ||
| // src/createInferenceSession/index.ts | ||
| var getExecutionProviders = (useGPUAcceleration) => useGPUAcceleration ? ["webnn", "webgpu", "webgl", "wasm"] : ["wasm"]; | ||
| var getRuntime = async (useGPUAcceleration) => { | ||
| if (useGPUAcceleration) { | ||
| return Promise.resolve().then(() => _interopRequireWildcard(require("onnxruntime-web/all"))); | ||
| } | ||
| const runtime = await Promise.resolve().then(() => _interopRequireWildcard(require("onnxruntime-web"))); | ||
| if (isDeno()) { | ||
| runtime.env.wasm.numThreads = 1; | ||
| } | ||
| return runtime; | ||
| }; | ||
| async function createInferenceSession(model) { | ||
| const useGPUAcceleration = GPUAccelerationSupported(); | ||
| const executionProviders = getExecutionProviders(useGPUAcceleration); | ||
| const runtime = await getRuntime(useGPUAcceleration); | ||
| try { | ||
| return await runtime.InferenceSession.create(model, { | ||
| executionProviders | ||
| }); | ||
| } catch (cause) { | ||
| throw new LocalInferenceUtilsError( | ||
| "INFERENCE_SESSION_CREATE_FAILED", | ||
| "failed to create ONNX Runtime inference session", | ||
| { cause } | ||
| ); | ||
| } | ||
| } | ||
| // src/createTokenizer/index.ts | ||
| var _bytecodec = require('@sovereignbase/bytecodec'); | ||
| var _sentencepiecejs = require('@sctg/sentencepiece-js'); | ||
| async function createTokenizer(model) { | ||
| const tokenizer = new (0, _sentencepiecejs.SentencePieceProcessor)(); | ||
| try { | ||
| await tokenizer.loadFromB64StringModel(_bytecodec.toBase64String.call(void 0, model)); | ||
| } catch (cause) { | ||
| throw new LocalInferenceUtilsError( | ||
| "TOKENIZER_MODEL_LOAD_FAILED", | ||
| "failed to load SentencePiece model", | ||
| { cause } | ||
| ); | ||
| } | ||
| return tokenizer; | ||
| } | ||
| exports.GPUAccelerationSupported = GPUAccelerationSupported; exports.LocalInferenceUtilsError = LocalInferenceUtilsError; exports.createInferenceSession = createInferenceSession; exports.createTokenizer = createTokenizer; | ||
| //# sourceMappingURL=index.cjs.map |
| {"version":3,"sources":["/home/runner/work/utils/utils/dist/index.cjs","../src/.errors/class.ts","../src/GPUAccelerationSupported/index.ts","../src/createInferenceSession/index.ts","../src/createTokenizer/index.ts"],"names":[],"mappings":"AAAA;ACIO,IAAM,yBAAA,EAAN,MAAA,QAAuC,MAAM;AAAA,EACzC;AAAA,EAET,WAAA,CACE,IAAA,EACA,OAAA,EACA,OAAA,EACA;AACA,IAAA,MAAM,OAAA,mBAAS,OAAA,UAAW,MAAA;AAC1B,IAAA,KAAA,CAAM,CAAA,wBAAA,EAA2B,MAAM,CAAA,CAAA;AAC3B,IAAA;AACA,IAAA;AACd,EAAA;AACF;ADP4C;AACA;AEWvB;AAGoC;AAG5C;AAGY;AAGH;AAIlB;AAK4B;AAC1B,EAAA;AAC6B,IAAA;AACF,MAAA;AAC7B,IAAA;AAEqC,IAAA;AACH,MAAA;AAClC,IAAA;AACM,EAAA;AACC,IAAA;AACT,EAAA;AAEO,EAAA;AACT;AAWoD;AACtB,EAAA;AACnB,IAAA;AACT,EAAA;AAEoC,EAAA;AACtC;AFrC4C;AACA;AG/Bb;AAI7B;AAEwB,EAAA;AACa,IAAA;AACrC,EAAA;AAE6B,EAAA;AAEf,EAAA;AACkB,IAAA;AAChC,EAAA;AAEO,EAAA;AACT;AAgBE;AAE2B,EAAA;AACA,EAAA;AACM,EAAA;AAE7B,EAAA;AACoC,IAAA;AACpC,MAAA;AACD,IAAA;AACa,EAAA;AACJ,IAAA;AACR,MAAA;AACA,MAAA;AACQ,MAAA;AACV,IAAA;AACF,EAAA;AACF;AHS4C;AACA;AInEb;AACQ;AAYJ;AACX,EAAA;AAElB,EAAA;AACqC,IAAA;AACzB,EAAA;AACJ,IAAA;AACR,MAAA;AACA,MAAA;AACQ,MAAA;AACV,IAAA;AACF,EAAA;AAEO,EAAA;AACT;AJwD4C;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/utils/utils/dist/index.cjs","sourcesContent":[null,"export type LocalInferenceUtilsErrorCode =\n | 'TOKENIZER_MODEL_LOAD_FAILED'\n | 'INFERENCE_SESSION_CREATE_FAILED'\n\nexport class LocalInferenceUtilsError extends Error {\n readonly code: LocalInferenceUtilsErrorCode\n\n constructor(\n code: LocalInferenceUtilsErrorCode,\n message?: string,\n options?: ErrorOptions\n ) {\n const detail = message ?? code\n super(`{@localinference/utils} ${detail}`, options)\n this.code = code\n this.name = 'LocalInferenceUtilsError'\n }\n}\n","type DenoGlobal = {\n Deno?: {\n version?: {\n deno?: string\n }\n }\n}\n\ntype NavigatorWithAcceleration = Navigator & {\n gpu?: {\n requestAdapter?: unknown\n }\n ml?: {\n createContext?: unknown\n }\n}\n\ntype CanvasLike = {\n getContext?: (...args: unknown[]) => unknown\n}\n\nconst isNodeJs = (): boolean =>\n typeof process !== 'undefined' && !!process.versions?.node\n\nexport const isDeno = (): boolean =>\n !!(globalThis as typeof globalThis & DenoGlobal).Deno?.version?.deno\n\nconst getNavigator = (): NavigatorWithAcceleration | undefined =>\n globalThis.navigator as NavigatorWithAcceleration | undefined\n\nconst hasWebNN = (): boolean =>\n typeof getNavigator()?.ml?.createContext === 'function'\n\nconst hasWebGPU = (): boolean =>\n typeof getNavigator()?.gpu?.requestAdapter === 'function'\n\nconst hasWebGLContext = (canvas: CanvasLike | null | undefined): boolean =>\n !!(\n canvas?.getContext?.('webgl2') ??\n canvas?.getContext?.('webgl') ??\n canvas?.getContext?.('experimental-webgl')\n )\n\nconst hasWebGL = (): boolean => {\n try {\n if (typeof OffscreenCanvas !== 'undefined') {\n return hasWebGLContext(new OffscreenCanvas(1, 1) as unknown as CanvasLike)\n }\n\n if (typeof document !== 'undefined') {\n return hasWebGLContext(document.createElement('canvas') as CanvasLike)\n }\n } catch {\n return false\n }\n\n return false\n}\n\n/**\n * Indicates whether the current runtime likely exposes GPU-backed browser APIs.\n *\n * This probe is intended for browsers and browser web workers. In Node.js,\n * Bun, Deno, and other server-style runtimes, it always returns `false`.\n *\n * @returns `true` when WebNN, WebGPU, or WebGL appears to be available;\n * otherwise `false`.\n */\nexport function GPUAccelerationSupported(): boolean {\n if (isNodeJs() || isDeno()) {\n return false\n }\n\n return hasWebNN() || hasWebGPU() || hasWebGL()\n}\n","import type { InferenceSession } from 'onnxruntime-web'\nimport { LocalInferenceUtilsError } from '../.errors/class.js'\nimport {\n GPUAccelerationSupported,\n isDeno,\n} from '../GPUAccelerationSupported/index.js'\n\nconst getExecutionProviders = (useGPUAcceleration: boolean): string[] =>\n useGPUAcceleration ? ['webnn', 'webgpu', 'webgl', 'wasm'] : ['wasm']\n\nconst getRuntime = async (\n useGPUAcceleration: boolean\n): Promise<typeof import('onnxruntime-web')> => {\n if (useGPUAcceleration) {\n return import('onnxruntime-web/all')\n }\n\n const runtime = await import('onnxruntime-web')\n\n if (isDeno()) {\n runtime.env.wasm.numThreads = 1\n }\n\n return runtime\n}\n\n/**\n * Creates an ONNX Runtime inference session from a serialized model.\n *\n * In Node.js, Bun, Deno, and runtimes without likely browser GPU acceleration,\n * the session uses the WASM execution provider. In browser-like runtimes that\n * appear to expose WebNN, WebGPU, or WebGL, it prefers those execution\n * providers before falling back to WASM.\n *\n * @param model The serialized ONNX model bytes.\n * @returns A promise that fulfills with the initialized inference session.\n * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded\n * into an ONNX Runtime session.\n */\nexport async function createInferenceSession(\n model: Uint8Array\n): Promise<InferenceSession> {\n const useGPUAcceleration = GPUAccelerationSupported()\n const executionProviders = getExecutionProviders(useGPUAcceleration)\n const runtime = await getRuntime(useGPUAcceleration)\n\n try {\n return await runtime.InferenceSession.create(model, {\n executionProviders,\n })\n } catch (cause) {\n throw new LocalInferenceUtilsError(\n 'INFERENCE_SESSION_CREATE_FAILED',\n 'failed to create ONNX Runtime inference session',\n { cause }\n )\n }\n}\n","import { toBase64String } from '@sovereignbase/bytecodec'\nimport { SentencePieceProcessor } from '@sctg/sentencepiece-js'\nimport { LocalInferenceUtilsError } from '../.errors/class.js'\n\n/**\n * Creates a SentencePiece tokenizer from a serialized model.\n *\n * @param model The serialized SentencePiece model bytes.\n * @returns A promise that fulfills with the loaded tokenizer.\n * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded.\n */\nexport async function createTokenizer(\n model: Uint8Array\n): Promise<SentencePieceProcessor> {\n const tokenizer = new SentencePieceProcessor()\n\n try {\n await tokenizer.loadFromB64StringModel(toBase64String(model))\n } catch (cause) {\n throw new LocalInferenceUtilsError(\n 'TOKENIZER_MODEL_LOAD_FAILED',\n 'failed to load SentencePiece model',\n { cause }\n )\n }\n\n return tokenizer\n}\n"]} |
| import { InferenceSession } from 'onnxruntime-web'; | ||
| import { SentencePieceProcessor } from '@sctg/sentencepiece-js'; | ||
| type LocalInferenceUtilsErrorCode = 'TOKENIZER_MODEL_LOAD_FAILED' | 'INFERENCE_SESSION_CREATE_FAILED'; | ||
| declare class LocalInferenceUtilsError extends Error { | ||
| readonly code: LocalInferenceUtilsErrorCode; | ||
| constructor(code: LocalInferenceUtilsErrorCode, message?: string, options?: ErrorOptions); | ||
| } | ||
| /** | ||
| * Indicates whether the current runtime likely exposes GPU-backed browser APIs. | ||
| * | ||
| * This probe is intended for browsers and browser web workers. In Node.js, | ||
| * Bun, Deno, and other server-style runtimes, it always returns `false`. | ||
| * | ||
| * @returns `true` when WebNN, WebGPU, or WebGL appears to be available; | ||
| * otherwise `false`. | ||
| */ | ||
| declare function GPUAccelerationSupported(): boolean; | ||
| /** | ||
| * Creates an ONNX Runtime inference session from a serialized model. | ||
| * | ||
| * In Node.js, Bun, Deno, and runtimes without likely browser GPU acceleration, | ||
| * the session uses the WASM execution provider. In browser-like runtimes that | ||
| * appear to expose WebNN, WebGPU, or WebGL, it prefers those execution | ||
| * providers before falling back to WASM. | ||
| * | ||
| * @param model The serialized ONNX model bytes. | ||
| * @returns A promise that fulfills with the initialized inference session. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded | ||
| * into an ONNX Runtime session. | ||
| */ | ||
| declare function createInferenceSession(model: Uint8Array): Promise<InferenceSession>; | ||
| /** | ||
| * Creates a SentencePiece tokenizer from a serialized model. | ||
| * | ||
| * @param model The serialized SentencePiece model bytes. | ||
| * @returns A promise that fulfills with the loaded tokenizer. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded. | ||
| */ | ||
| declare function createTokenizer(model: Uint8Array): Promise<SentencePieceProcessor>; | ||
| export { GPUAccelerationSupported, LocalInferenceUtilsError, type LocalInferenceUtilsErrorCode, createInferenceSession, createTokenizer }; |
| {"version":3,"sources":["../src/.errors/class.ts","../src/GPUAccelerationSupported/index.ts","../src/createInferenceSession/index.ts","../src/createTokenizer/index.ts"],"sourcesContent":["export type LocalInferenceUtilsErrorCode =\n | 'TOKENIZER_MODEL_LOAD_FAILED'\n | 'INFERENCE_SESSION_CREATE_FAILED'\n\nexport class LocalInferenceUtilsError extends Error {\n readonly code: LocalInferenceUtilsErrorCode\n\n constructor(\n code: LocalInferenceUtilsErrorCode,\n message?: string,\n options?: ErrorOptions\n ) {\n const detail = message ?? code\n super(`{@localinference/utils} ${detail}`, options)\n this.code = code\n this.name = 'LocalInferenceUtilsError'\n }\n}\n","type DenoGlobal = {\n Deno?: {\n version?: {\n deno?: string\n }\n }\n}\n\ntype NavigatorWithAcceleration = Navigator & {\n gpu?: {\n requestAdapter?: unknown\n }\n ml?: {\n createContext?: unknown\n }\n}\n\ntype CanvasLike = {\n getContext?: (...args: unknown[]) => unknown\n}\n\nconst isNodeJs = (): boolean =>\n typeof process !== 'undefined' && !!process.versions?.node\n\nexport const isDeno = (): boolean =>\n !!(globalThis as typeof globalThis & DenoGlobal).Deno?.version?.deno\n\nconst getNavigator = (): NavigatorWithAcceleration | undefined =>\n globalThis.navigator as NavigatorWithAcceleration | undefined\n\nconst hasWebNN = (): boolean =>\n typeof getNavigator()?.ml?.createContext === 'function'\n\nconst hasWebGPU = (): boolean =>\n typeof getNavigator()?.gpu?.requestAdapter === 'function'\n\nconst hasWebGLContext = (canvas: CanvasLike | null | undefined): boolean =>\n !!(\n canvas?.getContext?.('webgl2') ??\n canvas?.getContext?.('webgl') ??\n canvas?.getContext?.('experimental-webgl')\n )\n\nconst hasWebGL = (): boolean => {\n try {\n if (typeof OffscreenCanvas !== 'undefined') {\n return hasWebGLContext(new OffscreenCanvas(1, 1) as unknown as CanvasLike)\n }\n\n if (typeof document !== 'undefined') {\n return hasWebGLContext(document.createElement('canvas') as CanvasLike)\n }\n } catch {\n return false\n }\n\n return false\n}\n\n/**\n * Indicates whether the current runtime likely exposes GPU-backed browser APIs.\n *\n * This probe is intended for browsers and browser web workers. In Node.js,\n * Bun, Deno, and other server-style runtimes, it always returns `false`.\n *\n * @returns `true` when WebNN, WebGPU, or WebGL appears to be available;\n * otherwise `false`.\n */\nexport function GPUAccelerationSupported(): boolean {\n if (isNodeJs() || isDeno()) {\n return false\n }\n\n return hasWebNN() || hasWebGPU() || hasWebGL()\n}\n","import type { InferenceSession } from 'onnxruntime-web'\nimport { LocalInferenceUtilsError } from '../.errors/class.js'\nimport {\n GPUAccelerationSupported,\n isDeno,\n} from '../GPUAccelerationSupported/index.js'\n\nconst getExecutionProviders = (useGPUAcceleration: boolean): string[] =>\n useGPUAcceleration ? ['webnn', 'webgpu', 'webgl', 'wasm'] : ['wasm']\n\nconst getRuntime = async (\n useGPUAcceleration: boolean\n): Promise<typeof import('onnxruntime-web')> => {\n if (useGPUAcceleration) {\n return import('onnxruntime-web/all')\n }\n\n const runtime = await import('onnxruntime-web')\n\n if (isDeno()) {\n runtime.env.wasm.numThreads = 1\n }\n\n return runtime\n}\n\n/**\n * Creates an ONNX Runtime inference session from a serialized model.\n *\n * In Node.js, Bun, Deno, and runtimes without likely browser GPU acceleration,\n * the session uses the WASM execution provider. In browser-like runtimes that\n * appear to expose WebNN, WebGPU, or WebGL, it prefers those execution\n * providers before falling back to WASM.\n *\n * @param model The serialized ONNX model bytes.\n * @returns A promise that fulfills with the initialized inference session.\n * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded\n * into an ONNX Runtime session.\n */\nexport async function createInferenceSession(\n model: Uint8Array\n): Promise<InferenceSession> {\n const useGPUAcceleration = GPUAccelerationSupported()\n const executionProviders = getExecutionProviders(useGPUAcceleration)\n const runtime = await getRuntime(useGPUAcceleration)\n\n try {\n return await runtime.InferenceSession.create(model, {\n executionProviders,\n })\n } catch (cause) {\n throw new LocalInferenceUtilsError(\n 'INFERENCE_SESSION_CREATE_FAILED',\n 'failed to create ONNX Runtime inference session',\n { cause }\n )\n }\n}\n","import { toBase64String } from '@sovereignbase/bytecodec'\nimport { SentencePieceProcessor } from '@sctg/sentencepiece-js'\nimport { LocalInferenceUtilsError } from '../.errors/class.js'\n\n/**\n * Creates a SentencePiece tokenizer from a serialized model.\n *\n * @param model The serialized SentencePiece model bytes.\n * @returns A promise that fulfills with the loaded tokenizer.\n * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded.\n */\nexport async function createTokenizer(\n model: Uint8Array\n): Promise<SentencePieceProcessor> {\n const tokenizer = new SentencePieceProcessor()\n\n try {\n await tokenizer.loadFromB64StringModel(toBase64String(model))\n } catch (cause) {\n throw new LocalInferenceUtilsError(\n 'TOKENIZER_MODEL_LOAD_FAILED',\n 'failed to load SentencePiece model',\n { cause }\n )\n }\n\n return tokenizer\n}\n"],"mappings":";AAIO,IAAM,2BAAN,cAAuC,MAAM;AAAA,EACzC;AAAA,EAET,YACE,MACA,SACA,SACA;AACA,UAAM,SAAS,WAAW;AAC1B,UAAM,2BAA2B,MAAM,IAAI,OAAO;AAClD,SAAK,OAAO;AACZ,SAAK,OAAO;AAAA,EACd;AACF;;;ACIA,IAAM,WAAW,MACf,OAAO,YAAY,eAAe,CAAC,CAAC,QAAQ,UAAU;AAEjD,IAAM,SAAS,MACpB,CAAC,CAAE,WAA8C,MAAM,SAAS;AAElE,IAAM,eAAe,MACnB,WAAW;AAEb,IAAM,WAAW,MACf,OAAO,aAAa,GAAG,IAAI,kBAAkB;AAE/C,IAAM,YAAY,MAChB,OAAO,aAAa,GAAG,KAAK,mBAAmB;AAEjD,IAAM,kBAAkB,CAAC,WACvB,CAAC,EACC,QAAQ,aAAa,QAAQ,KAC7B,QAAQ,aAAa,OAAO,KAC5B,QAAQ,aAAa,oBAAoB;AAG7C,IAAM,WAAW,MAAe;AAC9B,MAAI;AACF,QAAI,OAAO,oBAAoB,aAAa;AAC1C,aAAO,gBAAgB,IAAI,gBAAgB,GAAG,CAAC,CAA0B;AAAA,IAC3E;AAEA,QAAI,OAAO,aAAa,aAAa;AACnC,aAAO,gBAAgB,SAAS,cAAc,QAAQ,CAAe;AAAA,IACvE;AAAA,EACF,QAAQ;AACN,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAWO,SAAS,2BAAoC;AAClD,MAAI,SAAS,KAAK,OAAO,GAAG;AAC1B,WAAO;AAAA,EACT;AAEA,SAAO,SAAS,KAAK,UAAU,KAAK,SAAS;AAC/C;;;ACnEA,IAAM,wBAAwB,CAAC,uBAC7B,qBAAqB,CAAC,SAAS,UAAU,SAAS,MAAM,IAAI,CAAC,MAAM;AAErE,IAAM,aAAa,OACjB,uBAC8C;AAC9C,MAAI,oBAAoB;AACtB,WAAO,OAAO,qBAAqB;AAAA,EACrC;AAEA,QAAM,UAAU,MAAM,OAAO,iBAAiB;AAE9C,MAAI,OAAO,GAAG;AACZ,YAAQ,IAAI,KAAK,aAAa;AAAA,EAChC;AAEA,SAAO;AACT;AAeA,eAAsB,uBACpB,OAC2B;AAC3B,QAAM,qBAAqB,yBAAyB;AACpD,QAAM,qBAAqB,sBAAsB,kBAAkB;AACnE,QAAM,UAAU,MAAM,WAAW,kBAAkB;AAEnD,MAAI;AACF,WAAO,MAAM,QAAQ,iBAAiB,OAAO,OAAO;AAAA,MAClD;AAAA,IACF,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR;AAAA,MACA;AAAA,MACA,EAAE,MAAM;AAAA,IACV;AAAA,EACF;AACF;;;ACzDA,SAAS,sBAAsB;AAC/B,SAAS,8BAA8B;AAUvC,eAAsB,gBACpB,OACiC;AACjC,QAAM,YAAY,IAAI,uBAAuB;AAE7C,MAAI;AACF,UAAM,UAAU,uBAAuB,eAAe,KAAK,CAAC;AAAA,EAC9D,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR;AAAA,MACA;AAAA,MACA,EAAE,MAAM;AAAA,IACV;AAAA,EACF;AAEA,SAAO;AACT;","names":[]} |
+45
-5
@@ -1,5 +0,45 @@ | ||
| export { LocalInferenceUtilsError } from './.errors/class.js'; | ||
| export { createInferenceSession } from './createInferenceSession/index.js'; | ||
| export { createTokenizer } from './createTokenizer/index.js'; | ||
| export type { LocalInferenceUtilsErrorCode } from './.errors/class.js'; | ||
| //# sourceMappingURL=index.d.ts.map | ||
| import { InferenceSession } from 'onnxruntime-web'; | ||
| import { SentencePieceProcessor } from '@sctg/sentencepiece-js'; | ||
| type LocalInferenceUtilsErrorCode = 'TOKENIZER_MODEL_LOAD_FAILED' | 'INFERENCE_SESSION_CREATE_FAILED'; | ||
| declare class LocalInferenceUtilsError extends Error { | ||
| readonly code: LocalInferenceUtilsErrorCode; | ||
| constructor(code: LocalInferenceUtilsErrorCode, message?: string, options?: ErrorOptions); | ||
| } | ||
| /** | ||
| * Indicates whether the current runtime likely exposes GPU-backed browser APIs. | ||
| * | ||
| * This probe is intended for browsers and browser web workers. In Node.js, | ||
| * Bun, Deno, and other server-style runtimes, it always returns `false`. | ||
| * | ||
| * @returns `true` when WebNN, WebGPU, or WebGL appears to be available; | ||
| * otherwise `false`. | ||
| */ | ||
| declare function GPUAccelerationSupported(): boolean; | ||
| /** | ||
| * Creates an ONNX Runtime inference session from a serialized model. | ||
| * | ||
| * In Node.js, Bun, Deno, and runtimes without likely browser GPU acceleration, | ||
| * the session uses the WASM execution provider. In browser-like runtimes that | ||
| * appear to expose WebNN, WebGPU, or WebGL, it prefers those execution | ||
| * providers before falling back to WASM. | ||
| * | ||
| * @param model The serialized ONNX model bytes. | ||
| * @returns A promise that fulfills with the initialized inference session. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded | ||
| * into an ONNX Runtime session. | ||
| */ | ||
| declare function createInferenceSession(model: Uint8Array): Promise<InferenceSession>; | ||
| /** | ||
| * Creates a SentencePiece tokenizer from a serialized model. | ||
| * | ||
| * @param model The serialized SentencePiece model bytes. | ||
| * @returns A promise that fulfills with the loaded tokenizer. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded. | ||
| */ | ||
| declare function createTokenizer(model: Uint8Array): Promise<SentencePieceProcessor>; | ||
| export { GPUAccelerationSupported, LocalInferenceUtilsError, type LocalInferenceUtilsErrorCode, createInferenceSession, createTokenizer }; |
+89
-3
@@ -1,4 +0,90 @@ | ||
| export { LocalInferenceUtilsError } from './.errors/class.js'; | ||
| export { createInferenceSession } from './createInferenceSession/index.js'; | ||
| export { createTokenizer } from './createTokenizer/index.js'; | ||
| // src/.errors/class.ts | ||
| var LocalInferenceUtilsError = class extends Error { | ||
| code; | ||
| constructor(code, message, options) { | ||
| const detail = message ?? code; | ||
| super(`{@localinference/utils} ${detail}`, options); | ||
| this.code = code; | ||
| this.name = "LocalInferenceUtilsError"; | ||
| } | ||
| }; | ||
| // src/GPUAccelerationSupported/index.ts | ||
| var isNodeJs = () => typeof process !== "undefined" && !!process.versions?.node; | ||
| var isDeno = () => !!globalThis.Deno?.version?.deno; | ||
| var getNavigator = () => globalThis.navigator; | ||
| var hasWebNN = () => typeof getNavigator()?.ml?.createContext === "function"; | ||
| var hasWebGPU = () => typeof getNavigator()?.gpu?.requestAdapter === "function"; | ||
| var hasWebGLContext = (canvas) => !!(canvas?.getContext?.("webgl2") ?? canvas?.getContext?.("webgl") ?? canvas?.getContext?.("experimental-webgl")); | ||
| var hasWebGL = () => { | ||
| try { | ||
| if (typeof OffscreenCanvas !== "undefined") { | ||
| return hasWebGLContext(new OffscreenCanvas(1, 1)); | ||
| } | ||
| if (typeof document !== "undefined") { | ||
| return hasWebGLContext(document.createElement("canvas")); | ||
| } | ||
| } catch { | ||
| return false; | ||
| } | ||
| return false; | ||
| }; | ||
| function GPUAccelerationSupported() { | ||
| if (isNodeJs() || isDeno()) { | ||
| return false; | ||
| } | ||
| return hasWebNN() || hasWebGPU() || hasWebGL(); | ||
| } | ||
| // src/createInferenceSession/index.ts | ||
| var getExecutionProviders = (useGPUAcceleration) => useGPUAcceleration ? ["webnn", "webgpu", "webgl", "wasm"] : ["wasm"]; | ||
| var getRuntime = async (useGPUAcceleration) => { | ||
| if (useGPUAcceleration) { | ||
| return import("onnxruntime-web/all"); | ||
| } | ||
| const runtime = await import("onnxruntime-web"); | ||
| if (isDeno()) { | ||
| runtime.env.wasm.numThreads = 1; | ||
| } | ||
| return runtime; | ||
| }; | ||
| async function createInferenceSession(model) { | ||
| const useGPUAcceleration = GPUAccelerationSupported(); | ||
| const executionProviders = getExecutionProviders(useGPUAcceleration); | ||
| const runtime = await getRuntime(useGPUAcceleration); | ||
| try { | ||
| return await runtime.InferenceSession.create(model, { | ||
| executionProviders | ||
| }); | ||
| } catch (cause) { | ||
| throw new LocalInferenceUtilsError( | ||
| "INFERENCE_SESSION_CREATE_FAILED", | ||
| "failed to create ONNX Runtime inference session", | ||
| { cause } | ||
| ); | ||
| } | ||
| } | ||
| // src/createTokenizer/index.ts | ||
| import { toBase64String } from "@sovereignbase/bytecodec"; | ||
| import { SentencePieceProcessor } from "@sctg/sentencepiece-js"; | ||
| async function createTokenizer(model) { | ||
| const tokenizer = new SentencePieceProcessor(); | ||
| try { | ||
| await tokenizer.loadFromB64StringModel(toBase64String(model)); | ||
| } catch (cause) { | ||
| throw new LocalInferenceUtilsError( | ||
| "TOKENIZER_MODEL_LOAD_FAILED", | ||
| "failed to load SentencePiece model", | ||
| { cause } | ||
| ); | ||
| } | ||
| return tokenizer; | ||
| } | ||
| export { | ||
| GPUAccelerationSupported, | ||
| LocalInferenceUtilsError, | ||
| createInferenceSession, | ||
| createTokenizer | ||
| }; | ||
| //# sourceMappingURL=index.js.map |
+0
-0
@@ -0,0 +0,0 @@ Apache License |
+11
-12
| { | ||
| "name": "@localinference/utils", | ||
| "version": "0.0.0", | ||
| "version": "1.0.0", | ||
| "description": "Local Inference internal utils package to avoid boilerplate across codebases.", | ||
@@ -18,4 +18,4 @@ "keywords": [ | ||
| "type": "module", | ||
| "homepage": "https://github.com/localinference/utils#readme", | ||
| "main": "./dist-cjs/index.cjs", | ||
| "main": "./dist/index.cjs", | ||
| "module": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
@@ -26,10 +26,10 @@ "exports": { | ||
| "import": "./dist/index.js", | ||
| "require": "./dist-cjs/index.cjs" | ||
| "require": "./dist/index.cjs", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "sideEffects": false, | ||
| "files": [ | ||
| "dist/**/*.js", | ||
| "dist/**/*.d.ts", | ||
| "dist-cjs/**/*.cjs", | ||
| "dist", | ||
| "LICENSE", | ||
@@ -45,8 +45,7 @@ "README.md" | ||
| }, | ||
| "homepage": "https://github.com/localinference/utils#readme", | ||
| "scripts": { | ||
| "format": "prettier . --write", | ||
| "format:check": "prettier . --check", | ||
| "build:esm": "tsc -p tsconfig.json", | ||
| "build:cjs": "tsup", | ||
| "build": "npm run build:esm && npm run build:cjs", | ||
| "build": "tsup", | ||
| "test:coverage": "npm run build && node test/run-coverage.mjs", | ||
@@ -71,3 +70,2 @@ "test:e2e": "npm run build && node test/e2e/run.mjs", | ||
| }, | ||
| "sideEffects": false, | ||
| "engines": { | ||
@@ -82,2 +80,3 @@ "node": ">=22" | ||
| "c8": "^11.0.0", | ||
| "edge-runtime": "^4.0.1", | ||
| "fast-glob": "^3.3.3", | ||
@@ -92,5 +91,5 @@ "husky": "^9.1.7", | ||
| "@sctg/sentencepiece-js": "^1.3.3", | ||
| "@sovereignbase/bytecodec": "^1.2.0", | ||
| "@sovereignbase/bytecodec": "^1.3.3", | ||
| "onnxruntime-web": "^1.24.3" | ||
| } | ||
| } |
+36
-11
@@ -12,3 +12,3 @@ [](https://www.npmjs.com/package/@localinference/utils) | ||
| - Runtimes: Node >= 22; Bun and Deno via npm compatibility; Browsers: modern browsers with WebAssembly; Workers/Edge: browser-like runtimes may work when ONNX Runtime Web assets are reachable. | ||
| - Runtimes: Node >= 22; Bun and Deno via npm compatibility; Browsers and browser web workers with WebAssembly; service-worker-style edge runtimes for package loading, GPU-detection fallback, and wasm-path selection smoke coverage. | ||
| - Module formats: ESM and CommonJS. | ||
@@ -33,2 +33,8 @@ - Required globals / APIs: `Uint8Array`; browser runtimes need ONNX Runtime Web compatible backends such as `WebNN`, `WebGPU`, `WebGL`, or `WebAssembly`. | ||
| yarn add @localinference/utils | ||
| # or | ||
| bun add @localinference/utils | ||
| # or | ||
| deno add jsr:@localinference/utils | ||
| # or | ||
| vlt install jsr:@localinference/utils | ||
| ``` | ||
@@ -38,2 +44,12 @@ | ||
| ### Check whether browser GPU acceleration is likely available | ||
| ```ts | ||
| import { GPUAccelerationSupported } from '@localinference/utils' | ||
| if (GPUAccelerationSupported()) { | ||
| console.log('Browser GPU acceleration APIs look available') | ||
| } | ||
| ``` | ||
| ### Load a tokenizer | ||
@@ -69,6 +85,12 @@ | ||
| ### Browsers / Edge runtimes | ||
| ### Browsers / Web Workers | ||
| `createInferenceSession()` loads `onnxruntime-web/all` and prefers `webnn`, `webgpu`, `webgl`, then `wasm`. Browser builds must make the ONNX Runtime Web wasm assets reachable to the app. | ||
| `GPUAccelerationSupported()` is a heuristic boolean probe for browser and browser-worker contexts. It checks for `WebNN`, `WebGPU`, and `WebGL`. `createInferenceSession()` only loads `onnxruntime-web/all` and prefers `webnn`, `webgpu`, `webgl`, then `wasm` when that probe returns `true`. Otherwise it stays on the `wasm` path. Browser builds must make the ONNX Runtime Web wasm assets reachable to the app. | ||
| ### Cloudflare Workers / Edge runtimes | ||
| This package is smoke-tested in a service-worker-style edge runtime through `edge-runtime`. That coverage verifies that the package loads, `GPUAccelerationSupported()` returns `false`, and `createInferenceSession()` selects the wasm-only path instead of browser GPU providers. | ||
| That is narrower than full cross-platform inference support. Real ONNX execution still depends on each edge platform's WebAssembly loading model and asset rules, so Cloudflare Workers, Vercel Edge, and similar runtimes should still be validated in the exact deployment target before being treated as a production inference environment. | ||
| ### Validation & errors | ||
@@ -93,5 +115,8 @@ | ||
| - Runtime e2e coverage: | ||
| - Node | ||
| - Bun | ||
| - Deno | ||
| - Node ESM | ||
| - Node CommonJS | ||
| - Bun ESM | ||
| - Bun CommonJS | ||
| - Deno ESM | ||
| - Vercel Edge Runtime ESM (`GPUAccelerationSupported()` / wasm-path smoke) | ||
| - Chromium | ||
@@ -104,3 +129,3 @@ - Firefox | ||
| `npm test` builds the package, runs the `node:test` unit/integration suite under `c8`, then runs the end-to-end smoke suite against the built package across all supported runtimes above. | ||
| `npm test` builds the package, runs the `node:test` unit/integration suite under `c8`, then runs the end-to-end smoke suite against the built package across the ESM/CJS/runtime matrix above. | ||
@@ -113,6 +138,6 @@ ## Benchmarks | ||
| - Results: | ||
| - tokenizer load: `14.6 ops/s` (`68.516 ms/op`) | ||
| - tokenizer encode: `74521.6 ops/s` (`0.013 ms/op`) | ||
| - session create: `14.3 ops/s` (`69.823 ms/op`) | ||
| - session run: `11461.1 ops/s` (`0.087 ms/op`) | ||
| - tokenizer load: `7.9 ops/s` (`127.049 ms/op`) | ||
| - tokenizer encode: `48509.3 ops/s` (`0.021 ms/op`) | ||
| - session create: `10.6 ops/s` (`94.519 ms/op`) | ||
| - session run: `8891.1 ops/s` (`0.112 ms/op`) | ||
@@ -119,0 +144,0 @@ Results vary by machine. |
Sorry, the diff of this file is too big to display
| export type LocalInferenceUtilsErrorCode = 'TOKENIZER_MODEL_LOAD_FAILED' | 'INFERENCE_SESSION_CREATE_FAILED'; | ||
| export declare class LocalInferenceUtilsError extends Error { | ||
| readonly code: LocalInferenceUtilsErrorCode; | ||
| constructor(code: LocalInferenceUtilsErrorCode, message?: string, options?: ErrorOptions); | ||
| } | ||
| //# sourceMappingURL=class.d.ts.map |
| export class LocalInferenceUtilsError extends Error { | ||
| code; | ||
| constructor(code, message, options) { | ||
| const detail = message ?? code; | ||
| super(`{@localinference/utils} ${detail}`, options); | ||
| this.code = code; | ||
| this.name = 'LocalInferenceUtilsError'; | ||
| } | ||
| } | ||
| //# sourceMappingURL=class.js.map |
| import type { InferenceSession } from 'onnxruntime-web'; | ||
| /** | ||
| * Creates an ONNX Runtime inference session from a serialized model. | ||
| * | ||
| * In Node.js and Deno, the session uses the WASM execution provider. In | ||
| * browser-like runtimes, the session prefers the WebNN, WebGPU, WebGL, and | ||
| * WASM execution providers in that order. | ||
| * | ||
| * @param model The serialized ONNX model bytes. | ||
| * @returns A promise that fulfills with the initialized inference session. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded | ||
| * into an ONNX Runtime session. | ||
| */ | ||
| export declare function createInferenceSession(model: Uint8Array): Promise<InferenceSession>; | ||
| //# sourceMappingURL=index.d.ts.map |
| import { LocalInferenceUtilsError } from '../.errors/class.js'; | ||
| const isNodeJs = () => typeof process !== 'undefined' && !!process.versions?.node; | ||
| const isDeno = () => !!globalThis.Deno?.version?.deno; | ||
| const usesWasmRuntimeOnly = () => isNodeJs() || isDeno(); | ||
| const getExecutionProviders = () => usesWasmRuntimeOnly() ? ['wasm'] : ['webnn', 'webgpu', 'webgl', 'wasm']; | ||
| const getInferenceSession = async () => { | ||
| if (usesWasmRuntimeOnly()) { | ||
| const runtime = await import('onnxruntime-web'); | ||
| if (isDeno()) { | ||
| runtime.env.wasm.numThreads = 1; | ||
| } | ||
| return runtime.InferenceSession; | ||
| } | ||
| const runtime = await import('onnxruntime-web/all'); | ||
| return runtime.InferenceSession; | ||
| }; | ||
| /** | ||
| * Creates an ONNX Runtime inference session from a serialized model. | ||
| * | ||
| * In Node.js and Deno, the session uses the WASM execution provider. In | ||
| * browser-like runtimes, the session prefers the WebNN, WebGPU, WebGL, and | ||
| * WASM execution providers in that order. | ||
| * | ||
| * @param model The serialized ONNX model bytes. | ||
| * @returns A promise that fulfills with the initialized inference session. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded | ||
| * into an ONNX Runtime session. | ||
| */ | ||
| export async function createInferenceSession(model) { | ||
| const Session = await getInferenceSession(); | ||
| try { | ||
| return await Session.create(model, { | ||
| executionProviders: getExecutionProviders(), | ||
| }); | ||
| } | ||
| catch (cause) { | ||
| throw new LocalInferenceUtilsError('INFERENCE_SESSION_CREATE_FAILED', 'failed to create ONNX Runtime inference session', { cause }); | ||
| } | ||
| } | ||
| //# sourceMappingURL=index.js.map |
| import { SentencePieceProcessor } from '@sctg/sentencepiece-js'; | ||
| /** | ||
| * Creates a SentencePiece tokenizer from a serialized model. | ||
| * | ||
| * @param model The serialized SentencePiece model bytes. | ||
| * @returns A promise that fulfills with the loaded tokenizer. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded. | ||
| */ | ||
| export declare function createTokenizer(model: Uint8Array): Promise<SentencePieceProcessor>; | ||
| //# sourceMappingURL=index.d.ts.map |
| import { toBase64String } from '@sovereignbase/bytecodec'; | ||
| import { SentencePieceProcessor } from '@sctg/sentencepiece-js'; | ||
| import { LocalInferenceUtilsError } from '../.errors/class.js'; | ||
| /** | ||
| * Creates a SentencePiece tokenizer from a serialized model. | ||
| * | ||
| * @param model The serialized SentencePiece model bytes. | ||
| * @returns A promise that fulfills with the loaded tokenizer. | ||
| * @throws {LocalInferenceUtilsError} Thrown if the model bytes cannot be loaded. | ||
| */ | ||
| export async function createTokenizer(model) { | ||
| const tokenizer = new SentencePieceProcessor(); | ||
| try { | ||
| await tokenizer.loadFromB64StringModel(toBase64String(model)); | ||
| } | ||
| catch (cause) { | ||
| throw new LocalInferenceUtilsError('TOKENIZER_MODEL_LOAD_FAILED', 'failed to load SentencePiece model', { cause }); | ||
| } | ||
| return tokenizer; | ||
| } | ||
| //# sourceMappingURL=index.js.map |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
0
-100%142
21.37%0
-100%44050
-94.67%12
9.09%9
-25%210
-96.93%