tempest-react-sdk 0.68.0 → 0.70.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/sw/create-push-handler.cjs +1 -1
- package/dist/sw/create-push-handler.cjs.map +1 -1
- package/dist/sw/create-push-handler.js +58 -30
- package/dist/sw/create-push-handler.js.map +1 -1
- package/dist/sw.d.ts +53 -2
- package/dist/tempest-react-sdk.d.ts +53 -2
- package/dist/vision/core/canvas.cjs +1 -1
- package/dist/vision/core/canvas.cjs.map +1 -1
- package/dist/vision/core/canvas.js +37 -13
- package/dist/vision/core/canvas.js.map +1 -1
- package/dist/vision/core/dtypes.cjs +2 -0
- package/dist/vision/core/dtypes.cjs.map +1 -0
- package/dist/vision/core/dtypes.js +33 -0
- package/dist/vision/core/dtypes.js.map +1 -0
- package/dist/vision/core/graph.cjs +1 -1
- package/dist/vision/core/graph.cjs.map +1 -1
- package/dist/vision/core/graph.js +10 -2
- package/dist/vision/core/graph.js.map +1 -1
- package/dist/vision/core/metadata.cjs +2 -2
- package/dist/vision/core/metadata.cjs.map +1 -1
- package/dist/vision/core/metadata.js +103 -13
- package/dist/vision/core/metadata.js.map +1 -1
- package/dist/vision/core/providers.cjs +1 -1
- package/dist/vision/core/providers.cjs.map +1 -1
- package/dist/vision/core/providers.js +9 -6
- package/dist/vision/core/providers.js.map +1 -1
- package/dist/vision/core/session.cjs +1 -1
- package/dist/vision/core/session.cjs.map +1 -1
- package/dist/vision/core/session.js +115 -27
- package/dist/vision/core/session.js.map +1 -1
- package/dist/vision/fusion.cjs +1 -1
- package/dist/vision/fusion.cjs.map +1 -1
- package/dist/vision/fusion.js +28 -23
- package/dist/vision/fusion.js.map +1 -1
- package/dist/vision/index.cjs +1 -1
- package/dist/vision/index.cjs.map +1 -1
- package/dist/vision/index.js +20 -19
- package/dist/vision/index.js.map +1 -1
- package/dist/vision/io/image.cjs +1 -1
- package/dist/vision/io/image.cjs.map +1 -1
- package/dist/vision/io/image.js +55 -20
- package/dist/vision/io/image.js.map +1 -1
- package/dist/vision/postprocess/detection.cjs +1 -1
- package/dist/vision/postprocess/detection.cjs.map +1 -1
- package/dist/vision/postprocess/detection.js +42 -51
- package/dist/vision/postprocess/detection.js.map +1 -1
- package/dist/vision/postprocess/segmentation.cjs +1 -1
- package/dist/vision/postprocess/segmentation.cjs.map +1 -1
- package/dist/vision/postprocess/segmentation.js +45 -31
- package/dist/vision/postprocess/segmentation.js.map +1 -1
- package/dist/vision/preprocess/pipeline.cjs +1 -1
- package/dist/vision/preprocess/pipeline.cjs.map +1 -1
- package/dist/vision/preprocess/pipeline.js +24 -25
- package/dist/vision/preprocess/pipeline.js.map +1 -1
- package/dist/vision/tasks/base.cjs +1 -1
- package/dist/vision/tasks/base.cjs.map +1 -1
- package/dist/vision/tasks/base.js +22 -7
- package/dist/vision/tasks/base.js.map +1 -1
- package/dist/vision/tasks/classifier.cjs +1 -1
- package/dist/vision/tasks/classifier.cjs.map +1 -1
- package/dist/vision/tasks/classifier.js +64 -55
- package/dist/vision/tasks/classifier.js.map +1 -1
- package/dist/vision/tasks/detectClassify.cjs +1 -1
- package/dist/vision/tasks/detectClassify.cjs.map +1 -1
- package/dist/vision/tasks/detectClassify.js +80 -69
- package/dist/vision/tasks/detectClassify.js.map +1 -1
- package/dist/vision/tasks/detector.cjs +1 -1
- package/dist/vision/tasks/detector.cjs.map +1 -1
- package/dist/vision/tasks/detector.js +74 -68
- package/dist/vision/tasks/detector.js.map +1 -1
- package/dist/vision/tasks/segmenter.cjs +1 -1
- package/dist/vision/tasks/segmenter.cjs.map +1 -1
- package/dist/vision/tasks/segmenter.js +85 -62
- package/dist/vision/tasks/segmenter.js.map +1 -1
- package/dist/vision/types.cjs +1 -1
- package/dist/vision/types.cjs.map +1 -1
- package/dist/vision/types.js +24 -4
- package/dist/vision/types.js.map +1 -1
- package/dist/vision.cjs +1 -1
- package/dist/vision.d.ts +1153 -777
- package/dist/vision.js +22 -21
- package/package.json +2 -2
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../../_virtual/_rolldown/runtime.cjs"),t=require("./exceptions.cjs"),n=require("./
|
|
1
|
+
const e=require("../../_virtual/_rolldown/runtime.cjs"),t=require("./exceptions.cjs"),n=require("./dtypes.cjs"),r=require("./graph.cjs"),i=require("./metadata.cjs"),a=require("./providers.cjs");let o=require("onnxruntime-web");o=e.__toESM(o,1);var s=`ort_vision_sdk.graph_optimization`,c=`CAcSDm9ydC12aXNpb24tc2RrOjoKEAoBeBIBeSIISWRlbnRpdHkSBHdhcm1aDwoBeBIKCggIARIECgIIAWIPCgF5EgoKCAgBEgQKAggBQgQKABAN`,l=new Map;function u(e){let t=e.map(a.providerName).join(`,`),n=l.get(t);return n===void 0&&(n=(async()=>{try{let t=Uint8Array.from(atob(c),e=>e.charCodeAt(0));await(await o.InferenceSession.create(t,{executionProviders:e})).release()}catch{return}})(),l.set(t,n)),n}function d(){console.warn(`[@ort-vision-sdk/web] This model was pre-optimized by optimize_model, which targets the WASM backend; on WebGPU some of its fused nodes fall back to the CPU. For WebGPU, ship the original export instead.`)}var f=`ort_vision_sdk.quantization`;function p(e,t){if(t[`ort_vision_sdk.quantization`]===void 0)return[...e];let n=e.filter(e=>a.providerName(e)!==`webgpu`);return n.length===e.length?n:(console.warn(`[@ort-vision-sdk/web] This model was quantized by quantize_model (${t[f]}); ONNX Runtime Web's WebGPU backend cannot run its quantized operators, so it runs on WASM.`),n.length>0?n:[a.FALLBACK_PROVIDER])}var m=`ort-vision-sdk-models`;async function h(e,t){let n=t===null?null:await _(t);if(n!==null){let t=await n.match(e).catch(()=>void 0);if(t!==void 0)return new Uint8Array(await t.arrayBuffer())}try{let t=await fetch(e);if(!t.ok)return v(e,`HTTP ${t.status} ${t.statusText}`),e;let r=await t.arrayBuffer();return n!==null&&await g(n,e,r),new Uint8Array(r)}catch(t){return v(e,t.message),e}}async function g(e,t,n){try{await e.put(t,new Response(n))}catch{return}}async function _(e){if(typeof caches>`u`)return null;try{return await caches.open(e)}catch{return null}}function v(e,t){console.warn(`[@ort-vision-sdk/web] Could not fetch ${e} to read its metadata (${t}). Letting ONNX Runtime load it instead: the model will work, but its baked-in class names are unavailable, so labels fall back to class_0, class_1, ... Pass \`labels\` explicitly to name them.`)}function y(e,t){let n=e.filter(e=>!t.includes(e));n.length!==0&&console.warn(`This browser cannot offer the requested execution provider(s) ${JSON.stringify(n)}; the session will run on ${JSON.stringify(t)}. Inference still produces correct results, on the fallback provider.`)}function b(e){let t=i.readModelInputTypes(e),r={};for(let[e,i]of Object.entries(t))r[e]=n.tensorTypeFor(i);return r}function x(){console.warn(`Could not read any input type from this model's bytes; feeds will be built as float32. A half-precision model will fail at inference with 'Unexpected input data type'. Please report this with the model that produced it.`)}function S(e){let r=Object.entries(e).filter(([,e])=>e===`float16`);if(r.length===0||n.hasFloat16Array())return;let i=r.map(([e])=>e).join(`, `);throw new t.ModelLoadError(`This model declares half-precision input(s) [${i}], but this environment has no Float16Array, which ONNX Runtime requires for a float16 tensor. Use a float32 export of the model, or run in a browser that supports Float16Array.`)}var C=class e{_session;providers;_metadata;requestedProviders;_inputTypes;constructor(e,t,n,r,i={}){this._session=e,this.providers=t,this._metadata=n,this.requestedProviders=r,this._inputTypes=i}_runQueue=Promise.resolve();static async create(n,r={}){let s=a.resolveProviders(r.providers),c=r.readMetadata!==!1,l=r.cache===!0?m:r.cache?r.cache:null,f=typeof n==`string`&&(c||l!==null),g=a.detectProviders(s).then(e=>e.length>0?e:[a.FALLBACK_PROVIDER]),[_,v]=await Promise.all([g,f?h(n,l):Promise.resolve(n),f?g.then(u):Promise.resolve()]),C=c&&typeof v!=`string`?i.readModelMetadata(v):{},w=p(_,C),T=s.map(a.providerName),E=w.map(a.providerName);r.providers!==void 0&&r.providers.length>0&&y(T,_.map(a.providerName)),C[`ort_vision_sdk.graph_optimization`]!==void 0&&E.includes(`webgpu`)&&d();let D={...C[`ort_vision_sdk.graph_optimization`]!==void 0&&r.sessionOptions?.graphOptimizationLevel===void 0?{graphOptimizationLevel:`disabled`}:{},...r.sessionOptions??{},executionProviders:w},O=typeof v==`string`?{}:b(v);typeof v!=`string`&&Object.keys(O).length===0&&x(),S(O);let k;try{k=(typeof v==`string`||v instanceof Uint8Array,await o.InferenceSession.create(v,D))}catch(e){throw new t.ModelLoadError(`Failed to load ONNX model: ${e.message}`,{cause:e})}return new e(k,E,C,T,O)}get inputDtypes(){return this.inputNames.map(e=>this._inputTypes[e]??`float32`)}get inputDtype(){return this._inputTypes[this.inputName]??`float32`}get inputNames(){return this._session.inputNames}get inputName(){let e=this._session.inputNames[0];if(e===void 0)throw new t.InferenceError(`Model has no inputs.`);return e}get outputNames(){return this._session.outputNames}get inputShapes(){return r.declaredShapesFrom(this._session.inputMetadata)}get inputShape(){return this.inputShapes[0]??[]}get outputShapes(){return r.declaredShapesFrom(this._session.outputMetadata)}get outputShape(){return this.outputShapes[0]??[]}get metadata(){return this._metadata}async release(){await this._session.release().catch(()=>void 0)}get raw(){return this._session}run(e){let t=this._runQueue.then(()=>this._runNow(e));return this._runQueue=t.catch(()=>void 0),t}async _runNow(e){try{return await this._session.run(this._asDeclaredTypes(e))}catch(e){throw new t.InferenceError(`Inference failed: ${e.message}`,{cause:e})}}_asDeclaredTypes(e){let t=null;for(let[r,i]of Object.entries(e)){let a=this._inputTypes[r];if(a===void 0||a===i.type||!(i.data instanceof Float32Array))continue;let s=n.toFeedData(i.data,a);s!==i.data&&(t??={...e},t[r]=new o.Tensor(a,s,i.dims))}return t??e}};exports.DEFAULT_MODEL_CACHE=m,exports.GRAPH_OPTIMIZATION_KEY=s,exports.OrtSession=C,exports.QUANTIZATION_KEY=f;
|
|
2
2
|
//# sourceMappingURL=session.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.cjs","names":[],"sources":["../../../src/vision/core/session.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Thin wrapper around `onnxruntime-web` `InferenceSession` with typed metadata.\n */\n\nimport type * as ort from \"onnxruntime-web\";\nimport * as ortRuntime from \"onnxruntime-web\";\n\nimport { InferenceError, ModelLoadError } from \"./exceptions\";\nimport { type DeclaredShape, declaredShapesFrom } from \"./graph\";\nimport { readModelMetadata } from \"./metadata\";\nimport { FALLBACK_PROVIDER, detectProviders, resolveProviders } from \"./providers\";\n\n/** Anything `InferenceSession.create` accepts. */\nexport type ModelSource = string | ArrayBufferLike | Uint8Array;\n\n/**\n * Fetch a model URL as bytes so its metadata can be read.\n *\n * Falls back to the URL itself when the fetch fails, letting ORT try its own\n * load path: losing the metadata map is a downgrade, but failing to load a model\n * that ORT could have fetched would be a regression.\n *\n * @param url Where the `.onnx` lives.\n * @returns The model bytes, or the original URL when they could not be fetched.\n */\nasync function fetchModel(url: string): Promise<Uint8Array | string> {\n try {\n const response = await fetch(url);\n if (!response.ok) {\n warnMetadataUnavailable(url, `HTTP ${response.status} ${response.statusText}`);\n return url;\n }\n return new Uint8Array(await response.arrayBuffer());\n } catch (err) {\n warnMetadataUnavailable(url, (err as Error).message);\n return url;\n }\n}\n\n/**\n * Warn that a model's metadata could not be read, and say what that costs.\n *\n * The fallback itself is right — losing the metadata beats failing a load that\n * ORT could have completed on its own — but it used to be silent, and the\n * symptom it produces is remote from the cause: class names come back as\n * `class_0`, `class_1`, ... with nothing anywhere explaining why. Whoever hits\n * this needs to be told that passing `labels` is the way out.\n *\n * @param url The model URL that could not be fetched here.\n * @param reason What went wrong, as reported by `fetch`.\n */\nfunction warnMetadataUnavailable(url: string, reason: string): void {\n console.warn(\n `[@ort-vision-sdk/web] Could not fetch ${url} to read its metadata (${reason}). ` +\n \"Letting ONNX Runtime load it instead: the model will work, but its baked-in \" +\n \"class names are unavailable, so labels fall back to class_0, class_1, ... \" +\n \"Pass `labels` explicitly to name them.\",\n );\n}\n\nexport interface OrtSessionOptions {\n /**\n * Execution providers in preference order. `undefined` uses {@link DEFAULT_PROVIDERS}.\n *\n * Naming one explicitly also opts into a `console.warn` when this browser\n * cannot offer it, instead of falling back in silence.\n */\n readonly providers?: readonly string[];\n /** Optional ORT session options forwarded to `InferenceSession.create`. */\n readonly sessionOptions?: ort.InferenceSession.SessionOptions;\n /**\n * Whether to read the model's custom metadata map (`names`, `task`, `imgsz`).\n * Defaults to `true`.\n *\n * The runtime does not expose that map, so it is read from the file itself —\n * which means a URL model is fetched here and handed to ORT as bytes instead\n * of letting ORT fetch it. That is the same single download either way, and\n * it is what lets a task resolve its labels off the model. Set to `false` to\n * keep the URL path untouched and leave {@link OrtSession.metadata} empty.\n *\n * `false` is also the escape hatch when a device cannot afford the bytes: the\n * fetched buffer is dropped before ORT builds the graph (see\n * {@link OrtSession.create}), but ORT's own load path still keeps the model out\n * of reach of anything the SDK holds. A session built this way resolves its\n * input size from the graph as usual — only the class names are lost, so a\n * caller taking this route has to pass `labels` itself.\n */\n readonly readMetadata?: boolean;\n}\n\n/**\n * Warn when a provider named explicitly by the caller is not going to run.\n *\n * Only explicit requests are worth a warning. The default list exists precisely\n * so that falling from `webgpu` to `wasm` is the expected outcome — but a caller\n * who wrote `providers: [\"webgpu\"]` and lands on WASM has a page several times\n * slower than intended and nothing in the console to explain it.\n *\n * @param requested Providers that were asked for.\n * @param effective Providers that survived capability detection.\n */\nfunction warnOnDroppedProviders(requested: readonly string[], effective: readonly string[]): void {\n const dropped = requested.filter((provider) => !effective.includes(provider));\n if (dropped.length === 0) {\n return;\n }\n console.warn(\n `This browser cannot offer the requested execution provider(s) ${JSON.stringify(dropped)}; ` +\n `the session will run on ${JSON.stringify(effective)}. Inference still produces correct ` +\n \"results, on the fallback provider.\",\n );\n}\n\n/**\n * Wrap an ONNX Runtime Web `InferenceSession` with convenient metadata access.\n *\n * The wrapper exposes input/output names and the shapes the graph declares,\n * manages execution-provider selection, provides a typed {@link OrtSession.run}\n * method, and releases the native session through {@link OrtSession.release}.\n */\nexport class OrtSession {\n private constructor(\n private readonly _session: ort.InferenceSession,\n /**\n * Execution providers this session is expected to run on.\n *\n * The requested list narrowed to what this browser can actually offer — a\n * `webgpu` entry survives only where an adapter exists. Best-effort: ORT-Web\n * exposes no way to ask which provider a session ended up on, so an entry\n * here means \"not ruled out\", not \"confirmed\". See\n * {@link requestedProviders} for what was asked for.\n *\n * When nothing survives — a caller asking for `webgpu` alone on a device\n * without it — this falls back to {@link FALLBACK_PROVIDER}, which ORT-Web\n * can always run. Handing ORT the unsatisfiable list instead makes\n * `InferenceSession.create` reject with \"no available backend found\", so the\n * page gets no inference at all rather than the slow-but-working fallback\n * the `console.warn` describes. Measured in a real Chromium, where\n * `navigator.gpu` exists but yields no adapter.\n */\n public readonly providers: readonly string[],\n private readonly _metadata: Readonly<Record<string, string>>,\n /**\n * Execution providers that were asked for, after defaults were applied.\n *\n * Kept separate because ORT-Web falls back silently: a page that asks for\n * `webgpu` on a device without it runs on WASM and is told nothing.\n */\n public readonly requestedProviders: readonly string[],\n ) {}\n\n /**\n * Load an ONNX model into an ORT inference session.\n *\n * The metadata map is read **before** the session is built, and that order is\n * load-bearing on memory-constrained devices. ORT copies the model into its\n * WASM heap and then allocates the graph and the weights on top of that copy;\n * a `readModelMetadata` call placed after `InferenceSession.create` keeps the\n * JavaScript-side buffer reachable across the whole build, so a 5 MB model\n * costs 5 MB of JS heap plus 5 MB of WASM heap plus the weights at the same\n * instant. Reading first makes the buffer collectable as soon as ORT has copied\n * it — on a phone that was the difference between a session and\n * `Can't create a session. failed to allocate a buffer of size N`.\n *\n * @param model Either a URL string, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.\n * @param options Provider list, pass-through `SessionOptions`, and whether to\n * read the model's metadata map (see {@link OrtSessionOptions.readMetadata}).\n * @throws {@link ModelLoadError} if the model cannot be loaded.\n */\n static async create(model: ModelSource, options: OrtSessionOptions = {}): Promise<OrtSession> {\n const requested = resolveProviders(options.providers);\n const detected = await detectProviders(requested);\n const providers = detected.length > 0 ? detected : [FALLBACK_PROVIDER];\n if (options.providers !== undefined && options.providers.length > 0) {\n warnOnDroppedProviders(requested, providers);\n }\n const sessionOptions: ort.InferenceSession.SessionOptions = {\n ...(options.sessionOptions ?? {}),\n executionProviders:\n providers as ort.InferenceSession.SessionOptions[\"executionProviders\"],\n };\n const wantsMetadata = options.readMetadata !== false;\n const source = typeof model === \"string\" && wantsMetadata ? await fetchModel(model) : model;\n const metadata =\n wantsMetadata && typeof source !== \"string\" ? readModelMetadata(source) : {};\n\n let session: ort.InferenceSession;\n try {\n if (typeof source === \"string\") {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else if (source instanceof Uint8Array) {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else {\n session = await ortRuntime.InferenceSession.create(\n source as ArrayBuffer,\n sessionOptions,\n );\n }\n } catch (err) {\n throw new ModelLoadError(`Failed to load ONNX model: ${(err as Error).message}`, {\n cause: err,\n });\n }\n\n return new OrtSession(session, providers, metadata, requested);\n }\n\n /** Names of the model's inputs, in declaration order. */\n get inputNames(): readonly string[] {\n return this._session.inputNames;\n }\n\n /** Name of the first (and usually only) input. */\n get inputName(): string {\n const name = this._session.inputNames[0];\n if (name === undefined) {\n throw new InferenceError(\"Model has no inputs.\");\n }\n return name;\n }\n\n /** Names of the model's outputs, in declaration order. */\n get outputNames(): readonly string[] {\n return this._session.outputNames;\n }\n\n /**\n * Shapes the graph declares for its inputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Empty shapes mean the runtime\n * reported no metadata — either a non-tensor input, or an `onnxruntime-web`\n * older than 1.21, which predates input metadata.\n */\n get inputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.inputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first input, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get inputShape(): DeclaredShape {\n return this.inputShapes[0] ?? [];\n }\n\n /**\n * Shapes the graph declares for its outputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Reading them is how a task can\n * tell how many classes a head emits without being told.\n */\n get outputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.outputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first output, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get outputShape(): DeclaredShape {\n return this.outputShapes[0] ?? [];\n }\n\n /**\n * The model's custom metadata map — `names`, `task`, `imgsz`, ... for an\n * Ultralytics export.\n *\n * Read from the model's bytes at load time, since the runtime does not expose\n * it. Empty when the session was created with `readMetadata: false`, from a\n * URL that could not be fetched here, or from a model carrying no metadata.\n */\n get metadata(): Readonly<Record<string, string>> {\n return this._metadata;\n }\n\n /**\n * Release the native session and free its memory.\n *\n * Call it when a session is discarded while the page lives on — rebuilding a\n * task at a different input size, swapping in a newer model. A failure from\n * the runtime is ignored: a session being torn down has nothing left to fail\n * at, and the caller is already moving on.\n */\n async release(): Promise<void> {\n await this._session.release().catch(() => undefined);\n }\n\n /** The underlying `onnxruntime-web` session, for advanced use cases. */\n get raw(): ort.InferenceSession {\n return this._session;\n }\n\n /**\n * Run inference and return all outputs.\n *\n * @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.\n * @throws {@link InferenceError} if ORT raises any error during execution.\n */\n async run(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>> {\n try {\n const result = await this._session.run(feeds);\n return result as Record<string, ort.Tensor>;\n } catch (err) {\n throw new InferenceError(`Inference failed: ${(err as Error).message}`, { cause: err });\n }\n }\n}\n"],"mappings":"0NA0BA,eAAe,EAAW,EAA2C,CACjE,GAAI,CACA,IAAM,EAAW,MAAM,MAAM,CAAG,EAKhC,OAJK,EAAS,GAIP,IAAI,WAAW,MAAM,EAAS,YAAY,CAAC,GAH9C,EAAwB,EAAK,QAAQ,EAAS,OAAO,GAAG,EAAS,YA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Ac,UAAW,CAC7E,MAAO,CACX,CAAC,CACL,CAEA,OAAO,IAAI,EAAW,EAAS,EAAW,EAAU,CAAS,CACjE,CAGA,IAAI,YAAgC,CAChC,OAAO,KAAK,SAAS,UACzB,CAGA,IAAI,WAAoB,CACpB,IAAM,EAAO,KAAK,SAAS,WAAW,GACtC,GAAI,IAAS,IAAA,GACT,MAAM,IAAI,EAAA,eAAe,sBAAsB,EAEnD,OAAO,CACX,CAGA,IAAI,aAAiC,CACjC,OAAO,KAAK,SAAS,WACzB,CASA,IAAI,aAAwC,CACxC,OAAO,EAAA,mBACH,KAAK,SAAS,aAElB,CACJ,CAOA,IAAI,YAA4B,CAC5B,OAAO,KAAK,YAAY,IAAM,CAAC,CACnC,CAQA,IAAI,cAAyC,CACzC,OAAO,EAAA,mBACH,KAAK,SAAS,cAElB,CACJ,CAOA,IAAI,aAA6B,CAC7B,OAAO,KAAK,aAAa,IAAM,CAAC,CACpC,CAUA,IAAI,UAA6C,CAC7C,OAAO,KAAK,SAChB,CAUA,MAAM,SAAyB,CAC3B,MAAM,KAAK,SAAS,QAAQ,CAAC,CAAC,UAAY,IAAA,EAAS,CACvD,CAGA,IAAI,KAA4B,CAC5B,OAAO,KAAK,QAChB,CAQA,MAAM,IAAI,EAAwE,CAC9E,GAAI,CAEA,OAAO,MADc,KAAK,SAAS,IAAI,CAAK,CAEhD,OAAS,EAAK,CACV,MAAM,IAAI,EAAA,eAAe,qBAAsB,EAAc,UAAW,CAAE,MAAO,CAAI,CAAC,CAC1F,CACJ,CACJ"}
|
|
1
|
+
{"version":3,"file":"session.cjs","names":[],"sources":["../../../src/vision/core/session.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Thin wrapper around `onnxruntime-web` `InferenceSession` with typed metadata.\n */\n\nimport type * as ort from \"onnxruntime-web\";\nimport * as ortRuntime from \"onnxruntime-web\";\n\nimport { DEFAULT_TENSOR_TYPE, hasFloat16Array, tensorTypeFor, toFeedData } from \"./dtypes\";\nimport { InferenceError, ModelLoadError } from \"./exceptions\";\nimport { type DeclaredShape, declaredShapesFrom } from \"./graph\";\nimport { readModelInputTypes, readModelMetadata } from \"./metadata\";\nimport {\n FALLBACK_PROVIDER,\n type ProviderSpec,\n detectProviders,\n providerName,\n resolveProviders,\n} from \"./providers\";\n\n/** Anything `InferenceSession.create` accepts. */\nexport type ModelSource = string | ArrayBufferLike | Uint8Array;\n\n/**\n * Metadata key naming the level a model was graph-optimized at offline.\n *\n * Written by the Python SDK's `optimize_model`; both test suites pin the same\n * string, so a rename on one side fails. See {@link OrtSession.create} for what\n * the session does when it finds it.\n */\nexport const GRAPH_OPTIMIZATION_KEY = \"ort_vision_sdk.graph_optimization\";\n\n/**\n * An 84-byte ONNX model — one `Identity` on a float `[1]` — used to start the runtime.\n *\n * Generated with `onnx.helper` (opset 13, IR 7, producer `ort-vision-sdk`).\n * See {@link warmRuntime} for why it exists.\n */\nconst RUNTIME_PROBE_MODEL =\n \"CAcSDm9ydC12aXNpb24tc2RrOjoKEAoBeBIBeSIISWRlbnRpdHkSBHdhcm1aDwoBeBIKCggIARIECgIIAWIPCgF5EgoKCAgBEgQKAggBQgQKABAN\";\n\n/** Runtime start-ups already begun on this page, keyed by provider names. */\nconst runtimeWarmups = new Map<string, Promise<void>>();\n\n/**\n * Start ONNX Runtime's backends for a provider list, once per page.\n *\n * ORT downloads and compiles its WebAssembly binary — 12.8 MB for the plain\n * WASM build, 25.9 MB for the one carrying WebGPU — inside the first\n * `InferenceSession.create`. {@link OrtSession.create} only reaches that call\n * after it has downloaded the model, so on a real network the two largest\n * downloads of a page load ran one after the other. Creating and releasing a\n * session on a tiny model while the real one downloads lets them overlap; the\n * real `create` then finds the runtime ready.\n *\n * A failure here is swallowed: the real `create` runs against the same runtime\n * and reports the same problem with the right model in the message.\n *\n * @param specs Providers the real session will use.\n * @returns Settles when the runtime is up, or when starting it failed.\n */\nfunction warmRuntime(specs: readonly ProviderSpec[]): Promise<void> {\n const key = specs.map(providerName).join(\",\");\n let pending = runtimeWarmups.get(key);\n if (pending === undefined) {\n pending = (async () => {\n try {\n const probe = Uint8Array.from(atob(RUNTIME_PROBE_MODEL), (c) => c.charCodeAt(0));\n const session = await ortRuntime.InferenceSession.create(probe, {\n executionProviders:\n specs as ort.InferenceSession.SessionOptions[\"executionProviders\"],\n });\n await session.release();\n } catch {\n return;\n }\n })();\n runtimeWarmups.set(key, pending);\n }\n return pending;\n}\n\n/**\n * Warn that a model optimized offline is about to run on WebGPU.\n *\n * `optimize_model` fuses operators for the CPU, which is what the WASM backend\n * runs. On WebGPU some of those fused nodes have no GPU kernel: on a\n * YOLO11n-seg, ONNX Runtime placed 2 nodes on the CPU for the optimized file\n * and none for the original, so every inference pays GPU↔CPU copies. The SDK\n * cannot swap the file, so it says which one to ship.\n */\nfunction warnPreOptimizedOnWebGpu(): void {\n console.warn(\n \"[@ort-vision-sdk/web] This model was pre-optimized by optimize_model, which targets the \" +\n \"WASM backend; on WebGPU some of its fused nodes fall back to the CPU. For WebGPU, ship \" +\n \"the original export instead.\",\n );\n}\n\n/**\n * Metadata key the Python SDK's `quantize_model` writes on an INT8 model.\n *\n * Mirrored by `QUANTIZATION_KEY` in the Python SDK; both test suites pin the\n * string. See {@link keepQuantizedOffWebGpu} for what a session does with it.\n */\nexport const QUANTIZATION_KEY = \"ort_vision_sdk.quantization\";\n\n/**\n * Drop `webgpu` from the providers of a model `quantize_model` produced.\n *\n * ONNX Runtime Web's WebGPU `DequantizeLinear` rejects the quantized bias such\n * a model carries — `scale and zero-point inputs must have the same rank` —\n * so the first `predict()` fails; and a variant with the bias left in float\n * ran but returned different detections than WASM for the same file. The\n * model is fine on WASM, which is also where INT8 pays (52.7 ms vs 68.4 ms on\n * a YOLO11n-seg), so the session runs there and says so.\n *\n * @param specs Providers that survived detection.\n * @param metadata The model's metadata map.\n * @returns `specs` without `webgpu` when the model is marked quantized; the\n * WASM fallback when nothing else is left.\n */\nfunction keepQuantizedOffWebGpu(\n specs: readonly ProviderSpec[],\n metadata: Readonly<Record<string, string>>,\n): ProviderSpec[] {\n if (metadata[QUANTIZATION_KEY] === undefined) return [...specs];\n const kept = specs.filter((spec) => providerName(spec) !== \"webgpu\");\n if (kept.length === specs.length) return kept;\n console.warn(\n `[@ort-vision-sdk/web] This model was quantized by quantize_model (${metadata[QUANTIZATION_KEY]}); ` +\n \"ONNX Runtime Web's WebGPU backend cannot run its quantized operators, so it runs on WASM.\",\n );\n return kept.length > 0 ? kept : [FALLBACK_PROVIDER];\n}\n\n/** Cache Storage bucket used when {@link OrtSessionOptions.cache} is `true`. */\nexport const DEFAULT_MODEL_CACHE = \"ort-vision-sdk-models\";\n\n/**\n * Fetch a model URL as bytes so its metadata can be read.\n *\n * Falls back to the URL itself when the fetch fails, letting ORT try its own\n * load path: losing the metadata map is a downgrade, but failing to load a model\n * that ORT could have fetched would be a regression.\n *\n * @param url Where the `.onnx` lives.\n * @param cacheName Cache Storage bucket to read from and fill, or `null` to\n * always go to the network.\n * @returns The model bytes, or the original URL when they could not be fetched.\n */\nasync function fetchModel(url: string, cacheName: string | null): Promise<Uint8Array | string> {\n const cache = cacheName === null ? null : await openCache(cacheName);\n if (cache !== null) {\n const hit = await cache.match(url).catch(() => undefined);\n if (hit !== undefined) return new Uint8Array(await hit.arrayBuffer());\n }\n try {\n const response = await fetch(url);\n if (!response.ok) {\n warnMetadataUnavailable(url, `HTTP ${response.status} ${response.statusText}`);\n return url;\n }\n const buffer = await response.arrayBuffer();\n if (cache !== null) await storeModel(cache, url, buffer);\n return new Uint8Array(buffer);\n } catch (err) {\n warnMetadataUnavailable(url, (err as Error).message);\n return url;\n }\n}\n\n/**\n * Put a model's bytes into the cache, never failing the load over it.\n *\n * Stores a fresh `Response` built from bytes already read rather than a\n * `clone()` of the network response: one read, and nothing that can throw\n * synchronously outside the guard. Awaited on purpose — a `put` left running\n * would keep its copy of the model reachable while ORT builds the session, the\n * same doubled peak {@link OrtSession.create} orders its reads to avoid.\n *\n * @param cache The open bucket.\n * @param url Cache key.\n * @param buffer The model.\n */\nasync function storeModel(cache: Cache, url: string, buffer: ArrayBuffer): Promise<void> {\n try {\n await cache.put(url, new Response(buffer));\n } catch {\n return;\n }\n}\n\n/**\n * Open a Cache Storage bucket, or report that there is none to open.\n *\n * Cache Storage only exists in secure contexts (HTTPS, `localhost`), and a\n * browser may refuse it outright — private windows, blocked site data, an\n * exhausted quota. None of that is worth failing a model load over: the model\n * is fetched from the network as if caching had not been asked for. A failed\n * `put` is swallowed for the same reason — see {@link storeModel}.\n *\n * @param name Bucket name.\n * @returns The cache, or `null` when this environment cannot provide one.\n */\nasync function openCache(name: string): Promise<Cache | null> {\n if (typeof caches === \"undefined\") return null;\n try {\n return await caches.open(name);\n } catch {\n return null;\n }\n}\n\n/**\n * Warn that a model's metadata could not be read, and say what that costs.\n *\n * The fallback itself is right — losing the metadata beats failing a load that\n * ORT could have completed on its own — but it used to be silent, and the\n * symptom it produces is remote from the cause: class names come back as\n * `class_0`, `class_1`, ... with nothing anywhere explaining why. Whoever hits\n * this needs to be told that passing `labels` is the way out.\n *\n * @param url The model URL that could not be fetched here.\n * @param reason What went wrong, as reported by `fetch`.\n */\nfunction warnMetadataUnavailable(url: string, reason: string): void {\n console.warn(\n `[@ort-vision-sdk/web] Could not fetch ${url} to read its metadata (${reason}). ` +\n \"Letting ONNX Runtime load it instead: the model will work, but its baked-in \" +\n \"class names are unavailable, so labels fall back to class_0, class_1, ... \" +\n \"Pass `labels` explicitly to name them.\",\n );\n}\n\nexport interface OrtSessionOptions {\n /**\n * Execution providers in preference order. `undefined` uses {@link DEFAULT_PROVIDERS}.\n *\n * Each entry is a name (`\"webgpu\"`, `\"wasm\"`) or ORT's config object for that\n * provider, which passes provider options through untouched —\n * `{ name: \"webgpu\", preferredLayout: \"NHWC\" }`.\n *\n * Naming one explicitly also opts into a `console.warn` when this browser\n * cannot offer it, instead of falling back in silence.\n */\n readonly providers?: readonly ProviderSpec[];\n /**\n * Keep a URL model in the browser's Cache Storage between page loads.\n *\n * `true` uses the {@link DEFAULT_MODEL_CACHE} bucket; a string names the\n * bucket. A later `create` with the same URL reads the bytes from the cache\n * instead of the network — for a model of several megabytes, the bulk of\n * what a returning visitor waits for. Defaults to `false`.\n *\n * The URL is the cache key and nothing expires it: publish a changed model\n * under a new URL (`yolo.v2.onnx`, `?v=2`) or a new bucket name, or delete\n * the bucket with `caches.delete(name)`. Ignored for models passed as bytes,\n * and silently skipped where Cache Storage is unavailable (non-secure\n * contexts, blocked site data).\n */\n readonly cache?: boolean | string;\n /** Optional ORT session options forwarded to `InferenceSession.create`. */\n readonly sessionOptions?: ort.InferenceSession.SessionOptions;\n /**\n * Whether to read the model's custom metadata map (`names`, `task`, `imgsz`).\n * Defaults to `true`.\n *\n * The runtime does not expose that map, so it is read from the file itself —\n * which means a URL model is fetched here and handed to ORT as bytes instead\n * of letting ORT fetch it. That is the same single download either way, and\n * it is what lets a task resolve its labels off the model. Set to `false` to\n * keep the URL path untouched (unless {@link cache} is set, which needs the\n * bytes) and leave {@link OrtSession.metadata} empty.\n *\n * `false` is also the escape hatch when a device cannot afford the bytes: the\n * fetched buffer is dropped before ORT builds the graph (see\n * {@link OrtSession.create}), but ORT's own load path still keeps the model out\n * of reach of anything the SDK holds. A session built this way resolves its\n * input size from the graph as usual — only the class names are lost, so a\n * caller taking this route has to pass `labels` itself.\n */\n readonly readMetadata?: boolean;\n}\n\n/**\n * Warn when a provider named explicitly by the caller is not going to run.\n *\n * Only explicit requests are worth a warning. The default list exists precisely\n * so that falling from `webgpu` to `wasm` is the expected outcome — but a caller\n * who wrote `providers: [\"webgpu\"]` and lands on WASM has a page several times\n * slower than intended and nothing in the console to explain it.\n *\n * @param requested Providers that were asked for.\n * @param effective Providers that survived capability detection.\n */\nfunction warnOnDroppedProviders(requested: readonly string[], effective: readonly string[]): void {\n const dropped = requested.filter((provider) => !effective.includes(provider));\n if (dropped.length === 0) {\n return;\n }\n console.warn(\n `This browser cannot offer the requested execution provider(s) ${JSON.stringify(dropped)}; ` +\n `the session will run on ${JSON.stringify(effective)}. Inference still produces correct ` +\n \"results, on the fallback provider.\",\n );\n}\n\n/**\n * Name the tensor type of every graph input, straight from the model bytes.\n *\n * @param model The `.onnx` file contents.\n * @returns Input name → tensor type name. Empty when the file carries no\n * readable graph, which callers read as \"assume float32\".\n */\nfunction declaredInputTypes(model: Uint8Array | ArrayBufferLike): Readonly<Record<string, string>> {\n const declared = readModelInputTypes(model);\n const named: Record<string, string> = {};\n for (const [name, elemType] of Object.entries(declared)) {\n named[name] = tensorTypeFor(elemType);\n }\n return named;\n}\n\n/**\n * Warn when the model's bytes were in hand but no input type came back.\n *\n * Every ONNX graph declares at least one input, so an empty result from bytes\n * means the reader failed, not that the model said nothing. Without this the\n * failure is invisible: the session falls back to float32, which is right for\n * most models and wrong for exactly the half-precision ones this reader exists\n * to support — so it looks like nothing is broken until a `predict()` throws\n * `Unexpected input data type`. That is how a 1 MB ceiling on the graph\n * descent shipped in 0.9.0 and reached a consumer.\n */\nfunction warnOnUnreadableTypes(): void {\n console.warn(\n \"Could not read any input type from this model's bytes; feeds will be built as float32. \" +\n \"A half-precision model will fail at inference with 'Unexpected input data type'. \" +\n \"Please report this with the model that produced it.\",\n );\n}\n\n/**\n * Refuse a half-precision model in an environment that cannot feed one.\n *\n * ORT requires a real `Float16Array` for a `float16` tensor — the same bits in\n * a `Uint16Array` are rejected — and not every browser has one yet. Without\n * this check the session builds happily and the first `predict()` throws from\n * inside the preprocessing, which is both later and further from the cause.\n *\n * @param inputTypes Tensor type per input name.\n * @throws {@link ModelLoadError} when the graph wants half precision and the\n * runtime has no `Float16Array`.\n */\nfunction assertFeedableTypes(inputTypes: Readonly<Record<string, string>>): void {\n const half = Object.entries(inputTypes).filter(([, type]) => type === \"float16\");\n if (half.length === 0 || hasFloat16Array()) return;\n const names = half.map(([name]) => name).join(\", \");\n throw new ModelLoadError(\n `This model declares half-precision input(s) [${names}], but this environment has no ` +\n \"Float16Array, which ONNX Runtime requires for a float16 tensor. Use a float32 export \" +\n \"of the model, or run in a browser that supports Float16Array.\",\n );\n}\n\n/**\n * Wrap an ONNX Runtime Web `InferenceSession` with convenient metadata access.\n *\n * The wrapper exposes input/output names and the shapes the graph declares,\n * manages execution-provider selection, provides a typed {@link OrtSession.run}\n * method, and releases the native session through {@link OrtSession.release}.\n */\nexport class OrtSession {\n private constructor(\n private readonly _session: ort.InferenceSession,\n /**\n * Execution providers this session is expected to run on.\n *\n * The requested list narrowed to what this browser can actually offer — a\n * `webgpu` entry survives only where an adapter exists. Best-effort: ORT-Web\n * exposes no way to ask which provider a session ended up on, so an entry\n * here means \"not ruled out\", not \"confirmed\". See\n * {@link requestedProviders} for what was asked for.\n *\n * When nothing survives — a caller asking for `webgpu` alone on a device\n * without it — this falls back to {@link FALLBACK_PROVIDER}, which ORT-Web\n * can always run. Handing ORT the unsatisfiable list instead makes\n * `InferenceSession.create` reject with \"no available backend found\", so the\n * page gets no inference at all rather than the slow-but-working fallback\n * the `console.warn` describes. Measured in a real Chromium, where\n * `navigator.gpu` exists but yields no adapter.\n */\n public readonly providers: readonly string[],\n private readonly _metadata: Readonly<Record<string, string>>,\n /**\n * Execution providers that were asked for, after defaults were applied.\n *\n * Kept separate because ORT-Web falls back silently: a page that asks for\n * `webgpu` on a device without it runs on WASM and is told nothing.\n */\n public readonly requestedProviders: readonly string[],\n /**\n * Tensor type each input declares, keyed by input name.\n *\n * Read from the model file, not from the session: `inputMetadata` is\n * `undefined` on `onnxruntime-web` 1.20.1. Empty when the bytes were never\n * in hand — a URL loaded with `readMetadata: false` — in which case every\n * feed is built as float32, the behaviour before this existed.\n */\n private readonly _inputTypes: Readonly<Record<string, string>> = {},\n ) {}\n\n /**\n * Tail of the run queue: settles when the latest queued run has finished.\n *\n * See {@link run} for why runs are serialized.\n */\n private _runQueue: Promise<unknown> = Promise.resolve();\n\n /**\n * Load an ONNX model into an ORT inference session.\n *\n * The metadata map is read **before** the session is built, and that order is\n * load-bearing on memory-constrained devices. ORT copies the model into its\n * WASM heap and then allocates the graph and the weights on top of that copy;\n * a `readModelMetadata` call placed after `InferenceSession.create` keeps the\n * JavaScript-side buffer reachable across the whole build, so a 5 MB model\n * costs 5 MB of JS heap plus 5 MB of WASM heap plus the weights at the same\n * instant. Reading first makes the buffer collectable as soon as ORT has copied\n * it — on a phone that was the difference between a session and\n * `Can't create a session. failed to allocate a buffer of size N`.\n *\n * The provider probe and the model download run concurrently: neither\n * depends on the other, and on a real GPU `requestAdapter()` is not free.\n * When the model is downloaded here, ORT's runtime is started alongside the\n * download too — see {@link warmRuntime}.\n *\n * A model whose metadata carries {@link GRAPH_OPTIMIZATION_KEY} was already\n * optimized offline by the Python SDK's `optimize_model`, so it is loaded\n * with `graphOptimizationLevel: \"disabled\"` instead of paying the optimizer a\n * second time — measured on a YOLO11n-seg under WASM, 29 ms of session\n * creation down to 12 ms with unchanged inference time. An explicit\n * `graphOptimizationLevel` in `sessionOptions` always wins. The mark is read\n * with the rest of the metadata, so `readMetadata: false` skips it too.\n *\n * @param model Either a URL string, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.\n * @param options Provider list, pass-through `SessionOptions`, and whether to\n * read the model's metadata map (see {@link OrtSessionOptions.readMetadata}).\n * @throws {@link ModelLoadError} if the model cannot be loaded.\n */\n static async create(model: ModelSource, options: OrtSessionOptions = {}): Promise<OrtSession> {\n const requested = resolveProviders(options.providers);\n const wantsMetadata = options.readMetadata !== false;\n const cacheName =\n options.cache === true ? DEFAULT_MODEL_CACHE : options.cache ? options.cache : null;\n const shouldFetch = typeof model === \"string\" && (wantsMetadata || cacheName !== null);\n const detection = detectProviders(requested).then((detected) =>\n detected.length > 0 ? detected : [FALLBACK_PROVIDER],\n );\n const [detected, source] = await Promise.all([\n detection,\n shouldFetch ? fetchModel(model, cacheName) : Promise.resolve(model),\n shouldFetch ? detection.then(warmRuntime) : Promise.resolve(),\n ]);\n const metadata =\n wantsMetadata && typeof source !== \"string\" ? readModelMetadata(source) : {};\n const specs = keepQuantizedOffWebGpu(detected, metadata);\n const requestedNames = requested.map(providerName);\n const providers = specs.map(providerName);\n if (options.providers !== undefined && options.providers.length > 0) {\n warnOnDroppedProviders(requestedNames, detected.map(providerName));\n }\n if (metadata[GRAPH_OPTIMIZATION_KEY] !== undefined && providers.includes(\"webgpu\")) {\n warnPreOptimizedOnWebGpu();\n }\n const preOptimized =\n metadata[GRAPH_OPTIMIZATION_KEY] !== undefined &&\n options.sessionOptions?.graphOptimizationLevel === undefined;\n const sessionOptions: ort.InferenceSession.SessionOptions = {\n ...(preOptimized ? { graphOptimizationLevel: \"disabled\" as const } : {}),\n ...(options.sessionOptions ?? {}),\n executionProviders: specs as ort.InferenceSession.SessionOptions[\"executionProviders\"],\n };\n const inputTypes = typeof source !== \"string\" ? declaredInputTypes(source) : {};\n if (typeof source !== \"string\" && Object.keys(inputTypes).length === 0) {\n warnOnUnreadableTypes();\n }\n assertFeedableTypes(inputTypes);\n\n let session: ort.InferenceSession;\n try {\n if (typeof source === \"string\") {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else if (source instanceof Uint8Array) {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else {\n session = await ortRuntime.InferenceSession.create(\n source as ArrayBuffer,\n sessionOptions,\n );\n }\n } catch (err) {\n throw new ModelLoadError(`Failed to load ONNX model: ${(err as Error).message}`, {\n cause: err,\n });\n }\n\n return new OrtSession(session, providers, metadata, requestedNames, inputTypes);\n }\n\n /**\n * Tensor type each input declares, in declaration order.\n *\n * `\"float32\"` for a normal export, `\"float16\"` for one exported with\n * `half=True`. ORT matches a feed's type against this exactly, so the tasks\n * read it and convert at the feed boundary.\n */\n get inputDtypes(): readonly string[] {\n return this.inputNames.map((name) => this._inputTypes[name] ?? DEFAULT_TENSOR_TYPE);\n }\n\n /** Tensor type the first input declares. */\n get inputDtype(): string {\n return this._inputTypes[this.inputName] ?? DEFAULT_TENSOR_TYPE;\n }\n\n /** Names of the model's inputs, in declaration order. */\n get inputNames(): readonly string[] {\n return this._session.inputNames;\n }\n\n /** Name of the first (and usually only) input. */\n get inputName(): string {\n const name = this._session.inputNames[0];\n if (name === undefined) {\n throw new InferenceError(\"Model has no inputs.\");\n }\n return name;\n }\n\n /** Names of the model's outputs, in declaration order. */\n get outputNames(): readonly string[] {\n return this._session.outputNames;\n }\n\n /**\n * Shapes the graph declares for its inputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Empty shapes mean the runtime\n * reported no metadata — either a non-tensor input, or an `onnxruntime-web`\n * older than 1.21, which predates input metadata.\n */\n get inputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.inputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first input, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get inputShape(): DeclaredShape {\n return this.inputShapes[0] ?? [];\n }\n\n /**\n * Shapes the graph declares for its outputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Reading them is how a task can\n * tell how many classes a head emits without being told.\n */\n get outputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.outputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first output, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get outputShape(): DeclaredShape {\n return this.outputShapes[0] ?? [];\n }\n\n /**\n * The model's custom metadata map — `names`, `task`, `imgsz`, ... for an\n * Ultralytics export.\n *\n * Read from the model's bytes at load time, since the runtime does not expose\n * it. Empty when the session was created with `readMetadata: false`, from a\n * URL that could not be fetched here, or from a model carrying no metadata.\n */\n get metadata(): Readonly<Record<string, string>> {\n return this._metadata;\n }\n\n /**\n * Release the native session and free its memory.\n *\n * Call it when a session is discarded while the page lives on — rebuilding a\n * task at a different input size, swapping in a newer model. A failure from\n * the runtime is ignored: a session being torn down has nothing left to fail\n * at, and the caller is already moving on.\n */\n async release(): Promise<void> {\n await this._session.release().catch(() => undefined);\n }\n\n /** The underlying `onnxruntime-web` session, for advanced use cases. */\n get raw(): ort.InferenceSession {\n return this._session;\n }\n\n /**\n * Run inference and return all outputs.\n *\n * Runs on one session are serialized. ONNX Runtime Web refuses a second\n * `run` while one is in flight — it rejects with `Session already started` —\n * so two overlapping `predict()` calls on the same task used to fail. Queued\n * here, they overlap where they can: while one run executes (in a worker,\n * with `env.wasm.proxy`, or on the GPU), the next `predict()` decodes and\n * preprocesses its frame and only waits for its turn at the runtime. A run\n * that fails does not block the ones behind it.\n *\n * @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.\n * @throws {@link InferenceError} if ORT raises any error during execution.\n */\n run(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>> {\n const turn = this._runQueue.then(() => this._runNow(feeds));\n this._runQueue = turn.catch(() => undefined);\n return turn;\n }\n\n /**\n * Execute one run immediately; only {@link run}'s queue calls this.\n *\n * @param feeds Map of input name to `ort.Tensor`.\n * @throws {@link InferenceError} if ORT raises any error during execution.\n */\n private async _runNow(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>> {\n try {\n const result = await this._session.run(this._asDeclaredTypes(feeds));\n return result as Record<string, ort.Tensor>;\n } catch (err) {\n throw new InferenceError(`Inference failed: ${(err as Error).message}`, { cause: err });\n }\n }\n\n /**\n * Rebuild any feed whose type does not match what its input declares.\n *\n * ORT matches feed types against the graph exactly: a float32 tensor against\n * a `half=True` export fails the run with `Unexpected input data type`. Every\n * preprocessing path in this SDK produces `Float32Array`, on purpose — the\n * normalization arithmetic belongs in single precision — so the conversion\n * happens here, once, at the only boundary all of them pass through. A feed\n * that already carries the declared type is handed on untouched, so a caller\n * building its own half tensor pays nothing.\n *\n * @param feeds The tensors to run with.\n * @returns The same mapping, with mismatched float32 feeds converted.\n */\n private _asDeclaredTypes(feeds: Record<string, ort.Tensor>): Record<string, ort.Tensor> {\n let converted: Record<string, ort.Tensor> | null = null;\n for (const [name, tensor] of Object.entries(feeds)) {\n const declared = this._inputTypes[name];\n if (declared === undefined || declared === tensor.type) continue;\n if (!(tensor.data instanceof Float32Array)) continue;\n const data = toFeedData(tensor.data, declared);\n if (data === tensor.data) continue;\n converted ??= { ...feeds };\n converted[name] = new ortRuntime.Tensor(\n declared as \"float16\",\n data as unknown as Float32Array,\n tensor.dims as number[],\n );\n }\n return converted ?? feeds;\n }\n}\n"],"mappings":"oPA8BA,IAAa,EAAyB,oCAQhC,EACF,mHAGE,EAAiB,IAAI,IAmB3B,SAAS,EAAY,EAA+C,CAChE,IAAM,EAAM,EAAM,IAAI,EAAA,YAAY,CAAC,CAAC,KAAK,GAAG,EACxC,EAAU,EAAe,IAAI,CAAG,EAgBpC,OAfI,IAAY,IAAA,KACZ,GAAW,SAAY,CACnB,GAAI,CACA,IAAM,EAAQ,WAAW,KAAK,KAAK,CAAmB,EAAI,GAAM,EAAE,WAAW,CAAC,CAAC,EAK/E,MAAM,MAJgB,EAAW,iBAAiB,OAAO,EAAO,CAC5D,mBACI,CACR,CAAC,EAAA,CACa,QAAQ,CAC1B,MAAQ,CACJ,MACJ,CACJ,EAAA,CAAG,EACH,EAAe,IAAI,EAAK,CAAO,GAE5B,CACX,CAWA,SAAS,GAAiC,CACtC,QAAQ,KACJ,6MAGJ,CACJ,CAQA,IAAa,EAAmB,8BAiBhC,SAAS,EACL,EACA,EACc,CACd,GAAI,EAAA,iCAA+B,IAAA,GAAW,MAAO,CAAC,GAAG,CAAK,EAC9D,IAAM,EAAO,EAAM,OAAQ,GAAS,EAAA,aAAa,CAAI,IAAM,QAAQ,EAMnE,OALI,EAAK,SAAW,EAAM,OAAe,GACzC,QAAQ,KACJ,qEAAqE,EAAS,GAAkB,6FAEpG,EACO,EAAK,OAAS,EAAI,EAAO,CAAC,EAAA,iBAAiB,EACtD,CAGA,IAAa,EAAsB,wBAcnC,eAAe,EAAW,EAA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,IACX,EACJ,CACA,OAAO,GAAa,CACxB,CACJ"}
|
|
@@ -1,45 +1,119 @@
|
|
|
1
1
|
import { InferenceError as e, ModelLoadError as t } from "./exceptions.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
2
|
+
import { hasFloat16Array as n, tensorTypeFor as r, toFeedData as i } from "./dtypes.js";
|
|
3
|
+
import { declaredShapesFrom as a } from "./graph.js";
|
|
4
|
+
import { readModelInputTypes as o, readModelMetadata as s } from "./metadata.js";
|
|
5
|
+
import { FALLBACK_PROVIDER as c, detectProviders as l, providerName as u, resolveProviders as d } from "./providers.js";
|
|
6
|
+
import * as f from "onnxruntime-web";
|
|
6
7
|
//#region src/vision/core/session.ts
|
|
7
|
-
|
|
8
|
+
var p = "ort_vision_sdk.graph_optimization", m = "CAcSDm9ydC12aXNpb24tc2RrOjoKEAoBeBIBeSIISWRlbnRpdHkSBHdhcm1aDwoBeBIKCggIARIECgIIAWIPCgF5EgoKCAgBEgQKAggBQgQKABAN", h = /* @__PURE__ */ new Map();
|
|
9
|
+
function g(e) {
|
|
10
|
+
let t = e.map(u).join(","), n = h.get(t);
|
|
11
|
+
return n === void 0 && (n = (async () => {
|
|
12
|
+
try {
|
|
13
|
+
let t = Uint8Array.from(atob(m), (e) => e.charCodeAt(0));
|
|
14
|
+
await (await f.InferenceSession.create(t, { executionProviders: e })).release();
|
|
15
|
+
} catch {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
})(), h.set(t, n)), n;
|
|
19
|
+
}
|
|
20
|
+
function _() {
|
|
21
|
+
console.warn("[@ort-vision-sdk/web] This model was pre-optimized by optimize_model, which targets the WASM backend; on WebGPU some of its fused nodes fall back to the CPU. For WebGPU, ship the original export instead.");
|
|
22
|
+
}
|
|
23
|
+
var v = "ort_vision_sdk.quantization";
|
|
24
|
+
function y(e, t) {
|
|
25
|
+
if (t["ort_vision_sdk.quantization"] === void 0) return [...e];
|
|
26
|
+
let n = e.filter((e) => u(e) !== "webgpu");
|
|
27
|
+
return n.length === e.length ? n : (console.warn(`[@ort-vision-sdk/web] This model was quantized by quantize_model (${t[v]}); ONNX Runtime Web's WebGPU backend cannot run its quantized operators, so it runs on WASM.`), n.length > 0 ? n : [c]);
|
|
28
|
+
}
|
|
29
|
+
var b = "ort-vision-sdk-models";
|
|
30
|
+
async function x(e, t) {
|
|
31
|
+
let n = t === null ? null : await C(t);
|
|
32
|
+
if (n !== null) {
|
|
33
|
+
let t = await n.match(e).catch(() => void 0);
|
|
34
|
+
if (t !== void 0) return new Uint8Array(await t.arrayBuffer());
|
|
35
|
+
}
|
|
8
36
|
try {
|
|
9
37
|
let t = await fetch(e);
|
|
10
|
-
|
|
38
|
+
if (!t.ok) return w(e, `HTTP ${t.status} ${t.statusText}`), e;
|
|
39
|
+
let r = await t.arrayBuffer();
|
|
40
|
+
return n !== null && await S(n, e, r), new Uint8Array(r);
|
|
11
41
|
} catch (t) {
|
|
12
|
-
return
|
|
42
|
+
return w(e, t.message), e;
|
|
13
43
|
}
|
|
14
44
|
}
|
|
15
|
-
function
|
|
45
|
+
async function S(e, t, n) {
|
|
46
|
+
try {
|
|
47
|
+
await e.put(t, new Response(n));
|
|
48
|
+
} catch {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function C(e) {
|
|
53
|
+
if (typeof caches > "u") return null;
|
|
54
|
+
try {
|
|
55
|
+
return await caches.open(e);
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function w(e, t) {
|
|
16
61
|
console.warn(`[@ort-vision-sdk/web] Could not fetch ${e} to read its metadata (${t}). Letting ONNX Runtime load it instead: the model will work, but its baked-in class names are unavailable, so labels fall back to class_0, class_1, ... Pass \`labels\` explicitly to name them.`);
|
|
17
62
|
}
|
|
18
|
-
function
|
|
63
|
+
function T(e, t) {
|
|
19
64
|
let n = e.filter((e) => !t.includes(e));
|
|
20
65
|
n.length !== 0 && console.warn(`This browser cannot offer the requested execution provider(s) ${JSON.stringify(n)}; the session will run on ${JSON.stringify(t)}. Inference still produces correct results, on the fallback provider.`);
|
|
21
66
|
}
|
|
22
|
-
|
|
67
|
+
function E(e) {
|
|
68
|
+
let t = o(e), n = {};
|
|
69
|
+
for (let [e, i] of Object.entries(t)) n[e] = r(i);
|
|
70
|
+
return n;
|
|
71
|
+
}
|
|
72
|
+
function D() {
|
|
73
|
+
console.warn("Could not read any input type from this model's bytes; feeds will be built as float32. A half-precision model will fail at inference with 'Unexpected input data type'. Please report this with the model that produced it.");
|
|
74
|
+
}
|
|
75
|
+
function O(e) {
|
|
76
|
+
let r = Object.entries(e).filter(([, e]) => e === "float16");
|
|
77
|
+
if (r.length === 0 || n()) return;
|
|
78
|
+
let i = r.map(([e]) => e).join(", ");
|
|
79
|
+
throw new t(`This model declares half-precision input(s) [${i}], but this environment has no Float16Array, which ONNX Runtime requires for a float16 tensor. Use a float32 export of the model, or run in a browser that supports Float16Array.`);
|
|
80
|
+
}
|
|
81
|
+
var k = class n {
|
|
23
82
|
_session;
|
|
24
83
|
providers;
|
|
25
84
|
_metadata;
|
|
26
85
|
requestedProviders;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
let m =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
86
|
+
_inputTypes;
|
|
87
|
+
constructor(e, t, n, r, i = {}) {
|
|
88
|
+
this._session = e, this.providers = t, this._metadata = n, this.requestedProviders = r, this._inputTypes = i;
|
|
89
|
+
}
|
|
90
|
+
_runQueue = Promise.resolve();
|
|
91
|
+
static async create(e, r = {}) {
|
|
92
|
+
let i = d(r.providers), a = r.readMetadata !== !1, o = r.cache === !0 ? b : r.cache ? r.cache : null, p = typeof e == "string" && (a || o !== null), m = l(i).then((e) => e.length > 0 ? e : [c]), [h, v] = await Promise.all([
|
|
93
|
+
m,
|
|
94
|
+
p ? x(e, o) : Promise.resolve(e),
|
|
95
|
+
p ? m.then(g) : Promise.resolve()
|
|
96
|
+
]), S = a && typeof v != "string" ? s(v) : {}, C = y(h, S), w = i.map(u), k = C.map(u);
|
|
97
|
+
r.providers !== void 0 && r.providers.length > 0 && T(w, h.map(u)), S["ort_vision_sdk.graph_optimization"] !== void 0 && k.includes("webgpu") && _();
|
|
98
|
+
let A = {
|
|
99
|
+
...S["ort_vision_sdk.graph_optimization"] !== void 0 && r.sessionOptions?.graphOptimizationLevel === void 0 ? { graphOptimizationLevel: "disabled" } : {},
|
|
100
|
+
...r.sessionOptions ?? {},
|
|
101
|
+
executionProviders: C
|
|
102
|
+
}, j = typeof v == "string" ? {} : E(v);
|
|
103
|
+
typeof v != "string" && Object.keys(j).length === 0 && D(), O(j);
|
|
104
|
+
let M;
|
|
37
105
|
try {
|
|
38
|
-
|
|
106
|
+
M = (typeof v == "string" || v instanceof Uint8Array, await f.InferenceSession.create(v, A));
|
|
39
107
|
} catch (e) {
|
|
40
108
|
throw new t(`Failed to load ONNX model: ${e.message}`, { cause: e });
|
|
41
109
|
}
|
|
42
|
-
return new
|
|
110
|
+
return new n(M, k, S, w, j);
|
|
111
|
+
}
|
|
112
|
+
get inputDtypes() {
|
|
113
|
+
return this.inputNames.map((e) => this._inputTypes[e] ?? "float32");
|
|
114
|
+
}
|
|
115
|
+
get inputDtype() {
|
|
116
|
+
return this._inputTypes[this.inputName] ?? "float32";
|
|
43
117
|
}
|
|
44
118
|
get inputNames() {
|
|
45
119
|
return this._session.inputNames;
|
|
@@ -53,13 +127,13 @@ var d = class l {
|
|
|
53
127
|
return this._session.outputNames;
|
|
54
128
|
}
|
|
55
129
|
get inputShapes() {
|
|
56
|
-
return
|
|
130
|
+
return a(this._session.inputMetadata);
|
|
57
131
|
}
|
|
58
132
|
get inputShape() {
|
|
59
133
|
return this.inputShapes[0] ?? [];
|
|
60
134
|
}
|
|
61
135
|
get outputShapes() {
|
|
62
|
-
return
|
|
136
|
+
return a(this._session.outputMetadata);
|
|
63
137
|
}
|
|
64
138
|
get outputShape() {
|
|
65
139
|
return this.outputShapes[0] ?? [];
|
|
@@ -73,15 +147,29 @@ var d = class l {
|
|
|
73
147
|
get raw() {
|
|
74
148
|
return this._session;
|
|
75
149
|
}
|
|
76
|
-
|
|
150
|
+
run(e) {
|
|
151
|
+
let t = this._runQueue.then(() => this._runNow(e));
|
|
152
|
+
return this._runQueue = t.catch(() => void 0), t;
|
|
153
|
+
}
|
|
154
|
+
async _runNow(t) {
|
|
77
155
|
try {
|
|
78
|
-
return await this._session.run(t);
|
|
156
|
+
return await this._session.run(this._asDeclaredTypes(t));
|
|
79
157
|
} catch (t) {
|
|
80
158
|
throw new e(`Inference failed: ${t.message}`, { cause: t });
|
|
81
159
|
}
|
|
82
160
|
}
|
|
161
|
+
_asDeclaredTypes(e) {
|
|
162
|
+
let t = null;
|
|
163
|
+
for (let [n, r] of Object.entries(e)) {
|
|
164
|
+
let a = this._inputTypes[n];
|
|
165
|
+
if (a === void 0 || a === r.type || !(r.data instanceof Float32Array)) continue;
|
|
166
|
+
let o = i(r.data, a);
|
|
167
|
+
o !== r.data && (t ??= { ...e }, t[n] = new f.Tensor(a, o, r.dims));
|
|
168
|
+
}
|
|
169
|
+
return t ?? e;
|
|
170
|
+
}
|
|
83
171
|
};
|
|
84
172
|
//#endregion
|
|
85
|
-
export {
|
|
173
|
+
export { b as DEFAULT_MODEL_CACHE, p as GRAPH_OPTIMIZATION_KEY, k as OrtSession, v as QUANTIZATION_KEY };
|
|
86
174
|
|
|
87
175
|
//# sourceMappingURL=session.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","names":[],"sources":["../../../src/vision/core/session.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Thin wrapper around `onnxruntime-web` `InferenceSession` with typed metadata.\n */\n\nimport type * as ort from \"onnxruntime-web\";\nimport * as ortRuntime from \"onnxruntime-web\";\n\nimport { InferenceError, ModelLoadError } from \"./exceptions\";\nimport { type DeclaredShape, declaredShapesFrom } from \"./graph\";\nimport { readModelMetadata } from \"./metadata\";\nimport { FALLBACK_PROVIDER, detectProviders, resolveProviders } from \"./providers\";\n\n/** Anything `InferenceSession.create` accepts. */\nexport type ModelSource = string | ArrayBufferLike | Uint8Array;\n\n/**\n * Fetch a model URL as bytes so its metadata can be read.\n *\n * Falls back to the URL itself when the fetch fails, letting ORT try its own\n * load path: losing the metadata map is a downgrade, but failing to load a model\n * that ORT could have fetched would be a regression.\n *\n * @param url Where the `.onnx` lives.\n * @returns The model bytes, or the original URL when they could not be fetched.\n */\nasync function fetchModel(url: string): Promise<Uint8Array | string> {\n try {\n const response = await fetch(url);\n if (!response.ok) {\n warnMetadataUnavailable(url, `HTTP ${response.status} ${response.statusText}`);\n return url;\n }\n return new Uint8Array(await response.arrayBuffer());\n } catch (err) {\n warnMetadataUnavailable(url, (err as Error).message);\n return url;\n }\n}\n\n/**\n * Warn that a model's metadata could not be read, and say what that costs.\n *\n * The fallback itself is right — losing the metadata beats failing a load that\n * ORT could have completed on its own — but it used to be silent, and the\n * symptom it produces is remote from the cause: class names come back as\n * `class_0`, `class_1`, ... with nothing anywhere explaining why. Whoever hits\n * this needs to be told that passing `labels` is the way out.\n *\n * @param url The model URL that could not be fetched here.\n * @param reason What went wrong, as reported by `fetch`.\n */\nfunction warnMetadataUnavailable(url: string, reason: string): void {\n console.warn(\n `[@ort-vision-sdk/web] Could not fetch ${url} to read its metadata (${reason}). ` +\n \"Letting ONNX Runtime load it instead: the model will work, but its baked-in \" +\n \"class names are unavailable, so labels fall back to class_0, class_1, ... \" +\n \"Pass `labels` explicitly to name them.\",\n );\n}\n\nexport interface OrtSessionOptions {\n /**\n * Execution providers in preference order. `undefined` uses {@link DEFAULT_PROVIDERS}.\n *\n * Naming one explicitly also opts into a `console.warn` when this browser\n * cannot offer it, instead of falling back in silence.\n */\n readonly providers?: readonly string[];\n /** Optional ORT session options forwarded to `InferenceSession.create`. */\n readonly sessionOptions?: ort.InferenceSession.SessionOptions;\n /**\n * Whether to read the model's custom metadata map (`names`, `task`, `imgsz`).\n * Defaults to `true`.\n *\n * The runtime does not expose that map, so it is read from the file itself —\n * which means a URL model is fetched here and handed to ORT as bytes instead\n * of letting ORT fetch it. That is the same single download either way, and\n * it is what lets a task resolve its labels off the model. Set to `false` to\n * keep the URL path untouched and leave {@link OrtSession.metadata} empty.\n *\n * `false` is also the escape hatch when a device cannot afford the bytes: the\n * fetched buffer is dropped before ORT builds the graph (see\n * {@link OrtSession.create}), but ORT's own load path still keeps the model out\n * of reach of anything the SDK holds. A session built this way resolves its\n * input size from the graph as usual — only the class names are lost, so a\n * caller taking this route has to pass `labels` itself.\n */\n readonly readMetadata?: boolean;\n}\n\n/**\n * Warn when a provider named explicitly by the caller is not going to run.\n *\n * Only explicit requests are worth a warning. The default list exists precisely\n * so that falling from `webgpu` to `wasm` is the expected outcome — but a caller\n * who wrote `providers: [\"webgpu\"]` and lands on WASM has a page several times\n * slower than intended and nothing in the console to explain it.\n *\n * @param requested Providers that were asked for.\n * @param effective Providers that survived capability detection.\n */\nfunction warnOnDroppedProviders(requested: readonly string[], effective: readonly string[]): void {\n const dropped = requested.filter((provider) => !effective.includes(provider));\n if (dropped.length === 0) {\n return;\n }\n console.warn(\n `This browser cannot offer the requested execution provider(s) ${JSON.stringify(dropped)}; ` +\n `the session will run on ${JSON.stringify(effective)}. Inference still produces correct ` +\n \"results, on the fallback provider.\",\n );\n}\n\n/**\n * Wrap an ONNX Runtime Web `InferenceSession` with convenient metadata access.\n *\n * The wrapper exposes input/output names and the shapes the graph declares,\n * manages execution-provider selection, provides a typed {@link OrtSession.run}\n * method, and releases the native session through {@link OrtSession.release}.\n */\nexport class OrtSession {\n private constructor(\n private readonly _session: ort.InferenceSession,\n /**\n * Execution providers this session is expected to run on.\n *\n * The requested list narrowed to what this browser can actually offer — a\n * `webgpu` entry survives only where an adapter exists. Best-effort: ORT-Web\n * exposes no way to ask which provider a session ended up on, so an entry\n * here means \"not ruled out\", not \"confirmed\". See\n * {@link requestedProviders} for what was asked for.\n *\n * When nothing survives — a caller asking for `webgpu` alone on a device\n * without it — this falls back to {@link FALLBACK_PROVIDER}, which ORT-Web\n * can always run. Handing ORT the unsatisfiable list instead makes\n * `InferenceSession.create` reject with \"no available backend found\", so the\n * page gets no inference at all rather than the slow-but-working fallback\n * the `console.warn` describes. Measured in a real Chromium, where\n * `navigator.gpu` exists but yields no adapter.\n */\n public readonly providers: readonly string[],\n private readonly _metadata: Readonly<Record<string, string>>,\n /**\n * Execution providers that were asked for, after defaults were applied.\n *\n * Kept separate because ORT-Web falls back silently: a page that asks for\n * `webgpu` on a device without it runs on WASM and is told nothing.\n */\n public readonly requestedProviders: readonly string[],\n ) {}\n\n /**\n * Load an ONNX model into an ORT inference session.\n *\n * The metadata map is read **before** the session is built, and that order is\n * load-bearing on memory-constrained devices. ORT copies the model into its\n * WASM heap and then allocates the graph and the weights on top of that copy;\n * a `readModelMetadata` call placed after `InferenceSession.create` keeps the\n * JavaScript-side buffer reachable across the whole build, so a 5 MB model\n * costs 5 MB of JS heap plus 5 MB of WASM heap plus the weights at the same\n * instant. Reading first makes the buffer collectable as soon as ORT has copied\n * it — on a phone that was the difference between a session and\n * `Can't create a session. failed to allocate a buffer of size N`.\n *\n * @param model Either a URL string, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.\n * @param options Provider list, pass-through `SessionOptions`, and whether to\n * read the model's metadata map (see {@link OrtSessionOptions.readMetadata}).\n * @throws {@link ModelLoadError} if the model cannot be loaded.\n */\n static async create(model: ModelSource, options: OrtSessionOptions = {}): Promise<OrtSession> {\n const requested = resolveProviders(options.providers);\n const detected = await detectProviders(requested);\n const providers = detected.length > 0 ? detected : [FALLBACK_PROVIDER];\n if (options.providers !== undefined && options.providers.length > 0) {\n warnOnDroppedProviders(requested, providers);\n }\n const sessionOptions: ort.InferenceSession.SessionOptions = {\n ...(options.sessionOptions ?? {}),\n executionProviders:\n providers as ort.InferenceSession.SessionOptions[\"executionProviders\"],\n };\n const wantsMetadata = options.readMetadata !== false;\n const source = typeof model === \"string\" && wantsMetadata ? await fetchModel(model) : model;\n const metadata =\n wantsMetadata && typeof source !== \"string\" ? readModelMetadata(source) : {};\n\n let session: ort.InferenceSession;\n try {\n if (typeof source === \"string\") {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else if (source instanceof Uint8Array) {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else {\n session = await ortRuntime.InferenceSession.create(\n source as ArrayBuffer,\n sessionOptions,\n );\n }\n } catch (err) {\n throw new ModelLoadError(`Failed to load ONNX model: ${(err as Error).message}`, {\n cause: err,\n });\n }\n\n return new OrtSession(session, providers, metadata, requested);\n }\n\n /** Names of the model's inputs, in declaration order. */\n get inputNames(): readonly string[] {\n return this._session.inputNames;\n }\n\n /** Name of the first (and usually only) input. */\n get inputName(): string {\n const name = this._session.inputNames[0];\n if (name === undefined) {\n throw new InferenceError(\"Model has no inputs.\");\n }\n return name;\n }\n\n /** Names of the model's outputs, in declaration order. */\n get outputNames(): readonly string[] {\n return this._session.outputNames;\n }\n\n /**\n * Shapes the graph declares for its inputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Empty shapes mean the runtime\n * reported no metadata — either a non-tensor input, or an `onnxruntime-web`\n * older than 1.21, which predates input metadata.\n */\n get inputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.inputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first input, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get inputShape(): DeclaredShape {\n return this.inputShapes[0] ?? [];\n }\n\n /**\n * Shapes the graph declares for its outputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Reading them is how a task can\n * tell how many classes a head emits without being told.\n */\n get outputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.outputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first output, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get outputShape(): DeclaredShape {\n return this.outputShapes[0] ?? [];\n }\n\n /**\n * The model's custom metadata map — `names`, `task`, `imgsz`, ... for an\n * Ultralytics export.\n *\n * Read from the model's bytes at load time, since the runtime does not expose\n * it. Empty when the session was created with `readMetadata: false`, from a\n * URL that could not be fetched here, or from a model carrying no metadata.\n */\n get metadata(): Readonly<Record<string, string>> {\n return this._metadata;\n }\n\n /**\n * Release the native session and free its memory.\n *\n * Call it when a session is discarded while the page lives on — rebuilding a\n * task at a different input size, swapping in a newer model. A failure from\n * the runtime is ignored: a session being torn down has nothing left to fail\n * at, and the caller is already moving on.\n */\n async release(): Promise<void> {\n await this._session.release().catch(() => undefined);\n }\n\n /** The underlying `onnxruntime-web` session, for advanced use cases. */\n get raw(): ort.InferenceSession {\n return this._session;\n }\n\n /**\n * Run inference and return all outputs.\n *\n * @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.\n * @throws {@link InferenceError} if ORT raises any error during execution.\n */\n async run(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>> {\n try {\n const result = await this._session.run(feeds);\n return result as Record<string, ort.Tensor>;\n } catch (err) {\n throw new InferenceError(`Inference failed: ${(err as Error).message}`, { cause: err });\n }\n }\n}\n"],"mappings":";;;;;;AA0BA,eAAe,EAAW,GAA2C;CACjE,IAAI;EACA,IAAM,IAAW,MAAM,MAAM,CAAG;EAKhC,OAJK,EAAS,KAIP,IAAI,WAAW,MAAM,EAAS,YAAY,CAAC,KAH9C,EAAwB,GAAK,QAAQ,EAAS,OAAO,GAAG,EAAS,YAAY,GACtE;CAGf,SAAS,GAAK;EAEV,OADA,EAAwB,GAAM,EAAc,OAAO,GAC5C;CACX;AACJ;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AAgC;EAChC,OAAO,KAAK,SAAS;CACzB;CAGA,IAAI,YAAoB;EACpB,IAAM,IAAO,KAAK,SAAS,WAAW;EACtC,IAAI,MAAS,KAAA,GACT,MAAM,IAAI,EAAe,sBAAsB;EAEnD,OAAO;CACX;CAGA,IAAI,cAAiC;EACjC,OAAO,KAAK,SAAS;CACzB;CASA,IAAI,cAAwC;EACxC,OAAO,EACH,KAAK,SAAS,aAElB;CACJ;CAOA,IAAI,aAA4B;EAC5B,OAAO,KAAK,YAAY,MAAM,CAAC;CACnC;CAQA,IAAI,eAAyC;EACzC,OAAO,EACH,KAAK,SAAS,cAElB;CACJ;CAOA,IAAI,cAA6B;EAC7B,OAAO,KAAK,aAAa,MAAM,CAAC;CACpC;CAUA,IAAI,WAA6C;EAC7C,OAAO,KAAK;CAChB;CAUA,MAAM,UAAyB;EAC3B,MAAM,KAAK,SAAS,QAAQ,CAAC,CAAC,YAAY,KAAA,CAAS;CACvD;CAGA,IAAI,MAA4B;EAC5B,OAAO,KAAK;CAChB;CAQA,MAAM,IAAI,GAAwE;EAC9E,IAAI;GAEA,OAAO,MADc,KAAK,SAAS,IAAI,CAAK;EAEhD,SAAS,GAAK;GACV,MAAM,IAAI,EAAe,qBAAsB,EAAc,WAAW,EAAE,OAAO,EAAI,CAAC;EAC1F;CACJ;AACJ"}
|
|
1
|
+
{"version":3,"file":"session.js","names":[],"sources":["../../../src/vision/core/session.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Thin wrapper around `onnxruntime-web` `InferenceSession` with typed metadata.\n */\n\nimport type * as ort from \"onnxruntime-web\";\nimport * as ortRuntime from \"onnxruntime-web\";\n\nimport { DEFAULT_TENSOR_TYPE, hasFloat16Array, tensorTypeFor, toFeedData } from \"./dtypes\";\nimport { InferenceError, ModelLoadError } from \"./exceptions\";\nimport { type DeclaredShape, declaredShapesFrom } from \"./graph\";\nimport { readModelInputTypes, readModelMetadata } from \"./metadata\";\nimport {\n FALLBACK_PROVIDER,\n type ProviderSpec,\n detectProviders,\n providerName,\n resolveProviders,\n} from \"./providers\";\n\n/** Anything `InferenceSession.create` accepts. */\nexport type ModelSource = string | ArrayBufferLike | Uint8Array;\n\n/**\n * Metadata key naming the level a model was graph-optimized at offline.\n *\n * Written by the Python SDK's `optimize_model`; both test suites pin the same\n * string, so a rename on one side fails. See {@link OrtSession.create} for what\n * the session does when it finds it.\n */\nexport const GRAPH_OPTIMIZATION_KEY = \"ort_vision_sdk.graph_optimization\";\n\n/**\n * An 84-byte ONNX model — one `Identity` on a float `[1]` — used to start the runtime.\n *\n * Generated with `onnx.helper` (opset 13, IR 7, producer `ort-vision-sdk`).\n * See {@link warmRuntime} for why it exists.\n */\nconst RUNTIME_PROBE_MODEL =\n \"CAcSDm9ydC12aXNpb24tc2RrOjoKEAoBeBIBeSIISWRlbnRpdHkSBHdhcm1aDwoBeBIKCggIARIECgIIAWIPCgF5EgoKCAgBEgQKAggBQgQKABAN\";\n\n/** Runtime start-ups already begun on this page, keyed by provider names. */\nconst runtimeWarmups = new Map<string, Promise<void>>();\n\n/**\n * Start ONNX Runtime's backends for a provider list, once per page.\n *\n * ORT downloads and compiles its WebAssembly binary — 12.8 MB for the plain\n * WASM build, 25.9 MB for the one carrying WebGPU — inside the first\n * `InferenceSession.create`. {@link OrtSession.create} only reaches that call\n * after it has downloaded the model, so on a real network the two largest\n * downloads of a page load ran one after the other. Creating and releasing a\n * session on a tiny model while the real one downloads lets them overlap; the\n * real `create` then finds the runtime ready.\n *\n * A failure here is swallowed: the real `create` runs against the same runtime\n * and reports the same problem with the right model in the message.\n *\n * @param specs Providers the real session will use.\n * @returns Settles when the runtime is up, or when starting it failed.\n */\nfunction warmRuntime(specs: readonly ProviderSpec[]): Promise<void> {\n const key = specs.map(providerName).join(\",\");\n let pending = runtimeWarmups.get(key);\n if (pending === undefined) {\n pending = (async () => {\n try {\n const probe = Uint8Array.from(atob(RUNTIME_PROBE_MODEL), (c) => c.charCodeAt(0));\n const session = await ortRuntime.InferenceSession.create(probe, {\n executionProviders:\n specs as ort.InferenceSession.SessionOptions[\"executionProviders\"],\n });\n await session.release();\n } catch {\n return;\n }\n })();\n runtimeWarmups.set(key, pending);\n }\n return pending;\n}\n\n/**\n * Warn that a model optimized offline is about to run on WebGPU.\n *\n * `optimize_model` fuses operators for the CPU, which is what the WASM backend\n * runs. On WebGPU some of those fused nodes have no GPU kernel: on a\n * YOLO11n-seg, ONNX Runtime placed 2 nodes on the CPU for the optimized file\n * and none for the original, so every inference pays GPU↔CPU copies. The SDK\n * cannot swap the file, so it says which one to ship.\n */\nfunction warnPreOptimizedOnWebGpu(): void {\n console.warn(\n \"[@ort-vision-sdk/web] This model was pre-optimized by optimize_model, which targets the \" +\n \"WASM backend; on WebGPU some of its fused nodes fall back to the CPU. For WebGPU, ship \" +\n \"the original export instead.\",\n );\n}\n\n/**\n * Metadata key the Python SDK's `quantize_model` writes on an INT8 model.\n *\n * Mirrored by `QUANTIZATION_KEY` in the Python SDK; both test suites pin the\n * string. See {@link keepQuantizedOffWebGpu} for what a session does with it.\n */\nexport const QUANTIZATION_KEY = \"ort_vision_sdk.quantization\";\n\n/**\n * Drop `webgpu` from the providers of a model `quantize_model` produced.\n *\n * ONNX Runtime Web's WebGPU `DequantizeLinear` rejects the quantized bias such\n * a model carries — `scale and zero-point inputs must have the same rank` —\n * so the first `predict()` fails; and a variant with the bias left in float\n * ran but returned different detections than WASM for the same file. The\n * model is fine on WASM, which is also where INT8 pays (52.7 ms vs 68.4 ms on\n * a YOLO11n-seg), so the session runs there and says so.\n *\n * @param specs Providers that survived detection.\n * @param metadata The model's metadata map.\n * @returns `specs` without `webgpu` when the model is marked quantized; the\n * WASM fallback when nothing else is left.\n */\nfunction keepQuantizedOffWebGpu(\n specs: readonly ProviderSpec[],\n metadata: Readonly<Record<string, string>>,\n): ProviderSpec[] {\n if (metadata[QUANTIZATION_KEY] === undefined) return [...specs];\n const kept = specs.filter((spec) => providerName(spec) !== \"webgpu\");\n if (kept.length === specs.length) return kept;\n console.warn(\n `[@ort-vision-sdk/web] This model was quantized by quantize_model (${metadata[QUANTIZATION_KEY]}); ` +\n \"ONNX Runtime Web's WebGPU backend cannot run its quantized operators, so it runs on WASM.\",\n );\n return kept.length > 0 ? kept : [FALLBACK_PROVIDER];\n}\n\n/** Cache Storage bucket used when {@link OrtSessionOptions.cache} is `true`. */\nexport const DEFAULT_MODEL_CACHE = \"ort-vision-sdk-models\";\n\n/**\n * Fetch a model URL as bytes so its metadata can be read.\n *\n * Falls back to the URL itself when the fetch fails, letting ORT try its own\n * load path: losing the metadata map is a downgrade, but failing to load a model\n * that ORT could have fetched would be a regression.\n *\n * @param url Where the `.onnx` lives.\n * @param cacheName Cache Storage bucket to read from and fill, or `null` to\n * always go to the network.\n * @returns The model bytes, or the original URL when they could not be fetched.\n */\nasync function fetchModel(url: string, cacheName: string | null): Promise<Uint8Array | string> {\n const cache = cacheName === null ? null : await openCache(cacheName);\n if (cache !== null) {\n const hit = await cache.match(url).catch(() => undefined);\n if (hit !== undefined) return new Uint8Array(await hit.arrayBuffer());\n }\n try {\n const response = await fetch(url);\n if (!response.ok) {\n warnMetadataUnavailable(url, `HTTP ${response.status} ${response.statusText}`);\n return url;\n }\n const buffer = await response.arrayBuffer();\n if (cache !== null) await storeModel(cache, url, buffer);\n return new Uint8Array(buffer);\n } catch (err) {\n warnMetadataUnavailable(url, (err as Error).message);\n return url;\n }\n}\n\n/**\n * Put a model's bytes into the cache, never failing the load over it.\n *\n * Stores a fresh `Response` built from bytes already read rather than a\n * `clone()` of the network response: one read, and nothing that can throw\n * synchronously outside the guard. Awaited on purpose — a `put` left running\n * would keep its copy of the model reachable while ORT builds the session, the\n * same doubled peak {@link OrtSession.create} orders its reads to avoid.\n *\n * @param cache The open bucket.\n * @param url Cache key.\n * @param buffer The model.\n */\nasync function storeModel(cache: Cache, url: string, buffer: ArrayBuffer): Promise<void> {\n try {\n await cache.put(url, new Response(buffer));\n } catch {\n return;\n }\n}\n\n/**\n * Open a Cache Storage bucket, or report that there is none to open.\n *\n * Cache Storage only exists in secure contexts (HTTPS, `localhost`), and a\n * browser may refuse it outright — private windows, blocked site data, an\n * exhausted quota. None of that is worth failing a model load over: the model\n * is fetched from the network as if caching had not been asked for. A failed\n * `put` is swallowed for the same reason — see {@link storeModel}.\n *\n * @param name Bucket name.\n * @returns The cache, or `null` when this environment cannot provide one.\n */\nasync function openCache(name: string): Promise<Cache | null> {\n if (typeof caches === \"undefined\") return null;\n try {\n return await caches.open(name);\n } catch {\n return null;\n }\n}\n\n/**\n * Warn that a model's metadata could not be read, and say what that costs.\n *\n * The fallback itself is right — losing the metadata beats failing a load that\n * ORT could have completed on its own — but it used to be silent, and the\n * symptom it produces is remote from the cause: class names come back as\n * `class_0`, `class_1`, ... with nothing anywhere explaining why. Whoever hits\n * this needs to be told that passing `labels` is the way out.\n *\n * @param url The model URL that could not be fetched here.\n * @param reason What went wrong, as reported by `fetch`.\n */\nfunction warnMetadataUnavailable(url: string, reason: string): void {\n console.warn(\n `[@ort-vision-sdk/web] Could not fetch ${url} to read its metadata (${reason}). ` +\n \"Letting ONNX Runtime load it instead: the model will work, but its baked-in \" +\n \"class names are unavailable, so labels fall back to class_0, class_1, ... \" +\n \"Pass `labels` explicitly to name them.\",\n );\n}\n\nexport interface OrtSessionOptions {\n /**\n * Execution providers in preference order. `undefined` uses {@link DEFAULT_PROVIDERS}.\n *\n * Each entry is a name (`\"webgpu\"`, `\"wasm\"`) or ORT's config object for that\n * provider, which passes provider options through untouched —\n * `{ name: \"webgpu\", preferredLayout: \"NHWC\" }`.\n *\n * Naming one explicitly also opts into a `console.warn` when this browser\n * cannot offer it, instead of falling back in silence.\n */\n readonly providers?: readonly ProviderSpec[];\n /**\n * Keep a URL model in the browser's Cache Storage between page loads.\n *\n * `true` uses the {@link DEFAULT_MODEL_CACHE} bucket; a string names the\n * bucket. A later `create` with the same URL reads the bytes from the cache\n * instead of the network — for a model of several megabytes, the bulk of\n * what a returning visitor waits for. Defaults to `false`.\n *\n * The URL is the cache key and nothing expires it: publish a changed model\n * under a new URL (`yolo.v2.onnx`, `?v=2`) or a new bucket name, or delete\n * the bucket with `caches.delete(name)`. Ignored for models passed as bytes,\n * and silently skipped where Cache Storage is unavailable (non-secure\n * contexts, blocked site data).\n */\n readonly cache?: boolean | string;\n /** Optional ORT session options forwarded to `InferenceSession.create`. */\n readonly sessionOptions?: ort.InferenceSession.SessionOptions;\n /**\n * Whether to read the model's custom metadata map (`names`, `task`, `imgsz`).\n * Defaults to `true`.\n *\n * The runtime does not expose that map, so it is read from the file itself —\n * which means a URL model is fetched here and handed to ORT as bytes instead\n * of letting ORT fetch it. That is the same single download either way, and\n * it is what lets a task resolve its labels off the model. Set to `false` to\n * keep the URL path untouched (unless {@link cache} is set, which needs the\n * bytes) and leave {@link OrtSession.metadata} empty.\n *\n * `false` is also the escape hatch when a device cannot afford the bytes: the\n * fetched buffer is dropped before ORT builds the graph (see\n * {@link OrtSession.create}), but ORT's own load path still keeps the model out\n * of reach of anything the SDK holds. A session built this way resolves its\n * input size from the graph as usual — only the class names are lost, so a\n * caller taking this route has to pass `labels` itself.\n */\n readonly readMetadata?: boolean;\n}\n\n/**\n * Warn when a provider named explicitly by the caller is not going to run.\n *\n * Only explicit requests are worth a warning. The default list exists precisely\n * so that falling from `webgpu` to `wasm` is the expected outcome — but a caller\n * who wrote `providers: [\"webgpu\"]` and lands on WASM has a page several times\n * slower than intended and nothing in the console to explain it.\n *\n * @param requested Providers that were asked for.\n * @param effective Providers that survived capability detection.\n */\nfunction warnOnDroppedProviders(requested: readonly string[], effective: readonly string[]): void {\n const dropped = requested.filter((provider) => !effective.includes(provider));\n if (dropped.length === 0) {\n return;\n }\n console.warn(\n `This browser cannot offer the requested execution provider(s) ${JSON.stringify(dropped)}; ` +\n `the session will run on ${JSON.stringify(effective)}. Inference still produces correct ` +\n \"results, on the fallback provider.\",\n );\n}\n\n/**\n * Name the tensor type of every graph input, straight from the model bytes.\n *\n * @param model The `.onnx` file contents.\n * @returns Input name → tensor type name. Empty when the file carries no\n * readable graph, which callers read as \"assume float32\".\n */\nfunction declaredInputTypes(model: Uint8Array | ArrayBufferLike): Readonly<Record<string, string>> {\n const declared = readModelInputTypes(model);\n const named: Record<string, string> = {};\n for (const [name, elemType] of Object.entries(declared)) {\n named[name] = tensorTypeFor(elemType);\n }\n return named;\n}\n\n/**\n * Warn when the model's bytes were in hand but no input type came back.\n *\n * Every ONNX graph declares at least one input, so an empty result from bytes\n * means the reader failed, not that the model said nothing. Without this the\n * failure is invisible: the session falls back to float32, which is right for\n * most models and wrong for exactly the half-precision ones this reader exists\n * to support — so it looks like nothing is broken until a `predict()` throws\n * `Unexpected input data type`. That is how a 1 MB ceiling on the graph\n * descent shipped in 0.9.0 and reached a consumer.\n */\nfunction warnOnUnreadableTypes(): void {\n console.warn(\n \"Could not read any input type from this model's bytes; feeds will be built as float32. \" +\n \"A half-precision model will fail at inference with 'Unexpected input data type'. \" +\n \"Please report this with the model that produced it.\",\n );\n}\n\n/**\n * Refuse a half-precision model in an environment that cannot feed one.\n *\n * ORT requires a real `Float16Array` for a `float16` tensor — the same bits in\n * a `Uint16Array` are rejected — and not every browser has one yet. Without\n * this check the session builds happily and the first `predict()` throws from\n * inside the preprocessing, which is both later and further from the cause.\n *\n * @param inputTypes Tensor type per input name.\n * @throws {@link ModelLoadError} when the graph wants half precision and the\n * runtime has no `Float16Array`.\n */\nfunction assertFeedableTypes(inputTypes: Readonly<Record<string, string>>): void {\n const half = Object.entries(inputTypes).filter(([, type]) => type === \"float16\");\n if (half.length === 0 || hasFloat16Array()) return;\n const names = half.map(([name]) => name).join(\", \");\n throw new ModelLoadError(\n `This model declares half-precision input(s) [${names}], but this environment has no ` +\n \"Float16Array, which ONNX Runtime requires for a float16 tensor. Use a float32 export \" +\n \"of the model, or run in a browser that supports Float16Array.\",\n );\n}\n\n/**\n * Wrap an ONNX Runtime Web `InferenceSession` with convenient metadata access.\n *\n * The wrapper exposes input/output names and the shapes the graph declares,\n * manages execution-provider selection, provides a typed {@link OrtSession.run}\n * method, and releases the native session through {@link OrtSession.release}.\n */\nexport class OrtSession {\n private constructor(\n private readonly _session: ort.InferenceSession,\n /**\n * Execution providers this session is expected to run on.\n *\n * The requested list narrowed to what this browser can actually offer — a\n * `webgpu` entry survives only where an adapter exists. Best-effort: ORT-Web\n * exposes no way to ask which provider a session ended up on, so an entry\n * here means \"not ruled out\", not \"confirmed\". See\n * {@link requestedProviders} for what was asked for.\n *\n * When nothing survives — a caller asking for `webgpu` alone on a device\n * without it — this falls back to {@link FALLBACK_PROVIDER}, which ORT-Web\n * can always run. Handing ORT the unsatisfiable list instead makes\n * `InferenceSession.create` reject with \"no available backend found\", so the\n * page gets no inference at all rather than the slow-but-working fallback\n * the `console.warn` describes. Measured in a real Chromium, where\n * `navigator.gpu` exists but yields no adapter.\n */\n public readonly providers: readonly string[],\n private readonly _metadata: Readonly<Record<string, string>>,\n /**\n * Execution providers that were asked for, after defaults were applied.\n *\n * Kept separate because ORT-Web falls back silently: a page that asks for\n * `webgpu` on a device without it runs on WASM and is told nothing.\n */\n public readonly requestedProviders: readonly string[],\n /**\n * Tensor type each input declares, keyed by input name.\n *\n * Read from the model file, not from the session: `inputMetadata` is\n * `undefined` on `onnxruntime-web` 1.20.1. Empty when the bytes were never\n * in hand — a URL loaded with `readMetadata: false` — in which case every\n * feed is built as float32, the behaviour before this existed.\n */\n private readonly _inputTypes: Readonly<Record<string, string>> = {},\n ) {}\n\n /**\n * Tail of the run queue: settles when the latest queued run has finished.\n *\n * See {@link run} for why runs are serialized.\n */\n private _runQueue: Promise<unknown> = Promise.resolve();\n\n /**\n * Load an ONNX model into an ORT inference session.\n *\n * The metadata map is read **before** the session is built, and that order is\n * load-bearing on memory-constrained devices. ORT copies the model into its\n * WASM heap and then allocates the graph and the weights on top of that copy;\n * a `readModelMetadata` call placed after `InferenceSession.create` keeps the\n * JavaScript-side buffer reachable across the whole build, so a 5 MB model\n * costs 5 MB of JS heap plus 5 MB of WASM heap plus the weights at the same\n * instant. Reading first makes the buffer collectable as soon as ORT has copied\n * it — on a phone that was the difference between a session and\n * `Can't create a session. failed to allocate a buffer of size N`.\n *\n * The provider probe and the model download run concurrently: neither\n * depends on the other, and on a real GPU `requestAdapter()` is not free.\n * When the model is downloaded here, ORT's runtime is started alongside the\n * download too — see {@link warmRuntime}.\n *\n * A model whose metadata carries {@link GRAPH_OPTIMIZATION_KEY} was already\n * optimized offline by the Python SDK's `optimize_model`, so it is loaded\n * with `graphOptimizationLevel: \"disabled\"` instead of paying the optimizer a\n * second time — measured on a YOLO11n-seg under WASM, 29 ms of session\n * creation down to 12 ms with unchanged inference time. An explicit\n * `graphOptimizationLevel` in `sessionOptions` always wins. The mark is read\n * with the rest of the metadata, so `readMetadata: false` skips it too.\n *\n * @param model Either a URL string, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.\n * @param options Provider list, pass-through `SessionOptions`, and whether to\n * read the model's metadata map (see {@link OrtSessionOptions.readMetadata}).\n * @throws {@link ModelLoadError} if the model cannot be loaded.\n */\n static async create(model: ModelSource, options: OrtSessionOptions = {}): Promise<OrtSession> {\n const requested = resolveProviders(options.providers);\n const wantsMetadata = options.readMetadata !== false;\n const cacheName =\n options.cache === true ? DEFAULT_MODEL_CACHE : options.cache ? options.cache : null;\n const shouldFetch = typeof model === \"string\" && (wantsMetadata || cacheName !== null);\n const detection = detectProviders(requested).then((detected) =>\n detected.length > 0 ? detected : [FALLBACK_PROVIDER],\n );\n const [detected, source] = await Promise.all([\n detection,\n shouldFetch ? fetchModel(model, cacheName) : Promise.resolve(model),\n shouldFetch ? detection.then(warmRuntime) : Promise.resolve(),\n ]);\n const metadata =\n wantsMetadata && typeof source !== \"string\" ? readModelMetadata(source) : {};\n const specs = keepQuantizedOffWebGpu(detected, metadata);\n const requestedNames = requested.map(providerName);\n const providers = specs.map(providerName);\n if (options.providers !== undefined && options.providers.length > 0) {\n warnOnDroppedProviders(requestedNames, detected.map(providerName));\n }\n if (metadata[GRAPH_OPTIMIZATION_KEY] !== undefined && providers.includes(\"webgpu\")) {\n warnPreOptimizedOnWebGpu();\n }\n const preOptimized =\n metadata[GRAPH_OPTIMIZATION_KEY] !== undefined &&\n options.sessionOptions?.graphOptimizationLevel === undefined;\n const sessionOptions: ort.InferenceSession.SessionOptions = {\n ...(preOptimized ? { graphOptimizationLevel: \"disabled\" as const } : {}),\n ...(options.sessionOptions ?? {}),\n executionProviders: specs as ort.InferenceSession.SessionOptions[\"executionProviders\"],\n };\n const inputTypes = typeof source !== \"string\" ? declaredInputTypes(source) : {};\n if (typeof source !== \"string\" && Object.keys(inputTypes).length === 0) {\n warnOnUnreadableTypes();\n }\n assertFeedableTypes(inputTypes);\n\n let session: ort.InferenceSession;\n try {\n if (typeof source === \"string\") {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else if (source instanceof Uint8Array) {\n session = await ortRuntime.InferenceSession.create(source, sessionOptions);\n } else {\n session = await ortRuntime.InferenceSession.create(\n source as ArrayBuffer,\n sessionOptions,\n );\n }\n } catch (err) {\n throw new ModelLoadError(`Failed to load ONNX model: ${(err as Error).message}`, {\n cause: err,\n });\n }\n\n return new OrtSession(session, providers, metadata, requestedNames, inputTypes);\n }\n\n /**\n * Tensor type each input declares, in declaration order.\n *\n * `\"float32\"` for a normal export, `\"float16\"` for one exported with\n * `half=True`. ORT matches a feed's type against this exactly, so the tasks\n * read it and convert at the feed boundary.\n */\n get inputDtypes(): readonly string[] {\n return this.inputNames.map((name) => this._inputTypes[name] ?? DEFAULT_TENSOR_TYPE);\n }\n\n /** Tensor type the first input declares. */\n get inputDtype(): string {\n return this._inputTypes[this.inputName] ?? DEFAULT_TENSOR_TYPE;\n }\n\n /** Names of the model's inputs, in declaration order. */\n get inputNames(): readonly string[] {\n return this._session.inputNames;\n }\n\n /** Name of the first (and usually only) input. */\n get inputName(): string {\n const name = this._session.inputNames[0];\n if (name === undefined) {\n throw new InferenceError(\"Model has no inputs.\");\n }\n return name;\n }\n\n /** Names of the model's outputs, in declaration order. */\n get outputNames(): readonly string[] {\n return this._session.outputNames;\n }\n\n /**\n * Shapes the graph declares for its inputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Empty shapes mean the runtime\n * reported no metadata — either a non-tensor input, or an `onnxruntime-web`\n * older than 1.21, which predates input metadata.\n */\n get inputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.inputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first input, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get inputShape(): DeclaredShape {\n return this.inputShapes[0] ?? [];\n }\n\n /**\n * Shapes the graph declares for its outputs, in declaration order.\n *\n * Dynamic (symbolic) axes appear as `null`. Reading them is how a task can\n * tell how many classes a head emits without being told.\n */\n get outputShapes(): readonly DeclaredShape[] {\n return declaredShapesFrom(\n this._session.outputMetadata as\n readonly ort.InferenceSession.ValueMetadata[] | undefined,\n );\n }\n\n /**\n * Shape the graph declares for its first output, dynamic axes as `null`.\n *\n * Empty when the runtime reports no metadata for it.\n */\n get outputShape(): DeclaredShape {\n return this.outputShapes[0] ?? [];\n }\n\n /**\n * The model's custom metadata map — `names`, `task`, `imgsz`, ... for an\n * Ultralytics export.\n *\n * Read from the model's bytes at load time, since the runtime does not expose\n * it. Empty when the session was created with `readMetadata: false`, from a\n * URL that could not be fetched here, or from a model carrying no metadata.\n */\n get metadata(): Readonly<Record<string, string>> {\n return this._metadata;\n }\n\n /**\n * Release the native session and free its memory.\n *\n * Call it when a session is discarded while the page lives on — rebuilding a\n * task at a different input size, swapping in a newer model. A failure from\n * the runtime is ignored: a session being torn down has nothing left to fail\n * at, and the caller is already moving on.\n */\n async release(): Promise<void> {\n await this._session.release().catch(() => undefined);\n }\n\n /** The underlying `onnxruntime-web` session, for advanced use cases. */\n get raw(): ort.InferenceSession {\n return this._session;\n }\n\n /**\n * Run inference and return all outputs.\n *\n * Runs on one session are serialized. ONNX Runtime Web refuses a second\n * `run` while one is in flight — it rejects with `Session already started` —\n * so two overlapping `predict()` calls on the same task used to fail. Queued\n * here, they overlap where they can: while one run executes (in a worker,\n * with `env.wasm.proxy`, or on the GPU), the next `predict()` decodes and\n * preprocesses its frame and only waits for its turn at the runtime. A run\n * that fails does not block the ones behind it.\n *\n * @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.\n * @throws {@link InferenceError} if ORT raises any error during execution.\n */\n run(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>> {\n const turn = this._runQueue.then(() => this._runNow(feeds));\n this._runQueue = turn.catch(() => undefined);\n return turn;\n }\n\n /**\n * Execute one run immediately; only {@link run}'s queue calls this.\n *\n * @param feeds Map of input name to `ort.Tensor`.\n * @throws {@link InferenceError} if ORT raises any error during execution.\n */\n private async _runNow(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>> {\n try {\n const result = await this._session.run(this._asDeclaredTypes(feeds));\n return result as Record<string, ort.Tensor>;\n } catch (err) {\n throw new InferenceError(`Inference failed: ${(err as Error).message}`, { cause: err });\n }\n }\n\n /**\n * Rebuild any feed whose type does not match what its input declares.\n *\n * ORT matches feed types against the graph exactly: a float32 tensor against\n * a `half=True` export fails the run with `Unexpected input data type`. Every\n * preprocessing path in this SDK produces `Float32Array`, on purpose — the\n * normalization arithmetic belongs in single precision — so the conversion\n * happens here, once, at the only boundary all of them pass through. A feed\n * that already carries the declared type is handed on untouched, so a caller\n * building its own half tensor pays nothing.\n *\n * @param feeds The tensors to run with.\n * @returns The same mapping, with mismatched float32 feeds converted.\n */\n private _asDeclaredTypes(feeds: Record<string, ort.Tensor>): Record<string, ort.Tensor> {\n let converted: Record<string, ort.Tensor> | null = null;\n for (const [name, tensor] of Object.entries(feeds)) {\n const declared = this._inputTypes[name];\n if (declared === undefined || declared === tensor.type) continue;\n if (!(tensor.data instanceof Float32Array)) continue;\n const data = toFeedData(tensor.data, declared);\n if (data === tensor.data) continue;\n converted ??= { ...feeds };\n converted[name] = new ortRuntime.Tensor(\n declared as \"float16\",\n data as unknown as Float32Array,\n tensor.dims as number[],\n );\n }\n return converted ?? feeds;\n }\n}\n"],"mappings":";;;;;;;AA8BA,IAAa,IAAyB,qCAQhC,IACF,oHAGE,oBAAiB,IAAI,IAA2B;AAmBtD,SAAS,EAAY,GAA+C;CAChE,IAAM,IAAM,EAAM,IAAI,CAAY,CAAC,CAAC,KAAK,GAAG,GACxC,IAAU,EAAe,IAAI,CAAG;CAgBpC,OAfI,MAAY,KAAA,MACZ,KAAW,YAAY;EACnB,IAAI;GACA,IAAM,IAAQ,WAAW,KAAK,KAAK,CAAmB,IAAI,MAAM,EAAE,WAAW,CAAC,CAAC;GAK/E,OAAM,MAJgB,EAAW,iBAAiB,OAAO,GAAO,EAC5D,oBACI,EACR,CAAC,EAAA,CACa,QAAQ;EAC1B,QAAQ;GACJ;EACJ;CACJ,EAAA,CAAG,GACH,EAAe,IAAI,GAAK,CAAO,IAE5B;AACX;AAWA,SAAS,IAAiC;CACtC,QAAQ,KACJ,6MAGJ;AACJ;AAQA,IAAa,IAAmB;AAiBhC,SAAS,EACL,GACA,GACc;CACd,IAAI,EAAA,mCAA+B,KAAA,GAAW,OAAO,CAAC,GAAG,CAAK;CAC9D,IAAM,IAAO,EAAM,QAAQ,MAAS,EAAa,CAAI,MAAM,QAAQ;CAMnE,OALI,EAAK,WAAW,EAAM,SAAe,KACzC,QAAQ,KACJ,qEAAqE,EAAS,GAAkB,6FAEpG,GACO,EAAK,SAAS,IAAI,IAAO,CAAC,CAAiB;AACtD;AAGA,IAAa,IAAsB;AAcnC,eAAe,EAAW,GAAa,GAAwD;CAC3F,IAAM,IAAQ,MAAc,OAAO,OAAO,MAAM,EAAU,CAAS;CACnE,IAAI,MAAU,MAAM;EAChB,IAAM,IAAM,MAAM,EAAM,MAAM,CAAG,CAAC,CAA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nC;CAQA,IAAI,eAAyC;EACzC,OAAO,EACH,KAAK,SAAS,cAElB;CACJ;CAOA,IAAI,cAA6B;EAC7B,OAAO,KAAK,aAAa,MAAM,CAAC;CACpC;CAUA,IAAI,WAA6C;EAC7C,OAAO,KAAK;CAChB;CAUA,MAAM,UAAyB;EAC3B,MAAM,KAAK,SAAS,QAAQ,CAAC,CAAC,YAAY,KAAA,CAAS;CACvD;CAGA,IAAI,MAA4B;EAC5B,OAAO,KAAK;CAChB;CAgBA,IAAI,GAAwE;EACxE,IAAM,IAAO,KAAK,UAAU,WAAW,KAAK,QAAQ,CAAK,CAAC;EAE1D,OADA,KAAK,YAAY,EAAK,YAAY,KAAA,CAAS,GACpC;CACX;CAQA,MAAc,QAAQ,GAAwE;EAC1F,IAAI;GAEA,OAAO,MADc,KAAK,SAAS,IAAI,KAAK,iBAAiB,CAAK,CAAC;EAEvE,SAAS,GAAK;GACV,MAAM,IAAI,EAAe,qBAAsB,EAAc,WAAW,EAAE,OAAO,EAAI,CAAC;EAC1F;CACJ;CAgBA,iBAAyB,GAA+D;EACpF,IAAI,IAA+C;EACnD,KAAK,IAAM,CAAC,GAAM,MAAW,OAAO,QAAQ,CAAK,GAAG;GAChD,IAAM,IAAW,KAAK,YAAY;GAElC,IADI,MAAa,KAAA,KAAa,MAAa,EAAO,QAC9C,EAAE,EAAO,gBAAgB,eAAe;GAC5C,IAAM,IAAO,EAAW,EAAO,MAAM,CAAQ;GACzC,MAAS,EAAO,SACpB,MAAc,EAAE,GAAG,EAAM,GACzB,EAAU,KAAQ,IAAI,EAAW,OAC7B,GACA,GACA,EAAO,IACX;EACJ;EACA,OAAO,KAAa;CACxB;AACJ"}
|
package/dist/vision/fusion.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./core/metadata.cjs");var t=`detect_classify`,n=`ovs.`,
|
|
1
|
+
const e=require("./core/metadata.cjs");var t=`detect_classify`,n=`detect_segment_classify`,r=`ovs.`,i=`images`,a=`source_image`,o=`letterbox_scale`,s=`letterbox_pad`,c=`boxes`,l=`scores`,u=`classes`,d=`num_detections`,f=`masks`,p=`probs`,m=`kind`,h=`sdk_version`,g=`input_size`,_=`crop_size`,v=`crop_source`,y=`max_detections`,b=`conf_threshold`,x=`iou_threshold`,S=`apply_softmax`,C=`detector_names`,w=`classifier_names`,T=`segmenter_names`,E=`mask_threshold`,D=`mask_applied`,O=`dynamic`;function k(e){if(!e)return null;let t=e.split(`,`);if(t.length!==2)return null;let n=Number(t[0]),r=Number(t[1]);return!Number.isInteger(n)||!Number.isInteger(r)||n<1||r<1?null:[n,r]}function A(e,t){if(!e)return t;let n=Number(e);return Number.isFinite(n)?n:t}function j(t){if(!t)return null;let r={};for(let[e,n]of Object.entries(t))e.startsWith(`ovs.`)&&(r[e.slice(4)]=n);let i=r[m];if(i!==`detect_classify`&&i!==`detect_segment_classify`)return null;let a=k(r[g]),o=k(r[_]);if(a===null||o===null)return null;let s=r[y]??O,c=Number(s),l=s===O||!Number.isInteger(c)||c<1?null:c,u=r[v]===`original`?`original`:`detector_input`;return{kind:i,inputSize:a,cropSize:o,cropSource:u,maxDetections:l,confThreshold:A(r[b],.25),iouThreshold:A(r[x],.45),applySoftmax:(r[S]??`1`)!==`0`,detectorNames:e.parseNames(r[C]),classifierNames:e.parseNames(r[w]),sdkVersion:r[h]??``,needsSourceImage:u===`original`,hasMasks:i===n,segmenterNames:e.parseNames(r[T]),maskThreshold:A(r[E],.5),maskApplied:(r[D]??`1`)!==`0`}}exports.FUSION_KIND_DETECT_CLASSIFY=t,exports.FUSION_KIND_DETECT_SEGMENT_CLASSIFY=n,exports.INPUT_IMAGE=i,exports.INPUT_PAD=s,exports.INPUT_SCALE=o,exports.INPUT_SOURCE=a,exports.METADATA_PREFIX=r,exports.OUTPUT_BOXES=c,exports.OUTPUT_CLASSES=u,exports.OUTPUT_MASKS=f,exports.OUTPUT_NUM_DETECTIONS=d,exports.OUTPUT_PROBS=p,exports.OUTPUT_SCORES=l,exports.readFusionSpec=j;
|
|
2
2
|
//# sourceMappingURL=fusion.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fusion.cjs","names":[],"sources":["../../src/vision/fusion.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * The contract a fused pipeline carries inside its own `.onnx` file.\n *\n * A pipeline built by the Python SDK's `ort_vision_sdk.compose` is a single\n * graph that already contains the detector, the crop-and-resize bridge and the\n * classifier. Everything the runtime needs to drive it — the letterbox\n * resolution, the crop resolution, where the crops come from, how many\n * detections the graph emits, the class names of both stages — was decided at\n * fusion time and written into the model's metadata.\n *\n * This module reads it back. It is the browser half of a contract whose other\n * half lives in `ort_vision_sdk/fusion.py`: the same keys, the same encodings,\n * the same fallbacks. A pipeline fused once therefore runs identically in\n * Python and in a browser tab, off the same file.\n *\n * Building a pipeline stays a Python-side build step — there is no ONNX\n * protobuf writer here, and there is no reason for one: fusing is something you\n * do once next to your export pipeline, not in a page load.\n */\n\nimport { parseNames } from \"./core/metadata\";\n\n/**\n * Which tensor the bridge crops the detected boxes out of.\n *\n * - `\"detector_input\"`: the letterboxed tensor already fed to the detector.\n * The fused graph then has a **single** image input, but a small object is\n * cropped out of its downscaled copy.\n * - `\"original\"`: a second, full-resolution image input. The bridge undoes the\n * letterbox transform in-graph and crops at native resolution. Two tensors to\n * feed, still one session and one model load.\n */\nexport type CropSource = \"detector_input\" | \"original\";\n\n/** Value of the `ovs.kind` metadata key for a detector→classifier pipeline. */\nexport const FUSION_KIND_DETECT_CLASSIFY = \"detect_classify\";\n\n/**\n * Namespace for every metadata key the fusion writes.\n *\n * Namespaced on purpose: the detector's own Ultralytics metadata (`names`,\n * `task`, `imgsz`) is carried over into the fused model, and an un-prefixed key\n * would either collide with it or be mistaken for it.\n */\nexport const METADATA_PREFIX = \"ovs.\";\n\n/** Name of the fused graph's letterboxed detector input, `[1, 3, H, W]` float32 in `[0, 1]`. */\nexport const INPUT_IMAGE = \"images\";\n\n/** Name of the full-resolution input. Present only when `cropSource === \"original\"`. */\nexport const INPUT_SOURCE = \"source_image\";\n\n/** Name of the `[1]` float32 letterbox scale factor. Only with `cropSource === \"original\"`. */\nexport const INPUT_SCALE = \"letterbox_scale\";\n\n/** Name of the `[2]` float32 `[padLeft, padTop]`. Only with `cropSource === \"original\"`. */\nexport const INPUT_PAD = \"letterbox_pad\";\n\n/**\n * Name of the `[K, 4]` float32 xyxy output, in **letterboxed** input pixels.\n *\n * A file fused by `ort-vision-sdk` 0.9.0 or later reports the box that was\n * actually classified: clamped to the image the crop came from, exactly as\n * RoiAlign received it. Older files report the raw box, so one that ran off the\n * frame draws a rectangle wider than the region the classifier saw.\n */\nexport const OUTPUT_BOXES = \"boxes\";\n\n/** Name of the `[K]` float32 detection-confidence output. */\nexport const OUTPUT_SCORES = \"scores\";\n\n/** Name of the `[K]` int64 detector-class output. */\nexport const OUTPUT_CLASSES = \"classes\";\n\n/** Name of the `[1]` int64 output holding how many of the `K` rows are real. */\nexport const OUTPUT_NUM_DETECTIONS = \"num_detections\";\n\n/** Name of the `[K, numClassifierClasses]` float32 classifier output, one row per box. */\nexport const OUTPUT_PROBS = \"probs\";\n\n/** Everything a fused pipeline declares about how it must be driven. */\nexport interface FusionSpec {\n /** Pipeline family. Only `\"detect_classify\"` exists today. */\n readonly kind: string;\n /** `[width, height]` the detector stage expects — the resolution to letterbox to. */\n readonly inputSize: readonly [number, number];\n /** `[width, height]` every crop is resampled to inside the graph. */\n readonly cropSize: readonly [number, number];\n /** Which tensor the crops are taken from. */\n readonly cropSource: CropSource;\n /**\n * Fixed number of rows `K` every output carries, surplus zero-padded and\n * counted by {@link OUTPUT_NUM_DETECTIONS}. `null` means the graph emits\n * exactly as many rows as survived NMS.\n */\n readonly maxDetections: number | null;\n /** Score threshold baked into the graph's NMS node. */\n readonly confThreshold: number;\n /** IoU threshold baked into the graph's NMS node. */\n readonly iouThreshold: number;\n /** Whether the classifier stage emits logits that still need a softmax. */\n readonly applySoftmax: boolean;\n /** Detector class names in class-id order, or `null` when the fusion recorded none. */\n readonly detectorNames: readonly string[] | null;\n /** Classifier class names in class-id order, or `null`. */\n readonly classifierNames: readonly string[] | null;\n /** Version of `ort-vision-sdk` that produced the file. */\n readonly sdkVersion: string;\n /** Whether driving this pipeline requires feeding the full-resolution input. */\n readonly needsSourceImage: boolean;\n}\n\nconst KEY_KIND = \"kind\";\nconst KEY_SDK_VERSION = \"sdk_version\";\nconst KEY_INPUT_SIZE = \"input_size\";\nconst KEY_CROP_SIZE = \"crop_size\";\nconst KEY_CROP_SOURCE = \"crop_source\";\nconst KEY_MAX_DETECTIONS = \"max_detections\";\nconst KEY_CONF_THRESHOLD = \"conf_threshold\";\nconst KEY_IOU_THRESHOLD = \"iou_threshold\";\nconst KEY_APPLY_SOFTMAX = \"apply_softmax\";\nconst KEY_DETECTOR_NAMES = \"detector_names\";\nconst KEY_CLASSIFIER_NAMES = \"classifier_names\";\n\nconst DYNAMIC = \"dynamic\";\n\n/**\n * Decode a `\"640,640\"` pair.\n *\n * @param raw The encoded pair.\n * @returns `[width, height]`, or `null` when the value is missing or malformed.\n */\nfunction decodeSize(raw: string | undefined): readonly [number, number] | null {\n if (!raw) return null;\n const parts = raw.split(\",\");\n if (parts.length !== 2) return null;\n const width = Number(parts[0]);\n const height = Number(parts[1]);\n if (!Number.isInteger(width) || !Number.isInteger(height)) return null;\n if (width < 1 || height < 1) return null;\n return [width, height];\n}\n\n/**\n * Decode a float, falling back when the value is missing or malformed.\n *\n * @param raw The encoded value.\n * @param fallback Value to use when `raw` cannot be read.\n * @returns The parsed number, or `fallback`.\n */\nfunction decodeFloat(raw: string | undefined, fallback: number): number {\n if (!raw) return fallback;\n const value = Number(raw);\n return Number.isFinite(value) ? value : fallback;\n}\n\n/**\n * Read a pipeline spec out of a model's custom metadata.\n *\n * Individual malformed entries fall back to the value a fusion would have used\n * by default — a single bad float is not a reason to reject an otherwise\n * loadable pipeline. A malformed resolution is fatal, because there is no safe\n * default for one.\n *\n * @param metadata A model's custom metadata map, as read by\n * {@link readModelMetadata}.\n * @returns The decoded spec, or `null` when the model is not a fused pipeline —\n * it carries no `ovs.kind` entry, or one naming a pipeline kind this version\n * does not know how to drive.\n */\nexport function readFusionSpec(\n metadata: Readonly<Record<string, string>> | undefined,\n): FusionSpec | null {\n if (!metadata) return null;\n\n const read: Record<string, string> = {};\n for (const [key, value] of Object.entries(metadata)) {\n if (key.startsWith(METADATA_PREFIX)) read[key.slice(METADATA_PREFIX.length)] = value;\n }\n if (read[KEY_KIND] !== FUSION_KIND_DETECT_CLASSIFY) return null;\n\n const inputSize = decodeSize(read[KEY_INPUT_SIZE]);\n const cropSize = decodeSize(read[KEY_CROP_SIZE]);\n if (inputSize === null || cropSize === null) return null;\n\n const rawMax = read[KEY_MAX_DETECTIONS] ?? DYNAMIC;\n const parsedMax = Number(rawMax);\n const maxDetections =\n rawMax === DYNAMIC || !Number.isInteger(parsedMax) || parsedMax < 1 ? null : parsedMax;\n\n const cropSource: CropSource =\n read[KEY_CROP_SOURCE] === \"original\" ? \"original\" : \"detector_input\";\n\n return {\n kind: FUSION_KIND_DETECT_CLASSIFY,\n inputSize,\n cropSize,\n cropSource,\n maxDetections,\n confThreshold: decodeFloat(read[KEY_CONF_THRESHOLD], 0.25),\n iouThreshold: decodeFloat(read[KEY_IOU_THRESHOLD], 0.45),\n applySoftmax: (read[KEY_APPLY_SOFTMAX] ?? \"1\") !== \"0\",\n detectorNames: parseNames(read[KEY_DETECTOR_NAMES]),\n classifierNames: parseNames(read[KEY_CLASSIFIER_NAMES]),\n sdkVersion: read[KEY_SDK_VERSION] ?? \"\",\n needsSourceImage: cropSource === \"original\",\n };\n}\n"],"mappings":"uCAoCA,IAAa,EAA8B,kBAS9B,EAAkB,OAGlB,EAAc,SAGd,EAAe,eAGf,EAAc,kBAGd,EAAY,gBAUZ,EAAe,QAGf,EAAgB,SAGhB,EA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,aACA,gBACA,cAAe,EAAY,EAAK,GAAqB,GAAI,EACzD,aAAc,EAAY,EAAK,GAAoB,GAAI,EACvD,cAAe,EAAK,IAAsB,OAAS,IACnD,cAAe,EAAA,WAAW,EAAK,EAAmB,EAClD,gBAAiB,EAAA,WAAW,EAAK,EAAqB,EACtD,WAAY,EAAK,IAAoB,GACrC,iBAAkB,IAAe,UACrC,CACJ"}
|
|
1
|
+
{"version":3,"file":"fusion.cjs","names":[],"sources":["../../src/vision/fusion.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * The contract a fused pipeline carries inside its own `.onnx` file.\n *\n * A pipeline built by the Python SDK's `ort_vision_sdk.compose` is a single\n * graph that already contains the detector, the crop-and-resize bridge and the\n * classifier. Everything the runtime needs to drive it — the letterbox\n * resolution, the crop resolution, where the crops come from, how many\n * detections the graph emits, the class names of both stages — was decided at\n * fusion time and written into the model's metadata.\n *\n * This module reads it back. It is the browser half of a contract whose other\n * half lives in `ort_vision_sdk/fusion.py`: the same keys, the same encodings,\n * the same fallbacks. A pipeline fused once therefore runs identically in\n * Python and in a browser tab, off the same file.\n *\n * Building a pipeline stays a Python-side build step — there is no ONNX\n * protobuf writer here, and there is no reason for one: fusing is something you\n * do once next to your export pipeline, not in a page load.\n */\n\nimport { parseNames } from \"./core/metadata\";\n\n/**\n * Which tensor the bridge crops the detected boxes out of.\n *\n * - `\"detector_input\"`: the letterboxed tensor already fed to the detector.\n * The fused graph then has a **single** image input, but a small object is\n * cropped out of its downscaled copy.\n * - `\"original\"`: a second, full-resolution image input. The bridge undoes the\n * letterbox transform in-graph and crops at native resolution. Two tensors to\n * feed, still one session and one model load.\n */\nexport type CropSource = \"detector_input\" | \"original\";\n\n/** Value of the `ovs.kind` metadata key for a detector→classifier pipeline. */\nexport const FUSION_KIND_DETECT_CLASSIFY = \"detect_classify\";\n\n/**\n * Value of `ovs.kind` for a detector→segmenter→classifier pipeline.\n *\n * The segmenter runs inside each crop rather than over the whole frame: the\n * detector finds the boxes, the bridge cuts them out, and the segmenter turns\n * each crop into a foreground mask that gates the pixels the classifier sees.\n * Fusing one is a build step on the Python side; the browser only loads the\n * result.\n */\nexport const FUSION_KIND_DETECT_SEGMENT_CLASSIFY = \"detect_segment_classify\";\n\n/**\n * Namespace for every metadata key the fusion writes.\n *\n * Namespaced on purpose: the detector's own Ultralytics metadata (`names`,\n * `task`, `imgsz`) is carried over into the fused model, and an un-prefixed key\n * would either collide with it or be mistaken for it.\n */\nexport const METADATA_PREFIX = \"ovs.\";\n\n/** Name of the fused graph's letterboxed detector input, `[1, 3, H, W]` float32 in `[0, 1]`. */\nexport const INPUT_IMAGE = \"images\";\n\n/** Name of the full-resolution input. Present only when `cropSource === \"original\"`. */\nexport const INPUT_SOURCE = \"source_image\";\n\n/** Name of the `[1]` float32 letterbox scale factor. Only with `cropSource === \"original\"`. */\nexport const INPUT_SCALE = \"letterbox_scale\";\n\n/** Name of the `[2]` float32 `[padLeft, padTop]`. Only with `cropSource === \"original\"`. */\nexport const INPUT_PAD = \"letterbox_pad\";\n\n/**\n * Name of the `[K, 4]` float32 xyxy output, in **letterboxed** input pixels.\n *\n * A file fused by `ort-vision-sdk` 0.9.0 or later reports the box that was\n * actually classified: clamped to the image the crop came from, exactly as\n * RoiAlign received it. Older files report the raw box, so one that ran off the\n * frame draws a rectangle wider than the region the classifier saw.\n */\nexport const OUTPUT_BOXES = \"boxes\";\n\n/** Name of the `[K]` float32 detection-confidence output. */\nexport const OUTPUT_SCORES = \"scores\";\n\n/** Name of the `[K]` int64 detector-class output. */\nexport const OUTPUT_CLASSES = \"classes\";\n\n/** Name of the `[1]` int64 output holding how many of the `K` rows are real. */\nexport const OUTPUT_NUM_DETECTIONS = \"num_detections\";\n\n/** Name of the `[K, numClassifierClasses]` float32 classifier output, one row per box. */\n/**\n * Name of the `(K, 1, cropH, cropW)` float32 mask output. Only on a\n * `detect_segment_classify` pipeline.\n *\n * The mask lives in the **crop's** coordinate space, not the original image's,\n * because that is where the segmenter computed it. Values are 0 or 1 after\n * thresholding; padded rows are all-zero, like every other output.\n */\nexport const OUTPUT_MASKS = \"masks\";\n\nexport const OUTPUT_PROBS = \"probs\";\n\n/** Everything a fused pipeline declares about how it must be driven. */\nexport interface FusionSpec {\n /** Pipeline family — {@link FUSION_KIND_DETECT_CLASSIFY} or {@link FUSION_KIND_DETECT_SEGMENT_CLASSIFY}. */\n readonly kind: string;\n /** `[width, height]` the detector stage expects — the resolution to letterbox to. */\n readonly inputSize: readonly [number, number];\n /** `[width, height]` every crop is resampled to inside the graph. */\n readonly cropSize: readonly [number, number];\n /** Which tensor the crops are taken from. */\n readonly cropSource: CropSource;\n /**\n * Fixed number of rows `K` every output carries, surplus zero-padded and\n * counted by {@link OUTPUT_NUM_DETECTIONS}. `null` means the graph emits\n * exactly as many rows as survived NMS.\n */\n readonly maxDetections: number | null;\n /** Score threshold baked into the graph's NMS node. */\n readonly confThreshold: number;\n /** IoU threshold baked into the graph's NMS node. */\n readonly iouThreshold: number;\n /** Whether the classifier stage emits logits that still need a softmax. */\n readonly applySoftmax: boolean;\n /** Detector class names in class-id order, or `null` when the fusion recorded none. */\n readonly detectorNames: readonly string[] | null;\n /** Classifier class names in class-id order, or `null`. */\n readonly classifierNames: readonly string[] | null;\n /** Version of `ort-vision-sdk` that produced the file. */\n readonly sdkVersion: string;\n /** Whether driving this pipeline requires feeding the full-resolution input. */\n readonly needsSourceImage: boolean;\n /** Whether this pipeline emits a {@link OUTPUT_MASKS} tensor. */\n readonly hasMasks: boolean;\n /** Class id → name for the segmenter's channels, or `null`. */\n readonly segmenterNames: readonly string[] | null;\n /**\n * Probability above which a segmenter pixel counted as foreground.\n *\n * Baked into the graph at fusion time; reported so a caller can say what the\n * masks mean.\n */\n readonly maskThreshold: number;\n /** Whether the mask gated the crop before the classifier saw it. */\n readonly maskApplied: boolean;\n}\n\nconst KEY_KIND = \"kind\";\nconst KEY_SDK_VERSION = \"sdk_version\";\nconst KEY_INPUT_SIZE = \"input_size\";\nconst KEY_CROP_SIZE = \"crop_size\";\nconst KEY_CROP_SOURCE = \"crop_source\";\nconst KEY_MAX_DETECTIONS = \"max_detections\";\nconst KEY_CONF_THRESHOLD = \"conf_threshold\";\nconst KEY_IOU_THRESHOLD = \"iou_threshold\";\nconst KEY_APPLY_SOFTMAX = \"apply_softmax\";\nconst KEY_DETECTOR_NAMES = \"detector_names\";\nconst KEY_CLASSIFIER_NAMES = \"classifier_names\";\nconst KEY_SEGMENTER_NAMES = \"segmenter_names\";\nconst KEY_MASK_THRESHOLD = \"mask_threshold\";\nconst KEY_MASK_APPLIED = \"mask_applied\";\n\nconst DYNAMIC = \"dynamic\";\n\n/**\n * Decode a `\"640,640\"` pair.\n *\n * @param raw The encoded pair.\n * @returns `[width, height]`, or `null` when the value is missing or malformed.\n */\nfunction decodeSize(raw: string | undefined): readonly [number, number] | null {\n if (!raw) return null;\n const parts = raw.split(\",\");\n if (parts.length !== 2) return null;\n const width = Number(parts[0]);\n const height = Number(parts[1]);\n if (!Number.isInteger(width) || !Number.isInteger(height)) return null;\n if (width < 1 || height < 1) return null;\n return [width, height];\n}\n\n/**\n * Decode a float, falling back when the value is missing or malformed.\n *\n * @param raw The encoded value.\n * @param fallback Value to use when `raw` cannot be read.\n * @returns The parsed number, or `fallback`.\n */\nfunction decodeFloat(raw: string | undefined, fallback: number): number {\n if (!raw) return fallback;\n const value = Number(raw);\n return Number.isFinite(value) ? value : fallback;\n}\n\n/**\n * Read a pipeline spec out of a model's custom metadata.\n *\n * Individual malformed entries fall back to the value a fusion would have used\n * by default — a single bad float is not a reason to reject an otherwise\n * loadable pipeline. A malformed resolution is fatal, because there is no safe\n * default for one.\n *\n * @param metadata A model's custom metadata map, as read by\n * {@link readModelMetadata}.\n * @returns The decoded spec, or `null` when the model is not a fused pipeline —\n * it carries no `ovs.kind` entry, or one naming a pipeline kind this version\n * does not know how to drive.\n */\nexport function readFusionSpec(\n metadata: Readonly<Record<string, string>> | undefined,\n): FusionSpec | null {\n if (!metadata) return null;\n\n const read: Record<string, string> = {};\n for (const [key, value] of Object.entries(metadata)) {\n if (key.startsWith(METADATA_PREFIX)) read[key.slice(METADATA_PREFIX.length)] = value;\n }\n const kind = read[KEY_KIND];\n if (kind !== FUSION_KIND_DETECT_CLASSIFY && kind !== FUSION_KIND_DETECT_SEGMENT_CLASSIFY) {\n return null;\n }\n\n const inputSize = decodeSize(read[KEY_INPUT_SIZE]);\n const cropSize = decodeSize(read[KEY_CROP_SIZE]);\n if (inputSize === null || cropSize === null) return null;\n\n const rawMax = read[KEY_MAX_DETECTIONS] ?? DYNAMIC;\n const parsedMax = Number(rawMax);\n const maxDetections =\n rawMax === DYNAMIC || !Number.isInteger(parsedMax) || parsedMax < 1 ? null : parsedMax;\n\n const cropSource: CropSource =\n read[KEY_CROP_SOURCE] === \"original\" ? \"original\" : \"detector_input\";\n\n return {\n kind,\n inputSize,\n cropSize,\n cropSource,\n maxDetections,\n confThreshold: decodeFloat(read[KEY_CONF_THRESHOLD], 0.25),\n iouThreshold: decodeFloat(read[KEY_IOU_THRESHOLD], 0.45),\n applySoftmax: (read[KEY_APPLY_SOFTMAX] ?? \"1\") !== \"0\",\n detectorNames: parseNames(read[KEY_DETECTOR_NAMES]),\n classifierNames: parseNames(read[KEY_CLASSIFIER_NAMES]),\n sdkVersion: read[KEY_SDK_VERSION] ?? \"\",\n needsSourceImage: cropSource === \"original\",\n hasMasks: kind === FUSION_KIND_DETECT_SEGMENT_CLASSIFY,\n segmenterNames: parseNames(read[KEY_SEGMENTER_NAMES]),\n maskThreshold: decodeFloat(read[KEY_MASK_THRESHOLD], 0.5),\n maskApplied: (read[KEY_MASK_APPLIED] ?? \"1\") !== \"0\",\n };\n}\n"],"mappings":"uCAoCA,IAAa,EAA8B,kBAW9B,EAAsC,0BAStC,EAAkB,OAGlB,EAAc,SAGd,EAAe,eAGf,EAAc,kBAGd,EAAY,gBAUZ,EA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