CoolFace
Apppublic

shreyask/KittenTTS-WebGPU

sourceHugging Faceapache-2.0updated 6mo agoView on Hugging Face
2likes
ort.bundle.min-DL658BJE.js2839 linesDownload Raw Back to assets
1var e=(e=>typeof require<`u`?require:typeof Proxy<`u`?new Proxy(e,{get:(e,t)=>(typeof require<`u`?require:e)[t]}):e)(function(e){if(typeof require<`u`)return require.apply(this,arguments);throw Error('Calling `require` for "'+e+"\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.")}),t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.prototype.hasOwnProperty,a=(t=>typeof e<`u`?e:typeof Proxy<`u`?new Proxy(t,{get:(t,n)=>(typeof e<`u`?e:t)[n]}):t)(function(t){if(typeof e<`u`)return e.apply(this,arguments);throw Error(`Dynamic require of "`+t+`" is not supported`)}),o=(e,t)=>()=>(e&&(t=e(e=0)),t),s=(e,n)=>{for(var r in n)t(e,r,{get:n[r],enumerable:!0})},c=(e,a,o,s)=>{if(a&&typeof a==`object`||typeof a==`function`)for(let c of r(a))!i.call(e,c)&&c!==o&&t(e,c,{get:()=>a[c],enumerable:!(s=n(a,c))||s.enumerable});return e},l=e=>c(t({},`__esModule`,{value:!0}),e),u,d,f,p,m,h=o(()=>{u=new Map,d=[],f=(e,t,n)=>{if(t&&typeof t.init==`function`&&typeof t.createInferenceSessionHandler==`function`){let r=u.get(e);if(r===void 0)u.set(e,{backend:t,priority:n});else{if(r.priority>n)return;if(r.priority===n&&r.backend!==t)throw Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){let t=d.indexOf(e);t!==-1&&d.splice(t,1);for(let t=0;t<d.length;t++)if(u.get(d[t]).priority<=n){d.splice(t,0,e);return}d.push(e)}return}throw TypeError(`not a valid backend`)},p=async e=>{let t=u.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(e){return n||(t.error=`${e}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},m=async e=>{let t=e.executionProviders||[],n=t.map(e=>typeof e==`string`?e:e.name),r=n.length===0?d:n,i,a=[],o=new Set;for(let e of r){let t=await p(e);typeof t==`string`?a.push({name:e,err:t}):(i||=t,i===t&&o.add(e))}if(!i)throw Error(`no available backend found. ERR: ${a.map(e=>`[${e.name}] ${e.err}`).join(`, `)}`);for(let{name:e,err:t}of a)n.includes(e)&&console.warn(`removing requested execution provider "${e}" from session options because it is not available: ${t}`);let s=t.filter(e=>o.has(typeof e==`string`?e:e.name));return[i,new Proxy(e,{get:(e,t)=>t===`executionProviders`?s:Reflect.get(e,t)})]}}),g=o(()=>{h()}),_,v=o(()=>{_=`1.24.0-dev.20251116-b39e144322`}),y,b,x=o(()=>{v(),y=`warning`,b={wasm:{},webgl:{},webgpu:{},versions:{common:_},set logLevel(e){if(e!==void 0){if(typeof e!=`string`||[`verbose`,`info`,`warning`,`error`,`fatal`].indexOf(e)===-1)throw Error(`Unsupported logging level: ${e}`);y=e}},get logLevel(){return y}},Object.defineProperty(b,`logLevel`,{enumerable:!0})}),S,C=o(()=>{x(),S=b}),w,ee,T=o(()=>{w=(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 r=n.getContext(`2d`);if(r!=null){let i,a;t?.tensorLayout!==void 0&&t.tensorLayout===`NHWC`?(i=e.dims[2],a=e.dims[3]):(i=e.dims[3],a=e.dims[2]);let o=t?.format===void 0?`RGB`:t.format,s=t?.norm,c,l;s===void 0||s.mean===void 0?c=[255,255,255,255]:typeof s.mean==`number`?c=[s.mean,s.mean,s.mean,s.mean]:(c=[s.mean[0],s.mean[1],s.mean[2],0],s.mean[3]!==void 0&&(c[3]=s.mean[3])),s===void 0||s.bias===void 0?l=[0,0,0,0]:typeof s.bias==`number`?l=[s.bias,s.bias,s.bias,s.bias]:(l=[s.bias[0],s.bias[1],s.bias[2],0],s.bias[3]!==void 0&&(l[3]=s.bias[3]));let u=a*i,d=0,f=u,p=u*2,m=-1;o===`RGBA`?(d=0,f=u,p=u*2,m=u*3):o===`RGB`?(d=0,f=u,p=u*2):o===`RBG`&&(d=0,p=u,f=u*2);for(let t=0;t<a;t++)for(let n=0;n<i;n++){let i=(e.data[d++]-l[0])*c[0],a=(e.data[f++]-l[1])*c[1],o=(e.data[p++]-l[2])*c[2],s=m===-1?255:(e.data[m++]-l[3])*c[3];r.fillStyle=`rgba(`+i+`,`+a+`,`+o+`,`+s+`)`,r.fillRect(n,t,1,1)}if(`toDataURL`in n)return n.toDataURL();throw Error(`toDataURL is not supported`)}else throw Error(`Can not access image data`)},ee=(e,t)=>{let n=typeof document<`u`?document.createElement(`canvas`).getContext(`2d`):new OffscreenCanvas(1,1).getContext(`2d`),r;if(n!=null){let i,a,o;t?.tensorLayout!==void 0&&t.tensorLayout===`NHWC`?(i=e.dims[2],a=e.dims[1],o=e.dims[3]):(i=e.dims[3],a=e.dims[2],o=e.dims[1]);let s=t!==void 0&&t.format!==void 0?t.format:`RGB`,c=t?.norm,l,u;c===void 0||c.mean===void 0?l=[255,255,255,255]:typeof c.mean==`number`?l=[c.mean,c.mean,c.mean,c.mean]:(l=[c.mean[0],c.mean[1],c.mean[2],255],c.mean[3]!==void 0&&(l[3]=c.mean[3])),c===void 0||c.bias===void 0?u=[0,0,0,0]:typeof c.bias==`number`?u=[c.bias,c.bias,c.bias,c.bias]:(u=[c.bias[0],c.bias[1],c.bias[2],0],c.bias[3]!==void 0&&(u[3]=c.bias[3]));let d=a*i;if(t!==void 0&&(t.format!==void 0&&o===4&&t.format!==`RGBA`||o===3&&t.format!==`RGB`&&t.format!==`BGR`))throw Error(`Tensor format doesn't match input tensor dims`);let f=0,p=1,m=2,h=3,g=0,_=d,v=d*2,y=-1;s===`RGBA`?(g=0,_=d,v=d*2,y=d*3):s===`RGB`?(g=0,_=d,v=d*2):s===`RBG`&&(g=0,v=d,_=d*2),r=n.createImageData(i,a);for(let t=0;t<a*i;f+=4,p+=4,m+=4,h+=4,t++)r.data[f]=(e.data[g++]-u[0])*l[0],r.data[p]=(e.data[_++]-u[1])*l[1],r.data[m]=(e.data[v++]-u[2])*l[2],r.data[h]=y===-1?255:(e.data[y++]-u[3])*l[3]}else throw Error(`Can not access image data`);return r}}),E,D,O,k,A,te,ne=o(()=>{fe(),E=(e,t)=>{if(e===void 0)throw Error(`Image buffer must be defined`);if(t.height===void 0||t.width===void 0)throw Error(`Image height and width must be defined`);if(t.tensorLayout===`NHWC`)throw Error(`NHWC Tensor layout is not supported yet`);let{height:n,width:r}=t,i=t.norm??{mean:255,bias:0},a,o;a=typeof i.mean==`number`?[i.mean,i.mean,i.mean,i.mean]:[i.mean[0],i.mean[1],i.mean[2],i.mean[3]??255],o=typeof i.bias==`number`?[i.bias,i.bias,i.bias,i.bias]:[i.bias[0],i.bias[1],i.bias[2],i.bias[3]??0];let s=t.format===void 0?`RGBA`:t.format,c=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:`RGB`,l=n*r,u=c===`RGBA`?new Float32Array(l*4):new Float32Array(l*3),d=4,f=0,p=1,m=2,h=3,g=0,_=l,v=l*2,y=-1;s===`RGB`&&(d=3,f=0,p=1,m=2,h=-1),c===`RGBA`?y=l*3:c===`RBG`?(g=0,v=l,_=l*2):c===`BGR`&&(v=0,_=l,g=l*2);for(let t=0;t<l;t++,f+=d,m+=d,p+=d,h+=d)u[g++]=(e[f]+o[0])/a[0],u[_++]=(e[p]+o[1])/a[1],u[v++]=(e[m]+o[2])/a[2],y!==-1&&h!==-1&&(u[y++]=(e[h]+o[3])/a[3]);return c===`RGBA`?new de(`float32`,u,[1,4,n,r]):new de(`float32`,u,[1,3,n,r])},D=async(e,t)=>{let n=typeof HTMLImageElement<`u`&&e instanceof HTMLImageElement,r=typeof ImageData<`u`&&e instanceof ImageData,i=typeof ImageBitmap<`u`&&e instanceof ImageBitmap,a=typeof e==`string`,o,s=t??{},c=()=>{if(typeof document<`u`)return document.createElement(`canvas`);if(typeof OffscreenCanvas<`u`)return new OffscreenCanvas(1,1);throw Error(`Canvas is not supported`)},l=e=>typeof HTMLCanvasElement<`u`&&e instanceof HTMLCanvasElement||e instanceof OffscreenCanvas?e.getContext(`2d`):null;if(n){let n=c();n.width=e.width,n.height=e.height;let r=l(n);if(r!=null){let n=e.height,i=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(n=t.resizedHeight,i=t.resizedWidth),t!==void 0){if(s=t,t.tensorFormat!==void 0)throw Error(`Image input config format must be RGBA for HTMLImageElement`);s.tensorFormat=`RGBA`,s.height=n,s.width=i}else s.tensorFormat=`RGBA`,s.height=n,s.width=i;r.drawImage(e,0,0),o=r.getImageData(0,0,i,n).data}else throw Error(`Can not access image data`)}else if(r){let n,r;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(n=t.resizedHeight,r=t.resizedWidth):(n=e.height,r=e.width),t!==void 0&&(s=t),s.format=`RGBA`,s.height=n,s.width=r,t!==void 0){let t=c();t.width=r,t.height=n;let i=l(t);if(i!=null)i.putImageData(e,0,0),o=i.getImageData(0,0,r,n).data;else throw Error(`Can not access image data`)}else o=e.data}else if(i){if(t===void 0)throw Error(`Please provide image config with format for Imagebitmap`);let n=c();n.width=e.width,n.height=e.height;let r=l(n);if(r!=null){let t=e.height,n=e.width;return r.drawImage(e,0,0,n,t),o=r.getImageData(0,0,n,t).data,s.height=t,s.width=n,E(o,s)}else throw Error(`Can not access image data`)}else{if(a)return new Promise((t,n)=>{let r=c(),i=l(r);if(!e||!i)return n();let a=new Image;a.crossOrigin=`Anonymous`,a.src=e,a.onload=()=>{r.width=a.width,r.height=a.height,i.drawImage(a,0,0,r.width,r.height);let e=i.getImageData(0,0,r.width,r.height);s.height=r.height,s.width=r.width,t(E(e.data,s))}});throw Error(`Input data provided is not supported - aborted tensor creation`)}if(o!==void 0)return E(o,s);throw Error(`Input data provided is not supported - aborted tensor creation`)},O=(e,t)=>{let{width:n,height:r,download:i,dispose:a}=t;return new de({location:`texture`,type:`float32`,texture:e,dims:[1,r,n,4],download:i,dispose:a})},k=(e,t)=>{let{dataType:n,dims:r,download:i,dispose:a}=t;return new de({location:`gpu-buffer`,type:n??`float32`,gpuBuffer:e,dims:r,download:i,dispose:a})},A=(e,t)=>{let{dataType:n,dims:r,download:i,dispose:a}=t;return new de({location:`ml-tensor`,type:n??`float32`,mlTensor:e,dims:r,download:i,dispose:a})},te=(e,t,n)=>new de({location:`cpu-pinned`,type:e,data:t,dims:n??[t.length]})}),re,ie,ae,oe,se=o(()=>{re=new Map([[`float32`,Float32Array],[`uint8`,Uint8Array],[`int8`,Int8Array],[`uint16`,Uint16Array],[`int16`,Int16Array],[`int32`,Int32Array],[`bool`,Uint8Array],[`float64`,Float64Array],[`uint32`,Uint32Array],[`int4`,Uint8Array],[`uint4`,Uint8Array]]),ie=new Map([[Float32Array,`float32`],[Uint8Array,`uint8`],[Int8Array,`int8`],[Uint16Array,`uint16`],[Int16Array,`int16`],[Int32Array,`int32`],[Float64Array,`float64`],[Uint32Array,`uint32`]]),ae=!1,oe=()=>{if(!ae){ae=!0;let e=typeof BigInt64Array<`u`&&BigInt64Array.from,t=typeof BigUint64Array<`u`&&BigUint64Array.from,n=globalThis.Float16Array,r=typeof n<`u`&&n.from;e&&(re.set(`int64`,BigInt64Array),ie.set(BigInt64Array,`int64`)),t&&(re.set(`uint64`,BigUint64Array),ie.set(BigUint64Array,`uint64`)),r?(re.set(`float16`,n),ie.set(n,`float16`)):re.set(`float16`,Uint16Array)}}}),ce,le,ue=o(()=>{fe(),ce=e=>{let t=1;for(let n=0;n<e.length;n++){let r=e[n];if(typeof r!=`number`||!Number.isSafeInteger(r))throw TypeError(`dims[${n}] must be an integer, got: ${r}`);if(r<0)throw RangeError(`dims[${n}] must be a non-negative integer, got: ${r}`);t*=r}return t},le=(e,t)=>{switch(e.location){case`cpu`:return new de(e.type,e.data,t);case`cpu-pinned`:return new de({location:`cpu-pinned`,data:e.data,type:e.type,dims:t});case`texture`:return new de({location:`texture`,texture:e.texture,type:e.type,dims:t});case`gpu-buffer`:return new de({location:`gpu-buffer`,gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case`ml-tensor`:return new de({location:`ml-tensor`,mlTensor:e.mlTensor,type:e.type,dims:t});default:throw Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),de,fe=o(()=>{T(),ne(),se(),ue(),de=class{constructor(e,t,n){oe();let r,i;if(typeof e==`object`&&`location`in e)switch(this.dataLocation=e.location,r=e.type,i=e.dims,e.location){case`cpu-pinned`:{let t=re.get(r);if(!t)throw TypeError(`unsupported type "${r}" to create tensor from pinned buffer`);if(!(e.data instanceof t))throw TypeError(`buffer should be of type ${t.name}`);this.cpuData=e.data;break}case`texture`:if(r!==`float32`)throw TypeError(`unsupported type "${r}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break;case`gpu-buffer`:if(r!==`float32`&&r!==`float16`&&r!==`int32`&&r!==`int64`&&r!==`uint32`&&r!==`uint8`&&r!==`bool`&&r!==`uint4`&&r!==`int4`)throw TypeError(`unsupported type "${r}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break;case`ml-tensor`:if(r!==`float32`&&r!==`float16`&&r!==`int32`&&r!==`int64`&&r!==`uint32`&&r!==`uint64`&&r!==`int8`&&r!==`uint8`&&r!==`bool`&&r!==`uint4`&&r!==`int4`)throw TypeError(`unsupported type "${r}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break;default:throw Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let a,o;if(typeof e==`string`)if(r=e,o=n,e===`string`){if(!Array.isArray(t))throw TypeError(`A string tensor's data must be a string array.`);a=t}else{let n=re.get(e);if(n===void 0)throw TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e===`float16`&&n===Uint16Array||e===`uint4`||e===`int4`)throw TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${n.name} as data.`);a=e===`uint64`||e===`int64`?n.from(t,BigInt):n.from(t)}else if(t instanceof n)a=t;else if(t instanceof Uint8ClampedArray)if(e===`uint8`)a=Uint8Array.from(t);else throw TypeError(`A Uint8ClampedArray tensor's data must be type of uint8`);else if(e===`float16`&&t instanceof Uint16Array&&n!==Uint16Array)a=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw TypeError(`A ${r} tensor's data must be type of ${n}`)}else if(o=t,Array.isArray(e)){if(e.length===0)throw TypeError(`Tensor type cannot be inferred from an empty array.`);let t=typeof e[0];if(t===`string`)r=`string`,a=e;else if(t===`boolean`)r=`bool`,a=Uint8Array.from(e);else throw TypeError(`Invalid element type of data array: ${t}.`)}else if(e instanceof Uint8ClampedArray)r=`uint8`,a=Uint8Array.from(e);else{let t=ie.get(e.constructor);if(t===void 0)throw TypeError(`Unsupported type for tensor data: ${e.constructor}.`);r=t,a=e}if(o===void 0)o=[a.length];else if(!Array.isArray(o))throw TypeError(`A tensor's dims must be a number array`);i=o,this.cpuData=a,this.dataLocation=`cpu`}let a=ce(i);if(this.cpuData&&a!==this.cpuData.length&&!((r===`uint4`||r===`int4`)&&Math.ceil(a/2)===this.cpuData.length))throw Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=r,this.dims=i,this.size=a}static async fromImage(e,t){return D(e,t)}static fromTexture(e,t){return O(e,t)}static fromGpuBuffer(e,t){return k(e,t)}static fromMLTensor(e,t){return A(e,t)}static fromPinnedBuffer(e,t,n){return te(e,t,n)}toDataURL(e){return w(this,e)}toImageData(e){return ee(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw 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 Error(`The data is not stored as a WebGL texture.`);return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw Error(`The data is not stored as a WebGPU buffer.`);return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw 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 Error(`The current tensor is not created with a specified data downloader.`);if(this.isDownloading)throw 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 Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw Error(`The current tensor is being downloaded.`);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 Error(`The tensor is disposed.`)}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw Error(`Cannot reshape a tensor that owns GPU resource.`);return le(this,e)}}}),pe,me=o(()=>{fe(),pe=de}),he,ge,_e,j,M,ve,ye=o(()=>{x(),he=(e,t)=>{(typeof b.trace>`u`?!b.wasm.trace:!b.trace)||console.timeStamp(`${e}::ORT::${t}`)},ge=(e,t)=>{let n=Error().stack?.split(/\r\n|\r|\n/g)||[],r=!1;for(let i=0;i<n.length;i++){if(r&&!n[i].includes(`TRACE_FUNC`)){let r=`FUNC_${e}::${n[i].trim().split(` `)[1]}`;t&&(r+=`::${t}`),he(`CPU`,r);return}n[i].includes(`TRACE_FUNC`)&&(r=!0)}},_e=e=>{(typeof b.trace>`u`?!b.wasm.trace:!b.trace)||ge(`BEGIN`,e)},j=e=>{(typeof b.trace>`u`?!b.wasm.trace:!b.trace)||ge(`END`,e)},M=e=>{(typeof b.trace>`u`?!b.wasm.trace:!b.trace)||console.time(`ORT::${e}`)},ve=e=>{(typeof b.trace>`u`?!b.wasm.trace:!b.trace)||console.timeEnd(`ORT::${e}`)}}),be,xe=o(()=>{h(),me(),ye(),be=class e{constructor(e){this.handler=e}async run(e,t,n){_e(),M(`InferenceSession.run`);let r={},i={};if(typeof e!=`object`||!e||e instanceof pe||Array.isArray(e))throw TypeError(`'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.`);let a=!0;if(typeof t==`object`){if(t===null)throw TypeError(`Unexpected argument[1]: cannot be null.`);if(t instanceof pe)throw TypeError(`'fetches' cannot be a Tensor`);if(Array.isArray(t)){if(t.length===0)throw TypeError(`'fetches' cannot be an empty array.`);a=!1;for(let e of t){if(typeof e!=`string`)throw TypeError(`'fetches' must be a string array or an object.`);if(this.outputNames.indexOf(e)===-1)throw RangeError(`'fetches' contains invalid output name: ${e}.`);r[e]=null}if(typeof n==`object`&&n)i=n;else if(typeof n<`u`)throw TypeError(`'options' must be an object.`)}else{let e=!1,o=Object.getOwnPropertyNames(t);for(let n of this.outputNames)if(o.indexOf(n)!==-1){let i=t[n];(i===null||i instanceof pe)&&(e=!0,a=!1,r[n]=i)}if(e){if(typeof n==`object`&&n)i=n;else if(typeof n<`u`)throw TypeError(`'options' must be an object.`)}else i=t}}else if(typeof t<`u`)throw TypeError(`Unexpected argument[1]: must be 'fetches' or 'options'.`);for(let t of this.inputNames)if(typeof e[t]>`u`)throw Error(`input '${t}' is missing in 'feeds'.`);if(a)for(let e of this.outputNames)r[e]=null;let o=await this.handler.run(e,r,i),s={};for(let e in o)if(Object.hasOwnProperty.call(o,e)){let t=o[e];t instanceof pe?s[e]=t:s[e]=new pe(t.type,t.data,t.dims)}return ve(`InferenceSession.run`),j(),s}async release(){return this.handler.dispose()}static async create(t,n,r,i){_e(),M(`InferenceSession.create`);let a,o={};if(typeof t==`string`){if(a=t,typeof n==`object`&&n)o=n;else if(typeof n<`u`)throw TypeError(`'options' must be an object.`)}else if(t instanceof Uint8Array){if(a=t,typeof n==`object`&&n)o=n;else if(typeof n<`u`)throw TypeError(`'options' must be an object.`)}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&t instanceof SharedArrayBuffer){let e=t,s=0,c=t.byteLength;if(typeof n==`object`&&n)o=n;else if(typeof n==`number`){if(s=n,!Number.isSafeInteger(s))throw RangeError(`'byteOffset' must be an integer.`);if(s<0||s>=e.byteLength)throw RangeError(`'byteOffset' is out of range [0, ${e.byteLength}).`);if(c=t.byteLength-s,typeof r==`number`){if(c=r,!Number.isSafeInteger(c))throw RangeError(`'byteLength' must be an integer.`);if(c<=0||s+c>e.byteLength)throw RangeError(`'byteLength' is out of range (0, ${e.byteLength-s}].`);if(typeof i==`object`&&i)o=i;else if(typeof i<`u`)throw TypeError(`'options' must be an object.`)}else if(typeof r<`u`)throw TypeError(`'byteLength' must be a number.`)}else if(typeof n<`u`)throw TypeError(`'options' must be an object.`);a=new Uint8Array(e,s,c)}else throw TypeError(`Unexpected argument[0]: must be 'path' or 'buffer'.`);let[s,c]=await m(o),l=await s.createInferenceSessionHandler(a,c);return ve(`InferenceSession.create`),j(),new e(l)}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}}}),Se,Ce=o(()=>{xe(),Se=be}),we=o(()=>{}),Te=o(()=>{}),Ee=o(()=>{}),De=o(()=>{}),Oe={};s(Oe,{InferenceSession:()=>Se,TRACE:()=>he,TRACE_EVENT_BEGIN:()=>M,TRACE_EVENT_END:()=>ve,TRACE_FUNC_BEGIN:()=>_e,TRACE_FUNC_END:()=>j,Tensor:()=>pe,env:()=>S,registerBackend:()=>f});var N=o(()=>{g(),C(),Ce(),me(),we(),Te(),ye(),Ee(),De()}),ke=o(()=>{}),Ae={};s(Ae,{default:()=>Ne});var je,Me,Ne,Pe=o(()=>{Wu(),ct(),$e(),je=`ort-wasm-proxy-worker`,Me=globalThis.self?.name===je,Me&&(self.onmessage=e=>{let{type:t,in:n}=e.data;try{switch(t){case`init-wasm`:st(n.wasm).then(()=>{Mu(n).then(()=>{postMessage({type:t})},e=>{postMessage({type:t,err:e})})},e=>{postMessage({type:t,err:e})});break;case`init-ep`:{let{epName:e,env:r}=n;Nu(r,e).then(()=>{postMessage({type:t})},e=>{postMessage({type:t,err:e})});break}case`copy-from`:{let{buffer:e}=n,r=Lu(e);postMessage({type:t,out:r});break}case`create`:{let{model:e,options:r}=n;Ru(e,r).then(e=>{postMessage({type:t,out:e})},e=>{postMessage({type:t,err:e})});break}case`release`:zu(n),postMessage({type:t});break;case`run`:{let{sessionId:e,inputIndices:r,inputs:i,outputIndices:a,options:o}=n;Vu(e,r,i,a,Array(a.length).fill(null),o).then(e=>{e.some(e=>e[3]!==`cpu`)?postMessage({type:t,err:`Proxy does not support non-cpu tensor location.`}):postMessage({type:t,out:e},Uu([...i,...e]))},e=>{postMessage({type:t,err:e})});break}case`end-profiling`:Hu(n),postMessage({type:t});break;default:}}catch(e){postMessage({type:t,err:e})}}),Ne=Me?null:e=>new Worker(e??Ue,{type:`module`,name:je})}),Fe={};s(Fe,{default:()=>Le});async function Ie(e={}){var t=e,n=!!globalThis.window,r=!!globalThis.WorkerGlobalScope,i=r&&self.name?.startsWith(`em-pthread`);t.mountExternalData=(e,n)=>{e.startsWith(`./`)&&(e=e.substring(2)),(t.Zc||=new Map).set(e,n)},t.unmountExternalData=()=>{delete t.Zc},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,ae:!0}).buffer.constructor;let a=e=>async(...n)=>{try{if(t.$c)throw Error(`Session already started`);let r=t.$c={Nd:n[0],errors:[]},i=await e(...n);if(t.$c!==r)throw Error(`Session mismatch`);t.gd?.flush();let a=r.errors;if(0<a.length){let e=await Promise.all(a);if(e=e.filter(e=>e),0<e.length)throw Error(e.join(`2`))}return i}finally{t.$c=null}};t.jsepInit=(e,n)=>{if(e===`webgpu`){[t.gd,t.Dd,t.Hd,t.jd,t.Gd,t.ac,t.Id,t.Kd,t.Ed,t.Fd,t.Jd]=n;let e=t.gd;t.jsepRegisterBuffer=(t,n,r,i)=>e.registerBuffer(t,n,r,i),t.jsepGetBuffer=t=>e.getBuffer(t),t.jsepCreateDownloader=(t,n,r)=>e.createDownloader(t,n,r),t.jsepOnCreateSession=t=>{e.onCreateSession(t)},t.jsepOnReleaseSession=t=>{e.onReleaseSession(t)},t.jsepOnRunStart=t=>e.onRunStart(t),t.Ld=(t,n)=>{e.upload(t,n)}}else if(e===`webnn`){let e=n[0];[t.Zd,t.vd,t.webnnEnsureTensor,t.wd,t.webnnDownloadTensor,t.Yd,t.webnnEnableTraceEvent]=n.slice(1),t.webnnReleaseTensorId=t.vd,t.webnnUploadTensor=t.wd,t.webnnRegisterMLContext=t.Yd,t.webnnOnRunStart=t=>e.onRunStart(t),t.webnnOnRunEnd=e.onRunEnd.bind(e),t.webnnOnReleaseSession=t=>{e.onReleaseSession(t)},t.webnnCreateMLTensorDownloader=(t,n)=>e.createMLTensorDownloader(t,n),t.webnnRegisterMLTensor=(t,n,r,i)=>e.registerMLTensor(t,n,r,i),t.webnnCreateMLContext=t=>e.createMLContext(t),t.webnnRegisterMLConstant=(n,r,i,a,o,s)=>e.registerMLConstant(n,r,i,a,o,t.Zc,s),t.webnnRegisterGraphInput=e.registerGraphInput.bind(e),t.webnnIsGraphInput=e.isGraphInput.bind(e),t.webnnRegisterGraphOutput=e.registerGraphOutput.bind(e),t.webnnIsGraphOutput=e.isGraphOutput.bind(e),t.webnnCreateTemporaryTensor=e.createTemporaryTensor.bind(e),t.webnnIsGraphInputOutputTypeSupported=e.isGraphInputOutputTypeSupported.bind(e)}};let o=()=>{let e=e=>(...t)=>{let n=Qt;return t=e(...t),Qt==n?t:new Promise((e,t)=>{on={resolve:e,reject:t}})};(()=>{for(let n of[`_OrtAppendExecutionProvider`,`_OrtCreateSession`,`_OrtRun`,`_OrtRunWithBinding`,`_OrtBindInput`])t[n]=e(t[n])})(),a!==void 0&&(t._OrtRun=a(t._OrtRun),t._OrtRunWithBinding=a(t._OrtRunWithBinding)),o=void 0};t.asyncInit=()=>{o?.()};var s,c,l=(e,t)=>{throw t},u=import.meta.url,d=``;if(n||r){try{d=new URL(`.`,u).href}catch{}r&&(c=e=>{var t=new XMLHttpRequest;return t.open(`GET`,e,!1),t.responseType=`arraybuffer`,t.send(null),new Uint8Array(t.response)}),s=async e=>{if(C(e))return new Promise((t,n)=>{var r=new XMLHttpRequest;r.open(`GET`,e,!0),r.responseType=`arraybuffer`,r.onload=()=>{r.status==200||r.status==0&&r.response?t(r.response):n(r.status)},r.onerror=n,r.send(null)});var t=await fetch(e,{credentials:`same-origin`});if(t.ok)return t.arrayBuffer();throw Error(t.status+` : `+t.url)}}var f,p,m,h,g,_,v=console.log.bind(console),y=console.error.bind(console),b=v,x=y,S=!1,C=e=>e.startsWith(`file://`);function w(){N.buffer!=T.buffer&&se()}if(i){let e=function(n){try{var r=n.data,i=r.Uc;if(i===`load`){let n=[];self.onmessage=e=>n.push(e),_=()=>{postMessage({Uc:`loaded`});for(let t of n)e(t);self.onmessage=e};for(let e of r.Ad)t[e]&&!t[e].proxy||(t[e]=(...t)=>{postMessage({Uc:`callHandler`,zd:e,args:t})},e==`print`&&(b=t[e]),e==`printErr`&&(x=t[e]));N=r.Vd,se(),p=r.Wd,de(),ia()}else if(i===`run`){(function(e){var t=(w(),A)[e+52>>>2>>>0];e=(w(),A)[e+56>>>2>>>0],Sr(t,t-e),Z(t)})(r.Tc),mr(r.Tc,0,0,1,0,0),Ee(),Ht(r.Tc),ee||=(ur(),!0);try{ke(r.Pd,r.dd)}catch(e){if(e!=`unwind`)throw e}}else r.target!==`setimmediate`&&(i===`checkMailbox`?ee&&Ut():i&&(x(`worker: received unknown command ${i}`),x(r)))}catch(e){throw hr(),e}};var ee=!1;self.onunhandledrejection=e=>{throw e.reason||e},self.onmessage=e}var T,E,D,O,k,A,te,ne,re,ie,ae,oe=!1;function se(){var e=N.buffer;t.HEAP8=T=new Int8Array(e),D=new Int16Array(e),t.HEAPU8=E=new Uint8Array(e),O=new Uint16Array(e),t.HEAP32=k=new Int32Array(e),t.HEAPU32=A=new Uint32Array(e),te=new Float32Array(e),ne=new Float64Array(e),re=new BigInt64Array(e),ie=new BigUint64Array(e)}function ce(){oe=!0,i?_():hi.tb()}function le(e){throw x(e=`Aborted(`+e+`)`),S=!0,e=new WebAssembly.RuntimeError(e+`. Build with -sASSERTIONS for more info.`),g?.(e),e}function ue(){return{a:{ma:yi,hb:vi,g:Me,J:Pe,f:ze,n:Be,h:Ve,ha:He,b:Ue,T:We,Ia:Ke,o:qe,_:Ze,Ya:Qe,Ea:$e,Ga:et,Za:tt,Wa:nt,Pa:rt,Va:it,ka:at,Fa:ot,Ca:st,Xa:F,Da:ct,cb:lt,ea:gt,xa:_t,va:Tt,da:Dt,O:Ot,H:L,wa:jt,Z:Rt,ya:zt,Sa:z,Aa:B,Ja:Gt,ta:Kt,fa:qt,Ra:Ht,$a:Jt,R:ln,r:gn,c:bt,ib:_n,y:vn,M:yn,D:bn,m:V,t:H,jb:xn,I:Sn,S:U,j:Cn,u:W,q:G,l:wn,Ma:Tn,Na:q,Oa:J,Ka:On,La:kn,ua:Y,eb:Mn,bb:Fn,v:Rn,aa:zn,ga:Bn,ab:Nn,V:Vn,_a:Hn,Ba:Un,F:jn,U:Wn,la:Yn,za:Xn,gb:Jn,fb:Zn,Ta:tr,Ua:nr,Ha:be,$:rr,ja:ir,Qa:ar,ia:sr,lb:na,na:Yi,mb:ta,oa:Ji,G:zi,d:Ci,s:xi,w:bi,B:Ni,pb:Gi,K:Ii,x:Ti,pa:Ki,X:Xi,ba:Wi,nb:ea,ob:$i,ra:Bi,qa:Ui,qb:Vi,N:Li,Y:qi,e:wi,A:Ei,k:Si,kb:ra,p:Oi,z:ki,C:Di,E:Ai,L:Pi,rb:Ri,Q:Zi,ca:Fi,W:Qi,sb:Mi,sa:ji,P:Hi,i:cr,a:N,db:ve}}}async function de(){function e(e,n){var r=hi=e.exports;e={};for(let[t,n]of Object.entries(r))typeof n==`function`?(r=Xt(n),e[t]=r):e[t]=n;return hi=e,hi=function(){var e=hi,t=e=>t=>e(t)>>>0,n=e=>()=>e()>>>0;return(e=Object.assign({},e)).ub=t(e.ub),e.Yb=n(e.Yb),e._b=t(e._b),e.mc=t(e.mc),e.nc=n(e.nc),e.rc=t(e.rc),e}(),Ce.push(hi.$b),lr=(e=hi).ub,ur=e.vb,t._OrtInit=e.wb,t._OrtGetLastError=e.xb,t._OrtCreateSessionOptions=e.yb,t._OrtAppendExecutionProvider=e.zb,t._OrtAddFreeDimensionOverride=e.Ab,t._OrtAddSessionConfigEntry=e.Bb,t._OrtReleaseSessionOptions=e.Cb,t._OrtCreateSession=e.Db,t._OrtReleaseSession=e.Eb,t._OrtGetInputOutputCount=e.Fb,t._OrtGetInputOutputMetadata=e.Gb,t._OrtFree=e.Hb,t._OrtCreateTensor=e.Ib,t._OrtGetTensorData=e.Jb,t._OrtReleaseTensor=e.Kb,t._OrtCreateRunOptions=e.Lb,t._OrtAddRunConfigEntry=e.Mb,t._OrtReleaseRunOptions=e.Nb,t._OrtCreateBinding=e.Ob,t._OrtBindInput=e.Pb,t._OrtBindOutput=e.Qb,t._OrtClearBoundOutputs=e.Rb,t._OrtReleaseBinding=e.Sb,t._OrtRunWithBinding=e.Tb,t._OrtRun=e.Ub,t._OrtEndProfiling=e.Vb,t._JsepOutput=e.Wb,t._JsepGetNodeName=e.Xb,dr=e.Yb,fr=t._free=e.Zb,pr=t._malloc=e._b,mr=e.bc,hr=e.cc,gr=e.dc,_r=e.ec,vr=e.fc,yr=e.gc,br=e.hc,X=e.ic,xr=e.jc,Sr=e.kc,Z=e.lc,Cr=e.mc,Q=e.nc,wr=e.oc,Tr=e.pc,Er=e.qc,Dr=e.rc,Or=e.sc,kr=e.tc,Ar=e.uc,jr=e.vc,Mr=e.wc,Nr=e.xc,Pr=e.yc,Fr=e.zc,Ir=e.Ac,Lr=e.Bc,Rr=e.Cc,zr=e.Dc,Br=e.Ec,Vr=e.Fc,Hr=e.Gc,Ur=e.Hc,Wr=e.Ic,Gr=e.Jc,Kr=e.Kc,qr=e.Lc,Jr=e.Mc,Yr=e.Nc,Xr=e.Oc,Zr=e.Pc,Qr=e.Rc,$r=e.Sc,ei=e.bd,ti=e.cd,ni=e.hd,ri=e.ld,$=e.md,ii=e.nd,ai=e.od,oi=e.pd,si=e.qd,ci=e.rd,li=e.sd,ui=e.xd,di=e.Rd,fi=e.Sd,pi=e.Td,mi=e.Ud,p=n,hi}var n,r=ue();return t.instantiateWasm?new Promise(n=>{t.instantiateWasm(r,(t,r)=>{n(e(t,r))})}):i?e(new WebAssembly.Instance(p,ue()),p):(ae??=t.locateFile?t.locateFile?t.locateFile(`ort-wasm-simd-threaded.jsep.wasm`,d):d+`ort-wasm-simd-threaded.jsep.wasm`:new URL(`/assets/ort-wasm-simd-threaded.jsep-Bzonanhp.wasm`,``+import.meta.url).href,n=await async function(e){var t=ae;if(!f&&!C(t))try{var n=fetch(t,{credentials:`same-origin`});return await WebAssembly.instantiateStreaming(n,e)}catch(e){x(`wasm streaming compile failed: ${e}`),x(`falling back to ArrayBuffer instantiation`)}return async function(e,t){try{var n=await async function(e){if(!f)try{var t=await s(e);return new Uint8Array(t)}catch{}if(e==ae&&f)e=new Uint8Array(f);else{if(!c)throw`both async and sync fetching of the wasm failed`;e=c(e)}return e}(e);return await WebAssembly.instantiate(n,t)}catch(e){x(`failed to asynchronously prepare wasm: ${e}`),le(e)}}(t,e)}(r),e(n.instance,n.module))}class fe{name=`ExitStatus`;constructor(e){this.message=`Program terminated with exit(${e})`,this.status=e}}var pe=e=>{e.terminate(),e.onmessage=()=>{}},me=[],he=0,ge=null,_e=e=>{xe.length==0&&(Oe(),De(xe[0]));var t=xe.pop();if(!t)return 6;Se.push(t),we[e.Tc]=t,t.Tc=e.Tc;var n={Uc:`run`,Pd:e.Od,dd:e.dd,Tc:e.Tc};return t.postMessage(n,e.ud),0},j=0,M=(e,t,...n)=>{var r,i=16*n.length,a=Q(),o=Cr(i),s=o>>>3;for(r of n)typeof r==`bigint`?((w(),re)[s++>>>0]=1n,(w(),re)[s++>>>0]=r):((w(),re)[s++>>>0]=0n,(w(),ne)[s++>>>0]=r);return e=gr(e,0,i,o,t),Z(a),e};function ve(e){if(i)return M(0,1,e);if(m=e,!(0<j)){for(var t of Se)pe(t);for(t of xe)pe(t);xe=[],Se=[],we={},S=!0}l(0,new fe(e))}function ye(e){if(i)return M(1,0,e);be(e)}var be=e=>{if(m=e,i)throw ye(e),`unwind`;ve(e)},xe=[],Se=[],Ce=[],we={},Te=e=>{var t=e.Tc;delete we[t],xe.push(e),Se.splice(Se.indexOf(e),1),e.Tc=0,_r(t)};function Ee(){Ce.forEach(e=>e())}var De=e=>new Promise(n=>{e.onmessage=r=>{var i=r.data;if(r=i.Uc,i.ad&&i.ad!=dr()){var a=we[i.ad];a?a.postMessage(i,i.ud):x(`Internal error! Worker sent a message "${r}" to target pthread ${i.ad}, but that thread no longer exists!`)}else r===`checkMailbox`?Ut():r===`spawnThread`?_e(i):r===`cleanupThread`?Bt(()=>{Te(we[i.Qd])}):r===`loaded`?(e.loaded=!0,n(e)):i.target===`setimmediate`?e.postMessage(i):r===`uncaughtException`?e.onerror(i.error):r===`callHandler`?t[i.zd](...i.args):r&&x(`worker sent an unknown command ${r}`)},e.onerror=e=>{throw x(`worker sent an error! ${e.filename}:${e.lineno}: ${e.message}`),e};var r,i=[];for(r of[])t.propertyIsEnumerable(r)&&i.push(r);e.postMessage({Uc:`load`,Ad:i,Vd:N,Wd:p})});function Oe(){var e=new Worker((()=>{let e=URL;return import.meta.url>`file:`&&import.meta.url<`file;`?new e(`ort.bundle.min.mjs`,import.meta.url):new URL(import.meta.url)})(),{type:`module`,workerData:`em-pthread`,name:`em-pthread`});xe.push(e)}var N,ke=(e,t)=>{j=0,e=kr(e,t),0<j?m=e:vr(e)},Ae=[],je=0;function Me(e){var t=new Le(e>>>=0);return(w(),T)[t.Vc+12>>>0]==0&&(Fe(t,!0),je--),Ie(t,!1),Ae.push(t),Dr(e)}var Ne=0,Pe=()=>{X(0,0);var e=Ae.pop();wr(e.ed),Ne=0};function Fe(e,t){t=t?1:0,(w(),T)[e.Vc+12>>>0]=t}function Ie(e,t){t=t?1:0,(w(),T)[e.Vc+13>>>0]=t}class Le{constructor(e){this.ed=e,this.Vc=e-24}}var Re=e=>{var t=Ne;if(!t)return xr(0),0;var n=new Le(t);(w(),A)[n.Vc+16>>>2>>>0]=t;var r=(w(),A)[n.Vc+4>>>2>>>0];if(!r)return xr(0),t;for(var i of e){if(i===0||i===r)break;if(Er(i,r,n.Vc+16))return xr(i),t}return xr(r),t};function ze(){return Re([])}function Be(e){return Re([e>>>0])}function Ve(e,t,n,r){return Re([e>>>0,t>>>0,n>>>0,r>>>0])}var He=()=>{var e=Ae.pop();e||le(`no exception to throw`);var t=e.ed;throw(w(),T)[e.Vc+13>>>0]==0&&(Ae.push(e),Ie(e,!0),Fe(e,!1),je++),Tr(t),Ne=t};function Ue(e,t,n){var r=new Le(e>>>=0);throw t>>>=0,n>>>=0,(w(),A)[r.Vc+16>>>2>>>0]=0,(w(),A)[r.Vc+4>>>2>>>0]=t,(w(),A)[r.Vc+8>>>2>>>0]=n,Tr(e),je++,Ne=e}var We=()=>je;function Ge(e,t,n,r){return i?M(2,1,e,t,n,r):Ke(e,t,n,r)}function Ke(e,t,n,r){if(e>>>=0,t>>>=0,n>>>=0,r>>>=0,!globalThis.SharedArrayBuffer)return 6;var a=[];return i&&a.length===0?Ge(e,t,n,r):(e={Od:n,Tc:e,dd:r,ud:a},i?(e.Uc=`spawnThread`,postMessage(e,a),0):_e(e))}function qe(e){throw Ne||=e>>>0,Ne}var Je=globalThis.TextDecoder&&new TextDecoder,Ye=(e,t,n,r)=>{if(n=t+n,r)return n;for(;e[t]&&!(t>=n);)++t;return t},Xe=(e,t=0,n,r)=>{if(16<(n=Ye(e,t>>>=0,n,r))-t&&e.buffer&&Je)return Je.decode(e.buffer instanceof ArrayBuffer?e.subarray(t,n):e.slice(t,n));for(r=``;t<n;){var i=e[t++];if(128&i){var a=63&e[t++];if((224&i)==192)r+=String.fromCharCode((31&i)<<6|a);else{var o=63&e[t++];65536>(i=(240&i)==224?(15&i)<<12|a<<6|o:(7&i)<<18|a<<12|o<<6|63&e[t++])?r+=String.fromCharCode(i):(i-=65536,r+=String.fromCharCode(55296|i>>10,56320|1023&i))}}else r+=String.fromCharCode(i)}return r},P=(e,t,n)=>(e>>>=0)?Xe((w(),E),e,t,n):``;function Ze(e,t,n){return i?M(3,1,e,t,n):0}function Qe(e,t){if(i)return M(4,1,e,t)}function $e(e,t){if(i)return M(5,1,e,t)}function et(e,t,n){if(i)return M(6,1,e,t,n)}function tt(e,t,n){return i?M(7,1,e,t,n):0}function nt(e,t){if(i)return M(8,1,e,t)}function rt(e,t,n){if(i)return M(9,1,e,t,n)}function it(e,t,n,r){if(i)return M(10,1,e,t,n,r)}function at(e,t,n,r){if(i)return M(11,1,e,t,n,r)}function ot(e,t,n,r){if(i)return M(12,1,e,t,n,r)}function st(e){if(i)return M(13,1,e)}function F(e,t){if(i)return M(14,1,e,t)}function ct(e,t,n){if(i)return M(15,1,e,t,n)}var lt=()=>le(``),ut=e=>{e>>>=0;for(var t=``;;){var n=(w(),E)[e++>>>0];if(!n)return t;t+=String.fromCharCode(n)}},I={},dt={},ft={},pt=class extends Error{constructor(e){super(e),this.name=`BindingError`}};function mt(e,t,n={}){return function(e,t,n={}){var r=t.name;if(!e)throw new pt(`type "${r}" must have a positive integer typeid pointer`);if(dt.hasOwnProperty(e)){if(n.Bd)return;throw new pt(`Cannot register type '${r}' twice`)}dt[e]=t,delete ft[e],I.hasOwnProperty(e)&&(t=I[e],delete I[e],t.forEach(e=>e()))}(e,t,n)}var ht=(e,t,n)=>{switch(t){case 1:return n?e=>(w(),T)[e>>>0]:e=>(w(),E)[e>>>0];case 2:return n?e=>(w(),D)[e>>>1>>>0]:e=>(w(),O)[e>>>1>>>0];case 4:return n?e=>(w(),k)[e>>>2>>>0]:e=>(w(),A)[e>>>2>>>0];case 8:return n?e=>(w(),re)[e>>>3>>>0]:e=>(w(),ie)[e>>>3>>>0];default:throw TypeError(`invalid integer width (${t}): ${e}`)}};function gt(e,t,n,r,i){e>>>=0,n>>>=0,t=ut(t>>>0);let a=e=>e;if(r=r===0n){let e=8*n;a=t=>BigInt.asUintN(e,t),i=a(i)}mt(e,{name:t,Qc:a,Xc:(e,t)=>(typeof t==`number`&&(t=BigInt(t)),t),Wc:ht(t,n,!r),Yc:null})}function _t(e,t,n,r){mt(e>>>=0,{name:t=ut(t>>>0),Qc:function(e){return!!e},Xc:function(e,t){return t?n:r},Wc:function(e){return this.Qc((w(),E)[e>>>0])},Yc:null})}var vt=[],yt=[0,1,,1,null,1,!0,1,!1,1];function bt(e){9<(e>>>=0)&&--yt[e+1]==0&&(yt[e]=void 0,vt.push(e))}var xt=e=>{if(!e)throw new pt(`Cannot use deleted val. handle = ${e}`);return yt[e]},St=e=>{switch(e){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let t=vt.pop()||yt.length;return yt[t]=e,yt[t+1]=1,t}};function Ct(e){return this.Qc((w(),A)[e>>>2>>>0])}var wt={name:`emscripten::val`,Qc:e=>{var t=xt(e);return bt(e),t},Xc:(e,t)=>St(t),Wc:Ct,Yc:null};function Tt(e){return mt(e>>>0,wt)}var Et=(e,t)=>{switch(t){case 4:return function(e){return this.Qc((w(),te)[e>>>2>>>0])};case 8:return function(e){return this.Qc((w(),ne)[e>>>3>>>0])};default:throw TypeError(`invalid float width (${t}): ${e}`)}};function Dt(e,t,n){n>>>=0,mt(e>>>=0,{name:t=ut(t>>>0),Qc:e=>e,Xc:(e,t)=>t,Wc:Et(t,n),Yc:null})}function Ot(e,t,n,r,i){e>>>=0,n>>>=0,t=ut(t>>>0);let a=e=>e;if(r===0){var o=32-8*n;a=e=>e<<o>>>o,i=a(i)}mt(e,{name:t,Qc:a,Xc:(e,t)=>t,Wc:ht(t,n,r!==0),Yc:null})}function L(e,t,n){function r(e){var t=(w(),A)[e>>>2>>>0];return e=(w(),A)[e+4>>>2>>>0],new i((w(),T).buffer,e,t)}var i=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][t];mt(e>>>=0,{name:n=ut(n>>>0),Qc:r,Wc:r},{Bd:!0})}var kt=(e,t,n)=>{var r=(w(),E);if(t>>>=0,0<n){var i=t;n=t+n-1;for(var a=0;a<e.length;++a){var o=e.codePointAt(a);if(127>=o){if(t>=n)break;r[t++>>>0]=o}else if(2047>=o){if(t+1>=n)break;r[t++>>>0]=192|o>>6,r[t++>>>0]=128|63&o}else if(65535>=o){if(t+2>=n)break;r[t++>>>0]=224|o>>12,r[t++>>>0]=128|o>>6&63,r[t++>>>0]=128|63&o}else{if(t+3>=n)break;r[t++>>>0]=240|o>>18,r[t++>>>0]=128|o>>12&63,r[t++>>>0]=128|o>>6&63,r[t++>>>0]=128|63&o,a++}}r[t>>>0]=0,e=t-i}else e=0;return e},At=e=>{for(var t=0,n=0;n<e.length;++n){var r=e.charCodeAt(n);127>=r?t++:2047>=r?t+=2:55296<=r&&57343>=r?(t+=4,++n):t+=3}return t};function jt(e,t){mt(e>>>=0,{name:t=ut(t>>>0),Qc(e){var t=(w(),A)[e>>>2>>>0];return t=P(e+4,t,!0),fr(e),t},Xc(e,t){t instanceof ArrayBuffer&&(t=new Uint8Array(t));var n=typeof t==`string`;if(!(n||ArrayBuffer.isView(t)&&t.BYTES_PER_ELEMENT==1))throw new pt(`Cannot pass non-string to std::string`);var r=n?At(t):t.length,i=pr(4+r+1),a=i+4;return(w(),A)[i>>>2>>>0]=r,n?kt(t,a,r+1):(w(),E).set(t,a>>>0),e!==null&&e.push(fr,i),i},Wc:Ct,Yc(e){fr(e)}})}var Mt=globalThis.TextDecoder?new TextDecoder(`utf-16le`):void 0,Nt=(e,t,n)=>{if(e>>>=1,16<(t=Ye((w(),O),e,t/2,n))-e&&Mt)return Mt.decode((w(),O).slice(e,t));for(n=``;e<t;++e){var r=(w(),O)[e>>>0];n+=String.fromCharCode(r)}return n},Pt=(e,t,n)=>{if(n??=2147483647,2>n)return 0;var r=t;n=(n-=2)<2*e.length?n/2:e.length;for(var i=0;i<n;++i){var a=e.charCodeAt(i);(w(),D)[t>>>1>>>0]=a,t+=2}return(w(),D)[t>>>1>>>0]=0,t-r},Ft=e=>2*e.length,It=(e,t,n)=>{var r=``;e>>>=2;for(var i=0;!(i>=t/4);i++){var a=(w(),A)[e+i>>>0];if(!a&&!n)break;r+=String.fromCodePoint(a)}return r},R=(e,t,n)=>{if(t>>>=0,n??=2147483647,4>n)return 0;var r=t;n=r+n-4;for(var i=0;i<e.length;++i){var a=e.codePointAt(i);if(65535<a&&i++,(w(),k)[t>>>2>>>0]=a,(t+=4)+4>n)break}return(w(),k)[t>>>2>>>0]=0,t-r},Lt=e=>{for(var t=0,n=0;n<e.length;++n)65535<e.codePointAt(n)&&n++,t+=4;return t};function Rt(e,t,n){if(e>>>=0,t>>>=0,n=ut(n>>>=0),t===2)var r=Nt,i=Pt,a=Ft;else r=It,i=R,a=Lt;mt(e,{name:n,Qc:e=>{var n=(w(),A)[e>>>2>>>0];return n=r(e+4,n*t,!0),fr(e),n},Xc:(e,r)=>{if(typeof r!=`string`)throw new pt(`Cannot pass non-string to C++ string type ${n}`);var o=a(r),s=pr(4+o+t);return(w(),A)[s>>>2>>>0]=o/t,i(r,s+4,o+t),e!==null&&e.push(fr,s),s},Wc:Ct,Yc(e){fr(e)}})}function zt(e,t){mt(e>>>=0,{Cd:!0,name:t=ut(t>>>0),Qc:()=>{},Xc:()=>{}})}function z(e){mr(e>>>0,!r,1,!n,131072,!1),Ee()}var Bt=e=>{if(!S)try{if(e(),!(0<j))try{i?dr()&&vr(m):be(m)}catch(e){e instanceof fe||e==`unwind`||l(0,e)}}catch(e){e instanceof fe||e==`unwind`||l(0,e)}},Vt=!Atomics.waitAsync||globalThis.navigator?.userAgent&&91>Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function Ht(e){e>>>=0,Vt||(Atomics.waitAsync((w(),k),e>>>2,e).value.then(Ut),e+=128,Atomics.store((w(),k),e>>>2,1))}var Ut=()=>Bt(()=>{var e=dr();e&&(Ht(e),br())});function B(e,t){(e>>>=0)==t>>>0?setTimeout(Ut):i?postMessage({ad:e,Uc:`checkMailbox`}):(e=we[e])&&e.postMessage({Uc:`checkMailbox`})}var Wt=[];function Gt(e,t,n,r,i){for(t>>>=0,i>>>=0,Wt.length=0,n=i>>>3,r=i+r>>>3;n<r;){var a=(w(),re)[n++>>>0]?(w(),re)[n++>>>0]:(w(),ne)[n++>>>0];Wt.push(a)}return(t?_i[t]:gi[e])(...Wt)}var Kt=()=>{j=0};function qt(e){e>>>=0,i?postMessage({Uc:`cleanupThread`,Qd:e}):Te(we[e])}function Jt(e){}var Yt=e=>{try{e()}catch(e){le(e)}};function Xt(e){var t=(...t)=>{en.push(e);try{return e(...t)}finally{S||(en.pop(),Qt&&Zt===1&&en.length===0&&(Zt=0,j+=1,Yt(fi),typeof Fibers<`u`&&Fibers.ce()))}};return rn.set(e,t),t}var Zt=0,Qt=null,$t=0,en=[],tn=new Map,nn=new Map,rn=new Map,an=0,on=null,sn=[],cn=e=>function(e){if(!S){if(Zt===0){var t=!1,n=!1;e((e=0)=>{if(!S&&($t=e,t=!0,n)){Zt=2,Yt(()=>pi(Qt)),typeof MainLoop<`u`&&MainLoop.yd&&MainLoop.resume(),e=!1;try{var r=function(){var e=(w(),k)[Qt+8>>>2>>>0];return e=nn.get(e),e=rn.get(e),--j,e()}()}catch(t){r=t,e=!0}var i=!1;if(!Qt){var a=on;a&&(on=null,(e?a.reject:a.resolve)(r),i=!0)}if(e&&!i)throw r}}),n=!0,t||(Zt=1,Qt=function(){var e=pr(65548),t=e+12;if((w(),A)[e>>>2>>>0]=t,(w(),A)[e+4>>>2>>>0]=t+65536,t=en[0],!tn.has(t)){var n=an++;tn.set(t,n),nn.set(n,t)}return t=tn.get(t),(w(),k)[e+8>>>2>>>0]=t,e}(),typeof MainLoop<`u`&&MainLoop.yd&&MainLoop.pause(),Yt(()=>di(Qt)))}else Zt===2?(Zt=0,Yt(mi),fr(Qt),Qt=null,sn.forEach(Bt)):le(`invalid state: ${Zt}`);return $t}}(t=>{e().then(t)});function ln(e){return e>>>=0,cn(async()=>St(await xt(e)))}var un=[],dn=e=>{var t=un.length;return un.push(e),t},fn=(e,t)=>{for(var n=Array(e),r=0;r<e;++r){var i=r,a=(w(),A)[t+4*r>>>2>>>0],o=dt[a];if(o===void 0)throw e=`parameter ${r}`,a=lr(a),t=ut(a),fr(a),new pt(`${e} has unknown type ${t}`);n[i]=o}return n},pn=(e,t,n)=>{var r=[];return e=e(r,n),r.length&&((w(),A)[t>>>2>>>0]=St(r)),e},mn={},hn=e=>{var t=mn[e];return t===void 0?ut(e):t};function gn(e,t,n){var[r,...i]=fn(e,t>>>0);t=r.Xc.bind(r);var a=i.map(e=>e.Wc.bind(e));e--;var o={toValue:xt};switch(e=a.map((e,t)=>{var n=`argFromPtr${t}`;return o[n]=e,`${n}(args${t?`+`+8*t:``})`}),n){case 0:var s=`toValue(handle)`;break;case 2:s=`new (toValue(handle))`;break;case 3:s=``;break;case 1:o.getStringOrSymbol=hn,s=`toValue(handle)[getStringOrSymbol(methodName)]`}return s+=`(${e})`,r.Cd||(o.toReturnWire=t,o.emval_returnValue=pn,s=`return emval_returnValue(toReturnWire, destructorsRef, ${s})`),s=`return function (handle, methodName, destructorsRef, args) {3  ${s}4  }`,n=Function(Object.keys(o),s)(...Object.values(o)),s=`methodCaller<(${i.map(e=>e.name)}) => ${r.name}>`,dn(Object.defineProperty(n,`name`,{value:s}))}function _n(e,t){return t>>>=0,(e=xt(e>>>0))==xt(t)}function vn(e){return(e>>>=0)?(e=hn(e),St(globalThis[e])):St(globalThis)}function yn(e){return e=hn(e>>>0),St(t[e])}function bn(e,t){return t>>>=0,e=xt(e>>>0),t=xt(t),St(e[t])}function V(e){9<(e>>>=0)&&(yt[e+1]+=1)}function H(e,t,n,r,i){return un[e>>>0](t>>>0,n>>>0,r>>>0,i>>>0)}function xn(e,t,n,r,i){return H(e>>>0,t>>>0,n>>>0,r>>>0,i>>>0)}function Sn(){return St([])}function U(e){e=xt(e>>>0);for(var t=Array(e.length),n=0;n<e.length;n++)t[n]=e[n];return St(t)}function Cn(e){return St(hn(e>>>0))}function W(){return St({})}function G(e){for(var t=xt(e>>>=0);t.length;){var n=t.pop();t.pop()(n)}bt(e)}function wn(e,t,n){t>>>=0,n>>>=0,e=xt(e>>>0),t=xt(t),n=xt(n),e[t]=n}function Tn(e,t){e=-9007199254740992>e||9007199254740992<e?NaN:Number(e),t>>>=0,e=new Date(1e3*e),(w(),k)[t>>>2>>>0]=e.getUTCSeconds(),(w(),k)[t+4>>>2>>>0]=e.getUTCMinutes(),(w(),k)[t+8>>>2>>>0]=e.getUTCHours(),(w(),k)[t+12>>>2>>>0]=e.getUTCDate(),(w(),k)[t+16>>>2>>>0]=e.getUTCMonth(),(w(),k)[t+20>>>2>>>0]=e.getUTCFullYear()-1900,(w(),k)[t+24>>>2>>>0]=e.getUTCDay(),e=(e.getTime()-Date.UTC(e.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(w(),k)[t+28>>>2>>>0]=e}var En=e=>e%4==0&&(e%100!=0||e%400==0),K=[0,31,60,91,121,152,182,213,244,274,305,335],Dn=[0,31,59,90,120,151,181,212,243,273,304,334];function q(e,t){e=-9007199254740992>e||9007199254740992<e?NaN:Number(e),t>>>=0,e=new Date(1e3*e),(w(),k)[t>>>2>>>0]=e.getSeconds(),(w(),k)[t+4>>>2>>>0]=e.getMinutes(),(w(),k)[t+8>>>2>>>0]=e.getHours(),(w(),k)[t+12>>>2>>>0]=e.getDate(),(w(),k)[t+16>>>2>>>0]=e.getMonth(),(w(),k)[t+20>>>2>>>0]=e.getFullYear()-1900,(w(),k)[t+24>>>2>>>0]=e.getDay();var n=(En(e.getFullYear())?K:Dn)[e.getMonth()]+e.getDate()-1|0;(w(),k)[t+28>>>2>>>0]=n,(w(),k)[t+36>>>2>>>0]=-60*e.getTimezoneOffset(),n=new Date(e.getFullYear(),6,1).getTimezoneOffset();var r=new Date(e.getFullYear(),0,1).getTimezoneOffset();e=0|(n!=r&&e.getTimezoneOffset()==Math.min(r,n)),(w(),k)[t+32>>>2>>>0]=e}function J(e){e>>>=0;var t=new Date((w(),k)[e+20>>>2>>>0]+1900,(w(),k)[e+16>>>2>>>0],(w(),k)[e+12>>>2>>>0],(w(),k)[e+8>>>2>>>0],(w(),k)[e+4>>>2>>>0],(w(),k)[e>>>2>>>0],0),n=(w(),k)[e+32>>>2>>>0],r=t.getTimezoneOffset(),i=new Date(t.getFullYear(),6,1).getTimezoneOffset(),a=new Date(t.getFullYear(),0,1).getTimezoneOffset(),o=Math.min(a,i);return 0>n?(w(),k)[e+32>>>2>>>0]=+(i!=a&&o==r):0<n!=(o==r)&&(i=Math.max(a,i),t.setTime(t.getTime()+6e4*((0<n?o:i)-r))),(w(),k)[e+24>>>2>>>0]=t.getDay(),n=(En(t.getFullYear())?K:Dn)[t.getMonth()]+t.getDate()-1|0,(w(),k)[e+28>>>2>>>0]=n,(w(),k)[e>>>2>>>0]=t.getSeconds(),(w(),k)[e+4>>>2>>>0]=t.getMinutes(),(w(),k)[e+8>>>2>>>0]=t.getHours(),(w(),k)[e+12>>>2>>>0]=t.getDate(),(w(),k)[e+16>>>2>>>0]=t.getMonth(),(w(),k)[e+20>>>2>>>0]=t.getYear(),e=t.getTime(),BigInt(isNaN(e)?-1:e/1e3)}function On(e,t,n,r,a,o,s){return i?M(16,1,e,t,n,r,a,o,s):-52}function kn(e,t,n,r,a,o){if(i)return M(17,1,e,t,n,r,a,o)}var An={},jn=()=>performance.timeOrigin+performance.now();function Y(e,t){return i?M(18,1,e,t):(An[e]&&(clearTimeout(An[e].id),delete An[e]),t&&(An[e]={id:setTimeout(()=>{delete An[e],Bt(()=>yr(e,performance.timeOrigin+performance.now()))},t),be:t}),0)}function Mn(e,t,n,r){e>>>=0,t>>>=0,n>>>=0,r>>>=0;var i=new Date().getFullYear(),a=new Date(i,0,1).getTimezoneOffset();i=new Date(i,6,1).getTimezoneOffset();var o=Math.max(a,i);(w(),A)[e>>>2>>>0]=60*o,(w(),k)[t>>>2>>>0]=+(a!=i),e=(t=e=>{var t=Math.abs(e);return`UTC${0<=e?`-`:`+`}${String(Math.floor(t/60)).padStart(2,`0`)}${String(t%60).padStart(2,`0`)}`})(a),t=t(i),i<a?(kt(e,n,17),kt(t,r,17)):(kt(e,r,17),kt(t,n,17))}var Nn=()=>Date.now(),Pn=1;function Fn(e,t,n){if(n>>>=0,!(0<=e&&3>=e))return 28;if(e===0)e=Date.now();else{if(!Pn)return 52;e=performance.timeOrigin+performance.now()}return e=Math.round(1e6*e),(w(),re)[n>>>3>>>0]=BigInt(e),0}var In=[],Ln=(e,t)=>{In.length=0;for(var n;n=(w(),E)[e++>>>0];){var r=n!=105;t+=(r&=n!=112)&&t%8?4:0,In.push(n==112?(w(),A)[t>>>2>>>0]:n==106?(w(),re)[t>>>3>>>0]:n==105?(w(),k)[t>>>2>>>0]:(w(),ne)[t>>>3>>>0]),t+=r?8:4}return In};function Rn(e,t,n){return e>>>=0,t=Ln(t>>>0,n>>>0),_i[e](...t)}function zn(e,t,n){return e>>>=0,t=Ln(t>>>0,n>>>0),_i[e](...t)}var Bn=()=>{};function Vn(e,t){return x(P(e>>>0,t>>>0))}var Hn=()=>{throw j+=1,`unwind`};function Un(){return 4294901760}var Wn=()=>navigator.hardwareConcurrency,Gn={},Kn=e=>{var t;return(t=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(e))?+t[1]:(t=/:(\d+):\d+(?:\)|$)/.exec(e))?2147483648|t[1]:0},qn=e=>{for(var t of e)(e=Kn(t))&&(Gn[e]=t)};function Jn(){var e=Error().stack.toString().split(`5`);return e[0]==`Error`&&e.shift(),qn(e),Gn.kd=Kn(e[3]),Gn.Md=e,Gn.kd}function Yn(e){if(!(e=Gn[e>>>0]))return 0;var t;if(t=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(e))e=t[1];else if(t=/^\s+at (.*) \(.*\)$/.exec(e))e=t[1];else{if(!(t=/^(.+?)@/.exec(e)))return 0;e=t[1]}fr(Yn.td??0),t=At(e)+1;var n=pr(t);return n&&kt(e,n,t),Yn.td=n,Yn.td}function Xn(e){e>>>=0;var t=(w(),E).length;if(e<=t||4294901760<e)return!1;for(var n=1;4>=n;n*=2){var r=t*(1+.2/n);r=Math.min(r,e+100663296);e:{r=(Math.min(4294901760,65536*Math.ceil(Math.max(e,r)/65536))-N.buffer.byteLength+65535)/65536|0;try{N.grow(r),se();var i=1;break e}catch{}i=void 0}if(i)return!0}return!1}function Zn(e,t,n){if(e>>>=0,t>>>=0,Gn.kd==e)var r=Gn.Md;else (r=Error().stack.toString().split(`6`))[0]==`Error`&&r.shift(),qn(r);for(var i=3;r[i]&&Kn(r[i])!=e;)++i;for(e=0;e<n&&r[e+i];++e)(w(),k)[t+4*e>>>2>>>0]=Kn(r[e+i]);return e}var Qn,$n={},er=()=>{if(!Qn){var e,t={USER:`web_user`,LOGNAME:`web_user`,PATH:`/`,PWD:`/`,HOME:`/home/web_user`,LANG:(globalThis.navigator?.language??`C`).replace(`-`,`_`)+`.UTF-8`,_:`./this.program`};for(e in $n)$n[e]===void 0?delete t[e]:t[e]=$n[e];var n=[];for(e in t)n.push(`${e}=${t[e]}`);Qn=n}return Qn};function tr(e,t){if(i)return M(19,1,e,t);e>>>=0,t>>>=0;var n,r=0,a=0;for(n of er()){var o=t+r;(w(),A)[e+a>>>2>>>0]=o,r+=kt(n,o,1/0)+1,a+=4}return 0}function nr(e,t){if(i)return M(20,1,e,t);e>>>=0,t>>>=0;var n=er();for(var r of((w(),A)[e>>>2>>>0]=n.length,e=0,n))e+=At(r)+1;return(w(),A)[t>>>2>>>0]=e,0}function rr(e){return i?M(21,1,e):52}function ir(e,t,n,r){return i?M(22,1,e,t,n,r):52}function ar(e,t,n,r){return i?M(23,1,e,t,n,r):70}var or=[null,[],[]];function sr(e,t,n,r){if(i)return M(24,1,e,t,n,r);t>>>=0,n>>>=0,r>>>=0;for(var a=0,o=0;o<n;o++){var s=(w(),A)[t>>>2>>>0],c=(w(),A)[t+4>>>2>>>0];t+=8;for(var l=0;l<c;l++){var u=e,d=(w(),E)[s+l>>>0],f=or[u];d===0||d===10?((u===1?b:x)(Xe(f)),f.length=0):f.push(d)}a+=c}return(w(),A)[r>>>2>>>0]=a,0}function cr(e){return e>>>0}i||function(){for(var e=t.numThreads-1;e--;)Oe();me.push(async()=>{var e=async function(){if(!i)return Promise.all(xe.map(De))}();he++,await e,--he==0&&ge&&(e=ge,ge=null,e())})}(),i||(N=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),se()),t.wasmBinary&&(f=t.wasmBinary),t.stackSave=()=>Q(),t.stackRestore=e=>Z(e),t.stackAlloc=e=>Cr(e),t.setValue=function(e,t,n=`i8`){switch(n.endsWith(`*`)&&(n=`*`),n){case`i1`:case`i8`:(w(),T)[e>>>0]=t;break;case`i16`:(w(),D)[e>>>1>>>0]=t;break;case`i32`:(w(),k)[e>>>2>>>0]=t;break;case`i64`:(w(),re)[e>>>3>>>0]=BigInt(t);break;case`float`:(w(),te)[e>>>2>>>0]=t;break;case`double`:(w(),ne)[e>>>3>>>0]=t;break;case`*`:(w(),A)[e>>>2>>>0]=t;break;default:le(`invalid type for setValue: ${n}`)}},t.getValue=function(e,t=`i8`){switch(t.endsWith(`*`)&&(t=`*`),t){case`i1`:case`i8`:return(w(),T)[e>>>0];case`i16`:return(w(),D)[e>>>1>>>0];case`i32`:return(w(),k)[e>>>2>>>0];case`i64`:return(w(),re)[e>>>3>>>0];case`float`:return(w(),te)[e>>>2>>>0];case`double`:return(w(),ne)[e>>>3>>>0];case`*`:return(w(),A)[e>>>2>>>0];default:le(`invalid type for getValue: ${t}`)}},t.UTF8ToString=P,t.stringToUTF8=kt,t.lengthBytesUTF8=At;var lr,ur,dr,fr,pr,mr,hr,gr,_r,vr,yr,br,X,xr,Sr,Z,Cr,Q,wr,Tr,Er,Dr,Or,kr,Ar,jr,Mr,Nr,Pr,Fr,Ir,Lr,Rr,zr,Br,Vr,Hr,Ur,Wr,Gr,Kr,qr,Jr,Yr,Xr,Zr,Qr,$r,ei,ti,ni,ri,$,ii,ai,oi,si,ci,li,ui,di,fi,pi,mi,hi,gi=[ve,ye,Ge,Ze,Qe,$e,et,tt,nt,rt,it,at,ot,st,F,ct,On,kn,Y,tr,nr,rr,ir,ar,sr],_i={937996:(e,n,r,i,a)=>{if(t===void 0||!t.Zc)return 1;if((e=P(Number(e>>>0))).startsWith(`./`)&&(e=e.substring(2)),!(e=t.Zc.get(e)))return 2;if(n=Number(n>>>0),r=Number(r>>>0),i=Number(i>>>0),n+r>e.byteLength)return 3;try{let o=e.subarray(n,n+r);switch(a){case 0:(w(),E).set(o,i>>>0);break;case 1:t.Xd?t.Xd(i,o):t.Ld(i,o);break;default:return 4}return 0}catch{return 4}},938820:(e,n,r)=>{t.wd(e,(w(),E).subarray(n>>>0,n+r>>>0))},938884:()=>t.Zd(),938926:e=>{t.vd(e)},938963:()=>{t.Ed()},938994:()=>{t.Fd()},939023:()=>{t.Jd()},939048:e=>t.Dd(e),939081:e=>t.Hd(e),939113:(e,n,r)=>{t.jd(Number(e),Number(n),Number(r),!0)},939176:(e,n,r)=>{t.jd(Number(e),Number(n),Number(r))},939233:()=>typeof wasmOffsetConverter<`u`,939290:e=>{t.ac(`Abs`,e,void 0)},939341:e=>{t.ac(`Neg`,e,void 0)},939392:e=>{t.ac(`Floor`,e,void 0)},939445:e=>{t.ac(`Ceil`,e,void 0)},939497:e=>{t.ac(`Reciprocal`,e,void 0)},939555:e=>{t.ac(`Sqrt`,e,void 0)},939607:e=>{t.ac(`Exp`,e,void 0)},939658:e=>{t.ac(`Erf`,e,void 0)},939709:e=>{t.ac(`Sigmoid`,e,void 0)},939764:(e,n,r)=>{t.ac(`HardSigmoid`,e,{alpha:n,beta:r})},939843:e=>{t.ac(`Log`,e,void 0)},939894:e=>{t.ac(`Sin`,e,void 0)},939945:e=>{t.ac(`Cos`,e,void 0)},939996:e=>{t.ac(`Tan`,e,void 0)},940047:e=>{t.ac(`Asin`,e,void 0)},940099:e=>{t.ac(`Acos`,e,void 0)},940151:e=>{t.ac(`Atan`,e,void 0)},940203:e=>{t.ac(`Sinh`,e,void 0)},940255:e=>{t.ac(`Cosh`,e,void 0)},940307:e=>{t.ac(`Asinh`,e,void 0)},940360:e=>{t.ac(`Acosh`,e,void 0)},940413:e=>{t.ac(`Atanh`,e,void 0)},940466:e=>{t.ac(`Tanh`,e,void 0)},940518:e=>{t.ac(`Not`,e,void 0)},940569:(e,n,r)=>{t.ac(`Clip`,e,{min:n,max:r})},940638:e=>{t.ac(`Clip`,e,void 0)},940690:(e,n)=>{t.ac(`Elu`,e,{alpha:n})},940748:e=>{t.ac(`Gelu`,e,void 0)},940800:e=>{t.ac(`Relu`,e,void 0)},940852:(e,n)=>{t.ac(`LeakyRelu`,e,{alpha:n})},940916:(e,n)=>{t.ac(`ThresholdedRelu`,e,{alpha:n})},940986:(e,n)=>{t.ac(`Cast`,e,{to:n})},941044:e=>{t.ac(`Add`,e,void 0)},941095:e=>{t.ac(`Sub`,e,void 0)},941146:e=>{t.ac(`Mul`,e,void 0)},941197:e=>{t.ac(`Div`,e,void 0)},941248:e=>{t.ac(`Pow`,e,void 0)},941299:e=>{t.ac(`Equal`,e,void 0)},941352:e=>{t.ac(`Greater`,e,void 0)},941407:e=>{t.ac(`GreaterOrEqual`,e,void 0)},941469:e=>{t.ac(`Less`,e,void 0)},941521:e=>{t.ac(`LessOrEqual`,e,void 0)},941580:(e,n,r,i,a)=>{t.ac(`ReduceMean`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},941755:(e,n,r,i,a)=>{t.ac(`ReduceMax`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},941929:(e,n,r,i,a)=>{t.ac(`ReduceMin`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},942103:(e,n,r,i,a)=>{t.ac(`ReduceProd`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},942278:(e,n,r,i,a)=>{t.ac(`ReduceSum`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},942452:(e,n,r,i,a)=>{t.ac(`ReduceL1`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},942625:(e,n,r,i,a)=>{t.ac(`ReduceL2`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},942798:(e,n,r,i,a)=>{t.ac(`ReduceLogSum`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},942975:(e,n,r,i,a)=>{t.ac(`ReduceSumSquare`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},943155:(e,n,r,i,a)=>{t.ac(`ReduceLogSumExp`,e,{keepDims:!!n,noopWithEmptyAxes:!!r,axes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},943335:e=>{t.ac(`Where`,e,void 0)},943388:(e,n,r)=>{t.ac(`Transpose`,e,{perm:n?Array.from((w(),k).subarray(Number(n)>>>0,Number(r)>>>0)):[]})},943512:(e,n,r,i)=>{t.ac(`DepthToSpace`,e,{blocksize:n,mode:P(r),format:i?`NHWC`:`NCHW`})},943645:(e,n,r,i)=>{t.ac(`DepthToSpace`,e,{blocksize:n,mode:P(r),format:i?`NHWC`:`NCHW`})},943778:(e,n,r,i,a,o,s,c,l,u,d,f,p,m,h)=>{t.ac(`ConvTranspose`,e,{format:l?`NHWC`:`NCHW`,autoPad:n,dilations:[r],group:i,kernelShape:[a],pads:[o,s],strides:[c],wIsConst:()=>!!(w(),T)[u>>>0],outputPadding:d?Array.from((w(),k).subarray(Number(d)>>>0,Number(f)>>>0)):[],outputShape:p?Array.from((w(),k).subarray(Number(p)>>>0,Number(m)>>>0)):[],activation:P(h)})},944211:(e,n,r,i,a,o,s,c,l,u,d,f,p,m)=>{t.ac(`ConvTranspose`,e,{format:c?`NHWC`:`NCHW`,autoPad:n,dilations:Array.from((w(),k).subarray(Number(r)>>>0,2+(Number(r)>>>0)>>>0)),group:i,kernelShape:Array.from((w(),k).subarray(Number(a)>>>0,2+(Number(a)>>>0)>>>0)),pads:Array.from((w(),k).subarray(Number(o)>>>0,4+(Number(o)>>>0)>>>0)),strides:Array.from((w(),k).subarray(Number(s)>>>0,2+(Number(s)>>>0)>>>0)),wIsConst:()=>!!(w(),T)[l>>>0],outputPadding:u?Array.from((w(),k).subarray(Number(u)>>>0,Number(d)>>>0)):[],outputShape:f?Array.from((w(),k).subarray(Number(f)>>>0,Number(p)>>>0)):[],activation:P(m)})},944872:(e,n,r,i,a,o,s,c,l,u,d,f,p,m,h)=>{t.ac(`ConvTranspose`,e,{format:l?`NHWC`:`NCHW`,autoPad:n,dilations:[r],group:i,kernelShape:[a],pads:[o,s],strides:[c],wIsConst:()=>!!(w(),T)[u>>>0],outputPadding:d?Array.from((w(),k).subarray(Number(d)>>>0,Number(f)>>>0)):[],outputShape:p?Array.from((w(),k).subarray(Number(p)>>>0,Number(m)>>>0)):[],activation:P(h)})},945305:(e,n,r,i,a,o,s,c,l,u,d,f,p,m)=>{t.ac(`ConvTranspose`,e,{format:c?`NHWC`:`NCHW`,autoPad:n,dilations:Array.from((w(),k).subarray(Number(r)>>>0,2+(Number(r)>>>0)>>>0)),group:i,kernelShape:Array.from((w(),k).subarray(Number(a)>>>0,2+(Number(a)>>>0)>>>0)),pads:Array.from((w(),k).subarray(Number(o)>>>0,4+(Number(o)>>>0)>>>0)),strides:Array.from((w(),k).subarray(Number(s)>>>0,2+(Number(s)>>>0)>>>0)),wIsConst:()=>!!(w(),T)[l>>>0],outputPadding:u?Array.from((w(),k).subarray(Number(u)>>>0,Number(d)>>>0)):[],outputShape:f?Array.from((w(),k).subarray(Number(f)>>>0,Number(p)>>>0)):[],activation:P(m)})},945966:(e,n)=>{t.ac(`GlobalAveragePool`,e,{format:n?`NHWC`:`NCHW`})},946057:(e,n,r,i,a,o,s,c,l,u,d,f,p,m)=>{t.ac(`AveragePool`,e,{format:m?`NHWC`:`NCHW`,auto_pad:n,ceil_mode:r,count_include_pad:i,storage_order:a,dilations:o?Array.from((w(),k).subarray(Number(o)>>>0,Number(s)>>>0)):[],kernel_shape:c?Array.from((w(),k).subarray(Number(c)>>>0,Number(l)>>>0)):[],pads:u?Array.from((w(),k).subarray(Number(u)>>>0,Number(d)>>>0)):[],strides:f?Array.from((w(),k).subarray(Number(f)>>>0,Number(p)>>>0)):[]})},946536:(e,n)=>{t.ac(`GlobalAveragePool`,e,{format:n?`NHWC`:`NCHW`})},946627:(e,n,r,i,a,o,s,c,l,u,d,f,p,m)=>{t.ac(`AveragePool`,e,{format:m?`NHWC`:`NCHW`,auto_pad:n,ceil_mode:r,count_include_pad:i,storage_order:a,dilations:o?Array.from((w(),k).subarray(Number(o)>>>0,Number(s)>>>0)):[],kernel_shape:c?Array.from((w(),k).subarray(Number(c)>>>0,Number(l)>>>0)):[],pads:u?Array.from((w(),k).subarray(Number(u)>>>0,Number(d)>>>0)):[],strides:f?Array.from((w(),k).subarray(Number(f)>>>0,Number(p)>>>0)):[]})},947106:(e,n)=>{t.ac(`GlobalMaxPool`,e,{format:n?`NHWC`:`NCHW`})},947193:(e,n,r,i,a,o,s,c,l,u,d,f,p,m)=>{t.ac(`MaxPool`,e,{format:m?`NHWC`:`NCHW`,auto_pad:n,ceil_mode:r,count_include_pad:i,storage_order:a,dilations:o?Array.from((w(),k).subarray(Number(o)>>>0,Number(s)>>>0)):[],kernel_shape:c?Array.from((w(),k).subarray(Number(c)>>>0,Number(l)>>>0)):[],pads:u?Array.from((w(),k).subarray(Number(u)>>>0,Number(d)>>>0)):[],strides:f?Array.from((w(),k).subarray(Number(f)>>>0,Number(p)>>>0)):[]})},947668:(e,n)=>{t.ac(`GlobalMaxPool`,e,{format:n?`NHWC`:`NCHW`})},947755:(e,n,r,i,a,o,s,c,l,u,d,f,p,m)=>{t.ac(`MaxPool`,e,{format:m?`NHWC`:`NCHW`,auto_pad:n,ceil_mode:r,count_include_pad:i,storage_order:a,dilations:o?Array.from((w(),k).subarray(Number(o)>>>0,Number(s)>>>0)):[],kernel_shape:c?Array.from((w(),k).subarray(Number(c)>>>0,Number(l)>>>0)):[],pads:u?Array.from((w(),k).subarray(Number(u)>>>0,Number(d)>>>0)):[],strides:f?Array.from((w(),k).subarray(Number(f)>>>0,Number(p)>>>0)):[]})},948230:(e,n,r,i,a)=>{t.ac(`Gemm`,e,{alpha:n,beta:r,transA:i,transB:a})},948334:e=>{t.ac(`MatMul`,e,void 0)},948388:(e,n,r,i)=>{t.ac(`ArgMax`,e,{keepDims:!!n,selectLastIndex:!!r,axis:i})},948496:(e,n,r,i)=>{t.ac(`ArgMin`,e,{keepDims:!!n,selectLastIndex:!!r,axis:i})},948604:(e,n)=>{t.ac(`Softmax`,e,{axis:n})},948667:(e,n)=>{t.ac(`Concat`,e,{axis:n})},948727:(e,n,r,i,a)=>{t.ac(`Split`,e,{axis:n,numOutputs:r,splitSizes:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},948883:e=>{t.ac(`Expand`,e,void 0)},948937:(e,n)=>{t.ac(`Gather`,e,{axis:Number(n)})},949008:(e,n)=>{t.ac(`GatherElements`,e,{axis:Number(n)})},949087:(e,n)=>{t.ac(`GatherND`,e,{batch_dims:Number(n)})},949166:(e,n,r,i,a,o,s,c,l,u,d)=>{t.ac(`Resize`,e,{antialias:n,axes:r?Array.from((w(),k).subarray(Number(r)>>>0,Number(i)>>>0)):[],coordinateTransformMode:P(a),cubicCoeffA:o,excludeOutside:s,extrapolationValue:c,keepAspectRatioPolicy:P(l),mode:P(u),nearestMode:P(d)})},949528:(e,n,r,i,a,o,s)=>{t.ac(`Slice`,e,{starts:n?Array.from((w(),k).subarray(Number(n)>>>0,Number(r)>>>0)):[],ends:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[],axes:o?Array.from((w(),k).subarray(Number(o)>>>0,Number(s)>>>0)):[]})},949792:e=>{t.ac(`Tile`,e,void 0)},949844:(e,n,r)=>{t.ac(`InstanceNormalization`,e,{epsilon:n,format:r?`NHWC`:`NCHW`})},949958:(e,n,r)=>{t.ac(`InstanceNormalization`,e,{epsilon:n,format:r?`NHWC`:`NCHW`})},950072:e=>{t.ac(`Range`,e,void 0)},950125:(e,n)=>{t.ac(`Einsum`,e,{equation:P(n)})},950206:(e,n,r,i,a)=>{t.ac(`Pad`,e,{mode:n,value:r,pads:i?Array.from((w(),k).subarray(Number(i)>>>0,Number(a)>>>0)):[]})},950349:(e,n,r,i,a,o)=>{t.ac(`BatchNormalization`,e,{epsilon:n,momentum:r,spatial:!!a,trainingMode:!!i,format:o?`NHWC`:`NCHW`})},950518:(e,n,r,i,a,o)=>{t.ac(`BatchNormalization`,e,{epsilon:n,momentum:r,spatial:!!a,trainingMode:!!i,format:o?`NHWC`:`NCHW`})},950687:(e,n,r)=>{t.ac(`CumSum`,e,{exclusive:Number(n),reverse:Number(r)})},950784:(e,n,r)=>{t.ac(`DequantizeLinear`,e,{axis:n,blockSize:r})},950874:(e,n,r,i,a)=>{t.ac(`GridSample`,e,{align_corners:n,mode:P(r),padding_mode:P(i),format:a?`NHWC`:`NCHW`})},951044:(e,n,r,i,a)=>{t.ac(`GridSample`,e,{align_corners:n,mode:P(r),padding_mode:P(i),format:a?`NHWC`:`NCHW`})},951214:(e,n)=>{t.ac(`ScatterND`,e,{reduction:P(n)})},951299:(e,n,r,i,a,o,s,c,l)=>{t.ac(`Attention`,e,{numHeads:n,isUnidirectional:r,maskFilterValue:i,scale:a,doRotary:o,qkvHiddenSizes:s?Array.from((w(),k).subarray(Number(c)>>>0,Number(c)+s>>>0)):[],pastPresentShareBuffer:!!l})},951571:e=>{t.ac(`BiasAdd`,e,void 0)},951626:e=>{t.ac(`BiasSplitGelu`,e,void 0)},951687:e=>{t.ac(`FastGelu`,e,void 0)},951743:(e,n,r,i,a,o,s,c,l,u,d,f,p,m,h,g)=>{t.ac(`Conv`,e,{format:f?`NHWC`:`NCHW`,auto_pad:n,dilations:r?Array.from((w(),k).subarray(Number(r)>>>0,Number(i)>>>0)):[],group:a,kernel_shape:o?Array.from((w(),k).subarray(Number(o)>>>0,Number(s)>>>0)):[],pads:c?Array.from((w(),k).subarray(Number(c)>>>0,Number(l)>>>0)):[],strides:u?Array.from((w(),k).subarray(Number(u)>>>0,Number(d)>>>0)):[],w_is_const:()=>!!(w(),T)[Number(p)>>>0],activation:P(m),activation_params:h?Array.from((w(),te).subarray(Number(h)>>>0,Number(g)>>>0)):[]})},952327:e=>{t.ac(`Gelu`,e,void 0)},952379:(e,n,r,i,a,o,s,c,l)=>{t.ac(`GroupQueryAttention`,e,{numHeads:n,kvNumHeads:r,scale:i,softcap:a,doRotary:o,rotaryInterleaved:s,smoothSoftmax:c,localWindowSize:l})},952596:(e,n,r,i)=>{t.ac(`LayerNormalization`,e,{axis:n,epsilon:r,simplified:!!i})},952707:(e,n,r,i)=>{t.ac(`LayerNormalization`,e,{axis:n,epsilon:r,simplified:!!i})},952818:(e,n,r,i,a,o)=>{t.ac(`MatMulNBits`,e,{k:n,n:r,accuracyLevel:i,bits:a,blockSize:o})},952945:(e,n,r,i,a,o)=>{t.ac(`MultiHeadAttention`,e,{numHeads:n,isUnidirectional:r,maskFilterValue:i,scale:a,doRotary:o})},953104:(e,n)=>{t.ac(`QuickGelu`,e,{alpha:n})},953168:(e,n,r,i,a)=>{t.ac(`RotaryEmbedding`,e,{interleaved:!!n,numHeads:r,rotaryEmbeddingDim:i,scale:a})},953307:(e,n,r)=>{t.ac(`SkipLayerNormalization`,e,{epsilon:n,simplified:!!r})},953409:(e,n,r)=>{t.ac(`SkipLayerNormalization`,e,{epsilon:n,simplified:!!r})},953511:(e,n,r,i)=>{t.ac(`GatherBlockQuantized`,e,{gatherAxis:n,quantizeAxis:r,blockSize:i})},953632:e=>{t.Id(e)},953666:(e,n)=>t.Kd(Number(e),Number(n),t.$c.Nd,t.$c.errors)};function vi(e,n,r){return cn(async()=>{await t.Gd(Number(e),Number(n),Number(r))})}function yi(){return typeof wasmOffsetConverter<`u`}function bi(e,t,n,r){var i=Q();try{return Lr(e,t,n,r)}catch(e){if(Z(i),e!==e+0)throw e;X(1,0)}}function xi(e,t,n){var r=Q();try{return Nr(e,t,n)}catch(e){if(Z(r),e!==e+0)throw e;X(1,0)}}function Si(e,t,n){var r=Q();try{Or(e,t,n)}catch(e){if(Z(r),e!==e+0)throw e;X(1,0)}}function Ci(e,t){var n=Q();try{return kr(e,t)}catch(e){if(Z(n),e!==e+0)throw e;X(1,0)}}function wi(e){var t=Q();try{Ar(e)}catch(e){if(Z(t),e!==e+0)throw e;X(1,0)}}function Ti(e,t,n,r,i,a,o){var s=Q();try{return Fr(e,t,n,r,i,a,o)}catch(e){if(Z(s),e!==e+0)throw e;X(1,0)}}function Ei(e,t){var n=Q();try{Rr(e,t)}catch(e){if(Z(n),e!==e+0)throw e;X(1,0)}}function Di(e,t,n,r,i,a){var o=Q();try{jr(e,t,n,r,i,a)}catch(e){if(Z(o),e!==e+0)throw e;X(1,0)}}function Oi(e,t,n,r){var i=Q();try{Ir(e,t,n,r)}catch(e){if(Z(i),e!==e+0)throw e;X(1,0)}}function ki(e,t,n,r,i){var a=Q();try{Mr(e,t,n,r,i)}catch(e){if(Z(a),e!==e+0)throw e;X(1,0)}}function Ai(e,t,n,r,i,a,o){var s=Q();try{Br(e,t,n,r,i,a,o)}catch(e){if(Z(s),e!==e+0)throw e;X(1,0)}}function ji(e,t,n,r,i,a,o){var s=Q();try{Vr(e,t,n,r,i,a,o)}catch(e){if(Z(s),e!==e+0)throw e;X(1,0)}}function Mi(e,t,n,r,i,a,o,s){var c=Q();try{Gr(e,t,n,r,i,a,o,s)}catch(e){if(Z(c),e!==e+0)throw e;X(1,0)}}function Ni(e,t,n,r,i){var a=Q();try{return zr(e,t,n,r,i)}catch(e){if(Z(a),e!==e+0)throw e;X(1,0)}}function Pi(e,t,n,r,i,a,o,s){var c=Q();try{Kr(e,t,n,r,i,a,o,s)}catch(e){if(Z(c),e!==e+0)throw e;X(1,0)}}function Fi(e,t,n,r,i,a,o,s,c,l,u,d){var f=Q();try{Hr(e,t,n,r,i,a,o,s,c,l,u,d)}catch(e){if(Z(f),e!==e+0)throw e;X(1,0)}}function Ii(e,t,n,r,i,a){var o=Q();try{return Ur(e,t,n,r,i,a)}catch(e){if(Z(o),e!==e+0)throw e;X(1,0)}}function Li(e,t,n){var r=Q();try{return qr(e,t,n)}catch(e){if(Z(r),e!==e+0)throw e;return X(1,0),0n}}function Ri(e,t,n,r,i,a,o,s,c){var l=Q();try{Pr(e,t,n,r,i,a,o,s,c)}catch(e){if(Z(l),e!==e+0)throw e;X(1,0)}}function zi(e){var t=Q();try{return Jr(e)}catch(e){if(Z(t),e!==e+0)throw e;X(1,0)}}function Bi(e,t,n){var r=Q();try{return Yr(e,t,n)}catch(e){if(Z(r),e!==e+0)throw e;X(1,0)}}function Vi(e,t){var n=Q();try{return ui(e,t)}catch(e){if(Z(n),e!==e+0)throw e;return X(1,0),0n}}function Hi(e,t,n,r,i){var a=Q();try{Xr(e,t,n,r,i)}catch(e){if(Z(a),e!==e+0)throw e;X(1,0)}}function Ui(e){var t=Q();try{return Zr(e)}catch(e){if(Z(t),e!==e+0)throw e;return X(1,0),0n}}function Wi(e,t,n,r,i,a){var o=Q();try{return ri(e,t,n,r,i,a)}catch(e){if(Z(o),e!==e+0)throw e;X(1,0)}}function Gi(e,t,n,r,i,a){var o=Q();try{return $(e,t,n,r,i,a)}catch(e){if(Z(o),e!==e+0)throw e;X(1,0)}}function Ki(e,t,n,r,i,a,o,s){var c=Q();try{return Wr(e,t,n,r,i,a,o,s)}catch(e){if(Z(c),e!==e+0)throw e;X(1,0)}}function qi(e,t,n,r,i){var a=Q();try{return ii(e,t,n,r,i)}catch(e){if(Z(a),e!==e+0)throw e;return X(1,0),0n}}function Ji(e,t,n,r){var i=Q();try{return ai(e,t,n,r)}catch(e){if(Z(i),e!==e+0)throw e;X(1,0)}}function Yi(e,t,n,r){var i=Q();try{return oi(e,t,n,r)}catch(e){if(Z(i),e!==e+0)throw e;X(1,0)}}function Xi(e,t,n,r,i,a,o,s,c,l,u,d){var f=Q();try{return si(e,t,n,r,i,a,o,s,c,l,u,d)}catch(e){if(Z(f),e!==e+0)throw e;X(1,0)}}function Zi(e,t,n,r,i,a,o,s,c,l,u){var d=Q();try{ti(e,t,n,r,i,a,o,s,c,l,u)}catch(e){if(Z(d),e!==e+0)throw e;X(1,0)}}function Qi(e,t,n,r,i,a,o,s,c,l,u,d,f,p,m,h){var g=Q();try{ni(e,t,n,r,i,a,o,s,c,l,u,d,f,p,m,h)}catch(e){if(Z(g),e!==e+0)throw e;X(1,0)}}function $i(e,t,n,r){var i=Q();try{return ci(e,t,n,r)}catch(e){if(Z(i),e!==e+0)throw e;X(1,0)}}function ea(e,t,n,r,i){var a=Q();try{return li(e,t,n,r,i)}catch(e){if(Z(a),e!==e+0)throw e;X(1,0)}}function ta(e,t,n){var r=Q();try{return Qr(e,t,n)}catch(e){if(Z(r),e!==e+0)throw e;X(1,0)}}function na(e,t,n){var r=Q();try{return $r(e,t,n)}catch(e){if(Z(r),e!==e+0)throw e;X(1,0)}}function ra(e,t,n,r){var i=Q();try{ei(e,t,n,r)}catch(e){if(Z(i),e!==e+0)throw e;X(1,0)}}function ia(){if(0<he)ge=ia;else if(i)h?.(t),ce();else{for(var e=me;0<e.length;)e.shift()(t);0<he?ge=ia:(t.calledRun=!0,S||(ce(),h?.(t)))}}return i||(hi=await de(),ia()),t.PTR_SIZE=4,oe?t:new Promise((e,t)=>{h=e,g=t})}var Le,Re,ze=o(()=>{Le=Ie,Re=globalThis.self?.name?.startsWith(`em-pthread`),Re&&Ie()}),Be,Ve,He,Ue,We,Ge,Ke,qe,Je,Ye,Xe,P,Ze,Qe,$e=o(()=>{ke(),Be=typeof location>`u`?void 0:location.origin,Ve=import.meta.url>`file:`&&import.meta.url<`file;`,He=()=>Ve?new URL(new URL(`ort.bundle.min.mjs`,import.meta.url).href,Be).href:import.meta.url,Ue=He(),We=()=>{if(Ue&&!Ue.startsWith(`blob:`))return Ue.substring(0,Ue.lastIndexOf(`/`)+1)},Ge=(e,t)=>{try{let n=t??Ue;return(n?new URL(e,n):new URL(e)).origin===Be}catch{return!1}},Ke=(e,t)=>{let n=t??Ue;try{return(n?new URL(e,n):new URL(e)).href}catch{return}},qe=(e,t)=>`${t??`./`}${e}`,Je=async e=>{let t=await(await fetch(e,{credentials:`same-origin`})).blob();return URL.createObjectURL(t)},Ye=async e=>(await import(e)).default,Xe=(Pe(),l(Ae)).default,P=async()=>{if(!Ue)throw Error(`Failed to load proxy worker: cannot determine the script source URL.`);if(Ge(Ue))return[void 0,Xe()];let e=await Je(Ue);return[e,Xe(e)]},Ze=(ze(),l(Fe)).default,Qe=async(e,t,n,r)=>{let i=Ze&&!(e||t);if(i)if(Ue)i=Ge(Ue)||r&&!n;else if(r&&!n)i=!0;else throw Error(`cannot determine the script source URL.`);if(i)return[void 0,Ze];{let r=`ort-wasm-simd-threaded.jsep.mjs`,i=e??Ke(r,t),a=n&&i&&!Ge(i,t),o=a?await Je(i):i??qe(r,t);return[a?o:void 0,await Ye(o)]}}}),et,tt,nt,rt,it,at,ot,st,F,ct=o(()=>{$e(),tt=!1,nt=!1,rt=!1,it=()=>{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}},at=()=>{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}},ot=()=>{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}},st=async e=>{if(tt)return Promise.resolve();if(nt)throw Error(`multiple calls to 'initializeWebAssembly()' detected.`);if(rt)throw Error(`previous call to 'initializeWebAssembly()' failed.`);nt=!0;let t=e.initTimeout,n=e.numThreads;if(e.simd!==!1){if(e.simd===`relaxed`){if(!ot())throw Error(`Relaxed WebAssembly SIMD is not supported in the current environment.`)}else if(!at())throw Error(`WebAssembly SIMD is not supported in the current environment.`)}let r=it();n>1&&!r&&(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 i=e.wasmPaths,a=typeof i==`string`?i:void 0,o=i?.mjs,s=o?.href??o,c=i?.wasm,l=c?.href??c,u=e.wasmBinary,[d,f]=await Qe(s,a,n>1,!!u||!!l),p=!1,m=[];if(t>0&&m.push(new Promise(e=>{setTimeout(()=>{p=!0,e()},t)})),m.push(new Promise((e,t)=>{let r={numThreads:n};if(u)r.wasmBinary=u,r.locateFile=e=>e;else if(l||a)r.locateFile=e=>l??a+e;else if(s&&s.indexOf(`blob:`)!==0)r.locateFile=e=>new URL(e,s).href;else if(d){let e=We();e&&(r.locateFile=t=>e+t)}f(r).then(t=>{nt=!1,tt=!0,et=t,e(),d&&URL.revokeObjectURL(d)},e=>{nt=!1,rt=!0,t(e)})})),await Promise.race(m),p)throw Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},F=()=>{if(tt&&et)return et;throw Error(`WebAssembly is not initialized yet.`)}}),lt,ut,I,dt=o(()=>{ct(),lt=(e,t)=>{let n=F(),r=n.lengthBytesUTF8(e)+1,i=n._malloc(r);return n.stringToUTF8(e,i,r),t.push(i),i},ut=(e,t,n,r)=>{if(typeof e==`object`&&e){if(n.has(e))throw Error(`Circular reference in options`);n.add(e)}Object.entries(e).forEach(([e,i])=>{let a=t?t+e:e;if(typeof i==`object`)ut(i,a+`.`,n,r);else if(typeof i==`string`||typeof i==`number`)r(a,i.toString());else if(typeof i==`boolean`)r(a,i?`1`:`0`);else throw Error(`Can't handle extra config type: ${typeof i}`)})},I=e=>{let t=F(),n=t.stackSave();try{let n=t.PTR_SIZE,r=t.stackAlloc(2*n);t._OrtGetLastError(r,r+n);let i=Number(t.getValue(r,n===4?`i32`:`i64`)),a=t.getValue(r+n,`*`),o=a?t.UTF8ToString(a):``;throw Error(`${e} ERROR_CODE: ${i}, ERROR_MESSAGE: ${o}`)}finally{t.stackRestore(n)}}}),ft,pt=o(()=>{ct(),dt(),ft=e=>{let t=F(),n=0,r=[],i=e||{};try{if(e?.logSeverityLevel===void 0)i.logSeverityLevel=2;else if(typeof e.logSeverityLevel!=`number`||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw Error(`log severity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)i.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!=`number`||!Number.isInteger(e.logVerbosityLevel))throw Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(i.terminate=!1);let a=0;return e?.tag!==void 0&&(a=lt(e.tag,r)),n=t._OrtCreateRunOptions(i.logSeverityLevel,i.logVerbosityLevel,!!i.terminate,a),n===0&&I(`Can't create run options.`),e?.extra!==void 0&&ut(e.extra,``,new WeakSet,(e,i)=>{let a=lt(e,r),o=lt(i,r);t._OrtAddRunConfigEntry(n,a,o)!==0&&I(`Can't set a run config entry: ${e} - ${i}.`)}),[n,r]}catch(e){throw n!==0&&t._OrtReleaseRunOptions(n),r.forEach(e=>t._free(e)),e}}}),mt,ht,gt,_t,vt,yt,bt=o(()=>{ct(),dt(),mt=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 Error(`unsupported graph optimization level: ${e}`)}},ht=e=>{switch(e){case`sequential`:return 0;case`parallel`:return 1;default:throw Error(`unsupported execution mode: ${e}`)}},gt=e=>{e.extra||={},e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||=`1`,e.executionProviders&&e.executionProviders.some(e=>(typeof e==`string`?e:e.name)===`webgpu`)&&(e.enableMemPattern=!1)},_t=(e,t,n,r)=>{let i=lt(t,r),a=lt(n,r);F()._OrtAddSessionConfigEntry(e,i,a)!==0&&I(`Can't set a session config entry: ${t} - ${n}.`)},vt=async(e,t,n)=>{let r=t.executionProviders;for(let t of r){let r=typeof t==`string`?t:t.name,i=[];switch(r){case`webnn`:if(r=`WEBNN`,typeof t!=`string`){let r=t?.deviceType;r&&_t(e,`deviceType`,r,n)}break;case`webgpu`:if(r=`JS`,typeof t!=`string`){let r=t;if(r?.preferredLayout){if(r.preferredLayout!==`NCHW`&&r.preferredLayout!==`NHWC`)throw Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${r.preferredLayout}`);_t(e,`preferredLayout`,r.preferredLayout,n)}}break;case`wasm`:case`cpu`:continue;default:throw Error(`not supported execution provider: ${r}`)}let a=lt(r,n),o=i.length,s=0,c=0;if(o>0){s=F()._malloc(o*F().PTR_SIZE),n.push(s),c=F()._malloc(o*F().PTR_SIZE),n.push(c);for(let e=0;e<o;e++)F().setValue(s+e*F().PTR_SIZE,i[e][0],`*`),F().setValue(c+e*F().PTR_SIZE,i[e][1],`*`)}await F()._OrtAppendExecutionProvider(e,a,s,c,o)!==0&&I(`Can't append execution provider: ${r}.`)}},yt=async e=>{let t=F(),n=0,r=[],i=e||{};gt(i);try{let e=mt(i.graphOptimizationLevel??`all`),a=ht(i.executionMode??`sequential`),o=typeof i.logId==`string`?lt(i.logId,r):0,s=i.logSeverityLevel??2;if(!Number.isInteger(s)||s<0||s>4)throw Error(`log severity level is not valid: ${s}`);let c=i.logVerbosityLevel??0;if(!Number.isInteger(c)||c<0||c>4)throw Error(`log verbosity level is not valid: ${c}`);let l=typeof i.optimizedModelFilePath==`string`?lt(i.optimizedModelFilePath,r):0;if(n=t._OrtCreateSessionOptions(e,!!i.enableCpuMemArena,!!i.enableMemPattern,a,!!i.enableProfiling,0,o,s,c,l),n===0&&I(`Can't create session options.`),i.executionProviders&&await vt(n,i,r),i.enableGraphCapture!==void 0){if(typeof i.enableGraphCapture!=`boolean`)throw Error(`enableGraphCapture must be a boolean value: ${i.enableGraphCapture}`);_t(n,`enableGraphCapture`,i.enableGraphCapture.toString(),r)}if(i.freeDimensionOverrides)for(let[e,a]of Object.entries(i.freeDimensionOverrides)){if(typeof e!=`string`)throw Error(`free dimension override name must be a string: ${e}`);if(typeof a!=`number`||!Number.isInteger(a)||a<0)throw Error(`free dimension override value must be a non-negative integer: ${a}`);let i=lt(e,r);t._OrtAddFreeDimensionOverride(n,i,a)!==0&&I(`Can't set a free dimension override: ${e} - ${a}.`)}return i.extra!==void 0&&ut(i.extra,``,new WeakSet,(e,t)=>{_t(n,e,t,r)}),[n,r]}catch(e){throw n!==0&&t._OrtReleaseSessionOptions(n)!==0&&I(`Can't release session options.`),r.forEach(e=>t._free(e)),e}}}),xt,St,Ct,wt,Tt,Et,Dt,Ot,L=o(()=>{xt=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 Error(`unsupported data type: ${e}`)}},St=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 Error(`unsupported data type: ${e}`)}},Ct=(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],r=typeof t==`number`?t:t.reduce((e,t)=>e*t,1);return n>0?Math.ceil(r*n):void 0},wt=e=>{switch(e){case`float16`:return typeof Float16Array<`u`&&Float16Array.from?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 Error(`unsupported type: ${e}`)}},Tt=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 Error(`unsupported logging level: ${e}`)}},Et=e=>e===`float32`||e===`float16`||e===`int32`||e===`int64`||e===`uint32`||e===`uint8`||e===`bool`||e===`uint4`||e===`int4`,Dt=e=>e===`float32`||e===`float16`||e===`int32`||e===`int64`||e===`uint32`||e===`uint64`||e===`int8`||e===`uint8`||e===`bool`||e===`uint4`||e===`int4`,Ot=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 Error(`unsupported data location: ${e}`)}}}),kt,At=o(()=>{ke(),kt=async e=>{if(typeof e==`string`){let t=await fetch(e);if(!t.ok)throw Error(`failed to load external data file: ${e}`);let n=t.headers.get(`Content-Length`),r=n?parseInt(n,10):0;if(r<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw Error(`failed to load external data file: ${e}, no response body.`);let n=t.body.getReader(),i;try{i=new ArrayBuffer(r)}catch(e){if(e instanceof RangeError){let e=Math.ceil(r/65536);i=new WebAssembly.Memory({initial:e,maximum:e}).buffer}else throw e}let a=0;for(;;){let{done:e,value:t}=await n.read();if(e)break;let r=t.byteLength;new Uint8Array(i,a,r).set(t),a+=r}return new Uint8Array(i,0,r)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),jt,Mt,Nt,Pt,Ft,It,R,Lt=o(()=>{L(),jt=[`V`,`I`,`W`,`E`,`F`],Mt=(e,t)=>{console.log(`[${jt[e]},${new Date().toISOString()}]${t}`)},Ft=(e,t)=>{Nt=e,Pt=t},It=(e,t)=>{let n=Tt(e);n>=Tt(Nt)&&Mt(n,typeof t==`function`?t():t)},R=(...e)=>{Pt&&It(...e)}}),Rt,zt,z,Bt,Vt,Ht,Ut,B=o(()=>{Rt=class{static calcMatMulShape(e,t){return e[1]===t[0]?[e[0],t[1]]:void 0}},zt=class{static calcShape(e,t,n=!1){let r=e.length,i=t.length;if(r===0)return t;if(i===0)return e;let a=Math.max(e.length,t.length),o=Array(a);if(n){if(r<2||i<2)return;let n=Rt.calcMatMulShape([e[r-2],e[r-1]],[t[i-2],t[i-1]]);if(n===void 0)return;[o[a-2],o[a-1]]=n}for(let s=n?3:1;s<=a;s++){let n=r-s<0?1:e[r-s],c=i-s<0?1:t[i-s];if(n!==c&&n>1&&c>1)return;let l=Math.max(n,c);if(n&&c)o[a-s]=Math.max(n,c);else{if(l>1)return;o[a-s]=0}}return o}static isValidBroadcast(e,t){let n=e.length,r=t.length;if(n>r)return!1;for(let i=1;i<=n;i++)if(e[n-i]!==1&&e[n-i]!==t[r-i])return!1;return!0}},z=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(e,t=4){let n=e.length;if(n===0)return[];let r=Array(n),i=n-1;for(;i>=0;){if(e[i]%t===0){r[i]=e[i]/t;break}if(t%e[i]!==0)throw Error(`cannot convert shape`);r[i]=1,t/=e[i],i--}for(i--;i>=0;i--)r[i]=e[i];return r}static sizeFromDimension(t,n){if(n<0||n>t.length)throw Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(e,t,n){let r=1;for(let i=t;i<n;i++){if(e[i]<0)throw Error(`cannot get valid size from specified dimension range. Most likely the range contains negative values in them.`);r*=Number(e[i])}return r}static computeStrides(e){let t=e.length;if(t===0)return[];if(t===1)return[1];let n=Array(t);n[t-1]=1,n[t-2]=e[t-1];for(let r=t-3;r>=0;--r)n[r]=n[r+1]*e[r+1];return n}static normalizeAxis(e,t){if(e<-t&&e>=t)throw Error(`unsupported axis for this operation.`);return e<0?e+t:e}static normalizeAxes(e,t){return e.map(n=>this.normalizeAxis(n,t??e.length))}static sortBasedOnPerm(e,t){return t?t.map(t=>e[t]):e.slice().reverse()}static padShape(e,t){let n=e.length;return e.map((e,r)=>e+t[r]+t[r+n])}static areEqual(e,t){return e.length===t.length?e.every((e,n)=>e===t[n]):!1}},Bt=class e{static adjustPoolAttributes(e,t,n,r,i,a){if(!e&&n.length!==t.length-2)throw Error(`length of specified kernel shapes should be 2 less than length of input dimensions`);if(e)for(let e=0;e<t.length-2;e++)e>=n.length?n.push(t[e+2]):n[e]=t[e+2];for(let e=0;e<n.length;e++)if(e<r.length){if(r[e]<0)throw Error(`strides should be greater than or equal to 1`)}else r.push(1);for(let e=0;e<n.length;e++)if(e<i.length){if(i[e]<0)throw Error(`dilations should be greater than or equal to 1`)}else i.push(1);for(let e=0;e<n.length*2;e++)if(e<a.length){if(a[e]<0)throw Error(`pad should be greater than or equal to 1`)}else a.push(0);for(let e=0;e<n.length;e++){if(n[e]<=0)throw Error(`kernel shapes need to be greater than 0`);if(a[e]>=n[e]||a[e+n.length]>=n[e])throw Error(`pads should be smaller than kernel`)}}static adjustPadsBasedOnAutoPad(t,n,r,i,a,o,s){if(s){if(a.length!==2*(t.length-2))throw Error(`length of pads should be twice the length of data dimensions`);if(n.length!==t.length-2)throw Error(`length of strides should be the length of data dimensions`);if(i.length!==t.length-2)throw Error(`length of kernel shapes should be the length of data dimensions`);for(let c=0;c<t.length-2;c++)e.adjustPadAndReturnShape(t[c+(o?1:2)],n[c],r[c],i[c],a,c,c+t.length-2,s)}}static computePoolOutputShape(t,n,r,i,a,o,s){if(n.length<=0)throw Error(`input shape must be of size greater than 0`);let c=[n[0],n[1]];return e.computeShapeHelper(t,n,c,r,i,a,o,s),c}static computeConvOutputShape(t,n,r,i,a,o,s){if(t.length<=0||n.length<=0)throw Error(`invalid input tensor dims or invalid filter tensor dims`);let c=[t[0],n[0]];return e.computeShapeHelper(!1,t,c,r,i,a,o,s),c}static computeShapeHelper(t,n,r,i,a,o,s,c){if(t)for(let e=0;e<n.length-2;e++)r.push(1);else for(let t=0;t<n.length-2;t++)r.push(e.adjustPadAndReturnShape(n[t+2],i[t],a[t],o[t],s,t,t+n.length-2,c))}static adjustPadAndReturnShape(e,t,n,r,i,a,o,s){let c=n*(r-1)+1;if(s&&s!==`NOTSET`)switch(s){case`VALID`:return i[a]=0,i[o]=0,Math.floor((e-c)/t+1);case`SAME_LOWER`:case`SAME_UPPER`:if(n!==1)throw Error(`Dilation not supported for SAME_UPPER or SAME_LOWER`);{let n=((e+t-1)/t-1)*t+r-e;return i[a]=Math.floor(s===`SAME_LOWER`?(n+1)/2:n/2),i[o]=n-i[a],Math.floor((e+n-r)/t+1)}default:throw Error(`Unsupported AutoPad type`)}else return Math.floor((e+i[a]+i[o]-c)/t+1)}},Vt=class{static getShapeOfGemmResult(e,t,n,r,i){if(e.length!==2||n.length!==2)throw Error(`shape need to be of size 2`);let a,o,s;t?(a=e[1],o=e[0]):(a=e[0],o=e[1]);let c=-1;if(r?(s=n[0],c=1):(s=n[1],c=0),n[c]!==o)throw Error(`dimension mismatch`);if(a<=0||s<=0||o<=0)throw Error(`invalid shape specified`);if(i&&!zt.isValidBroadcast(i,[a,s]))throw Error(`gemm: invalid bias shape for broadcast`);return[a,s,o]}},Ht=-34028234663852886e22,Ut=34028234663852886e22}),Wt,Gt=o(()=>{L(),Wt=(e,t)=>new(wt(t))(e)}),Kt,qt,Jt,Yt,Xt,Zt,Qt,$t,en,tn,nn,rn=o(()=>{L(),Lt(),Kt=new Map([[`float32`,32],[`float16`,16],[`int32`,32],[`uint32`,32],[`int64`,64],[`uint64`,64],[`int8`,8],[`uint8`,8],[`int4`,4],[`uint4`,4]]),qt=(e,t)=>{if(t===`int32`)return e;let n=Kt.get(t);if(!n)throw Error(`WebNN backend does not support data type: ${t}`);let r=n/8;if(e.byteLength%r!==0)throw Error(`Invalid Uint8Array length - must be a multiple of ${r}.`);let i=e.byteLength/r,a=new(wt(t))(e.buffer,e.byteOffset,i);switch(t){case`int64`:case`uint64`:{let e=new Int32Array(i);for(let t=0;t<i;t++){let n=a[t];if(n>2147483647n||n<-2147483648n)throw Error(`Can not convert int64 data to int32 - value out of range.`);e[t]=Number(n)}return new Uint8Array(e.buffer)}case`int8`:case`uint8`:case`uint32`:{if(t===`uint32`&&a.some(e=>e>2147483647))throw Error(`Can not convert uint32 data to int32 - value out of range.`);let e=Int32Array.from(a,Number);return new Uint8Array(e.buffer)}default:throw Error(`Unsupported data conversion from ${t} to 'int32'`)}},Jt=(e,t)=>{if(t===`int32`)return e;if(e.byteLength%4!=0)throw Error(`Invalid Uint8Array length - must be a multiple of 4 (int32).`);let n=e.byteLength/4,r=new Int32Array(e.buffer,e.byteOffset,n);switch(t){case`int64`:{let e=BigInt64Array.from(r,BigInt);return new Uint8Array(e.buffer)}case`uint64`:{if(r.some(e=>e<0))throw Error(`Can not convert int32 data to uin64 - negative value found.`);let e=BigUint64Array.from(r,BigInt);return new Uint8Array(e.buffer)}case`int8`:{if(r.some(e=>e<-128||e>127))throw Error(`Can not convert int32 data to int8 - value out of range.`);let e=Int8Array.from(r,Number);return new Uint8Array(e.buffer)}case`uint8`:if(r.some(e=>e<0||e>255))throw Error(`Can not convert int32 data to uint8 - value out of range.`);return Uint8Array.from(r,Number);case`uint32`:{if(r.some(e=>e<0))throw Error(`Can not convert int32 data to uint32 - negative value found.`);let e=Uint32Array.from(r,Number);return new Uint8Array(e.buffer)}default:throw Error(`Unsupported data conversion from 'int32' to ${t}`)}},Yt=1,Xt=()=>Yt++,Zt=new Map([[`int8`,`int32`],[`uint8`,`int32`],[`uint32`,`int32`],[`int64`,`int32`]]),Qt=(e,t)=>{let n=Kt.get(e);if(!n)throw Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((e,t)=>e*t)*n/8):0},$t=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:n,tensor:r,dataType:i,shape:a,fallbackDataType:o}=e;this.sessionId=t,this.mlContext=n,this.mlTensor=r,this.dataType=i,this.tensorShape=a,this.fallbackDataType=o}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return Qt(this.dataType,this.tensorShape)}destroy(){R(`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=Jt(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 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((e,t)=>e===n[t])}setIsDataConverted(e){this.isDataConverted=e}},en=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,r){let i=this.tensorManager.getMLContext(e),a=this.tensorManager.getMLOpSupportLimits(e),o;if(!a?.input.dataTypes.includes(t)){if(o=Zt.get(t),!o||a?.input.dataTypes.includes(o))throw Error(`WebNN backend does not support data type: ${t}`);R(`verbose`,()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${o}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(i,t,n))return this.wrapper.tensor;if(r){if(this.wrapper.byteLength!==Qt(t,n))throw Error(`Unable to copy data to tensor with different size.`);this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let s=typeof MLTensorUsage>`u`?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,n,s,!0,!0,o),r&&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=qt(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else R(`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?Jt(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 Error(`Tensor has not been created.`);return e?this.wrapper.read(e):this.wrapper.read()}},tn=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 Error(`MLContext not found for session.`);return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=Xt();return this.tensorTrackersById.set(e,new en(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,r,i){R(`verbose`,()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${n}, shape: ${r}, copyOld: ${i}}`);let a=this.tensorTrackersById.get(t);if(!a)throw Error(`Tensor not found.`);return a.ensureTensor(e,n,r,i)}upload(e,t){let n=this.tensorTrackersById.get(e);if(!n)throw Error(`Tensor not found.`);n.upload(t)}async download(e,t){R(`verbose`,()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t?.byteLength}}`);let n=this.tensorTrackersById.get(e);if(!n)throw 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,r){let i=this.getMLContext(e),a=Xt(),o=new $t({sessionId:e,context:i,tensor:t,dataType:n,shape:r});return this.tensorTrackersById.set(a,new en(this,o)),this.externalTensors.add(o),a}async getCachedTensor(e,t,n,r,i,a,o){let s=this.getMLContext(e);for(let[r,i]of this.freeTensors.entries())if(i.canReuseTensor(s,t,n)){R(`verbose`,()=>`[WebNN] Reusing tensor {dataType: ${t}, ${o?`fallbackDataType: ${o},`:``} shape: ${n}`);let i=this.freeTensors.splice(r,1)[0];return i.sessionId=e,i}R(`verbose`,()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${o?`fallbackDataType: ${o},`:``} shape: ${n}}`);let c=await s.createTensor({dataType:o??t,shape:n,dimensions:n,usage:r,writable:i,readable:a});return new $t({sessionId:e,context:s,tensor:c,dataType:t,shape:n,fallbackDataType:o})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},nn=(...e)=>new tn(...e)}),an,on,sn,cn=o(()=>{L(),ct(),Gt(),rn(),Lt(),an=new Map([[1,`float32`],[10,`float16`],[6,`int32`],[12,`uint32`],[7,`int64`],[13,`uint64`],[22,`int4`],[21,`uint4`],[3,`int8`],[2,`uint8`],[9,`uint8`]]),on=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let n=Object.keys(e).sort(),r=Object.keys(t).sort();return n.length===r.length&&n.every((n,i)=>n===r[i]&&e[n]===t[n])},sn=class{constructor(e){this.tensorManager=nn(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,Ft(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw Error(`No active session`);return this.activeSessionId}onRunStart(e){R(`verbose`,()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){R(`verbose`,()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let e of t)R(`verbose`,()=>`[WebNN] releasing temporary tensor {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let t=this.mlContextCache.findIndex(t=>t.gpuDevice===e);if(t!==-1)return this.mlContextCache[t].mlContext;{let t=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:t}),t}}else if(e===void 0){let e=this.mlContextCache.findIndex(e=>e.options===void 0&&e.gpuDevice===void 0);if(e!==-1)return this.mlContextCache[e].mlContext;{let e=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:e}),e}}let t=this.mlContextCache.findIndex(t=>on(t.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let t=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:t}),t}}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 e=this.mlContextCache.findIndex(e=>e.mlContext===t);e!==-1&&this.mlContextCache.splice(e,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){R(`verbose`,()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,n,r,i){let a=an.get(n);if(!a)throw Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,a,r,i)}async createTemporaryTensor(e,t,n){R(`verbose`,()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${n}}`);let r=an.get(t);if(!r)throw Error(`Unsupported ONNX data type: ${t}`);let i=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,i,r,n,!1);let a=this.temporarySessionTensorIds.get(e);return a?a.push(i):this.temporarySessionTensorIds.set(e,[i]),i}uploadTensor(e,t){if(!F().shouldTransferToMLTensor)throw Error(`Trying to upload to a MLTensor while shouldTransferToMLTensor is false`);R(`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 Wt(n,t)}}registerMLTensor(e,t,n,r){let i=an.get(n);if(!i)throw Error(`Unsupported ONNX data type: ${n}`);let a=this.tensorManager.registerTensor(e,t,i,r);return R(`verbose`,()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${i}, dimensions: ${r}} -> {tensorId: ${a}}`),a}registerMLConstant(e,t,n,r,i,a,o=!1){if(!a)throw Error(`External mounted files are not available.`);let s=e;e.startsWith(`./`)&&(s=e.substring(2));let c=a.get(s);if(!c)throw Error(`File with name ${s} not found in preloaded files.`);if(t+n>c.byteLength)throw Error(`Out of bounds: data offset and length exceed the external file data size.`);let l=c.slice(t,t+n).buffer,u;switch(i.dataType){case`float32`:u=new Float32Array(l);break;case`float16`:u=typeof Float16Array<`u`&&Float16Array.from?new Float16Array(l):new Uint16Array(l);break;case`int32`:u=new Int32Array(l);break;case`uint32`:u=new Uint32Array(l);break;case`int64`:if(o){let e=qt(new Uint8Array(l),`int64`);u=new Int32Array(e.buffer),i.dataType=`int32`}else u=new BigInt64Array(l);break;case`uint64`:u=new BigUint64Array(l);break;case`int8`:u=new Int8Array(l);break;case`int4`:case`uint4`:case`uint8`:u=new Uint8Array(l);break;default:throw Error(`Unsupported data type: ${i.dataType} in creating WebNN Constant from external data.`)}return R(`verbose`,()=>`[WebNN] registerMLConstant {dataType: ${i.dataType}, shape: ${i.shape}}} ${o?`(Note: it was int64 data type and registered to int32 as workaround)`:``}`),r.constant(i,u)}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 r=an.get(xt(t)),i=this.mlOpSupportLimitsBySessionId.get(e);return typeof r>`u`?!1:n?!!i?.input.dataTypes.includes(r):!!i?.output.dataTypes.includes(r)}flush(){}}}),ln=o(()=>{}),un,dn,fn,pn,mn,hn,gn,_n,vn,yn=o(()=>{Lt(),ln(),un=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]]),dn=[],fn=e=>Math.ceil(Number(e)/16)*16,pn=e=>{for(let t=0;t<dn.length;t++){let n=dn[t];if(e<=n)return n}return Math.ceil(e/16)*16},mn=1,hn=()=>mn++,gn=async(e,t,n,r)=>{let i=fn(n),a=e.device.createBuffer({size:i,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let o=e.getCommandEncoder();e.endComputePass(),o.copyBufferToBuffer(t,0,a,0,i),e.flush(),await a.mapAsync(GPUMapMode.READ);let s=a.getMappedRange();if(r){let e=r();return e.set(new Uint8Array(s,0,n)),e}else return new Uint8Array(s.slice(0,n))}finally{a.destroy()}},_n=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[e]of un)dn.push(e),this.freeBuffers.set(e,[]),this.freeUniformBuffers.set(e,[]);this.sessionCount=0}upload(e,t){let n=t.buffer,r=t.byteOffset,i=t.byteLength,a=fn(i),o=this.storageCache.get(e);if(!o)throw Error(`gpu data for uploading does not exist`);if(Number(o.originalSize)!==i)throw Error(`inconsistent data size. gpu data size=${o.originalSize}, data size=${i}`);let s=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),c=s.getMappedRange();new Uint8Array(c).set(new Uint8Array(n,r,i)),s.unmap();let l=this.backend.device.createCommandEncoder();l.copyBufferToBuffer(s,0,o.gpuData.buffer,0,a),this.backend.device.queue.submit([l.finish()]),s.destroy(),R(`verbose`,()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let n=this.storageCache.get(e);if(!n)throw Error(`source gpu data for memcpy does not exist`);let r=this.storageCache.get(t);if(!r)throw Error(`destination gpu data for memcpy does not exist`);if(n.originalSize!==r.originalSize)throw Error(`inconsistent source and destination gpu data size`);let i=fn(n.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(n.gpuData.buffer,0,r.gpuData.buffer,0,i)}registerExternalBuffer(e,t,n){let r;if(n){if(r=n[0],e===n[1])return R(`verbose`,()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${r}, buffer is the same, skip.`),r;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw Error(`Registering a different external buffer under graph capture mode is not supported yet.7             Please use the previous external buffer!`)}else r=hn();return this.storageCache.set(r,{gpuData:{id:r,type:0,buffer:e},originalSize:t}),R(`verbose`,()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${r}, registered.`),r}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),R(`verbose`,()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=pn(e),r,i=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||a){let e=(i?this.freeBuffers:this.freeUniformBuffers).get(n);r=e&&e.length>0?e.pop():this.backend.device.createBuffer({size:n,usage:t})}else r=this.backend.device.createBuffer({size:n,usage:t});let o={id:hn(),type:0,buffer:r};return this.storageCache.set(o.id,{gpuData:o,originalSize:Number(e)}),R(`verbose`,()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${o.id}`),o}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 Error(`releasing data does not exist`)}return R(`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 Error(`data does not exist`);await gn(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=un.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(e=>{e.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(e=>{e.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(e=>{e.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(e=>{e.destroy()}),this.capturedPendingBuffers.delete(e)),--this.sessionCount,this.sessionCount===0&&(R(`warning`,()=>`[WebGPU] Clearing webgpu buffer cache`),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.storageCache=new Map)}},vn=(...e)=>new _n(...e)}),bn,V,H=o(()=>{bn=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(`;`),this.key}},V=e=>new bn(e)}),xn,Sn,U,Cn,W,G,wn,Tn,En,K,Dn,q,J,On,kn,An,jn,Y=o(()=>{L(),B(),xn=64,Sn=(e,t)=>{if(t===3)throw Error(`vec3 has same alignment as vec4, use vec4 instead`);switch(Number(e)){case 10:return t>1?`vec${t}<f16>`:`f16`;case 1:return t>1?`vec${t}<f32>`:`f32`;case 6:return t>1?`vec${t}<i32>`:`i32`;case 12:return t>1?`vec${t}<u32>`:`u32`;case 7:if(t>1)throw Error(`currently not supported vecX of uint64 yet`);return[`vec2<u32>`,`i32`];case 13:if(t>1)throw Error(`currently not supported vecX of uint64 yet`);return[`vec2<u32>`,`u32`];case 9:if(t!==4)throw Error(`bool must be vec4`);return[`u32`,`vec4<bool>`];case 22:return`i32`;case 21:return`u32`;default:throw Error(`Unknown data type: ${e}`)}},U=(e,t=1)=>{let n=Sn(e,t);return typeof n==`string`?n:n[0]},Cn=(e,t=1)=>{let n=Sn(e,t);return typeof n==`string`?n:n[1]},W=(...e)=>{let t=[];return e.forEach(e=>{e.length!==0&&t.push({type:12,data:e},{type:12,data:z.computeStrides(e)})}),t},G=e=>e%4==0?4:e%2==0?2:1,wn=(e=`f32`,t,n=`0`)=>!t||t===1?`${e}(${n})`:`vec${t}<${e}>(${n})`,Tn=(e,t,n)=>e===`f32`?n:t===1?`f32(${n})`:`vec${t}<f32>(${n})`,En=(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,K=(e,t,n,r)=>e.startsWith(`uniforms.`)&&n>4?typeof t==`string`?r===`f16`?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:r===`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,Dn=(e,t,n,r,i)=>{let a=typeof n==`number`,o=a?n:n.length,s=[...Array(o).keys()],c=o<2?`u32`:o<=4?`vec${o}<u32>`:`array<u32, ${o}>`,l=Sn(t,i),u=typeof l==`string`?l:l[1],d={indices:c,value:u,storage:typeof l==`string`?l:l[0],tensor:t},f=e=>typeof e==`string`?e:`${e}u`,p={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},m=a?`uniforms.`:``,h=`${m}${e}_shape`,g=`${m}${e}_strides`,_=``;for(let e=0;e<o-1;e++)_+=`8    let dim${e} = current / ${K(g,e,o)};9    let rest${e} = current % ${K(g,e,o)};10    indices[${e}] = dim${e};11    current = rest${e};12    `;_+=`indices[${o-1}] = current;`;let v=o<2?``:`13  fn o2i_${e}(offset: u32) -> ${d.indices} {14    var indices: ${d.indices};15    var current = offset;16    ${_}17    return indices;18  }`,y=t=>(p.offsetToIndices=!0,o<2?t:`o2i_${e}(${t})`),b=[];if(o>=2)for(let e=o-1;e>=0;e--)b.push(`${K(g,e,o)} * (indices[${e}])`);let x=o<2?``:`19  fn i2o_${e}(indices: ${d.indices}) -> u32 {20    return ${b.join(`+`)};21  }`,S=t=>(p.indicesToOffset=!0,o<2?t:`i2o_${e}(${t})`),C=(...e)=>o===0?`0u`:`${d.indices}(${e.map(f).join(`,`)})`,w=(e,t)=>o<2?`${e}`:`${K(e,t,o)}`,ee=(e,t,n)=>o<2?`${e}=${n};`:`${K(e,t,o)}=${n};`,T={},E=(t,n)=>{p.broadcastedIndicesToOffset=!0;let r=`${n.name}broadcastedIndicesTo${e}Offset`;if(r in T)return`${r}(${t})`;let i=[];for(let e=o-1;e>=0;e--){let t=n.indicesGet(`outputIndices`,e+n.rank-o);i.push(`${w(g,e)} * (${t} % ${w(h,e)})`)}return T[r]=`fn ${r}(outputIndices: ${n.type.indices}) -> u32 {22             return ${i.length>0?i.join(`+`):`0u`};23           }`,`${r}(${t})`},D=(t,n)=>(()=>{if(d.storage===d.value)return`${e}[${t}]=${n};`;if(d.storage===`vec2<u32>`&&d.value===`i32`)return`${e}[${t}]=vec2<u32>(u32(${n}), select(0u, 0xFFFFFFFFu, ${n} < 0));`;if(d.storage===`vec2<u32>`&&d.value===`u32`)return`${e}[${t}]=vec2<u32>(u32(${n}), 0u);`;if(d.storage===`u32`&&d.value===`vec4<bool>`)return`${e}[${t}]=dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(${n}));`;throw Error(`not supported combination of storage type ${d.storage} and value type ${d.value} yet`)})(),O=t=>(()=>{if(d.storage===d.value)return`${e}[${t}]`;if(d.storage===`vec2<u32>`&&d.value===`i32`)return`i32(${e}[${t}].x)`;if(d.storage===`vec2<u32>`&&d.value===`u32`)return`u32(${e}[${t}].x)`;if(d.storage===`u32`&&d.value===`vec4<bool>`)return`vec4<bool>(bool(${e}[${t}] & 0xFFu), bool(${e}[${t}] & 0xFF00u), bool(${e}[${t}] & 0xFF0000u), bool(${e}[${t}] & 0xFF000000u))`;throw Error(`not supported combination of storage type ${d.storage} and value type ${d.value} yet`)})(),k=o<2?``:`24  fn get_${e}ByIndices(indices: ${d.indices}) -> ${u} {25    return ${O(`i2o_${e}(indices)`)};26  }`,A=o<2?``:`27  fn get_${e}(${s.map(e=>`d${e}: u32`).join(`, `)}) -> ${u} {28    return get_${e}ByIndices(${C(s.map(e=>`d${e}`).join(`, `))});29  }`,te=(...t)=>{if(t.length!==o)throw Error(`indices length must be ${o}`);let n=t.map(f).join(`,`);return o===0?O(`0u`):o===1?O(n[0]):(p.get=!0,p.getByIndices=!0,p.indicesToOffset=!0,`get_${e}(${n})`)},ne=t=>o<2?O(t):(p.getByIndices=!0,p.indicesToOffset=!0,`get_${e}ByIndices(${t})`),re=o<2?``:`30  fn set_${e}ByIndices(indices: ${d.indices}, value: ${u}) {31    ${D(`i2o_${e}(indices)`,`value`)}32  }`,ie=o<2?``:`33  fn set_${e}(${s.map(e=>`d${e}: u32`).join(`, `)}, value: ${u}) {34    set_${e}ByIndices(${C(s.map(e=>`d${e}`).join(`, `))}, value);35  }`;return{impl:()=>{let e=[],t=!1;return p.offsetToIndices&&(e.push(v),t=!0),p.indicesToOffset&&(e.push(x),t=!0),p.broadcastedIndicesToOffset&&(Object.values(T).forEach(t=>e.push(t)),t=!0),p.set&&(e.push(ie),t=!0),p.setByIndices&&(e.push(re),t=!0),p.get&&(e.push(A),t=!0),p.getByIndices&&(e.push(k),t=!0),!a&&t&&e.unshift(`const ${h} = ${d.indices}(${n.join(`,`)});`,`const ${g} = ${d.indices}(${z.computeStrides(n).join(`,`)});`),e.join(`36`)},type:d,offsetToIndices:y,indicesToOffset:S,broadcastedIndicesToOffset:E,indices:C,indicesGet:w,indicesSet:ee,set:(...t)=>{if(t.length!==o+1)throw Error(`indices length must be ${o}`);let n=t[o];if(typeof n!=`string`)throw Error(`value must be string`);let r=t.slice(0,o).map(f).join(`,`);return o===0?D(`0u`,n):o===1?D(r[0],n):(p.set=!0,p.setByIndices=!0,p.indicesToOffset=!0,`set_${e}(${r}, ${n})`)},setByOffset:D,setByIndices:(t,n)=>o<2?D(t,n):(p.setByIndices=!0,p.indicesToOffset=!0,`set_${e}ByIndices(${t}, ${n});`),get:te,getByOffset:O,getByIndices:ne,usage:r,name:e,strides:g,shape:h,rank:o}},q=(e,t,n,r=1)=>Dn(e,t,n,`input`,r),J=(e,t,n,r=1)=>Dn(e,t,n,`output`,r),On=(e,t,n)=>Dn(e,t,n,`atomicOutput`,1),kn=(e,t,n,r=1)=>Dn(e,t,n,`internal`,r),An=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=xn){let t=typeof e==`number`?e:e[0],n=typeof e==`number`?1:e[1],r=typeof e==`number`?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||r>this.limits.maxComputeWorkgroupSizeZ)throw Error(`workgroup size [${t}, ${n}, ${r}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*n*r>this.limits.maxComputeInvocationsPerWorkgroup)throw Error(`workgroup size [${t}, ${n}, ${r}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1;return`@compute @workgroup_size(${t}, ${n}, ${r})37  fn main(${i?`@builtin(global_invocation_id) global_id : vec3<u32>,38    @builtin(workgroup_id) workgroup_id : vec3<u32>,39    @builtin(local_invocation_index) local_idx : u32,40    @builtin(local_invocation_id) local_id : vec3<u32>`:`@builtin(global_invocation_id) global_id : vec3<u32>,41                                             @builtin(local_invocation_id) local_id : vec3<u32>,42    @builtin(local_invocation_index) local_idx : u32,43    @builtin(workgroup_id) workgroup_id : vec3<u32>,44    @builtin(num_workgroups) num_workgroups : vec3<u32>`}) {45    ${i?`let global_idx = global_id.x;46         let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] +47             workgroup_id.y * num_workgroups[0] + workgroup_id.x;48         let global_idx = workgroup_index * ${t*n*r}u + local_idx;`}49  `}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 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`,r=e.usage===`atomicOutput`?`atomic<i32>`:e.type.storage;return`@group(0) @binding(${t}) var<storage, ${n}> ${e.name}: array<${r}>;`}declareVariables(...e){return e.map(e=>this.declareVariable(e,this.variableIndex++)).join(`50`)}registerInternalVariable(e){if(e.usage!==`internal`)throw Error(`cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.`);this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(e=>this.registerInternalVariable(e)),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:r}of this.uniforms)if(r&&r>4)n===`f16`?e.push(`@align(16) ${t}:array<mat2x4<${n}>, ${Math.ceil(r/8)}>`):e.push(`${t}:array<vec4<${n}>, ${Math.ceil(r/4)}>`);else{let i=r==null||r===1?n:`vec${r}<${n}>`;e.push(`${t}:${i}`)}return`51      struct Uniforms { ${e.join(`, `)} };52      @group(0) @binding(${this.variableIndex}) var<uniform> uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(e=>e.impl()).join(`53`)+this.internalVariables.map(e=>e.impl()).join(`54`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=e=>[12,10,1,6][[`u32`,`f16`,`f32`,`i32`].indexOf(e)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},jn=(e,t)=>new An(e,t)}),Mn,Nn,Pn,Fn,In,Ln,Rn,zn,Bn,Vn=o(()=>{L(),B(),H(),Y(),Mn=(e,t)=>{if(!e||e.length!==1)throw Error(`Transpose requires 1 input.`);if(t.length!==0&&t.length!==e[0].dims.length)throw Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},Nn=(e,t)=>t.length===0?[...Array(e).keys()].reverse():t,Pn=(e,t)=>z.sortBasedOnPerm(e,Nn(e.length,t)),Fn=(e,t,n,r)=>{let i=`fn perm(i: ${r.type.indices}) -> ${n.type.indices} {55    var a: ${n.type.indices};`;for(let n=0;n<t;++n)i+=`a[${e[n]}]=i[${n}];`;return i+=`return a;}`},In=(e,t)=>{let n=[],r=[];for(let i=0;i<e.length;++i)e[i]!==1&&n.push(e[i]),e[t[i]]!==1&&r.push(t[i]);return{newShape:n,newPerm:r}},Ln=(e,t)=>{let n=0;for(let r=0;r<e.length;++r)if(t[e[r]]!==1){if(e[r]<n)return!1;n=e[r]}return!0},Rn=(e,t)=>{let n=e.dataType,r=e.dims.length,i=Nn(r,t),a=Pn(e.dims,i),o=e.dims,s=a,c=r<2||Ln(i,e.dims),l;if(c)return l=e=>{let t=q(`input`,n,o,4),r=J(`output`,n,s,4);return`56  ${e.registerUniform(`output_size`,`u32`).declareVariables(t,r)}57  ${e.mainStart()}58    ${e.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.output_size`)}59    output[global_idx] = input[global_idx];60  }`},{name:`TransposeCopy`,shaderCache:{inputDependencies:[`type`]},getRunData:()=>{let t=z.size(a);return{outputs:[{dims:a,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(t/64/4)},programUniforms:[{type:12,data:Math.ceil(t/4)}]}},getShaderSource:l};let{newShape:u,newPerm:d}=In(e.dims,i),f=z.areEqual(d,[2,3,1]),p=z.areEqual(d,[3,1,2]);return u.length===2||f||p?(o=f?[u[0],u[1]*u[2]]:p?[u[0]*u[1],u[2]]:u,s=[o[1],o[0]],l=e=>{let t=q(`a`,n,o.length),r=J(`output`,n,s.length);return`61  ${e.registerUniform(`output_size`,`u32`).declareVariables(t,r)}62  var<workgroup> tile : array<array<${r.type.value}, 17>, 16>;63  ${e.mainStart([16,16,1])}64    let stride = (uniforms.output_shape[1] - 1) / 16 + 1;65    let workgroup_id_x = workgroup_index % stride;66    let workgroup_id_y = workgroup_index / stride;67    let input_col = workgroup_id_y * 16u + local_id.x;68    let input_row = workgroup_id_x * 16u + local_id.y;69    if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) {70      tile[local_id.y][local_id.x] = ${t.getByIndices(`${t.type.indices}(input_row, input_col)`)};71    }72    workgroupBarrier();73 74    let output_col = workgroup_id_x * 16u + local_id.x;75    let output_row = workgroup_id_y * 16u + local_id.y;76    if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) {77      ${r.setByIndices(`${r.type.indices}(output_row, output_col)`,`tile[local_id.x][local_id.y]`)}78    }79  }`},{name:`TransposeShared`,shaderCache:{inputDependencies:[`type`]},getRunData:()=>{let t=z.size(a);return{outputs:[{dims:a,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(s[1]/16),y:Math.ceil(s[0]/16)},programUniforms:[{type:12,data:t},...W(o,s)]}},getShaderSource:l}):(l=e=>{let t=q(`a`,n,o.length),a=J(`output`,n,s.length);return`80  ${e.registerUniform(`output_size`,`u32`).declareVariables(t,a)}81 82  ${Fn(i,r,t,a)}83 84  ${e.mainStart()}85    ${e.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.output_size`)}86 87    let indices = ${a.offsetToIndices(`global_idx`)};88    let aIndices = perm(indices);89 90    ${a.setByOffset(`global_idx`,t.getByIndices(`aIndices`))}91  }`},{name:`Transpose`,shaderCache:{hint:`${t}`,inputDependencies:[`rank`]},getRunData:()=>{let t=z.size(a);return{outputs:[{dims:a,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(t/64)},programUniforms:[{type:12,data:t},...W(o,s)]}},getShaderSource:l})},zn=(e,t)=>{Mn(e.inputs,t.perm),e.compute(Rn(e.inputs[0],t.perm))},Bn=e=>V({perm:e.perm})}),Hn,Un,Wn,Gn,Kn,qn,Jn,Yn,Xn,Zn,Qn,$n,er,tr,nr,rr,ir,ar,or,sr,cr,lr=o(()=>{L(),B(),Y(),Nr(),Vn(),Hn={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`},Un={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`},Wn={max:`_A[offset]`,min:`_A[offset]`,mean:`0`,sum:`0`,prod:`1`,sumSquare:`0`,logSumExp:`0`,l1:`0`,l2:`0`,logSum:`0`},Gn={max:`bestValue`,min:`bestValue`,sum:`bestValue`,prod:`bestValue`,sumSquare:`bestValue`,logSumExp:`log(bestValue)`,l1:`bestValue`,l2:`sqrt(bestValue)`,logSum:`log(bestValue)`},Kn=(e,t)=>{let n=[];for(let r=t-e;r<t;++r)n.push(r);return n},qn=(e,t)=>{let n=[],r=e.length;for(let i=0;i<r;i++)t.indexOf(i)===-1&&n.push(e[i]);return[n,t.map(t=>e[t])]},Jn=(e,t)=>{let n=e.length+t.length,r=[],i=0;for(let a=0;a<n;a++)t.indexOf(a)===-1?r.push(e[i++]):r.push(1);return r},Yn=(e,t)=>{for(let n=0;n<e.length;++n)if(e[e.length-n-1]!==t-1-n)return!1;return!0},Xn=(e,t)=>{let n=[];if(!Yn(e,t)){for(let r=0;r<t;++r)e.indexOf(r)===-1&&n.push(r);e.forEach(e=>n.push(e))}return n},Zn=(e,t,n,r,i,a,o)=>{let s=n[0].dims,c=z.size(a),l=z.size(o),u=q(`_A`,n[0].dataType,s),d=J(`output`,i,a),f=64;c===1&&(f=256);let p=`92          var<workgroup> aBestValues : array<f32, ${f}>;93       `;return{name:e,shaderCache:{hint:`${t};${f}`,inputDependencies:[`type`]},getShaderSource:e=>`94        ${e.registerUniform(`reduceSize`,`u32`).declareVariables(u,d)}95        ${p}96        fn DIV_CEIL(a : u32, b : u32) -> u32 {97          return ((a - 1u) / b + 1u);98         }99         ${e.mainStart(f)}100 101          let outputIndex = global_idx / ${f};102          let offset = outputIndex * uniforms.reduceSize;103 104          var bestValue = f32(${Wn[r]});105          let Length = uniforms.reduceSize;106          for (var k = local_idx; k < Length; k = k + ${f}) {107           let candidate = f32(${u.getByOffset(`offset + k`)});108           bestValue = ${Hn[r]};109          }110          aBestValues[local_idx] = bestValue;111          workgroupBarrier();112 113         var reduceSize = min(Length, ${f}u);114         for (var currentSize = reduceSize / 2u; reduceSize > 1u;115             currentSize = reduceSize / 2u) {116           let interval = DIV_CEIL(reduceSize, 2u);117           if (local_idx < currentSize) {118            let candidate = aBestValues[local_idx + interval];119            bestValue = ${Un[r]};120            aBestValues[local_idx] = bestValue;121           }122           reduceSize = interval;123           workgroupBarrier();124         }125 126         if (local_idx == 0u) {127          ${d.setByOffset(`outputIndex`,`${r===`mean`?`${d.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${d.type.storage}(${Gn[r]})`}`)};128         }129        }`,getRunData:()=>({outputs:[{dims:a,dataType:i}],dispatchGroup:{x:c},programUniforms:[{type:12,data:l}]})}},Qn=(e,t,n,r)=>{let i=e.inputs.length===1?n:pr(e.inputs,n),a=i.axes;a.length===0&&!i.noopWithEmptyAxes&&(a=e.inputs[0].dims.map((e,t)=>t));let o=z.normalizeAxes(a,e.inputs[0].dims.length),s=o,c=e.inputs[0],l=Xn(s,e.inputs[0].dims.length);l.length>0&&(c=e.compute(Rn(e.inputs[0],l),{inputs:[0],outputs:[-1]})[0],s=Kn(s.length,c.dims.length));let[u,d]=qn(c.dims,s),f=u;i.keepDims&&(f=Jn(u,o)),e.compute(Zn(t,i.cacheKey,[c],r,e.inputs[0].dataType,f,d),{inputs:[c]})},$n=(e,t)=>{Qn(e,`ReduceMeanShared`,t,`mean`)},er=(e,t)=>{Qn(e,`ReduceL1Shared`,t,`l1`)},tr=(e,t)=>{Qn(e,`ReduceL2Shared`,t,`l2`)},nr=(e,t)=>{Qn(e,`ReduceLogSumExpShared`,t,`logSumExp`)},rr=(e,t)=>{Qn(e,`ReduceMaxShared`,t,`max`)},ir=(e,t)=>{Qn(e,`ReduceMinShared`,t,`min`)},ar=(e,t)=>{Qn(e,`ReduceProdShared`,t,`prod`)},or=(e,t)=>{Qn(e,`ReduceSumShared`,t,`sum`)},sr=(e,t)=>{Qn(e,`ReduceSumSquareShared`,t,`sumSquare`)},cr=(e,t)=>{Qn(e,`ReduceLogSumShared`,t,`logSum`)}}),ur,dr,fr,pr,mr,hr,gr,_r,vr,yr,br,X,xr,Sr,Z,Cr,Q,wr,Tr,Er,Dr,Or,kr,Ar,jr,Mr,Nr=o(()=>{L(),B(),H(),Y(),lr(),ur=e=>{if(!e||e.length===0||e.length>2)throw Error(`Reduce op requires 1 or 2 inputs.`);if(e.length===2&&e[1].dims.length!==1)throw Error(`Invalid axes input dims.`)},dr=e=>[``,``,`var value = ${e.getByIndices(`input_indices`)};`,``],fr=(e,t,n,r,i,a,o=!1,s=!1)=>{let c=[],l=n[0].dims,u=l.length,d=z.normalizeAxes(i,u),f=!s&&d.length===0;l.forEach((e,t)=>{f||d.indexOf(t)>=0?o&&c.push(1):c.push(e)});let p=c.length,m=z.size(c);return{name:e,shaderCache:t,getShaderSource:e=>{let t=[],i=q(`_A`,n[0].dataType,u),s=J(`output`,a,p),c=r(i,s,d),m=c[2];for(let e=0,n=0;e<u;e++)f||d.indexOf(e)>=0?(o&&n++,m=`for(var j${e}: u32 = 0; j${e} < ${l[e]}; j${e}++) {130                  ${c[2].includes(`last_index`)?`let last_index = j${e};`:``}131                  ${i.indicesSet(`input_indices`,e,`j${e}`)}132                  ${m}133                }`):(t.push(`${i.indicesSet(`input_indices`,e,s.indicesGet(`output_indices`,n))};`),n++);return`134 135        ${e.registerUniform(`output_size`,`u32`).declareVariables(i,s)}136 137        ${e.mainStart()}138          ${e.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.output_size`)}139          var input_indices: ${i.type.indices};140          let output_indices = ${s.offsetToIndices(`global_idx`)};141 142          ${t.join(`143`)}144          ${c[0]}       // init ops for reduce max/min145          ${c[1]}146          ${m}147          ${c[3]}148          ${c.length===4?s.setByOffset(`global_idx`,`value`):c.slice(4).join(`149`)}150        }`},getRunData:()=>({outputs:[{dims:c,dataType:a}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...W(l,c)]})}},pr=(e,t)=>{let n=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(e=>n.push(Number(e))),V({axes:n,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},mr=(e,t,n,r)=>{let i=e.inputs,a=i.length===1?n:pr(i,n);e.compute(fr(t,{hint:a.cacheKey,inputDependencies:[`rank`]},[i[0]],a.noopWithEmptyAxes&&a.axes.length===0?dr:r,a.axes,i[0].dataType,a.keepDims,a.noopWithEmptyAxes),{inputs:[0]})},hr=(e,t)=>{ur(e.inputs),mr(e,`ReduceLogSum`,t,(e,t)=>[`var value = ${t.type.storage}(0);`,``,`value += ${e.getByIndices(`input_indices`)};`,`value = log(value);`])},gr=(e,t)=>{ur(e.inputs),mr(e,`ReduceL1`,t,(e,t)=>[`var value = ${t.type.storage}(0);`,``,`value += abs(${e.getByIndices(`input_indices`)});`,``])},_r=(e,t)=>{ur(e.inputs),mr(e,`ReduceL2`,t,(e,t)=>[`var t = ${t.type.value}(0); var value = ${t.type.value}(0);`,``,`t = ${e.getByIndices(`input_indices`)}; value += (t * t);`,`value = sqrt(value);`])},vr=(e,t)=>{ur(e.inputs),mr(e,`ReduceLogSumExp`,t,(e,t)=>[`var value = ${t.type.storage}(0);`,``,`value += exp(${e.getByIndices(`input_indices`)});`,`value = log(value);`])},yr=(e,t)=>{ur(e.inputs),mr(e,`ReduceMax`,t,(e,t,n)=>{let r=[];for(let t=0;t<e.rank;t++)(n.indexOf(t)>=0||n.length===0)&&r.push(e.indicesSet(`input_indices`,t,0));return[`${r.join(`151`)}`,`var value = ${e.getByIndices(`input_indices`)};`,`value = max(value, ${e.getByIndices(`input_indices`)});`,``]})},br=(e,t)=>{ur(e.inputs),mr(e,`ReduceMean`,t,(t,n,r)=>{let i=1;for(let n=0;n<t.rank;n++)(r.indexOf(n)>=0||r.length===0)&&(i*=e.inputs[0].dims[n]);return[`var sum = f32(0);`,``,`sum += f32(${t.getByIndices(`input_indices`)});`,`let value = ${n.type.value}(sum / ${i});`]})},X=(e,t)=>{ur(e.inputs),mr(e,`ReduceMin`,t,(e,t,n)=>{let r=[];for(let t=0;t<e.rank;t++)(n.indexOf(t)>=0||n.length===0)&&r.push(`input_indices[${t}] = 0;`);return[`${r.join(`152`)}`,`var value = ${e.getByIndices(`input_indices`)};`,`value = min(value, ${e.getByIndices(`input_indices`)});`,``]})},xr=(e,t)=>{ur(e.inputs),mr(e,`ReduceProd`,t,(e,t)=>[`var value = ${t.type.storage}(1);`,``,`value *= ${e.getByIndices(`input_indices`)};`,``])},Sr=(e,t)=>{ur(e.inputs),mr(e,`ReduceSum`,t,(e,t)=>[`var value = ${t.type.storage}(0);`,``,`value += ${e.getByIndices(`input_indices`)};`,``])},Z=(e,t)=>{ur(e.inputs),mr(e,`ReduceSumSquare`,t,(e,t)=>[`var t = ${t.type.value}(0); var value = ${t.type.value}(0);`,``,`t = ${e.getByIndices(`input_indices`)}; value += t * t;`,``])},Cr=(e,t,n)=>{if(t.length===0)return n;let r=1,i=1;for(let n=0;n<t.length;n++)t.indexOf(n)===-1?r*=e[n]:i*=e[n];return i<32&&r>1024},Q=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?br(e,t):$n(e,t)},wr=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?gr(e,t):er(e,t)},Tr=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?_r(e,t):tr(e,t)},Er=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?vr(e,t):nr(e,t)},Dr=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?yr(e,t):rr(e,t)},Or=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?X(e,t):ir(e,t)},kr=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?xr(e,t):ar(e,t)},Ar=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Sr(e,t):or(e,t)},jr=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Z(e,t):sr(e,t)},Mr=(e,t)=>{Cr(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?hr(e,t):cr(e,t)}}),Pr,Fr,Ir,Lr,Rr=o(()=>{L(),H(),Nr(),Pr=e=>{if(!e||e.length===0||e.length>2)throw Error(`ArgMinMaxOp op requires 1 or 2 inputs.`);if(e[0].dataType!==1)throw Error(`Invalid input type.`)},Fr=(e,t)=>{Pr(e.inputs),e.compute(fr(`ArgMin`,{hint:t.cacheKey,inputDependencies:[`rank`]},[e.inputs[0]],(e,n,r)=>{let i=[];for(let t=0;t<e.rank;t++)(r.indexOf(t)>=0||r.length===0)&&i.push(`input_indices[${t}] = 0;`);return[`${i.join(`153`)}`,`var value = ${e.getByIndices(`input_indices`)};154var best_index : i32 = 0;`,`if (${e.getByIndices(`input_indices`)} ${t.selectLastIndex>0?`<=`:`<`} value) {155         value = ${e.getByIndices(`input_indices`)};156         best_index = i32(last_index);157       }`,``,n.setByOffset(`global_idx`,`best_index`)]},[t.axis],7,t.keepDims),{inputs:[0]})},Ir=(e,t)=>{Pr(e.inputs),e.compute(fr(`argMax`,{hint:t.cacheKey,inputDependencies:[`rank`]},[e.inputs[0]],(e,n,r)=>{let i=[];for(let t=0;t<e.rank;t++)(r.indexOf(t)>=0||r.length===0)&&i.push(`input_indices[${t}] = 0;`);return[`${i.join(`158`)}`,`var value = ${e.getByIndices(`input_indices`)};159var best_index : i32 = 0;`,`if (${e.getByIndices(`input_indices`)} ${t.selectLastIndex>0?`>=`:`>`} value) {160         value = ${e.getByIndices(`input_indices`)};161         best_index = i32(last_index);162       }`,``,n.setByOffset(`global_idx`,`best_index`)]},[t.axis],7,t.keepDims),{inputs:[0]})},Lr=e=>V(e)}),zr,Br,Vr,Hr,Ur,Wr,Gr,Kr,qr=o(()=>{L(),B(),ln(),Y(),zr=(e,t)=>{let n=e[0],r=e[1],i=e[2],a=e[3],o=e[4],s=e[5];if(o&&s)throw Error(`Attention cannot have both past and attention_bias`);if(n.dims.length!==3)throw Error(`Input "input" must have 3 dimensions`);let c=n.dims[0],l=n.dims[1],u=n.dims[2];if(i.dims.length!==1)throw Error(`Input "bias" is expected to have 1 dimensions`);if(r.dims.length!==2)throw Error(`Input "weights" is expected to have 2 dimensions`);if(r.dims[0]!==u)throw Error(`Input 1 dimension 0 should have same length as dimension 2 of input 0`);if(i.dims[0]!==r.dims[1])throw Error(`Input "bias" dimension 0 should have same length as dimension 1 of input "weights"`);let d=i.dims[0]/3,f=d,p=f;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw Error(`qkv_hidden_sizes attribute should have 3 elements`);for(let e of t.qkvHiddenSizes)if(e%t.numHeads!==0)throw Error(`qkv_hidden_sizes should be divisible by num_heads`);d=t.qkvHiddenSizes[0],f=t.qkvHiddenSizes[1],p=t.qkvHiddenSizes[2]}let m=l;if(d!==f)throw Error(`qkv_hidden_sizes first element should be same as the second`);if(i.dims[0]!==d+f+p)throw Error(`Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes`);let h=0;if(o){if(f!==p)throw Error(`Input "past" expect k_hidden_size == v_hidden_size`);if(o.dims.length!==5)throw Error(`Input "past" must have 5 dimensions`);if(o.dims[0]!==2)throw Error(`Input "past" first dimension must be 2`);if(o.dims[1]!==c)throw Error(`Input "past" second dimension must be batch_size`);if(o.dims[2]!==t.numHeads)throw Error(`Input "past" third dimension must be num_heads`);if(o.dims[4]!==f/t.numHeads)throw Error(`Input "past" fifth dimension must be k_hidden_size / num_heads`);t.pastPresentShareBuffer||(h=o.dims[3])}let g=m+h;if(a)throw Error(`Mask not supported`);if(o)throw Error(`past is not supported`);if(s){if(s.dims.length!==4)throw Error(`Input "attention_bias" must have 4 dimensions`);if(s.dims[0]!==c||s.dims[1]!==t.numHeads||s.dims[2]!==l||s.dims[3]!==g)throw Error(`Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)`)}return{batchSize:c,sequenceLength:l,pastSequenceLength:h,kvSequenceLength:m,totalSequenceLength:g,maxSequenceLength:-1,inputHiddenSize:u,hiddenSize:d,vHiddenSize:p,headSize:Math.floor(d/t.numHeads),vHeadSize:Math.floor(p/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:0,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},Br=(e,t,n)=>t&&e?`163      let total_sequence_length_input = u32(${t.getByOffset(`0`)});164      let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length);165      let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input;166      let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input;167      total_sequence_length = u32(${e?.getByOffset(`batchIdx`)}) + 1;168      var past_sequence_length: u32 = 0;169      if (is_first_prompt == false) {170        past_sequence_length = total_sequence_length - sequence_length;171      }172       `:`173    ${n?`let past_sequence_length = uniforms.past_sequence_length`:``};174    let present_sequence_length = total_sequence_length;175    `,Vr=(e,t,n,r,i,a,o,s)=>{let c=G(o?1:a),l=64,u=a/c;u<l&&(l=32);let d=Math.ceil(a/c/l),f=[{type:12,data:t},{type:12,data:n},{type:12,data:r},{type:12,data:i},{type:12,data:u},{type:12,data:d}],p=U(e.dataType,c),m=Cn(1,c),h=[`type`];return o&&h.push(`type`),s&&h.push(`type`),{name:`AttentionProbsSoftmax`,shaderCache:{hint:`${l};${p};${c}`,inputDependencies:h},getShaderSource:t=>{let n=J(`x`,e.dataType,e.dims,c),r=[n],i=o?q(`seq_lens`,o.dataType,o.dims):void 0;i&&r.push(i);let a=s?q(`total_sequence_length_input`,s.dataType,s.dims):void 0;a&&r.push(a);let u=Cn(e.dataType);return`176  var<workgroup> thread_max: array<f32, ${l}>;177  var<workgroup> thread_sum: array<f32, ${l}>;178  ${t.registerUniforms([{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`}]).declareVariables(...r)}179  ${t.mainStart([l,1,1])}180    let batchIdx = workgroup_id.z / uniforms.num_heads;181    let headIdx = workgroup_id.z % uniforms.num_heads;182    let sequence_length = uniforms.sequence_length;183    var total_sequence_length = uniforms.total_sequence_length;184    ${Br(i,a,!1)}185    let local_offset = local_idx * uniforms.elements_per_thread;186    let offset = (global_idx / ${l}) * uniforms.total_sequence_length + local_offset;187    let seq_causal_length = ${o?`u32(past_sequence_length + workgroup_id.y + 1)`:`total_sequence_length`};188    var thread_max_vector = ${m}(-3.4028234663852886e+38f);189    for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {190      thread_max_vector = max(${m}(x[offset + i]), thread_max_vector);191    }192    thread_max[local_idx] = ${(()=>{switch(c){case 1:return`thread_max_vector`;case 2:return`max(thread_max_vector.x, thread_max_vector.y)`;case 4:return`max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))`;default:throw Error(`Unsupported components: ${c}`)}})()};193    workgroupBarrier();194 195    var max_value =  f32(-3.4028234663852886e+38f);196    for (var i = 0u; i < ${l}; i++) {197      max_value = max(thread_max[i], max_value);198    }199 200    var sum_vector = ${m}(0);201    for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {202      sum_vector += exp(${m}(x[offset + i]) - max_value);203    }204    thread_sum[local_idx] = ${(()=>{switch(c){case 1:return`sum_vector`;case 2:return`sum_vector.x + sum_vector.y`;case 4:return`sum_vector.x + sum_vector.y + sum_vector.z + sum_vector.w`;default:throw Error(`Unsupported components: ${c}`)}})()};205    workgroupBarrier();206 207    var sum: f32 = 0;208    for (var i = 0u; i < ${l}; i++) {209      sum += thread_sum[i];210    }211 212    if (sum == 0) {213      for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {214        x[offset + i] = ${n.type.value}(${u}(1.0) / ${u}(seq_causal_length));215      }216    } else {217      for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {218        var f32input = ${m}(x[offset + i]);219        x[offset + i] = ${n.type.value}(exp(f32input - max_value) / sum);220      }221    }222      ${o?`223        for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) {224          x[offset + total_seq_id] = ${n.type.value}(${u}(0));225        }`:``};226  }`},getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:i,z:t*n},programUniforms:f})}},Hr=(e,t,n,r,i,a,o,s,c)=>{let l=o+a.kvSequenceLength,u=[a.batchSize,a.numHeads,a.sequenceLength,l],d=e>1&&r,f=a.kvNumHeads?a.kvNumHeads:a.numHeads,p=d?[a.batchSize,f,l,a.headSize]:void 0,m=a.nReps?a.nReps:1,h=a.scale===0?1/Math.sqrt(a.headSize):a.scale,g=G(a.headSize),_=a.headSize/g,v={x:Math.ceil(l/12),y:Math.ceil(a.sequenceLength/12),z:a.batchSize*a.numHeads},y=[{type:12,data:a.sequenceLength},{type:12,data:_},{type:12,data:l},{type:12,data:a.numHeads},{type:12,data:a.headSize},{type:1,data:h},{type:12,data:o},{type:12,data:a.kvSequenceLength},{type:12,data:m}],b=d&&r&&z.size(r.dims)>0,x=[`type`,`type`];b&&x.push(`type`),i&&x.push(`type`),s&&x.push(`type`),c&&x.push(`type`);let S=[{dims:u,dataType:t.dataType,gpuDataType:0}];return d&&S.push({dims:p,dataType:t.dataType,gpuDataType:0}),{name:`AttentionProbs`,shaderCache:{hint:`${g};${i!==void 0};${r!==void 0};${e}`,inputDependencies:x},getRunData:()=>({outputs:S,dispatchGroup:v,programUniforms:y}),getShaderSource:e=>{let a=q(`q`,t.dataType,t.dims,g),o=[a,q(`key`,n.dataType,n.dims,g)];if(b){let e=q(`past_key`,r.dataType,r.dims,g);o.push(e)}i&&o.push(q(`attention_bias`,i.dataType,i.dims));let l=s?q(`seq_lens`,s.dataType,s.dims):void 0;l&&o.push(l);let f=c?q(`total_sequence_length_input`,c.dataType,c.dims):void 0;f&&o.push(f);let h=J(`output`,t.dataType,u),_=[h];d&&_.push(J(`present_key`,t.dataType,p,g));let v=Cn(1,g);return`227  const TILE_SIZE = 12u;228 229  var<workgroup> tileQ: array<${a.type.storage}, 144>;230  var<workgroup> tileK: array<${a.type.storage}, 144>;231  ${e.registerUniforms([{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`}]).declareVariables(...o,..._)}232  ${e.mainStart([12,12,1])}233    // x holds the N and y holds the M234    let headIdx = workgroup_id.z % uniforms.num_heads;235    let kvHeadIdx = ${m===1?`headIdx`:`headIdx / uniforms.n_reps`};236    let kv_num_heads = ${m===1?`uniforms.num_heads`:`uniforms.num_heads / uniforms.n_reps`};237    let batchIdx = workgroup_id.z / uniforms.num_heads;238    let m = workgroup_id.y * TILE_SIZE;239    let n = workgroup_id.x * TILE_SIZE;240    let sequence_length = uniforms.M;241    var total_sequence_length = uniforms.N;242    ${Br(l,f,!0)}243    let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx;244    let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;245    ${b&&d?`let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;`:``};246    let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K;247    ${d?`let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;`:``}248    var value = ${v}(0);249    for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {250      if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) {251        tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x];252      }253      if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) {254        var idx = TILE_SIZE * local_id.y + local_id.x;255      ${b&&d?`256              if (n + local_id.y < past_sequence_length) {257                tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x];258              } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {259                tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x];260              }`:`261          if (n + local_id.y < uniforms.kv_sequence_length) {262            tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x];263          }`}264      ${d?`if (n + local_id.y < present_sequence_length) {265        present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx];266      }`:``}267      }268      workgroupBarrier();269 270      for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) {271          value += ${v}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]);272      }273 274      workgroupBarrier();275    }276 277    if (global_id.y < uniforms.M && global_id.x < total_sequence_length) {278      let headOffset = workgroup_id.z * uniforms.M * uniforms.N;279      let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x;280      var sum: f32 = ${(()=>{switch(g){case 1:return`value`;case 2:return`value.x + value.y`;case 4:return`value.x + value.y + value.z + value.w`;default:throw Error(`Unsupported components: ${g}`)}})()};281        output[outputIdx] = ${h.type.value} (sum * uniforms.alpha) + ${i?`attention_bias[outputIdx]`:`0.0`};282    }283  }`}}},Ur=(e,t,n,r,i,a,o=void 0,s=void 0)=>{let c=a+i.kvSequenceLength,l=i.nReps?i.nReps:1,u=i.vHiddenSize*l,d=e>1&&r,f=i.kvNumHeads?i.kvNumHeads:i.numHeads,p=d?[i.batchSize,f,c,i.headSize]:void 0,m=[i.batchSize,i.sequenceLength,u],h={x:Math.ceil(i.vHeadSize/12),y:Math.ceil(i.sequenceLength/12),z:i.batchSize*i.numHeads},g=[{type:12,data:i.sequenceLength},{type:12,data:c},{type:12,data:i.vHeadSize},{type:12,data:i.numHeads},{type:12,data:i.headSize},{type:12,data:u},{type:12,data:a},{type:12,data:i.kvSequenceLength},{type:12,data:l}],_=d&&r&&z.size(r.dims)>0,v=[`type`,`type`];_&&v.push(`type`),o&&v.push(`type`),s&&v.push(`type`);let y=[{dims:m,dataType:t.dataType,gpuDataType:0}];return d&&y.push({dims:p,dataType:t.dataType,gpuDataType:0}),{name:`AttentionScore`,shaderCache:{hint:`${r!==void 0};${e}`,inputDependencies:v},getRunData:()=>({outputs:y,dispatchGroup:h,programUniforms:g}),getShaderSource:e=>{let i=q(`probs`,t.dataType,t.dims),a=[i,q(`v`,n.dataType,n.dims)];_&&a.push(q(`past_value`,r.dataType,r.dims));let c=o?q(`seq_lens`,o.dataType,o.dims):void 0;o&&a.push(c);let u=s?q(`total_sequence_length_input`,s.dataType,s.dims):void 0;s&&a.push(u);let f=[J(`output`,t.dataType,m)];return d&&f.push(J(`present_value`,t.dataType,p)),`284  const TILE_SIZE = 12u;285  var<workgroup> tileQ: array<${i.type.value}, 144>;286  var<workgroup> tileV: array<${i.type.value}, 144>;287  ${e.registerUniforms([{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`}]).declareVariables(...a,...f)}288  ${e.mainStart([12,12,1])}289   let headIdx = workgroup_id.z % uniforms.num_heads;290   let batchIdx = workgroup_id.z / uniforms.num_heads;291   let kvHeadIdx = ${l===1?`headIdx`:`headIdx / uniforms.n_reps`};292   let kv_num_heads = ${l===1?`uniforms.num_heads`:`uniforms.num_heads / uniforms.n_reps`};293   let m = global_id.y;294   let n = global_id.x;295   let sequence_length = uniforms.M;296   var total_sequence_length = uniforms.K;297   ${Br(c,u,!0)}298   let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;299   let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch300   ${_&&d?`let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;`:``};301   let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n;302   ${d?`let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;`:``}303   var value = ${i.type.storage}(0);304   for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {305      if (m < uniforms.M && w + local_id.x < uniforms.K) {306        tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x];307      }308      if (n < uniforms.N && w + local_id.y < uniforms.K) {309        var idx = TILE_SIZE * local_id.y + local_id.x;310        ${_&&d?`311        if (w + local_id.y < past_sequence_length) {312          tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N];313        } else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {314          tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N];315        }316      `:`317            if (w + local_id.y < uniforms.kv_sequence_length) {318              tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N];319            }`}320        ${d?`321            if (w + local_id.y < present_sequence_length) {322          present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx];323        }`:``}324      }325     workgroupBarrier();326     for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) {327       value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x];328     }329     workgroupBarrier();330   }331 332   // we need to transpose output from BNSH_v to BSND_v333   if (m < uniforms.M && n < uniforms.N) {334     let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size335       + headIdx * uniforms.N + n;336     output[outputIdx] = value;337   }338  }`}}},Wr=(e,t,n,r,i,a,o,s,c,l,u=void 0,d=void 0)=>{let f=Math.min(e.outputCount,1+(o?1:0)+(s?1:0)),p=f>1?l.pastSequenceLength:0,m=p+l.kvSequenceLength,h=c&&z.size(c.dims)>0?c:void 0,g=[t,n];f>1&&o&&z.size(o.dims)>0&&g.push(o),h&&g.push(h),u&&g.push(u),d&&g.push(d);let _=e.compute(Hr(f,t,n,o,h,l,p,u,d),{inputs:g,outputs:f>1?[-1,1]:[-1]})[0];e.compute(Vr(_,l.batchSize,l.numHeads,p,l.sequenceLength,m,u,d),{inputs:u&&d?[_,u,d]:[_],outputs:[]});let v=[_,r];f>1&&s&&z.size(s.dims)>0&&v.push(s),u&&v.push(u),d&&v.push(d),e.compute(Ur(f,_,r,s,l,p,u,d),{inputs:v,outputs:f>1?[0,2]:[0]})},Gr=(e,t)=>{let n=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],r=t.sequenceLength,i=t.inputHiddenSize,a=t.headSize,o={x:Math.ceil(t.headSize/12),y:Math.ceil(t.sequenceLength/12),z:t.batchSize*t.numHeads},s=[e.inputs[0],e.inputs[1],e.inputs[2]],c=[{type:12,data:r},{type:12,data:i},{type:12,data:a},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}];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:o,programUniforms:c}),getShaderSource:e=>{let t=J(`output_q`,s[0].dataType,n),r=J(`output_k`,s[0].dataType,n),i=J(`output_v`,s[0].dataType,n),a=q(`input`,s[0].dataType,s[0].dims),o=q(`weight`,s[1].dataType,s[1].dims),c=q(`bias`,s[2].dataType,s[2].dims),l=a.type.storage;return`339  const TILE_SIZE = 12u;340  var<workgroup> tileInput: array<${l}, 144>;341  var<workgroup> tileWeightQ: array<${l}, 144>;342  var<workgroup> tileWeightK: array<${l}, 144>;343  var<workgroup> tileWeightV: array<${l}, 144>;344  ${e.registerUniforms([{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`}]).declareVariables(a,o,c,t,r,i)}345  ${e.mainStart([12,12,1])}346    let batchIndex = workgroup_id.z / uniforms.num_heads;347    let headNumber = workgroup_id.z % uniforms.num_heads;348    let m = global_id.y;349    let n = global_id.x;350 351    let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K;352    let biasOffsetQ = headNumber * uniforms.head_size;353    let biasOffsetK = uniforms.hidden_size + biasOffsetQ;354    let biasOffsetV = uniforms.hidden_size + biasOffsetK;355 356    var valueQ = ${l}(0);357    var valueK = ${l}(0);358    var valueV = ${l}(0);359    for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {360      if (m < uniforms.M && w + local_id.x < uniforms.K) {361        tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x];362      }363      if (n < uniforms.N && w + local_id.y < uniforms.K) {364        let offset = n + (w + local_id.y) * uniforms.ldb;365        tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset];366        tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset];367        tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset];368      }369      workgroupBarrier();370      for (var k: u32 = 0u; k<TILE_SIZE && w+k < uniforms.K; k++) {371        let inputTileOffset = TILE_SIZE * local_id.y + k;372        let weightTileOffset = TILE_SIZE * k + local_id.x;373        valueQ += tileInput[inputTileOffset] * tileWeightQ[weightTileOffset];374        valueK += tileInput[inputTileOffset] * tileWeightK[weightTileOffset];375        valueV += tileInput[inputTileOffset] * tileWeightV[weightTileOffset];376      }377 378      workgroupBarrier();379    }380 381    let headOffset = (m * uniforms.N + n) % uniforms.head_size;382    valueQ += bias[headOffset + biasOffsetQ];383    valueK += bias[headOffset + biasOffsetK];384    valueV += bias[headOffset + biasOffsetV];385 386    let offset = workgroup_id.z * uniforms.M * uniforms.N;387    if (m < uniforms.M && n < uniforms.N) {388      let outputIdx = offset + m * uniforms.N + n;389      output_q[outputIdx] = valueQ;390      output_k[outputIdx] = valueK;391      output_v[outputIdx] = valueV;392    }393  }`}},{inputs:s,outputs:[-1,-1,-1]})},Kr=(e,t)=>{let n=zr(e.inputs,t),[r,i,a]=Gr(e,n);return Wr(e,r,i,a,e.inputs[4],void 0,void 0,void 0,e.inputs[5],n)}}),Jr,Yr,Xr,Zr,Qr=o(()=>{N(),L(),B(),H(),Y(),Jr=(e,t)=>{if(!e||e.length!==5)throw Error(`BatchNormalization requires 5 inputs`);let n=(e,t,n)=>{let r=t.length;if(r!==e.length)throw Error(`${n}: num dimensions != ${r}`);t.forEach((t,r)=>{if(t!==e[r])throw Error(`${n}: dim[${r}] do not match`)})};if(e[0].dims.length>1){let r=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,r,`Invalid input scale`),n(e[2].dims,r,`Invalid input B`),n(e[3].dims,r,`Invalid input mean`),n(e[4].dims,r,`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`)},Yr=(e,t)=>{let{epsilon:n,spatial:r,format:i}=t,a=e[0].dims,o=r?G(a[a.length-1]):1,s=i===`NHWC`&&a.length>1?o:1,c=z.size(a)/o,l=r,u=l?a.length:a,d=q(`x`,e[0].dataType,e[0].dims,o),f=q(`scale`,e[1].dataType,e[1].dims,s),p=q(`bias`,e[2].dataType,e[2].dims,s),m=q(`inputMean`,e[3].dataType,e[3].dims,s),h=q(`inputVar`,e[4].dataType,e[4].dims,s),g=J(`y`,e[0].dataType,u,o),_=()=>{let e=``;if(r)e=`let cOffset = ${a.length===1?`0u`:i===`NHWC`?`outputIndices[${a.length-1}] / ${o}`:`outputIndices[1]`};`;else if(i===`NCHW`)e=`394            ${g.indicesSet(`outputIndices`,`0`,`0`)}395            let cOffset = ${g.indicesToOffset(`outputIndices`)};`;else{e=`var cIndices = ${f.type.indices}(0);396                       cIndices[0] = outputIndices[${a.length-1}];`;for(let t=1;t<f.rank;t++)e+=`cIndices[${t}] = outputIndices[${t}];`;e+=`let cOffset = ${f.indicesToOffset(`cIndices`)};`}return e};return{name:`BatchNormalization`,shaderCache:{hint:`${t.epsilon}_${t.format}_${r}_${o}`,inputDependencies:l?[`rank`,`type`,`type`,`type`,`type`]:void 0},getShaderSource:e=>`397  const epsilon = ${n};398  ${e.registerUniform(`outputSize`,`u32`).declareVariables(d,f,p,m,h,g)}399  ${e.mainStart()}400  ${e.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.outputSize`)}401    var outputIndices = ${g.offsetToIndices(`global_idx * ${o}`)};402    ${_()}403    let scale = ${f.getByOffset(`cOffset`)};404    let bias = ${p.getByOffset(`cOffset`)};405    let inputMean = ${m.getByOffset(`cOffset`)};406    let inputVar = ${h.getByOffset(`cOffset`)};407    let x = ${d.getByOffset(`global_idx`)};408    let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias;409    ${g.setByOffset(`global_idx`,`value`)}410  }`,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:l?[{type:12,data:c},...W(a)]:[{type:12,data:c}]})}},Xr=e=>V(e),Zr=(e,t)=>{let{inputs:n,outputCount:r}=e,i=Xr({...t,outputCount:r});if(S.webgpu.validateInputContent&&Jr(n,i),t.trainingMode)throw Error(`BatchNormalization trainingMode is not supported yet.`);e.compute(Yr(n,i))}}),$r,ei,ti,ni=o(()=>{B(),Y(),$r=e=>{if(e[0].dims.length!==3)throw Error(`input should have 3 dimensions`);if(![320,640,1280].includes(e[0].dims[2]))throw Error(`number of channels should be 320, 640 or 1280`);if(e[1].dims.length!==1)throw Error(`bias is expected to have 1 dimensions`);if(e[0].dims[2]!==e[1].dims[0])throw Error(`last dimension of input and bias are not the same`)},ei=e=>{let t=e[0].dims,n=e[0].dims[2],r=z.size(t)/4,i=e[0].dataType,a=q(`input`,i,t,4),o=q(`bias`,i,[n],4),s=q(`residual`,i,t,4),c=J(`output`,i,t,4);return{name:`BiasAdd`,getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)}}),getShaderSource:e=>`411  const channels = ${n}u / 4;412  ${e.declareVariables(a,o,s,c)}413 414  ${e.mainStart()}415    ${e.guardAgainstOutOfBoundsWorkgroupSizes(r)}416    let value = ${a.getByOffset(`global_idx`)}417      + ${o.getByOffset(`global_idx % channels`)} + ${s.getByOffset(`global_idx`)};418    ${c.setByOffset(`global_idx`,`value`)}419  }`}},ti=e=>{$r(e.inputs),e.compute(ei(e.inputs))}}),ri,$,ii,ai,oi,si,ci,li,ui,di,fi,pi,mi,hi,gi,_i,vi,yi,bi,xi,Si,Ci,wi,Ti,Ei,Di,Oi,ki,Ai,ji,Mi,Ni,Pi,Fi,Ii,Li,Ri,zi,Bi,Vi,Hi,Ui,Wi,Gi,Ki,qi=o(()=>{L(),B(),H(),Y(),ri=(e,t,n,r,i,a,o)=>{let s=Math.ceil(t/4),c=``;c=typeof i==`string`?`${i}(a)`:i(`a`);let l=q(`inputData`,n,[s],4),u=J(`outputData`,r,[s],4),d=[{name:`vec_size`,type:`u32`}];return o&&d.push(...o),`420      ${e.registerUniforms(d).declareVariables(l,u)}421 422  ${a??``}423 424  ${e.mainStart()}425    ${e.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.vec_size`)}426 427    let a = ${l.getByOffset(`global_idx`)};428    ${u.setByOffset(`global_idx`,c)}429  }`},$=(e,t,n,r,i,a=e.dataType,o,s)=>{let c=[{type:12,data:Math.ceil(z.size(e.dims)/4)}];return o&&c.push(...o),{name:t,shaderCache:{hint:i,inputDependencies:[`type`]},getShaderSource:t=>ri(t,z.size(e.dims),e.dataType,a,n,r,s),getRunData:t=>({outputs:[{dims:e.dims,dataType:a}],dispatchGroup:{x:Math.ceil(z.size(t[0].dims)/64/4)},programUniforms:c})}},ii=e=>{e.compute($(e.inputs[0],`Abs`,`abs`))},ai=e=>{e.compute($(e.inputs[0],`Acos`,`acos`))},oi=e=>{e.compute($(e.inputs[0],`Acosh`,`acosh`))},si=e=>{e.compute($(e.inputs[0],`Asin`,`asin`))},ci=e=>{e.compute($(e.inputs[0],`Asinh`,`asinh`))},li=e=>{e.compute($(e.inputs[0],`Atan`,`atan`))},ui=e=>{e.compute($(e.inputs[0],`Atanh`,`atanh`))},di=e=>V(e),fi=(e,t)=>{let n;switch(t.to){case 10:n=`vec4<f16>`;break;case 1:n=`vec4<f32>`;break;case 12:n=`vec4<u32>`;break;case 6:n=`vec4<i32>`;break;case 9:n=`vec4<bool>`;break;default:throw RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute($(e.inputs[0],`Cast`,n,void 0,t.cacheKey,t.to))},pi=e=>{let t,n,r=e.length>=2&&e[1].data!==0,i=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=r?e[1].getFloat32Array()[0]:-34028234663852886e22,n=i?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=r?e[1].getUint16Array()[0]:64511,n=i?e[2].getUint16Array()[0]:31743;break;default:throw Error(`Unsupport data type`)}return V({min:t,max:n})},mi=(e,t)=>{let n=t||pi(e.inputs),r=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`Clip`,e=>`clamp(${e}, vec4<${r}>(uniforms.min), vec4<${r}>(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:r},{name:`max`,type:r}]),{inputs:[0]})},hi=e=>{e.compute($(e.inputs[0],`Ceil`,`ceil`))},gi=e=>{e.compute($(e.inputs[0],`Cos`,`cos`))},_i=e=>{e.compute($(e.inputs[0],`Cosh`,`cosh`))},vi=e=>V(e),yi=(e,t)=>{let n=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`Elu`,e=>`elu_vf32(${e})`,`430  const elu_alpha_ = ${n}(${t.alpha});431 432  fn elu_f32(a: ${n}) -> ${n} {433  return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0);434  }435 436  fn elu_vf32(v: vec4<${n}>) -> vec4<${n}> {437  return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w));438  }`,t.cacheKey))},bi=(e=`f32`)=>`439const r0: ${e} = 0.3275911;440const r1: ${e} = 0.254829592;441const r2: ${e} = -0.284496736;442const r3: ${e} = 1.421413741;443const r4: ${e} = -1.453152027;444const r5: ${e} = 1.061405429;445 446fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> {447  let absv = abs(v);448  let x = 1.0 / (1.0 + r0 * absv);449  return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv));450}`,xi=e=>{let t=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`Erf`,e=>`erf_vf32(${e})`,bi(t)))},Si=e=>{e.compute($(e.inputs[0],`Exp`,`exp`))},Ci=e=>{e.compute($(e.inputs[0],`Floor`,`floor`))},wi=e=>{let t=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`Gelu`,e=>`0.5 * ${e} * (1.0 + erf_vf32(${e} * 0.7071067811865475))`,bi(t)))},Ti=(e,t)=>{let n=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`LeakyRelu`,e=>`select(leaky_relu_alpha_ * ${e}, ${e}, ${e} >= vec4<${n}>(0.0))`,`const leaky_relu_alpha_ = ${n}(${t.alpha});`,t.cacheKey))},Ei=e=>{e.compute($(e.inputs[0],`Not`,e=>`!${e}`))},Di=e=>{e.compute($(e.inputs[0],`Neg`,e=>`-${e}`))},Oi=e=>{e.compute($(e.inputs[0],`Reciprocal`,e=>`1.0/${e}`))},ki=e=>{let t=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`Relu`,e=>`select(vec4<${t}>(0.0), ${e}, ${e} > vec4<${t}>(0.0))`))},Ai=e=>{e.compute($(e.inputs[0],`Sigmoid`,e=>`(1.0 / (1.0 + exp(-${e})))`))},ji=e=>V(e),Mi=(e,t)=>{let n=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`HardSigmoid`,e=>`max(vec4<${n}>(0.0), min(vec4<${n}>(1.0), ${t.alpha} * ${e} + vec4<${n}>(${t.beta})))`,void 0,t.cacheKey))},Ni=e=>{e.compute($(e.inputs[0],`Sin`,`sin`))},Pi=e=>{e.compute($(e.inputs[0],`Sinh`,`sinh`))},Fi=e=>{e.compute($(e.inputs[0],`Sqrt`,`sqrt`))},Ii=e=>{e.compute($(e.inputs[0],`Tan`,`tan`))},Li=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,Ri=e=>{e.compute($(e.inputs[0],`Tanh`,Li))},zi=(e=`f32`)=>`451const fast_gelu_a: ${e} = 0.5;452const fast_gelu_b: ${e} = 0.7978845608028654;453const fast_gelu_c: ${e} = 0.035677408136300125;454 455fn tanh_v(v: vec4<${e}>) -> vec4<${e}> {456  return ${Li(`v`)};457}458`,Bi=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,Vi=e=>{let t=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`FastGelu`,Bi,zi(t),void 0,e.inputs[0].dataType))},Hi=(e,t)=>{let n=Cn(e.inputs[0].dataType);return e.compute($(e.inputs[0],`ThresholdedRelu`,e=>`select(vec4<${n}>(0.0), ${e}, ${e} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${n}>(${t.alpha});`,t.cacheKey)),0},Ui=e=>{e.compute($(e.inputs[0],`Log`,`log`))},Wi=(e,t)=>`459const alpha = vec4<${e}>(${t});460const one = ${e}(1.0);461const zero = ${e}(0.0);462 463fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {464  let v = x *alpha;465  var x1 : vec4<${e}>;466  for (var i = 0; i < 4; i = i + 1) {467    if (v[i] >= zero) {468      x1[i] = one / (one + exp(-v[i]));469    } else {470      x1[i] = one - one / (one + exp(v[i]));471    }472  }473  return x * x1;474}475`,Gi=e=>`quick_gelu_impl(${e})`,Ki=(e,t)=>{let n=Cn(e.inputs[0].dataType);e.compute($(e.inputs[0],`QuickGelu`,Gi,Wi(n,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),Ji,Yi,Xi,Zi=o(()=>{B(),Y(),qi(),Ji=e=>{if(e[0].dims.length!==3)throw Error(`input should have 3 dimensions`);if(![2560,5120,10240].includes(e[0].dims[2]))throw Error(`hidden state should be 2560, 5120 or 10240`);if(e[1].dims.length!==1)throw Error(`bias is expected to have 1 dimensions`);if(e[0].dims[2]!==e[1].dims[0])throw Error(`last dimension of input and bias are not the same`)},Yi=e=>{let t=e[0].dims.slice();t[2]/=2;let n=q(`input`,e[0].dataType,e[0].dims,4),r=q(`bias`,e[0].dataType,[e[0].dims[2]],4),i=J(`output`,e[0].dataType,t,4),a=z.size(t)/4,o=U(e[0].dataType);return{name:`BiasSplitGelu`,getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)}}),getShaderSource:t=>`476  const M_SQRT2 = sqrt(2.0);477  const halfChannels = ${e[0].dims[2]/4/2}u;478 479  ${t.declareVariables(n,r,i)}480 481  ${bi(o)}482 483  ${t.mainStart()}484    ${t.guardAgainstOutOfBoundsWorkgroupSizes(a)}485    let biasIdx = global_idx % halfChannels;486    let batchIndex = global_idx / halfChannels;487    let inputOffset = biasIdx + batchIndex * halfChannels * 2;488    let valueLeft = input[inputOffset] + bias[biasIdx];489    let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels];490    let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1);491 492    ${i.setByOffset(`global_idx`,`valueLeft * geluRight`)}493  }`}},Xi=e=>{Ji(e.inputs),e.compute(Yi(e.inputs))}}),Qi,$i,ea,ta,na,ra,ia,aa,oa,sa,ca,la,ua,da=o(()=>{L(),B(),Y(),Qi=(e,t,n,r,i,a,o,s,c,l,u,d)=>{let f,p;typeof s==`string`?f=p=(e,t)=>`${s}((${e}),(${t}))`:typeof s==`function`?f=p=s:(f=s.scalar,p=s.vector);let m=J(`outputData`,u,r.length,4),h=q(`aData`,c,t.length,4),g=q(`bData`,l,n.length,4),_;if(i)if(a){let e=z.size(t)===1,r=z.size(n)===1,i=t.length>0&&t[t.length-1]%4==0,a=n.length>0&&n[n.length-1]%4==0;_=e||r?m.setByOffset(`global_idx`,p(e?`${h.type.value}(${h.getByOffset(`0`)}.x)`:h.getByOffset(`global_idx`),r?`${g.type.value}(${g.getByOffset(`0`)}.x)`:g.getByOffset(`global_idx`))):`494            let outputIndices = ${m.offsetToIndices(`global_idx * 4u`)};495            let offsetA = ${h.broadcastedIndicesToOffset(`outputIndices`,m)};496            let offsetB = ${g.broadcastedIndicesToOffset(`outputIndices`,m)};497            ${m.setByOffset(`global_idx`,p(o||i?h.getByOffset(`offsetA / 4u`):`${h.type.value}(${h.getByOffset(`offsetA / 4u`)}[offsetA % 4u])`,o||a?g.getByOffset(`offsetB / 4u`):`${g.type.value}(${g.getByOffset(`offsetB / 4u`)}[offsetB % 4u])`))}498          `}else _=m.setByOffset(`global_idx`,p(h.getByOffset(`global_idx`),g.getByOffset(`global_idx`)));else{if(!a)throw Error(`no necessary to use scalar implementation for element-wise binary op implementation.`);let e=(e,t,n=``)=>{let r=`aData[indexA${t}][componentA${t}]`,i=`bData[indexB${t}][componentB${t}]`;return`499            let outputIndices${t} = ${m.offsetToIndices(`global_idx * 4u + ${t}u`)};500            let offsetA${t} = ${h.broadcastedIndicesToOffset(`outputIndices${t}`,m)};501            let offsetB${t} = ${g.broadcastedIndicesToOffset(`outputIndices${t}`,m)};502            let indexA${t} = offsetA${t} / 4u;503            let indexB${t} = offsetB${t} / 4u;504            let componentA${t} = offsetA${t} % 4u;505            let componentB${t} = offsetB${t} % 4u;506            ${e}[${t}] = ${n}(${f(r,i)});507          `};_=u===9?`508            var data = vec4<u32>(0);509            ${e(`data`,0,`u32`)}510            ${e(`data`,1,`u32`)}511            ${e(`data`,2,`u32`)}512            ${e(`data`,3,`u32`)}513            outputData[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:`514            ${e(`outputData[global_idx]`,0)}515            ${e(`outputData[global_idx]`,1)}516            ${e(`outputData[global_idx]`,2)}517            ${e(`outputData[global_idx]`,3)}518          `}return`519        ${e.registerUniform(`vec_size`,`u32`).declareVariables(h,g,m)}520 521        ${d??``}522 523        ${e.mainStart()}524        ${e.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.vec_size`)}525        ${_}526      }`},$i=(e,t,n,r,i,a,o=n.dataType)=>{let s=n.dims.map(Number),c=r.dims.map(Number),l=!z.areEqual(s,c),u=s,d=z.size(s),f=!1,p=!1,m=[l];if(l){let e=zt.calcShape(s,c,!1);if(!e)throw Error(`Can't perform binary op on the given tensors`);u=e.slice(),d=z.size(u);let t=z.size(s)===1,n=z.size(c)===1,r=s.length>0&&s[s.length-1]%4==0,i=c.length>0&&c[c.length-1]%4==0;m.push(t),m.push(n),m.push(r),m.push(i);let a=1;for(let e=1;e<u.length;e++){let t=s[s.length-e];if(t===c[c.length-e])a*=t;else break}a%4==0?(p=!0,f=!0):(t||n||r||i)&&(f=!0)}else f=!0;return m.push(f),{name:e,shaderCache:{hint:t+m.map(e=>e.toString()).join(`_`),inputDependencies:[`rank`,`rank`]},getShaderSource:e=>Qi(e,s,c,u,f,l,p,i,n.dataType,r.dataType,o,a),getRunData:()=>({outputs:[{dims:u,dataType:o}],dispatchGroup:{x:Math.ceil(d/64/4)},programUniforms:[{type:12,data:Math.ceil(z.size(u)/4)},...W(s,c,u)]})}},ea=(e,t,n,r,i,a)=>{e.compute($i(t,i??``,e.inputs[0],e.inputs[1],n,r,a))},ta=e=>{ea(e,`Add`,(e,t)=>`${e}+${t}`)},na=e=>{ea(e,`Div`,(e,t)=>`${e}/${t}`)},ra=e=>{ea(e,`Equal`,{scalar:(e,t)=>`u32(${e}==${t})`,vector:(e,t)=>`vec4<u32>(${e}==${t})`},void 0,void 0,9)},ia=e=>{ea(e,`Mul`,(e,t)=>`${e}*${t}`)},aa=e=>{let t=q(`input`,e.inputs[0].dataType,e.inputs[0].dims).type.value;ea(e,`Pow`,{scalar:(e,t)=>`pow_custom(${e},${t})`,vector:(e,t)=>`pow_vector_custom(${e},${t})`},`527    fn pow_custom(a : ${t}, b : ${t}) -> ${t} {528      if (b == ${t}(0.0)) {529        return ${t}(1.0);530      } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) {531        return ${t}(pow(f32(a), f32(b))); // NaN532      }533      return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t===`i32`?`round`:``}(pow(f32(abs(a)), f32(b))));534    }535    fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> {536      // TODO: implement vectorized pow537      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));538    }539      `)},oa=e=>{ea(e,`Sub`,(e,t)=>`${e}-${t}`)},sa=e=>{ea(e,`Greater`,{scalar:(e,t)=>`u32(${e}>${t})`,vector:(e,t)=>`vec4<u32>(${e}>${t})`},void 0,void 0,9)},ca=e=>{ea(e,`Less`,{scalar:(e,t)=>`u32(${e}<${t})`,vector:(e,t)=>`vec4<u32>(${e}<${t})`},void 0,void 0,9)},la=e=>{ea(e,`GreaterOrEqual`,{scalar:(e,t)=>`u32(${e}>=${t})`,vector:(e,t)=>`vec4<u32>(${e}>=${t})`},void 0,void 0,9)},ua=e=>{ea(e,`LessOrEqual`,{scalar:(e,t)=>`u32(${e}<=${t})`,vector:(e,t)=>`vec4<u32>(${e}<=${t})`},void 0,void 0,9)}}),fa,pa,ma,ha,ga,_a,va=o(()=>{L(),B(),H(),Y(),fa=(e,t)=>{if(!e||e.length<1)throw Error(`too few inputs`);let n=e[0],r=n.dataType,i=n.dims.length;e.forEach((e,a)=>{if(a!==0){if(e.dataType!==r)throw Error(`input tensors should be one type`);if(e.dims.length!==i)throw Error(`input tensors should have the same shape`);e.dims.forEach((e,r)=>{if(r!==t&&e!==n.dims[r])throw Error(`non concat dimensions must match`)})}})},pa=(e,t)=>`540  fn calculateInputIndex(index: u32) -> u32 {541    let sizeInConcatAxis = array<u32, ${e}u>(${t});542    for (var i: u32 = 0u; i < ${e}; i += 1u ) {543      if (index < sizeInConcatAxis[i]) {544        return i;545      }546    }547    return ${e}u;548  }`,ma=(e,t)=>{let n=e.length,r=[];for(let i=0;i<n;++i){let a=t.setByOffset(`global_idx`,e[i].getByIndices(`indices`));n===1?r.push(a):i===0?r.push(`if (inputIndex == ${i}u) { ${a} }`):i===n-1?r.push(`else { ${a} }`):r.push(`else if (inputIndex == ${i}) { ${a} }`)}return r.join(`549`)},ha=(e,t,n,r)=>{let i=z.size(n),a=Array(e.length),o=Array(e.length),s=0,c=[],l=[],u=[{type:12,data:i}];for(let n=0;n<e.length;++n)s+=e[n].dims[t],a[n]=s,l.push(e[n].dims.length),o[n]=q(`input${n}`,r,l[n]),c.push(`rank`),u.push({type:12,data:a[n]});for(let t=0;t<e.length;++t)u.push(...W(e[t].dims));u.push(...W(n));let d=J(`output`,r,n.length),f=d.indicesGet(`indices`,t),p=Array.from(Array(a.length).keys()).map(e=>`uniforms.sizeInConcatAxis${e}`).join(`,`);return{name:`Concat`,shaderCache:{hint:`${t}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:n,dataType:r}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:u}),getShaderSource:t=>`550 551  ${(()=>{t.registerUniform(`outputSize`,`u32`);for(let n=0;n<e.length;n++)t.registerUniform(`sizeInConcatAxis${n}`,`u32`);return t.declareVariables(...o,d)})()}552 553  ${pa(a.length,p)}554 555  ${t.mainStart()}556    ${t.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.outputSize`)}557 558    var indices = ${d.offsetToIndices(`global_idx`)};559 560    let inputIndex = calculateInputIndex(${f});561    if (inputIndex != 0u) {562      let sizeInConcatAxis = array<u32, ${a.length}u>(${p});563      ${f} -= sizeInConcatAxis[inputIndex - 1u];564    }565 566    ${ma(o,d)}567  }`}},ga=(e,t)=>{let n=e.inputs,r=n[0].dims,i=z.normalizeAxis(t.axis,r.length);fa(n,i);let a=r.slice();a[i]=n.reduce((e,t)=>e+(t.dims.length>i?t.dims[i]:0),0);let o=n.filter(e=>z.size(e.dims)>0);e.compute(ha(o,i,a,n[0].dataType),{inputs:o})},_a=e=>V({axis:e.axis})}),ya,ba,xa,Sa,Ca=o(()=>{L(),B(),ya=(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));568              value = sign(value) * (1.0 - e2x) / (1.0 + e2x);569        `;case``:return``;default:throw Error(`Unsupported activation ${e.activation}`)}},ba=(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})},xa=(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`})},Sa=e=>{let t=e?.activation||``;if(t===`HardSigmoid`){let[n,r]=e?.activation_params||[.2,.5];return{activation:t,alpha:n,beta:r}}else if(t===`Clip`){let[n,r]=e?.activation_params||[Ht,Ut];return{activation:t,clipMax:r,clipMin:n}}else if(t===`LeakyRelu`){let[n]=e?.activation_params||[.01];return{activation:t,alpha:n}}return{activation:t}}}),wa,Ta,Ea=o(()=>{wa=(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 Error(`${e}-component is not supported.`)}},Ta=e=>`570      ${e?`value = value + getBiasByOutputCoords(coords);`:``}571      `}),Da,Oa=o(()=>{Da=e=>`572fn getIndexFromCoords4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 {573  return dot(coords, vec4<i32>(574      shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));575}576fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {577  return dot(coords, vec4<i32>(578    i32(${e}.x), i32(${e}.y), i32(${e}.z), 1));579}580`}),ka,Aa,ja=o(()=>{L(),B(),Y(),Ca(),ka=(e,t,n,r,i)=>{let a=r-n;return`581      ${Array.from({length:n}).map((n,o)=>`582      if (${K(t.shape,o,t.rank)} != 1) {583        ${t.indicesSet(e,o,K(i,o+a,r))}584      } else {585        ${t.indicesSet(e,o,0)}586      }`).join(``)}587`},Aa=(e,t,n,r,i=!1,a)=>{let o=e[0].dims,s=e[1].dims,c=o[o.length-2],l=s[s.length-1],u=o[o.length-1],d=G(l),f=G(u),p=G(c),m=z.size(n)/d/p,h=e.length>2,g=r?r.slice(0,-2):n.slice(0,-2),_=[z.size(g),c,l],v=[{type:12,data:m},{type:12,data:c},{type:12,data:l},{type:12,data:u}];return ba(t,v),v.push(...W(g,o,s)),h&&v.push(...W(e[2].dims)),v.push(...W(_)),{name:`MatMulNaive`,shaderCache:{hint:`${t.activation};${d};${f};${p};${i}`,inputDependencies:h?[`rank`,`rank`,`rank`]:[`rank`,`rank`]},getRunData:()=>({outputs:[{dims:a?a(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:v}),getShaderSource:r=>{let a=kn(`batch_dims`,e[0].dataType,g.length),c=q(`a`,e[0].dataType,o.length,f),l=q(`b`,e[1].dataType,s.length,d),u=J(`output`,e[0].dataType,_.length,d),m=U(u.type.tensor),v=ya(t,u.type.value,m),y=[c,l],b=``;if(h){let t=i?d:1;y.push(q(`bias`,e[2].dataType,e[2].dims.length,t)),b=`${i?`value += bias[col / ${t}];`:`value += ${u.type.value}(bias[row + i]);`}`}let x=[{name:`output_size`,type:`u32`},{name:`M`,type:`u32`},{name:`N`,type:`u32`},{name:`K`,type:`u32`}];xa(t,x);let S=()=>{let e=`var a_data: ${c.type.value};`;for(let t=0;t<f;t++)e+=`588              let b_data${t} = b[(b_offset + (k + ${t}) * uniforms.N + col) / ${d}];`;for(let t=0;t<p;t++){e+=`a_data = a[(a_offset + (row + ${t}) * uniforms.K + k) / ${f}];`;for(let n=0;n<f;n++)e+=`589            values[${t}] = fma(${l.type.value}(a_data${f===1?``:`[${n}]`}), b_data${n}, values[${t}]);590`}return e};return`591  ${r.registerUniforms(x).registerInternalVariables(a).declareVariables(...y,u)}592  ${r.mainStart()}593    ${r.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.output_size`)}594    let col = (global_idx % (uniforms.N / ${d})) * ${d};595    var index1 = global_idx / (uniforms.N / ${d});596    let stride1 = uniforms.M / ${p};597    let row = (index1 % stride1) * ${p};598    let batch = index1 / stride1;599 600    ${n.length===2?``:`let batch_indices = ${a.offsetToIndices(`batch`)};`}601 602    var a_indices: ${c.type.indices};603    ${ka(`a_indices`,c,c.rank-2,a.rank,`batch_indices`)}604    ${c.indicesSet(`a_indices`,c.rank-2,0)}605    ${c.indicesSet(`a_indices`,c.rank-1,0)}606    let a_offset = ${c.indicesToOffset(`a_indices`)};607 608    var b_indices: ${l.type.indices};609    ${ka(`b_indices`,l,l.rank-2,a.rank,`batch_indices`)}610    ${l.indicesSet(`b_indices`,l.rank-2,0)}611    ${l.indicesSet(`b_indices`,l.rank-1,0)}612    let b_offset = ${l.indicesToOffset(`b_indices`)};613    var values: array<${u.type.value}, ${p}>;614    for (var k: u32 = 0u; k < uniforms.K; k = k + ${f}) {615      ${S()}616    }617    for (var i = 0u; i < ${p}u; i++) {618      var value = values[i];619      ${b}620      ${v}621      let cur_indices = ${u.type.indices}(batch, row + i, col);622      let offset = ${u.indicesToOffset(`cur_indices`)};623      ${u.setByOffset(`offset / ${d}`,`value`)};624    }625  }626  `}}}}),Ma,Na,Pa,Fa,Ia,La,Ra,za,Ba=o(()=>{L(),B(),Y(),Ca(),ja(),Ea(),Ma=(e,t)=>e?`627        mm_Asub[inputRow][inputCol] = mm_readA(batch,628          kStart + inputRow,629          globalRowStart / innerElementSize + inputCol${t?`, batchIndices`:``});630        `:`631        mm_Asub[inputRow][inputCol] = mm_readA(batch,632          globalRow + innerRow,633          kStart / innerElementSize + inputCol${t?`, batchIndices`:``});634        `,Na=(e,t)=>e?`635        let ACached0 = mm_Asub[k * innerElementSize][localRow];636        let ACached1 = mm_Asub[k * innerElementSize + 1][localRow];637        let ACached2 = mm_Asub[k * innerElementSize + 2][localRow];638        ${t===3?``:`let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];`}639        for (var i = 0; i < rowPerThread; i = i + 1) {640          acc[i] = BCached0 * ACached0[i] + acc[i];641          acc[i] = BCached1 * ACached1[i] + acc[i];642          acc[i] = BCached2 * ACached2[i] + acc[i];643          ${t===3?``:`acc[i] = BCached3 * ACached3[i] + acc[i];`}644        }`:`645        for (var i = 0; i < rowPerThread; i = i + 1) {646          let ACached = mm_Asub[tileRow + i][k];647          acc[i] = BCached0 * ACached.x + acc[i];648          acc[i] = BCached1 * ACached.y + acc[i];649          acc[i] = BCached2 * ACached.z + acc[i];650          ${t===3?``:`acc[i] = BCached3 * ACached.w + acc[i];`}651        }`,Pa=(e,t,n=`f32`,r,i=!1,a=32,o=!1,s=32)=>{let c=t[1]*e[1],l=t[0]*e[0],u=i?c:a,d=i?a:c,f=u/t[0],p=a/t[1];if(!((i&&f===4&&e[1]===4||!i&&(f===3||f===4))&&u%t[0]===0&&a%t[1]===0&&e[0]===4))throw Error(`If transposeA ${i} is true, innerElementSize ${f} and workPerThread[1] ${e[1]} must be 4.652      Otherwise, innerElementSize ${f} must be 3 or 4.653  tileAWidth ${u} must be divisible by workgroupSize[0]${t[0]}. tileInner ${a} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return`654var<workgroup> mm_Asub: array<array<vec${f}<${n}>, ${u/f}>, ${d}>;655var<workgroup> mm_Bsub: array<array<vec4<${n}>, ${l/e[0]}>, ${a}>;656 657const rowPerThread = ${e[1]};658const colPerThread = ${e[0]};659const innerElementSize = ${f};660const tileInner = ${a};661 662@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})663fn main(@builtin(local_invocation_id) localId : vec3<u32>,664        @builtin(global_invocation_id) globalId : vec3<u32>,665        @builtin(workgroup_id) workgroupId : vec3<u32>) {666  let localRow = i32(localId.y);667  let tileRow = localRow * rowPerThread;668  let tileCol = i32(localId.x);669 670  let globalRow =i32(globalId.y) * rowPerThread;671  let globalCol = i32(globalId.x);672  let batch = ${o?`0`:`i32(globalId.z)`};673  ${r?`let batchIndices = ${r.offsetToIndices(`u32(batch)`)};`:``}674  let globalRowStart = i32(workgroupId.y) * ${c};675 676  let num_tiles = ${o?`${Math.ceil(s/a)}`:`(uniforms.dim_inner - 1) / tileInner + 1`};677  var kStart = ${o?`i32(globalId.z) * ${s}`:`0`};678 679  var acc: array<vec4<${n}>, rowPerThread>;680 681  // Loop over shared dimension.682  let tileRowB = localRow * ${p};683  for (var t = 0; t < num_tiles; t = t + 1) {684      // Load one tile of A into local memory.685      for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {686          let inputRow = tileRow + innerRow;687          let inputCol = tileCol;688          ${Ma(i,r)}689      }690 691      // Load one tile of B into local memory.692      for (var innerRow = 0; innerRow < ${p}; innerRow = innerRow + 1) {693          let inputRow = tileRowB + innerRow;694          let inputCol = tileCol;695          mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${r?`, batchIndices`:``});696      }697      kStart = kStart + tileInner;698      workgroupBarrier();699 700      // Compute acc values for a single thread.701      for (var k = 0; k < tileInner / innerElementSize; k = k + 1) {702          let BCached0 = mm_Bsub[k * innerElementSize][tileCol];703          let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol];704          let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol];705          ${f===3?``:`let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];`}706 707          ${Na(i,f)}708      }709 710      workgroupBarrier();711  }712 713  for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {714      mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]);715  }716}`},Fa=(e,t)=>e?`717            mm_Asub[inputRow][inputCol] = mm_readA(batch,718              kStart + inputRow,719              globalRowStart + inputCol${t?`, batchIndices`:``});720            `:`721            mm_Asub[inputRow][inputCol] = mm_readA(batch,722              globalRowStart + inputRow,723              kStart + inputCol${t?`, batchIndices`:``});724            `,Ia=e=>e?`let ACached = mm_Asub[k][tileRow + innerRow];`:`let ACached = mm_Asub[tileRow + innerRow][k];`,La=(e,t,n=`f32`,r,i=!1,a=32,o=!1,s=32,c=!1)=>{let l=e[1]*t[1],u=e[0]*t[0],d=i?l:a,f=i?a:l;if(!(f%t[1]===0&&d%t[0]===0&&a%t[1]===0))throw Error(`tileAHight ${f} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${d} must be divisible by workgroupSize[0]${t[0]}, tileInner ${a} must be divisible by workgroupSize[1]${t[1]}`);let p=f/t[1],m=d/t[0],h=a/t[1],g=c?`725    let localRow = i32(localId.y);726    let localCol = i32(localId.x);727    let globalRowStart = i32(workgroupId.y) * ${l};728    let globalColStart = i32(workgroupId.x) * ${u};729 730    // Loop over shared dimension.731    for (var t = 0; t < num_tiles; t = t + 1) {732      // Load one tile of A into local memory.733      for (var inputRow = localRow; inputRow < ${f}; inputRow = inputRow + ${t[1]}) {734        for (var inputCol = localCol; inputCol < ${d}; inputCol = inputCol + ${t[0]}) {735          ${Fa(i,r)}736        }737      }738      // Load one tile of B into local memory.739      for (var inputRow = localRow; inputRow < ${a}; inputRow = inputRow + ${t[1]}) {740            for (var inputCol = localCol; inputCol < ${u}; inputCol = inputCol + ${t[0]}) {741          mm_Bsub[inputRow][inputCol] = mm_readB(batch,742            kStart + inputRow,743            globalColStart + inputCol${r?`, batchIndices`:``});744        }745      }746      kStart = kStart + tileInner;747      workgroupBarrier();748 749      // Compute acc values for a single thread.750      var BCached : array<${n}, colPerThread>;751      for (var k = 0; k < tileInner; k = k + 1) {752        for (var inner = 0; inner < colPerThread; inner = inner + 1) {753          BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}];754        }755        for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {756          let ACached = ${i?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`}757          for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {758            acc[innerRow][innerCol] = acc[innerRow][innerCol] +759                ACached * BCached[innerCol];760          }761        }762      }763      workgroupBarrier();764    }765    for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {766      let gRow = globalRowStart + localRow + innerRow * ${t[1]};767      for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {768        let gCol = globalColStart + localCol + innerCol * ${t[0]};769        mm_write(batch, gRow, gCol, acc[innerRow][innerCol]);770      }771    }772    `:`773let tileRow = i32(localId.y) * rowPerThread;774let tileCol = i32(localId.x) * colPerThread;775 776let globalRow = i32(globalId.y) * rowPerThread;777let globalCol = i32(globalId.x) * colPerThread;778let globalRowStart = i32(workgroupId.y) * ${l};779 780let tileRowA = i32(localId.y) * ${p};781let tileColA = i32(localId.x) * ${m};782let tileRowB = i32(localId.y) * ${h};783// Loop over shared dimension.784for (var t = 0; t < num_tiles; t = t + 1) {785  // Load one tile of A into local memory.786  for (var innerRow = 0; innerRow < ${p}; innerRow = innerRow + 1) {787    for (var innerCol = 0; innerCol < ${m}; innerCol = innerCol + 1) {788      let inputRow = tileRowA + innerRow;789      let inputCol = tileColA + innerCol;790      ${Fa(i,r)}791    }792  }793 794  // Load one tile of B into local memory.795  for (var innerRow = 0; innerRow < ${h}; innerRow = innerRow + 1) {796    for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {797      let inputRow = tileRowB + innerRow;798      let inputCol = tileCol + innerCol;799      mm_Bsub[inputRow][inputCol] = mm_readB(batch,800        kStart + inputRow,801        globalCol + innerCol${r?`, batchIndices`:``});802    }803  }804  kStart = kStart + tileInner;805  workgroupBarrier();806 807  // Compute acc values for a single thread.808  var BCached : array<${n}, colPerThread>;809  for (var k = 0; k < tileInner; k = k + 1) {810    for (var inner = 0; inner < colPerThread; inner = inner + 1) {811      BCached[inner] = mm_Bsub[k][tileCol + inner];812    }813 814    for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {815      ${Ia(i)}816      for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {817        acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];818      }819    }820  }821 822  workgroupBarrier();823}824 825for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {826  for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {827    mm_write(batch, globalRow + innerRow, globalCol + innerCol,828        acc[innerRow][innerCol]);829  }830}831`;return`832  var<workgroup> mm_Asub : array<array<${n}, ${d}>, ${f}>;833  var<workgroup> mm_Bsub : array<array<${n}, ${u}>, ${a}>;834  const rowPerThread = ${e[1]};835  const colPerThread = ${e[0]};836  const tileInner = ${a};837 838@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})839fn main(@builtin(local_invocation_id) localId : vec3<u32>,840        @builtin(global_invocation_id) globalId : vec3<u32>,841        @builtin(workgroup_id) workgroupId : vec3<u32>) {842    let batch = ${o?`0`:`i32(globalId.z)`};843    ${r?`let batchIndices = ${r.offsetToIndices(`u32(batch)`)};`:``}844    let num_tiles = ${o?`${Math.ceil(s/a)}`:`(uniforms.dim_inner - 1) / tileInner + 1`};845    var kStart = ${o?`i32(globalId.z) * ${s}`:`0`};846 847    var acc : array<array<${n}, colPerThread>, rowPerThread>;848    ${g}849  }850`},Ra=(e,t,n,r,i=!1)=>{let[a,o,s,c]=r,l=U(r[0].type.tensor);return`851    fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${a.type.indices}) -> ${wa(e,l)} {852      var value = ${wa(e,l)}(0.0);853      let col = colIn * ${e};854      if(row < uniforms.dim_a_outer && col < uniforms.dim_inner)855      {856        var aIndices: ${o.type.indices};857        ${ka(`aIndices`,o,o.rank-2,a.rank,`batchIndices`)}858        ${o.indicesSet(`aIndices`,o.rank-2,`u32(row)`)}859        ${o.indicesSet(`aIndices`,o.rank-1,`u32(colIn)`)}860        value = ${o.getByIndices(`aIndices`)};861      }862      return value;863    }864 865    fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${a.type.indices}) -> ${wa(e,l)} {866      var value = ${wa(e,l)}(0.0);867      let col = colIn * ${e};868      if(row < uniforms.dim_inner && col < uniforms.dim_b_outer)869      {870        var bIndices: ${s.type.indices};871        ${ka(`bIndices`,s,s.rank-2,a.rank,`batchIndices`)}872        ${s.indicesSet(`bIndices`,s.rank-2,`u32(row)`)}873        ${s.indicesSet(`bIndices`,s.rank-1,`u32(colIn)`)}874        value = ${s.getByIndices(`bIndices`)};875      }876      return value;877    }878 879    fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${wa(e,l)}) {880      let col = colIn * ${e};881      if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) {882        var value = valueIn;883        let coords = vec3<i32>(batch, row, colIn);884        ${t?`value = value + ${i?`bias[colIn]`:`${wa(e,l)}(bias[row])`};`:``}885        ${n}886        ${c.setByIndices(`vec3<u32>(coords)`,`value`)}887      }888    }889    `},za=(e,t,n,r,i=!1,a)=>{let o=e[0].dims,s=e[1].dims,c=o.slice(0,-2),l=s.slice(0,-2),u=r?r.slice(0,-2):n.slice(0,-2),d=z.size(u),f=o[o.length-2],p=o[o.length-1],m=s[s.length-1],h=p%4==0&&m%4==0,g=f<=8?[4,1,1]:[4,4,1],_=[8,8,1],v=[Math.ceil(m/_[0]/g[0]),Math.ceil(f/_[1]/g[1]),Math.ceil(d/_[2]/g[2])],y=h?4:1,b=[...c,f,p/y],x=b.length,S=[...l,p,m/y],C=S.length,w=[d,f,m/y],ee=[{type:6,data:f},{type:6,data:m},{type:6,data:p}];ba(t,ee),ee.push(...W(u,b,S));let T=[`rank`,`rank`],E=e.length>2;return E&&(ee.push(...W(e[2].dims)),T.push(`rank`)),ee.push(...W(w)),{name:`MatMul`,shaderCache:{hint:`${g};${t.activation};${h};${i}`,inputDependencies:T},getRunData:()=>({outputs:[{dims:a?a(n):n,dataType:e[0].dataType}],dispatchGroup:{x:v[0],y:v[1],z:v[2]},programUniforms:ee}),getShaderSource:n=>{let r=u.length,a=kn(`batchDims`,e[0].dataType,r,1),o=U(e[0].dataType),s=q(`a`,e[0].dataType,x,y),c=q(`b`,e[1].dataType,C,y),l=J(`result`,e[0].dataType,w.length,y),d=[s,c];if(E){let t=i?y:1;d.push(q(`bias`,e[2].dataType,e[2].dims.length,t))}let f=[{name:`dim_a_outer`,type:`i32`},{name:`dim_b_outer`,type:`i32`},{name:`dim_inner`,type:`i32`}];xa(t,f);let p=U(l.type.tensor),m=ya(t,l.type.value,p),v=Ra(y,E,m,[a,s,c,l],i);return`890  ${n.registerUniforms(f).registerInternalVariables(a).declareVariables(...d,l)}891  ${v}892  ${h?Pa(g,_,o,a):La(g,_,o,a)}893                   `}}}}),Va,Ha,Ua=o(()=>{L(),Lt(),Y(),Ca(),Ea(),Oa(),Ba(),Va=(e,t,n,r,i=!1,a,o=4,s=4,c=4,l=`f32`)=>{let u=e=>{switch(e){case 1:return`resData = x[xIndex];`;case 3:return`resData = vec3<${l}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return`resData = x[xIndex / 4];`;default:throw Error(`innerElementSize ${e} is not supported.`)}},d=e=>{switch(e){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 Error(`innerElementSize ${e} is not supported.`)}},f=e?`894    let coord = vec4<i32>(batch, xRow, xCol, xCh);895    `:`896    let coord = vec4<i32>(batch, xCh, xRow, xCol);897    `,p=e?`898    let coords = vec4<i32>(899      batch,900      row / outWidth,901      row % outWidth,902      col);903    `:`904    let coords = vec4<i32>(905      batch,906      row,907      col / outWidth,908      col % outWidth);909    `,m=e?`i32(uniforms.x_shape[1])`:`i32(uniforms.x_shape[2])`,h=e?`i32(uniforms.x_shape[2])`:`i32(uniforms.x_shape[3])`,g=e?`row`:`col`,_=e?`col`:`row`,v=`910    let inChannels = i32(uniforms.w_shape[2]);911    let outWidth = ${e?`i32(uniforms.result_shape[2])`:`i32(uniforms.result_shape[3])`};912    let outRow = ${g} / outWidth;913    let outCol = ${g} % outWidth;914 915    let WRow = ${_} / (i32(uniforms.w_shape[1]) * inChannels);916    let WCol = ${_} / inChannels % i32(uniforms.w_shape[1]);917    let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];918    let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];919    let xCh = ${_} % inChannels;920    var resData = ${wa(o,l)}(0.0);921    // The bounds checking is always needed since we use it to pad zero for922    // the 'same' padding type.923    if (xRow >= 0 && xRow < ${m} && xCol >= 0 && xCol < ${h}) {924      ${f}925      let xIndex = getIndexFromCoords4D(coord, vec4<i32>(uniforms.x_shape));926      ${u(o)}927    }928    return resData;`,y=e?t&&r?`929    let col = colIn * ${o};930    ${v}`:`931    let col = colIn * ${o};932    if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {933      ${v}934    }935    return ${wa(o,l)}(0.0);`:r&&n?`936    let col = colIn * ${o};937    ${v}`:`938    let col = colIn * ${o};939    if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {940      ${v}941    }942    return ${wa(o,l)}(0.0);`,b=e?r&&n?d(s):`943    let col = colIn * ${s};944    if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {945      ${d(s)}946    }947    return ${wa(s,l)}(0.0);`:`948    let col = colIn * ${s};949    if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) {950      ${d(s)}951    }952    return ${wa(s,l)}(0.0);`,x=wa(c,l),S=wa(e?o:s,l),C=wa(e?s:o,l),w=ya(a,x,l);return`953    fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${S} {954      ${e?y:b}955    }956 957    fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${C} {958      ${e?b:y}959    }960 961    fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${x}) {962      let col = colIn * ${c};963      if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer)964      {965      var value = valueIn;966      let outWidth = ${e?`i32(uniforms.result_shape[2])`:`i32(uniforms.result_shape[3])`};967      ${p}968      ${Ta(i)}969      ${w}970      setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);971      }972    }`},Ha=(e,t,n,r,i,a,o,s,c)=>{let l=t.format===`NHWC`,u=l?e[0].dims[3]:e[0].dims[1],d=n[0],f=l?n[2]:n[3],p=l?n[1]:n[2],m=l?n[3]:n[1],h=l&&(u%4==0||u%3==0)&&m%4==0,g=l?m:f*p,_=l?f*p:m,v=[8,8,1],y=r<=8?[4,1,1]:[4,4,1],b=[Math.ceil(g/v[0]/y[0]),Math.ceil(_/v[1]/y[1]),Math.ceil(d/v[2]/y[2])];R(`verbose`,()=>`[conv2d_mm_webgpu] dispatch = ${b}`);let x=h?l&&u%4!=0?3:4:1,S=v[1]*y[1],C=v[0]*y[0],w=Math.max(v[0]*x,v[1]),ee=r%S===0,T=i%C===0,E=a%w===0,D=h?[x,4,4]:[1,1,1],O=[{type:6,data:r},{type:6,data:i},{type:6,data:a},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];ba(t,O),O.push(...W(e[0].dims,e[1].dims));let k=[`rank`,`rank`];return o&&(O.push(...W(e[2].dims)),k.push(`rank`)),O.push(...W(n)),{name:`Conv2DMatMul`,shaderCache:{hint:`${t.cacheKey};${x};${h};${ee};${T};${E};${S};${C};${w}`,inputDependencies:k},getRunData:()=>({outputs:[{dims:c?c(n):n,dataType:e[0].dataType}],dispatchGroup:{x:b[0],y:b[1],z:b[2]},programUniforms:O}),getShaderSource:r=>{let i=[{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}];xa(t,i);let a=h?4:1,c=U(e[0].dataType),u=`973      fn setOutputAtIndex(flatIndex : i32, value : ${h?`vec4<${c}>`:c}) {974        result[flatIndex] = ${h?`vec4<${c}>`:c}(value);975      }976      fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${h?`vec4<${c}>`:c}) {977        let flatIndex = getOutputIndexFromCoords(vec4<i32>(d0, d1, d2, d3));978        setOutputAtIndex(flatIndex ${h?`/ 4`:``}, value);979      }`,d=[q(`x`,e[0].dataType,e[0].dims.length,x===3?1:x),q(`w`,e[1].dataType,e[1].dims.length,a)],f=J(`result`,e[0].dataType,n.length,a);if(o){let t=q(`bias`,e[2].dataType,e[2].dims.length,a);d.push(t),u+=`980        fn getBiasByOutputCoords(coords : vec4<i32>) -> ${h?`vec4<${c}>`:c} {981          return bias[coords.${l?`w`:`y`}${h?`/ 4`:``}];982        }`}return`983        ${Da(`uniforms.result_strides`)}984        //struct Uniforms { xShape : vec4<i32>, wShape : vec4<i32>, outShape : vec4<i32>,985        //  outShapeStrides: vec3<i32>, filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>,986        //  dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32 };987        ${r.registerUniforms(i).declareVariables(...d,f)}988        ${u}989        ${Va(l,ee,T,E,o,t,D[0],D[1],D[2],c)}990        ${h?Pa(y,v,c,void 0,!l,w):La(y,v,c,void 0,!l,w,!1,void 0,s)}`}}}}),Wa,Ga,Ka,qa,Ja,Ya,Xa,Za,Qa=o(()=>{L(),Lt(),B(),Y(),Ca(),Ea(),Wa=e=>{let t=1;for(let n=0;n<e.length;n++)t*=e[n];return t},Ga=e=>typeof e==`number`?[e,e,e]:e,Ka=(e,t)=>t<=1?e:e+(e-1)*(t-1),qa=(e,t,n,r=1)=>{let i=Ka(t,r);return Math.floor((e[0]*(n-1)-n+i)/2)},Ja=(e,t,n,r,i)=>{i??=qa(e,t[0],r[0]);let a=[0,0,0,n];for(let n=0;n<3;n++)e[n]+2*i>=t[n]&&(a[n]=Math.trunc((e[n]-t[n]+2*i)/r[n]+1));return a},Ya=(e,t,n,r,i,a,o,s,c,l)=>{let u,d,f,p;if(e===`VALID`&&(e=0),typeof e==`number`){u={top:e,bottom:e,left:e,right:e,front:e,back:e};let m=Ja([t,n,r,1],[s,c,l],1,[i,a,o],e);d=m[0],f=m[1],p=m[2]}else if(Array.isArray(e)){if(!e.every((e,t,n)=>e===n[0]))throw Error(`Unsupported padding parameter: ${e}`);u={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let m=Ja([t,n,r,1],[s,c,l],1,[i,a,o],e[0]);d=m[0],f=m[1],p=m[2]}else if(e===`SAME_UPPER`){d=Math.ceil(t/i),f=Math.ceil(n/a),p=Math.ceil(r/o);let e=(d-1)*i+s-t,m=(f-1)*a+c-n,h=(p-1)*o+l-r,g=Math.floor(e/2),_=e-g,v=Math.floor(m/2),y=m-v,b=Math.floor(h/2);u={top:v,bottom:y,left:b,right:h-b,front:g,back:_}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:u,outDepth:d,outHeight:f,outWidth:p}},Xa=(e,t,n,r,i,a=!1,o=`channelsLast`)=>{let s,c,l,u,d;if(o===`channelsLast`)[s,c,l,u,d]=e;else if(o===`channelsFirst`)[s,d,c,l,u]=e;else throw Error(`Unknown dataFormat ${o}`);let[f,,p,m,h]=t,[g,_,v]=Ga(n),[y,b,x]=Ga(r),S=Ka(p,y),C=Ka(m,b),w=Ka(h,x),{padInfo:ee,outDepth:T,outHeight:E,outWidth:D}=Ya(i,c,l,u,g,_,v,S,C,w),O=a?f*d:f,k=[0,0,0,0,0];return o===`channelsFirst`?k=[s,O,T,E,D]:o===`channelsLast`&&(k=[s,T,E,D,O]),{batchSize:s,dataFormat:o,inDepth:c,inHeight:l,inWidth:u,inChannels:d,outDepth:T,outHeight:E,outWidth:D,outChannels:O,padInfo:ee,strideDepth:g,strideHeight:_,strideWidth:v,filterDepth:p,filterHeight:m,filterWidth:h,effectiveFilterDepth:S,effectiveFilterHeight:C,effectiveFilterWidth:w,dilationDepth:y,dilationHeight:b,dilationWidth:x,inShape:e,outShape:k,filterShape:t}},Za=(e,t,n,r,i,a)=>{let o=a===`channelsLast`;o?e[0].dims[3]:e[0].dims[1];let s=[64,1,1],c={x:n.map((e,t)=>t)},l=[Math.ceil(Wa(c.x.map(e=>n[e]))/s[0]),1,1];R(`verbose`,()=>`[conv3d_naive_webgpu] dispatch = ${l}`);let u=[{type:12,data:z.size(n)},{type:12,data:r},{type:12,data:i},{type:12,data:t.strides},{type:12,data:t.dilations}];ba(t,u),u.push(...W(e[0].dims,e[1].dims));let d=[`rank`,`rank`],f=e.length===3;return f&&(u.push(...W(e[2].dims)),d.push(`rank`)),u.push(...W(n)),{name:`Conv3DNaive`,shaderCache:{hint:`${t.cacheKey};${o};1;${f}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:l[0],y:l[1],z:l[2]},programUniforms:u}),getShaderSource:a=>{let s=[{name:`output_size`,type:`u32`},{name:`filter_dims`,type:`u32`,length:r.length},{name:`pads`,type:`u32`,length:i.length},{name:`strides`,type:`u32`,length:t.strides.length},{name:`dilations`,type:`u32`,length:t.dilations.length}];xa(t,s);let c=U(e[0].dataType),l=q(`x`,e[0].dataType,e[0].dims.length,1),u=q(`W`,e[1].dataType,e[1].dims.length,1),d=[l,u],p=J(`result`,e[0].dataType,n.length,1),m=``;if(f){let t=q(`bias`,e[2].dataType,e[2].dims.length,1);d.push(t),m+=`991        fn getBiasByOutputCoords(coords : array<u32, 5>) -> ${c} {992          return bias[${o?K(`coords`,4,5):K(`coords`,1,5)}];993        }`}let h=wa(1,c),g=ya(t,h,c);return`994            ${m}995            fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {996              let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);997              return ${l.getByIndices(`aIndices`)};998            }999            fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {1000              let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);1001              return ${u.getByIndices(`aIndices`)};1002            }1003          ${a.registerUniforms(s).declareVariables(...d,p)}1004          ${a.mainStart()}1005          ${a.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.output_size`)}1006              let coords = ${p.offsetToIndices(`global_idx`)};1007              let batch = ${K(`coords`,0,l.rank)};1008              let d2 = ${o?K(`coords`,l.rank-1,l.rank):K(`coords`,1,l.rank)};1009              let xFRCCorner = vec3<u32>(${o?K(`coords`,1,l.rank):K(`coords`,2,l.rank)},1010              ${o?K(`coords`,2,l.rank):K(`coords`,3,l.rank)},1011              ${o?K(`coords`,3,l.rank):K(`coords`,4,l.rank)}) * uniforms.strides - uniforms.pads;1012              let xFCorner = xFRCCorner.x;1013              let xRCorner = xFRCCorner.y;1014              let xCCorner = xFRCCorner.z;1015              let xShapeY = ${o?K(`uniforms.x_shape`,1,l.rank):K(`uniforms.x_shape`,2,l.rank)};1016              let xShapeZ = ${o?K(`uniforms.x_shape`,2,l.rank):K(`uniforms.x_shape`,3,l.rank)};1017              let xShapeW = ${o?K(`uniforms.x_shape`,3,l.rank):K(`uniforms.x_shape`,4,l.rank)};1018              let xShapeU = ${o?K(`uniforms.x_shape`,4,l.rank):K(`uniforms.x_shape`,1,l.rank)};1019              let inputDepthNearestVec4 = (xShapeU / 4) * 4;1020              let inputDepthVec4Remainder = xShapeU % 4;1021 1022              var value = 0.0;1023              for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) {1024                let xF = xFCorner + wF * uniforms.dilations[0];1025                if (xF < 0 || xF >= xShapeY) {1026                  continue;1027                }1028 1029                for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) {1030                  let xR = xRCorner + wR * uniforms.dilations[1];1031                  if (xR < 0 || xR >= xShapeZ) {1032                    continue;1033                  }1034 1035                  for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) {1036                    let xC = xCCorner + wC * uniforms.dilations[2];1037                    if (xC < 0 || xC >= xShapeW) {1038                      continue;1039                    }1040 1041                    for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) {1042                      ${o?`let xValues = vec4<f32>(1043                               getX(batch, xF, xR, xC, d1),1044                               getX(batch, xF, xR, xC, d1 + 1),1045                               getX(batch, xF, xR, xC, d1 + 2),1046                               getX(batch, xF, xR, xC, d1 + 3));1047                            `:`let xValues = vec4<f32>(1048                               getX(batch, d1, xF, xR, xC),1049                               getX(batch, d1 + 1, xF, xR, xC),1050                               getX(batch, d1 + 2, xF, xR, xC),1051                               getX(batch, d1 + 3, xF, xR, xC));1052                            `}1053                            let wValues = vec4<f32>(1054                              getW(d2, d1, wF, wR, wC),1055                              getW(d2, d1 + 1, wF, wR, wC),1056                              getW(d2, d1 + 2, wF, wR, wC),1057                              getW(d2, d1 + 3, wF, wR, wC));1058                      value += dot(xValues, wValues);1059                    }1060                    if (inputDepthVec4Remainder == 1) {1061                        ${o?`value += getX(batch, xF, xR, xC, inputDepthNearestVec4)1062                          * getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC)1063                          * getW(d2, inputDepthNearestVec4, wF, wR, wC);`}1064                    } else if (inputDepthVec4Remainder == 2) {1065                      ${o?`let xValues = vec2<f32>(1066                        getX(batch, xF, xR, xC, inputDepthNearestVec4),1067                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1));1068                      `:`let xValues = vec2<f32>(1069                        getX(batch, inputDepthNearestVec4, xF, xR, xC),1070                        getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC));1071                    `}1072                    let wValues = vec2<f32>(1073                      getW(d2, inputDepthNearestVec4, wF, wR, wC),1074                      getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC));1075                      value += dot(xValues, wValues);1076                    } else if (inputDepthVec4Remainder == 3) {1077                      ${o?`let xValues = vec3<f32>(1078                        getX(batch, xF, xR, xC, inputDepthNearestVec4),1079                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1),1080                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2));1081                      `:`let xValues = vec3<f32>(1082                        getX(batch, inputDepthNearestVec4, xF, xR, xC),1083                        getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC),1084                        getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC));1085                    `}1086                    let wValues = vec3<f32>(1087                      getW(d2, inputDepthNearestVec4, wF, wR, wC),1088                      getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC),1089                      getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC));1090                      value += dot(xValues, wValues);1091                    }1092                  }1093                }1094              }1095              ${f?`value = value + getBiasByOutputCoords(coords)`:``};1096              ${g}1097              result[global_idx] = f32(value);1098          }`}}}}),$a,eo,to=o(()=>{L(),B(),Y(),Ca(),$a=(e,t,n,r)=>{let i=e.length>2,a=i?`value += b[output_channel];`:``,o=e[0].dims,s=e[1].dims,c=t.format===`NHWC`,l=c?n[3]:n[1],u=l/t.group,d=c&&u>=4?G(l):1,f=z.size(n)/d,p=[{type:12,data:f},{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:u}];ba(t,p),p.push(...W(o,[s[0],s[1],s[2],s[3]/d]));let m=i?[`rank`,`rank`,`rank`]:[`rank`,`rank`];return p.push(...W([n[0],n[1],n[2],n[3]/d])),{name:`GroupedConv`,shaderCache:{hint:`${t.cacheKey}_${d}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:p}),getShaderSource:r=>{let l=J(`output`,e[0].dataType,n.length,d),u=U(l.type.tensor),f=ya(t,l.type.value,u),p=q(`x`,e[0].dataType,o.length),m=q(`w`,e[1].dataType,s.length,d),h=[p,m];i&&h.push(q(`b`,e[2].dataType,e[2].dims,d));let g=[{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`}];xa(t,g);let _=c?`1099      for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) {1100        let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];1101 1102        if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) {1103          continue;1104        }1105 1106        for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) {1107          let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];1108          if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) {1109            continue;1110          }1111 1112          for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) {1113            let input_channel = in_channel_offset + wInChannel;1114            let xVal = ${p.get(`batch`,`xHeight`,`xWidth`,`input_channel`)};1115            let wVal = ${m.get(`wHeight`,`wWidth`,`wInChannel`,`output_channel`)};1116            value += xVal * wVal;1117          }1118        }1119      }1120      `:`1121      for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) {1122        let input_channel = in_channel_offset + wInChannel;1123        for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) {1124          let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];1125 1126          if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) {1127            continue;1128          }1129 1130          for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) {1131            let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];1132            if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) {1133              continue;1134            }1135 1136            let xVal = ${p.get(`batch`,`input_channel`,`xHeight`,`xWidth`)};1137            let wVal = ${m.get(`output_channel`,`wInChannel`,`wHeight`,`wWidth`)};1138            value += xVal * wVal;1139          }1140        }1141      }1142      `;return`1143  ${r.registerUniforms(g).declareVariables(...h,l)}1144 1145  ${r.mainStart()}1146    ${r.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.output_size`)}1147 1148    let outputIndices = ${l.offsetToIndices(`global_idx`)};1149    let batch: u32 = outputIndices[0];1150    let output_channel: u32 = outputIndices[${c?3:1}];1151    let xRCCorner: vec2<u32> = vec2<u32>(outputIndices[${c?1:2}], outputIndices[${c?2:3}]) * uniforms.strides - uniforms.pads;1152    let group_id: u32 = output_channel * ${d} / uniforms.output_channels_per_group;1153    var in_channel_offset = group_id * uniforms.w_shape[${c?2:1}];1154 1155    var value: ${l.type.value} = ${l.type.value}(0);1156    ${_}1157    ${a}1158    ${f}1159    ${l.setByOffset(`global_idx`,`value`)}1160  }`}}},eo=(e,t,n,r)=>{let i=e.length>2,a=G(n[3]),o=G(n[2]),s=z.size(n)/a/o,c=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/a],l=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/a],u=[n[0],n[1],n[2],n[3]/a],d=[{type:12,data:s},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];ba(t,d),d.push(...W(c,l,u));let f=(o-1)*t.strides[1]+l[1];return{name:`GroupedConv-Vectorize`,shaderCache:{hint:`${t.cacheKey};${a};${o};${f};${l[0]};${l[1]}`,inputDependencies:i?[`rank`,`rank`,`type`]:[`rank`,`rank`]},getRunData:()=>({outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:d}),getShaderSource:n=>{let r=J(`output`,e[0].dataType,u.length,a),s=U(r.type.tensor),d=ya(t,r.type.value,s),p=q(`x`,e[0].dataType,c.length,a),m=q(`w`,e[1].dataType,l.length,a),h=[p,m];i&&h.push(q(`b`,e[2].dataType,e[2].dims,a));let g=i?`value += b[output_channel];`:``,_=[{name:`output_size`,type:`u32`},{name:`strides`,type:`i32`,length:2},{name:`pads`,type:`i32`,length:2}];return xa(t,_),`1161  ${n.registerUniforms(_).declareVariables(...h,r)}1162  ${n.mainStart()}1163    ${n.guardAgainstOutOfBoundsWorkgroupSizes(`uniforms.output_size`)}1164    let width0 = uniforms.output_shape[3];1165    let output_channel = global_idx % width0;1166    var index1 = global_idx / width0;1167    let width1 = uniforms.output_shape[2] / ${o}u;1168    let col = (index1 % width1) * ${o}u;1169    index1 = index1 / width1;1170    let row = index1 % uniforms.output_shape[1];1171    let batch = index1 / uniforms.output_shape[1];1172 1173    let x_corner = vec2<i32>(i32(row), i32(col)) * uniforms.strides - uniforms.pads;1174 1175    var x_vals: array<${p.type.value}, ${f}>;1176    var values: array<${r.type.value}, ${o}>;1177    let input_channel = output_channel;1178    // Use constant instead of uniform can give better performance for w's height/width.1179    for (var w_height: u32 = 0u; w_height < ${l[0]}; w_height++) {1180      let x_height = x_corner.x + i32(w_height);1181      if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) {1182        for (var i = 0; i < ${f}; i++) {1183          let x_width = x_corner.y + i;1184          if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) {1185            x_vals[i] = ${p.get(`batch`,`u32(x_height)`,`u32(x_width)`,`input_channel`)};1186          } else {1187            x_vals[i] = ${p.type.value}(0);1188          }1189        }1190        for (var w_width: u32 = 0u; w_width < ${l[1]}; w_width++) {1191          let w_val = ${m.get(`w_height`,`w_width`,`0`,`output_channel`)};1192          for (var i = 0u; i < ${o}u; i++) {1193            values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]);1194          }1195        }1196      }1197    }1198 1199    for (var i = 0u; i < ${o}u; i++) {1200      var value = values[i];

Showing the first 1,200 of 2839 lines. Download the file for the rest.