From 818a524a1346bffe2766727bf8c4d37a35dadc66 Mon Sep 17 00:00:00 2001 From: RDCLder Date: Mon, 9 Nov 2026 23:31:25 -0600 Subject: [PATCH 1/2] Runtime: vendor cardvision-browser from toolkit 0.10.0 (d0584b7) From cfb8fb2 (cardvision 0.6.0rc2) to d0584b7 (cardvision 0.10.0). In between: release-load timings, the tilted retry (off unless a release asks for it), layout registration (not used by DuelSight), the session turning recognition off when the bundled release fails to load, and turned outlines (rotatedOutlines, off). OpenCV.js and ONNX Runtime Web 1.29.0 unchanged. No DuelSight code change was needed: the type check, lint and 576 tests pass; the store build has the same file list as 0.7.0; on Chrome for Testing 152 a dev build loads the live pointer release f3ed25290dde66e9 and names cards, and the Card language menu works as in 0.7.0. Signed-off-by: RDCLder --- vendor/cardvision-browser/VENDOR.json | 4 +- vendor/cardvision-browser/cardvision.js | 2058 +++++++-------- vendor/cardvision-browser/compute-session.js | 2 +- vendor/cardvision-browser/page-capture.js | 2 +- vendor/cardvision-browser/types/cadence.d.ts | 7 +- .../types/compute-session.d.ts | 2 +- .../types/detector/detector.d.ts | 3 + vendor/cardvision-browser/types/embedder.d.ts | 3 + .../types/frame-scheduler.d.ts | 23 +- .../types/hover-geometry.d.ts | 6 +- .../cardvision-browser/types/hover-layer.d.ts | 4 +- vendor/cardvision-browser/types/index.d.ts | 17 +- .../cardvision-browser/types/layout-fit.d.ts | 201 ++ .../types/layout-metrics.d.ts | 51 + .../types/layout-recording.d.ts | 149 ++ .../types/layout-state.d.ts | 149 ++ vendor/cardvision-browser/types/load.d.ts | 39 + vendor/cardvision-browser/types/messages.d.ts | 32 +- .../types/mismatch-monitor.d.ts | 52 + vendor/cardvision-browser/types/ort.d.ts | 9 +- vendor/cardvision-browser/types/pipeline.d.ts | 48 +- .../cardvision-browser/types/recognition.d.ts | 65 +- vendor/cardvision-browser/types/release.d.ts | 7 + vendor/cardvision-browser/types/scene.d.ts | 2 + .../types/session-core.d.ts | 15 +- vendor/cardvision-browser/types/session.d.ts | 14 +- vendor/cardvision-browser/types/tilt.d.ts | 106 + vendor/cardvision-browser/types/tracker.d.ts | 3 + .../cardvision-browser/types/worker-host.d.ts | 2 + .../types/worker-session.d.ts | 9 +- vendor/cardvision-browser/worker.js | 2226 ++++++++--------- 31 files changed, 3133 insertions(+), 2177 deletions(-) create mode 100644 vendor/cardvision-browser/types/layout-fit.d.ts create mode 100644 vendor/cardvision-browser/types/layout-metrics.d.ts create mode 100644 vendor/cardvision-browser/types/layout-recording.d.ts create mode 100644 vendor/cardvision-browser/types/layout-state.d.ts create mode 100644 vendor/cardvision-browser/types/mismatch-monitor.d.ts create mode 100644 vendor/cardvision-browser/types/tilt.d.ts diff --git a/vendor/cardvision-browser/VENDOR.json b/vendor/cardvision-browser/VENDOR.json index 77d5d77..7b28418 100644 --- a/vendor/cardvision-browser/VENDOR.json +++ b/vendor/cardvision-browser/VENDOR.json @@ -3,8 +3,8 @@ "version": "0.1.0", "license": "Apache-2.0", "source": "cardvision-browser, the browser runtime of the cardvision toolkit (source published with the toolkit release)", - "commit": "cfb8fb23fb08a88c1edd9f1fa60c8b05fca7e376", - "toolkitRelease": "cardvision 0.6.0rc2", + "commit": "d0584b7f6cf1ddc1938ba654f025483768fce429", + "toolkitRelease": "cardvision 0.10.0", "onnxruntimeWeb": "1.29.0", "ortFiles": [ "ort-wasm-simd-threaded.jsep.mjs", diff --git a/vendor/cardvision-browser/cardvision.js b/vendor/cardvision-browser/cardvision.js index 73da9f6..7d1a11e 100644 --- a/vendor/cardvision-browser/cardvision.js +++ b/vendor/cardvision-browser/cardvision.js @@ -1,4 +1,4 @@ -var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var dI=Object.create,Co=Object.defineProperty,cI=Object.getOwnPropertyDescriptor,pI=Object.getOwnPropertyNames,hI=Object.getPrototypeOf,fI=Object.prototype.hasOwnProperty,cu=(e=>typeof Ir<"u"?Ir:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof Ir<"u"?Ir:t)[n]}):e)(function(e){if(typeof Ir<"u")return Ir.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')}),D=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}},oe=(e,t)=>()=>{try{return t||e((t={exports:{}}).exports,t),t.exports}catch(n){throw t=0,n}},nn=(e,t)=>{for(var n in t)Co(e,n,{get:t[n],enumerable:!0})},Oy=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of pI(t))!fI.call(e,u)&&u!==n&&Co(e,u,{get:()=>t[u],enumerable:!(o=cI(t,u))||o.enumerable});return e},fe=(e,t,n)=>(n=e!=null?dI(hI(e)):{},Oy(t||!e||!e.__esModule?Co(n,"default",{value:e,enumerable:!0}):n,e)),wn=e=>Oy(Co({},"__esModule",{value:!0}),e),Fn,or,Zr,$p,Ey,Ay=D(()=>{"use strict";Fn=new Map,or=[],Zr=(e,t,n)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let o=Fn.get(e);if(o===void 0)Fn.set(e,{backend:t,priority:n});else{if(o.priority>n)return;if(o.priority===n&&o.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){let u=or.indexOf(e);u!==-1&&or.splice(u,1);for(let l=0;l{let t=Fn.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let n=!!t.initPromise;try{return n||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(o){return n||(t.error=`${o}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},Ey=async e=>{let t=e.executionProviders||[],n=t.map(s=>typeof s=="string"?s:s.name),o=n.length===0?or:n,u,l=[],d=new Set;for(let s of o){let r=await $p(s);typeof r=="string"?l.push({name:s,err:r}):(u||(u=r),u===r&&d.add(s))}if(!u)throw new Error(`no available backend found. ERR: ${l.map(s=>`[${s.name}] ${s.err}`).join(", ")}`);for(let{name:s,err:r}of l)n.includes(s)&&console.warn(`removing requested execution provider "${s}" from session options because it is not available: ${r}`);let c=t.filter(s=>d.has(typeof s=="string"?s:s.name));return[u,new Proxy(e,{get:(s,r)=>r==="executionProviders"?c:Reflect.get(s,r)})]}}),mI=D(()=>{"use strict";Ay()}),ky,gI=D(()=>{"use strict";ky="1.29.0"}),Rs,et,Py=D(()=>{"use strict";gI(),Rs="warning",et={wasm:{},webgl:{},webgpu:{},versions:{common:ky},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Rs=e}},get logLevel(){return Rs}},Object.defineProperty(et,"logLevel",{enumerable:!0})}),pe,bI=D(()=>{"use strict";Py(),pe=et}),Cy,Dy,yI=D(()=>{"use strict";Cy=(e,t)=>{let n=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);n.width=e.dims[3],n.height=e.dims[2];let o=n.getContext("2d");if(o!=null){let u,l;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[3]):(u=e.dims[3],l=e.dims[2]);let d=t?.format!==void 0?t.format:"RGB",c=t?.norm,s,r;c===void 0||c.mean===void 0?s=[255,255,255,255]:typeof c.mean=="number"?s=[c.mean,c.mean,c.mean,c.mean]:(s=[c.mean[0],c.mean[1],c.mean[2],0],c.mean[3]!==void 0&&(s[3]=c.mean[3])),c===void 0||c.bias===void 0?r=[0,0,0,0]:typeof c.bias=="number"?r=[c.bias,c.bias,c.bias,c.bias]:(r=[c.bias[0],c.bias[1],c.bias[2],0],c.bias[3]!==void 0&&(r[3]=c.bias[3]));let i=l*u,a=0,p=i,h=i*2,m=-1;d==="RGBA"?(a=0,p=i,h=i*2,m=i*3):d==="RGB"?(a=0,p=i,h=i*2):d==="RBG"&&(a=0,h=i,p=i*2);for(let b=0;b{let n=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),o;if(n!=null){let u,l,d;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[1],d=e.dims[3]):(u=e.dims[3],l=e.dims[2],d=e.dims[1]);let c=t!==void 0&&t.format!==void 0?t.format:"RGB",s=t?.norm,r,i;s===void 0||s.mean===void 0?r=[255,255,255,255]:typeof s.mean=="number"?r=[s.mean,s.mean,s.mean,s.mean]:(r=[s.mean[0],s.mean[1],s.mean[2],255],s.mean[3]!==void 0&&(r[3]=s.mean[3])),s===void 0||s.bias===void 0?i=[0,0,0,0]:typeof s.bias=="number"?i=[s.bias,s.bias,s.bias,s.bias]:(i=[s.bias[0],s.bias[1],s.bias[2],0],s.bias[3]!==void 0&&(i[3]=s.bias[3]));let a=l*u;if(t!==void 0&&(t.format!==void 0&&d===4&&t.format!=="RGBA"||d===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let p=4,h=0,m=1,b=2,$=3,_=0,y=a,T=a*2,S=-1;c==="RGBA"?(_=0,y=a,T=a*2,S=a*3):c==="RGB"?(_=0,y=a,T=a*2):c==="RBG"&&(_=0,T=a,y=a*2),o=n.createImageData(u,l);for(let O=0;O{"use strict";bl(),Yi=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:n,width:o}=t,u=t.norm??{mean:255,bias:0},l,d;typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:l=[u.mean[0],u.mean[1],u.mean[2],u.mean[3]??255],typeof u.bias=="number"?d=[u.bias,u.bias,u.bias,u.bias]:d=[u.bias[0],u.bias[1],u.bias[2],u.bias[3]??0];let c=t.format!==void 0?t.format:"RGBA",s=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",r=n*o,i=s==="RGBA"?new Float32Array(r*4):new Float32Array(r*3),a=4,p=0,h=1,m=2,b=3,$=0,_=r,y=r*2,T=-1;c==="RGB"&&(a=3,p=0,h=1,m=2,b=-1),s==="RGBA"?T=r*3:s==="RBG"?($=0,y=r,_=r*2):s==="BGR"&&(y=0,_=r,$=r*2);for(let S=0;S{let n=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,o=typeof ImageData<"u"&&e instanceof ImageData,u=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,l=typeof e=="string",d,c=t??{},s=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},r=i=>typeof HTMLCanvasElement<"u"&&i instanceof HTMLCanvasElement||i instanceof OffscreenCanvas?i.getContext("2d"):null;if(n){let i=s();i.width=e.width,i.height=e.height;let a=r(i);if(a!=null){let p=e.height,h=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(p=t.resizedHeight,h=t.resizedWidth),t!==void 0){if(c=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");c.tensorFormat="RGBA",c.height=p,c.width=h}else c.tensorFormat="RGBA",c.height=p,c.width=h;a.drawImage(e,0,0),d=a.getImageData(0,0,h,p).data}else throw new Error("Can not access image data")}else if(o){let i,a;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(i=t.resizedHeight,a=t.resizedWidth):(i=e.height,a=e.width),t!==void 0&&(c=t),c.format="RGBA",c.height=i,c.width=a,t!==void 0){let p=s();p.width=a,p.height=i;let h=r(p);if(h!=null)h.putImageData(e,0,0),d=h.getImageData(0,0,a,i).data;else throw new Error("Can not access image data")}else d=e.data}else if(u){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let i=s();i.width=e.width,i.height=e.height;let a=r(i);if(a!=null){let p=e.height,h=e.width;return a.drawImage(e,0,0,h,p),d=a.getImageData(0,0,h,p).data,c.height=p,c.width=h,Yi(d,c)}else throw new Error("Can not access image data")}else{if(l)return new Promise((i,a)=>{let p=s(),h=r(p);if(!e||!h)return a();let m=new Image;m.crossOrigin="Anonymous",m.src=e,m.onload=()=>{p.width=m.width,p.height=m.height,h.drawImage(m,0,0,p.width,p.height);let b=h.getImageData(0,0,p.width,p.height);c.height=p.height,c.width=p.width,i(Yi(b.data,c))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(d!==void 0)return Yi(d,c);throw new Error("Input data provided is not supported - aborted tensor creation")},Ny=(e,t)=>{let{width:n,height:o,download:u,dispose:l}=t,d=[1,o,n,4];return new ht({location:"texture",type:"float32",texture:e,dims:d,download:u,dispose:l})},zy=(e,t)=>{let{dataType:n,dims:o,download:u,dispose:l}=t;return new ht({location:"gpu-buffer",type:n??"float32",gpuBuffer:e,dims:o,download:u,dispose:l})},My=(e,t)=>{let{dataType:n,dims:o,download:u,dispose:l}=t;return new ht({location:"ml-tensor",type:n??"float32",mlTensor:e,dims:o,download:u,dispose:l})},By=(e,t,n)=>new ht({location:"cpu-pinned",type:e,data:t,dims:n??[t.length]})}),Gr,ri,Ns,Fy,wI=D(()=>{"use strict";Gr=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),ri=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),Ns=!1,Fy=()=>{if(!Ns){Ns=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,n=globalThis.Float16Array,o=typeof n<"u"&&n.from;e&&(Gr.set("int64",BigInt64Array),ri.set(BigInt64Array,"int64")),t&&(Gr.set("uint64",BigUint64Array),ri.set(BigUint64Array,"uint64")),o?(Gr.set("float16",n),ri.set(n,"float16")):Gr.set("float16",Uint16Array)}}}),Ly,jy,vI=D(()=>{"use strict";bl(),Ly=e=>{let t=1;for(let n=0;n{switch(e.location){case"cpu":return new ht(e.type,e.data,t);case"cpu-pinned":return new ht({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new ht({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new ht({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new ht({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),ht,bl=D(()=>{"use strict";yI(),_I(),wI(),vI(),ht=class{constructor(e,t,n){Fy();let o,u;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,o=e.type,u=e.dims,e.location){case"cpu-pinned":{let d=Gr.get(o);if(!d)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(e.data instanceof d))throw new TypeError(`buffer should be of type ${d.name}`);this.cpuData=e.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool"&&o!=="uint4"&&o!=="int4")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint64"&&o!=="int8"&&o!=="uint8"&&o!=="bool"&&o!=="uint4"&&o!=="int4")throw new TypeError(`unsupported type "${o}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let d,c;if(typeof e=="string")if(o=e,c=n,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");d=t}else{let s=Gr.get(e);if(s===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&s===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${s.name} as data.`);e==="uint64"||e==="int64"?d=s.from(t,BigInt):d=s.from(t)}else if(t instanceof s)d=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")d=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&s!==Uint16Array)d=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${o} tensor's data must be type of ${s}`)}else if(c=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let s=typeof e[0];if(s==="string")o="string",d=e;else if(s==="boolean")o="bool",d=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${s}.`)}else if(e instanceof Uint8ClampedArray)o="uint8",d=Uint8Array.from(e);else{let s=ri.get(e.constructor);if(s===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);o=s,d=e}if(c===void 0)c=[d.length];else if(!Array.isArray(c))throw new TypeError("A tensor's dims must be a number array");u=c,this.cpuData=d,this.dataLocation="cpu"}let l=Ly(u);if(this.cpuData&&l!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(l/2)===this.cpuData.length))throw new Error(`Tensor's size(${l}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=u,this.size=l}static async fromImage(e,t){return Ry(e,t)}static fromTexture(e,t){return Ny(e,t)}static fromGpuBuffer(e,t){return zy(e,t)}static fromMLTensor(e,t){return My(e,t)}static fromPinnedBuffer(e,t,n){return By(e,t,n)}toDataURL(e){return Cy(this,e)}toImageData(e){return Dy(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return jy(this,e)}}}),ft,Uy=D(()=>{"use strict";bl(),ft=ht}),wo,zs,jt,Dt,Yr,Jr,Vy=D(()=>{"use strict";Py(),wo=(e,t)=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||console.timeStamp(`${e}::ORT::${t}`)},zs=(e,t)=>{let n=new Error().stack?.split(/\r\n|\r|\n/g)||[],o=!1;for(let u=0;u{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||zs("BEGIN",e)},Dt=e=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||zs("END",e)},Yr=e=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||console.time(`ORT::${e}`)},Jr=e=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||console.timeEnd(`ORT::${e}`)}}),qy,xI=D(()=>{"use strict";Ay(),Uy(),Vy(),qy=class Gy{constructor(t){this.handler=t}async run(t,n,o){jt(),Yr("InferenceSession.run");let u={},l={};if(typeof t!="object"||t===null||t instanceof ft||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof n=="object"){if(n===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(n instanceof ft)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(n)){if(n.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let r of n){if(typeof r!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(r)===-1)throw new RangeError(`'fetches' contains invalid output name: ${r}.`);u[r]=null}if(typeof o=="object"&&o!==null)l=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else{let r=!1,i=Object.getOwnPropertyNames(n);for(let a of this.outputNames)if(i.indexOf(a)!==-1){let p=n[a];(p===null||p instanceof ft)&&(r=!0,d=!1,u[a]=p)}if(r){if(typeof o=="object"&&o!==null)l=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else l=n}}else if(typeof n<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let r of this.inputNames)if(typeof t[r]>"u")throw new Error(`input '${r}' is missing in 'feeds'.`);if(d)for(let r of this.outputNames)u[r]=null;let c=await this.handler.run(t,u,l),s={};for(let r in c)if(Object.hasOwnProperty.call(c,r)){let i=c[r];i instanceof ft?s[r]=i:s[r]=new ft(i.type,i.data,i.dims)}return Jr("InferenceSession.run"),Dt(),s}async release(){return this.handler.dispose()}static async create(t,n,o,u){jt(),Yr("InferenceSession.create");let l,d={};if(typeof t=="string"){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let i=t,a=0,p=t.byteLength;if(typeof n=="object"&&n!==null)d=n;else if(typeof n=="number"){if(a=n,!Number.isSafeInteger(a))throw new RangeError("'byteOffset' must be an integer.");if(a<0||a>=i.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${i.byteLength}).`);if(p=t.byteLength-a,typeof o=="number"){if(p=o,!Number.isSafeInteger(p))throw new RangeError("'byteLength' must be an integer.");if(p<=0||a+p>i.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${i.byteLength-a}].`);if(typeof u=="object"&&u!==null)d=u;else if(typeof u<"u")throw new TypeError("'options' must be an object.")}else if(typeof o<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof n<"u")throw new TypeError("'options' must be an object.");l=new Uint8Array(i,a,p)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[c,s]=await Ey(d),r=await c.createInferenceSessionHandler(l,s);return Jr("InferenceSession.create"),Dt(),new Gy(r)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),Do,$I=D(()=>{"use strict";xI(),Do=qy}),TI=D(()=>{"use strict"}),SI=D(()=>{"use strict"}),II=D(()=>{"use strict"}),OI=D(()=>{"use strict"}),EI={};nn(EI,{InferenceSession:()=>Do,TRACE:()=>wo,TRACE_EVENT_BEGIN:()=>Yr,TRACE_EVENT_END:()=>Jr,TRACE_FUNC_BEGIN:()=>jt,TRACE_FUNC_END:()=>Dt,Tensor:()=>ft,env:()=>pe,registerBackend:()=>Zr});var it=D(()=>{"use strict";mI(),bI(),$I(),Uy(),TI(),SI(),Vy(),II(),OI()});function Hr(e,t,n,o){if(t===void 0)return AI(e);if(n===void 0)Ji(e,t,1);else if(typeof n=="number"&&o===void 0)Ji(e,t,n);else if(typeof n=="string"&&o===void 0)Ji(e,n,1,t);else if(typeof n=="string"&&typeof o=="number")Ji(e,n,o,t);else throw new TypeError("input is valid")}function AI(e){return{verbose:Hr.verbose.bind(null,e),info:Hr.info.bind(null,e),warning:Hr.warning.bind(null,e),error:Hr.error.bind(null,e),fatal:Hr.fatal.bind(null,e)}}function Ji(e,t,n,o){let u=yn[o||""]||yn[""];pu[e]{"use strict";Tp=class{log(e,t,n){}},Sp=class{log(e,t,n){console.log(`${this.color(e)} ${n?"\x1B[35m"+n+"\x1B[0m ":""}${t}`)}color(e){switch(e){case"verbose":return"\x1B[34;40mv\x1B[0m";case"info":return"\x1B[32mi\x1B[0m";case"warning":return"\x1B[30;43mw\x1B[0m";case"error":return"\x1B[31;40me\x1B[0m";case"fatal":return"\x1B[101mf\x1B[0m";default:throw new Error(`unsupported severity: ${e}`)}}},pu={verbose:1e3,info:2e3,warning:4e3,error:5e3,fatal:6e3},Hy={none:new Tp,console:new Sp},Ms={provider:"console",minimalSeverity:"warning",logDateTime:!0,logSourceLocation:!1},yn={"":Ms},(e=>{function t(r,i){e("verbose",r,i)}e.verbose=t;function n(r,i){e("info",r,i)}e.info=n;function o(r,i){e("warning",r,i)}e.warning=o;function u(r,i){e("error",r,i)}e.error=u;function l(r,i){e("fatal",r,i)}e.fatal=l;function d(r){yn={},c("",r||{})}e.reset=d;function c(r,i){if(r==="*")d(i);else{let a=yn[r]||Ms;yn[r]={provider:i.provider||a.provider,minimalSeverity:i.minimalSeverity||a.minimalSeverity,logDateTime:i.logDateTime===void 0?a.logDateTime:i.logDateTime,logSourceLocation:i.logSourceLocation===void 0?a.logSourceLocation:i.logSourceLocation}}}e.set=c;function s(r){let i={};r.logLevel&&(i.minimalSeverity=r.logLevel),c("",i)}e.setWithEnv=s})(Hr||={}),We=Hr,Bs=class{constructor(e,t,n,o,u,l){this.category=e,this.name=t,this.startTime=n,this.endCallback=o,this.timer=u,this.ctx=l}async end(){return this.endCallback(this)}async checkTimer(){if(this.ctx===void 0||this.timer===void 0)throw new Error("No webgl timer found");return this.ctx.endTimer(),this.ctx.waitForQueryAndGetTime(this.timer)}},Fs=class{constructor(e,t,n,o){this.category=e,this.name=t,this.startTime=n,this.endTime=o}},Wy=class{constructor(e,t,n){this._started=!1,this._flushPointer=0,this._started=!1,this._maxNumberEvents=e===void 0?1e4:e,this._flushBatchSize=t===void 0?10:t,this._flushIntervalInMilliseconds=n===void 0?5e3:n}static create(e){return e===void 0?new this:new this(e.maxNumberEvents,e.flushBatchSize,e.flushIntervalInMilliseconds)}start(){this._started=!0,this._timingEvents=[],this._flushTime=Ln(),this._flushPointer=0}stop(){for(this._started=!1;this._flushPointer{d.then(async r=>{u&&await u.end(),c(r)},async r=>{u&&await u.end(),s(r)})});if(!l&&u){let c=u.end();if(c&&typeof c.then=="function")return new Promise((s,r)=>{c.then(()=>{s(d)},i=>{r(i)})})}return d}begin(e,t,n){if(!this._started)throw new Error("profiler is not started yet");if(n===void 0){let o=Ln();return this.flush(o),new Bs(e,t,o,u=>this.endSync(u))}else{let o=n.beginTimer();return new Bs(e,t,0,async u=>this.end(u),o,n)}}async end(e){let t=await e.checkTimer();this._timingEvents.length=this._flushBatchSize||e-this._flushTime>=this._flushIntervalInMilliseconds){for(let t=this._flushPointer;this._flushPointerperformance.now():Date.now});function kI(e,t,n){for(let o of n){let u=o[0],l=o[1],d=o[2],c=o[3],s=o[4];if(e.opType===u){for(let r of t)if((r.domain===l||r.domain==="ai.onnx"&&l==="")&&PI(r.version,d))return{opImpl:c,opInit:s}}}throw new TypeError(`cannot resolve operator '${e.opType}' with opsets: ${t.map(o=>`${o.domain||"ai.onnx"} v${o.version}`).join(", ")}`)}function PI(e,t){if(t.endsWith("+")){let n=Number.parseInt(t.substring(0,t.length-1),10);return!isNaN(n)&&n<=e}else if(t.split("-").length===2){let n=t.split("-"),o=Number.parseInt(n[0],10),u=Number.parseInt(n[1],10);return!isNaN(o)&&!isNaN(u)&&o<=e&&e<=u}else return Number.parseInt(t,10)===e}var CI=D(()=>{"use strict"}),DI=oe(e=>{"use strict";e.__esModule=!0;var t=(function(){function n(o){if(!o)throw new TypeError("Invalid argument; `value` has no value.");this.value=n.EMPTY,o&&n.isGuid(o)&&(this.value=o)}return n.isGuid=function(o){var u=o.toString();return o&&(o instanceof n||n.validator.test(u))},n.create=function(){return new n([n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-"))},n.createEmpty=function(){return new n("emptyguid")},n.parse=function(o){return new n(o)},n.raw=function(){return[n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-")},n.gen=function(o){for(var u="",l=0;l>>=0,(u=0<=e&&e<256)&&(o=fu[e],o)?o:(n=Ee(e,0,!0),u&&(fu[e]=n),n)):(e|=0,(u=-128<=e&&e<128)&&(o=hu[e],o)?o:(n=Ee(e,e<0?-1:0,!1),u&&(hu[e]=n),n))}function $t(e,t){if(isNaN(e))return t?Ft:xt;if(t){if(e<0)return Ft;if(e>=mu)return yu}else{if(e<=-gu)return st;if(e+1>=gu)return bu}return e<0?$t(-e,t).neg():Ee(e%Wr|0,e/Wr|0,t)}function Ee(e,t,n){return new Re(e,t,n)}function yl(e,t,n){if(e.length===0)throw Error("empty string");if(typeof t=="number"?(n=t,t=!1):t=!!t,e==="NaN"||e==="Infinity"||e==="+Infinity"||e==="-Infinity")return t?Ft:xt;if(n=n||10,n<2||360)throw Error("interior hyphen");if(o===0)return yl(e.substring(1),t,n).neg();for(var u=$t(di(n,8)),l=xt,d=0;d{lt=null;try{lt=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}Re.prototype.__isLong__,Object.defineProperty(Re.prototype,"__isLong__",{value:!0}),Re.isLong=nt,hu={},fu={},Re.fromInt=Or,Re.fromNumber=$t,Re.fromBits=Ee,di=Math.pow,Re.fromString=yl,Re.fromValue=yt,Ls=65536,Op=1<<24,Wr=Ls*Ls,mu=Wr*Wr,gu=mu/2,js=Or(Op),xt=Or(0),Re.ZERO=xt,Ft=Or(0,!0),Re.UZERO=Ft,Er=Or(1),Re.ONE=Er,Us=Or(1,!0),Re.UONE=Us,Qi=Or(-1),Re.NEG_ONE=Qi,bu=Ee(-1,2147483647,!1),Re.MAX_VALUE=bu,yu=Ee(-1,-1,!0),Re.MAX_UNSIGNED_VALUE=yu,st=Ee(0,-2147483648,!1),Re.MIN_VALUE=st,W=Re.prototype,W.toInt=function(){return this.unsigned?this.low>>>0:this.low},W.toNumber=function(){return this.unsigned?(this.high>>>0)*Wr+(this.low>>>0):this.high*Wr+(this.low>>>0)},W.toString=function(e){if(e=e||10,e<2||36>>0,r=s.toString(e);if(l=c,l.isZero())return r+d;for(;r.length<6;)r="0"+r;d=""+r+d}},W.getHighBits=function(){return this.high},W.getHighBitsUnsigned=function(){return this.high>>>0},W.getLowBits=function(){return this.low},W.getLowBitsUnsigned=function(){return this.low>>>0},W.getNumBitsAbs=function(){if(this.isNegative())return this.eq(st)?64:this.neg().getNumBitsAbs();for(var e=this.high!=0?this.high:this.low,t=31;t>0&&(e&1<>21;return e?this.unsigned?!1:e===-1&&!(this.low===0&&this.high===-2097152):!0},W.isZero=function(){return this.high===0&&this.low===0},W.eqz=W.isZero,W.isNegative=function(){return!this.unsigned&&this.high<0},W.isPositive=function(){return this.unsigned||this.high>=0},W.isOdd=function(){return(this.low&1)===1},W.isEven=function(){return(this.low&1)===0},W.equals=function(e){return nt(e)||(e=yt(e)),this.unsigned!==e.unsigned&&this.high>>>31===1&&e.high>>>31===1?!1:this.high===e.high&&this.low===e.low},W.eq=W.equals,W.notEquals=function(e){return!this.eq(e)},W.neq=W.notEquals,W.ne=W.notEquals,W.lessThan=function(e){return this.comp(e)<0},W.lt=W.lessThan,W.lessThanOrEqual=function(e){return this.comp(e)<=0},W.lte=W.lessThanOrEqual,W.le=W.lessThanOrEqual,W.greaterThan=function(e){return this.comp(e)>0},W.gt=W.greaterThan,W.greaterThanOrEqual=function(e){return this.comp(e)>=0},W.gte=W.greaterThanOrEqual,W.ge=W.greaterThanOrEqual,W.compare=function(e){if(nt(e)||(e=yt(e)),this.eq(e))return 0;var t=this.isNegative(),n=e.isNegative();return t&&!n?-1:!t&&n?1:this.unsigned?e.high>>>0>this.high>>>0||e.high===this.high&&e.low>>>0>this.low>>>0?-1:1:this.sub(e).isNegative()?-1:1},W.comp=W.compare,W.negate=function(){return!this.unsigned&&this.eq(st)?st:this.not().add(Er)},W.neg=W.negate,W.add=function(e){nt(e)||(e=yt(e));var t=this.high>>>16,n=this.high&65535,o=this.low>>>16,u=this.low&65535,l=e.high>>>16,d=e.high&65535,c=e.low>>>16,s=e.low&65535,r=0,i=0,a=0,p=0;return p+=u+s,a+=p>>>16,p&=65535,a+=o+c,i+=a>>>16,a&=65535,i+=n+d,r+=i>>>16,i&=65535,r+=t+l,r&=65535,Ee(a<<16|p,r<<16|i,this.unsigned)},W.subtract=function(e){return nt(e)||(e=yt(e)),this.add(e.neg())},W.sub=W.subtract,W.multiply=function(e){if(this.isZero())return this;if(nt(e)||(e=yt(e)),lt){var t=lt.mul(this.low,this.high,e.low,e.high);return Ee(t,lt.get_high(),this.unsigned)}if(e.isZero())return this.unsigned?Ft:xt;if(this.eq(st))return e.isOdd()?st:xt;if(e.eq(st))return this.isOdd()?st:xt;if(this.isNegative())return e.isNegative()?this.neg().mul(e.neg()):this.neg().mul(e).neg();if(e.isNegative())return this.mul(e.neg()).neg();if(this.lt(js)&&e.lt(js))return $t(this.toNumber()*e.toNumber(),this.unsigned);var n=this.high>>>16,o=this.high&65535,u=this.low>>>16,l=this.low&65535,d=e.high>>>16,c=e.high&65535,s=e.low>>>16,r=e.low&65535,i=0,a=0,p=0,h=0;return h+=l*r,p+=h>>>16,h&=65535,p+=u*r,a+=p>>>16,p&=65535,p+=l*s,a+=p>>>16,p&=65535,a+=o*r,i+=a>>>16,a&=65535,a+=u*s,i+=a>>>16,a&=65535,a+=l*c,i+=a>>>16,a&=65535,i+=n*r+o*s+u*c+l*d,i&=65535,Ee(p<<16|h,i<<16|a,this.unsigned)},W.mul=W.multiply,W.divide=function(e){if(nt(e)||(e=yt(e)),e.isZero())throw Error("division by zero");if(lt){if(!this.unsigned&&this.high===-2147483648&&e.low===-1&&e.high===-1)return this;var t=(this.unsigned?lt.div_u:lt.div_s)(this.low,this.high,e.low,e.high);return Ee(t,lt.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?Ft:xt;var n,o,u;if(this.unsigned){if(e.unsigned||(e=e.toUnsigned()),e.gt(this))return Ft;if(e.gt(this.shru(1)))return Us;u=Ft}else{if(this.eq(st)){if(e.eq(Er)||e.eq(Qi))return st;if(e.eq(st))return Er;var l=this.shr(1);return n=l.div(e).shl(1),n.eq(xt)?e.isNegative()?Er:Qi:(o=this.sub(e.mul(n)),u=n.add(o.div(e)),u)}else if(e.eq(st))return this.unsigned?Ft:xt;if(this.isNegative())return e.isNegative()?this.neg().div(e.neg()):this.neg().div(e).neg();if(e.isNegative())return this.div(e.neg()).neg();u=xt}for(o=this;o.gte(e);){n=Math.max(1,Math.floor(o.toNumber()/e.toNumber()));for(var d=Math.ceil(Math.log(n)/Math.LN2),c=d<=48?1:di(2,d-48),s=$t(n),r=s.mul(e);r.isNegative()||r.gt(o);)n-=c,s=$t(n,this.unsigned),r=s.mul(e);s.isZero()&&(s=Er),u=u.add(s),o=o.sub(r)}return u},W.div=W.divide,W.modulo=function(e){if(nt(e)||(e=yt(e)),lt){var t=(this.unsigned?lt.rem_u:lt.rem_s)(this.low,this.high,e.low,e.high);return Ee(t,lt.get_high(),this.unsigned)}return this.sub(this.div(e).mul(e))},W.mod=W.modulo,W.rem=W.modulo,W.not=function(){return Ee(~this.low,~this.high,this.unsigned)},W.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},W.clz=W.countLeadingZeros,W.countTrailingZeros=function(){return this.low?Ip(this.low):Ip(this.high)+32},W.ctz=W.countTrailingZeros,W.and=function(e){return nt(e)||(e=yt(e)),Ee(this.low&e.low,this.high&e.high,this.unsigned)},W.or=function(e){return nt(e)||(e=yt(e)),Ee(this.low|e.low,this.high|e.high,this.unsigned)},W.xor=function(e){return nt(e)||(e=yt(e)),Ee(this.low^e.low,this.high^e.high,this.unsigned)},W.shiftLeft=function(e){return nt(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ee(this.low<>>32-e,this.unsigned):Ee(0,this.low<>>e|this.high<<32-e,this.high>>e,this.unsigned):Ee(this.high>>e-32,this.high>=0?0:-1,this.unsigned)},W.shr=W.shiftRight,W.shiftRightUnsigned=function(e){return nt(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ee(this.low>>>e|this.high<<32-e,this.high>>>e,this.unsigned):e===32?Ee(this.high,0,this.unsigned):Ee(this.high>>>e-32,0,this.unsigned)},W.shru=W.shiftRightUnsigned,W.shr_u=W.shiftRightUnsigned,W.rotateLeft=function(e){var t;return nt(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ee(this.high,this.low,this.unsigned):e<32?(t=32-e,Ee(this.low<>>t,this.high<>>t,this.unsigned)):(e-=32,t=32-e,Ee(this.high<>>t,this.low<>>t,this.unsigned))},W.rotl=W.rotateLeft,W.rotateRight=function(e){var t;return nt(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ee(this.high,this.low,this.unsigned):e<32?(t=32-e,Ee(this.high<>>e,this.low<>>e,this.unsigned)):(e-=32,t=32-e,Ee(this.low<>>e,this.high<>>e,this.unsigned))},W.rotr=W.rotateRight,W.toSigned=function(){return this.unsigned?Ee(this.low,this.high,!1):this},W.toUnsigned=function(){return this.unsigned?this:Ee(this.low,this.high,!0)},W.toBytes=function(e){return e?this.toBytesLE():this.toBytesBE()},W.toBytesLE=function(){var e=this.high,t=this.low;return[t&255,t>>>8&255,t>>>16&255,t>>>24,e&255,e>>>8&255,e>>>16&255,e>>>24]},W.toBytesBE=function(){var e=this.high,t=this.low;return[e>>>24,e>>>16&255,e>>>8&255,e&255,t>>>24,t>>>16&255,t>>>8&255,t&255]},Re.fromBytes=function(e,t,n){return n?Re.fromBytesLE(e,t):Re.fromBytesBE(e,t)},Re.fromBytesLE=function(e,t){return new Re(e[0]|e[1]<<8|e[2]<<16|e[3]<<24,e[4]|e[5]<<8|e[6]<<16|e[7]<<24,t)},Re.fromBytesBE=function(e,t){return new Re(e[4]<<24|e[5]<<16|e[6]<<8|e[7],e[0]<<24|e[1]<<16|e[2]<<8|e[3],t)},typeof BigInt=="function"&&(Re.fromBigInt=function(e,t){var n=Number(BigInt.asIntN(32,e)),o=Number(BigInt.asIntN(32,e>>BigInt(32)));return Ee(n,o,t)},Re.fromValue=function(e,t){return typeof e=="bigint"?Re.fromBigInt(e,t):yt(e,t)},W.toBigInt=function(){var e=BigInt(this.low>>>0),t=BigInt(this.unsigned?this.high>>>0:this.high);return t<{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ArgType=void 0;var t;(function(n){n[n.INPUT=0]="INPUT",n[n.OUTPUT=1]="OUTPUT"})(t||(e.ArgType=t={}))}),_l=oe(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.SIZE_PREFIX_LENGTH=e.FILE_IDENTIFIER_LENGTH=e.SIZEOF_INT=e.SIZEOF_SHORT=void 0,e.SIZEOF_SHORT=2,e.SIZEOF_INT=4,e.FILE_IDENTIFIER_LENGTH=4,e.SIZE_PREFIX_LENGTH=4}),Zy=oe(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.isLittleEndian=e.float64=e.float32=e.int32=void 0,e.int32=new Int32Array(2),e.float32=new Float32Array(e.int32.buffer),e.float64=new Float64Array(e.int32.buffer),e.isLittleEndian=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1}),Yy=oe(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=void 0;var t;(function(n){n[n.UTF8_BYTES=1]="UTF8_BYTES",n[n.UTF16_STRING=2]="UTF16_STRING"})(t||(e.Encoding=t={}))}),Jy=oe(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ByteBuffer=void 0;var t=_l(),n=Yy(),o=Zy(),u=class Qy{constructor(d){this.bytes_=d,this.position_=0,this.text_decoder_=new TextDecoder}static allocate(d){return new Qy(new Uint8Array(d))}clear(){this.position_=0}bytes(){return this.bytes_}position(){return this.position_}setPosition(d){this.position_=d}capacity(){return this.bytes_.length}readInt8(d){return this.readUint8(d)<<24>>24}readUint8(d){return this.bytes_[d]}readInt16(d){return this.readUint16(d)<<16>>16}readUint16(d){return this.bytes_[d]|this.bytes_[d+1]<<8}readInt32(d){return this.bytes_[d]|this.bytes_[d+1]<<8|this.bytes_[d+2]<<16|this.bytes_[d+3]<<24}readUint32(d){return this.readInt32(d)>>>0}readInt64(d){return BigInt.asIntN(64,BigInt(this.readUint32(d))+(BigInt(this.readUint32(d+4))<>8}writeUint16(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8}writeInt32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeUint32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeInt64(d,c){this.writeInt32(d,Number(BigInt.asIntN(32,c))),this.writeInt32(d+4,Number(BigInt.asIntN(32,c>>BigInt(32))))}writeUint64(d,c){this.writeUint32(d,Number(BigInt.asUintN(32,c))),this.writeUint32(d+4,Number(BigInt.asUintN(32,c>>BigInt(32))))}writeFloat32(d,c){o.float32[0]=c,this.writeInt32(d,o.int32[0])}writeFloat64(d,c){o.float64[0]=c,this.writeInt32(d,o.int32[o.isLittleEndian?0:1]),this.writeInt32(d+4,o.int32[o.isLittleEndian?1:0])}getBufferIdentifier(){if(this.bytes_.length{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Builder=void 0;var t=Jy(),n=_l(),o=class e0{constructor(l){this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null,this.text_encoder=new TextEncoder;let d;l?d=l:d=1024,this.bb=t.ByteBuffer.allocate(d),this.space=d}clear(){this.bb.clear(),this.space=this.bb.capacity(),this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null}forceDefaults(l){this.force_defaults=l}dataBuffer(){return this.bb}asUint8Array(){return this.bb.bytes().subarray(this.bb.position(),this.bb.position()+this.offset())}prep(l,d){l>this.minalign&&(this.minalign=l);let c=~(this.bb.capacity()-this.space+d)+1&l-1;for(;this.space=0&&this.vtable[d]==0;d--);let c=d+1;for(;d>=0;d--)this.addInt16(this.vtable[d]!=0?l-this.vtable[d]:0);let s=2;this.addInt16(l-this.object_start);let r=(c+s)*n.SIZEOF_SHORT;this.addInt16(r);let i=0,a=this.space;e:for(d=0;d=0;i--)this.writeInt8(r.charCodeAt(i))}this.prep(this.minalign,n.SIZEOF_INT+s),this.addOffset(l),s&&this.addInt32(this.bb.capacity()-this.space),this.bb.setPosition(this.space)}finishSizePrefixed(l,d){this.finish(l,d,!0)}requiredField(l,d){let c=this.bb.capacity()-l,s=c-this.bb.readInt32(c);if(!(d{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=e.ByteBuffer=e.Builder=e.isLittleEndian=e.int32=e.float64=e.float32=e.SIZE_PREFIX_LENGTH=e.SIZEOF_SHORT=e.SIZEOF_INT=e.FILE_IDENTIFIER_LENGTH=void 0;var t=_l();Object.defineProperty(e,"FILE_IDENTIFIER_LENGTH",{enumerable:!0,get:function(){return t.FILE_IDENTIFIER_LENGTH}}),Object.defineProperty(e,"SIZEOF_INT",{enumerable:!0,get:function(){return t.SIZEOF_INT}}),Object.defineProperty(e,"SIZEOF_SHORT",{enumerable:!0,get:function(){return t.SIZEOF_SHORT}}),Object.defineProperty(e,"SIZE_PREFIX_LENGTH",{enumerable:!0,get:function(){return t.SIZE_PREFIX_LENGTH}});var n=Zy();Object.defineProperty(e,"float32",{enumerable:!0,get:function(){return n.float32}}),Object.defineProperty(e,"float64",{enumerable:!0,get:function(){return n.float64}}),Object.defineProperty(e,"int32",{enumerable:!0,get:function(){return n.int32}}),Object.defineProperty(e,"isLittleEndian",{enumerable:!0,get:function(){return n.isLittleEndian}});var o=RI();Object.defineProperty(e,"Builder",{enumerable:!0,get:function(){return o.Builder}});var u=Jy();Object.defineProperty(e,"ByteBuffer",{enumerable:!0,get:function(){return u.ByteBuffer}});var l=Yy();Object.defineProperty(e,"Encoding",{enumerable:!0,get:function(){return l.Encoding}})}),t0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.AttributeType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.INT=2]="INT",n[n.STRING=3]="STRING",n[n.TENSOR=4]="TENSOR",n[n.GRAPH=5]="GRAPH",n[n.FLOATS=6]="FLOATS",n[n.INTS=7]="INTS",n[n.STRINGS=8]="STRINGS",n[n.TENSORS=9]="TENSORS",n[n.GRAPHS=10]="GRAPHS",n[n.SPARSE_TENSOR=11]="SPARSE_TENSOR",n[n.SPARSE_TENSORS=12]="SPARSE_TENSORS"})(t||(e.AttributeType=t={}))}),n0=oe(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=void 0;var t;(function(n){n[n.Primitive=0]="Primitive",n[n.Fused=1]="Fused"})(t||(e.NodeType=t={}))}),i0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(i[a]);return r.endVector()}static startInputsVector(r,i){r.startVector(4,i,4)}static addOutputs(r,i){r.addFieldOffset(9,i,0)}static createOutputsVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addOffset(i[a]);return r.endVector()}static startOutputsVector(r,i){r.startVector(4,i,4)}static addAttributes(r,i){r.addFieldOffset(10,i,0)}static createAttributesVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addOffset(i[a]);return r.endVector()}static startAttributesVector(r,i){r.startVector(4,i,4)}static addInputArgCounts(r,i){r.addFieldOffset(11,i,0)}static createInputArgCountsVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addInt32(i[a]);return r.endVector()}static startInputArgCountsVector(r,i){r.startVector(4,i,4)}static addImplicitInputs(r,i){r.addFieldOffset(12,i,0)}static createImplicitInputsVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addOffset(i[a]);return r.endVector()}static startImplicitInputsVector(r,i){r.startVector(4,i,4)}static endNode(r){return r.endObject()}static createNode(r,i,a,p,h,m,b,$,_,y,T,S,O,A){return Qe.startNode(r),Qe.addName(r,i),Qe.addDocString(r,a),Qe.addDomain(r,p),Qe.addSinceVersion(r,h),Qe.addIndex(r,m),Qe.addOpType(r,b),Qe.addType(r,$),Qe.addExecutionProviderType(r,_),Qe.addInputs(r,y),Qe.addOutputs(r,T),Qe.addAttributes(r,S),Qe.addInputArgCounts(r,O),Qe.addImplicitInputs(r,A),Qe.endNode(r)}};e.Node=c}),o0=oe(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.EdgeEnd=void 0;var t=class{constructor(){this.bb=null,this.bb_pos=0}__init(n,o){return this.bb_pos=n,this.bb=o,this}nodeIndex(){return this.bb.readUint32(this.bb_pos)}srcArgIndex(){return this.bb.readInt32(this.bb_pos+4)}dstArgIndex(){return this.bb.readInt32(this.bb_pos+8)}static sizeOf(){return 12}static createEdgeEnd(n,o,u,l){return n.prep(4,12),n.writeInt32(l),n.writeInt32(u),n.writeInt32(o),n.offset()}};e.EdgeEnd=t}),s0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addNumInputs(c,s){c.addFieldInt32(1,s,0)}static addNumOutputs(c,s){c.addFieldInt32(2,s,0)}static addHasVariadicInput(c,s){c.addFieldInt8(3,+s,0)}static addHasVariadicOutput(c,s){c.addFieldInt8(4,+s,0)}static addNumVariadicInputs(c,s){c.addFieldInt32(5,s,0)}static addNumVariadicOutputs(c,s){c.addFieldInt32(6,s,0)}static endNodesToOptimizeIndices(c){return c.endObject()}static createNodesToOptimizeIndices(c,s,r,i,a,p,h,m){return wt.startNodesToOptimizeIndices(c),wt.addNodeIndices(c,s),wt.addNumInputs(c,r),wt.addNumOutputs(c,i),wt.addHasVariadicInput(c,a),wt.addHasVariadicOutput(c,p),wt.addNumVariadicInputs(c,h),wt.addNumVariadicOutputs(c,m),wt.endNodesToOptimizeIndices(c)}};e.NodesToOptimizeIndices=l}),u0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startProducedOpIdsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecord(s){return s.endObject()}};e.RuntimeOptimizationRecord=d}),l0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startRuntimeOptimizationRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecordContainerEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createRuntimeOptimizationRecordContainerEntry(s,r,i){return Nr.startRuntimeOptimizationRecordContainerEntry(s),Nr.addOptimizerName(s,r),Nr.addRuntimeOptimizationRecords(s,i),Nr.endRuntimeOptimizationRecordContainerEntry(s)}};e.RuntimeOptimizationRecordContainerEntry=d}),d0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizations(s){return s.endObject()}static createRuntimeOptimizations(s,r){return fn.startRuntimeOptimizations(s),fn.addRecords(s,r),fn.endRuntimeOptimizations(s)}};e.RuntimeOptimizations=d}),Ro=oe(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TensorDataType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.UINT8=2]="UINT8",n[n.INT8=3]="INT8",n[n.UINT16=4]="UINT16",n[n.INT16=5]="INT16",n[n.INT32=6]="INT32",n[n.INT64=7]="INT64",n[n.STRING=8]="STRING",n[n.BOOL=9]="BOOL",n[n.FLOAT16=10]="FLOAT16",n[n.DOUBLE=11]="DOUBLE",n[n.UINT32=12]="UINT32",n[n.UINT64=13]="UINT64",n[n.COMPLEX64=14]="COMPLEX64",n[n.COMPLEX128=15]="COMPLEX128",n[n.BFLOAT16=16]="BFLOAT16",n[n.FLOAT8E4M3FN=17]="FLOAT8E4M3FN",n[n.FLOAT8E4M3FNUZ=18]="FLOAT8E4M3FNUZ",n[n.FLOAT8E5M2=19]="FLOAT8E5M2",n[n.FLOAT8E5M2FNUZ=20]="FLOAT8E5M2FNUZ"})(t||(e.TensorDataType=t={}))}),No=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addInt64(r[i]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static addDataType(s,r){s.addFieldInt32(3,r,l.TensorDataType.UNDEFINED)}static addRawData(s,r){s.addFieldOffset(4,r,0)}static createRawDataVector(s,r){s.startVector(1,r.length,1);for(let i=r.length-1;i>=0;i--)s.addInt8(r[i]);return s.endVector()}static startRawDataVector(s,r){s.startVector(1,r,1)}static addStringData(s,r){s.addFieldOffset(5,r,0)}static createStringDataVector(s,r){s.startVector(4,r.length,4);for(let i=r.length-1;i>=0;i--)s.addOffset(r[i]);return s.endVector()}static startStringDataVector(s,r){s.startVector(4,r,4)}static addExternalDataOffset(s,r){s.addFieldInt64(6,r,BigInt("-1"))}static endTensor(s){return s.endObject()}static createTensor(s,r,i,a,p,h,m,b){return vt.startTensor(s),vt.addName(s,r),vt.addDocString(s,i),vt.addDims(s,a),vt.addDataType(s,p),vt.addRawData(s,h),vt.addStringData(s,m),vt.addExternalDataOffset(s,b),vt.endTensor(s)}};e.Tensor=d}),c0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addInt64(r[i]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static endSparseTensor(s){return s.endObject()}};e.SparseTensor=d}),p0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.DimensionValueType=void 0;var t;(function(n){n[n.UNKNOWN=0]="UNKNOWN",n[n.VALUE=1]="VALUE",n[n.PARAM=2]="PARAM"})(t||(e.DimensionValueType=t={}))}),m0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startDimVector(s,r){s.startVector(4,r,4)}static endShape(s){return s.endObject()}static createShape(s,r){return gn.startShape(s),gn.addDim(s,r),gn.endShape(s)}};e.Shape=d}),y0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TypeInfoValue=void 0,e.unionToTypeInfoValue=l,e.unionListToTypeInfoValue=d;var t=p0(),n=h0(),o=y0(),u;(function(c){c[c.NONE=0]="NONE",c[c.tensor_type=1]="tensor_type",c[c.sequence_type=2]="sequence_type",c[c.map_type=3]="map_type"})(u||(e.TypeInfoValue=u={}));function l(c,s){switch(u[c]){case"NONE":return null;case"tensor_type":return s(new o.TensorTypeAndShape);case"sequence_type":return s(new n.SequenceType);case"map_type":return s(new t.MapType);default:return null}}function d(c,s,r){switch(u[c]){case"NONE":return null;case"tensor_type":return s(r,new o.TensorTypeAndShape);case"sequence_type":return s(r,new n.SequenceType);case"map_type":return s(r,new t.MapType);default:return null}}}),zo=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(p,h,m,b){b===void 0&&(b=m);var $=Object.getOwnPropertyDescriptor(h,m);(!$||("get"in $?!h.__esModule:$.writable||$.configurable))&&($={enumerable:!0,get:function(){return h[m]}}),Object.defineProperty(p,b,$)}:function(p,h,m,b){b===void 0&&(b=m),p[b]=h[m]}),n=e&&e.__setModuleDefault||(Object.create?function(p,h){Object.defineProperty(p,"default",{enumerable:!0,value:h})}:function(p,h){p.default=h}),o=e&&e.__importStar||(function(){var p=function(h){return p=Object.getOwnPropertyNames||function(m){var b=[];for(var $ in m)Object.prototype.hasOwnProperty.call(m,$)&&(b[b.length]=$);return b},p(h)};return function(h){if(h&&h.__esModule)return h;var m={};if(h!=null)for(var b=p(h),$=0;$=0;b--)h.addOffset(m[b]);return h.endVector()}static startInitializersVector(h,m){h.startVector(4,m,4)}static addNodeArgs(h,m){h.addFieldOffset(1,m,0)}static createNodeArgsVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startNodeArgsVector(h,m){h.startVector(4,m,4)}static addNodes(h,m){h.addFieldOffset(2,m,0)}static createNodesVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startNodesVector(h,m){h.startVector(4,m,4)}static addMaxNodeIndex(h,m){h.addFieldInt32(3,m,0)}static addNodeEdges(h,m){h.addFieldOffset(4,m,0)}static createNodeEdgesVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startNodeEdgesVector(h,m){h.startVector(4,m,4)}static addInputs(h,m){h.addFieldOffset(5,m,0)}static createInputsVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startInputsVector(h,m){h.startVector(4,m,4)}static addOutputs(h,m){h.addFieldOffset(6,m,0)}static createOutputsVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startOutputsVector(h,m){h.startVector(4,m,4)}static addSparseInitializers(h,m){h.addFieldOffset(7,m,0)}static createSparseInitializersVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startSparseInitializersVector(h,m){h.startVector(4,m,4)}static addRuntimeOptimizations(h,m){h.addFieldOffset(8,m,0)}static endGraph(h){return h.endObject()}};e.Graph=a}),v0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,i,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(i,a);(!h||("get"in h?!i.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return i[a]}}),Object.defineProperty(r,p,h)}:function(r,i,a,p){p===void 0&&(p=a),r[p]=i[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,i){Object.defineProperty(r,"default",{enumerable:!0,value:i})}:function(r,i){r.default=i}),o=e&&e.__importStar||(function(){var r=function(i){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(i)};return function(i){if(i&&i.__esModule)return i;var a={};if(i!=null)for(var p=r(i),h=0;h=0;p--)i.addFloat32(a[p]);return i.endVector()}static startFloatsVector(i,a){i.startVector(4,a,4)}static addInts(i,a){i.addFieldOffset(9,a,0)}static createIntsVector(i,a){i.startVector(8,a.length,8);for(let p=a.length-1;p>=0;p--)i.addInt64(a[p]);return i.endVector()}static startIntsVector(i,a){i.startVector(8,a,8)}static addStrings(i,a){i.addFieldOffset(10,a,0)}static createStringsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startStringsVector(i,a){i.startVector(4,a,4)}static addTensors(i,a){i.addFieldOffset(11,a,0)}static createTensorsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startTensorsVector(i,a){i.startVector(4,a,4)}static addGraphs(i,a){i.addFieldOffset(12,a,0)}static createGraphsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startGraphsVector(i,a){i.startVector(4,a,4)}static endAttribute(i){return i.endObject()}};e.Attribute=s}),x0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addKernelDefHashes(c,s){c.addFieldOffset(1,s,0)}static createKernelDefHashesVector(c,s){c.startVector(8,s.length,8);for(let r=s.length-1;r>=0;r--)c.addInt64(s[r]);return c.endVector()}static startKernelDefHashesVector(c,s){c.startVector(8,s,8)}static endDeprecatedKernelCreateInfos(c){return c.endObject()}static createDeprecatedKernelCreateInfos(c,s,r){return Mr.startDeprecatedKernelCreateInfos(c),Mr.addNodeIndices(c,s),Mr.addKernelDefHashes(c,r),Mr.endDeprecatedKernelCreateInfos(c)}};e.DeprecatedKernelCreateInfos=l}),NI=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(i[a]);return r.endVector()}static startSubGraphSessionStatesVector(r,i){r.startVector(4,i,4)}static endDeprecatedSessionState(r){return r.endObject()}static createDeprecatedSessionState(r,i,a){return Fr.startDeprecatedSessionState(r),Fr.addKernels(r,i),Fr.addSubGraphSessionStates(r,a),Fr.endDeprecatedSessionState(r)}};e.DeprecatedSessionState=c}),S0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createKernelTypeStrArgsEntry(s,r,i){return Lr.startKernelTypeStrArgsEntry(s),Lr.addKernelTypeStr(s,r),Lr.addArgs(s,i),Lr.endKernelTypeStrArgsEntry(s)}};e.KernelTypeStrArgsEntry=d}),I0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endOpIdKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createOpIdKernelTypeStrArgsEntry(s,r,i){return jr.startOpIdKernelTypeStrArgsEntry(s),jr.addOpId(s,r),jr.addKernelTypeStrArgs(s,i),jr.endOpIdKernelTypeStrArgsEntry(s)}};e.OpIdKernelTypeStrArgsEntry=d}),O0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startOpKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrResolver(s){return s.endObject()}static createKernelTypeStrResolver(s,r){return bn.startKernelTypeStrResolver(s),bn.addOpKernelTypeStrArgs(s,r),bn.endKernelTypeStrResolver(s)}};e.KernelTypeStrResolver=d}),E0=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,i,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(i,a);(!h||("get"in h?!i.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return i[a]}}),Object.defineProperty(r,p,h)}:function(r,i,a,p){p===void 0&&(p=a),r[p]=i[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,i){Object.defineProperty(r,"default",{enumerable:!0,value:i})}:function(r,i){r.default=i}),o=e&&e.__importStar||(function(){var r=function(i){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(i)};return function(i){if(i&&i.__esModule)return i;var a={};if(i!=null)for(var p=r(i),h=0;h=0;p--)i.addOffset(a[p]);return i.endVector()}static startOpsetImportVector(i,a){i.startVector(4,a,4)}static addProducerName(i,a){i.addFieldOffset(2,a,0)}static addProducerVersion(i,a){i.addFieldOffset(3,a,0)}static addDomain(i,a){i.addFieldOffset(4,a,0)}static addModelVersion(i,a){i.addFieldInt64(5,a,BigInt("0"))}static addDocString(i,a){i.addFieldOffset(6,a,0)}static addGraph(i,a){i.addFieldOffset(7,a,0)}static addGraphDocString(i,a){i.addFieldOffset(8,a,0)}static addMetadataProps(i,a){i.addFieldOffset(9,a,0)}static createMetadataPropsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startMetadataPropsVector(i,a){i.startVector(4,a,4)}static endModel(i){return i.endObject()}};e.Model=s}),zI=oe(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p{"use strict";MI=fe(Xy()),BI=fe(t0()),Au=fe(v0()),Pt=fe(r0()),FI=fe(x0()),LI=fe(NI()),jI=fe(T0()),UI=fe($0()),VI=fe(g0()),qI=fe(m0()),GI=fe(f0()),HI=fe(o0()),P0=fe(wl()),C0=fe(zI()),WI=fe(S0()),KI=fe(O0()),XI=fe(p0()),ZI=fe(k0()),D0=fe(i0()),YI=fe(s0()),JI=fe(n0()),QI=fe(a0()),e3=fe(I0()),t3=fe(E0()),r3=fe(u0()),n3=fe(l0()),i3=fe(d0()),o3=fe(h0()),s3=fe(b0()),a3=fe(c0()),u3=fe(A0()),l3=fe(No()),go=fe(Ro()),R0=fe(y0()),d3=fe(zo()),N0=fe(_0()),c3=fe(w0())}),Mo=D(()=>{"use strict";p3()}),h3=oe((e,t)=>{"use strict";t.exports=n;function n(o,u){for(var l=new Array(arguments.length-1),d=0,c=2,s=!0;c{"use strict";var t=e;t.length=function(d){var c=d.length;if(!c)return 0;for(var s=0;--c%4>1&&d.charAt(c)==="=";)++s;return Math.ceil(d.length*3)/4-s};var n=new Array(64),o=new Array(123);for(u=0;u<64;)o[n[u]=u<26?u+65:u<52?u+71:u<62?u-4:u-59|43]=u++;var u;t.encode=function(d,c,s){for(var r=null,i=[],a=0,p=0,h;c>2],h=(m&3)<<4,p=1;break;case 1:i[a++]=n[h|m>>4],h=(m&15)<<2,p=2;break;case 2:i[a++]=n[h|m>>6],i[a++]=n[m&63],p=0;break}a>8191&&((r||(r=[])).push(String.fromCharCode.apply(String,i)),a=0)}return p&&(i[a++]=n[h],i[a++]=61,p===1&&(i[a++]=61)),r?(a&&r.push(String.fromCharCode.apply(String,i.slice(0,a))),r.join("")):String.fromCharCode.apply(String,i.slice(0,a))};var l="invalid encoding";t.decode=function(d,c,s){for(var r=s,i=0,a,p=0;p1)break;if((h=o[h])===void 0)throw Error(l);switch(i){case 0:a=h,i=1;break;case 1:c[s++]=a<<2|(h&48)>>4,a=h,i=2;break;case 2:c[s++]=(a&15)<<4|(h&60)>>2,a=h,i=3;break;case 3:c[s++]=(a&3)<<6|h,i=0;break}}if(i===1)throw Error(l);return s-r},t.test=function(d){return/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(d)}}),m3=oe((e,t)=>{"use strict";t.exports=n;function n(){this._listeners=Object.create(null)}n.prototype.on=function(o,u,l){return(this._listeners[o]||(this._listeners[o]=[])).push({fn:u,ctx:l||this}),this},n.prototype.off=function(o,u){if(o===void 0)this._listeners=Object.create(null);else if(u===void 0)this._listeners[o]=[];else{var l=this._listeners[o];if(!l)return this;for(var d=0;d{"use strict";t.exports=n(n);function n(c){return typeof Float32Array<"u"?(function(){var s=new Float32Array([-0]),r=new Uint8Array(s.buffer),i=r[3]===128;function a(b,$,_){s[0]=b,$[_]=r[0],$[_+1]=r[1],$[_+2]=r[2],$[_+3]=r[3]}function p(b,$,_){s[0]=b,$[_]=r[3],$[_+1]=r[2],$[_+2]=r[1],$[_+3]=r[0]}c.writeFloatLE=i?a:p,c.writeFloatBE=i?p:a;function h(b,$){return r[0]=b[$],r[1]=b[$+1],r[2]=b[$+2],r[3]=b[$+3],s[0]}function m(b,$){return r[3]=b[$],r[2]=b[$+1],r[1]=b[$+2],r[0]=b[$+3],s[0]}c.readFloatLE=i?h:m,c.readFloatBE=i?m:h})():(function(){function s(i,a,p,h){var m=a<0?1:0;if(m&&(a=-a),a===0)i(1/a>0?0:2147483648,p,h);else if(isNaN(a))i(2143289344,p,h);else if(a>34028234663852886e22)i((m<<31|2139095040)>>>0,p,h);else if(a<11754943508222875e-54)i((m<<31|Math.round(a/1401298464324817e-60))>>>0,p,h);else{var b=Math.floor(Math.log(a)/Math.LN2),$=Math.round(a*Math.pow(2,-b)*8388608)&8388607;i((m<<31|b+127<<23|$)>>>0,p,h)}}c.writeFloatLE=s.bind(null,o),c.writeFloatBE=s.bind(null,u);function r(i,a,p){var h=i(a,p),m=(h>>31)*2+1,b=h>>>23&255,$=h&8388607;return b===255?$?NaN:m*(1/0):b===0?m*1401298464324817e-60*$:m*Math.pow(2,b-150)*($+8388608)}c.readFloatLE=r.bind(null,l),c.readFloatBE=r.bind(null,d)})(),typeof Float64Array<"u"?(function(){var s=new Float64Array([-0]),r=new Uint8Array(s.buffer),i=r[7]===128;function a(b,$,_){s[0]=b,$[_]=r[0],$[_+1]=r[1],$[_+2]=r[2],$[_+3]=r[3],$[_+4]=r[4],$[_+5]=r[5],$[_+6]=r[6],$[_+7]=r[7]}function p(b,$,_){s[0]=b,$[_]=r[7],$[_+1]=r[6],$[_+2]=r[5],$[_+3]=r[4],$[_+4]=r[3],$[_+5]=r[2],$[_+6]=r[1],$[_+7]=r[0]}c.writeDoubleLE=i?a:p,c.writeDoubleBE=i?p:a;function h(b,$){return r[0]=b[$],r[1]=b[$+1],r[2]=b[$+2],r[3]=b[$+3],r[4]=b[$+4],r[5]=b[$+5],r[6]=b[$+6],r[7]=b[$+7],s[0]}function m(b,$){return r[7]=b[$],r[6]=b[$+1],r[5]=b[$+2],r[4]=b[$+3],r[3]=b[$+4],r[2]=b[$+5],r[1]=b[$+6],r[0]=b[$+7],s[0]}c.readDoubleLE=i?h:m,c.readDoubleBE=i?m:h})():(function(){function s(i,a,p,h,m,b){var $=h<0?1:0;if($&&(h=-h),h===0)i(0,m,b+a),i(1/h>0?0:2147483648,m,b+p);else if(isNaN(h))i(0,m,b+a),i(2146959360,m,b+p);else if(h>17976931348623157e292)i(0,m,b+a),i(($<<31|2146435072)>>>0,m,b+p);else{var _;if(h<22250738585072014e-324)_=h/5e-324,i(_>>>0,m,b+a),i(($<<31|_/4294967296)>>>0,m,b+p);else{var y=Math.floor(Math.log(h)/Math.LN2);y===1024&&(y=1023),_=h*Math.pow(2,-y),i(_*4503599627370496>>>0,m,b+a),i(($<<31|y+1023<<20|_*1048576&1048575)>>>0,m,b+p)}}}c.writeDoubleLE=s.bind(null,o,0,4),c.writeDoubleBE=s.bind(null,u,4,0);function r(i,a,p,h,m){var b=i(h,m+a),$=i(h,m+p),_=($>>31)*2+1,y=$>>>20&2047,T=4294967296*($&1048575)+b;return y===2047?T?NaN:_*(1/0):y===0?_*5e-324*T:_*Math.pow(2,y-1075)*(T+4503599627370496)}c.readDoubleLE=r.bind(null,l,0,4),c.readDoubleBE=r.bind(null,d,4,0)})(),c}function o(c,s,r){s[r]=c&255,s[r+1]=c>>>8&255,s[r+2]=c>>>16&255,s[r+3]=c>>>24}function u(c,s,r){s[r]=c>>>24,s[r+1]=c>>>16&255,s[r+2]=c>>>8&255,s[r+3]=c&255}function l(c,s){return(c[s]|c[s+1]<<8|c[s+2]<<16|c[s+3]<<24)>>>0}function d(c,s){return(c[s]<<24|c[s+1]<<16|c[s+2]<<8|c[s+3])>>>0}}),b3=oe((e,t)=>{"use strict";t.exports=n;function n(o){try{if(typeof cu!="function")return null;var u=cu(o);return u&&(u.length||Object.keys(u).length)?u:null}catch{return null}}}),y3=oe(e=>{"use strict";var t=e,n="\uFFFD";t.length=function(o){for(var u=0,l=0,d=0;d=192&&s<224){var r=(s&31)<<6|o[c++]&63;d+=r>=128?String.fromCharCode(r):n}else if(s>=224&&s<240){var i=(s&15)<<12|(o[c++]&63)<<6|o[c++]&63;d+=i>=2048?String.fromCharCode(i):n}else if(s>=240){var a=(s&7)<<18|(o[c++]&63)<<12|(o[c++]&63)<<6|o[c++]&63;a<65536||a>1114111?d+=n:(a-=65536,d+=String.fromCharCode(55296+(a>>10)),d+=String.fromCharCode(56320+(a&1023)))}}return d},t.write=function(o,u,l){for(var d=l,c,s,r=0;r>6|192,u[l++]=c&63|128):(c&64512)===55296&&((s=o.charCodeAt(r+1))&64512)===56320?(c=65536+((c&1023)<<10)+(s&1023),++r,u[l++]=c>>18|240,u[l++]=c>>12&63|128,u[l++]=c>>6&63|128,u[l++]=c&63|128):(u[l++]=c>>12|224,u[l++]=c>>6&63|128,u[l++]=c&63|128);return l-d}}),_3=oe((e,t)=>{"use strict";t.exports=n;function n(o,u,l){var d=l||8192,c=d>>>1,s=null,r=d;return function(i){if(i<1||i>c)return o(i);r+i>d&&(s=o(d),r=0);var a=u.call(s,r,r+=i);return r&7&&(r=(r|7)+1),a}}}),w3=oe((e,t)=>{"use strict";t.exports=o;var n=on();function o(c,s){this.lo=c>>>0,this.hi=s>>>0}var u=o.zero=new o(0,0);u.toNumber=function(){return 0},u.zzEncode=u.zzDecode=function(){return this},u.length=function(){return 1};var l=o.zeroHash="\0\0\0\0\0\0\0\0";o.fromNumber=function(c){if(c===0)return u;var s=c<0;s&&(c=-c);var r=c>>>0,i=(c-r)/4294967296>>>0;return s&&(i=~i>>>0,r=~r>>>0,++r>4294967295&&(r=0,++i>4294967295&&(i=0))),new o(r,i)},o.from=function(c){if(typeof c=="number")return o.fromNumber(c);if(n.isString(c))if(n.Long)c=n.Long.fromString(c);else return o.fromNumber(parseInt(c,10));return c.low||c.high?new o(c.low>>>0,c.high>>>0):u},o.prototype.toNumber=function(c){if(!c&&this.hi>>>31){var s=~this.lo+1>>>0,r=~this.hi>>>0;return s||(r=r+1>>>0),-(s+r*4294967296)}return this.lo+this.hi*4294967296},o.prototype.toLong=function(c){return n.Long?new n.Long(this.lo|0,this.hi|0,!!c):{low:this.lo|0,high:this.hi|0,unsigned:!!c}};var d=String.prototype.charCodeAt;o.fromHash=function(c){return c===l?u:new o((d.call(c,0)|d.call(c,1)<<8|d.call(c,2)<<16|d.call(c,3)<<24)>>>0,(d.call(c,4)|d.call(c,5)<<8|d.call(c,6)<<16|d.call(c,7)<<24)>>>0)},o.prototype.toHash=function(){return String.fromCharCode(this.lo&255,this.lo>>>8&255,this.lo>>>16&255,this.lo>>>24,this.hi&255,this.hi>>>8&255,this.hi>>>16&255,this.hi>>>24)},o.prototype.zzEncode=function(){var c=this.hi>>31;return this.hi=((this.hi<<1|this.lo>>>31)^c)>>>0,this.lo=(this.lo<<1^c)>>>0,this},o.prototype.zzDecode=function(){var c=-(this.lo&1);return this.lo=((this.lo>>>1|this.hi<<31)^c)>>>0,this.hi=(this.hi>>>1^c)>>>0,this},o.prototype.length=function(){var c=this.lo,s=(this.lo>>>28|this.hi<<4)>>>0,r=this.hi>>>24;return r===0?s===0?c<16384?c<128?1:2:c<2097152?3:4:s<16384?s<128?5:6:s<2097152?7:8:r<128?9:10}}),v3=oe((e,t)=>{(function(n,o){function u(l){return l.default||l}typeof define=="function"&&define.amd?define([],function(){var l={};return o(l),u(l)}):typeof e=="object"?(o(e),typeof t=="object"&&(t.exports=u(e))):(function(){var l={};o(l),n.Long=u(l)})()})(typeof globalThis<"u"?globalThis:typeof self<"u"?self:e,function(n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;var o=null;try{o=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}function u(w,C,U){this.low=w|0,this.high=C|0,this.unsigned=!!U}u.prototype.__isLong__,Object.defineProperty(u.prototype,"__isLong__",{value:!0});function l(w){return(w&&w.__isLong__)===!0}function d(w){var C=Math.clz32(w&-w);return w?31-C:C}u.isLong=l;var c={},s={};function r(w,C){var U,H,G;return C?(w>>>=0,(G=0<=w&&w<256)&&(H=s[w],H)?H:(U=a(w,0,!0),G&&(s[w]=U),U)):(w|=0,(G=-128<=w&&w<128)&&(H=c[w],H)?H:(U=a(w,w<0?-1:0,!1),G&&(c[w]=U),U))}u.fromInt=r;function i(w,C){if(isNaN(w))return C?A:O;if(C){if(w<0)return A;if(w>=y)return Z}else{if(w<=-T)return q;if(w+1>=T)return V}return w<0?i(-w,C).neg():a(w%_|0,w/_|0,C)}u.fromNumber=i;function a(w,C,U){return new u(w,C,U)}u.fromBits=a;var p=Math.pow;function h(w,C,U){if(w.length===0)throw Error("empty string");if(typeof C=="number"?(U=C,C=!1):C=!!C,w==="NaN"||w==="Infinity"||w==="+Infinity"||w==="-Infinity")return C?A:O;if(U=U||10,U<2||360)throw Error("interior hyphen");if(H===0)return h(w.substring(1),C,U).neg();for(var G=i(p(U,8)),J=O,B=0;B>>0:this.low},N.toNumber=function(){return this.unsigned?(this.high>>>0)*_+(this.low>>>0):this.high*_+(this.low>>>0)},N.toString=function(w){if(w=w||10,w<2||36>>0,M=K.toString(w);if(J=X,J.isZero())return M+B;for(;M.length<6;)M="0"+M;B=""+M+B}},N.getHighBits=function(){return this.high},N.getHighBitsUnsigned=function(){return this.high>>>0},N.getLowBits=function(){return this.low},N.getLowBitsUnsigned=function(){return this.low>>>0},N.getNumBitsAbs=function(){if(this.isNegative())return this.eq(q)?64:this.neg().getNumBitsAbs();for(var w=this.high!=0?this.high:this.low,C=31;C>0&&(w&1<>21;return w?this.unsigned?!1:w===-1&&!(this.low===0&&this.high===-2097152):!0},N.isZero=function(){return this.high===0&&this.low===0},N.eqz=N.isZero,N.isNegative=function(){return!this.unsigned&&this.high<0},N.isPositive=function(){return this.unsigned||this.high>=0},N.isOdd=function(){return(this.low&1)===1},N.isEven=function(){return(this.low&1)===0},N.equals=function(w){return l(w)||(w=m(w)),this.unsigned!==w.unsigned&&this.high>>>31===1&&w.high>>>31===1?!1:this.high===w.high&&this.low===w.low},N.eq=N.equals,N.notEquals=function(w){return!this.eq(w)},N.neq=N.notEquals,N.ne=N.notEquals,N.lessThan=function(w){return this.comp(w)<0},N.lt=N.lessThan,N.lessThanOrEqual=function(w){return this.comp(w)<=0},N.lte=N.lessThanOrEqual,N.le=N.lessThanOrEqual,N.greaterThan=function(w){return this.comp(w)>0},N.gt=N.greaterThan,N.greaterThanOrEqual=function(w){return this.comp(w)>=0},N.gte=N.greaterThanOrEqual,N.ge=N.greaterThanOrEqual,N.compare=function(w){if(l(w)||(w=m(w)),this.eq(w))return 0;var C=this.isNegative(),U=w.isNegative();return C&&!U?-1:!C&&U?1:this.unsigned?w.high>>>0>this.high>>>0||w.high===this.high&&w.low>>>0>this.low>>>0?-1:1:this.sub(w).isNegative()?-1:1},N.comp=N.compare,N.negate=function(){return!this.unsigned&&this.eq(q)?q:this.not().add(P)},N.neg=N.negate,N.add=function(w){l(w)||(w=m(w));var C=this.high>>>16,U=this.high&65535,H=this.low>>>16,G=this.low&65535,J=w.high>>>16,B=w.high&65535,X=w.low>>>16,K=w.low&65535,M=0,ne=0,Q=0,le=0;return le+=G+K,Q+=le>>>16,le&=65535,Q+=H+X,ne+=Q>>>16,Q&=65535,ne+=U+B,M+=ne>>>16,ne&=65535,M+=C+J,M&=65535,a(Q<<16|le,M<<16|ne,this.unsigned)},N.subtract=function(w){return l(w)||(w=m(w)),this.add(w.neg())},N.sub=N.subtract,N.multiply=function(w){if(this.isZero())return this;if(l(w)||(w=m(w)),o){var C=o.mul(this.low,this.high,w.low,w.high);return a(C,o.get_high(),this.unsigned)}if(w.isZero())return this.unsigned?A:O;if(this.eq(q))return w.isOdd()?q:O;if(w.eq(q))return this.isOdd()?q:O;if(this.isNegative())return w.isNegative()?this.neg().mul(w.neg()):this.neg().mul(w).neg();if(w.isNegative())return this.mul(w.neg()).neg();if(this.lt(S)&&w.lt(S))return i(this.toNumber()*w.toNumber(),this.unsigned);var U=this.high>>>16,H=this.high&65535,G=this.low>>>16,J=this.low&65535,B=w.high>>>16,X=w.high&65535,K=w.low>>>16,M=w.low&65535,ne=0,Q=0,le=0,Ae=0;return Ae+=J*M,le+=Ae>>>16,Ae&=65535,le+=G*M,Q+=le>>>16,le&=65535,le+=J*K,Q+=le>>>16,le&=65535,Q+=H*M,ne+=Q>>>16,Q&=65535,Q+=G*K,ne+=Q>>>16,Q&=65535,Q+=J*X,ne+=Q>>>16,Q&=65535,ne+=U*M+H*K+G*X+J*B,ne&=65535,a(le<<16|Ae,ne<<16|Q,this.unsigned)},N.mul=N.multiply,N.divide=function(w){if(l(w)||(w=m(w)),w.isZero())throw Error("division by zero");if(o){if(!this.unsigned&&this.high===-2147483648&&w.low===-1&&w.high===-1)return this;var C=(this.unsigned?o.div_u:o.div_s)(this.low,this.high,w.low,w.high);return a(C,o.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?A:O;var U,H,G;if(this.unsigned){if(w.unsigned||(w=w.toUnsigned()),w.gt(this))return A;if(w.gt(this.shru(1)))return I;G=A}else{if(this.eq(q)){if(w.eq(P)||w.eq(L))return q;if(w.eq(q))return P;var J=this.shr(1);return U=J.div(w).shl(1),U.eq(O)?w.isNegative()?P:L:(H=this.sub(w.mul(U)),G=U.add(H.div(w)),G)}else if(w.eq(q))return this.unsigned?A:O;if(this.isNegative())return w.isNegative()?this.neg().div(w.neg()):this.neg().div(w).neg();if(w.isNegative())return this.div(w.neg()).neg();G=O}for(H=this;H.gte(w);){U=Math.max(1,Math.floor(H.toNumber()/w.toNumber()));for(var B=Math.ceil(Math.log(U)/Math.LN2),X=B<=48?1:p(2,B-48),K=i(U),M=K.mul(w);M.isNegative()||M.gt(H);)U-=X,K=i(U,this.unsigned),M=K.mul(w);K.isZero()&&(K=P),G=G.add(K),H=H.sub(M)}return G},N.div=N.divide,N.modulo=function(w){if(l(w)||(w=m(w)),o){var C=(this.unsigned?o.rem_u:o.rem_s)(this.low,this.high,w.low,w.high);return a(C,o.get_high(),this.unsigned)}return this.sub(this.div(w).mul(w))},N.mod=N.modulo,N.rem=N.modulo,N.not=function(){return a(~this.low,~this.high,this.unsigned)},N.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},N.clz=N.countLeadingZeros,N.countTrailingZeros=function(){return this.low?d(this.low):d(this.high)+32},N.ctz=N.countTrailingZeros,N.and=function(w){return l(w)||(w=m(w)),a(this.low&w.low,this.high&w.high,this.unsigned)},N.or=function(w){return l(w)||(w=m(w)),a(this.low|w.low,this.high|w.high,this.unsigned)},N.xor=function(w){return l(w)||(w=m(w)),a(this.low^w.low,this.high^w.high,this.unsigned)},N.shiftLeft=function(w){return l(w)&&(w=w.toInt()),(w&=63)===0?this:w<32?a(this.low<>>32-w,this.unsigned):a(0,this.low<>>w|this.high<<32-w,this.high>>w,this.unsigned):a(this.high>>w-32,this.high>=0?0:-1,this.unsigned)},N.shr=N.shiftRight,N.shiftRightUnsigned=function(w){return l(w)&&(w=w.toInt()),(w&=63)===0?this:w<32?a(this.low>>>w|this.high<<32-w,this.high>>>w,this.unsigned):w===32?a(this.high,0,this.unsigned):a(this.high>>>w-32,0,this.unsigned)},N.shru=N.shiftRightUnsigned,N.shr_u=N.shiftRightUnsigned,N.rotateLeft=function(w){var C;return l(w)&&(w=w.toInt()),(w&=63)===0?this:w===32?a(this.high,this.low,this.unsigned):w<32?(C=32-w,a(this.low<>>C,this.high<>>C,this.unsigned)):(w-=32,C=32-w,a(this.high<>>C,this.low<>>C,this.unsigned))},N.rotl=N.rotateLeft,N.rotateRight=function(w){var C;return l(w)&&(w=w.toInt()),(w&=63)===0?this:w===32?a(this.high,this.low,this.unsigned):w<32?(C=32-w,a(this.high<>>w,this.low<>>w,this.unsigned)):(w-=32,C=32-w,a(this.low<>>w,this.high<>>w,this.unsigned))},N.rotr=N.rotateRight,N.toSigned=function(){return this.unsigned?a(this.low,this.high,!1):this},N.toUnsigned=function(){return this.unsigned?this:a(this.low,this.high,!0)},N.toBytes=function(w){return w?this.toBytesLE():this.toBytesBE()},N.toBytesLE=function(){var w=this.high,C=this.low;return[C&255,C>>>8&255,C>>>16&255,C>>>24,w&255,w>>>8&255,w>>>16&255,w>>>24]},N.toBytesBE=function(){var w=this.high,C=this.low;return[w>>>24,w>>>16&255,w>>>8&255,w&255,C>>>24,C>>>16&255,C>>>8&255,C&255]},u.fromBytes=function(w,C,U){return U?u.fromBytesLE(w,C):u.fromBytesBE(w,C)},u.fromBytesLE=function(w,C){return new u(w[0]|w[1]<<8|w[2]<<16|w[3]<<24,w[4]|w[5]<<8|w[6]<<16|w[7]<<24,C)},u.fromBytesBE=function(w,C){return new u(w[4]<<24|w[5]<<16|w[6]<<8|w[7],w[0]<<24|w[1]<<16|w[2]<<8|w[3],C)},typeof BigInt=="function"&&(u.fromBigInt=function(w,C){var U=Number(BigInt.asIntN(32,w)),H=Number(BigInt.asIntN(32,w>>BigInt(32)));return a(U,H,C)},u.fromValue=function(w,C){return typeof w=="bigint"?u.fromBigInt(w,C):m(w,C)},N.toBigInt=function(){var w=BigInt(this.low>>>0),C=BigInt(this.unsigned?this.high>>>0:this.high);return C<{"use strict";var t=e;t.asPromise=h3(),t.base64=f3(),t.EventEmitter=m3(),t.float=g3(),t.inquire=b3(),t.utf8=y3(),t.pool=_3(),t.LongBits=w3();function n(l){return l==="__proto__"||l==="prototype"||l==="constructor"}t.isUnsafeProperty=n,t.isNode=!!(typeof global<"u"&&global&&global.process&&global.process.versions&&global.process.versions.node),t.global=t.isNode&&global||typeof window<"u"&&window||typeof self<"u"&&self||e,t.emptyArray=Object.freeze?Object.freeze([]):[],t.emptyObject=Object.freeze?Object.freeze({}):{},t.isInteger=Number.isInteger||function(l){return typeof l=="number"&&isFinite(l)&&Math.floor(l)===l},t.isString=function(l){return typeof l=="string"||l instanceof String},t.isObject=function(l){return l&&typeof l=="object"},t.isset=t.isSet=function(l,d){var c=l[d];return c!=null&&Object.hasOwnProperty.call(l,d)?typeof c!="object"||(Array.isArray(c)?c.length:Object.keys(c).length)>0:!1},t.Buffer=(function(){try{var l=t.global.Buffer;return l.prototype.utf8Write?l:null}catch{return null}})(),t._Buffer_from=null,t._Buffer_allocUnsafe=null,t.newBuffer=function(l){return typeof l=="number"?t.Buffer?t._Buffer_allocUnsafe(l):new t.Array(l):t.Buffer?t._Buffer_from(l):typeof Uint8Array>"u"?l:new Uint8Array(l)},t.Array=typeof Uint8Array<"u"?Uint8Array:Array,t.Long=t.global.dcodeIO&&t.global.dcodeIO.Long||t.global.Long||(function(){try{var l=v3();return l&&l.isLong?l:null}catch{return null}})(),t.key2Re=/^true|false|0|1$/,t.key32Re=/^-?(?:0|[1-9][0-9]*)$/,t.key64Re=/^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/,t.longToHash=function(l){return l?t.LongBits.from(l).toHash():t.LongBits.zeroHash},t.longFromHash=function(l,d){var c=t.LongBits.fromHash(l);return t.Long?t.Long.fromBits(c.lo,c.hi,d):c.toNumber(!!d)};function o(l){var d=typeof arguments[arguments.length-1]=="boolean",c=d?arguments.length-1:arguments.length;d=d&&arguments[arguments.length-1];for(var s=1;s-1;--r)if(d[s[r]]===1&&this[s[r]]!==void 0&&this[s[r]]!==null)return s[r]}},t.oneOfSetter=function(l){return function(d){for(var c=0;c{"use strict";t.exports=i;var n=on(),o,u=n.LongBits,l=n.base64,d=n.utf8;function c(y,T,S){this.fn=y,this.len=T,this.next=void 0,this.val=S}function s(){}function r(y){this.head=y.head,this.tail=y.tail,this.len=y.len,this.next=y.states}function i(){this.len=0,this.head=new c(s,0,0),this.tail=this.head,this.states=null}var a=function(){return n.Buffer?function(){return(i.create=function(){return new o})()}:function(){return new i}};i.create=a(),i.alloc=function(y){return new n.Array(y)},n.Array!==Array&&(i.alloc=n.pool(i.alloc,n.Array.prototype.subarray)),i.prototype._push=function(y,T,S){return this.tail=this.tail.next=new c(y,T,S),this.len+=T,this};function p(y,T,S){T[S]=y&255}function h(y,T,S){for(;y>127;)T[S++]=y&127|128,y>>>=7;T[S]=y}function m(y,T){this.len=y,this.next=void 0,this.val=T}m.prototype=Object.create(c.prototype),m.prototype.fn=h,i.prototype.uint32=function(y){return this.len+=(this.tail=this.tail.next=new m((y=y>>>0)<128?1:y<16384?2:y<2097152?3:y<268435456?4:5,y)).len,this},i.prototype.int32=function(y){return(y|=0)<0?this._push(b,10,u.fromNumber(y)):this.uint32(y)},i.prototype.sint32=function(y){return this.uint32((y<<1^y>>31)>>>0)};function b(y,T,S){for(var O=y.lo,A=y.hi;A;)T[S++]=O&127|128,O=(O>>>7|A<<25)>>>0,A>>>=7;for(;O>127;)T[S++]=O&127|128,O=O>>>7;T[S++]=O}i.prototype.uint64=function(y){var T=u.from(y);return this._push(b,T.length(),T)},i.prototype.int64=i.prototype.uint64,i.prototype.sint64=function(y){var T=u.from(y).zzEncode();return this._push(b,T.length(),T)},i.prototype.bool=function(y){return this._push(p,1,y?1:0)};function $(y,T,S){T[S]=y&255,T[S+1]=y>>>8&255,T[S+2]=y>>>16&255,T[S+3]=y>>>24}i.prototype.fixed32=function(y){return this._push($,4,y>>>0)},i.prototype.sfixed32=i.prototype.fixed32,i.prototype.fixed64=function(y){var T=u.from(y);return this._push($,4,T.lo)._push($,4,T.hi)},i.prototype.sfixed64=i.prototype.fixed64,i.prototype.float=function(y){return this._push(n.float.writeFloatLE,4,y)},i.prototype.double=function(y){return this._push(n.float.writeDoubleLE,8,y)};var _=n.Array.prototype.set?function(y,T,S){T.set(y,S)}:function(y,T,S){for(var O=0;O>>0;if(!T)return this._push(p,1,0);if(n.isString(y)){var S=i.alloc(T=l.length(y));l.decode(y,S,0),y=S}return this.uint32(T)._push(_,T,y)},i.prototype.string=function(y){var T=d.length(y);return T?this.uint32(T)._push(d.write,T,y):this._push(p,1,0)},i.prototype.fork=function(){return this.states=new r(this),this.head=this.tail=new c(s,0,0),this.len=0,this},i.prototype.reset=function(){return this.states?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new c(s,0,0),this.len=0),this},i.prototype.ldelim=function(){var y=this.head,T=this.tail,S=this.len;return this.reset().uint32(S),S&&(this.tail.next=y.next,this.tail=T,this.len+=S),this},i.prototype.finish=function(){for(var y=this.head.next,T=this.constructor.alloc(this.len),S=0;y;)y.fn(y.val,T,S),S+=y.len,y=y.next;return T},i._configure=function(y){o=y,i.create=a(),o._configure()}}),x3=oe((e,t)=>{"use strict";t.exports=u;var n=z0();(u.prototype=Object.create(n.prototype)).constructor=u;var o=on();function u(){n.call(this)}u._configure=function(){u.alloc=o._Buffer_allocUnsafe,u.writeBytesBuffer=o.Buffer&&o.Buffer.prototype instanceof Uint8Array&&o.Buffer.prototype.set.name==="set"?function(d,c,s){c.set(d,s)}:function(d,c,s){if(d.copy)d.copy(c,s,0,d.length);else for(var r=0;r>>0;return this.uint32(c),c&&this._push(u.writeBytesBuffer,c,d),this};function l(d,c,s){d.length<40?o.utf8.write(d,c,s):c.utf8Write?c.utf8Write(d,s):c.write(d,s)}u.prototype.string=function(d){var c=o.Buffer.byteLength(d);return this.uint32(c),c&&this._push(l,c,d),this},u._configure()}),M0=oe((e,t)=>{"use strict";t.exports=c;var n=on(),o,u=n.LongBits,l=n.utf8;function d(h,m){return RangeError("index out of range: "+h.pos+" + "+(m||1)+" > "+h.len)}function c(h){this.buf=h,this.pos=0,this.len=h.length}var s=typeof Uint8Array<"u"?function(h){if(h instanceof Uint8Array||Array.isArray(h))return new c(h);throw Error("illegal buffer")}:function(h){if(Array.isArray(h))return new c(h);throw Error("illegal buffer")},r=function(){return n.Buffer?function(h){return(c.create=function(m){return n.Buffer.isBuffer(m)?new o(m):s(m)})(h)}:s};c.create=r(),c.prototype._slice=n.Array.prototype.subarray||n.Array.prototype.slice,c.prototype.uint32=(function(){var h=4294967295;return function(){if(h=(this.buf[this.pos]&127)>>>0,this.buf[this.pos++]<128||(h=(h|(this.buf[this.pos]&127)<<7)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<14)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<21)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&15)<<28)>>>0,this.buf[this.pos++]<128))return h;if((this.pos+=5)>this.len)throw this.pos=this.len,d(this,10);return h}})(),c.prototype.int32=function(){return this.uint32()|0},c.prototype.sint32=function(){var h=this.uint32();return h>>>1^-(h&1)|0};function i(){var h=new u(0,0),m=0;if(this.len-this.pos>4){for(;m<4;++m)if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h;if(h.lo=(h.lo|(this.buf[this.pos]&127)<<28)>>>0,h.hi=(h.hi|(this.buf[this.pos]&127)>>4)>>>0,this.buf[this.pos++]<128)return h;m=0}else{for(;m<3;++m){if(this.pos>=this.len)throw d(this);if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}return h.lo=(h.lo|(this.buf[this.pos++]&127)<>>0,h}if(this.len-this.pos>4){for(;m<5;++m)if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}else for(;m<5;++m){if(this.pos>=this.len)throw d(this);if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}throw Error("invalid varint encoding")}c.prototype.bool=function(){return this.uint32()!==0};function a(h,m){return(h[m-4]|h[m-3]<<8|h[m-2]<<16|h[m-1]<<24)>>>0}c.prototype.fixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)},c.prototype.sfixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)|0};function p(){if(this.pos+8>this.len)throw d(this,8);return new u(a(this.buf,this.pos+=4),a(this.buf,this.pos+=4))}c.prototype.float=function(){if(this.pos+4>this.len)throw d(this,4);var h=n.float.readFloatLE(this.buf,this.pos);return this.pos+=4,h},c.prototype.double=function(){if(this.pos+8>this.len)throw d(this,4);var h=n.float.readDoubleLE(this.buf,this.pos);return this.pos+=8,h},c.prototype.bytes=function(){var h=this.uint32(),m=this.pos,b=this.pos+h;if(b>this.len)throw d(this,h);if(this.pos+=h,Array.isArray(this.buf))return this.buf.slice(m,b);if(m===b){var $=n.Buffer;return $?$.alloc(0):new this.buf.constructor(0)}return this._slice.call(this.buf,m,b)},c.prototype.string=function(){var h=this.bytes();return l.read(h,0,h.length)},c.prototype.skip=function(h){if(typeof h=="number"){if(this.pos+h>this.len)throw d(this,h);this.pos+=h}else do if(this.pos>=this.len)throw d(this);while(this.buf[this.pos++]&128);return this},c.recursionLimit=n.recursionLimit,c.prototype.skipType=function(h,m){if(m===void 0&&(m=0),m>c.recursionLimit)throw Error("maximum nesting depth exceeded");switch(h){case 0:this.skip();break;case 1:this.skip(8);break;case 2:this.skip(this.uint32());break;case 3:for(;(h=this.uint32()&7)!==4;)this.skipType(h,m+1);break;case 5:this.skip(4);break;default:throw Error("invalid wire type "+h+" at offset "+this.pos)}return this},c._configure=function(h){o=h,c.create=r(),o._configure();var m=n.Long?"toLong":"toNumber";n.merge(c.prototype,{int64:function(){return i.call(this)[m](!1)},uint64:function(){return i.call(this)[m](!0)},sint64:function(){return i.call(this).zzDecode()[m](!1)},fixed64:function(){return p.call(this)[m](!0)},sfixed64:function(){return p.call(this)[m](!1)}})}}),$3=oe((e,t)=>{"use strict";t.exports=u;var n=M0();(u.prototype=Object.create(n.prototype)).constructor=u;var o=on();function u(l){n.call(this,l)}u._configure=function(){o.Buffer&&(u.prototype._slice=o.Buffer.prototype.slice)},u.prototype.string=function(){var l=this.uint32();return this.buf.utf8Slice?this.buf.utf8Slice(this.pos,this.pos=Math.min(this.pos+l,this.len)):this.buf.toString("utf-8",this.pos,this.pos=Math.min(this.pos+l,this.len))},u._configure()}),T3=oe((e,t)=>{"use strict";t.exports=o;var n=on();(o.prototype=Object.create(n.EventEmitter.prototype)).constructor=o;function o(u,l,d){if(typeof u!="function")throw TypeError("rpcImpl must be a function");n.EventEmitter.call(this),this.rpcImpl=u,this.requestDelimited=!!l,this.responseDelimited=!!d}o.prototype.rpcCall=function u(l,d,c,s,r){if(!s)throw TypeError("request must be specified");var i=this;if(!r)return n.asPromise(u,i,l,d,c,s);if(!i.rpcImpl){setTimeout(function(){r(Error("already ended"))},0);return}try{return i.rpcImpl(l,d[i.requestDelimited?"encodeDelimited":"encode"](s).finish(),function(a,p){if(a)return i.emit("error",a,l),r(a);if(p===null){i.end(!0);return}if(!(p instanceof c))try{p=c[i.responseDelimited?"decodeDelimited":"decode"](p)}catch(h){return i.emit("error",h,l),r(h)}return i.emit("data",p,l),r(null,p)})}catch(a){i.emit("error",a,l),setTimeout(function(){r(a)},0);return}},o.prototype.end=function(u){return this.rpcImpl&&(u||this.rpcImpl(null,null,null),this.rpcImpl=null,this.emit("end").off()),this}}),S3=oe(e=>{"use strict";var t=e;t.Service=T3()}),I3=oe((e,t)=>{"use strict";t.exports=Object.create(null)}),O3=oe(e=>{"use strict";var t=e;t.build="minimal",t.Writer=z0(),t.BufferWriter=x3(),t.Reader=M0(),t.BufferReader=$3(),t.util=on(),t.rpc=S3(),t.roots=I3(),t.configure=n;function n(){t.util._configure(),t.Writer._configure(t.BufferWriter),t.Reader._configure(t.BufferReader)}n()}),E3=oe((e,t)=>{"use strict";t.exports=O3()}),_i=oe((e,t)=>{"use strict";var n=E3(),o=n.Reader,u=n.Writer,l=n.util,d=n.roots.default||(n.roots.default={});d.onnx=(function(){var c={};return c.Version=(function(){var s={},r=Object.create(s);return r[s[0]="_START_VERSION"]=0,r[s[1]="IR_VERSION_2017_10_10"]=1,r[s[2]="IR_VERSION_2017_10_30"]=2,r[s[3]="IR_VERSION_2017_11_3"]=3,r[s[4]="IR_VERSION_2019_1_22"]=4,r[s[5]="IR_VERSION_2019_3_18"]=5,r[s[6]="IR_VERSION_2019_9_19"]=6,r[s[7]="IR_VERSION_2020_5_8"]=7,r[s[8]="IR_VERSION_2021_7_30"]=8,r[s[9]="IR_VERSION"]=9,r})(),c.AttributeProto=(function(){function s(r){if(this.floats=[],this.ints=[],this.strings=[],this.tensors=[],this.graphs=[],this.sparseTensors=[],this.typeProtos=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 21:{p.refAttrName=r.string();break}case 13:{p.docString=r.string();break}case 20:{p.type=r.int32();break}case 2:{p.f=r.float();break}case 3:{p.i=r.int64();break}case 4:{p.s=r.bytes();break}case 5:{p.t=d.onnx.TensorProto.decode(r,r.uint32());break}case 6:{p.g=d.onnx.GraphProto.decode(r,r.uint32());break}case 22:{p.sparseTensor=d.onnx.SparseTensorProto.decode(r,r.uint32());break}case 14:{p.tp=d.onnx.TypeProto.decode(r,r.uint32());break}case 7:{if(p.floats&&p.floats.length||(p.floats=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.i.high>>>0).toNumber())),r.s!=null&&(typeof r.s=="string"?l.base64.decode(r.s,i.s=l.newBuffer(l.base64.length(r.s)),0):r.s.length>=0&&(i.s=r.s)),r.t!=null){if(typeof r.t!="object")throw TypeError(".onnx.AttributeProto.t: object expected");i.t=d.onnx.TensorProto.fromObject(r.t)}if(r.g!=null){if(typeof r.g!="object")throw TypeError(".onnx.AttributeProto.g: object expected");i.g=d.onnx.GraphProto.fromObject(r.g)}if(r.sparseTensor!=null){if(typeof r.sparseTensor!="object")throw TypeError(".onnx.AttributeProto.sparseTensor: object expected");i.sparseTensor=d.onnx.SparseTensorProto.fromObject(r.sparseTensor)}if(r.tp!=null){if(typeof r.tp!="object")throw TypeError(".onnx.AttributeProto.tp: object expected");i.tp=d.onnx.TypeProto.fromObject(r.tp)}if(r.floats){if(!Array.isArray(r.floats))throw TypeError(".onnx.AttributeProto.floats: array expected");i.floats=[];for(var a=0;a>>0,r.ints[a].high>>>0).toNumber())}if(r.strings){if(!Array.isArray(r.strings))throw TypeError(".onnx.AttributeProto.strings: array expected");i.strings=[];for(var a=0;a=0&&(i.strings[a]=r.strings[a])}if(r.tensors){if(!Array.isArray(r.tensors))throw TypeError(".onnx.AttributeProto.tensors: array expected");i.tensors=[];for(var a=0;a>>0,r.i.high>>>0).toNumber():r.i),r.s!=null&&r.hasOwnProperty("s")&&(a.s=i.bytes===String?l.base64.encode(r.s,0,r.s.length):i.bytes===Array?Array.prototype.slice.call(r.s):r.s),r.t!=null&&r.hasOwnProperty("t")&&(a.t=d.onnx.TensorProto.toObject(r.t,i)),r.g!=null&&r.hasOwnProperty("g")&&(a.g=d.onnx.GraphProto.toObject(r.g,i)),r.floats&&r.floats.length){a.floats=[];for(var h=0;h>>0,r.ints[h].high>>>0).toNumber():r.ints[h]}if(r.strings&&r.strings.length){a.strings=[];for(var h=0;h>>3){case 1:{p.name=r.string();break}case 2:{p.type=d.onnx.TypeProto.decode(r,r.uint32());break}case 3:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.type!=null&&r.hasOwnProperty("type")){var i=d.onnx.TypeProto.verify(r.type);if(i)return"type."+i}return r.docString!=null&&r.hasOwnProperty("docString")&&!l.isString(r.docString)?"docString: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.ValueInfoProto)return r;var i=new d.onnx.ValueInfoProto;if(r.name!=null&&(i.name=String(r.name)),r.type!=null){if(typeof r.type!="object")throw TypeError(".onnx.ValueInfoProto.type: object expected");i.type=d.onnx.TypeProto.fromObject(r.type)}return r.docString!=null&&(i.docString=String(r.docString)),i},s.toObject=function(r,i){i||(i={});var a={};return i.defaults&&(a.name="",a.type=null,a.docString=""),r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.type!=null&&r.hasOwnProperty("type")&&(a.type=d.onnx.TypeProto.toObject(r.type,i)),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.ValueInfoProto"},s})(),c.NodeProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 2:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 3:{p.name=r.string();break}case 4:{p.opType=r.string();break}case 7:{p.domain=r.string();break}case 5:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 6:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var i=0;i>>3){case 1:{p.initialization=d.onnx.GraphProto.decode(r,r.uint32());break}case 2:{p.algorithm=d.onnx.GraphProto.decode(r,r.uint32());break}case 3:{p.initializationBinding&&p.initializationBinding.length||(p.initializationBinding=[]),p.initializationBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 4:{p.updateBinding&&p.updateBinding.length||(p.updateBinding=[]),p.updateBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.initialization!=null&&r.hasOwnProperty("initialization")){var i=d.onnx.GraphProto.verify(r.initialization);if(i)return"initialization."+i}if(r.algorithm!=null&&r.hasOwnProperty("algorithm")){var i=d.onnx.GraphProto.verify(r.algorithm);if(i)return"algorithm."+i}if(r.initializationBinding!=null&&r.hasOwnProperty("initializationBinding")){if(!Array.isArray(r.initializationBinding))return"initializationBinding: array expected";for(var a=0;a>>3){case 1:{p.irVersion=r.int64();break}case 8:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 2:{p.producerName=r.string();break}case 3:{p.producerVersion=r.string();break}case 4:{p.domain=r.string();break}case 5:{p.modelVersion=r.int64();break}case 6:{p.docString=r.string();break}case 7:{p.graph=d.onnx.GraphProto.decode(r,r.uint32());break}case 14:{p.metadataProps&&p.metadataProps.length||(p.metadataProps=[]),p.metadataProps.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 20:{p.trainingInfo&&p.trainingInfo.length||(p.trainingInfo=[]),p.trainingInfo.push(d.onnx.TrainingInfoProto.decode(r,r.uint32()));break}case 25:{p.functions&&p.functions.length||(p.functions=[]),p.functions.push(d.onnx.FunctionProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.irVersion!=null&&r.hasOwnProperty("irVersion")&&!l.isInteger(r.irVersion)&&!(r.irVersion&&l.isInteger(r.irVersion.low)&&l.isInteger(r.irVersion.high)))return"irVersion: integer|Long expected";if(r.opsetImport!=null&&r.hasOwnProperty("opsetImport")){if(!Array.isArray(r.opsetImport))return"opsetImport: array expected";for(var i=0;i>>0,r.irVersion.high>>>0).toNumber())),r.opsetImport){if(!Array.isArray(r.opsetImport))throw TypeError(".onnx.ModelProto.opsetImport: array expected");i.opsetImport=[];for(var a=0;a>>0,r.modelVersion.high>>>0).toNumber())),r.docString!=null&&(i.docString=String(r.docString)),r.graph!=null){if(typeof r.graph!="object")throw TypeError(".onnx.ModelProto.graph: object expected");i.graph=d.onnx.GraphProto.fromObject(r.graph)}if(r.metadataProps){if(!Array.isArray(r.metadataProps))throw TypeError(".onnx.ModelProto.metadataProps: array expected");i.metadataProps=[];for(var a=0;a>>0,r.irVersion.high>>>0).toNumber():r.irVersion),r.producerName!=null&&r.hasOwnProperty("producerName")&&(a.producerName=r.producerName),r.producerVersion!=null&&r.hasOwnProperty("producerVersion")&&(a.producerVersion=r.producerVersion),r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.modelVersion!=null&&r.hasOwnProperty("modelVersion")&&(typeof r.modelVersion=="number"?a.modelVersion=i.longs===String?String(r.modelVersion):r.modelVersion:a.modelVersion=i.longs===String?l.Long.prototype.toString.call(r.modelVersion):i.longs===Number?new l.LongBits(r.modelVersion.low>>>0,r.modelVersion.high>>>0).toNumber():r.modelVersion),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.graph!=null&&r.hasOwnProperty("graph")&&(a.graph=d.onnx.GraphProto.toObject(r.graph,i)),r.opsetImport&&r.opsetImport.length){a.opsetImport=[];for(var h=0;h>>3){case 1:{p.key=r.string();break}case 2:{p.value=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.key!=null&&r.hasOwnProperty("key")&&!l.isString(r.key)?"key: string expected":r.value!=null&&r.hasOwnProperty("value")&&!l.isString(r.value)?"value: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.StringStringEntryProto)return r;var i=new d.onnx.StringStringEntryProto;return r.key!=null&&(i.key=String(r.key)),r.value!=null&&(i.value=String(r.value)),i},s.toObject=function(r,i){i||(i={});var a={};return i.defaults&&(a.key="",a.value=""),r.key!=null&&r.hasOwnProperty("key")&&(a.key=r.key),r.value!=null&&r.hasOwnProperty("value")&&(a.value=r.value),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.StringStringEntryProto"},s})(),c.TensorAnnotation=(function(){function s(r){if(this.quantParameterTensorNames=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.tensorName=r.string();break}case 2:{p.quantParameterTensorNames&&p.quantParameterTensorNames.length||(p.quantParameterTensorNames=[]),p.quantParameterTensorNames.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.tensorName!=null&&r.hasOwnProperty("tensorName")&&!l.isString(r.tensorName))return"tensorName: string expected";if(r.quantParameterTensorNames!=null&&r.hasOwnProperty("quantParameterTensorNames")){if(!Array.isArray(r.quantParameterTensorNames))return"quantParameterTensorNames: array expected";for(var i=0;i>>3){case 1:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 2:{p.name=r.string();break}case 5:{p.initializer&&p.initializer.length||(p.initializer=[]),p.initializer.push(d.onnx.TensorProto.decode(r,r.uint32()));break}case 15:{p.sparseInitializer&&p.sparseInitializer.length||(p.sparseInitializer=[]),p.sparseInitializer.push(d.onnx.SparseTensorProto.decode(r,r.uint32()));break}case 10:{p.docString=r.string();break}case 11:{p.input&&p.input.length||(p.input=[]),p.input.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 12:{p.output&&p.output.length||(p.output=[]),p.output.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 13:{p.valueInfo&&p.valueInfo.length||(p.valueInfo=[]),p.valueInfo.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 14:{p.quantizationAnnotation&&p.quantizationAnnotation.length||(p.quantizationAnnotation=[]),p.quantizationAnnotation.push(d.onnx.TensorAnnotation.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.node!=null&&r.hasOwnProperty("node")){if(!Array.isArray(r.node))return"node: array expected";for(var i=0;i>>3){case 1:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}if(r.dataType!=null&&(i.dataType=r.dataType|0),r.segment!=null){if(typeof r.segment!="object")throw TypeError(".onnx.TensorProto.segment: object expected");i.segment=d.onnx.TensorProto.Segment.fromObject(r.segment)}if(r.floatData){if(!Array.isArray(r.floatData))throw TypeError(".onnx.TensorProto.floatData: array expected");i.floatData=[];for(var a=0;a=0&&(i.stringData[a]=r.stringData[a])}if(r.int64Data){if(!Array.isArray(r.int64Data))throw TypeError(".onnx.TensorProto.int64Data: array expected");i.int64Data=[];for(var a=0;a>>0,r.int64Data[a].high>>>0).toNumber())}if(r.name!=null&&(i.name=String(r.name)),r.docString!=null&&(i.docString=String(r.docString)),r.rawData!=null&&(typeof r.rawData=="string"?l.base64.decode(r.rawData,i.rawData=l.newBuffer(l.base64.length(r.rawData)),0):r.rawData.length>=0&&(i.rawData=r.rawData)),r.externalData){if(!Array.isArray(r.externalData))throw TypeError(".onnx.TensorProto.externalData: array expected");i.externalData=[];for(var a=0;a>>0,r.uint64Data[a].high>>>0).toNumber(!0))}return i},s.toObject=function(r,i){i||(i={});var a={};if((i.arrays||i.defaults)&&(a.dims=[],a.floatData=[],a.int32Data=[],a.stringData=[],a.int64Data=[],a.doubleData=[],a.uint64Data=[],a.externalData=[]),i.defaults&&(a.dataType=0,a.segment=null,a.name="",i.bytes===String?a.rawData="":(a.rawData=[],i.bytes!==Array&&(a.rawData=l.newBuffer(a.rawData))),a.docString="",a.dataLocation=i.enums===String?"DEFAULT":0),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}if(r.dataType!=null&&r.hasOwnProperty("dataType")&&(a.dataType=r.dataType),r.segment!=null&&r.hasOwnProperty("segment")&&(a.segment=d.onnx.TensorProto.Segment.toObject(r.segment,i)),r.floatData&&r.floatData.length){a.floatData=[];for(var p=0;p>>0,r.int64Data[p].high>>>0).toNumber():r.int64Data[p]}if(r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.rawData!=null&&r.hasOwnProperty("rawData")&&(a.rawData=i.bytes===String?l.base64.encode(r.rawData,0,r.rawData.length):i.bytes===Array?Array.prototype.slice.call(r.rawData):r.rawData),r.doubleData&&r.doubleData.length){a.doubleData=[];for(var p=0;p>>0,r.uint64Data[p].high>>>0).toNumber(!0):r.uint64Data[p]}if(r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.externalData&&r.externalData.length){a.externalData=[];for(var p=0;p>>3){case 1:{h.begin=i.int64();break}case 2:{h.end=i.int64();break}default:i.skipType(m&7);break}}return h},r.decodeDelimited=function(i){return i instanceof o||(i=new o(i)),this.decode(i,i.uint32())},r.verify=function(i){return typeof i!="object"||i===null?"object expected":i.begin!=null&&i.hasOwnProperty("begin")&&!l.isInteger(i.begin)&&!(i.begin&&l.isInteger(i.begin.low)&&l.isInteger(i.begin.high))?"begin: integer|Long expected":i.end!=null&&i.hasOwnProperty("end")&&!l.isInteger(i.end)&&!(i.end&&l.isInteger(i.end.low)&&l.isInteger(i.end.high))?"end: integer|Long expected":null},r.fromObject=function(i){if(i instanceof d.onnx.TensorProto.Segment)return i;var a=new d.onnx.TensorProto.Segment;return i.begin!=null&&(l.Long?(a.begin=l.Long.fromValue(i.begin)).unsigned=!1:typeof i.begin=="string"?a.begin=parseInt(i.begin,10):typeof i.begin=="number"?a.begin=i.begin:typeof i.begin=="object"&&(a.begin=new l.LongBits(i.begin.low>>>0,i.begin.high>>>0).toNumber())),i.end!=null&&(l.Long?(a.end=l.Long.fromValue(i.end)).unsigned=!1:typeof i.end=="string"?a.end=parseInt(i.end,10):typeof i.end=="number"?a.end=i.end:typeof i.end=="object"&&(a.end=new l.LongBits(i.end.low>>>0,i.end.high>>>0).toNumber())),a},r.toObject=function(i,a){a||(a={});var p={};if(a.defaults){if(l.Long){var h=new l.Long(0,0,!1);p.begin=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.begin=a.longs===String?"0":0;if(l.Long){var h=new l.Long(0,0,!1);p.end=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.end=a.longs===String?"0":0}return i.begin!=null&&i.hasOwnProperty("begin")&&(typeof i.begin=="number"?p.begin=a.longs===String?String(i.begin):i.begin:p.begin=a.longs===String?l.Long.prototype.toString.call(i.begin):a.longs===Number?new l.LongBits(i.begin.low>>>0,i.begin.high>>>0).toNumber():i.begin),i.end!=null&&i.hasOwnProperty("end")&&(typeof i.end=="number"?p.end=a.longs===String?String(i.end):i.end:p.end=a.longs===String?l.Long.prototype.toString.call(i.end):a.longs===Number?new l.LongBits(i.end.low>>>0,i.end.high>>>0).toNumber():i.end),p},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(i){return i===void 0&&(i="type.googleapis.com"),i+"/onnx.TensorProto.Segment"},r})(),s.DataLocation=(function(){var r={},i=Object.create(r);return i[r[0]="DEFAULT"]=0,i[r[1]="EXTERNAL"]=1,i})(),s})(),c.SparseTensorProto=(function(){function s(r){if(this.dims=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.values=d.onnx.TensorProto.decode(r,r.uint32());break}case 2:{p.indices=d.onnx.TensorProto.decode(r,r.uint32());break}case 3:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}return i},s.toObject=function(r,i){i||(i={});var a={};if((i.arrays||i.defaults)&&(a.dims=[]),i.defaults&&(a.values=null,a.indices=null),r.values!=null&&r.hasOwnProperty("values")&&(a.values=d.onnx.TensorProto.toObject(r.values,i)),r.indices!=null&&r.hasOwnProperty("indices")&&(a.indices=d.onnx.TensorProto.toObject(r.indices,i)),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}return a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.SparseTensorProto"},s})(),c.TensorShapeProto=(function(){function s(r){if(this.dim=[],r)for(var i=Object.keys(r),a=0;a>>3===1?(p.dim&&p.dim.length||(p.dim=[]),p.dim.push(d.onnx.TensorShapeProto.Dimension.decode(r,r.uint32()))):r.skipType(h&7)}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.dim!=null&&r.hasOwnProperty("dim")){if(!Array.isArray(r.dim))return"dim: array expected";for(var i=0;i>>3){case 1:{m.dimValue=a.int64();break}case 2:{m.dimParam=a.string();break}case 3:{m.denotation=a.string();break}default:a.skipType(b&7);break}}return m},r.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},r.verify=function(a){if(typeof a!="object"||a===null)return"object expected";var p={};if(a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(p.value=1,!l.isInteger(a.dimValue)&&!(a.dimValue&&l.isInteger(a.dimValue.low)&&l.isInteger(a.dimValue.high))))return"dimValue: integer|Long expected";if(a.dimParam!=null&&a.hasOwnProperty("dimParam")){if(p.value===1)return"value: multiple values";if(p.value=1,!l.isString(a.dimParam))return"dimParam: string expected"}return a.denotation!=null&&a.hasOwnProperty("denotation")&&!l.isString(a.denotation)?"denotation: string expected":null},r.fromObject=function(a){if(a instanceof d.onnx.TensorShapeProto.Dimension)return a;var p=new d.onnx.TensorShapeProto.Dimension;return a.dimValue!=null&&(l.Long?(p.dimValue=l.Long.fromValue(a.dimValue)).unsigned=!1:typeof a.dimValue=="string"?p.dimValue=parseInt(a.dimValue,10):typeof a.dimValue=="number"?p.dimValue=a.dimValue:typeof a.dimValue=="object"&&(p.dimValue=new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber())),a.dimParam!=null&&(p.dimParam=String(a.dimParam)),a.denotation!=null&&(p.denotation=String(a.denotation)),p},r.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.denotation=""),a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(typeof a.dimValue=="number"?h.dimValue=p.longs===String?String(a.dimValue):a.dimValue:h.dimValue=p.longs===String?l.Long.prototype.toString.call(a.dimValue):p.longs===Number?new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber():a.dimValue,p.oneofs&&(h.value="dimValue")),a.dimParam!=null&&a.hasOwnProperty("dimParam")&&(h.dimParam=a.dimParam,p.oneofs&&(h.value="dimParam")),a.denotation!=null&&a.hasOwnProperty("denotation")&&(h.denotation=a.denotation),h},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TensorShapeProto.Dimension"},r})(),s})(),c.TypeProto=(function(){function s(i){if(i)for(var a=Object.keys(i),p=0;p>>3){case 1:{h.tensorType=d.onnx.TypeProto.Tensor.decode(i,i.uint32());break}case 4:{h.sequenceType=d.onnx.TypeProto.Sequence.decode(i,i.uint32());break}case 5:{h.mapType=d.onnx.TypeProto.Map.decode(i,i.uint32());break}case 9:{h.optionalType=d.onnx.TypeProto.Optional.decode(i,i.uint32());break}case 8:{h.sparseTensorType=d.onnx.TypeProto.SparseTensor.decode(i,i.uint32());break}case 6:{h.denotation=i.string();break}default:i.skipType(m&7);break}}return h},s.decodeDelimited=function(i){return i instanceof o||(i=new o(i)),this.decode(i,i.uint32())},s.verify=function(i){if(typeof i!="object"||i===null)return"object expected";var a={};if(i.tensorType!=null&&i.hasOwnProperty("tensorType")){a.value=1;{var p=d.onnx.TypeProto.Tensor.verify(i.tensorType);if(p)return"tensorType."+p}}if(i.sequenceType!=null&&i.hasOwnProperty("sequenceType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Sequence.verify(i.sequenceType);if(p)return"sequenceType."+p}}if(i.mapType!=null&&i.hasOwnProperty("mapType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Map.verify(i.mapType);if(p)return"mapType."+p}}if(i.optionalType!=null&&i.hasOwnProperty("optionalType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Optional.verify(i.optionalType);if(p)return"optionalType."+p}}if(i.sparseTensorType!=null&&i.hasOwnProperty("sparseTensorType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.SparseTensor.verify(i.sparseTensorType);if(p)return"sparseTensorType."+p}}return i.denotation!=null&&i.hasOwnProperty("denotation")&&!l.isString(i.denotation)?"denotation: string expected":null},s.fromObject=function(i){if(i instanceof d.onnx.TypeProto)return i;var a=new d.onnx.TypeProto;if(i.tensorType!=null){if(typeof i.tensorType!="object")throw TypeError(".onnx.TypeProto.tensorType: object expected");a.tensorType=d.onnx.TypeProto.Tensor.fromObject(i.tensorType)}if(i.sequenceType!=null){if(typeof i.sequenceType!="object")throw TypeError(".onnx.TypeProto.sequenceType: object expected");a.sequenceType=d.onnx.TypeProto.Sequence.fromObject(i.sequenceType)}if(i.mapType!=null){if(typeof i.mapType!="object")throw TypeError(".onnx.TypeProto.mapType: object expected");a.mapType=d.onnx.TypeProto.Map.fromObject(i.mapType)}if(i.optionalType!=null){if(typeof i.optionalType!="object")throw TypeError(".onnx.TypeProto.optionalType: object expected");a.optionalType=d.onnx.TypeProto.Optional.fromObject(i.optionalType)}if(i.sparseTensorType!=null){if(typeof i.sparseTensorType!="object")throw TypeError(".onnx.TypeProto.sparseTensorType: object expected");a.sparseTensorType=d.onnx.TypeProto.SparseTensor.fromObject(i.sparseTensorType)}return i.denotation!=null&&(a.denotation=String(i.denotation)),a},s.toObject=function(i,a){a||(a={});var p={};return a.defaults&&(p.denotation=""),i.tensorType!=null&&i.hasOwnProperty("tensorType")&&(p.tensorType=d.onnx.TypeProto.Tensor.toObject(i.tensorType,a),a.oneofs&&(p.value="tensorType")),i.sequenceType!=null&&i.hasOwnProperty("sequenceType")&&(p.sequenceType=d.onnx.TypeProto.Sequence.toObject(i.sequenceType,a),a.oneofs&&(p.value="sequenceType")),i.mapType!=null&&i.hasOwnProperty("mapType")&&(p.mapType=d.onnx.TypeProto.Map.toObject(i.mapType,a),a.oneofs&&(p.value="mapType")),i.denotation!=null&&i.hasOwnProperty("denotation")&&(p.denotation=i.denotation),i.sparseTensorType!=null&&i.hasOwnProperty("sparseTensorType")&&(p.sparseTensorType=d.onnx.TypeProto.SparseTensor.toObject(i.sparseTensorType,a),a.oneofs&&(p.value="sparseTensorType")),i.optionalType!=null&&i.hasOwnProperty("optionalType")&&(p.optionalType=d.onnx.TypeProto.Optional.toObject(i.optionalType,a),a.oneofs&&(p.value="optionalType")),p},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(i){return i===void 0&&(i="type.googleapis.com"),i+"/onnx.TypeProto"},s.Tensor=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(b&7);break}}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Tensor)return a;var p=new d.onnx.TypeProto.Tensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.Tensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Tensor"},i})(),s.Sequence=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(b&7)}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Sequence)return a;var p=new d.onnx.TypeProto.Sequence;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Sequence.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Sequence"},i})(),s.Map=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.keyType=a.int32();break}case 2:{m.valueType=d.onnx.TypeProto.decode(a,a.uint32());break}default:a.skipType(b&7);break}}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.keyType!=null&&a.hasOwnProperty("keyType")&&!l.isInteger(a.keyType))return"keyType: integer expected";if(a.valueType!=null&&a.hasOwnProperty("valueType")){var p=d.onnx.TypeProto.verify(a.valueType);if(p)return"valueType."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Map)return a;var p=new d.onnx.TypeProto.Map;if(a.keyType!=null&&(p.keyType=a.keyType|0),a.valueType!=null){if(typeof a.valueType!="object")throw TypeError(".onnx.TypeProto.Map.valueType: object expected");p.valueType=d.onnx.TypeProto.fromObject(a.valueType)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.keyType=0,h.valueType=null),a.keyType!=null&&a.hasOwnProperty("keyType")&&(h.keyType=a.keyType),a.valueType!=null&&a.hasOwnProperty("valueType")&&(h.valueType=d.onnx.TypeProto.toObject(a.valueType,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Map"},i})(),s.Optional=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(b&7)}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Optional)return a;var p=new d.onnx.TypeProto.Optional;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Optional.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Optional"},i})(),s.SparseTensor=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(b&7);break}}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.SparseTensor)return a;var p=new d.onnx.TypeProto.SparseTensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.SparseTensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.SparseTensor"},i})(),s})(),c.OperatorSetIdProto=(function(){function s(r){if(r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.domain=r.string();break}case 2:{p.version=r.int64();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.domain!=null&&r.hasOwnProperty("domain")&&!l.isString(r.domain)?"domain: string expected":r.version!=null&&r.hasOwnProperty("version")&&!l.isInteger(r.version)&&!(r.version&&l.isInteger(r.version.low)&&l.isInteger(r.version.high))?"version: integer|Long expected":null},s.fromObject=function(r){if(r instanceof d.onnx.OperatorSetIdProto)return r;var i=new d.onnx.OperatorSetIdProto;return r.domain!=null&&(i.domain=String(r.domain)),r.version!=null&&(l.Long?(i.version=l.Long.fromValue(r.version)).unsigned=!1:typeof r.version=="string"?i.version=parseInt(r.version,10):typeof r.version=="number"?i.version=r.version:typeof r.version=="object"&&(i.version=new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber())),i},s.toObject=function(r,i){i||(i={});var a={};if(i.defaults)if(a.domain="",l.Long){var p=new l.Long(0,0,!1);a.version=i.longs===String?p.toString():i.longs===Number?p.toNumber():p}else a.version=i.longs===String?"0":0;return r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.version!=null&&r.hasOwnProperty("version")&&(typeof r.version=="number"?a.version=i.longs===String?String(r.version):r.version:a.version=i.longs===String?l.Long.prototype.toString.call(r.version):i.longs===Number?new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber():r.version),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.OperatorSetIdProto"},s})(),c.OperatorStatus=(function(){var s={},r=Object.create(s);return r[s[0]="EXPERIMENTAL"]=0,r[s[1]="STABLE"]=1,r})(),c.FunctionProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],this.attributeProto=[],this.node=[],this.opsetImport=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 4:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 5:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 6:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(r.string());break}case 11:{p.attributeProto&&p.attributeProto.length||(p.attributeProto=[]),p.attributeProto.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 7:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 8:{p.docString=r.string();break}case 9:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 10:{p.domain=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var i=0;i{"use strict";Ky(),Me=fe(_i()),Tn(),ni=class{static arraysEqual(e,t){if(e.length!==t.length)return!1;for(let n=0;n1&&i>1)return;c[d-s]=Math.max(r,i)}return c}static index(t,n){let o=new Array(n.length);return ii.fillIndex(t,n,o),o}static fillIndex(t,n,o){let u=t.length-n.length;for(let l=0;l=0;y--)r[y]=$%d[y],$=Math.floor($/d[y]);m||(ii.fillIndex(r,t.dims,i),p=t.get(i)),b||(ii.fillIndex(r,n.dims,a),h=n.get(a)),s.set(r,o(p,h))}}return s}}static isValidBroadcast(t,n){let o=t.length,u=n.length;if(o>u)return!1;for(let l=1;l<=o;l++)if(t[o-l]!==1&&t[o-l]!==n[u-l])return!1;return!0}static getBroadcastDims(t,n){let o=t.length,u=[];for(let l=0;l1&&c===1&&u.unshift(d)}return u}},B0=class{static getShapeOfGemmResult(e,t,n,o,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(o?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!Ut.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},pt=class Cu{static tensorDataTypeFromProto(t){switch(t){case Me.onnx.TensorProto.DataType.INT8:return"int8";case Me.onnx.TensorProto.DataType.UINT8:return"uint8";case Me.onnx.TensorProto.DataType.BOOL:return"bool";case Me.onnx.TensorProto.DataType.INT16:return"int16";case Me.onnx.TensorProto.DataType.UINT16:return"uint16";case Me.onnx.TensorProto.DataType.INT32:return"int32";case Me.onnx.TensorProto.DataType.UINT32:return"uint32";case Me.onnx.TensorProto.DataType.FLOAT:return"float32";case Me.onnx.TensorProto.DataType.DOUBLE:return"float64";case Me.onnx.TensorProto.DataType.STRING:return"string";case Me.onnx.TensorProto.DataType.INT64:return"int32";case Me.onnx.TensorProto.DataType.UINT64:return"uint32";default:throw new Error(`unsupported data type: ${Me.onnx.TensorProto.DataType[t]}`)}}static tensorDataTypeStringToEnum(t){switch(t){case"int8":return Me.onnx.TensorProto.DataType.INT8;case"uint8":return Me.onnx.TensorProto.DataType.UINT8;case"bool":return Me.onnx.TensorProto.DataType.BOOL;case"int16":return Me.onnx.TensorProto.DataType.INT16;case"uint16":return Me.onnx.TensorProto.DataType.UINT16;case"int32":return Me.onnx.TensorProto.DataType.INT32;case"uint32":return Me.onnx.TensorProto.DataType.UINT32;case"float32":return Me.onnx.TensorProto.DataType.FLOAT;case"float64":return Me.onnx.TensorProto.DataType.DOUBLE;case"string":return Me.onnx.TensorProto.DataType.STRING;case"int64":return Me.onnx.TensorProto.DataType.INT64;case"uint64":return Me.onnx.TensorProto.DataType.UINT64;default:throw new Error(`unsupported data type: ${t}`)}}static tensorDimsFromProto(t){return t.map(n=>Qr.isLong(n)?n.toNumber():n)}static tensorValueTypeFromProto(t){return{tensorType:Cu.tensorDataTypeFromProto(t.elemType),shape:{dims:Cu.tensorDimsFromProto(t.shape.dim.map(n=>n.dimValue))}}}static tensorDimsFromORTFormat(t){let n=[];for(let o=0;ot.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return hr.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return hr.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,o){let u=1;for(let l=n;l=0;--u)o[u]=o[u+1]*t[u+1];return o}static transpose(t){return t.slice().reverse()}static indicesToOffset(t,n,o){o===void 0&&(o=t.length);let u=0;for(let l=0;l=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(o=>this.normalizeAxis(o,n))}static incrementIndex(t,n,o){if(n.length===0||t.length===0)throw new Error("Index incrementing unsupported for scalar Tensor");if(o===void 0)o=n.length;else if(o<=0||o>n.length)throw new Error("Incorrect axis to increment on");for(let u=o-1;u>=0&&(t[u]++,!(t[u]=t.length)throw new Error("the dimension with value zero exceeds the dimension size of the input tensor");u[s]=t[s]}else u[s]=n[s];d*=u[s]}}let c=hr.size(t);if(l!==-1){if(c%d!==0)throw new Error(`the input tensor cannot be reshaped to the requested shape. Input shape: [${t}] Output shape: [${n}]`);u[l]=c/d}else if(d!==c)throw new Error("reshapedDims and originalDims don't have matching sizes");return u}static sortBasedOnPerm(t,n){return n?n.map(o=>t[o]):t.slice().reverse()}static padShape(t,n){let o=t.length;return t.map((u,l)=>u+n[l]+n[l+o])}static areEqual(t,n){return t.length!==n.length?!1:t.every((o,u)=>o===n[u])}static validateDimsAndCalcSize(t){if(t.length>6)throw new TypeError("Only rank 0 to 6 is supported for tensor shape.");let n=1;for(let o of t){if(!Number.isInteger(o))throw new TypeError(`Invalid shape: ${o} is not an integer`);if(o<0||o>2147483647)throw new TypeError(`Invalid shape: length ${o} is not allowed`);n*=o}return n}static flattenShape(t,n){n<0&&(n+=t.length);let o=t.reduce((l,d)=>l*d,1),u=t.slice(n).reduce((l,d)=>l*d,1);return[o/u,u]}static squeezeShape(t,n){let o=new Array;n=hr.normalizeAxes(n,t.length);for(let u=0;u=0;if(l&&t[u]!==1)throw new Error("squeeze an axis of size different than 1");(n.length===0&&t[u]>1||n.length>0&&!l)&&o.push(t[u])}return o}static unsqueezeShape(t,n){let o=new Array(t.length+n.length);o.fill(0);for(let l=0;l=o.length)throw new Error("'axes' has an out of range axis");if(o[d]!==0)throw new Error("'axes' has a duplicate axis");o[d]=1}let u=0;for(let l=0;l=o.length?o.push(n[c+2]):o[c]=n[c+2];for(let c=0;c=o[c]||d[c+o.length]>=o[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,o,u,l,d){if(d){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let c=0;c=o+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,o,u,l,d,c,s,r=0){let i=o*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,fr.computeOutputSize(t-i,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(o!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],fr.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return fr.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}},mi=-34028234663852886e22,gi=34028234663852886e22});function A3(e){switch(e){case"bool":case"int8":case"uint8":return 1;case"int16":case"uint16":return 2;case"int32":case"uint32":case"float32":return 4;case"float64":return 8;default:throw new Error(`cannot calculate sizeof() on type ${e}`)}}function Ap(e){switch(e){case ye.onnx.TensorProto.DataType.UINT8:case ye.onnx.TensorProto.DataType.INT8:case ye.onnx.TensorProto.DataType.BOOL:return 1;case ye.onnx.TensorProto.DataType.UINT16:case ye.onnx.TensorProto.DataType.INT16:return 2;case ye.onnx.TensorProto.DataType.FLOAT:case ye.onnx.TensorProto.DataType.INT32:case ye.onnx.TensorProto.DataType.UINT32:return 4;case ye.onnx.TensorProto.DataType.INT64:case ye.onnx.TensorProto.DataType.DOUBLE:case ye.onnx.TensorProto.DataType.UINT64:return 8;default:throw new Error(`cannot calculate sizeof() on type ${ye.onnx.TensorProto.DataType[e]}`)}}function k3(e,t){return new(L0(t))(e)}function L0(e){switch(e){case"bool":case"uint8":return Uint8Array;case"int8":return Int8Array;case"int16":return Int16Array;case"uint16":return Uint16Array;case"int32":return Int32Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"float32":return Float32Array;case"float64":return Float64Array;default:throw new Error("unspecified error")}}function Du(e,t){if(t===ye.onnx.TensorProto.DataType.INT64||t===go.TensorDataType.INT64){if(e.greaterThanOrEqual(2147483648)||e.lessThan(-2147483648))throw new TypeError("int64 is not supported")}else if(t===ye.onnx.TensorProto.DataType.UINT32||t===go.TensorDataType.UINT32||t===ye.onnx.TensorProto.DataType.UINT64||t===go.TensorDataType.UINT64){if(e.greaterThanOrEqual(4294967296)||e.lessThan(0))throw new TypeError("uint64 is not supported")}else throw new TypeError(`not a LONG type: ${ye.onnx.TensorProto.DataType[t]}`);return e.toNumber()}function kp(e,t,n){switch(t){case ye.onnx.TensorProto.DataType.BOOL:case ye.onnx.TensorProto.DataType.UINT8:return e.getUint8(n);case ye.onnx.TensorProto.DataType.INT8:return e.getInt8(n);case ye.onnx.TensorProto.DataType.UINT16:return e.getUint16(n,!0);case ye.onnx.TensorProto.DataType.INT16:return e.getInt16(n,!0);case ye.onnx.TensorProto.DataType.FLOAT:return e.getFloat32(n,!0);case ye.onnx.TensorProto.DataType.INT32:return e.getInt32(n,!0);case ye.onnx.TensorProto.DataType.UINT32:return e.getUint32(n,!0);case ye.onnx.TensorProto.DataType.INT64:return Du(Qr.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!1),t);case ye.onnx.TensorProto.DataType.DOUBLE:return e.getFloat64(n,!0);case ye.onnx.TensorProto.DataType.UINT64:return Du(Qr.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!0),t);default:throw new Error(`cannot read from DataView for type ${ye.onnx.TensorProto.DataType[t]}`)}}var Pp,ye,mt,Tn=D(()=>{"use strict";Pp=fe(DI()),Ky(),Mo(),ye=fe(_i()),Ce(),mt=class bo{constructor(t,n,o,u,l,d=Pp.Guid.create()){this.dims=t,this.type=n,this.dataProvider=o,this.asyncDataProvider=u,this.cache=l,this.dataId=d,this.size=me.validateDimsAndCalcSize(t);let c=this.size,s=o===void 0&&u===void 0&&l===void 0;if(l!==void 0&&l.length!==c)throw new RangeError("Input dims doesn't match data length.");if(n==="string"){if(l!==void 0&&(!Array.isArray(l)||!l.every(r=>typeof r=="string")))throw new TypeError("cache should be a string array");s&&(this.cache=new Array(c))}else{if(l!==void 0){let r=L0(n);if(!(l instanceof r))throw new TypeError(`cache should be type ${r.name}`)}if(s){let r=new ArrayBuffer(c*A3(n));this.cache=k3(r,n)}}}get data(){if(this.cache===void 0){let t=this.dataProvider(this.dataId);if(t.length!==this.size)throw new Error("Length of data provided by the Data Provider is inconsistent with the dims of this Tensor.");this.cache=t}return this.cache}get stringData(){if(this.type!=="string")throw new TypeError("data type is not string");return this.data}get integerData(){switch(this.type){case"uint8":case"int8":case"uint16":case"int16":case"int32":case"uint32":case"bool":return this.data;default:throw new TypeError("data type is not integer (uint8, int8, uint16, int16, int32, uint32, bool)")}}get floatData(){switch(this.type){case"float32":case"float64":return this.data;default:throw new TypeError("data type is not float (float32, float64)")}}get numberData(){if(this.type!=="string")return this.data;throw new TypeError("type cannot be non-number (string)")}get(t){return this.data[me.indicesToOffset(t,this.strides)]}set(t,n){this.data[me.indicesToOffset(t,this.strides)]=n}async getData(){return this.cache===void 0&&(this.cache=await this.asyncDataProvider(this.dataId)),this.cache}get strides(){return this._strides||(this._strides=me.computeStrides(this.dims)),this._strides}static fromProto(t){if(!t)throw new Error("cannot construct Value from an empty tensor");let n=pt.tensorDataTypeFromProto(t.dataType),o=pt.tensorDimsFromProto(t.dims),u=new bo(o,n);if(n==="string")t.stringData.forEach((l,d)=>{u.data[d]=ku(l)});else if(t.rawData&&typeof t.rawData.byteLength=="number"&&t.rawData.byteLength>0){let l=u.data,d=new DataView(t.rawData.buffer,t.rawData.byteOffset,t.rawData.byteLength),c=Ap(t.dataType),s=t.rawData.byteLength/c;if(t.rawData.byteLength%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;r0){let l=u.data,d=new DataView(t.rawDataArray().buffer,t.rawDataArray().byteOffset,t.rawDataLength()),c=Ap(t.dataType()),s=t.rawDataLength()/c;if(t.rawDataLength()%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;rtypeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var m3=Object.create,Jo=Object.defineProperty,g3=Object.getOwnPropertyDescriptor,b3=Object.getOwnPropertyNames,y3=Object.getPrototypeOf,w3=Object.prototype.hasOwnProperty,Bu=(e=>typeof jr<"u"?jr:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof jr<"u"?jr:t)[n]}):e)(function(e){if(typeof jr<"u")return jr.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')}),D=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(i){throw n=[i],i}},ae=(e,t)=>()=>{try{return t||e((t={exports:{}}).exports,t),t.exports}catch(n){throw t=0,n}},wn=(e,t)=>{for(var n in t)Jo(e,n,{get:t[n],enumerable:!0})},l0=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of b3(t))!w3.call(e,u)&&u!==n&&Jo(e,u,{get:()=>t[u],enumerable:!(i=g3(t,u))||i.enumerable});return e},be=(e,t,n)=>(n=e!=null?m3(y3(e)):{},l0(t||!e||!e.__esModule?Jo(n,"default",{value:e,enumerable:!0}):n,e)),Nn=e=>l0(Jo({},"__esModule",{value:!0}),e),ri,br,pn,oh,d0,c0=D(()=>{"use strict";ri=new Map,br=[],pn=(e,t,n)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let i=ri.get(e);if(i===void 0)ri.set(e,{backend:t,priority:n});else{if(i.priority>n)return;if(i.priority===n&&i.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){let u=br.indexOf(e);u!==-1&&br.splice(u,1);for(let l=0;l{let t=ri.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let n=!!t.initPromise;try{return n||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(i){return n||(t.error=`${i}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},d0=async e=>{let t=e.executionProviders||[],n=t.map(s=>typeof s=="string"?s:s.name),i=n.length===0?br:n,u,l=[],d=new Set;for(let s of i){let r=await oh(s);typeof r=="string"?l.push({name:s,err:r}):(u||(u=r),u===r&&d.add(s))}if(!u)throw new Error(`no available backend found. ERR: ${l.map(s=>`[${s.name}] ${s.err}`).join(", ")}`);for(let{name:s,err:r}of l)n.includes(s)&&console.warn(`removing requested execution provider "${s}" from session options because it is not available: ${r}`);let c=t.filter(s=>d.has(typeof s=="string"?s:s.name));return[u,new Proxy(e,{get:(s,r)=>r==="executionProviders"?c:Reflect.get(s,r)})]}}),_3=D(()=>{"use strict";c0()}),p0,v3=D(()=>{"use strict";p0="1.29.0"}),aa,st,h0=D(()=>{"use strict";v3(),aa="warning",st={wasm:{},webgl:{},webgpu:{},versions:{common:p0},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);aa=e}},get logLevel(){return aa}},Object.defineProperty(st,"logLevel",{enumerable:!0})}),me,x3=D(()=>{"use strict";h0(),me=st}),f0,m0,$3=D(()=>{"use strict";f0=(e,t)=>{let n=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);n.width=e.dims[3],n.height=e.dims[2];let i=n.getContext("2d");if(i!=null){let u,l;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[3]):(u=e.dims[3],l=e.dims[2]);let d=t?.format!==void 0?t.format:"RGB",c=t?.norm,s,r;c===void 0||c.mean===void 0?s=[255,255,255,255]:typeof c.mean=="number"?s=[c.mean,c.mean,c.mean,c.mean]:(s=[c.mean[0],c.mean[1],c.mean[2],0],c.mean[3]!==void 0&&(s[3]=c.mean[3])),c===void 0||c.bias===void 0?r=[0,0,0,0]:typeof c.bias=="number"?r=[c.bias,c.bias,c.bias,c.bias]:(r=[c.bias[0],c.bias[1],c.bias[2],0],c.bias[3]!==void 0&&(r[3]=c.bias[3]));let o=l*u,a=0,p=o,h=o*2,m=-1;d==="RGBA"?(a=0,p=o,h=o*2,m=o*3):d==="RGB"?(a=0,p=o,h=o*2):d==="RBG"&&(a=0,h=o,p=o*2);for(let g=0;g{let n=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),i;if(n!=null){let u,l,d;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[1],d=e.dims[3]):(u=e.dims[3],l=e.dims[2],d=e.dims[1]);let c=t!==void 0&&t.format!==void 0?t.format:"RGB",s=t?.norm,r,o;s===void 0||s.mean===void 0?r=[255,255,255,255]:typeof s.mean=="number"?r=[s.mean,s.mean,s.mean,s.mean]:(r=[s.mean[0],s.mean[1],s.mean[2],255],s.mean[3]!==void 0&&(r[3]=s.mean[3])),s===void 0||s.bias===void 0?o=[0,0,0,0]:typeof s.bias=="number"?o=[s.bias,s.bias,s.bias,s.bias]:(o=[s.bias[0],s.bias[1],s.bias[2],0],s.bias[3]!==void 0&&(o[3]=s.bias[3]));let a=l*u;if(t!==void 0&&(t.format!==void 0&&d===4&&t.format!=="RGBA"||d===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let p=4,h=0,m=1,g=2,_=3,w=0,y=a,T=a*2,S=-1;c==="RGBA"?(w=0,y=a,T=a*2,S=a*3):c==="RGB"?(w=0,y=a,T=a*2):c==="RBG"&&(w=0,T=a,y=a*2),i=n.createImageData(u,l);for(let O=0;O{"use strict";ql(),yo=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:n,width:i}=t,u=t.norm??{mean:255,bias:0},l,d;typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:l=[u.mean[0],u.mean[1],u.mean[2],u.mean[3]??255],typeof u.bias=="number"?d=[u.bias,u.bias,u.bias,u.bias]:d=[u.bias[0],u.bias[1],u.bias[2],u.bias[3]??0];let c=t.format!==void 0?t.format:"RGBA",s=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",r=n*i,o=s==="RGBA"?new Float32Array(r*4):new Float32Array(r*3),a=4,p=0,h=1,m=2,g=3,_=0,w=r,y=r*2,T=-1;c==="RGB"&&(a=3,p=0,h=1,m=2,g=-1),s==="RGBA"?T=r*3:s==="RBG"?(_=0,y=r,w=r*2):s==="BGR"&&(y=0,w=r,_=r*2);for(let S=0;S{let n=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,i=typeof ImageData<"u"&&e instanceof ImageData,u=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,l=typeof e=="string",d,c=t??{},s=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},r=o=>typeof HTMLCanvasElement<"u"&&o instanceof HTMLCanvasElement||o instanceof OffscreenCanvas?o.getContext("2d"):null;if(n){let o=s();o.width=e.width,o.height=e.height;let a=r(o);if(a!=null){let p=e.height,h=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(p=t.resizedHeight,h=t.resizedWidth),t!==void 0){if(c=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");c.tensorFormat="RGBA",c.height=p,c.width=h}else c.tensorFormat="RGBA",c.height=p,c.width=h;a.drawImage(e,0,0),d=a.getImageData(0,0,h,p).data}else throw new Error("Can not access image data")}else if(i){let o,a;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(o=t.resizedHeight,a=t.resizedWidth):(o=e.height,a=e.width),t!==void 0&&(c=t),c.format="RGBA",c.height=o,c.width=a,t!==void 0){let p=s();p.width=a,p.height=o;let h=r(p);if(h!=null)h.putImageData(e,0,0),d=h.getImageData(0,0,a,o).data;else throw new Error("Can not access image data")}else d=e.data}else if(u){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let o=s();o.width=e.width,o.height=e.height;let a=r(o);if(a!=null){let p=e.height,h=e.width;return a.drawImage(e,0,0,h,p),d=a.getImageData(0,0,h,p).data,c.height=p,c.width=h,yo(d,c)}else throw new Error("Can not access image data")}else{if(l)return new Promise((o,a)=>{let p=s(),h=r(p);if(!e||!h)return a();let m=new Image;m.crossOrigin="Anonymous",m.src=e,m.onload=()=>{p.width=m.width,p.height=m.height,h.drawImage(m,0,0,p.width,p.height);let g=h.getImageData(0,0,p.width,p.height);c.height=p.height,c.width=p.width,o(yo(g.data,c))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(d!==void 0)return yo(d,c);throw new Error("Input data provided is not supported - aborted tensor creation")},b0=(e,t)=>{let{width:n,height:i,download:u,dispose:l}=t,d=[1,i,n,4];return new _t({location:"texture",type:"float32",texture:e,dims:d,download:u,dispose:l})},y0=(e,t)=>{let{dataType:n,dims:i,download:u,dispose:l}=t;return new _t({location:"gpu-buffer",type:n??"float32",gpuBuffer:e,dims:i,download:u,dispose:l})},w0=(e,t)=>{let{dataType:n,dims:i,download:u,dispose:l}=t;return new _t({location:"ml-tensor",type:n??"float32",mlTensor:e,dims:i,download:u,dispose:l})},_0=(e,t,n)=>new _t({location:"cpu-pinned",type:e,data:t,dims:n??[t.length]})}),an,wi,ua,v0,S3=D(()=>{"use strict";an=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),wi=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),ua=!1,v0=()=>{if(!ua){ua=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,n=globalThis.Float16Array,i=typeof n<"u"&&n.from;e&&(an.set("int64",BigInt64Array),wi.set(BigInt64Array,"int64")),t&&(an.set("uint64",BigUint64Array),wi.set(BigUint64Array,"uint64")),i?(an.set("float16",n),wi.set(n,"float16")):an.set("float16",Uint16Array)}}}),x0,$0,I3=D(()=>{"use strict";ql(),x0=e=>{let t=1;for(let n=0;n{switch(e.location){case"cpu":return new _t(e.type,e.data,t);case"cpu-pinned":return new _t({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new _t({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new _t({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new _t({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),_t,ql=D(()=>{"use strict";$3(),T3(),S3(),I3(),_t=class{constructor(e,t,n){v0();let i,u;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,i=e.type,u=e.dims,e.location){case"cpu-pinned":{let d=an.get(i);if(!d)throw new TypeError(`unsupported type "${i}" to create tensor from pinned buffer`);if(!(e.data instanceof d))throw new TypeError(`buffer should be of type ${d.name}`);this.cpuData=e.data;break}case"texture":{if(i!=="float32")throw new TypeError(`unsupported type "${i}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint64"&&i!=="int8"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let d,c;if(typeof e=="string")if(i=e,c=n,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");d=t}else{let s=an.get(e);if(s===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&s===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${s.name} as data.`);e==="uint64"||e==="int64"?d=s.from(t,BigInt):d=s.from(t)}else if(t instanceof s)d=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")d=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&s!==Uint16Array)d=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${i} tensor's data must be type of ${s}`)}else if(c=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let s=typeof e[0];if(s==="string")i="string",d=e;else if(s==="boolean")i="bool",d=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${s}.`)}else if(e instanceof Uint8ClampedArray)i="uint8",d=Uint8Array.from(e);else{let s=wi.get(e.constructor);if(s===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);i=s,d=e}if(c===void 0)c=[d.length];else if(!Array.isArray(c))throw new TypeError("A tensor's dims must be a number array");u=c,this.cpuData=d,this.dataLocation="cpu"}let l=x0(u);if(this.cpuData&&l!==this.cpuData.length&&!((i==="uint4"||i==="int4")&&Math.ceil(l/2)===this.cpuData.length))throw new Error(`Tensor's size(${l}) does not match data length(${this.cpuData.length}).`);this.type=i,this.dims=u,this.size=l}static async fromImage(e,t){return g0(e,t)}static fromTexture(e,t){return b0(e,t)}static fromGpuBuffer(e,t){return y0(e,t)}static fromMLTensor(e,t){return w0(e,t)}static fromPinnedBuffer(e,t,n){return _0(e,t,n)}toDataURL(e){return f0(this,e)}toImageData(e){return m0(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return $0(this,e)}}}),vt,T0=D(()=>{"use strict";ql(),vt=_t}),Lo,la,Jt,Vt,hn,fn,S0=D(()=>{"use strict";h0(),Lo=(e,t)=>{(typeof st.trace>"u"?!st.wasm.trace:!st.trace)||console.timeStamp(`${e}::ORT::${t}`)},la=(e,t)=>{let n=new Error().stack?.split(/\r\n|\r|\n/g)||[],i=!1;for(let u=0;u{(typeof st.trace>"u"?!st.wasm.trace:!st.trace)||la("BEGIN",e)},Vt=e=>{(typeof st.trace>"u"?!st.wasm.trace:!st.trace)||la("END",e)},hn=e=>{(typeof st.trace>"u"?!st.wasm.trace:!st.trace)||console.time(`ORT::${e}`)},fn=e=>{(typeof st.trace>"u"?!st.wasm.trace:!st.trace)||console.timeEnd(`ORT::${e}`)}}),I0,O3=D(()=>{"use strict";c0(),T0(),S0(),I0=class O0{constructor(t){this.handler=t}async run(t,n,i){Jt(),hn("InferenceSession.run");let u={},l={};if(typeof t!="object"||t===null||t instanceof vt||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof n=="object"){if(n===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(n instanceof vt)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(n)){if(n.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let r of n){if(typeof r!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(r)===-1)throw new RangeError(`'fetches' contains invalid output name: ${r}.`);u[r]=null}if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let r=!1,o=Object.getOwnPropertyNames(n);for(let a of this.outputNames)if(o.indexOf(a)!==-1){let p=n[a];(p===null||p instanceof vt)&&(r=!0,d=!1,u[a]=p)}if(r){if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else l=n}}else if(typeof n<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let r of this.inputNames)if(typeof t[r]>"u")throw new Error(`input '${r}' is missing in 'feeds'.`);if(d)for(let r of this.outputNames)u[r]=null;let c=await this.handler.run(t,u,l),s={};for(let r in c)if(Object.hasOwnProperty.call(c,r)){let o=c[r];o instanceof vt?s[r]=o:s[r]=new vt(o.type,o.data,o.dims)}return fn("InferenceSession.run"),Vt(),s}async release(){return this.handler.dispose()}static async create(t,n,i,u){Jt(),hn("InferenceSession.create");let l,d={};if(typeof t=="string"){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let o=t,a=0,p=t.byteLength;if(typeof n=="object"&&n!==null)d=n;else if(typeof n=="number"){if(a=n,!Number.isSafeInteger(a))throw new RangeError("'byteOffset' must be an integer.");if(a<0||a>=o.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${o.byteLength}).`);if(p=t.byteLength-a,typeof i=="number"){if(p=i,!Number.isSafeInteger(p))throw new RangeError("'byteLength' must be an integer.");if(p<=0||a+p>o.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${o.byteLength-a}].`);if(typeof u=="object"&&u!==null)d=u;else if(typeof u<"u")throw new TypeError("'options' must be an object.")}else if(typeof i<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof n<"u")throw new TypeError("'options' must be an object.");l=new Uint8Array(o,a,p)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[c,s]=await d0(d),r=await c.createInferenceSessionHandler(l,s);return fn("InferenceSession.create"),Vt(),new O0(r)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),Qo,E3=D(()=>{"use strict";O3(),Qo=I0}),A3=D(()=>{"use strict"}),k3=D(()=>{"use strict"}),P3=D(()=>{"use strict"}),C3=D(()=>{"use strict"}),R3={};wn(R3,{InferenceSession:()=>Qo,TRACE:()=>Lo,TRACE_EVENT_BEGIN:()=>hn,TRACE_EVENT_END:()=>fn,TRACE_FUNC_BEGIN:()=>Jt,TRACE_FUNC_END:()=>Vt,Tensor:()=>vt,env:()=>me,registerBackend:()=>pn});var dt=D(()=>{"use strict";_3(),x3(),E3(),T0(),A3(),k3(),S0(),P3(),C3()});function un(e,t,n,i){if(t===void 0)return D3(e);if(n===void 0)wo(e,t,1);else if(typeof n=="number"&&i===void 0)wo(e,t,n);else if(typeof n=="string"&&i===void 0)wo(e,n,1,t);else if(typeof n=="string"&&typeof i=="number")wo(e,n,i,t);else throw new TypeError("input is valid")}function D3(e){return{verbose:un.verbose.bind(null,e),info:un.info.bind(null,e),warning:un.warning.bind(null,e),error:un.error.bind(null,e),fatal:un.fatal.bind(null,e)}}function wo(e,t,n,i){let u=Dn[i||""]||Dn[""];Fu[e]{"use strict";sh=class{log(e,t,n){}},ah=class{log(e,t,n){console.log(`${this.color(e)} ${n?"\x1B[35m"+n+"\x1B[0m ":""}${t}`)}color(e){switch(e){case"verbose":return"\x1B[34;40mv\x1B[0m";case"info":return"\x1B[32mi\x1B[0m";case"warning":return"\x1B[30;43mw\x1B[0m";case"error":return"\x1B[31;40me\x1B[0m";case"fatal":return"\x1B[101mf\x1B[0m";default:throw new Error(`unsupported severity: ${e}`)}}},Fu={verbose:1e3,info:2e3,warning:4e3,error:5e3,fatal:6e3},E0={none:new sh,console:new ah},da={provider:"console",minimalSeverity:"warning",logDateTime:!0,logSourceLocation:!1},Dn={"":da},(e=>{function t(r,o){e("verbose",r,o)}e.verbose=t;function n(r,o){e("info",r,o)}e.info=n;function i(r,o){e("warning",r,o)}e.warning=i;function u(r,o){e("error",r,o)}e.error=u;function l(r,o){e("fatal",r,o)}e.fatal=l;function d(r){Dn={},c("",r||{})}e.reset=d;function c(r,o){if(r==="*")d(o);else{let a=Dn[r]||da;Dn[r]={provider:o.provider||a.provider,minimalSeverity:o.minimalSeverity||a.minimalSeverity,logDateTime:o.logDateTime===void 0?a.logDateTime:o.logDateTime,logSourceLocation:o.logSourceLocation===void 0?a.logSourceLocation:o.logSourceLocation}}}e.set=c;function s(r){let o={};r.logLevel&&(o.minimalSeverity=r.logLevel),c("",o)}e.setWithEnv=s})(un||={}),Je=un,ca=class{constructor(e,t,n,i,u,l){this.category=e,this.name=t,this.startTime=n,this.endCallback=i,this.timer=u,this.ctx=l}async end(){return this.endCallback(this)}async checkTimer(){if(this.ctx===void 0||this.timer===void 0)throw new Error("No webgl timer found");return this.ctx.endTimer(),this.ctx.waitForQueryAndGetTime(this.timer)}},pa=class{constructor(e,t,n,i){this.category=e,this.name=t,this.startTime=n,this.endTime=i}},A0=class{constructor(e,t,n){this._started=!1,this._flushPointer=0,this._started=!1,this._maxNumberEvents=e===void 0?1e4:e,this._flushBatchSize=t===void 0?10:t,this._flushIntervalInMilliseconds=n===void 0?5e3:n}static create(e){return e===void 0?new this:new this(e.maxNumberEvents,e.flushBatchSize,e.flushIntervalInMilliseconds)}start(){this._started=!0,this._timingEvents=[],this._flushTime=ni(),this._flushPointer=0}stop(){for(this._started=!1;this._flushPointer{d.then(async r=>{u&&await u.end(),c(r)},async r=>{u&&await u.end(),s(r)})});if(!l&&u){let c=u.end();if(c&&typeof c.then=="function")return new Promise((s,r)=>{c.then(()=>{s(d)},o=>{r(o)})})}return d}begin(e,t,n){if(!this._started)throw new Error("profiler is not started yet");if(n===void 0){let i=ni();return this.flush(i),new ca(e,t,i,u=>this.endSync(u))}else{let i=n.beginTimer();return new ca(e,t,0,async u=>this.end(u),i,n)}}async end(e){let t=await e.checkTimer();this._timingEvents.length=this._flushBatchSize||e-this._flushTime>=this._flushIntervalInMilliseconds){for(let t=this._flushPointer;this._flushPointerperformance.now():Date.now});function M3(e,t,n){for(let i of n){let u=i[0],l=i[1],d=i[2],c=i[3],s=i[4];if(e.opType===u){for(let r of t)if((r.domain===l||r.domain==="ai.onnx"&&l==="")&&N3(r.version,d))return{opImpl:c,opInit:s}}}throw new TypeError(`cannot resolve operator '${e.opType}' with opsets: ${t.map(i=>`${i.domain||"ai.onnx"} v${i.version}`).join(", ")}`)}function N3(e,t){if(t.endsWith("+")){let n=Number.parseInt(t.substring(0,t.length-1),10);return!isNaN(n)&&n<=e}else if(t.split("-").length===2){let n=t.split("-"),i=Number.parseInt(n[0],10),u=Number.parseInt(n[1],10);return!isNaN(i)&&!isNaN(u)&&i<=e&&e<=u}else return Number.parseInt(t,10)===e}var z3=D(()=>{"use strict"}),B3=ae(e=>{"use strict";e.__esModule=!0;var t=(function(){function n(i){if(!i)throw new TypeError("Invalid argument; `value` has no value.");this.value=n.EMPTY,i&&n.isGuid(i)&&(this.value=i)}return n.isGuid=function(i){var u=i.toString();return i&&(i instanceof n||n.validator.test(u))},n.create=function(){return new n([n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-"))},n.createEmpty=function(){return new n("emptyguid")},n.parse=function(i){return new n(i)},n.raw=function(){return[n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-")},n.gen=function(i){for(var u="",l=0;l>>=0,(u=0<=e&&e<256)&&(i=ju[e],i)?i:(n=Ce(e,0,!0),u&&(ju[e]=n),n)):(e|=0,(u=-128<=e&&e<128)&&(i=Lu[e],i)?i:(n=Ce(e,e<0?-1:0,!1),u&&(Lu[e]=n),n))}function Rt(e,t){if(isNaN(e))return t?Zt:Ct;if(t){if(e<0)return Zt;if(e>=Uu)return Gu}else{if(e<=-Vu)return pt;if(e+1>=Vu)return qu}return e<0?Rt(-e,t).neg():Ce(e%ln|0,e/ln|0,t)}function Ce(e,t,n){return new ze(e,t,n)}function Gl(e,t,n){if(e.length===0)throw Error("empty string");if(typeof t=="number"?(n=t,t=!1):t=!!t,e==="NaN"||e==="Infinity"||e==="+Infinity"||e==="-Infinity")return t?Zt:Ct;if(n=n||10,n<2||360)throw Error("interior hyphen");if(i===0)return Gl(e.substring(1),t,n).neg();for(var u=Rt(Oi(n,8)),l=Ct,d=0;d{gt=null;try{gt=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}ze.prototype.__isLong__,Object.defineProperty(ze.prototype,"__isLong__",{value:!0}),ze.isLong=lt,Lu={},ju={},ze.fromInt=Ur,ze.fromNumber=Rt,ze.fromBits=Ce,Oi=Math.pow,ze.fromString=Gl,ze.fromValue=Et,ha=65536,lh=1<<24,ln=ha*ha,Uu=ln*ln,Vu=Uu/2,fa=Ur(lh),Ct=Ur(0),ze.ZERO=Ct,Zt=Ur(0,!0),ze.UZERO=Zt,Vr=Ur(1),ze.ONE=Vr,ma=Ur(1,!0),ze.UONE=ma,_o=Ur(-1),ze.NEG_ONE=_o,qu=Ce(-1,2147483647,!1),ze.MAX_VALUE=qu,Gu=Ce(-1,-1,!0),ze.MAX_UNSIGNED_VALUE=Gu,pt=Ce(0,-2147483648,!1),ze.MIN_VALUE=pt,Z=ze.prototype,Z.toInt=function(){return this.unsigned?this.low>>>0:this.low},Z.toNumber=function(){return this.unsigned?(this.high>>>0)*ln+(this.low>>>0):this.high*ln+(this.low>>>0)},Z.toString=function(e){if(e=e||10,e<2||36>>0,r=s.toString(e);if(l=c,l.isZero())return r+d;for(;r.length<6;)r="0"+r;d=""+r+d}},Z.getHighBits=function(){return this.high},Z.getHighBitsUnsigned=function(){return this.high>>>0},Z.getLowBits=function(){return this.low},Z.getLowBitsUnsigned=function(){return this.low>>>0},Z.getNumBitsAbs=function(){if(this.isNegative())return this.eq(pt)?64:this.neg().getNumBitsAbs();for(var e=this.high!=0?this.high:this.low,t=31;t>0&&(e&1<>21;return e?this.unsigned?!1:e===-1&&!(this.low===0&&this.high===-2097152):!0},Z.isZero=function(){return this.high===0&&this.low===0},Z.eqz=Z.isZero,Z.isNegative=function(){return!this.unsigned&&this.high<0},Z.isPositive=function(){return this.unsigned||this.high>=0},Z.isOdd=function(){return(this.low&1)===1},Z.isEven=function(){return(this.low&1)===0},Z.equals=function(e){return lt(e)||(e=Et(e)),this.unsigned!==e.unsigned&&this.high>>>31===1&&e.high>>>31===1?!1:this.high===e.high&&this.low===e.low},Z.eq=Z.equals,Z.notEquals=function(e){return!this.eq(e)},Z.neq=Z.notEquals,Z.ne=Z.notEquals,Z.lessThan=function(e){return this.comp(e)<0},Z.lt=Z.lessThan,Z.lessThanOrEqual=function(e){return this.comp(e)<=0},Z.lte=Z.lessThanOrEqual,Z.le=Z.lessThanOrEqual,Z.greaterThan=function(e){return this.comp(e)>0},Z.gt=Z.greaterThan,Z.greaterThanOrEqual=function(e){return this.comp(e)>=0},Z.gte=Z.greaterThanOrEqual,Z.ge=Z.greaterThanOrEqual,Z.compare=function(e){if(lt(e)||(e=Et(e)),this.eq(e))return 0;var t=this.isNegative(),n=e.isNegative();return t&&!n?-1:!t&&n?1:this.unsigned?e.high>>>0>this.high>>>0||e.high===this.high&&e.low>>>0>this.low>>>0?-1:1:this.sub(e).isNegative()?-1:1},Z.comp=Z.compare,Z.negate=function(){return!this.unsigned&&this.eq(pt)?pt:this.not().add(Vr)},Z.neg=Z.negate,Z.add=function(e){lt(e)||(e=Et(e));var t=this.high>>>16,n=this.high&65535,i=this.low>>>16,u=this.low&65535,l=e.high>>>16,d=e.high&65535,c=e.low>>>16,s=e.low&65535,r=0,o=0,a=0,p=0;return p+=u+s,a+=p>>>16,p&=65535,a+=i+c,o+=a>>>16,a&=65535,o+=n+d,r+=o>>>16,o&=65535,r+=t+l,r&=65535,Ce(a<<16|p,r<<16|o,this.unsigned)},Z.subtract=function(e){return lt(e)||(e=Et(e)),this.add(e.neg())},Z.sub=Z.subtract,Z.multiply=function(e){if(this.isZero())return this;if(lt(e)||(e=Et(e)),gt){var t=gt.mul(this.low,this.high,e.low,e.high);return Ce(t,gt.get_high(),this.unsigned)}if(e.isZero())return this.unsigned?Zt:Ct;if(this.eq(pt))return e.isOdd()?pt:Ct;if(e.eq(pt))return this.isOdd()?pt:Ct;if(this.isNegative())return e.isNegative()?this.neg().mul(e.neg()):this.neg().mul(e).neg();if(e.isNegative())return this.mul(e.neg()).neg();if(this.lt(fa)&&e.lt(fa))return Rt(this.toNumber()*e.toNumber(),this.unsigned);var n=this.high>>>16,i=this.high&65535,u=this.low>>>16,l=this.low&65535,d=e.high>>>16,c=e.high&65535,s=e.low>>>16,r=e.low&65535,o=0,a=0,p=0,h=0;return h+=l*r,p+=h>>>16,h&=65535,p+=u*r,a+=p>>>16,p&=65535,p+=l*s,a+=p>>>16,p&=65535,a+=i*r,o+=a>>>16,a&=65535,a+=u*s,o+=a>>>16,a&=65535,a+=l*c,o+=a>>>16,a&=65535,o+=n*r+i*s+u*c+l*d,o&=65535,Ce(p<<16|h,o<<16|a,this.unsigned)},Z.mul=Z.multiply,Z.divide=function(e){if(lt(e)||(e=Et(e)),e.isZero())throw Error("division by zero");if(gt){if(!this.unsigned&&this.high===-2147483648&&e.low===-1&&e.high===-1)return this;var t=(this.unsigned?gt.div_u:gt.div_s)(this.low,this.high,e.low,e.high);return Ce(t,gt.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?Zt:Ct;var n,i,u;if(this.unsigned){if(e.unsigned||(e=e.toUnsigned()),e.gt(this))return Zt;if(e.gt(this.shru(1)))return ma;u=Zt}else{if(this.eq(pt)){if(e.eq(Vr)||e.eq(_o))return pt;if(e.eq(pt))return Vr;var l=this.shr(1);return n=l.div(e).shl(1),n.eq(Ct)?e.isNegative()?Vr:_o:(i=this.sub(e.mul(n)),u=n.add(i.div(e)),u)}else if(e.eq(pt))return this.unsigned?Zt:Ct;if(this.isNegative())return e.isNegative()?this.neg().div(e.neg()):this.neg().div(e).neg();if(e.isNegative())return this.div(e.neg()).neg();u=Ct}for(i=this;i.gte(e);){n=Math.max(1,Math.floor(i.toNumber()/e.toNumber()));for(var d=Math.ceil(Math.log(n)/Math.LN2),c=d<=48?1:Oi(2,d-48),s=Rt(n),r=s.mul(e);r.isNegative()||r.gt(i);)n-=c,s=Rt(n,this.unsigned),r=s.mul(e);s.isZero()&&(s=Vr),u=u.add(s),i=i.sub(r)}return u},Z.div=Z.divide,Z.modulo=function(e){if(lt(e)||(e=Et(e)),gt){var t=(this.unsigned?gt.rem_u:gt.rem_s)(this.low,this.high,e.low,e.high);return Ce(t,gt.get_high(),this.unsigned)}return this.sub(this.div(e).mul(e))},Z.mod=Z.modulo,Z.rem=Z.modulo,Z.not=function(){return Ce(~this.low,~this.high,this.unsigned)},Z.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},Z.clz=Z.countLeadingZeros,Z.countTrailingZeros=function(){return this.low?uh(this.low):uh(this.high)+32},Z.ctz=Z.countTrailingZeros,Z.and=function(e){return lt(e)||(e=Et(e)),Ce(this.low&e.low,this.high&e.high,this.unsigned)},Z.or=function(e){return lt(e)||(e=Et(e)),Ce(this.low|e.low,this.high|e.high,this.unsigned)},Z.xor=function(e){return lt(e)||(e=Et(e)),Ce(this.low^e.low,this.high^e.high,this.unsigned)},Z.shiftLeft=function(e){return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ce(this.low<>>32-e,this.unsigned):Ce(0,this.low<>>e|this.high<<32-e,this.high>>e,this.unsigned):Ce(this.high>>e-32,this.high>=0?0:-1,this.unsigned)},Z.shr=Z.shiftRight,Z.shiftRightUnsigned=function(e){return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ce(this.low>>>e|this.high<<32-e,this.high>>>e,this.unsigned):e===32?Ce(this.high,0,this.unsigned):Ce(this.high>>>e-32,0,this.unsigned)},Z.shru=Z.shiftRightUnsigned,Z.shr_u=Z.shiftRightUnsigned,Z.rotateLeft=function(e){var t;return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ce(this.high,this.low,this.unsigned):e<32?(t=32-e,Ce(this.low<>>t,this.high<>>t,this.unsigned)):(e-=32,t=32-e,Ce(this.high<>>t,this.low<>>t,this.unsigned))},Z.rotl=Z.rotateLeft,Z.rotateRight=function(e){var t;return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ce(this.high,this.low,this.unsigned):e<32?(t=32-e,Ce(this.high<>>e,this.low<>>e,this.unsigned)):(e-=32,t=32-e,Ce(this.low<>>e,this.high<>>e,this.unsigned))},Z.rotr=Z.rotateRight,Z.toSigned=function(){return this.unsigned?Ce(this.low,this.high,!1):this},Z.toUnsigned=function(){return this.unsigned?this:Ce(this.low,this.high,!0)},Z.toBytes=function(e){return e?this.toBytesLE():this.toBytesBE()},Z.toBytesLE=function(){var e=this.high,t=this.low;return[t&255,t>>>8&255,t>>>16&255,t>>>24,e&255,e>>>8&255,e>>>16&255,e>>>24]},Z.toBytesBE=function(){var e=this.high,t=this.low;return[e>>>24,e>>>16&255,e>>>8&255,e&255,t>>>24,t>>>16&255,t>>>8&255,t&255]},ze.fromBytes=function(e,t,n){return n?ze.fromBytesLE(e,t):ze.fromBytesBE(e,t)},ze.fromBytesLE=function(e,t){return new ze(e[0]|e[1]<<8|e[2]<<16|e[3]<<24,e[4]|e[5]<<8|e[6]<<16|e[7]<<24,t)},ze.fromBytesBE=function(e,t){return new ze(e[4]<<24|e[5]<<16|e[6]<<8|e[7],e[0]<<24|e[1]<<16|e[2]<<8|e[3],t)},typeof BigInt=="function"&&(ze.fromBigInt=function(e,t){var n=Number(BigInt.asIntN(32,e)),i=Number(BigInt.asIntN(32,e>>BigInt(32)));return Ce(n,i,t)},ze.fromValue=function(e,t){return typeof e=="bigint"?ze.fromBigInt(e,t):Et(e,t)},Z.toBigInt=function(){var e=BigInt(this.low>>>0),t=BigInt(this.unsigned?this.high>>>0:this.high);return t<{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ArgType=void 0;var t;(function(n){n[n.INPUT=0]="INPUT",n[n.OUTPUT=1]="OUTPUT"})(t||(e.ArgType=t={}))}),Hl=ae(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.SIZE_PREFIX_LENGTH=e.FILE_IDENTIFIER_LENGTH=e.SIZEOF_INT=e.SIZEOF_SHORT=void 0,e.SIZEOF_SHORT=2,e.SIZEOF_INT=4,e.FILE_IDENTIFIER_LENGTH=4,e.SIZE_PREFIX_LENGTH=4}),C0=ae(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.isLittleEndian=e.float64=e.float32=e.int32=void 0,e.int32=new Int32Array(2),e.float32=new Float32Array(e.int32.buffer),e.float64=new Float64Array(e.int32.buffer),e.isLittleEndian=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1}),R0=ae(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=void 0;var t;(function(n){n[n.UTF8_BYTES=1]="UTF8_BYTES",n[n.UTF16_STRING=2]="UTF16_STRING"})(t||(e.Encoding=t={}))}),D0=ae(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ByteBuffer=void 0;var t=Hl(),n=R0(),i=C0(),u=class M0{constructor(d){this.bytes_=d,this.position_=0,this.text_decoder_=new TextDecoder}static allocate(d){return new M0(new Uint8Array(d))}clear(){this.position_=0}bytes(){return this.bytes_}position(){return this.position_}setPosition(d){this.position_=d}capacity(){return this.bytes_.length}readInt8(d){return this.readUint8(d)<<24>>24}readUint8(d){return this.bytes_[d]}readInt16(d){return this.readUint16(d)<<16>>16}readUint16(d){return this.bytes_[d]|this.bytes_[d+1]<<8}readInt32(d){return this.bytes_[d]|this.bytes_[d+1]<<8|this.bytes_[d+2]<<16|this.bytes_[d+3]<<24}readUint32(d){return this.readInt32(d)>>>0}readInt64(d){return BigInt.asIntN(64,BigInt(this.readUint32(d))+(BigInt(this.readUint32(d+4))<>8}writeUint16(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8}writeInt32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeUint32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeInt64(d,c){this.writeInt32(d,Number(BigInt.asIntN(32,c))),this.writeInt32(d+4,Number(BigInt.asIntN(32,c>>BigInt(32))))}writeUint64(d,c){this.writeUint32(d,Number(BigInt.asUintN(32,c))),this.writeUint32(d+4,Number(BigInt.asUintN(32,c>>BigInt(32))))}writeFloat32(d,c){i.float32[0]=c,this.writeInt32(d,i.int32[0])}writeFloat64(d,c){i.float64[0]=c,this.writeInt32(d,i.int32[i.isLittleEndian?0:1]),this.writeInt32(d+4,i.int32[i.isLittleEndian?1:0])}getBufferIdentifier(){if(this.bytes_.length{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Builder=void 0;var t=D0(),n=Hl(),i=class N0{constructor(l){this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null,this.text_encoder=new TextEncoder;let d;l?d=l:d=1024,this.bb=t.ByteBuffer.allocate(d),this.space=d}clear(){this.bb.clear(),this.space=this.bb.capacity(),this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null}forceDefaults(l){this.force_defaults=l}dataBuffer(){return this.bb}asUint8Array(){return this.bb.bytes().subarray(this.bb.position(),this.bb.position()+this.offset())}prep(l,d){l>this.minalign&&(this.minalign=l);let c=~(this.bb.capacity()-this.space+d)+1&l-1;for(;this.space=0&&this.vtable[d]==0;d--);let c=d+1;for(;d>=0;d--)this.addInt16(this.vtable[d]!=0?l-this.vtable[d]:0);let s=2;this.addInt16(l-this.object_start);let r=(c+s)*n.SIZEOF_SHORT;this.addInt16(r);let o=0,a=this.space;e:for(d=0;d=0;o--)this.writeInt8(r.charCodeAt(o))}this.prep(this.minalign,n.SIZEOF_INT+s),this.addOffset(l),s&&this.addInt32(this.bb.capacity()-this.space),this.bb.setPosition(this.space)}finishSizePrefixed(l,d){this.finish(l,d,!0)}requiredField(l,d){let c=this.bb.capacity()-l,s=c-this.bb.readInt32(c);if(!(d{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=e.ByteBuffer=e.Builder=e.isLittleEndian=e.int32=e.float64=e.float32=e.SIZE_PREFIX_LENGTH=e.SIZEOF_SHORT=e.SIZEOF_INT=e.FILE_IDENTIFIER_LENGTH=void 0;var t=Hl();Object.defineProperty(e,"FILE_IDENTIFIER_LENGTH",{enumerable:!0,get:function(){return t.FILE_IDENTIFIER_LENGTH}}),Object.defineProperty(e,"SIZEOF_INT",{enumerable:!0,get:function(){return t.SIZEOF_INT}}),Object.defineProperty(e,"SIZEOF_SHORT",{enumerable:!0,get:function(){return t.SIZEOF_SHORT}}),Object.defineProperty(e,"SIZE_PREFIX_LENGTH",{enumerable:!0,get:function(){return t.SIZE_PREFIX_LENGTH}});var n=C0();Object.defineProperty(e,"float32",{enumerable:!0,get:function(){return n.float32}}),Object.defineProperty(e,"float64",{enumerable:!0,get:function(){return n.float64}}),Object.defineProperty(e,"int32",{enumerable:!0,get:function(){return n.int32}}),Object.defineProperty(e,"isLittleEndian",{enumerable:!0,get:function(){return n.isLittleEndian}});var i=F3();Object.defineProperty(e,"Builder",{enumerable:!0,get:function(){return i.Builder}});var u=D0();Object.defineProperty(e,"ByteBuffer",{enumerable:!0,get:function(){return u.ByteBuffer}});var l=R0();Object.defineProperty(e,"Encoding",{enumerable:!0,get:function(){return l.Encoding}})}),z0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.AttributeType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.INT=2]="INT",n[n.STRING=3]="STRING",n[n.TENSOR=4]="TENSOR",n[n.GRAPH=5]="GRAPH",n[n.FLOATS=6]="FLOATS",n[n.INTS=7]="INTS",n[n.STRINGS=8]="STRINGS",n[n.TENSORS=9]="TENSORS",n[n.GRAPHS=10]="GRAPHS",n[n.SPARSE_TENSOR=11]="SPARSE_TENSOR",n[n.SPARSE_TENSORS=12]="SPARSE_TENSORS"})(t||(e.AttributeType=t={}))}),F0=ae(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=void 0;var t;(function(n){n[n.Primitive=0]="Primitive",n[n.Fused=1]="Fused"})(t||(e.NodeType=t={}))}),L0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(o[a]);return r.endVector()}static startInputsVector(r,o){r.startVector(4,o,4)}static addOutputs(r,o){r.addFieldOffset(9,o,0)}static createOutputsVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addOffset(o[a]);return r.endVector()}static startOutputsVector(r,o){r.startVector(4,o,4)}static addAttributes(r,o){r.addFieldOffset(10,o,0)}static createAttributesVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addOffset(o[a]);return r.endVector()}static startAttributesVector(r,o){r.startVector(4,o,4)}static addInputArgCounts(r,o){r.addFieldOffset(11,o,0)}static createInputArgCountsVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addInt32(o[a]);return r.endVector()}static startInputArgCountsVector(r,o){r.startVector(4,o,4)}static addImplicitInputs(r,o){r.addFieldOffset(12,o,0)}static createImplicitInputsVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addOffset(o[a]);return r.endVector()}static startImplicitInputsVector(r,o){r.startVector(4,o,4)}static endNode(r){return r.endObject()}static createNode(r,o,a,p,h,m,g,_,w,y,T,S,O,A){return ot.startNode(r),ot.addName(r,o),ot.addDocString(r,a),ot.addDomain(r,p),ot.addSinceVersion(r,h),ot.addIndex(r,m),ot.addOpType(r,g),ot.addType(r,_),ot.addExecutionProviderType(r,w),ot.addInputs(r,y),ot.addOutputs(r,T),ot.addAttributes(r,S),ot.addInputArgCounts(r,O),ot.addImplicitInputs(r,A),ot.endNode(r)}};e.Node=c}),j0=ae(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.EdgeEnd=void 0;var t=class{constructor(){this.bb=null,this.bb_pos=0}__init(n,i){return this.bb_pos=n,this.bb=i,this}nodeIndex(){return this.bb.readUint32(this.bb_pos)}srcArgIndex(){return this.bb.readInt32(this.bb_pos+4)}dstArgIndex(){return this.bb.readInt32(this.bb_pos+8)}static sizeOf(){return 12}static createEdgeEnd(n,i,u,l){return n.prep(4,12),n.writeInt32(l),n.writeInt32(u),n.writeInt32(i),n.offset()}};e.EdgeEnd=t}),U0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addNumInputs(c,s){c.addFieldInt32(1,s,0)}static addNumOutputs(c,s){c.addFieldInt32(2,s,0)}static addHasVariadicInput(c,s){c.addFieldInt8(3,+s,0)}static addHasVariadicOutput(c,s){c.addFieldInt8(4,+s,0)}static addNumVariadicInputs(c,s){c.addFieldInt32(5,s,0)}static addNumVariadicOutputs(c,s){c.addFieldInt32(6,s,0)}static endNodesToOptimizeIndices(c){return c.endObject()}static createNodesToOptimizeIndices(c,s,r,o,a,p,h,m){return kt.startNodesToOptimizeIndices(c),kt.addNodeIndices(c,s),kt.addNumInputs(c,r),kt.addNumOutputs(c,o),kt.addHasVariadicInput(c,a),kt.addHasVariadicOutput(c,p),kt.addNumVariadicInputs(c,h),kt.addNumVariadicOutputs(c,m),kt.endNodesToOptimizeIndices(c)}};e.NodesToOptimizeIndices=l}),q0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startProducedOpIdsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecord(s){return s.endObject()}};e.RuntimeOptimizationRecord=d}),G0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startRuntimeOptimizationRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecordContainerEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createRuntimeOptimizationRecordContainerEntry(s,r,o){return Zr.startRuntimeOptimizationRecordContainerEntry(s),Zr.addOptimizerName(s,r),Zr.addRuntimeOptimizationRecords(s,o),Zr.endRuntimeOptimizationRecordContainerEntry(s)}};e.RuntimeOptimizationRecordContainerEntry=d}),H0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizations(s){return s.endObject()}static createRuntimeOptimizations(s,r){return kn.startRuntimeOptimizations(s),kn.addRecords(s,r),kn.endRuntimeOptimizations(s)}};e.RuntimeOptimizations=d}),es=ae(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TensorDataType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.UINT8=2]="UINT8",n[n.INT8=3]="INT8",n[n.UINT16=4]="UINT16",n[n.INT16=5]="INT16",n[n.INT32=6]="INT32",n[n.INT64=7]="INT64",n[n.STRING=8]="STRING",n[n.BOOL=9]="BOOL",n[n.FLOAT16=10]="FLOAT16",n[n.DOUBLE=11]="DOUBLE",n[n.UINT32=12]="UINT32",n[n.UINT64=13]="UINT64",n[n.COMPLEX64=14]="COMPLEX64",n[n.COMPLEX128=15]="COMPLEX128",n[n.BFLOAT16=16]="BFLOAT16",n[n.FLOAT8E4M3FN=17]="FLOAT8E4M3FN",n[n.FLOAT8E4M3FNUZ=18]="FLOAT8E4M3FNUZ",n[n.FLOAT8E5M2=19]="FLOAT8E5M2",n[n.FLOAT8E5M2FNUZ=20]="FLOAT8E5M2FNUZ"})(t||(e.TensorDataType=t={}))}),ts=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addInt64(r[o]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static addDataType(s,r){s.addFieldInt32(3,r,l.TensorDataType.UNDEFINED)}static addRawData(s,r){s.addFieldOffset(4,r,0)}static createRawDataVector(s,r){s.startVector(1,r.length,1);for(let o=r.length-1;o>=0;o--)s.addInt8(r[o]);return s.endVector()}static startRawDataVector(s,r){s.startVector(1,r,1)}static addStringData(s,r){s.addFieldOffset(5,r,0)}static createStringDataVector(s,r){s.startVector(4,r.length,4);for(let o=r.length-1;o>=0;o--)s.addOffset(r[o]);return s.endVector()}static startStringDataVector(s,r){s.startVector(4,r,4)}static addExternalDataOffset(s,r){s.addFieldInt64(6,r,BigInt("-1"))}static endTensor(s){return s.endObject()}static createTensor(s,r,o,a,p,h,m,g){return Pt.startTensor(s),Pt.addName(s,r),Pt.addDocString(s,o),Pt.addDims(s,a),Pt.addDataType(s,p),Pt.addRawData(s,h),Pt.addStringData(s,m),Pt.addExternalDataOffset(s,g),Pt.endTensor(s)}};e.Tensor=d}),W0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addInt64(r[o]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static endSparseTensor(s){return s.endObject()}};e.SparseTensor=d}),K0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.DimensionValueType=void 0;var t;(function(n){n[n.UNKNOWN=0]="UNKNOWN",n[n.VALUE=1]="VALUE",n[n.PARAM=2]="PARAM"})(t||(e.DimensionValueType=t={}))}),Y0=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startDimVector(s,r){s.startVector(4,r,4)}static endShape(s){return s.endObject()}static createShape(s,r){return Cn.startShape(s),Cn.addDim(s,r),Cn.endShape(s)}};e.Shape=d}),ew=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TypeInfoValue=void 0,e.unionToTypeInfoValue=l,e.unionListToTypeInfoValue=d;var t=K0(),n=X0(),i=ew(),u;(function(c){c[c.NONE=0]="NONE",c[c.tensor_type=1]="tensor_type",c[c.sequence_type=2]="sequence_type",c[c.map_type=3]="map_type"})(u||(e.TypeInfoValue=u={}));function l(c,s){switch(u[c]){case"NONE":return null;case"tensor_type":return s(new i.TensorTypeAndShape);case"sequence_type":return s(new n.SequenceType);case"map_type":return s(new t.MapType);default:return null}}function d(c,s,r){switch(u[c]){case"NONE":return null;case"tensor_type":return s(r,new i.TensorTypeAndShape);case"sequence_type":return s(r,new n.SequenceType);case"map_type":return s(r,new t.MapType);default:return null}}}),rs=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(p,h,m,g){g===void 0&&(g=m);var _=Object.getOwnPropertyDescriptor(h,m);(!_||("get"in _?!h.__esModule:_.writable||_.configurable))&&(_={enumerable:!0,get:function(){return h[m]}}),Object.defineProperty(p,g,_)}:function(p,h,m,g){g===void 0&&(g=m),p[g]=h[m]}),n=e&&e.__setModuleDefault||(Object.create?function(p,h){Object.defineProperty(p,"default",{enumerable:!0,value:h})}:function(p,h){p.default=h}),i=e&&e.__importStar||(function(){var p=function(h){return p=Object.getOwnPropertyNames||function(m){var g=[];for(var _ in m)Object.prototype.hasOwnProperty.call(m,_)&&(g[g.length]=_);return g},p(h)};return function(h){if(h&&h.__esModule)return h;var m={};if(h!=null)for(var g=p(h),_=0;_=0;g--)h.addOffset(m[g]);return h.endVector()}static startInitializersVector(h,m){h.startVector(4,m,4)}static addNodeArgs(h,m){h.addFieldOffset(1,m,0)}static createNodeArgsVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startNodeArgsVector(h,m){h.startVector(4,m,4)}static addNodes(h,m){h.addFieldOffset(2,m,0)}static createNodesVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startNodesVector(h,m){h.startVector(4,m,4)}static addMaxNodeIndex(h,m){h.addFieldInt32(3,m,0)}static addNodeEdges(h,m){h.addFieldOffset(4,m,0)}static createNodeEdgesVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startNodeEdgesVector(h,m){h.startVector(4,m,4)}static addInputs(h,m){h.addFieldOffset(5,m,0)}static createInputsVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startInputsVector(h,m){h.startVector(4,m,4)}static addOutputs(h,m){h.addFieldOffset(6,m,0)}static createOutputsVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startOutputsVector(h,m){h.startVector(4,m,4)}static addSparseInitializers(h,m){h.addFieldOffset(7,m,0)}static createSparseInitializersVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startSparseInitializersVector(h,m){h.startVector(4,m,4)}static addRuntimeOptimizations(h,m){h.addFieldOffset(8,m,0)}static endGraph(h){return h.endObject()}};e.Graph=a}),nw=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,o,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(o,a);(!h||("get"in h?!o.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return o[a]}}),Object.defineProperty(r,p,h)}:function(r,o,a,p){p===void 0&&(p=a),r[p]=o[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,o){Object.defineProperty(r,"default",{enumerable:!0,value:o})}:function(r,o){r.default=o}),i=e&&e.__importStar||(function(){var r=function(o){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(o)};return function(o){if(o&&o.__esModule)return o;var a={};if(o!=null)for(var p=r(o),h=0;h=0;p--)o.addFloat32(a[p]);return o.endVector()}static startFloatsVector(o,a){o.startVector(4,a,4)}static addInts(o,a){o.addFieldOffset(9,a,0)}static createIntsVector(o,a){o.startVector(8,a.length,8);for(let p=a.length-1;p>=0;p--)o.addInt64(a[p]);return o.endVector()}static startIntsVector(o,a){o.startVector(8,a,8)}static addStrings(o,a){o.addFieldOffset(10,a,0)}static createStringsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startStringsVector(o,a){o.startVector(4,a,4)}static addTensors(o,a){o.addFieldOffset(11,a,0)}static createTensorsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startTensorsVector(o,a){o.startVector(4,a,4)}static addGraphs(o,a){o.addFieldOffset(12,a,0)}static createGraphsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startGraphsVector(o,a){o.startVector(4,a,4)}static endAttribute(o){return o.endObject()}};e.Attribute=s}),iw=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addKernelDefHashes(c,s){c.addFieldOffset(1,s,0)}static createKernelDefHashesVector(c,s){c.startVector(8,s.length,8);for(let r=s.length-1;r>=0;r--)c.addInt64(s[r]);return c.endVector()}static startKernelDefHashesVector(c,s){c.startVector(8,s,8)}static endDeprecatedKernelCreateInfos(c){return c.endObject()}static createDeprecatedKernelCreateInfos(c,s,r){return Jr.startDeprecatedKernelCreateInfos(c),Jr.addNodeIndices(c,s),Jr.addKernelDefHashes(c,r),Jr.endDeprecatedKernelCreateInfos(c)}};e.DeprecatedKernelCreateInfos=l}),L3=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(o[a]);return r.endVector()}static startSubGraphSessionStatesVector(r,o){r.startVector(4,o,4)}static endDeprecatedSessionState(r){return r.endObject()}static createDeprecatedSessionState(r,o,a){return en.startDeprecatedSessionState(r),en.addKernels(r,o),en.addSubGraphSessionStates(r,a),en.endDeprecatedSessionState(r)}};e.DeprecatedSessionState=c}),aw=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createKernelTypeStrArgsEntry(s,r,o){return tn.startKernelTypeStrArgsEntry(s),tn.addKernelTypeStr(s,r),tn.addArgs(s,o),tn.endKernelTypeStrArgsEntry(s)}};e.KernelTypeStrArgsEntry=d}),uw=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endOpIdKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createOpIdKernelTypeStrArgsEntry(s,r,o){return rn.startOpIdKernelTypeStrArgsEntry(s),rn.addOpId(s,r),rn.addKernelTypeStrArgs(s,o),rn.endOpIdKernelTypeStrArgsEntry(s)}};e.OpIdKernelTypeStrArgsEntry=d}),lw=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startOpKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrResolver(s){return s.endObject()}static createKernelTypeStrResolver(s,r){return Rn.startKernelTypeStrResolver(s),Rn.addOpKernelTypeStrArgs(s,r),Rn.endKernelTypeStrResolver(s)}};e.KernelTypeStrResolver=d}),dw=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,o,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(o,a);(!h||("get"in h?!o.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return o[a]}}),Object.defineProperty(r,p,h)}:function(r,o,a,p){p===void 0&&(p=a),r[p]=o[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,o){Object.defineProperty(r,"default",{enumerable:!0,value:o})}:function(r,o){r.default=o}),i=e&&e.__importStar||(function(){var r=function(o){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(o)};return function(o){if(o&&o.__esModule)return o;var a={};if(o!=null)for(var p=r(o),h=0;h=0;p--)o.addOffset(a[p]);return o.endVector()}static startOpsetImportVector(o,a){o.startVector(4,a,4)}static addProducerName(o,a){o.addFieldOffset(2,a,0)}static addProducerVersion(o,a){o.addFieldOffset(3,a,0)}static addDomain(o,a){o.addFieldOffset(4,a,0)}static addModelVersion(o,a){o.addFieldInt64(5,a,BigInt("0"))}static addDocString(o,a){o.addFieldOffset(6,a,0)}static addGraph(o,a){o.addFieldOffset(7,a,0)}static addGraphDocString(o,a){o.addFieldOffset(8,a,0)}static addMetadataProps(o,a){o.addFieldOffset(9,a,0)}static createMetadataPropsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startMetadataPropsVector(o,a){o.startVector(4,a,4)}static endModel(o){return o.endObject()}};e.Model=s}),j3=ae(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p{"use strict";U3=be(P0()),V3=be(z0()),rl=be(nw()),jt=be(B0()),q3=be(iw()),G3=be(L3()),H3=be(sw()),W3=be(ow()),K3=be(J0()),X3=be(Y0()),Z3=be(Z0()),Y3=be(j0()),hw=be(Wl()),fw=be(j3()),J3=be(aw()),Q3=be(lw()),eO=be(K0()),tO=be(pw()),mw=be(L0()),rO=be(U0()),nO=be(F0()),iO=be(V0()),oO=be(uw()),sO=be(dw()),aO=be(q0()),uO=be(G0()),lO=be(H0()),dO=be(X0()),cO=be(Q0()),pO=be(W0()),hO=be(cw()),fO=be(ts()),No=be(es()),gw=be(ew()),mO=be(rs()),bw=be(tw()),gO=be(rw())}),ns=D(()=>{"use strict";bO()}),yO=ae((e,t)=>{"use strict";t.exports=n;function n(i,u){for(var l=new Array(arguments.length-1),d=0,c=2,s=!0;c{"use strict";var t=e;t.length=function(d){var c=d.length;if(!c)return 0;for(var s=0;--c%4>1&&d.charAt(c)==="=";)++s;return Math.ceil(d.length*3)/4-s};var n=new Array(64),i=new Array(123);for(u=0;u<64;)i[n[u]=u<26?u+65:u<52?u+71:u<62?u-4:u-59|43]=u++;var u;t.encode=function(d,c,s){for(var r=null,o=[],a=0,p=0,h;c>2],h=(m&3)<<4,p=1;break;case 1:o[a++]=n[h|m>>4],h=(m&15)<<2,p=2;break;case 2:o[a++]=n[h|m>>6],o[a++]=n[m&63],p=0;break}a>8191&&((r||(r=[])).push(String.fromCharCode.apply(String,o)),a=0)}return p&&(o[a++]=n[h],o[a++]=61,p===1&&(o[a++]=61)),r?(a&&r.push(String.fromCharCode.apply(String,o.slice(0,a))),r.join("")):String.fromCharCode.apply(String,o.slice(0,a))};var l="invalid encoding";t.decode=function(d,c,s){for(var r=s,o=0,a,p=0;p1)break;if((h=i[h])===void 0)throw Error(l);switch(o){case 0:a=h,o=1;break;case 1:c[s++]=a<<2|(h&48)>>4,a=h,o=2;break;case 2:c[s++]=(a&15)<<4|(h&60)>>2,a=h,o=3;break;case 3:c[s++]=(a&3)<<6|h,o=0;break}}if(o===1)throw Error(l);return s-r},t.test=function(d){return/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(d)}}),_O=ae((e,t)=>{"use strict";t.exports=n;function n(){this._listeners=Object.create(null)}n.prototype.on=function(i,u,l){return(this._listeners[i]||(this._listeners[i]=[])).push({fn:u,ctx:l||this}),this},n.prototype.off=function(i,u){if(i===void 0)this._listeners=Object.create(null);else if(u===void 0)this._listeners[i]=[];else{var l=this._listeners[i];if(!l)return this;for(var d=0;d{"use strict";t.exports=n(n);function n(c){return typeof Float32Array<"u"?(function(){var s=new Float32Array([-0]),r=new Uint8Array(s.buffer),o=r[3]===128;function a(g,_,w){s[0]=g,_[w]=r[0],_[w+1]=r[1],_[w+2]=r[2],_[w+3]=r[3]}function p(g,_,w){s[0]=g,_[w]=r[3],_[w+1]=r[2],_[w+2]=r[1],_[w+3]=r[0]}c.writeFloatLE=o?a:p,c.writeFloatBE=o?p:a;function h(g,_){return r[0]=g[_],r[1]=g[_+1],r[2]=g[_+2],r[3]=g[_+3],s[0]}function m(g,_){return r[3]=g[_],r[2]=g[_+1],r[1]=g[_+2],r[0]=g[_+3],s[0]}c.readFloatLE=o?h:m,c.readFloatBE=o?m:h})():(function(){function s(o,a,p,h){var m=a<0?1:0;if(m&&(a=-a),a===0)o(1/a>0?0:2147483648,p,h);else if(isNaN(a))o(2143289344,p,h);else if(a>34028234663852886e22)o((m<<31|2139095040)>>>0,p,h);else if(a<11754943508222875e-54)o((m<<31|Math.round(a/1401298464324817e-60))>>>0,p,h);else{var g=Math.floor(Math.log(a)/Math.LN2),_=Math.round(a*Math.pow(2,-g)*8388608)&8388607;o((m<<31|g+127<<23|_)>>>0,p,h)}}c.writeFloatLE=s.bind(null,i),c.writeFloatBE=s.bind(null,u);function r(o,a,p){var h=o(a,p),m=(h>>31)*2+1,g=h>>>23&255,_=h&8388607;return g===255?_?NaN:m*(1/0):g===0?m*1401298464324817e-60*_:m*Math.pow(2,g-150)*(_+8388608)}c.readFloatLE=r.bind(null,l),c.readFloatBE=r.bind(null,d)})(),typeof Float64Array<"u"?(function(){var s=new Float64Array([-0]),r=new Uint8Array(s.buffer),o=r[7]===128;function a(g,_,w){s[0]=g,_[w]=r[0],_[w+1]=r[1],_[w+2]=r[2],_[w+3]=r[3],_[w+4]=r[4],_[w+5]=r[5],_[w+6]=r[6],_[w+7]=r[7]}function p(g,_,w){s[0]=g,_[w]=r[7],_[w+1]=r[6],_[w+2]=r[5],_[w+3]=r[4],_[w+4]=r[3],_[w+5]=r[2],_[w+6]=r[1],_[w+7]=r[0]}c.writeDoubleLE=o?a:p,c.writeDoubleBE=o?p:a;function h(g,_){return r[0]=g[_],r[1]=g[_+1],r[2]=g[_+2],r[3]=g[_+3],r[4]=g[_+4],r[5]=g[_+5],r[6]=g[_+6],r[7]=g[_+7],s[0]}function m(g,_){return r[7]=g[_],r[6]=g[_+1],r[5]=g[_+2],r[4]=g[_+3],r[3]=g[_+4],r[2]=g[_+5],r[1]=g[_+6],r[0]=g[_+7],s[0]}c.readDoubleLE=o?h:m,c.readDoubleBE=o?m:h})():(function(){function s(o,a,p,h,m,g){var _=h<0?1:0;if(_&&(h=-h),h===0)o(0,m,g+a),o(1/h>0?0:2147483648,m,g+p);else if(isNaN(h))o(0,m,g+a),o(2146959360,m,g+p);else if(h>17976931348623157e292)o(0,m,g+a),o((_<<31|2146435072)>>>0,m,g+p);else{var w;if(h<22250738585072014e-324)w=h/5e-324,o(w>>>0,m,g+a),o((_<<31|w/4294967296)>>>0,m,g+p);else{var y=Math.floor(Math.log(h)/Math.LN2);y===1024&&(y=1023),w=h*Math.pow(2,-y),o(w*4503599627370496>>>0,m,g+a),o((_<<31|y+1023<<20|w*1048576&1048575)>>>0,m,g+p)}}}c.writeDoubleLE=s.bind(null,i,0,4),c.writeDoubleBE=s.bind(null,u,4,0);function r(o,a,p,h,m){var g=o(h,m+a),_=o(h,m+p),w=(_>>31)*2+1,y=_>>>20&2047,T=4294967296*(_&1048575)+g;return y===2047?T?NaN:w*(1/0):y===0?w*5e-324*T:w*Math.pow(2,y-1075)*(T+4503599627370496)}c.readDoubleLE=r.bind(null,l,0,4),c.readDoubleBE=r.bind(null,d,4,0)})(),c}function i(c,s,r){s[r]=c&255,s[r+1]=c>>>8&255,s[r+2]=c>>>16&255,s[r+3]=c>>>24}function u(c,s,r){s[r]=c>>>24,s[r+1]=c>>>16&255,s[r+2]=c>>>8&255,s[r+3]=c&255}function l(c,s){return(c[s]|c[s+1]<<8|c[s+2]<<16|c[s+3]<<24)>>>0}function d(c,s){return(c[s]<<24|c[s+1]<<16|c[s+2]<<8|c[s+3])>>>0}}),xO=ae((e,t)=>{"use strict";t.exports=n;function n(i){try{if(typeof Bu!="function")return null;var u=Bu(i);return u&&(u.length||Object.keys(u).length)?u:null}catch{return null}}}),$O=ae(e=>{"use strict";var t=e,n="\uFFFD";t.length=function(i){for(var u=0,l=0,d=0;d=192&&s<224){var r=(s&31)<<6|i[c++]&63;d+=r>=128?String.fromCharCode(r):n}else if(s>=224&&s<240){var o=(s&15)<<12|(i[c++]&63)<<6|i[c++]&63;d+=o>=2048?String.fromCharCode(o):n}else if(s>=240){var a=(s&7)<<18|(i[c++]&63)<<12|(i[c++]&63)<<6|i[c++]&63;a<65536||a>1114111?d+=n:(a-=65536,d+=String.fromCharCode(55296+(a>>10)),d+=String.fromCharCode(56320+(a&1023)))}}return d},t.write=function(i,u,l){for(var d=l,c,s,r=0;r>6|192,u[l++]=c&63|128):(c&64512)===55296&&((s=i.charCodeAt(r+1))&64512)===56320?(c=65536+((c&1023)<<10)+(s&1023),++r,u[l++]=c>>18|240,u[l++]=c>>12&63|128,u[l++]=c>>6&63|128,u[l++]=c&63|128):(u[l++]=c>>12|224,u[l++]=c>>6&63|128,u[l++]=c&63|128);return l-d}}),TO=ae((e,t)=>{"use strict";t.exports=n;function n(i,u,l){var d=l||8192,c=d>>>1,s=null,r=d;return function(o){if(o<1||o>c)return i(o);r+o>d&&(s=i(d),r=0);var a=u.call(s,r,r+=o);return r&7&&(r=(r|7)+1),a}}}),SO=ae((e,t)=>{"use strict";t.exports=i;var n=_n();function i(c,s){this.lo=c>>>0,this.hi=s>>>0}var u=i.zero=new i(0,0);u.toNumber=function(){return 0},u.zzEncode=u.zzDecode=function(){return this},u.length=function(){return 1};var l=i.zeroHash="\0\0\0\0\0\0\0\0";i.fromNumber=function(c){if(c===0)return u;var s=c<0;s&&(c=-c);var r=c>>>0,o=(c-r)/4294967296>>>0;return s&&(o=~o>>>0,r=~r>>>0,++r>4294967295&&(r=0,++o>4294967295&&(o=0))),new i(r,o)},i.from=function(c){if(typeof c=="number")return i.fromNumber(c);if(n.isString(c))if(n.Long)c=n.Long.fromString(c);else return i.fromNumber(parseInt(c,10));return c.low||c.high?new i(c.low>>>0,c.high>>>0):u},i.prototype.toNumber=function(c){if(!c&&this.hi>>>31){var s=~this.lo+1>>>0,r=~this.hi>>>0;return s||(r=r+1>>>0),-(s+r*4294967296)}return this.lo+this.hi*4294967296},i.prototype.toLong=function(c){return n.Long?new n.Long(this.lo|0,this.hi|0,!!c):{low:this.lo|0,high:this.hi|0,unsigned:!!c}};var d=String.prototype.charCodeAt;i.fromHash=function(c){return c===l?u:new i((d.call(c,0)|d.call(c,1)<<8|d.call(c,2)<<16|d.call(c,3)<<24)>>>0,(d.call(c,4)|d.call(c,5)<<8|d.call(c,6)<<16|d.call(c,7)<<24)>>>0)},i.prototype.toHash=function(){return String.fromCharCode(this.lo&255,this.lo>>>8&255,this.lo>>>16&255,this.lo>>>24,this.hi&255,this.hi>>>8&255,this.hi>>>16&255,this.hi>>>24)},i.prototype.zzEncode=function(){var c=this.hi>>31;return this.hi=((this.hi<<1|this.lo>>>31)^c)>>>0,this.lo=(this.lo<<1^c)>>>0,this},i.prototype.zzDecode=function(){var c=-(this.lo&1);return this.lo=((this.lo>>>1|this.hi<<31)^c)>>>0,this.hi=(this.hi>>>1^c)>>>0,this},i.prototype.length=function(){var c=this.lo,s=(this.lo>>>28|this.hi<<4)>>>0,r=this.hi>>>24;return r===0?s===0?c<16384?c<128?1:2:c<2097152?3:4:s<16384?s<128?5:6:s<2097152?7:8:r<128?9:10}}),IO=ae((e,t)=>{(function(n,i){function u(l){return l.default||l}typeof define=="function"&&define.amd?define([],function(){var l={};return i(l),u(l)}):typeof e=="object"?(i(e),typeof t=="object"&&(t.exports=u(e))):(function(){var l={};i(l),n.Long=u(l)})()})(typeof globalThis<"u"?globalThis:typeof self<"u"?self:e,function(n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;var i=null;try{i=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}function u(v,R,H){this.low=v|0,this.high=R|0,this.unsigned=!!H}u.prototype.__isLong__,Object.defineProperty(u.prototype,"__isLong__",{value:!0});function l(v){return(v&&v.__isLong__)===!0}function d(v){var R=Math.clz32(v&-v);return v?31-R:R}u.isLong=l;var c={},s={};function r(v,R){var H,L,V;return R?(v>>>=0,(V=0<=v&&v<256)&&(L=s[v],L)?L:(H=a(v,0,!0),V&&(s[v]=H),H)):(v|=0,(V=-128<=v&&v<128)&&(L=c[v],L)?L:(H=a(v,v<0?-1:0,!1),V&&(c[v]=H),H))}u.fromInt=r;function o(v,R){if(isNaN(v))return R?A:O;if(R){if(v<0)return A;if(v>=y)return J}else{if(v<=-T)return q;if(v+1>=T)return G}return v<0?o(-v,R).neg():a(v%w|0,v/w|0,R)}u.fromNumber=o;function a(v,R,H){return new u(v,R,H)}u.fromBits=a;var p=Math.pow;function h(v,R,H){if(v.length===0)throw Error("empty string");if(typeof R=="number"?(H=R,R=!1):R=!!R,v==="NaN"||v==="Infinity"||v==="+Infinity"||v==="-Infinity")return R?A:O;if(H=H||10,H<2||360)throw Error("interior hyphen");if(L===0)return h(v.substring(1),R,H).neg();for(var V=o(p(H,8)),K=O,B=0;B>>0:this.low},C.toNumber=function(){return this.unsigned?(this.high>>>0)*w+(this.low>>>0):this.high*w+(this.low>>>0)},C.toString=function(v){if(v=v||10,v<2||36>>0,N=Y.toString(v);if(K=te,K.isZero())return N+B;for(;N.length<6;)N="0"+N;B=""+N+B}},C.getHighBits=function(){return this.high},C.getHighBitsUnsigned=function(){return this.high>>>0},C.getLowBits=function(){return this.low},C.getLowBitsUnsigned=function(){return this.low>>>0},C.getNumBitsAbs=function(){if(this.isNegative())return this.eq(q)?64:this.neg().getNumBitsAbs();for(var v=this.high!=0?this.high:this.low,R=31;R>0&&(v&1<>21;return v?this.unsigned?!1:v===-1&&!(this.low===0&&this.high===-2097152):!0},C.isZero=function(){return this.high===0&&this.low===0},C.eqz=C.isZero,C.isNegative=function(){return!this.unsigned&&this.high<0},C.isPositive=function(){return this.unsigned||this.high>=0},C.isOdd=function(){return(this.low&1)===1},C.isEven=function(){return(this.low&1)===0},C.equals=function(v){return l(v)||(v=m(v)),this.unsigned!==v.unsigned&&this.high>>>31===1&&v.high>>>31===1?!1:this.high===v.high&&this.low===v.low},C.eq=C.equals,C.notEquals=function(v){return!this.eq(v)},C.neq=C.notEquals,C.ne=C.notEquals,C.lessThan=function(v){return this.comp(v)<0},C.lt=C.lessThan,C.lessThanOrEqual=function(v){return this.comp(v)<=0},C.lte=C.lessThanOrEqual,C.le=C.lessThanOrEqual,C.greaterThan=function(v){return this.comp(v)>0},C.gt=C.greaterThan,C.greaterThanOrEqual=function(v){return this.comp(v)>=0},C.gte=C.greaterThanOrEqual,C.ge=C.greaterThanOrEqual,C.compare=function(v){if(l(v)||(v=m(v)),this.eq(v))return 0;var R=this.isNegative(),H=v.isNegative();return R&&!H?-1:!R&&H?1:this.unsigned?v.high>>>0>this.high>>>0||v.high===this.high&&v.low>>>0>this.low>>>0?-1:1:this.sub(v).isNegative()?-1:1},C.comp=C.compare,C.negate=function(){return!this.unsigned&&this.eq(q)?q:this.not().add(P)},C.neg=C.negate,C.add=function(v){l(v)||(v=m(v));var R=this.high>>>16,H=this.high&65535,L=this.low>>>16,V=this.low&65535,K=v.high>>>16,B=v.high&65535,te=v.low>>>16,Y=v.low&65535,N=0,X=0,W=0,ne=0;return ne+=V+Y,W+=ne>>>16,ne&=65535,W+=L+te,X+=W>>>16,W&=65535,X+=H+B,N+=X>>>16,X&=65535,N+=R+K,N&=65535,a(W<<16|ne,N<<16|X,this.unsigned)},C.subtract=function(v){return l(v)||(v=m(v)),this.add(v.neg())},C.sub=C.subtract,C.multiply=function(v){if(this.isZero())return this;if(l(v)||(v=m(v)),i){var R=i.mul(this.low,this.high,v.low,v.high);return a(R,i.get_high(),this.unsigned)}if(v.isZero())return this.unsigned?A:O;if(this.eq(q))return v.isOdd()?q:O;if(v.eq(q))return this.isOdd()?q:O;if(this.isNegative())return v.isNegative()?this.neg().mul(v.neg()):this.neg().mul(v).neg();if(v.isNegative())return this.mul(v.neg()).neg();if(this.lt(S)&&v.lt(S))return o(this.toNumber()*v.toNumber(),this.unsigned);var H=this.high>>>16,L=this.high&65535,V=this.low>>>16,K=this.low&65535,B=v.high>>>16,te=v.high&65535,Y=v.low>>>16,N=v.low&65535,X=0,W=0,ne=0,le=0;return le+=K*N,ne+=le>>>16,le&=65535,ne+=V*N,W+=ne>>>16,ne&=65535,ne+=K*Y,W+=ne>>>16,ne&=65535,W+=L*N,X+=W>>>16,W&=65535,W+=V*Y,X+=W>>>16,W&=65535,W+=K*te,X+=W>>>16,W&=65535,X+=H*N+L*Y+V*te+K*B,X&=65535,a(ne<<16|le,X<<16|W,this.unsigned)},C.mul=C.multiply,C.divide=function(v){if(l(v)||(v=m(v)),v.isZero())throw Error("division by zero");if(i){if(!this.unsigned&&this.high===-2147483648&&v.low===-1&&v.high===-1)return this;var R=(this.unsigned?i.div_u:i.div_s)(this.low,this.high,v.low,v.high);return a(R,i.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?A:O;var H,L,V;if(this.unsigned){if(v.unsigned||(v=v.toUnsigned()),v.gt(this))return A;if(v.gt(this.shru(1)))return I;V=A}else{if(this.eq(q)){if(v.eq(P)||v.eq(z))return q;if(v.eq(q))return P;var K=this.shr(1);return H=K.div(v).shl(1),H.eq(O)?v.isNegative()?P:z:(L=this.sub(v.mul(H)),V=H.add(L.div(v)),V)}else if(v.eq(q))return this.unsigned?A:O;if(this.isNegative())return v.isNegative()?this.neg().div(v.neg()):this.neg().div(v).neg();if(v.isNegative())return this.div(v.neg()).neg();V=O}for(L=this;L.gte(v);){H=Math.max(1,Math.floor(L.toNumber()/v.toNumber()));for(var B=Math.ceil(Math.log(H)/Math.LN2),te=B<=48?1:p(2,B-48),Y=o(H),N=Y.mul(v);N.isNegative()||N.gt(L);)H-=te,Y=o(H,this.unsigned),N=Y.mul(v);Y.isZero()&&(Y=P),V=V.add(Y),L=L.sub(N)}return V},C.div=C.divide,C.modulo=function(v){if(l(v)||(v=m(v)),i){var R=(this.unsigned?i.rem_u:i.rem_s)(this.low,this.high,v.low,v.high);return a(R,i.get_high(),this.unsigned)}return this.sub(this.div(v).mul(v))},C.mod=C.modulo,C.rem=C.modulo,C.not=function(){return a(~this.low,~this.high,this.unsigned)},C.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},C.clz=C.countLeadingZeros,C.countTrailingZeros=function(){return this.low?d(this.low):d(this.high)+32},C.ctz=C.countTrailingZeros,C.and=function(v){return l(v)||(v=m(v)),a(this.low&v.low,this.high&v.high,this.unsigned)},C.or=function(v){return l(v)||(v=m(v)),a(this.low|v.low,this.high|v.high,this.unsigned)},C.xor=function(v){return l(v)||(v=m(v)),a(this.low^v.low,this.high^v.high,this.unsigned)},C.shiftLeft=function(v){return l(v)&&(v=v.toInt()),(v&=63)===0?this:v<32?a(this.low<>>32-v,this.unsigned):a(0,this.low<>>v|this.high<<32-v,this.high>>v,this.unsigned):a(this.high>>v-32,this.high>=0?0:-1,this.unsigned)},C.shr=C.shiftRight,C.shiftRightUnsigned=function(v){return l(v)&&(v=v.toInt()),(v&=63)===0?this:v<32?a(this.low>>>v|this.high<<32-v,this.high>>>v,this.unsigned):v===32?a(this.high,0,this.unsigned):a(this.high>>>v-32,0,this.unsigned)},C.shru=C.shiftRightUnsigned,C.shr_u=C.shiftRightUnsigned,C.rotateLeft=function(v){var R;return l(v)&&(v=v.toInt()),(v&=63)===0?this:v===32?a(this.high,this.low,this.unsigned):v<32?(R=32-v,a(this.low<>>R,this.high<>>R,this.unsigned)):(v-=32,R=32-v,a(this.high<>>R,this.low<>>R,this.unsigned))},C.rotl=C.rotateLeft,C.rotateRight=function(v){var R;return l(v)&&(v=v.toInt()),(v&=63)===0?this:v===32?a(this.high,this.low,this.unsigned):v<32?(R=32-v,a(this.high<>>v,this.low<>>v,this.unsigned)):(v-=32,R=32-v,a(this.low<>>v,this.high<>>v,this.unsigned))},C.rotr=C.rotateRight,C.toSigned=function(){return this.unsigned?a(this.low,this.high,!1):this},C.toUnsigned=function(){return this.unsigned?this:a(this.low,this.high,!0)},C.toBytes=function(v){return v?this.toBytesLE():this.toBytesBE()},C.toBytesLE=function(){var v=this.high,R=this.low;return[R&255,R>>>8&255,R>>>16&255,R>>>24,v&255,v>>>8&255,v>>>16&255,v>>>24]},C.toBytesBE=function(){var v=this.high,R=this.low;return[v>>>24,v>>>16&255,v>>>8&255,v&255,R>>>24,R>>>16&255,R>>>8&255,R&255]},u.fromBytes=function(v,R,H){return H?u.fromBytesLE(v,R):u.fromBytesBE(v,R)},u.fromBytesLE=function(v,R){return new u(v[0]|v[1]<<8|v[2]<<16|v[3]<<24,v[4]|v[5]<<8|v[6]<<16|v[7]<<24,R)},u.fromBytesBE=function(v,R){return new u(v[4]<<24|v[5]<<16|v[6]<<8|v[7],v[0]<<24|v[1]<<16|v[2]<<8|v[3],R)},typeof BigInt=="function"&&(u.fromBigInt=function(v,R){var H=Number(BigInt.asIntN(32,v)),L=Number(BigInt.asIntN(32,v>>BigInt(32)));return a(H,L,R)},u.fromValue=function(v,R){return typeof v=="bigint"?u.fromBigInt(v,R):m(v,R)},C.toBigInt=function(){var v=BigInt(this.low>>>0),R=BigInt(this.unsigned?this.high>>>0:this.high);return R<{"use strict";var t=e;t.asPromise=yO(),t.base64=wO(),t.EventEmitter=_O(),t.float=vO(),t.inquire=xO(),t.utf8=$O(),t.pool=TO(),t.LongBits=SO();function n(l){return l==="__proto__"||l==="prototype"||l==="constructor"}t.isUnsafeProperty=n,t.isNode=!!(typeof global<"u"&&global&&global.process&&global.process.versions&&global.process.versions.node),t.global=t.isNode&&global||typeof window<"u"&&window||typeof self<"u"&&self||e,t.emptyArray=Object.freeze?Object.freeze([]):[],t.emptyObject=Object.freeze?Object.freeze({}):{},t.isInteger=Number.isInteger||function(l){return typeof l=="number"&&isFinite(l)&&Math.floor(l)===l},t.isString=function(l){return typeof l=="string"||l instanceof String},t.isObject=function(l){return l&&typeof l=="object"},t.isset=t.isSet=function(l,d){var c=l[d];return c!=null&&Object.hasOwnProperty.call(l,d)?typeof c!="object"||(Array.isArray(c)?c.length:Object.keys(c).length)>0:!1},t.Buffer=(function(){try{var l=t.global.Buffer;return l.prototype.utf8Write?l:null}catch{return null}})(),t._Buffer_from=null,t._Buffer_allocUnsafe=null,t.newBuffer=function(l){return typeof l=="number"?t.Buffer?t._Buffer_allocUnsafe(l):new t.Array(l):t.Buffer?t._Buffer_from(l):typeof Uint8Array>"u"?l:new Uint8Array(l)},t.Array=typeof Uint8Array<"u"?Uint8Array:Array,t.Long=t.global.dcodeIO&&t.global.dcodeIO.Long||t.global.Long||(function(){try{var l=IO();return l&&l.isLong?l:null}catch{return null}})(),t.key2Re=/^true|false|0|1$/,t.key32Re=/^-?(?:0|[1-9][0-9]*)$/,t.key64Re=/^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/,t.longToHash=function(l){return l?t.LongBits.from(l).toHash():t.LongBits.zeroHash},t.longFromHash=function(l,d){var c=t.LongBits.fromHash(l);return t.Long?t.Long.fromBits(c.lo,c.hi,d):c.toNumber(!!d)};function i(l){var d=typeof arguments[arguments.length-1]=="boolean",c=d?arguments.length-1:arguments.length;d=d&&arguments[arguments.length-1];for(var s=1;s-1;--r)if(d[s[r]]===1&&this[s[r]]!==void 0&&this[s[r]]!==null)return s[r]}},t.oneOfSetter=function(l){return function(d){for(var c=0;c{"use strict";t.exports=o;var n=_n(),i,u=n.LongBits,l=n.base64,d=n.utf8;function c(y,T,S){this.fn=y,this.len=T,this.next=void 0,this.val=S}function s(){}function r(y){this.head=y.head,this.tail=y.tail,this.len=y.len,this.next=y.states}function o(){this.len=0,this.head=new c(s,0,0),this.tail=this.head,this.states=null}var a=function(){return n.Buffer?function(){return(o.create=function(){return new i})()}:function(){return new o}};o.create=a(),o.alloc=function(y){return new n.Array(y)},n.Array!==Array&&(o.alloc=n.pool(o.alloc,n.Array.prototype.subarray)),o.prototype._push=function(y,T,S){return this.tail=this.tail.next=new c(y,T,S),this.len+=T,this};function p(y,T,S){T[S]=y&255}function h(y,T,S){for(;y>127;)T[S++]=y&127|128,y>>>=7;T[S]=y}function m(y,T){this.len=y,this.next=void 0,this.val=T}m.prototype=Object.create(c.prototype),m.prototype.fn=h,o.prototype.uint32=function(y){return this.len+=(this.tail=this.tail.next=new m((y=y>>>0)<128?1:y<16384?2:y<2097152?3:y<268435456?4:5,y)).len,this},o.prototype.int32=function(y){return(y|=0)<0?this._push(g,10,u.fromNumber(y)):this.uint32(y)},o.prototype.sint32=function(y){return this.uint32((y<<1^y>>31)>>>0)};function g(y,T,S){for(var O=y.lo,A=y.hi;A;)T[S++]=O&127|128,O=(O>>>7|A<<25)>>>0,A>>>=7;for(;O>127;)T[S++]=O&127|128,O=O>>>7;T[S++]=O}o.prototype.uint64=function(y){var T=u.from(y);return this._push(g,T.length(),T)},o.prototype.int64=o.prototype.uint64,o.prototype.sint64=function(y){var T=u.from(y).zzEncode();return this._push(g,T.length(),T)},o.prototype.bool=function(y){return this._push(p,1,y?1:0)};function _(y,T,S){T[S]=y&255,T[S+1]=y>>>8&255,T[S+2]=y>>>16&255,T[S+3]=y>>>24}o.prototype.fixed32=function(y){return this._push(_,4,y>>>0)},o.prototype.sfixed32=o.prototype.fixed32,o.prototype.fixed64=function(y){var T=u.from(y);return this._push(_,4,T.lo)._push(_,4,T.hi)},o.prototype.sfixed64=o.prototype.fixed64,o.prototype.float=function(y){return this._push(n.float.writeFloatLE,4,y)},o.prototype.double=function(y){return this._push(n.float.writeDoubleLE,8,y)};var w=n.Array.prototype.set?function(y,T,S){T.set(y,S)}:function(y,T,S){for(var O=0;O>>0;if(!T)return this._push(p,1,0);if(n.isString(y)){var S=o.alloc(T=l.length(y));l.decode(y,S,0),y=S}return this.uint32(T)._push(w,T,y)},o.prototype.string=function(y){var T=d.length(y);return T?this.uint32(T)._push(d.write,T,y):this._push(p,1,0)},o.prototype.fork=function(){return this.states=new r(this),this.head=this.tail=new c(s,0,0),this.len=0,this},o.prototype.reset=function(){return this.states?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new c(s,0,0),this.len=0),this},o.prototype.ldelim=function(){var y=this.head,T=this.tail,S=this.len;return this.reset().uint32(S),S&&(this.tail.next=y.next,this.tail=T,this.len+=S),this},o.prototype.finish=function(){for(var y=this.head.next,T=this.constructor.alloc(this.len),S=0;y;)y.fn(y.val,T,S),S+=y.len,y=y.next;return T},o._configure=function(y){i=y,o.create=a(),i._configure()}}),OO=ae((e,t)=>{"use strict";t.exports=u;var n=yw();(u.prototype=Object.create(n.prototype)).constructor=u;var i=_n();function u(){n.call(this)}u._configure=function(){u.alloc=i._Buffer_allocUnsafe,u.writeBytesBuffer=i.Buffer&&i.Buffer.prototype instanceof Uint8Array&&i.Buffer.prototype.set.name==="set"?function(d,c,s){c.set(d,s)}:function(d,c,s){if(d.copy)d.copy(c,s,0,d.length);else for(var r=0;r>>0;return this.uint32(c),c&&this._push(u.writeBytesBuffer,c,d),this};function l(d,c,s){d.length<40?i.utf8.write(d,c,s):c.utf8Write?c.utf8Write(d,s):c.write(d,s)}u.prototype.string=function(d){var c=i.Buffer.byteLength(d);return this.uint32(c),c&&this._push(l,c,d),this},u._configure()}),ww=ae((e,t)=>{"use strict";t.exports=c;var n=_n(),i,u=n.LongBits,l=n.utf8;function d(h,m){return RangeError("index out of range: "+h.pos+" + "+(m||1)+" > "+h.len)}function c(h){this.buf=h,this.pos=0,this.len=h.length}var s=typeof Uint8Array<"u"?function(h){if(h instanceof Uint8Array||Array.isArray(h))return new c(h);throw Error("illegal buffer")}:function(h){if(Array.isArray(h))return new c(h);throw Error("illegal buffer")},r=function(){return n.Buffer?function(h){return(c.create=function(m){return n.Buffer.isBuffer(m)?new i(m):s(m)})(h)}:s};c.create=r(),c.prototype._slice=n.Array.prototype.subarray||n.Array.prototype.slice,c.prototype.uint32=(function(){var h=4294967295;return function(){if(h=(this.buf[this.pos]&127)>>>0,this.buf[this.pos++]<128||(h=(h|(this.buf[this.pos]&127)<<7)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<14)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<21)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&15)<<28)>>>0,this.buf[this.pos++]<128))return h;if((this.pos+=5)>this.len)throw this.pos=this.len,d(this,10);return h}})(),c.prototype.int32=function(){return this.uint32()|0},c.prototype.sint32=function(){var h=this.uint32();return h>>>1^-(h&1)|0};function o(){var h=new u(0,0),m=0;if(this.len-this.pos>4){for(;m<4;++m)if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h;if(h.lo=(h.lo|(this.buf[this.pos]&127)<<28)>>>0,h.hi=(h.hi|(this.buf[this.pos]&127)>>4)>>>0,this.buf[this.pos++]<128)return h;m=0}else{for(;m<3;++m){if(this.pos>=this.len)throw d(this);if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}return h.lo=(h.lo|(this.buf[this.pos++]&127)<>>0,h}if(this.len-this.pos>4){for(;m<5;++m)if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}else for(;m<5;++m){if(this.pos>=this.len)throw d(this);if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}throw Error("invalid varint encoding")}c.prototype.bool=function(){return this.uint32()!==0};function a(h,m){return(h[m-4]|h[m-3]<<8|h[m-2]<<16|h[m-1]<<24)>>>0}c.prototype.fixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)},c.prototype.sfixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)|0};function p(){if(this.pos+8>this.len)throw d(this,8);return new u(a(this.buf,this.pos+=4),a(this.buf,this.pos+=4))}c.prototype.float=function(){if(this.pos+4>this.len)throw d(this,4);var h=n.float.readFloatLE(this.buf,this.pos);return this.pos+=4,h},c.prototype.double=function(){if(this.pos+8>this.len)throw d(this,4);var h=n.float.readDoubleLE(this.buf,this.pos);return this.pos+=8,h},c.prototype.bytes=function(){var h=this.uint32(),m=this.pos,g=this.pos+h;if(g>this.len)throw d(this,h);if(this.pos+=h,Array.isArray(this.buf))return this.buf.slice(m,g);if(m===g){var _=n.Buffer;return _?_.alloc(0):new this.buf.constructor(0)}return this._slice.call(this.buf,m,g)},c.prototype.string=function(){var h=this.bytes();return l.read(h,0,h.length)},c.prototype.skip=function(h){if(typeof h=="number"){if(this.pos+h>this.len)throw d(this,h);this.pos+=h}else do if(this.pos>=this.len)throw d(this);while(this.buf[this.pos++]&128);return this},c.recursionLimit=n.recursionLimit,c.prototype.skipType=function(h,m){if(m===void 0&&(m=0),m>c.recursionLimit)throw Error("maximum nesting depth exceeded");switch(h){case 0:this.skip();break;case 1:this.skip(8);break;case 2:this.skip(this.uint32());break;case 3:for(;(h=this.uint32()&7)!==4;)this.skipType(h,m+1);break;case 5:this.skip(4);break;default:throw Error("invalid wire type "+h+" at offset "+this.pos)}return this},c._configure=function(h){i=h,c.create=r(),i._configure();var m=n.Long?"toLong":"toNumber";n.merge(c.prototype,{int64:function(){return o.call(this)[m](!1)},uint64:function(){return o.call(this)[m](!0)},sint64:function(){return o.call(this).zzDecode()[m](!1)},fixed64:function(){return p.call(this)[m](!0)},sfixed64:function(){return p.call(this)[m](!1)}})}}),EO=ae((e,t)=>{"use strict";t.exports=u;var n=ww();(u.prototype=Object.create(n.prototype)).constructor=u;var i=_n();function u(l){n.call(this,l)}u._configure=function(){i.Buffer&&(u.prototype._slice=i.Buffer.prototype.slice)},u.prototype.string=function(){var l=this.uint32();return this.buf.utf8Slice?this.buf.utf8Slice(this.pos,this.pos=Math.min(this.pos+l,this.len)):this.buf.toString("utf-8",this.pos,this.pos=Math.min(this.pos+l,this.len))},u._configure()}),AO=ae((e,t)=>{"use strict";t.exports=i;var n=_n();(i.prototype=Object.create(n.EventEmitter.prototype)).constructor=i;function i(u,l,d){if(typeof u!="function")throw TypeError("rpcImpl must be a function");n.EventEmitter.call(this),this.rpcImpl=u,this.requestDelimited=!!l,this.responseDelimited=!!d}i.prototype.rpcCall=function u(l,d,c,s,r){if(!s)throw TypeError("request must be specified");var o=this;if(!r)return n.asPromise(u,o,l,d,c,s);if(!o.rpcImpl){setTimeout(function(){r(Error("already ended"))},0);return}try{return o.rpcImpl(l,d[o.requestDelimited?"encodeDelimited":"encode"](s).finish(),function(a,p){if(a)return o.emit("error",a,l),r(a);if(p===null){o.end(!0);return}if(!(p instanceof c))try{p=c[o.responseDelimited?"decodeDelimited":"decode"](p)}catch(h){return o.emit("error",h,l),r(h)}return o.emit("data",p,l),r(null,p)})}catch(a){o.emit("error",a,l),setTimeout(function(){r(a)},0);return}},i.prototype.end=function(u){return this.rpcImpl&&(u||this.rpcImpl(null,null,null),this.rpcImpl=null,this.emit("end").off()),this}}),kO=ae(e=>{"use strict";var t=e;t.Service=AO()}),PO=ae((e,t)=>{"use strict";t.exports=Object.create(null)}),CO=ae(e=>{"use strict";var t=e;t.build="minimal",t.Writer=yw(),t.BufferWriter=OO(),t.Reader=ww(),t.BufferReader=EO(),t.util=_n(),t.rpc=kO(),t.roots=PO(),t.configure=n;function n(){t.util._configure(),t.Writer._configure(t.BufferWriter),t.Reader._configure(t.BufferReader)}n()}),RO=ae((e,t)=>{"use strict";t.exports=CO()}),Ni=ae((e,t)=>{"use strict";var n=RO(),i=n.Reader,u=n.Writer,l=n.util,d=n.roots.default||(n.roots.default={});d.onnx=(function(){var c={};return c.Version=(function(){var s={},r=Object.create(s);return r[s[0]="_START_VERSION"]=0,r[s[1]="IR_VERSION_2017_10_10"]=1,r[s[2]="IR_VERSION_2017_10_30"]=2,r[s[3]="IR_VERSION_2017_11_3"]=3,r[s[4]="IR_VERSION_2019_1_22"]=4,r[s[5]="IR_VERSION_2019_3_18"]=5,r[s[6]="IR_VERSION_2019_9_19"]=6,r[s[7]="IR_VERSION_2020_5_8"]=7,r[s[8]="IR_VERSION_2021_7_30"]=8,r[s[9]="IR_VERSION"]=9,r})(),c.AttributeProto=(function(){function s(r){if(this.floats=[],this.ints=[],this.strings=[],this.tensors=[],this.graphs=[],this.sparseTensors=[],this.typeProtos=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 21:{p.refAttrName=r.string();break}case 13:{p.docString=r.string();break}case 20:{p.type=r.int32();break}case 2:{p.f=r.float();break}case 3:{p.i=r.int64();break}case 4:{p.s=r.bytes();break}case 5:{p.t=d.onnx.TensorProto.decode(r,r.uint32());break}case 6:{p.g=d.onnx.GraphProto.decode(r,r.uint32());break}case 22:{p.sparseTensor=d.onnx.SparseTensorProto.decode(r,r.uint32());break}case 14:{p.tp=d.onnx.TypeProto.decode(r,r.uint32());break}case 7:{if(p.floats&&p.floats.length||(p.floats=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.i.high>>>0).toNumber())),r.s!=null&&(typeof r.s=="string"?l.base64.decode(r.s,o.s=l.newBuffer(l.base64.length(r.s)),0):r.s.length>=0&&(o.s=r.s)),r.t!=null){if(typeof r.t!="object")throw TypeError(".onnx.AttributeProto.t: object expected");o.t=d.onnx.TensorProto.fromObject(r.t)}if(r.g!=null){if(typeof r.g!="object")throw TypeError(".onnx.AttributeProto.g: object expected");o.g=d.onnx.GraphProto.fromObject(r.g)}if(r.sparseTensor!=null){if(typeof r.sparseTensor!="object")throw TypeError(".onnx.AttributeProto.sparseTensor: object expected");o.sparseTensor=d.onnx.SparseTensorProto.fromObject(r.sparseTensor)}if(r.tp!=null){if(typeof r.tp!="object")throw TypeError(".onnx.AttributeProto.tp: object expected");o.tp=d.onnx.TypeProto.fromObject(r.tp)}if(r.floats){if(!Array.isArray(r.floats))throw TypeError(".onnx.AttributeProto.floats: array expected");o.floats=[];for(var a=0;a>>0,r.ints[a].high>>>0).toNumber())}if(r.strings){if(!Array.isArray(r.strings))throw TypeError(".onnx.AttributeProto.strings: array expected");o.strings=[];for(var a=0;a=0&&(o.strings[a]=r.strings[a])}if(r.tensors){if(!Array.isArray(r.tensors))throw TypeError(".onnx.AttributeProto.tensors: array expected");o.tensors=[];for(var a=0;a>>0,r.i.high>>>0).toNumber():r.i),r.s!=null&&r.hasOwnProperty("s")&&(a.s=o.bytes===String?l.base64.encode(r.s,0,r.s.length):o.bytes===Array?Array.prototype.slice.call(r.s):r.s),r.t!=null&&r.hasOwnProperty("t")&&(a.t=d.onnx.TensorProto.toObject(r.t,o)),r.g!=null&&r.hasOwnProperty("g")&&(a.g=d.onnx.GraphProto.toObject(r.g,o)),r.floats&&r.floats.length){a.floats=[];for(var h=0;h>>0,r.ints[h].high>>>0).toNumber():r.ints[h]}if(r.strings&&r.strings.length){a.strings=[];for(var h=0;h>>3){case 1:{p.name=r.string();break}case 2:{p.type=d.onnx.TypeProto.decode(r,r.uint32());break}case 3:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.type!=null&&r.hasOwnProperty("type")){var o=d.onnx.TypeProto.verify(r.type);if(o)return"type."+o}return r.docString!=null&&r.hasOwnProperty("docString")&&!l.isString(r.docString)?"docString: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.ValueInfoProto)return r;var o=new d.onnx.ValueInfoProto;if(r.name!=null&&(o.name=String(r.name)),r.type!=null){if(typeof r.type!="object")throw TypeError(".onnx.ValueInfoProto.type: object expected");o.type=d.onnx.TypeProto.fromObject(r.type)}return r.docString!=null&&(o.docString=String(r.docString)),o},s.toObject=function(r,o){o||(o={});var a={};return o.defaults&&(a.name="",a.type=null,a.docString=""),r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.type!=null&&r.hasOwnProperty("type")&&(a.type=d.onnx.TypeProto.toObject(r.type,o)),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.ValueInfoProto"},s})(),c.NodeProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 2:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 3:{p.name=r.string();break}case 4:{p.opType=r.string();break}case 7:{p.domain=r.string();break}case 5:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 6:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var o=0;o>>3){case 1:{p.initialization=d.onnx.GraphProto.decode(r,r.uint32());break}case 2:{p.algorithm=d.onnx.GraphProto.decode(r,r.uint32());break}case 3:{p.initializationBinding&&p.initializationBinding.length||(p.initializationBinding=[]),p.initializationBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 4:{p.updateBinding&&p.updateBinding.length||(p.updateBinding=[]),p.updateBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.initialization!=null&&r.hasOwnProperty("initialization")){var o=d.onnx.GraphProto.verify(r.initialization);if(o)return"initialization."+o}if(r.algorithm!=null&&r.hasOwnProperty("algorithm")){var o=d.onnx.GraphProto.verify(r.algorithm);if(o)return"algorithm."+o}if(r.initializationBinding!=null&&r.hasOwnProperty("initializationBinding")){if(!Array.isArray(r.initializationBinding))return"initializationBinding: array expected";for(var a=0;a>>3){case 1:{p.irVersion=r.int64();break}case 8:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 2:{p.producerName=r.string();break}case 3:{p.producerVersion=r.string();break}case 4:{p.domain=r.string();break}case 5:{p.modelVersion=r.int64();break}case 6:{p.docString=r.string();break}case 7:{p.graph=d.onnx.GraphProto.decode(r,r.uint32());break}case 14:{p.metadataProps&&p.metadataProps.length||(p.metadataProps=[]),p.metadataProps.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 20:{p.trainingInfo&&p.trainingInfo.length||(p.trainingInfo=[]),p.trainingInfo.push(d.onnx.TrainingInfoProto.decode(r,r.uint32()));break}case 25:{p.functions&&p.functions.length||(p.functions=[]),p.functions.push(d.onnx.FunctionProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.irVersion!=null&&r.hasOwnProperty("irVersion")&&!l.isInteger(r.irVersion)&&!(r.irVersion&&l.isInteger(r.irVersion.low)&&l.isInteger(r.irVersion.high)))return"irVersion: integer|Long expected";if(r.opsetImport!=null&&r.hasOwnProperty("opsetImport")){if(!Array.isArray(r.opsetImport))return"opsetImport: array expected";for(var o=0;o>>0,r.irVersion.high>>>0).toNumber())),r.opsetImport){if(!Array.isArray(r.opsetImport))throw TypeError(".onnx.ModelProto.opsetImport: array expected");o.opsetImport=[];for(var a=0;a>>0,r.modelVersion.high>>>0).toNumber())),r.docString!=null&&(o.docString=String(r.docString)),r.graph!=null){if(typeof r.graph!="object")throw TypeError(".onnx.ModelProto.graph: object expected");o.graph=d.onnx.GraphProto.fromObject(r.graph)}if(r.metadataProps){if(!Array.isArray(r.metadataProps))throw TypeError(".onnx.ModelProto.metadataProps: array expected");o.metadataProps=[];for(var a=0;a>>0,r.irVersion.high>>>0).toNumber():r.irVersion),r.producerName!=null&&r.hasOwnProperty("producerName")&&(a.producerName=r.producerName),r.producerVersion!=null&&r.hasOwnProperty("producerVersion")&&(a.producerVersion=r.producerVersion),r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.modelVersion!=null&&r.hasOwnProperty("modelVersion")&&(typeof r.modelVersion=="number"?a.modelVersion=o.longs===String?String(r.modelVersion):r.modelVersion:a.modelVersion=o.longs===String?l.Long.prototype.toString.call(r.modelVersion):o.longs===Number?new l.LongBits(r.modelVersion.low>>>0,r.modelVersion.high>>>0).toNumber():r.modelVersion),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.graph!=null&&r.hasOwnProperty("graph")&&(a.graph=d.onnx.GraphProto.toObject(r.graph,o)),r.opsetImport&&r.opsetImport.length){a.opsetImport=[];for(var h=0;h>>3){case 1:{p.key=r.string();break}case 2:{p.value=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.key!=null&&r.hasOwnProperty("key")&&!l.isString(r.key)?"key: string expected":r.value!=null&&r.hasOwnProperty("value")&&!l.isString(r.value)?"value: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.StringStringEntryProto)return r;var o=new d.onnx.StringStringEntryProto;return r.key!=null&&(o.key=String(r.key)),r.value!=null&&(o.value=String(r.value)),o},s.toObject=function(r,o){o||(o={});var a={};return o.defaults&&(a.key="",a.value=""),r.key!=null&&r.hasOwnProperty("key")&&(a.key=r.key),r.value!=null&&r.hasOwnProperty("value")&&(a.value=r.value),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.StringStringEntryProto"},s})(),c.TensorAnnotation=(function(){function s(r){if(this.quantParameterTensorNames=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.tensorName=r.string();break}case 2:{p.quantParameterTensorNames&&p.quantParameterTensorNames.length||(p.quantParameterTensorNames=[]),p.quantParameterTensorNames.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.tensorName!=null&&r.hasOwnProperty("tensorName")&&!l.isString(r.tensorName))return"tensorName: string expected";if(r.quantParameterTensorNames!=null&&r.hasOwnProperty("quantParameterTensorNames")){if(!Array.isArray(r.quantParameterTensorNames))return"quantParameterTensorNames: array expected";for(var o=0;o>>3){case 1:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 2:{p.name=r.string();break}case 5:{p.initializer&&p.initializer.length||(p.initializer=[]),p.initializer.push(d.onnx.TensorProto.decode(r,r.uint32()));break}case 15:{p.sparseInitializer&&p.sparseInitializer.length||(p.sparseInitializer=[]),p.sparseInitializer.push(d.onnx.SparseTensorProto.decode(r,r.uint32()));break}case 10:{p.docString=r.string();break}case 11:{p.input&&p.input.length||(p.input=[]),p.input.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 12:{p.output&&p.output.length||(p.output=[]),p.output.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 13:{p.valueInfo&&p.valueInfo.length||(p.valueInfo=[]),p.valueInfo.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 14:{p.quantizationAnnotation&&p.quantizationAnnotation.length||(p.quantizationAnnotation=[]),p.quantizationAnnotation.push(d.onnx.TensorAnnotation.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.node!=null&&r.hasOwnProperty("node")){if(!Array.isArray(r.node))return"node: array expected";for(var o=0;o>>3){case 1:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}if(r.dataType!=null&&(o.dataType=r.dataType|0),r.segment!=null){if(typeof r.segment!="object")throw TypeError(".onnx.TensorProto.segment: object expected");o.segment=d.onnx.TensorProto.Segment.fromObject(r.segment)}if(r.floatData){if(!Array.isArray(r.floatData))throw TypeError(".onnx.TensorProto.floatData: array expected");o.floatData=[];for(var a=0;a=0&&(o.stringData[a]=r.stringData[a])}if(r.int64Data){if(!Array.isArray(r.int64Data))throw TypeError(".onnx.TensorProto.int64Data: array expected");o.int64Data=[];for(var a=0;a>>0,r.int64Data[a].high>>>0).toNumber())}if(r.name!=null&&(o.name=String(r.name)),r.docString!=null&&(o.docString=String(r.docString)),r.rawData!=null&&(typeof r.rawData=="string"?l.base64.decode(r.rawData,o.rawData=l.newBuffer(l.base64.length(r.rawData)),0):r.rawData.length>=0&&(o.rawData=r.rawData)),r.externalData){if(!Array.isArray(r.externalData))throw TypeError(".onnx.TensorProto.externalData: array expected");o.externalData=[];for(var a=0;a>>0,r.uint64Data[a].high>>>0).toNumber(!0))}return o},s.toObject=function(r,o){o||(o={});var a={};if((o.arrays||o.defaults)&&(a.dims=[],a.floatData=[],a.int32Data=[],a.stringData=[],a.int64Data=[],a.doubleData=[],a.uint64Data=[],a.externalData=[]),o.defaults&&(a.dataType=0,a.segment=null,a.name="",o.bytes===String?a.rawData="":(a.rawData=[],o.bytes!==Array&&(a.rawData=l.newBuffer(a.rawData))),a.docString="",a.dataLocation=o.enums===String?"DEFAULT":0),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}if(r.dataType!=null&&r.hasOwnProperty("dataType")&&(a.dataType=r.dataType),r.segment!=null&&r.hasOwnProperty("segment")&&(a.segment=d.onnx.TensorProto.Segment.toObject(r.segment,o)),r.floatData&&r.floatData.length){a.floatData=[];for(var p=0;p>>0,r.int64Data[p].high>>>0).toNumber():r.int64Data[p]}if(r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.rawData!=null&&r.hasOwnProperty("rawData")&&(a.rawData=o.bytes===String?l.base64.encode(r.rawData,0,r.rawData.length):o.bytes===Array?Array.prototype.slice.call(r.rawData):r.rawData),r.doubleData&&r.doubleData.length){a.doubleData=[];for(var p=0;p>>0,r.uint64Data[p].high>>>0).toNumber(!0):r.uint64Data[p]}if(r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.externalData&&r.externalData.length){a.externalData=[];for(var p=0;p>>3){case 1:{h.begin=o.int64();break}case 2:{h.end=o.int64();break}default:o.skipType(m&7);break}}return h},r.decodeDelimited=function(o){return o instanceof i||(o=new i(o)),this.decode(o,o.uint32())},r.verify=function(o){return typeof o!="object"||o===null?"object expected":o.begin!=null&&o.hasOwnProperty("begin")&&!l.isInteger(o.begin)&&!(o.begin&&l.isInteger(o.begin.low)&&l.isInteger(o.begin.high))?"begin: integer|Long expected":o.end!=null&&o.hasOwnProperty("end")&&!l.isInteger(o.end)&&!(o.end&&l.isInteger(o.end.low)&&l.isInteger(o.end.high))?"end: integer|Long expected":null},r.fromObject=function(o){if(o instanceof d.onnx.TensorProto.Segment)return o;var a=new d.onnx.TensorProto.Segment;return o.begin!=null&&(l.Long?(a.begin=l.Long.fromValue(o.begin)).unsigned=!1:typeof o.begin=="string"?a.begin=parseInt(o.begin,10):typeof o.begin=="number"?a.begin=o.begin:typeof o.begin=="object"&&(a.begin=new l.LongBits(o.begin.low>>>0,o.begin.high>>>0).toNumber())),o.end!=null&&(l.Long?(a.end=l.Long.fromValue(o.end)).unsigned=!1:typeof o.end=="string"?a.end=parseInt(o.end,10):typeof o.end=="number"?a.end=o.end:typeof o.end=="object"&&(a.end=new l.LongBits(o.end.low>>>0,o.end.high>>>0).toNumber())),a},r.toObject=function(o,a){a||(a={});var p={};if(a.defaults){if(l.Long){var h=new l.Long(0,0,!1);p.begin=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.begin=a.longs===String?"0":0;if(l.Long){var h=new l.Long(0,0,!1);p.end=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.end=a.longs===String?"0":0}return o.begin!=null&&o.hasOwnProperty("begin")&&(typeof o.begin=="number"?p.begin=a.longs===String?String(o.begin):o.begin:p.begin=a.longs===String?l.Long.prototype.toString.call(o.begin):a.longs===Number?new l.LongBits(o.begin.low>>>0,o.begin.high>>>0).toNumber():o.begin),o.end!=null&&o.hasOwnProperty("end")&&(typeof o.end=="number"?p.end=a.longs===String?String(o.end):o.end:p.end=a.longs===String?l.Long.prototype.toString.call(o.end):a.longs===Number?new l.LongBits(o.end.low>>>0,o.end.high>>>0).toNumber():o.end),p},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(o){return o===void 0&&(o="type.googleapis.com"),o+"/onnx.TensorProto.Segment"},r})(),s.DataLocation=(function(){var r={},o=Object.create(r);return o[r[0]="DEFAULT"]=0,o[r[1]="EXTERNAL"]=1,o})(),s})(),c.SparseTensorProto=(function(){function s(r){if(this.dims=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.values=d.onnx.TensorProto.decode(r,r.uint32());break}case 2:{p.indices=d.onnx.TensorProto.decode(r,r.uint32());break}case 3:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}return o},s.toObject=function(r,o){o||(o={});var a={};if((o.arrays||o.defaults)&&(a.dims=[]),o.defaults&&(a.values=null,a.indices=null),r.values!=null&&r.hasOwnProperty("values")&&(a.values=d.onnx.TensorProto.toObject(r.values,o)),r.indices!=null&&r.hasOwnProperty("indices")&&(a.indices=d.onnx.TensorProto.toObject(r.indices,o)),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}return a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.SparseTensorProto"},s})(),c.TensorShapeProto=(function(){function s(r){if(this.dim=[],r)for(var o=Object.keys(r),a=0;a>>3===1?(p.dim&&p.dim.length||(p.dim=[]),p.dim.push(d.onnx.TensorShapeProto.Dimension.decode(r,r.uint32()))):r.skipType(h&7)}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.dim!=null&&r.hasOwnProperty("dim")){if(!Array.isArray(r.dim))return"dim: array expected";for(var o=0;o>>3){case 1:{m.dimValue=a.int64();break}case 2:{m.dimParam=a.string();break}case 3:{m.denotation=a.string();break}default:a.skipType(g&7);break}}return m},r.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},r.verify=function(a){if(typeof a!="object"||a===null)return"object expected";var p={};if(a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(p.value=1,!l.isInteger(a.dimValue)&&!(a.dimValue&&l.isInteger(a.dimValue.low)&&l.isInteger(a.dimValue.high))))return"dimValue: integer|Long expected";if(a.dimParam!=null&&a.hasOwnProperty("dimParam")){if(p.value===1)return"value: multiple values";if(p.value=1,!l.isString(a.dimParam))return"dimParam: string expected"}return a.denotation!=null&&a.hasOwnProperty("denotation")&&!l.isString(a.denotation)?"denotation: string expected":null},r.fromObject=function(a){if(a instanceof d.onnx.TensorShapeProto.Dimension)return a;var p=new d.onnx.TensorShapeProto.Dimension;return a.dimValue!=null&&(l.Long?(p.dimValue=l.Long.fromValue(a.dimValue)).unsigned=!1:typeof a.dimValue=="string"?p.dimValue=parseInt(a.dimValue,10):typeof a.dimValue=="number"?p.dimValue=a.dimValue:typeof a.dimValue=="object"&&(p.dimValue=new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber())),a.dimParam!=null&&(p.dimParam=String(a.dimParam)),a.denotation!=null&&(p.denotation=String(a.denotation)),p},r.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.denotation=""),a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(typeof a.dimValue=="number"?h.dimValue=p.longs===String?String(a.dimValue):a.dimValue:h.dimValue=p.longs===String?l.Long.prototype.toString.call(a.dimValue):p.longs===Number?new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber():a.dimValue,p.oneofs&&(h.value="dimValue")),a.dimParam!=null&&a.hasOwnProperty("dimParam")&&(h.dimParam=a.dimParam,p.oneofs&&(h.value="dimParam")),a.denotation!=null&&a.hasOwnProperty("denotation")&&(h.denotation=a.denotation),h},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TensorShapeProto.Dimension"},r})(),s})(),c.TypeProto=(function(){function s(o){if(o)for(var a=Object.keys(o),p=0;p>>3){case 1:{h.tensorType=d.onnx.TypeProto.Tensor.decode(o,o.uint32());break}case 4:{h.sequenceType=d.onnx.TypeProto.Sequence.decode(o,o.uint32());break}case 5:{h.mapType=d.onnx.TypeProto.Map.decode(o,o.uint32());break}case 9:{h.optionalType=d.onnx.TypeProto.Optional.decode(o,o.uint32());break}case 8:{h.sparseTensorType=d.onnx.TypeProto.SparseTensor.decode(o,o.uint32());break}case 6:{h.denotation=o.string();break}default:o.skipType(m&7);break}}return h},s.decodeDelimited=function(o){return o instanceof i||(o=new i(o)),this.decode(o,o.uint32())},s.verify=function(o){if(typeof o!="object"||o===null)return"object expected";var a={};if(o.tensorType!=null&&o.hasOwnProperty("tensorType")){a.value=1;{var p=d.onnx.TypeProto.Tensor.verify(o.tensorType);if(p)return"tensorType."+p}}if(o.sequenceType!=null&&o.hasOwnProperty("sequenceType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Sequence.verify(o.sequenceType);if(p)return"sequenceType."+p}}if(o.mapType!=null&&o.hasOwnProperty("mapType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Map.verify(o.mapType);if(p)return"mapType."+p}}if(o.optionalType!=null&&o.hasOwnProperty("optionalType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Optional.verify(o.optionalType);if(p)return"optionalType."+p}}if(o.sparseTensorType!=null&&o.hasOwnProperty("sparseTensorType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.SparseTensor.verify(o.sparseTensorType);if(p)return"sparseTensorType."+p}}return o.denotation!=null&&o.hasOwnProperty("denotation")&&!l.isString(o.denotation)?"denotation: string expected":null},s.fromObject=function(o){if(o instanceof d.onnx.TypeProto)return o;var a=new d.onnx.TypeProto;if(o.tensorType!=null){if(typeof o.tensorType!="object")throw TypeError(".onnx.TypeProto.tensorType: object expected");a.tensorType=d.onnx.TypeProto.Tensor.fromObject(o.tensorType)}if(o.sequenceType!=null){if(typeof o.sequenceType!="object")throw TypeError(".onnx.TypeProto.sequenceType: object expected");a.sequenceType=d.onnx.TypeProto.Sequence.fromObject(o.sequenceType)}if(o.mapType!=null){if(typeof o.mapType!="object")throw TypeError(".onnx.TypeProto.mapType: object expected");a.mapType=d.onnx.TypeProto.Map.fromObject(o.mapType)}if(o.optionalType!=null){if(typeof o.optionalType!="object")throw TypeError(".onnx.TypeProto.optionalType: object expected");a.optionalType=d.onnx.TypeProto.Optional.fromObject(o.optionalType)}if(o.sparseTensorType!=null){if(typeof o.sparseTensorType!="object")throw TypeError(".onnx.TypeProto.sparseTensorType: object expected");a.sparseTensorType=d.onnx.TypeProto.SparseTensor.fromObject(o.sparseTensorType)}return o.denotation!=null&&(a.denotation=String(o.denotation)),a},s.toObject=function(o,a){a||(a={});var p={};return a.defaults&&(p.denotation=""),o.tensorType!=null&&o.hasOwnProperty("tensorType")&&(p.tensorType=d.onnx.TypeProto.Tensor.toObject(o.tensorType,a),a.oneofs&&(p.value="tensorType")),o.sequenceType!=null&&o.hasOwnProperty("sequenceType")&&(p.sequenceType=d.onnx.TypeProto.Sequence.toObject(o.sequenceType,a),a.oneofs&&(p.value="sequenceType")),o.mapType!=null&&o.hasOwnProperty("mapType")&&(p.mapType=d.onnx.TypeProto.Map.toObject(o.mapType,a),a.oneofs&&(p.value="mapType")),o.denotation!=null&&o.hasOwnProperty("denotation")&&(p.denotation=o.denotation),o.sparseTensorType!=null&&o.hasOwnProperty("sparseTensorType")&&(p.sparseTensorType=d.onnx.TypeProto.SparseTensor.toObject(o.sparseTensorType,a),a.oneofs&&(p.value="sparseTensorType")),o.optionalType!=null&&o.hasOwnProperty("optionalType")&&(p.optionalType=d.onnx.TypeProto.Optional.toObject(o.optionalType,a),a.oneofs&&(p.value="optionalType")),p},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(o){return o===void 0&&(o="type.googleapis.com"),o+"/onnx.TypeProto"},s.Tensor=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(g&7);break}}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Tensor)return a;var p=new d.onnx.TypeProto.Tensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.Tensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Tensor"},o})(),s.Sequence=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(g&7)}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Sequence)return a;var p=new d.onnx.TypeProto.Sequence;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Sequence.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Sequence"},o})(),s.Map=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.keyType=a.int32();break}case 2:{m.valueType=d.onnx.TypeProto.decode(a,a.uint32());break}default:a.skipType(g&7);break}}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.keyType!=null&&a.hasOwnProperty("keyType")&&!l.isInteger(a.keyType))return"keyType: integer expected";if(a.valueType!=null&&a.hasOwnProperty("valueType")){var p=d.onnx.TypeProto.verify(a.valueType);if(p)return"valueType."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Map)return a;var p=new d.onnx.TypeProto.Map;if(a.keyType!=null&&(p.keyType=a.keyType|0),a.valueType!=null){if(typeof a.valueType!="object")throw TypeError(".onnx.TypeProto.Map.valueType: object expected");p.valueType=d.onnx.TypeProto.fromObject(a.valueType)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.keyType=0,h.valueType=null),a.keyType!=null&&a.hasOwnProperty("keyType")&&(h.keyType=a.keyType),a.valueType!=null&&a.hasOwnProperty("valueType")&&(h.valueType=d.onnx.TypeProto.toObject(a.valueType,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Map"},o})(),s.Optional=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(g&7)}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Optional)return a;var p=new d.onnx.TypeProto.Optional;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Optional.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Optional"},o})(),s.SparseTensor=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(g&7);break}}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.SparseTensor)return a;var p=new d.onnx.TypeProto.SparseTensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.SparseTensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.SparseTensor"},o})(),s})(),c.OperatorSetIdProto=(function(){function s(r){if(r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.domain=r.string();break}case 2:{p.version=r.int64();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.domain!=null&&r.hasOwnProperty("domain")&&!l.isString(r.domain)?"domain: string expected":r.version!=null&&r.hasOwnProperty("version")&&!l.isInteger(r.version)&&!(r.version&&l.isInteger(r.version.low)&&l.isInteger(r.version.high))?"version: integer|Long expected":null},s.fromObject=function(r){if(r instanceof d.onnx.OperatorSetIdProto)return r;var o=new d.onnx.OperatorSetIdProto;return r.domain!=null&&(o.domain=String(r.domain)),r.version!=null&&(l.Long?(o.version=l.Long.fromValue(r.version)).unsigned=!1:typeof r.version=="string"?o.version=parseInt(r.version,10):typeof r.version=="number"?o.version=r.version:typeof r.version=="object"&&(o.version=new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber())),o},s.toObject=function(r,o){o||(o={});var a={};if(o.defaults)if(a.domain="",l.Long){var p=new l.Long(0,0,!1);a.version=o.longs===String?p.toString():o.longs===Number?p.toNumber():p}else a.version=o.longs===String?"0":0;return r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.version!=null&&r.hasOwnProperty("version")&&(typeof r.version=="number"?a.version=o.longs===String?String(r.version):r.version:a.version=o.longs===String?l.Long.prototype.toString.call(r.version):o.longs===Number?new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber():r.version),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.OperatorSetIdProto"},s})(),c.OperatorStatus=(function(){var s={},r=Object.create(s);return r[s[0]="EXPERIMENTAL"]=0,r[s[1]="STABLE"]=1,r})(),c.FunctionProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],this.attributeProto=[],this.node=[],this.opsetImport=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 4:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 5:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 6:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(r.string());break}case 11:{p.attributeProto&&p.attributeProto.length||(p.attributeProto=[]),p.attributeProto.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 7:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 8:{p.docString=r.string();break}case 9:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 10:{p.domain=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var o=0;o{"use strict";k0(),Le=be(Ni()),Ln(),_i=class{static arraysEqual(e,t){if(e.length!==t.length)return!1;for(let n=0;n1&&o>1)return;c[d-s]=Math.max(r,o)}return c}static index(t,n){let i=new Array(n.length);return vi.fillIndex(t,n,i),i}static fillIndex(t,n,i){let u=t.length-n.length;for(let l=0;l=0;y--)r[y]=_%d[y],_=Math.floor(_/d[y]);m||(vi.fillIndex(r,t.dims,o),p=t.get(o)),g||(vi.fillIndex(r,n.dims,a),h=n.get(a)),s.set(r,i(p,h))}}return s}}static isValidBroadcast(t,n){let i=t.length,u=n.length;if(i>u)return!1;for(let l=1;l<=i;l++)if(t[i-l]!==1&&t[i-l]!==n[u-l])return!1;return!0}static getBroadcastDims(t,n){let i=t.length,u=[];for(let l=0;l1&&c===1&&u.unshift(d)}return u}},_w=class{static getShapeOfGemmResult(e,t,n,i,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(i?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!Qt.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},wt=class ol{static tensorDataTypeFromProto(t){switch(t){case Le.onnx.TensorProto.DataType.INT8:return"int8";case Le.onnx.TensorProto.DataType.UINT8:return"uint8";case Le.onnx.TensorProto.DataType.BOOL:return"bool";case Le.onnx.TensorProto.DataType.INT16:return"int16";case Le.onnx.TensorProto.DataType.UINT16:return"uint16";case Le.onnx.TensorProto.DataType.INT32:return"int32";case Le.onnx.TensorProto.DataType.UINT32:return"uint32";case Le.onnx.TensorProto.DataType.FLOAT:return"float32";case Le.onnx.TensorProto.DataType.DOUBLE:return"float64";case Le.onnx.TensorProto.DataType.STRING:return"string";case Le.onnx.TensorProto.DataType.INT64:return"int32";case Le.onnx.TensorProto.DataType.UINT64:return"uint32";default:throw new Error(`unsupported data type: ${Le.onnx.TensorProto.DataType[t]}`)}}static tensorDataTypeStringToEnum(t){switch(t){case"int8":return Le.onnx.TensorProto.DataType.INT8;case"uint8":return Le.onnx.TensorProto.DataType.UINT8;case"bool":return Le.onnx.TensorProto.DataType.BOOL;case"int16":return Le.onnx.TensorProto.DataType.INT16;case"uint16":return Le.onnx.TensorProto.DataType.UINT16;case"int32":return Le.onnx.TensorProto.DataType.INT32;case"uint32":return Le.onnx.TensorProto.DataType.UINT32;case"float32":return Le.onnx.TensorProto.DataType.FLOAT;case"float64":return Le.onnx.TensorProto.DataType.DOUBLE;case"string":return Le.onnx.TensorProto.DataType.STRING;case"int64":return Le.onnx.TensorProto.DataType.INT64;case"uint64":return Le.onnx.TensorProto.DataType.UINT64;default:throw new Error(`unsupported data type: ${t}`)}}static tensorDimsFromProto(t){return t.map(n=>mn.isLong(n)?n.toNumber():n)}static tensorValueTypeFromProto(t){return{tensorType:ol.tensorDataTypeFromProto(t.elemType),shape:{dims:ol.tensorDimsFromProto(t.shape.dim.map(n=>n.dimValue))}}}static tensorDimsFromORTFormat(t){let n=[];for(let i=0;it.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return Sr.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return Sr.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,i){let u=1;for(let l=n;l=0;--u)i[u]=i[u+1]*t[u+1];return i}static transpose(t){return t.slice().reverse()}static indicesToOffset(t,n,i){i===void 0&&(i=t.length);let u=0;for(let l=0;l=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(i=>this.normalizeAxis(i,n))}static incrementIndex(t,n,i){if(n.length===0||t.length===0)throw new Error("Index incrementing unsupported for scalar Tensor");if(i===void 0)i=n.length;else if(i<=0||i>n.length)throw new Error("Incorrect axis to increment on");for(let u=i-1;u>=0&&(t[u]++,!(t[u]=t.length)throw new Error("the dimension with value zero exceeds the dimension size of the input tensor");u[s]=t[s]}else u[s]=n[s];d*=u[s]}}let c=Sr.size(t);if(l!==-1){if(c%d!==0)throw new Error(`the input tensor cannot be reshaped to the requested shape. Input shape: [${t}] Output shape: [${n}]`);u[l]=c/d}else if(d!==c)throw new Error("reshapedDims and originalDims don't have matching sizes");return u}static sortBasedOnPerm(t,n){return n?n.map(i=>t[i]):t.slice().reverse()}static padShape(t,n){let i=t.length;return t.map((u,l)=>u+n[l]+n[l+i])}static areEqual(t,n){return t.length!==n.length?!1:t.every((i,u)=>i===n[u])}static validateDimsAndCalcSize(t){if(t.length>6)throw new TypeError("Only rank 0 to 6 is supported for tensor shape.");let n=1;for(let i of t){if(!Number.isInteger(i))throw new TypeError(`Invalid shape: ${i} is not an integer`);if(i<0||i>2147483647)throw new TypeError(`Invalid shape: length ${i} is not allowed`);n*=i}return n}static flattenShape(t,n){n<0&&(n+=t.length);let i=t.reduce((l,d)=>l*d,1),u=t.slice(n).reduce((l,d)=>l*d,1);return[i/u,u]}static squeezeShape(t,n){let i=new Array;n=Sr.normalizeAxes(n,t.length);for(let u=0;u=0;if(l&&t[u]!==1)throw new Error("squeeze an axis of size different than 1");(n.length===0&&t[u]>1||n.length>0&&!l)&&i.push(t[u])}return i}static unsqueezeShape(t,n){let i=new Array(t.length+n.length);i.fill(0);for(let l=0;l=i.length)throw new Error("'axes' has an out of range axis");if(i[d]!==0)throw new Error("'axes' has a duplicate axis");i[d]=1}let u=0;for(let l=0;l=i.length?i.push(n[c+2]):i[c]=n[c+2];for(let c=0;c=i[c]||d[c+i.length]>=i[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,i,u,l,d){if(d){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let c=0;c=i+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,i,u,l,d,c,s,r=0){let o=i*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,Ir.computeOutputSize(t-o,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(i!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],Ir.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return Ir.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}},Ci=-34028234663852886e22,Ri=34028234663852886e22});function DO(e){switch(e){case"bool":case"int8":case"uint8":return 1;case"int16":case"uint16":return 2;case"int32":case"uint32":case"float32":return 4;case"float64":return 8;default:throw new Error(`cannot calculate sizeof() on type ${e}`)}}function ch(e){switch(e){case ve.onnx.TensorProto.DataType.UINT8:case ve.onnx.TensorProto.DataType.INT8:case ve.onnx.TensorProto.DataType.BOOL:return 1;case ve.onnx.TensorProto.DataType.UINT16:case ve.onnx.TensorProto.DataType.INT16:return 2;case ve.onnx.TensorProto.DataType.FLOAT:case ve.onnx.TensorProto.DataType.INT32:case ve.onnx.TensorProto.DataType.UINT32:return 4;case ve.onnx.TensorProto.DataType.INT64:case ve.onnx.TensorProto.DataType.DOUBLE:case ve.onnx.TensorProto.DataType.UINT64:return 8;default:throw new Error(`cannot calculate sizeof() on type ${ve.onnx.TensorProto.DataType[e]}`)}}function MO(e,t){return new(xw(t))(e)}function xw(e){switch(e){case"bool":case"uint8":return Uint8Array;case"int8":return Int8Array;case"int16":return Int16Array;case"uint16":return Uint16Array;case"int32":return Int32Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"float32":return Float32Array;case"float64":return Float64Array;default:throw new Error("unspecified error")}}function sl(e,t){if(t===ve.onnx.TensorProto.DataType.INT64||t===No.TensorDataType.INT64){if(e.greaterThanOrEqual(2147483648)||e.lessThan(-2147483648))throw new TypeError("int64 is not supported")}else if(t===ve.onnx.TensorProto.DataType.UINT32||t===No.TensorDataType.UINT32||t===ve.onnx.TensorProto.DataType.UINT64||t===No.TensorDataType.UINT64){if(e.greaterThanOrEqual(4294967296)||e.lessThan(0))throw new TypeError("uint64 is not supported")}else throw new TypeError(`not a LONG type: ${ve.onnx.TensorProto.DataType[t]}`);return e.toNumber()}function ph(e,t,n){switch(t){case ve.onnx.TensorProto.DataType.BOOL:case ve.onnx.TensorProto.DataType.UINT8:return e.getUint8(n);case ve.onnx.TensorProto.DataType.INT8:return e.getInt8(n);case ve.onnx.TensorProto.DataType.UINT16:return e.getUint16(n,!0);case ve.onnx.TensorProto.DataType.INT16:return e.getInt16(n,!0);case ve.onnx.TensorProto.DataType.FLOAT:return e.getFloat32(n,!0);case ve.onnx.TensorProto.DataType.INT32:return e.getInt32(n,!0);case ve.onnx.TensorProto.DataType.UINT32:return e.getUint32(n,!0);case ve.onnx.TensorProto.DataType.INT64:return sl(mn.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!1),t);case ve.onnx.TensorProto.DataType.DOUBLE:return e.getFloat64(n,!0);case ve.onnx.TensorProto.DataType.UINT64:return sl(mn.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!0),t);default:throw new Error(`cannot read from DataView for type ${ve.onnx.TensorProto.DataType[t]}`)}}var hh,ve,xt,Ln=D(()=>{"use strict";hh=be(B3()),k0(),ns(),ve=be(Ni()),Me(),xt=class zo{constructor(t,n,i,u,l,d=hh.Guid.create()){this.dims=t,this.type=n,this.dataProvider=i,this.asyncDataProvider=u,this.cache=l,this.dataId=d,this.size=ye.validateDimsAndCalcSize(t);let c=this.size,s=i===void 0&&u===void 0&&l===void 0;if(l!==void 0&&l.length!==c)throw new RangeError("Input dims doesn't match data length.");if(n==="string"){if(l!==void 0&&(!Array.isArray(l)||!l.every(r=>typeof r=="string")))throw new TypeError("cache should be a string array");s&&(this.cache=new Array(c))}else{if(l!==void 0){let r=xw(n);if(!(l instanceof r))throw new TypeError(`cache should be type ${r.name}`)}if(s){let r=new ArrayBuffer(c*DO(n));this.cache=MO(r,n)}}}get data(){if(this.cache===void 0){let t=this.dataProvider(this.dataId);if(t.length!==this.size)throw new Error("Length of data provided by the Data Provider is inconsistent with the dims of this Tensor.");this.cache=t}return this.cache}get stringData(){if(this.type!=="string")throw new TypeError("data type is not string");return this.data}get integerData(){switch(this.type){case"uint8":case"int8":case"uint16":case"int16":case"int32":case"uint32":case"bool":return this.data;default:throw new TypeError("data type is not integer (uint8, int8, uint16, int16, int32, uint32, bool)")}}get floatData(){switch(this.type){case"float32":case"float64":return this.data;default:throw new TypeError("data type is not float (float32, float64)")}}get numberData(){if(this.type!=="string")return this.data;throw new TypeError("type cannot be non-number (string)")}get(t){return this.data[ye.indicesToOffset(t,this.strides)]}set(t,n){this.data[ye.indicesToOffset(t,this.strides)]=n}async getData(){return this.cache===void 0&&(this.cache=await this.asyncDataProvider(this.dataId)),this.cache}get strides(){return this._strides||(this._strides=ye.computeStrides(this.dims)),this._strides}static fromProto(t){if(!t)throw new Error("cannot construct Value from an empty tensor");let n=wt.tensorDataTypeFromProto(t.dataType),i=wt.tensorDimsFromProto(t.dims),u=new zo(i,n);if(n==="string")t.stringData.forEach((l,d)=>{u.data[d]=nl(l)});else if(t.rawData&&typeof t.rawData.byteLength=="number"&&t.rawData.byteLength>0){let l=u.data,d=new DataView(t.rawData.buffer,t.rawData.byteOffset,t.rawData.byteLength),c=ch(t.dataType),s=t.rawData.byteLength/c;if(t.rawData.byteLength%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;r0){let l=u.data,d=new DataView(t.rawDataArray().buffer,t.rawDataArray().byteOffset,t.rawDataLength()),c=ch(t.dataType()),s=t.rawDataLength()/c;if(t.rawDataLength()%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;rtypeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( { gl_Position = vec4(position, 1.0); TexCoords = textureCoord; - }`}function C3(e){let t=Te(e);return`${t.version} + }`}function zO(e){let t=Ae(e);return`${t.version} precision highp float; precision highp int; precision highp sampler2D; @@ -41,14 +41,14 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( return x - y * (x / y); } - `}function D3(e,t){let n=Te(e);return` + `}function BO(e,t){let n=Ae(e);return` void main() { int indices[${t}]; toVec(TexCoords, indices); vec4 result = vec4(process(indices)); ${n.output} = result; } - `}var j0,U0,Ge=D(()=>{"use strict";j0={version:"",attribute:"attribute",varyingVertex:"varying",varyingFrag:"varying",texture2D:"texture2D",output:"gl_FragColor",outputDeclaration:""},U0={version:"#version 300 es",attribute:"in",varyingVertex:"out",varyingFrag:"in",texture2D:"texture",output:"outputColor",outputDeclaration:"out vec4 outputColor;"}}),Ie=D(()=>{"use strict"});async function Cp(e,t=o=>0,n){return new Promise((o,u)=>{let l=0,d=()=>{if(e()){o();return}l++;let c=t(l);if(n!=null&&l>=n){u();return}setTimeout(d,c)};d()})}function Vs(e){return vo(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)}function R3(e){return vo(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)+"AtOutCoords"}function jn(e,t){let n=JSON.parse(JSON.stringify(e));return n=t,n}function Un(e,t){return t.map(n=>e[n]).join(", ")}function br(e){if(e<=1)return"int";if(e===2)return"ivec2";if(e===3)return"ivec3";if(e===4)return"ivec4";if(e===5)return"ivec5";if(e===6)return"ivec6";throw Error(`GPU for rank ${e} is not yet supported`)}function vn(e=6){return["x","y","z","w","u","v"].slice(0,e)}var Zt=D(()=>{"use strict";Ce()});function N3(e,t){return vn(t).map(n=>`${e}.${n}`)}function vl(e,t){return t===1?[e]:N3(e,t)}function wi(){return` + `}var $w,Tw,Ke=D(()=>{"use strict";$w={version:"",attribute:"attribute",varyingVertex:"varying",varyingFrag:"varying",texture2D:"texture2D",output:"gl_FragColor",outputDeclaration:""},Tw={version:"#version 300 es",attribute:"in",varyingVertex:"out",varyingFrag:"in",texture2D:"texture",output:"outputColor",outputDeclaration:"out vec4 outputColor;"}}),Pe=D(()=>{"use strict"});async function fh(e,t=i=>0,n){return new Promise((i,u)=>{let l=0,d=()=>{if(e()){i();return}l++;let c=t(l);if(n!=null&&l>=n){u();return}setTimeout(d,c)};d()})}function ga(e){return jo(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)}function FO(e){return jo(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)+"AtOutCoords"}function ii(e,t){let n=JSON.parse(JSON.stringify(e));return n=t,n}function oi(e,t){return t.map(n=>e[n]).join(", ")}function Ar(e){if(e<=1)return"int";if(e===2)return"ivec2";if(e===3)return"ivec3";if(e===4)return"ivec4";if(e===5)return"ivec5";if(e===6)return"ivec6";throw Error(`GPU for rank ${e} is not yet supported`)}function zn(e=6){return["x","y","z","w","u","v"].slice(0,e)}var ar=D(()=>{"use strict";Me()});function LO(e,t){return zn(t).map(n=>`${e}.${n}`)}function Kl(e,t){return t===1?[e]:LO(e,t)}function zi(){return` float getChannel(vec4 frag, int dim) { int modCoord = imod(dim, 2); return modCoord == 0 ? frag.r : frag.g; @@ -60,23 +60,23 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( (modCoord.y == 0. ? frag.r : frag.g) : (modCoord.y == 0. ? frag.b : frag.a); } - `}var Sn=D(()=>{"use strict";Zt()});function z3(e,t,n){if(e===0)return"false";if(e===1)return`rc > ${t[0]}`;let o="";for(let u=e-2;u= ${t[u-e+2]}`,u{"use strict";ar()});function jO(e,t,n){if(e===0)return"false";if(e===1)return`rc > ${t[0]}`;let i="";for(let u=e-2;u= ${t[u-e+2]}`,u= ${e[0]} ? 0. : getA(rc + 1), - 0, 0`;let o="r, c",u="r, cp1",l="rp1, c",d="rp1, cp1",c="";if(n>2)for(let s=0;s2)for(let s=0;s= ${o}; + bool rEdge = rp1 >= ${i}; bool cEdge = cp1 >= ${n}; - `}var qs,Dp,V0,F3=D(()=>{"use strict";Ge(),Ie(),Zt(),Sn(),qs={name:"pack",inputNames:["A"],inputTypes:[1]},Dp=(e,t)=>{let n=Te(e.session.backend.glContext.version),o=t.dims,u=o.length,l=t.dims.length,d=br(l),c=vl("rc",l),s=B3(l,c,o[o.length-2],o[o.length-1]),r;u===0?r=[1,1]:u===1?r=[o[0],1]:r=[o[l-1],o[l-2]];let i=z3(l,r,c),a=M3(o,c),p=` + `}var ba,mh,Sw,qO=D(()=>{"use strict";Ke(),Pe(),ar(),jn(),ba={name:"pack",inputNames:["A"],inputTypes:[1]},mh=(e,t)=>{let n=Ae(e.session.backend.glContext.version),i=t.dims,u=i.length,l=t.dims.length,d=Ar(l),c=Kl("rc",l),s=VO(l,c,i[i.length-2],i[i.length-1]),r;u===0?r=[1,1]:u===1?r=[i[0],1]:r=[i[l-1],i[l-2]];let o=jO(l,r,c),a=UO(i,c),p=` void main() { ${d} rc = getOutputCoords(); - if(${i}) { + if(${o}) { ${n.output} = vec4(0); } else { ${s} @@ -84,18 +84,18 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( ${n.output} = vec4(${a}); } } - `;return{...qs,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:2},shaderSource:p}},V0=(e,t)=>({...qs,get:()=>Dp(e,t)})});function Rp(e){if(e.length===0)return[1,1,1];let t=1;for(let n=0;n1?e[e.length-2]:1,e[e.length-1]]}function L3(e,t){let n=!1;return e.length===0||t.length===0?n=!0:e.length<2||t.length<2?n=e[e.length-1]===t[t.length-1]:n=e[e.length-1]===t[t.length-1]&&e[e.length-2]===t[t.length-2],n}function j3(e){let t=me.computeStrides(e),n=["b","r","c"],o="index";return` + `;return{...ba,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:2},shaderSource:p}},Sw=(e,t)=>({...ba,get:()=>mh(e,t)})});function gh(e){if(e.length===0)return[1,1,1];let t=1;for(let n=0;n1?e[e.length-2]:1,e[e.length-1]]}function GO(e,t){let n=!1;return e.length===0||t.length===0?n=!0:e.length<2||t.length<2?n=e[e.length-1]===t[t.length-1]:n=e[e.length-1]===t[t.length-1]&&e[e.length-2]===t[t.length-2],n}function HO(e){let t=ye.computeStrides(e),n=["b","r","c"],i="index";return` ivec3 inputCoordsFromReshapedOutCoords(int index) { - ${t.map((u,l)=>{let d=`int ${n[l]} = ${o} / ${u}`,c=l===t.length-1?`int ${n[l+1]} = ${o} - ${n[l]} * ${u}`:`index -= ${n[l]} * ${u}`;return`${d}; ${c};`}).join("")} + ${t.map((u,l)=>{let d=`int ${n[l]} = ${i} / ${u}`,c=l===t.length-1?`int ${n[l+1]} = ${i} - ${n[l]} * ${u}`:`index -= ${n[l]} * ${u}`;return`${d}; ${c};`}).join("")} return ivec3(b, r, c); } - `}function U3(e){let t=me.computeStrides(e);return` + `}function WO(e){let t=ye.computeStrides(e);return` int getFlattenedIndex(ivec3 coords) { // reverse y, z order return coords.x * ${t[0]} + coords.z * ${t[1]} + coords.y; } -`}var Np,zp,q0,V3=D(()=>{"use strict";Ce(),Ge(),Ie(),Sn(),Np=e=>({name:"Reshape (packed)",inputTypes:[2],inputNames:["A"],cacheHint:`${e}`}),zp=(e,t,n,o)=>{let u=t.dims,l=o,d="";for(let r=0;r<4;r++){let i="";switch(r){case 0:i="outputCoords = rc;";break;case 1:i="outputCoords = ivec3(rc.x, rc.y+1, rc.z);";break;case 2:i="outputCoords = ivec3(rc.x, rc.y, rc.z+1);";break;case 3:i="outputCoords = ivec3(rc.x, rc.y+1, rc.z+1);";break;default:throw new Error}d+=` - ${i} +`}var bh,yh,Iw,KO=D(()=>{"use strict";Me(),Ke(),Pe(),jn(),bh=e=>({name:"Reshape (packed)",inputTypes:[2],inputNames:["A"],cacheHint:`${e}`}),yh=(e,t,n,i)=>{let u=t.dims,l=i,d="";for(let r=0;r<4;r++){let o="";switch(r){case 0:o="outputCoords = rc;";break;case 1:o="outputCoords = ivec3(rc.x, rc.y+1, rc.z);";break;case 2:o="outputCoords = ivec3(rc.x, rc.y, rc.z+1);";break;case 3:o="outputCoords = ivec3(rc.x, rc.y+1, rc.z+1);";break;default:throw new Error}d+=` + ${o} ${r>0?"if(outputCoords.y < rows && outputCoords.z < cols){":""} int flattenedIndex = getFlattenedIndex(outputCoords); @@ -105,10 +105,10 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( result[${r}] = getChannel(getA(inputRC.x, inputRC.y, inputRC.z), innerDims); ${r>0?"}":""} - `}let c=Te(e.session.backend.glContext.version),s=` - ${j3(u)} - ${U3(l)} - ${wi()} + `}let c=Ae(e.session.backend.glContext.version),s=` + ${HO(u)} + ${WO(l)} + ${zi()} void main() { ivec3 rc = getOutputCoords(); @@ -122,7 +122,7 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( ${d} ${c.output} = result; } - `;return{...n,output:{dims:l,type:t.type,textureType:2},shaderSource:s,hasMain:!0}},q0=(e,t,n)=>{let o=Np(n);return{...o,get:()=>zp(e,t,o,n)}}}),Ru,q3=D(()=>{"use strict";Ge(),Ie(),Ru=(e,t)=>{let n=t.shape,o=Te(e.session.backend.glContext.version),u=` + `;return{...n,output:{dims:l,type:t.type,textureType:2},shaderSource:s,hasMain:!0}},Iw=(e,t,n)=>{let i=bh(n);return{...i,get:()=>yh(e,t,i,n)}}}),al,XO=D(()=>{"use strict";Ke(),Pe(),al=(e,t)=>{let n=t.shape,i=Ae(e.session.backend.glContext.version),u=` const float FLOAT_MAX = 1.70141184e38; const float FLOAT_MIN = 1.17549435e-38; @@ -167,9 +167,9 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( } void main() { - float value = ${o.texture2D}(X,TexCoords).r; - ${o.output} = encodeAsUint8(value); - }`,l={name:"Uint8Encode",inputTypes:[0],inputNames:["X"],output:{dims:n,type:t.tensor.type,textureType:3},shaderSource:u,hasMain:!0};return e.executeProgram(l,[t.tensor])}});function G3(e,t){if(e===1)return"rc";let n="";for(let o=0;o{"use strict";Ge(),Ie(),Zt(),Sn(),Gs={name:"unpack",inputNames:["A"],inputTypes:[2]},Mp=(e,t)=>{let n=t.dims.length,o=vl("rc",n),u=o.slice(-2),l=br(n),d=wi(),c=t.dims.length===0?"":G3(n,o),s=n<=1?"rc":`vec2(${u.join(",")})`,r=Te(e.session.backend.glContext.version),i=` + float value = ${i.texture2D}(X,TexCoords).r; + ${i.output} = encodeAsUint8(value); + }`,l={name:"Uint8Encode",inputTypes:[0],inputNames:["X"],output:{dims:n,type:t.tensor.type,textureType:3},shaderSource:u,hasMain:!0};return e.executeProgram(l,[t.tensor])}});function ZO(e,t){if(e===1)return"rc";let n="";for(let i=0;i{"use strict";Ke(),Pe(),ar(),jn(),ya={name:"unpack",inputNames:["A"],inputTypes:[2]},wh=(e,t)=>{let n=t.dims.length,i=Kl("rc",n),u=i.slice(-2),l=Ar(n),d=zi(),c=t.dims.length===0?"":ZO(n,i),s=n<=1?"rc":`vec2(${u.join(",")})`,r=Ae(e.session.backend.glContext.version),o=` ${d} void main() { ${l} rc = getOutputCoords(); @@ -179,51 +179,51 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( ${r.output} = vec4(getChannel(packedInput, ${s}), 0, 0, 0); } - `;return{...Gs,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:i}},G0=(e,t)=>({...Gs,get:()=>Mp(e,t)})}),H0,Nu,W0,$o=D(()=>{"use strict";Rt(),H0=class{constructor(e,t=1){if(t===1)this.internalFormat=e.R32F,this.format=e.RED,this.textureType=e.FLOAT,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA32F,this.format=e.RGBA,this.textureType=e.FLOAT,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){let n,o;return e.constructor!==Float32Array&&(We.warning("Encoder","data was not of type Float32; creating new Float32Array"),o=new Float32Array(e)),t*this.channelSize>e.length?(We.warning("Encoder","Source data too small. Allocating larger array"),o=e,n=this.allocate(t*this.channelSize),o.forEach((u,l)=>n[l]=u)):(o=e,n=o),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,o)=>o%4===0).subarray(0,t):e.subarray(0,t)}},Nu=class{constructor(e,t=1,n){if(t!==1&&t!==4)throw new Error(`Invalid number of channels: ${t}`);this.internalFormat=e.RGBA,this.format=e.RGBA,this.channelSize=t,this.textureType=n||e.FLOAT}encode(e,t){let n=e;return this.channelSize===1&&(We.verbose("Encoder","Exploding into a larger array"),n=this.allocate(t),e.forEach((o,u)=>n[u*4]=o)),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,o)=>o%4===0).subarray(0,t):e.subarray(0,t)}},W0=class{constructor(e,t=1){if(this.channelSize=4,t===1)this.internalFormat=e.ALPHA,this.format=e.ALPHA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA,this.format=e.RGBA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}allocate(e){return new Uint8Array(e*this.channelSize)}decode(e,t){if(e instanceof Uint8Array)return e.subarray(0,t);throw new Error(`Invalid array type: ${e.constructor}`)}}}),ci,K0,zu,W3=D(()=>{"use strict";Ce(),Ie(),ci=(e,t,n)=>{let o=n===0||n===1?1:4,u=n===2,l=n===1||n===2,d=n===4?t.length-1:void 0,c=n===4?t.map((s,r)=>r===t.length-1?s*4:s):void 0;return zu(e,t,o,c,{isPacked:u,reverseWH:l,breakAxis:d})},K0=(e,t,n)=>{let o=ci(e,t,n);return[o.width,o.height]},zu=(e,t,n=1,o,u)=>{let l=!!(u&&u.isPacked),[d,c]=e.computeTextureWH(l&&o||t,u),s=t.length,r=t.slice(0);if(s===0&&(r=[1]),n===1)o=t;else if(l){if(n!==4)throw new Error("a packed texture must be 4-channel");o=t,s>0&&(r[s-1]=Math.ceil(r[s-1]/2)),s>1&&(r[s-2]=Math.ceil(r[s-2]/2))}else if(!o)throw new Error("Unpacked shape is needed when using channels > 1");return{width:d,height:c,channels:n,isPacked:l,shape:r,strides:me.computeStrides(r),unpackedShape:o,reversedWH:u&&u.reverseWH}}}),Bp,X0,K3=D(()=>{"use strict";Rt(),Tn(),Ce(),F3(),V3(),q3(),H3(),$o(),W3(),Ie(),Bp=(e,t)=>{let n=t.map(u=>`${u.unpackedShape.join(",")};${u.width}x${u.height}`).join("_"),o=e.name;return e.cacheHint&&(o+="["+e.cacheHint+"]"),o+=":"+n,o},X0=class{constructor(e){this.session=e,this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map}calculateTextureWidthAndHeight(e,t){return K0(this.session.layoutStrategy,e,t)}executeProgram(e,t){if(t.lengththis.readTexture(l),async d=>this.readTextureAsync(l),void 0,u),texture:n};return this.setTextureData(l.tensor.dataId,l,e.isPacked),l}getTextureData(e,t=!1){return this.session.isInitializer(e)?this.session.getTextureData(e,t):t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){this.session.isInitializer(e)?this.session.setTextureData(e,t,n):(n?this.packedTextureDataCache:this.unpackedTextureDataCache).set(e,t)}isTextureLayoutCached(e,t=!1){return!!this.getTextureData(e.dataId,t)}dispose(){this.session.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.unpackedTextureDataCache=new Map}readTexture(e){return e.isPacked?this.readTexture(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTexture(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(Ru(this,e))}async readTextureAsync(e){return e.isPacked?this.readTextureAsync(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTextureAsync(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(Ru(this,e))}pack(e){return this.executeProgram(V0(this,e.tensor),[e.tensor])}unpack(e){return this.executeProgram(G0(this,e.tensor),[e.tensor])}}}),Fp,Be,rt=D(()=>{"use strict";Fp=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},Be=e=>new Fp(e)}),Hs,Z0,Y0,Lp,jp,X3=D(()=>{"use strict";rt(),Ge(),Ie(),Hs={name:"BatchNormalization",inputNames:["A","Scale","B","Mean","Variance"],inputTypes:[0,0,0,0,0]},Z0=(e,t,n)=>(jp(t),[e.run({...Hs,cacheHint:n.cacheKey,get:()=>Lp(e,t,n)},t)]),Y0=e=>{let t=e.attributes.getFloat("epsilon",1e-5),n=e.attributes.getFloat("momentum",.9),o=e.attributes.getInt("spatial",1);return Be({epsilon:t,momentum:n,spatial:o})},Lp=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),u=t[0].dims.length,[l,d]=e.calculateTextureWidthAndHeight(t[1].dims,0),c=` + `;return{...ya,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:o}},Ow=(e,t)=>({...ya,get:()=>wh(e,t)})}),Ew,ul,Aw,Vo=D(()=>{"use strict";qt(),Ew=class{constructor(e,t=1){if(t===1)this.internalFormat=e.R32F,this.format=e.RED,this.textureType=e.FLOAT,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA32F,this.format=e.RGBA,this.textureType=e.FLOAT,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){let n,i;return e.constructor!==Float32Array&&(Je.warning("Encoder","data was not of type Float32; creating new Float32Array"),i=new Float32Array(e)),t*this.channelSize>e.length?(Je.warning("Encoder","Source data too small. Allocating larger array"),i=e,n=this.allocate(t*this.channelSize),i.forEach((u,l)=>n[l]=u)):(i=e,n=i),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,i)=>i%4===0).subarray(0,t):e.subarray(0,t)}},ul=class{constructor(e,t=1,n){if(t!==1&&t!==4)throw new Error(`Invalid number of channels: ${t}`);this.internalFormat=e.RGBA,this.format=e.RGBA,this.channelSize=t,this.textureType=n||e.FLOAT}encode(e,t){let n=e;return this.channelSize===1&&(Je.verbose("Encoder","Exploding into a larger array"),n=this.allocate(t),e.forEach((i,u)=>n[u*4]=i)),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,i)=>i%4===0).subarray(0,t):e.subarray(0,t)}},Aw=class{constructor(e,t=1){if(this.channelSize=4,t===1)this.internalFormat=e.ALPHA,this.format=e.ALPHA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA,this.format=e.RGBA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}allocate(e){return new Uint8Array(e*this.channelSize)}decode(e,t){if(e instanceof Uint8Array)return e.subarray(0,t);throw new Error(`Invalid array type: ${e.constructor}`)}}}),Ei,kw,ll,JO=D(()=>{"use strict";Me(),Pe(),Ei=(e,t,n)=>{let i=n===0||n===1?1:4,u=n===2,l=n===1||n===2,d=n===4?t.length-1:void 0,c=n===4?t.map((s,r)=>r===t.length-1?s*4:s):void 0;return ll(e,t,i,c,{isPacked:u,reverseWH:l,breakAxis:d})},kw=(e,t,n)=>{let i=Ei(e,t,n);return[i.width,i.height]},ll=(e,t,n=1,i,u)=>{let l=!!(u&&u.isPacked),[d,c]=e.computeTextureWH(l&&i||t,u),s=t.length,r=t.slice(0);if(s===0&&(r=[1]),n===1)i=t;else if(l){if(n!==4)throw new Error("a packed texture must be 4-channel");i=t,s>0&&(r[s-1]=Math.ceil(r[s-1]/2)),s>1&&(r[s-2]=Math.ceil(r[s-2]/2))}else if(!i)throw new Error("Unpacked shape is needed when using channels > 1");return{width:d,height:c,channels:n,isPacked:l,shape:r,strides:ye.computeStrides(r),unpackedShape:i,reversedWH:u&&u.reverseWH}}}),_h,Pw,QO=D(()=>{"use strict";qt(),Ln(),Me(),qO(),KO(),XO(),YO(),Vo(),JO(),Pe(),_h=(e,t)=>{let n=t.map(u=>`${u.unpackedShape.join(",")};${u.width}x${u.height}`).join("_"),i=e.name;return e.cacheHint&&(i+="["+e.cacheHint+"]"),i+=":"+n,i},Pw=class{constructor(e){this.session=e,this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map}calculateTextureWidthAndHeight(e,t){return kw(this.session.layoutStrategy,e,t)}executeProgram(e,t){if(t.lengththis.readTexture(l),async d=>this.readTextureAsync(l),void 0,u),texture:n};return this.setTextureData(l.tensor.dataId,l,e.isPacked),l}getTextureData(e,t=!1){return this.session.isInitializer(e)?this.session.getTextureData(e,t):t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){this.session.isInitializer(e)?this.session.setTextureData(e,t,n):(n?this.packedTextureDataCache:this.unpackedTextureDataCache).set(e,t)}isTextureLayoutCached(e,t=!1){return!!this.getTextureData(e.dataId,t)}dispose(){this.session.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.unpackedTextureDataCache=new Map}readTexture(e){return e.isPacked?this.readTexture(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTexture(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(al(this,e))}async readTextureAsync(e){return e.isPacked?this.readTextureAsync(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTextureAsync(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(al(this,e))}pack(e){return this.executeProgram(Sw(this,e.tensor),[e.tensor])}unpack(e){return this.executeProgram(Ow(this,e.tensor),[e.tensor])}}}),vh,je,ut=D(()=>{"use strict";vh=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},je=e=>new vh(e)}),wa,Cw,Rw,xh,$h,eE=D(()=>{"use strict";ut(),Ke(),Pe(),wa={name:"BatchNormalization",inputNames:["A","Scale","B","Mean","Variance"],inputTypes:[0,0,0,0,0]},Cw=(e,t,n)=>($h(t),[e.run({...wa,cacheHint:n.cacheKey,get:()=>xh(e,t,n)},t)]),Rw=e=>{let t=e.attributes.getFloat("epsilon",1e-5),n=e.attributes.getFloat("momentum",.9),i=e.attributes.getInt("spatial",1);return je({epsilon:t,momentum:n,spatial:i})},xh=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),u=t[0].dims.length,[l,d]=e.calculateTextureWidthAndHeight(t[1].dims,0),c=` float process(int[${u}] indices) { vec2 position = offsetToCoords(indices[1], ${l}, ${d}); - float scale = getColorAsFloat(${o.texture2D}(Scale, position)); - float mean = getColorAsFloat(${o.texture2D}(Mean, position)); - float variance = getColorAsFloat(${o.texture2D}(Variance, position)); - float b = getColorAsFloat(${o.texture2D}(B, position)); + float scale = getColorAsFloat(${i.texture2D}(Scale, position)); + float mean = getColorAsFloat(${i.texture2D}(Mean, position)); + float variance = getColorAsFloat(${i.texture2D}(Variance, position)); + float b = getColorAsFloat(${i.texture2D}(B, position)); return scale * ( (_A(indices) - mean) / sqrt(variance + float(${n.epsilon})) ) + b; - }`;return{...Hs,output:{dims:t[0].dims,type:t[0].type,textureType:0},shaderSource:c}},jp=e=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs.");let t=e[0],n=e[1],o=e[2],u=e[3],l=e[4];if(t.dims.length<3||n.dims.length!==1||o.dims.length!==1||u.dims.length!==1||l.dims.length!==1)throw new Error("invalid input shape.");if(n.dims[0]!==t.dims[1]||o.dims[0]!==t.dims[1]||u.dims[0]!==t.dims[1]||l.dims[0]!==t.dims[1])throw new Error("invalid input shape.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||o.type!=="float32"&&o.type!=="float64"||u.type!=="float32"&&u.type!=="float64"||l.type!=="float32"&&l.type!=="float64")throw new Error("invalid input tensor types.")}}),J0,In,te,Mu,Q0,_r=D(()=>{"use strict";J0=class{constructor(e,t,n,o){this.glContext=e,this.programInfo=t,this.inputTextureLayouts=n,this.outputTextureLayout=o}},In=class{constructor(e){this.context=e}},te=class{constructor(e,t){this.routineBody=e,this.dependencies=t}},Mu=class{constructor(e,t,n){this.name=e,n?this.dependencies=n:this.dependencies=[],t&&(this.routineBody=t)}addDependency(e){e&&this.dependencies.push(e)}},Q0=class{static returnOrderedNodes(e){if(!e||e.length===0)return[];if(e.length===1)return e;let t=new Set,n=new Set,o=new Array;return this.createOrderedNodes(e,t,n,o),o}static createOrderedNodes(e,t,n,o){for(let u=0;u0)for(let l=0;l{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs.");let t=e[0],n=e[1],i=e[2],u=e[3],l=e[4];if(t.dims.length<3||n.dims.length!==1||i.dims.length!==1||u.dims.length!==1||l.dims.length!==1)throw new Error("invalid input shape.");if(n.dims[0]!==t.dims[1]||i.dims[0]!==t.dims[1]||u.dims[0]!==t.dims[1]||l.dims[0]!==t.dims[1])throw new Error("invalid input shape.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||i.type!=="float32"&&i.type!=="float64"||u.type!=="float32"&&u.type!=="float64"||l.type!=="float32"&&l.type!=="float64")throw new Error("invalid input tensor types.")}}),Dw,Un,ie,dl,Mw,Pr=D(()=>{"use strict";Dw=class{constructor(e,t,n,i){this.glContext=e,this.programInfo=t,this.inputTextureLayouts=n,this.outputTextureLayout=i}},Un=class{constructor(e){this.context=e}},ie=class{constructor(e,t){this.routineBody=e,this.dependencies=t}},dl=class{constructor(e,t,n){this.name=e,n?this.dependencies=n:this.dependencies=[],t&&(this.routineBody=t)}addDependency(e){e&&this.dependencies.push(e)}},Mw=class{static returnOrderedNodes(e){if(!e||e.length===0)return[];if(e.length===1)return e;let t=new Set,n=new Set,i=new Array;return this.createOrderedNodes(e,t,n,i),i}static createOrderedNodes(e,t,n,i){for(let u=0;u0)for(let l=0;l b); } @@ -233,7 +233,7 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( v1.b > v2.b, v1.a > v2.a ); } - `,name:e,type:0}}function rO(){let e="less_";return{body:` + `,name:e,type:0}}function aE(){let e="less_";return{body:` float ${e}(float a, float b) { return float(a < b); } @@ -243,7 +243,7 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( v1.b < v2.b, v1.a < v2.a ); } - `,name:e,type:0}}function nO(){let e="and_";return{body:` + `,name:e,type:0}}function uE(){let e="and_";return{body:` float ${e}(float a, float b) { return float( bool(a) && bool(b) ); } @@ -255,7 +255,7 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( b1.b && b2.b, b1.a && b2.a ); } - `,name:e,type:0}}function iO(){return{body:` + `,name:e,type:0}}function lE(){return{body:` float or_(float a, float b) { return float( bool(a) || bool(b) ); } @@ -267,7 +267,7 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( b1.b || b2.b, b1.a || b2.a ); } - `,name:"or_",type:0}}function oO(){let e="xor_";return{body:` + `,name:"or_",type:0}}function dE(){let e="xor_";return{body:` float ${e}(float a, float b) { return float( bool(a) ^^ bool(b) ); } @@ -279,7 +279,7 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( b1.b ^^ b2.b, b1.a ^^ b2.a ); } - `,name:e,type:0}}function sO(){return uO("pow")}function aO(){let e="prelu_";return{body:` + `,name:e,type:0}}function cE(){return hE("pow")}function pE(){let e="prelu_";return{body:` float ${e}(float a, float b) { return a < 0.0 ? a * b: a; } @@ -291,29 +291,29 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( v1.a < 0.0 ? v1.a * v2.a: v1.a ); } - `,name:e,type:0}}function uO(e){let t=`${e}_`;return{body:` + `,name:e,type:0}}function hE(e){let t=`${e}_`;return{body:` float ${t}(float a, float b) { return ${e}(a, b); } vec4 ${t}(vec4 v1, vec4 v2) { return ${e}(v1, v2); } - `,name:t,type:0}}var dt,Up,e_,t_,r_,n_,i_,o_,s_,a_,u_,l_,d_,c_,lO=D(()=>{"use strict";Ce(),_r(),Ge(),Ie(),dt=(e,t,n,o=t[0].type,u)=>{let l=e.session.pack?2:0;return{name:n.name,inputNames:["A","B"],inputTypes:[l,l],cacheHint:u,get:()=>Up(e,t,n,o)}},Up=(e,t,n,o=t[0].type)=>{let u=e.session.pack?2:0,l=!me.areEqual(t[0].dims,t[1].dims),d=t[0].dims,c=e.session.pack;if(l){let i=Ut.calcShape(t[0].dims,t[1].dims,!1);if(!i)throw new Error("Can't perform binary op on the given tensors");d=i;let a=d.length,p=t[0].dims.length!==0?t[0].dims.length:1,h=t[1].dims.length!==0?t[1].dims.length:1,m=t[0].dims.length!==0?"bcastIndices_A(indices, aindices);":"aindices[0] = 0;",b=t[1].dims.length!==0?"bcastIndices_B(indices, bindices);":"bindices[0] = 0;",$=Te(e.session.backend.glContext.version),_=c?` + `,name:t,type:0}}var bt,Th,Nw,zw,Bw,Fw,Lw,jw,Uw,Vw,qw,Gw,Hw,Ww,fE=D(()=>{"use strict";Me(),Pr(),Ke(),Pe(),bt=(e,t,n,i=t[0].type,u)=>{let l=e.session.pack?2:0;return{name:n.name,inputNames:["A","B"],inputTypes:[l,l],cacheHint:u,get:()=>Th(e,t,n,i)}},Th=(e,t,n,i=t[0].type)=>{let u=e.session.pack?2:0,l=!ye.areEqual(t[0].dims,t[1].dims),d=t[0].dims,c=e.session.pack;if(l){let o=Qt.calcShape(t[0].dims,t[1].dims,!1);if(!o)throw new Error("Can't perform binary op on the given tensors");d=o;let a=d.length,p=t[0].dims.length!==0?t[0].dims.length:1,h=t[1].dims.length!==0?t[1].dims.length:1,m=t[0].dims.length!==0?"bcastIndices_A(indices, aindices);":"aindices[0] = 0;",g=t[1].dims.length!==0?"bcastIndices_B(indices, bindices);":"bindices[0] = 0;",_=Ae(e.session.backend.glContext.version),w=c?` ${n.body} void main() { vec4 a = getAAtOutCoords(); vec4 b = getBAtOutCoords(); vec4 result = ${n.name}(a, b); - ${$.output} = result; + ${_.output} = result; }`:` ${n.body} float process(int indices[${a}]) { int aindices[${p}]; int bindices[${h}]; ${m} - ${b} + ${g} return ${n.name}(_A(aindices), _B(bindices)); - }`;return{name:n.name,inputNames:["A","B"],inputTypes:[u,u],output:{dims:d,type:o,textureType:u},shaderSource:_,hasMain:c}}let s=Te(e.session.backend.glContext.version),r=` + }`;return{name:n.name,inputNames:["A","B"],inputTypes:[u,u],output:{dims:d,type:i,textureType:u},shaderSource:w,hasMain:c}}let s=Ae(e.session.backend.glContext.version),r=` ${n.body} void main() { vec4 v1 = ${s.texture2D}(A, TexCoords); @@ -321,21 +321,21 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( vec4 result = ${n.name}(v1, v2); ${s.output} = result; } - `;return{name:n.name,inputNames:["A","B"],inputTypes:[u,u],output:{dims:t[0].dims,type:o,textureType:u},shaderSource:r,hasMain:!0}},e_=(e,t)=>[e.run(dt(e,t,Z3()),t)],t_=(e,t)=>[e.run(dt(e,t,nO(),"bool"),t)],r_=(e,t)=>[e.run(dt(e,t,Y3()),t)],n_=(e,t)=>[e.run(dt(e,t,eO(),"bool"),t)],i_=(e,t)=>[e.run(dt(e,t,tO(),"bool"),t)],o_=(e,t)=>[e.run(dt(e,t,rO(),"bool"),t)],s_=(e,t)=>[e.run(dt(e,t,J3()),t)],a_=(e,t)=>[e.run(dt(e,t,iO(),"bool"),t)],u_=(e,t)=>[e.run(dt(e,t,sO()),t)],l_=(e,t)=>[e.run(dt(e,t,aO()),t)],d_=(e,t)=>[e.run(dt(e,t,Q3()),t)],c_=(e,t)=>[e.run(dt(e,t,oO(),"bool"),t)]}),p_,h_,Vp,dO=D(()=>{"use strict";Ce(),p_=(e,t,n)=>(Vp(t),[e.cast(t[0],n)]),h_=e=>pt.tensorDataTypeFromProto(e.attributes.getInt("to")),Vp=e=>{if(!e||e.length!==1)throw new Error("Cast requires 1 input.");if(e[0].type==="string")throw new Error("Invalid input type.")}}),qp,Gp,f_,Vn,cO=D(()=>{"use strict";Ge(),Ie(),Zt(),Sn(),qp=(e,t)=>({name:"Concat (packed)",inputNames:Array.from({length:e},(n,o)=>`X${o}`),inputTypes:Array(e).fill(2),cacheHint:t}),Gp=(e,t,n,o)=>{let u=n[0].dims.slice();if(o>=u.length||o<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");o<0&&(o=u.length+o);let l=u.slice(0);for(let O=1;OO.dims),a=vn(d),p=new Array(i.length-1);p[0]=i[0][o];for(let O=1;O[e.run(bt(e,t,tE()),t)],zw=(e,t)=>[e.run(bt(e,t,uE(),"bool"),t)],Bw=(e,t)=>[e.run(bt(e,t,rE()),t)],Fw=(e,t)=>[e.run(bt(e,t,oE(),"bool"),t)],Lw=(e,t)=>[e.run(bt(e,t,sE(),"bool"),t)],jw=(e,t)=>[e.run(bt(e,t,aE(),"bool"),t)],Uw=(e,t)=>[e.run(bt(e,t,nE()),t)],Vw=(e,t)=>[e.run(bt(e,t,lE(),"bool"),t)],qw=(e,t)=>[e.run(bt(e,t,cE()),t)],Gw=(e,t)=>[e.run(bt(e,t,pE()),t)],Hw=(e,t)=>[e.run(bt(e,t,iE()),t)],Ww=(e,t)=>[e.run(bt(e,t,dE(),"bool"),t)]}),Kw,Xw,Sh,mE=D(()=>{"use strict";Me(),Kw=(e,t,n)=>(Sh(t),[e.cast(t[0],n)]),Xw=e=>wt.tensorDataTypeFromProto(e.attributes.getInt("to")),Sh=e=>{if(!e||e.length!==1)throw new Error("Cast requires 1 input.");if(e[0].type==="string")throw new Error("Invalid input type.")}}),Ih,Oh,Zw,si,gE=D(()=>{"use strict";Ke(),Pe(),ar(),jn(),Ih=(e,t)=>({name:"Concat (packed)",inputNames:Array.from({length:e},(n,i)=>`X${i}`),inputTypes:Array(e).fill(2),cacheHint:t}),Oh=(e,t,n,i)=>{let u=n[0].dims.slice();if(i>=u.length||i<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");i<0&&(i=u.length+i);let l=u.slice(0);for(let O=1;OO.dims),a=zn(d),p=new Array(o.length-1);p[0]=o[0][i];for(let O=1;O= ${p[O-1]}) { return getChannel( - getX${O}(${Vn(a,h,A)}), - vec2(${Vn(m,h,A)})); - }`}let _=p.length,y=p[p.length-1];$+=` + getX${O}(${si(a,h,A)}), + vec2(${si(m,h,A)})); + }`}let w=p.length,y=p[p.length-1];_+=` return getChannel( - getX${_}(${Vn(a,h,y)}), - vec2(${Vn(m,h,y)}));`;let T=Te(e.session.backend.glContext.version),S=` + getX${w}(${si(a,h,y)}), + vec2(${si(m,h,y)}));`;let T=Ae(e.session.backend.glContext.version),S=` ${r} float getValue(${a.map(O=>"int "+O)}) { - ${$} + ${_} } void main() { @@ -363,24 +363,24 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( } ${T.output} = result; } - `;return{...t,output:{dims:l,type:n[0].type,textureType:2},shaderSource:S,hasMain:!0}},f_=(e,t,n)=>{let o=qp(t.length,n.cacheKey);return{...o,get:()=>Gp(e,o,t,n.axis)}},Vn=(e,t,n)=>{let o=e.indexOf(t);return e.map((u,l)=>l===o?`${u} - ${n}`:u).join()}}),m_,Hp,Wp,Kp,Ws,Xp,Zp,Yp,g_,Jp,pO=D(()=>{"use strict";rt(),Ie(),cO(),m_=(e,t,n)=>(Jp(t),e.session.pack&&t[0].dims.length>1?[e.run(f_(e,t,n),t)]:[e.run(Kp(e,t,n),t)]),Hp=(e,t)=>({name:"Concat",inputNames:Array.from({length:e},(n,o)=>`X${o}`),inputTypes:Array(e).fill(0),cacheHint:t}),Wp=(e,t,n,o)=>{let u=n[0].dims.slice();if(o>=u.length||o<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");o<0&&(o=u.length+o);let l=u.slice(0);for(let h=1;h{let i=Ih(t.length,n.cacheKey);return{...i,get:()=>Oh(e,i,t,n.axis)}},si=(e,t,n)=>{let i=e.indexOf(t);return e.map((u,l)=>l===i?`${u} - ${n}`:u).join()}}),Yw,Eh,Ah,kh,_a,Ph,Ch,Rh,Jw,Dh,bE=D(()=>{"use strict";ut(),Pe(),gE(),Yw=(e,t,n)=>(Dh(t),e.session.pack&&t[0].dims.length>1?[e.run(Zw(e,t,n),t)]:[e.run(kh(e,t,n),t)]),Eh=(e,t)=>({name:"Concat",inputNames:Array.from({length:e},(n,i)=>`X${i}`),inputTypes:Array(e).fill(0),cacheHint:t}),Ah=(e,t,n,i)=>{let u=n[0].dims.slice();if(i>=u.length||i<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");i<0&&(i=u.length+i);let l=u.slice(0);for(let h=1;h{let o=Hp(t.length,n.cacheKey);return{...o,get:()=>Wp(e,o,t,n.axis)}},Ws=e=>`int getTextureWhereDataResides(int index) { + }`;return{...t,output:{dims:l,type:n[0].type,textureType:0},shaderSource:p}},kh=(e,t,n)=>{let i=Eh(t.length,n.cacheKey);return{...i,get:()=>Ah(e,i,t,n.axis)}},_a=e=>`int getTextureWhereDataResides(int index) { ${e.map((t,n)=>`if(index<${t}) {return ${n};} `).join("")} - }`,Xp=e=>Ws(e),Zp=(e,t)=>{let n=[`float fetchDataFromCorrectTexture(int textureIndex, int indices[${t}]) {`];for(let o=0;o{let t=["int getSizeInConcatAxisValueFromIndex(int index) {"];for(let n=0;nBe({axis:e.attributes.getInt("axis")}),Jp=e=>{if(!e||e.length<1)throw new Error("too few inputs");let t=e[0].type,n=e[0].dims.length;if(t==="string")throw new Error("string tensor is not supported yet");for(let o of e){if(o.type!==t)throw new Error("input tensors should be one type");if(o.dims.length!==n)throw new Error("input tensors should have the same shape")}}});function hO(){return Tt("abs")}function fO(){return Tt("acos")}function mO(){return Tt("asin")}function gO(){return Tt("atan")}function bO(){return Tt("ceil")}function yO(){return Tt("cos")}function _O(e){return{body:` + }`,Ph=e=>_a(e),Ch=(e,t)=>{let n=[`float fetchDataFromCorrectTexture(int textureIndex, int indices[${t}]) {`];for(let i=0;i{let t=["int getSizeInConcatAxisValueFromIndex(int index) {"];for(let n=0;nje({axis:e.attributes.getInt("axis")}),Dh=e=>{if(!e||e.length<1)throw new Error("too few inputs");let t=e[0].type,n=e[0].dims.length;if(t==="string")throw new Error("string tensor is not supported yet");for(let i of e){if(i.type!==t)throw new Error("input tensors should be one type");if(i.dims.length!==n)throw new Error("input tensors should have the same shape")}}});function yE(){return Dt("abs")}function wE(){return Dt("acos")}function _E(){return Dt("asin")}function vE(){return Dt("atan")}function xE(){return Dt("ceil")}function $E(){return Dt("cos")}function TE(e){return{body:` const float alpha = float(${e}); float elu_(float a) { @@ -389,7 +389,7 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( vec4 elu_(vec4 v) { return vec4(elu_(v.x), elu_(v.y), elu_(v.z), elu_(v.w)); } - `,name:"elu",type:0}}function wO(){return Tt("exp")}function vO(){return Tt("floor")}function b_(e,t){let n="clip";return{body:` + `,name:"elu",type:0}}function SE(){return Dt("exp")}function IE(){return Dt("floor")}function Qw(e,t){let n="clip";return{body:` const float min = float(${e}); const float max = float(${t}); @@ -399,14 +399,14 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( vec4 ${n}_(vec4 v) { return clamp(v, min, max); } - `,name:n,type:0}}function xO(){let e="indentity";return{body:` + `,name:n,type:0}}function OE(){let e="indentity";return{body:` float ${e}_(float a) { return a; } vec4 ${e}_(vec4 v) { return v; } - `,name:e,type:0}}function $O(e){let t="leakyRelu";return{body:` + `,name:e,type:0}}function EE(e){let t="leakyRelu";return{body:` const float alpha = float(${e}); float ${t}_(float a) { @@ -415,14 +415,14 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( vec4 ${t}_(vec4 v) { return vec4(${t}_(v.x), ${t}_(v.y), ${t}_(v.z), ${t}_(v.w)); } - `,name:t,type:0}}function TO(){return Tt("log")}function SO(){return{body:` + `,name:t,type:0}}function AE(){return Dt("log")}function kE(){return{body:` float neg_(float a) { return -a; } vec4 neg_(vec4 v) { return -v; } - `,name:"neg",type:0}}function IO(){return{body:` + `,name:"neg",type:0}}function PE(){return{body:` float not_(float a) { return float( ! bool(a) ); } @@ -435,21 +435,21 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( bvec4 not_(bvec4 v) { return bvec4(!v.x, !v.y, !v.z, !v.w); } - `,name:"not",type:0}}function OO(){return Tt("sin")}function y_(){let e="relu";return{body:` + `,name:"not",type:0}}function CE(){return Dt("sin")}function e_(){let e="relu";return{body:` float ${e}_(float a) { return max( a, 0.0 ); } vec4 ${e}_(vec4 v) { return max( v, 0.0 ); } - `,name:e,type:0}}function __(){let e="sigmoid";return{body:` + `,name:e,type:0}}function t_(){let e="sigmoid";return{body:` float ${e}_(float a) { return 1.0 / (1.0 + exp(-a)); } vec4 ${e}_(vec4 v) { return 1.0 / (1.0 + exp(-v)); } - `,name:e,type:0}}function EO(){return Tt("sqrt")}function AO(){return Tt("tan")}function kO(){let e="tanh";return{body:` + `,name:e,type:0}}function RE(){return Dt("sqrt")}function DE(){return Dt("tan")}function ME(){let e="tanh";return{body:` float ${e}_(float a) { a = clamp(a, -10., 10.); a = exp(2.*a); @@ -460,24 +460,24 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( v = exp(2.*v); return (v - 1.) / (v + 1.); } - `,name:e,type:0}}function Tt(e){return{body:` + `,name:e,type:0}}function Dt(e){return{body:` float ${e}_(float a) { return ${e}(a); } vec4 ${e}_(vec4 v) { return ${e}(v); } - `,name:e,type:0}}var Qp,He,w_,v_,x_,$_,Bu,T_,S_,eh,I_,O_,E_,A_,k_,P_,Fu,C_,D_,R_,N_,z_,M_,B_,F_,L_,j_,U_,V_=D(()=>{"use strict";rt(),Ce(),_r(),Ge(),Ie(),Qp=(e,t,n,o)=>{let u=e.session.pack?2:0,l=Te(e.session.backend.glContext.version);return{...t,output:{dims:n.dims,type:n.type,textureType:u},shaderSource:` - ${o.body} + `,name:e,type:0}}var Mh,Ye,r_,n_,i_,o_,cl,s_,a_,Nh,u_,l_,d_,c_,p_,h_,pl,f_,m_,g_,b_,y_,w_,__,v_,x_,$_,T_,S_=D(()=>{"use strict";ut(),Me(),Pr(),Ke(),Pe(),Mh=(e,t,n,i)=>{let u=e.session.pack?2:0,l=Ae(e.session.backend.glContext.version);return{...t,output:{dims:n.dims,type:n.type,textureType:u},shaderSource:` + ${i.body} void main() { vec4 v = ${l.texture2D}(A, TexCoords); - v = ${o.name}_(v); + v = ${i.name}_(v); ${l.output} = v; } - `,hasMain:!0}},He=(e,t,n,o)=>{let u=e.session.pack?2:0,l={name:n.name,inputTypes:[u],inputNames:["A"],cacheHint:o};return{...l,get:()=>Qp(e,l,t,n)}},w_=(e,t)=>[e.run(He(e,t[0],hO()),t)],v_=(e,t)=>[e.run(He(e,t[0],fO()),t)],x_=(e,t)=>[e.run(He(e,t[0],mO()),t)],$_=(e,t)=>[e.run(He(e,t[0],gO()),t)],Bu=(e,t,n)=>[e.run(He(e,t[0],b_(n.min,n.max),n.cacheKey),t)],T_=e=>Be({min:e.attributes.getFloat("min",mi),max:e.attributes.getFloat("max",gi)}),S_=(e,t)=>{let n=eh(e,t);return Bu(e,[t[0]],n)},eh=(e,t)=>{if(t.length>=3&&(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)))throw new Error("dynamic clip attributes are not allowed");let n=t.length>=3?t[1].numberData[0]:mi,o=t.length>=3?t[2].numberData[0]:gi;return Be({min:n,max:o})},I_=(e,t)=>[e.run(He(e,t[0],bO()),t)],O_=(e,t)=>[e.run(He(e,t[0],yO()),t)],E_=(e,t,n)=>[e.run(He(e,t[0],_O(n.alpha),n.cacheKey),t)],A_=e=>Be({alpha:e.attributes.getFloat("alpha",1)}),k_=(e,t)=>[e.run(He(e,t[0],wO()),t)],P_=(e,t)=>[e.run(He(e,t[0],vO()),t)],Fu=(e,t)=>[e.run(He(e,t[0],xO()),t)],C_=(e,t,n)=>[e.run(He(e,t[0],$O(n.alpha),n.cacheKey),t)],D_=e=>Be({alpha:e.attributes.getFloat("alpha",.01)}),R_=(e,t)=>[e.run(He(e,t[0],TO()),t)],N_=(e,t)=>[e.run(He(e,t[0],SO()),t)],z_=(e,t)=>[e.run(He(e,t[0],IO()),t)],M_=(e,t)=>[e.run(He(e,t[0],y_()),t)],B_=(e,t)=>[e.run(He(e,t[0],__()),t)],F_=(e,t)=>[e.run(He(e,t[0],OO()),t)],L_=(e,t)=>[e.run(He(e,t[0],EO()),t)],j_=(e,t)=>[e.run(He(e,t[0],AO()),t)],U_=(e,t)=>[e.run(He(e,t[0],kO()),t)]});function vi(e){let t;switch(e.activation){case"Relu":t=y_();break;case"Sigmoid":t=__();break;case"Clip":t=b_(e.clipMin,e.clipMax);break;default:return{activationFunction:"",applyActivation:""}}let n=t.name,o=t.body,u=`value = ${n}_(value);`;return{activationFunction:o,applyActivation:u}}var Bo,On=D(()=>{"use strict";Ce(),V_(),Bo=e=>{let t=e.getString("activation","");if(t==="Clip"){let[n,o]=e.getFloats("activation_params",[mi,gi]);return{activation:t,clipMax:o,clipMin:n,activationCacheKey:`${t}:${n},${o}`}}return{activation:t,activationCacheKey:t}}}),th,rh,q_,PO=D(()=>{"use strict";Rt(),Ge(),Ie(),Tl(),On(),th=(e,t)=>({name:"GroupedConv",inputNames:e?["X","W","Bias"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),rh=(e,t,n,o)=>{let u=t.length>2?"value += getBias(output_channel);":"",l=t[0].dims.slice(),d=t[1].dims.slice(),c=d[0]/o.group;We.verbose("GroupedConv",`autpPad:${o.autoPad}, dilations:${o.dilations}, group:${o.group}, kernelShape:${o.kernelShape}, pads:${o.pads}, strides:${o.strides}`);let s=pi(l,d,o.dilations,o.pads,o.strides),r=Te(e.session.backend.glContext.version),{activationFunction:i,applyActivation:a}=vi(o),p=` - const ivec2 strides = ivec2(${o.strides[0]}, ${o.strides[1]}); - const ivec2 pads = ivec2(${o.pads[0]}, ${o.pads[1]}); - ${i} + `,hasMain:!0}},Ye=(e,t,n,i)=>{let u=e.session.pack?2:0,l={name:n.name,inputTypes:[u],inputNames:["A"],cacheHint:i};return{...l,get:()=>Mh(e,l,t,n)}},r_=(e,t)=>[e.run(Ye(e,t[0],yE()),t)],n_=(e,t)=>[e.run(Ye(e,t[0],wE()),t)],i_=(e,t)=>[e.run(Ye(e,t[0],_E()),t)],o_=(e,t)=>[e.run(Ye(e,t[0],vE()),t)],cl=(e,t,n)=>[e.run(Ye(e,t[0],Qw(n.min,n.max),n.cacheKey),t)],s_=e=>je({min:e.attributes.getFloat("min",Ci),max:e.attributes.getFloat("max",Ri)}),a_=(e,t)=>{let n=Nh(e,t);return cl(e,[t[0]],n)},Nh=(e,t)=>{if(t.length>=3&&(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)))throw new Error("dynamic clip attributes are not allowed");let n=t.length>=3?t[1].numberData[0]:Ci,i=t.length>=3?t[2].numberData[0]:Ri;return je({min:n,max:i})},u_=(e,t)=>[e.run(Ye(e,t[0],xE()),t)],l_=(e,t)=>[e.run(Ye(e,t[0],$E()),t)],d_=(e,t,n)=>[e.run(Ye(e,t[0],TE(n.alpha),n.cacheKey),t)],c_=e=>je({alpha:e.attributes.getFloat("alpha",1)}),p_=(e,t)=>[e.run(Ye(e,t[0],SE()),t)],h_=(e,t)=>[e.run(Ye(e,t[0],IE()),t)],pl=(e,t)=>[e.run(Ye(e,t[0],OE()),t)],f_=(e,t,n)=>[e.run(Ye(e,t[0],EE(n.alpha),n.cacheKey),t)],m_=e=>je({alpha:e.attributes.getFloat("alpha",.01)}),g_=(e,t)=>[e.run(Ye(e,t[0],AE()),t)],b_=(e,t)=>[e.run(Ye(e,t[0],kE()),t)],y_=(e,t)=>[e.run(Ye(e,t[0],PE()),t)],w_=(e,t)=>[e.run(Ye(e,t[0],e_()),t)],__=(e,t)=>[e.run(Ye(e,t[0],t_()),t)],v_=(e,t)=>[e.run(Ye(e,t[0],CE()),t)],x_=(e,t)=>[e.run(Ye(e,t[0],RE()),t)],$_=(e,t)=>[e.run(Ye(e,t[0],DE()),t)],T_=(e,t)=>[e.run(Ye(e,t[0],ME()),t)]});function Bi(e){let t;switch(e.activation){case"Relu":t=e_();break;case"Sigmoid":t=t_();break;case"Clip":t=Qw(e.clipMin,e.clipMax);break;default:return{activationFunction:"",applyActivation:""}}let n=t.name,i=t.body,u=`value = ${n}_(value);`;return{activationFunction:i,applyActivation:u}}var is,Vn=D(()=>{"use strict";Me(),S_(),is=e=>{let t=e.getString("activation","");if(t==="Clip"){let[n,i]=e.getFloats("activation_params",[Ci,Ri]);return{activation:t,clipMax:i,clipMin:n,activationCacheKey:`${t}:${n},${i}`}}return{activation:t,activationCacheKey:t}}}),zh,Bh,I_,NE=D(()=>{"use strict";qt(),Ke(),Pe(),Yl(),Vn(),zh=(e,t)=>({name:"GroupedConv",inputNames:e?["X","W","Bias"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),Bh=(e,t,n,i)=>{let u=t.length>2?"value += getBias(output_channel);":"",l=t[0].dims.slice(),d=t[1].dims.slice(),c=d[0]/i.group;Je.verbose("GroupedConv",`autpPad:${i.autoPad}, dilations:${i.dilations}, group:${i.group}, kernelShape:${i.kernelShape}, pads:${i.pads}, strides:${i.strides}`);let s=Ai(l,d,i.dilations,i.pads,i.strides),r=Ae(e.session.backend.glContext.version),{activationFunction:o,applyActivation:a}=Bi(i),p=` + const ivec2 strides = ivec2(${i.strides[0]}, ${i.strides[1]}); + const ivec2 pads = ivec2(${i.pads[0]}, ${i.pads[1]}); + ${o} void main() { ivec4 coords = getOutputCoords(); int batch = coords.x; @@ -489,14 +489,14 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( for (int wInChannel = 0; wInChannel < ${d[1]}; wInChannel++) { int input_channel = group_id * ${d[1]} + wInChannel; for (int wHeight = 0; wHeight < ${d[2]}; wHeight++) { - int xHeight = xRCCorner.x + wHeight * ${o.dilations[0]}; + int xHeight = xRCCorner.x + wHeight * ${i.dilations[0]}; if (xHeight < 0 || xHeight >= ${l[2]}) { continue; } for (int wWidth = 0; wWidth < ${d[3]}; wWidth++) { - int xWidth = xRCCorner.y + wWidth * ${o.dilations[1]}; + int xWidth = xRCCorner.y + wWidth * ${i.dilations[1]}; if (xWidth < 0 || xWidth >= ${l[3]}) { continue; } @@ -511,17 +511,17 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( ${a} ${r.output} = vec4(value, .0, .0, .0); } -`;return{...n,output:{dims:s,type:t[0].type,textureType:0},shaderSource:p,hasMain:!0}},q_=(e,t,n)=>{let o=th(t.length>2,n.cacheKey);return{...o,get:()=>rh(e,t,o,n)}}}),nh,ih,G_,CO=D(()=>{"use strict";Ge(),Ie(),Sn(),nh=e=>({name:"Im2Col (packed)",inputNames:["A"],inputTypes:[2],cacheHint:e}),ih=(e,t,n,o,u,l)=>{let d=n.dims,c=o.dims,s=2,r=3,i=u.length,a=[c[1]*c[2]*c[3],u[2]*u[3]],p=c[2]*c[3],h=wi(),m=Te(e.session.backend.glContext.version),b="";for(let _=0;_<=1;_++)for(let y=0;y<=1;y++)b+=` +`;return{...n,output:{dims:s,type:t[0].type,textureType:0},shaderSource:p,hasMain:!0}},I_=(e,t,n)=>{let i=zh(t.length>2,n.cacheKey);return{...i,get:()=>Bh(e,t,i,n)}}}),Fh,Lh,O_,zE=D(()=>{"use strict";Ke(),Pe(),jn(),Fh=e=>({name:"Im2Col (packed)",inputNames:["A"],inputTypes:[2],cacheHint:e}),Lh=(e,t,n,i,u,l)=>{let d=n.dims,c=i.dims,s=2,r=3,o=u.length,a=[c[1]*c[2]*c[3],u[2]*u[3]],p=c[2]*c[3],h=zi(),m=Ae(e.session.backend.glContext.version),g="";for(let w=0;w<=1;w++)for(let y=0;y<=1;y++)g+=` blockIndex = rc.x + ${y}; - pos = rc.y + ${_}; + pos = rc.y + ${w}; if(blockIndex < ${a[1]} && pos < ${a[0]}) { - offsetY = int(blockIndex / (${u[i-1]})) * ${l.strides[0]} - + offsetY = int(blockIndex / (${u[o-1]})) * ${l.strides[0]} - ${l.pads[0]}; d0 = offsetY + ${l.dilations[0]} * (imod(pos, ${p}) / ${c[2]}); if(d0 < ${d[s]} && d0 >= 0) { - offsetX = imod(blockIndex, ${u[i-1]}) * ${l.strides[1]} - + offsetX = imod(blockIndex, ${u[o-1]}) * ${l.strides[1]} - ${l.pads[1]}; d1 = offsetX + ${l.dilations[1]} * imod(imod(pos, ${p}), ${c[2]}); @@ -529,14 +529,14 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( ch = int(float(pos)/ ${p}.); innerDims = vec2(d0, d1); - result[${_*2+y}] = getChannel( + result[${w*2+y}] = getChannel( getA(0, ch, int(innerDims.x), int(innerDims.y)), innerDims); } } } - `;let $=` + `;let _=` ${h} void main() { @@ -544,29 +544,29 @@ var Ir=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>( vec4 result = vec4(0.0); int blockIndex, pos, offsetY, d0, offsetX, d1, ch; vec2 innerDims; - ${b} + ${g} ${m.output} = result; } - `;return{...t,output:{dims:a,type:n.type,textureType:2},shaderSource:$,hasMain:!0}},G_=(e,t,n,o,u)=>{let l=nh(u.cacheKey);return{...l,get:()=>ih(e,l,t,n,o,u)}}});function DO(e,t,n){let o=t[0].dims,u=t[1].dims,l=Ut.calcShape(o,u,!0);if(!l)throw new Error("Can't use matmul on the given tensors");let d=br(l.length),c=vn(),{activationFunction:s,applyActivation:r}=vi(n),i=t.length>2,a=i?"value += getBiasForMatmul();":"",p=i?`${W_(d,c,t[2].dims,l,!1)}`:"",h=l.length,m=o.length,b=u.length,$=o[o.length-1],_=` + `;return{...t,output:{dims:a,type:n.type,textureType:2},shaderSource:_,hasMain:!0}},O_=(e,t,n,i,u)=>{let l=Fh(u.cacheKey);return{...l,get:()=>Lh(e,l,t,n,i,u)}}});function BE(e,t,n){let i=t[0].dims,u=t[1].dims,l=Qt.calcShape(i,u,!0);if(!l)throw new Error("Can't use matmul on the given tensors");let d=Ar(l.length),c=zn(),{activationFunction:s,applyActivation:r}=Bi(n),o=t.length>2,a=o?"value += getBiasForMatmul();":"",p=o?`${A_(d,c,t[2].dims,l,!1)}`:"",h=l.length,m=i.length,g=u.length,_=i[i.length-1],w=` ${s} ${p} float process(int indices[${h}]) { int a[${m}]; - int b[${b}]; + int b[${g}]; bcastMatmulIndices_A(indices, a); bcastMatmulIndices_B(indices, b); float value; - for (int k=0; k<${$}; ++k) { + for (int k=0; k<${_}; ++k) { a[${m-1}] = k; - b[${b-2}] = k; + b[${g-2}] = k; value += _A(a) * _B(b); } ${a} ${r} return value; - }`;return{...e,output:{dims:l,type:t[0].type,textureType:0},shaderSource:_}}function H_(e,t){let n=Z_(e.length>2,t.activationCacheKey);return{...n,get:()=>DO(n,e,t)}}function W_(e,t,n,o,u){let l="",d=n.length,c=o.length,s=c-d;c<2&&d>0?l="coords":l=n.map((p,h)=>`coords.${t[h+s]}`).join(", ");let r=Ut.getBroadcastDims(n,o).map(p=>`coords.${t[p+s]} = 0;`).join(` -`),i=me.size(n)===1,a="vec4(outputValue.xx, outputValue.yy)";return i&&(a="vec4(outputValue.x)"),u?` + }`;return{...e,output:{dims:l,type:t[0].type,textureType:0},shaderSource:w}}function E_(e,t){let n=C_(e.length>2,t.activationCacheKey);return{...n,get:()=>BE(n,e,t)}}function A_(e,t,n,i,u){let l="",d=n.length,c=i.length,s=c-d;c<2&&d>0?l="coords":l=n.map((p,h)=>`coords.${t[h+s]}`).join(", ");let r=Qt.getBroadcastDims(n,i).map(p=>`coords.${t[p+s]} = 0;`).join(` +`),o=ye.size(n)===1,a="vec4(outputValue.xx, outputValue.yy)";return o&&(a="vec4(outputValue.x)"),u?` vec4 getBiasForMatmul() { ${e} coords = getOutputCoords(); ${r} @@ -577,29 +577,29 @@ float getBiasForMatmul() { ${e} coords = getOutputCoords(); ${r} return getBias(coords.x); -}`}var K_,X_,Z_,oh,xl=D(()=>{"use strict";Ce(),Ie(),Zt(),On(),Y_(),K_=(e,t,n)=>(oh(t),e.session.pack?[e.run($l(e,t,n),t)]:[e.run(H_(t,n),t)]),X_=e=>Bo(e.attributes),Z_=(e,t)=>({name:"MatMul",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),oh=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64")throw new Error("inputs should be float type");if(e[0].type!==e[1].type)throw new Error("inputs types should match")}});function RO(e,t,n,o){let u=[],l=[],d=n[0].dims,c=n[1].dims,s=d.length,r=c.length,i=o.length,a=i-s,p=i-r;u=d.map((y,T)=>`coords.${t[T+a]}`),u[s-1]="i*2",u.join(", "),l=c.map((y,T)=>`coords.${t[T+p]}`),l[r-2]="i*2",l.join(", ");let h=Ut.getBroadcastDims(d,o),m=Ut.getBroadcastDims(c,o),b=h.map(y=>`coords.${t[y+a]} = 0;`).join(` -`),$=m.map(y=>`coords.${t[y+p]} = 0;`).join(` -`),_=`int lastDim = coords.${t[i-1]}; - coords.${t[i-1]} = coords.${t[i-2]}; - coords.${t[i-2]} = lastDim;`;return` +}`}var k_,P_,C_,jh,Xl=D(()=>{"use strict";Me(),Pe(),ar(),Vn(),R_(),k_=(e,t,n)=>(jh(t),e.session.pack?[e.run(Zl(e,t,n),t)]:[e.run(E_(t,n),t)]),P_=e=>is(e.attributes),C_=(e,t)=>({name:"MatMul",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),jh=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64")throw new Error("inputs should be float type");if(e[0].type!==e[1].type)throw new Error("inputs types should match")}});function FE(e,t,n,i){let u=[],l=[],d=n[0].dims,c=n[1].dims,s=d.length,r=c.length,o=i.length,a=o-s,p=o-r;u=d.map((y,T)=>`coords.${t[T+a]}`),u[s-1]="i*2",u.join(", "),l=c.map((y,T)=>`coords.${t[T+p]}`),l[r-2]="i*2",l.join(", ");let h=Qt.getBroadcastDims(d,i),m=Qt.getBroadcastDims(c,i),g=h.map(y=>`coords.${t[y+a]} = 0;`).join(` +`),_=m.map(y=>`coords.${t[y+p]} = 0;`).join(` +`),w=`int lastDim = coords.${t[o-1]}; + coords.${t[o-1]} = coords.${t[o-2]}; + coords.${t[o-2]} = lastDim;`;return` vec4 getAAtOutCoordsMatmul(int i) { ${e} coords = getOutputCoords(); - ${_} - ${b} + ${w} + ${g} vec4 outputValue = getA(${u}); return outputValue; } vec4 getBAtOutCoordsMatmul(int i) { ${e} coords = getOutputCoords(); + ${w} ${_} - ${$} vec4 outputValue = getB(${l}); return outputValue; -}`}function NO(e,t){let n="";for(let o=0;o{"use strict";Ce(),Ge(),Ie(),Zt(),On(),xl(),sh=(e,t)=>({name:"MatMul (packed)",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[2,2,2]:[2,2],cacheHint:t}),ah=(e,t,n,o)=>{let u=n.length>2,l=u?"value += getBiasForMatmul();":"",d=n[0].dims,c=n[1].dims,s=Ut.calcShape(d,c,!0),r=!me.areEqual(n[0].dims,n[1].dims);if(!s)throw new Error("Can't use matmul on the given tensors");let i=d[d.length-1],a=Math.ceil(i/2),p=d.length,h=c.length,m=Te(e.session.backend.glContext.version),b=br(s.length),$=s.length,_=vn(),{activationFunction:y,applyActivation:T}=vi(o),S=u?`${W_(b,_,n[2].dims,s,!0)}`:"",O=r?`${RO(b,_,n,s)}`:"",A=r?"getAAtOutCoordsMatmul(i)":`getA(${NO(_,p)})`,P=r?"getBAtOutCoordsMatmul(i)":`getB(${zO(_,h)})`,I=r?"":`${b} rc = - getOutputCoords(); int lastDim = rc.${_[$-1]}; rc.${_[$-1]} = - rc.${_[$-2]}; rc.${_[$-2]} = lastDim; - `,L=` +}`}function LE(e,t){let n="";for(let i=0;i{"use strict";Me(),Ke(),Pe(),ar(),Vn(),Xl(),Uh=(e,t)=>({name:"MatMul (packed)",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[2,2,2]:[2,2],cacheHint:t}),Vh=(e,t,n,i)=>{let u=n.length>2,l=u?"value += getBiasForMatmul();":"",d=n[0].dims,c=n[1].dims,s=Qt.calcShape(d,c,!0),r=!ye.areEqual(n[0].dims,n[1].dims);if(!s)throw new Error("Can't use matmul on the given tensors");let o=d[d.length-1],a=Math.ceil(o/2),p=d.length,h=c.length,m=Ae(e.session.backend.glContext.version),g=Ar(s.length),_=s.length,w=zn(),{activationFunction:y,applyActivation:T}=Bi(i),S=u?`${A_(g,w,n[2].dims,s,!0)}`:"",O=r?`${FE(g,w,n,s)}`:"",A=r?"getAAtOutCoordsMatmul(i)":`getA(${LE(w,p)})`,P=r?"getBAtOutCoordsMatmul(i)":`getB(${jE(w,h)})`,I=r?"":`${g} rc = + getOutputCoords(); int lastDim = rc.${w[_-1]}; rc.${w[_-1]} = + rc.${w[_-2]}; rc.${w[_-2]} = lastDim; + `,z=` ${O} ${S} ${y} @@ -617,7 +617,7 @@ vec4 getBAtOutCoordsMatmul(int i) { ${l} ${T} ${m.output} = value; - }`;return{...t,output:{dims:s,type:n[0].type,textureType:2},shaderSource:L,hasMain:!0}},$l=(e,t,n)=>{let o=sh(t.length>2,n.activationCacheKey);return{...o,get:()=>ah(e,o,t,n)}}}),J_,MO=D(()=>{"use strict";Tl(),CO(),Y_(),J_=(e,t,n)=>{let o=t[0].dims,u=t[1].dims,l=pi(o,u,n.dilations,n.pads,n.strides),d=e.run(G_(e,t[0],t[1],l,n),[t[0]]),c=e.reshapePacked(t[1],[u[0],u[1]*u[2]*u[3]]),s=t.length===3?[c,d,t[2]]:[c,d],r=e.run($l(e,s,n),s);return e.reshapePacked(r,l)}}),uh,lh,Q_,Lu,ew=D(()=>{"use strict";Ie(),uh=e=>({name:"Im2Col",inputNames:["X"],inputTypes:[0],cacheHint:e}),lh=(e,t,n,o,u,l)=>{let d=n.dims,c=o.dims,s=u.length,r=Lu(d,c,u,4),i=` + }`;return{...t,output:{dims:s,type:n[0].type,textureType:2},shaderSource:z,hasMain:!0}},Zl=(e,t,n)=>{let i=Uh(t.length>2,n.activationCacheKey);return{...i,get:()=>Vh(e,i,t,n)}}}),D_,UE=D(()=>{"use strict";Yl(),zE(),R_(),D_=(e,t,n)=>{let i=t[0].dims,u=t[1].dims,l=Ai(i,u,n.dilations,n.pads,n.strides),d=e.run(O_(e,t[0],t[1],l,n),[t[0]]),c=e.reshapePacked(t[1],[u[0],u[1]*u[2]*u[3]]),s=t.length===3?[c,d,t[2]]:[c,d],r=e.run(Zl(e,s,n),s);return e.reshapePacked(r,l)}}),qh,Gh,M_,hl,N_=D(()=>{"use strict";Pe(),qh=e=>({name:"Im2Col",inputNames:["X"],inputTypes:[0],cacheHint:e}),Gh=(e,t,n,i,u,l)=>{let d=n.dims,c=i.dims,s=u.length,r=hl(d,c,u,4),o=` const int XC = ${d[1]}; const int XH = ${d[2]}; const int XW = ${d[3]}; @@ -661,8 +661,8 @@ vec4 getBAtOutCoordsMatmul(int i) { } return value; } - `;return{...t,output:{dims:r,type:n.type,textureType:4},shaderSource:i}},Q_=(e,t,n,o,u)=>{let l=uh(u.cacheKey);return{...l,get:()=>lh(e,l,t,n,o,u)}},Lu=(e,t,n,o=4)=>[n[0],n[2],n[3],Math.ceil(e[1]*t[2]*t[3]/o)]}),dh,ch,tw,BO=D(()=>{"use strict";Ce(),Ge(),Ie(),On(),ew(),dh=(e,t)=>({name:"ConvDotProduct",inputNames:e?["Im2Col","K","B"]:["Im2Col","K"],inputTypes:e?[0,4,0]:[0,4],cacheKey:t.activationCacheKey}),ch=(e,t,n,o,u)=>{let l=n[0].dims,d=n[1].dims,c=[d[0],Math.ceil(l[1]*d[2]*d[3]/4)],s=Lu(l,d,o),[r,i]=e.calculateTextureWidthAndHeight(c,4),a=me.computeStrides(s),[p,h]=e.calculateTextureWidthAndHeight(s,4),m=o.length,b=n.length<3?"0.0":"_B(b)",$=Math.ceil(l[1]*d[2]*d[3]/4),{activationFunction:_,applyActivation:y}=vi(u),T=Te(e.session.backend.glContext.version),S=` -${_} + `;return{...t,output:{dims:r,type:n.type,textureType:4},shaderSource:o}},M_=(e,t,n,i,u)=>{let l=qh(u.cacheKey);return{...l,get:()=>Gh(e,l,t,n,i,u)}},hl=(e,t,n,i=4)=>[n[0],n[2],n[3],Math.ceil(e[1]*t[2]*t[3]/i)]}),Hh,Wh,z_,VE=D(()=>{"use strict";Me(),Ke(),Pe(),Vn(),N_(),Hh=(e,t)=>({name:"ConvDotProduct",inputNames:e?["Im2Col","K","B"]:["Im2Col","K"],inputTypes:e?[0,4,0]:[0,4],cacheKey:t.activationCacheKey}),Wh=(e,t,n,i,u)=>{let l=n[0].dims,d=n[1].dims,c=[d[0],Math.ceil(l[1]*d[2]*d[3]/4)],s=hl(l,d,i),[r,o]=e.calculateTextureWidthAndHeight(c,4),a=ye.computeStrides(s),[p,h]=e.calculateTextureWidthAndHeight(s,4),m=i.length,g=n.length<3?"0.0":"_B(b)",_=Math.ceil(l[1]*d[2]*d[3]/4),{activationFunction:w,applyActivation:y}=Bi(u),T=Ae(e.session.backend.glContext.version),S=` +${w} float process(int indices[${m}]) { int b[1]; b[0] = indices[1]; @@ -672,19 +672,19 @@ float process(int indices[${m}]) { im2col[2] = indices[3]; int im2colOffset = im2col[0] * ${a[0]} + im2col[1] * ${a[1]} + im2col[2] * ${a[2]}; int kernelOffset = indices[1] * ${c[1]}; - float value = ${b}; - for (int i = 0; i < ${$}; ++i) { + float value = ${g}; + for (int i = 0; i < ${_}; ++i) { vec2 im2colCoords = offsetToCoords(im2colOffset, ${p}, ${h}); - vec2 kernelCoords = offsetToCoords(kernelOffset, ${r}, ${i}); + vec2 kernelCoords = offsetToCoords(kernelOffset, ${r}, ${o}); value += dot(${T.texture2D}(Im2Col, im2colCoords), ${T.texture2D}(K, kernelCoords)); ++im2colOffset; ++kernelOffset; } ${y} return value; -}`;return{...t,output:{dims:o,type:n[0].type,textureType:0},shaderSource:S}},tw=(e,t,n,o)=>{let u=dh(t.length>2,o);return{...u,get:()=>ch(e,u,t,n,o)}}}),pi,ju,ph,hh,fh,mh,Uu,gh,Tl=D(()=>{"use strict";rt(),Ce(),PO(),MO(),BO(),On(),ew(),xl(),pi=(e,t,n,o,u)=>{let l=e[0],d=e.slice(2),c=d.length,s=t[0],r=t.slice(2).map((a,p)=>a+(a-1)*(n[p]-1)),i=d.map((a,p)=>a+o[p]+o[p+c]).map((a,p)=>Math.floor((a-r[p]+u[p])/u[p]));return[l,s].concat(...i)},ju=(e,t,n)=>(gh(t,n),ph(e,t,n)),ph=(e,t,n)=>{let o=mh(n,t),u=e.session.pack,l=o.kernelShape[0]===1&&o.kernelShape[1]===1;return o.group>1?[e.run(q_(e,t,o),t)]:l&&u?[hh(e,t,o)]:u&&t[0].dims.length===4&&t[0].dims[0]===1&&!l?[J_(e,t,o)]:[fh(e,t,o)]},hh=(e,t,n)=>{let o=t[0].dims,u=t[1].dims,l=pi(o,u,n.dilations,n.pads,n.strides),d=e.reshapeUnpacked(t[0],[o[1],o[2]*o[3]]),c=e.reshapeUnpacked(t[1],[u[0],u[1]]),s=t.length>2?[c,d,t[2]]:[c,d],r=e.run(H_(s,n),s);return e.reshapeUnpacked(r,l)},fh=(e,t,n)=>{let o=t[0].dims,u=t[1].dims,l=pi(o,u,n.dilations,n.pads,n.strides),d=e.run(Q_(e,t[0],t[1],l,n),[t[0]]),c=t.length===3?[d,t[1],t[2]]:[d,t[1]];return e.run(tw(e,t,l,n),c)},mh=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let l=2;l{let t=e.attributes,n=Bo(t),o=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("pads",[0,0,0,0]),s=t.getInts("strides",[1,1]);return Be({autoPad:o,dilations:u,group:l,kernelShape:d,pads:c,strides:s,...n})},gh=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],o=e[1].dims[1]*t.group;if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("Conv input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("Conv input(bias) should be float tensor")}}),bh,yh,_h,rw,wh,vh,xh,$h,Th,Sh,nw,Ih,FO=D(()=>{"use strict";rt(),Ge(),Ie(),On(),bh=(e,t,n,o,u,l)=>(e-1)*t+n+(o-1)*u+1-l,yh=(e,t,n,o,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[o]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[o]=e-l,n[u]=l)},_h=(e,t,n,o,u,l,d,c)=>{let s=e.length-2,r=c.length===0;for(let i=0;i(Ih(t,n),wh(e,t,n)),wh=(e,t,n)=>{let o=Sh(n,t);return[Th(e,t,o)]},vh=(e,t)=>({name:"ConvTranspose",inputNames:e?["X","W","B"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),xh=(e,t,n,o)=>{let u=t.length>2?"getB(output_channel)":"0.0",l=t[0].dims,d=t[1].dims,c=d[1],s=d[0]/o.group,r=[t[0].dims[0],t[1].dims[1]*o.group,...o.outputShape],i=Te(e.session.backend.glContext.version),{activationFunction:a,applyActivation:p}=vi(o),h=` - const ivec2 strides = ivec2(${o.strides[0]}, ${o.strides[1]}); - const ivec2 pads = ivec2(${o.pads[0]}, ${o.pads[1]}); +}`;return{...t,output:{dims:i,type:n[0].type,textureType:0},shaderSource:S}},z_=(e,t,n,i)=>{let u=Hh(t.length>2,i);return{...u,get:()=>Wh(e,u,t,n,i)}}}),Ai,fl,Kh,Xh,Zh,Yh,ml,Jh,Yl=D(()=>{"use strict";ut(),Me(),NE(),UE(),VE(),Vn(),N_(),Xl(),Ai=(e,t,n,i,u)=>{let l=e[0],d=e.slice(2),c=d.length,s=t[0],r=t.slice(2).map((a,p)=>a+(a-1)*(n[p]-1)),o=d.map((a,p)=>a+i[p]+i[p+c]).map((a,p)=>Math.floor((a-r[p]+u[p])/u[p]));return[l,s].concat(...o)},fl=(e,t,n)=>(Jh(t,n),Kh(e,t,n)),Kh=(e,t,n)=>{let i=Yh(n,t),u=e.session.pack,l=i.kernelShape[0]===1&&i.kernelShape[1]===1;return i.group>1?[e.run(I_(e,t,i),t)]:l&&u?[Xh(e,t,i)]:u&&t[0].dims.length===4&&t[0].dims[0]===1&&!l?[D_(e,t,i)]:[Zh(e,t,i)]},Xh=(e,t,n)=>{let i=t[0].dims,u=t[1].dims,l=Ai(i,u,n.dilations,n.pads,n.strides),d=e.reshapeUnpacked(t[0],[i[1],i[2]*i[3]]),c=e.reshapeUnpacked(t[1],[u[0],u[1]]),s=t.length>2?[c,d,t[2]]:[c,d],r=e.run(E_(s,n),s);return e.reshapeUnpacked(r,l)},Zh=(e,t,n)=>{let i=t[0].dims,u=t[1].dims,l=Ai(i,u,n.dilations,n.pads,n.strides),d=e.run(M_(e,t[0],t[1],l,n),[t[0]]),c=t.length===3?[d,t[1],t[2]]:[d,t[1]];return e.run(z_(e,t,l,n),c)},Yh=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let l=2;l{let t=e.attributes,n=is(t),i=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("pads",[0,0,0,0]),s=t.getInts("strides",[1,1]);return je({autoPad:i,dilations:u,group:l,kernelShape:d,pads:c,strides:s,...n})},Jh=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],i=e[1].dims[1]*t.group;if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("Conv input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("Conv input(bias) should be float tensor")}}),Qh,ef,tf,B_,rf,nf,of,sf,af,uf,F_,lf,qE=D(()=>{"use strict";ut(),Ke(),Pe(),Vn(),Qh=(e,t,n,i,u,l)=>(e-1)*t+n+(i-1)*u+1-l,ef=(e,t,n,i,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[i]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[i]=e-l,n[u]=l)},tf=(e,t,n,i,u,l,d,c)=>{let s=e.length-2,r=c.length===0;for(let o=0;o(lf(t,n),rf(e,t,n)),rf=(e,t,n)=>{let i=uf(n,t);return[af(e,t,i)]},nf=(e,t)=>({name:"ConvTranspose",inputNames:e?["X","W","B"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),of=(e,t,n,i)=>{let u=t.length>2?"getB(output_channel)":"0.0",l=t[0].dims,d=t[1].dims,c=d[1],s=d[0]/i.group,r=[t[0].dims[0],t[1].dims[1]*i.group,...i.outputShape],o=Ae(e.session.backend.glContext.version),{activationFunction:a,applyActivation:p}=Bi(i),h=` + const ivec2 strides = ivec2(${i.strides[0]}, ${i.strides[1]}); + const ivec2 pads = ivec2(${i.pads[0]}, ${i.pads[1]}); ${a} void main() { ivec4 coords = getOutputCoords(); @@ -701,7 +701,7 @@ float process(int indices[${m}]) { int input_channel = group_id * ${s} + inChannelOffset; for (int wWOff = 0; wWOff < ${d[2]}; wWOff++) { for (int wHOff = 0; wHOff < ${d[3]}; wHOff++) { - ivec2 wOff = ivec2(wWOff * ${o.dilations[0]}, wHOff * ${o.dilations[1]}); + ivec2 wOff = ivec2(wWOff * ${i.dilations[0]}, wHOff * ${i.dilations[1]}); ivec2 wLoc = loc - wOff; ivec2 wLocIn = wLoc / strides; if ( @@ -717,29 +717,29 @@ float process(int indices[${m}]) { } } ${p} - ${i.output} = vec4(value, .0, .0, .0); + ${o.output} = vec4(value, .0, .0, .0); } -`;return{...n,output:{dims:r,type:t[0].type,textureType:0},shaderSource:h,hasMain:!0}},$h=(e,t,n)=>{let o=vh(t.length>2,n.cacheKey);return{...o,get:()=>xh(e,t,o,n)}},Th=(e,t,n)=>e.run($h(e,t,n),t),Sh=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let c=2;c{let t=e.attributes,n=Bo(t),o=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("output_padding",[0,0]),s=t.getInts("output_shape",[]),r=t.getInts("pads",[0,0,0,0]),i=t.getInts("strides",[1,1]);return Be({autoPad:o,dilations:u,group:l,kernelShape:d,outputPadding:c,outputShape:s,pads:r,strides:i,...n})},Ih=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],o=e[1].dims[0];if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("ConvTranspose input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("ConvTranspose input(bias) should be float tensor")}}),Ks,bi,iw,Oh,Xs,Eh,Ah,kh,Sl=D(()=>{"use strict";rt(),Ce(),Ie(),Ks={name:"Transpose",inputNames:["A"],inputTypes:[0]},bi=(e,t,n)=>(kh(t),[e.run({...Ks,cacheHint:n.cacheKey,get:()=>Oh(e,t[0],n.perm)},t)]),iw=e=>Be({perm:e.attributes.getInts("perm",[])}),Oh=(e,t,n)=>{let o=t.dims;n=Xs(o,n);let u=Eh(o,n),l=o.length,d=` - ${Ah("perm",n,l)} +`;return{...n,output:{dims:r,type:t[0].type,textureType:0},shaderSource:h,hasMain:!0}},sf=(e,t,n)=>{let i=nf(t.length>2,n.cacheKey);return{...i,get:()=>of(e,t,i,n)}},af=(e,t,n)=>e.run(sf(e,t,n),t),uf=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let c=2;c{let t=e.attributes,n=is(t),i=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("output_padding",[0,0]),s=t.getInts("output_shape",[]),r=t.getInts("pads",[0,0,0,0]),o=t.getInts("strides",[1,1]);return je({autoPad:i,dilations:u,group:l,kernelShape:d,outputPadding:c,outputShape:s,pads:r,strides:o,...n})},lf=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],i=e[1].dims[0];if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("ConvTranspose input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("ConvTranspose input(bias) should be float tensor")}}),va,Di,L_,df,xa,cf,pf,hf,Jl=D(()=>{"use strict";ut(),Me(),Pe(),va={name:"Transpose",inputNames:["A"],inputTypes:[0]},Di=(e,t,n)=>(hf(t),[e.run({...va,cacheHint:n.cacheKey,get:()=>df(e,t[0],n.perm)},t)]),L_=e=>je({perm:e.attributes.getInts("perm",[])}),df=(e,t,n)=>{let i=t.dims;n=xa(i,n);let u=cf(i,n),l=i.length,d=` + ${pf("perm",n,l)} float process(int indices[${l}]) { int a[${l}]; perm(a, indices); return _A(a); - }`;return{...Ks,output:{dims:u,type:t.type,textureType:0},shaderSource:d}},Xs=(e,t)=>(t&&t.length!==e.length&&(t=[...e.keys()].reverse()),t),Eh=(e,t)=>(t=Xs(e,t),me.sortBasedOnPerm(e,t)),Ah=(e,t,n)=>{let o=[];o.push(`void ${e}(out int a[${n}], int src[${n}]) {`);for(let u=0;u{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("input should be float tensor")}}),ow,sw,Ph,LO=D(()=>{"use strict";Sl(),ow=(e,t,n)=>{Ph(t);let o=n.blocksize,u=o*o,l=n.mode==="DCR"?[0,3,4,1,5,2]:[0,1,4,2,5,3],d=n.mode==="DCR"?[t[0].dims[0],o,o,t[0].dims[1]/u,t[0].dims[2],t[0].dims[3]]:[t[0].dims[0],t[0].dims[1]/u,o,o,t[0].dims[2],t[0].dims[3]],c=e.reshapeUnpacked(t[0],d),s={perm:l,cacheKey:`${l}`},[r]=bi(e,[c],s),i=[t[0].dims[0],t[0].dims[1]/u,t[0].dims[2]*o,t[0].dims[3]*o];return[e.reshapeUnpacked(r,i)]},sw=e=>{let t=e.attributes.getInt("blocksize");if(t<1)throw new Error(`blocksize must be >= 1, but got : ${t} for DepthToSpace`);let n=e.attributes.getString("mode","DCR");if(n!=="DCR"&&n!=="CRD")throw new Error(`unrecognized mode: ${n} for DepthToSpace`);return{mode:n,blocksize:t}},Ph=e=>{if(e.length!==1)throw new Error(`DepthToSpace expect 1 inputs, but got ${e.length}`);if(e[0].type==="string"||e[0].dims.length!==4)throw new TypeError("DepthToSpace input should be a 4-D numeric tensor")}}),aw,uw,Ch,jO=D(()=>{"use strict";Ce(),aw=(e,t,n)=>{Ch(t,n);let o=me.flattenShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],o)]},uw=e=>e.attributes.getInt("axis",1),Ch=(e,t)=>{if(!e||e.length!==1)throw new Error("Flatten requires 1 input.");let n=e[0].dims.length;if(n===0)throw new Error("scalar tensor is not supported.");if(t<-n||t>n)throw new Error("Invalid axis");if(e[0].type==="string")throw new Error("string tensor is not supported.")}}),xi,Fo=D(()=>{"use strict";xi=["float32","float64","int32","int16","int8","uint16","uint32","uint8"]}),lw,dw,Dh,Rh,Nh,zh,UO=D(()=>{"use strict";rt(),Fo(),Ce(),Ie(),lw=(e,t,n)=>(zh(t,n.axis),[e.run(Nh(e,t,n),t)]),dw=e=>Be({axis:e.attributes.getInt("axis",0)}),Dh={name:"Gather",inputNames:["A","B"],inputTypes:[0,0]},Rh=(e,t,n,o)=>{let u=n[0].dims.slice(),l=n[1].dims.slice(),d=new Array(u.length+l.length-1);o=me.normalizeAxis(o,u.length);let c=[];for(let p=0;p(t&&t.length!==e.length&&(t=[...e.keys()].reverse()),t),cf=(e,t)=>(t=xa(e,t),ye.sortBasedOnPerm(e,t)),pf=(e,t,n)=>{let i=[];i.push(`void ${e}(out int a[${n}], int src[${n}]) {`);for(let u=0;u{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("input should be float tensor")}}),j_,U_,ff,GE=D(()=>{"use strict";Jl(),j_=(e,t,n)=>{ff(t);let i=n.blocksize,u=i*i,l=n.mode==="DCR"?[0,3,4,1,5,2]:[0,1,4,2,5,3],d=n.mode==="DCR"?[t[0].dims[0],i,i,t[0].dims[1]/u,t[0].dims[2],t[0].dims[3]]:[t[0].dims[0],t[0].dims[1]/u,i,i,t[0].dims[2],t[0].dims[3]],c=e.reshapeUnpacked(t[0],d),s={perm:l,cacheKey:`${l}`},[r]=Di(e,[c],s),o=[t[0].dims[0],t[0].dims[1]/u,t[0].dims[2]*i,t[0].dims[3]*i];return[e.reshapeUnpacked(r,o)]},U_=e=>{let t=e.attributes.getInt("blocksize");if(t<1)throw new Error(`blocksize must be >= 1, but got : ${t} for DepthToSpace`);let n=e.attributes.getString("mode","DCR");if(n!=="DCR"&&n!=="CRD")throw new Error(`unrecognized mode: ${n} for DepthToSpace`);return{mode:n,blocksize:t}},ff=e=>{if(e.length!==1)throw new Error(`DepthToSpace expect 1 inputs, but got ${e.length}`);if(e[0].type==="string"||e[0].dims.length!==4)throw new TypeError("DepthToSpace input should be a 4-D numeric tensor")}}),V_,q_,mf,HE=D(()=>{"use strict";Me(),V_=(e,t,n)=>{mf(t,n);let i=ye.flattenShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],i)]},q_=e=>e.attributes.getInt("axis",1),mf=(e,t)=>{if(!e||e.length!==1)throw new Error("Flatten requires 1 input.");let n=e[0].dims.length;if(n===0)throw new Error("scalar tensor is not supported.");if(t<-n||t>n)throw new Error("Invalid axis");if(e[0].type==="string")throw new Error("string tensor is not supported.")}}),Fi,os=D(()=>{"use strict";Fi=["float32","float64","int32","int16","int8","uint16","uint32","uint8"]}),G_,H_,gf,bf,yf,wf,WE=D(()=>{"use strict";ut(),os(),Me(),Pe(),G_=(e,t,n)=>(wf(t,n.axis),[e.run(yf(e,t,n),t)]),H_=e=>je({axis:e.attributes.getInt("axis",0)}),gf={name:"Gather",inputNames:["A","B"],inputTypes:[0,0]},bf=(e,t,n,i)=>{let u=n[0].dims.slice(),l=n[1].dims.slice(),d=new Array(u.length+l.length-1);i=ye.normalizeAxis(i,u.length);let c=[];for(let p=0;p{let o={...Dh,cacheHint:n.cacheKey};return{...o,get:()=>Rh(e,o,t,n.axis)}},zh=(e,t)=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.");let n=e[0].dims.length;if(n<1)throw new Error("Invalid input shape.");if(t<-n||t>n-1)throw new Error("Invalid axis.");if(xi.indexOf(e[0].type)===-1)throw new Error("Invaid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invaid input type.")}}),Vu,Zs,cw,pw,Mh,Bh,Fh,VO=D(()=>{"use strict";rt(),Ce(),Ie(),Vu=(e,t,n)=>(Fh(t,n),[e.run(Mh(t,n),t)]),Zs=(e,t)=>{let n=e.attributes.getInt("transA",0)!==0,o=e.attributes.getInt("transB",0)!==0,u=e.attributes.getFloat("alpha",1),l=e.attributes.getFloat("beta",1);return Be({transA:n,transB:o,alpha:u,beta:l,isOptionalC:t})},cw=e=>Zs(e,!1),pw=e=>Zs(e,!0),Mh=(e,t)=>{let n={name:"Gemm",inputNames:e.length===3?["A","B","C"]:["A","B"],inputTypes:e.length===3?[0,0,0]:[0,0],key:t.cacheKey};return{...n,get:()=>Bh(n,e,t)}},Bh=(e,t,n)=>{let o=t[0].dims.slice(),u=t[1].dims.slice(),[l,d]=B0.getShapeOfGemmResult(o,n.transA,u,n.transB,t.length===3?t[2].dims:void 0),c=[l,d];if(!c)throw new Error("Can't use gemm on the given tensors");let s=o[o.length-1],r="";n.transA&&(s=o[0]),n.transA&&n.transB?r="value += _A_T(a) * _B_T(b);":n.transA&&!n.transB?r="value += _A_T(a) * _B(b);":!n.transA&&n.transB?r="value += _A(a) * _B_T(b);":!n.transA&&!n.transB&&(r="value += _A(a) * _B(b);");let i=c.length,a=t.length===3?`int c[${t[2].dims.length}];`:"",p=t.length===3?"bcastIndices_C(indices, c);":"",h=t.length===3?"value += beta * _C(c);":"",m=` - float process(int indices[${i}]) { - int a[${i}]; - int b[${i}]; + }`;return{...t,output:{dims:d,type:n[0].type,textureType:0},shaderSource:a}},yf=(e,t,n)=>{let i={...gf,cacheHint:n.cacheKey};return{...i,get:()=>bf(e,i,t,n.axis)}},wf=(e,t)=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.");let n=e[0].dims.length;if(n<1)throw new Error("Invalid input shape.");if(t<-n||t>n-1)throw new Error("Invalid axis.");if(Fi.indexOf(e[0].type)===-1)throw new Error("Invaid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invaid input type.")}}),gl,$a,W_,K_,_f,vf,xf,KE=D(()=>{"use strict";ut(),Me(),Pe(),gl=(e,t,n)=>(xf(t,n),[e.run(_f(t,n),t)]),$a=(e,t)=>{let n=e.attributes.getInt("transA",0)!==0,i=e.attributes.getInt("transB",0)!==0,u=e.attributes.getFloat("alpha",1),l=e.attributes.getFloat("beta",1);return je({transA:n,transB:i,alpha:u,beta:l,isOptionalC:t})},W_=e=>$a(e,!1),K_=e=>$a(e,!0),_f=(e,t)=>{let n={name:"Gemm",inputNames:e.length===3?["A","B","C"]:["A","B"],inputTypes:e.length===3?[0,0,0]:[0,0],key:t.cacheKey};return{...n,get:()=>vf(n,e,t)}},vf=(e,t,n)=>{let i=t[0].dims.slice(),u=t[1].dims.slice(),[l,d]=_w.getShapeOfGemmResult(i,n.transA,u,n.transB,t.length===3?t[2].dims:void 0),c=[l,d];if(!c)throw new Error("Can't use gemm on the given tensors");let s=i[i.length-1],r="";n.transA&&(s=i[0]),n.transA&&n.transB?r="value += _A_T(a) * _B_T(b);":n.transA&&!n.transB?r="value += _A_T(a) * _B(b);":!n.transA&&n.transB?r="value += _A(a) * _B_T(b);":!n.transA&&!n.transB&&(r="value += _A(a) * _B(b);");let o=c.length,a=t.length===3?`int c[${t[2].dims.length}];`:"",p=t.length===3?"bcastIndices_C(indices, c);":"",h=t.length===3?"value += beta * _C(c);":"",m=` + float process(int indices[${o}]) { + int a[${o}]; + int b[${o}]; ${a} copyVec(indices, a); @@ -748,20 +748,20 @@ float process(int indices[${m}]) { float value = 0.0; for (int k=0; k<${s}; ++k) { - a[${i-1}] = k; - b[${i-2}] = k; + a[${o-1}] = k; + b[${o-2}] = k; ${r} } value = value * alpha; ${h} return value; - }`;return{...e,output:{dims:c,type:t[0].type,textureType:0},variables:[{name:"alpha",type:"float",data:n.alpha},{name:"beta",type:"float",data:n.beta}],shaderSource:m}},Fh=(e,t)=>{if(!e)throw new Error("Input is missing");if(t.isOptionalC&&(e.length<2||e.length>3))throw new Error("Invaid input shape.");if(!t.isOptionalC&&e.length!==3)throw new Error("Gemm requires 3 inputs");if(e.length===3&&e[2].dims.length!==1&&e[2].dims.length!==2)throw new Error("Invalid input shape of C");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64"||e.length===3&&e[2].type!=="float32"&&e[2].type!=="float64")throw new Error("Invalid input type.");if(e[0].type!==e[1].type||e.length===3&&e[0].type!==e[2].type)throw new Error("Input types are mismatched")}}),hw,fw,Lh,jh,Uh,Vh,qh,qO=D(()=>{"use strict";rt(),Ie(),hw=(e,t,n)=>(qh(t),[e.run(Uh(e,t,n),t)]),fw=e=>{let t=e.attributes.getFloat("scale"),n=e.attributes.getFloats("bias");return Be({scale:t,bias:n})},Lh={name:"ImageScaler",inputNames:["X"],inputTypes:[0]},jh=(e,t,n,o)=>{let u=n[0].dims.slice(),l=u.length,d=` - ${Vh(o.bias.length)} + }`;return{...e,output:{dims:c,type:t[0].type,textureType:0},variables:[{name:"alpha",type:"float",data:n.alpha},{name:"beta",type:"float",data:n.beta}],shaderSource:m}},xf=(e,t)=>{if(!e)throw new Error("Input is missing");if(t.isOptionalC&&(e.length<2||e.length>3))throw new Error("Invaid input shape.");if(!t.isOptionalC&&e.length!==3)throw new Error("Gemm requires 3 inputs");if(e.length===3&&e[2].dims.length!==1&&e[2].dims.length!==2)throw new Error("Invalid input shape of C");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64"||e.length===3&&e[2].type!=="float32"&&e[2].type!=="float64")throw new Error("Invalid input type.");if(e[0].type!==e[1].type||e.length===3&&e[0].type!==e[2].type)throw new Error("Input types are mismatched")}}),X_,Z_,$f,Tf,Sf,If,Of,XE=D(()=>{"use strict";ut(),Pe(),X_=(e,t,n)=>(Of(t),[e.run(Sf(e,t,n),t)]),Z_=e=>{let t=e.attributes.getFloat("scale"),n=e.attributes.getFloats("bias");return je({scale:t,bias:n})},$f={name:"ImageScaler",inputNames:["X"],inputTypes:[0]},Tf=(e,t,n,i)=>{let u=n[0].dims.slice(),l=u.length,d=` + ${If(i.bias.length)} float process(int indices[${l}]) { return _X(indices) * scale + getBias(bias, indices[1]); - }`;return{...t,output:{dims:u,type:n[0].type,textureType:0},variables:[{name:"bias",type:"float",arrayLength:o.bias.length,data:o.bias},{name:"scale",type:"float",data:o.scale}],shaderSource:d}},Uh=(e,t,n)=>{let o={...Lh,cacheHint:n.cacheKey};return{...o,get:()=>jh(e,o,t,n)}},Vh=e=>{let t=[`float getBias(float bias[${e}], int channel) {`];for(let n=0;n{if(!e||e.length!==1)throw new Error("ImageScaler requires 1 input.");if(e[0].dims.length!==4)throw new Error("Invalid input shape.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")}}),mw,gw,Ys,Gh,Hh,Wh,Kh,Xh,Zh,GO=D(()=>{"use strict";Ge(),Ie(),mw=(e,t,n)=>{Zh(t);let o=e.run(Hh(t[0]),t);return[e.run(Xh(e,t[0],n,o.dims),[t[0],o,t[1],t[2]])]},gw=e=>e.attributes.getFloat("epsilon",1e-5),Ys={name:"InstanceNormalization_MeanAndVariance",inputNames:["X"],inputTypes:[0]},Gh=(e,t)=>{let n=t.dims.slice(),o=n[1],u=n[2]*n[3],l=[n[0],o],d=` + }`;return{...t,output:{dims:u,type:n[0].type,textureType:0},variables:[{name:"bias",type:"float",arrayLength:i.bias.length,data:i.bias},{name:"scale",type:"float",data:i.scale}],shaderSource:d}},Sf=(e,t,n)=>{let i={...$f,cacheHint:n.cacheKey};return{...i,get:()=>Tf(e,i,t,n)}},If=e=>{let t=[`float getBias(float bias[${e}], int channel) {`];for(let n=0;n{if(!e||e.length!==1)throw new Error("ImageScaler requires 1 input.");if(e[0].dims.length!==4)throw new Error("Invalid input shape.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")}}),Y_,J_,Ta,Ef,Af,kf,Pf,Cf,Rf,ZE=D(()=>{"use strict";Ke(),Pe(),Y_=(e,t,n)=>{Rf(t);let i=e.run(Af(t[0]),t);return[e.run(Cf(e,t[0],n,i.dims),[t[0],i,t[1],t[2]])]},J_=e=>e.attributes.getFloat("epsilon",1e-5),Ta={name:"InstanceNormalization_MeanAndVariance",inputNames:["X"],inputTypes:[0]},Ef=(e,t)=>{let n=t.dims.slice(),i=n[1],u=n[2]*n[3],l=[n[0],i],d=` vec4 process(int[2] indices) { vec4 v = vec4(0.0); int a[4]; @@ -790,7 +790,7 @@ float process(int indices[${m}]) { v.g = temp / float(${u}); return v; - }`;return{...e,output:{dims:l,type:t.type,textureType:4},shaderSource:d}},Hh=e=>({...Ys,get:()=>Gh(Ys,e)}),Wh={name:"InstanceNormalization_ComputeOutput",inputNames:["X","MeanAndVariance","Scale","B"],inputTypes:[0,4,0,0]},Kh=(e,t,n,o,u)=>{let l=Te(e.session.backend.glContext.version),[d,c]=e.calculateTextureWidthAndHeight(u,4),[s,r]=[d/4,c],i=` + }`;return{...e,output:{dims:l,type:t.type,textureType:4},shaderSource:d}},Af=e=>({...Ta,get:()=>Ef(Ta,e)}),kf={name:"InstanceNormalization_ComputeOutput",inputNames:["X","MeanAndVariance","Scale","B"],inputTypes:[0,4,0,0]},Pf=(e,t,n,i,u)=>{let l=Ae(e.session.backend.glContext.version),[d,c]=e.calculateTextureWidthAndHeight(u,4),[s,r]=[d/4,c],o=` vec4 get_MeanAndVariance(int[2] mv) { int offset = indicesToOffset_MeanAndVariance(mv); vec2 coords = offsetToCoords(offset, ${s}, ${r}); @@ -811,8 +811,8 @@ float process(int indices[${m}]) { float b = _B(sb); return scale * (_X(indices) - mean) / sqrt(variance + epsilon) + b; - }`;return{...t,output:{dims:n.dims,type:n.type,textureType:0},variables:[{name:"epsilon",type:"float",data:o}],shaderSource:i}},Xh=(e,t,n,o)=>{let u={...Wh,cacheHint:`${n}`};return{...u,get:()=>Kh(e,u,t,n,o)}},Zh=e=>{if(!e||e.length!==3)throw new Error("InstanceNormalization requires 3 inputs.");let t=e[0],n=e[1],o=e[2];if(t.dims.length<3||n.dims.length!==1||o.dims.length!==1)throw new Error("Invalid input shape.");if(n.dims[0]!==t.dims[1]||o.dims[0]!==t.dims[1])throw new Error("Input shapes are mismatched.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||o.type!=="float32"&&o.type!=="float64")throw new Error("Invalid input type.");if(e[0].dims.length!==4)throw new Error("Only support 4-D input shape.")}});function HO(e,t){let n=e[0].dims[1],o=e[0].dims.length,u=-Math.floor((t.size-1)/2),l=Math.ceil((t.size-1)/2),d=`float(${t.alpha}) / float(${t.size})`,c=`float(${t.bias})`,s=`float(${t.beta})`,r=` - float process(int indices[${o}]) { + }`;return{...t,output:{dims:n.dims,type:n.type,textureType:0},variables:[{name:"epsilon",type:"float",data:i}],shaderSource:o}},Cf=(e,t,n,i)=>{let u={...kf,cacheHint:`${n}`};return{...u,get:()=>Pf(e,u,t,n,i)}},Rf=e=>{if(!e||e.length!==3)throw new Error("InstanceNormalization requires 3 inputs.");let t=e[0],n=e[1],i=e[2];if(t.dims.length<3||n.dims.length!==1||i.dims.length!==1)throw new Error("Invalid input shape.");if(n.dims[0]!==t.dims[1]||i.dims[0]!==t.dims[1])throw new Error("Input shapes are mismatched.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||i.type!=="float32"&&i.type!=="float64")throw new Error("Invalid input type.");if(e[0].dims.length!==4)throw new Error("Only support 4-D input shape.")}});function YE(e,t){let n=e[0].dims[1],i=e[0].dims.length,u=-Math.floor((t.size-1)/2),l=Math.ceil((t.size-1)/2),d=`float(${t.alpha}) / float(${t.size})`,c=`float(${t.bias})`,s=`float(${t.beta})`,r=` + float process(int indices[${i}]) { int c = indices[1]; float x = _X(indices); float square_sum = 0.0; @@ -826,11 +826,11 @@ float process(int indices[${m}]) { } } return x / pow(${c} + ${d} * square_sum, ${s}); - }`;return{...Il,cacheHint:t.cacheKey,output:{dims:e[0].dims,type:e[0].type,textureType:0},shaderSource:r}}function WO(e,t){return{...Il,cacheHint:t.cacheKey,get:()=>HO(e,t)}}var bw,yw,Il,Yh,KO=D(()=>{"use strict";rt(),Ie(),bw=(e,t,n)=>(Yh(t),[e.run(WO(t,n),t)]),yw=e=>{let t=e.attributes.getFloat("alpha",1e-4),n=e.attributes.getFloat("beta",.75),o=e.attributes.getFloat("bias",1),u=e.attributes.getInt("size");return Be({alpha:t,beta:n,bias:o,size:u})},Il={name:"LRN",inputNames:["X"],inputTypes:[0]},Yh=e=>{if(!e||e.length!==1)throw new Error("LRN requires 1 input.");if(e[0].dims.length!==4)throw new Error('currently only support LRN for input with "NCHW" format');if(e[0].type!=="float32")throw new Error("input should be float type")}}),Jh,qu,_w,ww,vw,Qh,ef,tf,rf,nf,of,sf,af,XO=D(()=>{"use strict";rt(),Ce(),Ge(),Ie(),Jh={name:"Pad",inputNames:["A"],inputTypes:[0]},qu=(e,t,n)=>(tf(t),[e.run({...Jh,cacheHint:n.cacheKey,get:()=>ef(e,t[0],n)},t)]),_w=e=>{let t=e.attributes.getString("mode","constant"),n=e.attributes.getFloat("value",0),o=e.attributes.getInts("pads");return Be({mode:t,value:n,pads:o})},ww=(e,t,n)=>{rf(t);let o=Qh(e,t,n);return qu(e,[t[0]],o)},vw=e=>e.attributes.getString("mode","constant"),Qh=(e,t,n)=>{if(!e.session.isInitializer(t[1].dataId)||t.length>=3&&!e.session.isInitializer(t[2].dataId))throw new Error("dynamic pad attributes are not allowed");let o=Array.from(t[1].integerData),u=t.length>=3?t[2].floatData[0]:0;return Be({mode:n,pads:o,value:u})},ef=(e,t,n)=>{let o=me.padShape(t.dims.slice(),n.pads),u=o.length,l=` - ${nf(e,t,n)} + }`;return{...Ql,cacheHint:t.cacheKey,output:{dims:e[0].dims,type:e[0].type,textureType:0},shaderSource:r}}function JE(e,t){return{...Ql,cacheHint:t.cacheKey,get:()=>YE(e,t)}}var Q_,ev,Ql,Df,QE=D(()=>{"use strict";ut(),Pe(),Q_=(e,t,n)=>(Df(t),[e.run(JE(t,n),t)]),ev=e=>{let t=e.attributes.getFloat("alpha",1e-4),n=e.attributes.getFloat("beta",.75),i=e.attributes.getFloat("bias",1),u=e.attributes.getInt("size");return je({alpha:t,beta:n,bias:i,size:u})},Ql={name:"LRN",inputNames:["X"],inputTypes:[0]},Df=e=>{if(!e||e.length!==1)throw new Error("LRN requires 1 input.");if(e[0].dims.length!==4)throw new Error('currently only support LRN for input with "NCHW" format');if(e[0].type!=="float32")throw new Error("input should be float type")}}),Mf,bl,tv,rv,nv,Nf,zf,Bf,Ff,Lf,jf,Uf,Vf,eA=D(()=>{"use strict";ut(),Me(),Ke(),Pe(),Mf={name:"Pad",inputNames:["A"],inputTypes:[0]},bl=(e,t,n)=>(Bf(t),[e.run({...Mf,cacheHint:n.cacheKey,get:()=>zf(e,t[0],n)},t)]),tv=e=>{let t=e.attributes.getString("mode","constant"),n=e.attributes.getFloat("value",0),i=e.attributes.getInts("pads");return je({mode:t,value:n,pads:i})},rv=(e,t,n)=>{Ff(t);let i=Nf(e,t,n);return bl(e,[t[0]],i)},nv=e=>e.attributes.getString("mode","constant"),Nf=(e,t,n)=>{if(!e.session.isInitializer(t[1].dataId)||t.length>=3&&!e.session.isInitializer(t[2].dataId))throw new Error("dynamic pad attributes are not allowed");let i=Array.from(t[1].integerData),u=t.length>=3?t[2].floatData[0]:0;return je({mode:n,pads:i,value:u})},zf=(e,t,n)=>{let i=ye.padShape(t.dims.slice(),n.pads),u=i.length,l=` + ${Lf(e,t,n)} float process(int[${u}] indices) { return padA(indices); - }`;return{name:"Pad",inputNames:["A"],inputTypes:[0],output:{dims:o,type:t.type,textureType:0},shaderSource:l}},tf=e=>{if(!e||e.length!==1)throw new Error("Pad requires 1 input");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},rf=e=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Pad requires 2 or 3 inputs");if(e[1].type!=="int32")throw new Error("Invalid input type.");if(e.length>=3&&e[2].type==="string")throw new Error("Invalid input type.")},nf=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t.dims,0),d=me.computeStrides(t.dims);switch(n.mode){case"constant":return of(o,t.dims,d,u,l,n.pads,n.value);case"reflect":return sf(o,t.dims,d,u,l,n.pads);case"edge":return af(o,t.dims,d,u,l,n.pads);default:throw new Error("Invalid mode")}},of=(e,t,n,o,u,l,d)=>{let c=t.length,s="";for(let r=c-1;r>=0;--r)s+=` + }`;return{name:"Pad",inputNames:["A"],inputTypes:[0],output:{dims:i,type:t.type,textureType:0},shaderSource:l}},Bf=e=>{if(!e||e.length!==1)throw new Error("Pad requires 1 input");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},Ff=e=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Pad requires 2 or 3 inputs");if(e[1].type!=="int32")throw new Error("Invalid input type.");if(e.length>=3&&e[2].type==="string")throw new Error("Invalid input type.")},Lf=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t.dims,0),d=ye.computeStrides(t.dims);switch(n.mode){case"constant":return jf(i,t.dims,d,u,l,n.pads,n.value);case"reflect":return Uf(i,t.dims,d,u,l,n.pads);case"edge":return Vf(i,t.dims,d,u,l,n.pads);default:throw new Error("Invalid mode")}},jf=(e,t,n,i,u,l,d)=>{let c=t.length,s="";for(let r=c-1;r>=0;--r)s+=` k = m[${r}] - ${l[r]}; if (k < 0) return constant; if (k >= ${t[r]}) return constant; @@ -841,11 +841,11 @@ float process(int indices[${m}]) { int offset = 0; int k = 0; ${s} - vec2 coords = offsetToCoords(offset, ${o}, ${u}); + vec2 coords = offsetToCoords(offset, ${i}, ${u}); float value = getColorAsFloat(${e.texture2D}(A, coords)); return value; } - `},sf=(e,t,n,o,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` + `},Uf=(e,t,n,i,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` k = m[${s}] - ${l[s]}; if (k < 0) { k = -k; } { @@ -859,11 +859,11 @@ float process(int indices[${m}]) { int offset = 0; int k = 0; ${c} - vec2 coords = offsetToCoords(offset, ${o}, ${u}); + vec2 coords = offsetToCoords(offset, ${i}, ${u}); float value = getColorAsFloat(${e.texture2D}(A, coords)); return value; } - `},af=(e,t,n,o,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` + `},Vf=(e,t,n,i,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` k = m[${s}] - ${l[s]}; if (k < 0) k = 0; if (k >= ${t[s]}) k = ${t[s]-1}; @@ -873,20 +873,20 @@ float process(int indices[${m}]) { int offset = 0; int k = 0; ${c} - vec2 coords = offsetToCoords(offset, ${o}, ${u}); + vec2 coords = offsetToCoords(offset, ${i}, ${u}); float value = getColorAsFloat(${e.texture2D}(A, coords)); return value; } - `}}),xw,$w,Js,Tw,Sw,Iw,Ow,Qs,ea,uf,ta,Ew,qn,ra,Gn,lf,ZO=D(()=>{"use strict";rt(),Ce(),Ie(),xw=(e,t,n)=>{qn(t);let o={name:"AveragePool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...o,get:()=>Js(t,o,!1,n)},t)]},$w=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),o=e.attributes.getInt("count_include_pad",0)!==0,u=e.attributes.getInts("kernel_shape"),l=e.attributes.getInts("strides",[]),d=e.attributes.getInts("pads",[]);if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");return Be({autoPad:t,ceilMode:n,countIncludePad:o,kernelShape:u,strides:l,pads:d})},Js=(e,t,n,o)=>{let[u,l]=ea(e,o,n),d=me.size(u.kernelShape),c="value += _X(x);",s="";u.countIncludePad?s+=`value /= float(${d});`:s+=`value /= float(${d} - pad);`;let r=` - ${ra(e[0].dims,u,c,s,"0.0")} - `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:r}},Tw=(e,t,n)=>{qn(t);let o={name:"GlobalAveragePool",inputNames:["X"],inputTypes:[0],cacheHint:`${n.countIncludePad}`};return[e.run({...o,get:()=>Js(t,o,!0,n)},t)]},Sw=e=>{let t=e.attributes.getInt("count_include_pad",0)!==0;return Be({autoPad:"",ceilMode:0,countIncludePad:t,kernelShape:[],strides:[],pads:[]})},Iw=(e,t,n)=>{qn(t);let o={name:"MaxPool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...o,get:()=>Qs(t,o,!1,n)},t)]},Ow=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),o=e.attributes.getInts("kernel_shape"),u=e.attributes.getInts("strides",[]),l=e.attributes.getInts("pads",[]),d=e.attributes.getInt("storage_order",0),c=e.attributes.getInts("dilations",[]);if(d!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");return Be({autoPad:t,ceilMode:n,countIncludePad:!1,kernelShape:o,strides:u,pads:l,storageOrder:d,dilations:c})},Qs=(e,t,n,o)=>{let[u,l]=ea(e,o,n),d=` - ${ra(e[0].dims,u,` + `}}),iv,ov,Sa,sv,av,uv,lv,Ia,Oa,qf,Ea,dv,ai,Aa,ui,Gf,tA=D(()=>{"use strict";ut(),Me(),Pe(),iv=(e,t,n)=>{ai(t);let i={name:"AveragePool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...i,get:()=>Sa(t,i,!1,n)},t)]},ov=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),i=e.attributes.getInt("count_include_pad",0)!==0,u=e.attributes.getInts("kernel_shape"),l=e.attributes.getInts("strides",[]),d=e.attributes.getInts("pads",[]);if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");return je({autoPad:t,ceilMode:n,countIncludePad:i,kernelShape:u,strides:l,pads:d})},Sa=(e,t,n,i)=>{let[u,l]=Oa(e,i,n),d=ye.size(u.kernelShape),c="value += _X(x);",s="";u.countIncludePad?s+=`value /= float(${d});`:s+=`value /= float(${d} - pad);`;let r=` + ${Aa(e[0].dims,u,c,s,"0.0")} + `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:r}},sv=(e,t,n)=>{ai(t);let i={name:"GlobalAveragePool",inputNames:["X"],inputTypes:[0],cacheHint:`${n.countIncludePad}`};return[e.run({...i,get:()=>Sa(t,i,!0,n)},t)]},av=e=>{let t=e.attributes.getInt("count_include_pad",0)!==0;return je({autoPad:"",ceilMode:0,countIncludePad:t,kernelShape:[],strides:[],pads:[]})},uv=(e,t,n)=>{ai(t);let i={name:"MaxPool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...i,get:()=>Ia(t,i,!1,n)},t)]},lv=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),i=e.attributes.getInts("kernel_shape"),u=e.attributes.getInts("strides",[]),l=e.attributes.getInts("pads",[]),d=e.attributes.getInt("storage_order",0),c=e.attributes.getInts("dilations",[]);if(d!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");return je({autoPad:t,ceilMode:n,countIncludePad:!1,kernelShape:i,strides:u,pads:l,storageOrder:d,dilations:c})},Ia=(e,t,n,i)=>{let[u,l]=Oa(e,i,n),d=` + ${Aa(e[0].dims,u,` value = max(_X(x), value); `,"","-1e5")} - `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:d}},ea=(e,t,n)=>{let o=e[0].dims.slice(),u=Object.hasOwnProperty.call(t,"dilations"),l=t.kernelShape.slice(),d=t.strides.slice(),c=u?t.dilations.slice():[],s=t.pads.slice();xo.adjustPoolAttributes(n,o,l,d,c,s);let r=xo.computePoolOutputShape(n,o,d,c,l,s,t.autoPad),i=Object.assign({},t);return u?Object.assign(i,{kernelShape:l,strides:d,pads:s,dilations:c,cacheKey:t.cacheKey}):Object.assign(i,{kernelShape:l,strides:d,pads:s,cacheKey:t.cacheKey}),[i,r]},uf={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[],cacheKey:""},ta={name:"GlobalMaxPool",inputNames:["X"],inputTypes:[0]},Ew=(e,t)=>(qn(t),[e.run({...ta,get:()=>Qs(t,ta,!0,uf)},t)]),qn=e=>{if(!e||e.length!==1)throw new Error("Pool ops requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},ra=(e,t,n,o,u)=>{let l=e.length;if(t.kernelShape.length<=2){let d=t.kernelShape[t.kernelShape.length-1],c=t.strides[t.strides.length-1],s=t.pads[t.pads.length/2-1],r=t.pads[t.pads.length-1],i=e[l-1],a="",p="",h="";if(s+r!==0?a=` + `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:d}},Oa=(e,t,n)=>{let i=e[0].dims.slice(),u=Object.hasOwnProperty.call(t,"dilations"),l=t.kernelShape.slice(),d=t.strides.slice(),c=u?t.dilations.slice():[],s=t.pads.slice();Uo.adjustPoolAttributes(n,i,l,d,c,s);let r=Uo.computePoolOutputShape(n,i,d,c,l,s,t.autoPad),o=Object.assign({},t);return u?Object.assign(o,{kernelShape:l,strides:d,pads:s,dilations:c,cacheKey:t.cacheKey}):Object.assign(o,{kernelShape:l,strides:d,pads:s,cacheKey:t.cacheKey}),[o,r]},qf={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[],cacheKey:""},Ea={name:"GlobalMaxPool",inputNames:["X"],inputTypes:[0]},dv=(e,t)=>(ai(t),[e.run({...Ea,get:()=>Ia(t,Ea,!0,qf)},t)]),ai=e=>{if(!e||e.length!==1)throw new Error("Pool ops requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},Aa=(e,t,n,i,u)=>{let l=e.length;if(t.kernelShape.length<=2){let d=t.kernelShape[t.kernelShape.length-1],c=t.strides[t.strides.length-1],s=t.pads[t.pads.length/2-1],r=t.pads[t.pads.length-1],o=e[l-1],a="",p="",h="";if(s+r!==0?a=` for (int i = 0; i < ${d}; i++) { x[${l} - 1] = indices[${l} - 1] * ${c} - ${s} + i; - if (x[${l} - 1] < 0 || x[${l} - 1] >= ${i}) { + if (x[${l} - 1] < 0 || x[${l} - 1] >= ${o}) { pad++; continue; } @@ -895,16 +895,16 @@ float process(int indices[${m}]) { for (int i = 0; i < ${d}; i++) { x[${l} - 1] = indices[${l} - 1] * ${c} - ${s} + i; ${n} - }`,t.kernelShape.length===2){let m=t.kernelShape[t.kernelShape.length-2],b=t.strides[t.strides.length-2],$=t.pads[t.pads.length/2-2],_=t.pads[t.pads.length-2],y=e[l-2];$+_!==0?p=` + }`,t.kernelShape.length===2){let m=t.kernelShape[t.kernelShape.length-2],g=t.strides[t.strides.length-2],_=t.pads[t.pads.length/2-2],w=t.pads[t.pads.length-2],y=e[l-2];_+w!==0?p=` for (int j = 0; j < ${m}; j++) { - x[${l} - 2] = indices[${l} - 2] * ${b} - ${$} + j; + x[${l} - 2] = indices[${l} - 2] * ${g} - ${_} + j; if (x[${l} - 2] < 0 || x[${l} - 2] >= ${y}) { pad+= ${d}; continue; } `:p=` for (int j = 0; j < ${m}; j++) { - x[${l} - 2] = indices[${l} - 2] * ${b} - ${$} + j; + x[${l} - 2] = indices[${l} - 2] * ${g} - ${_} + j; `,h=` } `}return` @@ -917,10 +917,10 @@ float process(int indices[${m}]) { ${p} ${a} ${h} - ${o} + ${i} return value; } - `}else{let d=me.size(t.kernelShape),c=me.computeStrides(t.kernelShape),s=c.length,r=t.pads.length,i=lf(s),a=Gn(e,"inputDims"),p=Gn(t.pads,"pads"),h=Gn(c,"kernelStrides"),m=Gn(t.strides,"strides"),b=t.pads.reduce((_,y)=>_+y),$="";return b?$=` + `}else{let d=ye.size(t.kernelShape),c=ye.computeStrides(t.kernelShape),s=c.length,r=t.pads.length,o=Gf(s),a=ui(e,"inputDims"),p=ui(t.pads,"pads"),h=ui(c,"kernelStrides"),m=ui(t.strides,"strides"),g=t.pads.reduce((w,y)=>w+y),_="";return g?_=` if (x[j] >= inputDims[j] || x[j] < 0) { pad++; isPad = true; @@ -929,11 +929,11 @@ float process(int indices[${m}]) { } if (!isPad) { ${n} - }`:$=` + }`:_=` } ${n} `,` - ${i} + ${o} float process(int indices[${l}]) { int x[${l}]; copyVec(indices, x); @@ -956,15 +956,15 @@ float process(int indices[${m}]) { for (int j = ${l} - ${s}; j < ${l}; j++) { x[j] = indices[j] * strides[j - ${l} + ${s}] + offset[j - ${l} + ${s}] - pads[j - 2]; - ${$} + ${_} } - ${o} + ${i} return value; } - `}},Gn=(e,t)=>{let n="";for(let o=0;o` + `}},ui=(e,t)=>{let n="";for(let i=0;i` void offsetToIndices(int offset, int[${e}] strides, out int[${e}] indices) { if (${e} == 0) { return; @@ -974,7 +974,7 @@ float process(int indices[${m}]) { offset -= indices[i] * strides[i]; } indices[${e} - 1] = offset; - }`}),sr,mr,df,cf,Aw,kw,Pw,Cw,Dw,Rw,Nw,YO=D(()=>{"use strict";rt(),Fo(),Ce(),Ie(),sr=(e,t,n,o,u)=>{cf(t);let l={name:o,inputNames:["A"],inputTypes:[0]};return[e.run({...l,cacheHint:n.cacheKey,get:()=>df(e,t,n,o,u,l)},t)]},mr=e=>{let t=e.attributes.getInts("axes",[]),n=e.attributes.getInt("keepdims",1)===1;return Be({axes:t,keepDims:n})},df=(e,t,n,o,u,l)=>{let d=[],c=t[0].dims.length||1,s=[],r=me.normalizeAxes(n.axes,t[0].dims.length),i=u(t,r),a=i[1];for(let h=0;h=0||r.length===0?(n.keepDims&&d.push(1),a=` + }`}),yr,Or,Hf,Wf,cv,pv,hv,fv,mv,gv,bv,rA=D(()=>{"use strict";ut(),os(),Me(),Pe(),yr=(e,t,n,i,u)=>{Wf(t);let l={name:i,inputNames:["A"],inputTypes:[0]};return[e.run({...l,cacheHint:n.cacheKey,get:()=>Hf(e,t,n,i,u,l)},t)]},Or=e=>{let t=e.attributes.getInts("axes",[]),n=e.attributes.getInt("keepdims",1)===1;return je({axes:t,keepDims:n})},Hf=(e,t,n,i,u,l)=>{let d=[],c=t[0].dims.length||1,s=[],r=ye.normalizeAxes(n.axes,t[0].dims.length),o=u(t,r),a=o[1];for(let h=0;h=0||r.length===0?(n.keepDims&&d.push(1),a=` for(int j${h} = 0; j${h} < ${t[0].dims[h]}; j${h}++) { inputIdx[${h}] = j${h}; ${a} @@ -984,24 +984,24 @@ float process(int indices[${m}]) { int inputIdx[${c}]; // addressing input data ${s.join(` `)} - ${i[0]} // init ops for reduce max/min + ${o[0]} // init ops for reduce max/min ${a} - ${i[2]} // final computation for reduce mean + ${o[2]} // final computation for reduce mean return value; - }`;return{...l,output:{dims:d,type:t[0].type,textureType:0},shaderSource:p}},cf=e=>{if(!e||e.length!==1)throw new Error("Reduce op requires 1 input.");if(xi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},Aw=(e,t,n)=>sr(e,t,n,"ReduceSum",()=>["value = 0.0;","value += _A(inputIdx);",""]),kw=(e,t,n)=>sr(e,t,n,"ReduceMean",(o,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=o[0].dims[d]);return["value = 0.0;","value += _A(inputIdx);",`value /= ${l}.;`]}),Pw=(e,t,n)=>sr(e,t,n,"ReduceMax",(o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` + }`;return{...l,output:{dims:d,type:t[0].type,textureType:0},shaderSource:p}},Wf=e=>{if(!e||e.length!==1)throw new Error("Reduce op requires 1 input.");if(Fi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},cv=(e,t,n)=>yr(e,t,n,"ReduceSum",()=>["value = 0.0;","value += _A(inputIdx);",""]),pv=(e,t,n)=>yr(e,t,n,"ReduceMean",(i,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=i[0].dims[d]);return["value = 0.0;","value += _A(inputIdx);",`value /= ${l}.;`]}),hv=(e,t,n)=>yr(e,t,n,"ReduceMax",(i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` `)} -value = _A(inputIdx);`,"value = max(value, _A(inputIdx));",""]}),Cw=(e,t,n)=>sr(e,t,n,"ReduceMin",(o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` +value = _A(inputIdx);`,"value = max(value, _A(inputIdx));",""]}),fv=(e,t,n)=>yr(e,t,n,"ReduceMin",(i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` `)} -value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr(e,t,n,"ReduceProd",()=>["value = 1.0;","value *= _A(inputIdx);",""]),Rw=(e,t,n)=>sr(e,t,n,"ReduceLogSum",()=>["value = 0.0;","value += _A(inputIdx);","value = log(value);"]),Nw=(e,t,n)=>sr(e,t,n,"ReduceLogSumSquare",()=>["float t; value = 0.0;","t = _A(inputIdx); value += t * t;",""])}),zw,JO=D(()=>{"use strict";Ce(),zw=(e,t)=>{let n=me.calculateReshapedDims(t[0].dims,t[1].integerData);return e.session.pack?[e.reshapePacked(t[0],n)]:[e.reshapeUnpacked(t[0],n)]}}),na,Gu,Mw,Bw,hi,pf,Hu,To,Fw=D(()=>{"use strict";rt(),Ge(),Ie(),na={name:"Upsample",inputNames:["X"],inputTypes:[0]},Gu=(e,t,n)=>(Hu(t,n),[e.run({...na,cacheHint:n.cacheKey,get:()=>pf(e,t,n)},t)]),Mw=e=>hi(e,7),Bw=e=>hi(e,9),hi=(e,t)=>{let n=t>=10,o=e.attributes.getString("mode","nearest");if(o!=="nearest"&&o!=="linear"&&(t<11||o!=="cubic"))throw new Error(`unrecognized mode: ${o}`);let u=[];t<9&&(u=e.attributes.getFloats("scales"),To(u,o,n));let l=e.attributes.getFloat("extrapolation_value",0),d=t>10?e.attributes.getString("coordinate_transformation_mode","half_pixel"):"asymmetric";if(["asymmetric","pytorch_half_pixel","tf_half_pixel_for_nn","align_corners","tf_crop_and_resize","half_pixel"].indexOf(d)===-1)throw new Error(`coordinate_transform_mode '${d}' is not supported`);let c=d==="tf_crop_and_resize",s=c,r=o==="nearest"&&t>=11?e.attributes.getString("nearest_mode","round_prefer_floor"):"";if(["round_prefer_floor","round_prefer_ceil","floor","ceil",""].indexOf(r)===-1)throw new Error(`nearest_mode '${r}' is not supported`);let i=e.attributes.getFloat("cubic_coeff_a",-.75),a=e.attributes.getInt("exclude_outside",0)!==0;if(a&&o!=="cubic")throw new Error("exclude_outside can be set to 1 only when mode is CUBIC.");let p=t<11?!0:o==="nearest"&&d==="asymmetric"&&r==="floor",h=0,m=0,b=0;return t>10?e.inputs.length>2?(h=1,m=2,b=3):(m=1,b=2):t===9&&(m=1),Be({opset:t,isResize:n,mode:o,scales:u,extrapolationValue:l,coordinateTransformMode:d,useExtrapolation:s,needRoiInput:c,nearestMode:r,cubicCoefficientA:i,excludeOutside:a,useNearest2xOptimization:p,roiInputIdx:h,scalesInputIdx:m,sizesInputIdx:b})},pf=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t[0].dims,0),d=t[0].dims.map((b,$)=>Math.floor(b*n.scales[$])),[c,s]=e.calculateTextureWidthAndHeight(d,0),r=d.length,i=new Array(r),a=new Array(r),p=` +value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),mv=(e,t,n)=>yr(e,t,n,"ReduceProd",()=>["value = 1.0;","value *= _A(inputIdx);",""]),gv=(e,t,n)=>yr(e,t,n,"ReduceLogSum",()=>["value = 0.0;","value += _A(inputIdx);","value = log(value);"]),bv=(e,t,n)=>yr(e,t,n,"ReduceLogSumSquare",()=>["float t; value = 0.0;","t = _A(inputIdx); value += t * t;",""])}),yv,nA=D(()=>{"use strict";Me(),yv=(e,t)=>{let n=ye.calculateReshapedDims(t[0].dims,t[1].integerData);return e.session.pack?[e.reshapePacked(t[0],n)]:[e.reshapeUnpacked(t[0],n)]}}),ka,yl,wv,_v,ki,Kf,wl,qo,vv=D(()=>{"use strict";ut(),Ke(),Pe(),ka={name:"Upsample",inputNames:["X"],inputTypes:[0]},yl=(e,t,n)=>(wl(t,n),[e.run({...ka,cacheHint:n.cacheKey,get:()=>Kf(e,t,n)},t)]),wv=e=>ki(e,7),_v=e=>ki(e,9),ki=(e,t)=>{let n=t>=10,i=e.attributes.getString("mode","nearest");if(i!=="nearest"&&i!=="linear"&&(t<11||i!=="cubic"))throw new Error(`unrecognized mode: ${i}`);let u=[];t<9&&(u=e.attributes.getFloats("scales"),qo(u,i,n));let l=e.attributes.getFloat("extrapolation_value",0),d=t>10?e.attributes.getString("coordinate_transformation_mode","half_pixel"):"asymmetric";if(["asymmetric","pytorch_half_pixel","tf_half_pixel_for_nn","align_corners","tf_crop_and_resize","half_pixel"].indexOf(d)===-1)throw new Error(`coordinate_transform_mode '${d}' is not supported`);let c=d==="tf_crop_and_resize",s=c,r=i==="nearest"&&t>=11?e.attributes.getString("nearest_mode","round_prefer_floor"):"";if(["round_prefer_floor","round_prefer_ceil","floor","ceil",""].indexOf(r)===-1)throw new Error(`nearest_mode '${r}' is not supported`);let o=e.attributes.getFloat("cubic_coeff_a",-.75),a=e.attributes.getInt("exclude_outside",0)!==0;if(a&&i!=="cubic")throw new Error("exclude_outside can be set to 1 only when mode is CUBIC.");let p=t<11?!0:i==="nearest"&&d==="asymmetric"&&r==="floor",h=0,m=0,g=0;return t>10?e.inputs.length>2?(h=1,m=2,g=3):(m=1,g=2):t===9&&(m=1),je({opset:t,isResize:n,mode:i,scales:u,extrapolationValue:l,coordinateTransformMode:d,useExtrapolation:s,needRoiInput:c,nearestMode:r,cubicCoefficientA:o,excludeOutside:a,useNearest2xOptimization:p,roiInputIdx:h,scalesInputIdx:m,sizesInputIdx:g})},Kf=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t[0].dims,0),d=t[0].dims.map((g,_)=>Math.floor(g*n.scales[_])),[c,s]=e.calculateTextureWidthAndHeight(d,0),r=d.length,o=new Array(r),a=new Array(r),p=` int output_pitches[${r}]; int input_pitches[${r}]; - `;for(let b=r-1;b>=0;b--)i[b]=b===r-1?1:i[b+1]*d[b+1],a[b]=b===r-1?1:a[b+1]*t[0].dims[b+1],p+=` - output_pitches[${b}] = ${i[b]}; - input_pitches[${b}] = ${a[b]}; + `;for(let g=r-1;g>=0;g--)o[g]=g===r-1?1:o[g+1]*d[g+1],a[g]=g===r-1?1:a[g+1]*t[0].dims[g+1],p+=` + output_pitches[${g}] = ${o[g]}; + input_pitches[${g}] = ${a[g]}; `;let h=` float getInputFloat(int index) { vec2 coords = offsetToCoords(index, ${u}, ${l}); - float value = getColorAsFloat(${o.texture2D}(X, coords)); + float value = getColorAsFloat(${i.texture2D}(X, coords)); return value; } `,m=n.mode==="nearest"?` @@ -1128,10 +1128,10 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( float y0 = x00 + float(y_offset) * (x01 - x00) / float(scales[0]); float y1 = x10 + float(y_offset) * (x11 - x10) / float(scales[0]); return y0 + float(x_offset) * (y1 - y0) / float(scales[1]); - }`;return{...na,output:{dims:d,type:t[0].type,textureType:0},shaderSource:m,variables:[{name:"scales",type:"int",arrayLength:n.scales.length,data:n.scales.map(b=>Math.ceil(b))}]}},Hu=(e,t)=>{if(!e||t.opset<9&&e.length!==1||t.opset>=9&&t.opset<11&&e.length!==2||t.opset>=11&&e.length<2)throw new Error("invalid inputs.");if(t.scales.length>0&&e[0].dims.length!==t.scales.length)throw new Error("Invalid input shape.");if(e[0].type==="string")throw new Error("Invalid input tensor types.")},To=(e,t,n)=>{if(n){for(let o of e)if(o<=0)throw new Error("Scale value should be greater than 0.")}else for(let o of e)if(o<1)throw new Error("Scale value should be greater than or equal to 1.");if((t==="linear"||t==="cubic")&&e.length!==2&&(e.length!==4||e[0]!==1||e[1]!==1))throw new Error(`'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the ${n?"Resize":"Upsample"} opeartor.`)}}),eo,Wu,Lw,jw,hf,ff,mf,gf,QO=D(()=>{"use strict";Ge(),Ie(),Zt(),Sn(),Fw(),eo={name:"Resize",inputNames:["A"],inputTypes:[2]},Wu=(e,t,n)=>(Hu(t,n),[e.run({...eo,cacheHint:n.cacheKey,get:()=>hf(e,t,n)},t)]),Lw=e=>hi(e,10),jw=e=>hi(e,11),hf=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),[u,l]=ff(t,n);if(u.every(y=>y===1)&&n.coordinateTransformMode!=="tf_crop_and_resize")return{...eo,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:`void main() { - vec4 v = ${o.texture2D}(X, TexCoords); - ${o.output} = v; - }`};let d=l.length;if(d<2)throw new Error(`output dimension should be at least 2, but got ${d}`);let c=l[d-2],s=l[d-1],r=t[0].dims;if(d!==r.length)throw new Error(`output dimension should match input ${r.length}, but got ${d}`);let i=r[d-2],a=r[d-1],p=u[d-2],h=u[d-1],m="";if(n.mode!=="linear")throw new Error(`resize (packed) does not support mode: '${n.mode}'`);switch(n.coordinateTransformMode){case"asymmetric":m=` + }`;return{...ka,output:{dims:d,type:t[0].type,textureType:0},shaderSource:m,variables:[{name:"scales",type:"int",arrayLength:n.scales.length,data:n.scales.map(g=>Math.ceil(g))}]}},wl=(e,t)=>{if(!e||t.opset<9&&e.length!==1||t.opset>=9&&t.opset<11&&e.length!==2||t.opset>=11&&e.length<2)throw new Error("invalid inputs.");if(t.scales.length>0&&e[0].dims.length!==t.scales.length)throw new Error("Invalid input shape.");if(e[0].type==="string")throw new Error("Invalid input tensor types.")},qo=(e,t,n)=>{if(n){for(let i of e)if(i<=0)throw new Error("Scale value should be greater than 0.")}else for(let i of e)if(i<1)throw new Error("Scale value should be greater than or equal to 1.");if((t==="linear"||t==="cubic")&&e.length!==2&&(e.length!==4||e[0]!==1||e[1]!==1))throw new Error(`'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the ${n?"Resize":"Upsample"} opeartor.`)}}),vo,_l,xv,$v,Xf,Zf,Yf,Jf,iA=D(()=>{"use strict";Ke(),Pe(),ar(),jn(),vv(),vo={name:"Resize",inputNames:["A"],inputTypes:[2]},_l=(e,t,n)=>(wl(t,n),[e.run({...vo,cacheHint:n.cacheKey,get:()=>Xf(e,t,n)},t)]),xv=e=>ki(e,10),$v=e=>ki(e,11),Xf=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),[u,l]=Zf(t,n);if(u.every(y=>y===1)&&n.coordinateTransformMode!=="tf_crop_and_resize")return{...vo,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:`void main() { + vec4 v = ${i.texture2D}(X, TexCoords); + ${i.output} = v; + }`};let d=l.length;if(d<2)throw new Error(`output dimension should be at least 2, but got ${d}`);let c=l[d-2],s=l[d-1],r=t[0].dims;if(d!==r.length)throw new Error(`output dimension should match input ${r.length}, but got ${d}`);let o=r[d-2],a=r[d-1],p=u[d-2],h=u[d-1],m="";if(n.mode!=="linear")throw new Error(`resize (packed) does not support mode: '${n.mode}'`);switch(n.coordinateTransformMode){case"asymmetric":m=` vec4 getSourceFracIndex(ivec4 coords) { return vec4(coords) / scaleWHWH; } @@ -1153,21 +1153,21 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( vec4 getSourceFracIndex(ivec4 coords) { vec4 resized = vec4(${s}.0 - 1.0, ${c}.0 - 1.0, ${s}.0 - 1.0, ${c}.0 - 1.0); - vec4 original = vec4(${a}.0 - 1.0, ${i}.0 - 1.0, ${a}.0 - 1.0, - ${i}.0 - 1.0); + vec4 original = vec4(${a}.0 - 1.0, ${o}.0 - 1.0, ${a}.0 - 1.0, + ${o}.0 - 1.0); vec4 new_scale = original / resized; return vec4(coords) * new_scale; } - `;break;default:throw new Error(`resize (packed) does not support coordinateTransformMode: '${n.coordinateTransformMode}'`)}let b=br(d),$=wi(),_=` - const vec2 inputWH = vec2(${i}.0, ${a}.0); + `;break;default:throw new Error(`resize (packed) does not support coordinateTransformMode: '${n.coordinateTransformMode}'`)}let g=Ar(d),_=zi(),w=` + const vec2 inputWH = vec2(${o}.0, ${a}.0); const vec4 scaleWHWH = vec4(float(${p}), float(${h}), float(${p}), float(${h})); - ${$} + ${_} ${m} float getAValue(int x10, int r, int c, int d) { return getChannel(getA(x10, r, c, d), vec2(c, d)); } void main() { - ${b} rc = getOutputCoords(); + ${g} rc = getOutputCoords(); int batch = rc[0]; int depth = rc[1]; @@ -1223,20 +1223,20 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( vec4 bottom = mix(bottomLeft, bottomRight, clampFrac.ywyw); vec4 newValue = mix(top, bottom, clampFrac.xxzz); - ${o.output} = vec4(newValue); + ${i.output} = vec4(newValue); } - `;return{...eo,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:_}},ff=(e,t)=>{let n=e[0].dims,o=t.scales,u;if(o.length===0){let d=e[t.scalesInputIdx];if(d&&d.size!==0){if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");o=mf(d,t.mode,t.isResize)}else{let c=e[t.sizesInputIdx];if(!c||c.size===0)throw new Error("Either scales or sizes MUST be provided as input.");u=Array.from(c.integerData),o=gf(u,n,t.mode,t.isResize)}}else if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");let l=u||n.map((d,c)=>Math.floor(d*o[c]));return[o,l]},mf=(e,t,n)=>{let o=Array.from(e.floatData);return To(o,t,n),o},gf=(e,t,n,o)=>{let u=t.length,l=new Array(u);for(let d=0,c=u;d{"use strict";Tn(),Uw=(e,t)=>(bf(t),[new mt([t[0].dims.length],"int32",void 0,void 0,new Int32Array(t[0].dims))]),bf=e=>{if(!e||e.length!==1)throw new Error("Shape requires 1 input.")}}),to,Vw,qw,ia,yf,Gw,_f,wf,tE=D(()=>{"use strict";rt(),Fo(),Ce(),Ie(),to={name:"Slice",inputNames:["A"],inputTypes:[0]},Vw=(e,t,n)=>(yf(t),[e.run({...to,cacheHint:n.cacheKey,get:()=>ia(e,t[0],n)},t)]),qw=e=>{let t=e.attributes.getInts("starts"),n=e.attributes.getInts("ends"),o=e.attributes.getInts("axes",[]);return Be({starts:t,ends:n,axes:o})},ia=(e,t,n)=>{let o=n.axes.length===0?t.dims.slice(0).map((i,a)=>a):n.axes,u=me.normalizeAxes(o,t.dims.length),l=n.starts.map((i,a)=>i>t.dims[u[a]]-1?t.dims[u[a]]:me.normalizeAxis(i,t.dims[u[a]])),d=n.ends.map((i,a)=>i>t.dims[u[a]]-1?t.dims[u[a]]:me.normalizeAxis(i,t.dims[u[a]])),c=t.dims.slice(),s=[];for(let i=0;i0&&s.push(`outputIdx[${u[i]}] += ${l[i]};`);let r=` + `;return{...vo,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:w}},Zf=(e,t)=>{let n=e[0].dims,i=t.scales,u;if(i.length===0){let d=e[t.scalesInputIdx];if(d&&d.size!==0){if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");i=Yf(d,t.mode,t.isResize)}else{let c=e[t.sizesInputIdx];if(!c||c.size===0)throw new Error("Either scales or sizes MUST be provided as input.");u=Array.from(c.integerData),i=Jf(u,n,t.mode,t.isResize)}}else if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");let l=u||n.map((d,c)=>Math.floor(d*i[c]));return[i,l]},Yf=(e,t,n)=>{let i=Array.from(e.floatData);return qo(i,t,n),i},Jf=(e,t,n,i)=>{let u=t.length,l=new Array(u);for(let d=0,c=u;d{"use strict";Ln(),Tv=(e,t)=>(Qf(t),[new xt([t[0].dims.length],"int32",void 0,void 0,new Int32Array(t[0].dims))]),Qf=e=>{if(!e||e.length!==1)throw new Error("Shape requires 1 input.")}}),xo,Sv,Iv,Pa,em,Ov,tm,rm,sA=D(()=>{"use strict";ut(),os(),Me(),Pe(),xo={name:"Slice",inputNames:["A"],inputTypes:[0]},Sv=(e,t,n)=>(em(t),[e.run({...xo,cacheHint:n.cacheKey,get:()=>Pa(e,t[0],n)},t)]),Iv=e=>{let t=e.attributes.getInts("starts"),n=e.attributes.getInts("ends"),i=e.attributes.getInts("axes",[]);return je({starts:t,ends:n,axes:i})},Pa=(e,t,n)=>{let i=n.axes.length===0?t.dims.slice(0).map((o,a)=>a):n.axes,u=ye.normalizeAxes(i,t.dims.length),l=n.starts.map((o,a)=>o>t.dims[u[a]]-1?t.dims[u[a]]:ye.normalizeAxis(o,t.dims[u[a]])),d=n.ends.map((o,a)=>o>t.dims[u[a]]-1?t.dims[u[a]]:ye.normalizeAxis(o,t.dims[u[a]])),c=t.dims.slice(),s=[];for(let o=0;o0&&s.push(`outputIdx[${u[o]}] += ${l[o]};`);let r=` float process(int outputIdx[${c.length}]) { ${s.join(` `)} return _A(outputIdx); - }`;return{...to,output:{dims:c,type:t.type,textureType:0},shaderSource:r}},yf=e=>{if(!e||e.length!==1)throw new Error("Slice requires 1 input.");if(xi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},Gw=(e,t)=>{wf(t);let n=_f(e,t);return[e.run({...to,cacheHint:n.cacheKey,get:()=>ia(e,t[0],n)},[t[0]])]},_f=(e,t)=>{if(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)||t.length>=4&&!e.session.isInitializer(t[3].dataId)||t.length>=5&&!e.session.isInitializer(t[4].dataId))throw new Error("dynamic slice attributes are not allowed");if(t.length>=5&&t[4].integerData.some(d=>d!==1))throw new Error("currently non-1 steps is not supported for Slice");let n=Array.from(t[1].integerData),o=Array.from(t[2].integerData),u=t.length>=4?Array.from(t[3].integerData):[],l=`${u};${n};${o}`;return{starts:n,ends:o,axes:u,cacheKey:l}},wf=e=>{if(!e||e.length<3||e.length>5)throw new Error("Invalid input number.");if(e[1].type!=="int32"||e[1].dims.length!==1)throw new Error("Invalid input type.");if(e[2].type!=="int32"||e[2].dims.length!==1)throw new Error("Invalid input type.");if(e.length>=4&&(e[3].type!=="int32"||e[3].dims.length!==1))throw new Error("Invalid input type.");if(e.length>=5&&(e[4].type!=="int32"||e[4].dims.length!==1))throw new Error("Invalid input type.")}}),oa,sa,aa,Hw,Ww,Kw,Xw,ua,vf,xf,$f,la,rE=D(()=>{"use strict";rt(),Ce(),Ge(),Ie(),Sl(),oa={name:"SoftmaxComputeMax",inputNames:["A"],inputTypes:[0]},sa={name:"SoftmaxComputeScale",inputNames:["A","Max"],inputTypes:[0,0]},aa={name:"SoftMax",inputNames:["A","Max","Norm"],inputTypes:[0,0,0]},Hw=(e,t,n)=>{la(t);let o=t[0].dims.slice(),u=me.normalizeAxis(n.axis,o.length),l=me.sizeToDimension(o,u),d=me.sizeFromDimension(o,u);return ua(e,t,n,l,d)},Ww=e=>Be({axis:e.attributes.getInt("axis",1)}),Kw=e=>Be({axis:e.attributes.getInt("axis",-1)}),Xw=(e,t,n)=>{la(t);let o=t[0].dims.slice(),u=me.normalizeAxis(n.axis,o.length),l=o.length,d=u!==l-1,c=[],s=[],r=[],i;d&&(s=Array.from({length:l}).map((m,b)=>b),s[u]=l-1,s[l-1]=u,s.map(m=>c.push(o[m])),i=Be({perm:s}),r=bi(e,t,i));let a=d?me.sizeToDimension(c,l-1):me.sizeToDimension(o,l-1),p=d?me.sizeFromDimension(c,l-1):me.sizeFromDimension(o,l-1),h=ua(e,d?r:t,n,a,p);return d?bi(e,h,i):h},ua=(e,t,n,o,u)=>{let l=vf(e,t[0],o,u,[o]),d=e.run({...oa,cacheHint:n.cacheKey,get:()=>l},t),c=xf(e,t[0],o,u,l.output.dims,[o]),s=e.run({...sa,cacheHint:n.cacheKey,get:()=>c},[t[0],d]),r=$f(e,t[0],o,u,l.output.dims,c.output.dims);return[e.run({...aa,cacheHint:n.cacheKey,get:()=>r},[t[0],d,s])]},vf=(e,t,n,o,u)=>{let[l,d]=e.calculateTextureWidthAndHeight(t.dims,0),c=u.length;if(n<1||o<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1)throw new Error("Dimensionality of the output should be 1");if(u[0]!==n)throw new Error("Shape of the output should be equal to logical row count");let s=Te(e.session.backend.glContext.version),r=` + }`;return{...xo,output:{dims:c,type:t.type,textureType:0},shaderSource:r}},em=e=>{if(!e||e.length!==1)throw new Error("Slice requires 1 input.");if(Fi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},Ov=(e,t)=>{rm(t);let n=tm(e,t);return[e.run({...xo,cacheHint:n.cacheKey,get:()=>Pa(e,t[0],n)},[t[0]])]},tm=(e,t)=>{if(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)||t.length>=4&&!e.session.isInitializer(t[3].dataId)||t.length>=5&&!e.session.isInitializer(t[4].dataId))throw new Error("dynamic slice attributes are not allowed");if(t.length>=5&&t[4].integerData.some(d=>d!==1))throw new Error("currently non-1 steps is not supported for Slice");let n=Array.from(t[1].integerData),i=Array.from(t[2].integerData),u=t.length>=4?Array.from(t[3].integerData):[],l=`${u};${n};${i}`;return{starts:n,ends:i,axes:u,cacheKey:l}},rm=e=>{if(!e||e.length<3||e.length>5)throw new Error("Invalid input number.");if(e[1].type!=="int32"||e[1].dims.length!==1)throw new Error("Invalid input type.");if(e[2].type!=="int32"||e[2].dims.length!==1)throw new Error("Invalid input type.");if(e.length>=4&&(e[3].type!=="int32"||e[3].dims.length!==1))throw new Error("Invalid input type.");if(e.length>=5&&(e[4].type!=="int32"||e[4].dims.length!==1))throw new Error("Invalid input type.")}}),Ca,Ra,Da,Ev,Av,kv,Pv,Ma,nm,im,om,Na,aA=D(()=>{"use strict";ut(),Me(),Ke(),Pe(),Jl(),Ca={name:"SoftmaxComputeMax",inputNames:["A"],inputTypes:[0]},Ra={name:"SoftmaxComputeScale",inputNames:["A","Max"],inputTypes:[0,0]},Da={name:"SoftMax",inputNames:["A","Max","Norm"],inputTypes:[0,0,0]},Ev=(e,t,n)=>{Na(t);let i=t[0].dims.slice(),u=ye.normalizeAxis(n.axis,i.length),l=ye.sizeToDimension(i,u),d=ye.sizeFromDimension(i,u);return Ma(e,t,n,l,d)},Av=e=>je({axis:e.attributes.getInt("axis",1)}),kv=e=>je({axis:e.attributes.getInt("axis",-1)}),Pv=(e,t,n)=>{Na(t);let i=t[0].dims.slice(),u=ye.normalizeAxis(n.axis,i.length),l=i.length,d=u!==l-1,c=[],s=[],r=[],o;d&&(s=Array.from({length:l}).map((m,g)=>g),s[u]=l-1,s[l-1]=u,s.map(m=>c.push(i[m])),o=je({perm:s}),r=Di(e,t,o));let a=d?ye.sizeToDimension(c,l-1):ye.sizeToDimension(i,l-1),p=d?ye.sizeFromDimension(c,l-1):ye.sizeFromDimension(i,l-1),h=Ma(e,d?r:t,n,a,p);return d?Di(e,h,o):h},Ma=(e,t,n,i,u)=>{let l=nm(e,t[0],i,u,[i]),d=e.run({...Ca,cacheHint:n.cacheKey,get:()=>l},t),c=im(e,t[0],i,u,l.output.dims,[i]),s=e.run({...Ra,cacheHint:n.cacheKey,get:()=>c},[t[0],d]),r=om(e,t[0],i,u,l.output.dims,c.output.dims);return[e.run({...Da,cacheHint:n.cacheKey,get:()=>r},[t[0],d,s])]},nm=(e,t,n,i,u)=>{let[l,d]=e.calculateTextureWidthAndHeight(t.dims,0),c=u.length;if(n<1||i<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1)throw new Error("Dimensionality of the output should be 1");if(u[0]!==n)throw new Error("Shape of the output should be equal to logical row count");let s=Ae(e.session.backend.glContext.version),r=` float process(int[${c}] indices) { - int logical_row_start_offset = indices[0] * ${o}; + int logical_row_start_offset = indices[0] * ${i}; float max = getColorAsFloat(${s.texture2D}(A, offsetToCoords(logical_row_start_offset, ${l}, ${d} ))); - for(int i=1; i<${o}; ++i) + for(int i=1; i<${i}; ++i) { float current = getColorAsFloat(${s.texture2D}(A, offsetToCoords(logical_row_start_offset + i, ${l}, ${d}))); @@ -1245,20 +1245,20 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( } return max; - }`;return{...oa,output:{dims:u,type:t.type,textureType:0},shaderSource:r}},xf=(e,t,n,o,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=l.length;if(n<1||o<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(l.length!==1)throw new Error("Dimensionality of the output should be 1");if(l[0]!==n)throw new Error("Shape of the output should be equal to logical row count");if(u.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=Te(e.session.backend.glContext.version),i=` + }`;return{...Ca,output:{dims:u,type:t.type,textureType:0},shaderSource:r}},im=(e,t,n,i,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=l.length;if(n<1||i<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(l.length!==1)throw new Error("Dimensionality of the output should be 1");if(l[0]!==n)throw new Error("Shape of the output should be equal to logical row count");if(u.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=Ae(e.session.backend.glContext.version),o=` float process(int[${s}] indices) { - int logical_row_start_offset = indices[0] * ${o}; + int logical_row_start_offset = indices[0] * ${i}; float norm_factor = 0.0; float max = _Max(indices); - for(int i=0; i<${o}; ++i) + for(int i=0; i<${i}; ++i) { norm_factor += exp(getColorAsFloat(${r.texture2D}(A, offsetToCoords(logical_row_start_offset + i, ${d}, ${c}))) - max); } return norm_factor; - }`;return{...sa,output:{dims:l,type:t.type,textureType:0},shaderSource:i}},$f=(e,t,n,o,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=t.dims.length;if(n<1||o<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1||l.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n||l[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=` + }`;return{...Ra,output:{dims:l,type:t.type,textureType:0},shaderSource:o}},om=(e,t,n,i,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=t.dims.length;if(n<1||i<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1||l.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n||l[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=` float process(int[${s}] indices) { // get offset of current logical tensor index from the 2-D texture coordinates (TexCoords) @@ -1266,7 +1266,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( //determine the logical row for this index int logical_row_index[1]; - logical_row_index[0] = offset / ${o}; + logical_row_index[0] = offset / ${i}; float norm_factor = _Norm(logical_row_index); @@ -1277,80 +1277,80 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( return 0.0; return exp(_A(indices) - _Max(logical_row_index)) / norm_factor; - }`;return{...aa,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:r}},la=e=>{if(!e||e.length!==1)throw new Error("Softmax requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type")}}),da,Zw,Yw,Tf,Sf,If,nE=D(()=>{"use strict";rt(),Ce(),Ie(),da={name:"Split",inputNames:["A"],inputTypes:[0]},Zw=(e,t,n)=>{If(t);let o=me.normalizeAxis(n.axis,t[0].dims.length),u=Tf(e,t,o,n),l=[];for(let d=0;dSf(e,t[0],n,o,d)},t));return l},Yw=e=>{let t=e.attributes.getInt("axis",0),n=e.attributes.getInts("split",[]),o=e.outputs.length;return Be({axis:t,split:n,numOutputs:o})},Tf=(e,t,n,o)=>{let[,u]=Pu.splitShape(t[0].dims,n,o.split,o.numOutputs);return u.length},Sf=(e,t,n,o,u)=>{let[l,d]=Pu.splitShape(t.dims,o,n.split,n.numOutputs),c=d[u],s=l[u],r=` + }`;return{...Da,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:r}},Na=e=>{if(!e||e.length!==1)throw new Error("Softmax requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type")}}),za,Cv,Rv,sm,am,um,uA=D(()=>{"use strict";ut(),Me(),Pe(),za={name:"Split",inputNames:["A"],inputTypes:[0]},Cv=(e,t,n)=>{um(t);let i=ye.normalizeAxis(n.axis,t[0].dims.length),u=sm(e,t,i,n),l=[];for(let d=0;dam(e,t[0],n,i,d)},t));return l},Rv=e=>{let t=e.attributes.getInt("axis",0),n=e.attributes.getInts("split",[]),i=e.outputs.length;return je({axis:t,split:n,numOutputs:i})},sm=(e,t,n,i)=>{let[,u]=il.splitShape(t[0].dims,n,i.split,i.numOutputs);return u.length},am=(e,t,n,i,u)=>{let[l,d]=il.splitShape(t.dims,i,n.split,n.numOutputs),c=d[u],s=l[u],r=` float process(int indices[${s.length}]) { - indices[${o}] += ${c}; + indices[${i}] += ${c}; return _A(indices); } - `;return{...da,cacheHint:`${n.cacheKey}:${u}`,output:{dims:s,type:t.type,textureType:0},shaderSource:r}},If=e=>{if(!e||e.length!==1)throw new Error("Split requires one input.");if(e[0].type!=="int8"&&e[0].type!=="uint8"&&e[0].type!=="int16"&&e[0].type!=="uint16"&&e[0].type!=="int32"&&e[0].type!=="uint32"&&e[0].type!=="float32"&&e[0].type!=="float64"&&e[0].type!=="bool")throw new Error("Invalid input type.")}}),Ku,Jw,Qw,Of,Ef,iE=D(()=>{"use strict";Ce(),Ku=(e,t,n)=>{Of(t);let o=me.squeezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],o)]},Jw=(e,t)=>(Ef(t),Ku(e,[t[0]],Array.from(t[1].integerData))),Qw=e=>e.attributes.getInts("axes"),Of=e=>{if(!e||e.length!==1)throw new Error("Squeeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},Ef=e=>{if(!e||e.length!==2)throw new Error("Squeeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),ev,Af,kf,oE=D(()=>{"use strict";Ge(),Ie(),ev=(e,t)=>{kf(t);let n={name:"Sum",inputNames:t.map((o,u)=>`X${u}`),inputTypes:new Array(t.length).fill(0)};return[e.run({...n,get:()=>Af(e,t,n)},t)]},Af=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),u=t[0].dims.slice(),l=` + `;return{...za,cacheHint:`${n.cacheKey}:${u}`,output:{dims:s,type:t.type,textureType:0},shaderSource:r}},um=e=>{if(!e||e.length!==1)throw new Error("Split requires one input.");if(e[0].type!=="int8"&&e[0].type!=="uint8"&&e[0].type!=="int16"&&e[0].type!=="uint16"&&e[0].type!=="int32"&&e[0].type!=="uint32"&&e[0].type!=="float32"&&e[0].type!=="float64"&&e[0].type!=="bool")throw new Error("Invalid input type.")}}),vl,Dv,Mv,lm,dm,lA=D(()=>{"use strict";Me(),vl=(e,t,n)=>{lm(t);let i=ye.squeezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],i)]},Dv=(e,t)=>(dm(t),vl(e,[t[0]],Array.from(t[1].integerData))),Mv=e=>e.attributes.getInts("axes"),lm=e=>{if(!e||e.length!==1)throw new Error("Squeeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},dm=e=>{if(!e||e.length!==2)throw new Error("Squeeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),Nv,cm,pm,dA=D(()=>{"use strict";Ke(),Pe(),Nv=(e,t)=>{pm(t);let n={name:"Sum",inputNames:t.map((i,u)=>`X${u}`),inputTypes:new Array(t.length).fill(0)};return[e.run({...n,get:()=>cm(e,t,n)},t)]},cm=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),u=t[0].dims.slice(),l=` void main() { - vec4 result = ${t.map((d,c)=>`${o.texture2D}(X${c},TexCoords)`).join(" + ")}; - ${o.output} = result; + vec4 result = ${t.map((d,c)=>`${i.texture2D}(X${c},TexCoords)`).join(" + ")}; + ${i.output} = result; } - `;return{...n,output:{dims:u,type:t[0].type,textureType:0},hasMain:!0,shaderSource:l}},kf=e=>{if(!e||e.length===0)throw new Error("Sum requires inputs.");let t=e[0].dims.length;for(let n=1;n{"use strict";Fo(),Ie(),tv=(e,t)=>{Cf(t);let n={name:"Tile",inputNames:["A"],inputTypes:[0]};return[e.run({...n,get:()=>Pf(e,t,n)},t)]},Pf=(e,t,n)=>{let o=t[0].dims.slice(),u=new Array(o.length),l=[];for(let s=0;s{if(!e||e.length===0)throw new Error("Sum requires inputs.");let t=e[0].dims.length;for(let n=1;n{"use strict";os(),Pe(),zv=(e,t)=>{fm(t);let n={name:"Tile",inputNames:["A"],inputTypes:[0]};return[e.run({...n,get:()=>hm(e,t,n)},t)]},hm=(e,t,n)=>{let i=t[0].dims.slice(),u=new Array(i.length),l=[];for(let s=0;s{if(!e||e.length!==2)throw new Error("Tile requires 2 input.");if(e[1].dims.length!==1)throw new Error("The second input shape must 1 dimension.");if(e[1].dims[0]!==e[0].dims.length)throw new Error("Invalid input shape.");if(xi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invalid repeat type.")}}),Xu,rv,nv,Df,Rf,aE=D(()=>{"use strict";Ce(),Xu=(e,t,n)=>{Df(t);let o=me.unsqueezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],o)]},rv=(e,t)=>(Rf(t),Xu(e,[t[0]],Array.from(t[1].integerData))),nv=e=>e.attributes.getInts("axes"),Df=e=>{if(!e||e.length!==1)throw new Error("Unsqueeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},Rf=e=>{if(!e||e.length!==2)throw new Error("Unsqueeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),iv,uE=D(()=>{"use strict";X3(),lO(),dO(),pO(),Tl(),FO(),LO(),jO(),UO(),VO(),qO(),GO(),KO(),xl(),XO(),ZO(),YO(),JO(),QO(),eE(),tE(),rE(),nE(),iE(),oE(),sE(),Sl(),V_(),aE(),Fw(),iv=[["Abs","","6+",w_],["Acos","","7+",v_],["Add","","7+",e_],["And","","7+",t_],["Asin","","7+",x_],["Atan","","7+",$_],["AveragePool","","7+",xw,$w],["BatchNormalization","","7+",Z0,Y0],["Cast","","6+",p_,h_],["Ceil","","6+",I_],["Clip","","6-10",Bu,T_],["Clip","","11+",S_],["Concat","","4+",m_,g_],["Conv","","1+",ju,Uu],["ConvTranspose","","1+",rw,nw],["Cos","","7+",O_],["Div","","7+",r_],["Dropout","","7+",Fu],["DepthToSpace","","1+",ow,sw],["Equal","","7+",n_],["Elu","","6+",E_,A_],["Exp","","6+",k_],["Flatten","","1+",aw,uw],["Floor","","6+",P_],["FusedConv","com.microsoft","1+",ju,Uu],["Gather","","1+",lw,dw],["Gemm","","7-10",Vu,cw],["Gemm","","11+",Vu,pw],["GlobalAveragePool","","1+",Tw,Sw],["GlobalMaxPool","","1+",Ew],["Greater","","7+",i_],["Identity","","1+",Fu],["ImageScaler","","1+",hw,fw],["InstanceNormalization","","6+",mw,gw],["LeakyRelu","","6+",C_,D_],["Less","","7+",o_],["LRN","","1+",bw,yw],["Log","","6+",R_],["MatMul","","1+",K_,X_],["MaxPool","","1+",Iw,Ow],["Mul","","7+",s_],["Neg","","6+",N_],["Not","","1+",z_],["Or","","7+",a_],["Pad","","2-10",qu,_w],["Pad","","11+",ww,vw],["Pow","","7+",u_],["PRelu","","7+",l_],["ReduceLogSum","","1+",Rw,mr],["ReduceMax","","1+",Pw,mr],["ReduceMean","","1+",kw,mr],["ReduceMin","","1+",Cw,mr],["ReduceProd","","1+",Dw,mr],["ReduceSum","","1-12",Aw,mr],["ReduceSumSquare","","1+",Nw,mr],["Relu","","6+",M_],["Reshape","","5+",zw],["Resize","","10",Wu,Lw],["Resize","","11+",Wu,jw],["Shape","","1+",Uw],["Sigmoid","","6+",B_],["Sin","","7+",F_],["Slice","","10+",Gw],["Slice","","1-9",Vw,qw],["Softmax","","1-12",Hw,Ww],["Softmax","","13+",Xw,Kw],["Split","","2-12",Zw,Yw],["Sqrt","","6+",L_],["Squeeze","","1-12",Ku,Qw],["Squeeze","","13+",Jw],["Sub","","7+",d_],["Sum","","6+",ev],["Tan","","7+",j_],["Tanh","","6+",U_],["Tile","","6+",tv],["Transpose","","1+",bi,iw],["Upsample","","7-8",Gu,Mw],["Upsample","","9",Gu,Bw],["Unsqueeze","","1-12",Xu,nv],["Unsqueeze","","13+",rv],["Xor","","7+",c_]]});function lE(e){let t={},n;for(;(n=Zu.exec(e))!==null;){let o=n[3].split(",").map(u=>{let l=u.trim().split(" ");return l&&l.length===2?{type:l[0],name:l[1]}:null}).filter(u=>u!==null);t[n[2]]={params:o,body:n[4]}}for(let o in t){let u=ov.replace("__FUNC__",o),l=new RegExp(u,"gm");for(;(n=l.exec(e))!==null;){let d=n[1],c=n[2],s=n[3].split(","),r=d?`${d} ${c};`:"",i=t[o].body,a="";t[o].params.forEach((h,m)=>{h&&(a+=`${h.type} ${h.name} = ${s[m]}; -`)}),i=`${a} - ${i}`,i=i.replace("return",`${c} = `);let p=` + `;return{...n,output:{dims:u,type:t[0].type,textureType:0},shaderSource:c}},fm=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 input.");if(e[1].dims.length!==1)throw new Error("The second input shape must 1 dimension.");if(e[1].dims[0]!==e[0].dims.length)throw new Error("Invalid input shape.");if(Fi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invalid repeat type.")}}),xl,Bv,Fv,mm,gm,pA=D(()=>{"use strict";Me(),xl=(e,t,n)=>{mm(t);let i=ye.unsqueezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],i)]},Bv=(e,t)=>(gm(t),xl(e,[t[0]],Array.from(t[1].integerData))),Fv=e=>e.attributes.getInts("axes"),mm=e=>{if(!e||e.length!==1)throw new Error("Unsqueeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},gm=e=>{if(!e||e.length!==2)throw new Error("Unsqueeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),Lv,hA=D(()=>{"use strict";eE(),fE(),mE(),bE(),Yl(),qE(),GE(),HE(),WE(),KE(),XE(),ZE(),QE(),Xl(),eA(),tA(),rA(),nA(),iA(),oA(),sA(),aA(),uA(),lA(),dA(),cA(),Jl(),S_(),pA(),vv(),Lv=[["Abs","","6+",r_],["Acos","","7+",n_],["Add","","7+",Nw],["And","","7+",zw],["Asin","","7+",i_],["Atan","","7+",o_],["AveragePool","","7+",iv,ov],["BatchNormalization","","7+",Cw,Rw],["Cast","","6+",Kw,Xw],["Ceil","","6+",u_],["Clip","","6-10",cl,s_],["Clip","","11+",a_],["Concat","","4+",Yw,Jw],["Conv","","1+",fl,ml],["ConvTranspose","","1+",B_,F_],["Cos","","7+",l_],["Div","","7+",Bw],["Dropout","","7+",pl],["DepthToSpace","","1+",j_,U_],["Equal","","7+",Fw],["Elu","","6+",d_,c_],["Exp","","6+",p_],["Flatten","","1+",V_,q_],["Floor","","6+",h_],["FusedConv","com.microsoft","1+",fl,ml],["Gather","","1+",G_,H_],["Gemm","","7-10",gl,W_],["Gemm","","11+",gl,K_],["GlobalAveragePool","","1+",sv,av],["GlobalMaxPool","","1+",dv],["Greater","","7+",Lw],["Identity","","1+",pl],["ImageScaler","","1+",X_,Z_],["InstanceNormalization","","6+",Y_,J_],["LeakyRelu","","6+",f_,m_],["Less","","7+",jw],["LRN","","1+",Q_,ev],["Log","","6+",g_],["MatMul","","1+",k_,P_],["MaxPool","","1+",uv,lv],["Mul","","7+",Uw],["Neg","","6+",b_],["Not","","1+",y_],["Or","","7+",Vw],["Pad","","2-10",bl,tv],["Pad","","11+",rv,nv],["Pow","","7+",qw],["PRelu","","7+",Gw],["ReduceLogSum","","1+",gv,Or],["ReduceMax","","1+",hv,Or],["ReduceMean","","1+",pv,Or],["ReduceMin","","1+",fv,Or],["ReduceProd","","1+",mv,Or],["ReduceSum","","1-12",cv,Or],["ReduceSumSquare","","1+",bv,Or],["Relu","","6+",w_],["Reshape","","5+",yv],["Resize","","10",_l,xv],["Resize","","11+",_l,$v],["Shape","","1+",Tv],["Sigmoid","","6+",__],["Sin","","7+",v_],["Slice","","10+",Ov],["Slice","","1-9",Sv,Iv],["Softmax","","1-12",Ev,Av],["Softmax","","13+",Pv,kv],["Split","","2-12",Cv,Rv],["Sqrt","","6+",x_],["Squeeze","","1-12",vl,Mv],["Squeeze","","13+",Dv],["Sub","","7+",Hw],["Sum","","6+",Nv],["Tan","","7+",$_],["Tanh","","6+",T_],["Tile","","6+",zv],["Transpose","","1+",Di,L_],["Upsample","","7-8",yl,wv],["Upsample","","9",yl,_v],["Unsqueeze","","1-12",xl,Fv],["Unsqueeze","","13+",Bv],["Xor","","7+",Ww]]});function fA(e){let t={},n;for(;(n=$l.exec(e))!==null;){let i=n[3].split(",").map(u=>{let l=u.trim().split(" ");return l&&l.length===2?{type:l[0],name:l[1]}:null}).filter(u=>u!==null);t[n[2]]={params:i,body:n[4]}}for(let i in t){let u=jv.replace("__FUNC__",i),l=new RegExp(u,"gm");for(;(n=l.exec(e))!==null;){let d=n[1],c=n[2],s=n[3].split(","),r=d?`${d} ${c};`:"",o=t[i].body,a="";t[i].params.forEach((h,m)=>{h&&(a+=`${h.type} ${h.name} = ${s[m]}; +`)}),o=`${a} + ${o}`,o=o.replace("return",`${c} = `);let p=` ${r} { - ${i} + ${o} } - `;e=e.replace(n[0],p)}}return e=e.replace(Zu,""),e}var Zu,ov,dE=D(()=>{"use strict";Zu=/@inline[\s\n\r]+(\w+)[\s\n\r]+([0-9a-zA-Z_]+)\s*\(([^)]*)\)\s*{(([^}]|[\n\r])*)}/gm,ov="(\\w+)?\\s+([_0-9a-zA-Z]+)\\s+=\\s+__FUNC__\\((.*)\\)\\s*;"});function oi(e,t){let n=[],o=[],u=t!=null&&Array.isArray(t)&&t.length===0,l=t==null||u?null:cE(t,e).sort(),d=0;for(let c=0;cc)&&e[c]===1&&(n.push(e[c]),o.push(c)),l[d]<=c&&d++}e[c]!==1&&(n.push(e[c]),o.push(c))}return{newShape:n,keptDims:o}}function cE(e,t){let n=t.length;return e=e==null?t.map((o,u)=>u):[].concat(e),vo(e.every(o=>o>=-n&&o`All values in axis param must be in range [-${n}, ${n}) but got axis ${e}`),vo(e.every(pE),()=>`All values in axis param must be integers but got axis ${e}`),e.map(o=>o<0?n+o:o)}function pE(e){return e%1===0}function hE(e){if(e.length===0)return 1;let t=e[0];for(let n=1;n{"use strict";Rt(),Ce(),sv=class{constructor(e){this.maxTextureSize=e}computeTextureWH(e,t){let n=this.computeTexture(e,t);return t&&t.isPacked&&(n[0]/=2,n[1]/=2),t&&t.reverseWH?[n[1],n[0]]:n}computeTexture(e,t){let n=t&&t.isPacked;if(e.length===0)return n?[2,2]:[1,1];let o=this.maxTextureSize;if(t&&t.breakAxis!==void 0){let d=t.breakAxis>=e.length?1:e.slice(t.breakAxis).reduce((s,r)=>s*r),c=t.breakAxis<=0?1:e.slice(0,t.breakAxis).reduce((s,r)=>s*r);if(d>o||c>o)We.verbose("TextureLayout",`Given width/height preferences were unattainable: shape:${e}, breakAxis:${t.breakAxis}`);else return[d,c]}let u=e.slice(0);n&&(o=o*2,u=u.map((d,c)=>c>=u.length-2?u[c]%2===0?u[c]:u[c]+1:u[c]),u.length===1&&(u=[2,u[0]])),u.length!==2&&(u=oi(u).newShape);let l=hE(u);return u.length<=1&&l<=o?[1,l]:u.length===2&&u[0]<=o&&u[1]<=o?u:u.length===3&&u[0]*u[1]<=o&&u[2]<=o?[u[0]*u[1],u[2]]:u.length===3&&u[0]<=o&&u[1]*u[2]<=o?[u[0],u[1]*u[2]]:u.length===4&&u[0]*u[1]*u[2]<=o&&u[3]<=o?[u[0]*u[1]*u[2],u[3]]:u.length===4&&u[0]<=o&&u[1]*u[2]*u[3]<=o?[u[0],u[1]*u[2]*u[3]]:n?Nf(l/4).map(d=>d*2):Nf(l)}}}),uv,fE=D(()=>{"use strict";Ce(),_r(),Ge(),av(),Zt(),uv=class extends In{constructor(e){super(e)}getFunctions(){return{...this.offsetToCoords(),...this.coordsToOffset(),...this.toVec(),...this.valueFrom(),...this.getCommonUtilFuncs(),...this.getInputsSamplingSnippets(),...this.getOutputSamplingSnippet()}}getCustomTypes(){return{}}offsetToCoords(){let e="offsetToCoords";return{offsetToCoords:new te(` + `;e=e.replace(n[0],p)}}return e=e.replace($l,""),e}var $l,jv,mA=D(()=>{"use strict";$l=/@inline[\s\n\r]+(\w+)[\s\n\r]+([0-9a-zA-Z_]+)\s*\(([^)]*)\)\s*{(([^}]|[\n\r])*)}/gm,jv="(\\w+)?\\s+([_0-9a-zA-Z]+)\\s+=\\s+__FUNC__\\((.*)\\)\\s*;"});function xi(e,t){let n=[],i=[],u=t!=null&&Array.isArray(t)&&t.length===0,l=t==null||u?null:gA(t,e).sort(),d=0;for(let c=0;cc)&&e[c]===1&&(n.push(e[c]),i.push(c)),l[d]<=c&&d++}e[c]!==1&&(n.push(e[c]),i.push(c))}return{newShape:n,keptDims:i}}function gA(e,t){let n=t.length;return e=e==null?t.map((i,u)=>u):[].concat(e),jo(e.every(i=>i>=-n&&i`All values in axis param must be in range [-${n}, ${n}) but got axis ${e}`),jo(e.every(bA),()=>`All values in axis param must be integers but got axis ${e}`),e.map(i=>i<0?n+i:i)}function bA(e){return e%1===0}function yA(e){if(e.length===0)return 1;let t=e[0];for(let n=1;n{"use strict";qt(),Me(),Uv=class{constructor(e){this.maxTextureSize=e}computeTextureWH(e,t){let n=this.computeTexture(e,t);return t&&t.isPacked&&(n[0]/=2,n[1]/=2),t&&t.reverseWH?[n[1],n[0]]:n}computeTexture(e,t){let n=t&&t.isPacked;if(e.length===0)return n?[2,2]:[1,1];let i=this.maxTextureSize;if(t&&t.breakAxis!==void 0){let d=t.breakAxis>=e.length?1:e.slice(t.breakAxis).reduce((s,r)=>s*r),c=t.breakAxis<=0?1:e.slice(0,t.breakAxis).reduce((s,r)=>s*r);if(d>i||c>i)Je.verbose("TextureLayout",`Given width/height preferences were unattainable: shape:${e}, breakAxis:${t.breakAxis}`);else return[d,c]}let u=e.slice(0);n&&(i=i*2,u=u.map((d,c)=>c>=u.length-2?u[c]%2===0?u[c]:u[c]+1:u[c]),u.length===1&&(u=[2,u[0]])),u.length!==2&&(u=xi(u).newShape);let l=yA(u);return u.length<=1&&l<=i?[1,l]:u.length===2&&u[0]<=i&&u[1]<=i?u:u.length===3&&u[0]*u[1]<=i&&u[2]<=i?[u[0]*u[1],u[2]]:u.length===3&&u[0]<=i&&u[1]*u[2]<=i?[u[0],u[1]*u[2]]:u.length===4&&u[0]*u[1]*u[2]<=i&&u[3]<=i?[u[0]*u[1]*u[2],u[3]]:u.length===4&&u[0]<=i&&u[1]*u[2]*u[3]<=i?[u[0],u[1]*u[2]*u[3]]:n?bm(l/4).map(d=>d*2):bm(l)}}}),qv,wA=D(()=>{"use strict";Me(),Pr(),Ke(),Vv(),ar(),qv=class extends Un{constructor(e){super(e)}getFunctions(){return{...this.offsetToCoords(),...this.coordsToOffset(),...this.toVec(),...this.valueFrom(),...this.getCommonUtilFuncs(),...this.getInputsSamplingSnippets(),...this.getOutputSamplingSnippet()}}getCustomTypes(){return{}}offsetToCoords(){let e="offsetToCoords";return{offsetToCoords:new ie(` vec2 ${e}(int offset, int width, int height) { int t = offset / width; int s = offset - t*width; vec2 coords = (vec2(s,t) + vec2(0.5,0.5)) / vec2(width, height); return coords; } - `)}}coordsToOffset(){let e="coordsToOffset";return{coordsToOffset:new te(` + `)}}coordsToOffset(){let e="coordsToOffset";return{coordsToOffset:new ie(` int ${e}(vec2 coords, int width, int height) { float s = coords.s * float(width); float t = coords.t * float(height); int offset = int(t) * width + int(s); return offset; } - `)}}getOutputSamplingSnippet(){let e=this.context.outputTextureLayout;return e.isPacked?this.getPackedOutputSamplingSnippet(e):this.getUnpackedOutputSamplingSnippet(e)}getPackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],o={},u="getOutputCoords";switch(t.length){case 0:o[u]=this.getOutputScalarCoords();break;case 1:o[u]=this.getOutputPacked1DCoords(t,n);break;case 2:o[u]=this.getOutputPacked2DCoords(t,n);break;case 3:o[u]=this.getOutputPacked3DCoords(t,n);break;default:o[u]=this.getOutputPackedNDCoords(t,n)}let l=` + `)}}getOutputSamplingSnippet(){let e=this.context.outputTextureLayout;return e.isPacked?this.getPackedOutputSamplingSnippet(e):this.getUnpackedOutputSamplingSnippet(e)}getPackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],i={},u="getOutputCoords";switch(t.length){case 0:i[u]=this.getOutputScalarCoords();break;case 1:i[u]=this.getOutputPacked1DCoords(t,n);break;case 2:i[u]=this.getOutputPacked2DCoords(t,n);break;case 3:i[u]=this.getOutputPacked3DCoords(t,n);break;default:i[u]=this.getOutputPackedNDCoords(t,n)}let l=` void setOutput(vec4 val) { - ${Te(this.context.glContext.version).output} = val; + ${Ae(this.context.glContext.version).output} = val; } - `,d="floatTextureSetRGBA";return o[d]=new te(l),o}getUnpackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],o={},u="getOutputCoords";switch(t.length){case 0:o[u]=this.getOutputScalarCoords();break;case 1:o[u]=this.getOutputUnpacked1DCoords(t,n);break;case 2:o[u]=this.getOutputUnpacked2DCoords(t,n);break;case 3:o[u]=this.getOutputUnpacked3DCoords(t,n);break;case 4:o[u]=this.getOutputUnpacked4DCoords(t,n);break;case 5:o[u]=this.getOutputUnpacked5DCoords(t,n);break;case 6:o[u]=this.getOutputUnpacked6DCoords(t,n);break;default:throw new Error(`Unsupported output dimensionality: ${t.length}`)}let l=` + `,d="floatTextureSetRGBA";return i[d]=new ie(l),i}getUnpackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],i={},u="getOutputCoords";switch(t.length){case 0:i[u]=this.getOutputScalarCoords();break;case 1:i[u]=this.getOutputUnpacked1DCoords(t,n);break;case 2:i[u]=this.getOutputUnpacked2DCoords(t,n);break;case 3:i[u]=this.getOutputUnpacked3DCoords(t,n);break;case 4:i[u]=this.getOutputUnpacked4DCoords(t,n);break;case 5:i[u]=this.getOutputUnpacked5DCoords(t,n);break;case 6:i[u]=this.getOutputUnpacked6DCoords(t,n);break;default:throw new Error(`Unsupported output dimensionality: ${t.length}`)}let l=` void setOutput(float val) { - ${Te(this.context.glContext.version).output} = vec4(val, 0, 0, 0); + ${Ae(this.context.glContext.version).output} = vec4(val, 0, 0, 0); } - `,d="floatTextureSetR";return o[d]=new te(l),o}getOutputScalarCoords(){return new te(` + `,d="floatTextureSetR";return i[d]=new ie(l),i}getOutputScalarCoords(){return new ie(` int getOutputCoords() { return 0; } - `)}getOutputPacked1DCoords(e,t){let n=t,o="";return n[0]===1?(o=` + `)}getOutputPacked1DCoords(e,t){let n=t,i="";return n[0]===1?(i=` int getOutputCoords() { return 2 * int(TexCoords.y * ${n[1]}.0); } - `,new te(o)):n[1]===1?(o=` + `,new ie(i)):n[1]===1?(i=` int getOutputCoords() { return 2 * int(TexCoords.x * ${n[0]}.0); } - `,new te(o)):(o=` + `,new ie(i)):(i=` int getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${n[0]}, ${n[1]})); return 2 * (resTexRC.y * ${n[0]} + resTexRC.x); } - `,new te(o))}getOutputPacked2DCoords(e,t){let n="";if(ni.arraysEqual(e,t))return n=` + `,new ie(i))}getOutputPacked2DCoords(e,t){let n="";if(_i.arraysEqual(e,t))return n=` ivec2 getOutputCoords() { return 2 * ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); } - `,new te(n);let o=t,u=Math.ceil(e[1]/2);return n=` + `,new ie(n);let i=t,u=Math.ceil(e[1]/2);return n=` ivec2 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * - vec2(${o[0]}, ${o[1]})); + vec2(${i[0]}, ${i[1]})); - int index = resTexRC.y * ${o[0]} + resTexRC.x; + int index = resTexRC.y * ${i[0]} + resTexRC.x; // reverse r and c order for packed texture int r = imod(index, ${u}) * 2; @@ -1358,7 +1358,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( return ivec2(r, c); } - `,new te(n)}getOutputPacked3DCoords(e,t){let n=[t[0],t[1]],o=Math.ceil(e[2]/2),u=o*Math.ceil(e[1]/2),l=` + `,new ie(n)}getOutputPacked3DCoords(e,t){let n=[t[0],t[1]],i=Math.ceil(e[2]/2),u=i*Math.ceil(e[1]/2),l=` ivec3 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${n[0]}, ${n[1]})); @@ -1368,12 +1368,12 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( index -= b * ${u}; // reverse r and c order for packed texture - int r = imod(index, ${o}) * 2; - int c = 2 * (index / ${o}); + int r = imod(index, ${i}) * 2; + int c = 2 * (index / ${i}); return ivec3(b, r, c); } - `;return new te(l)}getOutputPackedNDCoords(e,t){let n=[t[0],t[1]],o=Math.ceil(e[e.length-1]/2),u=o*Math.ceil(e[e.length-2]/2),l=u,d="",c="b, r, c";for(let r=2;rsr( index -= b * ${u}; // reverse r and c order for packed texture - int r = imod(index, ${o}) * 2; - int c = 2 * (index / ${o}); + int r = imod(index, ${i}) * 2; + int c = 2 * (index / ${i}); return ivec${e.length}(${c}); } - `;return new te(s)}getOutputUnpacked1DCoords(e,t){let n=` + `;return new ie(s)}getOutputUnpacked1DCoords(e,t){let n=` int getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); return resTexRC.y * ${t[0]} + resTexRC.x; } - `;return new te(n)}getOutputUnpacked2DCoords(e,t){let n=` + `;return new ie(n)}getOutputUnpacked2DCoords(e,t){let n=` ivec2 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1408,7 +1408,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( int c = index - r * ${e[1]}; return ivec2(r, c); } - `;return new te(n)}getOutputUnpacked3DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `;return new ie(n)}getOutputUnpacked3DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec3 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1416,7 +1416,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( ${d} return ivec3(r, c, d); } - `,new te(n)}getOutputUnpacked4DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `,new ie(n)}getOutputUnpacked4DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec4 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1424,7 +1424,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( ${d} return ivec4(r, c, d, d2); } - `,new te(n)}getOutputUnpacked5DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `,new ie(n)}getOutputUnpacked5DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec5 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1432,7 +1432,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( ${d} return ivec5(r, c, d, d2, d3); } - `,new te(n)}getOutputUnpacked6DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3","d4"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `,new ie(n)}getOutputUnpacked6DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3","d4"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec6 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1440,7 +1440,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( ${d} return ivec6(r, c, d, d2, d3, d4); } - `,new te(n)}getCommonUtilFuncs(){let e={},t="uvFromFlat";e[t]=new te(` + `,new ie(n)}getCommonUtilFuncs(){let e={},t="uvFromFlat";e[t]=new ie(` vec2 uvFromFlat(int texNumR, int texNumC, int index) { int texC = index / texNumR; int texR = index - texC * texNumR; @@ -1448,21 +1448,21 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( // v. return (vec2(texR, texC) + halfCR) / vec2(texNumR, texNumC); } - `),t="packedUVfrom1D",e[t]=new te(` + `),t="packedUVfrom1D",e[t]=new ie(` vec2 packedUVfrom1D(int texNumR, int texNumC, int index) { int texelIndex = index / 2; int texR = texelIndex / texNumC; int texC = texelIndex - texR * texNumC; return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); } - `),t="packedUVfrom2D",e[t]=new te(` + `),t="packedUVfrom2D",e[t]=new ie(` vec2 packedUVfrom2D(int texNumR, int texNumC, int texelsInLogicalRow, int row, int col) { int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2); int texR = texelIndex / texNumC; int texC = texelIndex - texR * texNumC; return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); } - `),t="packedUVfrom3D",e[t]=new te(` + `),t="packedUVfrom3D",e[t]=new ie(` vec2 packedUVfrom3D(int texNumR, int texNumC, int texelsInBatch, int texelsInLogicalRow, int b, int row, int col) { @@ -1471,162 +1471,162 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( int texC = index - texR * texNumC; return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); } - `),t="sampleTexture";let n=Te(this.context.glContext.version);return e[t]=new te(` + `),t="sampleTexture";let n=Ae(this.context.glContext.version);return e[t]=new ie(` float sampleTexture(sampler2D textureSampler, vec2 uv) { return ${n.texture2D}(textureSampler, uv).r; - }`),e}getInputsSamplingSnippets(){let e={},t=this.context.outputTextureLayout;return this.context.programInfo.inputNames.forEach((n,o)=>{let u=this.context.inputTextureLayouts[o],l=Vs(n);u.isPacked?e[l]=this.getPackedSamplerFromInput(l,n,u):e[l]=this.getUnpackedSamplerFromInput(l,n,u);let d=R3(n);u.unpackedShape.length<=t.unpackedShape.length&&(u.isPacked?e[d]=this.getPackedSamplerAtOutputCoords(d,u,t,n):e[d]=this.getUnpackedSamplerAtOutputCoords(d,u,t,n))}),e}getPackedSamplerAtOutputCoords(e,t,n,o){let u=t.unpackedShape,l=n.unpackedShape,d=Vs(o),c=u.length,s=l.length,r=Ut.getBroadcastDims(u,l),i=br(s),a=s-c,p,h=vn();c===0?p="":s<2&&r.length>=1?p="coords = 0;":p=r.map(S=>`coords.${h[S+a]} = 0;`).join(` -`);let m="";s<2&&c>0?m="coords":m=u.map((S,O)=>`coords.${h[O+a]}`).join(", ");let b="return outputValue;",$=me.size(u)===1,_=me.size(l)===1;if(c===1&&!$&&!_)b=` + }`),e}getInputsSamplingSnippets(){let e={},t=this.context.outputTextureLayout;return this.context.programInfo.inputNames.forEach((n,i)=>{let u=this.context.inputTextureLayouts[i],l=ga(n);u.isPacked?e[l]=this.getPackedSamplerFromInput(l,n,u):e[l]=this.getUnpackedSamplerFromInput(l,n,u);let d=FO(n);u.unpackedShape.length<=t.unpackedShape.length&&(u.isPacked?e[d]=this.getPackedSamplerAtOutputCoords(d,u,t,n):e[d]=this.getUnpackedSamplerAtOutputCoords(d,u,t,n))}),e}getPackedSamplerAtOutputCoords(e,t,n,i){let u=t.unpackedShape,l=n.unpackedShape,d=ga(i),c=u.length,s=l.length,r=Qt.getBroadcastDims(u,l),o=Ar(s),a=s-c,p,h=zn();c===0?p="":s<2&&r.length>=1?p="coords = 0;":p=r.map(S=>`coords.${h[S+a]} = 0;`).join(` +`);let m="";s<2&&c>0?m="coords":m=u.map((S,O)=>`coords.${h[O+a]}`).join(", ");let g="return outputValue;",_=ye.size(u)===1,w=ye.size(l)===1;if(c===1&&!_&&!w)g=` return vec4(outputValue.xy, outputValue.xy); - `;else if($&&!_)s===1?b=` + `;else if(_&&!w)s===1?g=` return vec4(outputValue.x, outputValue.x, 0., 0.); - `:b=` + `:g=` return vec4(outputValue.x); - `;else if(r.length){let S=c-2,O=c-1;r.indexOf(S)>-1&&r.indexOf(O)>-1?b="return vec4(outputValue.x);":r.indexOf(S)>-1?b="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":r.indexOf(O)>-1&&(b="return vec4(outputValue.xx, outputValue.zz);")}let y=` + `;else if(r.length){let S=c-2,O=c-1;r.indexOf(S)>-1&&r.indexOf(O)>-1?g="return vec4(outputValue.x);":r.indexOf(S)>-1?g="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":r.indexOf(O)>-1&&(g="return vec4(outputValue.xx, outputValue.zz);")}let y=` int lastDim = coords.${h[s-1]}; coords.${h[s-1]} = coords.${h[s-2]}; coords.${h[s-2]} = lastDim; `,T=` vec4 ${e}() { - ${i} coords = getOutputCoords(); + ${o} coords = getOutputCoords(); ${y} ${p} vec4 outputValue = ${d}(${m}); - ${b} + ${g} } - `;return new te(T,["coordinates.getOutputCoords"])}getUnpackedSamplerAtOutputCoords(e,t,n,o){let u=[n.width,n.height],l=[t.width,t.height],d=t.unpackedShape.length,c=n.unpackedShape.length,s=t.unpackedShape,r=n.unpackedShape,i=Vs(o);if(d===c&&ni.arraysEqual(l,u)){let y=` + `;return new ie(T,["coordinates.getOutputCoords"])}getUnpackedSamplerAtOutputCoords(e,t,n,i){let u=[n.width,n.height],l=[t.width,t.height],d=t.unpackedShape.length,c=n.unpackedShape.length,s=t.unpackedShape,r=n.unpackedShape,o=ga(i);if(d===c&&_i.arraysEqual(l,u)){let y=` float ${e}() { - return sampleTexture(${o}, TexCoords); + return sampleTexture(${i}, TexCoords); } - `;return new te(y,["coordinates.sampleTexture"])}let a=br(c),p=Ut.getBroadcastDims(s,r),h=c-d,m,b=vn();d===0?m="":c<2&&p.length>=1?m="coords = 0;":m=p.map(y=>`coords.${b[y+h]} = 0;`).join(` -`);let $="";c<2&&d>0?$="coords":$=t.unpackedShape.map((y,T)=>`coords.${b[T+h]}`).join(", ");let _=` + `;return new ie(y,["coordinates.sampleTexture"])}let a=Ar(c),p=Qt.getBroadcastDims(s,r),h=c-d,m,g=zn();d===0?m="":c<2&&p.length>=1?m="coords = 0;":m=p.map(y=>`coords.${g[y+h]} = 0;`).join(` +`);let _="";c<2&&d>0?_="coords":_=t.unpackedShape.map((y,T)=>`coords.${g[T+h]}`).join(", ");let w=` float ${e}() { ${a} coords = getOutputCoords(); ${m} - return ${i}(${$}); + return ${o}(${_}); } - `;return new te(_,["coordinates.getOutputCoords"])}getPackedSamplerFromInput(e,t,n){switch(n.unpackedShape.length){case 0:return this.getPackedSamplerScalar(e,t);case 1:return this.getPackedSampler1D(e,t,n);case 2:return this.getPackedSampler2D(e,t,n);case 3:return this.getPackedSampler3D(e,t,n);default:return this.getPackedSamplerND(e,t,n)}}getUnpackedSamplerFromInput(e,t,n){let o=n.unpackedShape;switch(o.length){case 0:return this.getUnpackedSamplerScalar(e,t,n);case 1:return this.getUnpackedSampler1D(e,t,n);case 2:return this.getUnpackedSampler2D(e,t,n);case 3:return this.getUnpackedSampler3D(e,t,n);case 4:return this.getUnpackedSampler4D(e,t,n);case 5:return this.getUnpackedSampler5D(e,t,n);case 6:return this.getUnpackedSampler6D(e,t,n);default:throw new Error(`Unsupported dimension ${o.length}-D`)}}getPackedSamplerScalar(e,t){let n=Te(this.context.glContext.version),o=` + `;return new ie(w,["coordinates.getOutputCoords"])}getPackedSamplerFromInput(e,t,n){switch(n.unpackedShape.length){case 0:return this.getPackedSamplerScalar(e,t);case 1:return this.getPackedSampler1D(e,t,n);case 2:return this.getPackedSampler2D(e,t,n);case 3:return this.getPackedSampler3D(e,t,n);default:return this.getPackedSamplerND(e,t,n)}}getUnpackedSamplerFromInput(e,t,n){let i=n.unpackedShape;switch(i.length){case 0:return this.getUnpackedSamplerScalar(e,t,n);case 1:return this.getUnpackedSampler1D(e,t,n);case 2:return this.getUnpackedSampler2D(e,t,n);case 3:return this.getUnpackedSampler3D(e,t,n);case 4:return this.getUnpackedSampler4D(e,t,n);case 5:return this.getUnpackedSampler5D(e,t,n);case 6:return this.getUnpackedSampler6D(e,t,n);default:throw new Error(`Unsupported dimension ${i.length}-D`)}}getPackedSamplerScalar(e,t){let n=Ae(this.context.glContext.version),i=` vec4 ${e}() { return ${n.texture2D}(${t}, halfCR); } - `;return new te(o)}getPackedSampler1D(e,t,n){let o=[n.width,n.height],u=[o[1],o[0]],l=Te(this.context.glContext.version),d=`vec4 ${e}(int index) { + `;return new ie(i)}getPackedSampler1D(e,t,n){let i=[n.width,n.height],u=[i[1],i[0]],l=Ae(this.context.glContext.version),d=`vec4 ${e}(int index) { vec2 uv = packedUVfrom1D( ${u[0]}, ${u[1]}, index); return ${l.texture2D}(${t}, uv); - }`;return new te(d,["coordinates.packedUVfrom1D"])}getPackedSampler2D(e,t,n){let o=n.unpackedShape,u=[n.width,n.height],l=Te(this.context.glContext.version),d=u[0],c=u[1];if(u!=null&&ni.arraysEqual(o,u)){let a=`vec4 ${e}(int row, int col) { + }`;return new ie(d,["coordinates.packedUVfrom1D"])}getPackedSampler2D(e,t,n){let i=n.unpackedShape,u=[n.width,n.height],l=Ae(this.context.glContext.version),d=u[0],c=u[1];if(u!=null&&_i.arraysEqual(i,u)){let a=`vec4 ${e}(int row, int col) { vec2 uv = (vec2(col, row) + halfCR) / vec2(${c}.0, ${d}.0); return ${l.texture2D}(${t}, uv); - }`;return new te(a)}let s=u,r=Math.ceil(o[1]/2),i=`vec4 ${e}(int row, int col) { + }`;return new ie(a)}let s=u,r=Math.ceil(i[1]/2),o=`vec4 ${e}(int row, int col) { vec2 uv = packedUVfrom2D(${s[1]}, ${s[0]}, ${r}, row, col); return ${l.texture2D}(${t}, uv); - }`;return new te(i,["coordinates.packedUVfrom2D"])}getPackedSampler3D(e,t,n){let o=n.unpackedShape,u=[n.width,n.height],l=[u[0],u[1]],d=Te(this.context.glContext.version);if(o[0]===1){let p=o.slice(1),h=[1,2],m=jn(o,p),b=["b","row","col"],$=JSON.parse(JSON.stringify(n));$.unpackedShape=m;let _=this.getPackedSamplerFromInput(e,t,$),y=`${_.routineBody} + }`;return new ie(o,["coordinates.packedUVfrom2D"])}getPackedSampler3D(e,t,n){let i=n.unpackedShape,u=[n.width,n.height],l=[u[0],u[1]],d=Ae(this.context.glContext.version);if(i[0]===1){let p=i.slice(1),h=[1,2],m=ii(i,p),g=["b","row","col"],_=JSON.parse(JSON.stringify(n));_.unpackedShape=m;let w=this.getPackedSamplerFromInput(e,t,_),y=`${w.routineBody} vec4 ${e}(int b, int row, int col) { - return ${e}(${Un(b,h)}); - } `;return new te(y,_.dependencies)}let c=l[0],s=l[1],r=Math.ceil(o[2]/2),i=r*Math.ceil(o[1]/2),a=`vec4 ${e}(int b, int row, int col) { + return ${e}(${oi(g,h)}); + } `;return new ie(y,w.dependencies)}let c=l[0],s=l[1],r=Math.ceil(i[2]/2),o=r*Math.ceil(i[1]/2),a=`vec4 ${e}(int b, int row, int col) { vec2 uv = packedUVfrom3D( - ${s}, ${c}, ${i}, ${r}, b, row, col); - return ${d.texture2D}(${t}, uv);}`;return new te(a,["coordinates.packedUVfrom3D"])}getPackedSamplerND(e,t,n){let o=n.unpackedShape,u=o.length,l=[n.width,n.height],d=Te(this.context.glContext.version),c=[l[0],l[1]],s=c[1],r=c[0],i=Math.ceil(o[u-1]/2),a=i*Math.ceil(o[u-2]/2),p="int b, int row, int col",h=`b * ${a} + (row / 2) * ${i} + (col / 2)`;for(let b=2;bsr( vec2 uv = uvFromFlat(${a}, ${p}, index); return sampleTexture(${t}, uv); } - `;return new te(h,["coordinates.uvFromFlat","coordinates.sampleTexture","coordinates.coordsToOffset"])}toVec(){let e=this.context.outputTextureLayout,t=e.shape.length,n=e.strides,o=e.width,u=e.height,l=[];for(let c=0;c{let o=this.context.inputTextureLayouts[n],u=(o.unpackedShape.length>0?o.unpackedShape:o.shape).length,l=`_${t}`;e[l]=new te(this.getValueFromSingle(t,u,o.width,o.height,!1),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"]),l=l+"_T",e[l]=new te(this.getValueFromSingle(t,u,o.width,o.height,!0),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"])}),e}getValueFromSingle(e,t,n,o,u){let l=`_${e}`;u&&(l=l+"_T");let d=Te(this.context.glContext.version);return` + `;return{toVec:new ie(d,["coordinates.coordsToOffset"])}}valueFrom(){let e={};return this.context.programInfo.inputNames.forEach((t,n)=>{let i=this.context.inputTextureLayouts[n],u=(i.unpackedShape.length>0?i.unpackedShape:i.shape).length,l=`_${t}`;e[l]=new ie(this.getValueFromSingle(t,u,i.width,i.height,!1),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"]),l=l+"_T",e[l]=new ie(this.getValueFromSingle(t,u,i.width,i.height,!0),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"])}),e}getValueFromSingle(e,t,n,i,u){let l=`_${e}`;u&&(l=l+"_T");let d=Ae(this.context.glContext.version);return` float ${l}(int m[${t}]) { int offset = indicesToOffset${l}(m); - vec2 coords = offsetToCoords(offset, ${n}, ${o}); + vec2 coords = offsetToCoords(offset, ${n}, ${i}); float value = getColorAsFloat(${d.texture2D}(${e}, coords)); return value; } - `}getPackedValueFrom(e,t,n,o,u){let l=`_${e}_Pack`;u&&(l=l+"_T");let d=Te(this.context.glContext.version);return` + `}getPackedValueFrom(e,t,n,i,u){let l=`_${e}_Pack`;u&&(l=l+"_T");let d=Ae(this.context.glContext.version);return` vec4 ${l}(int m[${t}]) { int offset = indicesToOffset_${e}(m); - vec2 coords = offsetToCoords(offset, ${n}, ${o}); + vec2 coords = offsetToCoords(offset, ${n}, ${i}); return ${d.texture2D}(${e}, coords); } - `}}}),lv,mE=D(()=>{"use strict";_r(),lv=class Yu extends In{constructor(t){super(t)}getFunctions(){return{...this.encodeFloat32(),...this.decodeFloat32()}}getCustomTypes(){return{}}encodeFloat32(){return{encode:new te(`highp vec4 encode(highp float f) { + `}}}),Gv,_A=D(()=>{"use strict";Pr(),Gv=class Tl extends Un{constructor(t){super(t)}getFunctions(){return{...this.encodeFloat32(),...this.decodeFloat32()}}getCustomTypes(){return{}}encodeFloat32(){return{encode:new ie(`highp vec4 encode(highp float f) { return vec4(f, 0.0, 0.0, 0.0); } - `)}}decodeFloat32(){return{decode:new te(`highp float decode(highp vec4 rgba) { + `)}}decodeFloat32(){return{decode:new ie(`highp float decode(highp vec4 rgba) { return rgba.r; } - `)}}encodeUint8(){let t=Yu.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{encode:new te(` + `)}}encodeUint8(){let t=Tl.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{encode:new ie(` highp vec4 encode(highp float f) { highp float F = abs(f); highp float Sign = step(0.0,-f); @@ -1680,7 +1680,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( rgba = rgba / 255.0; // values need to be normalized to [0,1] return rgba; } - `)}}decodeUint8(){let t=Yu.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{decode:new te(` + `)}}decodeUint8(){let t=Tl.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{decode:new ie(` highp float decode(highp vec4 rgba) { rgba = rgba * 255.0; // values need to be de-normalized from [0,1] to [0,255] ${t} @@ -1690,44 +1690,44 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),Dw=(e,t,n)=>sr( highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); return Result; } - `)}}static isLittleEndian(){let t=new ArrayBuffer(4),n=new Uint32Array(t),o=new Uint8Array(t);if(n[0]=3735928559,o[0]===239)return!0;if(o[0]===222)return!1;throw new Error("unknown endianness")}}}),dv,gE=D(()=>{"use strict";_r(),Ge(),dv=class extends In{constructor(e){super(e)}getFunctions(){return{...this.setFragColor(),...this.getColorAsFloat()}}getCustomTypes(){return{}}setFragColor(){let e=Te(this.context.glContext.version);return{setFragColor:new te(` + `)}}static isLittleEndian(){let t=new ArrayBuffer(4),n=new Uint32Array(t),i=new Uint8Array(t);if(n[0]=3735928559,i[0]===239)return!0;if(i[0]===222)return!1;throw new Error("unknown endianness")}}}),Hv,vA=D(()=>{"use strict";Pr(),Ke(),Hv=class extends Un{constructor(e){super(e)}getFunctions(){return{...this.setFragColor(),...this.getColorAsFloat()}}getCustomTypes(){return{}}setFragColor(){let e=Ae(this.context.glContext.version);return{setFragColor:new ie(` void setFragColor(float value) { ${e.output} = encode(value); } - `,["encoding.encode"])}}getColorAsFloat(){return{getColorAsFloat:new te(` + `,["encoding.encode"])}}getColorAsFloat(){return{getColorAsFloat:new ie(` float getColorAsFloat(vec4 color) { return decode(color); } - `,["encoding.decode"])}}}}),cv,bE=D(()=>{"use strict";_r(),cv=class si extends In{constructor(t){super(t)}getFunctions(){return{...this.bcastIndex(),...this.bcastMatmulIndex(),...this.offsetToIndices(),...this.indicesToOffset(),...this.incrementIndices()}}getCustomTypes(){return{}}bcastIndex(){let t=this.context.outputTextureLayout.shape.length,n={};return this.context.programInfo.inputNames.forEach((o,u)=>{let l=this.context.inputTextureLayouts[u].unpackedShape;if(l.length<=t){let d=l.length,c=t-d,s=`bcastIndices_${o}`,r="";for(let a=0;a{"use strict";Pr(),Wv=class $i extends Un{constructor(t){super(t)}getFunctions(){return{...this.bcastIndex(),...this.bcastMatmulIndex(),...this.offsetToIndices(),...this.indicesToOffset(),...this.incrementIndices()}}getCustomTypes(){return{}}bcastIndex(){let t=this.context.outputTextureLayout.shape.length,n={};return this.context.programInfo.inputNames.forEach((i,u)=>{let l=this.context.inputTextureLayouts[u].unpackedShape;if(l.length<=t){let d=l.length,c=t-d,s=`bcastIndices_${i}`,r="";for(let a=0;a{let l=this.context.inputTextureLayouts[u].shape;if(!(l.length<2||l.length>t)){let d=l.length,c=t-d,s=`bcastMatmulIndices_${o}`,r="";for(let a=0;a{let l=this.context.inputTextureLayouts[u].shape;if(!(l.length<2||l.length>t)){let d=l.length,c=t-d,s=`bcastMatmulIndices_${i}`,r="";for(let a=0;a{let u=this.context.inputTextureLayouts[o].shape,l=this.context.inputTextureLayouts[o].strides,d=u.length,c=`indicesToOffset_${n}`;t[c]=new te(si.indexToOffsetSingle(c,d,l)),c=`indicesToOffset_${n}_T`,t[c]=new te(si.indexToOffsetSingle(c,d,l.slice().reverse()))}),t}static indexToOffsetSingle(t,n,o){let u="";for(let l=n-1;l>=0;--l)u+=` - offset += indices[${l}] * ${o[l]}; + `;n[s]=new ie(o)}}),n}indicesToOffset(){let t={};return this.context.programInfo.inputNames.forEach((n,i)=>{let u=this.context.inputTextureLayouts[i].shape,l=this.context.inputTextureLayouts[i].strides,d=u.length,c=`indicesToOffset_${n}`;t[c]=new ie($i.indexToOffsetSingle(c,d,l)),c=`indicesToOffset_${n}_T`,t[c]=new ie($i.indexToOffsetSingle(c,d,l.slice().reverse()))}),t}static indexToOffsetSingle(t,n,i){let u="";for(let l=n-1;l>=0;--l)u+=` + offset += indices[${l}] * ${i[l]}; `;return` int ${t}(int indices[${n}]) { int offset = 0; ${u} return offset; } - `}offsetToIndices(){let t={};return this.context.programInfo.inputNames.forEach((n,o)=>{let u=this.context.inputTextureLayouts[o].shape,l=this.context.inputTextureLayouts[o].strides,d=u.length,c=`offsetToIndices_${n}`;t[c]=new te(si.offsetToIndicesSingle(c,d,l)),c=`offsetToIndices_${n}_T`,t[c]=new te(si.offsetToIndicesSingle(c,d,l.slice().reverse()))}),t}static offsetToIndicesSingle(t,n,o){let u=[];for(let l=0;l{let u=this.context.inputTextureLayouts[i].shape,l=this.context.inputTextureLayouts[i].strides,d=u.length,c=`offsetToIndices_${n}`;t[c]=new ie($i.offsetToIndicesSingle(c,d,l)),c=`offsetToIndices_${n}_T`,t[c]=new ie($i.offsetToIndicesSingle(c,d,l.slice().reverse()))}),t}static offsetToIndicesSingle(t,n,i){let u=[];for(let l=0;l{let u=this.context.inputTextureLayouts[o].shape,l=u.length,d=`incrementIndices_${n}`,c="";for(let r=0;r{let u=this.context.inputTextureLayouts[i].shape,l=u.length,d=`incrementIndices_${n}`,c="";for(let r=0;rsr( indices[i] = 0; } } - `;t[d]=new te(s)}),t}}}),pv,yE=D(()=>{"use strict";_r(),pv=class extends In{constructor(e){super(e)}getCustomTypes(){return{}}getFunctions(){return{...this.binaryVecFunctions(),...this.copyVec(),...this.setVecItem(),...this.getVecItem()}}binaryVecFunctions(){let e=this.context.outputTextureLayout.shape.length,t={add:"+=",sub:"-=",mul:"*=",div:"/="},n={};for(let o in t){let u=`${o}Vec`,l="";for(let c=0;c{"use strict";Pr(),Kv=class extends Un{constructor(e){super(e)}getCustomTypes(){return{}}getFunctions(){return{...this.binaryVecFunctions(),...this.copyVec(),...this.setVecItem(),...this.getVecItem()}}binaryVecFunctions(){let e=this.context.outputTextureLayout.shape.length,t={add:"+=",sub:"-=",mul:"*=",div:"/="},n={};for(let i in t){let u=`${i}Vec`,l="";for(let c=0;csr( void setVecItem(out int m[${e}], int index, int value) { ${t} } - `;return{setVecItem:new te(n)}}getVecItem(){let e=this.context.outputTextureLayout.shape.length,t=` + `;return{setVecItem:new ie(n)}}getVecItem(){let e=this.context.outputTextureLayout.shape.length,t=` if(index < 0) index = ${e} + index; if (index == 0) return m[0]; - `;for(let o=1;osr( int getVecItem(int m[${e}], int index) { ${t} } - `;return{getVecItem:new te(n)}}}}),Ju,_E=D(()=>{"use strict";fE(),mE(),gE(),bE(),yE(),Ju={encoding:lv,fragcolor:dv,vec:pv,shapeUtils:cv,coordinates:uv}}),hv,wE=D(()=>{"use strict";_r(),dE(),_E(),Ge(),hv=class{constructor(e,t,n,o){this.libs={},this.glslLibRoutineDependencyGraph={},this.context=new J0(e,t,n,o),Object.keys(Ju).forEach(l=>{let d=new Ju[l](this.context);this.libs[l]=d});let u=this.glslLibRoutineDependencyGraph;for(let l in this.libs){let d=this.libs[l].getFunctions();for(let c in d){let s=l+"."+c,r;u[s]?(r=u[s],r.routineBody=d[c].routineBody):(r=new Mu(s,d[c].routineBody),u[s]=r);let i=d[c].dependencies;if(i)for(let a=0;a{"use strict";wA(),_A(),vA(),xA(),$A(),Sl={encoding:Gv,fragcolor:Hv,vec:Kv,shapeUtils:Wv,coordinates:qv}}),Xv,SA=D(()=>{"use strict";Pr(),mA(),TA(),Ke(),Xv=class{constructor(e,t,n,i){this.libs={},this.glslLibRoutineDependencyGraph={},this.context=new Dw(e,t,n,i),Object.keys(Sl).forEach(l=>{let d=new Sl[l](this.context);this.libs[l]=d});let u=this.glslLibRoutineDependencyGraph;for(let l in this.libs){let d=this.libs[l].getFunctions();for(let c in d){let s=l+"."+c,r;u[s]?(r=u[s],r.routineBody=d[c].routineBody):(r=new dl(s,d[c].routineBody),u[s]=r);let o=d[c].dependencies;if(o)for(let a=0;a{let o=n.split(".")[1];e.indexOf(o)!==-1&&t.push(this.glslLibRoutineDependencyGraph[n])}),Q0.returnOrderedNodes(t)}getUniforms(e,t){let n=[];if(e)for(let o of e)n.push(`uniform sampler2D ${o};`);if(t)for(let o of t)n.push(`uniform ${o.type} ${o.name}${o.arrayLength?`[${o.arrayLength}]`:""};`);return n.join(` -`)}}}),fv,vE=D(()=>{"use strict";it(),Rt(),wE(),Ge(),fv=class{constructor(e,t,n){this.profiler=e,this.glContext=t,this.textureLayoutStrategy=n,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n){this.profiler.event("op",`ProgramManager.run ${e.programInfo.name??"unknown kernel"}`,()=>{let o=this.glContext.gl,u=e.program;o.useProgram(u);try{this.bindOutput(n),this.attributesBound||this.bindAttributes(e.attribLocations),this.bindUniforms(e.uniformLocations,e.programInfo.variables??[],t)}catch(l){throw We.error("ProgramManager",e.programInfo.shaderSource),l}this.profiler.event("backend","GlContext.draw()",()=>{this.glContext.draw()})},this.glContext)}dispose(){this.vertexShader&&this.glContext.deleteShader(this.vertexShader),this.repo.forEach(e=>this.glContext.deleteProgram(e.program))}build(e,t,n){return this.profiler.event("backend","ProgramManager.build",()=>{let o=new hv(this.glContext,e,t,n),u=o.preprocess(),l=this.compile(u);return{programInfo:e,program:l,uniformLocations:this.getUniformLocations(l,o.context.programInfo.inputNames,o.context.programInfo.variables),attribLocations:this.getAttribLocations(l)}})}compile(e){if(!this.vertexShader){We.verbose("ProrgramManager","Compiling and caching Vertex shader for the first time");let o=P3(this.glContext.version);this.vertexShader=this.glContext.compileShader(o,this.glContext.gl.VERTEX_SHADER)}pe.debug&&We.verbose("ProrgramManager",`FragShader: + ${t}`}getImports(e){let t=this.selectGlslLibRoutinesToBeIncluded(e);if(t.length===0)return"";let n="";for(let i=0;i{let i=n.split(".")[1];e.indexOf(i)!==-1&&t.push(this.glslLibRoutineDependencyGraph[n])}),Mw.returnOrderedNodes(t)}getUniforms(e,t){let n=[];if(e)for(let i of e)n.push(`uniform sampler2D ${i};`);if(t)for(let i of t)n.push(`uniform ${i.type} ${i.name}${i.arrayLength?`[${i.arrayLength}]`:""};`);return n.join(` +`)}}}),Zv,IA=D(()=>{"use strict";dt(),qt(),SA(),Ke(),Zv=class{constructor(e,t,n){this.profiler=e,this.glContext=t,this.textureLayoutStrategy=n,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n){this.profiler.event("op",`ProgramManager.run ${e.programInfo.name??"unknown kernel"}`,()=>{let i=this.glContext.gl,u=e.program;i.useProgram(u);try{this.bindOutput(n),this.attributesBound||this.bindAttributes(e.attribLocations),this.bindUniforms(e.uniformLocations,e.programInfo.variables??[],t)}catch(l){throw Je.error("ProgramManager",e.programInfo.shaderSource),l}this.profiler.event("backend","GlContext.draw()",()=>{this.glContext.draw()})},this.glContext)}dispose(){this.vertexShader&&this.glContext.deleteShader(this.vertexShader),this.repo.forEach(e=>this.glContext.deleteProgram(e.program))}build(e,t,n){return this.profiler.event("backend","ProgramManager.build",()=>{let i=new Xv(this.glContext,e,t,n),u=i.preprocess(),l=this.compile(u);return{programInfo:e,program:l,uniformLocations:this.getUniformLocations(l,i.context.programInfo.inputNames,i.context.programInfo.variables),attribLocations:this.getAttribLocations(l)}})}compile(e){if(!this.vertexShader){Je.verbose("ProrgramManager","Compiling and caching Vertex shader for the first time");let i=NO(this.glContext.version);this.vertexShader=this.glContext.compileShader(i,this.glContext.gl.VERTEX_SHADER)}me.debug&&Je.verbose("ProrgramManager",`FragShader: ${e} -`);let t=this.glContext.compileShader(e,this.glContext.gl.FRAGMENT_SHADER),n=this.glContext.createProgram(this.vertexShader,t);return this.glContext.deleteShader(t),n}bindOutput(e){let t=e.width,n=e.height;We.verbose("ProrgramManager",`Binding output texture to Framebuffer: w/h=${t}/${n}, shape=${e.shape}, type=${e.tensor.type}`),this.glContext.attachFramebuffer(e.texture,t,n)}bindAttributes(e){let t=e.position,n=e.textureCoord;this.glContext.setVertexAttributes(t,n),this.attributesBound=!0}bindUniforms(e,t,n){let o=this.glContext.gl,u=0;for(let{name:l,type:d,location:c,arrayLength:s}of e){let r=t.find(i=>i.name===l)?.data;if(d!=="sampler2D"&&!r)throw new Error(`variable '${l}' does not have data defined in program info`);switch(d){case"sampler2D":this.bindTexture(n[u],c,u),u++;break;case"float":s?o.uniform1fv(c,r):o.uniform1f(c,r);break;case"int":s?o.uniform1iv(c,r):o.uniform1i(c,r);break;default:throw new Error(`Uniform not implemented: ${d}`)}}}bindTexture(e,t,n){this.glContext.bindTextureToUniform(e.texture,n,t)}getAttribLocations(e){return{position:this.getAttribLocation(e,"position"),textureCoord:this.getAttribLocation(e,"textureCoord")}}getUniformLocations(e,t,n){let o=[];if(t)for(let u of t)o.push({name:u,type:"sampler2D",location:this.getUniformLocation(e,u)});if(n)for(let u of n)o.push({...u,location:this.getUniformLocation(e,u.name)});return o}getUniformLocation(e,t){let n=this.glContext.gl.getUniformLocation(e,t);if(n===null)throw new Error(`Uniform ${t} not found.`);return n}getAttribLocation(e,t){return this.glContext.gl.getAttribLocation(e,t)}}}),mv,xE=D(()=>{"use strict";Rt(),$o(),mv=class{constructor(e,t,n,o){this.glContext=e,this.layoutStrategy=t,this.profiler=n,this.config=o,this.pendingRead=new Map,o.reuseTextures&&(this.inUseTextures=new Map,this.idleTextures=new Map,this.textureLookup=new Map)}createTextureFromLayout(e,t,n,o){let u=this.toEncoderType(e),l=this.glContext.getEncoder(u,t.channels||1,o);if(t.isPacked&&o===1)throw new Error("not implemented");let d=t.width,c=t.height,s,r;if(this.config.reuseTextures){s=`${d}x${c}_${l.format}_${l.internalFormat}_${l.textureType}`,r=this.inUseTextures.get(s),r||(r=[],this.inUseTextures.set(s,r));let a=this.idleTextures.get(s);if(a&&a.length>0){let p=a.pop();return r.push(p),o===1&&this.glContext.updateTexture(p,d,c,l,this.toTextureData(e,n)),p}}We.verbose("TextureManager",`Creating new texture of size ${t.width}x${t.height}`);let i=this.glContext.allocateTexture(d,c,l,this.toTextureData(e,n));return this.config.reuseTextures&&(r.push(i),this.textureLookup.set(i,s)),i}readTexture(e,t,n){return n||(n=1),this.profiler.event("backend","TextureManager.readTexture",()=>{let o=e.shape.reduce((l,d)=>l*d)*n,u=this.glContext.readTexture(e.texture,e.width,e.height,o,this.toEncoderType(t),n);return this.toTensorData(t,u)})}async readTextureAsync(e,t,n){let o=e.tensor.dataId;if(n||(n=1),this.pendingRead.has(o)){let u=this.pendingRead.get(o);return new Promise(l=>u?.push(l))}return this.profiler.event("backend","TextureManager.readTextureAsync",async()=>{this.pendingRead.set(o,[]);let u=e.shape.reduce((s,r)=>s*r)*n;await this.glContext.createAndWaitForFence();let l=this.glContext.readTexture(e.texture,e.width,e.height,u,this.toEncoderType(t),n),d=this.toTensorData(t,l),c=this.pendingRead.get(o);return this.pendingRead.delete(o),c?.forEach(s=>s(d)),d})}readUint8TextureAsFloat(e){return this.profiler.event("backend","TextureManager.readUint8TextureAsFloat",()=>{let t=e.shape.reduce((o,u)=>o*u),n=this.glContext.readTexture(e.texture,e.width,e.height,t*4,"byte",4);return new Float32Array(n.buffer,n.byteOffset,t)})}releaseTexture(e,t){let n;if(this.config.reuseTextures&&(n=this.textureLookup.get(e.texture),n)){t&&this.textureLookup.delete(n);let o=this.inUseTextures.get(n);if(o){let u=o.indexOf(e.texture);if(u!==-1){o.splice(u,1);let l=this.idleTextures.get(n);l||(l=[],this.idleTextures.set(n,l)),l.push(e.texture)}}}(!n||t)&&(We.verbose("TextureManager",`Deleting texture of size ${e.width}x${e.height}`),this.glContext.deleteTexture(e.texture))}toTensorData(e,t){switch(e){case"int16":return t instanceof Int16Array?t:Int16Array.from(t);case"int32":return t instanceof Int32Array?t:Int32Array.from(t);case"int8":return t instanceof Int8Array?t:Int8Array.from(t);case"uint16":return t instanceof Uint16Array?t:Uint16Array.from(t);case"uint32":return t instanceof Uint32Array?t:Uint32Array.from(t);case"uint8":case"bool":return t instanceof Uint8Array?t:Uint8Array.from(t);case"float32":return t instanceof Float32Array?t:Float32Array.from(t);case"float64":return t instanceof Float64Array?t:Float64Array.from(t);default:throw new Error(`TensorData type ${e} is not supported`)}}toTextureData(e,t){if(t)return t instanceof Float32Array?t:new Float32Array(t)}toEncoderType(e){return"float"}clearActiveTextures(){this.glContext.clearActiveTextures()}}}),gv,$E=D(()=>{"use strict";Rt(),CI(),K3(),uE(),vE(),av(),xE(),gv=class{constructor(e,t){this.backend=e,this.context=t,this.layoutStrategy=new sv(e.glContext.maxTextureSize),this.programManager=new fv(this.context.profiler,e.glContext,this.layoutStrategy),this.textureManager=new mv(e.glContext,this.layoutStrategy,this.context.profiler,{reuseTextures:e.textureCacheMode==="full"}),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map,this.pack=e.pack,this.pack2unpackMap=new Map,this.unpack2packMap=new Map}createInferenceHandler(){return new X0(this)}onGraphInitialized(e){let t=e.getValues().filter(n=>n.from===-1&&n.tensor).map(n=>n.tensor.dataId);this.initializers=new Set(t)}isInitializer(e){return this.initializers?this.initializers.has(e):!1}addInitializer(e){this.initializers.add(e)}getTextureData(e,t){return t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){We.verbose("WebGLSessionHandler","Storing Texture data in cache"),n?this.packedTextureDataCache.set(e,t):this.unpackedTextureDataCache.set(e,t)}dispose(){this.programManager.dispose(),this.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.unpackedTextureDataCache=new Map}resolve(e,t,n){let o=kI(e,t,iv);return{impl:o.opImpl,context:o.opInit?o.opInit(e,n):e}}}});function TE(e){let t=0;for(;t{"use strict";it(),$o(),$o(),Zt(),Qu=class{constructor(e,t){this.frameBufferBound=!1,this.itemsToPoll=[],this.gl=e,this.version=t,this.getExtensions(),this.vertexbuffer=this.createVertexbuffer(),this.framebuffer=this.createFramebuffer(),this.queryVitalParameters()}allocateTexture(e,t,n,o){let u=this.gl,l=u.createTexture();u.bindTexture(u.TEXTURE_2D,l),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MIN_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MAG_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_S,u.CLAMP_TO_EDGE),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_T,u.CLAMP_TO_EDGE);let d=o?n.encode(o,e*t):null;return u.texImage2D(u.TEXTURE_2D,0,n.internalFormat,e,t,0,n.format,n.textureType,d),this.checkError(),l}updateTexture(e,t,n,o,u){let l=this.gl;l.bindTexture(l.TEXTURE_2D,e);let d=o.encode(u,t*n);l.texSubImage2D(l.TEXTURE_2D,0,0,0,t,n,o.format,o.textureType,d),this.checkError()}attachFramebuffer(e,t,n){let o=this.gl;o.bindTexture(o.TEXTURE_2D,e),o.bindFramebuffer(o.FRAMEBUFFER,this.framebuffer),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,e,0),this.checkError(),o.viewport(0,0,t,n),o.scissor(0,0,t,n)}readTexture(e,t,n,o,u,l){let d=this.gl;l||(l=1),this.frameBufferBound||this.attachFramebuffer(e,t,n);let c=this.getEncoder(u,l),s=c.allocate(t*n);return d.bindTexture(d.TEXTURE_2D,e),d.framebufferTexture2D(d.FRAMEBUFFER,d.COLOR_ATTACHMENT0,d.TEXTURE_2D,e,0),d.readPixels(0,0,t,n,d.RGBA,c.textureType,s),this.checkError(),c.decode(s,o)}isFramebufferReady(){return!0}getActiveTexture(){let e=this.gl;return`TEXTURE${e.getParameter(this.gl.ACTIVE_TEXTURE)-e.TEXTURE0}`}getTextureBinding(){return this.gl.getParameter(this.gl.TEXTURE_BINDING_2D)}getFramebufferBinding(){return this.gl.getParameter(this.gl.FRAMEBUFFER_BINDING)}setVertexAttributes(e,t){let n=this.gl;n.vertexAttribPointer(e,3,n.FLOAT,!1,20,0),n.enableVertexAttribArray(e),t!==-1&&(n.vertexAttribPointer(t,2,n.FLOAT,!1,20,12),n.enableVertexAttribArray(t)),this.checkError()}createProgram(e,t){let n=this.gl,o=n.createProgram();return n.attachShader(o,e),n.attachShader(o,t),n.linkProgram(o),o}compileShader(e,t){let n=this.gl,o=n.createShader(t);if(!o)throw new Error(`createShader() returned null with type ${t}`);if(n.shaderSource(o,e),n.compileShader(o),n.getShaderParameter(o,n.COMPILE_STATUS)===!1)throw new Error(`Failed to compile shader: ${n.getShaderInfoLog(o)} +`);let t=this.glContext.compileShader(e,this.glContext.gl.FRAGMENT_SHADER),n=this.glContext.createProgram(this.vertexShader,t);return this.glContext.deleteShader(t),n}bindOutput(e){let t=e.width,n=e.height;Je.verbose("ProrgramManager",`Binding output texture to Framebuffer: w/h=${t}/${n}, shape=${e.shape}, type=${e.tensor.type}`),this.glContext.attachFramebuffer(e.texture,t,n)}bindAttributes(e){let t=e.position,n=e.textureCoord;this.glContext.setVertexAttributes(t,n),this.attributesBound=!0}bindUniforms(e,t,n){let i=this.glContext.gl,u=0;for(let{name:l,type:d,location:c,arrayLength:s}of e){let r=t.find(o=>o.name===l)?.data;if(d!=="sampler2D"&&!r)throw new Error(`variable '${l}' does not have data defined in program info`);switch(d){case"sampler2D":this.bindTexture(n[u],c,u),u++;break;case"float":s?i.uniform1fv(c,r):i.uniform1f(c,r);break;case"int":s?i.uniform1iv(c,r):i.uniform1i(c,r);break;default:throw new Error(`Uniform not implemented: ${d}`)}}}bindTexture(e,t,n){this.glContext.bindTextureToUniform(e.texture,n,t)}getAttribLocations(e){return{position:this.getAttribLocation(e,"position"),textureCoord:this.getAttribLocation(e,"textureCoord")}}getUniformLocations(e,t,n){let i=[];if(t)for(let u of t)i.push({name:u,type:"sampler2D",location:this.getUniformLocation(e,u)});if(n)for(let u of n)i.push({...u,location:this.getUniformLocation(e,u.name)});return i}getUniformLocation(e,t){let n=this.glContext.gl.getUniformLocation(e,t);if(n===null)throw new Error(`Uniform ${t} not found.`);return n}getAttribLocation(e,t){return this.glContext.gl.getAttribLocation(e,t)}}}),Yv,OA=D(()=>{"use strict";qt(),Vo(),Yv=class{constructor(e,t,n,i){this.glContext=e,this.layoutStrategy=t,this.profiler=n,this.config=i,this.pendingRead=new Map,i.reuseTextures&&(this.inUseTextures=new Map,this.idleTextures=new Map,this.textureLookup=new Map)}createTextureFromLayout(e,t,n,i){let u=this.toEncoderType(e),l=this.glContext.getEncoder(u,t.channels||1,i);if(t.isPacked&&i===1)throw new Error("not implemented");let d=t.width,c=t.height,s,r;if(this.config.reuseTextures){s=`${d}x${c}_${l.format}_${l.internalFormat}_${l.textureType}`,r=this.inUseTextures.get(s),r||(r=[],this.inUseTextures.set(s,r));let a=this.idleTextures.get(s);if(a&&a.length>0){let p=a.pop();return r.push(p),i===1&&this.glContext.updateTexture(p,d,c,l,this.toTextureData(e,n)),p}}Je.verbose("TextureManager",`Creating new texture of size ${t.width}x${t.height}`);let o=this.glContext.allocateTexture(d,c,l,this.toTextureData(e,n));return this.config.reuseTextures&&(r.push(o),this.textureLookup.set(o,s)),o}readTexture(e,t,n){return n||(n=1),this.profiler.event("backend","TextureManager.readTexture",()=>{let i=e.shape.reduce((l,d)=>l*d)*n,u=this.glContext.readTexture(e.texture,e.width,e.height,i,this.toEncoderType(t),n);return this.toTensorData(t,u)})}async readTextureAsync(e,t,n){let i=e.tensor.dataId;if(n||(n=1),this.pendingRead.has(i)){let u=this.pendingRead.get(i);return new Promise(l=>u?.push(l))}return this.profiler.event("backend","TextureManager.readTextureAsync",async()=>{this.pendingRead.set(i,[]);let u=e.shape.reduce((s,r)=>s*r)*n;await this.glContext.createAndWaitForFence();let l=this.glContext.readTexture(e.texture,e.width,e.height,u,this.toEncoderType(t),n),d=this.toTensorData(t,l),c=this.pendingRead.get(i);return this.pendingRead.delete(i),c?.forEach(s=>s(d)),d})}readUint8TextureAsFloat(e){return this.profiler.event("backend","TextureManager.readUint8TextureAsFloat",()=>{let t=e.shape.reduce((i,u)=>i*u),n=this.glContext.readTexture(e.texture,e.width,e.height,t*4,"byte",4);return new Float32Array(n.buffer,n.byteOffset,t)})}releaseTexture(e,t){let n;if(this.config.reuseTextures&&(n=this.textureLookup.get(e.texture),n)){t&&this.textureLookup.delete(n);let i=this.inUseTextures.get(n);if(i){let u=i.indexOf(e.texture);if(u!==-1){i.splice(u,1);let l=this.idleTextures.get(n);l||(l=[],this.idleTextures.set(n,l)),l.push(e.texture)}}}(!n||t)&&(Je.verbose("TextureManager",`Deleting texture of size ${e.width}x${e.height}`),this.glContext.deleteTexture(e.texture))}toTensorData(e,t){switch(e){case"int16":return t instanceof Int16Array?t:Int16Array.from(t);case"int32":return t instanceof Int32Array?t:Int32Array.from(t);case"int8":return t instanceof Int8Array?t:Int8Array.from(t);case"uint16":return t instanceof Uint16Array?t:Uint16Array.from(t);case"uint32":return t instanceof Uint32Array?t:Uint32Array.from(t);case"uint8":case"bool":return t instanceof Uint8Array?t:Uint8Array.from(t);case"float32":return t instanceof Float32Array?t:Float32Array.from(t);case"float64":return t instanceof Float64Array?t:Float64Array.from(t);default:throw new Error(`TensorData type ${e} is not supported`)}}toTextureData(e,t){if(t)return t instanceof Float32Array?t:new Float32Array(t)}toEncoderType(e){return"float"}clearActiveTextures(){this.glContext.clearActiveTextures()}}}),Jv,EA=D(()=>{"use strict";qt(),z3(),QO(),hA(),IA(),Vv(),OA(),Jv=class{constructor(e,t){this.backend=e,this.context=t,this.layoutStrategy=new Uv(e.glContext.maxTextureSize),this.programManager=new Zv(this.context.profiler,e.glContext,this.layoutStrategy),this.textureManager=new Yv(e.glContext,this.layoutStrategy,this.context.profiler,{reuseTextures:e.textureCacheMode==="full"}),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map,this.pack=e.pack,this.pack2unpackMap=new Map,this.unpack2packMap=new Map}createInferenceHandler(){return new Pw(this)}onGraphInitialized(e){let t=e.getValues().filter(n=>n.from===-1&&n.tensor).map(n=>n.tensor.dataId);this.initializers=new Set(t)}isInitializer(e){return this.initializers?this.initializers.has(e):!1}addInitializer(e){this.initializers.add(e)}getTextureData(e,t){return t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){Je.verbose("WebGLSessionHandler","Storing Texture data in cache"),n?this.packedTextureDataCache.set(e,t):this.unpackedTextureDataCache.set(e,t)}dispose(){this.programManager.dispose(),this.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.unpackedTextureDataCache=new Map}resolve(e,t,n){let i=M3(e,t,Lv);return{impl:i.opImpl,context:i.opInit?i.opInit(e,n):e}}}});function AA(e){let t=0;for(;t{"use strict";dt(),Vo(),Vo(),ar(),Il=class{constructor(e,t){this.frameBufferBound=!1,this.itemsToPoll=[],this.gl=e,this.version=t,this.getExtensions(),this.vertexbuffer=this.createVertexbuffer(),this.framebuffer=this.createFramebuffer(),this.queryVitalParameters()}allocateTexture(e,t,n,i){let u=this.gl,l=u.createTexture();u.bindTexture(u.TEXTURE_2D,l),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MIN_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MAG_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_S,u.CLAMP_TO_EDGE),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_T,u.CLAMP_TO_EDGE);let d=i?n.encode(i,e*t):null;return u.texImage2D(u.TEXTURE_2D,0,n.internalFormat,e,t,0,n.format,n.textureType,d),this.checkError(),l}updateTexture(e,t,n,i,u){let l=this.gl;l.bindTexture(l.TEXTURE_2D,e);let d=i.encode(u,t*n);l.texSubImage2D(l.TEXTURE_2D,0,0,0,t,n,i.format,i.textureType,d),this.checkError()}attachFramebuffer(e,t,n){let i=this.gl;i.bindTexture(i.TEXTURE_2D,e),i.bindFramebuffer(i.FRAMEBUFFER,this.framebuffer),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,e,0),this.checkError(),i.viewport(0,0,t,n),i.scissor(0,0,t,n)}readTexture(e,t,n,i,u,l){let d=this.gl;l||(l=1),this.frameBufferBound||this.attachFramebuffer(e,t,n);let c=this.getEncoder(u,l),s=c.allocate(t*n);return d.bindTexture(d.TEXTURE_2D,e),d.framebufferTexture2D(d.FRAMEBUFFER,d.COLOR_ATTACHMENT0,d.TEXTURE_2D,e,0),d.readPixels(0,0,t,n,d.RGBA,c.textureType,s),this.checkError(),c.decode(s,i)}isFramebufferReady(){return!0}getActiveTexture(){let e=this.gl;return`TEXTURE${e.getParameter(this.gl.ACTIVE_TEXTURE)-e.TEXTURE0}`}getTextureBinding(){return this.gl.getParameter(this.gl.TEXTURE_BINDING_2D)}getFramebufferBinding(){return this.gl.getParameter(this.gl.FRAMEBUFFER_BINDING)}setVertexAttributes(e,t){let n=this.gl;n.vertexAttribPointer(e,3,n.FLOAT,!1,20,0),n.enableVertexAttribArray(e),t!==-1&&(n.vertexAttribPointer(t,2,n.FLOAT,!1,20,12),n.enableVertexAttribArray(t)),this.checkError()}createProgram(e,t){let n=this.gl,i=n.createProgram();return n.attachShader(i,e),n.attachShader(i,t),n.linkProgram(i),i}compileShader(e,t){let n=this.gl,i=n.createShader(t);if(!i)throw new Error(`createShader() returned null with type ${t}`);if(n.shaderSource(i,e),n.compileShader(i),n.getShaderParameter(i,n.COMPILE_STATUS)===!1)throw new Error(`Failed to compile shader: ${n.getShaderInfoLog(i)} Shader source: -${e}`);return o}deleteShader(e){this.gl.deleteShader(e)}bindTextureToUniform(e,t,n){let o=this.gl;o.activeTexture(o.TEXTURE0+t),this.checkError(),o.bindTexture(o.TEXTURE_2D,e),this.checkError(),o.uniform1i(n,t),this.checkError()}draw(){this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),this.checkError()}checkError(){if(pe.debug){let e=this.gl,t=e.getError(),n="";switch(t){case e.NO_ERROR:return;case e.INVALID_ENUM:n="INVALID_ENUM";break;case e.INVALID_VALUE:n="INVALID_VALUE";break;case e.INVALID_OPERATION:n="INVALID_OPERATION";break;case e.INVALID_FRAMEBUFFER_OPERATION:n="INVALID_FRAMEBUFFER_OPERATION";break;case e.OUT_OF_MEMORY:n="OUT_OF_MEMORY";break;case e.CONTEXT_LOST_WEBGL:n="CONTEXT_LOST_WEBGL";break;default:n=`Unknown WebGL Error: ${t.toString(16)}`}throw new Error(n)}}deleteTexture(e){this.gl.deleteTexture(e)}deleteProgram(e){this.gl.deleteProgram(e)}getEncoder(e,t,n=0){if(this.version===2)return new H0(this.gl,t);switch(e){case"float":return n===1||this.isRenderFloat32Supported?new Nu(this.gl,t):new Nu(this.gl,t,this.textureHalfFloatExtension.HALF_FLOAT_OES);case"int":throw new Error("not implemented");case"byte":return new W0(this.gl,t);default:throw new Error(`Invalid dataType: ${e}`)}}clearActiveTextures(){let e=this.gl;for(let t=0;tthis.isTimerResultAvailable(e)),this.getTimerResult(e)}async createAndWaitForFence(){let e=this.createFence(this.gl);return this.pollFence(e)}createFence(e){let t,n=e,o=n.fenceSync(n.SYNC_GPU_COMMANDS_COMPLETE,0);return e.flush(),o===null?t=()=>!0:t=()=>{let u=n.clientWaitSync(o,0,0);return u===n.ALREADY_SIGNALED||u===n.CONDITION_SATISFIED},{query:o,isFencePassed:t}}async pollFence(e){return new Promise(t=>{this.addItemToPoll(()=>e.isFencePassed(),()=>t())})}pollItems(){let e=TE(this.itemsToPoll.map(t=>t.isDoneFn));for(let t=0;t<=e;++t){let{resolveFn:n}=this.itemsToPoll[t];n()}this.itemsToPoll=this.itemsToPoll.slice(e+1)}async addItemToPoll(e,t){this.itemsToPoll.push({isDoneFn:e,resolveFn:t}),!(this.itemsToPoll.length>1)&&await Cp(()=>(this.pollItems(),this.itemsToPoll.length===0))}}});function bv(e){let t;if((!e||e==="webgl2")&&"webgl2"in qr?t=qr.webgl2:(!e||e==="webgl")&&"webgl"in qr&&(t=qr.webgl),!t)try{let o=OE();t=zf(o,e)}catch{let o=IE();t=zf(o,e)}e=e||t.version===1?"webgl":"webgl2";let n=t.gl;return qr[e]=t,n.isContextLost()?(delete qr[e],bv(e)):(n.disable(n.DEPTH_TEST),n.disable(n.STENCIL_TEST),n.disable(n.BLEND),n.disable(n.DITHER),n.disable(n.POLYGON_OFFSET_FILL),n.disable(n.SAMPLE_COVERAGE),n.enable(n.SCISSOR_TEST),n.enable(n.CULL_FACE),n.cullFace(n.BACK),t)}function zf(e,t){let n={alpha:!1,depth:!1,antialias:!1,stencil:!1,preserveDrawingBuffer:!1,premultipliedAlpha:!1,failIfMajorPerformanceCaveat:!1},o,u=n;if((!t||t==="webgl2")&&(o=e.getContext("webgl2",u),o))try{return new Qu(o,2)}catch(l){We.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl2'. Error: ${l}`)}if((!t||t==="webgl")&&(o=e.getContext("webgl",u)||e.getContext("experimental-webgl",u),o))try{return new Qu(o,1)}catch(l){We.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl' or 'experimental-webgl'. Error: ${l}`)}throw new Error("WebGL is not supported")}function IE(){if(typeof document>"u")throw new TypeError("failed to create canvas: document is not supported");let e=document.createElement("canvas");return e.width=1,e.height=1,e}function OE(){if(typeof OffscreenCanvas>"u")throw new TypeError("failed to create offscreen canvas: OffscreenCanvas is not supported");return new OffscreenCanvas(1,1)}var qr,EE=D(()=>{"use strict";Rt(),SE(),qr={}}),yv,AE=D(()=>{"use strict";it(),Rt(),$E(),EE(),yv=class{get contextId(){return pe.webgl.contextId}set contextId(e){pe.webgl.contextId=e}get matmulMaxBatchSize(){return pe.webgl.matmulMaxBatchSize}set matmulMaxBatchSize(e){pe.webgl.matmulMaxBatchSize=e}get textureCacheMode(){return pe.webgl.textureCacheMode}set textureCacheMode(e){pe.webgl.textureCacheMode=e}get pack(){return pe.webgl.pack}set pack(e){pe.webgl.pack=e}get async(){return pe.webgl.async}set async(e){pe.webgl.async=e}initialize(){try{return this.glContext=bv(this.contextId),typeof this.matmulMaxBatchSize!="number"&&(this.matmulMaxBatchSize=16),typeof this.textureCacheMode!="string"&&(this.textureCacheMode="full"),typeof this.pack!="boolean"&&(this.pack=!1),typeof this.async!="boolean"&&(this.async=!1),We.setWithEnv(pe),pe.webgl.context||Object.defineProperty(pe.webgl,"context",{value:this.glContext.gl}),We.verbose("WebGLBackend",`Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${this.matmulMaxBatchSize}; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${this.async}.`),!0}catch(e){return We.warning("WebGLBackend",`Unable to initialize WebGLBackend. ${e}`),!1}}createSessionHandler(e){return new gv(this,e)}dispose(){this.glContext.dispose()}}});async function _v(e){if(e){let t=typeof e=="string"?[e]:e;for(let n of t){let o=Ol.get(n);if(o)return o;let u=await kE(n);if(u)return u}}else return _v(["webgl"]);throw new Error("no available backend to use")}async function kE(e){let t=wv;if(typeof t[e]<"u"&&PE(t[e])){let n=t[e],o=n.initialize();if(typeof o=="object"&&"then"in o&&(o=await o),o)return Ol.set(e,n),n}}function PE(e){let t=e;return"initialize"in t&&typeof t.initialize=="function"&&"createSessionHandler"in t&&typeof t.createSessionHandler=="function"&&"dispose"in t&&typeof t.dispose=="function"}var Ol,wv,CE=D(()=>{"use strict";AE(),Ol=new Map,wv={webgl:new yv}}),Mf,vv,DE=D(()=>{"use strict";Rt(),Mf=class{constructor(e,t){this.op=e,this.node=t}},vv=class{constructor(e,t,n){this.graph=e,this.profiler=n,this.initialize(t)}initialize(e){this.profiler.event("session","ExecutionPlan.initialize",()=>{let t=this.graph.getNodes();if(t.length!==e.length)throw new Error("The size of nodes and OPs do not match.");this._ops=e.map((n,o)=>new Mf(n,t[o])),this.reset(),this._starter=[],this._ops.forEach((n,o)=>{let u=!0;for(let l of n.node.inputs)if(!this._values[l]&&this.graph.getInputIndices().indexOf(l)===-1){u=!1;break}u&&this._starter.push(o)})})}reset(){this._values=this.graph.getValues().map(e=>e.tensor)}async execute(e,t){return this.profiler.event("session","ExecutionPlan.execute",async()=>{this.reset();let n=e.createInferenceHandler(),o=this.graph.getInputIndices();if(t.length!==o.length)throw new Error(`number of input tensors don't match the number of inputs to the model: actual: ${t.length} expected: ${o.length}`);t.forEach((r,i)=>{let a=o[i];this._values[a]=r});let u=this._starter.slice(0),l=this.graph.getValues(),d=this.graph.getNodes(),c=0;for(;cthis._values[b]);if(a.indexOf(void 0)!==-1)throw new Error(`unresolved input detected: op: ${i.node}`);let p=a;We.verbose("ExecPlan",`Running op:${i.node.name} (${p.map((b,$)=>`'${i.node.inputs[$]}': ${b.type}[${b.dims.join(",")}]`).join(", ")})`);let h=await this.profiler.event("node",i.node.name,async()=>i.op.impl(n,p,i.op.context));if(h.length!==i.node.outputs.length)throw new Error("the size of output does not match model definition.");h.forEach((b,$)=>{let _=i.node.outputs[$];if(this._values[_])throw new Error(`output [${_}] already has value: op:${i.node.name}`);this._values[_]=b});let m=new Set;h.forEach((b,$)=>{let _=i.node.outputs[$];for(let y of l[_].to){let T=d[y],S=!0;for(let O of T.inputs)if(!this._values[O]){S=!1;break}S&&m.add(y)}}),u.push(...m)}let s=[];for(let r=0;r{"use strict";Mo(),we=fe(_i()),Tn(),Ce(),el=class ai{constructor(t){if(this._attributes=new Map,t!=null){for(let n of t)n instanceof we.onnx.AttributeProto?this._attributes.set(n.name,[ai.getValue(n),ai.getType(n)]):n instanceof Au.Attribute&&this._attributes.set(n.name(),[ai.getValue(n),ai.getType(n)]);if(this._attributes.sizemt.fromProto(u));if(t instanceof Au.Attribute)return o.map(u=>mt.fromOrtTensor(u))}return n===we.onnx.AttributeProto.AttributeType.STRING&&t instanceof we.onnx.AttributeProto?ku(o):n===we.onnx.AttributeProto.AttributeType.STRINGS&&t instanceof we.onnx.AttributeProto?o.map(ku):o}static getValueNoCheck(t){return t instanceof we.onnx.AttributeProto?this.getValueNoCheckFromOnnxFormat(t):this.getValueNoCheckFromOrtFormat(t)}static getValueNoCheckFromOnnxFormat(t){switch(t.type){case we.onnx.AttributeProto.AttributeType.FLOAT:return t.f;case we.onnx.AttributeProto.AttributeType.INT:return t.i;case we.onnx.AttributeProto.AttributeType.STRING:return t.s;case we.onnx.AttributeProto.AttributeType.TENSOR:return t.t;case we.onnx.AttributeProto.AttributeType.GRAPH:return t.g;case we.onnx.AttributeProto.AttributeType.FLOATS:return t.floats;case we.onnx.AttributeProto.AttributeType.INTS:return t.ints;case we.onnx.AttributeProto.AttributeType.STRINGS:return t.strings;case we.onnx.AttributeProto.AttributeType.TENSORS:return t.tensors;case we.onnx.AttributeProto.AttributeType.GRAPHS:return t.graphs;default:throw new Error(`unsupported attribute type: ${we.onnx.AttributeProto.AttributeType[t.type]}`)}}static getValueNoCheckFromOrtFormat(t){switch(t.type()){case Pt.AttributeType.FLOAT:return t.f();case Pt.AttributeType.INT:return t.i();case Pt.AttributeType.STRING:return t.s();case Pt.AttributeType.TENSOR:return t.t();case Pt.AttributeType.GRAPH:return t.g();case Pt.AttributeType.FLOATS:return t.floatsArray();case Pt.AttributeType.INTS:{let n=[];for(let o=0;o{"use strict";RE(),Mo(),ca=fe(_i()),Tn(),Ce(),tl={from:(e,t)=>new Bf(e,t)},Ht=class{constructor(e){this._from=void 0,this._to=[],this.tensor=void 0,this.type=void 0,e&&(this.type=pt.tensorValueTypeFromProto(e.type.tensorType))}get from(){return this._from}get to(){return this._to}},pa=class{constructor(e,t){e instanceof ca.onnx.NodeProto?(this.name=e.name,this.opType=e.opType,this.attributes=new el(e.attribute)):e instanceof D0.Node&&(this.name=t??e.name(),this.opType=e.opType(),this.attributes=new el(pt.tensorAttributesFromORTFormat(e))),this.inputs=[],this.outputs=[],this.executeNode=!0}},Bf=class{constructor(e,t){if(!e)throw new TypeError("graph is empty");this.buildGraph(e),this.transformGraph(t),this.checkIsAcyclic()}getInputIndices(){return this._allInputIndices}getInputNames(){return this._allInputNames}getOutputIndices(){return this._allOutputIndices}getOutputNames(){return this._allOutputNames}getValues(){return this._allData}getNodes(){return this._nodes}buildGraph(e){if(e instanceof ca.onnx.GraphProto)this.buildGraphFromOnnxFormat(e);else if(e instanceof P0.Graph)this.buildGraphFromOrtFormat(e);else throw new TypeError("Graph type is not supported.")}buildGraphFromOnnxFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map;if(!e.input)throw new Error("missing information in graph: input");let o=[];for(let u of e.input){if(t.has(u.name))throw new Error(`duplicated input name: ${u.name}`);let l=this._allData.push(new Ht(u))-1;t.set(u.name,l),o.push(u.name)}if(!e.initializer)throw new Error("missing information in graph: initializer");for(let u of e.initializer){let l=t.get(u.name);if(l===void 0){let d=new Ht;d.type={shape:{dims:pt.tensorDimsFromProto(u.dims)},tensorType:pt.tensorDataTypeFromProto(u.dataType)},l=this._allData.push(d)-1,t.set(u.name,l)}this._allData[l]._from=-1,this._allData[l].tensor=mt.fromProto(u)}for(let u=0;u"u"&&(s=this._allData.push(new Ht)-1,t.set(c,s)),l.outputs.push(s),this._allData[s]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${s}`);if(this._allData[s]._from=u,d.opType==="Constant"){if(!d.attribute||d.attribute.length!==1||!d.attribute[0].t)throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(!d.output||d.output.length!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[s]._from=-1,this._allData[s].tensor=mt.fromProto(d.attribute[0].t)}}}for(let u=0;u"u"){if(c===""&&(d.input.length===3||d.input.length===4)&&d.opType==="Resize")continue;throw new Error(`unrecognized input '${c}' for node: ${d.name}`)}l.inputs.push(s),this._allData[s]._to.push(u)}}return!0}buildGraphFromOrtFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map,o=[];for(let u=0;u"u"&&(r=this._allData.push(new Ht)-1,t.set(s,r)),l.outputs.push(r),this._allData[r]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${r}`);if(this._allData[r]._from=u,d.opType()==="Constant"){if(d.attributesLength()!==1||!d.attributes(0).t())throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(d.outputsLength()!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[r]._from=-1,this._allData[r].tensor=mt.fromOrtTensor(d.attributes(0).t())}}}for(let u=0;u"u")throw new Error(`unrecognized input '${s}' for node: ${d.name()}`);l.inputs.push(r),this._allData[r]._to.push(u)}}}checkIsAcyclic(){let e=new Set;this._allInputIndices.forEach(o=>{this._allData[o]._to.forEach(u=>{e.add(u)})});let t=Array.from(e),n=new Array(this._nodes.length).fill("white");for(;t.length>0;){let o=t.pop();n[o]==="gray"?n[o]="black":(t.push(o),n[o]="gray",this._nodes[o].outputs.forEach(u=>{let l=this._allData[u];if(typeof l.tensor<"u")throw new Error("node outputs should not be initialized");if(l._from!==o)throw new Error("from property of the Value object doesn't match index of Node being processed");l._to.forEach(d=>{if(n[d]==="gray")throw new Error("model graph is cyclic");n[d]==="white"&&t.push(d)})}))}}transformGraph(e){this.removeAllIdentityNodes(),this.removeAllDropoutNodes(),this.fuseConvActivationNodes(),e&&e.transformGraph(this),this.finalizeGraph()}finalizeGraph(){let e=0,t=new Array(this._nodes.length,0),n=0;for(let o=0;o{this._allData[u]._from=-2});this._nodes.splice(n,this._nodes.length-n);for(let o=0;o=0)u._to[l]=t[u._to[l]];else throw new Error("Trying to update a removed node")}e=0;for(let o=0;o0){let u=-1;this._allData[o].from!==void 0&&this._allData[o].from!==-1?(u=this._nodes[this._allData[o].from].outputs.indexOf(o+e),u!==-1&&(this._nodes[this._allData[o].from].outputs[u]=o)):(u=this._allInputIndices.indexOf(o+e),u!==-1&&(this._allInputIndices[u]=o)),this._allData[o].to.forEach(l=>{u=this._nodes[l].inputs.indexOf(o+e),u!==-1&&(this._nodes[l].inputs[u]=o)}),this._allData[o].to.length===0&&(u=this._allOutputIndices.indexOf(o+e),u!==-1&&(this._allOutputIndices[u]=o))}}}deleteNode(e){let t=this._nodes[e];if(t.outputs.length>1){for(let d=1;d0)throw new Error("Node deletion with more than one output connected to other nodes is not supported. ")}t.executeNode=!1;let n=t.inputs[0],o=t.outputs[0],u=this._allData[o].to;for(let d=0;d0)for(let d of u){let c=this._nodes[d].inputs.indexOf(o);if(c===-1)throw new Error("The Node object doesn't have the output Value in it's 'inputs' property ");this._nodes[d].inputs[c]=n,this._allData[n].to.push(d)}}removeAllDropoutNodes(){let e=0;for(let t of this._nodes){if(t.opType==="Dropout"){if(t.inputs.length!==1)throw new Error("Dropout nodes should only contain one input. ");if(t.outputs.length!==1&&t.outputs.length!==2)throw new Error("Dropout nodes should contain either 1 or 2 output(s)");if(t.outputs.length===2&&this._allData[t.outputs[1]]._to.length!==0)throw new Error("Dropout nodes's second output should not be referenced by other nodes");this.deleteNode(e)}e++}}removeAllIdentityNodes(){let e=0;for(let t of this._nodes)t.opType==="Identity"&&this.deleteNode(e),e++}isActivation(e){switch(e.opType){case"Relu":case"Sigmoid":case"Clip":return!0;default:return!1}}fuseConvActivationNodes(){for(let e of this._nodes)if(e.opType==="Conv"){let t=this._allData[e.outputs[0]]._to;if(t.length===1&&this.isActivation(this._nodes[t[0]])){let n=this._nodes[t[0]];if(n.opType==="Clip")if(n.inputs.length===1)try{e.attributes.set("activation_params","floats",[n.attributes.getFloat("min"),n.attributes.getFloat("max")])}catch{e.attributes.set("activation_params","floats",[mi,gi])}else if(n.inputs.length>=3&&this._allData[n.inputs[1]].tensor!==void 0&&this._allData[n.inputs[2]].tensor!==void 0)e.attributes.set("activation_params","floats",[this._allData[n.inputs[1]].tensor.floatData[0],this._allData[n.inputs[2]].tensor.floatData[0]]);else continue;e.attributes.set("activation","string",n.opType),this.deleteNode(t[0])}}}}}),Ff,Lf,xv,zE=D(()=>{"use strict";Ff=fe(Pe()),NE(),Mo(),Lf=fe(_i()),Ce(),xv=class{constructor(){}load(e,t,n){let o;if(!n)try{this.loadFromOnnxFormat(e,t);return}catch(u){if(n!==void 0)throw u;o=u}try{this.loadFromOrtFormat(e,t)}catch(u){throw n!==void 0?u:new Error(`Failed to load model as ONNX format: ${o} -as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Lf.onnx.ModelProto.decode(e);if(Lt.longToNumber(n.irVersion)<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=n.opsetImport.map(o=>({domain:o.domain,version:Lt.longToNumber(o.version)})),this._graph=tl.from(n.graph,t)}loadFromOrtFormat(e,t){let n=new Ff.ByteBuffer(e),o=C0.InferenceSession.getRootAsInferenceSession(n).model();if(Lt.longToNumber(o.irVersion())<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=[];for(let u=0;u{"use strict";CE(),DE(),Rt(),zE(),$v=class{constructor(e={}){this._initialized=!1,this.backendHint=e.backendHint,this.profiler=Wy.create(e.profiler),this.context={profiler:this.profiler,graphInputTypes:[],graphInputDims:[]}}get inputNames(){return this._model.graph.getInputNames()}get outputNames(){return this._model.graph.getOutputNames()}startProfiling(){this.profiler.start()}endProfiling(){this.profiler.stop()}async loadModel(e,t,n){await this.profiler.event("session","Session.loadModel",async()=>{let o=await _v(this.backendHint);if(this.sessionHandler=o.createSessionHandler(this.context),this._model=new xv,typeof e=="string"){let u=e.endsWith(".ort");{let l=await(await fetch(e)).arrayBuffer();this.initialize(new Uint8Array(l),u)}}else if(ArrayBuffer.isView(e))this.initialize(e);else{let u=new Uint8Array(e,t||0,n||e.byteLength);this.initialize(u)}})}initialize(e,t){if(this._initialized)throw new Error("already initialized");this.profiler.event("session","Session.initialize",()=>{let n=this.sessionHandler.transformGraph?this.sessionHandler:void 0;this._model.load(e,n,t),this.sessionHandler.onGraphInitialized&&this.sessionHandler.onGraphInitialized(this._model.graph),this.initializeOps(this._model.graph),this._executionPlan=new vv(this._model.graph,this._ops,this.profiler)}),this._initialized=!0}async run(e){if(!this._initialized)throw new Error("session not initialized yet");return this.profiler.event("session","Session.run",async()=>{let t=this.normalizeAndValidateInputs(e),n=await this._executionPlan.execute(this.sessionHandler,t);return this.createOutput(n)})}normalizeAndValidateInputs(e){let t=this._model.graph.getInputNames();if(Array.isArray(e)){if(e.length!==t.length)throw new Error(`incorrect input array length: expected ${t.length} but got ${e.length}`)}else{if(e.size!==t.length)throw new Error(`incorrect input map size: expected ${t.length} but got ${e.size}`);let n=new Array(e.size),o=0;for(let u=0;u{"use strict";it(),Tn(),Tv=class{constructor(e){this.session=e,this.inputNames=this.session.inputNames,this.outputNames=this.session.outputNames}get inputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}get outputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}async dispose(){}async run(e,t,n){let o=new Map;for(let d in e)if(Object.hasOwnProperty.call(e,d)){let c=e[d];o.set(d,new mt(c.dims,c.type,void 0,void 0,c.data))}let u=await this.session.run(o),l={};return u.forEach((d,c)=>{l[c]=new ft(d.type,d.data,d.dims)}),l}startProfiling(){this.session.startProfiling()}endProfiling(){this.session.endProfiling()}}}),Sv={};nn(Sv,{onnxjsBackend:()=>Iv});var jf,Iv,FE=D(()=>{"use strict";ME(),BE(),jf=class{async init(){}async createInferenceSessionHandler(e,t){let n=new $v(t);return typeof e=="string"?await n.loadModel(e):await n.loadModel(e),new Tv(n)}},Iv=new jf}),El=D(()=>{"use strict"}),Ov={};nn(Ov,{default:()=>Ev});var ha,fa,Ev,LE=D(()=>{"use strict";z2(),sn(),Al(),ha="ort-wasm-proxy-worker",fa=globalThis.self?.name===ha,fa&&(self.onmessage=e=>{let{type:t,in:n}=e.data;try{switch(t){case"init-wasm":kl(n.wasm).then(()=>{Wl(n).then(()=>{postMessage({type:t})},o=>{postMessage({type:t,err:o})})},o=>{postMessage({type:t,err:o})});break;case"init-ep":{let{epName:o,env:u}=n;Kl(u,o).then(()=>{postMessage({type:t})},l=>{postMessage({type:t,err:l})});break}case"copy-from":{let{buffer:o}=n,u=Po(o);postMessage({type:t,out:u});break}case"create":{let{model:o,options:u}=n;Xl(o,u).then(l=>{postMessage({type:t,out:l})},l=>{postMessage({type:t,err:l})});break}case"release":Zl(n),postMessage({type:t});break;case"run":{let{sessionId:o,inputIndices:u,inputs:l,outputIndices:d,options:c}=n;Yl(o,u,l,d,new Array(d.length).fill(null),c).then(s=>{s.some(r=>r[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:s},Ql([...l,...s]))},s=>{postMessage({type:t,err:s})});break}case"end-profiling":Jl(n),postMessage({type:t});break;default:}}catch(o){postMessage({type:t,err:o})}}),Ev=fa?null:e=>new Worker(e??ct,{type:"module",name:ha})}),Av={};nn(Av,{default:()=>kv});async function Uf(e={}){var t=e,n=!!globalThis.window,o=!!globalThis.WorkerGlobalScope,u=o&&self.name?.startsWith("em-pthread");t.mountExternalData=(f,g)=>{f.startsWith("./")&&(f=f.substring(2)),(t.Yc||(t.Yc=new Map)).set(f,g)},t.unmountExternalData=()=>{delete t.Yc,delete t.Zd,delete t.Yd,delete t.$d},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let l=f=>async(...g)=>{try{if(t.Xc)throw Error("Session already started");let x=t.Xc={Kd:g[0],errors:[]},v=await f(...g);if(t.Xc!==x)throw Error("Session mismatch");t.dd?.flush();let E=x.errors;if(0R),0{if(f==="webgpu"){[t.dd,t.Ad,t.Ed,t.ed,t.Dd,t.$b,t.Fd,t.Hd,t.Bd,t.Cd,t.Gd]=g;let x=t.dd;t.jsepRegisterBuffer=(v,E,k,R)=>x.registerBuffer(v,E,k,R),t.jsepGetBuffer=v=>x.getBuffer(v),t.jsepCreateDownloader=(v,E,k)=>x.createDownloader(v,E,k),t.jsepOnCreateSession=v=>{x.onCreateSession(v)},t.jsepOnReleaseSession=v=>{x.onReleaseSession(v)},t.jsepOnRunStart=v=>x.onRunStart(v),t.Id=(v,E)=>{x.upload(v,E)}}else if(f==="webnn"){let x=g[0];[t.Sd,t.sd,t.webnnEnsureTensor,t.td,t.webnnDownloadTensor,t.Rd,t.webnnEnableTraceEvent]=g.slice(1),t.webnnReleaseTensorId=t.sd,t.webnnUploadTensor=t.td,t.webnnRegisterMLContext=t.Rd,t.webnnOnRunStart=v=>x.onRunStart(v),t.webnnOnRunEnd=x.onRunEnd.bind(x),t.webnnOnReleaseSession=v=>{x.onReleaseSession(v)},t.webnnCreateMLTensorDownloader=(v,E)=>x.createMLTensorDownloader(v,E),t.webnnRegisterMLTensor=(v,E,k,R)=>x.registerMLTensor(v,E,k,R),t.webnnCreateMLContext=v=>x.createMLContext(v),t.webnnRegisterGraphInput=x.registerGraphInput.bind(x),t.webnnIsGraphInput=x.isGraphInput.bind(x),t.webnnRegisterGraphOutput=x.registerGraphOutput.bind(x),t.webnnIsGraphOutput=x.isGraphOutput.bind(x),t.webnnCreateTemporaryTensor=x.createTemporaryTensor.bind(x),t.webnnIsGraphInputOutputTypeSupported=x.isGraphInputOutputTypeSupported.bind(x)}};let d=()=>{let f=g=>(...x)=>{let v=zt;return x=g(...x),zt!=v?new Promise((E,k)=>{vs={resolve:E,reject:k}}):x};(()=>{for(let g of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[g]=f(t[g])})(),l!==void 0&&(t._OrtRun=l(t._OrtRun),t._OrtRunWithBinding=l(t._OrtRunWithBinding)),d=void 0};t.asyncInit=()=>{d?.()};var c,s,r=(f,g)=>{throw g},i=import.meta.url,a="";if(n||o){try{a=new URL(".",i).href}catch{}o&&(s=f=>{var g=new XMLHttpRequest;return g.open("GET",f,!1),g.responseType="arraybuffer",g.send(null),new Uint8Array(g.response)}),c=async f=>{if(P(f))return new Promise((x,v)=>{var E=new XMLHttpRequest;E.open("GET",f,!0),E.responseType="arraybuffer",E.onload=()=>{E.status==200||E.status==0&&E.response?x(E.response):v(E.status)},E.onerror=v,E.send(null)});var g=await fetch(f,{credentials:"same-origin"});if(g.ok)return g.arrayBuffer();throw Error(g.status+" : "+g.url)}}var p,h,m,b,$,_,y=console.log.bind(console),T=console.error.bind(console),S=y,O=T,A=!1,P=f=>f.startsWith("file://");function I(){tr.buffer!=Z.buffer&&K()}if(u){let f=function(g){try{var x=g.data,v=x.Sc;if(v==="load"){let E=[];self.onmessage=k=>E.push(k),_=()=>{postMessage({Sc:"loaded"});for(let k of E)f(k);self.onmessage=f};for(let k of x.xd)t[k]&&!t[k].proxy||(t[k]=(...R)=>{postMessage({Sc:"callHandler",vd:k,args:R})},k=="print"&&(S=t[k]),k=="printErr"&&(O=t[k]));tr=x.Od,K(),h=x.Pd,le(),Zi()}else if(v==="run"){(function(E){var k=(I(),C)[E+52>>>2>>>0];E=(I(),C)[E+56>>>2>>>0],Dc(k,k-E),ge(k)})(x.Rc),Is(x.Rc,0,0,1,0,0),Rd(),ys(x.Rc),V||(Oc(),V=!0);try{eT(x.Md,x.bd)}catch(E){if(E!="unwind")throw E}}else x.target!=="setimmediate"&&(v==="checkMailbox"?V&&Vi():v&&(O(`worker: received unknown command ${v}`),O(x)))}catch(E){throw Ec(),E}};var L=f,V=!1;self.onunhandledrejection=g=>{throw g.reason||g},self.onmessage=f}var Z,q,N,ue,w,C,U,H,G,J,B,X=!1;function K(){var f=tr.buffer;t.HEAP8=Z=new Int8Array(f),N=new Int16Array(f),t.HEAPU8=q=new Uint8Array(f),ue=new Uint16Array(f),t.HEAP32=w=new Int32Array(f),t.HEAPU32=C=new Uint32Array(f),U=new Float32Array(f),H=new Float64Array(f),G=new BigInt64Array(f),J=new BigUint64Array(f)}function M(){X=!0,u?_():Gt.sb()}function ne(f){throw O(f="Aborted("+f+")"),A=!0,f=new WebAssembly.RuntimeError(f+". Build with -sASSERTIONS for more info."),$?.(f),f}function Q(){return{a:{ma:SS,hb:TS,g:tT,J:rT,f:nT,o:iT,i:oT,$:sT,b:aT,S:uT,Ia:Ld,n:lT,aa:qd,Ya:Gd,Ea:Hd,Ga:Wd,Za:Kd,Wa:Xd,Pa:Zd,Va:Yd,ka:Jd,Fa:Qd,Ca:ec,Xa:tc,Da:rc,cb:dT,fa:pT,xa:hT,va:mT,ea:bT,N:yT,H:_T,wa:wT,_:OT,ya:ET,Sa:AT,Aa:PT,Ja:CT,ta:DT,ga:RT,Ra:ys,$a:NT,Q:FT,r:qT,c:gs,ib:GT,y:HT,M:WT,D:KT,l:XT,s:dc,jb:ZT,I:YT,R:JT,j:QT,u:eS,q:tS,k:rS,Ma:nS,Na:iS,Oa:oS,Ka:fc,La:mc,ua:gc,eb:aS,bb:dS,v:cS,ba:pS,ha:hS,ab:uS,V:fS,_a:mS,Ba:gS,F:sS,T:bS,la:Ki,za:_S,gb:yS,fb:wS,Ta:wc,Ua:vc,Ha:cs,U:xc,ja:$c,Qa:Tc,ia:Sc,lb:aI,na:rI,mb:sI,oa:tI,G:GS,e:AS,t:OS,w:IS,B:FS,nb:JS,Z:YS,x:CS,pa:QS,X:nI,ca:ZS,ob:XS,pb:KS,O:LS,qa:WS,qb:HS,L:VS,Y:eI,d:ES,A:PS,m:kS,kb:uI,p:RS,z:NS,C:DS,E:zS,K:jS,ra:qS,P:iI,da:US,W:oI,rb:BS,sa:MS,h:xS,a:tr,db:ds}}}async function le(){function f(v,E){var k=Gt=v.exports;v={};for(let[R,j]of Object.entries(k))typeof j=="function"?(k=zT(j),v[R]=k):v[R]=j;return Gt=v,Gt=(function(){var R=Gt,j=ee=>he=>ee(he)>>>0,Y=ee=>()=>ee()>>>0;return(R=Object.assign({},R)).tb=j(R.tb),R.Xb=Y(R.Xb),R.Zb=j(R.Zb),R.lc=j(R.lc),R.mc=Y(R.mc),R.qc=j(R.qc),R})(),Cd.push(Gt._b),Ic=(v=Gt).tb,Oc=v.ub,t._OrtInit=v.vb,t._OrtGetLastError=v.wb,t._OrtCreateSessionOptions=v.xb,t._OrtAppendExecutionProvider=v.yb,t._OrtAddFreeDimensionOverride=v.zb,t._OrtAddSessionConfigEntry=v.Ab,t._OrtReleaseSessionOptions=v.Bb,t._OrtCreateSession=v.Cb,t._OrtReleaseSession=v.Db,t._OrtGetInputOutputCount=v.Eb,t._OrtGetInputOutputMetadata=v.Fb,t._OrtFree=v.Gb,t._OrtCreateTensor=v.Hb,t._OrtGetTensorData=v.Ib,t._OrtReleaseTensor=v.Jb,t._OrtCreateRunOptions=v.Kb,t._OrtAddRunConfigEntry=v.Lb,t._OrtReleaseRunOptions=v.Mb,t._OrtCreateBinding=v.Nb,t._OrtBindInput=v.Ob,t._OrtBindOutput=v.Pb,t._OrtClearBoundOutputs=v.Qb,t._OrtReleaseBinding=v.Rb,t._OrtRunWithBinding=v.Sb,t._OrtRun=v.Tb,t._OrtEndProfiling=v.Ub,t._JsepOutput=v.Vb,t._JsepGetNodeName=v.Wb,Xi=v.Xb,Mt=t._free=v.Yb,Mn=t._malloc=v.Zb,Is=v.ac,Ec=v.bc,Ac=v.cc,kc=v.dc,Os=v.ec,Pc=v.fc,Cc=v.gc,_e=v.hc,Bn=v.ic,Dc=v.jc,ge=v.kc,Es=v.lc,be=v.mc,Rc=v.nc,As=v.oc,Nc=v.pc,zc=v.qc,Mc=v.rc,ks=v.sc,Bc=v.tc,Fc=v.uc,Lc=v.vc,jc=v.wc,Uc=v.xc,Vc=v.yc,qc=v.zc,Gc=v.Ac,Hc=v.Bc,Wc=v.Cc,Kc=v.Dc,Xc=v.Ec,Zc=v.Fc,Yc=v.Gc,Jc=v.Hc,Qc=v.Ic,ep=v.Jc,tp=v.Kc,rp=v.Lc,np=v.Mc,ip=v.Nc,op=v.Pc,sp=v.Qc,ap=v.$c,up=v.ad,lp=v.fd,dp=v.kd,cp=v.ld,pp=v.md,hp=v.nd,fp=v.od,mp=v.pd,gp=v.qd,bp=v.rd,yp=v.wd,_p=v.Ud,wp=v.Vd,vp=v.Wd,xp=v.Xd,h=E,Gt}var g,x=Q();return t.instantiateWasm?new Promise(v=>{t.instantiateWasm(x,(E,k)=>{v(f(E,k))})}):u?f(new WebAssembly.Instance(h,Q()),h):(B??=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",a):a+"ort-wasm-simd-threaded.jsep.wasm":new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href,g=await(async function(v){var E=B;if(!p&&!P(E))try{var k=fetch(E,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(k,v)}catch(R){O(`wasm streaming compile failed: ${R}`),O("falling back to ArrayBuffer instantiation")}return(async function(R,j){try{var Y=await(async function(ee){if(!p)try{var he=await c(ee);return new Uint8Array(he)}catch{}if(ee==B&&p)ee=new Uint8Array(p);else{if(!s)throw"both async and sync fetching of the wasm failed";ee=s(ee)}return ee})(R);return await WebAssembly.instantiate(Y,j)}catch(ee){O(`failed to asynchronously prepare wasm: ${ee}`),ne(ee)}})(E,v)})(x),f(g.instance,g.module))}class Ae{name="ExitStatus";constructor(g){this.message=`Program terminated with exit(${g})`,this.status=g}}var Oe=f=>{f.terminate(),f.onmessage=()=>{}},je=[],at=0,xr=null,Bi=f=>{er.length==0&&(zd(),Nd(er[0]));var g=er.pop();if(!g)return 6;Nn.push(g),$r[f.Rc]=g,g.Rc=f.Rc;var x={Sc:"run",Md:f.Ld,bd:f.bd,Rc:f.Rc};return g.postMessage(x,f.jd),0},Qt=0,Le=(f,g,...x)=>{var v,E=16*x.length,k=be(),R=Es(E),j=R>>>3;for(v of x)typeof v=="bigint"?((I(),G)[j++>>>0]=1n,(I(),G)[j++>>>0]=v):((I(),G)[j++>>>0]=0n,(I(),H)[j++>>>0]=v);return f=Ac(f,0,E,R,g),ge(k),f};function ds(f){if(u)return Le(0,1,f);if(m=f,!(0{if(m=f,u)throw Pd(f),"unwind";ds(f)},er=[],Nn=[],Cd=[],$r={},Dd=f=>{var g=f.Rc;delete $r[g],er.push(f),Nn.splice(Nn.indexOf(f),1),f.Rc=0,kc(g)};function Rd(){Cd.forEach(f=>f())}var Nd=f=>new Promise(g=>{f.onmessage=E=>{var k=E.data;if(E=k.Sc,k.Zc&&k.Zc!=Xi()){var R=$r[k.Zc];R?R.postMessage(k,k.jd):O(`Internal error! Worker sent a message "${E}" to target pthread ${k.Zc}, but that thread no longer exists!`)}else E==="checkMailbox"?Vi():E==="spawnThread"?Bi(k):E==="cleanupThread"?Ui(()=>{Dd($r[k.Nd])}):E==="loaded"?(f.loaded=!0,g(f)):k.target==="setimmediate"?f.postMessage(k):E==="uncaughtException"?f.onerror(k.error):E==="callHandler"?t[k.vd](...k.args):E&&O(`worker sent an unknown command ${E}`)},f.onerror=E=>{throw O(`worker sent an error! ${E.filename}:${E.lineno}: ${E.message}`),E};var x,v=[];for(x of[])t.propertyIsEnumerable(x)&&v.push(x);f.postMessage({Sc:"load",xd:v,Od:tr,Pd:h})});function zd(){var f=new Worker((()=>{let g=URL;return import.meta.url>"file:"&&import.meta.url<"file;"?new g("ort.all.bundle.min.mjs",import.meta.url):new URL(import.meta.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});er.push(f)}var tr,eT=(f,g)=>{Qt=0,f=ks(f,g),0>>=0);return(I(),Z)[g.Tc+12>>>0]==0&&(Md(g,!0),Li--),Bd(g,!1),Fi.push(g),zc(f)}var cn=0,rT=()=>{_e(0,0);var f=Fi.pop();Rc(f.cd),cn=0};function Md(f,g){g=g?1:0,(I(),Z)[f.Tc+12>>>0]=g}function Bd(f,g){g=g?1:0,(I(),Z)[f.Tc+13>>>0]=g}class ps{constructor(g){this.cd=g,this.Tc=g-24}}var hs=f=>{var g=cn;if(!g)return Bn(0),0;var x=new ps(g);(I(),C)[x.Tc+16>>>2>>>0]=g;var v=(I(),C)[x.Tc+4>>>2>>>0];if(!v)return Bn(0),g;for(var E of f){if(E===0||E===v)break;if(Nc(E,v,x.Tc+16))return Bn(E),g}return Bn(v),g};function nT(){return hs([])}function iT(f){return hs([f>>>0])}function oT(f,g,x,v){return hs([f>>>0,g>>>0,x>>>0,v>>>0])}var sT=()=>{var f=Fi.pop();f||ne("no exception to throw");var g=f.cd;throw(I(),Z)[f.Tc+13>>>0]==0&&(Fi.push(f),Bd(f,!0),Md(f,!1),Li++),As(g),cn=g};function aT(f,g,x){var v=new ps(f>>>=0);throw g>>>=0,x>>>=0,(I(),C)[v.Tc+16>>>2>>>0]=0,(I(),C)[v.Tc+4>>>2>>>0]=g,(I(),C)[v.Tc+8>>>2>>>0]=x,As(f),Li++,cn=f}var uT=()=>Li;function Fd(f,g,x,v){return u?Le(2,1,f,g,x,v):Ld(f,g,x,v)}function Ld(f,g,x,v){if(f>>>=0,g>>>=0,x>>>=0,v>>>=0,!globalThis.SharedArrayBuffer)return 6;var E=[];return u&&E.length===0?Fd(f,g,x,v):(f={Ld:x,Rc:f,bd:v,jd:E},u?(f.Sc="spawnThread",postMessage(f,E),0):Bi(f))}function lT(f){throw cn||=f>>>0,cn}var jd=globalThis.TextDecoder&&new TextDecoder,Ud=(f,g,x,v)=>{if(x=g+x,v)return x;for(;f[g]&&!(g>=x);)++g;return g},Vd=(f,g=0,x,v)=>{if(16<(x=Ud(f,g>>>=0,x,v))-g&&f.buffer&&jd)return jd.decode(f.buffer instanceof ArrayBuffer?f.subarray(g,x):f.slice(g,x));for(v="";g(E=(240&E)==224?(15&E)<<12|k<<6|R:(7&E)<<18|k<<12|R<<6|63&f[g++])?v+=String.fromCharCode(E):(E-=65536,v+=String.fromCharCode(55296|E>>10,56320|1023&E))}}else v+=String.fromCharCode(E)}return v},Xe=(f,g,x)=>(f>>>=0)?Vd((I(),q),f,g,x):"";function qd(f,g,x){return u?Le(3,1,f,g,x):0}function Gd(f,g){if(u)return Le(4,1,f,g)}function Hd(f,g){if(u)return Le(5,1,f,g)}function Wd(f,g,x){if(u)return Le(6,1,f,g,x)}function Kd(f,g,x){return u?Le(7,1,f,g,x):0}function Xd(f,g){if(u)return Le(8,1,f,g)}function Zd(f,g,x){if(u)return Le(9,1,f,g,x)}function Yd(f,g,x,v){if(u)return Le(10,1,f,g,x,v)}function Jd(f,g,x,v){if(u)return Le(11,1,f,g,x,v)}function Qd(f,g,x,v){if(u)return Le(12,1,f,g,x,v)}function ec(f){if(u)return Le(13,1,f)}function tc(f,g){if(u)return Le(14,1,f,g)}function rc(f,g,x){if(u)return Le(15,1,f,g,x)}var dT=()=>ne(""),Nt=f=>{f>>>=0;for(var g="";;){var x=(I(),q)[f++>>>0];if(!x)return g;g+=String.fromCharCode(x)}},fs={},ms={},cT={},pn=class extends Error{constructor(f){super(f),this.name="BindingError"}};function qt(f,g,x={}){return(function(v,E,k={}){var R=E.name;if(!v)throw new pn(`type "${R}" must have a positive integer typeid pointer`);if(ms.hasOwnProperty(v)){if(k.yd)return;throw new pn(`Cannot register type '${R}' twice`)}ms[v]=E,delete cT[v],fs.hasOwnProperty(v)&&(E=fs[v],delete fs[v],E.forEach(j=>j()))})(f,g,x)}var nc=(f,g,x)=>{switch(g){case 1:return x?v=>(I(),Z)[v>>>0]:v=>(I(),q)[v>>>0];case 2:return x?v=>(I(),N)[v>>>1>>>0]:v=>(I(),ue)[v>>>1>>>0];case 4:return x?v=>(I(),w)[v>>>2>>>0]:v=>(I(),C)[v>>>2>>>0];case 8:return x?v=>(I(),G)[v>>>3>>>0]:v=>(I(),J)[v>>>3>>>0];default:throw new TypeError(`invalid integer width (${g}): ${f}`)}};function pT(f,g,x,v,E){f>>>=0,x>>>=0,g=Nt(g>>>0);let k=R=>R;if(v=v===0n){let R=8*x;k=j=>BigInt.asUintN(R,j),E=k(E)}qt(f,{name:g,Oc:k,Vc:(R,j)=>(typeof j=="number"&&(j=BigInt(j)),j),Uc:nc(g,x,!v),Wc:null})}function hT(f,g,x,v){qt(f>>>=0,{name:g=Nt(g>>>0),Oc:function(E){return!!E},Vc:function(E,k){return k?x:v},Uc:function(E){return this.Oc((I(),q)[E>>>0])},Wc:null})}var ic=[],Tr=[0,1,,1,null,1,!0,1,!1,1];function gs(f){9<(f>>>=0)&&--Tr[f+1]===0&&(Tr[f]=void 0,ic.push(f))}var bt=f=>{if(!f)throw new pn(`Cannot use deleted val. handle = ${f}`);return Tr[f]},St=f=>{switch(f){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let g=ic.pop()||Tr.length;return Tr[g]=f,Tr[g+1]=1,g}};function bs(f){return this.Oc((I(),C)[f>>>2>>>0])}var fT={name:"emscripten::val",Oc:f=>{var g=bt(f);return gs(f),g},Vc:(f,g)=>St(g),Uc:bs,Wc:null};function mT(f){return qt(f>>>0,fT)}var gT=(f,g)=>{switch(g){case 4:return function(x){return this.Oc((I(),U)[x>>>2>>>0])};case 8:return function(x){return this.Oc((I(),H)[x>>>3>>>0])};default:throw new TypeError(`invalid float width (${g}): ${f}`)}};function bT(f,g,x){x>>>=0,qt(f>>>=0,{name:g=Nt(g>>>0),Oc:v=>v,Vc:(v,E)=>E,Uc:gT(g,x),Wc:null})}function yT(f,g,x,v,E){f>>>=0,x>>>=0,g=Nt(g>>>0);let k=j=>j;if(v===0){var R=32-8*x;k=j=>j<>>R,E=k(E)}qt(f,{name:g,Oc:k,Vc:(j,Y)=>Y,Uc:nc(g,x,v!==0),Wc:null})}function _T(f,g,x){function v(k){var R=(I(),C)[k>>>2>>>0];return k=(I(),C)[k+4>>>2>>>0],new E((I(),Z).buffer,k,R)}var E=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][g];qt(f>>>=0,{name:x=Nt(x>>>0),Oc:v,Uc:v},{yd:!0})}var rr=(f,g,x)=>{var v=(I(),q);if(g>>>=0,0=R){if(g>=x)break;v[g++>>>0]=R}else if(2047>=R){if(g+1>=x)break;v[g++>>>0]=192|R>>6,v[g++>>>0]=128|63&R}else if(65535>=R){if(g+2>=x)break;v[g++>>>0]=224|R>>12,v[g++>>>0]=128|R>>6&63,v[g++>>>0]=128|63&R}else{if(g+3>=x)break;v[g++>>>0]=240|R>>18,v[g++>>>0]=128|R>>12&63,v[g++>>>0]=128|R>>6&63,v[g++>>>0]=128|63&R,k++}}v[g>>>0]=0,f=g-E}else f=0;return f},ji=f=>{for(var g=0,x=0;x=v?g++:2047>=v?g+=2:55296<=v&&57343>=v?(g+=4,++x):g+=3}return g};function wT(f,g){qt(f>>>=0,{name:g=Nt(g>>>0),Oc(x){var v=(I(),C)[x>>>2>>>0];return v=Xe(x+4,v,!0),Mt(x),v},Vc(x,v){v instanceof ArrayBuffer&&(v=new Uint8Array(v));var E=typeof v=="string";if(!(E||ArrayBuffer.isView(v)&&v.BYTES_PER_ELEMENT==1))throw new pn("Cannot pass non-string to std::string");var k=E?ji(v):v.length,R=Mn(4+k+1),j=R+4;return(I(),C)[R>>>2>>>0]=k,E?rr(v,j,k+1):(I(),q).set(v,j>>>0),x!==null&&x.push(Mt,R),R},Uc:bs,Wc(x){Mt(x)}})}var oc=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,vT=(f,g,x)=>{if(f>>>=1,16<(g=Ud((I(),ue),f,g/2,x))-f&&oc)return oc.decode((I(),ue).slice(f,g));for(x="";f>>0];x+=String.fromCharCode(v)}return x},xT=(f,g,x)=>{if(x??=2147483647,2>x)return 0;var v=g;x=(x-=2)<2*f.length?x/2:f.length;for(var E=0;E>>1>>>0]=k,g+=2}return(I(),N)[g>>>1>>>0]=0,g-v},$T=f=>2*f.length,TT=(f,g,x)=>{var v="";f>>>=2;for(var E=0;!(E>=g/4);E++){var k=(I(),C)[f+E>>>0];if(!k&&!x)break;v+=String.fromCodePoint(k)}return v},ST=(f,g,x)=>{if(g>>>=0,x??=2147483647,4>x)return 0;var v=g;x=v+x-4;for(var E=0;E>>2>>>0]=k,(g+=4)+4>x)break}return(I(),w)[g>>>2>>>0]=0,g-v},IT=f=>{for(var g=0,x=0;x>>=0,g>>>=0,x=Nt(x>>>=0),g===2)var v=vT,E=xT,k=$T;else v=TT,E=ST,k=IT;qt(f,{name:x,Oc:R=>{var j=(I(),C)[R>>>2>>>0];return j=v(R+4,j*g,!0),Mt(R),j},Vc:(R,j)=>{if(typeof j!="string")throw new pn(`Cannot pass non-string to C++ string type ${x}`);var Y=k(j),ee=Mn(4+Y+g);return(I(),C)[ee>>>2>>>0]=Y/g,E(j,ee+4,Y+g),R!==null&&R.push(Mt,ee),ee},Uc:bs,Wc(R){Mt(R)}})}function ET(f,g){qt(f>>>=0,{zd:!0,name:g=Nt(g>>>0),Oc:()=>{},Vc:()=>{}})}function AT(f){Is(f>>>0,!o,1,!n,131072,!1),Rd()}var Ui=f=>{if(!A)try{if(f(),!(0Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function ys(f){f>>>=0,kT||(Atomics.waitAsync((I(),w),f>>>2,f).value.then(Vi),f+=128,Atomics.store((I(),w),f>>>2,1))}var Vi=()=>Ui(()=>{var f=Xi();f&&(ys(f),Cc())});function PT(f,g){(f>>>=0)==g>>>0?setTimeout(Vi):u?postMessage({Zc:f,Sc:"checkMailbox"}):(f=$r[f])&&f.postMessage({Sc:"checkMailbox"})}var _s=[];function CT(f,g,x,v,E){for(g>>>=0,E>>>=0,_s.length=0,x=E>>>3,v=E+v>>>3;x>>0]?(I(),G)[x++>>>0]:(I(),H)[x++>>>0],_s.push(k)}return(g?Ps[g]:$S[f])(..._s)}var DT=()=>{Qt=0};function RT(f){f>>>=0,u?postMessage({Sc:"cleanupThread",Nd:f}):Dd($r[f])}function NT(f){}var qi=f=>{try{f()}catch(g){ne(g)}};function zT(f){var g=(...x)=>{Gi.push(f);try{return f(...x)}finally{A||(Gi.pop(),zt&&nr===1&&Gi.length===0&&(nr=0,Qt+=1,qi(wp),typeof Fibers<"u"&&Fibers.be()))}};return uc.set(f,g),g}var nr=0,zt=null,sc=0,Gi=[],ws=new Map,ac=new Map,uc=new Map,MT=0,vs=null,BT=[],lc=f=>(function(g){if(!A){if(nr===0){var x=!1,v=!1;g((E=0)=>{if(!A&&(sc=E,x=!0,v)){nr=2,qi(()=>vp(zt)),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.resume(),E=!1;try{var k=(function(){var Y=(I(),w)[zt+8>>>2>>>0];return Y=ac.get(Y),Y=uc.get(Y),--Qt,Y()})()}catch(Y){k=Y,E=!0}var R=!1;if(!zt){var j=vs;j&&(vs=null,(E?j.reject:j.resolve)(k),R=!0)}if(E&&!R)throw k}}),v=!0,x||(nr=1,zt=(function(){var E=Mn(65548),k=E+12;if((I(),C)[E>>>2>>>0]=k,(I(),C)[E+4>>>2>>>0]=k+65536,k=Gi[0],!ws.has(k)){var R=MT++;ws.set(k,R),ac.set(R,k)}return k=ws.get(k),(I(),w)[E+8>>>2>>>0]=k,E})(),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.pause(),qi(()=>_p(zt)))}else nr===2?(nr=0,qi(xp),Mt(zt),zt=null,BT.forEach(Ui)):ne(`invalid state: ${nr}`);return sc}})(g=>{f().then(g)});function FT(f){return f>>>=0,lc(async()=>{var g=await bt(f);return St(g)})}var xs=[],LT=f=>{var g=xs.length;return xs.push(f),g},jT=(f,g)=>{for(var x=Array(f),v=0;v>>2>>>0],R=ms[k];if(R===void 0)throw f=`parameter ${v}`,k=Ic(k),g=Nt(k),Mt(k),new pn(`${f} has unknown type ${g}`);x[E]=R}return x},UT=(f,g,x)=>{var v=[];return f=f(v,x),v.length&&((I(),C)[g>>>2>>>0]=St(v)),f},VT={},Hi=f=>{var g=VT[f];return g===void 0?Nt(f):g};function qT(f,g,x){var[v,...E]=jT(f,g>>>0);g=v.Vc.bind(v);var k=E.map(Y=>Y.Uc.bind(Y));f--;var R={toValue:bt};switch(f=k.map((Y,ee)=>{var he=`argFromPtr${ee}`;return R[he]=Y,`${he}(args${ee?"+"+8*ee:""})`}),x){case 0:var j="toValue(handle)";break;case 2:j="new (toValue(handle))";break;case 3:j="";break;case 1:R.getStringOrSymbol=Hi,j="toValue(handle)[getStringOrSymbol(methodName)]"}return j+=`(${f})`,v.zd||(R.toReturnWire=g,R.emval_returnValue=UT,j=`return emval_returnValue(toReturnWire, destructorsRef, ${j})`),j=`return function (handle, methodName, destructorsRef, args) { - ${j} - }`,x=new Function(Object.keys(R),j)(...Object.values(R)),j=`methodCaller<(${E.map(Y=>Y.name)}) => ${v.name}>`,LT(Object.defineProperty(x,"name",{value:j}))}function GT(f,g){return g>>>=0,(f=bt(f>>>0))==bt(g)}function HT(f){return(f>>>=0)?(f=Hi(f),St(globalThis[f])):St(globalThis)}function WT(f){return f=Hi(f>>>0),St(t[f])}function KT(f,g){return g>>>=0,f=bt(f>>>0),g=bt(g),St(f[g])}function XT(f){9<(f>>>=0)&&(Tr[f+1]+=1)}function dc(f,g,x,v,E){return xs[f>>>0](g>>>0,x>>>0,v>>>0,E>>>0)}function ZT(f,g,x,v,E){return dc(f>>>0,g>>>0,x>>>0,v>>>0,E>>>0)}function YT(){return St([])}function JT(f){f=bt(f>>>0);for(var g=Array(f.length),x=0;x>>0))}function eS(){return St({})}function tS(f){for(var g=bt(f>>>=0);g.length;){var x=g.pop();g.pop()(x)}gs(f)}function rS(f,g,x){g>>>=0,x>>>=0,f=bt(f>>>0),g=bt(g),x=bt(x),f[g]=x}function nS(f,g){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(I(),w)[g>>>2>>>0]=f.getUTCSeconds(),(I(),w)[g+4>>>2>>>0]=f.getUTCMinutes(),(I(),w)[g+8>>>2>>>0]=f.getUTCHours(),(I(),w)[g+12>>>2>>>0]=f.getUTCDate(),(I(),w)[g+16>>>2>>>0]=f.getUTCMonth(),(I(),w)[g+20>>>2>>>0]=f.getUTCFullYear()-1900,(I(),w)[g+24>>>2>>>0]=f.getUTCDay(),f=(f.getTime()-Date.UTC(f.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(I(),w)[g+28>>>2>>>0]=f}var cc=f=>f%4==0&&(f%100!=0||f%400==0),pc=[0,31,60,91,121,152,182,213,244,274,305,335],hc=[0,31,59,90,120,151,181,212,243,273,304,334];function iS(f,g){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(I(),w)[g>>>2>>>0]=f.getSeconds(),(I(),w)[g+4>>>2>>>0]=f.getMinutes(),(I(),w)[g+8>>>2>>>0]=f.getHours(),(I(),w)[g+12>>>2>>>0]=f.getDate(),(I(),w)[g+16>>>2>>>0]=f.getMonth(),(I(),w)[g+20>>>2>>>0]=f.getFullYear()-1900,(I(),w)[g+24>>>2>>>0]=f.getDay();var x=(cc(f.getFullYear())?pc:hc)[f.getMonth()]+f.getDate()-1|0;(I(),w)[g+28>>>2>>>0]=x,(I(),w)[g+36>>>2>>>0]=-60*f.getTimezoneOffset(),x=new Date(f.getFullYear(),6,1).getTimezoneOffset();var v=new Date(f.getFullYear(),0,1).getTimezoneOffset();f=0|(x!=v&&f.getTimezoneOffset()==Math.min(v,x)),(I(),w)[g+32>>>2>>>0]=f}function oS(f){f>>>=0;var g=new Date((I(),w)[f+20>>>2>>>0]+1900,(I(),w)[f+16>>>2>>>0],(I(),w)[f+12>>>2>>>0],(I(),w)[f+8>>>2>>>0],(I(),w)[f+4>>>2>>>0],(I(),w)[f>>>2>>>0],0),x=(I(),w)[f+32>>>2>>>0],v=g.getTimezoneOffset(),E=new Date(g.getFullYear(),6,1).getTimezoneOffset(),k=new Date(g.getFullYear(),0,1).getTimezoneOffset(),R=Math.min(k,E);return 0>x?(I(),w)[f+32>>>2>>>0]=+(E!=k&&R==v):0>>2>>>0]=g.getDay(),x=(cc(g.getFullYear())?pc:hc)[g.getMonth()]+g.getDate()-1|0,(I(),w)[f+28>>>2>>>0]=x,(I(),w)[f>>>2>>>0]=g.getSeconds(),(I(),w)[f+4>>>2>>>0]=g.getMinutes(),(I(),w)[f+8>>>2>>>0]=g.getHours(),(I(),w)[f+12>>>2>>>0]=g.getDate(),(I(),w)[f+16>>>2>>>0]=g.getMonth(),(I(),w)[f+20>>>2>>>0]=g.getYear(),f=g.getTime(),BigInt(isNaN(f)?-1:f/1e3)}function fc(f,g,x,v,E,k,R){return u?Le(16,1,f,g,x,v,E,k,R):-52}function mc(f,g,x,v,E,k){if(u)return Le(17,1,f,g,x,v,E,k)}var zn={},sS=()=>performance.timeOrigin+performance.now();function gc(f,g){if(u)return Le(18,1,f,g);if(zn[f]&&(clearTimeout(zn[f].id),delete zn[f]),!g)return 0;var x=setTimeout(()=>{delete zn[f],Ui(()=>Pc(f,performance.timeOrigin+performance.now()))},g);return zn[f]={id:x,ae:g},0}function aS(f,g,x,v){f>>>=0,g>>>=0,x>>>=0,v>>>=0;var E=new Date().getFullYear(),k=new Date(E,0,1).getTimezoneOffset();E=new Date(E,6,1).getTimezoneOffset();var R=Math.max(k,E);(I(),C)[f>>>2>>>0]=60*R,(I(),w)[g>>>2>>>0]=+(k!=E),f=(g=j=>{var Y=Math.abs(j);return`UTC${0<=j?"-":"+"}${String(Math.floor(Y/60)).padStart(2,"0")}${String(Y%60).padStart(2,"0")}`})(k),g=g(E),EDate.now(),lS=1;function dS(f,g,x){if(x>>>=0,!(0<=f&&3>=f))return 28;if(f===0)f=Date.now();else{if(!lS)return 52;f=performance.timeOrigin+performance.now()}return f=Math.round(1e6*f),(I(),G)[x>>>3>>>0]=BigInt(f),0}var $s=[],bc=(f,g)=>{$s.length=0;for(var x;x=(I(),q)[f++>>>0];){var v=x!=105;g+=(v&=x!=112)&&g%8?4:0,$s.push(x==112?(I(),C)[g>>>2>>>0]:x==106?(I(),G)[g>>>3>>>0]:x==105?(I(),w)[g>>>2>>>0]:(I(),H)[g>>>3>>>0]),g+=v?8:4}return $s};function cS(f,g,x){return f>>>=0,g=bc(g>>>0,x>>>0),Ps[f](...g)}function pS(f,g,x){return f>>>=0,g=bc(g>>>0,x>>>0),Ps[f](...g)}var hS=()=>{};function fS(f,g){return O(Xe(f>>>0,g>>>0))}var mS=()=>{throw Qt+=1,"unwind"};function gS(){return 4294901760}var bS=()=>navigator.hardwareConcurrency,Sr={},Wi=f=>{var g;return(g=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(f))?+g[1]:(g=/:(\d+):\d+(?:\)|$)/.exec(f))?2147483648|+g[1]:0},yc=f=>{for(var g of f)(f=Wi(g))&&(Sr[f]=g)};function yS(){var f=Error().stack.toString().split(` -`);return f[0]=="Error"&&f.shift(),yc(f),Sr.gd=Wi(f[3]),Sr.Jd=f,Sr.gd}function Ki(f){if(!(f=Sr[f>>>0]))return 0;var g;if(g=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(f))f=g[1];else if(g=/^\s+at (.*) \(.*\)$/.exec(f))f=g[1];else{if(!(g=/^(.+?)@/.exec(f)))return 0;f=g[1]}Mt(Ki.hd??0),g=ji(f)+1;var x=Mn(g);return x&&rr(f,x,g),Ki.hd=x,Ki.hd}function _S(f){f>>>=0;var g=(I(),q).length;if(f<=g||4294901760=x;x*=2){var v=g*(1+.2/x);v=Math.min(v,f+100663296);e:{v=(Math.min(4294901760,65536*Math.ceil(Math.max(f,v)/65536))-tr.buffer.byteLength+65535)/65536|0;try{tr.grow(v),K();var E=1;break e}catch{}E=void 0}if(E)return!0}return!1}function wS(f,g,x){if(f>>>=0,g>>>=0,Sr.gd==f)var v=Sr.Jd;else(v=Error().stack.toString().split(` -`))[0]=="Error"&&v.shift(),yc(v);for(var E=3;v[E]&&Wi(v[E])!=f;)++E;for(f=0;f>>2>>>0]=Wi(v[f+E]);return f}var Ts,Ss={},_c=()=>{if(!Ts){var f,g={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(globalThis.navigator?.language??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(f in Ss)Ss[f]===void 0?delete g[f]:g[f]=Ss[f];var x=[];for(f in g)x.push(`${f}=${g[f]}`);Ts=x}return Ts};function wc(f,g){if(u)return Le(19,1,f,g);f>>>=0,g>>>=0;var x,v=0,E=0;for(x of _c()){var k=g+v;(I(),C)[f+E>>>2>>>0]=k,v+=rr(x,k,1/0)+1,E+=4}return 0}function vc(f,g){if(u)return Le(20,1,f,g);f>>>=0,g>>>=0;var x=_c();for(var v of((I(),C)[f>>>2>>>0]=x.length,f=0,x))f+=ji(v)+1;return(I(),C)[g>>>2>>>0]=f,0}function xc(f){return u?Le(21,1,f):52}function $c(f,g,x,v){return u?Le(22,1,f,g,x,v):52}function Tc(f,g,x,v){return u?Le(23,1,f,g,x,v):70}var vS=[null,[],[]];function Sc(f,g,x,v){if(u)return Le(24,1,f,g,x,v);g>>>=0,x>>>=0,v>>>=0;for(var E=0,k=0;k>>2>>>0],j=(I(),C)[g+4>>>2>>>0];g+=8;for(var Y=0;Y>>0],xe=vS[ee];he===0||he===10?((ee===1?S:O)(Vd(xe)),xe.length=0):xe.push(he)}E+=j}return(I(),C)[v>>>2>>>0]=E,0}function xS(f){return f>>>0}u||(function(){for(var f=t.numThreads-1;f--;)zd();je.push(async()=>{var g=(async function(){if(!u)return Promise.all(er.map(Nd))})();at++,await g,--at==0&&xr&&(g=xr,xr=null,g())})})(),u||(tr=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),K()),t.wasmBinary&&(p=t.wasmBinary),t.stackSave=()=>be(),t.stackRestore=f=>ge(f),t.stackAlloc=f=>Es(f),t.setValue=function(f,g,x="i8"){switch(x.endsWith("*")&&(x="*"),x){case"i1":case"i8":(I(),Z)[f>>>0]=g;break;case"i16":(I(),N)[f>>>1>>>0]=g;break;case"i32":(I(),w)[f>>>2>>>0]=g;break;case"i64":(I(),G)[f>>>3>>>0]=BigInt(g);break;case"float":(I(),U)[f>>>2>>>0]=g;break;case"double":(I(),H)[f>>>3>>>0]=g;break;case"*":(I(),C)[f>>>2>>>0]=g;break;default:ne(`invalid type for setValue: ${x}`)}},t.getValue=function(f,g="i8"){switch(g.endsWith("*")&&(g="*"),g){case"i1":case"i8":return(I(),Z)[f>>>0];case"i16":return(I(),N)[f>>>1>>>0];case"i32":return(I(),w)[f>>>2>>>0];case"i64":return(I(),G)[f>>>3>>>0];case"float":return(I(),U)[f>>>2>>>0];case"double":return(I(),H)[f>>>3>>>0];case"*":return(I(),C)[f>>>2>>>0];default:ne(`invalid type for getValue: ${g}`)}},t.UTF8ToString=Xe,t.stringToUTF8=rr,t.lengthBytesUTF8=ji;var Ic,Oc,Xi,Mt,Mn,Is,Ec,Ac,kc,Os,Pc,Cc,_e,Bn,Dc,ge,Es,be,Rc,As,Nc,zc,Mc,ks,Bc,Fc,Lc,jc,Uc,Vc,qc,Gc,Hc,Wc,Kc,Xc,Zc,Yc,Jc,Qc,ep,tp,rp,np,ip,op,sp,ap,up,lp,dp,cp,pp,hp,fp,mp,gp,bp,yp,_p,wp,vp,xp,Gt,$S=[ds,Pd,Fd,qd,Gd,Hd,Wd,Kd,Xd,Zd,Yd,Jd,Qd,ec,tc,rc,fc,mc,gc,wc,vc,xc,$c,Tc,Sc],Ps={1055492:(f,g,x,v,E)=>{if(t===void 0||!t.Yc)return 1;if((f=Xe(Number(f>>>0))).startsWith("./")&&(f=f.substring(2)),!(f=t.Yc.get(f)))return 2;if(g=Number(g>>>0),x=Number(x>>>0),v=Number(v>>>0),g+x>f.byteLength)return 3;try{let k=f.subarray(g,g+x);switch(E){case 0:(I(),q).set(k,v>>>0);break;case 1:t.Qd?t.Qd(v,k):t.Id(v,k);break;default:return 4}return 0}catch{return 4}},1056316:(f,g,x)=>{t.td(f,(I(),q).subarray(g>>>0,g+x>>>0))},1056380:()=>t.Sd(),1056422:f=>{t.sd(f)},1056459:()=>{t.Bd()},1056490:()=>{t.Cd()},1056519:()=>{t.Gd()},1056544:f=>t.Ad(f),1056577:f=>t.Ed(f),1056609:(f,g,x)=>{t.ed(Number(f),Number(g),Number(x),!0)},1056672:(f,g,x)=>{t.ed(Number(f),Number(g),Number(x))},1056729:()=>typeof wasmOffsetConverter<"u",1056786:f=>{t.$b("Abs",f,void 0)},1056837:f=>{t.$b("Neg",f,void 0)},1056888:f=>{t.$b("Floor",f,void 0)},1056941:f=>{t.$b("Ceil",f,void 0)},1056993:f=>{t.$b("Reciprocal",f,void 0)},1057051:f=>{t.$b("Sqrt",f,void 0)},1057103:f=>{t.$b("Exp",f,void 0)},1057154:f=>{t.$b("Erf",f,void 0)},1057205:f=>{t.$b("Sigmoid",f,void 0)},1057260:(f,g,x)=>{t.$b("HardSigmoid",f,{alpha:g,beta:x})},1057339:f=>{t.$b("HardSwish",f,void 0)},1057396:f=>{t.$b("Log",f,void 0)},1057447:f=>{t.$b("Sin",f,void 0)},1057498:f=>{t.$b("Cos",f,void 0)},1057549:f=>{t.$b("Tan",f,void 0)},1057600:f=>{t.$b("Asin",f,void 0)},1057652:f=>{t.$b("Acos",f,void 0)},1057704:f=>{t.$b("Atan",f,void 0)},1057756:f=>{t.$b("Sinh",f,void 0)},1057808:f=>{t.$b("Cosh",f,void 0)},1057860:f=>{t.$b("Asinh",f,void 0)},1057913:f=>{t.$b("Acosh",f,void 0)},1057966:f=>{t.$b("Atanh",f,void 0)},1058019:f=>{t.$b("Tanh",f,void 0)},1058071:f=>{t.$b("Not",f,void 0)},1058122:(f,g,x)=>{t.$b("Clip",f,{min:g,max:x})},1058191:f=>{t.$b("Clip",f,void 0)},1058243:(f,g)=>{t.$b("Elu",f,{alpha:g})},1058301:f=>{t.$b("Gelu",f,void 0)},1058353:f=>{t.$b("Relu",f,void 0)},1058405:(f,g)=>{t.$b("LeakyRelu",f,{alpha:g})},1058469:(f,g)=>{t.$b("ThresholdedRelu",f,{alpha:g})},1058539:(f,g)=>{t.$b("Cast",f,{to:g})},1058597:f=>{t.$b("Add",f,void 0)},1058648:f=>{t.$b("Sub",f,void 0)},1058699:f=>{t.$b("Mul",f,void 0)},1058750:f=>{t.$b("Div",f,void 0)},1058801:f=>{t.$b("Pow",f,void 0)},1058852:f=>{t.$b("Equal",f,void 0)},1058905:f=>{t.$b("Greater",f,void 0)},1058960:f=>{t.$b("GreaterOrEqual",f,void 0)},1059022:f=>{t.$b("Less",f,void 0)},1059074:f=>{t.$b("LessOrEqual",f,void 0)},1059133:(f,g,x,v,E)=>{t.$b("ReduceMean",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059308:(f,g,x,v,E)=>{t.$b("ReduceMax",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059482:(f,g,x,v,E)=>{t.$b("ReduceMin",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059656:(f,g,x,v,E)=>{t.$b("ReduceProd",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059831:(f,g,x,v,E)=>{t.$b("ReduceSum",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060005:(f,g,x,v,E)=>{t.$b("ReduceL1",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060178:(f,g,x,v,E)=>{t.$b("ReduceL2",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060351:(f,g,x,v,E)=>{t.$b("ReduceLogSum",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060528:(f,g,x,v,E)=>{t.$b("ReduceSumSquare",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060708:(f,g,x,v,E)=>{t.$b("ReduceLogSumExp",f,{keepDims:!!g,noopWithEmptyAxes:!!x,axes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060888:f=>{t.$b("Where",f,void 0)},1060941:(f,g,x)=>{t.$b("Transpose",f,{perm:g?Array.from((I(),w).subarray(Number(g)>>>0,Number(x)>>>0)):[]})},1061065:(f,g,x,v)=>{t.$b("DepthToSpace",f,{blocksize:g,mode:Xe(x),format:v?"NHWC":"NCHW"})},1061198:(f,g,x,v)=>{t.$b("DepthToSpace",f,{blocksize:g,mode:Xe(x),format:v?"NHWC":"NCHW"})},1061331:(f,g,x,v)=>{t.$b("DFT",f,{axis:g,inverse:x,onesided:v})},1061423:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze,ir)=>{t.$b("ConvTranspose",f,{format:Y?"NHWC":"NCHW",autoPad:g,dilations:[x],group:v,kernelShape:[E],pads:[k,R],strides:[j],wIsConst:()=>!!(I(),Z)[ee>>>0],outputPadding:he?Array.from((I(),w).subarray(Number(he)>>>0,Number(xe)>>>0)):[],outputShape:De?Array.from((I(),w).subarray(Number(De)>>>0,Number(ze)>>>0)):[],activation:Xe(ir)})},1061856:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze)=>{t.$b("ConvTranspose",f,{format:j?"NHWC":"NCHW",autoPad:g,dilations:Array.from((I(),w).subarray(Number(x)>>>0,(Number(x)>>>0)+2>>>0)),group:v,kernelShape:Array.from((I(),w).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((I(),w).subarray(Number(k)>>>0,(Number(k)>>>0)+4>>>0)),strides:Array.from((I(),w).subarray(Number(R)>>>0,(Number(R)>>>0)+2>>>0)),wIsConst:()=>!!(I(),Z)[Y>>>0],outputPadding:ee?Array.from((I(),w).subarray(Number(ee)>>>0,Number(he)>>>0)):[],outputShape:xe?Array.from((I(),w).subarray(Number(xe)>>>0,Number(De)>>>0)):[],activation:Xe(ze)})},1062517:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze,ir)=>{t.$b("ConvTranspose",f,{format:Y?"NHWC":"NCHW",autoPad:g,dilations:[x],group:v,kernelShape:[E],pads:[k,R],strides:[j],wIsConst:()=>!!(I(),Z)[ee>>>0],outputPadding:he?Array.from((I(),w).subarray(Number(he)>>>0,Number(xe)>>>0)):[],outputShape:De?Array.from((I(),w).subarray(Number(De)>>>0,Number(ze)>>>0)):[],activation:Xe(ir)})},1062950:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze)=>{t.$b("ConvTranspose",f,{format:j?"NHWC":"NCHW",autoPad:g,dilations:Array.from((I(),w).subarray(Number(x)>>>0,(Number(x)>>>0)+2>>>0)),group:v,kernelShape:Array.from((I(),w).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((I(),w).subarray(Number(k)>>>0,(Number(k)>>>0)+4>>>0)),strides:Array.from((I(),w).subarray(Number(R)>>>0,(Number(R)>>>0)+2>>>0)),wIsConst:()=>!!(I(),Z)[Y>>>0],outputPadding:ee?Array.from((I(),w).subarray(Number(ee)>>>0,Number(he)>>>0)):[],outputShape:xe?Array.from((I(),w).subarray(Number(xe)>>>0,Number(De)>>>0)):[],activation:Xe(ze)})},1063611:(f,g)=>{t.$b("GlobalAveragePool",f,{format:g?"NHWC":"NCHW"})},1063702:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze)=>{t.$b("AveragePool",f,{format:ze?"NHWC":"NCHW",auto_pad:g,ceil_mode:x,count_include_pad:v,storage_order:E,dilations:k?Array.from((I(),w).subarray(Number(k)>>>0,Number(R)>>>0)):[],kernel_shape:j?Array.from((I(),w).subarray(Number(j)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((I(),w).subarray(Number(ee)>>>0,Number(he)>>>0)):[],strides:xe?Array.from((I(),w).subarray(Number(xe)>>>0,Number(De)>>>0)):[]})},1064181:(f,g)=>{t.$b("GlobalAveragePool",f,{format:g?"NHWC":"NCHW"})},1064272:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze)=>{t.$b("AveragePool",f,{format:ze?"NHWC":"NCHW",auto_pad:g,ceil_mode:x,count_include_pad:v,storage_order:E,dilations:k?Array.from((I(),w).subarray(Number(k)>>>0,Number(R)>>>0)):[],kernel_shape:j?Array.from((I(),w).subarray(Number(j)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((I(),w).subarray(Number(ee)>>>0,Number(he)>>>0)):[],strides:xe?Array.from((I(),w).subarray(Number(xe)>>>0,Number(De)>>>0)):[]})},1064751:(f,g)=>{t.$b("GlobalMaxPool",f,{format:g?"NHWC":"NCHW"})},1064838:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze)=>{t.$b("MaxPool",f,{format:ze?"NHWC":"NCHW",auto_pad:g,ceil_mode:x,count_include_pad:v,storage_order:E,dilations:k?Array.from((I(),w).subarray(Number(k)>>>0,Number(R)>>>0)):[],kernel_shape:j?Array.from((I(),w).subarray(Number(j)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((I(),w).subarray(Number(ee)>>>0,Number(he)>>>0)):[],strides:xe?Array.from((I(),w).subarray(Number(xe)>>>0,Number(De)>>>0)):[]})},1065313:(f,g)=>{t.$b("GlobalMaxPool",f,{format:g?"NHWC":"NCHW"})},1065400:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze)=>{t.$b("MaxPool",f,{format:ze?"NHWC":"NCHW",auto_pad:g,ceil_mode:x,count_include_pad:v,storage_order:E,dilations:k?Array.from((I(),w).subarray(Number(k)>>>0,Number(R)>>>0)):[],kernel_shape:j?Array.from((I(),w).subarray(Number(j)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((I(),w).subarray(Number(ee)>>>0,Number(he)>>>0)):[],strides:xe?Array.from((I(),w).subarray(Number(xe)>>>0,Number(De)>>>0)):[]})},1065875:(f,g,x,v,E)=>{t.$b("Gemm",f,{alpha:g,beta:x,transA:v,transB:E})},1065979:f=>{t.$b("MatMul",f,void 0)},1066033:(f,g,x,v)=>{t.$b("ArgMax",f,{keepDims:!!g,selectLastIndex:!!x,axis:v})},1066141:(f,g,x,v)=>{t.$b("ArgMin",f,{keepDims:!!g,selectLastIndex:!!x,axis:v})},1066249:(f,g)=>{t.$b("Softmax",f,{axis:g})},1066312:(f,g)=>{t.$b("Concat",f,{axis:g})},1066372:(f,g,x,v,E)=>{t.$b("Split",f,{axis:g,numOutputs:x,splitSizes:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1066528:f=>{t.$b("Expand",f,void 0)},1066582:(f,g)=>{t.$b("Gather",f,{axis:Number(g)})},1066653:(f,g)=>{t.$b("GatherElements",f,{axis:Number(g)})},1066732:(f,g)=>{t.$b("GatherND",f,{batch_dims:Number(g)})},1066811:(f,g,x,v,E,k,R,j,Y,ee,he)=>{t.$b("Resize",f,{antialias:g,axes:x?Array.from((I(),w).subarray(Number(x)>>>0,Number(v)>>>0)):[],coordinateTransformMode:Xe(E),cubicCoeffA:k,excludeOutside:R,extrapolationValue:j,keepAspectRatioPolicy:Xe(Y),mode:Xe(ee),nearestMode:Xe(he)})},1067173:(f,g,x,v,E,k,R)=>{t.$b("Slice",f,{starts:g?Array.from((I(),w).subarray(Number(g)>>>0,Number(x)>>>0)):[],ends:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[],axes:k?Array.from((I(),w).subarray(Number(k)>>>0,Number(R)>>>0)):[]})},1067437:f=>{t.$b("Tile",f,void 0)},1067489:(f,g,x)=>{t.$b("InstanceNormalization",f,{epsilon:g,format:x?"NHWC":"NCHW"})},1067603:(f,g,x)=>{t.$b("InstanceNormalization",f,{epsilon:g,format:x?"NHWC":"NCHW"})},1067717:f=>{t.$b("Range",f,void 0)},1067770:(f,g)=>{t.$b("Einsum",f,{equation:Xe(g)})},1067851:(f,g,x,v,E)=>{t.$b("Pad",f,{mode:g,value:x,pads:v?Array.from((I(),w).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1067994:(f,g,x,v,E,k)=>{t.$b("BatchNormalization",f,{epsilon:g,momentum:x,spatial:!!E,trainingMode:!!v,format:k?"NHWC":"NCHW"})},1068163:(f,g,x,v,E,k)=>{t.$b("BatchNormalization",f,{epsilon:g,momentum:x,spatial:!!E,trainingMode:!!v,format:k?"NHWC":"NCHW"})},1068332:(f,g,x)=>{t.$b("CumSum",f,{exclusive:Number(g),reverse:Number(x)})},1068429:(f,g,x)=>{t.$b("DequantizeLinear",f,{axis:g,blockSize:x})},1068519:(f,g,x,v,E)=>{t.$b("GridSample",f,{align_corners:g,mode:Xe(x),padding_mode:Xe(v),format:E?"NHWC":"NCHW"})},1068689:(f,g,x,v,E)=>{t.$b("GridSample",f,{align_corners:g,mode:Xe(x),padding_mode:Xe(v),format:E?"NHWC":"NCHW"})},1068859:(f,g)=>{t.$b("ScatterND",f,{reduction:Xe(g)})},1068944:(f,g,x,v,E,k,R,j,Y)=>{t.$b("Attention",f,{numHeads:g,isUnidirectional:x,maskFilterValue:v,scale:E,doRotary:k,qkvHiddenSizes:R?Array.from((I(),w).subarray(Number(j)>>>0,Number(j)+R>>>0)):[],pastPresentShareBuffer:!!Y})},1069216:f=>{t.$b("BiasAdd",f,void 0)},1069271:f=>{t.$b("BiasSplitGelu",f,void 0)},1069332:f=>{t.$b("FastGelu",f,void 0)},1069388:(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze,ir,Cs)=>{t.$b("Conv",f,{format:xe?"NHWC":"NCHW",auto_pad:g,dilations:x?Array.from((I(),w).subarray(Number(x)>>>0,Number(v)>>>0)):[],group:E,kernel_shape:k?Array.from((I(),w).subarray(Number(k)>>>0,Number(R)>>>0)):[],pads:j?Array.from((I(),w).subarray(Number(j)>>>0,Number(Y)>>>0)):[],strides:ee?Array.from((I(),w).subarray(Number(ee)>>>0,Number(he)>>>0)):[],w_is_const:()=>!!(I(),Z)[Number(De)>>>0],activation:Xe(ze),activation_params:ir?Array.from((I(),U).subarray(Number(ir)>>>0,Number(Cs)>>>0)):[]})},1069972:f=>{t.$b("Gelu",f,void 0)},1070024:(f,g,x,v,E,k,R,j,Y)=>{t.$b("GroupQueryAttention",f,{numHeads:g,kvNumHeads:x,scale:v,softcap:E,doRotary:k,rotaryInterleaved:R,smoothSoftmax:j,localWindowSize:Y})},1070241:(f,g,x,v)=>{t.$b("LayerNormalization",f,{axis:g,epsilon:x,simplified:!!v})},1070352:(f,g,x,v)=>{t.$b("LayerNormalization",f,{axis:g,epsilon:x,simplified:!!v})},1070463:(f,g,x,v,E,k)=>{t.$b("MatMulNBits",f,{k:g,n:x,accuracyLevel:v,bits:E,blockSize:k})},1070590:(f,g,x,v,E,k)=>{t.$b("MultiHeadAttention",f,{numHeads:g,isUnidirectional:x,maskFilterValue:v,scale:E,doRotary:k})},1070749:(f,g)=>{t.$b("QuickGelu",f,{alpha:g})},1070813:(f,g,x,v,E)=>{t.$b("RotaryEmbedding",f,{interleaved:!!g,numHeads:x,rotaryEmbeddingDim:v,scale:E})},1070952:(f,g,x)=>{t.$b("SkipLayerNormalization",f,{epsilon:g,simplified:!!x})},1071054:(f,g,x)=>{t.$b("SkipLayerNormalization",f,{epsilon:g,simplified:!!x})},1071156:(f,g,x,v)=>{t.$b("GatherBlockQuantized",f,{gatherAxis:g,quantizeAxis:x,blockSize:v})},1071277:f=>{t.Fd(f)},1071311:(f,g)=>t.Hd(Number(f),Number(g),t.Xc.Kd,t.Xc.errors)};function TS(f,g,x){return lc(async()=>{await t.Dd(Number(f),Number(g),Number(x))})}function SS(){return typeof wasmOffsetConverter<"u"}function IS(f,g,x,v){var E=be();try{return Gc(f,g,x,v)}catch(k){if(ge(E),k!==k+0)throw k;_e(1,0)}}function OS(f,g,x){var v=be();try{return jc(f,g,x)}catch(E){if(ge(v),E!==E+0)throw E;_e(1,0)}}function ES(f){var g=be();try{Bc(f)}catch(x){if(ge(g),x!==x+0)throw x;_e(1,0)}}function AS(f,g){var x=be();try{return ks(f,g)}catch(v){if(ge(x),v!==v+0)throw v;_e(1,0)}}function kS(f,g,x){var v=be();try{Mc(f,g,x)}catch(E){if(ge(v),E!==E+0)throw E;_e(1,0)}}function PS(f,g){var x=be();try{Hc(f,g)}catch(v){if(ge(x),v!==v+0)throw v;_e(1,0)}}function CS(f,g,x,v,E,k,R){var j=be();try{return Vc(f,g,x,v,E,k,R)}catch(Y){if(ge(j),Y!==Y+0)throw Y;_e(1,0)}}function DS(f,g,x,v,E,k){var R=be();try{Fc(f,g,x,v,E,k)}catch(j){if(ge(R),j!==j+0)throw j;_e(1,0)}}function RS(f,g,x,v){var E=be();try{qc(f,g,x,v)}catch(k){if(ge(E),k!==k+0)throw k;_e(1,0)}}function NS(f,g,x,v,E){var k=be();try{Lc(f,g,x,v,E)}catch(R){if(ge(k),R!==R+0)throw R;_e(1,0)}}function zS(f,g,x,v,E,k,R){var j=be();try{Kc(f,g,x,v,E,k,R)}catch(Y){if(ge(j),Y!==Y+0)throw Y;_e(1,0)}}function MS(f,g,x,v,E,k,R){var j=be();try{Xc(f,g,x,v,E,k,R)}catch(Y){if(ge(j),Y!==Y+0)throw Y;_e(1,0)}}function BS(f,g,x,v,E,k,R,j){var Y=be();try{Qc(f,g,x,v,E,k,R,j)}catch(ee){if(ge(Y),ee!==ee+0)throw ee;_e(1,0)}}function FS(f,g,x,v,E){var k=be();try{return Wc(f,g,x,v,E)}catch(R){if(ge(k),R!==R+0)throw R;_e(1,0)}}function LS(f,g,x){var v=be();try{return ep(f,g,x)}catch(E){if(ge(v),E!==E+0)throw E;_e(1,0)}}function jS(f,g,x,v,E,k,R,j){var Y=be();try{tp(f,g,x,v,E,k,R,j)}catch(ee){if(ge(Y),ee!==ee+0)throw ee;_e(1,0)}}function US(f,g,x,v,E,k,R,j,Y,ee,he,xe){var De=be();try{Zc(f,g,x,v,E,k,R,j,Y,ee,he,xe)}catch(ze){if(ge(De),ze!==ze+0)throw ze;_e(1,0)}}function VS(f,g,x){var v=be();try{return rp(f,g,x)}catch(E){if(ge(v),E!==E+0)throw E;return _e(1,0),0n}}function qS(f,g,x,v,E,k,R,j,Y){var ee=be();try{Uc(f,g,x,v,E,k,R,j,Y)}catch(he){if(ge(ee),he!==he+0)throw he;_e(1,0)}}function GS(f){var g=be();try{return np(f)}catch(x){if(ge(g),x!==x+0)throw x;_e(1,0)}}function HS(f,g){var x=be();try{return yp(f,g)}catch(v){if(ge(x),v!==v+0)throw v;return _e(1,0),0n}}function WS(f){var g=be();try{return ip(f)}catch(x){if(ge(g),x!==x+0)throw x;return _e(1,0),0n}}function KS(f,g,x,v){var E=be();try{return dp(f,g,x,v)}catch(k){if(ge(E),k!==k+0)throw k;_e(1,0)}}function XS(f,g,x,v,E){var k=be();try{return cp(f,g,x,v,E)}catch(R){if(ge(k),R!==R+0)throw R;_e(1,0)}}function ZS(f,g,x,v,E,k){var R=be();try{return pp(f,g,x,v,E,k)}catch(j){if(ge(R),j!==j+0)throw j;_e(1,0)}}function YS(f,g,x,v,E,k){var R=be();try{return Yc(f,g,x,v,E,k)}catch(j){if(ge(R),j!==j+0)throw j;_e(1,0)}}function JS(f,g,x,v,E,k){var R=be();try{return hp(f,g,x,v,E,k)}catch(j){if(ge(R),j!==j+0)throw j;_e(1,0)}}function QS(f,g,x,v,E,k,R,j){var Y=be();try{return Jc(f,g,x,v,E,k,R,j)}catch(ee){if(ge(Y),ee!==ee+0)throw ee;_e(1,0)}}function eI(f,g,x,v,E){var k=be();try{return fp(f,g,x,v,E)}catch(R){if(ge(k),R!==R+0)throw R;return _e(1,0),0n}}function tI(f,g,x,v){var E=be();try{return mp(f,g,x,v)}catch(k){if(ge(E),k!==k+0)throw k;_e(1,0)}}function rI(f,g,x,v){var E=be();try{return gp(f,g,x,v)}catch(k){if(ge(E),k!==k+0)throw k;_e(1,0)}}function nI(f,g,x,v,E,k,R,j,Y,ee,he,xe){var De=be();try{return bp(f,g,x,v,E,k,R,j,Y,ee,he,xe)}catch(ze){if(ge(De),ze!==ze+0)throw ze;_e(1,0)}}function iI(f,g,x,v,E,k,R,j,Y,ee,he){var xe=be();try{up(f,g,x,v,E,k,R,j,Y,ee,he)}catch(De){if(ge(xe),De!==De+0)throw De;_e(1,0)}}function oI(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze,ir,Cs){var lI=be();try{lp(f,g,x,v,E,k,R,j,Y,ee,he,xe,De,ze,ir,Cs)}catch(Ds){if(ge(lI),Ds!==Ds+0)throw Ds;_e(1,0)}}function sI(f,g,x){var v=be();try{return op(f,g,x)}catch(E){if(ge(v),E!==E+0)throw E;_e(1,0)}}function aI(f,g,x){var v=be();try{return sp(f,g,x)}catch(E){if(ge(v),E!==E+0)throw E;_e(1,0)}}function uI(f,g,x,v){var E=be();try{ap(f,g,x,v)}catch(k){if(ge(E),k!==k+0)throw k;_e(1,0)}}function Zi(){if(0{b=f,$=g})}var kv,Vf,jE=D(()=>{"use strict";kv=Uf,Vf=globalThis.self?.name?.startsWith("em-pthread"),Vf&&Uf()}),ma,rl,qf,ct,Pv,ro,Gf,Hf,ga,Wf,ba,Cv,ya,Dv,Al=D(()=>{"use strict";El(),ma=typeof location>"u"?void 0:location.origin,rl=import.meta.url>"file:"&&import.meta.url<"file;",qf=()=>{if(rl){let e=URL;return new URL(new e("ort.all.bundle.min.mjs",import.meta.url).href,ma).href}return import.meta.url},ct=qf(),Pv=()=>{if(ct&&!ct.startsWith("blob:"))return ct.substring(0,ct.lastIndexOf("/")+1)},ro=(e,t)=>{try{let n=t??ct;return(n?new URL(e,n):new URL(e)).origin===ma}catch{return!1}},Gf=(e,t)=>{let n=t??ct;try{return(n?new URL(e,n):new URL(e)).href}catch{return}},Hf=(e,t)=>`${t??"./"}${e}`,ga=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},Wf=async e=>(await import(e)).default,ba=(LE(),wn(Ov)).default,Cv=async()=>{if(!ct)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(ro(ct))return[void 0,ba()];let e=await ga(ct);return[e,ba(e)]},ya=(jE(),wn(Av)).default,Dv=async(e,t,n,o)=>{let u=ya&&!(e||t);if(u)if(ct)u=ro(ct)||o&&!n;else if(o&&!n)u=!0;else throw new Error("cannot determine the script source URL.");if(u)return[void 0,ya];{let l="ort-wasm-simd-threaded.jsep.mjs",d=e??Gf(l,t),c=n&&d&&!ro(d,t),s=c?await ga(d):d??Hf(l,t);return[c?s:void 0,await Wf(s)]}}}),_a,no,Hn,wa,Kf,Xf,Zf,kl,Ne,sn=D(()=>{"use strict";Al(),no=!1,Hn=!1,wa=!1,Kf=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Xf=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Zf=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},kl=async e=>{if(no)return Promise.resolve();if(Hn)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(wa)throw new Error("previous call to 'initializeWebAssembly()' failed.");Hn=!0;let t=e.initTimeout,n=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!Zf())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!Xf())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let o=Kf();n>1&&!o&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+n+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=n=1);let u=e.wasmPaths,l=typeof u=="string"?u:void 0,d=u?.mjs,c=d?.href??d,s=u?.wasm,r=s?.href??s,i=e.wasmBinary,[a,p]=await Dv(c,l,n>1,!!i||!!r),h=!1,m=[];if(t>0&&m.push(new Promise(b=>{setTimeout(()=>{h=!0,b()},t)})),m.push(new Promise((b,$)=>{let _={numThreads:n};if(i)_.wasmBinary=i,_.locateFile=y=>y;else if(r||l)_.locateFile=y=>r??l+y;else if(c&&c.indexOf("blob:")!==0)_.locateFile=y=>new URL(y,c).href;else if(a){let y=Pv();y&&(_.locateFile=T=>y+T)}p(_).then(y=>{Hn=!1,no=!0,_a=y,b(),a&&URL.revokeObjectURL(a)},y=>{Hn=!1,wa=!0,$(y)})})),await Promise.race(m),h)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},Ne=()=>{if(no&&_a)return _a;throw new Error("WebAssembly is not initialized yet.")}}),Ct,So,ke,Pl=D(()=>{"use strict";sn(),Ct=(e,t)=>{let n=Ne(),o=n.lengthBytesUTF8(e)+1,u=n._malloc(o);return n.stringToUTF8(e,u,o),t.push(u),u},So=(e,t,n,o)=>{if(typeof e=="object"&&e!==null){if(n.has(e))throw new Error("Circular reference in options");n.add(e)}Object.entries(e).forEach(([u,l])=>{let d=t?t+u:u;if(typeof l=="object")So(l,d+".",n,o);else if(typeof l=="string"||typeof l=="number")o(d,l.toString());else if(typeof l=="boolean")o(d,l?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof l}`)})},ke=e=>{let t=Ne(),n=t.stackSave();try{let o=t.PTR_SIZE,u=t.stackAlloc(2*o);t._OrtGetLastError(u,u+o);let l=Number(t.getValue(u,o===4?"i32":"i64")),d=t.getValue(u+o,"*"),c=d?t.UTF8ToString(d):"";throw new Error(`${e} ERROR_CODE: ${l}, ERROR_MESSAGE: ${c}`)}finally{t.stackRestore(n)}}}),Rv,UE=D(()=>{"use strict";sn(),Pl(),Rv=e=>{let t=Ne(),n=0,o=[],u=e||{};try{if(e?.logSeverityLevel===void 0)u.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)u.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(u.terminate=!1);let l=0;return e?.tag!==void 0&&(l=Ct(e.tag,o)),n=t._OrtCreateRunOptions(u.logSeverityLevel,u.logVerbosityLevel,!!u.terminate,l),n===0&&ke("Can't create run options."),e?.extra!==void 0&&So(e.extra,"",new WeakSet,(d,c)=>{let s=Ct(d,o),r=Ct(c,o);t._OrtAddRunConfigEntry(n,s,r)!==0&&ke(`Can't set a run config entry: ${d} - ${c}.`)}),[n,o]}catch(l){throw n!==0&&t._OrtReleaseRunOptions(n),o.forEach(d=>t._free(d)),l}}}),Yf,Jf,Qf,Ar,em,Nv,VE=D(()=>{"use strict";sn(),Pl(),Yf=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},Jf=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},Qf=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(n=>(typeof n=="string"?n:n.name)==="webgpu")&&(e.enableMemPattern=!1)},Ar=(e,t,n,o)=>{let u=Ct(t,o),l=Ct(n,o);Ne()._OrtAddSessionConfigEntry(e,u,l)!==0&&ke(`Can't set a session config entry: ${t} - ${n}.`)},em=async(e,t,n)=>{let o=t.executionProviders;for(let u of o){let l=typeof u=="string"?u:u.name,d=[];switch(l){case"webnn":if(l="WEBNN",Ar(e,"session.disable_quant_qdq","1",n),Ar(e,"session.disable_qdq_constant_folding","1",n),typeof u!="string"){let a=u?.deviceType;a&&Ar(e,"deviceType",a,n)}break;case"webgpu":if(l="JS",typeof u!="string"){let a=u;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);Ar(e,"preferredLayout",a.preferredLayout,n)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${l}`)}let c=Ct(l,n),s=d.length,r=0,i=0;if(s>0){r=Ne()._malloc(s*Ne().PTR_SIZE),n.push(r),i=Ne()._malloc(s*Ne().PTR_SIZE),n.push(i);for(let a=0;a{let t=Ne(),n=0,o=[],u=e||{};Qf(u);try{let l=Yf(u.graphOptimizationLevel??"all"),d=Jf(u.executionMode??"sequential"),c=typeof u.logId=="string"?Ct(u.logId,o):0,s=u.logSeverityLevel??2;if(!Number.isInteger(s)||s<0||s>4)throw new Error(`log severity level is not valid: ${s}`);let r=u.logVerbosityLevel??0;if(!Number.isInteger(r)||r<0||r>4)throw new Error(`log verbosity level is not valid: ${r}`);let i=typeof u.optimizedModelFilePath=="string"?Ct(u.optimizedModelFilePath,o):0;if(n=t._OrtCreateSessionOptions(l,!!u.enableCpuMemArena,!!u.enableMemPattern,d,!!u.enableProfiling,0,c,s,r,i),n===0&&ke("Can't create session options."),u.executionProviders&&await em(n,u,o),u.enableGraphCapture!==void 0){if(typeof u.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${u.enableGraphCapture}`);Ar(n,"enableGraphCapture",u.enableGraphCapture.toString(),o)}if(u.freeDimensionOverrides)for(let[a,p]of Object.entries(u.freeDimensionOverrides)){if(typeof a!="string")throw new Error(`free dimension override name must be a string: ${a}`);if(typeof p!="number"||!Number.isInteger(p)||p<0)throw new Error(`free dimension override value must be a non-negative integer: ${p}`);let h=Ct(a,o);t._OrtAddFreeDimensionOverride(n,h,p)!==0&&ke(`Can't set a free dimension override: ${a} - ${p}.`)}return u.extra!==void 0&&So(u.extra,"",new WeakSet,(a,p)=>{Ar(n,a,p,o)}),[n,o]}catch(l){throw n!==0&&t._OrtReleaseSessionOptions(n)!==0&&ke("Can't release session options."),o.forEach(d=>t._free(d)),l}}}),Kr,Xt,Xr,Lo,Io,Cl,Dl,nl,ae=D(()=>{"use strict";Kr=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},Xt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},Xr=(e,t)=>{let n=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],o=typeof t=="number"?t:t.reduce((u,l)=>u*l,1);return n>0?Math.ceil(o*n):void 0},Lo=e=>{switch(e){case"float16":return typeof Float16Array<"u"?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Io=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Cl=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Dl=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",nl=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),Rl,zv=D(()=>{"use strict";El(),Rl=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let n=t.headers.get("Content-Length"),o=n?parseInt(n,10):0;if(o<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let u=t.body.getReader(),l;try{l=new ArrayBuffer(o)}catch(c){if(c instanceof RangeError){let s=Math.ceil(o/65536);l=new WebAssembly.Memory({initial:s,maximum:s}).buffer}else throw c}let d=0;for(;;){let{done:c,value:s}=await u.read();if(c)break;let r=s.byteLength;new Uint8Array(l,d,r).set(s),d+=r}return new Uint8Array(l,0,o)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),tm,rm,nm,im,Nl,om,ve,Yt=D(()=>{"use strict";ae(),tm=["V","I","W","E","F"],rm=(e,t)=>{console.log(`[${tm[e]},${new Date().toISOString()}]${t}`)},Nl=(e,t)=>{nm=e,im=t},om=(e,t)=>{let n=Io(e),o=Io(nm);n>=o&&rm(n,typeof t=="function"?t():t)},ve=(...e)=>{im&&om(...e)}}),sm,xn,z,Oo,Mv,Bv,Fv,de=D(()=>{"use strict";sm=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},xn=class{static calcShape(e,t,n=!1){let o=e.length,u=t.length;if(o===0)return t;if(u===0)return e;let l=Math.max(e.length,t.length),d=new Array(l);if(n){if(o<2||u<2)return;let c=sm.calcMatMulShape([e[o-2],e[o-1]],[t[u-2],t[u-1]]);if(c===void 0)return;[d[l-2],d[l-1]]=c}for(let c=n?3:1;c<=l;c++){let s=o-c<0?1:e[o-c],r=u-c<0?1:t[u-c];if(s!==r&&s>1&&r>1)return;let i=Math.max(s,r);if(s&&r)d[l-c]=Math.max(s,r);else{if(i>1)return;d[l-c]=0}}return d}static isValidBroadcast(e,t){let n=e.length,o=t.length;if(n>o)return!1;for(let u=1;u<=n;u++)if(e[n-u]!==1&&e[n-u]!==t[o-u])return!1;return!0}},z=class yo{static size(t){return yo.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,n=4){let o=t.length;if(o===0)return[];let u=new Array(o),l=o-1;for(;l>=0;){if(t[l]%n===0){u[l]=t[l]/n;break}if(n%t[l]!==0)throw new Error("cannot convert shape");u[l]=1,n/=t[l],l--}for(l--;l>=0;l--)u[l]=t[l];return u}static sizeFromDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return yo.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return yo.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,o){let u=1;for(let l=n;l=0;--u)o[u]=o[u+1]*t[u+1];return o}static normalizeAxis(t,n){if(t<-n&&t>=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(o=>this.normalizeAxis(o,n??t.length))}static sortBasedOnPerm(t,n){return n?n.map(o=>t[o]):t.slice().reverse()}static padShape(t,n){let o=t.length;return t.map((u,l)=>u+n[l]+n[l+o])}static areEqual(t,n){return t.length!==n.length?!1:t.every((o,u)=>o===n[u])}},Oo=class gr{static adjustPoolAttributes(t,n,o,u,l,d){if(!t&&o.length!==n.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let c=0;c=o.length?o.push(n[c+2]):o[c]=n[c+2];for(let c=0;c=o[c]||d[c+o.length]>=o[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,o,u,l,d,c){if(c){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let s=0;s=o+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,o,u,l,d,c,s,r=0){let i=o*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,gr.computeOutputSize(t-i,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(o!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],gr.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return gr.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}},Mv=class{static getShapeOfGemmResult(e,t,n,o,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(o?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!xn.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},Bv=-34028234663852886e22,Fv=34028234663852886e22}),zl,Lv=D(()=>{"use strict";ae(),zl=(e,t)=>new(Lo(t))(e)}),va,am,xa,um,$a,lm,Ta,Sa,Ia,dm,jv,qE=D(()=>{"use strict";ae(),Yt(),va=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),am=(e,t)=>{if(t==="int32")return e;let n=va.get(t);if(!n)throw new Error(`WebNN backend does not support data type: ${t}`);let o=n/8;if(e.byteLength%o!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${o}.`);let u=e.byteLength/o,l=new(Lo(t))(e.buffer,e.byteOffset,u);switch(t){case"int64":case"uint64":{let d=new Int32Array(u);for(let c=0;c2147483647n||s<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");d[c]=Number(s)}return new Uint8Array(d.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&l.some(c=>c>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let d=Int32Array.from(l,Number);return new Uint8Array(d.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},xa=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let n=e.byteLength/4,o=new Int32Array(e.buffer,e.byteOffset,n);switch(t){case"int64":{let u=BigInt64Array.from(o,BigInt);return new Uint8Array(u.buffer)}case"uint64":{if(o.some(l=>l<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let u=BigUint64Array.from(o,BigInt);return new Uint8Array(u.buffer)}case"int8":{if(o.some(l=>l<-128||l>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let u=Int8Array.from(o,Number);return new Uint8Array(u.buffer)}case"uint8":{if(o.some(u=>u<0||u>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(o,Number)}case"uint32":{if(o.some(l=>l<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let u=Uint32Array.from(o,Number);return new Uint8Array(u.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},um=1,$a=()=>um++,lm=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),Ta=(e,t)=>{let n=va.get(e);if(!n)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((o,u)=>o*u)*n/8):0},Sa=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:n,tensor:o,dataType:u,shape:l,fallbackDataType:d}=e;this.sessionId=t,this.mlContext=n,this.mlTensor=o,this.dataType=u,this.tensorShape=l,this.fallbackDataType=d}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return Ta(this.dataType,this.tensorShape)}destroy(){ve("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),n=xa(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(n);return}else return new Uint8Array(n).buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,n){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===n.length&&this.tensorShape.every((o,u)=>o===n[u])}setIsDataConverted(e){this.isDataConverted=e}},Ia=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(e,t,n,o){let u=this.tensorManager.getMLContext(e),l=this.tensorManager.getMLOpSupportLimits(e),d;if(!l?.input.dataTypes.includes(t)){if(d=lm.get(t),!d||l?.input.dataTypes.includes(d))throw new Error(`WebNN backend does not support data type: ${t}`);ve("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${d}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(u,t,n))return this.wrapper.tensor;if(o){if(this.wrapper.byteLength!==Ta(t,n))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let c=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,n,c,!0,!0,d),o&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=am(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else ve("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){if(this.activeUpload){let t=this.wrapper?.isDataConverted?xa(this.activeUpload,this.wrapper?.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(t):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(t);return}else return t.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},dm=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=$a();return this.tensorTrackersById.set(e,new Ia(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,n,o,u){ve("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${n}, shape: ${o}, copyOld: ${u}}`);let l=this.tensorTrackersById.get(t);if(!l)throw new Error("Tensor not found.");return l.ensureTensor(e,n,o,u)}upload(e,t){let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");n.upload(t)}async download(e,t){ve("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t?.byteLength}}`);let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");return n.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}registerTensor(e,t,n,o){let u=this.getMLContext(e),l=$a(),d=new Sa({sessionId:e,context:u,tensor:t,dataType:n,shape:o});return this.tensorTrackersById.set(l,new Ia(this,d)),this.externalTensors.add(d),l}async getCachedTensor(e,t,n,o,u,l,d){let c=this.getMLContext(e);for(let[r,i]of this.freeTensors.entries())if(i.canReuseTensor(c,t,n)){ve("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}`);let a=this.freeTensors.splice(r,1)[0];return a.sessionId=e,a}ve("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}}`);let s=await c.createTensor({dataType:d??t,shape:n,dimensions:n,usage:o,writable:u,readable:l});return new Sa({sessionId:e,context:c,tensor:s,dataType:t,shape:n,fallbackDataType:d})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},jv=(...e)=>new dm(...e)}),Wn,cm,Uv,GE=D(()=>{"use strict";ae(),sn(),Lv(),qE(),Yt(),Wn=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),cm=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let n=Object.keys(e).sort(),o=Object.keys(t).sort();return n.length===o.length&&n.every((u,l)=>u===o[l]&&e[u]===t[u])},Uv=class{constructor(e){this.tensorManager=jv(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,Nl(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){ve("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){ve("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let n of t)ve("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${n}}`),this.tensorManager.releaseTensorId(n);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let n=this.mlContextCache.findIndex(o=>o.gpuDevice===e);if(n!==-1)return this.mlContextCache[n].mlContext;{let o=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:o}),o}}else if(e===void 0){let n=this.mlContextCache.findIndex(o=>o.options===void 0&&o.gpuDevice===void 0);if(n!==-1)return this.mlContextCache[n].mlContext;{let o=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:o}),o}}let t=this.mlContextCache.findIndex(n=>cm(n.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let n=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:n}),n}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let n=this.sessionIdsByMLContext.get(t);n||(n=new Set,this.sessionIdsByMLContext.set(t,n)),n.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let n=this.sessionIdsByMLContext.get(t);if(n.delete(e),n.size===0){this.sessionIdsByMLContext.delete(t);let o=this.mlContextCache.findIndex(u=>u.mlContext===t);o!==-1&&this.mlContextCache.splice(o,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){ve("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,n,o,u){let l=Wn.get(n);if(!l)throw new Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,l,o,u)}async createTemporaryTensor(e,t,n){ve("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${n}}`);let o=Wn.get(t);if(!o)throw new Error(`Unsupported ONNX data type: ${t}`);let u=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,u,o,n,!1);let l=this.temporarySessionTensorIds.get(e);return l?l.push(u):this.temporarySessionTensorIds.set(e,[u]),u}uploadTensor(e,t){if(!Ne().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");ve("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let n=await this.tensorManager.download(e);return zl(n,t)}}registerMLTensor(e,t,n,o){let u=Wn.get(n);if(!u)throw new Error(`Unsupported ONNX data type: ${n}`);let l=this.tensorManager.registerTensor(e,t,u,o);return ve("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${u}, dimensions: ${o}} -> {tensorId: ${l}}`),l}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let n=this.sessionGraphInputs.get(e);return n?n.includes(t):!1}isGraphOutput(e,t){let n=this.sessionGraphOutputs.get(e);return n?n.includes(t):!1}isGraphInputOutputTypeSupported(e,t,n=!0){let o=Wn.get(Kr(t)),u=this.mlOpSupportLimitsBySessionId.get(e);return typeof o>"u"?!1:n?!!u?.input.dataTypes.includes(o):!!u?.output.dataTypes.includes(o)}flush(){}}}),Ml=D(()=>{"use strict"}),Oa,io,oo,pm,hm,Ea,il,fm,Vv,HE=D(()=>{"use strict";Yt(),Ml(),Oa=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),io=[],oo=e=>Math.ceil(Number(e)/16)*16,pm=e=>{for(let t=0;thm++,il=async(e,t,n,o)=>{let u=oo(n),l=e.device.createBuffer({size:u,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let d=e.getCommandEncoder();e.endComputePass(),d.copyBufferToBuffer(t,0,l,0,u),e.flush(),await l.mapAsync(GPUMapMode.READ);let c=l.getMappedRange();if(o){let s=o();return s.set(new Uint8Array(c,0,n)),s}else return new Uint8Array(c.slice(0,n))}finally{l.destroy()}},fm=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of Oa)io.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let n=t.buffer,o=t.byteOffset,u=t.byteLength,l=oo(u),d=this.storageCache.get(e);if(!d)throw new Error("gpu data for uploading does not exist");if(Number(d.originalSize)!==u)throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${u}`);if(l===u&&o%4===0)this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,n,o,u);else{let c=new Uint8Array(l);c.set(t),this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,c,0,l)}ve("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let n=this.storageCache.get(e);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(t);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let u=oo(n.originalSize),l=this.backend.getCommandEncoder();this.backend.endComputePass(),l.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,u)}registerExternalBuffer(e,t,n){let o;if(n){if(o=n[0],e===n[1])return ve("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. - Please use the previous external buffer!`)}else o=Ea();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:e},originalSize:t}),ve("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${o}, registered.`),o}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),ve("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=pm(e),o,u=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,l=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(u||l){let c=(u?this.freeBuffers:this.freeUniformBuffers).get(n);c?c.length>0?o=c.pop():o=this.backend.device.createBuffer({size:n,usage:t}):o=this.backend.device.createBuffer({size:n,usage:t})}else o=this.backend.device.createBuffer({size:n,usage:t});let d={id:Ea(),type:0,buffer:o};return this.storageCache.set(d.id,{gpuData:d,originalSize:Number(e)}),ve("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${d.id}`),d}get(e){return this.storageCache.get(e)?.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,n=this.storageCache.get(t);if(!n){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return ve("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${n.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(n.gpuData.buffer),n.originalSize}async download(e,t){let n=this.storageCache.get(Number(e));if(!n)throw new Error("data does not exist");await il(this.backend,n.gpuData.buffer,n.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=Oa.get(e.size);if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(ve("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(n=>{n.gpuData.buffer.destroy()}),this.storageCache=new Map)}},Vv=(...e)=>new fm(...e)}),mm,Se,Ve=D(()=>{"use strict";mm=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},Se=e=>new mm(e)}),$n,so,Je,Ye,se,Ue,ol,_n,yr,ie,Kn,F,re,qv,Bl,gm,Gv,ce=D(()=>{"use strict";ae(),de(),$n=64,so=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},Je=(e,t=1)=>{let n=so(e,t);return typeof n=="string"?n:n[0]},Ye=(e,t=1)=>{let n=so(e,t);return typeof n=="string"?n:n[1]},se=(...e)=>{let t=[];return e.forEach(n=>{n.length!==0&&t.push({type:12,data:n},{type:12,data:z.computeStrides(n)})}),t},Ue=e=>e%4===0?4:e%2===0?2:1,ol=(e="f32",t,n="0")=>!t||t===1?`${e}(${n})`:`vec${t}<${e}>(${n})`,_n=(e,t,n)=>e==="f32"?n:t===1?`f32(${n})`:`vec${t}(${n})`,yr=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,ie=(e,t,n,o)=>e.startsWith("uniforms.")&&n>4?typeof t=="string"?o==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:o==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:n>1?`${e}[${t}]`:e,Kn=(e,t,n,o,u)=>{let l=typeof n=="number",d=l?n:n.length,c=[...new Array(d).keys()],s=d<2?"u32":d<=4?`vec${d}`:`array`,r=so(t,u),i=typeof r=="string"?r:r[1],a=typeof r=="string"?r:r[0],p={indices:s,value:i,storage:a,tensor:t},h=B=>typeof B=="string"?B:`${B}u`,m={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},b=l?"uniforms.":"",$=`${b}${e}_shape`,_=`${b}${e}_strides`,y="";for(let B=0;Bthis.isTimerResultAvailable(e)),this.getTimerResult(e)}async createAndWaitForFence(){let e=this.createFence(this.gl);return this.pollFence(e)}createFence(e){let t,n=e,i=n.fenceSync(n.SYNC_GPU_COMMANDS_COMPLETE,0);return e.flush(),i===null?t=()=>!0:t=()=>{let u=n.clientWaitSync(i,0,0);return u===n.ALREADY_SIGNALED||u===n.CONDITION_SATISFIED},{query:i,isFencePassed:t}}async pollFence(e){return new Promise(t=>{this.addItemToPoll(()=>e.isFencePassed(),()=>t())})}pollItems(){let e=AA(this.itemsToPoll.map(t=>t.isDoneFn));for(let t=0;t<=e;++t){let{resolveFn:n}=this.itemsToPoll[t];n()}this.itemsToPoll=this.itemsToPoll.slice(e+1)}async addItemToPoll(e,t){this.itemsToPoll.push({isDoneFn:e,resolveFn:t}),!(this.itemsToPoll.length>1)&&await fh(()=>(this.pollItems(),this.itemsToPoll.length===0))}}});function Qv(e){let t;if((!e||e==="webgl2")&&"webgl2"in sn?t=sn.webgl2:(!e||e==="webgl")&&"webgl"in sn&&(t=sn.webgl),!t)try{let i=CA();t=ym(i,e)}catch{let i=PA();t=ym(i,e)}e=e||t.version===1?"webgl":"webgl2";let n=t.gl;return sn[e]=t,n.isContextLost()?(delete sn[e],Qv(e)):(n.disable(n.DEPTH_TEST),n.disable(n.STENCIL_TEST),n.disable(n.BLEND),n.disable(n.DITHER),n.disable(n.POLYGON_OFFSET_FILL),n.disable(n.SAMPLE_COVERAGE),n.enable(n.SCISSOR_TEST),n.enable(n.CULL_FACE),n.cullFace(n.BACK),t)}function ym(e,t){let n={alpha:!1,depth:!1,antialias:!1,stencil:!1,preserveDrawingBuffer:!1,premultipliedAlpha:!1,failIfMajorPerformanceCaveat:!1},i,u=n;if((!t||t==="webgl2")&&(i=e.getContext("webgl2",u),i))try{return new Il(i,2)}catch(l){Je.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl2'. Error: ${l}`)}if((!t||t==="webgl")&&(i=e.getContext("webgl",u)||e.getContext("experimental-webgl",u),i))try{return new Il(i,1)}catch(l){Je.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl' or 'experimental-webgl'. Error: ${l}`)}throw new Error("WebGL is not supported")}function PA(){if(typeof document>"u")throw new TypeError("failed to create canvas: document is not supported");let e=document.createElement("canvas");return e.width=1,e.height=1,e}function CA(){if(typeof OffscreenCanvas>"u")throw new TypeError("failed to create offscreen canvas: OffscreenCanvas is not supported");return new OffscreenCanvas(1,1)}var sn,RA=D(()=>{"use strict";qt(),kA(),sn={}}),ex,DA=D(()=>{"use strict";dt(),qt(),EA(),RA(),ex=class{get contextId(){return me.webgl.contextId}set contextId(e){me.webgl.contextId=e}get matmulMaxBatchSize(){return me.webgl.matmulMaxBatchSize}set matmulMaxBatchSize(e){me.webgl.matmulMaxBatchSize=e}get textureCacheMode(){return me.webgl.textureCacheMode}set textureCacheMode(e){me.webgl.textureCacheMode=e}get pack(){return me.webgl.pack}set pack(e){me.webgl.pack=e}get async(){return me.webgl.async}set async(e){me.webgl.async=e}initialize(){try{return this.glContext=Qv(this.contextId),typeof this.matmulMaxBatchSize!="number"&&(this.matmulMaxBatchSize=16),typeof this.textureCacheMode!="string"&&(this.textureCacheMode="full"),typeof this.pack!="boolean"&&(this.pack=!1),typeof this.async!="boolean"&&(this.async=!1),Je.setWithEnv(me),me.webgl.context||Object.defineProperty(me.webgl,"context",{value:this.glContext.gl}),Je.verbose("WebGLBackend",`Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${this.matmulMaxBatchSize}; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${this.async}.`),!0}catch(e){return Je.warning("WebGLBackend",`Unable to initialize WebGLBackend. ${e}`),!1}}createSessionHandler(e){return new Jv(this,e)}dispose(){this.glContext.dispose()}}});async function tx(e){if(e){let t=typeof e=="string"?[e]:e;for(let n of t){let i=ed.get(n);if(i)return i;let u=await MA(n);if(u)return u}}else return tx(["webgl"]);throw new Error("no available backend to use")}async function MA(e){let t=rx;if(typeof t[e]<"u"&&NA(t[e])){let n=t[e],i=n.initialize();if(typeof i=="object"&&"then"in i&&(i=await i),i)return ed.set(e,n),n}}function NA(e){let t=e;return"initialize"in t&&typeof t.initialize=="function"&&"createSessionHandler"in t&&typeof t.createSessionHandler=="function"&&"dispose"in t&&typeof t.dispose=="function"}var ed,rx,zA=D(()=>{"use strict";DA(),ed=new Map,rx={webgl:new ex}}),wm,nx,BA=D(()=>{"use strict";qt(),wm=class{constructor(e,t){this.op=e,this.node=t}},nx=class{constructor(e,t,n){this.graph=e,this.profiler=n,this.initialize(t)}initialize(e){this.profiler.event("session","ExecutionPlan.initialize",()=>{let t=this.graph.getNodes();if(t.length!==e.length)throw new Error("The size of nodes and OPs do not match.");this._ops=e.map((n,i)=>new wm(n,t[i])),this.reset(),this._starter=[],this._ops.forEach((n,i)=>{let u=!0;for(let l of n.node.inputs)if(!this._values[l]&&this.graph.getInputIndices().indexOf(l)===-1){u=!1;break}u&&this._starter.push(i)})})}reset(){this._values=this.graph.getValues().map(e=>e.tensor)}async execute(e,t){return this.profiler.event("session","ExecutionPlan.execute",async()=>{this.reset();let n=e.createInferenceHandler(),i=this.graph.getInputIndices();if(t.length!==i.length)throw new Error(`number of input tensors don't match the number of inputs to the model: actual: ${t.length} expected: ${i.length}`);t.forEach((r,o)=>{let a=i[o];this._values[a]=r});let u=this._starter.slice(0),l=this.graph.getValues(),d=this.graph.getNodes(),c=0;for(;cthis._values[g]);if(a.indexOf(void 0)!==-1)throw new Error(`unresolved input detected: op: ${o.node}`);let p=a;Je.verbose("ExecPlan",`Running op:${o.node.name} (${p.map((g,_)=>`'${o.node.inputs[_]}': ${g.type}[${g.dims.join(",")}]`).join(", ")})`);let h=await this.profiler.event("node",o.node.name,async()=>o.op.impl(n,p,o.op.context));if(h.length!==o.node.outputs.length)throw new Error("the size of output does not match model definition.");h.forEach((g,_)=>{let w=o.node.outputs[_];if(this._values[w])throw new Error(`output [${w}] already has value: op:${o.node.name}`);this._values[w]=g});let m=new Set;h.forEach((g,_)=>{let w=o.node.outputs[_];for(let y of l[w].to){let T=d[y],S=!0;for(let O of T.inputs)if(!this._values[O]){S=!1;break}S&&m.add(y)}}),u.push(...m)}let s=[];for(let r=0;r{"use strict";ns(),Se=be(Ni()),Ln(),Me(),Ol=class Ti{constructor(t){if(this._attributes=new Map,t!=null){for(let n of t)n instanceof Se.onnx.AttributeProto?this._attributes.set(n.name,[Ti.getValue(n),Ti.getType(n)]):n instanceof rl.Attribute&&this._attributes.set(n.name(),[Ti.getValue(n),Ti.getType(n)]);if(this._attributes.sizext.fromProto(u));if(t instanceof rl.Attribute)return i.map(u=>xt.fromOrtTensor(u))}return n===Se.onnx.AttributeProto.AttributeType.STRING&&t instanceof Se.onnx.AttributeProto?nl(i):n===Se.onnx.AttributeProto.AttributeType.STRINGS&&t instanceof Se.onnx.AttributeProto?i.map(nl):i}static getValueNoCheck(t){return t instanceof Se.onnx.AttributeProto?this.getValueNoCheckFromOnnxFormat(t):this.getValueNoCheckFromOrtFormat(t)}static getValueNoCheckFromOnnxFormat(t){switch(t.type){case Se.onnx.AttributeProto.AttributeType.FLOAT:return t.f;case Se.onnx.AttributeProto.AttributeType.INT:return t.i;case Se.onnx.AttributeProto.AttributeType.STRING:return t.s;case Se.onnx.AttributeProto.AttributeType.TENSOR:return t.t;case Se.onnx.AttributeProto.AttributeType.GRAPH:return t.g;case Se.onnx.AttributeProto.AttributeType.FLOATS:return t.floats;case Se.onnx.AttributeProto.AttributeType.INTS:return t.ints;case Se.onnx.AttributeProto.AttributeType.STRINGS:return t.strings;case Se.onnx.AttributeProto.AttributeType.TENSORS:return t.tensors;case Se.onnx.AttributeProto.AttributeType.GRAPHS:return t.graphs;default:throw new Error(`unsupported attribute type: ${Se.onnx.AttributeProto.AttributeType[t.type]}`)}}static getValueNoCheckFromOrtFormat(t){switch(t.type()){case jt.AttributeType.FLOAT:return t.f();case jt.AttributeType.INT:return t.i();case jt.AttributeType.STRING:return t.s();case jt.AttributeType.TENSOR:return t.t();case jt.AttributeType.GRAPH:return t.g();case jt.AttributeType.FLOATS:return t.floatsArray();case jt.AttributeType.INTS:{let n=[];for(let i=0;i{"use strict";FA(),ns(),Ba=be(Ni()),Ln(),Me(),El={from:(e,t)=>new _m(e,t)},nr=class{constructor(e){this._from=void 0,this._to=[],this.tensor=void 0,this.type=void 0,e&&(this.type=wt.tensorValueTypeFromProto(e.type.tensorType))}get from(){return this._from}get to(){return this._to}},Fa=class{constructor(e,t){e instanceof Ba.onnx.NodeProto?(this.name=e.name,this.opType=e.opType,this.attributes=new Ol(e.attribute)):e instanceof mw.Node&&(this.name=t??e.name(),this.opType=e.opType(),this.attributes=new Ol(wt.tensorAttributesFromORTFormat(e))),this.inputs=[],this.outputs=[],this.executeNode=!0}},_m=class{constructor(e,t){if(!e)throw new TypeError("graph is empty");this.buildGraph(e),this.transformGraph(t),this.checkIsAcyclic()}getInputIndices(){return this._allInputIndices}getInputNames(){return this._allInputNames}getOutputIndices(){return this._allOutputIndices}getOutputNames(){return this._allOutputNames}getValues(){return this._allData}getNodes(){return this._nodes}buildGraph(e){if(e instanceof Ba.onnx.GraphProto)this.buildGraphFromOnnxFormat(e);else if(e instanceof hw.Graph)this.buildGraphFromOrtFormat(e);else throw new TypeError("Graph type is not supported.")}buildGraphFromOnnxFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map;if(!e.input)throw new Error("missing information in graph: input");let i=[];for(let u of e.input){if(t.has(u.name))throw new Error(`duplicated input name: ${u.name}`);let l=this._allData.push(new nr(u))-1;t.set(u.name,l),i.push(u.name)}if(!e.initializer)throw new Error("missing information in graph: initializer");for(let u of e.initializer){let l=t.get(u.name);if(l===void 0){let d=new nr;d.type={shape:{dims:wt.tensorDimsFromProto(u.dims)},tensorType:wt.tensorDataTypeFromProto(u.dataType)},l=this._allData.push(d)-1,t.set(u.name,l)}this._allData[l]._from=-1,this._allData[l].tensor=xt.fromProto(u)}for(let u=0;u"u"&&(s=this._allData.push(new nr)-1,t.set(c,s)),l.outputs.push(s),this._allData[s]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${s}`);if(this._allData[s]._from=u,d.opType==="Constant"){if(!d.attribute||d.attribute.length!==1||!d.attribute[0].t)throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(!d.output||d.output.length!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[s]._from=-1,this._allData[s].tensor=xt.fromProto(d.attribute[0].t)}}}for(let u=0;u"u"){if(c===""&&(d.input.length===3||d.input.length===4)&&d.opType==="Resize")continue;throw new Error(`unrecognized input '${c}' for node: ${d.name}`)}l.inputs.push(s),this._allData[s]._to.push(u)}}return!0}buildGraphFromOrtFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map,i=[];for(let u=0;u"u"&&(r=this._allData.push(new nr)-1,t.set(s,r)),l.outputs.push(r),this._allData[r]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${r}`);if(this._allData[r]._from=u,d.opType()==="Constant"){if(d.attributesLength()!==1||!d.attributes(0).t())throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(d.outputsLength()!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[r]._from=-1,this._allData[r].tensor=xt.fromOrtTensor(d.attributes(0).t())}}}for(let u=0;u"u")throw new Error(`unrecognized input '${s}' for node: ${d.name()}`);l.inputs.push(r),this._allData[r]._to.push(u)}}}checkIsAcyclic(){let e=new Set;this._allInputIndices.forEach(i=>{this._allData[i]._to.forEach(u=>{e.add(u)})});let t=Array.from(e),n=new Array(this._nodes.length).fill("white");for(;t.length>0;){let i=t.pop();n[i]==="gray"?n[i]="black":(t.push(i),n[i]="gray",this._nodes[i].outputs.forEach(u=>{let l=this._allData[u];if(typeof l.tensor<"u")throw new Error("node outputs should not be initialized");if(l._from!==i)throw new Error("from property of the Value object doesn't match index of Node being processed");l._to.forEach(d=>{if(n[d]==="gray")throw new Error("model graph is cyclic");n[d]==="white"&&t.push(d)})}))}}transformGraph(e){this.removeAllIdentityNodes(),this.removeAllDropoutNodes(),this.fuseConvActivationNodes(),e&&e.transformGraph(this),this.finalizeGraph()}finalizeGraph(){let e=0,t=new Array(this._nodes.length,0),n=0;for(let i=0;i{this._allData[u]._from=-2});this._nodes.splice(n,this._nodes.length-n);for(let i=0;i=0)u._to[l]=t[u._to[l]];else throw new Error("Trying to update a removed node")}e=0;for(let i=0;i0){let u=-1;this._allData[i].from!==void 0&&this._allData[i].from!==-1?(u=this._nodes[this._allData[i].from].outputs.indexOf(i+e),u!==-1&&(this._nodes[this._allData[i].from].outputs[u]=i)):(u=this._allInputIndices.indexOf(i+e),u!==-1&&(this._allInputIndices[u]=i)),this._allData[i].to.forEach(l=>{u=this._nodes[l].inputs.indexOf(i+e),u!==-1&&(this._nodes[l].inputs[u]=i)}),this._allData[i].to.length===0&&(u=this._allOutputIndices.indexOf(i+e),u!==-1&&(this._allOutputIndices[u]=i))}}}deleteNode(e){let t=this._nodes[e];if(t.outputs.length>1){for(let d=1;d0)throw new Error("Node deletion with more than one output connected to other nodes is not supported. ")}t.executeNode=!1;let n=t.inputs[0],i=t.outputs[0],u=this._allData[i].to;for(let d=0;d0)for(let d of u){let c=this._nodes[d].inputs.indexOf(i);if(c===-1)throw new Error("The Node object doesn't have the output Value in it's 'inputs' property ");this._nodes[d].inputs[c]=n,this._allData[n].to.push(d)}}removeAllDropoutNodes(){let e=0;for(let t of this._nodes){if(t.opType==="Dropout"){if(t.inputs.length!==1)throw new Error("Dropout nodes should only contain one input. ");if(t.outputs.length!==1&&t.outputs.length!==2)throw new Error("Dropout nodes should contain either 1 or 2 output(s)");if(t.outputs.length===2&&this._allData[t.outputs[1]]._to.length!==0)throw new Error("Dropout nodes's second output should not be referenced by other nodes");this.deleteNode(e)}e++}}removeAllIdentityNodes(){let e=0;for(let t of this._nodes)t.opType==="Identity"&&this.deleteNode(e),e++}isActivation(e){switch(e.opType){case"Relu":case"Sigmoid":case"Clip":return!0;default:return!1}}fuseConvActivationNodes(){for(let e of this._nodes)if(e.opType==="Conv"){let t=this._allData[e.outputs[0]]._to;if(t.length===1&&this.isActivation(this._nodes[t[0]])){let n=this._nodes[t[0]];if(n.opType==="Clip")if(n.inputs.length===1)try{e.attributes.set("activation_params","floats",[n.attributes.getFloat("min"),n.attributes.getFloat("max")])}catch{e.attributes.set("activation_params","floats",[Ci,Ri])}else if(n.inputs.length>=3&&this._allData[n.inputs[1]].tensor!==void 0&&this._allData[n.inputs[2]].tensor!==void 0)e.attributes.set("activation_params","floats",[this._allData[n.inputs[1]].tensor.floatData[0],this._allData[n.inputs[2]].tensor.floatData[0]]);else continue;e.attributes.set("activation","string",n.opType),this.deleteNode(t[0])}}}}}),vm,xm,ix,jA=D(()=>{"use strict";vm=be(De()),LA(),ns(),xm=be(Ni()),Me(),ix=class{constructor(){}load(e,t,n){let i;if(!n)try{this.loadFromOnnxFormat(e,t);return}catch(u){if(n!==void 0)throw u;i=u}try{this.loadFromOrtFormat(e,t)}catch(u){throw n!==void 0?u:new Error(`Failed to load model as ONNX format: ${i} +as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=xm.onnx.ModelProto.decode(e);if(Yt.longToNumber(n.irVersion)<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=n.opsetImport.map(i=>({domain:i.domain,version:Yt.longToNumber(i.version)})),this._graph=El.from(n.graph,t)}loadFromOrtFormat(e,t){let n=new vm.ByteBuffer(e),i=fw.InferenceSession.getRootAsInferenceSession(n).model();if(Yt.longToNumber(i.irVersion())<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=[];for(let u=0;u{"use strict";zA(),BA(),qt(),jA(),ox=class{constructor(e={}){this._initialized=!1,this.backendHint=e.backendHint,this.profiler=A0.create(e.profiler),this.context={profiler:this.profiler,graphInputTypes:[],graphInputDims:[]}}get inputNames(){return this._model.graph.getInputNames()}get outputNames(){return this._model.graph.getOutputNames()}startProfiling(){this.profiler.start()}endProfiling(){this.profiler.stop()}async loadModel(e,t,n){await this.profiler.event("session","Session.loadModel",async()=>{let i=await tx(this.backendHint);if(this.sessionHandler=i.createSessionHandler(this.context),this._model=new ix,typeof e=="string"){let u=e.endsWith(".ort");{let l=await(await fetch(e)).arrayBuffer();this.initialize(new Uint8Array(l),u)}}else if(ArrayBuffer.isView(e))this.initialize(e);else{let u=new Uint8Array(e,t||0,n||e.byteLength);this.initialize(u)}})}initialize(e,t){if(this._initialized)throw new Error("already initialized");this.profiler.event("session","Session.initialize",()=>{let n=this.sessionHandler.transformGraph?this.sessionHandler:void 0;this._model.load(e,n,t),this.sessionHandler.onGraphInitialized&&this.sessionHandler.onGraphInitialized(this._model.graph),this.initializeOps(this._model.graph),this._executionPlan=new nx(this._model.graph,this._ops,this.profiler)}),this._initialized=!0}async run(e){if(!this._initialized)throw new Error("session not initialized yet");return this.profiler.event("session","Session.run",async()=>{let t=this.normalizeAndValidateInputs(e),n=await this._executionPlan.execute(this.sessionHandler,t);return this.createOutput(n)})}normalizeAndValidateInputs(e){let t=this._model.graph.getInputNames();if(Array.isArray(e)){if(e.length!==t.length)throw new Error(`incorrect input array length: expected ${t.length} but got ${e.length}`)}else{if(e.size!==t.length)throw new Error(`incorrect input map size: expected ${t.length} but got ${e.size}`);let n=new Array(e.size),i=0;for(let u=0;u{"use strict";dt(),Ln(),sx=class{constructor(e){this.session=e,this.inputNames=this.session.inputNames,this.outputNames=this.session.outputNames}get inputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}get outputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}async dispose(){}async run(e,t,n){let i=new Map;for(let d in e)if(Object.hasOwnProperty.call(e,d)){let c=e[d];i.set(d,new xt(c.dims,c.type,void 0,void 0,c.data))}let u=await this.session.run(i),l={};return u.forEach((d,c)=>{l[c]=new vt(d.type,d.data,d.dims)}),l}startProfiling(){this.session.startProfiling()}endProfiling(){this.session.endProfiling()}}}),ax={};wn(ax,{onnxjsBackend:()=>ux});var $m,ux,qA=D(()=>{"use strict";UA(),VA(),$m=class{async init(){}async createInferenceSessionHandler(e,t){let n=new ox(t);return typeof e=="string"?await n.loadModel(e):await n.loadModel(e),new sx(n)}},ux=new $m}),td=D(()=>{"use strict"}),lx={};wn(lx,{default:()=>dx});var La,ja,dx,GA=D(()=>{"use strict";y$(),vn(),rd(),La="ort-wasm-proxy-worker",ja=globalThis.self?.name===La,ja&&(self.onmessage=e=>{let{type:t,in:n}=e.data;try{switch(t){case"init-wasm":nd(n.wasm).then(()=>{_d(n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})})},i=>{postMessage({type:t,err:i})});break;case"init-ep":{let{epName:i,env:u}=n;vd(u,i).then(()=>{postMessage({type:t})},l=>{postMessage({type:t,err:l})});break}case"copy-from":{let{buffer:i}=n,u=Yo(i);postMessage({type:t,out:u});break}case"create":{let{model:i,options:u}=n;xd(i,u).then(l=>{postMessage({type:t,out:l})},l=>{postMessage({type:t,err:l})});break}case"release":$d(n),postMessage({type:t});break;case"run":{let{sessionId:i,inputIndices:u,inputs:l,outputIndices:d,options:c}=n;Td(i,u,l,d,new Array(d.length).fill(null),c).then(s=>{s.some(r=>r[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:s},Id([...l,...s]))},s=>{postMessage({type:t,err:s})});break}case"end-profiling":Sd(n),postMessage({type:t});break;default:}}catch(i){postMessage({type:t,err:i})}}),dx=ja?null:e=>new Worker(e??yt,{type:"module",name:La})}),cx={};wn(cx,{default:()=>px});async function Tm(e={}){var t=e,n=!!globalThis.window,i=!!globalThis.WorkerGlobalScope,u=i&&self.name?.startsWith("em-pthread");t.mountExternalData=(f,b)=>{f.startsWith("./")&&(f=f.substring(2)),(t.Yc||(t.Yc=new Map)).set(f,b)},t.unmountExternalData=()=>{delete t.Yc,delete t.Zd,delete t.Yd,delete t.$d},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let l=f=>async(...b)=>{try{if(t.Xc)throw Error("Session already started");let $=t.Xc={Kd:b[0],errors:[]},x=await f(...b);if(t.Xc!==$)throw Error("Session mismatch");t.dd?.flush();let E=$.errors;if(0M),0{if(f==="webgpu"){[t.dd,t.Ad,t.Ed,t.ed,t.Dd,t.$b,t.Fd,t.Hd,t.Bd,t.Cd,t.Gd]=b;let $=t.dd;t.jsepRegisterBuffer=(x,E,k,M)=>$.registerBuffer(x,E,k,M),t.jsepGetBuffer=x=>$.getBuffer(x),t.jsepCreateDownloader=(x,E,k)=>$.createDownloader(x,E,k),t.jsepOnCreateSession=x=>{$.onCreateSession(x)},t.jsepOnReleaseSession=x=>{$.onReleaseSession(x)},t.jsepOnRunStart=x=>$.onRunStart(x),t.Id=(x,E)=>{$.upload(x,E)}}else if(f==="webnn"){let $=b[0];[t.Sd,t.sd,t.webnnEnsureTensor,t.td,t.webnnDownloadTensor,t.Rd,t.webnnEnableTraceEvent]=b.slice(1),t.webnnReleaseTensorId=t.sd,t.webnnUploadTensor=t.td,t.webnnRegisterMLContext=t.Rd,t.webnnOnRunStart=x=>$.onRunStart(x),t.webnnOnRunEnd=$.onRunEnd.bind($),t.webnnOnReleaseSession=x=>{$.onReleaseSession(x)},t.webnnCreateMLTensorDownloader=(x,E)=>$.createMLTensorDownloader(x,E),t.webnnRegisterMLTensor=(x,E,k,M)=>$.registerMLTensor(x,E,k,M),t.webnnCreateMLContext=x=>$.createMLContext(x),t.webnnRegisterGraphInput=$.registerGraphInput.bind($),t.webnnIsGraphInput=$.isGraphInput.bind($),t.webnnRegisterGraphOutput=$.registerGraphOutput.bind($),t.webnnIsGraphOutput=$.isGraphOutput.bind($),t.webnnCreateTemporaryTensor=$.createTemporaryTensor.bind($),t.webnnIsGraphInputOutputTypeSupported=$.isGraphInputOutputTypeSupported.bind($)}};let d=()=>{let f=b=>(...$)=>{let x=Wt;return $=b(...$),Wt!=x?new Promise((E,k)=>{Ks={resolve:E,reject:k}}):$};(()=>{for(let b of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[b]=f(t[b])})(),l!==void 0&&(t._OrtRun=l(t._OrtRun),t._OrtRunWithBinding=l(t._OrtRunWithBinding)),d=void 0};t.asyncInit=()=>{d?.()};var c,s,r=(f,b)=>{throw b},o=import.meta.url,a="";if(n||i){try{a=new URL(".",o).href}catch{}i&&(s=f=>{var b=new XMLHttpRequest;return b.open("GET",f,!1),b.responseType="arraybuffer",b.send(null),new Uint8Array(b.response)}),c=async f=>{if(P(f))return new Promise(($,x)=>{var E=new XMLHttpRequest;E.open("GET",f,!0),E.responseType="arraybuffer",E.onload=()=>{E.status==200||E.status==0&&E.response?$(E.response):x(E.status)},E.onerror=x,E.send(null)});var b=await fetch(f,{credentials:"same-origin"});if(b.ok)return b.arrayBuffer();throw Error(b.status+" : "+b.url)}}var p,h,m,g,_,w,y=console.log.bind(console),T=console.error.bind(console),S=y,O=T,A=!1,P=f=>f.startsWith("file://");function I(){hr.buffer!=J.buffer&&Y()}if(u){let f=function(b){try{var $=b.data,x=$.Sc;if(x==="load"){let E=[];self.onmessage=k=>E.push(k),w=()=>{postMessage({Sc:"loaded"});for(let k of E)f(k);self.onmessage=f};for(let k of $.xd)t[k]&&!t[k].proxy||(t[k]=(...M)=>{postMessage({Sc:"callHandler",vd:k,args:M})},k=="print"&&(S=t[k]),k=="printErr"&&(O=t[k]));hr=$.Od,Y(),h=$.Pd,ne(),bo()}else if(x==="run"){(function(E){var k=(I(),R)[E+52>>>2>>>0];E=(I(),R)[E+56>>>2>>>0],mp(k,k-E),we(k)})($.Rc),Qs($.Rc,0,0,1,0,0),gc(),Gs($.Rc),G||(lp(),G=!0);try{oS($.Md,$.bd)}catch(E){if(E!="unwind")throw E}}else $.target!=="setimmediate"&&(x==="checkMailbox"?G&&lo():x&&(O(`worker: received unknown command ${x}`),O($)))}catch(E){throw dp(),E}};var z=f,G=!1;self.onunhandledrejection=b=>{throw b.reason||b},self.onmessage=f}var J,q,C,ee,v,R,H,L,V,K,B,te=!1;function Y(){var f=hr.buffer;t.HEAP8=J=new Int8Array(f),C=new Int16Array(f),t.HEAPU8=q=new Uint8Array(f),ee=new Uint16Array(f),t.HEAP32=v=new Int32Array(f),t.HEAPU32=R=new Uint32Array(f),H=new Float32Array(f),L=new Float64Array(f),V=new BigInt64Array(f),K=new BigUint64Array(f)}function N(){te=!0,u?w():rr.sb()}function X(f){throw O(f="Aborted("+f+")"),A=!0,f=new WebAssembly.RuntimeError(f+". Build with -sASSERTIONS for more info."),_?.(f),f}function W(){return{a:{ma:kI,hb:AI,g:sS,J:aS,f:uS,o:lS,i:dS,$:cS,b:pS,S:hS,Ia:xc,n:fS,aa:Ic,Ya:Oc,Ea:Ec,Ga:Ac,Za:kc,Wa:Pc,Pa:Cc,Va:Rc,ka:Dc,Fa:Mc,Ca:Nc,Xa:zc,Da:Bc,cb:mS,fa:bS,xa:yS,va:_S,ea:xS,N:$S,H:TS,wa:SS,_:CS,ya:RS,Sa:DS,Aa:NS,Ja:zS,ta:BS,ga:FS,Ra:Gs,$a:LS,Q:qS,r:XS,c:Vs,ib:ZS,y:YS,M:JS,D:QS,l:eI,s:Hc,jb:tI,I:rI,R:nI,j:iI,u:oI,q:sI,k:aI,Ma:uI,Na:lI,Oa:dI,Ka:Zc,La:Yc,ua:Jc,eb:pI,bb:mI,v:gI,ba:bI,ha:yI,ab:hI,V:wI,_a:_I,Ba:vI,F:cI,T:xI,la:mo,za:TI,gb:$I,fb:SI,Ta:rp,Ua:np,Ha:zr,U:ip,ja:op,Qa:sp,ia:ap,lb:p3,na:a3,mb:c3,oa:s3,G:ZI,e:DI,t:CI,w:PI,B:qI,nb:n3,Z:r3,x:zI,pa:i3,X:u3,ca:t3,ob:e3,pb:QI,O:GI,qa:JI,qb:YI,L:KI,Y:o3,d:RI,A:NI,m:MI,kb:h3,p:FI,z:LI,C:BI,E:jI,K:HI,ra:XI,P:l3,da:WI,W:d3,rb:VI,sa:UI,h:OI,a:hr,db:ht}}}async function ne(){function f(x,E){var k=rr=x.exports;x={};for(let[M,U]of Object.entries(k))typeof U=="function"?(k=jS(U),x[M]=k):x[M]=U;return rr=x,rr=(function(){var M=rr,U=re=>ge=>re(ge)>>>0,Q=re=>()=>re()>>>0;return(M=Object.assign({},M)).tb=U(M.tb),M.Xb=Q(M.Xb),M.Zb=U(M.Zb),M.lc=U(M.lc),M.mc=Q(M.mc),M.qc=U(M.qc),M})(),fc.push(rr._b),up=(x=rr).tb,lp=x.ub,t._OrtInit=x.vb,t._OrtGetLastError=x.wb,t._OrtCreateSessionOptions=x.xb,t._OrtAppendExecutionProvider=x.yb,t._OrtAddFreeDimensionOverride=x.zb,t._OrtAddSessionConfigEntry=x.Ab,t._OrtReleaseSessionOptions=x.Bb,t._OrtCreateSession=x.Cb,t._OrtReleaseSession=x.Db,t._OrtGetInputOutputCount=x.Eb,t._OrtGetInputOutputMetadata=x.Fb,t._OrtFree=x.Gb,t._OrtCreateTensor=x.Hb,t._OrtGetTensorData=x.Ib,t._OrtReleaseTensor=x.Jb,t._OrtCreateRunOptions=x.Kb,t._OrtAddRunConfigEntry=x.Lb,t._OrtReleaseRunOptions=x.Mb,t._OrtCreateBinding=x.Nb,t._OrtBindInput=x.Ob,t._OrtBindOutput=x.Pb,t._OrtClearBoundOutputs=x.Qb,t._OrtReleaseBinding=x.Rb,t._OrtRunWithBinding=x.Sb,t._OrtRun=x.Tb,t._OrtEndProfiling=x.Ub,t._JsepOutput=x.Vb,t._JsepGetNodeName=x.Wb,go=x.Xb,Kt=t._free=x.Yb,ei=t._malloc=x.Zb,Qs=x.ac,dp=x.bc,cp=x.cc,pp=x.dc,ea=x.ec,hp=x.fc,fp=x.gc,Te=x.hc,ti=x.ic,mp=x.jc,we=x.kc,ta=x.lc,_e=x.mc,gp=x.nc,ra=x.oc,bp=x.pc,yp=x.qc,wp=x.rc,na=x.sc,_p=x.tc,vp=x.uc,xp=x.vc,$p=x.wc,Tp=x.xc,Sp=x.yc,Ip=x.zc,Op=x.Ac,Ep=x.Bc,Ap=x.Cc,kp=x.Dc,Pp=x.Ec,Cp=x.Fc,Rp=x.Gc,Dp=x.Hc,Mp=x.Ic,Np=x.Jc,zp=x.Kc,Bp=x.Lc,Fp=x.Mc,Lp=x.Nc,jp=x.Pc,Up=x.Qc,Vp=x.$c,qp=x.ad,Gp=x.fd,Hp=x.kd,Wp=x.ld,Kp=x.md,Xp=x.nd,Zp=x.od,Yp=x.pd,Jp=x.qd,Qp=x.rd,eh=x.wd,th=x.Ud,rh=x.Vd,nh=x.Wd,ih=x.Xd,h=E,rr}var b,$=W();return t.instantiateWasm?new Promise(x=>{t.instantiateWasm($,(E,k)=>{x(f(E,k))})}):u?f(new WebAssembly.Instance(h,W()),h):(B??=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",a):a+"ort-wasm-simd-threaded.jsep.wasm":new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href,b=await(async function(x){var E=B;if(!p&&!P(E))try{var k=fetch(E,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(k,x)}catch(M){O(`wasm streaming compile failed: ${M}`),O("falling back to ArrayBuffer instantiation")}return(async function(M,U){try{var Q=await(async function(re){if(!p)try{var ge=await c(re);return new Uint8Array(ge)}catch{}if(re==B&&p)re=new Uint8Array(p);else{if(!s)throw"both async and sync fetching of the wasm failed";re=s(re)}return re})(M);return await WebAssembly.instantiate(Q,U)}catch(re){O(`failed to asynchronously prepare wasm: ${re}`),X(re)}})(E,x)})($),f(b.instance,b.module))}class le{name="ExitStatus";constructor(b){this.message=`Program terminated with exit(${b})`,this.status=b}}var ce=f=>{f.terminate(),f.onmessage=()=>{}},xe=[],qe=0,Ve=null,it=f=>{Gt.length==0&&(yc(),bc(Gt[0]));var b=Gt.pop();if(!b)return 6;Jn.push(b),Br[f.Rc]=b,b.Rc=f.Rc;var $={Sc:"run",Md:f.Ld,bd:f.bd,Rc:f.Rc};return b.postMessage($,f.jd),0},Ze=0,$e=(f,b,...$)=>{var x,E=16*$.length,k=_e(),M=ta(E),U=M>>>3;for(x of $)typeof x=="bigint"?((I(),V)[U++>>>0]=1n,(I(),V)[U++>>>0]=x):((I(),V)[U++>>>0]=0n,(I(),L)[U++>>>0]=x);return f=cp(f,0,E,M,b),we(k),f};function ht(f){if(u)return $e(0,1,f);if(m=f,!(0{if(m=f,u)throw It(f),"unwind";ht(f)},Gt=[],Jn=[],fc=[],Br={},mc=f=>{var b=f.Rc;delete Br[b],Gt.push(f),Jn.splice(Jn.indexOf(f),1),f.Rc=0,pp(b)};function gc(){fc.forEach(f=>f())}var bc=f=>new Promise(b=>{f.onmessage=E=>{var k=E.data;if(E=k.Sc,k.Zc&&k.Zc!=go()){var M=Br[k.Zc];M?M.postMessage(k,k.jd):O(`Internal error! Worker sent a message "${E}" to target pthread ${k.Zc}, but that thread no longer exists!`)}else E==="checkMailbox"?lo():E==="spawnThread"?it(k):E==="cleanupThread"?uo(()=>{mc(Br[k.Nd])}):E==="loaded"?(f.loaded=!0,b(f)):k.target==="setimmediate"?f.postMessage(k):E==="uncaughtException"?f.onerror(k.error):E==="callHandler"?t[k.vd](...k.args):E&&O(`worker sent an unknown command ${E}`)},f.onerror=E=>{throw O(`worker sent an error! ${E.filename}:${E.lineno}: ${E.message}`),E};var $,x=[];for($ of[])t.propertyIsEnumerable($)&&x.push($);f.postMessage({Sc:"load",xd:x,Od:hr,Pd:h})});function yc(){var f=new Worker((()=>{let b=URL;return import.meta.url>"file:"&&import.meta.url<"file;"?new b("ort.all.bundle.min.mjs",import.meta.url):new URL(import.meta.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});Gt.push(f)}var hr,oS=(f,b)=>{Ze=0,f=na(f,b),0>>=0);return(I(),J)[b.Tc+12>>>0]==0&&(wc(b,!0),so--),_c(b,!1),oo.push(b),yp(f)}var On=0,aS=()=>{Te(0,0);var f=oo.pop();gp(f.cd),On=0};function wc(f,b){b=b?1:0,(I(),J)[f.Tc+12>>>0]=b}function _c(f,b){b=b?1:0,(I(),J)[f.Tc+13>>>0]=b}class Fs{constructor(b){this.cd=b,this.Tc=b-24}}var Ls=f=>{var b=On;if(!b)return ti(0),0;var $=new Fs(b);(I(),R)[$.Tc+16>>>2>>>0]=b;var x=(I(),R)[$.Tc+4>>>2>>>0];if(!x)return ti(0),b;for(var E of f){if(E===0||E===x)break;if(bp(E,x,$.Tc+16))return ti(E),b}return ti(x),b};function uS(){return Ls([])}function lS(f){return Ls([f>>>0])}function dS(f,b,$,x){return Ls([f>>>0,b>>>0,$>>>0,x>>>0])}var cS=()=>{var f=oo.pop();f||X("no exception to throw");var b=f.cd;throw(I(),J)[f.Tc+13>>>0]==0&&(oo.push(f),_c(f,!0),wc(f,!1),so++),ra(b),On=b};function pS(f,b,$){var x=new Fs(f>>>=0);throw b>>>=0,$>>>=0,(I(),R)[x.Tc+16>>>2>>>0]=0,(I(),R)[x.Tc+4>>>2>>>0]=b,(I(),R)[x.Tc+8>>>2>>>0]=$,ra(f),so++,On=f}var hS=()=>so;function vc(f,b,$,x){return u?$e(2,1,f,b,$,x):xc(f,b,$,x)}function xc(f,b,$,x){if(f>>>=0,b>>>=0,$>>>=0,x>>>=0,!globalThis.SharedArrayBuffer)return 6;var E=[];return u&&E.length===0?vc(f,b,$,x):(f={Ld:$,Rc:f,bd:x,jd:E},u?(f.Sc="spawnThread",postMessage(f,E),0):it(f))}function fS(f){throw On||=f>>>0,On}var $c=globalThis.TextDecoder&&new TextDecoder,Tc=(f,b,$,x)=>{if($=b+$,x)return $;for(;f[b]&&!(b>=$);)++b;return b},Sc=(f,b=0,$,x)=>{if(16<($=Tc(f,b>>>=0,$,x))-b&&f.buffer&&$c)return $c.decode(f.buffer instanceof ArrayBuffer?f.subarray(b,$):f.slice(b,$));for(x="";b<$;){var E=f[b++];if(128&E){var k=63&f[b++];if((224&E)==192)x+=String.fromCharCode((31&E)<<6|k);else{var M=63&f[b++];65536>(E=(240&E)==224?(15&E)<<12|k<<6|M:(7&E)<<18|k<<12|M<<6|63&f[b++])?x+=String.fromCharCode(E):(E-=65536,x+=String.fromCharCode(55296|E>>10,56320|1023&E))}}else x+=String.fromCharCode(E)}return x},et=(f,b,$)=>(f>>>=0)?Sc((I(),q),f,b,$):"";function Ic(f,b,$){return u?$e(3,1,f,b,$):0}function Oc(f,b){if(u)return $e(4,1,f,b)}function Ec(f,b){if(u)return $e(5,1,f,b)}function Ac(f,b,$){if(u)return $e(6,1,f,b,$)}function kc(f,b,$){return u?$e(7,1,f,b,$):0}function Pc(f,b){if(u)return $e(8,1,f,b)}function Cc(f,b,$){if(u)return $e(9,1,f,b,$)}function Rc(f,b,$,x){if(u)return $e(10,1,f,b,$,x)}function Dc(f,b,$,x){if(u)return $e(11,1,f,b,$,x)}function Mc(f,b,$,x){if(u)return $e(12,1,f,b,$,x)}function Nc(f){if(u)return $e(13,1,f)}function zc(f,b){if(u)return $e(14,1,f,b)}function Bc(f,b,$){if(u)return $e(15,1,f,b,$)}var mS=()=>X(""),Ht=f=>{f>>>=0;for(var b="";;){var $=(I(),q)[f++>>>0];if(!$)return b;b+=String.fromCharCode($)}},js={},Us={},gS={},En=class extends Error{constructor(f){super(f),this.name="BindingError"}};function tr(f,b,$={}){return(function(x,E,k={}){var M=E.name;if(!x)throw new En(`type "${M}" must have a positive integer typeid pointer`);if(Us.hasOwnProperty(x)){if(k.yd)return;throw new En(`Cannot register type '${M}' twice`)}Us[x]=E,delete gS[x],js.hasOwnProperty(x)&&(E=js[x],delete js[x],E.forEach(U=>U()))})(f,b,$)}var Fc=(f,b,$)=>{switch(b){case 1:return $?x=>(I(),J)[x>>>0]:x=>(I(),q)[x>>>0];case 2:return $?x=>(I(),C)[x>>>1>>>0]:x=>(I(),ee)[x>>>1>>>0];case 4:return $?x=>(I(),v)[x>>>2>>>0]:x=>(I(),R)[x>>>2>>>0];case 8:return $?x=>(I(),V)[x>>>3>>>0]:x=>(I(),K)[x>>>3>>>0];default:throw new TypeError(`invalid integer width (${b}): ${f}`)}};function bS(f,b,$,x,E){f>>>=0,$>>>=0,b=Ht(b>>>0);let k=M=>M;if(x=x===0n){let M=8*$;k=U=>BigInt.asUintN(M,U),E=k(E)}tr(f,{name:b,Oc:k,Vc:(M,U)=>(typeof U=="number"&&(U=BigInt(U)),U),Uc:Fc(b,$,!x),Wc:null})}function yS(f,b,$,x){tr(f>>>=0,{name:b=Ht(b>>>0),Oc:function(E){return!!E},Vc:function(E,k){return k?$:x},Uc:function(E){return this.Oc((I(),q)[E>>>0])},Wc:null})}var Lc=[],Fr=[0,1,,1,null,1,!0,1,!1,1];function Vs(f){9<(f>>>=0)&&--Fr[f+1]===0&&(Fr[f]=void 0,Lc.push(f))}var Ot=f=>{if(!f)throw new En(`Cannot use deleted val. handle = ${f}`);return Fr[f]},Mt=f=>{switch(f){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let b=Lc.pop()||Fr.length;return Fr[b]=f,Fr[b+1]=1,b}};function qs(f){return this.Oc((I(),R)[f>>>2>>>0])}var wS={name:"emscripten::val",Oc:f=>{var b=Ot(f);return Vs(f),b},Vc:(f,b)=>Mt(b),Uc:qs,Wc:null};function _S(f){return tr(f>>>0,wS)}var vS=(f,b)=>{switch(b){case 4:return function($){return this.Oc((I(),H)[$>>>2>>>0])};case 8:return function($){return this.Oc((I(),L)[$>>>3>>>0])};default:throw new TypeError(`invalid float width (${b}): ${f}`)}};function xS(f,b,$){$>>>=0,tr(f>>>=0,{name:b=Ht(b>>>0),Oc:x=>x,Vc:(x,E)=>E,Uc:vS(b,$),Wc:null})}function $S(f,b,$,x,E){f>>>=0,$>>>=0,b=Ht(b>>>0);let k=U=>U;if(x===0){var M=32-8*$;k=U=>U<>>M,E=k(E)}tr(f,{name:b,Oc:k,Vc:(U,Q)=>Q,Uc:Fc(b,$,x!==0),Wc:null})}function TS(f,b,$){function x(k){var M=(I(),R)[k>>>2>>>0];return k=(I(),R)[k+4>>>2>>>0],new E((I(),J).buffer,k,M)}var E=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][b];tr(f>>>=0,{name:$=Ht($>>>0),Oc:x,Uc:x},{yd:!0})}var fr=(f,b,$)=>{var x=(I(),q);if(b>>>=0,0<$){var E=b;$=b+$-1;for(var k=0;k=M){if(b>=$)break;x[b++>>>0]=M}else if(2047>=M){if(b+1>=$)break;x[b++>>>0]=192|M>>6,x[b++>>>0]=128|63&M}else if(65535>=M){if(b+2>=$)break;x[b++>>>0]=224|M>>12,x[b++>>>0]=128|M>>6&63,x[b++>>>0]=128|63&M}else{if(b+3>=$)break;x[b++>>>0]=240|M>>18,x[b++>>>0]=128|M>>12&63,x[b++>>>0]=128|M>>6&63,x[b++>>>0]=128|63&M,k++}}x[b>>>0]=0,f=b-E}else f=0;return f},ao=f=>{for(var b=0,$=0;$=x?b++:2047>=x?b+=2:55296<=x&&57343>=x?(b+=4,++$):b+=3}return b};function SS(f,b){tr(f>>>=0,{name:b=Ht(b>>>0),Oc($){var x=(I(),R)[$>>>2>>>0];return x=et($+4,x,!0),Kt($),x},Vc($,x){x instanceof ArrayBuffer&&(x=new Uint8Array(x));var E=typeof x=="string";if(!(E||ArrayBuffer.isView(x)&&x.BYTES_PER_ELEMENT==1))throw new En("Cannot pass non-string to std::string");var k=E?ao(x):x.length,M=ei(4+k+1),U=M+4;return(I(),R)[M>>>2>>>0]=k,E?fr(x,U,k+1):(I(),q).set(x,U>>>0),$!==null&&$.push(Kt,M),M},Uc:qs,Wc($){Kt($)}})}var jc=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,IS=(f,b,$)=>{if(f>>>=1,16<(b=Tc((I(),ee),f,b/2,$))-f&&jc)return jc.decode((I(),ee).slice(f,b));for($="";f>>0];$+=String.fromCharCode(x)}return $},OS=(f,b,$)=>{if($??=2147483647,2>$)return 0;var x=b;$=($-=2)<2*f.length?$/2:f.length;for(var E=0;E<$;++E){var k=f.charCodeAt(E);(I(),C)[b>>>1>>>0]=k,b+=2}return(I(),C)[b>>>1>>>0]=0,b-x},ES=f=>2*f.length,AS=(f,b,$)=>{var x="";f>>>=2;for(var E=0;!(E>=b/4);E++){var k=(I(),R)[f+E>>>0];if(!k&&!$)break;x+=String.fromCodePoint(k)}return x},kS=(f,b,$)=>{if(b>>>=0,$??=2147483647,4>$)return 0;var x=b;$=x+$-4;for(var E=0;E>>2>>>0]=k,(b+=4)+4>$)break}return(I(),v)[b>>>2>>>0]=0,b-x},PS=f=>{for(var b=0,$=0;$>>=0,b>>>=0,$=Ht($>>>=0),b===2)var x=IS,E=OS,k=ES;else x=AS,E=kS,k=PS;tr(f,{name:$,Oc:M=>{var U=(I(),R)[M>>>2>>>0];return U=x(M+4,U*b,!0),Kt(M),U},Vc:(M,U)=>{if(typeof U!="string")throw new En(`Cannot pass non-string to C++ string type ${$}`);var Q=k(U),re=ei(4+Q+b);return(I(),R)[re>>>2>>>0]=Q/b,E(U,re+4,Q+b),M!==null&&M.push(Kt,re),re},Uc:qs,Wc(M){Kt(M)}})}function RS(f,b){tr(f>>>=0,{zd:!0,name:b=Ht(b>>>0),Oc:()=>{},Vc:()=>{}})}function DS(f){Qs(f>>>0,!i,1,!n,131072,!1),gc()}var uo=f=>{if(!A)try{if(f(),!(0Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function Gs(f){f>>>=0,MS||(Atomics.waitAsync((I(),v),f>>>2,f).value.then(lo),f+=128,Atomics.store((I(),v),f>>>2,1))}var lo=()=>uo(()=>{var f=go();f&&(Gs(f),fp())});function NS(f,b){(f>>>=0)==b>>>0?setTimeout(lo):u?postMessage({Zc:f,Sc:"checkMailbox"}):(f=Br[f])&&f.postMessage({Sc:"checkMailbox"})}var Hs=[];function zS(f,b,$,x,E){for(b>>>=0,E>>>=0,Hs.length=0,$=E>>>3,x=E+x>>>3;$>>0]?(I(),V)[$++>>>0]:(I(),L)[$++>>>0],Hs.push(k)}return(b?ia[b]:EI[f])(...Hs)}var BS=()=>{Ze=0};function FS(f){f>>>=0,u?postMessage({Sc:"cleanupThread",Nd:f}):mc(Br[f])}function LS(f){}var co=f=>{try{f()}catch(b){X(b)}};function jS(f){var b=(...$)=>{po.push(f);try{return f(...$)}finally{A||(po.pop(),Wt&&mr===1&&po.length===0&&(mr=0,Ze+=1,co(rh),typeof Fibers<"u"&&Fibers.be()))}};return qc.set(f,b),b}var mr=0,Wt=null,Uc=0,po=[],Ws=new Map,Vc=new Map,qc=new Map,US=0,Ks=null,VS=[],Gc=f=>(function(b){if(!A){if(mr===0){var $=!1,x=!1;b((E=0)=>{if(!A&&(Uc=E,$=!0,x)){mr=2,co(()=>nh(Wt)),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.resume(),E=!1;try{var k=(function(){var Q=(I(),v)[Wt+8>>>2>>>0];return Q=Vc.get(Q),Q=qc.get(Q),--Ze,Q()})()}catch(Q){k=Q,E=!0}var M=!1;if(!Wt){var U=Ks;U&&(Ks=null,(E?U.reject:U.resolve)(k),M=!0)}if(E&&!M)throw k}}),x=!0,$||(mr=1,Wt=(function(){var E=ei(65548),k=E+12;if((I(),R)[E>>>2>>>0]=k,(I(),R)[E+4>>>2>>>0]=k+65536,k=po[0],!Ws.has(k)){var M=US++;Ws.set(k,M),Vc.set(M,k)}return k=Ws.get(k),(I(),v)[E+8>>>2>>>0]=k,E})(),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.pause(),co(()=>th(Wt)))}else mr===2?(mr=0,co(ih),Kt(Wt),Wt=null,VS.forEach(uo)):X(`invalid state: ${mr}`);return Uc}})(b=>{f().then(b)});function qS(f){return f>>>=0,Gc(async()=>{var b=await Ot(f);return Mt(b)})}var Xs=[],GS=f=>{var b=Xs.length;return Xs.push(f),b},HS=(f,b)=>{for(var $=Array(f),x=0;x>>2>>>0],M=Us[k];if(M===void 0)throw f=`parameter ${x}`,k=up(k),b=Ht(k),Kt(k),new En(`${f} has unknown type ${b}`);$[E]=M}return $},WS=(f,b,$)=>{var x=[];return f=f(x,$),x.length&&((I(),R)[b>>>2>>>0]=Mt(x)),f},KS={},ho=f=>{var b=KS[f];return b===void 0?Ht(f):b};function XS(f,b,$){var[x,...E]=HS(f,b>>>0);b=x.Vc.bind(x);var k=E.map(Q=>Q.Uc.bind(Q));f--;var M={toValue:Ot};switch(f=k.map((Q,re)=>{var ge=`argFromPtr${re}`;return M[ge]=Q,`${ge}(args${re?"+"+8*re:""})`}),$){case 0:var U="toValue(handle)";break;case 2:U="new (toValue(handle))";break;case 3:U="";break;case 1:M.getStringOrSymbol=ho,U="toValue(handle)[getStringOrSymbol(methodName)]"}return U+=`(${f})`,x.zd||(M.toReturnWire=b,M.emval_returnValue=WS,U=`return emval_returnValue(toReturnWire, destructorsRef, ${U})`),U=`return function (handle, methodName, destructorsRef, args) { + ${U} + }`,$=new Function(Object.keys(M),U)(...Object.values(M)),U=`methodCaller<(${E.map(Q=>Q.name)}) => ${x.name}>`,GS(Object.defineProperty($,"name",{value:U}))}function ZS(f,b){return b>>>=0,(f=Ot(f>>>0))==Ot(b)}function YS(f){return(f>>>=0)?(f=ho(f),Mt(globalThis[f])):Mt(globalThis)}function JS(f){return f=ho(f>>>0),Mt(t[f])}function QS(f,b){return b>>>=0,f=Ot(f>>>0),b=Ot(b),Mt(f[b])}function eI(f){9<(f>>>=0)&&(Fr[f+1]+=1)}function Hc(f,b,$,x,E){return Xs[f>>>0](b>>>0,$>>>0,x>>>0,E>>>0)}function tI(f,b,$,x,E){return Hc(f>>>0,b>>>0,$>>>0,x>>>0,E>>>0)}function rI(){return Mt([])}function nI(f){f=Ot(f>>>0);for(var b=Array(f.length),$=0;$>>0))}function oI(){return Mt({})}function sI(f){for(var b=Ot(f>>>=0);b.length;){var $=b.pop();b.pop()($)}Vs(f)}function aI(f,b,$){b>>>=0,$>>>=0,f=Ot(f>>>0),b=Ot(b),$=Ot($),f[b]=$}function uI(f,b){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(I(),v)[b>>>2>>>0]=f.getUTCSeconds(),(I(),v)[b+4>>>2>>>0]=f.getUTCMinutes(),(I(),v)[b+8>>>2>>>0]=f.getUTCHours(),(I(),v)[b+12>>>2>>>0]=f.getUTCDate(),(I(),v)[b+16>>>2>>>0]=f.getUTCMonth(),(I(),v)[b+20>>>2>>>0]=f.getUTCFullYear()-1900,(I(),v)[b+24>>>2>>>0]=f.getUTCDay(),f=(f.getTime()-Date.UTC(f.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(I(),v)[b+28>>>2>>>0]=f}var Wc=f=>f%4==0&&(f%100!=0||f%400==0),Kc=[0,31,60,91,121,152,182,213,244,274,305,335],Xc=[0,31,59,90,120,151,181,212,243,273,304,334];function lI(f,b){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(I(),v)[b>>>2>>>0]=f.getSeconds(),(I(),v)[b+4>>>2>>>0]=f.getMinutes(),(I(),v)[b+8>>>2>>>0]=f.getHours(),(I(),v)[b+12>>>2>>>0]=f.getDate(),(I(),v)[b+16>>>2>>>0]=f.getMonth(),(I(),v)[b+20>>>2>>>0]=f.getFullYear()-1900,(I(),v)[b+24>>>2>>>0]=f.getDay();var $=(Wc(f.getFullYear())?Kc:Xc)[f.getMonth()]+f.getDate()-1|0;(I(),v)[b+28>>>2>>>0]=$,(I(),v)[b+36>>>2>>>0]=-60*f.getTimezoneOffset(),$=new Date(f.getFullYear(),6,1).getTimezoneOffset();var x=new Date(f.getFullYear(),0,1).getTimezoneOffset();f=0|($!=x&&f.getTimezoneOffset()==Math.min(x,$)),(I(),v)[b+32>>>2>>>0]=f}function dI(f){f>>>=0;var b=new Date((I(),v)[f+20>>>2>>>0]+1900,(I(),v)[f+16>>>2>>>0],(I(),v)[f+12>>>2>>>0],(I(),v)[f+8>>>2>>>0],(I(),v)[f+4>>>2>>>0],(I(),v)[f>>>2>>>0],0),$=(I(),v)[f+32>>>2>>>0],x=b.getTimezoneOffset(),E=new Date(b.getFullYear(),6,1).getTimezoneOffset(),k=new Date(b.getFullYear(),0,1).getTimezoneOffset(),M=Math.min(k,E);return 0>$?(I(),v)[f+32>>>2>>>0]=+(E!=k&&M==x):0<$!=(M==x)&&(E=Math.max(k,E),b.setTime(b.getTime()+6e4*((0<$?M:E)-x))),(I(),v)[f+24>>>2>>>0]=b.getDay(),$=(Wc(b.getFullYear())?Kc:Xc)[b.getMonth()]+b.getDate()-1|0,(I(),v)[f+28>>>2>>>0]=$,(I(),v)[f>>>2>>>0]=b.getSeconds(),(I(),v)[f+4>>>2>>>0]=b.getMinutes(),(I(),v)[f+8>>>2>>>0]=b.getHours(),(I(),v)[f+12>>>2>>>0]=b.getDate(),(I(),v)[f+16>>>2>>>0]=b.getMonth(),(I(),v)[f+20>>>2>>>0]=b.getYear(),f=b.getTime(),BigInt(isNaN(f)?-1:f/1e3)}function Zc(f,b,$,x,E,k,M){return u?$e(16,1,f,b,$,x,E,k,M):-52}function Yc(f,b,$,x,E,k){if(u)return $e(17,1,f,b,$,x,E,k)}var Qn={},cI=()=>performance.timeOrigin+performance.now();function Jc(f,b){if(u)return $e(18,1,f,b);if(Qn[f]&&(clearTimeout(Qn[f].id),delete Qn[f]),!b)return 0;var $=setTimeout(()=>{delete Qn[f],uo(()=>hp(f,performance.timeOrigin+performance.now()))},b);return Qn[f]={id:$,ae:b},0}function pI(f,b,$,x){f>>>=0,b>>>=0,$>>>=0,x>>>=0;var E=new Date().getFullYear(),k=new Date(E,0,1).getTimezoneOffset();E=new Date(E,6,1).getTimezoneOffset();var M=Math.max(k,E);(I(),R)[f>>>2>>>0]=60*M,(I(),v)[b>>>2>>>0]=+(k!=E),f=(b=U=>{var Q=Math.abs(U);return`UTC${0<=U?"-":"+"}${String(Math.floor(Q/60)).padStart(2,"0")}${String(Q%60).padStart(2,"0")}`})(k),b=b(E),EDate.now(),fI=1;function mI(f,b,$){if($>>>=0,!(0<=f&&3>=f))return 28;if(f===0)f=Date.now();else{if(!fI)return 52;f=performance.timeOrigin+performance.now()}return f=Math.round(1e6*f),(I(),V)[$>>>3>>>0]=BigInt(f),0}var Zs=[],Qc=(f,b)=>{Zs.length=0;for(var $;$=(I(),q)[f++>>>0];){var x=$!=105;b+=(x&=$!=112)&&b%8?4:0,Zs.push($==112?(I(),R)[b>>>2>>>0]:$==106?(I(),V)[b>>>3>>>0]:$==105?(I(),v)[b>>>2>>>0]:(I(),L)[b>>>3>>>0]),b+=x?8:4}return Zs};function gI(f,b,$){return f>>>=0,b=Qc(b>>>0,$>>>0),ia[f](...b)}function bI(f,b,$){return f>>>=0,b=Qc(b>>>0,$>>>0),ia[f](...b)}var yI=()=>{};function wI(f,b){return O(et(f>>>0,b>>>0))}var _I=()=>{throw Ze+=1,"unwind"};function vI(){return 4294901760}var xI=()=>navigator.hardwareConcurrency,Lr={},fo=f=>{var b;return(b=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(f))?+b[1]:(b=/:(\d+):\d+(?:\)|$)/.exec(f))?2147483648|+b[1]:0},ep=f=>{for(var b of f)(f=fo(b))&&(Lr[f]=b)};function $I(){var f=Error().stack.toString().split(` +`);return f[0]=="Error"&&f.shift(),ep(f),Lr.gd=fo(f[3]),Lr.Jd=f,Lr.gd}function mo(f){if(!(f=Lr[f>>>0]))return 0;var b;if(b=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(f))f=b[1];else if(b=/^\s+at (.*) \(.*\)$/.exec(f))f=b[1];else{if(!(b=/^(.+?)@/.exec(f)))return 0;f=b[1]}Kt(mo.hd??0),b=ao(f)+1;var $=ei(b);return $&&fr(f,$,b),mo.hd=$,mo.hd}function TI(f){f>>>=0;var b=(I(),q).length;if(f<=b||4294901760=$;$*=2){var x=b*(1+.2/$);x=Math.min(x,f+100663296);e:{x=(Math.min(4294901760,65536*Math.ceil(Math.max(f,x)/65536))-hr.buffer.byteLength+65535)/65536|0;try{hr.grow(x),Y();var E=1;break e}catch{}E=void 0}if(E)return!0}return!1}function SI(f,b,$){if(f>>>=0,b>>>=0,Lr.gd==f)var x=Lr.Jd;else(x=Error().stack.toString().split(` +`))[0]=="Error"&&x.shift(),ep(x);for(var E=3;x[E]&&fo(x[E])!=f;)++E;for(f=0;f<$&&x[f+E];++f)(I(),v)[b+4*f>>>2>>>0]=fo(x[f+E]);return f}var Ys,Js={},tp=()=>{if(!Ys){var f,b={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(globalThis.navigator?.language??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(f in Js)Js[f]===void 0?delete b[f]:b[f]=Js[f];var $=[];for(f in b)$.push(`${f}=${b[f]}`);Ys=$}return Ys};function rp(f,b){if(u)return $e(19,1,f,b);f>>>=0,b>>>=0;var $,x=0,E=0;for($ of tp()){var k=b+x;(I(),R)[f+E>>>2>>>0]=k,x+=fr($,k,1/0)+1,E+=4}return 0}function np(f,b){if(u)return $e(20,1,f,b);f>>>=0,b>>>=0;var $=tp();for(var x of((I(),R)[f>>>2>>>0]=$.length,f=0,$))f+=ao(x)+1;return(I(),R)[b>>>2>>>0]=f,0}function ip(f){return u?$e(21,1,f):52}function op(f,b,$,x){return u?$e(22,1,f,b,$,x):52}function sp(f,b,$,x){return u?$e(23,1,f,b,$,x):70}var II=[null,[],[]];function ap(f,b,$,x){if(u)return $e(24,1,f,b,$,x);b>>>=0,$>>>=0,x>>>=0;for(var E=0,k=0;k<$;k++){var M=(I(),R)[b>>>2>>>0],U=(I(),R)[b+4>>>2>>>0];b+=8;for(var Q=0;Q>>0],Oe=II[re];ge===0||ge===10?((re===1?S:O)(Sc(Oe)),Oe.length=0):Oe.push(ge)}E+=U}return(I(),R)[x>>>2>>>0]=E,0}function OI(f){return f>>>0}u||(function(){for(var f=t.numThreads-1;f--;)yc();xe.push(async()=>{var b=(async function(){if(!u)return Promise.all(Gt.map(bc))})();qe++,await b,--qe==0&&Ve&&(b=Ve,Ve=null,b())})})(),u||(hr=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),Y()),t.wasmBinary&&(p=t.wasmBinary),t.stackSave=()=>_e(),t.stackRestore=f=>we(f),t.stackAlloc=f=>ta(f),t.setValue=function(f,b,$="i8"){switch($.endsWith("*")&&($="*"),$){case"i1":case"i8":(I(),J)[f>>>0]=b;break;case"i16":(I(),C)[f>>>1>>>0]=b;break;case"i32":(I(),v)[f>>>2>>>0]=b;break;case"i64":(I(),V)[f>>>3>>>0]=BigInt(b);break;case"float":(I(),H)[f>>>2>>>0]=b;break;case"double":(I(),L)[f>>>3>>>0]=b;break;case"*":(I(),R)[f>>>2>>>0]=b;break;default:X(`invalid type for setValue: ${$}`)}},t.getValue=function(f,b="i8"){switch(b.endsWith("*")&&(b="*"),b){case"i1":case"i8":return(I(),J)[f>>>0];case"i16":return(I(),C)[f>>>1>>>0];case"i32":return(I(),v)[f>>>2>>>0];case"i64":return(I(),V)[f>>>3>>>0];case"float":return(I(),H)[f>>>2>>>0];case"double":return(I(),L)[f>>>3>>>0];case"*":return(I(),R)[f>>>2>>>0];default:X(`invalid type for getValue: ${b}`)}},t.UTF8ToString=et,t.stringToUTF8=fr,t.lengthBytesUTF8=ao;var up,lp,go,Kt,ei,Qs,dp,cp,pp,ea,hp,fp,Te,ti,mp,we,ta,_e,gp,ra,bp,yp,wp,na,_p,vp,xp,$p,Tp,Sp,Ip,Op,Ep,Ap,kp,Pp,Cp,Rp,Dp,Mp,Np,zp,Bp,Fp,Lp,jp,Up,Vp,qp,Gp,Hp,Wp,Kp,Xp,Zp,Yp,Jp,Qp,eh,th,rh,nh,ih,rr,EI=[ht,It,vc,Ic,Oc,Ec,Ac,kc,Pc,Cc,Rc,Dc,Mc,Nc,zc,Bc,Zc,Yc,Jc,rp,np,ip,op,sp,ap],ia={1055492:(f,b,$,x,E)=>{if(t===void 0||!t.Yc)return 1;if((f=et(Number(f>>>0))).startsWith("./")&&(f=f.substring(2)),!(f=t.Yc.get(f)))return 2;if(b=Number(b>>>0),$=Number($>>>0),x=Number(x>>>0),b+$>f.byteLength)return 3;try{let k=f.subarray(b,b+$);switch(E){case 0:(I(),q).set(k,x>>>0);break;case 1:t.Qd?t.Qd(x,k):t.Id(x,k);break;default:return 4}return 0}catch{return 4}},1056316:(f,b,$)=>{t.td(f,(I(),q).subarray(b>>>0,b+$>>>0))},1056380:()=>t.Sd(),1056422:f=>{t.sd(f)},1056459:()=>{t.Bd()},1056490:()=>{t.Cd()},1056519:()=>{t.Gd()},1056544:f=>t.Ad(f),1056577:f=>t.Ed(f),1056609:(f,b,$)=>{t.ed(Number(f),Number(b),Number($),!0)},1056672:(f,b,$)=>{t.ed(Number(f),Number(b),Number($))},1056729:()=>typeof wasmOffsetConverter<"u",1056786:f=>{t.$b("Abs",f,void 0)},1056837:f=>{t.$b("Neg",f,void 0)},1056888:f=>{t.$b("Floor",f,void 0)},1056941:f=>{t.$b("Ceil",f,void 0)},1056993:f=>{t.$b("Reciprocal",f,void 0)},1057051:f=>{t.$b("Sqrt",f,void 0)},1057103:f=>{t.$b("Exp",f,void 0)},1057154:f=>{t.$b("Erf",f,void 0)},1057205:f=>{t.$b("Sigmoid",f,void 0)},1057260:(f,b,$)=>{t.$b("HardSigmoid",f,{alpha:b,beta:$})},1057339:f=>{t.$b("HardSwish",f,void 0)},1057396:f=>{t.$b("Log",f,void 0)},1057447:f=>{t.$b("Sin",f,void 0)},1057498:f=>{t.$b("Cos",f,void 0)},1057549:f=>{t.$b("Tan",f,void 0)},1057600:f=>{t.$b("Asin",f,void 0)},1057652:f=>{t.$b("Acos",f,void 0)},1057704:f=>{t.$b("Atan",f,void 0)},1057756:f=>{t.$b("Sinh",f,void 0)},1057808:f=>{t.$b("Cosh",f,void 0)},1057860:f=>{t.$b("Asinh",f,void 0)},1057913:f=>{t.$b("Acosh",f,void 0)},1057966:f=>{t.$b("Atanh",f,void 0)},1058019:f=>{t.$b("Tanh",f,void 0)},1058071:f=>{t.$b("Not",f,void 0)},1058122:(f,b,$)=>{t.$b("Clip",f,{min:b,max:$})},1058191:f=>{t.$b("Clip",f,void 0)},1058243:(f,b)=>{t.$b("Elu",f,{alpha:b})},1058301:f=>{t.$b("Gelu",f,void 0)},1058353:f=>{t.$b("Relu",f,void 0)},1058405:(f,b)=>{t.$b("LeakyRelu",f,{alpha:b})},1058469:(f,b)=>{t.$b("ThresholdedRelu",f,{alpha:b})},1058539:(f,b)=>{t.$b("Cast",f,{to:b})},1058597:f=>{t.$b("Add",f,void 0)},1058648:f=>{t.$b("Sub",f,void 0)},1058699:f=>{t.$b("Mul",f,void 0)},1058750:f=>{t.$b("Div",f,void 0)},1058801:f=>{t.$b("Pow",f,void 0)},1058852:f=>{t.$b("Equal",f,void 0)},1058905:f=>{t.$b("Greater",f,void 0)},1058960:f=>{t.$b("GreaterOrEqual",f,void 0)},1059022:f=>{t.$b("Less",f,void 0)},1059074:f=>{t.$b("LessOrEqual",f,void 0)},1059133:(f,b,$,x,E)=>{t.$b("ReduceMean",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059308:(f,b,$,x,E)=>{t.$b("ReduceMax",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059482:(f,b,$,x,E)=>{t.$b("ReduceMin",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059656:(f,b,$,x,E)=>{t.$b("ReduceProd",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059831:(f,b,$,x,E)=>{t.$b("ReduceSum",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060005:(f,b,$,x,E)=>{t.$b("ReduceL1",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060178:(f,b,$,x,E)=>{t.$b("ReduceL2",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060351:(f,b,$,x,E)=>{t.$b("ReduceLogSum",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060528:(f,b,$,x,E)=>{t.$b("ReduceSumSquare",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060708:(f,b,$,x,E)=>{t.$b("ReduceLogSumExp",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060888:f=>{t.$b("Where",f,void 0)},1060941:(f,b,$)=>{t.$b("Transpose",f,{perm:b?Array.from((I(),v).subarray(Number(b)>>>0,Number($)>>>0)):[]})},1061065:(f,b,$,x)=>{t.$b("DepthToSpace",f,{blocksize:b,mode:et($),format:x?"NHWC":"NCHW"})},1061198:(f,b,$,x)=>{t.$b("DepthToSpace",f,{blocksize:b,mode:et($),format:x?"NHWC":"NCHW"})},1061331:(f,b,$,x)=>{t.$b("DFT",f,{axis:b,inverse:$,onesided:x})},1061423:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe,gr)=>{t.$b("ConvTranspose",f,{format:Q?"NHWC":"NCHW",autoPad:b,dilations:[$],group:x,kernelShape:[E],pads:[k,M],strides:[U],wIsConst:()=>!!(I(),J)[re>>>0],outputPadding:ge?Array.from((I(),v).subarray(Number(ge)>>>0,Number(Oe)>>>0)):[],outputShape:Ne?Array.from((I(),v).subarray(Number(Ne)>>>0,Number(Fe)>>>0)):[],activation:et(gr)})},1061856:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe)=>{t.$b("ConvTranspose",f,{format:U?"NHWC":"NCHW",autoPad:b,dilations:Array.from((I(),v).subarray(Number($)>>>0,(Number($)>>>0)+2>>>0)),group:x,kernelShape:Array.from((I(),v).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((I(),v).subarray(Number(k)>>>0,(Number(k)>>>0)+4>>>0)),strides:Array.from((I(),v).subarray(Number(M)>>>0,(Number(M)>>>0)+2>>>0)),wIsConst:()=>!!(I(),J)[Q>>>0],outputPadding:re?Array.from((I(),v).subarray(Number(re)>>>0,Number(ge)>>>0)):[],outputShape:Oe?Array.from((I(),v).subarray(Number(Oe)>>>0,Number(Ne)>>>0)):[],activation:et(Fe)})},1062517:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe,gr)=>{t.$b("ConvTranspose",f,{format:Q?"NHWC":"NCHW",autoPad:b,dilations:[$],group:x,kernelShape:[E],pads:[k,M],strides:[U],wIsConst:()=>!!(I(),J)[re>>>0],outputPadding:ge?Array.from((I(),v).subarray(Number(ge)>>>0,Number(Oe)>>>0)):[],outputShape:Ne?Array.from((I(),v).subarray(Number(Ne)>>>0,Number(Fe)>>>0)):[],activation:et(gr)})},1062950:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe)=>{t.$b("ConvTranspose",f,{format:U?"NHWC":"NCHW",autoPad:b,dilations:Array.from((I(),v).subarray(Number($)>>>0,(Number($)>>>0)+2>>>0)),group:x,kernelShape:Array.from((I(),v).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((I(),v).subarray(Number(k)>>>0,(Number(k)>>>0)+4>>>0)),strides:Array.from((I(),v).subarray(Number(M)>>>0,(Number(M)>>>0)+2>>>0)),wIsConst:()=>!!(I(),J)[Q>>>0],outputPadding:re?Array.from((I(),v).subarray(Number(re)>>>0,Number(ge)>>>0)):[],outputShape:Oe?Array.from((I(),v).subarray(Number(Oe)>>>0,Number(Ne)>>>0)):[],activation:et(Fe)})},1063611:(f,b)=>{t.$b("GlobalAveragePool",f,{format:b?"NHWC":"NCHW"})},1063702:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe)=>{t.$b("AveragePool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:k?Array.from((I(),v).subarray(Number(k)>>>0,Number(M)>>>0)):[],kernel_shape:U?Array.from((I(),v).subarray(Number(U)>>>0,Number(Q)>>>0)):[],pads:re?Array.from((I(),v).subarray(Number(re)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),v).subarray(Number(Oe)>>>0,Number(Ne)>>>0)):[]})},1064181:(f,b)=>{t.$b("GlobalAveragePool",f,{format:b?"NHWC":"NCHW"})},1064272:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe)=>{t.$b("AveragePool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:k?Array.from((I(),v).subarray(Number(k)>>>0,Number(M)>>>0)):[],kernel_shape:U?Array.from((I(),v).subarray(Number(U)>>>0,Number(Q)>>>0)):[],pads:re?Array.from((I(),v).subarray(Number(re)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),v).subarray(Number(Oe)>>>0,Number(Ne)>>>0)):[]})},1064751:(f,b)=>{t.$b("GlobalMaxPool",f,{format:b?"NHWC":"NCHW"})},1064838:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe)=>{t.$b("MaxPool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:k?Array.from((I(),v).subarray(Number(k)>>>0,Number(M)>>>0)):[],kernel_shape:U?Array.from((I(),v).subarray(Number(U)>>>0,Number(Q)>>>0)):[],pads:re?Array.from((I(),v).subarray(Number(re)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),v).subarray(Number(Oe)>>>0,Number(Ne)>>>0)):[]})},1065313:(f,b)=>{t.$b("GlobalMaxPool",f,{format:b?"NHWC":"NCHW"})},1065400:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe)=>{t.$b("MaxPool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:k?Array.from((I(),v).subarray(Number(k)>>>0,Number(M)>>>0)):[],kernel_shape:U?Array.from((I(),v).subarray(Number(U)>>>0,Number(Q)>>>0)):[],pads:re?Array.from((I(),v).subarray(Number(re)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),v).subarray(Number(Oe)>>>0,Number(Ne)>>>0)):[]})},1065875:(f,b,$,x,E)=>{t.$b("Gemm",f,{alpha:b,beta:$,transA:x,transB:E})},1065979:f=>{t.$b("MatMul",f,void 0)},1066033:(f,b,$,x)=>{t.$b("ArgMax",f,{keepDims:!!b,selectLastIndex:!!$,axis:x})},1066141:(f,b,$,x)=>{t.$b("ArgMin",f,{keepDims:!!b,selectLastIndex:!!$,axis:x})},1066249:(f,b)=>{t.$b("Softmax",f,{axis:b})},1066312:(f,b)=>{t.$b("Concat",f,{axis:b})},1066372:(f,b,$,x,E)=>{t.$b("Split",f,{axis:b,numOutputs:$,splitSizes:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1066528:f=>{t.$b("Expand",f,void 0)},1066582:(f,b)=>{t.$b("Gather",f,{axis:Number(b)})},1066653:(f,b)=>{t.$b("GatherElements",f,{axis:Number(b)})},1066732:(f,b)=>{t.$b("GatherND",f,{batch_dims:Number(b)})},1066811:(f,b,$,x,E,k,M,U,Q,re,ge)=>{t.$b("Resize",f,{antialias:b,axes:$?Array.from((I(),v).subarray(Number($)>>>0,Number(x)>>>0)):[],coordinateTransformMode:et(E),cubicCoeffA:k,excludeOutside:M,extrapolationValue:U,keepAspectRatioPolicy:et(Q),mode:et(re),nearestMode:et(ge)})},1067173:(f,b,$,x,E,k,M)=>{t.$b("Slice",f,{starts:b?Array.from((I(),v).subarray(Number(b)>>>0,Number($)>>>0)):[],ends:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[],axes:k?Array.from((I(),v).subarray(Number(k)>>>0,Number(M)>>>0)):[]})},1067437:f=>{t.$b("Tile",f,void 0)},1067489:(f,b,$)=>{t.$b("InstanceNormalization",f,{epsilon:b,format:$?"NHWC":"NCHW"})},1067603:(f,b,$)=>{t.$b("InstanceNormalization",f,{epsilon:b,format:$?"NHWC":"NCHW"})},1067717:f=>{t.$b("Range",f,void 0)},1067770:(f,b)=>{t.$b("Einsum",f,{equation:et(b)})},1067851:(f,b,$,x,E)=>{t.$b("Pad",f,{mode:b,value:$,pads:x?Array.from((I(),v).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1067994:(f,b,$,x,E,k)=>{t.$b("BatchNormalization",f,{epsilon:b,momentum:$,spatial:!!E,trainingMode:!!x,format:k?"NHWC":"NCHW"})},1068163:(f,b,$,x,E,k)=>{t.$b("BatchNormalization",f,{epsilon:b,momentum:$,spatial:!!E,trainingMode:!!x,format:k?"NHWC":"NCHW"})},1068332:(f,b,$)=>{t.$b("CumSum",f,{exclusive:Number(b),reverse:Number($)})},1068429:(f,b,$)=>{t.$b("DequantizeLinear",f,{axis:b,blockSize:$})},1068519:(f,b,$,x,E)=>{t.$b("GridSample",f,{align_corners:b,mode:et($),padding_mode:et(x),format:E?"NHWC":"NCHW"})},1068689:(f,b,$,x,E)=>{t.$b("GridSample",f,{align_corners:b,mode:et($),padding_mode:et(x),format:E?"NHWC":"NCHW"})},1068859:(f,b)=>{t.$b("ScatterND",f,{reduction:et(b)})},1068944:(f,b,$,x,E,k,M,U,Q)=>{t.$b("Attention",f,{numHeads:b,isUnidirectional:$,maskFilterValue:x,scale:E,doRotary:k,qkvHiddenSizes:M?Array.from((I(),v).subarray(Number(U)>>>0,Number(U)+M>>>0)):[],pastPresentShareBuffer:!!Q})},1069216:f=>{t.$b("BiasAdd",f,void 0)},1069271:f=>{t.$b("BiasSplitGelu",f,void 0)},1069332:f=>{t.$b("FastGelu",f,void 0)},1069388:(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe,gr,oa)=>{t.$b("Conv",f,{format:Oe?"NHWC":"NCHW",auto_pad:b,dilations:$?Array.from((I(),v).subarray(Number($)>>>0,Number(x)>>>0)):[],group:E,kernel_shape:k?Array.from((I(),v).subarray(Number(k)>>>0,Number(M)>>>0)):[],pads:U?Array.from((I(),v).subarray(Number(U)>>>0,Number(Q)>>>0)):[],strides:re?Array.from((I(),v).subarray(Number(re)>>>0,Number(ge)>>>0)):[],w_is_const:()=>!!(I(),J)[Number(Ne)>>>0],activation:et(Fe),activation_params:gr?Array.from((I(),H).subarray(Number(gr)>>>0,Number(oa)>>>0)):[]})},1069972:f=>{t.$b("Gelu",f,void 0)},1070024:(f,b,$,x,E,k,M,U,Q)=>{t.$b("GroupQueryAttention",f,{numHeads:b,kvNumHeads:$,scale:x,softcap:E,doRotary:k,rotaryInterleaved:M,smoothSoftmax:U,localWindowSize:Q})},1070241:(f,b,$,x)=>{t.$b("LayerNormalization",f,{axis:b,epsilon:$,simplified:!!x})},1070352:(f,b,$,x)=>{t.$b("LayerNormalization",f,{axis:b,epsilon:$,simplified:!!x})},1070463:(f,b,$,x,E,k)=>{t.$b("MatMulNBits",f,{k:b,n:$,accuracyLevel:x,bits:E,blockSize:k})},1070590:(f,b,$,x,E,k)=>{t.$b("MultiHeadAttention",f,{numHeads:b,isUnidirectional:$,maskFilterValue:x,scale:E,doRotary:k})},1070749:(f,b)=>{t.$b("QuickGelu",f,{alpha:b})},1070813:(f,b,$,x,E)=>{t.$b("RotaryEmbedding",f,{interleaved:!!b,numHeads:$,rotaryEmbeddingDim:x,scale:E})},1070952:(f,b,$)=>{t.$b("SkipLayerNormalization",f,{epsilon:b,simplified:!!$})},1071054:(f,b,$)=>{t.$b("SkipLayerNormalization",f,{epsilon:b,simplified:!!$})},1071156:(f,b,$,x)=>{t.$b("GatherBlockQuantized",f,{gatherAxis:b,quantizeAxis:$,blockSize:x})},1071277:f=>{t.Fd(f)},1071311:(f,b)=>t.Hd(Number(f),Number(b),t.Xc.Kd,t.Xc.errors)};function AI(f,b,$){return Gc(async()=>{await t.Dd(Number(f),Number(b),Number($))})}function kI(){return typeof wasmOffsetConverter<"u"}function PI(f,b,$,x){var E=_e();try{return Op(f,b,$,x)}catch(k){if(we(E),k!==k+0)throw k;Te(1,0)}}function CI(f,b,$){var x=_e();try{return $p(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function RI(f){var b=_e();try{_p(f)}catch($){if(we(b),$!==$+0)throw $;Te(1,0)}}function DI(f,b){var $=_e();try{return na(f,b)}catch(x){if(we($),x!==x+0)throw x;Te(1,0)}}function MI(f,b,$){var x=_e();try{wp(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function NI(f,b){var $=_e();try{Ep(f,b)}catch(x){if(we($),x!==x+0)throw x;Te(1,0)}}function zI(f,b,$,x,E,k,M){var U=_e();try{return Sp(f,b,$,x,E,k,M)}catch(Q){if(we(U),Q!==Q+0)throw Q;Te(1,0)}}function BI(f,b,$,x,E,k){var M=_e();try{vp(f,b,$,x,E,k)}catch(U){if(we(M),U!==U+0)throw U;Te(1,0)}}function FI(f,b,$,x){var E=_e();try{Ip(f,b,$,x)}catch(k){if(we(E),k!==k+0)throw k;Te(1,0)}}function LI(f,b,$,x,E){var k=_e();try{xp(f,b,$,x,E)}catch(M){if(we(k),M!==M+0)throw M;Te(1,0)}}function jI(f,b,$,x,E,k,M){var U=_e();try{kp(f,b,$,x,E,k,M)}catch(Q){if(we(U),Q!==Q+0)throw Q;Te(1,0)}}function UI(f,b,$,x,E,k,M){var U=_e();try{Pp(f,b,$,x,E,k,M)}catch(Q){if(we(U),Q!==Q+0)throw Q;Te(1,0)}}function VI(f,b,$,x,E,k,M,U){var Q=_e();try{Mp(f,b,$,x,E,k,M,U)}catch(re){if(we(Q),re!==re+0)throw re;Te(1,0)}}function qI(f,b,$,x,E){var k=_e();try{return Ap(f,b,$,x,E)}catch(M){if(we(k),M!==M+0)throw M;Te(1,0)}}function GI(f,b,$){var x=_e();try{return Np(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function HI(f,b,$,x,E,k,M,U){var Q=_e();try{zp(f,b,$,x,E,k,M,U)}catch(re){if(we(Q),re!==re+0)throw re;Te(1,0)}}function WI(f,b,$,x,E,k,M,U,Q,re,ge,Oe){var Ne=_e();try{Cp(f,b,$,x,E,k,M,U,Q,re,ge,Oe)}catch(Fe){if(we(Ne),Fe!==Fe+0)throw Fe;Te(1,0)}}function KI(f,b,$){var x=_e();try{return Bp(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;return Te(1,0),0n}}function XI(f,b,$,x,E,k,M,U,Q){var re=_e();try{Tp(f,b,$,x,E,k,M,U,Q)}catch(ge){if(we(re),ge!==ge+0)throw ge;Te(1,0)}}function ZI(f){var b=_e();try{return Fp(f)}catch($){if(we(b),$!==$+0)throw $;Te(1,0)}}function YI(f,b){var $=_e();try{return eh(f,b)}catch(x){if(we($),x!==x+0)throw x;return Te(1,0),0n}}function JI(f){var b=_e();try{return Lp(f)}catch($){if(we(b),$!==$+0)throw $;return Te(1,0),0n}}function QI(f,b,$,x){var E=_e();try{return Hp(f,b,$,x)}catch(k){if(we(E),k!==k+0)throw k;Te(1,0)}}function e3(f,b,$,x,E){var k=_e();try{return Wp(f,b,$,x,E)}catch(M){if(we(k),M!==M+0)throw M;Te(1,0)}}function t3(f,b,$,x,E,k){var M=_e();try{return Kp(f,b,$,x,E,k)}catch(U){if(we(M),U!==U+0)throw U;Te(1,0)}}function r3(f,b,$,x,E,k){var M=_e();try{return Rp(f,b,$,x,E,k)}catch(U){if(we(M),U!==U+0)throw U;Te(1,0)}}function n3(f,b,$,x,E,k){var M=_e();try{return Xp(f,b,$,x,E,k)}catch(U){if(we(M),U!==U+0)throw U;Te(1,0)}}function i3(f,b,$,x,E,k,M,U){var Q=_e();try{return Dp(f,b,$,x,E,k,M,U)}catch(re){if(we(Q),re!==re+0)throw re;Te(1,0)}}function o3(f,b,$,x,E){var k=_e();try{return Zp(f,b,$,x,E)}catch(M){if(we(k),M!==M+0)throw M;return Te(1,0),0n}}function s3(f,b,$,x){var E=_e();try{return Yp(f,b,$,x)}catch(k){if(we(E),k!==k+0)throw k;Te(1,0)}}function a3(f,b,$,x){var E=_e();try{return Jp(f,b,$,x)}catch(k){if(we(E),k!==k+0)throw k;Te(1,0)}}function u3(f,b,$,x,E,k,M,U,Q,re,ge,Oe){var Ne=_e();try{return Qp(f,b,$,x,E,k,M,U,Q,re,ge,Oe)}catch(Fe){if(we(Ne),Fe!==Fe+0)throw Fe;Te(1,0)}}function l3(f,b,$,x,E,k,M,U,Q,re,ge){var Oe=_e();try{qp(f,b,$,x,E,k,M,U,Q,re,ge)}catch(Ne){if(we(Oe),Ne!==Ne+0)throw Ne;Te(1,0)}}function d3(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe,gr,oa){var f3=_e();try{Gp(f,b,$,x,E,k,M,U,Q,re,ge,Oe,Ne,Fe,gr,oa)}catch(sa){if(we(f3),sa!==sa+0)throw sa;Te(1,0)}}function c3(f,b,$){var x=_e();try{return jp(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function p3(f,b,$){var x=_e();try{return Up(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function h3(f,b,$,x){var E=_e();try{Vp(f,b,$,x)}catch(k){if(we(E),k!==k+0)throw k;Te(1,0)}}function bo(){if(0{g=f,_=b})}var px,Sm,HA=D(()=>{"use strict";px=Tm,Sm=globalThis.self?.name?.startsWith("em-pthread"),Sm&&Tm()}),Ua,Al,Im,yt,hx,$o,Om,Em,Va,Am,qa,fx,Ga,mx,rd=D(()=>{"use strict";td(),Ua=typeof location>"u"?void 0:location.origin,Al=import.meta.url>"file:"&&import.meta.url<"file;",Im=()=>{if(Al){let e=URL;return new URL(new e("ort.all.bundle.min.mjs",import.meta.url).href,Ua).href}return import.meta.url},yt=Im(),hx=()=>{if(yt&&!yt.startsWith("blob:"))return yt.substring(0,yt.lastIndexOf("/")+1)},$o=(e,t)=>{try{let n=t??yt;return(n?new URL(e,n):new URL(e)).origin===Ua}catch{return!1}},Om=(e,t)=>{let n=t??yt;try{return(n?new URL(e,n):new URL(e)).href}catch{return}},Em=(e,t)=>`${t??"./"}${e}`,Va=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},Am=async e=>(await import(e)).default,qa=(GA(),Nn(lx)).default,fx=async()=>{if(!yt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if($o(yt))return[void 0,qa()];let e=await Va(yt);return[e,qa(e)]},Ga=(HA(),Nn(cx)).default,mx=async(e,t,n,i)=>{let u=Ga&&!(e||t);if(u)if(yt)u=$o(yt)||i&&!n;else if(i&&!n)u=!0;else throw new Error("cannot determine the script source URL.");if(u)return[void 0,Ga];{let l="ort-wasm-simd-threaded.jsep.mjs",d=e??Om(l,t),c=n&&d&&!$o(d,t),s=c?await Va(d):d??Em(l,t);return[c?s:void 0,await Am(s)]}}}),Ha,To,li,Wa,km,Pm,Cm,nd,Be,vn=D(()=>{"use strict";rd(),To=!1,li=!1,Wa=!1,km=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Pm=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Cm=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},nd=async e=>{if(To)return Promise.resolve();if(li)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Wa)throw new Error("previous call to 'initializeWebAssembly()' failed.");li=!0;let t=e.initTimeout,n=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!Cm())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!Pm())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let i=km();n>1&&!i&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+n+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=n=1);let u=e.wasmPaths,l=typeof u=="string"?u:void 0,d=u?.mjs,c=d?.href??d,s=u?.wasm,r=s?.href??s,o=e.wasmBinary,[a,p]=await mx(c,l,n>1,!!o||!!r),h=!1,m=[];if(t>0&&m.push(new Promise(g=>{setTimeout(()=>{h=!0,g()},t)})),m.push(new Promise((g,_)=>{let w={numThreads:n};if(o)w.wasmBinary=o,w.locateFile=y=>y;else if(r||l)w.locateFile=y=>r??l+y;else if(c&&c.indexOf("blob:")!==0)w.locateFile=y=>new URL(y,c).href;else if(a){let y=hx();y&&(w.locateFile=T=>y+T)}p(w).then(y=>{li=!1,To=!0,Ha=y,g(),a&&URL.revokeObjectURL(a)},y=>{li=!1,Wa=!0,_(y)})})),await Promise.race(m),h)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},Be=()=>{if(To&&Ha)return Ha;throw new Error("WebAssembly is not initialized yet.")}}),Ut,Go,Re,id=D(()=>{"use strict";vn(),Ut=(e,t)=>{let n=Be(),i=n.lengthBytesUTF8(e)+1,u=n._malloc(i);return n.stringToUTF8(e,u,i),t.push(u),u},Go=(e,t,n,i)=>{if(typeof e=="object"&&e!==null){if(n.has(e))throw new Error("Circular reference in options");n.add(e)}Object.entries(e).forEach(([u,l])=>{let d=t?t+u:u;if(typeof l=="object")Go(l,d+".",n,i);else if(typeof l=="string"||typeof l=="number")i(d,l.toString());else if(typeof l=="boolean")i(d,l?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof l}`)})},Re=e=>{let t=Be(),n=t.stackSave();try{let i=t.PTR_SIZE,u=t.stackAlloc(2*i);t._OrtGetLastError(u,u+i);let l=Number(t.getValue(u,i===4?"i32":"i64")),d=t.getValue(u+i,"*"),c=d?t.UTF8ToString(d):"";throw new Error(`${e} ERROR_CODE: ${l}, ERROR_MESSAGE: ${c}`)}finally{t.stackRestore(n)}}}),gx,WA=D(()=>{"use strict";vn(),id(),gx=e=>{let t=Be(),n=0,i=[],u=e||{};try{if(e?.logSeverityLevel===void 0)u.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)u.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(u.terminate=!1);let l=0;return e?.tag!==void 0&&(l=Ut(e.tag,i)),n=t._OrtCreateRunOptions(u.logSeverityLevel,u.logVerbosityLevel,!!u.terminate,l),n===0&&Re("Can't create run options."),e?.extra!==void 0&&Go(e.extra,"",new WeakSet,(d,c)=>{let s=Ut(d,i),r=Ut(c,i);t._OrtAddRunConfigEntry(n,s,r)!==0&&Re(`Can't set a run config entry: ${d} - ${c}.`)}),[n,i]}catch(l){throw n!==0&&t._OrtReleaseRunOptions(n),i.forEach(d=>t._free(d)),l}}}),Rm,Dm,Mm,qr,Nm,bx,KA=D(()=>{"use strict";vn(),id(),Rm=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},Dm=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},Mm=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(n=>(typeof n=="string"?n:n.name)==="webgpu")&&(e.enableMemPattern=!1)},qr=(e,t,n,i)=>{let u=Ut(t,i),l=Ut(n,i);Be()._OrtAddSessionConfigEntry(e,u,l)!==0&&Re(`Can't set a session config entry: ${t} - ${n}.`)},Nm=async(e,t,n)=>{let i=t.executionProviders;for(let u of i){let l=typeof u=="string"?u:u.name,d=[];switch(l){case"webnn":if(l="WEBNN",qr(e,"session.disable_quant_qdq","1",n),qr(e,"session.disable_qdq_constant_folding","1",n),typeof u!="string"){let a=u?.deviceType;a&&qr(e,"deviceType",a,n)}break;case"webgpu":if(l="JS",typeof u!="string"){let a=u;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);qr(e,"preferredLayout",a.preferredLayout,n)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${l}`)}let c=Ut(l,n),s=d.length,r=0,o=0;if(s>0){r=Be()._malloc(s*Be().PTR_SIZE),n.push(r),o=Be()._malloc(s*Be().PTR_SIZE),n.push(o);for(let a=0;a{let t=Be(),n=0,i=[],u=e||{};Mm(u);try{let l=Rm(u.graphOptimizationLevel??"all"),d=Dm(u.executionMode??"sequential"),c=typeof u.logId=="string"?Ut(u.logId,i):0,s=u.logSeverityLevel??2;if(!Number.isInteger(s)||s<0||s>4)throw new Error(`log severity level is not valid: ${s}`);let r=u.logVerbosityLevel??0;if(!Number.isInteger(r)||r<0||r>4)throw new Error(`log verbosity level is not valid: ${r}`);let o=typeof u.optimizedModelFilePath=="string"?Ut(u.optimizedModelFilePath,i):0;if(n=t._OrtCreateSessionOptions(l,!!u.enableCpuMemArena,!!u.enableMemPattern,d,!!u.enableProfiling,0,c,s,r,o),n===0&&Re("Can't create session options."),u.executionProviders&&await Nm(n,u,i),u.enableGraphCapture!==void 0){if(typeof u.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${u.enableGraphCapture}`);qr(n,"enableGraphCapture",u.enableGraphCapture.toString(),i)}if(u.freeDimensionOverrides)for(let[a,p]of Object.entries(u.freeDimensionOverrides)){if(typeof a!="string")throw new Error(`free dimension override name must be a string: ${a}`);if(typeof p!="number"||!Number.isInteger(p)||p<0)throw new Error(`free dimension override value must be a non-negative integer: ${p}`);let h=Ut(a,i);t._OrtAddFreeDimensionOverride(n,h,p)!==0&&Re(`Can't set a free dimension override: ${a} - ${p}.`)}return u.extra!==void 0&&Go(u.extra,"",new WeakSet,(a,p)=>{qr(n,a,p,i)}),[n,i]}catch(l){throw n!==0&&t._OrtReleaseSessionOptions(n)!==0&&Re("Can't release session options."),i.forEach(d=>t._free(d)),l}}}),dn,sr,cn,ss,Ho,od,sd,kl,de=D(()=>{"use strict";dn=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},sr=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},cn=(e,t)=>{let n=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],i=typeof t=="number"?t:t.reduce((u,l)=>u*l,1);return n>0?Math.ceil(i*n):void 0},ss=e=>{switch(e){case"float16":return typeof Float16Array<"u"?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Ho=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},od=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",sd=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",kl=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),ad,yx=D(()=>{"use strict";td(),ad=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let n=t.headers.get("Content-Length"),i=n?parseInt(n,10):0;if(i<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let u=t.body.getReader(),l;try{l=new ArrayBuffer(i)}catch(c){if(c instanceof RangeError){let s=Math.ceil(i/65536);l=new WebAssembly.Memory({initial:s,maximum:s}).buffer}else throw c}let d=0;for(;;){let{done:c,value:s}=await u.read();if(c)break;let r=s.byteLength;new Uint8Array(l,d,r).set(s),d+=r}return new Uint8Array(l,0,i)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),zm,Bm,Fm,Lm,ud,jm,Ie,ur=D(()=>{"use strict";de(),zm=["V","I","W","E","F"],Bm=(e,t)=>{console.log(`[${zm[e]},${new Date().toISOString()}]${t}`)},ud=(e,t)=>{Fm=e,Lm=t},jm=(e,t)=>{let n=Ho(e),i=Ho(Fm);n>=i&&Bm(n,typeof t=="function"?t():t)},Ie=(...e)=>{Lm&&jm(...e)}}),Um,Bn,F,Wo,wx,_x,vx,pe=D(()=>{"use strict";Um=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},Bn=class{static calcShape(e,t,n=!1){let i=e.length,u=t.length;if(i===0)return t;if(u===0)return e;let l=Math.max(e.length,t.length),d=new Array(l);if(n){if(i<2||u<2)return;let c=Um.calcMatMulShape([e[i-2],e[i-1]],[t[u-2],t[u-1]]);if(c===void 0)return;[d[l-2],d[l-1]]=c}for(let c=n?3:1;c<=l;c++){let s=i-c<0?1:e[i-c],r=u-c<0?1:t[u-c];if(s!==r&&s>1&&r>1)return;let o=Math.max(s,r);if(s&&r)d[l-c]=Math.max(s,r);else{if(o>1)return;d[l-c]=0}}return d}static isValidBroadcast(e,t){let n=e.length,i=t.length;if(n>i)return!1;for(let u=1;u<=n;u++)if(e[n-u]!==1&&e[n-u]!==t[i-u])return!1;return!0}},F=class Bo{static size(t){return Bo.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,n=4){let i=t.length;if(i===0)return[];let u=new Array(i),l=i-1;for(;l>=0;){if(t[l]%n===0){u[l]=t[l]/n;break}if(n%t[l]!==0)throw new Error("cannot convert shape");u[l]=1,n/=t[l],l--}for(l--;l>=0;l--)u[l]=t[l];return u}static sizeFromDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return Bo.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return Bo.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,i){let u=1;for(let l=n;l=0;--u)i[u]=i[u+1]*t[u+1];return i}static normalizeAxis(t,n){if(t<-n&&t>=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(i=>this.normalizeAxis(i,n??t.length))}static sortBasedOnPerm(t,n){return n?n.map(i=>t[i]):t.slice().reverse()}static padShape(t,n){let i=t.length;return t.map((u,l)=>u+n[l]+n[l+i])}static areEqual(t,n){return t.length!==n.length?!1:t.every((i,u)=>i===n[u])}},Wo=class Er{static adjustPoolAttributes(t,n,i,u,l,d){if(!t&&i.length!==n.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let c=0;c=i.length?i.push(n[c+2]):i[c]=n[c+2];for(let c=0;c=i[c]||d[c+i.length]>=i[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,i,u,l,d,c){if(c){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let s=0;s=i+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,i,u,l,d,c,s,r=0){let o=i*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,Er.computeOutputSize(t-o,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(i!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],Er.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return Er.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}},wx=class{static getShapeOfGemmResult(e,t,n,i,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(i?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!Bn.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},_x=-34028234663852886e22,vx=34028234663852886e22}),ld,xx=D(()=>{"use strict";de(),ld=(e,t)=>new(ss(t))(e)}),Ka,Vm,Xa,qm,Za,Gm,Ya,Ja,Qa,Hm,$x,XA=D(()=>{"use strict";de(),ur(),Ka=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),Vm=(e,t)=>{if(t==="int32")return e;let n=Ka.get(t);if(!n)throw new Error(`WebNN backend does not support data type: ${t}`);let i=n/8;if(e.byteLength%i!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${i}.`);let u=e.byteLength/i,l=new(ss(t))(e.buffer,e.byteOffset,u);switch(t){case"int64":case"uint64":{let d=new Int32Array(u);for(let c=0;c2147483647n||s<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");d[c]=Number(s)}return new Uint8Array(d.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&l.some(c=>c>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let d=Int32Array.from(l,Number);return new Uint8Array(d.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},Xa=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let n=e.byteLength/4,i=new Int32Array(e.buffer,e.byteOffset,n);switch(t){case"int64":{let u=BigInt64Array.from(i,BigInt);return new Uint8Array(u.buffer)}case"uint64":{if(i.some(l=>l<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let u=BigUint64Array.from(i,BigInt);return new Uint8Array(u.buffer)}case"int8":{if(i.some(l=>l<-128||l>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let u=Int8Array.from(i,Number);return new Uint8Array(u.buffer)}case"uint8":{if(i.some(u=>u<0||u>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(i,Number)}case"uint32":{if(i.some(l=>l<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let u=Uint32Array.from(i,Number);return new Uint8Array(u.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},qm=1,Za=()=>qm++,Gm=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),Ya=(e,t)=>{let n=Ka.get(e);if(!n)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((i,u)=>i*u)*n/8):0},Ja=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:n,tensor:i,dataType:u,shape:l,fallbackDataType:d}=e;this.sessionId=t,this.mlContext=n,this.mlTensor=i,this.dataType=u,this.tensorShape=l,this.fallbackDataType=d}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return Ya(this.dataType,this.tensorShape)}destroy(){Ie("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),n=Xa(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(n);return}else return new Uint8Array(n).buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,n){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===n.length&&this.tensorShape.every((i,u)=>i===n[u])}setIsDataConverted(e){this.isDataConverted=e}},Qa=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(e,t,n,i){let u=this.tensorManager.getMLContext(e),l=this.tensorManager.getMLOpSupportLimits(e),d;if(!l?.input.dataTypes.includes(t)){if(d=Gm.get(t),!d||l?.input.dataTypes.includes(d))throw new Error(`WebNN backend does not support data type: ${t}`);Ie("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${d}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(u,t,n))return this.wrapper.tensor;if(i){if(this.wrapper.byteLength!==Ya(t,n))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let c=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,n,c,!0,!0,d),i&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=Vm(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else Ie("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){if(this.activeUpload){let t=this.wrapper?.isDataConverted?Xa(this.activeUpload,this.wrapper?.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(t):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(t);return}else return t.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},Hm=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=Za();return this.tensorTrackersById.set(e,new Qa(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,n,i,u){Ie("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${n}, shape: ${i}, copyOld: ${u}}`);let l=this.tensorTrackersById.get(t);if(!l)throw new Error("Tensor not found.");return l.ensureTensor(e,n,i,u)}upload(e,t){let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");n.upload(t)}async download(e,t){Ie("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t?.byteLength}}`);let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");return n.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}registerTensor(e,t,n,i){let u=this.getMLContext(e),l=Za(),d=new Ja({sessionId:e,context:u,tensor:t,dataType:n,shape:i});return this.tensorTrackersById.set(l,new Qa(this,d)),this.externalTensors.add(d),l}async getCachedTensor(e,t,n,i,u,l,d){let c=this.getMLContext(e);for(let[r,o]of this.freeTensors.entries())if(o.canReuseTensor(c,t,n)){Ie("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}`);let a=this.freeTensors.splice(r,1)[0];return a.sessionId=e,a}Ie("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}}`);let s=await c.createTensor({dataType:d??t,shape:n,dimensions:n,usage:i,writable:u,readable:l});return new Ja({sessionId:e,context:c,tensor:s,dataType:t,shape:n,fallbackDataType:d})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},$x=(...e)=>new Hm(...e)}),di,Wm,Tx,ZA=D(()=>{"use strict";de(),vn(),xx(),XA(),ur(),di=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),Wm=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let n=Object.keys(e).sort(),i=Object.keys(t).sort();return n.length===i.length&&n.every((u,l)=>u===i[l]&&e[u]===t[u])},Tx=class{constructor(e){this.tensorManager=$x(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,ud(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){Ie("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){Ie("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let n of t)Ie("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${n}}`),this.tensorManager.releaseTensorId(n);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let n=this.mlContextCache.findIndex(i=>i.gpuDevice===e);if(n!==-1)return this.mlContextCache[n].mlContext;{let i=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:i}),i}}else if(e===void 0){let n=this.mlContextCache.findIndex(i=>i.options===void 0&&i.gpuDevice===void 0);if(n!==-1)return this.mlContextCache[n].mlContext;{let i=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:i}),i}}let t=this.mlContextCache.findIndex(n=>Wm(n.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let n=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:n}),n}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let n=this.sessionIdsByMLContext.get(t);n||(n=new Set,this.sessionIdsByMLContext.set(t,n)),n.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let n=this.sessionIdsByMLContext.get(t);if(n.delete(e),n.size===0){this.sessionIdsByMLContext.delete(t);let i=this.mlContextCache.findIndex(u=>u.mlContext===t);i!==-1&&this.mlContextCache.splice(i,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){Ie("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,n,i,u){let l=di.get(n);if(!l)throw new Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,l,i,u)}async createTemporaryTensor(e,t,n){Ie("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${n}}`);let i=di.get(t);if(!i)throw new Error(`Unsupported ONNX data type: ${t}`);let u=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,u,i,n,!1);let l=this.temporarySessionTensorIds.get(e);return l?l.push(u):this.temporarySessionTensorIds.set(e,[u]),u}uploadTensor(e,t){if(!Be().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");Ie("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let n=await this.tensorManager.download(e);return ld(n,t)}}registerMLTensor(e,t,n,i){let u=di.get(n);if(!u)throw new Error(`Unsupported ONNX data type: ${n}`);let l=this.tensorManager.registerTensor(e,t,u,i);return Ie("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${u}, dimensions: ${i}} -> {tensorId: ${l}}`),l}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let n=this.sessionGraphInputs.get(e);return n?n.includes(t):!1}isGraphOutput(e,t){let n=this.sessionGraphOutputs.get(e);return n?n.includes(t):!1}isGraphInputOutputTypeSupported(e,t,n=!0){let i=di.get(dn(t)),u=this.mlOpSupportLimitsBySessionId.get(e);return typeof i>"u"?!1:n?!!u?.input.dataTypes.includes(i):!!u?.output.dataTypes.includes(i)}flush(){}}}),dd=D(()=>{"use strict"}),eu,So,Io,Km,Xm,tu,Pl,Zm,Sx,YA=D(()=>{"use strict";ur(),dd(),eu=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),So=[],Io=e=>Math.ceil(Number(e)/16)*16,Km=e=>{for(let t=0;tXm++,Pl=async(e,t,n,i)=>{let u=Io(n),l=e.device.createBuffer({size:u,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let d=e.getCommandEncoder();e.endComputePass(),d.copyBufferToBuffer(t,0,l,0,u),e.flush(),await l.mapAsync(GPUMapMode.READ);let c=l.getMappedRange();if(i){let s=i();return s.set(new Uint8Array(c,0,n)),s}else return new Uint8Array(c.slice(0,n))}finally{l.destroy()}},Zm=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of eu)So.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let n=t.buffer,i=t.byteOffset,u=t.byteLength,l=Io(u),d=this.storageCache.get(e);if(!d)throw new Error("gpu data for uploading does not exist");if(Number(d.originalSize)!==u)throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${u}`);if(l===u&&i%4===0)this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,n,i,u);else{let c=new Uint8Array(l);c.set(t),this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,c,0,l)}Ie("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let n=this.storageCache.get(e);if(!n)throw new Error("source gpu data for memcpy does not exist");let i=this.storageCache.get(t);if(!i)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==i.originalSize)throw new Error("inconsistent source and destination gpu data size");let u=Io(n.originalSize),l=this.backend.getCommandEncoder();this.backend.endComputePass(),l.copyBufferToBuffer(n.gpuData.buffer,0,i.gpuData.buffer,0,u)}registerExternalBuffer(e,t,n){let i;if(n){if(i=n[0],e===n[1])return Ie("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, buffer is the same, skip.`),i;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. + Please use the previous external buffer!`)}else i=tu();return this.storageCache.set(i,{gpuData:{id:i,type:0,buffer:e},originalSize:t}),Ie("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, registered.`),i}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),Ie("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=Km(e),i,u=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,l=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(u||l){let c=(u?this.freeBuffers:this.freeUniformBuffers).get(n);c?c.length>0?i=c.pop():i=this.backend.device.createBuffer({size:n,usage:t}):i=this.backend.device.createBuffer({size:n,usage:t})}else i=this.backend.device.createBuffer({size:n,usage:t});let d={id:tu(),type:0,buffer:i};return this.storageCache.set(d.id,{gpuData:d,originalSize:Number(e)}),Ie("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${d.id}`),d}get(e){return this.storageCache.get(e)?.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,n=this.storageCache.get(t);if(!n){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return Ie("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${n.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(n.gpuData.buffer),n.originalSize}async download(e,t){let n=this.storageCache.get(Number(e));if(!n)throw new Error("data does not exist");await Pl(this.backend,n.gpuData.buffer,n.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=eu.get(e.size);if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(Ie("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(n=>{n.gpuData.buffer.destroy()}),this.storageCache=new Map)}},Sx=(...e)=>new Zm(...e)}),Ym,ke,He=D(()=>{"use strict";Ym=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},ke=e=>new Ym(e)}),Fn,Oo,nt,rt,ue,Ge,Cl,Mn,kr,se,ci,j,oe,Ix,cd,Jm,Ox,he=D(()=>{"use strict";de(),pe(),Fn=64,Oo=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},nt=(e,t=1)=>{let n=Oo(e,t);return typeof n=="string"?n:n[0]},rt=(e,t=1)=>{let n=Oo(e,t);return typeof n=="string"?n:n[1]},ue=(...e)=>{let t=[];return e.forEach(n=>{n.length!==0&&t.push({type:12,data:n},{type:12,data:F.computeStrides(n)})}),t},Ge=e=>e%4===0?4:e%2===0?2:1,Cl=(e="f32",t,n="0")=>!t||t===1?`${e}(${n})`:`vec${t}<${e}>(${n})`,Mn=(e,t,n)=>e==="f32"?n:t===1?`f32(${n})`:`vec${t}(${n})`,kr=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,se=(e,t,n,i)=>e.startsWith("uniforms.")&&n>4?typeof t=="string"?i==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:i==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:n>1?`${e}[${t}]`:e,ci=(e,t,n,i,u)=>{let l=typeof n=="number",d=l?n:n.length,c=[...new Array(d).keys()],s=d<2?"u32":d<=4?`vec${d}`:`array`,r=Oo(t,u),o=typeof r=="string"?r:r[1],a=typeof r=="string"?r:r[0],p={indices:s,value:o,storage:a,tensor:t},h=B=>typeof B=="string"?B:`${B}u`,m={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},g=l?"uniforms.":"",_=`${g}${e}_shape`,w=`${g}${e}_strides`,y="";for(let B=0;B(m.offsetToIndices=!0,d<2?B:`o2i_${e}(${B})`),O=[];if(d>=2)for(let B=d-1;B>=0;B--)O.push(`${ie(_,B,d)} * (indices[${B}])`);let A=d<2?"":` + }`,S=B=>(m.offsetToIndices=!0,d<2?B:`o2i_${e}(${B})`),O=[];if(d>=2)for(let B=d-1;B>=0;B--)O.push(`${se(w,B,d)} * (indices[${B}])`);let A=d<2?"":` fn i2o_${e}(indices: ${p.indices}) -> u32 { return ${O.join("+")}; - }`,P=B=>(m.indicesToOffset=!0,d<2?B:`i2o_${e}(${B})`),I=(...B)=>d===0?"0u":`${p.indices}(${B.map(h).join(",")})`,L=(B,X)=>d<2?`${B}`:`${ie(B,X,d)}`,V=(B,X,K)=>d<2?`${B}=${K};`:`${ie(B,X,d)}=${K};`,Z={},q=(B,X)=>{m.broadcastedIndicesToOffset=!0;let K=`${X.name}broadcastedIndicesTo${e}Offset`;if(K in Z)return`${K}(${B})`;let M=[];for(let ne=d-1;ne>=0;ne--){let Q=X.indicesGet("outputIndices",ne+X.rank-d);M.push(`${L(_,ne)} * (${Q} % ${L($,ne)})`)}return Z[K]=`fn ${K}(outputIndices: ${X.type.indices}) -> u32 { - return ${M.length>0?M.join("+"):"0u"}; - }`,`${K}(${B})`},N=(B,X)=>(()=>{if(p.storage===p.value)return`${e}[${B}]=${X};`;if(p.storage==="vec2"&&p.value==="i32")return`${e}[${B}]=vec2(u32(${X}), select(0u, 0xFFFFFFFFu, ${X} < 0));`;if(p.storage==="vec2"&&p.value==="u32")return`${e}[${B}]=vec2(u32(${X}), 0u);`;if(p.storage==="u32"&&p.value==="vec4")return`${e}[${B}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${X}));`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),ue=B=>(()=>{if(p.storage===p.value)return`${e}[${B}]`;if(p.storage==="vec2"&&p.value==="i32")return`i32(${e}[${B}].x)`;if(p.storage==="vec2"&&p.value==="u32")return`u32(${e}[${B}].x)`;if(p.storage==="u32"&&p.value==="vec4")return`vec4(bool(${e}[${B}] & 0xFFu), bool(${e}[${B}] & 0xFF00u), bool(${e}[${B}] & 0xFF0000u), bool(${e}[${B}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),w=d<2?"":` - fn get_${e}ByIndices(indices: ${p.indices}) -> ${i} { - return ${ue(`i2o_${e}(indices)`)}; - }`,C=d<2?"":(()=>{let B=c.map(K=>`d${K}: u32`).join(", "),X=c.map(K=>`d${K}`).join(", ");return` - fn get_${e}(${B}) -> ${i} { - return get_${e}ByIndices(${I(X)}); - }`})(),U=(...B)=>{if(B.length!==d)throw new Error(`indices length must be ${d}`);let X=B.map(h).join(",");return d===0?ue("0u"):d===1?ue(X[0]):(m.get=!0,m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}(${X})`)},H=B=>d<2?ue(B):(m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}ByIndices(${B})`),G=d<2?"":` - fn set_${e}ByIndices(indices: ${p.indices}, value: ${i}) { - ${N(`i2o_${e}(indices)`,"value")} - }`,J=d<2?"":(()=>{let B=c.map(K=>`d${K}: u32`).join(", "),X=c.map(K=>`d${K}`).join(", ");return` - fn set_${e}(${B}, value: ${i}) { - set_${e}ByIndices(${I(X)}, value); - }`})();return{impl:()=>{let B=[],X=!1;return m.offsetToIndices&&(B.push(T),X=!0),m.indicesToOffset&&(B.push(A),X=!0),m.broadcastedIndicesToOffset&&(Object.values(Z).forEach(K=>B.push(K)),X=!0),m.set&&(B.push(J),X=!0),m.setByIndices&&(B.push(G),X=!0),m.get&&(B.push(C),X=!0),m.getByIndices&&(B.push(w),X=!0),!l&&X&&B.unshift(`const ${$} = ${p.indices}(${n.join(",")});`,`const ${_} = ${p.indices}(${z.computeStrides(n).join(",")});`),B.join(` -`)},type:p,offsetToIndices:S,indicesToOffset:P,broadcastedIndicesToOffset:q,indices:I,indicesGet:L,indicesSet:V,set:(...B)=>{if(B.length!==d+1)throw new Error(`indices length must be ${d}`);let X=B[d];if(typeof X!="string")throw new Error("value must be string");let K=B.slice(0,d).map(h).join(",");return d===0?N("0u",X):d===1?N(K[0],X):(m.set=!0,m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}(${K}, ${X})`)},setByOffset:N,setByIndices:(B,X)=>d<2?N(B,X):(m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}ByIndices(${B}, ${X});`),get:U,getByOffset:ue,getByIndices:H,usage:o,name:e,strides:_,shape:$,rank:d}},F=(e,t,n,o=1)=>Kn(e,t,n,"input",o),re=(e,t,n,o=1)=>Kn(e,t,n,"output",o),qv=(e,t,n)=>Kn(e,t,n,"atomicOutput",1),Bl=(e,t,n,o=1)=>Kn(e,t,n,"internal",o),gm=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=$n){let t=typeof e=="number"?e:e[0],n=typeof e=="number"?1:e[1],o=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*n*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let u=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,l=u?`@builtin(global_invocation_id) global_id : vec3, + }`,P=B=>(m.indicesToOffset=!0,d<2?B:`i2o_${e}(${B})`),I=(...B)=>d===0?"0u":`${p.indices}(${B.map(h).join(",")})`,z=(B,te)=>d<2?`${B}`:`${se(B,te,d)}`,G=(B,te,Y)=>d<2?`${B}=${Y};`:`${se(B,te,d)}=${Y};`,J={},q=(B,te)=>{m.broadcastedIndicesToOffset=!0;let Y=`${te.name}broadcastedIndicesTo${e}Offset`;if(Y in J)return`${Y}(${B})`;let N=[];for(let X=d-1;X>=0;X--){let W=te.indicesGet("outputIndices",X+te.rank-d);N.push(`${z(w,X)} * (${W} % ${z(_,X)})`)}return J[Y]=`fn ${Y}(outputIndices: ${te.type.indices}) -> u32 { + return ${N.length>0?N.join("+"):"0u"}; + }`,`${Y}(${B})`},C=(B,te)=>(()=>{if(p.storage===p.value)return`${e}[${B}]=${te};`;if(p.storage==="vec2"&&p.value==="i32")return`${e}[${B}]=vec2(u32(${te}), select(0u, 0xFFFFFFFFu, ${te} < 0));`;if(p.storage==="vec2"&&p.value==="u32")return`${e}[${B}]=vec2(u32(${te}), 0u);`;if(p.storage==="u32"&&p.value==="vec4")return`${e}[${B}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${te}));`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),ee=B=>(()=>{if(p.storage===p.value)return`${e}[${B}]`;if(p.storage==="vec2"&&p.value==="i32")return`i32(${e}[${B}].x)`;if(p.storage==="vec2"&&p.value==="u32")return`u32(${e}[${B}].x)`;if(p.storage==="u32"&&p.value==="vec4")return`vec4(bool(${e}[${B}] & 0xFFu), bool(${e}[${B}] & 0xFF00u), bool(${e}[${B}] & 0xFF0000u), bool(${e}[${B}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),v=d<2?"":` + fn get_${e}ByIndices(indices: ${p.indices}) -> ${o} { + return ${ee(`i2o_${e}(indices)`)}; + }`,R=d<2?"":(()=>{let B=c.map(Y=>`d${Y}: u32`).join(", "),te=c.map(Y=>`d${Y}`).join(", ");return` + fn get_${e}(${B}) -> ${o} { + return get_${e}ByIndices(${I(te)}); + }`})(),H=(...B)=>{if(B.length!==d)throw new Error(`indices length must be ${d}`);let te=B.map(h).join(",");return d===0?ee("0u"):d===1?ee(te[0]):(m.get=!0,m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}(${te})`)},L=B=>d<2?ee(B):(m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}ByIndices(${B})`),V=d<2?"":` + fn set_${e}ByIndices(indices: ${p.indices}, value: ${o}) { + ${C(`i2o_${e}(indices)`,"value")} + }`,K=d<2?"":(()=>{let B=c.map(Y=>`d${Y}: u32`).join(", "),te=c.map(Y=>`d${Y}`).join(", ");return` + fn set_${e}(${B}, value: ${o}) { + set_${e}ByIndices(${I(te)}, value); + }`})();return{impl:()=>{let B=[],te=!1;return m.offsetToIndices&&(B.push(T),te=!0),m.indicesToOffset&&(B.push(A),te=!0),m.broadcastedIndicesToOffset&&(Object.values(J).forEach(Y=>B.push(Y)),te=!0),m.set&&(B.push(K),te=!0),m.setByIndices&&(B.push(V),te=!0),m.get&&(B.push(R),te=!0),m.getByIndices&&(B.push(v),te=!0),!l&&te&&B.unshift(`const ${_} = ${p.indices}(${n.join(",")});`,`const ${w} = ${p.indices}(${F.computeStrides(n).join(",")});`),B.join(` +`)},type:p,offsetToIndices:S,indicesToOffset:P,broadcastedIndicesToOffset:q,indices:I,indicesGet:z,indicesSet:G,set:(...B)=>{if(B.length!==d+1)throw new Error(`indices length must be ${d}`);let te=B[d];if(typeof te!="string")throw new Error("value must be string");let Y=B.slice(0,d).map(h).join(",");return d===0?C("0u",te):d===1?C(Y[0],te):(m.set=!0,m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}(${Y}, ${te})`)},setByOffset:C,setByIndices:(B,te)=>d<2?C(B,te):(m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}ByIndices(${B}, ${te});`),get:H,getByOffset:ee,getByIndices:L,usage:i,name:e,strides:w,shape:_,rank:d}},j=(e,t,n,i=1)=>ci(e,t,n,"input",i),oe=(e,t,n,i=1)=>ci(e,t,n,"output",i),Ix=(e,t,n)=>ci(e,t,n,"atomicOutput",1),cd=(e,t,n,i=1)=>ci(e,t,n,"internal",i),Jm=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=Fn){let t=typeof e=="number"?e:e[0],n=typeof e=="number"?1:e[1],i=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||i>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${n}, ${i}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*n*i>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${n}, ${i}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let u=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,l=u?`@builtin(global_invocation_id) global_id : vec3, @builtin(workgroup_id) workgroup_id : vec3, @builtin(local_invocation_index) local_idx : u32, @builtin(local_invocation_id) local_id : vec3`:`@builtin(global_invocation_id) global_id : vec3, @@ -1839,69 +1839,69 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Lf.onnx.ModelProto.decode(e @builtin(num_workgroups) num_workgroups : vec3`,d=u?`let global_idx = global_id.x; let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + workgroup_id.y * num_workgroups[0] + workgroup_id.x; - let global_idx = workgroup_index * ${t*n*o}u + local_idx;`;return`@compute @workgroup_size(${t}, ${n}, ${o}) + let global_idx = workgroup_index * ${t*n*i}u + local_idx;`;return`@compute @workgroup_size(${t}, ${n}, ${i}) fn main(${l}) { ${d} - `}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let n=e.usage==="input"?"read":"read_write",o=e.usage==="atomicOutput"?"atomic":e.type.storage;return`@group(0) @binding(${t}) var ${e.name}: array<${o}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(` -`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,n=1){return this.uniforms.push({name:e,type:t,length:n}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:n,length:o}of this.uniforms)if(o&&o>4)n==="f16"?e.push(`@align(16) ${t}:array, ${Math.ceil(o/8)}>`):e.push(`${t}:array, ${Math.ceil(o/4)}>`);else{let u=o==null||o===1?n:`vec${o}<${n}>`;e.push(`${t}:${u}`)}return` + `}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let n=e.usage==="input"?"read":"read_write",i=e.usage==="atomicOutput"?"atomic":e.type.storage;return`@group(0) @binding(${t}) var ${e.name}: array<${i}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(` +`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,n=1){return this.uniforms.push({name:e,type:t,length:n}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:n,length:i}of this.uniforms)if(i&&i>4)n==="f16"?e.push(`@align(16) ${t}:array, ${Math.ceil(i/8)}>`):e.push(`${t}:array, ${Math.ceil(i/4)}>`);else{let u=i==null||i===1?n:`vec${i}<${n}>`;e.push(`${t}:${u}`)}return` struct Uniforms { ${e.join(", ")} }; @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(e=>e.impl()).join(` `)+this.internalVariables.map(e=>e.impl()).join(` -`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},Gv=(e,t)=>new gm(e,t)}),bm,Aa,ym,_m,wm,vm,gt,Hv,Wv,wr=D(()=>{"use strict";ae(),de(),Ve(),ce(),bm=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},Aa=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),ym=(e,t)=>z.sortBasedOnPerm(e,Aa(e.length,t)),_m=(e,t,n,o)=>{let u=`fn perm(i: ${o.type.indices}) -> ${n.type.indices} { - var a: ${n.type.indices};`;for(let l=0;l{let n=[],o=[];for(let u=0;u{let n=0;for(let o=0;o{let n=e.dataType,o=e.dims.length,u=Aa(o,t),l=ym(e.dims,u),d=e.dims,c=l,s=o<2||vm(u,e.dims),r;if(s)return r=m=>{let b=F("input",n,d,4),$=re("output",n,c,4);return` - ${m.registerUniform("output_size","u32").declareVariables(b,$)} +`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},Ox=(e,t)=>new Jm(e,t)}),Qm,ru,eg,tg,rg,ng,$t,Ex,Ax,Cr=D(()=>{"use strict";de(),pe(),He(),he(),Qm=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},ru=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),eg=(e,t)=>F.sortBasedOnPerm(e,ru(e.length,t)),tg=(e,t,n,i)=>{let u=`fn perm(i: ${i.type.indices}) -> ${n.type.indices} { + var a: ${n.type.indices};`;for(let l=0;l{let n=[],i=[];for(let u=0;u{let n=0;for(let i=0;i{let n=e.dataType,i=e.dims.length,u=ru(i,t),l=eg(e.dims,u),d=e.dims,c=l,s=i<2||ng(u,e.dims),r;if(s)return r=m=>{let g=j("input",n,d,4),_=oe("output",n,c,4);return` + ${m.registerUniform("output_size","u32").declareVariables(g,_)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} output[global_idx] = input[global_idx]; - }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let m=z.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64/4)},programUniforms:[{type:12,data:Math.ceil(m/4)}]}},getShaderSource:r};let{newShape:i,newPerm:a}=wm(e.dims,u),p=z.areEqual(a,[2,3,1]),h=z.areEqual(a,[3,1,2]);if(i.length===2||p||h){d=p?[i[0],i[1]*i[2]]:h?[i[0]*i[1],i[2]]:i,c=[d[1],d[0]];let m=16;return r=b=>{let $=F("a",n,d.length),_=re("output",n,c.length);return` - ${b.registerUniform("output_size","u32").declareVariables($,_)} - var tile : array, ${m}>; - ${b.mainStart([m,m,1])} + }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let m=F.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64/4)},programUniforms:[{type:12,data:Math.ceil(m/4)}]}},getShaderSource:r};let{newShape:o,newPerm:a}=rg(e.dims,u),p=F.areEqual(a,[2,3,1]),h=F.areEqual(a,[3,1,2]);if(o.length===2||p||h){d=p?[o[0],o[1]*o[2]]:h?[o[0]*o[1],o[2]]:o,c=[d[1],d[0]];let m=16;return r=g=>{let _=j("a",n,d.length),w=oe("output",n,c.length);return` + ${g.registerUniform("output_size","u32").declareVariables(_,w)} + var tile : array, ${m}>; + ${g.mainStart([m,m,1])} let stride = (uniforms.output_shape[1] - 1) / ${m} + 1; let workgroup_id_x = workgroup_index % stride; let workgroup_id_y = workgroup_index / stride; let input_col = workgroup_id_y * ${m}u + local_id.x; let input_row = workgroup_id_x * ${m}u + local_id.y; if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { - tile[local_id.y][local_id.x] = ${$.getByIndices(`${$.type.indices}(input_row, input_col)`)}; + tile[local_id.y][local_id.x] = ${_.getByIndices(`${_.type.indices}(input_row, input_col)`)}; } workgroupBarrier(); let output_col = workgroup_id_x * ${m}u + local_id.x; let output_row = workgroup_id_y * ${m}u + local_id.y; if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { - ${_.setByIndices(`${_.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} + ${w.setByIndices(`${w.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} } - }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let b=z.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(c[1]/m),y:Math.ceil(c[0]/m)},programUniforms:[{type:12,data:b},...se(d,c)]}},getShaderSource:r}}return r=m=>{let b=F("a",n,d.length),$=re("output",n,c.length);return` - ${m.registerUniform("output_size","u32").declareVariables(b,$)} + }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let g=F.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(c[1]/m),y:Math.ceil(c[0]/m)},programUniforms:[{type:12,data:g},...ue(d,c)]}},getShaderSource:r}}return r=m=>{let g=j("a",n,d.length),_=oe("output",n,c.length);return` + ${m.registerUniform("output_size","u32").declareVariables(g,_)} - ${_m(u,o,b,$)} + ${tg(u,i,g,_)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let indices = ${$.offsetToIndices("global_idx")}; + let indices = ${_.offsetToIndices("global_idx")}; let aIndices = perm(indices); - ${$.setByOffset("global_idx",b.getByIndices("aIndices"))} - }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let m=z.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...se(d,c)]}},getShaderSource:r}},Hv=(e,t)=>{bm(e.inputs,t.perm),e.compute(gt(e.inputs[0],t.perm))},Wv=e=>Se({perm:e.perm})}),xm,$m,Tm,Sm,Im,Om,Em,Am,km,Pm,It,Kv,Xv,Zv,Yv,Jv,Qv,ex,tx,rx,nx,WE=D(()=>{"use strict";ae(),de(),ce(),Fl(),wr(),xm={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},$m={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},Tm={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Sm={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Im=(e,t)=>{let n=[];for(let o=t-e;o{let n=[],o=e.length;for(let l=0;le[l]);return[n,u]},Em=(e,t)=>{let n=e.length+t.length,o=[],u=0;for(let l=0;l{for(let n=0;n{let n=[];if(!Am(e,t)){for(let o=0;on.push(o))}return n},Pm=(e,t,n,o,u,l,d)=>{let c=n[0].dims,s=z.size(l),r=z.size(d),i=F("_A",n[0].dataType,c),a=re("output",u,l),p=64;s===1&&(p=256);let h=` + ${_.setByOffset("global_idx",g.getByIndices("aIndices"))} + }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let m=F.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...ue(d,c)]}},getShaderSource:r}},Ex=(e,t)=>{Qm(e.inputs,t.perm),e.compute($t(e.inputs[0],t.perm))},Ax=e=>ke({perm:e.perm})}),ig,og,sg,ag,ug,lg,dg,cg,pg,hg,Nt,kx,Px,Cx,Rx,Dx,Mx,Nx,zx,Bx,Fx,JA=D(()=>{"use strict";de(),pe(),he(),pd(),Cr(),ig={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},og={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},sg={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},ag={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},ug=(e,t)=>{let n=[];for(let i=t-e;i{let n=[],i=e.length;for(let l=0;le[l]);return[n,u]},dg=(e,t)=>{let n=e.length+t.length,i=[],u=0;for(let l=0;l{for(let n=0;n{let n=[];if(!cg(e,t)){for(let i=0;in.push(i))}return n},hg=(e,t,n,i,u,l,d)=>{let c=n[0].dims,s=F.size(l),r=F.size(d),o=j("_A",n[0].dataType,c),a=oe("output",u,l),p=64;s===1&&(p=256);let h=` var aBestValues : array; - `,m=b=>` - ${b.registerUniform("reduceSize","u32").declareVariables(i,a)} + `,m=g=>` + ${g.registerUniform("reduceSize","u32").declareVariables(o,a)} ${h} fn DIV_CEIL(a : u32, b : u32) -> u32 { return ((a - 1u) / b + 1u); } - ${b.mainStart(p)} + ${g.mainStart(p)} let outputIndex = global_idx / ${p}; let offset = outputIndex * uniforms.reduceSize; - var bestValue = f32(${Tm[o]}); + var bestValue = f32(${sg[i]}); let Length = uniforms.reduceSize; for (var k = local_idx; k < Length; k = k + ${p}) { - let candidate = f32(${i.getByOffset("offset + k")}); - bestValue = ${xm[o]}; + let candidate = f32(${o.getByOffset("offset + k")}); + bestValue = ${ig[i]}; } aBestValues[local_idx] = bestValue; workgroupBarrier(); @@ -1912,7 +1912,7 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Lf.onnx.ModelProto.decode(e let interval = DIV_CEIL(reduceSize, 2u); if (local_idx < currentSize) { let candidate = aBestValues[local_idx + interval]; - bestValue = ${$m[o]}; + bestValue = ${og[i]}; aBestValues[local_idx] = bestValue; } reduceSize = interval; @@ -1920,22 +1920,22 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Lf.onnx.ModelProto.decode(e } if (local_idx == 0u) { - ${a.setByOffset("outputIndex",`${o==="mean"?`${a.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${a.type.storage}(${Sm[o]})`}`)}; + ${a.setByOffset("outputIndex",`${i==="mean"?`${a.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${a.type.storage}(${ag[i]})`}`)}; } - }`;return{name:e,shaderCache:{hint:`${t};${p}`,inputDependencies:["type"]},getShaderSource:m,getRunData:()=>({outputs:[{dims:l,dataType:u}],dispatchGroup:{x:s},programUniforms:[{type:12,data:r}]})}},It=(e,t,n,o)=>{let u=e.inputs.length===1?n:sl(e.inputs,n),l=u.axes;l.length===0&&!u.noopWithEmptyAxes&&(l=e.inputs[0].dims.map((h,m)=>m));let d=z.normalizeAxes(l,e.inputs[0].dims.length),c=d,s=e.inputs[0],r=km(c,e.inputs[0].dims.length);r.length>0&&(s=e.compute(gt(e.inputs[0],r),{inputs:[0],outputs:[-1]})[0],c=Im(c.length,s.dims.length));let[i,a]=Om(s.dims,c),p=i;u.keepDims&&(p=Em(i,d)),e.compute(Pm(t,u.cacheKey,[s],o,e.inputs[0].dataType,p,a),{inputs:[s]})},Kv=(e,t)=>{It(e,"ReduceMeanShared",t,"mean")},Xv=(e,t)=>{It(e,"ReduceL1Shared",t,"l1")},Zv=(e,t)=>{It(e,"ReduceL2Shared",t,"l2")},Yv=(e,t)=>{It(e,"ReduceLogSumExpShared",t,"logSumExp")},Jv=(e,t)=>{It(e,"ReduceMaxShared",t,"max")},Qv=(e,t)=>{It(e,"ReduceMinShared",t,"min")},ex=(e,t)=>{It(e,"ReduceProdShared",t,"prod")},tx=(e,t)=>{It(e,"ReduceSumShared",t,"sum")},rx=(e,t)=>{It(e,"ReduceSumSquareShared",t,"sumSquare")},nx=(e,t)=>{It(e,"ReduceLogSumShared",t,"logSum")}}),Ot,Cm,Eo,sl,Et,Dm,Rm,Nm,zm,Mm,Bm,Fm,Lm,jm,Um,At,ix,ox,sx,ax,ux,lx,dx,cx,px,hx,Fl=D(()=>{"use strict";ae(),de(),Ve(),ce(),WE(),Ot=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Cm=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Eo=(e,t,n,o,u,l,d=!1,c=!1)=>{let s=[],r=n[0].dims,i=r.length,a=z.normalizeAxes(u,i),p=!c&&a.length===0;r.forEach((b,$)=>{p||a.indexOf($)>=0?d&&s.push(1):s.push(b)});let h=s.length,m=z.size(s);return{name:e,shaderCache:t,getShaderSource:b=>{let $=[],_=F("_A",n[0].dataType,i),y=re("output",l,h),T=o(_,y,a),S=T[2];for(let O=0,A=0;O=0?(d&&A++,S=`for(var j${O}: u32 = 0; j${O} < ${r[O]}; j${O}++) { + }`;return{name:e,shaderCache:{hint:`${t};${p}`,inputDependencies:["type"]},getShaderSource:m,getRunData:()=>({outputs:[{dims:l,dataType:u}],dispatchGroup:{x:s},programUniforms:[{type:12,data:r}]})}},Nt=(e,t,n,i)=>{let u=e.inputs.length===1?n:Rl(e.inputs,n),l=u.axes;l.length===0&&!u.noopWithEmptyAxes&&(l=e.inputs[0].dims.map((h,m)=>m));let d=F.normalizeAxes(l,e.inputs[0].dims.length),c=d,s=e.inputs[0],r=pg(c,e.inputs[0].dims.length);r.length>0&&(s=e.compute($t(e.inputs[0],r),{inputs:[0],outputs:[-1]})[0],c=ug(c.length,s.dims.length));let[o,a]=lg(s.dims,c),p=o;u.keepDims&&(p=dg(o,d)),e.compute(hg(t,u.cacheKey,[s],i,e.inputs[0].dataType,p,a),{inputs:[s]})},kx=(e,t)=>{Nt(e,"ReduceMeanShared",t,"mean")},Px=(e,t)=>{Nt(e,"ReduceL1Shared",t,"l1")},Cx=(e,t)=>{Nt(e,"ReduceL2Shared",t,"l2")},Rx=(e,t)=>{Nt(e,"ReduceLogSumExpShared",t,"logSumExp")},Dx=(e,t)=>{Nt(e,"ReduceMaxShared",t,"max")},Mx=(e,t)=>{Nt(e,"ReduceMinShared",t,"min")},Nx=(e,t)=>{Nt(e,"ReduceProdShared",t,"prod")},zx=(e,t)=>{Nt(e,"ReduceSumShared",t,"sum")},Bx=(e,t)=>{Nt(e,"ReduceSumSquareShared",t,"sumSquare")},Fx=(e,t)=>{Nt(e,"ReduceLogSumShared",t,"logSum")}}),zt,fg,Ko,Rl,Bt,mg,gg,bg,yg,wg,_g,vg,xg,$g,Tg,Ft,Lx,jx,Ux,Vx,qx,Gx,Hx,Wx,Kx,Xx,pd=D(()=>{"use strict";de(),pe(),He(),he(),JA(),zt=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},fg=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Ko=(e,t,n,i,u,l,d=!1,c=!1)=>{let s=[],r=n[0].dims,o=r.length,a=F.normalizeAxes(u,o),p=!c&&a.length===0;r.forEach((g,_)=>{p||a.indexOf(_)>=0?d&&s.push(1):s.push(g)});let h=s.length,m=F.size(s);return{name:e,shaderCache:t,getShaderSource:g=>{let _=[],w=j("_A",n[0].dataType,o),y=oe("output",l,h),T=i(w,y,a),S=T[2];for(let O=0,A=0;O=0?(d&&A++,S=`for(var j${O}: u32 = 0; j${O} < ${r[O]}; j${O}++) { ${T[2].includes("last_index")?`let last_index = j${O};`:""} - ${_.indicesSet("input_indices",O,`j${O}`)} + ${w.indicesSet("input_indices",O,`j${O}`)} ${S} - }`):($.push(`${_.indicesSet("input_indices",O,y.indicesGet("output_indices",A))};`),A++);return` + }`):(_.push(`${w.indicesSet("input_indices",O,y.indicesGet("output_indices",A))};`),A++);return` - ${b.registerUniform("output_size","u32").declareVariables(_,y)} + ${g.registerUniform("output_size","u32").declareVariables(w,y)} - ${b.mainStart()} - ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - var input_indices: ${_.type.indices}; + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var input_indices: ${w.type.indices}; let output_indices = ${y.offsetToIndices("global_idx")}; - ${$.join(` + ${_.join(` `)} ${T[0]} // init ops for reduce max/min ${T[1]} @@ -1943,19 +1943,19 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Lf.onnx.ModelProto.decode(e ${T[3]} ${T.length===4?y.setByOffset("global_idx","value"):T.slice(4).join(` `)} - }`},getRunData:()=>({outputs:[{dims:s,dataType:l}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...se(r,s)]})}},sl=(e,t)=>{let n=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(o=>n.push(Number(o))),Se({axes:n,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},Et=(e,t,n,o)=>{let u=e.inputs,l=u.length===1?n:sl(u,n);e.compute(Eo(t,{hint:l.cacheKey,inputDependencies:["rank"]},[u[0]],l.noopWithEmptyAxes&&l.axes.length===0?Cm:o,l.axes,u[0].dataType,l.keepDims,l.noopWithEmptyAxes),{inputs:[0]})},Dm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceLogSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},Rm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceL1",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},Nm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceL2",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},zm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceLogSumExp",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},Mm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceMax",t,(n,o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(n.indicesSet("input_indices",d,0));return[`${l.join(` -`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},Bm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceMean",t,(n,o,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=e.inputs[0].dims[d]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${l});`]})},Fm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceMin",t,(n,o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`input_indices[${d}] = 0;`);return[`${l.join(` -`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},Lm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceProd",t,(n,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},jm=(e,t)=>{Ot(e.inputs),Et(e,"ReduceSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Um=(e,t)=>{Ot(e.inputs),Et(e,"ReduceSumSquare",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},At=(e,t,n)=>{if(t.length===0)return n;let o=1,u=1;for(let l=0;l1024},ix=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Bm(e,t):Kv(e,t)},ox=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Rm(e,t):Xv(e,t)},sx=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Nm(e,t):Zv(e,t)},ax=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?zm(e,t):Yv(e,t)},ux=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Mm(e,t):Jv(e,t)},lx=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Fm(e,t):Qv(e,t)},dx=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Lm(e,t):ex(e,t)},cx=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?jm(e,t):tx(e,t)},px=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Um(e,t):rx(e,t)},hx=(e,t)=>{At(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Dm(e,t):nx(e,t)}}),ka,fx,mx,al,KE=D(()=>{"use strict";ae(),Ve(),Fl(),ka=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},fx=(e,t)=>{ka(e.inputs);let n=(o,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` -`)}`,`var value = ${o.getByIndices("input_indices")}; -var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { - value = ${o.getByIndices("input_indices")}; + }`},getRunData:()=>({outputs:[{dims:s,dataType:l}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...ue(r,s)]})}},Rl=(e,t)=>{let n=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(i=>n.push(Number(i))),ke({axes:n,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},Bt=(e,t,n,i)=>{let u=e.inputs,l=u.length===1?n:Rl(u,n);e.compute(Ko(t,{hint:l.cacheKey,inputDependencies:["rank"]},[u[0]],l.noopWithEmptyAxes&&l.axes.length===0?fg:i,l.axes,u[0].dataType,l.keepDims,l.noopWithEmptyAxes),{inputs:[0]})},mg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceLogSum",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},gg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceL1",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},bg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceL2",t,(n,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},yg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceLogSumExp",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},wg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceMax",t,(n,i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(n.indicesSet("input_indices",d,0));return[`${l.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},_g=(e,t)=>{zt(e.inputs),Bt(e,"ReduceMean",t,(n,i,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=e.inputs[0].dims[d]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${i.type.value}(sum / ${l});`]})},vg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceMin",t,(n,i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`input_indices[${d}] = 0;`);return[`${l.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},xg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceProd",t,(n,i)=>[`var value = ${i.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},$g=(e,t)=>{zt(e.inputs),Bt(e,"ReduceSum",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Tg=(e,t)=>{zt(e.inputs),Bt(e,"ReduceSumSquare",t,(n,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},Ft=(e,t,n)=>{if(t.length===0)return n;let i=1,u=1;for(let l=0;l1024},Lx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?_g(e,t):kx(e,t)},jx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?gg(e,t):Px(e,t)},Ux=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?bg(e,t):Cx(e,t)},Vx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?yg(e,t):Rx(e,t)},qx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?wg(e,t):Dx(e,t)},Gx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?vg(e,t):Mx(e,t)},Hx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?xg(e,t):Nx(e,t)},Wx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?$g(e,t):zx(e,t)},Kx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Tg(e,t):Bx(e,t)},Xx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?mg(e,t):Fx(e,t)}}),nu,Zx,Yx,Dl,QA=D(()=>{"use strict";de(),He(),pd(),nu=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},Zx=(e,t)=>{nu(e.inputs);let n=(i,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` +`)}`,`var value = ${i.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { + value = ${i.getByIndices("input_indices")}; best_index = i32(last_index); - }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Eo("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},mx=(e,t)=>{ka(e.inputs);let n=(o,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` -`)}`,`var value = ${o.getByIndices("input_indices")}; -var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { - value = ${o.getByIndices("input_indices")}; + }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Ko("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},Yx=(e,t)=>{nu(e.inputs);let n=(i,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` +`)}`,`var value = ${i.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { + value = ${i.getByIndices("input_indices")}; best_index = i32(last_index); - }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Eo("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},al=e=>Se(e)}),Vm,ao,qm,Gm,Hm,yi,Wm,gx,Ll=D(()=>{"use strict";ae(),de(),Ml(),ce(),Vm=(e,t)=>{let n=e[0],o=e[1],u=e[2],l=e[3],d=e[4],c=e[5];if(d&&c)throw new Error("Attention cannot have both past and attention_bias");if(n.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let s=n.dims[0],r=n.dims[1],i=n.dims[2];if(u.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(o.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(o.dims[0]!==i)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(u.dims[0]!==o.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let a=u.dims[0]/3,p=a,h=p;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let T of t.qkvHiddenSizes)if(T%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");a=t.qkvHiddenSizes[0],p=t.qkvHiddenSizes[1],h=t.qkvHiddenSizes[2]}let m=r;if(a!==p)throw new Error("qkv_hidden_sizes first element should be same as the second");if(u.dims[0]!==a+p+h)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let b=0;if(d){if(p!==h)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(d.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(d.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(d.dims[1]!==s)throw new Error('Input "past" second dimension must be batch_size');if(d.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(d.dims[4]!==p/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(b=d.dims[3])}let $=m+b,_=-1,y=0;if(l)throw new Error("Mask not supported");if(d)throw new Error("past is not supported");if(c){if(c.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(c.dims[0]!==s||c.dims[1]!==t.numHeads||c.dims[2]!==r||c.dims[3]!==$)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:s,sequenceLength:r,pastSequenceLength:b,kvSequenceLength:m,totalSequenceLength:$,maxSequenceLength:_,inputHiddenSize:i,hiddenSize:a,vHiddenSize:h,headSize:Math.floor(a/t.numHeads),vHeadSize:Math.floor(h/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:y,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},ao=(e,t,n)=>t&&e?` + }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Ko("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},Dl=e=>ke(e)}),Sg,Eo,Ig,Og,Eg,Mi,Ag,Jx,hd=D(()=>{"use strict";de(),pe(),dd(),he(),Sg=(e,t)=>{let n=e[0],i=e[1],u=e[2],l=e[3],d=e[4],c=e[5];if(d&&c)throw new Error("Attention cannot have both past and attention_bias");if(n.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let s=n.dims[0],r=n.dims[1],o=n.dims[2];if(u.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(i.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(i.dims[0]!==o)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(u.dims[0]!==i.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let a=u.dims[0]/3,p=a,h=p;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let T of t.qkvHiddenSizes)if(T%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");a=t.qkvHiddenSizes[0],p=t.qkvHiddenSizes[1],h=t.qkvHiddenSizes[2]}let m=r;if(a!==p)throw new Error("qkv_hidden_sizes first element should be same as the second");if(u.dims[0]!==a+p+h)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let g=0;if(d){if(p!==h)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(d.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(d.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(d.dims[1]!==s)throw new Error('Input "past" second dimension must be batch_size');if(d.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(d.dims[4]!==p/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(g=d.dims[3])}let _=m+g,w=-1,y=0;if(l)throw new Error("Mask not supported");if(d)throw new Error("past is not supported");if(c){if(c.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(c.dims[0]!==s||c.dims[1]!==t.numHeads||c.dims[2]!==r||c.dims[3]!==_)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:s,sequenceLength:r,pastSequenceLength:g,kvSequenceLength:m,totalSequenceLength:_,maxSequenceLength:w,inputHiddenSize:o,hiddenSize:a,vHiddenSize:h,headSize:Math.floor(a/t.numHeads),vHeadSize:Math.floor(h/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:y,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},Eo=(e,t,n)=>t&&e?` let total_sequence_length_input = u32(${t.getByOffset("0")}); let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length); let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input; @@ -1968,16 +1968,16 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas `:` ${n?"let past_sequence_length = uniforms.past_sequence_length":""}; let present_sequence_length = total_sequence_length; - `,qm=(e,t,n,o,u,l,d,c)=>{let s=Ue(d?1:l),r=64,i=l/s;i{let y=re("x",e.dataType,e.dims,s),T=[y],S=d?F("seq_lens",d.dataType,d.dims):void 0;S&&T.push(S);let O=c?F("total_sequence_length_input",c.dataType,c.dims):void 0;O&&T.push(O);let A=Ye(e.dataType),P=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` + `,Ig=(e,t,n,i,u,l,d,c)=>{let s=Ge(d?1:l),r=64,o=l/s;o{let y=oe("x",e.dataType,e.dims,s),T=[y],S=d?j("seq_lens",d.dataType,d.dims):void 0;S&&T.push(S);let O=c?j("total_sequence_length_input",c.dataType,c.dims):void 0;O&&T.push(O);let A=rt(e.dataType),P=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` var thread_max: array; var thread_sum: array; - ${_.registerUniforms(P).declareVariables(...T)} - ${_.mainStart([r,1,1])} + ${w.registerUniforms(P).declareVariables(...T)} + ${w.mainStart([r,1,1])} let batchIdx = workgroup_id.z / uniforms.num_heads; let headIdx = workgroup_id.z % uniforms.num_heads; let sequence_length = uniforms.sequence_length; var total_sequence_length = uniforms.total_sequence_length; - ${ao(S,O,!1)} + ${Eo(S,O,!1)} let local_offset = local_idx * uniforms.elements_per_thread; let offset = (global_idx / ${r}) * uniforms.total_sequence_length + local_offset; let seq_causal_length = ${d?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"}; @@ -2019,13 +2019,13 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) { x[offset + total_seq_id] = ${y.type.value}(${A}(0)); }`:""}; - }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${r};${h};${s}`,inputDependencies:b},getShaderSource:$,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:u,z:t*n},programUniforms:p})}},Gm=(e,t,n,o,u,l,d,c,s)=>{let r=d+l.kvSequenceLength,i=[l.batchSize,l.numHeads,l.sequenceLength,r],a=e>1&&o,p=l.kvNumHeads?l.kvNumHeads:l.numHeads,h=a?[l.batchSize,p,r,l.headSize]:void 0,m=l.nReps?l.nReps:1,b=l.scale===0?1/Math.sqrt(l.headSize):l.scale,$=Ue(l.headSize),_=l.headSize/$,y=12,T={x:Math.ceil(r/y),y:Math.ceil(l.sequenceLength/y),z:l.batchSize*l.numHeads},S=[{type:12,data:l.sequenceLength},{type:12,data:_},{type:12,data:r},{type:12,data:l.numHeads},{type:12,data:l.headSize},{type:1,data:b},{type:12,data:d},{type:12,data:l.kvSequenceLength},{type:12,data:m}],O=a&&o&&z.size(o.dims)>0,A=["type","type"];O&&A.push("type"),u&&A.push("type"),c&&A.push("type"),s&&A.push("type");let P=[{dims:i,dataType:t.dataType,gpuDataType:0}];a&&P.push({dims:h,dataType:t.dataType,gpuDataType:0});let I=L=>{let V=F("q",t.dataType,t.dims,$),Z=F("key",n.dataType,n.dims,$),q=[V,Z];if(O){let G=F("past_key",o.dataType,o.dims,$);q.push(G)}u&&q.push(F("attention_bias",u.dataType,u.dims));let N=c?F("seq_lens",c.dataType,c.dims):void 0;N&&q.push(N);let ue=s?F("total_sequence_length_input",s.dataType,s.dims):void 0;ue&&q.push(ue);let w=re("output",t.dataType,i),C=[w];a&&C.push(re("present_key",t.dataType,h,$));let U=Ye(1,$),H=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${r};${h};${s}`,inputDependencies:g},getShaderSource:_,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:u,z:t*n},programUniforms:p})}},Og=(e,t,n,i,u,l,d,c,s)=>{let r=d+l.kvSequenceLength,o=[l.batchSize,l.numHeads,l.sequenceLength,r],a=e>1&&i,p=l.kvNumHeads?l.kvNumHeads:l.numHeads,h=a?[l.batchSize,p,r,l.headSize]:void 0,m=l.nReps?l.nReps:1,g=l.scale===0?1/Math.sqrt(l.headSize):l.scale,_=Ge(l.headSize),w=l.headSize/_,y=12,T={x:Math.ceil(r/y),y:Math.ceil(l.sequenceLength/y),z:l.batchSize*l.numHeads},S=[{type:12,data:l.sequenceLength},{type:12,data:w},{type:12,data:r},{type:12,data:l.numHeads},{type:12,data:l.headSize},{type:1,data:g},{type:12,data:d},{type:12,data:l.kvSequenceLength},{type:12,data:m}],O=a&&i&&F.size(i.dims)>0,A=["type","type"];O&&A.push("type"),u&&A.push("type"),c&&A.push("type"),s&&A.push("type");let P=[{dims:o,dataType:t.dataType,gpuDataType:0}];a&&P.push({dims:h,dataType:t.dataType,gpuDataType:0});let I=z=>{let G=j("q",t.dataType,t.dims,_),J=j("key",n.dataType,n.dims,_),q=[G,J];if(O){let V=j("past_key",i.dataType,i.dims,_);q.push(V)}u&&q.push(j("attention_bias",u.dataType,u.dims));let C=c?j("seq_lens",c.dataType,c.dims):void 0;C&&q.push(C);let ee=s?j("total_sequence_length_input",s.dataType,s.dims):void 0;ee&&q.push(ee);let v=oe("output",t.dataType,o),R=[v];a&&R.push(oe("present_key",t.dataType,h,_));let H=rt(1,_),L=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` const TILE_SIZE = ${y}u; - var tileQ: array<${V.type.storage}, ${y*y}>; - var tileK: array<${V.type.storage}, ${y*y}>; - ${L.registerUniforms(H).declareVariables(...q,...C)} - ${L.mainStart([y,y,1])} + var tileQ: array<${G.type.storage}, ${y*y}>; + var tileK: array<${G.type.storage}, ${y*y}>; + ${z.registerUniforms(L).declareVariables(...q,...R)} + ${z.mainStart([y,y,1])} // x holds the N and y holds the M let headIdx = workgroup_id.z % uniforms.num_heads; let kvHeadIdx = ${m===1?"headIdx":"headIdx / uniforms.n_reps"}; @@ -2035,13 +2035,13 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas let n = workgroup_id.x * TILE_SIZE; let sequence_length = uniforms.M; var total_sequence_length = uniforms.N; - ${ao(N,ue,!0)} + ${Eo(C,ee,!0)} let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; ${O&&a?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""}; let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K; ${a?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""} - var value = ${U}(0); + var value = ${H}(0); for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) { tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x]; @@ -2064,7 +2064,7 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas workgroupBarrier(); for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) { - value += ${U}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); + value += ${H}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); } workgroupBarrier(); @@ -2073,15 +2073,15 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas if (global_id.y < uniforms.M && global_id.x < total_sequence_length) { let headOffset = workgroup_id.z * uniforms.M * uniforms.N; let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x; - var sum: f32 = ${(()=>{switch($){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${$}`)}})()}; - output[outputIdx] = ${w.type.value} (sum * uniforms.alpha) + ${u?"attention_bias[outputIdx]":"0.0"}; + var sum: f32 = ${(()=>{switch(_){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${_}`)}})()}; + output[outputIdx] = ${v.type.value} (sum * uniforms.alpha) + ${u?"attention_bias[outputIdx]":"0.0"}; } - }`};return{name:"AttentionProbs",shaderCache:{hint:`${$};${u!==void 0};${o!==void 0};${e}`,inputDependencies:A},getRunData:()=>({outputs:P,dispatchGroup:T,programUniforms:S}),getShaderSource:I}},Hm=(e,t,n,o,u,l,d=void 0,c=void 0)=>{let s=l+u.kvSequenceLength,r=u.nReps?u.nReps:1,i=u.vHiddenSize*r,a=e>1&&o,p=u.kvNumHeads?u.kvNumHeads:u.numHeads,h=a?[u.batchSize,p,s,u.headSize]:void 0,m=[u.batchSize,u.sequenceLength,i],b=12,$={x:Math.ceil(u.vHeadSize/b),y:Math.ceil(u.sequenceLength/b),z:u.batchSize*u.numHeads},_=[{type:12,data:u.sequenceLength},{type:12,data:s},{type:12,data:u.vHeadSize},{type:12,data:u.numHeads},{type:12,data:u.headSize},{type:12,data:i},{type:12,data:l},{type:12,data:u.kvSequenceLength},{type:12,data:r}],y=a&&o&&z.size(o.dims)>0,T=["type","type"];y&&T.push("type"),d&&T.push("type"),c&&T.push("type");let S=[{dims:m,dataType:t.dataType,gpuDataType:0}];a&&S.push({dims:h,dataType:t.dataType,gpuDataType:0});let O=A=>{let P=F("probs",t.dataType,t.dims),I=F("v",n.dataType,n.dims),L=[P,I];y&&L.push(F("past_value",o.dataType,o.dims));let V=d?F("seq_lens",d.dataType,d.dims):void 0;d&&L.push(V);let Z=c?F("total_sequence_length_input",c.dataType,c.dims):void 0;c&&L.push(Z);let q=[re("output",t.dataType,m)];a&&q.push(re("present_value",t.dataType,h));let N=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` - const TILE_SIZE = ${b}u; - var tileQ: array<${P.type.value}, ${b*b}>; - var tileV: array<${P.type.value}, ${b*b}>; - ${A.registerUniforms(N).declareVariables(...L,...q)} - ${A.mainStart([b,b,1])} + }`};return{name:"AttentionProbs",shaderCache:{hint:`${_};${u!==void 0};${i!==void 0};${e}`,inputDependencies:A},getRunData:()=>({outputs:P,dispatchGroup:T,programUniforms:S}),getShaderSource:I}},Eg=(e,t,n,i,u,l,d=void 0,c=void 0)=>{let s=l+u.kvSequenceLength,r=u.nReps?u.nReps:1,o=u.vHiddenSize*r,a=e>1&&i,p=u.kvNumHeads?u.kvNumHeads:u.numHeads,h=a?[u.batchSize,p,s,u.headSize]:void 0,m=[u.batchSize,u.sequenceLength,o],g=12,_={x:Math.ceil(u.vHeadSize/g),y:Math.ceil(u.sequenceLength/g),z:u.batchSize*u.numHeads},w=[{type:12,data:u.sequenceLength},{type:12,data:s},{type:12,data:u.vHeadSize},{type:12,data:u.numHeads},{type:12,data:u.headSize},{type:12,data:o},{type:12,data:l},{type:12,data:u.kvSequenceLength},{type:12,data:r}],y=a&&i&&F.size(i.dims)>0,T=["type","type"];y&&T.push("type"),d&&T.push("type"),c&&T.push("type");let S=[{dims:m,dataType:t.dataType,gpuDataType:0}];a&&S.push({dims:h,dataType:t.dataType,gpuDataType:0});let O=A=>{let P=j("probs",t.dataType,t.dims),I=j("v",n.dataType,n.dims),z=[P,I];y&&z.push(j("past_value",i.dataType,i.dims));let G=d?j("seq_lens",d.dataType,d.dims):void 0;d&&z.push(G);let J=c?j("total_sequence_length_input",c.dataType,c.dims):void 0;c&&z.push(J);let q=[oe("output",t.dataType,m)];a&&q.push(oe("present_value",t.dataType,h));let C=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${g}u; + var tileQ: array<${P.type.value}, ${g*g}>; + var tileV: array<${P.type.value}, ${g*g}>; + ${A.registerUniforms(C).declareVariables(...z,...q)} + ${A.mainStart([g,g,1])} let headIdx = workgroup_id.z % uniforms.num_heads; let batchIdx = workgroup_id.z / uniforms.num_heads; let kvHeadIdx = ${r===1?"headIdx":"headIdx / uniforms.n_reps"}; @@ -2090,7 +2090,7 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas let n = global_id.x; let sequence_length = uniforms.M; var total_sequence_length = uniforms.K; - ${ao(V,Z,!0)} + ${Eo(G,J,!0)} let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch ${y&&a?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""}; @@ -2131,13 +2131,13 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas + headIdx * uniforms.N + n; output[outputIdx] = value; } - }`};return{name:"AttentionScore",shaderCache:{hint:`${o!==void 0};${e}`,inputDependencies:T},getRunData:()=>({outputs:S,dispatchGroup:$,programUniforms:_}),getShaderSource:O}},yi=(e,t,n,o,u,l,d,c,s,r,i=void 0,a=void 0)=>{let p=Math.min(e.outputCount,1+(d?1:0)+(c?1:0)),h=p>1?d:void 0,m=p>1?c:void 0,b=p>1?r.pastSequenceLength:0,$=b+r.kvSequenceLength,_=s&&z.size(s.dims)>0?s:void 0,y=[t,n];h&&z.size(h.dims)>0&&y.push(h),_&&y.push(_),i&&y.push(i),a&&y.push(a);let T=e.compute(Gm(p,t,n,h,_,r,b,i,a),{inputs:y,outputs:p>1?[-1,1]:[-1]})[0];e.compute(qm(T,r.batchSize,r.numHeads,b,r.sequenceLength,$,i,a),{inputs:i&&a?[T,i,a]:[T],outputs:[]});let S=[T,o];m&&z.size(m.dims)>0&&S.push(m),i&&S.push(i),a&&S.push(a),e.compute(Hm(p,T,o,m,r,b,i,a),{inputs:S,outputs:p>1?[0,2]:[0]})},Wm=(e,t)=>{let n=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],o=t.sequenceLength,u=t.inputHiddenSize,l=t.headSize,d=12,c={x:Math.ceil(t.headSize/d),y:Math.ceil(t.sequenceLength/d),z:t.batchSize*t.numHeads},s=[e.inputs[0],e.inputs[1],e.inputs[2]],r=[{type:12,data:o},{type:12,data:u},{type:12,data:l},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],i=a=>{let p=re("output_q",s[0].dataType,n),h=re("output_k",s[0].dataType,n),m=re("output_v",s[0].dataType,n),b=F("input",s[0].dataType,s[0].dims),$=F("weight",s[1].dataType,s[1].dims),_=F("bias",s[2].dataType,s[2].dims),y=b.type.storage,T=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` + }`};return{name:"AttentionScore",shaderCache:{hint:`${i!==void 0};${e}`,inputDependencies:T},getRunData:()=>({outputs:S,dispatchGroup:_,programUniforms:w}),getShaderSource:O}},Mi=(e,t,n,i,u,l,d,c,s,r,o=void 0,a=void 0)=>{let p=Math.min(e.outputCount,1+(d?1:0)+(c?1:0)),h=p>1?d:void 0,m=p>1?c:void 0,g=p>1?r.pastSequenceLength:0,_=g+r.kvSequenceLength,w=s&&F.size(s.dims)>0?s:void 0,y=[t,n];h&&F.size(h.dims)>0&&y.push(h),w&&y.push(w),o&&y.push(o),a&&y.push(a);let T=e.compute(Og(p,t,n,h,w,r,g,o,a),{inputs:y,outputs:p>1?[-1,1]:[-1]})[0];e.compute(Ig(T,r.batchSize,r.numHeads,g,r.sequenceLength,_,o,a),{inputs:o&&a?[T,o,a]:[T],outputs:[]});let S=[T,i];m&&F.size(m.dims)>0&&S.push(m),o&&S.push(o),a&&S.push(a),e.compute(Eg(p,T,i,m,r,g,o,a),{inputs:S,outputs:p>1?[0,2]:[0]})},Ag=(e,t)=>{let n=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],i=t.sequenceLength,u=t.inputHiddenSize,l=t.headSize,d=12,c={x:Math.ceil(t.headSize/d),y:Math.ceil(t.sequenceLength/d),z:t.batchSize*t.numHeads},s=[e.inputs[0],e.inputs[1],e.inputs[2]],r=[{type:12,data:i},{type:12,data:u},{type:12,data:l},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],o=a=>{let p=oe("output_q",s[0].dataType,n),h=oe("output_k",s[0].dataType,n),m=oe("output_v",s[0].dataType,n),g=j("input",s[0].dataType,s[0].dims),_=j("weight",s[1].dataType,s[1].dims),w=j("bias",s[2].dataType,s[2].dims),y=g.type.storage,T=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` const TILE_SIZE = ${d}u; var tileInput: array<${y}, ${d*d}>; var tileWeightQ: array<${y}, ${d*d}>; var tileWeightK: array<${y}, ${d*d}>; var tileWeightV: array<${y}, ${d*d}>; - ${a.registerUniforms(T).declareVariables(b,$,_,p,h,m)} + ${a.registerUniforms(T).declareVariables(g,_,w,p,h,m)} ${a.mainStart([d,d,1])} let batchIndex = workgroup_id.z / uniforms.num_heads; let headNumber = workgroup_id.z % uniforms.num_heads; @@ -2186,34 +2186,34 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas output_k[outputIdx] = valueK; output_v[outputIdx] = valueV; } - }`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:c,programUniforms:r}),getShaderSource:i},{inputs:s,outputs:[-1,-1,-1]})},gx=(e,t)=>{let n=Vm(e.inputs,t),[o,u,l]=Wm(e,n);return yi(e,o,u,l,e.inputs[4],void 0,void 0,void 0,e.inputs[5],n)}}),Km,Xm,Zm,bx,XE=D(()=>{"use strict";it(),ae(),de(),Ve(),ce(),Km=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let n=(o,u,l)=>{let d=u.length;if(d!==o.length)throw new Error(`${l}: num dimensions != ${d}`);u.forEach((c,s)=>{if(c!==o[s])throw new Error(`${l}: dim[${s}] do not match`)})};if(e[0].dims.length>1){let o=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);n(e[1].dims,o,"Invalid input scale"),n(e[2].dims,o,"Invalid input B"),n(e[3].dims,o,"Invalid input mean"),n(e[4].dims,o,"Invalid input var")}else n(e[1].dims,[1],"Invalid input scale"),n(e[2].dims,[1],"Invalid input B"),n(e[3].dims,[1],"Invalid input mean"),n(e[4].dims,[1],"Invalid input var")},Xm=(e,t)=>{let{epsilon:n,spatial:o,format:u}=t,l=e[0].dims,d=o?Ue(l[l.length-1]):1,c=u==="NHWC"&&l.length>1?d:1,s=z.size(l)/d,r=o,i=r?l.length:l,a=F("x",e[0].dataType,e[0].dims,d),p=F("scale",e[1].dataType,e[1].dims,c),h=F("bias",e[2].dataType,e[2].dims,c),m=F("inputMean",e[3].dataType,e[3].dims,c),b=F("inputVar",e[4].dataType,e[4].dims,c),$=re("y",e[0].dataType,i,d),_=()=>{let T="";if(o)T=`let cOffset = ${l.length===1?"0u":u==="NHWC"?`outputIndices[${l.length-1}] / ${d}`:"outputIndices[1]"};`;else if(u==="NCHW")T=` - ${$.indicesSet("outputIndices","0","0")} - let cOffset = ${$.indicesToOffset("outputIndices")};`;else{T=`var cIndices = ${p.type.indices}(0); + }`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:c,programUniforms:r}),getShaderSource:o},{inputs:s,outputs:[-1,-1,-1]})},Jx=(e,t)=>{let n=Sg(e.inputs,t),[i,u,l]=Ag(e,n);return Mi(e,i,u,l,e.inputs[4],void 0,void 0,void 0,e.inputs[5],n)}}),kg,Pg,Cg,Qx,ek=D(()=>{"use strict";dt(),de(),pe(),He(),he(),kg=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let n=(i,u,l)=>{let d=u.length;if(d!==i.length)throw new Error(`${l}: num dimensions != ${d}`);u.forEach((c,s)=>{if(c!==i[s])throw new Error(`${l}: dim[${s}] do not match`)})};if(e[0].dims.length>1){let i=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);n(e[1].dims,i,"Invalid input scale"),n(e[2].dims,i,"Invalid input B"),n(e[3].dims,i,"Invalid input mean"),n(e[4].dims,i,"Invalid input var")}else n(e[1].dims,[1],"Invalid input scale"),n(e[2].dims,[1],"Invalid input B"),n(e[3].dims,[1],"Invalid input mean"),n(e[4].dims,[1],"Invalid input var")},Pg=(e,t)=>{let{epsilon:n,spatial:i,format:u}=t,l=e[0].dims,d=i?Ge(l[l.length-1]):1,c=u==="NHWC"&&l.length>1?d:1,s=F.size(l)/d,r=i,o=r?l.length:l,a=j("x",e[0].dataType,e[0].dims,d),p=j("scale",e[1].dataType,e[1].dims,c),h=j("bias",e[2].dataType,e[2].dims,c),m=j("inputMean",e[3].dataType,e[3].dims,c),g=j("inputVar",e[4].dataType,e[4].dims,c),_=oe("y",e[0].dataType,o,d),w=()=>{let T="";if(i)T=`let cOffset = ${l.length===1?"0u":u==="NHWC"?`outputIndices[${l.length-1}] / ${d}`:"outputIndices[1]"};`;else if(u==="NCHW")T=` + ${_.indicesSet("outputIndices","0","0")} + let cOffset = ${_.indicesToOffset("outputIndices")};`;else{T=`var cIndices = ${p.type.indices}(0); cIndices[0] = outputIndices[${l.length-1}];`;for(let S=1;S` const epsilon = ${n}; - ${T.registerUniform("outputSize","u32").declareVariables(a,p,h,m,b,$)} + ${T.registerUniform("outputSize","u32").declareVariables(a,p,h,m,g,_)} ${T.mainStart()} ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - var outputIndices = ${$.offsetToIndices(`global_idx * ${d}`)}; - ${_()} + var outputIndices = ${_.offsetToIndices(`global_idx * ${d}`)}; + ${w()} let scale = ${p.getByOffset("cOffset")}; let bias = ${h.getByOffset("cOffset")}; let inputMean = ${m.getByOffset("cOffset")}; - let inputVar = ${b.getByOffset("cOffset")}; + let inputVar = ${g.getByOffset("cOffset")}; let x = ${a.getByOffset("global_idx")}; let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; - ${$.setByOffset("global_idx","value")} - }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${o}_${d}`,inputDependencies:r?["rank","type","type","type","type"]:void 0},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r?[{type:12,data:s},...se(l)]:[{type:12,data:s}]})}},Zm=e=>Se(e),bx=(e,t)=>{let{inputs:n,outputCount:o}=e,u=Zm({...t,outputCount:o});if(pe.webgpu.validateInputContent&&Km(n,u),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(Xm(n,u))}}),Ym,Jm,yx,ZE=D(()=>{"use strict";de(),ce(),Ym=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Jm=e=>{let t=e[0].dims,n=e[0].dims[2],o=z.size(t)/4,u=e[0].dataType,l=F("input",u,t,4),d=F("bias",u,[n],4),c=F("residual",u,t,4),s=re("output",u,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)}}),getShaderSource:r=>` + ${_.setByOffset("global_idx","value")} + }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${i}_${d}`,inputDependencies:r?["rank","type","type","type","type"]:void 0},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r?[{type:12,data:s},...ue(l)]:[{type:12,data:s}]})}},Cg=e=>ke(e),Qx=(e,t)=>{let{inputs:n,outputCount:i}=e,u=Cg({...t,outputCount:i});if(me.webgpu.validateInputContent&&kg(n,u),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(Pg(n,u))}}),Rg,Dg,e1,tk=D(()=>{"use strict";pe(),he(),Rg=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Dg=e=>{let t=e[0].dims,n=e[0].dims[2],i=F.size(t)/4,u=e[0].dataType,l=j("input",u,t,4),d=j("bias",u,[n],4),c=j("residual",u,t,4),s=oe("output",u,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:r=>` const channels = ${n}u / 4; ${r.declareVariables(l,d,c,s)} ${r.mainStart()} - ${r.guardAgainstOutOfBoundsWorkgroupSizes(o)} + ${r.guardAgainstOutOfBoundsWorkgroupSizes(i)} let value = ${l.getByOffset("global_idx")} + ${d.getByOffset("global_idx % channels")} + ${c.getByOffset("global_idx")}; ${s.setByOffset("global_idx","value")} - }`}},yx=e=>{Ym(e.inputs),e.compute(Jm(e.inputs))}}),Qm,$e,_x,wx,vx,xx,$x,Tx,Sx,Ix,Ox,eg,Ex,Ax,kx,Px,ui,Cx,_o,Dx,Rx,Nx,zx,Mx,Bx,Fx,Lx,jx,Ux,Vx,qx,Gx,Hx,Wx,Kx,Xx,Pa,Zx,ul,ll,Yx,Jx,Qx,tg,rg,e1,jl=D(()=>{"use strict";ae(),de(),Ve(),ce(),Qm=(e,t,n,o,u,l,d)=>{let c=Math.ceil(t/4),s="";typeof u=="string"?s=`${u}(a)`:s=u("a");let r=F("inputData",n,[c],4),i=re("outputData",o,[c],4),a=[{name:"vec_size",type:"u32"}];return d&&a.push(...d),` - ${e.registerUniforms(a).declareVariables(r,i)} + }`}},e1=e=>{Rg(e.inputs),e.compute(Dg(e.inputs))}}),Mg,Ee,t1,r1,n1,i1,o1,s1,a1,u1,l1,Ng,d1,c1,p1,h1,Si,f1,Fo,m1,g1,b1,y1,w1,_1,v1,x1,$1,T1,S1,I1,O1,E1,A1,k1,P1,iu,C1,Ml,Nl,R1,D1,M1,zg,Bg,N1,fd=D(()=>{"use strict";de(),pe(),He(),he(),Mg=(e,t,n,i,u,l,d)=>{let c=Math.ceil(t/4),s="";typeof u=="string"?s=`${u}(a)`:s=u("a");let r=j("inputData",n,[c],4),o=oe("outputData",i,[c],4),a=[{name:"vec_size",type:"u32"}];return d&&a.push(...d),` + ${e.registerUniforms(a).declareVariables(r,o)} ${l??""} @@ -2221,8 +2221,8 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} let a = ${r.getByOffset("global_idx")}; - ${i.setByOffset("global_idx",s)} - }`},$e=(e,t,n,o,u,l=e.dataType,d,c)=>{let s=[{type:12,data:Math.ceil(z.size(e.dims)/4)}];return d&&s.push(...d),{name:t,shaderCache:{hint:u,inputDependencies:["type"]},getShaderSource:r=>Qm(r,z.size(e.dims),e.dataType,l,n,o,c),getRunData:r=>({outputs:[{dims:e.dims,dataType:l}],dispatchGroup:{x:Math.ceil(z.size(r[0].dims)/64/4)},programUniforms:s})}},_x=e=>{e.compute($e(e.inputs[0],"Abs","abs"))},wx=e=>{e.compute($e(e.inputs[0],"Acos","acos"))},vx=e=>{e.compute($e(e.inputs[0],"Acosh","acosh"))},xx=e=>{e.compute($e(e.inputs[0],"Asin","asin"))},$x=e=>{e.compute($e(e.inputs[0],"Asinh","asinh"))},Tx=e=>{e.compute($e(e.inputs[0],"Atan","atan"))},Sx=e=>{e.compute($e(e.inputs[0],"Atanh","atanh"))},Ix=e=>Se(e),Ox=(e,t)=>{let n;switch(t.to){case 10:n="vec4";break;case 1:n="vec4";break;case 12:n="vec4";break;case 6:n="vec4";break;case 9:n="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute($e(e.inputs[0],"Cast",n,void 0,t.cacheKey,t.to))},eg=e=>{let t,n,o=e.length>=2&&e[1].data!==0,u=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=o?e[1].getFloat32Array()[0]:-34028234663852886e22,n=u?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=o?e[1].getUint16Array()[0]:64511,n=u?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return Se({min:t,max:n})},Ex=(e,t)=>{let n=t||eg(e.inputs),o=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Clip",u=>`clamp(${u}, vec4<${o}>(uniforms.min), vec4<${o}>(uniforms.max))`,void 0,n.cacheKey,void 0,[{type:e.inputs[0].dataType,data:n.min},{type:e.inputs[0].dataType,data:n.max}],[{name:"min",type:o},{name:"max",type:o}]),{inputs:[0]})},Ax=e=>{e.compute($e(e.inputs[0],"Ceil","ceil"))},kx=e=>{e.compute($e(e.inputs[0],"Cos","cos"))},Px=e=>{e.compute($e(e.inputs[0],"Cosh","cosh"))},ui=e=>Se(e),Cx=(e,t)=>{let n=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Elu",o=>`elu_vf32(${o})`,` + ${o.setByOffset("global_idx",s)} + }`},Ee=(e,t,n,i,u,l=e.dataType,d,c)=>{let s=[{type:12,data:Math.ceil(F.size(e.dims)/4)}];return d&&s.push(...d),{name:t,shaderCache:{hint:u,inputDependencies:["type"]},getShaderSource:r=>Mg(r,F.size(e.dims),e.dataType,l,n,i,c),getRunData:r=>({outputs:[{dims:e.dims,dataType:l}],dispatchGroup:{x:Math.ceil(F.size(r[0].dims)/64/4)},programUniforms:s})}},t1=e=>{e.compute(Ee(e.inputs[0],"Abs","abs"))},r1=e=>{e.compute(Ee(e.inputs[0],"Acos","acos"))},n1=e=>{e.compute(Ee(e.inputs[0],"Acosh","acosh"))},i1=e=>{e.compute(Ee(e.inputs[0],"Asin","asin"))},o1=e=>{e.compute(Ee(e.inputs[0],"Asinh","asinh"))},s1=e=>{e.compute(Ee(e.inputs[0],"Atan","atan"))},a1=e=>{e.compute(Ee(e.inputs[0],"Atanh","atanh"))},u1=e=>ke(e),l1=(e,t)=>{let n;switch(t.to){case 10:n="vec4";break;case 1:n="vec4";break;case 12:n="vec4";break;case 6:n="vec4";break;case 9:n="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(Ee(e.inputs[0],"Cast",n,void 0,t.cacheKey,t.to))},Ng=e=>{let t,n,i=e.length>=2&&e[1].data!==0,u=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=i?e[1].getFloat32Array()[0]:-34028234663852886e22,n=u?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=i?e[1].getUint16Array()[0]:64511,n=u?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return ke({min:t,max:n})},d1=(e,t)=>{let n=t||Ng(e.inputs),i=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Clip",u=>`clamp(${u}, vec4<${i}>(uniforms.min), vec4<${i}>(uniforms.max))`,void 0,n.cacheKey,void 0,[{type:e.inputs[0].dataType,data:n.min},{type:e.inputs[0].dataType,data:n.max}],[{name:"min",type:i},{name:"max",type:i}]),{inputs:[0]})},c1=e=>{e.compute(Ee(e.inputs[0],"Ceil","ceil"))},p1=e=>{e.compute(Ee(e.inputs[0],"Cos","cos"))},h1=e=>{e.compute(Ee(e.inputs[0],"Cosh","cosh"))},Si=e=>ke(e),f1=(e,t)=>{let n=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Elu",i=>`elu_vf32(${i})`,` const elu_alpha_ = ${n}(${t.alpha}); fn elu_f32(a: ${n}) -> ${n} { @@ -2231,7 +2231,7 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas fn elu_vf32(v: vec4<${n}>) -> vec4<${n}> { return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); - }`,t.cacheKey))},_o=(e="f32")=>` + }`,t.cacheKey))},Fo=(e="f32")=>` const r0: ${e} = 0.3275911; const r1: ${e} = 0.254829592; const r2: ${e} = -0.284496736; @@ -2243,15 +2243,15 @@ fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { let absv = abs(v); let x = 1.0 / (1.0 + r0 * absv); return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); -}`,Dx=e=>{let t=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Erf",n=>`erf_vf32(${n})`,_o(t)))},Rx=e=>{e.compute($e(e.inputs[0],"Exp","exp"))},Nx=e=>{e.compute($e(e.inputs[0],"Floor","floor"))},zx=e=>{let t=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Gelu",n=>`0.5 * ${n} * (1.0 + erf_vf32(${n} * 0.7071067811865475))`,_o(t)))},Mx=(e,t)=>{let n=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"LeakyRelu",o=>`select(leaky_relu_alpha_ * ${o}, ${o}, ${o} >= vec4<${n}>(0.0))`,`const leaky_relu_alpha_ = ${n}(${t.alpha});`,t.cacheKey))},Bx=e=>{e.compute($e(e.inputs[0],"Not",t=>`!${t}`))},Fx=e=>{e.compute($e(e.inputs[0],"Neg",t=>`-${t}`))},Lx=e=>{e.compute($e(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},jx=e=>{let t=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Relu",n=>`select(vec4<${t}>(0.0), ${n}, ${n} > vec4<${t}>(0.0))`))},Ux=e=>{e.compute($e(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},Vx=e=>Se(e),qx=(e,t)=>{let n=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"HardSigmoid",o=>`max(vec4<${n}>(0.0), min(vec4<${n}>(1.0), ${t.alpha} * ${o} + vec4<${n}>(${t.beta})))`,void 0,t.cacheKey))},Gx=e=>{let t=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"HardSwish",n=>`${n} * max(vec4<${t}>(0.0), min(vec4<${t}>(1.0), vec4<${t}>(${t}(1.0 / 6.0)) * ${n} + vec4<${t}>(0.5)))`))},Hx=e=>{e.compute($e(e.inputs[0],"Sin","sin"))},Wx=e=>{e.compute($e(e.inputs[0],"Sinh","sinh"))},Kx=e=>{e.compute($e(e.inputs[0],"Sqrt","sqrt"))},Xx=e=>{e.compute($e(e.inputs[0],"Tan","tan"))},Pa=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,Zx=e=>{e.compute($e(e.inputs[0],"Tanh",Pa))},ul=(e="f32")=>` +}`,m1=e=>{let t=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Erf",n=>`erf_vf32(${n})`,Fo(t)))},g1=e=>{e.compute(Ee(e.inputs[0],"Exp","exp"))},b1=e=>{e.compute(Ee(e.inputs[0],"Floor","floor"))},y1=e=>{let t=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Gelu",n=>`0.5 * ${n} * (1.0 + erf_vf32(${n} * 0.7071067811865475))`,Fo(t)))},w1=(e,t)=>{let n=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"LeakyRelu",i=>`select(leaky_relu_alpha_ * ${i}, ${i}, ${i} >= vec4<${n}>(0.0))`,`const leaky_relu_alpha_ = ${n}(${t.alpha});`,t.cacheKey))},_1=e=>{e.compute(Ee(e.inputs[0],"Not",t=>`!${t}`))},v1=e=>{e.compute(Ee(e.inputs[0],"Neg",t=>`-${t}`))},x1=e=>{e.compute(Ee(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},$1=e=>{let t=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Relu",n=>`select(vec4<${t}>(0.0), ${n}, ${n} > vec4<${t}>(0.0))`))},T1=e=>{e.compute(Ee(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},S1=e=>ke(e),I1=(e,t)=>{let n=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"HardSigmoid",i=>`max(vec4<${n}>(0.0), min(vec4<${n}>(1.0), ${t.alpha} * ${i} + vec4<${n}>(${t.beta})))`,void 0,t.cacheKey))},O1=e=>{let t=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"HardSwish",n=>`${n} * max(vec4<${t}>(0.0), min(vec4<${t}>(1.0), vec4<${t}>(${t}(1.0 / 6.0)) * ${n} + vec4<${t}>(0.5)))`))},E1=e=>{e.compute(Ee(e.inputs[0],"Sin","sin"))},A1=e=>{e.compute(Ee(e.inputs[0],"Sinh","sinh"))},k1=e=>{e.compute(Ee(e.inputs[0],"Sqrt","sqrt"))},P1=e=>{e.compute(Ee(e.inputs[0],"Tan","tan"))},iu=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,C1=e=>{e.compute(Ee(e.inputs[0],"Tanh",iu))},Ml=(e="f32")=>` const fast_gelu_a: ${e} = 0.5; const fast_gelu_b: ${e} = 0.7978845608028654; const fast_gelu_c: ${e} = 0.035677408136300125; fn tanh_v(v: vec4<${e}>) -> vec4<${e}> { - return ${Pa("v")}; + return ${iu("v")}; } -`,ll=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,Yx=e=>{let t=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"FastGelu",ll,ul(t),void 0,e.inputs[0].dataType))},Jx=(e,t)=>{let n=Ye(e.inputs[0].dataType);return e.compute($e(e.inputs[0],"ThresholdedRelu",o=>`select(vec4<${n}>(0.0), ${o}, ${o} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${n}>(${t.alpha});`,t.cacheKey)),0},Qx=e=>{e.compute($e(e.inputs[0],"Log","log"))},tg=(e,t)=>` +`,Nl=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,R1=e=>{let t=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"FastGelu",Nl,Ml(t),void 0,e.inputs[0].dataType))},D1=(e,t)=>{let n=rt(e.inputs[0].dataType);return e.compute(Ee(e.inputs[0],"ThresholdedRelu",i=>`select(vec4<${n}>(0.0), ${i}, ${i} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${n}>(${t.alpha});`,t.cacheKey)),0},M1=e=>{e.compute(Ee(e.inputs[0],"Log","log"))},zg=(e,t)=>` const alpha = vec4<${e}>(${t}); const one = ${e}(1.0); const zero = ${e}(0.0); @@ -2268,13 +2268,13 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { } return x * x1; } -`,rg=e=>`quick_gelu_impl(${e})`,e1=(e,t)=>{let n=Ye(e.inputs[0].dataType);e.compute($e(e.inputs[0],"QuickGelu",rg,tg(n,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),ng,ig,t1,YE=D(()=>{"use strict";de(),ce(),jl(),ng=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},ig=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let n=F("input",e[0].dataType,e[0].dims,4),o=F("bias",e[0].dataType,[e[0].dims[2]],4),u=re("output",e[0].dataType,t,4),l=z.size(t)/4,d=Je(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)}}),getShaderSource:c=>` +`,Bg=e=>`quick_gelu_impl(${e})`,N1=(e,t)=>{let n=rt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"QuickGelu",Bg,zg(n,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),Fg,Lg,z1,rk=D(()=>{"use strict";pe(),he(),fd(),Fg=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Lg=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let n=j("input",e[0].dataType,e[0].dims,4),i=j("bias",e[0].dataType,[e[0].dims[2]],4),u=oe("output",e[0].dataType,t,4),l=F.size(t)/4,d=nt(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)}}),getShaderSource:c=>` const M_SQRT2 = sqrt(2.0); const halfChannels = ${e[0].dims[2]/4/2}u; - ${c.declareVariables(n,o,u)} + ${c.declareVariables(n,i,u)} - ${_o(d)} + ${Fo(d)} ${c.mainStart()} ${c.guardAgainstOutOfBoundsWorkgroupSizes(l)} @@ -2286,40 +2286,40 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); ${u.setByOffset("global_idx","valueLeft * geluRight")} - }`}},t1=e=>{ng(e.inputs),e.compute(ig(e.inputs))}}),og,sg,kt,r1,n1,i1,o1,s1,a1,u1,l1,d1,c1,JE=D(()=>{"use strict";ae(),de(),ce(),og=(e,t,n,o,u,l,d,c,s,r,i,a)=>{let p,h;typeof c=="string"?p=h=(y,T)=>`${c}((${y}),(${T}))`:typeof c=="function"?p=h=c:(p=c.scalar,h=c.vector);let m=re("outputData",i,o.length,4),b=F("aData",s,t.length,4),$=F("bData",r,n.length,4),_;if(u)if(l){let y=z.size(t)===1,T=z.size(n)===1,S=t.length>0&&t[t.length-1]%4===0,O=n.length>0&&n[n.length-1]%4===0;y||T?_=m.setByOffset("global_idx",h(y?`${b.type.value}(${b.getByOffset("0")}.x)`:b.getByOffset("global_idx"),T?`${$.type.value}(${$.getByOffset("0")}.x)`:$.getByOffset("global_idx"))):_=` + }`}},z1=e=>{Fg(e.inputs),e.compute(Lg(e.inputs))}}),jg,Ug,Lt,B1,F1,L1,j1,U1,V1,q1,G1,H1,W1,nk=D(()=>{"use strict";de(),pe(),he(),jg=(e,t,n,i,u,l,d,c,s,r,o,a)=>{let p,h;typeof c=="string"?p=h=(y,T)=>`${c}((${y}),(${T}))`:typeof c=="function"?p=h=c:(p=c.scalar,h=c.vector);let m=oe("outputData",o,i.length,4),g=j("aData",s,t.length,4),_=j("bData",r,n.length,4),w;if(u)if(l){let y=F.size(t)===1,T=F.size(n)===1,S=t.length>0&&t[t.length-1]%4===0,O=n.length>0&&n[n.length-1]%4===0;y||T?w=m.setByOffset("global_idx",h(y?`${g.type.value}(${g.getByOffset("0")}.x)`:g.getByOffset("global_idx"),T?`${_.type.value}(${_.getByOffset("0")}.x)`:_.getByOffset("global_idx"))):w=` let outputIndices = ${m.offsetToIndices("global_idx * 4u")}; - let offsetA = ${b.broadcastedIndicesToOffset("outputIndices",m)}; - let offsetB = ${$.broadcastedIndicesToOffset("outputIndices",m)}; - ${m.setByOffset("global_idx",h(d||S?b.getByOffset("offsetA / 4u"):`${b.type.value}(${b.getByOffset("offsetA / 4u")}[offsetA % 4u])`,d||O?$.getByOffset("offsetB / 4u"):`${$.type.value}(${$.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} - `}else _=m.setByOffset("global_idx",h(b.getByOffset("global_idx"),$.getByOffset("global_idx")));else{if(!l)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let y=(T,S,O="")=>{let A=`aData[indexA${S}][componentA${S}]`,P=`bData[indexB${S}][componentB${S}]`;return` + let offsetA = ${g.broadcastedIndicesToOffset("outputIndices",m)}; + let offsetB = ${_.broadcastedIndicesToOffset("outputIndices",m)}; + ${m.setByOffset("global_idx",h(d||S?g.getByOffset("offsetA / 4u"):`${g.type.value}(${g.getByOffset("offsetA / 4u")}[offsetA % 4u])`,d||O?_.getByOffset("offsetB / 4u"):`${_.type.value}(${_.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} + `}else w=m.setByOffset("global_idx",h(g.getByOffset("global_idx"),_.getByOffset("global_idx")));else{if(!l)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let y=(T,S,O="")=>{let A=`aData[indexA${S}][componentA${S}]`,P=`bData[indexB${S}][componentB${S}]`;return` let outputIndices${S} = ${m.offsetToIndices(`global_idx * 4u + ${S}u`)}; - let offsetA${S} = ${b.broadcastedIndicesToOffset(`outputIndices${S}`,m)}; - let offsetB${S} = ${$.broadcastedIndicesToOffset(`outputIndices${S}`,m)}; + let offsetA${S} = ${g.broadcastedIndicesToOffset(`outputIndices${S}`,m)}; + let offsetB${S} = ${_.broadcastedIndicesToOffset(`outputIndices${S}`,m)}; let indexA${S} = offsetA${S} / 4u; let indexB${S} = offsetB${S} / 4u; let componentA${S} = offsetA${S} % 4u; let componentB${S} = offsetB${S} % 4u; ${T}[${S}] = ${O}(${p(A,P)}); - `};i===9?_=` + `};o===9?w=` var data = vec4(0); ${y("data",0,"u32")} ${y("data",1,"u32")} ${y("data",2,"u32")} ${y("data",3,"u32")} - outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:_=` + outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:w=` ${y("outputData[global_idx]",0)} ${y("outputData[global_idx]",1)} ${y("outputData[global_idx]",2)} ${y("outputData[global_idx]",3)} `}return` - ${e.registerUniform("vec_size","u32").declareVariables(b,$,m)} + ${e.registerUniform("vec_size","u32").declareVariables(g,_,m)} ${a??""} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} - ${_} - }`},sg=(e,t,n,o,u,l,d=n.dataType)=>{let c=n.dims.map(Number),s=o.dims.map(Number),r=!z.areEqual(c,s),i=c,a=z.size(c),p=!1,h=!1,m=[r];if(r){let b=xn.calcShape(c,s,!1);if(!b)throw new Error("Can't perform binary op on the given tensors");i=b.slice(),a=z.size(i);let $=z.size(c)===1,_=z.size(s)===1,y=c.length>0&&c[c.length-1]%4===0,T=s.length>0&&s[s.length-1]%4===0;m.push($),m.push(_),m.push(y),m.push(T);let S=1;for(let O=1;Ob.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:b=>og(b,c,s,i,p,r,h,u,n.dataType,o.dataType,d,l),getRunData:()=>({outputs:[{dims:i,dataType:d}],dispatchGroup:{x:Math.ceil(a/64/4)},programUniforms:[{type:12,data:Math.ceil(z.size(i)/4)},...se(c,s,i)]})}},kt=(e,t,n,o,u,l)=>{e.compute(sg(t,u??"",e.inputs[0],e.inputs[1],n,o,l))},r1=e=>{kt(e,"Add",(t,n)=>`${t}+${n}`)},n1=e=>{kt(e,"Div",(t,n)=>`${t}/${n}`)},i1=e=>{kt(e,"Equal",{scalar:(t,n)=>`u32(${t}==${n})`,vector:(t,n)=>`vec4(${t}==${n})`},void 0,void 0,9)},o1=e=>{kt(e,"Mul",(t,n)=>`${t}*${n}`)},s1=e=>{let t=F("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;kt(e,"Pow",{scalar:(n,o)=>`pow_custom(${n},${o})`,vector:(n,o)=>`pow_vector_custom(${n},${o})`},` + ${w} + }`},Ug=(e,t,n,i,u,l,d=n.dataType)=>{let c=n.dims.map(Number),s=i.dims.map(Number),r=!F.areEqual(c,s),o=c,a=F.size(c),p=!1,h=!1,m=[r];if(r){let g=Bn.calcShape(c,s,!1);if(!g)throw new Error("Can't perform binary op on the given tensors");o=g.slice(),a=F.size(o);let _=F.size(c)===1,w=F.size(s)===1,y=c.length>0&&c[c.length-1]%4===0,T=s.length>0&&s[s.length-1]%4===0;m.push(_),m.push(w),m.push(y),m.push(T);let S=1;for(let O=1;Og.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:g=>jg(g,c,s,o,p,r,h,u,n.dataType,i.dataType,d,l),getRunData:()=>({outputs:[{dims:o,dataType:d}],dispatchGroup:{x:Math.ceil(a/64/4)},programUniforms:[{type:12,data:Math.ceil(F.size(o)/4)},...ue(c,s,o)]})}},Lt=(e,t,n,i,u,l)=>{e.compute(Ug(t,u??"",e.inputs[0],e.inputs[1],n,i,l))},B1=e=>{Lt(e,"Add",(t,n)=>`${t}+${n}`)},F1=e=>{Lt(e,"Div",(t,n)=>`${t}/${n}`)},L1=e=>{Lt(e,"Equal",{scalar:(t,n)=>`u32(${t}==${n})`,vector:(t,n)=>`vec4(${t}==${n})`},void 0,void 0,9)},j1=e=>{Lt(e,"Mul",(t,n)=>`${t}*${n}`)},U1=e=>{let t=j("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;Lt(e,"Pow",{scalar:(n,i)=>`pow_custom(${n},${i})`,vector:(n,i)=>`pow_vector_custom(${n},${i})`},` fn pow_custom(a : ${t}, b : ${t}) -> ${t} { if (b == ${t}(0.0)) { return ${t}(1.0); @@ -2332,7 +2332,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { // TODO: implement vectorized pow return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); } - `)},a1=e=>{kt(e,"Sub",(t,n)=>`${t}-${n}`)},u1=e=>{kt(e,"Greater",{scalar:(t,n)=>`u32(${t}>${n})`,vector:(t,n)=>`vec4(${t}>${n})`},void 0,void 0,9)},l1=e=>{kt(e,"Less",{scalar:(t,n)=>`u32(${t}<${n})`,vector:(t,n)=>`vec4(${t}<${n})`},void 0,void 0,9)},d1=e=>{kt(e,"GreaterOrEqual",{scalar:(t,n)=>`u32(${t}>=${n})`,vector:(t,n)=>`vec4(${t}>=${n})`},void 0,void 0,9)},c1=e=>{kt(e,"LessOrEqual",{scalar:(t,n)=>`u32(${t}<=${n})`,vector:(t,n)=>`vec4(${t}<=${n})`},void 0,void 0,9)}}),ag,ug,lg,dg,p1,h1,QE=D(()=>{"use strict";ae(),de(),Ve(),ce(),ag=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let n=0,o=e[n],u=o.dataType,l=o.dims.length;e.forEach((d,c)=>{if(c!==n){if(d.dataType!==u)throw new Error("input tensors should be one type");if(d.dims.length!==l)throw new Error("input tensors should have the same shape");d.dims.forEach((s,r)=>{if(r!==t&&s!==o.dims[r])throw new Error("non concat dimensions must match")})}})},ug=(e,t)=>` + `)},V1=e=>{Lt(e,"Sub",(t,n)=>`${t}-${n}`)},q1=e=>{Lt(e,"Greater",{scalar:(t,n)=>`u32(${t}>${n})`,vector:(t,n)=>`vec4(${t}>${n})`},void 0,void 0,9)},G1=e=>{Lt(e,"Less",{scalar:(t,n)=>`u32(${t}<${n})`,vector:(t,n)=>`vec4(${t}<${n})`},void 0,void 0,9)},H1=e=>{Lt(e,"GreaterOrEqual",{scalar:(t,n)=>`u32(${t}>=${n})`,vector:(t,n)=>`vec4(${t}>=${n})`},void 0,void 0,9)},W1=e=>{Lt(e,"LessOrEqual",{scalar:(t,n)=>`u32(${t}<=${n})`,vector:(t,n)=>`vec4(${t}<=${n})`},void 0,void 0,9)}}),Vg,qg,Gg,Hg,K1,X1,ik=D(()=>{"use strict";de(),pe(),He(),he(),Vg=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let n=0,i=e[n],u=i.dataType,l=i.dims.length;e.forEach((d,c)=>{if(c!==n){if(d.dataType!==u)throw new Error("input tensors should be one type");if(d.dims.length!==l)throw new Error("input tensors should have the same shape");d.dims.forEach((s,r)=>{if(r!==t&&s!==i.dims[r])throw new Error("non concat dimensions must match")})}})},qg=(e,t)=>` fn calculateInputIndex(index: u32) -> u32 { let sizeInConcatAxis = array(${t}); for (var i: u32 = 0u; i < ${e}; i += 1u ) { @@ -2341,15 +2341,15 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { } } return ${e}u; - }`,lg=(e,t)=>{let n=e.length,o=[];for(let u=0;u{let u=z.size(n),l=new Array(e.length),d=new Array(e.length),c=0,s=[],r=[],i=[{type:12,data:u}];for(let b=0;b`uniforms.sizeInConcatAxis${b}`).join(","),m=b=>` + }`,Gg=(e,t)=>{let n=e.length,i=[];for(let u=0;u{let u=F.size(n),l=new Array(e.length),d=new Array(e.length),c=0,s=[],r=[],o=[{type:12,data:u}];for(let g=0;g`uniforms.sizeInConcatAxis${g}`).join(","),m=g=>` - ${(()=>{b.registerUniform("outputSize","u32");for(let $=0;${g.registerUniform("outputSize","u32");for(let _=0;_) -> vec4<${e}> { ${p} -= sizeInConcatAxis[inputIndex - 1u]; } - ${lg(d,a)} - }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:s},getRunData:()=>({outputs:[{dims:n,dataType:o}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:i}),getShaderSource:m}},p1=(e,t)=>{let n=e.inputs,o=n[0].dims,u=z.normalizeAxis(t.axis,o.length);ag(n,u);let l=o.slice();l[u]=n.reduce((c,s)=>c+(s.dims.length>u?s.dims[u]:0),0);let d=n.filter(c=>z.size(c.dims)>0);e.compute(dg(d,u,l,n[0].dataType),{inputs:d})},h1=e=>Se({axis:e.axis})}),en,tn,rn,Ul,an=D(()=>{"use strict";ae(),de(),en=(e,t,n="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${n}(uniforms.clip_min)), ${t}(${n}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${n}(uniforms.alpha) * value + ${n}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${n}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); + ${Gg(d,a)} + }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:s},getRunData:()=>({outputs:[{dims:n,dataType:i}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:o}),getShaderSource:m}},K1=(e,t)=>{let n=e.inputs,i=n[0].dims,u=F.normalizeAxis(t.axis,i.length);Vg(n,u);let l=i.slice();l[u]=n.reduce((c,s)=>c+(s.dims.length>u?s.dims[u]:0),0);let d=n.filter(c=>F.size(c.dims)>0);e.compute(Hg(d,u,l,n[0].dataType),{inputs:d})},X1=e=>ke({axis:e.axis})}),gn,bn,yn,md,xn=D(()=>{"use strict";de(),pe(),gn=(e,t,n="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${n}(uniforms.clip_min)), ${t}(${n}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${n}(uniforms.alpha) * value + ${n}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${n}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); value = sign(value) * (1.0 - e2x) / (1.0 + e2x); - `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},tn=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},rn=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},Ul=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[n,o]=e?.activation_params||[.2,.5];return{activation:t,alpha:n,beta:o}}else if(t==="Clip"){let[n,o]=e?.activation_params||[Bv,Fv];return{activation:t,clipMax:o,clipMin:n}}else if(t==="LeakyRelu"){let[n]=e?.activation_params||[.01];return{activation:t,alpha:n}}return{activation:t}}}),tt,f1,Vl=D(()=>{"use strict";tt=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},f1=e=>` + `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},bn=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},yn=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},md=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[n,i]=e?.activation_params||[.2,.5];return{activation:t,alpha:n,beta:i}}else if(t==="Clip"){let[n,i]=e?.activation_params||[_x,vx];return{activation:t,clipMax:i,clipMin:n}}else if(t==="LeakyRelu"){let[n]=e?.activation_params||[.01];return{activation:t,alpha:n}}return{activation:t}}}),at,Z1,gd=D(()=>{"use strict";at=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},Z1=e=>` ${e?"value = value + getBiasByOutputCoords(coords);":""} - `}),m1,eA=D(()=>{"use strict";m1=e=>` + `}),Y1,ok=D(()=>{"use strict";Y1=e=>` fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { return dot(coords, vec4( shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); @@ -2373,18 +2373,18 @@ fn getOutputIndexFromCoords(coords : vec4) -> i32 { return dot(coords, vec4( i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); } -`}),fi,ql,Gl=D(()=>{"use strict";ae(),de(),ce(),an(),fi=(e,t,n,o,u)=>{let l=o-n;return` +`}),Pi,bd,yd=D(()=>{"use strict";de(),pe(),he(),xn(),Pi=(e,t,n,i,u)=>{let l=i-n;return` ${Array.from({length:n}).map((d,c)=>` - if (${ie(t.shape,c,t.rank)} != 1) { - ${t.indicesSet(e,c,ie(u,c+l,o))} + if (${se(t.shape,c,t.rank)} != 1) { + ${t.indicesSet(e,c,se(u,c+l,i))} } else { ${t.indicesSet(e,c,0)} }`).join("")} -`},ql=(e,t,n,o,u=!1,l)=>{let d=e[0].dims,c=e[1].dims,s=d[d.length-2],r=c[c.length-1],i=d[d.length-1],a=Ue(r),p=Ue(i),h=Ue(s),m=z.size(n)/a/h,b=e.length>2,$=o?o.slice(0,-2):n.slice(0,-2),_=[z.size($),s,r],y=[{type:12,data:m},{type:12,data:s},{type:12,data:r},{type:12,data:i}];tn(t,y),y.push(...se($,d,c)),b&&y.push(...se(e[2].dims)),y.push(...se(_));let T=S=>{let O=Bl("batch_dims",e[0].dataType,$.length),A=F("a",e[0].dataType,d.length,p),P=F("b",e[1].dataType,c.length,a),I=re("output",e[0].dataType,_.length,a),L=Je(I.type.tensor),V=en(t,I.type.value,L),Z=[A,P],q="";if(b){let w=u?a:1;Z.push(F("bias",e[2].dataType,e[2].dims.length,w)),q=`${u?`value += bias[col / ${w}];`:`value += ${I.type.value}(bias[row + i]);`}`}let N=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];rn(t,N);let ue=()=>{let w=`var a_data: ${A.type.value};`;for(let C=0;C{let d=e[0].dims,c=e[1].dims,s=d[d.length-2],r=c[c.length-1],o=d[d.length-1],a=Ge(r),p=Ge(o),h=Ge(s),m=F.size(n)/a/h,g=e.length>2,_=i?i.slice(0,-2):n.slice(0,-2),w=[F.size(_),s,r],y=[{type:12,data:m},{type:12,data:s},{type:12,data:r},{type:12,data:o}];bn(t,y),y.push(...ue(_,d,c)),g&&y.push(...ue(e[2].dims)),y.push(...ue(w));let T=S=>{let O=cd("batch_dims",e[0].dataType,_.length),A=j("a",e[0].dataType,d.length,p),P=j("b",e[1].dataType,c.length,a),I=oe("output",e[0].dataType,w.length,a),z=nt(I.type.tensor),G=gn(t,I.type.value,z),J=[A,P],q="";if(g){let v=u?a:1;J.push(j("bias",e[2].dataType,e[2].dims.length,v)),q=`${u?`value += bias[col / ${v}];`:`value += ${I.type.value}(bias[row + i]);`}`}let C=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];yn(t,C);let ee=()=>{let v=`var a_data: ${A.type.value};`;for(let R=0;R) -> i32 { ${n.length===2?"":`let batch_indices = ${O.offsetToIndices("batch")};`} var a_indices: ${A.type.indices}; - ${fi("a_indices",A,A.rank-2,O.rank,"batch_indices")} + ${Pi("a_indices",A,A.rank-2,O.rank,"batch_indices")} ${A.indicesSet("a_indices",A.rank-2,0)} ${A.indicesSet("a_indices",A.rank-1,0)} let a_offset = ${A.indicesToOffset("a_indices")}; var b_indices: ${P.type.indices}; - ${fi("b_indices",P,P.rank-2,O.rank,"batch_indices")} + ${Pi("b_indices",P,P.rank-2,O.rank,"batch_indices")} ${P.indicesSet("b_indices",P.rank-2,0)} ${P.indicesSet("b_indices",P.rank-1,0)} let b_offset = ${P.indicesToOffset("b_indices")}; var values: array<${I.type.value}, ${h}>; for (var k: u32 = 0u; k < uniforms.K; k = k + ${p}) { - ${ue()} + ${ee()} } for (var i = 0u; i < ${h}u; i++) { var value = values[i]; ${q} - ${V} + ${G} let cur_indices = ${I.type.indices}(batch, row + i, col); let offset = ${I.indicesToOffset("cur_indices")}; ${I.setByOffset(`offset / ${a}`,"value")}; } } - `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${a};${p};${h};${u}`,inputDependencies:b?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:y}),getShaderSource:T}}}),cg,pg,dl,Ca,hg,cl,fg,Ao,Hl=D(()=>{"use strict";ae(),de(),ce(),an(),Gl(),Vl(),cg=(e,t)=>e?` + `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${a};${p};${h};${u}`,inputDependencies:g?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:y}),getShaderSource:T}}}),Wg,Kg,zl,ou,Xg,Bl,Zg,Xo,wd=D(()=>{"use strict";de(),pe(),he(),xn(),yd(),gd(),Wg=(e,t)=>e?` mm_Asub[inputRow][inputCol] = mm_readA(batch, kStart + inputRow, globalRowStart / innerElementSize + inputCol${t?", batchIndices":""}); @@ -2427,7 +2427,7 @@ fn getOutputIndexFromCoords(coords : vec4) -> i32 { mm_Asub[inputRow][inputCol] = mm_readA(batch, globalRow + innerRow, kStart / innerElementSize + inputCol${t?", batchIndices":""}); - `,pg=(e,t)=>e?` + `,Kg=(e,t)=>e?` let ACached0 = mm_Asub[k * innerElementSize][localRow]; let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; @@ -2444,10 +2444,10 @@ fn getOutputIndexFromCoords(coords : vec4) -> i32 { acc[i] = BCached1 * ACached.y + acc[i]; acc[i] = BCached2 * ACached.z + acc[i]; ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"} - }`,dl=(e,t,n="f32",o,u=!1,l=32,d=!1,c=32)=>{let s=t[1]*e[1],r=t[0]*e[0],i=u?s:l,a=u?l:s,p=i/t[0],h=l/t[1];if(!((u&&p===4&&e[1]===4||!u&&(p===3||p===4))&&i%t[0]===0&&l%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${u} is true, innerElementSize ${p} and workPerThread[1] ${e[1]} must be 4. + }`,zl=(e,t,n="f32",i,u=!1,l=32,d=!1,c=32)=>{let s=t[1]*e[1],r=t[0]*e[0],o=u?s:l,a=u?l:s,p=o/t[0],h=l/t[1];if(!((u&&p===4&&e[1]===4||!u&&(p===3||p===4))&&o%t[0]===0&&l%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${u} is true, innerElementSize ${p} and workPerThread[1] ${e[1]} must be 4. Otherwise, innerElementSize ${p} must be 3 or 4. - tileAWidth ${i} must be divisible by workgroupSize[0]${t[0]}. tileInner ${l} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` -var mm_Asub: array, ${i/p}>, ${a}>; + tileAWidth ${o} must be divisible by workgroupSize[0]${t[0]}. tileInner ${l} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` +var mm_Asub: array, ${o/p}>, ${a}>; var mm_Bsub: array, ${r/e[0]}>, ${l}>; const rowPerThread = ${e[1]}; @@ -2466,7 +2466,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let globalRow =i32(globalId.y) * rowPerThread; let globalCol = i32(globalId.x); let batch = ${d?"0":"i32(globalId.z)"}; - ${o?`let batchIndices = ${o.offsetToIndices("u32(batch)")};`:""} + ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""} let globalRowStart = i32(workgroupId.y) * ${s}; let num_tiles = ${d?`${Math.ceil(c/l)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; @@ -2481,14 +2481,14 @@ fn main(@builtin(local_invocation_id) localId : vec3, for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { let inputRow = tileRow + innerRow; let inputCol = tileCol; - ${cg(u,o)} + ${Wg(u,i)} } // Load one tile of B into local memory. for (var innerRow = 0; innerRow < ${h}; innerRow = innerRow + 1) { let inputRow = tileRowB + innerRow; let inputCol = tileCol; - mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${o?", batchIndices":""}); + mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${i?", batchIndices":""}); } kStart = kStart + tileInner; workgroupBarrier(); @@ -2500,7 +2500,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; ${p===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} - ${pg(u,p)} + ${Kg(u,p)} } workgroupBarrier(); @@ -2509,7 +2509,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); } -}`},Ca=(e,t)=>e?` +}`},ou=(e,t)=>e?` mm_Asub[inputRow][inputCol] = mm_readA(batch, kStart + inputRow, globalRowStart + inputCol${t?", batchIndices":""}); @@ -2517,26 +2517,26 @@ fn main(@builtin(local_invocation_id) localId : vec3, mm_Asub[inputRow][inputCol] = mm_readA(batch, globalRowStart + inputRow, kStart + inputCol${t?", batchIndices":""}); - `,hg=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",cl=(e,t,n="f32",o,u=!1,l=32,d=!1,c=32,s=!1)=>{let r=e[1]*t[1],i=e[0]*t[0],a=u?r:l,p=u?l:r;if(!(p%t[1]===0&&a%t[0]===0&&l%t[1]===0))throw new Error(`tileAHight ${p} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${a} must be divisible by workgroupSize[0]${t[0]}, tileInner ${l} must be divisible by workgroupSize[1]${t[1]}`);let h=p/t[1],m=a/t[0],b=l/t[1],$=s?` + `,Xg=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",Bl=(e,t,n="f32",i,u=!1,l=32,d=!1,c=32,s=!1)=>{let r=e[1]*t[1],o=e[0]*t[0],a=u?r:l,p=u?l:r;if(!(p%t[1]===0&&a%t[0]===0&&l%t[1]===0))throw new Error(`tileAHight ${p} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${a} must be divisible by workgroupSize[0]${t[0]}, tileInner ${l} must be divisible by workgroupSize[1]${t[1]}`);let h=p/t[1],m=a/t[0],g=l/t[1],_=s?` let localRow = i32(localId.y); let localCol = i32(localId.x); let globalRowStart = i32(workgroupId.y) * ${r}; - let globalColStart = i32(workgroupId.x) * ${i}; + let globalColStart = i32(workgroupId.x) * ${o}; // Loop over shared dimension. for (var t = 0; t < num_tiles; t = t + 1) { // Load one tile of A into local memory. for (var inputRow = localRow; inputRow < ${p}; inputRow = inputRow + ${t[1]}) { for (var inputCol = localCol; inputCol < ${a}; inputCol = inputCol + ${t[0]}) { - ${Ca(u,o)} + ${ou(u,i)} } } // Load one tile of B into local memory. for (var inputRow = localRow; inputRow < ${l}; inputRow = inputRow + ${t[1]}) { - for (var inputCol = localCol; inputCol < ${i}; inputCol = inputCol + ${t[0]}) { + for (var inputCol = localCol; inputCol < ${o}; inputCol = inputCol + ${t[0]}) { mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, - globalColStart + inputCol${o?", batchIndices":""}); + globalColStart + inputCol${i?", batchIndices":""}); } } kStart = kStart + tileInner; @@ -2575,7 +2575,7 @@ let globalRowStart = i32(workgroupId.y) * ${r}; let tileRowA = i32(localId.y) * ${h}; let tileColA = i32(localId.x) * ${m}; -let tileRowB = i32(localId.y) * ${b}; +let tileRowB = i32(localId.y) * ${g}; // Loop over shared dimension. for (var t = 0; t < num_tiles; t = t + 1) { // Load one tile of A into local memory. @@ -2583,18 +2583,18 @@ for (var t = 0; t < num_tiles; t = t + 1) { for (var innerCol = 0; innerCol < ${m}; innerCol = innerCol + 1) { let inputRow = tileRowA + innerRow; let inputCol = tileColA + innerCol; - ${Ca(u,o)} + ${ou(u,i)} } } // Load one tile of B into local memory. - for (var innerRow = 0; innerRow < ${b}; innerRow = innerRow + 1) { + for (var innerRow = 0; innerRow < ${g}; innerRow = innerRow + 1) { for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { let inputRow = tileRowB + innerRow; let inputCol = tileCol + innerCol; mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, - globalCol + innerCol${o?", batchIndices":""}); + globalCol + innerCol${i?", batchIndices":""}); } } kStart = kStart + tileInner; @@ -2608,7 +2608,7 @@ for (var t = 0; t < num_tiles; t = t + 1) { } for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - ${hg(u)} + ${Xg(u)} for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; } @@ -2626,7 +2626,7 @@ for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { } `;return` var mm_Asub : array, ${p}>; - var mm_Bsub : array, ${l}>; + var mm_Bsub : array, ${l}>; const rowPerThread = ${e[1]}; const colPerThread = ${e[0]}; const tileInner = ${l}; @@ -2636,21 +2636,21 @@ fn main(@builtin(local_invocation_id) localId : vec3, @builtin(global_invocation_id) globalId : vec3, @builtin(workgroup_id) workgroupId : vec3) { let batch = ${d?"0":"i32(globalId.z)"}; - ${o?`let batchIndices = ${o.offsetToIndices("u32(batch)")};`:""} + ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""} let num_tiles = ${d?`${Math.ceil(c/l)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; var kStart = ${d?`i32(globalId.z) * ${c}`:"0"}; var acc : array, rowPerThread>; - ${$} + ${_} } -`},fg=(e,t,n,o,u=!1)=>{let[l,d,c,s]=o,r=Je(o[0].type.tensor);return` - fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${tt(e,r)} { - var value = ${tt(e,r)}(0.0); +`},Zg=(e,t,n,i,u=!1)=>{let[l,d,c,s]=i,r=nt(i[0].type.tensor);return` + fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${at(e,r)} { + var value = ${at(e,r)}(0.0); let col = colIn * ${e}; if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) { var aIndices: ${d.type.indices}; - ${fi("aIndices",d,d.rank-2,l.rank,"batchIndices")} + ${Pi("aIndices",d,d.rank-2,l.rank,"batchIndices")} ${d.indicesSet("aIndices",d.rank-2,"u32(row)")} ${d.indicesSet("aIndices",d.rank-1,"u32(colIn)")} value = ${d.getByIndices("aIndices")}; @@ -2658,13 +2658,13 @@ fn main(@builtin(local_invocation_id) localId : vec3, return value; } - fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${tt(e,r)} { - var value = ${tt(e,r)}(0.0); + fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${at(e,r)} { + var value = ${at(e,r)}(0.0); let col = colIn * ${e}; if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) { var bIndices: ${c.type.indices}; - ${fi("bIndices",c,c.rank-2,l.rank,"batchIndices")} + ${Pi("bIndices",c,c.rank-2,l.rank,"batchIndices")} ${c.indicesSet("bIndices",c.rank-2,"u32(row)")} ${c.indicesSet("bIndices",c.rank-1,"u32(colIn)")} value = ${c.getByIndices("bIndices")}; @@ -2672,21 +2672,21 @@ fn main(@builtin(local_invocation_id) localId : vec3, return value; } - fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${tt(e,r)}) { + fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${at(e,r)}) { let col = colIn * ${e}; if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { var value = valueIn; let coords = vec3(batch, row, colIn); - ${t?`value = value + ${u?"bias[colIn]":`${tt(e,r)}(bias[row])`};`:""} + ${t?`value = value + ${u?"bias[colIn]":`${at(e,r)}(bias[row])`};`:""} ${n} ${s.setByIndices("vec3(coords)","value")} } } - `},Ao=(e,t,n,o,u=!1,l)=>{let d=e[0].dims,c=e[1].dims,s=d.slice(0,-2),r=c.slice(0,-2),i=o?o.slice(0,-2):n.slice(0,-2),a=z.size(i),p=d[d.length-2],h=d[d.length-1],m=c[c.length-1],b=h%4===0&&m%4===0,$=p<=8?[4,1,1]:[4,4,1],_=[8,8,1],y=[Math.ceil(m/_[0]/$[0]),Math.ceil(p/_[1]/$[1]),Math.ceil(a/_[2]/$[2])],T=b?4:1,S=[...s,p,h/T],O=S.length,A=[...r,h,m/T],P=A.length,I=[a,p,m/T],L=[{type:6,data:p},{type:6,data:m},{type:6,data:h}];tn(t,L),L.push(...se(i,S,A));let V=["rank","rank"],Z=e.length>2;Z&&(L.push(...se(e[2].dims)),V.push("rank")),L.push(...se(I));let q=N=>{let ue=i.length,w=Bl("batchDims",e[0].dataType,ue,1),C=Je(e[0].dataType),U=F("a",e[0].dataType,O,T),H=F("b",e[1].dataType,P,T),G=re("result",e[0].dataType,I.length,T),J=[U,H];if(Z){let ne=u?T:1;J.push(F("bias",e[2].dataType,e[2].dims.length,ne))}let B=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];rn(t,B);let X=Je(G.type.tensor),K=en(t,G.type.value,X),M=fg(T,Z,K,[w,U,H,G],u);return` - ${N.registerUniforms(B).registerInternalVariables(w).declareVariables(...J,G)} - ${M} - ${b?dl($,_,C,w):cl($,_,C,w)} - `};return{name:"MatMul",shaderCache:{hint:`${$};${t.activation};${b};${u}`,inputDependencies:V},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:y[0],y:y[1],z:y[2]},programUniforms:L}),getShaderSource:q}}}),mg,g1,tA=D(()=>{"use strict";ae(),Yt(),ce(),an(),Vl(),eA(),Hl(),mg=(e,t,n,o,u=!1,l,d=4,c=4,s=4,r="f32")=>{let i=L=>{switch(L){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${r}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},a=L=>{switch(L){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},p=e?` + `},Xo=(e,t,n,i,u=!1,l)=>{let d=e[0].dims,c=e[1].dims,s=d.slice(0,-2),r=c.slice(0,-2),o=i?i.slice(0,-2):n.slice(0,-2),a=F.size(o),p=d[d.length-2],h=d[d.length-1],m=c[c.length-1],g=h%4===0&&m%4===0,_=p<=8?[4,1,1]:[4,4,1],w=[8,8,1],y=[Math.ceil(m/w[0]/_[0]),Math.ceil(p/w[1]/_[1]),Math.ceil(a/w[2]/_[2])],T=g?4:1,S=[...s,p,h/T],O=S.length,A=[...r,h,m/T],P=A.length,I=[a,p,m/T],z=[{type:6,data:p},{type:6,data:m},{type:6,data:h}];bn(t,z),z.push(...ue(o,S,A));let G=["rank","rank"],J=e.length>2;J&&(z.push(...ue(e[2].dims)),G.push("rank")),z.push(...ue(I));let q=C=>{let ee=o.length,v=cd("batchDims",e[0].dataType,ee,1),R=nt(e[0].dataType),H=j("a",e[0].dataType,O,T),L=j("b",e[1].dataType,P,T),V=oe("result",e[0].dataType,I.length,T),K=[H,L];if(J){let X=u?T:1;K.push(j("bias",e[2].dataType,e[2].dims.length,X))}let B=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];yn(t,B);let te=nt(V.type.tensor),Y=gn(t,V.type.value,te),N=Zg(T,J,Y,[v,H,L,V],u);return` + ${C.registerUniforms(B).registerInternalVariables(v).declareVariables(...K,V)} + ${N} + ${g?zl(_,w,R,v):Bl(_,w,R,v)} + `};return{name:"MatMul",shaderCache:{hint:`${_};${t.activation};${g};${u}`,inputDependencies:G},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:y[0],y:y[1],z:y[2]},programUniforms:z}),getShaderSource:q}}}),Yg,J1,sk=D(()=>{"use strict";de(),ur(),he(),xn(),gd(),ok(),wd(),Yg=(e,t,n,i,u=!1,l,d=4,c=4,s=4,r="f32")=>{let o=z=>{switch(z){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${r}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${z} is not supported.`)}},a=z=>{switch(z){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${z} is not supported.`)}},p=e?` let coord = vec4(batch, xRow, xCol, xCh); `:` let coord = vec4(batch, xCh, xRow, xCol); @@ -2702,50 +2702,50 @@ fn main(@builtin(local_invocation_id) localId : vec3, row, col / outWidth, col % outWidth); - `,m=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",b=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",$=e?"row":"col",_=e?"col":"row",y=` + `,m=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",g=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",_=e?"row":"col",w=e?"col":"row",y=` let inChannels = i32(uniforms.w_shape[2]); let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; - let outRow = ${$} / outWidth; - let outCol = ${$} % outWidth; + let outRow = ${_} / outWidth; + let outCol = ${_} % outWidth; - let WRow = ${_} / (i32(uniforms.w_shape[1]) * inChannels); - let WCol = ${_} / inChannels % i32(uniforms.w_shape[1]); + let WRow = ${w} / (i32(uniforms.w_shape[1]) * inChannels); + let WCol = ${w} / inChannels % i32(uniforms.w_shape[1]); let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; - let xCh = ${_} % inChannels; - var resData = ${tt(d,r)}(0.0); + let xCh = ${w} % inChannels; + var resData = ${at(d,r)}(0.0); // The bounds checking is always needed since we use it to pad zero for // the 'same' padding type. - if (xRow >= 0 && xRow < ${m} && xCol >= 0 && xCol < ${b}) { + if (xRow >= 0 && xRow < ${m} && xCol >= 0 && xCol < ${g}) { ${p} let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); - ${i(d)} + ${o(d)} } - return resData;`,T=e?t&&o?` + return resData;`,T=e?t&&i?` let col = colIn * ${d}; ${y}`:` let col = colIn * ${d}; if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { ${y} } - return ${tt(d,r)}(0.0);`:o&&n?` + return ${at(d,r)}(0.0);`:i&&n?` let col = colIn * ${d}; ${y}`:` let col = colIn * ${d}; if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { ${y} } - return ${tt(d,r)}(0.0);`,S=e?o&&n?a(c):` + return ${at(d,r)}(0.0);`,S=e?i&&n?a(c):` let col = colIn * ${c}; if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { ${a(c)} } - return ${tt(c,r)}(0.0);`:` + return ${at(c,r)}(0.0);`:` let col = colIn * ${c}; if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) { ${a(c)} } - return ${tt(c,r)}(0.0);`,O=tt(s,r),A=tt(e?d:c,r),P=tt(e?c:d,r),I=en(l,O,r);return` + return ${at(c,r)}(0.0);`,O=at(s,r),A=at(e?d:c,r),P=at(e?c:d,r),I=gn(l,O,r);return` fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${A} { ${e?T:S} } @@ -2761,33 +2761,33 @@ fn main(@builtin(local_invocation_id) localId : vec3, var value = valueIn; let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; ${h} - ${f1(u)} + ${Z1(u)} ${I} setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); } - }`},g1=(e,t,n,o,u,l,d,c,s)=>{let r=t.format==="NHWC",i=r?e[0].dims[3]:e[0].dims[1],a=n[0],p=r?n[2]:n[3],h=r?n[1]:n[2],m=r?n[3]:n[1],b=r&&(i%4===0||i%3===0)&&m%4===0,$=r?m:p*h,_=r?p*h:m,y=[8,8,1],T=o<=8?[4,1,1]:[4,4,1],S=[Math.ceil($/y[0]/T[0]),Math.ceil(_/y[1]/T[1]),Math.ceil(a/y[2]/T[2])];ve("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${S}`);let O=b?r&&i%4!==0?3:4:1,A=y[1]*T[1],P=y[0]*T[0],I=Math.max(y[0]*O,y[1]),L=o%A===0,V=u%P===0,Z=l%I===0,q=b?[O,4,4]:[1,1,1],N=[{type:6,data:o},{type:6,data:u},{type:6,data:l},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];tn(t,N),N.push(...se(e[0].dims,e[1].dims));let ue=["rank","rank"];d&&(N.push(...se(e[2].dims)),ue.push("rank")),N.push(...se(n));let w=C=>{let U=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];rn(t,U);let H=b?4:1,G=Je(e[0].dataType),J=` - fn setOutputAtIndex(flatIndex : i32, value : ${b?`vec4<${G}>`:G}) { - result[flatIndex] = ${b?`vec4<${G}>`:G}(value); + }`},J1=(e,t,n,i,u,l,d,c,s)=>{let r=t.format==="NHWC",o=r?e[0].dims[3]:e[0].dims[1],a=n[0],p=r?n[2]:n[3],h=r?n[1]:n[2],m=r?n[3]:n[1],g=r&&(o%4===0||o%3===0)&&m%4===0,_=r?m:p*h,w=r?p*h:m,y=[8,8,1],T=i<=8?[4,1,1]:[4,4,1],S=[Math.ceil(_/y[0]/T[0]),Math.ceil(w/y[1]/T[1]),Math.ceil(a/y[2]/T[2])];Ie("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${S}`);let O=g?r&&o%4!==0?3:4:1,A=y[1]*T[1],P=y[0]*T[0],I=Math.max(y[0]*O,y[1]),z=i%A===0,G=u%P===0,J=l%I===0,q=g?[O,4,4]:[1,1,1],C=[{type:6,data:i},{type:6,data:u},{type:6,data:l},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];bn(t,C),C.push(...ue(e[0].dims,e[1].dims));let ee=["rank","rank"];d&&(C.push(...ue(e[2].dims)),ee.push("rank")),C.push(...ue(n));let v=R=>{let H=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];yn(t,H);let L=g?4:1,V=nt(e[0].dataType),K=` + fn setOutputAtIndex(flatIndex : i32, value : ${g?`vec4<${V}>`:V}) { + result[flatIndex] = ${g?`vec4<${V}>`:V}(value); } - fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${b?`vec4<${G}>`:G}) { + fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${g?`vec4<${V}>`:V}) { let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); - setOutputAtIndex(flatIndex ${b?"/ 4":""}, value); - }`,B=F("x",e[0].dataType,e[0].dims.length,O===3?1:O),X=F("w",e[1].dataType,e[1].dims.length,H),K=[B,X],M=re("result",e[0].dataType,n.length,H);if(d){let ne=F("bias",e[2].dataType,e[2].dims.length,H);K.push(ne),J+=` - fn getBiasByOutputCoords(coords : vec4) -> ${b?`vec4<${G}>`:G} { - return bias[coords.${r?"w":"y"}${b?"/ 4":""}]; + setOutputAtIndex(flatIndex ${g?"/ 4":""}, value); + }`,B=j("x",e[0].dataType,e[0].dims.length,O===3?1:O),te=j("w",e[1].dataType,e[1].dims.length,L),Y=[B,te],N=oe("result",e[0].dataType,n.length,L);if(d){let X=j("bias",e[2].dataType,e[2].dims.length,L);Y.push(X),K+=` + fn getBiasByOutputCoords(coords : vec4) -> ${g?`vec4<${V}>`:V} { + return bias[coords.${r?"w":"y"}${g?"/ 4":""}]; }`}return` - ${m1("uniforms.result_strides")} + ${Y1("uniforms.result_strides")} //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; - ${C.registerUniforms(U).declareVariables(...K,M)} - ${J} - ${mg(r,L,V,Z,d,t,q[0],q[1],q[2],G)} - ${b?dl(T,y,G,void 0,!r,I):cl(T,y,G,void 0,!r,I,!1,void 0,c)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${O};${b};${L};${V};${Z};${A};${P};${I}`,inputDependencies:ue},getRunData:()=>({outputs:[{dims:s?s(n):n,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:N}),getShaderSource:w}}}),gg,Da,Xn,bg,Ra,yg,b1,y1,rA=D(()=>{"use strict";ae(),Yt(),de(),ce(),an(),Vl(),gg=e=>{let t=1;for(let n=0;ntypeof e=="number"?[e,e,e]:e,Xn=(e,t)=>t<=1?e:e+(e-1)*(t-1),bg=(e,t,n,o=1)=>{let u=Xn(t,o);return Math.floor((e[0]*(n-1)-n+u)/2)},Ra=(e,t,n,o,u)=>{u==null&&(u=bg(e,t[0],o[0]));let l=[0,0,0,n];for(let d=0;d<3;d++)e[d]+2*u>=t[d]&&(l[d]=Math.trunc((e[d]-t[d]+2*u)/o[d]+1));return l},yg=(e,t,n,o,u,l,d,c,s,r)=>{let i,a,p,h;if(e==="VALID"&&(e=0),typeof e=="number"){i={top:e,bottom:e,left:e,right:e,front:e,back:e};let m=Ra([t,n,o,1],[c,s,r],1,[u,l,d],e);a=m[0],p=m[1],h=m[2]}else if(Array.isArray(e)){if(!e.every((b,$,_)=>b===_[0]))throw Error(`Unsupported padding parameter: ${e}`);i={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let m=Ra([t,n,o,1],[c,s,r],1,[u,l,d],e[0]);a=m[0],p=m[1],h=m[2]}else if(e==="SAME_UPPER"){a=Math.ceil(t/u),p=Math.ceil(n/l),h=Math.ceil(o/d);let m=(a-1)*u+c-t,b=(p-1)*l+s-n,$=(h-1)*d+r-o,_=Math.floor(m/2),y=m-_,T=Math.floor(b/2),S=b-T,O=Math.floor($/2),A=$-O;i={top:T,bottom:S,left:O,right:A,front:_,back:y}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:i,outDepth:a,outHeight:p,outWidth:h}},b1=(e,t,n,o,u,l=!1,d="channelsLast")=>{let c,s,r,i,a;if(d==="channelsLast")[c,s,r,i,a]=e;else if(d==="channelsFirst")[c,a,s,r,i]=e;else throw new Error(`Unknown dataFormat ${d}`);let[p,,h,m,b]=t,[$,_,y]=Da(n),[T,S,O]=Da(o),A=Xn(h,T),P=Xn(m,S),I=Xn(b,O),{padInfo:L,outDepth:V,outHeight:Z,outWidth:q}=yg(u,s,r,i,$,_,y,A,P,I),N=l?p*a:p,ue=[0,0,0,0,0];return d==="channelsFirst"?ue=[c,N,V,Z,q]:d==="channelsLast"&&(ue=[c,V,Z,q,N]),{batchSize:c,dataFormat:d,inDepth:s,inHeight:r,inWidth:i,inChannels:a,outDepth:V,outHeight:Z,outWidth:q,outChannels:N,padInfo:L,strideDepth:$,strideHeight:_,strideWidth:y,filterDepth:h,filterHeight:m,filterWidth:b,effectiveFilterDepth:A,effectiveFilterHeight:P,effectiveFilterWidth:I,dilationDepth:T,dilationHeight:S,dilationWidth:O,inShape:e,outShape:ue,filterShape:t}},y1=(e,t,n,o,u,l)=>{let d=l==="channelsLast",c=d?e[0].dims[3]:e[0].dims[1],s=!1,r=[64,1,1],i={x:n.map((y,T)=>T)},a=[Math.ceil(gg(i.x.map(y=>n[y]))/r[0]),1,1];ve("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${a}`);let p=s?d&&c%4!==0?3:4:1,h=z.size(n),m=[{type:12,data:h},{type:12,data:o},{type:12,data:u},{type:12,data:t.strides},{type:12,data:t.dilations}];tn(t,m),m.push(...se(e[0].dims,e[1].dims));let b=["rank","rank"],$=e.length===3;$&&(m.push(...se(e[2].dims)),b.push("rank")),m.push(...se(n));let _=y=>{let T=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:o.length},{name:"pads",type:"u32",length:u.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];rn(t,T);let S=s?4:1,O=Je(e[0].dataType),A=F("x",e[0].dataType,e[0].dims.length,p===3?1:p),P=F("W",e[1].dataType,e[1].dims.length,S),I=[A,P],L=re("result",e[0].dataType,n.length,S),V="";if($){let N=F("bias",e[2].dataType,e[2].dims.length,S);I.push(N),V+=` + ${R.registerUniforms(H).declareVariables(...Y,N)} + ${K} + ${Yg(r,z,G,J,d,t,q[0],q[1],q[2],V)} + ${g?zl(T,y,V,void 0,!r,I):Bl(T,y,V,void 0,!r,I,!1,void 0,c)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${O};${g};${z};${G};${J};${A};${P};${I}`,inputDependencies:ee},getRunData:()=>({outputs:[{dims:s?s(n):n,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:C}),getShaderSource:v}}}),Jg,su,pi,Qg,au,eb,Q1,e2,ak=D(()=>{"use strict";de(),ur(),pe(),he(),xn(),gd(),Jg=e=>{let t=1;for(let n=0;ntypeof e=="number"?[e,e,e]:e,pi=(e,t)=>t<=1?e:e+(e-1)*(t-1),Qg=(e,t,n,i=1)=>{let u=pi(t,i);return Math.floor((e[0]*(n-1)-n+u)/2)},au=(e,t,n,i,u)=>{u==null&&(u=Qg(e,t[0],i[0]));let l=[0,0,0,n];for(let d=0;d<3;d++)e[d]+2*u>=t[d]&&(l[d]=Math.trunc((e[d]-t[d]+2*u)/i[d]+1));return l},eb=(e,t,n,i,u,l,d,c,s,r)=>{let o,a,p,h;if(e==="VALID"&&(e=0),typeof e=="number"){o={top:e,bottom:e,left:e,right:e,front:e,back:e};let m=au([t,n,i,1],[c,s,r],1,[u,l,d],e);a=m[0],p=m[1],h=m[2]}else if(Array.isArray(e)){if(!e.every((g,_,w)=>g===w[0]))throw Error(`Unsupported padding parameter: ${e}`);o={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let m=au([t,n,i,1],[c,s,r],1,[u,l,d],e[0]);a=m[0],p=m[1],h=m[2]}else if(e==="SAME_UPPER"){a=Math.ceil(t/u),p=Math.ceil(n/l),h=Math.ceil(i/d);let m=(a-1)*u+c-t,g=(p-1)*l+s-n,_=(h-1)*d+r-i,w=Math.floor(m/2),y=m-w,T=Math.floor(g/2),S=g-T,O=Math.floor(_/2),A=_-O;o={top:T,bottom:S,left:O,right:A,front:w,back:y}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:o,outDepth:a,outHeight:p,outWidth:h}},Q1=(e,t,n,i,u,l=!1,d="channelsLast")=>{let c,s,r,o,a;if(d==="channelsLast")[c,s,r,o,a]=e;else if(d==="channelsFirst")[c,a,s,r,o]=e;else throw new Error(`Unknown dataFormat ${d}`);let[p,,h,m,g]=t,[_,w,y]=su(n),[T,S,O]=su(i),A=pi(h,T),P=pi(m,S),I=pi(g,O),{padInfo:z,outDepth:G,outHeight:J,outWidth:q}=eb(u,s,r,o,_,w,y,A,P,I),C=l?p*a:p,ee=[0,0,0,0,0];return d==="channelsFirst"?ee=[c,C,G,J,q]:d==="channelsLast"&&(ee=[c,G,J,q,C]),{batchSize:c,dataFormat:d,inDepth:s,inHeight:r,inWidth:o,inChannels:a,outDepth:G,outHeight:J,outWidth:q,outChannels:C,padInfo:z,strideDepth:_,strideHeight:w,strideWidth:y,filterDepth:h,filterHeight:m,filterWidth:g,effectiveFilterDepth:A,effectiveFilterHeight:P,effectiveFilterWidth:I,dilationDepth:T,dilationHeight:S,dilationWidth:O,inShape:e,outShape:ee,filterShape:t}},e2=(e,t,n,i,u,l)=>{let d=l==="channelsLast",c=d?e[0].dims[3]:e[0].dims[1],s=!1,r=[64,1,1],o={x:n.map((y,T)=>T)},a=[Math.ceil(Jg(o.x.map(y=>n[y]))/r[0]),1,1];Ie("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${a}`);let p=s?d&&c%4!==0?3:4:1,h=F.size(n),m=[{type:12,data:h},{type:12,data:i},{type:12,data:u},{type:12,data:t.strides},{type:12,data:t.dilations}];bn(t,m),m.push(...ue(e[0].dims,e[1].dims));let g=["rank","rank"],_=e.length===3;_&&(m.push(...ue(e[2].dims)),g.push("rank")),m.push(...ue(n));let w=y=>{let T=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:i.length},{name:"pads",type:"u32",length:u.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];yn(t,T);let S=s?4:1,O=nt(e[0].dataType),A=j("x",e[0].dataType,e[0].dims.length,p===3?1:p),P=j("W",e[1].dataType,e[1].dims.length,S),I=[A,P],z=oe("result",e[0].dataType,n.length,S),G="";if(_){let C=j("bias",e[2].dataType,e[2].dims.length,S);I.push(C),G+=` fn getBiasByOutputCoords(coords : array) -> ${s?`vec4<${O}>`:O} { - return bias[${d?ie("coords",4,5):ie("coords",1,5)}${s?"/ 4":""}]; - }`}let Z=tt(p,O),q=en(t,Z,O);return` - ${V} + return bias[${d?se("coords",4,5):se("coords",1,5)}${s?"/ 4":""}]; + }`}let J=at(p,O),q=gn(t,J,O);return` + ${G} fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { let aIndices = array(d0, d1, d2, d3, d4); return ${A.getByIndices("aIndices")}; @@ -2796,22 +2796,22 @@ fn main(@builtin(local_invocation_id) localId : vec3, let aIndices = array(d0, d1, d2, d3, d4); return ${P.getByIndices("aIndices")}; } - ${y.registerUniforms(T).declareVariables(...I,L)} + ${y.registerUniforms(T).declareVariables(...I,z)} ${y.mainStart()} ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let coords = ${L.offsetToIndices("global_idx")}; - let batch = ${ie("coords",0,A.rank)}; - let d2 = ${d?ie("coords",A.rank-1,A.rank):ie("coords",1,A.rank)}; - let xFRCCorner = vec3(${d?ie("coords",1,A.rank):ie("coords",2,A.rank)}, - ${d?ie("coords",2,A.rank):ie("coords",3,A.rank)}, - ${d?ie("coords",3,A.rank):ie("coords",4,A.rank)}) * uniforms.strides - uniforms.pads; + let coords = ${z.offsetToIndices("global_idx")}; + let batch = ${se("coords",0,A.rank)}; + let d2 = ${d?se("coords",A.rank-1,A.rank):se("coords",1,A.rank)}; + let xFRCCorner = vec3(${d?se("coords",1,A.rank):se("coords",2,A.rank)}, + ${d?se("coords",2,A.rank):se("coords",3,A.rank)}, + ${d?se("coords",3,A.rank):se("coords",4,A.rank)}) * uniforms.strides - uniforms.pads; let xFCorner = xFRCCorner.x; let xRCorner = xFRCCorner.y; let xCCorner = xFRCCorner.z; - let xShapeY = ${d?ie("uniforms.x_shape",1,A.rank):ie("uniforms.x_shape",2,A.rank)}; - let xShapeZ = ${d?ie("uniforms.x_shape",2,A.rank):ie("uniforms.x_shape",3,A.rank)}; - let xShapeW = ${d?ie("uniforms.x_shape",3,A.rank):ie("uniforms.x_shape",4,A.rank)}; - let xShapeU = ${d?ie("uniforms.x_shape",4,A.rank):ie("uniforms.x_shape",1,A.rank)}; + let xShapeY = ${d?se("uniforms.x_shape",1,A.rank):se("uniforms.x_shape",2,A.rank)}; + let xShapeZ = ${d?se("uniforms.x_shape",2,A.rank):se("uniforms.x_shape",3,A.rank)}; + let xShapeW = ${d?se("uniforms.x_shape",3,A.rank):se("uniforms.x_shape",4,A.rank)}; + let xShapeU = ${d?se("uniforms.x_shape",4,A.rank):se("uniforms.x_shape",1,A.rank)}; let inputDepthNearestVec4 = (xShapeU / 4) * 4; let inputDepthVec4Remainder = xShapeU % 4; @@ -2888,10 +2888,10 @@ fn main(@builtin(local_invocation_id) localId : vec3, } } } - ${$?"value = value + getBiasByOutputCoords(coords)":""}; + ${_?"value = value + getBiasByOutputCoords(coords)":""}; ${q} result[global_idx] = f32(value); - }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${d};${p};${$}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:a[0],y:a[1],z:a[2]},programUniforms:m}),getShaderSource:_}}}),_1,w1,nA=D(()=>{"use strict";ae(),de(),ce(),an(),_1=(e,t,n,o)=>{let u=e.length>2,l=u?"value += b[output_channel];":"",d=e[0].dims,c=e[1].dims,s=t.format==="NHWC",r=s?n[3]:n[1],i=r/t.group,a=s&&i>=4?Ue(r):1,p=z.size(n)/a,h=[{type:12,data:p},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:i}];tn(t,h),h.push(...se(d,[c[0],c[1],c[2],c[3]/a]));let m=u?["rank","rank","rank"]:["rank","rank"];h.push(...se([n[0],n[1],n[2],n[3]/a]));let b=$=>{let _=re("output",e[0].dataType,n.length,a),y=Je(_.type.tensor),T=en(t,_.type.value,y),S=F("x",e[0].dataType,d.length),O=F("w",e[1].dataType,c.length,a),A=[S,O];u&&A.push(F("b",e[2].dataType,e[2].dims,a));let P=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];rn(t,P);let I=s?` + }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${d};${p};${_}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:a[0],y:a[1],z:a[2]},programUniforms:m}),getShaderSource:w}}}),t2,r2,uk=D(()=>{"use strict";de(),pe(),he(),xn(),t2=(e,t,n,i)=>{let u=e.length>2,l=u?"value += b[output_channel];":"",d=e[0].dims,c=e[1].dims,s=t.format==="NHWC",r=s?n[3]:n[1],o=r/t.group,a=s&&o>=4?Ge(r):1,p=F.size(n)/a,h=[{type:12,data:p},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:o}];bn(t,h),h.push(...ue(d,[c[0],c[1],c[2],c[3]/a]));let m=u?["rank","rank","rank"]:["rank","rank"];h.push(...ue([n[0],n[1],n[2],n[3]/a]));let g=_=>{let w=oe("output",e[0].dataType,n.length,a),y=nt(w.type.tensor),T=gn(t,w.type.value,y),S=j("x",e[0].dataType,d.length),O=j("w",e[1].dataType,c.length,a),A=[S,O];u&&A.push(j("b",e[2].dataType,e[2].dims,a));let P=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];yn(t,P);let I=s?` for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; @@ -2936,25 +2936,25 @@ fn main(@builtin(local_invocation_id) localId : vec3, } } `;return` - ${$.registerUniforms(P).declareVariables(...A,_)} + ${_.registerUniforms(P).declareVariables(...A,w)} - ${$.mainStart()} - ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${_.mainStart()} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let outputIndices = ${_.offsetToIndices("global_idx")}; + let outputIndices = ${w.offsetToIndices("global_idx")}; let batch: u32 = outputIndices[0]; let output_channel: u32 = outputIndices[${s?3:1}]; let xRCCorner: vec2 = vec2(outputIndices[${s?1:2}], outputIndices[${s?2:3}]) * uniforms.strides - uniforms.pads; let group_id: u32 = output_channel * ${a} / uniforms.output_channels_per_group; var in_channel_offset = group_id * uniforms.w_shape[${s?2:1}]; - var value: ${_.type.value} = ${_.type.value}(0); + var value: ${w.type.value} = ${w.type.value}(0); ${I} ${l} ${T} - ${_.setByOffset("global_idx","value")} - }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${a}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:o?o(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:b}},w1=(e,t,n,o)=>{let u=e.length>2,l=Ue(n[3]),d=Ue(n[2]),c=z.size(n)/l/d,s=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/l],r=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/l],i=[n[0],n[1],n[2],n[3]/l],a=[{type:12,data:c},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];tn(t,a),a.push(...se(s,r,i));let p=(d-1)*t.strides[1]+r[1],h=m=>{let b=re("output",e[0].dataType,i.length,l),$=Je(b.type.tensor),_=en(t,b.type.value,$),y=F("x",e[0].dataType,s.length,l),T=F("w",e[1].dataType,r.length,l),S=[y,T];u&&S.push(F("b",e[2].dataType,e[2].dims,l));let O=u?"value += b[output_channel];":"",A=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return rn(t,A),` - ${m.registerUniforms(A).declareVariables(...S,b)} + ${w.setByOffset("global_idx","value")} + }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${a}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:i?i(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:g}},r2=(e,t,n,i)=>{let u=e.length>2,l=Ge(n[3]),d=Ge(n[2]),c=F.size(n)/l/d,s=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/l],r=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/l],o=[n[0],n[1],n[2],n[3]/l],a=[{type:12,data:c},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];bn(t,a),a.push(...ue(s,r,o));let p=(d-1)*t.strides[1]+r[1],h=m=>{let g=oe("output",e[0].dataType,o.length,l),_=nt(g.type.tensor),w=gn(t,g.type.value,_),y=j("x",e[0].dataType,s.length,l),T=j("w",e[1].dataType,r.length,l),S=[y,T];u&&S.push(j("b",e[2].dataType,e[2].dims,l));let O=u?"value += b[output_channel];":"",A=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return yn(t,A),` + ${m.registerUniforms(A).declareVariables(...S,g)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let width0 = uniforms.output_shape[3]; @@ -2969,7 +2969,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; var x_vals: array<${y.type.value}, ${p}>; - var values: array<${b.type.value}, ${d}>; + var values: array<${g.type.value}, ${d}>; let input_channel = output_channel; // Use constant instead of uniform can give better performance for w's height/width. for (var w_height: u32 = 0u; w_height < ${r[0]}; w_height++) { @@ -2995,37 +2995,37 @@ fn main(@builtin(local_invocation_id) localId : vec3, for (var i = 0u; i < ${d}u; i++) { var value = values[i]; ${O} - ${_} - ${b.set("batch","row","col + i","output_channel","value")}; + ${w} + ${g.set("batch","row","col + i","output_channel","value")}; } - }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${l};${d};${p};${r[0]};${r[1]}`,inputDependencies:u?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:o?o(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:a}),getShaderSource:h}}}),_g,uo,wg,lo,pl,Na,vg,xg,hl,iA=D(()=>{"use strict";de(),tA(),rA(),Hl(),nA(),an(),Gl(),wr(),_g=(e,t,n,o,u,l)=>{let d=e[0],c=e.slice(l?1:2,l?3:4),s=c.length,r=t[0],i=t.slice(2).map((p,h)=>p+(p-1)*(n[h]-1)),a=c.map((p,h)=>p+o[h]+o[h+s]).map((p,h)=>Math.floor((p-i[h]+u[h])/u[h]));return a.splice(0,0,d),a.splice(l?3:1,0,r),a},uo=[2,3,1,0],wg=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],o=e[1].dims[1]*t.group;if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},lo=(e,t)=>{let n=e.kernelShape.slice();n.length{let t=Ul(e),n=e.format,o=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],u=e.dilations,l=e.group,d=e.kernel_shape,c=e.pads,s=e.strides,r=e.w_is_const();return{autoPad:o,format:n,dilations:u,group:l,kernelShape:d,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},Na=(e,t,n,o)=>{let u=n.format==="NHWC",l=_g(t[0].dims,t[1].dims,n.dilations,n.pads,n.strides,u);if(n.group!==1){let A=[t[0]];if(u){let P=e.kernelCustomData.wT??e.compute(gt(t[1],uo),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=P),A.push(P)}else A.push(t[1]);t.length===3&&A.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&u&&t[1].dims[0]===n.group&&t[1].dims[1]===1&&n.dilations[0]===1&&n.dilations[1]===1?e.compute(w1(A,n,l,o),{inputs:A}):e.compute(_1(A,n,l,o),{inputs:A});return}let d=t.length===3,c=t[0].dims[u?1:2],s=t[0].dims[u?2:3],r=t[0].dims[u?3:1],i=t[1].dims[2],a=t[1].dims[3],p=l[u?1:2],h=l[u?2:3],m=l[u?3:1],b=u&&i===c&&a===s&&n.pads[0]===0&&n.pads[1]===0;if(b||i===1&&a===1&&n.dilations[0]===1&&n.dilations[1]===1&&n.strides[0]===1&&n.strides[1]===1&&n.pads[0]===0&&n.pads[1]===0){let A=l[0],P,I,L,V=[];if(u){let N=e.kernelCustomData.wT??e.compute(gt(t[1],uo),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];if(n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=N),b){let ue=c*s*r;P=t[0].reshape([1,A,ue]),I=N.reshape([1,ue,m]),L=[1,A,m]}else P=t[0].reshape([A,c*s,r]),I=N.reshape([1,r,m]),L=[A,p*h,m];V.push(P),V.push(I)}else P=t[0].reshape([A,r,c*s]),I=t[1].reshape([1,m,r]),L=[A,m,p*h],V.push(I),V.push(P);d&&V.push(t[2]);let Z=L[2],q=V[0].dims[V[0].dims.length-1];Z<8&&q<8?e.compute(ql(V,n,l,L,u,o),{inputs:V}):e.compute(Ao(V,n,l,L,u,o),{inputs:V});return}let $=!0,_=e.kernelCustomData.wT??e.compute(gt(t[1],uo),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=_);let y=[t[0],_];d&&y.push(t[2]);let T=u?p*h:m,S=u?m:p*h,O=i*a*r;e.compute(g1(y,n,l,T,S,O,d,$,o),{inputs:y})},vg=(e,t)=>{let n=t.format==="NHWC",o=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&o.push(e.inputs[2]);let u=[0,t.pads[0],0,t.pads[1]],l=[1].concat(t.strides),d=[1].concat(t.dilations),c=[1].concat(t.kernelShape),s=lo({...t,pads:u,strides:l,dilations:d,kernelShape:c},o);Na(e,o,s,r=>n?[r[0],r[2],r[3]]:[r[0],r[1],r[3]])},xg=(e,t,n)=>{let o=n.format==="NHWC"?"channelsLast":"channelsFirst",u=lo(n,t),l=n.autoPad==="NOTSET"?n.pads:n.autoPad,d=b1(t[0].dims,t[1].dims,n.strides,n.dilations,l,!1,o);e.compute(y1(t,u,d.outShape,[d.filterDepth,d.filterHeight,d.filterWidth],[d.padInfo.front,d.padInfo.top,d.padInfo.left],o))},hl=(e,t)=>{if(wg(e.inputs,t),e.inputs[0].dims.length===3)vg(e,t);else if(e.inputs[0].dims.length===5)xg(e,e.inputs,t);else{let n=lo(t,e.inputs);Na(e,e.inputs,n)}}}),v1,oA=D(()=>{"use strict";ae(),Yt(),de(),ce(),v1=(e,t,n)=>{let o=e.length>2,u=t.outputShape,l=t.format==="NHWC",d=t.group,c=e[1].dims,s=c[2]/d,r=c[3],i=l?Ue(s):1,a=l&&r===1&&s>=4,p=a?Math.floor(s/4)*4:Math.floor(s/i)*i,h=s-p,m=l?Ue(r):1,b=l?r===1?i:m:1,$=z.size(u)/m,_=[Math.ceil($/64),1,1];ve("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${_}`);let y=["rank","rank"],T=[t.strides[0],t.strides[1]],S=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],O=[t.dilations[0],t.dilations[1]],A=[S[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),S[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],P=[A[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),A[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],I=[{type:12,data:$},{type:12,data:T},{type:12,data:S},{type:12,data:O},{type:12,data:A},{type:6,data:P},{type:12,data:p},{type:12,data:s},{type:12,data:r},...se(e[0].dims,e[1].dims)];o&&(I.push(...se(e[2].dims)),y.push("rank")),I.push(...se(u));let L=V=>{let Z=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:T.length},{name:"filter_dims",type:"u32",length:S.length},{name:"dilations",type:"u32",length:S.length},{name:"effective_filter_dims",type:"u32",length:A.length},{name:"pads",type:"i32",length:P.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],q=Je(e[0].dataType),N=l?1:2,ue=l?2:3,w=l?3:1,C=F("W",e[1].dataType,e[1].dims.length,b),U=F("Dy",e[0].dataType,e[0].dims.length,i),H=[U,C];o&&H.push(F("bias",e[2].dataType,[u[w]].length,m));let G=re("result",e[0].dataType,u.length,m),J=()=>{let K="";if(a)i===4?K+=` - let xValue = ${U.getByOffset("x_offset")}; - let wValue = ${C.getByOffset("w_offset")}; + }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${l};${d};${p};${r[0]};${r[1]}`,inputDependencies:u?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:a}),getShaderSource:h}}}),tb,Ao,rb,ko,Fl,uu,nb,ib,Ll,lk=D(()=>{"use strict";pe(),sk(),ak(),wd(),uk(),xn(),yd(),Cr(),tb=(e,t,n,i,u,l)=>{let d=e[0],c=e.slice(l?1:2,l?3:4),s=c.length,r=t[0],o=t.slice(2).map((p,h)=>p+(p-1)*(n[h]-1)),a=c.map((p,h)=>p+i[h]+i[h+s]).map((p,h)=>Math.floor((p-o[h]+u[h])/u[h]));return a.splice(0,0,d),a.splice(l?3:1,0,r),a},Ao=[2,3,1,0],rb=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[1]*t.group;if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},ko=(e,t)=>{let n=e.kernelShape.slice();n.length{let t=md(e),n=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],u=e.dilations,l=e.group,d=e.kernel_shape,c=e.pads,s=e.strides,r=e.w_is_const();return{autoPad:i,format:n,dilations:u,group:l,kernelShape:d,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},uu=(e,t,n,i)=>{let u=n.format==="NHWC",l=tb(t[0].dims,t[1].dims,n.dilations,n.pads,n.strides,u);if(n.group!==1){let A=[t[0]];if(u){let P=e.kernelCustomData.wT??e.compute($t(t[1],Ao),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=P),A.push(P)}else A.push(t[1]);t.length===3&&A.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&u&&t[1].dims[0]===n.group&&t[1].dims[1]===1&&n.dilations[0]===1&&n.dilations[1]===1?e.compute(r2(A,n,l,i),{inputs:A}):e.compute(t2(A,n,l,i),{inputs:A});return}let d=t.length===3,c=t[0].dims[u?1:2],s=t[0].dims[u?2:3],r=t[0].dims[u?3:1],o=t[1].dims[2],a=t[1].dims[3],p=l[u?1:2],h=l[u?2:3],m=l[u?3:1],g=u&&o===c&&a===s&&n.pads[0]===0&&n.pads[1]===0;if(g||o===1&&a===1&&n.dilations[0]===1&&n.dilations[1]===1&&n.strides[0]===1&&n.strides[1]===1&&n.pads[0]===0&&n.pads[1]===0){let A=l[0],P,I,z,G=[];if(u){let C=e.kernelCustomData.wT??e.compute($t(t[1],Ao),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];if(n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=C),g){let ee=c*s*r;P=t[0].reshape([1,A,ee]),I=C.reshape([1,ee,m]),z=[1,A,m]}else P=t[0].reshape([A,c*s,r]),I=C.reshape([1,r,m]),z=[A,p*h,m];G.push(P),G.push(I)}else P=t[0].reshape([A,r,c*s]),I=t[1].reshape([1,m,r]),z=[A,m,p*h],G.push(I),G.push(P);d&&G.push(t[2]);let J=z[2],q=G[0].dims[G[0].dims.length-1];J<8&&q<8?e.compute(bd(G,n,l,z,u,i),{inputs:G}):e.compute(Xo(G,n,l,z,u,i),{inputs:G});return}let _=!0,w=e.kernelCustomData.wT??e.compute($t(t[1],Ao),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=w);let y=[t[0],w];d&&y.push(t[2]);let T=u?p*h:m,S=u?m:p*h,O=o*a*r;e.compute(J1(y,n,l,T,S,O,d,_,i),{inputs:y})},nb=(e,t)=>{let n=t.format==="NHWC",i=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let u=[0,t.pads[0],0,t.pads[1]],l=[1].concat(t.strides),d=[1].concat(t.dilations),c=[1].concat(t.kernelShape),s=ko({...t,pads:u,strides:l,dilations:d,kernelShape:c},i);uu(e,i,s,r=>n?[r[0],r[2],r[3]]:[r[0],r[1],r[3]])},ib=(e,t,n)=>{let i=n.format==="NHWC"?"channelsLast":"channelsFirst",u=ko(n,t),l=n.autoPad==="NOTSET"?n.pads:n.autoPad,d=Q1(t[0].dims,t[1].dims,n.strides,n.dilations,l,!1,i);e.compute(e2(t,u,d.outShape,[d.filterDepth,d.filterHeight,d.filterWidth],[d.padInfo.front,d.padInfo.top,d.padInfo.left],i))},Ll=(e,t)=>{if(rb(e.inputs,t),e.inputs[0].dims.length===3)nb(e,t);else if(e.inputs[0].dims.length===5)ib(e,e.inputs,t);else{let n=ko(t,e.inputs);uu(e,e.inputs,n)}}}),n2,dk=D(()=>{"use strict";de(),ur(),pe(),he(),n2=(e,t,n)=>{let i=e.length>2,u=t.outputShape,l=t.format==="NHWC",d=t.group,c=e[1].dims,s=c[2]/d,r=c[3],o=l?Ge(s):1,a=l&&r===1&&s>=4,p=a?Math.floor(s/4)*4:Math.floor(s/o)*o,h=s-p,m=l?Ge(r):1,g=l?r===1?o:m:1,_=F.size(u)/m,w=[Math.ceil(_/64),1,1];Ie("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${w}`);let y=["rank","rank"],T=[t.strides[0],t.strides[1]],S=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],O=[t.dilations[0],t.dilations[1]],A=[S[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),S[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],P=[A[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),A[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],I=[{type:12,data:_},{type:12,data:T},{type:12,data:S},{type:12,data:O},{type:12,data:A},{type:6,data:P},{type:12,data:p},{type:12,data:s},{type:12,data:r},...ue(e[0].dims,e[1].dims)];i&&(I.push(...ue(e[2].dims)),y.push("rank")),I.push(...ue(u));let z=G=>{let J=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:T.length},{name:"filter_dims",type:"u32",length:S.length},{name:"dilations",type:"u32",length:S.length},{name:"effective_filter_dims",type:"u32",length:A.length},{name:"pads",type:"i32",length:P.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],q=nt(e[0].dataType),C=l?1:2,ee=l?2:3,v=l?3:1,R=j("W",e[1].dataType,e[1].dims.length,g),H=j("Dy",e[0].dataType,e[0].dims.length,o),L=[H,R];i&&L.push(j("bias",e[2].dataType,[u[v]].length,m));let V=oe("result",e[0].dataType,u.length,m),K=()=>{let Y="";if(a)o===4?Y+=` + let xValue = ${H.getByOffset("x_offset")}; + let wValue = ${R.getByOffset("w_offset")}; dotProd = dotProd + dot(xValue, wValue); x_offset += 1u; - w_offset += 1u;`:i===2?K+=` - dotProd = dotProd + dot(vec4<${q}>(${U.getByOffset("x_offset")}, ${U.getByOffset("x_offset + 1u")}), vec4<${q}>(${C.getByOffset("w_offset")}, ${C.getByOffset("w_offset + 1u")})); + w_offset += 1u;`:o===2?Y+=` + dotProd = dotProd + dot(vec4<${q}>(${H.getByOffset("x_offset")}, ${H.getByOffset("x_offset + 1u")}), vec4<${q}>(${R.getByOffset("w_offset")}, ${R.getByOffset("w_offset + 1u")})); x_offset += 2u; - w_offset += 2u;`:i===1&&(K+=` - dotProd = dotProd + dot(vec4<${q}>(${U.getByOffset("x_offset")}, ${U.getByOffset("x_offset + 1u")}, ${U.getByOffset("x_offset + 2u")}, ${U.getByOffset("x_offset + 3u")}), vec4<${q}>(${C.getByOffset("w_offset")}, ${C.getByOffset("w_offset + 1u")}, ${C.getByOffset("w_offset + 2u")}, ${C.getByOffset("w_offset + 3u")})); + w_offset += 2u;`:o===1&&(Y+=` + dotProd = dotProd + dot(vec4<${q}>(${H.getByOffset("x_offset")}, ${H.getByOffset("x_offset + 1u")}, ${H.getByOffset("x_offset + 2u")}, ${H.getByOffset("x_offset + 3u")}), vec4<${q}>(${R.getByOffset("w_offset")}, ${R.getByOffset("w_offset + 1u")}, ${R.getByOffset("w_offset + 2u")}, ${R.getByOffset("w_offset + 3u")})); x_offset += 4u; - w_offset += 4u;`);else if(K+=` - let xValue = ${l?U.getByOffset(`${U.indicesToOffset(`${U.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${i}`):U.get("batch","inputChannel","idyR","idyC")}; - `,i===1)K+=` - let w_offset = ${C.indicesToOffset(`${C.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)}; - let wValue = ${C.getByOffset(`w_offset / ${b}`)}; - dotProd = dotProd + xValue * wValue;`;else for(let M=0;M{if(h===0)return"";if(!a)throw new Error(`packInputAs4 ${a} is not true.`);let K="";if(i===1){K+="dotProd = dotProd";for(let M=0;M{if(h===0)return"";if(!a)throw new Error(`packInputAs4 ${a} is not true.`);let Y="";if(o===1){Y+="dotProd = dotProd";for(let N=0;N(i32(r), i32(c)) - uniforms.pads; let dyRCorner = dyCorner.x; let dyCCorner = dyCorner.y; @@ -3033,7 +3033,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). // ? = to be determined. : = across all values in that axis. - var dotProd = ${G.type.value}(0.0); + var dotProd = ${V.type.value}(0.0); var wR: u32 = 0; if (uniforms.dilations.x == 1) { // Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0 @@ -3045,7 +3045,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, } let dyR = (${q}(dyRCorner) + ${q}(wR)) / ${q}(uniforms.strides[0]); let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; - if (dyR < 0.0 || dyR >= ${q}(uniforms.Dy_shape[${N}]) || fract(dyR) > 0.0 || + if (dyR < 0.0 || dyR >= ${q}(uniforms.Dy_shape[${C}]) || fract(dyR) > 0.0 || wRPerm < 0) { continue; } @@ -3061,64 +3061,64 @@ fn main(@builtin(local_invocation_id) localId : vec3, } let dyC = (${q}(dyCCorner) + ${q}(wC)) / ${q}(uniforms.strides.y); let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; - if (dyC < 0.0 || dyC >= ${q}(uniforms.Dy_shape[${ue}]) || + if (dyC < 0.0 || dyC >= ${q}(uniforms.Dy_shape[${ee}]) || fract(dyC) > 0.0 || wCPerm < 0) { continue; } let idyC: u32 = u32(dyC); var inputChannel = groupId * uniforms.input_channels_per_group; ${a?` - var x_offset = ${U.indicesToOffset(`${U.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${i}; - var w_offset = ${C.indicesToOffset(`${C.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${b}; + var x_offset = ${H.indicesToOffset(`${H.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${o}; + var w_offset = ${R.indicesToOffset(`${R.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${g}; `:""} - for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${a?4:i}) { - ${J()} - inputChannel = inputChannel + ${a?4:i}; + for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${a?4:o}) { + ${K()} + inputChannel = inputChannel + ${a?4:o}; } ${B()} wC = wC + uniforms.strides.y - 1; } wR = wR + uniforms.strides[0] - 1; } - let value = dotProd${o?` + bias[d1 / ${m}]`:""}; - ${G.setByOffset("global_idx","value")}; + let value = dotProd${i?` + bias[d1 / ${m}]`:""}; + ${V.setByOffset("global_idx","value")}; `;return` - ${V.registerUniforms(Z).declareVariables(...H,G)} - ${V.mainStart()} - ${V.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; - ${X}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${i}${b}${m}${a}${h}`,inputDependencies:y},getRunData:()=>({dispatchGroup:{x:_[0],y:_[1],z:_[2]},outputs:[{dims:n?n(u):u,dataType:e[0].dataType}],programUniforms:I}),getShaderSource:L}}}),$g,Tg,Sg,za,x1,Ig,Ma,Og,$1,sA=D(()=>{"use strict";oA(),an(),wr(),$g=(e,t,n,o,u,l)=>(e-1)*t+n+(o-1)*u+1-l,Tg=(e,t,n,o,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[o]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[o]=e-l,n[u]=l)},Sg=(e,t,n,o,u,l,d,c,s,r)=>{let i=e.length-2,a=r.length===0;s.length{let n=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((a,p)=>a*p,1)===0){n.length=0;for(let a=2;aa+p,0)===0){let a=t[0].dims.length-2;s=new Array(a).fill(1)}let r=e.strides.slice();if(r.reduce((a,p)=>a+p,0)===0){let a=t[0].dims.length-2;r=new Array(a).fill(1)}Sg(c,n,s,e.autoPad,e.group,u,r,o,d,l);let i=Object.assign({},e);return Object.assign(i,{kernelShape:n,pads:u,outputPadding:d,outputShape:l,dilations:s,strides:r}),i},x1=e=>{let t=Ul(e),n=e.format,o=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],u=e.dilations,l=e.group??1,d=e.kernelShape,c=e.pads,s=e.strides,r=e.wIsConst(),i=e.outputPadding,a=e.outputShape;return{autoPad:o,format:n,dilations:u,group:l,kernelShape:d,outputPadding:i,outputShape:a,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},Ig=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],o=e[1].dims[0];if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.reduce((d,c)=>d+c,0)>0&&t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.reduce((d,c)=>d+c,0)>0&&t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.reduce((d,c)=>d+c,0)>0&&t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.reduce((d,c)=>d+c,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Ma=(e,t,n,o)=>{let u=e.kernelCustomData.wT??e.compute(gt(t[1],[2,3,0,1]),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=u);let l=[t[0],u];t.length===3&&l.push(t[2]),e.compute(v1(l,n,o),{inputs:l})},Og=(e,t)=>{let n=t.format==="NHWC",o=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&o.push(e.inputs[2]);let u=t.kernelShape;(u.length===0||u[0]===0)&&(u=[e.inputs[1].dims[2]]);let l=t.dilations;(l.length===0||l[0]===0)&&(l=[1]);let d=t.strides;(d.length===0||d[0]===0)&&(d=[1]);let c=t.pads;c.length===0&&(c=[0,0]),c=[0,c[0],0,c[1]],d=[1].concat(d),l=[1].concat(l),u=[1].concat(u);let s=t.outputPadding;s=[0].concat(s);let r=za({...t,pads:c,strides:d,dilations:l,kernelShape:u,outputPadding:s},o);Ma(e,o,r,i=>n?[i[0],i[2],i[3]]:[i[0],i[1],i[3]])},$1=(e,t)=>{if(Ig(e.inputs,t),e.inputs[0].dims.length===3)Og(e,t);else{let n=za(t,e.inputs);Ma(e,e.inputs,n)}}}),Eg,T1,S1,aA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Eg=(e,t,n,o)=>{let u=z.size(t),l=t.length,d=F("input",e,l),c=re("output",e,l),s=n.dataType===6?n.getInt32Array()[0]:Number(n.getBigInt64Array()[0]),r=z.normalizeAxis(s,l),i=a=>{let p=` i32(${d.indicesGet("inputIndices","uniforms.axis")}) `,h=ie("uniforms.input_shape","uniforms.axis",l),m=o.reverse?p+(o.exclusive?" + 1":""):"0",b=o.reverse?h:p+(o.exclusive?"":" + 1");return` + ${G.registerUniforms(J).declareVariables(...L,V)} + ${G.mainStart()} + ${G.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; + ${te}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${o}${g}${m}${a}${h}`,inputDependencies:y},getRunData:()=>({dispatchGroup:{x:w[0],y:w[1],z:w[2]},outputs:[{dims:n?n(u):u,dataType:e[0].dataType}],programUniforms:I}),getShaderSource:z}}}),ob,sb,ab,lu,i2,ub,du,lb,o2,ck=D(()=>{"use strict";dk(),xn(),Cr(),ob=(e,t,n,i,u,l)=>(e-1)*t+n+(i-1)*u+1-l,sb=(e,t,n,i,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[i]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[i]=e-l,n[u]=l)},ab=(e,t,n,i,u,l,d,c,s,r)=>{let o=e.length-2,a=r.length===0;s.length{let n=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((a,p)=>a*p,1)===0){n.length=0;for(let a=2;aa+p,0)===0){let a=t[0].dims.length-2;s=new Array(a).fill(1)}let r=e.strides.slice();if(r.reduce((a,p)=>a+p,0)===0){let a=t[0].dims.length-2;r=new Array(a).fill(1)}ab(c,n,s,e.autoPad,e.group,u,r,i,d,l);let o=Object.assign({},e);return Object.assign(o,{kernelShape:n,pads:u,outputPadding:d,outputShape:l,dilations:s,strides:r}),o},i2=e=>{let t=md(e),n=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],u=e.dilations,l=e.group??1,d=e.kernelShape,c=e.pads,s=e.strides,r=e.wIsConst(),o=e.outputPadding,a=e.outputShape;return{autoPad:i,format:n,dilations:u,group:l,kernelShape:d,outputPadding:o,outputShape:a,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},ub=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[0];if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.reduce((d,c)=>d+c,0)>0&&t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.reduce((d,c)=>d+c,0)>0&&t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.reduce((d,c)=>d+c,0)>0&&t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.reduce((d,c)=>d+c,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},du=(e,t,n,i)=>{let u=e.kernelCustomData.wT??e.compute($t(t[1],[2,3,0,1]),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=u);let l=[t[0],u];t.length===3&&l.push(t[2]),e.compute(n2(l,n,i),{inputs:l})},lb=(e,t)=>{let n=t.format==="NHWC",i=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let u=t.kernelShape;(u.length===0||u[0]===0)&&(u=[e.inputs[1].dims[2]]);let l=t.dilations;(l.length===0||l[0]===0)&&(l=[1]);let d=t.strides;(d.length===0||d[0]===0)&&(d=[1]);let c=t.pads;c.length===0&&(c=[0,0]),c=[0,c[0],0,c[1]],d=[1].concat(d),l=[1].concat(l),u=[1].concat(u);let s=t.outputPadding;s=[0].concat(s);let r=lu({...t,pads:c,strides:d,dilations:l,kernelShape:u,outputPadding:s},i);du(e,i,r,o=>n?[o[0],o[2],o[3]]:[o[0],o[1],o[3]])},o2=(e,t)=>{if(ub(e.inputs,t),e.inputs[0].dims.length===3)lb(e,t);else{let n=lu(t,e.inputs);du(e,e.inputs,n)}}}),db,s2,a2,pk=D(()=>{"use strict";de(),pe(),He(),he(),db=(e,t,n,i)=>{let u=F.size(t),l=t.length,d=j("input",e,l),c=oe("output",e,l),s=n.dataType===6?n.getInt32Array()[0]:Number(n.getBigInt64Array()[0]),r=F.normalizeAxis(s,l),o=a=>{let p=` i32(${d.indicesGet("inputIndices","uniforms.axis")}) `,h=se("uniforms.input_shape","uniforms.axis",l),m=i.reverse?p+(i.exclusive?" + 1":""):"0",g=i.reverse?h:p+(i.exclusive?"":" + 1");return` ${a.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(d,c)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} var inputIndices = ${c.offsetToIndices("global_idx")}; var sum = ${c.type.value}(0); let first : i32 = ${m}; - let last : i32 = ${b}; + let last : i32 = ${g}; for (var i : i32 = first; i < last; i++) { ${d.indicesSet("inputIndices","uniforms.axis","u32(i)")}; sum = sum + ${d.getByIndices("inputIndices")}; } ${c.setByOffset("global_idx","sum")}; - }`};return{name:"CumSum",shaderCache:{hint:o.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:[{type:12,data:u},{type:12,data:r},...se(t,t)]}),getShaderSource:i}},T1=(e,t)=>{let n=e.inputs[0].dims,o=e.inputs[0].dataType,u=e.inputs[1];e.compute(Eg(o,n,u,t),{inputs:[0]})},S1=e=>{let t=e.exclusive===1,n=e.reverse===1;return Se({exclusive:t,reverse:n})}}),Ag,kg,Pg,I1,O1,uA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Ag=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},kg=(e,t,n,o)=>{let u=[];u.push(`fn perm(i: ${o.type.indices}) -> ${n.type.indices} { + }`};return{name:"CumSum",shaderCache:{hint:i.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:[{type:12,data:u},{type:12,data:r},...ue(t,t)]}),getShaderSource:o}},s2=(e,t)=>{let n=e.inputs[0].dims,i=e.inputs[0].dataType,u=e.inputs[1];e.compute(db(i,n,u,t),{inputs:[0]})},a2=e=>{let t=e.exclusive===1,n=e.reverse===1;return ke({exclusive:t,reverse:n})}}),cb,pb,hb,u2,l2,hk=D(()=>{"use strict";de(),pe(),He(),he(),cb=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},pb=(e,t,n,i)=>{let u=[];u.push(`fn perm(i: ${i.type.indices}) -> ${n.type.indices} { var a: ${n.type.indices};`);for(let l=0;l{let n,o,u,l,d,c,s=t.format==="NHWC",r=t.blocksize,i=t.mode==="DCR";s?([n,o,u,l]=e.dims,d=i?[n,o,u,r,r,l/r**2]:[n,o,u,l/r**2,r,r],c=i?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([n,o,u,l]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],d=i?[n,r,r,l/r**2,o,u]:[n,l/r**2,r,r,o,u],c=i?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let a=e.reshape(d),p=a.dims.length,h=e.dataType,m=F("a",h,p),b=re("output",h,p),$=_=>` - ${_.registerUniform("output_size","u32").declareVariables(m,b)} +`)},hb=(e,t)=>{let n,i,u,l,d,c,s=t.format==="NHWC",r=t.blocksize,o=t.mode==="DCR";s?([n,i,u,l]=e.dims,d=o?[n,i,u,r,r,l/r**2]:[n,i,u,l/r**2,r,r],c=o?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([n,i,u,l]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],d=o?[n,r,r,l/r**2,i,u]:[n,l/r**2,r,r,i,u],c=o?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let a=e.reshape(d),p=a.dims.length,h=e.dataType,m=j("a",h,p),g=oe("output",h,p),_=w=>` + ${w.registerUniform("output_size","u32").declareVariables(m,g)} - ${kg(c,p,m,b)} + ${pb(c,p,m,g)} - ${_.mainStart()} - ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let indices = ${b.offsetToIndices("global_idx")}; + let indices = ${g.offsetToIndices("global_idx")}; let aIndices = perm(indices); - ${b.setByOffset("global_idx",m.getByIndices("aIndices"))} - }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:_=>{let y=s?[n,o*r,u*r,l/r**2]:[n,l/r**2,o*r,u*r],T=z.size(y),S=a.dims,O=z.sortBasedOnPerm(S,c);return{outputs:[{dims:y,dataType:_[0].dataType}],dispatchGroup:{x:Math.ceil(T/64)},programUniforms:[{type:12,data:T},...se(S,O)]}},getShaderSource:$}},I1=(e,t)=>{Ag(e.inputs),e.compute(Pg(e.inputs[0],t))},O1=e=>Se({blocksize:e.blocksize,mode:e.mode,format:e.format})}),Wt,Zn,co,Ba,ar,Cg,Dg,Rg,Fa,La,ja,Ng,zg,Ua,Mg,E1,A1,lA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Wt=256,Zn=512,co=2*Math.PI,Ba=e=>{let t=[],n=e;for(let o of[4,2,3,5])for(;n%o===0;)t.push(o),n/=o;return n===1?t:void 0},ar=e=>{let t=e.toPrecision(9);return/[.eE]/.test(t)?t:`${t}.0`},Cg=(e,t,n,o,u)=>{let l=n/e,d=Zn-o,c=r=>`smem[${d}u + base + ${r*t}u]`,s=` for (var t = local_idx; t < ${l}u; t += ${Wt}u) { + ${g.setByOffset("global_idx",m.getByIndices("aIndices"))} + }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:w=>{let y=s?[n,i*r,u*r,l/r**2]:[n,l/r**2,i*r,u*r],T=F.size(y),S=a.dims,O=F.sortBasedOnPerm(S,c);return{outputs:[{dims:y,dataType:w[0].dataType}],dispatchGroup:{x:Math.ceil(T/64)},programUniforms:[{type:12,data:T},...ue(S,O)]}},getShaderSource:_}},u2=(e,t)=>{cb(e.inputs),e.compute(hb(e.inputs[0],t))},l2=e=>ke({blocksize:e.blocksize,mode:e.mode,format:e.format})}),ir,hi,Po,cu,wr,fb,mb,gb,pu,hu,fu,bb,yb,mu,wb,d2,c2,fk=D(()=>{"use strict";de(),pe(),He(),he(),ir=256,hi=512,Po=2*Math.PI,cu=e=>{let t=[],n=e;for(let i of[4,2,3,5])for(;n%i===0;)t.push(i),n/=i;return n===1?t:void 0},wr=e=>{let t=e.toPrecision(9);return/[.eE]/.test(t)?t:`${t}.0`},fb=(e,t,n,i,u)=>{let l=n/e,d=hi-i,c=r=>`smem[${d}u + base + ${r*t}u]`,s=` for (var t = local_idx; t < ${l}u; t += ${ir}u) { `;s+=` let twiddleIndex = t % ${t}u; let angleUnit = f32(twiddleIndex); `,s+=` var leg: array, 5>; -`;for(let r=0;r(cos(a), sin(a))); } +`;for(let r=0;r(cos(a), sin(a))); } `}}if(s+=` let base = (t / ${t}u) * ${t*e}u + twiddleIndex; `,e===2)s+=` ${c(0)} = leg[0] + leg[1]; ${c(1)} = leg[0] - leg[1]; @@ -3129,45 +3129,45 @@ fn main(@builtin(local_invocation_id) localId : vec3, ${c(1)} = evenDiff + oddRot; `,s+=` ${c(2)} = evenSum - oddSum; ${c(3)} = evenDiff - oddRot; -`}else for(let r=0;r(leg[${a}].x*${h} - leg[${a}].y*${m}, leg[${a}].x*${m} + leg[${a}].y*${h})`)}s+=` ${c(r)} = ${i.join(" + ")}; +`}else for(let r=0;r(leg[${a}].x*${h} - leg[${a}].y*${m}, leg[${a}].x*${m} + leg[${a}].y*${h})`)}s+=` ${c(r)} = ${o.join(" + ")}; `}return`${s} } workgroupBarrier(); -`},Dg=(e,t,n)=>{let o="",u=1,l=0;for(let d of e)o+=Cg(d,u,t,l,n),u*=d,l=Zn-l;return{code:o,resultOffset:l}},Rg=(e,t,n,o,u)=>{let l=e.dims,d=l.length,c=l[d-1],s=l[t],r=n&&o?(s-1)*2:s;u!==void 0&&(r=u);let i=n&&o?1:2,a=o&&!n?Math.floor(r/2)+1:r,p=l.slice();p[t]=a,p[d-1]=i;let h=1;for(let b=t+1;b[t,e.length,e.inputComponents,e.outputComponents,e.inverse,e.onesided].join(";"),La=e=>[{type:12,data:e.batch},{type:12,data:e.signalLength},{type:12,data:e.inner},{type:12,data:e.outputLength}],ja=(e,t,n)=>e.registerUniform("batch","u32").registerUniform("signalLength","u32").registerUniform("inner","u32").registerUniform("outputLength","u32").declareVariables(t,n),Ng=e=>{let{dataType:t,length:n,inputComponents:o,outputComponents:u,inverse:l,onesided:d}=e,c=Ye(t),s=l?1:-1,r=l?1/n:1,i=Ba(n),a=p=>{let h=F("x",t,[1]),m=re("y",t,[1]),b=O=>{let A=`inBase + (${O}) * uniforms.inner * ${o}u`,P=`f32(${h.getByOffset(A)})`,I=o===2?`f32(${h.getByOffset(`${A} + 1u`)})`:"0.0";return`vec2(${P}, ${I})`},$;if(l&&d){let O=Math.floor(n/2)+1,A=n%2===0?`select(provided, provided - 1u, provided == ${O}u)`:"provided";$=` +`},mb=(e,t,n)=>{let i="",u=1,l=0;for(let d of e)i+=fb(d,u,t,l,n),u*=d,l=hi-l;return{code:i,resultOffset:l}},gb=(e,t,n,i,u)=>{let l=e.dims,d=l.length,c=l[d-1],s=l[t],r=n&&i?(s-1)*2:s;u!==void 0&&(r=u);let o=n&&i?1:2,a=i&&!n?Math.floor(r/2)+1:r,p=l.slice();p[t]=a,p[d-1]=o;let h=1;for(let g=t+1;g[t,e.length,e.inputComponents,e.outputComponents,e.inverse,e.onesided].join(";"),hu=e=>[{type:12,data:e.batch},{type:12,data:e.signalLength},{type:12,data:e.inner},{type:12,data:e.outputLength}],fu=(e,t,n)=>e.registerUniform("batch","u32").registerUniform("signalLength","u32").registerUniform("inner","u32").registerUniform("outputLength","u32").declareVariables(t,n),bb=e=>{let{dataType:t,length:n,inputComponents:i,outputComponents:u,inverse:l,onesided:d}=e,c=rt(t),s=l?1:-1,r=l?1/n:1,o=cu(n),a=p=>{let h=j("x",t,[1]),m=oe("y",t,[1]),g=O=>{let A=`inBase + (${O}) * uniforms.inner * ${i}u`,P=`f32(${h.getByOffset(A)})`,I=i===2?`f32(${h.getByOffset(`${A} + 1u`)})`:"0.0";return`vec2(${P}, ${I})`},_;if(l&&d){let O=Math.floor(n/2)+1,A=n%2===0?`select(provided, provided - 1u, provided == ${O}u)`:"provided";_=` let provided = min(uniforms.signalLength, ${O}u); - for (var i = local_idx; i < ${n}u; i += ${Wt}u) { - if (i < provided) { smem[i] = ${b("i")}; } else { smem[i] = vec2(0.0); } + for (var i = local_idx; i < ${n}u; i += ${ir}u) { + if (i < provided) { smem[i] = ${g("i")}; } else { smem[i] = vec2(0.0); } } workgroupBarrier(); - for (var k = local_idx + 1u; k < ${A}; k += ${Wt}u) { + for (var k = local_idx + 1u; k < ${A}; k += ${ir}u) { let h = smem[k]; smem[${n}u - k] = vec2(h.x, -h.y); } - workgroupBarrier();`}else $=` + workgroupBarrier();`}else _=` let loadCount = min(uniforms.signalLength, ${n}u); - for (var i = local_idx; i < ${n}u; i += ${Wt}u) { - if (i < loadCount) { smem[i] = ${b("i")}; } else { smem[i] = vec2(0.0); } + for (var i = local_idx; i < ${n}u; i += ${ir}u) { + if (i < loadCount) { smem[i] = ${g("i")}; } else { smem[i] = vec2(0.0); } } - workgroupBarrier();`;let{code:_,resultOffset:y}=Dg(i,n,s),T=r===1?`smem[${y}u + i]`:`smem[${y}u + i] * ${ar(r)}`,S=u===2?m.setByOffset("off + 1u",`${c}(v.y)`):"";return` - ${ja(p,h,m)} - var smem: array, ${2*Zn}>; + workgroupBarrier();`;let{code:w,resultOffset:y}=mb(o,n,s),T=r===1?`smem[${y}u + i]`:`smem[${y}u + i] * ${wr(r)}`,S=u===2?m.setByOffset("off + 1u",`${c}(v.y)`):"";return` + ${fu(p,h,m)} + var smem: array, ${2*hi}>; fn cmul(a: vec2, b: vec2) -> vec2 { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } - ${p.mainStart(Wt)} + ${p.mainStart(ir)} let row = workgroup_index; if (row >= uniforms.batch) { return; } let outer = row / uniforms.inner; let within = row % uniforms.inner; - let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${o}u; + let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${i}u; let outBase = (outer * uniforms.outputLength * uniforms.inner + within) * ${u}u; - ${$} -${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { + ${_} +${w} for (var i = local_idx; i < uniforms.outputLength; i += ${ir}u) { let v = ${T}; let off = outBase + i * uniforms.inner * ${u}u; ${m.setByOffset("off",`${c}(v.x)`)} ${S} } - }`};return{name:"DFT",shaderCache:{hint:Fa(e,"fft"),inputDependencies:["type"]},getShaderSource:a,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:La(e),dispatchGroup:{x:e.batch}})}},zg=e=>{let{dataType:t,length:n,inputComponents:o,outputComponents:u,inverse:l,onesided:d}=e,c=Ye(t),s=l?1:-1,r=l?1/n:1,i=a=>{let p=F("x",t,[1]),h=re("y",t,[1]),m=T=>{let S=`inBase + (${T}) * uniforms.inner * ${o}u`,O=`f32(${p.getByOffset(S)})`,A=o===2?`f32(${p.getByOffset(`${S} + 1u`)})`:"0.0";return`vec2(${O}, ${A})`},b=l&&d?`fn spectrum(inBase: u32, k: u32) -> vec2 { + }`};return{name:"DFT",shaderCache:{hint:pu(e,"fft"),inputDependencies:["type"]},getShaderSource:a,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:hu(e),dispatchGroup:{x:e.batch}})}},yb=e=>{let{dataType:t,length:n,inputComponents:i,outputComponents:u,inverse:l,onesided:d}=e,c=rt(t),s=l?1:-1,r=l?1/n:1,o=a=>{let p=j("x",t,[1]),h=oe("y",t,[1]),m=T=>{let S=`inBase + (${T}) * uniforms.inner * ${i}u`,O=`f32(${p.getByOffset(S)})`,A=i===2?`f32(${p.getByOffset(`${S} + 1u`)})`:"0.0";return`vec2(${O}, ${A})`},g=l&&d?`fn spectrum(inBase: u32, k: u32) -> vec2 { let provided = min(uniforms.signalLength, ${Math.floor(n/2)+1}u); if (k < provided) { return ${m("k")}; } let m = ${n}u - k; @@ -3179,35 +3179,35 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { }`:`fn spectrum(inBase: u32, n: u32) -> vec2 { if (n < uniforms.signalLength) { return ${m("n")}; } return vec2(0.0, 0.0); - }`,$=` - let angle = ${ar(s*co)} * f32(knMod) / ${ar(n)}; + }`,_=` + let angle = ${wr(s*Po)} * f32(knMod) / ${wr(n)}; acc += cmul(spectrum(inBase, n), vec2(cos(angle), sin(angle))); knMod += k; - if (knMod >= ${n}u) { knMod -= ${n}u; }`,_=u===2?h.setByOffset("off + 1u",`${c}(v.y)`):"",y=r===1?"acc":`acc * ${ar(r)}`;return` - ${ja(a,p,h)} + if (knMod >= ${n}u) { knMod -= ${n}u; }`,w=u===2?h.setByOffset("off + 1u",`${c}(v.y)`):"",y=r===1?"acc":`acc * ${wr(r)}`;return` + ${fu(a,p,h)} fn cmul(a: vec2, b: vec2) -> vec2 { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } - ${b} - ${a.mainStart(Wt)} + ${g} + ${a.mainStart(ir)} let row = workgroup_index; if (row >= uniforms.batch) { return; } let outer = row / uniforms.inner; let within = row % uniforms.inner; - let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${o}u; + let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${i}u; let outBase = (outer * uniforms.outputLength * uniforms.inner + within) * ${u}u; - for (var k = local_idx; k < uniforms.outputLength; k += ${Wt}u) { + for (var k = local_idx; k < uniforms.outputLength; k += ${ir}u) { var acc = vec2(0.0, 0.0); var knMod = 0u; - for (var n = 0u; n < ${n}u; n++) {${$} + for (var n = 0u; n < ${n}u; n++) {${_} } let v = ${y}; let off = outBase + k * uniforms.inner * ${u}u; ${h.setByOffset("off",`${c}(v.x)`)} - ${_} + ${w} } - }`};return{name:"DFT",shaderCache:{hint:Fa(e,"direct"),inputDependencies:["type"]},getShaderSource:i,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:La(e),dispatchGroup:{x:e.batch}})}},Ua=e=>{if(!e||e.dataType===0)return;if(z.size(e.dims)!==1)throw new Error("DFT optional scalar inputs must have exactly 1 element.");if(e.dataType===6)return e.getInt32Array()[0];let t=Number(e.getBigInt64Array()[0]);if(!Number.isSafeInteger(t))throw new Error("DFT optional scalar inputs are out of JavaScript safe integer range.");return t},Mg=e=>{if(!e||e.length<1)throw new Error("DFT requires at least 1 input.");let t=e[0].dims;if(t.length<2)throw new Error("DFT input must have at least 2 dimensions.");let n=t[t.length-1];if(n!==1&&n!==2)throw new Error("DFT input's innermost dimension must be 1 (real) or 2 (complex).")},E1=(e,t)=>{Mg(e.inputs);let n=e.inputs[0],o=n.dims.length,u=t.inverse!==0,l=t.onesided!==0,d=Ua(e.inputs[1]);if(d!==void 0&&d<=0)throw new Error("dft_length must be greater than zero.");let c=z.normalizeAxis(Ua(e.inputs[2])??t.axis,o);if(c===o-1)throw new Error("DFT axis must refer to a signal dimension, not the innermost (real/imaginary) dimension.");if(u&&l&&n.dims[o-1]!==2)throw new Error("Inverse one-sided DFT (IRFFT) requires complex-valued input (innermost dimension 2).");let s=Rg(n,c,u,l,d);if(s.length<=0)throw new Error(`Invalid DFT length: ${s.length}`);let r=s.length<=Zn&&Ba(s.length)!==void 0?Ng(s):zg(s);e.compute(r,{inputs:[0]})},A1=e=>Se({axis:e.axis??1,inverse:e.inverse??0,onesided:e.onesided??0})}),po,Yn,Va,Bg,Fg,Lg,jg,qa,Ug,k1,P1,dA=D(()=>{"use strict";ae(),de(),Ve(),ce(),po="[a-zA-Z]|\\.\\.\\.",Yn="("+po+")+",Va="^"+Yn+"$",Bg="("+Yn+",)*"+Yn,Fg="^"+Bg+"$",Lg=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let n=this.symbolToIndices.get(e);n===void 0?n=[t]:n.push(t),this.symbolToIndices.set(e,n)}},jg=class{constructor(e,t){this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,o]=t.includes("->")?t.split("->",2):[t,""];if(!n.match(RegExp(Fg)))throw new Error("Invalid LHS term");if(n.split(",").forEach((u,l)=>{let d=e[l].dims.slice();if(!u.match(RegExp(Va)))throw new Error("Invalid LHS term");let c=this.processTerm(u,!0,d,l);this.lhs.push(c)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([u,l])=>l.count===1||u==="...").map(([u])=>u).join("");else if(!o.match(RegExp(Yn)))throw new Error("Invalid RHS");o.match(RegExp(po,"g"))?.forEach(u=>{if(u==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let l=this.symbolToInfo.get(u);if(l===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(l.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(e,t,n){let o=this.symbolToInfo.get(e);if(o!==void 0){if(o.dimValue!==t&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(n)}else o={count:1,dimValue:t,inputIndices:[n]};this.symbolToInfo.set(e,o)}processTerm(e,t,n,o=-1){let u=n.length,l=!1,d=[],c=0;if(!e.match(RegExp(Va))&&!t&&e!=="")throw new Error("Invalid LHS term");let s=e.match(RegExp(po,"g")),r=new Lg(o);return s?.forEach((i,a)=>{if(i==="..."){if(l)throw new Error("Only one ellipsis is allowed per input term");l=!0;let p=u-s.length+1;if(p<0)throw new Error("Ellipsis out of bounds");if(d=n.slice(c,c+p),this.hasEllipsis){if(this.ellipsisDims.length!==d.length||this.ellipsisDims.toString()!==d.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=d;else throw new Error("Ellipsis must be specified in the LHS");for(let h=0;he+"_max",Ug=(e,t,n,o)=>{let u=e.map(r=>r.length).map((r,i)=>F(`input${i}`,t,r)),l=z.size(o),d=re("output",t,o.length),c=[...n.symbolToInfo.keys()].filter(r=>!n.rhs.symbolToIndices.has(r)),s=r=>{let i=[],a="var prod = 1.0;",p="var sum = 0.0;",h="sum += prod;",m=[],b=[],$=[],_=[],y=n.symbolToInfo.size===n.rhs.symbolToIndices.size;n.symbolToInfo.forEach((S,O)=>{if(n.rhs.symbolToIndices.has(O)){let A=n.rhs.symbolToIndices.get(O)?.[0];A!==void 0&&n.lhs.forEach((P,I)=>{if(S.inputIndices.includes(I)){let L=P.symbolToIndices.get(O);if(L===void 0)throw new Error("Invalid symbol error");L.forEach(V=>{i.push(`${u[I].indicesSet(`input${I}Indices`,V,d.indicesGet("outputIndices",A))}`)})}})}else n.lhs.forEach((A,P)=>{if(S.inputIndices.includes(P)){let I=A.symbolToIndices.get(O);if(I===void 0)throw new Error("Invalid symbol error");I.forEach(L=>{m.push(`${u[P].indicesSet(`input${P}Indices`,L,`${O}`)}`)}),_.push(`prod *= ${u[P].getByIndices(`input${P}Indices`)};`)}}),b.push(`for(var ${O}: u32 = 0; ${O} < uniforms.${qa(O)}; ${O}++) {`),$.push("}")});let T=y?[...i,`let sum = ${u.map((S,O)=>S.getByIndices(`input${O}Indices`)).join(" * ")};`]:[...i,p,...b,...m,a,..._,h,...$];return` - ${r.registerUniforms(c.map(S=>({name:`${qa(S)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...u,d)} + }`};return{name:"DFT",shaderCache:{hint:pu(e,"direct"),inputDependencies:["type"]},getShaderSource:o,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:hu(e),dispatchGroup:{x:e.batch}})}},mu=e=>{if(!e||e.dataType===0)return;if(F.size(e.dims)!==1)throw new Error("DFT optional scalar inputs must have exactly 1 element.");if(e.dataType===6)return e.getInt32Array()[0];let t=Number(e.getBigInt64Array()[0]);if(!Number.isSafeInteger(t))throw new Error("DFT optional scalar inputs are out of JavaScript safe integer range.");return t},wb=e=>{if(!e||e.length<1)throw new Error("DFT requires at least 1 input.");let t=e[0].dims;if(t.length<2)throw new Error("DFT input must have at least 2 dimensions.");let n=t[t.length-1];if(n!==1&&n!==2)throw new Error("DFT input's innermost dimension must be 1 (real) or 2 (complex).")},d2=(e,t)=>{wb(e.inputs);let n=e.inputs[0],i=n.dims.length,u=t.inverse!==0,l=t.onesided!==0,d=mu(e.inputs[1]);if(d!==void 0&&d<=0)throw new Error("dft_length must be greater than zero.");let c=F.normalizeAxis(mu(e.inputs[2])??t.axis,i);if(c===i-1)throw new Error("DFT axis must refer to a signal dimension, not the innermost (real/imaginary) dimension.");if(u&&l&&n.dims[i-1]!==2)throw new Error("Inverse one-sided DFT (IRFFT) requires complex-valued input (innermost dimension 2).");let s=gb(n,c,u,l,d);if(s.length<=0)throw new Error(`Invalid DFT length: ${s.length}`);let r=s.length<=hi&&cu(s.length)!==void 0?bb(s):yb(s);e.compute(r,{inputs:[0]})},c2=e=>ke({axis:e.axis??1,inverse:e.inverse??0,onesided:e.onesided??0})}),Co,fi,gu,_b,vb,xb,$b,bu,Tb,p2,h2,mk=D(()=>{"use strict";de(),pe(),He(),he(),Co="[a-zA-Z]|\\.\\.\\.",fi="("+Co+")+",gu="^"+fi+"$",_b="("+fi+",)*"+fi,vb="^"+_b+"$",xb=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let n=this.symbolToIndices.get(e);n===void 0?n=[t]:n.push(t),this.symbolToIndices.set(e,n)}},$b=class{constructor(e,t){this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,i]=t.includes("->")?t.split("->",2):[t,""];if(!n.match(RegExp(vb)))throw new Error("Invalid LHS term");if(n.split(",").forEach((u,l)=>{let d=e[l].dims.slice();if(!u.match(RegExp(gu)))throw new Error("Invalid LHS term");let c=this.processTerm(u,!0,d,l);this.lhs.push(c)}),i==="")i+=[...this.symbolToInfo.entries()].filter(([u,l])=>l.count===1||u==="...").map(([u])=>u).join("");else if(!i.match(RegExp(fi)))throw new Error("Invalid RHS");i.match(RegExp(Co,"g"))?.forEach(u=>{if(u==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let l=this.symbolToInfo.get(u);if(l===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(l.dimValue)}}),this.rhs=this.processTerm(i,!1,this.outputDims)}addSymbol(e,t,n){let i=this.symbolToInfo.get(e);if(i!==void 0){if(i.dimValue!==t&&i.count!==1)throw new Error("Dimension mismatch");i.count++,i.inputIndices.push(n)}else i={count:1,dimValue:t,inputIndices:[n]};this.symbolToInfo.set(e,i)}processTerm(e,t,n,i=-1){let u=n.length,l=!1,d=[],c=0;if(!e.match(RegExp(gu))&&!t&&e!=="")throw new Error("Invalid LHS term");let s=e.match(RegExp(Co,"g")),r=new xb(i);return s?.forEach((o,a)=>{if(o==="..."){if(l)throw new Error("Only one ellipsis is allowed per input term");l=!0;let p=u-s.length+1;if(p<0)throw new Error("Ellipsis out of bounds");if(d=n.slice(c,c+p),this.hasEllipsis){if(this.ellipsisDims.length!==d.length||this.ellipsisDims.toString()!==d.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=d;else throw new Error("Ellipsis must be specified in the LHS");for(let h=0;he+"_max",Tb=(e,t,n,i)=>{let u=e.map(r=>r.length).map((r,o)=>j(`input${o}`,t,r)),l=F.size(i),d=oe("output",t,i.length),c=[...n.symbolToInfo.keys()].filter(r=>!n.rhs.symbolToIndices.has(r)),s=r=>{let o=[],a="var prod = 1.0;",p="var sum = 0.0;",h="sum += prod;",m=[],g=[],_=[],w=[],y=n.symbolToInfo.size===n.rhs.symbolToIndices.size;n.symbolToInfo.forEach((S,O)=>{if(n.rhs.symbolToIndices.has(O)){let A=n.rhs.symbolToIndices.get(O)?.[0];A!==void 0&&n.lhs.forEach((P,I)=>{if(S.inputIndices.includes(I)){let z=P.symbolToIndices.get(O);if(z===void 0)throw new Error("Invalid symbol error");z.forEach(G=>{o.push(`${u[I].indicesSet(`input${I}Indices`,G,d.indicesGet("outputIndices",A))}`)})}})}else n.lhs.forEach((A,P)=>{if(S.inputIndices.includes(P)){let I=A.symbolToIndices.get(O);if(I===void 0)throw new Error("Invalid symbol error");I.forEach(z=>{m.push(`${u[P].indicesSet(`input${P}Indices`,z,`${O}`)}`)}),w.push(`prod *= ${u[P].getByIndices(`input${P}Indices`)};`)}}),g.push(`for(var ${O}: u32 = 0; ${O} < uniforms.${bu(O)}; ${O}++) {`),_.push("}")});let T=y?[...o,`let sum = ${u.map((S,O)=>S.getByIndices(`input${O}Indices`)).join(" * ")};`]:[...o,p,...g,...m,a,...w,h,..._];return` + ${r.registerUniforms(c.map(S=>({name:`${bu(S)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...u,d)} ${r.mainStart()} ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} @@ -3217,19 +3217,19 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { ${T.join(` `)}; ${d.setByOffset("global_idx","sum")}; - }`};return{name:"Einsum",shaderCache:{hint:n.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let r=c.filter(a=>n.symbolToInfo.has(a)).map(a=>({type:12,data:n.symbolToInfo.get(a)?.dimValue||0}));r.push({type:12,data:l});let i=e.map((a,p)=>[...se(a)]).reduce((a,p)=>a.concat(p),r);return i.push(...se(o)),{outputs:[{dims:o,dataType:t}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:i}},getShaderSource:s}},k1=(e,t)=>{let n=new jg(e.inputs,t.equation),o=n.outputDims,u=e.inputs.map((l,d)=>l.dims);e.compute(Ug(u,e.inputs[0].dataType,n,o))},P1=e=>{let t=e.equation.replace(/\s+/g,"");return Se({equation:t})}}),Vg,Ga,qg,Gg,C1,cA=D(()=>{"use strict";ae(),de(),ce(),Vg=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),o=n.length{let n=e.length-t.length,o=[];for(let u=0;ue.length>t.length?Ga(e,t):Ga(t,e),Gg=e=>{let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),o=qg(t,n),u=e[0].dataType,l=u===9||z.size(t)===1,d=u===9||t.length>0&&t[t.length-1]%4===0?4:1,c=l||o.length>0&&o[o.length-1]%4===0?4:1,s=Math.ceil(z.size(o)/c),r=a=>{let p=F("input",u,t.length,d),h=re("output",u,o.length,c),m;if(u===9){let b=($,_,y="")=>` - let outputIndices${_} = ${h.offsetToIndices(`outputOffset + ${_}u`)}; - let offset${_} = ${p.broadcastedIndicesToOffset(`outputIndices${_}`,h)}; - let index${_} = offset${_} / 4u; - let component${_} = offset${_} % 4u; - ${$}[${_}] = ${y}(${p.getByOffset(`index${_}`)}[component${_}]); + }`};return{name:"Einsum",shaderCache:{hint:n.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let r=c.filter(a=>n.symbolToInfo.has(a)).map(a=>({type:12,data:n.symbolToInfo.get(a)?.dimValue||0}));r.push({type:12,data:l});let o=e.map((a,p)=>[...ue(a)]).reduce((a,p)=>a.concat(p),r);return o.push(...ue(i)),{outputs:[{dims:i,dataType:t}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:o}},getShaderSource:s}},p2=(e,t)=>{let n=new $b(e.inputs,t.equation),i=n.outputDims,u=e.inputs.map((l,d)=>l.dims);e.compute(Tb(u,e.inputs[0].dataType,n,i))},h2=e=>{let t=e.equation.replace(/\s+/g,"");return ke({equation:t})}}),Sb,yu,Ib,Ob,f2,gk=D(()=>{"use strict";de(),pe(),he(),Sb=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),i=n.length{let n=e.length-t.length,i=[];for(let u=0;ue.length>t.length?yu(e,t):yu(t,e),Ob=e=>{let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),i=Ib(t,n),u=e[0].dataType,l=u===9||F.size(t)===1,d=u===9||t.length>0&&t[t.length-1]%4===0?4:1,c=l||i.length>0&&i[i.length-1]%4===0?4:1,s=Math.ceil(F.size(i)/c),r=a=>{let p=j("input",u,t.length,d),h=oe("output",u,i.length,c),m;if(u===9){let g=(_,w,y="")=>` + let outputIndices${w} = ${h.offsetToIndices(`outputOffset + ${w}u`)}; + let offset${w} = ${p.broadcastedIndicesToOffset(`outputIndices${w}`,h)}; + let index${w} = offset${w} / 4u; + let component${w} = offset${w} % 4u; + ${_}[${w}] = ${y}(${p.getByOffset(`index${w}`)}[component${w}]); `;m=` let outputOffset = global_idx * ${c}; var data = vec4(0); - ${b("data",0,"u32")} - ${b("data",1,"u32")} - ${b("data",2,"u32")} - ${b("data",3,"u32")} + ${g("data",0,"u32")} + ${g("data",1,"u32")} + ${g("data",2,"u32")} + ${g("data",3,"u32")} ${h.setByOffset("global_idx","data")} }`}else m=` let outputIndices = ${h.offsetToIndices(`global_idx * ${c}`)}; @@ -3240,54 +3240,54 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { ${a.registerUniform("vec_size","u32").declareVariables(p,h)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} - ${m}`},i=[{type:12,data:s},...se(t,o)];return{name:"Expand",shaderCache:{hint:`${o.length};${d}${c}`,inputDependencies:["rank"]},getShaderSource:r,getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:i})}},C1=e=>{Vg(e.inputs),e.compute(Gg(e.inputs),{inputs:[0]})}}),Hg,D1,pA=D(()=>{"use strict";ae(),de(),ce(),jl(),Hg=e=>{let t=e[0].dataType,n=z.size(e[0].dims),o=z.size(e[1].dims),u=o%4===0,l=d=>{let c=F("x",t,[1],4),s=F("bias",t,[1],4),r=re("y",t,[1],4),i=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],a=h=>` + ${m}`},o=[{type:12,data:s},...ue(t,i)];return{name:"Expand",shaderCache:{hint:`${i.length};${d}${c}`,inputDependencies:["rank"]},getShaderSource:r,getRunData:()=>({outputs:[{dims:i,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:o})}},f2=e=>{Sb(e.inputs),e.compute(Ob(e.inputs),{inputs:[0]})}}),Eb,m2,bk=D(()=>{"use strict";de(),pe(),he(),fd(),Eb=e=>{let t=e[0].dataType,n=F.size(e[0].dims),i=F.size(e[1].dims),u=i%4===0,l=d=>{let c=j("x",t,[1],4),s=j("bias",t,[1],4),r=oe("y",t,[1],4),o=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],a=h=>` let bias${h}_offset: u32 = (global_idx * 4 + ${h}) % uniforms.bias_size; let bias${h} = ${s.getByOffset(`bias${h}_offset / 4`)}[bias${h}_offset % 4];`,p=u?` let bias = ${s.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${a(0)}${a(1)}${a(2)}${a(3)} - let bias = ${c.type.value}(bias0, bias1, bias2, bias3);`;return`${d.registerUniforms(i).declareVariables(c,s,r)} + let bias = ${c.type.value}(bias0, bias1, bias2, bias3);`;return`${d.registerUniforms(o).declareVariables(c,s,r)} - ${ul(Ye(t))} + ${Ml(rt(t))} - ${d.mainStart($n)} + ${d.mainStart(Fn)} ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} let x = ${c.getByOffset("global_idx")}; ${p} let x_in = x + bias; - ${r.setByOffset("global_idx",ll("x_in"))} - }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${u}`,inputDependencies:["type","type"]},getShaderSource:l,getRunData:d=>({outputs:[{dims:d[0].dims,dataType:d[0].dataType}],programUniforms:[{type:12,data:Math.ceil(n/4)},{type:12,data:o}],dispatchGroup:{x:Math.ceil(n/$n/4)}})}},D1=e=>{e.inputs.length<2||z.size(e.inputs[1].dims)===0?Yx(e):e.compute(Hg(e.inputs))}}),Wg,Kg,R1,N1,hA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Wg=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Kg=(e,t)=>{let n=e[0].dims,o=e[1].dims,u=n.length,l=z.normalizeAxis(t.axis,u),d=n.slice(0);d.splice(l,1,...o);let c=n[l],s=e[0].dataType===9?4:1,r=Math.ceil(z.size(d)/s),i=[{type:12,data:r},{type:6,data:c},{type:12,data:l},...se(e[0].dims,e[1].dims,d)],a=p=>{let h=F("data",e[0].dataType,e[0].dims.length,s),m=F("inputIndices",e[1].dataType,e[1].dims.length),b=re("output",e[0].dataType,d.length,s),$=y=>{let T=o.length,S=`var indicesIndices${y} = ${m.type.indices}(0);`;for(let O=0;O1?`indicesIndices${y}[${O}]`:`indicesIndices${y}`} = ${d.length>1?`outputIndices${y}[uniforms.axis + ${O}]`:`outputIndices${y}`};`;S+=` + ${r.setByOffset("global_idx",Nl("x_in"))} + }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${u}`,inputDependencies:["type","type"]},getShaderSource:l,getRunData:d=>({outputs:[{dims:d[0].dims,dataType:d[0].dataType}],programUniforms:[{type:12,data:Math.ceil(n/4)},{type:12,data:i}],dispatchGroup:{x:Math.ceil(n/Fn/4)}})}},m2=e=>{e.inputs.length<2||F.size(e.inputs[1].dims)===0?R1(e):e.compute(Eb(e.inputs))}}),Ab,kb,g2,b2,yk=D(()=>{"use strict";de(),pe(),He(),he(),Ab=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},kb=(e,t)=>{let n=e[0].dims,i=e[1].dims,u=n.length,l=F.normalizeAxis(t.axis,u),d=n.slice(0);d.splice(l,1,...i);let c=n[l],s=e[0].dataType===9?4:1,r=Math.ceil(F.size(d)/s),o=[{type:12,data:r},{type:6,data:c},{type:12,data:l},...ue(e[0].dims,e[1].dims,d)],a=p=>{let h=j("data",e[0].dataType,e[0].dims.length,s),m=j("inputIndices",e[1].dataType,e[1].dims.length),g=oe("output",e[0].dataType,d.length,s),_=y=>{let T=i.length,S=`var indicesIndices${y} = ${m.type.indices}(0);`;for(let O=0;O1?`indicesIndices${y}[${O}]`:`indicesIndices${y}`} = ${d.length>1?`outputIndices${y}[uniforms.axis + ${O}]`:`outputIndices${y}`};`;S+=` var idx${y} = ${m.getByIndices(`indicesIndices${y}`)}; if (idx${y} < 0) { idx${y} = idx${y} + uniforms.axisDimLimit; } var dataIndices${y} : ${h.type.indices}; - `;for(let O=0,A=0;O1?`dataIndices${y}[${O}]`:`dataIndices${y}`} = u32(idx${y});`,A+=T):(S+=`${u>1?`dataIndices${y}[${O}]`:`dataIndices${y}`} = ${d.length>1?`outputIndices${y}[${A}]`:`outputIndices${y}`};`,A++);return S},_;if(e[0].dataType===9){let y=(T,S,O="")=>` - let outputIndices${S} = ${b.offsetToIndices(`outputOffset + ${S}u`)}; - ${$(S)}; + `;for(let O=0,A=0;O1?`dataIndices${y}[${O}]`:`dataIndices${y}`} = u32(idx${y});`,A+=T):(S+=`${u>1?`dataIndices${y}[${O}]`:`dataIndices${y}`} = ${d.length>1?`outputIndices${y}[${A}]`:`outputIndices${y}`};`,A++);return S},w;if(e[0].dataType===9){let y=(T,S,O="")=>` + let outputIndices${S} = ${g.offsetToIndices(`outputOffset + ${S}u`)}; + ${_(S)}; let offset${S} = ${h.indicesToOffset(`dataIndices${S}`)}; let index${S} = offset${S} / 4u; let component${S} = offset${S} % 4u; ${T}[${S}] = ${O}(${h.getByOffset(`index${S}`)}[component${S}]); - `;_=` + `;w=` let outputOffset = global_idx * ${s}; var value = vec4(0); ${y("value",0,"u32")} ${y("value",1,"u32")} ${y("value",2,"u32")} ${y("value",3,"u32")} - ${b.setByOffset("global_idx","value")} - `}else _=` - let outputIndices = ${b.offsetToIndices("global_idx")}; - ${$("")}; + ${g.setByOffset("global_idx","value")} + `}else w=` + let outputIndices = ${g.offsetToIndices("global_idx")}; + ${_("")}; let value = ${h.getByIndices("dataIndices")}; - ${b.setByOffset("global_idx","value")}; + ${g.setByOffset("global_idx","value")}; `;return` - ${p.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(h,m,b)} + ${p.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(h,m,g)} ${p.mainStart()} ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - ${_} - }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:i}),getShaderSource:a}},R1=e=>Se({axis:e.axis}),N1=(e,t)=>{let n=e.inputs;Wg(n),e.compute(Kg(e.inputs,t))}}),Xg,z1,M1,fA=D(()=>{"use strict";ae(),de(),ce(),Xg=(e,t,n,o,u,l,d,c,s)=>{let r=[{type:12,data:l},{type:12,data:o},{type:12,data:u},{type:12,data:n},{type:12,data:d},{type:12,data:c},{type:12,data:s}],i=[l];r.push(...se(t.dims,i));let a=p=>{let h=F("indices_data",t.dataType,t.dims.length),m=re("input_slice_offsets_data",12,1,1),b=[h,m],$=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:u.length},{name:"sizes_from_slice_dims_data",type:"u32",length:n.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return` - ${p.registerUniforms($).declareVariables(...b)} + ${w} + }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:o}),getShaderSource:a}},g2=e=>ke({axis:e.axis}),b2=(e,t)=>{let n=e.inputs;Ab(n),e.compute(kb(e.inputs,t))}}),Pb,y2,w2,wk=D(()=>{"use strict";de(),pe(),he(),Pb=(e,t,n,i,u,l,d,c,s)=>{let r=[{type:12,data:l},{type:12,data:i},{type:12,data:u},{type:12,data:n},{type:12,data:d},{type:12,data:c},{type:12,data:s}],o=[l];r.push(...ue(t.dims,o));let a=p=>{let h=j("indices_data",t.dataType,t.dims.length),m=oe("input_slice_offsets_data",12,1,1),g=[h,m],_=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:u.length},{name:"sizes_from_slice_dims_data",type:"u32",length:n.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return` + ${p.registerUniforms(_).declareVariables(...g)} ${p.mainStart()} ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let batch_idx = global_idx / uniforms.num_slices_per_batch; @@ -3305,34 +3305,34 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { } input_slice_offsets_data[global_idx] = base_offset + u32(relative_slice_offset); - }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${u.length}_${n.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:i,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:r}),getShaderSource:a},{inputs:[t],outputs:[-1]})[0]},z1=(e,t)=>{let n=e.inputs,o=n[0].dims,u=n[0].dataType,l=n[1].dims,d=l[l.length-1],c=z.sizeToDimension(l,l.length-1),s=z.sizeFromDimension(o,t.batchDims+d),r=z.sizeToDimension(o,t.batchDims),i=z.sizeFromDimension(o,t.batchDims),a=c/r,p=new Array(d),h=s;for(let S=0;So.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let $=l.slice(0,-1).concat(o.slice(b)),_=z.size($),y=[{type:12,data:_},{type:12,data:s},...se(n[0].dims,m.dims,$)],T=S=>{let O=F("data",n[0].dataType,n[0].dims.length),A=F("slice_offsets",12,m.dims.length),P=re("output",n[0].dataType,$.length);return` + }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${u.length}_${n.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:r}),getShaderSource:a},{inputs:[t],outputs:[-1]})[0]},y2=(e,t)=>{let n=e.inputs,i=n[0].dims,u=n[0].dataType,l=n[1].dims,d=l[l.length-1],c=F.sizeToDimension(l,l.length-1),s=F.sizeFromDimension(i,t.batchDims+d),r=F.sizeToDimension(i,t.batchDims),o=F.sizeFromDimension(i,t.batchDims),a=c/r,p=new Array(d),h=s;for(let S=0;Si.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let _=l.slice(0,-1).concat(i.slice(g)),w=F.size(_),y=[{type:12,data:w},{type:12,data:s},...ue(n[0].dims,m.dims,_)],T=S=>{let O=j("data",n[0].dataType,n[0].dims.length),A=j("slice_offsets",12,m.dims.length),P=oe("output",n[0].dataType,_.length);return` ${S.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(O,A,P)} ${S.mainStart()} ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let slice_offset = slice_offsets[global_idx / uniforms.slice_size]; output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size]; - }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:$,dataType:u}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:y}),getShaderSource:T},{inputs:[n[0],m]})},M1=e=>({batchDims:e.batch_dims,cacheKey:""})}),Zg,Yg,B1,F1,mA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Zg=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let n=z.normalizeAxis(t.quantizeAxis,e[0].dims.length),o=t.blockSize,u=e[0],l=e[2],d=e.length===4?e[3]:void 0;if(l.dims.length!==u.dims.length||!u.dims.map((c,s)=>s===n?Math.ceil(c/o)===l.dims[s]:c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(d){if(d.dataType!==u.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(d.dims.length!==l.dims.length||!d.dims.map((c,s)=>c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},Yg=(e,t)=>{let n=e[0].dims,o=e[1].dims,u=n.length,l=z.normalizeAxis(t.gatherAxis,u),d=z.normalizeAxis(t.quantizeAxis,u),c=n.slice(0);c.splice(l,1,...o);let s=z.size(c),r=e[2].dataType,i=e[0].dataType===22,a=[{type:12,data:s},{type:12,data:d},{type:12,data:l},{type:12,data:t.blockSize},...se(...e.map((h,m)=>h.dims),c)],p=h=>{let m=F("data",e[0].dataType,e[0].dims.length),b=F("inputIndices",e[1].dataType,e[1].dims.length),$=F("scales",e[2].dataType,e[2].dims.length),_=e.length>3?F("zeroPoint",e[3].dataType,e[3].dims.length):void 0,y=re("output",r,c.length),T=[m,b,$];_&&T.push(_);let S=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` + }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:_,dataType:u}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:y}),getShaderSource:T},{inputs:[n[0],m]})},w2=e=>({batchDims:e.batch_dims,cacheKey:""})}),Cb,Rb,_2,v2,_k=D(()=>{"use strict";de(),pe(),He(),he(),Cb=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let n=F.normalizeAxis(t.quantizeAxis,e[0].dims.length),i=t.blockSize,u=e[0],l=e[2],d=e.length===4?e[3]:void 0;if(l.dims.length!==u.dims.length||!u.dims.map((c,s)=>s===n?Math.ceil(c/i)===l.dims[s]:c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(d){if(d.dataType!==u.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(d.dims.length!==l.dims.length||!d.dims.map((c,s)=>c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},Rb=(e,t)=>{let n=e[0].dims,i=e[1].dims,u=n.length,l=F.normalizeAxis(t.gatherAxis,u),d=F.normalizeAxis(t.quantizeAxis,u),c=n.slice(0);c.splice(l,1,...i);let s=F.size(c),r=e[2].dataType,o=e[0].dataType===22,a=[{type:12,data:s},{type:12,data:d},{type:12,data:l},{type:12,data:t.blockSize},...ue(...e.map((h,m)=>h.dims),c)],p=h=>{let m=j("data",e[0].dataType,e[0].dims.length),g=j("inputIndices",e[1].dataType,e[1].dims.length),_=j("scales",e[2].dataType,e[2].dims.length),w=e.length>3?j("zeroPoint",e[3].dataType,e[3].dims.length):void 0,y=oe("output",r,c.length),T=[m,g,_];w&&T.push(w);let S=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` ${h.registerUniforms(S).declareVariables(...T,y)} ${h.mainStart()} let output_indices = ${y.offsetToIndices("global_idx")}; - var indices_indices = ${b.type.indices}(0); - ${o.length>1?` - for (var i: u32 = 0; i < ${o.length}; i++) { + var indices_indices = ${g.type.indices}(0); + ${i.length>1?` + for (var i: u32 = 0; i < ${i.length}; i++) { let index = ${y.indicesGet("output_indices","uniforms.gather_axis + i")}; - ${b.indicesSet("indices_indices","i","index")}; + ${g.indicesSet("indices_indices","i","index")}; }`:`indices_indices = ${y.indicesGet("output_indices","uniforms.gather_axis")};`}; var data_indices = ${m.type.indices}(0); for (var i: u32 = 0; i < uniforms.gather_axis; i++) { let index = ${y.indicesGet("output_indices","i")}; ${m.indicesSet("data_indices","i","index")}; } - var index_from_indices = ${b.getByIndices("indices_indices")}; + var index_from_indices = ${g.getByIndices("indices_indices")}; if (index_from_indices < 0) { index_from_indices += ${n[l]}; } ${m.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")}; for (var i = uniforms.gather_axis + 1; i < ${c.length}; i++) { - let index = ${y.indicesGet("output_indices",`i + ${o.length} - 1`)}; + let index = ${y.indicesGet("output_indices",`i + ${i.length} - 1`)}; ${m.indicesSet("data_indices","i","index")}; } let data_offset = ${m.indicesToOffset("data_indices")}; @@ -3340,27 +3340,27 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { // Convert 4-bit packed data to 8-bit packed data. let packed_4bit_quantized_data = ${m.getByOffset("data_offset / 8")}; let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; - let quantized_data_vec = ${i?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); + let quantized_data_vec = ${o?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); let quantized_data = quantized_data_vec[data_index / 2]; var scale_indices = data_indices; - let quantize_axis_index = ${$.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; - ${$.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; - var scale = ${$.getByIndices("scale_indices")}; - ${_?` + let quantize_axis_index = ${_.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; + ${_.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; + var scale = ${_.getByIndices("scale_indices")}; + ${w?` let zero_point_indices = scale_indices; - let zero_point_offset = ${_.indicesToOffset("zero_point_indices")}; + let zero_point_offset = ${w.indicesToOffset("zero_point_indices")}; let zero_point_index = zero_point_offset % 8; - let packed_4bit_zero_points = ${_.getByOffset("zero_point_offset / 8")}; + let packed_4bit_zero_points = ${w.getByOffset("zero_point_offset / 8")}; let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; - let zero_point_vec = ${i?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); + let zero_point_vec = ${o?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"}; - let dequantized_data = ${Ye(r)}(quantized_data - zero_point) * scale; + let dequantized_data = ${rt(r)}(quantized_data - zero_point) * scale; ${y.setByOffset("global_idx","dequantized_data")}; - }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((h,m)=>m!==1).map(h=>h.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(h,m)=>"rank")},getRunData:()=>({outputs:[{dims:c,dataType:r}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:a}),getShaderSource:p}},B1=(e,t)=>{let n=e.inputs;Zg(n,t),e.compute(Yg(e.inputs,t))},F1=e=>Se({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),Jg,Qg,L1,j1,gA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Jg=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and - indices input tensors be of same rank.`)},Qg=(e,t)=>{let n=e[0].dims,o=e[0].dataType,u=n.length,l=e[1].dims,d=e[1].dataType,c=z.normalizeAxis(t.axis,u),s=n[c],r=l.slice(0),i=z.size(r),a=F("input",o,u),p=F("indicesInput",d,l.length),h=re("output",o,r.length),m=[{type:12,data:i},{type:6,data:s},{type:12,data:c}];return m.push(...se(n,l,r)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:m}),getShaderSource:b=>` - ${b.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(a,p,h)} - ${b.mainStart()} - ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((h,m)=>m!==1).map(h=>h.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(h,m)=>"rank")},getRunData:()=>({outputs:[{dims:c,dataType:r}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:a}),getShaderSource:p}},_2=(e,t)=>{let n=e.inputs;Cb(n,t),e.compute(Rb(e.inputs,t))},v2=e=>ke({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),Db,Mb,x2,$2,vk=D(()=>{"use strict";de(),pe(),He(),he(),Db=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and + indices input tensors be of same rank.`)},Mb=(e,t)=>{let n=e[0].dims,i=e[0].dataType,u=n.length,l=e[1].dims,d=e[1].dataType,c=F.normalizeAxis(t.axis,u),s=n[c],r=l.slice(0),o=F.size(r),a=j("input",i,u),p=j("indicesInput",d,l.length),h=oe("output",i,r.length),m=[{type:12,data:o},{type:6,data:s},{type:12,data:c}];return m.push(...ue(n,l,r)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:m}),getShaderSource:g=>` + ${g.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(a,p,h)} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let outputIndices = ${h.offsetToIndices("global_idx")}; @@ -3373,11 +3373,11 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { let value = ${a.getByIndices("inputIndices")}; ${h.setByOffset("global_idx","value")}; - }`}},L1=e=>Se({axis:e.axis}),j1=(e,t)=>{let n=e.inputs;Jg(n),e.compute(Qg(e.inputs,t))}}),eb,tb,U1,V1,bA=D(()=>{"use strict";ae(),de(),ce(),eb=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},tb=(e,t)=>{let n=e[0].dims.slice(),o=e[1].dims.slice(),[u,l,d]=Mv.getShapeOfGemmResult(n,t.transA,o,t.transB,e.length===3?e[2].dims:void 0),c=[u,l];if(!c)throw new Error("Can't use gemm on the given tensors");let s=16,r=Math.ceil(l/s),i=Math.ceil(u/s),a=!0,p=z.size(c),h=[{type:12,data:a?r:p},{type:12,data:u},{type:12,data:l},{type:12,data:d},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(h.push(...se(e[2].dims)),m.push("rank")),h.push(...se(c));let b=_=>{let y="";t.transA&&t.transB?y="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?y="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?y="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(y="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let T=t.alpha===1?"":"value *= uniforms.alpha;",S=F("a",e[0].dataType,e[0].dims),O=F("b",e[1].dataType,e[1].dims),A=S.type.value,P=null,I=[S,O];e.length===3&&(P=F("c",e[2].dataType,e[2].dims.length),I.push(P));let L=re("output",e[0].dataType,c.length);I.push(L);let V=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` - ${_.registerUniforms(V).declareVariables(...I)} + }`}},x2=e=>ke({axis:e.axis}),$2=(e,t)=>{let n=e.inputs;Db(n),e.compute(Mb(e.inputs,t))}}),Nb,zb,T2,S2,xk=D(()=>{"use strict";de(),pe(),he(),Nb=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},zb=(e,t)=>{let n=e[0].dims.slice(),i=e[1].dims.slice(),[u,l,d]=wx.getShapeOfGemmResult(n,t.transA,i,t.transB,e.length===3?e[2].dims:void 0),c=[u,l];if(!c)throw new Error("Can't use gemm on the given tensors");let s=16,r=Math.ceil(l/s),o=Math.ceil(u/s),a=!0,p=F.size(c),h=[{type:12,data:a?r:p},{type:12,data:u},{type:12,data:l},{type:12,data:d},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(h.push(...ue(e[2].dims)),m.push("rank")),h.push(...ue(c));let g=w=>{let y="";t.transA&&t.transB?y="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?y="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?y="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(y="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let T=t.alpha===1?"":"value *= uniforms.alpha;",S=j("a",e[0].dataType,e[0].dims),O=j("b",e[1].dataType,e[1].dims),A=S.type.value,P=null,I=[S,O];e.length===3&&(P=j("c",e[2].dataType,e[2].dims.length),I.push(P));let z=oe("output",e[0].dataType,c.length);I.push(z);let G=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` + ${w.registerUniforms(G).declareVariables(...I)} - ${_.mainStart()} - ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let m = global_idx / uniforms.N; let n = global_idx % uniforms.N; @@ -3388,9 +3388,9 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { } ${T} - ${P!=null?`let cOffset = ${P.broadcastedIndicesToOffset("vec2(m, n)",L)}; value += ${A}(uniforms.beta) * ${P.getByOffset("cOffset")};`:""} + ${P!=null?`let cOffset = ${P.broadcastedIndicesToOffset("vec2(m, n)",z)}; value += ${A}(uniforms.beta) * ${P.getByOffset("cOffset")};`:""} output[global_idx] = value; - }`},$=_=>{let y=F("a",e[0].dataType,e[0].dims),T=F("b",e[1].dataType,e[1].dims),S=null,O=[y,T];e.length===3&&(S=F("c",e[2].dataType,e[2].dims.length),O.push(S));let A=re("output",e[0].dataType,c.length);O.push(A);let P=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],I="",L="";t.transA&&t.transB?(L=` + }`},_=w=>{let y=j("a",e[0].dataType,e[0].dims),T=j("b",e[1].dataType,e[1].dims),S=null,O=[y,T];e.length===3&&(S=j("c",e[2].dataType,e[2].dims.length),O.push(S));let A=oe("output",e[0].dataType,c.length);O.push(A);let P=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],I="",z="";t.transA&&t.transB?(z=` var col = tile_row_start + local_id.x; var row = k_start + local_id.y; if (col < uniforms.M && row < uniforms.K) { @@ -3406,7 +3406,7 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { } else { tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,I="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(L=` + `,I="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(z=` var col = tile_row_start + local_id.x; var row = k_start + local_id.y; if (col < uniforms.M && row < uniforms.K) { @@ -3422,7 +3422,7 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { } else { tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,I="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(L=` + `,I="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(z=` var col = k_start + local_id.x; var row = tile_row_start + local_id.y; if (col < uniforms.K && row < uniforms.M) { @@ -3438,7 +3438,7 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { } else { tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,I="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(L=` + `,I="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(z=` var col = k_start + local_id.x; var row = tile_row_start + local_id.y; if (col < uniforms.K && row < uniforms.M) { @@ -3454,18 +3454,18 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { } else { tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,I="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let V=t.alpha===1?"":"value *= uniforms.alpha;";return` - ${_.registerUniforms(P).declareVariables(...O)} + `,I="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let G=t.alpha===1?"":"value *= uniforms.alpha;";return` + ${w.registerUniforms(P).declareVariables(...O)} var tile_a: array, ${s}>; var tile_b: array, ${s}>; - ${_.mainStart([s,s,1])} + ${w.mainStart([s,s,1])} let tile_col_start = (workgroup_index % uniforms.num_tile_n) * ${s}; let tile_row_start = (workgroup_index / uniforms.num_tile_n) * ${s}; let num_tiles = (uniforms.K - 1) / ${s} + 1; var k_start = 0u; var value = ${A.type.value}(0); for (var t: u32 = 0u; t < num_tiles; t++) { - ${L} + ${z} k_start = k_start + ${s}; workgroupBarrier(); @@ -3475,14 +3475,14 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { workgroupBarrier(); } - ${V} + ${G} let m = tile_row_start + local_id.y; let n = tile_col_start + local_id.x; ${S!=null?`let cOffset = ${S.broadcastedIndicesToOffset("vec2(m, n)",A)}; value += ${A.type.value}(uniforms.beta) * ${S.getByOffset("cOffset")};`:""} if (m < uniforms.M && n < uniforms.N) { output[m * uniforms.N + n] = value; } - }`};return a?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:r*i},programUniforms:h}),getShaderSource:$}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:b}},U1=e=>{let t=e.transA,n=e.transB,o=e.alpha,u=e.beta;return{transA:t,transB:n,alpha:o,beta:u,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},V1=(e,t)=>{eb(e.inputs),e.compute(tb(e.inputs,t))}}),Bt,Kt,kr,Pr,rb,nb,ib,ob,sb,ab,ub,lb,q1,G1,yA=D(()=>{"use strict";ae(),de(),Ve(),ce(),[Bt,Kt,kr,Pr]=[0,1,2,3],rb=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},nb=` + }`};return a?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:r*o},programUniforms:h}),getShaderSource:_}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:g}},T2=e=>{let t=e.transA,n=e.transB,i=e.alpha,u=e.beta;return{transA:t,transB:n,alpha:i,beta:u,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},S2=(e,t)=>{Nb(e.inputs),e.compute(zb(e.inputs,t))}}),Xt,or,Gr,Hr,Bb,Fb,Lb,jb,Ub,Vb,qb,Gb,I2,O2,$k=D(()=>{"use strict";de(),pe(),He(),he(),[Xt,or,Gr,Hr]=[0,1,2,3],Bb=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},Fb=` fn gs_get_cubic_coeffs(x: f32) -> vec4 { let cubic_alpha = -0.75f; let x_abs = abs(x); @@ -3493,7 +3493,7 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha); return coeffs; } -`,ib=e=>` +`,Lb=e=>` fn gs_bicubic_interpolate(p: mat4x4<${e}>, x: f32, y: f32) -> ${e} { var v: vec4; var coeffs = gs_get_cubic_coeffs(x); @@ -3504,7 +3504,7 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { let pixel = ${e}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]); return pixel; } -`,ob=e=>` +`,jb=e=>` fn gs_denormalize(n: f32, length: i32) -> f32 { ${e.alignCorners===0?` // alignCorners: false => [-1, 1] to [-0.5, length - 0.5] @@ -3514,7 +3514,7 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { return (n + 1.0) / 2.0 * (f32(length - 1)); `} } -`,sb=e=>` +`,Ub=e=>` ${e.paddingMode==="reflection"?` fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 { var dx = 0.0; @@ -3541,39 +3541,39 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { } return u32(fx); }`:""} -`,ab=(e,t,n)=>` +`,Vb=(e,t,n)=>` fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4) -> ${t} { var pixel = ${t}(0); var indices = vec4(0); - indices[${Bt}] = batch; - indices[${Kt}] = channel;`+(()=>{switch(n.paddingMode){case"zeros":return` + indices[${Xt}] = batch; + indices[${or}] = channel;`+(()=>{switch(n.paddingMode){case"zeros":return` if (r >= 0 && r < H && c >=0 && c < W) { - indices[${kr}] = u32(r); - indices[${Pr}] = u32(c); + indices[${Gr}] = u32(r); + indices[${Hr}] = u32(c); } else { return ${t}(0); } `;case"border":return` - indices[${kr}] = u32(clamp(r, 0, H - 1)); - indices[${Pr}] = u32(clamp(c, 0, W - 1)); + indices[${Gr}] = u32(clamp(r, 0, H - 1)); + indices[${Hr}] = u32(clamp(c, 0, W - 1)); `;case"reflection":return` - indices[${kr}] = gs_reflect(r, border[1], border[3]); - indices[${Pr}] = gs_reflect(c, border[0], border[2]); + indices[${Gr}] = gs_reflect(r, border[1], border[3]); + indices[${Hr}] = gs_reflect(c, border[0], border[2]); `;default:throw new Error(`padding mode ${n.paddingMode} is not supported`)}})()+` return ${e.getByIndices("indices")}; } -`,ub=(e,t,n)=>(()=>{switch(n.mode){case"nearest":return` - let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${Bt}], indices[${Kt}], border); +`,qb=(e,t,n)=>(()=>{switch(n.mode){case"nearest":return` + let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${Xt}], indices[${or}], border); `;case"bilinear":return` let x1 = i32(floor(x)); let y1 = i32(floor(y)); let x2 = x1 + 1; let y2 = y1 + 1; - let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${Bt}], indices[${Kt}], border); - let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${Bt}], indices[${Kt}], border); - let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${Bt}], indices[${Kt}], border); - let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${Bt}], indices[${Kt}], border); + let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${Xt}], indices[${or}], border); + let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${Xt}], indices[${or}], border); + let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${Xt}], indices[${or}], border); + let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${Xt}], indices[${or}], border); let dx2 = ${t}(f32(x2) - x); let dx1 = ${t}(x - f32(x1)); @@ -3586,25 +3586,25 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { var p: mat4x4<${t}>; for (var h = 0; h < 4; h++) { for (var w = 0; w < 4; w++) { - p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${Bt}], indices[${Kt}], border); + p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${Xt}], indices[${or}], border); } } let dx = x - f32(x0 + 1); let dy = y - f32(y0 + 1); let result = gs_bicubic_interpolate(p, dx, dy); - `;default:throw new Error(`mode ${n.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,lb=(e,t)=>{let n=F("x",e[0].dataType,e[0].dims.length),o=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],u=F("grid",e[1].dataType,o.length,2),l=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(l=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[Bt,Kt,kr,Pr]=[0,3,1,2]);let d=re("output",e[0].dataType,l.length),c=n.type.value,s=z.size(l),r=[{type:12,data:s},...se(e[0].dims,o,l)],i=a=>` + `;default:throw new Error(`mode ${n.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,Gb=(e,t)=>{let n=j("x",e[0].dataType,e[0].dims.length),i=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],u=j("grid",e[1].dataType,i.length,2),l=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(l=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[Xt,or,Gr,Hr]=[0,3,1,2]);let d=oe("output",e[0].dataType,l.length),c=n.type.value,s=F.size(l),r=[{type:12,data:s},...ue(e[0].dims,i,l)],o=a=>` ${a.registerUniform("output_size","u32").declareVariables(n,u,d)} - ${nb} - ${ib(c)} - ${ob(t)} - ${sb(t)} - ${ab(n,c,t)} + ${Fb} + ${Lb(c)} + ${jb(t)} + ${Ub(t)} + ${Vb(n,c,t)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let H_in = i32(uniforms.x_shape[${kr}]); - let W_in = i32(uniforms.x_shape[${Pr}]); + let H_in = i32(uniforms.x_shape[${Gr}]); + let W_in = i32(uniforms.x_shape[${Hr}]); ${t.alignCorners===0?` let x_min = -0.5; @@ -3620,35 +3620,35 @@ ${_} for (var i = local_idx; i < uniforms.outputLength; i += ${Wt}u) { let border = vec4(x_min, y_min, x_max, y_max); let indices = ${d.offsetToIndices("global_idx")}; - var grid_indices = vec3(indices[${Bt}], indices[${kr}], indices[${Pr}]); + var grid_indices = vec3(indices[${Xt}], indices[${Gr}], indices[${Hr}]); let nxy = ${u.getByIndices("grid_indices")}; var x = gs_denormalize(f32(nxy[0]), W_in); var y = gs_denormalize(f32(nxy[1]), H_in); - ${ub(d,c,t)} - }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:a=>{let p=z.size(l);return{outputs:[{dims:l,dataType:a[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:r}},getShaderSource:i}},q1=(e,t)=>{rb(e.inputs),e.compute(lb(e.inputs,t))},G1=e=>Se({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),ot,db,H1,Ha,cb,li,W1,K1=D(()=>{"use strict";ae(),de(),Ve(),Ml(),Ll(),ce(),wr(),ot=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,db=(e,t)=>{let n=e[0],o=ot(e,1),u=ot(e,2),l=ot(e,3),d=ot(e,4),c=ot(e,5),s=ot(e,6),r=ot(e,7);if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let i=n.dims[0],a=n.dims[1],p=n.dims.length===3?n.dims[2]:t.numHeads*n.dims[4],h=a,m=0,b=0,$=Math.floor(p/t.numHeads);if(s&&r&&z.size(s.dims)&&z.size(r.dims)){if(s.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(s.dims[0]!==i||s.dims[1]!==t.numHeads||s.dims[3]!==$)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(r.dims[0]!==i||r.dims[1]!==t.numHeads||r.dims[3]!==$)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(s.dims[2]!==r.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(r.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');m=s.dims[2],b=s.dims[2]}else if(s&&z.size(s.dims)||r&&z.size(r.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let _;if(o&&z.size(o.dims)>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(o.dims.length<3||o.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==o.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(o.dims.length===3){if(o.dims[2]!==n.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');_=2,h=o.dims[1]}else if(o.dims.length===5){if(o.dims[2]!==t.numHeads||o.dims[3]!==2||o.dims[4]!==$)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');_=5,h=o.dims[1]}else{if(o.dims[1]!==t.numHeads||o.dims[3]!==$)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');_=0,h=o.dims[2]}}else{if(n.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(n.dims[2]!==t.numHeads||n.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');_=3}if(l&&z.size(l.dims)>0){if(l.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(o&&o.dims.length===5&&o.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let y=m+h,T=0;if(d&&z.size(d.dims)>0){T=8;let P=d.dims;throw P.length===1?P[0]===i?T=1:P[0]===3*i+2&&(T=3):P.length===2&&P[0]===i&&P[1]===y&&(T=5),T===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let S=!1,O=p;if(u&&z.size(u.dims)>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(h!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');O=u.dims[2]}else{if(h!==u.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');O=u.dims[1]*u.dims[3],S=!0}}let A=!1;if(d&&z.size(d.dims)>0)throw new Error("Key padding mask is not supported");if(c&&z.size(c.dims)>0){if(c.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(c.dims[0]!==i||c.dims[1]!==t.numHeads||c.dims[2]!==a||c.dims[3]!==y)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:i,sequenceLength:a,pastSequenceLength:m,kvSequenceLength:h,totalSequenceLength:y,maxSequenceLength:b,inputHiddenSize:0,hiddenSize:p,vHiddenSize:O,headSize:$,vHeadSize:Math.floor(O/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:T,scale:t.scale,broadcastResPosBias:A,passPastInKv:S,qkvFormat:_}},H1=e=>Se({...e}),Ha=Se({perm:[0,2,1,3]}),cb=(e,t,n,o,u,l,d)=>{let c=[o,u,l],s=z.size(c),r=[{type:12,data:s},{type:12,data:d},{type:12,data:l}],i=a=>{let p=re("qkv_with_bias",t.dataType,c),h=F("qkv",t.dataType,c),m=F("bias",n.dataType,c),b=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` - ${a.registerUniforms(b).declareVariables(h,m,p)} + ${qb(d,c,t)} + }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:a=>{let p=F.size(l);return{outputs:[{dims:l,dataType:a[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:r}},getShaderSource:o}},I2=(e,t)=>{Bb(e.inputs),e.compute(Gb(e.inputs,t))},O2=e=>ke({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),ct,Hb,E2,wu,Wb,Ii,A2,k2=D(()=>{"use strict";de(),pe(),He(),dd(),hd(),he(),Cr(),ct=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,Hb=(e,t)=>{let n=e[0],i=ct(e,1),u=ct(e,2),l=ct(e,3),d=ct(e,4),c=ct(e,5),s=ct(e,6),r=ct(e,7);if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let o=n.dims[0],a=n.dims[1],p=n.dims.length===3?n.dims[2]:t.numHeads*n.dims[4],h=a,m=0,g=0,_=Math.floor(p/t.numHeads);if(s&&r&&F.size(s.dims)&&F.size(r.dims)){if(s.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(s.dims[0]!==o||s.dims[1]!==t.numHeads||s.dims[3]!==_)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(r.dims[0]!==o||r.dims[1]!==t.numHeads||r.dims[3]!==_)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(s.dims[2]!==r.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(r.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');m=s.dims[2],g=s.dims[2]}else if(s&&F.size(s.dims)||r&&F.size(r.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let w;if(i&&F.size(i.dims)>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(i.dims[2]!==n.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');w=2,h=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==_)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');w=5,h=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==_)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');w=0,h=i.dims[2]}}else{if(n.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(n.dims[2]!==t.numHeads||n.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');w=3}if(l&&F.size(l.dims)>0){if(l.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(i&&i.dims.length===5&&i.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let y=m+h,T=0;if(d&&F.size(d.dims)>0){T=8;let P=d.dims;throw P.length===1?P[0]===o?T=1:P[0]===3*o+2&&(T=3):P.length===2&&P[0]===o&&P[1]===y&&(T=5),T===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let S=!1,O=p;if(u&&F.size(u.dims)>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(h!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');O=u.dims[2]}else{if(h!==u.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');O=u.dims[1]*u.dims[3],S=!0}}let A=!1;if(d&&F.size(d.dims)>0)throw new Error("Key padding mask is not supported");if(c&&F.size(c.dims)>0){if(c.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(c.dims[0]!==o||c.dims[1]!==t.numHeads||c.dims[2]!==a||c.dims[3]!==y)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:o,sequenceLength:a,pastSequenceLength:m,kvSequenceLength:h,totalSequenceLength:y,maxSequenceLength:g,inputHiddenSize:0,hiddenSize:p,vHiddenSize:O,headSize:_,vHeadSize:Math.floor(O/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:T,scale:t.scale,broadcastResPosBias:A,passPastInKv:S,qkvFormat:w}},E2=e=>ke({...e}),wu=ke({perm:[0,2,1,3]}),Wb=(e,t,n,i,u,l,d)=>{let c=[i,u,l],s=F.size(c),r=[{type:12,data:s},{type:12,data:d},{type:12,data:l}],o=a=>{let p=oe("qkv_with_bias",t.dataType,c),h=j("qkv",t.dataType,c),m=j("bias",n.dataType,c),g=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` + ${a.registerUniforms(g).declareVariables(h,m,p)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; - }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:c,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r}),getShaderSource:i},{inputs:[t,n],outputs:[-1]})[0]},li=(e,t,n,o,u,l,d,c)=>{let s=l;if(d&&z.size(d.dims)>0){if(o===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return s=cb(e,l,d,t,o,n*u,c),s=s.reshape([t,o,n,u]),n===1||o===1?s:e.compute(gt(s,Ha.perm),{inputs:[s],outputs:[-1]})[0]}else return l.dims.length===3&&(s=l.reshape([t,o,n,u])),n===1||o===1?s:e.compute(gt(s,Ha.perm),{inputs:[s],outputs:[-1]})[0]},W1=(e,t)=>{let n=db(e.inputs,t),o=e.inputs[0],u=ot(e.inputs,1),l=ot(e.inputs,2),d=ot(e.inputs,3),c=ot(e.inputs,4),s=ot(e.inputs,5),r=ot(e.inputs,6),i=ot(e.inputs,7);if(o.dims.length===5)throw new Error("Packed QKV is not implemented");if(u?.dims.length===5)throw new Error("Packed KV is not implemented");let a=u&&l&&u.dims.length===4&&l.dims.length===4,p=li(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,o,d,0);if(a)return yi(e,p,u,l,c,void 0,r,i,s,n);if(!u||!l)throw new Error("key and value must be provided");let h=li(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.headSize,u,d,n.hiddenSize),m=li(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.vHeadSize,l,d,2*n.hiddenSize);yi(e,p,h,m,c,void 0,r,i,s,n)}}),pb,hb,fb,mb,fl,X1,Z1,Y1=D(()=>{"use strict";ae(),de(),Ve(),ce(),pb=e=>{if(!e||e.length<1)throw new Error("too few inputs")},hb=(e,t)=>{let n=[],o=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(u=>n.push(Number(u))),o=n.length),Se({numOutputs:o,axis:t.axis,splitSizes:n})},fb=e=>` + }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:c,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r}),getShaderSource:o},{inputs:[t,n],outputs:[-1]})[0]},Ii=(e,t,n,i,u,l,d,c)=>{let s=l;if(d&&F.size(d.dims)>0){if(i===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return s=Wb(e,l,d,t,i,n*u,c),s=s.reshape([t,i,n,u]),n===1||i===1?s:e.compute($t(s,wu.perm),{inputs:[s],outputs:[-1]})[0]}else return l.dims.length===3&&(s=l.reshape([t,i,n,u])),n===1||i===1?s:e.compute($t(s,wu.perm),{inputs:[s],outputs:[-1]})[0]},A2=(e,t)=>{let n=Hb(e.inputs,t),i=e.inputs[0],u=ct(e.inputs,1),l=ct(e.inputs,2),d=ct(e.inputs,3),c=ct(e.inputs,4),s=ct(e.inputs,5),r=ct(e.inputs,6),o=ct(e.inputs,7);if(i.dims.length===5)throw new Error("Packed QKV is not implemented");if(u?.dims.length===5)throw new Error("Packed KV is not implemented");let a=u&&l&&u.dims.length===4&&l.dims.length===4,p=Ii(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,i,d,0);if(a)return Mi(e,p,u,l,c,void 0,r,o,s,n);if(!u||!l)throw new Error("key and value must be provided");let h=Ii(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.headSize,u,d,n.hiddenSize),m=Ii(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.vHeadSize,l,d,2*n.hiddenSize);Mi(e,p,h,m,c,void 0,r,o,s,n)}}),Kb,Xb,Zb,Yb,jl,P2,C2,R2=D(()=>{"use strict";de(),pe(),He(),he(),Kb=e=>{if(!e||e.length<1)throw new Error("too few inputs")},Xb=(e,t)=>{let n=[],i=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(u=>n.push(Number(u))),i=n.length),ke({numOutputs:i,axis:t.axis,splitSizes:n})},Zb=e=>` fn calculateOutputIndex(index: u32) -> u32 { for (var i: u32 = 0u; i < ${e}u; i += 1u ) { - if (index < ${ie("uniforms.size_in_split_axis","i",e)}) { + if (index < ${se("uniforms.size_in_split_axis","i",e)}) { return i; } } return ${e}u; -}`,mb=e=>{let t=e.length,n=[];for(let o=0;o{let t=e.length,n=[];for(let i=0;i{let n=e[0].dims,o=z.size(n),u=e[0].dataType,l=z.normalizeAxis(t.axis,n.length),d=new Array(t.numOutputs),c=F("input",u,n.length),s=new Array(t.numOutputs),r=[],i=[],a=0,p=[{type:12,data:o}];for(let m=0;m` + }`},jl=(e,t)=>{let n=e[0].dims,i=F.size(n),u=e[0].dataType,l=F.normalizeAxis(t.axis,n.length),d=new Array(t.numOutputs),c=j("input",u,n.length),s=new Array(t.numOutputs),r=[],o=[],a=0,p=[{type:12,data:i}];for(let m=0;m` ${m.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",s.length).declareVariables(c,...d)} - ${fb(s.length)} - ${mb(d)} + ${Zb(s.length)} + ${Yb(d)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} @@ -3657,40 +3657,40 @@ fn calculateOutputIndex(index: u32) -> u32 { var index = ${c.indicesGet("indices",l)}; let output_number = calculateOutputIndex(index); if (output_number != 0) { - index -= ${ie("uniforms.size_in_split_axis","output_number - 1u",s.length)}; + index -= ${se("uniforms.size_in_split_axis","output_number - 1u",s.length)}; ${c.indicesSet("indices",l,"index")}; } writeBufferData(output_number, indices, global_idx); - }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:h,getRunData:()=>({outputs:r,dispatchGroup:{x:Math.ceil(o/64)},programUniforms:p})}},X1=(e,t)=>{pb(e.inputs);let n=e.inputs.length===1?t:hb(e.inputs,t);e.compute(fl(e.inputs,n),{inputs:[0]})},Z1=e=>{let t=e.axis,n=e.splitSizes,o=e.numOutputs<0?n.length:e.numOutputs;if(o!==n.length)throw new Error("numOutputs and splitSizes length must be equal");return Se({axis:t,numOutputs:o,splitSizes:n})}}),gb,ko,J1,Q1=D(()=>{"use strict";ae(),de(),Ve(),ce(),gb=(e,t)=>{let[n,o,u,l]=e,{numHeads:d,rotaryEmbeddingDim:c}=t;if(n.dims.length!==3&&n.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${n.dims.length}`);if(!z.areEqual(o.dims,[])&&!z.areEqual(o.dims,[1])&&o.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${o.dims.length}`);if(u.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${u.dims.length}`);if(l.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${l.dims.length}`);if(!z.areEqual(u.dims,l.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(c>0&&d===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let s=n.dims[0],r=n.dims[n.dims.length-2],i=u.dims[0],a=z.sizeFromDimension(n.dims,1)/r,p=c===0?u.dims[1]*2:a/d;if(c>p)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(o.dims.length===2){if(s!==o.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${o.dims[0]}`);if(r!==o.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${o.dims[1]}`)}if(r>i)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported");if(p/2!==u.dims[1]&&c/2!==u.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${u.dims[1]}`)},ko=(e,t)=>{let{interleaved:n,numHeads:o,rotaryEmbeddingDim:u,scale:l}=t,d=e[0].dims[0],c=z.sizeFromDimension(e[0].dims,1),s=e[0].dims[e[0].dims.length-2],r=c/s,i=e[2].dims[1],a=u===0?i*2:r/o,p=new Array(d,s,r/a,a-i),h=z.computeStrides(p),m=[{type:1,data:l},{type:12,data:p},{type:12,data:h},...e[0].dims.length===3?new Array({type:12,data:[c,r,a,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[c,a,s*a,1]}):[],...se(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],b=$=>{let _=F("input",e[0].dataType,e[0].dims.length),y=F("position_ids",e[1].dataType,e[1].dims.length),T=F("cos_cache",e[2].dataType,e[2].dims.length),S=F("sin_cache",e[3].dataType,e[3].dims.length),O=re("output",e[0].dataType,e[0].dims.length);return $.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:p.length},{name:"global_strides",type:"u32",length:h.length},{name:"input_output_strides",type:"u32",length:h.length}]),` - ${$.declareVariables(_,y,T,S,O)} + }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:h,getRunData:()=>({outputs:r,dispatchGroup:{x:Math.ceil(i/64)},programUniforms:p})}},P2=(e,t)=>{Kb(e.inputs);let n=e.inputs.length===1?t:Xb(e.inputs,t);e.compute(jl(e.inputs,n),{inputs:[0]})},C2=e=>{let t=e.axis,n=e.splitSizes,i=e.numOutputs<0?n.length:e.numOutputs;if(i!==n.length)throw new Error("numOutputs and splitSizes length must be equal");return ke({axis:t,numOutputs:i,splitSizes:n})}}),Jb,Zo,D2,M2=D(()=>{"use strict";de(),pe(),He(),he(),Jb=(e,t)=>{let[n,i,u,l]=e,{numHeads:d,rotaryEmbeddingDim:c}=t;if(n.dims.length!==3&&n.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${n.dims.length}`);if(!F.areEqual(i.dims,[])&&!F.areEqual(i.dims,[1])&&i.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${i.dims.length}`);if(u.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${u.dims.length}`);if(l.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${l.dims.length}`);if(!F.areEqual(u.dims,l.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(c>0&&d===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let s=n.dims[0],r=n.dims[n.dims.length-2],o=u.dims[0],a=F.sizeFromDimension(n.dims,1)/r,p=c===0?u.dims[1]*2:a/d;if(c>p)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(i.dims.length===2){if(s!==i.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${i.dims[0]}`);if(r!==i.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${i.dims[1]}`)}if(r>o)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported");if(p/2!==u.dims[1]&&c/2!==u.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${u.dims[1]}`)},Zo=(e,t)=>{let{interleaved:n,numHeads:i,rotaryEmbeddingDim:u,scale:l}=t,d=e[0].dims[0],c=F.sizeFromDimension(e[0].dims,1),s=e[0].dims[e[0].dims.length-2],r=c/s,o=e[2].dims[1],a=u===0?o*2:r/i,p=new Array(d,s,r/a,a-o),h=F.computeStrides(p),m=[{type:1,data:l},{type:12,data:p},{type:12,data:h},...e[0].dims.length===3?new Array({type:12,data:[c,r,a,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[c,a,s*a,1]}):[],...ue(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],g=_=>{let w=j("input",e[0].dataType,e[0].dims.length),y=j("position_ids",e[1].dataType,e[1].dims.length),T=j("cos_cache",e[2].dataType,e[2].dims.length),S=j("sin_cache",e[3].dataType,e[3].dims.length),O=oe("output",e[0].dataType,e[0].dims.length);return _.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:p.length},{name:"global_strides",type:"u32",length:h.length},{name:"input_output_strides",type:"u32",length:h.length}]),` + ${_.declareVariables(w,y,T,S,O)} - ${$.mainStart($n)} + ${_.mainStart(Fn)} let half_rotary_emb_dim = uniforms.${T.name}_shape[1]; let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; let size = uniforms.global_shape[0] * uniforms.global_strides[0]; - ${$.guardAgainstOutOfBoundsWorkgroupSizes("size")} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("size")} if (bsnh[3] < half_rotary_emb_dim) { let position_ids_idx = - ${y.broadcastedIndicesToOffset("bsnh.xy",re("",y.type.tensor,2))}; + ${y.broadcastedIndicesToOffset("bsnh.xy",oe("",y.type.tensor,2))}; let position_id = u32(${y.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${n}); let j = i + select(half_rotary_emb_dim, 1, ${n}); - let re = ${_.getByOffset("i")} * ${T.get("position_id","bsnh[3]")} - - ${_.getByOffset("j")} * ${S.get("position_id","bsnh[3]")}; + let re = ${w.getByOffset("i")} * ${T.get("position_id","bsnh[3]")} - + ${w.getByOffset("j")} * ${S.get("position_id","bsnh[3]")}; ${O.setByOffset("i","re")} - let im = ${_.getByOffset("i")} * ${S.get("position_id","bsnh[3]")} + - ${_.getByOffset("j")} * ${T.get("position_id","bsnh[3]")}; + let im = ${w.getByOffset("i")} * ${S.get("position_id","bsnh[3]")} + + ${w.getByOffset("j")} * ${T.get("position_id","bsnh[3]")}; ${O.setByOffset("j","im")} } else { let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; - ${O.setByOffset("k",_.getByOffset("k"))} + ${O.setByOffset("k",w.getByOffset("k"))} } - }`};return{name:"RotaryEmbedding",shaderCache:{hint:Se({interleaved:n}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:b,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(z.size(p)/$n)},programUniforms:m})}},J1=(e,t)=>{gb(e.inputs,t),e.compute(ko(e.inputs,t))}}),bb,yb,Wa,_b,e2,_A=D(()=>{"use strict";Ve(),ae(),Ll(),K1(),Y1(),wr(),Q1(),ce(),bb=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let n=e[0],o=e[1],u=e[2],l=e[3],d=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let c=!1,s=n.dims[0],r=n.dims[1],i=n.dims.length===3?c?n.dims[2]/3:n.dims[2]:t.numHeads*n.dims[4],a=r,p=0,h=!o||o.dims.length===0,m=Math.floor(h?i/(t.numHeads+2*t.kvNumHeads):i/t.numHeads);h&&(i=m*t.numHeads);let b=l&&l.dims.length!==0,$=d&&d.dims.length!==0;if(b&&l.dims.length===4&&l.dims[0]===s&&l.dims[1]!==t.kvNumHeads&&l.dims[2]===t.kvNumHeads&&l.dims[3]===m)throw new Error("BSNH pastKey/pastValue is not supported");if(b&&$){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');p=l.dims[2]}else if(b||$)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let _=1;if(o&&o.dims.length>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(o.dims.length<3||o.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==o.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(o.dims.length===3){if(n.dims[2]%o.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');a=o.dims[1]}else if(o.dims.length===5){if(o.dims[2]!==t.numHeads||o.dims[3]!==2||o.dims[4]!==m)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');a=o.dims[1]}else{if(o.dims[1]!==t.numHeads||o.dims[3]!==m)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');a=o.dims[2]}}else{if(n.dims.length!==3&&n.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(n.dims.length===5&&(n.dims[2]!==t.numHeads||n.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');_=3}let y=0,T=!1,S=t.kvNumHeads?m*t.kvNumHeads:i;if(u&&u.dims.length>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(a!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');S=u.dims[2]}else{if(a!==u.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');S=u.dims[1]*u.dims[3],T=!0}}let O=e.length>4?e[5]:void 0;if(O){if(O.dims.length===0)throw new Error("seqlens_k must be at least 1D, got scalar.");let A=O.dims.reduce((P,I)=>P*I,1);if(A!==s)throw new Error(`seqlens_k must have batch_size (${s}) elements, got ${A}.`);for(let P=0;P{let o=t,u=n.kvNumHeads;return t.dims.length===3&&n.kvSequenceLength!==0&&(o=t.reshape([n.batchSize,n.kvSequenceLength,u,n.headSize]),o=e.compute(gt(o,yb.perm),{inputs:[o],outputs:[-1]})[0]),o},_b=(e,t,n,o)=>{let u=7,l=["type","type"],d=[e*t],c=e*t,s=[{type:12,data:c},{type:12,data:t},{type:12,data:e}],r=i=>{let a=F("seq_lens",n.dataType,n.dims),p=F("total_seq_lens",o.dataType,o.dims),h=re("pos_ids",u,d),m=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return` - ${i.registerUniforms(m).declareVariables(a,p,h)} - ${i.mainStart()} - ${i.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + }`};return{name:"RotaryEmbedding",shaderCache:{hint:ke({interleaved:n}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:g,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(F.size(p)/Fn)},programUniforms:m})}},D2=(e,t)=>{Jb(e.inputs,t),e.compute(Zo(e.inputs,t))}}),Qb,ey,_u,ty,N2,Tk=D(()=>{"use strict";He(),de(),hd(),k2(),R2(),Cr(),M2(),he(),Qb=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let n=e[0],i=e[1],u=e[2],l=e[3],d=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let c=!1,s=n.dims[0],r=n.dims[1],o=n.dims.length===3?c?n.dims[2]/3:n.dims[2]:t.numHeads*n.dims[4],a=r,p=0,h=!i||i.dims.length===0,m=Math.floor(h?o/(t.numHeads+2*t.kvNumHeads):o/t.numHeads);h&&(o=m*t.numHeads);let g=l&&l.dims.length!==0,_=d&&d.dims.length!==0;if(g&&l.dims.length===4&&l.dims[0]===s&&l.dims[1]!==t.kvNumHeads&&l.dims[2]===t.kvNumHeads&&l.dims[3]===m)throw new Error("BSNH pastKey/pastValue is not supported");if(g&&_){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');p=l.dims[2]}else if(g||_)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let w=1;if(i&&i.dims.length>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(n.dims[2]%i.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');a=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==m)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');a=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==m)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');a=i.dims[2]}}else{if(n.dims.length!==3&&n.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(n.dims.length===5&&(n.dims[2]!==t.numHeads||n.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');w=3}let y=0,T=!1,S=t.kvNumHeads?m*t.kvNumHeads:o;if(u&&u.dims.length>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(a!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');S=u.dims[2]}else{if(a!==u.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');S=u.dims[1]*u.dims[3],T=!0}}let O=e.length>4?e[5]:void 0;if(O){if(O.dims.length===0)throw new Error("seqlens_k must be at least 1D, got scalar.");let A=O.dims.reduce((P,I)=>P*I,1);if(A!==s)throw new Error(`seqlens_k must have batch_size (${s}) elements, got ${A}.`);for(let P=0;P{let i=t,u=n.kvNumHeads;return t.dims.length===3&&n.kvSequenceLength!==0&&(i=t.reshape([n.batchSize,n.kvSequenceLength,u,n.headSize]),i=e.compute($t(i,ey.perm),{inputs:[i],outputs:[-1]})[0]),i},ty=(e,t,n,i)=>{let u=7,l=["type","type"],d=[e*t],c=e*t,s=[{type:12,data:c},{type:12,data:t},{type:12,data:e}],r=o=>{let a=j("seq_lens",n.dataType,n.dims),p=j("total_seq_lens",i.dataType,i.dims),h=oe("pos_ids",u,d),m=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return` + ${o.registerUniforms(m).declareVariables(a,p,h)} + ${o.mainStart()} + ${o.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let total_sequence_length = u32(${p.getByOffset("0")}); let is_subsequent_prompt = uniforms.sequence_length > 1 && uniforms.sequence_length != total_sequence_length; let is_first_prompt = !is_subsequent_prompt && uniforms.sequence_length == total_sequence_length; @@ -3718,8 +3718,8 @@ fn calculateOutputIndex(index: u32) -> u32 { ${h.setByOffset("global_idx","seqlen")} }; } - `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:s}),getShaderSource:r}},e2=(e,t)=>{if(e.inputs.length>14&&e.inputs[14]||e.inputs.length>15&&e.inputs[15])throw new Error("GroupQueryAttention (JSEP): q_norm_weight / k_norm_weight inputs are not supported. The per-head Q/K RMS normalization prologue is implemented only on the CUDA and native WebGPU EPs.");let n=bb(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let o=e.inputs[0],u=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,l=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,d=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,c=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,s=e.inputs.length>4?e.inputs[5]:void 0,r=e.inputs.length>5?e.inputs[6]:void 0,i=n.kvNumHeads?n.kvNumHeads:n.numHeads,a=Se({axis:2,numOutputs:3,splitSizes:[n.numHeads*n.headSize,i*n.headSize,i*n.headSize]}),[p,h,m]=!u&&!l?e.compute(fl([o],a),{inputs:[o],outputs:[-1,-1,-1]}):[o,u,l],b,$;if(t.doRotary){let S=e.compute(_b(n.batchSize,n.sequenceLength,s,r),{inputs:[s,r],outputs:[-1]})[0],O=e.inputs[7],A=e.inputs[8],P=Se({interleaved:t.rotaryInterleaved!==0,numHeads:n.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),I=[p,S,O,A],L=[-1];b=e.compute(ko(I,P),{inputs:I,outputs:L})[0],I.splice(0,1,h);let V=Se({interleaved:t.rotaryInterleaved!==0,numHeads:n.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});$=e.compute(ko(I,V),{inputs:I,outputs:L})[0]}let _=li(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,t.doRotary?b:p,void 0,0),y=Wa(e,t.doRotary?$:h,n),T=Wa(e,m,n);yi(e,_,y,T,void 0,void 0,d,c,void 0,n,s,r)}}),Ka,wb,vb,t2,wA=D(()=>{"use strict";ae(),de(),wr(),ce(),Ka=(e,t,n,o,u,l,d,c)=>{let s=Ue(l),r=s===1?"f32":`vec${s}f`,i=s===1?"vec2f":`mat2x${s}f`,a=u*d,p=64;a===1&&(p=256);let h=[u,d,l/s],m=[u,d,2],b=["rank","type","type"],$=[];$.push(...se(h,m));let _=y=>{let T=F("x",t.dataType,3,s),S=F("scale",n.dataType,n.dims),O=F("bias",o.dataType,o.dims),A=re("output",1,3,2),P=[T,S,O,A];return` - var workgroup_shared : array<${i}, ${p}>; + `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:s}),getShaderSource:r}},N2=(e,t)=>{if(e.inputs.length>14&&e.inputs[14]||e.inputs.length>15&&e.inputs[15])throw new Error("GroupQueryAttention (JSEP): q_norm_weight / k_norm_weight inputs are not supported. The per-head Q/K RMS normalization prologue is implemented only on the CUDA and native WebGPU EPs.");let n=Qb(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let i=e.inputs[0],u=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,l=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,d=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,c=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,s=e.inputs.length>4?e.inputs[5]:void 0,r=e.inputs.length>5?e.inputs[6]:void 0,o=n.kvNumHeads?n.kvNumHeads:n.numHeads,a=ke({axis:2,numOutputs:3,splitSizes:[n.numHeads*n.headSize,o*n.headSize,o*n.headSize]}),[p,h,m]=!u&&!l?e.compute(jl([i],a),{inputs:[i],outputs:[-1,-1,-1]}):[i,u,l],g,_;if(t.doRotary){let S=e.compute(ty(n.batchSize,n.sequenceLength,s,r),{inputs:[s,r],outputs:[-1]})[0],O=e.inputs[7],A=e.inputs[8],P=ke({interleaved:t.rotaryInterleaved!==0,numHeads:n.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),I=[p,S,O,A],z=[-1];g=e.compute(Zo(I,P),{inputs:I,outputs:z})[0],I.splice(0,1,h);let G=ke({interleaved:t.rotaryInterleaved!==0,numHeads:n.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});_=e.compute(Zo(I,G),{inputs:I,outputs:z})[0]}let w=Ii(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,t.doRotary?g:p,void 0,0),y=_u(e,t.doRotary?_:h,n),T=_u(e,m,n);Mi(e,w,y,T,void 0,void 0,d,c,void 0,n,s,r)}}),vu,ry,ny,z2,Sk=D(()=>{"use strict";de(),pe(),Cr(),he(),vu=(e,t,n,i,u,l,d,c)=>{let s=Ge(l),r=s===1?"f32":`vec${s}f`,o=s===1?"vec2f":`mat2x${s}f`,a=u*d,p=64;a===1&&(p=256);let h=[u,d,l/s],m=[u,d,2],g=["rank","type","type"],_=[];_.push(...ue(h,m));let w=y=>{let T=j("x",t.dataType,3,s),S=j("scale",n.dataType,n.dims),O=j("bias",i.dataType,i.dims),A=oe("output",1,3,2),P=[T,S,O,A];return` + var workgroup_shared : array<${o}, ${p}>; const workgroup_size = ${p}u; ${y.declareVariables(...P)} ${y.mainStart(p)} @@ -3734,7 +3734,7 @@ fn calculateOutputIndex(index: u32) -> u32 { sum += value; squared_sum += value * value; } - workgroup_shared[local_idx] = ${i}(sum, squared_sum); + workgroup_shared[local_idx] = ${o}(sum, squared_sum); workgroupBarrier(); for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { @@ -3744,91 +3744,91 @@ fn calculateOutputIndex(index: u32) -> u32 { workgroupBarrier(); } if (local_idx == 0) { - let sum_final = ${yr("workgroup_shared[0][0]",s)} / f32(hight * ${s}); - let squared_sum_final = ${yr("workgroup_shared[0][1]",s)} / f32(hight * ${s}); + let sum_final = ${kr("workgroup_shared[0][0]",s)} / f32(hight * ${s}); + let squared_sum_final = ${kr("workgroup_shared[0][1]",s)} / f32(hight * ${s}); let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${c})); let channel_scale = inv_std_dev * f32(scale[channel]); let channel_shift = f32(bias[channel]) - sum_final * channel_scale; output[workgroup_index] = vec2f(channel_scale, channel_shift); } - }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${s};${c};${p}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:m,dataType:1}],dispatchGroup:{x:a},programUniforms:$}),getShaderSource:_},{inputs:[t,n,o],outputs:[-1]})[0]},wb=(e,t,n)=>{let o=t[0].dims,u=o,l=2,d=o[0],c=o[1],s=z.sizeFromDimension(o,l),r=Ue(s),i=z.size(u)/r,a=Ka(e,t[0],t[1],t[2],d,s,c,n.epsilon),p=[d,c,s/r],h=[d,c],m=["type","none"],b=$=>{let _=F("x",t[0].dataType,p.length,r),y=F("scale_shift",1,h.length,2),T=re("output",t[0].dataType,p.length,r),S=[_,y,T];return` - ${$.registerUniform("output_size","u32").declareVariables(...S)} - ${$.mainStart()} - ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${s};${c};${p}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:m,dataType:1}],dispatchGroup:{x:a},programUniforms:_}),getShaderSource:w},{inputs:[t,n,i],outputs:[-1]})[0]},ry=(e,t,n)=>{let i=t[0].dims,u=i,l=2,d=i[0],c=i[1],s=F.sizeFromDimension(i,l),r=Ge(s),o=F.size(u)/r,a=vu(e,t[0],t[1],t[2],d,s,c,n.epsilon),p=[d,c,s/r],h=[d,c],m=["type","none"],g=_=>{let w=j("x",t[0].dataType,p.length,r),y=j("scale_shift",1,h.length,2),T=oe("output",t[0].dataType,p.length,r),S=[w,y,T];return` + ${_.registerUniform("output_size","u32").declareVariables(...S)} + ${_.mainStart()} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let outputIndices = ${T.offsetToIndices("global_idx")}; let batch = outputIndices[0]; let channel = outputIndices[1]; let scale_shift = ${y.getByIndices("vec2(batch, channel)")}; - let value = ${_.getByOffset("global_idx")} * ${T.type.value}(scale_shift.x) + ${T.type.value}(scale_shift.y); + let value = ${w.getByOffset("global_idx")} * ${T.type.value}(scale_shift.x) + ${T.type.value}(scale_shift.y); ${T.setByOffset("global_idx","value")}; - }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${r}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...se(p,h,p)]}),getShaderSource:b},{inputs:[t[0],a]})},vb=(e,t,n)=>{let o=t[0].dims,u=o,l=o[0],d=o[o.length-1],c=z.sizeFromDimension(o,1)/d,s=Ue(d),r=z.size(u)/s,i=[{type:12,data:c},{type:12,data:Math.floor(d/s)}],a=["type","type"],p=!1,h=[0,o.length-1];for(let _=0;_o[h[y]])),b=Ka(e,m,t[1],t[2],l,c,d,n.epsilon),$=_=>{let y=Je(t[0].dataType),T=s===1?"vec2f":`mat${s}x2f`,S=P=>{let I=P===0?"x":"y",L=s===1?"f32":`vec${s}f`;switch(s){case 1:return`${y}(${L}(scale.${I}))`;case 2:return`vec2<${y}>(${L}(scale[0].${I}, scale[1].${I}))`;case 4:return`vec4<${y}>(${L}(scale[0].${I}, scale[1].${I}, scale[2].${I}, scale[3].${I}))`;default:throw new Error(`Not supported compoents ${s}`)}},O=F("input",t[0].dataType,t[0].dims,s),A=re("output",t[0].dataType,u,s);return` + }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${r}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},...ue(p,h,p)]}),getShaderSource:g},{inputs:[t[0],a]})},ny=(e,t,n)=>{let i=t[0].dims,u=i,l=i[0],d=i[i.length-1],c=F.sizeFromDimension(i,1)/d,s=Ge(d),r=F.size(u)/s,o=[{type:12,data:c},{type:12,data:Math.floor(d/s)}],a=["type","type"],p=!1,h=[0,i.length-1];for(let w=0;wi[h[y]])),g=vu(e,m,t[1],t[2],l,c,d,n.epsilon),_=w=>{let y=nt(t[0].dataType),T=s===1?"vec2f":`mat${s}x2f`,S=P=>{let I=P===0?"x":"y",z=s===1?"f32":`vec${s}f`;switch(s){case 1:return`${y}(${z}(scale.${I}))`;case 2:return`vec2<${y}>(${z}(scale[0].${I}, scale[1].${I}))`;case 4:return`vec4<${y}>(${z}(scale[0].${I}, scale[1].${I}, scale[2].${I}, scale[3].${I}))`;default:throw new Error(`Not supported compoents ${s}`)}},O=j("input",t[0].dataType,t[0].dims,s),A=oe("output",t[0].dataType,u,s);return` @group(0) @binding(0) var input : array<${O.type.storage}>; @group(0) @binding(1) var scale_input : array<${T}>; @group(0) @binding(2) var output : array<${A.type.storage}>; struct Uniforms {H: u32, C : u32}; @group(0) @binding(3) var uniforms: Uniforms; - ${_.mainStart()} + ${w.mainStart()} let current_image_number = global_idx / (uniforms.C * uniforms.H); let current_channel_number = global_idx % uniforms.C; let scale_offset = current_image_number * uniforms.C + current_channel_number; let scale = scale_input[scale_offset]; output[global_idx] = fma(input[global_idx], ${S(0)}, ${S(1)}); - }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${s}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:i}),getShaderSource:$},{inputs:[t[0],b]})},t2=(e,t)=>{t.format==="NHWC"?vb(e,e.inputs,t):wb(e,e.inputs,t)}}),xb,$b,r2,vA=D(()=>{"use strict";ae(),de(),ce(),xb=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},$b=(e,t,n)=>{let o=t.simplified,u=e[0].dims,l=e[1],d=!o&&e[2],c=u,s=z.normalizeAxis(t.axis,u.length),r=z.sizeToDimension(u,s),i=z.sizeFromDimension(u,s),a=z.size(l.dims),p=d?z.size(d.dims):0;if(a!==i||d&&p!==i)throw new Error(`Size of X.shape()[axis:] == ${i}. + }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${s}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:o}),getShaderSource:_},{inputs:[t[0],g]})},z2=(e,t)=>{t.format==="NHWC"?ny(e,e.inputs,t):ry(e,e.inputs,t)}}),iy,oy,B2,Ik=D(()=>{"use strict";de(),pe(),he(),iy=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},oy=(e,t,n)=>{let i=t.simplified,u=e[0].dims,l=e[1],d=!i&&e[2],c=u,s=F.normalizeAxis(t.axis,u.length),r=F.sizeToDimension(u,s),o=F.sizeFromDimension(u,s),a=F.size(l.dims),p=d?F.size(d.dims):0;if(a!==o||d&&p!==o)throw new Error(`Size of X.shape()[axis:] == ${o}. Size of scale and bias (if provided) must match this. - Got scale size of ${a} and bias size of ${p}`);let h=[];for(let O=0;O1,y=n>2,T=O=>{let A=Je(e[0].dataType),P=[F("x",e[0].dataType,e[0].dims,m),F("scale",l.dataType,l.dims,m)];d&&P.push(F("bias",d.dataType,d.dims,m)),P.push(re("output",e[0].dataType,c,m)),_&&P.push(re("mean_data_output",1,h)),y&&P.push(re("inv_std_output",1,h));let I=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` + Got scale size of ${a} and bias size of ${p}`);let h=[];for(let O=0;O1,y=n>2,T=O=>{let A=nt(e[0].dataType),P=[j("x",e[0].dataType,e[0].dims,m),j("scale",l.dataType,l.dims,m)];d&&P.push(j("bias",d.dataType,d.dims,m)),P.push(oe("output",e[0].dataType,c,m)),w&&P.push(oe("mean_data_output",1,h)),y&&P.push(oe("inv_std_output",1,h));let I=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` ${O.registerUniforms(I).declareVariables(...P)} ${O.mainStart()} ${O.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} let offset = global_idx * uniforms.norm_size_vectorized; - var mean_vector = ${ol("f32",m)}; - var mean_square_vector = ${ol("f32",m)}; + var mean_vector = ${Cl("f32",m)}; + var mean_square_vector = ${Cl("f32",m)}; for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { - let value = ${_n(A,m,"x[h + offset]")}; + let value = ${Mn(A,m,"x[h + offset]")}; mean_vector += value; mean_square_vector += value * value; } - let mean = ${yr("mean_vector",m)} / uniforms.norm_size; - let inv_std_dev = inverseSqrt(${yr("mean_square_vector",m)} / uniforms.norm_size ${o?"":"- mean * mean"} + uniforms.epsilon); + let mean = ${kr("mean_vector",m)} / uniforms.norm_size; + let inv_std_dev = inverseSqrt(${kr("mean_square_vector",m)} / uniforms.norm_size ${i?"":"- mean * mean"} + uniforms.epsilon); for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { - let f32input = ${_n(A,m,"x[j + offset]")}; - let f32scale = ${_n(A,m,"scale[j]")}; - output[j + offset] = ${P[0].type.value}((f32input ${o?"":"- mean"}) * inv_std_dev * f32scale - ${d?`+ ${_n(A,m,"bias[j]")}`:""} + let f32input = ${Mn(A,m,"x[j + offset]")}; + let f32scale = ${Mn(A,m,"scale[j]")}; + output[j + offset] = ${P[0].type.value}((f32input ${i?"":"- mean"}) * inv_std_dev * f32scale + ${d?`+ ${Mn(A,m,"bias[j]")}`:""} ); } - ${_?"mean_data_output[global_idx] = mean":""}; + ${w?"mean_data_output[global_idx] = mean":""}; ${y?"inv_std_output[global_idx] = inv_std_dev":""}; - }`},S=[{dims:c,dataType:e[0].dataType}];return _&&S.push({dims:h,dataType:1}),y&&S.push({dims:h,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${m};${n};${o}`,inputDependencies:b},getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(r/64)},programUniforms:$}),getShaderSource:T}},r2=(e,t)=>{xb(e.inputs),e.compute($b(e.inputs,t,e.outputCount))}}),Tb,n2,xA=D(()=>{"use strict";de(),Gl(),Hl(),Tb=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},n2=e=>{Tb(e.inputs);let t=xn.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let n=t[t.length-1],o=e.inputs[0].dims[e.inputs[0].dims.length-1];if(n<8&&o<8)e.compute(ql(e.inputs,{activation:""},t));else{let u=t[t.length-2],l=z.size(e.inputs[0].dims.slice(0,-2)),d=z.size(e.inputs[1].dims.slice(0,-2));if(l!==1&&u===1&&d===1){let c=e.inputs[0].reshape([1,l,o]),s=e.inputs[1].reshape([1,o,n]),r=[1,l,n],i=[c,s];e.compute(Ao(i,{activation:""},t,r),{inputs:i})}else e.compute(Ao(e.inputs,{activation:""},t))}}}),Sb,Ib,Ob,i2,o2,$A=D(()=>{"use strict";ae(),de(),Ve(),ce(),Sb=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let n=e[0],o=n.dims.length;if(n.dims[o-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let u=Math.floor((t.k+t.blockSize-1)/t.blockSize),l=t.blockSize/8*t.bits,d=e[1];if(!z.areEqual(d.dims,[t.n,u,l]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let c=e[2].dims;if(z.size(c)!==t.n*u)throw new Error("scales input size error.");if(e.length===4){let s=e[3].dims,r=t.n*(t.bits===8?u:Math.floor((u*t.bits+7)/8));if(z.size(s)!==r)throw new Error("zeroPoints input size error.")}},Ib=(e,t)=>{let n=e[0].dims,o=n.length,u=n[o-2],l=t.k,d=t.n,c=n.slice(0,o-2),s=z.size(c),r=e[1].dims[2]/4,i=e[0].dataType,a=Ue(t.k),p=Ue(r),h=Ue(d),m=c.concat([u,d]),b=u>1&&d/h%2===0?2:1,$=z.size(m)/h/b,_=64,y=[],T=[s,u,l/a],S=z.convertShape(e[1].dims).slice();S.splice(-1,1,r/p),y.push(...se(T)),y.push(...se(S)),y.push(...se(e[2].dims)),e.length===4&&y.push(...se(z.convertShape(e[3].dims)));let O=[s,u,d/h];y.push(...se(O));let A=P=>{let I=T.length,L=F("a",e[0].dataType,I,a),V=F("b",12,S.length,p),Z=F("scales",e[2].dataType,e[2].dims.length),q=[L,V,Z],N=e.length===4?F("zero_points",12,e[3].dims.length):void 0;N&&q.push(N);let ue=O.length,w=re("output",e[0].dataType,ue,h),C=Je(e[0].dataType),U=(()=>{switch(a){case 1:return`array<${C}, 8>`;case 2:return`mat4x2<${C}>`;case 4:return`mat2x4<${C}>`;default:throw new Error(`${a}-component is not supported.`)}})(),H=Math.floor(32/t.bits),G=Math.floor(H/8),J=()=>{let K="";for(let M=0;M0?M:""} = ${M===0?L.indicesToOffset(`${L.type.indices}(batch, row, word_offset)`):"input_offset"}; - var a_data${M>0?M:""}: ${U}; - for (var j${M>0?M:""}: u32 = 0; j${M>0?M:""} < ${8/a}; j${M>0?M:""}++) { - a_data${M>0?M:""}[j${M>0?M:""}] = ${L.getByOffset(`input_offset${M>0?M:""}`)}; - input_offset${M>0?M:""}++; + }`},S=[{dims:c,dataType:e[0].dataType}];return w&&S.push({dims:h,dataType:1}),y&&S.push({dims:h,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${m};${n};${i}`,inputDependencies:g},getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(r/64)},programUniforms:_}),getShaderSource:T}},B2=(e,t)=>{iy(e.inputs),e.compute(oy(e.inputs,t,e.outputCount))}}),sy,F2,Ok=D(()=>{"use strict";pe(),yd(),wd(),sy=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},F2=e=>{sy(e.inputs);let t=Bn.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let n=t[t.length-1],i=e.inputs[0].dims[e.inputs[0].dims.length-1];if(n<8&&i<8)e.compute(bd(e.inputs,{activation:""},t));else{let u=t[t.length-2],l=F.size(e.inputs[0].dims.slice(0,-2)),d=F.size(e.inputs[1].dims.slice(0,-2));if(l!==1&&u===1&&d===1){let c=e.inputs[0].reshape([1,l,i]),s=e.inputs[1].reshape([1,i,n]),r=[1,l,n],o=[c,s];e.compute(Xo(o,{activation:""},t,r),{inputs:o})}else e.compute(Xo(e.inputs,{activation:""},t))}}}),ay,uy,ly,L2,j2,Ek=D(()=>{"use strict";de(),pe(),He(),he(),ay=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let n=e[0],i=n.dims.length;if(n.dims[i-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let u=Math.floor((t.k+t.blockSize-1)/t.blockSize),l=t.blockSize/8*t.bits,d=e[1];if(!F.areEqual(d.dims,[t.n,u,l]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let c=e[2].dims;if(F.size(c)!==t.n*u)throw new Error("scales input size error.");if(e.length===4){let s=e[3].dims,r=t.n*(t.bits===8?u:Math.floor((u*t.bits+7)/8));if(F.size(s)!==r)throw new Error("zeroPoints input size error.")}},uy=(e,t)=>{let n=e[0].dims,i=n.length,u=n[i-2],l=t.k,d=t.n,c=n.slice(0,i-2),s=F.size(c),r=e[1].dims[2]/4,o=e[0].dataType,a=Ge(t.k),p=Ge(r),h=Ge(d),m=c.concat([u,d]),g=u>1&&d/h%2===0?2:1,_=F.size(m)/h/g,w=64,y=[],T=[s,u,l/a],S=F.convertShape(e[1].dims).slice();S.splice(-1,1,r/p),y.push(...ue(T)),y.push(...ue(S)),y.push(...ue(e[2].dims)),e.length===4&&y.push(...ue(F.convertShape(e[3].dims)));let O=[s,u,d/h];y.push(...ue(O));let A=P=>{let I=T.length,z=j("a",e[0].dataType,I,a),G=j("b",12,S.length,p),J=j("scales",e[2].dataType,e[2].dims.length),q=[z,G,J],C=e.length===4?j("zero_points",12,e[3].dims.length):void 0;C&&q.push(C);let ee=O.length,v=oe("output",e[0].dataType,ee,h),R=nt(e[0].dataType),H=(()=>{switch(a){case 1:return`array<${R}, 8>`;case 2:return`mat4x2<${R}>`;case 4:return`mat2x4<${R}>`;default:throw new Error(`${a}-component is not supported.`)}})(),L=Math.floor(32/t.bits),V=Math.floor(L/8),K=()=>{let Y="";for(let N=0;N0?N:""} = ${N===0?z.indicesToOffset(`${z.type.indices}(batch, row, word_offset)`):"input_offset"}; + var a_data${N>0?N:""}: ${H}; + for (var j${N>0?N:""}: u32 = 0; j${N>0?N:""} < ${8/a}; j${N>0?N:""}++) { + a_data${N>0?N:""}[j${N>0?N:""}] = ${z.getByOffset(`input_offset${N>0?N:""}`)}; + input_offset${N>0?N:""}++; } - `;for(let le=0;le> ${M*16}u; + let half_word = b_value >> ${N*16}u; let byte_lo = half_word & 0xFFu; let byte_hi = (half_word >> 8u) & 0xFFu; let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u); b_value_lower = unpack4xU8(spread_word & b_mask); b_value_upper = unpack4xU8((spread_word >> 2u) & b_mask); - }`:`b_value_lower = unpack4xU8((b_value >> ${ne}u) & b_mask); - b_value_upper = unpack4xU8((b_value >> ${Q}u) & b_mask);`} - b_quantized_values = ${U}(${Array.from({length:4},(Ae,Oe)=>`${C}(b_value_lower[${Oe}]), ${C}(b_value_upper[${Oe}])`).join(", ")}); - b_dequantized_values = ${a===1?`${U}(${Array.from({length:8},(Ae,Oe)=>`(b_quantized_values[${Oe}] - ${N?`zero_point${le}`:"zero_point"}) * scale${le}`).join(", ")});`:`(b_quantized_values - ${U}(${Array(8).fill(`${N?`zero_point${le}`:"zero_point"}`).join(",")})) * scale${le};`}; - workgroup_shared[local_id.x * ${b} + ${Math.floor(le/h)}]${h>1?`[${le%h}]`:""} += ${Array.from({length:8/a},(Ae,Oe)=>`${a===1?`a_data${M>0?M:""}[${Oe}] * b_dequantized_values[${Oe}]`:`dot(a_data${M>0?M:""}[${Oe}], b_dequantized_values[${Oe}])`}`).join(" + ")}; - `}return K},B=()=>{let K=` + }`:`b_value_lower = unpack4xU8((b_value >> ${X}u) & b_mask); + b_value_upper = unpack4xU8((b_value >> ${W}u) & b_mask);`} + b_quantized_values = ${H}(${Array.from({length:4},(le,ce)=>`${R}(b_value_lower[${ce}]), ${R}(b_value_upper[${ce}])`).join(", ")}); + b_dequantized_values = ${a===1?`${H}(${Array.from({length:8},(le,ce)=>`(b_quantized_values[${ce}] - ${C?`zero_point${ne}`:"zero_point"}) * scale${ne}`).join(", ")});`:`(b_quantized_values - ${H}(${Array(8).fill(`${C?`zero_point${ne}`:"zero_point"}`).join(",")})) * scale${ne};`}; + workgroup_shared[local_id.x * ${g} + ${Math.floor(ne/h)}]${h>1?`[${ne%h}]`:""} += ${Array.from({length:8/a},(le,ce)=>`${a===1?`a_data${N>0?N:""}[${ce}] * b_dequantized_values[${ce}]`:`dot(a_data${N>0?N:""}[${ce}], b_dequantized_values[${ce}])`}`).join(" + ")}; + `}return Y},B=()=>{let Y=` var col_index = col * ${h}; - ${N?` + ${C?` let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u; let zero_point_bytes_per_col = (nBlocksPerCol + zero_point_values_per_byte - 1u) / zero_point_values_per_byte; var zero_point_byte_count: u32; @@ -3838,69 +3838,69 @@ fn calculateOutputIndex(index: u32) -> u32 { var zero_point_bits_offset: u32; var zero_point_word: u32;`:` // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization. - let zero_point = ${C}(${Math.pow(2,t.bits-1).toFixed(1)});`} - `;for(let M=0;M> 0x2u; zero_point_byte_offset = zero_point_byte_count & 0x3u; zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u); - zero_point_word = ${N.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; - let zero_point${M} = ${C}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:""} - col_index += 1;`;return K},X=()=>{let K=`col_index = col * ${h};`;for(let M=0;M> zero_point_bits_offset; + let zero_point${N} = ${R}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:""} + col_index += 1;`;return Y},te=()=>{let Y=`col_index = col * ${h};`;for(let N=0;N; var b_value_upper: vec4; - var b_quantized_values: ${U}; - var b_dequantized_values: ${U};`,K};return` - var workgroup_shared: array<${w.type.value}, ${b*_}>; - ${P.declareVariables(...q,w)} - ${P.mainStart([_,1,1])} - let output_indices = ${w.offsetToIndices(`(global_idx / ${_}) * ${b}`)}; + var b_quantized_values: ${H}; + var b_dequantized_values: ${H};`,Y};return` + var workgroup_shared: array<${v.type.value}, ${g*w}>; + ${P.declareVariables(...q,v)} + ${P.mainStart([w,1,1])} + let output_indices = ${v.offsetToIndices(`(global_idx / ${w}) * ${g}`)}; let col = output_indices[2]; let row = output_indices[1]; let batch = output_indices[0]; let nBlocksPerCol = uniforms.b_shape[1]; - for (var block = local_id.x; block < nBlocksPerCol; block += ${_}) { + for (var block = local_id.x; block < nBlocksPerCol; block += ${w}) { //process one block var word_offset: u32 = block * ${t.blockSize/a}; ${B()} for (var word: u32 = 0; word < ${r}; word += ${p}) { - ${X()} + ${te()} for (var i: u32 = 0; i < ${p}; i++) { - ${J()} - word_offset += ${H/a}; + ${K()} + word_offset += ${L/a}; } } } workgroupBarrier(); - if (local_id.x < ${b}) { - var output_value: ${w.type.value} = ${w.type.value}(0); + if (local_id.x < ${g}) { + var output_value: ${v.type.value} = ${v.type.value}(0); var workgroup_shared_offset: u32 = local_id.x; - for (var b: u32 = 0u; b < ${_}u; b++) { + for (var b: u32 = 0u; b < ${w}u; b++) { output_value += workgroup_shared[workgroup_shared_offset]; - workgroup_shared_offset += ${b}; + workgroup_shared_offset += ${g}; } - ${w.setByIndices(`${w.type.indices}(batch, row, col + local_id.x)`,"output_value")}; + ${v.setByIndices(`${v.type.indices}(batch, row, col + local_id.x)`,"output_value")}; } - }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${a};${p};${h};${b};${_}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:m,dataType:i}],dispatchGroup:{x:$},programUniforms:y}),getShaderSource:A}},Ob=(e,t)=>{let n=e[0].dims,o=n.length,u=n[o-2],l=t.k,d=t.n,c=n.slice(0,o-2),s=z.size(c),r=e[1].dims[2]/4,i=e[0].dataType,a=Ue(t.k),p=Ue(r),h=c.concat([u,d]),m=128,b=d%8===0?8:d%4===0?4:1,$=m/b,_=Math.floor(32/t.bits),y=$*p*_,T=y/a,S=y/t.blockSize,O=z.size(h)/b,A=[],P=[s,u,l/a],I=z.convertShape(e[1].dims).slice();I.splice(-1,1,r/p),A.push(...se(P)),A.push(...se(I)),A.push(...se(e[2].dims)),e.length===4&&A.push(...se(z.convertShape(e[3].dims)));let L=[s,u,d];A.push(...se(L));let V=Z=>{let q=P.length,N=F("a",e[0].dataType,q,a),ue=F("b",12,I.length,p),w=F("scales",e[2].dataType,e[2].dims.length),C=[N,ue,w],U=e.length===4?F("zero_points",12,e[3].dims.length):void 0;U&&C.push(U);let H=L.length,G=re("output",e[0].dataType,H),J=Je(e[0].dataType),B=()=>{switch(a){case 1:return` - let a_data0 = vec4<${J}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); - let a_data1 = vec4<${J}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` - let a_data0 = vec4<${J}>(sub_a[word_offset], sub_a[word_offset + 1]); - let a_data1 = vec4<${J}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` + }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${a};${p};${h};${g};${w}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:m,dataType:o}],dispatchGroup:{x:_},programUniforms:y}),getShaderSource:A}},ly=(e,t)=>{let n=e[0].dims,i=n.length,u=n[i-2],l=t.k,d=t.n,c=n.slice(0,i-2),s=F.size(c),r=e[1].dims[2]/4,o=e[0].dataType,a=Ge(t.k),p=Ge(r),h=c.concat([u,d]),m=128,g=d%8===0?8:d%4===0?4:1,_=m/g,w=Math.floor(32/t.bits),y=_*p*w,T=y/a,S=y/t.blockSize,O=F.size(h)/g,A=[],P=[s,u,l/a],I=F.convertShape(e[1].dims).slice();I.splice(-1,1,r/p),A.push(...ue(P)),A.push(...ue(I)),A.push(...ue(e[2].dims)),e.length===4&&A.push(...ue(F.convertShape(e[3].dims)));let z=[s,u,d];A.push(...ue(z));let G=J=>{let q=P.length,C=j("a",e[0].dataType,q,a),ee=j("b",12,I.length,p),v=j("scales",e[2].dataType,e[2].dims.length),R=[C,ee,v],H=e.length===4?j("zero_points",12,e[3].dims.length):void 0;H&&R.push(H);let L=z.length,V=oe("output",e[0].dataType,L),K=nt(e[0].dataType),B=()=>{switch(a){case 1:return` + let a_data0 = vec4<${K}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); + let a_data1 = vec4<${K}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` + let a_data0 = vec4<${K}>(sub_a[word_offset], sub_a[word_offset + 1]); + let a_data1 = vec4<${K}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` let a_data0 = sub_a[word_offset]; let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${a}-component is not supported.`)}};return` - var sub_a: array<${N.type.value}, ${T}>; - var inter_results: array, ${b}>; - ${Z.declareVariables(...C,G)} - ${Z.mainStart([$,b,1])} - let output_indices = ${G.offsetToIndices(`workgroup_index * ${b}`)}; + var sub_a: array<${C.type.value}, ${T}>; + var inter_results: array, ${g}>; + ${J.declareVariables(...R,V)} + ${J.mainStart([_,g,1])} + let output_indices = ${V.offsetToIndices(`workgroup_index * ${g}`)}; let col = output_indices[2]; let row = output_indices[1]; let batch = output_indices[0]; @@ -3916,9 +3916,9 @@ fn calculateOutputIndex(index: u32) -> u32 { let a_col = a_col_start + a_offset; if (a_col < uniforms.a_shape[2]) { - sub_a[a_offset] = ${N.getByIndices(`${N.type.indices}(batch, row, a_col)`)}; + sub_a[a_offset] = ${C.getByIndices(`${C.type.indices}(batch, row, a_col)`)}; } else { - sub_a[a_offset] = ${N.type.value}(0); + sub_a[a_offset] = ${C.type.value}(0); } } workgroupBarrier(); @@ -3926,7 +3926,7 @@ fn calculateOutputIndex(index: u32) -> u32 { // each thread process one block let b_row = col + local_id.y; let block = tile * ${S} + local_id.x; - ${U?` + ${H?` let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u; let zero_point_bytes_per_col = (n_blocks_per_col + zero_point_values_per_byte - 1u) / zero_point_values_per_byte; let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block / zero_point_values_per_byte); @@ -3934,119 +3934,119 @@ fn calculateOutputIndex(index: u32) -> u32 { let zero_point_byte_offset = zero_point_byte_count & 0x3u; let zero_point_sub_offset: u32 = block % zero_point_values_per_byte; let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u); - let zero_point_word = ${U.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; - let zero_point = ${J}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:` + let zero_point_word = ${H.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point = ${K}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:` // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization. - let zero_point = ${J}(${Math.pow(2,t.bits-1).toFixed(1)});`} - let scale = ${w.getByOffset("b_row * n_blocks_per_col + block")}; - let b_data = ${ue.getByIndices(`${ue.type.indices}(b_row, block, 0)`)}; + let zero_point = ${K}(${Math.pow(2,t.bits-1).toFixed(1)});`} + let scale = ${v.getByOffset("b_row * n_blocks_per_col + block")}; + let b_data = ${ee.getByIndices(`${ee.type.indices}(b_row, block, 0)`)}; var word_offset = local_id.x * ${t.blockSize/a}; for (var i: u32 = 0; i < ${p}; i++) { let b_value = ${p===1?"b_data":"b_data[i]"}; - ${(()=>{let X=Math.floor(_/8),K="";for(let M=0;M{let te=Math.floor(w/8),Y="";for(let N=0;N> ${M*16}u; + let half_word = b_value >> ${N*16}u; let byte_lo = half_word & 0xFFu; let byte_hi = (half_word >> 8u) & 0xFFu; let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u); let b_value_lower = unpack4xU8(spread_word & 0x03030303u); let b_value_upper = unpack4xU8((spread_word >> 2u) & 0x03030303u);`:` - let b_value_lower = unpack4xU8((b_value >> ${ne}u) & 0x0F0F0F0Fu); - let b_value_upper = unpack4xU8((b_value >> ${Q}u) & 0x0F0F0F0Fu);`} - let b_quantized_values = mat2x4<${J}>(${Array.from({length:4},(le,Ae)=>`${J}(b_value_lower[${Ae}]), ${J}(b_value_upper[${Ae}])`).join(", ")}); - let b_dequantized_values = (b_quantized_values - mat2x4<${J}>(${Array(8).fill("zero_point").join(",")})) * scale; - inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(le,Ae)=>`${`dot(a_data${Ae}, b_dequantized_values[${Ae}])`}`).join(" + ")}; + let b_value_lower = unpack4xU8((b_value >> ${X}u) & 0x0F0F0F0Fu); + let b_value_upper = unpack4xU8((b_value >> ${W}u) & 0x0F0F0F0Fu);`} + let b_quantized_values = mat2x4<${K}>(${Array.from({length:4},(ne,le)=>`${K}(b_value_lower[${le}]), ${K}(b_value_upper[${le}])`).join(", ")}); + let b_dequantized_values = (b_quantized_values - mat2x4<${K}>(${Array(8).fill("zero_point").join(",")})) * scale; + inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(ne,le)=>`${`dot(a_data${le}, b_dequantized_values[${le}])`}`).join(" + ")}; } - word_offset += ${8/a};`}return K})()} + word_offset += ${8/a};`}return Y})()} } workgroupBarrier(); } - if (local_idx < ${b}) { - var output_value: ${G.type.value} = ${G.type.value}(0); - for (var b = 0u; b < ${$}; b++) { + if (local_idx < ${g}) { + var output_value: ${V.type.value} = ${V.type.value}(0); + for (var b = 0u; b < ${_}; b++) { output_value += inter_results[local_idx][b]; } if (col + local_idx < uniforms.output_shape[2]) { - ${G.setByIndices(`${G.type.indices}(batch, row, col + local_idx)`,"output_value")} + ${V.setByIndices(`${V.type.indices}(batch, row, col + local_idx)`,"output_value")} } } - }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${a};${p};${$};${b}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:h,dataType:i}],dispatchGroup:{x:O},programUniforms:A}),getShaderSource:V}},i2=(e,t)=>{Sb(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Ob(e.inputs,t)):e.compute(Ib(e.inputs,t))},o2=e=>Se(e)}),Eb,Ab,kb,Pb,Cb,Db,Rb,Nb,s2,TA=D(()=>{"use strict";ae(),de(),ce(),Eb=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},Ab=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ie("uniforms.pads",u,n)}; + }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${a};${p};${_};${g}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:h,dataType:o}],dispatchGroup:{x:O},programUniforms:A}),getShaderSource:G}},L2=(e,t)=>{ay(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(ly(e.inputs,t)):e.compute(uy(e.inputs,t))},j2=e=>ke(e)}),dy,cy,py,hy,fy,my,gy,by,U2,Ak=D(()=>{"use strict";de(),pe(),he(),dy=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},cy=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${se("uniforms.pads",u,n)}; if (k < 0) { break; } - if (k >= i32(${ie("uniforms.x_shape",u,t)})) { + if (k >= i32(${se("uniforms.x_shape",u,t)})) { break; } - offset += k * i32(${ie("uniforms.x_strides",u,t)}); + offset += k * i32(${se("uniforms.x_strides",u,t)}); `;return` value = ${e.type.value}(uniforms.constant_value); for (var i = 0; i < 1; i++) { var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; } - `},kb=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ie("uniforms.pads",u,n)}; + `},py=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${se("uniforms.pads",u,n)}; if (k < 0) { k = -k; } { - let _2n_1 = 2 * (i32(${ie("uniforms.x_shape",u,t)}) - 1); + let _2n_1 = 2 * (i32(${se("uniforms.x_shape",u,t)}) - 1); k = k % _2n_1; - if(k >= i32(${ie("uniforms.x_shape",u,t)})) { + if(k >= i32(${se("uniforms.x_shape",u,t)})) { k = _2n_1 - k; } } - offset += k * i32(${ie("uniforms.x_strides",u,t)}); + offset += k * i32(${se("uniforms.x_strides",u,t)}); `;return` var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; - `},Pb=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ie("uniforms.pads",u,n)}; + `},hy=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${se("uniforms.pads",u,n)}; if (k < 0) { k = 0; } - if (k >= i32(${ie("uniforms.x_shape",u,t)})) { - k = i32(${ie("uniforms.x_shape",u,t)}) - 1; + if (k >= i32(${se("uniforms.x_shape",u,t)})) { + k = i32(${se("uniforms.x_shape",u,t)}) - 1; } - offset += k * i32(${ie("uniforms.x_strides",u,t)}); + offset += k * i32(${se("uniforms.x_strides",u,t)}); `;return` var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; - `},Cb=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ie("uniforms.pads",u,n)}; + `},fy=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${se("uniforms.pads",u,n)}; if (k < 0) { - k += i32(${ie("uniforms.x_shape",u,t)}]); + k += i32(${se("uniforms.x_shape",u,t)}]); } - if (k >= i32(${ie("uniforms.x_shape",u,t)})) { - k -= i32(${ie("uniforms.x_shape",u,t)}); + if (k >= i32(${se("uniforms.x_shape",u,t)})) { + k -= i32(${se("uniforms.x_shape",u,t)}); } - offset += k * i32(${ie("uniforms.x_strides",u,t)}); + offset += k * i32(${se("uniforms.x_strides",u,t)}); `;return` var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; - `},Db=(e,t,n)=>{switch(n.mode){case 0:return Ab(e,t,n.pads.length);case 1:return kb(e,t,n.pads.length);case 2:return Pb(e,t,n.pads.length);case 3:return Cb(e,t,n.pads.length);default:throw new Error("Invalid mode")}},Rb=(e,t)=>{let n=z.padShape(e[0].dims.slice(),t.pads),o=e[0].dims,u=z.size(n),l=[{type:12,data:u},{type:6,data:t.pads}],d=e.length>=3&&e[2].data;t.mode===0&&l.push({type:d?e[2].dataType:1,data:t.value}),l.push(...se(e[0].dims,n));let c=["rank"],s=r=>{let i=re("output",e[0].dataType,n.length),a=F("x",e[0].dataType,o.length),p=a.type.value,h=Db(i,o.length,t),m=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&m.push({name:"constant_value",type:d?p:"f32"}),` - ${r.registerUniforms(m).declareVariables(a,i)} + `},my=(e,t,n)=>{switch(n.mode){case 0:return cy(e,t,n.pads.length);case 1:return py(e,t,n.pads.length);case 2:return hy(e,t,n.pads.length);case 3:return fy(e,t,n.pads.length);default:throw new Error("Invalid mode")}},gy=(e,t)=>{let n=F.padShape(e[0].dims.slice(),t.pads),i=e[0].dims,u=F.size(n),l=[{type:12,data:u},{type:6,data:t.pads}],d=e.length>=3&&e[2].data;t.mode===0&&l.push({type:d?e[2].dataType:1,data:t.value}),l.push(...ue(e[0].dims,n));let c=["rank"],s=r=>{let o=oe("output",e[0].dataType,n.length),a=j("x",e[0].dataType,i.length),p=a.type.value,h=my(o,i.length,t),m=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&m.push({name:"constant_value",type:d?p:"f32"}),` + ${r.registerUniforms(m).declareVariables(a,o)} ${r.mainStart()} ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let indices = ${i.offsetToIndices("global_idx")}; + let indices = ${o.offsetToIndices("global_idx")}; var value = ${p}(0); ${h} output[global_idx] = value; - }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${d}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(z.size(n)/64)},programUniforms:l}),getShaderSource:s}},Nb=(e,t)=>{if(e.length>1){let n=e[1].getBigInt64Array(),o=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,u=e[0].dims.length,l=new Int32Array(2*u).fill(0);if(e.length>=4){let c=e[3].getBigInt64Array();for(let s=0;sl[Number(s)]=Number(c));let d=[];return l.forEach(c=>d.push(c)),{mode:t.mode,value:o,pads:d}}else return t},s2=(e,t)=>{Eb(e.inputs);let n=Nb(e.inputs,t);e.compute(Rb(e.inputs,n),{inputs:[0]})}}),Jn,Xa,Za,Ya,Ja,zb,Mb,Qa,eu,a2,u2,tu,l2,d2,ru,c2,p2,h2,f2,SA=D(()=>{"use strict";it(),ae(),de(),ce(),Jn=e=>{if(pe.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},Xa=(e,t,n)=>{let o=t.format==="NHWC",u=e.dims.slice();o&&u.splice(1,0,u.pop());let l=Object.hasOwnProperty.call(t,"dilations"),d=t.kernelShape.slice(),c=t.strides.slice(),s=l?t.dilations.slice():[],r=t.pads.slice();Oo.adjustPoolAttributes(n,u,d,c,s,r);let i=Oo.computePoolOutputShape(n,u,c,s,d,r,t.autoPad,t.ceilMode),a=Object.assign({},t);l?Object.assign(a,{kernelShape:d,strides:c,pads:r,dilations:s,cacheKey:t.cacheKey}):Object.assign(a,{kernelShape:d,strides:c,pads:r,cacheKey:t.cacheKey});let p=i.slice();return p.push(p.splice(1,1)[0]),[a,o?p:i]},Za=(e,t)=>{let n=t.format==="NHWC",o=z.size(e),u=z.size(t.kernelShape),l=[{type:12,data:o},{type:12,data:u}],d=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let c=t.kernelShape[t.kernelShape.length-1],s=t.strides[t.strides.length-1],r=t.pads[t.pads.length/2-1],i=t.pads[t.pads.length-1],a=!!(r+i);l.push({type:12,data:c},{type:12,data:s},{type:12,data:r},{type:12,data:i}),d.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let p=!1;if(t.kernelShape.length===2){let h=t.kernelShape[t.kernelShape.length-2],m=t.strides[t.strides.length-2],b=t.pads[t.pads.length/2-2],$=t.pads[t.pads.length-2];p=!!(b+$),l.push({type:12,data:h},{type:12,data:m},{type:12,data:b},{type:12,data:$}),d.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[l,d,!0,a,p]}else{if(n)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let c=z.computeStrides(t.kernelShape);l.push({type:12,data:c},{type:12,data:t.pads},{type:12,data:t.strides}),d.push({name:"kernelStrides",type:"u32",length:c.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let s=t.pads.reduce((r,i)=>r+i);return[l,d,!!s,!1,!1]}},Ya=(e,t,n,o,u,l,d,c,s,r,i,a)=>{let p=u.format==="NHWC",h=t.type.value,m=re("output",t.type.tensor,o);if(u.kernelShape.length<=2){let b="",$="",_="",y=n-(p?2:1);if(i?b=` + }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${d}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(F.size(n)/64)},programUniforms:l}),getShaderSource:s}},by=(e,t)=>{if(e.length>1){let n=e[1].getBigInt64Array(),i=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,u=e[0].dims.length,l=new Int32Array(2*u).fill(0);if(e.length>=4){let c=e[3].getBigInt64Array();for(let s=0;sl[Number(s)]=Number(c));let d=[];return l.forEach(c=>d.push(c)),{mode:t.mode,value:i,pads:d}}else return t},U2=(e,t)=>{dy(e.inputs);let n=by(e.inputs,t);e.compute(gy(e.inputs,n),{inputs:[0]})}}),mi,xu,$u,Tu,Su,yy,wy,Iu,Ou,V2,q2,Eu,G2,H2,Au,W2,K2,X2,Z2,kk=D(()=>{"use strict";dt(),de(),pe(),he(),mi=e=>{if(me.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},xu=(e,t,n)=>{let i=t.format==="NHWC",u=e.dims.slice();i&&u.splice(1,0,u.pop());let l=Object.hasOwnProperty.call(t,"dilations"),d=t.kernelShape.slice(),c=t.strides.slice(),s=l?t.dilations.slice():[],r=t.pads.slice();Wo.adjustPoolAttributes(n,u,d,c,s,r);let o=Wo.computePoolOutputShape(n,u,c,s,d,r,t.autoPad,t.ceilMode),a=Object.assign({},t);l?Object.assign(a,{kernelShape:d,strides:c,pads:r,dilations:s,cacheKey:t.cacheKey}):Object.assign(a,{kernelShape:d,strides:c,pads:r,cacheKey:t.cacheKey});let p=o.slice();return p.push(p.splice(1,1)[0]),[a,i?p:o]},$u=(e,t)=>{let n=t.format==="NHWC",i=F.size(e),u=F.size(t.kernelShape),l=[{type:12,data:i},{type:12,data:u}],d=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let c=t.kernelShape[t.kernelShape.length-1],s=t.strides[t.strides.length-1],r=t.pads[t.pads.length/2-1],o=t.pads[t.pads.length-1],a=!!(r+o);l.push({type:12,data:c},{type:12,data:s},{type:12,data:r},{type:12,data:o}),d.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let p=!1;if(t.kernelShape.length===2){let h=t.kernelShape[t.kernelShape.length-2],m=t.strides[t.strides.length-2],g=t.pads[t.pads.length/2-2],_=t.pads[t.pads.length-2];p=!!(g+_),l.push({type:12,data:h},{type:12,data:m},{type:12,data:g},{type:12,data:_}),d.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[l,d,!0,a,p]}else{if(n)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let c=F.computeStrides(t.kernelShape);l.push({type:12,data:c},{type:12,data:t.pads},{type:12,data:t.strides}),d.push({name:"kernelStrides",type:"u32",length:c.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let s=t.pads.reduce((r,o)=>r+o);return[l,d,!!s,!1,!1]}},Tu=(e,t,n,i,u,l,d,c,s,r,o,a)=>{let p=u.format==="NHWC",h=t.type.value,m=oe("output",t.type.tensor,i);if(u.kernelShape.length<=2){let g="",_="",w="",y=n-(p?2:1);if(o?g=` for (var i: u32 = 0u; i < uniforms.kw; i++) { xIndices[${y}] = indices[${y}] * uniforms.sw - uniforms.pwStart + i; if (xIndices[${y}] < 0 || xIndices[${y}] @@ -4056,22 +4056,22 @@ fn calculateOutputIndex(index: u32) -> u32 { } let x_val = x[${t.indicesToOffset("xIndices")}]; ${l} - }`:b=` + }`:g=` for (var i: u32 = 0u; i < uniforms.kw; i++) { xIndices[${y}] = indices[${y}] * uniforms.sw - uniforms.pwStart + i; let x_val = x[${t.indicesToOffset("xIndices")}]; ${l} - }`,u.kernelShape.length===2){let T=n-(p?3:2);a?$=` + }`,u.kernelShape.length===2){let T=n-(p?3:2);a?_=` for (var j: u32 = 0u; j < uniforms.kh; j++) { xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; if (xIndices[${T}] < 0 || xIndices[${T}] >= uniforms.x_shape[${T}]) { pad += i32(uniforms.kw); continue; } - `:$=` + `:_=` for (var j: u32 = 0u; j < uniforms.kh; j++) { xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; - `,_=` + `,w=` } `}return` ${e.registerUniforms(s).declareVariables(t,m)} @@ -4084,13 +4084,13 @@ fn calculateOutputIndex(index: u32) -> u32 { var value = ${h}(${c}); var pad = 0; - ${$} - ${b} ${_} + ${g} + ${w} ${d} output[global_idx] = value; - }`}else{if(p)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let b=u.kernelShape.length,$=u.pads.length,_="";return r?_=` + }`}else{if(p)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let g=u.kernelShape.length,_=u.pads.length,w="";return r?w=` if (xIndices[j] >= uniforms.x_shape[j]) { pad++; isPad = true; @@ -4100,7 +4100,7 @@ fn calculateOutputIndex(index: u32) -> u32 { if (!isPad) { let x_val = x[${t.indicesToOffset("xIndices")}]; ${l} - }`:_=` + }`:w=` } let x_val = x[${t.indicesToOffset("xIndices")}]; ${l} @@ -4112,7 +4112,7 @@ fn calculateOutputIndex(index: u32) -> u32 { let indices = ${m.offsetToIndices("global_idx")}; var xIndices = ${m.offsetToIndices("global_idx")}; - var offsets: array; + var offsets: array; var value = ${h}(${c}); var pad = 0; @@ -4120,34 +4120,34 @@ fn calculateOutputIndex(index: u32) -> u32 { for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { var offset = i; - for (var j = 0u; j < ${b-1}u; j++) { - offsets[j] = offset / ${ie("uniforms.kernelStrides","j",b)}; - offset -= offsets[j] * ${ie("uniforms.kernelStrides","j",b)}; + for (var j = 0u; j < ${g-1}u; j++) { + offsets[j] = offset / ${se("uniforms.kernelStrides","j",g)}; + offset -= offsets[j] * ${se("uniforms.kernelStrides","j",g)}; } - offsets[${b-1}] = offset; + offsets[${g-1}] = offset; isPad = false; - for (var j = ${n-b}u; j < ${n}u; j++) { - xIndices[j] = indices[j] * ${ie("uniforms.strides",`j - ${n-b}u`,b)} - + offsets[j - ${n-b}u] - ${ie("uniforms.pads","j - 2u",$)}; - ${_} + for (var j = ${n-g}u; j < ${n}u; j++) { + xIndices[j] = indices[j] * ${se("uniforms.strides",`j - ${n-g}u`,g)} + + offsets[j - ${n-g}u] - ${se("uniforms.pads","j - 2u",_)}; + ${w} } ${d} output[global_idx] = value; - }`}},Ja=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,zb=e=>`${Ja(e)};${e.countIncludePad}`,Mb=e=>`${Ja(e)};${e.storageOrder};${e.dilations}`,Qa=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),eu=(e,t,n,o)=>{let[u,l]=Xa(t,o,n),d=F("x",t.dataType,t.dims.length),c=d.type.value,s="value += x_val;",r="";u.countIncludePad?r+=`value /= ${c}(uniforms.kernelSize);`:r+=`value /= ${c}(i32(uniforms.kernelSize) - pad);`;let[i,a,p,h,m]=Za(l,u);i.push(...se(t.dims,l));let b=["rank"];return{name:e,shaderCache:{hint:`${o.cacheKey};${p};${h};${m}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(z.size(l)/64)},programUniforms:i}),getShaderSource:$=>Ya($,d,t.dims.length,l.length,u,s,r,0,a,p,h,m)}},a2=e=>{let t=e.count_include_pad!==0,n=Qa(e);if(n.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding/divisor) is not yet implemented in the WebGPU AveragePool kernel");let o={countIncludePad:t,...n,cacheKey:""};return{...o,cacheKey:zb(o)}},u2=(e,t)=>{Jn(e.inputs),e.compute(eu("AveragePool",e.inputs[0],!1,t))},tu={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},l2=e=>{let t=e.format;return{format:t,...tu,cacheKey:t}},d2=(e,t)=>{Jn(e.inputs),e.compute(eu("GlobalAveragePool",e.inputs[0],!0,t))},ru=(e,t,n,o)=>{let[u,l]=Xa(t,o,n),d=` + }`}},Su=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,yy=e=>`${Su(e)};${e.countIncludePad}`,wy=e=>`${Su(e)};${e.storageOrder};${e.dilations}`,Iu=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Ou=(e,t,n,i)=>{let[u,l]=xu(t,i,n),d=j("x",t.dataType,t.dims.length),c=d.type.value,s="value += x_val;",r="";u.countIncludePad?r+=`value /= ${c}(uniforms.kernelSize);`:r+=`value /= ${c}(i32(uniforms.kernelSize) - pad);`;let[o,a,p,h,m]=$u(l,u);o.push(...ue(t.dims,l));let g=["rank"];return{name:e,shaderCache:{hint:`${i.cacheKey};${p};${h};${m}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(F.size(l)/64)},programUniforms:o}),getShaderSource:_=>Tu(_,d,t.dims.length,l.length,u,s,r,0,a,p,h,m)}},V2=e=>{let t=e.count_include_pad!==0,n=Iu(e);if(n.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding/divisor) is not yet implemented in the WebGPU AveragePool kernel");let i={countIncludePad:t,...n,cacheKey:""};return{...i,cacheKey:yy(i)}},q2=(e,t)=>{mi(e.inputs),e.compute(Ou("AveragePool",e.inputs[0],!1,t))},Eu={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},G2=e=>{let t=e.format;return{format:t,...Eu,cacheKey:t}},H2=(e,t)=>{mi(e.inputs),e.compute(Ou("GlobalAveragePool",e.inputs[0],!0,t))},Au=(e,t,n,i)=>{let[u,l]=xu(t,i,n),d=` value = max(x_val, value); - `,c="",s=F("x",t.dataType,t.dims.length),r=["rank"],[i,a,p,h,m]=Za(l,u);return i.push(...se(t.dims,l)),{name:e,shaderCache:{hint:`${o.cacheKey};${p};${h};${m}`,inputDependencies:r},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(z.size(l)/64)},programUniforms:i}),getShaderSource:b=>Ya(b,s,t.dims.length,l.length,u,d,c,t.dataType===10?-65504:-1e5,a,p,h,m)}},c2=(e,t)=>{Jn(e.inputs),e.compute(ru("MaxPool",e.inputs[0],!1,t))},p2=e=>{let t=e.storage_order,n=e.dilations,o=Qa(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(o.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding) is not yet implemented in the WebGPU MaxPool kernel");let u={storageOrder:t,dilations:n,...o,cacheKey:""};return{...u,cacheKey:Mb(u)}},h2=e=>{let t=e.format;return{format:t,...tu,cacheKey:t}},f2=(e,t)=>{Jn(e.inputs),e.compute(ru("GlobalMaxPool",e.inputs[0],!0,t))}}),Bb,Fb,m2,g2,IA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Bb=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((n,o)=>n===e[2].dims[o]).reduce((n,o)=>n&&o,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((u,l)=>l===t.axis||u===e[0].dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let n=e[0].dims[t.axis],o=e[1].dims[t.axis];if(t.blockSizeMath.ceil(n/(o-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},Fb=(e,t)=>{let n=z.normalizeAxis(t.axis,e[0].dims.length),o=e[0].dataType,u=o===3,l=e[0].dims,d=e[1].dataType,c=z.size(l),s=o===3||o===2,r=s?[Math.ceil(z.size(e[0].dims)/4)]:e[0].dims,i=e[1].dims,a=e.length>2?e[2]:void 0,p=a?s?[Math.ceil(z.size(a.dims)/4)]:a.dims:void 0,h=i.length===0||i.length===1&&i[0]===1,m=h===!1&&i.length===1,b=Ue(c),$=h&&(!s||b===4),_=$?b:1,y=$&&!s?b:1,T=F("input",s?12:o,r.length,y),S=F("scale",d,i.length),O=a?F("zero_point",s?12:o,p.length):void 0,A=re("output",d,l.length,_),P=[T,S];O&&P.push(O);let I=[r,i];a&&I.push(p);let L=[{type:12,data:c/_},{type:12,data:n},{type:12,data:t.blockSize},...se(...I,l)],V=Z=>{let q=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` - ${Z.registerUniforms(q).declareVariables(...P,A)} - ${Z.mainStart()} - ${Z.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + `,c="",s=j("x",t.dataType,t.dims.length),r=["rank"],[o,a,p,h,m]=$u(l,u);return o.push(...ue(t.dims,l)),{name:e,shaderCache:{hint:`${i.cacheKey};${p};${h};${m}`,inputDependencies:r},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(F.size(l)/64)},programUniforms:o}),getShaderSource:g=>Tu(g,s,t.dims.length,l.length,u,d,c,t.dataType===10?-65504:-1e5,a,p,h,m)}},W2=(e,t)=>{mi(e.inputs),e.compute(Au("MaxPool",e.inputs[0],!1,t))},K2=e=>{let t=e.storage_order,n=e.dilations,i=Iu(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(i.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding) is not yet implemented in the WebGPU MaxPool kernel");let u={storageOrder:t,dilations:n,...i,cacheKey:""};return{...u,cacheKey:wy(u)}},X2=e=>{let t=e.format;return{format:t,...Eu,cacheKey:t}},Z2=(e,t)=>{mi(e.inputs),e.compute(Au("GlobalMaxPool",e.inputs[0],!0,t))}}),_y,vy,Y2,J2,Pk=D(()=>{"use strict";de(),pe(),He(),he(),_y=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((n,i)=>n===e[2].dims[i]).reduce((n,i)=>n&&i,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((u,l)=>l===t.axis||u===e[0].dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let n=e[0].dims[t.axis],i=e[1].dims[t.axis];if(t.blockSizeMath.ceil(n/(i-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},vy=(e,t)=>{let n=F.normalizeAxis(t.axis,e[0].dims.length),i=e[0].dataType,u=i===3,l=e[0].dims,d=e[1].dataType,c=F.size(l),s=i===3||i===2,r=s?[Math.ceil(F.size(e[0].dims)/4)]:e[0].dims,o=e[1].dims,a=e.length>2?e[2]:void 0,p=a?s?[Math.ceil(F.size(a.dims)/4)]:a.dims:void 0,h=o.length===0||o.length===1&&o[0]===1,m=h===!1&&o.length===1,g=Ge(c),_=h&&(!s||g===4),w=_?g:1,y=_&&!s?g:1,T=j("input",s?12:i,r.length,y),S=j("scale",d,o.length),O=a?j("zero_point",s?12:i,p.length):void 0,A=oe("output",d,l.length,w),P=[T,S];O&&P.push(O);let I=[r,o];a&&I.push(p);let z=[{type:12,data:c/w},{type:12,data:n},{type:12,data:t.blockSize},...ue(...I,l)],G=J=>{let q=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${J.registerUniforms(q).declareVariables(...P,A)} + ${J.mainStart()} + ${J.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let output_indices = ${A.offsetToIndices("global_idx")}; // Set input x ${s?` let input = ${T.getByOffset("global_idx / 4")}; let x_vec = ${u?"unpack4xI8(input)":"unpack4xU8(input)"}; - let x_value = ${_===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${T.getByOffset("global_idx")};`}; + let x_value = ${w===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${T.getByOffset("global_idx")};`}; // Set scale input ${h?`let scale_value= ${S.getByOffset("0")}`:m?` @@ -4175,12 +4175,12 @@ fn calculateOutputIndex(index: u32) -> u32 { let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${O.getByIndices("scale_indices")};`:`let zero_point_value = ${s?u?"i32":"u32":T.type.value}(0);`}; // Compute and write output ${A.setByOffset("global_idx",`${A.type.value}(x_value - zero_point_value) * scale_value`)}; - }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:O?["rank","rank","rank"]:["rank","rank"]},getShaderSource:V,getRunData:()=>({outputs:[{dims:l,dataType:d}],dispatchGroup:{x:Math.ceil(c/_/64),y:1,z:1},programUniforms:L})}},m2=(e,t)=>{Bb(e.inputs,t),e.compute(Fb(e.inputs,t))},g2=e=>Se({axis:e.axis,blockSize:e.blockSize})}),Lb,jb,b2,OA=D(()=>{"use strict";it(),ae(),ce(),Lb=(e,t,n)=>{let o=e===t,u=et&&n>0;if(o||u||l)throw new Error("Range these inputs' contents are invalid.")},jb=(e,t,n,o)=>{let u=Math.abs(Math.ceil((t-e)/n)),l=[u],d=u,c=[{type:12,data:d},{type:o,data:e},{type:o,data:n},...se(l)],s=r=>{let i=re("output",o,l.length),a=i.type.value,p=[{name:"outputSize",type:"u32"},{name:"start",type:a},{name:"delta",type:a}];return` - ${r.registerUniforms(p).declareVariables(i)} + }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:O?["rank","rank","rank"]:["rank","rank"]},getShaderSource:G,getRunData:()=>({outputs:[{dims:l,dataType:d}],dispatchGroup:{x:Math.ceil(c/w/64),y:1,z:1},programUniforms:z})}},Y2=(e,t)=>{_y(e.inputs,t),e.compute(vy(e.inputs,t))},J2=e=>ke({axis:e.axis,blockSize:e.blockSize})}),xy,$y,Q2,Ck=D(()=>{"use strict";dt(),de(),he(),xy=(e,t,n)=>{let i=e===t,u=et&&n>0;if(i||u||l)throw new Error("Range these inputs' contents are invalid.")},$y=(e,t,n,i)=>{let u=Math.abs(Math.ceil((t-e)/n)),l=[u],d=u,c=[{type:12,data:d},{type:i,data:e},{type:i,data:n},...ue(l)],s=r=>{let o=oe("output",i,l.length),a=o.type.value,p=[{name:"outputSize",type:"u32"},{name:"start",type:a},{name:"delta",type:a}];return` + ${r.registerUniforms(p).declareVariables(o)} ${r.mainStart()} ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} output[global_idx] = uniforms.start + ${a}(global_idx) * uniforms.delta; - }`};return{name:"Range",shaderCache:{hint:`${o}`},getShaderSource:s,getRunData:()=>({outputs:[{dims:l,dataType:o}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c})}},b2=e=>{let t=0,n=0,o=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],n=e.inputs[1].getInt32Array()[0],o=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],n=e.inputs[1].getFloat32Array()[0],o=e.inputs[2].getFloat32Array()[0]),pe.webgpu.validateInputContent&&Lb(t,n,o),e.compute(jb(t,n,o,e.inputs[0].dataType),{inputs:[]})}}),Ub,Vb,y2,_2,EA=D(()=>{"use strict";ae(),de(),Ve(),ce(),Ub=(e,t,n,o)=>{if(e!=="none"&&o!=="i32"&&o!=="u32"&&o!=="f32")throw new Error(`Input ${o} is not supported with reduction ${e}.`);let u=`{ + }`};return{name:"Range",shaderCache:{hint:`${i}`},getShaderSource:s,getRunData:()=>({outputs:[{dims:l,dataType:i}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c})}},Q2=e=>{let t=0,n=0,i=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],n=e.inputs[1].getInt32Array()[0],i=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],n=e.inputs[1].getFloat32Array()[0],i=e.inputs[2].getFloat32Array()[0]),me.webgpu.validateInputContent&&xy(t,n,i),e.compute($y(t,n,i,e.inputs[0].dataType),{inputs:[]})}}),Ty,Sy,e$,t$,Rk=D(()=>{"use strict";de(),pe(),He(),he(),Ty=(e,t,n,i)=>{if(e!=="none"&&i!=="i32"&&i!=="u32"&&i!=="f32")throw new Error(`Input ${i} is not supported with reduction ${e}.`);let u=`{ var oldValue = 0; loop { let newValueF32 =`,l=`; @@ -4191,9 +4191,9 @@ fn calculateOutputIndex(index: u32) -> u32 { } oldValue = res.old_value; } - }`;switch(e){case"none":return`${t}=${n};`;case"add":return o==="i32"||o==="u32"?`atomicAdd(&${t}, bitcast<${o}>(${n}));`:` - ${u}bitcast<${o}>(oldValue) + (${n})${l}`;case"max":return o==="i32"||o==="u32"?`atomicMax(&${t}, bitcast<${o}>(${n}));`:` - ${u}max(bitcast(oldValue), (${n}))${l}`;case"min":return o==="i32"||o==="u32"?`atomicMin(&${t}, bitcast<${o}>(${n}));`:`${u}min(bitcast<${o}>(oldValue), (${n}))${l}`;case"mul":return`${u}(bitcast<${o}>(oldValue) * (${n}))${l}`;default:throw new Error(`Reduction ${e} is not supported.`)}},Vb=(e,t)=>{let n=e[0].dims,o=e[1].dims,u=n,l=1,d=Math.ceil(z.sizeToDimension(o,o.length-1)/l),c=o[o.length-1],s=z.sizeFromDimension(n,c),r=[{type:12,data:d},{type:12,data:c},{type:12,data:s},...se(e[1].dims,e[2].dims,u)],i=a=>{let p=F("indices",e[1].dataType,e[1].dims.length),h=F("updates",e[2].dataType,e[2].dims.length,l),m=t.reduction!=="none"&&t.reduction!==""?qv("output",e[0].dataType,u.length):re("output",e[0].dataType,u.length,l);return` + }`;switch(e){case"none":return`${t}=${n};`;case"add":return i==="i32"||i==="u32"?`atomicAdd(&${t}, bitcast<${i}>(${n}));`:` + ${u}bitcast<${i}>(oldValue) + (${n})${l}`;case"max":return i==="i32"||i==="u32"?`atomicMax(&${t}, bitcast<${i}>(${n}));`:` + ${u}max(bitcast(oldValue), (${n}))${l}`;case"min":return i==="i32"||i==="u32"?`atomicMin(&${t}, bitcast<${i}>(${n}));`:`${u}min(bitcast<${i}>(oldValue), (${n}))${l}`;case"mul":return`${u}(bitcast<${i}>(oldValue) * (${n}))${l}`;default:throw new Error(`Reduction ${e} is not supported.`)}},Sy=(e,t)=>{let n=e[0].dims,i=e[1].dims,u=n,l=1,d=Math.ceil(F.sizeToDimension(i,i.length-1)/l),c=i[i.length-1],s=F.sizeFromDimension(n,c),r=[{type:12,data:d},{type:12,data:c},{type:12,data:s},...ue(e[1].dims,e[2].dims,u)],o=a=>{let p=j("indices",e[1].dataType,e[1].dims.length),h=j("updates",e[2].dataType,e[2].dims.length,l),m=t.reduction!=="none"&&t.reduction!==""?Ix("output",e[0].dataType,u.length):oe("output",e[0].dataType,u.length,l);return` ${a.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(p,h,m)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} @@ -4223,24 +4223,24 @@ fn calculateOutputIndex(index: u32) -> u32 { for (var i = 0u; i < uniforms.num_updates_elements; i++) { let value = updates[uniforms.num_updates_elements * global_idx + i]; - ${Ub(t.reduction,"output[data_offset + i]","value",m.type.value)} + ${Ty(t.reduction,"output[data_offset + i]","value",m.type.value)} } - }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:r}),getShaderSource:i}},y2=e=>Se({reduction:e.reduction}),_2=(e,t)=>{e.compute(Vb(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),qb,Gb,Hb,nu,Wb,Kb,Xb,Zb,Yb,Jb,Qb,ey,iu,ty,ry,ny,iy,oy,w2,v2,AA=D(()=>{"use strict";ae(),de(),Ve(),ce(),qb=(e,t)=>{if(e.every(n=>n>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and - one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Gb=(e,t,n)=>{t.every(u=>u>=0&&u{throw new Error("Resize requires axes input values to be positive and less than rank")}));let o=new Array(n).fill(1);return t.forEach((u,l)=>o[u]=e[l]),o},Hb=(e,t,n,o,u,l)=>{let[d,c,s]=n>10?[1,2,3]:[-1,e.length>1?1:-1,-1],r=e[0].dims.length;if(d>0&&e.length>d&&e[d].dims.length>0)e[d].getFloat32Array().forEach(i=>l.push(i));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(c>0&&e.length>c&&e[c].dims.length===1&&e[c].dims[0]>0){if(e[c].getFloat32Array().forEach(i=>o.push(i)),o.length!==0&&o.length!==r&&n>=18&&o.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");qb(o,t),t.axes.length>0&&Gb(o,t.axes,r).forEach((i,a)=>o[a]=i)}if(s>0&&e.length>s&&e[s].dims.length===1&&e[s].dims[0]>0&&(e[s].getBigInt64Array().forEach(i=>u.push(Number(i))),u.length!==0&&u.length!==r&&n>=18&&u.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(o.length!==0&&o.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(u.length!==0&&u.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof o<"u"&&typeof u<"u"&&o.length>0&&u.length>r)throw new Error("Resize requires only of scales or sizes to be specified")},nu=(e,t,n,o)=>` + }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:r}),getShaderSource:o}},e$=e=>ke({reduction:e.reduction}),t$=(e,t)=>{e.compute(Sy(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),Iy,Oy,Ey,ku,Ay,ky,Py,Cy,Ry,Dy,My,Ny,Pu,zy,By,Fy,Ly,jy,r$,n$,Dk=D(()=>{"use strict";de(),pe(),He(),he(),Iy=(e,t)=>{if(e.every(n=>n>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and + one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Oy=(e,t,n)=>{t.every(u=>u>=0&&u{throw new Error("Resize requires axes input values to be positive and less than rank")}));let i=new Array(n).fill(1);return t.forEach((u,l)=>i[u]=e[l]),i},Ey=(e,t,n,i,u,l)=>{let[d,c,s]=n>10?[1,2,3]:[-1,e.length>1?1:-1,-1],r=e[0].dims.length;if(d>0&&e.length>d&&e[d].dims.length>0)e[d].getFloat32Array().forEach(o=>l.push(o));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(c>0&&e.length>c&&e[c].dims.length===1&&e[c].dims[0]>0){if(e[c].getFloat32Array().forEach(o=>i.push(o)),i.length!==0&&i.length!==r&&n>=18&&i.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");Iy(i,t),t.axes.length>0&&Oy(i,t.axes,r).forEach((o,a)=>i[a]=o)}if(s>0&&e.length>s&&e[s].dims.length===1&&e[s].dims[0]>0&&(e[s].getBigInt64Array().forEach(o=>u.push(Number(o))),u.length!==0&&u.length!==r&&n>=18&&u.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(i.length!==0&&i.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(u.length!==0&&u.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof i<"u"&&typeof u<"u"&&i.length>0&&u.length>r)throw new Error("Resize requires only of scales or sizes to be specified")},ku=(e,t,n,i)=>` // The whole part and the fractional part are calculated separately due to inaccuracy of floating // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an // offset-by-one error later in floor(). let big = (${e}) * (${t}); - let whole = ${o}(big / (${n})); - let fract = ${o}(big % (${n})) / ${o}(${n}); + let whole = ${i}(big / (${n})); + let fract = ${i}(big % (${n})) / ${i}(${n}); return whole + fract; -`,Wb=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, +`,Ay=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return` if (xScale < 1.0 || floor(xScale) != xScale) { return ${t}(xResized) / ${t}(xScale); } else { - ${nu("xResized","lengthOriginal","lengthResized",t)} + ${ku("xResized","lengthOriginal","lengthResized",t)} } `;case"pytorch_half_pixel":return`if (lengthResized > 1) { return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5; @@ -4249,7 +4249,7 @@ fn calculateOutputIndex(index: u32) -> u32 { }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) { return 0.0; } else { - ${nu("xResized","lengthOriginal - 1","lengthResized - 1",t)} + ${ku("xResized","lengthOriginal - 1","lengthResized - 1",t)} }`;case"tf_crop_and_resize":return`if (lengthResized > 1) { return ${t}(roiStart) * ${t}(lengthOriginal - 1) + (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) / @@ -4260,38 +4260,38 @@ fn calculateOutputIndex(index: u32) -> u32 { const adjustment = ${t}(lengthResized) / outputWidth; const center = ${t}(lengthOriginal) / 2; const offset = center * (1 - adjustment); - return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Kb=(e,t,n)=>`fn getNearestPixelFromOriginal(xOriginal: ${n}, isDownSample: bool) -> ${n} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Xb=(e,t,n)=>{let o=new Array(n).fill(0).concat(new Array(n).fill(1)),u=e.length===0?o:e.slice();return t.length>0?(t.forEach((l,d)=>{o[l]=u[d],o[d+n]=u[t.length+d]}),o):u},Zb=(e,t,n,o)=>{let u=[];if(n.length>0)if(o.length>0){if(e.forEach(l=>u.push(l)),Math.max(...o)>e.length)throw new Error("axes is out of bound");o.forEach((l,d)=>u[l]=n[d])}else n.forEach(l=>u.push(l));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");u=e.map((l,d)=>Math.round(l*t[d]))}return u},Yb=(e,t,n)=>{let o=(()=>{switch(n.keepAspectRatioPolicy){case"not_larger":return n.axes.length>0?Math.min(...n.axes.map(l=>t[l]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return n.axes.length>0?Math.max(...n.axes.map(l=>t[l]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${n.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let u=e.slice();return n.axes.length>0?(n.axes.forEach(l=>t[l]=o),n.axes.forEach(l=>u[l]=Math.round(e[l]*t[l]))):(t.fill(o,0,t.length),u.forEach((l,d)=>u[d]=Math.round(l*t[d]))),u},Jb=(e,t,n,o,u)=>` + return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",ky=(e,t,n)=>`fn getNearestPixelFromOriginal(xOriginal: ${n}, isDownSample: bool) -> ${n} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Py=(e,t,n)=>{let i=new Array(n).fill(0).concat(new Array(n).fill(1)),u=e.length===0?i:e.slice();return t.length>0?(t.forEach((l,d)=>{i[l]=u[d],i[d+n]=u[t.length+d]}),i):u},Cy=(e,t,n,i)=>{let u=[];if(n.length>0)if(i.length>0){if(e.forEach(l=>u.push(l)),Math.max(...i)>e.length)throw new Error("axes is out of bound");i.forEach((l,d)=>u[l]=n[d])}else n.forEach(l=>u.push(l));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");u=e.map((l,d)=>Math.round(l*t[d]))}return u},Ry=(e,t,n)=>{let i=(()=>{switch(n.keepAspectRatioPolicy){case"not_larger":return n.axes.length>0?Math.min(...n.axes.map(l=>t[l]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return n.axes.length>0?Math.max(...n.axes.map(l=>t[l]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${n.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let u=e.slice();return n.axes.length>0?(n.axes.forEach(l=>t[l]=i),n.axes.forEach(l=>u[l]=Math.round(e[l]*t[l]))):(t.fill(i,0,t.length),u.forEach((l,d)=>u[d]=Math.round(l*t[d]))),u},Dy=(e,t,n,i,u)=>` fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${n.length}> { var original_indices: array<${e.type.value}, ${n.length}>; for (var i:u32 = 0; i < ${n.length}; i++) { var output_index = ${e.indicesGet("output_indices","i")}; - var scale = ${ie("uniforms.scales","i",o)}; - var roi_low = ${ie("uniforms.roi","i",u)}; - var roi_hi = ${ie("uniforms.roi",`i + ${t.length}`,u)}; + var scale = ${se("uniforms.scales","i",i)}; + var roi_low = ${se("uniforms.roi","i",u)}; + var roi_hi = ${se("uniforms.roi",`i + ${t.length}`,u)}; if (scale == 1.0) { original_indices[i] = ${e.type.value}(output_index); } else { - var input_shape_i = ${ie("uniforms.input_shape","i",t.length)}; - var output_shape_i = ${ie("uniforms.output_shape","i",n.length)}; + var input_shape_i = ${se("uniforms.input_shape","i",t.length)}; + var output_shape_i = ${se("uniforms.output_shape","i",n.length)}; original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, input_shape_i, roi_low, roi_hi); } } return original_indices; - }`,Qb=(e,t,n,o,u,l,d)=>` + }`,My=(e,t,n,i,u,l,d)=>` fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { var input_indices: ${e.type.indices}; - for (var i:u32 = 0; i < ${o.length}; i++) { + for (var i:u32 = 0; i < ${i.length}; i++) { var output_index = ${t.indicesGet("output_indices","i")}; var input_index: u32; - var scale = ${ie("uniforms.scales","i",u)}; + var scale = ${se("uniforms.scales","i",u)}; if (scale == 1.0) { input_index = output_index; } else { - var roi_low = ${ie("uniforms.roi","i",l)}; - var roi_hi = ${ie("uniforms.roi",`i + ${n.length}`,l)}; - var input_shape_i = ${ie("uniforms.input_shape","i",n.length)}; - var output_shape_i = ${ie("uniforms.output_shape","i",o.length)}; + var roi_low = ${se("uniforms.roi","i",l)}; + var roi_hi = ${se("uniforms.roi",`i + ${n.length}`,l)}; + var input_shape_i = ${se("uniforms.input_shape","i",n.length)}; + var output_shape_i = ${se("uniforms.output_shape","i",i.length)}; var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, input_shape_i, roi_low, roi_hi); if (!${d} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { @@ -4309,24 +4309,24 @@ fn calculateOutputIndex(index: u32) -> u32 { ${e.indicesSet("input_indices","i","input_index")} } return input_indices; - }`,ey=(e,t)=>` + }`,Ny=(e,t)=>` fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { for (var i:u32 = 0; i < ${t.length}; i++) { var input_index = ${e.indicesGet("input_indices","i")}; - if (input_index < 0 || input_index >= ${ie("uniforms.input_shape","i",t.length)}) { + if (input_index < 0 || input_index >= ${se("uniforms.input_shape","i",t.length)}) { return false; } } return true; - }`,iu=(e,t,n,o)=>e.rank>o?` + }`,Pu=(e,t,n,i)=>e.rank>i?` ${e.indicesSet("input_indices",t,"channel")}; ${e.indicesSet("input_indices",n,"batch")}; -`:"",ty=(e,t,n,o,u)=>{let[l,d,c,s]=n.length===2?[-1,0,1,-1]:[0,2,3,1],r=e.type.value;return` +`:"",zy=(e,t,n,i,u)=>{let[l,d,c,s]=n.length===2?[-1,0,1,-1]:[0,2,3,1],r=e.type.value;return` fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${r} { var input_indices: ${e.type.indices}; ${e.indicesSet("input_indices",d,`max(0, min(row, ${n[d]} - 1))`)}; ${e.indicesSet("input_indices",c,`max(0, min(col, ${n[c]} - 1))`)}; - ${iu(e,s,l,2)} + ${Pu(e,s,l,2)} return ${e.getByIndices("input_indices")}; } @@ -4334,7 +4334,7 @@ fn calculateOutputIndex(index: u32) -> u32 { var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); var row:${r} = originalIndices[${d}]; var col:${r} = originalIndices[${c}]; - ${o?`if (row < 0 || row > (${n[d]} - 1) || col < 0 || col > (${n[c]} - 1)) { + ${i?`if (row < 0 || row > (${n[d]} - 1) || col < 0 || col > (${n[c]} - 1)) { return ${u}; }`:""}; row = max(0, min(row, ${n[d]} - 1)); @@ -4362,32 +4362,32 @@ fn calculateOutputIndex(index: u32) -> u32 { dy2 = 0.5; } return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); - }`},ry=(e,t,n,o,u,l,d,c,s,r)=>{let i=n.length===2,a=!0,[p,h]=i?[0,1]:a?[2,3]:[1,2],m=e.type.value,b=$=>{let _=$===p?"row":"col";return` - fn ${_}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${m} { - var output_index = ${t.indicesGet("output_indices",$)}; - var originalIdx: ${m} = getOriginalCoordinateFromResizedCoordinate(output_index, ${u[$]}, - ${o[$]}, ${n[$]}, ${l[$]}, ${l[$]} + ${n.length}); + }`},By=(e,t,n,i,u,l,d,c,s,r)=>{let o=n.length===2,a=!0,[p,h]=o?[0,1]:a?[2,3]:[1,2],m=e.type.value,g=_=>{let w=_===p?"row":"col";return` + fn ${w}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${m} { + var output_index = ${t.indicesGet("output_indices",_)}; + var originalIdx: ${m} = getOriginalCoordinateFromResizedCoordinate(output_index, ${u[_]}, + ${i[_]}, ${n[_]}, ${l[_]}, ${l[_]} + ${n.length}); var fractOriginalIdx: ${m} = originalIdx - floor(originalIdx); var coefs = getCubicInterpolationCoefs(fractOriginalIdx); - if (${c} && (originalIdx < 0 || originalIdx > (${n[$]} - 1))) { + if (${c} && (originalIdx < 0 || originalIdx > (${n[_]} - 1))) { return ${s}; } var data: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); for (var i: i32 = -1; i < 3; i++) { - var ${_}: ${m} = originalIdx + ${m}(i); - if (${_} < 0 || ${_} >= ${n[$]}) { + var ${w}: ${m} = originalIdx + ${m}(i); + if (${w} < 0 || ${w} >= ${n[_]}) { ${r?`coefs[i + 1] = 0.0; - continue;`:c?`return ${s};`:`${_} = max(0, min(${_}, ${n[$]} - 1));`}; + continue;`:c?`return ${s};`:`${w} = max(0, min(${w}, ${n[_]} - 1));`}; } var input_indices_copy: ${e.type.indices} = input_indices; - ${e.indicesSet("input_indices_copy",$,`u32(${_})`)}; - data[i + 1] = ${$===p?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; + ${e.indicesSet("input_indices_copy",_,`u32(${w})`)}; + data[i + 1] = ${_===p?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; } return cubicInterpolation1D(data, coefs); }`};return` - ${b(p)}; - ${b(h)}; + ${g(p)}; + ${g(h)}; fn getCubicInterpolationCoefs(s: ${m}) -> array<${m}, 4> { var absS = abs(s); var coeffs: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); @@ -4410,22 +4410,22 @@ fn calculateOutputIndex(index: u32) -> u32 { var input_indices: ${e.type.indices} = output_indices; return colCubicInterpolation(input_indices, output_indices); } - `},ny=(e,t,n,o,u)=>{let[l,d,c,s,r]=n.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],i=e.type.value;return` - fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${i} { + `},Fy=(e,t,n,i,u)=>{let[l,d,c,s,r]=n.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],o=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${o} { var input_indices: ${e.type.indices}; ${e.indicesSet("input_indices",d,`max(0, min(depth, ${n[d]} - 1))`)}; ${e.indicesSet("input_indices",c,`max(0, min(height, ${n[c]} - 1))`)}; ${e.indicesSet("input_indices",s,`max(0, min(width, ${n[s]} - 1))`)}; - ${iu(e,r,l,3)} + ${Pu(e,r,l,3)} return ${e.getByIndices("input_indices")}; } - fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${i} { + fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${o} { var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); - var depth:${i} = originalIndices[${d}]; - var height:${i} = originalIndices[${c}]; - var width:${i} = originalIndices[${s}]; - ${o?`if (depth < 0 || depth > (${n[d]} - 1) || height < 0 || height > (${n[c]} - 1) || width < 0 || (width > ${n[s]} - 1)) { + var depth:${o} = originalIndices[${d}]; + var height:${o} = originalIndices[${c}]; + var width:${o} = originalIndices[${s}]; + ${i?`if (depth < 0 || depth > (${n[d]} - 1) || height < 0 || height > (${n[c]} - 1) || width < 0 || (width > ${n[s]} - 1)) { return ${u}; }`:""}; @@ -4441,20 +4441,20 @@ fn calculateOutputIndex(index: u32) -> u32 { var channel: u32 = ${n.length>3?`u32(originalIndices[${r}])`:"0"}; var batch: u32 = ${n.length>3?`u32(originalIndices[${l}])`:"0"}; - var x111: ${i} = getInputValue(batch, channel, depth1, height1, width1); - var x112: ${i} = getInputValue(batch, channel, depth1, height1, width2); - var x121: ${i} = getInputValue(batch, channel, depth1, height2, width1); - var x122: ${i} = getInputValue(batch, channel, depth1, height2, width2); - var x211: ${i} = getInputValue(batch, channel, depth2, height1, width1); - var x212: ${i} = getInputValue(batch, channel, depth2, height1, width2); - var x221: ${i} = getInputValue(batch, channel, depth2, height2, width1); - var x222: ${i} = getInputValue(batch, channel, depth2, height2, width2); - var dx1: ${i} = abs(depth - ${i}(depth1)); - var dx2: ${i} = abs(${i}(depth2) - depth); - var dy1: ${i} = abs(height - ${i}(height1)); - var dy2: ${i} = abs(${i}(height2) - height); - var dz1: ${i} = abs(width - ${i}(width1)); - var dz2: ${i} = abs(${i}(width2) - width); + var x111: ${o} = getInputValue(batch, channel, depth1, height1, width1); + var x112: ${o} = getInputValue(batch, channel, depth1, height1, width2); + var x121: ${o} = getInputValue(batch, channel, depth1, height2, width1); + var x122: ${o} = getInputValue(batch, channel, depth1, height2, width2); + var x211: ${o} = getInputValue(batch, channel, depth2, height1, width1); + var x212: ${o} = getInputValue(batch, channel, depth2, height1, width2); + var x221: ${o} = getInputValue(batch, channel, depth2, height2, width1); + var x222: ${o} = getInputValue(batch, channel, depth2, height2, width2); + var dx1: ${o} = abs(depth - ${o}(depth1)); + var dx2: ${o} = abs(${o}(depth2) - depth); + var dy1: ${o} = abs(height - ${o}(height1)); + var dy2: ${o} = abs(${o}(height2) - height); + var dz1: ${o} = abs(width - ${o}(width1)); + var dz2: ${o} = abs(${o}(width2) - width); if (depth1 == depth2) { dx1 = 0.5; dx2 = 0.5; @@ -4469,25 +4469,25 @@ fn calculateOutputIndex(index: u32) -> u32 { } return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); - }`},iy=(e,t,n,o,u,l)=>{let d=e.dims,c=Xb(l,t.axes,d.length),s=Zb(d,o,u,t.axes),r=o.slice();o.length===0&&(r=d.map((y,T)=>y===0?1:s[T]/y),t.keepAspectRatioPolicy!=="stretch"&&(s=Yb(d,r,t)));let i=re("output",e.dataType,s.length),a=F("input",e.dataType,d.length),p=z.size(s),h=d.length===s.length&&d.every((y,T)=>y===s[T]),m=t.coordinateTransformMode==="tf_crop_and_resize",b=t.extrapolationValue,$=a.type.value,_=y=>` + }`},Ly=(e,t,n,i,u,l)=>{let d=e.dims,c=Py(l,t.axes,d.length),s=Cy(d,i,u,t.axes),r=i.slice();i.length===0&&(r=d.map((y,T)=>y===0?1:s[T]/y),t.keepAspectRatioPolicy!=="stretch"&&(s=Ry(d,r,t)));let o=oe("output",e.dataType,s.length),a=j("input",e.dataType,d.length),p=F.size(s),h=d.length===s.length&&d.every((y,T)=>y===s[T]),m=t.coordinateTransformMode==="tf_crop_and_resize",g=t.extrapolationValue,_=a.type.value,w=y=>` ${h?"":` - ${Wb(t.coordinateTransformMode,$)}; + ${Ay(t.coordinateTransformMode,_)}; ${(()=>{switch(t.mode){case"nearest":return` - ${ey(a,d)}; - ${Kb(t.nearestMode,n,$)}; - ${Qb(a,i,d,s,r.length,c.length,m)}; + ${Ny(a,d)}; + ${ky(t.nearestMode,n,_)}; + ${My(a,o,d,s,r.length,c.length,m)}; `;case"linear":return` - ${Jb(i,d,s,r.length,c.length)}; - ${(()=>{if(d.length===2||d.length===4)return`${ty(a,i,d,m,b)}`;if(d.length===3||d.length===5)return`${ny(a,i,d,m,b)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; + ${Dy(o,d,s,r.length,c.length)}; + ${(()=>{if(d.length===2||d.length===4)return`${zy(a,o,d,m,g)}`;if(d.length===3||d.length===5)return`${Fy(a,o,d,m,g)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; `;case"cubic":return` - ${(()=>{if(d.length===2||d.length===4)return`${ry(a,i,d,s,r,c,t.cubicCoeffA,m,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; + ${(()=>{if(d.length===2||d.length===4)return`${By(a,o,d,s,r,c,t.cubicCoeffA,m,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; `;default:throw Error("Invalid resize mode")}})()}; `} - ${y.registerUniform("output_size","u32").registerUniform("scales","f32",r.length).registerUniform("roi","f32",c.length).declareVariables(a,i)} + ${y.registerUniform("output_size","u32").registerUniform("scales","f32",r.length).registerUniform("roi","f32",c.length).declareVariables(a,o)} ${y.mainStart()} ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} ${h?"output[global_idx] = input[global_idx];":` - let output_indices = ${i.offsetToIndices("global_idx")}; + let output_indices = ${o.offsetToIndices("global_idx")}; var input_indices: ${a.type.indices}; ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); if (checkInputIndices(input_indices)) { @@ -4496,21 +4496,21 @@ fn calculateOutputIndex(index: u32) -> u32 { output[global_idx] = ${t.extrapolationValue}; }`;case"linear":return`output[global_idx] = ${d.length===2||d.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()}; `} - }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${n}|${r.length>0?t.mode==="cubic"?r:r.length:""}|${u.length>0?u:""}|${c.length>0?c:""}|${h}|${t.mode==="nearest"?d.length:d}`,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:[{type:12,data:p},{type:1,data:r},{type:1,data:c},...se(d,s)]})}},oy=e=>{let t=e.customDataBuffer;return new Uint32Array(t.buffer,t.byteOffset,1)[0]},w2=(e,t)=>{let n=[],o=[],u=[],l=oy(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Hb(e.inputs,t,l,n,o,u),e.compute(iy(e.inputs[0],t,l,n,o,u),{inputs:[0]})},v2=e=>{let t=e.antialias,n=e.axes,o=e.coordinateTransformMode,u=e.cubicCoeffA,l=e.excludeOutside!==0,d=e.extrapolationValue,c=e.keepAspectRatioPolicy,s=e.mode,r=e.nearestMode===""?"simple":e.nearestMode;return Se({antialias:t,axes:n,coordinateTransformMode:o,cubicCoeffA:u,excludeOutside:l,extrapolationValue:d,keepAspectRatioPolicy:c,mode:s,nearestMode:r})}}),sy,ay,x2,kA=D(()=>{"use strict";ae(),de(),ce(),sy=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],n=e[1],o=e[2];if(t.dataType!==n.dataType||t.dataType!==o.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(n.dims.length!==3&&n.dims.length!==2)throw new Error("Skip must be 2D or 3D");let u=t.dims[t.dims.length-1],l=t.dims[t.dims.length-2];if(n.dims[n.dims.length-1]!==u)throw new Error("Skip must have the same hidden size as input");if(n.dims[n.dims.length-2]!==l)throw new Error("Skip must have the same sequence length as input");if(o.dims.length!==1)throw new Error("Gamma must be 1D");if(o.dims[o.dims.length-1]!==u)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let d=e[3];if(d.dims.length!==1)throw new Error("Beta must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let d=e[4];if(d.dims.length!==1)throw new Error("Bias must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Bias must have the same hidden size as input")}},ay=(e,t,n,o)=>{let u=t.simplified,l=e[0].dims,d=z.size(l),c=l,s=d,r=l.slice(-1)[0],i=o?l.slice(0,-1).concat(1):[],a=!u&&e.length>3,p=e.length>4,h=o&&n>1,m=o&&n>2,b=n>3,$=64,_=Ue(r),y=[{type:12,data:s},{type:12,data:_},{type:12,data:r},{type:1,data:t.epsilon}],T=O=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],P=[F("x",e[0].dataType,e[0].dims,_),F("skip",e[1].dataType,e[1].dims,_),F("gamma",e[2].dataType,e[2].dims,_)];a&&P.push(F("beta",e[3].dataType,e[3].dims,_)),p&&P.push(F("bias",e[4].dataType,e[4].dims,_)),P.push(re("output",e[0].dataType,c,_)),h&&P.push(re("mean_output",1,i)),m&&P.push(re("inv_std_output",1,i)),b&&P.push(re("input_skip_bias_sum",e[0].dataType,c,_));let I=Je(e[0].dataType),L=Je(1,_);return` + }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${n}|${r.length>0?t.mode==="cubic"?r:r.length:""}|${u.length>0?u:""}|${c.length>0?c:""}|${h}|${t.mode==="nearest"?d.length:d}`,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:[{type:12,data:p},{type:1,data:r},{type:1,data:c},...ue(d,s)]})}},jy=e=>{let t=e.customDataBuffer;return new Uint32Array(t.buffer,t.byteOffset,1)[0]},r$=(e,t)=>{let n=[],i=[],u=[],l=jy(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Ey(e.inputs,t,l,n,i,u),e.compute(Ly(e.inputs[0],t,l,n,i,u),{inputs:[0]})},n$=e=>{let t=e.antialias,n=e.axes,i=e.coordinateTransformMode,u=e.cubicCoeffA,l=e.excludeOutside!==0,d=e.extrapolationValue,c=e.keepAspectRatioPolicy,s=e.mode,r=e.nearestMode===""?"simple":e.nearestMode;return ke({antialias:t,axes:n,coordinateTransformMode:i,cubicCoeffA:u,excludeOutside:l,extrapolationValue:d,keepAspectRatioPolicy:c,mode:s,nearestMode:r})}}),Uy,Vy,i$,Mk=D(()=>{"use strict";de(),pe(),he(),Uy=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],n=e[1],i=e[2];if(t.dataType!==n.dataType||t.dataType!==i.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(n.dims.length!==3&&n.dims.length!==2)throw new Error("Skip must be 2D or 3D");let u=t.dims[t.dims.length-1],l=t.dims[t.dims.length-2];if(n.dims[n.dims.length-1]!==u)throw new Error("Skip must have the same hidden size as input");if(n.dims[n.dims.length-2]!==l)throw new Error("Skip must have the same sequence length as input");if(i.dims.length!==1)throw new Error("Gamma must be 1D");if(i.dims[i.dims.length-1]!==u)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let d=e[3];if(d.dims.length!==1)throw new Error("Beta must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let d=e[4];if(d.dims.length!==1)throw new Error("Bias must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Bias must have the same hidden size as input")}},Vy=(e,t,n,i)=>{let u=t.simplified,l=e[0].dims,d=F.size(l),c=l,s=d,r=l.slice(-1)[0],o=i?l.slice(0,-1).concat(1):[],a=!u&&e.length>3,p=e.length>4,h=i&&n>1,m=i&&n>2,g=n>3,_=64,w=Ge(r),y=[{type:12,data:s},{type:12,data:w},{type:12,data:r},{type:1,data:t.epsilon}],T=O=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],P=[j("x",e[0].dataType,e[0].dims,w),j("skip",e[1].dataType,e[1].dims,w),j("gamma",e[2].dataType,e[2].dims,w)];a&&P.push(j("beta",e[3].dataType,e[3].dims,w)),p&&P.push(j("bias",e[4].dataType,e[4].dims,w)),P.push(oe("output",e[0].dataType,c,w)),h&&P.push(oe("mean_output",1,o)),m&&P.push(oe("inv_std_output",1,o)),g&&P.push(oe("input_skip_bias_sum",e[0].dataType,c,w));let I=nt(e[0].dataType),z=nt(1,w);return` ${O.registerUniforms(A).declareVariables(...P)} - var sum_shared : array<${L}, ${$}>; - var sum_squared_shared : array<${L}, ${$}>; + var sum_shared : array<${z}, ${_}>; + var sum_squared_shared : array<${z}, ${_}>; - ${O.mainStart([$,1,1])} + ${O.mainStart([_,1,1])} let ix = local_id.x; - let iy = global_id.x / ${$}; + let iy = global_id.x / ${_}; let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; - var stride = hidden_size_vectorized / ${$}; + var stride = hidden_size_vectorized / ${_}; let offset = ix * stride + iy * hidden_size_vectorized; let offset1d = stride * ix; - if (ix == ${$-1}) { + if (ix == ${_-1}) { stride = hidden_size_vectorized - stride * ix; } for (var i: u32 = 0; i < stride; i++) { @@ -4518,15 +4518,15 @@ fn calculateOutputIndex(index: u32) -> u32 { let bias_value = ${p?"bias[offset1d + i]":I+"(0.0)"}; let input_value = x[offset + i]; let value = input_value + skip_value + bias_value; - ${b?"input_skip_bias_sum[offset + i] = value;":""} + ${g?"input_skip_bias_sum[offset + i] = value;":""} output[offset + i] = value; - let f32_value = ${_n(I,_,"value")}; + let f32_value = ${Mn(I,w,"value")}; sum_shared[ix] += f32_value; sum_squared_shared[ix] += f32_value * f32_value; } workgroupBarrier(); - var reduce_size : u32 = ${$}; + var reduce_size : u32 = ${_}; for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { reduce_size = curr_size + (reduce_size & 1); if (ix < curr_size) { @@ -4538,8 +4538,8 @@ fn calculateOutputIndex(index: u32) -> u32 { let sum = sum_shared[0]; let square_sum = sum_squared_shared[0]; - let mean = ${yr("sum",_)} / f32(uniforms.hidden_size); - let inv_std_dev = inverseSqrt(${yr("square_sum",_)} / f32(uniforms.hidden_size) ${u?"":"- mean * mean"} + uniforms.epsilon); + let mean = ${kr("sum",w)} / f32(uniforms.hidden_size); + let inv_std_dev = inverseSqrt(${kr("square_sum",w)} / f32(uniforms.hidden_size) ${u?"":"- mean * mean"} + uniforms.epsilon); ${h?"mean_output[global_idx] = mean;":""} ${m?"inv_std_output[global_idx] = inv_std_dev;":""} @@ -4548,14 +4548,14 @@ fn calculateOutputIndex(index: u32) -> u32 { ${I}(inv_std_dev) * gamma[offset1d + i] ${a?"+ beta[offset1d + i]":""}; } - }`},S=[{dims:c,dataType:e[0].dataType}];return n>1&&S.push({dims:i,dataType:1}),n>2&&S.push({dims:i,dataType:1}),n>3&&S.push({dims:l,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${_};${h};${m};${b}`,inputDependencies:e.map((O,A)=>"type")},getShaderSource:T,getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(s/r)},programUniforms:y})}},x2=(e,t)=>{sy(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(ay(e.inputs,t,e.outputCount,!1),{outputs:n})}}),uy,Qn,ly,ou,dy,cy,$2,T2,PA=D(()=>{"use strict";ae(),de(),Ve(),ce(),uy=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((n,o)=>{if(e[o+1].dataType!==6&&e[o+1].dataType!==7)throw new Error(`Input ${o} must be an array of int32 or int64`)})},Qn=(e,t)=>{let n=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(o=>n.push(Number(o)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(o=>n.push(Number(o)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return n},ly=(e,t)=>{if(e.length>1){let n=Qn(e,1),o=Qn(e,2),u=Qn(e,3);return u.length===0&&(u=[...Array(e[0].dims.length).keys()]),Se({starts:n,ends:o,axes:u})}else return t},ou=(e,t,n,o,u)=>{let l=e;return e<0&&(l+=n[o[t]]),u[t]<0?Math.max(0,Math.min(l,n[o[t]]-1)):Math.max(0,Math.min(l,n[o[t]]))},dy=(e,t,n)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + }`},S=[{dims:c,dataType:e[0].dataType}];return n>1&&S.push({dims:o,dataType:1}),n>2&&S.push({dims:o,dataType:1}),n>3&&S.push({dims:l,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${w};${h};${m};${g}`,inputDependencies:e.map((O,A)=>"type")},getShaderSource:T,getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(s/r)},programUniforms:y})}},i$=(e,t)=>{Uy(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(Vy(e.inputs,t,e.outputCount,!1),{outputs:n})}}),qy,gi,Gy,Cu,Hy,Wy,o$,s$,Nk=D(()=>{"use strict";de(),pe(),He(),he(),qy=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((n,i)=>{if(e[i+1].dataType!==6&&e[i+1].dataType!==7)throw new Error(`Input ${i} must be an array of int32 or int64`)})},gi=(e,t)=>{let n=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(i=>n.push(Number(i)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(i=>n.push(Number(i)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return n},Gy=(e,t)=>{if(e.length>1){let n=gi(e,1),i=gi(e,2),u=gi(e,3);return u.length===0&&(u=[...Array(e[0].dims.length).keys()]),ke({starts:n,ends:i,axes:u})}else return t},Cu=(e,t,n,i,u)=>{let l=e;return e<0&&(l+=n[i[t]]),u[t]<0?Math.max(0,Math.min(l,n[i[t]]-1)):Math.max(0,Math.min(l,n[i[t]]))},Hy=(e,t,n)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { var input_indices: ${e.type.indices}; var carry = 0u; for (var i = ${n.length-1}; i >= 0; i--) { - let input_shape_i = ${ie("uniforms.input_shape","i",n.length)}; - let steps_i = ${ie("uniforms.steps","i",n.length)}; - let signs_i = ${ie("uniforms.signs","i",n.length)}; - let starts_i = ${ie("uniforms.starts","i",n.length)}; + let input_shape_i = ${se("uniforms.input_shape","i",n.length)}; + let steps_i = ${se("uniforms.steps","i",n.length)}; + let signs_i = ${se("uniforms.signs","i",n.length)}; + let starts_i = ${se("uniforms.starts","i",n.length)}; var output_index = ${t.indicesGet("output_indices","i")}; var input_index = output_index * steps_i + starts_i + carry; carry = input_index / input_shape_i; @@ -4566,18 +4566,18 @@ fn calculateOutputIndex(index: u32) -> u32 { ${e.indicesSet("input_indices","i","input_index")}; } return input_indices; - }`,cy=(e,t)=>{let n=e[0].dims,o=z.size(n),u=t.axes.length>0?z.normalizeAxes(t.axes,n.length):[...Array(n.length).keys()],l=Qn(e,4);l.forEach(_=>_!==0||(()=>{throw new Error("step cannot be 0")})),l.length===0&&(l=Array(u.length).fill(1));let d=t.starts.map((_,y)=>ou(_,y,n,u,l)),c=t.ends.map((_,y)=>ou(_,y,n,u,l));if(u.length!==d.length||u.length!==c.length)throw new Error("start, ends and axes should have the same number of elements");if(u.length!==n.length)for(let _=0;_Math.sign(_));l.forEach((_,y,T)=>{if(_<0){let S=(c[y]-d[y])/_,O=d[y],A=O+S*l[y];d[y]=A,c[y]=O,T[y]=-_}});let r=n.slice(0);u.forEach((_,y)=>{r[_]=Math.ceil((c[_]-d[_])/l[_])});let i={dims:r,dataType:e[0].dataType},a=re("output",e[0].dataType,r.length),p=F("input",e[0].dataType,e[0].dims.length),h=z.size(r),m=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:d.length},{name:"signs",type:"i32",length:s.length},{name:"steps",type:"u32",length:l.length}],b=[{type:12,data:h},{type:12,data:d},{type:6,data:s},{type:12,data:l},...se(e[0].dims,r)],$=_=>` - ${_.registerUniforms(m).declareVariables(p,a)} - ${dy(p,a,n)} - ${_.mainStart()} - ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + }`,Wy=(e,t)=>{let n=e[0].dims,i=F.size(n),u=t.axes.length>0?F.normalizeAxes(t.axes,n.length):[...Array(n.length).keys()],l=gi(e,4);l.forEach(w=>w!==0||(()=>{throw new Error("step cannot be 0")})),l.length===0&&(l=Array(u.length).fill(1));let d=t.starts.map((w,y)=>Cu(w,y,n,u,l)),c=t.ends.map((w,y)=>Cu(w,y,n,u,l));if(u.length!==d.length||u.length!==c.length)throw new Error("start, ends and axes should have the same number of elements");if(u.length!==n.length)for(let w=0;wMath.sign(w));l.forEach((w,y,T)=>{if(w<0){let S=(c[y]-d[y])/w,O=d[y],A=O+S*l[y];d[y]=A,c[y]=O,T[y]=-w}});let r=n.slice(0);u.forEach((w,y)=>{r[w]=Math.ceil((c[w]-d[w])/l[w])});let o={dims:r,dataType:e[0].dataType},a=oe("output",e[0].dataType,r.length),p=j("input",e[0].dataType,e[0].dims.length),h=F.size(r),m=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:d.length},{name:"signs",type:"i32",length:s.length},{name:"steps",type:"u32",length:l.length}],g=[{type:12,data:h},{type:12,data:d},{type:6,data:s},{type:12,data:l},...ue(e[0].dims,r)],_=w=>` + ${w.registerUniforms(m).declareVariables(p,a)} + ${Hy(p,a,n)} + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let output_indices = ${a.offsetToIndices("global_idx")}; let input_indices = calculateInputIndices(output_indices); ${a.setByOffset("global_idx",p.getByIndices("input_indices"))} - }`;return{name:"Slice",shaderCache:{hint:`${s.length}_${d.length}_${l.length}`,inputDependencies:["rank"]},getShaderSource:$,getRunData:()=>({outputs:[i],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:b})}},$2=(e,t)=>{uy(e.inputs,t);let n=ly(e.inputs,t);e.compute(cy(e.inputs,n),{inputs:[0]})},T2=e=>{let t=e.starts,n=e.ends,o=e.axes;return Se({starts:t,ends:n,axes:o})}}),py,hy,S2,I2,CA=D(()=>{"use strict";ae(),de(),Ve(),wr(),ce(),py=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},hy=(e,t)=>{let n=e.inputs[0],o=n.dims,u=z.size(o),l=o.length,d=z.normalizeAxis(t.axis,l),c=dI),r[d]=l-1,r[l-1]=d,s=e.compute(gt(n,r),{inputs:[n],outputs:[-1]})[0]):s=n;let i=s.dims,a=i[l-1],p=u/a,h=Ue(a),m=a/h,b=64;p===1&&(b=256);let $=(P,I)=>I===4?`max(max(${P}.x, ${P}.y), max(${P}.z, ${P}.w))`:I===2?`max(${P}.x, ${P}.y)`:I===3?`max(max(${P}.x, ${P}.y), ${P}.z)`:P,_=F("x",s.dataType,s.dims,h),y=re("result",s.dataType,s.dims,h),T=_.type.value,S=Je(s.dataType)==="f32"?`var threadMax = ${T}(-3.4028234663852886e+38f);`:`var threadMax = ${T}(-65504.0h);`,O=P=>` + }`;return{name:"Slice",shaderCache:{hint:`${s.length}_${d.length}_${l.length}`,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:[o],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:g})}},o$=(e,t)=>{qy(e.inputs,t);let n=Gy(e.inputs,t);e.compute(Wy(e.inputs,n),{inputs:[0]})},s$=e=>{let t=e.starts,n=e.ends,i=e.axes;return ke({starts:t,ends:n,axes:i})}}),Ky,Xy,a$,u$,zk=D(()=>{"use strict";de(),pe(),He(),Cr(),he(),Ky=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},Xy=(e,t)=>{let n=e.inputs[0],i=n.dims,u=F.size(i),l=i.length,d=F.normalizeAxis(t.axis,l),c=dI),r[d]=l-1,r[l-1]=d,s=e.compute($t(n,r),{inputs:[n],outputs:[-1]})[0]):s=n;let o=s.dims,a=o[l-1],p=u/a,h=Ge(a),m=a/h,g=64;p===1&&(g=256);let _=(P,I)=>I===4?`max(max(${P}.x, ${P}.y), max(${P}.z, ${P}.w))`:I===2?`max(${P}.x, ${P}.y)`:I===3?`max(max(${P}.x, ${P}.y), ${P}.z)`:P,w=j("x",s.dataType,s.dims,h),y=oe("result",s.dataType,s.dims,h),T=w.type.value,S=nt(s.dataType)==="f32"?`var threadMax = ${T}(-3.4028234663852886e+38f);`:`var threadMax = ${T}(-65504.0h);`,O=P=>` var rowMaxShared : ${T}; var rowSumShared : ${T}; - var threadShared : array<${T}, ${b}>; + var threadShared : array<${T}, ${g}>; fn getValue(row: i32, col: i32, row_stride: i32) -> ${T} { let index = row * row_stride + col; @@ -4588,11 +4588,11 @@ fn calculateOutputIndex(index: u32) -> u32 { let index = row * row_stride + col; result[index] = value; } - ${P.registerUniform("packedCols","i32").declareVariables(_,y)} - ${P.mainStart(b)} + ${P.registerUniform("packedCols","i32").declareVariables(w,y)} + ${P.mainStart(g)} let gindex = i32(global_idx); let lindex = i32(local_idx); - const wg = ${b}; + const wg = ${g}; let row = gindex / wg; let cols = uniforms.packedCols; let row_stride : i32 = uniforms.packedCols; @@ -4617,7 +4617,7 @@ fn calculateOutputIndex(index: u32) -> u32 { workgroupBarrier(); } if (lindex == 0) { - rowMaxShared = ${T}(${$("threadShared[0]",h)}); + rowMaxShared = ${T}(${_("threadShared[0]",h)}); } workgroupBarrier(); @@ -4637,7 +4637,7 @@ fn calculateOutputIndex(index: u32) -> u32 { workgroupBarrier(); } if (lindex == 0) { - rowSumShared = ${T}(${yr("threadShared[0]",h)}); + rowSumShared = ${T}(${kr("threadShared[0]",h)}); } workgroupBarrier(); @@ -4648,11 +4648,11 @@ fn calculateOutputIndex(index: u32) -> u32 { value = max(value, ${T}(0.0)); setValue(row, col, row_stride, value); } - }`,A=e.compute({name:"Softmax",shaderCache:{hint:`${h};${b}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:i,dataType:s.dataType}],dispatchGroup:{x:p},programUniforms:[{type:6,data:m}]}),getShaderSource:O},{inputs:[s],outputs:[c?-1:0]})[0];c&&e.compute(gt(A,r),{inputs:[A]})},S2=(e,t)=>{py(e.inputs),hy(e,t)},I2=e=>Se({axis:e.axis})}),su,fy,my,gy,O2,DA=D(()=>{"use strict";ae(),de(),ce(),su=e=>Array.from(e.getBigInt64Array(),Number),fy=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(su(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},my=(e,t)=>{let n=[];for(let o=0;o{let n=e[0].dims,o=t??su(e[1]),u=my(n,o),l=z.size(u),d=e[0].dataType,c=F("input",d,n.length),s=re("output",d,u.length),r=i=>` + }`,A=e.compute({name:"Softmax",shaderCache:{hint:`${h};${g}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:o,dataType:s.dataType}],dispatchGroup:{x:p},programUniforms:[{type:6,data:m}]}),getShaderSource:O},{inputs:[s],outputs:[c?-1:0]})[0];c&&e.compute($t(A,r),{inputs:[A]})},a$=(e,t)=>{Ky(e.inputs),Xy(e,t)},u$=e=>ke({axis:e.axis})}),Ru,Zy,Yy,Jy,l$,Bk=D(()=>{"use strict";de(),pe(),he(),Ru=e=>Array.from(e.getBigInt64Array(),Number),Zy=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(Ru(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},Yy=(e,t)=>{let n=[];for(let i=0;i{let n=e[0].dims,i=t??Ru(e[1]),u=Yy(n,i),l=F.size(u),d=e[0].dataType,c=j("input",d,n.length),s=oe("output",d,u.length),r=o=>` const inputShape = ${c.indices(...n)}; - ${i.registerUniform("output_size","u32").declareVariables(c,s)} - ${i.mainStart()} - ${i.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${o.registerUniform("output_size","u32").declareVariables(c,s)} + ${o.mainStart()} + ${o.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let output_indices = ${s.offsetToIndices("global_idx")}; var input_indices: ${c.type.indices}; for (var i = 0; i < ${n.length}; i++) { @@ -4662,7 +4662,7 @@ fn calculateOutputIndex(index: u32) -> u32 { ${c.indicesSet("input_indices","i","input_dim_value")} } ${s.setByOffset("global_idx",c.getByIndices("input_indices"))} - }`;return{name:"Tile",shaderCache:{hint:`${o}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:[{type:12,data:l},...se(e[0].dims,u)]}),getShaderSource:r}},O2=e=>{fy(e.inputs),e.compute(gy(e.inputs),{inputs:[0]})}}),by,yy,E2,RA=D(()=>{"use strict";ae(),de(),ce(),by=(e,t,n,o,u)=>{let l=re("output_data",u,n.length,4),d=F("a_data",t[1].dataType,t[1].dims.length,4),c=F("b_data",t[2].dataType,t[2].dims.length,4),s=F("c_data",t[0].dataType,t[0].dims.length,4),r,i=(a,p,h)=>`select(${p}, ${a}, ${h})`;if(!o)r=l.setByOffset("global_idx",i(d.getByOffset("global_idx"),c.getByOffset("global_idx"),s.getByOffset("global_idx")));else{let a=(p,h,m="")=>{let b=`a_data[index_a${h}][component_a${h}]`,$=`b_data[index_b${h}][component_b${h}]`,_=`bool(c_data[index_c${h}] & (0xffu << (component_c${h} * 8)))`;return` + }`;return{name:"Tile",shaderCache:{hint:`${i}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:[{type:12,data:l},...ue(e[0].dims,u)]}),getShaderSource:r}},l$=e=>{Zy(e.inputs),e.compute(Jy(e.inputs),{inputs:[0]})}}),Qy,e0,d$,Fk=D(()=>{"use strict";de(),pe(),he(),Qy=(e,t,n,i,u)=>{let l=oe("output_data",u,n.length,4),d=j("a_data",t[1].dataType,t[1].dims.length,4),c=j("b_data",t[2].dataType,t[2].dims.length,4),s=j("c_data",t[0].dataType,t[0].dims.length,4),r,o=(a,p,h)=>`select(${p}, ${a}, ${h})`;if(!i)r=l.setByOffset("global_idx",o(d.getByOffset("global_idx"),c.getByOffset("global_idx"),s.getByOffset("global_idx")));else{let a=(p,h,m="")=>{let g=`a_data[index_a${h}][component_a${h}]`,_=`b_data[index_b${h}][component_b${h}]`,w=`bool(c_data[index_c${h}] & (0xffu << (component_c${h} * 8)))`;return` let output_indices${h} = ${l.offsetToIndices(`global_idx * 4u + ${h}u`)}; let offset_a${h} = ${d.broadcastedIndicesToOffset(`output_indices${h}`,l)}; let offset_b${h} = ${c.broadcastedIndicesToOffset(`output_indices${h}`,l)}; @@ -4673,7 +4673,7 @@ fn calculateOutputIndex(index: u32) -> u32 { let component_a${h} = offset_a${h} % 4u; let component_b${h} = offset_b${h} % 4u; let component_c${h} = offset_c${h} % 4u; - ${p}[${h}] = ${m}(${i(b,$,_)}); + ${p}[${h}] = ${m}(${o(g,_,w)}); `};u===9?r=` var data = vec4(0); ${a("data",0,"u32")} @@ -4690,22 +4690,22 @@ fn calculateOutputIndex(index: u32) -> u32 { ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} ${r} - }`},yy=e=>{let t=e[1].dims,n=e[2].dims,o=e[0].dims,u=e[1].dataType,l=!(z.areEqual(t,n)&&z.areEqual(n,o)),d=t,c=z.size(t);if(l){let r=xn.calcShape(xn.calcShape(t,n,!1),o,!1);if(!r)throw new Error("Can't perform where op on the given tensors");d=r,c=z.size(d)}let s=Math.ceil(c/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:r=>by(r,e,d,l,u),getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:s},...se(o,t,n,d)]})}},E2=e=>{e.compute(yy(e.inputs))}}),A2,NA=D(()=>{"use strict";KE(),Ll(),XE(),ZE(),YE(),JE(),QE(),iA(),sA(),aA(),uA(),lA(),dA(),cA(),pA(),hA(),fA(),mA(),gA(),bA(),yA(),_A(),wA(),vA(),xA(),$A(),K1(),TA(),SA(),IA(),OA(),EA(),Fl(),AA(),Q1(),kA(),PA(),CA(),Y1(),DA(),wr(),jl(),RA(),A2=new Map([["Abs",[_x]],["Acos",[wx]],["Acosh",[vx]],["Add",[r1]],["ArgMax",[mx,al]],["ArgMin",[fx,al]],["Asin",[xx]],["Asinh",[$x]],["Atan",[Tx]],["Atanh",[Sx]],["Attention",[gx]],["AveragePool",[u2,a2]],["BatchNormalization",[bx]],["BiasAdd",[yx]],["BiasSplitGelu",[t1]],["Cast",[Ox,Ix]],["Ceil",[Ax]],["Clip",[Ex]],["Concat",[p1,h1]],["Conv",[hl,pl]],["ConvTranspose",[$1,x1]],["Cos",[kx]],["Cosh",[Px]],["CumSum",[T1,S1]],["DepthToSpace",[I1,O1]],["DequantizeLinear",[m2,g2]],["DFT",[E1,A1]],["Div",[n1]],["Einsum",[k1,P1]],["Elu",[Cx,ui]],["Equal",[i1]],["Erf",[Dx]],["Exp",[Rx]],["Expand",[C1]],["FastGelu",[D1]],["Floor",[Nx]],["FusedConv",[hl,pl]],["Gather",[N1,R1]],["GatherElements",[j1,L1]],["GatherBlockQuantized",[B1,F1]],["GatherND",[z1,M1]],["Gelu",[zx]],["Gemm",[V1,U1]],["GlobalAveragePool",[d2,l2]],["GlobalMaxPool",[f2,h2]],["Greater",[u1]],["GreaterOrEqual",[d1]],["GridSample",[q1,G1]],["GroupQueryAttention",[e2]],["HardSigmoid",[qx,Vx]],["HardSwish",[Gx]],["InstanceNormalization",[t2]],["LayerNormalization",[r2]],["LeakyRelu",[Mx,ui]],["Less",[l1]],["LessOrEqual",[c1]],["Log",[Qx]],["MatMul",[n2]],["MatMulNBits",[i2,o2]],["MaxPool",[c2,p2]],["Mul",[o1]],["MultiHeadAttention",[W1,H1]],["Neg",[Fx]],["Not",[Bx]],["Pad",[s2]],["Pow",[s1]],["QuickGelu",[e1,ui]],["Range",[b2]],["Reciprocal",[Lx]],["ReduceMin",[lx]],["ReduceMean",[ix]],["ReduceMax",[ux]],["ReduceSum",[cx]],["ReduceProd",[dx]],["ReduceL1",[ox]],["ReduceL2",[sx]],["ReduceLogSum",[hx]],["ReduceLogSumExp",[ax]],["ReduceSumSquare",[px]],["Relu",[jx]],["Resize",[w2,v2]],["RotaryEmbedding",[J1]],["ScatterND",[_2,y2]],["Sigmoid",[Ux]],["Sin",[Hx]],["Sinh",[Wx]],["Slice",[$2,T2]],["SkipLayerNormalization",[x2]],["Split",[X1,Z1]],["Sqrt",[Kx]],["Softmax",[S2,I2]],["Sub",[a1]],["Tan",[Xx]],["Tanh",[Zx]],["ThresholdedRelu",[Jx,ui]],["Tile",[O2]],["Transpose",[Hv,Wv]],["Where",[E2]]])}),k2,zA=D(()=>{"use strict";it(),Yt(),ce(),k2=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n,o,u){jt(e.programInfo.name);let l=this.backend.device,d=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let c=[];for(let r of t)c.push({binding:c.length,resource:{buffer:r.buffer}});for(let r of n)c.push({binding:c.length,resource:{buffer:r.buffer}});u&&c.push({binding:c.length,resource:u});let s=l.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:c,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let r={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:s,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(r)}d.setPipeline(e.computePipeline),d.setBindGroup(0,s),d.dispatchWorkgroups(...o),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Dt(e.programInfo.name)}dispose(){}build(e,t){jt(e.name);let n=this.backend.device,o=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(r=>{n.features.has(r.feature)&&o.push(`enable ${r.extension};`)});let u=Gv(t,this.backend.device.limits),l=e.getShaderSource(u),d=`${o.join(` + }`},e0=e=>{let t=e[1].dims,n=e[2].dims,i=e[0].dims,u=e[1].dataType,l=!(F.areEqual(t,n)&&F.areEqual(n,i)),d=t,c=F.size(t);if(l){let r=Bn.calcShape(Bn.calcShape(t,n,!1),i,!1);if(!r)throw new Error("Can't perform where op on the given tensors");d=r,c=F.size(d)}let s=Math.ceil(c/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:r=>Qy(r,e,d,l,u),getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:s},...ue(i,t,n,d)]})}},d$=e=>{e.compute(e0(e.inputs))}}),c$,Lk=D(()=>{"use strict";QA(),hd(),ek(),tk(),rk(),nk(),ik(),lk(),ck(),pk(),hk(),fk(),mk(),gk(),bk(),yk(),wk(),_k(),vk(),xk(),$k(),Tk(),Sk(),Ik(),Ok(),Ek(),k2(),Ak(),kk(),Pk(),Ck(),Rk(),pd(),Dk(),M2(),Mk(),Nk(),zk(),R2(),Bk(),Cr(),fd(),Fk(),c$=new Map([["Abs",[t1]],["Acos",[r1]],["Acosh",[n1]],["Add",[B1]],["ArgMax",[Yx,Dl]],["ArgMin",[Zx,Dl]],["Asin",[i1]],["Asinh",[o1]],["Atan",[s1]],["Atanh",[a1]],["Attention",[Jx]],["AveragePool",[q2,V2]],["BatchNormalization",[Qx]],["BiasAdd",[e1]],["BiasSplitGelu",[z1]],["Cast",[l1,u1]],["Ceil",[c1]],["Clip",[d1]],["Concat",[K1,X1]],["Conv",[Ll,Fl]],["ConvTranspose",[o2,i2]],["Cos",[p1]],["Cosh",[h1]],["CumSum",[s2,a2]],["DepthToSpace",[u2,l2]],["DequantizeLinear",[Y2,J2]],["DFT",[d2,c2]],["Div",[F1]],["Einsum",[p2,h2]],["Elu",[f1,Si]],["Equal",[L1]],["Erf",[m1]],["Exp",[g1]],["Expand",[f2]],["FastGelu",[m2]],["Floor",[b1]],["FusedConv",[Ll,Fl]],["Gather",[b2,g2]],["GatherElements",[$2,x2]],["GatherBlockQuantized",[_2,v2]],["GatherND",[y2,w2]],["Gelu",[y1]],["Gemm",[S2,T2]],["GlobalAveragePool",[H2,G2]],["GlobalMaxPool",[Z2,X2]],["Greater",[q1]],["GreaterOrEqual",[H1]],["GridSample",[I2,O2]],["GroupQueryAttention",[N2]],["HardSigmoid",[I1,S1]],["HardSwish",[O1]],["InstanceNormalization",[z2]],["LayerNormalization",[B2]],["LeakyRelu",[w1,Si]],["Less",[G1]],["LessOrEqual",[W1]],["Log",[M1]],["MatMul",[F2]],["MatMulNBits",[L2,j2]],["MaxPool",[W2,K2]],["Mul",[j1]],["MultiHeadAttention",[A2,E2]],["Neg",[v1]],["Not",[_1]],["Pad",[U2]],["Pow",[U1]],["QuickGelu",[N1,Si]],["Range",[Q2]],["Reciprocal",[x1]],["ReduceMin",[Gx]],["ReduceMean",[Lx]],["ReduceMax",[qx]],["ReduceSum",[Wx]],["ReduceProd",[Hx]],["ReduceL1",[jx]],["ReduceL2",[Ux]],["ReduceLogSum",[Xx]],["ReduceLogSumExp",[Vx]],["ReduceSumSquare",[Kx]],["Relu",[$1]],["Resize",[r$,n$]],["RotaryEmbedding",[D2]],["ScatterND",[t$,e$]],["Sigmoid",[T1]],["Sin",[E1]],["Sinh",[A1]],["Slice",[o$,s$]],["SkipLayerNormalization",[i$]],["Split",[P2,C2]],["Sqrt",[k1]],["Softmax",[a$,u$]],["Sub",[V1]],["Tan",[P1]],["Tanh",[C1]],["ThresholdedRelu",[D1,Si]],["Tile",[l$]],["Transpose",[Ex,Ax]],["Where",[d$]]])}),p$,jk=D(()=>{"use strict";dt(),ur(),he(),p$=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n,i,u){Jt(e.programInfo.name);let l=this.backend.device,d=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let c=[];for(let r of t)c.push({binding:c.length,resource:{buffer:r.buffer}});for(let r of n)c.push({binding:c.length,resource:{buffer:r.buffer}});u&&c.push({binding:c.length,resource:u});let s=l.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:c,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let r={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:s,dispatchGroup:i};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(r)}d.setPipeline(e.computePipeline),d.setBindGroup(0,s),d.dispatchWorkgroups(...i),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Vt(e.programInfo.name)}dispose(){}build(e,t){Jt(e.name);let n=this.backend.device,i=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(r=>{n.features.has(r.feature)&&i.push(`enable ${r.extension};`)});let u=Ox(t,this.backend.device.limits),l=e.getShaderSource(u),d=`${i.join(` `)} ${u.additionalImplementations} -${l}`,c=n.createShaderModule({code:d,label:e.name});ve("verbose",()=>`[WebGPU] ${e.name} shader code: ${d}`);let s=n.createComputePipeline({compute:{module:c,entryPoint:"main"},layout:"auto",label:e.name});return Dt(e.name),{programInfo:e,computePipeline:s,uniformVariablesInfo:u.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,n=typeof e=="number"?1:e.y||1,o=typeof e=="number"?1:e.z||1,u=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=u&&n<=u&&o<=u)return[t,n,o];let l=t*n*o,d=Math.ceil(Math.sqrt(l));if(d>u){if(d=Math.ceil(Math.cbrt(l)),d>u)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[d,d,d]}else return[d,d,1]}}}),P2={};nn(P2,{WebGpuBackend:()=>C2});var _y,wy,vy,C2,MA=D(()=>{"use strict";it(),ae(),Yt(),Lv(),HE(),NA(),zA(),_y=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let n=[];for(let o=0;o{let o=e.name;return e.shaderCache?.hint&&(o+="["+e.shaderCache.hint+"]"),o+=":"+n+`:${_y(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,o},vy=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vendor)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},C2=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n},u=c=>t.features.has(c)&&n.push(c)&&!0;u("chromium-experimental-timestamp-query-inside-passes")||u("timestamp-query"),u("shader-f16"),u("subgroups"),this.device=await t.requestDevice(o);let l=t,d=t.info??(typeof l.requestAdapterInfo=="function"?await l.requestAdapterInfo():void 0);this.adapterInfo=new vy(d),this.gpuDataManager=Vv(this),this.programManager=new k2(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Nl(e.logLevel,!!e.debug),this.device.onuncapturederror=c=>{c.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${c.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!0}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose(),this.device&&this.env?.webgpu&&this.device.lost.then(()=>{delete this.env.webgpu.device})}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;jt(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{let t=new BigUint64Array(e.getMappedRange()),n=this.pendingQueries.get(e);for(let o=0;o"u"&&(this.queryTimeBase=p);let m=Number(p-this.queryTimeBase),b=Number(h-this.queryTimeBase);if(!Number.isSafeInteger(m)||!Number.isSafeInteger(b))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:i.map($=>({dims:$.dims,dataType:Xt($.dataType)})),outputsMetadata:a.map($=>({dims:$.dims,dataType:Xt($.dataType)})),kernelId:l,kernelType:c,kernelName:s,programName:r,startTime:m,endTime:b});else{let $="";i.forEach((y,T)=>{$+=`input[${T}]: [${y.dims}] | ${Xt(y.dataType)}, `});let _="";a.forEach((y,T)=>{_+=`output[${T}]: [${y.dims}] | ${Xt(y.dataType)}, `}),console.log(`[profiling] kernel "${l}|${c}|${s}|${r}" ${$}${_}start time: ${m} ns, execution time: ${b-m} ns`)}wo("GPU",`${r}::${p}::${h}`)}e.unmap(),this.pendingQueries.delete(e)}),Dt()}run(e,t,n,o,u,l){jt(e.name);let d=[];for(let y=0;yT):n;if(i.length!==c.length)throw new Error(`Output size ${i.length} must be equal to ${c.length}.`);let a=[],p=[];for(let y=0;y=l)throw new Error(`Invalid output index: ${i[y]}`);if(i[y]===-3)continue;let T=i[y]===-1,S=i[y]===-2,O=T||S?u(c[y].dataType,c[y].dims):o(i[y],c[y].dataType,c[y].dims);if(a.push(O),O.data===0)continue;let A=this.gpuDataManager.get(O.data);if(!A)throw new Error(`no GPU data for output: ${O.data}`);if(T&&this.temporaryData.push(A),S){let P=this.kernelPersistentData.get(this.currentKernelId);P||(P=[],this.kernelPersistentData.set(this.currentKernelId,P)),P.push(A)}p.push(A)}if(d.length!==t.length||p.length!==a.length){if(p.length===0)return Dt(e.name),a;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let h;if(r){let y=0,T=[];r.forEach(P=>{let I=typeof P.data=="number"?[P.data]:P.data;if(I.length===0)return;let L=P.type===10?2:4,V,Z;P.type===10?(Z=I.length>4?16:I.length>2?8:I.length*L,V=I.length>4?16:L*I.length):(Z=I.length<=2?I.length*L:16,V=16),y=Math.ceil(y/Z)*Z,T.push(y);let q=P.type===10?8:4;y+=I.length>4?Math.ceil(I.length/q)*V:I.length*L});let S=16;y=Math.ceil(y/S)*S;let O=new ArrayBuffer(y);r.forEach((P,I)=>{let L=T[I],V=typeof P.data=="number"?[P.data]:P.data;if(P.type===6)new Int32Array(O,L,V.length).set(V);else if(P.type===12)new Uint32Array(O,L,V.length).set(V);else if(P.type===10)new Uint16Array(O,L,V.length).set(V);else if(P.type===1)new Float32Array(O,L,V.length).set(V);else throw new Error(`Unsupported uniform type: ${Xt(P.type)}`)});let A=this.gpuDataManager.create(y,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(A.buffer,0,O,0,y),this.gpuDataManager.release(A.id),h={offset:0,size:y,buffer:A.buffer}}let m=this.programManager.normalizeDispatchGroupSize(s),b=m[1]===1&&m[2]===1,$=wy(e,t,b),_=this.programManager.getArtifact($);if(_||(_=this.programManager.build(e,m),this.programManager.setArtifact($,_),ve("info",()=>`[artifact] key: ${$}, programName: ${e.name}`)),r&&_.uniformVariablesInfo){if(r.length!==_.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${_.uniformVariablesInfo.length}, got ${r.length} in program "${_.programInfo.name}".`);for(let y=0;y`[ProgramManager] run "${e.name}" (key=${$}) with ${m[0]}x${m[1]}x${m[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let y={kernelId:this.currentKernelId,programName:_.programInfo.name,inputTensorViews:t,outputTensorViews:a};this.pendingKernels.push(y),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(y)}return this.programManager.run(_,d,p,m,h),Dt(e.name),a}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,n,o){let u=A2.get(e);if(!u)throw new Error(`kernel not implemented: ${e}`);let l={kernelType:e,kernelName:o,kernelEntry:u[0],attributes:[u[1],n]};this.kernels.set(t,l)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let n of t)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,n){let o=this.kernels.get(e);if(!o)throw new Error(`kernel not created: ${e}`);let u=o.kernelType,l=o.kernelName,d=o.kernelEntry,c=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${u}] ${l}" is not allowed to be called recursively`);this.currentKernelId=e,c[0]&&(c[1]=c[0](c[1]),c[0]=void 0),ve("info",()=>`[WebGPU] Start to run kernel "[${u}] ${l}"...`);let s=this.env.debug;this.temporaryData=[];try{return s&&this.device.pushErrorScope("validation"),d(t,c[1]),0}catch(r){return n.push(Promise.resolve(`[WebGPU] Kernel "[${u}] ${l}" failed. ${r}`)),1}finally{s&&n.push(this.device.popErrorScope().then(r=>r?`GPU validation error for kernel "[${u}] ${l}": ${r.message}`:null));for(let r of this.temporaryData)this.gpuDataManager.release(r.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,n,o){let u=this.sessionExternalDataMapping.get(e);u||(u=new Map,this.sessionExternalDataMapping.set(e,u));let l=u.get(t),d=this.gpuDataManager.registerExternalBuffer(n,o,l);return u.set(t,[d,n]),d}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,n){return async()=>{let o=await il(this,e,t);return zl(o.buffer,n)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){ve("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){ve("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){ve("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),n=e.length;this.pendingKernels=[];for(let o=0;o=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),D2={};nn(D2,{init:()=>R2});var ho,xy,R2,BA=D(()=>{"use strict";ae(),Yt(),de(),GE(),ho=class N2{constructor(t,n,o,u){this.module=t,this.dataType=n,this.data=o,this.dims=u}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(z.size(t)!==z.size(this.dims))throw new Error("Invalid new shape");return new N2(this.module,this.dataType,this.data,t)}},xy=class{constructor(e,t,n){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let o=e.PTR_SIZE,u=n/e.PTR_SIZE,l=o===4?"i32":"i64";this.opKernelContext=Number(e.getValue(o*u++,l));let d=Number(e.getValue(o*u++,l));this.outputCount=Number(e.getValue(o*u++,l)),this.customDataOffset=Number(e.getValue(o*u++,"*")),this.customDataSize=Number(e.getValue(o*u++,l));let c=[];for(let s=0;stypeof d=="number"?this.inputs[d]:d)??this.inputs,o=t?.outputs??[],u=(d,c,s)=>new ho(this.module,c,this.output(d,s),s),l=(d,c)=>{let s=Xr(d,c);if(!s)throw new Error(`Unsupported data type: ${d}`);let r=s>0?this.backend.gpuDataManager.create(s).id:0;return new ho(this.module,d,r,c)};return this.backend.run(e,n,o,u,l,this.outputCount)}output(e,t){let n=this.module.stackSave();try{let o=this.module.PTR_SIZE,u=o===4?"i32":"i64",l=this.module.stackAlloc((1+t.length)*o);this.module.setValue(l,t.length,u);for(let d=0;d{let u=t.jsepInit;if(!u)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let l=(MA(),wn(P2)).WebGpuBackend,d=new l;await d.initialize(n,o),u("webgpu",[d,c=>d.alloc(Number(c)),c=>d.free(c),(c,s,r,i=!1)=>{if(i)ve("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(c)}, dst=${Number(s)}, size=${Number(r)}`),d.memcpy(Number(c),Number(s));else{ve("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(c)}, gpuDataId=${Number(s)}, size=${Number(r)}`);let a=t.HEAPU8.subarray(Number(c>>>0),Number(c>>>0)+Number(r));d.upload(Number(s),a)}},async(c,s,r)=>{ve("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${c}, dataOffset=${s}, size=${r}`),await d.download(Number(c),()=>t.HEAPU8.subarray(Number(s)>>>0,Number(s+r)>>>0))},(c,s,r)=>d.createKernel(c,Number(s),r,t.UTF8ToString(t._JsepGetNodeName(Number(s)))),c=>d.releaseKernel(c),(c,s,r,i)=>{ve("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${r}, kernel=${c}, contextDataOffset=${s}`);let a=new xy(t,d,Number(s));return d.computeKernel(Number(c),a,i)},()=>d.captureBegin(),()=>d.captureEnd(),()=>d.replay()])}else{let l=new Uv(n);u("webnn",[l,()=>l.reserveTensorId(),d=>l.releaseTensorId(d),async(d,c,s,r,i)=>l.ensureTensor(d,c,s,r,i),(d,c)=>{l.uploadTensor(d,c)},async(d,c)=>l.downloadTensor(d,c),(d,c)=>l.registerMLContext(d,c),!!n.trace])}}}),$y,Wl,Kl,ur,Ty,au,Po,Xl,Zl,uu,Yl,Jl,Ql,z2=D(()=>{"use strict";it(),UE(),VE(),ae(),sn(),Pl(),zv(),$y=(e,t)=>{Ne()._OrtInit(e,t)!==0&&ke("Can't initialize onnxruntime.")},Wl=async e=>{$y(e.wasm.numThreads,Io(e.logLevel))},Kl=async(e,t)=>{Ne().asyncInit?.();let n=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let u=e.webgpu.forceFallbackAdapter;if(u!==void 0&&typeof u!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${u}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:u}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let o=(BA(),wn(D2)).init;t==="webgpu"&&await o("webgpu",Ne(),e,n),t==="webnn"&&await o("webnn",Ne(),e)}},ur=new Map,Ty=e=>{let t=Ne(),n=t.stackSave();try{let o=t.PTR_SIZE,u=t.stackAlloc(2*o);t._OrtGetInputOutputCount(e,u,u+o)!==0&&ke("Can't get session input/output count.");let l=o===4?"i32":"i64";return[Number(t.getValue(u,l)),Number(t.getValue(u+o,l))]}finally{t.stackRestore(n)}},au=(e,t)=>{let n=Ne(),o=n.stackSave(),u=0;try{let l=n.PTR_SIZE,d=n.stackAlloc(2*l);n._OrtGetInputOutputMetadata(e,t,d,d+l)!==0&&ke("Can't get session input/output metadata.");let c=Number(n.getValue(d,"*"));u=Number(n.getValue(d+l,"*"));let s=n.HEAP32[u/4];if(s===0)return[c,0];let r=n.HEAPU32[u/4+1],i=[];for(let a=0;a{let t=Ne(),n=t._malloc(e.byteLength);if(n===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,n),[n,e.byteLength]},Xl=async(e,t)=>{let n,o,u=Ne();Array.isArray(e)?[n,o]=e:e.buffer===u.HEAPU8.buffer?[n,o]=[e.byteOffset,e.byteLength]:[n,o]=Po(e);let l=0,d=0,c=0,s=[],r=[],i=[];try{if([d,s]=await Nv(t),t?.externalData&&u.mountExternalData){let S=[];for(let O of t.externalData){let A=typeof O=="string"?O:O.path,P=typeof O=="string"?O:O.data;S.push(Rl(P).then(I=>{u.mountExternalData(A,I)}))}await Promise.all(S)}for(let S of t?.executionProviders??[])if((typeof S=="string"?S:S.name)==="webnn"){if(u.shouldTransferToMLTensor=!1,typeof S!="string"){let O=S,A=O?.context,P=O?.gpuDevice,I=O?.deviceType,L=O?.powerPreference;A?u.currentContext=A:P?u.currentContext=await u.webnnCreateMLContext(P):u.currentContext=await u.webnnCreateMLContext({deviceType:I,powerPreference:L})}else u.currentContext=await u.webnnCreateMLContext();break}l=await u._OrtCreateSession(n,o,d),u.webgpuOnCreateSession?.(l),l===0&&ke("Can't create a session."),u.jsepOnCreateSession?.(),u.currentContext&&(u.webnnRegisterMLContext(l,u.currentContext),u.currentContext=void 0,u.shouldTransferToMLTensor=!0);let[a,p]=Ty(l),h=!!t?.enableGraphCapture,m=[],b=[],$=[],_=[],y=[];for(let S=0;SS==="gpu-buffer"||S==="ml-tensor"||S==="ml-tensor-cpu-output")&&(c=u._OrtCreateBinding(l),c===0&&ke("Can't create IO binding."),T={handle:c,outputPreferredLocations:y,outputPreferredLocationsEncoded:y.map(S=>S==="ml-tensor-cpu-output"?"ml-tensor":S).map(S=>nl(S))}),ur.set(l,[l,r,i,T,h,!1]),[l,m,b,$,_]}catch(a){throw r.forEach(p=>u._OrtFree(p)),i.forEach(p=>u._OrtFree(p)),c!==0&&u._OrtReleaseBinding(c)!==0&&ke("Can't release IO binding."),l!==0&&u._OrtReleaseSession(l)!==0&&ke("Can't release session."),a}finally{u._free(n),d!==0&&u._OrtReleaseSessionOptions(d)!==0&&ke("Can't release session options."),s.forEach(a=>u._free(a)),u.unmountExternalData?.()}},Zl=e=>{let t=Ne(),n=ur.get(e);if(!n)throw new Error(`cannot release session. invalid session id: ${e}`);let[o,u,l,d,c]=n;d&&(c&&t._OrtClearBoundOutputs(d.handle)!==0&&ke("Can't clear bound outputs."),t._OrtReleaseBinding(d.handle)!==0&&ke("Can't release IO binding.")),t.jsepOnReleaseSession?.(e),t.webnnOnReleaseSession?.(e),t.webgpuOnReleaseSession?.(e),u.forEach(s=>t._OrtFree(s)),l.forEach(s=>t._OrtFree(s)),t._OrtReleaseSession(o)!==0&&ke("Can't release session."),ur.delete(e)},uu=async(e,t,n,o,u,l,d=!1)=>{if(!e){t.push(0);return}let c=Ne(),s=c.PTR_SIZE,r=e[0],i=e[1],a=e[3],p=a,h,m;if(r==="string"&&(a==="gpu-buffer"||a==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(d&&a!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${l} when enableGraphCapture is true.`);if(a==="gpu-buffer"){let _=e[2].gpuBuffer;m=Xr(Kr(r),i);{let y=c.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');h=y(o,l,_,m)}}else if(a==="ml-tensor"){let _=e[2].mlTensor;m=Xr(Kr(r),i);let y=c.webnnRegisterMLTensor;if(!y)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');h=y(o,_,Kr(r),i)}else{let _=e[2];if(Array.isArray(_)){m=s*_.length,h=c._malloc(m),n.push(h);for(let y=0;y<_.length;y++){if(typeof _[y]!="string")throw new TypeError(`tensor data at index ${y} is not a string`);c.setValue(h+y*s,Ct(_[y],n),"*")}}else{let y=c.webnnIsGraphInput,T=c.webnnIsGraphOutput;if(r!=="string"&&y&&T){let S=c.UTF8ToString(u);if(y(o,S)||T(o,S)){let O=Kr(r);m=Xr(O,i),p="ml-tensor";let A=c.webnnCreateTemporaryTensor,P=c.webnnUploadTensor;if(!A||!P)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');let I=await A(o,O,i);P(I,new Uint8Array(_.buffer,_.byteOffset,_.byteLength)),h=I}else m=_.byteLength,h=c._malloc(m),n.push(h),c.HEAPU8.set(new Uint8Array(_.buffer,_.byteOffset,m),h)}else m=_.byteLength,h=c._malloc(m),n.push(h),c.HEAPU8.set(new Uint8Array(_.buffer,_.byteOffset,m),h)}}let b=c.stackSave(),$=c.stackAlloc(4*i.length);try{i.forEach((y,T)=>c.setValue($+T*s,y,s===4?"i32":"i64"));let _=c._OrtCreateTensor(Kr(r),h,m,$,i.length,nl(p));_===0&&ke(`Can't create tensor for input/output. session=${o}, index=${l}.`),t.push(_)}finally{c.stackRestore(b)}},Yl=async(e,t,n,o,u,l)=>{let d=Ne(),c=d.PTR_SIZE,s=ur.get(e);if(!s)throw new Error(`cannot run inference. invalid session id: ${e}`);let r=s[0],i=s[1],a=s[2],p=s[3],h=s[4],m=s[5],b=t.length,$=o.length,_=0,y=[],T=[],S=[],O=[],A=[],P=d.stackSave(),I=d.stackAlloc(b*c),L=d.stackAlloc(b*c),V=d.stackAlloc($*c),Z=d.stackAlloc($*c);try{[_,y]=Rv(l),Yr("wasm prepareInputOutputTensor");for(let w=0;wOe*je,1);J=Xt(K);let Ae=p?.outputPreferredLocations[o[w]];if(J==="string"){if(Ae==="gpu-buffer"||Ae==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let Oe=[];for(let je=0;je0){let Oe=d.jsepGetBuffer;if(!Oe)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let je=Oe(B),at=Xr(K,le);if(at===void 0||!Cl(J))throw new Error(`Unsupported data type: ${J}`);G=!0,N.push([J,Q,{gpuBuffer:je,download:d.jsepCreateDownloader(je,at,J),dispose:()=>{d._OrtReleaseTensor(C)!==0&&ke("Can't release tensor.")}},"gpu-buffer"])}else if(Ae==="ml-tensor"&&le>0){let Oe=d.webnnEnsureTensor,je=d.webnnIsGraphInputOutputTypeSupported;if(!Oe||!je)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(Xr(K,le)===void 0||!Dl(J))throw new Error(`Unsupported data type: ${J}`);if(!je(e,J,!1))throw new Error(`preferredLocation "ml-tensor" for ${J} output is not supported by current WebNN Context.`);let at=await Oe(e,B,K,Q,!1);G=!0,N.push([J,Q,{mlTensor:at,download:d.webnnCreateMLTensorDownloader(B,J),dispose:()=>{d.webnnReleaseTensorId(B),d._OrtReleaseTensor(C)}},"ml-tensor"])}else if(Ae==="ml-tensor-cpu-output"&&le>0){let Oe=d.webnnCreateMLTensorDownloader(B,J)(),je=N.length;G=!0,ue.push((async()=>{let at=[je,await Oe];return d.webnnReleaseTensorId(B),d._OrtReleaseTensor(C),at})()),N.push([J,Q,[],"cpu"])}else{let Oe=Lo(J),je=new Oe(le);new Uint8Array(je.buffer,je.byteOffset,je.byteLength).set(d.HEAPU8.subarray(B,B+je.byteLength)),N.push([J,Q,je,"cpu"])}}finally{d.stackRestore(U),J==="string"&&B&&d._free(B),G||d._OrtReleaseTensor(C)}}p&&!h&&(d._OrtClearBoundOutputs(p.handle)!==0&&ke("Can't clear bound outputs."),ur.set(e,[r,i,a,p,h,!1]));for(let[w,C]of await Promise.all(ue))N[w][2]=C;return Jr("wasm ProcessOutputTensor"),N}finally{d.webnnOnRunEnd?.(r),d.stackRestore(P),T.forEach(q=>d._OrtReleaseTensor(q)),S.forEach(q=>d._OrtReleaseTensor(q)),O.forEach(q=>d._free(q)),_!==0&&d._OrtReleaseRunOptions(_),y.forEach(q=>d._free(q))}},Jl=e=>{let t=Ne(),n=ur.get(e);if(!n)throw new Error("invalid session id");let o=n[0],u=t._OrtEndProfiling(o);u===0&&ke("Can't get an profile file name."),t._OrtFree(u)},Ql=e=>{let t=[];for(let n of e){let o=n[2];!Array.isArray(o)&&"buffer"in o&&t.push(o.buffer)}return t}}),lr,_t,hn,ei,ti,fo,lu,mo,Cr,Dr,Sy,M2,B2,F2,L2,j2,U2,V2,q2=D(()=>{"use strict";it(),z2(),sn(),Al(),lr=()=>!!pe.wasm.proxy&&typeof document<"u",hn=!1,ei=!1,ti=!1,mo=new Map,Cr=(e,t)=>{let n=mo.get(e);n?n.push(t):mo.set(e,[t])},Dr=()=>{if(hn||!ei||ti||!_t)throw new Error("worker not ready")},Sy=e=>{switch(e.data.type){case"init-wasm":hn=!1,e.data.err?(ti=!0,lu[1](e.data.err)):(ei=!0,lu[0]()),fo&&(URL.revokeObjectURL(fo),fo=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=mo.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},M2=async()=>{if(!ei){if(hn)throw new Error("multiple calls to 'initWasm()' detected.");if(ti)throw new Error("previous call to 'initWasm()' failed.");if(hn=!0,lr())return new Promise((e,t)=>{_t?.terminate(),Cv().then(([n,o])=>{try{_t=o,_t.onerror=l=>t(l),_t.onmessage=Sy,lu=[e,t];let u={type:"init-wasm",in:pe};!u.in.wasm.wasmPaths&&(n||rl)&&(u.in.wasm.wasmPaths={wasm:new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href}),_t.postMessage(u),fo=n}catch(u){t(u)}},t)});try{await kl(pe.wasm),await Wl(pe),ei=!0}catch(e){throw ti=!0,e}finally{hn=!1}}},B2=async e=>{if(lr())return Dr(),new Promise((t,n)=>{Cr("init-ep",[t,n]);let o={type:"init-ep",in:{epName:e,env:pe}};_t.postMessage(o)});await Kl(pe,e)},F2=async e=>lr()?(Dr(),new Promise((t,n)=>{Cr("copy-from",[t,n]);let o={type:"copy-from",in:{buffer:e}};_t.postMessage(o,[e.buffer])})):Po(e),L2=async(e,t)=>{if(lr()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Dr(),new Promise((n,o)=>{Cr("create",[n,o]);let u={type:"create",in:{model:e,options:{...t}}},l=[];e instanceof Uint8Array&&l.push(e.buffer),_t.postMessage(u,l)})}else return Xl(e,t)},j2=async e=>{if(lr())return Dr(),new Promise((t,n)=>{Cr("release",[t,n]);let o={type:"release",in:e};_t.postMessage(o)});Zl(e)},U2=async(e,t,n,o,u,l)=>{if(lr()){if(n.some(d=>d[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(u.some(d=>d))throw new Error("pre-allocated output tensor is not supported for proxy.");return Dr(),new Promise((d,c)=>{Cr("run",[d,c]);let s=n,r={type:"run",in:{sessionId:e,inputIndices:t,inputs:s,outputIndices:o,options:l}};_t.postMessage(r,Ql(s))})}else return Yl(e,t,n,o,u,l)},V2=async e=>{if(lr())return Dr(),new Promise((t,n)=>{Cr("end-profiling",[t,n]);let o={type:"end-profiling",in:e};_t.postMessage(o)});Jl(e)}}),du,Iy,G2,FA=D(()=>{"use strict";it(),q2(),ae(),El(),zv(),du=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},Iy=e=>{switch(e[3]){case"cpu":return new ft(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Cl(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:n,download:o,dispose:u}=e[2];return ft.fromGpuBuffer(n,{dataType:t,dims:e[1],download:o,dispose:u})}case"ml-tensor":{let t=e[0];if(!Dl(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:n,download:o,dispose:u}=e[2];return ft.fromMLTensor(n,{dataType:t,dims:e[1],download:o,dispose:u})}default:throw new Error(`invalid data location: ${e[3]}`)}},G2=class{async fetchModelAndCopyToWasmMemory(e){return F2(await Rl(e))}async loadModel(e,t){jt();let n;typeof e=="string"?n=await this.fetchModelAndCopyToWasmMemory(e):n=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await L2(n,t),Dt()}async dispose(){return j2(this.sessionId)}async run(e,t,n){jt();let o=[],u=[];Object.entries(e).forEach(a=>{let p=a[0],h=a[1],m=this.inputNames.indexOf(p);if(m===-1)throw new Error(`invalid input '${p}'`);o.push(h),u.push(m)});let l=[],d=[];Object.entries(t).forEach(a=>{let p=a[0],h=a[1],m=this.outputNames.indexOf(p);if(m===-1)throw new Error(`invalid output '${p}'`);l.push(h),d.push(m)});let c=o.map((a,p)=>du(a,()=>`input "${this.inputNames[u[p]]}"`)),s=l.map((a,p)=>a?du(a,()=>`output "${this.outputNames[d[p]]}"`):null),r=await U2(this.sessionId,u,c,d,s,n),i={};for(let a=0;agl,initializeFlags:()=>ml,wasmBackend:()=>W2});var ml,gl,W2,LA=D(()=>{"use strict";it(),q2(),FA(),ml=()=>{(typeof pe.wasm.initTimeout!="number"||pe.wasm.initTimeout<0)&&(pe.wasm.initTimeout=0);let e=pe.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),pe.wasm.simd=!1),typeof pe.wasm.proxy!="boolean"&&(pe.wasm.proxy=!1),typeof pe.wasm.trace!="boolean"&&(pe.wasm.trace=!1),typeof pe.wasm.numThreads!="number"||!Number.isInteger(pe.wasm.numThreads)||pe.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)pe.wasm.numThreads=1;else{let t=typeof navigator>"u"?cu("node:os").cpus().length:navigator.hardwareConcurrency;pe.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},gl=class{async init(e){ml(),await M2(),await B2(e)}async createInferenceSessionHandler(e,t){let n=new G2;return await n.loadModel(e,t),n}},W2=new gl});it();it();it();var jA="1.29.0";{let e=(FE(),wn(Sv)).onnxjsBackend;Zr("webgl",e,-10)}{let e=(LA(),wn(H2)).wasmBackend;Zr("webgpu",e,5),Zr("webnn",e,5),Zr("cpu",e,10),Zr("wasm",e,10)}Object.defineProperty(pe.versions,"web",{value:jA,enumerable:!0});function K2(){return pe.versions.web??""}function X2(e={}){pe.wasm.numThreads=e.wasmThreads??1,e.wasmPaths!==void 0&&(pe.wasm.wasmPaths=e.wasmPaths)}var jo=[];function ed(){let e=jo;return jo=[],e}async function Uo(e,t,n){for(let o of t){let u=null;try{if(u=await Do.create(e.slice().buffer,{executionProviders:[o]}),await n(u))return{session:u,ep:o};jo.push(`${o}: the model does not have the inputs and outputs the release describes`)}catch(l){jo.push(`${o}: ${l instanceof Error?l.message:String(l)}`.slice(0,300))}await u?.release().catch(()=>{})}return null}function En(e,t){return new ft("float32",e,t)}function td(e){return{cx:e.x+e.width/2,cy:e.y+e.height/2,w:e.width,h:e.height,angle:0}}function An(e,t){let n=Math.max(e.x,t.x),o=Math.max(e.y,t.y),u=Math.min(e.x+e.width,t.x+t.width),l=Math.min(e.y+e.height,t.y+t.height);if(u<=n||l<=o)return 0;let d=(u-n)*(l-o);return d/(e.width*e.height+t.width*t.height-d)}function rd(e,t,n,o){let u=[...e].sort((d,c)=>n(c)-n(d)),l=[];for(let d of u){let c=t(d);l.every(s=>An(c,t(s))O&&(O=P,S=A)}O>=l&&(c.push(_),s.push(O),r.push(S))}let i=c.length;if(i===0)return[];let a=new Float32Array(i),p=new Float32Array(i),h=new Float32Array(i),m=new Float32Array(i);for(let _=0;_y).sort((_,y)=>s[y]-s[_]||_-y),$=[];for(;b.length>0;){let _=b[0];if($.push(_),b.length===1)break;let y=Ke(Ke(h[_]-a[_])*Ke(m[_]-p[_])),T=[];for(let S=1;S{let y={x:a[_]/n,y:p[_]/n,width:Ke(h[_]-a[_])/n,height:Ke(m[_]-p[_])/n};return{aabb:y,rect:td(y),score:s[_],cls:r[_]}})}function Vo(e,t,n,o=kn){let u=Math.trunc(Math.max(0,e.x)),l=Math.trunc(Math.max(0,e.y)),d=Math.trunc(Math.min(t,e.x+e.width)),c=Math.trunc(Math.min(n,e.y+e.height));return d-u>=o&&c-l>=o}function Y2(e,t,n,o,u,l=nd,d=kn){return Z2(e,t,n,l).filter(c=>Vo(c.aabb,o,u,d))}function qA(e,t,n,o,u){let l=o/t,d=u/n;return[Math.max(0,e.x*l),Math.max(0,e.y*d),Math.min(o,(e.x+e.width)*l),Math.min(u,(e.y+e.height)*d)]}function J2(e,t){let n=e.aabb;if(n.x>=0&&n.y>=0&&n.x+n.width<=t.width&&n.y+n.height<=t.height)return e;let[o,u,l,d]=qA(n,t.width,t.height,t.width,t.height),c={x:o,y:u,width:Math.max(0,l-o),height:Math.max(0,d-u)};return{...e,aabb:c,rect:td(c)}}function Q2(e,t=vr){let n=[],o=[];for(let u of e)(u.cls===t?n:o).push(u);return{faces:n,others:o}}function GA(e){return e<0&&(e=-e),e<1?1-e:0}function e$(e,t){let n=e/t,o=n<1?1:n,u=o,l=Math.ceil(u)*2+1,d=new Int32Array(t*2),c=new Int32Array(t*l),s=new Float64Array(l);for(let r=0;re&&(m=e),m-=h;for(let b=0;b>22;return t<0?0:t>255?255:t}var t$="",id=null,od=null;function HA(e,t,n,o){let u=`${e}x${t}>${n}x${o}`;return(u!==t$||!id||!od)&&(id=e$(e,n),od=e$(t,o),t$=u),{h:id,v:od}}function Go(e,t,n,o,u){let{h:l,v:d}=HA(t,n,o,u),c=o!==t,s=u!==n,r,i;if(c){let _=s?d.bounds[0]:0,T=(s?d.bounds[u*2-2]+d.bounds[u*2-1]:n)-_;i=_,r=new Uint8Array(o*T*3);let{ksize:S,bounds:O,kk:A}=l;for(let P=0;P.5?t+1:n<.5||t%2===0?t:t+1}function sd(e,t,n=Ho){let o=Math.min(n.height/t,n.width/e);return{ratio:o,width:r$(e*o),height:r$(t*o)}}function WA(e,t,n,o=Ho){let u=sd(t,n,o),l=Go(e,t,n,u.width,u.height),d=new Uint8Array(o.width*o.height*3).fill(o.pad);for(let c=0;c{let i=(await s.run({[n.inputName]:En(new Float32Array(3*u*l),[1,3,l,u])}))[n.outputName]?.dims??[];return i.length===3&&i[0]===1&&i[1]===n.anchors&&i[2]===d});return c?new e(c.session,c.ep,n):null}async propose(t){let{letterbox:n,inputName:o,outputName:u,decode:l,minCropPx:d}=this.settings,c=performance.now(),{tensor:s,ratio:r}=ad(t.data,t.width,t.height,n),i=performance.now(),a=await this.session.run({[o]:En(s,[1,3,n.height,n.width])});return this.lastTimings={preprocessMs:i-c,inferMs:performance.now()-i},Y2(a[u].data,this.columns,r,t.width,t.height,l,d)}async release(){await this.session.release().catch(()=>{})}};var KA=32;async function XA(e,t,n,o,u=KA){if(n<=u)return e(t,n);let l=t.length/n,d=new Float32Array(n*o);for(let c=0;c(await d.run({[n.inputName]:En(new Float32Array(3*u*u),[1,3,u,u])}))[n.outputName]?.data.length===n.dim);return l?new e(l.session,l.ep,n):null}async embedBatch(t,n){return XA((o,u)=>this.run(o,u),t,n,this.settings.dim)}async run(t,n){let{inputName:o,outputName:u,inputSize:l,dim:d}=this.settings,s=(await this.session.run({[o]:En(t,[n,3,l,l])}))[u].data;if(s.length!==n*d)throw new Error(`unexpected embedding size ${s.length} for ${n} crops`);return s}async release(){await this.session.release().catch(()=>{})}};var ZA=["orb","akaze","sift"];var YA={orb:[["scaleFactor",1.2,!1],["nLevels",8,!0],["edgeThreshold",15,!0],["patchSize",15,!0],["fastThreshold",10,!0]],akaze:[["threshold",.001,!1],["nOctaves",4,!0],["nOctaveLayers",4,!0]],sift:[["nOctaveLayers",3,!0],["contrastThreshold",.04,!1],["edgeThreshold",10,!1],["sigma",1.6,!1]]},VC=Object.fromEntries(ZA.map(e=>[e,Object.fromEntries(YA[e].map(([t,n])=>[t,n]))])),qC=Object.freeze({seed:0,features:{type:"orb",maxFeatures:300,artLongSide:320,mask:[],params:{}},crop:{longSide:320,maxFeatures:500},candidates:{embeddingTopK:5,voteTopN:0},vote:{matcher:"bf",ratio:.8,lshTables:12,lshKeySize:20,lshProbes:2,kdTrees:4,checks:64},match:{ratio:.8},ransac:{model:"homography",method:"ransac",threshold:6,maxIters:2e3,confidence:.995,minAreaRatio:.25,maxAreaRatio:4},fusion:{weight:.1,minInliers:8,fullInliers:30,penalty:0,baseline:"none",reorder:!0},gate:{belowAccept:!1,sources:null}}),JA=["features","crop","candidates","vote","match","ransac","fusion","gate"],GC=["index","seed",...JA];var WC=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]);function n$(e,t,n){let o=new Float64Array(e);for(let u of t){let l=u.fused-u.cosine;for(let d of n(u.cardId))o[d]=o[d]+l}return o}var QA=10n;function ek(e){let t=new DataView(new ArrayBuffer(8));t.setFloat64(0,e);let n=t.getUint32(0),o=t.getUint32(4),u=n>>>31?-1n:1n,l=n>>>20&2047,d=BigInt(n&1048575)<<32n|BigInt(o),c;return l===0?c=-1074:(d|=1n<<52n,c=l-1075),{mantissa:u*d,exponent:c}}function Si(e,t){if(!Number.isFinite(e)||e===0)return e;let{mantissa:n,exponent:o}=ek(e),u=n<0n,d=(u?-n:n)*QA**BigInt(t),c=1n;o>=0?d<<=BigInt(o):c<<=BigInt(-o);let s=d/c,i=2n*(d-s*c);if((i>c||i===c&&s%2n===1n)&&(s+=1n),s===0n)return u?-0:0;let a=`${u?"-":""}${s.toString()}e-${t}`;return Number(a)}function tk(e){return{cardId:e.cardId,cosine:Si(e.cosine,6),foundBy:e.foundBy,votes:e.votes,matches:e.matches,inliers:e.inliers,evidence:Si(e.evidence,6),fused:Si(e.fused,6)}}function i$(e,t){return{before:{topId:e.topId,similarity:Si(e.similarity,6),margin:Si(e.margin,6),accepted:e.accepted},candidates:t.candidates.map(tk),...t.debug?{debug:t.debug}:{}}}var rk=23,YC=(1<!n.representative.has(d))??[...u].sort()[0];for(let d of u)o.set(d,`group:${n.representative.get(d)??l}`)}return o}var Pn="file:///";function s$(e){let t=new URL(e,Pn).href.slice(Pn.length);try{return decodeURIComponent(t)}catch{return t}}var a$="busy";var Vt={full128:{id:"full128",size:128,centerMask:!1,poolVersion:2,crop:"truncate"}};function ud(e){return typeof e=="string"&&Object.prototype.hasOwnProperty.call(Vt,e)}function Wo(e){if(typeof e!="object"||e===null||Array.isArray(e))return null;let t=e;if(!ud(t.id))return null;let n=Vt[t.id],o=Object.keys(t);return o.length===Object.keys(n).length&&o.every(l=>n[l]===t[l])?t.id:null}function l$(e,t,n,o,u,l){let d=new Float64Array(o*u*3),c=new Uint32Array(o*u);for(let a=0;a0&&(d[a*3]=d[a*3]/p,d[a*3+1]=d[a*3+1]/p,d[a*3+2]=d[a*3+2]/p)}if(l===1||t>=o&&n>=u||t<=0||n<=0)return d;let s=u$(n,u),r=u$(t,o),i=new Float64Array(d.length);for(let a=0;a=0&&!n[l];)l--;let d=u+1;for(;d=0&&(d>=t||u-l<=d-u)?l:d}return o}function ld(e,t,n,o){let u=o.size,l=l$(e,t,n,u,u,o.poolVersion),d=new Float32Array(3*u*u),c=u*u;for(let s=0;s>10&31,s=l&1023;c===0?o[u]=d*s*2**-24:c===31?o[u]=s?NaN:d*(1/0):o[u]=d*(1+s/1024)*2**(c-15)}return o}function ik(e,t){let n;try{n=atob(e)}catch{return null}if(n.length!==t*2)return null;let o=new Uint8Array(n.length);for(let u=0;u0)||!Number.isFinite(u))return!1;for(let l=0;l=Cn.length&&Cn.every((t,n)=>e[n]===t)}function hd(e){return pd(e)&&e.length>Cn.length?e[Cn.length]:null}function h$(e){let t=e instanceof Uint8Array?e:new Uint8Array(e);if(hd(t)!==Ko||t.lengthtypeof a=="string")||u.entryCount!==d.length)return null;let c=u.artVs;if(c!==void 0&&(!Array.isArray(c)||c.length!==d.length||!c.every(a=>a===null||typeof a=="string")))return null;let s=l.dim,r=Ii+n;if(t.length-r!==d.length*s*2)return null;let i=new Float32Array(d.length*s);for(let a=0;a({x:o+p*s-h*r,y:u+p*r+h*s}))}}function f$(e,t){let{points:n}=t,o=0;for(let u=0;u0?1:-1;if(o===0)o=p;else if(p!==o)return!1}return!0}function md(e,t){let n=null;for(let o of t)f$(e,o.quad)&&(!n||o.zIndex>=n.zIndex)&&(n=o);return n?n.card:null}function Dn(e){return e<0?0:e>1?1:e}function Xo(e,t){let{frameWidth:n,frameHeight:o,names:u}=t;return e.map(l=>{let{aabb:d,rect:c}=l,s={instanceId:l.instanceId,bounds:{x:Dn(d.x/n),y:Dn(d.y/o),width:Dn(d.width/n),height:Dn(d.height/o)},rotation:(c.angle+45)%90-45,localWidth:Dn(c.w/n),localHeight:Dn(c.h/o),landscape:c.w>c.h,cardId:null,name:null};if(l.identity){let r=u.get(l.identity);r!==void 0&&(s.cardId=l.identity,s.name=r)}return s})}var g$=["accepted-nms","best-score","detector-score"];function b$(e){return typeof e=="string"&&g$.includes(e)}var gd="accepted-nms",ak=.5,uk="hover_";function m$(e){return e.width*e.height}function y$(e,t){let n=t==="accepted-nms"?e.kept:e.proposals.map((u,l)=>l),o=(u,l)=>{let d=e.proposals[u],c=e.proposals[l];if(t==="detector-score")return d.detectorScore!==c.detectorScore?d.detectorScore{let u=e.proposals[n];return{proposal:n,aabb:u.aabb,rect:u.rect,identity:u.accepted?u.displayId:null,score:u.score,zIndex:o+1}})}var Zo=class{prev=[];nextId=1;reset(){this.prev=[]}assign(t){let n=new Set,o=t.map((l,d)=>d).sort((l,d)=>t[d].zIndex-t[l].zIndex),u=new Array(t.length);for(let l of o){let d=t[l],c=-1,s=ak;for(let r=0;r=s&&(c<0||i>s)&&(c=r,s=i)}c>=0?(n.add(c),u[l]=this.prev[c].id):u[l]=`${uk}${String(this.nextId++).padStart(4,"0")}`}return this.prev=t.map((l,d)=>({id:u[d],aabb:l.aabb,identity:l.identity})),u}};function Yo(e,t,n,o=u=>u){return Xo(e.map((l,d)=>({instanceId:t[d],aabb:l.aabb,rect:l.rect,identity:l.identity})),n).map((l,d)=>({...l,zIndex:e[d].zIndex,boxIndex:o(e[d].proposal)}))}var lk={card:null,boxIndex:null,box:null,cardId:null,name:null};function bd(e,t,n){let o=e.map(l=>({card:l,quad:fd(l,n),zIndex:l.zIndex??0})),u=md(t,o);return u?{card:u,boxIndex:u.boxIndex??null,box:{x:u.bounds.x*n.width,y:u.bounds.y*n.height,width:u.bounds.width*n.width,height:u.bounds.height*n.height},cardId:u.cardId,name:u.name}:lk}var dk="identity-groups";function yd(e){if(typeof e!="object"||e===null)return null;let t=e;if(t.schemaVersion!==1||t.kind!==dk||typeof t.version!="number"||!Number.isInteger(t.version)||!Array.isArray(t.groups)||typeof t.representative!="object"||t.representative===null||Array.isArray(t.representative))return null;let n=new Map,o=[];for(let l of t.groups){if(!Array.isArray(l)||l.length<2)return null;let d=[];for(let c of l){if(typeof c!="string"||c.length===0||n.has(c))return null;n.set(c,o.length),d.push(c)}o.push(d)}let u=new Map;for(let[l,d]of Object.entries(t.representative)){if(typeof d!="string"||l===d)return null;let c=n.get(l);if(c===void 0||n.get(d)!==c)return null;u.set(l,d)}for(let l of o){let d=l.filter(c=>!u.has(c));if(d.length!==1||l.some(c=>u.has(c)&&u.get(c)!==d[0]))return null}return{version:t.version,groups:o,representative:u}}var ck=["name","printing"];function _d(e){return ck.includes(e)}function w$(e,t){return t==="printing"?e.printingOf??e.id:e.name}function v$(e,t){let n=new Map;for(let o of[...new Set(e)].sort(_$)){let u=t.get(o);if(u===void 0)continue;let l=n.get(u);l?l.push(o):n.set(u,[o])}return[...n.values()].filter(o=>o.length>=2).sort((o,u)=>_$(o[0],u[0]))}function _$(e,t){return et?1:0}var ln=class{rep=new Map;display;constructor(t=null,n,o="representative"){if(this.display=o,!!t)for(let[u,l]of t.representative)o==="representative"&&n&&!n(l)||this.rep.set(u,l)}of(t){return this.rep.get(t)??t}shown(t){return this.display==="best-printing"?t:this.of(t)}get collapsedCount(){return this.rep.size}};var Jo=["webgpu","wasm"];function wd(e,t=e.hover.selection){let n=e.boxes.flatMap((s,r)=>s.face!==null?[r]:[]),o=n.map(s=>e.boxes[s]),u=new Map,l=o.map(s=>{let r=s.face;r.cardId!==null&&r.name!==null&&u.set(r.cardId,r.name);let i={x:s.box[0],y:s.box[1],width:s.box[2],height:s.box[3]};return{aabb:i,rect:{cx:i.x+i.width/2,cy:i.y+i.height/2,w:i.width,h:i.height,angle:0},score:r.similarity,detectorScore:s.score,accepted:r.cardId!==null,displayId:r.cardId??r.topId}}),d=o.flatMap((s,r)=>s.face.kept?[r]:[]),c=Oi({proposals:l,kept:d},t);return Yo(c,c.map((s,r)=>`${t}_${r}`),{frameWidth:e.frame.width,frameHeight:e.frame.height,names:u},s=>n[s])}function Qo(e,t,n,o){let u=o===void 0||o===e.hover.selection?e.hover.cards:wd(e,o);return bd(u,{x:t,y:n},e.frame)}var pk=.3;function T$(e,t){let n=Math.max(0,Math.floor(t.x)),o=Math.max(0,Math.floor(t.y)),u=Math.min(e.width,Math.floor(t.x+t.width)),l=Math.min(e.height,Math.floor(t.y+t.height)),d=Math.max(0,u-n),c=Math.max(0,l-o),s=new Uint8ClampedArray(d*c*4);for(let r=0;r=t.tau&&e.margin>=t.delta}function S$(e,t,n){if(dn(e,n)||!n.rotationRetry)return{orientation:0,match:e,retried:!1};let o=[e,...t()],u=-1;for(let l=0;lo[u].similarity)&&(u=l);if(u<0){u=0;for(let l=1;lo[u].similarity&&(u=l)}return{orientation:u,match:o[u],retried:!0}}function x$(e){let t=[],n=[];return e.forEach((o,u)=>{o.tauPassed&&(o.orientation===0?t:n).push(u)}),[...t,...n]}function $$(e,t){let n=(t??e.map((o,u)=>u)).filter(o=>e[o].tauPassed);return rd(n,o=>e[o].aabb,o=>e[o].score,pk)}async function vd(e,t,n,o,u=kn,l=null){let d=n.preprocessing,c=n.rule,s=d.size,r=n.dim,i=[],a=[],p=[];for(let w of o){let C=T$(e,w.aabb),{rgba:U,width:H,height:G}=C;Hn.matchScored(b,C*r)),_=[];if(c.rotationRetry)for(let w=0;w[w,C])),T=null;if(_.length>0){let w=new Float32Array(_.length*3*h);for(let C=0;C<_.length;C++){let U=i[_[C]];for(let H=1;H<=3;H++)w.set(dd(U,H,s),(C*3+H-1)*h)}T=await t.embedBatch(w,_.length*3)}let S=(w,C)=>{let U=y.get(w);if(U===void 0||T===null)throw new Error(`the turned looks of proposal ${w} were not embedded`);return(U*3+C-1)*r},O=a.map((w,C)=>S$($[C],()=>[1,2,3].map(U=>n.matchScored(T,S(C,U))),c)),A=w=>O[w].orientation===0?{embs:b,off:w*r}:{embs:T,off:S(w,O[w].orientation)},P=new Map,I,L,V=!1;if(l)try{let w=performance.now(),C=[],U=[],H=[];for(let G=0;G0){let G=await l.verifyBatch(H,n.bankIds);C.forEach((J,B)=>{let X=G[B],K=U[B],M=O[J].match,ne=n$(K,X.candidates,Oe=>n.columnsOf(Oe)),Q=l.reorder?n.lookOf(ne):n.lookWithin(ne,n.lookOf(K).row),{row:le,...Ae}=Q;P.set(J,{match:Ae,geometric:i$({topId:M.topId,similarity:M.similarity,margin:M.margin,accepted:dn(M,c)},X)})})}I=performance.now()-w}catch(w){V=!0,L=w,P.clear(),I=void 0}let Z=w=>{let C=[];for(let U=0;Uo.shown(l));let u=new Map;this.rowIdent=new Int32Array(t.ids.length);for(let l=0;lc&&(c=p,s=a)}let r=es,i=s>=0?this.rowIdent[s]:-1;for(let a=0;ar&&(r=d[a]);return{bestRow:s,best:c,other:r}}matchScored(t,n){let{bestRow:o,best:u,other:l}=this.topAndOther(t,n),d=this.bank.ids[o];return{cardId:u>=this.rule.tau?d:null,similarity:u,topId:d,identity:this.identRep[this.rowIdent[o]],displayId:this.rowShown[o],margin:u-l}}scoreRow(t,n){let{matrix:o,dim:u,ids:l}=this.bank,d=new Float64Array(l.length);for(let c=0;cd);n.sort((l,d)=>t[d]-t[l]);let o=new Set,u=[];for(let l of n){let d=this.bank.ids[l];o.has(d)||(o.add(d),u.push({cardId:d,cosine:t[l]}))}return u}columns=null;columnsOf(t){return this.columns===null&&(this.columns=new Map,this.bank.ids.forEach((n,o)=>{let u=this.columns.get(n);u?u.push(o):this.columns.set(n,[o])})),this.columns.get(t)??[]}bankIdSet=null;get bankIds(){return this.columnsOf(""),this.bankIdSet??=new Set(this.columns.keys()),this.bankIdSet}scored(t,n,o){let u=this.bank.ids[t];return{cardId:n>=this.rule.tau?u:null,similarity:n,topId:u,identity:this.identRep[this.rowIdent[t]],displayId:this.rowShown[t],margin:n-o,row:t}}lookOf(t){let n=0;for(let d=1;dt[n]&&(n=d);let o=this.rowIdent[n],u=es,l=!1;for(let d=0;du)&&(u=t[d]),l=!0);return this.scored(n,t[n],u)}lookWithin(t,n){let o=this.rowIdent[n],u=-1,l=es,d=!1;for(let c=0;ct[u])&&(u=c):((!d||t[c]>l)&&(l=t[c]),d=!0);return this.scored(u,t[u],l)}matchPooled(t,n=this.rule.delta){let{bestRow:o,best:u,other:l}=this.topAndOther(t,0),d=u-l;return{cardId:u>=this.rule.tau&&d>=n?this.rowShown[o]:null,similarity:u,margin:d}}};var Ai=class{constructor(t=3){this.k=t;if(!Number.isInteger(t)||t<1)throw new RangeError(`k must be a positive integer, got ${t}`)}k;run=0;misses=0;showing=!1;reset(){this.run=0,this.misses=0,this.showing=!1}observe(t){return t?(this.run+=1,this.misses=0,this.run>=this.k&&(this.showing=!0)):(this.run=0,this.misses+=1,this.misses>=this.k&&(this.showing=!1)),{confirmed:this.showing,consecutive:this.run}}};var hk=.4,fk=2,mk=5,gk=2,bk="track_";function I$(e){let t=e[0].length,n=new Float32Array(t);for(let l of e)for(let d=0;d0))return null;for(let l=0;lu));for(let o of this.tracks){let u=-1,l=hk;for(let d of n){let c=An(o.detection.aabb,t[d].aabb);c>l&&(l=c,u=d)}if(u>=0){let d=t[u];o.detection=d,o.misses=0,o.age+=1,o.embeddings.push(d.embedding),o.embeddings.length>mk&&o.embeddings.shift(),n.delete(u)}else o.misses+=1}this.tracks=this.tracks.filter(o=>o.misses<=fk);for(let o of n){let u=t[o];this.tracks.push({instanceId:`${bk}${String(this.nextId++).padStart(4,"0")}`,detection:u,embeddings:[u.embedding],misses:0,age:1})}return this.tracks.filter(o=>o.misses===0).map(o=>({instanceId:o.instanceId,detection:o.detection,identity:o.age>=gk?this.identityOf(o):null}))}};var yk=.25,_k=5;function wk(e,t){if(e===null||t===void 0)return!1;let n=t-e;return n<-yk||n>_k}function vk(e){return[e.x,e.y,e.width,e.height]}var Pi=class{hoverSelection;detector;embedder;recognizer;names;classNames;faceClass;minCropPx;now;tracker;gate=new Ai(3);hoverIds=new Zo;heldCards=[];lastVideoTimeSec=null;geometric;geometricFailure=null;constructor(t){this.detector=t.detector,this.embedder=t.embedder,this.names=t.names,this.classNames=t.classNames??["face","back"],this.faceClass=t.faceClass??vr,this.minCropPx=t.minCropPx??kn,this.hoverSelection=t.hoverSelection??gd,this.now=t.now??(()=>performance.now()),this.geometric=t.geometric??null;let n=new ln(t.identityGroups??null,o=>this.names.has(o),t.identityDisplay??"best-printing");this.recognizer=new Ei(t.bank,t.accept??ts,n),this.tracker=new ki(o=>this.recognizer.matchPooled(o).cardId)}get size(){return this.recognizer.size}takeGeometricFailure(){let t=this.geometricFailure;return this.geometricFailure=null,t}get geometricStage(){return this.geometric}setGeometric(t){this.geometric=t,this.geometricFailure=null}reset(){this.tracker.reset(),this.gate.reset(),this.heldCards=[],this.hoverIds.reset(),this.lastVideoTimeSec=null}async analyze(t,n){let o=this.now(),u=wk(this.lastVideoTimeSec,n);u&&this.reset(),n!==void 0&&(this.lastVideoTimeSec=n);let l=await this.detector.propose(t),d=this.now()-o,c=l.filter(A=>Vo(A.aabb,t.width,t.height,this.minCropPx)).map(A=>J2(A,t)),{faces:s}=Q2(c,this.faceClass),r=this.now(),i=await vd(t,this.embedder,this.recognizer,s,this.minCropPx,this.geometric),a=this.now()-r;if(i.geometricError!==void 0&&this.geometric&&(this.geometricFailure={stage:this.geometric,error:i.geometricError},this.geometric=null),i.proposals.length!==s.length)throw new Error("pipeline: a valid face box was not recognized");let p={frameWidth:t.width,frameHeight:t.height,names:this.names},h=Oi(i,this.hoverSelection),m=s.map(A=>c.indexOf(A)),b=Yo(h,this.hoverIds.assign(h),p,A=>m[A]),$=i.base??i,_=$.kept.map(A=>{let P=$.proposals[A];return{aabb:P.aabb,rect:P.rect,cardId:P.topId,embedding:P.embedding}}),y=this.tracker.update(_),T=_.length>0,S=this.gate.observe(T),O;return S.confirmed?T?(O=Xo(y.map(A=>({instanceId:A.instanceId,aabb:A.detection.aabb,rect:A.detection.rect,identity:A.identity})),p),this.heldCards=O):O=this.heldCards:(this.heldCards=[],O=[]),{frame:{width:t.width,height:t.height},boxes:this.boxResults(c,s,i),hover:{selection:this.hoverSelection,cards:b},tracks:{shown:S.confirmed,cards:O},discontinuity:u,retried:i.retried,embeddings:i.embeddings,timings:{detectorMs:d,recognizeMs:a,analyzeMs:this.now()-o,...i.geometricMs!==void 0?{geometricMs:i.geometricMs}:{}}}}boxResults(t,n,o){let u=new Map(n.map((d,c)=>[d,c])),l=new Set(o.kept);return t.map(d=>{let c=u.get(d),s=null;if(c!==void 0){let r=o.proposals[c],i=r.accepted&&this.names.has(r.displayId)?r.displayId:null;s={topId:r.topId,topName:this.names.get(r.topId)??null,similarity:r.score,margin:r.margin,look:r.orientation,retried:r.retried,tauPassed:r.tauPassed,accepted:r.accepted,cardId:i,name:i===null?null:this.names.get(i)??null,kept:l.has(c),...r.geometric?{geometric:r.geometric}:{}}}return{box:vk(d.aabb),score:d.score,cls:d.cls,className:this.classNames[d.cls]??String(d.cls),face:s}})}};var O$=1,E$="vision-release",xk=[8,16,32];function A$(e,t){return xk.reduce((n,o)=>n+Math.floor(e/o)*Math.floor(t/o),0)}var $k=/^[0-9a-f]{16}$/,Tk=/^[0-9a-f]{64}$/,Sk=/^\d+\.\d+\.\d+$/,Ik=["schemaVersion","kind","id","game","locale","detector","recognizer","bank","identity","catalog","accept","hover","runtime"],k$=["tau","delta","rotationRetry"],Fe=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),ut=e=>typeof e=="number"&&Number.isInteger(e)&&e>0,Ni=e=>typeof e=="number"&&Number.isFinite(e),Ze=e=>typeof e=="string"&&e.trim()!=="";function Ok(e){return!Ze(e)||e.includes("\\")||/^[A-Za-z][A-Za-z0-9+.-]*:/.test(e)||/[\x00-\x1f\x7f]/.test(e)||/^ | $/.test(e)||e.startsWith("/")?!1:!e.split("/").some(t=>{let n=t.replace(/%2e/gi,".");return n==="."||n===".."})}function Di(e){return!Fe(e)||!Ok(e.path)||typeof e.sha256!="string"||!Tk.test(e.sha256)||!ut(e.bytes)?null:{path:e.path,sha256:e.sha256,bytes:e.bytes}}function Ek(e){let t=e.input,n=e.decode;if(!Fe(t)||!ut(t.width)||!ut(t.height))return"detector.input must give a width and a height";if(!Fe(n))return"detector.decode must be an object";if(!Ze(n.inputName)||!Ze(n.outputName))return"detector.decode must name the model's input and output";let o=[["layout","NCHW"],["channels","BGR"],["scale","0-255"],["score","obj * max(cls)"],["class","argmax(cls)"]];for(let[i,a]of o)if(n[i]!==a)return`detector.decode.${i} is not ${JSON.stringify(a)}, the contract this runtime implements`;let u=n.letterbox;if(!Fe(u)||u.width!==t.width||u.height!==t.height)return"detector.decode.letterbox differs from detector.input";if(typeof u.pad!="number"||!Number.isInteger(u.pad)||u.pad<0||u.pad>255)return"detector.decode.letterbox.pad must be a whole number from 0 to 255";let l=[["anchor","top-left"],["resize","pillow-bilinear"],["ratio","min(height / frameHeight, width / frameWidth)"],["size","round(frame * ratio)"]];for(let[i,a]of l)if(u[i]!==a)return`detector.decode.letterbox.${i} is not ${JSON.stringify(a)}`;let d=n.classes;if(!Array.isArray(d)||d.length===0||!d.every(Ze)||new Set(d).size!==d.length)return"detector.decode.classes must be a list of distinct class names";if(e.faceClass!==vr||d[vr]!=="face")return`detector.faceClass must be ${vr} and that class must be named "face"`;let c=n.output,s=["cx","cy","w","h","obj",...d.map(i=>`cls_${i}`)];if(!Fe(c)||!Array.isArray(c.columns)||c.columns.length!==s.length||!c.columns.every((i,a)=>i===s[a]))return"detector.decode.output.columns must be cx, cy, w, h, obj and one cls_ per class";if(c.anchors!==A$(t.width,t.height))return`detector.decode.output.anchors must be ${A$(t.width,t.height)} for a ${t.width} x ${t.height} input`;if(!Ni(n.threshold)||!(n.threshold>0&&n.threshold<1))return"detector.decode.threshold must be a number between 0 and 1";if(n.threshold<.01)return`detector.decode.threshold ${n.threshold} is below 0.01, the lowest score at which the detector export checks box parity`;let r=n.nms;return!Fe(r)||!Ni(r.iou)||!(r.iou>0&&r.iou<=1)||r.classAgnostic!==!0||r.greedy!==!0?"detector.decode.nms must give an iou above 0 and at most 1, with classAgnostic and greedy both true":ut(n.maxProposals)?ut(e.minCropPx)?{inputName:n.inputName,outputName:n.outputName,letterbox:{width:t.width,height:t.height,pad:u.pad},anchors:c.anchors,classes:d,decode:{threshold:n.threshold,nmsIou:r.iou,maxProposals:n.maxProposals},faceClass:vr,minCropPx:e.minCropPx}:"detector.minCropPx must be a whole number above 0":"detector.decode.maxProposals must be a whole number above 0"}function C$(e){let t=O=>({error:O});if(!Fe(e)||e.schemaVersion!==O$||e.kind!==E$)return t(`not a "${E$}" document of schema version ${O$}`);let n=Object.keys(e).filter(O=>!Ik.includes(O));if(n.length>0)return t(`unknown key(s) ${n.sort().join(", ")}`);if(typeof e.id!="string"||!$k.test(e.id))return t("id must be 16 lowercase hex digits");if(!Ze(e.game)||!Ze(e.locale))return t("game and locale must be non-empty strings");let o=e.detector;if(!Fe(o))return t("incomplete release: detector missing (a release is all-or-nothing)");let u=Di(o);if(!u||!Ze(o.version)||!Ze(o.weights))return t("detector must pin a file and carry a version and a weights label");if(o.arm!=="yolox")return t(`detector.arm ${JSON.stringify(o.arm)} is not one this runtime implements`);let l=Ek(o);if(typeof l=="string")return t(l);let d=e.recognizer;if(!Fe(d))return t("incomplete release: recognizer missing (a release is all-or-nothing)");let c=Di(d);if(!c||!Ze(d.version)||!Ze(d.weights)||!ut(d.dim))return t("recognizer must pin a file and carry a version, a weights label and a dim");if(!Ze(d.inputName)||!Ze(d.outputName))return t("recognizer must name the model's input and output");let s=Wo(d.preprocessing);if(s===null)return t(`recognizer.preprocessing is not a contract this runtime implements (known: ${Object.keys(Vt).join(", ")})`);let r=e.bank;if(!Fe(r))return t("incomplete release: bank missing (a release is all-or-nothing)");let i=Di(r);if(!i||!ut(r.version)||!ut(r.cardCount)||!ut(r.entryCount))return t("bank must pin a file and carry a version, a cardCount and an entryCount");if(r.aggregation!=="mean")return t('bank.aggregation must be "mean"');let a=e.identity;if(!Fe(a)||!_d(a.level))return t('identity.level must be "name" or "printing"');let p=a.groups,h=Di(p);if(!h||!Fe(p)||!ut(p.version))return t("incomplete release: identity.groups must pin a file and carry a version");if(a.display!=="best-printing"&&a.display!=="representative")return t(`identity.display ${JSON.stringify(a.display)} is not one of best-printing, representative`);let m=e.catalog,b=Di(m);if(!b||!Fe(m)||!ut(m.cardCount))return t("incomplete release: catalog must pin a file and carry a cardCount");let $=e.accept;if(!Fe($))return t("incomplete release: accept missing (a release is all-or-nothing)");if(Object.keys($).some(O=>!k$.includes(O)))return t(`accept has unknown key(s) (expected ${k$.join(", ")})`);if(!Ni($.tau)||!($.tau>0&&$.tau<=1))return t("accept.tau must be a number above 0 and at most 1");if(!Ni($.delta)||!($.delta>=0&&$.delta<1))return t("accept.delta must be a number from 0 to below 1");if(typeof $.rotationRetry!="boolean")return t("accept.rotationRetry must be true or false");let _=e.hover;if(!Fe(_)||!b$(_.selection))return t("hover.selection is not one of accepted-nms, best-score, detector-score");if(!ut(_.maxAgeMs))return t("hover.maxAgeMs must be a whole number above 0");let y=e.runtime;if(!Fe(y)||typeof y.ortWeb!="string"||!Sk.test(y.ortWeb))return t("runtime.ortWeb must be an exact version of the form 1.2.3");if(y.wasmThreads!==1)return t("runtime.wasmThreads must be 1");let T=y.executionProviders;if(!Array.isArray(T)||T.length===0||new Set(T).size!==T.length||!T.every(O=>Jo.includes(O)))return t(`runtime.executionProviders must list one or more of ${Jo.join(", ")}, each once`);let S=y.analysisWidth;return!Fe(S)||!ut(S.min)||!ut(S.max)||S.min>S.max?t("runtime.analysisWidth must give whole numbers min <= max above 0"):{release:{id:e.id,game:e.game,locale:e.locale,detector:{...u,arm:"yolox",version:o.version,weights:o.weights,settings:l},recognizer:{...c,version:d.version,weights:d.weights,dim:d.dim,inputName:d.inputName,outputName:d.outputName,preprocessing:s},bank:{...i,version:r.version,cardCount:r.cardCount,entryCount:r.entryCount},identity:{level:a.level,groups:{...h,version:p.version},display:a.display},catalog:{...b,cardCount:m.cardCount},accept:{tau:$.tau,delta:$.delta,rotationRetry:$.rotationRetry},hover:{selection:_.selection,maxAgeMs:_.maxAgeMs},runtime:{ortWeb:y.ortWeb,executionProviders:T,wasmThreads:1,analysisWidth:{min:S.min,max:S.max}}}}}function Ak(e){let t=0,n=()=>{throw new Error("not JSON")},o=()=>{for(;t{let c=t;for(e[t]!=='"'&&n(),t++;t=e.length&&n(),t++,JSON.parse(e.slice(c,t))},l=c=>{o();let s=e[t];if(s==="{"){t++;let i=[];if(o(),e[t]==="}")t++;else for(;;){o();let a=u();o(),e[t]!==":"&&n(),t++;let p=l(!1);if(c&&a==="id"||i.push([a,p]),o(),e[t]===",")t++;else if(e[t]==="}"){t++;break}else n()}return i.sort((a,p)=>a[0]p[0]?1:0),`{${i.map(([a,p])=>`${JSON.stringify(a)}:${p}`).join(",")}}`}if(s==="["){t++;let i=[];if(o(),e[t]==="]")t++;else for(;;)if(i.push(l(!1)),o(),e[t]===",")t++;else if(e[t]==="]"){t++;break}else n();return`[${i.join(",")}]`}if(s==='"')return JSON.stringify(u());let r=/^(?:true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/.exec(e.slice(t,t+64));return r?(t+=r[0].length,r[0]):n()};o(),e[t]!=="{"&&n();let d=l(!0);return o(),t!==e.length&&n(),d}async function kk(e,t){let n=new TextEncoder().encode(Ak(e));return(await t(n.buffer)).slice(0,16)}async function Pk(e){let t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t),n=>n.toString(16).padStart(2,"0")).join("")}var xd={fetchFn:e=>fetch(e),digestSha256Hex:Pk};function Ck(e,t){let n=l=>/^(\d+)\.(\d+)\.\d+/.exec(l),o=n(e),u=n(t);return o!==null&&u!==null&&o[1]===u[1]&&o[2]===u[2]}var qe=class extends Error{};async function D$(e,t,n){let o;try{o=await e.fetchFn(t)}catch(u){throw new qe(`${n}: fetch failed (${String(u)})`)}if(!o.ok)throw new qe(`${n}: HTTP ${o.status}`);return o.arrayBuffer()}async function Ri(e,t,n,o){let u=await D$(e,new URL(n.path,t).href,o);if(u.byteLength!==n.bytes)throw new qe(`${o}: ${n.path} is ${u.byteLength} bytes, the release says ${n.bytes}`);if(await e.digestSha256Hex(u)!==n.sha256)throw new qe(`${o}: sha256 of ${n.path} does not match the release`);return u}function rs(e,t){try{return JSON.parse(new TextDecoder("utf-8",{fatal:!0}).decode(e))}catch{throw new qe(`${t}: the file is not JSON`)}}var Dk=/^[a-z][a-zA-Z0-9]*$/,Rk=["key","label","values","type"],P$=e=>typeof e=="string"?"text":"number",Jt=e=>JSON.stringify(e)??String(e);function R$(e,t,n){for(let o=0;o!Rk.includes(r));if(l.length>0)return`${u}: unknown key(s) ${l.join(", ")}`;if(typeof o.key!="string"||!Dk.test(o.key))return`${u}: "key" ${Jt(o.key)} must be a lowercase letter, then letters and digits only`;let d=o.key;if(d==="kind")return`${u}: "key" "kind" is reserved: the card's kind is always the first filter`;if(t.some(r=>r.key===d))return`${u}: duplicate key ${Jt(d)}`;if(!Ze(o.label))return`${u} (${d}): "label" must be a non-empty string`;let c=null;if("type"in o){if(o.type!=="text"&&o.type!=="number")return`${u} (${d}): "type" must be "text" or "number", not ${Jt(o.type)}`;c=o.type}let s=null;if("values"in o){if(!Array.isArray(o.values)||o.values.length===0)return`${u} (${d}): "values" must be a non-empty list`;let r=R$(o.values,c,`${u} (${d}) values`);if(r!=="text"&&r!=="number")return r;c=r,s=o.values}t.push({key:d,label:o.label,type:c,values:s})}return t}function zk(e,t,n,o){let u=`card ${n}`;if(!Fe(e))return`${u}: "facets" must be an object of facet key to a value or a list of values`;for(let[l,d]of Object.entries(e)){let c=t.find(i=>i.key===l);if(c===void 0)return`${u}: facets.${l}: the catalog declares no facet ${Jt(l)}`;let s=Array.isArray(d)?d:[d];if(s.length===0)return`${u}: facets.${l} is an empty list (leave the key out instead)`;let r=R$(s,c.type,`${u}: facets.${l}`);if(r!=="text"&&r!=="number")return r;c.type=r;for(let i of s){if(c.values!==null&&!c.values.includes(i))return`${u}: facets.${l}: ${Jt(i)} is not one of the declared values`;let a=o.get(l)??[];a.includes(i)||a.push(i),o.set(l,a)}}return null}function Mk(e){if(!Fe(e)||!Ze(e.game)||!Ze(e.locale)||!Array.isArray(e.cards)||e.cards.length===0)return"the file is not a catalog (game, locale and a list of cards)";let t=e.identity===void 0?"name":e.identity;if(!_d(t))return`"identity" must be "name" or "printing", not ${JSON.stringify(e.identity)}`;let n=e.facets===void 0?[]:Nk(e.facets);if(typeof n=="string")return n;let o=new Map,u=new Map,l=[];for(let c of e.cards){if(!Fe(c)||!Ze(c.id)||!Ze(c.name))return"every card needs an id, a name and an art path";if(!("art"in c)||c.art!==null&&!Ze(c.art))return`card ${c.id}: "art" must be an art path (a file path or an http(s) URL), or null for a card without art`;if(u.has(c.id))return`card id ${c.id} appears twice`;let s=c.printingOf??null;if(s!==null&&!Ze(s))return`card ${c.id}: "printingOf" must be a card id or null`;let r=c.facets===void 0?null:zk(c.facets,n,c.id,o);if(r!==null)return r;if(c.extra!==void 0&&!Fe(c.extra))return`card ${c.id}: "extra" must be an object (any keys and values), not ${Jt(c.extra)}`;let i={id:c.id,name:c.name,art:c.art,printingOf:s};c.extra!==void 0&&(i.extra=c.extra),u.set(i.id,i),l.push(i)}if(t==="printing")for(let c of l){if(c.printingOf===null)continue;let s=u.get(c.printingOf);if(s===void 0||s===c)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is not another card`;if(s.printingOf!==null)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is itself a printing of ${s.printingOf}`}let d=n.map(c=>{let s=c.values??[...o.get(c.key)??[]];return c.values===null&&c.type==="number"&&s.sort((r,i)=>r-i),{key:c.key,label:c.label,type:c.type??"text",values:[...s]}});return{game:e.game,locale:e.locale,identity:t,cards:l,facets:d}}async function $d(e,t=xd,n={}){try{let o=await D$(t,e,"release"),u=C$(rs(o,"release"));if("error"in u)throw new qe(`release: ${u.error}`);let l=u.release,d=await kk(new TextDecoder().decode(o),t.digestSha256Hex);if(d!==l.id)throw new qe(`release: id ${l.id} is not the hash of the document (which is ${d})`);if(n.ortWeb!==void 0&&!Ck(l.runtime.ortWeb,n.ortWeb))throw new qe(`release: measured on onnxruntime-web ${l.runtime.ortWeb}, this build runs ${n.ortWeb} (major and minor version must match)`);try{n.onDocument?.([l.detector,l.recognizer,l.bank,l.identity.groups,l.catalog].reduce((V,Z)=>V+Z.bytes,0))}catch{}let[c,s,r,i,a]=await Promise.all([Ri(t,e,l.detector,"detector"),Ri(t,e,l.recognizer,"recognizer"),Ri(t,e,l.bank,"bank"),Ri(t,e,l.identity.groups,"identity.groups"),Ri(t,e,l.catalog,"catalog")]),p=new Uint8Array(r),h=hd(p);if(h!==null&&h!==Ko)throw new qe(`bank: the binary bank is of format version ${h}, and this runtime reads version ${Ko} only`);let m=pd(p)?h$(p):cd(rs(r,"bank"));if(!m)throw new qe("bank: the file is not a bank this runtime can read");let b=[[m.version===l.bank.version,"bank.version differs from the bank file's"],[m.model===l.recognizer.version,`bank: the file's model ${JSON.stringify(m.model)} is not recognizer.version ${JSON.stringify(l.recognizer.version)}`],[m.dim===l.recognizer.dim,"bank: the vector size differs from recognizer.dim"],[m.preprocessing===l.recognizer.preprocessing,"bank: the file's preprocessing differs from recognizer.preprocessing"],[m.ids.length===l.bank.entryCount,"bank.entryCount is not the number of entries in the bank file"],[new Set(m.ids).size===l.bank.cardCount,"bank.cardCount is not the number of distinct card ids in the bank file"],[m.tau===null||m.tau===l.accept.tau,`bank: the file records tau ${m.tau}, accept.tau is ${l.accept.tau}`]];for(let[V,Z]of b)if(!V)throw new qe(Z);let $=Mk(rs(a,"catalog"));if(typeof $=="string")throw new qe(`catalog: ${$}`);if($.game!==l.game||$.locale!==l.locale)throw new qe("catalog: the file is for another game or locale");if($.cards.length!==l.catalog.cardCount)throw new qe(`catalog.cardCount ${l.catalog.cardCount} is not the file's ${$.cards.length} cards`);let _=new Map($.cards.map(V=>[V.id,V.name])),y=m.ids.filter(V=>!_.has(V));if(y.length>0)throw new qe(`${new Set(y).size} bank id(s) are not in the catalog (e.g. ${y[0]})`);let T=rs(i,"identity.groups"),S=yd(T);if(!S)throw new qe("identity.groups: the file is not an identity-groups document");if(S.version!==l.identity.groups.version)throw new qe("identity.groups.version differs from the file's");if((Fe(T)&&Fe(T.bank)?T.bank.sha256:void 0)!==l.bank.sha256)throw new qe("identity.groups: the file was not made for this release's bank (its bank sha256 differs)");let A=$.identity;if(l.identity.level!==A)throw new qe(`identity.level is "${l.identity.level}", and the catalog states the identity rule "${A}": a release is used with the rule its catalog states`);if((Fe(T)&&Fe(T.rule)?T.rule.level:void 0)!==A)throw new qe(`identity.groups: the file's groups are not by ${A}`);let I=new Map($.cards.map(V=>[V.id,w$(V,A)])),L=V=>JSON.stringify(V.map(Z=>[...Z].sort()).sort((Z,q)=>Z[0]q[0]?1:0));if(L(S.groups)!==L(v$(m.ids,I)))throw new qe(`identity.groups: the groups are not the ones the catalog's ${A==="name"?"names give":"identity rule gives"} for the bank's ids`);return{ok:!0,data:{release:l,detectorBytes:new Uint8Array(c),recognizerBytes:new Uint8Array(s),bank:m,identityGroups:S,cards:$.cards,names:_}}}catch(o){return{ok:!1,reason:o instanceof qe?o.message:`unexpected: ${String(o)}`}}}async function Bk(e){for(let t of e)try{await t?.release?.()}catch{}}async function N$(e,t,n=e.release.runtime.executionProviders){let{release:o}=e,u=null,l=null,d=async c=>(await Bk([u,l]),{error:c});try{if(n.length===0)return await d("no execution provider to try");let c=o.recognizer;return u=await t.embedder(e.recognizerBytes,{inputName:c.inputName,outputName:c.outputName,inputSize:Vt[c.preprocessing].size,dim:c.dim},n),u?(l=await t.detector(e.detectorBytes,o.detector.settings,n),l?{pipeline:new Pi({detector:l,embedder:u,bank:e.bank,accept:o.accept,identityGroups:e.identityGroups,identityDisplay:o.identity.display,names:e.names,hoverSelection:o.hover.selection,classNames:o.detector.settings.classes,faceClass:o.detector.settings.faceClass,minCropPx:o.detector.settings.minCropPx}),embedder:u,detector:l,providers:[...n]}:await d("detector: the model did not initialize on any execution provider, or is not the model the release describes")):await d("recognizer: the model did not initialize on any execution provider, or is not the model the release describes")}catch(c){return await d(`unexpected while building: ${String(c)}`)}}var zi=class extends Error{constructor(n){super(`the release could not be loaded: ${n}`);this.reason=n;this.name="ReleaseError"}reason};function Fk(e){return async t=>{if(!t.startsWith(Pn))throw new Error(`${t} is not one of the release's files`);let n=s$(t.slice(Pn.length)),o=Object.hasOwn(e,n)?e[n]:void 0;if(o===void 0)throw new Error(`no file ${n} among the release's files`);return{ok:!0,status:200,arrayBuffer:()=>o.arrayBuffer()}}}var Lk={embedder:(e,t,n)=>Ti.create(e,t,n),detector:(e,t,n)=>$i.create(e,t,n)};async function jk(e,t={}){let n=t.files?Pn:typeof location<"u"?location.href:void 0,o=new URL(e,n).href,u=r=>{try{t.onProgress?.(r)}catch{}};u({phase:"downloading",bytes:null});let l={...xd,...t.deps,...t.files?{fetchFn:Fk(t.files)}:{}},d=await $d(o,l,{ortWeb:K2(),...t.onProgress?{onDocument:r=>u({phase:"downloading",bytes:r})}:{}});if(!d.ok)throw new zi(d.reason);u({phase:"preparing"});let{data:c}=d;X2({wasmThreads:c.release.runtime.wasmThreads,...t.wasmPaths!==void 0?{wasmPaths:t.wasmPaths}:{}}),ed();let s=await N$(c,Lk,t.providers??c.release.runtime.executionProviders);if("error"in s){let r=ed();throw new zi(r.length>0?`${s.error} (${r.join("; ")})`:s.error)}return{release:c.release,pipeline:s.pipeline,backend:{detector:s.detector.ep,recognizer:s.embedder.ep},cards:c.cards,names:c.names,stageInputs:{bankSha256:c.release.bank.sha256,bankIds:[...new Set(c.bank.ids)],identities:o$(c.cards,c.release.identity.level,c.identityGroups)},dispose:async()=>{await s.embedder.release?.(),await s.detector.release?.()}}}function Uk(e,t,n){return e.pipeline.analyze(t,n)}function ns(e){return e==="wasm"?2e3:1e3}function Vk(e){return e==="wasm"?1e3:500}function is(e,t){return t==="always-on"?ns(e):Vk(e)}var qk=1920,Gk=720;function as(e,t,n={min:720,max:1920}){let o=Math.max(n.min,Math.min(n.max,e));return{width:o,height:Math.max(1,Math.round(o*t/e))}}function B$(e,t,n){if(!(e.width>0&&e.height>0))return null;let o=(t-e.left)/e.width,u=(n-e.top)/e.height;return o>=0&&o<=1&&u>=0&&u<=1?{x:o,y:u}:null}function Rn(e){return e instanceof HTMLVideoElement?{width:e.videoWidth,height:e.videoHeight}:{width:e.naturalWidth,height:e.naturalHeight}}function Hk(e){return e instanceof HTMLVideoElement?e.currentTime:0}async function Td(e,t,n,o){let u=Rn(e),l=as(u.width,u.height,o);t.width=l.width,t.height=l.height;let d=performance.now(),c=Hk(e);return n.drawImage(e,0,0,t.width,t.height),{bitmap:await createImageBitmap(t),videoTimeSec:c,tapMs:performance.now()-d}}var z$=128,M$=.06,Wk=.5,F$=60,ss=class{ref=null;seeded=!1;seed(t){this.ref=t,this.seeded=!0}reset(){this.ref=null,this.seeded=!1}get hasReference(){return this.ref!==null}observe(t,n){let o=this.ref;if(o===null||o.width!==t.width||o.height!==t.height)return this.ref=t,this.seeded=!1,{distance:null,cut:!1};let u=Zk(o,t,n);return u===null?{distance:u,cut:!1}:u>M$?{distance:u,cut:!0}:((this.seeded||u<=M$*Wk)&&(this.ref=t),this.seeded=!1,{distance:u,cut:!1})}};function Kk(e,t){return{width:z$,height:Math.max(1,Math.round(z$*t/Math.max(1,e)))}}function Xk(e,t,n){let o=new Uint8Array(t*n);for(let u=0,l=0;u>8;return o}function Zk(e,t,n){if(e.width!==t.width||e.height!==t.height||!(n.width>0)||!(n.height>0))return null;let{width:o,height:u}=e,l=os(Math.floor(n.x*o),0,o-1),d=os(Math.ceil((n.x+n.width)*o),l+1,o),c=os(Math.floor(n.y*u),0,u-1),s=os(Math.ceil((n.y+n.height)*u),c+1,u),r=0;for(let i=c;i`[WebGPU] ${e.name} shader code: ${d}`);let s=n.createComputePipeline({compute:{module:c,entryPoint:"main"},layout:"auto",label:e.name});return Vt(e.name),{programInfo:e,computePipeline:s,uniformVariablesInfo:u.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,n=typeof e=="number"?1:e.y||1,i=typeof e=="number"?1:e.z||1,u=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=u&&n<=u&&i<=u)return[t,n,i];let l=t*n*i,d=Math.ceil(Math.sqrt(l));if(d>u){if(d=Math.ceil(Math.cbrt(l)),d>u)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[d,d,d]}else return[d,d,1]}}}),h$={};wn(h$,{WebGpuBackend:()=>f$});var t0,r0,n0,f$,Uk=D(()=>{"use strict";dt(),de(),ur(),xx(),YA(),Lk(),jk(),t0=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let n=[];for(let i=0;i{let i=e.name;return e.shaderCache?.hint&&(i+="["+e.shaderCache.hint+"]"),i+=":"+n+`:${t0(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,i},n0=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vendor)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},f$=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let n=[],i={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n},u=c=>t.features.has(c)&&n.push(c)&&!0;u("chromium-experimental-timestamp-query-inside-passes")||u("timestamp-query"),u("shader-f16"),u("subgroups"),this.device=await t.requestDevice(i);let l=t,d=t.info??(typeof l.requestAdapterInfo=="function"?await l.requestAdapterInfo():void 0);this.adapterInfo=new n0(d),this.gpuDataManager=Sx(this),this.programManager=new p$(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,ud(e.logLevel,!!e.debug),this.device.onuncapturederror=c=>{c.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${c.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!0}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose(),this.device&&this.env?.webgpu&&this.device.lost.then(()=>{delete this.env.webgpu.device})}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Jt(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{let t=new BigUint64Array(e.getMappedRange()),n=this.pendingQueries.get(e);for(let i=0;i"u"&&(this.queryTimeBase=p);let m=Number(p-this.queryTimeBase),g=Number(h-this.queryTimeBase);if(!Number.isSafeInteger(m)||!Number.isSafeInteger(g))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:o.map(_=>({dims:_.dims,dataType:sr(_.dataType)})),outputsMetadata:a.map(_=>({dims:_.dims,dataType:sr(_.dataType)})),kernelId:l,kernelType:c,kernelName:s,programName:r,startTime:m,endTime:g});else{let _="";o.forEach((y,T)=>{_+=`input[${T}]: [${y.dims}] | ${sr(y.dataType)}, `});let w="";a.forEach((y,T)=>{w+=`output[${T}]: [${y.dims}] | ${sr(y.dataType)}, `}),console.log(`[profiling] kernel "${l}|${c}|${s}|${r}" ${_}${w}start time: ${m} ns, execution time: ${g-m} ns`)}Lo("GPU",`${r}::${p}::${h}`)}e.unmap(),this.pendingQueries.delete(e)}),Vt()}run(e,t,n,i,u,l){Jt(e.name);let d=[];for(let y=0;yT):n;if(o.length!==c.length)throw new Error(`Output size ${o.length} must be equal to ${c.length}.`);let a=[],p=[];for(let y=0;y=l)throw new Error(`Invalid output index: ${o[y]}`);if(o[y]===-3)continue;let T=o[y]===-1,S=o[y]===-2,O=T||S?u(c[y].dataType,c[y].dims):i(o[y],c[y].dataType,c[y].dims);if(a.push(O),O.data===0)continue;let A=this.gpuDataManager.get(O.data);if(!A)throw new Error(`no GPU data for output: ${O.data}`);if(T&&this.temporaryData.push(A),S){let P=this.kernelPersistentData.get(this.currentKernelId);P||(P=[],this.kernelPersistentData.set(this.currentKernelId,P)),P.push(A)}p.push(A)}if(d.length!==t.length||p.length!==a.length){if(p.length===0)return Vt(e.name),a;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let h;if(r){let y=0,T=[];r.forEach(P=>{let I=typeof P.data=="number"?[P.data]:P.data;if(I.length===0)return;let z=P.type===10?2:4,G,J;P.type===10?(J=I.length>4?16:I.length>2?8:I.length*z,G=I.length>4?16:z*I.length):(J=I.length<=2?I.length*z:16,G=16),y=Math.ceil(y/J)*J,T.push(y);let q=P.type===10?8:4;y+=I.length>4?Math.ceil(I.length/q)*G:I.length*z});let S=16;y=Math.ceil(y/S)*S;let O=new ArrayBuffer(y);r.forEach((P,I)=>{let z=T[I],G=typeof P.data=="number"?[P.data]:P.data;if(P.type===6)new Int32Array(O,z,G.length).set(G);else if(P.type===12)new Uint32Array(O,z,G.length).set(G);else if(P.type===10)new Uint16Array(O,z,G.length).set(G);else if(P.type===1)new Float32Array(O,z,G.length).set(G);else throw new Error(`Unsupported uniform type: ${sr(P.type)}`)});let A=this.gpuDataManager.create(y,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(A.buffer,0,O,0,y),this.gpuDataManager.release(A.id),h={offset:0,size:y,buffer:A.buffer}}let m=this.programManager.normalizeDispatchGroupSize(s),g=m[1]===1&&m[2]===1,_=r0(e,t,g),w=this.programManager.getArtifact(_);if(w||(w=this.programManager.build(e,m),this.programManager.setArtifact(_,w),Ie("info",()=>`[artifact] key: ${_}, programName: ${e.name}`)),r&&w.uniformVariablesInfo){if(r.length!==w.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${w.uniformVariablesInfo.length}, got ${r.length} in program "${w.programInfo.name}".`);for(let y=0;y`[ProgramManager] run "${e.name}" (key=${_}) with ${m[0]}x${m[1]}x${m[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let y={kernelId:this.currentKernelId,programName:w.programInfo.name,inputTensorViews:t,outputTensorViews:a};this.pendingKernels.push(y),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(y)}return this.programManager.run(w,d,p,m,h),Vt(e.name),a}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,n,i){let u=c$.get(e);if(!u)throw new Error(`kernel not implemented: ${e}`);let l={kernelType:e,kernelName:i,kernelEntry:u[0],attributes:[u[1],n]};this.kernels.set(t,l)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let n of t)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,n){let i=this.kernels.get(e);if(!i)throw new Error(`kernel not created: ${e}`);let u=i.kernelType,l=i.kernelName,d=i.kernelEntry,c=i.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${u}] ${l}" is not allowed to be called recursively`);this.currentKernelId=e,c[0]&&(c[1]=c[0](c[1]),c[0]=void 0),Ie("info",()=>`[WebGPU] Start to run kernel "[${u}] ${l}"...`);let s=this.env.debug;this.temporaryData=[];try{return s&&this.device.pushErrorScope("validation"),d(t,c[1]),0}catch(r){return n.push(Promise.resolve(`[WebGPU] Kernel "[${u}] ${l}" failed. ${r}`)),1}finally{s&&n.push(this.device.popErrorScope().then(r=>r?`GPU validation error for kernel "[${u}] ${l}": ${r.message}`:null));for(let r of this.temporaryData)this.gpuDataManager.release(r.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,n,i){let u=this.sessionExternalDataMapping.get(e);u||(u=new Map,this.sessionExternalDataMapping.set(e,u));let l=u.get(t),d=this.gpuDataManager.registerExternalBuffer(n,i,l);return u.set(t,[d,n]),d}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,n){return async()=>{let i=await Pl(this,e,t);return ld(i.buffer,n)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){Ie("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){Ie("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){Ie("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),n=e.length;this.pendingKernels=[];for(let i=0;i=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),m$={};wn(m$,{init:()=>g$});var Ro,i0,g$,Vk=D(()=>{"use strict";de(),ur(),pe(),ZA(),Ro=class b${constructor(t,n,i,u){this.module=t,this.dataType=n,this.data=i,this.dims=u}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=F.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=F.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=F.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=F.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(F.size(t)!==F.size(this.dims))throw new Error("Invalid new shape");return new b$(this.module,this.dataType,this.data,t)}},i0=class{constructor(e,t,n){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let i=e.PTR_SIZE,u=n/e.PTR_SIZE,l=i===4?"i32":"i64";this.opKernelContext=Number(e.getValue(i*u++,l));let d=Number(e.getValue(i*u++,l));this.outputCount=Number(e.getValue(i*u++,l)),this.customDataOffset=Number(e.getValue(i*u++,"*")),this.customDataSize=Number(e.getValue(i*u++,l));let c=[];for(let s=0;stypeof d=="number"?this.inputs[d]:d)??this.inputs,i=t?.outputs??[],u=(d,c,s)=>new Ro(this.module,c,this.output(d,s),s),l=(d,c)=>{let s=cn(d,c);if(!s)throw new Error(`Unsupported data type: ${d}`);let r=s>0?this.backend.gpuDataManager.create(s).id:0;return new Ro(this.module,d,r,c)};return this.backend.run(e,n,i,u,l,this.outputCount)}output(e,t){let n=this.module.stackSave();try{let i=this.module.PTR_SIZE,u=i===4?"i32":"i64",l=this.module.stackAlloc((1+t.length)*i);this.module.setValue(l,t.length,u);for(let d=0;d{let u=t.jsepInit;if(!u)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let l=(Uk(),Nn(h$)).WebGpuBackend,d=new l;await d.initialize(n,i),u("webgpu",[d,c=>d.alloc(Number(c)),c=>d.free(c),(c,s,r,o=!1)=>{if(o)Ie("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(c)}, dst=${Number(s)}, size=${Number(r)}`),d.memcpy(Number(c),Number(s));else{Ie("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(c)}, gpuDataId=${Number(s)}, size=${Number(r)}`);let a=t.HEAPU8.subarray(Number(c>>>0),Number(c>>>0)+Number(r));d.upload(Number(s),a)}},async(c,s,r)=>{Ie("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${c}, dataOffset=${s}, size=${r}`),await d.download(Number(c),()=>t.HEAPU8.subarray(Number(s)>>>0,Number(s+r)>>>0))},(c,s,r)=>d.createKernel(c,Number(s),r,t.UTF8ToString(t._JsepGetNodeName(Number(s)))),c=>d.releaseKernel(c),(c,s,r,o)=>{Ie("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${r}, kernel=${c}, contextDataOffset=${s}`);let a=new i0(t,d,Number(s));return d.computeKernel(Number(c),a,o)},()=>d.captureBegin(),()=>d.captureEnd(),()=>d.replay()])}else{let l=new Tx(n);u("webnn",[l,()=>l.reserveTensorId(),d=>l.releaseTensorId(d),async(d,c,s,r,o)=>l.ensureTensor(d,c,s,r,o),(d,c)=>{l.uploadTensor(d,c)},async(d,c)=>l.downloadTensor(d,c),(d,c)=>l.registerMLContext(d,c),!!n.trace])}}}),o0,_d,vd,_r,s0,Du,Yo,xd,$d,Mu,Td,Sd,Id,y$=D(()=>{"use strict";dt(),WA(),KA(),de(),vn(),id(),yx(),o0=(e,t)=>{Be()._OrtInit(e,t)!==0&&Re("Can't initialize onnxruntime.")},_d=async e=>{o0(e.wasm.numThreads,Ho(e.logLevel))},vd=async(e,t)=>{Be().asyncInit?.();let n=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let i=e.webgpu.powerPreference;if(i!==void 0&&i!=="low-power"&&i!=="high-performance")throw new Error(`Invalid powerPreference setting: "${i}"`);let u=e.webgpu.forceFallbackAdapter;if(u!==void 0&&typeof u!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${u}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:i,forceFallbackAdapter:u}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let i=(Vk(),Nn(m$)).init;t==="webgpu"&&await i("webgpu",Be(),e,n),t==="webnn"&&await i("webnn",Be(),e)}},_r=new Map,s0=e=>{let t=Be(),n=t.stackSave();try{let i=t.PTR_SIZE,u=t.stackAlloc(2*i);t._OrtGetInputOutputCount(e,u,u+i)!==0&&Re("Can't get session input/output count.");let l=i===4?"i32":"i64";return[Number(t.getValue(u,l)),Number(t.getValue(u+i,l))]}finally{t.stackRestore(n)}},Du=(e,t)=>{let n=Be(),i=n.stackSave(),u=0;try{let l=n.PTR_SIZE,d=n.stackAlloc(2*l);n._OrtGetInputOutputMetadata(e,t,d,d+l)!==0&&Re("Can't get session input/output metadata.");let c=Number(n.getValue(d,"*"));u=Number(n.getValue(d+l,"*"));let s=n.HEAP32[u/4];if(s===0)return[c,0];let r=n.HEAPU32[u/4+1],o=[];for(let a=0;a{let t=Be(),n=t._malloc(e.byteLength);if(n===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,n),[n,e.byteLength]},xd=async(e,t)=>{let n,i,u=Be();Array.isArray(e)?[n,i]=e:e.buffer===u.HEAPU8.buffer?[n,i]=[e.byteOffset,e.byteLength]:[n,i]=Yo(e);let l=0,d=0,c=0,s=[],r=[],o=[];try{if([d,s]=await bx(t),t?.externalData&&u.mountExternalData){let S=[];for(let O of t.externalData){let A=typeof O=="string"?O:O.path,P=typeof O=="string"?O:O.data;S.push(ad(P).then(I=>{u.mountExternalData(A,I)}))}await Promise.all(S)}for(let S of t?.executionProviders??[])if((typeof S=="string"?S:S.name)==="webnn"){if(u.shouldTransferToMLTensor=!1,typeof S!="string"){let O=S,A=O?.context,P=O?.gpuDevice,I=O?.deviceType,z=O?.powerPreference;A?u.currentContext=A:P?u.currentContext=await u.webnnCreateMLContext(P):u.currentContext=await u.webnnCreateMLContext({deviceType:I,powerPreference:z})}else u.currentContext=await u.webnnCreateMLContext();break}l=await u._OrtCreateSession(n,i,d),u.webgpuOnCreateSession?.(l),l===0&&Re("Can't create a session."),u.jsepOnCreateSession?.(),u.currentContext&&(u.webnnRegisterMLContext(l,u.currentContext),u.currentContext=void 0,u.shouldTransferToMLTensor=!0);let[a,p]=s0(l),h=!!t?.enableGraphCapture,m=[],g=[],_=[],w=[],y=[];for(let S=0;SS==="gpu-buffer"||S==="ml-tensor"||S==="ml-tensor-cpu-output")&&(c=u._OrtCreateBinding(l),c===0&&Re("Can't create IO binding."),T={handle:c,outputPreferredLocations:y,outputPreferredLocationsEncoded:y.map(S=>S==="ml-tensor-cpu-output"?"ml-tensor":S).map(S=>kl(S))}),_r.set(l,[l,r,o,T,h,!1]),[l,m,g,_,w]}catch(a){throw r.forEach(p=>u._OrtFree(p)),o.forEach(p=>u._OrtFree(p)),c!==0&&u._OrtReleaseBinding(c)!==0&&Re("Can't release IO binding."),l!==0&&u._OrtReleaseSession(l)!==0&&Re("Can't release session."),a}finally{u._free(n),d!==0&&u._OrtReleaseSessionOptions(d)!==0&&Re("Can't release session options."),s.forEach(a=>u._free(a)),u.unmountExternalData?.()}},$d=e=>{let t=Be(),n=_r.get(e);if(!n)throw new Error(`cannot release session. invalid session id: ${e}`);let[i,u,l,d,c]=n;d&&(c&&t._OrtClearBoundOutputs(d.handle)!==0&&Re("Can't clear bound outputs."),t._OrtReleaseBinding(d.handle)!==0&&Re("Can't release IO binding.")),t.jsepOnReleaseSession?.(e),t.webnnOnReleaseSession?.(e),t.webgpuOnReleaseSession?.(e),u.forEach(s=>t._OrtFree(s)),l.forEach(s=>t._OrtFree(s)),t._OrtReleaseSession(i)!==0&&Re("Can't release session."),_r.delete(e)},Mu=async(e,t,n,i,u,l,d=!1)=>{if(!e){t.push(0);return}let c=Be(),s=c.PTR_SIZE,r=e[0],o=e[1],a=e[3],p=a,h,m;if(r==="string"&&(a==="gpu-buffer"||a==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(d&&a!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${l} when enableGraphCapture is true.`);if(a==="gpu-buffer"){let w=e[2].gpuBuffer;m=cn(dn(r),o);{let y=c.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');h=y(i,l,w,m)}}else if(a==="ml-tensor"){let w=e[2].mlTensor;m=cn(dn(r),o);let y=c.webnnRegisterMLTensor;if(!y)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');h=y(i,w,dn(r),o)}else{let w=e[2];if(Array.isArray(w)){m=s*w.length,h=c._malloc(m),n.push(h);for(let y=0;yc.setValue(_+T*s,y,s===4?"i32":"i64"));let w=c._OrtCreateTensor(dn(r),h,m,_,o.length,kl(p));w===0&&Re(`Can't create tensor for input/output. session=${i}, index=${l}.`),t.push(w)}finally{c.stackRestore(g)}},Td=async(e,t,n,i,u,l)=>{let d=Be(),c=d.PTR_SIZE,s=_r.get(e);if(!s)throw new Error(`cannot run inference. invalid session id: ${e}`);let r=s[0],o=s[1],a=s[2],p=s[3],h=s[4],m=s[5],g=t.length,_=i.length,w=0,y=[],T=[],S=[],O=[],A=[],P=d.stackSave(),I=d.stackAlloc(g*c),z=d.stackAlloc(g*c),G=d.stackAlloc(_*c),J=d.stackAlloc(_*c);try{[w,y]=gx(l),hn("wasm prepareInputOutputTensor");for(let v=0;vce*xe,1);K=sr(Y);let le=p?.outputPreferredLocations[i[v]];if(K==="string"){if(le==="gpu-buffer"||le==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let ce=[];for(let xe=0;xe0){let ce=d.jsepGetBuffer;if(!ce)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let xe=ce(B),qe=cn(Y,ne);if(qe===void 0||!od(K))throw new Error(`Unsupported data type: ${K}`);V=!0,C.push([K,W,{gpuBuffer:xe,download:d.jsepCreateDownloader(xe,qe,K),dispose:()=>{d._OrtReleaseTensor(R)!==0&&Re("Can't release tensor.")}},"gpu-buffer"])}else if(le==="ml-tensor"&&ne>0){let ce=d.webnnEnsureTensor,xe=d.webnnIsGraphInputOutputTypeSupported;if(!ce||!xe)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(cn(Y,ne)===void 0||!sd(K))throw new Error(`Unsupported data type: ${K}`);if(!xe(e,K,!1))throw new Error(`preferredLocation "ml-tensor" for ${K} output is not supported by current WebNN Context.`);let qe=await ce(e,B,Y,W,!1);V=!0,C.push([K,W,{mlTensor:qe,download:d.webnnCreateMLTensorDownloader(B,K),dispose:()=>{d.webnnReleaseTensorId(B),d._OrtReleaseTensor(R)}},"ml-tensor"])}else if(le==="ml-tensor-cpu-output"&&ne>0){let ce=d.webnnCreateMLTensorDownloader(B,K)(),xe=C.length;V=!0,ee.push((async()=>{let qe=[xe,await ce];return d.webnnReleaseTensorId(B),d._OrtReleaseTensor(R),qe})()),C.push([K,W,[],"cpu"])}else{let ce=ss(K),xe=new ce(ne);new Uint8Array(xe.buffer,xe.byteOffset,xe.byteLength).set(d.HEAPU8.subarray(B,B+xe.byteLength)),C.push([K,W,xe,"cpu"])}}finally{d.stackRestore(H),K==="string"&&B&&d._free(B),V||d._OrtReleaseTensor(R)}}p&&!h&&(d._OrtClearBoundOutputs(p.handle)!==0&&Re("Can't clear bound outputs."),_r.set(e,[r,o,a,p,h,!1]));for(let[v,R]of await Promise.all(ee))C[v][2]=R;return fn("wasm ProcessOutputTensor"),C}finally{d.webnnOnRunEnd?.(r),d.stackRestore(P),T.forEach(q=>d._OrtReleaseTensor(q)),S.forEach(q=>d._OrtReleaseTensor(q)),O.forEach(q=>d._free(q)),w!==0&&d._OrtReleaseRunOptions(w),y.forEach(q=>d._free(q))}},Sd=e=>{let t=Be(),n=_r.get(e);if(!n)throw new Error("invalid session id");let i=n[0],u=t._OrtEndProfiling(i);u===0&&Re("Can't get an profile file name."),t._OrtFree(u)},Id=e=>{let t=[];for(let n of e){let i=n[2];!Array.isArray(i)&&"buffer"in i&&t.push(i.buffer)}return t}}),vr,At,An,bi,yi,Do,Nu,Mo,Wr,Kr,a0,w$,_$,v$,x$,$$,T$,S$,I$=D(()=>{"use strict";dt(),y$(),vn(),rd(),vr=()=>!!me.wasm.proxy&&typeof document<"u",An=!1,bi=!1,yi=!1,Mo=new Map,Wr=(e,t)=>{let n=Mo.get(e);n?n.push(t):Mo.set(e,[t])},Kr=()=>{if(An||!bi||yi||!At)throw new Error("worker not ready")},a0=e=>{switch(e.data.type){case"init-wasm":An=!1,e.data.err?(yi=!0,Nu[1](e.data.err)):(bi=!0,Nu[0]()),Do&&(URL.revokeObjectURL(Do),Do=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=Mo.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},w$=async()=>{if(!bi){if(An)throw new Error("multiple calls to 'initWasm()' detected.");if(yi)throw new Error("previous call to 'initWasm()' failed.");if(An=!0,vr())return new Promise((e,t)=>{At?.terminate(),fx().then(([n,i])=>{try{At=i,At.onerror=l=>t(l),At.onmessage=a0,Nu=[e,t];let u={type:"init-wasm",in:me};!u.in.wasm.wasmPaths&&(n||Al)&&(u.in.wasm.wasmPaths={wasm:new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href}),At.postMessage(u),Do=n}catch(u){t(u)}},t)});try{await nd(me.wasm),await _d(me),bi=!0}catch(e){throw yi=!0,e}finally{An=!1}}},_$=async e=>{if(vr())return Kr(),new Promise((t,n)=>{Wr("init-ep",[t,n]);let i={type:"init-ep",in:{epName:e,env:me}};At.postMessage(i)});await vd(me,e)},v$=async e=>vr()?(Kr(),new Promise((t,n)=>{Wr("copy-from",[t,n]);let i={type:"copy-from",in:{buffer:e}};At.postMessage(i,[e.buffer])})):Yo(e),x$=async(e,t)=>{if(vr()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Kr(),new Promise((n,i)=>{Wr("create",[n,i]);let u={type:"create",in:{model:e,options:{...t}}},l=[];e instanceof Uint8Array&&l.push(e.buffer),At.postMessage(u,l)})}else return xd(e,t)},$$=async e=>{if(vr())return Kr(),new Promise((t,n)=>{Wr("release",[t,n]);let i={type:"release",in:e};At.postMessage(i)});$d(e)},T$=async(e,t,n,i,u,l)=>{if(vr()){if(n.some(d=>d[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(u.some(d=>d))throw new Error("pre-allocated output tensor is not supported for proxy.");return Kr(),new Promise((d,c)=>{Wr("run",[d,c]);let s=n,r={type:"run",in:{sessionId:e,inputIndices:t,inputs:s,outputIndices:i,options:l}};At.postMessage(r,Id(s))})}else return Td(e,t,n,i,u,l)},S$=async e=>{if(vr())return Kr(),new Promise((t,n)=>{Wr("end-profiling",[t,n]);let i={type:"end-profiling",in:e};At.postMessage(i)});Sd(e)}}),zu,u0,O$,qk=D(()=>{"use strict";dt(),I$(),de(),td(),yx(),zu=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},u0=e=>{switch(e[3]){case"cpu":return new vt(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!od(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:n,download:i,dispose:u}=e[2];return vt.fromGpuBuffer(n,{dataType:t,dims:e[1],download:i,dispose:u})}case"ml-tensor":{let t=e[0];if(!sd(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:n,download:i,dispose:u}=e[2];return vt.fromMLTensor(n,{dataType:t,dims:e[1],download:i,dispose:u})}default:throw new Error(`invalid data location: ${e[3]}`)}},O$=class{async fetchModelAndCopyToWasmMemory(e){return v$(await ad(e))}async loadModel(e,t){Jt();let n;typeof e=="string"?n=await this.fetchModelAndCopyToWasmMemory(e):n=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await x$(n,t),Vt()}async dispose(){return $$(this.sessionId)}async run(e,t,n){Jt();let i=[],u=[];Object.entries(e).forEach(a=>{let p=a[0],h=a[1],m=this.inputNames.indexOf(p);if(m===-1)throw new Error(`invalid input '${p}'`);i.push(h),u.push(m)});let l=[],d=[];Object.entries(t).forEach(a=>{let p=a[0],h=a[1],m=this.outputNames.indexOf(p);if(m===-1)throw new Error(`invalid output '${p}'`);l.push(h),d.push(m)});let c=i.map((a,p)=>zu(a,()=>`input "${this.inputNames[u[p]]}"`)),s=l.map((a,p)=>a?zu(a,()=>`output "${this.outputNames[d[p]]}"`):null),r=await T$(this.sessionId,u,c,d,s,n),o={};for(let a=0;aVl,initializeFlags:()=>Ul,wasmBackend:()=>A$});var Ul,Vl,A$,Gk=D(()=>{"use strict";dt(),I$(),qk(),Ul=()=>{(typeof me.wasm.initTimeout!="number"||me.wasm.initTimeout<0)&&(me.wasm.initTimeout=0);let e=me.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),me.wasm.simd=!1),typeof me.wasm.proxy!="boolean"&&(me.wasm.proxy=!1),typeof me.wasm.trace!="boolean"&&(me.wasm.trace=!1),typeof me.wasm.numThreads!="number"||!Number.isInteger(me.wasm.numThreads)||me.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)me.wasm.numThreads=1;else{let t=typeof navigator>"u"?Bu("node:os").cpus().length:navigator.hardwareConcurrency;me.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},Vl=class{async init(e){Ul(),await w$(),await _$(e)}async createInferenceSessionHandler(e,t){let n=new O$;return await n.loadModel(e,t),n}},A$=new Vl});dt();dt();dt();var Hk="1.29.0";{let e=(qA(),Nn(ax)).onnxjsBackend;pn("webgl",e,-10)}{let e=(Gk(),Nn(E$)).wasmBackend;pn("webgpu",e,5),pn("webnn",e,5),pn("cpu",e,10),pn("wasm",e,10)}Object.defineProperty(me.versions,"web",{value:Hk,enumerable:!0});function k$(){return me.versions.web??""}function P$(e={}){me.wasm.numThreads=e.wasmThreads??1,e.wasmPaths!==void 0&&(me.wasm.wasmPaths=e.wasmPaths)}var as=[];function Od(){let e=as;return as=[],e}async function us(e,t,n){for(let i of t){let u=null;try{let l=performance.now();u=await Qo.create(e.slice().buffer,{executionProviders:[i]});let d=performance.now();if(await n(u))return{session:u,ep:i,createMs:d-l,firstRunMs:performance.now()-d};as.push(`${i}: the model does not have the inputs and outputs the release describes`)}catch(l){as.push(`${i}: ${l instanceof Error?l.message:String(l)}`.slice(0,300))}await u?.release().catch(()=>{})}return null}function qn(e,t){return new vt("float32",e,t)}function Ed(e){return{cx:e.x+e.width/2,cy:e.y+e.height/2,w:e.width,h:e.height,angle:0}}function Gn(e,t){let n=Math.max(e.x,t.x),i=Math.max(e.y,t.y),u=Math.min(e.x+e.width,t.x+t.width),l=Math.min(e.y+e.height,t.y+t.height);if(u<=n||l<=i)return 0;let d=(u-n)*(l-i);return d/(e.width*e.height+t.width*t.height-d)}function Ad(e,t,n,i){let u=[...e].sort((d,c)=>n(c)-n(d)),l=[];for(let d of u){let c=t(d);l.every(s=>Gn(c,t(s))O&&(O=P,S=A)}O>=l&&(c.push(w),s.push(O),r.push(S))}let o=c.length;if(o===0)return[];let a=new Float32Array(o),p=new Float32Array(o),h=new Float32Array(o),m=new Float32Array(o);for(let w=0;wy).sort((w,y)=>s[y]-s[w]||w-y),_=[];for(;g.length>0;){let w=g[0];if(_.push(w),g.length===1)break;let y=Qe(Qe(h[w]-a[w])*Qe(m[w]-p[w])),T=[];for(let S=1;S{let y={x:a[w]/n,y:p[w]/n,width:Qe(h[w]-a[w])/n,height:Qe(m[w]-p[w])/n};return{aabb:y,rect:Ed(y),score:s[w],cls:r[w]}})}function ls(e,t,n,i=Hn){let u=Math.trunc(Math.max(0,e.x)),l=Math.trunc(Math.max(0,e.y)),d=Math.trunc(Math.min(t,e.x+e.width)),c=Math.trunc(Math.min(n,e.y+e.height));return d-u>=i&&c-l>=i}function R$(e,t,n,i,u,l=kd,d=Hn){return C$(e,t,n,l).filter(c=>ls(c.aabb,i,u,d))}function Xk(e,t,n,i,u){let l=i/t,d=u/n;return[Math.max(0,e.x*l),Math.max(0,e.y*d),Math.min(i,(e.x+e.width)*l),Math.min(u,(e.y+e.height)*d)]}function D$(e,t){let n=e.aabb;if(n.x>=0&&n.y>=0&&n.x+n.width<=t.width&&n.y+n.height<=t.height)return e;let[i,u,l,d]=Xk(n,t.width,t.height,t.width,t.height),c={x:i,y:u,width:Math.max(0,l-i),height:Math.max(0,d-u)};return{...e,aabb:c,rect:Ed(c)}}function M$(e,t=Rr){let n=[],i=[];for(let u of e)(u.cls===t?n:i).push(u);return{faces:n,others:i}}function Zk(e){return e<0&&(e=-e),e<1?1-e:0}function N$(e,t){let n=e/t,i=n<1?1:n,u=i,l=Math.ceil(u)*2+1,d=new Int32Array(t*2),c=new Int32Array(t*l),s=new Float64Array(l);for(let r=0;re&&(m=e),m-=h;for(let g=0;g>22;return t<0?0:t>255?255:t}var z$="",Pd=null,Cd=null;function Yk(e,t,n,i){let u=`${e}x${t}>${n}x${i}`;return(u!==z$||!Pd||!Cd)&&(Pd=N$(e,n),Cd=N$(t,i),z$=u),{h:Pd,v:Cd}}function cs(e,t,n,i,u){let{h:l,v:d}=Yk(t,n,i,u),c=i!==t,s=u!==n,r,o;if(c){let w=s?d.bounds[0]:0,T=(s?d.bounds[u*2-2]+d.bounds[u*2-1]:n)-w;o=w,r=new Uint8Array(i*T*3);let{ksize:S,bounds:O,kk:A}=l;for(let P=0;P.5?t+1:n<.5||t%2===0?t:t+1}function Rd(e,t,n=ps){let i=Math.min(n.height/t,n.width/e);return{ratio:i,width:B$(e*i),height:B$(t*i)}}function Jk(e,t,n,i=ps){let u=Rd(t,n,i),l=cs(e,t,n,u.width,u.height),d=new Uint8Array(i.width*i.height*3).fill(i.pad);for(let c=0;c{let o=(await s.run({[n.inputName]:qn(new Float32Array(3*u*l),[1,3,l,u])}))[n.outputName]?.dims??[];return o.length===3&&o[0]===1&&o[1]===n.anchors&&o[2]===d});return c?new e(c.session,c.ep,n,{createMs:c.createMs,firstRunMs:c.firstRunMs}):null}async propose(t){let{letterbox:n,inputName:i,outputName:u,decode:l,minCropPx:d}=this.settings,c=performance.now(),{tensor:s,ratio:r}=Dd(t.data,t.width,t.height,n),o=performance.now(),a=await this.session.run({[i]:qn(s,[1,3,n.height,n.width])});return this.lastTimings={preprocessMs:o-c,inferMs:performance.now()-o},R$(a[u].data,this.columns,r,t.width,t.height,l,d)}async release(){await this.session.release().catch(()=>{})}};var Qk=32;async function eP(e,t,n,i,u=Qk){if(n<=u)return e(t,n);let l=t.length/n,d=new Float32Array(n*i);for(let c=0;c(await d.run({[n.inputName]:qn(new Float32Array(3*u*u),[1,3,u,u])}))[n.outputName]?.data.length===n.dim);return l?new e(l.session,l.ep,n,{createMs:l.createMs,firstRunMs:l.firstRunMs}):null}async embedBatch(t,n){return eP((i,u)=>this.run(i,u),t,n,this.settings.dim)}async run(t,n){let{inputName:i,outputName:u,inputSize:l,dim:d}=this.settings,s=(await this.session.run({[i]:qn(t,[n,3,l,l])}))[u].data;if(s.length!==n*d)throw new Error(`unexpected embedding size ${s.length} for ${n} crops`);return s}async release(){await this.session.release().catch(()=>{})}};var tP=["orb","akaze","sift"];var rP={orb:[["scaleFactor",1.2,!1],["nLevels",8,!0],["edgeThreshold",15,!0],["patchSize",15,!0],["fastThreshold",10,!0]],akaze:[["threshold",.001,!1],["nOctaves",4,!0],["nOctaveLayers",4,!0]],sift:[["nOctaveLayers",3,!0],["contrastThreshold",.04,!1],["edgeThreshold",10,!1],["sigma",1.6,!1]]},RD=Object.fromEntries(tP.map(e=>[e,Object.fromEntries(rP[e].map(([t,n])=>[t,n]))])),DD=Object.freeze({seed:0,features:{type:"orb",maxFeatures:300,artLongSide:320,mask:[],params:{}},crop:{longSide:320,maxFeatures:500},candidates:{embeddingTopK:5,voteTopN:0},vote:{matcher:"bf",ratio:.8,lshTables:12,lshKeySize:20,lshProbes:2,kdTrees:4,checks:64},match:{ratio:.8},ransac:{model:"homography",method:"ransac",threshold:6,maxIters:2e3,confidence:.995,minAreaRatio:.25,maxAreaRatio:4},fusion:{weight:.1,minInliers:8,fullInliers:30,penalty:0,baseline:"none",reorder:!0},gate:{belowAccept:!1,sources:null}}),nP=["features","crop","candidates","vote","match","ransac","fusion","gate"],MD=["index","seed",...nP];var zD=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]);function F$(e,t,n){let i=new Float64Array(e);for(let u of t){let l=u.fused-u.cosine;for(let d of n(u.cardId))i[d]=i[d]+l}return i}var iP=10n;function oP(e){let t=new DataView(new ArrayBuffer(8));t.setFloat64(0,e);let n=t.getUint32(0),i=t.getUint32(4),u=n>>>31?-1n:1n,l=n>>>20&2047,d=BigInt(n&1048575)<<32n|BigInt(i),c;return l===0?c=-1074:(d|=1n<<52n,c=l-1075),{mantissa:u*d,exponent:c}}function Ui(e,t){if(!Number.isFinite(e)||e===0)return e;let{mantissa:n,exponent:i}=oP(e),u=n<0n,d=(u?-n:n)*iP**BigInt(t),c=1n;i>=0?d<<=BigInt(i):c<<=BigInt(-i);let s=d/c,o=2n*(d-s*c);if((o>c||o===c&&s%2n===1n)&&(s+=1n),s===0n)return u?-0:0;let a=`${u?"-":""}${s.toString()}e-${t}`;return Number(a)}function sP(e){return{cardId:e.cardId,cosine:Ui(e.cosine,6),foundBy:e.foundBy,votes:e.votes,matches:e.matches,inliers:e.inliers,evidence:Ui(e.evidence,6),fused:Ui(e.fused,6)}}function L$(e,t){return{before:{topId:e.topId,similarity:Ui(e.similarity,6),margin:Ui(e.margin,6),accepted:e.accepted},candidates:t.candidates.map(sP),...t.debug?{debug:t.debug}:{}}}var aP=23,jD=(1<!n.representative.has(d))??[...u].sort()[0];for(let d of u)i.set(d,`group:${n.representative.get(d)??l}`)}return i}var Wn="file:///";function U$(e){let t=new URL(e,Wn).href.slice(Wn.length);try{return decodeURIComponent(t)}catch{return t}}var V$="busy";var er={full128:{id:"full128",size:128,centerMask:!1,poolVersion:2,crop:"truncate"}};function Md(e){return typeof e=="string"&&Object.prototype.hasOwnProperty.call(er,e)}function hs(e){if(typeof e!="object"||e===null||Array.isArray(e))return null;let t=e;if(!Md(t.id))return null;let n=er[t.id],i=Object.keys(t);return i.length===Object.keys(n).length&&i.every(l=>n[l]===t[l])?t.id:null}function G$(e,t,n,i,u,l){let d=new Float64Array(i*u*3),c=new Uint32Array(i*u);for(let a=0;a0&&(d[a*3]=d[a*3]/p,d[a*3+1]=d[a*3+1]/p,d[a*3+2]=d[a*3+2]/p)}if(l===1||t>=i&&n>=u||t<=0||n<=0)return d;let s=q$(n,u),r=q$(t,i),o=new Float64Array(d.length);for(let a=0;a=0&&!n[l];)l--;let d=u+1;for(;d=0&&(d>=t||u-l<=d-u)?l:d}return i}function fs(e,t,n,i){let u=i.size,l=G$(e,t,n,u,u,i.poolVersion),d=new Float32Array(3*u*u),c=u*u;for(let s=0;s>10&31,s=l&1023;c===0?i[u]=d*s*2**-24:c===31?i[u]=s?NaN:d*(1/0):i[u]=d*(1+s/1024)*2**(c-15)}return i}function lP(e,t){let n;try{n=atob(e)}catch{return null}if(n.length!==t*2)return null;let i=new Uint8Array(n.length);for(let u=0;u0)||!Number.isFinite(u))return!1;for(let l=0;l=Kn.length&&Kn.every((t,n)=>e[n]===t)}function Bd(e){return zd(e)&&e.length>Kn.length?e[Kn.length]:null}function X$(e){let t=e instanceof Uint8Array?e:new Uint8Array(e);if(Bd(t)!==gs||t.lengthtypeof a=="string")||u.entryCount!==d.length)return null;let c=u.artVs;if(c!==void 0&&(!Array.isArray(c)||c.length!==d.length||!c.every(a=>a===null||typeof a=="string")))return null;let s=l.dim,r=Vi+n;if(t.length-r!==d.length*s*2)return null;let o=new Float32Array(d.length*s);for(let a=0;a({x:i+p*s-h*r,y:u+p*r+h*s}))}}function Z$(e,t){let{points:n}=t,i=0;for(let u=0;u0?1:-1;if(i===0)i=p;else if(p!==i)return!1}return!0}function Ld(e,t){let n=null;for(let i of t)Z$(e,i.quad)&&(!n||i.zIndex>=n.zIndex)&&(n=i);return n?n.card:null}function Xn(e){return e<0?0:e>1?1:e}function bs(e,t){let{frameWidth:n,frameHeight:i,names:u}=t;return e.map(l=>{let{aabb:d}=l,c=l.identity?u.get(l.identity):void 0,s=c!==void 0&&l.outline?l.outline:l.rect,r={instanceId:l.instanceId,bounds:{x:Xn(d.x/n),y:Xn(d.y/i),width:Xn(d.width/n),height:Xn(d.height/i)},rotation:(s.angle+45)%90-45,localWidth:Xn(s.w/n),localHeight:Xn(s.h/i),landscape:s.w>s.h,cardId:null,name:null};return l.identity&&c!==void 0&&(r.cardId=l.identity,r.name=c),r})}var J$=["accepted-nms","best-score","detector-score"];function Q$(e){return typeof e=="string"&&J$.includes(e)}var jd="accepted-nms",pP=.5,hP="hover_";function Y$(e){return e.width*e.height}function eT(e,t){let n=t==="accepted-nms"?e.kept:e.proposals.map((u,l)=>l),i=(u,l)=>{let d=e.proposals[u],c=e.proposals[l];if(t==="detector-score")return d.detectorScore!==c.detectorScore?d.detectorScore{let u=e.proposals[n];return{proposal:n,aabb:u.aabb,rect:u.rect,...u.outline?{outline:u.outline}:{},identity:u.accepted?u.displayId:null,score:u.score,zIndex:i+1}})}var ys=class{prev=[];nextId=1;reset(){this.prev=[]}assign(t){let n=new Set,i=t.map((l,d)=>d).sort((l,d)=>t[d].zIndex-t[l].zIndex),u=new Array(t.length);for(let l of i){let d=t[l],c=-1,s=pP;for(let r=0;r=s&&(c<0||o>s)&&(c=r,s=o)}c>=0?(n.add(c),u[l]=this.prev[c].id):u[l]=`${hP}${String(this.nextId++).padStart(4,"0")}`}return this.prev=t.map((l,d)=>({id:u[d],aabb:l.aabb,identity:l.identity})),u}};function ws(e,t,n,i=u=>u){return bs(e.map((l,d)=>({instanceId:t[d],aabb:l.aabb,rect:l.rect,...l.outline?{outline:l.outline}:{},identity:l.identity})),n).map((l,d)=>({...l,zIndex:e[d].zIndex,boxIndex:i(e[d].proposal)}))}var fP={card:null,boxIndex:null,box:null,cardId:null,name:null};function Ud(e,t,n){let i=e.map(l=>({card:l,quad:Fd(l,n),zIndex:l.zIndex??0})),u=Ld(t,i);return u?{card:u,boxIndex:u.boxIndex??null,box:{x:u.bounds.x*n.width,y:u.bounds.y*n.height,width:u.bounds.width*n.width,height:u.bounds.height*n.height},cardId:u.cardId,name:u.name}:fP}var mP="identity-groups";function Vd(e){if(typeof e!="object"||e===null)return null;let t=e;if(t.schemaVersion!==1||t.kind!==mP||typeof t.version!="number"||!Number.isInteger(t.version)||!Array.isArray(t.groups)||typeof t.representative!="object"||t.representative===null||Array.isArray(t.representative))return null;let n=new Map,i=[];for(let l of t.groups){if(!Array.isArray(l)||l.length<2)return null;let d=[];for(let c of l){if(typeof c!="string"||c.length===0||n.has(c))return null;n.set(c,i.length),d.push(c)}i.push(d)}let u=new Map;for(let[l,d]of Object.entries(t.representative)){if(typeof d!="string"||l===d)return null;let c=n.get(l);if(c===void 0||n.get(d)!==c)return null;u.set(l,d)}for(let l of i){let d=l.filter(c=>!u.has(c));if(d.length!==1||l.some(c=>u.has(c)&&u.get(c)!==d[0]))return null}return{version:t.version,groups:i,representative:u}}var gP=["name","printing"];function qd(e){return gP.includes(e)}function rT(e,t){return t==="printing"?e.printingOf??e.id:e.name}function nT(e,t){let n=new Map;for(let i of[...new Set(e)].sort(tT)){let u=t.get(i);if(u===void 0)continue;let l=n.get(u);l?l.push(i):n.set(u,[i])}return[...n.values()].filter(i=>i.length>=2).sort((i,u)=>tT(i[0],u[0]))}function tT(e,t){return et?1:0}var Tn=class{rep=new Map;display;constructor(t=null,n,i="representative"){if(this.display=i,!!t)for(let[u,l]of t.representative)i==="representative"&&n&&!n(l)||this.rep.set(u,l)}of(t){return this.rep.get(t)??t}shown(t){return this.display==="best-printing"?t:this.of(t)}get collapsedCount(){return this.rep.size}};var _s=["webgpu","wasm"];function Gd(e,t=e.hover.selection){let n=e.boxes.flatMap((s,r)=>s.face!==null?[r]:[]),i=n.map(s=>e.boxes[s]),u=new Map,l=i.map(s=>{let r=s.face;r.cardId!==null&&r.name!==null&&u.set(r.cardId,r.name);let o={x:s.box[0],y:s.box[1],width:s.box[2],height:s.box[3]};return{aabb:o,rect:{cx:o.x+o.width/2,cy:o.y+o.height/2,w:o.width,h:o.height,angle:0},score:r.similarity,detectorScore:s.score,accepted:r.cardId!==null,displayId:r.cardId??r.topId}}),d=i.flatMap((s,r)=>s.face.kept?[r]:[]),c=qi({proposals:l,kept:d},t);return ws(c,c.map((s,r)=>`${t}_${r}`),{frameWidth:e.frame.width,frameHeight:e.frame.height,names:u},s=>n[s])}function vs(e,t,n,i){let u=i===void 0||i===e.hover.selection?e.hover.cards:Gd(e,i);return Ud(u,{x:t,y:n},e.frame)}var fe=Math.fround,iT=63/88,bP=128,Tt=90,yP=.2,wP=8,_P=.35,vP=.55,xP=.9,$P=Object.freeze({scales:[.95,1,1.05],maxFacesPerFrame:8,budgetMs:1500}),TP=[1,.8824969025845955,.6065306597126334,.32465246735834974,.1353352832366127,.04393693362340742,.011108996538242306],oT=6,SP=fe(fe(180)/fe(Math.PI)),Hd=[fe(.299),fe(.587),fe(.114)];function IP(e){let t=e-90*Math.ceil((e-45)/90);return t<=-45?45:t}function OP(e,t){let n=e%t;return n!==0&&n<0!=t<0?n+t:n}function EP(e,t){let n=e%t;return n===0?0:n<0?fe(n+t):n}function sT(e){let t=Math.floor(e),n=e-t;return n<.5?t:n>.5?t+1:t%2===0?t:t+1}function AP(e,t){let n=e.length,i=Float32Array.from(e).sort(),u=fe(t),l=fe(fe(fe(n*u)+fe(1+fe(u*-1)))-1),d,c;l>=n-1?d=c=n-1:l<0?d=c=0:(d=Math.floor(l),c=d+1);let s=d===c?0:fe(l-d),r=i[d],o=i[c],a=fe(o-r);return s>=.5?fe(o-fe(a*fe(1-s))):fe(r+fe(a*s))}function Wd(e){let{width:t,height:n}=e,i=e.channels??4,u=Math.max(1,Math.ceil(Math.max(n,t)/bP)),l=u===1?n:Math.floor(n/u),d=u===1?t:Math.floor(t/u);if(Math.min(l,d)c[Math.min(l-1,Math.max(0,C))*d+Math.min(d-1,Math.max(0,ee))],o=l-2,a=d-2,p=o*a,h=new Float32Array(p),m=new Float32Array(p),g=new Uint8Array(p),_=new Float32Array(p),w=0,y=0;for(let C=0;Cy&&(y=_[B]))}if(y<=0)return 0;let T=new Float32Array(w);for(let C=0,ee=0;C=S)||!(ee>0))continue;let v=EP(fe(fe(Math.atan2(m[C],h[C]))*SP),90),R=Math.min(Math.trunc(fe(fe(v/90)*Tt)),Tt-1);O[R]=O[R]+ee}let A=new Float64Array(Tt);for(let C=0;CA[P]&&(P=C);let I=A[(P-1+Tt)%Tt],z=A[(P+1)%Tt],G=I-2*A[P]+z,J=G!==0?.5*(I-z)/G:0,q=OP((P+.5+J)*90/Tt,90);return IP(q)}var aT=1;function Kd(e,t,n){let i=Math.abs(n)*(Math.PI/180),u=Math.cos(i),l=Math.sin(i),d=u*u-l*l;if(d>_P){let s=(e*u-t*l)/d,r=(t*u-e*l)/d;if(s>0&&r>0){let o=Math.min(s,r)/Math.max(s,r);if(o>=vP&&o<=xP)return{w:s,h:r,how:"exact"}}}let c=null;for(let s of[iT,1/iT]){let r=s*u+l,o=s*l+u,a=(e*r+t*o)/(r*r+o*o),p=(e-a*r)**2+(t-a*o)**2;(c===null||pw>=0&&w=0&&yuT(e,c,s,l*r,d*r,u))}}function lT(e,t,n){if(Math.abs(e)!["scales","maxFacesPerFrame","budgetMs"].includes(l));if(n.length>0)return`tiltedRetry has unknown key(s) ${n.sort().join(", ")}`;let i=t.scales;if(!Array.isArray(i)||i.length<1||i.length>5||!i.every(l=>typeof l=="number"&&Number.isFinite(l)&&l>=.5&&l<=1.5))return"tiltedRetry.scales must list 1 to 5 numbers from 0.5 to 1.5";let u=l=>typeof l=="number"&&Number.isInteger(l)&&l>=1;return u(t.maxFacesPerFrame)?u(t.budgetMs)?null:"tiltedRetry.budgetMs must be a whole number above 0":"tiltedRetry.maxFacesPerFrame must be a whole number above 0"}var kP=.3;function hT(e,t){let n=Math.max(0,Math.floor(t.x)),i=Math.max(0,Math.floor(t.y)),u=Math.min(e.width,Math.floor(t.x+t.width)),l=Math.min(e.height,Math.floor(t.y+t.height)),d=Math.max(0,u-n),c=Math.max(0,l-i),s=new Uint8ClampedArray(d*c*4);for(let r=0;r=t.tau&&e.margin>=t.delta}function fT(e,t,n){if(lr(e,n)||!n.rotationRetry)return{orientation:0,match:e,retried:!1};let i=[e,...t()],u=-1;for(let l=0;li[u].similarity)&&(u=l);if(u<0){u=0;for(let l=1;li[u].similarity&&(u=l)}return{orientation:u,match:i[u],retried:!0}}function cT(e){let t=[],n=[];return e.forEach((i,u)=>{let l=i.tilted!==void 0&&i.tilted.scale!==null;i.tauPassed&&(i.orientation===0&&!l?t:n).push(u)}),[...t,...n]}function pT(e,t){let n=(t??e.map((i,u)=>u)).filter(i=>e[i].tauPassed);return Ad(n,i=>e[i].aabb,i=>e[i].score,kP)}async function Yd(e,t,n,i,u=Hn,l=null,d=null,c=!1){let s=n.preprocessing,r=n.rule,o=s.size,a=n.dim,p=[],h=[],m=[];for(let N of i){let X=hT(e,N.aabb),{rgba:W,width:ne,height:le}=X;nen.matchScored(w,X*a)),T=[];if(r.rotationRetry)for(let N=0;N[N,X])),O=null;if(T.length>0){let N=new Float32Array(T.length*3*g);for(let X=0;X{let W=S.get(N);if(W===void 0||O===null)throw new Error(`the turned looks of proposal ${N} were not embedded`);return(W*3+X-1)*a},P=h.map((N,X)=>fT(y[X],()=>[1,2,3].map(W=>n.matchScored(O,A(X,W))),r)),I=N=>P[N].orientation===0?{embs:w,off:N*a}:{embs:O,off:A(N,P[N].orientation)},z=new Map,G,J,q=!1;if(l)try{let N=performance.now(),X=[],W=[],ne=[];for(let le=0;le0){let le=await l.verifyBatch(ne,n.bankIds);X.forEach((ce,xe)=>{let qe=le[xe],Ve=W[xe],it=P[ce].match,Ze=F$(Ve,qe.candidates,zr=>n.columnsOf(zr)),$e=l.reorder?n.lookOf(Ze):n.lookWithin(Ze,n.lookOf(Ve).row),{row:ht,...It}=$e;z.set(ce,{match:It,geometric:L$({topId:it.topId,similarity:it.similarity,margin:it.margin,accepted:lr(it,r)},qe)})})}G=performance.now()-N}catch(N){q=!0,J=N,z.clear(),G=void 0}let C=new Map,ee,v,R=!1,H=0;if(d)try{let N=performance.now(),{scales:X,maxFacesPerFrame:W}=d.settings,ne=ce=>z.get(ce)?.match??P[ce].match,le=mT(h.map((ce,xe)=>({aabb:ce.aabb,similarity:P[xe].match.similarity,accepted:lr(ne(xe),r)})),d.focus??null).slice(0,W);if(le.length>0){let ce=[],xe=new Float32Array(le.length*X.length*g);le.forEach((Ve,it)=>{let Ze=m[Ve],{tilt:$e,rects:ht}=Xd(e,h[Ve].aabb,{data:Ze.rgba,width:Ze.width,height:Ze.height},X);ce.push($e),ht.forEach((It,zr)=>{let Gt=fs(It.rgba,It.width,It.height,s);xe.set(ms(Gt,P[Ve].orientation,o),(it*X.length+zr)*g)})}),H=le.length*X.length;let qe=await t.embedBatch(xe,H);le.forEach((Ve,it)=>{let Ze=ne(Ve),$e=null;for(let ht=0;ht$e.match.similarity)&&($e={q:ht,match:It})}C.set(Ve,{match:$e?$e.match:null,evidence:{tilt:ce[it],scale:$e?X[$e.q]:null,before:{topId:Ze.topId,similarity:Ze.similarity,margin:Ze.margin}}})})}ee=performance.now()-N}catch(N){R=!0,v=N,C.clear(),H=0,ee=void 0}let L=new Map,V=N=>{let X=L.get(N);if(X===void 0){let{aabb:W}=h[N],ne=m[N],{tilt:le,w:ce,h:xe}=Zd({data:ne.rgba,width:ne.width,height:ne.height},W.width,W.height);X={cx:W.x+W.width/2,cy:W.y+W.height/2,w:ce,h:xe,angle:le},L.set(N,X)}return X},K=N=>{let X=[];for(let W=0;Wt!==null&&t.x>=d.x&&t.x<=d.x+d.width&&t.y>=d.y&&t.y<=d.y+d.height,i=e.map((d,c)=>c).filter(d=>!e[d].accepted),u=i.filter(d=>n(e[d].aabb)).sort((d,c)=>e[d].aabb.width*e[d].aabb.height-e[c].aabb.width*e[c].aabb.height),l=i.filter(d=>!n(e[d].aabb)).sort((d,c)=>e[c].similarity-e[d].similarity);return[...u,...l]}var $s={tau:.62,delta:.05,rotationRetry:!0},xs=-2,Gi=class{constructor(t,n=$s,i=new Tn){this.bank=t;this.rule=n;this.contract=er[t.preprocessing],this.rowShown=t.ids.map(l=>i.shown(l));let u=new Map;this.rowIdent=new Int32Array(t.ids.length);for(let l=0;lc&&(c=p,s=a)}let r=xs,o=s>=0?this.rowIdent[s]:-1;for(let a=0;ar&&(r=d[a]);return{bestRow:s,best:c,other:r}}matchScored(t,n){let{bestRow:i,best:u,other:l}=this.topAndOther(t,n),d=this.bank.ids[i];return{cardId:u>=this.rule.tau?d:null,similarity:u,topId:d,identity:this.identRep[this.rowIdent[i]],displayId:this.rowShown[i],margin:u-l}}scoreRow(t,n){let{matrix:i,dim:u,ids:l}=this.bank,d=new Float64Array(l.length);for(let c=0;cd);n.sort((l,d)=>t[d]-t[l]);let i=new Set,u=[];for(let l of n){let d=this.bank.ids[l];i.has(d)||(i.add(d),u.push({cardId:d,cosine:t[l]}))}return u}columns=null;columnsOf(t){return this.columns===null&&(this.columns=new Map,this.bank.ids.forEach((n,i)=>{let u=this.columns.get(n);u?u.push(i):this.columns.set(n,[i])})),this.columns.get(t)??[]}bankIdSet=null;get bankIds(){return this.columnsOf(""),this.bankIdSet??=new Set(this.columns.keys()),this.bankIdSet}scored(t,n,i){let u=this.bank.ids[t];return{cardId:n>=this.rule.tau?u:null,similarity:n,topId:u,identity:this.identRep[this.rowIdent[t]],displayId:this.rowShown[t],margin:n-i,row:t}}lookOf(t){let n=0;for(let d=1;dt[n]&&(n=d);let i=this.rowIdent[n],u=xs,l=!1;for(let d=0;du)&&(u=t[d]),l=!0);return this.scored(n,t[n],u)}lookWithin(t,n){let i=this.rowIdent[n],u=-1,l=xs,d=!1;for(let c=0;ct[u])&&(u=c):((!d||t[c]>l)&&(l=t[c]),d=!0);return this.scored(u,t[u],l)}matchPooled(t,n=this.rule.delta){let{bestRow:i,best:u,other:l}=this.topAndOther(t,0),d=u-l;return{cardId:u>=this.rule.tau&&d>=n?this.rowShown[i]:null,similarity:u,margin:d}}};var Hi=class{constructor(t=3){this.k=t;if(!Number.isInteger(t)||t<1)throw new RangeError(`k must be a positive integer, got ${t}`)}k;run=0;misses=0;showing=!1;reset(){this.run=0,this.misses=0,this.showing=!1}observe(t){return t?(this.run+=1,this.misses=0,this.run>=this.k&&(this.showing=!0)):(this.run=0,this.misses+=1,this.misses>=this.k&&(this.showing=!1)),{confirmed:this.showing,consecutive:this.run}}};var PP=.4,CP=2,RP=5,DP=2,MP="track_";function gT(e){let t=e[0].length,n=new Float32Array(t);for(let l of e)for(let d=0;d0))return null;for(let l=0;lu));for(let i of this.tracks){let u=-1,l=PP;for(let d of n){let c=Gn(i.detection.aabb,t[d].aabb);c>l&&(l=c,u=d)}if(u>=0){let d=t[u];i.detection=d,i.misses=0,i.age+=1,i.embeddings.push(d.embedding),i.embeddings.length>RP&&i.embeddings.shift(),n.delete(u)}else i.misses+=1}this.tracks=this.tracks.filter(i=>i.misses<=CP);for(let i of n){let u=t[i];this.tracks.push({instanceId:`${MP}${String(this.nextId++).padStart(4,"0")}`,detection:u,embeddings:[u.embedding],misses:0,age:1})}return this.tracks.filter(i=>i.misses===0).map(i=>({instanceId:i.instanceId,detection:i.detection,identity:i.age>=DP?this.identityOf(i):null}))}};var NP=.25,zP=5;function BP(e,t){if(e===null||t===void 0)return!1;let n=t-e;return n<-NP||n>zP}function FP(e){return[e.x,e.y,e.width,e.height]}var Ki=class{hoverSelection;detector;embedder;recognizer;names;classNames;faceClass;minCropPx;now;tracker;gate=new Hi(3);hoverIds=new ys;heldCards=[];lastVideoTimeSec=null;geometric;geometricFailure=null;tiltedRetry;tiltedFailure=null;constructor(t){this.detector=t.detector,this.embedder=t.embedder,this.names=t.names,this.classNames=t.classNames??["face","back"],this.faceClass=t.faceClass??Rr,this.minCropPx=t.minCropPx??Hn,this.hoverSelection=t.hoverSelection??jd,this.now=t.now??(()=>performance.now()),this.geometric=t.geometric??null,this.tiltedRetry=t.tiltedRetry??null;let n=new Tn(t.identityGroups??null,i=>this.names.has(i),t.identityDisplay??"best-printing");this.recognizer=new Gi(t.bank,t.accept??$s,n),this.tracker=new Wi(i=>this.recognizer.matchPooled(i).cardId)}get size(){return this.recognizer.size}takeGeometricFailure(){let t=this.geometricFailure;return this.geometricFailure=null,t}takeTiltedFailure(){let t=this.tiltedFailure;return this.tiltedFailure=null,t}get tiltedSettings(){return this.tiltedRetry}setTiltedRetry(t){this.tiltedRetry=t,this.tiltedFailure=null}get geometricStage(){return this.geometric}setGeometric(t){this.geometric=t,this.geometricFailure=null}reset(){this.tracker.reset(),this.gate.reset(),this.heldCards=[],this.hoverIds.reset(),this.lastVideoTimeSec=null}async analyze(t,n,i={}){let u=this.now(),l=BP(this.lastVideoTimeSec,n);l&&this.reset(),n!==void 0&&(this.lastVideoTimeSec=n);let d=await this.detector.propose(t),c=this.now()-u,s=d.filter(I=>ls(I.aabb,t.width,t.height,this.minCropPx)).map(I=>D$(I,t)),{faces:r}=M$(s,this.faceClass),o=this.now(),a=this.tiltedRetry&&i.tilted!==!1?{settings:this.tiltedRetry,focus:i.focus??null}:null,p=await Yd(t,this.embedder,this.recognizer,r,this.minCropPx,this.geometric,a,i.rotatedOutlines===!0),h=this.now()-o;if(p.tiltedError!==void 0&&this.tiltedRetry&&(this.tiltedFailure={error:p.tiltedError},this.tiltedRetry=null),p.geometricError!==void 0&&this.geometric&&(this.geometricFailure={stage:this.geometric,error:p.geometricError},this.geometric=null),p.proposals.length!==r.length)throw new Error("pipeline: a valid face box was not recognized");let m={frameWidth:t.width,frameHeight:t.height,names:this.names},g=qi(p,this.hoverSelection),_=r.map(I=>s.indexOf(I)),w=ws(g,this.hoverIds.assign(g),m,I=>_[I]),y=p.base??p,T=y.kept.map(I=>{let z=y.proposals[I];return{aabb:z.aabb,rect:z.rect,cardId:z.topId,embedding:z.embedding,...z.outline?{outline:z.outline}:{}}}),S=this.tracker.update(T),O=T.length>0,A=this.gate.observe(O),P;return A.confirmed?O?(P=bs(S.map(I=>({instanceId:I.instanceId,aabb:I.detection.aabb,rect:I.detection.rect,...I.detection.outline?{outline:I.detection.outline}:{},identity:I.identity})),m),this.heldCards=P):P=this.heldCards:(this.heldCards=[],P=[]),{frame:{width:t.width,height:t.height},boxes:this.boxResults(s,r,p),hover:{selection:this.hoverSelection,cards:w},tracks:{shown:A.confirmed,cards:P},discontinuity:l,retried:p.retried,embeddings:p.embeddings,timings:{detectorMs:c,recognizeMs:h,analyzeMs:this.now()-u,...p.geometricMs!==void 0?{geometricMs:p.geometricMs}:{},...p.tiltedMs!==void 0?{tiltedMs:p.tiltedMs}:{}}}}boxResults(t,n,i){let u=new Map(n.map((d,c)=>[d,c])),l=new Set(i.kept);return t.map(d=>{let c=u.get(d),s=null;if(c!==void 0){let r=i.proposals[c],o=r.accepted&&this.names.has(r.displayId)?r.displayId:null;s={topId:r.topId,topName:this.names.get(r.topId)??null,similarity:r.score,margin:r.margin,look:r.orientation,retried:r.retried,tauPassed:r.tauPassed,accepted:r.accepted,cardId:o,name:o===null?null:this.names.get(o)??null,kept:l.has(c),...r.geometric?{geometric:r.geometric}:{},...r.tilted?{tilted:r.tilted}:{}}}return{box:FP(d.aabb),score:d.score,cls:d.cls,className:this.classNames[d.cls]??String(d.cls),face:s}})}};var bT=1,yT="vision-release",LP=[8,16,32];function wT(e,t){return LP.reduce((n,i)=>n+Math.floor(e/i)*Math.floor(t/i),0)}var jP=/^[0-9a-f]{16}$/,UP=/^[0-9a-f]{64}$/,VP=/^\d+\.\d+\.\d+$/,qP=["schemaVersion","kind","id","game","locale","detector","recognizer","bank","identity","catalog","accept","hover","runtime","tiltedRetry"],_T=["tau","delta","rotationRetry"],Ue=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),ft=e=>typeof e=="number"&&Number.isInteger(e)&&e>0,Ji=e=>typeof e=="number"&&Number.isFinite(e),tt=e=>typeof e=="string"&&e.trim()!=="";function GP(e){return!tt(e)||e.includes("\\")||/^[A-Za-z][A-Za-z0-9+.-]*:/.test(e)||/[\x00-\x1f\x7f]/.test(e)||/^ | $/.test(e)||e.startsWith("/")?!1:!e.split("/").some(t=>{let n=t.replace(/%2e/gi,".");return n==="."||n===".."})}function Zi(e){return!Ue(e)||!GP(e.path)||typeof e.sha256!="string"||!UP.test(e.sha256)||!ft(e.bytes)?null:{path:e.path,sha256:e.sha256,bytes:e.bytes}}function HP(e){let t=e.input,n=e.decode;if(!Ue(t)||!ft(t.width)||!ft(t.height))return"detector.input must give a width and a height";if(!Ue(n))return"detector.decode must be an object";if(!tt(n.inputName)||!tt(n.outputName))return"detector.decode must name the model's input and output";let i=[["layout","NCHW"],["channels","BGR"],["scale","0-255"],["score","obj * max(cls)"],["class","argmax(cls)"]];for(let[o,a]of i)if(n[o]!==a)return`detector.decode.${o} is not ${JSON.stringify(a)}, the contract this runtime implements`;let u=n.letterbox;if(!Ue(u)||u.width!==t.width||u.height!==t.height)return"detector.decode.letterbox differs from detector.input";if(typeof u.pad!="number"||!Number.isInteger(u.pad)||u.pad<0||u.pad>255)return"detector.decode.letterbox.pad must be a whole number from 0 to 255";let l=[["anchor","top-left"],["resize","pillow-bilinear"],["ratio","min(height / frameHeight, width / frameWidth)"],["size","round(frame * ratio)"]];for(let[o,a]of l)if(u[o]!==a)return`detector.decode.letterbox.${o} is not ${JSON.stringify(a)}`;let d=n.classes;if(!Array.isArray(d)||d.length===0||!d.every(tt)||new Set(d).size!==d.length)return"detector.decode.classes must be a list of distinct class names";if(e.faceClass!==Rr||d[Rr]!=="face")return`detector.faceClass must be ${Rr} and that class must be named "face"`;let c=n.output,s=["cx","cy","w","h","obj",...d.map(o=>`cls_${o}`)];if(!Ue(c)||!Array.isArray(c.columns)||c.columns.length!==s.length||!c.columns.every((o,a)=>o===s[a]))return"detector.decode.output.columns must be cx, cy, w, h, obj and one cls_ per class";if(c.anchors!==wT(t.width,t.height))return`detector.decode.output.anchors must be ${wT(t.width,t.height)} for a ${t.width} x ${t.height} input`;if(!Ji(n.threshold)||!(n.threshold>0&&n.threshold<1))return"detector.decode.threshold must be a number between 0 and 1";if(n.threshold<.01)return`detector.decode.threshold ${n.threshold} is below 0.01, the lowest score at which the detector export checks box parity`;let r=n.nms;return!Ue(r)||!Ji(r.iou)||!(r.iou>0&&r.iou<=1)||r.classAgnostic!==!0||r.greedy!==!0?"detector.decode.nms must give an iou above 0 and at most 1, with classAgnostic and greedy both true":ft(n.maxProposals)?ft(e.minCropPx)?{inputName:n.inputName,outputName:n.outputName,letterbox:{width:t.width,height:t.height,pad:u.pad},anchors:c.anchors,classes:d,decode:{threshold:n.threshold,nmsIou:r.iou,maxProposals:n.maxProposals},faceClass:Rr,minCropPx:e.minCropPx}:"detector.minCropPx must be a whole number above 0":"detector.decode.maxProposals must be a whole number above 0"}function xT(e){let t=A=>({error:A});if(!Ue(e)||e.schemaVersion!==bT||e.kind!==yT)return t(`not a "${yT}" document of schema version ${bT}`);let n=Object.keys(e).filter(A=>!qP.includes(A));if(n.length>0)return t(`unknown key(s) ${n.sort().join(", ")}`);if(typeof e.id!="string"||!jP.test(e.id))return t("id must be 16 lowercase hex digits");if(!tt(e.game)||!tt(e.locale))return t("game and locale must be non-empty strings");let i=e.detector;if(!Ue(i))return t("incomplete release: detector missing (a release is all-or-nothing)");let u=Zi(i);if(!u||!tt(i.version)||!tt(i.weights))return t("detector must pin a file and carry a version and a weights label");if(i.arm!=="yolox")return t(`detector.arm ${JSON.stringify(i.arm)} is not one this runtime implements`);let l=HP(i);if(typeof l=="string")return t(l);let d=e.recognizer;if(!Ue(d))return t("incomplete release: recognizer missing (a release is all-or-nothing)");let c=Zi(d);if(!c||!tt(d.version)||!tt(d.weights)||!ft(d.dim))return t("recognizer must pin a file and carry a version, a weights label and a dim");if(!tt(d.inputName)||!tt(d.outputName))return t("recognizer must name the model's input and output");let s=hs(d.preprocessing);if(s===null)return t(`recognizer.preprocessing is not a contract this runtime implements (known: ${Object.keys(er).join(", ")})`);let r=e.bank;if(!Ue(r))return t("incomplete release: bank missing (a release is all-or-nothing)");let o=Zi(r);if(!o||!ft(r.version)||!ft(r.cardCount)||!ft(r.entryCount))return t("bank must pin a file and carry a version, a cardCount and an entryCount");if(r.aggregation!=="mean")return t('bank.aggregation must be "mean"');let a=e.identity;if(!Ue(a)||!qd(a.level))return t('identity.level must be "name" or "printing"');let p=a.groups,h=Zi(p);if(!h||!Ue(p)||!ft(p.version))return t("incomplete release: identity.groups must pin a file and carry a version");if(a.display!=="best-printing"&&a.display!=="representative")return t(`identity.display ${JSON.stringify(a.display)} is not one of best-printing, representative`);let m=e.catalog,g=Zi(m);if(!g||!Ue(m)||!ft(m.cardCount))return t("incomplete release: catalog must pin a file and carry a cardCount");let _=e.accept;if(!Ue(_))return t("incomplete release: accept missing (a release is all-or-nothing)");if(Object.keys(_).some(A=>!_T.includes(A)))return t(`accept has unknown key(s) (expected ${_T.join(", ")})`);if(!Ji(_.tau)||!(_.tau>0&&_.tau<=1))return t("accept.tau must be a number above 0 and at most 1");if(!Ji(_.delta)||!(_.delta>=0&&_.delta<1))return t("accept.delta must be a number from 0 to below 1");if(typeof _.rotationRetry!="boolean")return t("accept.rotationRetry must be true or false");let w=e.hover;if(!Ue(w)||!Q$(w.selection))return t("hover.selection is not one of accepted-nms, best-score, detector-score");if(!ft(w.maxAgeMs))return t("hover.maxAgeMs must be a whole number above 0");let y=e.runtime;if(!Ue(y)||typeof y.ortWeb!="string"||!VP.test(y.ortWeb))return t("runtime.ortWeb must be an exact version of the form 1.2.3");if(y.wasmThreads!==1)return t("runtime.wasmThreads must be 1");let T=y.executionProviders;if(!Array.isArray(T)||T.length===0||new Set(T).size!==T.length||!T.every(A=>_s.includes(A)))return t(`runtime.executionProviders must list one or more of ${_s.join(", ")}, each once`);let S=y.analysisWidth;if(!Ue(S)||!ft(S.min)||!ft(S.max)||S.min>S.max)return t("runtime.analysisWidth must give whole numbers min <= max above 0");let O=null;if("tiltedRetry"in e){let A=dT(e.tiltedRetry);if(A!==null)return t(A);let P=e.tiltedRetry;O={scales:[...P.scales],maxFacesPerFrame:P.maxFacesPerFrame,budgetMs:P.budgetMs}}return{release:{id:e.id,game:e.game,locale:e.locale,detector:{...u,arm:"yolox",version:i.version,weights:i.weights,settings:l},recognizer:{...c,version:d.version,weights:d.weights,dim:d.dim,inputName:d.inputName,outputName:d.outputName,preprocessing:s},bank:{...o,version:r.version,cardCount:r.cardCount,entryCount:r.entryCount},identity:{level:a.level,groups:{...h,version:p.version},display:a.display},catalog:{...g,cardCount:m.cardCount},accept:{tau:_.tau,delta:_.delta,rotationRetry:_.rotationRetry},hover:{selection:w.selection,maxAgeMs:w.maxAgeMs},runtime:{ortWeb:y.ortWeb,executionProviders:T,wasmThreads:1,analysisWidth:{min:S.min,max:S.max}},tiltedRetry:O}}}function WP(e){let t=0,n=()=>{throw new Error("not JSON")},i=()=>{for(;t{let c=t;for(e[t]!=='"'&&n(),t++;t=e.length&&n(),t++,JSON.parse(e.slice(c,t))},l=c=>{i();let s=e[t];if(s==="{"){t++;let o=[];if(i(),e[t]==="}")t++;else for(;;){i();let a=u();i(),e[t]!==":"&&n(),t++;let p=l(!1);if(c&&a==="id"||o.push([a,p]),i(),e[t]===",")t++;else if(e[t]==="}"){t++;break}else n()}return o.sort((a,p)=>a[0]p[0]?1:0),`{${o.map(([a,p])=>`${JSON.stringify(a)}:${p}`).join(",")}}`}if(s==="["){t++;let o=[];if(i(),e[t]==="]")t++;else for(;;)if(o.push(l(!1)),i(),e[t]===",")t++;else if(e[t]==="]"){t++;break}else n();return`[${o.join(",")}]`}if(s==='"')return JSON.stringify(u());let r=/^(?:true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/.exec(e.slice(t,t+64));return r?(t+=r[0].length,r[0]):n()};i(),e[t]!=="{"&&n();let d=l(!0);return i(),t!==e.length&&n(),d}async function KP(e,t){let n=new TextEncoder().encode(WP(e));return(await t(n.buffer)).slice(0,16)}async function XP(e){let t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t),n=>n.toString(16).padStart(2,"0")).join("")}var Jd={fetchFn:e=>fetch(e),digestSha256Hex:XP};function ZP(e,t){let n=l=>/^(\d+)\.(\d+)\.\d+/.exec(l),i=n(e),u=n(t);return i!==null&&u!==null&&i[1]===u[1]&&i[2]===u[2]}var We=class extends Error{};async function $T(e,t,n){let i;try{i=await e.fetchFn(t)}catch(u){throw new We(`${n}: fetch failed (${String(u)})`)}if(!i.ok)throw new We(`${n}: HTTP ${i.status}`);return i.arrayBuffer()}async function Yi(e,t,n,i){let u=await $T(e,new URL(n.path,t).href,i);if(u.byteLength!==n.bytes)throw new We(`${i}: ${n.path} is ${u.byteLength} bytes, the release says ${n.bytes}`);if(await e.digestSha256Hex(u)!==n.sha256)throw new We(`${i}: sha256 of ${n.path} does not match the release`);return u}function Ts(e,t){try{return JSON.parse(new TextDecoder("utf-8",{fatal:!0}).decode(e))}catch{throw new We(`${t}: the file is not JSON`)}}var YP=/^[a-z][a-zA-Z0-9]*$/,JP=["key","label","values","type"],vT=e=>typeof e=="string"?"text":"number",dr=e=>JSON.stringify(e)??String(e);function TT(e,t,n){for(let i=0;i!JP.includes(r));if(l.length>0)return`${u}: unknown key(s) ${l.join(", ")}`;if(typeof i.key!="string"||!YP.test(i.key))return`${u}: "key" ${dr(i.key)} must be a lowercase letter, then letters and digits only`;let d=i.key;if(d==="kind")return`${u}: "key" "kind" is reserved: the card's kind is always the first filter`;if(t.some(r=>r.key===d))return`${u}: duplicate key ${dr(d)}`;if(!tt(i.label))return`${u} (${d}): "label" must be a non-empty string`;let c=null;if("type"in i){if(i.type!=="text"&&i.type!=="number")return`${u} (${d}): "type" must be "text" or "number", not ${dr(i.type)}`;c=i.type}let s=null;if("values"in i){if(!Array.isArray(i.values)||i.values.length===0)return`${u} (${d}): "values" must be a non-empty list`;let r=TT(i.values,c,`${u} (${d}) values`);if(r!=="text"&&r!=="number")return r;c=r,s=i.values}t.push({key:d,label:i.label,type:c,values:s})}return t}function eC(e,t,n,i){let u=`card ${n}`;if(!Ue(e))return`${u}: "facets" must be an object of facet key to a value or a list of values`;for(let[l,d]of Object.entries(e)){let c=t.find(o=>o.key===l);if(c===void 0)return`${u}: facets.${l}: the catalog declares no facet ${dr(l)}`;let s=Array.isArray(d)?d:[d];if(s.length===0)return`${u}: facets.${l} is an empty list (leave the key out instead)`;let r=TT(s,c.type,`${u}: facets.${l}`);if(r!=="text"&&r!=="number")return r;c.type=r;for(let o of s){if(c.values!==null&&!c.values.includes(o))return`${u}: facets.${l}: ${dr(o)} is not one of the declared values`;let a=i.get(l)??[];a.includes(o)||a.push(o),i.set(l,a)}}return null}function tC(e){if(!Ue(e)||!tt(e.game)||!tt(e.locale)||!Array.isArray(e.cards)||e.cards.length===0)return"the file is not a catalog (game, locale and a list of cards)";let t=e.identity===void 0?"name":e.identity;if(!qd(t))return`"identity" must be "name" or "printing", not ${JSON.stringify(e.identity)}`;let n=e.facets===void 0?[]:QP(e.facets);if(typeof n=="string")return n;let i=new Map,u=new Map,l=[];for(let c of e.cards){if(!Ue(c)||!tt(c.id)||!tt(c.name))return"every card needs an id, a name and an art path";if(!("art"in c)||c.art!==null&&!tt(c.art))return`card ${c.id}: "art" must be an art path (a file path or an http(s) URL), or null for a card without art`;if(u.has(c.id))return`card id ${c.id} appears twice`;let s=c.printingOf??null;if(s!==null&&!tt(s))return`card ${c.id}: "printingOf" must be a card id or null`;let r=c.facets===void 0?null:eC(c.facets,n,c.id,i);if(r!==null)return r;if(c.extra!==void 0&&!Ue(c.extra))return`card ${c.id}: "extra" must be an object (any keys and values), not ${dr(c.extra)}`;let o={id:c.id,name:c.name,art:c.art,printingOf:s};c.extra!==void 0&&(o.extra=c.extra),u.set(o.id,o),l.push(o)}if(t==="printing")for(let c of l){if(c.printingOf===null)continue;let s=u.get(c.printingOf);if(s===void 0||s===c)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is not another card`;if(s.printingOf!==null)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is itself a printing of ${s.printingOf}`}let d=n.map(c=>{let s=c.values??[...i.get(c.key)??[]];return c.values===null&&c.type==="number"&&s.sort((r,o)=>r-o),{key:c.key,label:c.label,type:c.type??"text",values:[...s]}});return{game:e.game,locale:e.locale,identity:t,cards:l,facets:d}}async function Qd(e,t=Jd,n={}){try{let i=await $T(t,e,"release"),u=xT(Ts(i,"release"));if("error"in u)throw new We(`release: ${u.error}`);let l=u.release,d=await KP(new TextDecoder().decode(i),t.digestSha256Hex);if(d!==l.id)throw new We(`release: id ${l.id} is not the hash of the document (which is ${d})`);if(n.ortWeb!==void 0&&!ZP(l.runtime.ortWeb,n.ortWeb))throw new We(`release: measured on onnxruntime-web ${l.runtime.ortWeb}, this build runs ${n.ortWeb} (major and minor version must match)`);try{n.onDocument?.([l.detector,l.recognizer,l.bank,l.identity.groups,l.catalog].reduce((G,J)=>G+J.bytes,0))}catch{}let[c,s,r,o,a]=await Promise.all([Yi(t,e,l.detector,"detector"),Yi(t,e,l.recognizer,"recognizer"),Yi(t,e,l.bank,"bank"),Yi(t,e,l.identity.groups,"identity.groups"),Yi(t,e,l.catalog,"catalog")]);try{n.onFetched?.()}catch{}let p=new Uint8Array(r),h=Bd(p);if(h!==null&&h!==gs)throw new We(`bank: the binary bank is of format version ${h}, and this runtime reads version ${gs} only`);let m=zd(p)?X$(p):Nd(Ts(r,"bank"));if(!m)throw new We("bank: the file is not a bank this runtime can read");let g=[[m.version===l.bank.version,"bank.version differs from the bank file's"],[m.model===l.recognizer.version,`bank: the file's model ${JSON.stringify(m.model)} is not recognizer.version ${JSON.stringify(l.recognizer.version)}`],[m.dim===l.recognizer.dim,"bank: the vector size differs from recognizer.dim"],[m.preprocessing===l.recognizer.preprocessing,"bank: the file's preprocessing differs from recognizer.preprocessing"],[m.ids.length===l.bank.entryCount,"bank.entryCount is not the number of entries in the bank file"],[new Set(m.ids).size===l.bank.cardCount,"bank.cardCount is not the number of distinct card ids in the bank file"],[m.tau===null||m.tau===l.accept.tau,`bank: the file records tau ${m.tau}, accept.tau is ${l.accept.tau}`]];for(let[G,J]of g)if(!G)throw new We(J);let _=tC(Ts(a,"catalog"));if(typeof _=="string")throw new We(`catalog: ${_}`);if(_.game!==l.game||_.locale!==l.locale)throw new We("catalog: the file is for another game or locale");if(_.cards.length!==l.catalog.cardCount)throw new We(`catalog.cardCount ${l.catalog.cardCount} is not the file's ${_.cards.length} cards`);let w=new Map(_.cards.map(G=>[G.id,G.name])),y=m.ids.filter(G=>!w.has(G));if(y.length>0)throw new We(`${new Set(y).size} bank id(s) are not in the catalog (e.g. ${y[0]})`);let T=Ts(o,"identity.groups"),S=Vd(T);if(!S)throw new We("identity.groups: the file is not an identity-groups document");if(S.version!==l.identity.groups.version)throw new We("identity.groups.version differs from the file's");if((Ue(T)&&Ue(T.bank)?T.bank.sha256:void 0)!==l.bank.sha256)throw new We("identity.groups: the file was not made for this release's bank (its bank sha256 differs)");let A=_.identity;if(l.identity.level!==A)throw new We(`identity.level is "${l.identity.level}", and the catalog states the identity rule "${A}": a release is used with the rule its catalog states`);if((Ue(T)&&Ue(T.rule)?T.rule.level:void 0)!==A)throw new We(`identity.groups: the file's groups are not by ${A}`);let I=new Map(_.cards.map(G=>[G.id,rT(G,A)])),z=G=>JSON.stringify(G.map(J=>[...J].sort()).sort((J,q)=>J[0]q[0]?1:0));if(z(S.groups)!==z(nT(m.ids,I)))throw new We(`identity.groups: the groups are not the ones the catalog's ${A==="name"?"names give":"identity rule gives"} for the bank's ids`);return{ok:!0,data:{release:l,detectorBytes:new Uint8Array(c),recognizerBytes:new Uint8Array(s),bank:m,identityGroups:S,cards:_.cards,names:w}}}catch(i){return{ok:!1,reason:i instanceof We?i.message:`unexpected: ${String(i)}`}}}async function rC(e){for(let t of e)try{await t?.release?.()}catch{}}async function ST(e,t,n=e.release.runtime.executionProviders){let{release:i}=e,u=null,l=null,d=async c=>(await rC([u,l]),{error:c});try{if(n.length===0)return await d("no execution provider to try");let c=i.recognizer;return u=await t.embedder(e.recognizerBytes,{inputName:c.inputName,outputName:c.outputName,inputSize:er[c.preprocessing].size,dim:c.dim},n),u?(l=await t.detector(e.detectorBytes,i.detector.settings,n),l?{pipeline:new Ki({detector:l,embedder:u,bank:e.bank,accept:i.accept,identityGroups:e.identityGroups,identityDisplay:i.identity.display,names:e.names,hoverSelection:i.hover.selection,classNames:i.detector.settings.classes,faceClass:i.detector.settings.faceClass,minCropPx:i.detector.settings.minCropPx,tiltedRetry:i.tiltedRetry}),embedder:u,detector:l,providers:[...n]}:await d("detector: the model did not initialize on any execution provider, or is not the model the release describes")):await d("recognizer: the model did not initialize on any execution provider, or is not the model the release describes")}catch(c){return await d(`unexpected while building: ${String(c)}`)}}var Qi=class extends Error{constructor(n){super(`the release could not be loaded: ${n}`);this.reason=n;this.name="ReleaseError"}reason};function nC(e){return async t=>{if(!t.startsWith(Wn))throw new Error(`${t} is not one of the release's files`);let n=U$(t.slice(Wn.length)),i=Object.hasOwn(e,n)?e[n]:void 0;if(i===void 0)throw new Error(`no file ${n} among the release's files`);return{ok:!0,status:200,arrayBuffer:()=>i.arrayBuffer()}}}var Dr=e=>Math.round(e);function iC(e){let t=(n,i)=>{let u=performance.now()-i;return{createMs:Dr(n.createMs),firstRunMs:Dr(n.firstRunMs),failedProvidersMs:Dr(Math.max(0,u-n.createMs-n.firstRunMs)),totalMs:Dr(u)}};return{embedder:async(n,i,u)=>{let l=performance.now(),d=await ji.create(n,i,u);return d&&(e.recognizer=t(d.setup,l)),d},detector:async(n,i,u)=>{let l=performance.now(),d=await Li.create(n,i,u);return d&&(e.detector=t(d.setup,l)),d}}}async function oC(e,t={}){let n=t.files?Wn:typeof location<"u"?location.href:void 0,i=new URL(e,n).href,u=g=>{try{t.onProgress?.(g)}catch{}},l=performance.now();u({phase:"downloading",bytes:null});let d={...Jd,...t.deps,...t.files?{fetchFn:nC(t.files)}:{}},c=null,s=await Qd(i,d,{ortWeb:k$(),onFetched:()=>{c=performance.now()},...t.onProgress?{onDocument:g=>u({phase:"downloading",bytes:g})}:{}});if(!s.ok)throw new Qi(s.reason);let r=performance.now(),o=(c??r)-l,a=r-(c??r);u({phase:"preparing"});let{data:p}=s;P$({wasmThreads:p.release.runtime.wasmThreads,...t.wasmPaths!==void 0?{wasmPaths:t.wasmPaths}:{}}),Od();let h={},m=await ST(p,iC(h),t.providers??p.release.runtime.executionProviders);if("error"in m){let g=Od();throw new Qi(g.length>0?`${m.error} (${g.join("; ")})`:m.error)}return{release:p.release,pipeline:m.pipeline,backend:{detector:m.detector.ep,recognizer:m.embedder.ep},cards:p.cards,names:p.names,stageInputs:{bankSha256:p.release.bank.sha256,bankIds:[...new Set(p.bank.ids)],identities:j$(p.cards,p.release.identity.level,p.identityGroups)},...h.recognizer&&h.detector?{timings:{startedAtMs:Dr(l),filesMs:Dr(o),readMs:Dr(a),recognizer:h.recognizer,detector:h.detector,totalMs:Dr(performance.now()-l)}}:{},dispose:async()=>{await m.embedder.release?.(),await m.detector.release?.()}}}function sC(e,t,n){return e.pipeline.analyze(t,n)}function Ss(e){return e==="wasm"?2e3:1e3}function aC(e){return e==="wasm"?1e3:500}function Is(e,t){return t==="always-on"?Ss(e):aC(e)}var uC=1920,lC=720;function As(e,t,n={min:720,max:1920}){let i=Math.max(n.min,Math.min(n.max,e));return{width:i,height:Math.max(1,Math.round(i*t/e))}}function ET(e,t,n){if(!(e.width>0&&e.height>0))return null;let i=(t-e.left)/e.width,u=(n-e.top)/e.height;return i>=0&&i<=1&&u>=0&&u<=1?{x:i,y:u}:null}function Zn(e){return e instanceof HTMLVideoElement?{width:e.videoWidth,height:e.videoHeight}:{width:e.naturalWidth,height:e.naturalHeight}}function dC(e){return e instanceof HTMLVideoElement?e.currentTime:0}async function ec(e,t,n,i){let u=Zn(e),l=As(u.width,u.height,i);t.width=l.width,t.height=l.height;let d=performance.now(),c=dC(e);return n.drawImage(e,0,0,t.width,t.height),{bitmap:await createImageBitmap(t),videoTimeSec:c,tapMs:performance.now()-d}}var IT=128,OT=.06,cC=.5,AT=60,Es=class{ref=null;seeded=!1;seed(t){this.ref=t,this.seeded=!0}reset(){this.ref=null,this.seeded=!1}get hasReference(){return this.ref!==null}observe(t,n){let i=this.ref;if(i===null||i.width!==t.width||i.height!==t.height)return this.ref=t,this.seeded=!1,{distance:null,cut:!1};let u=fC(i,t,n);return u===null?{distance:u,cut:!1}:u>OT?{distance:u,cut:!0}:((this.seeded||u<=OT*cC)&&(this.ref=t),this.seeded=!1,{distance:u,cut:!1})}};function pC(e,t){return{width:IT,height:Math.max(1,Math.round(IT*t/Math.max(1,e)))}}function hC(e,t,n){let i=new Uint8Array(t*n);for(let u=0,l=0;u>8;return i}function fC(e,t,n){if(e.width!==t.width||e.height!==t.height||!(n.width>0)||!(n.height>0))return null;let{width:i,height:u}=e,l=Os(Math.floor(n.x*i),0,i-1),d=Os(Math.ceil((n.x+n.width)*i),l+1,i),c=Os(Math.floor(n.y*u),0,u-1),s=Os(Math.ceil((n.y+n.height)*u),c+1,u),r=0;for(let o=c;o{s=!1});function i(p){return d.find(h=>h.ticket===p)}function a(p){p.fence&&t.deleteSync(p.fence),p.fence=null,p.ticket=null}return{get usable(){return s&&!t.isContextLost()},sample(p){let h=Rn(p);if(!this.usable||h.width===0||h.height===0)return null;let m=d.find(y=>y.ticket===null);if(!m){if(d.length>=Yk)return null;let y=t.createBuffer();if(!y)return null;m={pbo:y,fence:null,width:0,height:0,ticket:null},d.push(m)}let{width:b,height:$}=Kk(h.width,h.height);try{(e.width!==b||e.height!==$)&&(e.width=b,e.height=$),t.viewport(0,0,b,$),t.bindTexture(t.TEXTURE_2D,l),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,p),t.generateMipmap(t.TEXTURE_2D),t.drawArrays(t.TRIANGLE_STRIP,0,4),t.bindBuffer(t.PIXEL_PACK_BUFFER,m.pbo),(m.width!==b||m.height!==$)&&(t.bufferData(t.PIXEL_PACK_BUFFER,b*$*4,t.STREAM_READ),m.width=b,m.height=$),t.readPixels(0,0,b,$,t.RGBA,t.UNSIGNED_BYTE,0),t.bindBuffer(t.PIXEL_PACK_BUFFER,null),m.fence=t.fenceSync(t.SYNC_GPU_COMMANDS_COMPLETE,0),t.flush()}catch{return s=!1,null}if(!m.fence)return null;let _={id:c++};return m.ticket=_,_},read(p){let h=i(p);if(!h||!h.fence||!this.usable||t.getSyncParameter(h.fence,t.SYNC_STATUS)!==t.SIGNALED)return null;let m=h.width*h.height*4;r.length!==m&&(r=new Uint8Array(m)),t.bindBuffer(t.PIXEL_PACK_BUFFER,h.pbo),t.getBufferSubData(t.PIXEL_PACK_BUFFER,0,r),t.bindBuffer(t.PIXEL_PACK_BUFFER,null);let b={width:h.width,height:h.height,luma:Xk(r,h.width,h.height)};return a(h),b},release(p){if(!p)return;let h=i(p);h&&a(h)},dispose(){if(s=!1,!t.isContextLost()){for(let p of d)a(p),t.deleteBuffer(p.pbo);d.length=0,t.getExtension("WEBGL_lose_context")?.loseContext()}}}}function Sd(e,t,n){let o={left:e.left,top:e.top,width:e.width,height:e.height};if(t.width<=0||t.height<=0||n==="fill")return o;let u=e.width/t.width,l=e.height/t.height,d;n==="cover"?d=Math.max(u,l):n==="none"?d=1:n==="scale-down"?d=Math.min(1,u,l):d=Math.min(u,l);let c=t.width*d,s=t.height*d;return{left:e.left+(e.width-c)/2,top:e.top+(e.height-s)/2,width:c,height:s}}var tP=250,U$=250,rP=1e4,nP=5e3,iP=400,Id=2e3;function V$(e,t,n){return Math.abs(t-e)*1e3<=n}var oP=250,q$=5e3,j$=[250,500,1e3,2e3];function Od(e,t){return e===void 0?"stale-request":e.requestId<=t.lastAcceptedRequestId?"superseded":e.requestId!==t.inFlightRequestId?"stale-request":e.videoKey!==t.videoKey?"video-key":e.generation!==t.generation?"generation":null}function G$(e){return e==="video-key"||e==="generation"}var Mi=class{constructor(t,n=3,o="always-on",u=t){this.intervalMs=t;this.confirmFrames=n;this.mode=o;this.readingIntervalMs=u}intervalMs;confirmFrames;mode;readingIntervalMs;pending=!1;repeatsLeft=0;lastStartedAt=-1/0;pointerInside=!1;lastPointerAt=-1/0;tooltipShowing=!1;lastTooltipAt=-1/0;lastHoverTriggerAt=-1/0;lastVideoTimeSec=null;discontinuitySinceLast=!1;lastTriggerKind=null;lookRequested=!1;unanswered=0;retryAt=null;retryPrompt=!1;lastUnansweredCapture=!1;refused=!1;setIntervalMs(t){this.intervalMs=t}setReadingIntervalMs(t){this.readingIntervalMs=t}setTooltipShowing(t,n){this.tooltipShowing=t,t&&this.pointerInside&&(this.lastTooltipAt=n)}setMode(t){this.mode=t}get analysisMode(){return this.mode}framePresented(){this.pending=!0,this.repeatsLeft=0}discontinuity(){this.discontinuitySinceLast=!0,this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}requestLook(){this.discontinuitySinceLast=!0,this.pending=!0,this.repeatsLeft=0,this.lookRequested=!0}lookUnanswered(t,n={}){this.repeatsLeft=0,this.retryAt=t+j$[Math.min(this.unanswered,j$.length-1)],this.retryPrompt=n.prompt===!0,this.lastUnansweredCapture=n.prompt===!0,this.unanswered+=1}lookAnswered(){this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}lookRefused(){this.refused=!0}pictureReceived(){this.refused=!1,this.lastUnansweredCapture&&(this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1)}retryReady(t){return this.retryAt!==null&&t>=this.retryAt}get lookPending(){return this.lookRequested}pointerMoved(t){this.pointerInside=!0,this.lastPointerAt=t,(this.unanswered>0||this.refused)&&this.retryAt===null&&(this.retryAt=t,this.retryPrompt=!1)}pointerLeft(t){this.pointerInside&&(this.lastPointerAt=t),this.pointerInside=!1,this.lastTooltipAt=-1/0}tapped(t){this.lastPointerAt=t}pointerActive(t){return t-this.lastPointerAt=this.intervalInEffectMs(t):this.repeatsLeft>0&&n>=tP}staleFrameMs(t){return Math.max(iP,this.intervalInEffectMs(t))}frameStale(t,n){return this.lastVideoTimeSec===null||this.discontinuitySinceLast?!0:Math.abs(t-this.lastVideoTimeSec)*1e3>this.staleFrameMs(n)}hoverTriggerDue(t,n,o){return o||!this.analysisActive(t)||t-this.lastHoverTriggerAt0&&(this.repeatsLeft-=1)}get lastTrigger(){return this.lastTriggerKind}msSinceLastAnalysis(t){return this.lastStartedAt===-1/0?null:t-this.lastStartedAt}get idle(){return!this.pending&&this.repeatsLeft===0&&this.retryAt===null}};var us="no picture",aP="no picture (the paused video draws nothing)",Ed="no 2d context",uP="the video may not be read",lP="the picture is too large",dP=16,cP=9,H$=()=>document.createElement("canvas"),pP=e=>createImageBitmap(e);function Ad(){return{canvas:null,readable:!1}}function W$(e){return e.paused&&e.readyState<2&&e.videoWidth>0}function kd(e,t){let n=t?.lastPlayingTime??null;return W$(e)&&n!==null?n:e.currentTime}function hP(e,t=H$){let n=dP,o=t();o.width=n,o.height=n;let u=o.getContext("2d");if(!u)return!1;u.drawImage(e,0,0,n,n);let l=u.getImageData(0,0,n,n).data,d=0,c=0;for(let r=0;r({kind:"failed",reason:h,retry:m}),u=e?.video;if(!e||!u||(n.requireConnected??!0)&&!u.isConnected||u.videoWidth===0)return o(us,!0);let l=W$(u);if(u.readyState<2&&!l)return o(us,!0);let d=n.createCanvas??H$,c=as(u.videoWidth,u.videoHeight,t),s=n.maxSide??4096;if(c.width>s||c.height>s)return o(lP);let r=e.source;r.canvas||(r.canvas=d(),r.readable=!1);let i=r.canvas;i.width=c.width,i.height=c.height;let a=i.getContext("2d");if(!a)return o(Ed);let p=kd(u,e);try{return a.drawImage(u,0,0,c.width,c.height),l&&hP(i,d)?(r.readable=!0,o(aP,!0)):(r.readable||(a.getImageData(0,0,1,1),r.readable=!0),{kind:"frame",bitmap:await(n.createImageBitmap??pP)(i),mediaTime:p})}catch(h){return o(fP(h)?uP:String(h))}}function X$(e,t){let n=null,o=Ad(),u={video:e,get lastPlayingTime(){return n},get source(){return o},mediaTime:()=>kd(e,u),detach:a},l=()=>t.discontinuity(!1),d=()=>{o=Ad(),n=null,t.discontinuity(!0)},c=()=>{!e.paused&&e.readyState>=2&&(n=e.currentTime),t.presented(u.mediaTime())},s=()=>t.presented(u.mediaTime()),r=[["seeking",l],["emptied",d],["loadeddata",d],["timeupdate",c],["seeked",c],["canplay",s],["playing",s]];for(let[p,h]of r)e.addEventListener(p,h);let i=!0;function a(){if(i){i=!1;for(let[p,h]of r)e.removeEventListener(p,h)}}return u}var R4=1440*60*1e3;var mP=[/^the worker did not start/,/^unexpected error while loading/];function Z$(e,t){return t||e.startsWith("unexpected:")?!1:!mP.some(n=>n.test(e))}var U4=Object.freeze({misses:2,maxMs:5e3,sameBoxIou:.5});function gP(e,t){return e==="loading"?{phase:t.phase,bytes:t.bytes}:void 0}function bP(e,t,n=40){return(e.hover?.cards??[]).map(o=>o.bounds).filter(o=>o&&!(Math.abs(o.x-t.x)<1e-6&&Math.abs(o.y-t.y)<1e-6&&Math.abs(o.width-t.width)<1e-6)).slice(0,n).map(o=>({x:o.x,y:o.y,width:o.width,height:o.height}))}var ls=class{constructor(t,n={}){this.deps=t;this.mode=n.mode??"pointer",this.hold=n.hold??null,this.maxOtherBoxes=n.maxOtherBoxes??40,this.hitTest=t.hoverAt??Qo,this.scheduler=new Mi(is(null,this.mode),3,this.mode,ns(null)),this.st={state:"loading",error:null,release:null,backend:null,schedule:"idle",frames:0,lastWorkerMs:null,lastRoundTripMs:null,lastFrameError:null,geometric:n.geometric?{state:"loading",reason:null,bytes:null}:null,loading:null},this.lastStatusKey=this.statusKey()}deps;scheduler;mode;hold;maxOtherBoxes;hitTest;st;cardList=[];analysisWidth={min:720,max:1920};maxAgeMs=Id;nextRequestId=1;inFlight=null;lastAcceptedRequestId=0;generation=0;sourceCount=0;result=null;pointerAt=null;mediaTime=0;lastShownKey="null";lastStatusKey="";held=null;get videoKey(){return`source-${this.sourceCount}`}pointer(t,n,o,u=!1){if(this.st.state==="stopped")return;let l=this.deps.now();this.pointerAt={x:t,y:n},this.mediaTime=o,u?this.scheduler.tapped(l):this.scheduler.pointerMoved(l),this.st.state==="ready"&&this.scheduler.hoverTriggerDue(l,this.videoTime(),this.inFlight!==null)&&this.requestCapture("hover"),this.publish()}pointerLeft(){this.st.state!=="stopped"&&(this.pointerAt=null,this.scheduler.pointerLeft(this.deps.now()),this.publish())}presented(t){this.st.state!=="stopped"&&(this.mediaTime=t,this.scheduler.framePresented(),this.publish())}discontinuity(t){this.st.state!=="stopped"&&(this.generation+=1,t&&(this.sourceCount+=1),this.scheduler.discontinuity(),this.result=null,this.held=null,t&&this.scheduler.framePresented(),this.publish())}cut(){this.st.state!=="stopped"&&(this.generation+=1,this.result=null,this.held=null,this.scheduler.requestLook(),this.publish())}frame(t,n,o){let u=this.inFlight;if(this.st.state==="stopped"||!u||u.requestId!==t||u.analyzing){n.close();return}if(this.scheduler.pictureReceived(),u.generation!==this.generation||u.videoKey!==this.videoKey){this.inFlight=null,n.close(),this.publish();return}u.analyzing=!0;let l={requestId:u.requestId,mediaTimeSec:o,generation:u.generation,videoKey:u.videoKey};try{this.deps.toWorker({kind:"frame",bitmap:n,videoTimeSec:o,envelope:l},[n])}catch(d){n.close(),this.inFlight=null,this.st.lastFrameError=`analysis-failed: ${String(d)}`,this.scheduler.lookUnanswered(this.deps.now())}this.publish()}captureFailed(t,n,o=!1){let u=this.inFlight;this.st.state==="stopped"||!u||u.requestId!==t||u.analyzing||(this.inFlight=null,this.st.lastFrameError=`capture-failed: ${n}`,o?this.scheduler.lookUnanswered(this.deps.now(),{prompt:!0}):this.scheduler.lookRefused(),this.publish())}fromWorker(t){if(this.st.state!=="stopped")switch(t.kind){case"geometric-status":this.st.geometric={state:t.state,reason:t.reason,bytes:t.bytes},this.deps.onGeometricStatus?.(t),this.publishStatus();return;case"release-progress":{let n=gP(this.st.state,t);n!==void 0&&(this.st.loading=n),this.publishStatus();return}case"release-status":this.st.state=t.ok?"ready":"failed",this.st.error=t.error,this.st.release=t.release,this.st.loading=null,this.cardList=t.cards,t.release&&(this.st.backend=t.release.recognizer.ep,this.maxAgeMs=t.release.hoverMaxAgeMs,this.analysisWidth={...t.release.analysisWidth},this.scheduler.setIntervalMs(is(this.st.backend,this.mode)),this.scheduler.setReadingIntervalMs(ns(this.st.backend)),this.scheduler.framePresented()),this.publish();return;case"frame-result":this.onFrameResult(t);return;default:return}}onFrameResult(t){let n=this.deps.now(),o=Od(t.envelope,{inFlightRequestId:this.inFlight?.requestId??null,lastAcceptedRequestId:this.lastAcceptedRequestId,generation:this.generation,videoKey:this.videoKey});if(o!==null&&!G$(o))return;let u=this.inFlight;if(this.inFlight=null,o!==null){this.publish();return}this.lastAcceptedRequestId=t.envelope.requestId,this.st.lastFrameError=t.error??null,this.st.lastWorkerMs=t.workerMs,this.st.lastRoundTripMs=+(n-u.sentAt).toFixed(2),t.analysis?(this.scheduler.lookAnswered(),this.st.frames+=1,this.result={analysis:t.analysis,videoTimeSec:t.videoTimeSec},this.deps.onAnalysis?.(t.analysis)):(this.result=null,t.error===a$&&this.scheduler.lookUnanswered(n)),this.held&&this.pointerAt&&this.current()&&!this.fresh()&&(this.held.misses+=1),this.scheduler.setTooltipShowing(this.shown()!==null,n),this.publish()}tick(){if(this.st.state==="stopped")return;let t=this.deps.now();this.inFlight&&t-this.inFlight.sentAt>=q$&&(this.inFlight=null,this.st.lastFrameError="no reply",this.scheduler.lookUnanswered(t)),this.scheduler.setTooltipShowing(this.shown()!==null,t),this.st.state==="ready"&&!this.inFlight&&this.scheduler.due(t)&&this.requestCapture(this.scheduler.lookPending?"cut":void 0),this.publish()}stop(){this.st.state!=="stopped"&&(this.st.state="stopped",this.st.loading=null,this.inFlight=null,this.result=null,this.held=null,this.pointerAt=null,this.publishShown(),this.publishStatus())}workerStopped(t){if(!(this.st.state==="stopped"||this.st.state==="failed")){if(this.inFlight=null,this.result=null,this.held=null,this.st.loading=null,this.publishShown(),"restart"in t){let n={kind:"geometric-status",state:"off",reason:"worker-restarted",bytes:null};this.st.geometric&&(this.st.geometric={state:n.state,reason:n.reason,bytes:n.bytes}),this.deps.onGeometricStatus?.(n),this.st.state="loading",this.st.error=null}else this.st.state="failed",this.st.error=t.error;this.publishStatus()}}reloading(t){this.st.state==="loading"&&(this.st.geometric=t.geometric?{state:"loading",reason:null,bytes:null}:null,this.st.loading=null,this.publishStatus())}requestCapture(t){if(this.st.state!=="ready"||this.inFlight)return;let n=this.deps.now(),o=this.nextRequestId++;this.inFlight={requestId:o,sentAt:n,generation:this.generation,videoKey:this.videoKey,analyzing:!1},this.scheduler.started(n,{videoTimeSec:this.videoTime(),...t?{trigger:t}:{}}),this.deps.capture(o,{...this.analysisWidth})}status(){return{...this.st,schedule:this.scheduler.analysisState(this.deps.now())}}analysis(){return this.current()?.analysis??null}cards(){return this.cardList}hitAt(t,n){let o=this.current();if(!o)return null;let{width:u,height:l}=o.analysis.frame;if(!(u>0&&l>0))return null;let d=t*u,c=n*l;if(!Number.isFinite(d)||!Number.isFinite(c))return null;let s=this.hitTest(o.analysis,d,c);if(!s.box||s.cardId===null||s.name===null)return null;let r={x:s.box.x/u,y:s.box.y/l,width:s.box.width/u,height:s.box.height/l},i={x:s.box.x,y:s.box.y,width:s.box.width,height:s.box.height};return{cardId:s.cardId,name:s.name,box:r,frameBox:i,others:bP(o.analysis,r,this.maxOtherBoxes)}}fresh(){return this.pointerAt?this.hitAt(this.pointerAt.x,this.pointerAt.y):null}shown(){let t=this.fresh(),n=this.hold;if(!n)return t;let o=this.deps.now();if(t)return this.held={card:t,misses:0,seenAt:o},t;let u=this.held;return!u||!this.pointerAt||!_P(u.card.box,this.pointerAt)?(this.held=null,null):this.boxAtPointer(u.card.box,n.sameBoxIou)==="other"||u.misses>=n.misses||o-u.seenAt>=n.maxMs?(this.held=null,null):u.card}current(){let t=this.result;return t&&V$(t.videoTimeSec,this.videoTime(),this.maxAgeMs)?t:null}videoTime(){return this.deps.mediaTimeNow?this.deps.mediaTimeNow():this.mediaTime}boxAtPointer(t,n){let o=this.current();if(!o||!this.pointerAt)return"unknown";let{width:u,height:l}=o.analysis.frame;if(!(u>0&&l>0))return"unknown";let d=this.pointerAt.x*u,c=this.pointerAt.y*l,s={x:t.x*u,y:t.y*l,width:t.width*u,height:t.height*l},r=0,i=!1;for(let a of o.analysis.boxes??[]){let[p,h,m,b]=a.box;dp+m||ch+b||(i=!0,r=Math.max(r,yP(s,{x:p,y:h,width:m,height:b})))}return i?r>=n?"same":"other":"none"}publish(){this.publishShown(),this.publishStatus()}publishShown(){let t=this.shown(),n=t?`${t.cardId}|${t.box.x}|${t.box.y}|${t.box.width}|${t.box.height}`:"null";n!==this.lastShownKey&&(this.lastShownKey=n,this.deps.onShown?.(t))}statusKey(){let t=this.status();return JSON.stringify({...t,release:t.release?t.release.id:null})}publishStatus(){let t=this.statusKey();t!==this.lastStatusKey&&(this.lastStatusKey=t,this.deps.onStatus?.(this.status()))}};function yP(e,t){let n=Math.min(e.x+e.width,t.x+t.width)-Math.max(e.x,t.x),o=Math.min(e.y+e.height,t.y+t.height)-Math.max(e.y,t.y);if(n<=0||o<=0)return 0;let u=n*o;return u/(e.width*e.height+t.width*t.height-u)}function _P(e,t){return t.x>=e.x&&t.x<=e.x+e.width&&t.y>=e.y&&t.y<=e.y+e.height}var J$=1;function wP(e){let{geometricUrl:t,geometricManifestSha256:n,geometricWatchdog:o,...u}=e;return u}function Q$(e){let t=null,n=null,o=0,u=!1,l=null,d=!1,c=null,s={source:e.chooseRelease?"choosing":"bundled",id:null,reason:null,table:null},r=_=>{e.onDecision?.({reused:0,downloaded:0,ms:0,..._})},i=_=>new ls({now:e.now,toWorker:(y,T)=>t?.postMessage(y,T??[]),capture:e.capture,...e.onShown?{onShown:e.onShown}:{},...e.onStatus?{onStatus:e.onStatus}:{},...e.onAnalysis?{onAnalysis:e.onAnalysis}:{},...e.onGeometricStatus?{onGeometricStatus:e.onGeometricStatus}:{},...e.hoverAt?{hoverAt:e.hoverAt}:{},...e.mediaTimeNow?{mediaTimeNow:e.mediaTimeNow}:{}},{...e.mode?{mode:e.mode}:{},...e.hold?{hold:e.hold}:{},geometric:_.geometricUrl!==void 0}),a=_=>{u||!n||(l&&_.kind==="release-progress"&&_.phase==="preparing"&&(d=!0),!(_.kind==="release-status"&&(n.status().state!=="loading"||l&&!p(_)))&&n.fromWorker(_))},p=_=>{let y=l;if(_.ok&&_.release?.id===y.pointer.id)return l=null,c=y.pointer.id,s.source="remote",s.id=y.pointer.id,s.reason="ok",s.table=y.facts,r({source:"remote",reason:"ok",pointer:y.pointer.id,reused:y.reused,downloaded:y.downloaded,ms:y.ms}),!0;if(_.ok)return e.recordSkip?.(y.pointer,"id-mismatch"),h("id-mismatch"),!1;let T=_.error??"no reason given";return Z$(T,d)&&e.recordSkip?.(y.pointer,`runtime:${T.slice(0,120)}`),h(`runtime:${T}`),!1},h=_=>{let y=l;l=null,t?.terminate(),s.source="bundled",s.id=null,s.reason=_,s.table=null,r({source:"bundled",reason:_,pointer:y.pointer.id,reused:y.reused,downloaded:y.downloaded,ms:y.ms});let T=y.fallback??e.loadRequest;n?.reloading({geometric:T.geometricUrl!==void 0}),$(T)},m=_=>{if(u||!n)return;let y=n.status().state;if(y==="failed"||y==="stopped")return;let T=_.message||"see the console";if(l){p({kind:"release-status",ok:!1,error:`the worker did not start (${T})`,release:null,cards:[]});return}if(t?.terminate(),y==="loading"&&o===0){n.workerStopped({error:`the worker did not start (${T})`});return}if(o{let y=e.createWorker();y.onmessage=T=>{y===t&&a(T.data)},y.onerror=T=>{y===t&&m(T)},t=y,y.postMessage(_)},$=_=>{n??=i(_);try{b(_)}catch(y){t=null,l=null,u=!0,n.workerStopped({error:`the recognition worker could not start (${String(y)})`})}};return e.chooseRelease?e.chooseRelease({onDownloading:()=>e.onDownloading?.()}).catch(_=>({source:"bundled",reason:`unexpected:${String(_)}`,pointer:null,downloaded:0,ms:0})).then(_=>{if(!u){if(_.source==="remote"){l=_,d=!1,$(_.request);return}s.source="bundled",s.reason=_.reason,r({source:"bundled",reason:_.reason,pointer:_.pointer?.id??null,downloaded:_.downloaded,ms:_.ms}),$(_.request??e.loadRequest)}}):$(e.loadRequest),{get core(){return n},get stopped(){return u},get restarts(){return o},get release(){return{...s}},tick(){u||n?.tick()},stop(){u=!0,l=null,t?.terminate(),t=null,n?.stop()}}}var vP={requireConnected:!1,maxSide:1/0};function xP(e,t){let n=e instanceof HTMLVideoElement?e:null,o=e,u=t.workerUrl??new URL("./worker.js",import.meta.url),l=()=>performance.now(),d=!1,c=0,s=!1,r=()=>{c>0?s=!0:d&&t.onUpdate?.()},i=(M,ne=!1)=>{c+=1;try{M()}finally{c-=1}ne&&(s=!0),c===0&&s&&(s=!1,d&&t.onUpdate?.())},a=null,p=new ss,h=null,m=null,b=-1/0,$=()=>{let M=h&&a?a.read(h):null;M?p.seed(M):(a?.release(h),p.reset()),h=null},_=n?null:document.createElement("canvas"),y=_?.getContext("2d")??null,T=async M=>{if(!_||!y)return{kind:"failed",reason:Ed,retry:!1};let ne=Rn(e);if(ne.width===0||ne.height===0)return{kind:"failed",reason:us,retry:!0};try{let Q=await Td(e,_,y,M);return{kind:"frame",bitmap:Q.bitmap,mediaTime:Q.videoTimeSec}}catch(Q){return{kind:"failed",reason:String(Q),retry:!1}}},S=(M,ne)=>{a?.release(h),h=a?.usable?a.sample(e):null,(L?K$(L,ne,vP):T(ne)).then(Q=>i(()=>{Q.kind==="frame"?q.frame(M,Q.bitmap,Q.mediaTime):q.captureFailed(M,Q.reason,Q.retry)},Q.kind==="failed"))},O=0,A=null,P=()=>{O+=1;let M;try{M=new Worker(u,{type:"module"})}catch(Q){throw O===1&&(A={error:Q}),Q}let ne={postMessage:(Q,le)=>M.postMessage(Q,le??[]),terminate:()=>M.terminate(),onmessage:null,onerror:null};return M.onmessage=Q=>i(()=>ne.onmessage?.(Q)),M.onerror=Q=>i(()=>ne.onerror?.(Q)),ne},I={kind:"load",releaseUrl:t.releaseFiles?String(t.releaseUrl):new URL(t.releaseUrl,location.href).href,...t.releaseFiles?{files:t.releaseFiles}:{},...t.wasmPaths!==void 0?{wasmPaths:t.wasmPaths}:{},...t.providers?{providers:t.providers}:{},...t.geometricUrl!==void 0?{geometricUrl:new URL(t.geometricUrl,location.href).href}:{},...t.geometricManifestSha256!==void 0?{geometricManifestSha256:t.geometricManifestSha256}:{},...t.geometricWatchdog===!1?{geometricWatchdog:!1}:{}},L=n?X$(n,{presented:M=>i(()=>q.presented(M)),discontinuity:M=>i(()=>{p.reset(),q.discontinuity(M)},!0)}):null,V=()=>L?.mediaTime()??0,Z=Q$({createWorker:P,loadRequest:I,now:l,...t.mode?{mode:t.mode}:{},capture:S,mediaTimeNow:()=>V(),onShown:r,onStatus:r,onAnalysis:$});if(A)throw L?.detach(),Z.stop(),A.error;let q=Z.core,N=(M,ne)=>{let Q=Sd(e.getBoundingClientRect(),Rn(e),getComputedStyle(e).objectFit);if(Q.width<=0||Q.height<=0)return null;let le=q.hitAt((M-Q.left)/Q.width,(ne-Q.top)/Q.height);return le?{hit:le,picture:Q}:null},ue=(M,ne)=>{let Q=N(M,ne);if(!Q)return null;let{hit:le,picture:Ae}=Q;return{name:le.name,cardId:le.cardId,box:{left:Ae.left+le.box.x*Ae.width,top:Ae.top+le.box.y*Ae.height,width:le.box.width*Ae.width,height:le.box.height*Ae.height},frameBox:le.frameBox}},w=null,C=()=>w?ue(w.x,w.y):null,U=M=>{let ne=w?N(w.x,w.y)?.hit.box??null:null;if(!ne){a?.release(m),m=null;return}if(a||(a=L$()),!!a?.usable){if(m){let Q=a.read(m);if(Q&&(m=null,p.observe(Q,ne).cut)){p.reset(),q.cut(),s=!0;return}}!m&&M-b>=F$&&(b=M,m=a.sample(e))}},H=null,G=()=>{!d||!n||(i(()=>{q.presented(V()),U(l())}),H=n.requestVideoFrameCallback(G))},J=()=>i(()=>{p.reset(),q.discontinuity(!0)},!0),B=M=>i(()=>{w={x:M.clientX,y:M.clientY};let ne=Sd(e.getBoundingClientRect(),Rn(e),getComputedStyle(e).objectFit),Q=B$(ne,M.clientX,M.clientY)??{x:(M.clientX-ne.left)/ne.width,y:(M.clientY-ne.top)/ne.height};q.pointer(Q.x,Q.y,V(),M.pointerType==="touch")},!0),X=()=>i(()=>{w=null,q.pointerLeft()},!0);n&&"requestVideoFrameCallback"in n&&(H=n.requestVideoFrameCallback(G)),n||e.addEventListener("load",J),o.addEventListener("pointermove",B),o.addEventListener("pointerdown",B),o.addEventListener("pointerleave",X);let K=setInterval(()=>i(()=>Z.tick()),U$);return d=!0,{status:()=>q.status(),at:ue,atPointer:C,analysis:()=>q.analysis(),cards:()=>q.cards(),stop:()=>{d&&(d=!1,clearInterval(K),H!==null&&n&&"cancelVideoFrameCallback"in n&&n.cancelVideoFrameCallback(H),H=null,L?.detach(),e.removeEventListener("load",J),o.removeEventListener("pointermove",B),o.removeEventListener("pointerdown",B),o.removeEventListener("pointerleave",X),a?.release(h),a?.release(m),a?.dispose(),p.reset(),Z.stop())}}}export{ts as DEFAULT_ACCEPT,nd as DEFAULT_DECODE,Ho as DEFAULT_LETTERBOX,$i as Detector,Jo as EXECUTION_PROVIDERS,Ti as Embedder,Ei as EmbeddingRecognizer,Mi as FrameScheduler,Id as HOVER_MAX_AGE_MS,g$ as HOVER_SELECTIONS,ln as IdentityMap,qk as MAX_ANALYSIS_WIDTH,Gk as MIN_ANALYSIS_WIDTH,Vt as PREPROCESSING,Pi as Pipeline,zi as ReleaseError,Ai as TemporalGate,ki as Tracker,as as analysisSize,Uk as analyzeFrame,l$ as boxAverageRgb,is as cadenceMs,Td as captureFrame,S$ as chooseLook,fd as computeCardQuad,xP as createSession,T$ as cropRgba,Z2 as decodeDetections,Qo as hoverAt,wd as hoverCardsOf,y$ as hoverOrder,An as iou,WA as letterboxBgr,sd as letterboxGeometry,jk as loadRelease,$d as loadReleaseData,dn as lookAccepted,rd as nonMaxSuppression,cd as parseBank,yd as parseIdentityGroups,C$ as parseRelease,f$ as pointInConvexQuad,I$ as poolEmbeddings,vd as recognizeFrame,Od as replyRejection,Go as resizeBilinearRgb,bd as resolveHover,md as resolveHoveredCard,dd as rotateTensor90,Oi as selectHoverLayer,ld as toContractTensor,ad as toDetectorTensor,Vo as validBox}; +}`;function kT(){try{return yC()}catch{return null}}function yC(){let e=document.createElement("canvas"),t=e.getContext("webgl2",{alpha:!0,antialias:!1,depth:!1,stencil:!1,preserveDrawingBuffer:!1,powerPreference:"low-power"});if(!t)return null;let n=t.createProgram();for(let[p,h]of[[t.VERTEX_SHADER,gC],[t.FRAGMENT_SHADER,bC]]){let m=t.createShader(p);if(!m)return null;t.shaderSource(m,h),t.compileShader(m),t.attachShader(n,m)}if(t.linkProgram(n),!t.getProgramParameter(n,t.LINK_STATUS))return null;t.useProgram(n);let i=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,i),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW);let u=t.getAttribLocation(n,"p");t.enableVertexAttribArray(u),t.vertexAttribPointer(u,2,t.FLOAT,!1,0,0);let l=t.createTexture();t.bindTexture(t.TEXTURE_2D,l),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR_MIPMAP_LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE);let d=[],c=1,s=!0,r=new Uint8Array(0);e.addEventListener("webglcontextlost",()=>{s=!1});function o(p){return d.find(h=>h.ticket===p)}function a(p){p.fence&&t.deleteSync(p.fence),p.fence=null,p.ticket=null}return{get usable(){return s&&!t.isContextLost()},sample(p){let h=Zn(p);if(!this.usable||h.width===0||h.height===0)return null;let m=d.find(y=>y.ticket===null);if(!m){if(d.length>=mC)return null;let y=t.createBuffer();if(!y)return null;m={pbo:y,fence:null,width:0,height:0,ticket:null},d.push(m)}let{width:g,height:_}=pC(h.width,h.height);try{(e.width!==g||e.height!==_)&&(e.width=g,e.height=_),t.viewport(0,0,g,_),t.bindTexture(t.TEXTURE_2D,l),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,p),t.generateMipmap(t.TEXTURE_2D),t.drawArrays(t.TRIANGLE_STRIP,0,4),t.bindBuffer(t.PIXEL_PACK_BUFFER,m.pbo),(m.width!==g||m.height!==_)&&(t.bufferData(t.PIXEL_PACK_BUFFER,g*_*4,t.STREAM_READ),m.width=g,m.height=_),t.readPixels(0,0,g,_,t.RGBA,t.UNSIGNED_BYTE,0),t.bindBuffer(t.PIXEL_PACK_BUFFER,null),m.fence=t.fenceSync(t.SYNC_GPU_COMMANDS_COMPLETE,0),t.flush()}catch{return s=!1,null}if(!m.fence)return null;let w={id:c++};return m.ticket=w,w},read(p){let h=o(p);if(!h||!h.fence||!this.usable||t.getSyncParameter(h.fence,t.SYNC_STATUS)!==t.SIGNALED)return null;let m=h.width*h.height*4;r.length!==m&&(r=new Uint8Array(m)),t.bindBuffer(t.PIXEL_PACK_BUFFER,h.pbo),t.getBufferSubData(t.PIXEL_PACK_BUFFER,0,r),t.bindBuffer(t.PIXEL_PACK_BUFFER,null);let g={width:h.width,height:h.height,luma:hC(r,h.width,h.height)};return a(h),g},release(p){if(!p)return;let h=o(p);h&&a(h)},dispose(){if(s=!1,!t.isContextLost()){for(let p of d)a(p),t.deleteBuffer(p.pbo);d.length=0,t.getExtension("WEBGL_lose_context")?.loseContext()}}}}function tc(e,t,n){let i={left:e.left,top:e.top,width:e.width,height:e.height};if(t.width<=0||t.height<=0||n==="fill")return i;let u=e.width/t.width,l=e.height/t.height,d;n==="cover"?d=Math.max(u,l):n==="none"?d=1:n==="scale-down"?d=Math.min(1,u,l):d=Math.min(u,l);let c=t.width*d,s=t.height*d;return{left:e.left+(e.width-c)/2,top:e.top+(e.height-s)/2,width:c,height:s}}var wC=250,CT=250,_C=1e4,vC=5e3,xC=400,rc=2e3;function RT(e,t,n){return Math.abs(t-e)*1e3<=n}var $C=250,DT=5e3,MT=15e3,PT=[250,500,1e3,2e3];function nc(e,t){return e===void 0?"stale-request":e.requestId<=t.lastAcceptedRequestId?"superseded":e.requestId!==t.inFlightRequestId?"stale-request":e.videoKey!==t.videoKey?"video-key":e.generation!==t.generation?"generation":null}function NT(e){return e==="video-key"||e==="generation"}var eo=class{constructor(t,n=3,i="always-on",u=t,l=null){this.intervalMs=t;this.confirmFrames=n;this.mode=i;this.readingIntervalMs=u;this.probeIntervalMs=l;this.checkProbe(i)}intervalMs;confirmFrames;mode;readingIntervalMs;probeIntervalMs;pending=!1;repeatsLeft=0;lastStartedAt=-1/0;pointerInside=!1;lastPointerAt=-1/0;tooltipShowing=!1;lastTooltipAt=-1/0;lastHoverTriggerAt=-1/0;lastVideoTimeSec=null;discontinuitySinceLast=!1;lastTriggerKind=null;lookRequested=!1;unanswered=0;retryAt=null;retryPrompt=!1;lastUnansweredCapture=!1;refused=!1;pointerAnalysis=!1;checkProbe(t){if(t==="layout"&&!(this.probeIntervalMs!==null&&this.probeIntervalMs>0))throw new Error("layout mode needs a positive probe interval")}setProbeIntervalMs(t){this.probeIntervalMs=t,this.checkProbe(this.mode)}setPointerAnalysis(t){this.pointerAnalysis=t}setIntervalMs(t){this.intervalMs=t}setReadingIntervalMs(t){this.readingIntervalMs=t}setTooltipShowing(t,n){this.tooltipShowing=t,t&&this.pointerInside&&(this.lastTooltipAt=n)}setMode(t){this.checkProbe(t),this.mode=t}get analysisMode(){return this.mode}framePresented(){this.pending=!0,this.repeatsLeft=0}discontinuity(){this.discontinuitySinceLast=!0,this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}requestLook(){this.discontinuitySinceLast=!0,this.pending=!0,this.repeatsLeft=0,this.lookRequested=!0}lookUnanswered(t,n={}){this.repeatsLeft=0,this.retryAt=t+PT[Math.min(this.unanswered,PT.length-1)],this.retryPrompt=n.prompt===!0,this.lastUnansweredCapture=n.prompt===!0,this.unanswered+=1}lookAnswered(){this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}lookRefused(){this.refused=!0}pictureReceived(){this.refused=!1,this.lastUnansweredCapture&&(this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1)}retryReady(t){return this.retryAt!==null&&t>=this.retryAt}get lookPending(){return this.lookRequested}pointerMoved(t){this.pointerInside=!0,this.lastPointerAt=t,(this.unanswered>0||this.refused)&&this.retryAt===null&&(this.retryAt=t,this.retryPrompt=!1)}pointerLeft(t){this.pointerInside&&(this.lastPointerAt=t),this.pointerInside=!1,this.lastTooltipAt=-1/0}tapped(t){this.lastPointerAt=t}pointerActive(t){return t-this.lastPointerAt<_C}analysisState(t){if(this.mode==="always-on")return"always-on";if(this.mode==="layout"){let n=this.pointerAnalysis?this.pointerState(t):"idle";return n==="idle"?"probe":n}return this.pointerState(t)}pointerState(t){return this.pointerActive(t)?"fast":this.pointerInside&&(this.tooltipShowing||t-this.lastTooltipAt=this.intervalInEffectMs(t):this.analysisState(t)==="probe"?!1:this.repeatsLeft>0&&n>=wC}staleFrameMs(t){return Math.max(xC,this.intervalInEffectMs(t))}frameStale(t,n){return this.lastVideoTimeSec===null||this.discontinuitySinceLast?!0:Math.abs(t-this.lastVideoTimeSec)*1e3>this.staleFrameMs(n)}hoverTriggerDue(t,n,i){let u=this.analysisState(t);return i||u==="idle"||u==="probe"||t-this.lastHoverTriggerAt<$C?!1:this.frameStale(n,t)}started(t,n={}){this.retryReady(t)&&(this.retryAt=null,this.retryPrompt=!1,this.pending=!0,this.repeatsLeft=0),this.lastStartedAt=t;let i=n.trigger==="hover",u=n.trigger==="cut"||this.lookRequested;this.lastTriggerKind=i?"hover":u?"cut":this.pending?"cadence":"repeat",i&&(this.lastHoverTriggerAt=t),n.videoTimeSec!==void 0&&(this.lastVideoTimeSec=n.videoTimeSec),this.discontinuitySinceLast=!1,this.lookRequested=!1,this.pending||i||u?(this.pending=!1,this.repeatsLeft=this.analysisState(t)==="probe"?0:this.confirmFrames-1):this.repeatsLeft>0&&(this.repeatsLeft-=1)}get lastTrigger(){return this.lastTriggerKind}msSinceLastAnalysis(t){return this.lastStartedAt===-1/0?null:t-this.lastStartedAt}get idle(){return!this.pending&&this.repeatsLeft===0&&this.retryAt===null}};var ks="no picture",SC="no picture (the paused video draws nothing)",ic="no 2d context",IC="the video may not be read",OC="the picture is too large",EC=16,AC=9,zT=()=>document.createElement("canvas"),kC=e=>createImageBitmap(e);function oc(){return{canvas:null,readable:!1}}function BT(e){return e.paused&&e.readyState<2&&e.videoWidth>0}function sc(e,t){let n=t?.lastPlayingTime??null;return BT(e)&&n!==null?n:e.currentTime}function PC(e,t=zT){let n=EC,i=t();i.width=n,i.height=n;let u=i.getContext("2d");if(!u)return!1;u.drawImage(e,0,0,n,n);let l=u.getImageData(0,0,n,n).data,d=0,c=0;for(let r=0;r({kind:"failed",reason:h,retry:m}),u=e?.video;if(!e||!u||(n.requireConnected??!0)&&!u.isConnected||u.videoWidth===0)return i(ks,!0);let l=BT(u);if(u.readyState<2&&!l)return i(ks,!0);let d=n.createCanvas??zT,c=As(u.videoWidth,u.videoHeight,t),s=n.maxSide??4096;if(c.width>s||c.height>s)return i(OC);let r=e.source;r.canvas||(r.canvas=d(),r.readable=!1);let o=r.canvas;o.width=c.width,o.height=c.height;let a=o.getContext("2d");if(!a)return i(ic);let p=sc(u,e);try{return a.drawImage(u,0,0,c.width,c.height),l&&PC(o,d)?(r.readable=!0,i(SC,!0)):(r.readable||(a.getImageData(0,0,1,1),r.readable=!0),{kind:"frame",bitmap:await(n.createImageBitmap??kC)(o),mediaTime:p})}catch(h){return i(CC(h)?IC:String(h))}}function LT(e,t){let n=null,i=oc(),u={video:e,get lastPlayingTime(){return n},get source(){return i},mediaTime:()=>sc(e,u),detach:a},l=()=>t.discontinuity(!1),d=()=>{i=oc(),n=null,t.discontinuity(!0)},c=()=>{!e.paused&&e.readyState>=2&&(n=e.currentTime),t.presented(u.mediaTime())},s=()=>t.presented(u.mediaTime()),r=[["seeking",l],["emptied",d],["loadeddata",d],["timeupdate",c],["seeked",c],["canplay",s],["playing",s]];for(let[p,h]of r)e.addEventListener(p,h);let o=!0;function a(){if(o){o=!1;for(let[p,h]of r)e.removeEventListener(p,h)}}return u}var OM=1440*60*1e3;var RC=[/^the worker did not start/,/^unexpected error while loading/];function jT(e,t){return t||e.startsWith("unexpected:")?!1:!RC.some(n=>n.test(e))}var MM=Object.freeze({misses:2,maxMs:5e3,sameBoxIou:.5});function DC(e,t){return e==="loading"?{phase:t.phase,bytes:t.bytes}:void 0}function MC(e,t,n=40){return(e.hover?.cards??[]).map(i=>i.bounds).filter(i=>i&&!(Math.abs(i.x-t.x)<1e-6&&Math.abs(i.y-t.y)<1e-6&&Math.abs(i.width-t.width)<1e-6)).slice(0,n).map(i=>({x:i.x,y:i.y,width:i.width,height:i.height}))}var Ps=class{constructor(t,n={}){this.deps=t;this.mode=n.mode??"pointer",this.hold=n.hold??null,this.maxOtherBoxes=n.maxOtherBoxes??40,this.rotatedOutlines=n.rotatedOutlines===!0,this.hitTest=t.hoverAt??vs,this.scheduler=new eo(Is(null,this.mode),3,this.mode,Ss(null)),this.st={state:"loading",error:null,release:null,backend:null,schedule:"idle",frames:0,lastWorkerMs:null,lastRoundTripMs:null,lastFrameError:null,geometric:n.geometric?{state:"loading",reason:null,bytes:null}:null,loading:null,tilted:null},this.lastStatusKey=this.statusKey()}deps;scheduler;mode;hold;maxOtherBoxes;rotatedOutlines;hitTest;st;cardList=[];analysisWidth={min:720,max:1920};maxAgeMs=rc;nextRequestId=1;inFlight=null;lastAcceptedRequestId=0;analyzedSinceLoad=!1;generation=0;sourceCount=0;result=null;pointerAt=null;mediaTime=0;lastShownKey="null";lastStatusKey="";held=null;get videoKey(){return`source-${this.sourceCount}`}pointer(t,n,i,u=!1){if(this.st.state==="stopped")return;let l=this.deps.now();this.pointerAt={x:t,y:n},this.mediaTime=i,u?this.scheduler.tapped(l):this.scheduler.pointerMoved(l),this.st.state==="ready"&&this.scheduler.hoverTriggerDue(l,this.videoTime(),this.inFlight!==null)&&this.requestCapture("hover"),this.publish()}pointerLeft(){this.st.state!=="stopped"&&(this.pointerAt=null,this.scheduler.pointerLeft(this.deps.now()),this.publish())}presented(t){this.st.state!=="stopped"&&(this.mediaTime=t,this.scheduler.framePresented(),this.publish())}discontinuity(t){this.st.state!=="stopped"&&(this.generation+=1,t&&(this.sourceCount+=1),this.scheduler.discontinuity(),this.result=null,this.held=null,t&&this.scheduler.framePresented(),this.publish())}cut(){this.st.state!=="stopped"&&(this.generation+=1,this.result=null,this.held=null,this.scheduler.requestLook(),this.publish())}frame(t,n,i){let u=this.inFlight;if(this.st.state==="stopped"||!u||u.requestId!==t||u.analyzing){n.close();return}if(this.scheduler.pictureReceived(),u.generation!==this.generation||u.videoKey!==this.videoKey){this.inFlight=null,n.close(),this.publish();return}u.analyzing=!0;let l={requestId:u.requestId,mediaTimeSec:i,generation:u.generation,videoKey:u.videoKey};try{this.deps.toWorker({kind:"frame",bitmap:n,videoTimeSec:i,envelope:l,...this.rotatedOutlines?{rotatedOutlines:!0}:{}},[n])}catch(d){n.close(),this.inFlight=null,this.st.lastFrameError=`analysis-failed: ${String(d)}`,this.scheduler.lookUnanswered(this.deps.now())}this.publish()}captureFailed(t,n,i=!1){let u=this.inFlight;this.st.state==="stopped"||!u||u.requestId!==t||u.analyzing||(this.inFlight=null,this.st.lastFrameError=`capture-failed: ${n}`,i?this.scheduler.lookUnanswered(this.deps.now(),{prompt:!0}):this.scheduler.lookRefused(),this.publish())}fromWorker(t){if(this.st.state!=="stopped")switch(t.kind){case"geometric-status":this.st.geometric={state:t.state,reason:t.reason,bytes:t.bytes},this.deps.onGeometricStatus?.(t),this.publishStatus();return;case"tilted-status":this.st.tilted={state:"off",reason:t.reason},this.deps.onTiltedStatus?.(t),this.publishStatus();return;case"release-progress":{let n=DC(this.st.state,t);n!==void 0&&(this.st.loading=n),this.publishStatus();return}case"release-status":this.st.state=t.ok?"ready":"failed",this.analyzedSinceLoad=!1,this.st.error=t.error,this.st.release=t.release,this.st.loading=null,this.st.tilted=t.release?.tiltedRetry?{state:"on",reason:null}:null,this.cardList=t.cards,t.release&&(this.st.backend=t.release.recognizer.ep,this.maxAgeMs=t.release.hoverMaxAgeMs,this.analysisWidth={...t.release.analysisWidth},this.scheduler.setIntervalMs(Is(this.st.backend,this.mode)),this.scheduler.setReadingIntervalMs(Ss(this.st.backend)),this.scheduler.framePresented()),this.publish();return;case"frame-result":this.onFrameResult(t);return;default:return}}onFrameResult(t){let n=this.deps.now(),i=nc(t.envelope,{inFlightRequestId:this.inFlight?.requestId??null,lastAcceptedRequestId:this.lastAcceptedRequestId,generation:this.generation,videoKey:this.videoKey});if(i!==null&&!NT(i))return;let u=this.inFlight;if(this.inFlight=null,i!==null){this.publish();return}this.lastAcceptedRequestId=t.envelope.requestId,this.st.lastFrameError=t.error??null,this.st.lastWorkerMs=t.workerMs,this.st.lastRoundTripMs=+(n-u.sentAt).toFixed(2),t.analysis?(this.analyzedSinceLoad=!0,this.scheduler.lookAnswered(),this.st.frames+=1,this.result={analysis:t.analysis,videoTimeSec:t.videoTimeSec},this.deps.onAnalysis?.(t.analysis)):(this.result=null,t.error===V$&&this.scheduler.lookUnanswered(n)),this.held&&this.pointerAt&&this.current()&&!this.fresh()&&(this.held.misses+=1),this.scheduler.setTooltipShowing(this.shown()!==null,n),this.publish()}tick(){if(this.st.state==="stopped")return;let t=this.deps.now(),n=this.analyzedSinceLoad?DT:MT;this.inFlight&&t-this.inFlight.sentAt>=n&&(this.inFlight=null,this.st.lastFrameError="no reply",this.scheduler.lookUnanswered(t)),this.scheduler.setTooltipShowing(this.shown()!==null,t),this.st.state==="ready"&&!this.inFlight&&this.scheduler.due(t)&&this.requestCapture(this.scheduler.lookPending?"cut":void 0),this.publish()}stop(){this.st.state!=="stopped"&&(this.st.state="stopped",this.st.loading=null,this.inFlight=null,this.result=null,this.held=null,this.pointerAt=null,this.publishShown(),this.publishStatus())}workerStopped(t){if(!(this.st.state==="stopped"||this.st.state==="failed")){if(this.inFlight=null,this.result=null,this.held=null,this.st.loading=null,this.publishShown(),"restart"in t){let n={kind:"geometric-status",state:"off",reason:"worker-restarted",bytes:null};this.st.geometric&&(this.st.geometric={state:n.state,reason:n.reason,bytes:n.bytes}),this.deps.onGeometricStatus?.(n),this.st.state="loading",this.analyzedSinceLoad=!1,this.st.error=null}else this.st.state="failed",this.st.error=t.error;this.publishStatus()}}reloading(t){this.st.state==="loading"&&(this.st.geometric=t.geometric?{state:"loading",reason:null,bytes:null}:null,this.st.loading=null,this.publishStatus())}requestCapture(t){if(this.st.state!=="ready"||this.inFlight)return;let n=this.deps.now(),i=this.nextRequestId++;this.inFlight={requestId:i,sentAt:n,generation:this.generation,videoKey:this.videoKey,analyzing:!1},this.scheduler.started(n,{videoTimeSec:this.videoTime(),...t?{trigger:t}:{}}),this.deps.capture(i,{...this.analysisWidth})}status(){return{...this.st,schedule:this.scheduler.analysisState(this.deps.now())}}analysis(){return this.current()?.analysis??null}cards(){return this.cardList}hitAt(t,n){let i=this.current();if(!i)return null;let{width:u,height:l}=i.analysis.frame;if(!(u>0&&l>0))return null;let d=t*u,c=n*l;if(!Number.isFinite(d)||!Number.isFinite(c))return null;let s=this.hitTest(i.analysis,d,c);if(!s.box||s.cardId===null||s.name===null)return null;let r={x:s.box.x/u,y:s.box.y/l,width:s.box.width/u,height:s.box.height/l},o={x:s.box.x,y:s.box.y,width:s.box.width,height:s.box.height};return{cardId:s.cardId,name:s.name,box:r,frameBox:o,others:MC(i.analysis,r,this.maxOtherBoxes)}}fresh(){return this.pointerAt?this.hitAt(this.pointerAt.x,this.pointerAt.y):null}shown(){let t=this.fresh(),n=this.hold;if(!n)return t;let i=this.deps.now();if(t)return this.held={card:t,misses:0,seenAt:i},t;let u=this.held;return!u||!this.pointerAt||!zC(u.card.box,this.pointerAt)?(this.held=null,null):this.boxAtPointer(u.card.box,n.sameBoxIou)==="other"||u.misses>=n.misses||i-u.seenAt>=n.maxMs?(this.held=null,null):u.card}current(){let t=this.result;return t&&RT(t.videoTimeSec,this.videoTime(),this.maxAgeMs)?t:null}videoTime(){return this.deps.mediaTimeNow?this.deps.mediaTimeNow():this.mediaTime}boxAtPointer(t,n){let i=this.current();if(!i||!this.pointerAt)return"unknown";let{width:u,height:l}=i.analysis.frame;if(!(u>0&&l>0))return"unknown";let d=this.pointerAt.x*u,c=this.pointerAt.y*l,s={x:t.x*u,y:t.y*l,width:t.width*u,height:t.height*l},r=0,o=!1;for(let a of i.analysis.boxes??[]){let[p,h,m,g]=a.box;dp+m||ch+g||(o=!0,r=Math.max(r,NC(s,{x:p,y:h,width:m,height:g})))}return o?r>=n?"same":"other":"none"}publish(){this.publishShown(),this.publishStatus()}publishShown(){let t=this.shown(),n=t?`${t.cardId}|${t.box.x}|${t.box.y}|${t.box.width}|${t.box.height}`:"null";n!==this.lastShownKey&&(this.lastShownKey=n,this.deps.onShown?.(t))}statusKey(){let t=this.status();return JSON.stringify({...t,release:t.release?t.release.id:null})}publishStatus(){let t=this.statusKey();t!==this.lastStatusKey&&(this.lastStatusKey=t,this.deps.onStatus?.(this.status()))}};function NC(e,t){let n=Math.min(e.x+e.width,t.x+t.width)-Math.max(e.x,t.x),i=Math.min(e.y+e.height,t.y+t.height)-Math.max(e.y,t.y);if(n<=0||i<=0)return 0;let u=n*i;return u/(e.width*e.height+t.width*t.height-u)}function zC(e,t){return t.x>=e.x&&t.x<=e.x+e.width&&t.y>=e.y&&t.y<=e.y+e.height}var VT=1;function BC(e){let{geometricUrl:t,geometricManifestSha256:n,geometricWatchdog:i,...u}=e;return u}function qT(e){let t=null,n=null,i=0,u=!1,l=null,d=!1,c=null,s={source:e.chooseRelease?"choosing":"bundled",id:null,reason:null,table:null},r=y=>{e.onDecision?.({reused:0,downloaded:0,ms:0,...y})},o=y=>new Ps({now:e.now,toWorker:(T,S)=>t?.postMessage(T,S??[]),capture:e.capture,...e.onShown?{onShown:e.onShown}:{},...e.onStatus?{onStatus:e.onStatus}:{},...e.onAnalysis?{onAnalysis:e.onAnalysis}:{},...e.onGeometricStatus?{onGeometricStatus:e.onGeometricStatus}:{},...e.hoverAt?{hoverAt:e.hoverAt}:{},...e.mediaTimeNow?{mediaTimeNow:e.mediaTimeNow}:{}},{...e.mode?{mode:e.mode}:{},...e.hold?{hold:e.hold}:{},...e.rotatedOutlines?{rotatedOutlines:!0}:{},geometric:y.geometricUrl!==void 0}),a=y=>{u||!n||(l&&y.kind==="release-progress"&&y.phase==="preparing"&&(d=!0),!(y.kind==="release-status"&&(n.status().state!=="loading"||l&&!h(y)))&&(y.kind==="release-status"&&!y.ok&&p(),n.fromWorker(y)))},p=()=>{u=!0,l=null,t?.terminate(),t=null},h=y=>{let T=l;if(y.ok&&y.release?.id===T.pointer.id)return l=null,c=T.pointer.id,s.source="remote",s.id=T.pointer.id,s.reason="ok",s.table=T.facts,r({source:"remote",reason:"ok",pointer:T.pointer.id,reused:T.reused,downloaded:T.downloaded,ms:T.ms}),!0;if(y.ok)return e.recordSkip?.(T.pointer,"id-mismatch"),m("id-mismatch"),!1;let S=y.error??"no reason given";return jT(S,d)&&e.recordSkip?.(T.pointer,`runtime:${S.slice(0,120)}`),m(`runtime:${S}`),!1},m=y=>{let T=l;l=null,t?.terminate(),s.source="bundled",s.id=null,s.reason=y,s.table=null,r({source:"bundled",reason:y,pointer:T.pointer.id,reused:T.reused,downloaded:T.downloaded,ms:T.ms});let S=T.fallback??e.loadRequest;n?.reloading({geometric:S.geometricUrl!==void 0}),w(S)},g=y=>{if(u||!n)return;let T=n.status().state;if(T==="failed"||T==="stopped")return;let S=y.message||"see the console";if(l){h({kind:"release-status",ok:!1,error:`the worker did not start (${S})`,release:null,cards:[]});return}if(t?.terminate(),T==="loading"&&i===0){p(),n.workerStopped({error:`the worker did not start (${S})`});return}if(i{let T=e.createWorker();T.onmessage=S=>{T===t&&a(S.data)},T.onerror=S=>{T===t&&g(S)},t=T,T.postMessage(y)},w=y=>{n??=o(y);try{_(y)}catch(T){t=null,l=null,u=!0,n.workerStopped({error:`the recognition worker could not start (${String(T)})`})}};return e.chooseRelease?e.chooseRelease({onDownloading:()=>e.onDownloading?.()}).catch(y=>({source:"bundled",reason:`unexpected:${String(y)}`,pointer:null,downloaded:0,ms:0})).then(y=>{if(!u){if(y.source==="remote"){l=y,d=!1,w(y.request);return}s.source="bundled",s.reason=y.reason,r({source:"bundled",reason:y.reason,pointer:y.pointer?.id??null,downloaded:y.downloaded,ms:y.ms}),w(y.request??e.loadRequest)}}):w(e.loadRequest),{get core(){return n},get stopped(){return u},get restarts(){return i},get release(){return{...s}},tick(){u||n?.tick()},stop(){u=!0,l=null,t?.terminate(),t=null,n?.stop()}}}var FC={requireConnected:!1,maxSide:1/0};function LC(e,t){let n=e instanceof HTMLVideoElement?e:null,i=e,u=t.workerUrl??new URL("./worker.js",import.meta.url),l=()=>performance.now(),d=!1,c=0,s=!1,r=()=>{c>0?s=!0:d&&t.onUpdate?.()},o=(N,X=!1)=>{c+=1;try{N()}finally{c-=1}X&&(s=!0),c===0&&s&&(s=!1,d&&t.onUpdate?.())},a=null,p=new Es,h=null,m=null,g=-1/0,_=()=>{let N=h&&a?a.read(h):null;N?p.seed(N):(a?.release(h),p.reset()),h=null},w=n?null:document.createElement("canvas"),y=w?.getContext("2d")??null,T=async N=>{if(!w||!y)return{kind:"failed",reason:ic,retry:!1};let X=Zn(e);if(X.width===0||X.height===0)return{kind:"failed",reason:ks,retry:!0};try{let W=await ec(e,w,y,N);return{kind:"frame",bitmap:W.bitmap,mediaTime:W.videoTimeSec}}catch(W){return{kind:"failed",reason:String(W),retry:!1}}},S=(N,X)=>{a?.release(h),h=a?.usable?a.sample(e):null,(z?FT(z,X,FC):T(X)).then(W=>o(()=>{W.kind==="frame"?q.frame(N,W.bitmap,W.mediaTime):q.captureFailed(N,W.reason,W.retry)},W.kind==="failed"))},O=0,A=null,P=()=>{O+=1;let N;try{N=new Worker(u,{type:"module"})}catch(W){throw O===1&&(A={error:W}),W}let X={postMessage:(W,ne)=>N.postMessage(W,ne??[]),terminate:()=>N.terminate(),onmessage:null,onerror:null};return N.onmessage=W=>o(()=>X.onmessage?.(W)),N.onerror=W=>o(()=>X.onerror?.(W)),X},I={kind:"load",releaseUrl:t.releaseFiles?String(t.releaseUrl):new URL(t.releaseUrl,location.href).href,...t.releaseFiles?{files:t.releaseFiles}:{},...t.wasmPaths!==void 0?{wasmPaths:t.wasmPaths}:{},...t.providers?{providers:t.providers}:{},...t.geometricUrl!==void 0?{geometricUrl:new URL(t.geometricUrl,location.href).href}:{},...t.geometricManifestSha256!==void 0?{geometricManifestSha256:t.geometricManifestSha256}:{},...t.geometricWatchdog===!1?{geometricWatchdog:!1}:{}},z=n?LT(n,{presented:N=>o(()=>q.presented(N)),discontinuity:N=>o(()=>{p.reset(),q.discontinuity(N)},!0)}):null,G=()=>z?.mediaTime()??0,J=qT({createWorker:P,loadRequest:I,now:l,...t.mode?{mode:t.mode}:{},...t.rotatedOutlines?{rotatedOutlines:!0}:{},capture:S,mediaTimeNow:()=>G(),onShown:r,onStatus:r,onAnalysis:_});if(A)throw z?.detach(),J.stop(),A.error;let q=J.core,C=(N,X)=>{let W=tc(e.getBoundingClientRect(),Zn(e),getComputedStyle(e).objectFit);if(W.width<=0||W.height<=0)return null;let ne=q.hitAt((N-W.left)/W.width,(X-W.top)/W.height);return ne?{hit:ne,picture:W}:null},ee=(N,X)=>{let W=C(N,X);if(!W)return null;let{hit:ne,picture:le}=W;return{name:ne.name,cardId:ne.cardId,box:{left:le.left+ne.box.x*le.width,top:le.top+ne.box.y*le.height,width:ne.box.width*le.width,height:ne.box.height*le.height},frameBox:ne.frameBox}},v=null,R=()=>v?ee(v.x,v.y):null,H=N=>{let X=v?C(v.x,v.y)?.hit.box??null:null;if(!X){a?.release(m),m=null;return}if(a||(a=kT()),!!a?.usable){if(m){let W=a.read(m);if(W&&(m=null,p.observe(W,X).cut)){p.reset(),q.cut(),s=!0;return}}!m&&N-g>=AT&&(g=N,m=a.sample(e))}},L=null,V=()=>{!d||!n||(o(()=>{q.presented(G()),H(l())}),L=n.requestVideoFrameCallback(V))},K=()=>o(()=>{p.reset(),q.discontinuity(!0)},!0),B=N=>o(()=>{v={x:N.clientX,y:N.clientY};let X=tc(e.getBoundingClientRect(),Zn(e),getComputedStyle(e).objectFit),W=ET(X,N.clientX,N.clientY)??{x:(N.clientX-X.left)/X.width,y:(N.clientY-X.top)/X.height};q.pointer(W.x,W.y,G(),N.pointerType==="touch")},!0),te=()=>o(()=>{v=null,q.pointerLeft()},!0);n&&"requestVideoFrameCallback"in n&&(L=n.requestVideoFrameCallback(V)),n||e.addEventListener("load",K),i.addEventListener("pointermove",B),i.addEventListener("pointerdown",B),i.addEventListener("pointerleave",te);let Y=setInterval(()=>o(()=>J.tick()),CT);return d=!0,{status:()=>q.status(),at:ee,atPointer:R,analysis:()=>q.analysis(),cards:()=>q.cards(),stop:()=>{d&&(d=!1,clearInterval(Y),L!==null&&n&&"cancelVideoFrameCallback"in n&&n.cancelVideoFrameCallback(L),L=null,z?.detach(),e.removeEventListener("load",K),i.removeEventListener("pointermove",B),i.removeEventListener("pointerdown",B),i.removeEventListener("pointerleave",te),a?.release(h),a?.release(m),a?.dispose(),p.reset(),J.stop())}}}var Mr={maxCorrespondences:40,fitMinInliers:3,inlierShare:[2,3],ambiguityMin:2,compatTol:.01,scaleRatioTol:.03},ac=1e-9,Cs=5;function cr(e,t){let n=Math.max(0,Math.min(e.x+e.w,t.x+t.w)-Math.max(e.x,t.x)),i=Math.max(0,Math.min(e.y+e.h,t.y+t.h)-Math.max(e.y,t.y)),u=n*i,l=e.w*e.h+t.w*t.h-u;return l>0?u/l:0}function Sn(e,t){return{x:e.sx*t.x+e.tx,y:e.sy*t.y+e.ty,w:e.sx*t.w,h:e.sy*t.h}}function to(e,t){return{x:e.sx*t.x+e.tx,y:e.sy*t.y+e.ty}}function uc(e){return{x:e.tx,y:e.ty,width:e.sx,height:e.sy}}function Rs(e){if(e.cards.length===0)return null;let t=1/0,n=1/0,i=-1/0,u=-1/0;for(let{bounds:l}of e.cards)t=Math.min(t,l.x),n=Math.min(n,l.y),i=Math.max(i,l.x+l.w),u=Math.max(u,l.y+l.h);return{x:t,y:n,w:i-t,h:u-n}}function lc(e){let t=[];for(let n=0;n=e.x&&t.x<=e.x+e.w&&t.y>=e.y&&t.y<=e.y+e.h}function GT(e,t){return et?1:0}function jC(e,t){return t.identity===void 0?!1:e.blocking===void 0||!In(e.blocking,XT(t.bounds))}function Ds(e,t,n=Mr.maxCorrespondences){let i=new Map;for(let d of e)i.set(d.identity,i.has(d.identity)?null:d);let u=new Map;for(let d of t.cards){if(!jC(t,d))continue;let c=d.identity;u.set(c,u.has(c)?null:d)}let l=[];for(let[d,c]of i){let s=u.get(d);!c||!s||l.push({identity:d,v:c.bounds,r:s.bounds,instance:s.instance})}return l.length>n&&(l.sort((d,c)=>c.v.w*c.v.h-d.v.w*d.v.h||GT(d.identity,c.identity)),l=l.slice(0,n)),l.sort((d,c)=>GT(d.identity,c.identity))}function UC(e){if(!(e.r.w>0)||!(e.r.h>0))return null;let t=e.v.w/e.r.w,n=e.v.h/e.r.h,i={sx:t,sy:n,tx:e.v.x-t*e.r.x,ty:e.v.y-n*e.r.y};return![i.sx,i.sy,i.tx,i.ty].every(Number.isFinite)||!(t>0)||!(n>0)?null:i}function HT(e){let t=e.length;if(t===0)return null;let n=0,i=0;for(let[s,r]of e)n+=s,i+=r;n/=t,i/=t;let u=0,l=0;for(let[s,r]of e)u+=(s-n)*(r-i),l+=(s-n)*(s-n);if(!(l>0))return null;let d=u/l;if(!(d>0)||!Number.isFinite(d))return null;let c=i-d*n;return Number.isFinite(c)?{s:d,t:c}:null}function WT(e){let t=[],n=[];for(let{r:l,v:d}of e)t.push([l.x,d.x],[l.x+l.w,d.x+d.w]),n.push([l.y,d.y],[l.y+l.h,d.y+d.h]);let i=HT(t),u=HT(n);return!i||!u?null:{sx:i.s,sy:u.s,tx:i.t,ty:u.t}}function dc(e,t,n){if(e.length===0||!(t>0)||!(n>0))return null;let i=[],u=[];for(let{r:y,v:T}of e)i.push([y.x,T.x],[y.x+y.w,T.x+T.w]),u.push([y.y,T.y],[y.y+y.h,T.y+T.h]);let l=(y,T)=>y.reduce((S,O)=>S+O[T],0)/y.length,d=l(i,0),c=l(i,1),s=l(u,0),r=l(u,1),o=0,a=0,p=0,h=0;for(let[y,T]of i)o+=(y-d)*(y-d),a+=(y-d)*(T-c);for(let[y,T]of u)p+=(y-s)*(y-s),h+=(y-s)*(T-r);let m=n*n,g=m*o+t*t*p;if(!(g>0))return null;let _=(m*a+t*h)/g;if(!(_>0)||!Number.isFinite(_))return null;let w={sx:_,sy:t*_,tx:c-_*d,ty:r-t*_*s};return[w.sy,w.tx,w.ty].every(Number.isFinite)?w:null}function VC(e,t){if(e.aspect===void 0||t===void 0)return null;let n=i=>Number.isFinite(i)&&i>0;return!n(e.aspect)||!n(t)?NaN:t/e.aspect}function qC(e,t,n,i,u=1){return Ms(e,t,n,i,u).outcome}function Ms(e,t,n,i,u=1){if(t===null)return{outcome:{kind:"no-layout"},correspondences:[],support:[],winner:-1,aspectRejected:[]};let l=Ds(e,t,n.maxCorrespondences),d=l.length,c=[],s=(L,V,K=[],B=-1)=>({outcome:{kind:"insufficient",reason:L,n:d,inliers:V},correspondences:l,support:K,winner:B,aspectRejected:c}),r=VC(t,i);if(r!==null&&(Number.isNaN(r)||!(Number.isFinite(u)&&u>0)))return s("degenerate",0);let o=L=>r!==null&&Math.abs(L.sy/L.sx/r-1)>n.scaleRatioTol,a=L=>{let V=[];for(let K=0;K=n.fitMinIoU&&V.push(K);return V},p=l.map(UC),h=p.map((L,V)=>L&&o(L)?(c.push(V),null):L),m=()=>c.some(L=>a(p[L]).length>=2),g=[],_=[];for(let L=0;L=n.fitMinIoU&&(K.push(te),B+=Y)}g.push(K),_.push(B)}let w=-1;for(let L=0;LK||V===K&&_[L]>_[w]+ac)&&(w=L)}if(w<0){let L=d=n.fitMinInliers)return s(m()?"aspect":"zero-inliers",0,g,w);let y=g[w].map(L=>l[L]),T=r===null?WT(y):dc(y,r*u,i);if(!T)return s("degenerate",0,g,w);let S=[],O=1/0;for(let L of g[w]){let V=cr(Sn(T,l[L].r),l[L].v);V>=n.fitMinIoU&&(S.push(L),O=Math.min(O,V))}let A=S.map(L=>l[L].identity),P=Rs(t),I=new Set(S),z=[];for(let L=0;L!I.has(B));K.length>z.length&&(z=K)}if(z.length>=n.ambiguityMin)return{outcome:{kind:"ambiguous",transform:T,inliers:A,runnerUp:z.map(L=>l[L].identity),n:d},correspondences:l,support:g,winner:w,aspectRejected:c};if(A.lengthl[V]));if(!L||o(L))return s("aspect",A.length,g,w);H=L.sy/L.sx/r}return{outcome:{kind:"verified",transform:T,inliers:A,n:d,minIoU:O,span:R,...H!==void 0?{boardRatio:H}:{}},correspondences:l,support:g,winner:w,aspectRejected:c}}var GC=8,HC=20,WC=.3;function KC(e,t,n,i,u){let l=0;for(let d of lc(n)){let c=to(e,d),s=to(t,d);l=Math.max(l,Math.abs(c.x-s.x)*i,Math.abs(c.y-s.y)*u)}return l}function XC(e,t){return t?e<=GC?"correct":e<=HC?"loose":"false":"none"}function ZT(e){return{x:e.x+e.w/2,y:e.y+e.h/2}}function ZC(e,t,n,i=[]){let u=ZT(e.box);if(!In(n,u)||i.some(c=>In(c,u)))return"blocked";let l=null,d=-1;for(let c of t){let s=cr(e.box,c.box);s>=WC&&s>d&&(l=c,d=s)}return l?!l.faceUp||l.identity===void 0?"back-hidden":l.identity===e.identity?"correct":"wrong-identity":"empty"}function YC(e,t,n,i=[]){let u=0,l=0;for(let d of e){if(!d.faceUp||d.identity===void 0)continue;let c=ZT(d.box);!In(n,c)||i.some(s=>In(s,c))||(u+=1,t.some(s=>s.identity===d.identity&&In(s.box,c))&&(l+=1))}return u===0?null:l/u}function JC(e,t){return t.map(n=>QC(e,n))}function QC(e,t){let n=null;for(let i of e){if(i.atMs>t)break;n=i.display}if(n!=="fitted")return 0;for(let i of e)if(i.atMs>=t&&i.display!=="fitted")return i.atMs-t;return null}var Ns={window:10,minIdentities:3,trip:.4,recover:.7},ro=class{params;exclude;window=[];stateValue="unknown";recoverRun=0;constructor(t={}){this.params=t.params??Ns,this.exclude=t.exclude??(()=>!1)}get state(){return this.stateValue}get tripped(){return this.stateValue==="tripped"}get share(){return this.summary().share}observe(t){if(t.windowIdentities.size===0)return{state:this.stateValue,event:null,...this.summary(),counted:!1};let n=new Map;for(let c of t.accepted)this.exclude(c)||n.set(c,(n.get(c)??!1)||t.windowIdentities.has(c));this.window.push(n),this.window.length>this.params.window&&this.window.shift();let{identities:i,share:u}=this.summary(),l=i>=this.params.minIdentities&&u!==null,d=null;return this.stateValue==="tripped"?(this.recoverRun=l&&u>=this.params.recover?this.recoverRun+1:0,this.recoverRun>=this.params.window&&(this.stateValue="in",this.recoverRun=0,d="recover")):l&&(u0&&t.scaleRatioTol<=.25))return"scaleRatioTol must be above 0 and at most 0.25";let n=t;return n.fitMinIoU<.5||n.fitMinIoU>1?"fitMinIoU must be between 0.5 and 1":n.minSpan<0||n.minSpan>1?"minSpan must be between 0 and 1":!Number.isInteger(n.confirmFits)||n.confirmFits<1||n.confirmFits>3?"confirmFits must be 1, 2 or 3":n.suppressionIoU<=0||n.suppressionIoU>1?"suppressionIoU must be above 0 and at most 1":n.intervalMs<=0?"intervalMs must be positive":n.confirmWindowMs<3*n.intervalMs?"confirmWindowMs must be at least 3 \xD7 intervalMs":n.freshnessMs<2*n.intervalMs?"freshnessMs must be at least 2 \xD7 intervalMs":n.holdMs=u){d.add(c.instance);break}}return e.cards.map(c=>c.instance).filter(c=>d.has(c))}var eS=15;function eR(e){let t=[...e].sort((i,u)=>i-u),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}var no=class{config;fitParams;monitor;generationValue=null;stateValue="off";contradicted=!1;verifiedThisGen=!1;run=null;confirmed=null;confirmedBasis=null;ratioPool=[];appliedScale=1;holdStart=null;lastAnalysisAt=null;zeroStreak=0;lastFrame={kind:"no-layout"};suppressed=[];current;constructor(t,n={}){let i=JT(t);if(i)throw new Error(i);this.config=Object.freeze({...t}),this.fitParams={...Mr,fitMinIoU:t.fitMinIoU,minSpan:t.minSpan,ambiguityMin:t.ambiguityMin??Mr.ambiguityMin,scaleRatioTol:t.scaleRatioTol??Mr.scaleRatioTol},this.monitor=new ro({exclude:n.exclude,params:Ns}),this.current=this.build(0,0)}get generation(){return this.generationValue}get state(){return this.stateValue}get verdict(){return this.current}eligibilityLost(t){return this.clear(),this.generationValue=null,this.stateValue="off",this.current=this.build(1,t)}reset(t,n){return this.clear(),this.generationValue=t,this.stateValue="unverified",this.current=this.build(2,n)}tick(t){return this.expired(t)?(this.expire(),this.current=this.build(11,t)):this.current=this.build(0,t)}analyze(t,n){if(this.generationValue===null||t.generation!==this.generationValue)return null;let i=this.expired(n);i&&this.expire();let u=this.stateValue,l=this.ratioScale(),d=Ms(t.observations,t.layout,this.fitParams,t.frameAspect,l),c=d.outcome;this.lastFrame=c;let s=null;c.kind==="verified"&&c.boardRatio!==void 0&&(s={correspondences:d.support[d.winner].map(_=>d.correspondences[_]),ratio:t.frameAspect/t.layout.aspect,frameAspect:t.frameAspect});let r=this.monitor.observe({windowIdentities:t.windowIdentities,accepted:t.observations.map(_=>_.identity)});this.lastAnalysisAt=n,this.zeroStreak=c.kind==="insufficient"&&c.reason==="zero-inliers"?this.zeroStreak+1:0;let o=t.layout?Rs(t.layout):null,a=c.kind==="verified"&&o!==null?c:null,p=this.fitParams.compatTol,h=a!==null&&this.confirmed!==null&&!Yn(a.transform,this.confirmed,o,p),m=this.run;if(a){let _=this.run!==null&&Yn(a.transform,this.run.latest,o,p)&&t.capturedAtMs-this.run.capturedAtMs<=this.config.confirmWindowMs;m={latest:a.transform,capturedAtMs:t.capturedAtMs,length:_?this.run.length+1:1,basis:s}}let g;return r.event==="trip"?(g=3,this.run=m,this.stateValue="mismatch",this.confirmed=null,this.confirmedBasis=null,this.holdStart=null,this.contradicted=!0):u==="mismatch"&&r.event==="recover"?(g=4,this.run=m,this.stateValue="unverified"):h?(g=5,this.run={latest:a.transform,capturedAtMs:t.capturedAtMs,length:1,basis:s},this.stateValue="contradicted",this.confirmed=null,this.confirmedBasis=null,this.holdStart=null,this.contradicted=!0,this.verifiedThisGen=!0):this.zeroStreak>=2&&(u==="verified"||u==="held")?(g=6,this.stateValue="contradicted",this.confirmed=null,this.confirmedBasis=null,this.holdStart=null,this.contradicted=!0,this.verifiedThisGen=!0):t.faces===0&&t.layout!==null&&t.layout.cards.length>=3&&this.verifiedThisGen&&u!=="mismatch"?(g=7,this.run=m,this.stateValue="no-faces"):a&&m.length>=this.config.confirmFits?(g=9,this.run=m,this.stateValue="verified",this.confirmed=a.transform,this.confirmedBasis=s,this.contradicted=!1,this.holdStart=n,this.verifiedThisGen=!0,this.monitor.clearTrip()):(this.run=m,(u==="verified"||u==="held"||u==="no-faces"&&a!==null)&&this.confirmed!==null&&this.holdStart!==null&&n-this.holdStarteS&&this.ratioPool.shift()),this.applyScale(this.ratioScale()),(this.stateValue==="verified"||this.stateValue==="held")&&this.confirmed&&t.layout?this.suppressed=QT(t.layout,t.observations,this.confirmed,this.fitParams.fitMinIoU,this.config.suppressionIoU,this.fitParams.maxCorrespondences):this.suppressed=[],this.current=this.build(g,n)}expired(t){let n=this.stateValue;if(n!=="verified"&&n!=="held"&&n!=="no-faces")return!1;let i=this.holdStart!==null&&t-this.holdStart>=this.config.holdMs,u=this.lastAnalysisAt!==null&&t-this.lastAnalysisAt>=this.config.freshnessMs;return i||u}applyScale(t){if(t===this.appliedScale)return;let n=i=>i?dc(i.correspondences,i.ratio*t,i.frameAspect):null;this.confirmed&&(this.confirmed=n(this.confirmedBasis)??this.confirmed),this.run&&(this.run.latest=n(this.run.basis)??this.run.latest),this.appliedScale=t}ratioScale(){if(this.ratioPool.length<2)return 1;let t=this.fitParams.scaleRatioTol;return Math.min(1+t,Math.max(1-t,eR(this.ratioPool)))}expire(){this.stateValue=this.monitor.tripped?"mismatch":"unverified",this.confirmed=null,this.confirmedBasis=null,this.holdStart=null,this.suppressed=[]}clear(){this.contradicted=!1,this.verifiedThisGen=!1,this.run=null,this.confirmed=null,this.confirmedBasis=null,this.ratioPool=[],this.appliedScale=1,this.holdStart=null,this.lastAnalysisAt=null,this.zeroStreak=0,this.lastFrame={kind:"no-layout"},this.suppressed=[],this.monitor.reset()}display(){switch(this.stateValue){case"off":return"today";case"verified":case"held":return"fitted";case"no-faces":return"hidden";case"mismatch":case"contradicted":return"vision";case"unverified":return this.contradicted?"vision":"today"}}build(t,n){let i=this.display(),u={display:i,suppressed:i==="fitted"?[...this.suppressed]:[],state:this.stateValue,contradicted:this.contradicted,frame:this.lastFrame,diagnostics:{elapsedSinceLastAnalysisMs:this.lastAnalysisAt===null?0:n-this.lastAnalysisAt,runLength:this.run?.length??0,monitorShare:this.monitor.share,row:t,ratioScale:this.ratioScale()}};return i==="fitted"&&(u.region=uc(this.confirmed)),u}};var cc="layout-recording/1",zs=2,rS=[{x:.05,y:.05},{x:.95,y:.05},{x:.5,y:.5},{x:.05,y:.95},{x:.95,y:.95}],tR=["supported-game","two-region","negative-no-faces","negative-gallery","negative-other-layout","transition"],Bs=class extends Error{};function St(e,t){throw new Bs(`${e}: ${t}`)}function mt(e,t){return(typeof e!="object"||e===null||Array.isArray(e))&&St(t,"must be an object"),e}function Nr(e,t){return Array.isArray(e)||St(t,"must be an array"),e}function Xe(e,t,n={}){return(typeof e!="number"||!Number.isFinite(e))&&St(t,"must be a finite number"),n.integer&&!Number.isInteger(e)&&St(t,"must be an integer"),n.min!==void 0&&e{let c=`${t}.cards[${d}]`,s=mt(l,c),r={instance:pr(s.instance,`${c}.instance`),bounds:io(s.bounds,`${c}.bounds`)};return s.identity!==void 0&&(r.identity=pr(s.identity,`${c}.identity`)),s.rotated!==void 0&&(typeof s.rotated!="boolean"&&St(`${c}.rotated`,"must be a boolean"),r.rotated=s.rotated),r}),u={key:pr(n.key,`${t}.key`),cards:i};return n.blocking!==void 0&&(u.blocking=io(n.blocking,`${t}.blocking`)),n.aspect!==void 0&&(u.aspect=Xe(n.aspect,`${t}.aspect`),u.aspect>0||St(`${t}.aspect`,"must be positive")),u}function nR(e,t){try{let n=mt(e,t),i=n.region!==null;return{region:i?tS(n.region,`${t}.region`):null,residualPx:i?Xe(n.residualPx,`${t}.residualPx`,{min:0}):typeof n.residualPx=="number"?n.residualPx:0,mask:io(n.mask,`${t}.mask`),blockers:n.blockers===void 0?[]:Nr(n.blockers,`${t}.blockers`).map((u,l)=>io(u,`${t}.blockers[${l}]`)),solves:n.solves===void 0?[]:Nr(n.solves,`${t}.solves`).map((u,l)=>{let d=mt(u,`${t}.solves[${l}]`);return{atMs:Xe(d.atMs,`${t}.solves[${l}].atMs`),residualPx:Xe(d.residualPx,`${t}.solves[${l}].residualPx`,{min:0}),region:i?tS(d.region,`${t}.solves[${l}].region`):null}})}}catch(n){if(n instanceof Bs)return n.message;throw n}}function iR(e,t,n){if(typeof e!="object"||e===null||Array.isArray(e))return n.push(`${t}: no clock`),{status:"missing"};let i=e;return i.status!=="ok"&&i.status!=="undecodable"&&i.status!=="missing"?(n.push(`${t}.status: unknown status ${JSON.stringify(i.status)}`),{status:"missing"}):i.status!=="ok"?{status:i.status}:typeof i.streamTimeMs!="number"||!Number.isFinite(i.streamTimeMs)?(n.push(`${t}.streamTimeMs: an ok clock without a finite time`),{status:"missing"}):{status:"ok",streamTimeMs:i.streamTimeMs}}function oR(e){let t=[],n=mt(e,"recording");n.schema!==cc&&St("recording.schema",`must be "${cc}"`);let i=mt(n.frame,"recording.frame"),u={width:Xe(i.width,"recording.frame.width",{min:1}),height:Xe(i.height,"recording.frame.height",{min:1})},l=Nr(n.cadenceProfiles,"recording.cadenceProfiles").map((o,a)=>{let p=mt(o,`recording.cadenceProfiles[${a}]`),h=Xe(p.intervalMs,`recording.cadenceProfiles[${a}].intervalMs`);return h>0||St(`recording.cadenceProfiles[${a}].intervalMs`,"must be positive"),{name:pr(p.name,`recording.cadenceProfiles[${a}].name`),intervalMs:h}}),d=Nr(n.scenes,"recording.scenes").map((o,a)=>{let p=`recording.scenes[${a}]`,h=mt(o,p),m=pr(h.class,`${p}.class`);tR.includes(m)||St(`${p}.class`,`unknown scene class "${m}"`);let g={id:pr(h.id,`${p}.id`),class:m,startMs:Xe(h.startMs,`${p}.startMs`),endMs:Xe(h.endMs,`${p}.endMs`)};if(g.endMs{let p=`recording.frames[${a}]`,h=mt(o,p),m=Xe(h.capturedAtMs,`${p}.capturedAtMs`);m{let T=`${p}.analysis.observations[${y}]`,S=mt(w,T);return{identity:pr(S.identity,`${T}.identity`),bounds:io(S.bounds,`${T}.bounds`)}})}}}),r=Nr(n.candidates,"recording.candidates").map((o,a)=>{let p=`recording.candidates[${a}]`,h=mt(o,p);return{key:pr(h.key,`${p}.key`),producedAtMs:Xe(h.producedAtMs,`${p}.producedAtMs`),arrivedAtMs:Xe(h.arrivedAtMs,`${p}.arrivedAtMs`),layout:rR(h.layout,`${p}.layout`)}});return{schema:cc,frame:u,cadenceProfiles:l,scenes:d,frames:s,candidates:r,problems:t}}function nS(e,t){return e.candidates.map((n,i)=>({c:n,i})).filter(({c:n})=>n.arrivedAtMs<=t).sort((n,i)=>n.c.arrivedAtMs-i.c.arrivedAtMs||n.i-i.i).map(({c:n})=>n)}function pc(e,t){let n=nS(e,t);return n.length>0?n[n.length-1]:null}function sR(e,t){if(!hc(t.clock))return null;let n=t.clock.streamTimeMs,i=null;for(let u of e.candidates)u.producedAtMs<=n&&(i===null||u.producedAtMs>i.producedAtMs)&&(i=u);return i}function hc(e){return e?.status==="ok"&&typeof e.streamTimeMs=="number"&&Number.isFinite(e.streamTimeMs)}function aR(e){let t=e.frames.filter(n=>hc(n.clock));return{frames:t,excluded:e.frames.length-t.length}}function uR(e,t){return e.scenes.find(n=>t>=n.startMs&&tu.residualPx<=zs))return!1;let i=[n.region];for(let u of n.solves){if(!u.region)return!1;i.push(u.region)}for(let u=0;uzs||l>zs)return!1}return!0}function iS(e){return{layoutAt:t=>pc(e,t)?.layout??null,windowIdentitiesAt:t=>cR(pc(e,t)?.layout??null)}}function cR(e){let t=new Set;for(let n of e?.cards??[])n.identity!==void 0&&t.add(n.identity);return t}function pR(e,t){let n=e.cadenceProfiles.find(p=>p.name===t.profile);if(!n)throw new Error(`no cadence profile "${t.profile}" in the recording`);let i={...t.config,intervalMs:n.intervalMs},u=new no(i,{exclude:t.exclude}),l=t.scheduler(n.intervalMs),d=t.source??iS(e),c=[],s=[],r=null,o=1;e.frames.length>0&&u.reset(o,e.frames[0].capturedAtMs);let a=()=>{let p=r;r=null;let h=p.frame,m={generation:o,capturedAtMs:h.capturedAtMs,faces:h.analysis.faces,observations:h.analysis.observations,layout:d.layoutAt(h.capturedAtMs),windowIdentities:d.windowIdentitiesAt(h.capturedAtMs),frameAspect:e.frame.width/e.frame.height};u.analyze(m,p.endedAtMs),s.push({frameIndex:h.index,startedAtMs:p.startedAtMs,endedAtMs:p.endedAtMs,late:p.endedAtMs>p.startedAtMs+n.intervalMs})};for(let p of e.frames){let h=p.capturedAtMs;r&&r.endedAtMs<=h&&a(),l.framePresented(),!r&&l.due(h)&&(l.started(h),r={frame:p,startedAtMs:h,endedAtMs:h+p.analysis.durationMs},p.analysis.durationMs===0&&a()),c.push({frameIndex:p.index,atMs:h,result:u.tick(h)})}return r&&a(),{steps:c,analyses:s}}export{$s as DEFAULT_ACCEPT,kd as DEFAULT_DECODE,ps as DEFAULT_LETTERBOX,$P as DEFAULT_TILTED_RETRY,Li as Detector,_s as EXECUTION_PROVIDERS,ji as Embedder,Gi as EmbeddingRecognizer,rS as FIDUCIAL_POINTS,eo as FrameScheduler,rc as HOVER_MAX_AGE_MS,J$ as HOVER_SELECTIONS,Tn as IdentityMap,Mr as LAYOUT_FIT_DEFAULTS,no as LayoutRegistration,uC as MAX_ANALYSIS_WIDTH,lC as MIN_ANALYSIS_WIDTH,Ns as MONITOR_DEFAULTS,ro as MismatchMonitor,aT as OUTLINE_MIN_TILT,er as PREPROCESSING,Ki as Pipeline,eS as RATIO_POOL_SIZE,Qi as ReleaseError,Hi as TemporalGate,Wi as Tracker,As as analysisSize,sC as analyzeFrame,G$ as boxAverageRgb,Is as cadenceMs,ec as captureFrame,fT as chooseLook,XC as classifyAccept,ZC as classifyHitbox,hc as clockUsable,Yn as compatible,Fd as computeCardQuad,Ds as correspondences,YC as coverage,LC as createSession,hT as cropRgba,C$ as decodeDetections,Wd as estimateTilt,JC as exitLatency,KC as fitError,qC as fitFrame,Ms as fitFrameDetail,vs as hoverAt,Gd as hoverCardsOf,eT as hoverOrder,Gn as iou,pc as latestOffered,JT as layoutConfigError,Rs as layoutExtent,cr as layoutIoU,Jk as letterboxBgr,Rd as letterboxGeometry,oC as loadRelease,Qd as loadReleaseData,lr as lookAccepted,Ad as nonMaxSuppression,nS as offeredCandidates,lT as outlineFromTilt,Zd as outlineRect,Nd as parseBank,Vd as parseIdentityGroups,xT as parseRelease,KT as placementDistance,Z$ as pointInConvexQuad,gT as poolEmbeddings,Sn as project,oR as readLayoutRecording,Yd as recognizeFrame,iS as recordingSource,Kd as rectFromAabb,pR as replayRecording,nc as replyRejection,cs as resizeBilinearRgb,Ud as resolveHover,Ld as resolveHoveredCard,ms as rotateTensor90,uT as sampleRect,uR as sceneAt,qi as selectHoverLayer,QT as suppressedInstances,mT as tiltedOrder,Xd as tiltedRects,aR as timedFrames,fs as toContractTensor,Dd as toDetectorTensor,uc as toRegion,sR as truthCandidate,lR as truthUsable,ls as validBox}; /*! For license information please see cardvision.js.LEGAL.txt */ diff --git a/vendor/cardvision-browser/compute-session.js b/vendor/cardvision-browser/compute-session.js index 812ddd7..10810a8 100644 --- a/vendor/cardvision-browser/compute-session.js +++ b/vendor/cardvision-browser/compute-session.js @@ -1 +1 @@ -function C(t){return t==="wasm"?2e3:1e3}function me(t){return t==="wasm"?1e3:500}function _(t,e){return e==="always-on"?C(t):me(t)}var fe=250,ye=250,ge=1e4,be=5e3,xe=400,O=2e3;function G(t,e,r){return Math.abs(e-t)*1e3<=r}var Se=250,K=5e3,V=[250,500,1e3,2e3];function W(t,e){return t===void 0?"stale-request":t.requestId<=e.lastAcceptedRequestId?"superseded":t.requestId!==e.inFlightRequestId?"stale-request":t.videoKey!==e.videoKey?"video-key":t.generation!==e.generation?"generation":null}function X(t){return t==="video-key"||t==="generation"}var T=class{constructor(e,r=3,s="always-on",n=e){this.intervalMs=e;this.confirmFrames=r;this.mode=s;this.readingIntervalMs=n}intervalMs;confirmFrames;mode;readingIntervalMs;pending=!1;repeatsLeft=0;lastStartedAt=-1/0;pointerInside=!1;lastPointerAt=-1/0;tooltipShowing=!1;lastTooltipAt=-1/0;lastHoverTriggerAt=-1/0;lastVideoTimeSec=null;discontinuitySinceLast=!1;lastTriggerKind=null;lookRequested=!1;unanswered=0;retryAt=null;retryPrompt=!1;lastUnansweredCapture=!1;refused=!1;setIntervalMs(e){this.intervalMs=e}setReadingIntervalMs(e){this.readingIntervalMs=e}setTooltipShowing(e,r){this.tooltipShowing=e,e&&this.pointerInside&&(this.lastTooltipAt=r)}setMode(e){this.mode=e}get analysisMode(){return this.mode}framePresented(){this.pending=!0,this.repeatsLeft=0}discontinuity(){this.discontinuitySinceLast=!0,this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}requestLook(){this.discontinuitySinceLast=!0,this.pending=!0,this.repeatsLeft=0,this.lookRequested=!0}lookUnanswered(e,r={}){this.repeatsLeft=0,this.retryAt=e+V[Math.min(this.unanswered,V.length-1)],this.retryPrompt=r.prompt===!0,this.lastUnansweredCapture=r.prompt===!0,this.unanswered+=1}lookAnswered(){this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}lookRefused(){this.refused=!0}pictureReceived(){this.refused=!1,this.lastUnansweredCapture&&(this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1)}retryReady(e){return this.retryAt!==null&&e>=this.retryAt}get lookPending(){return this.lookRequested}pointerMoved(e){this.pointerInside=!0,this.lastPointerAt=e,(this.unanswered>0||this.refused)&&this.retryAt===null&&(this.retryAt=e,this.retryPrompt=!1)}pointerLeft(e){this.pointerInside&&(this.lastPointerAt=e),this.pointerInside=!1,this.lastTooltipAt=-1/0}tapped(e){this.lastPointerAt=e}pointerActive(e){return e-this.lastPointerAt=this.intervalInEffectMs(e):this.repeatsLeft>0&&r>=fe}staleFrameMs(e){return Math.max(xe,this.intervalInEffectMs(e))}frameStale(e,r){return this.lastVideoTimeSec===null||this.discontinuitySinceLast?!0:Math.abs(e-this.lastVideoTimeSec)*1e3>this.staleFrameMs(r)}hoverTriggerDue(e,r,s){return s||!this.analysisActive(e)||e-this.lastHoverTriggerAt0&&(this.repeatsLeft-=1)}get lastTrigger(){return this.lastTriggerKind}msSinceLastAnalysis(e){return this.lastStartedAt===-1/0?null:e-this.lastStartedAt}get idle(){return!this.pending&&this.repeatsLeft===0&&this.retryAt===null}};function ve(t){let e=t.localWidth??t.bounds.width,r=t.localHeight??t.bounds.height,s=t.bounds.x+t.bounds.width/2,n=t.bounds.y+t.bounds.height/2;return{left:s-e/2,top:n-r/2,width:e,height:r}}function z(t,e){let r=ve(t),s=(r.left+r.width/2)*e.width,n=(r.top+r.height/2)*e.height,i=r.width/2*e.width,o=r.height/2*e.height,l=t.rotation*Math.PI/180,a=Math.cos(l),u=Math.sin(l);return{points:[{x:-i,y:-o},{x:i,y:-o},{x:i,y:o},{x:-i,y:o}].map(({x:g,y:b})=>({x:s+g*a-b*u,y:n+g*u+b*a}))}}function we(t,e){let{points:r}=e,s=0;for(let n=0;n0?1:-1;if(s===0)s=g;else if(g!==s)return!1}return!0}function $(t,e){let r=null;for(let s of e)we(t,s.quad)&&(!r||s.zIndex>=r.zIndex)&&(r=s);return r?r.card:null}function E(t){return t<0?0:t>1?1:t}function U(t,e){let{frameWidth:r,frameHeight:s,names:n}=e;return t.map(i=>{let{aabb:o,rect:l}=i,a={instanceId:i.instanceId,bounds:{x:E(o.x/r),y:E(o.y/s),width:E(o.width/r),height:E(o.height/s)},rotation:(l.angle+45)%90-45,localWidth:E(l.w/r),localHeight:E(l.h/s),landscape:l.w>l.h,cardId:null,name:null};if(i.identity){let u=n.get(i.identity);u!==void 0&&(a.cardId=i.identity,a.name=u)}return a})}var Ae="accepted-nms";function Y(t){return t.width*t.height}function ke(t,e){let r=e==="accepted-nms"?t.kept:t.proposals.map((n,i)=>i),s=(n,i)=>{let o=t.proposals[n],l=t.proposals[i];if(e==="detector-score")return o.detectorScore!==l.detectorScore?o.detectorScore{let n=t.proposals[r];return{proposal:r,aabb:n.aabb,rect:n.rect,identity:n.accepted?n.displayId:null,score:n.score,zIndex:s+1}})}function Q(t,e,r,s=n=>n){return U(t.map((i,o)=>({instanceId:e[o],aabb:i.aabb,rect:i.rect,identity:i.identity})),r).map((i,o)=>({...i,zIndex:t[o].zIndex,boxIndex:s(t[o].proposal)}))}var Ie={card:null,boxIndex:null,box:null,cardId:null,name:null};function J(t,e,r){let s=t.map(i=>({card:i,quad:z(i,r),zIndex:i.zIndex??0})),n=$(e,s);return n?{card:n,boxIndex:n.boxIndex??null,box:{x:n.bounds.x*r.width,y:n.bounds.y*r.height,width:n.bounds.width*r.width,height:n.bounds.height*r.height},cardId:n.cardId,name:n.name}:Ie}function Re(t,e=t.hover.selection){let r=t.boxes.flatMap((a,u)=>a.face!==null?[u]:[]),s=r.map(a=>t.boxes[a]),n=new Map,i=s.map(a=>{let u=a.face;u.cardId!==null&&u.name!==null&&n.set(u.cardId,u.name);let p={x:a.box[0],y:a.box[1],width:a.box[2],height:a.box[3]};return{aabb:p,rect:{cx:p.x+p.width/2,cy:p.y+p.height/2,w:p.width,h:p.height,angle:0},score:u.similarity,detectorScore:a.score,accepted:u.cardId!==null,displayId:u.cardId??u.topId}}),o=s.flatMap((a,u)=>a.face.kept?[u]:[]),l=Z({proposals:i,kept:o},e);return Q(l,l.map((a,u)=>`${e}_${u}`),{frameWidth:t.frame.width,frameHeight:t.frame.height,names:n},a=>r[a])}function H(t,e,r,s){let n=s===void 0||s===t.hover.selection?t.hover.cards:Re(t,s);return J(n,{x:e,y:r},t.frame)}var B=1,N=4096,L=40,Ee=200,q=200,Te=/^[0-9a-f]{16}$/,Me=/^[0-9a-f]{16}$/,A=t=>typeof t=="number"&&Number.isFinite(t),ee=t=>A(t)&&t>=0&&t<=1,F=t=>Number.isSafeInteger(t)&&t>0,te=(t,e=200)=>typeof t=="string"&&t.length>0&&t.length<=e,re=F,ne=t=>Number.isInteger(t)&&t>=1&&t<=4096,se=t=>t===null?null:typeof t=="string"?t.slice(0,200):void 0;function Ce(t){return typeof ImageBitmap<"u"&&t instanceof ImageBitmap}function _e(t){let{width:e,height:r}=t;return Number.isInteger(e)&&Number.isInteger(r)&&e>0&&r>0&&e<=4096&&r<=4096}function ie(t){if(typeof t!="object"||t===null)return null;let e=t;return A(e.x)&&A(e.y)&&A(e.width)&&A(e.height)&&e.width>0&&e.height>0?{x:e.x,y:e.y,width:e.width,height:e.height}:null}var Fe=new Set(["kind","x","y","mediaTime","touch","newSource","requestId","bitmap","reason","retry","state","error","backend","analysisWidth","card","version","instance"]),Le=/^[a-z][a-z0-9-]*$/;function Pe(t="cardvision"){if(typeof t!="string"||!Le.test(t)||Fe.has(t))throw new TypeError(`computeProtocol: not a usable tag: ${String(t)}`);function e(i){if(typeof i!="object"||i===null)return null;let o=i;return o[t]===1&&typeof o.kind=="string"?o:null}function r(i){return Ce(i.bitmap)?F(i.requestId)&&A(i.mediaTime)&&_e(i.bitmap)?{kind:"frame",requestId:i.requestId,bitmap:i.bitmap,mediaTime:i.mediaTime}:(i.bitmap.close(),null):null}function s(i){let o=e(i);if(!o)return null;switch(o.kind){case"hello":return re(o.version)?{kind:"hello",version:o.version}:null;case"pointer":return ee(o.x)&&ee(o.y)&&A(o.mediaTime)&&typeof o.touch=="boolean"?{kind:"pointer",x:o.x,y:o.y,mediaTime:o.mediaTime,touch:o.touch}:null;case"leave":return{kind:"leave"};case"presented":return A(o.mediaTime)?{kind:"presented",mediaTime:o.mediaTime}:null;case"discontinuity":return typeof o.newSource=="boolean"?{kind:"discontinuity",newSource:o.newSource}:null;case"cut":return{kind:"cut"};case"frame":return r(o);case"capture-failed":return F(o.requestId)&&typeof o.reason=="string"?{kind:"capture-failed",requestId:o.requestId,reason:o.reason.slice(0,200),retry:o.retry===!0}:null;default:return null}}function n(i){let o=e(i);if(!o)return null;switch(o.kind){case"ready":return re(o.version)&&typeof o.instance=="string"&&Me.test(o.instance)?{kind:"ready",version:o.version,instance:o.instance}:null;case"status":{let l=se(o.error),a=se(o.backend);return(o.state==="loading"||o.state==="ready"||o.state==="failed")&&l!==void 0&&a!==void 0?{kind:"status",state:o.state,error:l,backend:a}:null}case"capture":{let l=o.analysisWidth;return F(o.requestId)&&typeof l=="object"&&l!==null&&ne(l.min)&&ne(l.max)&&l.max>=l.min?{kind:"capture",requestId:o.requestId,analysisWidth:{min:l.min,max:l.max}}:null}case"shown":{if(o.card===null)return{kind:"shown",card:null};let l=o.card,a=typeof l=="object"&&l!==null?ie(l.box):null;if(!(l&&te(l.cardId)&&te(l.name)&&a))return null;let u=Array.isArray(l.others)?l.others.slice(0,40).map(ie).filter(y=>y!==null):[],p=typeof l.releaseId=="string"&&Te.test(l.releaseId)?l.releaseId:void 0;return{kind:"shown",card:{cardId:l.cardId,name:l.name,...p?{releaseId:p}:{},box:a,others:u}}}default:return null}}return{name:t,tag:i=>({...i,[t]:1}),parseComputeIn:s,parseComputeOut:n}}var oe="busy";var Oe=Object.freeze({misses:2,maxMs:5e3,sameBoxIou:.5});function We(t,e){return t==="loading"?{phase:e.phase,bytes:e.bytes}:void 0}function ae(t,e,r=40){return(t.hover?.cards??[]).map(s=>s.bounds).filter(s=>s&&!(Math.abs(s.x-e.x)<1e-6&&Math.abs(s.y-e.y)<1e-6&&Math.abs(s.width-e.width)<1e-6)).slice(0,r).map(s=>({x:s.x,y:s.y,width:s.width,height:s.height}))}var M=class{constructor(e,r={}){this.deps=e;this.mode=r.mode??"pointer",this.hold=r.hold??null,this.maxOtherBoxes=r.maxOtherBoxes??40,this.hitTest=e.hoverAt??H,this.scheduler=new T(_(null,this.mode),3,this.mode,C(null)),this.st={state:"loading",error:null,release:null,backend:null,schedule:"idle",frames:0,lastWorkerMs:null,lastRoundTripMs:null,lastFrameError:null,geometric:r.geometric?{state:"loading",reason:null,bytes:null}:null,loading:null},this.lastStatusKey=this.statusKey()}deps;scheduler;mode;hold;maxOtherBoxes;hitTest;st;cardList=[];analysisWidth={min:720,max:1920};maxAgeMs=O;nextRequestId=1;inFlight=null;lastAcceptedRequestId=0;generation=0;sourceCount=0;result=null;pointerAt=null;mediaTime=0;lastShownKey="null";lastStatusKey="";held=null;get videoKey(){return`source-${this.sourceCount}`}pointer(e,r,s,n=!1){if(this.st.state==="stopped")return;let i=this.deps.now();this.pointerAt={x:e,y:r},this.mediaTime=s,n?this.scheduler.tapped(i):this.scheduler.pointerMoved(i),this.st.state==="ready"&&this.scheduler.hoverTriggerDue(i,this.videoTime(),this.inFlight!==null)&&this.requestCapture("hover"),this.publish()}pointerLeft(){this.st.state!=="stopped"&&(this.pointerAt=null,this.scheduler.pointerLeft(this.deps.now()),this.publish())}presented(e){this.st.state!=="stopped"&&(this.mediaTime=e,this.scheduler.framePresented(),this.publish())}discontinuity(e){this.st.state!=="stopped"&&(this.generation+=1,e&&(this.sourceCount+=1),this.scheduler.discontinuity(),this.result=null,this.held=null,e&&this.scheduler.framePresented(),this.publish())}cut(){this.st.state!=="stopped"&&(this.generation+=1,this.result=null,this.held=null,this.scheduler.requestLook(),this.publish())}frame(e,r,s){let n=this.inFlight;if(this.st.state==="stopped"||!n||n.requestId!==e||n.analyzing){r.close();return}if(this.scheduler.pictureReceived(),n.generation!==this.generation||n.videoKey!==this.videoKey){this.inFlight=null,r.close(),this.publish();return}n.analyzing=!0;let i={requestId:n.requestId,mediaTimeSec:s,generation:n.generation,videoKey:n.videoKey};try{this.deps.toWorker({kind:"frame",bitmap:r,videoTimeSec:s,envelope:i},[r])}catch(o){r.close(),this.inFlight=null,this.st.lastFrameError=`analysis-failed: ${String(o)}`,this.scheduler.lookUnanswered(this.deps.now())}this.publish()}captureFailed(e,r,s=!1){let n=this.inFlight;this.st.state==="stopped"||!n||n.requestId!==e||n.analyzing||(this.inFlight=null,this.st.lastFrameError=`capture-failed: ${r}`,s?this.scheduler.lookUnanswered(this.deps.now(),{prompt:!0}):this.scheduler.lookRefused(),this.publish())}fromWorker(e){if(this.st.state!=="stopped")switch(e.kind){case"geometric-status":this.st.geometric={state:e.state,reason:e.reason,bytes:e.bytes},this.deps.onGeometricStatus?.(e),this.publishStatus();return;case"release-progress":{let r=We(this.st.state,e);r!==void 0&&(this.st.loading=r),this.publishStatus();return}case"release-status":this.st.state=e.ok?"ready":"failed",this.st.error=e.error,this.st.release=e.release,this.st.loading=null,this.cardList=e.cards,e.release&&(this.st.backend=e.release.recognizer.ep,this.maxAgeMs=e.release.hoverMaxAgeMs,this.analysisWidth={...e.release.analysisWidth},this.scheduler.setIntervalMs(_(this.st.backend,this.mode)),this.scheduler.setReadingIntervalMs(C(this.st.backend)),this.scheduler.framePresented()),this.publish();return;case"frame-result":this.onFrameResult(e);return;default:return}}onFrameResult(e){let r=this.deps.now(),s=W(e.envelope,{inFlightRequestId:this.inFlight?.requestId??null,lastAcceptedRequestId:this.lastAcceptedRequestId,generation:this.generation,videoKey:this.videoKey});if(s!==null&&!X(s))return;let n=this.inFlight;if(this.inFlight=null,s!==null){this.publish();return}this.lastAcceptedRequestId=e.envelope.requestId,this.st.lastFrameError=e.error??null,this.st.lastWorkerMs=e.workerMs,this.st.lastRoundTripMs=+(r-n.sentAt).toFixed(2),e.analysis?(this.scheduler.lookAnswered(),this.st.frames+=1,this.result={analysis:e.analysis,videoTimeSec:e.videoTimeSec},this.deps.onAnalysis?.(e.analysis)):(this.result=null,e.error===oe&&this.scheduler.lookUnanswered(r)),this.held&&this.pointerAt&&this.current()&&!this.fresh()&&(this.held.misses+=1),this.scheduler.setTooltipShowing(this.shown()!==null,r),this.publish()}tick(){if(this.st.state==="stopped")return;let e=this.deps.now();this.inFlight&&e-this.inFlight.sentAt>=K&&(this.inFlight=null,this.st.lastFrameError="no reply",this.scheduler.lookUnanswered(e)),this.scheduler.setTooltipShowing(this.shown()!==null,e),this.st.state==="ready"&&!this.inFlight&&this.scheduler.due(e)&&this.requestCapture(this.scheduler.lookPending?"cut":void 0),this.publish()}stop(){this.st.state!=="stopped"&&(this.st.state="stopped",this.st.loading=null,this.inFlight=null,this.result=null,this.held=null,this.pointerAt=null,this.publishShown(),this.publishStatus())}workerStopped(e){if(!(this.st.state==="stopped"||this.st.state==="failed")){if(this.inFlight=null,this.result=null,this.held=null,this.st.loading=null,this.publishShown(),"restart"in e){let r={kind:"geometric-status",state:"off",reason:"worker-restarted",bytes:null};this.st.geometric&&(this.st.geometric={state:r.state,reason:r.reason,bytes:r.bytes}),this.deps.onGeometricStatus?.(r),this.st.state="loading",this.st.error=null}else this.st.state="failed",this.st.error=e.error;this.publishStatus()}}reloading(e){this.st.state==="loading"&&(this.st.geometric=e.geometric?{state:"loading",reason:null,bytes:null}:null,this.st.loading=null,this.publishStatus())}requestCapture(e){if(this.st.state!=="ready"||this.inFlight)return;let r=this.deps.now(),s=this.nextRequestId++;this.inFlight={requestId:s,sentAt:r,generation:this.generation,videoKey:this.videoKey,analyzing:!1},this.scheduler.started(r,{videoTimeSec:this.videoTime(),...e?{trigger:e}:{}}),this.deps.capture(s,{...this.analysisWidth})}status(){return{...this.st,schedule:this.scheduler.analysisState(this.deps.now())}}analysis(){return this.current()?.analysis??null}cards(){return this.cardList}hitAt(e,r){let s=this.current();if(!s)return null;let{width:n,height:i}=s.analysis.frame;if(!(n>0&&i>0))return null;let o=e*n,l=r*i;if(!Number.isFinite(o)||!Number.isFinite(l))return null;let a=this.hitTest(s.analysis,o,l);if(!a.box||a.cardId===null||a.name===null)return null;let u={x:a.box.x/n,y:a.box.y/i,width:a.box.width/n,height:a.box.height/i},p={x:a.box.x,y:a.box.y,width:a.box.width,height:a.box.height};return{cardId:a.cardId,name:a.name,box:u,frameBox:p,others:ae(s.analysis,u,this.maxOtherBoxes)}}fresh(){return this.pointerAt?this.hitAt(this.pointerAt.x,this.pointerAt.y):null}shown(){let e=this.fresh(),r=this.hold;if(!r)return e;let s=this.deps.now();if(e)return this.held={card:e,misses:0,seenAt:s},e;let n=this.held;return!n||!this.pointerAt||!Be(n.card.box,this.pointerAt)?(this.held=null,null):this.boxAtPointer(n.card.box,r.sameBoxIou)==="other"||n.misses>=r.misses||s-n.seenAt>=r.maxMs?(this.held=null,null):n.card}current(){let e=this.result;return e&&G(e.videoTimeSec,this.videoTime(),this.maxAgeMs)?e:null}videoTime(){return this.deps.mediaTimeNow?this.deps.mediaTimeNow():this.mediaTime}boxAtPointer(e,r){let s=this.current();if(!s||!this.pointerAt)return"unknown";let{width:n,height:i}=s.analysis.frame;if(!(n>0&&i>0))return"unknown";let o=this.pointerAt.x*n,l=this.pointerAt.y*i,a={x:e.x*n,y:e.y*i,width:e.width*n,height:e.height*i},u=0,p=!1;for(let y of s.analysis.boxes??[]){let[g,b,k,I]=y.box;og+k||lb+I||(p=!0,u=Math.max(u,He(a,{x:g,y:b,width:k,height:I})))}return p?u>=r?"same":"other":"none"}publish(){this.publishShown(),this.publishStatus()}publishShown(){let e=this.shown(),r=e?`${e.cardId}|${e.box.x}|${e.box.y}|${e.box.width}|${e.box.height}`:"null";r!==this.lastShownKey&&(this.lastShownKey=r,this.deps.onShown?.(e))}statusKey(){let e=this.status();return JSON.stringify({...e,release:e.release?e.release.id:null})}publishStatus(){let e=this.statusKey();e!==this.lastStatusKey&&(this.lastStatusKey=e,this.deps.onStatus?.(this.status()))}};function He(t,e){let r=Math.min(t.x+t.width,e.x+e.width)-Math.max(t.x,e.x),s=Math.min(t.y+t.height,e.y+e.height)-Math.max(t.y,e.y);if(r<=0||s<=0)return 0;let n=r*s;return n/(t.width*t.height+e.width*e.height-n)}function Be(t,e){return e.x>=t.x&&e.x<=t.x+t.width&&e.y>=t.y&&e.y<=t.y+t.height}var bt=1440*60*1e3;var Ne=[/^the worker did not start/,/^unexpected error while loading/];function le(t,e){return e||t.startsWith("unexpected:")?!1:!Ne.some(r=>r.test(t))}var ue=1;function de(t){let{geometricUrl:e,geometricManifestSha256:r,geometricWatchdog:s,...n}=t;return n}function j(t){let e=null,r=null,s=0,n=!1,i=null,o=!1,l=null,a={source:t.chooseRelease?"choosing":"bundled",id:null,reason:null,table:null},u=d=>{t.onDecision?.({reused:0,downloaded:0,ms:0,...d})},p=d=>new M({now:t.now,toWorker:(h,f)=>e?.postMessage(h,f??[]),capture:t.capture,...t.onShown?{onShown:t.onShown}:{},...t.onStatus?{onStatus:t.onStatus}:{},...t.onAnalysis?{onAnalysis:t.onAnalysis}:{},...t.onGeometricStatus?{onGeometricStatus:t.onGeometricStatus}:{},...t.hoverAt?{hoverAt:t.hoverAt}:{},...t.mediaTimeNow?{mediaTimeNow:t.mediaTimeNow}:{}},{...t.mode?{mode:t.mode}:{},...t.hold?{hold:t.hold}:{},geometric:d.geometricUrl!==void 0}),y=d=>{n||!r||(i&&d.kind==="release-progress"&&d.phase==="preparing"&&(o=!0),!(d.kind==="release-status"&&(r.status().state!=="loading"||i&&!g(d)))&&r.fromWorker(d))},g=d=>{let h=i;if(d.ok&&d.release?.id===h.pointer.id)return i=null,l=h.pointer.id,a.source="remote",a.id=h.pointer.id,a.reason="ok",a.table=h.facts,u({source:"remote",reason:"ok",pointer:h.pointer.id,reused:h.reused,downloaded:h.downloaded,ms:h.ms}),!0;if(d.ok)return t.recordSkip?.(h.pointer,"id-mismatch"),b("id-mismatch"),!1;let f=d.error??"no reason given";return le(f,o)&&t.recordSkip?.(h.pointer,`runtime:${f.slice(0,120)}`),b(`runtime:${f}`),!1},b=d=>{let h=i;i=null,e?.terminate(),a.source="bundled",a.id=null,a.reason=d,a.table=null,u({source:"bundled",reason:d,pointer:h.pointer.id,reused:h.reused,downloaded:h.downloaded,ms:h.ms});let f=h.fallback??t.loadRequest;r?.reloading({geometric:f.geometricUrl!==void 0}),v(f)},k=d=>{if(n||!r)return;let h=r.status().state;if(h==="failed"||h==="stopped")return;let f=d.message||"see the console";if(i){g({kind:"release-status",ok:!1,error:`the worker did not start (${f})`,release:null,cards:[]});return}if(e?.terminate(),h==="loading"&&s===0){r.workerStopped({error:`the worker did not start (${f})`});return}if(s{let h=t.createWorker();h.onmessage=f=>{h===e&&y(f.data)},h.onerror=f=>{h===e&&k(f)},e=h,h.postMessage(d)},v=d=>{r??=p(d);try{I(d)}catch(h){e=null,i=null,n=!0,r.workerStopped({error:`the recognition worker could not start (${String(h)})`})}};return t.chooseRelease?t.chooseRelease({onDownloading:()=>t.onDownloading?.()}).catch(d=>({source:"bundled",reason:`unexpected:${String(d)}`,pointer:null,downloaded:0,ms:0})).then(d=>{if(!n){if(d.source==="remote"){i=d,o=!1,v(d.request);return}a.source="bundled",a.reason=d.reason,u({source:"bundled",reason:d.reason,pointer:d.pointer?.id??null,downloaded:d.downloaded,ms:d.ms}),v(d.request??t.loadRequest)}}):v(t.loadRequest),{get core(){return r},get stopped(){return n},get restarts(){return s},get release(){return{...a}},tick(){n||r?.tick()},stop(){n=!0,i=null,e?.terminate(),e=null,r?.stop()}}}var qe=/^[0-9a-f]{16}$/;function je(){let t=crypto.getRandomValues(new Uint8Array(8));return Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")}function De(t){let e=t.parentOrigin;if(!t.framed||e===null||!t.allowedParentOrigin(e))return null;let{win:r,protocol:s}=t,n=(t.instanceId??je)();if(typeof n!="string"||!qe.test(n))throw new TypeError(`startComputeFrame: not an instance id: ${String(n)}`);let i=c=>r.parent.postMessage(s.tag(c),e),o=()=>i({kind:"ready",version:1,instance:n}),l=!1,a=()=>{l||(l=!0,o())},u={state:"loading",error:null,backend:null},p=c=>c===null?null:c.slice(0,200),y=(c,m,x,R=!1)=>{let w={state:c,error:p(m),backend:p(x)};!R&&w.state===u.state&&w.error===u.error&&w.backend===u.backend||(u=w,i({kind:"status",...w}))},g=c=>{c.state!=="stopped"&&y(c.state,c.error,c.state==="ready"?c.backend:null)},b=null,k=()=>b?.release??{source:t.chooseRelease?"choosing":"bundled",id:null,reason:null,table:null},I=null,v=c=>{I=c,t.onShown?.(c,k()),i({kind:"shown",card:c})},d=c=>{let m=k(),x=c?{cardId:c.cardId,name:c.name,...m.source==="remote"&&m.id!==null?{releaseId:m.id}:{},box:{...c.box},others:c.others.map(R=>({...R}))}:null;v(x)},{win:h,framed:f,parentOrigin:Ge,allowedParentOrigin:Ke,protocol:Xe,onShown:ze,instanceId:$e,...ce}=t,S=j({...ce,createWorker:()=>(a(),t.createWorker()),capture:(c,m)=>{let x=Math.min(m.max,4096);i({kind:"capture",requestId:c,analysisWidth:{min:Math.min(m.min,x),max:x}})},onShown:d,onStatus:g,onDownloading:()=>{y("loading",null,null,!0),t.onDownloading?.()}});b=S,S.core&&a();let he=()=>{o(),y(u.state,u.error,u.backend,!0),v(I)},pe=(c,m)=>{switch(m.kind){case"pointer":c.pointer(m.x,m.y,m.mediaTime,m.touch);return;case"leave":c.pointerLeft();return;case"presented":c.presented(m.mediaTime);return;case"discontinuity":c.discontinuity(m.newSource);return;case"cut":c.cut();return;case"frame":c.frame(m.requestId,m.bitmap,m.mediaTime);return;case"capture-failed":c.captureFailed(m.requestId,m.reason,m.retry===!0);return}},D=c=>{let x=c.source===r.parent&&c.origin===e?s.parseComputeIn(c.data):null,R=S.core,w=null;try{x?.kind==="hello"?R&&he():x&&R&&!S.stopped&&(x.kind==="frame"&&(w=x.bitmap),pe(R,x))}finally{Ve(c.data,w)}};return r.addEventListener("message",D),{get core(){return S.core},get stopped(){return S.stopped},get restarts(){return S.restarts},get release(){return S.release},instance:n,tick(){S.tick()},stop(){r.removeEventListener("message",D),S.stop()}}}function Ve(t,e){if(!(typeof ImageBitmap>"u"||typeof t!="object"||t===null))for(let r of Object.values(t))r!==e&&r instanceof ImageBitmap&&r.close()}export{ye as DRIVER_TICK_MS,T as FrameScheduler,O as HOVER_MAX_AGE_MS,N as MAX_FRAME_SIDE,L as MAX_OTHER_BOXES,Ee as MAX_REASON_LENGTH,q as MAX_TEXT_LENGTH,B as PROTOCOL_VERSION,Oe as SHOWN_HOLD,M as SessionCore,ue as WORKER_RESTARTS,_ as cadenceMs,Pe as computeProtocol,j as createWorkerSession,H as hoverAt,ae as neighborBoxes,de as plainLoad,W as replyRejection,De as startComputeFrame}; +function C(t){return t==="wasm"?2e3:1e3}function ye(t){return t==="wasm"?1e3:500}function F(t,e){return e==="always-on"?C(t):ye(t)}var ge=250,be=250,Se=1e4,xe=5e3,ve=400,W=2e3;function z(t,e,r){return Math.abs(e-t)*1e3<=r}var we=250,K=5e3,X=15e3,G=[250,500,1e3,2e3];function H(t,e){return t===void 0?"stale-request":t.requestId<=e.lastAcceptedRequestId?"superseded":t.requestId!==e.inFlightRequestId?"stale-request":t.videoKey!==e.videoKey?"video-key":t.generation!==e.generation?"generation":null}function $(t){return t==="video-key"||t==="generation"}var T=class{constructor(e,r=3,i="always-on",n=e,s=null){this.intervalMs=e;this.confirmFrames=r;this.mode=i;this.readingIntervalMs=n;this.probeIntervalMs=s;this.checkProbe(i)}intervalMs;confirmFrames;mode;readingIntervalMs;probeIntervalMs;pending=!1;repeatsLeft=0;lastStartedAt=-1/0;pointerInside=!1;lastPointerAt=-1/0;tooltipShowing=!1;lastTooltipAt=-1/0;lastHoverTriggerAt=-1/0;lastVideoTimeSec=null;discontinuitySinceLast=!1;lastTriggerKind=null;lookRequested=!1;unanswered=0;retryAt=null;retryPrompt=!1;lastUnansweredCapture=!1;refused=!1;pointerAnalysis=!1;checkProbe(e){if(e==="layout"&&!(this.probeIntervalMs!==null&&this.probeIntervalMs>0))throw new Error("layout mode needs a positive probe interval")}setProbeIntervalMs(e){this.probeIntervalMs=e,this.checkProbe(this.mode)}setPointerAnalysis(e){this.pointerAnalysis=e}setIntervalMs(e){this.intervalMs=e}setReadingIntervalMs(e){this.readingIntervalMs=e}setTooltipShowing(e,r){this.tooltipShowing=e,e&&this.pointerInside&&(this.lastTooltipAt=r)}setMode(e){this.checkProbe(e),this.mode=e}get analysisMode(){return this.mode}framePresented(){this.pending=!0,this.repeatsLeft=0}discontinuity(){this.discontinuitySinceLast=!0,this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}requestLook(){this.discontinuitySinceLast=!0,this.pending=!0,this.repeatsLeft=0,this.lookRequested=!0}lookUnanswered(e,r={}){this.repeatsLeft=0,this.retryAt=e+G[Math.min(this.unanswered,G.length-1)],this.retryPrompt=r.prompt===!0,this.lastUnansweredCapture=r.prompt===!0,this.unanswered+=1}lookAnswered(){this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1,this.refused=!1}lookRefused(){this.refused=!0}pictureReceived(){this.refused=!1,this.lastUnansweredCapture&&(this.unanswered=0,this.retryAt=null,this.lastUnansweredCapture=!1)}retryReady(e){return this.retryAt!==null&&e>=this.retryAt}get lookPending(){return this.lookRequested}pointerMoved(e){this.pointerInside=!0,this.lastPointerAt=e,(this.unanswered>0||this.refused)&&this.retryAt===null&&(this.retryAt=e,this.retryPrompt=!1)}pointerLeft(e){this.pointerInside&&(this.lastPointerAt=e),this.pointerInside=!1,this.lastTooltipAt=-1/0}tapped(e){this.lastPointerAt=e}pointerActive(e){return e-this.lastPointerAt=this.intervalInEffectMs(e):this.analysisState(e)==="probe"?!1:this.repeatsLeft>0&&r>=ge}staleFrameMs(e){return Math.max(ve,this.intervalInEffectMs(e))}frameStale(e,r){return this.lastVideoTimeSec===null||this.discontinuitySinceLast?!0:Math.abs(e-this.lastVideoTimeSec)*1e3>this.staleFrameMs(r)}hoverTriggerDue(e,r,i){let n=this.analysisState(e);return i||n==="idle"||n==="probe"||e-this.lastHoverTriggerAt0&&(this.repeatsLeft-=1)}get lastTrigger(){return this.lastTriggerKind}msSinceLastAnalysis(e){return this.lastStartedAt===-1/0?null:e-this.lastStartedAt}get idle(){return!this.pending&&this.repeatsLeft===0&&this.retryAt===null}};function Ae(t){let e=t.localWidth??t.bounds.width,r=t.localHeight??t.bounds.height,i=t.bounds.x+t.bounds.width/2,n=t.bounds.y+t.bounds.height/2;return{left:i-e/2,top:n-r/2,width:e,height:r}}function U(t,e){let r=Ae(t),i=(r.left+r.width/2)*e.width,n=(r.top+r.height/2)*e.height,s=r.width/2*e.width,o=r.height/2*e.height,l=t.rotation*Math.PI/180,a=Math.cos(l),u=Math.sin(l);return{points:[{x:-s,y:-o},{x:s,y:-o},{x:s,y:o},{x:-s,y:o}].map(({x:g,y:b})=>({x:i+g*a-b*u,y:n+g*u+b*a}))}}function ke(t,e){let{points:r}=e,i=0;for(let n=0;n0?1:-1;if(i===0)i=g;else if(g!==i)return!1}return!0}function Y(t,e){let r=null;for(let i of e)ke(t,i.quad)&&(!r||i.zIndex>=r.zIndex)&&(r=i);return r?r.card:null}function E(t){return t<0?0:t>1?1:t}function Z(t,e){let{frameWidth:r,frameHeight:i,names:n}=e;return t.map(s=>{let{aabb:o}=s,l=s.identity?n.get(s.identity):void 0,a=l!==void 0&&s.outline?s.outline:s.rect,u={instanceId:s.instanceId,bounds:{x:E(o.x/r),y:E(o.y/i),width:E(o.width/r),height:E(o.height/i)},rotation:(a.angle+45)%90-45,localWidth:E(a.w/r),localHeight:E(a.h/i),landscape:a.w>a.h,cardId:null,name:null};return s.identity&&l!==void 0&&(u.cardId=s.identity,u.name=l),u})}var Ie="accepted-nms";function Q(t){return t.width*t.height}function Re(t,e){let r=e==="accepted-nms"?t.kept:t.proposals.map((n,s)=>s),i=(n,s)=>{let o=t.proposals[n],l=t.proposals[s];if(e==="detector-score")return o.detectorScore!==l.detectorScore?o.detectorScore{let n=t.proposals[r];return{proposal:r,aabb:n.aabb,rect:n.rect,...n.outline?{outline:n.outline}:{},identity:n.accepted?n.displayId:null,score:n.score,zIndex:i+1}})}function ee(t,e,r,i=n=>n){return Z(t.map((s,o)=>({instanceId:e[o],aabb:s.aabb,rect:s.rect,...s.outline?{outline:s.outline}:{},identity:s.identity})),r).map((s,o)=>({...s,zIndex:t[o].zIndex,boxIndex:i(t[o].proposal)}))}var Ee={card:null,boxIndex:null,box:null,cardId:null,name:null};function te(t,e,r){let i=t.map(s=>({card:s,quad:U(s,r),zIndex:s.zIndex??0})),n=Y(e,i);return n?{card:n,boxIndex:n.boxIndex??null,box:{x:n.bounds.x*r.width,y:n.bounds.y*r.height,width:n.bounds.width*r.width,height:n.bounds.height*r.height},cardId:n.cardId,name:n.name}:Ee}function Te(t,e=t.hover.selection){let r=t.boxes.flatMap((a,u)=>a.face!==null?[u]:[]),i=r.map(a=>t.boxes[a]),n=new Map,s=i.map(a=>{let u=a.face;u.cardId!==null&&u.name!==null&&n.set(u.cardId,u.name);let p={x:a.box[0],y:a.box[1],width:a.box[2],height:a.box[3]};return{aabb:p,rect:{cx:p.x+p.width/2,cy:p.y+p.height/2,w:p.width,h:p.height,angle:0},score:u.similarity,detectorScore:a.score,accepted:u.cardId!==null,displayId:u.cardId??u.topId}}),o=i.flatMap((a,u)=>a.face.kept?[u]:[]),l=J({proposals:s,kept:o},e);return ee(l,l.map((a,u)=>`${e}_${u}`),{frameWidth:t.frame.width,frameHeight:t.frame.height,names:n},a=>r[a])}function B(t,e,r,i){let n=i===void 0||i===t.hover.selection?t.hover.cards:Te(t,i);return te(n,{x:e,y:r},t.frame)}var N=1,q=4096,P=40,Me=200,j=200,_e=/^[0-9a-f]{16}$/,Ce=/^[0-9a-f]{16}$/,A=t=>typeof t=="number"&&Number.isFinite(t),re=t=>A(t)&&t>=0&&t<=1,L=t=>Number.isSafeInteger(t)&&t>0,ne=(t,e=200)=>typeof t=="string"&&t.length>0&&t.length<=e,se=L,ie=t=>Number.isInteger(t)&&t>=1&&t<=4096,oe=t=>t===null?null:typeof t=="string"?t.slice(0,200):void 0;function Fe(t){return typeof ImageBitmap<"u"&&t instanceof ImageBitmap}function Le(t){let{width:e,height:r}=t;return Number.isInteger(e)&&Number.isInteger(r)&&e>0&&r>0&&e<=4096&&r<=4096}function ae(t){if(typeof t!="object"||t===null)return null;let e=t;return A(e.x)&&A(e.y)&&A(e.width)&&A(e.height)&&e.width>0&&e.height>0?{x:e.x,y:e.y,width:e.width,height:e.height}:null}var Pe=new Set(["kind","x","y","mediaTime","touch","newSource","requestId","bitmap","reason","retry","state","error","backend","analysisWidth","card","version","instance"]),Oe=/^[a-z][a-z0-9-]*$/;function We(t="cardvision"){if(typeof t!="string"||!Oe.test(t)||Pe.has(t))throw new TypeError(`computeProtocol: not a usable tag: ${String(t)}`);function e(s){if(typeof s!="object"||s===null)return null;let o=s;return o[t]===1&&typeof o.kind=="string"?o:null}function r(s){return Fe(s.bitmap)?L(s.requestId)&&A(s.mediaTime)&&Le(s.bitmap)?{kind:"frame",requestId:s.requestId,bitmap:s.bitmap,mediaTime:s.mediaTime}:(s.bitmap.close(),null):null}function i(s){let o=e(s);if(!o)return null;switch(o.kind){case"hello":return se(o.version)?{kind:"hello",version:o.version}:null;case"pointer":return re(o.x)&&re(o.y)&&A(o.mediaTime)&&typeof o.touch=="boolean"?{kind:"pointer",x:o.x,y:o.y,mediaTime:o.mediaTime,touch:o.touch}:null;case"leave":return{kind:"leave"};case"presented":return A(o.mediaTime)?{kind:"presented",mediaTime:o.mediaTime}:null;case"discontinuity":return typeof o.newSource=="boolean"?{kind:"discontinuity",newSource:o.newSource}:null;case"cut":return{kind:"cut"};case"frame":return r(o);case"capture-failed":return L(o.requestId)&&typeof o.reason=="string"?{kind:"capture-failed",requestId:o.requestId,reason:o.reason.slice(0,200),retry:o.retry===!0}:null;default:return null}}function n(s){let o=e(s);if(!o)return null;switch(o.kind){case"ready":return se(o.version)&&typeof o.instance=="string"&&Ce.test(o.instance)?{kind:"ready",version:o.version,instance:o.instance}:null;case"status":{let l=oe(o.error),a=oe(o.backend);return(o.state==="loading"||o.state==="ready"||o.state==="failed")&&l!==void 0&&a!==void 0?{kind:"status",state:o.state,error:l,backend:a}:null}case"capture":{let l=o.analysisWidth;return L(o.requestId)&&typeof l=="object"&&l!==null&&ie(l.min)&&ie(l.max)&&l.max>=l.min?{kind:"capture",requestId:o.requestId,analysisWidth:{min:l.min,max:l.max}}:null}case"shown":{if(o.card===null)return{kind:"shown",card:null};let l=o.card,a=typeof l=="object"&&l!==null?ae(l.box):null;if(!(l&&ne(l.cardId)&&ne(l.name)&&a))return null;let u=Array.isArray(l.others)?l.others.slice(0,40).map(ae).filter(y=>y!==null):[],p=typeof l.releaseId=="string"&&_e.test(l.releaseId)?l.releaseId:void 0;return{kind:"shown",card:{cardId:l.cardId,name:l.name,...p?{releaseId:p}:{},box:a,others:u}}}default:return null}}return{name:t,tag:s=>({...s,[t]:1}),parseComputeIn:i,parseComputeOut:n}}var le="busy";var He=Object.freeze({misses:2,maxMs:5e3,sameBoxIou:.5});function Be(t,e){return t==="loading"?{phase:e.phase,bytes:e.bytes}:void 0}function ue(t,e,r=40){return(t.hover?.cards??[]).map(i=>i.bounds).filter(i=>i&&!(Math.abs(i.x-e.x)<1e-6&&Math.abs(i.y-e.y)<1e-6&&Math.abs(i.width-e.width)<1e-6)).slice(0,r).map(i=>({x:i.x,y:i.y,width:i.width,height:i.height}))}var M=class{constructor(e,r={}){this.deps=e;this.mode=r.mode??"pointer",this.hold=r.hold??null,this.maxOtherBoxes=r.maxOtherBoxes??40,this.rotatedOutlines=r.rotatedOutlines===!0,this.hitTest=e.hoverAt??B,this.scheduler=new T(F(null,this.mode),3,this.mode,C(null)),this.st={state:"loading",error:null,release:null,backend:null,schedule:"idle",frames:0,lastWorkerMs:null,lastRoundTripMs:null,lastFrameError:null,geometric:r.geometric?{state:"loading",reason:null,bytes:null}:null,loading:null,tilted:null},this.lastStatusKey=this.statusKey()}deps;scheduler;mode;hold;maxOtherBoxes;rotatedOutlines;hitTest;st;cardList=[];analysisWidth={min:720,max:1920};maxAgeMs=W;nextRequestId=1;inFlight=null;lastAcceptedRequestId=0;analyzedSinceLoad=!1;generation=0;sourceCount=0;result=null;pointerAt=null;mediaTime=0;lastShownKey="null";lastStatusKey="";held=null;get videoKey(){return`source-${this.sourceCount}`}pointer(e,r,i,n=!1){if(this.st.state==="stopped")return;let s=this.deps.now();this.pointerAt={x:e,y:r},this.mediaTime=i,n?this.scheduler.tapped(s):this.scheduler.pointerMoved(s),this.st.state==="ready"&&this.scheduler.hoverTriggerDue(s,this.videoTime(),this.inFlight!==null)&&this.requestCapture("hover"),this.publish()}pointerLeft(){this.st.state!=="stopped"&&(this.pointerAt=null,this.scheduler.pointerLeft(this.deps.now()),this.publish())}presented(e){this.st.state!=="stopped"&&(this.mediaTime=e,this.scheduler.framePresented(),this.publish())}discontinuity(e){this.st.state!=="stopped"&&(this.generation+=1,e&&(this.sourceCount+=1),this.scheduler.discontinuity(),this.result=null,this.held=null,e&&this.scheduler.framePresented(),this.publish())}cut(){this.st.state!=="stopped"&&(this.generation+=1,this.result=null,this.held=null,this.scheduler.requestLook(),this.publish())}frame(e,r,i){let n=this.inFlight;if(this.st.state==="stopped"||!n||n.requestId!==e||n.analyzing){r.close();return}if(this.scheduler.pictureReceived(),n.generation!==this.generation||n.videoKey!==this.videoKey){this.inFlight=null,r.close(),this.publish();return}n.analyzing=!0;let s={requestId:n.requestId,mediaTimeSec:i,generation:n.generation,videoKey:n.videoKey};try{this.deps.toWorker({kind:"frame",bitmap:r,videoTimeSec:i,envelope:s,...this.rotatedOutlines?{rotatedOutlines:!0}:{}},[r])}catch(o){r.close(),this.inFlight=null,this.st.lastFrameError=`analysis-failed: ${String(o)}`,this.scheduler.lookUnanswered(this.deps.now())}this.publish()}captureFailed(e,r,i=!1){let n=this.inFlight;this.st.state==="stopped"||!n||n.requestId!==e||n.analyzing||(this.inFlight=null,this.st.lastFrameError=`capture-failed: ${r}`,i?this.scheduler.lookUnanswered(this.deps.now(),{prompt:!0}):this.scheduler.lookRefused(),this.publish())}fromWorker(e){if(this.st.state!=="stopped")switch(e.kind){case"geometric-status":this.st.geometric={state:e.state,reason:e.reason,bytes:e.bytes},this.deps.onGeometricStatus?.(e),this.publishStatus();return;case"tilted-status":this.st.tilted={state:"off",reason:e.reason},this.deps.onTiltedStatus?.(e),this.publishStatus();return;case"release-progress":{let r=Be(this.st.state,e);r!==void 0&&(this.st.loading=r),this.publishStatus();return}case"release-status":this.st.state=e.ok?"ready":"failed",this.analyzedSinceLoad=!1,this.st.error=e.error,this.st.release=e.release,this.st.loading=null,this.st.tilted=e.release?.tiltedRetry?{state:"on",reason:null}:null,this.cardList=e.cards,e.release&&(this.st.backend=e.release.recognizer.ep,this.maxAgeMs=e.release.hoverMaxAgeMs,this.analysisWidth={...e.release.analysisWidth},this.scheduler.setIntervalMs(F(this.st.backend,this.mode)),this.scheduler.setReadingIntervalMs(C(this.st.backend)),this.scheduler.framePresented()),this.publish();return;case"frame-result":this.onFrameResult(e);return;default:return}}onFrameResult(e){let r=this.deps.now(),i=H(e.envelope,{inFlightRequestId:this.inFlight?.requestId??null,lastAcceptedRequestId:this.lastAcceptedRequestId,generation:this.generation,videoKey:this.videoKey});if(i!==null&&!$(i))return;let n=this.inFlight;if(this.inFlight=null,i!==null){this.publish();return}this.lastAcceptedRequestId=e.envelope.requestId,this.st.lastFrameError=e.error??null,this.st.lastWorkerMs=e.workerMs,this.st.lastRoundTripMs=+(r-n.sentAt).toFixed(2),e.analysis?(this.analyzedSinceLoad=!0,this.scheduler.lookAnswered(),this.st.frames+=1,this.result={analysis:e.analysis,videoTimeSec:e.videoTimeSec},this.deps.onAnalysis?.(e.analysis)):(this.result=null,e.error===le&&this.scheduler.lookUnanswered(r)),this.held&&this.pointerAt&&this.current()&&!this.fresh()&&(this.held.misses+=1),this.scheduler.setTooltipShowing(this.shown()!==null,r),this.publish()}tick(){if(this.st.state==="stopped")return;let e=this.deps.now(),r=this.analyzedSinceLoad?K:X;this.inFlight&&e-this.inFlight.sentAt>=r&&(this.inFlight=null,this.st.lastFrameError="no reply",this.scheduler.lookUnanswered(e)),this.scheduler.setTooltipShowing(this.shown()!==null,e),this.st.state==="ready"&&!this.inFlight&&this.scheduler.due(e)&&this.requestCapture(this.scheduler.lookPending?"cut":void 0),this.publish()}stop(){this.st.state!=="stopped"&&(this.st.state="stopped",this.st.loading=null,this.inFlight=null,this.result=null,this.held=null,this.pointerAt=null,this.publishShown(),this.publishStatus())}workerStopped(e){if(!(this.st.state==="stopped"||this.st.state==="failed")){if(this.inFlight=null,this.result=null,this.held=null,this.st.loading=null,this.publishShown(),"restart"in e){let r={kind:"geometric-status",state:"off",reason:"worker-restarted",bytes:null};this.st.geometric&&(this.st.geometric={state:r.state,reason:r.reason,bytes:r.bytes}),this.deps.onGeometricStatus?.(r),this.st.state="loading",this.analyzedSinceLoad=!1,this.st.error=null}else this.st.state="failed",this.st.error=e.error;this.publishStatus()}}reloading(e){this.st.state==="loading"&&(this.st.geometric=e.geometric?{state:"loading",reason:null,bytes:null}:null,this.st.loading=null,this.publishStatus())}requestCapture(e){if(this.st.state!=="ready"||this.inFlight)return;let r=this.deps.now(),i=this.nextRequestId++;this.inFlight={requestId:i,sentAt:r,generation:this.generation,videoKey:this.videoKey,analyzing:!1},this.scheduler.started(r,{videoTimeSec:this.videoTime(),...e?{trigger:e}:{}}),this.deps.capture(i,{...this.analysisWidth})}status(){return{...this.st,schedule:this.scheduler.analysisState(this.deps.now())}}analysis(){return this.current()?.analysis??null}cards(){return this.cardList}hitAt(e,r){let i=this.current();if(!i)return null;let{width:n,height:s}=i.analysis.frame;if(!(n>0&&s>0))return null;let o=e*n,l=r*s;if(!Number.isFinite(o)||!Number.isFinite(l))return null;let a=this.hitTest(i.analysis,o,l);if(!a.box||a.cardId===null||a.name===null)return null;let u={x:a.box.x/n,y:a.box.y/s,width:a.box.width/n,height:a.box.height/s},p={x:a.box.x,y:a.box.y,width:a.box.width,height:a.box.height};return{cardId:a.cardId,name:a.name,box:u,frameBox:p,others:ue(i.analysis,u,this.maxOtherBoxes)}}fresh(){return this.pointerAt?this.hitAt(this.pointerAt.x,this.pointerAt.y):null}shown(){let e=this.fresh(),r=this.hold;if(!r)return e;let i=this.deps.now();if(e)return this.held={card:e,misses:0,seenAt:i},e;let n=this.held;return!n||!this.pointerAt||!qe(n.card.box,this.pointerAt)?(this.held=null,null):this.boxAtPointer(n.card.box,r.sameBoxIou)==="other"||n.misses>=r.misses||i-n.seenAt>=r.maxMs?(this.held=null,null):n.card}current(){let e=this.result;return e&&z(e.videoTimeSec,this.videoTime(),this.maxAgeMs)?e:null}videoTime(){return this.deps.mediaTimeNow?this.deps.mediaTimeNow():this.mediaTime}boxAtPointer(e,r){let i=this.current();if(!i||!this.pointerAt)return"unknown";let{width:n,height:s}=i.analysis.frame;if(!(n>0&&s>0))return"unknown";let o=this.pointerAt.x*n,l=this.pointerAt.y*s,a={x:e.x*n,y:e.y*s,width:e.width*n,height:e.height*s},u=0,p=!1;for(let y of i.analysis.boxes??[]){let[g,b,v,k]=y.box;og+v||lb+k||(p=!0,u=Math.max(u,Ne(a,{x:g,y:b,width:v,height:k})))}return p?u>=r?"same":"other":"none"}publish(){this.publishShown(),this.publishStatus()}publishShown(){let e=this.shown(),r=e?`${e.cardId}|${e.box.x}|${e.box.y}|${e.box.width}|${e.box.height}`:"null";r!==this.lastShownKey&&(this.lastShownKey=r,this.deps.onShown?.(e))}statusKey(){let e=this.status();return JSON.stringify({...e,release:e.release?e.release.id:null})}publishStatus(){let e=this.statusKey();e!==this.lastStatusKey&&(this.lastStatusKey=e,this.deps.onStatus?.(this.status()))}};function Ne(t,e){let r=Math.min(t.x+t.width,e.x+e.width)-Math.max(t.x,e.x),i=Math.min(t.y+t.height,e.y+e.height)-Math.max(t.y,e.y);if(r<=0||i<=0)return 0;let n=r*i;return n/(t.width*t.height+e.width*e.height-n)}function qe(t,e){return e.x>=t.x&&e.x<=t.x+t.width&&e.y>=t.y&&e.y<=t.y+t.height}var St=1440*60*1e3;var je=[/^the worker did not start/,/^unexpected error while loading/];function de(t,e){return e||t.startsWith("unexpected:")?!1:!je.some(r=>r.test(t))}var ce=1;function he(t){let{geometricUrl:e,geometricManifestSha256:r,geometricWatchdog:i,...n}=t;return n}function D(t){let e=null,r=null,i=0,n=!1,s=null,o=!1,l=null,a={source:t.chooseRelease?"choosing":"bundled",id:null,reason:null,table:null},u=d=>{t.onDecision?.({reused:0,downloaded:0,ms:0,...d})},p=d=>new M({now:t.now,toWorker:(h,f)=>e?.postMessage(h,f??[]),capture:t.capture,...t.onShown?{onShown:t.onShown}:{},...t.onStatus?{onStatus:t.onStatus}:{},...t.onAnalysis?{onAnalysis:t.onAnalysis}:{},...t.onGeometricStatus?{onGeometricStatus:t.onGeometricStatus}:{},...t.hoverAt?{hoverAt:t.hoverAt}:{},...t.mediaTimeNow?{mediaTimeNow:t.mediaTimeNow}:{}},{...t.mode?{mode:t.mode}:{},...t.hold?{hold:t.hold}:{},...t.rotatedOutlines?{rotatedOutlines:!0}:{},geometric:d.geometricUrl!==void 0}),y=d=>{n||!r||(s&&d.kind==="release-progress"&&d.phase==="preparing"&&(o=!0),!(d.kind==="release-status"&&(r.status().state!=="loading"||s&&!b(d)))&&(d.kind==="release-status"&&!d.ok&&g(),r.fromWorker(d)))},g=()=>{n=!0,s=null,e?.terminate(),e=null},b=d=>{let h=s;if(d.ok&&d.release?.id===h.pointer.id)return s=null,l=h.pointer.id,a.source="remote",a.id=h.pointer.id,a.reason="ok",a.table=h.facts,u({source:"remote",reason:"ok",pointer:h.pointer.id,reused:h.reused,downloaded:h.downloaded,ms:h.ms}),!0;if(d.ok)return t.recordSkip?.(h.pointer,"id-mismatch"),v("id-mismatch"),!1;let f=d.error??"no reason given";return de(f,o)&&t.recordSkip?.(h.pointer,`runtime:${f.slice(0,120)}`),v(`runtime:${f}`),!1},v=d=>{let h=s;s=null,e?.terminate(),a.source="bundled",a.id=null,a.reason=d,a.table=null,u({source:"bundled",reason:d,pointer:h.pointer.id,reused:h.reused,downloaded:h.downloaded,ms:h.ms});let f=h.fallback??t.loadRequest;r?.reloading({geometric:f.geometricUrl!==void 0}),I(f)},k=d=>{if(n||!r)return;let h=r.status().state;if(h==="failed"||h==="stopped")return;let f=d.message||"see the console";if(s){b({kind:"release-status",ok:!1,error:`the worker did not start (${f})`,release:null,cards:[]});return}if(e?.terminate(),h==="loading"&&i===0){g(),r.workerStopped({error:`the worker did not start (${f})`});return}if(i{let h=t.createWorker();h.onmessage=f=>{h===e&&y(f.data)},h.onerror=f=>{h===e&&k(f)},e=h,h.postMessage(d)},I=d=>{r??=p(d);try{_(d)}catch(h){e=null,s=null,n=!0,r.workerStopped({error:`the recognition worker could not start (${String(h)})`})}};return t.chooseRelease?t.chooseRelease({onDownloading:()=>t.onDownloading?.()}).catch(d=>({source:"bundled",reason:`unexpected:${String(d)}`,pointer:null,downloaded:0,ms:0})).then(d=>{if(!n){if(d.source==="remote"){s=d,o=!1,I(d.request);return}a.source="bundled",a.reason=d.reason,u({source:"bundled",reason:d.reason,pointer:d.pointer?.id??null,downloaded:d.downloaded,ms:d.ms}),I(d.request??t.loadRequest)}}):I(t.loadRequest),{get core(){return r},get stopped(){return n},get restarts(){return i},get release(){return{...a}},tick(){n||r?.tick()},stop(){n=!0,s=null,e?.terminate(),e=null,r?.stop()}}}var De=/^[0-9a-f]{16}$/;function Ve(){let t=crypto.getRandomValues(new Uint8Array(8));return Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")}function Ge(t){let e=t.parentOrigin;if(!t.framed||e===null||!t.allowedParentOrigin(e))return null;let{win:r,protocol:i}=t,n=(t.instanceId??Ve)();if(typeof n!="string"||!De.test(n))throw new TypeError(`startComputeFrame: not an instance id: ${String(n)}`);let s=c=>r.parent.postMessage(i.tag(c),e),o=()=>s({kind:"ready",version:1,instance:n}),l=!1,a=()=>{l||(l=!0,o())},u={state:"loading",error:null,backend:null},p=c=>c===null?null:c.slice(0,200),y=(c,m,S,R=!1)=>{let w={state:c,error:p(m),backend:p(S)};!R&&w.state===u.state&&w.error===u.error&&w.backend===u.backend||(u=w,s({kind:"status",...w}))},g=c=>{c.state!=="stopped"&&y(c.state,c.error,c.state==="ready"?c.backend:null)},b=null,v=()=>b?.release??{source:t.chooseRelease?"choosing":"bundled",id:null,reason:null,table:null},k=null,_=c=>{k=c,t.onShown?.(c,v()),s({kind:"shown",card:c})},I=c=>{let m=v(),S=c?{cardId:c.cardId,name:c.name,...m.source==="remote"&&m.id!==null?{releaseId:m.id}:{},box:{...c.box},others:c.others.map(R=>({...R}))}:null;_(S)},{win:d,framed:h,parentOrigin:f,allowedParentOrigin:Ke,protocol:Xe,onShown:$e,instanceId:Ue,...pe}=t,x=D({...pe,createWorker:()=>(a(),t.createWorker()),capture:(c,m)=>{let S=Math.min(m.max,4096);s({kind:"capture",requestId:c,analysisWidth:{min:Math.min(m.min,S),max:S}})},onShown:I,onStatus:g,onDownloading:()=>{y("loading",null,null,!0),t.onDownloading?.()}});b=x,x.core&&a();let me=()=>{o(),y(u.state,u.error,u.backend,!0),_(k)},fe=(c,m)=>{switch(m.kind){case"pointer":c.pointer(m.x,m.y,m.mediaTime,m.touch);return;case"leave":c.pointerLeft();return;case"presented":c.presented(m.mediaTime);return;case"discontinuity":c.discontinuity(m.newSource);return;case"cut":c.cut();return;case"frame":c.frame(m.requestId,m.bitmap,m.mediaTime);return;case"capture-failed":c.captureFailed(m.requestId,m.reason,m.retry===!0);return}},V=c=>{let S=c.source===r.parent&&c.origin===e?i.parseComputeIn(c.data):null,R=x.core,w=null;try{S?.kind==="hello"?R&&me():S&&R&&!x.stopped&&(S.kind==="frame"&&(w=S.bitmap),fe(R,S))}finally{ze(c.data,w)}};return r.addEventListener("message",V),{get core(){return x.core},get stopped(){return x.stopped},get restarts(){return x.restarts},get release(){return x.release},instance:n,tick(){x.tick()},stop(){r.removeEventListener("message",V),x.stop()}}}function ze(t,e){if(!(typeof ImageBitmap>"u"||typeof t!="object"||t===null))for(let r of Object.values(t))r!==e&&r instanceof ImageBitmap&&r.close()}export{be as DRIVER_TICK_MS,T as FrameScheduler,W as HOVER_MAX_AGE_MS,q as MAX_FRAME_SIDE,P as MAX_OTHER_BOXES,Me as MAX_REASON_LENGTH,j as MAX_TEXT_LENGTH,N as PROTOCOL_VERSION,He as SHOWN_HOLD,M as SessionCore,ce as WORKER_RESTARTS,F as cadenceMs,We as computeProtocol,D as createWorkerSession,B as hoverAt,ue as neighborBoxes,he as plainLoad,H as replyRejection,Ge as startComputeFrame}; diff --git a/vendor/cardvision-browser/page-capture.js b/vendor/cardvision-browser/page-capture.js index ca3febd..483d50d 100644 --- a/vendor/cardvision-browser/page-capture.js +++ b/vendor/cardvision-browser/page-capture.js @@ -1 +1 @@ -function K(e,n,r={min:720,max:1920}){let s=Math.max(r.min,Math.min(r.max,e));return{width:s,height:Math.max(1,Math.round(s*n/e))}}function z(e,n,r){if(!(e.width>0&&e.height>0))return null;let s=(n-e.left)/e.width,c=(r-e.top)/e.height;return s>=0&&s<=1&&c>=0&&c<=1?{x:s,y:c}:null}function j(e,n,r){let s={left:e.left,top:e.top,width:e.width,height:e.height};if(n.width<=0||n.height<=0||r==="fill")return s;let c=e.width/n.width,l=e.height/n.height,t;r==="cover"?t=Math.max(c,l):r==="none"?t=1:r==="scale-down"?t=Math.min(1,c,l):t=Math.min(c,l);let a=n.width*t,u=n.height*t;return{left:e.left+(e.width-a)/2,top:e.top+(e.height-u)/2,width:a,height:u}}var M=1,$=4096,Ne=40,Fe=200,Oe=200,De=/^[0-9a-f]{16}$/,Ge=/^[0-9a-f]{16}$/,I=e=>typeof e=="number"&&Number.isFinite(e),oe=e=>I(e)&&e>=0&&e<=1,O=e=>Number.isSafeInteger(e)&&e>0,ae=(e,n=200)=>typeof e=="string"&&e.length>0&&e.length<=n,se=O,ue=e=>Number.isInteger(e)&&e>=1&&e<=4096,le=e=>e===null?null:typeof e=="string"?e.slice(0,200):void 0;function Be(e){return typeof ImageBitmap<"u"&&e instanceof ImageBitmap}function Xe(e){let{width:n,height:r}=e;return Number.isInteger(n)&&Number.isInteger(r)&&n>0&&r>0&&n<=4096&&r<=4096}function ce(e){if(typeof e!="object"||e===null)return null;let n=e;return I(n.x)&&I(n.y)&&I(n.width)&&I(n.height)&&n.width>0&&n.height>0?{x:n.x,y:n.y,width:n.width,height:n.height}:null}var He=new Set(["kind","x","y","mediaTime","touch","newSource","requestId","bitmap","reason","retry","state","error","backend","analysisWidth","card","version","instance"]),Ue=/^[a-z][a-z0-9-]*$/;function We(e="cardvision"){if(typeof e!="string"||!Ue.test(e)||He.has(e))throw new TypeError(`computeProtocol: not a usable tag: ${String(e)}`);function n(l){if(typeof l!="object"||l===null)return null;let t=l;return t[e]===1&&typeof t.kind=="string"?t:null}function r(l){return Be(l.bitmap)?O(l.requestId)&&I(l.mediaTime)&&Xe(l.bitmap)?{kind:"frame",requestId:l.requestId,bitmap:l.bitmap,mediaTime:l.mediaTime}:(l.bitmap.close(),null):null}function s(l){let t=n(l);if(!t)return null;switch(t.kind){case"hello":return se(t.version)?{kind:"hello",version:t.version}:null;case"pointer":return oe(t.x)&&oe(t.y)&&I(t.mediaTime)&&typeof t.touch=="boolean"?{kind:"pointer",x:t.x,y:t.y,mediaTime:t.mediaTime,touch:t.touch}:null;case"leave":return{kind:"leave"};case"presented":return I(t.mediaTime)?{kind:"presented",mediaTime:t.mediaTime}:null;case"discontinuity":return typeof t.newSource=="boolean"?{kind:"discontinuity",newSource:t.newSource}:null;case"cut":return{kind:"cut"};case"frame":return r(t);case"capture-failed":return O(t.requestId)&&typeof t.reason=="string"?{kind:"capture-failed",requestId:t.requestId,reason:t.reason.slice(0,200),retry:t.retry===!0}:null;default:return null}}function c(l){let t=n(l);if(!t)return null;switch(t.kind){case"ready":return se(t.version)&&typeof t.instance=="string"&&Ge.test(t.instance)?{kind:"ready",version:t.version,instance:t.instance}:null;case"status":{let a=le(t.error),u=le(t.backend);return(t.state==="loading"||t.state==="ready"||t.state==="failed")&&a!==void 0&&u!==void 0?{kind:"status",state:t.state,error:a,backend:u}:null}case"capture":{let a=t.analysisWidth;return O(t.requestId)&&typeof a=="object"&&a!==null&&ue(a.min)&&ue(a.max)&&a.max>=a.min?{kind:"capture",requestId:t.requestId,analysisWidth:{min:a.min,max:a.max}}:null}case"shown":{if(t.card===null)return{kind:"shown",card:null};let a=t.card,u=typeof a=="object"&&a!==null?ce(a.box):null;if(!(a&&ae(a.cardId)&&ae(a.name)&&u))return null;let o=Array.isArray(a.others)?a.others.slice(0,40).map(ce).filter(g=>g!==null):[],f=typeof a.releaseId=="string"&&De.test(a.releaseId)?a.releaseId:void 0;return{kind:"shown",card:{cardId:a.cardId,name:a.name,...f?{releaseId:f}:{},box:u,others:o}}}default:return null}}return{name:e,tag:l=>({...l,[e]:1}),parseComputeIn:s,parseComputeOut:c}}var J="no picture",de="no picture (the paused video draws nothing)",me="no 2d context",fe="the video may not be read",pe="the picture is too large",Ve=16,qe=9,he=()=>document.createElement("canvas"),Ye=e=>createImageBitmap(e);function D(){return{canvas:null,readable:!1}}function Q(e){return e.paused&&e.readyState<2&&e.videoWidth>0}function G(e,n){let r=n?.lastPlayingTime??null;return Q(e)&&r!==null?r:e.currentTime}function ge(e,n=he){let r=Ve,s=n();s.width=r,s.height=r;let c=s.getContext("2d");if(!c)return!1;c.drawImage(e,0,0,r,r);let l=c.getImageData(0,0,r,r).data,t=0,a=0;for(let o=0;o({kind:"failed",reason:p,retry:A}),c=e?.video;if(!e||!c||(r.requireConnected??!0)&&!c.isConnected||c.videoWidth===0)return s(J,!0);let l=Q(c);if(c.readyState<2&&!l)return s(J,!0);let t=r.createCanvas??he,a=K(c.videoWidth,c.videoHeight,n),u=r.maxSide??4096;if(a.width>u||a.height>u)return s(pe);let o=e.source;o.canvas||(o.canvas=t(),o.readable=!1);let f=o.canvas;f.width=a.width,f.height=a.height;let g=f.getContext("2d");if(!g)return s(me);let S=G(c,e);try{return g.drawImage(c,0,0,a.width,a.height),l&&ge(f,t)?(o.readable=!0,s(de,!0)):(o.readable||(g.getImageData(0,0,1,1),o.readable=!0),{kind:"frame",bitmap:await(r.createImageBitmap??Ye)(f),mediaTime:S})}catch(p){return s(Ke(p)?fe:String(p))}}function ee(e,n){let r=null,s=D(),c={video:e,get lastPlayingTime(){return r},get source(){return s},mediaTime:()=>G(e,c),detach:g},l=()=>n.discontinuity(!1),t=()=>{s=D(),r=null,n.discontinuity(!0)},a=()=>{!e.paused&&e.readyState>=2&&(r=e.currentTime),n.presented(c.mediaTime())},u=()=>n.presented(c.mediaTime()),o=[["seeking",l],["emptied",t],["loadeddata",t],["timeupdate",a],["seeked",a],["canplay",u],["playing",u]];for(let[S,p]of o)e.addEventListener(S,p);let f=!0;function g(){if(f){f=!1;for(let[S,p]of o)e.removeEventListener(S,p)}}return c}var te=1e4,ne=5e3;var be=6e4;function B(e,n,{graceMs:r=te,lingerMs:s=ne,restMs:c=be}={}){return!e.inside||!Number.isFinite(e.lastTrustedPointerAt)||n-e.lastTrustedPointerAt>=c?!1:n-e.lastTrustedPointerAtsetTimeout(e,n),clear:e=>clearTimeout(e)};function Qe(e,n,r){let s=r.ownerDocument?.defaultView,c=s?s.getComputedStyle(r).objectFit:"contain";return z(j(r.getBoundingClientRect(),{width:r.videoWidth,height:r.videoHeight},c),e,n)}var Ze=e=>Math.min(2e3,250*2**e);function et(e){let{protocol:n,frameOrigin:r}=e,s=e.now??(()=>performance.now()),c=e.eligible??Qe,l=e.timers??Je,t=e.helloRetries??10,a=e.helloDelayMs??Ze,u=null,o=null,f=!1,g=-1/0,S=0,p=null,A=null,k=0,X=null,H=!1,b=!1,U=new Set;function R(){let i=e.frame();return p&&i===p.window?i:null}function re(i){try{return i.location.origin===r}catch{return!0}}function w(i,m=R()){if(!(b||!m||!re(m)))try{m.postMessage(n.tag(i),r)}catch{}}function L(i,m){return u?c(i,m,u.video):null}function y(){let i=o!==null;o=null,f=!1,g=-1/0,S++,i&&w({kind:"leave"})}function W(){return o?o.kind==="tap"?{inside:!0,lastTrustedPointerAt:o.at,tooltipShowing:!1,lastTooltipAt:-1/0}:{inside:!0,lastTrustedPointerAt:o.at,tooltipShowing:f,lastTooltipAt:g}:{inside:!1,lastTrustedPointerAt:-1/0,tooltipShowing:f,lastTooltipAt:g}}function ie(){return!o||!u?!1:L(o.clientX,o.clientY)?B(W(),s(),e.limits):(y(),!1)}function P(){if(!o||!u)return;let i=L(o.clientX,o.clientY);i&&w({kind:"pointer",x:i.x,y:i.y,mediaTime:u.mediaTime(),touch:o.kind==="tap"})}function we(i){if(b||i.isTrusted!==!0||!u)return;let m=i.pointerType==="touch"?"tap":"hover",d=i.pointerId,h=L(i.clientX,i.clientY);if(m==="hover"){h?(o={kind:m,pointerId:d,clientX:i.clientX,clientY:i.clientY,at:s()},P()):o?.kind==="hover"&&y();return}let T=o?.kind==="tap"&&o.pointerId===d;if(i.type==="pointerdown")h?(o={kind:m,pointerId:d,clientX:i.clientX,clientY:i.clientY,at:s()},P()):o?.kind==="tap"&&y();else if(i.type==="pointermove"&&T&&o){if(!h)return y();o.clientX=i.clientX,o.clientY=i.clientY,P()}}function xe(i){b||i.isTrusted!==!0||o&&o.pointerId===i.pointerId&&y()}function Ie(i){i.visibilityState==="hidden"&&y()}function _e(i){b||(u?.video??null)===i||(y(),u?.detach(),u=null,i&&(u=ee(i,{presented:d=>w({kind:"presented",mediaTime:d}),discontinuity:d=>{d&&y(),w({kind:"discontinuity",newSource:d})}})),w({kind:"discontinuity",newSource:!0}))}function N(){A!==null&&l.clear(A),A=null}function V(){if(A=null,b||R())return;let i=e.frame();if(i&&!re(i)){let m=s();X??=m,m-X=t){e.onProblem?.({kind:"no-answer"});return}w({kind:"hello",version:1},i),A=l.set(V,a(k)),k++}function Re(){b||(N(),k=0,X=null,V())}function Ce(i,m){let{version:d,instance:h}=i;if(d!==1){p=null,N(),y(),e.onProblem?.({kind:"version",expected:1,received:d});return}if(typeof h!="string"||!ze.test(h)||(N(),p&&p.window===m&&p.instance===h))return;p={window:m,instance:h};let T=o;f=!1,g=-1/0,S++,T&&!(L(T.clientX,T.clientY)&&B(W(),s(),e.limits))&&(o=null),u&&w({kind:"presented",mediaTime:u.mediaTime()}),P()}async function Me(i,m){let d=(x,C=!1)=>w({kind:"capture-failed",requestId:i,reason:x,retry:C});if(H)return d(Te);let h;try{h=ie()}catch(x){return d(String(x))}if(!h)return d(ye);let T={watch:u,source:u?.source,handshake:p,resets:S};H=!0;let E;try{E=await Z(u,m,e.grab)}catch(x){E={kind:"failed",reason:String(x),retry:!1}}finally{H=!1}let q=!b&&p===T.handshake&&R()!==null,F=u===T.watch&&u?.source===T.source&&S===T.resets;if(!q||!F){E.kind==="frame"&&E.bitmap.close(),q&&d(Ee);return}if(E.kind==="failed")return d(E.reason,E.retry);Ae(R(),n.tag({kind:"frame",requestId:i,bitmap:E.bitmap,mediaTime:E.mediaTime}),r,E.bitmap)}function ke(i){if(b)return!1;let m=e.frame(),d=ve(i,{frame:m,origin:r,protocol:n});if(!d||!m)return!1;if(d.kind==="ready")return Ce(d,m),!0;if(!R())return!0;switch(d.kind){case"status":e.onStatus?.(d);break;case"shown":{o&&((f||d.card)&&(g=s()),f=d.card!==null),e.onShown?.(d.card);break}case"capture":Me(d.requestId,d.analysisWidth);break}return!0}function Le(i,m){let d={capture:!0,passive:!0},h=v=>we(v),F=[[i,"pointermove",h],[i,"pointerdown",h],[i,"pointerup",h],[i,"pointercancel",v=>xe(v)],[i,"pointerleave",v=>{let _=v;b||_.isTrusted!==!0||_.target!==m.documentElement||_.pointerType==="touch"||y()}],[i,"visibilitychange",()=>Ie(m)]];for(let[v,_,Y]of F)v.addEventListener(_,Y,d);let x=!0,C=()=>{if(x){x=!1,U.delete(C);for(let[v,_,Y]of F)v.removeEventListener(_,Y,d)}};return U.add(C),C}function Pe(){if(!b){y(),b=!0,N();for(let i of[...U])i();u?.detach(),u=null,p=null}}return{attach:_e,listen:Le,connect:Re,onMessage:ke,get watch(){return u},get connected(){return R()!==null},gateState:W,captureAllowedNow:ie,stop:Pe}}export{de as BLANK_PICTURE,Te as CAPTURE_PENDING,$ as MAX_FRAME_SIDE,Ne as MAX_OTHER_BOXES,Fe as MAX_REASON_LENGTH,be as MAX_REST_MS,Oe as MAX_TEXT_LENGTH,ye as NOT_POINTING,me as NO_CONTEXT,J as NO_PICTURE,Ee as PICTURE_CHANGED,te as POINTER_GRACE_MS,M as PROTOCOL_VERSION,ne as READING_LINGER_MS,pe as TOO_LARGE,fe as UNREADABLE,ve as acceptComputeOut,K as analysisSize,ge as blankFrame,B as captureAllowed,We as computeProtocol,et as createCaptureControl,D as createSourceCanvas,z as fractionIn,Se as fromExtensionFrame,Z as grabFrame,Q as isFrozen,G as mediaTimeOf,j as pictureBox,Ae as postWithTransfer,ee as watchVideo}; +function K(e,n,r={min:720,max:1920}){let s=Math.max(r.min,Math.min(r.max,e));return{width:s,height:Math.max(1,Math.round(s*n/e))}}function z(e,n,r){if(!(e.width>0&&e.height>0))return null;let s=(n-e.left)/e.width,c=(r-e.top)/e.height;return s>=0&&s<=1&&c>=0&&c<=1?{x:s,y:c}:null}function j(e,n,r){let s={left:e.left,top:e.top,width:e.width,height:e.height};if(n.width<=0||n.height<=0||r==="fill")return s;let c=e.width/n.width,l=e.height/n.height,t;r==="cover"?t=Math.max(c,l):r==="none"?t=1:r==="scale-down"?t=Math.min(1,c,l):t=Math.min(c,l);let a=n.width*t,u=n.height*t;return{left:e.left+(e.width-a)/2,top:e.top+(e.height-u)/2,width:a,height:u}}var M=1,$=4096,Ne=40,Fe=200,Oe=200,De=/^[0-9a-f]{16}$/,Ge=/^[0-9a-f]{16}$/,I=e=>typeof e=="number"&&Number.isFinite(e),oe=e=>I(e)&&e>=0&&e<=1,O=e=>Number.isSafeInteger(e)&&e>0,ae=(e,n=200)=>typeof e=="string"&&e.length>0&&e.length<=n,se=O,ue=e=>Number.isInteger(e)&&e>=1&&e<=4096,le=e=>e===null?null:typeof e=="string"?e.slice(0,200):void 0;function Be(e){return typeof ImageBitmap<"u"&&e instanceof ImageBitmap}function Xe(e){let{width:n,height:r}=e;return Number.isInteger(n)&&Number.isInteger(r)&&n>0&&r>0&&n<=4096&&r<=4096}function ce(e){if(typeof e!="object"||e===null)return null;let n=e;return I(n.x)&&I(n.y)&&I(n.width)&&I(n.height)&&n.width>0&&n.height>0?{x:n.x,y:n.y,width:n.width,height:n.height}:null}var He=new Set(["kind","x","y","mediaTime","touch","newSource","requestId","bitmap","reason","retry","state","error","backend","analysisWidth","card","version","instance"]),Ue=/^[a-z][a-z0-9-]*$/;function We(e="cardvision"){if(typeof e!="string"||!Ue.test(e)||He.has(e))throw new TypeError(`computeProtocol: not a usable tag: ${String(e)}`);function n(l){if(typeof l!="object"||l===null)return null;let t=l;return t[e]===1&&typeof t.kind=="string"?t:null}function r(l){return Be(l.bitmap)?O(l.requestId)&&I(l.mediaTime)&&Xe(l.bitmap)?{kind:"frame",requestId:l.requestId,bitmap:l.bitmap,mediaTime:l.mediaTime}:(l.bitmap.close(),null):null}function s(l){let t=n(l);if(!t)return null;switch(t.kind){case"hello":return se(t.version)?{kind:"hello",version:t.version}:null;case"pointer":return oe(t.x)&&oe(t.y)&&I(t.mediaTime)&&typeof t.touch=="boolean"?{kind:"pointer",x:t.x,y:t.y,mediaTime:t.mediaTime,touch:t.touch}:null;case"leave":return{kind:"leave"};case"presented":return I(t.mediaTime)?{kind:"presented",mediaTime:t.mediaTime}:null;case"discontinuity":return typeof t.newSource=="boolean"?{kind:"discontinuity",newSource:t.newSource}:null;case"cut":return{kind:"cut"};case"frame":return r(t);case"capture-failed":return O(t.requestId)&&typeof t.reason=="string"?{kind:"capture-failed",requestId:t.requestId,reason:t.reason.slice(0,200),retry:t.retry===!0}:null;default:return null}}function c(l){let t=n(l);if(!t)return null;switch(t.kind){case"ready":return se(t.version)&&typeof t.instance=="string"&&Ge.test(t.instance)?{kind:"ready",version:t.version,instance:t.instance}:null;case"status":{let a=le(t.error),u=le(t.backend);return(t.state==="loading"||t.state==="ready"||t.state==="failed")&&a!==void 0&&u!==void 0?{kind:"status",state:t.state,error:a,backend:u}:null}case"capture":{let a=t.analysisWidth;return O(t.requestId)&&typeof a=="object"&&a!==null&&ue(a.min)&&ue(a.max)&&a.max>=a.min?{kind:"capture",requestId:t.requestId,analysisWidth:{min:a.min,max:a.max}}:null}case"shown":{if(t.card===null)return{kind:"shown",card:null};let a=t.card,u=typeof a=="object"&&a!==null?ce(a.box):null;if(!(a&&ae(a.cardId)&&ae(a.name)&&u))return null;let o=Array.isArray(a.others)?a.others.slice(0,40).map(ce).filter(g=>g!==null):[],p=typeof a.releaseId=="string"&&De.test(a.releaseId)?a.releaseId:void 0;return{kind:"shown",card:{cardId:a.cardId,name:a.name,...p?{releaseId:p}:{},box:u,others:o}}}default:return null}}return{name:e,tag:l=>({...l,[e]:1}),parseComputeIn:s,parseComputeOut:c}}var J="no picture",de="no picture (the paused video draws nothing)",me="no 2d context",pe="the video may not be read",fe="the picture is too large",Ve=16,qe=9,he=()=>document.createElement("canvas"),Ye=e=>createImageBitmap(e);function D(){return{canvas:null,readable:!1}}function Q(e){return e.paused&&e.readyState<2&&e.videoWidth>0}function G(e,n){let r=n?.lastPlayingTime??null;return Q(e)&&r!==null?r:e.currentTime}function ge(e,n=he){let r=Ve,s=n();s.width=r,s.height=r;let c=s.getContext("2d");if(!c)return!1;c.drawImage(e,0,0,r,r);let l=c.getImageData(0,0,r,r).data,t=0,a=0;for(let o=0;o({kind:"failed",reason:f,retry:A}),c=e?.video;if(!e||!c||(r.requireConnected??!0)&&!c.isConnected||c.videoWidth===0)return s(J,!0);let l=Q(c);if(c.readyState<2&&!l)return s(J,!0);let t=r.createCanvas??he,a=K(c.videoWidth,c.videoHeight,n),u=r.maxSide??4096;if(a.width>u||a.height>u)return s(fe);let o=e.source;o.canvas||(o.canvas=t(),o.readable=!1);let p=o.canvas;p.width=a.width,p.height=a.height;let g=p.getContext("2d");if(!g)return s(me);let E=G(c,e);try{return g.drawImage(c,0,0,a.width,a.height),l&&ge(p,t)?(o.readable=!0,s(de,!0)):(o.readable||(g.getImageData(0,0,1,1),o.readable=!0),{kind:"frame",bitmap:await(r.createImageBitmap??Ye)(p),mediaTime:E})}catch(f){return s(Ke(f)?pe:String(f))}}function ee(e,n){let r=null,s=D(),c={video:e,get lastPlayingTime(){return r},get source(){return s},mediaTime:()=>G(e,c),detach:g},l=()=>n.discontinuity(!1),t=()=>{s=D(),r=null,n.discontinuity(!0)},a=()=>{!e.paused&&e.readyState>=2&&(r=e.currentTime),n.presented(c.mediaTime())},u=()=>n.presented(c.mediaTime()),o=[["seeking",l],["emptied",t],["loadeddata",t],["timeupdate",a],["seeked",a],["canplay",u],["playing",u]];for(let[E,f]of o)e.addEventListener(E,f);let p=!0;function g(){if(p){p=!1;for(let[E,f]of o)e.removeEventListener(E,f)}}return c}var te=1e4,ne=5e3;var be=6e4;function B(e,n,{graceMs:r=te,lingerMs:s=ne,restMs:c=be}={}){return!e.inside||!Number.isFinite(e.lastTrustedPointerAt)||n-e.lastTrustedPointerAt>=c?!1:n-e.lastTrustedPointerAtsetTimeout(e,n),clear:e=>clearTimeout(e)};function Qe(e,n,r){let s=r.ownerDocument?.defaultView,c=s?s.getComputedStyle(r).objectFit:"contain";return z(j(r.getBoundingClientRect(),{width:r.videoWidth,height:r.videoHeight},c),e,n)}var Ze=e=>Math.min(2e3,250*2**e);function et(e){let{protocol:n,frameOrigin:r}=e,s=e.now??(()=>performance.now()),c=e.eligible??Qe,l=e.timers??Je,t=e.helloRetries??10,a=e.helloDelayMs??Ze,u=null,o=null,p=!1,g=-1/0,E=0,f=null,A=null,k=0,X=null,H=!1,b=!1,U=new Set;function R(){let i=e.frame();return f&&i===f.window?i:null}function re(i){try{return i.location.origin===r}catch{return!0}}function w(i,m=R()){if(!(b||!m||!re(m)))try{m.postMessage(n.tag(i),r)}catch{}}function P(i,m){return u?c(i,m,u.video):null}function y(){let i=o!==null;o=null,p=!1,g=-1/0,E++,i&&w({kind:"leave"})}function W(){return o?o.kind==="tap"?{inside:!0,lastTrustedPointerAt:o.at,tooltipShowing:!1,lastTooltipAt:-1/0}:{inside:!0,lastTrustedPointerAt:o.at,tooltipShowing:p,lastTooltipAt:g}:{inside:!1,lastTrustedPointerAt:-1/0,tooltipShowing:p,lastTooltipAt:g}}function ie(){return!o||!u?!1:P(o.clientX,o.clientY)?B(W(),s(),e.limits):(y(),!1)}function L(i){if(!o||!u)return;let m=i??P(o.clientX,o.clientY);m&&w({kind:"pointer",x:m.x,y:m.y,mediaTime:u.mediaTime(),touch:o.kind==="tap"})}function we(i){if(b||i.isTrusted!==!0||!u)return;let m=i.pointerType==="touch"?"tap":"hover",d=i.pointerId,h=P(i.clientX,i.clientY);if(m==="hover"){h?(o={kind:m,pointerId:d,clientX:i.clientX,clientY:i.clientY,at:s()},L(h)):o?.kind==="hover"&&y();return}let T=o?.kind==="tap"&&o.pointerId===d;if(i.type==="pointerdown")h?(o={kind:m,pointerId:d,clientX:i.clientX,clientY:i.clientY,at:s()},L(h)):o?.kind==="tap"&&y();else if(i.type==="pointermove"&&T&&o){if(!h)return y();o.clientX=i.clientX,o.clientY=i.clientY,L(h)}}function xe(i){b||i.isTrusted!==!0||o&&o.pointerId===i.pointerId&&y()}function Ie(i){i.visibilityState==="hidden"&&y()}function _e(i){b||(u?.video??null)===i||(y(),u?.detach(),u=null,i&&(u=ee(i,{presented:d=>w({kind:"presented",mediaTime:d}),discontinuity:d=>{d&&y(),w({kind:"discontinuity",newSource:d})}})),w({kind:"discontinuity",newSource:!0}))}function N(){A!==null&&l.clear(A),A=null}function V(){if(A=null,b||R())return;let i=e.frame();if(i&&!re(i)){let m=s();X??=m,m-X=t){e.onProblem?.({kind:"no-answer"});return}w({kind:"hello",version:1},i),A=l.set(V,a(k)),k++}function Re(){b||(N(),k=0,X=null,V())}function Ce(i,m){let{version:d,instance:h}=i;if(d!==1){f=null,N(),y(),e.onProblem?.({kind:"version",expected:1,received:d});return}if(typeof h!="string"||!ze.test(h)||(N(),f&&f.window===m&&f.instance===h))return;f={window:m,instance:h};let T=o;p=!1,g=-1/0,E++,T&&!(P(T.clientX,T.clientY)&&B(W(),s(),e.limits))&&(o=null),u&&w({kind:"presented",mediaTime:u.mediaTime()}),L()}async function Me(i,m){let d=(x,C=!1)=>w({kind:"capture-failed",requestId:i,reason:x,retry:C});if(H)return d(Te);let h;try{h=ie()}catch(x){return d(String(x))}if(!h)return d(ye);let T={watch:u,source:u?.source,handshake:f,resets:E};H=!0;let S;try{S=await Z(u,m,e.grab)}catch(x){S={kind:"failed",reason:String(x),retry:!1}}finally{H=!1}let q=!b&&f===T.handshake&&R()!==null,F=u===T.watch&&u?.source===T.source&&E===T.resets;if(!q||!F){S.kind==="frame"&&S.bitmap.close(),q&&d(Se);return}if(S.kind==="failed")return d(S.reason,S.retry);Ae(R(),n.tag({kind:"frame",requestId:i,bitmap:S.bitmap,mediaTime:S.mediaTime}),r,S.bitmap)}function ke(i){if(b)return!1;let m=e.frame(),d=ve(i,{frame:m,origin:r,protocol:n});if(!d||!m)return!1;if(d.kind==="ready")return Ce(d,m),!0;if(!R())return!0;switch(d.kind){case"status":e.onStatus?.(d);break;case"shown":{o&&((p||d.card)&&(g=s()),p=d.card!==null),e.onShown?.(d.card);break}case"capture":Me(d.requestId,d.analysisWidth);break}return!0}function Pe(i,m){let d={capture:!0,passive:!0},h=v=>we(v),F=[[i,"pointermove",h],[i,"pointerdown",h],[i,"pointerup",h],[i,"pointercancel",v=>xe(v)],[i,"pointerleave",v=>{let _=v;b||_.isTrusted!==!0||_.target!==m.documentElement||_.pointerType==="touch"||y()}],[i,"visibilitychange",()=>Ie(m)]];for(let[v,_,Y]of F)v.addEventListener(_,Y,d);let x=!0,C=()=>{if(x){x=!1,U.delete(C);for(let[v,_,Y]of F)v.removeEventListener(_,Y,d)}};return U.add(C),C}function Le(){if(!b){y(),b=!0,N();for(let i of[...U])i();u?.detach(),u=null,f=null}}return{attach:_e,listen:Pe,connect:Re,onMessage:ke,get watch(){return u},get connected(){return R()!==null},gateState:W,captureAllowedNow:ie,stop:Le}}export{de as BLANK_PICTURE,Te as CAPTURE_PENDING,$ as MAX_FRAME_SIDE,Ne as MAX_OTHER_BOXES,Fe as MAX_REASON_LENGTH,be as MAX_REST_MS,Oe as MAX_TEXT_LENGTH,ye as NOT_POINTING,me as NO_CONTEXT,J as NO_PICTURE,Se as PICTURE_CHANGED,te as POINTER_GRACE_MS,M as PROTOCOL_VERSION,ne as READING_LINGER_MS,fe as TOO_LARGE,pe as UNREADABLE,ve as acceptComputeOut,K as analysisSize,ge as blankFrame,B as captureAllowed,We as computeProtocol,et as createCaptureControl,D as createSourceCanvas,z as fractionIn,Ee as fromExtensionFrame,Z as grabFrame,Q as isFrozen,G as mediaTimeOf,j as pictureBox,Ae as postWithTransfer,ee as watchVideo}; diff --git a/vendor/cardvision-browser/types/cadence.d.ts b/vendor/cardvision-browser/types/cadence.d.ts index 63ce47e..8c18c55 100644 --- a/vendor/cardvision-browser/types/cadence.d.ts +++ b/vendor/cardvision-browser/types/cadence.d.ts @@ -1,5 +1,7 @@ import type { ExecutionProvider } from "./providers.js"; -export type AnalysisMode = "pointer" | "always-on"; +export type AnalysisMode = "pointer" | "always-on" | "layout"; +/** The modes a session runs. */ +export type SessionMode = Exclude; /** The slow cadence on WebGPU, and before the execution provider is known. */ export declare const DEFAULT_ANALYZE_INTERVAL_MS = 1000; /** The slow cadence on WebAssembly. */ @@ -12,7 +14,8 @@ export declare const ACTIVE_WASM_ANALYZE_INTERVAL_MS = 1000; export declare function analyzeIntervalMs(ep: ExecutionProvider | null): number; /** The fast interval for the recognizer's execution provider (null: not known yet). */ export declare function activeAnalyzeIntervalMs(ep: ExecutionProvider | null): number; -/** The interval a scheduler is armed with for an execution provider in an analysis mode. */ +/** The interval a scheduler is armed with for an execution provider in an analysis mode. In + * "layout" mode this is the pointer's fast cadence; the probe interval is the product's. */ export declare function cadenceMs(ep: ExecutionProvider | null, mode: AnalysisMode): number; /** Whether this device runs the slower cadences. */ export declare function isSlowDevice(ep: ExecutionProvider | null): boolean; diff --git a/vendor/cardvision-browser/types/compute-session.d.ts b/vendor/cardvision-browser/types/compute-session.d.ts index 8989baf..b5e5900 100644 --- a/vendor/cardvision-browser/types/compute-session.d.ts +++ b/vendor/cardvision-browser/types/compute-session.d.ts @@ -1,4 +1,4 @@ -export { type AnalysisMode, cadenceMs } from "./cadence.js"; +export { type AnalysisMode, cadenceMs, type SessionMode } from "./cadence.js"; export { type AnalysisState, FrameScheduler, HOVER_MAX_AGE_MS, replyRejection } from "./frame-scheduler.js"; export { hoverAt } from "./hover-at.js"; export { type ComputeIn, type ComputeOut, type ComputeProtocol, computeProtocol, type FractionBox, MAX_FRAME_SIDE, MAX_OTHER_BOXES, MAX_REASON_LENGTH, MAX_TEXT_LENGTH, type ShownCard, type Tagged, } from "./compute-messages.js"; diff --git a/vendor/cardvision-browser/types/detector/detector.d.ts b/vendor/cardvision-browser/types/detector/detector.d.ts index 28497c8..c9ab664 100644 --- a/vendor/cardvision-browser/types/detector/detector.d.ts +++ b/vendor/cardvision-browser/types/detector/detector.d.ts @@ -1,4 +1,5 @@ import type { Candidate } from "../boxes.js"; +import { type SessionSetup } from "../ort.js"; import type { ExecutionProvider } from "../providers.js"; import { type DecodeSettings } from "./decode.js"; import { type LetterboxSettings } from "./letterbox.js"; @@ -38,6 +39,8 @@ export declare class Detector implements FrameDetector { /** The execution provider that initialized. */ readonly ep: ExecutionProvider; private readonly settings; + /** How long the session took to create and to run once, on `ep`. */ + readonly setup: SessionSetup; lastTimings: { preprocessMs: number; inferMs: number; diff --git a/vendor/cardvision-browser/types/embedder.d.ts b/vendor/cardvision-browser/types/embedder.d.ts index 1139358..c3ce805 100644 --- a/vendor/cardvision-browser/types/embedder.d.ts +++ b/vendor/cardvision-browser/types/embedder.d.ts @@ -1,3 +1,4 @@ +import { type SessionSetup } from "./ort.js"; import type { ExecutionProvider } from "./providers.js"; /** * Most crops one model run takes. A frame can ask for every proposal at once (up to the @@ -30,6 +31,8 @@ export declare class Embedder implements BatchEmbedder { /** The execution provider that initialized. */ readonly ep: ExecutionProvider; private readonly settings; + /** How long the session took to create and to run once, on `ep`. */ + readonly setup: SessionSetup; private constructor(); /** * Create a session on the first of `providers` that initializes and returns embeddings of diff --git a/vendor/cardvision-browser/types/frame-scheduler.d.ts b/vendor/cardvision-browser/types/frame-scheduler.d.ts index 5f05e70..0e122ff 100644 --- a/vendor/cardvision-browser/types/frame-scheduler.d.ts +++ b/vendor/cardvision-browser/types/frame-scheduler.d.ts @@ -23,6 +23,11 @@ export declare function withinMaxAge(resultSec: number, nowSec: number, maxAgeMs export declare const HOVER_TRIGGER_MIN_GAP_MS = 250; /** An analysis with no reply after this long is given up on, so that the next frame can go. */ export declare const ANALYSIS_WATCHDOG_MS = 5000; +/** The watchdog of the looks before a worker's first analysis, once its release has loaded: the + * first analysis warms the models up, and on a slow machine it can take several times longer + * than the ones after it. Given up on at 5 s, it was dropped when it came back and looked again, + * so the first card was late or never came. */ +export declare const FIRST_ANALYSIS_WATCHDOG_MS = 15000; /** After a look comes back with the picture not analyzed, the same picture is due again after * these delays, one per unanswered look in a row, then the last one each time. */ export declare const LOOK_RETRY_MS: readonly number[]; @@ -59,7 +64,7 @@ export declare function replyRejection(envelope: FrameEnvelope | undefined, live /** Does a rejected reply still finish the request in flight? */ export declare function rejectionAnswersInFlight(reason: ReplyRejection): boolean; /** Which analysis policy applies right now. */ -export type AnalysisState = "always-on" | "fast" | "reading" | "idle"; +export type AnalysisState = "always-on" | "fast" | "reading" | "idle" | "probe"; export interface StartedInfo { /** Time of the analyzed picture in the video, in seconds: what staleness is measured from. */ videoTimeSec?: number; @@ -71,6 +76,7 @@ export declare class FrameScheduler { private readonly confirmFrames; private mode; private readingIntervalMs; + private probeIntervalMs; private pending; private repeatsLeft; private lastStartedAt; @@ -96,14 +102,22 @@ export declare class FrameScheduler { private lastUnansweredCapture; /** A look's capture was refused since the picture was last answered, received or replaced. */ private refused; + /** Layout mode: the host also wants pointer-driven analysis. */ + private pointerAnalysis; /** * @param intervalMs The cadence of the mode: the fast cadence in pointer mode, the slow one * in always-on mode (cadence.ts `cadenceMs`). * @param confirmFrames How many times a still picture is analyzed in all. * @param mode Defaults to "always-on": the cadence alone decides. * @param readingIntervalMs The slow cadence of the "reading" state. Defaults to `intervalMs`. + * @param probeIntervalMs Layout mode: the probe interval (the product's). Required there. */ - constructor(intervalMs: number, confirmFrames?: number, mode?: AnalysisMode, readingIntervalMs?: number); + constructor(intervalMs: number, confirmFrames?: number, mode?: AnalysisMode, readingIntervalMs?: number, probeIntervalMs?: number | null); + private checkProbe; + setProbeIntervalMs(ms: number): void; + /** Layout mode: does the host also want pointer-driven analysis (while its display shows the + * recognizer's own cards)? */ + setPointerAnalysis(on: boolean): void; setIntervalMs(ms: number): void; setReadingIntervalMs(ms: number): void; /** Is a tooltip showing at the resting pointer? Report it before each evaluation and after @@ -120,8 +134,8 @@ export declare class FrameScheduler { requestLook(): void; /** * A look came back with the picture not analyzed: the picture could not be captured, the - * worker was still busy with an earlier picture, or no reply came within - * `ANALYSIS_WATCHDOG_MS`. The still picture's other repeats are dropped, and the same picture + * worker was still busy with an earlier picture, or no reply came within the watchdog's time + * (`ANALYSIS_WATCHDOG_MS`, or `FIRST_ANALYSIS_WATCHDOG_MS` before the first analysis). The still picture's other repeats are dropped, and the same picture * counts as newly presented again after a back-off, so that a paused video is looked at again * (with its repeats). This is not `requestLook`, which would also make the picture stale and * let every pointer move look again at once, past the back-off. @@ -171,6 +185,7 @@ export declare class FrameScheduler { /** Is the grace period of the last pointer activity still running? */ pointerActive(now: number): boolean; analysisState(now: number): AnalysisState; + private pointerState; /** May anything be analyzed right now? */ analysisActive(now: number): boolean; /** The cadence in effect: the slow reading cadence while reading, else the mode's cadence. */ diff --git a/vendor/cardvision-browser/types/hover-geometry.d.ts b/vendor/cardvision-browser/types/hover-geometry.d.ts index f212109..a20eef3 100644 --- a/vendor/cardvision-browser/types/hover-geometry.d.ts +++ b/vendor/cardvision-browser/types/hover-geometry.d.ts @@ -9,8 +9,10 @@ export interface NormalizedBounds { * One card of a layer, in fractions of the frame. * * `bounds` is the card's axis-aligned box. `localWidth` and `localHeight` are the card's own - * size before any turn, and `rotation` its tilt in degrees, in (-45, 45]. The detector's boxes - * are axis-aligned, so for them the tilt is 0 and the local size equals the box's size. + * size before any turn, and `rotation` its tilt in degrees, in (-45, 45], positive clockwise on + * screen. The detector's boxes are axis-aligned, so for them the tilt is 0 and the local size + * equals the box's size; with rotated outlines a named card takes its outline's (tilt.ts + * `outlineRect`), and `bounds` stays the box. * `cardId` and `name` are set only for a card that is named; a card that was detected and not * named carries neither, so nothing can be shown for it by mistake. */ diff --git a/vendor/cardvision-browser/types/hover-layer.d.ts b/vendor/cardvision-browser/types/hover-layer.d.ts index 66c002d..de55641 100644 --- a/vendor/cardvision-browser/types/hover-layer.d.ts +++ b/vendor/cardvision-browser/types/hover-layer.d.ts @@ -18,6 +18,8 @@ export interface HoverEntry { proposal: number; aabb: AABB; rect: Rect; + /** The card's outline when rotated outlines are on (`ScoredProposal.outline`). */ + outline?: Rect; /** The card id to show, or null: the box takes part and shows nothing. */ identity: string | null; /** The proposal's score. */ @@ -26,7 +28,7 @@ export interface HoverEntry { zIndex: number; } type HoverFrame = Pick & { - proposals: ReadonlyArray>; + proposals: ReadonlyArray>; }; /** * The selection's members as indices into `frame.proposals`, bottom of the stack first (entry diff --git a/vendor/cardvision-browser/types/index.d.ts b/vendor/cardvision-browser/types/index.d.ts index ad7faba..807f8f7 100644 --- a/vendor/cardvision-browser/types/index.d.ts +++ b/vendor/cardvision-browser/types/index.d.ts @@ -1,9 +1,10 @@ -export { analyzeFrame, type LoadedRelease, type LoadOptions, type LoadProgress, loadRelease, ReleaseError } from "./load.js"; +export { analyzeFrame, type LoadedRelease, type LoadOptions, type LoadProgress, type LoadTimings, loadRelease, type ModelLoadTimings, ReleaseError } from "./load.js"; +export type { SessionSetup } from "./ort.js"; export { type ClientBox, createSession, type Session, type SessionOptions, type SessionStatus, type Shown } from "./session.js"; -export { type BoxResult, type FaceResult, type FrameAnalysis, hoverAt, hoverCardsOf, Pipeline, type PipelineOptions } from "./pipeline.js"; +export { type AnalyzeOptions, type BoxResult, type FaceResult, type FrameAnalysis, hoverAt, hoverCardsOf, Pipeline, type PipelineOptions } from "./pipeline.js"; export { type AABB, type Candidate, iou, nonMaxSuppression, type Rect } from "./boxes.js"; export { type EmbeddingBank, parseBank } from "./bank.js"; -export { type AnalysisMode, cadenceMs } from "./cadence.js"; +export { type AnalysisMode, cadenceMs, type SessionMode } from "./cadence.js"; export { analysisSize, captureFrame, type FrameSource, MAX_ANALYSIS_WIDTH, MIN_ANALYSIS_WIDTH } from "./capture.js"; export { type DecodeSettings, decodeDetections, DEFAULT_DECODE, validBox } from "./detector/decode.js"; export { Detector, type DetectorSettings, type Frame, type FrameDetector } from "./detector/detector.js"; @@ -12,11 +13,17 @@ export { type BatchEmbedder, Embedder, type EmbedderSettings } from "./embedder. export { type AnalysisState, FrameScheduler, HOVER_MAX_AGE_MS, replyRejection } from "./frame-scheduler.js"; export { computeCardQuad, type LayerCard, type NormalizedBounds, pointInConvexQuad, resolveHoveredCard } from "./hover-geometry.js"; export { HOVER_SELECTIONS, type HoverOutcome, type HoverSelection, hoverOrder, resolveHover, selectHoverLayer } from "./hover-layer.js"; +export { type Box, compatible, correspondences, type Correspondence, fitFrame, fitFrameDetail, type FitParams, type FrameFitOutcome, iou as layoutIoU, type Layout, type LayoutCard, LAYOUT_FIT_DEFAULTS, layoutExtent, type Observation, placementDistance, project, type Region, toRegion, type Transform, } from "./layout-fit.js"; +export { classifyAccept, classifyHitbox, coverage, exitLatency, fitError, type Hitbox, type TruthCard } from "./layout-metrics.js"; +export { clockUsable, FIDUCIAL_POINTS, type LayoutCandidate, type LayoutRecording, latestOffered, offeredCandidates, type ProbeScheduler, readLayoutRecording, recordingSource, type Replay, replayRecording, sceneAt, timedFrames, truthCandidate, truthUsable, } from "./layout-recording.js"; +export { type Display, type FrameInput, type LayoutConfig, layoutConfigError, LayoutRegistration, type LayoutSource, RATIO_POOL_SIZE, type State as LayoutState, suppressedInstances, type Verdict, } from "./layout-state.js"; +export { MismatchMonitor, MONITOR_DEFAULTS } from "./mismatch-monitor.js"; export { type IdentityDisplay, type IdentityGroups, IdentityMap, type IdentityRule, parseIdentityGroups } from "./identity.js"; -export type { FrameEnvelope, FrameResult, ReleaseProgress, ReleaseStatus, WorkerReply, WorkerRequest } from "./messages.js"; +export type { FrameEnvelope, FrameResult, ReleaseProgress, ReleaseStatus, TiltedStatus, WorkerReply, WorkerRequest } from "./messages.js"; export { boxAverageRgb, PREPROCESSING, type PreprocessingContract, rotateTensor90, toContractTensor } from "./preprocess.js"; export { EXECUTION_PROVIDERS, type ExecutionProvider } from "./providers.js"; -export { chooseLook, cropRgba, lookAccepted, recognizeFrame, type ScoredFrame, type ScoredProposal } from "./recognition.js"; +export { chooseLook, cropRgba, lookAccepted, recognizeFrame, type ScoredFrame, type ScoredProposal, type TiltedEvidence, type TiltedRun, tiltedOrder } from "./recognition.js"; +export { DEFAULT_TILTED_RETRY, estimateTilt, OUTLINE_MIN_TILT, outlineFromTilt, outlineRect, rectFromAabb, sampleRect, tiltedRects, type TiltedRetrySettings, } from "./tilt.js"; export { type AcceptRule, DEFAULT_ACCEPT, EmbeddingRecognizer, type ScoredMatch } from "./recognizer.js"; export { type CatalogCard, loadReleaseData, parseRelease, type ReleaseData, type VisionRelease } from "./release.js"; export { resizeBilinearRgb } from "./resize.js"; diff --git a/vendor/cardvision-browser/types/layout-fit.d.ts b/vendor/cardvision-browser/types/layout-fit.d.ts new file mode 100644 index 0000000..565d416 --- /dev/null +++ b/vendor/cardvision-browser/types/layout-fit.d.ts @@ -0,0 +1,201 @@ +/** Axis-aligned box; x, y top-left. Frame boxes are fractions of the frame; layout boxes are + * fractions of the layout's unit square. */ +export interface Box { + x: number; + y: number; + w: number; + h: number; +} +export interface LayoutCard { + /** Stable id of this card instance within its layout. */ + instance: string; + /** The identity the recognizer also uses; absent for an unknown or hidden card. */ + identity?: string; + bounds: Box; + /** Informational only; the fit does not use it. */ + rotated?: boolean; +} +export interface Layout { + key: string; + cards: LayoutCard[]; + /** A region of the layout painted over in the picture (a dialog). Cards whose center lies in + * it are never matched. */ + blocking?: Box; + /** The layout's own width / height in pixels. With the frame's aspect, it turns on the aspect + * prior (see the file comment); without it, a placement scales each axis freely. */ + aspect?: number; +} +/** A card the recognizer accepted, in frame units. */ +export interface Observation { + identity: string; + bounds: Box; +} +/** Per-axis scale and offset from layout units to frame units: v = s·a + t. */ +export interface Transform { + sx: number; + sy: number; + tx: number; + ty: number; +} +/** The same map as a region: the layout's unit square placed in the frame. */ +export interface Region { + x: number; + y: number; + width: number; + height: number; +} +export type InsufficientReason = "too-few" | "share" | "span" | "degenerate" | "zero-inliers" | "aspect"; +export type FrameFitOutcome = { + kind: "verified"; + transform: Transform; + inliers: string[]; + n: number; + minIoU: number; + /** The inliers' layout-center range on each axis, in layout units. */ + span: [number, number]; + /** With the aspect prior: the per-axis refit's sy / sx over the inliers, divided by the + * aspect-implied ratio (1 for a picture in proportion). */ + boardRatio?: number; +} | { + kind: "ambiguous"; + transform: Transform; + inliers: string[]; + runnerUp: string[]; + n: number; +} | { + kind: "insufficient"; + reason: InsufficientReason; + n: number; + inliers: number; +} | { + kind: "no-layout"; +}; +/** The fit's fixed parameters. The toolkit's values are `LAYOUT_FIT_DEFAULTS`; `fitMinIoU` and + * `minSpan` come from the product's configuration. */ +export interface FitParams { + /** A correspondence supports a placement when its projected layout box overlaps its frame + * box at least this much. */ + fitMinIoU: number; + /** The inliers' layout centers must spread over at least this share of the layout's extent, + * on each axis. */ + minSpan: number; + /** At most this many correspondences are scored (the largest frame boxes). */ + maxCorrespondences: number; + /** At least this many inliers. */ + fitMinInliers: number; + /** At least this share of the correspondences are inliers, as [numerator, denominator]: the + * test is `denominator · inliers ≥ numerator · n`, in integers. */ + inlierShare: readonly [number, number]; + /** A second placement with this many supporters outside the first makes the frame ambiguous. */ + ambiguityMin: number; + /** Two placements are compatible when they put every point of the layout's extent within + * this distance of each other, as a fraction of the frame on each axis. */ + compatTol: number; + /** With the aspect prior: a hypothesis whose sy / sx differs from the aspect-implied ratio by + * more than this share of it is set aside. */ + scaleRatioTol: number; +} +/** The toolkit's defaults for the parameters a product does not tune. */ +export declare const LAYOUT_FIT_DEFAULTS: { + /** Bounds the n² scoring. */ + readonly maxCorrespondences: 40; + /** Two names fit any placement exactly; three is the least that can outvote a wrong one. */ + readonly fitMinInliers: 3; + readonly inlierShare: readonly [2, 3]; + readonly ambiguityMin: 2; + /** 0.01 of the frame per axis: 19.2 px across and 10.8 px down a 1920 × 1080 frame. */ + readonly compatTol: 0.01; + /** 3 % of the aspect-implied ratio. */ + readonly scaleRatioTol: 0.03; +}; +/** The 5 × 5 grid over a box (corners included) on which placements are compared. */ +export declare const GRID_STEPS = 5; +/** Intersection over union; 0 when the union has no area. */ +export declare function iou(a: Box, b: Box): number; +/** A layout box placed in the frame. */ +export declare function project(t: Transform, b: Box): Box; +/** A layout point placed in the frame. */ +export declare function projectPoint(t: Transform, p: { + x: number; + y: number; +}): { + x: number; + y: number; +}; +/** The layout's unit square in the frame. */ +export declare function toRegion(t: Transform): Region; +/** The bounding box of every card of a layout; null for a layout without cards. */ +export declare function layoutExtent(layout: Layout): Box | null; +/** The points of the 5 × 5 grid over a box, row by row. */ +export declare function gridPoints(box: Box): Array<{ + x: number; + y: number; +}>; +/** The largest distance, per axis in frame fractions, between two placements over the grid. */ +export declare function placementDistance(t1: Transform, t2: Transform, extent: Box): { + x: number; + y: number; +}; +/** Do two placements agree within `compatTol` of the frame, on each axis, over the extent? */ +export declare function compatible(t1: Transform, t2: Transform, extent: Box, compatTol?: number): boolean; +/** Is a point inside a box (edges included)? */ +export declare function containsPoint(b: Box, p: { + x: number; + y: number; +}): boolean; +export interface Correspondence { + identity: string; + /** The observation's box, frame units. */ + v: Box; + /** The layout card's box, layout units. */ + r: Box; + /** The layout card's instance. */ + instance: string; +} +/** Is this layout card one the fit may match: it has an identity and is not under the + * layout's blocking box? */ +export declare function matchable(layout: Layout, card: LayoutCard): boolean; +/** + * The frame's correspondences with the layout: identities occurring exactly once among the + * observations and exactly once among the layout's matchable cards. At most `maxCorrespondences` + * are kept, the largest frame boxes first (ties by identity). The result is in identity order + * (code units), which is the index order the fit's tie-breaks use. + */ +export declare function correspondences(observations: readonly Observation[], layout: Layout, maxCorrespondences?: number): Correspondence[]; +/** The placement that maps one correspondence's layout box exactly onto its frame box; null + * when it is not a valid placement. */ +export declare function hypothesis(c: Correspondence): Transform | null; +/** Least squares of v = s·a + t; null when the points do not determine a positive scale. */ +export declare function fitAxis(points: ReadonlyArray): { + s: number; + t: number; +} | null; +/** The per-axis refit over a set of correspondences: both edges of each box on each axis. */ +export declare function refit(cs: readonly Correspondence[]): Transform | null; +/** + * The refit under the aspect prior: one scale, sy = ratio · sx, and a translation per axis, by + * least squares over both edges of each box on each axis, in pixels (x residuals weighted by + * the frame's aspect, so that a pixel counts the same across and down). Null when the points + * do not determine a positive scale. + */ +export declare function refitAspect(cs: readonly Correspondence[], ratio: number, frameAspect: number): Transform | null; +/** Everything the fit computed, for diagnostics and tests. */ +export interface FitDetail { + outcome: FrameFitOutcome; + correspondences: Correspondence[]; + /** Supporters of each hypothesis (indices into `correspondences`); empty for an invalid one. */ + support: number[][]; + /** The winning hypothesis's index, or -1. */ + winner: number; + /** Hypotheses the aspect prior set aside (indices into `correspondences`). */ + aspectRejected: number[]; +} +/** + * Fit one frame. See the file comment for the steps. + * @param frameAspect The analyzed frame's width / height in pixels; with the layout's `aspect`, + * it turns on the aspect prior. + * @param ratioScale With the aspect prior: the refit uses the aspect-implied ratio times this + * (1 by default). A value that is not a positive number makes no placement. + */ +export declare function fitFrame(observations: readonly Observation[], layout: Layout | null, params: FitParams, frameAspect?: number, ratioScale?: number): FrameFitOutcome; +export declare function fitFrameDetail(observations: readonly Observation[], layout: Layout | null, params: FitParams, frameAspect?: number, ratioScale?: number): FitDetail; diff --git a/vendor/cardvision-browser/types/layout-metrics.d.ts b/vendor/cardvision-browser/types/layout-metrics.d.ts new file mode 100644 index 0000000..624a938 --- /dev/null +++ b/vendor/cardvision-browser/types/layout-metrics.d.ts @@ -0,0 +1,51 @@ +import { type Box, type Transform } from "./layout-fit.js"; +export { compatible } from "./layout-fit.js"; +/** A displayed placement within this many pixels of the truth is correct. */ +export declare const CORRECT_PX = 8; +/** Beyond this many pixels a displayed placement is false. */ +export declare const FALSE_PX = 20; +/** A drawn box lies on a true card when they overlap at least this much. */ +export declare const HITBOX_IOU = 0.3; +/** The largest distance, in pixels of a `width` × `height` frame, between a placement and the + * true one over the 5 × 5 grid of `extent` (layout units). */ +export declare function fitError(t: Transform, truth: Transform, extent: Box, width: number, height: number): number; +export type AcceptClass = "correct" | "loose" | "false" | "none"; +/** How good a displayed placement is; "none" when nothing was displayed. */ +export declare function classifyAccept(errorPx: number, displayed: boolean): AcceptClass; +/** A card as it truly is in the picture, frame units. */ +export interface TruthCard { + box: Box; + identity?: string; + faceUp: boolean; +} +/** A card box the product drew, frame units. */ +export interface Hitbox { + box: Box; + identity?: string; +} +export type HitboxClass = "blocked" | "wrong-identity" | "back-hidden" | "correct" | "empty"; +/** + * What a drawn box lies on: "blocked" when its center is outside the visible `mask` or inside a + * blocker; otherwise by the true card it overlaps most (at `HITBOX_IOU` or more): "back-hidden" + * for a face-down card or one without identity, "wrong-identity" for another identity, + * "correct" for the same one; "empty" when no true card overlaps it enough. + */ +export declare function classifyHitbox(h: Hitbox, truthCards: readonly TruthCard[], mask: Box, blockers?: readonly Box[]): HitboxClass; +/** + * The share of visible, face-up, identified true cards whose center lies inside a drawn box of + * the same identity; null when there are none. A card is visible when its center is inside the + * mask and outside every blocker. + */ +export declare function coverage(truthCards: readonly TruthCard[], hitboxes: readonly Hitbox[], mask: Box, blockers?: readonly Box[]): number | null; +/** One entry of a verdict trace: what was displayed from `atMs` on. */ +export interface TraceEntry { + atMs: number; + display: string; +} +/** + * How long a fitted display outlives each scene boundary: the time from the boundary to the + * first trace entry at or after it that is not "fitted" (0 when the display was not fitted at + * the boundary); null when the display stays fitted to the end of the trace. The trace is in + * time order. + */ +export declare function exitLatency(trace: readonly TraceEntry[], sceneBoundaries: readonly number[]): Array; diff --git a/vendor/cardvision-browser/types/layout-recording.d.ts b/vendor/cardvision-browser/types/layout-recording.d.ts new file mode 100644 index 0000000..44780cd --- /dev/null +++ b/vendor/cardvision-browser/types/layout-recording.d.ts @@ -0,0 +1,149 @@ +import type { Box, Layout, Observation, Region, Transform } from "./layout-fit.js"; +import { type LayoutConfig, type LayoutSource, type Verdict } from "./layout-state.js"; +export declare const LAYOUT_RECORDING_SCHEMA = "layout-recording/1"; +/** A scene's true placement is unusable when a solve's marker error exceeds this many pixels, + * or two solves disagree by more at a fiducial point. */ +export declare const MAX_TRUTH_RESIDUAL_PX = 2; +/** Where solves are compared: points of the layout's unit square (corners inset by 0.05, and the + * center). */ +export declare const FIDUCIAL_POINTS: ReadonlyArray<{ + x: number; + y: number; +}>; +export declare const SCENE_CLASSES: readonly ["supported-game", "two-region", "negative-no-faces", "negative-gallery", "negative-other-layout", "transition"]; +export type SceneClass = (typeof SCENE_CLASSES)[number]; +export interface SceneTruth { + /** The layout's unit square in the frame (frame fractions); null for a scene with no layout to + * place (a negative scene). */ + region: Region | null; + /** The largest marker error of the solve, in pixels. */ + residualPx: number; + /** The visible captured area, frame fractions. */ + mask: Box; + /** Areas painted over the picture, frame fractions. */ + blockers: Box[]; + /** Repeated solves during the scene, each with the placement it found (null in a negative + * scene). */ + solves: Array<{ + atMs: number; + residualPx: number; + region: Region | null; + }>; +} +export interface Scene { + id: string; + class: SceneClass; + startMs: number; + endMs: number; + truth?: SceneTruth; +} +export type ClockStatus = "ok" | "undecodable" | "missing"; +export interface RecordedFrame { + index: number; + capturedAtMs: number; + image: string; + clock: { + status: ClockStatus; + streamTimeMs?: number; + }; + analysis: { + durationMs: number; + faces: number; + observations: Observation[]; + }; +} +export interface LayoutCandidate { + key: string; + producedAtMs: number; + arrivedAtMs: number; + layout: Layout; +} +export interface LayoutRecording { + schema: typeof LAYOUT_RECORDING_SCHEMA; + frame: { + width: number; + height: number; + }; + cadenceProfiles: Array<{ + name: string; + intervalMs: number; + }>; + scenes: Scene[]; + frames: RecordedFrame[]; + candidates: LayoutCandidate[]; + /** What the reader set aside: a malformed clock or truth, by its path. */ + problems: string[]; +} +/** Read and check a `layout-recording/1` document; throws naming the first bad field that + * replay needs. */ +export declare function readLayoutRecording(json: unknown): LayoutRecording; +/** The candidates a picture captured at `capturedAtMs` may be offered: those that had arrived, + * in arrival order. */ +export declare function offeredCandidates(rec: LayoutRecording, capturedAtMs: number): LayoutCandidate[]; +/** The newest candidate that had arrived by `capturedAtMs`; null for none. */ +export declare function latestOffered(rec: LayoutRecording, capturedAtMs: number): LayoutCandidate | null; +/** The truth state of a frame: the candidate produced nearest before (or at) its decoded stream + * time. Null when its clock was not decoded or nothing was produced by then. */ +export declare function truthCandidate(rec: LayoutRecording, frame: RecordedFrame): LayoutCandidate | null; +/** Can a frame's clock time it: decoded, with a finite stream time? */ +export declare function clockUsable(clock: RecordedFrame["clock"] | null | undefined): boolean; +/** Frames usable for time-dependent measurements (clock decoded), and how many were left out. */ +export declare function timedFrames(rec: LayoutRecording): { + frames: RecordedFrame[]; + excluded: number; +}; +/** The scene a time falls in (start inclusive, end exclusive); null when none. */ +export declare function sceneAt(rec: LayoutRecording, atMs: number): Scene | null; +/** + * Is a scene's true placement usable? It must be present, its residual and every solve's within + * `MAX_TRUTH_RESIDUAL_PX`, and the region and every solve must agree: no two of them may put a + * fiducial point more than that many pixels apart (on either axis, in a frame of + * `frame.width` × `frame.height`). Null for a scene with no placement to judge + * (`region: null`, a negative scene); false for a scene without a truth. + */ +export declare function truthUsable(scene: Scene, frame: { + width: number; + height: number; +}): boolean | null; +/** A region as the placement it stands for. */ +export declare function regionToTransform(r: Region): Transform; +/** A recording as a layout source: the newest arrived candidate; the window is that candidate's + * identities. */ +export declare function recordingSource(rec: LayoutRecording): LayoutSource; +/** The scheduler policy the driver replays through: the toolkit's `FrameScheduler` in "layout" + * mode fits, and so does any product scheduler with these three calls. */ +export interface ProbeScheduler { + framePresented(): void; + due(nowMs: number): boolean; + started(nowMs: number): void; +} +export interface ReplayAnalysis { + frameIndex: number; + startedAtMs: number; + endedAtMs: number; + /** It ended after the next analysis was due. */ + late: boolean; +} +export interface ReplayStep { + frameIndex: number; + atMs: number; + result: Verdict; +} +export interface Replay { + /** The verdict at every frame, after any analysis that ended by then. */ + steps: ReplayStep[]; + analyses: ReplayAnalysis[]; +} +export interface ReplayOptions { + /** The product's configuration; its `intervalMs` is replaced by the profile's. */ + config: LayoutConfig; + /** The cadence profile's name in the recording. */ + profile: string; + /** Makes the scheduler for an interval, e.g. a `FrameScheduler` in "layout" mode. */ + scheduler: (intervalMs: number) => ProbeScheduler; + /** Where layouts come from; the recording's own candidates by default. */ + source?: LayoutSource; + exclude?: (identity: string) => boolean; +} +/** Replay a recording through a scheduler and the state machine (one generation). */ +export declare function replayRecording(rec: LayoutRecording, options: ReplayOptions): Replay; diff --git a/vendor/cardvision-browser/types/layout-state.d.ts b/vendor/cardvision-browser/types/layout-state.d.ts new file mode 100644 index 0000000..21f5321 --- /dev/null +++ b/vendor/cardvision-browser/types/layout-state.d.ts @@ -0,0 +1,149 @@ +import { type FrameFitOutcome, type Layout, type Observation, type Region, type Transform } from "./layout-fit.js"; +/** The product's tuned values. None has a default: a product cannot run on an unexamined one. */ +export interface LayoutConfig { + /** Fit: a correspondence supports a placement at this IoU or more (0.5 to 1). */ + fitMinIoU: number; + /** Fit: the inliers spread over at least this share of the layout's extent per axis (0 to 1). */ + minSpan: number; + /** Compatible frame-verified fits in a row that confirm a placement (1 to 3). */ + confirmFits: number; + /** Two fits in a run are at most this far apart; at least 3 × `intervalMs`. */ + confirmWindowMs: number; + /** A confirmed placement survives analyses that do not confirm for this long; at least + * `freshnessMs`. */ + holdMs: number; + /** A fitted page is withdrawn when no analysis arrived for this long; at least 2 × `intervalMs`. */ + freshnessMs: number; + /** The host's probe interval (see `AnalysisMode` "layout"). */ + intervalMs: number; + /** Optional: a second, incompatible placement with this many supporters of its own makes a + * frame ambiguous (2 or 3; 2 when absent). */ + ambiguityMin?: number; + /** Optional: with the aspect prior (a layout's `aspect` and an analysis's `frameAspect`), a + * placement whose sy / sx is off the layout's proportions by more than this share is set + * aside (above 0, at most 0.25; 0.03 when absent). */ + scaleRatioTol?: number; + /** A layout card whose placed box overlaps a recognized card of another identity at this IoU + * or more is not drawn (0 to 1). */ + suppressionIoU: number; +} +/** Why a configuration is refused, or null when it is valid. */ +export declare function layoutConfigError(config: unknown): string | null; +export interface FrameInput { + /** The product's generation for this analysis; a different one than the current is dropped. */ + generation: number; + /** When the analyzed picture was captured, on the host clock that stops while paused. */ + capturedAtMs: number; + /** Card faces the detector found in the picture, named or not. */ + faces: number; + /** The recognizer's accepted cards, frame units. */ + observations: Observation[]; + /** The candidate layout in force at `capturedAtMs`; null when there is none. */ + layout: Layout | null; + /** The analyzed picture's width / height in pixels. With the layout's `aspect`, it turns on + * the aspect prior; absent, a placement scales each axis freely. */ + frameAspect?: number; + /** Identities the product's source offers around this instant (the monitor; empty: not + * counted). */ + windowIdentities: ReadonlySet; +} +/** Where a layout comes from. A product implements it over its own feed; the recording reader + * (layout-recording.ts) implements it over a recording. */ +export interface LayoutSource { + /** The candidate layout in force for a picture captured at `capturedAtMs`; null for none. */ + layoutAt(capturedAtMs: number): Layout | null; + /** The identities the source offers around that instant. */ + windowIdentitiesAt(capturedAtMs: number): ReadonlySet; +} +export type State = "off" | "unverified" | "verified" | "held" | "contradicted" | "mismatch" | "no-faces"; +export type Display = "fitted" | "today" | "vision" | "hidden"; +/** Which row of the precedence table produced a verdict; 0: a timer with nothing to withdraw. */ +export type Row = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12; +export interface Verdict { + display: Display; + /** Present exactly when `display` is "fitted". */ + region?: Region; + /** Layout instances not to draw; empty unless fitted. */ + suppressed: string[]; + state: State; + /** Evidence went against the layout in this generation, and no placement was confirmed since. */ + contradicted: boolean; + /** The last analysis's fit ("no-layout" before the first). */ + frame: FrameFitOutcome; + diagnostics: { + /** Time since the last analysis arrived (0 before the first). */ + elapsedSinceLastAnalysisMs: number; + runLength: number; + monitorShare: number | null; + row: Row; + /** The correction to the layout's proportions in force after this event (1 without the + * aspect prior, or until two verified fits are pooled). */ + ratioScale: number; + }; +} +/** + * The layout instances not to draw through a placement: (a) a correspondence the placement does + * not put on its recognized card (the recognizer saw that card somewhere else), and (b) a card + * whose placed box overlaps a recognized card of another identity at `suppressionIoU` or more. + * In layout order. + */ +export declare function suppressedInstances(layout: Layout, observations: readonly Observation[], t: Transform, fitMinIoU: number, suppressionIoU: number, maxCorrespondences?: number): string[]; +/** Frame-verified fits whose board ratio the correction pools. */ +export declare const RATIO_POOL_SIZE = 15; +export declare class LayoutRegistration { + readonly config: Readonly; + private readonly fitParams; + private readonly monitor; + private generationValue; + private stateValue; + private contradicted; + private verifiedThisGen; + private run; + private confirmed; + private confirmedBasis; + private ratioPool; + /** The correction the kept placements (confirmed, run's latest) were last made with. */ + private appliedScale; + private holdStart; + private lastAnalysisAt; + private zeroStreak; + private lastFrame; + private suppressed; + private current; + /** + * @param config The product's tuned values; checked here (a bad one throws). + * @param options.exclude Identities the monitor leaves out (card types every layout shares). + */ + constructor(config: LayoutConfig, options?: { + exclude?: (identity: string) => boolean; + }); + /** The current generation; null while off. */ + get generation(): number | null; + get state(): State; + /** The verdict after the last event. */ + get verdict(): Verdict; + /** Row 1: the product's mode is no longer eligible. Everything is cleared and every reply + * dropped until `reset`. */ + eligibilityLost(nowMs: number): Verdict; + /** Row 2: a new generation (a navigation, a new source, eligibility regained). */ + reset(generation: number, nowMs: number): Verdict; + /** A timer event: withdraw a placement whose hold or freshness ran out (row 11). */ + tick(nowMs: number): Verdict; + /** An analysis arrived. Null when it belongs to another generation (dropped unprocessed). */ + analyze(input: FrameInput, nowMs: number): Verdict | null; + /** Row 11's condition: a page showing a placement (or hiding the picture) whose hold or + * freshness ran out. */ + private expired; + /** + * Make the kept placements (confirmed, the run's latest) with this correction, from the + * correspondences they were made from. It only moves placements: no state, row, timer or run + * length changes here. + */ + private applyScale; + /** The correction to the layout's proportions the next refit uses. */ + private ratioScale; + private expire; + private clear; + private display; + private build; +} diff --git a/vendor/cardvision-browser/types/load.d.ts b/vendor/cardvision-browser/types/load.d.ts index 75bc1ac..d1332b1 100644 --- a/vendor/cardvision-browser/types/load.d.ts +++ b/vendor/cardvision-browser/types/load.d.ts @@ -36,6 +36,42 @@ export type LoadProgress = { } | { phase: "preparing"; }; +/** How long one model took to become ready, in whole ms. */ +export interface ModelLoadTimings { + /** Creating its session on the execution provider that worked. ONNX Runtime starts a backend + * (and WebGPU its device) on the first session created on it: the recognizer is prepared + * first, so that time is in its `createMs`, or in its `failedProvidersMs` when a provider + * tried before failed. */ + createMs: number; + /** Its first run, a warm-up on a blank input: it pays one-off allocation and, on WebGPU, + * shader compilation, so it can take many times longer than the runs after it. */ + firstRunMs: number; + /** Providers tried before the one that worked, which failed: their sessions, runs and + * release. When ONNX Runtime's backend started on one of them, that time is here. */ + failedProvidersMs: number; + /** All of it: the failed providers, then the create and first run. Each part is rounded on + * its own, so they add up to it to within 1 ms. */ + totalMs: number; +} +/** Where a load's time went, in whole ms, for a product that wants to see why the first card + * is slow on some machine. Measured, never used to decide anything. */ +export interface LoadTimings { + /** The thread's clock when the load began. In a worker it counts from the worker's start, so + * it holds loading the worker's script and ONNX Runtime's module. */ + startedAtMs: number; + /** Fetching and reading the release document, then fetching every file it pins and checking + * their sizes and SHA-256. With `options.files` nothing is fetched (a remote release's files were downloaded + * before, and timed by whoever chose it): only the checks. */ + filesMs: number; + /** Reading the files once checked: decoding the bank, parsing the catalog and the identity + * groups, and checking that they belong together. Plain script: its CPU cost shows here. */ + readMs: number; + recognizer: ModelLoadTimings; + detector: ModelLoadTimings; + /** The whole load, from `startedAtMs`. A little more than its parts: it also holds the short + * steps between them, such as building the pipeline once both models are ready. */ + totalMs: number; +} /** A release that loaded whole: its document, its catalog and a pipeline ready for frames. */ export interface LoadedRelease { release: VisionRelease; @@ -55,6 +91,9 @@ export interface LoadedRelease { bankIds: string[]; identities: Map; }; + /** Where the load's time went. Set by `loadRelease`; optional so that a stand-in made + * elsewhere need not state it. */ + timings?: LoadTimings; /** Free the models. */ dispose(): Promise; } diff --git a/vendor/cardvision-browser/types/messages.d.ts b/vendor/cardvision-browser/types/messages.d.ts index 5e7db50..335a19e 100644 --- a/vendor/cardvision-browser/types/messages.d.ts +++ b/vendor/cardvision-browser/types/messages.d.ts @@ -1,7 +1,9 @@ import type { IdentityRule } from "./identity.js"; +import type { LoadTimings } from "./load.js"; import type { FrameAnalysis } from "./pipeline.js"; import type { ExecutionProvider } from "./providers.js"; import type { AcceptRule } from "./recognizer.js"; +import type { TiltedRetrySettings } from "./tilt.js"; /** * What a frame request was taken from. The worker echoes it unchanged on the reply and never * interprets it; the session uses it to decide whether the reply still describes the picture @@ -57,6 +59,12 @@ export interface ReleaseStatus { max: number; }; ortWeb: string; + /** Where the load's time went, in whole ms (`LoadTimings` in load.ts). Measured, never + * used to decide anything; absent from a worker older than 0.7.0. */ + loadTimings?: LoadTimings; + /** The release's tilted retry settings, or null when it does not turn the retry on; absent + * from a worker older than 0.8.0. */ + tiltedRetry?: TiltedRetrySettings | null; } | null; /** Card id to name and image path (null for a card without art), from the release's catalog; * empty when none loaded. */ @@ -108,6 +116,9 @@ export type WorkerRequest = { /** False turns the geometric watchdog off (default: on). For a scoring run, which measures * the stage on every frame and times it separately; a session that shows cards keeps it. */ geometricWatchdog?: boolean; + /** False turns the tilted retry's watchdog off (default: on), as `geometricWatchdog` does + * for the stage. */ + tiltedWatchdog?: boolean; } | { kind: "frame"; /** The picture, already at analysis size (capture.ts). The worker closes it. */ @@ -115,6 +126,17 @@ export type WorkerRequest = { /** Time of the picture in the video, in seconds. */ videoTimeSec: number; envelope: FrameEnvelope; + /** False skips the release's tilted retry on this frame (default: it runs). */ + tilted?: boolean; + /** A point in the analyzed frame's pixels, such as the pointer: the tilted retry takes the + * faces whose box holds it first. */ + focus?: { + x: number; + y: number; + } | null; + /** True: named cards on this frame follow their outline (pipeline.ts `AnalyzeOptions`). + * Default false. */ + rotatedOutlines?: boolean; }; /** A frame result's `error` when the frame arrived while another was still being analyzed. */ export declare const BUSY_ERROR = "busy"; @@ -157,6 +179,14 @@ export interface ReleaseProgress { phase: "downloading" | "preparing"; bytes: number | null; } +/** The tilted retry was turned off for the rest of the session: it failed during a frame (that + * frame was answered without it), or the watchdog found it over the release's `budgetMs` of a + * frame on two frames in a row. Sent once. */ +export interface TiltedStatus { + kind: "tilted-status"; + state: "off"; + reason: string; +} export type WorkerReply = { kind: "pong"; -} | ReleaseStatus | ReleaseProgress | FrameResult | GeometricStatus; +} | ReleaseStatus | ReleaseProgress | FrameResult | GeometricStatus | TiltedStatus; diff --git a/vendor/cardvision-browser/types/mismatch-monitor.d.ts b/vendor/cardvision-browser/types/mismatch-monitor.d.ts new file mode 100644 index 0000000..1310606 --- /dev/null +++ b/vendor/cardvision-browser/types/mismatch-monitor.d.ts @@ -0,0 +1,52 @@ +export interface MonitorParams { + /** Counted analyses in the window; also the length of the recovery run. */ + window: number; + /** Distinct accepted identities needed before the share means anything. */ + minIdentities: number; + /** Trip when the share falls strictly below this. */ + trip: number; + /** Recover after `window` consecutive counted analyses at or above this. */ + recover: number; +} +/** The toolkit's defaults: 10 counted analyses, 3 identities, trip below 0.4, recover at 0.7. */ +export declare const MONITOR_DEFAULTS: MonitorParams; +export type MonitorState = "unknown" | "in" | "tripped"; +export interface MonitorInput { + /** The identities the product's source offered around this instant; empty: not counted. */ + windowIdentities: ReadonlySet; + /** Identities the recognizer accepted in this analysis. */ + accepted: Iterable; +} +export interface MonitorStep { + state: MonitorState; + /** "trip" or "recover" when this analysis changed the state that way; else null. */ + event: "trip" | "recover" | null; + /** Distinct accepted identities in the window. */ + identities: number; + /** Their in-share, or null when there are none. */ + share: number | null; + /** Whether this analysis was counted. */ + counted: boolean; +} +export declare class MismatchMonitor { + private readonly params; + private readonly exclude; + private window; + private stateValue; + private recoverRun; + constructor(options?: { + exclude?: (identity: string) => boolean; + params?: MonitorParams; + }); + get state(): MonitorState; + get tripped(): boolean; + /** The share over the current window, or null with no identities. */ + get share(): number | null; + observe(input: MonitorInput): MonitorStep; + /** Geometry confirmed the layout: forget the trip and the identities behind it, so that only + * evidence gathered after the confirmation can trip it again. A scene with a persistently low + * share beside a board that keeps verifying can still trip and clear at the window's period. */ + clearTrip(): void; + reset(): void; + private summary; +} diff --git a/vendor/cardvision-browser/types/ort.d.ts b/vendor/cardvision-browser/types/ort.d.ts index 7dd0815..011b493 100644 --- a/vendor/cardvision-browser/types/ort.d.ts +++ b/vendor/cardvision-browser/types/ort.d.ts @@ -13,7 +13,14 @@ export declare function configureOrt(options?: { }): void; /** The reasons collected since the last call, for an error message; the list is emptied. */ export declare function takeSessionFailures(): string[]; -export interface OpenSession { +/** How long a model took to become ready on the provider that worked, in ms: creating its + * session, then its first run (the warm-up check, which also pays one-off allocation and, on + * WebGPU, shader compilation). Providers tried before it and failed are not counted here. */ +export interface SessionSetup { + createMs: number; + firstRunMs: number; +} +export interface OpenSession extends SessionSetup { session: ort.InferenceSession; /** The execution provider that initialized. */ ep: ExecutionProvider; diff --git a/vendor/cardvision-browser/types/pipeline.d.ts b/vendor/cardvision-browser/types/pipeline.d.ts index c08884b..608ed6b 100644 --- a/vendor/cardvision-browser/types/pipeline.d.ts +++ b/vendor/cardvision-browser/types/pipeline.d.ts @@ -7,8 +7,9 @@ import type { LayerCard, StageSize } from "./hover-geometry.js"; import { type HoverSelection } from "./hover-layer.js"; export { hoverAt, hoverCardsOf } from "./hover-at.js"; import { type IdentityDisplay, type IdentityGroups } from "./identity.js"; -import { type Orientation } from "./recognition.js"; +import { type Orientation, type TiltedEvidence } from "./recognition.js"; import { type AcceptRule } from "./recognizer.js"; +import type { TiltedRetrySettings } from "./tilt.js"; /** What the recognizer concluded about one face. */ export interface FaceResult { /** The chosen look's best bank row: its card id and name, whether or not it was accepted. */ @@ -36,6 +37,10 @@ export interface FaceResult { /** With a geometric stage that ran on this face: what it found and what the embedding alone * answered (Python: `GeometricOutcome.to_json`). The fields above are the fused look's. */ geometric?: GeometricEvidence; + /** With the tilted retry, on a face it was tried on: the tilt, the rectangle that named the + * card (or none: `scale` null) and the answer before it (Python: `TiltedOutcome.to_json`). + * When a rectangle named the card, the fields above are its look's. */ + tilted?: TiltedEvidence; } /** One detected card of a frame. */ export interface BoxResult { @@ -71,15 +76,32 @@ export interface FrameAnalysis { /** How many faces had their turned looks computed, and how many embeddings were computed. */ retried: number; embeddings: number; - /** Where the time went, in ms. `geometricMs`, part of `recognizeMs`, is there only while a - * geometric stage is set. */ + /** Where the time went, in ms. `geometricMs` and `tiltedMs`, parts of `recognizeMs`, are + * there only while a geometric stage is set and while the tilted retry runs. */ timings: { detectorMs: number; recognizeMs: number; analyzeMs: number; geometricMs?: number; + tiltedMs?: number; }; } +/** What a caller says about one frame (`Pipeline.analyze`). */ +export interface AnalyzeOptions { + /** False skips the tilted retry on this frame: the caller's own condition, such as the kind + * of footage. Default true. */ + tilted?: boolean; + /** A point in the frame's pixels, such as the pointer: the tilted retry takes the faces whose + * box holds it first. */ + focus?: { + x: number; + y: number; + } | null; + /** True gives each named card on this frame its outline (tilt.ts `outlineRect`): `rotation`, + * `localWidth` and `localHeight` follow a tilted card, `bounds` stays the box. Default false: + * every card's outline is its box, as before. */ + rotatedOutlines?: boolean; +} export interface PipelineOptions { detector: FrameDetector; embedder: BatchEmbedder; @@ -99,6 +121,9 @@ export interface PipelineOptions { /** The optional geometric stage (geometric/stage.ts). Without it the pipeline is exactly what * it was before the stage existed. `setGeometric` sets or removes it between frames. */ geometric?: GeometricStage | null; + /** The release's `tiltedRetry` settings (tilt.ts), or null: the retry is off. `setTiltedRetry` + * turns it off between frames. */ + tiltedRetry?: TiltedRetrySettings | null; } /** Video time moving back by more than this is a break in continuity (a seek back, a new still). */ export declare const DISCONTINUITY_BACK_SEC = 0.25; @@ -128,6 +153,9 @@ export declare class Pipeline { private geometric; /** The stage that failed during the last frame, and why; taken by `takeGeometricFailure`. */ private geometricFailure; + private tiltedRetry; + /** Why the tilted retry failed during the last frame; taken by `takeTiltedFailure`. */ + private tiltedFailure; constructor(options: PipelineOptions); /** Rows in the bank. */ get size(): number; @@ -137,6 +165,15 @@ export declare class Pipeline { stage: GeometricStage; error: unknown; } | null; + /** The tilted retry's error during a frame since the last call, or null. The pipeline has + * already turned the retry off. */ + takeTiltedFailure(): { + error: unknown; + } | null; + /** The tilted retry's settings in use, or null when it is off. */ + get tiltedSettings(): TiltedRetrySettings | null; + /** Set or remove the tilted retry's settings. Call it between frames. */ + setTiltedRetry(settings: TiltedRetrySettings | null): void; /** The geometric stage in use, or null. */ get geometricStage(): GeometricStage | null; /** Set or remove the geometric stage. Call it between frames: a frame being analyzed keeps @@ -150,8 +187,9 @@ export declare class Pipeline { * it moves backwards, or jumps further forward than any cadence explains, the state between * frames is reset first: after a seek the new picture's cards sit where the old ones were, * and without a reset the tracker would carry the old identities for a few frames. A detector - * or model error rejects the call, and that frame shows nothing. + * or model error rejects the call, and that frame shows nothing. `options` says whether the + * tilted retry runs on this frame and where its focus is. */ - analyze(frame: Frame, videoTimeSec?: number): Promise; + analyze(frame: Frame, videoTimeSec?: number, options?: AnalyzeOptions): Promise; private boxResults; } diff --git a/vendor/cardvision-browser/types/recognition.d.ts b/vendor/cardvision-browser/types/recognition.d.ts index d375a99..c3649b4 100644 --- a/vendor/cardvision-browser/types/recognition.d.ts +++ b/vendor/cardvision-browser/types/recognition.d.ts @@ -4,6 +4,7 @@ import type { BatchEmbedder } from "./embedder.js"; import { type GeometricEvidence } from "./geometric/fusion.js"; import type { GeometricStage } from "./geometric/stage.js"; import type { AcceptRule, EmbeddingRecognizer, ScoredMatch } from "./recognizer.js"; +import { type TiltedRetrySettings } from "./tilt.js"; /** Among boxes whose look reaches tau, one that overlaps a better one by this much is dropped. */ export declare const ACCEPTED_NMS_IOU = 0.3; /** @@ -77,6 +78,39 @@ export interface ScoredProposal { * answered (Python: `GeometricOutcome.to_json`). The fields above are then the fused look's. * Absent when no stage ran. */ geometric?: GeometricEvidence; + /** With the tilted retry, on a face it was tried on: the tilt, the rectangle that named the + * card (or none), and the answer before it. When a rectangle named the card, the fields above + * are that rectangle's look and `orientation` is the look it was embedded at. Absent when the + * retry was not tried. */ + tilted?: TiltedEvidence; + /** With rotated outlines asked for, on a face whose score reaches tau: the card's outline in + * the frame's pixels (`outlineRect`), centered on the box, `angle` its tilt in degrees; the + * box's own rectangle when the tilt is under a degree or cannot be solved exactly. It decides + * nothing. Absent otherwise. */ + outline?: Rect; +} +/** What the tilted retry did with one face (Python: `TiltedOutcome.to_json`). */ +export interface TiltedEvidence { + /** The estimated tilt, in degrees. */ + tilt: number; + /** The scale of the rectangle that named the card, or null when none passed. */ + scale: number | null; + /** The face's answer before the retry. */ + before: { + topId: string; + similarity: number; + margin: number; + }; +} +/** The tilted retry for one frame: the release's settings and, when known, a focus point. */ +export interface TiltedRun { + settings: TiltedRetrySettings; + /** A point in frame pixels, such as the pointer: the faces whose box holds it are retried + * first, smallest box first, then the rest by their score. */ + focus?: { + x: number; + y: number; + } | null; } /** One frame's recognition. */ export interface ScoredFrame { @@ -100,6 +134,11 @@ export interface ScoredFrame { /** The stage's error when it failed on this frame. The frame is then answered exactly as * without a stage, and the caller turns the stage off. */ geometricError?: unknown; + /** With the tilted retry: its time on this frame, in ms. Absent without it, and when it failed. */ + tiltedMs?: number; + /** The tilted retry's error when it failed on this frame. The frame is then answered as + * without it, and the caller turns it off. */ + tiltedError?: unknown; } /** * The order in which the proposals that reach tau enter the suppression: the ones whose chosen @@ -109,6 +148,7 @@ export interface ScoredFrame { export declare function nmsPreOrder(looks: ReadonlyArray<{ tauPassed: boolean; orientation: number; + tilted?: TiltedEvidence; }>): number[]; /** * Suppression over the proposals that reach tau, by score, ignoring identity: a proposal is @@ -135,5 +175,28 @@ export declare function keepAfterNms(proposals: ReadonlyArray; +export declare function recognizeFrame(frame: Frame, embedder: BatchEmbedder, recognizer: EmbeddingRecognizer, faces: readonly Candidate[], minCropPx?: number, stage?: GeometricStage | null, tilted?: TiltedRun | null, rotatedOutlines?: boolean): Promise; +/** + * The order in which the tilted retry takes the faces that are not accepted: those whose box + * holds `focus`, smallest box first, then the rest by their score (`similarity`, the chosen + * cosine look's), highest first; on equal keys, in proposal order. Returns indices. + */ +export declare function tiltedOrder(faces: ReadonlyArray<{ + aabb: AABB; + similarity: number; + accepted: boolean; +}>, focus: { + x: number; + y: number; +} | null): number[]; diff --git a/vendor/cardvision-browser/types/release.d.ts b/vendor/cardvision-browser/types/release.d.ts index b732ea0..89662ff 100644 --- a/vendor/cardvision-browser/types/release.d.ts +++ b/vendor/cardvision-browser/types/release.d.ts @@ -7,6 +7,7 @@ import { type ExecutionProvider } from "./providers.js"; import { Pipeline } from "./pipeline.js"; import { type PreprocessingId } from "./preprocess.js"; import type { AcceptRule } from "./recognizer.js"; +import { type TiltedRetrySettings } from "./tilt.js"; export declare const RELEASE_SCHEMA_VERSION = 1; export declare const RELEASE_KIND = "vision-release"; /** How many rows a detector of this input size emits. */ @@ -65,6 +66,9 @@ export interface VisionRelease { max: number; }; }; + /** The optional `tiltedRetry` block (tilt.ts): null when the release does not turn the + * tilted retry on. */ + tiltedRetry: TiltedRetrySettings | null; } /** * Whether a path is a plain relative one that stays inside the folder of the release document. @@ -149,6 +153,9 @@ export interface ReleaseLoadOptions { * sum of their sizes as the document pins them: what is left to download. Advisory: what it * throws is ignored, and the load goes on. */ onDocument?: (pinnedBytes: number) => void; + /** Called once every file the release pins is fetched and its size and SHA-256 checked, + * before any is read. Advisory: what it throws is ignored, and the load goes on. */ + onFetched?: () => void; } /** * Whether a release measured on onnxruntime-web `releaseOrt` may run on `bundledOrt`: the same diff --git a/vendor/cardvision-browser/types/scene.d.ts b/vendor/cardvision-browser/types/scene.d.ts index 7e3d956..eba6107 100644 --- a/vendor/cardvision-browser/types/scene.d.ts +++ b/vendor/cardvision-browser/types/scene.d.ts @@ -5,6 +5,8 @@ export interface SceneItem { instanceId: string; aabb: AABB; rect: Rect; + /** The card's outline (rotated outlines): a named card is placed by it in place of `rect`. */ + outline?: Rect; /** The card id to show, or null: detected, not named. */ identity: string | null; } diff --git a/vendor/cardvision-browser/types/session-core.d.ts b/vendor/cardvision-browser/types/session-core.d.ts index 17e6f53..f35fb50 100644 --- a/vendor/cardvision-browser/types/session-core.d.ts +++ b/vendor/cardvision-browser/types/session-core.d.ts @@ -1,8 +1,8 @@ import type { AABB } from "./boxes.js"; import type { AnalysisWidth } from "./capture.js"; -import { type AnalysisMode } from "./cadence.js"; +import { type SessionMode } from "./cadence.js"; import { FrameScheduler } from "./frame-scheduler.js"; -import { type GeometricStatus, type ReleaseProgress, type ReleaseStatus, type WorkerReply, type WorkerRequest } from "./messages.js"; +import { type GeometricStatus, type ReleaseProgress, type ReleaseStatus, type TiltedStatus, type WorkerReply, type WorkerRequest } from "./messages.js"; import { type FractionBox } from "./compute-messages.js"; import { hoverAt } from "./hover-at.js"; import type { FrameAnalysis } from "./pipeline.js"; @@ -51,6 +51,8 @@ export interface SessionCoreDeps { onAnalysis?(analysis: FrameAnalysis): void; /** The geometric stage's state changed. */ onGeometricStatus?(status: GeometricStatus): void; + /** The tilted retry was turned off for the session. */ + onTiltedStatus?(status: TiltedStatus): void; /** Replaceable for tests; `hoverAt` by default. */ hoverAt?: typeof hoverAt; /** The time of the picture on screen now, in seconds, for a host that can read it at any @@ -62,13 +64,16 @@ export interface SessionCoreDeps { export interface SessionCoreOptions { /** "pointer" (the default): analyze only around pointer activity. "always-on": analyze for as * long as the session runs. */ - mode?: AnalysisMode; + mode?: SessionMode; /** Hold a shown card through short gaps. Off when absent; `SHOWN_HOLD` is the usual value. */ hold?: ShownHold; /** Other cards' boxes reported with a shown card (default `MAX_OTHER_BOXES`). */ maxOtherBoxes?: number; /** The geometric stage was asked for: its status starts as "loading" (null otherwise). */ geometric?: boolean; + /** True: named cards follow their outline on every frame (`SessionOptions.rotatedOutlines`). + * Default false. */ + rotatedOutlines?: boolean; } /** What `status().loading` becomes on a release-progress reply: the reply's phase while the * release loads; `undefined` (no change) once it has loaded or failed, or the session has @@ -82,6 +87,7 @@ export declare class SessionCore { private readonly mode; private readonly hold; private readonly maxOtherBoxes; + private readonly rotatedOutlines; private readonly hitTest; private readonly st; private cardList; @@ -90,6 +96,9 @@ export declare class SessionCore { private nextRequestId; private inFlight; private lastAcceptedRequestId; + /** Whether the worker answered a look with an analysis since its release loaded: until then + * the watchdog waits `FIRST_ANALYSIS_WATCHDOG_MS`. */ + private analyzedSinceLoad; private generation; private sourceCount; private result; diff --git a/vendor/cardvision-browser/types/session.d.ts b/vendor/cardvision-browser/types/session.d.ts index cb5570e..0b5c248 100644 --- a/vendor/cardvision-browser/types/session.d.ts +++ b/vendor/cardvision-browser/types/session.d.ts @@ -1,5 +1,5 @@ import { type AABB } from "./boxes.js"; -import { type AnalysisMode } from "./cadence.js"; +import { type SessionMode } from "./cadence.js"; import { type ClientBox, type FrameSource, pictureBox } from "./capture.js"; import { type AnalysisState } from "./frame-scheduler.js"; import type { GeometricStatus, ReleaseStatus } from "./messages.js"; @@ -24,7 +24,7 @@ export interface SessionOptions { providers?: ExecutionProvider[]; /** "pointer" (the default): analyze only around pointer activity. "always-on": analyze for as * long as the session runs. */ - mode?: AnalysisMode; + mode?: SessionMode; /** URL of the geometric stage's settings document (it names the packed index). Optional and * off by default: without it recognition is the embedding's alone. With it the stage loads * after the release and joins between frames; `status().geometric` says where it is. */ @@ -35,6 +35,10 @@ export interface SessionOptions { /** False turns the geometric watchdog off (default: on). Meant for measuring the stage, not * for showing cards: with it off, a slow stage is never turned off. */ geometricWatchdog?: boolean; + /** True: each named card's `rotation`, `localWidth` and `localHeight` follow the card when it + * sits at an angle (tilt.ts `outlineRect`), for outlines and hit tests that follow tilted + * cards; `bounds` stays the detector's box. Default false: every card is its box, as before. */ + rotatedOutlines?: boolean; /** Called whenever the status or what would be shown may have changed. */ onUpdate?: () => void; } @@ -74,6 +78,12 @@ export interface SessionStatus { reason: string | null; bytes: number | null; } | null; + /** The tilted retry: on when the release turns it on, off with the worker's reason once it is + * turned off for the session; null when the release does not turn it on. */ + tilted?: { + state: "on" | "off"; + reason: string | null; + } | null; /** While the release loads: downloading its files (`bytes`, their sum once known) or preparing * the models. Null once it has loaded or failed, and before the worker has said. */ loading?: { diff --git a/vendor/cardvision-browser/types/tilt.d.ts b/vendor/cardvision-browser/types/tilt.d.ts new file mode 100644 index 0000000..aec03e8 --- /dev/null +++ b/vendor/cardvision-browser/types/tilt.d.ts @@ -0,0 +1,106 @@ +/** Short side over long side of the card the prior assumes; the same for every game, used only + * when the exact solve is not. */ +export declare const CARD_ASPECT: number; +export declare const TILT_MAX_SIDE = 128; +export declare const TILT_BINS = 90; +export declare const TILT_BORDER = 0.2; +export declare const TILT_MIN_SIDE = 8; +/** The settings of a release's `tiltedRetry` block (Python: `TiltedRetry`). */ +export interface TiltedRetrySettings { + /** Sizes of the rectangles relative to the solved card, in the order they are tried. */ + scales: number[]; + /** At most this many faces of one frame are retried: faces under the focus point first. */ + maxFacesPerFrame: number; + /** The retry is turned off for the session when it takes more than this of a frame on two + * frames in a row. */ + budgetMs: number; +} +export declare const DEFAULT_TILTED_RETRY: Readonly; +/** An angle in degrees folded to (-45, 45] modulo 90. */ +export declare function fold(degrees: number): number; +/** Python's `round` to a whole number: halves go to the even neighbor. */ +export declare function roundHalfEven(value: number): number; +/** An RGB or RGBA picture, row by row. */ +export interface Pixels { + data: Uint8ClampedArray | Uint8Array; + width: number; + height: number; + /** Values per pixel: 4 for RGBA (the default), 3 for RGB. */ + channels?: 3 | 4; +} +/** + * The in-plane tilt, in degrees in (-45, 45], of the card in a crop (Python: `estimate_tilt`). + * The crop is the shipped crop of a detector box. One under 8 px a side once shrunk, or with no + * edges, gives 0. Image coordinates, y down. + */ +export declare function estimateTilt(crop: Pixels): number; +/** The outline (`outlineRect`) stays the detector's box below this tilt, in degrees: an upright + * card reads a fraction of a degree (exactly +0.5 when perfectly axis-aligned), which is noise. */ +export declare const OUTLINE_MIN_TILT = 1; +/** How `rectFromAabb` found the sides. */ +export type RectHow = "exact" | "prior"; +/** + * The sides `[w, h]` of a card turned by `tilt` degrees whose bounding box is `width` x `height` + * (Python: `rect_from_aabb`). `w` runs along the tilted top edge. The exact solve is used while + * it is well posed and card-shaped; otherwise the card-aspect prior, portrait or landscape, + * whichever fits better, scaled by least squares. + */ +export declare function rectFromAabb(width: number, height: number, tilt: number): { + w: number; + h: number; + how: RectHow; +}; +/** + * The rectangle of `frame` centered on `(cx, cy)`, `width` x `height`, turned by `tilt` degrees, + * as RGBA (Python: `sample_rect`). Sides are rounded half to even, at least 2 px. Bilinear, + * pixel centers at integer + 0.5, black outside the frame, rounded half up. + */ +export declare function sampleRect(frame: Pixels, cx: number, cy: number, width: number, height: number, tilt: number): { + rgba: Uint8ClampedArray; + width: number; + height: number; +}; +/** + * The tilt of the card in `box` and its straightened rectangles at `scales`, in that order + * (Python: `tilted_rects`). `box` is the detector box in `frame`'s pixels, unrounded; `crop` + * the shipped crop it gave. + */ +export declare function tiltedRects(frame: Pixels, box: { + x: number; + y: number; + width: number; + height: number; +}, crop: Pixels, scales: readonly number[]): { + tilt: number; + rects: Array<{ + rgba: Uint8ClampedArray; + width: number; + height: number; + }>; +}; +/** + * The outline of a card whose detector box is `boxWidth` x `boxHeight` and whose estimated tilt + * is `tilt` degrees (Python: `outline_from_tilt`): the rectangle `rectFromAabb` solves, centered + * on the box and turned by the tilt, or the box itself (`tilt` 0, the box's sides) when the tilt + * is under `OUTLINE_MIN_TILT` in size or the solve is not exact (the card-aspect prior, near 45 + * degrees, or a box no tilted card fits). `w` runs along the tilted top edge. + */ +export declare function outlineFromTilt(tilt: number, boxWidth: number, boxHeight: number): { + tilt: number; + w: number; + h: number; +}; +/** + * The outline of the card in a detector box `boxWidth` x `boxHeight` pixels, from `crop`, the + * shipped crop that box gave (Python: `outline_rect`): `outlineFromTilt` of its `estimateTilt`. + * In pixels, `tilt` in degrees in (-45, 45], positive clockwise on screen (y down). For drawing + * and hit-testing a card that sits at an angle; it decides nothing. + */ +export declare function outlineRect(crop: Pixels, boxWidth: number, boxHeight: number): { + tilt: number; + w: number; + h: number; +}; +/** Every reason `raw` is not a valid `tiltedRetry` block, or null when it is (Python: + * `tilted_retry_errors`). */ +export declare function tiltedRetryError(raw: unknown): string | null; diff --git a/vendor/cardvision-browser/types/tracker.d.ts b/vendor/cardvision-browser/types/tracker.d.ts index cb7a040..fb4fc52 100644 --- a/vendor/cardvision-browser/types/tracker.d.ts +++ b/vendor/cardvision-browser/types/tracker.d.ts @@ -7,6 +7,9 @@ export interface Detection { cardId: string | null; /** The look's unit-length embedding. */ embedding: Float32Array; + /** The card's outline when rotated outlines are on (`ScoredProposal.outline`); carried along, + * never used to match. */ + outline?: Rect; } /** One live track after an update. */ export interface TrackCard { diff --git a/vendor/cardvision-browser/types/worker-host.d.ts b/vendor/cardvision-browser/types/worker-host.d.ts index 7934133..ee1de41 100644 --- a/vendor/cardvision-browser/types/worker-host.d.ts +++ b/vendor/cardvision-browser/types/worker-host.d.ts @@ -33,6 +33,8 @@ export interface WorkerHostDeps { readBitmap(bitmap: ImageBitmap): FramePixels | null; now?: () => number; watchdog?: GeometricWatchdog; + /** The tilted retry's watchdog; by default one over the release's `budgetMs`. */ + tiltedWatchdog?: GeometricWatchdog; } /** The request handler of one worker. */ export declare function createWorkerHost(deps: WorkerHostDeps): { diff --git a/vendor/cardvision-browser/types/worker-session.d.ts b/vendor/cardvision-browser/types/worker-session.d.ts index c65ba55..f8dba09 100644 --- a/vendor/cardvision-browser/types/worker-session.d.ts +++ b/vendor/cardvision-browser/types/worker-session.d.ts @@ -1,5 +1,5 @@ import type { AnalysisWidth } from "./capture.js"; -import type { AnalysisMode } from "./cadence.js"; +import type { SessionMode } from "./cadence.js"; import type { GeometricStatus, WorkerReply, WorkerRequest } from "./messages.js"; import type { FrameAnalysis } from "./pipeline.js"; import type { CardTable } from "./remote/card-table.js"; @@ -36,9 +36,11 @@ export interface WorkerSessionEnv { loadRequest: LoadRequest; /** The clock, in ms. */ now(): number; - mode?: AnalysisMode; + mode?: SessionMode; /** Hold a shown card through short gaps (`SHOWN_HOLD`); off when absent. */ hold?: ShownHold; + /** True: named cards follow their outline (`SessionOptions.rotatedOutlines`). */ + rotatedOutlines?: boolean; /** Capture the picture now and answer the core with `frame` or `captureFailed`. */ capture(requestId: number, analysisWidth: AnalysisWidth): void; onShown?(card: Shown | null): void; @@ -68,7 +70,8 @@ export interface WorkerSessionEnv { export interface WorkerSession { /** Null only while `chooseRelease` decides. */ readonly core: SessionCore | null; - /** Recognition is off for good: the restart was used up, a worker could not be made, or `stop`. */ + /** Recognition is off for good: the bundled release failed to load, the restart was used up, a + * worker could not be made, or `stop`. The core's status says why. */ readonly stopped: boolean; readonly restarts: number; readonly release: ReleaseInEffect; diff --git a/vendor/cardvision-browser/worker.js b/vendor/cardvision-browser/worker.js index caee298..6e45dc8 100644 --- a/vendor/cardvision-browser/worker.js +++ b/vendor/cardvision-browser/worker.js @@ -1,4 +1,4 @@ -var kr=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var wp="geometric-index-packed";var AI="cardvision-geometric-index",_t=class extends Error{code;constructor(t,n){super(n),this.name="GeometricIndexError",this.code=t}},kI=/^[0-9a-f]{16}$/,Rs=/^[0-9a-f]{64}$/,_p=40,mn=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),gn=e=>typeof e=="number"&&Number.isInteger(e)&&e>=0,rt=e=>{throw new _t("manifest",e)};function PI(e){return typeof e!="string"||e.trim()===""||e.includes("\\")||e.includes("://")||e.startsWith("/")||/^[A-Za-z]:/.test(e)?!1:!e.split("/").includes("..")}function CI(e){if(!mn(e))return rt("the manifest is not an object");if(e.kind!==wp)return rt(`the manifest's kind is not ${wp}`);if(e.schemaVersion!==1)return rt(`schemaVersion ${String(e.schemaVersion)} is not 1`);let t=e.source,n=e.bank;if(!mn(t)||typeof t.sha256!="string"||!Rs.test(t.sha256))return rt("source.sha256 must be 64 hex digits");if(!mn(n)||typeof n.sha256!="string"||!Rs.test(n.sha256))return rt("bank.sha256 must be 64 hex digits");if(typeof e.opencv!="string")return rt("opencv must be a version string");let o=e.features;if(!mn(o)||o.type!=="orb")return rt("features.type must be orb: only ORB indexes are packed");if(!Array.isArray(e.shards)||e.shards.length===0)return rt("shards must be a non-empty list");if(!Array.isArray(e.cards))return rt("cards must be a list");let u=[],l=[],d=0;for(let[a,p]of e.shards.entries()){if(!mn(p)||!PI(p.path)||typeof p.sha256!="string"||!Rs.test(p.sha256)||!gn(p.rows))return rt(`shards[${a}] must have a relative path, a 64-hex sha256 and a row count`);u.push({path:p.path,sha256:p.sha256,rows:p.rows}),l.push(d),d+=p.rows}let c=u.map(()=>0),s=new Set,r=[],i=0;for(let[a,p]of e.cards.entries()){if(!mn(p)||typeof p.cardId!="string"||p.cardId===""||s.has(p.cardId))return rt(`cards[${a}] has a missing or repeated cardId`);let h=`cards[${a}] (${p.cardId})`;if(typeof p.artV!="string"||!kI.test(p.artV))return rt(`${h}: artV must be 16 lowercase hex digits`);if(!gn(p.width)||!gn(p.height)||!gn(p.n)||!gn(p.shard)||!gn(p.offset))return rt(`${h}: width, height, n, shard and offset must be whole numbers`);let{shard:m,offset:b,n:x}=p;if(m>=u.length)return rt(`${h}: shard ${m} does not exist`);if(mn.toString(16).padStart(2,"0")).join("")}function DI(e,t,n){if(e.bankSha256!==t)throw new _t("bank-sha",`the index was built for bank ${e.bankSha256.slice(0,16)}, the release's is ${t.slice(0,16)}`);let o=new Set(e.cards.map(l=>l.cardId)),u=n.filter(l=>!o.has(l));if(u.length>0)throw new _t("bank-ids",`the index has no entry for ${u.length} card(s) of the bank (e.g. ${u.slice(0,3).join(", ")})`)}async function vp(e,t){let n=t.fetch??((T,I)=>fetch(T,I)),o=t.cache??(typeof caches>"u"?void 0:caches),u=typeof AbortController>"u"?null:new AbortController,l=async(T,I)=>{let A;try{A=await n(T,u?{signal:u.signal}:void 0)}catch(P){throw new _t("shard",`${I}: ${P instanceof Error?P.message:String(P)}`)}if(!A.ok)throw new _t("shard",`${I}: HTTP ${A.status}`);return A},d,c=await(await l(e,"manifest")).arrayBuffer().catch(T=>{throw new _t("shard",`manifest: ${T instanceof Error?T.message:String(T)}`)});if(t.manifestSha256!==void 0){let T=await Ns(c);if(T!==t.manifestSha256.toLowerCase())throw new _t("manifest-sha",`the manifest's SHA-256 ${T.slice(0,16)} is not the pinned ${t.manifestSha256.slice(0,16)}`)}try{d=JSON.parse(new TextDecoder().decode(c))}catch(T){throw new _t("manifest",`the manifest is not JSON: ${T instanceof Error?T.message:String(T)}`)}let s=CI(d),r=new URL(e,globalThis.location?.href).href;DI(s,t.bankSha256,t.bankIds);let i=o?await o.open(AI).catch(()=>{}):void 0,a=new zs(s),p=async T=>{let I=s.shards[T],A=new URL(I.path,r).href,P=i?await i.match(A).catch(()=>{}):void 0;if(P){let U=await P.arrayBuffer();if(await Ns(U)===I.sha256)return U;await i?.delete(A).catch(()=>!1)}let O=await(await l(A,I.path)).arrayBuffer(),B=await Ns(O);if(B!==I.sha256)throw new _t("hash",`${I.path}: SHA-256 ${B.slice(0,16)} is not the manifest's ${I.sha256.slice(0,16)}`);return await i?.put(A,new Response(O.slice(0))).catch(()=>{}),O},h=0,m=!1,b=async()=>{try{for(;;){if(m)return;let T=h++;if(T>=s.shards.length)return;let I=await p(T);if(m)return;a.place(T,I)}}catch(T){if(!m)throw m=!0,u?.abort(),T}},x=Array.from({length:Math.min(2,s.shards.length)},b),y=(await Promise.allSettled(x)).find(T=>T.status==="rejected");if(y)throw y.reason;let S=a.finish();return i&&await RI(i,new URL(".",r).href),S}async function RI(e,t){try{let n=await e.keys();await Promise.all(n.filter(o=>!o.url.startsWith(t)).map(o=>e.delete(o)))}catch{}}var Bs=new Map;function xp(e,t={}){let n=String(e),o=Bs.get(n);return o===void 0&&(o=(async()=>{let u=await import(n);if(typeof u.default!="function")throw new Error(`${n}: not an OpenCV.js ES module (no default export)`);let l={};t.wasmBinary!==void 0&&(l.wasmBinary=t.wasmBinary);let d=await u.default(l);if(typeof d.ORB!="function"||typeof d.estimateAffine2D!="function")throw new Error(`${n}: this OpenCV.js lacks the functions the geometric stage needs`);return d})(),o.catch(()=>Bs.delete(n)),Bs.set(n,o)),o}function NI(e){if(e.length%32!==0)throw new Error(`queries: ${e.length} bytes is not a whole number of 32-byte descriptors`);return e.length/32}function zI(e){if(e.length%32!==0)throw new Error(`descriptors: ${e.length} bytes is not a whole number of 32-byte descriptors`);return e.length/32}function BI(e){return{best:new Uint32Array(e).fill(4294967295),second:new Uint32Array(e).fill(4294967295),dBest:new Uint16Array(e).fill(65535),dSecond:new Uint16Array(e).fill(65535)}}var eo=23,Ms=(1<Array.from({length:t},(l,d)=>u(d)).join(` +var Br=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var Ip="geometric-index-packed";var LI="cardvision-geometric-index",Ot=class extends Error{code;constructor(t,n){super(n),this.name="GeometricIndexError",this.code=t}},UI=/^[0-9a-f]{16}$/,Bs=/^[0-9a-f]{64}$/,Op=40,$n=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),Tn=e=>typeof e=="number"&&Number.isInteger(e)&&e>=0,at=e=>{throw new Ot("manifest",e)};function VI(e){return typeof e!="string"||e.trim()===""||e.includes("\\")||e.includes("://")||e.startsWith("/")||/^[A-Za-z]:/.test(e)?!1:!e.split("/").includes("..")}function GI(e){if(!$n(e))return at("the manifest is not an object");if(e.kind!==Ip)return at(`the manifest's kind is not ${Ip}`);if(e.schemaVersion!==1)return at(`schemaVersion ${String(e.schemaVersion)} is not 1`);let t=e.source,n=e.bank;if(!$n(t)||typeof t.sha256!="string"||!Bs.test(t.sha256))return at("source.sha256 must be 64 hex digits");if(!$n(n)||typeof n.sha256!="string"||!Bs.test(n.sha256))return at("bank.sha256 must be 64 hex digits");if(typeof e.opencv!="string")return at("opencv must be a version string");let i=e.features;if(!$n(i)||i.type!=="orb")return at("features.type must be orb: only ORB indexes are packed");if(!Array.isArray(e.shards)||e.shards.length===0)return at("shards must be a non-empty list");if(!Array.isArray(e.cards))return at("cards must be a list");let u=[],l=[],d=0;for(let[a,p]of e.shards.entries()){if(!$n(p)||!VI(p.path)||typeof p.sha256!="string"||!Bs.test(p.sha256)||!Tn(p.rows))return at(`shards[${a}] must have a relative path, a 64-hex sha256 and a row count`);u.push({path:p.path,sha256:p.sha256,rows:p.rows}),l.push(d),d+=p.rows}let c=u.map(()=>0),s=new Set,r=[],o=0;for(let[a,p]of e.cards.entries()){if(!$n(p)||typeof p.cardId!="string"||p.cardId===""||s.has(p.cardId))return at(`cards[${a}] has a missing or repeated cardId`);let h=`cards[${a}] (${p.cardId})`;if(typeof p.artV!="string"||!UI.test(p.artV))return at(`${h}: artV must be 16 lowercase hex digits`);if(!Tn(p.width)||!Tn(p.height)||!Tn(p.n)||!Tn(p.shard)||!Tn(p.offset))return at(`${h}: width, height, n, shard and offset must be whole numbers`);let{shard:m,offset:g,n:v}=p;if(m>=u.length)return at(`${h}: shard ${m} does not exist`);if(mn.toString(16).padStart(2,"0")).join("")}function qI(e,t,n){if(e.bankSha256!==t)throw new Ot("bank-sha",`the index was built for bank ${e.bankSha256.slice(0,16)}, the release's is ${t.slice(0,16)}`);let i=new Set(e.cards.map(l=>l.cardId)),u=n.filter(l=>!i.has(l));if(u.length>0)throw new Ot("bank-ids",`the index has no entry for ${u.length} card(s) of the bank (e.g. ${u.slice(0,3).join(", ")})`)}async function Ep(e,t){let n=t.fetch??((S,O)=>fetch(S,O)),i=t.cache??(typeof caches>"u"?void 0:caches),u=typeof AbortController>"u"?null:new AbortController,l=async(S,O)=>{let A;try{A=await n(S,u?{signal:u.signal}:void 0)}catch(k){throw new Ot("shard",`${O}: ${k instanceof Error?k.message:String(k)}`)}if(!A.ok)throw new Ot("shard",`${O}: HTTP ${A.status}`);return A},d,c=await(await l(e,"manifest")).arrayBuffer().catch(S=>{throw new Ot("shard",`manifest: ${S instanceof Error?S.message:String(S)}`)});if(t.manifestSha256!==void 0){let S=await Fs(c);if(S!==t.manifestSha256.toLowerCase())throw new Ot("manifest-sha",`the manifest's SHA-256 ${S.slice(0,16)} is not the pinned ${t.manifestSha256.slice(0,16)}`)}try{d=JSON.parse(new TextDecoder().decode(c))}catch(S){throw new Ot("manifest",`the manifest is not JSON: ${S instanceof Error?S.message:String(S)}`)}let s=GI(d),r=new URL(e,globalThis.location?.href).href;qI(s,t.bankSha256,t.bankIds);let o=i?await i.open(LI).catch(()=>{}):void 0,a=new js(s),p=async S=>{let O=s.shards[S],A=new URL(O.path,r).href,k=o?await o.match(A).catch(()=>{}):void 0;if(k){let M=await k.arrayBuffer();if(await Fs(M)===O.sha256)return M;await o?.delete(A).catch(()=>!1)}let I=await(await l(A,O.path)).arrayBuffer(),R=await Fs(I);if(R!==O.sha256)throw new Ot("hash",`${O.path}: SHA-256 ${R.slice(0,16)} is not the manifest's ${O.sha256.slice(0,16)}`);return await o?.put(A,new Response(I.slice(0))).catch(()=>{}),I},h=0,m=!1,g=async()=>{try{for(;;){if(m)return;let S=h++;if(S>=s.shards.length)return;let O=await p(S);if(m)return;a.place(S,O)}}catch(S){if(!m)throw m=!0,u?.abort(),S}},v=Array.from({length:Math.min(2,s.shards.length)},g),y=(await Promise.allSettled(v)).find(S=>S.status==="rejected");if(y)throw y.reason;let T=a.finish();return o&&await HI(o,new URL(".",r).href),T}async function HI(e,t){try{let n=await e.keys();await Promise.all(n.filter(i=>!i.url.startsWith(t)).map(i=>e.delete(i)))}catch{}}var Ls=new Map;function Ap(e,t={}){let n=String(e),i=Ls.get(n);return i===void 0&&(i=(async()=>{let u=await import(n);if(typeof u.default!="function")throw new Error(`${n}: not an OpenCV.js ES module (no default export)`);let l={};t.wasmBinary!==void 0&&(l.wasmBinary=t.wasmBinary);let d=await u.default(l);if(typeof d.ORB!="function"||typeof d.estimateAffine2D!="function")throw new Error(`${n}: this OpenCV.js lacks the functions the geometric stage needs`);return d})(),i.catch(()=>Ls.delete(n)),Ls.set(n,i)),i}function WI(e){if(e.length%32!==0)throw new Error(`queries: ${e.length} bytes is not a whole number of 32-byte descriptors`);return e.length/32}function KI(e){if(e.length%32!==0)throw new Error(`descriptors: ${e.length} bytes is not a whole number of 32-byte descriptors`);return e.length/32}function XI(e){return{best:new Uint32Array(e).fill(4294967295),second:new Uint32Array(e).fill(4294967295),dBest:new Uint16Array(e).fill(65535),dSecond:new Uint16Array(e).fill(65535)}}var ao=23,Us=(1<Array.from({length:t},(l,d)=>u(d)).join(` `);return` struct Params { queries: u32, rows: u32, base: u32, chunkRows: u32, firstChunk: u32, chunks: u32 } @group(0) @binding(0) var desc: array>; @@ -10,7 +10,7 @@ var tile: array, ${n*2}>; @compute @workgroup_size(${e}) fn main(@builtin(workgroup_id) wg: vec3, @builtin(local_invocation_index) li: u32) { let first = (wg.y * ${e}u + li) * ${t}u; -${o(u=>` let qi${u} = min(first + ${u}u, max(p.queries, 1u) - 1u); +${i(u=>` let qi${u} = min(first + ${u}u, max(p.queries, 1u) - 1u); let qa${u} = queries[qi${u} * 2u]; let qb${u} = queries[qi${u} * 2u + 1u]; var b${u} = 0xffffffffu; @@ -29,19 +29,19 @@ ${o(u=>` let qi${u} = min(first + ${u}u, max(p.queries, 1u) - 1u); let a = tile[r * 2u]; let b = tile[r * 2u + 1u]; let row = row0 + r; -${o(u=>` { +${i(u=>` { let c = countOneBits(qa${u} ^ a) + countOneBits(qb${u} ^ b); - let k = ((c.x + c.y + c.z + c.w) << ${eo}u) | row; + let k = ((c.x + c.y + c.z + c.w) << ${ao}u) | row; s${u} = min(s${u}, max(b${u}, k)); b${u} = min(b${u}, k); }`)} } } -${o(u=>` if (first + ${u}u < p.queries) { +${i(u=>` if (first + ${u}u < p.queries) { partial[(first + ${u}u) * p.chunks + p.firstChunk + wg.x] = vec2(b${u}, s${u}); }`)} } -`}var jI=` +`}var JI=` struct Params { queries: u32, chunks: u32 } @group(0) @binding(0) var partial: array>; @group(0) @binding(1) var merged: array>; @@ -60,7 +60,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { } merged[q] = vec2(b, s); } -`;async function Ip(e,t,n={}){let o=zI(t);if(o>$p)throw new Error(`descriptors: ${o} rows; the GPU vote holds at most ${$p}`);let u={...MI,...n};for(let[D,Q]of Object.entries(u))if(Q!==void 0&&(!Number.isInteger(Q)||Q<1))throw new Error(`gpuVoter: ${D} must be a positive integer`);if(u.chunkRows%u.tileRows!==0)throw new Error("gpuVoter: chunkRows must be a multiple of tileRows");let l=e.limits,d=Math.min(l.maxStorageBufferBindingSize,l.maxBufferSize),c=Math.floor(d/32/u.chunkRows)*u.chunkRows;n.maxRowsPerBuffer!==void 0&&(c=Math.min(c,Math.floor(n.maxRowsPerBuffer/u.chunkRows)*u.chunkRows));let s=l.maxComputeWorkgroupsPerDimension;if(c=Math.min(c,s*u.chunkRows),cl.maxComputeWorkgroupStorageSize)throw new Error("gpuVoter: tileRows exceeds the device's workgroup memory");if(u.workgroupSize>l.maxComputeInvocationsPerWorkgroup)throw new Error("gpuVoter: workgroupSize exceeds the device's limit");e.pushErrorScope("validation"),e.pushErrorScope("out-of-memory");let h=await e.createComputePipelineAsync({layout:"auto",compute:{module:e.createShaderModule({code:FI(u.workgroupSize,u.queriesPerInvocation,u.tileRows)}),entryPoint:"main"}}),m=await e.createComputePipelineAsync({layout:"auto",compute:{module:e.createShaderModule({code:jI}),entryPoint:"main"}}),b=[];for(let D=0;D0&&e.queue.writeBuffer(_,0,t,D*32,Q*32);let C=e.createBuffer({size:32,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST});if(b.push({buffer:_,rows:Q,base:D,firstChunk:D/u.chunkRows,chunks:Math.max(1,Math.ceil(Q/u.chunkRows)),params:C}),Q<=0)break}let x=e.createBuffer({size:p*32,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST}),w=e.createBuffer({size:p*r*8,usage:GPUBufferUsage.STORAGE}),y=e.createBuffer({size:p*8,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),S=e.createBuffer({size:p*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),T=e.createBuffer({size:16,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}),I=b.map(D=>e.createBindGroup({layout:h.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:D.buffer}},{binding:1,resource:{buffer:x}},{binding:2,resource:{buffer:w}},{binding:3,resource:{buffer:D.params}}]})),A=e.createBindGroup({layout:m.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:w}},{binding:1,resource:{buffer:y}},{binding:2,resource:{buffer:T}}]}),P=await e.popErrorScope(),O=await e.popErrorScope(),B=P??O;if(B)throw new Error(`gpuVoter: ${B.message}`);let U=!1,X=Promise.resolve();async function q(D,Q,_,C){e.queue.writeBuffer(x,0,D.buffer,D.byteOffset,Q*32);for(let H of b)e.queue.writeBuffer(H.params,0,new Uint32Array([Q,H.rows,H.base,u.chunkRows,H.firstChunk,r,0,0]));e.queue.writeBuffer(T,0,new Uint32Array([Q,r,0,0]));let F=e.createCommandEncoder(),V=F.beginComputePass();V.setPipeline(h),b.forEach((H,M)=>{V.setBindGroup(0,I[M]),V.dispatchWorkgroups(H.chunks,Math.ceil(Q/i))}),V.setPipeline(m),V.setBindGroup(0,A),V.dispatchWorkgroups(Math.ceil(Q/64)),V.end(),F.copyBufferToBuffer(y,0,S,0,Q*8),e.queue.submit([F.finish()]),await S.mapAsync(GPUMapMode.READ,0,Q*8);let G=new Uint32Array(S.getMappedRange(0,Q*8).slice(0));S.unmap();for(let H=0;H>>eo),J!==Tp&&(_.second[C+H]=J&Ms,_.dSecond[C+H]=J>>>eo)}}return{knn2(D){let Q=async()=>{if(U)throw new Error("gpuVoter: used after dispose");let C=NI(D),F=BI(C);if(o===0)return F;for(let V=0;V{}),_},dispose(){U||(U=!0,X.then(()=>{for(let D of b)D.buffer.destroy(),D.params.destroy();for(let D of[x,w,y,S,T])D.destroy()}))}}}var LI=Object.create,zo=Object.defineProperty,UI=Object.getOwnPropertyDescriptor,VI=Object.getOwnPropertyNames,GI=Object.getPrototypeOf,qI=Object.prototype.hasOwnProperty,gu=(e=>typeof kr<"u"?kr:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof kr<"u"?kr:t)[n]}):e)(function(e){if(typeof kr<"u")return kr.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')}),R=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}},ie=(e,t)=>()=>{try{return t||e((t={exports:{}}).exports,t),t.exports}catch(n){throw t=0,n}},un=(e,t)=>{for(var n in t)zo(e,n,{get:t[n],enumerable:!0})},Py=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of VI(t))!qI.call(e,u)&&u!==n&&zo(e,u,{get:()=>t[u],enumerable:!(o=UI(t,u))||o.enumerable});return e},ce=(e,t,n)=>(n=e!=null?LI(GI(e)):{},Py(t||!e||!e.__esModule?zo(n,"default",{value:e,enumerable:!0}):n,e)),Tn=e=>Py(zo({},"__esModule",{value:!0}),e),Kn,ur,en,Op,Cy,Dy=R(()=>{"use strict";Kn=new Map,ur=[],en=(e,t,n)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let o=Kn.get(e);if(o===void 0)Kn.set(e,{backend:t,priority:n});else{if(o.priority>n)return;if(o.priority===n&&o.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){let u=ur.indexOf(e);u!==-1&&ur.splice(u,1);for(let l=0;l{let t=Kn.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let n=!!t.initPromise;try{return n||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(o){return n||(t.error=`${o}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},Cy=async e=>{let t=e.executionProviders||[],n=t.map(s=>typeof s=="string"?s:s.name),o=n.length===0?ur:n,u,l=[],d=new Set;for(let s of o){let r=await Op(s);typeof r=="string"?l.push({name:s,err:r}):(u||(u=r),u===r&&d.add(s))}if(!u)throw new Error(`no available backend found. ERR: ${l.map(s=>`[${s.name}] ${s.err}`).join(", ")}`);for(let{name:s,err:r}of l)n.includes(s)&&console.warn(`removing requested execution provider "${s}" from session options because it is not available: ${r}`);let c=t.filter(s=>d.has(typeof s=="string"?s:s.name));return[u,new Proxy(e,{get:(s,r)=>r==="executionProviders"?c:Reflect.get(s,r)})]}}),HI=R(()=>{"use strict";Dy()}),Ry,WI=R(()=>{"use strict";Ry="1.29.0"}),Fs,et,Ny=R(()=>{"use strict";WI(),Fs="warning",et={wasm:{},webgl:{},webgpu:{},versions:{common:Ry},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Fs=e}},get logLevel(){return Fs}},Object.defineProperty(et,"logLevel",{enumerable:!0})}),le,KI=R(()=>{"use strict";Ny(),le=et}),zy,By,XI=R(()=>{"use strict";zy=(e,t)=>{let n=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);n.width=e.dims[3],n.height=e.dims[2];let o=n.getContext("2d");if(o!=null){let u,l;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[3]):(u=e.dims[3],l=e.dims[2]);let d=t?.format!==void 0?t.format:"RGB",c=t?.norm,s,r;c===void 0||c.mean===void 0?s=[255,255,255,255]:typeof c.mean=="number"?s=[c.mean,c.mean,c.mean,c.mean]:(s=[c.mean[0],c.mean[1],c.mean[2],0],c.mean[3]!==void 0&&(s[3]=c.mean[3])),c===void 0||c.bias===void 0?r=[0,0,0,0]:typeof c.bias=="number"?r=[c.bias,c.bias,c.bias,c.bias]:(r=[c.bias[0],c.bias[1],c.bias[2],0],c.bias[3]!==void 0&&(r[3]=c.bias[3]));let i=l*u,a=0,p=i,h=i*2,m=-1;d==="RGBA"?(a=0,p=i,h=i*2,m=i*3):d==="RGB"?(a=0,p=i,h=i*2):d==="RBG"&&(a=0,h=i,p=i*2);for(let b=0;b{let n=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),o;if(n!=null){let u,l,d;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[1],d=e.dims[3]):(u=e.dims[3],l=e.dims[2],d=e.dims[1]);let c=t!==void 0&&t.format!==void 0?t.format:"RGB",s=t?.norm,r,i;s===void 0||s.mean===void 0?r=[255,255,255,255]:typeof s.mean=="number"?r=[s.mean,s.mean,s.mean,s.mean]:(r=[s.mean[0],s.mean[1],s.mean[2],255],s.mean[3]!==void 0&&(r[3]=s.mean[3])),s===void 0||s.bias===void 0?i=[0,0,0,0]:typeof s.bias=="number"?i=[s.bias,s.bias,s.bias,s.bias]:(i=[s.bias[0],s.bias[1],s.bias[2],0],s.bias[3]!==void 0&&(i[3]=s.bias[3]));let a=l*u;if(t!==void 0&&(t.format!==void 0&&d===4&&t.format!=="RGBA"||d===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let p=4,h=0,m=1,b=2,x=3,w=0,y=a,S=a*2,T=-1;c==="RGBA"?(w=0,y=a,S=a*2,T=a*3):c==="RGB"?(w=0,y=a,S=a*2):c==="RBG"&&(w=0,S=a,y=a*2),o=n.createImageData(u,l);for(let I=0;I{"use strict";xl(),to=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:n,width:o}=t,u=t.norm??{mean:255,bias:0},l,d;typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:l=[u.mean[0],u.mean[1],u.mean[2],u.mean[3]??255],typeof u.bias=="number"?d=[u.bias,u.bias,u.bias,u.bias]:d=[u.bias[0],u.bias[1],u.bias[2],u.bias[3]??0];let c=t.format!==void 0?t.format:"RGBA",s=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",r=n*o,i=s==="RGBA"?new Float32Array(r*4):new Float32Array(r*3),a=4,p=0,h=1,m=2,b=3,x=0,w=r,y=r*2,S=-1;c==="RGB"&&(a=3,p=0,h=1,m=2,b=-1),s==="RGBA"?S=r*3:s==="RBG"?(x=0,y=r,w=r*2):s==="BGR"&&(y=0,w=r,x=r*2);for(let T=0;T{let n=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,o=typeof ImageData<"u"&&e instanceof ImageData,u=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,l=typeof e=="string",d,c=t??{},s=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},r=i=>typeof HTMLCanvasElement<"u"&&i instanceof HTMLCanvasElement||i instanceof OffscreenCanvas?i.getContext("2d"):null;if(n){let i=s();i.width=e.width,i.height=e.height;let a=r(i);if(a!=null){let p=e.height,h=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(p=t.resizedHeight,h=t.resizedWidth),t!==void 0){if(c=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");c.tensorFormat="RGBA",c.height=p,c.width=h}else c.tensorFormat="RGBA",c.height=p,c.width=h;a.drawImage(e,0,0),d=a.getImageData(0,0,h,p).data}else throw new Error("Can not access image data")}else if(o){let i,a;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(i=t.resizedHeight,a=t.resizedWidth):(i=e.height,a=e.width),t!==void 0&&(c=t),c.format="RGBA",c.height=i,c.width=a,t!==void 0){let p=s();p.width=a,p.height=i;let h=r(p);if(h!=null)h.putImageData(e,0,0),d=h.getImageData(0,0,a,i).data;else throw new Error("Can not access image data")}else d=e.data}else if(u){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let i=s();i.width=e.width,i.height=e.height;let a=r(i);if(a!=null){let p=e.height,h=e.width;return a.drawImage(e,0,0,h,p),d=a.getImageData(0,0,h,p).data,c.height=p,c.width=h,to(d,c)}else throw new Error("Can not access image data")}else{if(l)return new Promise((i,a)=>{let p=s(),h=r(p);if(!e||!h)return a();let m=new Image;m.crossOrigin="Anonymous",m.src=e,m.onload=()=>{p.width=m.width,p.height=m.height,h.drawImage(m,0,0,p.width,p.height);let b=h.getImageData(0,0,p.width,p.height);c.height=p.height,c.width=p.width,i(to(b.data,c))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(d!==void 0)return to(d,c);throw new Error("Input data provided is not supported - aborted tensor creation")},Fy=(e,t)=>{let{width:n,height:o,download:u,dispose:l}=t,d=[1,o,n,4];return new ft({location:"texture",type:"float32",texture:e,dims:d,download:u,dispose:l})},jy=(e,t)=>{let{dataType:n,dims:o,download:u,dispose:l}=t;return new ft({location:"gpu-buffer",type:n??"float32",gpuBuffer:e,dims:o,download:u,dispose:l})},Ly=(e,t)=>{let{dataType:n,dims:o,download:u,dispose:l}=t;return new ft({location:"ml-tensor",type:n??"float32",mlTensor:e,dims:o,download:u,dispose:l})},Uy=(e,t,n)=>new ft({location:"cpu-pinned",type:e,data:t,dims:n??[t.length]})}),Xr,ci,js,Vy,JI=R(()=>{"use strict";Xr=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),ci=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),js=!1,Vy=()=>{if(!js){js=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,n=globalThis.Float16Array,o=typeof n<"u"&&n.from;e&&(Xr.set("int64",BigInt64Array),ci.set(BigInt64Array,"int64")),t&&(Xr.set("uint64",BigUint64Array),ci.set(BigUint64Array,"uint64")),o?(Xr.set("float16",n),ci.set(n,"float16")):Xr.set("float16",Uint16Array)}}}),Gy,qy,YI=R(()=>{"use strict";xl(),Gy=e=>{let t=1;for(let n=0;n{switch(e.location){case"cpu":return new ft(e.type,e.data,t);case"cpu-pinned":return new ft({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new ft({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new ft({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new ft({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),ft,xl=R(()=>{"use strict";XI(),ZI(),JI(),YI(),ft=class{constructor(e,t,n){Vy();let o,u;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,o=e.type,u=e.dims,e.location){case"cpu-pinned":{let d=Xr.get(o);if(!d)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(e.data instanceof d))throw new TypeError(`buffer should be of type ${d.name}`);this.cpuData=e.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool"&&o!=="uint4"&&o!=="int4")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint64"&&o!=="int8"&&o!=="uint8"&&o!=="bool"&&o!=="uint4"&&o!=="int4")throw new TypeError(`unsupported type "${o}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let d,c;if(typeof e=="string")if(o=e,c=n,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");d=t}else{let s=Xr.get(e);if(s===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&s===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${s.name} as data.`);e==="uint64"||e==="int64"?d=s.from(t,BigInt):d=s.from(t)}else if(t instanceof s)d=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")d=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&s!==Uint16Array)d=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${o} tensor's data must be type of ${s}`)}else if(c=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let s=typeof e[0];if(s==="string")o="string",d=e;else if(s==="boolean")o="bool",d=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${s}.`)}else if(e instanceof Uint8ClampedArray)o="uint8",d=Uint8Array.from(e);else{let s=ci.get(e.constructor);if(s===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);o=s,d=e}if(c===void 0)c=[d.length];else if(!Array.isArray(c))throw new TypeError("A tensor's dims must be a number array");u=c,this.cpuData=d,this.dataLocation="cpu"}let l=Gy(u);if(this.cpuData&&l!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(l/2)===this.cpuData.length))throw new Error(`Tensor's size(${l}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=u,this.size=l}static async fromImage(e,t){return My(e,t)}static fromTexture(e,t){return Fy(e,t)}static fromGpuBuffer(e,t){return jy(e,t)}static fromMLTensor(e,t){return Ly(e,t)}static fromPinnedBuffer(e,t,n){return Uy(e,t,n)}toDataURL(e){return zy(this,e)}toImageData(e){return By(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return qy(this,e)}}}),mt,Hy=R(()=>{"use strict";xl(),mt=ft}),To,Ls,Gt,zt,tn,rn,Wy=R(()=>{"use strict";Ny(),To=(e,t)=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||console.timeStamp(`${e}::ORT::${t}`)},Ls=(e,t)=>{let n=new Error().stack?.split(/\r\n|\r|\n/g)||[],o=!1;for(let u=0;u{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||Ls("BEGIN",e)},zt=e=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||Ls("END",e)},tn=e=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||console.time(`ORT::${e}`)},rn=e=>{(typeof et.trace>"u"?!et.wasm.trace:!et.trace)||console.timeEnd(`ORT::${e}`)}}),Ky,QI=R(()=>{"use strict";Dy(),Hy(),Wy(),Ky=class Xy{constructor(t){this.handler=t}async run(t,n,o){Gt(),tn("InferenceSession.run");let u={},l={};if(typeof t!="object"||t===null||t instanceof mt||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof n=="object"){if(n===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(n instanceof mt)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(n)){if(n.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let r of n){if(typeof r!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(r)===-1)throw new RangeError(`'fetches' contains invalid output name: ${r}.`);u[r]=null}if(typeof o=="object"&&o!==null)l=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else{let r=!1,i=Object.getOwnPropertyNames(n);for(let a of this.outputNames)if(i.indexOf(a)!==-1){let p=n[a];(p===null||p instanceof mt)&&(r=!0,d=!1,u[a]=p)}if(r){if(typeof o=="object"&&o!==null)l=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else l=n}}else if(typeof n<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let r of this.inputNames)if(typeof t[r]>"u")throw new Error(`input '${r}' is missing in 'feeds'.`);if(d)for(let r of this.outputNames)u[r]=null;let c=await this.handler.run(t,u,l),s={};for(let r in c)if(Object.hasOwnProperty.call(c,r)){let i=c[r];i instanceof mt?s[r]=i:s[r]=new mt(i.type,i.data,i.dims)}return rn("InferenceSession.run"),zt(),s}async release(){return this.handler.dispose()}static async create(t,n,o,u){Gt(),tn("InferenceSession.create");let l,d={};if(typeof t=="string"){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let i=t,a=0,p=t.byteLength;if(typeof n=="object"&&n!==null)d=n;else if(typeof n=="number"){if(a=n,!Number.isSafeInteger(a))throw new RangeError("'byteOffset' must be an integer.");if(a<0||a>=i.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${i.byteLength}).`);if(p=t.byteLength-a,typeof o=="number"){if(p=o,!Number.isSafeInteger(p))throw new RangeError("'byteLength' must be an integer.");if(p<=0||a+p>i.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${i.byteLength-a}].`);if(typeof u=="object"&&u!==null)d=u;else if(typeof u<"u")throw new TypeError("'options' must be an object.")}else if(typeof o<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof n<"u")throw new TypeError("'options' must be an object.");l=new Uint8Array(i,a,p)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[c,s]=await Cy(d),r=await c.createInferenceSessionHandler(l,s);return rn("InferenceSession.create"),zt(),new Xy(r)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),Bo,e3=R(()=>{"use strict";QI(),Bo=Ky}),t3=R(()=>{"use strict"}),r3=R(()=>{"use strict"}),n3=R(()=>{"use strict"}),i3=R(()=>{"use strict"}),o3={};un(o3,{InferenceSession:()=>Bo,TRACE:()=>To,TRACE_EVENT_BEGIN:()=>tn,TRACE_EVENT_END:()=>rn,TRACE_FUNC_BEGIN:()=>Gt,TRACE_FUNC_END:()=>zt,Tensor:()=>mt,env:()=>le,registerBackend:()=>en});var ot=R(()=>{"use strict";HI(),KI(),e3(),Hy(),t3(),r3(),Wy(),n3(),i3()});function Zr(e,t,n,o){if(t===void 0)return s3(e);if(n===void 0)ro(e,t,1);else if(typeof n=="number"&&o===void 0)ro(e,t,n);else if(typeof n=="string"&&o===void 0)ro(e,n,1,t);else if(typeof n=="string"&&typeof o=="number")ro(e,n,o,t);else throw new TypeError("input is valid")}function s3(e){return{verbose:Zr.verbose.bind(null,e),info:Zr.info.bind(null,e),warning:Zr.warning.bind(null,e),error:Zr.error.bind(null,e),fatal:Zr.fatal.bind(null,e)}}function ro(e,t,n,o){let u=xn[o||""]||xn[""];bu[e]{"use strict";Ep=class{log(e,t,n){}},Ap=class{log(e,t,n){console.log(`${this.color(e)} ${n?"\x1B[35m"+n+"\x1B[0m ":""}${t}`)}color(e){switch(e){case"verbose":return"\x1B[34;40mv\x1B[0m";case"info":return"\x1B[32mi\x1B[0m";case"warning":return"\x1B[30;43mw\x1B[0m";case"error":return"\x1B[31;40me\x1B[0m";case"fatal":return"\x1B[101mf\x1B[0m";default:throw new Error(`unsupported severity: ${e}`)}}},bu={verbose:1e3,info:2e3,warning:4e3,error:5e3,fatal:6e3},Zy={none:new Ep,console:new Ap},Us={provider:"console",minimalSeverity:"warning",logDateTime:!0,logSourceLocation:!1},xn={"":Us},(e=>{function t(r,i){e("verbose",r,i)}e.verbose=t;function n(r,i){e("info",r,i)}e.info=n;function o(r,i){e("warning",r,i)}e.warning=o;function u(r,i){e("error",r,i)}e.error=u;function l(r,i){e("fatal",r,i)}e.fatal=l;function d(r){xn={},c("",r||{})}e.reset=d;function c(r,i){if(r==="*")d(i);else{let a=xn[r]||Us;xn[r]={provider:i.provider||a.provider,minimalSeverity:i.minimalSeverity||a.minimalSeverity,logDateTime:i.logDateTime===void 0?a.logDateTime:i.logDateTime,logSourceLocation:i.logSourceLocation===void 0?a.logSourceLocation:i.logSourceLocation}}}e.set=c;function s(r){let i={};r.logLevel&&(i.minimalSeverity=r.logLevel),c("",i)}e.setWithEnv=s})(Zr||={}),We=Zr,Vs=class{constructor(e,t,n,o,u,l){this.category=e,this.name=t,this.startTime=n,this.endCallback=o,this.timer=u,this.ctx=l}async end(){return this.endCallback(this)}async checkTimer(){if(this.ctx===void 0||this.timer===void 0)throw new Error("No webgl timer found");return this.ctx.endTimer(),this.ctx.waitForQueryAndGetTime(this.timer)}},Gs=class{constructor(e,t,n,o){this.category=e,this.name=t,this.startTime=n,this.endTime=o}},Jy=class{constructor(e,t,n){this._started=!1,this._flushPointer=0,this._started=!1,this._maxNumberEvents=e===void 0?1e4:e,this._flushBatchSize=t===void 0?10:t,this._flushIntervalInMilliseconds=n===void 0?5e3:n}static create(e){return e===void 0?new this:new this(e.maxNumberEvents,e.flushBatchSize,e.flushIntervalInMilliseconds)}start(){this._started=!0,this._timingEvents=[],this._flushTime=Xn(),this._flushPointer=0}stop(){for(this._started=!1;this._flushPointer{d.then(async r=>{u&&await u.end(),c(r)},async r=>{u&&await u.end(),s(r)})});if(!l&&u){let c=u.end();if(c&&typeof c.then=="function")return new Promise((s,r)=>{c.then(()=>{s(d)},i=>{r(i)})})}return d}begin(e,t,n){if(!this._started)throw new Error("profiler is not started yet");if(n===void 0){let o=Xn();return this.flush(o),new Vs(e,t,o,u=>this.endSync(u))}else{let o=n.beginTimer();return new Vs(e,t,0,async u=>this.end(u),o,n)}}async end(e){let t=await e.checkTimer();this._timingEvents.length=this._flushBatchSize||e-this._flushTime>=this._flushIntervalInMilliseconds){for(let t=this._flushPointer;this._flushPointerperformance.now():Date.now});function a3(e,t,n){for(let o of n){let u=o[0],l=o[1],d=o[2],c=o[3],s=o[4];if(e.opType===u){for(let r of t)if((r.domain===l||r.domain==="ai.onnx"&&l==="")&&u3(r.version,d))return{opImpl:c,opInit:s}}}throw new TypeError(`cannot resolve operator '${e.opType}' with opsets: ${t.map(o=>`${o.domain||"ai.onnx"} v${o.version}`).join(", ")}`)}function u3(e,t){if(t.endsWith("+")){let n=Number.parseInt(t.substring(0,t.length-1),10);return!isNaN(n)&&n<=e}else if(t.split("-").length===2){let n=t.split("-"),o=Number.parseInt(n[0],10),u=Number.parseInt(n[1],10);return!isNaN(o)&&!isNaN(u)&&o<=e&&e<=u}else return Number.parseInt(t,10)===e}var l3=R(()=>{"use strict"}),d3=ie(e=>{"use strict";e.__esModule=!0;var t=(function(){function n(o){if(!o)throw new TypeError("Invalid argument; `value` has no value.");this.value=n.EMPTY,o&&n.isGuid(o)&&(this.value=o)}return n.isGuid=function(o){var u=o.toString();return o&&(o instanceof n||n.validator.test(u))},n.create=function(){return new n([n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-"))},n.createEmpty=function(){return new n("emptyguid")},n.parse=function(o){return new n(o)},n.raw=function(){return[n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-")},n.gen=function(o){for(var u="",l=0;l>>=0,(u=0<=e&&e<256)&&(o=wu[e],o)?o:(n=Ee(e,0,!0),u&&(wu[e]=n),n)):(e|=0,(u=-128<=e&&e<128)&&(o=yu[e],o)?o:(n=Ee(e,e<0?-1:0,!1),u&&(yu[e]=n),n))}function It(e,t){if(isNaN(e))return t?Ut:St;if(t){if(e<0)return Ut;if(e>=_u)return $u}else{if(e<=-vu)return at;if(e+1>=vu)return xu}return e<0?It(-e,t).neg():Ee(e%Jr|0,e/Jr|0,t)}function Ee(e,t,n){return new De(e,t,n)}function $l(e,t,n){if(e.length===0)throw Error("empty string");if(typeof t=="number"?(n=t,t=!1):t=!!t,e==="NaN"||e==="Infinity"||e==="+Infinity"||e==="-Infinity")return t?Ut:St;if(n=n||10,n<2||360)throw Error("interior hyphen");if(o===0)return $l(e.substring(1),t,n).neg();for(var u=It(wi(n,8)),l=St,d=0;d{dt=null;try{dt=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}De.prototype.__isLong__,Object.defineProperty(De.prototype,"__isLong__",{value:!0}),De.isLong=it,yu={},wu={},De.fromInt=Pr,De.fromNumber=It,De.fromBits=Ee,wi=Math.pow,De.fromString=$l,De.fromValue=vt,qs=65536,Pp=1<<24,Jr=qs*qs,_u=Jr*Jr,vu=_u/2,Hs=Pr(Pp),St=Pr(0),De.ZERO=St,Ut=Pr(0,!0),De.UZERO=Ut,Cr=Pr(1),De.ONE=Cr,Ws=Pr(1,!0),De.UONE=Ws,no=Pr(-1),De.NEG_ONE=no,xu=Ee(-1,2147483647,!1),De.MAX_VALUE=xu,$u=Ee(-1,-1,!0),De.MAX_UNSIGNED_VALUE=$u,at=Ee(0,-2147483648,!1),De.MIN_VALUE=at,K=De.prototype,K.toInt=function(){return this.unsigned?this.low>>>0:this.low},K.toNumber=function(){return this.unsigned?(this.high>>>0)*Jr+(this.low>>>0):this.high*Jr+(this.low>>>0)},K.toString=function(e){if(e=e||10,e<2||36>>0,r=s.toString(e);if(l=c,l.isZero())return r+d;for(;r.length<6;)r="0"+r;d=""+r+d}},K.getHighBits=function(){return this.high},K.getHighBitsUnsigned=function(){return this.high>>>0},K.getLowBits=function(){return this.low},K.getLowBitsUnsigned=function(){return this.low>>>0},K.getNumBitsAbs=function(){if(this.isNegative())return this.eq(at)?64:this.neg().getNumBitsAbs();for(var e=this.high!=0?this.high:this.low,t=31;t>0&&(e&1<>21;return e?this.unsigned?!1:e===-1&&!(this.low===0&&this.high===-2097152):!0},K.isZero=function(){return this.high===0&&this.low===0},K.eqz=K.isZero,K.isNegative=function(){return!this.unsigned&&this.high<0},K.isPositive=function(){return this.unsigned||this.high>=0},K.isOdd=function(){return(this.low&1)===1},K.isEven=function(){return(this.low&1)===0},K.equals=function(e){return it(e)||(e=vt(e)),this.unsigned!==e.unsigned&&this.high>>>31===1&&e.high>>>31===1?!1:this.high===e.high&&this.low===e.low},K.eq=K.equals,K.notEquals=function(e){return!this.eq(e)},K.neq=K.notEquals,K.ne=K.notEquals,K.lessThan=function(e){return this.comp(e)<0},K.lt=K.lessThan,K.lessThanOrEqual=function(e){return this.comp(e)<=0},K.lte=K.lessThanOrEqual,K.le=K.lessThanOrEqual,K.greaterThan=function(e){return this.comp(e)>0},K.gt=K.greaterThan,K.greaterThanOrEqual=function(e){return this.comp(e)>=0},K.gte=K.greaterThanOrEqual,K.ge=K.greaterThanOrEqual,K.compare=function(e){if(it(e)||(e=vt(e)),this.eq(e))return 0;var t=this.isNegative(),n=e.isNegative();return t&&!n?-1:!t&&n?1:this.unsigned?e.high>>>0>this.high>>>0||e.high===this.high&&e.low>>>0>this.low>>>0?-1:1:this.sub(e).isNegative()?-1:1},K.comp=K.compare,K.negate=function(){return!this.unsigned&&this.eq(at)?at:this.not().add(Cr)},K.neg=K.negate,K.add=function(e){it(e)||(e=vt(e));var t=this.high>>>16,n=this.high&65535,o=this.low>>>16,u=this.low&65535,l=e.high>>>16,d=e.high&65535,c=e.low>>>16,s=e.low&65535,r=0,i=0,a=0,p=0;return p+=u+s,a+=p>>>16,p&=65535,a+=o+c,i+=a>>>16,a&=65535,i+=n+d,r+=i>>>16,i&=65535,r+=t+l,r&=65535,Ee(a<<16|p,r<<16|i,this.unsigned)},K.subtract=function(e){return it(e)||(e=vt(e)),this.add(e.neg())},K.sub=K.subtract,K.multiply=function(e){if(this.isZero())return this;if(it(e)||(e=vt(e)),dt){var t=dt.mul(this.low,this.high,e.low,e.high);return Ee(t,dt.get_high(),this.unsigned)}if(e.isZero())return this.unsigned?Ut:St;if(this.eq(at))return e.isOdd()?at:St;if(e.eq(at))return this.isOdd()?at:St;if(this.isNegative())return e.isNegative()?this.neg().mul(e.neg()):this.neg().mul(e).neg();if(e.isNegative())return this.mul(e.neg()).neg();if(this.lt(Hs)&&e.lt(Hs))return It(this.toNumber()*e.toNumber(),this.unsigned);var n=this.high>>>16,o=this.high&65535,u=this.low>>>16,l=this.low&65535,d=e.high>>>16,c=e.high&65535,s=e.low>>>16,r=e.low&65535,i=0,a=0,p=0,h=0;return h+=l*r,p+=h>>>16,h&=65535,p+=u*r,a+=p>>>16,p&=65535,p+=l*s,a+=p>>>16,p&=65535,a+=o*r,i+=a>>>16,a&=65535,a+=u*s,i+=a>>>16,a&=65535,a+=l*c,i+=a>>>16,a&=65535,i+=n*r+o*s+u*c+l*d,i&=65535,Ee(p<<16|h,i<<16|a,this.unsigned)},K.mul=K.multiply,K.divide=function(e){if(it(e)||(e=vt(e)),e.isZero())throw Error("division by zero");if(dt){if(!this.unsigned&&this.high===-2147483648&&e.low===-1&&e.high===-1)return this;var t=(this.unsigned?dt.div_u:dt.div_s)(this.low,this.high,e.low,e.high);return Ee(t,dt.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?Ut:St;var n,o,u;if(this.unsigned){if(e.unsigned||(e=e.toUnsigned()),e.gt(this))return Ut;if(e.gt(this.shru(1)))return Ws;u=Ut}else{if(this.eq(at)){if(e.eq(Cr)||e.eq(no))return at;if(e.eq(at))return Cr;var l=this.shr(1);return n=l.div(e).shl(1),n.eq(St)?e.isNegative()?Cr:no:(o=this.sub(e.mul(n)),u=n.add(o.div(e)),u)}else if(e.eq(at))return this.unsigned?Ut:St;if(this.isNegative())return e.isNegative()?this.neg().div(e.neg()):this.neg().div(e).neg();if(e.isNegative())return this.div(e.neg()).neg();u=St}for(o=this;o.gte(e);){n=Math.max(1,Math.floor(o.toNumber()/e.toNumber()));for(var d=Math.ceil(Math.log(n)/Math.LN2),c=d<=48?1:wi(2,d-48),s=It(n),r=s.mul(e);r.isNegative()||r.gt(o);)n-=c,s=It(n,this.unsigned),r=s.mul(e);s.isZero()&&(s=Cr),u=u.add(s),o=o.sub(r)}return u},K.div=K.divide,K.modulo=function(e){if(it(e)||(e=vt(e)),dt){var t=(this.unsigned?dt.rem_u:dt.rem_s)(this.low,this.high,e.low,e.high);return Ee(t,dt.get_high(),this.unsigned)}return this.sub(this.div(e).mul(e))},K.mod=K.modulo,K.rem=K.modulo,K.not=function(){return Ee(~this.low,~this.high,this.unsigned)},K.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},K.clz=K.countLeadingZeros,K.countTrailingZeros=function(){return this.low?kp(this.low):kp(this.high)+32},K.ctz=K.countTrailingZeros,K.and=function(e){return it(e)||(e=vt(e)),Ee(this.low&e.low,this.high&e.high,this.unsigned)},K.or=function(e){return it(e)||(e=vt(e)),Ee(this.low|e.low,this.high|e.high,this.unsigned)},K.xor=function(e){return it(e)||(e=vt(e)),Ee(this.low^e.low,this.high^e.high,this.unsigned)},K.shiftLeft=function(e){return it(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ee(this.low<>>32-e,this.unsigned):Ee(0,this.low<>>e|this.high<<32-e,this.high>>e,this.unsigned):Ee(this.high>>e-32,this.high>=0?0:-1,this.unsigned)},K.shr=K.shiftRight,K.shiftRightUnsigned=function(e){return it(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ee(this.low>>>e|this.high<<32-e,this.high>>>e,this.unsigned):e===32?Ee(this.high,0,this.unsigned):Ee(this.high>>>e-32,0,this.unsigned)},K.shru=K.shiftRightUnsigned,K.shr_u=K.shiftRightUnsigned,K.rotateLeft=function(e){var t;return it(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ee(this.high,this.low,this.unsigned):e<32?(t=32-e,Ee(this.low<>>t,this.high<>>t,this.unsigned)):(e-=32,t=32-e,Ee(this.high<>>t,this.low<>>t,this.unsigned))},K.rotl=K.rotateLeft,K.rotateRight=function(e){var t;return it(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ee(this.high,this.low,this.unsigned):e<32?(t=32-e,Ee(this.high<>>e,this.low<>>e,this.unsigned)):(e-=32,t=32-e,Ee(this.low<>>e,this.high<>>e,this.unsigned))},K.rotr=K.rotateRight,K.toSigned=function(){return this.unsigned?Ee(this.low,this.high,!1):this},K.toUnsigned=function(){return this.unsigned?this:Ee(this.low,this.high,!0)},K.toBytes=function(e){return e?this.toBytesLE():this.toBytesBE()},K.toBytesLE=function(){var e=this.high,t=this.low;return[t&255,t>>>8&255,t>>>16&255,t>>>24,e&255,e>>>8&255,e>>>16&255,e>>>24]},K.toBytesBE=function(){var e=this.high,t=this.low;return[e>>>24,e>>>16&255,e>>>8&255,e&255,t>>>24,t>>>16&255,t>>>8&255,t&255]},De.fromBytes=function(e,t,n){return n?De.fromBytesLE(e,t):De.fromBytesBE(e,t)},De.fromBytesLE=function(e,t){return new De(e[0]|e[1]<<8|e[2]<<16|e[3]<<24,e[4]|e[5]<<8|e[6]<<16|e[7]<<24,t)},De.fromBytesBE=function(e,t){return new De(e[4]<<24|e[5]<<16|e[6]<<8|e[7],e[0]<<24|e[1]<<16|e[2]<<8|e[3],t)},typeof BigInt=="function"&&(De.fromBigInt=function(e,t){var n=Number(BigInt.asIntN(32,e)),o=Number(BigInt.asIntN(32,e>>BigInt(32)));return Ee(n,o,t)},De.fromValue=function(e,t){return typeof e=="bigint"?De.fromBigInt(e,t):vt(e,t)},K.toBigInt=function(){var e=BigInt(this.low>>>0),t=BigInt(this.unsigned?this.high>>>0:this.high);return t<{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ArgType=void 0;var t;(function(n){n[n.INPUT=0]="INPUT",n[n.OUTPUT=1]="OUTPUT"})(t||(e.ArgType=t={}))}),Tl=ie(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.SIZE_PREFIX_LENGTH=e.FILE_IDENTIFIER_LENGTH=e.SIZEOF_INT=e.SIZEOF_SHORT=void 0,e.SIZEOF_SHORT=2,e.SIZEOF_INT=4,e.FILE_IDENTIFIER_LENGTH=4,e.SIZE_PREFIX_LENGTH=4}),e0=ie(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.isLittleEndian=e.float64=e.float32=e.int32=void 0,e.int32=new Int32Array(2),e.float32=new Float32Array(e.int32.buffer),e.float64=new Float64Array(e.int32.buffer),e.isLittleEndian=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1}),t0=ie(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=void 0;var t;(function(n){n[n.UTF8_BYTES=1]="UTF8_BYTES",n[n.UTF16_STRING=2]="UTF16_STRING"})(t||(e.Encoding=t={}))}),r0=ie(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ByteBuffer=void 0;var t=Tl(),n=t0(),o=e0(),u=class n0{constructor(d){this.bytes_=d,this.position_=0,this.text_decoder_=new TextDecoder}static allocate(d){return new n0(new Uint8Array(d))}clear(){this.position_=0}bytes(){return this.bytes_}position(){return this.position_}setPosition(d){this.position_=d}capacity(){return this.bytes_.length}readInt8(d){return this.readUint8(d)<<24>>24}readUint8(d){return this.bytes_[d]}readInt16(d){return this.readUint16(d)<<16>>16}readUint16(d){return this.bytes_[d]|this.bytes_[d+1]<<8}readInt32(d){return this.bytes_[d]|this.bytes_[d+1]<<8|this.bytes_[d+2]<<16|this.bytes_[d+3]<<24}readUint32(d){return this.readInt32(d)>>>0}readInt64(d){return BigInt.asIntN(64,BigInt(this.readUint32(d))+(BigInt(this.readUint32(d+4))<>8}writeUint16(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8}writeInt32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeUint32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeInt64(d,c){this.writeInt32(d,Number(BigInt.asIntN(32,c))),this.writeInt32(d+4,Number(BigInt.asIntN(32,c>>BigInt(32))))}writeUint64(d,c){this.writeUint32(d,Number(BigInt.asUintN(32,c))),this.writeUint32(d+4,Number(BigInt.asUintN(32,c>>BigInt(32))))}writeFloat32(d,c){o.float32[0]=c,this.writeInt32(d,o.int32[0])}writeFloat64(d,c){o.float64[0]=c,this.writeInt32(d,o.int32[o.isLittleEndian?0:1]),this.writeInt32(d+4,o.int32[o.isLittleEndian?1:0])}getBufferIdentifier(){if(this.bytes_.length{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Builder=void 0;var t=r0(),n=Tl(),o=class i0{constructor(l){this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null,this.text_encoder=new TextEncoder;let d;l?d=l:d=1024,this.bb=t.ByteBuffer.allocate(d),this.space=d}clear(){this.bb.clear(),this.space=this.bb.capacity(),this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null}forceDefaults(l){this.force_defaults=l}dataBuffer(){return this.bb}asUint8Array(){return this.bb.bytes().subarray(this.bb.position(),this.bb.position()+this.offset())}prep(l,d){l>this.minalign&&(this.minalign=l);let c=~(this.bb.capacity()-this.space+d)+1&l-1;for(;this.space=0&&this.vtable[d]==0;d--);let c=d+1;for(;d>=0;d--)this.addInt16(this.vtable[d]!=0?l-this.vtable[d]:0);let s=2;this.addInt16(l-this.object_start);let r=(c+s)*n.SIZEOF_SHORT;this.addInt16(r);let i=0,a=this.space;e:for(d=0;d=0;i--)this.writeInt8(r.charCodeAt(i))}this.prep(this.minalign,n.SIZEOF_INT+s),this.addOffset(l),s&&this.addInt32(this.bb.capacity()-this.space),this.bb.setPosition(this.space)}finishSizePrefixed(l,d){this.finish(l,d,!0)}requiredField(l,d){let c=this.bb.capacity()-l,s=c-this.bb.readInt32(c);if(!(d{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=e.ByteBuffer=e.Builder=e.isLittleEndian=e.int32=e.float64=e.float32=e.SIZE_PREFIX_LENGTH=e.SIZEOF_SHORT=e.SIZEOF_INT=e.FILE_IDENTIFIER_LENGTH=void 0;var t=Tl();Object.defineProperty(e,"FILE_IDENTIFIER_LENGTH",{enumerable:!0,get:function(){return t.FILE_IDENTIFIER_LENGTH}}),Object.defineProperty(e,"SIZEOF_INT",{enumerable:!0,get:function(){return t.SIZEOF_INT}}),Object.defineProperty(e,"SIZEOF_SHORT",{enumerable:!0,get:function(){return t.SIZEOF_SHORT}}),Object.defineProperty(e,"SIZE_PREFIX_LENGTH",{enumerable:!0,get:function(){return t.SIZE_PREFIX_LENGTH}});var n=e0();Object.defineProperty(e,"float32",{enumerable:!0,get:function(){return n.float32}}),Object.defineProperty(e,"float64",{enumerable:!0,get:function(){return n.float64}}),Object.defineProperty(e,"int32",{enumerable:!0,get:function(){return n.int32}}),Object.defineProperty(e,"isLittleEndian",{enumerable:!0,get:function(){return n.isLittleEndian}});var o=c3();Object.defineProperty(e,"Builder",{enumerable:!0,get:function(){return o.Builder}});var u=r0();Object.defineProperty(e,"ByteBuffer",{enumerable:!0,get:function(){return u.ByteBuffer}});var l=t0();Object.defineProperty(e,"Encoding",{enumerable:!0,get:function(){return l.Encoding}})}),o0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.AttributeType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.INT=2]="INT",n[n.STRING=3]="STRING",n[n.TENSOR=4]="TENSOR",n[n.GRAPH=5]="GRAPH",n[n.FLOATS=6]="FLOATS",n[n.INTS=7]="INTS",n[n.STRINGS=8]="STRINGS",n[n.TENSORS=9]="TENSORS",n[n.GRAPHS=10]="GRAPHS",n[n.SPARSE_TENSOR=11]="SPARSE_TENSOR",n[n.SPARSE_TENSORS=12]="SPARSE_TENSORS"})(t||(e.AttributeType=t={}))}),a0=ie(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=void 0;var t;(function(n){n[n.Primitive=0]="Primitive",n[n.Fused=1]="Fused"})(t||(e.NodeType=t={}))}),u0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(i[a]);return r.endVector()}static startInputsVector(r,i){r.startVector(4,i,4)}static addOutputs(r,i){r.addFieldOffset(9,i,0)}static createOutputsVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addOffset(i[a]);return r.endVector()}static startOutputsVector(r,i){r.startVector(4,i,4)}static addAttributes(r,i){r.addFieldOffset(10,i,0)}static createAttributesVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addOffset(i[a]);return r.endVector()}static startAttributesVector(r,i){r.startVector(4,i,4)}static addInputArgCounts(r,i){r.addFieldOffset(11,i,0)}static createInputArgCountsVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addInt32(i[a]);return r.endVector()}static startInputArgCountsVector(r,i){r.startVector(4,i,4)}static addImplicitInputs(r,i){r.addFieldOffset(12,i,0)}static createImplicitInputsVector(r,i){r.startVector(4,i.length,4);for(let a=i.length-1;a>=0;a--)r.addOffset(i[a]);return r.endVector()}static startImplicitInputsVector(r,i){r.startVector(4,i,4)}static endNode(r){return r.endObject()}static createNode(r,i,a,p,h,m,b,x,w,y,S,T,I,A){return Qe.startNode(r),Qe.addName(r,i),Qe.addDocString(r,a),Qe.addDomain(r,p),Qe.addSinceVersion(r,h),Qe.addIndex(r,m),Qe.addOpType(r,b),Qe.addType(r,x),Qe.addExecutionProviderType(r,w),Qe.addInputs(r,y),Qe.addOutputs(r,S),Qe.addAttributes(r,T),Qe.addInputArgCounts(r,I),Qe.addImplicitInputs(r,A),Qe.endNode(r)}};e.Node=c}),l0=ie(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.EdgeEnd=void 0;var t=class{constructor(){this.bb=null,this.bb_pos=0}__init(n,o){return this.bb_pos=n,this.bb=o,this}nodeIndex(){return this.bb.readUint32(this.bb_pos)}srcArgIndex(){return this.bb.readInt32(this.bb_pos+4)}dstArgIndex(){return this.bb.readInt32(this.bb_pos+8)}static sizeOf(){return 12}static createEdgeEnd(n,o,u,l){return n.prep(4,12),n.writeInt32(l),n.writeInt32(u),n.writeInt32(o),n.offset()}};e.EdgeEnd=t}),d0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addNumInputs(c,s){c.addFieldInt32(1,s,0)}static addNumOutputs(c,s){c.addFieldInt32(2,s,0)}static addHasVariadicInput(c,s){c.addFieldInt8(3,+s,0)}static addHasVariadicOutput(c,s){c.addFieldInt8(4,+s,0)}static addNumVariadicInputs(c,s){c.addFieldInt32(5,s,0)}static addNumVariadicOutputs(c,s){c.addFieldInt32(6,s,0)}static endNodesToOptimizeIndices(c){return c.endObject()}static createNodesToOptimizeIndices(c,s,r,i,a,p,h,m){return $t.startNodesToOptimizeIndices(c),$t.addNodeIndices(c,s),$t.addNumInputs(c,r),$t.addNumOutputs(c,i),$t.addHasVariadicInput(c,a),$t.addHasVariadicOutput(c,p),$t.addNumVariadicInputs(c,h),$t.addNumVariadicOutputs(c,m),$t.endNodesToOptimizeIndices(c)}};e.NodesToOptimizeIndices=l}),p0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startProducedOpIdsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecord(s){return s.endObject()}};e.RuntimeOptimizationRecord=d}),h0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startRuntimeOptimizationRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecordContainerEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createRuntimeOptimizationRecordContainerEntry(s,r,i){return Fr.startRuntimeOptimizationRecordContainerEntry(s),Fr.addOptimizerName(s,r),Fr.addRuntimeOptimizationRecords(s,i),Fr.endRuntimeOptimizationRecordContainerEntry(s)}};e.RuntimeOptimizationRecordContainerEntry=d}),f0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizations(s){return s.endObject()}static createRuntimeOptimizations(s,r){return yn.startRuntimeOptimizations(s),yn.addRecords(s,r),yn.endRuntimeOptimizations(s)}};e.RuntimeOptimizations=d}),Mo=ie(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TensorDataType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.UINT8=2]="UINT8",n[n.INT8=3]="INT8",n[n.UINT16=4]="UINT16",n[n.INT16=5]="INT16",n[n.INT32=6]="INT32",n[n.INT64=7]="INT64",n[n.STRING=8]="STRING",n[n.BOOL=9]="BOOL",n[n.FLOAT16=10]="FLOAT16",n[n.DOUBLE=11]="DOUBLE",n[n.UINT32=12]="UINT32",n[n.UINT64=13]="UINT64",n[n.COMPLEX64=14]="COMPLEX64",n[n.COMPLEX128=15]="COMPLEX128",n[n.BFLOAT16=16]="BFLOAT16",n[n.FLOAT8E4M3FN=17]="FLOAT8E4M3FN",n[n.FLOAT8E4M3FNUZ=18]="FLOAT8E4M3FNUZ",n[n.FLOAT8E5M2=19]="FLOAT8E5M2",n[n.FLOAT8E5M2FNUZ=20]="FLOAT8E5M2FNUZ"})(t||(e.TensorDataType=t={}))}),Fo=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addInt64(r[i]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static addDataType(s,r){s.addFieldInt32(3,r,l.TensorDataType.UNDEFINED)}static addRawData(s,r){s.addFieldOffset(4,r,0)}static createRawDataVector(s,r){s.startVector(1,r.length,1);for(let i=r.length-1;i>=0;i--)s.addInt8(r[i]);return s.endVector()}static startRawDataVector(s,r){s.startVector(1,r,1)}static addStringData(s,r){s.addFieldOffset(5,r,0)}static createStringDataVector(s,r){s.startVector(4,r.length,4);for(let i=r.length-1;i>=0;i--)s.addOffset(r[i]);return s.endVector()}static startStringDataVector(s,r){s.startVector(4,r,4)}static addExternalDataOffset(s,r){s.addFieldInt64(6,r,BigInt("-1"))}static endTensor(s){return s.endObject()}static createTensor(s,r,i,a,p,h,m,b){return Tt.startTensor(s),Tt.addName(s,r),Tt.addDocString(s,i),Tt.addDims(s,a),Tt.addDataType(s,p),Tt.addRawData(s,h),Tt.addStringData(s,m),Tt.addExternalDataOffset(s,b),Tt.endTensor(s)}};e.Tensor=d}),m0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addInt64(r[i]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static endSparseTensor(s){return s.endObject()}};e.SparseTensor=d}),g0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.DimensionValueType=void 0;var t;(function(n){n[n.UNKNOWN=0]="UNKNOWN",n[n.VALUE=1]="VALUE",n[n.PARAM=2]="PARAM"})(t||(e.DimensionValueType=t={}))}),w0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startDimVector(s,r){s.startVector(4,r,4)}static endShape(s){return s.endObject()}static createShape(s,r){return _n.startShape(s),_n.addDim(s,r),_n.endShape(s)}};e.Shape=d}),x0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TypeInfoValue=void 0,e.unionToTypeInfoValue=l,e.unionListToTypeInfoValue=d;var t=g0(),n=b0(),o=x0(),u;(function(c){c[c.NONE=0]="NONE",c[c.tensor_type=1]="tensor_type",c[c.sequence_type=2]="sequence_type",c[c.map_type=3]="map_type"})(u||(e.TypeInfoValue=u={}));function l(c,s){switch(u[c]){case"NONE":return null;case"tensor_type":return s(new o.TensorTypeAndShape);case"sequence_type":return s(new n.SequenceType);case"map_type":return s(new t.MapType);default:return null}}function d(c,s,r){switch(u[c]){case"NONE":return null;case"tensor_type":return s(r,new o.TensorTypeAndShape);case"sequence_type":return s(r,new n.SequenceType);case"map_type":return s(r,new t.MapType);default:return null}}}),jo=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(p,h,m,b){b===void 0&&(b=m);var x=Object.getOwnPropertyDescriptor(h,m);(!x||("get"in x?!h.__esModule:x.writable||x.configurable))&&(x={enumerable:!0,get:function(){return h[m]}}),Object.defineProperty(p,b,x)}:function(p,h,m,b){b===void 0&&(b=m),p[b]=h[m]}),n=e&&e.__setModuleDefault||(Object.create?function(p,h){Object.defineProperty(p,"default",{enumerable:!0,value:h})}:function(p,h){p.default=h}),o=e&&e.__importStar||(function(){var p=function(h){return p=Object.getOwnPropertyNames||function(m){var b=[];for(var x in m)Object.prototype.hasOwnProperty.call(m,x)&&(b[b.length]=x);return b},p(h)};return function(h){if(h&&h.__esModule)return h;var m={};if(h!=null)for(var b=p(h),x=0;x=0;b--)h.addOffset(m[b]);return h.endVector()}static startInitializersVector(h,m){h.startVector(4,m,4)}static addNodeArgs(h,m){h.addFieldOffset(1,m,0)}static createNodeArgsVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startNodeArgsVector(h,m){h.startVector(4,m,4)}static addNodes(h,m){h.addFieldOffset(2,m,0)}static createNodesVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startNodesVector(h,m){h.startVector(4,m,4)}static addMaxNodeIndex(h,m){h.addFieldInt32(3,m,0)}static addNodeEdges(h,m){h.addFieldOffset(4,m,0)}static createNodeEdgesVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startNodeEdgesVector(h,m){h.startVector(4,m,4)}static addInputs(h,m){h.addFieldOffset(5,m,0)}static createInputsVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startInputsVector(h,m){h.startVector(4,m,4)}static addOutputs(h,m){h.addFieldOffset(6,m,0)}static createOutputsVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startOutputsVector(h,m){h.startVector(4,m,4)}static addSparseInitializers(h,m){h.addFieldOffset(7,m,0)}static createSparseInitializersVector(h,m){h.startVector(4,m.length,4);for(let b=m.length-1;b>=0;b--)h.addOffset(m[b]);return h.endVector()}static startSparseInitializersVector(h,m){h.startVector(4,m,4)}static addRuntimeOptimizations(h,m){h.addFieldOffset(8,m,0)}static endGraph(h){return h.endObject()}};e.Graph=a}),S0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,i,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(i,a);(!h||("get"in h?!i.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return i[a]}}),Object.defineProperty(r,p,h)}:function(r,i,a,p){p===void 0&&(p=a),r[p]=i[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,i){Object.defineProperty(r,"default",{enumerable:!0,value:i})}:function(r,i){r.default=i}),o=e&&e.__importStar||(function(){var r=function(i){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(i)};return function(i){if(i&&i.__esModule)return i;var a={};if(i!=null)for(var p=r(i),h=0;h=0;p--)i.addFloat32(a[p]);return i.endVector()}static startFloatsVector(i,a){i.startVector(4,a,4)}static addInts(i,a){i.addFieldOffset(9,a,0)}static createIntsVector(i,a){i.startVector(8,a.length,8);for(let p=a.length-1;p>=0;p--)i.addInt64(a[p]);return i.endVector()}static startIntsVector(i,a){i.startVector(8,a,8)}static addStrings(i,a){i.addFieldOffset(10,a,0)}static createStringsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startStringsVector(i,a){i.startVector(4,a,4)}static addTensors(i,a){i.addFieldOffset(11,a,0)}static createTensorsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startTensorsVector(i,a){i.startVector(4,a,4)}static addGraphs(i,a){i.addFieldOffset(12,a,0)}static createGraphsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startGraphsVector(i,a){i.startVector(4,a,4)}static endAttribute(i){return i.endObject()}};e.Attribute=s}),I0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addKernelDefHashes(c,s){c.addFieldOffset(1,s,0)}static createKernelDefHashesVector(c,s){c.startVector(8,s.length,8);for(let r=s.length-1;r>=0;r--)c.addInt64(s[r]);return c.endVector()}static startKernelDefHashesVector(c,s){c.startVector(8,s,8)}static endDeprecatedKernelCreateInfos(c){return c.endObject()}static createDeprecatedKernelCreateInfos(c,s,r){return Lr.startDeprecatedKernelCreateInfos(c),Lr.addNodeIndices(c,s),Lr.addKernelDefHashes(c,r),Lr.endDeprecatedKernelCreateInfos(c)}};e.DeprecatedKernelCreateInfos=l}),p3=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(i[a]);return r.endVector()}static startSubGraphSessionStatesVector(r,i){r.startVector(4,i,4)}static endDeprecatedSessionState(r){return r.endObject()}static createDeprecatedSessionState(r,i,a){return Vr.startDeprecatedSessionState(r),Vr.addKernels(r,i),Vr.addSubGraphSessionStates(r,a),Vr.endDeprecatedSessionState(r)}};e.DeprecatedSessionState=c}),A0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createKernelTypeStrArgsEntry(s,r,i){return Gr.startKernelTypeStrArgsEntry(s),Gr.addKernelTypeStr(s,r),Gr.addArgs(s,i),Gr.endKernelTypeStrArgsEntry(s)}};e.KernelTypeStrArgsEntry=d}),k0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endOpIdKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createOpIdKernelTypeStrArgsEntry(s,r,i){return qr.startOpIdKernelTypeStrArgsEntry(s),qr.addOpId(s,r),qr.addKernelTypeStrArgs(s,i),qr.endOpIdKernelTypeStrArgsEntry(s)}};e.OpIdKernelTypeStrArgsEntry=d}),P0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,i){i===void 0&&(i=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,i,a)}:function(c,s,r,i){i===void 0&&(i=r),c[i]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),o=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var i=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[i.length]=a);return i},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var i=c(s),a=0;a=0;i--)s.addOffset(r[i]);return s.endVector()}static startOpKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrResolver(s){return s.endObject()}static createKernelTypeStrResolver(s,r){return vn.startKernelTypeStrResolver(s),vn.addOpKernelTypeStrArgs(s,r),vn.endKernelTypeStrResolver(s)}};e.KernelTypeStrResolver=d}),C0=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var i=Object.getOwnPropertyDescriptor(c,s);(!i||("get"in i?!c.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,i)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),o=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[r.length]=i);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),i=0;i{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,i,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(i,a);(!h||("get"in h?!i.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return i[a]}}),Object.defineProperty(r,p,h)}:function(r,i,a,p){p===void 0&&(p=a),r[p]=i[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,i){Object.defineProperty(r,"default",{enumerable:!0,value:i})}:function(r,i){r.default=i}),o=e&&e.__importStar||(function(){var r=function(i){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(i)};return function(i){if(i&&i.__esModule)return i;var a={};if(i!=null)for(var p=r(i),h=0;h=0;p--)i.addOffset(a[p]);return i.endVector()}static startOpsetImportVector(i,a){i.startVector(4,a,4)}static addProducerName(i,a){i.addFieldOffset(2,a,0)}static addProducerVersion(i,a){i.addFieldOffset(3,a,0)}static addDomain(i,a){i.addFieldOffset(4,a,0)}static addModelVersion(i,a){i.addFieldInt64(5,a,BigInt("0"))}static addDocString(i,a){i.addFieldOffset(6,a,0)}static addGraph(i,a){i.addFieldOffset(7,a,0)}static addGraphDocString(i,a){i.addFieldOffset(8,a,0)}static addMetadataProps(i,a){i.addFieldOffset(9,a,0)}static createMetadataPropsVector(i,a){i.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)i.addOffset(a[p]);return i.endVector()}static startMetadataPropsVector(i,a){i.startVector(4,a,4)}static endModel(i){return i.endObject()}};e.Model=s}),h3=ie(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,i,a){a===void 0&&(a=i);var p=Object.getOwnPropertyDescriptor(r,i);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[i]}}),Object.defineProperty(s,a,p)}:function(s,r,i,a){a===void 0&&(a=i),s[a]=r[i]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),o=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(i){var a=[];for(var p in i)Object.prototype.hasOwnProperty.call(i,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var i={};if(r!=null)for(var a=s(r),p=0;p{"use strict";f3=ce(Qy()),m3=ce(o0()),Ru=ce(S0()),Rt=ce(s0()),g3=ce(I0()),b3=ce(p3()),y3=ce(E0()),w3=ce(O0()),_3=ce(_0()),v3=ce(w0()),x3=ce(y0()),$3=ce(l0()),N0=ce(Sl()),z0=ce(h3()),T3=ce(A0()),S3=ce(P0()),I3=ce(g0()),O3=ce(R0()),B0=ce(u0()),E3=ce(d0()),A3=ce(a0()),k3=ce(c0()),P3=ce(k0()),C3=ce(C0()),D3=ce(p0()),R3=ce(h0()),N3=ce(f0()),z3=ce(b0()),B3=ce(v0()),M3=ce(m0()),F3=ce(D0()),j3=ce(Fo()),_o=ce(Mo()),M0=ce(x0()),L3=ce(jo()),F0=ce($0()),U3=ce(T0())}),Lo=R(()=>{"use strict";V3()}),G3=ie((e,t)=>{"use strict";t.exports=n;function n(o,u){for(var l=new Array(arguments.length-1),d=0,c=2,s=!0;c{"use strict";var t=e;t.length=function(d){var c=d.length;if(!c)return 0;for(var s=0;--c%4>1&&d.charAt(c)==="=";)++s;return Math.ceil(d.length*3)/4-s};var n=new Array(64),o=new Array(123);for(u=0;u<64;)o[n[u]=u<26?u+65:u<52?u+71:u<62?u-4:u-59|43]=u++;var u;t.encode=function(d,c,s){for(var r=null,i=[],a=0,p=0,h;c>2],h=(m&3)<<4,p=1;break;case 1:i[a++]=n[h|m>>4],h=(m&15)<<2,p=2;break;case 2:i[a++]=n[h|m>>6],i[a++]=n[m&63],p=0;break}a>8191&&((r||(r=[])).push(String.fromCharCode.apply(String,i)),a=0)}return p&&(i[a++]=n[h],i[a++]=61,p===1&&(i[a++]=61)),r?(a&&r.push(String.fromCharCode.apply(String,i.slice(0,a))),r.join("")):String.fromCharCode.apply(String,i.slice(0,a))};var l="invalid encoding";t.decode=function(d,c,s){for(var r=s,i=0,a,p=0;p1)break;if((h=o[h])===void 0)throw Error(l);switch(i){case 0:a=h,i=1;break;case 1:c[s++]=a<<2|(h&48)>>4,a=h,i=2;break;case 2:c[s++]=(a&15)<<4|(h&60)>>2,a=h,i=3;break;case 3:c[s++]=(a&3)<<6|h,i=0;break}}if(i===1)throw Error(l);return s-r},t.test=function(d){return/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(d)}}),H3=ie((e,t)=>{"use strict";t.exports=n;function n(){this._listeners=Object.create(null)}n.prototype.on=function(o,u,l){return(this._listeners[o]||(this._listeners[o]=[])).push({fn:u,ctx:l||this}),this},n.prototype.off=function(o,u){if(o===void 0)this._listeners=Object.create(null);else if(u===void 0)this._listeners[o]=[];else{var l=this._listeners[o];if(!l)return this;for(var d=0;d{"use strict";t.exports=n(n);function n(c){return typeof Float32Array<"u"?(function(){var s=new Float32Array([-0]),r=new Uint8Array(s.buffer),i=r[3]===128;function a(b,x,w){s[0]=b,x[w]=r[0],x[w+1]=r[1],x[w+2]=r[2],x[w+3]=r[3]}function p(b,x,w){s[0]=b,x[w]=r[3],x[w+1]=r[2],x[w+2]=r[1],x[w+3]=r[0]}c.writeFloatLE=i?a:p,c.writeFloatBE=i?p:a;function h(b,x){return r[0]=b[x],r[1]=b[x+1],r[2]=b[x+2],r[3]=b[x+3],s[0]}function m(b,x){return r[3]=b[x],r[2]=b[x+1],r[1]=b[x+2],r[0]=b[x+3],s[0]}c.readFloatLE=i?h:m,c.readFloatBE=i?m:h})():(function(){function s(i,a,p,h){var m=a<0?1:0;if(m&&(a=-a),a===0)i(1/a>0?0:2147483648,p,h);else if(isNaN(a))i(2143289344,p,h);else if(a>34028234663852886e22)i((m<<31|2139095040)>>>0,p,h);else if(a<11754943508222875e-54)i((m<<31|Math.round(a/1401298464324817e-60))>>>0,p,h);else{var b=Math.floor(Math.log(a)/Math.LN2),x=Math.round(a*Math.pow(2,-b)*8388608)&8388607;i((m<<31|b+127<<23|x)>>>0,p,h)}}c.writeFloatLE=s.bind(null,o),c.writeFloatBE=s.bind(null,u);function r(i,a,p){var h=i(a,p),m=(h>>31)*2+1,b=h>>>23&255,x=h&8388607;return b===255?x?NaN:m*(1/0):b===0?m*1401298464324817e-60*x:m*Math.pow(2,b-150)*(x+8388608)}c.readFloatLE=r.bind(null,l),c.readFloatBE=r.bind(null,d)})(),typeof Float64Array<"u"?(function(){var s=new Float64Array([-0]),r=new Uint8Array(s.buffer),i=r[7]===128;function a(b,x,w){s[0]=b,x[w]=r[0],x[w+1]=r[1],x[w+2]=r[2],x[w+3]=r[3],x[w+4]=r[4],x[w+5]=r[5],x[w+6]=r[6],x[w+7]=r[7]}function p(b,x,w){s[0]=b,x[w]=r[7],x[w+1]=r[6],x[w+2]=r[5],x[w+3]=r[4],x[w+4]=r[3],x[w+5]=r[2],x[w+6]=r[1],x[w+7]=r[0]}c.writeDoubleLE=i?a:p,c.writeDoubleBE=i?p:a;function h(b,x){return r[0]=b[x],r[1]=b[x+1],r[2]=b[x+2],r[3]=b[x+3],r[4]=b[x+4],r[5]=b[x+5],r[6]=b[x+6],r[7]=b[x+7],s[0]}function m(b,x){return r[7]=b[x],r[6]=b[x+1],r[5]=b[x+2],r[4]=b[x+3],r[3]=b[x+4],r[2]=b[x+5],r[1]=b[x+6],r[0]=b[x+7],s[0]}c.readDoubleLE=i?h:m,c.readDoubleBE=i?m:h})():(function(){function s(i,a,p,h,m,b){var x=h<0?1:0;if(x&&(h=-h),h===0)i(0,m,b+a),i(1/h>0?0:2147483648,m,b+p);else if(isNaN(h))i(0,m,b+a),i(2146959360,m,b+p);else if(h>17976931348623157e292)i(0,m,b+a),i((x<<31|2146435072)>>>0,m,b+p);else{var w;if(h<22250738585072014e-324)w=h/5e-324,i(w>>>0,m,b+a),i((x<<31|w/4294967296)>>>0,m,b+p);else{var y=Math.floor(Math.log(h)/Math.LN2);y===1024&&(y=1023),w=h*Math.pow(2,-y),i(w*4503599627370496>>>0,m,b+a),i((x<<31|y+1023<<20|w*1048576&1048575)>>>0,m,b+p)}}}c.writeDoubleLE=s.bind(null,o,0,4),c.writeDoubleBE=s.bind(null,u,4,0);function r(i,a,p,h,m){var b=i(h,m+a),x=i(h,m+p),w=(x>>31)*2+1,y=x>>>20&2047,S=4294967296*(x&1048575)+b;return y===2047?S?NaN:w*(1/0):y===0?w*5e-324*S:w*Math.pow(2,y-1075)*(S+4503599627370496)}c.readDoubleLE=r.bind(null,l,0,4),c.readDoubleBE=r.bind(null,d,4,0)})(),c}function o(c,s,r){s[r]=c&255,s[r+1]=c>>>8&255,s[r+2]=c>>>16&255,s[r+3]=c>>>24}function u(c,s,r){s[r]=c>>>24,s[r+1]=c>>>16&255,s[r+2]=c>>>8&255,s[r+3]=c&255}function l(c,s){return(c[s]|c[s+1]<<8|c[s+2]<<16|c[s+3]<<24)>>>0}function d(c,s){return(c[s]<<24|c[s+1]<<16|c[s+2]<<8|c[s+3])>>>0}}),K3=ie((e,t)=>{"use strict";t.exports=n;function n(o){try{if(typeof gu!="function")return null;var u=gu(o);return u&&(u.length||Object.keys(u).length)?u:null}catch{return null}}}),X3=ie(e=>{"use strict";var t=e,n="\uFFFD";t.length=function(o){for(var u=0,l=0,d=0;d=192&&s<224){var r=(s&31)<<6|o[c++]&63;d+=r>=128?String.fromCharCode(r):n}else if(s>=224&&s<240){var i=(s&15)<<12|(o[c++]&63)<<6|o[c++]&63;d+=i>=2048?String.fromCharCode(i):n}else if(s>=240){var a=(s&7)<<18|(o[c++]&63)<<12|(o[c++]&63)<<6|o[c++]&63;a<65536||a>1114111?d+=n:(a-=65536,d+=String.fromCharCode(55296+(a>>10)),d+=String.fromCharCode(56320+(a&1023)))}}return d},t.write=function(o,u,l){for(var d=l,c,s,r=0;r>6|192,u[l++]=c&63|128):(c&64512)===55296&&((s=o.charCodeAt(r+1))&64512)===56320?(c=65536+((c&1023)<<10)+(s&1023),++r,u[l++]=c>>18|240,u[l++]=c>>12&63|128,u[l++]=c>>6&63|128,u[l++]=c&63|128):(u[l++]=c>>12|224,u[l++]=c>>6&63|128,u[l++]=c&63|128);return l-d}}),Z3=ie((e,t)=>{"use strict";t.exports=n;function n(o,u,l){var d=l||8192,c=d>>>1,s=null,r=d;return function(i){if(i<1||i>c)return o(i);r+i>d&&(s=o(d),r=0);var a=u.call(s,r,r+=i);return r&7&&(r=(r|7)+1),a}}}),J3=ie((e,t)=>{"use strict";t.exports=o;var n=ln();function o(c,s){this.lo=c>>>0,this.hi=s>>>0}var u=o.zero=new o(0,0);u.toNumber=function(){return 0},u.zzEncode=u.zzDecode=function(){return this},u.length=function(){return 1};var l=o.zeroHash="\0\0\0\0\0\0\0\0";o.fromNumber=function(c){if(c===0)return u;var s=c<0;s&&(c=-c);var r=c>>>0,i=(c-r)/4294967296>>>0;return s&&(i=~i>>>0,r=~r>>>0,++r>4294967295&&(r=0,++i>4294967295&&(i=0))),new o(r,i)},o.from=function(c){if(typeof c=="number")return o.fromNumber(c);if(n.isString(c))if(n.Long)c=n.Long.fromString(c);else return o.fromNumber(parseInt(c,10));return c.low||c.high?new o(c.low>>>0,c.high>>>0):u},o.prototype.toNumber=function(c){if(!c&&this.hi>>>31){var s=~this.lo+1>>>0,r=~this.hi>>>0;return s||(r=r+1>>>0),-(s+r*4294967296)}return this.lo+this.hi*4294967296},o.prototype.toLong=function(c){return n.Long?new n.Long(this.lo|0,this.hi|0,!!c):{low:this.lo|0,high:this.hi|0,unsigned:!!c}};var d=String.prototype.charCodeAt;o.fromHash=function(c){return c===l?u:new o((d.call(c,0)|d.call(c,1)<<8|d.call(c,2)<<16|d.call(c,3)<<24)>>>0,(d.call(c,4)|d.call(c,5)<<8|d.call(c,6)<<16|d.call(c,7)<<24)>>>0)},o.prototype.toHash=function(){return String.fromCharCode(this.lo&255,this.lo>>>8&255,this.lo>>>16&255,this.lo>>>24,this.hi&255,this.hi>>>8&255,this.hi>>>16&255,this.hi>>>24)},o.prototype.zzEncode=function(){var c=this.hi>>31;return this.hi=((this.hi<<1|this.lo>>>31)^c)>>>0,this.lo=(this.lo<<1^c)>>>0,this},o.prototype.zzDecode=function(){var c=-(this.lo&1);return this.lo=((this.lo>>>1|this.hi<<31)^c)>>>0,this.hi=(this.hi>>>1^c)>>>0,this},o.prototype.length=function(){var c=this.lo,s=(this.lo>>>28|this.hi<<4)>>>0,r=this.hi>>>24;return r===0?s===0?c<16384?c<128?1:2:c<2097152?3:4:s<16384?s<128?5:6:s<2097152?7:8:r<128?9:10}}),Y3=ie((e,t)=>{(function(n,o){function u(l){return l.default||l}typeof define=="function"&&define.amd?define([],function(){var l={};return o(l),u(l)}):typeof e=="object"?(o(e),typeof t=="object"&&(t.exports=u(e))):(function(){var l={};o(l),n.Long=u(l)})()})(typeof globalThis<"u"?globalThis:typeof self<"u"?self:e,function(n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;var o=null;try{o=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}function u(_,C,F){this.low=_|0,this.high=C|0,this.unsigned=!!F}u.prototype.__isLong__,Object.defineProperty(u.prototype,"__isLong__",{value:!0});function l(_){return(_&&_.__isLong__)===!0}function d(_){var C=Math.clz32(_&-_);return _?31-C:C}u.isLong=l;var c={},s={};function r(_,C){var F,V,G;return C?(_>>>=0,(G=0<=_&&_<256)&&(V=s[_],V)?V:(F=a(_,0,!0),G&&(s[_]=F),F)):(_|=0,(G=-128<=_&&_<128)&&(V=c[_],V)?V:(F=a(_,_<0?-1:0,!1),G&&(c[_]=F),F))}u.fromInt=r;function i(_,C){if(isNaN(_))return C?A:I;if(C){if(_<0)return A;if(_>=y)return X}else{if(_<=-S)return q;if(_+1>=S)return U}return _<0?i(-_,C).neg():a(_%w|0,_/w|0,C)}u.fromNumber=i;function a(_,C,F){return new u(_,C,F)}u.fromBits=a;var p=Math.pow;function h(_,C,F){if(_.length===0)throw Error("empty string");if(typeof C=="number"?(F=C,C=!1):C=!!C,_==="NaN"||_==="Infinity"||_==="+Infinity"||_==="-Infinity")return C?A:I;if(F=F||10,F<2||360)throw Error("interior hyphen");if(V===0)return h(_.substring(1),C,F).neg();for(var G=i(p(F,8)),H=I,M=0;M<_.length;M+=8){var J=Math.min(8,_.length-M),Z=parseInt(_.substring(M,M+J),F);if(J<8){var W=i(p(F,J));H=H.mul(W).add(i(Z))}else H=H.mul(G),H=H.add(i(Z))}return H.unsigned=C,H}u.fromString=h;function m(_,C){return typeof _=="number"?i(_,C):typeof _=="string"?h(_,C):a(_.low,_.high,typeof C=="boolean"?C:_.unsigned)}u.fromValue=m;var b=65536,x=1<<24,w=b*b,y=w*w,S=y/2,T=r(x),I=r(0);u.ZERO=I;var A=r(0,!0);u.UZERO=A;var P=r(1);u.ONE=P;var O=r(1,!0);u.UONE=O;var B=r(-1);u.NEG_ONE=B;var U=a(-1,2147483647,!1);u.MAX_VALUE=U;var X=a(-1,-1,!0);u.MAX_UNSIGNED_VALUE=X;var q=a(0,-2147483648,!1);u.MIN_VALUE=q;var D=u.prototype;D.toInt=function(){return this.unsigned?this.low>>>0:this.low},D.toNumber=function(){return this.unsigned?(this.high>>>0)*w+(this.low>>>0):this.high*w+(this.low>>>0)},D.toString=function(_){if(_=_||10,_<2||36<_)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative())if(this.eq(q)){var C=i(_),F=this.div(C),V=F.mul(C).sub(this);return F.toString(_)+V.toInt().toString(_)}else return"-"+this.neg().toString(_);for(var G=i(p(_,6),this.unsigned),H=this,M="";;){var J=H.div(G),Z=H.sub(J.mul(G)).toInt()>>>0,W=Z.toString(_);if(H=J,H.isZero())return W+M;for(;W.length<6;)W="0"+W;M=""+W+M}},D.getHighBits=function(){return this.high},D.getHighBitsUnsigned=function(){return this.high>>>0},D.getLowBits=function(){return this.low},D.getLowBitsUnsigned=function(){return this.low>>>0},D.getNumBitsAbs=function(){if(this.isNegative())return this.eq(q)?64:this.neg().getNumBitsAbs();for(var _=this.high!=0?this.high:this.low,C=31;C>0&&(_&1<>21;return _?this.unsigned?!1:_===-1&&!(this.low===0&&this.high===-2097152):!0},D.isZero=function(){return this.high===0&&this.low===0},D.eqz=D.isZero,D.isNegative=function(){return!this.unsigned&&this.high<0},D.isPositive=function(){return this.unsigned||this.high>=0},D.isOdd=function(){return(this.low&1)===1},D.isEven=function(){return(this.low&1)===0},D.equals=function(_){return l(_)||(_=m(_)),this.unsigned!==_.unsigned&&this.high>>>31===1&&_.high>>>31===1?!1:this.high===_.high&&this.low===_.low},D.eq=D.equals,D.notEquals=function(_){return!this.eq(_)},D.neq=D.notEquals,D.ne=D.notEquals,D.lessThan=function(_){return this.comp(_)<0},D.lt=D.lessThan,D.lessThanOrEqual=function(_){return this.comp(_)<=0},D.lte=D.lessThanOrEqual,D.le=D.lessThanOrEqual,D.greaterThan=function(_){return this.comp(_)>0},D.gt=D.greaterThan,D.greaterThanOrEqual=function(_){return this.comp(_)>=0},D.gte=D.greaterThanOrEqual,D.ge=D.greaterThanOrEqual,D.compare=function(_){if(l(_)||(_=m(_)),this.eq(_))return 0;var C=this.isNegative(),F=_.isNegative();return C&&!F?-1:!C&&F?1:this.unsigned?_.high>>>0>this.high>>>0||_.high===this.high&&_.low>>>0>this.low>>>0?-1:1:this.sub(_).isNegative()?-1:1},D.comp=D.compare,D.negate=function(){return!this.unsigned&&this.eq(q)?q:this.not().add(P)},D.neg=D.negate,D.add=function(_){l(_)||(_=m(_));var C=this.high>>>16,F=this.high&65535,V=this.low>>>16,G=this.low&65535,H=_.high>>>16,M=_.high&65535,J=_.low>>>16,Z=_.low&65535,W=0,pe=0,fe=0,ye=0;return ye+=G+Z,fe+=ye>>>16,ye&=65535,fe+=V+J,pe+=fe>>>16,fe&=65535,pe+=F+M,W+=pe>>>16,pe&=65535,W+=C+H,W&=65535,a(fe<<16|ye,W<<16|pe,this.unsigned)},D.subtract=function(_){return l(_)||(_=m(_)),this.add(_.neg())},D.sub=D.subtract,D.multiply=function(_){if(this.isZero())return this;if(l(_)||(_=m(_)),o){var C=o.mul(this.low,this.high,_.low,_.high);return a(C,o.get_high(),this.unsigned)}if(_.isZero())return this.unsigned?A:I;if(this.eq(q))return _.isOdd()?q:I;if(_.eq(q))return this.isOdd()?q:I;if(this.isNegative())return _.isNegative()?this.neg().mul(_.neg()):this.neg().mul(_).neg();if(_.isNegative())return this.mul(_.neg()).neg();if(this.lt(T)&&_.lt(T))return i(this.toNumber()*_.toNumber(),this.unsigned);var F=this.high>>>16,V=this.high&65535,G=this.low>>>16,H=this.low&65535,M=_.high>>>16,J=_.high&65535,Z=_.low>>>16,W=_.low&65535,pe=0,fe=0,ye=0,Fe=0;return Fe+=H*W,ye+=Fe>>>16,Fe&=65535,ye+=G*W,fe+=ye>>>16,ye&=65535,ye+=H*Z,fe+=ye>>>16,ye&=65535,fe+=V*W,pe+=fe>>>16,fe&=65535,fe+=G*Z,pe+=fe>>>16,fe&=65535,fe+=H*J,pe+=fe>>>16,fe&=65535,pe+=F*W+V*Z+G*J+H*M,pe&=65535,a(ye<<16|Fe,pe<<16|fe,this.unsigned)},D.mul=D.multiply,D.divide=function(_){if(l(_)||(_=m(_)),_.isZero())throw Error("division by zero");if(o){if(!this.unsigned&&this.high===-2147483648&&_.low===-1&&_.high===-1)return this;var C=(this.unsigned?o.div_u:o.div_s)(this.low,this.high,_.low,_.high);return a(C,o.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?A:I;var F,V,G;if(this.unsigned){if(_.unsigned||(_=_.toUnsigned()),_.gt(this))return A;if(_.gt(this.shru(1)))return O;G=A}else{if(this.eq(q)){if(_.eq(P)||_.eq(B))return q;if(_.eq(q))return P;var H=this.shr(1);return F=H.div(_).shl(1),F.eq(I)?_.isNegative()?P:B:(V=this.sub(_.mul(F)),G=F.add(V.div(_)),G)}else if(_.eq(q))return this.unsigned?A:I;if(this.isNegative())return _.isNegative()?this.neg().div(_.neg()):this.neg().div(_).neg();if(_.isNegative())return this.div(_.neg()).neg();G=I}for(V=this;V.gte(_);){F=Math.max(1,Math.floor(V.toNumber()/_.toNumber()));for(var M=Math.ceil(Math.log(F)/Math.LN2),J=M<=48?1:p(2,M-48),Z=i(F),W=Z.mul(_);W.isNegative()||W.gt(V);)F-=J,Z=i(F,this.unsigned),W=Z.mul(_);Z.isZero()&&(Z=P),G=G.add(Z),V=V.sub(W)}return G},D.div=D.divide,D.modulo=function(_){if(l(_)||(_=m(_)),o){var C=(this.unsigned?o.rem_u:o.rem_s)(this.low,this.high,_.low,_.high);return a(C,o.get_high(),this.unsigned)}return this.sub(this.div(_).mul(_))},D.mod=D.modulo,D.rem=D.modulo,D.not=function(){return a(~this.low,~this.high,this.unsigned)},D.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},D.clz=D.countLeadingZeros,D.countTrailingZeros=function(){return this.low?d(this.low):d(this.high)+32},D.ctz=D.countTrailingZeros,D.and=function(_){return l(_)||(_=m(_)),a(this.low&_.low,this.high&_.high,this.unsigned)},D.or=function(_){return l(_)||(_=m(_)),a(this.low|_.low,this.high|_.high,this.unsigned)},D.xor=function(_){return l(_)||(_=m(_)),a(this.low^_.low,this.high^_.high,this.unsigned)},D.shiftLeft=function(_){return l(_)&&(_=_.toInt()),(_&=63)===0?this:_<32?a(this.low<<_,this.high<<_|this.low>>>32-_,this.unsigned):a(0,this.low<<_-32,this.unsigned)},D.shl=D.shiftLeft,D.shiftRight=function(_){return l(_)&&(_=_.toInt()),(_&=63)===0?this:_<32?a(this.low>>>_|this.high<<32-_,this.high>>_,this.unsigned):a(this.high>>_-32,this.high>=0?0:-1,this.unsigned)},D.shr=D.shiftRight,D.shiftRightUnsigned=function(_){return l(_)&&(_=_.toInt()),(_&=63)===0?this:_<32?a(this.low>>>_|this.high<<32-_,this.high>>>_,this.unsigned):_===32?a(this.high,0,this.unsigned):a(this.high>>>_-32,0,this.unsigned)},D.shru=D.shiftRightUnsigned,D.shr_u=D.shiftRightUnsigned,D.rotateLeft=function(_){var C;return l(_)&&(_=_.toInt()),(_&=63)===0?this:_===32?a(this.high,this.low,this.unsigned):_<32?(C=32-_,a(this.low<<_|this.high>>>C,this.high<<_|this.low>>>C,this.unsigned)):(_-=32,C=32-_,a(this.high<<_|this.low>>>C,this.low<<_|this.high>>>C,this.unsigned))},D.rotl=D.rotateLeft,D.rotateRight=function(_){var C;return l(_)&&(_=_.toInt()),(_&=63)===0?this:_===32?a(this.high,this.low,this.unsigned):_<32?(C=32-_,a(this.high<>>_,this.low<>>_,this.unsigned)):(_-=32,C=32-_,a(this.low<>>_,this.high<>>_,this.unsigned))},D.rotr=D.rotateRight,D.toSigned=function(){return this.unsigned?a(this.low,this.high,!1):this},D.toUnsigned=function(){return this.unsigned?this:a(this.low,this.high,!0)},D.toBytes=function(_){return _?this.toBytesLE():this.toBytesBE()},D.toBytesLE=function(){var _=this.high,C=this.low;return[C&255,C>>>8&255,C>>>16&255,C>>>24,_&255,_>>>8&255,_>>>16&255,_>>>24]},D.toBytesBE=function(){var _=this.high,C=this.low;return[_>>>24,_>>>16&255,_>>>8&255,_&255,C>>>24,C>>>16&255,C>>>8&255,C&255]},u.fromBytes=function(_,C,F){return F?u.fromBytesLE(_,C):u.fromBytesBE(_,C)},u.fromBytesLE=function(_,C){return new u(_[0]|_[1]<<8|_[2]<<16|_[3]<<24,_[4]|_[5]<<8|_[6]<<16|_[7]<<24,C)},u.fromBytesBE=function(_,C){return new u(_[4]<<24|_[5]<<16|_[6]<<8|_[7],_[0]<<24|_[1]<<16|_[2]<<8|_[3],C)},typeof BigInt=="function"&&(u.fromBigInt=function(_,C){var F=Number(BigInt.asIntN(32,_)),V=Number(BigInt.asIntN(32,_>>BigInt(32)));return a(F,V,C)},u.fromValue=function(_,C){return typeof _=="bigint"?u.fromBigInt(_,C):m(_,C)},D.toBigInt=function(){var _=BigInt(this.low>>>0),C=BigInt(this.unsigned?this.high>>>0:this.high);return C<{"use strict";var t=e;t.asPromise=G3(),t.base64=q3(),t.EventEmitter=H3(),t.float=W3(),t.inquire=K3(),t.utf8=X3(),t.pool=Z3(),t.LongBits=J3();function n(l){return l==="__proto__"||l==="prototype"||l==="constructor"}t.isUnsafeProperty=n,t.isNode=!!(typeof global<"u"&&global&&global.process&&global.process.versions&&global.process.versions.node),t.global=t.isNode&&global||typeof window<"u"&&window||typeof self<"u"&&self||e,t.emptyArray=Object.freeze?Object.freeze([]):[],t.emptyObject=Object.freeze?Object.freeze({}):{},t.isInteger=Number.isInteger||function(l){return typeof l=="number"&&isFinite(l)&&Math.floor(l)===l},t.isString=function(l){return typeof l=="string"||l instanceof String},t.isObject=function(l){return l&&typeof l=="object"},t.isset=t.isSet=function(l,d){var c=l[d];return c!=null&&Object.hasOwnProperty.call(l,d)?typeof c!="object"||(Array.isArray(c)?c.length:Object.keys(c).length)>0:!1},t.Buffer=(function(){try{var l=t.global.Buffer;return l.prototype.utf8Write?l:null}catch{return null}})(),t._Buffer_from=null,t._Buffer_allocUnsafe=null,t.newBuffer=function(l){return typeof l=="number"?t.Buffer?t._Buffer_allocUnsafe(l):new t.Array(l):t.Buffer?t._Buffer_from(l):typeof Uint8Array>"u"?l:new Uint8Array(l)},t.Array=typeof Uint8Array<"u"?Uint8Array:Array,t.Long=t.global.dcodeIO&&t.global.dcodeIO.Long||t.global.Long||(function(){try{var l=Y3();return l&&l.isLong?l:null}catch{return null}})(),t.key2Re=/^true|false|0|1$/,t.key32Re=/^-?(?:0|[1-9][0-9]*)$/,t.key64Re=/^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/,t.longToHash=function(l){return l?t.LongBits.from(l).toHash():t.LongBits.zeroHash},t.longFromHash=function(l,d){var c=t.LongBits.fromHash(l);return t.Long?t.Long.fromBits(c.lo,c.hi,d):c.toNumber(!!d)};function o(l){var d=typeof arguments[arguments.length-1]=="boolean",c=d?arguments.length-1:arguments.length;d=d&&arguments[arguments.length-1];for(var s=1;s-1;--r)if(d[s[r]]===1&&this[s[r]]!==void 0&&this[s[r]]!==null)return s[r]}},t.oneOfSetter=function(l){return function(d){for(var c=0;c{"use strict";t.exports=i;var n=ln(),o,u=n.LongBits,l=n.base64,d=n.utf8;function c(y,S,T){this.fn=y,this.len=S,this.next=void 0,this.val=T}function s(){}function r(y){this.head=y.head,this.tail=y.tail,this.len=y.len,this.next=y.states}function i(){this.len=0,this.head=new c(s,0,0),this.tail=this.head,this.states=null}var a=function(){return n.Buffer?function(){return(i.create=function(){return new o})()}:function(){return new i}};i.create=a(),i.alloc=function(y){return new n.Array(y)},n.Array!==Array&&(i.alloc=n.pool(i.alloc,n.Array.prototype.subarray)),i.prototype._push=function(y,S,T){return this.tail=this.tail.next=new c(y,S,T),this.len+=S,this};function p(y,S,T){S[T]=y&255}function h(y,S,T){for(;y>127;)S[T++]=y&127|128,y>>>=7;S[T]=y}function m(y,S){this.len=y,this.next=void 0,this.val=S}m.prototype=Object.create(c.prototype),m.prototype.fn=h,i.prototype.uint32=function(y){return this.len+=(this.tail=this.tail.next=new m((y=y>>>0)<128?1:y<16384?2:y<2097152?3:y<268435456?4:5,y)).len,this},i.prototype.int32=function(y){return(y|=0)<0?this._push(b,10,u.fromNumber(y)):this.uint32(y)},i.prototype.sint32=function(y){return this.uint32((y<<1^y>>31)>>>0)};function b(y,S,T){for(var I=y.lo,A=y.hi;A;)S[T++]=I&127|128,I=(I>>>7|A<<25)>>>0,A>>>=7;for(;I>127;)S[T++]=I&127|128,I=I>>>7;S[T++]=I}i.prototype.uint64=function(y){var S=u.from(y);return this._push(b,S.length(),S)},i.prototype.int64=i.prototype.uint64,i.prototype.sint64=function(y){var S=u.from(y).zzEncode();return this._push(b,S.length(),S)},i.prototype.bool=function(y){return this._push(p,1,y?1:0)};function x(y,S,T){S[T]=y&255,S[T+1]=y>>>8&255,S[T+2]=y>>>16&255,S[T+3]=y>>>24}i.prototype.fixed32=function(y){return this._push(x,4,y>>>0)},i.prototype.sfixed32=i.prototype.fixed32,i.prototype.fixed64=function(y){var S=u.from(y);return this._push(x,4,S.lo)._push(x,4,S.hi)},i.prototype.sfixed64=i.prototype.fixed64,i.prototype.float=function(y){return this._push(n.float.writeFloatLE,4,y)},i.prototype.double=function(y){return this._push(n.float.writeDoubleLE,8,y)};var w=n.Array.prototype.set?function(y,S,T){S.set(y,T)}:function(y,S,T){for(var I=0;I>>0;if(!S)return this._push(p,1,0);if(n.isString(y)){var T=i.alloc(S=l.length(y));l.decode(y,T,0),y=T}return this.uint32(S)._push(w,S,y)},i.prototype.string=function(y){var S=d.length(y);return S?this.uint32(S)._push(d.write,S,y):this._push(p,1,0)},i.prototype.fork=function(){return this.states=new r(this),this.head=this.tail=new c(s,0,0),this.len=0,this},i.prototype.reset=function(){return this.states?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new c(s,0,0),this.len=0),this},i.prototype.ldelim=function(){var y=this.head,S=this.tail,T=this.len;return this.reset().uint32(T),T&&(this.tail.next=y.next,this.tail=S,this.len+=T),this},i.prototype.finish=function(){for(var y=this.head.next,S=this.constructor.alloc(this.len),T=0;y;)y.fn(y.val,S,T),T+=y.len,y=y.next;return S},i._configure=function(y){o=y,i.create=a(),o._configure()}}),Q3=ie((e,t)=>{"use strict";t.exports=u;var n=j0();(u.prototype=Object.create(n.prototype)).constructor=u;var o=ln();function u(){n.call(this)}u._configure=function(){u.alloc=o._Buffer_allocUnsafe,u.writeBytesBuffer=o.Buffer&&o.Buffer.prototype instanceof Uint8Array&&o.Buffer.prototype.set.name==="set"?function(d,c,s){c.set(d,s)}:function(d,c,s){if(d.copy)d.copy(c,s,0,d.length);else for(var r=0;r>>0;return this.uint32(c),c&&this._push(u.writeBytesBuffer,c,d),this};function l(d,c,s){d.length<40?o.utf8.write(d,c,s):c.utf8Write?c.utf8Write(d,s):c.write(d,s)}u.prototype.string=function(d){var c=o.Buffer.byteLength(d);return this.uint32(c),c&&this._push(l,c,d),this},u._configure()}),L0=ie((e,t)=>{"use strict";t.exports=c;var n=ln(),o,u=n.LongBits,l=n.utf8;function d(h,m){return RangeError("index out of range: "+h.pos+" + "+(m||1)+" > "+h.len)}function c(h){this.buf=h,this.pos=0,this.len=h.length}var s=typeof Uint8Array<"u"?function(h){if(h instanceof Uint8Array||Array.isArray(h))return new c(h);throw Error("illegal buffer")}:function(h){if(Array.isArray(h))return new c(h);throw Error("illegal buffer")},r=function(){return n.Buffer?function(h){return(c.create=function(m){return n.Buffer.isBuffer(m)?new o(m):s(m)})(h)}:s};c.create=r(),c.prototype._slice=n.Array.prototype.subarray||n.Array.prototype.slice,c.prototype.uint32=(function(){var h=4294967295;return function(){if(h=(this.buf[this.pos]&127)>>>0,this.buf[this.pos++]<128||(h=(h|(this.buf[this.pos]&127)<<7)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<14)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<21)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&15)<<28)>>>0,this.buf[this.pos++]<128))return h;if((this.pos+=5)>this.len)throw this.pos=this.len,d(this,10);return h}})(),c.prototype.int32=function(){return this.uint32()|0},c.prototype.sint32=function(){var h=this.uint32();return h>>>1^-(h&1)|0};function i(){var h=new u(0,0),m=0;if(this.len-this.pos>4){for(;m<4;++m)if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h;if(h.lo=(h.lo|(this.buf[this.pos]&127)<<28)>>>0,h.hi=(h.hi|(this.buf[this.pos]&127)>>4)>>>0,this.buf[this.pos++]<128)return h;m=0}else{for(;m<3;++m){if(this.pos>=this.len)throw d(this);if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}return h.lo=(h.lo|(this.buf[this.pos++]&127)<>>0,h}if(this.len-this.pos>4){for(;m<5;++m)if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}else for(;m<5;++m){if(this.pos>=this.len)throw d(this);if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}throw Error("invalid varint encoding")}c.prototype.bool=function(){return this.uint32()!==0};function a(h,m){return(h[m-4]|h[m-3]<<8|h[m-2]<<16|h[m-1]<<24)>>>0}c.prototype.fixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)},c.prototype.sfixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)|0};function p(){if(this.pos+8>this.len)throw d(this,8);return new u(a(this.buf,this.pos+=4),a(this.buf,this.pos+=4))}c.prototype.float=function(){if(this.pos+4>this.len)throw d(this,4);var h=n.float.readFloatLE(this.buf,this.pos);return this.pos+=4,h},c.prototype.double=function(){if(this.pos+8>this.len)throw d(this,4);var h=n.float.readDoubleLE(this.buf,this.pos);return this.pos+=8,h},c.prototype.bytes=function(){var h=this.uint32(),m=this.pos,b=this.pos+h;if(b>this.len)throw d(this,h);if(this.pos+=h,Array.isArray(this.buf))return this.buf.slice(m,b);if(m===b){var x=n.Buffer;return x?x.alloc(0):new this.buf.constructor(0)}return this._slice.call(this.buf,m,b)},c.prototype.string=function(){var h=this.bytes();return l.read(h,0,h.length)},c.prototype.skip=function(h){if(typeof h=="number"){if(this.pos+h>this.len)throw d(this,h);this.pos+=h}else do if(this.pos>=this.len)throw d(this);while(this.buf[this.pos++]&128);return this},c.recursionLimit=n.recursionLimit,c.prototype.skipType=function(h,m){if(m===void 0&&(m=0),m>c.recursionLimit)throw Error("maximum nesting depth exceeded");switch(h){case 0:this.skip();break;case 1:this.skip(8);break;case 2:this.skip(this.uint32());break;case 3:for(;(h=this.uint32()&7)!==4;)this.skipType(h,m+1);break;case 5:this.skip(4);break;default:throw Error("invalid wire type "+h+" at offset "+this.pos)}return this},c._configure=function(h){o=h,c.create=r(),o._configure();var m=n.Long?"toLong":"toNumber";n.merge(c.prototype,{int64:function(){return i.call(this)[m](!1)},uint64:function(){return i.call(this)[m](!0)},sint64:function(){return i.call(this).zzDecode()[m](!1)},fixed64:function(){return p.call(this)[m](!0)},sfixed64:function(){return p.call(this)[m](!1)}})}}),eO=ie((e,t)=>{"use strict";t.exports=u;var n=L0();(u.prototype=Object.create(n.prototype)).constructor=u;var o=ln();function u(l){n.call(this,l)}u._configure=function(){o.Buffer&&(u.prototype._slice=o.Buffer.prototype.slice)},u.prototype.string=function(){var l=this.uint32();return this.buf.utf8Slice?this.buf.utf8Slice(this.pos,this.pos=Math.min(this.pos+l,this.len)):this.buf.toString("utf-8",this.pos,this.pos=Math.min(this.pos+l,this.len))},u._configure()}),tO=ie((e,t)=>{"use strict";t.exports=o;var n=ln();(o.prototype=Object.create(n.EventEmitter.prototype)).constructor=o;function o(u,l,d){if(typeof u!="function")throw TypeError("rpcImpl must be a function");n.EventEmitter.call(this),this.rpcImpl=u,this.requestDelimited=!!l,this.responseDelimited=!!d}o.prototype.rpcCall=function u(l,d,c,s,r){if(!s)throw TypeError("request must be specified");var i=this;if(!r)return n.asPromise(u,i,l,d,c,s);if(!i.rpcImpl){setTimeout(function(){r(Error("already ended"))},0);return}try{return i.rpcImpl(l,d[i.requestDelimited?"encodeDelimited":"encode"](s).finish(),function(a,p){if(a)return i.emit("error",a,l),r(a);if(p===null){i.end(!0);return}if(!(p instanceof c))try{p=c[i.responseDelimited?"decodeDelimited":"decode"](p)}catch(h){return i.emit("error",h,l),r(h)}return i.emit("data",p,l),r(null,p)})}catch(a){i.emit("error",a,l),setTimeout(function(){r(a)},0);return}},o.prototype.end=function(u){return this.rpcImpl&&(u||this.rpcImpl(null,null,null),this.rpcImpl=null,this.emit("end").off()),this}}),rO=ie(e=>{"use strict";var t=e;t.Service=tO()}),nO=ie((e,t)=>{"use strict";t.exports=Object.create(null)}),iO=ie(e=>{"use strict";var t=e;t.build="minimal",t.Writer=j0(),t.BufferWriter=Q3(),t.Reader=L0(),t.BufferReader=eO(),t.util=ln(),t.rpc=rO(),t.roots=nO(),t.configure=n;function n(){t.util._configure(),t.Writer._configure(t.BufferWriter),t.Reader._configure(t.BufferReader)}n()}),oO=ie((e,t)=>{"use strict";t.exports=iO()}),Ei=ie((e,t)=>{"use strict";var n=oO(),o=n.Reader,u=n.Writer,l=n.util,d=n.roots.default||(n.roots.default={});d.onnx=(function(){var c={};return c.Version=(function(){var s={},r=Object.create(s);return r[s[0]="_START_VERSION"]=0,r[s[1]="IR_VERSION_2017_10_10"]=1,r[s[2]="IR_VERSION_2017_10_30"]=2,r[s[3]="IR_VERSION_2017_11_3"]=3,r[s[4]="IR_VERSION_2019_1_22"]=4,r[s[5]="IR_VERSION_2019_3_18"]=5,r[s[6]="IR_VERSION_2019_9_19"]=6,r[s[7]="IR_VERSION_2020_5_8"]=7,r[s[8]="IR_VERSION_2021_7_30"]=8,r[s[9]="IR_VERSION"]=9,r})(),c.AttributeProto=(function(){function s(r){if(this.floats=[],this.ints=[],this.strings=[],this.tensors=[],this.graphs=[],this.sparseTensors=[],this.typeProtos=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 21:{p.refAttrName=r.string();break}case 13:{p.docString=r.string();break}case 20:{p.type=r.int32();break}case 2:{p.f=r.float();break}case 3:{p.i=r.int64();break}case 4:{p.s=r.bytes();break}case 5:{p.t=d.onnx.TensorProto.decode(r,r.uint32());break}case 6:{p.g=d.onnx.GraphProto.decode(r,r.uint32());break}case 22:{p.sparseTensor=d.onnx.SparseTensorProto.decode(r,r.uint32());break}case 14:{p.tp=d.onnx.TypeProto.decode(r,r.uint32());break}case 7:{if(p.floats&&p.floats.length||(p.floats=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.i.high>>>0).toNumber())),r.s!=null&&(typeof r.s=="string"?l.base64.decode(r.s,i.s=l.newBuffer(l.base64.length(r.s)),0):r.s.length>=0&&(i.s=r.s)),r.t!=null){if(typeof r.t!="object")throw TypeError(".onnx.AttributeProto.t: object expected");i.t=d.onnx.TensorProto.fromObject(r.t)}if(r.g!=null){if(typeof r.g!="object")throw TypeError(".onnx.AttributeProto.g: object expected");i.g=d.onnx.GraphProto.fromObject(r.g)}if(r.sparseTensor!=null){if(typeof r.sparseTensor!="object")throw TypeError(".onnx.AttributeProto.sparseTensor: object expected");i.sparseTensor=d.onnx.SparseTensorProto.fromObject(r.sparseTensor)}if(r.tp!=null){if(typeof r.tp!="object")throw TypeError(".onnx.AttributeProto.tp: object expected");i.tp=d.onnx.TypeProto.fromObject(r.tp)}if(r.floats){if(!Array.isArray(r.floats))throw TypeError(".onnx.AttributeProto.floats: array expected");i.floats=[];for(var a=0;a>>0,r.ints[a].high>>>0).toNumber())}if(r.strings){if(!Array.isArray(r.strings))throw TypeError(".onnx.AttributeProto.strings: array expected");i.strings=[];for(var a=0;a=0&&(i.strings[a]=r.strings[a])}if(r.tensors){if(!Array.isArray(r.tensors))throw TypeError(".onnx.AttributeProto.tensors: array expected");i.tensors=[];for(var a=0;a>>0,r.i.high>>>0).toNumber():r.i),r.s!=null&&r.hasOwnProperty("s")&&(a.s=i.bytes===String?l.base64.encode(r.s,0,r.s.length):i.bytes===Array?Array.prototype.slice.call(r.s):r.s),r.t!=null&&r.hasOwnProperty("t")&&(a.t=d.onnx.TensorProto.toObject(r.t,i)),r.g!=null&&r.hasOwnProperty("g")&&(a.g=d.onnx.GraphProto.toObject(r.g,i)),r.floats&&r.floats.length){a.floats=[];for(var h=0;h>>0,r.ints[h].high>>>0).toNumber():r.ints[h]}if(r.strings&&r.strings.length){a.strings=[];for(var h=0;h>>3){case 1:{p.name=r.string();break}case 2:{p.type=d.onnx.TypeProto.decode(r,r.uint32());break}case 3:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.type!=null&&r.hasOwnProperty("type")){var i=d.onnx.TypeProto.verify(r.type);if(i)return"type."+i}return r.docString!=null&&r.hasOwnProperty("docString")&&!l.isString(r.docString)?"docString: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.ValueInfoProto)return r;var i=new d.onnx.ValueInfoProto;if(r.name!=null&&(i.name=String(r.name)),r.type!=null){if(typeof r.type!="object")throw TypeError(".onnx.ValueInfoProto.type: object expected");i.type=d.onnx.TypeProto.fromObject(r.type)}return r.docString!=null&&(i.docString=String(r.docString)),i},s.toObject=function(r,i){i||(i={});var a={};return i.defaults&&(a.name="",a.type=null,a.docString=""),r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.type!=null&&r.hasOwnProperty("type")&&(a.type=d.onnx.TypeProto.toObject(r.type,i)),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.ValueInfoProto"},s})(),c.NodeProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 2:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 3:{p.name=r.string();break}case 4:{p.opType=r.string();break}case 7:{p.domain=r.string();break}case 5:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 6:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var i=0;i>>3){case 1:{p.initialization=d.onnx.GraphProto.decode(r,r.uint32());break}case 2:{p.algorithm=d.onnx.GraphProto.decode(r,r.uint32());break}case 3:{p.initializationBinding&&p.initializationBinding.length||(p.initializationBinding=[]),p.initializationBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 4:{p.updateBinding&&p.updateBinding.length||(p.updateBinding=[]),p.updateBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.initialization!=null&&r.hasOwnProperty("initialization")){var i=d.onnx.GraphProto.verify(r.initialization);if(i)return"initialization."+i}if(r.algorithm!=null&&r.hasOwnProperty("algorithm")){var i=d.onnx.GraphProto.verify(r.algorithm);if(i)return"algorithm."+i}if(r.initializationBinding!=null&&r.hasOwnProperty("initializationBinding")){if(!Array.isArray(r.initializationBinding))return"initializationBinding: array expected";for(var a=0;a>>3){case 1:{p.irVersion=r.int64();break}case 8:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 2:{p.producerName=r.string();break}case 3:{p.producerVersion=r.string();break}case 4:{p.domain=r.string();break}case 5:{p.modelVersion=r.int64();break}case 6:{p.docString=r.string();break}case 7:{p.graph=d.onnx.GraphProto.decode(r,r.uint32());break}case 14:{p.metadataProps&&p.metadataProps.length||(p.metadataProps=[]),p.metadataProps.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 20:{p.trainingInfo&&p.trainingInfo.length||(p.trainingInfo=[]),p.trainingInfo.push(d.onnx.TrainingInfoProto.decode(r,r.uint32()));break}case 25:{p.functions&&p.functions.length||(p.functions=[]),p.functions.push(d.onnx.FunctionProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.irVersion!=null&&r.hasOwnProperty("irVersion")&&!l.isInteger(r.irVersion)&&!(r.irVersion&&l.isInteger(r.irVersion.low)&&l.isInteger(r.irVersion.high)))return"irVersion: integer|Long expected";if(r.opsetImport!=null&&r.hasOwnProperty("opsetImport")){if(!Array.isArray(r.opsetImport))return"opsetImport: array expected";for(var i=0;i>>0,r.irVersion.high>>>0).toNumber())),r.opsetImport){if(!Array.isArray(r.opsetImport))throw TypeError(".onnx.ModelProto.opsetImport: array expected");i.opsetImport=[];for(var a=0;a>>0,r.modelVersion.high>>>0).toNumber())),r.docString!=null&&(i.docString=String(r.docString)),r.graph!=null){if(typeof r.graph!="object")throw TypeError(".onnx.ModelProto.graph: object expected");i.graph=d.onnx.GraphProto.fromObject(r.graph)}if(r.metadataProps){if(!Array.isArray(r.metadataProps))throw TypeError(".onnx.ModelProto.metadataProps: array expected");i.metadataProps=[];for(var a=0;a>>0,r.irVersion.high>>>0).toNumber():r.irVersion),r.producerName!=null&&r.hasOwnProperty("producerName")&&(a.producerName=r.producerName),r.producerVersion!=null&&r.hasOwnProperty("producerVersion")&&(a.producerVersion=r.producerVersion),r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.modelVersion!=null&&r.hasOwnProperty("modelVersion")&&(typeof r.modelVersion=="number"?a.modelVersion=i.longs===String?String(r.modelVersion):r.modelVersion:a.modelVersion=i.longs===String?l.Long.prototype.toString.call(r.modelVersion):i.longs===Number?new l.LongBits(r.modelVersion.low>>>0,r.modelVersion.high>>>0).toNumber():r.modelVersion),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.graph!=null&&r.hasOwnProperty("graph")&&(a.graph=d.onnx.GraphProto.toObject(r.graph,i)),r.opsetImport&&r.opsetImport.length){a.opsetImport=[];for(var h=0;h>>3){case 1:{p.key=r.string();break}case 2:{p.value=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.key!=null&&r.hasOwnProperty("key")&&!l.isString(r.key)?"key: string expected":r.value!=null&&r.hasOwnProperty("value")&&!l.isString(r.value)?"value: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.StringStringEntryProto)return r;var i=new d.onnx.StringStringEntryProto;return r.key!=null&&(i.key=String(r.key)),r.value!=null&&(i.value=String(r.value)),i},s.toObject=function(r,i){i||(i={});var a={};return i.defaults&&(a.key="",a.value=""),r.key!=null&&r.hasOwnProperty("key")&&(a.key=r.key),r.value!=null&&r.hasOwnProperty("value")&&(a.value=r.value),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.StringStringEntryProto"},s})(),c.TensorAnnotation=(function(){function s(r){if(this.quantParameterTensorNames=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.tensorName=r.string();break}case 2:{p.quantParameterTensorNames&&p.quantParameterTensorNames.length||(p.quantParameterTensorNames=[]),p.quantParameterTensorNames.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.tensorName!=null&&r.hasOwnProperty("tensorName")&&!l.isString(r.tensorName))return"tensorName: string expected";if(r.quantParameterTensorNames!=null&&r.hasOwnProperty("quantParameterTensorNames")){if(!Array.isArray(r.quantParameterTensorNames))return"quantParameterTensorNames: array expected";for(var i=0;i>>3){case 1:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 2:{p.name=r.string();break}case 5:{p.initializer&&p.initializer.length||(p.initializer=[]),p.initializer.push(d.onnx.TensorProto.decode(r,r.uint32()));break}case 15:{p.sparseInitializer&&p.sparseInitializer.length||(p.sparseInitializer=[]),p.sparseInitializer.push(d.onnx.SparseTensorProto.decode(r,r.uint32()));break}case 10:{p.docString=r.string();break}case 11:{p.input&&p.input.length||(p.input=[]),p.input.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 12:{p.output&&p.output.length||(p.output=[]),p.output.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 13:{p.valueInfo&&p.valueInfo.length||(p.valueInfo=[]),p.valueInfo.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 14:{p.quantizationAnnotation&&p.quantizationAnnotation.length||(p.quantizationAnnotation=[]),p.quantizationAnnotation.push(d.onnx.TensorAnnotation.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.node!=null&&r.hasOwnProperty("node")){if(!Array.isArray(r.node))return"node: array expected";for(var i=0;i>>3){case 1:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}if(r.dataType!=null&&(i.dataType=r.dataType|0),r.segment!=null){if(typeof r.segment!="object")throw TypeError(".onnx.TensorProto.segment: object expected");i.segment=d.onnx.TensorProto.Segment.fromObject(r.segment)}if(r.floatData){if(!Array.isArray(r.floatData))throw TypeError(".onnx.TensorProto.floatData: array expected");i.floatData=[];for(var a=0;a=0&&(i.stringData[a]=r.stringData[a])}if(r.int64Data){if(!Array.isArray(r.int64Data))throw TypeError(".onnx.TensorProto.int64Data: array expected");i.int64Data=[];for(var a=0;a>>0,r.int64Data[a].high>>>0).toNumber())}if(r.name!=null&&(i.name=String(r.name)),r.docString!=null&&(i.docString=String(r.docString)),r.rawData!=null&&(typeof r.rawData=="string"?l.base64.decode(r.rawData,i.rawData=l.newBuffer(l.base64.length(r.rawData)),0):r.rawData.length>=0&&(i.rawData=r.rawData)),r.externalData){if(!Array.isArray(r.externalData))throw TypeError(".onnx.TensorProto.externalData: array expected");i.externalData=[];for(var a=0;a>>0,r.uint64Data[a].high>>>0).toNumber(!0))}return i},s.toObject=function(r,i){i||(i={});var a={};if((i.arrays||i.defaults)&&(a.dims=[],a.floatData=[],a.int32Data=[],a.stringData=[],a.int64Data=[],a.doubleData=[],a.uint64Data=[],a.externalData=[]),i.defaults&&(a.dataType=0,a.segment=null,a.name="",i.bytes===String?a.rawData="":(a.rawData=[],i.bytes!==Array&&(a.rawData=l.newBuffer(a.rawData))),a.docString="",a.dataLocation=i.enums===String?"DEFAULT":0),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}if(r.dataType!=null&&r.hasOwnProperty("dataType")&&(a.dataType=r.dataType),r.segment!=null&&r.hasOwnProperty("segment")&&(a.segment=d.onnx.TensorProto.Segment.toObject(r.segment,i)),r.floatData&&r.floatData.length){a.floatData=[];for(var p=0;p>>0,r.int64Data[p].high>>>0).toNumber():r.int64Data[p]}if(r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.rawData!=null&&r.hasOwnProperty("rawData")&&(a.rawData=i.bytes===String?l.base64.encode(r.rawData,0,r.rawData.length):i.bytes===Array?Array.prototype.slice.call(r.rawData):r.rawData),r.doubleData&&r.doubleData.length){a.doubleData=[];for(var p=0;p>>0,r.uint64Data[p].high>>>0).toNumber(!0):r.uint64Data[p]}if(r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.externalData&&r.externalData.length){a.externalData=[];for(var p=0;p>>3){case 1:{h.begin=i.int64();break}case 2:{h.end=i.int64();break}default:i.skipType(m&7);break}}return h},r.decodeDelimited=function(i){return i instanceof o||(i=new o(i)),this.decode(i,i.uint32())},r.verify=function(i){return typeof i!="object"||i===null?"object expected":i.begin!=null&&i.hasOwnProperty("begin")&&!l.isInteger(i.begin)&&!(i.begin&&l.isInteger(i.begin.low)&&l.isInteger(i.begin.high))?"begin: integer|Long expected":i.end!=null&&i.hasOwnProperty("end")&&!l.isInteger(i.end)&&!(i.end&&l.isInteger(i.end.low)&&l.isInteger(i.end.high))?"end: integer|Long expected":null},r.fromObject=function(i){if(i instanceof d.onnx.TensorProto.Segment)return i;var a=new d.onnx.TensorProto.Segment;return i.begin!=null&&(l.Long?(a.begin=l.Long.fromValue(i.begin)).unsigned=!1:typeof i.begin=="string"?a.begin=parseInt(i.begin,10):typeof i.begin=="number"?a.begin=i.begin:typeof i.begin=="object"&&(a.begin=new l.LongBits(i.begin.low>>>0,i.begin.high>>>0).toNumber())),i.end!=null&&(l.Long?(a.end=l.Long.fromValue(i.end)).unsigned=!1:typeof i.end=="string"?a.end=parseInt(i.end,10):typeof i.end=="number"?a.end=i.end:typeof i.end=="object"&&(a.end=new l.LongBits(i.end.low>>>0,i.end.high>>>0).toNumber())),a},r.toObject=function(i,a){a||(a={});var p={};if(a.defaults){if(l.Long){var h=new l.Long(0,0,!1);p.begin=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.begin=a.longs===String?"0":0;if(l.Long){var h=new l.Long(0,0,!1);p.end=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.end=a.longs===String?"0":0}return i.begin!=null&&i.hasOwnProperty("begin")&&(typeof i.begin=="number"?p.begin=a.longs===String?String(i.begin):i.begin:p.begin=a.longs===String?l.Long.prototype.toString.call(i.begin):a.longs===Number?new l.LongBits(i.begin.low>>>0,i.begin.high>>>0).toNumber():i.begin),i.end!=null&&i.hasOwnProperty("end")&&(typeof i.end=="number"?p.end=a.longs===String?String(i.end):i.end:p.end=a.longs===String?l.Long.prototype.toString.call(i.end):a.longs===Number?new l.LongBits(i.end.low>>>0,i.end.high>>>0).toNumber():i.end),p},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(i){return i===void 0&&(i="type.googleapis.com"),i+"/onnx.TensorProto.Segment"},r})(),s.DataLocation=(function(){var r={},i=Object.create(r);return i[r[0]="DEFAULT"]=0,i[r[1]="EXTERNAL"]=1,i})(),s})(),c.SparseTensorProto=(function(){function s(r){if(this.dims=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.values=d.onnx.TensorProto.decode(r,r.uint32());break}case 2:{p.indices=d.onnx.TensorProto.decode(r,r.uint32());break}case 3:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}return i},s.toObject=function(r,i){i||(i={});var a={};if((i.arrays||i.defaults)&&(a.dims=[]),i.defaults&&(a.values=null,a.indices=null),r.values!=null&&r.hasOwnProperty("values")&&(a.values=d.onnx.TensorProto.toObject(r.values,i)),r.indices!=null&&r.hasOwnProperty("indices")&&(a.indices=d.onnx.TensorProto.toObject(r.indices,i)),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}return a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.SparseTensorProto"},s})(),c.TensorShapeProto=(function(){function s(r){if(this.dim=[],r)for(var i=Object.keys(r),a=0;a>>3===1?(p.dim&&p.dim.length||(p.dim=[]),p.dim.push(d.onnx.TensorShapeProto.Dimension.decode(r,r.uint32()))):r.skipType(h&7)}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.dim!=null&&r.hasOwnProperty("dim")){if(!Array.isArray(r.dim))return"dim: array expected";for(var i=0;i>>3){case 1:{m.dimValue=a.int64();break}case 2:{m.dimParam=a.string();break}case 3:{m.denotation=a.string();break}default:a.skipType(b&7);break}}return m},r.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},r.verify=function(a){if(typeof a!="object"||a===null)return"object expected";var p={};if(a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(p.value=1,!l.isInteger(a.dimValue)&&!(a.dimValue&&l.isInteger(a.dimValue.low)&&l.isInteger(a.dimValue.high))))return"dimValue: integer|Long expected";if(a.dimParam!=null&&a.hasOwnProperty("dimParam")){if(p.value===1)return"value: multiple values";if(p.value=1,!l.isString(a.dimParam))return"dimParam: string expected"}return a.denotation!=null&&a.hasOwnProperty("denotation")&&!l.isString(a.denotation)?"denotation: string expected":null},r.fromObject=function(a){if(a instanceof d.onnx.TensorShapeProto.Dimension)return a;var p=new d.onnx.TensorShapeProto.Dimension;return a.dimValue!=null&&(l.Long?(p.dimValue=l.Long.fromValue(a.dimValue)).unsigned=!1:typeof a.dimValue=="string"?p.dimValue=parseInt(a.dimValue,10):typeof a.dimValue=="number"?p.dimValue=a.dimValue:typeof a.dimValue=="object"&&(p.dimValue=new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber())),a.dimParam!=null&&(p.dimParam=String(a.dimParam)),a.denotation!=null&&(p.denotation=String(a.denotation)),p},r.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.denotation=""),a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(typeof a.dimValue=="number"?h.dimValue=p.longs===String?String(a.dimValue):a.dimValue:h.dimValue=p.longs===String?l.Long.prototype.toString.call(a.dimValue):p.longs===Number?new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber():a.dimValue,p.oneofs&&(h.value="dimValue")),a.dimParam!=null&&a.hasOwnProperty("dimParam")&&(h.dimParam=a.dimParam,p.oneofs&&(h.value="dimParam")),a.denotation!=null&&a.hasOwnProperty("denotation")&&(h.denotation=a.denotation),h},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TensorShapeProto.Dimension"},r})(),s})(),c.TypeProto=(function(){function s(i){if(i)for(var a=Object.keys(i),p=0;p>>3){case 1:{h.tensorType=d.onnx.TypeProto.Tensor.decode(i,i.uint32());break}case 4:{h.sequenceType=d.onnx.TypeProto.Sequence.decode(i,i.uint32());break}case 5:{h.mapType=d.onnx.TypeProto.Map.decode(i,i.uint32());break}case 9:{h.optionalType=d.onnx.TypeProto.Optional.decode(i,i.uint32());break}case 8:{h.sparseTensorType=d.onnx.TypeProto.SparseTensor.decode(i,i.uint32());break}case 6:{h.denotation=i.string();break}default:i.skipType(m&7);break}}return h},s.decodeDelimited=function(i){return i instanceof o||(i=new o(i)),this.decode(i,i.uint32())},s.verify=function(i){if(typeof i!="object"||i===null)return"object expected";var a={};if(i.tensorType!=null&&i.hasOwnProperty("tensorType")){a.value=1;{var p=d.onnx.TypeProto.Tensor.verify(i.tensorType);if(p)return"tensorType."+p}}if(i.sequenceType!=null&&i.hasOwnProperty("sequenceType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Sequence.verify(i.sequenceType);if(p)return"sequenceType."+p}}if(i.mapType!=null&&i.hasOwnProperty("mapType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Map.verify(i.mapType);if(p)return"mapType."+p}}if(i.optionalType!=null&&i.hasOwnProperty("optionalType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Optional.verify(i.optionalType);if(p)return"optionalType."+p}}if(i.sparseTensorType!=null&&i.hasOwnProperty("sparseTensorType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.SparseTensor.verify(i.sparseTensorType);if(p)return"sparseTensorType."+p}}return i.denotation!=null&&i.hasOwnProperty("denotation")&&!l.isString(i.denotation)?"denotation: string expected":null},s.fromObject=function(i){if(i instanceof d.onnx.TypeProto)return i;var a=new d.onnx.TypeProto;if(i.tensorType!=null){if(typeof i.tensorType!="object")throw TypeError(".onnx.TypeProto.tensorType: object expected");a.tensorType=d.onnx.TypeProto.Tensor.fromObject(i.tensorType)}if(i.sequenceType!=null){if(typeof i.sequenceType!="object")throw TypeError(".onnx.TypeProto.sequenceType: object expected");a.sequenceType=d.onnx.TypeProto.Sequence.fromObject(i.sequenceType)}if(i.mapType!=null){if(typeof i.mapType!="object")throw TypeError(".onnx.TypeProto.mapType: object expected");a.mapType=d.onnx.TypeProto.Map.fromObject(i.mapType)}if(i.optionalType!=null){if(typeof i.optionalType!="object")throw TypeError(".onnx.TypeProto.optionalType: object expected");a.optionalType=d.onnx.TypeProto.Optional.fromObject(i.optionalType)}if(i.sparseTensorType!=null){if(typeof i.sparseTensorType!="object")throw TypeError(".onnx.TypeProto.sparseTensorType: object expected");a.sparseTensorType=d.onnx.TypeProto.SparseTensor.fromObject(i.sparseTensorType)}return i.denotation!=null&&(a.denotation=String(i.denotation)),a},s.toObject=function(i,a){a||(a={});var p={};return a.defaults&&(p.denotation=""),i.tensorType!=null&&i.hasOwnProperty("tensorType")&&(p.tensorType=d.onnx.TypeProto.Tensor.toObject(i.tensorType,a),a.oneofs&&(p.value="tensorType")),i.sequenceType!=null&&i.hasOwnProperty("sequenceType")&&(p.sequenceType=d.onnx.TypeProto.Sequence.toObject(i.sequenceType,a),a.oneofs&&(p.value="sequenceType")),i.mapType!=null&&i.hasOwnProperty("mapType")&&(p.mapType=d.onnx.TypeProto.Map.toObject(i.mapType,a),a.oneofs&&(p.value="mapType")),i.denotation!=null&&i.hasOwnProperty("denotation")&&(p.denotation=i.denotation),i.sparseTensorType!=null&&i.hasOwnProperty("sparseTensorType")&&(p.sparseTensorType=d.onnx.TypeProto.SparseTensor.toObject(i.sparseTensorType,a),a.oneofs&&(p.value="sparseTensorType")),i.optionalType!=null&&i.hasOwnProperty("optionalType")&&(p.optionalType=d.onnx.TypeProto.Optional.toObject(i.optionalType,a),a.oneofs&&(p.value="optionalType")),p},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(i){return i===void 0&&(i="type.googleapis.com"),i+"/onnx.TypeProto"},s.Tensor=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(b&7);break}}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Tensor)return a;var p=new d.onnx.TypeProto.Tensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.Tensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Tensor"},i})(),s.Sequence=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(b&7)}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Sequence)return a;var p=new d.onnx.TypeProto.Sequence;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Sequence.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Sequence"},i})(),s.Map=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.keyType=a.int32();break}case 2:{m.valueType=d.onnx.TypeProto.decode(a,a.uint32());break}default:a.skipType(b&7);break}}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.keyType!=null&&a.hasOwnProperty("keyType")&&!l.isInteger(a.keyType))return"keyType: integer expected";if(a.valueType!=null&&a.hasOwnProperty("valueType")){var p=d.onnx.TypeProto.verify(a.valueType);if(p)return"valueType."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Map)return a;var p=new d.onnx.TypeProto.Map;if(a.keyType!=null&&(p.keyType=a.keyType|0),a.valueType!=null){if(typeof a.valueType!="object")throw TypeError(".onnx.TypeProto.Map.valueType: object expected");p.valueType=d.onnx.TypeProto.fromObject(a.valueType)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.keyType=0,h.valueType=null),a.keyType!=null&&a.hasOwnProperty("keyType")&&(h.keyType=a.keyType),a.valueType!=null&&a.hasOwnProperty("valueType")&&(h.valueType=d.onnx.TypeProto.toObject(a.valueType,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Map"},i})(),s.Optional=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(b&7)}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Optional)return a;var p=new d.onnx.TypeProto.Optional;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Optional.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Optional"},i})(),s.SparseTensor=(function(){function i(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(b&7);break}}return m},i.decodeDelimited=function(a){return a instanceof o||(a=new o(a)),this.decode(a,a.uint32())},i.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},i.fromObject=function(a){if(a instanceof d.onnx.TypeProto.SparseTensor)return a;var p=new d.onnx.TypeProto.SparseTensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.SparseTensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},i.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},i.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},i.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.SparseTensor"},i})(),s})(),c.OperatorSetIdProto=(function(){function s(r){if(r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.domain=r.string();break}case 2:{p.version=r.int64();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.domain!=null&&r.hasOwnProperty("domain")&&!l.isString(r.domain)?"domain: string expected":r.version!=null&&r.hasOwnProperty("version")&&!l.isInteger(r.version)&&!(r.version&&l.isInteger(r.version.low)&&l.isInteger(r.version.high))?"version: integer|Long expected":null},s.fromObject=function(r){if(r instanceof d.onnx.OperatorSetIdProto)return r;var i=new d.onnx.OperatorSetIdProto;return r.domain!=null&&(i.domain=String(r.domain)),r.version!=null&&(l.Long?(i.version=l.Long.fromValue(r.version)).unsigned=!1:typeof r.version=="string"?i.version=parseInt(r.version,10):typeof r.version=="number"?i.version=r.version:typeof r.version=="object"&&(i.version=new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber())),i},s.toObject=function(r,i){i||(i={});var a={};if(i.defaults)if(a.domain="",l.Long){var p=new l.Long(0,0,!1);a.version=i.longs===String?p.toString():i.longs===Number?p.toNumber():p}else a.version=i.longs===String?"0":0;return r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.version!=null&&r.hasOwnProperty("version")&&(typeof r.version=="number"?a.version=i.longs===String?String(r.version):r.version:a.version=i.longs===String?l.Long.prototype.toString.call(r.version):i.longs===Number?new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber():r.version),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.OperatorSetIdProto"},s})(),c.OperatorStatus=(function(){var s={},r=Object.create(s);return r[s[0]="EXPERIMENTAL"]=0,r[s[1]="STABLE"]=1,r})(),c.FunctionProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],this.attributeProto=[],this.node=[],this.opsetImport=[],r)for(var i=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 4:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 5:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 6:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(r.string());break}case 11:{p.attributeProto&&p.attributeProto.length||(p.attributeProto=[]),p.attributeProto.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 7:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 8:{p.docString=r.string();break}case 9:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 10:{p.domain=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof o||(r=new o(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var i=0;i{"use strict";Yy(),ze=ce(Ei()),En(),pi=class{static arraysEqual(e,t){if(e.length!==t.length)return!1;for(let n=0;n1&&i>1)return;c[d-s]=Math.max(r,i)}return c}static index(t,n){let o=new Array(n.length);return hi.fillIndex(t,n,o),o}static fillIndex(t,n,o){let u=t.length-n.length;for(let l=0;l=0;y--)r[y]=x%d[y],x=Math.floor(x/d[y]);m||(hi.fillIndex(r,t.dims,i),p=t.get(i)),b||(hi.fillIndex(r,n.dims,a),h=n.get(a)),s.set(r,o(p,h))}}return s}}static isValidBroadcast(t,n){let o=t.length,u=n.length;if(o>u)return!1;for(let l=1;l<=o;l++)if(t[o-l]!==1&&t[o-l]!==n[u-l])return!1;return!0}static getBroadcastDims(t,n){let o=t.length,u=[];for(let l=0;l1&&c===1&&u.unshift(d)}return u}},U0=class{static getShapeOfGemmResult(e,t,n,o,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(o?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!qt.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},ht=class Bu{static tensorDataTypeFromProto(t){switch(t){case ze.onnx.TensorProto.DataType.INT8:return"int8";case ze.onnx.TensorProto.DataType.UINT8:return"uint8";case ze.onnx.TensorProto.DataType.BOOL:return"bool";case ze.onnx.TensorProto.DataType.INT16:return"int16";case ze.onnx.TensorProto.DataType.UINT16:return"uint16";case ze.onnx.TensorProto.DataType.INT32:return"int32";case ze.onnx.TensorProto.DataType.UINT32:return"uint32";case ze.onnx.TensorProto.DataType.FLOAT:return"float32";case ze.onnx.TensorProto.DataType.DOUBLE:return"float64";case ze.onnx.TensorProto.DataType.STRING:return"string";case ze.onnx.TensorProto.DataType.INT64:return"int32";case ze.onnx.TensorProto.DataType.UINT64:return"uint32";default:throw new Error(`unsupported data type: ${ze.onnx.TensorProto.DataType[t]}`)}}static tensorDataTypeStringToEnum(t){switch(t){case"int8":return ze.onnx.TensorProto.DataType.INT8;case"uint8":return ze.onnx.TensorProto.DataType.UINT8;case"bool":return ze.onnx.TensorProto.DataType.BOOL;case"int16":return ze.onnx.TensorProto.DataType.INT16;case"uint16":return ze.onnx.TensorProto.DataType.UINT16;case"int32":return ze.onnx.TensorProto.DataType.INT32;case"uint32":return ze.onnx.TensorProto.DataType.UINT32;case"float32":return ze.onnx.TensorProto.DataType.FLOAT;case"float64":return ze.onnx.TensorProto.DataType.DOUBLE;case"string":return ze.onnx.TensorProto.DataType.STRING;case"int64":return ze.onnx.TensorProto.DataType.INT64;case"uint64":return ze.onnx.TensorProto.DataType.UINT64;default:throw new Error(`unsupported data type: ${t}`)}}static tensorDimsFromProto(t){return t.map(n=>nn.isLong(n)?n.toNumber():n)}static tensorValueTypeFromProto(t){return{tensorType:Bu.tensorDataTypeFromProto(t.elemType),shape:{dims:Bu.tensorDimsFromProto(t.shape.dim.map(n=>n.dimValue))}}}static tensorDimsFromORTFormat(t){let n=[];for(let o=0;ot.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return gr.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return gr.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,o){let u=1;for(let l=n;l=0;--u)o[u]=o[u+1]*t[u+1];return o}static transpose(t){return t.slice().reverse()}static indicesToOffset(t,n,o){o===void 0&&(o=t.length);let u=0;for(let l=0;l=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(o=>this.normalizeAxis(o,n))}static incrementIndex(t,n,o){if(n.length===0||t.length===0)throw new Error("Index incrementing unsupported for scalar Tensor");if(o===void 0)o=n.length;else if(o<=0||o>n.length)throw new Error("Incorrect axis to increment on");for(let u=o-1;u>=0&&(t[u]++,!(t[u]=t.length)throw new Error("the dimension with value zero exceeds the dimension size of the input tensor");u[s]=t[s]}else u[s]=n[s];d*=u[s]}}let c=gr.size(t);if(l!==-1){if(c%d!==0)throw new Error(`the input tensor cannot be reshaped to the requested shape. Input shape: [${t}] Output shape: [${n}]`);u[l]=c/d}else if(d!==c)throw new Error("reshapedDims and originalDims don't have matching sizes");return u}static sortBasedOnPerm(t,n){return n?n.map(o=>t[o]):t.slice().reverse()}static padShape(t,n){let o=t.length;return t.map((u,l)=>u+n[l]+n[l+o])}static areEqual(t,n){return t.length!==n.length?!1:t.every((o,u)=>o===n[u])}static validateDimsAndCalcSize(t){if(t.length>6)throw new TypeError("Only rank 0 to 6 is supported for tensor shape.");let n=1;for(let o of t){if(!Number.isInteger(o))throw new TypeError(`Invalid shape: ${o} is not an integer`);if(o<0||o>2147483647)throw new TypeError(`Invalid shape: length ${o} is not allowed`);n*=o}return n}static flattenShape(t,n){n<0&&(n+=t.length);let o=t.reduce((l,d)=>l*d,1),u=t.slice(n).reduce((l,d)=>l*d,1);return[o/u,u]}static squeezeShape(t,n){let o=new Array;n=gr.normalizeAxes(n,t.length);for(let u=0;u=0;if(l&&t[u]!==1)throw new Error("squeeze an axis of size different than 1");(n.length===0&&t[u]>1||n.length>0&&!l)&&o.push(t[u])}return o}static unsqueezeShape(t,n){let o=new Array(t.length+n.length);o.fill(0);for(let l=0;l=o.length)throw new Error("'axes' has an out of range axis");if(o[d]!==0)throw new Error("'axes' has a duplicate axis");o[d]=1}let u=0;for(let l=0;l=o.length?o.push(n[c+2]):o[c]=n[c+2];for(let c=0;c=o[c]||d[c+o.length]>=o[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,o,u,l,d){if(d){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let c=0;c=o+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,o,u,l,d,c,s,r=0){let i=o*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,br.computeOutputSize(t-i,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(o!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],br.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return br.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}},Ti=-34028234663852886e22,Si=34028234663852886e22});function sO(e){switch(e){case"bool":case"int8":case"uint8":return 1;case"int16":case"uint16":return 2;case"int32":case"uint32":case"float32":return 4;case"float64":return 8;default:throw new Error(`cannot calculate sizeof() on type ${e}`)}}function Dp(e){switch(e){case be.onnx.TensorProto.DataType.UINT8:case be.onnx.TensorProto.DataType.INT8:case be.onnx.TensorProto.DataType.BOOL:return 1;case be.onnx.TensorProto.DataType.UINT16:case be.onnx.TensorProto.DataType.INT16:return 2;case be.onnx.TensorProto.DataType.FLOAT:case be.onnx.TensorProto.DataType.INT32:case be.onnx.TensorProto.DataType.UINT32:return 4;case be.onnx.TensorProto.DataType.INT64:case be.onnx.TensorProto.DataType.DOUBLE:case be.onnx.TensorProto.DataType.UINT64:return 8;default:throw new Error(`cannot calculate sizeof() on type ${be.onnx.TensorProto.DataType[e]}`)}}function aO(e,t){return new(G0(t))(e)}function G0(e){switch(e){case"bool":case"uint8":return Uint8Array;case"int8":return Int8Array;case"int16":return Int16Array;case"uint16":return Uint16Array;case"int32":return Int32Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"float32":return Float32Array;case"float64":return Float64Array;default:throw new Error("unspecified error")}}function Mu(e,t){if(t===be.onnx.TensorProto.DataType.INT64||t===_o.TensorDataType.INT64){if(e.greaterThanOrEqual(2147483648)||e.lessThan(-2147483648))throw new TypeError("int64 is not supported")}else if(t===be.onnx.TensorProto.DataType.UINT32||t===_o.TensorDataType.UINT32||t===be.onnx.TensorProto.DataType.UINT64||t===_o.TensorDataType.UINT64){if(e.greaterThanOrEqual(4294967296)||e.lessThan(0))throw new TypeError("uint64 is not supported")}else throw new TypeError(`not a LONG type: ${be.onnx.TensorProto.DataType[t]}`);return e.toNumber()}function Rp(e,t,n){switch(t){case be.onnx.TensorProto.DataType.BOOL:case be.onnx.TensorProto.DataType.UINT8:return e.getUint8(n);case be.onnx.TensorProto.DataType.INT8:return e.getInt8(n);case be.onnx.TensorProto.DataType.UINT16:return e.getUint16(n,!0);case be.onnx.TensorProto.DataType.INT16:return e.getInt16(n,!0);case be.onnx.TensorProto.DataType.FLOAT:return e.getFloat32(n,!0);case be.onnx.TensorProto.DataType.INT32:return e.getInt32(n,!0);case be.onnx.TensorProto.DataType.UINT32:return e.getUint32(n,!0);case be.onnx.TensorProto.DataType.INT64:return Mu(nn.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!1),t);case be.onnx.TensorProto.DataType.DOUBLE:return e.getFloat64(n,!0);case be.onnx.TensorProto.DataType.UINT64:return Mu(nn.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!0),t);default:throw new Error(`cannot read from DataView for type ${be.onnx.TensorProto.DataType[t]}`)}}var Np,be,gt,En=R(()=>{"use strict";Np=ce(d3()),Yy(),Lo(),be=ce(Ei()),Pe(),gt=class vo{constructor(t,n,o,u,l,d=Np.Guid.create()){this.dims=t,this.type=n,this.dataProvider=o,this.asyncDataProvider=u,this.cache=l,this.dataId=d,this.size=he.validateDimsAndCalcSize(t);let c=this.size,s=o===void 0&&u===void 0&&l===void 0;if(l!==void 0&&l.length!==c)throw new RangeError("Input dims doesn't match data length.");if(n==="string"){if(l!==void 0&&(!Array.isArray(l)||!l.every(r=>typeof r=="string")))throw new TypeError("cache should be a string array");s&&(this.cache=new Array(c))}else{if(l!==void 0){let r=G0(n);if(!(l instanceof r))throw new TypeError(`cache should be type ${r.name}`)}if(s){let r=new ArrayBuffer(c*sO(n));this.cache=aO(r,n)}}}get data(){if(this.cache===void 0){let t=this.dataProvider(this.dataId);if(t.length!==this.size)throw new Error("Length of data provided by the Data Provider is inconsistent with the dims of this Tensor.");this.cache=t}return this.cache}get stringData(){if(this.type!=="string")throw new TypeError("data type is not string");return this.data}get integerData(){switch(this.type){case"uint8":case"int8":case"uint16":case"int16":case"int32":case"uint32":case"bool":return this.data;default:throw new TypeError("data type is not integer (uint8, int8, uint16, int16, int32, uint32, bool)")}}get floatData(){switch(this.type){case"float32":case"float64":return this.data;default:throw new TypeError("data type is not float (float32, float64)")}}get numberData(){if(this.type!=="string")return this.data;throw new TypeError("type cannot be non-number (string)")}get(t){return this.data[he.indicesToOffset(t,this.strides)]}set(t,n){this.data[he.indicesToOffset(t,this.strides)]=n}async getData(){return this.cache===void 0&&(this.cache=await this.asyncDataProvider(this.dataId)),this.cache}get strides(){return this._strides||(this._strides=he.computeStrides(this.dims)),this._strides}static fromProto(t){if(!t)throw new Error("cannot construct Value from an empty tensor");let n=ht.tensorDataTypeFromProto(t.dataType),o=ht.tensorDimsFromProto(t.dims),u=new vo(o,n);if(n==="string")t.stringData.forEach((l,d)=>{u.data[d]=Nu(l)});else if(t.rawData&&typeof t.rawData.byteLength=="number"&&t.rawData.byteLength>0){let l=u.data,d=new DataView(t.rawData.buffer,t.rawData.byteOffset,t.rawData.byteLength),c=Dp(t.dataType),s=t.rawData.byteLength/c;if(t.rawData.byteLength%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;r0){let l=u.data,d=new DataView(t.rawDataArray().buffer,t.rawDataArray().byteOffset,t.rawDataLength()),c=Dp(t.dataType()),s=t.rawDataLength()/c;if(t.rawDataLength()%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;rkp)throw new Error(`descriptors: ${i} rows; the GPU vote holds at most ${kp}`);let u={...ZI,...n};for(let[C,W]of Object.entries(u))if(W!==void 0&&(!Number.isInteger(W)||W<1))throw new Error(`gpuVoter: ${C} must be a positive integer`);if(u.chunkRows%u.tileRows!==0)throw new Error("gpuVoter: chunkRows must be a multiple of tileRows");let l=e.limits,d=Math.min(l.maxStorageBufferBindingSize,l.maxBufferSize),c=Math.floor(d/32/u.chunkRows)*u.chunkRows;n.maxRowsPerBuffer!==void 0&&(c=Math.min(c,Math.floor(n.maxRowsPerBuffer/u.chunkRows)*u.chunkRows));let s=l.maxComputeWorkgroupsPerDimension;if(c=Math.min(c,s*u.chunkRows),cl.maxComputeWorkgroupStorageSize)throw new Error("gpuVoter: tileRows exceeds the device's workgroup memory");if(u.workgroupSize>l.maxComputeInvocationsPerWorkgroup)throw new Error("gpuVoter: workgroupSize exceeds the device's limit");e.pushErrorScope("validation"),e.pushErrorScope("out-of-memory");let h=await e.createComputePipelineAsync({layout:"auto",compute:{module:e.createShaderModule({code:YI(u.workgroupSize,u.queriesPerInvocation,u.tileRows)}),entryPoint:"main"}}),m=await e.createComputePipelineAsync({layout:"auto",compute:{module:e.createShaderModule({code:JI}),entryPoint:"main"}}),g=[];for(let C=0;C0&&e.queue.writeBuffer(_,0,t,C*32,W*32);let D=e.createBuffer({size:32,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST});if(g.push({buffer:_,rows:W,base:C,firstChunk:C/u.chunkRows,chunks:Math.max(1,Math.ceil(W/u.chunkRows)),params:D}),W<=0)break}let v=e.createBuffer({size:p*32,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST}),w=e.createBuffer({size:p*r*8,usage:GPUBufferUsage.STORAGE}),y=e.createBuffer({size:p*8,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),T=e.createBuffer({size:p*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),S=e.createBuffer({size:16,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}),O=g.map(C=>e.createBindGroup({layout:h.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:C.buffer}},{binding:1,resource:{buffer:v}},{binding:2,resource:{buffer:w}},{binding:3,resource:{buffer:C.params}}]})),A=e.createBindGroup({layout:m.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:w}},{binding:1,resource:{buffer:y}},{binding:2,resource:{buffer:S}}]}),k=await e.popErrorScope(),I=await e.popErrorScope(),R=k??I;if(R)throw new Error(`gpuVoter: ${R.message}`);let M=!1,X=Promise.resolve();async function G(C,W,_,D){e.queue.writeBuffer(v,0,C.buffer,C.byteOffset,W*32);for(let K of g)e.queue.writeBuffer(K.params,0,new Uint32Array([W,K.rows,K.base,u.chunkRows,K.firstChunk,r,0,0]));e.queue.writeBuffer(S,0,new Uint32Array([W,r,0,0]));let V=e.createCommandEncoder(),q=V.beginComputePass();q.setPipeline(h),g.forEach((K,F)=>{q.setBindGroup(0,O[F]),q.dispatchWorkgroups(K.chunks,Math.ceil(W/o))}),q.setPipeline(m),q.setBindGroup(0,A),q.dispatchWorkgroups(Math.ceil(W/64)),q.end(),V.copyBufferToBuffer(y,0,T,0,W*8),e.queue.submit([V.finish()]),await T.mapAsync(GPUMapMode.READ,0,W*8);let H=new Uint32Array(T.getMappedRange(0,W*8).slice(0));T.unmap();for(let K=0;K>>ao),Q!==Pp&&(_.second[D+K]=Q&Us,_.dSecond[D+K]=Q>>>ao)}}return{knn2(C){let W=async()=>{if(M)throw new Error("gpuVoter: used after dispose");let D=WI(C),V=XI(D);if(i===0)return V;for(let q=0;q{}),_},dispose(){M||(M=!0,X.then(()=>{for(let C of g)C.buffer.destroy(),C.params.destroy();for(let C of[v,w,y,T,S])C.destroy()}))}}}var QI=Object.create,Vo=Object.defineProperty,e3=Object.getOwnPropertyDescriptor,t3=Object.getOwnPropertyNames,r3=Object.getPrototypeOf,n3=Object.prototype.hasOwnProperty,_u=(e=>typeof Br<"u"?Br:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof Br<"u"?Br:t)[n]}):e)(function(e){if(typeof Br<"u")return Br.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')}),N=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(i){throw n=[i],i}},se=(e,t)=>()=>{try{return t||e((t={exports:{}}).exports,t),t.exports}catch(n){throw t=0,n}},gn=(e,t)=>{for(var n in t)Vo(e,n,{get:t[n],enumerable:!0})},By=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of t3(t))!n3.call(e,u)&&u!==n&&Vo(e,u,{get:()=>t[u],enumerable:!(i=e3(t,u))||i.enumerable});return e},be=(e,t,n)=>(n=e!=null?QI(r3(e)):{},By(t||!e||!e.__esModule?Vo(n,"default",{value:e,enumerable:!0}):n,e)),Cn=e=>By(Vo({},"__esModule",{value:!0}),e),ti,fr,ln,Rp,Fy,jy=N(()=>{"use strict";ti=new Map,fr=[],ln=(e,t,n)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let i=ti.get(e);if(i===void 0)ti.set(e,{backend:t,priority:n});else{if(i.priority>n)return;if(i.priority===n&&i.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){let u=fr.indexOf(e);u!==-1&&fr.splice(u,1);for(let l=0;l{let t=ti.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let n=!!t.initPromise;try{return n||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(i){return n||(t.error=`${i}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},Fy=async e=>{let t=e.executionProviders||[],n=t.map(s=>typeof s=="string"?s:s.name),i=n.length===0?fr:n,u,l=[],d=new Set;for(let s of i){let r=await Rp(s);typeof r=="string"?l.push({name:s,err:r}):(u||(u=r),u===r&&d.add(s))}if(!u)throw new Error(`no available backend found. ERR: ${l.map(s=>`[${s.name}] ${s.err}`).join(", ")}`);for(let{name:s,err:r}of l)n.includes(s)&&console.warn(`removing requested execution provider "${s}" from session options because it is not available: ${r}`);let c=t.filter(s=>d.has(typeof s=="string"?s:s.name));return[u,new Proxy(e,{get:(s,r)=>r==="executionProviders"?c:Reflect.get(s,r)})]}}),i3=N(()=>{"use strict";jy()}),Ly,o3=N(()=>{"use strict";Ly="1.29.0"}),Vs,ot,Uy=N(()=>{"use strict";o3(),Vs="warning",ot={wasm:{},webgl:{},webgpu:{},versions:{common:Ly},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Vs=e}},get logLevel(){return Vs}},Object.defineProperty(ot,"logLevel",{enumerable:!0})}),me,s3=N(()=>{"use strict";Uy(),me=ot}),Vy,Gy,a3=N(()=>{"use strict";Vy=(e,t)=>{let n=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);n.width=e.dims[3],n.height=e.dims[2];let i=n.getContext("2d");if(i!=null){let u,l;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[3]):(u=e.dims[3],l=e.dims[2]);let d=t?.format!==void 0?t.format:"RGB",c=t?.norm,s,r;c===void 0||c.mean===void 0?s=[255,255,255,255]:typeof c.mean=="number"?s=[c.mean,c.mean,c.mean,c.mean]:(s=[c.mean[0],c.mean[1],c.mean[2],0],c.mean[3]!==void 0&&(s[3]=c.mean[3])),c===void 0||c.bias===void 0?r=[0,0,0,0]:typeof c.bias=="number"?r=[c.bias,c.bias,c.bias,c.bias]:(r=[c.bias[0],c.bias[1],c.bias[2],0],c.bias[3]!==void 0&&(r[3]=c.bias[3]));let o=l*u,a=0,p=o,h=o*2,m=-1;d==="RGBA"?(a=0,p=o,h=o*2,m=o*3):d==="RGB"?(a=0,p=o,h=o*2):d==="RBG"&&(a=0,h=o,p=o*2);for(let g=0;g{let n=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),i;if(n!=null){let u,l,d;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(u=e.dims[2],l=e.dims[1],d=e.dims[3]):(u=e.dims[3],l=e.dims[2],d=e.dims[1]);let c=t!==void 0&&t.format!==void 0?t.format:"RGB",s=t?.norm,r,o;s===void 0||s.mean===void 0?r=[255,255,255,255]:typeof s.mean=="number"?r=[s.mean,s.mean,s.mean,s.mean]:(r=[s.mean[0],s.mean[1],s.mean[2],255],s.mean[3]!==void 0&&(r[3]=s.mean[3])),s===void 0||s.bias===void 0?o=[0,0,0,0]:typeof s.bias=="number"?o=[s.bias,s.bias,s.bias,s.bias]:(o=[s.bias[0],s.bias[1],s.bias[2],0],s.bias[3]!==void 0&&(o[3]=s.bias[3]));let a=l*u;if(t!==void 0&&(t.format!==void 0&&d===4&&t.format!=="RGBA"||d===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let p=4,h=0,m=1,g=2,v=3,w=0,y=a,T=a*2,S=-1;c==="RGBA"?(w=0,y=a,T=a*2,S=a*3):c==="RGB"?(w=0,y=a,T=a*2):c==="RBG"&&(w=0,T=a,y=a*2),i=n.createImageData(u,l);for(let O=0;O{"use strict";Il(),uo=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:n,width:i}=t,u=t.norm??{mean:255,bias:0},l,d;typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:l=[u.mean[0],u.mean[1],u.mean[2],u.mean[3]??255],typeof u.bias=="number"?d=[u.bias,u.bias,u.bias,u.bias]:d=[u.bias[0],u.bias[1],u.bias[2],u.bias[3]??0];let c=t.format!==void 0?t.format:"RGBA",s=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",r=n*i,o=s==="RGBA"?new Float32Array(r*4):new Float32Array(r*3),a=4,p=0,h=1,m=2,g=3,v=0,w=r,y=r*2,T=-1;c==="RGB"&&(a=3,p=0,h=1,m=2,g=-1),s==="RGBA"?T=r*3:s==="RBG"?(v=0,y=r,w=r*2):s==="BGR"&&(y=0,w=r,v=r*2);for(let S=0;S{let n=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,i=typeof ImageData<"u"&&e instanceof ImageData,u=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,l=typeof e=="string",d,c=t??{},s=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},r=o=>typeof HTMLCanvasElement<"u"&&o instanceof HTMLCanvasElement||o instanceof OffscreenCanvas?o.getContext("2d"):null;if(n){let o=s();o.width=e.width,o.height=e.height;let a=r(o);if(a!=null){let p=e.height,h=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(p=t.resizedHeight,h=t.resizedWidth),t!==void 0){if(c=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");c.tensorFormat="RGBA",c.height=p,c.width=h}else c.tensorFormat="RGBA",c.height=p,c.width=h;a.drawImage(e,0,0),d=a.getImageData(0,0,h,p).data}else throw new Error("Can not access image data")}else if(i){let o,a;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(o=t.resizedHeight,a=t.resizedWidth):(o=e.height,a=e.width),t!==void 0&&(c=t),c.format="RGBA",c.height=o,c.width=a,t!==void 0){let p=s();p.width=a,p.height=o;let h=r(p);if(h!=null)h.putImageData(e,0,0),d=h.getImageData(0,0,a,o).data;else throw new Error("Can not access image data")}else d=e.data}else if(u){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let o=s();o.width=e.width,o.height=e.height;let a=r(o);if(a!=null){let p=e.height,h=e.width;return a.drawImage(e,0,0,h,p),d=a.getImageData(0,0,h,p).data,c.height=p,c.width=h,uo(d,c)}else throw new Error("Can not access image data")}else{if(l)return new Promise((o,a)=>{let p=s(),h=r(p);if(!e||!h)return a();let m=new Image;m.crossOrigin="Anonymous",m.src=e,m.onload=()=>{p.width=m.width,p.height=m.height,h.drawImage(m,0,0,p.width,p.height);let g=h.getImageData(0,0,p.width,p.height);c.height=p.height,c.width=p.width,o(uo(g.data,c))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(d!==void 0)return uo(d,c);throw new Error("Input data provided is not supported - aborted tensor creation")},Hy=(e,t)=>{let{width:n,height:i,download:u,dispose:l}=t,d=[1,i,n,4];return new wt({location:"texture",type:"float32",texture:e,dims:d,download:u,dispose:l})},Wy=(e,t)=>{let{dataType:n,dims:i,download:u,dispose:l}=t;return new wt({location:"gpu-buffer",type:n??"float32",gpuBuffer:e,dims:i,download:u,dispose:l})},Ky=(e,t)=>{let{dataType:n,dims:i,download:u,dispose:l}=t;return new wt({location:"ml-tensor",type:n??"float32",mlTensor:e,dims:i,download:u,dispose:l})},Xy=(e,t,n)=>new wt({location:"cpu-pinned",type:e,data:t,dims:n??[t.length]})}),nn,yi,Gs,Zy,l3=N(()=>{"use strict";nn=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),yi=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),Gs=!1,Zy=()=>{if(!Gs){Gs=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,n=globalThis.Float16Array,i=typeof n<"u"&&n.from;e&&(nn.set("int64",BigInt64Array),yi.set(BigInt64Array,"int64")),t&&(nn.set("uint64",BigUint64Array),yi.set(BigUint64Array,"uint64")),i?(nn.set("float16",n),yi.set(n,"float16")):nn.set("float16",Uint16Array)}}}),Yy,Jy,d3=N(()=>{"use strict";Il(),Yy=e=>{let t=1;for(let n=0;n{switch(e.location){case"cpu":return new wt(e.type,e.data,t);case"cpu-pinned":return new wt({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new wt({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new wt({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new wt({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),wt,Il=N(()=>{"use strict";a3(),u3(),l3(),d3(),wt=class{constructor(e,t,n){Zy();let i,u;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,i=e.type,u=e.dims,e.location){case"cpu-pinned":{let d=nn.get(i);if(!d)throw new TypeError(`unsupported type "${i}" to create tensor from pinned buffer`);if(!(e.data instanceof d))throw new TypeError(`buffer should be of type ${d.name}`);this.cpuData=e.data;break}case"texture":{if(i!=="float32")throw new TypeError(`unsupported type "${i}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint64"&&i!=="int8"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let d,c;if(typeof e=="string")if(i=e,c=n,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");d=t}else{let s=nn.get(e);if(s===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&s===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${s.name} as data.`);e==="uint64"||e==="int64"?d=s.from(t,BigInt):d=s.from(t)}else if(t instanceof s)d=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")d=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&s!==Uint16Array)d=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${i} tensor's data must be type of ${s}`)}else if(c=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let s=typeof e[0];if(s==="string")i="string",d=e;else if(s==="boolean")i="bool",d=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${s}.`)}else if(e instanceof Uint8ClampedArray)i="uint8",d=Uint8Array.from(e);else{let s=yi.get(e.constructor);if(s===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);i=s,d=e}if(c===void 0)c=[d.length];else if(!Array.isArray(c))throw new TypeError("A tensor's dims must be a number array");u=c,this.cpuData=d,this.dataLocation="cpu"}let l=Yy(u);if(this.cpuData&&l!==this.cpuData.length&&!((i==="uint4"||i==="int4")&&Math.ceil(l/2)===this.cpuData.length))throw new Error(`Tensor's size(${l}) does not match data length(${this.cpuData.length}).`);this.type=i,this.dims=u,this.size=l}static async fromImage(e,t){return qy(e,t)}static fromTexture(e,t){return Hy(e,t)}static fromGpuBuffer(e,t){return Wy(e,t)}static fromMLTensor(e,t){return Ky(e,t)}static fromPinnedBuffer(e,t,n){return Xy(e,t,n)}toDataURL(e){return Vy(this,e)}toImageData(e){return Gy(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return Jy(this,e)}}}),_t,Qy=N(()=>{"use strict";Il(),_t=wt}),Po,qs,Jt,Vt,dn,cn,e0=N(()=>{"use strict";Uy(),Po=(e,t)=>{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||console.timeStamp(`${e}::ORT::${t}`)},qs=(e,t)=>{let n=new Error().stack?.split(/\r\n|\r|\n/g)||[],i=!1;for(let u=0;u{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||qs("BEGIN",e)},Vt=e=>{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||qs("END",e)},dn=e=>{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||console.time(`ORT::${e}`)},cn=e=>{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||console.timeEnd(`ORT::${e}`)}}),t0,c3=N(()=>{"use strict";jy(),Qy(),e0(),t0=class r0{constructor(t){this.handler=t}async run(t,n,i){Jt(),dn("InferenceSession.run");let u={},l={};if(typeof t!="object"||t===null||t instanceof _t||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof n=="object"){if(n===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(n instanceof _t)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(n)){if(n.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let r of n){if(typeof r!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(r)===-1)throw new RangeError(`'fetches' contains invalid output name: ${r}.`);u[r]=null}if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let r=!1,o=Object.getOwnPropertyNames(n);for(let a of this.outputNames)if(o.indexOf(a)!==-1){let p=n[a];(p===null||p instanceof _t)&&(r=!0,d=!1,u[a]=p)}if(r){if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else l=n}}else if(typeof n<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let r of this.inputNames)if(typeof t[r]>"u")throw new Error(`input '${r}' is missing in 'feeds'.`);if(d)for(let r of this.outputNames)u[r]=null;let c=await this.handler.run(t,u,l),s={};for(let r in c)if(Object.hasOwnProperty.call(c,r)){let o=c[r];o instanceof _t?s[r]=o:s[r]=new _t(o.type,o.data,o.dims)}return cn("InferenceSession.run"),Vt(),s}async release(){return this.handler.dispose()}static async create(t,n,i,u){Jt(),dn("InferenceSession.create");let l,d={};if(typeof t=="string"){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(l=t,typeof n=="object"&&n!==null)d=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let o=t,a=0,p=t.byteLength;if(typeof n=="object"&&n!==null)d=n;else if(typeof n=="number"){if(a=n,!Number.isSafeInteger(a))throw new RangeError("'byteOffset' must be an integer.");if(a<0||a>=o.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${o.byteLength}).`);if(p=t.byteLength-a,typeof i=="number"){if(p=i,!Number.isSafeInteger(p))throw new RangeError("'byteLength' must be an integer.");if(p<=0||a+p>o.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${o.byteLength-a}].`);if(typeof u=="object"&&u!==null)d=u;else if(typeof u<"u")throw new TypeError("'options' must be an object.")}else if(typeof i<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof n<"u")throw new TypeError("'options' must be an object.");l=new Uint8Array(o,a,p)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[c,s]=await Fy(d),r=await c.createInferenceSessionHandler(l,s);return cn("InferenceSession.create"),Vt(),new r0(r)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),Go,p3=N(()=>{"use strict";c3(),Go=t0}),h3=N(()=>{"use strict"}),f3=N(()=>{"use strict"}),m3=N(()=>{"use strict"}),g3=N(()=>{"use strict"}),b3={};gn(b3,{InferenceSession:()=>Go,TRACE:()=>Po,TRACE_EVENT_BEGIN:()=>dn,TRACE_EVENT_END:()=>cn,TRACE_FUNC_BEGIN:()=>Jt,TRACE_FUNC_END:()=>Vt,Tensor:()=>_t,env:()=>me,registerBackend:()=>ln});var dt=N(()=>{"use strict";i3(),s3(),p3(),Qy(),h3(),f3(),e0(),m3(),g3()});function on(e,t,n,i){if(t===void 0)return y3(e);if(n===void 0)lo(e,t,1);else if(typeof n=="number"&&i===void 0)lo(e,t,n);else if(typeof n=="string"&&i===void 0)lo(e,n,1,t);else if(typeof n=="string"&&typeof i=="number")lo(e,n,i,t);else throw new TypeError("input is valid")}function y3(e){return{verbose:on.verbose.bind(null,e),info:on.info.bind(null,e),warning:on.warning.bind(null,e),error:on.error.bind(null,e),fatal:on.fatal.bind(null,e)}}function lo(e,t,n,i){let u=kn[i||""]||kn[""];vu[e]{"use strict";Np=class{log(e,t,n){}},zp=class{log(e,t,n){console.log(`${this.color(e)} ${n?"\x1B[35m"+n+"\x1B[0m ":""}${t}`)}color(e){switch(e){case"verbose":return"\x1B[34;40mv\x1B[0m";case"info":return"\x1B[32mi\x1B[0m";case"warning":return"\x1B[30;43mw\x1B[0m";case"error":return"\x1B[31;40me\x1B[0m";case"fatal":return"\x1B[101mf\x1B[0m";default:throw new Error(`unsupported severity: ${e}`)}}},vu={verbose:1e3,info:2e3,warning:4e3,error:5e3,fatal:6e3},n0={none:new Np,console:new zp},Hs={provider:"console",minimalSeverity:"warning",logDateTime:!0,logSourceLocation:!1},kn={"":Hs},(e=>{function t(r,o){e("verbose",r,o)}e.verbose=t;function n(r,o){e("info",r,o)}e.info=n;function i(r,o){e("warning",r,o)}e.warning=i;function u(r,o){e("error",r,o)}e.error=u;function l(r,o){e("fatal",r,o)}e.fatal=l;function d(r){kn={},c("",r||{})}e.reset=d;function c(r,o){if(r==="*")d(o);else{let a=kn[r]||Hs;kn[r]={provider:o.provider||a.provider,minimalSeverity:o.minimalSeverity||a.minimalSeverity,logDateTime:o.logDateTime===void 0?a.logDateTime:o.logDateTime,logSourceLocation:o.logSourceLocation===void 0?a.logSourceLocation:o.logSourceLocation}}}e.set=c;function s(r){let o={};r.logLevel&&(o.minimalSeverity=r.logLevel),c("",o)}e.setWithEnv=s})(on||={}),Ye=on,Ws=class{constructor(e,t,n,i,u,l){this.category=e,this.name=t,this.startTime=n,this.endCallback=i,this.timer=u,this.ctx=l}async end(){return this.endCallback(this)}async checkTimer(){if(this.ctx===void 0||this.timer===void 0)throw new Error("No webgl timer found");return this.ctx.endTimer(),this.ctx.waitForQueryAndGetTime(this.timer)}},Ks=class{constructor(e,t,n,i){this.category=e,this.name=t,this.startTime=n,this.endTime=i}},i0=class{constructor(e,t,n){this._started=!1,this._flushPointer=0,this._started=!1,this._maxNumberEvents=e===void 0?1e4:e,this._flushBatchSize=t===void 0?10:t,this._flushIntervalInMilliseconds=n===void 0?5e3:n}static create(e){return e===void 0?new this:new this(e.maxNumberEvents,e.flushBatchSize,e.flushIntervalInMilliseconds)}start(){this._started=!0,this._timingEvents=[],this._flushTime=ri(),this._flushPointer=0}stop(){for(this._started=!1;this._flushPointer{d.then(async r=>{u&&await u.end(),c(r)},async r=>{u&&await u.end(),s(r)})});if(!l&&u){let c=u.end();if(c&&typeof c.then=="function")return new Promise((s,r)=>{c.then(()=>{s(d)},o=>{r(o)})})}return d}begin(e,t,n){if(!this._started)throw new Error("profiler is not started yet");if(n===void 0){let i=ri();return this.flush(i),new Ws(e,t,i,u=>this.endSync(u))}else{let i=n.beginTimer();return new Ws(e,t,0,async u=>this.end(u),i,n)}}async end(e){let t=await e.checkTimer();this._timingEvents.length=this._flushBatchSize||e-this._flushTime>=this._flushIntervalInMilliseconds){for(let t=this._flushPointer;this._flushPointerperformance.now():Date.now});function w3(e,t,n){for(let i of n){let u=i[0],l=i[1],d=i[2],c=i[3],s=i[4];if(e.opType===u){for(let r of t)if((r.domain===l||r.domain==="ai.onnx"&&l==="")&&_3(r.version,d))return{opImpl:c,opInit:s}}}throw new TypeError(`cannot resolve operator '${e.opType}' with opsets: ${t.map(i=>`${i.domain||"ai.onnx"} v${i.version}`).join(", ")}`)}function _3(e,t){if(t.endsWith("+")){let n=Number.parseInt(t.substring(0,t.length-1),10);return!isNaN(n)&&n<=e}else if(t.split("-").length===2){let n=t.split("-"),i=Number.parseInt(n[0],10),u=Number.parseInt(n[1],10);return!isNaN(i)&&!isNaN(u)&&i<=e&&e<=u}else return Number.parseInt(t,10)===e}var v3=N(()=>{"use strict"}),x3=se(e=>{"use strict";e.__esModule=!0;var t=(function(){function n(i){if(!i)throw new TypeError("Invalid argument; `value` has no value.");this.value=n.EMPTY,i&&n.isGuid(i)&&(this.value=i)}return n.isGuid=function(i){var u=i.toString();return i&&(i instanceof n||n.validator.test(u))},n.create=function(){return new n([n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-"))},n.createEmpty=function(){return new n("emptyguid")},n.parse=function(i){return new n(i)},n.raw=function(){return[n.gen(2),n.gen(1),n.gen(1),n.gen(1),n.gen(3)].join("-")},n.gen=function(i){for(var u="",l=0;l>>=0,(u=0<=e&&e<256)&&(i=$u[e],i)?i:(n=Ce(e,0,!0),u&&($u[e]=n),n)):(e|=0,(u=-128<=e&&e<128)&&(i=xu[e],i)?i:(n=Ce(e,e<0?-1:0,!1),u&&(xu[e]=n),n))}function Dt(e,t){if(isNaN(e))return t?Zt:Ct;if(t){if(e<0)return Zt;if(e>=Tu)return Ou}else{if(e<=-Su)return pt;if(e+1>=Su)return Iu}return e<0?Dt(-e,t).neg():Ce(e%sn|0,e/sn|0,t)}function Ce(e,t,n){return new Me(e,t,n)}function Ol(e,t,n){if(e.length===0)throw Error("empty string");if(typeof t=="number"?(n=t,t=!1):t=!!t,e==="NaN"||e==="Infinity"||e==="+Infinity"||e==="-Infinity")return t?Zt:Ct;if(n=n||10,n<2||360)throw Error("interior hyphen");if(i===0)return Ol(e.substring(1),t,n).neg();for(var u=Dt(Ii(n,8)),l=Ct,d=0;d{mt=null;try{mt=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}Me.prototype.__isLong__,Object.defineProperty(Me.prototype,"__isLong__",{value:!0}),Me.isLong=lt,xu={},$u={},Me.fromInt=Fr,Me.fromNumber=Dt,Me.fromBits=Ce,Ii=Math.pow,Me.fromString=Ol,Me.fromValue=Et,Xs=65536,Bp=1<<24,sn=Xs*Xs,Tu=sn*sn,Su=Tu/2,Zs=Fr(Bp),Ct=Fr(0),Me.ZERO=Ct,Zt=Fr(0,!0),Me.UZERO=Zt,jr=Fr(1),Me.ONE=jr,Ys=Fr(1,!0),Me.UONE=Ys,co=Fr(-1),Me.NEG_ONE=co,Iu=Ce(-1,2147483647,!1),Me.MAX_VALUE=Iu,Ou=Ce(-1,-1,!0),Me.MAX_UNSIGNED_VALUE=Ou,pt=Ce(0,-2147483648,!1),Me.MIN_VALUE=pt,Z=Me.prototype,Z.toInt=function(){return this.unsigned?this.low>>>0:this.low},Z.toNumber=function(){return this.unsigned?(this.high>>>0)*sn+(this.low>>>0):this.high*sn+(this.low>>>0)},Z.toString=function(e){if(e=e||10,e<2||36>>0,r=s.toString(e);if(l=c,l.isZero())return r+d;for(;r.length<6;)r="0"+r;d=""+r+d}},Z.getHighBits=function(){return this.high},Z.getHighBitsUnsigned=function(){return this.high>>>0},Z.getLowBits=function(){return this.low},Z.getLowBitsUnsigned=function(){return this.low>>>0},Z.getNumBitsAbs=function(){if(this.isNegative())return this.eq(pt)?64:this.neg().getNumBitsAbs();for(var e=this.high!=0?this.high:this.low,t=31;t>0&&(e&1<>21;return e?this.unsigned?!1:e===-1&&!(this.low===0&&this.high===-2097152):!0},Z.isZero=function(){return this.high===0&&this.low===0},Z.eqz=Z.isZero,Z.isNegative=function(){return!this.unsigned&&this.high<0},Z.isPositive=function(){return this.unsigned||this.high>=0},Z.isOdd=function(){return(this.low&1)===1},Z.isEven=function(){return(this.low&1)===0},Z.equals=function(e){return lt(e)||(e=Et(e)),this.unsigned!==e.unsigned&&this.high>>>31===1&&e.high>>>31===1?!1:this.high===e.high&&this.low===e.low},Z.eq=Z.equals,Z.notEquals=function(e){return!this.eq(e)},Z.neq=Z.notEquals,Z.ne=Z.notEquals,Z.lessThan=function(e){return this.comp(e)<0},Z.lt=Z.lessThan,Z.lessThanOrEqual=function(e){return this.comp(e)<=0},Z.lte=Z.lessThanOrEqual,Z.le=Z.lessThanOrEqual,Z.greaterThan=function(e){return this.comp(e)>0},Z.gt=Z.greaterThan,Z.greaterThanOrEqual=function(e){return this.comp(e)>=0},Z.gte=Z.greaterThanOrEqual,Z.ge=Z.greaterThanOrEqual,Z.compare=function(e){if(lt(e)||(e=Et(e)),this.eq(e))return 0;var t=this.isNegative(),n=e.isNegative();return t&&!n?-1:!t&&n?1:this.unsigned?e.high>>>0>this.high>>>0||e.high===this.high&&e.low>>>0>this.low>>>0?-1:1:this.sub(e).isNegative()?-1:1},Z.comp=Z.compare,Z.negate=function(){return!this.unsigned&&this.eq(pt)?pt:this.not().add(jr)},Z.neg=Z.negate,Z.add=function(e){lt(e)||(e=Et(e));var t=this.high>>>16,n=this.high&65535,i=this.low>>>16,u=this.low&65535,l=e.high>>>16,d=e.high&65535,c=e.low>>>16,s=e.low&65535,r=0,o=0,a=0,p=0;return p+=u+s,a+=p>>>16,p&=65535,a+=i+c,o+=a>>>16,a&=65535,o+=n+d,r+=o>>>16,o&=65535,r+=t+l,r&=65535,Ce(a<<16|p,r<<16|o,this.unsigned)},Z.subtract=function(e){return lt(e)||(e=Et(e)),this.add(e.neg())},Z.sub=Z.subtract,Z.multiply=function(e){if(this.isZero())return this;if(lt(e)||(e=Et(e)),mt){var t=mt.mul(this.low,this.high,e.low,e.high);return Ce(t,mt.get_high(),this.unsigned)}if(e.isZero())return this.unsigned?Zt:Ct;if(this.eq(pt))return e.isOdd()?pt:Ct;if(e.eq(pt))return this.isOdd()?pt:Ct;if(this.isNegative())return e.isNegative()?this.neg().mul(e.neg()):this.neg().mul(e).neg();if(e.isNegative())return this.mul(e.neg()).neg();if(this.lt(Zs)&&e.lt(Zs))return Dt(this.toNumber()*e.toNumber(),this.unsigned);var n=this.high>>>16,i=this.high&65535,u=this.low>>>16,l=this.low&65535,d=e.high>>>16,c=e.high&65535,s=e.low>>>16,r=e.low&65535,o=0,a=0,p=0,h=0;return h+=l*r,p+=h>>>16,h&=65535,p+=u*r,a+=p>>>16,p&=65535,p+=l*s,a+=p>>>16,p&=65535,a+=i*r,o+=a>>>16,a&=65535,a+=u*s,o+=a>>>16,a&=65535,a+=l*c,o+=a>>>16,a&=65535,o+=n*r+i*s+u*c+l*d,o&=65535,Ce(p<<16|h,o<<16|a,this.unsigned)},Z.mul=Z.multiply,Z.divide=function(e){if(lt(e)||(e=Et(e)),e.isZero())throw Error("division by zero");if(mt){if(!this.unsigned&&this.high===-2147483648&&e.low===-1&&e.high===-1)return this;var t=(this.unsigned?mt.div_u:mt.div_s)(this.low,this.high,e.low,e.high);return Ce(t,mt.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?Zt:Ct;var n,i,u;if(this.unsigned){if(e.unsigned||(e=e.toUnsigned()),e.gt(this))return Zt;if(e.gt(this.shru(1)))return Ys;u=Zt}else{if(this.eq(pt)){if(e.eq(jr)||e.eq(co))return pt;if(e.eq(pt))return jr;var l=this.shr(1);return n=l.div(e).shl(1),n.eq(Ct)?e.isNegative()?jr:co:(i=this.sub(e.mul(n)),u=n.add(i.div(e)),u)}else if(e.eq(pt))return this.unsigned?Zt:Ct;if(this.isNegative())return e.isNegative()?this.neg().div(e.neg()):this.neg().div(e).neg();if(e.isNegative())return this.div(e.neg()).neg();u=Ct}for(i=this;i.gte(e);){n=Math.max(1,Math.floor(i.toNumber()/e.toNumber()));for(var d=Math.ceil(Math.log(n)/Math.LN2),c=d<=48?1:Ii(2,d-48),s=Dt(n),r=s.mul(e);r.isNegative()||r.gt(i);)n-=c,s=Dt(n,this.unsigned),r=s.mul(e);s.isZero()&&(s=jr),u=u.add(s),i=i.sub(r)}return u},Z.div=Z.divide,Z.modulo=function(e){if(lt(e)||(e=Et(e)),mt){var t=(this.unsigned?mt.rem_u:mt.rem_s)(this.low,this.high,e.low,e.high);return Ce(t,mt.get_high(),this.unsigned)}return this.sub(this.div(e).mul(e))},Z.mod=Z.modulo,Z.rem=Z.modulo,Z.not=function(){return Ce(~this.low,~this.high,this.unsigned)},Z.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},Z.clz=Z.countLeadingZeros,Z.countTrailingZeros=function(){return this.low?Mp(this.low):Mp(this.high)+32},Z.ctz=Z.countTrailingZeros,Z.and=function(e){return lt(e)||(e=Et(e)),Ce(this.low&e.low,this.high&e.high,this.unsigned)},Z.or=function(e){return lt(e)||(e=Et(e)),Ce(this.low|e.low,this.high|e.high,this.unsigned)},Z.xor=function(e){return lt(e)||(e=Et(e)),Ce(this.low^e.low,this.high^e.high,this.unsigned)},Z.shiftLeft=function(e){return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ce(this.low<>>32-e,this.unsigned):Ce(0,this.low<>>e|this.high<<32-e,this.high>>e,this.unsigned):Ce(this.high>>e-32,this.high>=0?0:-1,this.unsigned)},Z.shr=Z.shiftRight,Z.shiftRightUnsigned=function(e){return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?Ce(this.low>>>e|this.high<<32-e,this.high>>>e,this.unsigned):e===32?Ce(this.high,0,this.unsigned):Ce(this.high>>>e-32,0,this.unsigned)},Z.shru=Z.shiftRightUnsigned,Z.shr_u=Z.shiftRightUnsigned,Z.rotateLeft=function(e){var t;return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ce(this.high,this.low,this.unsigned):e<32?(t=32-e,Ce(this.low<>>t,this.high<>>t,this.unsigned)):(e-=32,t=32-e,Ce(this.high<>>t,this.low<>>t,this.unsigned))},Z.rotl=Z.rotateLeft,Z.rotateRight=function(e){var t;return lt(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?Ce(this.high,this.low,this.unsigned):e<32?(t=32-e,Ce(this.high<>>e,this.low<>>e,this.unsigned)):(e-=32,t=32-e,Ce(this.low<>>e,this.high<>>e,this.unsigned))},Z.rotr=Z.rotateRight,Z.toSigned=function(){return this.unsigned?Ce(this.low,this.high,!1):this},Z.toUnsigned=function(){return this.unsigned?this:Ce(this.low,this.high,!0)},Z.toBytes=function(e){return e?this.toBytesLE():this.toBytesBE()},Z.toBytesLE=function(){var e=this.high,t=this.low;return[t&255,t>>>8&255,t>>>16&255,t>>>24,e&255,e>>>8&255,e>>>16&255,e>>>24]},Z.toBytesBE=function(){var e=this.high,t=this.low;return[e>>>24,e>>>16&255,e>>>8&255,e&255,t>>>24,t>>>16&255,t>>>8&255,t&255]},Me.fromBytes=function(e,t,n){return n?Me.fromBytesLE(e,t):Me.fromBytesBE(e,t)},Me.fromBytesLE=function(e,t){return new Me(e[0]|e[1]<<8|e[2]<<16|e[3]<<24,e[4]|e[5]<<8|e[6]<<16|e[7]<<24,t)},Me.fromBytesBE=function(e,t){return new Me(e[4]<<24|e[5]<<16|e[6]<<8|e[7],e[0]<<24|e[1]<<16|e[2]<<8|e[3],t)},typeof BigInt=="function"&&(Me.fromBigInt=function(e,t){var n=Number(BigInt.asIntN(32,e)),i=Number(BigInt.asIntN(32,e>>BigInt(32)));return Ce(n,i,t)},Me.fromValue=function(e,t){return typeof e=="bigint"?Me.fromBigInt(e,t):Et(e,t)},Z.toBigInt=function(){var e=BigInt(this.low>>>0),t=BigInt(this.unsigned?this.high>>>0:this.high);return t<{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ArgType=void 0;var t;(function(n){n[n.INPUT=0]="INPUT",n[n.OUTPUT=1]="OUTPUT"})(t||(e.ArgType=t={}))}),El=se(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.SIZE_PREFIX_LENGTH=e.FILE_IDENTIFIER_LENGTH=e.SIZEOF_INT=e.SIZEOF_SHORT=void 0,e.SIZEOF_SHORT=2,e.SIZEOF_INT=4,e.FILE_IDENTIFIER_LENGTH=4,e.SIZE_PREFIX_LENGTH=4}),a0=se(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.isLittleEndian=e.float64=e.float32=e.int32=void 0,e.int32=new Int32Array(2),e.float32=new Float32Array(e.int32.buffer),e.float64=new Float64Array(e.int32.buffer),e.isLittleEndian=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1}),u0=se(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=void 0;var t;(function(n){n[n.UTF8_BYTES=1]="UTF8_BYTES",n[n.UTF16_STRING=2]="UTF16_STRING"})(t||(e.Encoding=t={}))}),l0=se(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ByteBuffer=void 0;var t=El(),n=u0(),i=a0(),u=class d0{constructor(d){this.bytes_=d,this.position_=0,this.text_decoder_=new TextDecoder}static allocate(d){return new d0(new Uint8Array(d))}clear(){this.position_=0}bytes(){return this.bytes_}position(){return this.position_}setPosition(d){this.position_=d}capacity(){return this.bytes_.length}readInt8(d){return this.readUint8(d)<<24>>24}readUint8(d){return this.bytes_[d]}readInt16(d){return this.readUint16(d)<<16>>16}readUint16(d){return this.bytes_[d]|this.bytes_[d+1]<<8}readInt32(d){return this.bytes_[d]|this.bytes_[d+1]<<8|this.bytes_[d+2]<<16|this.bytes_[d+3]<<24}readUint32(d){return this.readInt32(d)>>>0}readInt64(d){return BigInt.asIntN(64,BigInt(this.readUint32(d))+(BigInt(this.readUint32(d+4))<>8}writeUint16(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8}writeInt32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeUint32(d,c){this.bytes_[d]=c,this.bytes_[d+1]=c>>8,this.bytes_[d+2]=c>>16,this.bytes_[d+3]=c>>24}writeInt64(d,c){this.writeInt32(d,Number(BigInt.asIntN(32,c))),this.writeInt32(d+4,Number(BigInt.asIntN(32,c>>BigInt(32))))}writeUint64(d,c){this.writeUint32(d,Number(BigInt.asUintN(32,c))),this.writeUint32(d+4,Number(BigInt.asUintN(32,c>>BigInt(32))))}writeFloat32(d,c){i.float32[0]=c,this.writeInt32(d,i.int32[0])}writeFloat64(d,c){i.float64[0]=c,this.writeInt32(d,i.int32[i.isLittleEndian?0:1]),this.writeInt32(d+4,i.int32[i.isLittleEndian?1:0])}getBufferIdentifier(){if(this.bytes_.length{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Builder=void 0;var t=l0(),n=El(),i=class c0{constructor(l){this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null,this.text_encoder=new TextEncoder;let d;l?d=l:d=1024,this.bb=t.ByteBuffer.allocate(d),this.space=d}clear(){this.bb.clear(),this.space=this.bb.capacity(),this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1,this.string_maps=null}forceDefaults(l){this.force_defaults=l}dataBuffer(){return this.bb}asUint8Array(){return this.bb.bytes().subarray(this.bb.position(),this.bb.position()+this.offset())}prep(l,d){l>this.minalign&&(this.minalign=l);let c=~(this.bb.capacity()-this.space+d)+1&l-1;for(;this.space=0&&this.vtable[d]==0;d--);let c=d+1;for(;d>=0;d--)this.addInt16(this.vtable[d]!=0?l-this.vtable[d]:0);let s=2;this.addInt16(l-this.object_start);let r=(c+s)*n.SIZEOF_SHORT;this.addInt16(r);let o=0,a=this.space;e:for(d=0;d=0;o--)this.writeInt8(r.charCodeAt(o))}this.prep(this.minalign,n.SIZEOF_INT+s),this.addOffset(l),s&&this.addInt32(this.bb.capacity()-this.space),this.bb.setPosition(this.space)}finishSizePrefixed(l,d){this.finish(l,d,!0)}requiredField(l,d){let c=this.bb.capacity()-l,s=c-this.bb.readInt32(c);if(!(d{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Encoding=e.ByteBuffer=e.Builder=e.isLittleEndian=e.int32=e.float64=e.float32=e.SIZE_PREFIX_LENGTH=e.SIZEOF_SHORT=e.SIZEOF_INT=e.FILE_IDENTIFIER_LENGTH=void 0;var t=El();Object.defineProperty(e,"FILE_IDENTIFIER_LENGTH",{enumerable:!0,get:function(){return t.FILE_IDENTIFIER_LENGTH}}),Object.defineProperty(e,"SIZEOF_INT",{enumerable:!0,get:function(){return t.SIZEOF_INT}}),Object.defineProperty(e,"SIZEOF_SHORT",{enumerable:!0,get:function(){return t.SIZEOF_SHORT}}),Object.defineProperty(e,"SIZE_PREFIX_LENGTH",{enumerable:!0,get:function(){return t.SIZE_PREFIX_LENGTH}});var n=a0();Object.defineProperty(e,"float32",{enumerable:!0,get:function(){return n.float32}}),Object.defineProperty(e,"float64",{enumerable:!0,get:function(){return n.float64}}),Object.defineProperty(e,"int32",{enumerable:!0,get:function(){return n.int32}}),Object.defineProperty(e,"isLittleEndian",{enumerable:!0,get:function(){return n.isLittleEndian}});var i=$3();Object.defineProperty(e,"Builder",{enumerable:!0,get:function(){return i.Builder}});var u=l0();Object.defineProperty(e,"ByteBuffer",{enumerable:!0,get:function(){return u.ByteBuffer}});var l=u0();Object.defineProperty(e,"Encoding",{enumerable:!0,get:function(){return l.Encoding}})}),p0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.AttributeType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.INT=2]="INT",n[n.STRING=3]="STRING",n[n.TENSOR=4]="TENSOR",n[n.GRAPH=5]="GRAPH",n[n.FLOATS=6]="FLOATS",n[n.INTS=7]="INTS",n[n.STRINGS=8]="STRINGS",n[n.TENSORS=9]="TENSORS",n[n.GRAPHS=10]="GRAPHS",n[n.SPARSE_TENSOR=11]="SPARSE_TENSOR",n[n.SPARSE_TENSORS=12]="SPARSE_TENSORS"})(t||(e.AttributeType=t={}))}),f0=se(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=void 0;var t;(function(n){n[n.Primitive=0]="Primitive",n[n.Fused=1]="Fused"})(t||(e.NodeType=t={}))}),m0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(o[a]);return r.endVector()}static startInputsVector(r,o){r.startVector(4,o,4)}static addOutputs(r,o){r.addFieldOffset(9,o,0)}static createOutputsVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addOffset(o[a]);return r.endVector()}static startOutputsVector(r,o){r.startVector(4,o,4)}static addAttributes(r,o){r.addFieldOffset(10,o,0)}static createAttributesVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addOffset(o[a]);return r.endVector()}static startAttributesVector(r,o){r.startVector(4,o,4)}static addInputArgCounts(r,o){r.addFieldOffset(11,o,0)}static createInputArgCountsVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addInt32(o[a]);return r.endVector()}static startInputArgCountsVector(r,o){r.startVector(4,o,4)}static addImplicitInputs(r,o){r.addFieldOffset(12,o,0)}static createImplicitInputsVector(r,o){r.startVector(4,o.length,4);for(let a=o.length-1;a>=0;a--)r.addOffset(o[a]);return r.endVector()}static startImplicitInputsVector(r,o){r.startVector(4,o,4)}static endNode(r){return r.endObject()}static createNode(r,o,a,p,h,m,g,v,w,y,T,S,O,A){return it.startNode(r),it.addName(r,o),it.addDocString(r,a),it.addDomain(r,p),it.addSinceVersion(r,h),it.addIndex(r,m),it.addOpType(r,g),it.addType(r,v),it.addExecutionProviderType(r,w),it.addInputs(r,y),it.addOutputs(r,T),it.addAttributes(r,S),it.addInputArgCounts(r,O),it.addImplicitInputs(r,A),it.endNode(r)}};e.Node=c}),g0=se(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.EdgeEnd=void 0;var t=class{constructor(){this.bb=null,this.bb_pos=0}__init(n,i){return this.bb_pos=n,this.bb=i,this}nodeIndex(){return this.bb.readUint32(this.bb_pos)}srcArgIndex(){return this.bb.readInt32(this.bb_pos+4)}dstArgIndex(){return this.bb.readInt32(this.bb_pos+8)}static sizeOf(){return 12}static createEdgeEnd(n,i,u,l){return n.prep(4,12),n.writeInt32(l),n.writeInt32(u),n.writeInt32(i),n.offset()}};e.EdgeEnd=t}),b0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addNumInputs(c,s){c.addFieldInt32(1,s,0)}static addNumOutputs(c,s){c.addFieldInt32(2,s,0)}static addHasVariadicInput(c,s){c.addFieldInt8(3,+s,0)}static addHasVariadicOutput(c,s){c.addFieldInt8(4,+s,0)}static addNumVariadicInputs(c,s){c.addFieldInt32(5,s,0)}static addNumVariadicOutputs(c,s){c.addFieldInt32(6,s,0)}static endNodesToOptimizeIndices(c){return c.endObject()}static createNodesToOptimizeIndices(c,s,r,o,a,p,h,m){return kt.startNodesToOptimizeIndices(c),kt.addNodeIndices(c,s),kt.addNumInputs(c,r),kt.addNumOutputs(c,o),kt.addHasVariadicInput(c,a),kt.addHasVariadicOutput(c,p),kt.addNumVariadicInputs(c,h),kt.addNumVariadicOutputs(c,m),kt.endNodesToOptimizeIndices(c)}};e.NodesToOptimizeIndices=l}),w0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startProducedOpIdsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecord(s){return s.endObject()}};e.RuntimeOptimizationRecord=d}),_0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startRuntimeOptimizationRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizationRecordContainerEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createRuntimeOptimizationRecordContainerEntry(s,r,o){return Wr.startRuntimeOptimizationRecordContainerEntry(s),Wr.addOptimizerName(s,r),Wr.addRuntimeOptimizationRecords(s,o),Wr.endRuntimeOptimizationRecordContainerEntry(s)}};e.RuntimeOptimizationRecordContainerEntry=d}),v0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startRecordsVector(s,r){s.startVector(4,r,4)}static endRuntimeOptimizations(s){return s.endObject()}static createRuntimeOptimizations(s,r){return In.startRuntimeOptimizations(s),In.addRecords(s,r),In.endRuntimeOptimizations(s)}};e.RuntimeOptimizations=d}),qo=se(e=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TensorDataType=void 0;var t;(function(n){n[n.UNDEFINED=0]="UNDEFINED",n[n.FLOAT=1]="FLOAT",n[n.UINT8=2]="UINT8",n[n.INT8=3]="INT8",n[n.UINT16=4]="UINT16",n[n.INT16=5]="INT16",n[n.INT32=6]="INT32",n[n.INT64=7]="INT64",n[n.STRING=8]="STRING",n[n.BOOL=9]="BOOL",n[n.FLOAT16=10]="FLOAT16",n[n.DOUBLE=11]="DOUBLE",n[n.UINT32=12]="UINT32",n[n.UINT64=13]="UINT64",n[n.COMPLEX64=14]="COMPLEX64",n[n.COMPLEX128=15]="COMPLEX128",n[n.BFLOAT16=16]="BFLOAT16",n[n.FLOAT8E4M3FN=17]="FLOAT8E4M3FN",n[n.FLOAT8E4M3FNUZ=18]="FLOAT8E4M3FNUZ",n[n.FLOAT8E5M2=19]="FLOAT8E5M2",n[n.FLOAT8E5M2FNUZ=20]="FLOAT8E5M2FNUZ"})(t||(e.TensorDataType=t={}))}),Ho=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addInt64(r[o]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static addDataType(s,r){s.addFieldInt32(3,r,l.TensorDataType.UNDEFINED)}static addRawData(s,r){s.addFieldOffset(4,r,0)}static createRawDataVector(s,r){s.startVector(1,r.length,1);for(let o=r.length-1;o>=0;o--)s.addInt8(r[o]);return s.endVector()}static startRawDataVector(s,r){s.startVector(1,r,1)}static addStringData(s,r){s.addFieldOffset(5,r,0)}static createStringDataVector(s,r){s.startVector(4,r.length,4);for(let o=r.length-1;o>=0;o--)s.addOffset(r[o]);return s.endVector()}static startStringDataVector(s,r){s.startVector(4,r,4)}static addExternalDataOffset(s,r){s.addFieldInt64(6,r,BigInt("-1"))}static endTensor(s){return s.endObject()}static createTensor(s,r,o,a,p,h,m,g){return Pt.startTensor(s),Pt.addName(s,r),Pt.addDocString(s,o),Pt.addDims(s,a),Pt.addDataType(s,p),Pt.addRawData(s,h),Pt.addStringData(s,m),Pt.addExternalDataOffset(s,g),Pt.endTensor(s)}};e.Tensor=d}),x0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addInt64(r[o]);return s.endVector()}static startDimsVector(s,r){s.startVector(8,r,8)}static endSparseTensor(s){return s.endObject()}};e.SparseTensor=d}),$0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.DimensionValueType=void 0;var t;(function(n){n[n.UNKNOWN=0]="UNKNOWN",n[n.VALUE=1]="VALUE",n[n.PARAM=2]="PARAM"})(t||(e.DimensionValueType=t={}))}),I0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startDimVector(s,r){s.startVector(4,r,4)}static endShape(s){return s.endObject()}static createShape(s,r){return En.startShape(s),En.addDim(s,r),En.endShape(s)}};e.Shape=d}),A0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p{"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.TypeInfoValue=void 0,e.unionToTypeInfoValue=l,e.unionListToTypeInfoValue=d;var t=$0(),n=T0(),i=A0(),u;(function(c){c[c.NONE=0]="NONE",c[c.tensor_type=1]="tensor_type",c[c.sequence_type=2]="sequence_type",c[c.map_type=3]="map_type"})(u||(e.TypeInfoValue=u={}));function l(c,s){switch(u[c]){case"NONE":return null;case"tensor_type":return s(new i.TensorTypeAndShape);case"sequence_type":return s(new n.SequenceType);case"map_type":return s(new t.MapType);default:return null}}function d(c,s,r){switch(u[c]){case"NONE":return null;case"tensor_type":return s(r,new i.TensorTypeAndShape);case"sequence_type":return s(r,new n.SequenceType);case"map_type":return s(r,new t.MapType);default:return null}}}),Wo=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(p,h,m,g){g===void 0&&(g=m);var v=Object.getOwnPropertyDescriptor(h,m);(!v||("get"in v?!h.__esModule:v.writable||v.configurable))&&(v={enumerable:!0,get:function(){return h[m]}}),Object.defineProperty(p,g,v)}:function(p,h,m,g){g===void 0&&(g=m),p[g]=h[m]}),n=e&&e.__setModuleDefault||(Object.create?function(p,h){Object.defineProperty(p,"default",{enumerable:!0,value:h})}:function(p,h){p.default=h}),i=e&&e.__importStar||(function(){var p=function(h){return p=Object.getOwnPropertyNames||function(m){var g=[];for(var v in m)Object.prototype.hasOwnProperty.call(m,v)&&(g[g.length]=v);return g},p(h)};return function(h){if(h&&h.__esModule)return h;var m={};if(h!=null)for(var g=p(h),v=0;v=0;g--)h.addOffset(m[g]);return h.endVector()}static startInitializersVector(h,m){h.startVector(4,m,4)}static addNodeArgs(h,m){h.addFieldOffset(1,m,0)}static createNodeArgsVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startNodeArgsVector(h,m){h.startVector(4,m,4)}static addNodes(h,m){h.addFieldOffset(2,m,0)}static createNodesVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startNodesVector(h,m){h.startVector(4,m,4)}static addMaxNodeIndex(h,m){h.addFieldInt32(3,m,0)}static addNodeEdges(h,m){h.addFieldOffset(4,m,0)}static createNodeEdgesVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startNodeEdgesVector(h,m){h.startVector(4,m,4)}static addInputs(h,m){h.addFieldOffset(5,m,0)}static createInputsVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startInputsVector(h,m){h.startVector(4,m,4)}static addOutputs(h,m){h.addFieldOffset(6,m,0)}static createOutputsVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startOutputsVector(h,m){h.startVector(4,m,4)}static addSparseInitializers(h,m){h.addFieldOffset(7,m,0)}static createSparseInitializersVector(h,m){h.startVector(4,m.length,4);for(let g=m.length-1;g>=0;g--)h.addOffset(m[g]);return h.endVector()}static startSparseInitializersVector(h,m){h.startVector(4,m,4)}static addRuntimeOptimizations(h,m){h.addFieldOffset(8,m,0)}static endGraph(h){return h.endObject()}};e.Graph=a}),C0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,o,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(o,a);(!h||("get"in h?!o.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return o[a]}}),Object.defineProperty(r,p,h)}:function(r,o,a,p){p===void 0&&(p=a),r[p]=o[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,o){Object.defineProperty(r,"default",{enumerable:!0,value:o})}:function(r,o){r.default=o}),i=e&&e.__importStar||(function(){var r=function(o){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(o)};return function(o){if(o&&o.__esModule)return o;var a={};if(o!=null)for(var p=r(o),h=0;h=0;p--)o.addFloat32(a[p]);return o.endVector()}static startFloatsVector(o,a){o.startVector(4,a,4)}static addInts(o,a){o.addFieldOffset(9,a,0)}static createIntsVector(o,a){o.startVector(8,a.length,8);for(let p=a.length-1;p>=0;p--)o.addInt64(a[p]);return o.endVector()}static startIntsVector(o,a){o.startVector(8,a,8)}static addStrings(o,a){o.addFieldOffset(10,a,0)}static createStringsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startStringsVector(o,a){o.startVector(4,a,4)}static addTensors(o,a){o.addFieldOffset(11,a,0)}static createTensorsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startTensorsVector(o,a){o.startVector(4,a,4)}static addGraphs(o,a){o.addFieldOffset(12,a,0)}static createGraphsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startGraphsVector(o,a){o.startVector(4,a,4)}static endAttribute(o){return o.endObject()}};e.Attribute=s}),D0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o=0;r--)c.addInt32(s[r]);return c.endVector()}static startNodeIndicesVector(c,s){c.startVector(4,s,4)}static addKernelDefHashes(c,s){c.addFieldOffset(1,s,0)}static createKernelDefHashesVector(c,s){c.startVector(8,s.length,8);for(let r=s.length-1;r>=0;r--)c.addInt64(s[r]);return c.endVector()}static startKernelDefHashesVector(c,s){c.startVector(8,s,8)}static endDeprecatedKernelCreateInfos(c){return c.endObject()}static createDeprecatedKernelCreateInfos(c,s,r){return Xr.startDeprecatedKernelCreateInfos(c),Xr.addNodeIndices(c,s),Xr.addKernelDefHashes(c,r),Xr.endDeprecatedKernelCreateInfos(c)}};e.DeprecatedKernelCreateInfos=l}),T3=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p=0;a--)r.addOffset(o[a]);return r.endVector()}static startSubGraphSessionStatesVector(r,o){r.startVector(4,o,4)}static endDeprecatedSessionState(r){return r.endObject()}static createDeprecatedSessionState(r,o,a){return Yr.startDeprecatedSessionState(r),Yr.addKernels(r,o),Yr.addSubGraphSessionStates(r,a),Yr.endDeprecatedSessionState(r)}};e.DeprecatedSessionState=c}),z0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createKernelTypeStrArgsEntry(s,r,o){return Jr.startKernelTypeStrArgsEntry(s),Jr.addKernelTypeStr(s,r),Jr.addArgs(s,o),Jr.endKernelTypeStrArgsEntry(s)}};e.KernelTypeStrArgsEntry=d}),M0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endOpIdKernelTypeStrArgsEntry(s){let r=s.endObject();return s.requiredField(r,4),r}static createOpIdKernelTypeStrArgsEntry(s,r,o){return Qr.startOpIdKernelTypeStrArgsEntry(s),Qr.addOpId(s,r),Qr.addKernelTypeStrArgs(s,o),Qr.endOpIdKernelTypeStrArgsEntry(s)}};e.OpIdKernelTypeStrArgsEntry=d}),B0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(c,s,r,o){o===void 0&&(o=r);var a=Object.getOwnPropertyDescriptor(s,r);(!a||("get"in a?!s.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return s[r]}}),Object.defineProperty(c,o,a)}:function(c,s,r,o){o===void 0&&(o=r),c[o]=s[r]}),n=e&&e.__setModuleDefault||(Object.create?function(c,s){Object.defineProperty(c,"default",{enumerable:!0,value:s})}:function(c,s){c.default=s}),i=e&&e.__importStar||(function(){var c=function(s){return c=Object.getOwnPropertyNames||function(r){var o=[];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(o[o.length]=a);return o},c(s)};return function(s){if(s&&s.__esModule)return s;var r={};if(s!=null)for(var o=c(s),a=0;a=0;o--)s.addOffset(r[o]);return s.endVector()}static startOpKernelTypeStrArgsVector(s,r){s.startVector(4,r,4)}static endKernelTypeStrResolver(s){return s.endObject()}static createKernelTypeStrResolver(s,r){return An.startKernelTypeStrResolver(s),An.addOpKernelTypeStrArgs(s,r),An.endKernelTypeStrResolver(s)}};e.KernelTypeStrResolver=d}),F0=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o{"use strict";var t=e&&e.__createBinding||(Object.create?function(d,c,s,r){r===void 0&&(r=s);var o=Object.getOwnPropertyDescriptor(c,s);(!o||("get"in o?!c.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return c[s]}}),Object.defineProperty(d,r,o)}:function(d,c,s,r){r===void 0&&(r=s),d[r]=c[s]}),n=e&&e.__setModuleDefault||(Object.create?function(d,c){Object.defineProperty(d,"default",{enumerable:!0,value:c})}:function(d,c){d.default=c}),i=e&&e.__importStar||(function(){var d=function(c){return d=Object.getOwnPropertyNames||function(s){var r=[];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(r[r.length]=o);return r},d(c)};return function(c){if(c&&c.__esModule)return c;var s={};if(c!=null)for(var r=d(c),o=0;o{"use strict";var t=e&&e.__createBinding||(Object.create?function(r,o,a,p){p===void 0&&(p=a);var h=Object.getOwnPropertyDescriptor(o,a);(!h||("get"in h?!o.__esModule:h.writable||h.configurable))&&(h={enumerable:!0,get:function(){return o[a]}}),Object.defineProperty(r,p,h)}:function(r,o,a,p){p===void 0&&(p=a),r[p]=o[a]}),n=e&&e.__setModuleDefault||(Object.create?function(r,o){Object.defineProperty(r,"default",{enumerable:!0,value:o})}:function(r,o){r.default=o}),i=e&&e.__importStar||(function(){var r=function(o){return r=Object.getOwnPropertyNames||function(a){var p=[];for(var h in a)Object.prototype.hasOwnProperty.call(a,h)&&(p[p.length]=h);return p},r(o)};return function(o){if(o&&o.__esModule)return o;var a={};if(o!=null)for(var p=r(o),h=0;h=0;p--)o.addOffset(a[p]);return o.endVector()}static startOpsetImportVector(o,a){o.startVector(4,a,4)}static addProducerName(o,a){o.addFieldOffset(2,a,0)}static addProducerVersion(o,a){o.addFieldOffset(3,a,0)}static addDomain(o,a){o.addFieldOffset(4,a,0)}static addModelVersion(o,a){o.addFieldInt64(5,a,BigInt("0"))}static addDocString(o,a){o.addFieldOffset(6,a,0)}static addGraph(o,a){o.addFieldOffset(7,a,0)}static addGraphDocString(o,a){o.addFieldOffset(8,a,0)}static addMetadataProps(o,a){o.addFieldOffset(9,a,0)}static createMetadataPropsVector(o,a){o.startVector(4,a.length,4);for(let p=a.length-1;p>=0;p--)o.addOffset(a[p]);return o.endVector()}static startMetadataPropsVector(o,a){o.startVector(4,a,4)}static endModel(o){return o.endObject()}};e.Model=s}),S3=se(e=>{"use strict";var t=e&&e.__createBinding||(Object.create?function(s,r,o,a){a===void 0&&(a=o);var p=Object.getOwnPropertyDescriptor(r,o);(!p||("get"in p?!r.__esModule:p.writable||p.configurable))&&(p={enumerable:!0,get:function(){return r[o]}}),Object.defineProperty(s,a,p)}:function(s,r,o,a){a===void 0&&(a=o),s[a]=r[o]}),n=e&&e.__setModuleDefault||(Object.create?function(s,r){Object.defineProperty(s,"default",{enumerable:!0,value:r})}:function(s,r){s.default=r}),i=e&&e.__importStar||(function(){var s=function(r){return s=Object.getOwnPropertyNames||function(o){var a=[];for(var p in o)Object.prototype.hasOwnProperty.call(o,p)&&(a[a.length]=p);return a},s(r)};return function(r){if(r&&r.__esModule)return r;var o={};if(r!=null)for(var a=s(r),p=0;p{"use strict";I3=be(s0()),O3=be(p0()),Bu=be(C0()),Lt=be(h0()),E3=be(D0()),A3=be(T3()),k3=be(N0()),P3=be(R0()),C3=be(O0()),D3=be(I0()),R3=be(S0()),N3=be(g0()),U0=be(Al()),V0=be(S3()),z3=be(z0()),M3=be(B0()),B3=be($0()),F3=be(L0()),G0=be(m0()),j3=be(b0()),L3=be(f0()),U3=be(y0()),V3=be(M0()),G3=be(F0()),q3=be(w0()),H3=be(_0()),W3=be(v0()),K3=be(T0()),X3=be(E0()),Z3=be(x0()),Y3=be(j0()),J3=be(Ho()),Oo=be(qo()),q0=be(A0()),Q3=be(Wo()),H0=be(k0()),eO=be(P0())}),Ko=N(()=>{"use strict";tO()}),rO=se((e,t)=>{"use strict";t.exports=n;function n(i,u){for(var l=new Array(arguments.length-1),d=0,c=2,s=!0;c{"use strict";var t=e;t.length=function(d){var c=d.length;if(!c)return 0;for(var s=0;--c%4>1&&d.charAt(c)==="=";)++s;return Math.ceil(d.length*3)/4-s};var n=new Array(64),i=new Array(123);for(u=0;u<64;)i[n[u]=u<26?u+65:u<52?u+71:u<62?u-4:u-59|43]=u++;var u;t.encode=function(d,c,s){for(var r=null,o=[],a=0,p=0,h;c>2],h=(m&3)<<4,p=1;break;case 1:o[a++]=n[h|m>>4],h=(m&15)<<2,p=2;break;case 2:o[a++]=n[h|m>>6],o[a++]=n[m&63],p=0;break}a>8191&&((r||(r=[])).push(String.fromCharCode.apply(String,o)),a=0)}return p&&(o[a++]=n[h],o[a++]=61,p===1&&(o[a++]=61)),r?(a&&r.push(String.fromCharCode.apply(String,o.slice(0,a))),r.join("")):String.fromCharCode.apply(String,o.slice(0,a))};var l="invalid encoding";t.decode=function(d,c,s){for(var r=s,o=0,a,p=0;p1)break;if((h=i[h])===void 0)throw Error(l);switch(o){case 0:a=h,o=1;break;case 1:c[s++]=a<<2|(h&48)>>4,a=h,o=2;break;case 2:c[s++]=(a&15)<<4|(h&60)>>2,a=h,o=3;break;case 3:c[s++]=(a&3)<<6|h,o=0;break}}if(o===1)throw Error(l);return s-r},t.test=function(d){return/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(d)}}),iO=se((e,t)=>{"use strict";t.exports=n;function n(){this._listeners=Object.create(null)}n.prototype.on=function(i,u,l){return(this._listeners[i]||(this._listeners[i]=[])).push({fn:u,ctx:l||this}),this},n.prototype.off=function(i,u){if(i===void 0)this._listeners=Object.create(null);else if(u===void 0)this._listeners[i]=[];else{var l=this._listeners[i];if(!l)return this;for(var d=0;d{"use strict";t.exports=n(n);function n(c){return typeof Float32Array<"u"?(function(){var s=new Float32Array([-0]),r=new Uint8Array(s.buffer),o=r[3]===128;function a(g,v,w){s[0]=g,v[w]=r[0],v[w+1]=r[1],v[w+2]=r[2],v[w+3]=r[3]}function p(g,v,w){s[0]=g,v[w]=r[3],v[w+1]=r[2],v[w+2]=r[1],v[w+3]=r[0]}c.writeFloatLE=o?a:p,c.writeFloatBE=o?p:a;function h(g,v){return r[0]=g[v],r[1]=g[v+1],r[2]=g[v+2],r[3]=g[v+3],s[0]}function m(g,v){return r[3]=g[v],r[2]=g[v+1],r[1]=g[v+2],r[0]=g[v+3],s[0]}c.readFloatLE=o?h:m,c.readFloatBE=o?m:h})():(function(){function s(o,a,p,h){var m=a<0?1:0;if(m&&(a=-a),a===0)o(1/a>0?0:2147483648,p,h);else if(isNaN(a))o(2143289344,p,h);else if(a>34028234663852886e22)o((m<<31|2139095040)>>>0,p,h);else if(a<11754943508222875e-54)o((m<<31|Math.round(a/1401298464324817e-60))>>>0,p,h);else{var g=Math.floor(Math.log(a)/Math.LN2),v=Math.round(a*Math.pow(2,-g)*8388608)&8388607;o((m<<31|g+127<<23|v)>>>0,p,h)}}c.writeFloatLE=s.bind(null,i),c.writeFloatBE=s.bind(null,u);function r(o,a,p){var h=o(a,p),m=(h>>31)*2+1,g=h>>>23&255,v=h&8388607;return g===255?v?NaN:m*(1/0):g===0?m*1401298464324817e-60*v:m*Math.pow(2,g-150)*(v+8388608)}c.readFloatLE=r.bind(null,l),c.readFloatBE=r.bind(null,d)})(),typeof Float64Array<"u"?(function(){var s=new Float64Array([-0]),r=new Uint8Array(s.buffer),o=r[7]===128;function a(g,v,w){s[0]=g,v[w]=r[0],v[w+1]=r[1],v[w+2]=r[2],v[w+3]=r[3],v[w+4]=r[4],v[w+5]=r[5],v[w+6]=r[6],v[w+7]=r[7]}function p(g,v,w){s[0]=g,v[w]=r[7],v[w+1]=r[6],v[w+2]=r[5],v[w+3]=r[4],v[w+4]=r[3],v[w+5]=r[2],v[w+6]=r[1],v[w+7]=r[0]}c.writeDoubleLE=o?a:p,c.writeDoubleBE=o?p:a;function h(g,v){return r[0]=g[v],r[1]=g[v+1],r[2]=g[v+2],r[3]=g[v+3],r[4]=g[v+4],r[5]=g[v+5],r[6]=g[v+6],r[7]=g[v+7],s[0]}function m(g,v){return r[7]=g[v],r[6]=g[v+1],r[5]=g[v+2],r[4]=g[v+3],r[3]=g[v+4],r[2]=g[v+5],r[1]=g[v+6],r[0]=g[v+7],s[0]}c.readDoubleLE=o?h:m,c.readDoubleBE=o?m:h})():(function(){function s(o,a,p,h,m,g){var v=h<0?1:0;if(v&&(h=-h),h===0)o(0,m,g+a),o(1/h>0?0:2147483648,m,g+p);else if(isNaN(h))o(0,m,g+a),o(2146959360,m,g+p);else if(h>17976931348623157e292)o(0,m,g+a),o((v<<31|2146435072)>>>0,m,g+p);else{var w;if(h<22250738585072014e-324)w=h/5e-324,o(w>>>0,m,g+a),o((v<<31|w/4294967296)>>>0,m,g+p);else{var y=Math.floor(Math.log(h)/Math.LN2);y===1024&&(y=1023),w=h*Math.pow(2,-y),o(w*4503599627370496>>>0,m,g+a),o((v<<31|y+1023<<20|w*1048576&1048575)>>>0,m,g+p)}}}c.writeDoubleLE=s.bind(null,i,0,4),c.writeDoubleBE=s.bind(null,u,4,0);function r(o,a,p,h,m){var g=o(h,m+a),v=o(h,m+p),w=(v>>31)*2+1,y=v>>>20&2047,T=4294967296*(v&1048575)+g;return y===2047?T?NaN:w*(1/0):y===0?w*5e-324*T:w*Math.pow(2,y-1075)*(T+4503599627370496)}c.readDoubleLE=r.bind(null,l,0,4),c.readDoubleBE=r.bind(null,d,4,0)})(),c}function i(c,s,r){s[r]=c&255,s[r+1]=c>>>8&255,s[r+2]=c>>>16&255,s[r+3]=c>>>24}function u(c,s,r){s[r]=c>>>24,s[r+1]=c>>>16&255,s[r+2]=c>>>8&255,s[r+3]=c&255}function l(c,s){return(c[s]|c[s+1]<<8|c[s+2]<<16|c[s+3]<<24)>>>0}function d(c,s){return(c[s]<<24|c[s+1]<<16|c[s+2]<<8|c[s+3])>>>0}}),sO=se((e,t)=>{"use strict";t.exports=n;function n(i){try{if(typeof _u!="function")return null;var u=_u(i);return u&&(u.length||Object.keys(u).length)?u:null}catch{return null}}}),aO=se(e=>{"use strict";var t=e,n="\uFFFD";t.length=function(i){for(var u=0,l=0,d=0;d=192&&s<224){var r=(s&31)<<6|i[c++]&63;d+=r>=128?String.fromCharCode(r):n}else if(s>=224&&s<240){var o=(s&15)<<12|(i[c++]&63)<<6|i[c++]&63;d+=o>=2048?String.fromCharCode(o):n}else if(s>=240){var a=(s&7)<<18|(i[c++]&63)<<12|(i[c++]&63)<<6|i[c++]&63;a<65536||a>1114111?d+=n:(a-=65536,d+=String.fromCharCode(55296+(a>>10)),d+=String.fromCharCode(56320+(a&1023)))}}return d},t.write=function(i,u,l){for(var d=l,c,s,r=0;r>6|192,u[l++]=c&63|128):(c&64512)===55296&&((s=i.charCodeAt(r+1))&64512)===56320?(c=65536+((c&1023)<<10)+(s&1023),++r,u[l++]=c>>18|240,u[l++]=c>>12&63|128,u[l++]=c>>6&63|128,u[l++]=c&63|128):(u[l++]=c>>12|224,u[l++]=c>>6&63|128,u[l++]=c&63|128);return l-d}}),uO=se((e,t)=>{"use strict";t.exports=n;function n(i,u,l){var d=l||8192,c=d>>>1,s=null,r=d;return function(o){if(o<1||o>c)return i(o);r+o>d&&(s=i(d),r=0);var a=u.call(s,r,r+=o);return r&7&&(r=(r|7)+1),a}}}),lO=se((e,t)=>{"use strict";t.exports=i;var n=bn();function i(c,s){this.lo=c>>>0,this.hi=s>>>0}var u=i.zero=new i(0,0);u.toNumber=function(){return 0},u.zzEncode=u.zzDecode=function(){return this},u.length=function(){return 1};var l=i.zeroHash="\0\0\0\0\0\0\0\0";i.fromNumber=function(c){if(c===0)return u;var s=c<0;s&&(c=-c);var r=c>>>0,o=(c-r)/4294967296>>>0;return s&&(o=~o>>>0,r=~r>>>0,++r>4294967295&&(r=0,++o>4294967295&&(o=0))),new i(r,o)},i.from=function(c){if(typeof c=="number")return i.fromNumber(c);if(n.isString(c))if(n.Long)c=n.Long.fromString(c);else return i.fromNumber(parseInt(c,10));return c.low||c.high?new i(c.low>>>0,c.high>>>0):u},i.prototype.toNumber=function(c){if(!c&&this.hi>>>31){var s=~this.lo+1>>>0,r=~this.hi>>>0;return s||(r=r+1>>>0),-(s+r*4294967296)}return this.lo+this.hi*4294967296},i.prototype.toLong=function(c){return n.Long?new n.Long(this.lo|0,this.hi|0,!!c):{low:this.lo|0,high:this.hi|0,unsigned:!!c}};var d=String.prototype.charCodeAt;i.fromHash=function(c){return c===l?u:new i((d.call(c,0)|d.call(c,1)<<8|d.call(c,2)<<16|d.call(c,3)<<24)>>>0,(d.call(c,4)|d.call(c,5)<<8|d.call(c,6)<<16|d.call(c,7)<<24)>>>0)},i.prototype.toHash=function(){return String.fromCharCode(this.lo&255,this.lo>>>8&255,this.lo>>>16&255,this.lo>>>24,this.hi&255,this.hi>>>8&255,this.hi>>>16&255,this.hi>>>24)},i.prototype.zzEncode=function(){var c=this.hi>>31;return this.hi=((this.hi<<1|this.lo>>>31)^c)>>>0,this.lo=(this.lo<<1^c)>>>0,this},i.prototype.zzDecode=function(){var c=-(this.lo&1);return this.lo=((this.lo>>>1|this.hi<<31)^c)>>>0,this.hi=(this.hi>>>1^c)>>>0,this},i.prototype.length=function(){var c=this.lo,s=(this.lo>>>28|this.hi<<4)>>>0,r=this.hi>>>24;return r===0?s===0?c<16384?c<128?1:2:c<2097152?3:4:s<16384?s<128?5:6:s<2097152?7:8:r<128?9:10}}),dO=se((e,t)=>{(function(n,i){function u(l){return l.default||l}typeof define=="function"&&define.amd?define([],function(){var l={};return i(l),u(l)}):typeof e=="object"?(i(e),typeof t=="object"&&(t.exports=u(e))):(function(){var l={};i(l),n.Long=u(l)})()})(typeof globalThis<"u"?globalThis:typeof self<"u"?self:e,function(n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;var i=null;try{i=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}function u(_,D,V){this.low=_|0,this.high=D|0,this.unsigned=!!V}u.prototype.__isLong__,Object.defineProperty(u.prototype,"__isLong__",{value:!0});function l(_){return(_&&_.__isLong__)===!0}function d(_){var D=Math.clz32(_&-_);return _?31-D:D}u.isLong=l;var c={},s={};function r(_,D){var V,q,H;return D?(_>>>=0,(H=0<=_&&_<256)&&(q=s[_],q)?q:(V=a(_,0,!0),H&&(s[_]=V),V)):(_|=0,(H=-128<=_&&_<128)&&(q=c[_],q)?q:(V=a(_,_<0?-1:0,!1),H&&(c[_]=V),V))}u.fromInt=r;function o(_,D){if(isNaN(_))return D?A:O;if(D){if(_<0)return A;if(_>=y)return X}else{if(_<=-T)return G;if(_+1>=T)return M}return _<0?o(-_,D).neg():a(_%w|0,_/w|0,D)}u.fromNumber=o;function a(_,D,V){return new u(_,D,V)}u.fromBits=a;var p=Math.pow;function h(_,D,V){if(_.length===0)throw Error("empty string");if(typeof D=="number"?(V=D,D=!1):D=!!D,_==="NaN"||_==="Infinity"||_==="+Infinity"||_==="-Infinity")return D?A:O;if(V=V||10,V<2||360)throw Error("interior hyphen");if(q===0)return h(_.substring(1),D,V).neg();for(var H=o(p(V,8)),K=O,F=0;F<_.length;F+=8){var Q=Math.min(8,_.length-F),Y=parseInt(_.substring(F,F+Q),V);if(Q<8){var j=o(p(V,Q));K=K.mul(j).add(o(Y))}else K=K.mul(H),K=K.add(o(Y))}return K.unsigned=D,K}u.fromString=h;function m(_,D){return typeof _=="number"?o(_,D):typeof _=="string"?h(_,D):a(_.low,_.high,typeof D=="boolean"?D:_.unsigned)}u.fromValue=m;var g=65536,v=1<<24,w=g*g,y=w*w,T=y/2,S=r(v),O=r(0);u.ZERO=O;var A=r(0,!0);u.UZERO=A;var k=r(1);u.ONE=k;var I=r(1,!0);u.UONE=I;var R=r(-1);u.NEG_ONE=R;var M=a(-1,2147483647,!1);u.MAX_VALUE=M;var X=a(-1,-1,!0);u.MAX_UNSIGNED_VALUE=X;var G=a(0,-2147483648,!1);u.MIN_VALUE=G;var C=u.prototype;C.toInt=function(){return this.unsigned?this.low>>>0:this.low},C.toNumber=function(){return this.unsigned?(this.high>>>0)*w+(this.low>>>0):this.high*w+(this.low>>>0)},C.toString=function(_){if(_=_||10,_<2||36<_)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative())if(this.eq(G)){var D=o(_),V=this.div(D),q=V.mul(D).sub(this);return V.toString(_)+q.toInt().toString(_)}else return"-"+this.neg().toString(_);for(var H=o(p(_,6),this.unsigned),K=this,F="";;){var Q=K.div(H),Y=K.sub(Q.mul(H)).toInt()>>>0,j=Y.toString(_);if(K=Q,K.isZero())return j+F;for(;j.length<6;)j="0"+j;F=""+j+F}},C.getHighBits=function(){return this.high},C.getHighBitsUnsigned=function(){return this.high>>>0},C.getLowBits=function(){return this.low},C.getLowBitsUnsigned=function(){return this.low>>>0},C.getNumBitsAbs=function(){if(this.isNegative())return this.eq(G)?64:this.neg().getNumBitsAbs();for(var _=this.high!=0?this.high:this.low,D=31;D>0&&(_&1<>21;return _?this.unsigned?!1:_===-1&&!(this.low===0&&this.high===-2097152):!0},C.isZero=function(){return this.high===0&&this.low===0},C.eqz=C.isZero,C.isNegative=function(){return!this.unsigned&&this.high<0},C.isPositive=function(){return this.unsigned||this.high>=0},C.isOdd=function(){return(this.low&1)===1},C.isEven=function(){return(this.low&1)===0},C.equals=function(_){return l(_)||(_=m(_)),this.unsigned!==_.unsigned&&this.high>>>31===1&&_.high>>>31===1?!1:this.high===_.high&&this.low===_.low},C.eq=C.equals,C.notEquals=function(_){return!this.eq(_)},C.neq=C.notEquals,C.ne=C.notEquals,C.lessThan=function(_){return this.comp(_)<0},C.lt=C.lessThan,C.lessThanOrEqual=function(_){return this.comp(_)<=0},C.lte=C.lessThanOrEqual,C.le=C.lessThanOrEqual,C.greaterThan=function(_){return this.comp(_)>0},C.gt=C.greaterThan,C.greaterThanOrEqual=function(_){return this.comp(_)>=0},C.gte=C.greaterThanOrEqual,C.ge=C.greaterThanOrEqual,C.compare=function(_){if(l(_)||(_=m(_)),this.eq(_))return 0;var D=this.isNegative(),V=_.isNegative();return D&&!V?-1:!D&&V?1:this.unsigned?_.high>>>0>this.high>>>0||_.high===this.high&&_.low>>>0>this.low>>>0?-1:1:this.sub(_).isNegative()?-1:1},C.comp=C.compare,C.negate=function(){return!this.unsigned&&this.eq(G)?G:this.not().add(k)},C.neg=C.negate,C.add=function(_){l(_)||(_=m(_));var D=this.high>>>16,V=this.high&65535,q=this.low>>>16,H=this.low&65535,K=_.high>>>16,F=_.high&65535,Q=_.low>>>16,Y=_.low&65535,j=0,ee=0,ie=0,ue=0;return ue+=H+Y,ie+=ue>>>16,ue&=65535,ie+=q+Q,ee+=ie>>>16,ie&=65535,ee+=V+F,j+=ee>>>16,ee&=65535,j+=D+K,j&=65535,a(ie<<16|ue,j<<16|ee,this.unsigned)},C.subtract=function(_){return l(_)||(_=m(_)),this.add(_.neg())},C.sub=C.subtract,C.multiply=function(_){if(this.isZero())return this;if(l(_)||(_=m(_)),i){var D=i.mul(this.low,this.high,_.low,_.high);return a(D,i.get_high(),this.unsigned)}if(_.isZero())return this.unsigned?A:O;if(this.eq(G))return _.isOdd()?G:O;if(_.eq(G))return this.isOdd()?G:O;if(this.isNegative())return _.isNegative()?this.neg().mul(_.neg()):this.neg().mul(_).neg();if(_.isNegative())return this.mul(_.neg()).neg();if(this.lt(S)&&_.lt(S))return o(this.toNumber()*_.toNumber(),this.unsigned);var V=this.high>>>16,q=this.high&65535,H=this.low>>>16,K=this.low&65535,F=_.high>>>16,Q=_.high&65535,Y=_.low>>>16,j=_.low&65535,ee=0,ie=0,ue=0,ce=0;return ce+=K*j,ue+=ce>>>16,ce&=65535,ue+=H*j,ie+=ue>>>16,ue&=65535,ue+=K*Y,ie+=ue>>>16,ue&=65535,ie+=q*j,ee+=ie>>>16,ie&=65535,ie+=H*Y,ee+=ie>>>16,ie&=65535,ie+=K*Q,ee+=ie>>>16,ie&=65535,ee+=V*j+q*Y+H*Q+K*F,ee&=65535,a(ue<<16|ce,ee<<16|ie,this.unsigned)},C.mul=C.multiply,C.divide=function(_){if(l(_)||(_=m(_)),_.isZero())throw Error("division by zero");if(i){if(!this.unsigned&&this.high===-2147483648&&_.low===-1&&_.high===-1)return this;var D=(this.unsigned?i.div_u:i.div_s)(this.low,this.high,_.low,_.high);return a(D,i.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?A:O;var V,q,H;if(this.unsigned){if(_.unsigned||(_=_.toUnsigned()),_.gt(this))return A;if(_.gt(this.shru(1)))return I;H=A}else{if(this.eq(G)){if(_.eq(k)||_.eq(R))return G;if(_.eq(G))return k;var K=this.shr(1);return V=K.div(_).shl(1),V.eq(O)?_.isNegative()?k:R:(q=this.sub(_.mul(V)),H=V.add(q.div(_)),H)}else if(_.eq(G))return this.unsigned?A:O;if(this.isNegative())return _.isNegative()?this.neg().div(_.neg()):this.neg().div(_).neg();if(_.isNegative())return this.div(_.neg()).neg();H=O}for(q=this;q.gte(_);){V=Math.max(1,Math.floor(q.toNumber()/_.toNumber()));for(var F=Math.ceil(Math.log(V)/Math.LN2),Q=F<=48?1:p(2,F-48),Y=o(V),j=Y.mul(_);j.isNegative()||j.gt(q);)V-=Q,Y=o(V,this.unsigned),j=Y.mul(_);Y.isZero()&&(Y=k),H=H.add(Y),q=q.sub(j)}return H},C.div=C.divide,C.modulo=function(_){if(l(_)||(_=m(_)),i){var D=(this.unsigned?i.rem_u:i.rem_s)(this.low,this.high,_.low,_.high);return a(D,i.get_high(),this.unsigned)}return this.sub(this.div(_).mul(_))},C.mod=C.modulo,C.rem=C.modulo,C.not=function(){return a(~this.low,~this.high,this.unsigned)},C.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32},C.clz=C.countLeadingZeros,C.countTrailingZeros=function(){return this.low?d(this.low):d(this.high)+32},C.ctz=C.countTrailingZeros,C.and=function(_){return l(_)||(_=m(_)),a(this.low&_.low,this.high&_.high,this.unsigned)},C.or=function(_){return l(_)||(_=m(_)),a(this.low|_.low,this.high|_.high,this.unsigned)},C.xor=function(_){return l(_)||(_=m(_)),a(this.low^_.low,this.high^_.high,this.unsigned)},C.shiftLeft=function(_){return l(_)&&(_=_.toInt()),(_&=63)===0?this:_<32?a(this.low<<_,this.high<<_|this.low>>>32-_,this.unsigned):a(0,this.low<<_-32,this.unsigned)},C.shl=C.shiftLeft,C.shiftRight=function(_){return l(_)&&(_=_.toInt()),(_&=63)===0?this:_<32?a(this.low>>>_|this.high<<32-_,this.high>>_,this.unsigned):a(this.high>>_-32,this.high>=0?0:-1,this.unsigned)},C.shr=C.shiftRight,C.shiftRightUnsigned=function(_){return l(_)&&(_=_.toInt()),(_&=63)===0?this:_<32?a(this.low>>>_|this.high<<32-_,this.high>>>_,this.unsigned):_===32?a(this.high,0,this.unsigned):a(this.high>>>_-32,0,this.unsigned)},C.shru=C.shiftRightUnsigned,C.shr_u=C.shiftRightUnsigned,C.rotateLeft=function(_){var D;return l(_)&&(_=_.toInt()),(_&=63)===0?this:_===32?a(this.high,this.low,this.unsigned):_<32?(D=32-_,a(this.low<<_|this.high>>>D,this.high<<_|this.low>>>D,this.unsigned)):(_-=32,D=32-_,a(this.high<<_|this.low>>>D,this.low<<_|this.high>>>D,this.unsigned))},C.rotl=C.rotateLeft,C.rotateRight=function(_){var D;return l(_)&&(_=_.toInt()),(_&=63)===0?this:_===32?a(this.high,this.low,this.unsigned):_<32?(D=32-_,a(this.high<>>_,this.low<>>_,this.unsigned)):(_-=32,D=32-_,a(this.low<>>_,this.high<>>_,this.unsigned))},C.rotr=C.rotateRight,C.toSigned=function(){return this.unsigned?a(this.low,this.high,!1):this},C.toUnsigned=function(){return this.unsigned?this:a(this.low,this.high,!0)},C.toBytes=function(_){return _?this.toBytesLE():this.toBytesBE()},C.toBytesLE=function(){var _=this.high,D=this.low;return[D&255,D>>>8&255,D>>>16&255,D>>>24,_&255,_>>>8&255,_>>>16&255,_>>>24]},C.toBytesBE=function(){var _=this.high,D=this.low;return[_>>>24,_>>>16&255,_>>>8&255,_&255,D>>>24,D>>>16&255,D>>>8&255,D&255]},u.fromBytes=function(_,D,V){return V?u.fromBytesLE(_,D):u.fromBytesBE(_,D)},u.fromBytesLE=function(_,D){return new u(_[0]|_[1]<<8|_[2]<<16|_[3]<<24,_[4]|_[5]<<8|_[6]<<16|_[7]<<24,D)},u.fromBytesBE=function(_,D){return new u(_[4]<<24|_[5]<<16|_[6]<<8|_[7],_[0]<<24|_[1]<<16|_[2]<<8|_[3],D)},typeof BigInt=="function"&&(u.fromBigInt=function(_,D){var V=Number(BigInt.asIntN(32,_)),q=Number(BigInt.asIntN(32,_>>BigInt(32)));return a(V,q,D)},u.fromValue=function(_,D){return typeof _=="bigint"?u.fromBigInt(_,D):m(_,D)},C.toBigInt=function(){var _=BigInt(this.low>>>0),D=BigInt(this.unsigned?this.high>>>0:this.high);return D<{"use strict";var t=e;t.asPromise=rO(),t.base64=nO(),t.EventEmitter=iO(),t.float=oO(),t.inquire=sO(),t.utf8=aO(),t.pool=uO(),t.LongBits=lO();function n(l){return l==="__proto__"||l==="prototype"||l==="constructor"}t.isUnsafeProperty=n,t.isNode=!!(typeof global<"u"&&global&&global.process&&global.process.versions&&global.process.versions.node),t.global=t.isNode&&global||typeof window<"u"&&window||typeof self<"u"&&self||e,t.emptyArray=Object.freeze?Object.freeze([]):[],t.emptyObject=Object.freeze?Object.freeze({}):{},t.isInteger=Number.isInteger||function(l){return typeof l=="number"&&isFinite(l)&&Math.floor(l)===l},t.isString=function(l){return typeof l=="string"||l instanceof String},t.isObject=function(l){return l&&typeof l=="object"},t.isset=t.isSet=function(l,d){var c=l[d];return c!=null&&Object.hasOwnProperty.call(l,d)?typeof c!="object"||(Array.isArray(c)?c.length:Object.keys(c).length)>0:!1},t.Buffer=(function(){try{var l=t.global.Buffer;return l.prototype.utf8Write?l:null}catch{return null}})(),t._Buffer_from=null,t._Buffer_allocUnsafe=null,t.newBuffer=function(l){return typeof l=="number"?t.Buffer?t._Buffer_allocUnsafe(l):new t.Array(l):t.Buffer?t._Buffer_from(l):typeof Uint8Array>"u"?l:new Uint8Array(l)},t.Array=typeof Uint8Array<"u"?Uint8Array:Array,t.Long=t.global.dcodeIO&&t.global.dcodeIO.Long||t.global.Long||(function(){try{var l=dO();return l&&l.isLong?l:null}catch{return null}})(),t.key2Re=/^true|false|0|1$/,t.key32Re=/^-?(?:0|[1-9][0-9]*)$/,t.key64Re=/^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/,t.longToHash=function(l){return l?t.LongBits.from(l).toHash():t.LongBits.zeroHash},t.longFromHash=function(l,d){var c=t.LongBits.fromHash(l);return t.Long?t.Long.fromBits(c.lo,c.hi,d):c.toNumber(!!d)};function i(l){var d=typeof arguments[arguments.length-1]=="boolean",c=d?arguments.length-1:arguments.length;d=d&&arguments[arguments.length-1];for(var s=1;s-1;--r)if(d[s[r]]===1&&this[s[r]]!==void 0&&this[s[r]]!==null)return s[r]}},t.oneOfSetter=function(l){return function(d){for(var c=0;c{"use strict";t.exports=o;var n=bn(),i,u=n.LongBits,l=n.base64,d=n.utf8;function c(y,T,S){this.fn=y,this.len=T,this.next=void 0,this.val=S}function s(){}function r(y){this.head=y.head,this.tail=y.tail,this.len=y.len,this.next=y.states}function o(){this.len=0,this.head=new c(s,0,0),this.tail=this.head,this.states=null}var a=function(){return n.Buffer?function(){return(o.create=function(){return new i})()}:function(){return new o}};o.create=a(),o.alloc=function(y){return new n.Array(y)},n.Array!==Array&&(o.alloc=n.pool(o.alloc,n.Array.prototype.subarray)),o.prototype._push=function(y,T,S){return this.tail=this.tail.next=new c(y,T,S),this.len+=T,this};function p(y,T,S){T[S]=y&255}function h(y,T,S){for(;y>127;)T[S++]=y&127|128,y>>>=7;T[S]=y}function m(y,T){this.len=y,this.next=void 0,this.val=T}m.prototype=Object.create(c.prototype),m.prototype.fn=h,o.prototype.uint32=function(y){return this.len+=(this.tail=this.tail.next=new m((y=y>>>0)<128?1:y<16384?2:y<2097152?3:y<268435456?4:5,y)).len,this},o.prototype.int32=function(y){return(y|=0)<0?this._push(g,10,u.fromNumber(y)):this.uint32(y)},o.prototype.sint32=function(y){return this.uint32((y<<1^y>>31)>>>0)};function g(y,T,S){for(var O=y.lo,A=y.hi;A;)T[S++]=O&127|128,O=(O>>>7|A<<25)>>>0,A>>>=7;for(;O>127;)T[S++]=O&127|128,O=O>>>7;T[S++]=O}o.prototype.uint64=function(y){var T=u.from(y);return this._push(g,T.length(),T)},o.prototype.int64=o.prototype.uint64,o.prototype.sint64=function(y){var T=u.from(y).zzEncode();return this._push(g,T.length(),T)},o.prototype.bool=function(y){return this._push(p,1,y?1:0)};function v(y,T,S){T[S]=y&255,T[S+1]=y>>>8&255,T[S+2]=y>>>16&255,T[S+3]=y>>>24}o.prototype.fixed32=function(y){return this._push(v,4,y>>>0)},o.prototype.sfixed32=o.prototype.fixed32,o.prototype.fixed64=function(y){var T=u.from(y);return this._push(v,4,T.lo)._push(v,4,T.hi)},o.prototype.sfixed64=o.prototype.fixed64,o.prototype.float=function(y){return this._push(n.float.writeFloatLE,4,y)},o.prototype.double=function(y){return this._push(n.float.writeDoubleLE,8,y)};var w=n.Array.prototype.set?function(y,T,S){T.set(y,S)}:function(y,T,S){for(var O=0;O>>0;if(!T)return this._push(p,1,0);if(n.isString(y)){var S=o.alloc(T=l.length(y));l.decode(y,S,0),y=S}return this.uint32(T)._push(w,T,y)},o.prototype.string=function(y){var T=d.length(y);return T?this.uint32(T)._push(d.write,T,y):this._push(p,1,0)},o.prototype.fork=function(){return this.states=new r(this),this.head=this.tail=new c(s,0,0),this.len=0,this},o.prototype.reset=function(){return this.states?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new c(s,0,0),this.len=0),this},o.prototype.ldelim=function(){var y=this.head,T=this.tail,S=this.len;return this.reset().uint32(S),S&&(this.tail.next=y.next,this.tail=T,this.len+=S),this},o.prototype.finish=function(){for(var y=this.head.next,T=this.constructor.alloc(this.len),S=0;y;)y.fn(y.val,T,S),S+=y.len,y=y.next;return T},o._configure=function(y){i=y,o.create=a(),i._configure()}}),cO=se((e,t)=>{"use strict";t.exports=u;var n=W0();(u.prototype=Object.create(n.prototype)).constructor=u;var i=bn();function u(){n.call(this)}u._configure=function(){u.alloc=i._Buffer_allocUnsafe,u.writeBytesBuffer=i.Buffer&&i.Buffer.prototype instanceof Uint8Array&&i.Buffer.prototype.set.name==="set"?function(d,c,s){c.set(d,s)}:function(d,c,s){if(d.copy)d.copy(c,s,0,d.length);else for(var r=0;r>>0;return this.uint32(c),c&&this._push(u.writeBytesBuffer,c,d),this};function l(d,c,s){d.length<40?i.utf8.write(d,c,s):c.utf8Write?c.utf8Write(d,s):c.write(d,s)}u.prototype.string=function(d){var c=i.Buffer.byteLength(d);return this.uint32(c),c&&this._push(l,c,d),this},u._configure()}),K0=se((e,t)=>{"use strict";t.exports=c;var n=bn(),i,u=n.LongBits,l=n.utf8;function d(h,m){return RangeError("index out of range: "+h.pos+" + "+(m||1)+" > "+h.len)}function c(h){this.buf=h,this.pos=0,this.len=h.length}var s=typeof Uint8Array<"u"?function(h){if(h instanceof Uint8Array||Array.isArray(h))return new c(h);throw Error("illegal buffer")}:function(h){if(Array.isArray(h))return new c(h);throw Error("illegal buffer")},r=function(){return n.Buffer?function(h){return(c.create=function(m){return n.Buffer.isBuffer(m)?new i(m):s(m)})(h)}:s};c.create=r(),c.prototype._slice=n.Array.prototype.subarray||n.Array.prototype.slice,c.prototype.uint32=(function(){var h=4294967295;return function(){if(h=(this.buf[this.pos]&127)>>>0,this.buf[this.pos++]<128||(h=(h|(this.buf[this.pos]&127)<<7)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<14)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&127)<<21)>>>0,this.buf[this.pos++]<128)||(h=(h|(this.buf[this.pos]&15)<<28)>>>0,this.buf[this.pos++]<128))return h;if((this.pos+=5)>this.len)throw this.pos=this.len,d(this,10);return h}})(),c.prototype.int32=function(){return this.uint32()|0},c.prototype.sint32=function(){var h=this.uint32();return h>>>1^-(h&1)|0};function o(){var h=new u(0,0),m=0;if(this.len-this.pos>4){for(;m<4;++m)if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h;if(h.lo=(h.lo|(this.buf[this.pos]&127)<<28)>>>0,h.hi=(h.hi|(this.buf[this.pos]&127)>>4)>>>0,this.buf[this.pos++]<128)return h;m=0}else{for(;m<3;++m){if(this.pos>=this.len)throw d(this);if(h.lo=(h.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}return h.lo=(h.lo|(this.buf[this.pos++]&127)<>>0,h}if(this.len-this.pos>4){for(;m<5;++m)if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}else for(;m<5;++m){if(this.pos>=this.len)throw d(this);if(h.hi=(h.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return h}throw Error("invalid varint encoding")}c.prototype.bool=function(){return this.uint32()!==0};function a(h,m){return(h[m-4]|h[m-3]<<8|h[m-2]<<16|h[m-1]<<24)>>>0}c.prototype.fixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)},c.prototype.sfixed32=function(){if(this.pos+4>this.len)throw d(this,4);return a(this.buf,this.pos+=4)|0};function p(){if(this.pos+8>this.len)throw d(this,8);return new u(a(this.buf,this.pos+=4),a(this.buf,this.pos+=4))}c.prototype.float=function(){if(this.pos+4>this.len)throw d(this,4);var h=n.float.readFloatLE(this.buf,this.pos);return this.pos+=4,h},c.prototype.double=function(){if(this.pos+8>this.len)throw d(this,4);var h=n.float.readDoubleLE(this.buf,this.pos);return this.pos+=8,h},c.prototype.bytes=function(){var h=this.uint32(),m=this.pos,g=this.pos+h;if(g>this.len)throw d(this,h);if(this.pos+=h,Array.isArray(this.buf))return this.buf.slice(m,g);if(m===g){var v=n.Buffer;return v?v.alloc(0):new this.buf.constructor(0)}return this._slice.call(this.buf,m,g)},c.prototype.string=function(){var h=this.bytes();return l.read(h,0,h.length)},c.prototype.skip=function(h){if(typeof h=="number"){if(this.pos+h>this.len)throw d(this,h);this.pos+=h}else do if(this.pos>=this.len)throw d(this);while(this.buf[this.pos++]&128);return this},c.recursionLimit=n.recursionLimit,c.prototype.skipType=function(h,m){if(m===void 0&&(m=0),m>c.recursionLimit)throw Error("maximum nesting depth exceeded");switch(h){case 0:this.skip();break;case 1:this.skip(8);break;case 2:this.skip(this.uint32());break;case 3:for(;(h=this.uint32()&7)!==4;)this.skipType(h,m+1);break;case 5:this.skip(4);break;default:throw Error("invalid wire type "+h+" at offset "+this.pos)}return this},c._configure=function(h){i=h,c.create=r(),i._configure();var m=n.Long?"toLong":"toNumber";n.merge(c.prototype,{int64:function(){return o.call(this)[m](!1)},uint64:function(){return o.call(this)[m](!0)},sint64:function(){return o.call(this).zzDecode()[m](!1)},fixed64:function(){return p.call(this)[m](!0)},sfixed64:function(){return p.call(this)[m](!1)}})}}),pO=se((e,t)=>{"use strict";t.exports=u;var n=K0();(u.prototype=Object.create(n.prototype)).constructor=u;var i=bn();function u(l){n.call(this,l)}u._configure=function(){i.Buffer&&(u.prototype._slice=i.Buffer.prototype.slice)},u.prototype.string=function(){var l=this.uint32();return this.buf.utf8Slice?this.buf.utf8Slice(this.pos,this.pos=Math.min(this.pos+l,this.len)):this.buf.toString("utf-8",this.pos,this.pos=Math.min(this.pos+l,this.len))},u._configure()}),hO=se((e,t)=>{"use strict";t.exports=i;var n=bn();(i.prototype=Object.create(n.EventEmitter.prototype)).constructor=i;function i(u,l,d){if(typeof u!="function")throw TypeError("rpcImpl must be a function");n.EventEmitter.call(this),this.rpcImpl=u,this.requestDelimited=!!l,this.responseDelimited=!!d}i.prototype.rpcCall=function u(l,d,c,s,r){if(!s)throw TypeError("request must be specified");var o=this;if(!r)return n.asPromise(u,o,l,d,c,s);if(!o.rpcImpl){setTimeout(function(){r(Error("already ended"))},0);return}try{return o.rpcImpl(l,d[o.requestDelimited?"encodeDelimited":"encode"](s).finish(),function(a,p){if(a)return o.emit("error",a,l),r(a);if(p===null){o.end(!0);return}if(!(p instanceof c))try{p=c[o.responseDelimited?"decodeDelimited":"decode"](p)}catch(h){return o.emit("error",h,l),r(h)}return o.emit("data",p,l),r(null,p)})}catch(a){o.emit("error",a,l),setTimeout(function(){r(a)},0);return}},i.prototype.end=function(u){return this.rpcImpl&&(u||this.rpcImpl(null,null,null),this.rpcImpl=null,this.emit("end").off()),this}}),fO=se(e=>{"use strict";var t=e;t.Service=hO()}),mO=se((e,t)=>{"use strict";t.exports=Object.create(null)}),gO=se(e=>{"use strict";var t=e;t.build="minimal",t.Writer=W0(),t.BufferWriter=cO(),t.Reader=K0(),t.BufferReader=pO(),t.util=bn(),t.rpc=fO(),t.roots=mO(),t.configure=n;function n(){t.util._configure(),t.Writer._configure(t.BufferWriter),t.Reader._configure(t.BufferReader)}n()}),bO=se((e,t)=>{"use strict";t.exports=gO()}),Ni=se((e,t)=>{"use strict";var n=bO(),i=n.Reader,u=n.Writer,l=n.util,d=n.roots.default||(n.roots.default={});d.onnx=(function(){var c={};return c.Version=(function(){var s={},r=Object.create(s);return r[s[0]="_START_VERSION"]=0,r[s[1]="IR_VERSION_2017_10_10"]=1,r[s[2]="IR_VERSION_2017_10_30"]=2,r[s[3]="IR_VERSION_2017_11_3"]=3,r[s[4]="IR_VERSION_2019_1_22"]=4,r[s[5]="IR_VERSION_2019_3_18"]=5,r[s[6]="IR_VERSION_2019_9_19"]=6,r[s[7]="IR_VERSION_2020_5_8"]=7,r[s[8]="IR_VERSION_2021_7_30"]=8,r[s[9]="IR_VERSION"]=9,r})(),c.AttributeProto=(function(){function s(r){if(this.floats=[],this.ints=[],this.strings=[],this.tensors=[],this.graphs=[],this.sparseTensors=[],this.typeProtos=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 21:{p.refAttrName=r.string();break}case 13:{p.docString=r.string();break}case 20:{p.type=r.int32();break}case 2:{p.f=r.float();break}case 3:{p.i=r.int64();break}case 4:{p.s=r.bytes();break}case 5:{p.t=d.onnx.TensorProto.decode(r,r.uint32());break}case 6:{p.g=d.onnx.GraphProto.decode(r,r.uint32());break}case 22:{p.sparseTensor=d.onnx.SparseTensorProto.decode(r,r.uint32());break}case 14:{p.tp=d.onnx.TypeProto.decode(r,r.uint32());break}case 7:{if(p.floats&&p.floats.length||(p.floats=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.i.high>>>0).toNumber())),r.s!=null&&(typeof r.s=="string"?l.base64.decode(r.s,o.s=l.newBuffer(l.base64.length(r.s)),0):r.s.length>=0&&(o.s=r.s)),r.t!=null){if(typeof r.t!="object")throw TypeError(".onnx.AttributeProto.t: object expected");o.t=d.onnx.TensorProto.fromObject(r.t)}if(r.g!=null){if(typeof r.g!="object")throw TypeError(".onnx.AttributeProto.g: object expected");o.g=d.onnx.GraphProto.fromObject(r.g)}if(r.sparseTensor!=null){if(typeof r.sparseTensor!="object")throw TypeError(".onnx.AttributeProto.sparseTensor: object expected");o.sparseTensor=d.onnx.SparseTensorProto.fromObject(r.sparseTensor)}if(r.tp!=null){if(typeof r.tp!="object")throw TypeError(".onnx.AttributeProto.tp: object expected");o.tp=d.onnx.TypeProto.fromObject(r.tp)}if(r.floats){if(!Array.isArray(r.floats))throw TypeError(".onnx.AttributeProto.floats: array expected");o.floats=[];for(var a=0;a>>0,r.ints[a].high>>>0).toNumber())}if(r.strings){if(!Array.isArray(r.strings))throw TypeError(".onnx.AttributeProto.strings: array expected");o.strings=[];for(var a=0;a=0&&(o.strings[a]=r.strings[a])}if(r.tensors){if(!Array.isArray(r.tensors))throw TypeError(".onnx.AttributeProto.tensors: array expected");o.tensors=[];for(var a=0;a>>0,r.i.high>>>0).toNumber():r.i),r.s!=null&&r.hasOwnProperty("s")&&(a.s=o.bytes===String?l.base64.encode(r.s,0,r.s.length):o.bytes===Array?Array.prototype.slice.call(r.s):r.s),r.t!=null&&r.hasOwnProperty("t")&&(a.t=d.onnx.TensorProto.toObject(r.t,o)),r.g!=null&&r.hasOwnProperty("g")&&(a.g=d.onnx.GraphProto.toObject(r.g,o)),r.floats&&r.floats.length){a.floats=[];for(var h=0;h>>0,r.ints[h].high>>>0).toNumber():r.ints[h]}if(r.strings&&r.strings.length){a.strings=[];for(var h=0;h>>3){case 1:{p.name=r.string();break}case 2:{p.type=d.onnx.TypeProto.decode(r,r.uint32());break}case 3:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.type!=null&&r.hasOwnProperty("type")){var o=d.onnx.TypeProto.verify(r.type);if(o)return"type."+o}return r.docString!=null&&r.hasOwnProperty("docString")&&!l.isString(r.docString)?"docString: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.ValueInfoProto)return r;var o=new d.onnx.ValueInfoProto;if(r.name!=null&&(o.name=String(r.name)),r.type!=null){if(typeof r.type!="object")throw TypeError(".onnx.ValueInfoProto.type: object expected");o.type=d.onnx.TypeProto.fromObject(r.type)}return r.docString!=null&&(o.docString=String(r.docString)),o},s.toObject=function(r,o){o||(o={});var a={};return o.defaults&&(a.name="",a.type=null,a.docString=""),r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.type!=null&&r.hasOwnProperty("type")&&(a.type=d.onnx.TypeProto.toObject(r.type,o)),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.ValueInfoProto"},s})(),c.NodeProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 2:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 3:{p.name=r.string();break}case 4:{p.opType=r.string();break}case 7:{p.domain=r.string();break}case 5:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 6:{p.docString=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var o=0;o>>3){case 1:{p.initialization=d.onnx.GraphProto.decode(r,r.uint32());break}case 2:{p.algorithm=d.onnx.GraphProto.decode(r,r.uint32());break}case 3:{p.initializationBinding&&p.initializationBinding.length||(p.initializationBinding=[]),p.initializationBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 4:{p.updateBinding&&p.updateBinding.length||(p.updateBinding=[]),p.updateBinding.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.initialization!=null&&r.hasOwnProperty("initialization")){var o=d.onnx.GraphProto.verify(r.initialization);if(o)return"initialization."+o}if(r.algorithm!=null&&r.hasOwnProperty("algorithm")){var o=d.onnx.GraphProto.verify(r.algorithm);if(o)return"algorithm."+o}if(r.initializationBinding!=null&&r.hasOwnProperty("initializationBinding")){if(!Array.isArray(r.initializationBinding))return"initializationBinding: array expected";for(var a=0;a>>3){case 1:{p.irVersion=r.int64();break}case 8:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 2:{p.producerName=r.string();break}case 3:{p.producerVersion=r.string();break}case 4:{p.domain=r.string();break}case 5:{p.modelVersion=r.int64();break}case 6:{p.docString=r.string();break}case 7:{p.graph=d.onnx.GraphProto.decode(r,r.uint32());break}case 14:{p.metadataProps&&p.metadataProps.length||(p.metadataProps=[]),p.metadataProps.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}case 20:{p.trainingInfo&&p.trainingInfo.length||(p.trainingInfo=[]),p.trainingInfo.push(d.onnx.TrainingInfoProto.decode(r,r.uint32()));break}case 25:{p.functions&&p.functions.length||(p.functions=[]),p.functions.push(d.onnx.FunctionProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.irVersion!=null&&r.hasOwnProperty("irVersion")&&!l.isInteger(r.irVersion)&&!(r.irVersion&&l.isInteger(r.irVersion.low)&&l.isInteger(r.irVersion.high)))return"irVersion: integer|Long expected";if(r.opsetImport!=null&&r.hasOwnProperty("opsetImport")){if(!Array.isArray(r.opsetImport))return"opsetImport: array expected";for(var o=0;o>>0,r.irVersion.high>>>0).toNumber())),r.opsetImport){if(!Array.isArray(r.opsetImport))throw TypeError(".onnx.ModelProto.opsetImport: array expected");o.opsetImport=[];for(var a=0;a>>0,r.modelVersion.high>>>0).toNumber())),r.docString!=null&&(o.docString=String(r.docString)),r.graph!=null){if(typeof r.graph!="object")throw TypeError(".onnx.ModelProto.graph: object expected");o.graph=d.onnx.GraphProto.fromObject(r.graph)}if(r.metadataProps){if(!Array.isArray(r.metadataProps))throw TypeError(".onnx.ModelProto.metadataProps: array expected");o.metadataProps=[];for(var a=0;a>>0,r.irVersion.high>>>0).toNumber():r.irVersion),r.producerName!=null&&r.hasOwnProperty("producerName")&&(a.producerName=r.producerName),r.producerVersion!=null&&r.hasOwnProperty("producerVersion")&&(a.producerVersion=r.producerVersion),r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.modelVersion!=null&&r.hasOwnProperty("modelVersion")&&(typeof r.modelVersion=="number"?a.modelVersion=o.longs===String?String(r.modelVersion):r.modelVersion:a.modelVersion=o.longs===String?l.Long.prototype.toString.call(r.modelVersion):o.longs===Number?new l.LongBits(r.modelVersion.low>>>0,r.modelVersion.high>>>0).toNumber():r.modelVersion),r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.graph!=null&&r.hasOwnProperty("graph")&&(a.graph=d.onnx.GraphProto.toObject(r.graph,o)),r.opsetImport&&r.opsetImport.length){a.opsetImport=[];for(var h=0;h>>3){case 1:{p.key=r.string();break}case 2:{p.value=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.key!=null&&r.hasOwnProperty("key")&&!l.isString(r.key)?"key: string expected":r.value!=null&&r.hasOwnProperty("value")&&!l.isString(r.value)?"value: string expected":null},s.fromObject=function(r){if(r instanceof d.onnx.StringStringEntryProto)return r;var o=new d.onnx.StringStringEntryProto;return r.key!=null&&(o.key=String(r.key)),r.value!=null&&(o.value=String(r.value)),o},s.toObject=function(r,o){o||(o={});var a={};return o.defaults&&(a.key="",a.value=""),r.key!=null&&r.hasOwnProperty("key")&&(a.key=r.key),r.value!=null&&r.hasOwnProperty("value")&&(a.value=r.value),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.StringStringEntryProto"},s})(),c.TensorAnnotation=(function(){function s(r){if(this.quantParameterTensorNames=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.tensorName=r.string();break}case 2:{p.quantParameterTensorNames&&p.quantParameterTensorNames.length||(p.quantParameterTensorNames=[]),p.quantParameterTensorNames.push(d.onnx.StringStringEntryProto.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.tensorName!=null&&r.hasOwnProperty("tensorName")&&!l.isString(r.tensorName))return"tensorName: string expected";if(r.quantParameterTensorNames!=null&&r.hasOwnProperty("quantParameterTensorNames")){if(!Array.isArray(r.quantParameterTensorNames))return"quantParameterTensorNames: array expected";for(var o=0;o>>3){case 1:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 2:{p.name=r.string();break}case 5:{p.initializer&&p.initializer.length||(p.initializer=[]),p.initializer.push(d.onnx.TensorProto.decode(r,r.uint32()));break}case 15:{p.sparseInitializer&&p.sparseInitializer.length||(p.sparseInitializer=[]),p.sparseInitializer.push(d.onnx.SparseTensorProto.decode(r,r.uint32()));break}case 10:{p.docString=r.string();break}case 11:{p.input&&p.input.length||(p.input=[]),p.input.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 12:{p.output&&p.output.length||(p.output=[]),p.output.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 13:{p.valueInfo&&p.valueInfo.length||(p.valueInfo=[]),p.valueInfo.push(d.onnx.ValueInfoProto.decode(r,r.uint32()));break}case 14:{p.quantizationAnnotation&&p.quantizationAnnotation.length||(p.quantizationAnnotation=[]),p.quantizationAnnotation.push(d.onnx.TensorAnnotation.decode(r,r.uint32()));break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.node!=null&&r.hasOwnProperty("node")){if(!Array.isArray(r.node))return"node: array expected";for(var o=0;o>>3){case 1:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}if(r.dataType!=null&&(o.dataType=r.dataType|0),r.segment!=null){if(typeof r.segment!="object")throw TypeError(".onnx.TensorProto.segment: object expected");o.segment=d.onnx.TensorProto.Segment.fromObject(r.segment)}if(r.floatData){if(!Array.isArray(r.floatData))throw TypeError(".onnx.TensorProto.floatData: array expected");o.floatData=[];for(var a=0;a=0&&(o.stringData[a]=r.stringData[a])}if(r.int64Data){if(!Array.isArray(r.int64Data))throw TypeError(".onnx.TensorProto.int64Data: array expected");o.int64Data=[];for(var a=0;a>>0,r.int64Data[a].high>>>0).toNumber())}if(r.name!=null&&(o.name=String(r.name)),r.docString!=null&&(o.docString=String(r.docString)),r.rawData!=null&&(typeof r.rawData=="string"?l.base64.decode(r.rawData,o.rawData=l.newBuffer(l.base64.length(r.rawData)),0):r.rawData.length>=0&&(o.rawData=r.rawData)),r.externalData){if(!Array.isArray(r.externalData))throw TypeError(".onnx.TensorProto.externalData: array expected");o.externalData=[];for(var a=0;a>>0,r.uint64Data[a].high>>>0).toNumber(!0))}return o},s.toObject=function(r,o){o||(o={});var a={};if((o.arrays||o.defaults)&&(a.dims=[],a.floatData=[],a.int32Data=[],a.stringData=[],a.int64Data=[],a.doubleData=[],a.uint64Data=[],a.externalData=[]),o.defaults&&(a.dataType=0,a.segment=null,a.name="",o.bytes===String?a.rawData="":(a.rawData=[],o.bytes!==Array&&(a.rawData=l.newBuffer(a.rawData))),a.docString="",a.dataLocation=o.enums===String?"DEFAULT":0),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}if(r.dataType!=null&&r.hasOwnProperty("dataType")&&(a.dataType=r.dataType),r.segment!=null&&r.hasOwnProperty("segment")&&(a.segment=d.onnx.TensorProto.Segment.toObject(r.segment,o)),r.floatData&&r.floatData.length){a.floatData=[];for(var p=0;p>>0,r.int64Data[p].high>>>0).toNumber():r.int64Data[p]}if(r.name!=null&&r.hasOwnProperty("name")&&(a.name=r.name),r.rawData!=null&&r.hasOwnProperty("rawData")&&(a.rawData=o.bytes===String?l.base64.encode(r.rawData,0,r.rawData.length):o.bytes===Array?Array.prototype.slice.call(r.rawData):r.rawData),r.doubleData&&r.doubleData.length){a.doubleData=[];for(var p=0;p>>0,r.uint64Data[p].high>>>0).toNumber(!0):r.uint64Data[p]}if(r.docString!=null&&r.hasOwnProperty("docString")&&(a.docString=r.docString),r.externalData&&r.externalData.length){a.externalData=[];for(var p=0;p>>3){case 1:{h.begin=o.int64();break}case 2:{h.end=o.int64();break}default:o.skipType(m&7);break}}return h},r.decodeDelimited=function(o){return o instanceof i||(o=new i(o)),this.decode(o,o.uint32())},r.verify=function(o){return typeof o!="object"||o===null?"object expected":o.begin!=null&&o.hasOwnProperty("begin")&&!l.isInteger(o.begin)&&!(o.begin&&l.isInteger(o.begin.low)&&l.isInteger(o.begin.high))?"begin: integer|Long expected":o.end!=null&&o.hasOwnProperty("end")&&!l.isInteger(o.end)&&!(o.end&&l.isInteger(o.end.low)&&l.isInteger(o.end.high))?"end: integer|Long expected":null},r.fromObject=function(o){if(o instanceof d.onnx.TensorProto.Segment)return o;var a=new d.onnx.TensorProto.Segment;return o.begin!=null&&(l.Long?(a.begin=l.Long.fromValue(o.begin)).unsigned=!1:typeof o.begin=="string"?a.begin=parseInt(o.begin,10):typeof o.begin=="number"?a.begin=o.begin:typeof o.begin=="object"&&(a.begin=new l.LongBits(o.begin.low>>>0,o.begin.high>>>0).toNumber())),o.end!=null&&(l.Long?(a.end=l.Long.fromValue(o.end)).unsigned=!1:typeof o.end=="string"?a.end=parseInt(o.end,10):typeof o.end=="number"?a.end=o.end:typeof o.end=="object"&&(a.end=new l.LongBits(o.end.low>>>0,o.end.high>>>0).toNumber())),a},r.toObject=function(o,a){a||(a={});var p={};if(a.defaults){if(l.Long){var h=new l.Long(0,0,!1);p.begin=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.begin=a.longs===String?"0":0;if(l.Long){var h=new l.Long(0,0,!1);p.end=a.longs===String?h.toString():a.longs===Number?h.toNumber():h}else p.end=a.longs===String?"0":0}return o.begin!=null&&o.hasOwnProperty("begin")&&(typeof o.begin=="number"?p.begin=a.longs===String?String(o.begin):o.begin:p.begin=a.longs===String?l.Long.prototype.toString.call(o.begin):a.longs===Number?new l.LongBits(o.begin.low>>>0,o.begin.high>>>0).toNumber():o.begin),o.end!=null&&o.hasOwnProperty("end")&&(typeof o.end=="number"?p.end=a.longs===String?String(o.end):o.end:p.end=a.longs===String?l.Long.prototype.toString.call(o.end):a.longs===Number?new l.LongBits(o.end.low>>>0,o.end.high>>>0).toNumber():o.end),p},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(o){return o===void 0&&(o="type.googleapis.com"),o+"/onnx.TensorProto.Segment"},r})(),s.DataLocation=(function(){var r={},o=Object.create(r);return o[r[0]="DEFAULT"]=0,o[r[1]="EXTERNAL"]=1,o})(),s})(),c.SparseTensorProto=(function(){function s(r){if(this.dims=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.values=d.onnx.TensorProto.decode(r,r.uint32());break}case 2:{p.indices=d.onnx.TensorProto.decode(r,r.uint32());break}case 3:{if(p.dims&&p.dims.length||(p.dims=[]),(h&7)===2)for(var m=r.uint32()+r.pos;r.pos>>0,r.dims[a].high>>>0).toNumber())}return o},s.toObject=function(r,o){o||(o={});var a={};if((o.arrays||o.defaults)&&(a.dims=[]),o.defaults&&(a.values=null,a.indices=null),r.values!=null&&r.hasOwnProperty("values")&&(a.values=d.onnx.TensorProto.toObject(r.values,o)),r.indices!=null&&r.hasOwnProperty("indices")&&(a.indices=d.onnx.TensorProto.toObject(r.indices,o)),r.dims&&r.dims.length){a.dims=[];for(var p=0;p>>0,r.dims[p].high>>>0).toNumber():r.dims[p]}return a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.SparseTensorProto"},s})(),c.TensorShapeProto=(function(){function s(r){if(this.dim=[],r)for(var o=Object.keys(r),a=0;a>>3===1?(p.dim&&p.dim.length||(p.dim=[]),p.dim.push(d.onnx.TensorShapeProto.Dimension.decode(r,r.uint32()))):r.skipType(h&7)}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.dim!=null&&r.hasOwnProperty("dim")){if(!Array.isArray(r.dim))return"dim: array expected";for(var o=0;o>>3){case 1:{m.dimValue=a.int64();break}case 2:{m.dimParam=a.string();break}case 3:{m.denotation=a.string();break}default:a.skipType(g&7);break}}return m},r.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},r.verify=function(a){if(typeof a!="object"||a===null)return"object expected";var p={};if(a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(p.value=1,!l.isInteger(a.dimValue)&&!(a.dimValue&&l.isInteger(a.dimValue.low)&&l.isInteger(a.dimValue.high))))return"dimValue: integer|Long expected";if(a.dimParam!=null&&a.hasOwnProperty("dimParam")){if(p.value===1)return"value: multiple values";if(p.value=1,!l.isString(a.dimParam))return"dimParam: string expected"}return a.denotation!=null&&a.hasOwnProperty("denotation")&&!l.isString(a.denotation)?"denotation: string expected":null},r.fromObject=function(a){if(a instanceof d.onnx.TensorShapeProto.Dimension)return a;var p=new d.onnx.TensorShapeProto.Dimension;return a.dimValue!=null&&(l.Long?(p.dimValue=l.Long.fromValue(a.dimValue)).unsigned=!1:typeof a.dimValue=="string"?p.dimValue=parseInt(a.dimValue,10):typeof a.dimValue=="number"?p.dimValue=a.dimValue:typeof a.dimValue=="object"&&(p.dimValue=new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber())),a.dimParam!=null&&(p.dimParam=String(a.dimParam)),a.denotation!=null&&(p.denotation=String(a.denotation)),p},r.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.denotation=""),a.dimValue!=null&&a.hasOwnProperty("dimValue")&&(typeof a.dimValue=="number"?h.dimValue=p.longs===String?String(a.dimValue):a.dimValue:h.dimValue=p.longs===String?l.Long.prototype.toString.call(a.dimValue):p.longs===Number?new l.LongBits(a.dimValue.low>>>0,a.dimValue.high>>>0).toNumber():a.dimValue,p.oneofs&&(h.value="dimValue")),a.dimParam!=null&&a.hasOwnProperty("dimParam")&&(h.dimParam=a.dimParam,p.oneofs&&(h.value="dimParam")),a.denotation!=null&&a.hasOwnProperty("denotation")&&(h.denotation=a.denotation),h},r.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},r.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TensorShapeProto.Dimension"},r})(),s})(),c.TypeProto=(function(){function s(o){if(o)for(var a=Object.keys(o),p=0;p>>3){case 1:{h.tensorType=d.onnx.TypeProto.Tensor.decode(o,o.uint32());break}case 4:{h.sequenceType=d.onnx.TypeProto.Sequence.decode(o,o.uint32());break}case 5:{h.mapType=d.onnx.TypeProto.Map.decode(o,o.uint32());break}case 9:{h.optionalType=d.onnx.TypeProto.Optional.decode(o,o.uint32());break}case 8:{h.sparseTensorType=d.onnx.TypeProto.SparseTensor.decode(o,o.uint32());break}case 6:{h.denotation=o.string();break}default:o.skipType(m&7);break}}return h},s.decodeDelimited=function(o){return o instanceof i||(o=new i(o)),this.decode(o,o.uint32())},s.verify=function(o){if(typeof o!="object"||o===null)return"object expected";var a={};if(o.tensorType!=null&&o.hasOwnProperty("tensorType")){a.value=1;{var p=d.onnx.TypeProto.Tensor.verify(o.tensorType);if(p)return"tensorType."+p}}if(o.sequenceType!=null&&o.hasOwnProperty("sequenceType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Sequence.verify(o.sequenceType);if(p)return"sequenceType."+p}}if(o.mapType!=null&&o.hasOwnProperty("mapType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Map.verify(o.mapType);if(p)return"mapType."+p}}if(o.optionalType!=null&&o.hasOwnProperty("optionalType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.Optional.verify(o.optionalType);if(p)return"optionalType."+p}}if(o.sparseTensorType!=null&&o.hasOwnProperty("sparseTensorType")){if(a.value===1)return"value: multiple values";a.value=1;{var p=d.onnx.TypeProto.SparseTensor.verify(o.sparseTensorType);if(p)return"sparseTensorType."+p}}return o.denotation!=null&&o.hasOwnProperty("denotation")&&!l.isString(o.denotation)?"denotation: string expected":null},s.fromObject=function(o){if(o instanceof d.onnx.TypeProto)return o;var a=new d.onnx.TypeProto;if(o.tensorType!=null){if(typeof o.tensorType!="object")throw TypeError(".onnx.TypeProto.tensorType: object expected");a.tensorType=d.onnx.TypeProto.Tensor.fromObject(o.tensorType)}if(o.sequenceType!=null){if(typeof o.sequenceType!="object")throw TypeError(".onnx.TypeProto.sequenceType: object expected");a.sequenceType=d.onnx.TypeProto.Sequence.fromObject(o.sequenceType)}if(o.mapType!=null){if(typeof o.mapType!="object")throw TypeError(".onnx.TypeProto.mapType: object expected");a.mapType=d.onnx.TypeProto.Map.fromObject(o.mapType)}if(o.optionalType!=null){if(typeof o.optionalType!="object")throw TypeError(".onnx.TypeProto.optionalType: object expected");a.optionalType=d.onnx.TypeProto.Optional.fromObject(o.optionalType)}if(o.sparseTensorType!=null){if(typeof o.sparseTensorType!="object")throw TypeError(".onnx.TypeProto.sparseTensorType: object expected");a.sparseTensorType=d.onnx.TypeProto.SparseTensor.fromObject(o.sparseTensorType)}return o.denotation!=null&&(a.denotation=String(o.denotation)),a},s.toObject=function(o,a){a||(a={});var p={};return a.defaults&&(p.denotation=""),o.tensorType!=null&&o.hasOwnProperty("tensorType")&&(p.tensorType=d.onnx.TypeProto.Tensor.toObject(o.tensorType,a),a.oneofs&&(p.value="tensorType")),o.sequenceType!=null&&o.hasOwnProperty("sequenceType")&&(p.sequenceType=d.onnx.TypeProto.Sequence.toObject(o.sequenceType,a),a.oneofs&&(p.value="sequenceType")),o.mapType!=null&&o.hasOwnProperty("mapType")&&(p.mapType=d.onnx.TypeProto.Map.toObject(o.mapType,a),a.oneofs&&(p.value="mapType")),o.denotation!=null&&o.hasOwnProperty("denotation")&&(p.denotation=o.denotation),o.sparseTensorType!=null&&o.hasOwnProperty("sparseTensorType")&&(p.sparseTensorType=d.onnx.TypeProto.SparseTensor.toObject(o.sparseTensorType,a),a.oneofs&&(p.value="sparseTensorType")),o.optionalType!=null&&o.hasOwnProperty("optionalType")&&(p.optionalType=d.onnx.TypeProto.Optional.toObject(o.optionalType,a),a.oneofs&&(p.value="optionalType")),p},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(o){return o===void 0&&(o="type.googleapis.com"),o+"/onnx.TypeProto"},s.Tensor=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(g&7);break}}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Tensor)return a;var p=new d.onnx.TypeProto.Tensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.Tensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Tensor"},o})(),s.Sequence=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(g&7)}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Sequence)return a;var p=new d.onnx.TypeProto.Sequence;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Sequence.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Sequence"},o})(),s.Map=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.keyType=a.int32();break}case 2:{m.valueType=d.onnx.TypeProto.decode(a,a.uint32());break}default:a.skipType(g&7);break}}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.keyType!=null&&a.hasOwnProperty("keyType")&&!l.isInteger(a.keyType))return"keyType: integer expected";if(a.valueType!=null&&a.hasOwnProperty("valueType")){var p=d.onnx.TypeProto.verify(a.valueType);if(p)return"valueType."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Map)return a;var p=new d.onnx.TypeProto.Map;if(a.keyType!=null&&(p.keyType=a.keyType|0),a.valueType!=null){if(typeof a.valueType!="object")throw TypeError(".onnx.TypeProto.Map.valueType: object expected");p.valueType=d.onnx.TypeProto.fromObject(a.valueType)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.keyType=0,h.valueType=null),a.keyType!=null&&a.hasOwnProperty("keyType")&&(h.keyType=a.keyType),a.valueType!=null&&a.hasOwnProperty("valueType")&&(h.valueType=d.onnx.TypeProto.toObject(a.valueType,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Map"},o})(),s.Optional=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3===1?m.elemType=d.onnx.TypeProto.decode(a,a.uint32()):a.skipType(g&7)}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")){var p=d.onnx.TypeProto.verify(a.elemType);if(p)return"elemType."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.Optional)return a;var p=new d.onnx.TypeProto.Optional;if(a.elemType!=null){if(typeof a.elemType!="object")throw TypeError(".onnx.TypeProto.Optional.elemType: object expected");p.elemType=d.onnx.TypeProto.fromObject(a.elemType)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=d.onnx.TypeProto.toObject(a.elemType,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.Optional"},o})(),s.SparseTensor=(function(){function o(a){if(a)for(var p=Object.keys(a),h=0;h>>3){case 1:{m.elemType=a.int32();break}case 2:{m.shape=d.onnx.TensorShapeProto.decode(a,a.uint32());break}default:a.skipType(g&7);break}}return m},o.decodeDelimited=function(a){return a instanceof i||(a=new i(a)),this.decode(a,a.uint32())},o.verify=function(a){if(typeof a!="object"||a===null)return"object expected";if(a.elemType!=null&&a.hasOwnProperty("elemType")&&!l.isInteger(a.elemType))return"elemType: integer expected";if(a.shape!=null&&a.hasOwnProperty("shape")){var p=d.onnx.TensorShapeProto.verify(a.shape);if(p)return"shape."+p}return null},o.fromObject=function(a){if(a instanceof d.onnx.TypeProto.SparseTensor)return a;var p=new d.onnx.TypeProto.SparseTensor;if(a.elemType!=null&&(p.elemType=a.elemType|0),a.shape!=null){if(typeof a.shape!="object")throw TypeError(".onnx.TypeProto.SparseTensor.shape: object expected");p.shape=d.onnx.TensorShapeProto.fromObject(a.shape)}return p},o.toObject=function(a,p){p||(p={});var h={};return p.defaults&&(h.elemType=0,h.shape=null),a.elemType!=null&&a.hasOwnProperty("elemType")&&(h.elemType=a.elemType),a.shape!=null&&a.hasOwnProperty("shape")&&(h.shape=d.onnx.TensorShapeProto.toObject(a.shape,p)),h},o.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},o.getTypeUrl=function(a){return a===void 0&&(a="type.googleapis.com"),a+"/onnx.TypeProto.SparseTensor"},o})(),s})(),c.OperatorSetIdProto=(function(){function s(r){if(r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.domain=r.string();break}case 2:{p.version=r.int64();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){return typeof r!="object"||r===null?"object expected":r.domain!=null&&r.hasOwnProperty("domain")&&!l.isString(r.domain)?"domain: string expected":r.version!=null&&r.hasOwnProperty("version")&&!l.isInteger(r.version)&&!(r.version&&l.isInteger(r.version.low)&&l.isInteger(r.version.high))?"version: integer|Long expected":null},s.fromObject=function(r){if(r instanceof d.onnx.OperatorSetIdProto)return r;var o=new d.onnx.OperatorSetIdProto;return r.domain!=null&&(o.domain=String(r.domain)),r.version!=null&&(l.Long?(o.version=l.Long.fromValue(r.version)).unsigned=!1:typeof r.version=="string"?o.version=parseInt(r.version,10):typeof r.version=="number"?o.version=r.version:typeof r.version=="object"&&(o.version=new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber())),o},s.toObject=function(r,o){o||(o={});var a={};if(o.defaults)if(a.domain="",l.Long){var p=new l.Long(0,0,!1);a.version=o.longs===String?p.toString():o.longs===Number?p.toNumber():p}else a.version=o.longs===String?"0":0;return r.domain!=null&&r.hasOwnProperty("domain")&&(a.domain=r.domain),r.version!=null&&r.hasOwnProperty("version")&&(typeof r.version=="number"?a.version=o.longs===String?String(r.version):r.version:a.version=o.longs===String?l.Long.prototype.toString.call(r.version):o.longs===Number?new l.LongBits(r.version.low>>>0,r.version.high>>>0).toNumber():r.version),a},s.prototype.toJSON=function(){return this.constructor.toObject(this,n.util.toJSONOptions)},s.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.OperatorSetIdProto"},s})(),c.OperatorStatus=(function(){var s={},r=Object.create(s);return r[s[0]="EXPERIMENTAL"]=0,r[s[1]="STABLE"]=1,r})(),c.FunctionProto=(function(){function s(r){if(this.input=[],this.output=[],this.attribute=[],this.attributeProto=[],this.node=[],this.opsetImport=[],r)for(var o=Object.keys(r),a=0;a>>3){case 1:{p.name=r.string();break}case 4:{p.input&&p.input.length||(p.input=[]),p.input.push(r.string());break}case 5:{p.output&&p.output.length||(p.output=[]),p.output.push(r.string());break}case 6:{p.attribute&&p.attribute.length||(p.attribute=[]),p.attribute.push(r.string());break}case 11:{p.attributeProto&&p.attributeProto.length||(p.attributeProto=[]),p.attributeProto.push(d.onnx.AttributeProto.decode(r,r.uint32()));break}case 7:{p.node&&p.node.length||(p.node=[]),p.node.push(d.onnx.NodeProto.decode(r,r.uint32()));break}case 8:{p.docString=r.string();break}case 9:{p.opsetImport&&p.opsetImport.length||(p.opsetImport=[]),p.opsetImport.push(d.onnx.OperatorSetIdProto.decode(r,r.uint32()));break}case 10:{p.domain=r.string();break}default:r.skipType(h&7);break}}return p},s.decodeDelimited=function(r){return r instanceof i||(r=new i(r)),this.decode(r,r.uint32())},s.verify=function(r){if(typeof r!="object"||r===null)return"object expected";if(r.name!=null&&r.hasOwnProperty("name")&&!l.isString(r.name))return"name: string expected";if(r.input!=null&&r.hasOwnProperty("input")){if(!Array.isArray(r.input))return"input: array expected";for(var o=0;o{"use strict";o0(),je=be(Ni()),zn(),wi=class{static arraysEqual(e,t){if(e.length!==t.length)return!1;for(let n=0;n1&&o>1)return;c[d-s]=Math.max(r,o)}return c}static index(t,n){let i=new Array(n.length);return _i.fillIndex(t,n,i),i}static fillIndex(t,n,i){let u=t.length-n.length;for(let l=0;l=0;y--)r[y]=v%d[y],v=Math.floor(v/d[y]);m||(_i.fillIndex(r,t.dims,o),p=t.get(o)),g||(_i.fillIndex(r,n.dims,a),h=n.get(a)),s.set(r,i(p,h))}}return s}}static isValidBroadcast(t,n){let i=t.length,u=n.length;if(i>u)return!1;for(let l=1;l<=i;l++)if(t[i-l]!==1&&t[i-l]!==n[u-l])return!1;return!0}static getBroadcastDims(t,n){let i=t.length,u=[];for(let l=0;l1&&c===1&&u.unshift(d)}return u}},X0=class{static getShapeOfGemmResult(e,t,n,i,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(i?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!Qt.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},yt=class Lu{static tensorDataTypeFromProto(t){switch(t){case je.onnx.TensorProto.DataType.INT8:return"int8";case je.onnx.TensorProto.DataType.UINT8:return"uint8";case je.onnx.TensorProto.DataType.BOOL:return"bool";case je.onnx.TensorProto.DataType.INT16:return"int16";case je.onnx.TensorProto.DataType.UINT16:return"uint16";case je.onnx.TensorProto.DataType.INT32:return"int32";case je.onnx.TensorProto.DataType.UINT32:return"uint32";case je.onnx.TensorProto.DataType.FLOAT:return"float32";case je.onnx.TensorProto.DataType.DOUBLE:return"float64";case je.onnx.TensorProto.DataType.STRING:return"string";case je.onnx.TensorProto.DataType.INT64:return"int32";case je.onnx.TensorProto.DataType.UINT64:return"uint32";default:throw new Error(`unsupported data type: ${je.onnx.TensorProto.DataType[t]}`)}}static tensorDataTypeStringToEnum(t){switch(t){case"int8":return je.onnx.TensorProto.DataType.INT8;case"uint8":return je.onnx.TensorProto.DataType.UINT8;case"bool":return je.onnx.TensorProto.DataType.BOOL;case"int16":return je.onnx.TensorProto.DataType.INT16;case"uint16":return je.onnx.TensorProto.DataType.UINT16;case"int32":return je.onnx.TensorProto.DataType.INT32;case"uint32":return je.onnx.TensorProto.DataType.UINT32;case"float32":return je.onnx.TensorProto.DataType.FLOAT;case"float64":return je.onnx.TensorProto.DataType.DOUBLE;case"string":return je.onnx.TensorProto.DataType.STRING;case"int64":return je.onnx.TensorProto.DataType.INT64;case"uint64":return je.onnx.TensorProto.DataType.UINT64;default:throw new Error(`unsupported data type: ${t}`)}}static tensorDimsFromProto(t){return t.map(n=>pn.isLong(n)?n.toNumber():n)}static tensorValueTypeFromProto(t){return{tensorType:Lu.tensorDataTypeFromProto(t.elemType),shape:{dims:Lu.tensorDimsFromProto(t.shape.dim.map(n=>n.dimValue))}}}static tensorDimsFromORTFormat(t){let n=[];for(let i=0;it.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return xr.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return xr.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,i){let u=1;for(let l=n;l=0;--u)i[u]=i[u+1]*t[u+1];return i}static transpose(t){return t.slice().reverse()}static indicesToOffset(t,n,i){i===void 0&&(i=t.length);let u=0;for(let l=0;l=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(i=>this.normalizeAxis(i,n))}static incrementIndex(t,n,i){if(n.length===0||t.length===0)throw new Error("Index incrementing unsupported for scalar Tensor");if(i===void 0)i=n.length;else if(i<=0||i>n.length)throw new Error("Incorrect axis to increment on");for(let u=i-1;u>=0&&(t[u]++,!(t[u]=t.length)throw new Error("the dimension with value zero exceeds the dimension size of the input tensor");u[s]=t[s]}else u[s]=n[s];d*=u[s]}}let c=xr.size(t);if(l!==-1){if(c%d!==0)throw new Error(`the input tensor cannot be reshaped to the requested shape. Input shape: [${t}] Output shape: [${n}]`);u[l]=c/d}else if(d!==c)throw new Error("reshapedDims and originalDims don't have matching sizes");return u}static sortBasedOnPerm(t,n){return n?n.map(i=>t[i]):t.slice().reverse()}static padShape(t,n){let i=t.length;return t.map((u,l)=>u+n[l]+n[l+i])}static areEqual(t,n){return t.length!==n.length?!1:t.every((i,u)=>i===n[u])}static validateDimsAndCalcSize(t){if(t.length>6)throw new TypeError("Only rank 0 to 6 is supported for tensor shape.");let n=1;for(let i of t){if(!Number.isInteger(i))throw new TypeError(`Invalid shape: ${i} is not an integer`);if(i<0||i>2147483647)throw new TypeError(`Invalid shape: length ${i} is not allowed`);n*=i}return n}static flattenShape(t,n){n<0&&(n+=t.length);let i=t.reduce((l,d)=>l*d,1),u=t.slice(n).reduce((l,d)=>l*d,1);return[i/u,u]}static squeezeShape(t,n){let i=new Array;n=xr.normalizeAxes(n,t.length);for(let u=0;u=0;if(l&&t[u]!==1)throw new Error("squeeze an axis of size different than 1");(n.length===0&&t[u]>1||n.length>0&&!l)&&i.push(t[u])}return i}static unsqueezeShape(t,n){let i=new Array(t.length+n.length);i.fill(0);for(let l=0;l=i.length)throw new Error("'axes' has an out of range axis");if(i[d]!==0)throw new Error("'axes' has a duplicate axis");i[d]=1}let u=0;for(let l=0;l=i.length?i.push(n[c+2]):i[c]=n[c+2];for(let c=0;c=i[c]||d[c+i.length]>=i[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,i,u,l,d){if(d){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let c=0;c=i+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,i,u,l,d,c,s,r=0){let o=i*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,$r.computeOutputSize(t-o,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(i!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],$r.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return $r.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}},Pi=-34028234663852886e22,Ci=34028234663852886e22});function yO(e){switch(e){case"bool":case"int8":case"uint8":return 1;case"int16":case"uint16":return 2;case"int32":case"uint32":case"float32":return 4;case"float64":return 8;default:throw new Error(`cannot calculate sizeof() on type ${e}`)}}function jp(e){switch(e){case ve.onnx.TensorProto.DataType.UINT8:case ve.onnx.TensorProto.DataType.INT8:case ve.onnx.TensorProto.DataType.BOOL:return 1;case ve.onnx.TensorProto.DataType.UINT16:case ve.onnx.TensorProto.DataType.INT16:return 2;case ve.onnx.TensorProto.DataType.FLOAT:case ve.onnx.TensorProto.DataType.INT32:case ve.onnx.TensorProto.DataType.UINT32:return 4;case ve.onnx.TensorProto.DataType.INT64:case ve.onnx.TensorProto.DataType.DOUBLE:case ve.onnx.TensorProto.DataType.UINT64:return 8;default:throw new Error(`cannot calculate sizeof() on type ${ve.onnx.TensorProto.DataType[e]}`)}}function wO(e,t){return new(Y0(t))(e)}function Y0(e){switch(e){case"bool":case"uint8":return Uint8Array;case"int8":return Int8Array;case"int16":return Int16Array;case"uint16":return Uint16Array;case"int32":return Int32Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"float32":return Float32Array;case"float64":return Float64Array;default:throw new Error("unspecified error")}}function Uu(e,t){if(t===ve.onnx.TensorProto.DataType.INT64||t===Oo.TensorDataType.INT64){if(e.greaterThanOrEqual(2147483648)||e.lessThan(-2147483648))throw new TypeError("int64 is not supported")}else if(t===ve.onnx.TensorProto.DataType.UINT32||t===Oo.TensorDataType.UINT32||t===ve.onnx.TensorProto.DataType.UINT64||t===Oo.TensorDataType.UINT64){if(e.greaterThanOrEqual(4294967296)||e.lessThan(0))throw new TypeError("uint64 is not supported")}else throw new TypeError(`not a LONG type: ${ve.onnx.TensorProto.DataType[t]}`);return e.toNumber()}function Lp(e,t,n){switch(t){case ve.onnx.TensorProto.DataType.BOOL:case ve.onnx.TensorProto.DataType.UINT8:return e.getUint8(n);case ve.onnx.TensorProto.DataType.INT8:return e.getInt8(n);case ve.onnx.TensorProto.DataType.UINT16:return e.getUint16(n,!0);case ve.onnx.TensorProto.DataType.INT16:return e.getInt16(n,!0);case ve.onnx.TensorProto.DataType.FLOAT:return e.getFloat32(n,!0);case ve.onnx.TensorProto.DataType.INT32:return e.getInt32(n,!0);case ve.onnx.TensorProto.DataType.UINT32:return e.getUint32(n,!0);case ve.onnx.TensorProto.DataType.INT64:return Uu(pn.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!1),t);case ve.onnx.TensorProto.DataType.DOUBLE:return e.getFloat64(n,!0);case ve.onnx.TensorProto.DataType.UINT64:return Uu(pn.fromBits(e.getUint32(n,!0),e.getUint32(n+4,!0),!0),t);default:throw new Error(`cannot read from DataView for type ${ve.onnx.TensorProto.DataType[t]}`)}}var Up,ve,vt,zn=N(()=>{"use strict";Up=be(x3()),o0(),Ko(),ve=be(Ni()),Ne(),vt=class Eo{constructor(t,n,i,u,l,d=Up.Guid.create()){this.dims=t,this.type=n,this.dataProvider=i,this.asyncDataProvider=u,this.cache=l,this.dataId=d,this.size=ye.validateDimsAndCalcSize(t);let c=this.size,s=i===void 0&&u===void 0&&l===void 0;if(l!==void 0&&l.length!==c)throw new RangeError("Input dims doesn't match data length.");if(n==="string"){if(l!==void 0&&(!Array.isArray(l)||!l.every(r=>typeof r=="string")))throw new TypeError("cache should be a string array");s&&(this.cache=new Array(c))}else{if(l!==void 0){let r=Y0(n);if(!(l instanceof r))throw new TypeError(`cache should be type ${r.name}`)}if(s){let r=new ArrayBuffer(c*yO(n));this.cache=wO(r,n)}}}get data(){if(this.cache===void 0){let t=this.dataProvider(this.dataId);if(t.length!==this.size)throw new Error("Length of data provided by the Data Provider is inconsistent with the dims of this Tensor.");this.cache=t}return this.cache}get stringData(){if(this.type!=="string")throw new TypeError("data type is not string");return this.data}get integerData(){switch(this.type){case"uint8":case"int8":case"uint16":case"int16":case"int32":case"uint32":case"bool":return this.data;default:throw new TypeError("data type is not integer (uint8, int8, uint16, int16, int32, uint32, bool)")}}get floatData(){switch(this.type){case"float32":case"float64":return this.data;default:throw new TypeError("data type is not float (float32, float64)")}}get numberData(){if(this.type!=="string")return this.data;throw new TypeError("type cannot be non-number (string)")}get(t){return this.data[ye.indicesToOffset(t,this.strides)]}set(t,n){this.data[ye.indicesToOffset(t,this.strides)]=n}async getData(){return this.cache===void 0&&(this.cache=await this.asyncDataProvider(this.dataId)),this.cache}get strides(){return this._strides||(this._strides=ye.computeStrides(this.dims)),this._strides}static fromProto(t){if(!t)throw new Error("cannot construct Value from an empty tensor");let n=yt.tensorDataTypeFromProto(t.dataType),i=yt.tensorDimsFromProto(t.dims),u=new Eo(i,n);if(n==="string")t.stringData.forEach((l,d)=>{u.data[d]=Fu(l)});else if(t.rawData&&typeof t.rawData.byteLength=="number"&&t.rawData.byteLength>0){let l=u.data,d=new DataView(t.rawData.buffer,t.rawData.byteOffset,t.rawData.byteLength),c=jp(t.dataType),s=t.rawData.byteLength/c;if(t.rawData.byteLength%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;r0){let l=u.data,d=new DataView(t.rawDataArray().buffer,t.rawDataArray().byteOffset,t.rawDataLength()),c=jp(t.dataType()),s=t.rawDataLength()/c;if(t.rawDataLength()%c!==0)throw new Error("invalid buffer length");if(l.length!==s)throw new Error("buffer length mismatch");for(let r=0;r) { { gl_Position = vec4(position, 1.0); TexCoords = textureCoord; - }`}function lO(e){let t=Te(e);return`${t.version} + }`}function vO(e){let t=Ae(e);return`${t.version} precision highp float; precision highp int; precision highp sampler2D; @@ -103,14 +103,14 @@ fn main(@builtin(global_invocation_id) id: vec3) { return x - y * (x / y); } - `}function dO(e,t){let n=Te(e);return` + `}function xO(e,t){let n=Ae(e);return` void main() { int indices[${t}]; toVec(TexCoords, indices); vec4 result = vec4(process(indices)); ${n.output} = result; } - `}var q0,H0,qe=R(()=>{"use strict";q0={version:"",attribute:"attribute",varyingVertex:"varying",varyingFrag:"varying",texture2D:"texture2D",output:"gl_FragColor",outputDeclaration:""},H0={version:"#version 300 es",attribute:"in",varyingVertex:"out",varyingFrag:"in",texture2D:"texture",output:"outputColor",outputDeclaration:"out vec4 outputColor;"}}),Ie=R(()=>{"use strict"});async function zp(e,t=o=>0,n){return new Promise((o,u)=>{let l=0,d=()=>{if(e()){o();return}l++;let c=t(l);if(n!=null&&l>=n){u();return}setTimeout(d,c)};d()})}function Ks(e){return So(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)}function cO(e){return So(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)+"AtOutCoords"}function Zn(e,t){let n=JSON.parse(JSON.stringify(e));return n=t,n}function Jn(e,t){return t.map(n=>e[n]).join(", ")}function _r(e){if(e<=1)return"int";if(e===2)return"ivec2";if(e===3)return"ivec3";if(e===4)return"ivec4";if(e===5)return"ivec5";if(e===6)return"ivec6";throw Error(`GPU for rank ${e} is not yet supported`)}function Sn(e=6){return["x","y","z","w","u","v"].slice(0,e)}var Yt=R(()=>{"use strict";Pe()});function pO(e,t){return Sn(t).map(n=>`${e}.${n}`)}function Il(e,t){return t===1?[e]:pO(e,t)}function Ai(){return` + `}var J0,Q0,Ke=N(()=>{"use strict";J0={version:"",attribute:"attribute",varyingVertex:"varying",varyingFrag:"varying",texture2D:"texture2D",output:"gl_FragColor",outputDeclaration:""},Q0={version:"#version 300 es",attribute:"in",varyingVertex:"out",varyingFrag:"in",texture2D:"texture",output:"outputColor",outputDeclaration:"out vec4 outputColor;"}}),Pe=N(()=>{"use strict"});async function Vp(e,t=i=>0,n){return new Promise((i,u)=>{let l=0,d=()=>{if(e()){i();return}l++;let c=t(l);if(n!=null&&l>=n){u();return}setTimeout(d,c)};d()})}function Js(e){return Co(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)}function $O(e){return Co(typeof e<"u"&&e.length!==0,()=>"empty string found for sampler name"),"get"+e.charAt(0).toUpperCase()+e.slice(1)+"AtOutCoords"}function ni(e,t){let n=JSON.parse(JSON.stringify(e));return n=t,n}function ii(e,t){return t.map(n=>e[n]).join(", ")}function Ir(e){if(e<=1)return"int";if(e===2)return"ivec2";if(e===3)return"ivec3";if(e===4)return"ivec4";if(e===5)return"ivec5";if(e===6)return"ivec6";throw Error(`GPU for rank ${e} is not yet supported`)}function Dn(e=6){return["x","y","z","w","u","v"].slice(0,e)}var sr=N(()=>{"use strict";Ne()});function TO(e,t){return Dn(t).map(n=>`${e}.${n}`)}function kl(e,t){return t===1?[e]:TO(e,t)}function zi(){return` float getChannel(vec4 frag, int dim) { int modCoord = imod(dim, 2); return modCoord == 0 ? frag.r : frag.g; @@ -122,23 +122,23 @@ fn main(@builtin(global_invocation_id) id: vec3) { (modCoord.y == 0. ? frag.r : frag.g) : (modCoord.y == 0. ? frag.b : frag.a); } - `}var An=R(()=>{"use strict";Yt()});function hO(e,t,n){if(e===0)return"false";if(e===1)return`rc > ${t[0]}`;let o="";for(let u=e-2;u= ${t[u-e+2]}`,u{"use strict";sr()});function SO(e,t,n){if(e===0)return"false";if(e===1)return`rc > ${t[0]}`;let i="";for(let u=e-2;u= ${t[u-e+2]}`,u= ${e[0]} ? 0. : getA(rc + 1), - 0, 0`;let o="r, c",u="r, cp1",l="rp1, c",d="rp1, cp1",c="";if(n>2)for(let s=0;s2)for(let s=0;s= ${o}; + bool rEdge = rp1 >= ${i}; bool cEdge = cp1 >= ${n}; - `}var Xs,Bp,W0,gO=R(()=>{"use strict";qe(),Ie(),Yt(),An(),Xs={name:"pack",inputNames:["A"],inputTypes:[1]},Bp=(e,t)=>{let n=Te(e.session.backend.glContext.version),o=t.dims,u=o.length,l=t.dims.length,d=_r(l),c=Il("rc",l),s=mO(l,c,o[o.length-2],o[o.length-1]),r;u===0?r=[1,1]:u===1?r=[o[0],1]:r=[o[l-1],o[l-2]];let i=hO(l,r,c),a=fO(o,c),p=` + `}var Qs,Gp,ew,EO=N(()=>{"use strict";Ke(),Pe(),sr(),Mn(),Qs={name:"pack",inputNames:["A"],inputTypes:[1]},Gp=(e,t)=>{let n=Ae(e.session.backend.glContext.version),i=t.dims,u=i.length,l=t.dims.length,d=Ir(l),c=kl("rc",l),s=OO(l,c,i[i.length-2],i[i.length-1]),r;u===0?r=[1,1]:u===1?r=[i[0],1]:r=[i[l-1],i[l-2]];let o=SO(l,r,c),a=IO(i,c),p=` void main() { ${d} rc = getOutputCoords(); - if(${i}) { + if(${o}) { ${n.output} = vec4(0); } else { ${s} @@ -146,18 +146,18 @@ fn main(@builtin(global_invocation_id) id: vec3) { ${n.output} = vec4(${a}); } } - `;return{...Xs,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:2},shaderSource:p}},W0=(e,t)=>({...Xs,get:()=>Bp(e,t)})});function Mp(e){if(e.length===0)return[1,1,1];let t=1;for(let n=0;n1?e[e.length-2]:1,e[e.length-1]]}function bO(e,t){let n=!1;return e.length===0||t.length===0?n=!0:e.length<2||t.length<2?n=e[e.length-1]===t[t.length-1]:n=e[e.length-1]===t[t.length-1]&&e[e.length-2]===t[t.length-2],n}function yO(e){let t=he.computeStrides(e),n=["b","r","c"],o="index";return` + `;return{...Qs,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:2},shaderSource:p}},ew=(e,t)=>({...Qs,get:()=>Gp(e,t)})});function qp(e){if(e.length===0)return[1,1,1];let t=1;for(let n=0;n1?e[e.length-2]:1,e[e.length-1]]}function AO(e,t){let n=!1;return e.length===0||t.length===0?n=!0:e.length<2||t.length<2?n=e[e.length-1]===t[t.length-1]:n=e[e.length-1]===t[t.length-1]&&e[e.length-2]===t[t.length-2],n}function kO(e){let t=ye.computeStrides(e),n=["b","r","c"],i="index";return` ivec3 inputCoordsFromReshapedOutCoords(int index) { - ${t.map((u,l)=>{let d=`int ${n[l]} = ${o} / ${u}`,c=l===t.length-1?`int ${n[l+1]} = ${o} - ${n[l]} * ${u}`:`index -= ${n[l]} * ${u}`;return`${d}; ${c};`}).join("")} + ${t.map((u,l)=>{let d=`int ${n[l]} = ${i} / ${u}`,c=l===t.length-1?`int ${n[l+1]} = ${i} - ${n[l]} * ${u}`:`index -= ${n[l]} * ${u}`;return`${d}; ${c};`}).join("")} return ivec3(b, r, c); } - `}function wO(e){let t=he.computeStrides(e);return` + `}function PO(e){let t=ye.computeStrides(e);return` int getFlattenedIndex(ivec3 coords) { // reverse y, z order return coords.x * ${t[0]} + coords.z * ${t[1]} + coords.y; } -`}var Fp,jp,K0,_O=R(()=>{"use strict";Pe(),qe(),Ie(),An(),Fp=e=>({name:"Reshape (packed)",inputTypes:[2],inputNames:["A"],cacheHint:`${e}`}),jp=(e,t,n,o)=>{let u=t.dims,l=o,d="";for(let r=0;r<4;r++){let i="";switch(r){case 0:i="outputCoords = rc;";break;case 1:i="outputCoords = ivec3(rc.x, rc.y+1, rc.z);";break;case 2:i="outputCoords = ivec3(rc.x, rc.y, rc.z+1);";break;case 3:i="outputCoords = ivec3(rc.x, rc.y+1, rc.z+1);";break;default:throw new Error}d+=` - ${i} +`}var Hp,Wp,tw,CO=N(()=>{"use strict";Ne(),Ke(),Pe(),Mn(),Hp=e=>({name:"Reshape (packed)",inputTypes:[2],inputNames:["A"],cacheHint:`${e}`}),Wp=(e,t,n,i)=>{let u=t.dims,l=i,d="";for(let r=0;r<4;r++){let o="";switch(r){case 0:o="outputCoords = rc;";break;case 1:o="outputCoords = ivec3(rc.x, rc.y+1, rc.z);";break;case 2:o="outputCoords = ivec3(rc.x, rc.y, rc.z+1);";break;case 3:o="outputCoords = ivec3(rc.x, rc.y+1, rc.z+1);";break;default:throw new Error}d+=` + ${o} ${r>0?"if(outputCoords.y < rows && outputCoords.z < cols){":""} int flattenedIndex = getFlattenedIndex(outputCoords); @@ -167,10 +167,10 @@ fn main(@builtin(global_invocation_id) id: vec3) { result[${r}] = getChannel(getA(inputRC.x, inputRC.y, inputRC.z), innerDims); ${r>0?"}":""} - `}let c=Te(e.session.backend.glContext.version),s=` - ${yO(u)} - ${wO(l)} - ${Ai()} + `}let c=Ae(e.session.backend.glContext.version),s=` + ${kO(u)} + ${PO(l)} + ${zi()} void main() { ivec3 rc = getOutputCoords(); @@ -184,7 +184,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { ${d} ${c.output} = result; } - `;return{...n,output:{dims:l,type:t.type,textureType:2},shaderSource:s,hasMain:!0}},K0=(e,t,n)=>{let o=Fp(n);return{...o,get:()=>jp(e,t,o,n)}}}),Fu,vO=R(()=>{"use strict";qe(),Ie(),Fu=(e,t)=>{let n=t.shape,o=Te(e.session.backend.glContext.version),u=` + `;return{...n,output:{dims:l,type:t.type,textureType:2},shaderSource:s,hasMain:!0}},tw=(e,t,n)=>{let i=Hp(n);return{...i,get:()=>Wp(e,t,i,n)}}}),Vu,DO=N(()=>{"use strict";Ke(),Pe(),Vu=(e,t)=>{let n=t.shape,i=Ae(e.session.backend.glContext.version),u=` const float FLOAT_MAX = 1.70141184e38; const float FLOAT_MIN = 1.17549435e-38; @@ -229,9 +229,9 @@ fn main(@builtin(global_invocation_id) id: vec3) { } void main() { - float value = ${o.texture2D}(X,TexCoords).r; - ${o.output} = encodeAsUint8(value); - }`,l={name:"Uint8Encode",inputTypes:[0],inputNames:["X"],output:{dims:n,type:t.tensor.type,textureType:3},shaderSource:u,hasMain:!0};return e.executeProgram(l,[t.tensor])}});function xO(e,t){if(e===1)return"rc";let n="";for(let o=0;o{"use strict";qe(),Ie(),Yt(),An(),Zs={name:"unpack",inputNames:["A"],inputTypes:[2]},Lp=(e,t)=>{let n=t.dims.length,o=Il("rc",n),u=o.slice(-2),l=_r(n),d=Ai(),c=t.dims.length===0?"":xO(n,o),s=n<=1?"rc":`vec2(${u.join(",")})`,r=Te(e.session.backend.glContext.version),i=` + float value = ${i.texture2D}(X,TexCoords).r; + ${i.output} = encodeAsUint8(value); + }`,l={name:"Uint8Encode",inputTypes:[0],inputNames:["X"],output:{dims:n,type:t.tensor.type,textureType:3},shaderSource:u,hasMain:!0};return e.executeProgram(l,[t.tensor])}});function RO(e,t){if(e===1)return"rc";let n="";for(let i=0;i{"use strict";Ke(),Pe(),sr(),Mn(),ea={name:"unpack",inputNames:["A"],inputTypes:[2]},Kp=(e,t)=>{let n=t.dims.length,i=kl("rc",n),u=i.slice(-2),l=Ir(n),d=zi(),c=t.dims.length===0?"":RO(n,i),s=n<=1?"rc":`vec2(${u.join(",")})`,r=Ae(e.session.backend.glContext.version),o=` ${d} void main() { ${l} rc = getOutputCoords(); @@ -241,51 +241,51 @@ fn main(@builtin(global_invocation_id) id: vec3) { ${r.output} = vec4(getChannel(packedInput, ${s}), 0, 0, 0); } - `;return{...Zs,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:i}},X0=(e,t)=>({...Zs,get:()=>Lp(e,t)})}),Z0,ju,J0,Oo=R(()=>{"use strict";Bt(),Z0=class{constructor(e,t=1){if(t===1)this.internalFormat=e.R32F,this.format=e.RED,this.textureType=e.FLOAT,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA32F,this.format=e.RGBA,this.textureType=e.FLOAT,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){let n,o;return e.constructor!==Float32Array&&(We.warning("Encoder","data was not of type Float32; creating new Float32Array"),o=new Float32Array(e)),t*this.channelSize>e.length?(We.warning("Encoder","Source data too small. Allocating larger array"),o=e,n=this.allocate(t*this.channelSize),o.forEach((u,l)=>n[l]=u)):(o=e,n=o),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,o)=>o%4===0).subarray(0,t):e.subarray(0,t)}},ju=class{constructor(e,t=1,n){if(t!==1&&t!==4)throw new Error(`Invalid number of channels: ${t}`);this.internalFormat=e.RGBA,this.format=e.RGBA,this.channelSize=t,this.textureType=n||e.FLOAT}encode(e,t){let n=e;return this.channelSize===1&&(We.verbose("Encoder","Exploding into a larger array"),n=this.allocate(t),e.forEach((o,u)=>n[u*4]=o)),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,o)=>o%4===0).subarray(0,t):e.subarray(0,t)}},J0=class{constructor(e,t=1){if(this.channelSize=4,t===1)this.internalFormat=e.ALPHA,this.format=e.ALPHA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA,this.format=e.RGBA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}allocate(e){return new Uint8Array(e*this.channelSize)}decode(e,t){if(e instanceof Uint8Array)return e.subarray(0,t);throw new Error(`Invalid array type: ${e.constructor}`)}}}),_i,Y0,Lu,TO=R(()=>{"use strict";Pe(),Ie(),_i=(e,t,n)=>{let o=n===0||n===1?1:4,u=n===2,l=n===1||n===2,d=n===4?t.length-1:void 0,c=n===4?t.map((s,r)=>r===t.length-1?s*4:s):void 0;return Lu(e,t,o,c,{isPacked:u,reverseWH:l,breakAxis:d})},Y0=(e,t,n)=>{let o=_i(e,t,n);return[o.width,o.height]},Lu=(e,t,n=1,o,u)=>{let l=!!(u&&u.isPacked),[d,c]=e.computeTextureWH(l&&o||t,u),s=t.length,r=t.slice(0);if(s===0&&(r=[1]),n===1)o=t;else if(l){if(n!==4)throw new Error("a packed texture must be 4-channel");o=t,s>0&&(r[s-1]=Math.ceil(r[s-1]/2)),s>1&&(r[s-2]=Math.ceil(r[s-2]/2))}else if(!o)throw new Error("Unpacked shape is needed when using channels > 1");return{width:d,height:c,channels:n,isPacked:l,shape:r,strides:he.computeStrides(r),unpackedShape:o,reversedWH:u&&u.reverseWH}}}),Up,Q0,SO=R(()=>{"use strict";Bt(),En(),Pe(),gO(),_O(),vO(),$O(),Oo(),TO(),Ie(),Up=(e,t)=>{let n=t.map(u=>`${u.unpackedShape.join(",")};${u.width}x${u.height}`).join("_"),o=e.name;return e.cacheHint&&(o+="["+e.cacheHint+"]"),o+=":"+n,o},Q0=class{constructor(e){this.session=e,this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map}calculateTextureWidthAndHeight(e,t){return Y0(this.session.layoutStrategy,e,t)}executeProgram(e,t){if(t.lengththis.readTexture(l),async d=>this.readTextureAsync(l),void 0,u),texture:n};return this.setTextureData(l.tensor.dataId,l,e.isPacked),l}getTextureData(e,t=!1){return this.session.isInitializer(e)?this.session.getTextureData(e,t):t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){this.session.isInitializer(e)?this.session.setTextureData(e,t,n):(n?this.packedTextureDataCache:this.unpackedTextureDataCache).set(e,t)}isTextureLayoutCached(e,t=!1){return!!this.getTextureData(e.dataId,t)}dispose(){this.session.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.unpackedTextureDataCache=new Map}readTexture(e){return e.isPacked?this.readTexture(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTexture(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(Fu(this,e))}async readTextureAsync(e){return e.isPacked?this.readTextureAsync(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTextureAsync(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(Fu(this,e))}pack(e){return this.executeProgram(W0(this,e.tensor),[e.tensor])}unpack(e){return this.executeProgram(X0(this,e.tensor),[e.tensor])}}}),Vp,Be,nt=R(()=>{"use strict";Vp=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},Be=e=>new Vp(e)}),Js,ew,tw,Gp,qp,IO=R(()=>{"use strict";nt(),qe(),Ie(),Js={name:"BatchNormalization",inputNames:["A","Scale","B","Mean","Variance"],inputTypes:[0,0,0,0,0]},ew=(e,t,n)=>(qp(t),[e.run({...Js,cacheHint:n.cacheKey,get:()=>Gp(e,t,n)},t)]),tw=e=>{let t=e.attributes.getFloat("epsilon",1e-5),n=e.attributes.getFloat("momentum",.9),o=e.attributes.getInt("spatial",1);return Be({epsilon:t,momentum:n,spatial:o})},Gp=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),u=t[0].dims.length,[l,d]=e.calculateTextureWidthAndHeight(t[1].dims,0),c=` + `;return{...ea,hasMain:!0,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:o}},rw=(e,t)=>({...ea,get:()=>Kp(e,t)})}),nw,Gu,iw,Ro=N(()=>{"use strict";Gt(),nw=class{constructor(e,t=1){if(t===1)this.internalFormat=e.R32F,this.format=e.RED,this.textureType=e.FLOAT,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA32F,this.format=e.RGBA,this.textureType=e.FLOAT,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){let n,i;return e.constructor!==Float32Array&&(Ye.warning("Encoder","data was not of type Float32; creating new Float32Array"),i=new Float32Array(e)),t*this.channelSize>e.length?(Ye.warning("Encoder","Source data too small. Allocating larger array"),i=e,n=this.allocate(t*this.channelSize),i.forEach((u,l)=>n[l]=u)):(i=e,n=i),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,i)=>i%4===0).subarray(0,t):e.subarray(0,t)}},Gu=class{constructor(e,t=1,n){if(t!==1&&t!==4)throw new Error(`Invalid number of channels: ${t}`);this.internalFormat=e.RGBA,this.format=e.RGBA,this.channelSize=t,this.textureType=n||e.FLOAT}encode(e,t){let n=e;return this.channelSize===1&&(Ye.verbose("Encoder","Exploding into a larger array"),n=this.allocate(t),e.forEach((i,u)=>n[u*4]=i)),n}allocate(e){return new Float32Array(e*4)}decode(e,t){return this.channelSize===1?e.filter((n,i)=>i%4===0).subarray(0,t):e.subarray(0,t)}},iw=class{constructor(e,t=1){if(this.channelSize=4,t===1)this.internalFormat=e.ALPHA,this.format=e.ALPHA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else if(t===4)this.internalFormat=e.RGBA,this.format=e.RGBA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=t;else throw new Error(`Invalid number of channels: ${t}`)}encode(e,t){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}allocate(e){return new Uint8Array(e*this.channelSize)}decode(e,t){if(e instanceof Uint8Array)return e.subarray(0,t);throw new Error(`Invalid array type: ${e.constructor}`)}}}),Oi,ow,qu,zO=N(()=>{"use strict";Ne(),Pe(),Oi=(e,t,n)=>{let i=n===0||n===1?1:4,u=n===2,l=n===1||n===2,d=n===4?t.length-1:void 0,c=n===4?t.map((s,r)=>r===t.length-1?s*4:s):void 0;return qu(e,t,i,c,{isPacked:u,reverseWH:l,breakAxis:d})},ow=(e,t,n)=>{let i=Oi(e,t,n);return[i.width,i.height]},qu=(e,t,n=1,i,u)=>{let l=!!(u&&u.isPacked),[d,c]=e.computeTextureWH(l&&i||t,u),s=t.length,r=t.slice(0);if(s===0&&(r=[1]),n===1)i=t;else if(l){if(n!==4)throw new Error("a packed texture must be 4-channel");i=t,s>0&&(r[s-1]=Math.ceil(r[s-1]/2)),s>1&&(r[s-2]=Math.ceil(r[s-2]/2))}else if(!i)throw new Error("Unpacked shape is needed when using channels > 1");return{width:d,height:c,channels:n,isPacked:l,shape:r,strides:ye.computeStrides(r),unpackedShape:i,reversedWH:u&&u.reverseWH}}}),Xp,sw,MO=N(()=>{"use strict";Gt(),zn(),Ne(),EO(),CO(),DO(),NO(),Ro(),zO(),Pe(),Xp=(e,t)=>{let n=t.map(u=>`${u.unpackedShape.join(",")};${u.width}x${u.height}`).join("_"),i=e.name;return e.cacheHint&&(i+="["+e.cacheHint+"]"),i+=":"+n,i},sw=class{constructor(e){this.session=e,this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map}calculateTextureWidthAndHeight(e,t){return ow(this.session.layoutStrategy,e,t)}executeProgram(e,t){if(t.lengththis.readTexture(l),async d=>this.readTextureAsync(l),void 0,u),texture:n};return this.setTextureData(l.tensor.dataId,l,e.isPacked),l}getTextureData(e,t=!1){return this.session.isInitializer(e)?this.session.getTextureData(e,t):t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){this.session.isInitializer(e)?this.session.setTextureData(e,t,n):(n?this.packedTextureDataCache:this.unpackedTextureDataCache).set(e,t)}isTextureLayoutCached(e,t=!1){return!!this.getTextureData(e.dataId,t)}dispose(){this.session.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.unpackedTextureDataCache=new Map}readTexture(e){return e.isPacked?this.readTexture(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTexture(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(Vu(this,e))}async readTextureAsync(e){return e.isPacked?this.readTextureAsync(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTextureAsync(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(Vu(this,e))}pack(e){return this.executeProgram(ew(this,e.tensor),[e.tensor])}unpack(e){return this.executeProgram(rw(this,e.tensor),[e.tensor])}}}),Zp,Le,ut=N(()=>{"use strict";Zp=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},Le=e=>new Zp(e)}),ta,aw,uw,Yp,Jp,BO=N(()=>{"use strict";ut(),Ke(),Pe(),ta={name:"BatchNormalization",inputNames:["A","Scale","B","Mean","Variance"],inputTypes:[0,0,0,0,0]},aw=(e,t,n)=>(Jp(t),[e.run({...ta,cacheHint:n.cacheKey,get:()=>Yp(e,t,n)},t)]),uw=e=>{let t=e.attributes.getFloat("epsilon",1e-5),n=e.attributes.getFloat("momentum",.9),i=e.attributes.getInt("spatial",1);return Le({epsilon:t,momentum:n,spatial:i})},Yp=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),u=t[0].dims.length,[l,d]=e.calculateTextureWidthAndHeight(t[1].dims,0),c=` float process(int[${u}] indices) { vec2 position = offsetToCoords(indices[1], ${l}, ${d}); - float scale = getColorAsFloat(${o.texture2D}(Scale, position)); - float mean = getColorAsFloat(${o.texture2D}(Mean, position)); - float variance = getColorAsFloat(${o.texture2D}(Variance, position)); - float b = getColorAsFloat(${o.texture2D}(B, position)); + float scale = getColorAsFloat(${i.texture2D}(Scale, position)); + float mean = getColorAsFloat(${i.texture2D}(Mean, position)); + float variance = getColorAsFloat(${i.texture2D}(Variance, position)); + float b = getColorAsFloat(${i.texture2D}(B, position)); return scale * ( (_A(indices) - mean) / sqrt(variance + float(${n.epsilon})) ) + b; - }`;return{...Js,output:{dims:t[0].dims,type:t[0].type,textureType:0},shaderSource:c}},qp=e=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs.");let t=e[0],n=e[1],o=e[2],u=e[3],l=e[4];if(t.dims.length<3||n.dims.length!==1||o.dims.length!==1||u.dims.length!==1||l.dims.length!==1)throw new Error("invalid input shape.");if(n.dims[0]!==t.dims[1]||o.dims[0]!==t.dims[1]||u.dims[0]!==t.dims[1]||l.dims[0]!==t.dims[1])throw new Error("invalid input shape.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||o.type!=="float32"&&o.type!=="float64"||u.type!=="float32"&&u.type!=="float64"||l.type!=="float32"&&l.type!=="float64")throw new Error("invalid input tensor types.")}}),rw,kn,te,Uu,nw,xr=R(()=>{"use strict";rw=class{constructor(e,t,n,o){this.glContext=e,this.programInfo=t,this.inputTextureLayouts=n,this.outputTextureLayout=o}},kn=class{constructor(e){this.context=e}},te=class{constructor(e,t){this.routineBody=e,this.dependencies=t}},Uu=class{constructor(e,t,n){this.name=e,n?this.dependencies=n:this.dependencies=[],t&&(this.routineBody=t)}addDependency(e){e&&this.dependencies.push(e)}},nw=class{static returnOrderedNodes(e){if(!e||e.length===0)return[];if(e.length===1)return e;let t=new Set,n=new Set,o=new Array;return this.createOrderedNodes(e,t,n,o),o}static createOrderedNodes(e,t,n,o){for(let u=0;u0)for(let l=0;l{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs.");let t=e[0],n=e[1],i=e[2],u=e[3],l=e[4];if(t.dims.length<3||n.dims.length!==1||i.dims.length!==1||u.dims.length!==1||l.dims.length!==1)throw new Error("invalid input shape.");if(n.dims[0]!==t.dims[1]||i.dims[0]!==t.dims[1]||u.dims[0]!==t.dims[1]||l.dims[0]!==t.dims[1])throw new Error("invalid input shape.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||i.type!=="float32"&&i.type!=="float64"||u.type!=="float32"&&u.type!=="float64"||l.type!=="float32"&&l.type!=="float64")throw new Error("invalid input tensor types.")}}),lw,Bn,re,Hu,dw,Er=N(()=>{"use strict";lw=class{constructor(e,t,n,i){this.glContext=e,this.programInfo=t,this.inputTextureLayouts=n,this.outputTextureLayout=i}},Bn=class{constructor(e){this.context=e}},re=class{constructor(e,t){this.routineBody=e,this.dependencies=t}},Hu=class{constructor(e,t,n){this.name=e,n?this.dependencies=n:this.dependencies=[],t&&(this.routineBody=t)}addDependency(e){e&&this.dependencies.push(e)}},dw=class{static returnOrderedNodes(e){if(!e||e.length===0)return[];if(e.length===1)return e;let t=new Set,n=new Set,i=new Array;return this.createOrderedNodes(e,t,n,i),i}static createOrderedNodes(e,t,n,i){for(let u=0;u0)for(let l=0;l b); } @@ -295,7 +295,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { v1.b > v2.b, v1.a > v2.a ); } - `,name:e,type:0}}function DO(){let e="less_";return{body:` + `,name:e,type:0}}function qO(){let e="less_";return{body:` float ${e}(float a, float b) { return float(a < b); } @@ -305,7 +305,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { v1.b < v2.b, v1.a < v2.a ); } - `,name:e,type:0}}function RO(){let e="and_";return{body:` + `,name:e,type:0}}function HO(){let e="and_";return{body:` float ${e}(float a, float b) { return float( bool(a) && bool(b) ); } @@ -317,7 +317,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { b1.b && b2.b, b1.a && b2.a ); } - `,name:e,type:0}}function NO(){return{body:` + `,name:e,type:0}}function WO(){return{body:` float or_(float a, float b) { return float( bool(a) || bool(b) ); } @@ -329,7 +329,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { b1.b || b2.b, b1.a || b2.a ); } - `,name:"or_",type:0}}function zO(){let e="xor_";return{body:` + `,name:"or_",type:0}}function KO(){let e="xor_";return{body:` float ${e}(float a, float b) { return float( bool(a) ^^ bool(b) ); } @@ -341,7 +341,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { b1.b ^^ b2.b, b1.a ^^ b2.a ); } - `,name:e,type:0}}function BO(){return FO("pow")}function MO(){let e="prelu_";return{body:` + `,name:e,type:0}}function XO(){return YO("pow")}function ZO(){let e="prelu_";return{body:` float ${e}(float a, float b) { return a < 0.0 ? a * b: a; } @@ -353,29 +353,29 @@ fn main(@builtin(global_invocation_id) id: vec3) { v1.a < 0.0 ? v1.a * v2.a: v1.a ); } - `,name:e,type:0}}function FO(e){let t=`${e}_`;return{body:` + `,name:e,type:0}}function YO(e){let t=`${e}_`;return{body:` float ${t}(float a, float b) { return ${e}(a, b); } vec4 ${t}(vec4 v1, vec4 v2) { return ${e}(v1, v2); } - `,name:t,type:0}}var ct,Hp,iw,ow,sw,aw,uw,lw,dw,cw,pw,hw,fw,mw,jO=R(()=>{"use strict";Pe(),xr(),qe(),Ie(),ct=(e,t,n,o=t[0].type,u)=>{let l=e.session.pack?2:0;return{name:n.name,inputNames:["A","B"],inputTypes:[l,l],cacheHint:u,get:()=>Hp(e,t,n,o)}},Hp=(e,t,n,o=t[0].type)=>{let u=e.session.pack?2:0,l=!he.areEqual(t[0].dims,t[1].dims),d=t[0].dims,c=e.session.pack;if(l){let i=qt.calcShape(t[0].dims,t[1].dims,!1);if(!i)throw new Error("Can't perform binary op on the given tensors");d=i;let a=d.length,p=t[0].dims.length!==0?t[0].dims.length:1,h=t[1].dims.length!==0?t[1].dims.length:1,m=t[0].dims.length!==0?"bcastIndices_A(indices, aindices);":"aindices[0] = 0;",b=t[1].dims.length!==0?"bcastIndices_B(indices, bindices);":"bindices[0] = 0;",x=Te(e.session.backend.glContext.version),w=c?` + `,name:t,type:0}}var gt,Qp,cw,pw,hw,fw,mw,gw,bw,yw,ww,_w,vw,xw,JO=N(()=>{"use strict";Ne(),Er(),Ke(),Pe(),gt=(e,t,n,i=t[0].type,u)=>{let l=e.session.pack?2:0;return{name:n.name,inputNames:["A","B"],inputTypes:[l,l],cacheHint:u,get:()=>Qp(e,t,n,i)}},Qp=(e,t,n,i=t[0].type)=>{let u=e.session.pack?2:0,l=!ye.areEqual(t[0].dims,t[1].dims),d=t[0].dims,c=e.session.pack;if(l){let o=Qt.calcShape(t[0].dims,t[1].dims,!1);if(!o)throw new Error("Can't perform binary op on the given tensors");d=o;let a=d.length,p=t[0].dims.length!==0?t[0].dims.length:1,h=t[1].dims.length!==0?t[1].dims.length:1,m=t[0].dims.length!==0?"bcastIndices_A(indices, aindices);":"aindices[0] = 0;",g=t[1].dims.length!==0?"bcastIndices_B(indices, bindices);":"bindices[0] = 0;",v=Ae(e.session.backend.glContext.version),w=c?` ${n.body} void main() { vec4 a = getAAtOutCoords(); vec4 b = getBAtOutCoords(); vec4 result = ${n.name}(a, b); - ${x.output} = result; + ${v.output} = result; }`:` ${n.body} float process(int indices[${a}]) { int aindices[${p}]; int bindices[${h}]; ${m} - ${b} + ${g} return ${n.name}(_A(aindices), _B(bindices)); - }`;return{name:n.name,inputNames:["A","B"],inputTypes:[u,u],output:{dims:d,type:o,textureType:u},shaderSource:w,hasMain:c}}let s=Te(e.session.backend.glContext.version),r=` + }`;return{name:n.name,inputNames:["A","B"],inputTypes:[u,u],output:{dims:d,type:i,textureType:u},shaderSource:w,hasMain:c}}let s=Ae(e.session.backend.glContext.version),r=` ${n.body} void main() { vec4 v1 = ${s.texture2D}(A, TexCoords); @@ -383,21 +383,21 @@ fn main(@builtin(global_invocation_id) id: vec3) { vec4 result = ${n.name}(v1, v2); ${s.output} = result; } - `;return{name:n.name,inputNames:["A","B"],inputTypes:[u,u],output:{dims:t[0].dims,type:o,textureType:u},shaderSource:r,hasMain:!0}},iw=(e,t)=>[e.run(ct(e,t,OO()),t)],ow=(e,t)=>[e.run(ct(e,t,RO(),"bool"),t)],sw=(e,t)=>[e.run(ct(e,t,EO()),t)],aw=(e,t)=>[e.run(ct(e,t,PO(),"bool"),t)],uw=(e,t)=>[e.run(ct(e,t,CO(),"bool"),t)],lw=(e,t)=>[e.run(ct(e,t,DO(),"bool"),t)],dw=(e,t)=>[e.run(ct(e,t,AO()),t)],cw=(e,t)=>[e.run(ct(e,t,NO(),"bool"),t)],pw=(e,t)=>[e.run(ct(e,t,BO()),t)],hw=(e,t)=>[e.run(ct(e,t,MO()),t)],fw=(e,t)=>[e.run(ct(e,t,kO()),t)],mw=(e,t)=>[e.run(ct(e,t,zO(),"bool"),t)]}),gw,bw,Wp,LO=R(()=>{"use strict";Pe(),gw=(e,t,n)=>(Wp(t),[e.cast(t[0],n)]),bw=e=>ht.tensorDataTypeFromProto(e.attributes.getInt("to")),Wp=e=>{if(!e||e.length!==1)throw new Error("Cast requires 1 input.");if(e[0].type==="string")throw new Error("Invalid input type.")}}),Kp,Xp,yw,Yn,UO=R(()=>{"use strict";qe(),Ie(),Yt(),An(),Kp=(e,t)=>({name:"Concat (packed)",inputNames:Array.from({length:e},(n,o)=>`X${o}`),inputTypes:Array(e).fill(2),cacheHint:t}),Xp=(e,t,n,o)=>{let u=n[0].dims.slice();if(o>=u.length||o<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");o<0&&(o=u.length+o);let l=u.slice(0);for(let I=1;II.dims),a=Sn(d),p=new Array(i.length-1);p[0]=i[0][o];for(let I=1;I[e.run(gt(e,t,FO()),t)],pw=(e,t)=>[e.run(gt(e,t,HO(),"bool"),t)],hw=(e,t)=>[e.run(gt(e,t,jO()),t)],fw=(e,t)=>[e.run(gt(e,t,VO(),"bool"),t)],mw=(e,t)=>[e.run(gt(e,t,GO(),"bool"),t)],gw=(e,t)=>[e.run(gt(e,t,qO(),"bool"),t)],bw=(e,t)=>[e.run(gt(e,t,LO()),t)],yw=(e,t)=>[e.run(gt(e,t,WO(),"bool"),t)],ww=(e,t)=>[e.run(gt(e,t,XO()),t)],_w=(e,t)=>[e.run(gt(e,t,ZO()),t)],vw=(e,t)=>[e.run(gt(e,t,UO()),t)],xw=(e,t)=>[e.run(gt(e,t,KO(),"bool"),t)]}),$w,Tw,eh,QO=N(()=>{"use strict";Ne(),$w=(e,t,n)=>(eh(t),[e.cast(t[0],n)]),Tw=e=>yt.tensorDataTypeFromProto(e.attributes.getInt("to")),eh=e=>{if(!e||e.length!==1)throw new Error("Cast requires 1 input.");if(e[0].type==="string")throw new Error("Invalid input type.")}}),th,rh,Sw,oi,eE=N(()=>{"use strict";Ke(),Pe(),sr(),Mn(),th=(e,t)=>({name:"Concat (packed)",inputNames:Array.from({length:e},(n,i)=>`X${i}`),inputTypes:Array(e).fill(2),cacheHint:t}),rh=(e,t,n,i)=>{let u=n[0].dims.slice();if(i>=u.length||i<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");i<0&&(i=u.length+i);let l=u.slice(0);for(let O=1;OO.dims),a=Dn(d),p=new Array(o.length-1);p[0]=o[0][i];for(let O=1;O= ${p[I-1]}) { + getX0(${g}), vec2(${m.join()})); + }`;for(let O=1;O= ${p[O-1]}) { return getChannel( - getX${I}(${Yn(a,h,A)}), - vec2(${Yn(m,h,A)})); - }`}let w=p.length,y=p[p.length-1];x+=` + getX${O}(${oi(a,h,A)}), + vec2(${oi(m,h,A)})); + }`}let w=p.length,y=p[p.length-1];v+=` return getChannel( - getX${w}(${Yn(a,h,y)}), - vec2(${Yn(m,h,y)}));`;let S=Te(e.session.backend.glContext.version),T=` + getX${w}(${oi(a,h,y)}), + vec2(${oi(m,h,y)}));`;let T=Ae(e.session.backend.glContext.version),S=` ${r} - float getValue(${a.map(I=>"int "+I)}) { - ${x} + float getValue(${a.map(O=>"int "+O)}) { + ${v} } void main() { @@ -423,26 +423,26 @@ fn main(@builtin(global_invocation_id) id: vec3) { ${c[d-1]} < ${l[d-1]}) { result.b = getValue(${c}); } - ${S.output} = result; + ${T.output} = result; } - `;return{...t,output:{dims:l,type:n[0].type,textureType:2},shaderSource:T,hasMain:!0}},yw=(e,t,n)=>{let o=Kp(t.length,n.cacheKey);return{...o,get:()=>Xp(e,o,t,n.axis)}},Yn=(e,t,n)=>{let o=e.indexOf(t);return e.map((u,l)=>l===o?`${u} - ${n}`:u).join()}}),ww,Zp,Jp,Yp,Ys,Qp,eh,th,_w,rh,VO=R(()=>{"use strict";nt(),Ie(),UO(),ww=(e,t,n)=>(rh(t),e.session.pack&&t[0].dims.length>1?[e.run(yw(e,t,n),t)]:[e.run(Yp(e,t,n),t)]),Zp=(e,t)=>({name:"Concat",inputNames:Array.from({length:e},(n,o)=>`X${o}`),inputTypes:Array(e).fill(0),cacheHint:t}),Jp=(e,t,n,o)=>{let u=n[0].dims.slice();if(o>=u.length||o<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");o<0&&(o=u.length+o);let l=u.slice(0);for(let h=1;h{let i=th(t.length,n.cacheKey);return{...i,get:()=>rh(e,i,t,n.axis)}},oi=(e,t,n)=>{let i=e.indexOf(t);return e.map((u,l)=>l===i?`${u} - ${n}`:u).join()}}),Iw,nh,ih,oh,ra,sh,ah,uh,Ow,lh,tE=N(()=>{"use strict";ut(),Pe(),eE(),Iw=(e,t,n)=>(lh(t),e.session.pack&&t[0].dims.length>1?[e.run(Sw(e,t,n),t)]:[e.run(oh(e,t,n),t)]),nh=(e,t)=>({name:"Concat",inputNames:Array.from({length:e},(n,i)=>`X${i}`),inputTypes:Array(e).fill(0),cacheHint:t}),ih=(e,t,n,i)=>{let u=n[0].dims.slice();if(i>=u.length||i<-1*u.length)throw new Error("axis specified for concat doesn't match input dimensionality");i<0&&(i=u.length+i);let l=u.slice(0);for(let h=1;h{let o=Zp(t.length,n.cacheKey);return{...o,get:()=>Jp(e,o,t,n.axis)}},Ys=e=>`int getTextureWhereDataResides(int index) { + }`;return{...t,output:{dims:l,type:n[0].type,textureType:0},shaderSource:p}},oh=(e,t,n)=>{let i=nh(t.length,n.cacheKey);return{...i,get:()=>ih(e,i,t,n.axis)}},ra=e=>`int getTextureWhereDataResides(int index) { ${e.map((t,n)=>`if(index<${t}) {return ${n};} `).join("")} - }`,Qp=e=>Ys(e),eh=(e,t)=>{let n=[`float fetchDataFromCorrectTexture(int textureIndex, int indices[${t}]) {`];for(let o=0;o{let t=["int getSizeInConcatAxisValueFromIndex(int index) {"];for(let n=0;nBe({axis:e.attributes.getInt("axis")}),rh=e=>{if(!e||e.length<1)throw new Error("too few inputs");let t=e[0].type,n=e[0].dims.length;if(t==="string")throw new Error("string tensor is not supported yet");for(let o of e){if(o.type!==t)throw new Error("input tensors should be one type");if(o.dims.length!==n)throw new Error("input tensors should have the same shape")}}});function GO(){return Ot("abs")}function qO(){return Ot("acos")}function HO(){return Ot("asin")}function WO(){return Ot("atan")}function KO(){return Ot("ceil")}function XO(){return Ot("cos")}function ZO(e){return{body:` + }`,sh=e=>ra(e),ah=(e,t)=>{let n=[`float fetchDataFromCorrectTexture(int textureIndex, int indices[${t}]) {`];for(let i=0;i{let t=["int getSizeInConcatAxisValueFromIndex(int index) {"];for(let n=0;nLe({axis:e.attributes.getInt("axis")}),lh=e=>{if(!e||e.length<1)throw new Error("too few inputs");let t=e[0].type,n=e[0].dims.length;if(t==="string")throw new Error("string tensor is not supported yet");for(let i of e){if(i.type!==t)throw new Error("input tensors should be one type");if(i.dims.length!==n)throw new Error("input tensors should have the same shape")}}});function rE(){return Rt("abs")}function nE(){return Rt("acos")}function iE(){return Rt("asin")}function oE(){return Rt("atan")}function sE(){return Rt("ceil")}function aE(){return Rt("cos")}function uE(e){return{body:` const float alpha = float(${e}); float elu_(float a) { @@ -451,7 +451,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { vec4 elu_(vec4 v) { return vec4(elu_(v.x), elu_(v.y), elu_(v.z), elu_(v.w)); } - `,name:"elu",type:0}}function JO(){return Ot("exp")}function YO(){return Ot("floor")}function vw(e,t){let n="clip";return{body:` + `,name:"elu",type:0}}function lE(){return Rt("exp")}function dE(){return Rt("floor")}function Ew(e,t){let n="clip";return{body:` const float min = float(${e}); const float max = float(${t}); @@ -461,14 +461,14 @@ fn main(@builtin(global_invocation_id) id: vec3) { vec4 ${n}_(vec4 v) { return clamp(v, min, max); } - `,name:n,type:0}}function QO(){let e="indentity";return{body:` + `,name:n,type:0}}function cE(){let e="indentity";return{body:` float ${e}_(float a) { return a; } vec4 ${e}_(vec4 v) { return v; } - `,name:e,type:0}}function eE(e){let t="leakyRelu";return{body:` + `,name:e,type:0}}function pE(e){let t="leakyRelu";return{body:` const float alpha = float(${e}); float ${t}_(float a) { @@ -477,14 +477,14 @@ fn main(@builtin(global_invocation_id) id: vec3) { vec4 ${t}_(vec4 v) { return vec4(${t}_(v.x), ${t}_(v.y), ${t}_(v.z), ${t}_(v.w)); } - `,name:t,type:0}}function tE(){return Ot("log")}function rE(){return{body:` + `,name:t,type:0}}function hE(){return Rt("log")}function fE(){return{body:` float neg_(float a) { return -a; } vec4 neg_(vec4 v) { return -v; } - `,name:"neg",type:0}}function nE(){return{body:` + `,name:"neg",type:0}}function mE(){return{body:` float not_(float a) { return float( ! bool(a) ); } @@ -497,21 +497,21 @@ fn main(@builtin(global_invocation_id) id: vec3) { bvec4 not_(bvec4 v) { return bvec4(!v.x, !v.y, !v.z, !v.w); } - `,name:"not",type:0}}function iE(){return Ot("sin")}function xw(){let e="relu";return{body:` + `,name:"not",type:0}}function gE(){return Rt("sin")}function Aw(){let e="relu";return{body:` float ${e}_(float a) { return max( a, 0.0 ); } vec4 ${e}_(vec4 v) { return max( v, 0.0 ); } - `,name:e,type:0}}function $w(){let e="sigmoid";return{body:` + `,name:e,type:0}}function kw(){let e="sigmoid";return{body:` float ${e}_(float a) { return 1.0 / (1.0 + exp(-a)); } vec4 ${e}_(vec4 v) { return 1.0 / (1.0 + exp(-v)); } - `,name:e,type:0}}function oE(){return Ot("sqrt")}function sE(){return Ot("tan")}function aE(){let e="tanh";return{body:` + `,name:e,type:0}}function bE(){return Rt("sqrt")}function yE(){return Rt("tan")}function wE(){let e="tanh";return{body:` float ${e}_(float a) { a = clamp(a, -10., 10.); a = exp(2.*a); @@ -522,24 +522,24 @@ fn main(@builtin(global_invocation_id) id: vec3) { v = exp(2.*v); return (v - 1.) / (v + 1.); } - `,name:e,type:0}}function Ot(e){return{body:` + `,name:e,type:0}}function Rt(e){return{body:` float ${e}_(float a) { return ${e}(a); } vec4 ${e}_(vec4 v) { return ${e}(v); } - `,name:e,type:0}}var nh,He,Tw,Sw,Iw,Ow,Vu,Ew,Aw,ih,kw,Pw,Cw,Dw,Rw,Nw,Gu,zw,Bw,Mw,Fw,jw,Lw,Uw,Vw,Gw,qw,Hw,Ww=R(()=>{"use strict";nt(),Pe(),xr(),qe(),Ie(),nh=(e,t,n,o)=>{let u=e.session.pack?2:0,l=Te(e.session.backend.glContext.version);return{...t,output:{dims:n.dims,type:n.type,textureType:u},shaderSource:` - ${o.body} + `,name:e,type:0}}var dh,Ze,Pw,Cw,Dw,Rw,Wu,Nw,zw,ch,Mw,Bw,Fw,jw,Lw,Uw,Ku,Vw,Gw,qw,Hw,Ww,Kw,Xw,Zw,Yw,Jw,Qw,e_=N(()=>{"use strict";ut(),Ne(),Er(),Ke(),Pe(),dh=(e,t,n,i)=>{let u=e.session.pack?2:0,l=Ae(e.session.backend.glContext.version);return{...t,output:{dims:n.dims,type:n.type,textureType:u},shaderSource:` + ${i.body} void main() { vec4 v = ${l.texture2D}(A, TexCoords); - v = ${o.name}_(v); + v = ${i.name}_(v); ${l.output} = v; } - `,hasMain:!0}},He=(e,t,n,o)=>{let u=e.session.pack?2:0,l={name:n.name,inputTypes:[u],inputNames:["A"],cacheHint:o};return{...l,get:()=>nh(e,l,t,n)}},Tw=(e,t)=>[e.run(He(e,t[0],GO()),t)],Sw=(e,t)=>[e.run(He(e,t[0],qO()),t)],Iw=(e,t)=>[e.run(He(e,t[0],HO()),t)],Ow=(e,t)=>[e.run(He(e,t[0],WO()),t)],Vu=(e,t,n)=>[e.run(He(e,t[0],vw(n.min,n.max),n.cacheKey),t)],Ew=e=>Be({min:e.attributes.getFloat("min",Ti),max:e.attributes.getFloat("max",Si)}),Aw=(e,t)=>{let n=ih(e,t);return Vu(e,[t[0]],n)},ih=(e,t)=>{if(t.length>=3&&(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)))throw new Error("dynamic clip attributes are not allowed");let n=t.length>=3?t[1].numberData[0]:Ti,o=t.length>=3?t[2].numberData[0]:Si;return Be({min:n,max:o})},kw=(e,t)=>[e.run(He(e,t[0],KO()),t)],Pw=(e,t)=>[e.run(He(e,t[0],XO()),t)],Cw=(e,t,n)=>[e.run(He(e,t[0],ZO(n.alpha),n.cacheKey),t)],Dw=e=>Be({alpha:e.attributes.getFloat("alpha",1)}),Rw=(e,t)=>[e.run(He(e,t[0],JO()),t)],Nw=(e,t)=>[e.run(He(e,t[0],YO()),t)],Gu=(e,t)=>[e.run(He(e,t[0],QO()),t)],zw=(e,t,n)=>[e.run(He(e,t[0],eE(n.alpha),n.cacheKey),t)],Bw=e=>Be({alpha:e.attributes.getFloat("alpha",.01)}),Mw=(e,t)=>[e.run(He(e,t[0],tE()),t)],Fw=(e,t)=>[e.run(He(e,t[0],rE()),t)],jw=(e,t)=>[e.run(He(e,t[0],nE()),t)],Lw=(e,t)=>[e.run(He(e,t[0],xw()),t)],Uw=(e,t)=>[e.run(He(e,t[0],$w()),t)],Vw=(e,t)=>[e.run(He(e,t[0],iE()),t)],Gw=(e,t)=>[e.run(He(e,t[0],oE()),t)],qw=(e,t)=>[e.run(He(e,t[0],sE()),t)],Hw=(e,t)=>[e.run(He(e,t[0],aE()),t)]});function ki(e){let t;switch(e.activation){case"Relu":t=xw();break;case"Sigmoid":t=$w();break;case"Clip":t=vw(e.clipMin,e.clipMax);break;default:return{activationFunction:"",applyActivation:""}}let n=t.name,o=t.body,u=`value = ${n}_(value);`;return{activationFunction:o,applyActivation:u}}var Uo,Pn=R(()=>{"use strict";Pe(),Ww(),Uo=e=>{let t=e.getString("activation","");if(t==="Clip"){let[n,o]=e.getFloats("activation_params",[Ti,Si]);return{activation:t,clipMax:o,clipMin:n,activationCacheKey:`${t}:${n},${o}`}}return{activation:t,activationCacheKey:t}}}),oh,sh,Kw,uE=R(()=>{"use strict";Bt(),qe(),Ie(),Al(),Pn(),oh=(e,t)=>({name:"GroupedConv",inputNames:e?["X","W","Bias"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),sh=(e,t,n,o)=>{let u=t.length>2?"value += getBias(output_channel);":"",l=t[0].dims.slice(),d=t[1].dims.slice(),c=d[0]/o.group;We.verbose("GroupedConv",`autpPad:${o.autoPad}, dilations:${o.dilations}, group:${o.group}, kernelShape:${o.kernelShape}, pads:${o.pads}, strides:${o.strides}`);let s=vi(l,d,o.dilations,o.pads,o.strides),r=Te(e.session.backend.glContext.version),{activationFunction:i,applyActivation:a}=ki(o),p=` - const ivec2 strides = ivec2(${o.strides[0]}, ${o.strides[1]}); - const ivec2 pads = ivec2(${o.pads[0]}, ${o.pads[1]}); - ${i} + `,hasMain:!0}},Ze=(e,t,n,i)=>{let u=e.session.pack?2:0,l={name:n.name,inputTypes:[u],inputNames:["A"],cacheHint:i};return{...l,get:()=>dh(e,l,t,n)}},Pw=(e,t)=>[e.run(Ze(e,t[0],rE()),t)],Cw=(e,t)=>[e.run(Ze(e,t[0],nE()),t)],Dw=(e,t)=>[e.run(Ze(e,t[0],iE()),t)],Rw=(e,t)=>[e.run(Ze(e,t[0],oE()),t)],Wu=(e,t,n)=>[e.run(Ze(e,t[0],Ew(n.min,n.max),n.cacheKey),t)],Nw=e=>Le({min:e.attributes.getFloat("min",Pi),max:e.attributes.getFloat("max",Ci)}),zw=(e,t)=>{let n=ch(e,t);return Wu(e,[t[0]],n)},ch=(e,t)=>{if(t.length>=3&&(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)))throw new Error("dynamic clip attributes are not allowed");let n=t.length>=3?t[1].numberData[0]:Pi,i=t.length>=3?t[2].numberData[0]:Ci;return Le({min:n,max:i})},Mw=(e,t)=>[e.run(Ze(e,t[0],sE()),t)],Bw=(e,t)=>[e.run(Ze(e,t[0],aE()),t)],Fw=(e,t,n)=>[e.run(Ze(e,t[0],uE(n.alpha),n.cacheKey),t)],jw=e=>Le({alpha:e.attributes.getFloat("alpha",1)}),Lw=(e,t)=>[e.run(Ze(e,t[0],lE()),t)],Uw=(e,t)=>[e.run(Ze(e,t[0],dE()),t)],Ku=(e,t)=>[e.run(Ze(e,t[0],cE()),t)],Vw=(e,t,n)=>[e.run(Ze(e,t[0],pE(n.alpha),n.cacheKey),t)],Gw=e=>Le({alpha:e.attributes.getFloat("alpha",.01)}),qw=(e,t)=>[e.run(Ze(e,t[0],hE()),t)],Hw=(e,t)=>[e.run(Ze(e,t[0],fE()),t)],Ww=(e,t)=>[e.run(Ze(e,t[0],mE()),t)],Kw=(e,t)=>[e.run(Ze(e,t[0],Aw()),t)],Xw=(e,t)=>[e.run(Ze(e,t[0],kw()),t)],Zw=(e,t)=>[e.run(Ze(e,t[0],gE()),t)],Yw=(e,t)=>[e.run(Ze(e,t[0],bE()),t)],Jw=(e,t)=>[e.run(Ze(e,t[0],yE()),t)],Qw=(e,t)=>[e.run(Ze(e,t[0],wE()),t)]});function Mi(e){let t;switch(e.activation){case"Relu":t=Aw();break;case"Sigmoid":t=kw();break;case"Clip":t=Ew(e.clipMin,e.clipMax);break;default:return{activationFunction:"",applyActivation:""}}let n=t.name,i=t.body,u=`value = ${n}_(value);`;return{activationFunction:i,applyActivation:u}}var Xo,Fn=N(()=>{"use strict";Ne(),e_(),Xo=e=>{let t=e.getString("activation","");if(t==="Clip"){let[n,i]=e.getFloats("activation_params",[Pi,Ci]);return{activation:t,clipMax:i,clipMin:n,activationCacheKey:`${t}:${n},${i}`}}return{activation:t,activationCacheKey:t}}}),ph,hh,t_,_E=N(()=>{"use strict";Gt(),Ke(),Pe(),Dl(),Fn(),ph=(e,t)=>({name:"GroupedConv",inputNames:e?["X","W","Bias"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),hh=(e,t,n,i)=>{let u=t.length>2?"value += getBias(output_channel);":"",l=t[0].dims.slice(),d=t[1].dims.slice(),c=d[0]/i.group;Ye.verbose("GroupedConv",`autpPad:${i.autoPad}, dilations:${i.dilations}, group:${i.group}, kernelShape:${i.kernelShape}, pads:${i.pads}, strides:${i.strides}`);let s=Ei(l,d,i.dilations,i.pads,i.strides),r=Ae(e.session.backend.glContext.version),{activationFunction:o,applyActivation:a}=Mi(i),p=` + const ivec2 strides = ivec2(${i.strides[0]}, ${i.strides[1]}); + const ivec2 pads = ivec2(${i.pads[0]}, ${i.pads[1]}); + ${o} void main() { ivec4 coords = getOutputCoords(); int batch = coords.x; @@ -551,14 +551,14 @@ fn main(@builtin(global_invocation_id) id: vec3) { for (int wInChannel = 0; wInChannel < ${d[1]}; wInChannel++) { int input_channel = group_id * ${d[1]} + wInChannel; for (int wHeight = 0; wHeight < ${d[2]}; wHeight++) { - int xHeight = xRCCorner.x + wHeight * ${o.dilations[0]}; + int xHeight = xRCCorner.x + wHeight * ${i.dilations[0]}; if (xHeight < 0 || xHeight >= ${l[2]}) { continue; } for (int wWidth = 0; wWidth < ${d[3]}; wWidth++) { - int xWidth = xRCCorner.y + wWidth * ${o.dilations[1]}; + int xWidth = xRCCorner.y + wWidth * ${i.dilations[1]}; if (xWidth < 0 || xWidth >= ${l[3]}) { continue; } @@ -573,17 +573,17 @@ fn main(@builtin(global_invocation_id) id: vec3) { ${a} ${r.output} = vec4(value, .0, .0, .0); } -`;return{...n,output:{dims:s,type:t[0].type,textureType:0},shaderSource:p,hasMain:!0}},Kw=(e,t,n)=>{let o=oh(t.length>2,n.cacheKey);return{...o,get:()=>sh(e,t,o,n)}}}),ah,uh,Xw,lE=R(()=>{"use strict";qe(),Ie(),An(),ah=e=>({name:"Im2Col (packed)",inputNames:["A"],inputTypes:[2],cacheHint:e}),uh=(e,t,n,o,u,l)=>{let d=n.dims,c=o.dims,s=2,r=3,i=u.length,a=[c[1]*c[2]*c[3],u[2]*u[3]],p=c[2]*c[3],h=Ai(),m=Te(e.session.backend.glContext.version),b="";for(let w=0;w<=1;w++)for(let y=0;y<=1;y++)b+=` +`;return{...n,output:{dims:s,type:t[0].type,textureType:0},shaderSource:p,hasMain:!0}},t_=(e,t,n)=>{let i=ph(t.length>2,n.cacheKey);return{...i,get:()=>hh(e,t,i,n)}}}),fh,mh,r_,vE=N(()=>{"use strict";Ke(),Pe(),Mn(),fh=e=>({name:"Im2Col (packed)",inputNames:["A"],inputTypes:[2],cacheHint:e}),mh=(e,t,n,i,u,l)=>{let d=n.dims,c=i.dims,s=2,r=3,o=u.length,a=[c[1]*c[2]*c[3],u[2]*u[3]],p=c[2]*c[3],h=zi(),m=Ae(e.session.backend.glContext.version),g="";for(let w=0;w<=1;w++)for(let y=0;y<=1;y++)g+=` blockIndex = rc.x + ${y}; pos = rc.y + ${w}; if(blockIndex < ${a[1]} && pos < ${a[0]}) { - offsetY = int(blockIndex / (${u[i-1]})) * ${l.strides[0]} - + offsetY = int(blockIndex / (${u[o-1]})) * ${l.strides[0]} - ${l.pads[0]}; d0 = offsetY + ${l.dilations[0]} * (imod(pos, ${p}) / ${c[2]}); if(d0 < ${d[s]} && d0 >= 0) { - offsetX = imod(blockIndex, ${u[i-1]}) * ${l.strides[1]} - + offsetX = imod(blockIndex, ${u[o-1]}) * ${l.strides[1]} - ${l.pads[1]}; d1 = offsetX + ${l.dilations[1]} * imod(imod(pos, ${p}), ${c[2]}); @@ -598,7 +598,7 @@ fn main(@builtin(global_invocation_id) id: vec3) { } } - `;let x=` + `;let v=` ${h} void main() { @@ -606,29 +606,29 @@ fn main(@builtin(global_invocation_id) id: vec3) { vec4 result = vec4(0.0); int blockIndex, pos, offsetY, d0, offsetX, d1, ch; vec2 innerDims; - ${b} + ${g} ${m.output} = result; } - `;return{...t,output:{dims:a,type:n.type,textureType:2},shaderSource:x,hasMain:!0}},Xw=(e,t,n,o,u)=>{let l=ah(u.cacheKey);return{...l,get:()=>uh(e,l,t,n,o,u)}}});function dE(e,t,n){let o=t[0].dims,u=t[1].dims,l=qt.calcShape(o,u,!0);if(!l)throw new Error("Can't use matmul on the given tensors");let d=_r(l.length),c=Sn(),{activationFunction:s,applyActivation:r}=ki(n),i=t.length>2,a=i?"value += getBiasForMatmul();":"",p=i?`${Jw(d,c,t[2].dims,l,!1)}`:"",h=l.length,m=o.length,b=u.length,x=o[o.length-1],w=` + `;return{...t,output:{dims:a,type:n.type,textureType:2},shaderSource:v,hasMain:!0}},r_=(e,t,n,i,u)=>{let l=fh(u.cacheKey);return{...l,get:()=>mh(e,l,t,n,i,u)}}});function xE(e,t,n){let i=t[0].dims,u=t[1].dims,l=Qt.calcShape(i,u,!0);if(!l)throw new Error("Can't use matmul on the given tensors");let d=Ir(l.length),c=Dn(),{activationFunction:s,applyActivation:r}=Mi(n),o=t.length>2,a=o?"value += getBiasForMatmul();":"",p=o?`${i_(d,c,t[2].dims,l,!1)}`:"",h=l.length,m=i.length,g=u.length,v=i[i.length-1],w=` ${s} ${p} float process(int indices[${h}]) { int a[${m}]; - int b[${b}]; + int b[${g}]; bcastMatmulIndices_A(indices, a); bcastMatmulIndices_B(indices, b); float value; - for (int k=0; k<${x}; ++k) { + for (int k=0; k<${v}; ++k) { a[${m-1}] = k; - b[${b-2}] = k; + b[${g-2}] = k; value += _A(a) * _B(b); } ${a} ${r} return value; - }`;return{...e,output:{dims:l,type:t[0].type,textureType:0},shaderSource:w}}function Zw(e,t){let n=e_(e.length>2,t.activationCacheKey);return{...n,get:()=>dE(n,e,t)}}function Jw(e,t,n,o,u){let l="",d=n.length,c=o.length,s=c-d;c<2&&d>0?l="coords":l=n.map((p,h)=>`coords.${t[h+s]}`).join(", ");let r=qt.getBroadcastDims(n,o).map(p=>`coords.${t[p+s]} = 0;`).join(` -`),i=he.size(n)===1,a="vec4(outputValue.xx, outputValue.yy)";return i&&(a="vec4(outputValue.x)"),u?` + }`;return{...e,output:{dims:l,type:t[0].type,textureType:0},shaderSource:w}}function n_(e,t){let n=a_(e.length>2,t.activationCacheKey);return{...n,get:()=>xE(n,e,t)}}function i_(e,t,n,i,u){let l="",d=n.length,c=i.length,s=c-d;c<2&&d>0?l="coords":l=n.map((p,h)=>`coords.${t[h+s]}`).join(", ");let r=Qt.getBroadcastDims(n,i).map(p=>`coords.${t[p+s]} = 0;`).join(` +`),o=ye.size(n)===1,a="vec4(outputValue.xx, outputValue.yy)";return o&&(a="vec4(outputValue.x)"),u?` vec4 getBiasForMatmul() { ${e} coords = getOutputCoords(); ${r} @@ -639,15 +639,15 @@ float getBiasForMatmul() { ${e} coords = getOutputCoords(); ${r} return getBias(coords.x); -}`}var Yw,Qw,e_,lh,Ol=R(()=>{"use strict";Pe(),Ie(),Yt(),Pn(),t_(),Yw=(e,t,n)=>(lh(t),e.session.pack?[e.run(El(e,t,n),t)]:[e.run(Zw(t,n),t)]),Qw=e=>Uo(e.attributes),e_=(e,t)=>({name:"MatMul",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),lh=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64")throw new Error("inputs should be float type");if(e[0].type!==e[1].type)throw new Error("inputs types should match")}});function cE(e,t,n,o){let u=[],l=[],d=n[0].dims,c=n[1].dims,s=d.length,r=c.length,i=o.length,a=i-s,p=i-r;u=d.map((y,S)=>`coords.${t[S+a]}`),u[s-1]="i*2",u.join(", "),l=c.map((y,S)=>`coords.${t[S+p]}`),l[r-2]="i*2",l.join(", ");let h=qt.getBroadcastDims(d,o),m=qt.getBroadcastDims(c,o),b=h.map(y=>`coords.${t[y+a]} = 0;`).join(` -`),x=m.map(y=>`coords.${t[y+p]} = 0;`).join(` -`),w=`int lastDim = coords.${t[i-1]}; - coords.${t[i-1]} = coords.${t[i-2]}; - coords.${t[i-2]} = lastDim;`;return` +}`}var o_,s_,a_,gh,Pl=N(()=>{"use strict";Ne(),Pe(),sr(),Fn(),u_(),o_=(e,t,n)=>(gh(t),e.session.pack?[e.run(Cl(e,t,n),t)]:[e.run(n_(t,n),t)]),s_=e=>Xo(e.attributes),a_=(e,t)=>({name:"MatMul",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),gh=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64")throw new Error("inputs should be float type");if(e[0].type!==e[1].type)throw new Error("inputs types should match")}});function $E(e,t,n,i){let u=[],l=[],d=n[0].dims,c=n[1].dims,s=d.length,r=c.length,o=i.length,a=o-s,p=o-r;u=d.map((y,T)=>`coords.${t[T+a]}`),u[s-1]="i*2",u.join(", "),l=c.map((y,T)=>`coords.${t[T+p]}`),l[r-2]="i*2",l.join(", ");let h=Qt.getBroadcastDims(d,i),m=Qt.getBroadcastDims(c,i),g=h.map(y=>`coords.${t[y+a]} = 0;`).join(` +`),v=m.map(y=>`coords.${t[y+p]} = 0;`).join(` +`),w=`int lastDim = coords.${t[o-1]}; + coords.${t[o-1]} = coords.${t[o-2]}; + coords.${t[o-2]} = lastDim;`;return` vec4 getAAtOutCoordsMatmul(int i) { ${e} coords = getOutputCoords(); ${w} - ${b} + ${g} vec4 outputValue = getA(${u}); return outputValue; } @@ -655,31 +655,31 @@ vec4 getAAtOutCoordsMatmul(int i) { vec4 getBAtOutCoordsMatmul(int i) { ${e} coords = getOutputCoords(); ${w} - ${x} + ${v} vec4 outputValue = getB(${l}); return outputValue; -}`}function pE(e,t){let n="";for(let o=0;o{"use strict";Pe(),qe(),Ie(),Yt(),Pn(),Ol(),dh=(e,t)=>({name:"MatMul (packed)",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[2,2,2]:[2,2],cacheHint:t}),ch=(e,t,n,o)=>{let u=n.length>2,l=u?"value += getBiasForMatmul();":"",d=n[0].dims,c=n[1].dims,s=qt.calcShape(d,c,!0),r=!he.areEqual(n[0].dims,n[1].dims);if(!s)throw new Error("Can't use matmul on the given tensors");let i=d[d.length-1],a=Math.ceil(i/2),p=d.length,h=c.length,m=Te(e.session.backend.glContext.version),b=_r(s.length),x=s.length,w=Sn(),{activationFunction:y,applyActivation:S}=ki(o),T=u?`${Jw(b,w,n[2].dims,s,!0)}`:"",I=r?`${cE(b,w,n,s)}`:"",A=r?"getAAtOutCoordsMatmul(i)":`getA(${pE(w,p)})`,P=r?"getBAtOutCoordsMatmul(i)":`getB(${hE(w,h)})`,O=r?"":`${b} rc = - getOutputCoords(); int lastDim = rc.${w[x-1]}; rc.${w[x-1]} = - rc.${w[x-2]}; rc.${w[x-2]} = lastDim; - `,B=` - ${I} - ${T} +}`}function TE(e,t){let n="";for(let i=0;i{"use strict";Ne(),Ke(),Pe(),sr(),Fn(),Pl(),bh=(e,t)=>({name:"MatMul (packed)",inputNames:e?["A","B","Bias"]:["A","B"],inputTypes:e?[2,2,2]:[2,2],cacheHint:t}),yh=(e,t,n,i)=>{let u=n.length>2,l=u?"value += getBiasForMatmul();":"",d=n[0].dims,c=n[1].dims,s=Qt.calcShape(d,c,!0),r=!ye.areEqual(n[0].dims,n[1].dims);if(!s)throw new Error("Can't use matmul on the given tensors");let o=d[d.length-1],a=Math.ceil(o/2),p=d.length,h=c.length,m=Ae(e.session.backend.glContext.version),g=Ir(s.length),v=s.length,w=Dn(),{activationFunction:y,applyActivation:T}=Mi(i),S=u?`${i_(g,w,n[2].dims,s,!0)}`:"",O=r?`${$E(g,w,n,s)}`:"",A=r?"getAAtOutCoordsMatmul(i)":`getA(${TE(w,p)})`,k=r?"getBAtOutCoordsMatmul(i)":`getB(${SE(w,h)})`,I=r?"":`${g} rc = + getOutputCoords(); int lastDim = rc.${w[v-1]}; rc.${w[v-1]} = + rc.${w[v-2]}; rc.${w[v-2]} = lastDim; + `,R=` + ${O} + ${S} ${y} void main() { - ${O} + ${I} vec4 value = vec4(0); for (int i = 0; i < ${a}; i++) { vec4 a = ${A}; - vec4 b = ${P}; + vec4 b = ${k}; value += (a.rrbb * b.rgrg); value += (a.ggaa * b.baba); } ${l} - ${S} + ${T} ${m.output} = value; - }`;return{...t,output:{dims:s,type:n[0].type,textureType:2},shaderSource:B,hasMain:!0}},El=(e,t,n)=>{let o=dh(t.length>2,n.activationCacheKey);return{...o,get:()=>ch(e,o,t,n)}}}),r_,fE=R(()=>{"use strict";Al(),lE(),t_(),r_=(e,t,n)=>{let o=t[0].dims,u=t[1].dims,l=vi(o,u,n.dilations,n.pads,n.strides),d=e.run(Xw(e,t[0],t[1],l,n),[t[0]]),c=e.reshapePacked(t[1],[u[0],u[1]*u[2]*u[3]]),s=t.length===3?[c,d,t[2]]:[c,d],r=e.run(El(e,s,n),s);return e.reshapePacked(r,l)}}),ph,hh,n_,qu,i_=R(()=>{"use strict";Ie(),ph=e=>({name:"Im2Col",inputNames:["X"],inputTypes:[0],cacheHint:e}),hh=(e,t,n,o,u,l)=>{let d=n.dims,c=o.dims,s=u.length,r=qu(d,c,u,4),i=` + }`;return{...t,output:{dims:s,type:n[0].type,textureType:2},shaderSource:R,hasMain:!0}},Cl=(e,t,n)=>{let i=bh(t.length>2,n.activationCacheKey);return{...i,get:()=>yh(e,i,t,n)}}}),l_,IE=N(()=>{"use strict";Dl(),vE(),u_(),l_=(e,t,n)=>{let i=t[0].dims,u=t[1].dims,l=Ei(i,u,n.dilations,n.pads,n.strides),d=e.run(r_(e,t[0],t[1],l,n),[t[0]]),c=e.reshapePacked(t[1],[u[0],u[1]*u[2]*u[3]]),s=t.length===3?[c,d,t[2]]:[c,d],r=e.run(Cl(e,s,n),s);return e.reshapePacked(r,l)}}),wh,_h,d_,Xu,c_=N(()=>{"use strict";Pe(),wh=e=>({name:"Im2Col",inputNames:["X"],inputTypes:[0],cacheHint:e}),_h=(e,t,n,i,u,l)=>{let d=n.dims,c=i.dims,s=u.length,r=Xu(d,c,u,4),o=` const int XC = ${d[1]}; const int XH = ${d[2]}; const int XW = ${d[3]}; @@ -723,7 +723,7 @@ vec4 getBAtOutCoordsMatmul(int i) { } return value; } - `;return{...t,output:{dims:r,type:n.type,textureType:4},shaderSource:i}},n_=(e,t,n,o,u)=>{let l=ph(u.cacheKey);return{...l,get:()=>hh(e,l,t,n,o,u)}},qu=(e,t,n,o=4)=>[n[0],n[2],n[3],Math.ceil(e[1]*t[2]*t[3]/o)]}),fh,mh,o_,mE=R(()=>{"use strict";Pe(),qe(),Ie(),Pn(),i_(),fh=(e,t)=>({name:"ConvDotProduct",inputNames:e?["Im2Col","K","B"]:["Im2Col","K"],inputTypes:e?[0,4,0]:[0,4],cacheKey:t.activationCacheKey}),mh=(e,t,n,o,u)=>{let l=n[0].dims,d=n[1].dims,c=[d[0],Math.ceil(l[1]*d[2]*d[3]/4)],s=qu(l,d,o),[r,i]=e.calculateTextureWidthAndHeight(c,4),a=he.computeStrides(s),[p,h]=e.calculateTextureWidthAndHeight(s,4),m=o.length,b=n.length<3?"0.0":"_B(b)",x=Math.ceil(l[1]*d[2]*d[3]/4),{activationFunction:w,applyActivation:y}=ki(u),S=Te(e.session.backend.glContext.version),T=` + `;return{...t,output:{dims:r,type:n.type,textureType:4},shaderSource:o}},d_=(e,t,n,i,u)=>{let l=wh(u.cacheKey);return{...l,get:()=>_h(e,l,t,n,i,u)}},Xu=(e,t,n,i=4)=>[n[0],n[2],n[3],Math.ceil(e[1]*t[2]*t[3]/i)]}),vh,xh,p_,OE=N(()=>{"use strict";Ne(),Ke(),Pe(),Fn(),c_(),vh=(e,t)=>({name:"ConvDotProduct",inputNames:e?["Im2Col","K","B"]:["Im2Col","K"],inputTypes:e?[0,4,0]:[0,4],cacheKey:t.activationCacheKey}),xh=(e,t,n,i,u)=>{let l=n[0].dims,d=n[1].dims,c=[d[0],Math.ceil(l[1]*d[2]*d[3]/4)],s=Xu(l,d,i),[r,o]=e.calculateTextureWidthAndHeight(c,4),a=ye.computeStrides(s),[p,h]=e.calculateTextureWidthAndHeight(s,4),m=i.length,g=n.length<3?"0.0":"_B(b)",v=Math.ceil(l[1]*d[2]*d[3]/4),{activationFunction:w,applyActivation:y}=Mi(u),T=Ae(e.session.backend.glContext.version),S=` ${w} float process(int indices[${m}]) { int b[1]; @@ -734,19 +734,19 @@ float process(int indices[${m}]) { im2col[2] = indices[3]; int im2colOffset = im2col[0] * ${a[0]} + im2col[1] * ${a[1]} + im2col[2] * ${a[2]}; int kernelOffset = indices[1] * ${c[1]}; - float value = ${b}; - for (int i = 0; i < ${x}; ++i) { + float value = ${g}; + for (int i = 0; i < ${v}; ++i) { vec2 im2colCoords = offsetToCoords(im2colOffset, ${p}, ${h}); - vec2 kernelCoords = offsetToCoords(kernelOffset, ${r}, ${i}); - value += dot(${S.texture2D}(Im2Col, im2colCoords), ${S.texture2D}(K, kernelCoords)); + vec2 kernelCoords = offsetToCoords(kernelOffset, ${r}, ${o}); + value += dot(${T.texture2D}(Im2Col, im2colCoords), ${T.texture2D}(K, kernelCoords)); ++im2colOffset; ++kernelOffset; } ${y} return value; -}`;return{...t,output:{dims:o,type:n[0].type,textureType:0},shaderSource:T}},o_=(e,t,n,o)=>{let u=fh(t.length>2,o);return{...u,get:()=>mh(e,u,t,n,o)}}}),vi,Hu,gh,bh,yh,wh,Wu,_h,Al=R(()=>{"use strict";nt(),Pe(),uE(),fE(),mE(),Pn(),i_(),Ol(),vi=(e,t,n,o,u)=>{let l=e[0],d=e.slice(2),c=d.length,s=t[0],r=t.slice(2).map((a,p)=>a+(a-1)*(n[p]-1)),i=d.map((a,p)=>a+o[p]+o[p+c]).map((a,p)=>Math.floor((a-r[p]+u[p])/u[p]));return[l,s].concat(...i)},Hu=(e,t,n)=>(_h(t,n),gh(e,t,n)),gh=(e,t,n)=>{let o=wh(n,t),u=e.session.pack,l=o.kernelShape[0]===1&&o.kernelShape[1]===1;return o.group>1?[e.run(Kw(e,t,o),t)]:l&&u?[bh(e,t,o)]:u&&t[0].dims.length===4&&t[0].dims[0]===1&&!l?[r_(e,t,o)]:[yh(e,t,o)]},bh=(e,t,n)=>{let o=t[0].dims,u=t[1].dims,l=vi(o,u,n.dilations,n.pads,n.strides),d=e.reshapeUnpacked(t[0],[o[1],o[2]*o[3]]),c=e.reshapeUnpacked(t[1],[u[0],u[1]]),s=t.length>2?[c,d,t[2]]:[c,d],r=e.run(Zw(s,n),s);return e.reshapeUnpacked(r,l)},yh=(e,t,n)=>{let o=t[0].dims,u=t[1].dims,l=vi(o,u,n.dilations,n.pads,n.strides),d=e.run(n_(e,t[0],t[1],l,n),[t[0]]),c=t.length===3?[d,t[1],t[2]]:[d,t[1]];return e.run(o_(e,t,l,n),c)},wh=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let l=2;l{let t=e.attributes,n=Uo(t),o=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("pads",[0,0,0,0]),s=t.getInts("strides",[1,1]);return Be({autoPad:o,dilations:u,group:l,kernelShape:d,pads:c,strides:s,...n})},_h=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],o=e[1].dims[1]*t.group;if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("Conv input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("Conv input(bias) should be float tensor")}}),vh,xh,$h,s_,Th,Sh,Ih,Oh,Eh,Ah,a_,kh,gE=R(()=>{"use strict";nt(),qe(),Ie(),Pn(),vh=(e,t,n,o,u,l)=>(e-1)*t+n+(o-1)*u+1-l,xh=(e,t,n,o,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[o]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[o]=e-l,n[u]=l)},$h=(e,t,n,o,u,l,d,c)=>{let s=e.length-2,r=c.length===0;for(let i=0;i(kh(t,n),Th(e,t,n)),Th=(e,t,n)=>{let o=Ah(n,t);return[Eh(e,t,o)]},Sh=(e,t)=>({name:"ConvTranspose",inputNames:e?["X","W","B"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),Ih=(e,t,n,o)=>{let u=t.length>2?"getB(output_channel)":"0.0",l=t[0].dims,d=t[1].dims,c=d[1],s=d[0]/o.group,r=[t[0].dims[0],t[1].dims[1]*o.group,...o.outputShape],i=Te(e.session.backend.glContext.version),{activationFunction:a,applyActivation:p}=ki(o),h=` - const ivec2 strides = ivec2(${o.strides[0]}, ${o.strides[1]}); - const ivec2 pads = ivec2(${o.pads[0]}, ${o.pads[1]}); +}`;return{...t,output:{dims:i,type:n[0].type,textureType:0},shaderSource:S}},p_=(e,t,n,i)=>{let u=vh(t.length>2,i);return{...u,get:()=>xh(e,u,t,n,i)}}}),Ei,Zu,$h,Th,Sh,Ih,Yu,Oh,Dl=N(()=>{"use strict";ut(),Ne(),_E(),IE(),OE(),Fn(),c_(),Pl(),Ei=(e,t,n,i,u)=>{let l=e[0],d=e.slice(2),c=d.length,s=t[0],r=t.slice(2).map((a,p)=>a+(a-1)*(n[p]-1)),o=d.map((a,p)=>a+i[p]+i[p+c]).map((a,p)=>Math.floor((a-r[p]+u[p])/u[p]));return[l,s].concat(...o)},Zu=(e,t,n)=>(Oh(t,n),$h(e,t,n)),$h=(e,t,n)=>{let i=Ih(n,t),u=e.session.pack,l=i.kernelShape[0]===1&&i.kernelShape[1]===1;return i.group>1?[e.run(t_(e,t,i),t)]:l&&u?[Th(e,t,i)]:u&&t[0].dims.length===4&&t[0].dims[0]===1&&!l?[l_(e,t,i)]:[Sh(e,t,i)]},Th=(e,t,n)=>{let i=t[0].dims,u=t[1].dims,l=Ei(i,u,n.dilations,n.pads,n.strides),d=e.reshapeUnpacked(t[0],[i[1],i[2]*i[3]]),c=e.reshapeUnpacked(t[1],[u[0],u[1]]),s=t.length>2?[c,d,t[2]]:[c,d],r=e.run(n_(s,n),s);return e.reshapeUnpacked(r,l)},Sh=(e,t,n)=>{let i=t[0].dims,u=t[1].dims,l=Ei(i,u,n.dilations,n.pads,n.strides),d=e.run(d_(e,t[0],t[1],l,n),[t[0]]),c=t.length===3?[d,t[1],t[2]]:[d,t[1]];return e.run(p_(e,t,l,n),c)},Ih=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let l=2;l{let t=e.attributes,n=Xo(t),i=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("pads",[0,0,0,0]),s=t.getInts("strides",[1,1]);return Le({autoPad:i,dilations:u,group:l,kernelShape:d,pads:c,strides:s,...n})},Oh=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],i=e[1].dims[1]*t.group;if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("Conv input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("Conv input(bias) should be float tensor")}}),Eh,Ah,kh,h_,Ph,Ch,Dh,Rh,Nh,zh,f_,Mh,EE=N(()=>{"use strict";ut(),Ke(),Pe(),Fn(),Eh=(e,t,n,i,u,l)=>(e-1)*t+n+(i-1)*u+1-l,Ah=(e,t,n,i,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[i]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[i]=e-l,n[u]=l)},kh=(e,t,n,i,u,l,d,c)=>{let s=e.length-2,r=c.length===0;for(let o=0;o(Mh(t,n),Ph(e,t,n)),Ph=(e,t,n)=>{let i=zh(n,t);return[Nh(e,t,i)]},Ch=(e,t)=>({name:"ConvTranspose",inputNames:e?["X","W","B"]:["X","W"],inputTypes:e?[0,0,0]:[0,0],cacheHint:t}),Dh=(e,t,n,i)=>{let u=t.length>2?"getB(output_channel)":"0.0",l=t[0].dims,d=t[1].dims,c=d[1],s=d[0]/i.group,r=[t[0].dims[0],t[1].dims[1]*i.group,...i.outputShape],o=Ae(e.session.backend.glContext.version),{activationFunction:a,applyActivation:p}=Mi(i),h=` + const ivec2 strides = ivec2(${i.strides[0]}, ${i.strides[1]}); + const ivec2 pads = ivec2(${i.pads[0]}, ${i.pads[1]}); ${a} void main() { ivec4 coords = getOutputCoords(); @@ -763,7 +763,7 @@ float process(int indices[${m}]) { int input_channel = group_id * ${s} + inChannelOffset; for (int wWOff = 0; wWOff < ${d[2]}; wWOff++) { for (int wHOff = 0; wHOff < ${d[3]}; wHOff++) { - ivec2 wOff = ivec2(wWOff * ${o.dilations[0]}, wHOff * ${o.dilations[1]}); + ivec2 wOff = ivec2(wWOff * ${i.dilations[0]}, wHOff * ${i.dilations[1]}); ivec2 wLoc = loc - wOff; ivec2 wLocIn = wLoc / strides; if ( @@ -779,29 +779,29 @@ float process(int indices[${m}]) { } } ${p} - ${i.output} = vec4(value, .0, .0, .0); + ${o.output} = vec4(value, .0, .0, .0); } -`;return{...n,output:{dims:r,type:t[0].type,textureType:0},shaderSource:h,hasMain:!0}},Oh=(e,t,n)=>{let o=Sh(t.length>2,n.cacheKey);return{...o,get:()=>Ih(e,t,o,n)}},Eh=(e,t,n)=>e.run(Oh(e,t,n),t),Ah=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let c=2;c{let t=e.attributes,n=Uo(t),o=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("output_padding",[0,0]),s=t.getInts("output_shape",[]),r=t.getInts("pads",[0,0,0,0]),i=t.getInts("strides",[1,1]);return Be({autoPad:o,dilations:u,group:l,kernelShape:d,outputPadding:c,outputShape:s,pads:r,strides:i,...n})},kh=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],o=e[1].dims[0];if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("ConvTranspose input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("ConvTranspose input(bias) should be float tensor")}}),Qs,Ii,u_,Ph,ea,Ch,Dh,Rh,kl=R(()=>{"use strict";nt(),Pe(),Ie(),Qs={name:"Transpose",inputNames:["A"],inputTypes:[0]},Ii=(e,t,n)=>(Rh(t),[e.run({...Qs,cacheHint:n.cacheKey,get:()=>Ph(e,t[0],n.perm)},t)]),u_=e=>Be({perm:e.attributes.getInts("perm",[])}),Ph=(e,t,n)=>{let o=t.dims;n=ea(o,n);let u=Ch(o,n),l=o.length,d=` - ${Dh("perm",n,l)} +`;return{...n,output:{dims:r,type:t[0].type,textureType:0},shaderSource:h,hasMain:!0}},Rh=(e,t,n)=>{let i=Ch(t.length>2,n.cacheKey);return{...i,get:()=>Dh(e,t,i,n)}},Nh=(e,t,n)=>e.run(Rh(e,t,n),t),zh=(e,t)=>{let n=e.kernelShape.slice();if(e.kernelShape.length===0)for(let c=2;c{let t=e.attributes,n=Xo(t),i=t.getString("auto_pad","NOTSET"),u=t.getInts("dilations",[1,1]),l=t.getInt("group",1),d=t.getInts("kernel_shape",[]),c=t.getInts("output_padding",[0,0]),s=t.getInts("output_shape",[]),r=t.getInts("pads",[0,0,0,0]),o=t.getInts("strides",[1,1]);return Le({autoPad:i,dilations:u,group:l,kernelShape:d,outputPadding:c,outputShape:s,pads:r,strides:o,...n})},Mh=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4||e[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let n=e[0].dims[1],i=e[1].dims[0];if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape");if(e[0].type!=="float32"||e[1].type!=="float32")throw new Error("ConvTranspose input(X,W) should be float tensor");if(e.length===3&&e[2].type!=="float32")throw new Error("ConvTranspose input(bias) should be float tensor")}}),na,Di,m_,Bh,ia,Fh,jh,Lh,Rl=N(()=>{"use strict";ut(),Ne(),Pe(),na={name:"Transpose",inputNames:["A"],inputTypes:[0]},Di=(e,t,n)=>(Lh(t),[e.run({...na,cacheHint:n.cacheKey,get:()=>Bh(e,t[0],n.perm)},t)]),m_=e=>Le({perm:e.attributes.getInts("perm",[])}),Bh=(e,t,n)=>{let i=t.dims;n=ia(i,n);let u=Fh(i,n),l=i.length,d=` + ${jh("perm",n,l)} float process(int indices[${l}]) { int a[${l}]; perm(a, indices); return _A(a); - }`;return{...Qs,output:{dims:u,type:t.type,textureType:0},shaderSource:d}},ea=(e,t)=>(t&&t.length!==e.length&&(t=[...e.keys()].reverse()),t),Ch=(e,t)=>(t=ea(e,t),he.sortBasedOnPerm(e,t)),Dh=(e,t,n)=>{let o=[];o.push(`void ${e}(out int a[${n}], int src[${n}]) {`);for(let u=0;u{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("input should be float tensor")}}),l_,d_,Nh,bE=R(()=>{"use strict";kl(),l_=(e,t,n)=>{Nh(t);let o=n.blocksize,u=o*o,l=n.mode==="DCR"?[0,3,4,1,5,2]:[0,1,4,2,5,3],d=n.mode==="DCR"?[t[0].dims[0],o,o,t[0].dims[1]/u,t[0].dims[2],t[0].dims[3]]:[t[0].dims[0],t[0].dims[1]/u,o,o,t[0].dims[2],t[0].dims[3]],c=e.reshapeUnpacked(t[0],d),s={perm:l,cacheKey:`${l}`},[r]=Ii(e,[c],s),i=[t[0].dims[0],t[0].dims[1]/u,t[0].dims[2]*o,t[0].dims[3]*o];return[e.reshapeUnpacked(r,i)]},d_=e=>{let t=e.attributes.getInt("blocksize");if(t<1)throw new Error(`blocksize must be >= 1, but got : ${t} for DepthToSpace`);let n=e.attributes.getString("mode","DCR");if(n!=="DCR"&&n!=="CRD")throw new Error(`unrecognized mode: ${n} for DepthToSpace`);return{mode:n,blocksize:t}},Nh=e=>{if(e.length!==1)throw new Error(`DepthToSpace expect 1 inputs, but got ${e.length}`);if(e[0].type==="string"||e[0].dims.length!==4)throw new TypeError("DepthToSpace input should be a 4-D numeric tensor")}}),c_,p_,zh,yE=R(()=>{"use strict";Pe(),c_=(e,t,n)=>{zh(t,n);let o=he.flattenShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],o)]},p_=e=>e.attributes.getInt("axis",1),zh=(e,t)=>{if(!e||e.length!==1)throw new Error("Flatten requires 1 input.");let n=e[0].dims.length;if(n===0)throw new Error("scalar tensor is not supported.");if(t<-n||t>n)throw new Error("Invalid axis");if(e[0].type==="string")throw new Error("string tensor is not supported.")}}),Pi,Vo=R(()=>{"use strict";Pi=["float32","float64","int32","int16","int8","uint16","uint32","uint8"]}),h_,f_,Bh,Mh,Fh,jh,wE=R(()=>{"use strict";nt(),Vo(),Pe(),Ie(),h_=(e,t,n)=>(jh(t,n.axis),[e.run(Fh(e,t,n),t)]),f_=e=>Be({axis:e.attributes.getInt("axis",0)}),Bh={name:"Gather",inputNames:["A","B"],inputTypes:[0,0]},Mh=(e,t,n,o)=>{let u=n[0].dims.slice(),l=n[1].dims.slice(),d=new Array(u.length+l.length-1);o=he.normalizeAxis(o,u.length);let c=[];for(let p=0;p(t&&t.length!==e.length&&(t=[...e.keys()].reverse()),t),Fh=(e,t)=>(t=ia(e,t),ye.sortBasedOnPerm(e,t)),jh=(e,t,n)=>{let i=[];i.push(`void ${e}(out int a[${n}], int src[${n}]) {`);for(let u=0;u{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("input should be float tensor")}}),g_,b_,Uh,AE=N(()=>{"use strict";Rl(),g_=(e,t,n)=>{Uh(t);let i=n.blocksize,u=i*i,l=n.mode==="DCR"?[0,3,4,1,5,2]:[0,1,4,2,5,3],d=n.mode==="DCR"?[t[0].dims[0],i,i,t[0].dims[1]/u,t[0].dims[2],t[0].dims[3]]:[t[0].dims[0],t[0].dims[1]/u,i,i,t[0].dims[2],t[0].dims[3]],c=e.reshapeUnpacked(t[0],d),s={perm:l,cacheKey:`${l}`},[r]=Di(e,[c],s),o=[t[0].dims[0],t[0].dims[1]/u,t[0].dims[2]*i,t[0].dims[3]*i];return[e.reshapeUnpacked(r,o)]},b_=e=>{let t=e.attributes.getInt("blocksize");if(t<1)throw new Error(`blocksize must be >= 1, but got : ${t} for DepthToSpace`);let n=e.attributes.getString("mode","DCR");if(n!=="DCR"&&n!=="CRD")throw new Error(`unrecognized mode: ${n} for DepthToSpace`);return{mode:n,blocksize:t}},Uh=e=>{if(e.length!==1)throw new Error(`DepthToSpace expect 1 inputs, but got ${e.length}`);if(e[0].type==="string"||e[0].dims.length!==4)throw new TypeError("DepthToSpace input should be a 4-D numeric tensor")}}),y_,w_,Vh,kE=N(()=>{"use strict";Ne(),y_=(e,t,n)=>{Vh(t,n);let i=ye.flattenShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],i)]},w_=e=>e.attributes.getInt("axis",1),Vh=(e,t)=>{if(!e||e.length!==1)throw new Error("Flatten requires 1 input.");let n=e[0].dims.length;if(n===0)throw new Error("scalar tensor is not supported.");if(t<-n||t>n)throw new Error("Invalid axis");if(e[0].type==="string")throw new Error("string tensor is not supported.")}}),Bi,Zo=N(()=>{"use strict";Bi=["float32","float64","int32","int16","int8","uint16","uint32","uint8"]}),__,v_,Gh,qh,Hh,Wh,PE=N(()=>{"use strict";ut(),Zo(),Ne(),Pe(),__=(e,t,n)=>(Wh(t,n.axis),[e.run(Hh(e,t,n),t)]),v_=e=>Le({axis:e.attributes.getInt("axis",0)}),Gh={name:"Gather",inputNames:["A","B"],inputTypes:[0,0]},qh=(e,t,n,i)=>{let u=n[0].dims.slice(),l=n[1].dims.slice(),d=new Array(u.length+l.length-1);i=ye.normalizeAxis(i,u.length);let c=[];for(let p=0;p{let o={...Bh,cacheHint:n.cacheKey};return{...o,get:()=>Mh(e,o,t,n.axis)}},jh=(e,t)=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.");let n=e[0].dims.length;if(n<1)throw new Error("Invalid input shape.");if(t<-n||t>n-1)throw new Error("Invalid axis.");if(Pi.indexOf(e[0].type)===-1)throw new Error("Invaid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invaid input type.")}}),Ku,ta,m_,g_,Lh,Uh,Vh,_E=R(()=>{"use strict";nt(),Pe(),Ie(),Ku=(e,t,n)=>(Vh(t,n),[e.run(Lh(t,n),t)]),ta=(e,t)=>{let n=e.attributes.getInt("transA",0)!==0,o=e.attributes.getInt("transB",0)!==0,u=e.attributes.getFloat("alpha",1),l=e.attributes.getFloat("beta",1);return Be({transA:n,transB:o,alpha:u,beta:l,isOptionalC:t})},m_=e=>ta(e,!1),g_=e=>ta(e,!0),Lh=(e,t)=>{let n={name:"Gemm",inputNames:e.length===3?["A","B","C"]:["A","B"],inputTypes:e.length===3?[0,0,0]:[0,0],key:t.cacheKey};return{...n,get:()=>Uh(n,e,t)}},Uh=(e,t,n)=>{let o=t[0].dims.slice(),u=t[1].dims.slice(),[l,d]=U0.getShapeOfGemmResult(o,n.transA,u,n.transB,t.length===3?t[2].dims:void 0),c=[l,d];if(!c)throw new Error("Can't use gemm on the given tensors");let s=o[o.length-1],r="";n.transA&&(s=o[0]),n.transA&&n.transB?r="value += _A_T(a) * _B_T(b);":n.transA&&!n.transB?r="value += _A_T(a) * _B(b);":!n.transA&&n.transB?r="value += _A(a) * _B_T(b);":!n.transA&&!n.transB&&(r="value += _A(a) * _B(b);");let i=c.length,a=t.length===3?`int c[${t[2].dims.length}];`:"",p=t.length===3?"bcastIndices_C(indices, c);":"",h=t.length===3?"value += beta * _C(c);":"",m=` - float process(int indices[${i}]) { - int a[${i}]; - int b[${i}]; + }`;return{...t,output:{dims:d,type:n[0].type,textureType:0},shaderSource:a}},Hh=(e,t,n)=>{let i={...Gh,cacheHint:n.cacheKey};return{...i,get:()=>qh(e,i,t,n.axis)}},Wh=(e,t)=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.");let n=e[0].dims.length;if(n<1)throw new Error("Invalid input shape.");if(t<-n||t>n-1)throw new Error("Invalid axis.");if(Bi.indexOf(e[0].type)===-1)throw new Error("Invaid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invaid input type.")}}),Ju,oa,x_,$_,Kh,Xh,Zh,CE=N(()=>{"use strict";ut(),Ne(),Pe(),Ju=(e,t,n)=>(Zh(t,n),[e.run(Kh(t,n),t)]),oa=(e,t)=>{let n=e.attributes.getInt("transA",0)!==0,i=e.attributes.getInt("transB",0)!==0,u=e.attributes.getFloat("alpha",1),l=e.attributes.getFloat("beta",1);return Le({transA:n,transB:i,alpha:u,beta:l,isOptionalC:t})},x_=e=>oa(e,!1),$_=e=>oa(e,!0),Kh=(e,t)=>{let n={name:"Gemm",inputNames:e.length===3?["A","B","C"]:["A","B"],inputTypes:e.length===3?[0,0,0]:[0,0],key:t.cacheKey};return{...n,get:()=>Xh(n,e,t)}},Xh=(e,t,n)=>{let i=t[0].dims.slice(),u=t[1].dims.slice(),[l,d]=X0.getShapeOfGemmResult(i,n.transA,u,n.transB,t.length===3?t[2].dims:void 0),c=[l,d];if(!c)throw new Error("Can't use gemm on the given tensors");let s=i[i.length-1],r="";n.transA&&(s=i[0]),n.transA&&n.transB?r="value += _A_T(a) * _B_T(b);":n.transA&&!n.transB?r="value += _A_T(a) * _B(b);":!n.transA&&n.transB?r="value += _A(a) * _B_T(b);":!n.transA&&!n.transB&&(r="value += _A(a) * _B(b);");let o=c.length,a=t.length===3?`int c[${t[2].dims.length}];`:"",p=t.length===3?"bcastIndices_C(indices, c);":"",h=t.length===3?"value += beta * _C(c);":"",m=` + float process(int indices[${o}]) { + int a[${o}]; + int b[${o}]; ${a} copyVec(indices, a); @@ -810,20 +810,20 @@ float process(int indices[${m}]) { float value = 0.0; for (int k=0; k<${s}; ++k) { - a[${i-1}] = k; - b[${i-2}] = k; + a[${o-1}] = k; + b[${o-2}] = k; ${r} } value = value * alpha; ${h} return value; - }`;return{...e,output:{dims:c,type:t[0].type,textureType:0},variables:[{name:"alpha",type:"float",data:n.alpha},{name:"beta",type:"float",data:n.beta}],shaderSource:m}},Vh=(e,t)=>{if(!e)throw new Error("Input is missing");if(t.isOptionalC&&(e.length<2||e.length>3))throw new Error("Invaid input shape.");if(!t.isOptionalC&&e.length!==3)throw new Error("Gemm requires 3 inputs");if(e.length===3&&e[2].dims.length!==1&&e[2].dims.length!==2)throw new Error("Invalid input shape of C");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64"||e.length===3&&e[2].type!=="float32"&&e[2].type!=="float64")throw new Error("Invalid input type.");if(e[0].type!==e[1].type||e.length===3&&e[0].type!==e[2].type)throw new Error("Input types are mismatched")}}),b_,y_,Gh,qh,Hh,Wh,Kh,vE=R(()=>{"use strict";nt(),Ie(),b_=(e,t,n)=>(Kh(t),[e.run(Hh(e,t,n),t)]),y_=e=>{let t=e.attributes.getFloat("scale"),n=e.attributes.getFloats("bias");return Be({scale:t,bias:n})},Gh={name:"ImageScaler",inputNames:["X"],inputTypes:[0]},qh=(e,t,n,o)=>{let u=n[0].dims.slice(),l=u.length,d=` - ${Wh(o.bias.length)} + }`;return{...e,output:{dims:c,type:t[0].type,textureType:0},variables:[{name:"alpha",type:"float",data:n.alpha},{name:"beta",type:"float",data:n.beta}],shaderSource:m}},Zh=(e,t)=>{if(!e)throw new Error("Input is missing");if(t.isOptionalC&&(e.length<2||e.length>3))throw new Error("Invaid input shape.");if(!t.isOptionalC&&e.length!==3)throw new Error("Gemm requires 3 inputs");if(e.length===3&&e[2].dims.length!==1&&e[2].dims.length!==2)throw new Error("Invalid input shape of C");if(e[0].type!=="float32"&&e[0].type!=="float64"||e[1].type!=="float32"&&e[1].type!=="float64"||e.length===3&&e[2].type!=="float32"&&e[2].type!=="float64")throw new Error("Invalid input type.");if(e[0].type!==e[1].type||e.length===3&&e[0].type!==e[2].type)throw new Error("Input types are mismatched")}}),T_,S_,Yh,Jh,Qh,ef,tf,DE=N(()=>{"use strict";ut(),Pe(),T_=(e,t,n)=>(tf(t),[e.run(Qh(e,t,n),t)]),S_=e=>{let t=e.attributes.getFloat("scale"),n=e.attributes.getFloats("bias");return Le({scale:t,bias:n})},Yh={name:"ImageScaler",inputNames:["X"],inputTypes:[0]},Jh=(e,t,n,i)=>{let u=n[0].dims.slice(),l=u.length,d=` + ${ef(i.bias.length)} float process(int indices[${l}]) { return _X(indices) * scale + getBias(bias, indices[1]); - }`;return{...t,output:{dims:u,type:n[0].type,textureType:0},variables:[{name:"bias",type:"float",arrayLength:o.bias.length,data:o.bias},{name:"scale",type:"float",data:o.scale}],shaderSource:d}},Hh=(e,t,n)=>{let o={...Gh,cacheHint:n.cacheKey};return{...o,get:()=>qh(e,o,t,n)}},Wh=e=>{let t=[`float getBias(float bias[${e}], int channel) {`];for(let n=0;n{if(!e||e.length!==1)throw new Error("ImageScaler requires 1 input.");if(e[0].dims.length!==4)throw new Error("Invalid input shape.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")}}),w_,__,ra,Xh,Zh,Jh,Yh,Qh,ef,xE=R(()=>{"use strict";qe(),Ie(),w_=(e,t,n)=>{ef(t);let o=e.run(Zh(t[0]),t);return[e.run(Qh(e,t[0],n,o.dims),[t[0],o,t[1],t[2]])]},__=e=>e.attributes.getFloat("epsilon",1e-5),ra={name:"InstanceNormalization_MeanAndVariance",inputNames:["X"],inputTypes:[0]},Xh=(e,t)=>{let n=t.dims.slice(),o=n[1],u=n[2]*n[3],l=[n[0],o],d=` + }`;return{...t,output:{dims:u,type:n[0].type,textureType:0},variables:[{name:"bias",type:"float",arrayLength:i.bias.length,data:i.bias},{name:"scale",type:"float",data:i.scale}],shaderSource:d}},Qh=(e,t,n)=>{let i={...Yh,cacheHint:n.cacheKey};return{...i,get:()=>Jh(e,i,t,n)}},ef=e=>{let t=[`float getBias(float bias[${e}], int channel) {`];for(let n=0;n{if(!e||e.length!==1)throw new Error("ImageScaler requires 1 input.");if(e[0].dims.length!==4)throw new Error("Invalid input shape.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")}}),I_,O_,sa,rf,nf,of,sf,af,uf,RE=N(()=>{"use strict";Ke(),Pe(),I_=(e,t,n)=>{uf(t);let i=e.run(nf(t[0]),t);return[e.run(af(e,t[0],n,i.dims),[t[0],i,t[1],t[2]])]},O_=e=>e.attributes.getFloat("epsilon",1e-5),sa={name:"InstanceNormalization_MeanAndVariance",inputNames:["X"],inputTypes:[0]},rf=(e,t)=>{let n=t.dims.slice(),i=n[1],u=n[2]*n[3],l=[n[0],i],d=` vec4 process(int[2] indices) { vec4 v = vec4(0.0); int a[4]; @@ -852,7 +852,7 @@ float process(int indices[${m}]) { v.g = temp / float(${u}); return v; - }`;return{...e,output:{dims:l,type:t.type,textureType:4},shaderSource:d}},Zh=e=>({...ra,get:()=>Xh(ra,e)}),Jh={name:"InstanceNormalization_ComputeOutput",inputNames:["X","MeanAndVariance","Scale","B"],inputTypes:[0,4,0,0]},Yh=(e,t,n,o,u)=>{let l=Te(e.session.backend.glContext.version),[d,c]=e.calculateTextureWidthAndHeight(u,4),[s,r]=[d/4,c],i=` + }`;return{...e,output:{dims:l,type:t.type,textureType:4},shaderSource:d}},nf=e=>({...sa,get:()=>rf(sa,e)}),of={name:"InstanceNormalization_ComputeOutput",inputNames:["X","MeanAndVariance","Scale","B"],inputTypes:[0,4,0,0]},sf=(e,t,n,i,u)=>{let l=Ae(e.session.backend.glContext.version),[d,c]=e.calculateTextureWidthAndHeight(u,4),[s,r]=[d/4,c],o=` vec4 get_MeanAndVariance(int[2] mv) { int offset = indicesToOffset_MeanAndVariance(mv); vec2 coords = offsetToCoords(offset, ${s}, ${r}); @@ -873,8 +873,8 @@ float process(int indices[${m}]) { float b = _B(sb); return scale * (_X(indices) - mean) / sqrt(variance + epsilon) + b; - }`;return{...t,output:{dims:n.dims,type:n.type,textureType:0},variables:[{name:"epsilon",type:"float",data:o}],shaderSource:i}},Qh=(e,t,n,o)=>{let u={...Jh,cacheHint:`${n}`};return{...u,get:()=>Yh(e,u,t,n,o)}},ef=e=>{if(!e||e.length!==3)throw new Error("InstanceNormalization requires 3 inputs.");let t=e[0],n=e[1],o=e[2];if(t.dims.length<3||n.dims.length!==1||o.dims.length!==1)throw new Error("Invalid input shape.");if(n.dims[0]!==t.dims[1]||o.dims[0]!==t.dims[1])throw new Error("Input shapes are mismatched.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||o.type!=="float32"&&o.type!=="float64")throw new Error("Invalid input type.");if(e[0].dims.length!==4)throw new Error("Only support 4-D input shape.")}});function $E(e,t){let n=e[0].dims[1],o=e[0].dims.length,u=-Math.floor((t.size-1)/2),l=Math.ceil((t.size-1)/2),d=`float(${t.alpha}) / float(${t.size})`,c=`float(${t.bias})`,s=`float(${t.beta})`,r=` - float process(int indices[${o}]) { + }`;return{...t,output:{dims:n.dims,type:n.type,textureType:0},variables:[{name:"epsilon",type:"float",data:i}],shaderSource:o}},af=(e,t,n,i)=>{let u={...of,cacheHint:`${n}`};return{...u,get:()=>sf(e,u,t,n,i)}},uf=e=>{if(!e||e.length!==3)throw new Error("InstanceNormalization requires 3 inputs.");let t=e[0],n=e[1],i=e[2];if(t.dims.length<3||n.dims.length!==1||i.dims.length!==1)throw new Error("Invalid input shape.");if(n.dims[0]!==t.dims[1]||i.dims[0]!==t.dims[1])throw new Error("Input shapes are mismatched.");if(t.type!=="float32"&&t.type!=="float64"||n.type!=="float32"&&n.type!=="float64"||i.type!=="float32"&&i.type!=="float64")throw new Error("Invalid input type.");if(e[0].dims.length!==4)throw new Error("Only support 4-D input shape.")}});function NE(e,t){let n=e[0].dims[1],i=e[0].dims.length,u=-Math.floor((t.size-1)/2),l=Math.ceil((t.size-1)/2),d=`float(${t.alpha}) / float(${t.size})`,c=`float(${t.bias})`,s=`float(${t.beta})`,r=` + float process(int indices[${i}]) { int c = indices[1]; float x = _X(indices); float square_sum = 0.0; @@ -888,11 +888,11 @@ float process(int indices[${m}]) { } } return x / pow(${c} + ${d} * square_sum, ${s}); - }`;return{...Pl,cacheHint:t.cacheKey,output:{dims:e[0].dims,type:e[0].type,textureType:0},shaderSource:r}}function TE(e,t){return{...Pl,cacheHint:t.cacheKey,get:()=>$E(e,t)}}var v_,x_,Pl,tf,SE=R(()=>{"use strict";nt(),Ie(),v_=(e,t,n)=>(tf(t),[e.run(TE(t,n),t)]),x_=e=>{let t=e.attributes.getFloat("alpha",1e-4),n=e.attributes.getFloat("beta",.75),o=e.attributes.getFloat("bias",1),u=e.attributes.getInt("size");return Be({alpha:t,beta:n,bias:o,size:u})},Pl={name:"LRN",inputNames:["X"],inputTypes:[0]},tf=e=>{if(!e||e.length!==1)throw new Error("LRN requires 1 input.");if(e[0].dims.length!==4)throw new Error('currently only support LRN for input with "NCHW" format');if(e[0].type!=="float32")throw new Error("input should be float type")}}),rf,Xu,$_,T_,S_,nf,of,sf,af,uf,lf,df,cf,IE=R(()=>{"use strict";nt(),Pe(),qe(),Ie(),rf={name:"Pad",inputNames:["A"],inputTypes:[0]},Xu=(e,t,n)=>(sf(t),[e.run({...rf,cacheHint:n.cacheKey,get:()=>of(e,t[0],n)},t)]),$_=e=>{let t=e.attributes.getString("mode","constant"),n=e.attributes.getFloat("value",0),o=e.attributes.getInts("pads");return Be({mode:t,value:n,pads:o})},T_=(e,t,n)=>{af(t);let o=nf(e,t,n);return Xu(e,[t[0]],o)},S_=e=>e.attributes.getString("mode","constant"),nf=(e,t,n)=>{if(!e.session.isInitializer(t[1].dataId)||t.length>=3&&!e.session.isInitializer(t[2].dataId))throw new Error("dynamic pad attributes are not allowed");let o=Array.from(t[1].integerData),u=t.length>=3?t[2].floatData[0]:0;return Be({mode:n,pads:o,value:u})},of=(e,t,n)=>{let o=he.padShape(t.dims.slice(),n.pads),u=o.length,l=` - ${uf(e,t,n)} + }`;return{...Nl,cacheHint:t.cacheKey,output:{dims:e[0].dims,type:e[0].type,textureType:0},shaderSource:r}}function zE(e,t){return{...Nl,cacheHint:t.cacheKey,get:()=>NE(e,t)}}var E_,A_,Nl,lf,ME=N(()=>{"use strict";ut(),Pe(),E_=(e,t,n)=>(lf(t),[e.run(zE(t,n),t)]),A_=e=>{let t=e.attributes.getFloat("alpha",1e-4),n=e.attributes.getFloat("beta",.75),i=e.attributes.getFloat("bias",1),u=e.attributes.getInt("size");return Le({alpha:t,beta:n,bias:i,size:u})},Nl={name:"LRN",inputNames:["X"],inputTypes:[0]},lf=e=>{if(!e||e.length!==1)throw new Error("LRN requires 1 input.");if(e[0].dims.length!==4)throw new Error('currently only support LRN for input with "NCHW" format');if(e[0].type!=="float32")throw new Error("input should be float type")}}),df,Qu,k_,P_,C_,cf,pf,hf,ff,mf,gf,bf,yf,BE=N(()=>{"use strict";ut(),Ne(),Ke(),Pe(),df={name:"Pad",inputNames:["A"],inputTypes:[0]},Qu=(e,t,n)=>(hf(t),[e.run({...df,cacheHint:n.cacheKey,get:()=>pf(e,t[0],n)},t)]),k_=e=>{let t=e.attributes.getString("mode","constant"),n=e.attributes.getFloat("value",0),i=e.attributes.getInts("pads");return Le({mode:t,value:n,pads:i})},P_=(e,t,n)=>{ff(t);let i=cf(e,t,n);return Qu(e,[t[0]],i)},C_=e=>e.attributes.getString("mode","constant"),cf=(e,t,n)=>{if(!e.session.isInitializer(t[1].dataId)||t.length>=3&&!e.session.isInitializer(t[2].dataId))throw new Error("dynamic pad attributes are not allowed");let i=Array.from(t[1].integerData),u=t.length>=3?t[2].floatData[0]:0;return Le({mode:n,pads:i,value:u})},pf=(e,t,n)=>{let i=ye.padShape(t.dims.slice(),n.pads),u=i.length,l=` + ${mf(e,t,n)} float process(int[${u}] indices) { return padA(indices); - }`;return{name:"Pad",inputNames:["A"],inputTypes:[0],output:{dims:o,type:t.type,textureType:0},shaderSource:l}},sf=e=>{if(!e||e.length!==1)throw new Error("Pad requires 1 input");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},af=e=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Pad requires 2 or 3 inputs");if(e[1].type!=="int32")throw new Error("Invalid input type.");if(e.length>=3&&e[2].type==="string")throw new Error("Invalid input type.")},uf=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t.dims,0),d=he.computeStrides(t.dims);switch(n.mode){case"constant":return lf(o,t.dims,d,u,l,n.pads,n.value);case"reflect":return df(o,t.dims,d,u,l,n.pads);case"edge":return cf(o,t.dims,d,u,l,n.pads);default:throw new Error("Invalid mode")}},lf=(e,t,n,o,u,l,d)=>{let c=t.length,s="";for(let r=c-1;r>=0;--r)s+=` + }`;return{name:"Pad",inputNames:["A"],inputTypes:[0],output:{dims:i,type:t.type,textureType:0},shaderSource:l}},hf=e=>{if(!e||e.length!==1)throw new Error("Pad requires 1 input");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},ff=e=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Pad requires 2 or 3 inputs");if(e[1].type!=="int32")throw new Error("Invalid input type.");if(e.length>=3&&e[2].type==="string")throw new Error("Invalid input type.")},mf=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t.dims,0),d=ye.computeStrides(t.dims);switch(n.mode){case"constant":return gf(i,t.dims,d,u,l,n.pads,n.value);case"reflect":return bf(i,t.dims,d,u,l,n.pads);case"edge":return yf(i,t.dims,d,u,l,n.pads);default:throw new Error("Invalid mode")}},gf=(e,t,n,i,u,l,d)=>{let c=t.length,s="";for(let r=c-1;r>=0;--r)s+=` k = m[${r}] - ${l[r]}; if (k < 0) return constant; if (k >= ${t[r]}) return constant; @@ -903,11 +903,11 @@ float process(int indices[${m}]) { int offset = 0; int k = 0; ${s} - vec2 coords = offsetToCoords(offset, ${o}, ${u}); + vec2 coords = offsetToCoords(offset, ${i}, ${u}); float value = getColorAsFloat(${e.texture2D}(A, coords)); return value; } - `},df=(e,t,n,o,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` + `},bf=(e,t,n,i,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` k = m[${s}] - ${l[s]}; if (k < 0) { k = -k; } { @@ -921,11 +921,11 @@ float process(int indices[${m}]) { int offset = 0; int k = 0; ${c} - vec2 coords = offsetToCoords(offset, ${o}, ${u}); + vec2 coords = offsetToCoords(offset, ${i}, ${u}); float value = getColorAsFloat(${e.texture2D}(A, coords)); return value; } - `},cf=(e,t,n,o,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` + `},yf=(e,t,n,i,u,l)=>{let d=t.length,c="";for(let s=d-1;s>=0;--s)c+=` k = m[${s}] - ${l[s]}; if (k < 0) k = 0; if (k >= ${t[s]}) k = ${t[s]-1}; @@ -935,20 +935,20 @@ float process(int indices[${m}]) { int offset = 0; int k = 0; ${c} - vec2 coords = offsetToCoords(offset, ${o}, ${u}); + vec2 coords = offsetToCoords(offset, ${i}, ${u}); float value = getColorAsFloat(${e.texture2D}(A, coords)); return value; } - `}}),I_,O_,na,E_,A_,k_,P_,ia,oa,pf,sa,C_,Qn,aa,ei,hf,OE=R(()=>{"use strict";nt(),Pe(),Ie(),I_=(e,t,n)=>{Qn(t);let o={name:"AveragePool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...o,get:()=>na(t,o,!1,n)},t)]},O_=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),o=e.attributes.getInt("count_include_pad",0)!==0,u=e.attributes.getInts("kernel_shape"),l=e.attributes.getInts("strides",[]),d=e.attributes.getInts("pads",[]);if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");return Be({autoPad:t,ceilMode:n,countIncludePad:o,kernelShape:u,strides:l,pads:d})},na=(e,t,n,o)=>{let[u,l]=oa(e,o,n),d=he.size(u.kernelShape),c="value += _X(x);",s="";u.countIncludePad?s+=`value /= float(${d});`:s+=`value /= float(${d} - pad);`;let r=` - ${aa(e[0].dims,u,c,s,"0.0")} - `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:r}},E_=(e,t,n)=>{Qn(t);let o={name:"GlobalAveragePool",inputNames:["X"],inputTypes:[0],cacheHint:`${n.countIncludePad}`};return[e.run({...o,get:()=>na(t,o,!0,n)},t)]},A_=e=>{let t=e.attributes.getInt("count_include_pad",0)!==0;return Be({autoPad:"",ceilMode:0,countIncludePad:t,kernelShape:[],strides:[],pads:[]})},k_=(e,t,n)=>{Qn(t);let o={name:"MaxPool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...o,get:()=>ia(t,o,!1,n)},t)]},P_=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),o=e.attributes.getInts("kernel_shape"),u=e.attributes.getInts("strides",[]),l=e.attributes.getInts("pads",[]),d=e.attributes.getInt("storage_order",0),c=e.attributes.getInts("dilations",[]);if(d!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");return Be({autoPad:t,ceilMode:n,countIncludePad:!1,kernelShape:o,strides:u,pads:l,storageOrder:d,dilations:c})},ia=(e,t,n,o)=>{let[u,l]=oa(e,o,n),d=` - ${aa(e[0].dims,u,` + `}}),D_,R_,aa,N_,z_,M_,B_,ua,la,wf,da,F_,si,ca,ai,_f,FE=N(()=>{"use strict";ut(),Ne(),Pe(),D_=(e,t,n)=>{si(t);let i={name:"AveragePool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...i,get:()=>aa(t,i,!1,n)},t)]},R_=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),i=e.attributes.getInt("count_include_pad",0)!==0,u=e.attributes.getInts("kernel_shape"),l=e.attributes.getInts("strides",[]),d=e.attributes.getInts("pads",[]);if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");return Le({autoPad:t,ceilMode:n,countIncludePad:i,kernelShape:u,strides:l,pads:d})},aa=(e,t,n,i)=>{let[u,l]=la(e,i,n),d=ye.size(u.kernelShape),c="value += _X(x);",s="";u.countIncludePad?s+=`value /= float(${d});`:s+=`value /= float(${d} - pad);`;let r=` + ${ca(e[0].dims,u,c,s,"0.0")} + `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:r}},N_=(e,t,n)=>{si(t);let i={name:"GlobalAveragePool",inputNames:["X"],inputTypes:[0],cacheHint:`${n.countIncludePad}`};return[e.run({...i,get:()=>aa(t,i,!0,n)},t)]},z_=e=>{let t=e.attributes.getInt("count_include_pad",0)!==0;return Le({autoPad:"",ceilMode:0,countIncludePad:t,kernelShape:[],strides:[],pads:[]})},M_=(e,t,n)=>{si(t);let i={name:"MaxPool",inputNames:["X"],inputTypes:[0],cacheHint:n.cacheKey};return[e.run({...i,get:()=>ua(t,i,!1,n)},t)]},B_=e=>{let t=e.attributes.getString("auto_pad","NOTSET"),n=e.attributes.getInt("ceil_mode",0),i=e.attributes.getInts("kernel_shape"),u=e.attributes.getInts("strides",[]),l=e.attributes.getInts("pads",[]),d=e.attributes.getInt("storage_order",0),c=e.attributes.getInts("dilations",[]);if(d!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");return Le({autoPad:t,ceilMode:n,countIncludePad:!1,kernelShape:i,strides:u,pads:l,storageOrder:d,dilations:c})},ua=(e,t,n,i)=>{let[u,l]=la(e,i,n),d=` + ${ca(e[0].dims,u,` value = max(_X(x), value); `,"","-1e5")} - `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:d}},oa=(e,t,n)=>{let o=e[0].dims.slice(),u=Object.hasOwnProperty.call(t,"dilations"),l=t.kernelShape.slice(),d=t.strides.slice(),c=u?t.dilations.slice():[],s=t.pads.slice();Io.adjustPoolAttributes(n,o,l,d,c,s);let r=Io.computePoolOutputShape(n,o,d,c,l,s,t.autoPad),i=Object.assign({},t);return u?Object.assign(i,{kernelShape:l,strides:d,pads:s,dilations:c,cacheKey:t.cacheKey}):Object.assign(i,{kernelShape:l,strides:d,pads:s,cacheKey:t.cacheKey}),[i,r]},pf={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[],cacheKey:""},sa={name:"GlobalMaxPool",inputNames:["X"],inputTypes:[0]},C_=(e,t)=>(Qn(t),[e.run({...sa,get:()=>ia(t,sa,!0,pf)},t)]),Qn=e=>{if(!e||e.length!==1)throw new Error("Pool ops requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},aa=(e,t,n,o,u)=>{let l=e.length;if(t.kernelShape.length<=2){let d=t.kernelShape[t.kernelShape.length-1],c=t.strides[t.strides.length-1],s=t.pads[t.pads.length/2-1],r=t.pads[t.pads.length-1],i=e[l-1],a="",p="",h="";if(s+r!==0?a=` + `;return{...t,output:{dims:l,type:e[0].type,textureType:0},shaderSource:d}},la=(e,t,n)=>{let i=e[0].dims.slice(),u=Object.hasOwnProperty.call(t,"dilations"),l=t.kernelShape.slice(),d=t.strides.slice(),c=u?t.dilations.slice():[],s=t.pads.slice();Do.adjustPoolAttributes(n,i,l,d,c,s);let r=Do.computePoolOutputShape(n,i,d,c,l,s,t.autoPad),o=Object.assign({},t);return u?Object.assign(o,{kernelShape:l,strides:d,pads:s,dilations:c,cacheKey:t.cacheKey}):Object.assign(o,{kernelShape:l,strides:d,pads:s,cacheKey:t.cacheKey}),[o,r]},wf={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[],cacheKey:""},da={name:"GlobalMaxPool",inputNames:["X"],inputTypes:[0]},F_=(e,t)=>(si(t),[e.run({...da,get:()=>ua(t,da,!0,wf)},t)]),si=e=>{if(!e||e.length!==1)throw new Error("Pool ops requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type.")},ca=(e,t,n,i,u)=>{let l=e.length;if(t.kernelShape.length<=2){let d=t.kernelShape[t.kernelShape.length-1],c=t.strides[t.strides.length-1],s=t.pads[t.pads.length/2-1],r=t.pads[t.pads.length-1],o=e[l-1],a="",p="",h="";if(s+r!==0?a=` for (int i = 0; i < ${d}; i++) { x[${l} - 1] = indices[${l} - 1] * ${c} - ${s} + i; - if (x[${l} - 1] < 0 || x[${l} - 1] >= ${i}) { + if (x[${l} - 1] < 0 || x[${l} - 1] >= ${o}) { pad++; continue; } @@ -957,16 +957,16 @@ float process(int indices[${m}]) { for (int i = 0; i < ${d}; i++) { x[${l} - 1] = indices[${l} - 1] * ${c} - ${s} + i; ${n} - }`,t.kernelShape.length===2){let m=t.kernelShape[t.kernelShape.length-2],b=t.strides[t.strides.length-2],x=t.pads[t.pads.length/2-2],w=t.pads[t.pads.length-2],y=e[l-2];x+w!==0?p=` + }`,t.kernelShape.length===2){let m=t.kernelShape[t.kernelShape.length-2],g=t.strides[t.strides.length-2],v=t.pads[t.pads.length/2-2],w=t.pads[t.pads.length-2],y=e[l-2];v+w!==0?p=` for (int j = 0; j < ${m}; j++) { - x[${l} - 2] = indices[${l} - 2] * ${b} - ${x} + j; + x[${l} - 2] = indices[${l} - 2] * ${g} - ${v} + j; if (x[${l} - 2] < 0 || x[${l} - 2] >= ${y}) { pad+= ${d}; continue; } `:p=` for (int j = 0; j < ${m}; j++) { - x[${l} - 2] = indices[${l} - 2] * ${b} - ${x} + j; + x[${l} - 2] = indices[${l} - 2] * ${g} - ${v} + j; `,h=` } `}return` @@ -979,10 +979,10 @@ float process(int indices[${m}]) { ${p} ${a} ${h} - ${o} + ${i} return value; } - `}else{let d=he.size(t.kernelShape),c=he.computeStrides(t.kernelShape),s=c.length,r=t.pads.length,i=hf(s),a=ei(e,"inputDims"),p=ei(t.pads,"pads"),h=ei(c,"kernelStrides"),m=ei(t.strides,"strides"),b=t.pads.reduce((w,y)=>w+y),x="";return b?x=` + `}else{let d=ye.size(t.kernelShape),c=ye.computeStrides(t.kernelShape),s=c.length,r=t.pads.length,o=_f(s),a=ai(e,"inputDims"),p=ai(t.pads,"pads"),h=ai(c,"kernelStrides"),m=ai(t.strides,"strides"),g=t.pads.reduce((w,y)=>w+y),v="";return g?v=` if (x[j] >= inputDims[j] || x[j] < 0) { pad++; isPad = true; @@ -991,11 +991,11 @@ float process(int indices[${m}]) { } if (!isPad) { ${n} - }`:x=` + }`:v=` } ${n} `,` - ${i} + ${o} float process(int indices[${l}]) { int x[${l}]; copyVec(indices, x); @@ -1018,15 +1018,15 @@ float process(int indices[${m}]) { for (int j = ${l} - ${s}; j < ${l}; j++) { x[j] = indices[j] * strides[j - ${l} + ${s}] + offset[j - ${l} + ${s}] - pads[j - 2]; - ${x} + ${v} } - ${o} + ${i} return value; } - `}},ei=(e,t)=>{let n="";for(let o=0;o` + `}},ai=(e,t)=>{let n="";for(let i=0;i` void offsetToIndices(int offset, int[${e}] strides, out int[${e}] indices) { if (${e} == 0) { return; @@ -1036,7 +1036,7 @@ float process(int indices[${m}]) { offset -= indices[i] * strides[i]; } indices[${e} - 1] = offset; - }`}),lr,yr,ff,mf,D_,R_,N_,z_,B_,M_,F_,EE=R(()=>{"use strict";nt(),Vo(),Pe(),Ie(),lr=(e,t,n,o,u)=>{mf(t);let l={name:o,inputNames:["A"],inputTypes:[0]};return[e.run({...l,cacheHint:n.cacheKey,get:()=>ff(e,t,n,o,u,l)},t)]},yr=e=>{let t=e.attributes.getInts("axes",[]),n=e.attributes.getInt("keepdims",1)===1;return Be({axes:t,keepDims:n})},ff=(e,t,n,o,u,l)=>{let d=[],c=t[0].dims.length||1,s=[],r=he.normalizeAxes(n.axes,t[0].dims.length),i=u(t,r),a=i[1];for(let h=0;h=0||r.length===0?(n.keepDims&&d.push(1),a=` + }`}),mr,Tr,vf,xf,j_,L_,U_,V_,G_,q_,H_,jE=N(()=>{"use strict";ut(),Zo(),Ne(),Pe(),mr=(e,t,n,i,u)=>{xf(t);let l={name:i,inputNames:["A"],inputTypes:[0]};return[e.run({...l,cacheHint:n.cacheKey,get:()=>vf(e,t,n,i,u,l)},t)]},Tr=e=>{let t=e.attributes.getInts("axes",[]),n=e.attributes.getInt("keepdims",1)===1;return Le({axes:t,keepDims:n})},vf=(e,t,n,i,u,l)=>{let d=[],c=t[0].dims.length||1,s=[],r=ye.normalizeAxes(n.axes,t[0].dims.length),o=u(t,r),a=o[1];for(let h=0;h=0||r.length===0?(n.keepDims&&d.push(1),a=` for(int j${h} = 0; j${h} < ${t[0].dims[h]}; j${h}++) { inputIdx[${h}] = j${h}; ${a} @@ -1046,24 +1046,24 @@ float process(int indices[${m}]) { int inputIdx[${c}]; // addressing input data ${s.join(` `)} - ${i[0]} // init ops for reduce max/min + ${o[0]} // init ops for reduce max/min ${a} - ${i[2]} // final computation for reduce mean + ${o[2]} // final computation for reduce mean return value; - }`;return{...l,output:{dims:d,type:t[0].type,textureType:0},shaderSource:p}},mf=e=>{if(!e||e.length!==1)throw new Error("Reduce op requires 1 input.");if(Pi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},D_=(e,t,n)=>lr(e,t,n,"ReduceSum",()=>["value = 0.0;","value += _A(inputIdx);",""]),R_=(e,t,n)=>lr(e,t,n,"ReduceMean",(o,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=o[0].dims[d]);return["value = 0.0;","value += _A(inputIdx);",`value /= ${l}.;`]}),N_=(e,t,n)=>lr(e,t,n,"ReduceMax",(o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` + }`;return{...l,output:{dims:d,type:t[0].type,textureType:0},shaderSource:p}},xf=e=>{if(!e||e.length!==1)throw new Error("Reduce op requires 1 input.");if(Bi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},j_=(e,t,n)=>mr(e,t,n,"ReduceSum",()=>["value = 0.0;","value += _A(inputIdx);",""]),L_=(e,t,n)=>mr(e,t,n,"ReduceMean",(i,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=i[0].dims[d]);return["value = 0.0;","value += _A(inputIdx);",`value /= ${l}.;`]}),U_=(e,t,n)=>mr(e,t,n,"ReduceMax",(i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` `)} -value = _A(inputIdx);`,"value = max(value, _A(inputIdx));",""]}),z_=(e,t,n)=>lr(e,t,n,"ReduceMin",(o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` +value = _A(inputIdx);`,"value = max(value, _A(inputIdx));",""]}),V_=(e,t,n)=>mr(e,t,n,"ReduceMin",(i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`inputIdx[${d}] = 0;`);return[`${l.join(` `)} -value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr(e,t,n,"ReduceProd",()=>["value = 1.0;","value *= _A(inputIdx);",""]),M_=(e,t,n)=>lr(e,t,n,"ReduceLogSum",()=>["value = 0.0;","value += _A(inputIdx);","value = log(value);"]),F_=(e,t,n)=>lr(e,t,n,"ReduceLogSumSquare",()=>["float t; value = 0.0;","t = _A(inputIdx); value += t * t;",""])}),j_,AE=R(()=>{"use strict";Pe(),j_=(e,t)=>{let n=he.calculateReshapedDims(t[0].dims,t[1].integerData);return e.session.pack?[e.reshapePacked(t[0],n)]:[e.reshapeUnpacked(t[0],n)]}}),ua,Zu,L_,U_,xi,gf,Ju,Eo,V_=R(()=>{"use strict";nt(),qe(),Ie(),ua={name:"Upsample",inputNames:["X"],inputTypes:[0]},Zu=(e,t,n)=>(Ju(t,n),[e.run({...ua,cacheHint:n.cacheKey,get:()=>gf(e,t,n)},t)]),L_=e=>xi(e,7),U_=e=>xi(e,9),xi=(e,t)=>{let n=t>=10,o=e.attributes.getString("mode","nearest");if(o!=="nearest"&&o!=="linear"&&(t<11||o!=="cubic"))throw new Error(`unrecognized mode: ${o}`);let u=[];t<9&&(u=e.attributes.getFloats("scales"),Eo(u,o,n));let l=e.attributes.getFloat("extrapolation_value",0),d=t>10?e.attributes.getString("coordinate_transformation_mode","half_pixel"):"asymmetric";if(["asymmetric","pytorch_half_pixel","tf_half_pixel_for_nn","align_corners","tf_crop_and_resize","half_pixel"].indexOf(d)===-1)throw new Error(`coordinate_transform_mode '${d}' is not supported`);let c=d==="tf_crop_and_resize",s=c,r=o==="nearest"&&t>=11?e.attributes.getString("nearest_mode","round_prefer_floor"):"";if(["round_prefer_floor","round_prefer_ceil","floor","ceil",""].indexOf(r)===-1)throw new Error(`nearest_mode '${r}' is not supported`);let i=e.attributes.getFloat("cubic_coeff_a",-.75),a=e.attributes.getInt("exclude_outside",0)!==0;if(a&&o!=="cubic")throw new Error("exclude_outside can be set to 1 only when mode is CUBIC.");let p=t<11?!0:o==="nearest"&&d==="asymmetric"&&r==="floor",h=0,m=0,b=0;return t>10?e.inputs.length>2?(h=1,m=2,b=3):(m=1,b=2):t===9&&(m=1),Be({opset:t,isResize:n,mode:o,scales:u,extrapolationValue:l,coordinateTransformMode:d,useExtrapolation:s,needRoiInput:c,nearestMode:r,cubicCoefficientA:i,excludeOutside:a,useNearest2xOptimization:p,roiInputIdx:h,scalesInputIdx:m,sizesInputIdx:b})},gf=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t[0].dims,0),d=t[0].dims.map((b,x)=>Math.floor(b*n.scales[x])),[c,s]=e.calculateTextureWidthAndHeight(d,0),r=d.length,i=new Array(r),a=new Array(r),p=` +value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),G_=(e,t,n)=>mr(e,t,n,"ReduceProd",()=>["value = 1.0;","value *= _A(inputIdx);",""]),q_=(e,t,n)=>mr(e,t,n,"ReduceLogSum",()=>["value = 0.0;","value += _A(inputIdx);","value = log(value);"]),H_=(e,t,n)=>mr(e,t,n,"ReduceLogSumSquare",()=>["float t; value = 0.0;","t = _A(inputIdx); value += t * t;",""])}),W_,LE=N(()=>{"use strict";Ne(),W_=(e,t)=>{let n=ye.calculateReshapedDims(t[0].dims,t[1].integerData);return e.session.pack?[e.reshapePacked(t[0],n)]:[e.reshapeUnpacked(t[0],n)]}}),pa,el,K_,X_,Ai,$f,tl,No,Z_=N(()=>{"use strict";ut(),Ke(),Pe(),pa={name:"Upsample",inputNames:["X"],inputTypes:[0]},el=(e,t,n)=>(tl(t,n),[e.run({...pa,cacheHint:n.cacheKey,get:()=>$f(e,t,n)},t)]),K_=e=>Ai(e,7),X_=e=>Ai(e,9),Ai=(e,t)=>{let n=t>=10,i=e.attributes.getString("mode","nearest");if(i!=="nearest"&&i!=="linear"&&(t<11||i!=="cubic"))throw new Error(`unrecognized mode: ${i}`);let u=[];t<9&&(u=e.attributes.getFloats("scales"),No(u,i,n));let l=e.attributes.getFloat("extrapolation_value",0),d=t>10?e.attributes.getString("coordinate_transformation_mode","half_pixel"):"asymmetric";if(["asymmetric","pytorch_half_pixel","tf_half_pixel_for_nn","align_corners","tf_crop_and_resize","half_pixel"].indexOf(d)===-1)throw new Error(`coordinate_transform_mode '${d}' is not supported`);let c=d==="tf_crop_and_resize",s=c,r=i==="nearest"&&t>=11?e.attributes.getString("nearest_mode","round_prefer_floor"):"";if(["round_prefer_floor","round_prefer_ceil","floor","ceil",""].indexOf(r)===-1)throw new Error(`nearest_mode '${r}' is not supported`);let o=e.attributes.getFloat("cubic_coeff_a",-.75),a=e.attributes.getInt("exclude_outside",0)!==0;if(a&&i!=="cubic")throw new Error("exclude_outside can be set to 1 only when mode is CUBIC.");let p=t<11?!0:i==="nearest"&&d==="asymmetric"&&r==="floor",h=0,m=0,g=0;return t>10?e.inputs.length>2?(h=1,m=2,g=3):(m=1,g=2):t===9&&(m=1),Le({opset:t,isResize:n,mode:i,scales:u,extrapolationValue:l,coordinateTransformMode:d,useExtrapolation:s,needRoiInput:c,nearestMode:r,cubicCoefficientA:o,excludeOutside:a,useNearest2xOptimization:p,roiInputIdx:h,scalesInputIdx:m,sizesInputIdx:g})},$f=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),[u,l]=e.calculateTextureWidthAndHeight(t[0].dims,0),d=t[0].dims.map((g,v)=>Math.floor(g*n.scales[v])),[c,s]=e.calculateTextureWidthAndHeight(d,0),r=d.length,o=new Array(r),a=new Array(r),p=` int output_pitches[${r}]; int input_pitches[${r}]; - `;for(let b=r-1;b>=0;b--)i[b]=b===r-1?1:i[b+1]*d[b+1],a[b]=b===r-1?1:a[b+1]*t[0].dims[b+1],p+=` - output_pitches[${b}] = ${i[b]}; - input_pitches[${b}] = ${a[b]}; + `;for(let g=r-1;g>=0;g--)o[g]=g===r-1?1:o[g+1]*d[g+1],a[g]=g===r-1?1:a[g+1]*t[0].dims[g+1],p+=` + output_pitches[${g}] = ${o[g]}; + input_pitches[${g}] = ${a[g]}; `;let h=` float getInputFloat(int index) { vec2 coords = offsetToCoords(index, ${u}, ${l}); - float value = getColorAsFloat(${o.texture2D}(X, coords)); + float value = getColorAsFloat(${i.texture2D}(X, coords)); return value; } `,m=n.mode==="nearest"?` @@ -1190,10 +1190,10 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( float y0 = x00 + float(y_offset) * (x01 - x00) / float(scales[0]); float y1 = x10 + float(y_offset) * (x11 - x10) / float(scales[0]); return y0 + float(x_offset) * (y1 - y0) / float(scales[1]); - }`;return{...ua,output:{dims:d,type:t[0].type,textureType:0},shaderSource:m,variables:[{name:"scales",type:"int",arrayLength:n.scales.length,data:n.scales.map(b=>Math.ceil(b))}]}},Ju=(e,t)=>{if(!e||t.opset<9&&e.length!==1||t.opset>=9&&t.opset<11&&e.length!==2||t.opset>=11&&e.length<2)throw new Error("invalid inputs.");if(t.scales.length>0&&e[0].dims.length!==t.scales.length)throw new Error("Invalid input shape.");if(e[0].type==="string")throw new Error("Invalid input tensor types.")},Eo=(e,t,n)=>{if(n){for(let o of e)if(o<=0)throw new Error("Scale value should be greater than 0.")}else for(let o of e)if(o<1)throw new Error("Scale value should be greater than or equal to 1.");if((t==="linear"||t==="cubic")&&e.length!==2&&(e.length!==4||e[0]!==1||e[1]!==1))throw new Error(`'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the ${n?"Resize":"Upsample"} opeartor.`)}}),io,Yu,G_,q_,bf,yf,wf,_f,kE=R(()=>{"use strict";qe(),Ie(),Yt(),An(),V_(),io={name:"Resize",inputNames:["A"],inputTypes:[2]},Yu=(e,t,n)=>(Ju(t,n),[e.run({...io,cacheHint:n.cacheKey,get:()=>bf(e,t,n)},t)]),G_=e=>xi(e,10),q_=e=>xi(e,11),bf=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),[u,l]=yf(t,n);if(u.every(y=>y===1)&&n.coordinateTransformMode!=="tf_crop_and_resize")return{...io,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:`void main() { - vec4 v = ${o.texture2D}(X, TexCoords); - ${o.output} = v; - }`};let d=l.length;if(d<2)throw new Error(`output dimension should be at least 2, but got ${d}`);let c=l[d-2],s=l[d-1],r=t[0].dims;if(d!==r.length)throw new Error(`output dimension should match input ${r.length}, but got ${d}`);let i=r[d-2],a=r[d-1],p=u[d-2],h=u[d-1],m="";if(n.mode!=="linear")throw new Error(`resize (packed) does not support mode: '${n.mode}'`);switch(n.coordinateTransformMode){case"asymmetric":m=` + }`;return{...pa,output:{dims:d,type:t[0].type,textureType:0},shaderSource:m,variables:[{name:"scales",type:"int",arrayLength:n.scales.length,data:n.scales.map(g=>Math.ceil(g))}]}},tl=(e,t)=>{if(!e||t.opset<9&&e.length!==1||t.opset>=9&&t.opset<11&&e.length!==2||t.opset>=11&&e.length<2)throw new Error("invalid inputs.");if(t.scales.length>0&&e[0].dims.length!==t.scales.length)throw new Error("Invalid input shape.");if(e[0].type==="string")throw new Error("Invalid input tensor types.")},No=(e,t,n)=>{if(n){for(let i of e)if(i<=0)throw new Error("Scale value should be greater than 0.")}else for(let i of e)if(i<1)throw new Error("Scale value should be greater than or equal to 1.");if((t==="linear"||t==="cubic")&&e.length!==2&&(e.length!==4||e[0]!==1||e[1]!==1))throw new Error(`'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the ${n?"Resize":"Upsample"} opeartor.`)}}),po,rl,Y_,J_,Tf,Sf,If,Of,UE=N(()=>{"use strict";Ke(),Pe(),sr(),Mn(),Z_(),po={name:"Resize",inputNames:["A"],inputTypes:[2]},rl=(e,t,n)=>(tl(t,n),[e.run({...po,cacheHint:n.cacheKey,get:()=>Tf(e,t,n)},t)]),Y_=e=>Ai(e,10),J_=e=>Ai(e,11),Tf=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),[u,l]=Sf(t,n);if(u.every(y=>y===1)&&n.coordinateTransformMode!=="tf_crop_and_resize")return{...po,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:`void main() { + vec4 v = ${i.texture2D}(X, TexCoords); + ${i.output} = v; + }`};let d=l.length;if(d<2)throw new Error(`output dimension should be at least 2, but got ${d}`);let c=l[d-2],s=l[d-1],r=t[0].dims;if(d!==r.length)throw new Error(`output dimension should match input ${r.length}, but got ${d}`);let o=r[d-2],a=r[d-1],p=u[d-2],h=u[d-1],m="";if(n.mode!=="linear")throw new Error(`resize (packed) does not support mode: '${n.mode}'`);switch(n.coordinateTransformMode){case"asymmetric":m=` vec4 getSourceFracIndex(ivec4 coords) { return vec4(coords) / scaleWHWH; } @@ -1215,21 +1215,21 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( vec4 getSourceFracIndex(ivec4 coords) { vec4 resized = vec4(${s}.0 - 1.0, ${c}.0 - 1.0, ${s}.0 - 1.0, ${c}.0 - 1.0); - vec4 original = vec4(${a}.0 - 1.0, ${i}.0 - 1.0, ${a}.0 - 1.0, - ${i}.0 - 1.0); + vec4 original = vec4(${a}.0 - 1.0, ${o}.0 - 1.0, ${a}.0 - 1.0, + ${o}.0 - 1.0); vec4 new_scale = original / resized; return vec4(coords) * new_scale; } - `;break;default:throw new Error(`resize (packed) does not support coordinateTransformMode: '${n.coordinateTransformMode}'`)}let b=_r(d),x=Ai(),w=` - const vec2 inputWH = vec2(${i}.0, ${a}.0); + `;break;default:throw new Error(`resize (packed) does not support coordinateTransformMode: '${n.coordinateTransformMode}'`)}let g=Ir(d),v=zi(),w=` + const vec2 inputWH = vec2(${o}.0, ${a}.0); const vec4 scaleWHWH = vec4(float(${p}), float(${h}), float(${p}), float(${h})); - ${x} + ${v} ${m} float getAValue(int x10, int r, int c, int d) { return getChannel(getA(x10, r, c, d), vec2(c, d)); } void main() { - ${b} rc = getOutputCoords(); + ${g} rc = getOutputCoords(); int batch = rc[0]; int depth = rc[1]; @@ -1285,20 +1285,20 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( vec4 bottom = mix(bottomLeft, bottomRight, clampFrac.ywyw); vec4 newValue = mix(top, bottom, clampFrac.xxzz); - ${o.output} = vec4(newValue); + ${i.output} = vec4(newValue); } - `;return{...io,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:w}},yf=(e,t)=>{let n=e[0].dims,o=t.scales,u;if(o.length===0){let d=e[t.scalesInputIdx];if(d&&d.size!==0){if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");o=wf(d,t.mode,t.isResize)}else{let c=e[t.sizesInputIdx];if(!c||c.size===0)throw new Error("Either scales or sizes MUST be provided as input.");u=Array.from(c.integerData),o=_f(u,n,t.mode,t.isResize)}}else if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");let l=u||n.map((d,c)=>Math.floor(d*o[c]));return[o,l]},wf=(e,t,n)=>{let o=Array.from(e.floatData);return Eo(o,t,n),o},_f=(e,t,n,o)=>{let u=t.length,l=new Array(u);for(let d=0,c=u;d{"use strict";En(),H_=(e,t)=>(vf(t),[new gt([t[0].dims.length],"int32",void 0,void 0,new Int32Array(t[0].dims))]),vf=e=>{if(!e||e.length!==1)throw new Error("Shape requires 1 input.")}}),oo,W_,K_,la,xf,X_,$f,Tf,CE=R(()=>{"use strict";nt(),Vo(),Pe(),Ie(),oo={name:"Slice",inputNames:["A"],inputTypes:[0]},W_=(e,t,n)=>(xf(t),[e.run({...oo,cacheHint:n.cacheKey,get:()=>la(e,t[0],n)},t)]),K_=e=>{let t=e.attributes.getInts("starts"),n=e.attributes.getInts("ends"),o=e.attributes.getInts("axes",[]);return Be({starts:t,ends:n,axes:o})},la=(e,t,n)=>{let o=n.axes.length===0?t.dims.slice(0).map((i,a)=>a):n.axes,u=he.normalizeAxes(o,t.dims.length),l=n.starts.map((i,a)=>i>t.dims[u[a]]-1?t.dims[u[a]]:he.normalizeAxis(i,t.dims[u[a]])),d=n.ends.map((i,a)=>i>t.dims[u[a]]-1?t.dims[u[a]]:he.normalizeAxis(i,t.dims[u[a]])),c=t.dims.slice(),s=[];for(let i=0;i0&&s.push(`outputIdx[${u[i]}] += ${l[i]};`);let r=` + `;return{...po,output:{dims:l,type:t[0].type,textureType:2},hasMain:!0,shaderSource:w}},Sf=(e,t)=>{let n=e[0].dims,i=t.scales,u;if(i.length===0){let d=e[t.scalesInputIdx];if(d&&d.size!==0){if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");i=If(d,t.mode,t.isResize)}else{let c=e[t.sizesInputIdx];if(!c||c.size===0)throw new Error("Either scales or sizes MUST be provided as input.");u=Array.from(c.integerData),i=Of(u,n,t.mode,t.isResize)}}else if(e[t.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");let l=u||n.map((d,c)=>Math.floor(d*i[c]));return[i,l]},If=(e,t,n)=>{let i=Array.from(e.floatData);return No(i,t,n),i},Of=(e,t,n,i)=>{let u=t.length,l=new Array(u);for(let d=0,c=u;d{"use strict";zn(),Q_=(e,t)=>(Ef(t),[new vt([t[0].dims.length],"int32",void 0,void 0,new Int32Array(t[0].dims))]),Ef=e=>{if(!e||e.length!==1)throw new Error("Shape requires 1 input.")}}),ho,ev,tv,ha,Af,rv,kf,Pf,GE=N(()=>{"use strict";ut(),Zo(),Ne(),Pe(),ho={name:"Slice",inputNames:["A"],inputTypes:[0]},ev=(e,t,n)=>(Af(t),[e.run({...ho,cacheHint:n.cacheKey,get:()=>ha(e,t[0],n)},t)]),tv=e=>{let t=e.attributes.getInts("starts"),n=e.attributes.getInts("ends"),i=e.attributes.getInts("axes",[]);return Le({starts:t,ends:n,axes:i})},ha=(e,t,n)=>{let i=n.axes.length===0?t.dims.slice(0).map((o,a)=>a):n.axes,u=ye.normalizeAxes(i,t.dims.length),l=n.starts.map((o,a)=>o>t.dims[u[a]]-1?t.dims[u[a]]:ye.normalizeAxis(o,t.dims[u[a]])),d=n.ends.map((o,a)=>o>t.dims[u[a]]-1?t.dims[u[a]]:ye.normalizeAxis(o,t.dims[u[a]])),c=t.dims.slice(),s=[];for(let o=0;o0&&s.push(`outputIdx[${u[o]}] += ${l[o]};`);let r=` float process(int outputIdx[${c.length}]) { ${s.join(` `)} return _A(outputIdx); - }`;return{...oo,output:{dims:c,type:t.type,textureType:0},shaderSource:r}},xf=e=>{if(!e||e.length!==1)throw new Error("Slice requires 1 input.");if(Pi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},X_=(e,t)=>{Tf(t);let n=$f(e,t);return[e.run({...oo,cacheHint:n.cacheKey,get:()=>la(e,t[0],n)},[t[0]])]},$f=(e,t)=>{if(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)||t.length>=4&&!e.session.isInitializer(t[3].dataId)||t.length>=5&&!e.session.isInitializer(t[4].dataId))throw new Error("dynamic slice attributes are not allowed");if(t.length>=5&&t[4].integerData.some(d=>d!==1))throw new Error("currently non-1 steps is not supported for Slice");let n=Array.from(t[1].integerData),o=Array.from(t[2].integerData),u=t.length>=4?Array.from(t[3].integerData):[],l=`${u};${n};${o}`;return{starts:n,ends:o,axes:u,cacheKey:l}},Tf=e=>{if(!e||e.length<3||e.length>5)throw new Error("Invalid input number.");if(e[1].type!=="int32"||e[1].dims.length!==1)throw new Error("Invalid input type.");if(e[2].type!=="int32"||e[2].dims.length!==1)throw new Error("Invalid input type.");if(e.length>=4&&(e[3].type!=="int32"||e[3].dims.length!==1))throw new Error("Invalid input type.");if(e.length>=5&&(e[4].type!=="int32"||e[4].dims.length!==1))throw new Error("Invalid input type.")}}),da,ca,pa,Z_,J_,Y_,Q_,ha,Sf,If,Of,fa,DE=R(()=>{"use strict";nt(),Pe(),qe(),Ie(),kl(),da={name:"SoftmaxComputeMax",inputNames:["A"],inputTypes:[0]},ca={name:"SoftmaxComputeScale",inputNames:["A","Max"],inputTypes:[0,0]},pa={name:"SoftMax",inputNames:["A","Max","Norm"],inputTypes:[0,0,0]},Z_=(e,t,n)=>{fa(t);let o=t[0].dims.slice(),u=he.normalizeAxis(n.axis,o.length),l=he.sizeToDimension(o,u),d=he.sizeFromDimension(o,u);return ha(e,t,n,l,d)},J_=e=>Be({axis:e.attributes.getInt("axis",1)}),Y_=e=>Be({axis:e.attributes.getInt("axis",-1)}),Q_=(e,t,n)=>{fa(t);let o=t[0].dims.slice(),u=he.normalizeAxis(n.axis,o.length),l=o.length,d=u!==l-1,c=[],s=[],r=[],i;d&&(s=Array.from({length:l}).map((m,b)=>b),s[u]=l-1,s[l-1]=u,s.map(m=>c.push(o[m])),i=Be({perm:s}),r=Ii(e,t,i));let a=d?he.sizeToDimension(c,l-1):he.sizeToDimension(o,l-1),p=d?he.sizeFromDimension(c,l-1):he.sizeFromDimension(o,l-1),h=ha(e,d?r:t,n,a,p);return d?Ii(e,h,i):h},ha=(e,t,n,o,u)=>{let l=Sf(e,t[0],o,u,[o]),d=e.run({...da,cacheHint:n.cacheKey,get:()=>l},t),c=If(e,t[0],o,u,l.output.dims,[o]),s=e.run({...ca,cacheHint:n.cacheKey,get:()=>c},[t[0],d]),r=Of(e,t[0],o,u,l.output.dims,c.output.dims);return[e.run({...pa,cacheHint:n.cacheKey,get:()=>r},[t[0],d,s])]},Sf=(e,t,n,o,u)=>{let[l,d]=e.calculateTextureWidthAndHeight(t.dims,0),c=u.length;if(n<1||o<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1)throw new Error("Dimensionality of the output should be 1");if(u[0]!==n)throw new Error("Shape of the output should be equal to logical row count");let s=Te(e.session.backend.glContext.version),r=` + }`;return{...ho,output:{dims:c,type:t.type,textureType:0},shaderSource:r}},Af=e=>{if(!e||e.length!==1)throw new Error("Slice requires 1 input.");if(Bi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.")},rv=(e,t)=>{Pf(t);let n=kf(e,t);return[e.run({...ho,cacheHint:n.cacheKey,get:()=>ha(e,t[0],n)},[t[0]])]},kf=(e,t)=>{if(!e.session.isInitializer(t[1].dataId)||!e.session.isInitializer(t[2].dataId)||t.length>=4&&!e.session.isInitializer(t[3].dataId)||t.length>=5&&!e.session.isInitializer(t[4].dataId))throw new Error("dynamic slice attributes are not allowed");if(t.length>=5&&t[4].integerData.some(d=>d!==1))throw new Error("currently non-1 steps is not supported for Slice");let n=Array.from(t[1].integerData),i=Array.from(t[2].integerData),u=t.length>=4?Array.from(t[3].integerData):[],l=`${u};${n};${i}`;return{starts:n,ends:i,axes:u,cacheKey:l}},Pf=e=>{if(!e||e.length<3||e.length>5)throw new Error("Invalid input number.");if(e[1].type!=="int32"||e[1].dims.length!==1)throw new Error("Invalid input type.");if(e[2].type!=="int32"||e[2].dims.length!==1)throw new Error("Invalid input type.");if(e.length>=4&&(e[3].type!=="int32"||e[3].dims.length!==1))throw new Error("Invalid input type.");if(e.length>=5&&(e[4].type!=="int32"||e[4].dims.length!==1))throw new Error("Invalid input type.")}}),fa,ma,ga,nv,iv,ov,sv,ba,Cf,Df,Rf,ya,qE=N(()=>{"use strict";ut(),Ne(),Ke(),Pe(),Rl(),fa={name:"SoftmaxComputeMax",inputNames:["A"],inputTypes:[0]},ma={name:"SoftmaxComputeScale",inputNames:["A","Max"],inputTypes:[0,0]},ga={name:"SoftMax",inputNames:["A","Max","Norm"],inputTypes:[0,0,0]},nv=(e,t,n)=>{ya(t);let i=t[0].dims.slice(),u=ye.normalizeAxis(n.axis,i.length),l=ye.sizeToDimension(i,u),d=ye.sizeFromDimension(i,u);return ba(e,t,n,l,d)},iv=e=>Le({axis:e.attributes.getInt("axis",1)}),ov=e=>Le({axis:e.attributes.getInt("axis",-1)}),sv=(e,t,n)=>{ya(t);let i=t[0].dims.slice(),u=ye.normalizeAxis(n.axis,i.length),l=i.length,d=u!==l-1,c=[],s=[],r=[],o;d&&(s=Array.from({length:l}).map((m,g)=>g),s[u]=l-1,s[l-1]=u,s.map(m=>c.push(i[m])),o=Le({perm:s}),r=Di(e,t,o));let a=d?ye.sizeToDimension(c,l-1):ye.sizeToDimension(i,l-1),p=d?ye.sizeFromDimension(c,l-1):ye.sizeFromDimension(i,l-1),h=ba(e,d?r:t,n,a,p);return d?Di(e,h,o):h},ba=(e,t,n,i,u)=>{let l=Cf(e,t[0],i,u,[i]),d=e.run({...fa,cacheHint:n.cacheKey,get:()=>l},t),c=Df(e,t[0],i,u,l.output.dims,[i]),s=e.run({...ma,cacheHint:n.cacheKey,get:()=>c},[t[0],d]),r=Rf(e,t[0],i,u,l.output.dims,c.output.dims);return[e.run({...ga,cacheHint:n.cacheKey,get:()=>r},[t[0],d,s])]},Cf=(e,t,n,i,u)=>{let[l,d]=e.calculateTextureWidthAndHeight(t.dims,0),c=u.length;if(n<1||i<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1)throw new Error("Dimensionality of the output should be 1");if(u[0]!==n)throw new Error("Shape of the output should be equal to logical row count");let s=Ae(e.session.backend.glContext.version),r=` float process(int[${c}] indices) { - int logical_row_start_offset = indices[0] * ${o}; + int logical_row_start_offset = indices[0] * ${i}; float max = getColorAsFloat(${s.texture2D}(A, offsetToCoords(logical_row_start_offset, ${l}, ${d} ))); - for(int i=1; i<${o}; ++i) + for(int i=1; i<${i}; ++i) { float current = getColorAsFloat(${s.texture2D}(A, offsetToCoords(logical_row_start_offset + i, ${l}, ${d}))); @@ -1307,20 +1307,20 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( } return max; - }`;return{...da,output:{dims:u,type:t.type,textureType:0},shaderSource:r}},If=(e,t,n,o,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=l.length;if(n<1||o<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(l.length!==1)throw new Error("Dimensionality of the output should be 1");if(l[0]!==n)throw new Error("Shape of the output should be equal to logical row count");if(u.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=Te(e.session.backend.glContext.version),i=` + }`;return{...fa,output:{dims:u,type:t.type,textureType:0},shaderSource:r}},Df=(e,t,n,i,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=l.length;if(n<1||i<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(l.length!==1)throw new Error("Dimensionality of the output should be 1");if(l[0]!==n)throw new Error("Shape of the output should be equal to logical row count");if(u.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=Ae(e.session.backend.glContext.version),o=` float process(int[${s}] indices) { - int logical_row_start_offset = indices[0] * ${o}; + int logical_row_start_offset = indices[0] * ${i}; float norm_factor = 0.0; float max = _Max(indices); - for(int i=0; i<${o}; ++i) + for(int i=0; i<${i}; ++i) { norm_factor += exp(getColorAsFloat(${r.texture2D}(A, offsetToCoords(logical_row_start_offset + i, ${d}, ${c}))) - max); } return norm_factor; - }`;return{...ca,output:{dims:l,type:t.type,textureType:0},shaderSource:i}},Of=(e,t,n,o,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=t.dims.length;if(n<1||o<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1||l.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n||l[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=` + }`;return{...ma,output:{dims:l,type:t.type,textureType:0},shaderSource:o}},Rf=(e,t,n,i,u,l)=>{let[d,c]=e.calculateTextureWidthAndHeight(t.dims,0),s=t.dims.length;if(n<1||i<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(u.length!==1||l.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(u[0]!==n||l[0]!==n)throw new Error("Shape of the intermediate results should be equal to logical row count");let r=` float process(int[${s}] indices) { // get offset of current logical tensor index from the 2-D texture coordinates (TexCoords) @@ -1328,7 +1328,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( //determine the logical row for this index int logical_row_index[1]; - logical_row_index[0] = offset / ${o}; + logical_row_index[0] = offset / ${i}; float norm_factor = _Norm(logical_row_index); @@ -1339,80 +1339,80 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( return 0.0; return exp(_A(indices) - _Max(logical_row_index)) / norm_factor; - }`;return{...pa,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:r}},fa=e=>{if(!e||e.length!==1)throw new Error("Softmax requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type")}}),ma,ev,tv,Ef,Af,kf,RE=R(()=>{"use strict";nt(),Pe(),Ie(),ma={name:"Split",inputNames:["A"],inputTypes:[0]},ev=(e,t,n)=>{kf(t);let o=he.normalizeAxis(n.axis,t[0].dims.length),u=Ef(e,t,o,n),l=[];for(let d=0;dAf(e,t[0],n,o,d)},t));return l},tv=e=>{let t=e.attributes.getInt("axis",0),n=e.attributes.getInts("split",[]),o=e.outputs.length;return Be({axis:t,split:n,numOutputs:o})},Ef=(e,t,n,o)=>{let[,u]=zu.splitShape(t[0].dims,n,o.split,o.numOutputs);return u.length},Af=(e,t,n,o,u)=>{let[l,d]=zu.splitShape(t.dims,o,n.split,n.numOutputs),c=d[u],s=l[u],r=` + }`;return{...ga,output:{dims:t.dims,type:t.type,textureType:0},shaderSource:r}},ya=e=>{if(!e||e.length!==1)throw new Error("Softmax requires 1 input.");if(e[0].type!=="float32"&&e[0].type!=="float64")throw new Error("Invalid input type")}}),wa,av,uv,Nf,zf,Mf,HE=N(()=>{"use strict";ut(),Ne(),Pe(),wa={name:"Split",inputNames:["A"],inputTypes:[0]},av=(e,t,n)=>{Mf(t);let i=ye.normalizeAxis(n.axis,t[0].dims.length),u=Nf(e,t,i,n),l=[];for(let d=0;dzf(e,t[0],n,i,d)},t));return l},uv=e=>{let t=e.attributes.getInt("axis",0),n=e.attributes.getInts("split",[]),i=e.outputs.length;return Le({axis:t,split:n,numOutputs:i})},Nf=(e,t,n,i)=>{let[,u]=ju.splitShape(t[0].dims,n,i.split,i.numOutputs);return u.length},zf=(e,t,n,i,u)=>{let[l,d]=ju.splitShape(t.dims,i,n.split,n.numOutputs),c=d[u],s=l[u],r=` float process(int indices[${s.length}]) { - indices[${o}] += ${c}; + indices[${i}] += ${c}; return _A(indices); } - `;return{...ma,cacheHint:`${n.cacheKey}:${u}`,output:{dims:s,type:t.type,textureType:0},shaderSource:r}},kf=e=>{if(!e||e.length!==1)throw new Error("Split requires one input.");if(e[0].type!=="int8"&&e[0].type!=="uint8"&&e[0].type!=="int16"&&e[0].type!=="uint16"&&e[0].type!=="int32"&&e[0].type!=="uint32"&&e[0].type!=="float32"&&e[0].type!=="float64"&&e[0].type!=="bool")throw new Error("Invalid input type.")}}),Qu,rv,nv,Pf,Cf,NE=R(()=>{"use strict";Pe(),Qu=(e,t,n)=>{Pf(t);let o=he.squeezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],o)]},rv=(e,t)=>(Cf(t),Qu(e,[t[0]],Array.from(t[1].integerData))),nv=e=>e.attributes.getInts("axes"),Pf=e=>{if(!e||e.length!==1)throw new Error("Squeeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},Cf=e=>{if(!e||e.length!==2)throw new Error("Squeeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),iv,Df,Rf,zE=R(()=>{"use strict";qe(),Ie(),iv=(e,t)=>{Rf(t);let n={name:"Sum",inputNames:t.map((o,u)=>`X${u}`),inputTypes:new Array(t.length).fill(0)};return[e.run({...n,get:()=>Df(e,t,n)},t)]},Df=(e,t,n)=>{let o=Te(e.session.backend.glContext.version),u=t[0].dims.slice(),l=` + `;return{...wa,cacheHint:`${n.cacheKey}:${u}`,output:{dims:s,type:t.type,textureType:0},shaderSource:r}},Mf=e=>{if(!e||e.length!==1)throw new Error("Split requires one input.");if(e[0].type!=="int8"&&e[0].type!=="uint8"&&e[0].type!=="int16"&&e[0].type!=="uint16"&&e[0].type!=="int32"&&e[0].type!=="uint32"&&e[0].type!=="float32"&&e[0].type!=="float64"&&e[0].type!=="bool")throw new Error("Invalid input type.")}}),nl,lv,dv,Bf,Ff,WE=N(()=>{"use strict";Ne(),nl=(e,t,n)=>{Bf(t);let i=ye.squeezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],i)]},lv=(e,t)=>(Ff(t),nl(e,[t[0]],Array.from(t[1].integerData))),dv=e=>e.attributes.getInts("axes"),Bf=e=>{if(!e||e.length!==1)throw new Error("Squeeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},Ff=e=>{if(!e||e.length!==2)throw new Error("Squeeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),cv,jf,Lf,KE=N(()=>{"use strict";Ke(),Pe(),cv=(e,t)=>{Lf(t);let n={name:"Sum",inputNames:t.map((i,u)=>`X${u}`),inputTypes:new Array(t.length).fill(0)};return[e.run({...n,get:()=>jf(e,t,n)},t)]},jf=(e,t,n)=>{let i=Ae(e.session.backend.glContext.version),u=t[0].dims.slice(),l=` void main() { - vec4 result = ${t.map((d,c)=>`${o.texture2D}(X${c},TexCoords)`).join(" + ")}; - ${o.output} = result; + vec4 result = ${t.map((d,c)=>`${i.texture2D}(X${c},TexCoords)`).join(" + ")}; + ${i.output} = result; } - `;return{...n,output:{dims:u,type:t[0].type,textureType:0},hasMain:!0,shaderSource:l}},Rf=e=>{if(!e||e.length===0)throw new Error("Sum requires inputs.");let t=e[0].dims.length;for(let n=1;n{"use strict";Vo(),Ie(),ov=(e,t)=>{zf(t);let n={name:"Tile",inputNames:["A"],inputTypes:[0]};return[e.run({...n,get:()=>Nf(e,t,n)},t)]},Nf=(e,t,n)=>{let o=t[0].dims.slice(),u=new Array(o.length),l=[];for(let s=0;s{if(!e||e.length===0)throw new Error("Sum requires inputs.");let t=e[0].dims.length;for(let n=1;n{"use strict";Zo(),Pe(),pv=(e,t)=>{Vf(t);let n={name:"Tile",inputNames:["A"],inputTypes:[0]};return[e.run({...n,get:()=>Uf(e,t,n)},t)]},Uf=(e,t,n)=>{let i=t[0].dims.slice(),u=new Array(i.length),l=[];for(let s=0;s{if(!e||e.length!==2)throw new Error("Tile requires 2 input.");if(e[1].dims.length!==1)throw new Error("The second input shape must 1 dimension.");if(e[1].dims[0]!==e[0].dims.length)throw new Error("Invalid input shape.");if(Pi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invalid repeat type.")}}),el,sv,av,Bf,Mf,ME=R(()=>{"use strict";Pe(),el=(e,t,n)=>{Bf(t);let o=he.unsqueezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],o)]},sv=(e,t)=>(Mf(t),el(e,[t[0]],Array.from(t[1].integerData))),av=e=>e.attributes.getInts("axes"),Bf=e=>{if(!e||e.length!==1)throw new Error("Unsqueeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},Mf=e=>{if(!e||e.length!==2)throw new Error("Unsqueeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),uv,FE=R(()=>{"use strict";IO(),jO(),LO(),VO(),Al(),gE(),bE(),yE(),wE(),_E(),vE(),xE(),SE(),Ol(),IE(),OE(),EE(),AE(),kE(),PE(),CE(),DE(),RE(),NE(),zE(),BE(),kl(),Ww(),ME(),V_(),uv=[["Abs","","6+",Tw],["Acos","","7+",Sw],["Add","","7+",iw],["And","","7+",ow],["Asin","","7+",Iw],["Atan","","7+",Ow],["AveragePool","","7+",I_,O_],["BatchNormalization","","7+",ew,tw],["Cast","","6+",gw,bw],["Ceil","","6+",kw],["Clip","","6-10",Vu,Ew],["Clip","","11+",Aw],["Concat","","4+",ww,_w],["Conv","","1+",Hu,Wu],["ConvTranspose","","1+",s_,a_],["Cos","","7+",Pw],["Div","","7+",sw],["Dropout","","7+",Gu],["DepthToSpace","","1+",l_,d_],["Equal","","7+",aw],["Elu","","6+",Cw,Dw],["Exp","","6+",Rw],["Flatten","","1+",c_,p_],["Floor","","6+",Nw],["FusedConv","com.microsoft","1+",Hu,Wu],["Gather","","1+",h_,f_],["Gemm","","7-10",Ku,m_],["Gemm","","11+",Ku,g_],["GlobalAveragePool","","1+",E_,A_],["GlobalMaxPool","","1+",C_],["Greater","","7+",uw],["Identity","","1+",Gu],["ImageScaler","","1+",b_,y_],["InstanceNormalization","","6+",w_,__],["LeakyRelu","","6+",zw,Bw],["Less","","7+",lw],["LRN","","1+",v_,x_],["Log","","6+",Mw],["MatMul","","1+",Yw,Qw],["MaxPool","","1+",k_,P_],["Mul","","7+",dw],["Neg","","6+",Fw],["Not","","1+",jw],["Or","","7+",cw],["Pad","","2-10",Xu,$_],["Pad","","11+",T_,S_],["Pow","","7+",pw],["PRelu","","7+",hw],["ReduceLogSum","","1+",M_,yr],["ReduceMax","","1+",N_,yr],["ReduceMean","","1+",R_,yr],["ReduceMin","","1+",z_,yr],["ReduceProd","","1+",B_,yr],["ReduceSum","","1-12",D_,yr],["ReduceSumSquare","","1+",F_,yr],["Relu","","6+",Lw],["Reshape","","5+",j_],["Resize","","10",Yu,G_],["Resize","","11+",Yu,q_],["Shape","","1+",H_],["Sigmoid","","6+",Uw],["Sin","","7+",Vw],["Slice","","10+",X_],["Slice","","1-9",W_,K_],["Softmax","","1-12",Z_,J_],["Softmax","","13+",Q_,Y_],["Split","","2-12",ev,tv],["Sqrt","","6+",Gw],["Squeeze","","1-12",Qu,nv],["Squeeze","","13+",rv],["Sub","","7+",fw],["Sum","","6+",iv],["Tan","","7+",qw],["Tanh","","6+",Hw],["Tile","","6+",ov],["Transpose","","1+",Ii,u_],["Upsample","","7-8",Zu,L_],["Upsample","","9",Zu,U_],["Unsqueeze","","1-12",el,av],["Unsqueeze","","13+",sv],["Xor","","7+",mw]]});function jE(e){let t={},n;for(;(n=tl.exec(e))!==null;){let o=n[3].split(",").map(u=>{let l=u.trim().split(" ");return l&&l.length===2?{type:l[0],name:l[1]}:null}).filter(u=>u!==null);t[n[2]]={params:o,body:n[4]}}for(let o in t){let u=lv.replace("__FUNC__",o),l=new RegExp(u,"gm");for(;(n=l.exec(e))!==null;){let d=n[1],c=n[2],s=n[3].split(","),r=d?`${d} ${c};`:"",i=t[o].body,a="";t[o].params.forEach((h,m)=>{h&&(a+=`${h.type} ${h.name} = ${s[m]}; -`)}),i=`${a} - ${i}`,i=i.replace("return",`${c} = `);let p=` + `;return{...n,output:{dims:u,type:t[0].type,textureType:0},shaderSource:c}},Vf=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 input.");if(e[1].dims.length!==1)throw new Error("The second input shape must 1 dimension.");if(e[1].dims[0]!==e[0].dims.length)throw new Error("Invalid input shape.");if(Bi.indexOf(e[0].type)===-1)throw new Error("Invalid input type.");if(e[1].type!=="int32"&&e[1].type!=="int16")throw new Error("Invalid repeat type.")}}),il,hv,fv,Gf,qf,ZE=N(()=>{"use strict";Ne(),il=(e,t,n)=>{Gf(t);let i=ye.unsqueezeShape(t[0].dims,n);return[e.reshapeUnpacked(t[0],i)]},hv=(e,t)=>(qf(t),il(e,[t[0]],Array.from(t[1].integerData))),fv=e=>e.attributes.getInts("axes"),Gf=e=>{if(!e||e.length!==1)throw new Error("Unsqueeze requires 1 input.");if(e[0].type==="string")throw new Error("invalid input tensor types.")},qf=e=>{if(!e||e.length!==2)throw new Error("Unsqueeze requires 2 inputs.");if(e[1].type!=="int32")throw new Error("Invalid input type.")}}),mv,YE=N(()=>{"use strict";BO(),JO(),QO(),tE(),Dl(),EE(),AE(),kE(),PE(),CE(),DE(),RE(),ME(),Pl(),BE(),FE(),jE(),LE(),UE(),VE(),GE(),qE(),HE(),WE(),KE(),XE(),Rl(),e_(),ZE(),Z_(),mv=[["Abs","","6+",Pw],["Acos","","7+",Cw],["Add","","7+",cw],["And","","7+",pw],["Asin","","7+",Dw],["Atan","","7+",Rw],["AveragePool","","7+",D_,R_],["BatchNormalization","","7+",aw,uw],["Cast","","6+",$w,Tw],["Ceil","","6+",Mw],["Clip","","6-10",Wu,Nw],["Clip","","11+",zw],["Concat","","4+",Iw,Ow],["Conv","","1+",Zu,Yu],["ConvTranspose","","1+",h_,f_],["Cos","","7+",Bw],["Div","","7+",hw],["Dropout","","7+",Ku],["DepthToSpace","","1+",g_,b_],["Equal","","7+",fw],["Elu","","6+",Fw,jw],["Exp","","6+",Lw],["Flatten","","1+",y_,w_],["Floor","","6+",Uw],["FusedConv","com.microsoft","1+",Zu,Yu],["Gather","","1+",__,v_],["Gemm","","7-10",Ju,x_],["Gemm","","11+",Ju,$_],["GlobalAveragePool","","1+",N_,z_],["GlobalMaxPool","","1+",F_],["Greater","","7+",mw],["Identity","","1+",Ku],["ImageScaler","","1+",T_,S_],["InstanceNormalization","","6+",I_,O_],["LeakyRelu","","6+",Vw,Gw],["Less","","7+",gw],["LRN","","1+",E_,A_],["Log","","6+",qw],["MatMul","","1+",o_,s_],["MaxPool","","1+",M_,B_],["Mul","","7+",bw],["Neg","","6+",Hw],["Not","","1+",Ww],["Or","","7+",yw],["Pad","","2-10",Qu,k_],["Pad","","11+",P_,C_],["Pow","","7+",ww],["PRelu","","7+",_w],["ReduceLogSum","","1+",q_,Tr],["ReduceMax","","1+",U_,Tr],["ReduceMean","","1+",L_,Tr],["ReduceMin","","1+",V_,Tr],["ReduceProd","","1+",G_,Tr],["ReduceSum","","1-12",j_,Tr],["ReduceSumSquare","","1+",H_,Tr],["Relu","","6+",Kw],["Reshape","","5+",W_],["Resize","","10",rl,Y_],["Resize","","11+",rl,J_],["Shape","","1+",Q_],["Sigmoid","","6+",Xw],["Sin","","7+",Zw],["Slice","","10+",rv],["Slice","","1-9",ev,tv],["Softmax","","1-12",nv,iv],["Softmax","","13+",sv,ov],["Split","","2-12",av,uv],["Sqrt","","6+",Yw],["Squeeze","","1-12",nl,dv],["Squeeze","","13+",lv],["Sub","","7+",vw],["Sum","","6+",cv],["Tan","","7+",Jw],["Tanh","","6+",Qw],["Tile","","6+",pv],["Transpose","","1+",Di,m_],["Upsample","","7-8",el,K_],["Upsample","","9",el,X_],["Unsqueeze","","1-12",il,fv],["Unsqueeze","","13+",hv],["Xor","","7+",xw]]});function JE(e){let t={},n;for(;(n=ol.exec(e))!==null;){let i=n[3].split(",").map(u=>{let l=u.trim().split(" ");return l&&l.length===2?{type:l[0],name:l[1]}:null}).filter(u=>u!==null);t[n[2]]={params:i,body:n[4]}}for(let i in t){let u=gv.replace("__FUNC__",i),l=new RegExp(u,"gm");for(;(n=l.exec(e))!==null;){let d=n[1],c=n[2],s=n[3].split(","),r=d?`${d} ${c};`:"",o=t[i].body,a="";t[i].params.forEach((h,m)=>{h&&(a+=`${h.type} ${h.name} = ${s[m]}; +`)}),o=`${a} + ${o}`,o=o.replace("return",`${c} = `);let p=` ${r} { - ${i} + ${o} } - `;e=e.replace(n[0],p)}}return e=e.replace(tl,""),e}var tl,lv,LE=R(()=>{"use strict";tl=/@inline[\s\n\r]+(\w+)[\s\n\r]+([0-9a-zA-Z_]+)\s*\(([^)]*)\)\s*{(([^}]|[\n\r])*)}/gm,lv="(\\w+)?\\s+([_0-9a-zA-Z]+)\\s+=\\s+__FUNC__\\((.*)\\)\\s*;"});function fi(e,t){let n=[],o=[],u=t!=null&&Array.isArray(t)&&t.length===0,l=t==null||u?null:UE(t,e).sort(),d=0;for(let c=0;cc)&&e[c]===1&&(n.push(e[c]),o.push(c)),l[d]<=c&&d++}e[c]!==1&&(n.push(e[c]),o.push(c))}return{newShape:n,keptDims:o}}function UE(e,t){let n=t.length;return e=e==null?t.map((o,u)=>u):[].concat(e),So(e.every(o=>o>=-n&&o`All values in axis param must be in range [-${n}, ${n}) but got axis ${e}`),So(e.every(VE),()=>`All values in axis param must be integers but got axis ${e}`),e.map(o=>o<0?n+o:o)}function VE(e){return e%1===0}function GE(e){if(e.length===0)return 1;let t=e[0];for(let n=1;n{"use strict";Bt(),Pe(),dv=class{constructor(e){this.maxTextureSize=e}computeTextureWH(e,t){let n=this.computeTexture(e,t);return t&&t.isPacked&&(n[0]/=2,n[1]/=2),t&&t.reverseWH?[n[1],n[0]]:n}computeTexture(e,t){let n=t&&t.isPacked;if(e.length===0)return n?[2,2]:[1,1];let o=this.maxTextureSize;if(t&&t.breakAxis!==void 0){let d=t.breakAxis>=e.length?1:e.slice(t.breakAxis).reduce((s,r)=>s*r),c=t.breakAxis<=0?1:e.slice(0,t.breakAxis).reduce((s,r)=>s*r);if(d>o||c>o)We.verbose("TextureLayout",`Given width/height preferences were unattainable: shape:${e}, breakAxis:${t.breakAxis}`);else return[d,c]}let u=e.slice(0);n&&(o=o*2,u=u.map((d,c)=>c>=u.length-2?u[c]%2===0?u[c]:u[c]+1:u[c]),u.length===1&&(u=[2,u[0]])),u.length!==2&&(u=fi(u).newShape);let l=GE(u);return u.length<=1&&l<=o?[1,l]:u.length===2&&u[0]<=o&&u[1]<=o?u:u.length===3&&u[0]*u[1]<=o&&u[2]<=o?[u[0]*u[1],u[2]]:u.length===3&&u[0]<=o&&u[1]*u[2]<=o?[u[0],u[1]*u[2]]:u.length===4&&u[0]*u[1]*u[2]<=o&&u[3]<=o?[u[0]*u[1]*u[2],u[3]]:u.length===4&&u[0]<=o&&u[1]*u[2]*u[3]<=o?[u[0],u[1]*u[2]*u[3]]:n?Ff(l/4).map(d=>d*2):Ff(l)}}}),pv,qE=R(()=>{"use strict";Pe(),xr(),qe(),cv(),Yt(),pv=class extends kn{constructor(e){super(e)}getFunctions(){return{...this.offsetToCoords(),...this.coordsToOffset(),...this.toVec(),...this.valueFrom(),...this.getCommonUtilFuncs(),...this.getInputsSamplingSnippets(),...this.getOutputSamplingSnippet()}}getCustomTypes(){return{}}offsetToCoords(){let e="offsetToCoords";return{offsetToCoords:new te(` + `;e=e.replace(n[0],p)}}return e=e.replace(ol,""),e}var ol,gv,QE=N(()=>{"use strict";ol=/@inline[\s\n\r]+(\w+)[\s\n\r]+([0-9a-zA-Z_]+)\s*\(([^)]*)\)\s*{(([^}]|[\n\r])*)}/gm,gv="(\\w+)?\\s+([_0-9a-zA-Z]+)\\s+=\\s+__FUNC__\\((.*)\\)\\s*;"});function vi(e,t){let n=[],i=[],u=t!=null&&Array.isArray(t)&&t.length===0,l=t==null||u?null:eA(t,e).sort(),d=0;for(let c=0;cc)&&e[c]===1&&(n.push(e[c]),i.push(c)),l[d]<=c&&d++}e[c]!==1&&(n.push(e[c]),i.push(c))}return{newShape:n,keptDims:i}}function eA(e,t){let n=t.length;return e=e==null?t.map((i,u)=>u):[].concat(e),Co(e.every(i=>i>=-n&&i`All values in axis param must be in range [-${n}, ${n}) but got axis ${e}`),Co(e.every(tA),()=>`All values in axis param must be integers but got axis ${e}`),e.map(i=>i<0?n+i:i)}function tA(e){return e%1===0}function rA(e){if(e.length===0)return 1;let t=e[0];for(let n=1;n{"use strict";Gt(),Ne(),bv=class{constructor(e){this.maxTextureSize=e}computeTextureWH(e,t){let n=this.computeTexture(e,t);return t&&t.isPacked&&(n[0]/=2,n[1]/=2),t&&t.reverseWH?[n[1],n[0]]:n}computeTexture(e,t){let n=t&&t.isPacked;if(e.length===0)return n?[2,2]:[1,1];let i=this.maxTextureSize;if(t&&t.breakAxis!==void 0){let d=t.breakAxis>=e.length?1:e.slice(t.breakAxis).reduce((s,r)=>s*r),c=t.breakAxis<=0?1:e.slice(0,t.breakAxis).reduce((s,r)=>s*r);if(d>i||c>i)Ye.verbose("TextureLayout",`Given width/height preferences were unattainable: shape:${e}, breakAxis:${t.breakAxis}`);else return[d,c]}let u=e.slice(0);n&&(i=i*2,u=u.map((d,c)=>c>=u.length-2?u[c]%2===0?u[c]:u[c]+1:u[c]),u.length===1&&(u=[2,u[0]])),u.length!==2&&(u=vi(u).newShape);let l=rA(u);return u.length<=1&&l<=i?[1,l]:u.length===2&&u[0]<=i&&u[1]<=i?u:u.length===3&&u[0]*u[1]<=i&&u[2]<=i?[u[0]*u[1],u[2]]:u.length===3&&u[0]<=i&&u[1]*u[2]<=i?[u[0],u[1]*u[2]]:u.length===4&&u[0]*u[1]*u[2]<=i&&u[3]<=i?[u[0]*u[1]*u[2],u[3]]:u.length===4&&u[0]<=i&&u[1]*u[2]*u[3]<=i?[u[0],u[1]*u[2]*u[3]]:n?Hf(l/4).map(d=>d*2):Hf(l)}}}),wv,nA=N(()=>{"use strict";Ne(),Er(),Ke(),yv(),sr(),wv=class extends Bn{constructor(e){super(e)}getFunctions(){return{...this.offsetToCoords(),...this.coordsToOffset(),...this.toVec(),...this.valueFrom(),...this.getCommonUtilFuncs(),...this.getInputsSamplingSnippets(),...this.getOutputSamplingSnippet()}}getCustomTypes(){return{}}offsetToCoords(){let e="offsetToCoords";return{offsetToCoords:new re(` vec2 ${e}(int offset, int width, int height) { int t = offset / width; int s = offset - t*width; vec2 coords = (vec2(s,t) + vec2(0.5,0.5)) / vec2(width, height); return coords; } - `)}}coordsToOffset(){let e="coordsToOffset";return{coordsToOffset:new te(` + `)}}coordsToOffset(){let e="coordsToOffset";return{coordsToOffset:new re(` int ${e}(vec2 coords, int width, int height) { float s = coords.s * float(width); float t = coords.t * float(height); int offset = int(t) * width + int(s); return offset; } - `)}}getOutputSamplingSnippet(){let e=this.context.outputTextureLayout;return e.isPacked?this.getPackedOutputSamplingSnippet(e):this.getUnpackedOutputSamplingSnippet(e)}getPackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],o={},u="getOutputCoords";switch(t.length){case 0:o[u]=this.getOutputScalarCoords();break;case 1:o[u]=this.getOutputPacked1DCoords(t,n);break;case 2:o[u]=this.getOutputPacked2DCoords(t,n);break;case 3:o[u]=this.getOutputPacked3DCoords(t,n);break;default:o[u]=this.getOutputPackedNDCoords(t,n)}let l=` + `)}}getOutputSamplingSnippet(){let e=this.context.outputTextureLayout;return e.isPacked?this.getPackedOutputSamplingSnippet(e):this.getUnpackedOutputSamplingSnippet(e)}getPackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],i={},u="getOutputCoords";switch(t.length){case 0:i[u]=this.getOutputScalarCoords();break;case 1:i[u]=this.getOutputPacked1DCoords(t,n);break;case 2:i[u]=this.getOutputPacked2DCoords(t,n);break;case 3:i[u]=this.getOutputPacked3DCoords(t,n);break;default:i[u]=this.getOutputPackedNDCoords(t,n)}let l=` void setOutput(vec4 val) { - ${Te(this.context.glContext.version).output} = val; + ${Ae(this.context.glContext.version).output} = val; } - `,d="floatTextureSetRGBA";return o[d]=new te(l),o}getUnpackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],o={},u="getOutputCoords";switch(t.length){case 0:o[u]=this.getOutputScalarCoords();break;case 1:o[u]=this.getOutputUnpacked1DCoords(t,n);break;case 2:o[u]=this.getOutputUnpacked2DCoords(t,n);break;case 3:o[u]=this.getOutputUnpacked3DCoords(t,n);break;case 4:o[u]=this.getOutputUnpacked4DCoords(t,n);break;case 5:o[u]=this.getOutputUnpacked5DCoords(t,n);break;case 6:o[u]=this.getOutputUnpacked6DCoords(t,n);break;default:throw new Error(`Unsupported output dimensionality: ${t.length}`)}let l=` + `,d="floatTextureSetRGBA";return i[d]=new re(l),i}getUnpackedOutputSamplingSnippet(e){let t=e.unpackedShape,n=[e.width,e.height],i={},u="getOutputCoords";switch(t.length){case 0:i[u]=this.getOutputScalarCoords();break;case 1:i[u]=this.getOutputUnpacked1DCoords(t,n);break;case 2:i[u]=this.getOutputUnpacked2DCoords(t,n);break;case 3:i[u]=this.getOutputUnpacked3DCoords(t,n);break;case 4:i[u]=this.getOutputUnpacked4DCoords(t,n);break;case 5:i[u]=this.getOutputUnpacked5DCoords(t,n);break;case 6:i[u]=this.getOutputUnpacked6DCoords(t,n);break;default:throw new Error(`Unsupported output dimensionality: ${t.length}`)}let l=` void setOutput(float val) { - ${Te(this.context.glContext.version).output} = vec4(val, 0, 0, 0); + ${Ae(this.context.glContext.version).output} = vec4(val, 0, 0, 0); } - `,d="floatTextureSetR";return o[d]=new te(l),o}getOutputScalarCoords(){return new te(` + `,d="floatTextureSetR";return i[d]=new re(l),i}getOutputScalarCoords(){return new re(` int getOutputCoords() { return 0; } - `)}getOutputPacked1DCoords(e,t){let n=t,o="";return n[0]===1?(o=` + `)}getOutputPacked1DCoords(e,t){let n=t,i="";return n[0]===1?(i=` int getOutputCoords() { return 2 * int(TexCoords.y * ${n[1]}.0); } - `,new te(o)):n[1]===1?(o=` + `,new re(i)):n[1]===1?(i=` int getOutputCoords() { return 2 * int(TexCoords.x * ${n[0]}.0); } - `,new te(o)):(o=` + `,new re(i)):(i=` int getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${n[0]}, ${n[1]})); return 2 * (resTexRC.y * ${n[0]} + resTexRC.x); } - `,new te(o))}getOutputPacked2DCoords(e,t){let n="";if(pi.arraysEqual(e,t))return n=` + `,new re(i))}getOutputPacked2DCoords(e,t){let n="";if(wi.arraysEqual(e,t))return n=` ivec2 getOutputCoords() { return 2 * ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); } - `,new te(n);let o=t,u=Math.ceil(e[1]/2);return n=` + `,new re(n);let i=t,u=Math.ceil(e[1]/2);return n=` ivec2 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * - vec2(${o[0]}, ${o[1]})); + vec2(${i[0]}, ${i[1]})); - int index = resTexRC.y * ${o[0]} + resTexRC.x; + int index = resTexRC.y * ${i[0]} + resTexRC.x; // reverse r and c order for packed texture int r = imod(index, ${u}) * 2; @@ -1420,7 +1420,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( return ivec2(r, c); } - `,new te(n)}getOutputPacked3DCoords(e,t){let n=[t[0],t[1]],o=Math.ceil(e[2]/2),u=o*Math.ceil(e[1]/2),l=` + `,new re(n)}getOutputPacked3DCoords(e,t){let n=[t[0],t[1]],i=Math.ceil(e[2]/2),u=i*Math.ceil(e[1]/2),l=` ivec3 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${n[0]}, ${n[1]})); @@ -1430,12 +1430,12 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( index -= b * ${u}; // reverse r and c order for packed texture - int r = imod(index, ${o}) * 2; - int c = 2 * (index / ${o}); + int r = imod(index, ${i}) * 2; + int c = 2 * (index / ${i}); return ivec3(b, r, c); } - `;return new te(l)}getOutputPackedNDCoords(e,t){let n=[t[0],t[1]],o=Math.ceil(e[e.length-1]/2),u=o*Math.ceil(e[e.length-2]/2),l=u,d="",c="b, r, c";for(let r=2;rlr( index -= b * ${u}; // reverse r and c order for packed texture - int r = imod(index, ${o}) * 2; - int c = 2 * (index / ${o}); + int r = imod(index, ${i}) * 2; + int c = 2 * (index / ${i}); return ivec${e.length}(${c}); } - `;return new te(s)}getOutputUnpacked1DCoords(e,t){let n=` + `;return new re(s)}getOutputUnpacked1DCoords(e,t){let n=` int getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); return resTexRC.y * ${t[0]} + resTexRC.x; } - `;return new te(n)}getOutputUnpacked2DCoords(e,t){let n=` + `;return new re(n)}getOutputUnpacked2DCoords(e,t){let n=` ivec2 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1470,7 +1470,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( int c = index - r * ${e[1]}; return ivec2(r, c); } - `;return new te(n)}getOutputUnpacked3DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `;return new re(n)}getOutputUnpacked3DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec3 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1478,7 +1478,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( ${d} return ivec3(r, c, d); } - `,new te(n)}getOutputUnpacked4DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `,new re(n)}getOutputUnpacked4DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec4 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1486,7 +1486,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( ${d} return ivec4(r, c, d, d2); } - `,new te(n)}getOutputUnpacked5DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `,new re(n)}getOutputUnpacked5DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec5 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1494,7 +1494,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( ${d} return ivec5(r, c, d, d2, d3); } - `,new te(n)}getOutputUnpacked6DCoords(e,t){let n="",o=e.length,u=null;o<2&&(u=[]),u=new Array(o-1),u[o-2]=e[o-1];for(let c=o-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3","d4"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,i=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${i};`}).join("");return n=` + `,new re(n)}getOutputUnpacked6DCoords(e,t){let n="",i=e.length,u=null;i<2&&(u=[]),u=new Array(i-1),u[i-2]=e[i-1];for(let c=i-3;c>=0;--c)u[c]=u[c+1]*e[c+1];let l=["r","c","d","d2","d3","d4"],d=u.map((c,s)=>{let r=`int ${l[s]} = index / ${c}`,o=s===u.length-1?`int ${l[s+1]} = index - ${l[s]} * ${c}`:`index -= ${l[s]} * ${c}`;return`${r}; ${o};`}).join("");return n=` ivec6 getOutputCoords() { ivec2 resTexRC = ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); @@ -1502,7 +1502,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( ${d} return ivec6(r, c, d, d2, d3, d4); } - `,new te(n)}getCommonUtilFuncs(){let e={},t="uvFromFlat";e[t]=new te(` + `,new re(n)}getCommonUtilFuncs(){let e={},t="uvFromFlat";e[t]=new re(` vec2 uvFromFlat(int texNumR, int texNumC, int index) { int texC = index / texNumR; int texR = index - texC * texNumR; @@ -1510,21 +1510,21 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( // v. return (vec2(texR, texC) + halfCR) / vec2(texNumR, texNumC); } - `),t="packedUVfrom1D",e[t]=new te(` + `),t="packedUVfrom1D",e[t]=new re(` vec2 packedUVfrom1D(int texNumR, int texNumC, int index) { int texelIndex = index / 2; int texR = texelIndex / texNumC; int texC = texelIndex - texR * texNumC; return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); } - `),t="packedUVfrom2D",e[t]=new te(` + `),t="packedUVfrom2D",e[t]=new re(` vec2 packedUVfrom2D(int texNumR, int texNumC, int texelsInLogicalRow, int row, int col) { int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2); int texR = texelIndex / texNumC; int texC = texelIndex - texR * texNumC; return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); } - `),t="packedUVfrom3D",e[t]=new te(` + `),t="packedUVfrom3D",e[t]=new re(` vec2 packedUVfrom3D(int texNumR, int texNumC, int texelsInBatch, int texelsInLogicalRow, int b, int row, int col) { @@ -1533,162 +1533,162 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( int texC = index - texR * texNumC; return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); } - `),t="sampleTexture";let n=Te(this.context.glContext.version);return e[t]=new te(` + `),t="sampleTexture";let n=Ae(this.context.glContext.version);return e[t]=new re(` float sampleTexture(sampler2D textureSampler, vec2 uv) { return ${n.texture2D}(textureSampler, uv).r; - }`),e}getInputsSamplingSnippets(){let e={},t=this.context.outputTextureLayout;return this.context.programInfo.inputNames.forEach((n,o)=>{let u=this.context.inputTextureLayouts[o],l=Ks(n);u.isPacked?e[l]=this.getPackedSamplerFromInput(l,n,u):e[l]=this.getUnpackedSamplerFromInput(l,n,u);let d=cO(n);u.unpackedShape.length<=t.unpackedShape.length&&(u.isPacked?e[d]=this.getPackedSamplerAtOutputCoords(d,u,t,n):e[d]=this.getUnpackedSamplerAtOutputCoords(d,u,t,n))}),e}getPackedSamplerAtOutputCoords(e,t,n,o){let u=t.unpackedShape,l=n.unpackedShape,d=Ks(o),c=u.length,s=l.length,r=qt.getBroadcastDims(u,l),i=_r(s),a=s-c,p,h=Sn();c===0?p="":s<2&&r.length>=1?p="coords = 0;":p=r.map(T=>`coords.${h[T+a]} = 0;`).join(` -`);let m="";s<2&&c>0?m="coords":m=u.map((T,I)=>`coords.${h[I+a]}`).join(", ");let b="return outputValue;",x=he.size(u)===1,w=he.size(l)===1;if(c===1&&!x&&!w)b=` + }`),e}getInputsSamplingSnippets(){let e={},t=this.context.outputTextureLayout;return this.context.programInfo.inputNames.forEach((n,i)=>{let u=this.context.inputTextureLayouts[i],l=Js(n);u.isPacked?e[l]=this.getPackedSamplerFromInput(l,n,u):e[l]=this.getUnpackedSamplerFromInput(l,n,u);let d=$O(n);u.unpackedShape.length<=t.unpackedShape.length&&(u.isPacked?e[d]=this.getPackedSamplerAtOutputCoords(d,u,t,n):e[d]=this.getUnpackedSamplerAtOutputCoords(d,u,t,n))}),e}getPackedSamplerAtOutputCoords(e,t,n,i){let u=t.unpackedShape,l=n.unpackedShape,d=Js(i),c=u.length,s=l.length,r=Qt.getBroadcastDims(u,l),o=Ir(s),a=s-c,p,h=Dn();c===0?p="":s<2&&r.length>=1?p="coords = 0;":p=r.map(S=>`coords.${h[S+a]} = 0;`).join(` +`);let m="";s<2&&c>0?m="coords":m=u.map((S,O)=>`coords.${h[O+a]}`).join(", ");let g="return outputValue;",v=ye.size(u)===1,w=ye.size(l)===1;if(c===1&&!v&&!w)g=` return vec4(outputValue.xy, outputValue.xy); - `;else if(x&&!w)s===1?b=` + `;else if(v&&!w)s===1?g=` return vec4(outputValue.x, outputValue.x, 0., 0.); - `:b=` + `:g=` return vec4(outputValue.x); - `;else if(r.length){let T=c-2,I=c-1;r.indexOf(T)>-1&&r.indexOf(I)>-1?b="return vec4(outputValue.x);":r.indexOf(T)>-1?b="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":r.indexOf(I)>-1&&(b="return vec4(outputValue.xx, outputValue.zz);")}let y=` + `;else if(r.length){let S=c-2,O=c-1;r.indexOf(S)>-1&&r.indexOf(O)>-1?g="return vec4(outputValue.x);":r.indexOf(S)>-1?g="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":r.indexOf(O)>-1&&(g="return vec4(outputValue.xx, outputValue.zz);")}let y=` int lastDim = coords.${h[s-1]}; coords.${h[s-1]} = coords.${h[s-2]}; coords.${h[s-2]} = lastDim; - `,S=` + `,T=` vec4 ${e}() { - ${i} coords = getOutputCoords(); + ${o} coords = getOutputCoords(); ${y} ${p} vec4 outputValue = ${d}(${m}); - ${b} + ${g} } - `;return new te(S,["coordinates.getOutputCoords"])}getUnpackedSamplerAtOutputCoords(e,t,n,o){let u=[n.width,n.height],l=[t.width,t.height],d=t.unpackedShape.length,c=n.unpackedShape.length,s=t.unpackedShape,r=n.unpackedShape,i=Ks(o);if(d===c&&pi.arraysEqual(l,u)){let y=` + `;return new re(T,["coordinates.getOutputCoords"])}getUnpackedSamplerAtOutputCoords(e,t,n,i){let u=[n.width,n.height],l=[t.width,t.height],d=t.unpackedShape.length,c=n.unpackedShape.length,s=t.unpackedShape,r=n.unpackedShape,o=Js(i);if(d===c&&wi.arraysEqual(l,u)){let y=` float ${e}() { - return sampleTexture(${o}, TexCoords); + return sampleTexture(${i}, TexCoords); } - `;return new te(y,["coordinates.sampleTexture"])}let a=_r(c),p=qt.getBroadcastDims(s,r),h=c-d,m,b=Sn();d===0?m="":c<2&&p.length>=1?m="coords = 0;":m=p.map(y=>`coords.${b[y+h]} = 0;`).join(` -`);let x="";c<2&&d>0?x="coords":x=t.unpackedShape.map((y,S)=>`coords.${b[S+h]}`).join(", ");let w=` + `;return new re(y,["coordinates.sampleTexture"])}let a=Ir(c),p=Qt.getBroadcastDims(s,r),h=c-d,m,g=Dn();d===0?m="":c<2&&p.length>=1?m="coords = 0;":m=p.map(y=>`coords.${g[y+h]} = 0;`).join(` +`);let v="";c<2&&d>0?v="coords":v=t.unpackedShape.map((y,T)=>`coords.${g[T+h]}`).join(", ");let w=` float ${e}() { ${a} coords = getOutputCoords(); ${m} - return ${i}(${x}); + return ${o}(${v}); } - `;return new te(w,["coordinates.getOutputCoords"])}getPackedSamplerFromInput(e,t,n){switch(n.unpackedShape.length){case 0:return this.getPackedSamplerScalar(e,t);case 1:return this.getPackedSampler1D(e,t,n);case 2:return this.getPackedSampler2D(e,t,n);case 3:return this.getPackedSampler3D(e,t,n);default:return this.getPackedSamplerND(e,t,n)}}getUnpackedSamplerFromInput(e,t,n){let o=n.unpackedShape;switch(o.length){case 0:return this.getUnpackedSamplerScalar(e,t,n);case 1:return this.getUnpackedSampler1D(e,t,n);case 2:return this.getUnpackedSampler2D(e,t,n);case 3:return this.getUnpackedSampler3D(e,t,n);case 4:return this.getUnpackedSampler4D(e,t,n);case 5:return this.getUnpackedSampler5D(e,t,n);case 6:return this.getUnpackedSampler6D(e,t,n);default:throw new Error(`Unsupported dimension ${o.length}-D`)}}getPackedSamplerScalar(e,t){let n=Te(this.context.glContext.version),o=` + `;return new re(w,["coordinates.getOutputCoords"])}getPackedSamplerFromInput(e,t,n){switch(n.unpackedShape.length){case 0:return this.getPackedSamplerScalar(e,t);case 1:return this.getPackedSampler1D(e,t,n);case 2:return this.getPackedSampler2D(e,t,n);case 3:return this.getPackedSampler3D(e,t,n);default:return this.getPackedSamplerND(e,t,n)}}getUnpackedSamplerFromInput(e,t,n){let i=n.unpackedShape;switch(i.length){case 0:return this.getUnpackedSamplerScalar(e,t,n);case 1:return this.getUnpackedSampler1D(e,t,n);case 2:return this.getUnpackedSampler2D(e,t,n);case 3:return this.getUnpackedSampler3D(e,t,n);case 4:return this.getUnpackedSampler4D(e,t,n);case 5:return this.getUnpackedSampler5D(e,t,n);case 6:return this.getUnpackedSampler6D(e,t,n);default:throw new Error(`Unsupported dimension ${i.length}-D`)}}getPackedSamplerScalar(e,t){let n=Ae(this.context.glContext.version),i=` vec4 ${e}() { return ${n.texture2D}(${t}, halfCR); } - `;return new te(o)}getPackedSampler1D(e,t,n){let o=[n.width,n.height],u=[o[1],o[0]],l=Te(this.context.glContext.version),d=`vec4 ${e}(int index) { + `;return new re(i)}getPackedSampler1D(e,t,n){let i=[n.width,n.height],u=[i[1],i[0]],l=Ae(this.context.glContext.version),d=`vec4 ${e}(int index) { vec2 uv = packedUVfrom1D( ${u[0]}, ${u[1]}, index); return ${l.texture2D}(${t}, uv); - }`;return new te(d,["coordinates.packedUVfrom1D"])}getPackedSampler2D(e,t,n){let o=n.unpackedShape,u=[n.width,n.height],l=Te(this.context.glContext.version),d=u[0],c=u[1];if(u!=null&&pi.arraysEqual(o,u)){let a=`vec4 ${e}(int row, int col) { + }`;return new re(d,["coordinates.packedUVfrom1D"])}getPackedSampler2D(e,t,n){let i=n.unpackedShape,u=[n.width,n.height],l=Ae(this.context.glContext.version),d=u[0],c=u[1];if(u!=null&&wi.arraysEqual(i,u)){let a=`vec4 ${e}(int row, int col) { vec2 uv = (vec2(col, row) + halfCR) / vec2(${c}.0, ${d}.0); return ${l.texture2D}(${t}, uv); - }`;return new te(a)}let s=u,r=Math.ceil(o[1]/2),i=`vec4 ${e}(int row, int col) { + }`;return new re(a)}let s=u,r=Math.ceil(i[1]/2),o=`vec4 ${e}(int row, int col) { vec2 uv = packedUVfrom2D(${s[1]}, ${s[0]}, ${r}, row, col); return ${l.texture2D}(${t}, uv); - }`;return new te(i,["coordinates.packedUVfrom2D"])}getPackedSampler3D(e,t,n){let o=n.unpackedShape,u=[n.width,n.height],l=[u[0],u[1]],d=Te(this.context.glContext.version);if(o[0]===1){let p=o.slice(1),h=[1,2],m=Zn(o,p),b=["b","row","col"],x=JSON.parse(JSON.stringify(n));x.unpackedShape=m;let w=this.getPackedSamplerFromInput(e,t,x),y=`${w.routineBody} + }`;return new re(o,["coordinates.packedUVfrom2D"])}getPackedSampler3D(e,t,n){let i=n.unpackedShape,u=[n.width,n.height],l=[u[0],u[1]],d=Ae(this.context.glContext.version);if(i[0]===1){let p=i.slice(1),h=[1,2],m=ni(i,p),g=["b","row","col"],v=JSON.parse(JSON.stringify(n));v.unpackedShape=m;let w=this.getPackedSamplerFromInput(e,t,v),y=`${w.routineBody} vec4 ${e}(int b, int row, int col) { - return ${e}(${Jn(b,h)}); - } `;return new te(y,w.dependencies)}let c=l[0],s=l[1],r=Math.ceil(o[2]/2),i=r*Math.ceil(o[1]/2),a=`vec4 ${e}(int b, int row, int col) { + return ${e}(${ii(g,h)}); + } `;return new re(y,w.dependencies)}let c=l[0],s=l[1],r=Math.ceil(i[2]/2),o=r*Math.ceil(i[1]/2),a=`vec4 ${e}(int b, int row, int col) { vec2 uv = packedUVfrom3D( - ${s}, ${c}, ${i}, ${r}, b, row, col); - return ${d.texture2D}(${t}, uv);}`;return new te(a,["coordinates.packedUVfrom3D"])}getPackedSamplerND(e,t,n){let o=n.unpackedShape,u=o.length,l=[n.width,n.height],d=Te(this.context.glContext.version),c=[l[0],l[1]],s=c[1],r=c[0],i=Math.ceil(o[u-1]/2),a=i*Math.ceil(o[u-2]/2),p="int b, int row, int col",h=`b * ${a} + (row / 2) * ${i} + (col / 2)`;for(let b=2;blr( vec2 uv = uvFromFlat(${a}, ${p}, index); return sampleTexture(${t}, uv); } - `;return new te(h,["coordinates.uvFromFlat","coordinates.sampleTexture","coordinates.coordsToOffset"])}toVec(){let e=this.context.outputTextureLayout,t=e.shape.length,n=e.strides,o=e.width,u=e.height,l=[];for(let c=0;c{let o=this.context.inputTextureLayouts[n],u=(o.unpackedShape.length>0?o.unpackedShape:o.shape).length,l=`_${t}`;e[l]=new te(this.getValueFromSingle(t,u,o.width,o.height,!1),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"]),l=l+"_T",e[l]=new te(this.getValueFromSingle(t,u,o.width,o.height,!0),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"])}),e}getValueFromSingle(e,t,n,o,u){let l=`_${e}`;u&&(l=l+"_T");let d=Te(this.context.glContext.version);return` + `;return{toVec:new re(d,["coordinates.coordsToOffset"])}}valueFrom(){let e={};return this.context.programInfo.inputNames.forEach((t,n)=>{let i=this.context.inputTextureLayouts[n],u=(i.unpackedShape.length>0?i.unpackedShape:i.shape).length,l=`_${t}`;e[l]=new re(this.getValueFromSingle(t,u,i.width,i.height,!1),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"]),l=l+"_T",e[l]=new re(this.getValueFromSingle(t,u,i.width,i.height,!0),[`shapeUtils.indicesToOffset${l}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"])}),e}getValueFromSingle(e,t,n,i,u){let l=`_${e}`;u&&(l=l+"_T");let d=Ae(this.context.glContext.version);return` float ${l}(int m[${t}]) { int offset = indicesToOffset${l}(m); - vec2 coords = offsetToCoords(offset, ${n}, ${o}); + vec2 coords = offsetToCoords(offset, ${n}, ${i}); float value = getColorAsFloat(${d.texture2D}(${e}, coords)); return value; } - `}getPackedValueFrom(e,t,n,o,u){let l=`_${e}_Pack`;u&&(l=l+"_T");let d=Te(this.context.glContext.version);return` + `}getPackedValueFrom(e,t,n,i,u){let l=`_${e}_Pack`;u&&(l=l+"_T");let d=Ae(this.context.glContext.version);return` vec4 ${l}(int m[${t}]) { int offset = indicesToOffset_${e}(m); - vec2 coords = offsetToCoords(offset, ${n}, ${o}); + vec2 coords = offsetToCoords(offset, ${n}, ${i}); return ${d.texture2D}(${e}, coords); } - `}}}),hv,HE=R(()=>{"use strict";xr(),hv=class rl extends kn{constructor(t){super(t)}getFunctions(){return{...this.encodeFloat32(),...this.decodeFloat32()}}getCustomTypes(){return{}}encodeFloat32(){return{encode:new te(`highp vec4 encode(highp float f) { + `}}}),_v,iA=N(()=>{"use strict";Er(),_v=class sl extends Bn{constructor(t){super(t)}getFunctions(){return{...this.encodeFloat32(),...this.decodeFloat32()}}getCustomTypes(){return{}}encodeFloat32(){return{encode:new re(`highp vec4 encode(highp float f) { return vec4(f, 0.0, 0.0, 0.0); } - `)}}decodeFloat32(){return{decode:new te(`highp float decode(highp vec4 rgba) { + `)}}decodeFloat32(){return{decode:new re(`highp float decode(highp vec4 rgba) { return rgba.r; } - `)}}encodeUint8(){let t=rl.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{encode:new te(` + `)}}encodeUint8(){let t=sl.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{encode:new re(` highp vec4 encode(highp float f) { highp float F = abs(f); highp float Sign = step(0.0,-f); @@ -1742,7 +1742,7 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( rgba = rgba / 255.0; // values need to be normalized to [0,1] return rgba; } - `)}}decodeUint8(){let t=rl.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{decode:new te(` + `)}}decodeUint8(){let t=sl.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{decode:new re(` highp float decode(highp vec4 rgba) { rgba = rgba * 255.0; // values need to be de-normalized from [0,1] to [0,255] ${t} @@ -1752,44 +1752,44 @@ value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),B_=(e,t,n)=>lr( highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); return Result; } - `)}}static isLittleEndian(){let t=new ArrayBuffer(4),n=new Uint32Array(t),o=new Uint8Array(t);if(n[0]=3735928559,o[0]===239)return!0;if(o[0]===222)return!1;throw new Error("unknown endianness")}}}),fv,WE=R(()=>{"use strict";xr(),qe(),fv=class extends kn{constructor(e){super(e)}getFunctions(){return{...this.setFragColor(),...this.getColorAsFloat()}}getCustomTypes(){return{}}setFragColor(){let e=Te(this.context.glContext.version);return{setFragColor:new te(` + `)}}static isLittleEndian(){let t=new ArrayBuffer(4),n=new Uint32Array(t),i=new Uint8Array(t);if(n[0]=3735928559,i[0]===239)return!0;if(i[0]===222)return!1;throw new Error("unknown endianness")}}}),vv,oA=N(()=>{"use strict";Er(),Ke(),vv=class extends Bn{constructor(e){super(e)}getFunctions(){return{...this.setFragColor(),...this.getColorAsFloat()}}getCustomTypes(){return{}}setFragColor(){let e=Ae(this.context.glContext.version);return{setFragColor:new re(` void setFragColor(float value) { ${e.output} = encode(value); } - `,["encoding.encode"])}}getColorAsFloat(){return{getColorAsFloat:new te(` + `,["encoding.encode"])}}getColorAsFloat(){return{getColorAsFloat:new re(` float getColorAsFloat(vec4 color) { return decode(color); } - `,["encoding.decode"])}}}}),mv,KE=R(()=>{"use strict";xr(),mv=class mi extends kn{constructor(t){super(t)}getFunctions(){return{...this.bcastIndex(),...this.bcastMatmulIndex(),...this.offsetToIndices(),...this.indicesToOffset(),...this.incrementIndices()}}getCustomTypes(){return{}}bcastIndex(){let t=this.context.outputTextureLayout.shape.length,n={};return this.context.programInfo.inputNames.forEach((o,u)=>{let l=this.context.inputTextureLayouts[u].unpackedShape;if(l.length<=t){let d=l.length,c=t-d,s=`bcastIndices_${o}`,r="";for(let a=0;a{"use strict";Er(),xv=class xi extends Bn{constructor(t){super(t)}getFunctions(){return{...this.bcastIndex(),...this.bcastMatmulIndex(),...this.offsetToIndices(),...this.indicesToOffset(),...this.incrementIndices()}}getCustomTypes(){return{}}bcastIndex(){let t=this.context.outputTextureLayout.shape.length,n={};return this.context.programInfo.inputNames.forEach((i,u)=>{let l=this.context.inputTextureLayouts[u].unpackedShape;if(l.length<=t){let d=l.length,c=t-d,s=`bcastIndices_${i}`,r="";for(let a=0;a{let l=this.context.inputTextureLayouts[u].shape;if(!(l.length<2||l.length>t)){let d=l.length,c=t-d,s=`bcastMatmulIndices_${o}`,r="";for(let a=0;a{let l=this.context.inputTextureLayouts[u].shape;if(!(l.length<2||l.length>t)){let d=l.length,c=t-d,s=`bcastMatmulIndices_${i}`,r="";for(let a=0;a{let u=this.context.inputTextureLayouts[o].shape,l=this.context.inputTextureLayouts[o].strides,d=u.length,c=`indicesToOffset_${n}`;t[c]=new te(mi.indexToOffsetSingle(c,d,l)),c=`indicesToOffset_${n}_T`,t[c]=new te(mi.indexToOffsetSingle(c,d,l.slice().reverse()))}),t}static indexToOffsetSingle(t,n,o){let u="";for(let l=n-1;l>=0;--l)u+=` - offset += indices[${l}] * ${o[l]}; + `;n[s]=new re(o)}}),n}indicesToOffset(){let t={};return this.context.programInfo.inputNames.forEach((n,i)=>{let u=this.context.inputTextureLayouts[i].shape,l=this.context.inputTextureLayouts[i].strides,d=u.length,c=`indicesToOffset_${n}`;t[c]=new re(xi.indexToOffsetSingle(c,d,l)),c=`indicesToOffset_${n}_T`,t[c]=new re(xi.indexToOffsetSingle(c,d,l.slice().reverse()))}),t}static indexToOffsetSingle(t,n,i){let u="";for(let l=n-1;l>=0;--l)u+=` + offset += indices[${l}] * ${i[l]}; `;return` int ${t}(int indices[${n}]) { int offset = 0; ${u} return offset; } - `}offsetToIndices(){let t={};return this.context.programInfo.inputNames.forEach((n,o)=>{let u=this.context.inputTextureLayouts[o].shape,l=this.context.inputTextureLayouts[o].strides,d=u.length,c=`offsetToIndices_${n}`;t[c]=new te(mi.offsetToIndicesSingle(c,d,l)),c=`offsetToIndices_${n}_T`,t[c]=new te(mi.offsetToIndicesSingle(c,d,l.slice().reverse()))}),t}static offsetToIndicesSingle(t,n,o){let u=[];for(let l=0;l{let u=this.context.inputTextureLayouts[i].shape,l=this.context.inputTextureLayouts[i].strides,d=u.length,c=`offsetToIndices_${n}`;t[c]=new re(xi.offsetToIndicesSingle(c,d,l)),c=`offsetToIndices_${n}_T`,t[c]=new re(xi.offsetToIndicesSingle(c,d,l.slice().reverse()))}),t}static offsetToIndicesSingle(t,n,i){let u=[];for(let l=0;l{let u=this.context.inputTextureLayouts[o].shape,l=u.length,d=`incrementIndices_${n}`,c="";for(let r=0;r{let u=this.context.inputTextureLayouts[i].shape,l=u.length,d=`incrementIndices_${n}`,c="";for(let r=0;rlr( indices[i] = 0; } } - `;t[d]=new te(s)}),t}}}),gv,XE=R(()=>{"use strict";xr(),gv=class extends kn{constructor(e){super(e)}getCustomTypes(){return{}}getFunctions(){return{...this.binaryVecFunctions(),...this.copyVec(),...this.setVecItem(),...this.getVecItem()}}binaryVecFunctions(){let e=this.context.outputTextureLayout.shape.length,t={add:"+=",sub:"-=",mul:"*=",div:"/="},n={};for(let o in t){let u=`${o}Vec`,l="";for(let c=0;c{"use strict";Er(),$v=class extends Bn{constructor(e){super(e)}getCustomTypes(){return{}}getFunctions(){return{...this.binaryVecFunctions(),...this.copyVec(),...this.setVecItem(),...this.getVecItem()}}binaryVecFunctions(){let e=this.context.outputTextureLayout.shape.length,t={add:"+=",sub:"-=",mul:"*=",div:"/="},n={};for(let i in t){let u=`${i}Vec`,l="";for(let c=0;clr( void setVecItem(out int m[${e}], int index, int value) { ${t} } - `;return{setVecItem:new te(n)}}getVecItem(){let e=this.context.outputTextureLayout.shape.length,t=` + `;return{setVecItem:new re(n)}}getVecItem(){let e=this.context.outputTextureLayout.shape.length,t=` if(index < 0) index = ${e} + index; if (index == 0) return m[0]; - `;for(let o=1;olr( int getVecItem(int m[${e}], int index) { ${t} } - `;return{getVecItem:new te(n)}}}}),nl,ZE=R(()=>{"use strict";qE(),HE(),WE(),KE(),XE(),nl={encoding:hv,fragcolor:fv,vec:gv,shapeUtils:mv,coordinates:pv}}),bv,JE=R(()=>{"use strict";xr(),LE(),ZE(),qe(),bv=class{constructor(e,t,n,o){this.libs={},this.glslLibRoutineDependencyGraph={},this.context=new rw(e,t,n,o),Object.keys(nl).forEach(l=>{let d=new nl[l](this.context);this.libs[l]=d});let u=this.glslLibRoutineDependencyGraph;for(let l in this.libs){let d=this.libs[l].getFunctions();for(let c in d){let s=l+"."+c,r;u[s]?(r=u[s],r.routineBody=d[c].routineBody):(r=new Uu(s,d[c].routineBody),u[s]=r);let i=d[c].dependencies;if(i)for(let a=0;a{"use strict";nA(),iA(),oA(),sA(),aA(),al={encoding:_v,fragcolor:vv,vec:$v,shapeUtils:xv,coordinates:wv}}),Tv,lA=N(()=>{"use strict";Er(),QE(),uA(),Ke(),Tv=class{constructor(e,t,n,i){this.libs={},this.glslLibRoutineDependencyGraph={},this.context=new lw(e,t,n,i),Object.keys(al).forEach(l=>{let d=new al[l](this.context);this.libs[l]=d});let u=this.glslLibRoutineDependencyGraph;for(let l in this.libs){let d=this.libs[l].getFunctions();for(let c in d){let s=l+"."+c,r;u[s]?(r=u[s],r.routineBody=d[c].routineBody):(r=new Hu(s,d[c].routineBody),u[s]=r);let o=d[c].dependencies;if(o)for(let a=0;a{let o=n.split(".")[1];e.indexOf(o)!==-1&&t.push(this.glslLibRoutineDependencyGraph[n])}),nw.returnOrderedNodes(t)}getUniforms(e,t){let n=[];if(e)for(let o of e)n.push(`uniform sampler2D ${o};`);if(t)for(let o of t)n.push(`uniform ${o.type} ${o.name}${o.arrayLength?`[${o.arrayLength}]`:""};`);return n.join(` -`)}}}),yv,YE=R(()=>{"use strict";ot(),Bt(),JE(),qe(),yv=class{constructor(e,t,n){this.profiler=e,this.glContext=t,this.textureLayoutStrategy=n,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n){this.profiler.event("op",`ProgramManager.run ${e.programInfo.name??"unknown kernel"}`,()=>{let o=this.glContext.gl,u=e.program;o.useProgram(u);try{this.bindOutput(n),this.attributesBound||this.bindAttributes(e.attribLocations),this.bindUniforms(e.uniformLocations,e.programInfo.variables??[],t)}catch(l){throw We.error("ProgramManager",e.programInfo.shaderSource),l}this.profiler.event("backend","GlContext.draw()",()=>{this.glContext.draw()})},this.glContext)}dispose(){this.vertexShader&&this.glContext.deleteShader(this.vertexShader),this.repo.forEach(e=>this.glContext.deleteProgram(e.program))}build(e,t,n){return this.profiler.event("backend","ProgramManager.build",()=>{let o=new bv(this.glContext,e,t,n),u=o.preprocess(),l=this.compile(u);return{programInfo:e,program:l,uniformLocations:this.getUniformLocations(l,o.context.programInfo.inputNames,o.context.programInfo.variables),attribLocations:this.getAttribLocations(l)}})}compile(e){if(!this.vertexShader){We.verbose("ProrgramManager","Compiling and caching Vertex shader for the first time");let o=uO(this.glContext.version);this.vertexShader=this.glContext.compileShader(o,this.glContext.gl.VERTEX_SHADER)}le.debug&&We.verbose("ProrgramManager",`FragShader: + ${t}`}getImports(e){let t=this.selectGlslLibRoutinesToBeIncluded(e);if(t.length===0)return"";let n="";for(let i=0;i{let i=n.split(".")[1];e.indexOf(i)!==-1&&t.push(this.glslLibRoutineDependencyGraph[n])}),dw.returnOrderedNodes(t)}getUniforms(e,t){let n=[];if(e)for(let i of e)n.push(`uniform sampler2D ${i};`);if(t)for(let i of t)n.push(`uniform ${i.type} ${i.name}${i.arrayLength?`[${i.arrayLength}]`:""};`);return n.join(` +`)}}}),Sv,dA=N(()=>{"use strict";dt(),Gt(),lA(),Ke(),Sv=class{constructor(e,t,n){this.profiler=e,this.glContext=t,this.textureLayoutStrategy=n,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n){this.profiler.event("op",`ProgramManager.run ${e.programInfo.name??"unknown kernel"}`,()=>{let i=this.glContext.gl,u=e.program;i.useProgram(u);try{this.bindOutput(n),this.attributesBound||this.bindAttributes(e.attribLocations),this.bindUniforms(e.uniformLocations,e.programInfo.variables??[],t)}catch(l){throw Ye.error("ProgramManager",e.programInfo.shaderSource),l}this.profiler.event("backend","GlContext.draw()",()=>{this.glContext.draw()})},this.glContext)}dispose(){this.vertexShader&&this.glContext.deleteShader(this.vertexShader),this.repo.forEach(e=>this.glContext.deleteProgram(e.program))}build(e,t,n){return this.profiler.event("backend","ProgramManager.build",()=>{let i=new Tv(this.glContext,e,t,n),u=i.preprocess(),l=this.compile(u);return{programInfo:e,program:l,uniformLocations:this.getUniformLocations(l,i.context.programInfo.inputNames,i.context.programInfo.variables),attribLocations:this.getAttribLocations(l)}})}compile(e){if(!this.vertexShader){Ye.verbose("ProrgramManager","Compiling and caching Vertex shader for the first time");let i=_O(this.glContext.version);this.vertexShader=this.glContext.compileShader(i,this.glContext.gl.VERTEX_SHADER)}me.debug&&Ye.verbose("ProrgramManager",`FragShader: ${e} -`);let t=this.glContext.compileShader(e,this.glContext.gl.FRAGMENT_SHADER),n=this.glContext.createProgram(this.vertexShader,t);return this.glContext.deleteShader(t),n}bindOutput(e){let t=e.width,n=e.height;We.verbose("ProrgramManager",`Binding output texture to Framebuffer: w/h=${t}/${n}, shape=${e.shape}, type=${e.tensor.type}`),this.glContext.attachFramebuffer(e.texture,t,n)}bindAttributes(e){let t=e.position,n=e.textureCoord;this.glContext.setVertexAttributes(t,n),this.attributesBound=!0}bindUniforms(e,t,n){let o=this.glContext.gl,u=0;for(let{name:l,type:d,location:c,arrayLength:s}of e){let r=t.find(i=>i.name===l)?.data;if(d!=="sampler2D"&&!r)throw new Error(`variable '${l}' does not have data defined in program info`);switch(d){case"sampler2D":this.bindTexture(n[u],c,u),u++;break;case"float":s?o.uniform1fv(c,r):o.uniform1f(c,r);break;case"int":s?o.uniform1iv(c,r):o.uniform1i(c,r);break;default:throw new Error(`Uniform not implemented: ${d}`)}}}bindTexture(e,t,n){this.glContext.bindTextureToUniform(e.texture,n,t)}getAttribLocations(e){return{position:this.getAttribLocation(e,"position"),textureCoord:this.getAttribLocation(e,"textureCoord")}}getUniformLocations(e,t,n){let o=[];if(t)for(let u of t)o.push({name:u,type:"sampler2D",location:this.getUniformLocation(e,u)});if(n)for(let u of n)o.push({...u,location:this.getUniformLocation(e,u.name)});return o}getUniformLocation(e,t){let n=this.glContext.gl.getUniformLocation(e,t);if(n===null)throw new Error(`Uniform ${t} not found.`);return n}getAttribLocation(e,t){return this.glContext.gl.getAttribLocation(e,t)}}}),wv,QE=R(()=>{"use strict";Bt(),Oo(),wv=class{constructor(e,t,n,o){this.glContext=e,this.layoutStrategy=t,this.profiler=n,this.config=o,this.pendingRead=new Map,o.reuseTextures&&(this.inUseTextures=new Map,this.idleTextures=new Map,this.textureLookup=new Map)}createTextureFromLayout(e,t,n,o){let u=this.toEncoderType(e),l=this.glContext.getEncoder(u,t.channels||1,o);if(t.isPacked&&o===1)throw new Error("not implemented");let d=t.width,c=t.height,s,r;if(this.config.reuseTextures){s=`${d}x${c}_${l.format}_${l.internalFormat}_${l.textureType}`,r=this.inUseTextures.get(s),r||(r=[],this.inUseTextures.set(s,r));let a=this.idleTextures.get(s);if(a&&a.length>0){let p=a.pop();return r.push(p),o===1&&this.glContext.updateTexture(p,d,c,l,this.toTextureData(e,n)),p}}We.verbose("TextureManager",`Creating new texture of size ${t.width}x${t.height}`);let i=this.glContext.allocateTexture(d,c,l,this.toTextureData(e,n));return this.config.reuseTextures&&(r.push(i),this.textureLookup.set(i,s)),i}readTexture(e,t,n){return n||(n=1),this.profiler.event("backend","TextureManager.readTexture",()=>{let o=e.shape.reduce((l,d)=>l*d)*n,u=this.glContext.readTexture(e.texture,e.width,e.height,o,this.toEncoderType(t),n);return this.toTensorData(t,u)})}async readTextureAsync(e,t,n){let o=e.tensor.dataId;if(n||(n=1),this.pendingRead.has(o)){let u=this.pendingRead.get(o);return new Promise(l=>u?.push(l))}return this.profiler.event("backend","TextureManager.readTextureAsync",async()=>{this.pendingRead.set(o,[]);let u=e.shape.reduce((s,r)=>s*r)*n;await this.glContext.createAndWaitForFence();let l=this.glContext.readTexture(e.texture,e.width,e.height,u,this.toEncoderType(t),n),d=this.toTensorData(t,l),c=this.pendingRead.get(o);return this.pendingRead.delete(o),c?.forEach(s=>s(d)),d})}readUint8TextureAsFloat(e){return this.profiler.event("backend","TextureManager.readUint8TextureAsFloat",()=>{let t=e.shape.reduce((o,u)=>o*u),n=this.glContext.readTexture(e.texture,e.width,e.height,t*4,"byte",4);return new Float32Array(n.buffer,n.byteOffset,t)})}releaseTexture(e,t){let n;if(this.config.reuseTextures&&(n=this.textureLookup.get(e.texture),n)){t&&this.textureLookup.delete(n);let o=this.inUseTextures.get(n);if(o){let u=o.indexOf(e.texture);if(u!==-1){o.splice(u,1);let l=this.idleTextures.get(n);l||(l=[],this.idleTextures.set(n,l)),l.push(e.texture)}}}(!n||t)&&(We.verbose("TextureManager",`Deleting texture of size ${e.width}x${e.height}`),this.glContext.deleteTexture(e.texture))}toTensorData(e,t){switch(e){case"int16":return t instanceof Int16Array?t:Int16Array.from(t);case"int32":return t instanceof Int32Array?t:Int32Array.from(t);case"int8":return t instanceof Int8Array?t:Int8Array.from(t);case"uint16":return t instanceof Uint16Array?t:Uint16Array.from(t);case"uint32":return t instanceof Uint32Array?t:Uint32Array.from(t);case"uint8":case"bool":return t instanceof Uint8Array?t:Uint8Array.from(t);case"float32":return t instanceof Float32Array?t:Float32Array.from(t);case"float64":return t instanceof Float64Array?t:Float64Array.from(t);default:throw new Error(`TensorData type ${e} is not supported`)}}toTextureData(e,t){if(t)return t instanceof Float32Array?t:new Float32Array(t)}toEncoderType(e){return"float"}clearActiveTextures(){this.glContext.clearActiveTextures()}}}),_v,eA=R(()=>{"use strict";Bt(),l3(),SO(),FE(),YE(),cv(),QE(),_v=class{constructor(e,t){this.backend=e,this.context=t,this.layoutStrategy=new dv(e.glContext.maxTextureSize),this.programManager=new yv(this.context.profiler,e.glContext,this.layoutStrategy),this.textureManager=new wv(e.glContext,this.layoutStrategy,this.context.profiler,{reuseTextures:e.textureCacheMode==="full"}),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map,this.pack=e.pack,this.pack2unpackMap=new Map,this.unpack2packMap=new Map}createInferenceHandler(){return new Q0(this)}onGraphInitialized(e){let t=e.getValues().filter(n=>n.from===-1&&n.tensor).map(n=>n.tensor.dataId);this.initializers=new Set(t)}isInitializer(e){return this.initializers?this.initializers.has(e):!1}addInitializer(e){this.initializers.add(e)}getTextureData(e,t){return t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){We.verbose("WebGLSessionHandler","Storing Texture data in cache"),n?this.packedTextureDataCache.set(e,t):this.unpackedTextureDataCache.set(e,t)}dispose(){this.programManager.dispose(),this.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.unpackedTextureDataCache=new Map}resolve(e,t,n){let o=a3(e,t,uv);return{impl:o.opImpl,context:o.opInit?o.opInit(e,n):e}}}});function tA(e){let t=0;for(;t{"use strict";ot(),Oo(),Oo(),Yt(),il=class{constructor(e,t){this.frameBufferBound=!1,this.itemsToPoll=[],this.gl=e,this.version=t,this.getExtensions(),this.vertexbuffer=this.createVertexbuffer(),this.framebuffer=this.createFramebuffer(),this.queryVitalParameters()}allocateTexture(e,t,n,o){let u=this.gl,l=u.createTexture();u.bindTexture(u.TEXTURE_2D,l),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MIN_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MAG_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_S,u.CLAMP_TO_EDGE),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_T,u.CLAMP_TO_EDGE);let d=o?n.encode(o,e*t):null;return u.texImage2D(u.TEXTURE_2D,0,n.internalFormat,e,t,0,n.format,n.textureType,d),this.checkError(),l}updateTexture(e,t,n,o,u){let l=this.gl;l.bindTexture(l.TEXTURE_2D,e);let d=o.encode(u,t*n);l.texSubImage2D(l.TEXTURE_2D,0,0,0,t,n,o.format,o.textureType,d),this.checkError()}attachFramebuffer(e,t,n){let o=this.gl;o.bindTexture(o.TEXTURE_2D,e),o.bindFramebuffer(o.FRAMEBUFFER,this.framebuffer),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,e,0),this.checkError(),o.viewport(0,0,t,n),o.scissor(0,0,t,n)}readTexture(e,t,n,o,u,l){let d=this.gl;l||(l=1),this.frameBufferBound||this.attachFramebuffer(e,t,n);let c=this.getEncoder(u,l),s=c.allocate(t*n);return d.bindTexture(d.TEXTURE_2D,e),d.framebufferTexture2D(d.FRAMEBUFFER,d.COLOR_ATTACHMENT0,d.TEXTURE_2D,e,0),d.readPixels(0,0,t,n,d.RGBA,c.textureType,s),this.checkError(),c.decode(s,o)}isFramebufferReady(){return!0}getActiveTexture(){let e=this.gl;return`TEXTURE${e.getParameter(this.gl.ACTIVE_TEXTURE)-e.TEXTURE0}`}getTextureBinding(){return this.gl.getParameter(this.gl.TEXTURE_BINDING_2D)}getFramebufferBinding(){return this.gl.getParameter(this.gl.FRAMEBUFFER_BINDING)}setVertexAttributes(e,t){let n=this.gl;n.vertexAttribPointer(e,3,n.FLOAT,!1,20,0),n.enableVertexAttribArray(e),t!==-1&&(n.vertexAttribPointer(t,2,n.FLOAT,!1,20,12),n.enableVertexAttribArray(t)),this.checkError()}createProgram(e,t){let n=this.gl,o=n.createProgram();return n.attachShader(o,e),n.attachShader(o,t),n.linkProgram(o),o}compileShader(e,t){let n=this.gl,o=n.createShader(t);if(!o)throw new Error(`createShader() returned null with type ${t}`);if(n.shaderSource(o,e),n.compileShader(o),n.getShaderParameter(o,n.COMPILE_STATUS)===!1)throw new Error(`Failed to compile shader: ${n.getShaderInfoLog(o)} +`);let t=this.glContext.compileShader(e,this.glContext.gl.FRAGMENT_SHADER),n=this.glContext.createProgram(this.vertexShader,t);return this.glContext.deleteShader(t),n}bindOutput(e){let t=e.width,n=e.height;Ye.verbose("ProrgramManager",`Binding output texture to Framebuffer: w/h=${t}/${n}, shape=${e.shape}, type=${e.tensor.type}`),this.glContext.attachFramebuffer(e.texture,t,n)}bindAttributes(e){let t=e.position,n=e.textureCoord;this.glContext.setVertexAttributes(t,n),this.attributesBound=!0}bindUniforms(e,t,n){let i=this.glContext.gl,u=0;for(let{name:l,type:d,location:c,arrayLength:s}of e){let r=t.find(o=>o.name===l)?.data;if(d!=="sampler2D"&&!r)throw new Error(`variable '${l}' does not have data defined in program info`);switch(d){case"sampler2D":this.bindTexture(n[u],c,u),u++;break;case"float":s?i.uniform1fv(c,r):i.uniform1f(c,r);break;case"int":s?i.uniform1iv(c,r):i.uniform1i(c,r);break;default:throw new Error(`Uniform not implemented: ${d}`)}}}bindTexture(e,t,n){this.glContext.bindTextureToUniform(e.texture,n,t)}getAttribLocations(e){return{position:this.getAttribLocation(e,"position"),textureCoord:this.getAttribLocation(e,"textureCoord")}}getUniformLocations(e,t,n){let i=[];if(t)for(let u of t)i.push({name:u,type:"sampler2D",location:this.getUniformLocation(e,u)});if(n)for(let u of n)i.push({...u,location:this.getUniformLocation(e,u.name)});return i}getUniformLocation(e,t){let n=this.glContext.gl.getUniformLocation(e,t);if(n===null)throw new Error(`Uniform ${t} not found.`);return n}getAttribLocation(e,t){return this.glContext.gl.getAttribLocation(e,t)}}}),Iv,cA=N(()=>{"use strict";Gt(),Ro(),Iv=class{constructor(e,t,n,i){this.glContext=e,this.layoutStrategy=t,this.profiler=n,this.config=i,this.pendingRead=new Map,i.reuseTextures&&(this.inUseTextures=new Map,this.idleTextures=new Map,this.textureLookup=new Map)}createTextureFromLayout(e,t,n,i){let u=this.toEncoderType(e),l=this.glContext.getEncoder(u,t.channels||1,i);if(t.isPacked&&i===1)throw new Error("not implemented");let d=t.width,c=t.height,s,r;if(this.config.reuseTextures){s=`${d}x${c}_${l.format}_${l.internalFormat}_${l.textureType}`,r=this.inUseTextures.get(s),r||(r=[],this.inUseTextures.set(s,r));let a=this.idleTextures.get(s);if(a&&a.length>0){let p=a.pop();return r.push(p),i===1&&this.glContext.updateTexture(p,d,c,l,this.toTextureData(e,n)),p}}Ye.verbose("TextureManager",`Creating new texture of size ${t.width}x${t.height}`);let o=this.glContext.allocateTexture(d,c,l,this.toTextureData(e,n));return this.config.reuseTextures&&(r.push(o),this.textureLookup.set(o,s)),o}readTexture(e,t,n){return n||(n=1),this.profiler.event("backend","TextureManager.readTexture",()=>{let i=e.shape.reduce((l,d)=>l*d)*n,u=this.glContext.readTexture(e.texture,e.width,e.height,i,this.toEncoderType(t),n);return this.toTensorData(t,u)})}async readTextureAsync(e,t,n){let i=e.tensor.dataId;if(n||(n=1),this.pendingRead.has(i)){let u=this.pendingRead.get(i);return new Promise(l=>u?.push(l))}return this.profiler.event("backend","TextureManager.readTextureAsync",async()=>{this.pendingRead.set(i,[]);let u=e.shape.reduce((s,r)=>s*r)*n;await this.glContext.createAndWaitForFence();let l=this.glContext.readTexture(e.texture,e.width,e.height,u,this.toEncoderType(t),n),d=this.toTensorData(t,l),c=this.pendingRead.get(i);return this.pendingRead.delete(i),c?.forEach(s=>s(d)),d})}readUint8TextureAsFloat(e){return this.profiler.event("backend","TextureManager.readUint8TextureAsFloat",()=>{let t=e.shape.reduce((i,u)=>i*u),n=this.glContext.readTexture(e.texture,e.width,e.height,t*4,"byte",4);return new Float32Array(n.buffer,n.byteOffset,t)})}releaseTexture(e,t){let n;if(this.config.reuseTextures&&(n=this.textureLookup.get(e.texture),n)){t&&this.textureLookup.delete(n);let i=this.inUseTextures.get(n);if(i){let u=i.indexOf(e.texture);if(u!==-1){i.splice(u,1);let l=this.idleTextures.get(n);l||(l=[],this.idleTextures.set(n,l)),l.push(e.texture)}}}(!n||t)&&(Ye.verbose("TextureManager",`Deleting texture of size ${e.width}x${e.height}`),this.glContext.deleteTexture(e.texture))}toTensorData(e,t){switch(e){case"int16":return t instanceof Int16Array?t:Int16Array.from(t);case"int32":return t instanceof Int32Array?t:Int32Array.from(t);case"int8":return t instanceof Int8Array?t:Int8Array.from(t);case"uint16":return t instanceof Uint16Array?t:Uint16Array.from(t);case"uint32":return t instanceof Uint32Array?t:Uint32Array.from(t);case"uint8":case"bool":return t instanceof Uint8Array?t:Uint8Array.from(t);case"float32":return t instanceof Float32Array?t:Float32Array.from(t);case"float64":return t instanceof Float64Array?t:Float64Array.from(t);default:throw new Error(`TensorData type ${e} is not supported`)}}toTextureData(e,t){if(t)return t instanceof Float32Array?t:new Float32Array(t)}toEncoderType(e){return"float"}clearActiveTextures(){this.glContext.clearActiveTextures()}}}),Ov,pA=N(()=>{"use strict";Gt(),v3(),MO(),YE(),dA(),yv(),cA(),Ov=class{constructor(e,t){this.backend=e,this.context=t,this.layoutStrategy=new bv(e.glContext.maxTextureSize),this.programManager=new Sv(this.context.profiler,e.glContext,this.layoutStrategy),this.textureManager=new Iv(e.glContext,this.layoutStrategy,this.context.profiler,{reuseTextures:e.textureCacheMode==="full"}),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map,this.pack=e.pack,this.pack2unpackMap=new Map,this.unpack2packMap=new Map}createInferenceHandler(){return new sw(this)}onGraphInitialized(e){let t=e.getValues().filter(n=>n.from===-1&&n.tensor).map(n=>n.tensor.dataId);this.initializers=new Set(t)}isInitializer(e){return this.initializers?this.initializers.has(e):!1}addInitializer(e){this.initializers.add(e)}getTextureData(e,t){return t?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,t,n=!1){Ye.verbose("WebGLSessionHandler","Storing Texture data in cache"),n?this.packedTextureDataCache.set(e,t):this.unpackedTextureDataCache.set(e,t)}dispose(){this.programManager.dispose(),this.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.unpackedTextureDataCache=new Map}resolve(e,t,n){let i=w3(e,t,mv);return{impl:i.opImpl,context:i.opInit?i.opInit(e,n):e}}}});function hA(e){let t=0;for(;t{"use strict";dt(),Ro(),Ro(),sr(),ul=class{constructor(e,t){this.frameBufferBound=!1,this.itemsToPoll=[],this.gl=e,this.version=t,this.getExtensions(),this.vertexbuffer=this.createVertexbuffer(),this.framebuffer=this.createFramebuffer(),this.queryVitalParameters()}allocateTexture(e,t,n,i){let u=this.gl,l=u.createTexture();u.bindTexture(u.TEXTURE_2D,l),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MIN_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_MAG_FILTER,u.NEAREST),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_S,u.CLAMP_TO_EDGE),u.texParameteri(u.TEXTURE_2D,u.TEXTURE_WRAP_T,u.CLAMP_TO_EDGE);let d=i?n.encode(i,e*t):null;return u.texImage2D(u.TEXTURE_2D,0,n.internalFormat,e,t,0,n.format,n.textureType,d),this.checkError(),l}updateTexture(e,t,n,i,u){let l=this.gl;l.bindTexture(l.TEXTURE_2D,e);let d=i.encode(u,t*n);l.texSubImage2D(l.TEXTURE_2D,0,0,0,t,n,i.format,i.textureType,d),this.checkError()}attachFramebuffer(e,t,n){let i=this.gl;i.bindTexture(i.TEXTURE_2D,e),i.bindFramebuffer(i.FRAMEBUFFER,this.framebuffer),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,e,0),this.checkError(),i.viewport(0,0,t,n),i.scissor(0,0,t,n)}readTexture(e,t,n,i,u,l){let d=this.gl;l||(l=1),this.frameBufferBound||this.attachFramebuffer(e,t,n);let c=this.getEncoder(u,l),s=c.allocate(t*n);return d.bindTexture(d.TEXTURE_2D,e),d.framebufferTexture2D(d.FRAMEBUFFER,d.COLOR_ATTACHMENT0,d.TEXTURE_2D,e,0),d.readPixels(0,0,t,n,d.RGBA,c.textureType,s),this.checkError(),c.decode(s,i)}isFramebufferReady(){return!0}getActiveTexture(){let e=this.gl;return`TEXTURE${e.getParameter(this.gl.ACTIVE_TEXTURE)-e.TEXTURE0}`}getTextureBinding(){return this.gl.getParameter(this.gl.TEXTURE_BINDING_2D)}getFramebufferBinding(){return this.gl.getParameter(this.gl.FRAMEBUFFER_BINDING)}setVertexAttributes(e,t){let n=this.gl;n.vertexAttribPointer(e,3,n.FLOAT,!1,20,0),n.enableVertexAttribArray(e),t!==-1&&(n.vertexAttribPointer(t,2,n.FLOAT,!1,20,12),n.enableVertexAttribArray(t)),this.checkError()}createProgram(e,t){let n=this.gl,i=n.createProgram();return n.attachShader(i,e),n.attachShader(i,t),n.linkProgram(i),i}compileShader(e,t){let n=this.gl,i=n.createShader(t);if(!i)throw new Error(`createShader() returned null with type ${t}`);if(n.shaderSource(i,e),n.compileShader(i),n.getShaderParameter(i,n.COMPILE_STATUS)===!1)throw new Error(`Failed to compile shader: ${n.getShaderInfoLog(i)} Shader source: -${e}`);return o}deleteShader(e){this.gl.deleteShader(e)}bindTextureToUniform(e,t,n){let o=this.gl;o.activeTexture(o.TEXTURE0+t),this.checkError(),o.bindTexture(o.TEXTURE_2D,e),this.checkError(),o.uniform1i(n,t),this.checkError()}draw(){this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),this.checkError()}checkError(){if(le.debug){let e=this.gl,t=e.getError(),n="";switch(t){case e.NO_ERROR:return;case e.INVALID_ENUM:n="INVALID_ENUM";break;case e.INVALID_VALUE:n="INVALID_VALUE";break;case e.INVALID_OPERATION:n="INVALID_OPERATION";break;case e.INVALID_FRAMEBUFFER_OPERATION:n="INVALID_FRAMEBUFFER_OPERATION";break;case e.OUT_OF_MEMORY:n="OUT_OF_MEMORY";break;case e.CONTEXT_LOST_WEBGL:n="CONTEXT_LOST_WEBGL";break;default:n=`Unknown WebGL Error: ${t.toString(16)}`}throw new Error(n)}}deleteTexture(e){this.gl.deleteTexture(e)}deleteProgram(e){this.gl.deleteProgram(e)}getEncoder(e,t,n=0){if(this.version===2)return new Z0(this.gl,t);switch(e){case"float":return n===1||this.isRenderFloat32Supported?new ju(this.gl,t):new ju(this.gl,t,this.textureHalfFloatExtension.HALF_FLOAT_OES);case"int":throw new Error("not implemented");case"byte":return new J0(this.gl,t);default:throw new Error(`Invalid dataType: ${e}`)}}clearActiveTextures(){let e=this.gl;for(let t=0;tthis.isTimerResultAvailable(e)),this.getTimerResult(e)}async createAndWaitForFence(){let e=this.createFence(this.gl);return this.pollFence(e)}createFence(e){let t,n=e,o=n.fenceSync(n.SYNC_GPU_COMMANDS_COMPLETE,0);return e.flush(),o===null?t=()=>!0:t=()=>{let u=n.clientWaitSync(o,0,0);return u===n.ALREADY_SIGNALED||u===n.CONDITION_SATISFIED},{query:o,isFencePassed:t}}async pollFence(e){return new Promise(t=>{this.addItemToPoll(()=>e.isFencePassed(),()=>t())})}pollItems(){let e=tA(this.itemsToPoll.map(t=>t.isDoneFn));for(let t=0;t<=e;++t){let{resolveFn:n}=this.itemsToPoll[t];n()}this.itemsToPoll=this.itemsToPoll.slice(e+1)}async addItemToPoll(e,t){this.itemsToPoll.push({isDoneFn:e,resolveFn:t}),!(this.itemsToPoll.length>1)&&await zp(()=>(this.pollItems(),this.itemsToPoll.length===0))}}});function vv(e){let t;if((!e||e==="webgl2")&&"webgl2"in Kr?t=Kr.webgl2:(!e||e==="webgl")&&"webgl"in Kr&&(t=Kr.webgl),!t)try{let o=iA();t=jf(o,e)}catch{let o=nA();t=jf(o,e)}e=e||t.version===1?"webgl":"webgl2";let n=t.gl;return Kr[e]=t,n.isContextLost()?(delete Kr[e],vv(e)):(n.disable(n.DEPTH_TEST),n.disable(n.STENCIL_TEST),n.disable(n.BLEND),n.disable(n.DITHER),n.disable(n.POLYGON_OFFSET_FILL),n.disable(n.SAMPLE_COVERAGE),n.enable(n.SCISSOR_TEST),n.enable(n.CULL_FACE),n.cullFace(n.BACK),t)}function jf(e,t){let n={alpha:!1,depth:!1,antialias:!1,stencil:!1,preserveDrawingBuffer:!1,premultipliedAlpha:!1,failIfMajorPerformanceCaveat:!1},o,u=n;if((!t||t==="webgl2")&&(o=e.getContext("webgl2",u),o))try{return new il(o,2)}catch(l){We.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl2'. Error: ${l}`)}if((!t||t==="webgl")&&(o=e.getContext("webgl",u)||e.getContext("experimental-webgl",u),o))try{return new il(o,1)}catch(l){We.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl' or 'experimental-webgl'. Error: ${l}`)}throw new Error("WebGL is not supported")}function nA(){if(typeof document>"u")throw new TypeError("failed to create canvas: document is not supported");let e=document.createElement("canvas");return e.width=1,e.height=1,e}function iA(){if(typeof OffscreenCanvas>"u")throw new TypeError("failed to create offscreen canvas: OffscreenCanvas is not supported");return new OffscreenCanvas(1,1)}var Kr,oA=R(()=>{"use strict";Bt(),rA(),Kr={}}),xv,sA=R(()=>{"use strict";ot(),Bt(),eA(),oA(),xv=class{get contextId(){return le.webgl.contextId}set contextId(e){le.webgl.contextId=e}get matmulMaxBatchSize(){return le.webgl.matmulMaxBatchSize}set matmulMaxBatchSize(e){le.webgl.matmulMaxBatchSize=e}get textureCacheMode(){return le.webgl.textureCacheMode}set textureCacheMode(e){le.webgl.textureCacheMode=e}get pack(){return le.webgl.pack}set pack(e){le.webgl.pack=e}get async(){return le.webgl.async}set async(e){le.webgl.async=e}initialize(){try{return this.glContext=vv(this.contextId),typeof this.matmulMaxBatchSize!="number"&&(this.matmulMaxBatchSize=16),typeof this.textureCacheMode!="string"&&(this.textureCacheMode="full"),typeof this.pack!="boolean"&&(this.pack=!1),typeof this.async!="boolean"&&(this.async=!1),We.setWithEnv(le),le.webgl.context||Object.defineProperty(le.webgl,"context",{value:this.glContext.gl}),We.verbose("WebGLBackend",`Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${this.matmulMaxBatchSize}; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${this.async}.`),!0}catch(e){return We.warning("WebGLBackend",`Unable to initialize WebGLBackend. ${e}`),!1}}createSessionHandler(e){return new _v(this,e)}dispose(){this.glContext.dispose()}}});async function $v(e){if(e){let t=typeof e=="string"?[e]:e;for(let n of t){let o=Cl.get(n);if(o)return o;let u=await aA(n);if(u)return u}}else return $v(["webgl"]);throw new Error("no available backend to use")}async function aA(e){let t=Tv;if(typeof t[e]<"u"&&uA(t[e])){let n=t[e],o=n.initialize();if(typeof o=="object"&&"then"in o&&(o=await o),o)return Cl.set(e,n),n}}function uA(e){let t=e;return"initialize"in t&&typeof t.initialize=="function"&&"createSessionHandler"in t&&typeof t.createSessionHandler=="function"&&"dispose"in t&&typeof t.dispose=="function"}var Cl,Tv,lA=R(()=>{"use strict";sA(),Cl=new Map,Tv={webgl:new xv}}),Lf,Sv,dA=R(()=>{"use strict";Bt(),Lf=class{constructor(e,t){this.op=e,this.node=t}},Sv=class{constructor(e,t,n){this.graph=e,this.profiler=n,this.initialize(t)}initialize(e){this.profiler.event("session","ExecutionPlan.initialize",()=>{let t=this.graph.getNodes();if(t.length!==e.length)throw new Error("The size of nodes and OPs do not match.");this._ops=e.map((n,o)=>new Lf(n,t[o])),this.reset(),this._starter=[],this._ops.forEach((n,o)=>{let u=!0;for(let l of n.node.inputs)if(!this._values[l]&&this.graph.getInputIndices().indexOf(l)===-1){u=!1;break}u&&this._starter.push(o)})})}reset(){this._values=this.graph.getValues().map(e=>e.tensor)}async execute(e,t){return this.profiler.event("session","ExecutionPlan.execute",async()=>{this.reset();let n=e.createInferenceHandler(),o=this.graph.getInputIndices();if(t.length!==o.length)throw new Error(`number of input tensors don't match the number of inputs to the model: actual: ${t.length} expected: ${o.length}`);t.forEach((r,i)=>{let a=o[i];this._values[a]=r});let u=this._starter.slice(0),l=this.graph.getValues(),d=this.graph.getNodes(),c=0;for(;cthis._values[b]);if(a.indexOf(void 0)!==-1)throw new Error(`unresolved input detected: op: ${i.node}`);let p=a;We.verbose("ExecPlan",`Running op:${i.node.name} (${p.map((b,x)=>`'${i.node.inputs[x]}': ${b.type}[${b.dims.join(",")}]`).join(", ")})`);let h=await this.profiler.event("node",i.node.name,async()=>i.op.impl(n,p,i.op.context));if(h.length!==i.node.outputs.length)throw new Error("the size of output does not match model definition.");h.forEach((b,x)=>{let w=i.node.outputs[x];if(this._values[w])throw new Error(`output [${w}] already has value: op:${i.node.name}`);this._values[w]=b});let m=new Set;h.forEach((b,x)=>{let w=i.node.outputs[x];for(let y of l[w].to){let S=d[y],T=!0;for(let I of S.inputs)if(!this._values[I]){T=!1;break}T&&m.add(y)}}),u.push(...m)}let s=[];for(let r=0;r{"use strict";Lo(),_e=ce(Ei()),En(),Pe(),ol=class gi{constructor(t){if(this._attributes=new Map,t!=null){for(let n of t)n instanceof _e.onnx.AttributeProto?this._attributes.set(n.name,[gi.getValue(n),gi.getType(n)]):n instanceof Ru.Attribute&&this._attributes.set(n.name(),[gi.getValue(n),gi.getType(n)]);if(this._attributes.sizegt.fromProto(u));if(t instanceof Ru.Attribute)return o.map(u=>gt.fromOrtTensor(u))}return n===_e.onnx.AttributeProto.AttributeType.STRING&&t instanceof _e.onnx.AttributeProto?Nu(o):n===_e.onnx.AttributeProto.AttributeType.STRINGS&&t instanceof _e.onnx.AttributeProto?o.map(Nu):o}static getValueNoCheck(t){return t instanceof _e.onnx.AttributeProto?this.getValueNoCheckFromOnnxFormat(t):this.getValueNoCheckFromOrtFormat(t)}static getValueNoCheckFromOnnxFormat(t){switch(t.type){case _e.onnx.AttributeProto.AttributeType.FLOAT:return t.f;case _e.onnx.AttributeProto.AttributeType.INT:return t.i;case _e.onnx.AttributeProto.AttributeType.STRING:return t.s;case _e.onnx.AttributeProto.AttributeType.TENSOR:return t.t;case _e.onnx.AttributeProto.AttributeType.GRAPH:return t.g;case _e.onnx.AttributeProto.AttributeType.FLOATS:return t.floats;case _e.onnx.AttributeProto.AttributeType.INTS:return t.ints;case _e.onnx.AttributeProto.AttributeType.STRINGS:return t.strings;case _e.onnx.AttributeProto.AttributeType.TENSORS:return t.tensors;case _e.onnx.AttributeProto.AttributeType.GRAPHS:return t.graphs;default:throw new Error(`unsupported attribute type: ${_e.onnx.AttributeProto.AttributeType[t.type]}`)}}static getValueNoCheckFromOrtFormat(t){switch(t.type()){case Rt.AttributeType.FLOAT:return t.f();case Rt.AttributeType.INT:return t.i();case Rt.AttributeType.STRING:return t.s();case Rt.AttributeType.TENSOR:return t.t();case Rt.AttributeType.GRAPH:return t.g();case Rt.AttributeType.FLOATS:return t.floatsArray();case Rt.AttributeType.INTS:{let n=[];for(let o=0;o{"use strict";cA(),Lo(),ga=ce(Ei()),En(),Pe(),sl={from:(e,t)=>new Uf(e,t)},Kt=class{constructor(e){this._from=void 0,this._to=[],this.tensor=void 0,this.type=void 0,e&&(this.type=ht.tensorValueTypeFromProto(e.type.tensorType))}get from(){return this._from}get to(){return this._to}},ba=class{constructor(e,t){e instanceof ga.onnx.NodeProto?(this.name=e.name,this.opType=e.opType,this.attributes=new ol(e.attribute)):e instanceof B0.Node&&(this.name=t??e.name(),this.opType=e.opType(),this.attributes=new ol(ht.tensorAttributesFromORTFormat(e))),this.inputs=[],this.outputs=[],this.executeNode=!0}},Uf=class{constructor(e,t){if(!e)throw new TypeError("graph is empty");this.buildGraph(e),this.transformGraph(t),this.checkIsAcyclic()}getInputIndices(){return this._allInputIndices}getInputNames(){return this._allInputNames}getOutputIndices(){return this._allOutputIndices}getOutputNames(){return this._allOutputNames}getValues(){return this._allData}getNodes(){return this._nodes}buildGraph(e){if(e instanceof ga.onnx.GraphProto)this.buildGraphFromOnnxFormat(e);else if(e instanceof N0.Graph)this.buildGraphFromOrtFormat(e);else throw new TypeError("Graph type is not supported.")}buildGraphFromOnnxFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map;if(!e.input)throw new Error("missing information in graph: input");let o=[];for(let u of e.input){if(t.has(u.name))throw new Error(`duplicated input name: ${u.name}`);let l=this._allData.push(new Kt(u))-1;t.set(u.name,l),o.push(u.name)}if(!e.initializer)throw new Error("missing information in graph: initializer");for(let u of e.initializer){let l=t.get(u.name);if(l===void 0){let d=new Kt;d.type={shape:{dims:ht.tensorDimsFromProto(u.dims)},tensorType:ht.tensorDataTypeFromProto(u.dataType)},l=this._allData.push(d)-1,t.set(u.name,l)}this._allData[l]._from=-1,this._allData[l].tensor=gt.fromProto(u)}for(let u=0;u"u"&&(s=this._allData.push(new Kt)-1,t.set(c,s)),l.outputs.push(s),this._allData[s]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${s}`);if(this._allData[s]._from=u,d.opType==="Constant"){if(!d.attribute||d.attribute.length!==1||!d.attribute[0].t)throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(!d.output||d.output.length!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[s]._from=-1,this._allData[s].tensor=gt.fromProto(d.attribute[0].t)}}}for(let u=0;u"u"){if(c===""&&(d.input.length===3||d.input.length===4)&&d.opType==="Resize")continue;throw new Error(`unrecognized input '${c}' for node: ${d.name}`)}l.inputs.push(s),this._allData[s]._to.push(u)}}return!0}buildGraphFromOrtFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map,o=[];for(let u=0;u"u"&&(r=this._allData.push(new Kt)-1,t.set(s,r)),l.outputs.push(r),this._allData[r]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${r}`);if(this._allData[r]._from=u,d.opType()==="Constant"){if(d.attributesLength()!==1||!d.attributes(0).t())throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(d.outputsLength()!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[r]._from=-1,this._allData[r].tensor=gt.fromOrtTensor(d.attributes(0).t())}}}for(let u=0;u"u")throw new Error(`unrecognized input '${s}' for node: ${d.name()}`);l.inputs.push(r),this._allData[r]._to.push(u)}}}checkIsAcyclic(){let e=new Set;this._allInputIndices.forEach(o=>{this._allData[o]._to.forEach(u=>{e.add(u)})});let t=Array.from(e),n=new Array(this._nodes.length).fill("white");for(;t.length>0;){let o=t.pop();n[o]==="gray"?n[o]="black":(t.push(o),n[o]="gray",this._nodes[o].outputs.forEach(u=>{let l=this._allData[u];if(typeof l.tensor<"u")throw new Error("node outputs should not be initialized");if(l._from!==o)throw new Error("from property of the Value object doesn't match index of Node being processed");l._to.forEach(d=>{if(n[d]==="gray")throw new Error("model graph is cyclic");n[d]==="white"&&t.push(d)})}))}}transformGraph(e){this.removeAllIdentityNodes(),this.removeAllDropoutNodes(),this.fuseConvActivationNodes(),e&&e.transformGraph(this),this.finalizeGraph()}finalizeGraph(){let e=0,t=new Array(this._nodes.length,0),n=0;for(let o=0;o{this._allData[u]._from=-2});this._nodes.splice(n,this._nodes.length-n);for(let o=0;o=0)u._to[l]=t[u._to[l]];else throw new Error("Trying to update a removed node")}e=0;for(let o=0;o0){let u=-1;this._allData[o].from!==void 0&&this._allData[o].from!==-1?(u=this._nodes[this._allData[o].from].outputs.indexOf(o+e),u!==-1&&(this._nodes[this._allData[o].from].outputs[u]=o)):(u=this._allInputIndices.indexOf(o+e),u!==-1&&(this._allInputIndices[u]=o)),this._allData[o].to.forEach(l=>{u=this._nodes[l].inputs.indexOf(o+e),u!==-1&&(this._nodes[l].inputs[u]=o)}),this._allData[o].to.length===0&&(u=this._allOutputIndices.indexOf(o+e),u!==-1&&(this._allOutputIndices[u]=o))}}}deleteNode(e){let t=this._nodes[e];if(t.outputs.length>1){for(let d=1;d0)throw new Error("Node deletion with more than one output connected to other nodes is not supported. ")}t.executeNode=!1;let n=t.inputs[0],o=t.outputs[0],u=this._allData[o].to;for(let d=0;d0)for(let d of u){let c=this._nodes[d].inputs.indexOf(o);if(c===-1)throw new Error("The Node object doesn't have the output Value in it's 'inputs' property ");this._nodes[d].inputs[c]=n,this._allData[n].to.push(d)}}removeAllDropoutNodes(){let e=0;for(let t of this._nodes){if(t.opType==="Dropout"){if(t.inputs.length!==1)throw new Error("Dropout nodes should only contain one input. ");if(t.outputs.length!==1&&t.outputs.length!==2)throw new Error("Dropout nodes should contain either 1 or 2 output(s)");if(t.outputs.length===2&&this._allData[t.outputs[1]]._to.length!==0)throw new Error("Dropout nodes's second output should not be referenced by other nodes");this.deleteNode(e)}e++}}removeAllIdentityNodes(){let e=0;for(let t of this._nodes)t.opType==="Identity"&&this.deleteNode(e),e++}isActivation(e){switch(e.opType){case"Relu":case"Sigmoid":case"Clip":return!0;default:return!1}}fuseConvActivationNodes(){for(let e of this._nodes)if(e.opType==="Conv"){let t=this._allData[e.outputs[0]]._to;if(t.length===1&&this.isActivation(this._nodes[t[0]])){let n=this._nodes[t[0]];if(n.opType==="Clip")if(n.inputs.length===1)try{e.attributes.set("activation_params","floats",[n.attributes.getFloat("min"),n.attributes.getFloat("max")])}catch{e.attributes.set("activation_params","floats",[Ti,Si])}else if(n.inputs.length>=3&&this._allData[n.inputs[1]].tensor!==void 0&&this._allData[n.inputs[2]].tensor!==void 0)e.attributes.set("activation_params","floats",[this._allData[n.inputs[1]].tensor.floatData[0],this._allData[n.inputs[2]].tensor.floatData[0]]);else continue;e.attributes.set("activation","string",n.opType),this.deleteNode(t[0])}}}}}),Vf,Gf,Iv,hA=R(()=>{"use strict";Vf=ce(ke()),pA(),Lo(),Gf=ce(Ei()),Pe(),Iv=class{constructor(){}load(e,t,n){let o;if(!n)try{this.loadFromOnnxFormat(e,t);return}catch(u){if(n!==void 0)throw u;o=u}try{this.loadFromOrtFormat(e,t)}catch(u){throw n!==void 0?u:new Error(`Failed to load model as ONNX format: ${o} -as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Gf.onnx.ModelProto.decode(e);if(Vt.longToNumber(n.irVersion)<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=n.opsetImport.map(o=>({domain:o.domain,version:Vt.longToNumber(o.version)})),this._graph=sl.from(n.graph,t)}loadFromOrtFormat(e,t){let n=new Vf.ByteBuffer(e),o=z0.InferenceSession.getRootAsInferenceSession(n).model();if(Vt.longToNumber(o.irVersion())<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=[];for(let u=0;u{"use strict";lA(),dA(),Bt(),hA(),Ov=class{constructor(e={}){this._initialized=!1,this.backendHint=e.backendHint,this.profiler=Jy.create(e.profiler),this.context={profiler:this.profiler,graphInputTypes:[],graphInputDims:[]}}get inputNames(){return this._model.graph.getInputNames()}get outputNames(){return this._model.graph.getOutputNames()}startProfiling(){this.profiler.start()}endProfiling(){this.profiler.stop()}async loadModel(e,t,n){await this.profiler.event("session","Session.loadModel",async()=>{let o=await $v(this.backendHint);if(this.sessionHandler=o.createSessionHandler(this.context),this._model=new Iv,typeof e=="string"){let u=e.endsWith(".ort");{let l=await(await fetch(e)).arrayBuffer();this.initialize(new Uint8Array(l),u)}}else if(ArrayBuffer.isView(e))this.initialize(e);else{let u=new Uint8Array(e,t||0,n||e.byteLength);this.initialize(u)}})}initialize(e,t){if(this._initialized)throw new Error("already initialized");this.profiler.event("session","Session.initialize",()=>{let n=this.sessionHandler.transformGraph?this.sessionHandler:void 0;this._model.load(e,n,t),this.sessionHandler.onGraphInitialized&&this.sessionHandler.onGraphInitialized(this._model.graph),this.initializeOps(this._model.graph),this._executionPlan=new Sv(this._model.graph,this._ops,this.profiler)}),this._initialized=!0}async run(e){if(!this._initialized)throw new Error("session not initialized yet");return this.profiler.event("session","Session.run",async()=>{let t=this.normalizeAndValidateInputs(e),n=await this._executionPlan.execute(this.sessionHandler,t);return this.createOutput(n)})}normalizeAndValidateInputs(e){let t=this._model.graph.getInputNames();if(Array.isArray(e)){if(e.length!==t.length)throw new Error(`incorrect input array length: expected ${t.length} but got ${e.length}`)}else{if(e.size!==t.length)throw new Error(`incorrect input map size: expected ${t.length} but got ${e.size}`);let n=new Array(e.size),o=0;for(let u=0;u{"use strict";ot(),En(),Ev=class{constructor(e){this.session=e,this.inputNames=this.session.inputNames,this.outputNames=this.session.outputNames}get inputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}get outputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}async dispose(){}async run(e,t,n){let o=new Map;for(let d in e)if(Object.hasOwnProperty.call(e,d)){let c=e[d];o.set(d,new gt(c.dims,c.type,void 0,void 0,c.data))}let u=await this.session.run(o),l={};return u.forEach((d,c)=>{l[c]=new mt(d.type,d.data,d.dims)}),l}startProfiling(){this.session.startProfiling()}endProfiling(){this.session.endProfiling()}}}),Av={};un(Av,{onnxjsBackend:()=>kv});var qf,kv,gA=R(()=>{"use strict";fA(),mA(),qf=class{async init(){}async createInferenceSessionHandler(e,t){let n=new Ov(t);return typeof e=="string"?await n.loadModel(e):await n.loadModel(e),new Ev(n)}},kv=new qf}),Dl=R(()=>{"use strict"}),Pv={};un(Pv,{default:()=>Cv});var ya,wa,Cv,bA=R(()=>{"use strict";j2(),dn(),Rl(),ya="ort-wasm-proxy-worker",wa=globalThis.self?.name===ya,wa&&(self.onmessage=e=>{let{type:t,in:n}=e.data;try{switch(t){case"init-wasm":Nl(n.wasm).then(()=>{Yl(n).then(()=>{postMessage({type:t})},o=>{postMessage({type:t,err:o})})},o=>{postMessage({type:t,err:o})});break;case"init-ep":{let{epName:o,env:u}=n;Ql(u,o).then(()=>{postMessage({type:t})},l=>{postMessage({type:t,err:l})});break}case"copy-from":{let{buffer:o}=n,u=No(o);postMessage({type:t,out:u});break}case"create":{let{model:o,options:u}=n;ed(o,u).then(l=>{postMessage({type:t,out:l})},l=>{postMessage({type:t,err:l})});break}case"release":td(n),postMessage({type:t});break;case"run":{let{sessionId:o,inputIndices:u,inputs:l,outputIndices:d,options:c}=n;rd(o,u,l,d,new Array(d.length).fill(null),c).then(s=>{s.some(r=>r[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:s},id([...l,...s]))},s=>{postMessage({type:t,err:s})});break}case"end-profiling":nd(n),postMessage({type:t});break;default:}}catch(o){postMessage({type:t,err:o})}}),Cv=wa?null:e=>new Worker(e??pt,{type:"module",name:ya})}),Dv={};un(Dv,{default:()=>Rv});async function Hf(e={}){var t=e,n=!!globalThis.window,o=!!globalThis.WorkerGlobalScope,u=o&&self.name?.startsWith("em-pthread");t.mountExternalData=(f,g)=>{f.startsWith("./")&&(f=f.substring(2)),(t.Yc||(t.Yc=new Map)).set(f,g)},t.unmountExternalData=()=>{delete t.Yc,delete t.Zd,delete t.Yd,delete t.$d},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let l=f=>async(...g)=>{try{if(t.Xc)throw Error("Session already started");let $=t.Xc={Kd:g[0],errors:[]},v=await f(...g);if(t.Xc!==$)throw Error("Session mismatch");t.dd?.flush();let E=$.errors;if(0N),0{if(f==="webgpu"){[t.dd,t.Ad,t.Ed,t.ed,t.Dd,t.$b,t.Fd,t.Hd,t.Bd,t.Cd,t.Gd]=g;let $=t.dd;t.jsepRegisterBuffer=(v,E,k,N)=>$.registerBuffer(v,E,k,N),t.jsepGetBuffer=v=>$.getBuffer(v),t.jsepCreateDownloader=(v,E,k)=>$.createDownloader(v,E,k),t.jsepOnCreateSession=v=>{$.onCreateSession(v)},t.jsepOnReleaseSession=v=>{$.onReleaseSession(v)},t.jsepOnRunStart=v=>$.onRunStart(v),t.Id=(v,E)=>{$.upload(v,E)}}else if(f==="webnn"){let $=g[0];[t.Sd,t.sd,t.webnnEnsureTensor,t.td,t.webnnDownloadTensor,t.Rd,t.webnnEnableTraceEvent]=g.slice(1),t.webnnReleaseTensorId=t.sd,t.webnnUploadTensor=t.td,t.webnnRegisterMLContext=t.Rd,t.webnnOnRunStart=v=>$.onRunStart(v),t.webnnOnRunEnd=$.onRunEnd.bind($),t.webnnOnReleaseSession=v=>{$.onReleaseSession(v)},t.webnnCreateMLTensorDownloader=(v,E)=>$.createMLTensorDownloader(v,E),t.webnnRegisterMLTensor=(v,E,k,N)=>$.registerMLTensor(v,E,k,N),t.webnnCreateMLContext=v=>$.createMLContext(v),t.webnnRegisterGraphInput=$.registerGraphInput.bind($),t.webnnIsGraphInput=$.isGraphInput.bind($),t.webnnRegisterGraphOutput=$.registerGraphOutput.bind($),t.webnnIsGraphOutput=$.isGraphOutput.bind($),t.webnnCreateTemporaryTensor=$.createTemporaryTensor.bind($),t.webnnIsGraphInputOutputTypeSupported=$.isGraphInputOutputTypeSupported.bind($)}};let d=()=>{let f=g=>(...$)=>{let v=Ft;return $=g(...$),Ft!=v?new Promise((E,k)=>{vs={resolve:E,reject:k}}):$};(()=>{for(let g of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[g]=f(t[g])})(),l!==void 0&&(t._OrtRun=l(t._OrtRun),t._OrtRunWithBinding=l(t._OrtRunWithBinding)),d=void 0};t.asyncInit=()=>{d?.()};var c,s,r=(f,g)=>{throw g},i=import.meta.url,a="";if(n||o){try{a=new URL(".",i).href}catch{}o&&(s=f=>{var g=new XMLHttpRequest;return g.open("GET",f,!1),g.responseType="arraybuffer",g.send(null),new Uint8Array(g.response)}),c=async f=>{if(P(f))return new Promise(($,v)=>{var E=new XMLHttpRequest;E.open("GET",f,!0),E.responseType="arraybuffer",E.onload=()=>{E.status==200||E.status==0&&E.response?$(E.response):v(E.status)},E.onerror=v,E.send(null)});var g=await fetch(f,{credentials:"same-origin"});if(g.ok)return g.arrayBuffer();throw Error(g.status+" : "+g.url)}}var p,h,m,b,x,w,y=console.log.bind(console),S=console.error.bind(console),T=y,I=S,A=!1,P=f=>f.startsWith("file://");function O(){ir.buffer!=X.buffer&&Z()}if(u){let f=function(g){try{var $=g.data,v=$.Sc;if(v==="load"){let E=[];self.onmessage=k=>E.push(k),w=()=>{postMessage({Sc:"loaded"});for(let k of E)f(k);self.onmessage=f};for(let k of $.xd)t[k]&&!t[k].proxy||(t[k]=(...N)=>{postMessage({Sc:"callHandler",vd:k,args:N})},k=="print"&&(T=t[k]),k=="printErr"&&(I=t[k]));ir=$.Od,Z(),h=$.Pd,ye(),Qi()}else if(v==="run"){(function(E){var k=(O(),C)[E+52>>>2>>>0];E=(O(),C)[E+56>>>2>>>0],Ac(k,k-E),me(k)})($.Rc),Is($.Rc,0,0,1,0,0),kd(),ys($.Rc),U||($c(),U=!0);try{wT($.Md,$.bd)}catch(E){if(E!="unwind")throw E}}else $.target!=="setimmediate"&&(v==="checkMailbox"?U&&Hi():v&&(I(`worker: received unknown command ${v}`),I($)))}catch(E){throw Tc(),E}};var B=f,U=!1;self.onunhandledrejection=g=>{throw g.reason||g},self.onmessage=f}var X,q,D,Q,_,C,F,V,G,H,M,J=!1;function Z(){var f=ir.buffer;t.HEAP8=X=new Int8Array(f),D=new Int16Array(f),t.HEAPU8=q=new Uint8Array(f),Q=new Uint16Array(f),t.HEAP32=_=new Int32Array(f),t.HEAPU32=C=new Uint32Array(f),F=new Float32Array(f),V=new Float64Array(f),G=new BigInt64Array(f),H=new BigUint64Array(f)}function W(){J=!0,u?w():Wt.sb()}function pe(f){throw I(f="Aborted("+f+")"),A=!0,f=new WebAssembly.RuntimeError(f+". Build with -sASSERTIONS for more info."),x?.(f),f}function fe(){return{a:{ma:GS,hb:VS,g:_T,J:vT,f:xT,o:$T,i:TT,$:ST,b:IT,S:OT,Ia:zd,n:ET,aa:jd,Ya:Ld,Ea:Ud,Ga:Vd,Za:Gd,Wa:qd,Pa:Hd,Va:Wd,ka:Kd,Fa:Xd,Ca:Zd,Xa:Jd,Da:Yd,cb:AT,fa:PT,xa:CT,va:RT,ea:zT,N:BT,H:MT,wa:FT,_:HT,ya:WT,Sa:KT,Aa:ZT,Ja:JT,ta:YT,ga:QT,Ra:ys,$a:eS,Q:iS,r:lS,c:gs,ib:dS,y:cS,M:pS,D:hS,l:fS,s:sc,jb:mS,I:gS,R:bS,j:yS,u:wS,q:_S,k:vS,Ma:xS,Na:$S,Oa:TS,Ka:dc,La:cc,ua:pc,eb:IS,bb:AS,v:kS,ba:PS,ha:CS,ab:OS,V:DS,_a:RS,Ba:NS,F:SS,T:zS,la:Ji,za:MS,gb:BS,fb:FS,Ta:gc,Ua:bc,Ha:cs,U:yc,ja:wc,Qa:_c,ia:vc,lb:II,na:vI,mb:SI,oa:_I,G:dI,e:KS,t:HS,w:qS,B:iI,nb:bI,Z:gI,x:JS,pa:yI,X:xI,ca:mI,ob:fI,pb:hI,O:oI,qa:pI,qb:cI,L:uI,Y:wI,d:WS,A:ZS,m:XS,kb:OI,p:QS,z:eI,C:YS,E:tI,K:sI,ra:lI,P:$I,da:aI,W:TI,rb:nI,sa:rI,h:LS,a:ir,db:ds}}}async function ye(){function f(v,E){var k=Wt=v.exports;v={};for(let[N,L]of Object.entries(k))typeof L=="function"?(k=tS(L),v[N]=k):v[N]=L;return Wt=v,Wt=(function(){var N=Wt,L=ee=>de=>ee(de)>>>0,Y=ee=>()=>ee()>>>0;return(N=Object.assign({},N)).tb=L(N.tb),N.Xb=Y(N.Xb),N.Zb=L(N.Zb),N.lc=L(N.lc),N.mc=Y(N.mc),N.qc=L(N.qc),N})(),Ed.push(Wt._b),xc=(v=Wt).tb,$c=v.ub,t._OrtInit=v.vb,t._OrtGetLastError=v.wb,t._OrtCreateSessionOptions=v.xb,t._OrtAppendExecutionProvider=v.yb,t._OrtAddFreeDimensionOverride=v.zb,t._OrtAddSessionConfigEntry=v.Ab,t._OrtReleaseSessionOptions=v.Bb,t._OrtCreateSession=v.Cb,t._OrtReleaseSession=v.Db,t._OrtGetInputOutputCount=v.Eb,t._OrtGetInputOutputMetadata=v.Fb,t._OrtFree=v.Gb,t._OrtCreateTensor=v.Hb,t._OrtGetTensorData=v.Ib,t._OrtReleaseTensor=v.Jb,t._OrtCreateRunOptions=v.Kb,t._OrtAddRunConfigEntry=v.Lb,t._OrtReleaseRunOptions=v.Mb,t._OrtCreateBinding=v.Nb,t._OrtBindInput=v.Ob,t._OrtBindOutput=v.Pb,t._OrtClearBoundOutputs=v.Qb,t._OrtReleaseBinding=v.Rb,t._OrtRunWithBinding=v.Sb,t._OrtRun=v.Tb,t._OrtEndProfiling=v.Ub,t._JsepOutput=v.Vb,t._JsepGetNodeName=v.Wb,Yi=v.Xb,jt=t._free=v.Yb,Hn=t._malloc=v.Zb,Is=v.ac,Tc=v.bc,Sc=v.cc,Ic=v.dc,Os=v.ec,Oc=v.fc,Ec=v.gc,we=v.hc,Wn=v.ic,Ac=v.jc,me=v.kc,Es=v.lc,ge=v.mc,kc=v.nc,As=v.oc,Pc=v.pc,Cc=v.qc,Dc=v.rc,ks=v.sc,Rc=v.tc,Nc=v.uc,zc=v.vc,Bc=v.wc,Mc=v.xc,Fc=v.yc,jc=v.zc,Lc=v.Ac,Uc=v.Bc,Vc=v.Cc,Gc=v.Dc,qc=v.Ec,Hc=v.Fc,Wc=v.Gc,Kc=v.Hc,Xc=v.Ic,Zc=v.Jc,Jc=v.Kc,Yc=v.Lc,Qc=v.Mc,ep=v.Nc,tp=v.Pc,rp=v.Qc,np=v.$c,ip=v.ad,op=v.fd,sp=v.kd,ap=v.ld,up=v.md,lp=v.nd,dp=v.od,cp=v.pd,pp=v.qd,hp=v.rd,fp=v.wd,mp=v.Ud,gp=v.Vd,bp=v.Wd,yp=v.Xd,h=E,Wt}var g,$=fe();return t.instantiateWasm?new Promise(v=>{t.instantiateWasm($,(E,k)=>{v(f(E,k))})}):u?f(new WebAssembly.Instance(h,fe()),h):(M??=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",a):a+"ort-wasm-simd-threaded.jsep.wasm":new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href,g=await(async function(v){var E=M;if(!p&&!P(E))try{var k=fetch(E,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(k,v)}catch(N){I(`wasm streaming compile failed: ${N}`),I("falling back to ArrayBuffer instantiation")}return(async function(N,L){try{var Y=await(async function(ee){if(!p)try{var de=await c(ee);return new Uint8Array(de)}catch{}if(ee==M&&p)ee=new Uint8Array(p);else{if(!s)throw"both async and sync fetching of the wasm failed";ee=s(ee)}return ee})(N);return await WebAssembly.instantiate(Y,L)}catch(ee){I(`failed to asynchronously prepare wasm: ${ee}`),pe(ee)}})(E,v)})($),f(g.instance,g.module))}class Fe{name="ExitStatus";constructor(g){this.message=`Program terminated with exit(${g})`,this.status=g}}var Oe=f=>{f.terminate(),f.onmessage=()=>{}},Le=[],ut=0,Ir=null,Li=f=>{nr.length==0&&(Cd(),Pd(nr[0]));var g=nr.pop();if(!g)return 6;Gn.push(g),Or[f.Rc]=g,g.Rc=f.Rc;var $={Sc:"run",Md:f.Ld,bd:f.bd,Rc:f.Rc};return g.postMessage($,f.jd),0},rr=0,je=(f,g,...$)=>{var v,E=16*$.length,k=ge(),N=Es(E),L=N>>>3;for(v of $)typeof v=="bigint"?((O(),G)[L++>>>0]=1n,(O(),G)[L++>>>0]=v):((O(),G)[L++>>>0]=0n,(O(),V)[L++>>>0]=v);return f=Sc(f,0,E,N,g),me(k),f};function ds(f){if(u)return je(0,1,f);if(m=f,!(0{if(m=f,u)throw Od(f),"unwind";ds(f)},nr=[],Gn=[],Ed=[],Or={},Ad=f=>{var g=f.Rc;delete Or[g],nr.push(f),Gn.splice(Gn.indexOf(f),1),f.Rc=0,Ic(g)};function kd(){Ed.forEach(f=>f())}var Pd=f=>new Promise(g=>{f.onmessage=E=>{var k=E.data;if(E=k.Sc,k.Zc&&k.Zc!=Yi()){var N=Or[k.Zc];N?N.postMessage(k,k.jd):I(`Internal error! Worker sent a message "${E}" to target pthread ${k.Zc}, but that thread no longer exists!`)}else E==="checkMailbox"?Hi():E==="spawnThread"?Li(k):E==="cleanupThread"?qi(()=>{Ad(Or[k.Nd])}):E==="loaded"?(f.loaded=!0,g(f)):k.target==="setimmediate"?f.postMessage(k):E==="uncaughtException"?f.onerror(k.error):E==="callHandler"?t[k.vd](...k.args):E&&I(`worker sent an unknown command ${E}`)},f.onerror=E=>{throw I(`worker sent an error! ${E.filename}:${E.lineno}: ${E.message}`),E};var $,v=[];for($ of[])t.propertyIsEnumerable($)&&v.push($);f.postMessage({Sc:"load",xd:v,Od:ir,Pd:h})});function Cd(){var f=new Worker((()=>{let g=URL;return import.meta.url>"file:"&&import.meta.url<"file;"?new g("ort.all.bundle.min.mjs",import.meta.url):new URL(import.meta.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});nr.push(f)}var ir,wT=(f,g)=>{rr=0,f=ks(f,g),0>>=0);return(O(),X)[g.Tc+12>>>0]==0&&(Dd(g,!0),Vi--),Rd(g,!1),Ui.push(g),Cc(f)}var hn=0,vT=()=>{we(0,0);var f=Ui.pop();kc(f.cd),hn=0};function Dd(f,g){g=g?1:0,(O(),X)[f.Tc+12>>>0]=g}function Rd(f,g){g=g?1:0,(O(),X)[f.Tc+13>>>0]=g}class ps{constructor(g){this.cd=g,this.Tc=g-24}}var hs=f=>{var g=hn;if(!g)return Wn(0),0;var $=new ps(g);(O(),C)[$.Tc+16>>>2>>>0]=g;var v=(O(),C)[$.Tc+4>>>2>>>0];if(!v)return Wn(0),g;for(var E of f){if(E===0||E===v)break;if(Pc(E,v,$.Tc+16))return Wn(E),g}return Wn(v),g};function xT(){return hs([])}function $T(f){return hs([f>>>0])}function TT(f,g,$,v){return hs([f>>>0,g>>>0,$>>>0,v>>>0])}var ST=()=>{var f=Ui.pop();f||pe("no exception to throw");var g=f.cd;throw(O(),X)[f.Tc+13>>>0]==0&&(Ui.push(f),Rd(f,!0),Dd(f,!1),Vi++),As(g),hn=g};function IT(f,g,$){var v=new ps(f>>>=0);throw g>>>=0,$>>>=0,(O(),C)[v.Tc+16>>>2>>>0]=0,(O(),C)[v.Tc+4>>>2>>>0]=g,(O(),C)[v.Tc+8>>>2>>>0]=$,As(f),Vi++,hn=f}var OT=()=>Vi;function Nd(f,g,$,v){return u?je(2,1,f,g,$,v):zd(f,g,$,v)}function zd(f,g,$,v){if(f>>>=0,g>>>=0,$>>>=0,v>>>=0,!globalThis.SharedArrayBuffer)return 6;var E=[];return u&&E.length===0?Nd(f,g,$,v):(f={Ld:$,Rc:f,bd:v,jd:E},u?(f.Sc="spawnThread",postMessage(f,E),0):Li(f))}function ET(f){throw hn||=f>>>0,hn}var Bd=globalThis.TextDecoder&&new TextDecoder,Md=(f,g,$,v)=>{if($=g+$,v)return $;for(;f[g]&&!(g>=$);)++g;return g},Fd=(f,g=0,$,v)=>{if(16<($=Md(f,g>>>=0,$,v))-g&&f.buffer&&Bd)return Bd.decode(f.buffer instanceof ArrayBuffer?f.subarray(g,$):f.slice(g,$));for(v="";g<$;){var E=f[g++];if(128&E){var k=63&f[g++];if((224&E)==192)v+=String.fromCharCode((31&E)<<6|k);else{var N=63&f[g++];65536>(E=(240&E)==224?(15&E)<<12|k<<6|N:(7&E)<<18|k<<12|N<<6|63&f[g++])?v+=String.fromCharCode(E):(E-=65536,v+=String.fromCharCode(55296|E>>10,56320|1023&E))}}else v+=String.fromCharCode(E)}return v},Xe=(f,g,$)=>(f>>>=0)?Fd((O(),q),f,g,$):"";function jd(f,g,$){return u?je(3,1,f,g,$):0}function Ld(f,g){if(u)return je(4,1,f,g)}function Ud(f,g){if(u)return je(5,1,f,g)}function Vd(f,g,$){if(u)return je(6,1,f,g,$)}function Gd(f,g,$){return u?je(7,1,f,g,$):0}function qd(f,g){if(u)return je(8,1,f,g)}function Hd(f,g,$){if(u)return je(9,1,f,g,$)}function Wd(f,g,$,v){if(u)return je(10,1,f,g,$,v)}function Kd(f,g,$,v){if(u)return je(11,1,f,g,$,v)}function Xd(f,g,$,v){if(u)return je(12,1,f,g,$,v)}function Zd(f){if(u)return je(13,1,f)}function Jd(f,g){if(u)return je(14,1,f,g)}function Yd(f,g,$){if(u)return je(15,1,f,g,$)}var AT=()=>pe(""),Mt=f=>{f>>>=0;for(var g="";;){var $=(O(),q)[f++>>>0];if(!$)return g;g+=String.fromCharCode($)}},fs={},ms={},kT={},fn=class extends Error{constructor(f){super(f),this.name="BindingError"}};function Ht(f,g,$={}){return(function(v,E,k={}){var N=E.name;if(!v)throw new fn(`type "${N}" must have a positive integer typeid pointer`);if(ms.hasOwnProperty(v)){if(k.yd)return;throw new fn(`Cannot register type '${N}' twice`)}ms[v]=E,delete kT[v],fs.hasOwnProperty(v)&&(E=fs[v],delete fs[v],E.forEach(L=>L()))})(f,g,$)}var Qd=(f,g,$)=>{switch(g){case 1:return $?v=>(O(),X)[v>>>0]:v=>(O(),q)[v>>>0];case 2:return $?v=>(O(),D)[v>>>1>>>0]:v=>(O(),Q)[v>>>1>>>0];case 4:return $?v=>(O(),_)[v>>>2>>>0]:v=>(O(),C)[v>>>2>>>0];case 8:return $?v=>(O(),G)[v>>>3>>>0]:v=>(O(),H)[v>>>3>>>0];default:throw new TypeError(`invalid integer width (${g}): ${f}`)}};function PT(f,g,$,v,E){f>>>=0,$>>>=0,g=Mt(g>>>0);let k=N=>N;if(v=v===0n){let N=8*$;k=L=>BigInt.asUintN(N,L),E=k(E)}Ht(f,{name:g,Oc:k,Vc:(N,L)=>(typeof L=="number"&&(L=BigInt(L)),L),Uc:Qd(g,$,!v),Wc:null})}function CT(f,g,$,v){Ht(f>>>=0,{name:g=Mt(g>>>0),Oc:function(E){return!!E},Vc:function(E,k){return k?$:v},Uc:function(E){return this.Oc((O(),q)[E>>>0])},Wc:null})}var ec=[],Er=[0,1,,1,null,1,!0,1,!1,1];function gs(f){9<(f>>>=0)&&--Er[f+1]===0&&(Er[f]=void 0,ec.push(f))}var wt=f=>{if(!f)throw new fn(`Cannot use deleted val. handle = ${f}`);return Er[f]},Et=f=>{switch(f){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let g=ec.pop()||Er.length;return Er[g]=f,Er[g+1]=1,g}};function bs(f){return this.Oc((O(),C)[f>>>2>>>0])}var DT={name:"emscripten::val",Oc:f=>{var g=wt(f);return gs(f),g},Vc:(f,g)=>Et(g),Uc:bs,Wc:null};function RT(f){return Ht(f>>>0,DT)}var NT=(f,g)=>{switch(g){case 4:return function($){return this.Oc((O(),F)[$>>>2>>>0])};case 8:return function($){return this.Oc((O(),V)[$>>>3>>>0])};default:throw new TypeError(`invalid float width (${g}): ${f}`)}};function zT(f,g,$){$>>>=0,Ht(f>>>=0,{name:g=Mt(g>>>0),Oc:v=>v,Vc:(v,E)=>E,Uc:NT(g,$),Wc:null})}function BT(f,g,$,v,E){f>>>=0,$>>>=0,g=Mt(g>>>0);let k=L=>L;if(v===0){var N=32-8*$;k=L=>L<>>N,E=k(E)}Ht(f,{name:g,Oc:k,Vc:(L,Y)=>Y,Uc:Qd(g,$,v!==0),Wc:null})}function MT(f,g,$){function v(k){var N=(O(),C)[k>>>2>>>0];return k=(O(),C)[k+4>>>2>>>0],new E((O(),X).buffer,k,N)}var E=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][g];Ht(f>>>=0,{name:$=Mt($>>>0),Oc:v,Uc:v},{yd:!0})}var or=(f,g,$)=>{var v=(O(),q);if(g>>>=0,0<$){var E=g;$=g+$-1;for(var k=0;k=N){if(g>=$)break;v[g++>>>0]=N}else if(2047>=N){if(g+1>=$)break;v[g++>>>0]=192|N>>6,v[g++>>>0]=128|63&N}else if(65535>=N){if(g+2>=$)break;v[g++>>>0]=224|N>>12,v[g++>>>0]=128|N>>6&63,v[g++>>>0]=128|63&N}else{if(g+3>=$)break;v[g++>>>0]=240|N>>18,v[g++>>>0]=128|N>>12&63,v[g++>>>0]=128|N>>6&63,v[g++>>>0]=128|63&N,k++}}v[g>>>0]=0,f=g-E}else f=0;return f},Gi=f=>{for(var g=0,$=0;$=v?g++:2047>=v?g+=2:55296<=v&&57343>=v?(g+=4,++$):g+=3}return g};function FT(f,g){Ht(f>>>=0,{name:g=Mt(g>>>0),Oc($){var v=(O(),C)[$>>>2>>>0];return v=Xe($+4,v,!0),jt($),v},Vc($,v){v instanceof ArrayBuffer&&(v=new Uint8Array(v));var E=typeof v=="string";if(!(E||ArrayBuffer.isView(v)&&v.BYTES_PER_ELEMENT==1))throw new fn("Cannot pass non-string to std::string");var k=E?Gi(v):v.length,N=Hn(4+k+1),L=N+4;return(O(),C)[N>>>2>>>0]=k,E?or(v,L,k+1):(O(),q).set(v,L>>>0),$!==null&&$.push(jt,N),N},Uc:bs,Wc($){jt($)}})}var tc=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,jT=(f,g,$)=>{if(f>>>=1,16<(g=Md((O(),Q),f,g/2,$))-f&&tc)return tc.decode((O(),Q).slice(f,g));for($="";f>>0];$+=String.fromCharCode(v)}return $},LT=(f,g,$)=>{if($??=2147483647,2>$)return 0;var v=g;$=($-=2)<2*f.length?$/2:f.length;for(var E=0;E<$;++E){var k=f.charCodeAt(E);(O(),D)[g>>>1>>>0]=k,g+=2}return(O(),D)[g>>>1>>>0]=0,g-v},UT=f=>2*f.length,VT=(f,g,$)=>{var v="";f>>>=2;for(var E=0;!(E>=g/4);E++){var k=(O(),C)[f+E>>>0];if(!k&&!$)break;v+=String.fromCodePoint(k)}return v},GT=(f,g,$)=>{if(g>>>=0,$??=2147483647,4>$)return 0;var v=g;$=v+$-4;for(var E=0;E>>2>>>0]=k,(g+=4)+4>$)break}return(O(),_)[g>>>2>>>0]=0,g-v},qT=f=>{for(var g=0,$=0;$>>=0,g>>>=0,$=Mt($>>>=0),g===2)var v=jT,E=LT,k=UT;else v=VT,E=GT,k=qT;Ht(f,{name:$,Oc:N=>{var L=(O(),C)[N>>>2>>>0];return L=v(N+4,L*g,!0),jt(N),L},Vc:(N,L)=>{if(typeof L!="string")throw new fn(`Cannot pass non-string to C++ string type ${$}`);var Y=k(L),ee=Hn(4+Y+g);return(O(),C)[ee>>>2>>>0]=Y/g,E(L,ee+4,Y+g),N!==null&&N.push(jt,ee),ee},Uc:bs,Wc(N){jt(N)}})}function WT(f,g){Ht(f>>>=0,{zd:!0,name:g=Mt(g>>>0),Oc:()=>{},Vc:()=>{}})}function KT(f){Is(f>>>0,!o,1,!n,131072,!1),kd()}var qi=f=>{if(!A)try{if(f(),!(0Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function ys(f){f>>>=0,XT||(Atomics.waitAsync((O(),_),f>>>2,f).value.then(Hi),f+=128,Atomics.store((O(),_),f>>>2,1))}var Hi=()=>qi(()=>{var f=Yi();f&&(ys(f),Ec())});function ZT(f,g){(f>>>=0)==g>>>0?setTimeout(Hi):u?postMessage({Zc:f,Sc:"checkMailbox"}):(f=Or[f])&&f.postMessage({Sc:"checkMailbox"})}var ws=[];function JT(f,g,$,v,E){for(g>>>=0,E>>>=0,ws.length=0,$=E>>>3,v=E+v>>>3;$>>0]?(O(),G)[$++>>>0]:(O(),V)[$++>>>0],ws.push(k)}return(g?Ps[g]:US[f])(...ws)}var YT=()=>{rr=0};function QT(f){f>>>=0,u?postMessage({Sc:"cleanupThread",Nd:f}):Ad(Or[f])}function eS(f){}var Wi=f=>{try{f()}catch(g){pe(g)}};function tS(f){var g=(...$)=>{Ki.push(f);try{return f(...$)}finally{A||(Ki.pop(),Ft&&sr===1&&Ki.length===0&&(sr=0,rr+=1,Wi(gp),typeof Fibers<"u"&&Fibers.be()))}};return ic.set(f,g),g}var sr=0,Ft=null,rc=0,Ki=[],_s=new Map,nc=new Map,ic=new Map,rS=0,vs=null,nS=[],oc=f=>(function(g){if(!A){if(sr===0){var $=!1,v=!1;g((E=0)=>{if(!A&&(rc=E,$=!0,v)){sr=2,Wi(()=>bp(Ft)),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.resume(),E=!1;try{var k=(function(){var Y=(O(),_)[Ft+8>>>2>>>0];return Y=nc.get(Y),Y=ic.get(Y),--rr,Y()})()}catch(Y){k=Y,E=!0}var N=!1;if(!Ft){var L=vs;L&&(vs=null,(E?L.reject:L.resolve)(k),N=!0)}if(E&&!N)throw k}}),v=!0,$||(sr=1,Ft=(function(){var E=Hn(65548),k=E+12;if((O(),C)[E>>>2>>>0]=k,(O(),C)[E+4>>>2>>>0]=k+65536,k=Ki[0],!_s.has(k)){var N=rS++;_s.set(k,N),nc.set(N,k)}return k=_s.get(k),(O(),_)[E+8>>>2>>>0]=k,E})(),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.pause(),Wi(()=>mp(Ft)))}else sr===2?(sr=0,Wi(yp),jt(Ft),Ft=null,nS.forEach(qi)):pe(`invalid state: ${sr}`);return rc}})(g=>{f().then(g)});function iS(f){return f>>>=0,oc(async()=>{var g=await wt(f);return Et(g)})}var xs=[],oS=f=>{var g=xs.length;return xs.push(f),g},sS=(f,g)=>{for(var $=Array(f),v=0;v>>2>>>0],N=ms[k];if(N===void 0)throw f=`parameter ${v}`,k=xc(k),g=Mt(k),jt(k),new fn(`${f} has unknown type ${g}`);$[E]=N}return $},aS=(f,g,$)=>{var v=[];return f=f(v,$),v.length&&((O(),C)[g>>>2>>>0]=Et(v)),f},uS={},Xi=f=>{var g=uS[f];return g===void 0?Mt(f):g};function lS(f,g,$){var[v,...E]=sS(f,g>>>0);g=v.Vc.bind(v);var k=E.map(Y=>Y.Uc.bind(Y));f--;var N={toValue:wt};switch(f=k.map((Y,ee)=>{var de=`argFromPtr${ee}`;return N[de]=Y,`${de}(args${ee?"+"+8*ee:""})`}),$){case 0:var L="toValue(handle)";break;case 2:L="new (toValue(handle))";break;case 3:L="";break;case 1:N.getStringOrSymbol=Xi,L="toValue(handle)[getStringOrSymbol(methodName)]"}return L+=`(${f})`,v.zd||(N.toReturnWire=g,N.emval_returnValue=aS,L=`return emval_returnValue(toReturnWire, destructorsRef, ${L})`),L=`return function (handle, methodName, destructorsRef, args) { - ${L} - }`,$=new Function(Object.keys(N),L)(...Object.values(N)),L=`methodCaller<(${E.map(Y=>Y.name)}) => ${v.name}>`,oS(Object.defineProperty($,"name",{value:L}))}function dS(f,g){return g>>>=0,(f=wt(f>>>0))==wt(g)}function cS(f){return(f>>>=0)?(f=Xi(f),Et(globalThis[f])):Et(globalThis)}function pS(f){return f=Xi(f>>>0),Et(t[f])}function hS(f,g){return g>>>=0,f=wt(f>>>0),g=wt(g),Et(f[g])}function fS(f){9<(f>>>=0)&&(Er[f+1]+=1)}function sc(f,g,$,v,E){return xs[f>>>0](g>>>0,$>>>0,v>>>0,E>>>0)}function mS(f,g,$,v,E){return sc(f>>>0,g>>>0,$>>>0,v>>>0,E>>>0)}function gS(){return Et([])}function bS(f){f=wt(f>>>0);for(var g=Array(f.length),$=0;$>>0))}function wS(){return Et({})}function _S(f){for(var g=wt(f>>>=0);g.length;){var $=g.pop();g.pop()($)}gs(f)}function vS(f,g,$){g>>>=0,$>>>=0,f=wt(f>>>0),g=wt(g),$=wt($),f[g]=$}function xS(f,g){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(O(),_)[g>>>2>>>0]=f.getUTCSeconds(),(O(),_)[g+4>>>2>>>0]=f.getUTCMinutes(),(O(),_)[g+8>>>2>>>0]=f.getUTCHours(),(O(),_)[g+12>>>2>>>0]=f.getUTCDate(),(O(),_)[g+16>>>2>>>0]=f.getUTCMonth(),(O(),_)[g+20>>>2>>>0]=f.getUTCFullYear()-1900,(O(),_)[g+24>>>2>>>0]=f.getUTCDay(),f=(f.getTime()-Date.UTC(f.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(O(),_)[g+28>>>2>>>0]=f}var ac=f=>f%4==0&&(f%100!=0||f%400==0),uc=[0,31,60,91,121,152,182,213,244,274,305,335],lc=[0,31,59,90,120,151,181,212,243,273,304,334];function $S(f,g){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(O(),_)[g>>>2>>>0]=f.getSeconds(),(O(),_)[g+4>>>2>>>0]=f.getMinutes(),(O(),_)[g+8>>>2>>>0]=f.getHours(),(O(),_)[g+12>>>2>>>0]=f.getDate(),(O(),_)[g+16>>>2>>>0]=f.getMonth(),(O(),_)[g+20>>>2>>>0]=f.getFullYear()-1900,(O(),_)[g+24>>>2>>>0]=f.getDay();var $=(ac(f.getFullYear())?uc:lc)[f.getMonth()]+f.getDate()-1|0;(O(),_)[g+28>>>2>>>0]=$,(O(),_)[g+36>>>2>>>0]=-60*f.getTimezoneOffset(),$=new Date(f.getFullYear(),6,1).getTimezoneOffset();var v=new Date(f.getFullYear(),0,1).getTimezoneOffset();f=0|($!=v&&f.getTimezoneOffset()==Math.min(v,$)),(O(),_)[g+32>>>2>>>0]=f}function TS(f){f>>>=0;var g=new Date((O(),_)[f+20>>>2>>>0]+1900,(O(),_)[f+16>>>2>>>0],(O(),_)[f+12>>>2>>>0],(O(),_)[f+8>>>2>>>0],(O(),_)[f+4>>>2>>>0],(O(),_)[f>>>2>>>0],0),$=(O(),_)[f+32>>>2>>>0],v=g.getTimezoneOffset(),E=new Date(g.getFullYear(),6,1).getTimezoneOffset(),k=new Date(g.getFullYear(),0,1).getTimezoneOffset(),N=Math.min(k,E);return 0>$?(O(),_)[f+32>>>2>>>0]=+(E!=k&&N==v):0<$!=(N==v)&&(E=Math.max(k,E),g.setTime(g.getTime()+6e4*((0<$?N:E)-v))),(O(),_)[f+24>>>2>>>0]=g.getDay(),$=(ac(g.getFullYear())?uc:lc)[g.getMonth()]+g.getDate()-1|0,(O(),_)[f+28>>>2>>>0]=$,(O(),_)[f>>>2>>>0]=g.getSeconds(),(O(),_)[f+4>>>2>>>0]=g.getMinutes(),(O(),_)[f+8>>>2>>>0]=g.getHours(),(O(),_)[f+12>>>2>>>0]=g.getDate(),(O(),_)[f+16>>>2>>>0]=g.getMonth(),(O(),_)[f+20>>>2>>>0]=g.getYear(),f=g.getTime(),BigInt(isNaN(f)?-1:f/1e3)}function dc(f,g,$,v,E,k,N){return u?je(16,1,f,g,$,v,E,k,N):-52}function cc(f,g,$,v,E,k){if(u)return je(17,1,f,g,$,v,E,k)}var qn={},SS=()=>performance.timeOrigin+performance.now();function pc(f,g){if(u)return je(18,1,f,g);if(qn[f]&&(clearTimeout(qn[f].id),delete qn[f]),!g)return 0;var $=setTimeout(()=>{delete qn[f],qi(()=>Oc(f,performance.timeOrigin+performance.now()))},g);return qn[f]={id:$,ae:g},0}function IS(f,g,$,v){f>>>=0,g>>>=0,$>>>=0,v>>>=0;var E=new Date().getFullYear(),k=new Date(E,0,1).getTimezoneOffset();E=new Date(E,6,1).getTimezoneOffset();var N=Math.max(k,E);(O(),C)[f>>>2>>>0]=60*N,(O(),_)[g>>>2>>>0]=+(k!=E),f=(g=L=>{var Y=Math.abs(L);return`UTC${0<=L?"-":"+"}${String(Math.floor(Y/60)).padStart(2,"0")}${String(Y%60).padStart(2,"0")}`})(k),g=g(E),EDate.now(),ES=1;function AS(f,g,$){if($>>>=0,!(0<=f&&3>=f))return 28;if(f===0)f=Date.now();else{if(!ES)return 52;f=performance.timeOrigin+performance.now()}return f=Math.round(1e6*f),(O(),G)[$>>>3>>>0]=BigInt(f),0}var $s=[],hc=(f,g)=>{$s.length=0;for(var $;$=(O(),q)[f++>>>0];){var v=$!=105;g+=(v&=$!=112)&&g%8?4:0,$s.push($==112?(O(),C)[g>>>2>>>0]:$==106?(O(),G)[g>>>3>>>0]:$==105?(O(),_)[g>>>2>>>0]:(O(),V)[g>>>3>>>0]),g+=v?8:4}return $s};function kS(f,g,$){return f>>>=0,g=hc(g>>>0,$>>>0),Ps[f](...g)}function PS(f,g,$){return f>>>=0,g=hc(g>>>0,$>>>0),Ps[f](...g)}var CS=()=>{};function DS(f,g){return I(Xe(f>>>0,g>>>0))}var RS=()=>{throw rr+=1,"unwind"};function NS(){return 4294901760}var zS=()=>navigator.hardwareConcurrency,Ar={},Zi=f=>{var g;return(g=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(f))?+g[1]:(g=/:(\d+):\d+(?:\)|$)/.exec(f))?2147483648|+g[1]:0},fc=f=>{for(var g of f)(f=Zi(g))&&(Ar[f]=g)};function BS(){var f=Error().stack.toString().split(` -`);return f[0]=="Error"&&f.shift(),fc(f),Ar.gd=Zi(f[3]),Ar.Jd=f,Ar.gd}function Ji(f){if(!(f=Ar[f>>>0]))return 0;var g;if(g=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(f))f=g[1];else if(g=/^\s+at (.*) \(.*\)$/.exec(f))f=g[1];else{if(!(g=/^(.+?)@/.exec(f)))return 0;f=g[1]}jt(Ji.hd??0),g=Gi(f)+1;var $=Hn(g);return $&&or(f,$,g),Ji.hd=$,Ji.hd}function MS(f){f>>>=0;var g=(O(),q).length;if(f<=g||4294901760=$;$*=2){var v=g*(1+.2/$);v=Math.min(v,f+100663296);e:{v=(Math.min(4294901760,65536*Math.ceil(Math.max(f,v)/65536))-ir.buffer.byteLength+65535)/65536|0;try{ir.grow(v),Z();var E=1;break e}catch{}E=void 0}if(E)return!0}return!1}function FS(f,g,$){if(f>>>=0,g>>>=0,Ar.gd==f)var v=Ar.Jd;else(v=Error().stack.toString().split(` -`))[0]=="Error"&&v.shift(),fc(v);for(var E=3;v[E]&&Zi(v[E])!=f;)++E;for(f=0;f<$&&v[f+E];++f)(O(),_)[g+4*f>>>2>>>0]=Zi(v[f+E]);return f}var Ts,Ss={},mc=()=>{if(!Ts){var f,g={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(globalThis.navigator?.language??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(f in Ss)Ss[f]===void 0?delete g[f]:g[f]=Ss[f];var $=[];for(f in g)$.push(`${f}=${g[f]}`);Ts=$}return Ts};function gc(f,g){if(u)return je(19,1,f,g);f>>>=0,g>>>=0;var $,v=0,E=0;for($ of mc()){var k=g+v;(O(),C)[f+E>>>2>>>0]=k,v+=or($,k,1/0)+1,E+=4}return 0}function bc(f,g){if(u)return je(20,1,f,g);f>>>=0,g>>>=0;var $=mc();for(var v of((O(),C)[f>>>2>>>0]=$.length,f=0,$))f+=Gi(v)+1;return(O(),C)[g>>>2>>>0]=f,0}function yc(f){return u?je(21,1,f):52}function wc(f,g,$,v){return u?je(22,1,f,g,$,v):52}function _c(f,g,$,v){return u?je(23,1,f,g,$,v):70}var jS=[null,[],[]];function vc(f,g,$,v){if(u)return je(24,1,f,g,$,v);g>>>=0,$>>>=0,v>>>=0;for(var E=0,k=0;k<$;k++){var N=(O(),C)[g>>>2>>>0],L=(O(),C)[g+4>>>2>>>0];g+=8;for(var Y=0;Y>>0],xe=jS[ee];de===0||de===10?((ee===1?T:I)(Fd(xe)),xe.length=0):xe.push(de)}E+=L}return(O(),C)[v>>>2>>>0]=E,0}function LS(f){return f>>>0}u||(function(){for(var f=t.numThreads-1;f--;)Cd();Le.push(async()=>{var g=(async function(){if(!u)return Promise.all(nr.map(Pd))})();ut++,await g,--ut==0&&Ir&&(g=Ir,Ir=null,g())})})(),u||(ir=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),Z()),t.wasmBinary&&(p=t.wasmBinary),t.stackSave=()=>ge(),t.stackRestore=f=>me(f),t.stackAlloc=f=>Es(f),t.setValue=function(f,g,$="i8"){switch($.endsWith("*")&&($="*"),$){case"i1":case"i8":(O(),X)[f>>>0]=g;break;case"i16":(O(),D)[f>>>1>>>0]=g;break;case"i32":(O(),_)[f>>>2>>>0]=g;break;case"i64":(O(),G)[f>>>3>>>0]=BigInt(g);break;case"float":(O(),F)[f>>>2>>>0]=g;break;case"double":(O(),V)[f>>>3>>>0]=g;break;case"*":(O(),C)[f>>>2>>>0]=g;break;default:pe(`invalid type for setValue: ${$}`)}},t.getValue=function(f,g="i8"){switch(g.endsWith("*")&&(g="*"),g){case"i1":case"i8":return(O(),X)[f>>>0];case"i16":return(O(),D)[f>>>1>>>0];case"i32":return(O(),_)[f>>>2>>>0];case"i64":return(O(),G)[f>>>3>>>0];case"float":return(O(),F)[f>>>2>>>0];case"double":return(O(),V)[f>>>3>>>0];case"*":return(O(),C)[f>>>2>>>0];default:pe(`invalid type for getValue: ${g}`)}},t.UTF8ToString=Xe,t.stringToUTF8=or,t.lengthBytesUTF8=Gi;var xc,$c,Yi,jt,Hn,Is,Tc,Sc,Ic,Os,Oc,Ec,we,Wn,Ac,me,Es,ge,kc,As,Pc,Cc,Dc,ks,Rc,Nc,zc,Bc,Mc,Fc,jc,Lc,Uc,Vc,Gc,qc,Hc,Wc,Kc,Xc,Zc,Jc,Yc,Qc,ep,tp,rp,np,ip,op,sp,ap,up,lp,dp,cp,pp,hp,fp,mp,gp,bp,yp,Wt,US=[ds,Od,Nd,jd,Ld,Ud,Vd,Gd,qd,Hd,Wd,Kd,Xd,Zd,Jd,Yd,dc,cc,pc,gc,bc,yc,wc,_c,vc],Ps={1055492:(f,g,$,v,E)=>{if(t===void 0||!t.Yc)return 1;if((f=Xe(Number(f>>>0))).startsWith("./")&&(f=f.substring(2)),!(f=t.Yc.get(f)))return 2;if(g=Number(g>>>0),$=Number($>>>0),v=Number(v>>>0),g+$>f.byteLength)return 3;try{let k=f.subarray(g,g+$);switch(E){case 0:(O(),q).set(k,v>>>0);break;case 1:t.Qd?t.Qd(v,k):t.Id(v,k);break;default:return 4}return 0}catch{return 4}},1056316:(f,g,$)=>{t.td(f,(O(),q).subarray(g>>>0,g+$>>>0))},1056380:()=>t.Sd(),1056422:f=>{t.sd(f)},1056459:()=>{t.Bd()},1056490:()=>{t.Cd()},1056519:()=>{t.Gd()},1056544:f=>t.Ad(f),1056577:f=>t.Ed(f),1056609:(f,g,$)=>{t.ed(Number(f),Number(g),Number($),!0)},1056672:(f,g,$)=>{t.ed(Number(f),Number(g),Number($))},1056729:()=>typeof wasmOffsetConverter<"u",1056786:f=>{t.$b("Abs",f,void 0)},1056837:f=>{t.$b("Neg",f,void 0)},1056888:f=>{t.$b("Floor",f,void 0)},1056941:f=>{t.$b("Ceil",f,void 0)},1056993:f=>{t.$b("Reciprocal",f,void 0)},1057051:f=>{t.$b("Sqrt",f,void 0)},1057103:f=>{t.$b("Exp",f,void 0)},1057154:f=>{t.$b("Erf",f,void 0)},1057205:f=>{t.$b("Sigmoid",f,void 0)},1057260:(f,g,$)=>{t.$b("HardSigmoid",f,{alpha:g,beta:$})},1057339:f=>{t.$b("HardSwish",f,void 0)},1057396:f=>{t.$b("Log",f,void 0)},1057447:f=>{t.$b("Sin",f,void 0)},1057498:f=>{t.$b("Cos",f,void 0)},1057549:f=>{t.$b("Tan",f,void 0)},1057600:f=>{t.$b("Asin",f,void 0)},1057652:f=>{t.$b("Acos",f,void 0)},1057704:f=>{t.$b("Atan",f,void 0)},1057756:f=>{t.$b("Sinh",f,void 0)},1057808:f=>{t.$b("Cosh",f,void 0)},1057860:f=>{t.$b("Asinh",f,void 0)},1057913:f=>{t.$b("Acosh",f,void 0)},1057966:f=>{t.$b("Atanh",f,void 0)},1058019:f=>{t.$b("Tanh",f,void 0)},1058071:f=>{t.$b("Not",f,void 0)},1058122:(f,g,$)=>{t.$b("Clip",f,{min:g,max:$})},1058191:f=>{t.$b("Clip",f,void 0)},1058243:(f,g)=>{t.$b("Elu",f,{alpha:g})},1058301:f=>{t.$b("Gelu",f,void 0)},1058353:f=>{t.$b("Relu",f,void 0)},1058405:(f,g)=>{t.$b("LeakyRelu",f,{alpha:g})},1058469:(f,g)=>{t.$b("ThresholdedRelu",f,{alpha:g})},1058539:(f,g)=>{t.$b("Cast",f,{to:g})},1058597:f=>{t.$b("Add",f,void 0)},1058648:f=>{t.$b("Sub",f,void 0)},1058699:f=>{t.$b("Mul",f,void 0)},1058750:f=>{t.$b("Div",f,void 0)},1058801:f=>{t.$b("Pow",f,void 0)},1058852:f=>{t.$b("Equal",f,void 0)},1058905:f=>{t.$b("Greater",f,void 0)},1058960:f=>{t.$b("GreaterOrEqual",f,void 0)},1059022:f=>{t.$b("Less",f,void 0)},1059074:f=>{t.$b("LessOrEqual",f,void 0)},1059133:(f,g,$,v,E)=>{t.$b("ReduceMean",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059308:(f,g,$,v,E)=>{t.$b("ReduceMax",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059482:(f,g,$,v,E)=>{t.$b("ReduceMin",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059656:(f,g,$,v,E)=>{t.$b("ReduceProd",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1059831:(f,g,$,v,E)=>{t.$b("ReduceSum",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060005:(f,g,$,v,E)=>{t.$b("ReduceL1",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060178:(f,g,$,v,E)=>{t.$b("ReduceL2",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060351:(f,g,$,v,E)=>{t.$b("ReduceLogSum",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060528:(f,g,$,v,E)=>{t.$b("ReduceSumSquare",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060708:(f,g,$,v,E)=>{t.$b("ReduceLogSumExp",f,{keepDims:!!g,noopWithEmptyAxes:!!$,axes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1060888:f=>{t.$b("Where",f,void 0)},1060941:(f,g,$)=>{t.$b("Transpose",f,{perm:g?Array.from((O(),_).subarray(Number(g)>>>0,Number($)>>>0)):[]})},1061065:(f,g,$,v)=>{t.$b("DepthToSpace",f,{blocksize:g,mode:Xe($),format:v?"NHWC":"NCHW"})},1061198:(f,g,$,v)=>{t.$b("DepthToSpace",f,{blocksize:g,mode:Xe($),format:v?"NHWC":"NCHW"})},1061331:(f,g,$,v)=>{t.$b("DFT",f,{axis:g,inverse:$,onesided:v})},1061423:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne,ar)=>{t.$b("ConvTranspose",f,{format:Y?"NHWC":"NCHW",autoPad:g,dilations:[$],group:v,kernelShape:[E],pads:[k,N],strides:[L],wIsConst:()=>!!(O(),X)[ee>>>0],outputPadding:de?Array.from((O(),_).subarray(Number(de)>>>0,Number(xe)>>>0)):[],outputShape:Ce?Array.from((O(),_).subarray(Number(Ce)>>>0,Number(Ne)>>>0)):[],activation:Xe(ar)})},1061856:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne)=>{t.$b("ConvTranspose",f,{format:L?"NHWC":"NCHW",autoPad:g,dilations:Array.from((O(),_).subarray(Number($)>>>0,(Number($)>>>0)+2>>>0)),group:v,kernelShape:Array.from((O(),_).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((O(),_).subarray(Number(k)>>>0,(Number(k)>>>0)+4>>>0)),strides:Array.from((O(),_).subarray(Number(N)>>>0,(Number(N)>>>0)+2>>>0)),wIsConst:()=>!!(O(),X)[Y>>>0],outputPadding:ee?Array.from((O(),_).subarray(Number(ee)>>>0,Number(de)>>>0)):[],outputShape:xe?Array.from((O(),_).subarray(Number(xe)>>>0,Number(Ce)>>>0)):[],activation:Xe(Ne)})},1062517:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne,ar)=>{t.$b("ConvTranspose",f,{format:Y?"NHWC":"NCHW",autoPad:g,dilations:[$],group:v,kernelShape:[E],pads:[k,N],strides:[L],wIsConst:()=>!!(O(),X)[ee>>>0],outputPadding:de?Array.from((O(),_).subarray(Number(de)>>>0,Number(xe)>>>0)):[],outputShape:Ce?Array.from((O(),_).subarray(Number(Ce)>>>0,Number(Ne)>>>0)):[],activation:Xe(ar)})},1062950:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne)=>{t.$b("ConvTranspose",f,{format:L?"NHWC":"NCHW",autoPad:g,dilations:Array.from((O(),_).subarray(Number($)>>>0,(Number($)>>>0)+2>>>0)),group:v,kernelShape:Array.from((O(),_).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((O(),_).subarray(Number(k)>>>0,(Number(k)>>>0)+4>>>0)),strides:Array.from((O(),_).subarray(Number(N)>>>0,(Number(N)>>>0)+2>>>0)),wIsConst:()=>!!(O(),X)[Y>>>0],outputPadding:ee?Array.from((O(),_).subarray(Number(ee)>>>0,Number(de)>>>0)):[],outputShape:xe?Array.from((O(),_).subarray(Number(xe)>>>0,Number(Ce)>>>0)):[],activation:Xe(Ne)})},1063611:(f,g)=>{t.$b("GlobalAveragePool",f,{format:g?"NHWC":"NCHW"})},1063702:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne)=>{t.$b("AveragePool",f,{format:Ne?"NHWC":"NCHW",auto_pad:g,ceil_mode:$,count_include_pad:v,storage_order:E,dilations:k?Array.from((O(),_).subarray(Number(k)>>>0,Number(N)>>>0)):[],kernel_shape:L?Array.from((O(),_).subarray(Number(L)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((O(),_).subarray(Number(ee)>>>0,Number(de)>>>0)):[],strides:xe?Array.from((O(),_).subarray(Number(xe)>>>0,Number(Ce)>>>0)):[]})},1064181:(f,g)=>{t.$b("GlobalAveragePool",f,{format:g?"NHWC":"NCHW"})},1064272:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne)=>{t.$b("AveragePool",f,{format:Ne?"NHWC":"NCHW",auto_pad:g,ceil_mode:$,count_include_pad:v,storage_order:E,dilations:k?Array.from((O(),_).subarray(Number(k)>>>0,Number(N)>>>0)):[],kernel_shape:L?Array.from((O(),_).subarray(Number(L)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((O(),_).subarray(Number(ee)>>>0,Number(de)>>>0)):[],strides:xe?Array.from((O(),_).subarray(Number(xe)>>>0,Number(Ce)>>>0)):[]})},1064751:(f,g)=>{t.$b("GlobalMaxPool",f,{format:g?"NHWC":"NCHW"})},1064838:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne)=>{t.$b("MaxPool",f,{format:Ne?"NHWC":"NCHW",auto_pad:g,ceil_mode:$,count_include_pad:v,storage_order:E,dilations:k?Array.from((O(),_).subarray(Number(k)>>>0,Number(N)>>>0)):[],kernel_shape:L?Array.from((O(),_).subarray(Number(L)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((O(),_).subarray(Number(ee)>>>0,Number(de)>>>0)):[],strides:xe?Array.from((O(),_).subarray(Number(xe)>>>0,Number(Ce)>>>0)):[]})},1065313:(f,g)=>{t.$b("GlobalMaxPool",f,{format:g?"NHWC":"NCHW"})},1065400:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne)=>{t.$b("MaxPool",f,{format:Ne?"NHWC":"NCHW",auto_pad:g,ceil_mode:$,count_include_pad:v,storage_order:E,dilations:k?Array.from((O(),_).subarray(Number(k)>>>0,Number(N)>>>0)):[],kernel_shape:L?Array.from((O(),_).subarray(Number(L)>>>0,Number(Y)>>>0)):[],pads:ee?Array.from((O(),_).subarray(Number(ee)>>>0,Number(de)>>>0)):[],strides:xe?Array.from((O(),_).subarray(Number(xe)>>>0,Number(Ce)>>>0)):[]})},1065875:(f,g,$,v,E)=>{t.$b("Gemm",f,{alpha:g,beta:$,transA:v,transB:E})},1065979:f=>{t.$b("MatMul",f,void 0)},1066033:(f,g,$,v)=>{t.$b("ArgMax",f,{keepDims:!!g,selectLastIndex:!!$,axis:v})},1066141:(f,g,$,v)=>{t.$b("ArgMin",f,{keepDims:!!g,selectLastIndex:!!$,axis:v})},1066249:(f,g)=>{t.$b("Softmax",f,{axis:g})},1066312:(f,g)=>{t.$b("Concat",f,{axis:g})},1066372:(f,g,$,v,E)=>{t.$b("Split",f,{axis:g,numOutputs:$,splitSizes:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1066528:f=>{t.$b("Expand",f,void 0)},1066582:(f,g)=>{t.$b("Gather",f,{axis:Number(g)})},1066653:(f,g)=>{t.$b("GatherElements",f,{axis:Number(g)})},1066732:(f,g)=>{t.$b("GatherND",f,{batch_dims:Number(g)})},1066811:(f,g,$,v,E,k,N,L,Y,ee,de)=>{t.$b("Resize",f,{antialias:g,axes:$?Array.from((O(),_).subarray(Number($)>>>0,Number(v)>>>0)):[],coordinateTransformMode:Xe(E),cubicCoeffA:k,excludeOutside:N,extrapolationValue:L,keepAspectRatioPolicy:Xe(Y),mode:Xe(ee),nearestMode:Xe(de)})},1067173:(f,g,$,v,E,k,N)=>{t.$b("Slice",f,{starts:g?Array.from((O(),_).subarray(Number(g)>>>0,Number($)>>>0)):[],ends:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[],axes:k?Array.from((O(),_).subarray(Number(k)>>>0,Number(N)>>>0)):[]})},1067437:f=>{t.$b("Tile",f,void 0)},1067489:(f,g,$)=>{t.$b("InstanceNormalization",f,{epsilon:g,format:$?"NHWC":"NCHW"})},1067603:(f,g,$)=>{t.$b("InstanceNormalization",f,{epsilon:g,format:$?"NHWC":"NCHW"})},1067717:f=>{t.$b("Range",f,void 0)},1067770:(f,g)=>{t.$b("Einsum",f,{equation:Xe(g)})},1067851:(f,g,$,v,E)=>{t.$b("Pad",f,{mode:g,value:$,pads:v?Array.from((O(),_).subarray(Number(v)>>>0,Number(E)>>>0)):[]})},1067994:(f,g,$,v,E,k)=>{t.$b("BatchNormalization",f,{epsilon:g,momentum:$,spatial:!!E,trainingMode:!!v,format:k?"NHWC":"NCHW"})},1068163:(f,g,$,v,E,k)=>{t.$b("BatchNormalization",f,{epsilon:g,momentum:$,spatial:!!E,trainingMode:!!v,format:k?"NHWC":"NCHW"})},1068332:(f,g,$)=>{t.$b("CumSum",f,{exclusive:Number(g),reverse:Number($)})},1068429:(f,g,$)=>{t.$b("DequantizeLinear",f,{axis:g,blockSize:$})},1068519:(f,g,$,v,E)=>{t.$b("GridSample",f,{align_corners:g,mode:Xe($),padding_mode:Xe(v),format:E?"NHWC":"NCHW"})},1068689:(f,g,$,v,E)=>{t.$b("GridSample",f,{align_corners:g,mode:Xe($),padding_mode:Xe(v),format:E?"NHWC":"NCHW"})},1068859:(f,g)=>{t.$b("ScatterND",f,{reduction:Xe(g)})},1068944:(f,g,$,v,E,k,N,L,Y)=>{t.$b("Attention",f,{numHeads:g,isUnidirectional:$,maskFilterValue:v,scale:E,doRotary:k,qkvHiddenSizes:N?Array.from((O(),_).subarray(Number(L)>>>0,Number(L)+N>>>0)):[],pastPresentShareBuffer:!!Y})},1069216:f=>{t.$b("BiasAdd",f,void 0)},1069271:f=>{t.$b("BiasSplitGelu",f,void 0)},1069332:f=>{t.$b("FastGelu",f,void 0)},1069388:(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne,ar,Cs)=>{t.$b("Conv",f,{format:xe?"NHWC":"NCHW",auto_pad:g,dilations:$?Array.from((O(),_).subarray(Number($)>>>0,Number(v)>>>0)):[],group:E,kernel_shape:k?Array.from((O(),_).subarray(Number(k)>>>0,Number(N)>>>0)):[],pads:L?Array.from((O(),_).subarray(Number(L)>>>0,Number(Y)>>>0)):[],strides:ee?Array.from((O(),_).subarray(Number(ee)>>>0,Number(de)>>>0)):[],w_is_const:()=>!!(O(),X)[Number(Ce)>>>0],activation:Xe(Ne),activation_params:ar?Array.from((O(),F).subarray(Number(ar)>>>0,Number(Cs)>>>0)):[]})},1069972:f=>{t.$b("Gelu",f,void 0)},1070024:(f,g,$,v,E,k,N,L,Y)=>{t.$b("GroupQueryAttention",f,{numHeads:g,kvNumHeads:$,scale:v,softcap:E,doRotary:k,rotaryInterleaved:N,smoothSoftmax:L,localWindowSize:Y})},1070241:(f,g,$,v)=>{t.$b("LayerNormalization",f,{axis:g,epsilon:$,simplified:!!v})},1070352:(f,g,$,v)=>{t.$b("LayerNormalization",f,{axis:g,epsilon:$,simplified:!!v})},1070463:(f,g,$,v,E,k)=>{t.$b("MatMulNBits",f,{k:g,n:$,accuracyLevel:v,bits:E,blockSize:k})},1070590:(f,g,$,v,E,k)=>{t.$b("MultiHeadAttention",f,{numHeads:g,isUnidirectional:$,maskFilterValue:v,scale:E,doRotary:k})},1070749:(f,g)=>{t.$b("QuickGelu",f,{alpha:g})},1070813:(f,g,$,v,E)=>{t.$b("RotaryEmbedding",f,{interleaved:!!g,numHeads:$,rotaryEmbeddingDim:v,scale:E})},1070952:(f,g,$)=>{t.$b("SkipLayerNormalization",f,{epsilon:g,simplified:!!$})},1071054:(f,g,$)=>{t.$b("SkipLayerNormalization",f,{epsilon:g,simplified:!!$})},1071156:(f,g,$,v)=>{t.$b("GatherBlockQuantized",f,{gatherAxis:g,quantizeAxis:$,blockSize:v})},1071277:f=>{t.Fd(f)},1071311:(f,g)=>t.Hd(Number(f),Number(g),t.Xc.Kd,t.Xc.errors)};function VS(f,g,$){return oc(async()=>{await t.Dd(Number(f),Number(g),Number($))})}function GS(){return typeof wasmOffsetConverter<"u"}function qS(f,g,$,v){var E=ge();try{return Lc(f,g,$,v)}catch(k){if(me(E),k!==k+0)throw k;we(1,0)}}function HS(f,g,$){var v=ge();try{return Bc(f,g,$)}catch(E){if(me(v),E!==E+0)throw E;we(1,0)}}function WS(f){var g=ge();try{Rc(f)}catch($){if(me(g),$!==$+0)throw $;we(1,0)}}function KS(f,g){var $=ge();try{return ks(f,g)}catch(v){if(me($),v!==v+0)throw v;we(1,0)}}function XS(f,g,$){var v=ge();try{Dc(f,g,$)}catch(E){if(me(v),E!==E+0)throw E;we(1,0)}}function ZS(f,g){var $=ge();try{Uc(f,g)}catch(v){if(me($),v!==v+0)throw v;we(1,0)}}function JS(f,g,$,v,E,k,N){var L=ge();try{return Fc(f,g,$,v,E,k,N)}catch(Y){if(me(L),Y!==Y+0)throw Y;we(1,0)}}function YS(f,g,$,v,E,k){var N=ge();try{Nc(f,g,$,v,E,k)}catch(L){if(me(N),L!==L+0)throw L;we(1,0)}}function QS(f,g,$,v){var E=ge();try{jc(f,g,$,v)}catch(k){if(me(E),k!==k+0)throw k;we(1,0)}}function eI(f,g,$,v,E){var k=ge();try{zc(f,g,$,v,E)}catch(N){if(me(k),N!==N+0)throw N;we(1,0)}}function tI(f,g,$,v,E,k,N){var L=ge();try{Gc(f,g,$,v,E,k,N)}catch(Y){if(me(L),Y!==Y+0)throw Y;we(1,0)}}function rI(f,g,$,v,E,k,N){var L=ge();try{qc(f,g,$,v,E,k,N)}catch(Y){if(me(L),Y!==Y+0)throw Y;we(1,0)}}function nI(f,g,$,v,E,k,N,L){var Y=ge();try{Xc(f,g,$,v,E,k,N,L)}catch(ee){if(me(Y),ee!==ee+0)throw ee;we(1,0)}}function iI(f,g,$,v,E){var k=ge();try{return Vc(f,g,$,v,E)}catch(N){if(me(k),N!==N+0)throw N;we(1,0)}}function oI(f,g,$){var v=ge();try{return Zc(f,g,$)}catch(E){if(me(v),E!==E+0)throw E;we(1,0)}}function sI(f,g,$,v,E,k,N,L){var Y=ge();try{Jc(f,g,$,v,E,k,N,L)}catch(ee){if(me(Y),ee!==ee+0)throw ee;we(1,0)}}function aI(f,g,$,v,E,k,N,L,Y,ee,de,xe){var Ce=ge();try{Hc(f,g,$,v,E,k,N,L,Y,ee,de,xe)}catch(Ne){if(me(Ce),Ne!==Ne+0)throw Ne;we(1,0)}}function uI(f,g,$){var v=ge();try{return Yc(f,g,$)}catch(E){if(me(v),E!==E+0)throw E;return we(1,0),0n}}function lI(f,g,$,v,E,k,N,L,Y){var ee=ge();try{Mc(f,g,$,v,E,k,N,L,Y)}catch(de){if(me(ee),de!==de+0)throw de;we(1,0)}}function dI(f){var g=ge();try{return Qc(f)}catch($){if(me(g),$!==$+0)throw $;we(1,0)}}function cI(f,g){var $=ge();try{return fp(f,g)}catch(v){if(me($),v!==v+0)throw v;return we(1,0),0n}}function pI(f){var g=ge();try{return ep(f)}catch($){if(me(g),$!==$+0)throw $;return we(1,0),0n}}function hI(f,g,$,v){var E=ge();try{return sp(f,g,$,v)}catch(k){if(me(E),k!==k+0)throw k;we(1,0)}}function fI(f,g,$,v,E){var k=ge();try{return ap(f,g,$,v,E)}catch(N){if(me(k),N!==N+0)throw N;we(1,0)}}function mI(f,g,$,v,E,k){var N=ge();try{return up(f,g,$,v,E,k)}catch(L){if(me(N),L!==L+0)throw L;we(1,0)}}function gI(f,g,$,v,E,k){var N=ge();try{return Wc(f,g,$,v,E,k)}catch(L){if(me(N),L!==L+0)throw L;we(1,0)}}function bI(f,g,$,v,E,k){var N=ge();try{return lp(f,g,$,v,E,k)}catch(L){if(me(N),L!==L+0)throw L;we(1,0)}}function yI(f,g,$,v,E,k,N,L){var Y=ge();try{return Kc(f,g,$,v,E,k,N,L)}catch(ee){if(me(Y),ee!==ee+0)throw ee;we(1,0)}}function wI(f,g,$,v,E){var k=ge();try{return dp(f,g,$,v,E)}catch(N){if(me(k),N!==N+0)throw N;return we(1,0),0n}}function _I(f,g,$,v){var E=ge();try{return cp(f,g,$,v)}catch(k){if(me(E),k!==k+0)throw k;we(1,0)}}function vI(f,g,$,v){var E=ge();try{return pp(f,g,$,v)}catch(k){if(me(E),k!==k+0)throw k;we(1,0)}}function xI(f,g,$,v,E,k,N,L,Y,ee,de,xe){var Ce=ge();try{return hp(f,g,$,v,E,k,N,L,Y,ee,de,xe)}catch(Ne){if(me(Ce),Ne!==Ne+0)throw Ne;we(1,0)}}function $I(f,g,$,v,E,k,N,L,Y,ee,de){var xe=ge();try{ip(f,g,$,v,E,k,N,L,Y,ee,de)}catch(Ce){if(me(xe),Ce!==Ce+0)throw Ce;we(1,0)}}function TI(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne,ar,Cs){var EI=ge();try{op(f,g,$,v,E,k,N,L,Y,ee,de,xe,Ce,Ne,ar,Cs)}catch(Ds){if(me(EI),Ds!==Ds+0)throw Ds;we(1,0)}}function SI(f,g,$){var v=ge();try{return tp(f,g,$)}catch(E){if(me(v),E!==E+0)throw E;we(1,0)}}function II(f,g,$){var v=ge();try{return rp(f,g,$)}catch(E){if(me(v),E!==E+0)throw E;we(1,0)}}function OI(f,g,$,v){var E=ge();try{np(f,g,$,v)}catch(k){if(me(E),k!==k+0)throw k;we(1,0)}}function Qi(){if(0{b=f,x=g})}var Rv,Wf,yA=R(()=>{"use strict";Rv=Hf,Wf=globalThis.self?.name?.startsWith("em-pthread"),Wf&&Hf()}),_a,al,Kf,pt,Nv,so,Xf,Zf,va,Jf,xa,zv,$a,Bv,Rl=R(()=>{"use strict";Dl(),_a=typeof location>"u"?void 0:location.origin,al=import.meta.url>"file:"&&import.meta.url<"file;",Kf=()=>{if(al){let e=URL;return new URL(new e("ort.all.bundle.min.mjs",import.meta.url).href,_a).href}return import.meta.url},pt=Kf(),Nv=()=>{if(pt&&!pt.startsWith("blob:"))return pt.substring(0,pt.lastIndexOf("/")+1)},so=(e,t)=>{try{let n=t??pt;return(n?new URL(e,n):new URL(e)).origin===_a}catch{return!1}},Xf=(e,t)=>{let n=t??pt;try{return(n?new URL(e,n):new URL(e)).href}catch{return}},Zf=(e,t)=>`${t??"./"}${e}`,va=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},Jf=async e=>(await import(e)).default,xa=(bA(),Tn(Pv)).default,zv=async()=>{if(!pt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(so(pt))return[void 0,xa()];let e=await va(pt);return[e,xa(e)]},$a=(yA(),Tn(Dv)).default,Bv=async(e,t,n,o)=>{let u=$a&&!(e||t);if(u)if(pt)u=so(pt)||o&&!n;else if(o&&!n)u=!0;else throw new Error("cannot determine the script source URL.");if(u)return[void 0,$a];{let l="ort-wasm-simd-threaded.jsep.mjs",d=e??Xf(l,t),c=n&&d&&!so(d,t),s=c?await va(d):d??Zf(l,t);return[c?s:void 0,await Jf(s)]}}}),Ta,ao,ti,Sa,Yf,Qf,em,Nl,Re,dn=R(()=>{"use strict";Rl(),ao=!1,ti=!1,Sa=!1,Yf=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Qf=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},em=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},Nl=async e=>{if(ao)return Promise.resolve();if(ti)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Sa)throw new Error("previous call to 'initializeWebAssembly()' failed.");ti=!0;let t=e.initTimeout,n=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!em())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!Qf())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let o=Yf();n>1&&!o&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+n+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=n=1);let u=e.wasmPaths,l=typeof u=="string"?u:void 0,d=u?.mjs,c=d?.href??d,s=u?.wasm,r=s?.href??s,i=e.wasmBinary,[a,p]=await Bv(c,l,n>1,!!i||!!r),h=!1,m=[];if(t>0&&m.push(new Promise(b=>{setTimeout(()=>{h=!0,b()},t)})),m.push(new Promise((b,x)=>{let w={numThreads:n};if(i)w.wasmBinary=i,w.locateFile=y=>y;else if(r||l)w.locateFile=y=>r??l+y;else if(c&&c.indexOf("blob:")!==0)w.locateFile=y=>new URL(y,c).href;else if(a){let y=Nv();y&&(w.locateFile=S=>y+S)}p(w).then(y=>{ti=!1,ao=!0,Ta=y,b(),a&&URL.revokeObjectURL(a)},y=>{ti=!1,Sa=!0,x(y)})})),await Promise.race(m),h)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},Re=()=>{if(ao&&Ta)return Ta;throw new Error("WebAssembly is not initialized yet.")}}),Nt,Ao,Ae,zl=R(()=>{"use strict";dn(),Nt=(e,t)=>{let n=Re(),o=n.lengthBytesUTF8(e)+1,u=n._malloc(o);return n.stringToUTF8(e,u,o),t.push(u),u},Ao=(e,t,n,o)=>{if(typeof e=="object"&&e!==null){if(n.has(e))throw new Error("Circular reference in options");n.add(e)}Object.entries(e).forEach(([u,l])=>{let d=t?t+u:u;if(typeof l=="object")Ao(l,d+".",n,o);else if(typeof l=="string"||typeof l=="number")o(d,l.toString());else if(typeof l=="boolean")o(d,l?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof l}`)})},Ae=e=>{let t=Re(),n=t.stackSave();try{let o=t.PTR_SIZE,u=t.stackAlloc(2*o);t._OrtGetLastError(u,u+o);let l=Number(t.getValue(u,o===4?"i32":"i64")),d=t.getValue(u+o,"*"),c=d?t.UTF8ToString(d):"";throw new Error(`${e} ERROR_CODE: ${l}, ERROR_MESSAGE: ${c}`)}finally{t.stackRestore(n)}}}),Mv,wA=R(()=>{"use strict";dn(),zl(),Mv=e=>{let t=Re(),n=0,o=[],u=e||{};try{if(e?.logSeverityLevel===void 0)u.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)u.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(u.terminate=!1);let l=0;return e?.tag!==void 0&&(l=Nt(e.tag,o)),n=t._OrtCreateRunOptions(u.logSeverityLevel,u.logVerbosityLevel,!!u.terminate,l),n===0&&Ae("Can't create run options."),e?.extra!==void 0&&Ao(e.extra,"",new WeakSet,(d,c)=>{let s=Nt(d,o),r=Nt(c,o);t._OrtAddRunConfigEntry(n,s,r)!==0&&Ae(`Can't set a run config entry: ${d} - ${c}.`)}),[n,o]}catch(l){throw n!==0&&t._OrtReleaseRunOptions(n),o.forEach(d=>t._free(d)),l}}}),tm,rm,nm,Dr,im,Fv,_A=R(()=>{"use strict";dn(),zl(),tm=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},rm=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},nm=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(n=>(typeof n=="string"?n:n.name)==="webgpu")&&(e.enableMemPattern=!1)},Dr=(e,t,n,o)=>{let u=Nt(t,o),l=Nt(n,o);Re()._OrtAddSessionConfigEntry(e,u,l)!==0&&Ae(`Can't set a session config entry: ${t} - ${n}.`)},im=async(e,t,n)=>{let o=t.executionProviders;for(let u of o){let l=typeof u=="string"?u:u.name,d=[];switch(l){case"webnn":if(l="WEBNN",Dr(e,"session.disable_quant_qdq","1",n),Dr(e,"session.disable_qdq_constant_folding","1",n),typeof u!="string"){let a=u?.deviceType;a&&Dr(e,"deviceType",a,n)}break;case"webgpu":if(l="JS",typeof u!="string"){let a=u;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);Dr(e,"preferredLayout",a.preferredLayout,n)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${l}`)}let c=Nt(l,n),s=d.length,r=0,i=0;if(s>0){r=Re()._malloc(s*Re().PTR_SIZE),n.push(r),i=Re()._malloc(s*Re().PTR_SIZE),n.push(i);for(let a=0;a{let t=Re(),n=0,o=[],u=e||{};nm(u);try{let l=tm(u.graphOptimizationLevel??"all"),d=rm(u.executionMode??"sequential"),c=typeof u.logId=="string"?Nt(u.logId,o):0,s=u.logSeverityLevel??2;if(!Number.isInteger(s)||s<0||s>4)throw new Error(`log severity level is not valid: ${s}`);let r=u.logVerbosityLevel??0;if(!Number.isInteger(r)||r<0||r>4)throw new Error(`log verbosity level is not valid: ${r}`);let i=typeof u.optimizedModelFilePath=="string"?Nt(u.optimizedModelFilePath,o):0;if(n=t._OrtCreateSessionOptions(l,!!u.enableCpuMemArena,!!u.enableMemPattern,d,!!u.enableProfiling,0,c,s,r,i),n===0&&Ae("Can't create session options."),u.executionProviders&&await im(n,u,o),u.enableGraphCapture!==void 0){if(typeof u.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${u.enableGraphCapture}`);Dr(n,"enableGraphCapture",u.enableGraphCapture.toString(),o)}if(u.freeDimensionOverrides)for(let[a,p]of Object.entries(u.freeDimensionOverrides)){if(typeof a!="string")throw new Error(`free dimension override name must be a string: ${a}`);if(typeof p!="number"||!Number.isInteger(p)||p<0)throw new Error(`free dimension override value must be a non-negative integer: ${p}`);let h=Nt(a,o);t._OrtAddFreeDimensionOverride(n,h,p)!==0&&Ae(`Can't set a free dimension override: ${a} - ${p}.`)}return u.extra!==void 0&&Ao(u.extra,"",new WeakSet,(a,p)=>{Dr(n,a,p,o)}),[n,o]}catch(l){throw n!==0&&t._OrtReleaseSessionOptions(n)!==0&&Ae("Can't release session options."),o.forEach(d=>t._free(d)),l}}}),Yr,Jt,Qr,Go,ko,Bl,Ml,ul,se=R(()=>{"use strict";Yr=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},Jt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},Qr=(e,t)=>{let n=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],o=typeof t=="number"?t:t.reduce((u,l)=>u*l,1);return n>0?Math.ceil(o*n):void 0},Go=e=>{switch(e){case"float16":return typeof Float16Array<"u"?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},ko=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Bl=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Ml=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",ul=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),Fl,jv=R(()=>{"use strict";Dl(),Fl=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let n=t.headers.get("Content-Length"),o=n?parseInt(n,10):0;if(o<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let u=t.body.getReader(),l;try{l=new ArrayBuffer(o)}catch(c){if(c instanceof RangeError){let s=Math.ceil(o/65536);l=new WebAssembly.Memory({initial:s,maximum:s}).buffer}else throw c}let d=0;for(;;){let{done:c,value:s}=await u.read();if(c)break;let r=s.byteLength;new Uint8Array(l,d,r).set(s),d+=r}return new Uint8Array(l,0,o)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),om,sm,am,um,jl,lm,ve,Qt=R(()=>{"use strict";se(),om=["V","I","W","E","F"],sm=(e,t)=>{console.log(`[${om[e]},${new Date().toISOString()}]${t}`)},jl=(e,t)=>{am=e,um=t},lm=(e,t)=>{let n=ko(e),o=ko(am);n>=o&&sm(n,typeof t=="function"?t():t)},ve=(...e)=>{um&&lm(...e)}}),dm,In,z,Po,Lv,Uv,Vv,ae=R(()=>{"use strict";dm=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},In=class{static calcShape(e,t,n=!1){let o=e.length,u=t.length;if(o===0)return t;if(u===0)return e;let l=Math.max(e.length,t.length),d=new Array(l);if(n){if(o<2||u<2)return;let c=dm.calcMatMulShape([e[o-2],e[o-1]],[t[u-2],t[u-1]]);if(c===void 0)return;[d[l-2],d[l-1]]=c}for(let c=n?3:1;c<=l;c++){let s=o-c<0?1:e[o-c],r=u-c<0?1:t[u-c];if(s!==r&&s>1&&r>1)return;let i=Math.max(s,r);if(s&&r)d[l-c]=Math.max(s,r);else{if(i>1)return;d[l-c]=0}}return d}static isValidBroadcast(e,t){let n=e.length,o=t.length;if(n>o)return!1;for(let u=1;u<=n;u++)if(e[n-u]!==1&&e[n-u]!==t[o-u])return!1;return!0}},z=class xo{static size(t){return xo.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,n=4){let o=t.length;if(o===0)return[];let u=new Array(o),l=o-1;for(;l>=0;){if(t[l]%n===0){u[l]=t[l]/n;break}if(n%t[l]!==0)throw new Error("cannot convert shape");u[l]=1,n/=t[l],l--}for(l--;l>=0;l--)u[l]=t[l];return u}static sizeFromDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return xo.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return xo.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,o){let u=1;for(let l=n;l=0;--u)o[u]=o[u+1]*t[u+1];return o}static normalizeAxis(t,n){if(t<-n&&t>=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(o=>this.normalizeAxis(o,n??t.length))}static sortBasedOnPerm(t,n){return n?n.map(o=>t[o]):t.slice().reverse()}static padShape(t,n){let o=t.length;return t.map((u,l)=>u+n[l]+n[l+o])}static areEqual(t,n){return t.length!==n.length?!1:t.every((o,u)=>o===n[u])}},Po=class wr{static adjustPoolAttributes(t,n,o,u,l,d){if(!t&&o.length!==n.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let c=0;c=o.length?o.push(n[c+2]):o[c]=n[c+2];for(let c=0;c=o[c]||d[c+o.length]>=o[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,o,u,l,d,c){if(c){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let s=0;s=o+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,o,u,l,d,c,s,r=0){let i=o*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,wr.computeOutputSize(t-i,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(o!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],wr.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return wr.computeOutputSize(t+l[d]+l[c]-i,n,t,l[d],r)}},Lv=class{static getShapeOfGemmResult(e,t,n,o,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(o?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!In.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},Uv=-34028234663852886e22,Vv=34028234663852886e22}),Ll,Gv=R(()=>{"use strict";se(),Ll=(e,t)=>new(Go(t))(e)}),Ia,cm,Oa,pm,Ea,hm,Aa,ka,Pa,fm,qv,vA=R(()=>{"use strict";se(),Qt(),Ia=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),cm=(e,t)=>{if(t==="int32")return e;let n=Ia.get(t);if(!n)throw new Error(`WebNN backend does not support data type: ${t}`);let o=n/8;if(e.byteLength%o!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${o}.`);let u=e.byteLength/o,l=new(Go(t))(e.buffer,e.byteOffset,u);switch(t){case"int64":case"uint64":{let d=new Int32Array(u);for(let c=0;c2147483647n||s<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");d[c]=Number(s)}return new Uint8Array(d.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&l.some(c=>c>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let d=Int32Array.from(l,Number);return new Uint8Array(d.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},Oa=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let n=e.byteLength/4,o=new Int32Array(e.buffer,e.byteOffset,n);switch(t){case"int64":{let u=BigInt64Array.from(o,BigInt);return new Uint8Array(u.buffer)}case"uint64":{if(o.some(l=>l<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let u=BigUint64Array.from(o,BigInt);return new Uint8Array(u.buffer)}case"int8":{if(o.some(l=>l<-128||l>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let u=Int8Array.from(o,Number);return new Uint8Array(u.buffer)}case"uint8":{if(o.some(u=>u<0||u>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(o,Number)}case"uint32":{if(o.some(l=>l<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let u=Uint32Array.from(o,Number);return new Uint8Array(u.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},pm=1,Ea=()=>pm++,hm=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),Aa=(e,t)=>{let n=Ia.get(e);if(!n)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((o,u)=>o*u)*n/8):0},ka=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:n,tensor:o,dataType:u,shape:l,fallbackDataType:d}=e;this.sessionId=t,this.mlContext=n,this.mlTensor=o,this.dataType=u,this.tensorShape=l,this.fallbackDataType=d}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return Aa(this.dataType,this.tensorShape)}destroy(){ve("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),n=Oa(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(n);return}else return new Uint8Array(n).buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,n){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===n.length&&this.tensorShape.every((o,u)=>o===n[u])}setIsDataConverted(e){this.isDataConverted=e}},Pa=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(e,t,n,o){let u=this.tensorManager.getMLContext(e),l=this.tensorManager.getMLOpSupportLimits(e),d;if(!l?.input.dataTypes.includes(t)){if(d=hm.get(t),!d||l?.input.dataTypes.includes(d))throw new Error(`WebNN backend does not support data type: ${t}`);ve("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${d}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(u,t,n))return this.wrapper.tensor;if(o){if(this.wrapper.byteLength!==Aa(t,n))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let c=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,n,c,!0,!0,d),o&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=cm(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else ve("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){if(this.activeUpload){let t=this.wrapper?.isDataConverted?Oa(this.activeUpload,this.wrapper?.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(t):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(t);return}else return t.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},fm=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=Ea();return this.tensorTrackersById.set(e,new Pa(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,n,o,u){ve("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${n}, shape: ${o}, copyOld: ${u}}`);let l=this.tensorTrackersById.get(t);if(!l)throw new Error("Tensor not found.");return l.ensureTensor(e,n,o,u)}upload(e,t){let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");n.upload(t)}async download(e,t){ve("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t?.byteLength}}`);let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");return n.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}registerTensor(e,t,n,o){let u=this.getMLContext(e),l=Ea(),d=new ka({sessionId:e,context:u,tensor:t,dataType:n,shape:o});return this.tensorTrackersById.set(l,new Pa(this,d)),this.externalTensors.add(d),l}async getCachedTensor(e,t,n,o,u,l,d){let c=this.getMLContext(e);for(let[r,i]of this.freeTensors.entries())if(i.canReuseTensor(c,t,n)){ve("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}`);let a=this.freeTensors.splice(r,1)[0];return a.sessionId=e,a}ve("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}}`);let s=await c.createTensor({dataType:d??t,shape:n,dimensions:n,usage:o,writable:u,readable:l});return new ka({sessionId:e,context:c,tensor:s,dataType:t,shape:n,fallbackDataType:d})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},qv=(...e)=>new fm(...e)}),ri,mm,Hv,xA=R(()=>{"use strict";se(),dn(),Gv(),vA(),Qt(),ri=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),mm=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let n=Object.keys(e).sort(),o=Object.keys(t).sort();return n.length===o.length&&n.every((u,l)=>u===o[l]&&e[u]===t[u])},Hv=class{constructor(e){this.tensorManager=qv(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,jl(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){ve("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){ve("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let n of t)ve("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${n}}`),this.tensorManager.releaseTensorId(n);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let n=this.mlContextCache.findIndex(o=>o.gpuDevice===e);if(n!==-1)return this.mlContextCache[n].mlContext;{let o=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:o}),o}}else if(e===void 0){let n=this.mlContextCache.findIndex(o=>o.options===void 0&&o.gpuDevice===void 0);if(n!==-1)return this.mlContextCache[n].mlContext;{let o=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:o}),o}}let t=this.mlContextCache.findIndex(n=>mm(n.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let n=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:n}),n}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let n=this.sessionIdsByMLContext.get(t);n||(n=new Set,this.sessionIdsByMLContext.set(t,n)),n.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let n=this.sessionIdsByMLContext.get(t);if(n.delete(e),n.size===0){this.sessionIdsByMLContext.delete(t);let o=this.mlContextCache.findIndex(u=>u.mlContext===t);o!==-1&&this.mlContextCache.splice(o,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){ve("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,n,o,u){let l=ri.get(n);if(!l)throw new Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,l,o,u)}async createTemporaryTensor(e,t,n){ve("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${n}}`);let o=ri.get(t);if(!o)throw new Error(`Unsupported ONNX data type: ${t}`);let u=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,u,o,n,!1);let l=this.temporarySessionTensorIds.get(e);return l?l.push(u):this.temporarySessionTensorIds.set(e,[u]),u}uploadTensor(e,t){if(!Re().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");ve("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let n=await this.tensorManager.download(e);return Ll(n,t)}}registerMLTensor(e,t,n,o){let u=ri.get(n);if(!u)throw new Error(`Unsupported ONNX data type: ${n}`);let l=this.tensorManager.registerTensor(e,t,u,o);return ve("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${u}, dimensions: ${o}} -> {tensorId: ${l}}`),l}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let n=this.sessionGraphInputs.get(e);return n?n.includes(t):!1}isGraphOutput(e,t){let n=this.sessionGraphOutputs.get(e);return n?n.includes(t):!1}isGraphInputOutputTypeSupported(e,t,n=!0){let o=ri.get(Yr(t)),u=this.mlOpSupportLimitsBySessionId.get(e);return typeof o>"u"?!1:n?!!u?.input.dataTypes.includes(o):!!u?.output.dataTypes.includes(o)}flush(){}}}),Ul=R(()=>{"use strict"}),Ca,uo,lo,gm,bm,Da,ll,ym,Wv,$A=R(()=>{"use strict";Qt(),Ul(),Ca=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),uo=[],lo=e=>Math.ceil(Number(e)/16)*16,gm=e=>{for(let t=0;tbm++,ll=async(e,t,n,o)=>{let u=lo(n),l=e.device.createBuffer({size:u,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let d=e.getCommandEncoder();e.endComputePass(),d.copyBufferToBuffer(t,0,l,0,u),e.flush(),await l.mapAsync(GPUMapMode.READ);let c=l.getMappedRange();if(o){let s=o();return s.set(new Uint8Array(c,0,n)),s}else return new Uint8Array(c.slice(0,n))}finally{l.destroy()}},ym=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of Ca)uo.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let n=t.buffer,o=t.byteOffset,u=t.byteLength,l=lo(u),d=this.storageCache.get(e);if(!d)throw new Error("gpu data for uploading does not exist");if(Number(d.originalSize)!==u)throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${u}`);if(l===u&&o%4===0)this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,n,o,u);else{let c=new Uint8Array(l);c.set(t),this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,c,0,l)}ve("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let n=this.storageCache.get(e);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(t);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let u=lo(n.originalSize),l=this.backend.getCommandEncoder();this.backend.endComputePass(),l.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,u)}registerExternalBuffer(e,t,n){let o;if(n){if(o=n[0],e===n[1])return ve("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. - Please use the previous external buffer!`)}else o=Da();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:e},originalSize:t}),ve("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${o}, registered.`),o}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),ve("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=gm(e),o,u=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,l=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(u||l){let c=(u?this.freeBuffers:this.freeUniformBuffers).get(n);c?c.length>0?o=c.pop():o=this.backend.device.createBuffer({size:n,usage:t}):o=this.backend.device.createBuffer({size:n,usage:t})}else o=this.backend.device.createBuffer({size:n,usage:t});let d={id:Da(),type:0,buffer:o};return this.storageCache.set(d.id,{gpuData:d,originalSize:Number(e)}),ve("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${d.id}`),d}get(e){return this.storageCache.get(e)?.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,n=this.storageCache.get(t);if(!n){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return ve("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${n.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(n.gpuData.buffer),n.originalSize}async download(e,t){let n=this.storageCache.get(Number(e));if(!n)throw new Error("data does not exist");await ll(this.backend,n.gpuData.buffer,n.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=Ca.get(e.size);if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(ve("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(n=>{n.gpuData.buffer.destroy()}),this.storageCache=new Map)}},Wv=(...e)=>new ym(...e)}),wm,Se,Ve=R(()=>{"use strict";wm=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},Se=e=>new wm(e)}),On,co,Ye,Je,oe,Ue,dl,$n,vr,ne,ni,j,re,Kv,Vl,_m,Xv,ue=R(()=>{"use strict";se(),ae(),On=64,co=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},Ye=(e,t=1)=>{let n=co(e,t);return typeof n=="string"?n:n[0]},Je=(e,t=1)=>{let n=co(e,t);return typeof n=="string"?n:n[1]},oe=(...e)=>{let t=[];return e.forEach(n=>{n.length!==0&&t.push({type:12,data:n},{type:12,data:z.computeStrides(n)})}),t},Ue=e=>e%4===0?4:e%2===0?2:1,dl=(e="f32",t,n="0")=>!t||t===1?`${e}(${n})`:`vec${t}<${e}>(${n})`,$n=(e,t,n)=>e==="f32"?n:t===1?`f32(${n})`:`vec${t}(${n})`,vr=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,ne=(e,t,n,o)=>e.startsWith("uniforms.")&&n>4?typeof t=="string"?o==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:o==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:n>1?`${e}[${t}]`:e,ni=(e,t,n,o,u)=>{let l=typeof n=="number",d=l?n:n.length,c=[...new Array(d).keys()],s=d<2?"u32":d<=4?`vec${d}`:`array`,r=co(t,u),i=typeof r=="string"?r:r[1],a=typeof r=="string"?r:r[0],p={indices:s,value:i,storage:a,tensor:t},h=M=>typeof M=="string"?M:`${M}u`,m={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},b=l?"uniforms.":"",x=`${b}${e}_shape`,w=`${b}${e}_strides`,y="";for(let M=0;Mthis.isTimerResultAvailable(e)),this.getTimerResult(e)}async createAndWaitForFence(){let e=this.createFence(this.gl);return this.pollFence(e)}createFence(e){let t,n=e,i=n.fenceSync(n.SYNC_GPU_COMMANDS_COMPLETE,0);return e.flush(),i===null?t=()=>!0:t=()=>{let u=n.clientWaitSync(i,0,0);return u===n.ALREADY_SIGNALED||u===n.CONDITION_SATISFIED},{query:i,isFencePassed:t}}async pollFence(e){return new Promise(t=>{this.addItemToPoll(()=>e.isFencePassed(),()=>t())})}pollItems(){let e=hA(this.itemsToPoll.map(t=>t.isDoneFn));for(let t=0;t<=e;++t){let{resolveFn:n}=this.itemsToPoll[t];n()}this.itemsToPoll=this.itemsToPoll.slice(e+1)}async addItemToPoll(e,t){this.itemsToPoll.push({isDoneFn:e,resolveFn:t}),!(this.itemsToPoll.length>1)&&await Vp(()=>(this.pollItems(),this.itemsToPoll.length===0))}}});function Ev(e){let t;if((!e||e==="webgl2")&&"webgl2"in rn?t=rn.webgl2:(!e||e==="webgl")&&"webgl"in rn&&(t=rn.webgl),!t)try{let i=gA();t=Wf(i,e)}catch{let i=mA();t=Wf(i,e)}e=e||t.version===1?"webgl":"webgl2";let n=t.gl;return rn[e]=t,n.isContextLost()?(delete rn[e],Ev(e)):(n.disable(n.DEPTH_TEST),n.disable(n.STENCIL_TEST),n.disable(n.BLEND),n.disable(n.DITHER),n.disable(n.POLYGON_OFFSET_FILL),n.disable(n.SAMPLE_COVERAGE),n.enable(n.SCISSOR_TEST),n.enable(n.CULL_FACE),n.cullFace(n.BACK),t)}function Wf(e,t){let n={alpha:!1,depth:!1,antialias:!1,stencil:!1,preserveDrawingBuffer:!1,premultipliedAlpha:!1,failIfMajorPerformanceCaveat:!1},i,u=n;if((!t||t==="webgl2")&&(i=e.getContext("webgl2",u),i))try{return new ul(i,2)}catch(l){Ye.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl2'. Error: ${l}`)}if((!t||t==="webgl")&&(i=e.getContext("webgl",u)||e.getContext("experimental-webgl",u),i))try{return new ul(i,1)}catch(l){Ye.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl' or 'experimental-webgl'. Error: ${l}`)}throw new Error("WebGL is not supported")}function mA(){if(typeof document>"u")throw new TypeError("failed to create canvas: document is not supported");let e=document.createElement("canvas");return e.width=1,e.height=1,e}function gA(){if(typeof OffscreenCanvas>"u")throw new TypeError("failed to create offscreen canvas: OffscreenCanvas is not supported");return new OffscreenCanvas(1,1)}var rn,bA=N(()=>{"use strict";Gt(),fA(),rn={}}),Av,yA=N(()=>{"use strict";dt(),Gt(),pA(),bA(),Av=class{get contextId(){return me.webgl.contextId}set contextId(e){me.webgl.contextId=e}get matmulMaxBatchSize(){return me.webgl.matmulMaxBatchSize}set matmulMaxBatchSize(e){me.webgl.matmulMaxBatchSize=e}get textureCacheMode(){return me.webgl.textureCacheMode}set textureCacheMode(e){me.webgl.textureCacheMode=e}get pack(){return me.webgl.pack}set pack(e){me.webgl.pack=e}get async(){return me.webgl.async}set async(e){me.webgl.async=e}initialize(){try{return this.glContext=Ev(this.contextId),typeof this.matmulMaxBatchSize!="number"&&(this.matmulMaxBatchSize=16),typeof this.textureCacheMode!="string"&&(this.textureCacheMode="full"),typeof this.pack!="boolean"&&(this.pack=!1),typeof this.async!="boolean"&&(this.async=!1),Ye.setWithEnv(me),me.webgl.context||Object.defineProperty(me.webgl,"context",{value:this.glContext.gl}),Ye.verbose("WebGLBackend",`Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${this.matmulMaxBatchSize}; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${this.async}.`),!0}catch(e){return Ye.warning("WebGLBackend",`Unable to initialize WebGLBackend. ${e}`),!1}}createSessionHandler(e){return new Ov(this,e)}dispose(){this.glContext.dispose()}}});async function kv(e){if(e){let t=typeof e=="string"?[e]:e;for(let n of t){let i=zl.get(n);if(i)return i;let u=await wA(n);if(u)return u}}else return kv(["webgl"]);throw new Error("no available backend to use")}async function wA(e){let t=Pv;if(typeof t[e]<"u"&&_A(t[e])){let n=t[e],i=n.initialize();if(typeof i=="object"&&"then"in i&&(i=await i),i)return zl.set(e,n),n}}function _A(e){let t=e;return"initialize"in t&&typeof t.initialize=="function"&&"createSessionHandler"in t&&typeof t.createSessionHandler=="function"&&"dispose"in t&&typeof t.dispose=="function"}var zl,Pv,vA=N(()=>{"use strict";yA(),zl=new Map,Pv={webgl:new Av}}),Kf,Cv,xA=N(()=>{"use strict";Gt(),Kf=class{constructor(e,t){this.op=e,this.node=t}},Cv=class{constructor(e,t,n){this.graph=e,this.profiler=n,this.initialize(t)}initialize(e){this.profiler.event("session","ExecutionPlan.initialize",()=>{let t=this.graph.getNodes();if(t.length!==e.length)throw new Error("The size of nodes and OPs do not match.");this._ops=e.map((n,i)=>new Kf(n,t[i])),this.reset(),this._starter=[],this._ops.forEach((n,i)=>{let u=!0;for(let l of n.node.inputs)if(!this._values[l]&&this.graph.getInputIndices().indexOf(l)===-1){u=!1;break}u&&this._starter.push(i)})})}reset(){this._values=this.graph.getValues().map(e=>e.tensor)}async execute(e,t){return this.profiler.event("session","ExecutionPlan.execute",async()=>{this.reset();let n=e.createInferenceHandler(),i=this.graph.getInputIndices();if(t.length!==i.length)throw new Error(`number of input tensors don't match the number of inputs to the model: actual: ${t.length} expected: ${i.length}`);t.forEach((r,o)=>{let a=i[o];this._values[a]=r});let u=this._starter.slice(0),l=this.graph.getValues(),d=this.graph.getNodes(),c=0;for(;cthis._values[g]);if(a.indexOf(void 0)!==-1)throw new Error(`unresolved input detected: op: ${o.node}`);let p=a;Ye.verbose("ExecPlan",`Running op:${o.node.name} (${p.map((g,v)=>`'${o.node.inputs[v]}': ${g.type}[${g.dims.join(",")}]`).join(", ")})`);let h=await this.profiler.event("node",o.node.name,async()=>o.op.impl(n,p,o.op.context));if(h.length!==o.node.outputs.length)throw new Error("the size of output does not match model definition.");h.forEach((g,v)=>{let w=o.node.outputs[v];if(this._values[w])throw new Error(`output [${w}] already has value: op:${o.node.name}`);this._values[w]=g});let m=new Set;h.forEach((g,v)=>{let w=o.node.outputs[v];for(let y of l[w].to){let T=d[y],S=!0;for(let O of T.inputs)if(!this._values[O]){S=!1;break}S&&m.add(y)}}),u.push(...m)}let s=[];for(let r=0;r{"use strict";Ko(),Se=be(Ni()),zn(),Ne(),ll=class $i{constructor(t){if(this._attributes=new Map,t!=null){for(let n of t)n instanceof Se.onnx.AttributeProto?this._attributes.set(n.name,[$i.getValue(n),$i.getType(n)]):n instanceof Bu.Attribute&&this._attributes.set(n.name(),[$i.getValue(n),$i.getType(n)]);if(this._attributes.sizevt.fromProto(u));if(t instanceof Bu.Attribute)return i.map(u=>vt.fromOrtTensor(u))}return n===Se.onnx.AttributeProto.AttributeType.STRING&&t instanceof Se.onnx.AttributeProto?Fu(i):n===Se.onnx.AttributeProto.AttributeType.STRINGS&&t instanceof Se.onnx.AttributeProto?i.map(Fu):i}static getValueNoCheck(t){return t instanceof Se.onnx.AttributeProto?this.getValueNoCheckFromOnnxFormat(t):this.getValueNoCheckFromOrtFormat(t)}static getValueNoCheckFromOnnxFormat(t){switch(t.type){case Se.onnx.AttributeProto.AttributeType.FLOAT:return t.f;case Se.onnx.AttributeProto.AttributeType.INT:return t.i;case Se.onnx.AttributeProto.AttributeType.STRING:return t.s;case Se.onnx.AttributeProto.AttributeType.TENSOR:return t.t;case Se.onnx.AttributeProto.AttributeType.GRAPH:return t.g;case Se.onnx.AttributeProto.AttributeType.FLOATS:return t.floats;case Se.onnx.AttributeProto.AttributeType.INTS:return t.ints;case Se.onnx.AttributeProto.AttributeType.STRINGS:return t.strings;case Se.onnx.AttributeProto.AttributeType.TENSORS:return t.tensors;case Se.onnx.AttributeProto.AttributeType.GRAPHS:return t.graphs;default:throw new Error(`unsupported attribute type: ${Se.onnx.AttributeProto.AttributeType[t.type]}`)}}static getValueNoCheckFromOrtFormat(t){switch(t.type()){case Lt.AttributeType.FLOAT:return t.f();case Lt.AttributeType.INT:return t.i();case Lt.AttributeType.STRING:return t.s();case Lt.AttributeType.TENSOR:return t.t();case Lt.AttributeType.GRAPH:return t.g();case Lt.AttributeType.FLOATS:return t.floatsArray();case Lt.AttributeType.INTS:{let n=[];for(let i=0;i{"use strict";$A(),Ko(),_a=be(Ni()),zn(),Ne(),dl={from:(e,t)=>new Xf(e,t)},rr=class{constructor(e){this._from=void 0,this._to=[],this.tensor=void 0,this.type=void 0,e&&(this.type=yt.tensorValueTypeFromProto(e.type.tensorType))}get from(){return this._from}get to(){return this._to}},va=class{constructor(e,t){e instanceof _a.onnx.NodeProto?(this.name=e.name,this.opType=e.opType,this.attributes=new ll(e.attribute)):e instanceof G0.Node&&(this.name=t??e.name(),this.opType=e.opType(),this.attributes=new ll(yt.tensorAttributesFromORTFormat(e))),this.inputs=[],this.outputs=[],this.executeNode=!0}},Xf=class{constructor(e,t){if(!e)throw new TypeError("graph is empty");this.buildGraph(e),this.transformGraph(t),this.checkIsAcyclic()}getInputIndices(){return this._allInputIndices}getInputNames(){return this._allInputNames}getOutputIndices(){return this._allOutputIndices}getOutputNames(){return this._allOutputNames}getValues(){return this._allData}getNodes(){return this._nodes}buildGraph(e){if(e instanceof _a.onnx.GraphProto)this.buildGraphFromOnnxFormat(e);else if(e instanceof U0.Graph)this.buildGraphFromOrtFormat(e);else throw new TypeError("Graph type is not supported.")}buildGraphFromOnnxFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map;if(!e.input)throw new Error("missing information in graph: input");let i=[];for(let u of e.input){if(t.has(u.name))throw new Error(`duplicated input name: ${u.name}`);let l=this._allData.push(new rr(u))-1;t.set(u.name,l),i.push(u.name)}if(!e.initializer)throw new Error("missing information in graph: initializer");for(let u of e.initializer){let l=t.get(u.name);if(l===void 0){let d=new rr;d.type={shape:{dims:yt.tensorDimsFromProto(u.dims)},tensorType:yt.tensorDataTypeFromProto(u.dataType)},l=this._allData.push(d)-1,t.set(u.name,l)}this._allData[l]._from=-1,this._allData[l].tensor=vt.fromProto(u)}for(let u=0;u"u"&&(s=this._allData.push(new rr)-1,t.set(c,s)),l.outputs.push(s),this._allData[s]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${s}`);if(this._allData[s]._from=u,d.opType==="Constant"){if(!d.attribute||d.attribute.length!==1||!d.attribute[0].t)throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(!d.output||d.output.length!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[s]._from=-1,this._allData[s].tensor=vt.fromProto(d.attribute[0].t)}}}for(let u=0;u"u"){if(c===""&&(d.input.length===3||d.input.length===4)&&d.opType==="Resize")continue;throw new Error(`unrecognized input '${c}' for node: ${d.name}`)}l.inputs.push(s),this._allData[s]._to.push(u)}}return!0}buildGraphFromOrtFormat(e){let t=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let n=new Map,i=[];for(let u=0;u"u"&&(r=this._allData.push(new rr)-1,t.set(s,r)),l.outputs.push(r),this._allData[r]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${r}`);if(this._allData[r]._from=u,d.opType()==="Constant"){if(d.attributesLength()!==1||!d.attributes(0).t())throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(d.outputsLength()!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");l.outputs.pop(),l.executeNode=!1,this._allData[r]._from=-1,this._allData[r].tensor=vt.fromOrtTensor(d.attributes(0).t())}}}for(let u=0;u"u")throw new Error(`unrecognized input '${s}' for node: ${d.name()}`);l.inputs.push(r),this._allData[r]._to.push(u)}}}checkIsAcyclic(){let e=new Set;this._allInputIndices.forEach(i=>{this._allData[i]._to.forEach(u=>{e.add(u)})});let t=Array.from(e),n=new Array(this._nodes.length).fill("white");for(;t.length>0;){let i=t.pop();n[i]==="gray"?n[i]="black":(t.push(i),n[i]="gray",this._nodes[i].outputs.forEach(u=>{let l=this._allData[u];if(typeof l.tensor<"u")throw new Error("node outputs should not be initialized");if(l._from!==i)throw new Error("from property of the Value object doesn't match index of Node being processed");l._to.forEach(d=>{if(n[d]==="gray")throw new Error("model graph is cyclic");n[d]==="white"&&t.push(d)})}))}}transformGraph(e){this.removeAllIdentityNodes(),this.removeAllDropoutNodes(),this.fuseConvActivationNodes(),e&&e.transformGraph(this),this.finalizeGraph()}finalizeGraph(){let e=0,t=new Array(this._nodes.length,0),n=0;for(let i=0;i{this._allData[u]._from=-2});this._nodes.splice(n,this._nodes.length-n);for(let i=0;i=0)u._to[l]=t[u._to[l]];else throw new Error("Trying to update a removed node")}e=0;for(let i=0;i0){let u=-1;this._allData[i].from!==void 0&&this._allData[i].from!==-1?(u=this._nodes[this._allData[i].from].outputs.indexOf(i+e),u!==-1&&(this._nodes[this._allData[i].from].outputs[u]=i)):(u=this._allInputIndices.indexOf(i+e),u!==-1&&(this._allInputIndices[u]=i)),this._allData[i].to.forEach(l=>{u=this._nodes[l].inputs.indexOf(i+e),u!==-1&&(this._nodes[l].inputs[u]=i)}),this._allData[i].to.length===0&&(u=this._allOutputIndices.indexOf(i+e),u!==-1&&(this._allOutputIndices[u]=i))}}}deleteNode(e){let t=this._nodes[e];if(t.outputs.length>1){for(let d=1;d0)throw new Error("Node deletion with more than one output connected to other nodes is not supported. ")}t.executeNode=!1;let n=t.inputs[0],i=t.outputs[0],u=this._allData[i].to;for(let d=0;d0)for(let d of u){let c=this._nodes[d].inputs.indexOf(i);if(c===-1)throw new Error("The Node object doesn't have the output Value in it's 'inputs' property ");this._nodes[d].inputs[c]=n,this._allData[n].to.push(d)}}removeAllDropoutNodes(){let e=0;for(let t of this._nodes){if(t.opType==="Dropout"){if(t.inputs.length!==1)throw new Error("Dropout nodes should only contain one input. ");if(t.outputs.length!==1&&t.outputs.length!==2)throw new Error("Dropout nodes should contain either 1 or 2 output(s)");if(t.outputs.length===2&&this._allData[t.outputs[1]]._to.length!==0)throw new Error("Dropout nodes's second output should not be referenced by other nodes");this.deleteNode(e)}e++}}removeAllIdentityNodes(){let e=0;for(let t of this._nodes)t.opType==="Identity"&&this.deleteNode(e),e++}isActivation(e){switch(e.opType){case"Relu":case"Sigmoid":case"Clip":return!0;default:return!1}}fuseConvActivationNodes(){for(let e of this._nodes)if(e.opType==="Conv"){let t=this._allData[e.outputs[0]]._to;if(t.length===1&&this.isActivation(this._nodes[t[0]])){let n=this._nodes[t[0]];if(n.opType==="Clip")if(n.inputs.length===1)try{e.attributes.set("activation_params","floats",[n.attributes.getFloat("min"),n.attributes.getFloat("max")])}catch{e.attributes.set("activation_params","floats",[Pi,Ci])}else if(n.inputs.length>=3&&this._allData[n.inputs[1]].tensor!==void 0&&this._allData[n.inputs[2]].tensor!==void 0)e.attributes.set("activation_params","floats",[this._allData[n.inputs[1]].tensor.floatData[0],this._allData[n.inputs[2]].tensor.floatData[0]]);else continue;e.attributes.set("activation","string",n.opType),this.deleteNode(t[0])}}}}}),Zf,Yf,Dv,SA=N(()=>{"use strict";Zf=be(Re()),TA(),Ko(),Yf=be(Ni()),Ne(),Dv=class{constructor(){}load(e,t,n){let i;if(!n)try{this.loadFromOnnxFormat(e,t);return}catch(u){if(n!==void 0)throw u;i=u}try{this.loadFromOrtFormat(e,t)}catch(u){throw n!==void 0?u:new Error(`Failed to load model as ONNX format: ${i} +as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Yf.onnx.ModelProto.decode(e);if(Yt.longToNumber(n.irVersion)<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=n.opsetImport.map(i=>({domain:i.domain,version:Yt.longToNumber(i.version)})),this._graph=dl.from(n.graph,t)}loadFromOrtFormat(e,t){let n=new Zf.ByteBuffer(e),i=V0.InferenceSession.getRootAsInferenceSession(n).model();if(Yt.longToNumber(i.irVersion())<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=[];for(let u=0;u{"use strict";vA(),xA(),Gt(),SA(),Rv=class{constructor(e={}){this._initialized=!1,this.backendHint=e.backendHint,this.profiler=i0.create(e.profiler),this.context={profiler:this.profiler,graphInputTypes:[],graphInputDims:[]}}get inputNames(){return this._model.graph.getInputNames()}get outputNames(){return this._model.graph.getOutputNames()}startProfiling(){this.profiler.start()}endProfiling(){this.profiler.stop()}async loadModel(e,t,n){await this.profiler.event("session","Session.loadModel",async()=>{let i=await kv(this.backendHint);if(this.sessionHandler=i.createSessionHandler(this.context),this._model=new Dv,typeof e=="string"){let u=e.endsWith(".ort");{let l=await(await fetch(e)).arrayBuffer();this.initialize(new Uint8Array(l),u)}}else if(ArrayBuffer.isView(e))this.initialize(e);else{let u=new Uint8Array(e,t||0,n||e.byteLength);this.initialize(u)}})}initialize(e,t){if(this._initialized)throw new Error("already initialized");this.profiler.event("session","Session.initialize",()=>{let n=this.sessionHandler.transformGraph?this.sessionHandler:void 0;this._model.load(e,n,t),this.sessionHandler.onGraphInitialized&&this.sessionHandler.onGraphInitialized(this._model.graph),this.initializeOps(this._model.graph),this._executionPlan=new Cv(this._model.graph,this._ops,this.profiler)}),this._initialized=!0}async run(e){if(!this._initialized)throw new Error("session not initialized yet");return this.profiler.event("session","Session.run",async()=>{let t=this.normalizeAndValidateInputs(e),n=await this._executionPlan.execute(this.sessionHandler,t);return this.createOutput(n)})}normalizeAndValidateInputs(e){let t=this._model.graph.getInputNames();if(Array.isArray(e)){if(e.length!==t.length)throw new Error(`incorrect input array length: expected ${t.length} but got ${e.length}`)}else{if(e.size!==t.length)throw new Error(`incorrect input map size: expected ${t.length} but got ${e.size}`);let n=new Array(e.size),i=0;for(let u=0;u{"use strict";dt(),zn(),Nv=class{constructor(e){this.session=e,this.inputNames=this.session.inputNames,this.outputNames=this.session.outputNames}get inputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}get outputMetadata(){throw new Error("Getting model metadata is not supported in webgl backend.")}async dispose(){}async run(e,t,n){let i=new Map;for(let d in e)if(Object.hasOwnProperty.call(e,d)){let c=e[d];i.set(d,new vt(c.dims,c.type,void 0,void 0,c.data))}let u=await this.session.run(i),l={};return u.forEach((d,c)=>{l[c]=new _t(d.type,d.data,d.dims)}),l}startProfiling(){this.session.startProfiling()}endProfiling(){this.session.endProfiling()}}}),zv={};gn(zv,{onnxjsBackend:()=>Mv});var Jf,Mv,EA=N(()=>{"use strict";IA(),OA(),Jf=class{async init(){}async createInferenceSessionHandler(e,t){let n=new Rv(t);return typeof e=="string"?await n.loadModel(e):await n.loadModel(e),new Nv(n)}},Mv=new Jf}),Ml=N(()=>{"use strict"}),Bv={};gn(Bv,{default:()=>Fv});var xa,$a,Fv,AA=N(()=>{"use strict";W2(),yn(),Bl(),xa="ort-wasm-proxy-worker",$a=globalThis.self?.name===xa,$a&&(self.onmessage=e=>{let{type:t,in:n}=e.data;try{switch(t){case"init-wasm":Fl(n.wasm).then(()=>{rd(n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})})},i=>{postMessage({type:t,err:i})});break;case"init-ep":{let{epName:i,env:u}=n;nd(u,i).then(()=>{postMessage({type:t})},l=>{postMessage({type:t,err:l})});break}case"copy-from":{let{buffer:i}=n,u=Uo(i);postMessage({type:t,out:u});break}case"create":{let{model:i,options:u}=n;id(i,u).then(l=>{postMessage({type:t,out:l})},l=>{postMessage({type:t,err:l})});break}case"release":od(n),postMessage({type:t});break;case"run":{let{sessionId:i,inputIndices:u,inputs:l,outputIndices:d,options:c}=n;sd(i,u,l,d,new Array(d.length).fill(null),c).then(s=>{s.some(r=>r[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:s},ud([...l,...s]))},s=>{postMessage({type:t,err:s})});break}case"end-profiling":ad(n),postMessage({type:t});break;default:}}catch(i){postMessage({type:t,err:i})}}),Fv=$a?null:e=>new Worker(e??bt,{type:"module",name:xa})}),jv={};gn(jv,{default:()=>Lv});async function Qf(e={}){var t=e,n=!!globalThis.window,i=!!globalThis.WorkerGlobalScope,u=i&&self.name?.startsWith("em-pthread");t.mountExternalData=(f,b)=>{f.startsWith("./")&&(f=f.substring(2)),(t.Yc||(t.Yc=new Map)).set(f,b)},t.unmountExternalData=()=>{delete t.Yc,delete t.Zd,delete t.Yd,delete t.$d},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let l=f=>async(...b)=>{try{if(t.Xc)throw Error("Session already started");let $=t.Xc={Kd:b[0],errors:[]},x=await f(...b);if(t.Xc!==$)throw Error("Session mismatch");t.dd?.flush();let E=$.errors;if(0z),0{if(f==="webgpu"){[t.dd,t.Ad,t.Ed,t.ed,t.Dd,t.$b,t.Fd,t.Hd,t.Bd,t.Cd,t.Gd]=b;let $=t.dd;t.jsepRegisterBuffer=(x,E,P,z)=>$.registerBuffer(x,E,P,z),t.jsepGetBuffer=x=>$.getBuffer(x),t.jsepCreateDownloader=(x,E,P)=>$.createDownloader(x,E,P),t.jsepOnCreateSession=x=>{$.onCreateSession(x)},t.jsepOnReleaseSession=x=>{$.onReleaseSession(x)},t.jsepOnRunStart=x=>$.onRunStart(x),t.Id=(x,E)=>{$.upload(x,E)}}else if(f==="webnn"){let $=b[0];[t.Sd,t.sd,t.webnnEnsureTensor,t.td,t.webnnDownloadTensor,t.Rd,t.webnnEnableTraceEvent]=b.slice(1),t.webnnReleaseTensorId=t.sd,t.webnnUploadTensor=t.td,t.webnnRegisterMLContext=t.Rd,t.webnnOnRunStart=x=>$.onRunStart(x),t.webnnOnRunEnd=$.onRunEnd.bind($),t.webnnOnReleaseSession=x=>{$.onReleaseSession(x)},t.webnnCreateMLTensorDownloader=(x,E)=>$.createMLTensorDownloader(x,E),t.webnnRegisterMLTensor=(x,E,P,z)=>$.registerMLTensor(x,E,P,z),t.webnnCreateMLContext=x=>$.createMLContext(x),t.webnnRegisterGraphInput=$.registerGraphInput.bind($),t.webnnIsGraphInput=$.isGraphInput.bind($),t.webnnRegisterGraphOutput=$.registerGraphOutput.bind($),t.webnnIsGraphOutput=$.isGraphOutput.bind($),t.webnnCreateTemporaryTensor=$.createTemporaryTensor.bind($),t.webnnIsGraphInputOutputTypeSupported=$.isGraphInputOutputTypeSupported.bind($)}};let d=()=>{let f=b=>(...$)=>{let x=Wt;return $=b(...$),Wt!=x?new Promise((E,P)=>{Ss={resolve:E,reject:P}}):$};(()=>{for(let b of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[b]=f(t[b])})(),l!==void 0&&(t._OrtRun=l(t._OrtRun),t._OrtRunWithBinding=l(t._OrtRunWithBinding)),d=void 0};t.asyncInit=()=>{d?.()};var c,s,r=(f,b)=>{throw b},o=import.meta.url,a="";if(n||i){try{a=new URL(".",o).href}catch{}i&&(s=f=>{var b=new XMLHttpRequest;return b.open("GET",f,!1),b.responseType="arraybuffer",b.send(null),new Uint8Array(b.response)}),c=async f=>{if(k(f))return new Promise(($,x)=>{var E=new XMLHttpRequest;E.open("GET",f,!0),E.responseType="arraybuffer",E.onload=()=>{E.status==200||E.status==0&&E.response?$(E.response):x(E.status)},E.onerror=x,E.send(null)});var b=await fetch(f,{credentials:"same-origin"});if(b.ok)return b.arrayBuffer();throw Error(b.status+" : "+b.url)}}var p,h,m,g,v,w,y=console.log.bind(console),T=console.error.bind(console),S=y,O=T,A=!1,k=f=>f.startsWith("file://");function I(){dr.buffer!=X.buffer&&Y()}if(u){let f=function(b){try{var $=b.data,x=$.Sc;if(x==="load"){let E=[];self.onmessage=P=>E.push(P),w=()=>{postMessage({Sc:"loaded"});for(let P of E)f(P);self.onmessage=f};for(let P of $.xd)t[P]&&!t[P].proxy||(t[P]=(...z)=>{postMessage({Sc:"callHandler",vd:P,args:z})},P=="print"&&(S=t[P]),P=="printErr"&&(O=t[P]));dr=$.Od,Y(),h=$.Pd,ue(),so()}else if(x==="run"){(function(E){var P=(I(),D)[E+52>>>2>>>0];E=(I(),D)[E+56>>>2>>>0],zc(P,P-E),we(P)})($.Rc),ks($.Rc,0,0,1,0,0),Md(),xs($.Rc),M||(kc(),M=!0);try{PT($.Md,$.bd)}catch(E){if(E!="unwind")throw E}}else $.target!=="setimmediate"&&(x==="checkMailbox"?M&&Qi():x&&(O(`worker: received unknown command ${x}`),O($)))}catch(E){throw Pc(),E}};var R=f,M=!1;self.onunhandledrejection=b=>{throw b.reason||b},self.onmessage=f}var X,G,C,W,_,D,V,q,H,K,F,Q=!1;function Y(){var f=dr.buffer;t.HEAP8=X=new Int8Array(f),C=new Int16Array(f),t.HEAPU8=G=new Uint8Array(f),W=new Uint16Array(f),t.HEAP32=_=new Int32Array(f),t.HEAPU32=D=new Uint32Array(f),V=new Float32Array(f),q=new Float64Array(f),H=new BigInt64Array(f),K=new BigUint64Array(f)}function j(){Q=!0,u?w():tr.sb()}function ee(f){throw O(f="Aborted("+f+")"),A=!0,f=new WebAssembly.RuntimeError(f+". Build with -sASSERTIONS for more info."),v?.(f),f}function ie(){return{a:{ma:rI,hb:tI,g:CT,J:DT,f:RT,o:NT,i:zT,$:MT,b:BT,S:FT,Ia:Vd,n:jT,aa:Wd,Ya:Kd,Ea:Xd,Ga:Zd,Za:Yd,Wa:Jd,Pa:Qd,Va:ec,ka:tc,Fa:rc,Ca:nc,Xa:ic,Da:oc,cb:LT,fa:VT,xa:GT,va:HT,ea:KT,N:XT,H:ZT,wa:YT,_:iS,ya:oS,Sa:sS,Aa:uS,Ja:lS,ta:dS,ga:cS,Ra:xs,$a:pS,Q:gS,r:vS,c:_s,ib:xS,y:$S,M:TS,D:SS,l:IS,s:hc,jb:OS,I:ES,R:AS,j:kS,u:PS,q:CS,k:DS,Ma:RS,Na:NS,Oa:zS,Ka:bc,La:yc,ua:wc,eb:BS,bb:LS,v:US,ba:VS,ha:GS,ab:FS,V:qS,_a:HS,Ba:WS,F:MS,T:KS,la:io,za:ZS,gb:XS,fb:YS,Ta:$c,Ua:Tc,Ha:Rr,U:Sc,ja:Ic,Qa:Oc,ia:Ec,lb:BI,na:DI,mb:MI,oa:CI,G:xI,e:sI,t:iI,w:nI,B:gI,nb:AI,Z:EI,x:lI,pa:kI,X:RI,ca:OI,ob:II,pb:SI,O:bI,qa:TI,qb:$I,L:_I,Y:PI,d:oI,A:uI,m:aI,kb:FI,p:cI,z:pI,C:dI,E:hI,K:yI,ra:vI,P:NI,da:wI,W:zI,rb:mI,sa:fI,h:QS,a:dr,db:ht}}}async function ue(){function f(x,E){var P=tr=x.exports;x={};for(let[z,U]of Object.entries(P))typeof U=="function"?(P=hS(U),x[z]=P):x[z]=U;return tr=x,tr=(function(){var z=tr,U=te=>ge=>te(ge)>>>0,J=te=>()=>te()>>>0;return(z=Object.assign({},z)).tb=U(z.tb),z.Xb=J(z.Xb),z.Zb=U(z.Zb),z.lc=U(z.lc),z.mc=J(z.mc),z.qc=U(z.qc),z})(),Nd.push(tr._b),Ac=(x=tr).tb,kc=x.ub,t._OrtInit=x.vb,t._OrtGetLastError=x.wb,t._OrtCreateSessionOptions=x.xb,t._OrtAppendExecutionProvider=x.yb,t._OrtAddFreeDimensionOverride=x.zb,t._OrtAddSessionConfigEntry=x.Ab,t._OrtReleaseSessionOptions=x.Bb,t._OrtCreateSession=x.Cb,t._OrtReleaseSession=x.Db,t._OrtGetInputOutputCount=x.Eb,t._OrtGetInputOutputMetadata=x.Fb,t._OrtFree=x.Gb,t._OrtCreateTensor=x.Hb,t._OrtGetTensorData=x.Ib,t._OrtReleaseTensor=x.Jb,t._OrtCreateRunOptions=x.Kb,t._OrtAddRunConfigEntry=x.Lb,t._OrtReleaseRunOptions=x.Mb,t._OrtCreateBinding=x.Nb,t._OrtBindInput=x.Ob,t._OrtBindOutput=x.Pb,t._OrtClearBoundOutputs=x.Qb,t._OrtReleaseBinding=x.Rb,t._OrtRunWithBinding=x.Sb,t._OrtRun=x.Tb,t._OrtEndProfiling=x.Ub,t._JsepOutput=x.Vb,t._JsepGetNodeName=x.Wb,oo=x.Xb,Kt=t._free=x.Yb,Qn=t._malloc=x.Zb,ks=x.ac,Pc=x.bc,Cc=x.cc,Dc=x.dc,Ps=x.ec,Rc=x.fc,Nc=x.gc,Te=x.hc,ei=x.ic,zc=x.jc,we=x.kc,Cs=x.lc,_e=x.mc,Mc=x.nc,Ds=x.oc,Bc=x.pc,Fc=x.qc,jc=x.rc,Rs=x.sc,Lc=x.tc,Uc=x.uc,Vc=x.vc,Gc=x.wc,qc=x.xc,Hc=x.yc,Wc=x.zc,Kc=x.Ac,Xc=x.Bc,Zc=x.Cc,Yc=x.Dc,Jc=x.Ec,Qc=x.Fc,ep=x.Gc,tp=x.Hc,rp=x.Ic,np=x.Jc,ip=x.Kc,op=x.Lc,sp=x.Mc,ap=x.Nc,up=x.Pc,lp=x.Qc,dp=x.$c,cp=x.ad,pp=x.fd,hp=x.kd,fp=x.ld,mp=x.md,gp=x.nd,bp=x.od,yp=x.pd,wp=x.qd,_p=x.rd,vp=x.wd,xp=x.Ud,$p=x.Vd,Tp=x.Wd,Sp=x.Xd,h=E,tr}var b,$=ie();return t.instantiateWasm?new Promise(x=>{t.instantiateWasm($,(E,P)=>{x(f(E,P))})}):u?f(new WebAssembly.Instance(h,ie()),h):(F??=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",a):a+"ort-wasm-simd-threaded.jsep.wasm":new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href,b=await(async function(x){var E=F;if(!p&&!k(E))try{var P=fetch(E,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(P,x)}catch(z){O(`wasm streaming compile failed: ${z}`),O("falling back to ArrayBuffer instantiation")}return(async function(z,U){try{var J=await(async function(te){if(!p)try{var ge=await c(te);return new Uint8Array(ge)}catch{}if(te==F&&p)te=new Uint8Array(p);else{if(!s)throw"both async and sync fetching of the wasm failed";te=s(te)}return te})(z);return await WebAssembly.instantiate(J,U)}catch(te){O(`failed to asynchronously prepare wasm: ${te}`),ee(te)}})(E,x)})($),f(b.instance,b.module))}class ce{name="ExitStatus";constructor(b){this.message=`Program terminated with exit(${b})`,this.status=b}}var de=f=>{f.terminate(),f.onmessage=()=>{}},xe=[],Ge=0,Ve=null,nt=f=>{qt.length==0&&(Fd(),Bd(qt[0]));var b=qt.pop();if(!b)return 6;Yn.push(b),Nr[f.Rc]=b,b.Rc=f.Rc;var $={Sc:"run",Md:f.Ld,bd:f.bd,Rc:f.Rc};return b.postMessage($,f.jd),0},Xe=0,$e=(f,b,...$)=>{var x,E=16*$.length,P=_e(),z=Cs(E),U=z>>>3;for(x of $)typeof x=="bigint"?((I(),H)[U++>>>0]=1n,(I(),H)[U++>>>0]=x):((I(),H)[U++>>>0]=0n,(I(),q)[U++>>>0]=x);return f=Cc(f,0,E,z,b),we(P),f};function ht(f){if(u)return $e(0,1,f);if(m=f,!(0{if(m=f,u)throw St(f),"unwind";ht(f)},qt=[],Yn=[],Nd=[],Nr={},zd=f=>{var b=f.Rc;delete Nr[b],qt.push(f),Yn.splice(Yn.indexOf(f),1),f.Rc=0,Dc(b)};function Md(){Nd.forEach(f=>f())}var Bd=f=>new Promise(b=>{f.onmessage=E=>{var P=E.data;if(E=P.Sc,P.Zc&&P.Zc!=oo()){var z=Nr[P.Zc];z?z.postMessage(P,P.jd):O(`Internal error! Worker sent a message "${E}" to target pthread ${P.Zc}, but that thread no longer exists!`)}else E==="checkMailbox"?Qi():E==="spawnThread"?nt(P):E==="cleanupThread"?Ji(()=>{zd(Nr[P.Nd])}):E==="loaded"?(f.loaded=!0,b(f)):P.target==="setimmediate"?f.postMessage(P):E==="uncaughtException"?f.onerror(P.error):E==="callHandler"?t[P.vd](...P.args):E&&O(`worker sent an unknown command ${E}`)},f.onerror=E=>{throw O(`worker sent an error! ${E.filename}:${E.lineno}: ${E.message}`),E};var $,x=[];for($ of[])t.propertyIsEnumerable($)&&x.push($);f.postMessage({Sc:"load",xd:x,Od:dr,Pd:h})});function Fd(){var f=new Worker((()=>{let b=URL;return import.meta.url>"file:"&&import.meta.url<"file;"?new b("ort.all.bundle.min.mjs",import.meta.url):new URL(import.meta.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});qt.push(f)}var dr,PT=(f,b)=>{Xe=0,f=Rs(f,b),0>>=0);return(I(),X)[b.Tc+12>>>0]==0&&(jd(b,!0),Zi--),Ld(b,!1),Xi.push(b),Fc(f)}var vn=0,DT=()=>{Te(0,0);var f=Xi.pop();Mc(f.cd),vn=0};function jd(f,b){b=b?1:0,(I(),X)[f.Tc+12>>>0]=b}function Ld(f,b){b=b?1:0,(I(),X)[f.Tc+13>>>0]=b}class gs{constructor(b){this.cd=b,this.Tc=b-24}}var bs=f=>{var b=vn;if(!b)return ei(0),0;var $=new gs(b);(I(),D)[$.Tc+16>>>2>>>0]=b;var x=(I(),D)[$.Tc+4>>>2>>>0];if(!x)return ei(0),b;for(var E of f){if(E===0||E===x)break;if(Bc(E,x,$.Tc+16))return ei(E),b}return ei(x),b};function RT(){return bs([])}function NT(f){return bs([f>>>0])}function zT(f,b,$,x){return bs([f>>>0,b>>>0,$>>>0,x>>>0])}var MT=()=>{var f=Xi.pop();f||ee("no exception to throw");var b=f.cd;throw(I(),X)[f.Tc+13>>>0]==0&&(Xi.push(f),Ld(f,!0),jd(f,!1),Zi++),Ds(b),vn=b};function BT(f,b,$){var x=new gs(f>>>=0);throw b>>>=0,$>>>=0,(I(),D)[x.Tc+16>>>2>>>0]=0,(I(),D)[x.Tc+4>>>2>>>0]=b,(I(),D)[x.Tc+8>>>2>>>0]=$,Ds(f),Zi++,vn=f}var FT=()=>Zi;function Ud(f,b,$,x){return u?$e(2,1,f,b,$,x):Vd(f,b,$,x)}function Vd(f,b,$,x){if(f>>>=0,b>>>=0,$>>>=0,x>>>=0,!globalThis.SharedArrayBuffer)return 6;var E=[];return u&&E.length===0?Ud(f,b,$,x):(f={Ld:$,Rc:f,bd:x,jd:E},u?(f.Sc="spawnThread",postMessage(f,E),0):nt(f))}function jT(f){throw vn||=f>>>0,vn}var Gd=globalThis.TextDecoder&&new TextDecoder,qd=(f,b,$,x)=>{if($=b+$,x)return $;for(;f[b]&&!(b>=$);)++b;return b},Hd=(f,b=0,$,x)=>{if(16<($=qd(f,b>>>=0,$,x))-b&&f.buffer&&Gd)return Gd.decode(f.buffer instanceof ArrayBuffer?f.subarray(b,$):f.slice(b,$));for(x="";b<$;){var E=f[b++];if(128&E){var P=63&f[b++];if((224&E)==192)x+=String.fromCharCode((31&E)<<6|P);else{var z=63&f[b++];65536>(E=(240&E)==224?(15&E)<<12|P<<6|z:(7&E)<<18|P<<12|z<<6|63&f[b++])?x+=String.fromCharCode(E):(E-=65536,x+=String.fromCharCode(55296|E>>10,56320|1023&E))}}else x+=String.fromCharCode(E)}return x},Qe=(f,b,$)=>(f>>>=0)?Hd((I(),G),f,b,$):"";function Wd(f,b,$){return u?$e(3,1,f,b,$):0}function Kd(f,b){if(u)return $e(4,1,f,b)}function Xd(f,b){if(u)return $e(5,1,f,b)}function Zd(f,b,$){if(u)return $e(6,1,f,b,$)}function Yd(f,b,$){return u?$e(7,1,f,b,$):0}function Jd(f,b){if(u)return $e(8,1,f,b)}function Qd(f,b,$){if(u)return $e(9,1,f,b,$)}function ec(f,b,$,x){if(u)return $e(10,1,f,b,$,x)}function tc(f,b,$,x){if(u)return $e(11,1,f,b,$,x)}function rc(f,b,$,x){if(u)return $e(12,1,f,b,$,x)}function nc(f){if(u)return $e(13,1,f)}function ic(f,b){if(u)return $e(14,1,f,b)}function oc(f,b,$){if(u)return $e(15,1,f,b,$)}var LT=()=>ee(""),Ht=f=>{f>>>=0;for(var b="";;){var $=(I(),G)[f++>>>0];if(!$)return b;b+=String.fromCharCode($)}},ys={},ws={},UT={},xn=class extends Error{constructor(f){super(f),this.name="BindingError"}};function er(f,b,$={}){return(function(x,E,P={}){var z=E.name;if(!x)throw new xn(`type "${z}" must have a positive integer typeid pointer`);if(ws.hasOwnProperty(x)){if(P.yd)return;throw new xn(`Cannot register type '${z}' twice`)}ws[x]=E,delete UT[x],ys.hasOwnProperty(x)&&(E=ys[x],delete ys[x],E.forEach(U=>U()))})(f,b,$)}var sc=(f,b,$)=>{switch(b){case 1:return $?x=>(I(),X)[x>>>0]:x=>(I(),G)[x>>>0];case 2:return $?x=>(I(),C)[x>>>1>>>0]:x=>(I(),W)[x>>>1>>>0];case 4:return $?x=>(I(),_)[x>>>2>>>0]:x=>(I(),D)[x>>>2>>>0];case 8:return $?x=>(I(),H)[x>>>3>>>0]:x=>(I(),K)[x>>>3>>>0];default:throw new TypeError(`invalid integer width (${b}): ${f}`)}};function VT(f,b,$,x,E){f>>>=0,$>>>=0,b=Ht(b>>>0);let P=z=>z;if(x=x===0n){let z=8*$;P=U=>BigInt.asUintN(z,U),E=P(E)}er(f,{name:b,Oc:P,Vc:(z,U)=>(typeof U=="number"&&(U=BigInt(U)),U),Uc:sc(b,$,!x),Wc:null})}function GT(f,b,$,x){er(f>>>=0,{name:b=Ht(b>>>0),Oc:function(E){return!!E},Vc:function(E,P){return P?$:x},Uc:function(E){return this.Oc((I(),G)[E>>>0])},Wc:null})}var ac=[],zr=[0,1,,1,null,1,!0,1,!1,1];function _s(f){9<(f>>>=0)&&--zr[f+1]===0&&(zr[f]=void 0,ac.push(f))}var It=f=>{if(!f)throw new xn(`Cannot use deleted val. handle = ${f}`);return zr[f]},Nt=f=>{switch(f){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let b=ac.pop()||zr.length;return zr[b]=f,zr[b+1]=1,b}};function vs(f){return this.Oc((I(),D)[f>>>2>>>0])}var qT={name:"emscripten::val",Oc:f=>{var b=It(f);return _s(f),b},Vc:(f,b)=>Nt(b),Uc:vs,Wc:null};function HT(f){return er(f>>>0,qT)}var WT=(f,b)=>{switch(b){case 4:return function($){return this.Oc((I(),V)[$>>>2>>>0])};case 8:return function($){return this.Oc((I(),q)[$>>>3>>>0])};default:throw new TypeError(`invalid float width (${b}): ${f}`)}};function KT(f,b,$){$>>>=0,er(f>>>=0,{name:b=Ht(b>>>0),Oc:x=>x,Vc:(x,E)=>E,Uc:WT(b,$),Wc:null})}function XT(f,b,$,x,E){f>>>=0,$>>>=0,b=Ht(b>>>0);let P=U=>U;if(x===0){var z=32-8*$;P=U=>U<>>z,E=P(E)}er(f,{name:b,Oc:P,Vc:(U,J)=>J,Uc:sc(b,$,x!==0),Wc:null})}function ZT(f,b,$){function x(P){var z=(I(),D)[P>>>2>>>0];return P=(I(),D)[P+4>>>2>>>0],new E((I(),X).buffer,P,z)}var E=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][b];er(f>>>=0,{name:$=Ht($>>>0),Oc:x,Uc:x},{yd:!0})}var cr=(f,b,$)=>{var x=(I(),G);if(b>>>=0,0<$){var E=b;$=b+$-1;for(var P=0;P=z){if(b>=$)break;x[b++>>>0]=z}else if(2047>=z){if(b+1>=$)break;x[b++>>>0]=192|z>>6,x[b++>>>0]=128|63&z}else if(65535>=z){if(b+2>=$)break;x[b++>>>0]=224|z>>12,x[b++>>>0]=128|z>>6&63,x[b++>>>0]=128|63&z}else{if(b+3>=$)break;x[b++>>>0]=240|z>>18,x[b++>>>0]=128|z>>12&63,x[b++>>>0]=128|z>>6&63,x[b++>>>0]=128|63&z,P++}}x[b>>>0]=0,f=b-E}else f=0;return f},Yi=f=>{for(var b=0,$=0;$=x?b++:2047>=x?b+=2:55296<=x&&57343>=x?(b+=4,++$):b+=3}return b};function YT(f,b){er(f>>>=0,{name:b=Ht(b>>>0),Oc($){var x=(I(),D)[$>>>2>>>0];return x=Qe($+4,x,!0),Kt($),x},Vc($,x){x instanceof ArrayBuffer&&(x=new Uint8Array(x));var E=typeof x=="string";if(!(E||ArrayBuffer.isView(x)&&x.BYTES_PER_ELEMENT==1))throw new xn("Cannot pass non-string to std::string");var P=E?Yi(x):x.length,z=Qn(4+P+1),U=z+4;return(I(),D)[z>>>2>>>0]=P,E?cr(x,U,P+1):(I(),G).set(x,U>>>0),$!==null&&$.push(Kt,z),z},Uc:vs,Wc($){Kt($)}})}var uc=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,JT=(f,b,$)=>{if(f>>>=1,16<(b=qd((I(),W),f,b/2,$))-f&&uc)return uc.decode((I(),W).slice(f,b));for($="";f>>0];$+=String.fromCharCode(x)}return $},QT=(f,b,$)=>{if($??=2147483647,2>$)return 0;var x=b;$=($-=2)<2*f.length?$/2:f.length;for(var E=0;E<$;++E){var P=f.charCodeAt(E);(I(),C)[b>>>1>>>0]=P,b+=2}return(I(),C)[b>>>1>>>0]=0,b-x},eS=f=>2*f.length,tS=(f,b,$)=>{var x="";f>>>=2;for(var E=0;!(E>=b/4);E++){var P=(I(),D)[f+E>>>0];if(!P&&!$)break;x+=String.fromCodePoint(P)}return x},rS=(f,b,$)=>{if(b>>>=0,$??=2147483647,4>$)return 0;var x=b;$=x+$-4;for(var E=0;E>>2>>>0]=P,(b+=4)+4>$)break}return(I(),_)[b>>>2>>>0]=0,b-x},nS=f=>{for(var b=0,$=0;$>>=0,b>>>=0,$=Ht($>>>=0),b===2)var x=JT,E=QT,P=eS;else x=tS,E=rS,P=nS;er(f,{name:$,Oc:z=>{var U=(I(),D)[z>>>2>>>0];return U=x(z+4,U*b,!0),Kt(z),U},Vc:(z,U)=>{if(typeof U!="string")throw new xn(`Cannot pass non-string to C++ string type ${$}`);var J=P(U),te=Qn(4+J+b);return(I(),D)[te>>>2>>>0]=J/b,E(U,te+4,J+b),z!==null&&z.push(Kt,te),te},Uc:vs,Wc(z){Kt(z)}})}function oS(f,b){er(f>>>=0,{zd:!0,name:b=Ht(b>>>0),Oc:()=>{},Vc:()=>{}})}function sS(f){ks(f>>>0,!i,1,!n,131072,!1),Md()}var Ji=f=>{if(!A)try{if(f(),!(0Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function xs(f){f>>>=0,aS||(Atomics.waitAsync((I(),_),f>>>2,f).value.then(Qi),f+=128,Atomics.store((I(),_),f>>>2,1))}var Qi=()=>Ji(()=>{var f=oo();f&&(xs(f),Nc())});function uS(f,b){(f>>>=0)==b>>>0?setTimeout(Qi):u?postMessage({Zc:f,Sc:"checkMailbox"}):(f=Nr[f])&&f.postMessage({Sc:"checkMailbox"})}var $s=[];function lS(f,b,$,x,E){for(b>>>=0,E>>>=0,$s.length=0,$=E>>>3,x=E+x>>>3;$>>0]?(I(),H)[$++>>>0]:(I(),q)[$++>>>0],$s.push(P)}return(b?Ns[b]:eI[f])(...$s)}var dS=()=>{Xe=0};function cS(f){f>>>=0,u?postMessage({Sc:"cleanupThread",Nd:f}):zd(Nr[f])}function pS(f){}var eo=f=>{try{f()}catch(b){ee(b)}};function hS(f){var b=(...$)=>{to.push(f);try{return f(...$)}finally{A||(to.pop(),Wt&&pr===1&&to.length===0&&(pr=0,Xe+=1,eo($p),typeof Fibers<"u"&&Fibers.be()))}};return cc.set(f,b),b}var pr=0,Wt=null,lc=0,to=[],Ts=new Map,dc=new Map,cc=new Map,fS=0,Ss=null,mS=[],pc=f=>(function(b){if(!A){if(pr===0){var $=!1,x=!1;b((E=0)=>{if(!A&&(lc=E,$=!0,x)){pr=2,eo(()=>Tp(Wt)),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.resume(),E=!1;try{var P=(function(){var J=(I(),_)[Wt+8>>>2>>>0];return J=dc.get(J),J=cc.get(J),--Xe,J()})()}catch(J){P=J,E=!0}var z=!1;if(!Wt){var U=Ss;U&&(Ss=null,(E?U.reject:U.resolve)(P),z=!0)}if(E&&!z)throw P}}),x=!0,$||(pr=1,Wt=(function(){var E=Qn(65548),P=E+12;if((I(),D)[E>>>2>>>0]=P,(I(),D)[E+4>>>2>>>0]=P+65536,P=to[0],!Ts.has(P)){var z=fS++;Ts.set(P,z),dc.set(z,P)}return P=Ts.get(P),(I(),_)[E+8>>>2>>>0]=P,E})(),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.pause(),eo(()=>xp(Wt)))}else pr===2?(pr=0,eo(Sp),Kt(Wt),Wt=null,mS.forEach(Ji)):ee(`invalid state: ${pr}`);return lc}})(b=>{f().then(b)});function gS(f){return f>>>=0,pc(async()=>{var b=await It(f);return Nt(b)})}var Is=[],bS=f=>{var b=Is.length;return Is.push(f),b},yS=(f,b)=>{for(var $=Array(f),x=0;x>>2>>>0],z=ws[P];if(z===void 0)throw f=`parameter ${x}`,P=Ac(P),b=Ht(P),Kt(P),new xn(`${f} has unknown type ${b}`);$[E]=z}return $},wS=(f,b,$)=>{var x=[];return f=f(x,$),x.length&&((I(),D)[b>>>2>>>0]=Nt(x)),f},_S={},ro=f=>{var b=_S[f];return b===void 0?Ht(f):b};function vS(f,b,$){var[x,...E]=yS(f,b>>>0);b=x.Vc.bind(x);var P=E.map(J=>J.Uc.bind(J));f--;var z={toValue:It};switch(f=P.map((J,te)=>{var ge=`argFromPtr${te}`;return z[ge]=J,`${ge}(args${te?"+"+8*te:""})`}),$){case 0:var U="toValue(handle)";break;case 2:U="new (toValue(handle))";break;case 3:U="";break;case 1:z.getStringOrSymbol=ro,U="toValue(handle)[getStringOrSymbol(methodName)]"}return U+=`(${f})`,x.zd||(z.toReturnWire=b,z.emval_returnValue=wS,U=`return emval_returnValue(toReturnWire, destructorsRef, ${U})`),U=`return function (handle, methodName, destructorsRef, args) { + ${U} + }`,$=new Function(Object.keys(z),U)(...Object.values(z)),U=`methodCaller<(${E.map(J=>J.name)}) => ${x.name}>`,bS(Object.defineProperty($,"name",{value:U}))}function xS(f,b){return b>>>=0,(f=It(f>>>0))==It(b)}function $S(f){return(f>>>=0)?(f=ro(f),Nt(globalThis[f])):Nt(globalThis)}function TS(f){return f=ro(f>>>0),Nt(t[f])}function SS(f,b){return b>>>=0,f=It(f>>>0),b=It(b),Nt(f[b])}function IS(f){9<(f>>>=0)&&(zr[f+1]+=1)}function hc(f,b,$,x,E){return Is[f>>>0](b>>>0,$>>>0,x>>>0,E>>>0)}function OS(f,b,$,x,E){return hc(f>>>0,b>>>0,$>>>0,x>>>0,E>>>0)}function ES(){return Nt([])}function AS(f){f=It(f>>>0);for(var b=Array(f.length),$=0;$>>0))}function PS(){return Nt({})}function CS(f){for(var b=It(f>>>=0);b.length;){var $=b.pop();b.pop()($)}_s(f)}function DS(f,b,$){b>>>=0,$>>>=0,f=It(f>>>0),b=It(b),$=It($),f[b]=$}function RS(f,b){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(I(),_)[b>>>2>>>0]=f.getUTCSeconds(),(I(),_)[b+4>>>2>>>0]=f.getUTCMinutes(),(I(),_)[b+8>>>2>>>0]=f.getUTCHours(),(I(),_)[b+12>>>2>>>0]=f.getUTCDate(),(I(),_)[b+16>>>2>>>0]=f.getUTCMonth(),(I(),_)[b+20>>>2>>>0]=f.getUTCFullYear()-1900,(I(),_)[b+24>>>2>>>0]=f.getUTCDay(),f=(f.getTime()-Date.UTC(f.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(I(),_)[b+28>>>2>>>0]=f}var fc=f=>f%4==0&&(f%100!=0||f%400==0),mc=[0,31,60,91,121,152,182,213,244,274,305,335],gc=[0,31,59,90,120,151,181,212,243,273,304,334];function NS(f,b){f=-9007199254740992>f||9007199254740992>>=0,f=new Date(1e3*f),(I(),_)[b>>>2>>>0]=f.getSeconds(),(I(),_)[b+4>>>2>>>0]=f.getMinutes(),(I(),_)[b+8>>>2>>>0]=f.getHours(),(I(),_)[b+12>>>2>>>0]=f.getDate(),(I(),_)[b+16>>>2>>>0]=f.getMonth(),(I(),_)[b+20>>>2>>>0]=f.getFullYear()-1900,(I(),_)[b+24>>>2>>>0]=f.getDay();var $=(fc(f.getFullYear())?mc:gc)[f.getMonth()]+f.getDate()-1|0;(I(),_)[b+28>>>2>>>0]=$,(I(),_)[b+36>>>2>>>0]=-60*f.getTimezoneOffset(),$=new Date(f.getFullYear(),6,1).getTimezoneOffset();var x=new Date(f.getFullYear(),0,1).getTimezoneOffset();f=0|($!=x&&f.getTimezoneOffset()==Math.min(x,$)),(I(),_)[b+32>>>2>>>0]=f}function zS(f){f>>>=0;var b=new Date((I(),_)[f+20>>>2>>>0]+1900,(I(),_)[f+16>>>2>>>0],(I(),_)[f+12>>>2>>>0],(I(),_)[f+8>>>2>>>0],(I(),_)[f+4>>>2>>>0],(I(),_)[f>>>2>>>0],0),$=(I(),_)[f+32>>>2>>>0],x=b.getTimezoneOffset(),E=new Date(b.getFullYear(),6,1).getTimezoneOffset(),P=new Date(b.getFullYear(),0,1).getTimezoneOffset(),z=Math.min(P,E);return 0>$?(I(),_)[f+32>>>2>>>0]=+(E!=P&&z==x):0<$!=(z==x)&&(E=Math.max(P,E),b.setTime(b.getTime()+6e4*((0<$?z:E)-x))),(I(),_)[f+24>>>2>>>0]=b.getDay(),$=(fc(b.getFullYear())?mc:gc)[b.getMonth()]+b.getDate()-1|0,(I(),_)[f+28>>>2>>>0]=$,(I(),_)[f>>>2>>>0]=b.getSeconds(),(I(),_)[f+4>>>2>>>0]=b.getMinutes(),(I(),_)[f+8>>>2>>>0]=b.getHours(),(I(),_)[f+12>>>2>>>0]=b.getDate(),(I(),_)[f+16>>>2>>>0]=b.getMonth(),(I(),_)[f+20>>>2>>>0]=b.getYear(),f=b.getTime(),BigInt(isNaN(f)?-1:f/1e3)}function bc(f,b,$,x,E,P,z){return u?$e(16,1,f,b,$,x,E,P,z):-52}function yc(f,b,$,x,E,P){if(u)return $e(17,1,f,b,$,x,E,P)}var Jn={},MS=()=>performance.timeOrigin+performance.now();function wc(f,b){if(u)return $e(18,1,f,b);if(Jn[f]&&(clearTimeout(Jn[f].id),delete Jn[f]),!b)return 0;var $=setTimeout(()=>{delete Jn[f],Ji(()=>Rc(f,performance.timeOrigin+performance.now()))},b);return Jn[f]={id:$,ae:b},0}function BS(f,b,$,x){f>>>=0,b>>>=0,$>>>=0,x>>>=0;var E=new Date().getFullYear(),P=new Date(E,0,1).getTimezoneOffset();E=new Date(E,6,1).getTimezoneOffset();var z=Math.max(P,E);(I(),D)[f>>>2>>>0]=60*z,(I(),_)[b>>>2>>>0]=+(P!=E),f=(b=U=>{var J=Math.abs(U);return`UTC${0<=U?"-":"+"}${String(Math.floor(J/60)).padStart(2,"0")}${String(J%60).padStart(2,"0")}`})(P),b=b(E),EDate.now(),jS=1;function LS(f,b,$){if($>>>=0,!(0<=f&&3>=f))return 28;if(f===0)f=Date.now();else{if(!jS)return 52;f=performance.timeOrigin+performance.now()}return f=Math.round(1e6*f),(I(),H)[$>>>3>>>0]=BigInt(f),0}var Os=[],_c=(f,b)=>{Os.length=0;for(var $;$=(I(),G)[f++>>>0];){var x=$!=105;b+=(x&=$!=112)&&b%8?4:0,Os.push($==112?(I(),D)[b>>>2>>>0]:$==106?(I(),H)[b>>>3>>>0]:$==105?(I(),_)[b>>>2>>>0]:(I(),q)[b>>>3>>>0]),b+=x?8:4}return Os};function US(f,b,$){return f>>>=0,b=_c(b>>>0,$>>>0),Ns[f](...b)}function VS(f,b,$){return f>>>=0,b=_c(b>>>0,$>>>0),Ns[f](...b)}var GS=()=>{};function qS(f,b){return O(Qe(f>>>0,b>>>0))}var HS=()=>{throw Xe+=1,"unwind"};function WS(){return 4294901760}var KS=()=>navigator.hardwareConcurrency,Mr={},no=f=>{var b;return(b=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(f))?+b[1]:(b=/:(\d+):\d+(?:\)|$)/.exec(f))?2147483648|+b[1]:0},vc=f=>{for(var b of f)(f=no(b))&&(Mr[f]=b)};function XS(){var f=Error().stack.toString().split(` +`);return f[0]=="Error"&&f.shift(),vc(f),Mr.gd=no(f[3]),Mr.Jd=f,Mr.gd}function io(f){if(!(f=Mr[f>>>0]))return 0;var b;if(b=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(f))f=b[1];else if(b=/^\s+at (.*) \(.*\)$/.exec(f))f=b[1];else{if(!(b=/^(.+?)@/.exec(f)))return 0;f=b[1]}Kt(io.hd??0),b=Yi(f)+1;var $=Qn(b);return $&&cr(f,$,b),io.hd=$,io.hd}function ZS(f){f>>>=0;var b=(I(),G).length;if(f<=b||4294901760=$;$*=2){var x=b*(1+.2/$);x=Math.min(x,f+100663296);e:{x=(Math.min(4294901760,65536*Math.ceil(Math.max(f,x)/65536))-dr.buffer.byteLength+65535)/65536|0;try{dr.grow(x),Y();var E=1;break e}catch{}E=void 0}if(E)return!0}return!1}function YS(f,b,$){if(f>>>=0,b>>>=0,Mr.gd==f)var x=Mr.Jd;else(x=Error().stack.toString().split(` +`))[0]=="Error"&&x.shift(),vc(x);for(var E=3;x[E]&&no(x[E])!=f;)++E;for(f=0;f<$&&x[f+E];++f)(I(),_)[b+4*f>>>2>>>0]=no(x[f+E]);return f}var Es,As={},xc=()=>{if(!Es){var f,b={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(globalThis.navigator?.language??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(f in As)As[f]===void 0?delete b[f]:b[f]=As[f];var $=[];for(f in b)$.push(`${f}=${b[f]}`);Es=$}return Es};function $c(f,b){if(u)return $e(19,1,f,b);f>>>=0,b>>>=0;var $,x=0,E=0;for($ of xc()){var P=b+x;(I(),D)[f+E>>>2>>>0]=P,x+=cr($,P,1/0)+1,E+=4}return 0}function Tc(f,b){if(u)return $e(20,1,f,b);f>>>=0,b>>>=0;var $=xc();for(var x of((I(),D)[f>>>2>>>0]=$.length,f=0,$))f+=Yi(x)+1;return(I(),D)[b>>>2>>>0]=f,0}function Sc(f){return u?$e(21,1,f):52}function Ic(f,b,$,x){return u?$e(22,1,f,b,$,x):52}function Oc(f,b,$,x){return u?$e(23,1,f,b,$,x):70}var JS=[null,[],[]];function Ec(f,b,$,x){if(u)return $e(24,1,f,b,$,x);b>>>=0,$>>>=0,x>>>=0;for(var E=0,P=0;P<$;P++){var z=(I(),D)[b>>>2>>>0],U=(I(),D)[b+4>>>2>>>0];b+=8;for(var J=0;J>>0],Oe=JS[te];ge===0||ge===10?((te===1?S:O)(Hd(Oe)),Oe.length=0):Oe.push(ge)}E+=U}return(I(),D)[x>>>2>>>0]=E,0}function QS(f){return f>>>0}u||(function(){for(var f=t.numThreads-1;f--;)Fd();xe.push(async()=>{var b=(async function(){if(!u)return Promise.all(qt.map(Bd))})();Ge++,await b,--Ge==0&&Ve&&(b=Ve,Ve=null,b())})})(),u||(dr=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),Y()),t.wasmBinary&&(p=t.wasmBinary),t.stackSave=()=>_e(),t.stackRestore=f=>we(f),t.stackAlloc=f=>Cs(f),t.setValue=function(f,b,$="i8"){switch($.endsWith("*")&&($="*"),$){case"i1":case"i8":(I(),X)[f>>>0]=b;break;case"i16":(I(),C)[f>>>1>>>0]=b;break;case"i32":(I(),_)[f>>>2>>>0]=b;break;case"i64":(I(),H)[f>>>3>>>0]=BigInt(b);break;case"float":(I(),V)[f>>>2>>>0]=b;break;case"double":(I(),q)[f>>>3>>>0]=b;break;case"*":(I(),D)[f>>>2>>>0]=b;break;default:ee(`invalid type for setValue: ${$}`)}},t.getValue=function(f,b="i8"){switch(b.endsWith("*")&&(b="*"),b){case"i1":case"i8":return(I(),X)[f>>>0];case"i16":return(I(),C)[f>>>1>>>0];case"i32":return(I(),_)[f>>>2>>>0];case"i64":return(I(),H)[f>>>3>>>0];case"float":return(I(),V)[f>>>2>>>0];case"double":return(I(),q)[f>>>3>>>0];case"*":return(I(),D)[f>>>2>>>0];default:ee(`invalid type for getValue: ${b}`)}},t.UTF8ToString=Qe,t.stringToUTF8=cr,t.lengthBytesUTF8=Yi;var Ac,kc,oo,Kt,Qn,ks,Pc,Cc,Dc,Ps,Rc,Nc,Te,ei,zc,we,Cs,_e,Mc,Ds,Bc,Fc,jc,Rs,Lc,Uc,Vc,Gc,qc,Hc,Wc,Kc,Xc,Zc,Yc,Jc,Qc,ep,tp,rp,np,ip,op,sp,ap,up,lp,dp,cp,pp,hp,fp,mp,gp,bp,yp,wp,_p,vp,xp,$p,Tp,Sp,tr,eI=[ht,St,Ud,Wd,Kd,Xd,Zd,Yd,Jd,Qd,ec,tc,rc,nc,ic,oc,bc,yc,wc,$c,Tc,Sc,Ic,Oc,Ec],Ns={1055492:(f,b,$,x,E)=>{if(t===void 0||!t.Yc)return 1;if((f=Qe(Number(f>>>0))).startsWith("./")&&(f=f.substring(2)),!(f=t.Yc.get(f)))return 2;if(b=Number(b>>>0),$=Number($>>>0),x=Number(x>>>0),b+$>f.byteLength)return 3;try{let P=f.subarray(b,b+$);switch(E){case 0:(I(),G).set(P,x>>>0);break;case 1:t.Qd?t.Qd(x,P):t.Id(x,P);break;default:return 4}return 0}catch{return 4}},1056316:(f,b,$)=>{t.td(f,(I(),G).subarray(b>>>0,b+$>>>0))},1056380:()=>t.Sd(),1056422:f=>{t.sd(f)},1056459:()=>{t.Bd()},1056490:()=>{t.Cd()},1056519:()=>{t.Gd()},1056544:f=>t.Ad(f),1056577:f=>t.Ed(f),1056609:(f,b,$)=>{t.ed(Number(f),Number(b),Number($),!0)},1056672:(f,b,$)=>{t.ed(Number(f),Number(b),Number($))},1056729:()=>typeof wasmOffsetConverter<"u",1056786:f=>{t.$b("Abs",f,void 0)},1056837:f=>{t.$b("Neg",f,void 0)},1056888:f=>{t.$b("Floor",f,void 0)},1056941:f=>{t.$b("Ceil",f,void 0)},1056993:f=>{t.$b("Reciprocal",f,void 0)},1057051:f=>{t.$b("Sqrt",f,void 0)},1057103:f=>{t.$b("Exp",f,void 0)},1057154:f=>{t.$b("Erf",f,void 0)},1057205:f=>{t.$b("Sigmoid",f,void 0)},1057260:(f,b,$)=>{t.$b("HardSigmoid",f,{alpha:b,beta:$})},1057339:f=>{t.$b("HardSwish",f,void 0)},1057396:f=>{t.$b("Log",f,void 0)},1057447:f=>{t.$b("Sin",f,void 0)},1057498:f=>{t.$b("Cos",f,void 0)},1057549:f=>{t.$b("Tan",f,void 0)},1057600:f=>{t.$b("Asin",f,void 0)},1057652:f=>{t.$b("Acos",f,void 0)},1057704:f=>{t.$b("Atan",f,void 0)},1057756:f=>{t.$b("Sinh",f,void 0)},1057808:f=>{t.$b("Cosh",f,void 0)},1057860:f=>{t.$b("Asinh",f,void 0)},1057913:f=>{t.$b("Acosh",f,void 0)},1057966:f=>{t.$b("Atanh",f,void 0)},1058019:f=>{t.$b("Tanh",f,void 0)},1058071:f=>{t.$b("Not",f,void 0)},1058122:(f,b,$)=>{t.$b("Clip",f,{min:b,max:$})},1058191:f=>{t.$b("Clip",f,void 0)},1058243:(f,b)=>{t.$b("Elu",f,{alpha:b})},1058301:f=>{t.$b("Gelu",f,void 0)},1058353:f=>{t.$b("Relu",f,void 0)},1058405:(f,b)=>{t.$b("LeakyRelu",f,{alpha:b})},1058469:(f,b)=>{t.$b("ThresholdedRelu",f,{alpha:b})},1058539:(f,b)=>{t.$b("Cast",f,{to:b})},1058597:f=>{t.$b("Add",f,void 0)},1058648:f=>{t.$b("Sub",f,void 0)},1058699:f=>{t.$b("Mul",f,void 0)},1058750:f=>{t.$b("Div",f,void 0)},1058801:f=>{t.$b("Pow",f,void 0)},1058852:f=>{t.$b("Equal",f,void 0)},1058905:f=>{t.$b("Greater",f,void 0)},1058960:f=>{t.$b("GreaterOrEqual",f,void 0)},1059022:f=>{t.$b("Less",f,void 0)},1059074:f=>{t.$b("LessOrEqual",f,void 0)},1059133:(f,b,$,x,E)=>{t.$b("ReduceMean",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059308:(f,b,$,x,E)=>{t.$b("ReduceMax",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059482:(f,b,$,x,E)=>{t.$b("ReduceMin",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059656:(f,b,$,x,E)=>{t.$b("ReduceProd",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1059831:(f,b,$,x,E)=>{t.$b("ReduceSum",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060005:(f,b,$,x,E)=>{t.$b("ReduceL1",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060178:(f,b,$,x,E)=>{t.$b("ReduceL2",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060351:(f,b,$,x,E)=>{t.$b("ReduceLogSum",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060528:(f,b,$,x,E)=>{t.$b("ReduceSumSquare",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060708:(f,b,$,x,E)=>{t.$b("ReduceLogSumExp",f,{keepDims:!!b,noopWithEmptyAxes:!!$,axes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1060888:f=>{t.$b("Where",f,void 0)},1060941:(f,b,$)=>{t.$b("Transpose",f,{perm:b?Array.from((I(),_).subarray(Number(b)>>>0,Number($)>>>0)):[]})},1061065:(f,b,$,x)=>{t.$b("DepthToSpace",f,{blocksize:b,mode:Qe($),format:x?"NHWC":"NCHW"})},1061198:(f,b,$,x)=>{t.$b("DepthToSpace",f,{blocksize:b,mode:Qe($),format:x?"NHWC":"NCHW"})},1061331:(f,b,$,x)=>{t.$b("DFT",f,{axis:b,inverse:$,onesided:x})},1061423:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe,hr)=>{t.$b("ConvTranspose",f,{format:J?"NHWC":"NCHW",autoPad:b,dilations:[$],group:x,kernelShape:[E],pads:[P,z],strides:[U],wIsConst:()=>!!(I(),X)[te>>>0],outputPadding:ge?Array.from((I(),_).subarray(Number(ge)>>>0,Number(Oe)>>>0)):[],outputShape:ze?Array.from((I(),_).subarray(Number(ze)>>>0,Number(Fe)>>>0)):[],activation:Qe(hr)})},1061856:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe)=>{t.$b("ConvTranspose",f,{format:U?"NHWC":"NCHW",autoPad:b,dilations:Array.from((I(),_).subarray(Number($)>>>0,(Number($)>>>0)+2>>>0)),group:x,kernelShape:Array.from((I(),_).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((I(),_).subarray(Number(P)>>>0,(Number(P)>>>0)+4>>>0)),strides:Array.from((I(),_).subarray(Number(z)>>>0,(Number(z)>>>0)+2>>>0)),wIsConst:()=>!!(I(),X)[J>>>0],outputPadding:te?Array.from((I(),_).subarray(Number(te)>>>0,Number(ge)>>>0)):[],outputShape:Oe?Array.from((I(),_).subarray(Number(Oe)>>>0,Number(ze)>>>0)):[],activation:Qe(Fe)})},1062517:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe,hr)=>{t.$b("ConvTranspose",f,{format:J?"NHWC":"NCHW",autoPad:b,dilations:[$],group:x,kernelShape:[E],pads:[P,z],strides:[U],wIsConst:()=>!!(I(),X)[te>>>0],outputPadding:ge?Array.from((I(),_).subarray(Number(ge)>>>0,Number(Oe)>>>0)):[],outputShape:ze?Array.from((I(),_).subarray(Number(ze)>>>0,Number(Fe)>>>0)):[],activation:Qe(hr)})},1062950:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe)=>{t.$b("ConvTranspose",f,{format:U?"NHWC":"NCHW",autoPad:b,dilations:Array.from((I(),_).subarray(Number($)>>>0,(Number($)>>>0)+2>>>0)),group:x,kernelShape:Array.from((I(),_).subarray(Number(E)>>>0,(Number(E)>>>0)+2>>>0)),pads:Array.from((I(),_).subarray(Number(P)>>>0,(Number(P)>>>0)+4>>>0)),strides:Array.from((I(),_).subarray(Number(z)>>>0,(Number(z)>>>0)+2>>>0)),wIsConst:()=>!!(I(),X)[J>>>0],outputPadding:te?Array.from((I(),_).subarray(Number(te)>>>0,Number(ge)>>>0)):[],outputShape:Oe?Array.from((I(),_).subarray(Number(Oe)>>>0,Number(ze)>>>0)):[],activation:Qe(Fe)})},1063611:(f,b)=>{t.$b("GlobalAveragePool",f,{format:b?"NHWC":"NCHW"})},1063702:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe)=>{t.$b("AveragePool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:P?Array.from((I(),_).subarray(Number(P)>>>0,Number(z)>>>0)):[],kernel_shape:U?Array.from((I(),_).subarray(Number(U)>>>0,Number(J)>>>0)):[],pads:te?Array.from((I(),_).subarray(Number(te)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),_).subarray(Number(Oe)>>>0,Number(ze)>>>0)):[]})},1064181:(f,b)=>{t.$b("GlobalAveragePool",f,{format:b?"NHWC":"NCHW"})},1064272:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe)=>{t.$b("AveragePool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:P?Array.from((I(),_).subarray(Number(P)>>>0,Number(z)>>>0)):[],kernel_shape:U?Array.from((I(),_).subarray(Number(U)>>>0,Number(J)>>>0)):[],pads:te?Array.from((I(),_).subarray(Number(te)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),_).subarray(Number(Oe)>>>0,Number(ze)>>>0)):[]})},1064751:(f,b)=>{t.$b("GlobalMaxPool",f,{format:b?"NHWC":"NCHW"})},1064838:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe)=>{t.$b("MaxPool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:P?Array.from((I(),_).subarray(Number(P)>>>0,Number(z)>>>0)):[],kernel_shape:U?Array.from((I(),_).subarray(Number(U)>>>0,Number(J)>>>0)):[],pads:te?Array.from((I(),_).subarray(Number(te)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),_).subarray(Number(Oe)>>>0,Number(ze)>>>0)):[]})},1065313:(f,b)=>{t.$b("GlobalMaxPool",f,{format:b?"NHWC":"NCHW"})},1065400:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe)=>{t.$b("MaxPool",f,{format:Fe?"NHWC":"NCHW",auto_pad:b,ceil_mode:$,count_include_pad:x,storage_order:E,dilations:P?Array.from((I(),_).subarray(Number(P)>>>0,Number(z)>>>0)):[],kernel_shape:U?Array.from((I(),_).subarray(Number(U)>>>0,Number(J)>>>0)):[],pads:te?Array.from((I(),_).subarray(Number(te)>>>0,Number(ge)>>>0)):[],strides:Oe?Array.from((I(),_).subarray(Number(Oe)>>>0,Number(ze)>>>0)):[]})},1065875:(f,b,$,x,E)=>{t.$b("Gemm",f,{alpha:b,beta:$,transA:x,transB:E})},1065979:f=>{t.$b("MatMul",f,void 0)},1066033:(f,b,$,x)=>{t.$b("ArgMax",f,{keepDims:!!b,selectLastIndex:!!$,axis:x})},1066141:(f,b,$,x)=>{t.$b("ArgMin",f,{keepDims:!!b,selectLastIndex:!!$,axis:x})},1066249:(f,b)=>{t.$b("Softmax",f,{axis:b})},1066312:(f,b)=>{t.$b("Concat",f,{axis:b})},1066372:(f,b,$,x,E)=>{t.$b("Split",f,{axis:b,numOutputs:$,splitSizes:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1066528:f=>{t.$b("Expand",f,void 0)},1066582:(f,b)=>{t.$b("Gather",f,{axis:Number(b)})},1066653:(f,b)=>{t.$b("GatherElements",f,{axis:Number(b)})},1066732:(f,b)=>{t.$b("GatherND",f,{batch_dims:Number(b)})},1066811:(f,b,$,x,E,P,z,U,J,te,ge)=>{t.$b("Resize",f,{antialias:b,axes:$?Array.from((I(),_).subarray(Number($)>>>0,Number(x)>>>0)):[],coordinateTransformMode:Qe(E),cubicCoeffA:P,excludeOutside:z,extrapolationValue:U,keepAspectRatioPolicy:Qe(J),mode:Qe(te),nearestMode:Qe(ge)})},1067173:(f,b,$,x,E,P,z)=>{t.$b("Slice",f,{starts:b?Array.from((I(),_).subarray(Number(b)>>>0,Number($)>>>0)):[],ends:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[],axes:P?Array.from((I(),_).subarray(Number(P)>>>0,Number(z)>>>0)):[]})},1067437:f=>{t.$b("Tile",f,void 0)},1067489:(f,b,$)=>{t.$b("InstanceNormalization",f,{epsilon:b,format:$?"NHWC":"NCHW"})},1067603:(f,b,$)=>{t.$b("InstanceNormalization",f,{epsilon:b,format:$?"NHWC":"NCHW"})},1067717:f=>{t.$b("Range",f,void 0)},1067770:(f,b)=>{t.$b("Einsum",f,{equation:Qe(b)})},1067851:(f,b,$,x,E)=>{t.$b("Pad",f,{mode:b,value:$,pads:x?Array.from((I(),_).subarray(Number(x)>>>0,Number(E)>>>0)):[]})},1067994:(f,b,$,x,E,P)=>{t.$b("BatchNormalization",f,{epsilon:b,momentum:$,spatial:!!E,trainingMode:!!x,format:P?"NHWC":"NCHW"})},1068163:(f,b,$,x,E,P)=>{t.$b("BatchNormalization",f,{epsilon:b,momentum:$,spatial:!!E,trainingMode:!!x,format:P?"NHWC":"NCHW"})},1068332:(f,b,$)=>{t.$b("CumSum",f,{exclusive:Number(b),reverse:Number($)})},1068429:(f,b,$)=>{t.$b("DequantizeLinear",f,{axis:b,blockSize:$})},1068519:(f,b,$,x,E)=>{t.$b("GridSample",f,{align_corners:b,mode:Qe($),padding_mode:Qe(x),format:E?"NHWC":"NCHW"})},1068689:(f,b,$,x,E)=>{t.$b("GridSample",f,{align_corners:b,mode:Qe($),padding_mode:Qe(x),format:E?"NHWC":"NCHW"})},1068859:(f,b)=>{t.$b("ScatterND",f,{reduction:Qe(b)})},1068944:(f,b,$,x,E,P,z,U,J)=>{t.$b("Attention",f,{numHeads:b,isUnidirectional:$,maskFilterValue:x,scale:E,doRotary:P,qkvHiddenSizes:z?Array.from((I(),_).subarray(Number(U)>>>0,Number(U)+z>>>0)):[],pastPresentShareBuffer:!!J})},1069216:f=>{t.$b("BiasAdd",f,void 0)},1069271:f=>{t.$b("BiasSplitGelu",f,void 0)},1069332:f=>{t.$b("FastGelu",f,void 0)},1069388:(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe,hr,zs)=>{t.$b("Conv",f,{format:Oe?"NHWC":"NCHW",auto_pad:b,dilations:$?Array.from((I(),_).subarray(Number($)>>>0,Number(x)>>>0)):[],group:E,kernel_shape:P?Array.from((I(),_).subarray(Number(P)>>>0,Number(z)>>>0)):[],pads:U?Array.from((I(),_).subarray(Number(U)>>>0,Number(J)>>>0)):[],strides:te?Array.from((I(),_).subarray(Number(te)>>>0,Number(ge)>>>0)):[],w_is_const:()=>!!(I(),X)[Number(ze)>>>0],activation:Qe(Fe),activation_params:hr?Array.from((I(),V).subarray(Number(hr)>>>0,Number(zs)>>>0)):[]})},1069972:f=>{t.$b("Gelu",f,void 0)},1070024:(f,b,$,x,E,P,z,U,J)=>{t.$b("GroupQueryAttention",f,{numHeads:b,kvNumHeads:$,scale:x,softcap:E,doRotary:P,rotaryInterleaved:z,smoothSoftmax:U,localWindowSize:J})},1070241:(f,b,$,x)=>{t.$b("LayerNormalization",f,{axis:b,epsilon:$,simplified:!!x})},1070352:(f,b,$,x)=>{t.$b("LayerNormalization",f,{axis:b,epsilon:$,simplified:!!x})},1070463:(f,b,$,x,E,P)=>{t.$b("MatMulNBits",f,{k:b,n:$,accuracyLevel:x,bits:E,blockSize:P})},1070590:(f,b,$,x,E,P)=>{t.$b("MultiHeadAttention",f,{numHeads:b,isUnidirectional:$,maskFilterValue:x,scale:E,doRotary:P})},1070749:(f,b)=>{t.$b("QuickGelu",f,{alpha:b})},1070813:(f,b,$,x,E)=>{t.$b("RotaryEmbedding",f,{interleaved:!!b,numHeads:$,rotaryEmbeddingDim:x,scale:E})},1070952:(f,b,$)=>{t.$b("SkipLayerNormalization",f,{epsilon:b,simplified:!!$})},1071054:(f,b,$)=>{t.$b("SkipLayerNormalization",f,{epsilon:b,simplified:!!$})},1071156:(f,b,$,x)=>{t.$b("GatherBlockQuantized",f,{gatherAxis:b,quantizeAxis:$,blockSize:x})},1071277:f=>{t.Fd(f)},1071311:(f,b)=>t.Hd(Number(f),Number(b),t.Xc.Kd,t.Xc.errors)};function tI(f,b,$){return pc(async()=>{await t.Dd(Number(f),Number(b),Number($))})}function rI(){return typeof wasmOffsetConverter<"u"}function nI(f,b,$,x){var E=_e();try{return Kc(f,b,$,x)}catch(P){if(we(E),P!==P+0)throw P;Te(1,0)}}function iI(f,b,$){var x=_e();try{return Gc(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function oI(f){var b=_e();try{Lc(f)}catch($){if(we(b),$!==$+0)throw $;Te(1,0)}}function sI(f,b){var $=_e();try{return Rs(f,b)}catch(x){if(we($),x!==x+0)throw x;Te(1,0)}}function aI(f,b,$){var x=_e();try{jc(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function uI(f,b){var $=_e();try{Xc(f,b)}catch(x){if(we($),x!==x+0)throw x;Te(1,0)}}function lI(f,b,$,x,E,P,z){var U=_e();try{return Hc(f,b,$,x,E,P,z)}catch(J){if(we(U),J!==J+0)throw J;Te(1,0)}}function dI(f,b,$,x,E,P){var z=_e();try{Uc(f,b,$,x,E,P)}catch(U){if(we(z),U!==U+0)throw U;Te(1,0)}}function cI(f,b,$,x){var E=_e();try{Wc(f,b,$,x)}catch(P){if(we(E),P!==P+0)throw P;Te(1,0)}}function pI(f,b,$,x,E){var P=_e();try{Vc(f,b,$,x,E)}catch(z){if(we(P),z!==z+0)throw z;Te(1,0)}}function hI(f,b,$,x,E,P,z){var U=_e();try{Yc(f,b,$,x,E,P,z)}catch(J){if(we(U),J!==J+0)throw J;Te(1,0)}}function fI(f,b,$,x,E,P,z){var U=_e();try{Jc(f,b,$,x,E,P,z)}catch(J){if(we(U),J!==J+0)throw J;Te(1,0)}}function mI(f,b,$,x,E,P,z,U){var J=_e();try{rp(f,b,$,x,E,P,z,U)}catch(te){if(we(J),te!==te+0)throw te;Te(1,0)}}function gI(f,b,$,x,E){var P=_e();try{return Zc(f,b,$,x,E)}catch(z){if(we(P),z!==z+0)throw z;Te(1,0)}}function bI(f,b,$){var x=_e();try{return np(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function yI(f,b,$,x,E,P,z,U){var J=_e();try{ip(f,b,$,x,E,P,z,U)}catch(te){if(we(J),te!==te+0)throw te;Te(1,0)}}function wI(f,b,$,x,E,P,z,U,J,te,ge,Oe){var ze=_e();try{Qc(f,b,$,x,E,P,z,U,J,te,ge,Oe)}catch(Fe){if(we(ze),Fe!==Fe+0)throw Fe;Te(1,0)}}function _I(f,b,$){var x=_e();try{return op(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;return Te(1,0),0n}}function vI(f,b,$,x,E,P,z,U,J){var te=_e();try{qc(f,b,$,x,E,P,z,U,J)}catch(ge){if(we(te),ge!==ge+0)throw ge;Te(1,0)}}function xI(f){var b=_e();try{return sp(f)}catch($){if(we(b),$!==$+0)throw $;Te(1,0)}}function $I(f,b){var $=_e();try{return vp(f,b)}catch(x){if(we($),x!==x+0)throw x;return Te(1,0),0n}}function TI(f){var b=_e();try{return ap(f)}catch($){if(we(b),$!==$+0)throw $;return Te(1,0),0n}}function SI(f,b,$,x){var E=_e();try{return hp(f,b,$,x)}catch(P){if(we(E),P!==P+0)throw P;Te(1,0)}}function II(f,b,$,x,E){var P=_e();try{return fp(f,b,$,x,E)}catch(z){if(we(P),z!==z+0)throw z;Te(1,0)}}function OI(f,b,$,x,E,P){var z=_e();try{return mp(f,b,$,x,E,P)}catch(U){if(we(z),U!==U+0)throw U;Te(1,0)}}function EI(f,b,$,x,E,P){var z=_e();try{return ep(f,b,$,x,E,P)}catch(U){if(we(z),U!==U+0)throw U;Te(1,0)}}function AI(f,b,$,x,E,P){var z=_e();try{return gp(f,b,$,x,E,P)}catch(U){if(we(z),U!==U+0)throw U;Te(1,0)}}function kI(f,b,$,x,E,P,z,U){var J=_e();try{return tp(f,b,$,x,E,P,z,U)}catch(te){if(we(J),te!==te+0)throw te;Te(1,0)}}function PI(f,b,$,x,E){var P=_e();try{return bp(f,b,$,x,E)}catch(z){if(we(P),z!==z+0)throw z;return Te(1,0),0n}}function CI(f,b,$,x){var E=_e();try{return yp(f,b,$,x)}catch(P){if(we(E),P!==P+0)throw P;Te(1,0)}}function DI(f,b,$,x){var E=_e();try{return wp(f,b,$,x)}catch(P){if(we(E),P!==P+0)throw P;Te(1,0)}}function RI(f,b,$,x,E,P,z,U,J,te,ge,Oe){var ze=_e();try{return _p(f,b,$,x,E,P,z,U,J,te,ge,Oe)}catch(Fe){if(we(ze),Fe!==Fe+0)throw Fe;Te(1,0)}}function NI(f,b,$,x,E,P,z,U,J,te,ge){var Oe=_e();try{cp(f,b,$,x,E,P,z,U,J,te,ge)}catch(ze){if(we(Oe),ze!==ze+0)throw ze;Te(1,0)}}function zI(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe,hr,zs){var jI=_e();try{pp(f,b,$,x,E,P,z,U,J,te,ge,Oe,ze,Fe,hr,zs)}catch(Ms){if(we(jI),Ms!==Ms+0)throw Ms;Te(1,0)}}function MI(f,b,$){var x=_e();try{return up(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function BI(f,b,$){var x=_e();try{return lp(f,b,$)}catch(E){if(we(x),E!==E+0)throw E;Te(1,0)}}function FI(f,b,$,x){var E=_e();try{dp(f,b,$,x)}catch(P){if(we(E),P!==P+0)throw P;Te(1,0)}}function so(){if(0{g=f,v=b})}var Lv,em,kA=N(()=>{"use strict";Lv=Qf,em=globalThis.self?.name?.startsWith("em-pthread"),em&&Qf()}),Ta,cl,tm,bt,Uv,fo,rm,nm,Sa,im,Ia,Vv,Oa,Gv,Bl=N(()=>{"use strict";Ml(),Ta=typeof location>"u"?void 0:location.origin,cl=import.meta.url>"file:"&&import.meta.url<"file;",tm=()=>{if(cl){let e=URL;return new URL(new e("ort.all.bundle.min.mjs",import.meta.url).href,Ta).href}return import.meta.url},bt=tm(),Uv=()=>{if(bt&&!bt.startsWith("blob:"))return bt.substring(0,bt.lastIndexOf("/")+1)},fo=(e,t)=>{try{let n=t??bt;return(n?new URL(e,n):new URL(e)).origin===Ta}catch{return!1}},rm=(e,t)=>{let n=t??bt;try{return(n?new URL(e,n):new URL(e)).href}catch{return}},nm=(e,t)=>`${t??"./"}${e}`,Sa=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},im=async e=>(await import(e)).default,Ia=(AA(),Cn(Bv)).default,Vv=async()=>{if(!bt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(fo(bt))return[void 0,Ia()];let e=await Sa(bt);return[e,Ia(e)]},Oa=(kA(),Cn(jv)).default,Gv=async(e,t,n,i)=>{let u=Oa&&!(e||t);if(u)if(bt)u=fo(bt)||i&&!n;else if(i&&!n)u=!0;else throw new Error("cannot determine the script source URL.");if(u)return[void 0,Oa];{let l="ort-wasm-simd-threaded.jsep.mjs",d=e??rm(l,t),c=n&&d&&!fo(d,t),s=c?await Sa(d):d??nm(l,t);return[c?s:void 0,await im(s)]}}}),Ea,mo,ui,Aa,om,sm,am,Fl,Be,yn=N(()=>{"use strict";Bl(),mo=!1,ui=!1,Aa=!1,om=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},sm=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},am=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},Fl=async e=>{if(mo)return Promise.resolve();if(ui)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Aa)throw new Error("previous call to 'initializeWebAssembly()' failed.");ui=!0;let t=e.initTimeout,n=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!am())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!sm())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let i=om();n>1&&!i&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+n+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=n=1);let u=e.wasmPaths,l=typeof u=="string"?u:void 0,d=u?.mjs,c=d?.href??d,s=u?.wasm,r=s?.href??s,o=e.wasmBinary,[a,p]=await Gv(c,l,n>1,!!o||!!r),h=!1,m=[];if(t>0&&m.push(new Promise(g=>{setTimeout(()=>{h=!0,g()},t)})),m.push(new Promise((g,v)=>{let w={numThreads:n};if(o)w.wasmBinary=o,w.locateFile=y=>y;else if(r||l)w.locateFile=y=>r??l+y;else if(c&&c.indexOf("blob:")!==0)w.locateFile=y=>new URL(y,c).href;else if(a){let y=Uv();y&&(w.locateFile=T=>y+T)}p(w).then(y=>{ui=!1,mo=!0,Ea=y,g(),a&&URL.revokeObjectURL(a)},y=>{ui=!1,Aa=!0,v(y)})})),await Promise.race(m),h)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},Be=()=>{if(mo&&Ea)return Ea;throw new Error("WebAssembly is not initialized yet.")}}),Ut,zo,De,jl=N(()=>{"use strict";yn(),Ut=(e,t)=>{let n=Be(),i=n.lengthBytesUTF8(e)+1,u=n._malloc(i);return n.stringToUTF8(e,u,i),t.push(u),u},zo=(e,t,n,i)=>{if(typeof e=="object"&&e!==null){if(n.has(e))throw new Error("Circular reference in options");n.add(e)}Object.entries(e).forEach(([u,l])=>{let d=t?t+u:u;if(typeof l=="object")zo(l,d+".",n,i);else if(typeof l=="string"||typeof l=="number")i(d,l.toString());else if(typeof l=="boolean")i(d,l?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof l}`)})},De=e=>{let t=Be(),n=t.stackSave();try{let i=t.PTR_SIZE,u=t.stackAlloc(2*i);t._OrtGetLastError(u,u+i);let l=Number(t.getValue(u,i===4?"i32":"i64")),d=t.getValue(u+i,"*"),c=d?t.UTF8ToString(d):"";throw new Error(`${e} ERROR_CODE: ${l}, ERROR_MESSAGE: ${c}`)}finally{t.stackRestore(n)}}}),qv,PA=N(()=>{"use strict";yn(),jl(),qv=e=>{let t=Be(),n=0,i=[],u=e||{};try{if(e?.logSeverityLevel===void 0)u.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)u.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(u.terminate=!1);let l=0;return e?.tag!==void 0&&(l=Ut(e.tag,i)),n=t._OrtCreateRunOptions(u.logSeverityLevel,u.logVerbosityLevel,!!u.terminate,l),n===0&&De("Can't create run options."),e?.extra!==void 0&&zo(e.extra,"",new WeakSet,(d,c)=>{let s=Ut(d,i),r=Ut(c,i);t._OrtAddRunConfigEntry(n,s,r)!==0&&De(`Can't set a run config entry: ${d} - ${c}.`)}),[n,i]}catch(l){throw n!==0&&t._OrtReleaseRunOptions(n),i.forEach(d=>t._free(d)),l}}}),um,lm,dm,Lr,cm,Hv,CA=N(()=>{"use strict";yn(),jl(),um=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},lm=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},dm=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(n=>(typeof n=="string"?n:n.name)==="webgpu")&&(e.enableMemPattern=!1)},Lr=(e,t,n,i)=>{let u=Ut(t,i),l=Ut(n,i);Be()._OrtAddSessionConfigEntry(e,u,l)!==0&&De(`Can't set a session config entry: ${t} - ${n}.`)},cm=async(e,t,n)=>{let i=t.executionProviders;for(let u of i){let l=typeof u=="string"?u:u.name,d=[];switch(l){case"webnn":if(l="WEBNN",Lr(e,"session.disable_quant_qdq","1",n),Lr(e,"session.disable_qdq_constant_folding","1",n),typeof u!="string"){let a=u?.deviceType;a&&Lr(e,"deviceType",a,n)}break;case"webgpu":if(l="JS",typeof u!="string"){let a=u;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);Lr(e,"preferredLayout",a.preferredLayout,n)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${l}`)}let c=Ut(l,n),s=d.length,r=0,o=0;if(s>0){r=Be()._malloc(s*Be().PTR_SIZE),n.push(r),o=Be()._malloc(s*Be().PTR_SIZE),n.push(o);for(let a=0;a{let t=Be(),n=0,i=[],u=e||{};dm(u);try{let l=um(u.graphOptimizationLevel??"all"),d=lm(u.executionMode??"sequential"),c=typeof u.logId=="string"?Ut(u.logId,i):0,s=u.logSeverityLevel??2;if(!Number.isInteger(s)||s<0||s>4)throw new Error(`log severity level is not valid: ${s}`);let r=u.logVerbosityLevel??0;if(!Number.isInteger(r)||r<0||r>4)throw new Error(`log verbosity level is not valid: ${r}`);let o=typeof u.optimizedModelFilePath=="string"?Ut(u.optimizedModelFilePath,i):0;if(n=t._OrtCreateSessionOptions(l,!!u.enableCpuMemArena,!!u.enableMemPattern,d,!!u.enableProfiling,0,c,s,r,o),n===0&&De("Can't create session options."),u.executionProviders&&await cm(n,u,i),u.enableGraphCapture!==void 0){if(typeof u.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${u.enableGraphCapture}`);Lr(n,"enableGraphCapture",u.enableGraphCapture.toString(),i)}if(u.freeDimensionOverrides)for(let[a,p]of Object.entries(u.freeDimensionOverrides)){if(typeof a!="string")throw new Error(`free dimension override name must be a string: ${a}`);if(typeof p!="number"||!Number.isInteger(p)||p<0)throw new Error(`free dimension override value must be a non-negative integer: ${p}`);let h=Ut(a,i);t._OrtAddFreeDimensionOverride(n,h,p)!==0&&De(`Can't set a free dimension override: ${a} - ${p}.`)}return u.extra!==void 0&&zo(u.extra,"",new WeakSet,(a,p)=>{Lr(n,a,p,i)}),[n,i]}catch(l){throw n!==0&&t._OrtReleaseSessionOptions(n)!==0&&De("Can't release session options."),i.forEach(d=>t._free(d)),l}}}),an,or,un,Yo,Mo,Ll,Ul,pl,le=N(()=>{"use strict";an=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},or=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},un=(e,t)=>{let n=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],i=typeof t=="number"?t:t.reduce((u,l)=>u*l,1);return n>0?Math.ceil(i*n):void 0},Yo=e=>{switch(e){case"float16":return typeof Float16Array<"u"?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Mo=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Ll=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Ul=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",pl=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),Vl,Wv=N(()=>{"use strict";Ml(),Vl=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let n=t.headers.get("Content-Length"),i=n?parseInt(n,10):0;if(i<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let u=t.body.getReader(),l;try{l=new ArrayBuffer(i)}catch(c){if(c instanceof RangeError){let s=Math.ceil(i/65536);l=new WebAssembly.Memory({initial:s,maximum:s}).buffer}else throw c}let d=0;for(;;){let{done:c,value:s}=await u.read();if(c)break;let r=s.byteLength;new Uint8Array(l,d,r).set(s),d+=r}return new Uint8Array(l,0,i)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),pm,hm,fm,mm,Gl,gm,Ie,ar=N(()=>{"use strict";le(),pm=["V","I","W","E","F"],hm=(e,t)=>{console.log(`[${pm[e]},${new Date().toISOString()}]${t}`)},Gl=(e,t)=>{fm=e,mm=t},gm=(e,t)=>{let n=Mo(e),i=Mo(fm);n>=i&&hm(n,typeof t=="function"?t():t)},Ie=(...e)=>{mm&&gm(...e)}}),bm,Rn,B,Bo,Kv,Xv,Zv,pe=N(()=>{"use strict";bm=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},Rn=class{static calcShape(e,t,n=!1){let i=e.length,u=t.length;if(i===0)return t;if(u===0)return e;let l=Math.max(e.length,t.length),d=new Array(l);if(n){if(i<2||u<2)return;let c=bm.calcMatMulShape([e[i-2],e[i-1]],[t[u-2],t[u-1]]);if(c===void 0)return;[d[l-2],d[l-1]]=c}for(let c=n?3:1;c<=l;c++){let s=i-c<0?1:e[i-c],r=u-c<0?1:t[u-c];if(s!==r&&s>1&&r>1)return;let o=Math.max(s,r);if(s&&r)d[l-c]=Math.max(s,r);else{if(o>1)return;d[l-c]=0}}return d}static isValidBroadcast(e,t){let n=e.length,i=t.length;if(n>i)return!1;for(let u=1;u<=n;u++)if(e[n-u]!==1&&e[n-u]!==t[i-u])return!1;return!0}},B=class Ao{static size(t){return Ao.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,n=4){let i=t.length;if(i===0)return[];let u=new Array(i),l=i-1;for(;l>=0;){if(t[l]%n===0){u[l]=t[l]/n;break}if(n%t[l]!==0)throw new Error("cannot convert shape");u[l]=1,n/=t[l],l--}for(l--;l>=0;l--)u[l]=t[l];return u}static sizeFromDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return Ao.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return Ao.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,i){let u=1;for(let l=n;l=0;--u)i[u]=i[u+1]*t[u+1];return i}static normalizeAxis(t,n){if(t<-n&&t>=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(i=>this.normalizeAxis(i,n??t.length))}static sortBasedOnPerm(t,n){return n?n.map(i=>t[i]):t.slice().reverse()}static padShape(t,n){let i=t.length;return t.map((u,l)=>u+n[l]+n[l+i])}static areEqual(t,n){return t.length!==n.length?!1:t.every((i,u)=>i===n[u])}},Bo=class Sr{static adjustPoolAttributes(t,n,i,u,l,d){if(!t&&i.length!==n.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let c=0;c=i.length?i.push(n[c+2]):i[c]=n[c+2];for(let c=0;c=i[c]||d[c+i.length]>=i[c])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,i,u,l,d,c){if(c){if(l.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(u.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let s=0;s=i+u&&(d-=1)),d}static adjustPadAndReturnShape(t,n,i,u,l,d,c,s,r=0){let o=i*(u-1)+1;if(s&&s!=="NOTSET")switch(s){case"VALID":return l[d]=0,l[c]=0,Sr.computeOutputSize(t-o,n,t,0,r);case"SAME_LOWER":case"SAME_UPPER":if(i!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let a=(Math.floor((t+n-1)/n)-1)*n+u-t;return l[d]=Math.floor(s==="SAME_LOWER"?(a+1)/2:a/2),l[c]=a-l[d],Sr.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}default:throw new Error("Unsupported AutoPad type")}else return Sr.computeOutputSize(t+l[d]+l[c]-o,n,t,l[d],r)}},Kv=class{static getShapeOfGemmResult(e,t,n,i,u){if(e.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let l,d,c;t?(l=e[1],d=e[0]):(l=e[0],d=e[1]);let s=-1;if(i?(c=n[0],s=1):(c=n[1],s=0),n[s]!==d)throw new Error("dimension mismatch");if(l<=0||c<=0||d<=0)throw new Error("invalid shape specified");if(u&&!Rn.isValidBroadcast(u,[l,c]))throw new Error("gemm: invalid bias shape for broadcast");return[l,c,d]}},Xv=-34028234663852886e22,Zv=34028234663852886e22}),ql,Yv=N(()=>{"use strict";le(),ql=(e,t)=>new(Yo(t))(e)}),ka,ym,Pa,wm,Ca,_m,Da,Ra,Na,vm,Jv,DA=N(()=>{"use strict";le(),ar(),ka=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),ym=(e,t)=>{if(t==="int32")return e;let n=ka.get(t);if(!n)throw new Error(`WebNN backend does not support data type: ${t}`);let i=n/8;if(e.byteLength%i!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${i}.`);let u=e.byteLength/i,l=new(Yo(t))(e.buffer,e.byteOffset,u);switch(t){case"int64":case"uint64":{let d=new Int32Array(u);for(let c=0;c2147483647n||s<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");d[c]=Number(s)}return new Uint8Array(d.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&l.some(c=>c>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let d=Int32Array.from(l,Number);return new Uint8Array(d.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},Pa=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let n=e.byteLength/4,i=new Int32Array(e.buffer,e.byteOffset,n);switch(t){case"int64":{let u=BigInt64Array.from(i,BigInt);return new Uint8Array(u.buffer)}case"uint64":{if(i.some(l=>l<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let u=BigUint64Array.from(i,BigInt);return new Uint8Array(u.buffer)}case"int8":{if(i.some(l=>l<-128||l>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let u=Int8Array.from(i,Number);return new Uint8Array(u.buffer)}case"uint8":{if(i.some(u=>u<0||u>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(i,Number)}case"uint32":{if(i.some(l=>l<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let u=Uint32Array.from(i,Number);return new Uint8Array(u.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},wm=1,Ca=()=>wm++,_m=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),Da=(e,t)=>{let n=ka.get(e);if(!n)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((i,u)=>i*u)*n/8):0},Ra=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:n,tensor:i,dataType:u,shape:l,fallbackDataType:d}=e;this.sessionId=t,this.mlContext=n,this.mlTensor=i,this.dataType=u,this.tensorShape=l,this.fallbackDataType=d}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return Da(this.dataType,this.tensorShape)}destroy(){Ie("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),n=Pa(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(n);return}else return new Uint8Array(n).buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,n){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===n.length&&this.tensorShape.every((i,u)=>i===n[u])}setIsDataConverted(e){this.isDataConverted=e}},Na=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(e,t,n,i){let u=this.tensorManager.getMLContext(e),l=this.tensorManager.getMLOpSupportLimits(e),d;if(!l?.input.dataTypes.includes(t)){if(d=_m.get(t),!d||l?.input.dataTypes.includes(d))throw new Error(`WebNN backend does not support data type: ${t}`);Ie("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${d}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(u,t,n))return this.wrapper.tensor;if(i){if(this.wrapper.byteLength!==Da(t,n))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let c=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,n,c,!0,!0,d),i&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=ym(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else Ie("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){if(this.activeUpload){let t=this.wrapper?.isDataConverted?Pa(this.activeUpload,this.wrapper?.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(t):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(t);return}else return t.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},vm=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=Ca();return this.tensorTrackersById.set(e,new Na(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,n,i,u){Ie("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${n}, shape: ${i}, copyOld: ${u}}`);let l=this.tensorTrackersById.get(t);if(!l)throw new Error("Tensor not found.");return l.ensureTensor(e,n,i,u)}upload(e,t){let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");n.upload(t)}async download(e,t){Ie("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t?.byteLength}}`);let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");return n.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}registerTensor(e,t,n,i){let u=this.getMLContext(e),l=Ca(),d=new Ra({sessionId:e,context:u,tensor:t,dataType:n,shape:i});return this.tensorTrackersById.set(l,new Na(this,d)),this.externalTensors.add(d),l}async getCachedTensor(e,t,n,i,u,l,d){let c=this.getMLContext(e);for(let[r,o]of this.freeTensors.entries())if(o.canReuseTensor(c,t,n)){Ie("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}`);let a=this.freeTensors.splice(r,1)[0];return a.sessionId=e,a}Ie("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${d?`fallbackDataType: ${d},`:""} shape: ${n}}`);let s=await c.createTensor({dataType:d??t,shape:n,dimensions:n,usage:i,writable:u,readable:l});return new Ra({sessionId:e,context:c,tensor:s,dataType:t,shape:n,fallbackDataType:d})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},Jv=(...e)=>new vm(...e)}),li,xm,Qv,RA=N(()=>{"use strict";le(),yn(),Yv(),DA(),ar(),li=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),xm=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let n=Object.keys(e).sort(),i=Object.keys(t).sort();return n.length===i.length&&n.every((u,l)=>u===i[l]&&e[u]===t[u])},Qv=class{constructor(e){this.tensorManager=Jv(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,Gl(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){Ie("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){Ie("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let n of t)Ie("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${n}}`),this.tensorManager.releaseTensorId(n);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let n=this.mlContextCache.findIndex(i=>i.gpuDevice===e);if(n!==-1)return this.mlContextCache[n].mlContext;{let i=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:i}),i}}else if(e===void 0){let n=this.mlContextCache.findIndex(i=>i.options===void 0&&i.gpuDevice===void 0);if(n!==-1)return this.mlContextCache[n].mlContext;{let i=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:i}),i}}let t=this.mlContextCache.findIndex(n=>xm(n.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let n=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:n}),n}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let n=this.sessionIdsByMLContext.get(t);n||(n=new Set,this.sessionIdsByMLContext.set(t,n)),n.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let n=this.sessionIdsByMLContext.get(t);if(n.delete(e),n.size===0){this.sessionIdsByMLContext.delete(t);let i=this.mlContextCache.findIndex(u=>u.mlContext===t);i!==-1&&this.mlContextCache.splice(i,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){Ie("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,n,i,u){let l=li.get(n);if(!l)throw new Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,l,i,u)}async createTemporaryTensor(e,t,n){Ie("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${n}}`);let i=li.get(t);if(!i)throw new Error(`Unsupported ONNX data type: ${t}`);let u=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,u,i,n,!1);let l=this.temporarySessionTensorIds.get(e);return l?l.push(u):this.temporarySessionTensorIds.set(e,[u]),u}uploadTensor(e,t){if(!Be().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");Ie("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let n=await this.tensorManager.download(e);return ql(n,t)}}registerMLTensor(e,t,n,i){let u=li.get(n);if(!u)throw new Error(`Unsupported ONNX data type: ${n}`);let l=this.tensorManager.registerTensor(e,t,u,i);return Ie("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${u}, dimensions: ${i}} -> {tensorId: ${l}}`),l}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let n=this.sessionGraphInputs.get(e);return n?n.includes(t):!1}isGraphOutput(e,t){let n=this.sessionGraphOutputs.get(e);return n?n.includes(t):!1}isGraphInputOutputTypeSupported(e,t,n=!0){let i=li.get(an(t)),u=this.mlOpSupportLimitsBySessionId.get(e);return typeof i>"u"?!1:n?!!u?.input.dataTypes.includes(i):!!u?.output.dataTypes.includes(i)}flush(){}}}),Hl=N(()=>{"use strict"}),za,go,bo,$m,Tm,Ma,hl,Sm,ex,NA=N(()=>{"use strict";ar(),Hl(),za=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),go=[],bo=e=>Math.ceil(Number(e)/16)*16,$m=e=>{for(let t=0;tTm++,hl=async(e,t,n,i)=>{let u=bo(n),l=e.device.createBuffer({size:u,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let d=e.getCommandEncoder();e.endComputePass(),d.copyBufferToBuffer(t,0,l,0,u),e.flush(),await l.mapAsync(GPUMapMode.READ);let c=l.getMappedRange();if(i){let s=i();return s.set(new Uint8Array(c,0,n)),s}else return new Uint8Array(c.slice(0,n))}finally{l.destroy()}},Sm=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of za)go.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let n=t.buffer,i=t.byteOffset,u=t.byteLength,l=bo(u),d=this.storageCache.get(e);if(!d)throw new Error("gpu data for uploading does not exist");if(Number(d.originalSize)!==u)throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${u}`);if(l===u&&i%4===0)this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,n,i,u);else{let c=new Uint8Array(l);c.set(t),this.backend.device.queue.writeBuffer(d.gpuData.buffer,0,c,0,l)}Ie("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let n=this.storageCache.get(e);if(!n)throw new Error("source gpu data for memcpy does not exist");let i=this.storageCache.get(t);if(!i)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==i.originalSize)throw new Error("inconsistent source and destination gpu data size");let u=bo(n.originalSize),l=this.backend.getCommandEncoder();this.backend.endComputePass(),l.copyBufferToBuffer(n.gpuData.buffer,0,i.gpuData.buffer,0,u)}registerExternalBuffer(e,t,n){let i;if(n){if(i=n[0],e===n[1])return Ie("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, buffer is the same, skip.`),i;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. + Please use the previous external buffer!`)}else i=Ma();return this.storageCache.set(i,{gpuData:{id:i,type:0,buffer:e},originalSize:t}),Ie("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, registered.`),i}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),Ie("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=$m(e),i,u=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,l=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(u||l){let c=(u?this.freeBuffers:this.freeUniformBuffers).get(n);c?c.length>0?i=c.pop():i=this.backend.device.createBuffer({size:n,usage:t}):i=this.backend.device.createBuffer({size:n,usage:t})}else i=this.backend.device.createBuffer({size:n,usage:t});let d={id:Ma(),type:0,buffer:i};return this.storageCache.set(d.id,{gpuData:d,originalSize:Number(e)}),Ie("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${d.id}`),d}get(e){return this.storageCache.get(e)?.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,n=this.storageCache.get(t);if(!n){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return Ie("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${n.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(n.gpuData.buffer),n.originalSize}async download(e,t){let n=this.storageCache.get(Number(e));if(!n)throw new Error("data does not exist");await hl(this.backend,n.gpuData.buffer,n.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=za.get(e.size);if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(Ie("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(n=>{n.gpuData.buffer.destroy()}),this.storageCache=new Map)}},ex=(...e)=>new Sm(...e)}),Im,ke,He=N(()=>{"use strict";Im=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},ke=e=>new Im(e)}),Nn,yo,rt,tt,ae,qe,fl,Pn,Or,oe,di,L,ne,tx,Wl,Om,rx,he=N(()=>{"use strict";le(),pe(),Nn=64,yo=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},rt=(e,t=1)=>{let n=yo(e,t);return typeof n=="string"?n:n[0]},tt=(e,t=1)=>{let n=yo(e,t);return typeof n=="string"?n:n[1]},ae=(...e)=>{let t=[];return e.forEach(n=>{n.length!==0&&t.push({type:12,data:n},{type:12,data:B.computeStrides(n)})}),t},qe=e=>e%4===0?4:e%2===0?2:1,fl=(e="f32",t,n="0")=>!t||t===1?`${e}(${n})`:`vec${t}<${e}>(${n})`,Pn=(e,t,n)=>e==="f32"?n:t===1?`f32(${n})`:`vec${t}(${n})`,Or=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,oe=(e,t,n,i)=>e.startsWith("uniforms.")&&n>4?typeof t=="string"?i==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:i==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:n>1?`${e}[${t}]`:e,di=(e,t,n,i,u)=>{let l=typeof n=="number",d=l?n:n.length,c=[...new Array(d).keys()],s=d<2?"u32":d<=4?`vec${d}`:`array`,r=yo(t,u),o=typeof r=="string"?r:r[1],a=typeof r=="string"?r:r[0],p={indices:s,value:o,storage:a,tensor:t},h=F=>typeof F=="string"?F:`${F}u`,m={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},g=l?"uniforms.":"",v=`${g}${e}_shape`,w=`${g}${e}_strides`,y="";for(let F=0;F ${p.indices} { var indices: ${p.indices}; var current = offset; ${y} return indices; - }`,T=M=>(m.offsetToIndices=!0,d<2?M:`o2i_${e}(${M})`),I=[];if(d>=2)for(let M=d-1;M>=0;M--)I.push(`${ne(w,M,d)} * (indices[${M}])`);let A=d<2?"":` + }`,S=F=>(m.offsetToIndices=!0,d<2?F:`o2i_${e}(${F})`),O=[];if(d>=2)for(let F=d-1;F>=0;F--)O.push(`${oe(w,F,d)} * (indices[${F}])`);let A=d<2?"":` fn i2o_${e}(indices: ${p.indices}) -> u32 { - return ${I.join("+")}; - }`,P=M=>(m.indicesToOffset=!0,d<2?M:`i2o_${e}(${M})`),O=(...M)=>d===0?"0u":`${p.indices}(${M.map(h).join(",")})`,B=(M,J)=>d<2?`${M}`:`${ne(M,J,d)}`,U=(M,J,Z)=>d<2?`${M}=${Z};`:`${ne(M,J,d)}=${Z};`,X={},q=(M,J)=>{m.broadcastedIndicesToOffset=!0;let Z=`${J.name}broadcastedIndicesTo${e}Offset`;if(Z in X)return`${Z}(${M})`;let W=[];for(let pe=d-1;pe>=0;pe--){let fe=J.indicesGet("outputIndices",pe+J.rank-d);W.push(`${B(w,pe)} * (${fe} % ${B(x,pe)})`)}return X[Z]=`fn ${Z}(outputIndices: ${J.type.indices}) -> u32 { - return ${W.length>0?W.join("+"):"0u"}; - }`,`${Z}(${M})`},D=(M,J)=>(()=>{if(p.storage===p.value)return`${e}[${M}]=${J};`;if(p.storage==="vec2"&&p.value==="i32")return`${e}[${M}]=vec2(u32(${J}), select(0u, 0xFFFFFFFFu, ${J} < 0));`;if(p.storage==="vec2"&&p.value==="u32")return`${e}[${M}]=vec2(u32(${J}), 0u);`;if(p.storage==="u32"&&p.value==="vec4")return`${e}[${M}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${J}));`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),Q=M=>(()=>{if(p.storage===p.value)return`${e}[${M}]`;if(p.storage==="vec2"&&p.value==="i32")return`i32(${e}[${M}].x)`;if(p.storage==="vec2"&&p.value==="u32")return`u32(${e}[${M}].x)`;if(p.storage==="u32"&&p.value==="vec4")return`vec4(bool(${e}[${M}] & 0xFFu), bool(${e}[${M}] & 0xFF00u), bool(${e}[${M}] & 0xFF0000u), bool(${e}[${M}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),_=d<2?"":` - fn get_${e}ByIndices(indices: ${p.indices}) -> ${i} { - return ${Q(`i2o_${e}(indices)`)}; - }`,C=d<2?"":(()=>{let M=c.map(Z=>`d${Z}: u32`).join(", "),J=c.map(Z=>`d${Z}`).join(", ");return` - fn get_${e}(${M}) -> ${i} { - return get_${e}ByIndices(${O(J)}); - }`})(),F=(...M)=>{if(M.length!==d)throw new Error(`indices length must be ${d}`);let J=M.map(h).join(",");return d===0?Q("0u"):d===1?Q(J[0]):(m.get=!0,m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}(${J})`)},V=M=>d<2?Q(M):(m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}ByIndices(${M})`),G=d<2?"":` - fn set_${e}ByIndices(indices: ${p.indices}, value: ${i}) { - ${D(`i2o_${e}(indices)`,"value")} - }`,H=d<2?"":(()=>{let M=c.map(Z=>`d${Z}: u32`).join(", "),J=c.map(Z=>`d${Z}`).join(", ");return` - fn set_${e}(${M}, value: ${i}) { - set_${e}ByIndices(${O(J)}, value); - }`})();return{impl:()=>{let M=[],J=!1;return m.offsetToIndices&&(M.push(S),J=!0),m.indicesToOffset&&(M.push(A),J=!0),m.broadcastedIndicesToOffset&&(Object.values(X).forEach(Z=>M.push(Z)),J=!0),m.set&&(M.push(H),J=!0),m.setByIndices&&(M.push(G),J=!0),m.get&&(M.push(C),J=!0),m.getByIndices&&(M.push(_),J=!0),!l&&J&&M.unshift(`const ${x} = ${p.indices}(${n.join(",")});`,`const ${w} = ${p.indices}(${z.computeStrides(n).join(",")});`),M.join(` -`)},type:p,offsetToIndices:T,indicesToOffset:P,broadcastedIndicesToOffset:q,indices:O,indicesGet:B,indicesSet:U,set:(...M)=>{if(M.length!==d+1)throw new Error(`indices length must be ${d}`);let J=M[d];if(typeof J!="string")throw new Error("value must be string");let Z=M.slice(0,d).map(h).join(",");return d===0?D("0u",J):d===1?D(Z[0],J):(m.set=!0,m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}(${Z}, ${J})`)},setByOffset:D,setByIndices:(M,J)=>d<2?D(M,J):(m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}ByIndices(${M}, ${J});`),get:F,getByOffset:Q,getByIndices:V,usage:o,name:e,strides:w,shape:x,rank:d}},j=(e,t,n,o=1)=>ni(e,t,n,"input",o),re=(e,t,n,o=1)=>ni(e,t,n,"output",o),Kv=(e,t,n)=>ni(e,t,n,"atomicOutput",1),Vl=(e,t,n,o=1)=>ni(e,t,n,"internal",o),_m=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=On){let t=typeof e=="number"?e:e[0],n=typeof e=="number"?1:e[1],o=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*n*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let u=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,l=u?`@builtin(global_invocation_id) global_id : vec3, + return ${O.join("+")}; + }`,k=F=>(m.indicesToOffset=!0,d<2?F:`i2o_${e}(${F})`),I=(...F)=>d===0?"0u":`${p.indices}(${F.map(h).join(",")})`,R=(F,Q)=>d<2?`${F}`:`${oe(F,Q,d)}`,M=(F,Q,Y)=>d<2?`${F}=${Y};`:`${oe(F,Q,d)}=${Y};`,X={},G=(F,Q)=>{m.broadcastedIndicesToOffset=!0;let Y=`${Q.name}broadcastedIndicesTo${e}Offset`;if(Y in X)return`${Y}(${F})`;let j=[];for(let ee=d-1;ee>=0;ee--){let ie=Q.indicesGet("outputIndices",ee+Q.rank-d);j.push(`${R(w,ee)} * (${ie} % ${R(v,ee)})`)}return X[Y]=`fn ${Y}(outputIndices: ${Q.type.indices}) -> u32 { + return ${j.length>0?j.join("+"):"0u"}; + }`,`${Y}(${F})`},C=(F,Q)=>(()=>{if(p.storage===p.value)return`${e}[${F}]=${Q};`;if(p.storage==="vec2"&&p.value==="i32")return`${e}[${F}]=vec2(u32(${Q}), select(0u, 0xFFFFFFFFu, ${Q} < 0));`;if(p.storage==="vec2"&&p.value==="u32")return`${e}[${F}]=vec2(u32(${Q}), 0u);`;if(p.storage==="u32"&&p.value==="vec4")return`${e}[${F}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${Q}));`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),W=F=>(()=>{if(p.storage===p.value)return`${e}[${F}]`;if(p.storage==="vec2"&&p.value==="i32")return`i32(${e}[${F}].x)`;if(p.storage==="vec2"&&p.value==="u32")return`u32(${e}[${F}].x)`;if(p.storage==="u32"&&p.value==="vec4")return`vec4(bool(${e}[${F}] & 0xFFu), bool(${e}[${F}] & 0xFF00u), bool(${e}[${F}] & 0xFF0000u), bool(${e}[${F}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),_=d<2?"":` + fn get_${e}ByIndices(indices: ${p.indices}) -> ${o} { + return ${W(`i2o_${e}(indices)`)}; + }`,D=d<2?"":(()=>{let F=c.map(Y=>`d${Y}: u32`).join(", "),Q=c.map(Y=>`d${Y}`).join(", ");return` + fn get_${e}(${F}) -> ${o} { + return get_${e}ByIndices(${I(Q)}); + }`})(),V=(...F)=>{if(F.length!==d)throw new Error(`indices length must be ${d}`);let Q=F.map(h).join(",");return d===0?W("0u"):d===1?W(Q[0]):(m.get=!0,m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}(${Q})`)},q=F=>d<2?W(F):(m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}ByIndices(${F})`),H=d<2?"":` + fn set_${e}ByIndices(indices: ${p.indices}, value: ${o}) { + ${C(`i2o_${e}(indices)`,"value")} + }`,K=d<2?"":(()=>{let F=c.map(Y=>`d${Y}: u32`).join(", "),Q=c.map(Y=>`d${Y}`).join(", ");return` + fn set_${e}(${F}, value: ${o}) { + set_${e}ByIndices(${I(Q)}, value); + }`})();return{impl:()=>{let F=[],Q=!1;return m.offsetToIndices&&(F.push(T),Q=!0),m.indicesToOffset&&(F.push(A),Q=!0),m.broadcastedIndicesToOffset&&(Object.values(X).forEach(Y=>F.push(Y)),Q=!0),m.set&&(F.push(K),Q=!0),m.setByIndices&&(F.push(H),Q=!0),m.get&&(F.push(D),Q=!0),m.getByIndices&&(F.push(_),Q=!0),!l&&Q&&F.unshift(`const ${v} = ${p.indices}(${n.join(",")});`,`const ${w} = ${p.indices}(${B.computeStrides(n).join(",")});`),F.join(` +`)},type:p,offsetToIndices:S,indicesToOffset:k,broadcastedIndicesToOffset:G,indices:I,indicesGet:R,indicesSet:M,set:(...F)=>{if(F.length!==d+1)throw new Error(`indices length must be ${d}`);let Q=F[d];if(typeof Q!="string")throw new Error("value must be string");let Y=F.slice(0,d).map(h).join(",");return d===0?C("0u",Q):d===1?C(Y[0],Q):(m.set=!0,m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}(${Y}, ${Q})`)},setByOffset:C,setByIndices:(F,Q)=>d<2?C(F,Q):(m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}ByIndices(${F}, ${Q});`),get:V,getByOffset:W,getByIndices:q,usage:i,name:e,strides:w,shape:v,rank:d}},L=(e,t,n,i=1)=>di(e,t,n,"input",i),ne=(e,t,n,i=1)=>di(e,t,n,"output",i),tx=(e,t,n)=>di(e,t,n,"atomicOutput",1),Wl=(e,t,n,i=1)=>di(e,t,n,"internal",i),Om=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=Nn){let t=typeof e=="number"?e:e[0],n=typeof e=="number"?1:e[1],i=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||i>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${n}, ${i}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*n*i>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${n}, ${i}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let u=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,l=u?`@builtin(global_invocation_id) global_id : vec3, @builtin(workgroup_id) workgroup_id : vec3, @builtin(local_invocation_index) local_idx : u32, @builtin(local_invocation_id) local_id : vec3`:`@builtin(global_invocation_id) global_id : vec3, @@ -1901,31 +1901,31 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Gf.onnx.ModelProto.decode(e @builtin(num_workgroups) num_workgroups : vec3`,d=u?`let global_idx = global_id.x; let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + workgroup_id.y * num_workgroups[0] + workgroup_id.x; - let global_idx = workgroup_index * ${t*n*o}u + local_idx;`;return`@compute @workgroup_size(${t}, ${n}, ${o}) + let global_idx = workgroup_index * ${t*n*i}u + local_idx;`;return`@compute @workgroup_size(${t}, ${n}, ${i}) fn main(${l}) { ${d} - `}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let n=e.usage==="input"?"read":"read_write",o=e.usage==="atomicOutput"?"atomic":e.type.storage;return`@group(0) @binding(${t}) var ${e.name}: array<${o}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(` -`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,n=1){return this.uniforms.push({name:e,type:t,length:n}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:n,length:o}of this.uniforms)if(o&&o>4)n==="f16"?e.push(`@align(16) ${t}:array, ${Math.ceil(o/8)}>`):e.push(`${t}:array, ${Math.ceil(o/4)}>`);else{let u=o==null||o===1?n:`vec${o}<${n}>`;e.push(`${t}:${u}`)}return` + `}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let n=e.usage==="input"?"read":"read_write",i=e.usage==="atomicOutput"?"atomic":e.type.storage;return`@group(0) @binding(${t}) var ${e.name}: array<${i}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(` +`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,n=1){return this.uniforms.push({name:e,type:t,length:n}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:n,length:i}of this.uniforms)if(i&&i>4)n==="f16"?e.push(`@align(16) ${t}:array, ${Math.ceil(i/8)}>`):e.push(`${t}:array, ${Math.ceil(i/4)}>`);else{let u=i==null||i===1?n:`vec${i}<${n}>`;e.push(`${t}:${u}`)}return` struct Uniforms { ${e.join(", ")} }; @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(e=>e.impl()).join(` `)+this.internalVariables.map(e=>e.impl()).join(` -`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},Xv=(e,t)=>new _m(e,t)}),vm,Ra,xm,$m,Tm,Sm,bt,Zv,Jv,$r=R(()=>{"use strict";se(),ae(),Ve(),ue(),vm=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},Ra=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),xm=(e,t)=>z.sortBasedOnPerm(e,Ra(e.length,t)),$m=(e,t,n,o)=>{let u=`fn perm(i: ${o.type.indices}) -> ${n.type.indices} { - var a: ${n.type.indices};`;for(let l=0;l{let n=[],o=[];for(let u=0;u{let n=0;for(let o=0;o{let n=e.dataType,o=e.dims.length,u=Ra(o,t),l=xm(e.dims,u),d=e.dims,c=l,s=o<2||Sm(u,e.dims),r;if(s)return r=m=>{let b=j("input",n,d,4),x=re("output",n,c,4);return` - ${m.registerUniform("output_size","u32").declareVariables(b,x)} +`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},rx=(e,t)=>new Om(e,t)}),Em,Ba,Am,km,Pm,Cm,xt,nx,ix,Ar=N(()=>{"use strict";le(),pe(),He(),he(),Em=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},Ba=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),Am=(e,t)=>B.sortBasedOnPerm(e,Ba(e.length,t)),km=(e,t,n,i)=>{let u=`fn perm(i: ${i.type.indices}) -> ${n.type.indices} { + var a: ${n.type.indices};`;for(let l=0;l{let n=[],i=[];for(let u=0;u{let n=0;for(let i=0;i{let n=e.dataType,i=e.dims.length,u=Ba(i,t),l=Am(e.dims,u),d=e.dims,c=l,s=i<2||Cm(u,e.dims),r;if(s)return r=m=>{let g=L("input",n,d,4),v=ne("output",n,c,4);return` + ${m.registerUniform("output_size","u32").declareVariables(g,v)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} output[global_idx] = input[global_idx]; - }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let m=z.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64/4)},programUniforms:[{type:12,data:Math.ceil(m/4)}]}},getShaderSource:r};let{newShape:i,newPerm:a}=Tm(e.dims,u),p=z.areEqual(a,[2,3,1]),h=z.areEqual(a,[3,1,2]);if(i.length===2||p||h){d=p?[i[0],i[1]*i[2]]:h?[i[0]*i[1],i[2]]:i,c=[d[1],d[0]];let m=16;return r=b=>{let x=j("a",n,d.length),w=re("output",n,c.length);return` - ${b.registerUniform("output_size","u32").declareVariables(x,w)} + }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let m=B.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64/4)},programUniforms:[{type:12,data:Math.ceil(m/4)}]}},getShaderSource:r};let{newShape:o,newPerm:a}=Pm(e.dims,u),p=B.areEqual(a,[2,3,1]),h=B.areEqual(a,[3,1,2]);if(o.length===2||p||h){d=p?[o[0],o[1]*o[2]]:h?[o[0]*o[1],o[2]]:o,c=[d[1],d[0]];let m=16;return r=g=>{let v=L("a",n,d.length),w=ne("output",n,c.length);return` + ${g.registerUniform("output_size","u32").declareVariables(v,w)} var tile : array, ${m}>; - ${b.mainStart([m,m,1])} + ${g.mainStart([m,m,1])} let stride = (uniforms.output_shape[1] - 1) / ${m} + 1; let workgroup_id_x = workgroup_index % stride; let workgroup_id_y = workgroup_index / stride; let input_col = workgroup_id_y * ${m}u + local_id.x; let input_row = workgroup_id_x * ${m}u + local_id.y; if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { - tile[local_id.y][local_id.x] = ${x.getByIndices(`${x.type.indices}(input_row, input_col)`)}; + tile[local_id.y][local_id.x] = ${v.getByIndices(`${v.type.indices}(input_row, input_col)`)}; } workgroupBarrier(); @@ -1934,36 +1934,36 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Gf.onnx.ModelProto.decode(e if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { ${w.setByIndices(`${w.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} } - }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let b=z.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(c[1]/m),y:Math.ceil(c[0]/m)},programUniforms:[{type:12,data:b},...oe(d,c)]}},getShaderSource:r}}return r=m=>{let b=j("a",n,d.length),x=re("output",n,c.length);return` - ${m.registerUniform("output_size","u32").declareVariables(b,x)} + }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let g=B.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(c[1]/m),y:Math.ceil(c[0]/m)},programUniforms:[{type:12,data:g},...ae(d,c)]}},getShaderSource:r}}return r=m=>{let g=L("a",n,d.length),v=ne("output",n,c.length);return` + ${m.registerUniform("output_size","u32").declareVariables(g,v)} - ${$m(u,o,b,x)} + ${km(u,i,g,v)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let indices = ${x.offsetToIndices("global_idx")}; + let indices = ${v.offsetToIndices("global_idx")}; let aIndices = perm(indices); - ${x.setByOffset("global_idx",b.getByIndices("aIndices"))} - }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let m=z.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...oe(d,c)]}},getShaderSource:r}},Zv=(e,t)=>{vm(e.inputs,t.perm),e.compute(bt(e.inputs[0],t.perm))},Jv=e=>Se({perm:e.perm})}),Im,Om,Em,Am,km,Pm,Cm,Dm,Rm,Nm,At,Yv,Qv,ex,tx,rx,nx,ix,ox,sx,ax,TA=R(()=>{"use strict";se(),ae(),ue(),Gl(),$r(),Im={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},Om={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},Em={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Am={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},km=(e,t)=>{let n=[];for(let o=t-e;o{let n=[],o=e.length;for(let l=0;le[l]);return[n,u]},Cm=(e,t)=>{let n=e.length+t.length,o=[],u=0;for(let l=0;l{for(let n=0;n{let n=[];if(!Dm(e,t)){for(let o=0;on.push(o))}return n},Nm=(e,t,n,o,u,l,d)=>{let c=n[0].dims,s=z.size(l),r=z.size(d),i=j("_A",n[0].dataType,c),a=re("output",u,l),p=64;s===1&&(p=256);let h=` + ${v.setByOffset("global_idx",g.getByIndices("aIndices"))} + }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let m=B.size(l);return{outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...ae(d,c)]}},getShaderSource:r}},nx=(e,t)=>{Em(e.inputs,t.perm),e.compute(xt(e.inputs[0],t.perm))},ix=e=>ke({perm:e.perm})}),Dm,Rm,Nm,zm,Mm,Bm,Fm,jm,Lm,Um,zt,ox,sx,ax,ux,lx,dx,cx,px,hx,fx,zA=N(()=>{"use strict";le(),pe(),he(),Kl(),Ar(),Dm={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},Rm={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},Nm={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},zm={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Mm=(e,t)=>{let n=[];for(let i=t-e;i{let n=[],i=e.length;for(let l=0;le[l]);return[n,u]},Fm=(e,t)=>{let n=e.length+t.length,i=[],u=0;for(let l=0;l{for(let n=0;n{let n=[];if(!jm(e,t)){for(let i=0;in.push(i))}return n},Um=(e,t,n,i,u,l,d)=>{let c=n[0].dims,s=B.size(l),r=B.size(d),o=L("_A",n[0].dataType,c),a=ne("output",u,l),p=64;s===1&&(p=256);let h=` var aBestValues : array; - `,m=b=>` - ${b.registerUniform("reduceSize","u32").declareVariables(i,a)} + `,m=g=>` + ${g.registerUniform("reduceSize","u32").declareVariables(o,a)} ${h} fn DIV_CEIL(a : u32, b : u32) -> u32 { return ((a - 1u) / b + 1u); } - ${b.mainStart(p)} + ${g.mainStart(p)} let outputIndex = global_idx / ${p}; let offset = outputIndex * uniforms.reduceSize; - var bestValue = f32(${Em[o]}); + var bestValue = f32(${Nm[i]}); let Length = uniforms.reduceSize; for (var k = local_idx; k < Length; k = k + ${p}) { - let candidate = f32(${i.getByOffset("offset + k")}); - bestValue = ${Im[o]}; + let candidate = f32(${o.getByOffset("offset + k")}); + bestValue = ${Dm[i]}; } aBestValues[local_idx] = bestValue; workgroupBarrier(); @@ -1974,7 +1974,7 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Gf.onnx.ModelProto.decode(e let interval = DIV_CEIL(reduceSize, 2u); if (local_idx < currentSize) { let candidate = aBestValues[local_idx + interval]; - bestValue = ${Om[o]}; + bestValue = ${Rm[i]}; aBestValues[local_idx] = bestValue; } reduceSize = interval; @@ -1982,42 +1982,42 @@ as ORT format: ${u}`)}}loadFromOnnxFormat(e,t){let n=Gf.onnx.ModelProto.decode(e } if (local_idx == 0u) { - ${a.setByOffset("outputIndex",`${o==="mean"?`${a.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${a.type.storage}(${Am[o]})`}`)}; + ${a.setByOffset("outputIndex",`${i==="mean"?`${a.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${a.type.storage}(${zm[i]})`}`)}; } - }`;return{name:e,shaderCache:{hint:`${t};${p}`,inputDependencies:["type"]},getShaderSource:m,getRunData:()=>({outputs:[{dims:l,dataType:u}],dispatchGroup:{x:s},programUniforms:[{type:12,data:r}]})}},At=(e,t,n,o)=>{let u=e.inputs.length===1?n:cl(e.inputs,n),l=u.axes;l.length===0&&!u.noopWithEmptyAxes&&(l=e.inputs[0].dims.map((h,m)=>m));let d=z.normalizeAxes(l,e.inputs[0].dims.length),c=d,s=e.inputs[0],r=Rm(c,e.inputs[0].dims.length);r.length>0&&(s=e.compute(bt(e.inputs[0],r),{inputs:[0],outputs:[-1]})[0],c=km(c.length,s.dims.length));let[i,a]=Pm(s.dims,c),p=i;u.keepDims&&(p=Cm(i,d)),e.compute(Nm(t,u.cacheKey,[s],o,e.inputs[0].dataType,p,a),{inputs:[s]})},Yv=(e,t)=>{At(e,"ReduceMeanShared",t,"mean")},Qv=(e,t)=>{At(e,"ReduceL1Shared",t,"l1")},ex=(e,t)=>{At(e,"ReduceL2Shared",t,"l2")},tx=(e,t)=>{At(e,"ReduceLogSumExpShared",t,"logSumExp")},rx=(e,t)=>{At(e,"ReduceMaxShared",t,"max")},nx=(e,t)=>{At(e,"ReduceMinShared",t,"min")},ix=(e,t)=>{At(e,"ReduceProdShared",t,"prod")},ox=(e,t)=>{At(e,"ReduceSumShared",t,"sum")},sx=(e,t)=>{At(e,"ReduceSumSquareShared",t,"sumSquare")},ax=(e,t)=>{At(e,"ReduceLogSumShared",t,"logSum")}}),kt,zm,Co,cl,Pt,Bm,Mm,Fm,jm,Lm,Um,Vm,Gm,qm,Hm,Ct,ux,lx,dx,cx,px,hx,fx,mx,gx,bx,Gl=R(()=>{"use strict";se(),ae(),Ve(),ue(),TA(),kt=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},zm=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Co=(e,t,n,o,u,l,d=!1,c=!1)=>{let s=[],r=n[0].dims,i=r.length,a=z.normalizeAxes(u,i),p=!c&&a.length===0;r.forEach((b,x)=>{p||a.indexOf(x)>=0?d&&s.push(1):s.push(b)});let h=s.length,m=z.size(s);return{name:e,shaderCache:t,getShaderSource:b=>{let x=[],w=j("_A",n[0].dataType,i),y=re("output",l,h),S=o(w,y,a),T=S[2];for(let I=0,A=0;I=0?(d&&A++,T=`for(var j${I}: u32 = 0; j${I} < ${r[I]}; j${I}++) { - ${S[2].includes("last_index")?`let last_index = j${I};`:""} - ${w.indicesSet("input_indices",I,`j${I}`)} - ${T} - }`):(x.push(`${w.indicesSet("input_indices",I,y.indicesGet("output_indices",A))};`),A++);return` + }`;return{name:e,shaderCache:{hint:`${t};${p}`,inputDependencies:["type"]},getShaderSource:m,getRunData:()=>({outputs:[{dims:l,dataType:u}],dispatchGroup:{x:s},programUniforms:[{type:12,data:r}]})}},zt=(e,t,n,i)=>{let u=e.inputs.length===1?n:ml(e.inputs,n),l=u.axes;l.length===0&&!u.noopWithEmptyAxes&&(l=e.inputs[0].dims.map((h,m)=>m));let d=B.normalizeAxes(l,e.inputs[0].dims.length),c=d,s=e.inputs[0],r=Lm(c,e.inputs[0].dims.length);r.length>0&&(s=e.compute(xt(e.inputs[0],r),{inputs:[0],outputs:[-1]})[0],c=Mm(c.length,s.dims.length));let[o,a]=Bm(s.dims,c),p=o;u.keepDims&&(p=Fm(o,d)),e.compute(Um(t,u.cacheKey,[s],i,e.inputs[0].dataType,p,a),{inputs:[s]})},ox=(e,t)=>{zt(e,"ReduceMeanShared",t,"mean")},sx=(e,t)=>{zt(e,"ReduceL1Shared",t,"l1")},ax=(e,t)=>{zt(e,"ReduceL2Shared",t,"l2")},ux=(e,t)=>{zt(e,"ReduceLogSumExpShared",t,"logSumExp")},lx=(e,t)=>{zt(e,"ReduceMaxShared",t,"max")},dx=(e,t)=>{zt(e,"ReduceMinShared",t,"min")},cx=(e,t)=>{zt(e,"ReduceProdShared",t,"prod")},px=(e,t)=>{zt(e,"ReduceSumShared",t,"sum")},hx=(e,t)=>{zt(e,"ReduceSumSquareShared",t,"sumSquare")},fx=(e,t)=>{zt(e,"ReduceLogSumShared",t,"logSum")}}),Mt,Vm,Fo,ml,Bt,Gm,qm,Hm,Wm,Km,Xm,Zm,Ym,Jm,Qm,Ft,mx,gx,bx,yx,wx,_x,vx,xx,$x,Tx,Kl=N(()=>{"use strict";le(),pe(),He(),he(),zA(),Mt=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Vm=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Fo=(e,t,n,i,u,l,d=!1,c=!1)=>{let s=[],r=n[0].dims,o=r.length,a=B.normalizeAxes(u,o),p=!c&&a.length===0;r.forEach((g,v)=>{p||a.indexOf(v)>=0?d&&s.push(1):s.push(g)});let h=s.length,m=B.size(s);return{name:e,shaderCache:t,getShaderSource:g=>{let v=[],w=L("_A",n[0].dataType,o),y=ne("output",l,h),T=i(w,y,a),S=T[2];for(let O=0,A=0;O=0?(d&&A++,S=`for(var j${O}: u32 = 0; j${O} < ${r[O]}; j${O}++) { + ${T[2].includes("last_index")?`let last_index = j${O};`:""} + ${w.indicesSet("input_indices",O,`j${O}`)} + ${S} + }`):(v.push(`${w.indicesSet("input_indices",O,y.indicesGet("output_indices",A))};`),A++);return` - ${b.registerUniform("output_size","u32").declareVariables(w,y)} + ${g.registerUniform("output_size","u32").declareVariables(w,y)} - ${b.mainStart()} - ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} var input_indices: ${w.type.indices}; let output_indices = ${y.offsetToIndices("global_idx")}; - ${x.join(` + ${v.join(` `)} - ${S[0]} // init ops for reduce max/min - ${S[1]} - ${T} - ${S[3]} - ${S.length===4?y.setByOffset("global_idx","value"):S.slice(4).join(` + ${T[0]} // init ops for reduce max/min + ${T[1]} + ${S} + ${T[3]} + ${T.length===4?y.setByOffset("global_idx","value"):T.slice(4).join(` `)} - }`},getRunData:()=>({outputs:[{dims:s,dataType:l}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...oe(r,s)]})}},cl=(e,t)=>{let n=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(o=>n.push(Number(o))),Se({axes:n,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},Pt=(e,t,n,o)=>{let u=e.inputs,l=u.length===1?n:cl(u,n);e.compute(Co(t,{hint:l.cacheKey,inputDependencies:["rank"]},[u[0]],l.noopWithEmptyAxes&&l.axes.length===0?zm:o,l.axes,u[0].dataType,l.keepDims,l.noopWithEmptyAxes),{inputs:[0]})},Bm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceLogSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},Mm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceL1",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},Fm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceL2",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},jm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceLogSumExp",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},Lm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceMax",t,(n,o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(n.indicesSet("input_indices",d,0));return[`${l.join(` -`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},Um=(e,t)=>{kt(e.inputs),Pt(e,"ReduceMean",t,(n,o,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=e.inputs[0].dims[d]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${l});`]})},Vm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceMin",t,(n,o,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`input_indices[${d}] = 0;`);return[`${l.join(` -`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},Gm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceProd",t,(n,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},qm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Hm=(e,t)=>{kt(e.inputs),Pt(e,"ReduceSumSquare",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},Ct=(e,t,n)=>{if(t.length===0)return n;let o=1,u=1;for(let l=0;l1024},ux=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Um(e,t):Yv(e,t)},lx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Mm(e,t):Qv(e,t)},dx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Fm(e,t):ex(e,t)},cx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?jm(e,t):tx(e,t)},px=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Lm(e,t):rx(e,t)},hx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Vm(e,t):nx(e,t)},fx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Gm(e,t):ix(e,t)},mx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?qm(e,t):ox(e,t)},gx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Hm(e,t):sx(e,t)},bx=(e,t)=>{Ct(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Bm(e,t):ax(e,t)}}),Na,yx,wx,pl,SA=R(()=>{"use strict";se(),Ve(),Gl(),Na=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},yx=(e,t)=>{Na(e.inputs);let n=(o,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` -`)}`,`var value = ${o.getByIndices("input_indices")}; -var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { - value = ${o.getByIndices("input_indices")}; + }`},getRunData:()=>({outputs:[{dims:s,dataType:l}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...ae(r,s)]})}},ml=(e,t)=>{let n=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(i=>n.push(Number(i))),ke({axes:n,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},Bt=(e,t,n,i)=>{let u=e.inputs,l=u.length===1?n:ml(u,n);e.compute(Fo(t,{hint:l.cacheKey,inputDependencies:["rank"]},[u[0]],l.noopWithEmptyAxes&&l.axes.length===0?Vm:i,l.axes,u[0].dataType,l.keepDims,l.noopWithEmptyAxes),{inputs:[0]})},Gm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceLogSum",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},qm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceL1",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},Hm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceL2",t,(n,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Wm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceLogSumExp",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},Km=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceMax",t,(n,i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(n.indicesSet("input_indices",d,0));return[`${l.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},Xm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceMean",t,(n,i,u)=>{let l=1;for(let d=0;d=0||u.length===0)&&(l*=e.inputs[0].dims[d]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${i.type.value}(sum / ${l});`]})},Zm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceMin",t,(n,i,u)=>{let l=[];for(let d=0;d=0||u.length===0)&&l.push(`input_indices[${d}] = 0;`);return[`${l.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},Ym=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceProd",t,(n,i)=>[`var value = ${i.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},Jm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceSum",t,(n,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Qm=(e,t)=>{Mt(e.inputs),Bt(e,"ReduceSumSquare",t,(n,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},Ft=(e,t,n)=>{if(t.length===0)return n;let i=1,u=1;for(let l=0;l1024},mx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Xm(e,t):ox(e,t)},gx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?qm(e,t):sx(e,t)},bx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Hm(e,t):ax(e,t)},yx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Wm(e,t):ux(e,t)},wx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Km(e,t):lx(e,t)},_x=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Zm(e,t):dx(e,t)},vx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Ym(e,t):cx(e,t)},xx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Jm(e,t):px(e,t)},$x=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Qm(e,t):hx(e,t)},Tx=(e,t)=>{Ft(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Gm(e,t):fx(e,t)}}),Fa,Sx,Ix,gl,MA=N(()=>{"use strict";le(),He(),Kl(),Fa=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},Sx=(e,t)=>{Fa(e.inputs);let n=(i,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` +`)}`,`var value = ${i.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { + value = ${i.getByIndices("input_indices")}; best_index = i32(last_index); - }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Co("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},wx=(e,t)=>{Na(e.inputs);let n=(o,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` -`)}`,`var value = ${o.getByIndices("input_indices")}; -var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { - value = ${o.getByIndices("input_indices")}; + }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Fo("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},Ix=(e,t)=>{Fa(e.inputs);let n=(i,u,l)=>{let d=[];for(let c=0;c=0||l.length===0)&&d.push(`input_indices[${c}] = 0;`);return[`${d.join(` +`)}`,`var value = ${i.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { + value = ${i.getByIndices("input_indices")}; best_index = i32(last_index); - }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Co("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},pl=e=>Se(e)}),Wm,po,Km,Xm,Zm,Oi,Jm,_x,ql=R(()=>{"use strict";se(),ae(),Ul(),ue(),Wm=(e,t)=>{let n=e[0],o=e[1],u=e[2],l=e[3],d=e[4],c=e[5];if(d&&c)throw new Error("Attention cannot have both past and attention_bias");if(n.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let s=n.dims[0],r=n.dims[1],i=n.dims[2];if(u.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(o.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(o.dims[0]!==i)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(u.dims[0]!==o.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let a=u.dims[0]/3,p=a,h=p;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let S of t.qkvHiddenSizes)if(S%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");a=t.qkvHiddenSizes[0],p=t.qkvHiddenSizes[1],h=t.qkvHiddenSizes[2]}let m=r;if(a!==p)throw new Error("qkv_hidden_sizes first element should be same as the second");if(u.dims[0]!==a+p+h)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let b=0;if(d){if(p!==h)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(d.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(d.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(d.dims[1]!==s)throw new Error('Input "past" second dimension must be batch_size');if(d.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(d.dims[4]!==p/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(b=d.dims[3])}let x=m+b,w=-1,y=0;if(l)throw new Error("Mask not supported");if(d)throw new Error("past is not supported");if(c){if(c.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(c.dims[0]!==s||c.dims[1]!==t.numHeads||c.dims[2]!==r||c.dims[3]!==x)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:s,sequenceLength:r,pastSequenceLength:b,kvSequenceLength:m,totalSequenceLength:x,maxSequenceLength:w,inputHiddenSize:i,hiddenSize:a,vHiddenSize:h,headSize:Math.floor(a/t.numHeads),vHeadSize:Math.floor(h/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:y,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},po=(e,t,n)=>t&&e?` + }`,"",u.setByOffset("global_idx","best_index")]};e.compute(Fo("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},gl=e=>ke(e)}),eg,wo,tg,rg,ng,Ri,ig,Ox,Xl=N(()=>{"use strict";le(),pe(),Hl(),he(),eg=(e,t)=>{let n=e[0],i=e[1],u=e[2],l=e[3],d=e[4],c=e[5];if(d&&c)throw new Error("Attention cannot have both past and attention_bias");if(n.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let s=n.dims[0],r=n.dims[1],o=n.dims[2];if(u.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(i.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(i.dims[0]!==o)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(u.dims[0]!==i.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let a=u.dims[0]/3,p=a,h=p;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let T of t.qkvHiddenSizes)if(T%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");a=t.qkvHiddenSizes[0],p=t.qkvHiddenSizes[1],h=t.qkvHiddenSizes[2]}let m=r;if(a!==p)throw new Error("qkv_hidden_sizes first element should be same as the second");if(u.dims[0]!==a+p+h)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let g=0;if(d){if(p!==h)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(d.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(d.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(d.dims[1]!==s)throw new Error('Input "past" second dimension must be batch_size');if(d.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(d.dims[4]!==p/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(g=d.dims[3])}let v=m+g,w=-1,y=0;if(l)throw new Error("Mask not supported");if(d)throw new Error("past is not supported");if(c){if(c.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(c.dims[0]!==s||c.dims[1]!==t.numHeads||c.dims[2]!==r||c.dims[3]!==v)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:s,sequenceLength:r,pastSequenceLength:g,kvSequenceLength:m,totalSequenceLength:v,maxSequenceLength:w,inputHiddenSize:o,hiddenSize:a,vHiddenSize:h,headSize:Math.floor(a/t.numHeads),vHeadSize:Math.floor(h/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:y,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},wo=(e,t,n)=>t&&e?` let total_sequence_length_input = u32(${t.getByOffset("0")}); let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length); let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input; @@ -2030,16 +2030,16 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas `:` ${n?"let past_sequence_length = uniforms.past_sequence_length":""}; let present_sequence_length = total_sequence_length; - `,Km=(e,t,n,o,u,l,d,c)=>{let s=Ue(d?1:l),r=64,i=l/s;i{let y=re("x",e.dataType,e.dims,s),S=[y],T=d?j("seq_lens",d.dataType,d.dims):void 0;T&&S.push(T);let I=c?j("total_sequence_length_input",c.dataType,c.dims):void 0;I&&S.push(I);let A=Je(e.dataType),P=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` + `,tg=(e,t,n,i,u,l,d,c)=>{let s=qe(d?1:l),r=64,o=l/s;o{let y=ne("x",e.dataType,e.dims,s),T=[y],S=d?L("seq_lens",d.dataType,d.dims):void 0;S&&T.push(S);let O=c?L("total_sequence_length_input",c.dataType,c.dims):void 0;O&&T.push(O);let A=tt(e.dataType),k=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` var thread_max: array; var thread_sum: array; - ${w.registerUniforms(P).declareVariables(...S)} + ${w.registerUniforms(k).declareVariables(...T)} ${w.mainStart([r,1,1])} let batchIdx = workgroup_id.z / uniforms.num_heads; let headIdx = workgroup_id.z % uniforms.num_heads; let sequence_length = uniforms.sequence_length; var total_sequence_length = uniforms.total_sequence_length; - ${po(T,I,!1)} + ${wo(S,O,!1)} let local_offset = local_idx * uniforms.elements_per_thread; let offset = (global_idx / ${r}) * uniforms.total_sequence_length + local_offset; let seq_causal_length = ${d?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"}; @@ -2081,13 +2081,13 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) { x[offset + total_seq_id] = ${y.type.value}(${A}(0)); }`:""}; - }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${r};${h};${s}`,inputDependencies:b},getShaderSource:x,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:u,z:t*n},programUniforms:p})}},Xm=(e,t,n,o,u,l,d,c,s)=>{let r=d+l.kvSequenceLength,i=[l.batchSize,l.numHeads,l.sequenceLength,r],a=e>1&&o,p=l.kvNumHeads?l.kvNumHeads:l.numHeads,h=a?[l.batchSize,p,r,l.headSize]:void 0,m=l.nReps?l.nReps:1,b=l.scale===0?1/Math.sqrt(l.headSize):l.scale,x=Ue(l.headSize),w=l.headSize/x,y=12,S={x:Math.ceil(r/y),y:Math.ceil(l.sequenceLength/y),z:l.batchSize*l.numHeads},T=[{type:12,data:l.sequenceLength},{type:12,data:w},{type:12,data:r},{type:12,data:l.numHeads},{type:12,data:l.headSize},{type:1,data:b},{type:12,data:d},{type:12,data:l.kvSequenceLength},{type:12,data:m}],I=a&&o&&z.size(o.dims)>0,A=["type","type"];I&&A.push("type"),u&&A.push("type"),c&&A.push("type"),s&&A.push("type");let P=[{dims:i,dataType:t.dataType,gpuDataType:0}];a&&P.push({dims:h,dataType:t.dataType,gpuDataType:0});let O=B=>{let U=j("q",t.dataType,t.dims,x),X=j("key",n.dataType,n.dims,x),q=[U,X];if(I){let G=j("past_key",o.dataType,o.dims,x);q.push(G)}u&&q.push(j("attention_bias",u.dataType,u.dims));let D=c?j("seq_lens",c.dataType,c.dims):void 0;D&&q.push(D);let Q=s?j("total_sequence_length_input",s.dataType,s.dims):void 0;Q&&q.push(Q);let _=re("output",t.dataType,i),C=[_];a&&C.push(re("present_key",t.dataType,h,x));let F=Je(1,x),V=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${r};${h};${s}`,inputDependencies:g},getShaderSource:v,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:u,z:t*n},programUniforms:p})}},rg=(e,t,n,i,u,l,d,c,s)=>{let r=d+l.kvSequenceLength,o=[l.batchSize,l.numHeads,l.sequenceLength,r],a=e>1&&i,p=l.kvNumHeads?l.kvNumHeads:l.numHeads,h=a?[l.batchSize,p,r,l.headSize]:void 0,m=l.nReps?l.nReps:1,g=l.scale===0?1/Math.sqrt(l.headSize):l.scale,v=qe(l.headSize),w=l.headSize/v,y=12,T={x:Math.ceil(r/y),y:Math.ceil(l.sequenceLength/y),z:l.batchSize*l.numHeads},S=[{type:12,data:l.sequenceLength},{type:12,data:w},{type:12,data:r},{type:12,data:l.numHeads},{type:12,data:l.headSize},{type:1,data:g},{type:12,data:d},{type:12,data:l.kvSequenceLength},{type:12,data:m}],O=a&&i&&B.size(i.dims)>0,A=["type","type"];O&&A.push("type"),u&&A.push("type"),c&&A.push("type"),s&&A.push("type");let k=[{dims:o,dataType:t.dataType,gpuDataType:0}];a&&k.push({dims:h,dataType:t.dataType,gpuDataType:0});let I=R=>{let M=L("q",t.dataType,t.dims,v),X=L("key",n.dataType,n.dims,v),G=[M,X];if(O){let H=L("past_key",i.dataType,i.dims,v);G.push(H)}u&&G.push(L("attention_bias",u.dataType,u.dims));let C=c?L("seq_lens",c.dataType,c.dims):void 0;C&&G.push(C);let W=s?L("total_sequence_length_input",s.dataType,s.dims):void 0;W&&G.push(W);let _=ne("output",t.dataType,o),D=[_];a&&D.push(ne("present_key",t.dataType,h,v));let V=tt(1,v),q=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` const TILE_SIZE = ${y}u; - var tileQ: array<${U.type.storage}, ${y*y}>; - var tileK: array<${U.type.storage}, ${y*y}>; - ${B.registerUniforms(V).declareVariables(...q,...C)} - ${B.mainStart([y,y,1])} + var tileQ: array<${M.type.storage}, ${y*y}>; + var tileK: array<${M.type.storage}, ${y*y}>; + ${R.registerUniforms(q).declareVariables(...G,...D)} + ${R.mainStart([y,y,1])} // x holds the N and y holds the M let headIdx = workgroup_id.z % uniforms.num_heads; let kvHeadIdx = ${m===1?"headIdx":"headIdx / uniforms.n_reps"}; @@ -2097,20 +2097,20 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas let n = workgroup_id.x * TILE_SIZE; let sequence_length = uniforms.M; var total_sequence_length = uniforms.N; - ${po(D,Q,!0)} + ${wo(C,W,!0)} let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; - ${I&&a?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""}; + ${O&&a?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""}; let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K; ${a?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""} - var value = ${F}(0); + var value = ${V}(0); for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) { tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x]; } if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) { var idx = TILE_SIZE * local_id.y + local_id.x; - ${I&&a?` + ${O&&a?` if (n + local_id.y < past_sequence_length) { tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { @@ -2126,7 +2126,7 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas workgroupBarrier(); for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) { - value += ${F}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); + value += ${V}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); } workgroupBarrier(); @@ -2135,15 +2135,15 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas if (global_id.y < uniforms.M && global_id.x < total_sequence_length) { let headOffset = workgroup_id.z * uniforms.M * uniforms.N; let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x; - var sum: f32 = ${(()=>{switch(x){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${x}`)}})()}; + var sum: f32 = ${(()=>{switch(v){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${v}`)}})()}; output[outputIdx] = ${_.type.value} (sum * uniforms.alpha) + ${u?"attention_bias[outputIdx]":"0.0"}; } - }`};return{name:"AttentionProbs",shaderCache:{hint:`${x};${u!==void 0};${o!==void 0};${e}`,inputDependencies:A},getRunData:()=>({outputs:P,dispatchGroup:S,programUniforms:T}),getShaderSource:O}},Zm=(e,t,n,o,u,l,d=void 0,c=void 0)=>{let s=l+u.kvSequenceLength,r=u.nReps?u.nReps:1,i=u.vHiddenSize*r,a=e>1&&o,p=u.kvNumHeads?u.kvNumHeads:u.numHeads,h=a?[u.batchSize,p,s,u.headSize]:void 0,m=[u.batchSize,u.sequenceLength,i],b=12,x={x:Math.ceil(u.vHeadSize/b),y:Math.ceil(u.sequenceLength/b),z:u.batchSize*u.numHeads},w=[{type:12,data:u.sequenceLength},{type:12,data:s},{type:12,data:u.vHeadSize},{type:12,data:u.numHeads},{type:12,data:u.headSize},{type:12,data:i},{type:12,data:l},{type:12,data:u.kvSequenceLength},{type:12,data:r}],y=a&&o&&z.size(o.dims)>0,S=["type","type"];y&&S.push("type"),d&&S.push("type"),c&&S.push("type");let T=[{dims:m,dataType:t.dataType,gpuDataType:0}];a&&T.push({dims:h,dataType:t.dataType,gpuDataType:0});let I=A=>{let P=j("probs",t.dataType,t.dims),O=j("v",n.dataType,n.dims),B=[P,O];y&&B.push(j("past_value",o.dataType,o.dims));let U=d?j("seq_lens",d.dataType,d.dims):void 0;d&&B.push(U);let X=c?j("total_sequence_length_input",c.dataType,c.dims):void 0;c&&B.push(X);let q=[re("output",t.dataType,m)];a&&q.push(re("present_value",t.dataType,h));let D=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` - const TILE_SIZE = ${b}u; - var tileQ: array<${P.type.value}, ${b*b}>; - var tileV: array<${P.type.value}, ${b*b}>; - ${A.registerUniforms(D).declareVariables(...B,...q)} - ${A.mainStart([b,b,1])} + }`};return{name:"AttentionProbs",shaderCache:{hint:`${v};${u!==void 0};${i!==void 0};${e}`,inputDependencies:A},getRunData:()=>({outputs:k,dispatchGroup:T,programUniforms:S}),getShaderSource:I}},ng=(e,t,n,i,u,l,d=void 0,c=void 0)=>{let s=l+u.kvSequenceLength,r=u.nReps?u.nReps:1,o=u.vHiddenSize*r,a=e>1&&i,p=u.kvNumHeads?u.kvNumHeads:u.numHeads,h=a?[u.batchSize,p,s,u.headSize]:void 0,m=[u.batchSize,u.sequenceLength,o],g=12,v={x:Math.ceil(u.vHeadSize/g),y:Math.ceil(u.sequenceLength/g),z:u.batchSize*u.numHeads},w=[{type:12,data:u.sequenceLength},{type:12,data:s},{type:12,data:u.vHeadSize},{type:12,data:u.numHeads},{type:12,data:u.headSize},{type:12,data:o},{type:12,data:l},{type:12,data:u.kvSequenceLength},{type:12,data:r}],y=a&&i&&B.size(i.dims)>0,T=["type","type"];y&&T.push("type"),d&&T.push("type"),c&&T.push("type");let S=[{dims:m,dataType:t.dataType,gpuDataType:0}];a&&S.push({dims:h,dataType:t.dataType,gpuDataType:0});let O=A=>{let k=L("probs",t.dataType,t.dims),I=L("v",n.dataType,n.dims),R=[k,I];y&&R.push(L("past_value",i.dataType,i.dims));let M=d?L("seq_lens",d.dataType,d.dims):void 0;d&&R.push(M);let X=c?L("total_sequence_length_input",c.dataType,c.dims):void 0;c&&R.push(X);let G=[ne("output",t.dataType,m)];a&&G.push(ne("present_value",t.dataType,h));let C=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${g}u; + var tileQ: array<${k.type.value}, ${g*g}>; + var tileV: array<${k.type.value}, ${g*g}>; + ${A.registerUniforms(C).declareVariables(...R,...G)} + ${A.mainStart([g,g,1])} let headIdx = workgroup_id.z % uniforms.num_heads; let batchIdx = workgroup_id.z / uniforms.num_heads; let kvHeadIdx = ${r===1?"headIdx":"headIdx / uniforms.n_reps"}; @@ -2152,13 +2152,13 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas let n = global_id.x; let sequence_length = uniforms.M; var total_sequence_length = uniforms.K; - ${po(U,X,!0)} + ${wo(M,X,!0)} let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch ${y&&a?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""}; let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n; ${a?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""} - var value = ${P.type.storage}(0); + var value = ${k.type.storage}(0); for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { if (m < uniforms.M && w + local_id.x < uniforms.K) { tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x]; @@ -2193,13 +2193,13 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas + headIdx * uniforms.N + n; output[outputIdx] = value; } - }`};return{name:"AttentionScore",shaderCache:{hint:`${o!==void 0};${e}`,inputDependencies:S},getRunData:()=>({outputs:T,dispatchGroup:x,programUniforms:w}),getShaderSource:I}},Oi=(e,t,n,o,u,l,d,c,s,r,i=void 0,a=void 0)=>{let p=Math.min(e.outputCount,1+(d?1:0)+(c?1:0)),h=p>1?d:void 0,m=p>1?c:void 0,b=p>1?r.pastSequenceLength:0,x=b+r.kvSequenceLength,w=s&&z.size(s.dims)>0?s:void 0,y=[t,n];h&&z.size(h.dims)>0&&y.push(h),w&&y.push(w),i&&y.push(i),a&&y.push(a);let S=e.compute(Xm(p,t,n,h,w,r,b,i,a),{inputs:y,outputs:p>1?[-1,1]:[-1]})[0];e.compute(Km(S,r.batchSize,r.numHeads,b,r.sequenceLength,x,i,a),{inputs:i&&a?[S,i,a]:[S],outputs:[]});let T=[S,o];m&&z.size(m.dims)>0&&T.push(m),i&&T.push(i),a&&T.push(a),e.compute(Zm(p,S,o,m,r,b,i,a),{inputs:T,outputs:p>1?[0,2]:[0]})},Jm=(e,t)=>{let n=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],o=t.sequenceLength,u=t.inputHiddenSize,l=t.headSize,d=12,c={x:Math.ceil(t.headSize/d),y:Math.ceil(t.sequenceLength/d),z:t.batchSize*t.numHeads},s=[e.inputs[0],e.inputs[1],e.inputs[2]],r=[{type:12,data:o},{type:12,data:u},{type:12,data:l},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],i=a=>{let p=re("output_q",s[0].dataType,n),h=re("output_k",s[0].dataType,n),m=re("output_v",s[0].dataType,n),b=j("input",s[0].dataType,s[0].dims),x=j("weight",s[1].dataType,s[1].dims),w=j("bias",s[2].dataType,s[2].dims),y=b.type.storage,S=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` + }`};return{name:"AttentionScore",shaderCache:{hint:`${i!==void 0};${e}`,inputDependencies:T},getRunData:()=>({outputs:S,dispatchGroup:v,programUniforms:w}),getShaderSource:O}},Ri=(e,t,n,i,u,l,d,c,s,r,o=void 0,a=void 0)=>{let p=Math.min(e.outputCount,1+(d?1:0)+(c?1:0)),h=p>1?d:void 0,m=p>1?c:void 0,g=p>1?r.pastSequenceLength:0,v=g+r.kvSequenceLength,w=s&&B.size(s.dims)>0?s:void 0,y=[t,n];h&&B.size(h.dims)>0&&y.push(h),w&&y.push(w),o&&y.push(o),a&&y.push(a);let T=e.compute(rg(p,t,n,h,w,r,g,o,a),{inputs:y,outputs:p>1?[-1,1]:[-1]})[0];e.compute(tg(T,r.batchSize,r.numHeads,g,r.sequenceLength,v,o,a),{inputs:o&&a?[T,o,a]:[T],outputs:[]});let S=[T,i];m&&B.size(m.dims)>0&&S.push(m),o&&S.push(o),a&&S.push(a),e.compute(ng(p,T,i,m,r,g,o,a),{inputs:S,outputs:p>1?[0,2]:[0]})},ig=(e,t)=>{let n=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],i=t.sequenceLength,u=t.inputHiddenSize,l=t.headSize,d=12,c={x:Math.ceil(t.headSize/d),y:Math.ceil(t.sequenceLength/d),z:t.batchSize*t.numHeads},s=[e.inputs[0],e.inputs[1],e.inputs[2]],r=[{type:12,data:i},{type:12,data:u},{type:12,data:l},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],o=a=>{let p=ne("output_q",s[0].dataType,n),h=ne("output_k",s[0].dataType,n),m=ne("output_v",s[0].dataType,n),g=L("input",s[0].dataType,s[0].dims),v=L("weight",s[1].dataType,s[1].dims),w=L("bias",s[2].dataType,s[2].dims),y=g.type.storage,T=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` const TILE_SIZE = ${d}u; var tileInput: array<${y}, ${d*d}>; var tileWeightQ: array<${y}, ${d*d}>; var tileWeightK: array<${y}, ${d*d}>; var tileWeightV: array<${y}, ${d*d}>; - ${a.registerUniforms(S).declareVariables(b,x,w,p,h,m)} + ${a.registerUniforms(T).declareVariables(g,v,w,p,h,m)} ${a.mainStart([d,d,1])} let batchIndex = workgroup_id.z / uniforms.num_heads; let headNumber = workgroup_id.z % uniforms.num_heads; @@ -2248,34 +2248,34 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas output_k[outputIdx] = valueK; output_v[outputIdx] = valueV; } - }`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:c,programUniforms:r}),getShaderSource:i},{inputs:s,outputs:[-1,-1,-1]})},_x=(e,t)=>{let n=Wm(e.inputs,t),[o,u,l]=Jm(e,n);return Oi(e,o,u,l,e.inputs[4],void 0,void 0,void 0,e.inputs[5],n)}}),Ym,Qm,eg,vx,IA=R(()=>{"use strict";ot(),se(),ae(),Ve(),ue(),Ym=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let n=(o,u,l)=>{let d=u.length;if(d!==o.length)throw new Error(`${l}: num dimensions != ${d}`);u.forEach((c,s)=>{if(c!==o[s])throw new Error(`${l}: dim[${s}] do not match`)})};if(e[0].dims.length>1){let o=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);n(e[1].dims,o,"Invalid input scale"),n(e[2].dims,o,"Invalid input B"),n(e[3].dims,o,"Invalid input mean"),n(e[4].dims,o,"Invalid input var")}else n(e[1].dims,[1],"Invalid input scale"),n(e[2].dims,[1],"Invalid input B"),n(e[3].dims,[1],"Invalid input mean"),n(e[4].dims,[1],"Invalid input var")},Qm=(e,t)=>{let{epsilon:n,spatial:o,format:u}=t,l=e[0].dims,d=o?Ue(l[l.length-1]):1,c=u==="NHWC"&&l.length>1?d:1,s=z.size(l)/d,r=o,i=r?l.length:l,a=j("x",e[0].dataType,e[0].dims,d),p=j("scale",e[1].dataType,e[1].dims,c),h=j("bias",e[2].dataType,e[2].dims,c),m=j("inputMean",e[3].dataType,e[3].dims,c),b=j("inputVar",e[4].dataType,e[4].dims,c),x=re("y",e[0].dataType,i,d),w=()=>{let S="";if(o)S=`let cOffset = ${l.length===1?"0u":u==="NHWC"?`outputIndices[${l.length-1}] / ${d}`:"outputIndices[1]"};`;else if(u==="NCHW")S=` - ${x.indicesSet("outputIndices","0","0")} - let cOffset = ${x.indicesToOffset("outputIndices")};`;else{S=`var cIndices = ${p.type.indices}(0); - cIndices[0] = outputIndices[${l.length-1}];`;for(let T=1;T` + }`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:c,programUniforms:r}),getShaderSource:o},{inputs:s,outputs:[-1,-1,-1]})},Ox=(e,t)=>{let n=eg(e.inputs,t),[i,u,l]=ig(e,n);return Ri(e,i,u,l,e.inputs[4],void 0,void 0,void 0,e.inputs[5],n)}}),og,sg,ag,Ex,BA=N(()=>{"use strict";dt(),le(),pe(),He(),he(),og=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let n=(i,u,l)=>{let d=u.length;if(d!==i.length)throw new Error(`${l}: num dimensions != ${d}`);u.forEach((c,s)=>{if(c!==i[s])throw new Error(`${l}: dim[${s}] do not match`)})};if(e[0].dims.length>1){let i=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);n(e[1].dims,i,"Invalid input scale"),n(e[2].dims,i,"Invalid input B"),n(e[3].dims,i,"Invalid input mean"),n(e[4].dims,i,"Invalid input var")}else n(e[1].dims,[1],"Invalid input scale"),n(e[2].dims,[1],"Invalid input B"),n(e[3].dims,[1],"Invalid input mean"),n(e[4].dims,[1],"Invalid input var")},sg=(e,t)=>{let{epsilon:n,spatial:i,format:u}=t,l=e[0].dims,d=i?qe(l[l.length-1]):1,c=u==="NHWC"&&l.length>1?d:1,s=B.size(l)/d,r=i,o=r?l.length:l,a=L("x",e[0].dataType,e[0].dims,d),p=L("scale",e[1].dataType,e[1].dims,c),h=L("bias",e[2].dataType,e[2].dims,c),m=L("inputMean",e[3].dataType,e[3].dims,c),g=L("inputVar",e[4].dataType,e[4].dims,c),v=ne("y",e[0].dataType,o,d),w=()=>{let T="";if(i)T=`let cOffset = ${l.length===1?"0u":u==="NHWC"?`outputIndices[${l.length-1}] / ${d}`:"outputIndices[1]"};`;else if(u==="NCHW")T=` + ${v.indicesSet("outputIndices","0","0")} + let cOffset = ${v.indicesToOffset("outputIndices")};`;else{T=`var cIndices = ${p.type.indices}(0); + cIndices[0] = outputIndices[${l.length-1}];`;for(let S=1;S` const epsilon = ${n}; - ${S.registerUniform("outputSize","u32").declareVariables(a,p,h,m,b,x)} - ${S.mainStart()} - ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - var outputIndices = ${x.offsetToIndices(`global_idx * ${d}`)}; + ${T.registerUniform("outputSize","u32").declareVariables(a,p,h,m,g,v)} + ${T.mainStart()} + ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${v.offsetToIndices(`global_idx * ${d}`)}; ${w()} let scale = ${p.getByOffset("cOffset")}; let bias = ${h.getByOffset("cOffset")}; let inputMean = ${m.getByOffset("cOffset")}; - let inputVar = ${b.getByOffset("cOffset")}; + let inputVar = ${g.getByOffset("cOffset")}; let x = ${a.getByOffset("global_idx")}; let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; - ${x.setByOffset("global_idx","value")} - }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${o}_${d}`,inputDependencies:r?["rank","type","type","type","type"]:void 0},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r?[{type:12,data:s},...oe(l)]:[{type:12,data:s}]})}},eg=e=>Se(e),vx=(e,t)=>{let{inputs:n,outputCount:o}=e,u=eg({...t,outputCount:o});if(le.webgpu.validateInputContent&&Ym(n,u),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(Qm(n,u))}}),tg,rg,xx,OA=R(()=>{"use strict";ae(),ue(),tg=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},rg=e=>{let t=e[0].dims,n=e[0].dims[2],o=z.size(t)/4,u=e[0].dataType,l=j("input",u,t,4),d=j("bias",u,[n],4),c=j("residual",u,t,4),s=re("output",u,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)}}),getShaderSource:r=>` + ${v.setByOffset("global_idx","value")} + }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${i}_${d}`,inputDependencies:r?["rank","type","type","type","type"]:void 0},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r?[{type:12,data:s},...ae(l)]:[{type:12,data:s}]})}},ag=e=>ke(e),Ex=(e,t)=>{let{inputs:n,outputCount:i}=e,u=ag({...t,outputCount:i});if(me.webgpu.validateInputContent&&og(n,u),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(sg(n,u))}}),ug,lg,Ax,FA=N(()=>{"use strict";pe(),he(),ug=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},lg=e=>{let t=e[0].dims,n=e[0].dims[2],i=B.size(t)/4,u=e[0].dataType,l=L("input",u,t,4),d=L("bias",u,[n],4),c=L("residual",u,t,4),s=ne("output",u,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:r=>` const channels = ${n}u / 4; ${r.declareVariables(l,d,c,s)} ${r.mainStart()} - ${r.guardAgainstOutOfBoundsWorkgroupSizes(o)} + ${r.guardAgainstOutOfBoundsWorkgroupSizes(i)} let value = ${l.getByOffset("global_idx")} + ${d.getByOffset("global_idx % channels")} + ${c.getByOffset("global_idx")}; ${s.setByOffset("global_idx","value")} - }`}},xx=e=>{tg(e.inputs),e.compute(rg(e.inputs))}}),ng,$e,$x,Tx,Sx,Ix,Ox,Ex,Ax,kx,Px,ig,Cx,Dx,Rx,Nx,bi,zx,$o,Bx,Mx,Fx,jx,Lx,Ux,Vx,Gx,qx,Hx,Wx,Kx,Xx,Zx,Jx,Yx,Qx,za,e1,hl,fl,t1,r1,n1,og,sg,i1,Hl=R(()=>{"use strict";se(),ae(),Ve(),ue(),ng=(e,t,n,o,u,l,d)=>{let c=Math.ceil(t/4),s="";typeof u=="string"?s=`${u}(a)`:s=u("a");let r=j("inputData",n,[c],4),i=re("outputData",o,[c],4),a=[{name:"vec_size",type:"u32"}];return d&&a.push(...d),` - ${e.registerUniforms(a).declareVariables(r,i)} + }`}},Ax=e=>{ug(e.inputs),e.compute(lg(e.inputs))}}),dg,Ee,kx,Px,Cx,Dx,Rx,Nx,zx,Mx,Bx,cg,Fx,jx,Lx,Ux,Ti,Vx,ko,Gx,qx,Hx,Wx,Kx,Xx,Zx,Yx,Jx,Qx,e1,t1,r1,n1,i1,o1,s1,ja,a1,bl,yl,u1,l1,d1,pg,hg,c1,Zl=N(()=>{"use strict";le(),pe(),He(),he(),dg=(e,t,n,i,u,l,d)=>{let c=Math.ceil(t/4),s="";typeof u=="string"?s=`${u}(a)`:s=u("a");let r=L("inputData",n,[c],4),o=ne("outputData",i,[c],4),a=[{name:"vec_size",type:"u32"}];return d&&a.push(...d),` + ${e.registerUniforms(a).declareVariables(r,o)} ${l??""} @@ -2283,8 +2283,8 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} let a = ${r.getByOffset("global_idx")}; - ${i.setByOffset("global_idx",s)} - }`},$e=(e,t,n,o,u,l=e.dataType,d,c)=>{let s=[{type:12,data:Math.ceil(z.size(e.dims)/4)}];return d&&s.push(...d),{name:t,shaderCache:{hint:u,inputDependencies:["type"]},getShaderSource:r=>ng(r,z.size(e.dims),e.dataType,l,n,o,c),getRunData:r=>({outputs:[{dims:e.dims,dataType:l}],dispatchGroup:{x:Math.ceil(z.size(r[0].dims)/64/4)},programUniforms:s})}},$x=e=>{e.compute($e(e.inputs[0],"Abs","abs"))},Tx=e=>{e.compute($e(e.inputs[0],"Acos","acos"))},Sx=e=>{e.compute($e(e.inputs[0],"Acosh","acosh"))},Ix=e=>{e.compute($e(e.inputs[0],"Asin","asin"))},Ox=e=>{e.compute($e(e.inputs[0],"Asinh","asinh"))},Ex=e=>{e.compute($e(e.inputs[0],"Atan","atan"))},Ax=e=>{e.compute($e(e.inputs[0],"Atanh","atanh"))},kx=e=>Se(e),Px=(e,t)=>{let n;switch(t.to){case 10:n="vec4";break;case 1:n="vec4";break;case 12:n="vec4";break;case 6:n="vec4";break;case 9:n="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute($e(e.inputs[0],"Cast",n,void 0,t.cacheKey,t.to))},ig=e=>{let t,n,o=e.length>=2&&e[1].data!==0,u=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=o?e[1].getFloat32Array()[0]:-34028234663852886e22,n=u?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=o?e[1].getUint16Array()[0]:64511,n=u?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return Se({min:t,max:n})},Cx=(e,t)=>{let n=t||ig(e.inputs),o=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Clip",u=>`clamp(${u}, vec4<${o}>(uniforms.min), vec4<${o}>(uniforms.max))`,void 0,n.cacheKey,void 0,[{type:e.inputs[0].dataType,data:n.min},{type:e.inputs[0].dataType,data:n.max}],[{name:"min",type:o},{name:"max",type:o}]),{inputs:[0]})},Dx=e=>{e.compute($e(e.inputs[0],"Ceil","ceil"))},Rx=e=>{e.compute($e(e.inputs[0],"Cos","cos"))},Nx=e=>{e.compute($e(e.inputs[0],"Cosh","cosh"))},bi=e=>Se(e),zx=(e,t)=>{let n=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Elu",o=>`elu_vf32(${o})`,` + ${o.setByOffset("global_idx",s)} + }`},Ee=(e,t,n,i,u,l=e.dataType,d,c)=>{let s=[{type:12,data:Math.ceil(B.size(e.dims)/4)}];return d&&s.push(...d),{name:t,shaderCache:{hint:u,inputDependencies:["type"]},getShaderSource:r=>dg(r,B.size(e.dims),e.dataType,l,n,i,c),getRunData:r=>({outputs:[{dims:e.dims,dataType:l}],dispatchGroup:{x:Math.ceil(B.size(r[0].dims)/64/4)},programUniforms:s})}},kx=e=>{e.compute(Ee(e.inputs[0],"Abs","abs"))},Px=e=>{e.compute(Ee(e.inputs[0],"Acos","acos"))},Cx=e=>{e.compute(Ee(e.inputs[0],"Acosh","acosh"))},Dx=e=>{e.compute(Ee(e.inputs[0],"Asin","asin"))},Rx=e=>{e.compute(Ee(e.inputs[0],"Asinh","asinh"))},Nx=e=>{e.compute(Ee(e.inputs[0],"Atan","atan"))},zx=e=>{e.compute(Ee(e.inputs[0],"Atanh","atanh"))},Mx=e=>ke(e),Bx=(e,t)=>{let n;switch(t.to){case 10:n="vec4";break;case 1:n="vec4";break;case 12:n="vec4";break;case 6:n="vec4";break;case 9:n="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(Ee(e.inputs[0],"Cast",n,void 0,t.cacheKey,t.to))},cg=e=>{let t,n,i=e.length>=2&&e[1].data!==0,u=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=i?e[1].getFloat32Array()[0]:-34028234663852886e22,n=u?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=i?e[1].getUint16Array()[0]:64511,n=u?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return ke({min:t,max:n})},Fx=(e,t)=>{let n=t||cg(e.inputs),i=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Clip",u=>`clamp(${u}, vec4<${i}>(uniforms.min), vec4<${i}>(uniforms.max))`,void 0,n.cacheKey,void 0,[{type:e.inputs[0].dataType,data:n.min},{type:e.inputs[0].dataType,data:n.max}],[{name:"min",type:i},{name:"max",type:i}]),{inputs:[0]})},jx=e=>{e.compute(Ee(e.inputs[0],"Ceil","ceil"))},Lx=e=>{e.compute(Ee(e.inputs[0],"Cos","cos"))},Ux=e=>{e.compute(Ee(e.inputs[0],"Cosh","cosh"))},Ti=e=>ke(e),Vx=(e,t)=>{let n=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Elu",i=>`elu_vf32(${i})`,` const elu_alpha_ = ${n}(${t.alpha}); fn elu_f32(a: ${n}) -> ${n} { @@ -2293,7 +2293,7 @@ var best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLas fn elu_vf32(v: vec4<${n}>) -> vec4<${n}> { return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); - }`,t.cacheKey))},$o=(e="f32")=>` + }`,t.cacheKey))},ko=(e="f32")=>` const r0: ${e} = 0.3275911; const r1: ${e} = 0.254829592; const r2: ${e} = -0.284496736; @@ -2305,15 +2305,15 @@ fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { let absv = abs(v); let x = 1.0 / (1.0 + r0 * absv); return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); -}`,Bx=e=>{let t=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Erf",n=>`erf_vf32(${n})`,$o(t)))},Mx=e=>{e.compute($e(e.inputs[0],"Exp","exp"))},Fx=e=>{e.compute($e(e.inputs[0],"Floor","floor"))},jx=e=>{let t=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Gelu",n=>`0.5 * ${n} * (1.0 + erf_vf32(${n} * 0.7071067811865475))`,$o(t)))},Lx=(e,t)=>{let n=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"LeakyRelu",o=>`select(leaky_relu_alpha_ * ${o}, ${o}, ${o} >= vec4<${n}>(0.0))`,`const leaky_relu_alpha_ = ${n}(${t.alpha});`,t.cacheKey))},Ux=e=>{e.compute($e(e.inputs[0],"Not",t=>`!${t}`))},Vx=e=>{e.compute($e(e.inputs[0],"Neg",t=>`-${t}`))},Gx=e=>{e.compute($e(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},qx=e=>{let t=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"Relu",n=>`select(vec4<${t}>(0.0), ${n}, ${n} > vec4<${t}>(0.0))`))},Hx=e=>{e.compute($e(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},Wx=e=>Se(e),Kx=(e,t)=>{let n=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"HardSigmoid",o=>`max(vec4<${n}>(0.0), min(vec4<${n}>(1.0), ${t.alpha} * ${o} + vec4<${n}>(${t.beta})))`,void 0,t.cacheKey))},Xx=e=>{let t=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"HardSwish",n=>`${n} * max(vec4<${t}>(0.0), min(vec4<${t}>(1.0), vec4<${t}>(${t}(1.0 / 6.0)) * ${n} + vec4<${t}>(0.5)))`))},Zx=e=>{e.compute($e(e.inputs[0],"Sin","sin"))},Jx=e=>{e.compute($e(e.inputs[0],"Sinh","sinh"))},Yx=e=>{e.compute($e(e.inputs[0],"Sqrt","sqrt"))},Qx=e=>{e.compute($e(e.inputs[0],"Tan","tan"))},za=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,e1=e=>{e.compute($e(e.inputs[0],"Tanh",za))},hl=(e="f32")=>` +}`,Gx=e=>{let t=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Erf",n=>`erf_vf32(${n})`,ko(t)))},qx=e=>{e.compute(Ee(e.inputs[0],"Exp","exp"))},Hx=e=>{e.compute(Ee(e.inputs[0],"Floor","floor"))},Wx=e=>{let t=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Gelu",n=>`0.5 * ${n} * (1.0 + erf_vf32(${n} * 0.7071067811865475))`,ko(t)))},Kx=(e,t)=>{let n=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"LeakyRelu",i=>`select(leaky_relu_alpha_ * ${i}, ${i}, ${i} >= vec4<${n}>(0.0))`,`const leaky_relu_alpha_ = ${n}(${t.alpha});`,t.cacheKey))},Xx=e=>{e.compute(Ee(e.inputs[0],"Not",t=>`!${t}`))},Zx=e=>{e.compute(Ee(e.inputs[0],"Neg",t=>`-${t}`))},Yx=e=>{e.compute(Ee(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},Jx=e=>{let t=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Relu",n=>`select(vec4<${t}>(0.0), ${n}, ${n} > vec4<${t}>(0.0))`))},Qx=e=>{e.compute(Ee(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},e1=e=>ke(e),t1=(e,t)=>{let n=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"HardSigmoid",i=>`max(vec4<${n}>(0.0), min(vec4<${n}>(1.0), ${t.alpha} * ${i} + vec4<${n}>(${t.beta})))`,void 0,t.cacheKey))},r1=e=>{let t=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"HardSwish",n=>`${n} * max(vec4<${t}>(0.0), min(vec4<${t}>(1.0), vec4<${t}>(${t}(1.0 / 6.0)) * ${n} + vec4<${t}>(0.5)))`))},n1=e=>{e.compute(Ee(e.inputs[0],"Sin","sin"))},i1=e=>{e.compute(Ee(e.inputs[0],"Sinh","sinh"))},o1=e=>{e.compute(Ee(e.inputs[0],"Sqrt","sqrt"))},s1=e=>{e.compute(Ee(e.inputs[0],"Tan","tan"))},ja=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,a1=e=>{e.compute(Ee(e.inputs[0],"Tanh",ja))},bl=(e="f32")=>` const fast_gelu_a: ${e} = 0.5; const fast_gelu_b: ${e} = 0.7978845608028654; const fast_gelu_c: ${e} = 0.035677408136300125; fn tanh_v(v: vec4<${e}>) -> vec4<${e}> { - return ${za("v")}; + return ${ja("v")}; } -`,fl=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,t1=e=>{let t=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"FastGelu",fl,hl(t),void 0,e.inputs[0].dataType))},r1=(e,t)=>{let n=Je(e.inputs[0].dataType);return e.compute($e(e.inputs[0],"ThresholdedRelu",o=>`select(vec4<${n}>(0.0), ${o}, ${o} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${n}>(${t.alpha});`,t.cacheKey)),0},n1=e=>{e.compute($e(e.inputs[0],"Log","log"))},og=(e,t)=>` +`,yl=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,u1=e=>{let t=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"FastGelu",yl,bl(t),void 0,e.inputs[0].dataType))},l1=(e,t)=>{let n=tt(e.inputs[0].dataType);return e.compute(Ee(e.inputs[0],"ThresholdedRelu",i=>`select(vec4<${n}>(0.0), ${i}, ${i} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${n}>(${t.alpha});`,t.cacheKey)),0},d1=e=>{e.compute(Ee(e.inputs[0],"Log","log"))},pg=(e,t)=>` const alpha = vec4<${e}>(${t}); const one = ${e}(1.0); const zero = ${e}(0.0); @@ -2330,13 +2330,13 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { } return x * x1; } -`,sg=e=>`quick_gelu_impl(${e})`,i1=(e,t)=>{let n=Je(e.inputs[0].dataType);e.compute($e(e.inputs[0],"QuickGelu",sg,og(n,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),ag,ug,o1,EA=R(()=>{"use strict";ae(),ue(),Hl(),ag=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},ug=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let n=j("input",e[0].dataType,e[0].dims,4),o=j("bias",e[0].dataType,[e[0].dims[2]],4),u=re("output",e[0].dataType,t,4),l=z.size(t)/4,d=Ye(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)}}),getShaderSource:c=>` +`,hg=e=>`quick_gelu_impl(${e})`,c1=(e,t)=>{let n=tt(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"QuickGelu",hg,pg(n,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),fg,mg,p1,jA=N(()=>{"use strict";pe(),he(),Zl(),fg=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},mg=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let n=L("input",e[0].dataType,e[0].dims,4),i=L("bias",e[0].dataType,[e[0].dims[2]],4),u=ne("output",e[0].dataType,t,4),l=B.size(t)/4,d=rt(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)}}),getShaderSource:c=>` const M_SQRT2 = sqrt(2.0); const halfChannels = ${e[0].dims[2]/4/2}u; - ${c.declareVariables(n,o,u)} + ${c.declareVariables(n,i,u)} - ${$o(d)} + ${ko(d)} ${c.mainStart()} ${c.guardAgainstOutOfBoundsWorkgroupSizes(l)} @@ -2348,21 +2348,21 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); ${u.setByOffset("global_idx","valueLeft * geluRight")} - }`}},o1=e=>{ag(e.inputs),e.compute(ug(e.inputs))}}),lg,dg,Dt,s1,a1,u1,l1,d1,c1,p1,h1,f1,m1,AA=R(()=>{"use strict";se(),ae(),ue(),lg=(e,t,n,o,u,l,d,c,s,r,i,a)=>{let p,h;typeof c=="string"?p=h=(y,S)=>`${c}((${y}),(${S}))`:typeof c=="function"?p=h=c:(p=c.scalar,h=c.vector);let m=re("outputData",i,o.length,4),b=j("aData",s,t.length,4),x=j("bData",r,n.length,4),w;if(u)if(l){let y=z.size(t)===1,S=z.size(n)===1,T=t.length>0&&t[t.length-1]%4===0,I=n.length>0&&n[n.length-1]%4===0;y||S?w=m.setByOffset("global_idx",h(y?`${b.type.value}(${b.getByOffset("0")}.x)`:b.getByOffset("global_idx"),S?`${x.type.value}(${x.getByOffset("0")}.x)`:x.getByOffset("global_idx"))):w=` + }`}},p1=e=>{fg(e.inputs),e.compute(mg(e.inputs))}}),gg,bg,jt,h1,f1,m1,g1,b1,y1,w1,_1,v1,x1,LA=N(()=>{"use strict";le(),pe(),he(),gg=(e,t,n,i,u,l,d,c,s,r,o,a)=>{let p,h;typeof c=="string"?p=h=(y,T)=>`${c}((${y}),(${T}))`:typeof c=="function"?p=h=c:(p=c.scalar,h=c.vector);let m=ne("outputData",o,i.length,4),g=L("aData",s,t.length,4),v=L("bData",r,n.length,4),w;if(u)if(l){let y=B.size(t)===1,T=B.size(n)===1,S=t.length>0&&t[t.length-1]%4===0,O=n.length>0&&n[n.length-1]%4===0;y||T?w=m.setByOffset("global_idx",h(y?`${g.type.value}(${g.getByOffset("0")}.x)`:g.getByOffset("global_idx"),T?`${v.type.value}(${v.getByOffset("0")}.x)`:v.getByOffset("global_idx"))):w=` let outputIndices = ${m.offsetToIndices("global_idx * 4u")}; - let offsetA = ${b.broadcastedIndicesToOffset("outputIndices",m)}; - let offsetB = ${x.broadcastedIndicesToOffset("outputIndices",m)}; - ${m.setByOffset("global_idx",h(d||T?b.getByOffset("offsetA / 4u"):`${b.type.value}(${b.getByOffset("offsetA / 4u")}[offsetA % 4u])`,d||I?x.getByOffset("offsetB / 4u"):`${x.type.value}(${x.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} - `}else w=m.setByOffset("global_idx",h(b.getByOffset("global_idx"),x.getByOffset("global_idx")));else{if(!l)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let y=(S,T,I="")=>{let A=`aData[indexA${T}][componentA${T}]`,P=`bData[indexB${T}][componentB${T}]`;return` - let outputIndices${T} = ${m.offsetToIndices(`global_idx * 4u + ${T}u`)}; - let offsetA${T} = ${b.broadcastedIndicesToOffset(`outputIndices${T}`,m)}; - let offsetB${T} = ${x.broadcastedIndicesToOffset(`outputIndices${T}`,m)}; - let indexA${T} = offsetA${T} / 4u; - let indexB${T} = offsetB${T} / 4u; - let componentA${T} = offsetA${T} % 4u; - let componentB${T} = offsetB${T} % 4u; - ${S}[${T}] = ${I}(${p(A,P)}); - `};i===9?w=` + let offsetA = ${g.broadcastedIndicesToOffset("outputIndices",m)}; + let offsetB = ${v.broadcastedIndicesToOffset("outputIndices",m)}; + ${m.setByOffset("global_idx",h(d||S?g.getByOffset("offsetA / 4u"):`${g.type.value}(${g.getByOffset("offsetA / 4u")}[offsetA % 4u])`,d||O?v.getByOffset("offsetB / 4u"):`${v.type.value}(${v.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} + `}else w=m.setByOffset("global_idx",h(g.getByOffset("global_idx"),v.getByOffset("global_idx")));else{if(!l)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let y=(T,S,O="")=>{let A=`aData[indexA${S}][componentA${S}]`,k=`bData[indexB${S}][componentB${S}]`;return` + let outputIndices${S} = ${m.offsetToIndices(`global_idx * 4u + ${S}u`)}; + let offsetA${S} = ${g.broadcastedIndicesToOffset(`outputIndices${S}`,m)}; + let offsetB${S} = ${v.broadcastedIndicesToOffset(`outputIndices${S}`,m)}; + let indexA${S} = offsetA${S} / 4u; + let indexB${S} = offsetB${S} / 4u; + let componentA${S} = offsetA${S} % 4u; + let componentB${S} = offsetB${S} % 4u; + ${T}[${S}] = ${O}(${p(A,k)}); + `};o===9?w=` var data = vec4(0); ${y("data",0,"u32")} ${y("data",1,"u32")} @@ -2374,14 +2374,14 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { ${y("outputData[global_idx]",2)} ${y("outputData[global_idx]",3)} `}return` - ${e.registerUniform("vec_size","u32").declareVariables(b,x,m)} + ${e.registerUniform("vec_size","u32").declareVariables(g,v,m)} ${a??""} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} ${w} - }`},dg=(e,t,n,o,u,l,d=n.dataType)=>{let c=n.dims.map(Number),s=o.dims.map(Number),r=!z.areEqual(c,s),i=c,a=z.size(c),p=!1,h=!1,m=[r];if(r){let b=In.calcShape(c,s,!1);if(!b)throw new Error("Can't perform binary op on the given tensors");i=b.slice(),a=z.size(i);let x=z.size(c)===1,w=z.size(s)===1,y=c.length>0&&c[c.length-1]%4===0,S=s.length>0&&s[s.length-1]%4===0;m.push(x),m.push(w),m.push(y),m.push(S);let T=1;for(let I=1;Ib.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:b=>lg(b,c,s,i,p,r,h,u,n.dataType,o.dataType,d,l),getRunData:()=>({outputs:[{dims:i,dataType:d}],dispatchGroup:{x:Math.ceil(a/64/4)},programUniforms:[{type:12,data:Math.ceil(z.size(i)/4)},...oe(c,s,i)]})}},Dt=(e,t,n,o,u,l)=>{e.compute(dg(t,u??"",e.inputs[0],e.inputs[1],n,o,l))},s1=e=>{Dt(e,"Add",(t,n)=>`${t}+${n}`)},a1=e=>{Dt(e,"Div",(t,n)=>`${t}/${n}`)},u1=e=>{Dt(e,"Equal",{scalar:(t,n)=>`u32(${t}==${n})`,vector:(t,n)=>`vec4(${t}==${n})`},void 0,void 0,9)},l1=e=>{Dt(e,"Mul",(t,n)=>`${t}*${n}`)},d1=e=>{let t=j("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;Dt(e,"Pow",{scalar:(n,o)=>`pow_custom(${n},${o})`,vector:(n,o)=>`pow_vector_custom(${n},${o})`},` + }`},bg=(e,t,n,i,u,l,d=n.dataType)=>{let c=n.dims.map(Number),s=i.dims.map(Number),r=!B.areEqual(c,s),o=c,a=B.size(c),p=!1,h=!1,m=[r];if(r){let g=Rn.calcShape(c,s,!1);if(!g)throw new Error("Can't perform binary op on the given tensors");o=g.slice(),a=B.size(o);let v=B.size(c)===1,w=B.size(s)===1,y=c.length>0&&c[c.length-1]%4===0,T=s.length>0&&s[s.length-1]%4===0;m.push(v),m.push(w),m.push(y),m.push(T);let S=1;for(let O=1;Og.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:g=>gg(g,c,s,o,p,r,h,u,n.dataType,i.dataType,d,l),getRunData:()=>({outputs:[{dims:o,dataType:d}],dispatchGroup:{x:Math.ceil(a/64/4)},programUniforms:[{type:12,data:Math.ceil(B.size(o)/4)},...ae(c,s,o)]})}},jt=(e,t,n,i,u,l)=>{e.compute(bg(t,u??"",e.inputs[0],e.inputs[1],n,i,l))},h1=e=>{jt(e,"Add",(t,n)=>`${t}+${n}`)},f1=e=>{jt(e,"Div",(t,n)=>`${t}/${n}`)},m1=e=>{jt(e,"Equal",{scalar:(t,n)=>`u32(${t}==${n})`,vector:(t,n)=>`vec4(${t}==${n})`},void 0,void 0,9)},g1=e=>{jt(e,"Mul",(t,n)=>`${t}*${n}`)},b1=e=>{let t=L("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;jt(e,"Pow",{scalar:(n,i)=>`pow_custom(${n},${i})`,vector:(n,i)=>`pow_vector_custom(${n},${i})`},` fn pow_custom(a : ${t}, b : ${t}) -> ${t} { if (b == ${t}(0.0)) { return ${t}(1.0); @@ -2394,7 +2394,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { // TODO: implement vectorized pow return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); } - `)},c1=e=>{Dt(e,"Sub",(t,n)=>`${t}-${n}`)},p1=e=>{Dt(e,"Greater",{scalar:(t,n)=>`u32(${t}>${n})`,vector:(t,n)=>`vec4(${t}>${n})`},void 0,void 0,9)},h1=e=>{Dt(e,"Less",{scalar:(t,n)=>`u32(${t}<${n})`,vector:(t,n)=>`vec4(${t}<${n})`},void 0,void 0,9)},f1=e=>{Dt(e,"GreaterOrEqual",{scalar:(t,n)=>`u32(${t}>=${n})`,vector:(t,n)=>`vec4(${t}>=${n})`},void 0,void 0,9)},m1=e=>{Dt(e,"LessOrEqual",{scalar:(t,n)=>`u32(${t}<=${n})`,vector:(t,n)=>`vec4(${t}<=${n})`},void 0,void 0,9)}}),cg,pg,hg,fg,g1,b1,kA=R(()=>{"use strict";se(),ae(),Ve(),ue(),cg=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let n=0,o=e[n],u=o.dataType,l=o.dims.length;e.forEach((d,c)=>{if(c!==n){if(d.dataType!==u)throw new Error("input tensors should be one type");if(d.dims.length!==l)throw new Error("input tensors should have the same shape");d.dims.forEach((s,r)=>{if(r!==t&&s!==o.dims[r])throw new Error("non concat dimensions must match")})}})},pg=(e,t)=>` + `)},y1=e=>{jt(e,"Sub",(t,n)=>`${t}-${n}`)},w1=e=>{jt(e,"Greater",{scalar:(t,n)=>`u32(${t}>${n})`,vector:(t,n)=>`vec4(${t}>${n})`},void 0,void 0,9)},_1=e=>{jt(e,"Less",{scalar:(t,n)=>`u32(${t}<${n})`,vector:(t,n)=>`vec4(${t}<${n})`},void 0,void 0,9)},v1=e=>{jt(e,"GreaterOrEqual",{scalar:(t,n)=>`u32(${t}>=${n})`,vector:(t,n)=>`vec4(${t}>=${n})`},void 0,void 0,9)},x1=e=>{jt(e,"LessOrEqual",{scalar:(t,n)=>`u32(${t}<=${n})`,vector:(t,n)=>`vec4(${t}<=${n})`},void 0,void 0,9)}}),yg,wg,_g,vg,$1,T1,UA=N(()=>{"use strict";le(),pe(),He(),he(),yg=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let n=0,i=e[n],u=i.dataType,l=i.dims.length;e.forEach((d,c)=>{if(c!==n){if(d.dataType!==u)throw new Error("input tensors should be one type");if(d.dims.length!==l)throw new Error("input tensors should have the same shape");d.dims.forEach((s,r)=>{if(r!==t&&s!==i.dims[r])throw new Error("non concat dimensions must match")})}})},wg=(e,t)=>` fn calculateInputIndex(index: u32) -> u32 { let sizeInConcatAxis = array(${t}); for (var i: u32 = 0u; i < ${e}; i += 1u ) { @@ -2403,15 +2403,15 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { } } return ${e}u; - }`,hg=(e,t)=>{let n=e.length,o=[];for(let u=0;u{let u=z.size(n),l=new Array(e.length),d=new Array(e.length),c=0,s=[],r=[],i=[{type:12,data:u}];for(let b=0;b`uniforms.sizeInConcatAxis${b}`).join(","),m=b=>` + }`,_g=(e,t)=>{let n=e.length,i=[];for(let u=0;u{let u=B.size(n),l=new Array(e.length),d=new Array(e.length),c=0,s=[],r=[],o=[{type:12,data:u}];for(let g=0;g`uniforms.sizeInConcatAxis${g}`).join(","),m=g=>` - ${(()=>{b.registerUniform("outputSize","u32");for(let x=0;x{g.registerUniform("outputSize","u32");for(let v=0;v) -> vec4<${e}> { ${p} -= sizeInConcatAxis[inputIndex - 1u]; } - ${hg(d,a)} - }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:s},getRunData:()=>({outputs:[{dims:n,dataType:o}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:i}),getShaderSource:m}},g1=(e,t)=>{let n=e.inputs,o=n[0].dims,u=z.normalizeAxis(t.axis,o.length);cg(n,u);let l=o.slice();l[u]=n.reduce((c,s)=>c+(s.dims.length>u?s.dims[u]:0),0);let d=n.filter(c=>z.size(c.dims)>0);e.compute(fg(d,u,l,n[0].dataType),{inputs:d})},b1=e=>Se({axis:e.axis})}),on,sn,an,Wl,cn=R(()=>{"use strict";se(),ae(),on=(e,t,n="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${n}(uniforms.clip_min)), ${t}(${n}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${n}(uniforms.alpha) * value + ${n}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${n}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); + ${_g(d,a)} + }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:s},getRunData:()=>({outputs:[{dims:n,dataType:i}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:o}),getShaderSource:m}},$1=(e,t)=>{let n=e.inputs,i=n[0].dims,u=B.normalizeAxis(t.axis,i.length);yg(n,u);let l=i.slice();l[u]=n.reduce((c,s)=>c+(s.dims.length>u?s.dims[u]:0),0);let d=n.filter(c=>B.size(c.dims)>0);e.compute(vg(d,u,l,n[0].dataType),{inputs:d})},T1=e=>ke({axis:e.axis})}),hn,fn,mn,Yl,wn=N(()=>{"use strict";le(),pe(),hn=(e,t,n="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${n}(uniforms.clip_min)), ${t}(${n}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${n}(uniforms.alpha) * value + ${n}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${n}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); value = sign(value) * (1.0 - e2x) / (1.0 + e2x); - `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},sn=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},an=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},Wl=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[n,o]=e?.activation_params||[.2,.5];return{activation:t,alpha:n,beta:o}}else if(t==="Clip"){let[n,o]=e?.activation_params||[Uv,Vv];return{activation:t,clipMax:o,clipMin:n}}else if(t==="LeakyRelu"){let[n]=e?.activation_params||[.01];return{activation:t,alpha:n}}return{activation:t}}}),tt,y1,Kl=R(()=>{"use strict";tt=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},y1=e=>` + `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},fn=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},mn=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},Yl=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[n,i]=e?.activation_params||[.2,.5];return{activation:t,alpha:n,beta:i}}else if(t==="Clip"){let[n,i]=e?.activation_params||[Xv,Zv];return{activation:t,clipMax:i,clipMin:n}}else if(t==="LeakyRelu"){let[n]=e?.activation_params||[.01];return{activation:t,alpha:n}}return{activation:t}}}),st,S1,Jl=N(()=>{"use strict";st=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},S1=e=>` ${e?"value = value + getBiasByOutputCoords(coords);":""} - `}),w1,PA=R(()=>{"use strict";w1=e=>` + `}),I1,VA=N(()=>{"use strict";I1=e=>` fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { return dot(coords, vec4( shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); @@ -2435,53 +2435,53 @@ fn getOutputIndexFromCoords(coords : vec4) -> i32 { return dot(coords, vec4( i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); } -`}),$i,Xl,Zl=R(()=>{"use strict";se(),ae(),ue(),cn(),$i=(e,t,n,o,u)=>{let l=o-n;return` +`}),ki,Ql,ed=N(()=>{"use strict";le(),pe(),he(),wn(),ki=(e,t,n,i,u)=>{let l=i-n;return` ${Array.from({length:n}).map((d,c)=>` - if (${ne(t.shape,c,t.rank)} != 1) { - ${t.indicesSet(e,c,ne(u,c+l,o))} + if (${oe(t.shape,c,t.rank)} != 1) { + ${t.indicesSet(e,c,oe(u,c+l,i))} } else { ${t.indicesSet(e,c,0)} }`).join("")} -`},Xl=(e,t,n,o,u=!1,l)=>{let d=e[0].dims,c=e[1].dims,s=d[d.length-2],r=c[c.length-1],i=d[d.length-1],a=Ue(r),p=Ue(i),h=Ue(s),m=z.size(n)/a/h,b=e.length>2,x=o?o.slice(0,-2):n.slice(0,-2),w=[z.size(x),s,r],y=[{type:12,data:m},{type:12,data:s},{type:12,data:r},{type:12,data:i}];sn(t,y),y.push(...oe(x,d,c)),b&&y.push(...oe(e[2].dims)),y.push(...oe(w));let S=T=>{let I=Vl("batch_dims",e[0].dataType,x.length),A=j("a",e[0].dataType,d.length,p),P=j("b",e[1].dataType,c.length,a),O=re("output",e[0].dataType,w.length,a),B=Ye(O.type.tensor),U=on(t,O.type.value,B),X=[A,P],q="";if(b){let _=u?a:1;X.push(j("bias",e[2].dataType,e[2].dims.length,_)),q=`${u?`value += bias[col / ${_}];`:`value += ${O.type.value}(bias[row + i]);`}`}let D=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];an(t,D);let Q=()=>{let _=`var a_data: ${A.type.value};`;for(let C=0;C{let d=e[0].dims,c=e[1].dims,s=d[d.length-2],r=c[c.length-1],o=d[d.length-1],a=qe(r),p=qe(o),h=qe(s),m=B.size(n)/a/h,g=e.length>2,v=i?i.slice(0,-2):n.slice(0,-2),w=[B.size(v),s,r],y=[{type:12,data:m},{type:12,data:s},{type:12,data:r},{type:12,data:o}];fn(t,y),y.push(...ae(v,d,c)),g&&y.push(...ae(e[2].dims)),y.push(...ae(w));let T=S=>{let O=Wl("batch_dims",e[0].dataType,v.length),A=L("a",e[0].dataType,d.length,p),k=L("b",e[1].dataType,c.length,a),I=ne("output",e[0].dataType,w.length,a),R=rt(I.type.tensor),M=hn(t,I.type.value,R),X=[A,k],G="";if(g){let _=u?a:1;X.push(L("bias",e[2].dataType,e[2].dims.length,_)),G=`${u?`value += bias[col / ${_}];`:`value += ${I.type.value}(bias[row + i]);`}`}let C=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];mn(t,C);let W=()=>{let _=`var a_data: ${A.type.value};`;for(let D=0;D; + var b_indices: ${k.type.indices}; + ${ki("b_indices",k,k.rank-2,O.rank,"batch_indices")} + ${k.indicesSet("b_indices",k.rank-2,0)} + ${k.indicesSet("b_indices",k.rank-1,0)} + let b_offset = ${k.indicesToOffset("b_indices")}; + var values: array<${I.type.value}, ${h}>; for (var k: u32 = 0u; k < uniforms.K; k = k + ${p}) { - ${Q()} + ${W()} } for (var i = 0u; i < ${h}u; i++) { var value = values[i]; - ${q} - ${U} - let cur_indices = ${O.type.indices}(batch, row + i, col); - let offset = ${O.indicesToOffset("cur_indices")}; - ${O.setByOffset(`offset / ${a}`,"value")}; + ${G} + ${M} + let cur_indices = ${I.type.indices}(batch, row + i, col); + let offset = ${I.indicesToOffset("cur_indices")}; + ${I.setByOffset(`offset / ${a}`,"value")}; } } - `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${a};${p};${h};${u}`,inputDependencies:b?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:y}),getShaderSource:S}}}),mg,gg,ml,Ba,bg,gl,yg,Do,Jl=R(()=>{"use strict";se(),ae(),ue(),cn(),Zl(),Kl(),mg=(e,t)=>e?` + `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${a};${p};${h};${u}`,inputDependencies:g?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:y}),getShaderSource:T}}}),xg,$g,wl,La,Tg,_l,Sg,jo,td=N(()=>{"use strict";le(),pe(),he(),wn(),ed(),Jl(),xg=(e,t)=>e?` mm_Asub[inputRow][inputCol] = mm_readA(batch, kStart + inputRow, globalRowStart / innerElementSize + inputCol${t?", batchIndices":""}); @@ -2489,7 +2489,7 @@ fn getOutputIndexFromCoords(coords : vec4) -> i32 { mm_Asub[inputRow][inputCol] = mm_readA(batch, globalRow + innerRow, kStart / innerElementSize + inputCol${t?", batchIndices":""}); - `,gg=(e,t)=>e?` + `,$g=(e,t)=>e?` let ACached0 = mm_Asub[k * innerElementSize][localRow]; let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; @@ -2506,10 +2506,10 @@ fn getOutputIndexFromCoords(coords : vec4) -> i32 { acc[i] = BCached1 * ACached.y + acc[i]; acc[i] = BCached2 * ACached.z + acc[i]; ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"} - }`,ml=(e,t,n="f32",o,u=!1,l=32,d=!1,c=32)=>{let s=t[1]*e[1],r=t[0]*e[0],i=u?s:l,a=u?l:s,p=i/t[0],h=l/t[1];if(!((u&&p===4&&e[1]===4||!u&&(p===3||p===4))&&i%t[0]===0&&l%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${u} is true, innerElementSize ${p} and workPerThread[1] ${e[1]} must be 4. + }`,wl=(e,t,n="f32",i,u=!1,l=32,d=!1,c=32)=>{let s=t[1]*e[1],r=t[0]*e[0],o=u?s:l,a=u?l:s,p=o/t[0],h=l/t[1];if(!((u&&p===4&&e[1]===4||!u&&(p===3||p===4))&&o%t[0]===0&&l%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${u} is true, innerElementSize ${p} and workPerThread[1] ${e[1]} must be 4. Otherwise, innerElementSize ${p} must be 3 or 4. - tileAWidth ${i} must be divisible by workgroupSize[0]${t[0]}. tileInner ${l} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` -var mm_Asub: array, ${i/p}>, ${a}>; + tileAWidth ${o} must be divisible by workgroupSize[0]${t[0]}. tileInner ${l} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` +var mm_Asub: array, ${o/p}>, ${a}>; var mm_Bsub: array, ${r/e[0]}>, ${l}>; const rowPerThread = ${e[1]}; @@ -2528,7 +2528,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let globalRow =i32(globalId.y) * rowPerThread; let globalCol = i32(globalId.x); let batch = ${d?"0":"i32(globalId.z)"}; - ${o?`let batchIndices = ${o.offsetToIndices("u32(batch)")};`:""} + ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""} let globalRowStart = i32(workgroupId.y) * ${s}; let num_tiles = ${d?`${Math.ceil(c/l)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; @@ -2543,14 +2543,14 @@ fn main(@builtin(local_invocation_id) localId : vec3, for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { let inputRow = tileRow + innerRow; let inputCol = tileCol; - ${mg(u,o)} + ${xg(u,i)} } // Load one tile of B into local memory. for (var innerRow = 0; innerRow < ${h}; innerRow = innerRow + 1) { let inputRow = tileRowB + innerRow; let inputCol = tileCol; - mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${o?", batchIndices":""}); + mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${i?", batchIndices":""}); } kStart = kStart + tileInner; workgroupBarrier(); @@ -2562,7 +2562,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; ${p===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} - ${gg(u,p)} + ${$g(u,p)} } workgroupBarrier(); @@ -2571,7 +2571,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); } -}`},Ba=(e,t)=>e?` +}`},La=(e,t)=>e?` mm_Asub[inputRow][inputCol] = mm_readA(batch, kStart + inputRow, globalRowStart + inputCol${t?", batchIndices":""}); @@ -2579,26 +2579,26 @@ fn main(@builtin(local_invocation_id) localId : vec3, mm_Asub[inputRow][inputCol] = mm_readA(batch, globalRowStart + inputRow, kStart + inputCol${t?", batchIndices":""}); - `,bg=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",gl=(e,t,n="f32",o,u=!1,l=32,d=!1,c=32,s=!1)=>{let r=e[1]*t[1],i=e[0]*t[0],a=u?r:l,p=u?l:r;if(!(p%t[1]===0&&a%t[0]===0&&l%t[1]===0))throw new Error(`tileAHight ${p} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${a} must be divisible by workgroupSize[0]${t[0]}, tileInner ${l} must be divisible by workgroupSize[1]${t[1]}`);let h=p/t[1],m=a/t[0],b=l/t[1],x=s?` + `,Tg=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",_l=(e,t,n="f32",i,u=!1,l=32,d=!1,c=32,s=!1)=>{let r=e[1]*t[1],o=e[0]*t[0],a=u?r:l,p=u?l:r;if(!(p%t[1]===0&&a%t[0]===0&&l%t[1]===0))throw new Error(`tileAHight ${p} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${a} must be divisible by workgroupSize[0]${t[0]}, tileInner ${l} must be divisible by workgroupSize[1]${t[1]}`);let h=p/t[1],m=a/t[0],g=l/t[1],v=s?` let localRow = i32(localId.y); let localCol = i32(localId.x); let globalRowStart = i32(workgroupId.y) * ${r}; - let globalColStart = i32(workgroupId.x) * ${i}; + let globalColStart = i32(workgroupId.x) * ${o}; // Loop over shared dimension. for (var t = 0; t < num_tiles; t = t + 1) { // Load one tile of A into local memory. for (var inputRow = localRow; inputRow < ${p}; inputRow = inputRow + ${t[1]}) { for (var inputCol = localCol; inputCol < ${a}; inputCol = inputCol + ${t[0]}) { - ${Ba(u,o)} + ${La(u,i)} } } // Load one tile of B into local memory. for (var inputRow = localRow; inputRow < ${l}; inputRow = inputRow + ${t[1]}) { - for (var inputCol = localCol; inputCol < ${i}; inputCol = inputCol + ${t[0]}) { + for (var inputCol = localCol; inputCol < ${o}; inputCol = inputCol + ${t[0]}) { mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, - globalColStart + inputCol${o?", batchIndices":""}); + globalColStart + inputCol${i?", batchIndices":""}); } } kStart = kStart + tileInner; @@ -2637,7 +2637,7 @@ let globalRowStart = i32(workgroupId.y) * ${r}; let tileRowA = i32(localId.y) * ${h}; let tileColA = i32(localId.x) * ${m}; -let tileRowB = i32(localId.y) * ${b}; +let tileRowB = i32(localId.y) * ${g}; // Loop over shared dimension. for (var t = 0; t < num_tiles; t = t + 1) { // Load one tile of A into local memory. @@ -2645,18 +2645,18 @@ for (var t = 0; t < num_tiles; t = t + 1) { for (var innerCol = 0; innerCol < ${m}; innerCol = innerCol + 1) { let inputRow = tileRowA + innerRow; let inputCol = tileColA + innerCol; - ${Ba(u,o)} + ${La(u,i)} } } // Load one tile of B into local memory. - for (var innerRow = 0; innerRow < ${b}; innerRow = innerRow + 1) { + for (var innerRow = 0; innerRow < ${g}; innerRow = innerRow + 1) { for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { let inputRow = tileRowB + innerRow; let inputCol = tileCol + innerCol; mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, - globalCol + innerCol${o?", batchIndices":""}); + globalCol + innerCol${i?", batchIndices":""}); } } kStart = kStart + tileInner; @@ -2670,7 +2670,7 @@ for (var t = 0; t < num_tiles; t = t + 1) { } for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - ${bg(u)} + ${Tg(u)} for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; } @@ -2688,7 +2688,7 @@ for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { } `;return` var mm_Asub : array, ${p}>; - var mm_Bsub : array, ${l}>; + var mm_Bsub : array, ${l}>; const rowPerThread = ${e[1]}; const colPerThread = ${e[0]}; const tileInner = ${l}; @@ -2698,21 +2698,21 @@ fn main(@builtin(local_invocation_id) localId : vec3, @builtin(global_invocation_id) globalId : vec3, @builtin(workgroup_id) workgroupId : vec3) { let batch = ${d?"0":"i32(globalId.z)"}; - ${o?`let batchIndices = ${o.offsetToIndices("u32(batch)")};`:""} + ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""} let num_tiles = ${d?`${Math.ceil(c/l)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; var kStart = ${d?`i32(globalId.z) * ${c}`:"0"}; var acc : array, rowPerThread>; - ${x} + ${v} } -`},yg=(e,t,n,o,u=!1)=>{let[l,d,c,s]=o,r=Ye(o[0].type.tensor);return` - fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${tt(e,r)} { - var value = ${tt(e,r)}(0.0); +`},Sg=(e,t,n,i,u=!1)=>{let[l,d,c,s]=i,r=rt(i[0].type.tensor);return` + fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${st(e,r)} { + var value = ${st(e,r)}(0.0); let col = colIn * ${e}; if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) { var aIndices: ${d.type.indices}; - ${$i("aIndices",d,d.rank-2,l.rank,"batchIndices")} + ${ki("aIndices",d,d.rank-2,l.rank,"batchIndices")} ${d.indicesSet("aIndices",d.rank-2,"u32(row)")} ${d.indicesSet("aIndices",d.rank-1,"u32(colIn)")} value = ${d.getByIndices("aIndices")}; @@ -2720,13 +2720,13 @@ fn main(@builtin(local_invocation_id) localId : vec3, return value; } - fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${tt(e,r)} { - var value = ${tt(e,r)}(0.0); + fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${l.type.indices}) -> ${st(e,r)} { + var value = ${st(e,r)}(0.0); let col = colIn * ${e}; if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) { var bIndices: ${c.type.indices}; - ${$i("bIndices",c,c.rank-2,l.rank,"batchIndices")} + ${ki("bIndices",c,c.rank-2,l.rank,"batchIndices")} ${c.indicesSet("bIndices",c.rank-2,"u32(row)")} ${c.indicesSet("bIndices",c.rank-1,"u32(colIn)")} value = ${c.getByIndices("bIndices")}; @@ -2734,21 +2734,21 @@ fn main(@builtin(local_invocation_id) localId : vec3, return value; } - fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${tt(e,r)}) { + fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${st(e,r)}) { let col = colIn * ${e}; if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { var value = valueIn; let coords = vec3(batch, row, colIn); - ${t?`value = value + ${u?"bias[colIn]":`${tt(e,r)}(bias[row])`};`:""} + ${t?`value = value + ${u?"bias[colIn]":`${st(e,r)}(bias[row])`};`:""} ${n} ${s.setByIndices("vec3(coords)","value")} } } - `},Do=(e,t,n,o,u=!1,l)=>{let d=e[0].dims,c=e[1].dims,s=d.slice(0,-2),r=c.slice(0,-2),i=o?o.slice(0,-2):n.slice(0,-2),a=z.size(i),p=d[d.length-2],h=d[d.length-1],m=c[c.length-1],b=h%4===0&&m%4===0,x=p<=8?[4,1,1]:[4,4,1],w=[8,8,1],y=[Math.ceil(m/w[0]/x[0]),Math.ceil(p/w[1]/x[1]),Math.ceil(a/w[2]/x[2])],S=b?4:1,T=[...s,p,h/S],I=T.length,A=[...r,h,m/S],P=A.length,O=[a,p,m/S],B=[{type:6,data:p},{type:6,data:m},{type:6,data:h}];sn(t,B),B.push(...oe(i,T,A));let U=["rank","rank"],X=e.length>2;X&&(B.push(...oe(e[2].dims)),U.push("rank")),B.push(...oe(O));let q=D=>{let Q=i.length,_=Vl("batchDims",e[0].dataType,Q,1),C=Ye(e[0].dataType),F=j("a",e[0].dataType,I,S),V=j("b",e[1].dataType,P,S),G=re("result",e[0].dataType,O.length,S),H=[F,V];if(X){let pe=u?S:1;H.push(j("bias",e[2].dataType,e[2].dims.length,pe))}let M=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];an(t,M);let J=Ye(G.type.tensor),Z=on(t,G.type.value,J),W=yg(S,X,Z,[_,F,V,G],u);return` - ${D.registerUniforms(M).registerInternalVariables(_).declareVariables(...H,G)} - ${W} - ${b?ml(x,w,C,_):gl(x,w,C,_)} - `};return{name:"MatMul",shaderCache:{hint:`${x};${t.activation};${b};${u}`,inputDependencies:U},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:y[0],y:y[1],z:y[2]},programUniforms:B}),getShaderSource:q}}}),wg,_1,CA=R(()=>{"use strict";se(),Qt(),ue(),cn(),Kl(),PA(),Jl(),wg=(e,t,n,o,u=!1,l,d=4,c=4,s=4,r="f32")=>{let i=B=>{switch(B){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${r}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${B} is not supported.`)}},a=B=>{switch(B){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${B} is not supported.`)}},p=e?` + `},jo=(e,t,n,i,u=!1,l)=>{let d=e[0].dims,c=e[1].dims,s=d.slice(0,-2),r=c.slice(0,-2),o=i?i.slice(0,-2):n.slice(0,-2),a=B.size(o),p=d[d.length-2],h=d[d.length-1],m=c[c.length-1],g=h%4===0&&m%4===0,v=p<=8?[4,1,1]:[4,4,1],w=[8,8,1],y=[Math.ceil(m/w[0]/v[0]),Math.ceil(p/w[1]/v[1]),Math.ceil(a/w[2]/v[2])],T=g?4:1,S=[...s,p,h/T],O=S.length,A=[...r,h,m/T],k=A.length,I=[a,p,m/T],R=[{type:6,data:p},{type:6,data:m},{type:6,data:h}];fn(t,R),R.push(...ae(o,S,A));let M=["rank","rank"],X=e.length>2;X&&(R.push(...ae(e[2].dims)),M.push("rank")),R.push(...ae(I));let G=C=>{let W=o.length,_=Wl("batchDims",e[0].dataType,W,1),D=rt(e[0].dataType),V=L("a",e[0].dataType,O,T),q=L("b",e[1].dataType,k,T),H=ne("result",e[0].dataType,I.length,T),K=[V,q];if(X){let ee=u?T:1;K.push(L("bias",e[2].dataType,e[2].dims.length,ee))}let F=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];mn(t,F);let Q=rt(H.type.tensor),Y=hn(t,H.type.value,Q),j=Sg(T,X,Y,[_,V,q,H],u);return` + ${C.registerUniforms(F).registerInternalVariables(_).declareVariables(...K,H)} + ${j} + ${g?wl(v,w,D,_):_l(v,w,D,_)} + `};return{name:"MatMul",shaderCache:{hint:`${v};${t.activation};${g};${u}`,inputDependencies:M},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:y[0],y:y[1],z:y[2]},programUniforms:R}),getShaderSource:G}}}),Ig,O1,GA=N(()=>{"use strict";le(),ar(),he(),wn(),Jl(),VA(),td(),Ig=(e,t,n,i,u=!1,l,d=4,c=4,s=4,r="f32")=>{let o=R=>{switch(R){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${r}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${R} is not supported.`)}},a=R=>{switch(R){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${R} is not supported.`)}},p=e?` let coord = vec4(batch, xRow, xCol, xCh); `:` let coord = vec4(batch, xCh, xRow, xCol); @@ -2764,116 +2764,116 @@ fn main(@builtin(local_invocation_id) localId : vec3, row, col / outWidth, col % outWidth); - `,m=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",b=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",x=e?"row":"col",w=e?"col":"row",y=` + `,m=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",g=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",v=e?"row":"col",w=e?"col":"row",y=` let inChannels = i32(uniforms.w_shape[2]); let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; - let outRow = ${x} / outWidth; - let outCol = ${x} % outWidth; + let outRow = ${v} / outWidth; + let outCol = ${v} % outWidth; let WRow = ${w} / (i32(uniforms.w_shape[1]) * inChannels); let WCol = ${w} / inChannels % i32(uniforms.w_shape[1]); let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; let xCh = ${w} % inChannels; - var resData = ${tt(d,r)}(0.0); + var resData = ${st(d,r)}(0.0); // The bounds checking is always needed since we use it to pad zero for // the 'same' padding type. - if (xRow >= 0 && xRow < ${m} && xCol >= 0 && xCol < ${b}) { + if (xRow >= 0 && xRow < ${m} && xCol >= 0 && xCol < ${g}) { ${p} let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); - ${i(d)} + ${o(d)} } - return resData;`,S=e?t&&o?` + return resData;`,T=e?t&&i?` let col = colIn * ${d}; ${y}`:` let col = colIn * ${d}; if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { ${y} } - return ${tt(d,r)}(0.0);`:o&&n?` + return ${st(d,r)}(0.0);`:i&&n?` let col = colIn * ${d}; ${y}`:` let col = colIn * ${d}; if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { ${y} } - return ${tt(d,r)}(0.0);`,T=e?o&&n?a(c):` + return ${st(d,r)}(0.0);`,S=e?i&&n?a(c):` let col = colIn * ${c}; if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { ${a(c)} } - return ${tt(c,r)}(0.0);`:` + return ${st(c,r)}(0.0);`:` let col = colIn * ${c}; if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) { ${a(c)} } - return ${tt(c,r)}(0.0);`,I=tt(s,r),A=tt(e?d:c,r),P=tt(e?c:d,r),O=on(l,I,r);return` + return ${st(c,r)}(0.0);`,O=st(s,r),A=st(e?d:c,r),k=st(e?c:d,r),I=hn(l,O,r);return` fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${A} { - ${e?S:T} + ${e?T:S} } - fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${P} { - ${e?T:S} + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${k} { + ${e?S:T} } - fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${I}) { + fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${O}) { let col = colIn * ${s}; if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { var value = valueIn; let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; ${h} - ${y1(u)} - ${O} + ${S1(u)} + ${I} setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); } - }`},_1=(e,t,n,o,u,l,d,c,s)=>{let r=t.format==="NHWC",i=r?e[0].dims[3]:e[0].dims[1],a=n[0],p=r?n[2]:n[3],h=r?n[1]:n[2],m=r?n[3]:n[1],b=r&&(i%4===0||i%3===0)&&m%4===0,x=r?m:p*h,w=r?p*h:m,y=[8,8,1],S=o<=8?[4,1,1]:[4,4,1],T=[Math.ceil(x/y[0]/S[0]),Math.ceil(w/y[1]/S[1]),Math.ceil(a/y[2]/S[2])];ve("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${T}`);let I=b?r&&i%4!==0?3:4:1,A=y[1]*S[1],P=y[0]*S[0],O=Math.max(y[0]*I,y[1]),B=o%A===0,U=u%P===0,X=l%O===0,q=b?[I,4,4]:[1,1,1],D=[{type:6,data:o},{type:6,data:u},{type:6,data:l},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];sn(t,D),D.push(...oe(e[0].dims,e[1].dims));let Q=["rank","rank"];d&&(D.push(...oe(e[2].dims)),Q.push("rank")),D.push(...oe(n));let _=C=>{let F=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];an(t,F);let V=b?4:1,G=Ye(e[0].dataType),H=` - fn setOutputAtIndex(flatIndex : i32, value : ${b?`vec4<${G}>`:G}) { - result[flatIndex] = ${b?`vec4<${G}>`:G}(value); + }`},O1=(e,t,n,i,u,l,d,c,s)=>{let r=t.format==="NHWC",o=r?e[0].dims[3]:e[0].dims[1],a=n[0],p=r?n[2]:n[3],h=r?n[1]:n[2],m=r?n[3]:n[1],g=r&&(o%4===0||o%3===0)&&m%4===0,v=r?m:p*h,w=r?p*h:m,y=[8,8,1],T=i<=8?[4,1,1]:[4,4,1],S=[Math.ceil(v/y[0]/T[0]),Math.ceil(w/y[1]/T[1]),Math.ceil(a/y[2]/T[2])];Ie("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${S}`);let O=g?r&&o%4!==0?3:4:1,A=y[1]*T[1],k=y[0]*T[0],I=Math.max(y[0]*O,y[1]),R=i%A===0,M=u%k===0,X=l%I===0,G=g?[O,4,4]:[1,1,1],C=[{type:6,data:i},{type:6,data:u},{type:6,data:l},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];fn(t,C),C.push(...ae(e[0].dims,e[1].dims));let W=["rank","rank"];d&&(C.push(...ae(e[2].dims)),W.push("rank")),C.push(...ae(n));let _=D=>{let V=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];mn(t,V);let q=g?4:1,H=rt(e[0].dataType),K=` + fn setOutputAtIndex(flatIndex : i32, value : ${g?`vec4<${H}>`:H}) { + result[flatIndex] = ${g?`vec4<${H}>`:H}(value); } - fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${b?`vec4<${G}>`:G}) { + fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${g?`vec4<${H}>`:H}) { let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); - setOutputAtIndex(flatIndex ${b?"/ 4":""}, value); - }`,M=j("x",e[0].dataType,e[0].dims.length,I===3?1:I),J=j("w",e[1].dataType,e[1].dims.length,V),Z=[M,J],W=re("result",e[0].dataType,n.length,V);if(d){let pe=j("bias",e[2].dataType,e[2].dims.length,V);Z.push(pe),H+=` - fn getBiasByOutputCoords(coords : vec4) -> ${b?`vec4<${G}>`:G} { - return bias[coords.${r?"w":"y"}${b?"/ 4":""}]; + setOutputAtIndex(flatIndex ${g?"/ 4":""}, value); + }`,F=L("x",e[0].dataType,e[0].dims.length,O===3?1:O),Q=L("w",e[1].dataType,e[1].dims.length,q),Y=[F,Q],j=ne("result",e[0].dataType,n.length,q);if(d){let ee=L("bias",e[2].dataType,e[2].dims.length,q);Y.push(ee),K+=` + fn getBiasByOutputCoords(coords : vec4) -> ${g?`vec4<${H}>`:H} { + return bias[coords.${r?"w":"y"}${g?"/ 4":""}]; }`}return` - ${w1("uniforms.result_strides")} + ${I1("uniforms.result_strides")} //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; - ${C.registerUniforms(F).declareVariables(...Z,W)} - ${H} - ${wg(r,B,U,X,d,t,q[0],q[1],q[2],G)} - ${b?ml(S,y,G,void 0,!r,O):gl(S,y,G,void 0,!r,O,!1,void 0,c)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${I};${b};${B};${U};${X};${A};${P};${O}`,inputDependencies:Q},getRunData:()=>({outputs:[{dims:s?s(n):n,dataType:e[0].dataType}],dispatchGroup:{x:T[0],y:T[1],z:T[2]},programUniforms:D}),getShaderSource:_}}}),_g,Ma,ii,vg,Fa,xg,v1,x1,DA=R(()=>{"use strict";se(),Qt(),ae(),ue(),cn(),Kl(),_g=e=>{let t=1;for(let n=0;ntypeof e=="number"?[e,e,e]:e,ii=(e,t)=>t<=1?e:e+(e-1)*(t-1),vg=(e,t,n,o=1)=>{let u=ii(t,o);return Math.floor((e[0]*(n-1)-n+u)/2)},Fa=(e,t,n,o,u)=>{u==null&&(u=vg(e,t[0],o[0]));let l=[0,0,0,n];for(let d=0;d<3;d++)e[d]+2*u>=t[d]&&(l[d]=Math.trunc((e[d]-t[d]+2*u)/o[d]+1));return l},xg=(e,t,n,o,u,l,d,c,s,r)=>{let i,a,p,h;if(e==="VALID"&&(e=0),typeof e=="number"){i={top:e,bottom:e,left:e,right:e,front:e,back:e};let m=Fa([t,n,o,1],[c,s,r],1,[u,l,d],e);a=m[0],p=m[1],h=m[2]}else if(Array.isArray(e)){if(!e.every((b,x,w)=>b===w[0]))throw Error(`Unsupported padding parameter: ${e}`);i={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let m=Fa([t,n,o,1],[c,s,r],1,[u,l,d],e[0]);a=m[0],p=m[1],h=m[2]}else if(e==="SAME_UPPER"){a=Math.ceil(t/u),p=Math.ceil(n/l),h=Math.ceil(o/d);let m=(a-1)*u+c-t,b=(p-1)*l+s-n,x=(h-1)*d+r-o,w=Math.floor(m/2),y=m-w,S=Math.floor(b/2),T=b-S,I=Math.floor(x/2),A=x-I;i={top:S,bottom:T,left:I,right:A,front:w,back:y}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:i,outDepth:a,outHeight:p,outWidth:h}},v1=(e,t,n,o,u,l=!1,d="channelsLast")=>{let c,s,r,i,a;if(d==="channelsLast")[c,s,r,i,a]=e;else if(d==="channelsFirst")[c,a,s,r,i]=e;else throw new Error(`Unknown dataFormat ${d}`);let[p,,h,m,b]=t,[x,w,y]=Ma(n),[S,T,I]=Ma(o),A=ii(h,S),P=ii(m,T),O=ii(b,I),{padInfo:B,outDepth:U,outHeight:X,outWidth:q}=xg(u,s,r,i,x,w,y,A,P,O),D=l?p*a:p,Q=[0,0,0,0,0];return d==="channelsFirst"?Q=[c,D,U,X,q]:d==="channelsLast"&&(Q=[c,U,X,q,D]),{batchSize:c,dataFormat:d,inDepth:s,inHeight:r,inWidth:i,inChannels:a,outDepth:U,outHeight:X,outWidth:q,outChannels:D,padInfo:B,strideDepth:x,strideHeight:w,strideWidth:y,filterDepth:h,filterHeight:m,filterWidth:b,effectiveFilterDepth:A,effectiveFilterHeight:P,effectiveFilterWidth:O,dilationDepth:S,dilationHeight:T,dilationWidth:I,inShape:e,outShape:Q,filterShape:t}},x1=(e,t,n,o,u,l)=>{let d=l==="channelsLast",c=d?e[0].dims[3]:e[0].dims[1],s=!1,r=[64,1,1],i={x:n.map((y,S)=>S)},a=[Math.ceil(_g(i.x.map(y=>n[y]))/r[0]),1,1];ve("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${a}`);let p=s?d&&c%4!==0?3:4:1,h=z.size(n),m=[{type:12,data:h},{type:12,data:o},{type:12,data:u},{type:12,data:t.strides},{type:12,data:t.dilations}];sn(t,m),m.push(...oe(e[0].dims,e[1].dims));let b=["rank","rank"],x=e.length===3;x&&(m.push(...oe(e[2].dims)),b.push("rank")),m.push(...oe(n));let w=y=>{let S=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:o.length},{name:"pads",type:"u32",length:u.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];an(t,S);let T=s?4:1,I=Ye(e[0].dataType),A=j("x",e[0].dataType,e[0].dims.length,p===3?1:p),P=j("W",e[1].dataType,e[1].dims.length,T),O=[A,P],B=re("result",e[0].dataType,n.length,T),U="";if(x){let D=j("bias",e[2].dataType,e[2].dims.length,T);O.push(D),U+=` - fn getBiasByOutputCoords(coords : array) -> ${s?`vec4<${I}>`:I} { - return bias[${d?ne("coords",4,5):ne("coords",1,5)}${s?"/ 4":""}]; - }`}let X=tt(p,I),q=on(t,X,I);return` - ${U} + ${D.registerUniforms(V).declareVariables(...Y,j)} + ${K} + ${Ig(r,R,M,X,d,t,G[0],G[1],G[2],H)} + ${g?wl(T,y,H,void 0,!r,I):_l(T,y,H,void 0,!r,I,!1,void 0,c)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${O};${g};${R};${M};${X};${A};${k};${I}`,inputDependencies:W},getRunData:()=>({outputs:[{dims:s?s(n):n,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:C}),getShaderSource:_}}}),Og,Ua,ci,Eg,Va,Ag,E1,A1,qA=N(()=>{"use strict";le(),ar(),pe(),he(),wn(),Jl(),Og=e=>{let t=1;for(let n=0;ntypeof e=="number"?[e,e,e]:e,ci=(e,t)=>t<=1?e:e+(e-1)*(t-1),Eg=(e,t,n,i=1)=>{let u=ci(t,i);return Math.floor((e[0]*(n-1)-n+u)/2)},Va=(e,t,n,i,u)=>{u==null&&(u=Eg(e,t[0],i[0]));let l=[0,0,0,n];for(let d=0;d<3;d++)e[d]+2*u>=t[d]&&(l[d]=Math.trunc((e[d]-t[d]+2*u)/i[d]+1));return l},Ag=(e,t,n,i,u,l,d,c,s,r)=>{let o,a,p,h;if(e==="VALID"&&(e=0),typeof e=="number"){o={top:e,bottom:e,left:e,right:e,front:e,back:e};let m=Va([t,n,i,1],[c,s,r],1,[u,l,d],e);a=m[0],p=m[1],h=m[2]}else if(Array.isArray(e)){if(!e.every((g,v,w)=>g===w[0]))throw Error(`Unsupported padding parameter: ${e}`);o={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let m=Va([t,n,i,1],[c,s,r],1,[u,l,d],e[0]);a=m[0],p=m[1],h=m[2]}else if(e==="SAME_UPPER"){a=Math.ceil(t/u),p=Math.ceil(n/l),h=Math.ceil(i/d);let m=(a-1)*u+c-t,g=(p-1)*l+s-n,v=(h-1)*d+r-i,w=Math.floor(m/2),y=m-w,T=Math.floor(g/2),S=g-T,O=Math.floor(v/2),A=v-O;o={top:T,bottom:S,left:O,right:A,front:w,back:y}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:o,outDepth:a,outHeight:p,outWidth:h}},E1=(e,t,n,i,u,l=!1,d="channelsLast")=>{let c,s,r,o,a;if(d==="channelsLast")[c,s,r,o,a]=e;else if(d==="channelsFirst")[c,a,s,r,o]=e;else throw new Error(`Unknown dataFormat ${d}`);let[p,,h,m,g]=t,[v,w,y]=Ua(n),[T,S,O]=Ua(i),A=ci(h,T),k=ci(m,S),I=ci(g,O),{padInfo:R,outDepth:M,outHeight:X,outWidth:G}=Ag(u,s,r,o,v,w,y,A,k,I),C=l?p*a:p,W=[0,0,0,0,0];return d==="channelsFirst"?W=[c,C,M,X,G]:d==="channelsLast"&&(W=[c,M,X,G,C]),{batchSize:c,dataFormat:d,inDepth:s,inHeight:r,inWidth:o,inChannels:a,outDepth:M,outHeight:X,outWidth:G,outChannels:C,padInfo:R,strideDepth:v,strideHeight:w,strideWidth:y,filterDepth:h,filterHeight:m,filterWidth:g,effectiveFilterDepth:A,effectiveFilterHeight:k,effectiveFilterWidth:I,dilationDepth:T,dilationHeight:S,dilationWidth:O,inShape:e,outShape:W,filterShape:t}},A1=(e,t,n,i,u,l)=>{let d=l==="channelsLast",c=d?e[0].dims[3]:e[0].dims[1],s=!1,r=[64,1,1],o={x:n.map((y,T)=>T)},a=[Math.ceil(Og(o.x.map(y=>n[y]))/r[0]),1,1];Ie("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${a}`);let p=s?d&&c%4!==0?3:4:1,h=B.size(n),m=[{type:12,data:h},{type:12,data:i},{type:12,data:u},{type:12,data:t.strides},{type:12,data:t.dilations}];fn(t,m),m.push(...ae(e[0].dims,e[1].dims));let g=["rank","rank"],v=e.length===3;v&&(m.push(...ae(e[2].dims)),g.push("rank")),m.push(...ae(n));let w=y=>{let T=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:i.length},{name:"pads",type:"u32",length:u.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];mn(t,T);let S=s?4:1,O=rt(e[0].dataType),A=L("x",e[0].dataType,e[0].dims.length,p===3?1:p),k=L("W",e[1].dataType,e[1].dims.length,S),I=[A,k],R=ne("result",e[0].dataType,n.length,S),M="";if(v){let C=L("bias",e[2].dataType,e[2].dims.length,S);I.push(C),M+=` + fn getBiasByOutputCoords(coords : array) -> ${s?`vec4<${O}>`:O} { + return bias[${d?oe("coords",4,5):oe("coords",1,5)}${s?"/ 4":""}]; + }`}let X=st(p,O),G=hn(t,X,O);return` + ${M} fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { let aIndices = array(d0, d1, d2, d3, d4); return ${A.getByIndices("aIndices")}; } fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { let aIndices = array(d0, d1, d2, d3, d4); - return ${P.getByIndices("aIndices")}; + return ${k.getByIndices("aIndices")}; } - ${y.registerUniforms(S).declareVariables(...O,B)} + ${y.registerUniforms(T).declareVariables(...I,R)} ${y.mainStart()} ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let coords = ${B.offsetToIndices("global_idx")}; - let batch = ${ne("coords",0,A.rank)}; - let d2 = ${d?ne("coords",A.rank-1,A.rank):ne("coords",1,A.rank)}; - let xFRCCorner = vec3(${d?ne("coords",1,A.rank):ne("coords",2,A.rank)}, - ${d?ne("coords",2,A.rank):ne("coords",3,A.rank)}, - ${d?ne("coords",3,A.rank):ne("coords",4,A.rank)}) * uniforms.strides - uniforms.pads; + let coords = ${R.offsetToIndices("global_idx")}; + let batch = ${oe("coords",0,A.rank)}; + let d2 = ${d?oe("coords",A.rank-1,A.rank):oe("coords",1,A.rank)}; + let xFRCCorner = vec3(${d?oe("coords",1,A.rank):oe("coords",2,A.rank)}, + ${d?oe("coords",2,A.rank):oe("coords",3,A.rank)}, + ${d?oe("coords",3,A.rank):oe("coords",4,A.rank)}) * uniforms.strides - uniforms.pads; let xFCorner = xFRCCorner.x; let xRCorner = xFRCCorner.y; let xCCorner = xFRCCorner.z; - let xShapeY = ${d?ne("uniforms.x_shape",1,A.rank):ne("uniforms.x_shape",2,A.rank)}; - let xShapeZ = ${d?ne("uniforms.x_shape",2,A.rank):ne("uniforms.x_shape",3,A.rank)}; - let xShapeW = ${d?ne("uniforms.x_shape",3,A.rank):ne("uniforms.x_shape",4,A.rank)}; - let xShapeU = ${d?ne("uniforms.x_shape",4,A.rank):ne("uniforms.x_shape",1,A.rank)}; + let xShapeY = ${d?oe("uniforms.x_shape",1,A.rank):oe("uniforms.x_shape",2,A.rank)}; + let xShapeZ = ${d?oe("uniforms.x_shape",2,A.rank):oe("uniforms.x_shape",3,A.rank)}; + let xShapeW = ${d?oe("uniforms.x_shape",3,A.rank):oe("uniforms.x_shape",4,A.rank)}; + let xShapeU = ${d?oe("uniforms.x_shape",4,A.rank):oe("uniforms.x_shape",1,A.rank)}; let inputDepthNearestVec4 = (xShapeU / 4) * 4; let inputDepthVec4Remainder = xShapeU % 4; @@ -2950,10 +2950,10 @@ fn main(@builtin(local_invocation_id) localId : vec3, } } } - ${x?"value = value + getBiasByOutputCoords(coords)":""}; - ${q} + ${v?"value = value + getBiasByOutputCoords(coords)":""}; + ${G} result[global_idx] = f32(value); - }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${d};${p};${x}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:a[0],y:a[1],z:a[2]},programUniforms:m}),getShaderSource:w}}}),$1,T1,RA=R(()=>{"use strict";se(),ae(),ue(),cn(),$1=(e,t,n,o)=>{let u=e.length>2,l=u?"value += b[output_channel];":"",d=e[0].dims,c=e[1].dims,s=t.format==="NHWC",r=s?n[3]:n[1],i=r/t.group,a=s&&i>=4?Ue(r):1,p=z.size(n)/a,h=[{type:12,data:p},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:i}];sn(t,h),h.push(...oe(d,[c[0],c[1],c[2],c[3]/a]));let m=u?["rank","rank","rank"]:["rank","rank"];h.push(...oe([n[0],n[1],n[2],n[3]/a]));let b=x=>{let w=re("output",e[0].dataType,n.length,a),y=Ye(w.type.tensor),S=on(t,w.type.value,y),T=j("x",e[0].dataType,d.length),I=j("w",e[1].dataType,c.length,a),A=[T,I];u&&A.push(j("b",e[2].dataType,e[2].dims,a));let P=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];an(t,P);let O=s?` + }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${d};${p};${v}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:a[0],y:a[1],z:a[2]},programUniforms:m}),getShaderSource:w}}}),k1,P1,HA=N(()=>{"use strict";le(),pe(),he(),wn(),k1=(e,t,n,i)=>{let u=e.length>2,l=u?"value += b[output_channel];":"",d=e[0].dims,c=e[1].dims,s=t.format==="NHWC",r=s?n[3]:n[1],o=r/t.group,a=s&&o>=4?qe(r):1,p=B.size(n)/a,h=[{type:12,data:p},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:o}];fn(t,h),h.push(...ae(d,[c[0],c[1],c[2],c[3]/a]));let m=u?["rank","rank","rank"]:["rank","rank"];h.push(...ae([n[0],n[1],n[2],n[3]/a]));let g=v=>{let w=ne("output",e[0].dataType,n.length,a),y=rt(w.type.tensor),T=hn(t,w.type.value,y),S=L("x",e[0].dataType,d.length),O=L("w",e[1].dataType,c.length,a),A=[S,O];u&&A.push(L("b",e[2].dataType,e[2].dims,a));let k=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];mn(t,k);let I=s?` for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; @@ -2969,8 +2969,8 @@ fn main(@builtin(local_invocation_id) localId : vec3, for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) { let input_channel = in_channel_offset + wInChannel; - let xVal = ${T.get("batch","xHeight","xWidth","input_channel")}; - let wVal = ${I.get("wHeight","wWidth","wInChannel","output_channel")}; + let xVal = ${S.get("batch","xHeight","xWidth","input_channel")}; + let wVal = ${O.get("wHeight","wWidth","wInChannel","output_channel")}; value += xVal * wVal; } } @@ -2991,17 +2991,17 @@ fn main(@builtin(local_invocation_id) localId : vec3, continue; } - let xVal = ${T.get("batch","input_channel","xHeight","xWidth")}; - let wVal = ${I.get("output_channel","wInChannel","wHeight","wWidth")}; + let xVal = ${S.get("batch","input_channel","xHeight","xWidth")}; + let wVal = ${O.get("output_channel","wInChannel","wHeight","wWidth")}; value += xVal * wVal; } } } `;return` - ${x.registerUniforms(P).declareVariables(...A,w)} + ${v.registerUniforms(k).declareVariables(...A,w)} - ${x.mainStart()} - ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let outputIndices = ${w.offsetToIndices("global_idx")}; let batch: u32 = outputIndices[0]; @@ -3011,12 +3011,12 @@ fn main(@builtin(local_invocation_id) localId : vec3, var in_channel_offset = group_id * uniforms.w_shape[${s?2:1}]; var value: ${w.type.value} = ${w.type.value}(0); - ${O} + ${I} ${l} - ${S} + ${T} ${w.setByOffset("global_idx","value")} - }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${a}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:o?o(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:b}},T1=(e,t,n,o)=>{let u=e.length>2,l=Ue(n[3]),d=Ue(n[2]),c=z.size(n)/l/d,s=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/l],r=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/l],i=[n[0],n[1],n[2],n[3]/l],a=[{type:12,data:c},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];sn(t,a),a.push(...oe(s,r,i));let p=(d-1)*t.strides[1]+r[1],h=m=>{let b=re("output",e[0].dataType,i.length,l),x=Ye(b.type.tensor),w=on(t,b.type.value,x),y=j("x",e[0].dataType,s.length,l),S=j("w",e[1].dataType,r.length,l),T=[y,S];u&&T.push(j("b",e[2].dataType,e[2].dims,l));let I=u?"value += b[output_channel];":"",A=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return an(t,A),` - ${m.registerUniforms(A).declareVariables(...T,b)} + }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${a}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:i?i(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:g}},P1=(e,t,n,i)=>{let u=e.length>2,l=qe(n[3]),d=qe(n[2]),c=B.size(n)/l/d,s=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/l],r=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/l],o=[n[0],n[1],n[2],n[3]/l],a=[{type:12,data:c},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];fn(t,a),a.push(...ae(s,r,o));let p=(d-1)*t.strides[1]+r[1],h=m=>{let g=ne("output",e[0].dataType,o.length,l),v=rt(g.type.tensor),w=hn(t,g.type.value,v),y=L("x",e[0].dataType,s.length,l),T=L("w",e[1].dataType,r.length,l),S=[y,T];u&&S.push(L("b",e[2].dataType,e[2].dims,l));let O=u?"value += b[output_channel];":"",A=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return mn(t,A),` + ${m.registerUniforms(A).declareVariables(...S,g)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let width0 = uniforms.output_shape[3]; @@ -3031,7 +3031,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; var x_vals: array<${y.type.value}, ${p}>; - var values: array<${b.type.value}, ${d}>; + var values: array<${g.type.value}, ${d}>; let input_channel = output_channel; // Use constant instead of uniform can give better performance for w's height/width. for (var w_height: u32 = 0u; w_height < ${r[0]}; w_height++) { @@ -3046,7 +3046,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, } } for (var w_width: u32 = 0u; w_width < ${r[1]}; w_width++) { - let w_val = ${S.get("w_height","w_width","0","output_channel")}; + let w_val = ${T.get("w_height","w_width","0","output_channel")}; for (var i = 0u; i < ${d}u; i++) { values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); } @@ -3056,38 +3056,38 @@ fn main(@builtin(local_invocation_id) localId : vec3, for (var i = 0u; i < ${d}u; i++) { var value = values[i]; - ${I} + ${O} ${w} - ${b.set("batch","row","col + i","output_channel","value")}; + ${g.set("batch","row","col + i","output_channel","value")}; } - }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${l};${d};${p};${r[0]};${r[1]}`,inputDependencies:u?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:o?o(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:a}),getShaderSource:h}}}),$g,ho,Tg,fo,bl,ja,Sg,Ig,yl,NA=R(()=>{"use strict";ae(),CA(),DA(),Jl(),RA(),cn(),Zl(),$r(),$g=(e,t,n,o,u,l)=>{let d=e[0],c=e.slice(l?1:2,l?3:4),s=c.length,r=t[0],i=t.slice(2).map((p,h)=>p+(p-1)*(n[h]-1)),a=c.map((p,h)=>p+o[h]+o[h+s]).map((p,h)=>Math.floor((p-i[h]+u[h])/u[h]));return a.splice(0,0,d),a.splice(l?3:1,0,r),a},ho=[2,3,1,0],Tg=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],o=e[1].dims[1]*t.group;if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},fo=(e,t)=>{let n=e.kernelShape.slice();n.length{let t=Wl(e),n=e.format,o=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],u=e.dilations,l=e.group,d=e.kernel_shape,c=e.pads,s=e.strides,r=e.w_is_const();return{autoPad:o,format:n,dilations:u,group:l,kernelShape:d,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},ja=(e,t,n,o)=>{let u=n.format==="NHWC",l=$g(t[0].dims,t[1].dims,n.dilations,n.pads,n.strides,u);if(n.group!==1){let A=[t[0]];if(u){let P=e.kernelCustomData.wT??e.compute(bt(t[1],ho),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=P),A.push(P)}else A.push(t[1]);t.length===3&&A.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&u&&t[1].dims[0]===n.group&&t[1].dims[1]===1&&n.dilations[0]===1&&n.dilations[1]===1?e.compute(T1(A,n,l,o),{inputs:A}):e.compute($1(A,n,l,o),{inputs:A});return}let d=t.length===3,c=t[0].dims[u?1:2],s=t[0].dims[u?2:3],r=t[0].dims[u?3:1],i=t[1].dims[2],a=t[1].dims[3],p=l[u?1:2],h=l[u?2:3],m=l[u?3:1],b=u&&i===c&&a===s&&n.pads[0]===0&&n.pads[1]===0;if(b||i===1&&a===1&&n.dilations[0]===1&&n.dilations[1]===1&&n.strides[0]===1&&n.strides[1]===1&&n.pads[0]===0&&n.pads[1]===0){let A=l[0],P,O,B,U=[];if(u){let D=e.kernelCustomData.wT??e.compute(bt(t[1],ho),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];if(n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=D),b){let Q=c*s*r;P=t[0].reshape([1,A,Q]),O=D.reshape([1,Q,m]),B=[1,A,m]}else P=t[0].reshape([A,c*s,r]),O=D.reshape([1,r,m]),B=[A,p*h,m];U.push(P),U.push(O)}else P=t[0].reshape([A,r,c*s]),O=t[1].reshape([1,m,r]),B=[A,m,p*h],U.push(O),U.push(P);d&&U.push(t[2]);let X=B[2],q=U[0].dims[U[0].dims.length-1];X<8&&q<8?e.compute(Xl(U,n,l,B,u,o),{inputs:U}):e.compute(Do(U,n,l,B,u,o),{inputs:U});return}let x=!0,w=e.kernelCustomData.wT??e.compute(bt(t[1],ho),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=w);let y=[t[0],w];d&&y.push(t[2]);let S=u?p*h:m,T=u?m:p*h,I=i*a*r;e.compute(_1(y,n,l,S,T,I,d,x,o),{inputs:y})},Sg=(e,t)=>{let n=t.format==="NHWC",o=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&o.push(e.inputs[2]);let u=[0,t.pads[0],0,t.pads[1]],l=[1].concat(t.strides),d=[1].concat(t.dilations),c=[1].concat(t.kernelShape),s=fo({...t,pads:u,strides:l,dilations:d,kernelShape:c},o);ja(e,o,s,r=>n?[r[0],r[2],r[3]]:[r[0],r[1],r[3]])},Ig=(e,t,n)=>{let o=n.format==="NHWC"?"channelsLast":"channelsFirst",u=fo(n,t),l=n.autoPad==="NOTSET"?n.pads:n.autoPad,d=v1(t[0].dims,t[1].dims,n.strides,n.dilations,l,!1,o);e.compute(x1(t,u,d.outShape,[d.filterDepth,d.filterHeight,d.filterWidth],[d.padInfo.front,d.padInfo.top,d.padInfo.left],o))},yl=(e,t)=>{if(Tg(e.inputs,t),e.inputs[0].dims.length===3)Sg(e,t);else if(e.inputs[0].dims.length===5)Ig(e,e.inputs,t);else{let n=fo(t,e.inputs);ja(e,e.inputs,n)}}}),S1,zA=R(()=>{"use strict";se(),Qt(),ae(),ue(),S1=(e,t,n)=>{let o=e.length>2,u=t.outputShape,l=t.format==="NHWC",d=t.group,c=e[1].dims,s=c[2]/d,r=c[3],i=l?Ue(s):1,a=l&&r===1&&s>=4,p=a?Math.floor(s/4)*4:Math.floor(s/i)*i,h=s-p,m=l?Ue(r):1,b=l?r===1?i:m:1,x=z.size(u)/m,w=[Math.ceil(x/64),1,1];ve("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${w}`);let y=["rank","rank"],S=[t.strides[0],t.strides[1]],T=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],I=[t.dilations[0],t.dilations[1]],A=[T[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),T[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],P=[A[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),A[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],O=[{type:12,data:x},{type:12,data:S},{type:12,data:T},{type:12,data:I},{type:12,data:A},{type:6,data:P},{type:12,data:p},{type:12,data:s},{type:12,data:r},...oe(e[0].dims,e[1].dims)];o&&(O.push(...oe(e[2].dims)),y.push("rank")),O.push(...oe(u));let B=U=>{let X=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:S.length},{name:"filter_dims",type:"u32",length:T.length},{name:"dilations",type:"u32",length:T.length},{name:"effective_filter_dims",type:"u32",length:A.length},{name:"pads",type:"i32",length:P.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],q=Ye(e[0].dataType),D=l?1:2,Q=l?2:3,_=l?3:1,C=j("W",e[1].dataType,e[1].dims.length,b),F=j("Dy",e[0].dataType,e[0].dims.length,i),V=[F,C];o&&V.push(j("bias",e[2].dataType,[u[_]].length,m));let G=re("result",e[0].dataType,u.length,m),H=()=>{let Z="";if(a)i===4?Z+=` - let xValue = ${F.getByOffset("x_offset")}; - let wValue = ${C.getByOffset("w_offset")}; + }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${l};${d};${p};${r[0]};${r[1]}`,inputDependencies:u?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:a}),getShaderSource:h}}}),kg,_o,Pg,vo,vl,Ga,Cg,Dg,xl,WA=N(()=>{"use strict";pe(),GA(),qA(),td(),HA(),wn(),ed(),Ar(),kg=(e,t,n,i,u,l)=>{let d=e[0],c=e.slice(l?1:2,l?3:4),s=c.length,r=t[0],o=t.slice(2).map((p,h)=>p+(p-1)*(n[h]-1)),a=c.map((p,h)=>p+i[h]+i[h+s]).map((p,h)=>Math.floor((p-o[h]+u[h])/u[h]));return a.splice(0,0,d),a.splice(l?3:1,0,r),a},_o=[2,3,1,0],Pg=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[1]*t.group;if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let u=e[0].dims.length-2;if(t.dilations.length!==u)throw new Error(`dilations should be ${u}D`);if(t.strides.length!==u)throw new Error(`strides should be ${u}D`);if(t.pads.length!==u*2)throw new Error(`pads should be ${u*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},vo=(e,t)=>{let n=e.kernelShape.slice();n.length{let t=Yl(e),n=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],u=e.dilations,l=e.group,d=e.kernel_shape,c=e.pads,s=e.strides,r=e.w_is_const();return{autoPad:i,format:n,dilations:u,group:l,kernelShape:d,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},Ga=(e,t,n,i)=>{let u=n.format==="NHWC",l=kg(t[0].dims,t[1].dims,n.dilations,n.pads,n.strides,u);if(n.group!==1){let A=[t[0]];if(u){let k=e.kernelCustomData.wT??e.compute(xt(t[1],_o),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=k),A.push(k)}else A.push(t[1]);t.length===3&&A.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&u&&t[1].dims[0]===n.group&&t[1].dims[1]===1&&n.dilations[0]===1&&n.dilations[1]===1?e.compute(P1(A,n,l,i),{inputs:A}):e.compute(k1(A,n,l,i),{inputs:A});return}let d=t.length===3,c=t[0].dims[u?1:2],s=t[0].dims[u?2:3],r=t[0].dims[u?3:1],o=t[1].dims[2],a=t[1].dims[3],p=l[u?1:2],h=l[u?2:3],m=l[u?3:1],g=u&&o===c&&a===s&&n.pads[0]===0&&n.pads[1]===0;if(g||o===1&&a===1&&n.dilations[0]===1&&n.dilations[1]===1&&n.strides[0]===1&&n.strides[1]===1&&n.pads[0]===0&&n.pads[1]===0){let A=l[0],k,I,R,M=[];if(u){let C=e.kernelCustomData.wT??e.compute(xt(t[1],_o),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];if(n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=C),g){let W=c*s*r;k=t[0].reshape([1,A,W]),I=C.reshape([1,W,m]),R=[1,A,m]}else k=t[0].reshape([A,c*s,r]),I=C.reshape([1,r,m]),R=[A,p*h,m];M.push(k),M.push(I)}else k=t[0].reshape([A,r,c*s]),I=t[1].reshape([1,m,r]),R=[A,m,p*h],M.push(I),M.push(k);d&&M.push(t[2]);let X=R[2],G=M[0].dims[M[0].dims.length-1];X<8&&G<8?e.compute(Ql(M,n,l,R,u,i),{inputs:M}):e.compute(jo(M,n,l,R,u,i),{inputs:M});return}let v=!0,w=e.kernelCustomData.wT??e.compute(xt(t[1],_o),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=w);let y=[t[0],w];d&&y.push(t[2]);let T=u?p*h:m,S=u?m:p*h,O=o*a*r;e.compute(O1(y,n,l,T,S,O,d,v,i),{inputs:y})},Cg=(e,t)=>{let n=t.format==="NHWC",i=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let u=[0,t.pads[0],0,t.pads[1]],l=[1].concat(t.strides),d=[1].concat(t.dilations),c=[1].concat(t.kernelShape),s=vo({...t,pads:u,strides:l,dilations:d,kernelShape:c},i);Ga(e,i,s,r=>n?[r[0],r[2],r[3]]:[r[0],r[1],r[3]])},Dg=(e,t,n)=>{let i=n.format==="NHWC"?"channelsLast":"channelsFirst",u=vo(n,t),l=n.autoPad==="NOTSET"?n.pads:n.autoPad,d=E1(t[0].dims,t[1].dims,n.strides,n.dilations,l,!1,i);e.compute(A1(t,u,d.outShape,[d.filterDepth,d.filterHeight,d.filterWidth],[d.padInfo.front,d.padInfo.top,d.padInfo.left],i))},xl=(e,t)=>{if(Pg(e.inputs,t),e.inputs[0].dims.length===3)Cg(e,t);else if(e.inputs[0].dims.length===5)Dg(e,e.inputs,t);else{let n=vo(t,e.inputs);Ga(e,e.inputs,n)}}}),C1,KA=N(()=>{"use strict";le(),ar(),pe(),he(),C1=(e,t,n)=>{let i=e.length>2,u=t.outputShape,l=t.format==="NHWC",d=t.group,c=e[1].dims,s=c[2]/d,r=c[3],o=l?qe(s):1,a=l&&r===1&&s>=4,p=a?Math.floor(s/4)*4:Math.floor(s/o)*o,h=s-p,m=l?qe(r):1,g=l?r===1?o:m:1,v=B.size(u)/m,w=[Math.ceil(v/64),1,1];Ie("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${w}`);let y=["rank","rank"],T=[t.strides[0],t.strides[1]],S=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],O=[t.dilations[0],t.dilations[1]],A=[S[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),S[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],k=[A[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),A[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],I=[{type:12,data:v},{type:12,data:T},{type:12,data:S},{type:12,data:O},{type:12,data:A},{type:6,data:k},{type:12,data:p},{type:12,data:s},{type:12,data:r},...ae(e[0].dims,e[1].dims)];i&&(I.push(...ae(e[2].dims)),y.push("rank")),I.push(...ae(u));let R=M=>{let X=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:T.length},{name:"filter_dims",type:"u32",length:S.length},{name:"dilations",type:"u32",length:S.length},{name:"effective_filter_dims",type:"u32",length:A.length},{name:"pads",type:"i32",length:k.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],G=rt(e[0].dataType),C=l?1:2,W=l?2:3,_=l?3:1,D=L("W",e[1].dataType,e[1].dims.length,g),V=L("Dy",e[0].dataType,e[0].dims.length,o),q=[V,D];i&&q.push(L("bias",e[2].dataType,[u[_]].length,m));let H=ne("result",e[0].dataType,u.length,m),K=()=>{let Y="";if(a)o===4?Y+=` + let xValue = ${V.getByOffset("x_offset")}; + let wValue = ${D.getByOffset("w_offset")}; dotProd = dotProd + dot(xValue, wValue); x_offset += 1u; - w_offset += 1u;`:i===2?Z+=` - dotProd = dotProd + dot(vec4<${q}>(${F.getByOffset("x_offset")}, ${F.getByOffset("x_offset + 1u")}), vec4<${q}>(${C.getByOffset("w_offset")}, ${C.getByOffset("w_offset + 1u")})); + w_offset += 1u;`:o===2?Y+=` + dotProd = dotProd + dot(vec4<${G}>(${V.getByOffset("x_offset")}, ${V.getByOffset("x_offset + 1u")}), vec4<${G}>(${D.getByOffset("w_offset")}, ${D.getByOffset("w_offset + 1u")})); x_offset += 2u; - w_offset += 2u;`:i===1&&(Z+=` - dotProd = dotProd + dot(vec4<${q}>(${F.getByOffset("x_offset")}, ${F.getByOffset("x_offset + 1u")}, ${F.getByOffset("x_offset + 2u")}, ${F.getByOffset("x_offset + 3u")}), vec4<${q}>(${C.getByOffset("w_offset")}, ${C.getByOffset("w_offset + 1u")}, ${C.getByOffset("w_offset + 2u")}, ${C.getByOffset("w_offset + 3u")})); + w_offset += 2u;`:o===1&&(Y+=` + dotProd = dotProd + dot(vec4<${G}>(${V.getByOffset("x_offset")}, ${V.getByOffset("x_offset + 1u")}, ${V.getByOffset("x_offset + 2u")}, ${V.getByOffset("x_offset + 3u")}), vec4<${G}>(${D.getByOffset("w_offset")}, ${D.getByOffset("w_offset + 1u")}, ${D.getByOffset("w_offset + 2u")}, ${D.getByOffset("w_offset + 3u")})); x_offset += 4u; - w_offset += 4u;`);else if(Z+=` - let xValue = ${l?F.getByOffset(`${F.indicesToOffset(`${F.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${i}`):F.get("batch","inputChannel","idyR","idyC")}; - `,i===1)Z+=` - let w_offset = ${C.indicesToOffset(`${C.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)}; - let wValue = ${C.getByOffset(`w_offset / ${b}`)}; - dotProd = dotProd + xValue * wValue;`;else for(let W=0;W{if(h===0)return"";if(!a)throw new Error(`packInputAs4 ${a} is not true.`);let Z="";if(i===1){Z+="dotProd = dotProd";for(let W=0;W{if(h===0)return"";if(!a)throw new Error(`packInputAs4 ${a} is not true.`);let Y="";if(o===1){Y+="dotProd = dotProd";for(let j=0;j(i32(r), i32(c)) - uniforms.pads; let dyRCorner = dyCorner.x; let dyCCorner = dyCorner.y; @@ -3095,7 +3095,7 @@ fn main(@builtin(local_invocation_id) localId : vec3, let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). // ? = to be determined. : = across all values in that axis. - var dotProd = ${G.type.value}(0.0); + var dotProd = ${H.type.value}(0.0); var wR: u32 = 0; if (uniforms.dilations.x == 1) { // Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0 @@ -3105,9 +3105,9 @@ fn main(@builtin(local_invocation_id) localId : vec3, if (wR % uniforms.dilations.x != 0) { continue; } - let dyR = (${q}(dyRCorner) + ${q}(wR)) / ${q}(uniforms.strides[0]); + let dyR = (${G}(dyRCorner) + ${G}(wR)) / ${G}(uniforms.strides[0]); let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; - if (dyR < 0.0 || dyR >= ${q}(uniforms.Dy_shape[${D}]) || fract(dyR) > 0.0 || + if (dyR < 0.0 || dyR >= ${G}(uniforms.Dy_shape[${C}]) || fract(dyR) > 0.0 || wRPerm < 0) { continue; } @@ -3121,66 +3121,66 @@ fn main(@builtin(local_invocation_id) localId : vec3, if (wC % uniforms.dilations.y != 0) { continue; } - let dyC = (${q}(dyCCorner) + ${q}(wC)) / ${q}(uniforms.strides.y); + let dyC = (${G}(dyCCorner) + ${G}(wC)) / ${G}(uniforms.strides.y); let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; - if (dyC < 0.0 || dyC >= ${q}(uniforms.Dy_shape[${Q}]) || + if (dyC < 0.0 || dyC >= ${G}(uniforms.Dy_shape[${W}]) || fract(dyC) > 0.0 || wCPerm < 0) { continue; } let idyC: u32 = u32(dyC); var inputChannel = groupId * uniforms.input_channels_per_group; ${a?` - var x_offset = ${F.indicesToOffset(`${F.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${i}; - var w_offset = ${C.indicesToOffset(`${C.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${b}; + var x_offset = ${V.indicesToOffset(`${V.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${o}; + var w_offset = ${D.indicesToOffset(`${D.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${g}; `:""} - for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${a?4:i}) { - ${H()} - inputChannel = inputChannel + ${a?4:i}; + for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${a?4:o}) { + ${K()} + inputChannel = inputChannel + ${a?4:o}; } - ${M()} + ${F()} wC = wC + uniforms.strides.y - 1; } wR = wR + uniforms.strides[0] - 1; } - let value = dotProd${o?` + bias[d1 / ${m}]`:""}; - ${G.setByOffset("global_idx","value")}; + let value = dotProd${i?` + bias[d1 / ${m}]`:""}; + ${H.setByOffset("global_idx","value")}; `;return` - ${U.registerUniforms(X).declareVariables(...V,G)} - ${U.mainStart()} - ${U.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; - ${J}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${i}${b}${m}${a}${h}`,inputDependencies:y},getRunData:()=>({dispatchGroup:{x:w[0],y:w[1],z:w[2]},outputs:[{dims:n?n(u):u,dataType:e[0].dataType}],programUniforms:O}),getShaderSource:B}}}),Og,Eg,Ag,La,I1,kg,Ua,Pg,O1,BA=R(()=>{"use strict";zA(),cn(),$r(),Og=(e,t,n,o,u,l)=>(e-1)*t+n+(o-1)*u+1-l,Eg=(e,t,n,o,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[o]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[o]=e-l,n[u]=l)},Ag=(e,t,n,o,u,l,d,c,s,r)=>{let i=e.length-2,a=r.length===0;s.length{let n=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((a,p)=>a*p,1)===0){n.length=0;for(let a=2;aa+p,0)===0){let a=t[0].dims.length-2;s=new Array(a).fill(1)}let r=e.strides.slice();if(r.reduce((a,p)=>a+p,0)===0){let a=t[0].dims.length-2;r=new Array(a).fill(1)}Ag(c,n,s,e.autoPad,e.group,u,r,o,d,l);let i=Object.assign({},e);return Object.assign(i,{kernelShape:n,pads:u,outputPadding:d,outputShape:l,dilations:s,strides:r}),i},I1=e=>{let t=Wl(e),n=e.format,o=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],u=e.dilations,l=e.group??1,d=e.kernelShape,c=e.pads,s=e.strides,r=e.wIsConst(),i=e.outputPadding,a=e.outputShape;return{autoPad:o,format:n,dilations:u,group:l,kernelShape:d,outputPadding:i,outputShape:a,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},kg=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],o=e[1].dims[0];if(n!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.reduce((d,c)=>d+c,0)>0&&t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.reduce((d,c)=>d+c,0)>0&&t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.reduce((d,c)=>d+c,0)>0&&t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.reduce((d,c)=>d+c,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Ua=(e,t,n,o)=>{let u=e.kernelCustomData.wT??e.compute(bt(t[1],[2,3,0,1]),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=u);let l=[t[0],u];t.length===3&&l.push(t[2]),e.compute(S1(l,n,o),{inputs:l})},Pg=(e,t)=>{let n=t.format==="NHWC",o=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&o.push(e.inputs[2]);let u=t.kernelShape;(u.length===0||u[0]===0)&&(u=[e.inputs[1].dims[2]]);let l=t.dilations;(l.length===0||l[0]===0)&&(l=[1]);let d=t.strides;(d.length===0||d[0]===0)&&(d=[1]);let c=t.pads;c.length===0&&(c=[0,0]),c=[0,c[0],0,c[1]],d=[1].concat(d),l=[1].concat(l),u=[1].concat(u);let s=t.outputPadding;s=[0].concat(s);let r=La({...t,pads:c,strides:d,dilations:l,kernelShape:u,outputPadding:s},o);Ua(e,o,r,i=>n?[i[0],i[2],i[3]]:[i[0],i[1],i[3]])},O1=(e,t)=>{if(kg(e.inputs,t),e.inputs[0].dims.length===3)Pg(e,t);else{let n=La(t,e.inputs);Ua(e,e.inputs,n)}}}),Cg,E1,A1,MA=R(()=>{"use strict";se(),ae(),Ve(),ue(),Cg=(e,t,n,o)=>{let u=z.size(t),l=t.length,d=j("input",e,l),c=re("output",e,l),s=n.dataType===6?n.getInt32Array()[0]:Number(n.getBigInt64Array()[0]),r=z.normalizeAxis(s,l),i=a=>{let p=` i32(${d.indicesGet("inputIndices","uniforms.axis")}) `,h=ne("uniforms.input_shape","uniforms.axis",l),m=o.reverse?p+(o.exclusive?" + 1":""):"0",b=o.reverse?h:p+(o.exclusive?"":" + 1");return` + ${M.registerUniforms(X).declareVariables(...q,H)} + ${M.mainStart()} + ${M.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; + ${Q}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${o}${g}${m}${a}${h}`,inputDependencies:y},getRunData:()=>({dispatchGroup:{x:w[0],y:w[1],z:w[2]},outputs:[{dims:n?n(u):u,dataType:e[0].dataType}],programUniforms:I}),getShaderSource:R}}}),Rg,Ng,zg,qa,D1,Mg,Ha,Bg,R1,XA=N(()=>{"use strict";KA(),wn(),Ar(),Rg=(e,t,n,i,u,l)=>(e-1)*t+n+(i-1)*u+1-l,Ng=(e,t,n,i,u)=>{let l=Math.floor(e/2);t==="SAME_UPPER"?(n[i]=l,n[u]=e-l):t==="SAME_LOWER"&&(n[i]=e-l,n[u]=l)},zg=(e,t,n,i,u,l,d,c,s,r)=>{let o=e.length-2,a=r.length===0;s.length{let n=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((a,p)=>a*p,1)===0){n.length=0;for(let a=2;aa+p,0)===0){let a=t[0].dims.length-2;s=new Array(a).fill(1)}let r=e.strides.slice();if(r.reduce((a,p)=>a+p,0)===0){let a=t[0].dims.length-2;r=new Array(a).fill(1)}zg(c,n,s,e.autoPad,e.group,u,r,i,d,l);let o=Object.assign({},e);return Object.assign(o,{kernelShape:n,pads:u,outputPadding:d,outputShape:l,dilations:s,strides:r}),o},D1=e=>{let t=Yl(e),n=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],u=e.dilations,l=e.group??1,d=e.kernelShape,c=e.pads,s=e.strides,r=e.wIsConst(),o=e.outputPadding,a=e.outputShape;return{autoPad:i,format:n,dilations:u,group:l,kernelShape:d,outputPadding:o,outputShape:a,pads:c,strides:s,wIsConst:r,...t,cacheKey:`${e.format};${t.activation};`}},Mg=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[0];if(n!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let u=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==u))throw new Error("invalid bias");let l=e[0].dims.length-2;if(t.dilations.reduce((d,c)=>d+c,0)>0&&t.dilations.length!==l)throw new Error(`dilations should be ${l}D`);if(t.strides.reduce((d,c)=>d+c,0)>0&&t.strides.length!==l)throw new Error(`strides should be ${l}D`);if(t.pads.reduce((d,c)=>d+c,0)>0&&t.pads.length!==l*2)throw new Error(`pads should be ${l*2}D`);if(t.outputPadding.length!==l&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${l}D`);if(t.kernelShape.reduce((d,c)=>d+c,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Ha=(e,t,n,i)=>{let u=e.kernelCustomData.wT??e.compute(xt(t[1],[2,3,0,1]),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=u);let l=[t[0],u];t.length===3&&l.push(t[2]),e.compute(C1(l,n,i),{inputs:l})},Bg=(e,t)=>{let n=t.format==="NHWC",i=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let u=t.kernelShape;(u.length===0||u[0]===0)&&(u=[e.inputs[1].dims[2]]);let l=t.dilations;(l.length===0||l[0]===0)&&(l=[1]);let d=t.strides;(d.length===0||d[0]===0)&&(d=[1]);let c=t.pads;c.length===0&&(c=[0,0]),c=[0,c[0],0,c[1]],d=[1].concat(d),l=[1].concat(l),u=[1].concat(u);let s=t.outputPadding;s=[0].concat(s);let r=qa({...t,pads:c,strides:d,dilations:l,kernelShape:u,outputPadding:s},i);Ha(e,i,r,o=>n?[o[0],o[2],o[3]]:[o[0],o[1],o[3]])},R1=(e,t)=>{if(Mg(e.inputs,t),e.inputs[0].dims.length===3)Bg(e,t);else{let n=qa(t,e.inputs);Ha(e,e.inputs,n)}}}),Fg,N1,z1,ZA=N(()=>{"use strict";le(),pe(),He(),he(),Fg=(e,t,n,i)=>{let u=B.size(t),l=t.length,d=L("input",e,l),c=ne("output",e,l),s=n.dataType===6?n.getInt32Array()[0]:Number(n.getBigInt64Array()[0]),r=B.normalizeAxis(s,l),o=a=>{let p=` i32(${d.indicesGet("inputIndices","uniforms.axis")}) `,h=oe("uniforms.input_shape","uniforms.axis",l),m=i.reverse?p+(i.exclusive?" + 1":""):"0",g=i.reverse?h:p+(i.exclusive?"":" + 1");return` ${a.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(d,c)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} var inputIndices = ${c.offsetToIndices("global_idx")}; var sum = ${c.type.value}(0); let first : i32 = ${m}; - let last : i32 = ${b}; + let last : i32 = ${g}; for (var i : i32 = first; i < last; i++) { ${d.indicesSet("inputIndices","uniforms.axis","u32(i)")}; sum = sum + ${d.getByIndices("inputIndices")}; } ${c.setByOffset("global_idx","sum")}; - }`};return{name:"CumSum",shaderCache:{hint:o.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:[{type:12,data:u},{type:12,data:r},...oe(t,t)]}),getShaderSource:i}},E1=(e,t)=>{let n=e.inputs[0].dims,o=e.inputs[0].dataType,u=e.inputs[1];e.compute(Cg(o,n,u,t),{inputs:[0]})},A1=e=>{let t=e.exclusive===1,n=e.reverse===1;return Se({exclusive:t,reverse:n})}}),Dg,Rg,Ng,k1,P1,FA=R(()=>{"use strict";se(),ae(),Ve(),ue(),Dg=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},Rg=(e,t,n,o)=>{let u=[];u.push(`fn perm(i: ${o.type.indices}) -> ${n.type.indices} { + }`};return{name:"CumSum",shaderCache:{hint:i.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:[{type:12,data:u},{type:12,data:r},...ae(t,t)]}),getShaderSource:o}},N1=(e,t)=>{let n=e.inputs[0].dims,i=e.inputs[0].dataType,u=e.inputs[1];e.compute(Fg(i,n,u,t),{inputs:[0]})},z1=e=>{let t=e.exclusive===1,n=e.reverse===1;return ke({exclusive:t,reverse:n})}}),jg,Lg,Ug,M1,B1,YA=N(()=>{"use strict";le(),pe(),He(),he(),jg=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},Lg=(e,t,n,i)=>{let u=[];u.push(`fn perm(i: ${i.type.indices}) -> ${n.type.indices} { var a: ${n.type.indices};`);for(let l=0;l{let n,o,u,l,d,c,s=t.format==="NHWC",r=t.blocksize,i=t.mode==="DCR";s?([n,o,u,l]=e.dims,d=i?[n,o,u,r,r,l/r**2]:[n,o,u,l/r**2,r,r],c=i?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([n,o,u,l]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],d=i?[n,r,r,l/r**2,o,u]:[n,l/r**2,r,r,o,u],c=i?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let a=e.reshape(d),p=a.dims.length,h=e.dataType,m=j("a",h,p),b=re("output",h,p),x=w=>` - ${w.registerUniform("output_size","u32").declareVariables(m,b)} +`)},Ug=(e,t)=>{let n,i,u,l,d,c,s=t.format==="NHWC",r=t.blocksize,o=t.mode==="DCR";s?([n,i,u,l]=e.dims,d=o?[n,i,u,r,r,l/r**2]:[n,i,u,l/r**2,r,r],c=o?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([n,i,u,l]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],d=o?[n,r,r,l/r**2,i,u]:[n,l/r**2,r,r,i,u],c=o?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let a=e.reshape(d),p=a.dims.length,h=e.dataType,m=L("a",h,p),g=ne("output",h,p),v=w=>` + ${w.registerUniform("output_size","u32").declareVariables(m,g)} - ${Rg(c,p,m,b)} + ${Lg(c,p,m,g)} ${w.mainStart()} ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let indices = ${b.offsetToIndices("global_idx")}; + let indices = ${g.offsetToIndices("global_idx")}; let aIndices = perm(indices); - ${b.setByOffset("global_idx",m.getByIndices("aIndices"))} - }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:w=>{let y=s?[n,o*r,u*r,l/r**2]:[n,l/r**2,o*r,u*r],S=z.size(y),T=a.dims,I=z.sortBasedOnPerm(T,c);return{outputs:[{dims:y,dataType:w[0].dataType}],dispatchGroup:{x:Math.ceil(S/64)},programUniforms:[{type:12,data:S},...oe(T,I)]}},getShaderSource:x}},k1=(e,t)=>{Dg(e.inputs),e.compute(Ng(e.inputs[0],t))},P1=e=>Se({blocksize:e.blocksize,mode:e.mode,format:e.format})}),Xt,oi,mo,Va,dr,zg,Bg,Mg,Ga,qa,Ha,Fg,jg,Wa,Lg,C1,D1,jA=R(()=>{"use strict";se(),ae(),Ve(),ue(),Xt=256,oi=512,mo=2*Math.PI,Va=e=>{let t=[],n=e;for(let o of[4,2,3,5])for(;n%o===0;)t.push(o),n/=o;return n===1?t:void 0},dr=e=>{let t=e.toPrecision(9);return/[.eE]/.test(t)?t:`${t}.0`},zg=(e,t,n,o,u)=>{let l=n/e,d=oi-o,c=r=>`smem[${d}u + base + ${r*t}u]`,s=` for (var t = local_idx; t < ${l}u; t += ${Xt}u) { + ${g.setByOffset("global_idx",m.getByIndices("aIndices"))} + }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:w=>{let y=s?[n,i*r,u*r,l/r**2]:[n,l/r**2,i*r,u*r],T=B.size(y),S=a.dims,O=B.sortBasedOnPerm(S,c);return{outputs:[{dims:y,dataType:w[0].dataType}],dispatchGroup:{x:Math.ceil(T/64)},programUniforms:[{type:12,data:T},...ae(S,O)]}},getShaderSource:v}},M1=(e,t)=>{jg(e.inputs),e.compute(Ug(e.inputs[0],t))},B1=e=>ke({blocksize:e.blocksize,mode:e.mode,format:e.format})}),nr,pi,xo,Wa,gr,Vg,Gg,qg,Ka,Xa,Za,Hg,Wg,Ya,Kg,F1,j1,JA=N(()=>{"use strict";le(),pe(),He(),he(),nr=256,pi=512,xo=2*Math.PI,Wa=e=>{let t=[],n=e;for(let i of[4,2,3,5])for(;n%i===0;)t.push(i),n/=i;return n===1?t:void 0},gr=e=>{let t=e.toPrecision(9);return/[.eE]/.test(t)?t:`${t}.0`},Vg=(e,t,n,i,u)=>{let l=n/e,d=pi-i,c=r=>`smem[${d}u + base + ${r*t}u]`,s=` for (var t = local_idx; t < ${l}u; t += ${nr}u) { `;s+=` let twiddleIndex = t % ${t}u; let angleUnit = f32(twiddleIndex); `,s+=` var leg: array, 5>; -`;for(let r=0;r(cos(a), sin(a))); } +`;for(let r=0;r(cos(a), sin(a))); } `}}if(s+=` let base = (t / ${t}u) * ${t*e}u + twiddleIndex; `,e===2)s+=` ${c(0)} = leg[0] + leg[1]; ${c(1)} = leg[0] - leg[1]; @@ -3191,45 +3191,45 @@ fn main(@builtin(local_invocation_id) localId : vec3, ${c(1)} = evenDiff + oddRot; `,s+=` ${c(2)} = evenSum - oddSum; ${c(3)} = evenDiff - oddRot; -`}else for(let r=0;r(leg[${a}].x*${h} - leg[${a}].y*${m}, leg[${a}].x*${m} + leg[${a}].y*${h})`)}s+=` ${c(r)} = ${i.join(" + ")}; +`}else for(let r=0;r(leg[${a}].x*${h} - leg[${a}].y*${m}, leg[${a}].x*${m} + leg[${a}].y*${h})`)}s+=` ${c(r)} = ${o.join(" + ")}; `}return`${s} } workgroupBarrier(); -`},Bg=(e,t,n)=>{let o="",u=1,l=0;for(let d of e)o+=zg(d,u,t,l,n),u*=d,l=oi-l;return{code:o,resultOffset:l}},Mg=(e,t,n,o,u)=>{let l=e.dims,d=l.length,c=l[d-1],s=l[t],r=n&&o?(s-1)*2:s;u!==void 0&&(r=u);let i=n&&o?1:2,a=o&&!n?Math.floor(r/2)+1:r,p=l.slice();p[t]=a,p[d-1]=i;let h=1;for(let b=t+1;b[t,e.length,e.inputComponents,e.outputComponents,e.inverse,e.onesided].join(";"),qa=e=>[{type:12,data:e.batch},{type:12,data:e.signalLength},{type:12,data:e.inner},{type:12,data:e.outputLength}],Ha=(e,t,n)=>e.registerUniform("batch","u32").registerUniform("signalLength","u32").registerUniform("inner","u32").registerUniform("outputLength","u32").declareVariables(t,n),Fg=e=>{let{dataType:t,length:n,inputComponents:o,outputComponents:u,inverse:l,onesided:d}=e,c=Je(t),s=l?1:-1,r=l?1/n:1,i=Va(n),a=p=>{let h=j("x",t,[1]),m=re("y",t,[1]),b=I=>{let A=`inBase + (${I}) * uniforms.inner * ${o}u`,P=`f32(${h.getByOffset(A)})`,O=o===2?`f32(${h.getByOffset(`${A} + 1u`)})`:"0.0";return`vec2(${P}, ${O})`},x;if(l&&d){let I=Math.floor(n/2)+1,A=n%2===0?`select(provided, provided - 1u, provided == ${I}u)`:"provided";x=` - let provided = min(uniforms.signalLength, ${I}u); - for (var i = local_idx; i < ${n}u; i += ${Xt}u) { - if (i < provided) { smem[i] = ${b("i")}; } else { smem[i] = vec2(0.0); } +`},Gg=(e,t,n)=>{let i="",u=1,l=0;for(let d of e)i+=Vg(d,u,t,l,n),u*=d,l=pi-l;return{code:i,resultOffset:l}},qg=(e,t,n,i,u)=>{let l=e.dims,d=l.length,c=l[d-1],s=l[t],r=n&&i?(s-1)*2:s;u!==void 0&&(r=u);let o=n&&i?1:2,a=i&&!n?Math.floor(r/2)+1:r,p=l.slice();p[t]=a,p[d-1]=o;let h=1;for(let g=t+1;g[t,e.length,e.inputComponents,e.outputComponents,e.inverse,e.onesided].join(";"),Xa=e=>[{type:12,data:e.batch},{type:12,data:e.signalLength},{type:12,data:e.inner},{type:12,data:e.outputLength}],Za=(e,t,n)=>e.registerUniform("batch","u32").registerUniform("signalLength","u32").registerUniform("inner","u32").registerUniform("outputLength","u32").declareVariables(t,n),Hg=e=>{let{dataType:t,length:n,inputComponents:i,outputComponents:u,inverse:l,onesided:d}=e,c=tt(t),s=l?1:-1,r=l?1/n:1,o=Wa(n),a=p=>{let h=L("x",t,[1]),m=ne("y",t,[1]),g=O=>{let A=`inBase + (${O}) * uniforms.inner * ${i}u`,k=`f32(${h.getByOffset(A)})`,I=i===2?`f32(${h.getByOffset(`${A} + 1u`)})`:"0.0";return`vec2(${k}, ${I})`},v;if(l&&d){let O=Math.floor(n/2)+1,A=n%2===0?`select(provided, provided - 1u, provided == ${O}u)`:"provided";v=` + let provided = min(uniforms.signalLength, ${O}u); + for (var i = local_idx; i < ${n}u; i += ${nr}u) { + if (i < provided) { smem[i] = ${g("i")}; } else { smem[i] = vec2(0.0); } } workgroupBarrier(); - for (var k = local_idx + 1u; k < ${A}; k += ${Xt}u) { + for (var k = local_idx + 1u; k < ${A}; k += ${nr}u) { let h = smem[k]; smem[${n}u - k] = vec2(h.x, -h.y); } - workgroupBarrier();`}else x=` + workgroupBarrier();`}else v=` let loadCount = min(uniforms.signalLength, ${n}u); - for (var i = local_idx; i < ${n}u; i += ${Xt}u) { - if (i < loadCount) { smem[i] = ${b("i")}; } else { smem[i] = vec2(0.0); } + for (var i = local_idx; i < ${n}u; i += ${nr}u) { + if (i < loadCount) { smem[i] = ${g("i")}; } else { smem[i] = vec2(0.0); } } - workgroupBarrier();`;let{code:w,resultOffset:y}=Bg(i,n,s),S=r===1?`smem[${y}u + i]`:`smem[${y}u + i] * ${dr(r)}`,T=u===2?m.setByOffset("off + 1u",`${c}(v.y)`):"";return` - ${Ha(p,h,m)} - var smem: array, ${2*oi}>; + workgroupBarrier();`;let{code:w,resultOffset:y}=Gg(o,n,s),T=r===1?`smem[${y}u + i]`:`smem[${y}u + i] * ${gr(r)}`,S=u===2?m.setByOffset("off + 1u",`${c}(v.y)`):"";return` + ${Za(p,h,m)} + var smem: array, ${2*pi}>; fn cmul(a: vec2, b: vec2) -> vec2 { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } - ${p.mainStart(Xt)} + ${p.mainStart(nr)} let row = workgroup_index; if (row >= uniforms.batch) { return; } let outer = row / uniforms.inner; let within = row % uniforms.inner; - let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${o}u; + let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${i}u; let outBase = (outer * uniforms.outputLength * uniforms.inner + within) * ${u}u; - ${x} -${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { - let v = ${S}; + ${v} +${w} for (var i = local_idx; i < uniforms.outputLength; i += ${nr}u) { + let v = ${T}; let off = outBase + i * uniforms.inner * ${u}u; ${m.setByOffset("off",`${c}(v.x)`)} - ${T} + ${S} } - }`};return{name:"DFT",shaderCache:{hint:Ga(e,"fft"),inputDependencies:["type"]},getShaderSource:a,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:qa(e),dispatchGroup:{x:e.batch}})}},jg=e=>{let{dataType:t,length:n,inputComponents:o,outputComponents:u,inverse:l,onesided:d}=e,c=Je(t),s=l?1:-1,r=l?1/n:1,i=a=>{let p=j("x",t,[1]),h=re("y",t,[1]),m=S=>{let T=`inBase + (${S}) * uniforms.inner * ${o}u`,I=`f32(${p.getByOffset(T)})`,A=o===2?`f32(${p.getByOffset(`${T} + 1u`)})`:"0.0";return`vec2(${I}, ${A})`},b=l&&d?`fn spectrum(inBase: u32, k: u32) -> vec2 { + }`};return{name:"DFT",shaderCache:{hint:Ka(e,"fft"),inputDependencies:["type"]},getShaderSource:a,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:Xa(e),dispatchGroup:{x:e.batch}})}},Wg=e=>{let{dataType:t,length:n,inputComponents:i,outputComponents:u,inverse:l,onesided:d}=e,c=tt(t),s=l?1:-1,r=l?1/n:1,o=a=>{let p=L("x",t,[1]),h=ne("y",t,[1]),m=T=>{let S=`inBase + (${T}) * uniforms.inner * ${i}u`,O=`f32(${p.getByOffset(S)})`,A=i===2?`f32(${p.getByOffset(`${S} + 1u`)})`:"0.0";return`vec2(${O}, ${A})`},g=l&&d?`fn spectrum(inBase: u32, k: u32) -> vec2 { let provided = min(uniforms.signalLength, ${Math.floor(n/2)+1}u); if (k < provided) { return ${m("k")}; } let m = ${n}u - k; @@ -3241,57 +3241,57 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { }`:`fn spectrum(inBase: u32, n: u32) -> vec2 { if (n < uniforms.signalLength) { return ${m("n")}; } return vec2(0.0, 0.0); - }`,x=` - let angle = ${dr(s*mo)} * f32(knMod) / ${dr(n)}; + }`,v=` + let angle = ${gr(s*xo)} * f32(knMod) / ${gr(n)}; acc += cmul(spectrum(inBase, n), vec2(cos(angle), sin(angle))); knMod += k; - if (knMod >= ${n}u) { knMod -= ${n}u; }`,w=u===2?h.setByOffset("off + 1u",`${c}(v.y)`):"",y=r===1?"acc":`acc * ${dr(r)}`;return` - ${Ha(a,p,h)} + if (knMod >= ${n}u) { knMod -= ${n}u; }`,w=u===2?h.setByOffset("off + 1u",`${c}(v.y)`):"",y=r===1?"acc":`acc * ${gr(r)}`;return` + ${Za(a,p,h)} fn cmul(a: vec2, b: vec2) -> vec2 { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } - ${b} - ${a.mainStart(Xt)} + ${g} + ${a.mainStart(nr)} let row = workgroup_index; if (row >= uniforms.batch) { return; } let outer = row / uniforms.inner; let within = row % uniforms.inner; - let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${o}u; + let inBase = (outer * uniforms.signalLength * uniforms.inner + within) * ${i}u; let outBase = (outer * uniforms.outputLength * uniforms.inner + within) * ${u}u; - for (var k = local_idx; k < uniforms.outputLength; k += ${Xt}u) { + for (var k = local_idx; k < uniforms.outputLength; k += ${nr}u) { var acc = vec2(0.0, 0.0); var knMod = 0u; - for (var n = 0u; n < ${n}u; n++) {${x} + for (var n = 0u; n < ${n}u; n++) {${v} } let v = ${y}; let off = outBase + k * uniforms.inner * ${u}u; ${h.setByOffset("off",`${c}(v.x)`)} ${w} } - }`};return{name:"DFT",shaderCache:{hint:Ga(e,"direct"),inputDependencies:["type"]},getShaderSource:i,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:qa(e),dispatchGroup:{x:e.batch}})}},Wa=e=>{if(!e||e.dataType===0)return;if(z.size(e.dims)!==1)throw new Error("DFT optional scalar inputs must have exactly 1 element.");if(e.dataType===6)return e.getInt32Array()[0];let t=Number(e.getBigInt64Array()[0]);if(!Number.isSafeInteger(t))throw new Error("DFT optional scalar inputs are out of JavaScript safe integer range.");return t},Lg=e=>{if(!e||e.length<1)throw new Error("DFT requires at least 1 input.");let t=e[0].dims;if(t.length<2)throw new Error("DFT input must have at least 2 dimensions.");let n=t[t.length-1];if(n!==1&&n!==2)throw new Error("DFT input's innermost dimension must be 1 (real) or 2 (complex).")},C1=(e,t)=>{Lg(e.inputs);let n=e.inputs[0],o=n.dims.length,u=t.inverse!==0,l=t.onesided!==0,d=Wa(e.inputs[1]);if(d!==void 0&&d<=0)throw new Error("dft_length must be greater than zero.");let c=z.normalizeAxis(Wa(e.inputs[2])??t.axis,o);if(c===o-1)throw new Error("DFT axis must refer to a signal dimension, not the innermost (real/imaginary) dimension.");if(u&&l&&n.dims[o-1]!==2)throw new Error("Inverse one-sided DFT (IRFFT) requires complex-valued input (innermost dimension 2).");let s=Mg(n,c,u,l,d);if(s.length<=0)throw new Error(`Invalid DFT length: ${s.length}`);let r=s.length<=oi&&Va(s.length)!==void 0?Fg(s):jg(s);e.compute(r,{inputs:[0]})},D1=e=>Se({axis:e.axis??1,inverse:e.inverse??0,onesided:e.onesided??0})}),go,si,Ka,Ug,Vg,Gg,qg,Xa,Hg,R1,N1,LA=R(()=>{"use strict";se(),ae(),Ve(),ue(),go="[a-zA-Z]|\\.\\.\\.",si="("+go+")+",Ka="^"+si+"$",Ug="("+si+",)*"+si,Vg="^"+Ug+"$",Gg=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let n=this.symbolToIndices.get(e);n===void 0?n=[t]:n.push(t),this.symbolToIndices.set(e,n)}},qg=class{constructor(e,t){this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,o]=t.includes("->")?t.split("->",2):[t,""];if(!n.match(RegExp(Vg)))throw new Error("Invalid LHS term");if(n.split(",").forEach((u,l)=>{let d=e[l].dims.slice();if(!u.match(RegExp(Ka)))throw new Error("Invalid LHS term");let c=this.processTerm(u,!0,d,l);this.lhs.push(c)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([u,l])=>l.count===1||u==="...").map(([u])=>u).join("");else if(!o.match(RegExp(si)))throw new Error("Invalid RHS");o.match(RegExp(go,"g"))?.forEach(u=>{if(u==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let l=this.symbolToInfo.get(u);if(l===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(l.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(e,t,n){let o=this.symbolToInfo.get(e);if(o!==void 0){if(o.dimValue!==t&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(n)}else o={count:1,dimValue:t,inputIndices:[n]};this.symbolToInfo.set(e,o)}processTerm(e,t,n,o=-1){let u=n.length,l=!1,d=[],c=0;if(!e.match(RegExp(Ka))&&!t&&e!=="")throw new Error("Invalid LHS term");let s=e.match(RegExp(go,"g")),r=new Gg(o);return s?.forEach((i,a)=>{if(i==="..."){if(l)throw new Error("Only one ellipsis is allowed per input term");l=!0;let p=u-s.length+1;if(p<0)throw new Error("Ellipsis out of bounds");if(d=n.slice(c,c+p),this.hasEllipsis){if(this.ellipsisDims.length!==d.length||this.ellipsisDims.toString()!==d.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=d;else throw new Error("Ellipsis must be specified in the LHS");for(let h=0;he+"_max",Hg=(e,t,n,o)=>{let u=e.map(r=>r.length).map((r,i)=>j(`input${i}`,t,r)),l=z.size(o),d=re("output",t,o.length),c=[...n.symbolToInfo.keys()].filter(r=>!n.rhs.symbolToIndices.has(r)),s=r=>{let i=[],a="var prod = 1.0;",p="var sum = 0.0;",h="sum += prod;",m=[],b=[],x=[],w=[],y=n.symbolToInfo.size===n.rhs.symbolToIndices.size;n.symbolToInfo.forEach((T,I)=>{if(n.rhs.symbolToIndices.has(I)){let A=n.rhs.symbolToIndices.get(I)?.[0];A!==void 0&&n.lhs.forEach((P,O)=>{if(T.inputIndices.includes(O)){let B=P.symbolToIndices.get(I);if(B===void 0)throw new Error("Invalid symbol error");B.forEach(U=>{i.push(`${u[O].indicesSet(`input${O}Indices`,U,d.indicesGet("outputIndices",A))}`)})}})}else n.lhs.forEach((A,P)=>{if(T.inputIndices.includes(P)){let O=A.symbolToIndices.get(I);if(O===void 0)throw new Error("Invalid symbol error");O.forEach(B=>{m.push(`${u[P].indicesSet(`input${P}Indices`,B,`${I}`)}`)}),w.push(`prod *= ${u[P].getByIndices(`input${P}Indices`)};`)}}),b.push(`for(var ${I}: u32 = 0; ${I} < uniforms.${Xa(I)}; ${I}++) {`),x.push("}")});let S=y?[...i,`let sum = ${u.map((T,I)=>T.getByIndices(`input${I}Indices`)).join(" * ")};`]:[...i,p,...b,...m,a,...w,h,...x];return` - ${r.registerUniforms(c.map(T=>({name:`${Xa(T)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...u,d)} + }`};return{name:"DFT",shaderCache:{hint:Ka(e,"direct"),inputDependencies:["type"]},getShaderSource:o,getRunData:()=>({outputs:[{dims:e.outputDims,dataType:t}],programUniforms:Xa(e),dispatchGroup:{x:e.batch}})}},Ya=e=>{if(!e||e.dataType===0)return;if(B.size(e.dims)!==1)throw new Error("DFT optional scalar inputs must have exactly 1 element.");if(e.dataType===6)return e.getInt32Array()[0];let t=Number(e.getBigInt64Array()[0]);if(!Number.isSafeInteger(t))throw new Error("DFT optional scalar inputs are out of JavaScript safe integer range.");return t},Kg=e=>{if(!e||e.length<1)throw new Error("DFT requires at least 1 input.");let t=e[0].dims;if(t.length<2)throw new Error("DFT input must have at least 2 dimensions.");let n=t[t.length-1];if(n!==1&&n!==2)throw new Error("DFT input's innermost dimension must be 1 (real) or 2 (complex).")},F1=(e,t)=>{Kg(e.inputs);let n=e.inputs[0],i=n.dims.length,u=t.inverse!==0,l=t.onesided!==0,d=Ya(e.inputs[1]);if(d!==void 0&&d<=0)throw new Error("dft_length must be greater than zero.");let c=B.normalizeAxis(Ya(e.inputs[2])??t.axis,i);if(c===i-1)throw new Error("DFT axis must refer to a signal dimension, not the innermost (real/imaginary) dimension.");if(u&&l&&n.dims[i-1]!==2)throw new Error("Inverse one-sided DFT (IRFFT) requires complex-valued input (innermost dimension 2).");let s=qg(n,c,u,l,d);if(s.length<=0)throw new Error(`Invalid DFT length: ${s.length}`);let r=s.length<=pi&&Wa(s.length)!==void 0?Hg(s):Wg(s);e.compute(r,{inputs:[0]})},j1=e=>ke({axis:e.axis??1,inverse:e.inverse??0,onesided:e.onesided??0})}),$o,hi,Ja,Xg,Zg,Yg,Jg,Qa,Qg,L1,U1,QA=N(()=>{"use strict";le(),pe(),He(),he(),$o="[a-zA-Z]|\\.\\.\\.",hi="("+$o+")+",Ja="^"+hi+"$",Xg="("+hi+",)*"+hi,Zg="^"+Xg+"$",Yg=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let n=this.symbolToIndices.get(e);n===void 0?n=[t]:n.push(t),this.symbolToIndices.set(e,n)}},Jg=class{constructor(e,t){this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,i]=t.includes("->")?t.split("->",2):[t,""];if(!n.match(RegExp(Zg)))throw new Error("Invalid LHS term");if(n.split(",").forEach((u,l)=>{let d=e[l].dims.slice();if(!u.match(RegExp(Ja)))throw new Error("Invalid LHS term");let c=this.processTerm(u,!0,d,l);this.lhs.push(c)}),i==="")i+=[...this.symbolToInfo.entries()].filter(([u,l])=>l.count===1||u==="...").map(([u])=>u).join("");else if(!i.match(RegExp(hi)))throw new Error("Invalid RHS");i.match(RegExp($o,"g"))?.forEach(u=>{if(u==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let l=this.symbolToInfo.get(u);if(l===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(l.dimValue)}}),this.rhs=this.processTerm(i,!1,this.outputDims)}addSymbol(e,t,n){let i=this.symbolToInfo.get(e);if(i!==void 0){if(i.dimValue!==t&&i.count!==1)throw new Error("Dimension mismatch");i.count++,i.inputIndices.push(n)}else i={count:1,dimValue:t,inputIndices:[n]};this.symbolToInfo.set(e,i)}processTerm(e,t,n,i=-1){let u=n.length,l=!1,d=[],c=0;if(!e.match(RegExp(Ja))&&!t&&e!=="")throw new Error("Invalid LHS term");let s=e.match(RegExp($o,"g")),r=new Yg(i);return s?.forEach((o,a)=>{if(o==="..."){if(l)throw new Error("Only one ellipsis is allowed per input term");l=!0;let p=u-s.length+1;if(p<0)throw new Error("Ellipsis out of bounds");if(d=n.slice(c,c+p),this.hasEllipsis){if(this.ellipsisDims.length!==d.length||this.ellipsisDims.toString()!==d.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=d;else throw new Error("Ellipsis must be specified in the LHS");for(let h=0;he+"_max",Qg=(e,t,n,i)=>{let u=e.map(r=>r.length).map((r,o)=>L(`input${o}`,t,r)),l=B.size(i),d=ne("output",t,i.length),c=[...n.symbolToInfo.keys()].filter(r=>!n.rhs.symbolToIndices.has(r)),s=r=>{let o=[],a="var prod = 1.0;",p="var sum = 0.0;",h="sum += prod;",m=[],g=[],v=[],w=[],y=n.symbolToInfo.size===n.rhs.symbolToIndices.size;n.symbolToInfo.forEach((S,O)=>{if(n.rhs.symbolToIndices.has(O)){let A=n.rhs.symbolToIndices.get(O)?.[0];A!==void 0&&n.lhs.forEach((k,I)=>{if(S.inputIndices.includes(I)){let R=k.symbolToIndices.get(O);if(R===void 0)throw new Error("Invalid symbol error");R.forEach(M=>{o.push(`${u[I].indicesSet(`input${I}Indices`,M,d.indicesGet("outputIndices",A))}`)})}})}else n.lhs.forEach((A,k)=>{if(S.inputIndices.includes(k)){let I=A.symbolToIndices.get(O);if(I===void 0)throw new Error("Invalid symbol error");I.forEach(R=>{m.push(`${u[k].indicesSet(`input${k}Indices`,R,`${O}`)}`)}),w.push(`prod *= ${u[k].getByIndices(`input${k}Indices`)};`)}}),g.push(`for(var ${O}: u32 = 0; ${O} < uniforms.${Qa(O)}; ${O}++) {`),v.push("}")});let T=y?[...o,`let sum = ${u.map((S,O)=>S.getByIndices(`input${O}Indices`)).join(" * ")};`]:[...o,p,...g,...m,a,...w,h,...v];return` + ${r.registerUniforms(c.map(S=>({name:`${Qa(S)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...u,d)} ${r.mainStart()} ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} var outputIndices = ${d.offsetToIndices("global_idx")}; - ${u.map((T,I)=>`var input${I}Indices: ${u[I].type.indices};`).join(` + ${u.map((S,O)=>`var input${O}Indices: ${u[O].type.indices};`).join(` `)} - ${S.join(` + ${T.join(` `)}; ${d.setByOffset("global_idx","sum")}; - }`};return{name:"Einsum",shaderCache:{hint:n.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let r=c.filter(a=>n.symbolToInfo.has(a)).map(a=>({type:12,data:n.symbolToInfo.get(a)?.dimValue||0}));r.push({type:12,data:l});let i=e.map((a,p)=>[...oe(a)]).reduce((a,p)=>a.concat(p),r);return i.push(...oe(o)),{outputs:[{dims:o,dataType:t}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:i}},getShaderSource:s}},R1=(e,t)=>{let n=new qg(e.inputs,t.equation),o=n.outputDims,u=e.inputs.map((l,d)=>l.dims);e.compute(Hg(u,e.inputs[0].dataType,n,o))},N1=e=>{let t=e.equation.replace(/\s+/g,"");return Se({equation:t})}}),Wg,Za,Kg,Xg,z1,UA=R(()=>{"use strict";se(),ae(),ue(),Wg=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),o=n.length{let n=e.length-t.length,o=[];for(let u=0;ue.length>t.length?Za(e,t):Za(t,e),Xg=e=>{let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),o=Kg(t,n),u=e[0].dataType,l=u===9||z.size(t)===1,d=u===9||t.length>0&&t[t.length-1]%4===0?4:1,c=l||o.length>0&&o[o.length-1]%4===0?4:1,s=Math.ceil(z.size(o)/c),r=a=>{let p=j("input",u,t.length,d),h=re("output",u,o.length,c),m;if(u===9){let b=(x,w,y="")=>` + }`};return{name:"Einsum",shaderCache:{hint:n.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let r=c.filter(a=>n.symbolToInfo.has(a)).map(a=>({type:12,data:n.symbolToInfo.get(a)?.dimValue||0}));r.push({type:12,data:l});let o=e.map((a,p)=>[...ae(a)]).reduce((a,p)=>a.concat(p),r);return o.push(...ae(i)),{outputs:[{dims:i,dataType:t}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:o}},getShaderSource:s}},L1=(e,t)=>{let n=new Jg(e.inputs,t.equation),i=n.outputDims,u=e.inputs.map((l,d)=>l.dims);e.compute(Qg(u,e.inputs[0].dataType,n,i))},U1=e=>{let t=e.equation.replace(/\s+/g,"");return ke({equation:t})}}),eb,eu,tb,rb,V1,ek=N(()=>{"use strict";le(),pe(),he(),eb=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),i=n.length{let n=e.length-t.length,i=[];for(let u=0;ue.length>t.length?eu(e,t):eu(t,e),rb=e=>{let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),i=tb(t,n),u=e[0].dataType,l=u===9||B.size(t)===1,d=u===9||t.length>0&&t[t.length-1]%4===0?4:1,c=l||i.length>0&&i[i.length-1]%4===0?4:1,s=Math.ceil(B.size(i)/c),r=a=>{let p=L("input",u,t.length,d),h=ne("output",u,i.length,c),m;if(u===9){let g=(v,w,y="")=>` let outputIndices${w} = ${h.offsetToIndices(`outputOffset + ${w}u`)}; let offset${w} = ${p.broadcastedIndicesToOffset(`outputIndices${w}`,h)}; let index${w} = offset${w} / 4u; let component${w} = offset${w} % 4u; - ${x}[${w}] = ${y}(${p.getByOffset(`index${w}`)}[component${w}]); + ${v}[${w}] = ${y}(${p.getByOffset(`index${w}`)}[component${w}]); `;m=` let outputOffset = global_idx * ${c}; var data = vec4(0); - ${b("data",0,"u32")} - ${b("data",1,"u32")} - ${b("data",2,"u32")} - ${b("data",3,"u32")} + ${g("data",0,"u32")} + ${g("data",1,"u32")} + ${g("data",2,"u32")} + ${g("data",3,"u32")} ${h.setByOffset("global_idx","data")} }`}else m=` let outputIndices = ${h.offsetToIndices(`global_idx * ${c}`)}; @@ -3302,34 +3302,34 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { ${a.registerUniform("vec_size","u32").declareVariables(p,h)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} - ${m}`},i=[{type:12,data:s},...oe(t,o)];return{name:"Expand",shaderCache:{hint:`${o.length};${d}${c}`,inputDependencies:["rank"]},getShaderSource:r,getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:i})}},z1=e=>{Wg(e.inputs),e.compute(Xg(e.inputs),{inputs:[0]})}}),Zg,B1,VA=R(()=>{"use strict";se(),ae(),ue(),Hl(),Zg=e=>{let t=e[0].dataType,n=z.size(e[0].dims),o=z.size(e[1].dims),u=o%4===0,l=d=>{let c=j("x",t,[1],4),s=j("bias",t,[1],4),r=re("y",t,[1],4),i=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],a=h=>` + ${m}`},o=[{type:12,data:s},...ae(t,i)];return{name:"Expand",shaderCache:{hint:`${i.length};${d}${c}`,inputDependencies:["rank"]},getShaderSource:r,getRunData:()=>({outputs:[{dims:i,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:o})}},V1=e=>{eb(e.inputs),e.compute(rb(e.inputs),{inputs:[0]})}}),nb,G1,tk=N(()=>{"use strict";le(),pe(),he(),Zl(),nb=e=>{let t=e[0].dataType,n=B.size(e[0].dims),i=B.size(e[1].dims),u=i%4===0,l=d=>{let c=L("x",t,[1],4),s=L("bias",t,[1],4),r=ne("y",t,[1],4),o=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],a=h=>` let bias${h}_offset: u32 = (global_idx * 4 + ${h}) % uniforms.bias_size; let bias${h} = ${s.getByOffset(`bias${h}_offset / 4`)}[bias${h}_offset % 4];`,p=u?` let bias = ${s.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${a(0)}${a(1)}${a(2)}${a(3)} - let bias = ${c.type.value}(bias0, bias1, bias2, bias3);`;return`${d.registerUniforms(i).declareVariables(c,s,r)} + let bias = ${c.type.value}(bias0, bias1, bias2, bias3);`;return`${d.registerUniforms(o).declareVariables(c,s,r)} - ${hl(Je(t))} + ${bl(tt(t))} - ${d.mainStart(On)} + ${d.mainStart(Nn)} ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} let x = ${c.getByOffset("global_idx")}; ${p} let x_in = x + bias; - ${r.setByOffset("global_idx",fl("x_in"))} - }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${u}`,inputDependencies:["type","type"]},getShaderSource:l,getRunData:d=>({outputs:[{dims:d[0].dims,dataType:d[0].dataType}],programUniforms:[{type:12,data:Math.ceil(n/4)},{type:12,data:o}],dispatchGroup:{x:Math.ceil(n/On/4)}})}},B1=e=>{e.inputs.length<2||z.size(e.inputs[1].dims)===0?t1(e):e.compute(Zg(e.inputs))}}),Jg,Yg,M1,F1,GA=R(()=>{"use strict";se(),ae(),Ve(),ue(),Jg=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Yg=(e,t)=>{let n=e[0].dims,o=e[1].dims,u=n.length,l=z.normalizeAxis(t.axis,u),d=n.slice(0);d.splice(l,1,...o);let c=n[l],s=e[0].dataType===9?4:1,r=Math.ceil(z.size(d)/s),i=[{type:12,data:r},{type:6,data:c},{type:12,data:l},...oe(e[0].dims,e[1].dims,d)],a=p=>{let h=j("data",e[0].dataType,e[0].dims.length,s),m=j("inputIndices",e[1].dataType,e[1].dims.length),b=re("output",e[0].dataType,d.length,s),x=y=>{let S=o.length,T=`var indicesIndices${y} = ${m.type.indices}(0);`;for(let I=0;I1?`indicesIndices${y}[${I}]`:`indicesIndices${y}`} = ${d.length>1?`outputIndices${y}[uniforms.axis + ${I}]`:`outputIndices${y}`};`;T+=` + ${r.setByOffset("global_idx",yl("x_in"))} + }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${u}`,inputDependencies:["type","type"]},getShaderSource:l,getRunData:d=>({outputs:[{dims:d[0].dims,dataType:d[0].dataType}],programUniforms:[{type:12,data:Math.ceil(n/4)},{type:12,data:i}],dispatchGroup:{x:Math.ceil(n/Nn/4)}})}},G1=e=>{e.inputs.length<2||B.size(e.inputs[1].dims)===0?u1(e):e.compute(nb(e.inputs))}}),ib,ob,q1,H1,rk=N(()=>{"use strict";le(),pe(),He(),he(),ib=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},ob=(e,t)=>{let n=e[0].dims,i=e[1].dims,u=n.length,l=B.normalizeAxis(t.axis,u),d=n.slice(0);d.splice(l,1,...i);let c=n[l],s=e[0].dataType===9?4:1,r=Math.ceil(B.size(d)/s),o=[{type:12,data:r},{type:6,data:c},{type:12,data:l},...ae(e[0].dims,e[1].dims,d)],a=p=>{let h=L("data",e[0].dataType,e[0].dims.length,s),m=L("inputIndices",e[1].dataType,e[1].dims.length),g=ne("output",e[0].dataType,d.length,s),v=y=>{let T=i.length,S=`var indicesIndices${y} = ${m.type.indices}(0);`;for(let O=0;O1?`indicesIndices${y}[${O}]`:`indicesIndices${y}`} = ${d.length>1?`outputIndices${y}[uniforms.axis + ${O}]`:`outputIndices${y}`};`;S+=` var idx${y} = ${m.getByIndices(`indicesIndices${y}`)}; if (idx${y} < 0) { idx${y} = idx${y} + uniforms.axisDimLimit; } var dataIndices${y} : ${h.type.indices}; - `;for(let I=0,A=0;I1?`dataIndices${y}[${I}]`:`dataIndices${y}`} = u32(idx${y});`,A+=S):(T+=`${u>1?`dataIndices${y}[${I}]`:`dataIndices${y}`} = ${d.length>1?`outputIndices${y}[${A}]`:`outputIndices${y}`};`,A++);return T},w;if(e[0].dataType===9){let y=(S,T,I="")=>` - let outputIndices${T} = ${b.offsetToIndices(`outputOffset + ${T}u`)}; - ${x(T)}; - let offset${T} = ${h.indicesToOffset(`dataIndices${T}`)}; - let index${T} = offset${T} / 4u; - let component${T} = offset${T} % 4u; - ${S}[${T}] = ${I}(${h.getByOffset(`index${T}`)}[component${T}]); + `;for(let O=0,A=0;O1?`dataIndices${y}[${O}]`:`dataIndices${y}`} = u32(idx${y});`,A+=T):(S+=`${u>1?`dataIndices${y}[${O}]`:`dataIndices${y}`} = ${d.length>1?`outputIndices${y}[${A}]`:`outputIndices${y}`};`,A++);return S},w;if(e[0].dataType===9){let y=(T,S,O="")=>` + let outputIndices${S} = ${g.offsetToIndices(`outputOffset + ${S}u`)}; + ${v(S)}; + let offset${S} = ${h.indicesToOffset(`dataIndices${S}`)}; + let index${S} = offset${S} / 4u; + let component${S} = offset${S} % 4u; + ${T}[${S}] = ${O}(${h.getByOffset(`index${S}`)}[component${S}]); `;w=` let outputOffset = global_idx * ${s}; var value = vec4(0); @@ -3337,19 +3337,19 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { ${y("value",1,"u32")} ${y("value",2,"u32")} ${y("value",3,"u32")} - ${b.setByOffset("global_idx","value")} + ${g.setByOffset("global_idx","value")} `}else w=` - let outputIndices = ${b.offsetToIndices("global_idx")}; - ${x("")}; + let outputIndices = ${g.offsetToIndices("global_idx")}; + ${v("")}; let value = ${h.getByIndices("dataIndices")}; - ${b.setByOffset("global_idx","value")}; + ${g.setByOffset("global_idx","value")}; `;return` - ${p.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(h,m,b)} + ${p.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(h,m,g)} ${p.mainStart()} ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} ${w} - }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:i}),getShaderSource:a}},M1=e=>Se({axis:e.axis}),F1=(e,t)=>{let n=e.inputs;Jg(n),e.compute(Yg(e.inputs,t))}}),Qg,j1,L1,qA=R(()=>{"use strict";se(),ae(),ue(),Qg=(e,t,n,o,u,l,d,c,s)=>{let r=[{type:12,data:l},{type:12,data:o},{type:12,data:u},{type:12,data:n},{type:12,data:d},{type:12,data:c},{type:12,data:s}],i=[l];r.push(...oe(t.dims,i));let a=p=>{let h=j("indices_data",t.dataType,t.dims.length),m=re("input_slice_offsets_data",12,1,1),b=[h,m],x=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:u.length},{name:"sizes_from_slice_dims_data",type:"u32",length:n.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return` - ${p.registerUniforms(x).declareVariables(...b)} + }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:o}),getShaderSource:a}},q1=e=>ke({axis:e.axis}),H1=(e,t)=>{let n=e.inputs;ib(n),e.compute(ob(e.inputs,t))}}),sb,W1,K1,nk=N(()=>{"use strict";le(),pe(),he(),sb=(e,t,n,i,u,l,d,c,s)=>{let r=[{type:12,data:l},{type:12,data:i},{type:12,data:u},{type:12,data:n},{type:12,data:d},{type:12,data:c},{type:12,data:s}],o=[l];r.push(...ae(t.dims,o));let a=p=>{let h=L("indices_data",t.dataType,t.dims.length),m=ne("input_slice_offsets_data",12,1,1),g=[h,m],v=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:u.length},{name:"sizes_from_slice_dims_data",type:"u32",length:n.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return` + ${p.registerUniforms(v).declareVariables(...g)} ${p.mainStart()} ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let batch_idx = global_idx / uniforms.num_slices_per_batch; @@ -3367,34 +3367,34 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { } input_slice_offsets_data[global_idx] = base_offset + u32(relative_slice_offset); - }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${u.length}_${n.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:i,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:r}),getShaderSource:a},{inputs:[t],outputs:[-1]})[0]},j1=(e,t)=>{let n=e.inputs,o=n[0].dims,u=n[0].dataType,l=n[1].dims,d=l[l.length-1],c=z.sizeToDimension(l,l.length-1),s=z.sizeFromDimension(o,t.batchDims+d),r=z.sizeToDimension(o,t.batchDims),i=z.sizeFromDimension(o,t.batchDims),a=c/r,p=new Array(d),h=s;for(let T=0;To.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let x=l.slice(0,-1).concat(o.slice(b)),w=z.size(x),y=[{type:12,data:w},{type:12,data:s},...oe(n[0].dims,m.dims,x)],S=T=>{let I=j("data",n[0].dataType,n[0].dims.length),A=j("slice_offsets",12,m.dims.length),P=re("output",n[0].dataType,x.length);return` - ${T.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(I,A,P)} - ${T.mainStart()} - ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${u.length}_${n.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:r}),getShaderSource:a},{inputs:[t],outputs:[-1]})[0]},W1=(e,t)=>{let n=e.inputs,i=n[0].dims,u=n[0].dataType,l=n[1].dims,d=l[l.length-1],c=B.sizeToDimension(l,l.length-1),s=B.sizeFromDimension(i,t.batchDims+d),r=B.sizeToDimension(i,t.batchDims),o=B.sizeFromDimension(i,t.batchDims),a=c/r,p=new Array(d),h=s;for(let S=0;Si.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let v=l.slice(0,-1).concat(i.slice(g)),w=B.size(v),y=[{type:12,data:w},{type:12,data:s},...ae(n[0].dims,m.dims,v)],T=S=>{let O=L("data",n[0].dataType,n[0].dims.length),A=L("slice_offsets",12,m.dims.length),k=ne("output",n[0].dataType,v.length);return` + ${S.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(O,A,k)} + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let slice_offset = slice_offsets[global_idx / uniforms.slice_size]; output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size]; - }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:x,dataType:u}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:y}),getShaderSource:S},{inputs:[n[0],m]})},L1=e=>({batchDims:e.batch_dims,cacheKey:""})}),eb,tb,U1,V1,HA=R(()=>{"use strict";se(),ae(),Ve(),ue(),eb=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let n=z.normalizeAxis(t.quantizeAxis,e[0].dims.length),o=t.blockSize,u=e[0],l=e[2],d=e.length===4?e[3]:void 0;if(l.dims.length!==u.dims.length||!u.dims.map((c,s)=>s===n?Math.ceil(c/o)===l.dims[s]:c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(d){if(d.dataType!==u.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(d.dims.length!==l.dims.length||!d.dims.map((c,s)=>c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},tb=(e,t)=>{let n=e[0].dims,o=e[1].dims,u=n.length,l=z.normalizeAxis(t.gatherAxis,u),d=z.normalizeAxis(t.quantizeAxis,u),c=n.slice(0);c.splice(l,1,...o);let s=z.size(c),r=e[2].dataType,i=e[0].dataType===22,a=[{type:12,data:s},{type:12,data:d},{type:12,data:l},{type:12,data:t.blockSize},...oe(...e.map((h,m)=>h.dims),c)],p=h=>{let m=j("data",e[0].dataType,e[0].dims.length),b=j("inputIndices",e[1].dataType,e[1].dims.length),x=j("scales",e[2].dataType,e[2].dims.length),w=e.length>3?j("zeroPoint",e[3].dataType,e[3].dims.length):void 0,y=re("output",r,c.length),S=[m,b,x];w&&S.push(w);let T=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` - ${h.registerUniforms(T).declareVariables(...S,y)} + }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:v,dataType:u}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:y}),getShaderSource:T},{inputs:[n[0],m]})},K1=e=>({batchDims:e.batch_dims,cacheKey:""})}),ab,ub,X1,Z1,ik=N(()=>{"use strict";le(),pe(),He(),he(),ab=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let n=B.normalizeAxis(t.quantizeAxis,e[0].dims.length),i=t.blockSize,u=e[0],l=e[2],d=e.length===4?e[3]:void 0;if(l.dims.length!==u.dims.length||!u.dims.map((c,s)=>s===n?Math.ceil(c/i)===l.dims[s]:c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(d){if(d.dataType!==u.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(d.dims.length!==l.dims.length||!d.dims.map((c,s)=>c===l.dims[s]).reduce((c,s)=>c&&s,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},ub=(e,t)=>{let n=e[0].dims,i=e[1].dims,u=n.length,l=B.normalizeAxis(t.gatherAxis,u),d=B.normalizeAxis(t.quantizeAxis,u),c=n.slice(0);c.splice(l,1,...i);let s=B.size(c),r=e[2].dataType,o=e[0].dataType===22,a=[{type:12,data:s},{type:12,data:d},{type:12,data:l},{type:12,data:t.blockSize},...ae(...e.map((h,m)=>h.dims),c)],p=h=>{let m=L("data",e[0].dataType,e[0].dims.length),g=L("inputIndices",e[1].dataType,e[1].dims.length),v=L("scales",e[2].dataType,e[2].dims.length),w=e.length>3?L("zeroPoint",e[3].dataType,e[3].dims.length):void 0,y=ne("output",r,c.length),T=[m,g,v];w&&T.push(w);let S=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${h.registerUniforms(S).declareVariables(...T,y)} ${h.mainStart()} let output_indices = ${y.offsetToIndices("global_idx")}; - var indices_indices = ${b.type.indices}(0); - ${o.length>1?` - for (var i: u32 = 0; i < ${o.length}; i++) { + var indices_indices = ${g.type.indices}(0); + ${i.length>1?` + for (var i: u32 = 0; i < ${i.length}; i++) { let index = ${y.indicesGet("output_indices","uniforms.gather_axis + i")}; - ${b.indicesSet("indices_indices","i","index")}; + ${g.indicesSet("indices_indices","i","index")}; }`:`indices_indices = ${y.indicesGet("output_indices","uniforms.gather_axis")};`}; var data_indices = ${m.type.indices}(0); for (var i: u32 = 0; i < uniforms.gather_axis; i++) { let index = ${y.indicesGet("output_indices","i")}; ${m.indicesSet("data_indices","i","index")}; } - var index_from_indices = ${b.getByIndices("indices_indices")}; + var index_from_indices = ${g.getByIndices("indices_indices")}; if (index_from_indices < 0) { index_from_indices += ${n[l]}; } ${m.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")}; for (var i = uniforms.gather_axis + 1; i < ${c.length}; i++) { - let index = ${y.indicesGet("output_indices",`i + ${o.length} - 1`)}; + let index = ${y.indicesGet("output_indices",`i + ${i.length} - 1`)}; ${m.indicesSet("data_indices","i","index")}; } let data_offset = ${m.indicesToOffset("data_indices")}; @@ -3402,27 +3402,27 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { // Convert 4-bit packed data to 8-bit packed data. let packed_4bit_quantized_data = ${m.getByOffset("data_offset / 8")}; let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; - let quantized_data_vec = ${i?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); + let quantized_data_vec = ${o?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); let quantized_data = quantized_data_vec[data_index / 2]; var scale_indices = data_indices; - let quantize_axis_index = ${x.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; - ${x.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; - var scale = ${x.getByIndices("scale_indices")}; + let quantize_axis_index = ${v.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; + ${v.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; + var scale = ${v.getByIndices("scale_indices")}; ${w?` let zero_point_indices = scale_indices; let zero_point_offset = ${w.indicesToOffset("zero_point_indices")}; let zero_point_index = zero_point_offset % 8; let packed_4bit_zero_points = ${w.getByOffset("zero_point_offset / 8")}; let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; - let zero_point_vec = ${i?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); + let zero_point_vec = ${o?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"}; - let dequantized_data = ${Je(r)}(quantized_data - zero_point) * scale; + let dequantized_data = ${tt(r)}(quantized_data - zero_point) * scale; ${y.setByOffset("global_idx","dequantized_data")}; - }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((h,m)=>m!==1).map(h=>h.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(h,m)=>"rank")},getRunData:()=>({outputs:[{dims:c,dataType:r}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:a}),getShaderSource:p}},U1=(e,t)=>{let n=e.inputs;eb(n,t),e.compute(tb(e.inputs,t))},V1=e=>Se({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),rb,nb,G1,q1,WA=R(()=>{"use strict";se(),ae(),Ve(),ue(),rb=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and - indices input tensors be of same rank.`)},nb=(e,t)=>{let n=e[0].dims,o=e[0].dataType,u=n.length,l=e[1].dims,d=e[1].dataType,c=z.normalizeAxis(t.axis,u),s=n[c],r=l.slice(0),i=z.size(r),a=j("input",o,u),p=j("indicesInput",d,l.length),h=re("output",o,r.length),m=[{type:12,data:i},{type:6,data:s},{type:12,data:c}];return m.push(...oe(n,l,r)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:m}),getShaderSource:b=>` - ${b.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(a,p,h)} - ${b.mainStart()} - ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((h,m)=>m!==1).map(h=>h.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(h,m)=>"rank")},getRunData:()=>({outputs:[{dims:c,dataType:r}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:a}),getShaderSource:p}},X1=(e,t)=>{let n=e.inputs;ab(n,t),e.compute(ub(e.inputs,t))},Z1=e=>ke({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),lb,db,Y1,J1,ok=N(()=>{"use strict";le(),pe(),He(),he(),lb=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and + indices input tensors be of same rank.`)},db=(e,t)=>{let n=e[0].dims,i=e[0].dataType,u=n.length,l=e[1].dims,d=e[1].dataType,c=B.normalizeAxis(t.axis,u),s=n[c],r=l.slice(0),o=B.size(r),a=L("input",i,u),p=L("indicesInput",d,l.length),h=ne("output",i,r.length),m=[{type:12,data:o},{type:6,data:s},{type:12,data:c}];return m.push(...ae(n,l,r)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:m}),getShaderSource:g=>` + ${g.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(a,p,h)} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let outputIndices = ${h.offsetToIndices("global_idx")}; @@ -3435,8 +3435,8 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { let value = ${a.getByIndices("inputIndices")}; ${h.setByOffset("global_idx","value")}; - }`}},G1=e=>Se({axis:e.axis}),q1=(e,t)=>{let n=e.inputs;rb(n),e.compute(nb(e.inputs,t))}}),ib,ob,H1,W1,KA=R(()=>{"use strict";se(),ae(),ue(),ib=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},ob=(e,t)=>{let n=e[0].dims.slice(),o=e[1].dims.slice(),[u,l,d]=Lv.getShapeOfGemmResult(n,t.transA,o,t.transB,e.length===3?e[2].dims:void 0),c=[u,l];if(!c)throw new Error("Can't use gemm on the given tensors");let s=16,r=Math.ceil(l/s),i=Math.ceil(u/s),a=!0,p=z.size(c),h=[{type:12,data:a?r:p},{type:12,data:u},{type:12,data:l},{type:12,data:d},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(h.push(...oe(e[2].dims)),m.push("rank")),h.push(...oe(c));let b=w=>{let y="";t.transA&&t.transB?y="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?y="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?y="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(y="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let S=t.alpha===1?"":"value *= uniforms.alpha;",T=j("a",e[0].dataType,e[0].dims),I=j("b",e[1].dataType,e[1].dims),A=T.type.value,P=null,O=[T,I];e.length===3&&(P=j("c",e[2].dataType,e[2].dims.length),O.push(P));let B=re("output",e[0].dataType,c.length);O.push(B);let U=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` - ${w.registerUniforms(U).declareVariables(...O)} + }`}},Y1=e=>ke({axis:e.axis}),J1=(e,t)=>{let n=e.inputs;lb(n),e.compute(db(e.inputs,t))}}),cb,pb,Q1,e2,sk=N(()=>{"use strict";le(),pe(),he(),cb=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},pb=(e,t)=>{let n=e[0].dims.slice(),i=e[1].dims.slice(),[u,l,d]=Kv.getShapeOfGemmResult(n,t.transA,i,t.transB,e.length===3?e[2].dims:void 0),c=[u,l];if(!c)throw new Error("Can't use gemm on the given tensors");let s=16,r=Math.ceil(l/s),o=Math.ceil(u/s),a=!0,p=B.size(c),h=[{type:12,data:a?r:p},{type:12,data:u},{type:12,data:l},{type:12,data:d},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(h.push(...ae(e[2].dims)),m.push("rank")),h.push(...ae(c));let g=w=>{let y="";t.transA&&t.transB?y="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?y="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?y="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(y="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let T=t.alpha===1?"":"value *= uniforms.alpha;",S=L("a",e[0].dataType,e[0].dims),O=L("b",e[1].dataType,e[1].dims),A=S.type.value,k=null,I=[S,O];e.length===3&&(k=L("c",e[2].dataType,e[2].dims.length),I.push(k));let R=ne("output",e[0].dataType,c.length);I.push(R);let M=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` + ${w.registerUniforms(M).declareVariables(...I)} ${w.mainStart()} ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} @@ -3449,10 +3449,10 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { ${y} } - ${S} - ${P!=null?`let cOffset = ${P.broadcastedIndicesToOffset("vec2(m, n)",B)}; value += ${A}(uniforms.beta) * ${P.getByOffset("cOffset")};`:""} + ${T} + ${k!=null?`let cOffset = ${k.broadcastedIndicesToOffset("vec2(m, n)",R)}; value += ${A}(uniforms.beta) * ${k.getByOffset("cOffset")};`:""} output[global_idx] = value; - }`},x=w=>{let y=j("a",e[0].dataType,e[0].dims),S=j("b",e[1].dataType,e[1].dims),T=null,I=[y,S];e.length===3&&(T=j("c",e[2].dataType,e[2].dims.length),I.push(T));let A=re("output",e[0].dataType,c.length);I.push(A);let P=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],O="",B="";t.transA&&t.transB?(B=` + }`},v=w=>{let y=L("a",e[0].dataType,e[0].dims),T=L("b",e[1].dataType,e[1].dims),S=null,O=[y,T];e.length===3&&(S=L("c",e[2].dataType,e[2].dims.length),O.push(S));let A=ne("output",e[0].dataType,c.length);O.push(A);let k=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],I="",R="";t.transA&&t.transB?(R=` var col = tile_row_start + local_id.x; var row = k_start + local_id.y; if (col < uniforms.M && row < uniforms.K) { @@ -3466,9 +3466,9 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { if (col < uniforms.K && row < uniforms.N) { tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; } else { - tile_b[local_id.y][local_id.x] = ${S.type.value}(0); + tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,O="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(B=` + `,I="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(R=` var col = tile_row_start + local_id.x; var row = k_start + local_id.y; if (col < uniforms.M && row < uniforms.K) { @@ -3482,9 +3482,9 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { if (col < uniforms.N && row < uniforms.K) { tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; } else { - tile_b[local_id.y][local_id.x] = ${S.type.value}(0); + tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,O="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(B=` + `,I="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(R=` var col = k_start + local_id.x; var row = tile_row_start + local_id.y; if (col < uniforms.K && row < uniforms.M) { @@ -3498,9 +3498,9 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { if (col < uniforms.K && row < uniforms.N) { tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; } else { - tile_b[local_id.y][local_id.x] = ${S.type.value}(0); + tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,O="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(B=` + `,I="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(R=` var col = k_start + local_id.x; var row = tile_row_start + local_id.y; if (col < uniforms.K && row < uniforms.M) { @@ -3514,12 +3514,12 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { if (col < uniforms.N && row < uniforms.K) { tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; } else { - tile_b[local_id.y][local_id.x] = ${S.type.value}(0); + tile_b[local_id.y][local_id.x] = ${T.type.value}(0); } - `,O="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let U=t.alpha===1?"":"value *= uniforms.alpha;";return` - ${w.registerUniforms(P).declareVariables(...I)} + `,I="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let M=t.alpha===1?"":"value *= uniforms.alpha;";return` + ${w.registerUniforms(k).declareVariables(...O)} var tile_a: array, ${s}>; - var tile_b: array, ${s}>; + var tile_b: array, ${s}>; ${w.mainStart([s,s,1])} let tile_col_start = (workgroup_index % uniforms.num_tile_n) * ${s}; let tile_row_start = (workgroup_index / uniforms.num_tile_n) * ${s}; @@ -3527,24 +3527,24 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { var k_start = 0u; var value = ${A.type.value}(0); for (var t: u32 = 0u; t < num_tiles; t++) { - ${B} + ${R} k_start = k_start + ${s}; workgroupBarrier(); for (var k: u32 = 0u; k < ${s}; k++) { - ${O} + ${I} } workgroupBarrier(); } - ${U} + ${M} let m = tile_row_start + local_id.y; let n = tile_col_start + local_id.x; - ${T!=null?`let cOffset = ${T.broadcastedIndicesToOffset("vec2(m, n)",A)}; value += ${A.type.value}(uniforms.beta) * ${T.getByOffset("cOffset")};`:""} + ${S!=null?`let cOffset = ${S.broadcastedIndicesToOffset("vec2(m, n)",A)}; value += ${A.type.value}(uniforms.beta) * ${S.getByOffset("cOffset")};`:""} if (m < uniforms.M && n < uniforms.N) { output[m * uniforms.N + n] = value; } - }`};return a?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:r*i},programUniforms:h}),getShaderSource:x}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:b}},H1=e=>{let t=e.transA,n=e.transB,o=e.alpha,u=e.beta;return{transA:t,transB:n,alpha:o,beta:u,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},W1=(e,t)=>{ib(e.inputs),e.compute(ob(e.inputs,t))}}),Lt,Zt,Rr,Nr,sb,ab,ub,lb,db,cb,pb,hb,K1,X1,XA=R(()=>{"use strict";se(),ae(),Ve(),ue(),[Lt,Zt,Rr,Nr]=[0,1,2,3],sb=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},ab=` + }`};return a?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:r*o},programUniforms:h}),getShaderSource:v}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:g}},Q1=e=>{let t=e.transA,n=e.transB,i=e.alpha,u=e.beta;return{transA:t,transB:n,alpha:i,beta:u,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},e2=(e,t)=>{cb(e.inputs),e.compute(pb(e.inputs,t))}}),Xt,ir,Ur,Vr,hb,fb,mb,gb,bb,yb,wb,_b,t2,r2,ak=N(()=>{"use strict";le(),pe(),He(),he(),[Xt,ir,Ur,Vr]=[0,1,2,3],hb=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},fb=` fn gs_get_cubic_coeffs(x: f32) -> vec4 { let cubic_alpha = -0.75f; let x_abs = abs(x); @@ -3555,7 +3555,7 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha); return coeffs; } -`,ub=e=>` +`,mb=e=>` fn gs_bicubic_interpolate(p: mat4x4<${e}>, x: f32, y: f32) -> ${e} { var v: vec4; var coeffs = gs_get_cubic_coeffs(x); @@ -3566,7 +3566,7 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { let pixel = ${e}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]); return pixel; } -`,lb=e=>` +`,gb=e=>` fn gs_denormalize(n: f32, length: i32) -> f32 { ${e.alignCorners===0?` // alignCorners: false => [-1, 1] to [-0.5, length - 0.5] @@ -3576,7 +3576,7 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { return (n + 1.0) / 2.0 * (f32(length - 1)); `} } -`,db=e=>` +`,bb=e=>` ${e.paddingMode==="reflection"?` fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 { var dx = 0.0; @@ -3603,39 +3603,39 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { } return u32(fx); }`:""} -`,cb=(e,t,n)=>` +`,yb=(e,t,n)=>` fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4) -> ${t} { var pixel = ${t}(0); var indices = vec4(0); - indices[${Lt}] = batch; - indices[${Zt}] = channel;`+(()=>{switch(n.paddingMode){case"zeros":return` + indices[${Xt}] = batch; + indices[${ir}] = channel;`+(()=>{switch(n.paddingMode){case"zeros":return` if (r >= 0 && r < H && c >=0 && c < W) { - indices[${Rr}] = u32(r); - indices[${Nr}] = u32(c); + indices[${Ur}] = u32(r); + indices[${Vr}] = u32(c); } else { return ${t}(0); } `;case"border":return` - indices[${Rr}] = u32(clamp(r, 0, H - 1)); - indices[${Nr}] = u32(clamp(c, 0, W - 1)); + indices[${Ur}] = u32(clamp(r, 0, H - 1)); + indices[${Vr}] = u32(clamp(c, 0, W - 1)); `;case"reflection":return` - indices[${Rr}] = gs_reflect(r, border[1], border[3]); - indices[${Nr}] = gs_reflect(c, border[0], border[2]); + indices[${Ur}] = gs_reflect(r, border[1], border[3]); + indices[${Vr}] = gs_reflect(c, border[0], border[2]); `;default:throw new Error(`padding mode ${n.paddingMode} is not supported`)}})()+` return ${e.getByIndices("indices")}; } -`,pb=(e,t,n)=>(()=>{switch(n.mode){case"nearest":return` - let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${Lt}], indices[${Zt}], border); +`,wb=(e,t,n)=>(()=>{switch(n.mode){case"nearest":return` + let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${Xt}], indices[${ir}], border); `;case"bilinear":return` let x1 = i32(floor(x)); let y1 = i32(floor(y)); let x2 = x1 + 1; let y2 = y1 + 1; - let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${Lt}], indices[${Zt}], border); - let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${Lt}], indices[${Zt}], border); - let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${Lt}], indices[${Zt}], border); - let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${Lt}], indices[${Zt}], border); + let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${Xt}], indices[${ir}], border); + let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${Xt}], indices[${ir}], border); + let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${Xt}], indices[${ir}], border); + let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${Xt}], indices[${ir}], border); let dx2 = ${t}(f32(x2) - x); let dx1 = ${t}(x - f32(x1)); @@ -3648,25 +3648,25 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { var p: mat4x4<${t}>; for (var h = 0; h < 4; h++) { for (var w = 0; w < 4; w++) { - p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${Lt}], indices[${Zt}], border); + p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${Xt}], indices[${ir}], border); } } let dx = x - f32(x0 + 1); let dy = y - f32(y0 + 1); let result = gs_bicubic_interpolate(p, dx, dy); - `;default:throw new Error(`mode ${n.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,hb=(e,t)=>{let n=j("x",e[0].dataType,e[0].dims.length),o=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],u=j("grid",e[1].dataType,o.length,2),l=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(l=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[Lt,Zt,Rr,Nr]=[0,3,1,2]);let d=re("output",e[0].dataType,l.length),c=n.type.value,s=z.size(l),r=[{type:12,data:s},...oe(e[0].dims,o,l)],i=a=>` + `;default:throw new Error(`mode ${n.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,_b=(e,t)=>{let n=L("x",e[0].dataType,e[0].dims.length),i=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],u=L("grid",e[1].dataType,i.length,2),l=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(l=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[Xt,ir,Ur,Vr]=[0,3,1,2]);let d=ne("output",e[0].dataType,l.length),c=n.type.value,s=B.size(l),r=[{type:12,data:s},...ae(e[0].dims,i,l)],o=a=>` ${a.registerUniform("output_size","u32").declareVariables(n,u,d)} - ${ab} - ${ub(c)} - ${lb(t)} - ${db(t)} - ${cb(n,c,t)} + ${fb} + ${mb(c)} + ${gb(t)} + ${bb(t)} + ${yb(n,c,t)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let H_in = i32(uniforms.x_shape[${Rr}]); - let W_in = i32(uniforms.x_shape[${Nr}]); + let H_in = i32(uniforms.x_shape[${Ur}]); + let W_in = i32(uniforms.x_shape[${Vr}]); ${t.alignCorners===0?` let x_min = -0.5; @@ -3682,35 +3682,35 @@ ${w} for (var i = local_idx; i < uniforms.outputLength; i += ${Xt}u) { let border = vec4(x_min, y_min, x_max, y_max); let indices = ${d.offsetToIndices("global_idx")}; - var grid_indices = vec3(indices[${Lt}], indices[${Rr}], indices[${Nr}]); + var grid_indices = vec3(indices[${Xt}], indices[${Ur}], indices[${Vr}]); let nxy = ${u.getByIndices("grid_indices")}; var x = gs_denormalize(f32(nxy[0]), W_in); var y = gs_denormalize(f32(nxy[1]), H_in); - ${pb(d,c,t)} - }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:a=>{let p=z.size(l);return{outputs:[{dims:l,dataType:a[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:r}},getShaderSource:i}},K1=(e,t)=>{sb(e.inputs),e.compute(hb(e.inputs,t))},X1=e=>Se({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),st,fb,Z1,Ja,mb,yi,J1,Y1=R(()=>{"use strict";se(),ae(),Ve(),Ul(),ql(),ue(),$r(),st=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,fb=(e,t)=>{let n=e[0],o=st(e,1),u=st(e,2),l=st(e,3),d=st(e,4),c=st(e,5),s=st(e,6),r=st(e,7);if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let i=n.dims[0],a=n.dims[1],p=n.dims.length===3?n.dims[2]:t.numHeads*n.dims[4],h=a,m=0,b=0,x=Math.floor(p/t.numHeads);if(s&&r&&z.size(s.dims)&&z.size(r.dims)){if(s.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(s.dims[0]!==i||s.dims[1]!==t.numHeads||s.dims[3]!==x)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(r.dims[0]!==i||r.dims[1]!==t.numHeads||r.dims[3]!==x)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(s.dims[2]!==r.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(r.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');m=s.dims[2],b=s.dims[2]}else if(s&&z.size(s.dims)||r&&z.size(r.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let w;if(o&&z.size(o.dims)>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(o.dims.length<3||o.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==o.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(o.dims.length===3){if(o.dims[2]!==n.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');w=2,h=o.dims[1]}else if(o.dims.length===5){if(o.dims[2]!==t.numHeads||o.dims[3]!==2||o.dims[4]!==x)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');w=5,h=o.dims[1]}else{if(o.dims[1]!==t.numHeads||o.dims[3]!==x)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');w=0,h=o.dims[2]}}else{if(n.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(n.dims[2]!==t.numHeads||n.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');w=3}if(l&&z.size(l.dims)>0){if(l.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(o&&o.dims.length===5&&o.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let y=m+h,S=0;if(d&&z.size(d.dims)>0){S=8;let P=d.dims;throw P.length===1?P[0]===i?S=1:P[0]===3*i+2&&(S=3):P.length===2&&P[0]===i&&P[1]===y&&(S=5),S===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let T=!1,I=p;if(u&&z.size(u.dims)>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(h!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');I=u.dims[2]}else{if(h!==u.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');I=u.dims[1]*u.dims[3],T=!0}}let A=!1;if(d&&z.size(d.dims)>0)throw new Error("Key padding mask is not supported");if(c&&z.size(c.dims)>0){if(c.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(c.dims[0]!==i||c.dims[1]!==t.numHeads||c.dims[2]!==a||c.dims[3]!==y)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:i,sequenceLength:a,pastSequenceLength:m,kvSequenceLength:h,totalSequenceLength:y,maxSequenceLength:b,inputHiddenSize:0,hiddenSize:p,vHiddenSize:I,headSize:x,vHeadSize:Math.floor(I/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:S,scale:t.scale,broadcastResPosBias:A,passPastInKv:T,qkvFormat:w}},Z1=e=>Se({...e}),Ja=Se({perm:[0,2,1,3]}),mb=(e,t,n,o,u,l,d)=>{let c=[o,u,l],s=z.size(c),r=[{type:12,data:s},{type:12,data:d},{type:12,data:l}],i=a=>{let p=re("qkv_with_bias",t.dataType,c),h=j("qkv",t.dataType,c),m=j("bias",n.dataType,c),b=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` - ${a.registerUniforms(b).declareVariables(h,m,p)} + ${wb(d,c,t)} + }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:a=>{let p=B.size(l);return{outputs:[{dims:l,dataType:a[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:r}},getShaderSource:o}},t2=(e,t)=>{hb(e.inputs),e.compute(_b(e.inputs,t))},r2=e=>ke({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),ct,vb,n2,tu,xb,Si,i2,o2=N(()=>{"use strict";le(),pe(),He(),Hl(),Xl(),he(),Ar(),ct=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,vb=(e,t)=>{let n=e[0],i=ct(e,1),u=ct(e,2),l=ct(e,3),d=ct(e,4),c=ct(e,5),s=ct(e,6),r=ct(e,7);if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let o=n.dims[0],a=n.dims[1],p=n.dims.length===3?n.dims[2]:t.numHeads*n.dims[4],h=a,m=0,g=0,v=Math.floor(p/t.numHeads);if(s&&r&&B.size(s.dims)&&B.size(r.dims)){if(s.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(s.dims[0]!==o||s.dims[1]!==t.numHeads||s.dims[3]!==v)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(r.dims[0]!==o||r.dims[1]!==t.numHeads||r.dims[3]!==v)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(s.dims[2]!==r.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(r.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');m=s.dims[2],g=s.dims[2]}else if(s&&B.size(s.dims)||r&&B.size(r.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let w;if(i&&B.size(i.dims)>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(i.dims[2]!==n.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');w=2,h=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==v)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');w=5,h=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==v)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');w=0,h=i.dims[2]}}else{if(n.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(n.dims[2]!==t.numHeads||n.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');w=3}if(l&&B.size(l.dims)>0){if(l.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(i&&i.dims.length===5&&i.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let y=m+h,T=0;if(d&&B.size(d.dims)>0){T=8;let k=d.dims;throw k.length===1?k[0]===o?T=1:k[0]===3*o+2&&(T=3):k.length===2&&k[0]===o&&k[1]===y&&(T=5),T===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let S=!1,O=p;if(u&&B.size(u.dims)>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(h!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');O=u.dims[2]}else{if(h!==u.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');O=u.dims[1]*u.dims[3],S=!0}}let A=!1;if(d&&B.size(d.dims)>0)throw new Error("Key padding mask is not supported");if(c&&B.size(c.dims)>0){if(c.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(c.dims[0]!==o||c.dims[1]!==t.numHeads||c.dims[2]!==a||c.dims[3]!==y)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:o,sequenceLength:a,pastSequenceLength:m,kvSequenceLength:h,totalSequenceLength:y,maxSequenceLength:g,inputHiddenSize:0,hiddenSize:p,vHiddenSize:O,headSize:v,vHeadSize:Math.floor(O/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:T,scale:t.scale,broadcastResPosBias:A,passPastInKv:S,qkvFormat:w}},n2=e=>ke({...e}),tu=ke({perm:[0,2,1,3]}),xb=(e,t,n,i,u,l,d)=>{let c=[i,u,l],s=B.size(c),r=[{type:12,data:s},{type:12,data:d},{type:12,data:l}],o=a=>{let p=ne("qkv_with_bias",t.dataType,c),h=L("qkv",t.dataType,c),m=L("bias",n.dataType,c),g=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` + ${a.registerUniforms(g).declareVariables(h,m,p)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; - }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:c,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r}),getShaderSource:i},{inputs:[t,n],outputs:[-1]})[0]},yi=(e,t,n,o,u,l,d,c)=>{let s=l;if(d&&z.size(d.dims)>0){if(o===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return s=mb(e,l,d,t,o,n*u,c),s=s.reshape([t,o,n,u]),n===1||o===1?s:e.compute(bt(s,Ja.perm),{inputs:[s],outputs:[-1]})[0]}else return l.dims.length===3&&(s=l.reshape([t,o,n,u])),n===1||o===1?s:e.compute(bt(s,Ja.perm),{inputs:[s],outputs:[-1]})[0]},J1=(e,t)=>{let n=fb(e.inputs,t),o=e.inputs[0],u=st(e.inputs,1),l=st(e.inputs,2),d=st(e.inputs,3),c=st(e.inputs,4),s=st(e.inputs,5),r=st(e.inputs,6),i=st(e.inputs,7);if(o.dims.length===5)throw new Error("Packed QKV is not implemented");if(u?.dims.length===5)throw new Error("Packed KV is not implemented");let a=u&&l&&u.dims.length===4&&l.dims.length===4,p=yi(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,o,d,0);if(a)return Oi(e,p,u,l,c,void 0,r,i,s,n);if(!u||!l)throw new Error("key and value must be provided");let h=yi(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.headSize,u,d,n.hiddenSize),m=yi(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.vHeadSize,l,d,2*n.hiddenSize);Oi(e,p,h,m,c,void 0,r,i,s,n)}}),gb,bb,yb,wb,wl,Q1,e2,t2=R(()=>{"use strict";se(),ae(),Ve(),ue(),gb=e=>{if(!e||e.length<1)throw new Error("too few inputs")},bb=(e,t)=>{let n=[],o=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(u=>n.push(Number(u))),o=n.length),Se({numOutputs:o,axis:t.axis,splitSizes:n})},yb=e=>` + }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:c,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:r}),getShaderSource:o},{inputs:[t,n],outputs:[-1]})[0]},Si=(e,t,n,i,u,l,d,c)=>{let s=l;if(d&&B.size(d.dims)>0){if(i===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return s=xb(e,l,d,t,i,n*u,c),s=s.reshape([t,i,n,u]),n===1||i===1?s:e.compute(xt(s,tu.perm),{inputs:[s],outputs:[-1]})[0]}else return l.dims.length===3&&(s=l.reshape([t,i,n,u])),n===1||i===1?s:e.compute(xt(s,tu.perm),{inputs:[s],outputs:[-1]})[0]},i2=(e,t)=>{let n=vb(e.inputs,t),i=e.inputs[0],u=ct(e.inputs,1),l=ct(e.inputs,2),d=ct(e.inputs,3),c=ct(e.inputs,4),s=ct(e.inputs,5),r=ct(e.inputs,6),o=ct(e.inputs,7);if(i.dims.length===5)throw new Error("Packed QKV is not implemented");if(u?.dims.length===5)throw new Error("Packed KV is not implemented");let a=u&&l&&u.dims.length===4&&l.dims.length===4,p=Si(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,i,d,0);if(a)return Ri(e,p,u,l,c,void 0,r,o,s,n);if(!u||!l)throw new Error("key and value must be provided");let h=Si(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.headSize,u,d,n.hiddenSize),m=Si(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.vHeadSize,l,d,2*n.hiddenSize);Ri(e,p,h,m,c,void 0,r,o,s,n)}}),$b,Tb,Sb,Ib,$l,s2,a2,u2=N(()=>{"use strict";le(),pe(),He(),he(),$b=e=>{if(!e||e.length<1)throw new Error("too few inputs")},Tb=(e,t)=>{let n=[],i=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(u=>n.push(Number(u))),i=n.length),ke({numOutputs:i,axis:t.axis,splitSizes:n})},Sb=e=>` fn calculateOutputIndex(index: u32) -> u32 { for (var i: u32 = 0u; i < ${e}u; i += 1u ) { - if (index < ${ne("uniforms.size_in_split_axis","i",e)}) { + if (index < ${oe("uniforms.size_in_split_axis","i",e)}) { return i; } } return ${e}u; -}`,wb=e=>{let t=e.length,n=[];for(let o=0;o{let t=e.length,n=[];for(let i=0;i{let n=e[0].dims,o=z.size(n),u=e[0].dataType,l=z.normalizeAxis(t.axis,n.length),d=new Array(t.numOutputs),c=j("input",u,n.length),s=new Array(t.numOutputs),r=[],i=[],a=0,p=[{type:12,data:o}];for(let m=0;m` + }`},$l=(e,t)=>{let n=e[0].dims,i=B.size(n),u=e[0].dataType,l=B.normalizeAxis(t.axis,n.length),d=new Array(t.numOutputs),c=L("input",u,n.length),s=new Array(t.numOutputs),r=[],o=[],a=0,p=[{type:12,data:i}];for(let m=0;m` ${m.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",s.length).declareVariables(c,...d)} - ${yb(s.length)} - ${wb(d)} + ${Sb(s.length)} + ${Ib(d)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} @@ -3719,40 +3719,40 @@ fn calculateOutputIndex(index: u32) -> u32 { var index = ${c.indicesGet("indices",l)}; let output_number = calculateOutputIndex(index); if (output_number != 0) { - index -= ${ne("uniforms.size_in_split_axis","output_number - 1u",s.length)}; + index -= ${oe("uniforms.size_in_split_axis","output_number - 1u",s.length)}; ${c.indicesSet("indices",l,"index")}; } writeBufferData(output_number, indices, global_idx); - }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:h,getRunData:()=>({outputs:r,dispatchGroup:{x:Math.ceil(o/64)},programUniforms:p})}},Q1=(e,t)=>{gb(e.inputs);let n=e.inputs.length===1?t:bb(e.inputs,t);e.compute(wl(e.inputs,n),{inputs:[0]})},e2=e=>{let t=e.axis,n=e.splitSizes,o=e.numOutputs<0?n.length:e.numOutputs;if(o!==n.length)throw new Error("numOutputs and splitSizes length must be equal");return Se({axis:t,numOutputs:o,splitSizes:n})}}),_b,Ro,r2,n2=R(()=>{"use strict";se(),ae(),Ve(),ue(),_b=(e,t)=>{let[n,o,u,l]=e,{numHeads:d,rotaryEmbeddingDim:c}=t;if(n.dims.length!==3&&n.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${n.dims.length}`);if(!z.areEqual(o.dims,[])&&!z.areEqual(o.dims,[1])&&o.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${o.dims.length}`);if(u.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${u.dims.length}`);if(l.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${l.dims.length}`);if(!z.areEqual(u.dims,l.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(c>0&&d===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let s=n.dims[0],r=n.dims[n.dims.length-2],i=u.dims[0],a=z.sizeFromDimension(n.dims,1)/r,p=c===0?u.dims[1]*2:a/d;if(c>p)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(o.dims.length===2){if(s!==o.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${o.dims[0]}`);if(r!==o.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${o.dims[1]}`)}if(r>i)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported");if(p/2!==u.dims[1]&&c/2!==u.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${u.dims[1]}`)},Ro=(e,t)=>{let{interleaved:n,numHeads:o,rotaryEmbeddingDim:u,scale:l}=t,d=e[0].dims[0],c=z.sizeFromDimension(e[0].dims,1),s=e[0].dims[e[0].dims.length-2],r=c/s,i=e[2].dims[1],a=u===0?i*2:r/o,p=new Array(d,s,r/a,a-i),h=z.computeStrides(p),m=[{type:1,data:l},{type:12,data:p},{type:12,data:h},...e[0].dims.length===3?new Array({type:12,data:[c,r,a,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[c,a,s*a,1]}):[],...oe(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],b=x=>{let w=j("input",e[0].dataType,e[0].dims.length),y=j("position_ids",e[1].dataType,e[1].dims.length),S=j("cos_cache",e[2].dataType,e[2].dims.length),T=j("sin_cache",e[3].dataType,e[3].dims.length),I=re("output",e[0].dataType,e[0].dims.length);return x.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:p.length},{name:"global_strides",type:"u32",length:h.length},{name:"input_output_strides",type:"u32",length:h.length}]),` - ${x.declareVariables(w,y,S,T,I)} + }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:h,getRunData:()=>({outputs:r,dispatchGroup:{x:Math.ceil(i/64)},programUniforms:p})}},s2=(e,t)=>{$b(e.inputs);let n=e.inputs.length===1?t:Tb(e.inputs,t);e.compute($l(e.inputs,n),{inputs:[0]})},a2=e=>{let t=e.axis,n=e.splitSizes,i=e.numOutputs<0?n.length:e.numOutputs;if(i!==n.length)throw new Error("numOutputs and splitSizes length must be equal");return ke({axis:t,numOutputs:i,splitSizes:n})}}),Ob,Lo,l2,d2=N(()=>{"use strict";le(),pe(),He(),he(),Ob=(e,t)=>{let[n,i,u,l]=e,{numHeads:d,rotaryEmbeddingDim:c}=t;if(n.dims.length!==3&&n.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${n.dims.length}`);if(!B.areEqual(i.dims,[])&&!B.areEqual(i.dims,[1])&&i.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${i.dims.length}`);if(u.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${u.dims.length}`);if(l.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${l.dims.length}`);if(!B.areEqual(u.dims,l.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(c>0&&d===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let s=n.dims[0],r=n.dims[n.dims.length-2],o=u.dims[0],a=B.sizeFromDimension(n.dims,1)/r,p=c===0?u.dims[1]*2:a/d;if(c>p)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(i.dims.length===2){if(s!==i.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${i.dims[0]}`);if(r!==i.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${i.dims[1]}`)}if(r>o)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported");if(p/2!==u.dims[1]&&c/2!==u.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${u.dims[1]}`)},Lo=(e,t)=>{let{interleaved:n,numHeads:i,rotaryEmbeddingDim:u,scale:l}=t,d=e[0].dims[0],c=B.sizeFromDimension(e[0].dims,1),s=e[0].dims[e[0].dims.length-2],r=c/s,o=e[2].dims[1],a=u===0?o*2:r/i,p=new Array(d,s,r/a,a-o),h=B.computeStrides(p),m=[{type:1,data:l},{type:12,data:p},{type:12,data:h},...e[0].dims.length===3?new Array({type:12,data:[c,r,a,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[c,a,s*a,1]}):[],...ae(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],g=v=>{let w=L("input",e[0].dataType,e[0].dims.length),y=L("position_ids",e[1].dataType,e[1].dims.length),T=L("cos_cache",e[2].dataType,e[2].dims.length),S=L("sin_cache",e[3].dataType,e[3].dims.length),O=ne("output",e[0].dataType,e[0].dims.length);return v.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:p.length},{name:"global_strides",type:"u32",length:h.length},{name:"input_output_strides",type:"u32",length:h.length}]),` + ${v.declareVariables(w,y,T,S,O)} - ${x.mainStart(On)} - let half_rotary_emb_dim = uniforms.${S.name}_shape[1]; + ${v.mainStart(Nn)} + let half_rotary_emb_dim = uniforms.${T.name}_shape[1]; let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; let size = uniforms.global_shape[0] * uniforms.global_strides[0]; - ${x.guardAgainstOutOfBoundsWorkgroupSizes("size")} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("size")} if (bsnh[3] < half_rotary_emb_dim) { let position_ids_idx = - ${y.broadcastedIndicesToOffset("bsnh.xy",re("",y.type.tensor,2))}; + ${y.broadcastedIndicesToOffset("bsnh.xy",ne("",y.type.tensor,2))}; let position_id = u32(${y.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${n}); let j = i + select(half_rotary_emb_dim, 1, ${n}); - let re = ${w.getByOffset("i")} * ${S.get("position_id","bsnh[3]")} - - ${w.getByOffset("j")} * ${T.get("position_id","bsnh[3]")}; - ${I.setByOffset("i","re")} - let im = ${w.getByOffset("i")} * ${T.get("position_id","bsnh[3]")} + + let re = ${w.getByOffset("i")} * ${T.get("position_id","bsnh[3]")} - ${w.getByOffset("j")} * ${S.get("position_id","bsnh[3]")}; - ${I.setByOffset("j","im")} + ${O.setByOffset("i","re")} + let im = ${w.getByOffset("i")} * ${S.get("position_id","bsnh[3]")} + + ${w.getByOffset("j")} * ${T.get("position_id","bsnh[3]")}; + ${O.setByOffset("j","im")} } else { let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; - ${I.setByOffset("k",w.getByOffset("k"))} + ${O.setByOffset("k",w.getByOffset("k"))} } - }`};return{name:"RotaryEmbedding",shaderCache:{hint:Se({interleaved:n}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:b,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(z.size(p)/On)},programUniforms:m})}},r2=(e,t)=>{_b(e.inputs,t),e.compute(Ro(e.inputs,t))}}),vb,xb,Ya,$b,i2,ZA=R(()=>{"use strict";Ve(),se(),ql(),Y1(),t2(),$r(),n2(),ue(),vb=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let n=e[0],o=e[1],u=e[2],l=e[3],d=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let c=!1,s=n.dims[0],r=n.dims[1],i=n.dims.length===3?c?n.dims[2]/3:n.dims[2]:t.numHeads*n.dims[4],a=r,p=0,h=!o||o.dims.length===0,m=Math.floor(h?i/(t.numHeads+2*t.kvNumHeads):i/t.numHeads);h&&(i=m*t.numHeads);let b=l&&l.dims.length!==0,x=d&&d.dims.length!==0;if(b&&l.dims.length===4&&l.dims[0]===s&&l.dims[1]!==t.kvNumHeads&&l.dims[2]===t.kvNumHeads&&l.dims[3]===m)throw new Error("BSNH pastKey/pastValue is not supported");if(b&&x){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');p=l.dims[2]}else if(b||x)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let w=1;if(o&&o.dims.length>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(o.dims.length<3||o.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==o.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(o.dims.length===3){if(n.dims[2]%o.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');a=o.dims[1]}else if(o.dims.length===5){if(o.dims[2]!==t.numHeads||o.dims[3]!==2||o.dims[4]!==m)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');a=o.dims[1]}else{if(o.dims[1]!==t.numHeads||o.dims[3]!==m)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');a=o.dims[2]}}else{if(n.dims.length!==3&&n.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(n.dims.length===5&&(n.dims[2]!==t.numHeads||n.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');w=3}let y=0,S=!1,T=t.kvNumHeads?m*t.kvNumHeads:i;if(u&&u.dims.length>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(a!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=u.dims[2]}else{if(a!==u.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');T=u.dims[1]*u.dims[3],S=!0}}let I=e.length>4?e[5]:void 0;if(I){if(I.dims.length===0)throw new Error("seqlens_k must be at least 1D, got scalar.");let A=I.dims.reduce((P,O)=>P*O,1);if(A!==s)throw new Error(`seqlens_k must have batch_size (${s}) elements, got ${A}.`);for(let P=0;P{let o=t,u=n.kvNumHeads;return t.dims.length===3&&n.kvSequenceLength!==0&&(o=t.reshape([n.batchSize,n.kvSequenceLength,u,n.headSize]),o=e.compute(bt(o,xb.perm),{inputs:[o],outputs:[-1]})[0]),o},$b=(e,t,n,o)=>{let u=7,l=["type","type"],d=[e*t],c=e*t,s=[{type:12,data:c},{type:12,data:t},{type:12,data:e}],r=i=>{let a=j("seq_lens",n.dataType,n.dims),p=j("total_seq_lens",o.dataType,o.dims),h=re("pos_ids",u,d),m=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return` - ${i.registerUniforms(m).declareVariables(a,p,h)} - ${i.mainStart()} - ${i.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + }`};return{name:"RotaryEmbedding",shaderCache:{hint:ke({interleaved:n}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:g,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(B.size(p)/Nn)},programUniforms:m})}},l2=(e,t)=>{Ob(e.inputs,t),e.compute(Lo(e.inputs,t))}}),Eb,Ab,ru,kb,c2,uk=N(()=>{"use strict";He(),le(),Xl(),o2(),u2(),Ar(),d2(),he(),Eb=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let n=e[0],i=e[1],u=e[2],l=e[3],d=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let c=!1,s=n.dims[0],r=n.dims[1],o=n.dims.length===3?c?n.dims[2]/3:n.dims[2]:t.numHeads*n.dims[4],a=r,p=0,h=!i||i.dims.length===0,m=Math.floor(h?o/(t.numHeads+2*t.kvNumHeads):o/t.numHeads);h&&(o=m*t.numHeads);let g=l&&l.dims.length!==0,v=d&&d.dims.length!==0;if(g&&l.dims.length===4&&l.dims[0]===s&&l.dims[1]!==t.kvNumHeads&&l.dims[2]===t.kvNumHeads&&l.dims[3]===m)throw new Error("BSNH pastKey/pastValue is not supported");if(g&&v){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');p=l.dims[2]}else if(g||v)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let w=1;if(i&&i.dims.length>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(n.dims[2]%i.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');a=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==m)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(u)throw new Error('Expect "value" be none when "key" has packed kv format.');a=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==m)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');a=i.dims[2]}}else{if(n.dims.length!==3&&n.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(n.dims.length===5&&(n.dims[2]!==t.numHeads||n.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');w=3}let y=0,T=!1,S=t.kvNumHeads?m*t.kvNumHeads:o;if(u&&u.dims.length>0){if(u.dims.length!==3&&u.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==u.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(u.dims.length===3){if(a!==u.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');S=u.dims[2]}else{if(a!==u.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');S=u.dims[1]*u.dims[3],T=!0}}let O=e.length>4?e[5]:void 0;if(O){if(O.dims.length===0)throw new Error("seqlens_k must be at least 1D, got scalar.");let A=O.dims.reduce((k,I)=>k*I,1);if(A!==s)throw new Error(`seqlens_k must have batch_size (${s}) elements, got ${A}.`);for(let k=0;k{let i=t,u=n.kvNumHeads;return t.dims.length===3&&n.kvSequenceLength!==0&&(i=t.reshape([n.batchSize,n.kvSequenceLength,u,n.headSize]),i=e.compute(xt(i,Ab.perm),{inputs:[i],outputs:[-1]})[0]),i},kb=(e,t,n,i)=>{let u=7,l=["type","type"],d=[e*t],c=e*t,s=[{type:12,data:c},{type:12,data:t},{type:12,data:e}],r=o=>{let a=L("seq_lens",n.dataType,n.dims),p=L("total_seq_lens",i.dataType,i.dims),h=ne("pos_ids",u,d),m=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return` + ${o.registerUniforms(m).declareVariables(a,p,h)} + ${o.mainStart()} + ${o.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let total_sequence_length = u32(${p.getByOffset("0")}); let is_subsequent_prompt = uniforms.sequence_length > 1 && uniforms.sequence_length != total_sequence_length; let is_first_prompt = !is_subsequent_prompt && uniforms.sequence_length == total_sequence_length; @@ -3780,10 +3780,10 @@ fn calculateOutputIndex(index: u32) -> u32 { ${h.setByOffset("global_idx","seqlen")} }; } - `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:s}),getShaderSource:r}},i2=(e,t)=>{if(e.inputs.length>14&&e.inputs[14]||e.inputs.length>15&&e.inputs[15])throw new Error("GroupQueryAttention (JSEP): q_norm_weight / k_norm_weight inputs are not supported. The per-head Q/K RMS normalization prologue is implemented only on the CUDA and native WebGPU EPs.");let n=vb(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let o=e.inputs[0],u=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,l=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,d=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,c=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,s=e.inputs.length>4?e.inputs[5]:void 0,r=e.inputs.length>5?e.inputs[6]:void 0,i=n.kvNumHeads?n.kvNumHeads:n.numHeads,a=Se({axis:2,numOutputs:3,splitSizes:[n.numHeads*n.headSize,i*n.headSize,i*n.headSize]}),[p,h,m]=!u&&!l?e.compute(wl([o],a),{inputs:[o],outputs:[-1,-1,-1]}):[o,u,l],b,x;if(t.doRotary){let T=e.compute($b(n.batchSize,n.sequenceLength,s,r),{inputs:[s,r],outputs:[-1]})[0],I=e.inputs[7],A=e.inputs[8],P=Se({interleaved:t.rotaryInterleaved!==0,numHeads:n.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),O=[p,T,I,A],B=[-1];b=e.compute(Ro(O,P),{inputs:O,outputs:B})[0],O.splice(0,1,h);let U=Se({interleaved:t.rotaryInterleaved!==0,numHeads:n.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});x=e.compute(Ro(O,U),{inputs:O,outputs:B})[0]}let w=yi(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,t.doRotary?b:p,void 0,0),y=Ya(e,t.doRotary?x:h,n),S=Ya(e,m,n);Oi(e,w,y,S,void 0,void 0,d,c,void 0,n,s,r)}}),Qa,Tb,Sb,o2,JA=R(()=>{"use strict";se(),ae(),$r(),ue(),Qa=(e,t,n,o,u,l,d,c)=>{let s=Ue(l),r=s===1?"f32":`vec${s}f`,i=s===1?"vec2f":`mat2x${s}f`,a=u*d,p=64;a===1&&(p=256);let h=[u,d,l/s],m=[u,d,2],b=["rank","type","type"],x=[];x.push(...oe(h,m));let w=y=>{let S=j("x",t.dataType,3,s),T=j("scale",n.dataType,n.dims),I=j("bias",o.dataType,o.dims),A=re("output",1,3,2),P=[S,T,I,A];return` - var workgroup_shared : array<${i}, ${p}>; + `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:s}),getShaderSource:r}},c2=(e,t)=>{if(e.inputs.length>14&&e.inputs[14]||e.inputs.length>15&&e.inputs[15])throw new Error("GroupQueryAttention (JSEP): q_norm_weight / k_norm_weight inputs are not supported. The per-head Q/K RMS normalization prologue is implemented only on the CUDA and native WebGPU EPs.");let n=Eb(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let i=e.inputs[0],u=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,l=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,d=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,c=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,s=e.inputs.length>4?e.inputs[5]:void 0,r=e.inputs.length>5?e.inputs[6]:void 0,o=n.kvNumHeads?n.kvNumHeads:n.numHeads,a=ke({axis:2,numOutputs:3,splitSizes:[n.numHeads*n.headSize,o*n.headSize,o*n.headSize]}),[p,h,m]=!u&&!l?e.compute($l([i],a),{inputs:[i],outputs:[-1,-1,-1]}):[i,u,l],g,v;if(t.doRotary){let S=e.compute(kb(n.batchSize,n.sequenceLength,s,r),{inputs:[s,r],outputs:[-1]})[0],O=e.inputs[7],A=e.inputs[8],k=ke({interleaved:t.rotaryInterleaved!==0,numHeads:n.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),I=[p,S,O,A],R=[-1];g=e.compute(Lo(I,k),{inputs:I,outputs:R})[0],I.splice(0,1,h);let M=ke({interleaved:t.rotaryInterleaved!==0,numHeads:n.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});v=e.compute(Lo(I,M),{inputs:I,outputs:R})[0]}let w=Si(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,t.doRotary?g:p,void 0,0),y=ru(e,t.doRotary?v:h,n),T=ru(e,m,n);Ri(e,w,y,T,void 0,void 0,d,c,void 0,n,s,r)}}),nu,Pb,Cb,p2,lk=N(()=>{"use strict";le(),pe(),Ar(),he(),nu=(e,t,n,i,u,l,d,c)=>{let s=qe(l),r=s===1?"f32":`vec${s}f`,o=s===1?"vec2f":`mat2x${s}f`,a=u*d,p=64;a===1&&(p=256);let h=[u,d,l/s],m=[u,d,2],g=["rank","type","type"],v=[];v.push(...ae(h,m));let w=y=>{let T=L("x",t.dataType,3,s),S=L("scale",n.dataType,n.dims),O=L("bias",i.dataType,i.dims),A=ne("output",1,3,2),k=[T,S,O,A];return` + var workgroup_shared : array<${o}, ${p}>; const workgroup_size = ${p}u; - ${y.declareVariables(...P)} + ${y.declareVariables(...k)} ${y.mainStart(p)} let batch = workgroup_index / uniforms.x_shape[1]; let channel = workgroup_index % uniforms.x_shape[1]; @@ -3792,11 +3792,11 @@ fn calculateOutputIndex(index: u32) -> u32 { var sum = ${r}(0); var squared_sum = ${r}(0); for (var h = local_idx; h < hight; h += workgroup_size) { - let value = ${r}(${S.get("batch","channel","h")}); + let value = ${r}(${T.get("batch","channel","h")}); sum += value; squared_sum += value * value; } - workgroup_shared[local_idx] = ${i}(sum, squared_sum); + workgroup_shared[local_idx] = ${o}(sum, squared_sum); workgroupBarrier(); for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { @@ -3806,27 +3806,27 @@ fn calculateOutputIndex(index: u32) -> u32 { workgroupBarrier(); } if (local_idx == 0) { - let sum_final = ${vr("workgroup_shared[0][0]",s)} / f32(hight * ${s}); - let squared_sum_final = ${vr("workgroup_shared[0][1]",s)} / f32(hight * ${s}); + let sum_final = ${Or("workgroup_shared[0][0]",s)} / f32(hight * ${s}); + let squared_sum_final = ${Or("workgroup_shared[0][1]",s)} / f32(hight * ${s}); let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${c})); let channel_scale = inv_std_dev * f32(scale[channel]); let channel_shift = f32(bias[channel]) - sum_final * channel_scale; output[workgroup_index] = vec2f(channel_scale, channel_shift); } - }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${s};${c};${p}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:m,dataType:1}],dispatchGroup:{x:a},programUniforms:x}),getShaderSource:w},{inputs:[t,n,o],outputs:[-1]})[0]},Tb=(e,t,n)=>{let o=t[0].dims,u=o,l=2,d=o[0],c=o[1],s=z.sizeFromDimension(o,l),r=Ue(s),i=z.size(u)/r,a=Qa(e,t[0],t[1],t[2],d,s,c,n.epsilon),p=[d,c,s/r],h=[d,c],m=["type","none"],b=x=>{let w=j("x",t[0].dataType,p.length,r),y=j("scale_shift",1,h.length,2),S=re("output",t[0].dataType,p.length,r),T=[w,y,S];return` - ${x.registerUniform("output_size","u32").declareVariables(...T)} - ${x.mainStart()} - ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let outputIndices = ${S.offsetToIndices("global_idx")}; + }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${s};${c};${p}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:m,dataType:1}],dispatchGroup:{x:a},programUniforms:v}),getShaderSource:w},{inputs:[t,n,i],outputs:[-1]})[0]},Pb=(e,t,n)=>{let i=t[0].dims,u=i,l=2,d=i[0],c=i[1],s=B.sizeFromDimension(i,l),r=qe(s),o=B.size(u)/r,a=nu(e,t[0],t[1],t[2],d,s,c,n.epsilon),p=[d,c,s/r],h=[d,c],m=["type","none"],g=v=>{let w=L("x",t[0].dataType,p.length,r),y=L("scale_shift",1,h.length,2),T=ne("output",t[0].dataType,p.length,r),S=[w,y,T];return` + ${v.registerUniform("output_size","u32").declareVariables(...S)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let outputIndices = ${T.offsetToIndices("global_idx")}; let batch = outputIndices[0]; let channel = outputIndices[1]; let scale_shift = ${y.getByIndices("vec2(batch, channel)")}; - let value = ${w.getByOffset("global_idx")} * ${S.type.value}(scale_shift.x) + ${S.type.value}(scale_shift.y); - ${S.setByOffset("global_idx","value")}; - }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${r}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...oe(p,h,p)]}),getShaderSource:b},{inputs:[t[0],a]})},Sb=(e,t,n)=>{let o=t[0].dims,u=o,l=o[0],d=o[o.length-1],c=z.sizeFromDimension(o,1)/d,s=Ue(d),r=z.size(u)/s,i=[{type:12,data:c},{type:12,data:Math.floor(d/s)}],a=["type","type"],p=!1,h=[0,o.length-1];for(let w=0;wo[h[y]])),b=Qa(e,m,t[1],t[2],l,c,d,n.epsilon),x=w=>{let y=Ye(t[0].dataType),S=s===1?"vec2f":`mat${s}x2f`,T=P=>{let O=P===0?"x":"y",B=s===1?"f32":`vec${s}f`;switch(s){case 1:return`${y}(${B}(scale.${O}))`;case 2:return`vec2<${y}>(${B}(scale[0].${O}, scale[1].${O}))`;case 4:return`vec4<${y}>(${B}(scale[0].${O}, scale[1].${O}, scale[2].${O}, scale[3].${O}))`;default:throw new Error(`Not supported compoents ${s}`)}},I=j("input",t[0].dataType,t[0].dims,s),A=re("output",t[0].dataType,u,s);return` - @group(0) @binding(0) var input : array<${I.type.storage}>; - @group(0) @binding(1) var scale_input : array<${S}>; + let value = ${w.getByOffset("global_idx")} * ${T.type.value}(scale_shift.x) + ${T.type.value}(scale_shift.y); + ${T.setByOffset("global_idx","value")}; + }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${r}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},...ae(p,h,p)]}),getShaderSource:g},{inputs:[t[0],a]})},Cb=(e,t,n)=>{let i=t[0].dims,u=i,l=i[0],d=i[i.length-1],c=B.sizeFromDimension(i,1)/d,s=qe(d),r=B.size(u)/s,o=[{type:12,data:c},{type:12,data:Math.floor(d/s)}],a=["type","type"],p=!1,h=[0,i.length-1];for(let w=0;wi[h[y]])),g=nu(e,m,t[1],t[2],l,c,d,n.epsilon),v=w=>{let y=rt(t[0].dataType),T=s===1?"vec2f":`mat${s}x2f`,S=k=>{let I=k===0?"x":"y",R=s===1?"f32":`vec${s}f`;switch(s){case 1:return`${y}(${R}(scale.${I}))`;case 2:return`vec2<${y}>(${R}(scale[0].${I}, scale[1].${I}))`;case 4:return`vec4<${y}>(${R}(scale[0].${I}, scale[1].${I}, scale[2].${I}, scale[3].${I}))`;default:throw new Error(`Not supported compoents ${s}`)}},O=L("input",t[0].dataType,t[0].dims,s),A=ne("output",t[0].dataType,u,s);return` + @group(0) @binding(0) var input : array<${O.type.storage}>; + @group(0) @binding(1) var scale_input : array<${T}>; @group(0) @binding(2) var output : array<${A.type.storage}>; struct Uniforms {H: u32, C : u32}; @group(0) @binding(3) var uniforms: Uniforms; @@ -3837,60 +3837,60 @@ fn calculateOutputIndex(index: u32) -> u32 { let scale_offset = current_image_number * uniforms.C + current_channel_number; let scale = scale_input[scale_offset]; - output[global_idx] = fma(input[global_idx], ${T(0)}, ${T(1)}); - }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${s}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:i}),getShaderSource:x},{inputs:[t[0],b]})},o2=(e,t)=>{t.format==="NHWC"?Sb(e,e.inputs,t):Tb(e,e.inputs,t)}}),Ib,Ob,s2,YA=R(()=>{"use strict";se(),ae(),ue(),Ib=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},Ob=(e,t,n)=>{let o=t.simplified,u=e[0].dims,l=e[1],d=!o&&e[2],c=u,s=z.normalizeAxis(t.axis,u.length),r=z.sizeToDimension(u,s),i=z.sizeFromDimension(u,s),a=z.size(l.dims),p=d?z.size(d.dims):0;if(a!==i||d&&p!==i)throw new Error(`Size of X.shape()[axis:] == ${i}. + output[global_idx] = fma(input[global_idx], ${S(0)}, ${S(1)}); + }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${s}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:o}),getShaderSource:v},{inputs:[t[0],g]})},p2=(e,t)=>{t.format==="NHWC"?Cb(e,e.inputs,t):Pb(e,e.inputs,t)}}),Db,Rb,h2,dk=N(()=>{"use strict";le(),pe(),he(),Db=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},Rb=(e,t,n)=>{let i=t.simplified,u=e[0].dims,l=e[1],d=!i&&e[2],c=u,s=B.normalizeAxis(t.axis,u.length),r=B.sizeToDimension(u,s),o=B.sizeFromDimension(u,s),a=B.size(l.dims),p=d?B.size(d.dims):0;if(a!==o||d&&p!==o)throw new Error(`Size of X.shape()[axis:] == ${o}. Size of scale and bias (if provided) must match this. - Got scale size of ${a} and bias size of ${p}`);let h=[];for(let I=0;I1,y=n>2,S=I=>{let A=Ye(e[0].dataType),P=[j("x",e[0].dataType,e[0].dims,m),j("scale",l.dataType,l.dims,m)];d&&P.push(j("bias",d.dataType,d.dims,m)),P.push(re("output",e[0].dataType,c,m)),w&&P.push(re("mean_data_output",1,h)),y&&P.push(re("inv_std_output",1,h));let O=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` - ${I.registerUniforms(O).declareVariables(...P)} - ${I.mainStart()} - ${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} + Got scale size of ${a} and bias size of ${p}`);let h=[];for(let O=0;O1,y=n>2,T=O=>{let A=rt(e[0].dataType),k=[L("x",e[0].dataType,e[0].dims,m),L("scale",l.dataType,l.dims,m)];d&&k.push(L("bias",d.dataType,d.dims,m)),k.push(ne("output",e[0].dataType,c,m)),w&&k.push(ne("mean_data_output",1,h)),y&&k.push(ne("inv_std_output",1,h));let I=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` + ${O.registerUniforms(I).declareVariables(...k)} + ${O.mainStart()} + ${O.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} let offset = global_idx * uniforms.norm_size_vectorized; - var mean_vector = ${dl("f32",m)}; - var mean_square_vector = ${dl("f32",m)}; + var mean_vector = ${fl("f32",m)}; + var mean_square_vector = ${fl("f32",m)}; for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { - let value = ${$n(A,m,"x[h + offset]")}; + let value = ${Pn(A,m,"x[h + offset]")}; mean_vector += value; mean_square_vector += value * value; } - let mean = ${vr("mean_vector",m)} / uniforms.norm_size; - let inv_std_dev = inverseSqrt(${vr("mean_square_vector",m)} / uniforms.norm_size ${o?"":"- mean * mean"} + uniforms.epsilon); + let mean = ${Or("mean_vector",m)} / uniforms.norm_size; + let inv_std_dev = inverseSqrt(${Or("mean_square_vector",m)} / uniforms.norm_size ${i?"":"- mean * mean"} + uniforms.epsilon); for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { - let f32input = ${$n(A,m,"x[j + offset]")}; - let f32scale = ${$n(A,m,"scale[j]")}; - output[j + offset] = ${P[0].type.value}((f32input ${o?"":"- mean"}) * inv_std_dev * f32scale - ${d?`+ ${$n(A,m,"bias[j]")}`:""} + let f32input = ${Pn(A,m,"x[j + offset]")}; + let f32scale = ${Pn(A,m,"scale[j]")}; + output[j + offset] = ${k[0].type.value}((f32input ${i?"":"- mean"}) * inv_std_dev * f32scale + ${d?`+ ${Pn(A,m,"bias[j]")}`:""} ); } ${w?"mean_data_output[global_idx] = mean":""}; ${y?"inv_std_output[global_idx] = inv_std_dev":""}; - }`},T=[{dims:c,dataType:e[0].dataType}];return w&&T.push({dims:h,dataType:1}),y&&T.push({dims:h,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${m};${n};${o}`,inputDependencies:b},getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(r/64)},programUniforms:x}),getShaderSource:S}},s2=(e,t)=>{Ib(e.inputs),e.compute(Ob(e.inputs,t,e.outputCount))}}),Eb,a2,QA=R(()=>{"use strict";ae(),Zl(),Jl(),Eb=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},a2=e=>{Eb(e.inputs);let t=In.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let n=t[t.length-1],o=e.inputs[0].dims[e.inputs[0].dims.length-1];if(n<8&&o<8)e.compute(Xl(e.inputs,{activation:""},t));else{let u=t[t.length-2],l=z.size(e.inputs[0].dims.slice(0,-2)),d=z.size(e.inputs[1].dims.slice(0,-2));if(l!==1&&u===1&&d===1){let c=e.inputs[0].reshape([1,l,o]),s=e.inputs[1].reshape([1,o,n]),r=[1,l,n],i=[c,s];e.compute(Do(i,{activation:""},t,r),{inputs:i})}else e.compute(Do(e.inputs,{activation:""},t))}}}),Ab,kb,Pb,u2,l2,ek=R(()=>{"use strict";se(),ae(),Ve(),ue(),Ab=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let n=e[0],o=n.dims.length;if(n.dims[o-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let u=Math.floor((t.k+t.blockSize-1)/t.blockSize),l=t.blockSize/8*t.bits,d=e[1];if(!z.areEqual(d.dims,[t.n,u,l]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let c=e[2].dims;if(z.size(c)!==t.n*u)throw new Error("scales input size error.");if(e.length===4){let s=e[3].dims,r=t.n*(t.bits===8?u:Math.floor((u*t.bits+7)/8));if(z.size(s)!==r)throw new Error("zeroPoints input size error.")}},kb=(e,t)=>{let n=e[0].dims,o=n.length,u=n[o-2],l=t.k,d=t.n,c=n.slice(0,o-2),s=z.size(c),r=e[1].dims[2]/4,i=e[0].dataType,a=Ue(t.k),p=Ue(r),h=Ue(d),m=c.concat([u,d]),b=u>1&&d/h%2===0?2:1,x=z.size(m)/h/b,w=64,y=[],S=[s,u,l/a],T=z.convertShape(e[1].dims).slice();T.splice(-1,1,r/p),y.push(...oe(S)),y.push(...oe(T)),y.push(...oe(e[2].dims)),e.length===4&&y.push(...oe(z.convertShape(e[3].dims)));let I=[s,u,d/h];y.push(...oe(I));let A=P=>{let O=S.length,B=j("a",e[0].dataType,O,a),U=j("b",12,T.length,p),X=j("scales",e[2].dataType,e[2].dims.length),q=[B,U,X],D=e.length===4?j("zero_points",12,e[3].dims.length):void 0;D&&q.push(D);let Q=I.length,_=re("output",e[0].dataType,Q,h),C=Ye(e[0].dataType),F=(()=>{switch(a){case 1:return`array<${C}, 8>`;case 2:return`mat4x2<${C}>`;case 4:return`mat2x4<${C}>`;default:throw new Error(`${a}-component is not supported.`)}})(),V=Math.floor(32/t.bits),G=Math.floor(V/8),H=()=>{let Z="";for(let W=0;W0?W:""} = ${W===0?B.indicesToOffset(`${B.type.indices}(batch, row, word_offset)`):"input_offset"}; - var a_data${W>0?W:""}: ${F}; - for (var j${W>0?W:""}: u32 = 0; j${W>0?W:""} < ${8/a}; j${W>0?W:""}++) { - a_data${W>0?W:""}[j${W>0?W:""}] = ${B.getByOffset(`input_offset${W>0?W:""}`)}; - input_offset${W>0?W:""}++; + }`},S=[{dims:c,dataType:e[0].dataType}];return w&&S.push({dims:h,dataType:1}),y&&S.push({dims:h,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${m};${n};${i}`,inputDependencies:g},getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(r/64)},programUniforms:v}),getShaderSource:T}},h2=(e,t)=>{Db(e.inputs),e.compute(Rb(e.inputs,t,e.outputCount))}}),Nb,f2,ck=N(()=>{"use strict";pe(),ed(),td(),Nb=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},f2=e=>{Nb(e.inputs);let t=Rn.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let n=t[t.length-1],i=e.inputs[0].dims[e.inputs[0].dims.length-1];if(n<8&&i<8)e.compute(Ql(e.inputs,{activation:""},t));else{let u=t[t.length-2],l=B.size(e.inputs[0].dims.slice(0,-2)),d=B.size(e.inputs[1].dims.slice(0,-2));if(l!==1&&u===1&&d===1){let c=e.inputs[0].reshape([1,l,i]),s=e.inputs[1].reshape([1,i,n]),r=[1,l,n],o=[c,s];e.compute(jo(o,{activation:""},t,r),{inputs:o})}else e.compute(jo(e.inputs,{activation:""},t))}}}),zb,Mb,Bb,m2,g2,pk=N(()=>{"use strict";le(),pe(),He(),he(),zb=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let n=e[0],i=n.dims.length;if(n.dims[i-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let u=Math.floor((t.k+t.blockSize-1)/t.blockSize),l=t.blockSize/8*t.bits,d=e[1];if(!B.areEqual(d.dims,[t.n,u,l]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let c=e[2].dims;if(B.size(c)!==t.n*u)throw new Error("scales input size error.");if(e.length===4){let s=e[3].dims,r=t.n*(t.bits===8?u:Math.floor((u*t.bits+7)/8));if(B.size(s)!==r)throw new Error("zeroPoints input size error.")}},Mb=(e,t)=>{let n=e[0].dims,i=n.length,u=n[i-2],l=t.k,d=t.n,c=n.slice(0,i-2),s=B.size(c),r=e[1].dims[2]/4,o=e[0].dataType,a=qe(t.k),p=qe(r),h=qe(d),m=c.concat([u,d]),g=u>1&&d/h%2===0?2:1,v=B.size(m)/h/g,w=64,y=[],T=[s,u,l/a],S=B.convertShape(e[1].dims).slice();S.splice(-1,1,r/p),y.push(...ae(T)),y.push(...ae(S)),y.push(...ae(e[2].dims)),e.length===4&&y.push(...ae(B.convertShape(e[3].dims)));let O=[s,u,d/h];y.push(...ae(O));let A=k=>{let I=T.length,R=L("a",e[0].dataType,I,a),M=L("b",12,S.length,p),X=L("scales",e[2].dataType,e[2].dims.length),G=[R,M,X],C=e.length===4?L("zero_points",12,e[3].dims.length):void 0;C&&G.push(C);let W=O.length,_=ne("output",e[0].dataType,W,h),D=rt(e[0].dataType),V=(()=>{switch(a){case 1:return`array<${D}, 8>`;case 2:return`mat4x2<${D}>`;case 4:return`mat2x4<${D}>`;default:throw new Error(`${a}-component is not supported.`)}})(),q=Math.floor(32/t.bits),H=Math.floor(q/8),K=()=>{let Y="";for(let j=0;j0?j:""} = ${j===0?R.indicesToOffset(`${R.type.indices}(batch, row, word_offset)`):"input_offset"}; + var a_data${j>0?j:""}: ${V}; + for (var j${j>0?j:""}: u32 = 0; j${j>0?j:""} < ${8/a}; j${j>0?j:""}++) { + a_data${j>0?j:""}[j${j>0?j:""}] = ${R.getByOffset(`input_offset${j>0?j:""}`)}; + input_offset${j>0?j:""}++; } - `;for(let ye=0;ye> ${W*16}u; + let half_word = b_value >> ${j*16}u; let byte_lo = half_word & 0xFFu; let byte_hi = (half_word >> 8u) & 0xFFu; let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u); b_value_lower = unpack4xU8(spread_word & b_mask); b_value_upper = unpack4xU8((spread_word >> 2u) & b_mask); - }`:`b_value_lower = unpack4xU8((b_value >> ${pe}u) & b_mask); - b_value_upper = unpack4xU8((b_value >> ${fe}u) & b_mask);`} - b_quantized_values = ${F}(${Array.from({length:4},(Fe,Oe)=>`${C}(b_value_lower[${Oe}]), ${C}(b_value_upper[${Oe}])`).join(", ")}); - b_dequantized_values = ${a===1?`${F}(${Array.from({length:8},(Fe,Oe)=>`(b_quantized_values[${Oe}] - ${D?`zero_point${ye}`:"zero_point"}) * scale${ye}`).join(", ")});`:`(b_quantized_values - ${F}(${Array(8).fill(`${D?`zero_point${ye}`:"zero_point"}`).join(",")})) * scale${ye};`}; - workgroup_shared[local_id.x * ${b} + ${Math.floor(ye/h)}]${h>1?`[${ye%h}]`:""} += ${Array.from({length:8/a},(Fe,Oe)=>`${a===1?`a_data${W>0?W:""}[${Oe}] * b_dequantized_values[${Oe}]`:`dot(a_data${W>0?W:""}[${Oe}], b_dequantized_values[${Oe}])`}`).join(" + ")}; - `}return Z},M=()=>{let Z=` + }`:`b_value_lower = unpack4xU8((b_value >> ${ee}u) & b_mask); + b_value_upper = unpack4xU8((b_value >> ${ie}u) & b_mask);`} + b_quantized_values = ${V}(${Array.from({length:4},(ce,de)=>`${D}(b_value_lower[${de}]), ${D}(b_value_upper[${de}])`).join(", ")}); + b_dequantized_values = ${a===1?`${V}(${Array.from({length:8},(ce,de)=>`(b_quantized_values[${de}] - ${C?`zero_point${ue}`:"zero_point"}) * scale${ue}`).join(", ")});`:`(b_quantized_values - ${V}(${Array(8).fill(`${C?`zero_point${ue}`:"zero_point"}`).join(",")})) * scale${ue};`}; + workgroup_shared[local_id.x * ${g} + ${Math.floor(ue/h)}]${h>1?`[${ue%h}]`:""} += ${Array.from({length:8/a},(ce,de)=>`${a===1?`a_data${j>0?j:""}[${de}] * b_dequantized_values[${de}]`:`dot(a_data${j>0?j:""}[${de}], b_dequantized_values[${de}])`}`).join(" + ")}; + `}return Y},F=()=>{let Y=` var col_index = col * ${h}; - ${D?` + ${C?` let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u; let zero_point_bytes_per_col = (nBlocksPerCol + zero_point_values_per_byte - 1u) / zero_point_values_per_byte; var zero_point_byte_count: u32; @@ -3900,29 +3900,29 @@ fn calculateOutputIndex(index: u32) -> u32 { var zero_point_bits_offset: u32; var zero_point_word: u32;`:` // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization. - let zero_point = ${C}(${Math.pow(2,t.bits-1).toFixed(1)});`} - `;for(let W=0;W> 0x2u; zero_point_byte_offset = zero_point_byte_count & 0x3u; zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u); - zero_point_word = ${D.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; - let zero_point${W} = ${C}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:""} - col_index += 1;`;return Z},J=()=>{let Z=`col_index = col * ${h};`;for(let W=0;W> zero_point_bits_offset; + let zero_point${j} = ${D}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:""} + col_index += 1;`;return Y},Q=()=>{let Y=`col_index = col * ${h};`;for(let j=0;j; var b_value_upper: vec4; - var b_quantized_values: ${F}; - var b_dequantized_values: ${F};`,Z};return` - var workgroup_shared: array<${_.type.value}, ${b*w}>; - ${P.declareVariables(...q,_)} - ${P.mainStart([w,1,1])} - let output_indices = ${_.offsetToIndices(`(global_idx / ${w}) * ${b}`)}; + var b_quantized_values: ${V}; + var b_dequantized_values: ${V};`,Y};return` + var workgroup_shared: array<${_.type.value}, ${g*w}>; + ${k.declareVariables(...G,_)} + ${k.mainStart([w,1,1])} + let output_indices = ${_.offsetToIndices(`(global_idx / ${w}) * ${g}`)}; let col = output_indices[2]; let row = output_indices[1]; let batch = output_indices[0]; @@ -3931,64 +3931,64 @@ fn calculateOutputIndex(index: u32) -> u32 { for (var block = local_id.x; block < nBlocksPerCol; block += ${w}) { //process one block var word_offset: u32 = block * ${t.blockSize/a}; - ${M()} + ${F()} for (var word: u32 = 0; word < ${r}; word += ${p}) { - ${J()} + ${Q()} for (var i: u32 = 0; i < ${p}; i++) { - ${H()} - word_offset += ${V/a}; + ${K()} + word_offset += ${q/a}; } } } workgroupBarrier(); - if (local_id.x < ${b}) { + if (local_id.x < ${g}) { var output_value: ${_.type.value} = ${_.type.value}(0); var workgroup_shared_offset: u32 = local_id.x; for (var b: u32 = 0u; b < ${w}u; b++) { output_value += workgroup_shared[workgroup_shared_offset]; - workgroup_shared_offset += ${b}; + workgroup_shared_offset += ${g}; } ${_.setByIndices(`${_.type.indices}(batch, row, col + local_id.x)`,"output_value")}; } - }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${a};${p};${h};${b};${w}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:m,dataType:i}],dispatchGroup:{x},programUniforms:y}),getShaderSource:A}},Pb=(e,t)=>{let n=e[0].dims,o=n.length,u=n[o-2],l=t.k,d=t.n,c=n.slice(0,o-2),s=z.size(c),r=e[1].dims[2]/4,i=e[0].dataType,a=Ue(t.k),p=Ue(r),h=c.concat([u,d]),m=128,b=d%8===0?8:d%4===0?4:1,x=m/b,w=Math.floor(32/t.bits),y=x*p*w,S=y/a,T=y/t.blockSize,I=z.size(h)/b,A=[],P=[s,u,l/a],O=z.convertShape(e[1].dims).slice();O.splice(-1,1,r/p),A.push(...oe(P)),A.push(...oe(O)),A.push(...oe(e[2].dims)),e.length===4&&A.push(...oe(z.convertShape(e[3].dims)));let B=[s,u,d];A.push(...oe(B));let U=X=>{let q=P.length,D=j("a",e[0].dataType,q,a),Q=j("b",12,O.length,p),_=j("scales",e[2].dataType,e[2].dims.length),C=[D,Q,_],F=e.length===4?j("zero_points",12,e[3].dims.length):void 0;F&&C.push(F);let V=B.length,G=re("output",e[0].dataType,V),H=Ye(e[0].dataType),M=()=>{switch(a){case 1:return` - let a_data0 = vec4<${H}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); - let a_data1 = vec4<${H}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` - let a_data0 = vec4<${H}>(sub_a[word_offset], sub_a[word_offset + 1]); - let a_data1 = vec4<${H}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` + }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${a};${p};${h};${g};${w}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:m,dataType:o}],dispatchGroup:{x:v},programUniforms:y}),getShaderSource:A}},Bb=(e,t)=>{let n=e[0].dims,i=n.length,u=n[i-2],l=t.k,d=t.n,c=n.slice(0,i-2),s=B.size(c),r=e[1].dims[2]/4,o=e[0].dataType,a=qe(t.k),p=qe(r),h=c.concat([u,d]),m=128,g=d%8===0?8:d%4===0?4:1,v=m/g,w=Math.floor(32/t.bits),y=v*p*w,T=y/a,S=y/t.blockSize,O=B.size(h)/g,A=[],k=[s,u,l/a],I=B.convertShape(e[1].dims).slice();I.splice(-1,1,r/p),A.push(...ae(k)),A.push(...ae(I)),A.push(...ae(e[2].dims)),e.length===4&&A.push(...ae(B.convertShape(e[3].dims)));let R=[s,u,d];A.push(...ae(R));let M=X=>{let G=k.length,C=L("a",e[0].dataType,G,a),W=L("b",12,I.length,p),_=L("scales",e[2].dataType,e[2].dims.length),D=[C,W,_],V=e.length===4?L("zero_points",12,e[3].dims.length):void 0;V&&D.push(V);let q=R.length,H=ne("output",e[0].dataType,q),K=rt(e[0].dataType),F=()=>{switch(a){case 1:return` + let a_data0 = vec4<${K}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); + let a_data1 = vec4<${K}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` + let a_data0 = vec4<${K}>(sub_a[word_offset], sub_a[word_offset + 1]); + let a_data1 = vec4<${K}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` let a_data0 = sub_a[word_offset]; let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${a}-component is not supported.`)}};return` - var sub_a: array<${D.type.value}, ${S}>; - var inter_results: array, ${b}>; - ${X.declareVariables(...C,G)} - ${X.mainStart([x,b,1])} - let output_indices = ${G.offsetToIndices(`workgroup_index * ${b}`)}; + var sub_a: array<${C.type.value}, ${T}>; + var inter_results: array, ${g}>; + ${X.declareVariables(...D,H)} + ${X.mainStart([v,g,1])} + let output_indices = ${H.offsetToIndices(`workgroup_index * ${g}`)}; let col = output_indices[2]; let row = output_indices[1]; let batch = output_indices[0]; let n_blocks_per_col = uniforms.b_shape[1]; - let num_tiles = (n_blocks_per_col - 1) / ${T} + 1; + let num_tiles = (n_blocks_per_col - 1) / ${S} + 1; // Loop over shared dimension. for (var tile: u32 = 0; tile < num_tiles; tile += 1) { - let a_col_start = tile * ${S}; + let a_col_start = tile * ${T}; // load one tile A data into shared memory. - for (var a_offset = local_idx; a_offset < ${S}; a_offset += ${m}) + for (var a_offset = local_idx; a_offset < ${T}; a_offset += ${m}) { let a_col = a_col_start + a_offset; if (a_col < uniforms.a_shape[2]) { - sub_a[a_offset] = ${D.getByIndices(`${D.type.indices}(batch, row, a_col)`)}; + sub_a[a_offset] = ${C.getByIndices(`${C.type.indices}(batch, row, a_col)`)}; } else { - sub_a[a_offset] = ${D.type.value}(0); + sub_a[a_offset] = ${C.type.value}(0); } } workgroupBarrier(); // each thread process one block let b_row = col + local_id.y; - let block = tile * ${T} + local_id.x; - ${F?` + let block = tile * ${S} + local_id.x; + ${V?` let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u; let zero_point_bytes_per_col = (n_blocks_per_col + zero_point_values_per_byte - 1u) / zero_point_values_per_byte; let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block / zero_point_values_per_byte); @@ -3996,119 +3996,119 @@ fn calculateOutputIndex(index: u32) -> u32 { let zero_point_byte_offset = zero_point_byte_count & 0x3u; let zero_point_sub_offset: u32 = block % zero_point_values_per_byte; let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u); - let zero_point_word = ${F.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; - let zero_point = ${H}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:` + let zero_point_word = ${V.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point = ${K}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:` // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization. - let zero_point = ${H}(${Math.pow(2,t.bits-1).toFixed(1)});`} + let zero_point = ${K}(${Math.pow(2,t.bits-1).toFixed(1)});`} let scale = ${_.getByOffset("b_row * n_blocks_per_col + block")}; - let b_data = ${Q.getByIndices(`${Q.type.indices}(b_row, block, 0)`)}; + let b_data = ${W.getByIndices(`${W.type.indices}(b_row, block, 0)`)}; var word_offset = local_id.x * ${t.blockSize/a}; for (var i: u32 = 0; i < ${p}; i++) { let b_value = ${p===1?"b_data":"b_data[i]"}; - ${(()=>{let J=Math.floor(w/8),Z="";for(let W=0;W{let Q=Math.floor(w/8),Y="";for(let j=0;j> ${W*16}u; + let half_word = b_value >> ${j*16}u; let byte_lo = half_word & 0xFFu; let byte_hi = (half_word >> 8u) & 0xFFu; let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u); let b_value_lower = unpack4xU8(spread_word & 0x03030303u); let b_value_upper = unpack4xU8((spread_word >> 2u) & 0x03030303u);`:` - let b_value_lower = unpack4xU8((b_value >> ${pe}u) & 0x0F0F0F0Fu); - let b_value_upper = unpack4xU8((b_value >> ${fe}u) & 0x0F0F0F0Fu);`} - let b_quantized_values = mat2x4<${H}>(${Array.from({length:4},(ye,Fe)=>`${H}(b_value_lower[${Fe}]), ${H}(b_value_upper[${Fe}])`).join(", ")}); - let b_dequantized_values = (b_quantized_values - mat2x4<${H}>(${Array(8).fill("zero_point").join(",")})) * scale; - inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(ye,Fe)=>`${`dot(a_data${Fe}, b_dequantized_values[${Fe}])`}`).join(" + ")}; + let b_value_lower = unpack4xU8((b_value >> ${ee}u) & 0x0F0F0F0Fu); + let b_value_upper = unpack4xU8((b_value >> ${ie}u) & 0x0F0F0F0Fu);`} + let b_quantized_values = mat2x4<${K}>(${Array.from({length:4},(ue,ce)=>`${K}(b_value_lower[${ce}]), ${K}(b_value_upper[${ce}])`).join(", ")}); + let b_dequantized_values = (b_quantized_values - mat2x4<${K}>(${Array(8).fill("zero_point").join(",")})) * scale; + inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(ue,ce)=>`${`dot(a_data${ce}, b_dequantized_values[${ce}])`}`).join(" + ")}; } - word_offset += ${8/a};`}return Z})()} + word_offset += ${8/a};`}return Y})()} } workgroupBarrier(); } - if (local_idx < ${b}) { - var output_value: ${G.type.value} = ${G.type.value}(0); - for (var b = 0u; b < ${x}; b++) { + if (local_idx < ${g}) { + var output_value: ${H.type.value} = ${H.type.value}(0); + for (var b = 0u; b < ${v}; b++) { output_value += inter_results[local_idx][b]; } if (col + local_idx < uniforms.output_shape[2]) { - ${G.setByIndices(`${G.type.indices}(batch, row, col + local_idx)`,"output_value")} + ${H.setByIndices(`${H.type.indices}(batch, row, col + local_idx)`,"output_value")} } } - }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${a};${p};${x};${b}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:h,dataType:i}],dispatchGroup:{x:I},programUniforms:A}),getShaderSource:U}},u2=(e,t)=>{Ab(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Pb(e.inputs,t)):e.compute(kb(e.inputs,t))},l2=e=>Se(e)}),Cb,Db,Rb,Nb,zb,Bb,Mb,Fb,d2,tk=R(()=>{"use strict";se(),ae(),ue(),Cb=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},Db=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ne("uniforms.pads",u,n)}; + }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${a};${p};${v};${g}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:h,dataType:o}],dispatchGroup:{x:O},programUniforms:A}),getShaderSource:M}},m2=(e,t)=>{zb(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Bb(e.inputs,t)):e.compute(Mb(e.inputs,t))},g2=e=>ke(e)}),Fb,jb,Lb,Ub,Vb,Gb,qb,Hb,b2,hk=N(()=>{"use strict";le(),pe(),he(),Fb=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},jb=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${oe("uniforms.pads",u,n)}; if (k < 0) { break; } - if (k >= i32(${ne("uniforms.x_shape",u,t)})) { + if (k >= i32(${oe("uniforms.x_shape",u,t)})) { break; } - offset += k * i32(${ne("uniforms.x_strides",u,t)}); + offset += k * i32(${oe("uniforms.x_strides",u,t)}); `;return` value = ${e.type.value}(uniforms.constant_value); for (var i = 0; i < 1; i++) { var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; } - `},Rb=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ne("uniforms.pads",u,n)}; + `},Lb=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${oe("uniforms.pads",u,n)}; if (k < 0) { k = -k; } { - let _2n_1 = 2 * (i32(${ne("uniforms.x_shape",u,t)}) - 1); + let _2n_1 = 2 * (i32(${oe("uniforms.x_shape",u,t)}) - 1); k = k % _2n_1; - if(k >= i32(${ne("uniforms.x_shape",u,t)})) { + if(k >= i32(${oe("uniforms.x_shape",u,t)})) { k = _2n_1 - k; } } - offset += k * i32(${ne("uniforms.x_strides",u,t)}); + offset += k * i32(${oe("uniforms.x_strides",u,t)}); `;return` var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; - `},Nb=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ne("uniforms.pads",u,n)}; + `},Ub=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${oe("uniforms.pads",u,n)}; if (k < 0) { k = 0; } - if (k >= i32(${ne("uniforms.x_shape",u,t)})) { - k = i32(${ne("uniforms.x_shape",u,t)}) - 1; + if (k >= i32(${oe("uniforms.x_shape",u,t)})) { + k = i32(${oe("uniforms.x_shape",u,t)}) - 1; } - offset += k * i32(${ne("uniforms.x_strides",u,t)}); + offset += k * i32(${oe("uniforms.x_strides",u,t)}); `;return` var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; - `},zb=(e,t,n)=>{let o="";for(let u=t-1;u>=0;--u)o+=` - k = i32(${e.indicesGet("indices",u)}) - ${ne("uniforms.pads",u,n)}; + `},Vb=(e,t,n)=>{let i="";for(let u=t-1;u>=0;--u)i+=` + k = i32(${e.indicesGet("indices",u)}) - ${oe("uniforms.pads",u,n)}; if (k < 0) { - k += i32(${ne("uniforms.x_shape",u,t)}]); + k += i32(${oe("uniforms.x_shape",u,t)}]); } - if (k >= i32(${ne("uniforms.x_shape",u,t)})) { - k -= i32(${ne("uniforms.x_shape",u,t)}); + if (k >= i32(${oe("uniforms.x_shape",u,t)})) { + k -= i32(${oe("uniforms.x_shape",u,t)}); } - offset += k * i32(${ne("uniforms.x_strides",u,t)}); + offset += k * i32(${oe("uniforms.x_strides",u,t)}); `;return` var offset = 0; var k = 0; - ${o} + ${i} value = x[offset]; - `},Bb=(e,t,n)=>{switch(n.mode){case 0:return Db(e,t,n.pads.length);case 1:return Rb(e,t,n.pads.length);case 2:return Nb(e,t,n.pads.length);case 3:return zb(e,t,n.pads.length);default:throw new Error("Invalid mode")}},Mb=(e,t)=>{let n=z.padShape(e[0].dims.slice(),t.pads),o=e[0].dims,u=z.size(n),l=[{type:12,data:u},{type:6,data:t.pads}],d=e.length>=3&&e[2].data;t.mode===0&&l.push({type:d?e[2].dataType:1,data:t.value}),l.push(...oe(e[0].dims,n));let c=["rank"],s=r=>{let i=re("output",e[0].dataType,n.length),a=j("x",e[0].dataType,o.length),p=a.type.value,h=Bb(i,o.length,t),m=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&m.push({name:"constant_value",type:d?p:"f32"}),` - ${r.registerUniforms(m).declareVariables(a,i)} + `},Gb=(e,t,n)=>{switch(n.mode){case 0:return jb(e,t,n.pads.length);case 1:return Lb(e,t,n.pads.length);case 2:return Ub(e,t,n.pads.length);case 3:return Vb(e,t,n.pads.length);default:throw new Error("Invalid mode")}},qb=(e,t)=>{let n=B.padShape(e[0].dims.slice(),t.pads),i=e[0].dims,u=B.size(n),l=[{type:12,data:u},{type:6,data:t.pads}],d=e.length>=3&&e[2].data;t.mode===0&&l.push({type:d?e[2].dataType:1,data:t.value}),l.push(...ae(e[0].dims,n));let c=["rank"],s=r=>{let o=ne("output",e[0].dataType,n.length),a=L("x",e[0].dataType,i.length),p=a.type.value,h=Gb(o,i.length,t),m=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&m.push({name:"constant_value",type:d?p:"f32"}),` + ${r.registerUniforms(m).declareVariables(a,o)} ${r.mainStart()} ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let indices = ${i.offsetToIndices("global_idx")}; + let indices = ${o.offsetToIndices("global_idx")}; var value = ${p}(0); ${h} output[global_idx] = value; - }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${d}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(z.size(n)/64)},programUniforms:l}),getShaderSource:s}},Fb=(e,t)=>{if(e.length>1){let n=e[1].getBigInt64Array(),o=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,u=e[0].dims.length,l=new Int32Array(2*u).fill(0);if(e.length>=4){let c=e[3].getBigInt64Array();for(let s=0;sl[Number(s)]=Number(c));let d=[];return l.forEach(c=>d.push(c)),{mode:t.mode,value:o,pads:d}}else return t},d2=(e,t)=>{Cb(e.inputs);let n=Fb(e.inputs,t);e.compute(Mb(e.inputs,n),{inputs:[0]})}}),ai,eu,tu,ru,nu,jb,Lb,iu,ou,c2,p2,su,h2,f2,au,m2,g2,b2,y2,rk=R(()=>{"use strict";ot(),se(),ae(),ue(),ai=e=>{if(le.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},eu=(e,t,n)=>{let o=t.format==="NHWC",u=e.dims.slice();o&&u.splice(1,0,u.pop());let l=Object.hasOwnProperty.call(t,"dilations"),d=t.kernelShape.slice(),c=t.strides.slice(),s=l?t.dilations.slice():[],r=t.pads.slice();Po.adjustPoolAttributes(n,u,d,c,s,r);let i=Po.computePoolOutputShape(n,u,c,s,d,r,t.autoPad,t.ceilMode),a=Object.assign({},t);l?Object.assign(a,{kernelShape:d,strides:c,pads:r,dilations:s,cacheKey:t.cacheKey}):Object.assign(a,{kernelShape:d,strides:c,pads:r,cacheKey:t.cacheKey});let p=i.slice();return p.push(p.splice(1,1)[0]),[a,o?p:i]},tu=(e,t)=>{let n=t.format==="NHWC",o=z.size(e),u=z.size(t.kernelShape),l=[{type:12,data:o},{type:12,data:u}],d=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let c=t.kernelShape[t.kernelShape.length-1],s=t.strides[t.strides.length-1],r=t.pads[t.pads.length/2-1],i=t.pads[t.pads.length-1],a=!!(r+i);l.push({type:12,data:c},{type:12,data:s},{type:12,data:r},{type:12,data:i}),d.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let p=!1;if(t.kernelShape.length===2){let h=t.kernelShape[t.kernelShape.length-2],m=t.strides[t.strides.length-2],b=t.pads[t.pads.length/2-2],x=t.pads[t.pads.length-2];p=!!(b+x),l.push({type:12,data:h},{type:12,data:m},{type:12,data:b},{type:12,data:x}),d.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[l,d,!0,a,p]}else{if(n)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let c=z.computeStrides(t.kernelShape);l.push({type:12,data:c},{type:12,data:t.pads},{type:12,data:t.strides}),d.push({name:"kernelStrides",type:"u32",length:c.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let s=t.pads.reduce((r,i)=>r+i);return[l,d,!!s,!1,!1]}},ru=(e,t,n,o,u,l,d,c,s,r,i,a)=>{let p=u.format==="NHWC",h=t.type.value,m=re("output",t.type.tensor,o);if(u.kernelShape.length<=2){let b="",x="",w="",y=n-(p?2:1);if(i?b=` + }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${d}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(B.size(n)/64)},programUniforms:l}),getShaderSource:s}},Hb=(e,t)=>{if(e.length>1){let n=e[1].getBigInt64Array(),i=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,u=e[0].dims.length,l=new Int32Array(2*u).fill(0);if(e.length>=4){let c=e[3].getBigInt64Array();for(let s=0;sl[Number(s)]=Number(c));let d=[];return l.forEach(c=>d.push(c)),{mode:t.mode,value:i,pads:d}}else return t},b2=(e,t)=>{Fb(e.inputs);let n=Hb(e.inputs,t);e.compute(qb(e.inputs,n),{inputs:[0]})}}),fi,iu,ou,su,au,Wb,Kb,uu,lu,y2,w2,du,_2,v2,cu,x2,$2,T2,S2,fk=N(()=>{"use strict";dt(),le(),pe(),he(),fi=e=>{if(me.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},iu=(e,t,n)=>{let i=t.format==="NHWC",u=e.dims.slice();i&&u.splice(1,0,u.pop());let l=Object.hasOwnProperty.call(t,"dilations"),d=t.kernelShape.slice(),c=t.strides.slice(),s=l?t.dilations.slice():[],r=t.pads.slice();Bo.adjustPoolAttributes(n,u,d,c,s,r);let o=Bo.computePoolOutputShape(n,u,c,s,d,r,t.autoPad,t.ceilMode),a=Object.assign({},t);l?Object.assign(a,{kernelShape:d,strides:c,pads:r,dilations:s,cacheKey:t.cacheKey}):Object.assign(a,{kernelShape:d,strides:c,pads:r,cacheKey:t.cacheKey});let p=o.slice();return p.push(p.splice(1,1)[0]),[a,i?p:o]},ou=(e,t)=>{let n=t.format==="NHWC",i=B.size(e),u=B.size(t.kernelShape),l=[{type:12,data:i},{type:12,data:u}],d=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let c=t.kernelShape[t.kernelShape.length-1],s=t.strides[t.strides.length-1],r=t.pads[t.pads.length/2-1],o=t.pads[t.pads.length-1],a=!!(r+o);l.push({type:12,data:c},{type:12,data:s},{type:12,data:r},{type:12,data:o}),d.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let p=!1;if(t.kernelShape.length===2){let h=t.kernelShape[t.kernelShape.length-2],m=t.strides[t.strides.length-2],g=t.pads[t.pads.length/2-2],v=t.pads[t.pads.length-2];p=!!(g+v),l.push({type:12,data:h},{type:12,data:m},{type:12,data:g},{type:12,data:v}),d.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[l,d,!0,a,p]}else{if(n)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let c=B.computeStrides(t.kernelShape);l.push({type:12,data:c},{type:12,data:t.pads},{type:12,data:t.strides}),d.push({name:"kernelStrides",type:"u32",length:c.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let s=t.pads.reduce((r,o)=>r+o);return[l,d,!!s,!1,!1]}},su=(e,t,n,i,u,l,d,c,s,r,o,a)=>{let p=u.format==="NHWC",h=t.type.value,m=ne("output",t.type.tensor,i);if(u.kernelShape.length<=2){let g="",v="",w="",y=n-(p?2:1);if(o?g=` for (var i: u32 = 0u; i < uniforms.kw; i++) { xIndices[${y}] = indices[${y}] * uniforms.sw - uniforms.pwStart + i; if (xIndices[${y}] < 0 || xIndices[${y}] @@ -4118,21 +4118,21 @@ fn calculateOutputIndex(index: u32) -> u32 { } let x_val = x[${t.indicesToOffset("xIndices")}]; ${l} - }`:b=` + }`:g=` for (var i: u32 = 0u; i < uniforms.kw; i++) { xIndices[${y}] = indices[${y}] * uniforms.sw - uniforms.pwStart + i; let x_val = x[${t.indicesToOffset("xIndices")}]; ${l} - }`,u.kernelShape.length===2){let S=n-(p?3:2);a?x=` + }`,u.kernelShape.length===2){let T=n-(p?3:2);a?v=` for (var j: u32 = 0u; j < uniforms.kh; j++) { - xIndices[${S}] = indices[${S}] * uniforms.sh - uniforms.phStart + j; - if (xIndices[${S}] < 0 || xIndices[${S}] >= uniforms.x_shape[${S}]) { + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; + if (xIndices[${T}] < 0 || xIndices[${T}] >= uniforms.x_shape[${T}]) { pad += i32(uniforms.kw); continue; } - `:x=` + `:v=` for (var j: u32 = 0u; j < uniforms.kh; j++) { - xIndices[${S}] = indices[${S}] * uniforms.sh - uniforms.phStart + j; + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; `,w=` } `}return` @@ -4146,13 +4146,13 @@ fn calculateOutputIndex(index: u32) -> u32 { var value = ${h}(${c}); var pad = 0; - ${x} - ${b} + ${v} + ${g} ${w} ${d} output[global_idx] = value; - }`}else{if(p)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let b=u.kernelShape.length,x=u.pads.length,w="";return r?w=` + }`}else{if(p)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let g=u.kernelShape.length,v=u.pads.length,w="";return r?w=` if (xIndices[j] >= uniforms.x_shape[j]) { pad++; isPad = true; @@ -4174,7 +4174,7 @@ fn calculateOutputIndex(index: u32) -> u32 { let indices = ${m.offsetToIndices("global_idx")}; var xIndices = ${m.offsetToIndices("global_idx")}; - var offsets: array; + var offsets: array; var value = ${h}(${c}); var pad = 0; @@ -4182,67 +4182,67 @@ fn calculateOutputIndex(index: u32) -> u32 { for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { var offset = i; - for (var j = 0u; j < ${b-1}u; j++) { - offsets[j] = offset / ${ne("uniforms.kernelStrides","j",b)}; - offset -= offsets[j] * ${ne("uniforms.kernelStrides","j",b)}; + for (var j = 0u; j < ${g-1}u; j++) { + offsets[j] = offset / ${oe("uniforms.kernelStrides","j",g)}; + offset -= offsets[j] * ${oe("uniforms.kernelStrides","j",g)}; } - offsets[${b-1}] = offset; + offsets[${g-1}] = offset; isPad = false; - for (var j = ${n-b}u; j < ${n}u; j++) { - xIndices[j] = indices[j] * ${ne("uniforms.strides",`j - ${n-b}u`,b)} - + offsets[j - ${n-b}u] - ${ne("uniforms.pads","j - 2u",x)}; + for (var j = ${n-g}u; j < ${n}u; j++) { + xIndices[j] = indices[j] * ${oe("uniforms.strides",`j - ${n-g}u`,g)} + + offsets[j - ${n-g}u] - ${oe("uniforms.pads","j - 2u",v)}; ${w} } ${d} output[global_idx] = value; - }`}},nu=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,jb=e=>`${nu(e)};${e.countIncludePad}`,Lb=e=>`${nu(e)};${e.storageOrder};${e.dilations}`,iu=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),ou=(e,t,n,o)=>{let[u,l]=eu(t,o,n),d=j("x",t.dataType,t.dims.length),c=d.type.value,s="value += x_val;",r="";u.countIncludePad?r+=`value /= ${c}(uniforms.kernelSize);`:r+=`value /= ${c}(i32(uniforms.kernelSize) - pad);`;let[i,a,p,h,m]=tu(l,u);i.push(...oe(t.dims,l));let b=["rank"];return{name:e,shaderCache:{hint:`${o.cacheKey};${p};${h};${m}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(z.size(l)/64)},programUniforms:i}),getShaderSource:x=>ru(x,d,t.dims.length,l.length,u,s,r,0,a,p,h,m)}},c2=e=>{let t=e.count_include_pad!==0,n=iu(e);if(n.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding/divisor) is not yet implemented in the WebGPU AveragePool kernel");let o={countIncludePad:t,...n,cacheKey:""};return{...o,cacheKey:jb(o)}},p2=(e,t)=>{ai(e.inputs),e.compute(ou("AveragePool",e.inputs[0],!1,t))},su={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},h2=e=>{let t=e.format;return{format:t,...su,cacheKey:t}},f2=(e,t)=>{ai(e.inputs),e.compute(ou("GlobalAveragePool",e.inputs[0],!0,t))},au=(e,t,n,o)=>{let[u,l]=eu(t,o,n),d=` + }`}},au=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Wb=e=>`${au(e)};${e.countIncludePad}`,Kb=e=>`${au(e)};${e.storageOrder};${e.dilations}`,uu=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),lu=(e,t,n,i)=>{let[u,l]=iu(t,i,n),d=L("x",t.dataType,t.dims.length),c=d.type.value,s="value += x_val;",r="";u.countIncludePad?r+=`value /= ${c}(uniforms.kernelSize);`:r+=`value /= ${c}(i32(uniforms.kernelSize) - pad);`;let[o,a,p,h,m]=ou(l,u);o.push(...ae(t.dims,l));let g=["rank"];return{name:e,shaderCache:{hint:`${i.cacheKey};${p};${h};${m}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(B.size(l)/64)},programUniforms:o}),getShaderSource:v=>su(v,d,t.dims.length,l.length,u,s,r,0,a,p,h,m)}},y2=e=>{let t=e.count_include_pad!==0,n=uu(e);if(n.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding/divisor) is not yet implemented in the WebGPU AveragePool kernel");let i={countIncludePad:t,...n,cacheKey:""};return{...i,cacheKey:Wb(i)}},w2=(e,t)=>{fi(e.inputs),e.compute(lu("AveragePool",e.inputs[0],!1,t))},du={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},_2=e=>{let t=e.format;return{format:t,...du,cacheKey:t}},v2=(e,t)=>{fi(e.inputs),e.compute(lu("GlobalAveragePool",e.inputs[0],!0,t))},cu=(e,t,n,i)=>{let[u,l]=iu(t,i,n),d=` value = max(x_val, value); - `,c="",s=j("x",t.dataType,t.dims.length),r=["rank"],[i,a,p,h,m]=tu(l,u);return i.push(...oe(t.dims,l)),{name:e,shaderCache:{hint:`${o.cacheKey};${p};${h};${m}`,inputDependencies:r},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(z.size(l)/64)},programUniforms:i}),getShaderSource:b=>ru(b,s,t.dims.length,l.length,u,d,c,t.dataType===10?-65504:-1e5,a,p,h,m)}},m2=(e,t)=>{ai(e.inputs),e.compute(au("MaxPool",e.inputs[0],!1,t))},g2=e=>{let t=e.storage_order,n=e.dilations,o=iu(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(o.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding) is not yet implemented in the WebGPU MaxPool kernel");let u={storageOrder:t,dilations:n,...o,cacheKey:""};return{...u,cacheKey:Lb(u)}},b2=e=>{let t=e.format;return{format:t,...su,cacheKey:t}},y2=(e,t)=>{ai(e.inputs),e.compute(au("GlobalMaxPool",e.inputs[0],!0,t))}}),Ub,Vb,w2,_2,nk=R(()=>{"use strict";se(),ae(),Ve(),ue(),Ub=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((n,o)=>n===e[2].dims[o]).reduce((n,o)=>n&&o,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((u,l)=>l===t.axis||u===e[0].dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let n=e[0].dims[t.axis],o=e[1].dims[t.axis];if(t.blockSizeMath.ceil(n/(o-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},Vb=(e,t)=>{let n=z.normalizeAxis(t.axis,e[0].dims.length),o=e[0].dataType,u=o===3,l=e[0].dims,d=e[1].dataType,c=z.size(l),s=o===3||o===2,r=s?[Math.ceil(z.size(e[0].dims)/4)]:e[0].dims,i=e[1].dims,a=e.length>2?e[2]:void 0,p=a?s?[Math.ceil(z.size(a.dims)/4)]:a.dims:void 0,h=i.length===0||i.length===1&&i[0]===1,m=h===!1&&i.length===1,b=Ue(c),x=h&&(!s||b===4),w=x?b:1,y=x&&!s?b:1,S=j("input",s?12:o,r.length,y),T=j("scale",d,i.length),I=a?j("zero_point",s?12:o,p.length):void 0,A=re("output",d,l.length,w),P=[S,T];I&&P.push(I);let O=[r,i];a&&O.push(p);let B=[{type:12,data:c/w},{type:12,data:n},{type:12,data:t.blockSize},...oe(...O,l)],U=X=>{let q=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` - ${X.registerUniforms(q).declareVariables(...P,A)} + `,c="",s=L("x",t.dataType,t.dims.length),r=["rank"],[o,a,p,h,m]=ou(l,u);return o.push(...ae(t.dims,l)),{name:e,shaderCache:{hint:`${i.cacheKey};${p};${h};${m}`,inputDependencies:r},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(B.size(l)/64)},programUniforms:o}),getShaderSource:g=>su(g,s,t.dims.length,l.length,u,d,c,t.dataType===10?-65504:-1e5,a,p,h,m)}},x2=(e,t)=>{fi(e.inputs),e.compute(cu("MaxPool",e.inputs[0],!1,t))},$2=e=>{let t=e.storage_order,n=e.dilations,i=uu(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(i.ceilMode!==0)throw new Error("ceil_mode output-shape is computed, but ceil_mode kernel execution (padding) is not yet implemented in the WebGPU MaxPool kernel");let u={storageOrder:t,dilations:n,...i,cacheKey:""};return{...u,cacheKey:Kb(u)}},T2=e=>{let t=e.format;return{format:t,...du,cacheKey:t}},S2=(e,t)=>{fi(e.inputs),e.compute(cu("GlobalMaxPool",e.inputs[0],!0,t))}}),Xb,Zb,I2,O2,mk=N(()=>{"use strict";le(),pe(),He(),he(),Xb=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((n,i)=>n===e[2].dims[i]).reduce((n,i)=>n&&i,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((u,l)=>l===t.axis||u===e[0].dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let n=e[0].dims[t.axis],i=e[1].dims[t.axis];if(t.blockSizeMath.ceil(n/(i-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},Zb=(e,t)=>{let n=B.normalizeAxis(t.axis,e[0].dims.length),i=e[0].dataType,u=i===3,l=e[0].dims,d=e[1].dataType,c=B.size(l),s=i===3||i===2,r=s?[Math.ceil(B.size(e[0].dims)/4)]:e[0].dims,o=e[1].dims,a=e.length>2?e[2]:void 0,p=a?s?[Math.ceil(B.size(a.dims)/4)]:a.dims:void 0,h=o.length===0||o.length===1&&o[0]===1,m=h===!1&&o.length===1,g=qe(c),v=h&&(!s||g===4),w=v?g:1,y=v&&!s?g:1,T=L("input",s?12:i,r.length,y),S=L("scale",d,o.length),O=a?L("zero_point",s?12:i,p.length):void 0,A=ne("output",d,l.length,w),k=[T,S];O&&k.push(O);let I=[r,o];a&&I.push(p);let R=[{type:12,data:c/w},{type:12,data:n},{type:12,data:t.blockSize},...ae(...I,l)],M=X=>{let G=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${X.registerUniforms(G).declareVariables(...k,A)} ${X.mainStart()} ${X.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let output_indices = ${A.offsetToIndices("global_idx")}; // Set input x ${s?` - let input = ${S.getByOffset("global_idx / 4")}; + let input = ${T.getByOffset("global_idx / 4")}; let x_vec = ${u?"unpack4xI8(input)":"unpack4xU8(input)"}; - let x_value = ${w===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${S.getByOffset("global_idx")};`}; + let x_value = ${w===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${T.getByOffset("global_idx")};`}; // Set scale input - ${h?`let scale_value= ${T.getByOffset("0")}`:m?` + ${h?`let scale_value= ${S.getByOffset("0")}`:m?` let scale_index = ${A.indicesGet("output_indices","uniforms.axis")}; - let scale_value= ${T.getByOffset("scale_index")};`:` - var scale_indices: ${T.type.indices} = output_indices; - let index = ${T.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size; - ${T.indicesSet("scale_indices","uniforms.axis","index")}; - let scale_value= ${T.getByIndices("scale_indices")};`}; + let scale_value= ${S.getByOffset("scale_index")};`:` + var scale_indices: ${S.type.indices} = output_indices; + let index = ${S.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size; + ${S.indicesSet("scale_indices","uniforms.axis","index")}; + let scale_value= ${S.getByIndices("scale_indices")};`}; // Set zero-point input - ${I?h?s?` - let zero_point_input = ${I.getByOffset("0")}; + ${O?h?s?` + let zero_point_input = ${O.getByOffset("0")}; let zero_point_vec = ${u?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; - let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${I.getByOffset("0")}`:m?s?` + let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${O.getByOffset("0")}`:m?s?` let zero_point_index = ${A.indicesGet("output_indices","uniforms.axis")}; - let zero_point_input = ${I.getByOffset("zero_point_index / 4")}; + let zero_point_input = ${O.getByOffset("zero_point_index / 4")}; let zero_point_vec = ${u?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; let zero_point_value = zero_point_vec[zero_point_index % 4]`:` let zero_point_index = ${A.indicesGet("output_indices","uniforms.axis")}; - let zero_point_value = ${I.getByOffset("zero_point_index")};`:s?` - let zero_point_offset = ${T.indicesToOffset("scale_indices")}; - let zero_point_input = ${I.getByOffset("zero_point_offset / 4")}; + let zero_point_value = ${O.getByOffset("zero_point_index")};`:s?` + let zero_point_offset = ${S.indicesToOffset("scale_indices")}; + let zero_point_input = ${O.getByOffset("zero_point_offset / 4")}; let zero_point_vec = ${u?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; - let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${I.getByIndices("scale_indices")};`:`let zero_point_value = ${s?u?"i32":"u32":S.type.value}(0);`}; + let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${O.getByIndices("scale_indices")};`:`let zero_point_value = ${s?u?"i32":"u32":T.type.value}(0);`}; // Compute and write output ${A.setByOffset("global_idx",`${A.type.value}(x_value - zero_point_value) * scale_value`)}; - }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:I?["rank","rank","rank"]:["rank","rank"]},getShaderSource:U,getRunData:()=>({outputs:[{dims:l,dataType:d}],dispatchGroup:{x:Math.ceil(c/w/64),y:1,z:1},programUniforms:B})}},w2=(e,t)=>{Ub(e.inputs,t),e.compute(Vb(e.inputs,t))},_2=e=>Se({axis:e.axis,blockSize:e.blockSize})}),Gb,qb,v2,ik=R(()=>{"use strict";ot(),se(),ue(),Gb=(e,t,n)=>{let o=e===t,u=et&&n>0;if(o||u||l)throw new Error("Range these inputs' contents are invalid.")},qb=(e,t,n,o)=>{let u=Math.abs(Math.ceil((t-e)/n)),l=[u],d=u,c=[{type:12,data:d},{type:o,data:e},{type:o,data:n},...oe(l)],s=r=>{let i=re("output",o,l.length),a=i.type.value,p=[{name:"outputSize",type:"u32"},{name:"start",type:a},{name:"delta",type:a}];return` - ${r.registerUniforms(p).declareVariables(i)} + }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:O?["rank","rank","rank"]:["rank","rank"]},getShaderSource:M,getRunData:()=>({outputs:[{dims:l,dataType:d}],dispatchGroup:{x:Math.ceil(c/w/64),y:1,z:1},programUniforms:R})}},I2=(e,t)=>{Xb(e.inputs,t),e.compute(Zb(e.inputs,t))},O2=e=>ke({axis:e.axis,blockSize:e.blockSize})}),Yb,Jb,E2,gk=N(()=>{"use strict";dt(),le(),he(),Yb=(e,t,n)=>{let i=e===t,u=et&&n>0;if(i||u||l)throw new Error("Range these inputs' contents are invalid.")},Jb=(e,t,n,i)=>{let u=Math.abs(Math.ceil((t-e)/n)),l=[u],d=u,c=[{type:12,data:d},{type:i,data:e},{type:i,data:n},...ae(l)],s=r=>{let o=ne("output",i,l.length),a=o.type.value,p=[{name:"outputSize",type:"u32"},{name:"start",type:a},{name:"delta",type:a}];return` + ${r.registerUniforms(p).declareVariables(o)} ${r.mainStart()} ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} output[global_idx] = uniforms.start + ${a}(global_idx) * uniforms.delta; - }`};return{name:"Range",shaderCache:{hint:`${o}`},getShaderSource:s,getRunData:()=>({outputs:[{dims:l,dataType:o}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c})}},v2=e=>{let t=0,n=0,o=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],n=e.inputs[1].getInt32Array()[0],o=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],n=e.inputs[1].getFloat32Array()[0],o=e.inputs[2].getFloat32Array()[0]),le.webgpu.validateInputContent&&Gb(t,n,o),e.compute(qb(t,n,o,e.inputs[0].dataType),{inputs:[]})}}),Hb,Wb,x2,$2,ok=R(()=>{"use strict";se(),ae(),Ve(),ue(),Hb=(e,t,n,o)=>{if(e!=="none"&&o!=="i32"&&o!=="u32"&&o!=="f32")throw new Error(`Input ${o} is not supported with reduction ${e}.`);let u=`{ + }`};return{name:"Range",shaderCache:{hint:`${i}`},getShaderSource:s,getRunData:()=>({outputs:[{dims:l,dataType:i}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c})}},E2=e=>{let t=0,n=0,i=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],n=e.inputs[1].getInt32Array()[0],i=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],n=e.inputs[1].getFloat32Array()[0],i=e.inputs[2].getFloat32Array()[0]),me.webgpu.validateInputContent&&Yb(t,n,i),e.compute(Jb(t,n,i,e.inputs[0].dataType),{inputs:[]})}}),Qb,ey,A2,k2,bk=N(()=>{"use strict";le(),pe(),He(),he(),Qb=(e,t,n,i)=>{if(e!=="none"&&i!=="i32"&&i!=="u32"&&i!=="f32")throw new Error(`Input ${i} is not supported with reduction ${e}.`);let u=`{ var oldValue = 0; loop { let newValueF32 =`,l=`; @@ -4253,9 +4253,9 @@ fn calculateOutputIndex(index: u32) -> u32 { } oldValue = res.old_value; } - }`;switch(e){case"none":return`${t}=${n};`;case"add":return o==="i32"||o==="u32"?`atomicAdd(&${t}, bitcast<${o}>(${n}));`:` - ${u}bitcast<${o}>(oldValue) + (${n})${l}`;case"max":return o==="i32"||o==="u32"?`atomicMax(&${t}, bitcast<${o}>(${n}));`:` - ${u}max(bitcast(oldValue), (${n}))${l}`;case"min":return o==="i32"||o==="u32"?`atomicMin(&${t}, bitcast<${o}>(${n}));`:`${u}min(bitcast<${o}>(oldValue), (${n}))${l}`;case"mul":return`${u}(bitcast<${o}>(oldValue) * (${n}))${l}`;default:throw new Error(`Reduction ${e} is not supported.`)}},Wb=(e,t)=>{let n=e[0].dims,o=e[1].dims,u=n,l=1,d=Math.ceil(z.sizeToDimension(o,o.length-1)/l),c=o[o.length-1],s=z.sizeFromDimension(n,c),r=[{type:12,data:d},{type:12,data:c},{type:12,data:s},...oe(e[1].dims,e[2].dims,u)],i=a=>{let p=j("indices",e[1].dataType,e[1].dims.length),h=j("updates",e[2].dataType,e[2].dims.length,l),m=t.reduction!=="none"&&t.reduction!==""?Kv("output",e[0].dataType,u.length):re("output",e[0].dataType,u.length,l);return` + }`;switch(e){case"none":return`${t}=${n};`;case"add":return i==="i32"||i==="u32"?`atomicAdd(&${t}, bitcast<${i}>(${n}));`:` + ${u}bitcast<${i}>(oldValue) + (${n})${l}`;case"max":return i==="i32"||i==="u32"?`atomicMax(&${t}, bitcast<${i}>(${n}));`:` + ${u}max(bitcast(oldValue), (${n}))${l}`;case"min":return i==="i32"||i==="u32"?`atomicMin(&${t}, bitcast<${i}>(${n}));`:`${u}min(bitcast<${i}>(oldValue), (${n}))${l}`;case"mul":return`${u}(bitcast<${i}>(oldValue) * (${n}))${l}`;default:throw new Error(`Reduction ${e} is not supported.`)}},ey=(e,t)=>{let n=e[0].dims,i=e[1].dims,u=n,l=1,d=Math.ceil(B.sizeToDimension(i,i.length-1)/l),c=i[i.length-1],s=B.sizeFromDimension(n,c),r=[{type:12,data:d},{type:12,data:c},{type:12,data:s},...ae(e[1].dims,e[2].dims,u)],o=a=>{let p=L("indices",e[1].dataType,e[1].dims.length),h=L("updates",e[2].dataType,e[2].dims.length,l),m=t.reduction!=="none"&&t.reduction!==""?tx("output",e[0].dataType,u.length):ne("output",e[0].dataType,u.length,l);return` ${a.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(p,h,m)} ${a.mainStart()} ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} @@ -4285,24 +4285,24 @@ fn calculateOutputIndex(index: u32) -> u32 { for (var i = 0u; i < uniforms.num_updates_elements; i++) { let value = updates[uniforms.num_updates_elements * global_idx + i]; - ${Hb(t.reduction,"output[data_offset + i]","value",m.type.value)} + ${Qb(t.reduction,"output[data_offset + i]","value",m.type.value)} } - }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:r}),getShaderSource:i}},x2=e=>Se({reduction:e.reduction}),$2=(e,t)=>{e.compute(Wb(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),Kb,Xb,Zb,uu,Jb,Yb,Qb,ey,ty,ry,ny,iy,lu,oy,sy,ay,uy,ly,T2,S2,sk=R(()=>{"use strict";se(),ae(),Ve(),ue(),Kb=(e,t)=>{if(e.every(n=>n>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and - one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Xb=(e,t,n)=>{t.every(u=>u>=0&&u{throw new Error("Resize requires axes input values to be positive and less than rank")}));let o=new Array(n).fill(1);return t.forEach((u,l)=>o[u]=e[l]),o},Zb=(e,t,n,o,u,l)=>{let[d,c,s]=n>10?[1,2,3]:[-1,e.length>1?1:-1,-1],r=e[0].dims.length;if(d>0&&e.length>d&&e[d].dims.length>0)e[d].getFloat32Array().forEach(i=>l.push(i));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(c>0&&e.length>c&&e[c].dims.length===1&&e[c].dims[0]>0){if(e[c].getFloat32Array().forEach(i=>o.push(i)),o.length!==0&&o.length!==r&&n>=18&&o.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");Kb(o,t),t.axes.length>0&&Xb(o,t.axes,r).forEach((i,a)=>o[a]=i)}if(s>0&&e.length>s&&e[s].dims.length===1&&e[s].dims[0]>0&&(e[s].getBigInt64Array().forEach(i=>u.push(Number(i))),u.length!==0&&u.length!==r&&n>=18&&u.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(o.length!==0&&o.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(u.length!==0&&u.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof o<"u"&&typeof u<"u"&&o.length>0&&u.length>r)throw new Error("Resize requires only of scales or sizes to be specified")},uu=(e,t,n,o)=>` + }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:r}),getShaderSource:o}},A2=e=>ke({reduction:e.reduction}),k2=(e,t)=>{e.compute(ey(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),ty,ry,ny,pu,iy,oy,sy,ay,uy,ly,dy,cy,hu,py,hy,fy,my,gy,P2,C2,yk=N(()=>{"use strict";le(),pe(),He(),he(),ty=(e,t)=>{if(e.every(n=>n>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and + one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},ry=(e,t,n)=>{t.every(u=>u>=0&&u{throw new Error("Resize requires axes input values to be positive and less than rank")}));let i=new Array(n).fill(1);return t.forEach((u,l)=>i[u]=e[l]),i},ny=(e,t,n,i,u,l)=>{let[d,c,s]=n>10?[1,2,3]:[-1,e.length>1?1:-1,-1],r=e[0].dims.length;if(d>0&&e.length>d&&e[d].dims.length>0)e[d].getFloat32Array().forEach(o=>l.push(o));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(c>0&&e.length>c&&e[c].dims.length===1&&e[c].dims[0]>0){if(e[c].getFloat32Array().forEach(o=>i.push(o)),i.length!==0&&i.length!==r&&n>=18&&i.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");ty(i,t),t.axes.length>0&&ry(i,t.axes,r).forEach((o,a)=>i[a]=o)}if(s>0&&e.length>s&&e[s].dims.length===1&&e[s].dims[0]>0&&(e[s].getBigInt64Array().forEach(o=>u.push(Number(o))),u.length!==0&&u.length!==r&&n>=18&&u.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(i.length!==0&&i.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(u.length!==0&&u.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof i<"u"&&typeof u<"u"&&i.length>0&&u.length>r)throw new Error("Resize requires only of scales or sizes to be specified")},pu=(e,t,n,i)=>` // The whole part and the fractional part are calculated separately due to inaccuracy of floating // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an // offset-by-one error later in floor(). let big = (${e}) * (${t}); - let whole = ${o}(big / (${n})); - let fract = ${o}(big % (${n})) / ${o}(${n}); + let whole = ${i}(big / (${n})); + let fract = ${i}(big % (${n})) / ${i}(${n}); return whole + fract; -`,Jb=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, +`,iy=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return` if (xScale < 1.0 || floor(xScale) != xScale) { return ${t}(xResized) / ${t}(xScale); } else { - ${uu("xResized","lengthOriginal","lengthResized",t)} + ${pu("xResized","lengthOriginal","lengthResized",t)} } `;case"pytorch_half_pixel":return`if (lengthResized > 1) { return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5; @@ -4311,7 +4311,7 @@ fn calculateOutputIndex(index: u32) -> u32 { }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) { return 0.0; } else { - ${uu("xResized","lengthOriginal - 1","lengthResized - 1",t)} + ${pu("xResized","lengthOriginal - 1","lengthResized - 1",t)} }`;case"tf_crop_and_resize":return`if (lengthResized > 1) { return ${t}(roiStart) * ${t}(lengthOriginal - 1) + (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) / @@ -4322,38 +4322,38 @@ fn calculateOutputIndex(index: u32) -> u32 { const adjustment = ${t}(lengthResized) / outputWidth; const center = ${t}(lengthOriginal) / 2; const offset = center * (1 - adjustment); - return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Yb=(e,t,n)=>`fn getNearestPixelFromOriginal(xOriginal: ${n}, isDownSample: bool) -> ${n} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Qb=(e,t,n)=>{let o=new Array(n).fill(0).concat(new Array(n).fill(1)),u=e.length===0?o:e.slice();return t.length>0?(t.forEach((l,d)=>{o[l]=u[d],o[d+n]=u[t.length+d]}),o):u},ey=(e,t,n,o)=>{let u=[];if(n.length>0)if(o.length>0){if(e.forEach(l=>u.push(l)),Math.max(...o)>e.length)throw new Error("axes is out of bound");o.forEach((l,d)=>u[l]=n[d])}else n.forEach(l=>u.push(l));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");u=e.map((l,d)=>Math.round(l*t[d]))}return u},ty=(e,t,n)=>{let o=(()=>{switch(n.keepAspectRatioPolicy){case"not_larger":return n.axes.length>0?Math.min(...n.axes.map(l=>t[l]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return n.axes.length>0?Math.max(...n.axes.map(l=>t[l]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${n.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let u=e.slice();return n.axes.length>0?(n.axes.forEach(l=>t[l]=o),n.axes.forEach(l=>u[l]=Math.round(e[l]*t[l]))):(t.fill(o,0,t.length),u.forEach((l,d)=>u[d]=Math.round(l*t[d]))),u},ry=(e,t,n,o,u)=>` + return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",oy=(e,t,n)=>`fn getNearestPixelFromOriginal(xOriginal: ${n}, isDownSample: bool) -> ${n} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",sy=(e,t,n)=>{let i=new Array(n).fill(0).concat(new Array(n).fill(1)),u=e.length===0?i:e.slice();return t.length>0?(t.forEach((l,d)=>{i[l]=u[d],i[d+n]=u[t.length+d]}),i):u},ay=(e,t,n,i)=>{let u=[];if(n.length>0)if(i.length>0){if(e.forEach(l=>u.push(l)),Math.max(...i)>e.length)throw new Error("axes is out of bound");i.forEach((l,d)=>u[l]=n[d])}else n.forEach(l=>u.push(l));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");u=e.map((l,d)=>Math.round(l*t[d]))}return u},uy=(e,t,n)=>{let i=(()=>{switch(n.keepAspectRatioPolicy){case"not_larger":return n.axes.length>0?Math.min(...n.axes.map(l=>t[l]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return n.axes.length>0?Math.max(...n.axes.map(l=>t[l]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${n.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let u=e.slice();return n.axes.length>0?(n.axes.forEach(l=>t[l]=i),n.axes.forEach(l=>u[l]=Math.round(e[l]*t[l]))):(t.fill(i,0,t.length),u.forEach((l,d)=>u[d]=Math.round(l*t[d]))),u},ly=(e,t,n,i,u)=>` fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${n.length}> { var original_indices: array<${e.type.value}, ${n.length}>; for (var i:u32 = 0; i < ${n.length}; i++) { var output_index = ${e.indicesGet("output_indices","i")}; - var scale = ${ne("uniforms.scales","i",o)}; - var roi_low = ${ne("uniforms.roi","i",u)}; - var roi_hi = ${ne("uniforms.roi",`i + ${t.length}`,u)}; + var scale = ${oe("uniforms.scales","i",i)}; + var roi_low = ${oe("uniforms.roi","i",u)}; + var roi_hi = ${oe("uniforms.roi",`i + ${t.length}`,u)}; if (scale == 1.0) { original_indices[i] = ${e.type.value}(output_index); } else { - var input_shape_i = ${ne("uniforms.input_shape","i",t.length)}; - var output_shape_i = ${ne("uniforms.output_shape","i",n.length)}; + var input_shape_i = ${oe("uniforms.input_shape","i",t.length)}; + var output_shape_i = ${oe("uniforms.output_shape","i",n.length)}; original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, input_shape_i, roi_low, roi_hi); } } return original_indices; - }`,ny=(e,t,n,o,u,l,d)=>` + }`,dy=(e,t,n,i,u,l,d)=>` fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { var input_indices: ${e.type.indices}; - for (var i:u32 = 0; i < ${o.length}; i++) { + for (var i:u32 = 0; i < ${i.length}; i++) { var output_index = ${t.indicesGet("output_indices","i")}; var input_index: u32; - var scale = ${ne("uniforms.scales","i",u)}; + var scale = ${oe("uniforms.scales","i",u)}; if (scale == 1.0) { input_index = output_index; } else { - var roi_low = ${ne("uniforms.roi","i",l)}; - var roi_hi = ${ne("uniforms.roi",`i + ${n.length}`,l)}; - var input_shape_i = ${ne("uniforms.input_shape","i",n.length)}; - var output_shape_i = ${ne("uniforms.output_shape","i",o.length)}; + var roi_low = ${oe("uniforms.roi","i",l)}; + var roi_hi = ${oe("uniforms.roi",`i + ${n.length}`,l)}; + var input_shape_i = ${oe("uniforms.input_shape","i",n.length)}; + var output_shape_i = ${oe("uniforms.output_shape","i",i.length)}; var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, input_shape_i, roi_low, roi_hi); if (!${d} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { @@ -4371,24 +4371,24 @@ fn calculateOutputIndex(index: u32) -> u32 { ${e.indicesSet("input_indices","i","input_index")} } return input_indices; - }`,iy=(e,t)=>` + }`,cy=(e,t)=>` fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { for (var i:u32 = 0; i < ${t.length}; i++) { var input_index = ${e.indicesGet("input_indices","i")}; - if (input_index < 0 || input_index >= ${ne("uniforms.input_shape","i",t.length)}) { + if (input_index < 0 || input_index >= ${oe("uniforms.input_shape","i",t.length)}) { return false; } } return true; - }`,lu=(e,t,n,o)=>e.rank>o?` + }`,hu=(e,t,n,i)=>e.rank>i?` ${e.indicesSet("input_indices",t,"channel")}; ${e.indicesSet("input_indices",n,"batch")}; -`:"",oy=(e,t,n,o,u)=>{let[l,d,c,s]=n.length===2?[-1,0,1,-1]:[0,2,3,1],r=e.type.value;return` +`:"",py=(e,t,n,i,u)=>{let[l,d,c,s]=n.length===2?[-1,0,1,-1]:[0,2,3,1],r=e.type.value;return` fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${r} { var input_indices: ${e.type.indices}; ${e.indicesSet("input_indices",d,`max(0, min(row, ${n[d]} - 1))`)}; ${e.indicesSet("input_indices",c,`max(0, min(col, ${n[c]} - 1))`)}; - ${lu(e,s,l,2)} + ${hu(e,s,l,2)} return ${e.getByIndices("input_indices")}; } @@ -4396,7 +4396,7 @@ fn calculateOutputIndex(index: u32) -> u32 { var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); var row:${r} = originalIndices[${d}]; var col:${r} = originalIndices[${c}]; - ${o?`if (row < 0 || row > (${n[d]} - 1) || col < 0 || col > (${n[c]} - 1)) { + ${i?`if (row < 0 || row > (${n[d]} - 1) || col < 0 || col > (${n[c]} - 1)) { return ${u}; }`:""}; row = max(0, min(row, ${n[d]} - 1)); @@ -4424,32 +4424,32 @@ fn calculateOutputIndex(index: u32) -> u32 { dy2 = 0.5; } return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); - }`},sy=(e,t,n,o,u,l,d,c,s,r)=>{let i=n.length===2,a=!0,[p,h]=i?[0,1]:a?[2,3]:[1,2],m=e.type.value,b=x=>{let w=x===p?"row":"col";return` + }`},hy=(e,t,n,i,u,l,d,c,s,r)=>{let o=n.length===2,a=!0,[p,h]=o?[0,1]:a?[2,3]:[1,2],m=e.type.value,g=v=>{let w=v===p?"row":"col";return` fn ${w}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${m} { - var output_index = ${t.indicesGet("output_indices",x)}; - var originalIdx: ${m} = getOriginalCoordinateFromResizedCoordinate(output_index, ${u[x]}, - ${o[x]}, ${n[x]}, ${l[x]}, ${l[x]} + ${n.length}); + var output_index = ${t.indicesGet("output_indices",v)}; + var originalIdx: ${m} = getOriginalCoordinateFromResizedCoordinate(output_index, ${u[v]}, + ${i[v]}, ${n[v]}, ${l[v]}, ${l[v]} + ${n.length}); var fractOriginalIdx: ${m} = originalIdx - floor(originalIdx); var coefs = getCubicInterpolationCoefs(fractOriginalIdx); - if (${c} && (originalIdx < 0 || originalIdx > (${n[x]} - 1))) { + if (${c} && (originalIdx < 0 || originalIdx > (${n[v]} - 1))) { return ${s}; } var data: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); for (var i: i32 = -1; i < 3; i++) { var ${w}: ${m} = originalIdx + ${m}(i); - if (${w} < 0 || ${w} >= ${n[x]}) { + if (${w} < 0 || ${w} >= ${n[v]}) { ${r?`coefs[i + 1] = 0.0; - continue;`:c?`return ${s};`:`${w} = max(0, min(${w}, ${n[x]} - 1));`}; + continue;`:c?`return ${s};`:`${w} = max(0, min(${w}, ${n[v]} - 1));`}; } var input_indices_copy: ${e.type.indices} = input_indices; - ${e.indicesSet("input_indices_copy",x,`u32(${w})`)}; - data[i + 1] = ${x===p?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; + ${e.indicesSet("input_indices_copy",v,`u32(${w})`)}; + data[i + 1] = ${v===p?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; } return cubicInterpolation1D(data, coefs); }`};return` - ${b(p)}; - ${b(h)}; + ${g(p)}; + ${g(h)}; fn getCubicInterpolationCoefs(s: ${m}) -> array<${m}, 4> { var absS = abs(s); var coeffs: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); @@ -4472,22 +4472,22 @@ fn calculateOutputIndex(index: u32) -> u32 { var input_indices: ${e.type.indices} = output_indices; return colCubicInterpolation(input_indices, output_indices); } - `},ay=(e,t,n,o,u)=>{let[l,d,c,s,r]=n.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],i=e.type.value;return` - fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${i} { + `},fy=(e,t,n,i,u)=>{let[l,d,c,s,r]=n.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],o=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${o} { var input_indices: ${e.type.indices}; ${e.indicesSet("input_indices",d,`max(0, min(depth, ${n[d]} - 1))`)}; ${e.indicesSet("input_indices",c,`max(0, min(height, ${n[c]} - 1))`)}; ${e.indicesSet("input_indices",s,`max(0, min(width, ${n[s]} - 1))`)}; - ${lu(e,r,l,3)} + ${hu(e,r,l,3)} return ${e.getByIndices("input_indices")}; } - fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${i} { + fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${o} { var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); - var depth:${i} = originalIndices[${d}]; - var height:${i} = originalIndices[${c}]; - var width:${i} = originalIndices[${s}]; - ${o?`if (depth < 0 || depth > (${n[d]} - 1) || height < 0 || height > (${n[c]} - 1) || width < 0 || (width > ${n[s]} - 1)) { + var depth:${o} = originalIndices[${d}]; + var height:${o} = originalIndices[${c}]; + var width:${o} = originalIndices[${s}]; + ${i?`if (depth < 0 || depth > (${n[d]} - 1) || height < 0 || height > (${n[c]} - 1) || width < 0 || (width > ${n[s]} - 1)) { return ${u}; }`:""}; @@ -4503,20 +4503,20 @@ fn calculateOutputIndex(index: u32) -> u32 { var channel: u32 = ${n.length>3?`u32(originalIndices[${r}])`:"0"}; var batch: u32 = ${n.length>3?`u32(originalIndices[${l}])`:"0"}; - var x111: ${i} = getInputValue(batch, channel, depth1, height1, width1); - var x112: ${i} = getInputValue(batch, channel, depth1, height1, width2); - var x121: ${i} = getInputValue(batch, channel, depth1, height2, width1); - var x122: ${i} = getInputValue(batch, channel, depth1, height2, width2); - var x211: ${i} = getInputValue(batch, channel, depth2, height1, width1); - var x212: ${i} = getInputValue(batch, channel, depth2, height1, width2); - var x221: ${i} = getInputValue(batch, channel, depth2, height2, width1); - var x222: ${i} = getInputValue(batch, channel, depth2, height2, width2); - var dx1: ${i} = abs(depth - ${i}(depth1)); - var dx2: ${i} = abs(${i}(depth2) - depth); - var dy1: ${i} = abs(height - ${i}(height1)); - var dy2: ${i} = abs(${i}(height2) - height); - var dz1: ${i} = abs(width - ${i}(width1)); - var dz2: ${i} = abs(${i}(width2) - width); + var x111: ${o} = getInputValue(batch, channel, depth1, height1, width1); + var x112: ${o} = getInputValue(batch, channel, depth1, height1, width2); + var x121: ${o} = getInputValue(batch, channel, depth1, height2, width1); + var x122: ${o} = getInputValue(batch, channel, depth1, height2, width2); + var x211: ${o} = getInputValue(batch, channel, depth2, height1, width1); + var x212: ${o} = getInputValue(batch, channel, depth2, height1, width2); + var x221: ${o} = getInputValue(batch, channel, depth2, height2, width1); + var x222: ${o} = getInputValue(batch, channel, depth2, height2, width2); + var dx1: ${o} = abs(depth - ${o}(depth1)); + var dx2: ${o} = abs(${o}(depth2) - depth); + var dy1: ${o} = abs(height - ${o}(height1)); + var dy2: ${o} = abs(${o}(height2) - height); + var dz1: ${o} = abs(width - ${o}(width1)); + var dz2: ${o} = abs(${o}(width2) - width); if (depth1 == depth2) { dx1 = 0.5; dx2 = 0.5; @@ -4531,25 +4531,25 @@ fn calculateOutputIndex(index: u32) -> u32 { } return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); - }`},uy=(e,t,n,o,u,l)=>{let d=e.dims,c=Qb(l,t.axes,d.length),s=ey(d,o,u,t.axes),r=o.slice();o.length===0&&(r=d.map((y,S)=>y===0?1:s[S]/y),t.keepAspectRatioPolicy!=="stretch"&&(s=ty(d,r,t)));let i=re("output",e.dataType,s.length),a=j("input",e.dataType,d.length),p=z.size(s),h=d.length===s.length&&d.every((y,S)=>y===s[S]),m=t.coordinateTransformMode==="tf_crop_and_resize",b=t.extrapolationValue,x=a.type.value,w=y=>` + }`},my=(e,t,n,i,u,l)=>{let d=e.dims,c=sy(l,t.axes,d.length),s=ay(d,i,u,t.axes),r=i.slice();i.length===0&&(r=d.map((y,T)=>y===0?1:s[T]/y),t.keepAspectRatioPolicy!=="stretch"&&(s=uy(d,r,t)));let o=ne("output",e.dataType,s.length),a=L("input",e.dataType,d.length),p=B.size(s),h=d.length===s.length&&d.every((y,T)=>y===s[T]),m=t.coordinateTransformMode==="tf_crop_and_resize",g=t.extrapolationValue,v=a.type.value,w=y=>` ${h?"":` - ${Jb(t.coordinateTransformMode,x)}; + ${iy(t.coordinateTransformMode,v)}; ${(()=>{switch(t.mode){case"nearest":return` - ${iy(a,d)}; - ${Yb(t.nearestMode,n,x)}; - ${ny(a,i,d,s,r.length,c.length,m)}; + ${cy(a,d)}; + ${oy(t.nearestMode,n,v)}; + ${dy(a,o,d,s,r.length,c.length,m)}; `;case"linear":return` - ${ry(i,d,s,r.length,c.length)}; - ${(()=>{if(d.length===2||d.length===4)return`${oy(a,i,d,m,b)}`;if(d.length===3||d.length===5)return`${ay(a,i,d,m,b)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; + ${ly(o,d,s,r.length,c.length)}; + ${(()=>{if(d.length===2||d.length===4)return`${py(a,o,d,m,g)}`;if(d.length===3||d.length===5)return`${fy(a,o,d,m,g)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; `;case"cubic":return` - ${(()=>{if(d.length===2||d.length===4)return`${sy(a,i,d,s,r,c,t.cubicCoeffA,m,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; + ${(()=>{if(d.length===2||d.length===4)return`${hy(a,o,d,s,r,c,t.cubicCoeffA,m,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; `;default:throw Error("Invalid resize mode")}})()}; `} - ${y.registerUniform("output_size","u32").registerUniform("scales","f32",r.length).registerUniform("roi","f32",c.length).declareVariables(a,i)} + ${y.registerUniform("output_size","u32").registerUniform("scales","f32",r.length).registerUniform("roi","f32",c.length).declareVariables(a,o)} ${y.mainStart()} ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} ${h?"output[global_idx] = input[global_idx];":` - let output_indices = ${i.offsetToIndices("global_idx")}; + let output_indices = ${o.offsetToIndices("global_idx")}; var input_indices: ${a.type.indices}; ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); if (checkInputIndices(input_indices)) { @@ -4558,37 +4558,37 @@ fn calculateOutputIndex(index: u32) -> u32 { output[global_idx] = ${t.extrapolationValue}; }`;case"linear":return`output[global_idx] = ${d.length===2||d.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()}; `} - }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${n}|${r.length>0?t.mode==="cubic"?r:r.length:""}|${u.length>0?u:""}|${c.length>0?c:""}|${h}|${t.mode==="nearest"?d.length:d}`,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:[{type:12,data:p},{type:1,data:r},{type:1,data:c},...oe(d,s)]})}},ly=e=>{let t=e.customDataBuffer;return new Uint32Array(t.buffer,t.byteOffset,1)[0]},T2=(e,t)=>{let n=[],o=[],u=[],l=ly(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Zb(e.inputs,t,l,n,o,u),e.compute(uy(e.inputs[0],t,l,n,o,u),{inputs:[0]})},S2=e=>{let t=e.antialias,n=e.axes,o=e.coordinateTransformMode,u=e.cubicCoeffA,l=e.excludeOutside!==0,d=e.extrapolationValue,c=e.keepAspectRatioPolicy,s=e.mode,r=e.nearestMode===""?"simple":e.nearestMode;return Se({antialias:t,axes:n,coordinateTransformMode:o,cubicCoeffA:u,excludeOutside:l,extrapolationValue:d,keepAspectRatioPolicy:c,mode:s,nearestMode:r})}}),dy,cy,I2,ak=R(()=>{"use strict";se(),ae(),ue(),dy=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],n=e[1],o=e[2];if(t.dataType!==n.dataType||t.dataType!==o.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(n.dims.length!==3&&n.dims.length!==2)throw new Error("Skip must be 2D or 3D");let u=t.dims[t.dims.length-1],l=t.dims[t.dims.length-2];if(n.dims[n.dims.length-1]!==u)throw new Error("Skip must have the same hidden size as input");if(n.dims[n.dims.length-2]!==l)throw new Error("Skip must have the same sequence length as input");if(o.dims.length!==1)throw new Error("Gamma must be 1D");if(o.dims[o.dims.length-1]!==u)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let d=e[3];if(d.dims.length!==1)throw new Error("Beta must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let d=e[4];if(d.dims.length!==1)throw new Error("Bias must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Bias must have the same hidden size as input")}},cy=(e,t,n,o)=>{let u=t.simplified,l=e[0].dims,d=z.size(l),c=l,s=d,r=l.slice(-1)[0],i=o?l.slice(0,-1).concat(1):[],a=!u&&e.length>3,p=e.length>4,h=o&&n>1,m=o&&n>2,b=n>3,x=64,w=Ue(r),y=[{type:12,data:s},{type:12,data:w},{type:12,data:r},{type:1,data:t.epsilon}],S=I=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],P=[j("x",e[0].dataType,e[0].dims,w),j("skip",e[1].dataType,e[1].dims,w),j("gamma",e[2].dataType,e[2].dims,w)];a&&P.push(j("beta",e[3].dataType,e[3].dims,w)),p&&P.push(j("bias",e[4].dataType,e[4].dims,w)),P.push(re("output",e[0].dataType,c,w)),h&&P.push(re("mean_output",1,i)),m&&P.push(re("inv_std_output",1,i)),b&&P.push(re("input_skip_bias_sum",e[0].dataType,c,w));let O=Ye(e[0].dataType),B=Ye(1,w);return` + }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${n}|${r.length>0?t.mode==="cubic"?r:r.length:""}|${u.length>0?u:""}|${c.length>0?c:""}|${h}|${t.mode==="nearest"?d.length:d}`,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:[{type:12,data:p},{type:1,data:r},{type:1,data:c},...ae(d,s)]})}},gy=e=>{let t=e.customDataBuffer;return new Uint32Array(t.buffer,t.byteOffset,1)[0]},P2=(e,t)=>{let n=[],i=[],u=[],l=gy(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");ny(e.inputs,t,l,n,i,u),e.compute(my(e.inputs[0],t,l,n,i,u),{inputs:[0]})},C2=e=>{let t=e.antialias,n=e.axes,i=e.coordinateTransformMode,u=e.cubicCoeffA,l=e.excludeOutside!==0,d=e.extrapolationValue,c=e.keepAspectRatioPolicy,s=e.mode,r=e.nearestMode===""?"simple":e.nearestMode;return ke({antialias:t,axes:n,coordinateTransformMode:i,cubicCoeffA:u,excludeOutside:l,extrapolationValue:d,keepAspectRatioPolicy:c,mode:s,nearestMode:r})}}),by,yy,D2,wk=N(()=>{"use strict";le(),pe(),he(),by=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],n=e[1],i=e[2];if(t.dataType!==n.dataType||t.dataType!==i.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(n.dims.length!==3&&n.dims.length!==2)throw new Error("Skip must be 2D or 3D");let u=t.dims[t.dims.length-1],l=t.dims[t.dims.length-2];if(n.dims[n.dims.length-1]!==u)throw new Error("Skip must have the same hidden size as input");if(n.dims[n.dims.length-2]!==l)throw new Error("Skip must have the same sequence length as input");if(i.dims.length!==1)throw new Error("Gamma must be 1D");if(i.dims[i.dims.length-1]!==u)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let d=e[3];if(d.dims.length!==1)throw new Error("Beta must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let d=e[4];if(d.dims.length!==1)throw new Error("Bias must be 1D");if(d.dims[d.dims.length-1]!==u)throw new Error("Bias must have the same hidden size as input")}},yy=(e,t,n,i)=>{let u=t.simplified,l=e[0].dims,d=B.size(l),c=l,s=d,r=l.slice(-1)[0],o=i?l.slice(0,-1).concat(1):[],a=!u&&e.length>3,p=e.length>4,h=i&&n>1,m=i&&n>2,g=n>3,v=64,w=qe(r),y=[{type:12,data:s},{type:12,data:w},{type:12,data:r},{type:1,data:t.epsilon}],T=O=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],k=[L("x",e[0].dataType,e[0].dims,w),L("skip",e[1].dataType,e[1].dims,w),L("gamma",e[2].dataType,e[2].dims,w)];a&&k.push(L("beta",e[3].dataType,e[3].dims,w)),p&&k.push(L("bias",e[4].dataType,e[4].dims,w)),k.push(ne("output",e[0].dataType,c,w)),h&&k.push(ne("mean_output",1,o)),m&&k.push(ne("inv_std_output",1,o)),g&&k.push(ne("input_skip_bias_sum",e[0].dataType,c,w));let I=rt(e[0].dataType),R=rt(1,w);return` - ${I.registerUniforms(A).declareVariables(...P)} - var sum_shared : array<${B}, ${x}>; - var sum_squared_shared : array<${B}, ${x}>; + ${O.registerUniforms(A).declareVariables(...k)} + var sum_shared : array<${R}, ${v}>; + var sum_squared_shared : array<${R}, ${v}>; - ${I.mainStart([x,1,1])} + ${O.mainStart([v,1,1])} let ix = local_id.x; - let iy = global_id.x / ${x}; + let iy = global_id.x / ${v}; let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; - var stride = hidden_size_vectorized / ${x}; + var stride = hidden_size_vectorized / ${v}; let offset = ix * stride + iy * hidden_size_vectorized; let offset1d = stride * ix; - if (ix == ${x-1}) { + if (ix == ${v-1}) { stride = hidden_size_vectorized - stride * ix; } for (var i: u32 = 0; i < stride; i++) { let skip_value = skip[offset + i]; - let bias_value = ${p?"bias[offset1d + i]":O+"(0.0)"}; + let bias_value = ${p?"bias[offset1d + i]":I+"(0.0)"}; let input_value = x[offset + i]; let value = input_value + skip_value + bias_value; - ${b?"input_skip_bias_sum[offset + i] = value;":""} + ${g?"input_skip_bias_sum[offset + i] = value;":""} output[offset + i] = value; - let f32_value = ${$n(O,w,"value")}; + let f32_value = ${Pn(I,w,"value")}; sum_shared[ix] += f32_value; sum_squared_shared[ix] += f32_value * f32_value; } workgroupBarrier(); - var reduce_size : u32 = ${x}; + var reduce_size : u32 = ${v}; for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { reduce_size = curr_size + (reduce_size & 1); if (ix < curr_size) { @@ -4600,24 +4600,24 @@ fn calculateOutputIndex(index: u32) -> u32 { let sum = sum_shared[0]; let square_sum = sum_squared_shared[0]; - let mean = ${vr("sum",w)} / f32(uniforms.hidden_size); - let inv_std_dev = inverseSqrt(${vr("square_sum",w)} / f32(uniforms.hidden_size) ${u?"":"- mean * mean"} + uniforms.epsilon); + let mean = ${Or("sum",w)} / f32(uniforms.hidden_size); + let inv_std_dev = inverseSqrt(${Or("square_sum",w)} / f32(uniforms.hidden_size) ${u?"":"- mean * mean"} + uniforms.epsilon); ${h?"mean_output[global_idx] = mean;":""} ${m?"inv_std_output[global_idx] = inv_std_dev;":""} for (var i: u32 = 0; i < stride; i++) { - output[offset + i] = (output[offset + i] ${u?"":`- ${O}(mean)`}) * - ${O}(inv_std_dev) * gamma[offset1d + i] + output[offset + i] = (output[offset + i] ${u?"":`- ${I}(mean)`}) * + ${I}(inv_std_dev) * gamma[offset1d + i] ${a?"+ beta[offset1d + i]":""}; } - }`},T=[{dims:c,dataType:e[0].dataType}];return n>1&&T.push({dims:i,dataType:1}),n>2&&T.push({dims:i,dataType:1}),n>3&&T.push({dims:l,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${w};${h};${m};${b}`,inputDependencies:e.map((I,A)=>"type")},getShaderSource:S,getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(s/r)},programUniforms:y})}},I2=(e,t)=>{dy(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(cy(e.inputs,t,e.outputCount,!1),{outputs:n})}}),py,ui,hy,du,fy,my,O2,E2,uk=R(()=>{"use strict";se(),ae(),Ve(),ue(),py=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((n,o)=>{if(e[o+1].dataType!==6&&e[o+1].dataType!==7)throw new Error(`Input ${o} must be an array of int32 or int64`)})},ui=(e,t)=>{let n=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(o=>n.push(Number(o)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(o=>n.push(Number(o)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return n},hy=(e,t)=>{if(e.length>1){let n=ui(e,1),o=ui(e,2),u=ui(e,3);return u.length===0&&(u=[...Array(e[0].dims.length).keys()]),Se({starts:n,ends:o,axes:u})}else return t},du=(e,t,n,o,u)=>{let l=e;return e<0&&(l+=n[o[t]]),u[t]<0?Math.max(0,Math.min(l,n[o[t]]-1)):Math.max(0,Math.min(l,n[o[t]]))},fy=(e,t,n)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + }`},S=[{dims:c,dataType:e[0].dataType}];return n>1&&S.push({dims:o,dataType:1}),n>2&&S.push({dims:o,dataType:1}),n>3&&S.push({dims:l,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${w};${h};${m};${g}`,inputDependencies:e.map((O,A)=>"type")},getShaderSource:T,getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(s/r)},programUniforms:y})}},D2=(e,t)=>{by(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(yy(e.inputs,t,e.outputCount,!1),{outputs:n})}}),wy,mi,_y,fu,vy,xy,R2,N2,_k=N(()=>{"use strict";le(),pe(),He(),he(),wy=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((n,i)=>{if(e[i+1].dataType!==6&&e[i+1].dataType!==7)throw new Error(`Input ${i} must be an array of int32 or int64`)})},mi=(e,t)=>{let n=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(i=>n.push(Number(i)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(i=>n.push(Number(i)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return n},_y=(e,t)=>{if(e.length>1){let n=mi(e,1),i=mi(e,2),u=mi(e,3);return u.length===0&&(u=[...Array(e[0].dims.length).keys()]),ke({starts:n,ends:i,axes:u})}else return t},fu=(e,t,n,i,u)=>{let l=e;return e<0&&(l+=n[i[t]]),u[t]<0?Math.max(0,Math.min(l,n[i[t]]-1)):Math.max(0,Math.min(l,n[i[t]]))},vy=(e,t,n)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { var input_indices: ${e.type.indices}; var carry = 0u; for (var i = ${n.length-1}; i >= 0; i--) { - let input_shape_i = ${ne("uniforms.input_shape","i",n.length)}; - let steps_i = ${ne("uniforms.steps","i",n.length)}; - let signs_i = ${ne("uniforms.signs","i",n.length)}; - let starts_i = ${ne("uniforms.starts","i",n.length)}; + let input_shape_i = ${oe("uniforms.input_shape","i",n.length)}; + let steps_i = ${oe("uniforms.steps","i",n.length)}; + let signs_i = ${oe("uniforms.signs","i",n.length)}; + let starts_i = ${oe("uniforms.starts","i",n.length)}; var output_index = ${t.indicesGet("output_indices","i")}; var input_index = output_index * steps_i + starts_i + carry; carry = input_index / input_shape_i; @@ -4628,39 +4628,39 @@ fn calculateOutputIndex(index: u32) -> u32 { ${e.indicesSet("input_indices","i","input_index")}; } return input_indices; - }`,my=(e,t)=>{let n=e[0].dims,o=z.size(n),u=t.axes.length>0?z.normalizeAxes(t.axes,n.length):[...Array(n.length).keys()],l=ui(e,4);l.forEach(w=>w!==0||(()=>{throw new Error("step cannot be 0")})),l.length===0&&(l=Array(u.length).fill(1));let d=t.starts.map((w,y)=>du(w,y,n,u,l)),c=t.ends.map((w,y)=>du(w,y,n,u,l));if(u.length!==d.length||u.length!==c.length)throw new Error("start, ends and axes should have the same number of elements");if(u.length!==n.length)for(let w=0;wMath.sign(w));l.forEach((w,y,S)=>{if(w<0){let T=(c[y]-d[y])/w,I=d[y],A=I+T*l[y];d[y]=A,c[y]=I,S[y]=-w}});let r=n.slice(0);u.forEach((w,y)=>{r[w]=Math.ceil((c[w]-d[w])/l[w])});let i={dims:r,dataType:e[0].dataType},a=re("output",e[0].dataType,r.length),p=j("input",e[0].dataType,e[0].dims.length),h=z.size(r),m=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:d.length},{name:"signs",type:"i32",length:s.length},{name:"steps",type:"u32",length:l.length}],b=[{type:12,data:h},{type:12,data:d},{type:6,data:s},{type:12,data:l},...oe(e[0].dims,r)],x=w=>` + }`,xy=(e,t)=>{let n=e[0].dims,i=B.size(n),u=t.axes.length>0?B.normalizeAxes(t.axes,n.length):[...Array(n.length).keys()],l=mi(e,4);l.forEach(w=>w!==0||(()=>{throw new Error("step cannot be 0")})),l.length===0&&(l=Array(u.length).fill(1));let d=t.starts.map((w,y)=>fu(w,y,n,u,l)),c=t.ends.map((w,y)=>fu(w,y,n,u,l));if(u.length!==d.length||u.length!==c.length)throw new Error("start, ends and axes should have the same number of elements");if(u.length!==n.length)for(let w=0;wMath.sign(w));l.forEach((w,y,T)=>{if(w<0){let S=(c[y]-d[y])/w,O=d[y],A=O+S*l[y];d[y]=A,c[y]=O,T[y]=-w}});let r=n.slice(0);u.forEach((w,y)=>{r[w]=Math.ceil((c[w]-d[w])/l[w])});let o={dims:r,dataType:e[0].dataType},a=ne("output",e[0].dataType,r.length),p=L("input",e[0].dataType,e[0].dims.length),h=B.size(r),m=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:d.length},{name:"signs",type:"i32",length:s.length},{name:"steps",type:"u32",length:l.length}],g=[{type:12,data:h},{type:12,data:d},{type:6,data:s},{type:12,data:l},...ae(e[0].dims,r)],v=w=>` ${w.registerUniforms(m).declareVariables(p,a)} - ${fy(p,a,n)} + ${vy(p,a,n)} ${w.mainStart()} ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let output_indices = ${a.offsetToIndices("global_idx")}; let input_indices = calculateInputIndices(output_indices); ${a.setByOffset("global_idx",p.getByIndices("input_indices"))} - }`;return{name:"Slice",shaderCache:{hint:`${s.length}_${d.length}_${l.length}`,inputDependencies:["rank"]},getShaderSource:x,getRunData:()=>({outputs:[i],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:b})}},O2=(e,t)=>{py(e.inputs,t);let n=hy(e.inputs,t);e.compute(my(e.inputs,n),{inputs:[0]})},E2=e=>{let t=e.starts,n=e.ends,o=e.axes;return Se({starts:t,ends:n,axes:o})}}),gy,by,A2,k2,lk=R(()=>{"use strict";se(),ae(),Ve(),$r(),ue(),gy=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},by=(e,t)=>{let n=e.inputs[0],o=n.dims,u=z.size(o),l=o.length,d=z.normalizeAxis(t.axis,l),c=dO),r[d]=l-1,r[l-1]=d,s=e.compute(bt(n,r),{inputs:[n],outputs:[-1]})[0]):s=n;let i=s.dims,a=i[l-1],p=u/a,h=Ue(a),m=a/h,b=64;p===1&&(b=256);let x=(P,O)=>O===4?`max(max(${P}.x, ${P}.y), max(${P}.z, ${P}.w))`:O===2?`max(${P}.x, ${P}.y)`:O===3?`max(max(${P}.x, ${P}.y), ${P}.z)`:P,w=j("x",s.dataType,s.dims,h),y=re("result",s.dataType,s.dims,h),S=w.type.value,T=Ye(s.dataType)==="f32"?`var threadMax = ${S}(-3.4028234663852886e+38f);`:`var threadMax = ${S}(-65504.0h);`,I=P=>` - var rowMaxShared : ${S}; - var rowSumShared : ${S}; - var threadShared : array<${S}, ${b}>; + }`;return{name:"Slice",shaderCache:{hint:`${s.length}_${d.length}_${l.length}`,inputDependencies:["rank"]},getShaderSource:v,getRunData:()=>({outputs:[o],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:g})}},R2=(e,t)=>{wy(e.inputs,t);let n=_y(e.inputs,t);e.compute(xy(e.inputs,n),{inputs:[0]})},N2=e=>{let t=e.starts,n=e.ends,i=e.axes;return ke({starts:t,ends:n,axes:i})}}),$y,Ty,z2,M2,vk=N(()=>{"use strict";le(),pe(),He(),Ar(),he(),$y=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},Ty=(e,t)=>{let n=e.inputs[0],i=n.dims,u=B.size(i),l=i.length,d=B.normalizeAxis(t.axis,l),c=dI),r[d]=l-1,r[l-1]=d,s=e.compute(xt(n,r),{inputs:[n],outputs:[-1]})[0]):s=n;let o=s.dims,a=o[l-1],p=u/a,h=qe(a),m=a/h,g=64;p===1&&(g=256);let v=(k,I)=>I===4?`max(max(${k}.x, ${k}.y), max(${k}.z, ${k}.w))`:I===2?`max(${k}.x, ${k}.y)`:I===3?`max(max(${k}.x, ${k}.y), ${k}.z)`:k,w=L("x",s.dataType,s.dims,h),y=ne("result",s.dataType,s.dims,h),T=w.type.value,S=rt(s.dataType)==="f32"?`var threadMax = ${T}(-3.4028234663852886e+38f);`:`var threadMax = ${T}(-65504.0h);`,O=k=>` + var rowMaxShared : ${T}; + var rowSumShared : ${T}; + var threadShared : array<${T}, ${g}>; - fn getValue(row: i32, col: i32, row_stride: i32) -> ${S} { + fn getValue(row: i32, col: i32, row_stride: i32) -> ${T} { let index = row * row_stride + col; return x[index]; } - fn setValue(row: i32, col: i32, row_stride: i32, value: ${S}) { + fn setValue(row: i32, col: i32, row_stride: i32, value: ${T}) { let index = row * row_stride + col; result[index] = value; } - ${P.registerUniform("packedCols","i32").declareVariables(w,y)} - ${P.mainStart(b)} + ${k.registerUniform("packedCols","i32").declareVariables(w,y)} + ${k.mainStart(g)} let gindex = i32(global_idx); let lindex = i32(local_idx); - const wg = ${b}; + const wg = ${g}; let row = gindex / wg; let cols = uniforms.packedCols; let row_stride : i32 = uniforms.packedCols; // find the rows max - ${T} + ${S} for (var col = lindex; col < cols; col += wg) { let value = getValue(row, col, row_stride); threadMax = max(threadMax, value); @@ -4679,12 +4679,12 @@ fn calculateOutputIndex(index: u32) -> u32 { workgroupBarrier(); } if (lindex == 0) { - rowMaxShared = ${S}(${x("threadShared[0]",h)}); + rowMaxShared = ${T}(${v("threadShared[0]",h)}); } workgroupBarrier(); // find the rows sum - var threadSum = ${S}(0.0); + var threadSum = ${T}(0.0); for (var col = lindex; col < cols; col += wg) { let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); threadSum += subExp; @@ -4699,7 +4699,7 @@ fn calculateOutputIndex(index: u32) -> u32 { workgroupBarrier(); } if (lindex == 0) { - rowSumShared = ${S}(${vr("threadShared[0]",h)}); + rowSumShared = ${T}(${Or("threadShared[0]",h)}); } workgroupBarrier(); @@ -4707,14 +4707,14 @@ fn calculateOutputIndex(index: u32) -> u32 { for (var col = lindex; col < cols; col += wg) { var value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; // max operation protects against NaN since all values should be >=0 - value = max(value, ${S}(0.0)); + value = max(value, ${T}(0.0)); setValue(row, col, row_stride, value); } - }`,A=e.compute({name:"Softmax",shaderCache:{hint:`${h};${b}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:i,dataType:s.dataType}],dispatchGroup:{x:p},programUniforms:[{type:6,data:m}]}),getShaderSource:I},{inputs:[s],outputs:[c?-1:0]})[0];c&&e.compute(bt(A,r),{inputs:[A]})},A2=(e,t)=>{gy(e.inputs),by(e,t)},k2=e=>Se({axis:e.axis})}),cu,yy,wy,_y,P2,dk=R(()=>{"use strict";se(),ae(),ue(),cu=e=>Array.from(e.getBigInt64Array(),Number),yy=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(cu(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},wy=(e,t)=>{let n=[];for(let o=0;o{let n=e[0].dims,o=t??cu(e[1]),u=wy(n,o),l=z.size(u),d=e[0].dataType,c=j("input",d,n.length),s=re("output",d,u.length),r=i=>` + }`,A=e.compute({name:"Softmax",shaderCache:{hint:`${h};${g}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:o,dataType:s.dataType}],dispatchGroup:{x:p},programUniforms:[{type:6,data:m}]}),getShaderSource:O},{inputs:[s],outputs:[c?-1:0]})[0];c&&e.compute(xt(A,r),{inputs:[A]})},z2=(e,t)=>{$y(e.inputs),Ty(e,t)},M2=e=>ke({axis:e.axis})}),mu,Sy,Iy,Oy,B2,xk=N(()=>{"use strict";le(),pe(),he(),mu=e=>Array.from(e.getBigInt64Array(),Number),Sy=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(mu(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},Iy=(e,t)=>{let n=[];for(let i=0;i{let n=e[0].dims,i=t??mu(e[1]),u=Iy(n,i),l=B.size(u),d=e[0].dataType,c=L("input",d,n.length),s=ne("output",d,u.length),r=o=>` const inputShape = ${c.indices(...n)}; - ${i.registerUniform("output_size","u32").declareVariables(c,s)} - ${i.mainStart()} - ${i.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${o.registerUniform("output_size","u32").declareVariables(c,s)} + ${o.mainStart()} + ${o.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let output_indices = ${s.offsetToIndices("global_idx")}; var input_indices: ${c.type.indices}; for (var i = 0; i < ${n.length}; i++) { @@ -4724,7 +4724,7 @@ fn calculateOutputIndex(index: u32) -> u32 { ${c.indicesSet("input_indices","i","input_dim_value")} } ${s.setByOffset("global_idx",c.getByIndices("input_indices"))} - }`;return{name:"Tile",shaderCache:{hint:`${o}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:[{type:12,data:l},...oe(e[0].dims,u)]}),getShaderSource:r}},P2=e=>{yy(e.inputs),e.compute(_y(e.inputs),{inputs:[0]})}}),vy,xy,C2,ck=R(()=>{"use strict";se(),ae(),ue(),vy=(e,t,n,o,u)=>{let l=re("output_data",u,n.length,4),d=j("a_data",t[1].dataType,t[1].dims.length,4),c=j("b_data",t[2].dataType,t[2].dims.length,4),s=j("c_data",t[0].dataType,t[0].dims.length,4),r,i=(a,p,h)=>`select(${p}, ${a}, ${h})`;if(!o)r=l.setByOffset("global_idx",i(d.getByOffset("global_idx"),c.getByOffset("global_idx"),s.getByOffset("global_idx")));else{let a=(p,h,m="")=>{let b=`a_data[index_a${h}][component_a${h}]`,x=`b_data[index_b${h}][component_b${h}]`,w=`bool(c_data[index_c${h}] & (0xffu << (component_c${h} * 8)))`;return` + }`;return{name:"Tile",shaderCache:{hint:`${i}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:[{type:12,data:l},...ae(e[0].dims,u)]}),getShaderSource:r}},B2=e=>{Sy(e.inputs),e.compute(Oy(e.inputs),{inputs:[0]})}}),Ey,Ay,F2,$k=N(()=>{"use strict";le(),pe(),he(),Ey=(e,t,n,i,u)=>{let l=ne("output_data",u,n.length,4),d=L("a_data",t[1].dataType,t[1].dims.length,4),c=L("b_data",t[2].dataType,t[2].dims.length,4),s=L("c_data",t[0].dataType,t[0].dims.length,4),r,o=(a,p,h)=>`select(${p}, ${a}, ${h})`;if(!i)r=l.setByOffset("global_idx",o(d.getByOffset("global_idx"),c.getByOffset("global_idx"),s.getByOffset("global_idx")));else{let a=(p,h,m="")=>{let g=`a_data[index_a${h}][component_a${h}]`,v=`b_data[index_b${h}][component_b${h}]`,w=`bool(c_data[index_c${h}] & (0xffu << (component_c${h} * 8)))`;return` let output_indices${h} = ${l.offsetToIndices(`global_idx * 4u + ${h}u`)}; let offset_a${h} = ${d.broadcastedIndicesToOffset(`output_indices${h}`,l)}; let offset_b${h} = ${c.broadcastedIndicesToOffset(`output_indices${h}`,l)}; @@ -4735,7 +4735,7 @@ fn calculateOutputIndex(index: u32) -> u32 { let component_a${h} = offset_a${h} % 4u; let component_b${h} = offset_b${h} % 4u; let component_c${h} = offset_c${h} % 4u; - ${p}[${h}] = ${m}(${i(b,x,w)}); + ${p}[${h}] = ${m}(${o(g,v,w)}); `};u===9?r=` var data = vec4(0); ${a("data",0,"u32")} @@ -4752,10 +4752,10 @@ fn calculateOutputIndex(index: u32) -> u32 { ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} ${r} - }`},xy=e=>{let t=e[1].dims,n=e[2].dims,o=e[0].dims,u=e[1].dataType,l=!(z.areEqual(t,n)&&z.areEqual(n,o)),d=t,c=z.size(t);if(l){let r=In.calcShape(In.calcShape(t,n,!1),o,!1);if(!r)throw new Error("Can't perform where op on the given tensors");d=r,c=z.size(d)}let s=Math.ceil(c/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:r=>vy(r,e,d,l,u),getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:s},...oe(o,t,n,d)]})}},C2=e=>{e.compute(xy(e.inputs))}}),D2,pk=R(()=>{"use strict";SA(),ql(),IA(),OA(),EA(),AA(),kA(),NA(),BA(),MA(),FA(),jA(),LA(),UA(),VA(),GA(),qA(),HA(),WA(),KA(),XA(),ZA(),JA(),YA(),QA(),ek(),Y1(),tk(),rk(),nk(),ik(),ok(),Gl(),sk(),n2(),ak(),uk(),lk(),t2(),dk(),$r(),Hl(),ck(),D2=new Map([["Abs",[$x]],["Acos",[Tx]],["Acosh",[Sx]],["Add",[s1]],["ArgMax",[wx,pl]],["ArgMin",[yx,pl]],["Asin",[Ix]],["Asinh",[Ox]],["Atan",[Ex]],["Atanh",[Ax]],["Attention",[_x]],["AveragePool",[p2,c2]],["BatchNormalization",[vx]],["BiasAdd",[xx]],["BiasSplitGelu",[o1]],["Cast",[Px,kx]],["Ceil",[Dx]],["Clip",[Cx]],["Concat",[g1,b1]],["Conv",[yl,bl]],["ConvTranspose",[O1,I1]],["Cos",[Rx]],["Cosh",[Nx]],["CumSum",[E1,A1]],["DepthToSpace",[k1,P1]],["DequantizeLinear",[w2,_2]],["DFT",[C1,D1]],["Div",[a1]],["Einsum",[R1,N1]],["Elu",[zx,bi]],["Equal",[u1]],["Erf",[Bx]],["Exp",[Mx]],["Expand",[z1]],["FastGelu",[B1]],["Floor",[Fx]],["FusedConv",[yl,bl]],["Gather",[F1,M1]],["GatherElements",[q1,G1]],["GatherBlockQuantized",[U1,V1]],["GatherND",[j1,L1]],["Gelu",[jx]],["Gemm",[W1,H1]],["GlobalAveragePool",[f2,h2]],["GlobalMaxPool",[y2,b2]],["Greater",[p1]],["GreaterOrEqual",[f1]],["GridSample",[K1,X1]],["GroupQueryAttention",[i2]],["HardSigmoid",[Kx,Wx]],["HardSwish",[Xx]],["InstanceNormalization",[o2]],["LayerNormalization",[s2]],["LeakyRelu",[Lx,bi]],["Less",[h1]],["LessOrEqual",[m1]],["Log",[n1]],["MatMul",[a2]],["MatMulNBits",[u2,l2]],["MaxPool",[m2,g2]],["Mul",[l1]],["MultiHeadAttention",[J1,Z1]],["Neg",[Vx]],["Not",[Ux]],["Pad",[d2]],["Pow",[d1]],["QuickGelu",[i1,bi]],["Range",[v2]],["Reciprocal",[Gx]],["ReduceMin",[hx]],["ReduceMean",[ux]],["ReduceMax",[px]],["ReduceSum",[mx]],["ReduceProd",[fx]],["ReduceL1",[lx]],["ReduceL2",[dx]],["ReduceLogSum",[bx]],["ReduceLogSumExp",[cx]],["ReduceSumSquare",[gx]],["Relu",[qx]],["Resize",[T2,S2]],["RotaryEmbedding",[r2]],["ScatterND",[$2,x2]],["Sigmoid",[Hx]],["Sin",[Zx]],["Sinh",[Jx]],["Slice",[O2,E2]],["SkipLayerNormalization",[I2]],["Split",[Q1,e2]],["Sqrt",[Yx]],["Softmax",[A2,k2]],["Sub",[c1]],["Tan",[Qx]],["Tanh",[e1]],["ThresholdedRelu",[r1,bi]],["Tile",[P2]],["Transpose",[Zv,Jv]],["Where",[C2]]])}),R2,hk=R(()=>{"use strict";ot(),Qt(),ue(),R2=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n,o,u){Gt(e.programInfo.name);let l=this.backend.device,d=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let c=[];for(let r of t)c.push({binding:c.length,resource:{buffer:r.buffer}});for(let r of n)c.push({binding:c.length,resource:{buffer:r.buffer}});u&&c.push({binding:c.length,resource:u});let s=l.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:c,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let r={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:s,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(r)}d.setPipeline(e.computePipeline),d.setBindGroup(0,s),d.dispatchWorkgroups(...o),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),zt(e.programInfo.name)}dispose(){}build(e,t){Gt(e.name);let n=this.backend.device,o=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(r=>{n.features.has(r.feature)&&o.push(`enable ${r.extension};`)});let u=Xv(t,this.backend.device.limits),l=e.getShaderSource(u),d=`${o.join(` + }`},Ay=e=>{let t=e[1].dims,n=e[2].dims,i=e[0].dims,u=e[1].dataType,l=!(B.areEqual(t,n)&&B.areEqual(n,i)),d=t,c=B.size(t);if(l){let r=Rn.calcShape(Rn.calcShape(t,n,!1),i,!1);if(!r)throw new Error("Can't perform where op on the given tensors");d=r,c=B.size(d)}let s=Math.ceil(c/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:r=>Ey(r,e,d,l,u),getRunData:()=>({outputs:[{dims:d,dataType:u}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:s},...ae(i,t,n,d)]})}},F2=e=>{e.compute(Ay(e.inputs))}}),j2,Tk=N(()=>{"use strict";MA(),Xl(),BA(),FA(),jA(),LA(),UA(),WA(),XA(),ZA(),YA(),JA(),QA(),ek(),tk(),rk(),nk(),ik(),ok(),sk(),ak(),uk(),lk(),dk(),ck(),pk(),o2(),hk(),fk(),mk(),gk(),bk(),Kl(),yk(),d2(),wk(),_k(),vk(),u2(),xk(),Ar(),Zl(),$k(),j2=new Map([["Abs",[kx]],["Acos",[Px]],["Acosh",[Cx]],["Add",[h1]],["ArgMax",[Ix,gl]],["ArgMin",[Sx,gl]],["Asin",[Dx]],["Asinh",[Rx]],["Atan",[Nx]],["Atanh",[zx]],["Attention",[Ox]],["AveragePool",[w2,y2]],["BatchNormalization",[Ex]],["BiasAdd",[Ax]],["BiasSplitGelu",[p1]],["Cast",[Bx,Mx]],["Ceil",[jx]],["Clip",[Fx]],["Concat",[$1,T1]],["Conv",[xl,vl]],["ConvTranspose",[R1,D1]],["Cos",[Lx]],["Cosh",[Ux]],["CumSum",[N1,z1]],["DepthToSpace",[M1,B1]],["DequantizeLinear",[I2,O2]],["DFT",[F1,j1]],["Div",[f1]],["Einsum",[L1,U1]],["Elu",[Vx,Ti]],["Equal",[m1]],["Erf",[Gx]],["Exp",[qx]],["Expand",[V1]],["FastGelu",[G1]],["Floor",[Hx]],["FusedConv",[xl,vl]],["Gather",[H1,q1]],["GatherElements",[J1,Y1]],["GatherBlockQuantized",[X1,Z1]],["GatherND",[W1,K1]],["Gelu",[Wx]],["Gemm",[e2,Q1]],["GlobalAveragePool",[v2,_2]],["GlobalMaxPool",[S2,T2]],["Greater",[w1]],["GreaterOrEqual",[v1]],["GridSample",[t2,r2]],["GroupQueryAttention",[c2]],["HardSigmoid",[t1,e1]],["HardSwish",[r1]],["InstanceNormalization",[p2]],["LayerNormalization",[h2]],["LeakyRelu",[Kx,Ti]],["Less",[_1]],["LessOrEqual",[x1]],["Log",[d1]],["MatMul",[f2]],["MatMulNBits",[m2,g2]],["MaxPool",[x2,$2]],["Mul",[g1]],["MultiHeadAttention",[i2,n2]],["Neg",[Zx]],["Not",[Xx]],["Pad",[b2]],["Pow",[b1]],["QuickGelu",[c1,Ti]],["Range",[E2]],["Reciprocal",[Yx]],["ReduceMin",[_x]],["ReduceMean",[mx]],["ReduceMax",[wx]],["ReduceSum",[xx]],["ReduceProd",[vx]],["ReduceL1",[gx]],["ReduceL2",[bx]],["ReduceLogSum",[Tx]],["ReduceLogSumExp",[yx]],["ReduceSumSquare",[$x]],["Relu",[Jx]],["Resize",[P2,C2]],["RotaryEmbedding",[l2]],["ScatterND",[k2,A2]],["Sigmoid",[Qx]],["Sin",[n1]],["Sinh",[i1]],["Slice",[R2,N2]],["SkipLayerNormalization",[D2]],["Split",[s2,a2]],["Sqrt",[o1]],["Softmax",[z2,M2]],["Sub",[y1]],["Tan",[s1]],["Tanh",[a1]],["ThresholdedRelu",[l1,Ti]],["Tile",[B2]],["Transpose",[nx,ix]],["Where",[F2]]])}),L2,Sk=N(()=>{"use strict";dt(),ar(),he(),L2=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n,i,u){Jt(e.programInfo.name);let l=this.backend.device,d=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let c=[];for(let r of t)c.push({binding:c.length,resource:{buffer:r.buffer}});for(let r of n)c.push({binding:c.length,resource:{buffer:r.buffer}});u&&c.push({binding:c.length,resource:u});let s=l.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:c,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let r={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:s,dispatchGroup:i};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(r)}d.setPipeline(e.computePipeline),d.setBindGroup(0,s),d.dispatchWorkgroups(...i),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Vt(e.programInfo.name)}dispose(){}build(e,t){Jt(e.name);let n=this.backend.device,i=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(r=>{n.features.has(r.feature)&&i.push(`enable ${r.extension};`)});let u=rx(t,this.backend.device.limits),l=e.getShaderSource(u),d=`${i.join(` `)} ${u.additionalImplementations} -${l}`,c=n.createShaderModule({code:d,label:e.name});ve("verbose",()=>`[WebGPU] ${e.name} shader code: ${d}`);let s=n.createComputePipeline({compute:{module:c,entryPoint:"main"},layout:"auto",label:e.name});return zt(e.name),{programInfo:e,computePipeline:s,uniformVariablesInfo:u.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,n=typeof e=="number"?1:e.y||1,o=typeof e=="number"?1:e.z||1,u=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=u&&n<=u&&o<=u)return[t,n,o];let l=t*n*o,d=Math.ceil(Math.sqrt(l));if(d>u){if(d=Math.ceil(Math.cbrt(l)),d>u)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[d,d,d]}else return[d,d,1]}}}),N2={};un(N2,{WebGpuBackend:()=>z2});var $y,Ty,Sy,z2,fk=R(()=>{"use strict";ot(),se(),Qt(),Gv(),$A(),pk(),hk(),$y=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let n=[];for(let o=0;o{let o=e.name;return e.shaderCache?.hint&&(o+="["+e.shaderCache.hint+"]"),o+=":"+n+`:${$y(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,o},Sy=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vendor)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},z2=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n},u=c=>t.features.has(c)&&n.push(c)&&!0;u("chromium-experimental-timestamp-query-inside-passes")||u("timestamp-query"),u("shader-f16"),u("subgroups"),this.device=await t.requestDevice(o);let l=t,d=t.info??(typeof l.requestAdapterInfo=="function"?await l.requestAdapterInfo():void 0);this.adapterInfo=new Sy(d),this.gpuDataManager=Wv(this),this.programManager=new R2(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,jl(e.logLevel,!!e.debug),this.device.onuncapturederror=c=>{c.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${c.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!0}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose(),this.device&&this.env?.webgpu&&this.device.lost.then(()=>{delete this.env.webgpu.device})}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Gt(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{let t=new BigUint64Array(e.getMappedRange()),n=this.pendingQueries.get(e);for(let o=0;o"u"&&(this.queryTimeBase=p);let m=Number(p-this.queryTimeBase),b=Number(h-this.queryTimeBase);if(!Number.isSafeInteger(m)||!Number.isSafeInteger(b))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:i.map(x=>({dims:x.dims,dataType:Jt(x.dataType)})),outputsMetadata:a.map(x=>({dims:x.dims,dataType:Jt(x.dataType)})),kernelId:l,kernelType:c,kernelName:s,programName:r,startTime:m,endTime:b});else{let x="";i.forEach((y,S)=>{x+=`input[${S}]: [${y.dims}] | ${Jt(y.dataType)}, `});let w="";a.forEach((y,S)=>{w+=`output[${S}]: [${y.dims}] | ${Jt(y.dataType)}, `}),console.log(`[profiling] kernel "${l}|${c}|${s}|${r}" ${x}${w}start time: ${m} ns, execution time: ${b-m} ns`)}To("GPU",`${r}::${p}::${h}`)}e.unmap(),this.pendingQueries.delete(e)}),zt()}run(e,t,n,o,u,l){Gt(e.name);let d=[];for(let y=0;yS):n;if(i.length!==c.length)throw new Error(`Output size ${i.length} must be equal to ${c.length}.`);let a=[],p=[];for(let y=0;y=l)throw new Error(`Invalid output index: ${i[y]}`);if(i[y]===-3)continue;let S=i[y]===-1,T=i[y]===-2,I=S||T?u(c[y].dataType,c[y].dims):o(i[y],c[y].dataType,c[y].dims);if(a.push(I),I.data===0)continue;let A=this.gpuDataManager.get(I.data);if(!A)throw new Error(`no GPU data for output: ${I.data}`);if(S&&this.temporaryData.push(A),T){let P=this.kernelPersistentData.get(this.currentKernelId);P||(P=[],this.kernelPersistentData.set(this.currentKernelId,P)),P.push(A)}p.push(A)}if(d.length!==t.length||p.length!==a.length){if(p.length===0)return zt(e.name),a;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let h;if(r){let y=0,S=[];r.forEach(P=>{let O=typeof P.data=="number"?[P.data]:P.data;if(O.length===0)return;let B=P.type===10?2:4,U,X;P.type===10?(X=O.length>4?16:O.length>2?8:O.length*B,U=O.length>4?16:B*O.length):(X=O.length<=2?O.length*B:16,U=16),y=Math.ceil(y/X)*X,S.push(y);let q=P.type===10?8:4;y+=O.length>4?Math.ceil(O.length/q)*U:O.length*B});let T=16;y=Math.ceil(y/T)*T;let I=new ArrayBuffer(y);r.forEach((P,O)=>{let B=S[O],U=typeof P.data=="number"?[P.data]:P.data;if(P.type===6)new Int32Array(I,B,U.length).set(U);else if(P.type===12)new Uint32Array(I,B,U.length).set(U);else if(P.type===10)new Uint16Array(I,B,U.length).set(U);else if(P.type===1)new Float32Array(I,B,U.length).set(U);else throw new Error(`Unsupported uniform type: ${Jt(P.type)}`)});let A=this.gpuDataManager.create(y,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(A.buffer,0,I,0,y),this.gpuDataManager.release(A.id),h={offset:0,size:y,buffer:A.buffer}}let m=this.programManager.normalizeDispatchGroupSize(s),b=m[1]===1&&m[2]===1,x=Ty(e,t,b),w=this.programManager.getArtifact(x);if(w||(w=this.programManager.build(e,m),this.programManager.setArtifact(x,w),ve("info",()=>`[artifact] key: ${x}, programName: ${e.name}`)),r&&w.uniformVariablesInfo){if(r.length!==w.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${w.uniformVariablesInfo.length}, got ${r.length} in program "${w.programInfo.name}".`);for(let y=0;y`[ProgramManager] run "${e.name}" (key=${x}) with ${m[0]}x${m[1]}x${m[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let y={kernelId:this.currentKernelId,programName:w.programInfo.name,inputTensorViews:t,outputTensorViews:a};this.pendingKernels.push(y),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(y)}return this.programManager.run(w,d,p,m,h),zt(e.name),a}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,n,o){let u=D2.get(e);if(!u)throw new Error(`kernel not implemented: ${e}`);let l={kernelType:e,kernelName:o,kernelEntry:u[0],attributes:[u[1],n]};this.kernels.set(t,l)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let n of t)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,n){let o=this.kernels.get(e);if(!o)throw new Error(`kernel not created: ${e}`);let u=o.kernelType,l=o.kernelName,d=o.kernelEntry,c=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${u}] ${l}" is not allowed to be called recursively`);this.currentKernelId=e,c[0]&&(c[1]=c[0](c[1]),c[0]=void 0),ve("info",()=>`[WebGPU] Start to run kernel "[${u}] ${l}"...`);let s=this.env.debug;this.temporaryData=[];try{return s&&this.device.pushErrorScope("validation"),d(t,c[1]),0}catch(r){return n.push(Promise.resolve(`[WebGPU] Kernel "[${u}] ${l}" failed. ${r}`)),1}finally{s&&n.push(this.device.popErrorScope().then(r=>r?`GPU validation error for kernel "[${u}] ${l}": ${r.message}`:null));for(let r of this.temporaryData)this.gpuDataManager.release(r.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,n,o){let u=this.sessionExternalDataMapping.get(e);u||(u=new Map,this.sessionExternalDataMapping.set(e,u));let l=u.get(t),d=this.gpuDataManager.registerExternalBuffer(n,o,l);return u.set(t,[d,n]),d}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,n){return async()=>{let o=await ll(this,e,t);return Ll(o.buffer,n)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){ve("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){ve("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){ve("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),n=e.length;this.pendingKernels=[];for(let o=0;o=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),B2={};un(B2,{init:()=>M2});var bo,Iy,M2,mk=R(()=>{"use strict";se(),Qt(),ae(),xA(),bo=class F2{constructor(t,n,o,u){this.module=t,this.dataType=n,this.data=o,this.dims=u}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(z.size(t)!==z.size(this.dims))throw new Error("Invalid new shape");return new F2(this.module,this.dataType,this.data,t)}},Iy=class{constructor(e,t,n){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let o=e.PTR_SIZE,u=n/e.PTR_SIZE,l=o===4?"i32":"i64";this.opKernelContext=Number(e.getValue(o*u++,l));let d=Number(e.getValue(o*u++,l));this.outputCount=Number(e.getValue(o*u++,l)),this.customDataOffset=Number(e.getValue(o*u++,"*")),this.customDataSize=Number(e.getValue(o*u++,l));let c=[];for(let s=0;stypeof d=="number"?this.inputs[d]:d)??this.inputs,o=t?.outputs??[],u=(d,c,s)=>new bo(this.module,c,this.output(d,s),s),l=(d,c)=>{let s=Qr(d,c);if(!s)throw new Error(`Unsupported data type: ${d}`);let r=s>0?this.backend.gpuDataManager.create(s).id:0;return new bo(this.module,d,r,c)};return this.backend.run(e,n,o,u,l,this.outputCount)}output(e,t){let n=this.module.stackSave();try{let o=this.module.PTR_SIZE,u=o===4?"i32":"i64",l=this.module.stackAlloc((1+t.length)*o);this.module.setValue(l,t.length,u);for(let d=0;d{let u=t.jsepInit;if(!u)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let l=(fk(),Tn(N2)).WebGpuBackend,d=new l;await d.initialize(n,o),u("webgpu",[d,c=>d.alloc(Number(c)),c=>d.free(c),(c,s,r,i=!1)=>{if(i)ve("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(c)}, dst=${Number(s)}, size=${Number(r)}`),d.memcpy(Number(c),Number(s));else{ve("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(c)}, gpuDataId=${Number(s)}, size=${Number(r)}`);let a=t.HEAPU8.subarray(Number(c>>>0),Number(c>>>0)+Number(r));d.upload(Number(s),a)}},async(c,s,r)=>{ve("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${c}, dataOffset=${s}, size=${r}`),await d.download(Number(c),()=>t.HEAPU8.subarray(Number(s)>>>0,Number(s+r)>>>0))},(c,s,r)=>d.createKernel(c,Number(s),r,t.UTF8ToString(t._JsepGetNodeName(Number(s)))),c=>d.releaseKernel(c),(c,s,r,i)=>{ve("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${r}, kernel=${c}, contextDataOffset=${s}`);let a=new Iy(t,d,Number(s));return d.computeKernel(Number(c),a,i)},()=>d.captureBegin(),()=>d.captureEnd(),()=>d.replay()])}else{let l=new Hv(n);u("webnn",[l,()=>l.reserveTensorId(),d=>l.releaseTensorId(d),async(d,c,s,r,i)=>l.ensureTensor(d,c,s,r,i),(d,c)=>{l.uploadTensor(d,c)},async(d,c)=>l.downloadTensor(d,c),(d,c)=>l.registerMLContext(d,c),!!n.trace])}}}),Oy,Yl,Ql,cr,Ey,pu,No,ed,td,hu,rd,nd,id,j2=R(()=>{"use strict";ot(),wA(),_A(),se(),dn(),zl(),jv(),Oy=(e,t)=>{Re()._OrtInit(e,t)!==0&&Ae("Can't initialize onnxruntime.")},Yl=async e=>{Oy(e.wasm.numThreads,ko(e.logLevel))},Ql=async(e,t)=>{Re().asyncInit?.();let n=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let u=e.webgpu.forceFallbackAdapter;if(u!==void 0&&typeof u!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${u}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:u}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let o=(mk(),Tn(B2)).init;t==="webgpu"&&await o("webgpu",Re(),e,n),t==="webnn"&&await o("webnn",Re(),e)}},cr=new Map,Ey=e=>{let t=Re(),n=t.stackSave();try{let o=t.PTR_SIZE,u=t.stackAlloc(2*o);t._OrtGetInputOutputCount(e,u,u+o)!==0&&Ae("Can't get session input/output count.");let l=o===4?"i32":"i64";return[Number(t.getValue(u,l)),Number(t.getValue(u+o,l))]}finally{t.stackRestore(n)}},pu=(e,t)=>{let n=Re(),o=n.stackSave(),u=0;try{let l=n.PTR_SIZE,d=n.stackAlloc(2*l);n._OrtGetInputOutputMetadata(e,t,d,d+l)!==0&&Ae("Can't get session input/output metadata.");let c=Number(n.getValue(d,"*"));u=Number(n.getValue(d+l,"*"));let s=n.HEAP32[u/4];if(s===0)return[c,0];let r=n.HEAPU32[u/4+1],i=[];for(let a=0;a{let t=Re(),n=t._malloc(e.byteLength);if(n===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,n),[n,e.byteLength]},ed=async(e,t)=>{let n,o,u=Re();Array.isArray(e)?[n,o]=e:e.buffer===u.HEAPU8.buffer?[n,o]=[e.byteOffset,e.byteLength]:[n,o]=No(e);let l=0,d=0,c=0,s=[],r=[],i=[];try{if([d,s]=await Fv(t),t?.externalData&&u.mountExternalData){let T=[];for(let I of t.externalData){let A=typeof I=="string"?I:I.path,P=typeof I=="string"?I:I.data;T.push(Fl(P).then(O=>{u.mountExternalData(A,O)}))}await Promise.all(T)}for(let T of t?.executionProviders??[])if((typeof T=="string"?T:T.name)==="webnn"){if(u.shouldTransferToMLTensor=!1,typeof T!="string"){let I=T,A=I?.context,P=I?.gpuDevice,O=I?.deviceType,B=I?.powerPreference;A?u.currentContext=A:P?u.currentContext=await u.webnnCreateMLContext(P):u.currentContext=await u.webnnCreateMLContext({deviceType:O,powerPreference:B})}else u.currentContext=await u.webnnCreateMLContext();break}l=await u._OrtCreateSession(n,o,d),u.webgpuOnCreateSession?.(l),l===0&&Ae("Can't create a session."),u.jsepOnCreateSession?.(),u.currentContext&&(u.webnnRegisterMLContext(l,u.currentContext),u.currentContext=void 0,u.shouldTransferToMLTensor=!0);let[a,p]=Ey(l),h=!!t?.enableGraphCapture,m=[],b=[],x=[],w=[],y=[];for(let T=0;TT==="gpu-buffer"||T==="ml-tensor"||T==="ml-tensor-cpu-output")&&(c=u._OrtCreateBinding(l),c===0&&Ae("Can't create IO binding."),S={handle:c,outputPreferredLocations:y,outputPreferredLocationsEncoded:y.map(T=>T==="ml-tensor-cpu-output"?"ml-tensor":T).map(T=>ul(T))}),cr.set(l,[l,r,i,S,h,!1]),[l,m,b,x,w]}catch(a){throw r.forEach(p=>u._OrtFree(p)),i.forEach(p=>u._OrtFree(p)),c!==0&&u._OrtReleaseBinding(c)!==0&&Ae("Can't release IO binding."),l!==0&&u._OrtReleaseSession(l)!==0&&Ae("Can't release session."),a}finally{u._free(n),d!==0&&u._OrtReleaseSessionOptions(d)!==0&&Ae("Can't release session options."),s.forEach(a=>u._free(a)),u.unmountExternalData?.()}},td=e=>{let t=Re(),n=cr.get(e);if(!n)throw new Error(`cannot release session. invalid session id: ${e}`);let[o,u,l,d,c]=n;d&&(c&&t._OrtClearBoundOutputs(d.handle)!==0&&Ae("Can't clear bound outputs."),t._OrtReleaseBinding(d.handle)!==0&&Ae("Can't release IO binding.")),t.jsepOnReleaseSession?.(e),t.webnnOnReleaseSession?.(e),t.webgpuOnReleaseSession?.(e),u.forEach(s=>t._OrtFree(s)),l.forEach(s=>t._OrtFree(s)),t._OrtReleaseSession(o)!==0&&Ae("Can't release session."),cr.delete(e)},hu=async(e,t,n,o,u,l,d=!1)=>{if(!e){t.push(0);return}let c=Re(),s=c.PTR_SIZE,r=e[0],i=e[1],a=e[3],p=a,h,m;if(r==="string"&&(a==="gpu-buffer"||a==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(d&&a!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${l} when enableGraphCapture is true.`);if(a==="gpu-buffer"){let w=e[2].gpuBuffer;m=Qr(Yr(r),i);{let y=c.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');h=y(o,l,w,m)}}else if(a==="ml-tensor"){let w=e[2].mlTensor;m=Qr(Yr(r),i);let y=c.webnnRegisterMLTensor;if(!y)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');h=y(o,w,Yr(r),i)}else{let w=e[2];if(Array.isArray(w)){m=s*w.length,h=c._malloc(m),n.push(h);for(let y=0;yc.setValue(x+S*s,y,s===4?"i32":"i64"));let w=c._OrtCreateTensor(Yr(r),h,m,x,i.length,ul(p));w===0&&Ae(`Can't create tensor for input/output. session=${o}, index=${l}.`),t.push(w)}finally{c.stackRestore(b)}},rd=async(e,t,n,o,u,l)=>{let d=Re(),c=d.PTR_SIZE,s=cr.get(e);if(!s)throw new Error(`cannot run inference. invalid session id: ${e}`);let r=s[0],i=s[1],a=s[2],p=s[3],h=s[4],m=s[5],b=t.length,x=o.length,w=0,y=[],S=[],T=[],I=[],A=[],P=d.stackSave(),O=d.stackAlloc(b*c),B=d.stackAlloc(b*c),U=d.stackAlloc(x*c),X=d.stackAlloc(x*c);try{[w,y]=Mv(l),tn("wasm prepareInputOutputTensor");for(let _=0;_Oe*Le,1);H=Jt(Z);let Fe=p?.outputPreferredLocations[o[_]];if(H==="string"){if(Fe==="gpu-buffer"||Fe==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let Oe=[];for(let Le=0;Le0){let Oe=d.jsepGetBuffer;if(!Oe)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let Le=Oe(M),ut=Qr(Z,ye);if(ut===void 0||!Bl(H))throw new Error(`Unsupported data type: ${H}`);G=!0,D.push([H,fe,{gpuBuffer:Le,download:d.jsepCreateDownloader(Le,ut,H),dispose:()=>{d._OrtReleaseTensor(C)!==0&&Ae("Can't release tensor.")}},"gpu-buffer"])}else if(Fe==="ml-tensor"&&ye>0){let Oe=d.webnnEnsureTensor,Le=d.webnnIsGraphInputOutputTypeSupported;if(!Oe||!Le)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(Qr(Z,ye)===void 0||!Ml(H))throw new Error(`Unsupported data type: ${H}`);if(!Le(e,H,!1))throw new Error(`preferredLocation "ml-tensor" for ${H} output is not supported by current WebNN Context.`);let ut=await Oe(e,M,Z,fe,!1);G=!0,D.push([H,fe,{mlTensor:ut,download:d.webnnCreateMLTensorDownloader(M,H),dispose:()=>{d.webnnReleaseTensorId(M),d._OrtReleaseTensor(C)}},"ml-tensor"])}else if(Fe==="ml-tensor-cpu-output"&&ye>0){let Oe=d.webnnCreateMLTensorDownloader(M,H)(),Le=D.length;G=!0,Q.push((async()=>{let ut=[Le,await Oe];return d.webnnReleaseTensorId(M),d._OrtReleaseTensor(C),ut})()),D.push([H,fe,[],"cpu"])}else{let Oe=Go(H),Le=new Oe(ye);new Uint8Array(Le.buffer,Le.byteOffset,Le.byteLength).set(d.HEAPU8.subarray(M,M+Le.byteLength)),D.push([H,fe,Le,"cpu"])}}finally{d.stackRestore(F),H==="string"&&M&&d._free(M),G||d._OrtReleaseTensor(C)}}p&&!h&&(d._OrtClearBoundOutputs(p.handle)!==0&&Ae("Can't clear bound outputs."),cr.set(e,[r,i,a,p,h,!1]));for(let[_,C]of await Promise.all(Q))D[_][2]=C;return rn("wasm ProcessOutputTensor"),D}finally{d.webnnOnRunEnd?.(r),d.stackRestore(P),S.forEach(q=>d._OrtReleaseTensor(q)),T.forEach(q=>d._OrtReleaseTensor(q)),I.forEach(q=>d._free(q)),w!==0&&d._OrtReleaseRunOptions(w),y.forEach(q=>d._free(q))}},nd=e=>{let t=Re(),n=cr.get(e);if(!n)throw new Error("invalid session id");let o=n[0],u=t._OrtEndProfiling(o);u===0&&Ae("Can't get an profile file name."),t._OrtFree(u)},id=e=>{let t=[];for(let n of e){let o=n[2];!Array.isArray(o)&&"buffer"in o&&t.push(o.buffer)}return t}}),pr,xt,bn,li,di,yo,fu,wo,zr,Br,Ay,L2,U2,V2,G2,q2,H2,W2,K2=R(()=>{"use strict";ot(),j2(),dn(),Rl(),pr=()=>!!le.wasm.proxy&&typeof document<"u",bn=!1,li=!1,di=!1,wo=new Map,zr=(e,t)=>{let n=wo.get(e);n?n.push(t):wo.set(e,[t])},Br=()=>{if(bn||!li||di||!xt)throw new Error("worker not ready")},Ay=e=>{switch(e.data.type){case"init-wasm":bn=!1,e.data.err?(di=!0,fu[1](e.data.err)):(li=!0,fu[0]()),yo&&(URL.revokeObjectURL(yo),yo=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=wo.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},L2=async()=>{if(!li){if(bn)throw new Error("multiple calls to 'initWasm()' detected.");if(di)throw new Error("previous call to 'initWasm()' failed.");if(bn=!0,pr())return new Promise((e,t)=>{xt?.terminate(),zv().then(([n,o])=>{try{xt=o,xt.onerror=l=>t(l),xt.onmessage=Ay,fu=[e,t];let u={type:"init-wasm",in:le};!u.in.wasm.wasmPaths&&(n||al)&&(u.in.wasm.wasmPaths={wasm:new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href}),xt.postMessage(u),yo=n}catch(u){t(u)}},t)});try{await Nl(le.wasm),await Yl(le),li=!0}catch(e){throw di=!0,e}finally{bn=!1}}},U2=async e=>{if(pr())return Br(),new Promise((t,n)=>{zr("init-ep",[t,n]);let o={type:"init-ep",in:{epName:e,env:le}};xt.postMessage(o)});await Ql(le,e)},V2=async e=>pr()?(Br(),new Promise((t,n)=>{zr("copy-from",[t,n]);let o={type:"copy-from",in:{buffer:e}};xt.postMessage(o,[e.buffer])})):No(e),G2=async(e,t)=>{if(pr()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Br(),new Promise((n,o)=>{zr("create",[n,o]);let u={type:"create",in:{model:e,options:{...t}}},l=[];e instanceof Uint8Array&&l.push(e.buffer),xt.postMessage(u,l)})}else return ed(e,t)},q2=async e=>{if(pr())return Br(),new Promise((t,n)=>{zr("release",[t,n]);let o={type:"release",in:e};xt.postMessage(o)});td(e)},H2=async(e,t,n,o,u,l)=>{if(pr()){if(n.some(d=>d[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(u.some(d=>d))throw new Error("pre-allocated output tensor is not supported for proxy.");return Br(),new Promise((d,c)=>{zr("run",[d,c]);let s=n,r={type:"run",in:{sessionId:e,inputIndices:t,inputs:s,outputIndices:o,options:l}};xt.postMessage(r,id(s))})}else return rd(e,t,n,o,u,l)},W2=async e=>{if(pr())return Br(),new Promise((t,n)=>{zr("end-profiling",[t,n]);let o={type:"end-profiling",in:e};xt.postMessage(o)});nd(e)}}),mu,ky,X2,gk=R(()=>{"use strict";ot(),K2(),se(),Dl(),jv(),mu=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},ky=e=>{switch(e[3]){case"cpu":return new mt(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Bl(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:n,download:o,dispose:u}=e[2];return mt.fromGpuBuffer(n,{dataType:t,dims:e[1],download:o,dispose:u})}case"ml-tensor":{let t=e[0];if(!Ml(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:n,download:o,dispose:u}=e[2];return mt.fromMLTensor(n,{dataType:t,dims:e[1],download:o,dispose:u})}default:throw new Error(`invalid data location: ${e[3]}`)}},X2=class{async fetchModelAndCopyToWasmMemory(e){return V2(await Fl(e))}async loadModel(e,t){Gt();let n;typeof e=="string"?n=await this.fetchModelAndCopyToWasmMemory(e):n=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await G2(n,t),zt()}async dispose(){return q2(this.sessionId)}async run(e,t,n){Gt();let o=[],u=[];Object.entries(e).forEach(a=>{let p=a[0],h=a[1],m=this.inputNames.indexOf(p);if(m===-1)throw new Error(`invalid input '${p}'`);o.push(h),u.push(m)});let l=[],d=[];Object.entries(t).forEach(a=>{let p=a[0],h=a[1],m=this.outputNames.indexOf(p);if(m===-1)throw new Error(`invalid output '${p}'`);l.push(h),d.push(m)});let c=o.map((a,p)=>mu(a,()=>`input "${this.inputNames[u[p]]}"`)),s=l.map((a,p)=>a?mu(a,()=>`output "${this.outputNames[d[p]]}"`):null),r=await H2(this.sessionId,u,c,d,s,n),i={};for(let a=0;avl,initializeFlags:()=>_l,wasmBackend:()=>J2});var _l,vl,J2,bk=R(()=>{"use strict";ot(),K2(),gk(),_l=()=>{(typeof le.wasm.initTimeout!="number"||le.wasm.initTimeout<0)&&(le.wasm.initTimeout=0);let e=le.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),le.wasm.simd=!1),typeof le.wasm.proxy!="boolean"&&(le.wasm.proxy=!1),typeof le.wasm.trace!="boolean"&&(le.wasm.trace=!1),typeof le.wasm.numThreads!="number"||!Number.isInteger(le.wasm.numThreads)||le.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)le.wasm.numThreads=1;else{let t=typeof navigator>"u"?gu("node:os").cpus().length:navigator.hardwareConcurrency;le.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},vl=class{async init(e){_l(),await L2(),await U2(e)}async createInferenceSessionHandler(e,t){let n=new X2;return await n.loadModel(e,t),n}},J2=new vl});ot();ot();ot();var yk="1.29.0";{let e=(gA(),Tn(Av)).onnxjsBackend;en("webgl",e,-10)}{let e=(bk(),Tn(Z2)).wasmBackend;en("webgpu",e,5),en("webnn",e,5),en("cpu",e,10),en("wasm",e,10)}Object.defineProperty(le.versions,"web",{value:yk,enumerable:!0});function Y2(){return le.versions.web??""}function Q2(e={}){le.wasm.numThreads=e.wasmThreads??1,e.wasmPaths!==void 0&&(le.wasm.wasmPaths=e.wasmPaths)}var qo=[];function od(){let e=qo;return qo=[],e}async function Ho(e,t,n){for(let o of t){let u=null;try{if(u=await Bo.create(e.slice().buffer,{executionProviders:[o]}),await n(u))return{session:u,ep:o};qo.push(`${o}: the model does not have the inputs and outputs the release describes`)}catch(l){qo.push(`${o}: ${l instanceof Error?l.message:String(l)}`.slice(0,300))}await u?.release().catch(()=>{})}return null}function Cn(e,t){return new mt("float32",e,t)}function sd(e){return{cx:e.x+e.width/2,cy:e.y+e.height/2,w:e.width,h:e.height,angle:0}}function Ci(e,t){let n=Math.max(e.x,t.x),o=Math.max(e.y,t.y),u=Math.min(e.x+e.width,t.x+t.width),l=Math.min(e.y+e.height,t.y+t.height);if(u<=n||l<=o)return 0;let d=(u-n)*(l-o);return d/(e.width*e.height+t.width*t.height-d)}function e$(e,t,n,o){let u=[...e].sort((d,c)=>n(c)-n(d)),l=[];for(let d of u){let c=t(d);l.every(s=>Ci(c,t(s))I&&(I=P,T=A)}I>=l&&(c.push(w),s.push(I),r.push(T))}let i=c.length;if(i===0)return[];let a=new Float32Array(i),p=new Float32Array(i),h=new Float32Array(i),m=new Float32Array(i);for(let w=0;wy).sort((w,y)=>s[y]-s[w]||w-y),x=[];for(;b.length>0;){let w=b[0];if(x.push(w),b.length===1)break;let y=Ke(Ke(h[w]-a[w])*Ke(m[w]-p[w])),S=[];for(let T=1;T{let y={x:a[w]/n,y:p[w]/n,width:Ke(h[w]-a[w])/n,height:Ke(m[w]-p[w])/n};return{aabb:y,rect:sd(y),score:s[w],cls:r[w]}})}function ad(e,t,n,o=Dn){let u=Math.trunc(Math.max(0,e.x)),l=Math.trunc(Math.max(0,e.y)),d=Math.trunc(Math.min(t,e.x+e.width)),c=Math.trunc(Math.min(n,e.y+e.height));return d-u>=o&&c-l>=o}function r$(e,t,n,o,u,l=t$,d=Dn){return vk(e,t,n,l).filter(c=>ad(c.aabb,o,u,d))}function xk(e,t,n,o,u){let l=o/t,d=u/n;return[Math.max(0,e.x*l),Math.max(0,e.y*d),Math.min(o,(e.x+e.width)*l),Math.min(u,(e.y+e.height)*d)]}function n$(e,t){let n=e.aabb;if(n.x>=0&&n.y>=0&&n.x+n.width<=t.width&&n.y+n.height<=t.height)return e;let[o,u,l,d]=xk(n,t.width,t.height,t.width,t.height),c={x:o,y:u,width:Math.max(0,l-o),height:Math.max(0,d-u)};return{...e,aabb:c,rect:sd(c)}}function i$(e,t=Tr){let n=[],o=[];for(let u of e)(u.cls===t?n:o).push(u);return{faces:n,others:o}}function $k(e){return e<0&&(e=-e),e<1?1-e:0}function o$(e,t){let n=e/t,o=n<1?1:n,u=o,l=Math.ceil(u)*2+1,d=new Int32Array(t*2),c=new Int32Array(t*l),s=new Float64Array(l);for(let r=0;re&&(m=e),m-=h;for(let b=0;b>22;return t<0?0:t>255?255:t}var s$="",ud=null,ld=null;function Tk(e,t,n,o){let u=`${e}x${t}>${n}x${o}`;return(u!==s$||!ud||!ld)&&(ud=o$(e,n),ld=o$(t,o),s$=u),{h:ud,v:ld}}function a$(e,t,n,o,u){let{h:l,v:d}=Tk(t,n,o,u),c=o!==t,s=u!==n,r,i;if(c){let w=s?d.bounds[0]:0,S=(s?d.bounds[u*2-2]+d.bounds[u*2-1]:n)-w;i=w,r=new Uint8Array(o*S*3);let{ksize:T,bounds:I,kk:A}=l;for(let P=0;P.5?t+1:n<.5||t%2===0?t:t+1}function Sk(e,t,n=l$){let o=Math.min(n.height/t,n.width/e);return{ratio:o,width:u$(e*o),height:u$(t*o)}}function d$(e,t,n,o=l$){let u=Sk(t,n,o),l=a$(e,t,n,u.width,u.height),d=o.width*o.height,c=new Float32Array(3*d).fill(o.pad);for(let s=0;s{let i=(await s.run({[n.inputName]:Cn(new Float32Array(3*u*l),[1,3,l,u])}))[n.outputName]?.dims??[];return i.length===3&&i[0]===1&&i[1]===n.anchors&&i[2]===d});return c?new e(c.session,c.ep,n):null}async propose(t){let{letterbox:n,inputName:o,outputName:u,decode:l,minCropPx:d}=this.settings,c=performance.now(),{tensor:s,ratio:r}=d$(t.data,t.width,t.height,n),i=performance.now(),a=await this.session.run({[o]:Cn(s,[1,3,n.height,n.width])});return this.lastTimings={preprocessMs:i-c,inferMs:performance.now()-i},r$(a[u].data,this.columns,r,t.width,t.height,l,d)}async release(){await this.session.release().catch(()=>{})}};var Ik=32;async function Ok(e,t,n,o,u=Ik){if(n<=u)return e(t,n);let l=t.length/n,d=new Float32Array(n*o);for(let c=0;c(await d.run({[n.inputName]:Cn(new Float32Array(3*u*u),[1,3,u,u])}))[n.outputName]?.data.length===n.dim);return l?new e(l.session,l.ep,n):null}async embedBatch(t,n){return Ok((o,u)=>this.run(o,u),t,n,this.settings.dim)}async run(t,n){let{inputName:o,outputName:u,inputSize:l,dim:d}=this.settings,s=(await this.session.run({[o]:Cn(t,[n,3,l,l])}))[u].data;if(s.length!==n*d)throw new Error(`unexpected embedding size ${s.length} for ${n} crops`);return s}async release(){await this.session.release().catch(()=>{})}};var dd=["orb","akaze","sift"],c$=["none","median"],p$=["bf","flann"],h$=["homography","affine","similarity"],f$=["ransac","usac"],b$={orb:[["scaleFactor",1.2,!1],["nLevels",8,!0],["edgeThreshold",15,!0],["patchSize",15,!0],["fastThreshold",10,!0]],akaze:[["threshold",.001,!1],["nOctaves",4,!0],["nOctaveLayers",4,!0]],sift:[["nOctaveLayers",3,!0],["contrastThreshold",.04,!1],["edgeThreshold",10,!1],["sigma",1.6,!1]]},c4=Object.fromEntries(dd.map(e=>[e,Object.fromEntries(b$[e].map(([t,n])=>[t,n]))])),m$=Object.freeze({seed:0,features:{type:"orb",maxFeatures:300,artLongSide:320,mask:[],params:{}},crop:{longSide:320,maxFeatures:500},candidates:{embeddingTopK:5,voteTopN:0},vote:{matcher:"bf",ratio:.8,lshTables:12,lshKeySize:20,lshProbes:2,kdTrees:4,checks:64},match:{ratio:.8},ransac:{model:"homography",method:"ransac",threshold:6,maxIters:2e3,confidence:.995,minAreaRatio:.25,maxAreaRatio:4},fusion:{weight:.1,minInliers:8,fullInliers:30,penalty:0,baseline:"none",reorder:!0},gate:{belowAccept:!1,sources:null}}),y$=["features","crop","candidates","vote","match","ransac","fusion","gate"],g$=["index","seed",...y$],Ek=/^[0-9a-f]{64}$/,Di=class extends Error{errors;constructor(t){super(t.join("; ")),this.name="GeometricConfigError",this.errors=[...t]}},cd=class{constructor(t,n=null,o=null){this.indexPath=n;this.indexSha256=o;this.settings=w$(structuredClone(t))}indexPath;indexSha256;settings;get seed(){return this.settings.seed}get features(){return this.settings.features}toJson(){return structuredClone(this.settings)}};function w$(e){if(typeof e=="object"&&e!==null&&!Object.isFrozen(e)){Object.freeze(e);for(let t of Object.values(e))w$(t)}return e}function Zo(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Rn(e){return typeof e=="number"&&Number.isInteger(e)}function Sr(e){return typeof e=="number"}function Ak(e){let t=e.includes("'")&&!e.includes('"')?'"':"'",n="";for(let o of e){let u=o.codePointAt(0);o==="\\"?n+="\\\\":o===t?n+=`\\${t}`:o===` -`?n+="\\n":o==="\r"?n+="\\r":o===" "?n+="\\t":u<32||u===127?n+=`\\x${u.toString(16).padStart(2,"0")}`:n+=o}return t+n+t}function yt(e){return typeof e=="string"?Ak(e):e==null?"None":typeof e=="boolean"?e?"True":"False":Array.isArray(e)?`[${e.map(yt).join(", ")}]`:typeof e=="number"?(Number.isInteger(e),String(e)):JSON.stringify(e)}function Ri(e){return[...e].sort((t,n)=>tn?1:0)}function Nn(e){return Object.keys(e)}function zn(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function kk(e,t){let n=structuredClone(m$);zn(e,"seed")&&(n.seed=e.seed);for(let o of y$){let u=zn(e,o)?e[o]:{};if(!Zo(u)){t.push(`${o} must be an object`);continue}let l=Nn(m$[o]),d=Ri(Nn(u).filter(s=>!l.includes(s)));d.length>0&&t.push(`${o} has unknown key(s) ${yt(d)} (expected ${yt(Ri(l))})`);let c=n[o];for(let s of Nn(u))l.includes(s)&&(c[s]=structuredClone(u[s]))}return n}function Pk(e,t){let n=e.type;if(typeof n!="string"||!dd.includes(n)){t.push(`features.type must be one of ${yt([...dd])}, got ${yt(n)}`);return}for(let i of["maxFeatures","artLongSide"]){let a=e[i];(!Rn(a)||a<1)&&t.push(`features.${i} must be a whole number of at least 1`)}let o=e.mask,u=i=>Array.isArray(i)&&i.length===4&&i.every(a=>Sr(a)&&0<=a&&a<=1)&&i[0]i),s=Ri(Nn(l).filter(i=>!c.includes(i)));s.length>0&&t.push(`features.params has unknown key(s) ${yt(s)} for ${n} (expected ${yt(Ri(c))})`);let r={};for(let[i,a]of d)r[i]=zn(l,i)?l[i]:a;for(let[i,,a]of d){let p=r[i];(a&&!Rn(p)||!Sr(p)||p<=0)&&t.push(`features.params.${i} must be a ${a?"whole ":""}number above 0`)}e.params=r}function Ck(e,t){let n=e.seed;(!Rn(n)||n<0)&&t.push("seed must be a whole number of 0 or more"),Pk(e.features,t);let o=e.crop,u=e.candidates,l=e.vote,d=e.match,c=e.ransac,s=e.fusion,r=[["crop",o,"longSide",1],["crop",o,"maxFeatures",1],["candidates",u,"embeddingTopK",0],["candidates",u,"voteTopN",0],["ransac",c,"maxIters",1],["vote",l,"lshTables",1],["vote",l,"lshKeySize",1],["vote",l,"lshProbes",0],["vote",l,"kdTrees",1],["vote",l,"checks",1],["fusion",s,"minInliers",0],["fusion",s,"fullInliers",1]];for(let[m,b,x,w]of r){let y=b[x];(!Rn(y)||ya)&&t.push("ransac.minAreaRatio and maxAreaRatio must be numbers of 0 or more, the first at most the second");for(let m of["weight","penalty"]){let b=s[m];(!Sr(b)||b<0)&&t.push(`fusion.${m} must be a number of 0 or more`)}let p=e.gate;typeof p.belowAccept!="boolean"&&t.push("gate.belowAccept must be true or false");let h=p.sources;h!==null&&(!Array.isArray(h)||h.length===0||!h.every(m=>typeof m=="string"&&m.length>0))&&t.push('gate.sources must be null or a non-empty list of source types or patterns, e.g. ["physical/*"]'),c$.includes(s.baseline)||t.push(`fusion.baseline must be one of ${yt([...c$])}`),typeof s.reorder!="boolean"&&t.push("fusion.reorder must be true or false")}function Dk(e,t=null,n="geometric settings"){if(!Zo(e))throw new Di([`${n}: must be a JSON object`]);let o=[],u=Ri(Nn(e).filter(r=>!g$.includes(r)));u.length>0&&o.push(`unknown key(s) ${yt(u)} (expected ${yt(g$)})`);let l=zn(e,"index")?e.index:{},d=null,c=null;if(!Zo(l)||Nn(l).some(r=>r!=="path"&&r!=="sha256"))o.push('index must be an object with the keys "path" and "sha256"');else{let r=zn(l,"path")?l.path:null;r!=null&&(typeof r!="string"||r.length===0?o.push("index.path must be a file path"):d=t===null?r:new URL(r,t).href);let i=zn(l,"sha256")?l.sha256:null;i!=null&&(typeof i!="string"||!Ek.test(i)?o.push("index.sha256 must be 64 lowercase hex digits"):c=i)}let s=kk(e,o);if(Ck(s,o),o.length>0)throw new Di(o.map(r=>`${n}: ${r}`));return new cd(Object.freeze(s),d,c)}function _$(e,t=null,n="geometric settings"){let o;try{o=JSON.parse(e)}catch(u){throw new Di([`${n}: not valid JSON: ${u instanceof Error?u.message:String(u)}`])}return Dk(o,t,n)}function v$(e,t,n){let o=n/Math.max(t,e);return o===1?"none":o<1?"down":"up"}var Rk=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]);function pd(e){let t=e.length,n=Math.ceil((t+9)/64),o=new Uint8Array(n*64);o.set(e),o[t]=128;let u=new DataView(o.buffer),l=t*8;u.setUint32(o.length-8,Math.floor(l/2**32)),u.setUint32(o.length-4,l>>>0);let d=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),c=new Uint32Array(64);for(let s=0;s>>7|y<<25)^(y>>>18|y<<14)^y>>>3,I=(S>>>17|S<<15)^(S>>>19|S<<13)^S>>>10;c[w]=c[w-16]+T+c[w-7]+I>>>0}let[r,i,a,p,h,m,b,x]=[d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7]];for(let w=0;w<64;w++){let y=(h>>>6|h<<26)^(h>>>11|h<<21)^(h>>>25|h<<7),S=h&m^~h&b,T=x+y+S+Rk[w]+c[w]>>>0,I=(r>>>2|r<<30)^(r>>>13|r<<19)^(r>>>22|r<<10),A=r&i^r&a^i&a,P=I+A>>>0;x=b,b=m,m=h,h=p+T>>>0,p=a,a=i,i=r,r=T+P>>>0}d[0]=d[0]+r>>>0,d[1]=d[1]+i>>>0,d[2]=d[2]+a>>>0,d[3]=d[3]+p>>>0,d[4]=d[4]+h>>>0,d[5]=d[5]+m>>>0,d[6]=d[6]+b>>>0,d[7]=d[7]+x>>>0}return Array.from(d,s=>s.toString(16).padStart(8,"0")).join("")}function x$(e,t,n,o=32){let u=new Uint8Array(8+o),l=new DataView(u.buffer),d=[];for(let c=0;cu);return n.sort((o,u)=>e[u]-e[o]),n.slice(0,t)}function T$(e,t,n,o,u,l,d){let c=hd(e,t,n,o,u);try{return fd(e,c,l,d)}finally{c.delete()}}function fd(e,t,n,o){let u=[t.cols,t.rows],l=zk(e,n,o),d=new e.KeyPointVector,c=new e.Mat,s=new e.Mat;try{l.detectAndCompute(t,s,d,c,!1);let r=d.size();if(r===0||c.empty()||c.rows===0)return{n:0,xy:new Float32Array(0),desc:new Uint8Array(0),size:u};let i=c.cols,a=new Float64Array(r),p=new Float32Array(r*2);for(let w=0;w{m[2*y]=p[2*w],m[2*y+1]=p[2*w+1],b.set(x.subarray(w*i,(w+1)*i),y*i)}),{n:h.length,xy:m,desc:b,size:u}}finally{l.delete(),d.delete(),c.delete(),s.delete()}}var S$="embedding",I$="vote",O$="both";function E$(e,t,n){return t>=n.minInliers?e+n.weight*Math.min(t,n.fullInliers)/n.fullInliers:e-n.penalty}function Mk(e){let t=[...e].sort((u,l)=>u-l),n=t.length;if(n===0)return Number.NaN;let o=n>>1;return n%2===1?t[o]:(t[o-1]+t[o])/2}function A$(e,t){return t.baseline==="median"&&e.length>0?Mk(e):0}function k$(e,t){return Math.max(0,e-t)}function P$(e,t,n){let o=new Float64Array(e);for(let u of t){let l=u.fused-u.cosine;for(let d of n(u.cardId))o[d]=o[d]+l}return o}var Fk=10n;function jk(e){let t=new DataView(new ArrayBuffer(8));t.setFloat64(0,e);let n=t.getUint32(0),o=t.getUint32(4),u=n>>>31?-1n:1n,l=n>>>20&2047,d=BigInt(n&1048575)<<32n|BigInt(o),c;return l===0?c=-1074:(d|=1n<<52n,c=l-1075),{mantissa:u*d,exponent:c}}function Ni(e,t){if(!Number.isFinite(e)||e===0)return e;let{mantissa:n,exponent:o}=jk(e),u=n<0n,d=(u?-n:n)*Fk**BigInt(t),c=1n;o>=0?d<<=BigInt(o):c<<=BigInt(-o);let s=d/c,i=2n*(d-s*c);if((i>c||i===c&&s%2n===1n)&&(s+=1n),s===0n)return u?-0:0;let a=`${u?"-":""}${s.toString()}e-${t}`;return Number(a)}function Lk(e){return{cardId:e.cardId,cosine:Ni(e.cosine,6),foundBy:e.foundBy,votes:e.votes,matches:e.matches,inliers:e.inliers,evidence:Ni(e.evidence,6),fused:Ni(e.fused,6)}}function C$(e,t){return{before:{topId:e.topId,similarity:Ni(e.similarity,6),margin:Ni(e.margin,6),accepted:e.accepted},candidates:t.candidates.map(Lk),...t.debug?{debug:t.debug}:{}}}function R$(e,t,n,o,u,l,d){let c=new Map;for(let s=n;s=d*e.dSecond[s])continue}c.set(i,(c.get(i)??0)+1)}return c}function N$(e,t,n,o,u,l,d=32){let c=new e.Mat(n,d,e.CV_8UC1),s=new e.Mat(u,d,e.CV_8UC1),r=new e.BFMatcher(e.NORM_HAMMING,!1),i=new e.DMatchVectorVector;try{c.data.set(t.subarray(0,n*d)),s.data.set(o.subarray(0,u*d)),r.knnMatch(c,s,i,2);let a=[],p=i.size();for(let h=0;h0&&!Gk(e,a,o,u,l))return 0;let b=0,x=i.data;for(let w=0;wtypeof performance<"u"?performance.now():Date.now();function M$(e,t){let n=e.settings;return n.features.type!=="orb"?`features.type ${JSON.stringify(n.features.type)} is not ported (only "orb")`:n.candidates.voteTopN>0&&n.vote.matcher!=="bf"?`vote.matcher ${JSON.stringify(n.vote.matcher)} is not ported (only "bf")`:z$(t,n.ransac)}var md=class{constructor(t,n,o,u,l=new Map){this.config=t;this.index=n;this.voter=o;this.cv=u;let d=M$(t,u);if(d)throw new Error(d);n.cards.forEach((c,s)=>this.position.set(c.cardId,s)),this.identity=n.cards.map(c=>l.get(c.cardId)??c.cardId),this.image=n.cards.map(c=>c.artV??c.cardId)}config;index;voter;cv;position=new Map;identity;image;debug=!1;get reorder(){return this.config.settings.fusion.reorder}applies(t,n){let o=this.config.settings.gate;return o.belowAccept&&n?!1:o.sources===null?!0:t!==null&&o.sources.some(u=>qk(t,u))}dispose(){this.voter.dispose()}cropFeatures(t){let{crop:n,features:o}=this.config.settings;return T$(this.cv,t.rgba,t.width,t.height,n.longSide,n.maxFeatures,o.params)}inliers(t,n){let o=this.index.cards[n];if(t.n<2||o.n<2)return[0,0];let u=this.index.desc.subarray(o.start*Bn,(o.start+o.n)*Bn),l=N$(this.cv,t.desc,t.n,u,o.n,this.config.settings.match.ratio,Bn);if(l.length===0)return[0,0];let d=new Float32Array(l.length*2),c=new Float32Array(l.length*2),s=this.index.xy;return l.forEach((r,i)=>{d[2*i]=s[2*(o.start+r.trainIdx)],d[2*i+1]=s[2*(o.start+r.trainIdx)+1],c[2*i]=t.xy[2*r.queryIdx],c[2*i+1]=t.xy[2*r.queryIdx+1]}),[l.length,B$(this.cv,d,c,t.size,[o.width,o.height],this.config.settings.ransac)]}async verify(t,n){return(await this.verifyBatch([t],n))[0]}async verifyBatch(t,n){let o=this.config.settings,u=t.map(()=>0),l=t.map(()=>null),d=t.map((s,r)=>{this.cv.setRNGSeed?.(o.seed);let i=Jo(),a=this.debug?this.debugFeatures(s,p=>l[r]=p):this.cropFeatures(s);return u[r]=u[r]+Jo()-i,a}),c=t.map(()=>new Map);if(o.candidates.voteTopN>0&&this.index.rows>0){let s=d.reduce((r,i)=>r+i.n,0);if(s>0){let r=new Uint8Array(s*Bn),i=[],a=0;for(let h of d)i.push(a),r.set(h.desc.subarray(0,h.n*Bn),a*Bn),a+=h.n;let p=await this.voter.knn2(r);d.forEach((h,m)=>{h.n<1||(c[m]=R$(p,h.n,i[m],this.index.rowCard,this.identity,this.image,o.vote.ratio))})}}return t.map((s,r)=>{let i=Jo(),a=this.finish(s,d[r],c[r],n);u[r]=u[r]+Jo()-i;let p=Math.round(u[r]*1e3)/1e3;return this.debug?{...a,ms:p,debug:this.debugRecord(s,l[r],d[r],c[r],p)}:{...a,ms:p}})}debugFeatures(t,n){let{crop:o,features:u}=this.config.settings,l=hd(this.cv,t.rgba,t.width,t.height,o.longSide);try{return n({sha256:pd(l.data.subarray(0,l.rows*l.cols)),width:l.cols,height:l.rows}),fd(this.cv,l,o.maxFeatures,u.params)}finally{l.delete()}}debugRecord(t,n,o,u,l){let d={},c=[...u.keys()].map(r=>this.index.cards[r].cardId).sort();for(let r of u.keys())d[this.index.cards[r].cardId]=u.get(r);let s={};for(let r of c)s[r]=d[r];return{grey:n,resize:v$(t.width,t.height,this.config.settings.crop.longSide),source:[t.width,t.height],keypoints:o.n,rows:x$(o.n,o.xy,o.desc),votes:s,ms:l}}finish(t,n,o,u){let l=this.config.settings,d=new Map;for(let{cardId:S,cosine:T}of t.ranked)d.has(S)||d.set(S,T);let c=[...d.keys()];c.sort((S,T)=>d.get(T)-d.get(S));let s=0,r=!0;for(let S of d.values())(r||Su===void 0||u.has(S)).sort((S,T)=>a.get(T)-a.get(S)||this.position.get(S)-this.position.get(T)).slice(0,l.candidates.voteTopN),h=[...new Set([...i,...p])],m=new Map;for(let S of h){let T=this.position.get(S);T!==void 0&&m.set(S,this.inliers(n,T))}let b=A$([...m.values()].map(([,S])=>S),l.fusion),x=new Set(i),w=new Set(p);return{candidates:h.map(S=>{let T=x.has(S)&&w.has(S)?O$:x.has(S)?S$:I$,I=d.get(S)??s,A=m.get(S);if(A===void 0)return{cardId:S,cosine:I,foundBy:T,votes:0,matches:0,inliers:0,evidence:0,fused:I,indexed:!1};let[P,O]=A,B=k$(O,b);return{cardId:S,cosine:I,foundBy:T,votes:a.get(S)??0,matches:P,inliers:O,evidence:B,fused:E$(I,B,l.fusion),indexed:!0}}),cropKeypoints:n.n,ms:0}}};function qk(e,t){let n="";for(let o=0;o=t.length)n+="\\[";else{let d=t.slice(o+1,l).replace(/\\/g,"\\\\");d.startsWith("!")?d="^"+d.slice(1):d.startsWith("^")&&(d="\\"+d),n+=`[${d}]`,o=l}}else n+=u.replace(/[.*+?^${}()|[\]\\/]/g,"\\$&")}return new RegExp(`^(?:${n})$`).test(e)}function F$(e,t,n){let o=new Map;for(let u of e)o.set(u.id,t==="printing"?u.printingOf??u.id:u.name);if(n)for(let u of n.groups){let l=u.find(d=>!n.representative.has(d))??[...u].sort()[0];for(let d of u)o.set(d,`group:${n.representative.get(d)??l}`)}return o}var Yo=class{constructor(t=1500,n=2){this.limitMs=t;this.frames=n}limitMs;frames;run=0;observe(t){return this.run=t>this.limitMs?this.run+1:0,this.run>=this.frames}reset(){this.run=0}};function gd(e,t){if(e===t)return!0;if(Array.isArray(e)||Array.isArray(t))return Array.isArray(e)&&Array.isArray(t)&&e.length===t.length&&e.every((u,l)=>gd(u,t[l]));if(typeof e!="object"||typeof t!="object"||e===null||t===null)return!1;let n=Object.keys(e),o=Object.keys(t);return n.length===o.length&&n.every(u=>Object.hasOwn(t,u)&&gd(e[u],t[u]))}function Hk(e,t){return[...new Set([...Object.keys(e),...Object.keys(t)])].filter(o=>!gd(e[o],t[o])).sort()}function Wk(e,t){let n=/Version control:\s*(\S+)/.exec(t.getBuildInformation?.()??"")?.[1]??"";return e===n?null:`the descriptor index was built with OpenCV ${e||"of an unrecorded version"} and OpenCV ${n||"of an unknown version"} is running; keypoints may differ slightly from the index's`}async function j$(e,t){let n=_$(await t.fetchText(e),e,e.split("/").pop()||"geometric settings");if(n.indexPath===null||n.indexSha256===null)throw new Error("index.path and index.sha256 are both needed");let o=await t.loadIndex(n.indexPath,{bankSha256:t.bankSha256,bankIds:t.bankIds});if(o.sourceSha256!==n.indexSha256)throw new Error(`the packed index repacks ${o.sourceSha256}, not the ${n.indexSha256} the settings pin`);let u=Hk(n.settings.features,o.features);if(u.length>0)throw new Error(`the index was built with other features settings (differing: ${u.join(", ")}); build it again with cardvision build-features`);let l=await t.loadCv(),d=M$(n,l);if(d)throw new Error(d);let c=await t.makeVoter(o.desc);try{return{stage:new md(n,o,c,l,t.identities),bytes:o.desc.byteLength+o.xy.byteLength,note:Wk(o.opencv,l)}}catch(s){throw c.dispose(),s}}var Mn="file:///";function L$(e){let t=new URL(e,Mn).href.slice(Mn.length);try{return decodeURIComponent(t)}catch{return t}}var U$="busy";var er={full128:{id:"full128",size:128,centerMask:!1,poolVersion:2,crop:"truncate"}};function bd(e){return typeof e=="string"&&Object.prototype.hasOwnProperty.call(er,e)}function Qo(e){if(typeof e!="object"||e===null||Array.isArray(e))return null;let t=e;if(!bd(t.id))return null;let n=er[t.id],o=Object.keys(t);return o.length===Object.keys(n).length&&o.every(l=>n[l]===t[l])?t.id:null}function Kk(e,t,n,o,u,l){let d=new Float64Array(o*u*3),c=new Uint32Array(o*u);for(let a=0;a0&&(d[a*3]=d[a*3]/p,d[a*3+1]=d[a*3+1]/p,d[a*3+2]=d[a*3+2]/p)}if(l===1||t>=o&&n>=u||t<=0||n<=0)return d;let s=V$(n,u),r=V$(t,o),i=new Float64Array(d.length);for(let a=0;a=0&&!n[l];)l--;let d=u+1;for(;d=0&&(d>=t||u-l<=d-u)?l:d}return o}function G$(e,t,n,o){let u=o.size,l=Kk(e,t,n,u,u,o.poolVersion),d=new Float32Array(3*u*u),c=u*u;for(let s=0;s>10&31,s=l&1023;c===0?o[u]=d*s*2**-24:c===31?o[u]=s?NaN:d*(1/0):o[u]=d*(1+s/1024)*2**(c-15)}return o}function Zk(e,t){let n;try{n=atob(e)}catch{return null}if(n.length!==t*2)return null;let o=new Uint8Array(n.length);for(let u=0;u0)||!Number.isFinite(u))return!1;for(let l=0;l=Fn.length&&Fn.every((t,n)=>e[n]===t)}function wd(e){return yd(e)&&e.length>Fn.length?e[Fn.length]:null}function Z$(e){let t=e instanceof Uint8Array?e:new Uint8Array(e);if(wd(t)!==es||t.lengthtypeof a=="string")||u.entryCount!==d.length)return null;let c=u.artVs;if(c!==void 0&&(!Array.isArray(c)||c.length!==d.length||!c.every(a=>a===null||typeof a=="string")))return null;let s=l.dim,r=zi+n;if(t.length-r!==d.length*s*2)return null;let i=new Float32Array(d.length*s);for(let a=0;a1?1:e}function ts(e,t){let{frameWidth:n,frameHeight:o,names:u}=t;return e.map(l=>{let{aabb:d,rect:c}=l,s={instanceId:l.instanceId,bounds:{x:jn(d.x/n),y:jn(d.y/o),width:jn(d.width/n),height:jn(d.height/o)},rotation:(c.angle+45)%90-45,localWidth:jn(c.w/n),localHeight:jn(c.h/o),landscape:c.w>c.h,cardId:null,name:null};if(l.identity){let r=u.get(l.identity);r!==void 0&&(s.cardId=l.identity,s.name=r)}return s})}var Yk=["accepted-nms","best-score","detector-score"];function Y$(e){return typeof e=="string"&&Yk.includes(e)}var _d="accepted-nms",Qk=.5,eP="hover_";function J$(e){return e.width*e.height}function tP(e,t){let n=t==="accepted-nms"?e.kept:e.proposals.map((u,l)=>l),o=(u,l)=>{let d=e.proposals[u],c=e.proposals[l];if(t==="detector-score")return d.detectorScore!==c.detectorScore?d.detectorScore{let u=e.proposals[n];return{proposal:n,aabb:u.aabb,rect:u.rect,identity:u.accepted?u.displayId:null,score:u.score,zIndex:o+1}})}var rs=class{prev=[];nextId=1;reset(){this.prev=[]}assign(t){let n=new Set,o=t.map((l,d)=>d).sort((l,d)=>t[d].zIndex-t[l].zIndex),u=new Array(t.length);for(let l of o){let d=t[l],c=-1,s=Qk;for(let r=0;r=s&&(c<0||i>s)&&(c=r,s=i)}c>=0?(n.add(c),u[l]=this.prev[c].id):u[l]=`${eP}${String(this.nextId++).padStart(4,"0")}`}return this.prev=t.map((l,d)=>({id:u[d],aabb:l.aabb,identity:l.identity})),u}};function xd(e,t,n,o=u=>u){return ts(e.map((l,d)=>({instanceId:t[d],aabb:l.aabb,rect:l.rect,identity:l.identity})),n).map((l,d)=>({...l,zIndex:e[d].zIndex,boxIndex:o(e[d].proposal)}))}var rP="identity-groups";function eT(e){if(typeof e!="object"||e===null)return null;let t=e;if(t.schemaVersion!==1||t.kind!==rP||typeof t.version!="number"||!Number.isInteger(t.version)||!Array.isArray(t.groups)||typeof t.representative!="object"||t.representative===null||Array.isArray(t.representative))return null;let n=new Map,o=[];for(let l of t.groups){if(!Array.isArray(l)||l.length<2)return null;let d=[];for(let c of l){if(typeof c!="string"||c.length===0||n.has(c))return null;n.set(c,o.length),d.push(c)}o.push(d)}let u=new Map;for(let[l,d]of Object.entries(t.representative)){if(typeof d!="string"||l===d)return null;let c=n.get(l);if(c===void 0||n.get(d)!==c)return null;u.set(l,d)}for(let l of o){let d=l.filter(c=>!u.has(c));if(d.length!==1||l.some(c=>u.has(c)&&u.get(c)!==d[0]))return null}return{version:t.version,groups:o,representative:u}}var nP=["name","printing"];function $d(e){return nP.includes(e)}function tT(e,t){return t==="printing"?e.printingOf??e.id:e.name}function rT(e,t){let n=new Map;for(let o of[...new Set(e)].sort(Q$)){let u=t.get(o);if(u===void 0)continue;let l=n.get(u);l?l.push(o):n.set(u,[o])}return[...n.values()].filter(o=>o.length>=2).sort((o,u)=>Q$(o[0],u[0]))}function Q$(e,t){return et?1:0}var Ln=class{rep=new Map;display;constructor(t=null,n,o="representative"){if(this.display=o,!!t)for(let[u,l]of t.representative)o==="representative"&&n&&!n(l)||this.rep.set(u,l)}of(t){return this.rep.get(t)??t}shown(t){return this.display==="best-printing"?t:this.of(t)}get collapsedCount(){return this.rep.size}};var Td=["webgpu","wasm"];var iP=.3;function oP(e,t){let n=Math.max(0,Math.floor(t.x)),o=Math.max(0,Math.floor(t.y)),u=Math.min(e.width,Math.floor(t.x+t.width)),l=Math.min(e.height,Math.floor(t.y+t.height)),d=Math.max(0,u-n),c=Math.max(0,l-o),s=new Uint8ClampedArray(d*c*4);for(let r=0;r=t.tau&&e.margin>=t.delta}function sP(e,t,n){if(Un(e,n)||!n.rotationRetry)return{orientation:0,match:e,retried:!1};let o=[e,...t()],u=-1;for(let l=0;lo[u].similarity)&&(u=l);if(u<0){u=0;for(let l=1;lo[u].similarity&&(u=l)}return{orientation:u,match:o[u],retried:!0}}function nT(e){let t=[],n=[];return e.forEach((o,u)=>{o.tauPassed&&(o.orientation===0?t:n).push(u)}),[...t,...n]}function iT(e,t){let n=(t??e.map((o,u)=>u)).filter(o=>e[o].tauPassed);return e$(n,o=>e[o].aabb,o=>e[o].score,iP)}async function oT(e,t,n,o,u=Dn,l=null){let d=n.preprocessing,c=n.rule,s=d.size,r=n.dim,i=[],a=[],p=[];for(let _ of o){let C=oP(e,_.aabb),{rgba:F,width:V,height:G}=C;Vn.matchScored(b,C*r)),w=[];if(c.rotationRetry)for(let _=0;_[_,C])),S=null;if(w.length>0){let _=new Float32Array(w.length*3*h);for(let C=0;C{let F=y.get(_);if(F===void 0||S===null)throw new Error(`the turned looks of proposal ${_} were not embedded`);return(F*3+C-1)*r},I=a.map((_,C)=>sP(x[C],()=>[1,2,3].map(F=>n.matchScored(S,T(C,F))),c)),A=_=>I[_].orientation===0?{embs:b,off:_*r}:{embs:S,off:T(_,I[_].orientation)},P=new Map,O,B,U=!1;if(l)try{let _=performance.now(),C=[],F=[],V=[];for(let G=0;G0){let G=await l.verifyBatch(V,n.bankIds);C.forEach((H,M)=>{let J=G[M],Z=F[M],W=I[H].match,pe=P$(Z,J.candidates,Oe=>n.columnsOf(Oe)),fe=l.reorder?n.lookOf(pe):n.lookWithin(pe,n.lookOf(Z).row),{row:ye,...Fe}=fe;P.set(H,{match:Fe,geometric:C$({topId:W.topId,similarity:W.similarity,margin:W.margin,accepted:Un(W,c)},J)})})}O=performance.now()-_}catch(_){U=!0,B=_,P.clear(),O=void 0}let X=_=>{let C=[];for(let F=0;Fo.shown(l));let u=new Map;this.rowIdent=new Int32Array(t.ids.length);for(let l=0;lc&&(c=p,s=a)}let r=ns,i=s>=0?this.rowIdent[s]:-1;for(let a=0;ar&&(r=d[a]);return{bestRow:s,best:c,other:r}}matchScored(t,n){let{bestRow:o,best:u,other:l}=this.topAndOther(t,n),d=this.bank.ids[o];return{cardId:u>=this.rule.tau?d:null,similarity:u,topId:d,identity:this.identRep[this.rowIdent[o]],displayId:this.rowShown[o],margin:u-l}}scoreRow(t,n){let{matrix:o,dim:u,ids:l}=this.bank,d=new Float64Array(l.length);for(let c=0;cd);n.sort((l,d)=>t[d]-t[l]);let o=new Set,u=[];for(let l of n){let d=this.bank.ids[l];o.has(d)||(o.add(d),u.push({cardId:d,cosine:t[l]}))}return u}columns=null;columnsOf(t){return this.columns===null&&(this.columns=new Map,this.bank.ids.forEach((n,o)=>{let u=this.columns.get(n);u?u.push(o):this.columns.set(n,[o])})),this.columns.get(t)??[]}bankIdSet=null;get bankIds(){return this.columnsOf(""),this.bankIdSet??=new Set(this.columns.keys()),this.bankIdSet}scored(t,n,o){let u=this.bank.ids[t];return{cardId:n>=this.rule.tau?u:null,similarity:n,topId:u,identity:this.identRep[this.rowIdent[t]],displayId:this.rowShown[t],margin:n-o,row:t}}lookOf(t){let n=0;for(let d=1;dt[n]&&(n=d);let o=this.rowIdent[n],u=ns,l=!1;for(let d=0;du)&&(u=t[d]),l=!0);return this.scored(n,t[n],u)}lookWithin(t,n){let o=this.rowIdent[n],u=-1,l=ns,d=!1;for(let c=0;ct[u])&&(u=c):((!d||t[c]>l)&&(l=t[c]),d=!0);return this.scored(u,t[u],l)}matchPooled(t,n=this.rule.delta){let{bestRow:o,best:u,other:l}=this.topAndOther(t,0),d=u-l;return{cardId:u>=this.rule.tau&&d>=n?this.rowShown[o]:null,similarity:u,margin:d}}};var os=class{constructor(t=3){this.k=t;if(!Number.isInteger(t)||t<1)throw new RangeError(`k must be a positive integer, got ${t}`)}k;run=0;misses=0;showing=!1;reset(){this.run=0,this.misses=0,this.showing=!1}observe(t){return t?(this.run+=1,this.misses=0,this.run>=this.k&&(this.showing=!0)):(this.run=0,this.misses+=1,this.misses>=this.k&&(this.showing=!1)),{confirmed:this.showing,consecutive:this.run}}};var aP=.4,uP=2,lP=5,dP=2,cP="track_";function pP(e){let t=e[0].length,n=new Float32Array(t);for(let l of e)for(let d=0;d0))return null;for(let l=0;lu));for(let o of this.tracks){let u=-1,l=aP;for(let d of n){let c=Ci(o.detection.aabb,t[d].aabb);c>l&&(l=c,u=d)}if(u>=0){let d=t[u];o.detection=d,o.misses=0,o.age+=1,o.embeddings.push(d.embedding),o.embeddings.length>lP&&o.embeddings.shift(),n.delete(u)}else o.misses+=1}this.tracks=this.tracks.filter(o=>o.misses<=uP);for(let o of n){let u=t[o];this.tracks.push({instanceId:`${cP}${String(this.nextId++).padStart(4,"0")}`,detection:u,embeddings:[u.embedding],misses:0,age:1})}return this.tracks.filter(o=>o.misses===0).map(o=>({instanceId:o.instanceId,detection:o.detection,identity:o.age>=dP?this.identityOf(o):null}))}};var fP=.25,mP=5;function gP(e,t){if(e===null||t===void 0)return!1;let n=t-e;return n<-fP||n>mP}function bP(e){return[e.x,e.y,e.width,e.height]}var as=class{hoverSelection;detector;embedder;recognizer;names;classNames;faceClass;minCropPx;now;tracker;gate=new os(3);hoverIds=new rs;heldCards=[];lastVideoTimeSec=null;geometric;geometricFailure=null;constructor(t){this.detector=t.detector,this.embedder=t.embedder,this.names=t.names,this.classNames=t.classNames??["face","back"],this.faceClass=t.faceClass??Tr,this.minCropPx=t.minCropPx??Dn,this.hoverSelection=t.hoverSelection??_d,this.now=t.now??(()=>performance.now()),this.geometric=t.geometric??null;let n=new Ln(t.identityGroups??null,o=>this.names.has(o),t.identityDisplay??"best-printing");this.recognizer=new is(t.bank,t.accept??Sd,n),this.tracker=new ss(o=>this.recognizer.matchPooled(o).cardId)}get size(){return this.recognizer.size}takeGeometricFailure(){let t=this.geometricFailure;return this.geometricFailure=null,t}get geometricStage(){return this.geometric}setGeometric(t){this.geometric=t,this.geometricFailure=null}reset(){this.tracker.reset(),this.gate.reset(),this.heldCards=[],this.hoverIds.reset(),this.lastVideoTimeSec=null}async analyze(t,n){let o=this.now(),u=gP(this.lastVideoTimeSec,n);u&&this.reset(),n!==void 0&&(this.lastVideoTimeSec=n);let l=await this.detector.propose(t),d=this.now()-o,c=l.filter(A=>ad(A.aabb,t.width,t.height,this.minCropPx)).map(A=>n$(A,t)),{faces:s}=i$(c,this.faceClass),r=this.now(),i=await oT(t,this.embedder,this.recognizer,s,this.minCropPx,this.geometric),a=this.now()-r;if(i.geometricError!==void 0&&this.geometric&&(this.geometricFailure={stage:this.geometric,error:i.geometricError},this.geometric=null),i.proposals.length!==s.length)throw new Error("pipeline: a valid face box was not recognized");let p={frameWidth:t.width,frameHeight:t.height,names:this.names},h=vd(i,this.hoverSelection),m=s.map(A=>c.indexOf(A)),b=xd(h,this.hoverIds.assign(h),p,A=>m[A]),x=i.base??i,w=x.kept.map(A=>{let P=x.proposals[A];return{aabb:P.aabb,rect:P.rect,cardId:P.topId,embedding:P.embedding}}),y=this.tracker.update(w),S=w.length>0,T=this.gate.observe(S),I;return T.confirmed?S?(I=ts(y.map(A=>({instanceId:A.instanceId,aabb:A.detection.aabb,rect:A.detection.rect,identity:A.identity})),p),this.heldCards=I):I=this.heldCards:(this.heldCards=[],I=[]),{frame:{width:t.width,height:t.height},boxes:this.boxResults(c,s,i),hover:{selection:this.hoverSelection,cards:b},tracks:{shown:T.confirmed,cards:I},discontinuity:u,retried:i.retried,embeddings:i.embeddings,timings:{detectorMs:d,recognizeMs:a,analyzeMs:this.now()-o,...i.geometricMs!==void 0?{geometricMs:i.geometricMs}:{}}}}boxResults(t,n,o){let u=new Map(n.map((d,c)=>[d,c])),l=new Set(o.kept);return t.map(d=>{let c=u.get(d),s=null;if(c!==void 0){let r=o.proposals[c],i=r.accepted&&this.names.has(r.displayId)?r.displayId:null;s={topId:r.topId,topName:this.names.get(r.topId)??null,similarity:r.score,margin:r.margin,look:r.orientation,retried:r.retried,tauPassed:r.tauPassed,accepted:r.accepted,cardId:i,name:i===null?null:this.names.get(i)??null,kept:l.has(c),...r.geometric?{geometric:r.geometric}:{}}}return{box:bP(d.aabb),score:d.score,cls:d.cls,className:this.classNames[d.cls]??String(d.cls),face:s}})}};var sT=1,aT="vision-release",yP=[8,16,32];function uT(e,t){return yP.reduce((n,o)=>n+Math.floor(e/o)*Math.floor(t/o),0)}var wP=/^[0-9a-f]{16}$/,_P=/^[0-9a-f]{64}$/,vP=/^\d+\.\d+\.\d+$/,xP=["schemaVersion","kind","id","game","locale","detector","recognizer","bank","identity","catalog","accept","hover","runtime"],lT=["tau","delta","rotationRetry"],Me=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),lt=e=>typeof e=="number"&&Number.isInteger(e)&&e>0,Fi=e=>typeof e=="number"&&Number.isFinite(e),Ze=e=>typeof e=="string"&&e.trim()!=="";function $P(e){return!Ze(e)||e.includes("\\")||/^[A-Za-z][A-Za-z0-9+.-]*:/.test(e)||/[\x00-\x1f\x7f]/.test(e)||/^ | $/.test(e)||e.startsWith("/")?!1:!e.split("/").some(t=>{let n=t.replace(/%2e/gi,".");return n==="."||n===".."})}function Bi(e){return!Me(e)||!$P(e.path)||typeof e.sha256!="string"||!_P.test(e.sha256)||!lt(e.bytes)?null:{path:e.path,sha256:e.sha256,bytes:e.bytes}}function TP(e){let t=e.input,n=e.decode;if(!Me(t)||!lt(t.width)||!lt(t.height))return"detector.input must give a width and a height";if(!Me(n))return"detector.decode must be an object";if(!Ze(n.inputName)||!Ze(n.outputName))return"detector.decode must name the model's input and output";let o=[["layout","NCHW"],["channels","BGR"],["scale","0-255"],["score","obj * max(cls)"],["class","argmax(cls)"]];for(let[i,a]of o)if(n[i]!==a)return`detector.decode.${i} is not ${JSON.stringify(a)}, the contract this runtime implements`;let u=n.letterbox;if(!Me(u)||u.width!==t.width||u.height!==t.height)return"detector.decode.letterbox differs from detector.input";if(typeof u.pad!="number"||!Number.isInteger(u.pad)||u.pad<0||u.pad>255)return"detector.decode.letterbox.pad must be a whole number from 0 to 255";let l=[["anchor","top-left"],["resize","pillow-bilinear"],["ratio","min(height / frameHeight, width / frameWidth)"],["size","round(frame * ratio)"]];for(let[i,a]of l)if(u[i]!==a)return`detector.decode.letterbox.${i} is not ${JSON.stringify(a)}`;let d=n.classes;if(!Array.isArray(d)||d.length===0||!d.every(Ze)||new Set(d).size!==d.length)return"detector.decode.classes must be a list of distinct class names";if(e.faceClass!==Tr||d[Tr]!=="face")return`detector.faceClass must be ${Tr} and that class must be named "face"`;let c=n.output,s=["cx","cy","w","h","obj",...d.map(i=>`cls_${i}`)];if(!Me(c)||!Array.isArray(c.columns)||c.columns.length!==s.length||!c.columns.every((i,a)=>i===s[a]))return"detector.decode.output.columns must be cx, cy, w, h, obj and one cls_ per class";if(c.anchors!==uT(t.width,t.height))return`detector.decode.output.anchors must be ${uT(t.width,t.height)} for a ${t.width} x ${t.height} input`;if(!Fi(n.threshold)||!(n.threshold>0&&n.threshold<1))return"detector.decode.threshold must be a number between 0 and 1";if(n.threshold<.01)return`detector.decode.threshold ${n.threshold} is below 0.01, the lowest score at which the detector export checks box parity`;let r=n.nms;return!Me(r)||!Fi(r.iou)||!(r.iou>0&&r.iou<=1)||r.classAgnostic!==!0||r.greedy!==!0?"detector.decode.nms must give an iou above 0 and at most 1, with classAgnostic and greedy both true":lt(n.maxProposals)?lt(e.minCropPx)?{inputName:n.inputName,outputName:n.outputName,letterbox:{width:t.width,height:t.height,pad:u.pad},anchors:c.anchors,classes:d,decode:{threshold:n.threshold,nmsIou:r.iou,maxProposals:n.maxProposals},faceClass:Tr,minCropPx:e.minCropPx}:"detector.minCropPx must be a whole number above 0":"detector.decode.maxProposals must be a whole number above 0"}function SP(e){let t=I=>({error:I});if(!Me(e)||e.schemaVersion!==sT||e.kind!==aT)return t(`not a "${aT}" document of schema version ${sT}`);let n=Object.keys(e).filter(I=>!xP.includes(I));if(n.length>0)return t(`unknown key(s) ${n.sort().join(", ")}`);if(typeof e.id!="string"||!wP.test(e.id))return t("id must be 16 lowercase hex digits");if(!Ze(e.game)||!Ze(e.locale))return t("game and locale must be non-empty strings");let o=e.detector;if(!Me(o))return t("incomplete release: detector missing (a release is all-or-nothing)");let u=Bi(o);if(!u||!Ze(o.version)||!Ze(o.weights))return t("detector must pin a file and carry a version and a weights label");if(o.arm!=="yolox")return t(`detector.arm ${JSON.stringify(o.arm)} is not one this runtime implements`);let l=TP(o);if(typeof l=="string")return t(l);let d=e.recognizer;if(!Me(d))return t("incomplete release: recognizer missing (a release is all-or-nothing)");let c=Bi(d);if(!c||!Ze(d.version)||!Ze(d.weights)||!lt(d.dim))return t("recognizer must pin a file and carry a version, a weights label and a dim");if(!Ze(d.inputName)||!Ze(d.outputName))return t("recognizer must name the model's input and output");let s=Qo(d.preprocessing);if(s===null)return t(`recognizer.preprocessing is not a contract this runtime implements (known: ${Object.keys(er).join(", ")})`);let r=e.bank;if(!Me(r))return t("incomplete release: bank missing (a release is all-or-nothing)");let i=Bi(r);if(!i||!lt(r.version)||!lt(r.cardCount)||!lt(r.entryCount))return t("bank must pin a file and carry a version, a cardCount and an entryCount");if(r.aggregation!=="mean")return t('bank.aggregation must be "mean"');let a=e.identity;if(!Me(a)||!$d(a.level))return t('identity.level must be "name" or "printing"');let p=a.groups,h=Bi(p);if(!h||!Me(p)||!lt(p.version))return t("incomplete release: identity.groups must pin a file and carry a version");if(a.display!=="best-printing"&&a.display!=="representative")return t(`identity.display ${JSON.stringify(a.display)} is not one of best-printing, representative`);let m=e.catalog,b=Bi(m);if(!b||!Me(m)||!lt(m.cardCount))return t("incomplete release: catalog must pin a file and carry a cardCount");let x=e.accept;if(!Me(x))return t("incomplete release: accept missing (a release is all-or-nothing)");if(Object.keys(x).some(I=>!lT.includes(I)))return t(`accept has unknown key(s) (expected ${lT.join(", ")})`);if(!Fi(x.tau)||!(x.tau>0&&x.tau<=1))return t("accept.tau must be a number above 0 and at most 1");if(!Fi(x.delta)||!(x.delta>=0&&x.delta<1))return t("accept.delta must be a number from 0 to below 1");if(typeof x.rotationRetry!="boolean")return t("accept.rotationRetry must be true or false");let w=e.hover;if(!Me(w)||!Y$(w.selection))return t("hover.selection is not one of accepted-nms, best-score, detector-score");if(!lt(w.maxAgeMs))return t("hover.maxAgeMs must be a whole number above 0");let y=e.runtime;if(!Me(y)||typeof y.ortWeb!="string"||!vP.test(y.ortWeb))return t("runtime.ortWeb must be an exact version of the form 1.2.3");if(y.wasmThreads!==1)return t("runtime.wasmThreads must be 1");let S=y.executionProviders;if(!Array.isArray(S)||S.length===0||new Set(S).size!==S.length||!S.every(I=>Td.includes(I)))return t(`runtime.executionProviders must list one or more of ${Td.join(", ")}, each once`);let T=y.analysisWidth;return!Me(T)||!lt(T.min)||!lt(T.max)||T.min>T.max?t("runtime.analysisWidth must give whole numbers min <= max above 0"):{release:{id:e.id,game:e.game,locale:e.locale,detector:{...u,arm:"yolox",version:o.version,weights:o.weights,settings:l},recognizer:{...c,version:d.version,weights:d.weights,dim:d.dim,inputName:d.inputName,outputName:d.outputName,preprocessing:s},bank:{...i,version:r.version,cardCount:r.cardCount,entryCount:r.entryCount},identity:{level:a.level,groups:{...h,version:p.version},display:a.display},catalog:{...b,cardCount:m.cardCount},accept:{tau:x.tau,delta:x.delta,rotationRetry:x.rotationRetry},hover:{selection:w.selection,maxAgeMs:w.maxAgeMs},runtime:{ortWeb:y.ortWeb,executionProviders:S,wasmThreads:1,analysisWidth:{min:T.min,max:T.max}}}}}function IP(e){let t=0,n=()=>{throw new Error("not JSON")},o=()=>{for(;t{let c=t;for(e[t]!=='"'&&n(),t++;t=e.length&&n(),t++,JSON.parse(e.slice(c,t))},l=c=>{o();let s=e[t];if(s==="{"){t++;let i=[];if(o(),e[t]==="}")t++;else for(;;){o();let a=u();o(),e[t]!==":"&&n(),t++;let p=l(!1);if(c&&a==="id"||i.push([a,p]),o(),e[t]===",")t++;else if(e[t]==="}"){t++;break}else n()}return i.sort((a,p)=>a[0]p[0]?1:0),`{${i.map(([a,p])=>`${JSON.stringify(a)}:${p}`).join(",")}}`}if(s==="["){t++;let i=[];if(o(),e[t]==="]")t++;else for(;;)if(i.push(l(!1)),o(),e[t]===",")t++;else if(e[t]==="]"){t++;break}else n();return`[${i.join(",")}]`}if(s==='"')return JSON.stringify(u());let r=/^(?:true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/.exec(e.slice(t,t+64));return r?(t+=r[0].length,r[0]):n()};o(),e[t]!=="{"&&n();let d=l(!0);return o(),t!==e.length&&n(),d}async function OP(e,t){let n=new TextEncoder().encode(IP(e));return(await t(n.buffer)).slice(0,16)}async function EP(e){let t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t),n=>n.toString(16).padStart(2,"0")).join("")}var Id={fetchFn:e=>fetch(e),digestSha256Hex:EP};function AP(e,t){let n=l=>/^(\d+)\.(\d+)\.\d+/.exec(l),o=n(e),u=n(t);return o!==null&&u!==null&&o[1]===u[1]&&o[2]===u[2]}var Ge=class extends Error{};async function cT(e,t,n){let o;try{o=await e.fetchFn(t)}catch(u){throw new Ge(`${n}: fetch failed (${String(u)})`)}if(!o.ok)throw new Ge(`${n}: HTTP ${o.status}`);return o.arrayBuffer()}async function Mi(e,t,n,o){let u=await cT(e,new URL(n.path,t).href,o);if(u.byteLength!==n.bytes)throw new Ge(`${o}: ${n.path} is ${u.byteLength} bytes, the release says ${n.bytes}`);if(await e.digestSha256Hex(u)!==n.sha256)throw new Ge(`${o}: sha256 of ${n.path} does not match the release`);return u}function us(e,t){try{return JSON.parse(new TextDecoder("utf-8",{fatal:!0}).decode(e))}catch{throw new Ge(`${t}: the file is not JSON`)}}var kP=/^[a-z][a-zA-Z0-9]*$/,PP=["key","label","values","type"],dT=e=>typeof e=="string"?"text":"number",tr=e=>JSON.stringify(e)??String(e);function pT(e,t,n){for(let o=0;o!PP.includes(r));if(l.length>0)return`${u}: unknown key(s) ${l.join(", ")}`;if(typeof o.key!="string"||!kP.test(o.key))return`${u}: "key" ${tr(o.key)} must be a lowercase letter, then letters and digits only`;let d=o.key;if(d==="kind")return`${u}: "key" "kind" is reserved: the card's kind is always the first filter`;if(t.some(r=>r.key===d))return`${u}: duplicate key ${tr(d)}`;if(!Ze(o.label))return`${u} (${d}): "label" must be a non-empty string`;let c=null;if("type"in o){if(o.type!=="text"&&o.type!=="number")return`${u} (${d}): "type" must be "text" or "number", not ${tr(o.type)}`;c=o.type}let s=null;if("values"in o){if(!Array.isArray(o.values)||o.values.length===0)return`${u} (${d}): "values" must be a non-empty list`;let r=pT(o.values,c,`${u} (${d}) values`);if(r!=="text"&&r!=="number")return r;c=r,s=o.values}t.push({key:d,label:o.label,type:c,values:s})}return t}function DP(e,t,n,o){let u=`card ${n}`;if(!Me(e))return`${u}: "facets" must be an object of facet key to a value or a list of values`;for(let[l,d]of Object.entries(e)){let c=t.find(i=>i.key===l);if(c===void 0)return`${u}: facets.${l}: the catalog declares no facet ${tr(l)}`;let s=Array.isArray(d)?d:[d];if(s.length===0)return`${u}: facets.${l} is an empty list (leave the key out instead)`;let r=pT(s,c.type,`${u}: facets.${l}`);if(r!=="text"&&r!=="number")return r;c.type=r;for(let i of s){if(c.values!==null&&!c.values.includes(i))return`${u}: facets.${l}: ${tr(i)} is not one of the declared values`;let a=o.get(l)??[];a.includes(i)||a.push(i),o.set(l,a)}}return null}function RP(e){if(!Me(e)||!Ze(e.game)||!Ze(e.locale)||!Array.isArray(e.cards)||e.cards.length===0)return"the file is not a catalog (game, locale and a list of cards)";let t=e.identity===void 0?"name":e.identity;if(!$d(t))return`"identity" must be "name" or "printing", not ${JSON.stringify(e.identity)}`;let n=e.facets===void 0?[]:CP(e.facets);if(typeof n=="string")return n;let o=new Map,u=new Map,l=[];for(let c of e.cards){if(!Me(c)||!Ze(c.id)||!Ze(c.name))return"every card needs an id, a name and an art path";if(!("art"in c)||c.art!==null&&!Ze(c.art))return`card ${c.id}: "art" must be an art path (a file path or an http(s) URL), or null for a card without art`;if(u.has(c.id))return`card id ${c.id} appears twice`;let s=c.printingOf??null;if(s!==null&&!Ze(s))return`card ${c.id}: "printingOf" must be a card id or null`;let r=c.facets===void 0?null:DP(c.facets,n,c.id,o);if(r!==null)return r;if(c.extra!==void 0&&!Me(c.extra))return`card ${c.id}: "extra" must be an object (any keys and values), not ${tr(c.extra)}`;let i={id:c.id,name:c.name,art:c.art,printingOf:s};c.extra!==void 0&&(i.extra=c.extra),u.set(i.id,i),l.push(i)}if(t==="printing")for(let c of l){if(c.printingOf===null)continue;let s=u.get(c.printingOf);if(s===void 0||s===c)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is not another card`;if(s.printingOf!==null)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is itself a printing of ${s.printingOf}`}let d=n.map(c=>{let s=c.values??[...o.get(c.key)??[]];return c.values===null&&c.type==="number"&&s.sort((r,i)=>r-i),{key:c.key,label:c.label,type:c.type??"text",values:[...s]}});return{game:e.game,locale:e.locale,identity:t,cards:l,facets:d}}async function hT(e,t=Id,n={}){try{let o=await cT(t,e,"release"),u=SP(us(o,"release"));if("error"in u)throw new Ge(`release: ${u.error}`);let l=u.release,d=await OP(new TextDecoder().decode(o),t.digestSha256Hex);if(d!==l.id)throw new Ge(`release: id ${l.id} is not the hash of the document (which is ${d})`);if(n.ortWeb!==void 0&&!AP(l.runtime.ortWeb,n.ortWeb))throw new Ge(`release: measured on onnxruntime-web ${l.runtime.ortWeb}, this build runs ${n.ortWeb} (major and minor version must match)`);try{n.onDocument?.([l.detector,l.recognizer,l.bank,l.identity.groups,l.catalog].reduce((U,X)=>U+X.bytes,0))}catch{}let[c,s,r,i,a]=await Promise.all([Mi(t,e,l.detector,"detector"),Mi(t,e,l.recognizer,"recognizer"),Mi(t,e,l.bank,"bank"),Mi(t,e,l.identity.groups,"identity.groups"),Mi(t,e,l.catalog,"catalog")]),p=new Uint8Array(r),h=wd(p);if(h!==null&&h!==es)throw new Ge(`bank: the binary bank is of format version ${h}, and this runtime reads version ${es} only`);let m=yd(p)?Z$(p):X$(us(r,"bank"));if(!m)throw new Ge("bank: the file is not a bank this runtime can read");let b=[[m.version===l.bank.version,"bank.version differs from the bank file's"],[m.model===l.recognizer.version,`bank: the file's model ${JSON.stringify(m.model)} is not recognizer.version ${JSON.stringify(l.recognizer.version)}`],[m.dim===l.recognizer.dim,"bank: the vector size differs from recognizer.dim"],[m.preprocessing===l.recognizer.preprocessing,"bank: the file's preprocessing differs from recognizer.preprocessing"],[m.ids.length===l.bank.entryCount,"bank.entryCount is not the number of entries in the bank file"],[new Set(m.ids).size===l.bank.cardCount,"bank.cardCount is not the number of distinct card ids in the bank file"],[m.tau===null||m.tau===l.accept.tau,`bank: the file records tau ${m.tau}, accept.tau is ${l.accept.tau}`]];for(let[U,X]of b)if(!U)throw new Ge(X);let x=RP(us(a,"catalog"));if(typeof x=="string")throw new Ge(`catalog: ${x}`);if(x.game!==l.game||x.locale!==l.locale)throw new Ge("catalog: the file is for another game or locale");if(x.cards.length!==l.catalog.cardCount)throw new Ge(`catalog.cardCount ${l.catalog.cardCount} is not the file's ${x.cards.length} cards`);let w=new Map(x.cards.map(U=>[U.id,U.name])),y=m.ids.filter(U=>!w.has(U));if(y.length>0)throw new Ge(`${new Set(y).size} bank id(s) are not in the catalog (e.g. ${y[0]})`);let S=us(i,"identity.groups"),T=eT(S);if(!T)throw new Ge("identity.groups: the file is not an identity-groups document");if(T.version!==l.identity.groups.version)throw new Ge("identity.groups.version differs from the file's");if((Me(S)&&Me(S.bank)?S.bank.sha256:void 0)!==l.bank.sha256)throw new Ge("identity.groups: the file was not made for this release's bank (its bank sha256 differs)");let A=x.identity;if(l.identity.level!==A)throw new Ge(`identity.level is "${l.identity.level}", and the catalog states the identity rule "${A}": a release is used with the rule its catalog states`);if((Me(S)&&Me(S.rule)?S.rule.level:void 0)!==A)throw new Ge(`identity.groups: the file's groups are not by ${A}`);let O=new Map(x.cards.map(U=>[U.id,tT(U,A)])),B=U=>JSON.stringify(U.map(X=>[...X].sort()).sort((X,q)=>X[0]q[0]?1:0));if(B(T.groups)!==B(rT(m.ids,O)))throw new Ge(`identity.groups: the groups are not the ones the catalog's ${A==="name"?"names give":"identity rule gives"} for the bank's ids`);return{ok:!0,data:{release:l,detectorBytes:new Uint8Array(c),recognizerBytes:new Uint8Array(s),bank:m,identityGroups:T,cards:x.cards,names:w}}}catch(o){return{ok:!1,reason:o instanceof Ge?o.message:`unexpected: ${String(o)}`}}}async function NP(e){for(let t of e)try{await t?.release?.()}catch{}}async function fT(e,t,n=e.release.runtime.executionProviders){let{release:o}=e,u=null,l=null,d=async c=>(await NP([u,l]),{error:c});try{if(n.length===0)return await d("no execution provider to try");let c=o.recognizer;return u=await t.embedder(e.recognizerBytes,{inputName:c.inputName,outputName:c.outputName,inputSize:er[c.preprocessing].size,dim:c.dim},n),u?(l=await t.detector(e.detectorBytes,o.detector.settings,n),l?{pipeline:new as({detector:l,embedder:u,bank:e.bank,accept:o.accept,identityGroups:e.identityGroups,identityDisplay:o.identity.display,names:e.names,hoverSelection:o.hover.selection,classNames:o.detector.settings.classes,faceClass:o.detector.settings.faceClass,minCropPx:o.detector.settings.minCropPx}),embedder:u,detector:l,providers:[...n]}:await d("detector: the model did not initialize on any execution provider, or is not the model the release describes")):await d("recognizer: the model did not initialize on any execution provider, or is not the model the release describes")}catch(c){return await d(`unexpected while building: ${String(c)}`)}}var Vn=class extends Error{constructor(n){super(`the release could not be loaded: ${n}`);this.reason=n;this.name="ReleaseError"}reason};function zP(e){return async t=>{if(!t.startsWith(Mn))throw new Error(`${t} is not one of the release's files`);let n=L$(t.slice(Mn.length)),o=Object.hasOwn(e,n)?e[n]:void 0;if(o===void 0)throw new Error(`no file ${n} among the release's files`);return{ok:!0,status:200,arrayBuffer:()=>o.arrayBuffer()}}}var BP={embedder:(e,t,n)=>Xo.create(e,t,n),detector:(e,t,n)=>Ko.create(e,t,n)};async function mT(e,t={}){let n=t.files?Mn:typeof location<"u"?location.href:void 0,o=new URL(e,n).href,u=r=>{try{t.onProgress?.(r)}catch{}};u({phase:"downloading",bytes:null});let l={...Id,...t.deps,...t.files?{fetchFn:zP(t.files)}:{}},d=await hT(o,l,{ortWeb:Y2(),...t.onProgress?{onDocument:r=>u({phase:"downloading",bytes:r})}:{}});if(!d.ok)throw new Vn(d.reason);u({phase:"preparing"});let{data:c}=d;Q2({wasmThreads:c.release.runtime.wasmThreads,...t.wasmPaths!==void 0?{wasmPaths:t.wasmPaths}:{}}),od();let s=await fT(c,BP,t.providers??c.release.runtime.executionProviders);if("error"in s){let r=od();throw new Vn(r.length>0?`${s.error} (${r.join("; ")})`:s.error)}return{release:c.release,pipeline:s.pipeline,backend:{detector:s.detector.ep,recognizer:s.embedder.ep},cards:c.cards,names:c.names,stageInputs:{bankSha256:c.release.bank.sha256,bankIds:[...new Set(c.bank.ids)],identities:F$(c.cards,c.release.identity.level,c.identityGroups)},dispose:async()=>{await s.embedder.release?.(),await s.detector.release?.()}}}async function gT(e,t,n){let o=typeof location<"u"?location.href:void 0,u=new URL(t,o).href,l=n.fetchText??(async d=>{let c=await fetch(d);if(!c.ok)throw new Error(`${d}: HTTP ${c.status}`);return c.text()});return j$(u,{...n,fetchText:l,...e.stageInputs})}var bT=e=>e instanceof Error?e.message:String(e);function yT(e){let t=e.now??(()=>performance.now()),n=e.watchdog??new Yo,o=null,u=!1,l=!1,d=null,c=!1,s=!0;function r(y,S=null,T=null,I=null){e.post({kind:"geometric-status",state:y,reason:S,bytes:T,...I!==null?{note:I}:{}})}function i(){if(!(l||!o)){if(c){let y=o.pipeline.geometricStage;y&&(o.pipeline.setGeometric(null),y.dispose()),d?.dispose(),d=null;return}d!==null&&(o.pipeline.setGeometric(d),d=null)}}function a(y){c||(c=!0,i(),r("off",y))}async function p(y,S,T,I=!1){r("loading");let A=null,P=!1;try{if(A=await e.voteDevice(),A===null){c=!0,r("off","no-webgpu");return}A.lost.then(X=>{X.reason!=="destroyed"&&a(`device-lost: ${X.message||X.reason}`)});let{stage:O,bytes:B,note:U}=await e.openStage(y,S,A,T);if(P=!0,c){O.dispose();return}O.debug=I,d=O,i(),r("ready",null,B,U)}catch(O){A!==null&&!P&&e.releaseDevice(A),c||(c=!0,r("off",`load-failed: ${bT(O)}`))}}function h(y){if(!o)return;let S=o.pipeline.takeGeometricFailure();if(S){S.stage.dispose(),a(`runtime: ${bT(S.error)}`);return}let T=y.timings.geometricMs;T===void 0||!s||n.observe(T)&&a(`watchdog: the stage took over ${n.limitMs} ms of a frame on ${n.frames} frames in a row`)}function m(y){return{kind:"release-status",ok:!1,error:y,release:null,cards:[]}}function b(y){if(u)return Promise.resolve();u=!0,s=y.geometricWatchdog!==!1;let S=y.wasmPaths??e.wasmPaths;return e.loadRelease(y.releaseUrl,{wasmPaths:S,...y.providers?{providers:y.providers}:{},...y.files?{files:y.files}:{},onProgress:T=>e.post({kind:"release-progress",phase:T.phase,bytes:T.phase==="downloading"?T.bytes:null})}).then(async T=>{o=T;let I=T.release;e.post({kind:"release-status",ok:!0,error:null,release:{id:I.id,game:I.game,locale:I.locale,detector:{version:I.detector.version,ep:T.backend.detector},recognizer:{version:I.recognizer.version,ep:T.backend.recognizer},bankCards:I.bank.cardCount,catalogCards:I.catalog.cardCount,identityLevel:I.identity.level,decode:{...I.detector.settings.decode,minCropPx:I.detector.settings.minCropPx},accept:{...I.accept},hoverSelection:I.hover.selection,hoverMaxAgeMs:I.hover.maxAgeMs,analysisWidth:I.runtime.analysisWidth,ortWeb:I.runtime.ortWeb},cards:T.cards}),y.geometricUrl!==void 0&&await p(T,y.geometricUrl,y.geometricManifestSha256,y.geometricDebug===!0)}).catch(T=>{o||e.post(m(T instanceof Vn?T.reason:`unexpected error while loading (${String(T)})`))})}function x(y,S){let{envelope:T,error:I,videoTimeSec:A,width:P,height:O,t0:B}=S;return{kind:"frame-result",envelope:T,...I!==void 0?{error:I}:{},videoTimeSec:A,width:P,height:O,workerMs:+(t()-B).toFixed(2),analysis:y}}async function w(y){let{bitmap:S,videoTimeSec:T,envelope:I}=y,A=t(),{width:P,height:O}=S,B=U=>e.post(x(null,{envelope:I,error:U,videoTimeSec:T,width:P,height:O,t0:A}));if(l){S.close(),B(U$);return}l=!0;try{if(!o){B("no-release");return}let U=e.readBitmap(S);if(!U){B("no-2d-context");return}let X=await o.pipeline.analyze({data:U.data,width:U.width,height:U.height},T);h(X),e.post(x(X,{envelope:I,videoTimeSec:T,width:U.width,height:U.height,t0:A}))}catch(U){B(`analysis-failed: ${String(U)}`)}finally{S.close(),l=!1,i()}}return{async handle(y){y.kind==="ping"?e.post({kind:"pong"}):y.kind==="load"?await b(y):y.kind==="frame"&&await w(y)}}}var MP=new URL("./opencv.mjs",import.meta.url).href,ji=null,ls=null;function FP(e){let{width:t,height:n}=e;return(!ji||ji.width!==t||ji.height!==n)&&(ji=new OffscreenCanvas(t,n),ls=ji.getContext("2d",{willReadFrequently:!0})),ls?(ls.drawImage(e,0,0),ls.getImageData(0,0,t,n)):null}async function jP(){let e=navigator.gpu;if(!e)return null;let t=await e.requestAdapter();return t?t.requestDevice({requiredLimits:Sp(t)}):null}var LP=yT({post:e=>self.postMessage(e),loadRelease:mT,wasmPaths:new URL("./",import.meta.url).href,voteDevice:jP,openStage:(e,t,n,o)=>gT(e,t,{loadIndex:(u,l)=>vp(u,{...l,...o!==void 0?{manifestSha256:o}:{}}),loadCv:()=>xp(MP),makeVoter:async u=>{let l=await Ip(n,u);return{knn2:d=>l.knn2(d),dispose:()=>{l.dispose(),n.destroy()}}}}),releaseDevice:e=>e.destroy(),readBitmap:FP});self.onmessage=e=>{LP.handle(e.data)}; +${l}`,c=n.createShaderModule({code:d,label:e.name});Ie("verbose",()=>`[WebGPU] ${e.name} shader code: ${d}`);let s=n.createComputePipeline({compute:{module:c,entryPoint:"main"},layout:"auto",label:e.name});return Vt(e.name),{programInfo:e,computePipeline:s,uniformVariablesInfo:u.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,n=typeof e=="number"?1:e.y||1,i=typeof e=="number"?1:e.z||1,u=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=u&&n<=u&&i<=u)return[t,n,i];let l=t*n*i,d=Math.ceil(Math.sqrt(l));if(d>u){if(d=Math.ceil(Math.cbrt(l)),d>u)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[d,d,d]}else return[d,d,1]}}}),U2={};gn(U2,{WebGpuBackend:()=>V2});var ky,Py,Cy,V2,Ik=N(()=>{"use strict";dt(),le(),ar(),Yv(),NA(),Tk(),Sk(),ky=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let n=[];for(let i=0;i{let i=e.name;return e.shaderCache?.hint&&(i+="["+e.shaderCache.hint+"]"),i+=":"+n+`:${ky(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,i},Cy=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vendor)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},V2=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let n=[],i={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n},u=c=>t.features.has(c)&&n.push(c)&&!0;u("chromium-experimental-timestamp-query-inside-passes")||u("timestamp-query"),u("shader-f16"),u("subgroups"),this.device=await t.requestDevice(i);let l=t,d=t.info??(typeof l.requestAdapterInfo=="function"?await l.requestAdapterInfo():void 0);this.adapterInfo=new Cy(d),this.gpuDataManager=ex(this),this.programManager=new L2(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Gl(e.logLevel,!!e.debug),this.device.onuncapturederror=c=>{c.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${c.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!0}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose(),this.device&&this.env?.webgpu&&this.device.lost.then(()=>{delete this.env.webgpu.device})}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Jt(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{let t=new BigUint64Array(e.getMappedRange()),n=this.pendingQueries.get(e);for(let i=0;i"u"&&(this.queryTimeBase=p);let m=Number(p-this.queryTimeBase),g=Number(h-this.queryTimeBase);if(!Number.isSafeInteger(m)||!Number.isSafeInteger(g))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:o.map(v=>({dims:v.dims,dataType:or(v.dataType)})),outputsMetadata:a.map(v=>({dims:v.dims,dataType:or(v.dataType)})),kernelId:l,kernelType:c,kernelName:s,programName:r,startTime:m,endTime:g});else{let v="";o.forEach((y,T)=>{v+=`input[${T}]: [${y.dims}] | ${or(y.dataType)}, `});let w="";a.forEach((y,T)=>{w+=`output[${T}]: [${y.dims}] | ${or(y.dataType)}, `}),console.log(`[profiling] kernel "${l}|${c}|${s}|${r}" ${v}${w}start time: ${m} ns, execution time: ${g-m} ns`)}Po("GPU",`${r}::${p}::${h}`)}e.unmap(),this.pendingQueries.delete(e)}),Vt()}run(e,t,n,i,u,l){Jt(e.name);let d=[];for(let y=0;yT):n;if(o.length!==c.length)throw new Error(`Output size ${o.length} must be equal to ${c.length}.`);let a=[],p=[];for(let y=0;y=l)throw new Error(`Invalid output index: ${o[y]}`);if(o[y]===-3)continue;let T=o[y]===-1,S=o[y]===-2,O=T||S?u(c[y].dataType,c[y].dims):i(o[y],c[y].dataType,c[y].dims);if(a.push(O),O.data===0)continue;let A=this.gpuDataManager.get(O.data);if(!A)throw new Error(`no GPU data for output: ${O.data}`);if(T&&this.temporaryData.push(A),S){let k=this.kernelPersistentData.get(this.currentKernelId);k||(k=[],this.kernelPersistentData.set(this.currentKernelId,k)),k.push(A)}p.push(A)}if(d.length!==t.length||p.length!==a.length){if(p.length===0)return Vt(e.name),a;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let h;if(r){let y=0,T=[];r.forEach(k=>{let I=typeof k.data=="number"?[k.data]:k.data;if(I.length===0)return;let R=k.type===10?2:4,M,X;k.type===10?(X=I.length>4?16:I.length>2?8:I.length*R,M=I.length>4?16:R*I.length):(X=I.length<=2?I.length*R:16,M=16),y=Math.ceil(y/X)*X,T.push(y);let G=k.type===10?8:4;y+=I.length>4?Math.ceil(I.length/G)*M:I.length*R});let S=16;y=Math.ceil(y/S)*S;let O=new ArrayBuffer(y);r.forEach((k,I)=>{let R=T[I],M=typeof k.data=="number"?[k.data]:k.data;if(k.type===6)new Int32Array(O,R,M.length).set(M);else if(k.type===12)new Uint32Array(O,R,M.length).set(M);else if(k.type===10)new Uint16Array(O,R,M.length).set(M);else if(k.type===1)new Float32Array(O,R,M.length).set(M);else throw new Error(`Unsupported uniform type: ${or(k.type)}`)});let A=this.gpuDataManager.create(y,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(A.buffer,0,O,0,y),this.gpuDataManager.release(A.id),h={offset:0,size:y,buffer:A.buffer}}let m=this.programManager.normalizeDispatchGroupSize(s),g=m[1]===1&&m[2]===1,v=Py(e,t,g),w=this.programManager.getArtifact(v);if(w||(w=this.programManager.build(e,m),this.programManager.setArtifact(v,w),Ie("info",()=>`[artifact] key: ${v}, programName: ${e.name}`)),r&&w.uniformVariablesInfo){if(r.length!==w.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${w.uniformVariablesInfo.length}, got ${r.length} in program "${w.programInfo.name}".`);for(let y=0;y`[ProgramManager] run "${e.name}" (key=${v}) with ${m[0]}x${m[1]}x${m[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let y={kernelId:this.currentKernelId,programName:w.programInfo.name,inputTensorViews:t,outputTensorViews:a};this.pendingKernels.push(y),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(y)}return this.programManager.run(w,d,p,m,h),Vt(e.name),a}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,n,i){let u=j2.get(e);if(!u)throw new Error(`kernel not implemented: ${e}`);let l={kernelType:e,kernelName:i,kernelEntry:u[0],attributes:[u[1],n]};this.kernels.set(t,l)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let n of t)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,n){let i=this.kernels.get(e);if(!i)throw new Error(`kernel not created: ${e}`);let u=i.kernelType,l=i.kernelName,d=i.kernelEntry,c=i.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${u}] ${l}" is not allowed to be called recursively`);this.currentKernelId=e,c[0]&&(c[1]=c[0](c[1]),c[0]=void 0),Ie("info",()=>`[WebGPU] Start to run kernel "[${u}] ${l}"...`);let s=this.env.debug;this.temporaryData=[];try{return s&&this.device.pushErrorScope("validation"),d(t,c[1]),0}catch(r){return n.push(Promise.resolve(`[WebGPU] Kernel "[${u}] ${l}" failed. ${r}`)),1}finally{s&&n.push(this.device.popErrorScope().then(r=>r?`GPU validation error for kernel "[${u}] ${l}": ${r.message}`:null));for(let r of this.temporaryData)this.gpuDataManager.release(r.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,n,i){let u=this.sessionExternalDataMapping.get(e);u||(u=new Map,this.sessionExternalDataMapping.set(e,u));let l=u.get(t),d=this.gpuDataManager.registerExternalBuffer(n,i,l);return u.set(t,[d,n]),d}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,n){return async()=>{let i=await hl(this,e,t);return ql(i.buffer,n)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){Ie("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){Ie("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){Ie("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),n=e.length;this.pendingKernels=[];for(let i=0;i=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),G2={};gn(G2,{init:()=>q2});var To,Dy,q2,Ok=N(()=>{"use strict";le(),ar(),pe(),RA(),To=class H2{constructor(t,n,i,u){this.module=t,this.dataType=n,this.data=i,this.dims=u}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=B.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=B.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=B.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=B.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(B.size(t)!==B.size(this.dims))throw new Error("Invalid new shape");return new H2(this.module,this.dataType,this.data,t)}},Dy=class{constructor(e,t,n){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let i=e.PTR_SIZE,u=n/e.PTR_SIZE,l=i===4?"i32":"i64";this.opKernelContext=Number(e.getValue(i*u++,l));let d=Number(e.getValue(i*u++,l));this.outputCount=Number(e.getValue(i*u++,l)),this.customDataOffset=Number(e.getValue(i*u++,"*")),this.customDataSize=Number(e.getValue(i*u++,l));let c=[];for(let s=0;stypeof d=="number"?this.inputs[d]:d)??this.inputs,i=t?.outputs??[],u=(d,c,s)=>new To(this.module,c,this.output(d,s),s),l=(d,c)=>{let s=un(d,c);if(!s)throw new Error(`Unsupported data type: ${d}`);let r=s>0?this.backend.gpuDataManager.create(s).id:0;return new To(this.module,d,r,c)};return this.backend.run(e,n,i,u,l,this.outputCount)}output(e,t){let n=this.module.stackSave();try{let i=this.module.PTR_SIZE,u=i===4?"i32":"i64",l=this.module.stackAlloc((1+t.length)*i);this.module.setValue(l,t.length,u);for(let d=0;d{let u=t.jsepInit;if(!u)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let l=(Ik(),Cn(U2)).WebGpuBackend,d=new l;await d.initialize(n,i),u("webgpu",[d,c=>d.alloc(Number(c)),c=>d.free(c),(c,s,r,o=!1)=>{if(o)Ie("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(c)}, dst=${Number(s)}, size=${Number(r)}`),d.memcpy(Number(c),Number(s));else{Ie("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(c)}, gpuDataId=${Number(s)}, size=${Number(r)}`);let a=t.HEAPU8.subarray(Number(c>>>0),Number(c>>>0)+Number(r));d.upload(Number(s),a)}},async(c,s,r)=>{Ie("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${c}, dataOffset=${s}, size=${r}`),await d.download(Number(c),()=>t.HEAPU8.subarray(Number(s)>>>0,Number(s+r)>>>0))},(c,s,r)=>d.createKernel(c,Number(s),r,t.UTF8ToString(t._JsepGetNodeName(Number(s)))),c=>d.releaseKernel(c),(c,s,r,o)=>{Ie("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${r}, kernel=${c}, contextDataOffset=${s}`);let a=new Dy(t,d,Number(s));return d.computeKernel(Number(c),a,o)},()=>d.captureBegin(),()=>d.captureEnd(),()=>d.replay()])}else{let l=new Qv(n);u("webnn",[l,()=>l.reserveTensorId(),d=>l.releaseTensorId(d),async(d,c,s,r,o)=>l.ensureTensor(d,c,s,r,o),(d,c)=>{l.uploadTensor(d,c)},async(d,c)=>l.downloadTensor(d,c),(d,c)=>l.registerMLContext(d,c),!!n.trace])}}}),Ry,rd,nd,br,Ny,gu,Uo,id,od,bu,sd,ad,ud,W2=N(()=>{"use strict";dt(),PA(),CA(),le(),yn(),jl(),Wv(),Ry=(e,t)=>{Be()._OrtInit(e,t)!==0&&De("Can't initialize onnxruntime.")},rd=async e=>{Ry(e.wasm.numThreads,Mo(e.logLevel))},nd=async(e,t)=>{Be().asyncInit?.();let n=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let i=e.webgpu.powerPreference;if(i!==void 0&&i!=="low-power"&&i!=="high-performance")throw new Error(`Invalid powerPreference setting: "${i}"`);let u=e.webgpu.forceFallbackAdapter;if(u!==void 0&&typeof u!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${u}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:i,forceFallbackAdapter:u}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let i=(Ok(),Cn(G2)).init;t==="webgpu"&&await i("webgpu",Be(),e,n),t==="webnn"&&await i("webnn",Be(),e)}},br=new Map,Ny=e=>{let t=Be(),n=t.stackSave();try{let i=t.PTR_SIZE,u=t.stackAlloc(2*i);t._OrtGetInputOutputCount(e,u,u+i)!==0&&De("Can't get session input/output count.");let l=i===4?"i32":"i64";return[Number(t.getValue(u,l)),Number(t.getValue(u+i,l))]}finally{t.stackRestore(n)}},gu=(e,t)=>{let n=Be(),i=n.stackSave(),u=0;try{let l=n.PTR_SIZE,d=n.stackAlloc(2*l);n._OrtGetInputOutputMetadata(e,t,d,d+l)!==0&&De("Can't get session input/output metadata.");let c=Number(n.getValue(d,"*"));u=Number(n.getValue(d+l,"*"));let s=n.HEAP32[u/4];if(s===0)return[c,0];let r=n.HEAPU32[u/4+1],o=[];for(let a=0;a{let t=Be(),n=t._malloc(e.byteLength);if(n===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,n),[n,e.byteLength]},id=async(e,t)=>{let n,i,u=Be();Array.isArray(e)?[n,i]=e:e.buffer===u.HEAPU8.buffer?[n,i]=[e.byteOffset,e.byteLength]:[n,i]=Uo(e);let l=0,d=0,c=0,s=[],r=[],o=[];try{if([d,s]=await Hv(t),t?.externalData&&u.mountExternalData){let S=[];for(let O of t.externalData){let A=typeof O=="string"?O:O.path,k=typeof O=="string"?O:O.data;S.push(Vl(k).then(I=>{u.mountExternalData(A,I)}))}await Promise.all(S)}for(let S of t?.executionProviders??[])if((typeof S=="string"?S:S.name)==="webnn"){if(u.shouldTransferToMLTensor=!1,typeof S!="string"){let O=S,A=O?.context,k=O?.gpuDevice,I=O?.deviceType,R=O?.powerPreference;A?u.currentContext=A:k?u.currentContext=await u.webnnCreateMLContext(k):u.currentContext=await u.webnnCreateMLContext({deviceType:I,powerPreference:R})}else u.currentContext=await u.webnnCreateMLContext();break}l=await u._OrtCreateSession(n,i,d),u.webgpuOnCreateSession?.(l),l===0&&De("Can't create a session."),u.jsepOnCreateSession?.(),u.currentContext&&(u.webnnRegisterMLContext(l,u.currentContext),u.currentContext=void 0,u.shouldTransferToMLTensor=!0);let[a,p]=Ny(l),h=!!t?.enableGraphCapture,m=[],g=[],v=[],w=[],y=[];for(let S=0;SS==="gpu-buffer"||S==="ml-tensor"||S==="ml-tensor-cpu-output")&&(c=u._OrtCreateBinding(l),c===0&&De("Can't create IO binding."),T={handle:c,outputPreferredLocations:y,outputPreferredLocationsEncoded:y.map(S=>S==="ml-tensor-cpu-output"?"ml-tensor":S).map(S=>pl(S))}),br.set(l,[l,r,o,T,h,!1]),[l,m,g,v,w]}catch(a){throw r.forEach(p=>u._OrtFree(p)),o.forEach(p=>u._OrtFree(p)),c!==0&&u._OrtReleaseBinding(c)!==0&&De("Can't release IO binding."),l!==0&&u._OrtReleaseSession(l)!==0&&De("Can't release session."),a}finally{u._free(n),d!==0&&u._OrtReleaseSessionOptions(d)!==0&&De("Can't release session options."),s.forEach(a=>u._free(a)),u.unmountExternalData?.()}},od=e=>{let t=Be(),n=br.get(e);if(!n)throw new Error(`cannot release session. invalid session id: ${e}`);let[i,u,l,d,c]=n;d&&(c&&t._OrtClearBoundOutputs(d.handle)!==0&&De("Can't clear bound outputs."),t._OrtReleaseBinding(d.handle)!==0&&De("Can't release IO binding.")),t.jsepOnReleaseSession?.(e),t.webnnOnReleaseSession?.(e),t.webgpuOnReleaseSession?.(e),u.forEach(s=>t._OrtFree(s)),l.forEach(s=>t._OrtFree(s)),t._OrtReleaseSession(i)!==0&&De("Can't release session."),br.delete(e)},bu=async(e,t,n,i,u,l,d=!1)=>{if(!e){t.push(0);return}let c=Be(),s=c.PTR_SIZE,r=e[0],o=e[1],a=e[3],p=a,h,m;if(r==="string"&&(a==="gpu-buffer"||a==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(d&&a!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${l} when enableGraphCapture is true.`);if(a==="gpu-buffer"){let w=e[2].gpuBuffer;m=un(an(r),o);{let y=c.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');h=y(i,l,w,m)}}else if(a==="ml-tensor"){let w=e[2].mlTensor;m=un(an(r),o);let y=c.webnnRegisterMLTensor;if(!y)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');h=y(i,w,an(r),o)}else{let w=e[2];if(Array.isArray(w)){m=s*w.length,h=c._malloc(m),n.push(h);for(let y=0;yc.setValue(v+T*s,y,s===4?"i32":"i64"));let w=c._OrtCreateTensor(an(r),h,m,v,o.length,pl(p));w===0&&De(`Can't create tensor for input/output. session=${i}, index=${l}.`),t.push(w)}finally{c.stackRestore(g)}},sd=async(e,t,n,i,u,l)=>{let d=Be(),c=d.PTR_SIZE,s=br.get(e);if(!s)throw new Error(`cannot run inference. invalid session id: ${e}`);let r=s[0],o=s[1],a=s[2],p=s[3],h=s[4],m=s[5],g=t.length,v=i.length,w=0,y=[],T=[],S=[],O=[],A=[],k=d.stackSave(),I=d.stackAlloc(g*c),R=d.stackAlloc(g*c),M=d.stackAlloc(v*c),X=d.stackAlloc(v*c);try{[w,y]=qv(l),dn("wasm prepareInputOutputTensor");for(let _=0;_de*xe,1);K=or(Y);let ce=p?.outputPreferredLocations[i[_]];if(K==="string"){if(ce==="gpu-buffer"||ce==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let de=[];for(let xe=0;xe0){let de=d.jsepGetBuffer;if(!de)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let xe=de(F),Ge=un(Y,ue);if(Ge===void 0||!Ll(K))throw new Error(`Unsupported data type: ${K}`);H=!0,C.push([K,ie,{gpuBuffer:xe,download:d.jsepCreateDownloader(xe,Ge,K),dispose:()=>{d._OrtReleaseTensor(D)!==0&&De("Can't release tensor.")}},"gpu-buffer"])}else if(ce==="ml-tensor"&&ue>0){let de=d.webnnEnsureTensor,xe=d.webnnIsGraphInputOutputTypeSupported;if(!de||!xe)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(un(Y,ue)===void 0||!Ul(K))throw new Error(`Unsupported data type: ${K}`);if(!xe(e,K,!1))throw new Error(`preferredLocation "ml-tensor" for ${K} output is not supported by current WebNN Context.`);let Ge=await de(e,F,Y,ie,!1);H=!0,C.push([K,ie,{mlTensor:Ge,download:d.webnnCreateMLTensorDownloader(F,K),dispose:()=>{d.webnnReleaseTensorId(F),d._OrtReleaseTensor(D)}},"ml-tensor"])}else if(ce==="ml-tensor-cpu-output"&&ue>0){let de=d.webnnCreateMLTensorDownloader(F,K)(),xe=C.length;H=!0,W.push((async()=>{let Ge=[xe,await de];return d.webnnReleaseTensorId(F),d._OrtReleaseTensor(D),Ge})()),C.push([K,ie,[],"cpu"])}else{let de=Yo(K),xe=new de(ue);new Uint8Array(xe.buffer,xe.byteOffset,xe.byteLength).set(d.HEAPU8.subarray(F,F+xe.byteLength)),C.push([K,ie,xe,"cpu"])}}finally{d.stackRestore(V),K==="string"&&F&&d._free(F),H||d._OrtReleaseTensor(D)}}p&&!h&&(d._OrtClearBoundOutputs(p.handle)!==0&&De("Can't clear bound outputs."),br.set(e,[r,o,a,p,h,!1]));for(let[_,D]of await Promise.all(W))C[_][2]=D;return cn("wasm ProcessOutputTensor"),C}finally{d.webnnOnRunEnd?.(r),d.stackRestore(k),T.forEach(G=>d._OrtReleaseTensor(G)),S.forEach(G=>d._OrtReleaseTensor(G)),O.forEach(G=>d._free(G)),w!==0&&d._OrtReleaseRunOptions(w),y.forEach(G=>d._free(G))}},ad=e=>{let t=Be(),n=br.get(e);if(!n)throw new Error("invalid session id");let i=n[0],u=t._OrtEndProfiling(i);u===0&&De("Can't get an profile file name."),t._OrtFree(u)},ud=e=>{let t=[];for(let n of e){let i=n[2];!Array.isArray(i)&&"buffer"in i&&t.push(i.buffer)}return t}}),yr,At,Sn,gi,bi,So,yu,Io,Gr,qr,zy,K2,X2,Z2,Y2,J2,Q2,e$,t$=N(()=>{"use strict";dt(),W2(),yn(),Bl(),yr=()=>!!me.wasm.proxy&&typeof document<"u",Sn=!1,gi=!1,bi=!1,Io=new Map,Gr=(e,t)=>{let n=Io.get(e);n?n.push(t):Io.set(e,[t])},qr=()=>{if(Sn||!gi||bi||!At)throw new Error("worker not ready")},zy=e=>{switch(e.data.type){case"init-wasm":Sn=!1,e.data.err?(bi=!0,yu[1](e.data.err)):(gi=!0,yu[0]()),So&&(URL.revokeObjectURL(So),So=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=Io.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},K2=async()=>{if(!gi){if(Sn)throw new Error("multiple calls to 'initWasm()' detected.");if(bi)throw new Error("previous call to 'initWasm()' failed.");if(Sn=!0,yr())return new Promise((e,t)=>{At?.terminate(),Vv().then(([n,i])=>{try{At=i,At.onerror=l=>t(l),At.onmessage=zy,yu=[e,t];let u={type:"init-wasm",in:me};!u.in.wasm.wasmPaths&&(n||cl)&&(u.in.wasm.wasmPaths={wasm:new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url).href}),At.postMessage(u),So=n}catch(u){t(u)}},t)});try{await Fl(me.wasm),await rd(me),gi=!0}catch(e){throw bi=!0,e}finally{Sn=!1}}},X2=async e=>{if(yr())return qr(),new Promise((t,n)=>{Gr("init-ep",[t,n]);let i={type:"init-ep",in:{epName:e,env:me}};At.postMessage(i)});await nd(me,e)},Z2=async e=>yr()?(qr(),new Promise((t,n)=>{Gr("copy-from",[t,n]);let i={type:"copy-from",in:{buffer:e}};At.postMessage(i,[e.buffer])})):Uo(e),Y2=async(e,t)=>{if(yr()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return qr(),new Promise((n,i)=>{Gr("create",[n,i]);let u={type:"create",in:{model:e,options:{...t}}},l=[];e instanceof Uint8Array&&l.push(e.buffer),At.postMessage(u,l)})}else return id(e,t)},J2=async e=>{if(yr())return qr(),new Promise((t,n)=>{Gr("release",[t,n]);let i={type:"release",in:e};At.postMessage(i)});od(e)},Q2=async(e,t,n,i,u,l)=>{if(yr()){if(n.some(d=>d[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(u.some(d=>d))throw new Error("pre-allocated output tensor is not supported for proxy.");return qr(),new Promise((d,c)=>{Gr("run",[d,c]);let s=n,r={type:"run",in:{sessionId:e,inputIndices:t,inputs:s,outputIndices:i,options:l}};At.postMessage(r,ud(s))})}else return sd(e,t,n,i,u,l)},e$=async e=>{if(yr())return qr(),new Promise((t,n)=>{Gr("end-profiling",[t,n]);let i={type:"end-profiling",in:e};At.postMessage(i)});ad(e)}}),wu,My,r$,Ek=N(()=>{"use strict";dt(),t$(),le(),Ml(),Wv(),wu=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},My=e=>{switch(e[3]){case"cpu":return new _t(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Ll(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:n,download:i,dispose:u}=e[2];return _t.fromGpuBuffer(n,{dataType:t,dims:e[1],download:i,dispose:u})}case"ml-tensor":{let t=e[0];if(!Ul(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:n,download:i,dispose:u}=e[2];return _t.fromMLTensor(n,{dataType:t,dims:e[1],download:i,dispose:u})}default:throw new Error(`invalid data location: ${e[3]}`)}},r$=class{async fetchModelAndCopyToWasmMemory(e){return Z2(await Vl(e))}async loadModel(e,t){Jt();let n;typeof e=="string"?n=await this.fetchModelAndCopyToWasmMemory(e):n=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await Y2(n,t),Vt()}async dispose(){return J2(this.sessionId)}async run(e,t,n){Jt();let i=[],u=[];Object.entries(e).forEach(a=>{let p=a[0],h=a[1],m=this.inputNames.indexOf(p);if(m===-1)throw new Error(`invalid input '${p}'`);i.push(h),u.push(m)});let l=[],d=[];Object.entries(t).forEach(a=>{let p=a[0],h=a[1],m=this.outputNames.indexOf(p);if(m===-1)throw new Error(`invalid output '${p}'`);l.push(h),d.push(m)});let c=i.map((a,p)=>wu(a,()=>`input "${this.inputNames[u[p]]}"`)),s=l.map((a,p)=>a?wu(a,()=>`output "${this.outputNames[d[p]]}"`):null),r=await Q2(this.sessionId,u,c,d,s,n),o={};for(let a=0;aSl,initializeFlags:()=>Tl,wasmBackend:()=>i$});var Tl,Sl,i$,Ak=N(()=>{"use strict";dt(),t$(),Ek(),Tl=()=>{(typeof me.wasm.initTimeout!="number"||me.wasm.initTimeout<0)&&(me.wasm.initTimeout=0);let e=me.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),me.wasm.simd=!1),typeof me.wasm.proxy!="boolean"&&(me.wasm.proxy=!1),typeof me.wasm.trace!="boolean"&&(me.wasm.trace=!1),typeof me.wasm.numThreads!="number"||!Number.isInteger(me.wasm.numThreads)||me.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)me.wasm.numThreads=1;else{let t=typeof navigator>"u"?_u("node:os").cpus().length:navigator.hardwareConcurrency;me.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},Sl=class{async init(e){Tl(),await K2(),await X2(e)}async createInferenceSessionHandler(e,t){let n=new r$;return await n.loadModel(e,t),n}},i$=new Sl});dt();dt();dt();var kk="1.29.0";{let e=(EA(),Cn(zv)).onnxjsBackend;ln("webgl",e,-10)}{let e=(Ak(),Cn(n$)).wasmBackend;ln("webgpu",e,5),ln("webnn",e,5),ln("cpu",e,10),ln("wasm",e,10)}Object.defineProperty(me.versions,"web",{value:kk,enumerable:!0});function o$(){return me.versions.web??""}function s$(e={}){me.wasm.numThreads=e.wasmThreads??1,e.wasmPaths!==void 0&&(me.wasm.wasmPaths=e.wasmPaths)}var Jo=[];function ld(){let e=Jo;return Jo=[],e}async function Qo(e,t,n){for(let i of t){let u=null;try{let l=performance.now();u=await Go.create(e.slice().buffer,{executionProviders:[i]});let d=performance.now();if(await n(u))return{session:u,ep:i,createMs:d-l,firstRunMs:performance.now()-d};Jo.push(`${i}: the model does not have the inputs and outputs the release describes`)}catch(l){Jo.push(`${i}: ${l instanceof Error?l.message:String(l)}`.slice(0,300))}await u?.release().catch(()=>{})}return null}function jn(e,t){return new _t("float32",e,t)}function dd(e){return{cx:e.x+e.width/2,cy:e.y+e.height/2,w:e.width,h:e.height,angle:0}}function Fi(e,t){let n=Math.max(e.x,t.x),i=Math.max(e.y,t.y),u=Math.min(e.x+e.width,t.x+t.width),l=Math.min(e.y+e.height,t.y+t.height);if(u<=n||l<=i)return 0;let d=(u-n)*(l-i);return d/(e.width*e.height+t.width*t.height-d)}function a$(e,t,n,i){let u=[...e].sort((d,c)=>n(c)-n(d)),l=[];for(let d of u){let c=t(d);l.every(s=>Fi(c,t(s))O&&(O=k,S=A)}O>=l&&(c.push(w),s.push(O),r.push(S))}let o=c.length;if(o===0)return[];let a=new Float32Array(o),p=new Float32Array(o),h=new Float32Array(o),m=new Float32Array(o);for(let w=0;wy).sort((w,y)=>s[y]-s[w]||w-y),v=[];for(;g.length>0;){let w=g[0];if(v.push(w),g.length===1)break;let y=Je(Je(h[w]-a[w])*Je(m[w]-p[w])),T=[];for(let S=1;S{let y={x:a[w]/n,y:p[w]/n,width:Je(h[w]-a[w])/n,height:Je(m[w]-p[w])/n};return{aabb:y,rect:dd(y),score:s[w],cls:r[w]}})}function cd(e,t,n,i=Ln){let u=Math.trunc(Math.max(0,e.x)),l=Math.trunc(Math.max(0,e.y)),d=Math.trunc(Math.min(t,e.x+e.width)),c=Math.trunc(Math.min(n,e.y+e.height));return d-u>=i&&c-l>=i}function l$(e,t,n,i,u,l=u$,d=Ln){return Dk(e,t,n,l).filter(c=>cd(c.aabb,i,u,d))}function Rk(e,t,n,i,u){let l=i/t,d=u/n;return[Math.max(0,e.x*l),Math.max(0,e.y*d),Math.min(i,(e.x+e.width)*l),Math.min(u,(e.y+e.height)*d)]}function d$(e,t){let n=e.aabb;if(n.x>=0&&n.y>=0&&n.x+n.width<=t.width&&n.y+n.height<=t.height)return e;let[i,u,l,d]=Rk(n,t.width,t.height,t.width,t.height),c={x:i,y:u,width:Math.max(0,l-i),height:Math.max(0,d-u)};return{...e,aabb:c,rect:dd(c)}}function c$(e,t=kr){let n=[],i=[];for(let u of e)(u.cls===t?n:i).push(u);return{faces:n,others:i}}function Nk(e){return e<0&&(e=-e),e<1?1-e:0}function p$(e,t){let n=e/t,i=n<1?1:n,u=i,l=Math.ceil(u)*2+1,d=new Int32Array(t*2),c=new Int32Array(t*l),s=new Float64Array(l);for(let r=0;re&&(m=e),m-=h;for(let g=0;g>22;return t<0?0:t>255?255:t}var h$="",pd=null,hd=null;function zk(e,t,n,i){let u=`${e}x${t}>${n}x${i}`;return(u!==h$||!pd||!hd)&&(pd=p$(e,n),hd=p$(t,i),h$=u),{h:pd,v:hd}}function f$(e,t,n,i,u){let{h:l,v:d}=zk(t,n,i,u),c=i!==t,s=u!==n,r,o;if(c){let w=s?d.bounds[0]:0,T=(s?d.bounds[u*2-2]+d.bounds[u*2-1]:n)-w;o=w,r=new Uint8Array(i*T*3);let{ksize:S,bounds:O,kk:A}=l;for(let k=0;k.5?t+1:n<.5||t%2===0?t:t+1}function Mk(e,t,n=g$){let i=Math.min(n.height/t,n.width/e);return{ratio:i,width:m$(e*i),height:m$(t*i)}}function b$(e,t,n,i=g$){let u=Mk(t,n,i),l=f$(e,t,n,u.width,u.height),d=i.width*i.height,c=new Float32Array(3*d).fill(i.pad);for(let s=0;s{let o=(await s.run({[n.inputName]:jn(new Float32Array(3*u*l),[1,3,l,u])}))[n.outputName]?.dims??[];return o.length===3&&o[0]===1&&o[1]===n.anchors&&o[2]===d});return c?new e(c.session,c.ep,n,{createMs:c.createMs,firstRunMs:c.firstRunMs}):null}async propose(t){let{letterbox:n,inputName:i,outputName:u,decode:l,minCropPx:d}=this.settings,c=performance.now(),{tensor:s,ratio:r}=b$(t.data,t.width,t.height,n),o=performance.now(),a=await this.session.run({[i]:jn(s,[1,3,n.height,n.width])});return this.lastTimings={preprocessMs:o-c,inferMs:performance.now()-o},l$(a[u].data,this.columns,r,t.width,t.height,l,d)}async release(){await this.session.release().catch(()=>{})}};var Bk=32;async function Fk(e,t,n,i,u=Bk){if(n<=u)return e(t,n);let l=t.length/n,d=new Float32Array(n*i);for(let c=0;c(await d.run({[n.inputName]:jn(new Float32Array(3*u*u),[1,3,u,u])}))[n.outputName]?.data.length===n.dim);return l?new e(l.session,l.ep,n,{createMs:l.createMs,firstRunMs:l.firstRunMs}):null}async embedBatch(t,n){return Fk((i,u)=>this.run(i,u),t,n,this.settings.dim)}async run(t,n){let{inputName:i,outputName:u,inputSize:l,dim:d}=this.settings,s=(await this.session.run({[i]:jn(t,[n,3,l,l])}))[u].data;if(s.length!==n*d)throw new Error(`unexpected embedding size ${s.length} for ${n} crops`);return s}async release(){await this.session.release().catch(()=>{})}};var fd=["orb","akaze","sift"],y$=["none","median"],w$=["bf","flann"],_$=["homography","affine","similarity"],v$=["ransac","usac"],T$={orb:[["scaleFactor",1.2,!1],["nLevels",8,!0],["edgeThreshold",15,!0],["patchSize",15,!0],["fastThreshold",10,!0]],akaze:[["threshold",.001,!1],["nOctaves",4,!0],["nOctaveLayers",4,!0]],sift:[["nOctaveLayers",3,!0],["contrastThreshold",.04,!1],["edgeThreshold",10,!1],["sigma",1.6,!1]]},F4=Object.fromEntries(fd.map(e=>[e,Object.fromEntries(T$[e].map(([t,n])=>[t,n]))])),x$=Object.freeze({seed:0,features:{type:"orb",maxFeatures:300,artLongSide:320,mask:[],params:{}},crop:{longSide:320,maxFeatures:500},candidates:{embeddingTopK:5,voteTopN:0},vote:{matcher:"bf",ratio:.8,lshTables:12,lshKeySize:20,lshProbes:2,kdTrees:4,checks:64},match:{ratio:.8},ransac:{model:"homography",method:"ransac",threshold:6,maxIters:2e3,confidence:.995,minAreaRatio:.25,maxAreaRatio:4},fusion:{weight:.1,minInliers:8,fullInliers:30,penalty:0,baseline:"none",reorder:!0},gate:{belowAccept:!1,sources:null}}),S$=["features","crop","candidates","vote","match","ransac","fusion","gate"],$$=["index","seed",...S$],jk=/^[0-9a-f]{64}$/,ji=class extends Error{errors;constructor(t){super(t.join("; ")),this.name="GeometricConfigError",this.errors=[...t]}},md=class{constructor(t,n=null,i=null){this.indexPath=n;this.indexSha256=i;this.settings=I$(structuredClone(t))}indexPath;indexSha256;settings;get seed(){return this.settings.seed}get features(){return this.settings.features}toJson(){return structuredClone(this.settings)}};function I$(e){if(typeof e=="object"&&e!==null&&!Object.isFrozen(e)){Object.freeze(e);for(let t of Object.values(e))I$(t)}return e}function ns(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Un(e){return typeof e=="number"&&Number.isInteger(e)}function Pr(e){return typeof e=="number"}function Lk(e){let t=e.includes("'")&&!e.includes('"')?'"':"'",n="";for(let i of e){let u=i.codePointAt(0);i==="\\"?n+="\\\\":i===t?n+=`\\${t}`:i===` +`?n+="\\n":i==="\r"?n+="\\r":i===" "?n+="\\t":u<32||u===127?n+=`\\x${u.toString(16).padStart(2,"0")}`:n+=i}return t+n+t}function $t(e){return typeof e=="string"?Lk(e):e==null?"None":typeof e=="boolean"?e?"True":"False":Array.isArray(e)?`[${e.map($t).join(", ")}]`:typeof e=="number"?(Number.isInteger(e),String(e)):JSON.stringify(e)}function Li(e){return[...e].sort((t,n)=>tn?1:0)}function Vn(e){return Object.keys(e)}function Gn(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function Uk(e,t){let n=structuredClone(x$);Gn(e,"seed")&&(n.seed=e.seed);for(let i of S$){let u=Gn(e,i)?e[i]:{};if(!ns(u)){t.push(`${i} must be an object`);continue}let l=Vn(x$[i]),d=Li(Vn(u).filter(s=>!l.includes(s)));d.length>0&&t.push(`${i} has unknown key(s) ${$t(d)} (expected ${$t(Li(l))})`);let c=n[i];for(let s of Vn(u))l.includes(s)&&(c[s]=structuredClone(u[s]))}return n}function Vk(e,t){let n=e.type;if(typeof n!="string"||!fd.includes(n)){t.push(`features.type must be one of ${$t([...fd])}, got ${$t(n)}`);return}for(let o of["maxFeatures","artLongSide"]){let a=e[o];(!Un(a)||a<1)&&t.push(`features.${o} must be a whole number of at least 1`)}let i=e.mask,u=o=>Array.isArray(o)&&o.length===4&&o.every(a=>Pr(a)&&0<=a&&a<=1)&&o[0]o),s=Li(Vn(l).filter(o=>!c.includes(o)));s.length>0&&t.push(`features.params has unknown key(s) ${$t(s)} for ${n} (expected ${$t(Li(c))})`);let r={};for(let[o,a]of d)r[o]=Gn(l,o)?l[o]:a;for(let[o,,a]of d){let p=r[o];(a&&!Un(p)||!Pr(p)||p<=0)&&t.push(`features.params.${o} must be a ${a?"whole ":""}number above 0`)}e.params=r}function Gk(e,t){let n=e.seed;(!Un(n)||n<0)&&t.push("seed must be a whole number of 0 or more"),Vk(e.features,t);let i=e.crop,u=e.candidates,l=e.vote,d=e.match,c=e.ransac,s=e.fusion,r=[["crop",i,"longSide",1],["crop",i,"maxFeatures",1],["candidates",u,"embeddingTopK",0],["candidates",u,"voteTopN",0],["ransac",c,"maxIters",1],["vote",l,"lshTables",1],["vote",l,"lshKeySize",1],["vote",l,"lshProbes",0],["vote",l,"kdTrees",1],["vote",l,"checks",1],["fusion",s,"minInliers",0],["fusion",s,"fullInliers",1]];for(let[m,g,v,w]of r){let y=g[v];(!Un(y)||ya)&&t.push("ransac.minAreaRatio and maxAreaRatio must be numbers of 0 or more, the first at most the second");for(let m of["weight","penalty"]){let g=s[m];(!Pr(g)||g<0)&&t.push(`fusion.${m} must be a number of 0 or more`)}let p=e.gate;typeof p.belowAccept!="boolean"&&t.push("gate.belowAccept must be true or false");let h=p.sources;h!==null&&(!Array.isArray(h)||h.length===0||!h.every(m=>typeof m=="string"&&m.length>0))&&t.push('gate.sources must be null or a non-empty list of source types or patterns, e.g. ["physical/*"]'),y$.includes(s.baseline)||t.push(`fusion.baseline must be one of ${$t([...y$])}`),typeof s.reorder!="boolean"&&t.push("fusion.reorder must be true or false")}function qk(e,t=null,n="geometric settings"){if(!ns(e))throw new ji([`${n}: must be a JSON object`]);let i=[],u=Li(Vn(e).filter(r=>!$$.includes(r)));u.length>0&&i.push(`unknown key(s) ${$t(u)} (expected ${$t($$)})`);let l=Gn(e,"index")?e.index:{},d=null,c=null;if(!ns(l)||Vn(l).some(r=>r!=="path"&&r!=="sha256"))i.push('index must be an object with the keys "path" and "sha256"');else{let r=Gn(l,"path")?l.path:null;r!=null&&(typeof r!="string"||r.length===0?i.push("index.path must be a file path"):d=t===null?r:new URL(r,t).href);let o=Gn(l,"sha256")?l.sha256:null;o!=null&&(typeof o!="string"||!jk.test(o)?i.push("index.sha256 must be 64 lowercase hex digits"):c=o)}let s=Uk(e,i);if(Gk(s,i),i.length>0)throw new ji(i.map(r=>`${n}: ${r}`));return new md(Object.freeze(s),d,c)}function O$(e,t=null,n="geometric settings"){let i;try{i=JSON.parse(e)}catch(u){throw new ji([`${n}: not valid JSON: ${u instanceof Error?u.message:String(u)}`])}return qk(i,t,n)}function E$(e,t,n){let i=n/Math.max(t,e);return i===1?"none":i<1?"down":"up"}var Hk=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]);function gd(e){let t=e.length,n=Math.ceil((t+9)/64),i=new Uint8Array(n*64);i.set(e),i[t]=128;let u=new DataView(i.buffer),l=t*8;u.setUint32(i.length-8,Math.floor(l/2**32)),u.setUint32(i.length-4,l>>>0);let d=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),c=new Uint32Array(64);for(let s=0;s>>7|y<<25)^(y>>>18|y<<14)^y>>>3,O=(T>>>17|T<<15)^(T>>>19|T<<13)^T>>>10;c[w]=c[w-16]+S+c[w-7]+O>>>0}let[r,o,a,p,h,m,g,v]=[d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7]];for(let w=0;w<64;w++){let y=(h>>>6|h<<26)^(h>>>11|h<<21)^(h>>>25|h<<7),T=h&m^~h&g,S=v+y+T+Hk[w]+c[w]>>>0,O=(r>>>2|r<<30)^(r>>>13|r<<19)^(r>>>22|r<<10),A=r&o^r&a^o&a,k=O+A>>>0;v=g,g=m,m=h,h=p+S>>>0,p=a,a=o,o=r,r=S+k>>>0}d[0]=d[0]+r>>>0,d[1]=d[1]+o>>>0,d[2]=d[2]+a>>>0,d[3]=d[3]+p>>>0,d[4]=d[4]+h>>>0,d[5]=d[5]+m>>>0,d[6]=d[6]+g>>>0,d[7]=d[7]+v>>>0}return Array.from(d,s=>s.toString(16).padStart(8,"0")).join("")}function A$(e,t,n,i=32){let u=new Uint8Array(8+i),l=new DataView(u.buffer),d=[];for(let c=0;cu);return n.sort((i,u)=>e[u]-e[i]),n.slice(0,t)}function P$(e,t,n,i,u,l,d){let c=bd(e,t,n,i,u);try{return yd(e,c,l,d)}finally{c.delete()}}function yd(e,t,n,i){let u=[t.cols,t.rows],l=Kk(e,n,i),d=new e.KeyPointVector,c=new e.Mat,s=new e.Mat;try{l.detectAndCompute(t,s,d,c,!1);let r=d.size();if(r===0||c.empty()||c.rows===0)return{n:0,xy:new Float32Array(0),desc:new Uint8Array(0),size:u};let o=c.cols,a=new Float64Array(r),p=new Float32Array(r*2);for(let w=0;w{m[2*y]=p[2*w],m[2*y+1]=p[2*w+1],g.set(v.subarray(w*o,(w+1)*o),y*o)}),{n:h.length,xy:m,desc:g,size:u}}finally{l.delete(),d.delete(),c.delete(),s.delete()}}var C$="embedding",D$="vote",R$="both";function N$(e,t,n){return t>=n.minInliers?e+n.weight*Math.min(t,n.fullInliers)/n.fullInliers:e-n.penalty}function Zk(e){let t=[...e].sort((u,l)=>u-l),n=t.length;if(n===0)return Number.NaN;let i=n>>1;return n%2===1?t[i]:(t[i-1]+t[i])/2}function z$(e,t){return t.baseline==="median"&&e.length>0?Zk(e):0}function M$(e,t){return Math.max(0,e-t)}function B$(e,t,n){let i=new Float64Array(e);for(let u of t){let l=u.fused-u.cosine;for(let d of n(u.cardId))i[d]=i[d]+l}return i}var Yk=10n;function Jk(e){let t=new DataView(new ArrayBuffer(8));t.setFloat64(0,e);let n=t.getUint32(0),i=t.getUint32(4),u=n>>>31?-1n:1n,l=n>>>20&2047,d=BigInt(n&1048575)<<32n|BigInt(i),c;return l===0?c=-1074:(d|=1n<<52n,c=l-1075),{mantissa:u*d,exponent:c}}function Ui(e,t){if(!Number.isFinite(e)||e===0)return e;let{mantissa:n,exponent:i}=Jk(e),u=n<0n,d=(u?-n:n)*Yk**BigInt(t),c=1n;i>=0?d<<=BigInt(i):c<<=BigInt(-i);let s=d/c,o=2n*(d-s*c);if((o>c||o===c&&s%2n===1n)&&(s+=1n),s===0n)return u?-0:0;let a=`${u?"-":""}${s.toString()}e-${t}`;return Number(a)}function Qk(e){return{cardId:e.cardId,cosine:Ui(e.cosine,6),foundBy:e.foundBy,votes:e.votes,matches:e.matches,inliers:e.inliers,evidence:Ui(e.evidence,6),fused:Ui(e.fused,6)}}function F$(e,t){return{before:{topId:e.topId,similarity:Ui(e.similarity,6),margin:Ui(e.margin,6),accepted:e.accepted},candidates:t.candidates.map(Qk),...t.debug?{debug:t.debug}:{}}}function L$(e,t,n,i,u,l,d){let c=new Map;for(let s=n;s=d*e.dSecond[s])continue}c.set(o,(c.get(o)??0)+1)}return c}function U$(e,t,n,i,u,l,d=32){let c=new e.Mat(n,d,e.CV_8UC1),s=new e.Mat(u,d,e.CV_8UC1),r=new e.BFMatcher(e.NORM_HAMMING,!1),o=new e.DMatchVectorVector;try{c.data.set(t.subarray(0,n*d)),s.data.set(i.subarray(0,u*d)),r.knnMatch(c,s,o,2);let a=[],p=o.size();for(let h=0;h0&&!rP(e,a,i,u,l))return 0;let g=0,v=o.data;for(let w=0;wtypeof performance<"u"?performance.now():Date.now();function q$(e,t){let n=e.settings;return n.features.type!=="orb"?`features.type ${JSON.stringify(n.features.type)} is not ported (only "orb")`:n.candidates.voteTopN>0&&n.vote.matcher!=="bf"?`vote.matcher ${JSON.stringify(n.vote.matcher)} is not ported (only "bf")`:V$(t,n.ransac)}var wd=class{constructor(t,n,i,u,l=new Map){this.config=t;this.index=n;this.voter=i;this.cv=u;let d=q$(t,u);if(d)throw new Error(d);n.cards.forEach((c,s)=>this.position.set(c.cardId,s)),this.identity=n.cards.map(c=>l.get(c.cardId)??c.cardId),this.image=n.cards.map(c=>c.artV??c.cardId)}config;index;voter;cv;position=new Map;identity;image;debug=!1;get reorder(){return this.config.settings.fusion.reorder}applies(t,n){let i=this.config.settings.gate;return i.belowAccept&&n?!1:i.sources===null?!0:t!==null&&i.sources.some(u=>nP(t,u))}dispose(){this.voter.dispose()}cropFeatures(t){let{crop:n,features:i}=this.config.settings;return P$(this.cv,t.rgba,t.width,t.height,n.longSide,n.maxFeatures,i.params)}inliers(t,n){let i=this.index.cards[n];if(t.n<2||i.n<2)return[0,0];let u=this.index.desc.subarray(i.start*qn,(i.start+i.n)*qn),l=U$(this.cv,t.desc,t.n,u,i.n,this.config.settings.match.ratio,qn);if(l.length===0)return[0,0];let d=new Float32Array(l.length*2),c=new Float32Array(l.length*2),s=this.index.xy;return l.forEach((r,o)=>{d[2*o]=s[2*(i.start+r.trainIdx)],d[2*o+1]=s[2*(i.start+r.trainIdx)+1],c[2*o]=t.xy[2*r.queryIdx],c[2*o+1]=t.xy[2*r.queryIdx+1]}),[l.length,G$(this.cv,d,c,t.size,[i.width,i.height],this.config.settings.ransac)]}async verify(t,n){return(await this.verifyBatch([t],n))[0]}async verifyBatch(t,n){let i=this.config.settings,u=t.map(()=>0),l=t.map(()=>null),d=t.map((s,r)=>{this.cv.setRNGSeed?.(i.seed);let o=is(),a=this.debug?this.debugFeatures(s,p=>l[r]=p):this.cropFeatures(s);return u[r]=u[r]+is()-o,a}),c=t.map(()=>new Map);if(i.candidates.voteTopN>0&&this.index.rows>0){let s=d.reduce((r,o)=>r+o.n,0);if(s>0){let r=new Uint8Array(s*qn),o=[],a=0;for(let h of d)o.push(a),r.set(h.desc.subarray(0,h.n*qn),a*qn),a+=h.n;let p=await this.voter.knn2(r);d.forEach((h,m)=>{h.n<1||(c[m]=L$(p,h.n,o[m],this.index.rowCard,this.identity,this.image,i.vote.ratio))})}}return t.map((s,r)=>{let o=is(),a=this.finish(s,d[r],c[r],n);u[r]=u[r]+is()-o;let p=Math.round(u[r]*1e3)/1e3;return this.debug?{...a,ms:p,debug:this.debugRecord(s,l[r],d[r],c[r],p)}:{...a,ms:p}})}debugFeatures(t,n){let{crop:i,features:u}=this.config.settings,l=bd(this.cv,t.rgba,t.width,t.height,i.longSide);try{return n({sha256:gd(l.data.subarray(0,l.rows*l.cols)),width:l.cols,height:l.rows}),yd(this.cv,l,i.maxFeatures,u.params)}finally{l.delete()}}debugRecord(t,n,i,u,l){let d={},c=[...u.keys()].map(r=>this.index.cards[r].cardId).sort();for(let r of u.keys())d[this.index.cards[r].cardId]=u.get(r);let s={};for(let r of c)s[r]=d[r];return{grey:n,resize:E$(t.width,t.height,this.config.settings.crop.longSide),source:[t.width,t.height],keypoints:i.n,rows:A$(i.n,i.xy,i.desc),votes:s,ms:l}}finish(t,n,i,u){let l=this.config.settings,d=new Map;for(let{cardId:T,cosine:S}of t.ranked)d.has(T)||d.set(T,S);let c=[...d.keys()];c.sort((T,S)=>d.get(S)-d.get(T));let s=0,r=!0;for(let T of d.values())(r||Tu===void 0||u.has(T)).sort((T,S)=>a.get(S)-a.get(T)||this.position.get(T)-this.position.get(S)).slice(0,l.candidates.voteTopN),h=[...new Set([...o,...p])],m=new Map;for(let T of h){let S=this.position.get(T);S!==void 0&&m.set(T,this.inliers(n,S))}let g=z$([...m.values()].map(([,T])=>T),l.fusion),v=new Set(o),w=new Set(p);return{candidates:h.map(T=>{let S=v.has(T)&&w.has(T)?R$:v.has(T)?C$:D$,O=d.get(T)??s,A=m.get(T);if(A===void 0)return{cardId:T,cosine:O,foundBy:S,votes:0,matches:0,inliers:0,evidence:0,fused:O,indexed:!1};let[k,I]=A,R=M$(I,g);return{cardId:T,cosine:O,foundBy:S,votes:a.get(T)??0,matches:k,inliers:I,evidence:R,fused:N$(O,R,l.fusion),indexed:!0}}),cropKeypoints:n.n,ms:0}}};function nP(e,t){let n="";for(let i=0;i=t.length)n+="\\[";else{let d=t.slice(i+1,l).replace(/\\/g,"\\\\");d.startsWith("!")?d="^"+d.slice(1):d.startsWith("^")&&(d="\\"+d),n+=`[${d}]`,i=l}}else n+=u.replace(/[.*+?^${}()|[\]\\/]/g,"\\$&")}return new RegExp(`^(?:${n})$`).test(e)}function H$(e,t,n){let i=new Map;for(let u of e)i.set(u.id,t==="printing"?u.printingOf??u.id:u.name);if(n)for(let u of n.groups){let l=u.find(d=>!n.representative.has(d))??[...u].sort()[0];for(let d of u)i.set(d,`group:${n.representative.get(d)??l}`)}return i}var Vi=class{constructor(t=1500,n=2){this.limitMs=t;this.frames=n}limitMs;frames;run=0;observe(t){return this.run=t>this.limitMs?this.run+1:0,this.run>=this.frames}reset(){this.run=0}};function _d(e,t){if(e===t)return!0;if(Array.isArray(e)||Array.isArray(t))return Array.isArray(e)&&Array.isArray(t)&&e.length===t.length&&e.every((u,l)=>_d(u,t[l]));if(typeof e!="object"||typeof t!="object"||e===null||t===null)return!1;let n=Object.keys(e),i=Object.keys(t);return n.length===i.length&&n.every(u=>Object.hasOwn(t,u)&&_d(e[u],t[u]))}function iP(e,t){return[...new Set([...Object.keys(e),...Object.keys(t)])].filter(i=>!_d(e[i],t[i])).sort()}function oP(e,t){let n=/Version control:\s*(\S+)/.exec(t.getBuildInformation?.()??"")?.[1]??"";return e===n?null:`the descriptor index was built with OpenCV ${e||"of an unrecorded version"} and OpenCV ${n||"of an unknown version"} is running; keypoints may differ slightly from the index's`}async function W$(e,t){let n=O$(await t.fetchText(e),e,e.split("/").pop()||"geometric settings");if(n.indexPath===null||n.indexSha256===null)throw new Error("index.path and index.sha256 are both needed");let i=await t.loadIndex(n.indexPath,{bankSha256:t.bankSha256,bankIds:t.bankIds});if(i.sourceSha256!==n.indexSha256)throw new Error(`the packed index repacks ${i.sourceSha256}, not the ${n.indexSha256} the settings pin`);let u=iP(n.settings.features,i.features);if(u.length>0)throw new Error(`the index was built with other features settings (differing: ${u.join(", ")}); build it again with cardvision build-features`);let l=await t.loadCv(),d=q$(n,l);if(d)throw new Error(d);let c=await t.makeVoter(i.desc);try{return{stage:new wd(n,i,c,l,t.identities),bytes:i.desc.byteLength+i.xy.byteLength,note:oP(i.opencv,l)}}catch(s){throw c.dispose(),s}}var Hn="file:///";function K$(e){let t=new URL(e,Hn).href.slice(Hn.length);try{return decodeURIComponent(t)}catch{return t}}var X$="busy";var ur={full128:{id:"full128",size:128,centerMask:!1,poolVersion:2,crop:"truncate"}};function vd(e){return typeof e=="string"&&Object.prototype.hasOwnProperty.call(ur,e)}function os(e){if(typeof e!="object"||e===null||Array.isArray(e))return null;let t=e;if(!vd(t.id))return null;let n=ur[t.id],i=Object.keys(t);return i.length===Object.keys(n).length&&i.every(l=>n[l]===t[l])?t.id:null}function sP(e,t,n,i,u,l){let d=new Float64Array(i*u*3),c=new Uint32Array(i*u);for(let a=0;a0&&(d[a*3]=d[a*3]/p,d[a*3+1]=d[a*3+1]/p,d[a*3+2]=d[a*3+2]/p)}if(l===1||t>=i&&n>=u||t<=0||n<=0)return d;let s=Z$(n,u),r=Z$(t,i),o=new Float64Array(d.length);for(let a=0;a=0&&!n[l];)l--;let d=u+1;for(;d=0&&(d>=t||u-l<=d-u)?l:d}return i}function xd(e,t,n,i){let u=i.size,l=sP(e,t,n,u,u,i.poolVersion),d=new Float32Array(3*u*u),c=u*u;for(let s=0;s>10&31,s=l&1023;c===0?i[u]=d*s*2**-24:c===31?i[u]=s?NaN:d*(1/0):i[u]=d*(1+s/1024)*2**(c-15)}return i}function uP(e,t){let n;try{n=atob(e)}catch{return null}if(n.length!==t*2)return null;let i=new Uint8Array(n.length);for(let u=0;u0)||!Number.isFinite(u))return!1;for(let l=0;l=Wn.length&&Wn.every((t,n)=>e[n]===t)}function Sd(e){return Td(e)&&e.length>Wn.length?e[Wn.length]:null}function tT(e){let t=e instanceof Uint8Array?e:new Uint8Array(e);if(Sd(t)!==ss||t.lengthtypeof a=="string")||u.entryCount!==d.length)return null;let c=u.artVs;if(c!==void 0&&(!Array.isArray(c)||c.length!==d.length||!c.every(a=>a===null||typeof a=="string")))return null;let s=l.dim,r=Gi+n;if(t.length-r!==d.length*s*2)return null;let o=new Float32Array(d.length*s);for(let a=0;a1?1:e}function as(e,t){let{frameWidth:n,frameHeight:i,names:u}=t;return e.map(l=>{let{aabb:d}=l,c=l.identity?u.get(l.identity):void 0,s=c!==void 0&&l.outline?l.outline:l.rect,r={instanceId:l.instanceId,bounds:{x:Kn(d.x/n),y:Kn(d.y/i),width:Kn(d.width/n),height:Kn(d.height/i)},rotation:(s.angle+45)%90-45,localWidth:Kn(s.w/n),localHeight:Kn(s.h/i),landscape:s.w>s.h,cardId:null,name:null};return l.identity&&c!==void 0&&(r.cardId=l.identity,r.name=c),r})}var dP=["accepted-nms","best-score","detector-score"];function nT(e){return typeof e=="string"&&dP.includes(e)}var Id="accepted-nms",cP=.5,pP="hover_";function rT(e){return e.width*e.height}function hP(e,t){let n=t==="accepted-nms"?e.kept:e.proposals.map((u,l)=>l),i=(u,l)=>{let d=e.proposals[u],c=e.proposals[l];if(t==="detector-score")return d.detectorScore!==c.detectorScore?d.detectorScore{let u=e.proposals[n];return{proposal:n,aabb:u.aabb,rect:u.rect,...u.outline?{outline:u.outline}:{},identity:u.accepted?u.displayId:null,score:u.score,zIndex:i+1}})}var us=class{prev=[];nextId=1;reset(){this.prev=[]}assign(t){let n=new Set,i=t.map((l,d)=>d).sort((l,d)=>t[d].zIndex-t[l].zIndex),u=new Array(t.length);for(let l of i){let d=t[l],c=-1,s=cP;for(let r=0;r=s&&(c<0||o>s)&&(c=r,s=o)}c>=0?(n.add(c),u[l]=this.prev[c].id):u[l]=`${pP}${String(this.nextId++).padStart(4,"0")}`}return this.prev=t.map((l,d)=>({id:u[d],aabb:l.aabb,identity:l.identity})),u}};function Ed(e,t,n,i=u=>u){return as(e.map((l,d)=>({instanceId:t[d],aabb:l.aabb,rect:l.rect,...l.outline?{outline:l.outline}:{},identity:l.identity})),n).map((l,d)=>({...l,zIndex:e[d].zIndex,boxIndex:i(e[d].proposal)}))}var fP="identity-groups";function oT(e){if(typeof e!="object"||e===null)return null;let t=e;if(t.schemaVersion!==1||t.kind!==fP||typeof t.version!="number"||!Number.isInteger(t.version)||!Array.isArray(t.groups)||typeof t.representative!="object"||t.representative===null||Array.isArray(t.representative))return null;let n=new Map,i=[];for(let l of t.groups){if(!Array.isArray(l)||l.length<2)return null;let d=[];for(let c of l){if(typeof c!="string"||c.length===0||n.has(c))return null;n.set(c,i.length),d.push(c)}i.push(d)}let u=new Map;for(let[l,d]of Object.entries(t.representative)){if(typeof d!="string"||l===d)return null;let c=n.get(l);if(c===void 0||n.get(d)!==c)return null;u.set(l,d)}for(let l of i){let d=l.filter(c=>!u.has(c));if(d.length!==1||l.some(c=>u.has(c)&&u.get(c)!==d[0]))return null}return{version:t.version,groups:i,representative:u}}var mP=["name","printing"];function Ad(e){return mP.includes(e)}function sT(e,t){return t==="printing"?e.printingOf??e.id:e.name}function aT(e,t){let n=new Map;for(let i of[...new Set(e)].sort(iT)){let u=t.get(i);if(u===void 0)continue;let l=n.get(u);l?l.push(i):n.set(u,[i])}return[...n.values()].filter(i=>i.length>=2).sort((i,u)=>iT(i[0],u[0]))}function iT(e,t){return et?1:0}var Xn=class{rep=new Map;display;constructor(t=null,n,i="representative"){if(this.display=i,!!t)for(let[u,l]of t.representative)i==="representative"&&n&&!n(l)||this.rep.set(u,l)}of(t){return this.rep.get(t)??t}shown(t){return this.display==="best-printing"?t:this.of(t)}get collapsedCount(){return this.rep.size}};var kd=["webgpu","wasm"];var fe=Math.fround,uT=63/88,gP=128,Tt=90,bP=.2,yP=8,wP=.35,_P=.55,vP=.9,bD=Object.freeze({scales:[.95,1,1.05],maxFacesPerFrame:8,budgetMs:1500}),xP=[1,.8824969025845955,.6065306597126334,.32465246735834974,.1353352832366127,.04393693362340742,.011108996538242306],lT=6,$P=fe(fe(180)/fe(Math.PI)),Pd=[fe(.299),fe(.587),fe(.114)];function TP(e){let t=e-90*Math.ceil((e-45)/90);return t<=-45?45:t}function SP(e,t){let n=e%t;return n!==0&&n<0!=t<0?n+t:n}function IP(e,t){let n=e%t;return n===0?0:n<0?fe(n+t):n}function dT(e){let t=Math.floor(e),n=e-t;return n<.5?t:n>.5?t+1:t%2===0?t:t+1}function OP(e,t){let n=e.length,i=Float32Array.from(e).sort(),u=fe(t),l=fe(fe(fe(n*u)+fe(1+fe(u*-1)))-1),d,c;l>=n-1?d=c=n-1:l<0?d=c=0:(d=Math.floor(l),c=d+1);let s=d===c?0:fe(l-d),r=i[d],o=i[c],a=fe(o-r);return s>=.5?fe(o-fe(a*fe(1-s))):fe(r+fe(a*s))}function cT(e){let{width:t,height:n}=e,i=e.channels??4,u=Math.max(1,Math.ceil(Math.max(n,t)/gP)),l=u===1?n:Math.floor(n/u),d=u===1?t:Math.floor(t/u);if(Math.min(l,d)c[Math.min(l-1,Math.max(0,C))*d+Math.min(d-1,Math.max(0,W))],o=l-2,a=d-2,p=o*a,h=new Float32Array(p),m=new Float32Array(p),g=new Uint8Array(p),v=new Float32Array(p),w=0,y=0;for(let C=0;Cy&&(y=v[F]))}if(y<=0)return 0;let T=new Float32Array(w);for(let C=0,W=0;C=S)||!(W>0))continue;let _=IP(fe(fe(Math.atan2(m[C],h[C]))*$P),90),D=Math.min(Math.trunc(fe(fe(_/90)*Tt)),Tt-1);O[D]=O[D]+W}let A=new Float64Array(Tt);for(let C=0;CA[k]&&(k=C);let I=A[(k-1+Tt)%Tt],R=A[(k+1)%Tt],M=I-2*A[k]+R,X=M!==0?.5*(I-R)/M:0,G=SP((k+.5+X)*90/Tt,90);return TP(G)}var EP=1;function pT(e,t,n){let i=Math.abs(n)*(Math.PI/180),u=Math.cos(i),l=Math.sin(i),d=u*u-l*l;if(d>wP){let s=(e*u-t*l)/d,r=(t*u-e*l)/d;if(s>0&&r>0){let o=Math.min(s,r)/Math.max(s,r);if(o>=_P&&o<=vP)return{w:s,h:r,how:"exact"}}}let c=null;for(let s of[uT,1/uT]){let r=s*u+l,o=s*l+u,a=(e*r+t*o)/(r*r+o*o),p=(e-a*r)**2+(t-a*o)**2;(c===null||pw>=0&&w=0&&yAP(e,c,s,l*r,d*r,u))}}function kP(e,t,n){if(Math.abs(e)!["scales","maxFacesPerFrame","budgetMs"].includes(l));if(n.length>0)return`tiltedRetry has unknown key(s) ${n.sort().join(", ")}`;let i=t.scales;if(!Array.isArray(i)||i.length<1||i.length>5||!i.every(l=>typeof l=="number"&&Number.isFinite(l)&&l>=.5&&l<=1.5))return"tiltedRetry.scales must list 1 to 5 numbers from 0.5 to 1.5";let u=l=>typeof l=="number"&&Number.isInteger(l)&&l>=1;return u(t.maxFacesPerFrame)?u(t.budgetMs)?null:"tiltedRetry.budgetMs must be a whole number above 0":"tiltedRetry.maxFacesPerFrame must be a whole number above 0"}var PP=.3;function CP(e,t){let n=Math.max(0,Math.floor(t.x)),i=Math.max(0,Math.floor(t.y)),u=Math.min(e.width,Math.floor(t.x+t.width)),l=Math.min(e.height,Math.floor(t.y+t.height)),d=Math.max(0,u-n),c=Math.max(0,l-i),s=new Uint8ClampedArray(d*c*4);for(let r=0;r=t.tau&&e.margin>=t.delta}function DP(e,t,n){if(Cr(e,n)||!n.rotationRetry)return{orientation:0,match:e,retried:!1};let i=[e,...t()],u=-1;for(let l=0;li[u].similarity)&&(u=l);if(u<0){u=0;for(let l=1;li[u].similarity&&(u=l)}return{orientation:u,match:i[u],retried:!0}}function gT(e){let t=[],n=[];return e.forEach((i,u)=>{let l=i.tilted!==void 0&&i.tilted.scale!==null;i.tauPassed&&(i.orientation===0&&!l?t:n).push(u)}),[...t,...n]}function bT(e,t){let n=(t??e.map((i,u)=>u)).filter(i=>e[i].tauPassed);return a$(n,i=>e[i].aabb,i=>e[i].score,PP)}async function yT(e,t,n,i,u=Ln,l=null,d=null,c=!1){let s=n.preprocessing,r=n.rule,o=s.size,a=n.dim,p=[],h=[],m=[];for(let j of i){let ee=CP(e,j.aabb),{rgba:ie,width:ue,height:ce}=ee;uen.matchScored(w,ee*a)),T=[];if(r.rotationRetry)for(let j=0;j[j,ee])),O=null;if(T.length>0){let j=new Float32Array(T.length*3*g);for(let ee=0;ee{let ie=S.get(j);if(ie===void 0||O===null)throw new Error(`the turned looks of proposal ${j} were not embedded`);return(ie*3+ee-1)*a},k=h.map((j,ee)=>DP(y[ee],()=>[1,2,3].map(ie=>n.matchScored(O,A(ee,ie))),r)),I=j=>k[j].orientation===0?{embs:w,off:j*a}:{embs:O,off:A(j,k[j].orientation)},R=new Map,M,X,G=!1;if(l)try{let j=performance.now(),ee=[],ie=[],ue=[];for(let ce=0;ce0){let ce=await l.verifyBatch(ue,n.bankIds);ee.forEach((de,xe)=>{let Ge=ce[xe],Ve=ie[xe],nt=k[de].match,Xe=B$(Ve,Ge.candidates,Rr=>n.columnsOf(Rr)),$e=l.reorder?n.lookOf(Xe):n.lookWithin(Xe,n.lookOf(Ve).row),{row:ht,...St}=$e;R.set(de,{match:St,geometric:F$({topId:nt.topId,similarity:nt.similarity,margin:nt.margin,accepted:Cr(nt,r)},Ge)})})}M=performance.now()-j}catch(j){G=!0,X=j,R.clear(),M=void 0}let C=new Map,W,_,D=!1,V=0;if(d)try{let j=performance.now(),{scales:ee,maxFacesPerFrame:ie}=d.settings,ue=de=>R.get(de)?.match??k[de].match,ce=RP(h.map((de,xe)=>({aabb:de.aabb,similarity:k[xe].match.similarity,accepted:Cr(ue(xe),r)})),d.focus??null).slice(0,ie);if(ce.length>0){let de=[],xe=new Float32Array(ce.length*ee.length*g);ce.forEach((Ve,nt)=>{let Xe=m[Ve],{tilt:$e,rects:ht}=hT(e,h[Ve].aabb,{data:Xe.rgba,width:Xe.width,height:Xe.height},ee);de.push($e),ht.forEach((St,Rr)=>{let qt=xd(St.rgba,St.width,St.height,s);xe.set($d(qt,k[Ve].orientation,o),(nt*ee.length+Rr)*g)})}),V=ce.length*ee.length;let Ge=await t.embedBatch(xe,V);ce.forEach((Ve,nt)=>{let Xe=ue(Ve),$e=null;for(let ht=0;ht$e.match.similarity)&&($e={q:ht,match:St})}C.set(Ve,{match:$e?$e.match:null,evidence:{tilt:de[nt],scale:$e?ee[$e.q]:null,before:{topId:Xe.topId,similarity:Xe.similarity,margin:Xe.margin}}})})}W=performance.now()-j}catch(j){D=!0,_=j,C.clear(),V=0,W=void 0}let q=new Map,H=j=>{let ee=q.get(j);if(ee===void 0){let{aabb:ie}=h[j],ue=m[j],{tilt:ce,w:de,h:xe}=fT({data:ue.rgba,width:ue.width,height:ue.height},ie.width,ie.height);ee={cx:ie.x+ie.width/2,cy:ie.y+ie.height/2,w:de,h:xe,angle:ce},q.set(j,ee)}return ee},K=j=>{let ee=[];for(let ie=0;iet!==null&&t.x>=d.x&&t.x<=d.x+d.width&&t.y>=d.y&&t.y<=d.y+d.height,i=e.map((d,c)=>c).filter(d=>!e[d].accepted),u=i.filter(d=>n(e[d].aabb)).sort((d,c)=>e[d].aabb.width*e[d].aabb.height-e[c].aabb.width*e[c].aabb.height),l=i.filter(d=>!n(e[d].aabb)).sort((d,c)=>e[c].similarity-e[d].similarity);return[...u,...l]}var Cd={tau:.62,delta:.05,rotationRetry:!0},ls=-2,ds=class{constructor(t,n=Cd,i=new Xn){this.bank=t;this.rule=n;this.contract=ur[t.preprocessing],this.rowShown=t.ids.map(l=>i.shown(l));let u=new Map;this.rowIdent=new Int32Array(t.ids.length);for(let l=0;lc&&(c=p,s=a)}let r=ls,o=s>=0?this.rowIdent[s]:-1;for(let a=0;ar&&(r=d[a]);return{bestRow:s,best:c,other:r}}matchScored(t,n){let{bestRow:i,best:u,other:l}=this.topAndOther(t,n),d=this.bank.ids[i];return{cardId:u>=this.rule.tau?d:null,similarity:u,topId:d,identity:this.identRep[this.rowIdent[i]],displayId:this.rowShown[i],margin:u-l}}scoreRow(t,n){let{matrix:i,dim:u,ids:l}=this.bank,d=new Float64Array(l.length);for(let c=0;cd);n.sort((l,d)=>t[d]-t[l]);let i=new Set,u=[];for(let l of n){let d=this.bank.ids[l];i.has(d)||(i.add(d),u.push({cardId:d,cosine:t[l]}))}return u}columns=null;columnsOf(t){return this.columns===null&&(this.columns=new Map,this.bank.ids.forEach((n,i)=>{let u=this.columns.get(n);u?u.push(i):this.columns.set(n,[i])})),this.columns.get(t)??[]}bankIdSet=null;get bankIds(){return this.columnsOf(""),this.bankIdSet??=new Set(this.columns.keys()),this.bankIdSet}scored(t,n,i){let u=this.bank.ids[t];return{cardId:n>=this.rule.tau?u:null,similarity:n,topId:u,identity:this.identRep[this.rowIdent[t]],displayId:this.rowShown[t],margin:n-i,row:t}}lookOf(t){let n=0;for(let d=1;dt[n]&&(n=d);let i=this.rowIdent[n],u=ls,l=!1;for(let d=0;du)&&(u=t[d]),l=!0);return this.scored(n,t[n],u)}lookWithin(t,n){let i=this.rowIdent[n],u=-1,l=ls,d=!1;for(let c=0;ct[u])&&(u=c):((!d||t[c]>l)&&(l=t[c]),d=!0);return this.scored(u,t[u],l)}matchPooled(t,n=this.rule.delta){let{bestRow:i,best:u,other:l}=this.topAndOther(t,0),d=u-l;return{cardId:u>=this.rule.tau&&d>=n?this.rowShown[i]:null,similarity:u,margin:d}}};var cs=class{constructor(t=3){this.k=t;if(!Number.isInteger(t)||t<1)throw new RangeError(`k must be a positive integer, got ${t}`)}k;run=0;misses=0;showing=!1;reset(){this.run=0,this.misses=0,this.showing=!1}observe(t){return t?(this.run+=1,this.misses=0,this.run>=this.k&&(this.showing=!0)):(this.run=0,this.misses+=1,this.misses>=this.k&&(this.showing=!1)),{confirmed:this.showing,consecutive:this.run}}};var NP=.4,zP=2,MP=5,BP=2,FP="track_";function jP(e){let t=e[0].length,n=new Float32Array(t);for(let l of e)for(let d=0;d0))return null;for(let l=0;lu));for(let i of this.tracks){let u=-1,l=NP;for(let d of n){let c=Fi(i.detection.aabb,t[d].aabb);c>l&&(l=c,u=d)}if(u>=0){let d=t[u];i.detection=d,i.misses=0,i.age+=1,i.embeddings.push(d.embedding),i.embeddings.length>MP&&i.embeddings.shift(),n.delete(u)}else i.misses+=1}this.tracks=this.tracks.filter(i=>i.misses<=zP);for(let i of n){let u=t[i];this.tracks.push({instanceId:`${FP}${String(this.nextId++).padStart(4,"0")}`,detection:u,embeddings:[u.embedding],misses:0,age:1})}return this.tracks.filter(i=>i.misses===0).map(i=>({instanceId:i.instanceId,detection:i.detection,identity:i.age>=BP?this.identityOf(i):null}))}};var UP=.25,VP=5;function GP(e,t){if(e===null||t===void 0)return!1;let n=t-e;return n<-UP||n>VP}function qP(e){return[e.x,e.y,e.width,e.height]}var hs=class{hoverSelection;detector;embedder;recognizer;names;classNames;faceClass;minCropPx;now;tracker;gate=new cs(3);hoverIds=new us;heldCards=[];lastVideoTimeSec=null;geometric;geometricFailure=null;tiltedRetry;tiltedFailure=null;constructor(t){this.detector=t.detector,this.embedder=t.embedder,this.names=t.names,this.classNames=t.classNames??["face","back"],this.faceClass=t.faceClass??kr,this.minCropPx=t.minCropPx??Ln,this.hoverSelection=t.hoverSelection??Id,this.now=t.now??(()=>performance.now()),this.geometric=t.geometric??null,this.tiltedRetry=t.tiltedRetry??null;let n=new Xn(t.identityGroups??null,i=>this.names.has(i),t.identityDisplay??"best-printing");this.recognizer=new ds(t.bank,t.accept??Cd,n),this.tracker=new ps(i=>this.recognizer.matchPooled(i).cardId)}get size(){return this.recognizer.size}takeGeometricFailure(){let t=this.geometricFailure;return this.geometricFailure=null,t}takeTiltedFailure(){let t=this.tiltedFailure;return this.tiltedFailure=null,t}get tiltedSettings(){return this.tiltedRetry}setTiltedRetry(t){this.tiltedRetry=t,this.tiltedFailure=null}get geometricStage(){return this.geometric}setGeometric(t){this.geometric=t,this.geometricFailure=null}reset(){this.tracker.reset(),this.gate.reset(),this.heldCards=[],this.hoverIds.reset(),this.lastVideoTimeSec=null}async analyze(t,n,i={}){let u=this.now(),l=GP(this.lastVideoTimeSec,n);l&&this.reset(),n!==void 0&&(this.lastVideoTimeSec=n);let d=await this.detector.propose(t),c=this.now()-u,s=d.filter(I=>cd(I.aabb,t.width,t.height,this.minCropPx)).map(I=>d$(I,t)),{faces:r}=c$(s,this.faceClass),o=this.now(),a=this.tiltedRetry&&i.tilted!==!1?{settings:this.tiltedRetry,focus:i.focus??null}:null,p=await yT(t,this.embedder,this.recognizer,r,this.minCropPx,this.geometric,a,i.rotatedOutlines===!0),h=this.now()-o;if(p.tiltedError!==void 0&&this.tiltedRetry&&(this.tiltedFailure={error:p.tiltedError},this.tiltedRetry=null),p.geometricError!==void 0&&this.geometric&&(this.geometricFailure={stage:this.geometric,error:p.geometricError},this.geometric=null),p.proposals.length!==r.length)throw new Error("pipeline: a valid face box was not recognized");let m={frameWidth:t.width,frameHeight:t.height,names:this.names},g=Od(p,this.hoverSelection),v=r.map(I=>s.indexOf(I)),w=Ed(g,this.hoverIds.assign(g),m,I=>v[I]),y=p.base??p,T=y.kept.map(I=>{let R=y.proposals[I];return{aabb:R.aabb,rect:R.rect,cardId:R.topId,embedding:R.embedding,...R.outline?{outline:R.outline}:{}}}),S=this.tracker.update(T),O=T.length>0,A=this.gate.observe(O),k;return A.confirmed?O?(k=as(S.map(I=>({instanceId:I.instanceId,aabb:I.detection.aabb,rect:I.detection.rect,...I.detection.outline?{outline:I.detection.outline}:{},identity:I.identity})),m),this.heldCards=k):k=this.heldCards:(this.heldCards=[],k=[]),{frame:{width:t.width,height:t.height},boxes:this.boxResults(s,r,p),hover:{selection:this.hoverSelection,cards:w},tracks:{shown:A.confirmed,cards:k},discontinuity:l,retried:p.retried,embeddings:p.embeddings,timings:{detectorMs:c,recognizeMs:h,analyzeMs:this.now()-u,...p.geometricMs!==void 0?{geometricMs:p.geometricMs}:{},...p.tiltedMs!==void 0?{tiltedMs:p.tiltedMs}:{}}}}boxResults(t,n,i){let u=new Map(n.map((d,c)=>[d,c])),l=new Set(i.kept);return t.map(d=>{let c=u.get(d),s=null;if(c!==void 0){let r=i.proposals[c],o=r.accepted&&this.names.has(r.displayId)?r.displayId:null;s={topId:r.topId,topName:this.names.get(r.topId)??null,similarity:r.score,margin:r.margin,look:r.orientation,retried:r.retried,tauPassed:r.tauPassed,accepted:r.accepted,cardId:o,name:o===null?null:this.names.get(o)??null,kept:l.has(c),...r.geometric?{geometric:r.geometric}:{},...r.tilted?{tilted:r.tilted}:{}}}return{box:qP(d.aabb),score:d.score,cls:d.cls,className:this.classNames[d.cls]??String(d.cls),face:s}})}};var wT=1,_T="vision-release",HP=[8,16,32];function vT(e,t){return HP.reduce((n,i)=>n+Math.floor(e/i)*Math.floor(t/i),0)}var WP=/^[0-9a-f]{16}$/,KP=/^[0-9a-f]{64}$/,XP=/^\d+\.\d+\.\d+$/,ZP=["schemaVersion","kind","id","game","locale","detector","recognizer","bank","identity","catalog","accept","hover","runtime","tiltedRetry"],xT=["tau","delta","rotationRetry"],Ue=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),ft=e=>typeof e=="number"&&Number.isInteger(e)&&e>0,Wi=e=>typeof e=="number"&&Number.isFinite(e),et=e=>typeof e=="string"&&e.trim()!=="";function YP(e){return!et(e)||e.includes("\\")||/^[A-Za-z][A-Za-z0-9+.-]*:/.test(e)||/[\x00-\x1f\x7f]/.test(e)||/^ | $/.test(e)||e.startsWith("/")?!1:!e.split("/").some(t=>{let n=t.replace(/%2e/gi,".");return n==="."||n===".."})}function qi(e){return!Ue(e)||!YP(e.path)||typeof e.sha256!="string"||!KP.test(e.sha256)||!ft(e.bytes)?null:{path:e.path,sha256:e.sha256,bytes:e.bytes}}function JP(e){let t=e.input,n=e.decode;if(!Ue(t)||!ft(t.width)||!ft(t.height))return"detector.input must give a width and a height";if(!Ue(n))return"detector.decode must be an object";if(!et(n.inputName)||!et(n.outputName))return"detector.decode must name the model's input and output";let i=[["layout","NCHW"],["channels","BGR"],["scale","0-255"],["score","obj * max(cls)"],["class","argmax(cls)"]];for(let[o,a]of i)if(n[o]!==a)return`detector.decode.${o} is not ${JSON.stringify(a)}, the contract this runtime implements`;let u=n.letterbox;if(!Ue(u)||u.width!==t.width||u.height!==t.height)return"detector.decode.letterbox differs from detector.input";if(typeof u.pad!="number"||!Number.isInteger(u.pad)||u.pad<0||u.pad>255)return"detector.decode.letterbox.pad must be a whole number from 0 to 255";let l=[["anchor","top-left"],["resize","pillow-bilinear"],["ratio","min(height / frameHeight, width / frameWidth)"],["size","round(frame * ratio)"]];for(let[o,a]of l)if(u[o]!==a)return`detector.decode.letterbox.${o} is not ${JSON.stringify(a)}`;let d=n.classes;if(!Array.isArray(d)||d.length===0||!d.every(et)||new Set(d).size!==d.length)return"detector.decode.classes must be a list of distinct class names";if(e.faceClass!==kr||d[kr]!=="face")return`detector.faceClass must be ${kr} and that class must be named "face"`;let c=n.output,s=["cx","cy","w","h","obj",...d.map(o=>`cls_${o}`)];if(!Ue(c)||!Array.isArray(c.columns)||c.columns.length!==s.length||!c.columns.every((o,a)=>o===s[a]))return"detector.decode.output.columns must be cx, cy, w, h, obj and one cls_ per class";if(c.anchors!==vT(t.width,t.height))return`detector.decode.output.anchors must be ${vT(t.width,t.height)} for a ${t.width} x ${t.height} input`;if(!Wi(n.threshold)||!(n.threshold>0&&n.threshold<1))return"detector.decode.threshold must be a number between 0 and 1";if(n.threshold<.01)return`detector.decode.threshold ${n.threshold} is below 0.01, the lowest score at which the detector export checks box parity`;let r=n.nms;return!Ue(r)||!Wi(r.iou)||!(r.iou>0&&r.iou<=1)||r.classAgnostic!==!0||r.greedy!==!0?"detector.decode.nms must give an iou above 0 and at most 1, with classAgnostic and greedy both true":ft(n.maxProposals)?ft(e.minCropPx)?{inputName:n.inputName,outputName:n.outputName,letterbox:{width:t.width,height:t.height,pad:u.pad},anchors:c.anchors,classes:d,decode:{threshold:n.threshold,nmsIou:r.iou,maxProposals:n.maxProposals},faceClass:kr,minCropPx:e.minCropPx}:"detector.minCropPx must be a whole number above 0":"detector.decode.maxProposals must be a whole number above 0"}function QP(e){let t=A=>({error:A});if(!Ue(e)||e.schemaVersion!==wT||e.kind!==_T)return t(`not a "${_T}" document of schema version ${wT}`);let n=Object.keys(e).filter(A=>!ZP.includes(A));if(n.length>0)return t(`unknown key(s) ${n.sort().join(", ")}`);if(typeof e.id!="string"||!WP.test(e.id))return t("id must be 16 lowercase hex digits");if(!et(e.game)||!et(e.locale))return t("game and locale must be non-empty strings");let i=e.detector;if(!Ue(i))return t("incomplete release: detector missing (a release is all-or-nothing)");let u=qi(i);if(!u||!et(i.version)||!et(i.weights))return t("detector must pin a file and carry a version and a weights label");if(i.arm!=="yolox")return t(`detector.arm ${JSON.stringify(i.arm)} is not one this runtime implements`);let l=JP(i);if(typeof l=="string")return t(l);let d=e.recognizer;if(!Ue(d))return t("incomplete release: recognizer missing (a release is all-or-nothing)");let c=qi(d);if(!c||!et(d.version)||!et(d.weights)||!ft(d.dim))return t("recognizer must pin a file and carry a version, a weights label and a dim");if(!et(d.inputName)||!et(d.outputName))return t("recognizer must name the model's input and output");let s=os(d.preprocessing);if(s===null)return t(`recognizer.preprocessing is not a contract this runtime implements (known: ${Object.keys(ur).join(", ")})`);let r=e.bank;if(!Ue(r))return t("incomplete release: bank missing (a release is all-or-nothing)");let o=qi(r);if(!o||!ft(r.version)||!ft(r.cardCount)||!ft(r.entryCount))return t("bank must pin a file and carry a version, a cardCount and an entryCount");if(r.aggregation!=="mean")return t('bank.aggregation must be "mean"');let a=e.identity;if(!Ue(a)||!Ad(a.level))return t('identity.level must be "name" or "printing"');let p=a.groups,h=qi(p);if(!h||!Ue(p)||!ft(p.version))return t("incomplete release: identity.groups must pin a file and carry a version");if(a.display!=="best-printing"&&a.display!=="representative")return t(`identity.display ${JSON.stringify(a.display)} is not one of best-printing, representative`);let m=e.catalog,g=qi(m);if(!g||!Ue(m)||!ft(m.cardCount))return t("incomplete release: catalog must pin a file and carry a cardCount");let v=e.accept;if(!Ue(v))return t("incomplete release: accept missing (a release is all-or-nothing)");if(Object.keys(v).some(A=>!xT.includes(A)))return t(`accept has unknown key(s) (expected ${xT.join(", ")})`);if(!Wi(v.tau)||!(v.tau>0&&v.tau<=1))return t("accept.tau must be a number above 0 and at most 1");if(!Wi(v.delta)||!(v.delta>=0&&v.delta<1))return t("accept.delta must be a number from 0 to below 1");if(typeof v.rotationRetry!="boolean")return t("accept.rotationRetry must be true or false");let w=e.hover;if(!Ue(w)||!nT(w.selection))return t("hover.selection is not one of accepted-nms, best-score, detector-score");if(!ft(w.maxAgeMs))return t("hover.maxAgeMs must be a whole number above 0");let y=e.runtime;if(!Ue(y)||typeof y.ortWeb!="string"||!XP.test(y.ortWeb))return t("runtime.ortWeb must be an exact version of the form 1.2.3");if(y.wasmThreads!==1)return t("runtime.wasmThreads must be 1");let T=y.executionProviders;if(!Array.isArray(T)||T.length===0||new Set(T).size!==T.length||!T.every(A=>kd.includes(A)))return t(`runtime.executionProviders must list one or more of ${kd.join(", ")}, each once`);let S=y.analysisWidth;if(!Ue(S)||!ft(S.min)||!ft(S.max)||S.min>S.max)return t("runtime.analysisWidth must give whole numbers min <= max above 0");let O=null;if("tiltedRetry"in e){let A=mT(e.tiltedRetry);if(A!==null)return t(A);let k=e.tiltedRetry;O={scales:[...k.scales],maxFacesPerFrame:k.maxFacesPerFrame,budgetMs:k.budgetMs}}return{release:{id:e.id,game:e.game,locale:e.locale,detector:{...u,arm:"yolox",version:i.version,weights:i.weights,settings:l},recognizer:{...c,version:d.version,weights:d.weights,dim:d.dim,inputName:d.inputName,outputName:d.outputName,preprocessing:s},bank:{...o,version:r.version,cardCount:r.cardCount,entryCount:r.entryCount},identity:{level:a.level,groups:{...h,version:p.version},display:a.display},catalog:{...g,cardCount:m.cardCount},accept:{tau:v.tau,delta:v.delta,rotationRetry:v.rotationRetry},hover:{selection:w.selection,maxAgeMs:w.maxAgeMs},runtime:{ortWeb:y.ortWeb,executionProviders:T,wasmThreads:1,analysisWidth:{min:S.min,max:S.max}},tiltedRetry:O}}}function eC(e){let t=0,n=()=>{throw new Error("not JSON")},i=()=>{for(;t{let c=t;for(e[t]!=='"'&&n(),t++;t=e.length&&n(),t++,JSON.parse(e.slice(c,t))},l=c=>{i();let s=e[t];if(s==="{"){t++;let o=[];if(i(),e[t]==="}")t++;else for(;;){i();let a=u();i(),e[t]!==":"&&n(),t++;let p=l(!1);if(c&&a==="id"||o.push([a,p]),i(),e[t]===",")t++;else if(e[t]==="}"){t++;break}else n()}return o.sort((a,p)=>a[0]p[0]?1:0),`{${o.map(([a,p])=>`${JSON.stringify(a)}:${p}`).join(",")}}`}if(s==="["){t++;let o=[];if(i(),e[t]==="]")t++;else for(;;)if(o.push(l(!1)),i(),e[t]===",")t++;else if(e[t]==="]"){t++;break}else n();return`[${o.join(",")}]`}if(s==='"')return JSON.stringify(u());let r=/^(?:true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/.exec(e.slice(t,t+64));return r?(t+=r[0].length,r[0]):n()};i(),e[t]!=="{"&&n();let d=l(!0);return i(),t!==e.length&&n(),d}async function tC(e,t){let n=new TextEncoder().encode(eC(e));return(await t(n.buffer)).slice(0,16)}async function rC(e){let t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t),n=>n.toString(16).padStart(2,"0")).join("")}var Dd={fetchFn:e=>fetch(e),digestSha256Hex:rC};function nC(e,t){let n=l=>/^(\d+)\.(\d+)\.\d+/.exec(l),i=n(e),u=n(t);return i!==null&&u!==null&&i[1]===u[1]&&i[2]===u[2]}var We=class extends Error{};async function TT(e,t,n){let i;try{i=await e.fetchFn(t)}catch(u){throw new We(`${n}: fetch failed (${String(u)})`)}if(!i.ok)throw new We(`${n}: HTTP ${i.status}`);return i.arrayBuffer()}async function Hi(e,t,n,i){let u=await TT(e,new URL(n.path,t).href,i);if(u.byteLength!==n.bytes)throw new We(`${i}: ${n.path} is ${u.byteLength} bytes, the release says ${n.bytes}`);if(await e.digestSha256Hex(u)!==n.sha256)throw new We(`${i}: sha256 of ${n.path} does not match the release`);return u}function fs(e,t){try{return JSON.parse(new TextDecoder("utf-8",{fatal:!0}).decode(e))}catch{throw new We(`${t}: the file is not JSON`)}}var iC=/^[a-z][a-zA-Z0-9]*$/,oC=["key","label","values","type"],$T=e=>typeof e=="string"?"text":"number",lr=e=>JSON.stringify(e)??String(e);function ST(e,t,n){for(let i=0;i!oC.includes(r));if(l.length>0)return`${u}: unknown key(s) ${l.join(", ")}`;if(typeof i.key!="string"||!iC.test(i.key))return`${u}: "key" ${lr(i.key)} must be a lowercase letter, then letters and digits only`;let d=i.key;if(d==="kind")return`${u}: "key" "kind" is reserved: the card's kind is always the first filter`;if(t.some(r=>r.key===d))return`${u}: duplicate key ${lr(d)}`;if(!et(i.label))return`${u} (${d}): "label" must be a non-empty string`;let c=null;if("type"in i){if(i.type!=="text"&&i.type!=="number")return`${u} (${d}): "type" must be "text" or "number", not ${lr(i.type)}`;c=i.type}let s=null;if("values"in i){if(!Array.isArray(i.values)||i.values.length===0)return`${u} (${d}): "values" must be a non-empty list`;let r=ST(i.values,c,`${u} (${d}) values`);if(r!=="text"&&r!=="number")return r;c=r,s=i.values}t.push({key:d,label:i.label,type:c,values:s})}return t}function aC(e,t,n,i){let u=`card ${n}`;if(!Ue(e))return`${u}: "facets" must be an object of facet key to a value or a list of values`;for(let[l,d]of Object.entries(e)){let c=t.find(o=>o.key===l);if(c===void 0)return`${u}: facets.${l}: the catalog declares no facet ${lr(l)}`;let s=Array.isArray(d)?d:[d];if(s.length===0)return`${u}: facets.${l} is an empty list (leave the key out instead)`;let r=ST(s,c.type,`${u}: facets.${l}`);if(r!=="text"&&r!=="number")return r;c.type=r;for(let o of s){if(c.values!==null&&!c.values.includes(o))return`${u}: facets.${l}: ${lr(o)} is not one of the declared values`;let a=i.get(l)??[];a.includes(o)||a.push(o),i.set(l,a)}}return null}function uC(e){if(!Ue(e)||!et(e.game)||!et(e.locale)||!Array.isArray(e.cards)||e.cards.length===0)return"the file is not a catalog (game, locale and a list of cards)";let t=e.identity===void 0?"name":e.identity;if(!Ad(t))return`"identity" must be "name" or "printing", not ${JSON.stringify(e.identity)}`;let n=e.facets===void 0?[]:sC(e.facets);if(typeof n=="string")return n;let i=new Map,u=new Map,l=[];for(let c of e.cards){if(!Ue(c)||!et(c.id)||!et(c.name))return"every card needs an id, a name and an art path";if(!("art"in c)||c.art!==null&&!et(c.art))return`card ${c.id}: "art" must be an art path (a file path or an http(s) URL), or null for a card without art`;if(u.has(c.id))return`card id ${c.id} appears twice`;let s=c.printingOf??null;if(s!==null&&!et(s))return`card ${c.id}: "printingOf" must be a card id or null`;let r=c.facets===void 0?null:aC(c.facets,n,c.id,i);if(r!==null)return r;if(c.extra!==void 0&&!Ue(c.extra))return`card ${c.id}: "extra" must be an object (any keys and values), not ${lr(c.extra)}`;let o={id:c.id,name:c.name,art:c.art,printingOf:s};c.extra!==void 0&&(o.extra=c.extra),u.set(o.id,o),l.push(o)}if(t==="printing")for(let c of l){if(c.printingOf===null)continue;let s=u.get(c.printingOf);if(s===void 0||s===c)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is not another card`;if(s.printingOf!==null)return`card ${c.id}: "printingOf" names ${c.printingOf}, which is itself a printing of ${s.printingOf}`}let d=n.map(c=>{let s=c.values??[...i.get(c.key)??[]];return c.values===null&&c.type==="number"&&s.sort((r,o)=>r-o),{key:c.key,label:c.label,type:c.type??"text",values:[...s]}});return{game:e.game,locale:e.locale,identity:t,cards:l,facets:d}}async function IT(e,t=Dd,n={}){try{let i=await TT(t,e,"release"),u=QP(fs(i,"release"));if("error"in u)throw new We(`release: ${u.error}`);let l=u.release,d=await tC(new TextDecoder().decode(i),t.digestSha256Hex);if(d!==l.id)throw new We(`release: id ${l.id} is not the hash of the document (which is ${d})`);if(n.ortWeb!==void 0&&!nC(l.runtime.ortWeb,n.ortWeb))throw new We(`release: measured on onnxruntime-web ${l.runtime.ortWeb}, this build runs ${n.ortWeb} (major and minor version must match)`);try{n.onDocument?.([l.detector,l.recognizer,l.bank,l.identity.groups,l.catalog].reduce((M,X)=>M+X.bytes,0))}catch{}let[c,s,r,o,a]=await Promise.all([Hi(t,e,l.detector,"detector"),Hi(t,e,l.recognizer,"recognizer"),Hi(t,e,l.bank,"bank"),Hi(t,e,l.identity.groups,"identity.groups"),Hi(t,e,l.catalog,"catalog")]);try{n.onFetched?.()}catch{}let p=new Uint8Array(r),h=Sd(p);if(h!==null&&h!==ss)throw new We(`bank: the binary bank is of format version ${h}, and this runtime reads version ${ss} only`);let m=Td(p)?tT(p):eT(fs(r,"bank"));if(!m)throw new We("bank: the file is not a bank this runtime can read");let g=[[m.version===l.bank.version,"bank.version differs from the bank file's"],[m.model===l.recognizer.version,`bank: the file's model ${JSON.stringify(m.model)} is not recognizer.version ${JSON.stringify(l.recognizer.version)}`],[m.dim===l.recognizer.dim,"bank: the vector size differs from recognizer.dim"],[m.preprocessing===l.recognizer.preprocessing,"bank: the file's preprocessing differs from recognizer.preprocessing"],[m.ids.length===l.bank.entryCount,"bank.entryCount is not the number of entries in the bank file"],[new Set(m.ids).size===l.bank.cardCount,"bank.cardCount is not the number of distinct card ids in the bank file"],[m.tau===null||m.tau===l.accept.tau,`bank: the file records tau ${m.tau}, accept.tau is ${l.accept.tau}`]];for(let[M,X]of g)if(!M)throw new We(X);let v=uC(fs(a,"catalog"));if(typeof v=="string")throw new We(`catalog: ${v}`);if(v.game!==l.game||v.locale!==l.locale)throw new We("catalog: the file is for another game or locale");if(v.cards.length!==l.catalog.cardCount)throw new We(`catalog.cardCount ${l.catalog.cardCount} is not the file's ${v.cards.length} cards`);let w=new Map(v.cards.map(M=>[M.id,M.name])),y=m.ids.filter(M=>!w.has(M));if(y.length>0)throw new We(`${new Set(y).size} bank id(s) are not in the catalog (e.g. ${y[0]})`);let T=fs(o,"identity.groups"),S=oT(T);if(!S)throw new We("identity.groups: the file is not an identity-groups document");if(S.version!==l.identity.groups.version)throw new We("identity.groups.version differs from the file's");if((Ue(T)&&Ue(T.bank)?T.bank.sha256:void 0)!==l.bank.sha256)throw new We("identity.groups: the file was not made for this release's bank (its bank sha256 differs)");let A=v.identity;if(l.identity.level!==A)throw new We(`identity.level is "${l.identity.level}", and the catalog states the identity rule "${A}": a release is used with the rule its catalog states`);if((Ue(T)&&Ue(T.rule)?T.rule.level:void 0)!==A)throw new We(`identity.groups: the file's groups are not by ${A}`);let I=new Map(v.cards.map(M=>[M.id,sT(M,A)])),R=M=>JSON.stringify(M.map(X=>[...X].sort()).sort((X,G)=>X[0]G[0]?1:0));if(R(S.groups)!==R(aT(m.ids,I)))throw new We(`identity.groups: the groups are not the ones the catalog's ${A==="name"?"names give":"identity rule gives"} for the bank's ids`);return{ok:!0,data:{release:l,detectorBytes:new Uint8Array(c),recognizerBytes:new Uint8Array(s),bank:m,identityGroups:S,cards:v.cards,names:w}}}catch(i){return{ok:!1,reason:i instanceof We?i.message:`unexpected: ${String(i)}`}}}async function lC(e){for(let t of e)try{await t?.release?.()}catch{}}async function OT(e,t,n=e.release.runtime.executionProviders){let{release:i}=e,u=null,l=null,d=async c=>(await lC([u,l]),{error:c});try{if(n.length===0)return await d("no execution provider to try");let c=i.recognizer;return u=await t.embedder(e.recognizerBytes,{inputName:c.inputName,outputName:c.outputName,inputSize:ur[c.preprocessing].size,dim:c.dim},n),u?(l=await t.detector(e.detectorBytes,i.detector.settings,n),l?{pipeline:new hs({detector:l,embedder:u,bank:e.bank,accept:i.accept,identityGroups:e.identityGroups,identityDisplay:i.identity.display,names:e.names,hoverSelection:i.hover.selection,classNames:i.detector.settings.classes,faceClass:i.detector.settings.faceClass,minCropPx:i.detector.settings.minCropPx,tiltedRetry:i.tiltedRetry}),embedder:u,detector:l,providers:[...n]}:await d("detector: the model did not initialize on any execution provider, or is not the model the release describes")):await d("recognizer: the model did not initialize on any execution provider, or is not the model the release describes")}catch(c){return await d(`unexpected while building: ${String(c)}`)}}var Zn=class extends Error{constructor(n){super(`the release could not be loaded: ${n}`);this.reason=n;this.name="ReleaseError"}reason};function dC(e){return async t=>{if(!t.startsWith(Hn))throw new Error(`${t} is not one of the release's files`);let n=K$(t.slice(Hn.length)),i=Object.hasOwn(e,n)?e[n]:void 0;if(i===void 0)throw new Error(`no file ${n} among the release's files`);return{ok:!0,status:200,arrayBuffer:()=>i.arrayBuffer()}}}var Dr=e=>Math.round(e);function cC(e){let t=(n,i)=>{let u=performance.now()-i;return{createMs:Dr(n.createMs),firstRunMs:Dr(n.firstRunMs),failedProvidersMs:Dr(Math.max(0,u-n.createMs-n.firstRunMs)),totalMs:Dr(u)}};return{embedder:async(n,i,u)=>{let l=performance.now(),d=await rs.create(n,i,u);return d&&(e.recognizer=t(d.setup,l)),d},detector:async(n,i,u)=>{let l=performance.now(),d=await ts.create(n,i,u);return d&&(e.detector=t(d.setup,l)),d}}}async function ET(e,t={}){let n=t.files?Hn:typeof location<"u"?location.href:void 0,i=new URL(e,n).href,u=g=>{try{t.onProgress?.(g)}catch{}},l=performance.now();u({phase:"downloading",bytes:null});let d={...Dd,...t.deps,...t.files?{fetchFn:dC(t.files)}:{}},c=null,s=await IT(i,d,{ortWeb:o$(),onFetched:()=>{c=performance.now()},...t.onProgress?{onDocument:g=>u({phase:"downloading",bytes:g})}:{}});if(!s.ok)throw new Zn(s.reason);let r=performance.now(),o=(c??r)-l,a=r-(c??r);u({phase:"preparing"});let{data:p}=s;s$({wasmThreads:p.release.runtime.wasmThreads,...t.wasmPaths!==void 0?{wasmPaths:t.wasmPaths}:{}}),ld();let h={},m=await OT(p,cC(h),t.providers??p.release.runtime.executionProviders);if("error"in m){let g=ld();throw new Zn(g.length>0?`${m.error} (${g.join("; ")})`:m.error)}return{release:p.release,pipeline:m.pipeline,backend:{detector:m.detector.ep,recognizer:m.embedder.ep},cards:p.cards,names:p.names,stageInputs:{bankSha256:p.release.bank.sha256,bankIds:[...new Set(p.bank.ids)],identities:H$(p.cards,p.release.identity.level,p.identityGroups)},...h.recognizer&&h.detector?{timings:{startedAtMs:Dr(l),filesMs:Dr(o),readMs:Dr(a),recognizer:h.recognizer,detector:h.detector,totalMs:Dr(performance.now()-l)}}:{},dispose:async()=>{await m.embedder.release?.(),await m.detector.release?.()}}}async function AT(e,t,n){let i=typeof location<"u"?location.href:void 0,u=new URL(t,i).href,l=n.fetchText??(async d=>{let c=await fetch(d);if(!c.ok)throw new Error(`${d}: HTTP ${c.status}`);return c.text()});return W$(u,{...n,fetchText:l,...e.stageInputs})}var Rd=e=>e instanceof Error?e.message:String(e);function kT(e){let t=e.now??(()=>performance.now()),n=e.watchdog??new Vi,i=null,u=!1,l=!1,d=null,c=!1,s=!0,r=null,o=!0,a=!1;function p(k){a||(a=!0,i?.pipeline.setTiltedRetry(null),e.post({kind:"tilted-status",state:"off",reason:k}))}function h(k,I=null,R=null,M=null){e.post({kind:"geometric-status",state:k,reason:I,bytes:R,...M!==null?{note:M}:{}})}function m(){if(!(l||!i)){if(c){let k=i.pipeline.geometricStage;k&&(i.pipeline.setGeometric(null),k.dispose()),d?.dispose(),d=null;return}d!==null&&(i.pipeline.setGeometric(d),d=null)}}function g(k){c||(c=!0,m(),h("off",k))}async function v(k,I,R,M=!1){h("loading");let X=null,G=!1;try{if(X=await e.voteDevice(),X===null){c=!0,h("off","no-webgpu");return}X.lost.then(D=>{D.reason!=="destroyed"&&g(`device-lost: ${D.message||D.reason}`)});let{stage:C,bytes:W,note:_}=await e.openStage(k,I,X,R);if(G=!0,c){C.dispose();return}C.debug=M,d=C,m(),h("ready",null,W,_)}catch(C){X!==null&&!G&&e.releaseDevice(X),c||(c=!0,h("off",`load-failed: ${Rd(C)}`))}}function w(k){if(!i)return;y(k);let I=i.pipeline.takeGeometricFailure();if(I){I.stage.dispose(),g(`runtime: ${Rd(I.error)}`);return}let R=k.timings.geometricMs;R===void 0||!s||n.observe(R)&&g(`watchdog: the stage took over ${n.limitMs} ms of a frame on ${n.frames} frames in a row`)}function y(k){if(!i||a)return;let I=i.pipeline.takeTiltedFailure();if(I){p(`runtime: ${Rd(I.error)}`);return}let R=k.timings.tiltedMs;R===void 0||!o||r===null||r.observe(R)&&p(`watchdog: the tilted retry took over ${r.limitMs} ms of a frame on ${r.frames} frames in a row`)}function T(k){return{kind:"release-status",ok:!1,error:k,release:null,cards:[]}}function S(k){if(u)return Promise.resolve();u=!0,s=k.geometricWatchdog!==!1,o=k.tiltedWatchdog!==!1;let I=k.wasmPaths??e.wasmPaths;return e.loadRelease(k.releaseUrl,{wasmPaths:I,...k.providers?{providers:k.providers}:{},...k.files?{files:k.files}:{},onProgress:R=>e.post({kind:"release-progress",phase:R.phase,bytes:R.phase==="downloading"?R.bytes:null})}).then(async R=>{i=R;let M=R.release;M.tiltedRetry&&(r=e.tiltedWatchdog??new Vi(M.tiltedRetry.budgetMs)),e.post({kind:"release-status",ok:!0,error:null,release:{id:M.id,game:M.game,locale:M.locale,detector:{version:M.detector.version,ep:R.backend.detector},recognizer:{version:M.recognizer.version,ep:R.backend.recognizer},bankCards:M.bank.cardCount,catalogCards:M.catalog.cardCount,identityLevel:M.identity.level,decode:{...M.detector.settings.decode,minCropPx:M.detector.settings.minCropPx},accept:{...M.accept},hoverSelection:M.hover.selection,hoverMaxAgeMs:M.hover.maxAgeMs,analysisWidth:M.runtime.analysisWidth,ortWeb:M.runtime.ortWeb,...R.timings?{loadTimings:R.timings}:{},tiltedRetry:M.tiltedRetry?{...M.tiltedRetry,scales:[...M.tiltedRetry.scales]}:null},cards:R.cards}),k.geometricUrl!==void 0&&await v(R,k.geometricUrl,k.geometricManifestSha256,k.geometricDebug===!0)}).catch(R=>{i||e.post(T(R instanceof Zn?R.reason:`unexpected error while loading (${String(R)})`))})}function O(k,I){let{envelope:R,error:M,videoTimeSec:X,width:G,height:C,t0:W}=I;return{kind:"frame-result",envelope:R,...M!==void 0?{error:M}:{},videoTimeSec:X,width:G,height:C,workerMs:+(t()-W).toFixed(2),analysis:k}}async function A(k){let{bitmap:I,videoTimeSec:R,envelope:M,tilted:X,focus:G,rotatedOutlines:C}=k,W=t(),{width:_,height:D}=I,V=q=>e.post(O(null,{envelope:M,error:q,videoTimeSec:R,width:_,height:D,t0:W}));if(l){I.close(),V(X$);return}l=!0;try{if(!i){V("no-release");return}let q=e.readBitmap(I);if(!q){V("no-2d-context");return}let H=await i.pipeline.analyze({data:q.data,width:q.width,height:q.height},R,{...X!==void 0?{tilted:X}:{},...G!==void 0?{focus:G}:{},...C===!0?{rotatedOutlines:C}:{}});w(H),e.post(O(H,{envelope:M,videoTimeSec:R,width:q.width,height:q.height,t0:W}))}catch(q){V(`analysis-failed: ${String(q)}`)}finally{I.close(),l=!1,m()}}return{async handle(k){k.kind==="ping"?e.post({kind:"pong"}):k.kind==="load"?await S(k):k.kind==="frame"&&await A(k)}}}var pC=new URL("./opencv.mjs",import.meta.url).href,Ki=null,ms=null;function hC(e){let{width:t,height:n}=e;return(!Ki||Ki.width!==t||Ki.height!==n)&&(Ki=new OffscreenCanvas(t,n),ms=Ki.getContext("2d",{willReadFrequently:!0})),ms?(ms.drawImage(e,0,0),ms.getImageData(0,0,t,n)):null}async function fC(){let e=navigator.gpu;if(!e)return null;let t=await e.requestAdapter();return t?t.requestDevice({requiredLimits:Cp(t)}):null}var mC=kT({post:e=>self.postMessage(e),loadRelease:ET,wasmPaths:new URL("./",import.meta.url).href,voteDevice:fC,openStage:(e,t,n,i)=>AT(e,t,{loadIndex:(u,l)=>Ep(u,{...l,...i!==void 0?{manifestSha256:i}:{}}),loadCv:()=>Ap(pC),makeVoter:async u=>{let l=await Dp(n,u);return{knn2:d=>l.knn2(d),dispose:()=>{l.dispose(),n.destroy()}}}}),releaseDevice:e=>e.destroy(),readBitmap:hC});self.onmessage=e=>{mC.handle(e.data)}; /*! For license information please see worker.js.LEGAL.txt */ From e19d9d4096fca14bb4d62b2da1123ee1b8f9750e Mon Sep 17 00:00:00 2001 From: RDCLder Date: Mon, 9 Nov 2026 23:31:25 -0600 Subject: [PATCH 2/2] Card updates on cardvision 0.10.0: pin, verdicts in the docs, smoke wait cdn/requirements.txt pins cardvision[cdn]==0.10.0. MAINTAINING.md (New cards, step 6) and cdn/SETTINGS.md give publish-release its refresh verdicts (--verdict-r, --verdict-n) or --no-refresh-gate with a reason, which 0.10.0 requires with --config. test/smoke/remote-smoke.mjs waits up to 2 minutes for the first analysis: with a pointer release to download and load, Chrome for Testing under Rosetta took 50 to 70 s, past the old 20 s. Signed-off-by: RDCLder --- MAINTAINING.md | 7 ++++++- cdn/SETTINGS.md | 10 ++++++++-- cdn/requirements.txt | 2 +- test/smoke/remote-smoke.mjs | 4 +++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/MAINTAINING.md b/MAINTAINING.md index a5150a8..edaca93 100644 --- a/MAINTAINING.md +++ b/MAINTAINING.md @@ -47,7 +47,12 @@ with `cardvision refresh` for the checks and `cardvision cdn` for the host ([cdn cards against Konami's database in each language before publishing. 6. **Publish.** The new art (`cardvision cdn publish --catalog … --images …`), then the release: `cardvision cdn publish-release --release --min-version 0.3.0 --data-file --config - cdn/config.json --set-pointer` (try it with `--dry-run` before). It uploads the release folder, verifies it + cdn/config.json --set-pointer --verdict-r --verdict-n ` (try it with + `--dry-run` before). The two verdicts are step 3's files: each must be a PASS of its own gate, not a + rehearsal, under the same rule, or the tool refuses before anything is fetched or written (cardvision + 0.10.0 and later). An update with no new card has no new-card verdict: it takes + `--no-refresh-gate "official renames, no new cards"` in place of both, after the regression + check has still been run and its verdict kept. It uploads the release folder, verifies it live, publishes the details file, and moves the pointer in `config.json` last. Publish each language's file with `cardvision cdn publish --data-file --config cdn/config.json` after setting its `details` entry. Commit the changed diff --git a/cdn/SETTINGS.md b/cdn/SETTINGS.md index c68ea3d..6323ccb 100644 --- a/cdn/SETTINGS.md +++ b/cdn/SETTINGS.md @@ -237,8 +237,14 @@ compressed (the bank and catalog are most of a card-only update). ### Publishing a release `cardvision cdn publish-release --release --min-version 0.3.0 [--data-file F] -[--config cdn/config.json --set-pointer] [--sessions] [--dry-run]`: - +[--config cdn/config.json --set-pointer (--verdict-r R --verdict-n N | --no-refresh-gate "REASON")] +[--sessions] [--dry-run]`: + +0. **Refresh gate** (with `--config`, cardvision 0.10.0 and later): both refresh verdicts, each a + PASS of its own gate with `rehearsal` false under one rule, or `--no-refresh-gate` with the reason + (a release that is not a gated card update, such as one that renames cards and adds none, or + one with new models). Otherwise the run is refused before anything is fetched or written, a dry run included. + The log names each verdict file with its SHA-256 (MAINTAINING.md, "New cards"). 1. **Seal:** the document parses, its id is the folder's name, every pinned file is a flat name present with its size and SHA-256, and `release-legal/` names this release (`NOTICE`'s first line, as for a build); then `vendor/cardvision-browser/node/verify-release.mjs`, the vendored runtime's own diff --git a/cdn/requirements.txt b/cdn/requirements.txt index 4065663..7285e52 100644 --- a/cdn/requirements.txt +++ b/cdn/requirements.txt @@ -1,2 +1,2 @@ # The publishing tool, `cardvision cdn` (the toolkit's docs/cdn.md), with boto3 for the uploads. -cardvision[cdn]==0.6.0rc2 +cardvision[cdn]==0.10.0 diff --git a/test/smoke/remote-smoke.mjs b/test/smoke/remote-smoke.mjs index 1d8183a..7f6d34c 100644 --- a/test/smoke/remote-smoke.mjs +++ b/test/smoke/remote-smoke.mjs @@ -283,7 +283,9 @@ async function hoverPage(tab, url) { const cx = rect.left + rect.width / 2; const cy = rect.top + rect.height / 2; await tab.mouse.move(cx, cy); - for (let i = 0; i < 80; i += 1) { + // Up to 2 minutes for the first analysis: a pointer release is downloaded and loaded first, and + // Chrome for Testing under Rosetta took 50-70 s on 2026-11-10 (20 s was too short). + for (let i = 0; i < 480; i += 1) { await tab.mouse.move(cx + (i % 2 ? 3 : -3), cy, { steps: 2 }); await sleep(250); const s = await computeState(tab);