@huggingface/transformers 3.0.0-alpha.7 → 3.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3771,7 +3771,7 @@ __webpack_require__.r(__webpack_exports__);
3771
3771
  // Licensed under the MIT License.
3772
3772
  // This file is generated by /js/scripts/update-version.ts
3773
3773
  // Do not modify file content manually.
3774
- const version = '1.18.0';
3774
+ const version = '1.19.0';
3775
3775
  //# sourceMappingURL=version.js.map
3776
3776
 
3777
3777
  /***/ }),
@@ -3784,34 +3784,33 @@ const version = '1.18.0';
3784
3784
 
3785
3785
  __webpack_require__.r(__webpack_exports__);
3786
3786
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
3787
- /* harmony export */ "InferenceSession": () => (/* binding */ dp),
3788
- /* harmony export */ "TRACE": () => (/* binding */ Sr),
3789
- /* harmony export */ "TRACE_FUNC_BEGIN": () => (/* binding */ We),
3790
- /* harmony export */ "TRACE_FUNC_END": () => (/* binding */ Me),
3791
- /* harmony export */ "Tensor": () => (/* binding */ De),
3792
- /* harmony export */ "TrainingSession": () => (/* binding */ cp),
3793
- /* harmony export */ "default": () => (/* binding */ K_),
3794
- /* harmony export */ "env": () => (/* binding */ be),
3795
- /* harmony export */ "registerBackend": () => (/* binding */ _t)
3787
+ /* harmony export */ "InferenceSession": () => (/* binding */ op),
3788
+ /* harmony export */ "TRACE": () => (/* binding */ $r),
3789
+ /* harmony export */ "TRACE_FUNC_BEGIN": () => (/* binding */ Ve),
3790
+ /* harmony export */ "TRACE_FUNC_END": () => (/* binding */ De),
3791
+ /* harmony export */ "Tensor": () => (/* binding */ Pe),
3792
+ /* harmony export */ "TrainingSession": () => (/* binding */ ap),
3793
+ /* harmony export */ "default": () => (/* binding */ ex),
3794
+ /* harmony export */ "env": () => (/* binding */ fe),
3795
+ /* harmony export */ "registerBackend": () => (/* binding */ wt)
3796
3796
  /* harmony export */ });
3797
3797
  /*!
3798
- * ONNX Runtime Web v1.19.0
3798
+ * ONNX Runtime Web v1.20.0-dev.20240818-417aa00406
3799
3799
  * Copyright (c) Microsoft Corporation. All rights reserved.
3800
3800
  * Licensed under the MIT License.
3801
3801
  */
3802
- var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Object.getOwnPropertyNames;var ap=Object.prototype.hasOwnProperty;var Mn=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var U=(e,t)=>()=>(e&&(t=e(e=0)),t);var Ft=(e,t)=>{for(var r in t)Rn(e,r,{get:t[r],enumerable:!0})},sp=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ip(t))!ap.call(e,o)&&o!==r&&Rn(e,o,{get:()=>t[o],enumerable:!(n=op(t,o))||n.enumerable});return e};var br=e=>sp(Rn({},"__esModule",{value:!0}),e);var wr,$t,_t,up,vr,$r=U(()=>{"use strict";wr=new Map,$t=[],_t=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let n=wr.get(e);if(n===void 0)wr.set(e,{backend:t,priority:r});else{if(n.priority>r)return;if(n.priority===r&&n.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let o=$t.indexOf(e);o!==-1&&$t.splice(o,1);for(let i=0;i<$t.length;i++)if(wr.get($t[i]).priority<=r){$t.splice(i,0,e);return}$t.push(e)}return}throw new TypeError("not a valid backend")},up=async e=>{let t=wr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(n){return r||(t.error=`${n}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},vr=async e=>{let t=e.executionProviders||[],r=t.map(d=>typeof d=="string"?d:d.name),n=r.length===0?$t:r,o,i=[],a=new Set;for(let d of n){let p=await up(d);typeof p=="string"?i.push({name:d,err:p}):(o||(o=p),o===p&&a.add(d))}if(!o)throw new Error(`no available backend found. ERR: ${i.map(d=>`[${d.name}] ${d.err}`).join(", ")}`);for(let{name:d,err:p}of i)r.includes(d)&&console.warn(`removing requested execution provider "${d}" from session options because it is not available: ${p}`);let l=t.filter(d=>a.has(typeof d=="string"?d:d.name));return[o,new Proxy(e,{get:(d,p)=>p==="executionProviders"?l:Reflect.get(d,p)})]}});var Yi=U(()=>{"use strict";$r()});var Xi,Zi=U(()=>{"use strict";Xi="1.19.0"});var Qi,Ne,Un=U(()=>{"use strict";Zi();Qi="warning",Ne={wasm:{},webgl:{},webgpu:{},versions:{common:Xi},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Qi=e}},get logLevel(){return Qi}};Object.defineProperty(Ne,"logLevel",{enumerable:!0})});var be,Ji=U(()=>{"use strict";Un();be=Ne});var ea,ta,ra=U(()=>{"use strict";ea=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let n=r.getContext("2d");if(n!=null){let o,i;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[3]):(o=e.dims[3],i=e.dims[2]);let a=t?.format!==void 0?t.format:"RGB",l=t?.norm,d,p;l===void 0||l.mean===void 0?d=[255,255,255,255]:typeof l.mean=="number"?d=[l.mean,l.mean,l.mean,l.mean]:(d=[l.mean[0],l.mean[1],l.mean[2],0],l.mean[3]!==void 0&&(d[3]=l.mean[3])),l===void 0||l.bias===void 0?p=[0,0,0,0]:typeof l.bias=="number"?p=[l.bias,l.bias,l.bias,l.bias]:(p=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(p[3]=l.bias[3]));let m=i*o,u=0,h=m,w=m*2,g=-1;a==="RGBA"?(u=0,h=m,w=m*2,g=m*3):a==="RGB"?(u=0,h=m,w=m*2):a==="RBG"&&(u=0,w=m,h=m*2);for(let b=0;b<i;b++)for(let x=0;x<o;x++){let _=(e.data[u++]-p[0])*d[0],$=(e.data[h++]-p[1])*d[1],S=(e.data[w++]-p[2])*d[2],I=g===-1?255:(e.data[g++]-p[3])*d[3];n.fillStyle="rgba("+_+","+$+","+S+","+I+")",n.fillRect(x,b,1,1)}if("toDataURL"in r)return r.toDataURL();throw new Error("toDataURL is not supported")}else throw new Error("Can not access image data")},ta=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),n;if(r!=null){let o,i,a;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[1],a=e.dims[3]):(o=e.dims[3],i=e.dims[2],a=e.dims[1]);let l=t!==void 0&&t.format!==void 0?t.format:"RGB",d=t?.norm,p,m;d===void 0||d.mean===void 0?p=[255,255,255,255]:typeof d.mean=="number"?p=[d.mean,d.mean,d.mean,d.mean]:(p=[d.mean[0],d.mean[1],d.mean[2],255],d.mean[3]!==void 0&&(p[3]=d.mean[3])),d===void 0||d.bias===void 0?m=[0,0,0,0]:typeof d.bias=="number"?m=[d.bias,d.bias,d.bias,d.bias]:(m=[d.bias[0],d.bias[1],d.bias[2],0],d.bias[3]!==void 0&&(m[3]=d.bias[3]));let u=i*o;if(t!==void 0&&(t.format!==void 0&&a===4&&t.format!=="RGBA"||a===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let h=4,w=0,g=1,b=2,x=3,_=0,$=u,S=u*2,I=-1;l==="RGBA"?(_=0,$=u,S=u*2,I=u*3):l==="RGB"?(_=0,$=u,S=u*2):l==="RBG"&&(_=0,S=u,$=u*2),n=r.createImageData(o,i);for(let T=0;T<i*o;w+=h,g+=h,b+=h,x+=h,T++)n.data[w]=(e.data[_++]-m[0])*p[0],n.data[g]=(e.data[$++]-m[1])*p[1],n.data[b]=(e.data[S++]-m[2])*p[2],n.data[x]=I===-1?255:(e.data[I++]-m[3])*p[3]}else throw new Error("Can not access image data");return n}});var Vn,na,oa,ia,aa,sa=U(()=>{"use strict";_r();Vn=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:n}=t,o=t.norm??{mean:255,bias:0},i,a;typeof o.mean=="number"?i=[o.mean,o.mean,o.mean,o.mean]:i=[o.mean[0],o.mean[1],o.mean[2],o.mean[3]??255],typeof o.bias=="number"?a=[o.bias,o.bias,o.bias,o.bias]:a=[o.bias[0],o.bias[1],o.bias[2],o.bias[3]??0];let l=t.format!==void 0?t.format:"RGBA",d=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",p=r*n,m=d==="RGBA"?new Float32Array(p*4):new Float32Array(p*3),u=4,h=0,w=1,g=2,b=3,x=0,_=p,$=p*2,S=-1;l==="RGB"&&(u=3,h=0,w=1,g=2,b=-1),d==="RGBA"?S=p*3:d==="RBG"?(x=0,$=p,_=p*2):d==="BGR"&&($=0,_=p,x=p*2);for(let T=0;T<p;T++,h+=u,g+=u,w+=u,b+=u)m[x++]=(e[h]+a[0])/i[0],m[_++]=(e[w]+a[1])/i[1],m[$++]=(e[g]+a[2])/i[2],S!==-1&&b!==-1&&(m[S++]=(e[b]+a[3])/i[3]);return d==="RGBA"?new Be("float32",m,[1,4,r,n]):new Be("float32",m,[1,3,r,n])},na=async(e,t)=>{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,n=typeof ImageData<"u"&&e instanceof ImageData,o=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,i=typeof e=="string",a,l=t??{},d=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},p=m=>m instanceof HTMLCanvasElement||m instanceof OffscreenCanvas?m.getContext("2d"):null;if(r){let m=d();m.width=e.width,m.height=e.height;let u=p(m);if(u!=null){let h=e.height,w=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(h=t.resizedHeight,w=t.resizedWidth),t!==void 0){if(l=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");l.tensorFormat="RGBA",l.height=h,l.width=w}else l.tensorFormat="RGBA",l.height=h,l.width=w;u.drawImage(e,0,0),a=u.getImageData(0,0,w,h).data}else throw new Error("Can not access image data")}else if(n){let m,u;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(m=t.resizedHeight,u=t.resizedWidth):(m=e.height,u=e.width),t!==void 0&&(l=t),l.format="RGBA",l.height=m,l.width=u,t!==void 0){let h=d();h.width=u,h.height=m;let w=p(h);if(w!=null)w.putImageData(e,0,0),a=w.getImageData(0,0,u,m).data;else throw new Error("Can not access image data")}else a=e.data}else if(o){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let m=d();m.width=e.width,m.height=e.height;let u=p(m);if(u!=null){let h=e.height,w=e.width;return u.drawImage(e,0,0,w,h),a=u.getImageData(0,0,w,h).data,l.height=h,l.width=w,Vn(a,l)}else throw new Error("Can not access image data")}else{if(i)return new Promise((m,u)=>{let h=d(),w=p(h);if(!e||!w)return u();let g=new Image;g.crossOrigin="Anonymous",g.src=e,g.onload=()=>{h.width=g.width,h.height=g.height,w.drawImage(g,0,0,h.width,h.height);let b=w.getImageData(0,0,h.width,h.height);l.height=h.height,l.width=h.width,m(Vn(b.data,l))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(a!==void 0)return Vn(a,l);throw new Error("Input data provided is not supported - aborted tensor creation")},oa=(e,t)=>{let{width:r,height:n,download:o,dispose:i}=t,a=[1,n,r,4];return new Be({location:"texture",type:"float32",texture:e,dims:a,download:o,dispose:i})},ia=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new Be({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:n,download:o,dispose:i})},aa=(e,t,r)=>new Be({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})});var xt,qt,ua,da,la=U(()=>{"use strict";xt=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array]]),qt=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),ua=!1,da=()=>{if(!ua){ua=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=typeof Float16Array<"u"&&Float16Array.from;e&&(xt.set("int64",BigInt64Array),qt.set(BigInt64Array,"int64")),t&&(xt.set("uint64",BigUint64Array),qt.set(BigUint64Array,"uint64")),r?(xt.set("float16",Float16Array),qt.set(Float16Array,"float16")):xt.set("float16",Uint16Array)}}});var ca,pa,ma=U(()=>{"use strict";_r();ca=e=>{let t=1;for(let r=0;r<e.length;r++){let n=e[r];if(typeof n!="number"||!Number.isSafeInteger(n))throw new TypeError(`dims[${r}] must be an integer, got: ${n}`);if(n<0)throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${n}`);t*=n}return t},pa=(e,t)=>{switch(e.location){case"cpu":return new Be(e.type,e.data,t);case"cpu-pinned":return new Be({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new Be({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new Be({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var Be,_r=U(()=>{"use strict";ra();sa();la();ma();Be=class{constructor(t,r,n){da();let o,i;if(typeof t=="object"&&"location"in t)switch(this.dataLocation=t.location,o=t.type,i=t.dims,t.location){case"cpu-pinned":{let l=xt.get(o);if(!l)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(t.data instanceof l))throw new TypeError(`buffer should be of type ${l.name}`);this.cpuData=t.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let l,d;if(typeof t=="string")if(o=t,d=n,t==="string"){if(!Array.isArray(r))throw new TypeError("A string tensor's data must be a string array.");l=r}else{let p=xt.get(t);if(p===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if(t==="float16"&&p===Uint16Array)throw new TypeError("Creating a float16 tensor from number array is not supported. Please use Uint16Array as data.");t==="uint64"||t==="int64"?l=p.from(r,BigInt):l=p.from(r)}else if(r instanceof p)l=r;else throw new TypeError(`A ${o} tensor's data must be type of ${p}`)}else if(d=r,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let p=typeof t[0];if(p==="string")o="string",l=t;else if(p==="boolean")o="bool",l=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${p}.`)}else{let p=qt.get(t.constructor);if(p===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);o=p,l=t}if(d===void 0)d=[l.length];else if(!Array.isArray(d))throw new TypeError("A tensor's dims must be a number array");i=d,this.cpuData=l,this.dataLocation="cpu"}let a=ca(i);if(this.cpuData&&a!==this.cpuData.length)throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=i,this.size=a}static async fromImage(t,r){return na(t,r)}static fromTexture(t,r){return oa(t,r)}static fromGpuBuffer(t,r){return ia(t,r)}static fromPinnedBuffer(t,r,n){return aa(t,r,n)}toDataURL(t){return ea(this,t)}toImageData(t){return ta(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return pa(this,t)}}});var De,xr=U(()=>{"use strict";_r();De=Be});var Sr,fa,We,Me,Nn=U(()=>{"use strict";Un();Sr=(e,t)=>{(typeof Ne.trace>"u"?!Ne.wasm.trace:!Ne.trace)||console.timeStamp(`${e}::ORT::${t}`)},fa=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],n=!1;for(let o=0;o<r.length;o++){if(n&&!r[o].includes("TRACE_FUNC")){let i=`FUNC_${e}::${r[o].trim().split(" ")[1]}`;t&&(i+=`::${t}`),Sr("CPU",i);return}r[o].includes("TRACE_FUNC")&&(n=!0)}},We=e=>{(typeof Ne.trace>"u"?!Ne.wasm.trace:!Ne.trace)||fa("BEGIN",e)},Me=e=>{(typeof Ne.trace>"u"?!Ne.wasm.trace:!Ne.trace)||fa("END",e)}});var Ir,ha=U(()=>{"use strict";$r();xr();Nn();Ir=class e{constructor(t){this.handler=t}async run(t,r,n){We();let o={},i={};if(typeof t!="object"||t===null||t instanceof De||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let a=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof De)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");a=!1;for(let p of r){if(typeof p!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(p)===-1)throw new RangeError(`'fetches' contains invalid output name: ${p}.`);o[p]=null}if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,m=Object.getOwnPropertyNames(r);for(let u of this.outputNames)if(m.indexOf(u)!==-1){let h=r[u];(h===null||h instanceof De)&&(p=!0,a=!1,o[u]=h)}if(p){if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else i=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let p of this.inputNames)if(typeof t[p]>"u")throw new Error(`input '${p}' is missing in 'feeds'.`);if(a)for(let p of this.outputNames)o[p]=null;let l=await this.handler.run(t,o,i),d={};for(let p in l)if(Object.hasOwnProperty.call(l,p)){let m=l[p];m instanceof De?d[p]=m:d[p]=new De(m.type,m.data,m.dims)}return Me(),d}async release(){return this.handler.dispose()}static async create(t,r,n,o){We();let i,a={};if(typeof t=="string"){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let m=t,u=0,h=t.byteLength;if(typeof r=="object"&&r!==null)a=r;else if(typeof r=="number"){if(u=r,!Number.isSafeInteger(u))throw new RangeError("'byteOffset' must be an integer.");if(u<0||u>=m.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${m.byteLength}).`);if(h=t.byteLength-u,typeof n=="number"){if(h=n,!Number.isSafeInteger(h))throw new RangeError("'byteLength' must be an integer.");if(h<=0||u+h>m.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${m.byteLength-u}].`);if(typeof o=="object"&&o!==null)a=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(typeof n<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");i=new Uint8Array(m,u,h)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[l,d]=await vr(a),p=await l.createInferenceSessionHandler(i,d);return Me(),new e(p)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var dp,ga=U(()=>{"use strict";ha();dp=Ir});var ya=U(()=>{"use strict"});var ba=U(()=>{"use strict"});var wa=U(()=>{"use strict"});var va=U(()=>{"use strict"});var lp,Cr,$a=U(()=>{"use strict";$r();xr();lp="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",Cr=class e{constructor(t,r,n){this.handler=t,this.hasOptimizerModel=r,this.hasEvalModel=n}get trainingInputNames(){return this.handler.inputNames}get trainingOutputNames(){return this.handler.outputNames}get evalInputNames(){if(this.hasEvalModel)return this.handler.evalInputNames;throw new Error("This training session has no evalModel loaded.")}get evalOutputNames(){if(this.hasEvalModel)return this.handler.evalOutputNames;throw new Error("This training session has no evalModel loaded.")}static async create(t,r){let n=t.evalModel||"",o=t.optimizerModel||"",i=r||{},[a,l]=await vr(i);if(a.createTrainingSessionHandler){let d=await a.createTrainingSessionHandler(t.checkpointState,t.trainModel,n,o,l);return new e(d,!!t.optimizerModel,!!t.evalModel)}else throw new Error(lp)}typeNarrowingForRunStep(t,r,n,o,i){let a={},l={};if(typeof n!="object"||n===null||n instanceof De||Array.isArray(n))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof o=="object"){if(o===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(o instanceof De)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(o)){if(o.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let p of o){if(typeof p!="string")throw new TypeError("'fetches' must be a string array or an object.");if(r.indexOf(p)===-1)throw new RangeError(`'fetches' contains invalid output name: ${p}.`);a[p]=null}if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,m=Object.getOwnPropertyNames(o);for(let u of r)if(m.indexOf(u)!==-1){let h=o[u];(h===null||h instanceof De)&&(p=!0,d=!1,a[u]=h)}if(p){if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else l=o}}else if(typeof o<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let p of t)if(typeof n[p]>"u")throw new Error(`input '${p}' is missing in 'feeds'.`);if(d)for(let p of r)a[p]=null;return[a,l]}convertHandlerReturnTypeToMapOfTensors(t){let r={};for(let n in t)if(Object.hasOwnProperty.call(t,n)){let o=t[n];o instanceof De?r[n]=o:r[n]=new De(o.type,o.data,o.dims)}return r}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(t,r,n){let[o,i]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,t,r,n),a=await this.handler.runTrainStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}async runOptimizerStep(t){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(t||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(t,r,n){if(this.hasEvalModel){let[o,i]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,t,r,n),a=await this.handler.runEvalStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(t=!0){return this.handler.getParametersSize(t)}async loadParametersBuffer(t,r=!0){let n=await this.getParametersSize(r);if(t.length!==4*n)throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check.");return this.handler.loadParametersBuffer(t,r)}async getContiguousParameters(t=!0){return this.handler.getContiguousParameters(t)}async release(){return this.handler.dispose()}}});var cp,_a=U(()=>{"use strict";$a();cp=Cr});var Wn={};Ft(Wn,{InferenceSession:()=>dp,TRACE:()=>Sr,TRACE_FUNC_BEGIN:()=>We,TRACE_FUNC_END:()=>Me,Tensor:()=>De,TrainingSession:()=>cp,env:()=>be,registerBackend:()=>_t});var Ke=U(()=>{"use strict";Yi();Ji();ga();xr();ya();ba();Nn();wa();va();_a()});var Tr=U(()=>{"use strict"});var Ca={};Ft(Ca,{default:()=>pp});var Sa,Ia,pp,Ta=U(()=>{"use strict";Hn();St();jt();Sa="ort-wasm-proxy-worker",Ia=globalThis.self?.name===Sa;Ia&&(self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":Ar(r.wasm).then(()=>{Er(r).then(()=>{postMessage({type:t})},n=>{postMessage({type:t,err:n})})},n=>{postMessage({type:t,err:n})});break;case"init-ep":{let{epName:n,env:o}=r;kr(o,n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})});break}case"copy-from":{let{buffer:n}=r,o=Kt(n);postMessage({type:t,out:o});break}case"create":{let{model:n,options:o}=r;Pr(n,o).then(i=>{postMessage({type:t,out:i})},i=>{postMessage({type:t,err:i})});break}case"release":Or(r),postMessage({type:t});break;case"run":{let{sessionId:n,inputIndices:o,inputs:i,outputIndices:a,options:l}=r;Dr(n,o,i,a,new Array(a.length).fill(null),l).then(d=>{d.some(p=>p[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:d},Br([...i,...d]))},d=>{postMessage({type:t,err:d})});break}case"end-profiling":zr(r),postMessage({type:t});break;default:}}catch(n){postMessage({type:t,err:n})}});pp=Ia?null:e=>new Worker(e??Ut,{type:"module",name:Sa})});var Ea={};Ft(Ea,{default:()=>mp});var Gn,Aa,mp,ka=U(()=>{"use strict";Aa=(Gn=import.meta.url,async function(e={}){function t(){return le.buffer!=re.buffer&&we(),re}function r(){return le.buffer!=re.buffer&&we(),se}function n(){return le.buffer!=re.buffer&&we(),Z}function o(){return le.buffer!=re.buffer&&we(),ne}function i(){return le.buffer!=re.buffer&&we(),J}function a(){return le.buffer!=re.buffer&&we(),ke}function l(){return le.buffer!=re.buffer&&we(),R}function d(){return le.buffer!=re.buffer&&we(),Re}var p,m,u=Object.assign({},e),h=new Promise((s,c)=>{p=s,m=c}),w=typeof window=="object",g=typeof importScripts=="function",b=g&&self.name=="em-pthread";u.mountExternalData=(s,c)=>{(u.Fb||(u.Fb=new Map)).set(s,c)},u.unmountExternalData=()=>{delete u.Fb};var x=globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let _=()=>{let s=(f,y,v)=>(...C)=>{let O=Je,B=y?.();C=f(...C);let N=y?.();return B!==N&&(f=N,v(B),y=v=null),Je!=O?new Promise((L,j)=>{An={resolve:L,reject:j}}):C},c=f=>async(...y)=>{try{if(u.Eb)throw Error("Session already started");let v=u.Eb={bc:y[0],errors:[]},C=await f(...y);if(u.Eb!==v)throw Error("Session mismatch");u.Mb?.flush();let O=v.errors;if(0<O.length){let B=await Promise.all(O);if(B=B.filter(N=>N),0<B.length)throw Error(B.join(`
3803
- `))}return C}finally{u.Eb=null}};u._OrtCreateSession=s(u._OrtCreateSession,()=>u._OrtCreateSession,f=>u._OrtCreateSession=f),u._OrtRun=c(s(u._OrtRun,()=>u._OrtRun,f=>u._OrtRun=f)),u._OrtRunWithBinding=c(s(u._OrtRunWithBinding,()=>u._OrtRunWithBinding,f=>u._OrtRunWithBinding=f)),u._OrtBindInput=s(u._OrtBindInput,()=>u._OrtBindInput,f=>u._OrtBindInput=f),_=void 0};u.jsepInit=(s,c)=>{if(_?.(),s==="webgpu"){[u.Mb,u.Tb,u.Xb,u.Nb,u.Wb,u.jb,u.Yb,u.$b,u.Ub,u.Vb,u.Zb]=c;let f=u.Mb;u.jsepRegisterBuffer=(y,v,C,O)=>f.registerBuffer(y,v,C,O),u.jsepGetBuffer=y=>f.getBuffer(y),u.jsepCreateDownloader=(y,v,C)=>f.createDownloader(y,v,C),u.jsepOnReleaseSession=y=>{f.onReleaseSession(y)},u.jsepOnRunStart=y=>f.onRunStart(y)}};var $,S,I=Object.assign({},u),T="./this.program",A=(s,c)=>{throw c},D="";(w||g)&&(g?D=self.location.href:typeof document<"u"&&document.currentScript&&(D=document.currentScript.src),Gn&&(D=Gn),D=D.startsWith("blob:")?"":D.substr(0,D.replace(/[?#].*/,"").lastIndexOf("/")+1),g&&(S=s=>{var c=new XMLHttpRequest;return c.open("GET",s,!1),c.responseType="arraybuffer",c.send(null),new Uint8Array(c.response)}),$=(s,c,f)=>{var y=new XMLHttpRequest;y.open("GET",s,!0),y.responseType="arraybuffer",y.onload=()=>{y.status==200||y.status==0&&y.response?c(y.response):f()},y.onerror=f,y.send(null)});var z,H=console.log.bind(console),W=console.error.bind(console),X=H,Y=W;if(Object.assign(u,I),I=null,b){let s=function(c){try{var f=c.data,y=f.cmd;if(y==="load"){let v=[];self.onmessage=C=>v.push(C),self.startWorker=()=>{postMessage({cmd:"loaded"});for(let C of v)s(C);self.onmessage=s};for(let C of f.handlers)u[C]&&!u[C].proxy||(u[C]=(...O)=>{postMessage({Lb:"callHandler",kc:C,args:O})},C=="print"&&(X=u[C]),C=="printErr"&&(Y=u[C]));le=f.wasmMemory,we(),pe(f.wasmModule)}else if(y==="run"){On(f.pthread_ptr,0,0,1,0,0),In(f.pthread_ptr),Hl(),No(),ue||(Mi(),ue=!0);try{Gl(f.start_routine,f.arg)}catch(v){if(v!="unwind")throw v}}else y==="cancel"?Mt()&&gr(-1):f.target!=="setimmediate"&&(y==="checkMailbox"?ue&&sr():y&&(Y(`worker: received unknown command ${y}`),Y(f)))}catch(v){throw Ui(),v}};var Ih=s,pe,ue=!1;Y=function(...c){c=c.join(" "),console.error(c)},self.alert=function(...c){postMessage({Lb:"alert",text:c.join(" "),mc:Mt()})},u.instantiateWasm=(c,f)=>new Promise(y=>{pe=v=>{v=new WebAssembly.Instance(v,Bo()),f(v),y()}}),self.onunhandledrejection=c=>{throw c.reason||c},self.onmessage=s}u.wasmBinary&&(z=u.wasmBinary);var le,K,de,re,se,Z,ne,J,ke,R,F,ce,Re,Se=!1;function we(){var s=le.buffer;u.HEAP8=re=new Int8Array(s),u.HEAP16=Z=new Int16Array(s),u.HEAPU8=se=new Uint8Array(s),u.HEAPU16=ne=new Uint16Array(s),u.HEAP32=J=new Int32Array(s),u.HEAPU32=ke=new Uint32Array(s),u.HEAPF32=R=new Float32Array(s),u.HEAPF64=Re=new Float64Array(s),u.HEAP64=F=new BigInt64Array(s),u.HEAPU64=ce=new BigUint64Array(s)}if(!b){if(!((le=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0})).buffer instanceof x))throw Y("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),Error("bad memory");we()}var Lt=[],Pe=[],ze=[],Fe=0,Ot=null,yt=null;function ko(){if(--Fe==0&&(Ot!==null&&(clearInterval(Ot),Ot=null),yt)){var s=yt;yt=null,s()}}function Dt(s){throw Y(s="Aborted("+s+")"),Se=!0,de=1,s=new WebAssembly.RuntimeError(s+". Build with -sASSERTIONS for more info."),m(s),s}var mn,Po=s=>s.startsWith("data:application/octet-stream;base64,"),Oo=s=>s.startsWith("file://");function Do(s){if(s==mn&&z)return new Uint8Array(z);if(S)return S(s);throw"both async and sync fetching of the wasm failed"}function zo(s,c,f){return function(y){if(!z&&(w||g)){if(typeof fetch=="function"&&!Oo(y))return fetch(y,{credentials:"same-origin"}).then(v=>{if(!v.ok)throw`failed to load wasm binary file at '${y}'`;return v.arrayBuffer()}).catch(()=>Do(y));if($)return new Promise((v,C)=>{$(y,O=>v(new Uint8Array(O)),C)})}return Promise.resolve().then(()=>Do(y))}(s).then(y=>WebAssembly.instantiate(y,c)).then(f,y=>{Y(`failed to asynchronously prepare wasm: ${y}`),Dt(y)})}function Bo(){return{a:{M:Wl,za:Nl,b:Fl,$:Lo,z:jo,pa:Ko,X:Xo,Z:Zo,qa:Qo,na:Jo,ga:ei,ma:ti,J:ri,Y:ni,V:oi,oa:ii,W:ai,va:ql,D:Kl,P:Yl,O:Zl,C:Jl,s:ec,p:tc,E:rc,y:dc,Q:lc,ta:cc,ja:pc,T:mc,aa:fc,F:hc,ia:In,sa:gc,u:yc,B:vc,o:$c,m:xc,c:xn,n:Sc,k:Tc,Aa:Ac,r:Ec,f:kc,v:Pc,l:Oc,g:Dc,i:zc,j:Bc,h:Rc,e:Mc,da:Uc,ea:Vc,fa:Nc,ba:vi,ca:$i,S:Wc,d:Hc,N:Gc,G:Lc,K:Fc,w:qc,ra:jc,U:Kc,t:xi,x:Yc,L:Xc,R:Zc,ya:Qc,xa:Jc,ka:Ci,la:Ti,_:bn,A:Ai,I:Ei,ha:ki,H:Pi,a:le,wa:yn,ua:zi,q:rp}}}var fn={849460:(s,c,f,y)=>{if(u===void 0||!u.Fb)return 1;if((s=Ae(s>>>0)).startsWith("./")&&(s=s.substring(2)),!(s=u.Fb.get(s)))return 2;if(y>>>=0,(c>>>=0)+(f>>>=0)>s.byteLength)return 3;try{return r().set(s.subarray(c,c+f),y>>>0),0}catch{return 4}},849961:()=>{u.Ub()},849992:()=>{u.Vb()},850021:()=>{u.Zb()},850046:s=>u.Tb(s),850079:s=>u.Xb(s),850111:(s,c,f)=>{u.Nb(s,c,f,!0)},850150:(s,c,f)=>{u.Nb(s,c,f)},850183:()=>typeof wasmOffsetConverter<"u",850240:s=>{u.jb("Abs",s,void 0)},850291:s=>{u.jb("Neg",s,void 0)},850342:s=>{u.jb("Floor",s,void 0)},850395:s=>{u.jb("Ceil",s,void 0)},850447:s=>{u.jb("Reciprocal",s,void 0)},850505:s=>{u.jb("Sqrt",s,void 0)},850557:s=>{u.jb("Exp",s,void 0)},850608:s=>{u.jb("Erf",s,void 0)},850659:s=>{u.jb("Sigmoid",s,void 0)},850714:(s,c,f)=>{u.jb("HardSigmoid",s,{alpha:c,beta:f})},850793:s=>{u.jb("Log",s,void 0)},850844:s=>{u.jb("Sin",s,void 0)},850895:s=>{u.jb("Cos",s,void 0)},850946:s=>{u.jb("Tan",s,void 0)},850997:s=>{u.jb("Asin",s,void 0)},851049:s=>{u.jb("Acos",s,void 0)},851101:s=>{u.jb("Atan",s,void 0)},851153:s=>{u.jb("Sinh",s,void 0)},851205:s=>{u.jb("Cosh",s,void 0)},851257:s=>{u.jb("Asinh",s,void 0)},851310:s=>{u.jb("Acosh",s,void 0)},851363:s=>{u.jb("Atanh",s,void 0)},851416:s=>{u.jb("Tanh",s,void 0)},851468:s=>{u.jb("Not",s,void 0)},851519:(s,c,f)=>{u.jb("Clip",s,{min:c,max:f})},851588:s=>{u.jb("Clip",s,void 0)},851640:(s,c)=>{u.jb("Elu",s,{alpha:c})},851698:s=>{u.jb("Relu",s,void 0)},851750:(s,c)=>{u.jb("LeakyRelu",s,{alpha:c})},851814:(s,c)=>{u.jb("ThresholdedRelu",s,{alpha:c})},851884:(s,c)=>{u.jb("Cast",s,{to:c})},851942:s=>{u.jb("Add",s,void 0)},851993:s=>{u.jb("Sub",s,void 0)},852044:s=>{u.jb("Mul",s,void 0)},852095:s=>{u.jb("Div",s,void 0)},852146:s=>{u.jb("Pow",s,void 0)},852197:s=>{u.jb("Equal",s,void 0)},852250:s=>{u.jb("Greater",s,void 0)},852305:s=>{u.jb("GreaterOrEqual",s,void 0)},852367:s=>{u.jb("Less",s,void 0)},852419:s=>{u.jb("LessOrEqual",s,void 0)},852478:(s,c,f,y,v)=>{u.jb("ReduceMean",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},852637:(s,c,f,y,v)=>{u.jb("ReduceMax",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},852795:(s,c,f,y,v)=>{u.jb("ReduceMin",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},852953:(s,c,f,y,v)=>{u.jb("ReduceProd",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},853112:(s,c,f,y,v)=>{u.jb("ReduceSum",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},853270:(s,c,f,y,v)=>{u.jb("ReduceL1",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},853427:(s,c,f,y,v)=>{u.jb("ReduceL2",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},853584:(s,c,f,y,v)=>{u.jb("ReduceLogSum",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},853745:(s,c,f,y,v)=>{u.jb("ReduceSumSquare",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},853909:(s,c,f,y,v)=>{u.jb("ReduceLogSumExp",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},854073:s=>{u.jb("Where",s,void 0)},854126:(s,c,f)=>{u.jb("Transpose",s,{perm:c?Array.from(i().subarray(c>>>0,f>>>0)):[]})},854234:(s,c,f,y)=>{u.jb("DepthToSpace",s,{blocksize:c,mode:Ae(f),format:y?"NHWC":"NCHW"})},854367:(s,c,f,y)=>{u.jb("DepthToSpace",s,{blocksize:c,mode:Ae(f),format:y?"NHWC":"NCHW"})},854500:(s,c,f,y,v,C,O,B,N,L,j,me,he,P,ee)=>{u.jb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:c,dilations:[f],group:y,kernelShape:[v],pads:[C,O],strides:[B],wIsConst:()=>!!t()[L>>>0],outputPadding:j?Array.from(i().subarray(j>>>0,me>>>0)):[],outputShape:he?Array.from(i().subarray(he>>>0,P>>>0)):[],activation:Ae(ee)})},854901:(s,c,f,y,v,C,O,B,N,L,j,me,he,P)=>{u.jb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:c,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:y,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(O>>>0,2+(O>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:L?Array.from(i().subarray(L>>>0,j>>>0)):[],outputShape:me?Array.from(i().subarray(me>>>0,he>>>0)):[],activation:Ae(P)})},855466:(s,c,f,y,v,C,O,B,N,L,j,me,he,P,ee)=>{u.jb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:c,dilations:[f],group:y,kernelShape:[v],pads:[C,O],strides:[B],wIsConst:()=>!!t()[L>>>0],outputPadding:j?Array.from(i().subarray(j>>>0,me>>>0)):[],outputShape:he?Array.from(i().subarray(he>>>0,P>>>0)):[],activation:Ae(ee)})},855867:(s,c,f,y,v,C,O,B,N,L,j,me,he,P)=>{u.jb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:c,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:y,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(O>>>0,2+(O>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:L?Array.from(i().subarray(L>>>0,j>>>0)):[],outputShape:me?Array.from(i().subarray(me>>>0,he>>>0)):[],activation:Ae(P)})},856432:(s,c)=>{u.jb("GlobalAveragePool",s,{format:c?"NHWC":"NCHW"})},856523:(s,c,f,y,v,C,O,B,N,L,j,me,he,P,ee,ye)=>{u.jb("AveragePool",s,{format:ye?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:y,storage_order:v,dilations:[C,O],kernel_shape:[B,N],pads:[L,j,me,he],strides:[P,ee]})},856807:(s,c)=>{u.jb("GlobalAveragePool",s,{format:c?"NHWC":"NCHW"})},856898:(s,c,f,y,v,C,O,B,N,L,j,me,he,P,ee,ye)=>{u.jb("AveragePool",s,{format:ye?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:y,storage_order:v,dilations:[C,O],kernel_shape:[B,N],pads:[L,j,me,he],strides:[P,ee]})},857182:(s,c)=>{u.jb("GlobalMaxPool",s,{format:c?"NHWC":"NCHW"})},857269:(s,c,f,y,v,C,O,B,N,L,j,me,he,P,ee,ye)=>{u.jb("MaxPool",s,{format:ye?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:y,storage_order:v,dilations:[C,O],kernel_shape:[B,N],pads:[L,j,me,he],strides:[P,ee]})},857549:(s,c)=>{u.jb("GlobalMaxPool",s,{format:c?"NHWC":"NCHW"})},857636:(s,c,f,y,v,C,O,B,N,L,j,me,he,P,ee,ye)=>{u.jb("MaxPool",s,{format:ye?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:y,storage_order:v,dilations:[C,O],kernel_shape:[B,N],pads:[L,j,me,he],strides:[P,ee]})},857916:(s,c,f,y,v)=>{u.jb("Gemm",s,{alpha:c,beta:f,transA:y,transB:v})},858020:s=>{u.jb("MatMul",s,void 0)},858074:(s,c,f,y)=>{u.jb("ArgMax",s,{keepDims:!!c,selectLastIndex:!!f,axis:y})},858182:(s,c,f,y)=>{u.jb("ArgMin",s,{keepDims:!!c,selectLastIndex:!!f,axis:y})},858290:(s,c)=>{u.jb("Softmax",s,{axis:c})},858353:(s,c)=>{u.jb("Concat",s,{axis:c})},858413:(s,c,f,y,v)=>{u.jb("Split",s,{axis:c,numOutputs:f,splitSizes:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},858553:s=>{u.jb("Expand",s,void 0)},858607:(s,c)=>{u.jb("Gather",s,{axis:Number(c)})},858678:(s,c)=>{u.jb("GatherElements",s,{axis:Number(c)})},858757:(s,c,f,y,v,C,O,B,N,L,j)=>{u.jb("Resize",s,{antialias:c,axes:f?Array.from(i().subarray(f>>>0,y>>>0)):[],coordinateTransformMode:Ae(v),cubicCoeffA:C,excludeOutside:O,extrapolationValue:B,keepAspectRatioPolicy:Ae(N),mode:Ae(L),nearestMode:Ae(j)})},859103:(s,c,f,y,v,C,O)=>{u.jb("Slice",s,{starts:c?Array.from(i().subarray(c>>>0,f>>>0)):[],ends:y?Array.from(i().subarray(y>>>0,v>>>0)):[],axes:C?Array.from(i().subarray(C>>>0,O>>>0)):[]})},859319:s=>{u.jb("Tile",s,void 0)},859371:(s,c,f)=>{u.jb("InstanceNormalization",s,{epsilon:c,format:f?"NHWC":"NCHW"})},859485:(s,c,f)=>{u.jb("InstanceNormalization",s,{epsilon:c,format:f?"NHWC":"NCHW"})},859599:s=>{u.jb("Range",s,void 0)},859652:(s,c)=>{u.jb("Einsum",s,{equation:Ae(c)})},859733:(s,c,f,y,v)=>{u.jb("Pad",s,{mode:c,value:f,pads:y?Array.from(i().subarray(y>>>0,v>>>0)):[]})},859860:(s,c,f,y,v,C)=>{u.jb("BatchNormalization",s,{epsilon:c,momentum:f,spatial:!!v,trainingMode:!!y,format:C?"NHWC":"NCHW"})},860029:(s,c,f,y,v,C)=>{u.jb("BatchNormalization",s,{epsilon:c,momentum:f,spatial:!!v,trainingMode:!!y,format:C?"NHWC":"NCHW"})},860198:(s,c,f)=>{u.jb("CumSum",s,{exclusive:Number(c),reverse:Number(f)})},860295:(s,c,f,y,v,C,O,B,N)=>{u.jb("Attention",s,{numHeads:c,isUnidirectional:f,maskFilterValue:y,scale:v,doRotary:C,qkvHiddenSizes:O?Array.from(i().subarray(Number(B)>>>0,Number(B)+O>>>0)):[],pastPresentShareBuffer:!!N})},860567:s=>{u.jb("BiasAdd",s,void 0)},860622:s=>{u.jb("BiasSplitGelu",s,void 0)},860683:s=>{u.jb("FastGelu",s,void 0)},860739:(s,c,f,y,v,C,O,B,N,L,j,me,he,P,ee,ye)=>{u.jb("Conv",s,{format:me?"NHWC":"NCHW",auto_pad:c,dilations:f?Array.from(i().subarray(f>>>0,y>>>0)):[],group:v,kernel_shape:C?Array.from(i().subarray(C>>>0,O>>>0)):[],pads:B?Array.from(i().subarray(B>>>0,N>>>0)):[],strides:L?Array.from(i().subarray(L>>>0,j>>>0)):[],w_is_const:()=>!!t()[he>>>0],activation:Ae(P),activation_params:ee?Array.from(l().subarray(ee>>>0,ye>>>0)):[]})},861235:s=>{u.jb("Gelu",s,void 0)},861287:(s,c,f,y)=>{u.jb("GroupQueryAttention",s,{numHeads:c,kvNumHeads:f,scale:y})},861400:(s,c,f,y)=>{u.jb("LayerNormalization",s,{axis:c,epsilon:f,simplified:!!y})},861511:(s,c,f,y)=>{u.jb("LayerNormalization",s,{axis:c,epsilon:f,simplified:!!y})},861622:(s,c,f,y,v,C)=>{u.jb("MatMulNBits",s,{k:c,n:f,accuracyLevel:y,bits:v,blockSize:C})},861749:(s,c,f,y,v,C)=>{u.jb("MultiHeadAttention",s,{numHeads:c,isUnidirectional:f,maskFilterValue:y,scale:v,doRotary:C})},861908:(s,c)=>{u.jb("QuickGelu",s,{alpha:c})},861972:(s,c,f,y,v)=>{u.jb("RotaryEmbedding",s,{interleaved:!!c,numHeads:f,rotaryEmbeddingDim:y,scale:v})},862111:(s,c,f)=>{u.jb("SkipLayerNormalization",s,{epsilon:c,simplified:!!f})},862213:s=>{u.Yb(s)},862247:(s,c)=>u.$b(s,c,u.Eb.bc,u.Eb.errors),862359:(s,c,f)=>{u.jb("SkipLayerNormalization",s,{epsilon:c,simplified:!!f})}};function Nl(s,c,f){return hi(async()=>{await u.Wb(s,c,f)})}function Wl(){return typeof wasmOffsetConverter<"u"}function hn(s){this.name="ExitStatus",this.message=`Program terminated with exit(${s})`,this.status=s}var gn=s=>{s.terminate(),s.onmessage=()=>{}},Ro=s=>{ct.length==0&&(Ho(),Wo(ct[0]));var c=ct.pop();if(!c)return 6;wt.push(c),Ze[s.Ab]=c,c.Ab=s.Ab;var f={cmd:"run",start_routine:s.cc,arg:s.Pb,pthread_ptr:s.Ab};return c.postMessage(f,s.ic),0},bt=0,xe=(s,c,...f)=>{for(var y=2*f.length,v=Bn(),C=zn(8*y),O=C>>>3,B=0;B<f.length;B++){var N=f[B];typeof N=="bigint"?(F[O+2*B]=1n,F[O+2*B+1]=N):(F[O+2*B]=0n,d()[O+2*B+1>>>0]=N)}return s=Vi(s,0,y,C,c),yr(v),s};function yn(s){if(b)return xe(0,1,s);if(de=s,!(0<bt)){for(var c of wt)gn(c);for(c of ct)gn(c);ct=[],wt=[],Ze=[],Se=!0}A(s,new hn(s))}function Mo(s){if(b)return xe(1,0,s);bn(s)}var bn=s=>{if(de=s,b)throw Mo(s),"unwind";yn(s)},ct=[],wt=[],Uo=[],Ze={},Vo=s=>{var c=s.Ab;delete Ze[c],ct.push(s),wt.splice(wt.indexOf(s),1),s.Ab=0,Dn(c)};function No(){Uo.forEach(s=>s())}var Wo=s=>new Promise(c=>{s.onmessage=v=>{var C=(v=v.data).cmd;if(v.targetThread&&v.targetThread!=Mt()){var O=Ze[v.targetThread];O?O.postMessage(v,v.transferList):Y(`Internal error! Worker sent a message "${C}" to target pthread ${v.targetThread}, but that thread no longer exists!`)}else C==="checkMailbox"?sr():C==="spawnThread"?Ro(v):C==="cleanupThread"?Vo(Ze[v.thread]):C==="killThread"?(v=v.thread,C=Ze[v],delete Ze[v],gn(C),Dn(v),wt.splice(wt.indexOf(C),1),C.Ab=0):C==="cancelThread"?Ze[v.thread].postMessage({cmd:"cancel"}):C==="loaded"?(s.loaded=!0,c(s)):C==="alert"?alert(`Thread ${v.threadId}: ${v.text}`):v.target==="setimmediate"?s.postMessage(v):C==="callHandler"?u[v.handler](...v.args):C&&Y(`worker sent an unknown command ${C}`)},s.onerror=v=>{throw Y(`worker sent an error! ${v.filename}:${v.lineno}: ${v.message}`),v};var f,y=[];for(f of[])u.hasOwnProperty(f)&&y.push(f);s.postMessage({cmd:"load",handlers:y,wasmMemory:le,wasmModule:K})});function Ho(){var s=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});ct.push(s)}var ar=s=>{for(;0<s.length;)s.shift()(u)},Hl=()=>{var s=Mt(),c=a()[s+52>>>2>>>0];s=a()[s+56>>>2>>>0],Wi(c,c-s),yr(c)},Gl=(s,c)=>{bt=0,s=Hi(s,c),0<bt?de=s:gr(s)};class Ll{constructor(c){this.Ib=c-24}}function Fl(s,c,f){var y=new Ll(s>>>=0);throw c>>>=0,f>>>=0,a()[y.Ib+16>>>2>>>0]=0,a()[y.Ib+4>>>2>>>0]=c,a()[y.Ib+8>>>2>>>0]=f,s}function Go(s,c,f,y){return b?xe(2,1,s,c,f,y):Lo(s,c,f,y)}function Lo(s,c,f,y){if(s>>>=0,c>>>=0,f>>>=0,y>>>=0,x===void 0)return Y("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var v=[];return b&&v.length===0?Go(s,c,f,y):(s={cc:f,Ab:s,Pb:y,ic:v},b?(s.Lb="spawnThread",postMessage(s,v),0):Ro(s))}var Fo=typeof TextDecoder<"u"?new TextDecoder("utf8"):void 0,qo=(s,c,f)=>{var y=(c>>>=0)+f;for(f=c;s[f]&&!(f>=y);)++f;if(16<f-c&&s.buffer&&Fo)return Fo.decode(s.buffer instanceof x?s.slice(c,f):s.subarray(c,f));for(y="";c<f;){var v=s[c++];if(128&v){var C=63&s[c++];if((224&v)==192)y+=String.fromCharCode((31&v)<<6|C);else{var O=63&s[c++];65536>(v=(240&v)==224?(15&v)<<12|C<<6|O:(7&v)<<18|C<<12|O<<6|63&s[c++])?y+=String.fromCharCode(v):(v-=65536,y+=String.fromCharCode(55296|v>>10,56320|1023&v))}}else y+=String.fromCharCode(v)}return y},Ae=(s,c)=>(s>>>=0)?qo(r(),s,c):"";function jo(s,c,f){return b?xe(3,1,s,c,f):0}function Ko(s,c){if(b)return xe(4,1,s,c)}var wn=s=>{for(var c=0,f=0;f<s.length;++f){var y=s.charCodeAt(f);127>=y?c++:2047>=y?c+=2:55296<=y&&57343>=y?(c+=4,++f):c+=3}return c},Yo=(s,c,f,y)=>{if(!(0<y))return 0;var v=f>>>=0;y=f+y-1;for(var C=0;C<s.length;++C){var O=s.charCodeAt(C);if(55296<=O&&57343>=O&&(O=65536+((1023&O)<<10)|1023&s.charCodeAt(++C)),127>=O){if(f>=y)break;c[f++>>>0]=O}else{if(2047>=O){if(f+1>=y)break;c[f++>>>0]=192|O>>6}else{if(65535>=O){if(f+2>=y)break;c[f++>>>0]=224|O>>12}else{if(f+3>=y)break;c[f++>>>0]=240|O>>18,c[f++>>>0]=128|O>>12&63}c[f++>>>0]=128|O>>6&63}c[f++>>>0]=128|63&O}}return c[f>>>0]=0,f-v},zt=(s,c,f)=>Yo(s,r(),c,f);function Xo(s,c){if(b)return xe(5,1,s,c)}function Zo(s,c,f){if(b)return xe(6,1,s,c,f)}function Qo(s,c,f){return b?xe(7,1,s,c,f):0}function Jo(s,c){if(b)return xe(8,1,s,c)}function ei(s,c,f){if(b)return xe(9,1,s,c,f)}function ti(s,c,f,y){if(b)return xe(10,1,s,c,f,y)}function ri(s,c,f,y){if(b)return xe(11,1,s,c,f,y)}function ni(s,c,f,y){if(b)return xe(12,1,s,c,f,y)}function oi(s){if(b)return xe(13,1,s)}function ii(s,c){if(b)return xe(14,1,s,c)}function ai(s,c,f){if(b)return xe(15,1,s,c,f)}var si,pt,ql=()=>{Dt("")},Qe=s=>{for(var c="";r()[s>>>0];)c+=si[r()[s++>>>0]];return c},vn={},$n={},jl={};function st(s,c,f={}){if(!("argPackAdvance"in c))throw new TypeError("registerType registeredInstance requires argPackAdvance");return function(y,v,C={}){var O=v.name;if(!y)throw new pt(`type "${O}" must have a positive integer typeid pointer`);if($n.hasOwnProperty(y)){if(C.Rb)return;throw new pt(`Cannot register type '${O}' twice`)}$n[y]=v,delete jl[y],vn.hasOwnProperty(y)&&(v=vn[y],delete vn[y],v.forEach(B=>B()))}(s,c,f)}var ui=(s,c,f)=>{switch(c){case 1:return f?y=>t()[y>>>0]:y=>r()[y>>>0];case 2:return f?y=>n()[y>>>1>>>0]:y=>o()[y>>>1>>>0];case 4:return f?y=>i()[y>>>2>>>0]:y=>a()[y>>>2>>>0];case 8:return f?y=>F[y>>>3]:y=>ce[y>>>3];default:throw new TypeError(`invalid integer width (${c}): ${s}`)}};function Kl(s,c,f){f>>>=0,st(s>>>=0,{name:c=Qe(c>>>0),fromWireType:y=>y,toWireType:function(y,v){if(typeof v!="bigint"&&typeof v!="number")throw v=v===null?"null":(y=typeof v)=="object"||y==="array"||y==="function"?v.toString():""+v,new TypeError(`Cannot convert "${v}" to ${this.name}`);return typeof v=="number"&&(v=BigInt(v)),v},argPackAdvance:mt,readValueFromPointer:ui(c,f,c.indexOf("u")==-1),Db:null})}var mt=8;function Yl(s,c,f,y){st(s>>>=0,{name:c=Qe(c>>>0),fromWireType:function(v){return!!v},toWireType:function(v,C){return C?f:y},argPackAdvance:mt,readValueFromPointer:function(v){return this.fromWireType(r()[v>>>0])},Db:null})}var _n=[],ut=[];function xn(s){9<(s>>>=0)&&--ut[s+1]==0&&(ut[s]=void 0,_n.push(s))}var qe=s=>{if(!s)throw new pt("Cannot use deleted val. handle = "+s);return ut[s]},je=s=>{switch(s){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let c=_n.pop()||ut.length;return ut[c]=s,ut[c+1]=1,c}};function Sn(s){return this.fromWireType(a()[s>>>2>>>0])}var Xl={name:"emscripten::val",fromWireType:s=>{var c=qe(s);return xn(s),c},toWireType:(s,c)=>je(c),argPackAdvance:mt,readValueFromPointer:Sn,Db:null};function Zl(s){return st(s>>>0,Xl)}var Ql=(s,c)=>{switch(c){case 4:return function(f){return this.fromWireType(l()[f>>>2>>>0])};case 8:return function(f){return this.fromWireType(d()[f>>>3>>>0])};default:throw new TypeError(`invalid float width (${c}): ${s}`)}};function Jl(s,c,f){f>>>=0,st(s>>>=0,{name:c=Qe(c>>>0),fromWireType:y=>y,toWireType:(y,v)=>v,argPackAdvance:mt,readValueFromPointer:Ql(c,f),Db:null})}function ec(s,c,f,y,v){if(s>>>=0,f>>>=0,c=Qe(c>>>0),v===-1&&(v=4294967295),v=B=>B,y===0){var C=32-8*f;v=B=>B<<C>>>C}var O=c.includes("unsigned")?function(B,N){return N>>>0}:function(B,N){return N};st(s,{name:c,fromWireType:v,toWireType:O,argPackAdvance:mt,readValueFromPointer:ui(c,f,y!==0),Db:null})}function tc(s,c,f){function y(C){var O=a()[C>>>2>>>0];return C=a()[C+4>>>2>>>0],new v(t().buffer,C,O)}var v=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][c];st(s>>>=0,{name:f=Qe(f>>>0),fromWireType:y,argPackAdvance:mt,readValueFromPointer:y},{Rb:!0})}function rc(s,c){s>>>=0;var f=(c=Qe(c>>>0))==="std::string";st(s,{name:c,fromWireType:function(y){var v=a()[y>>>2>>>0],C=y+4;if(f)for(var O=C,B=0;B<=v;++B){var N=C+B;if(B==v||r()[N>>>0]==0){if(O=Ae(O,N-O),L===void 0)var L=O;else L+=String.fromCharCode(0),L+=O;O=N+1}}else{for(L=Array(v),B=0;B<v;++B)L[B]=String.fromCharCode(r()[C+B>>>0]);L=L.join("")}return et(y),L},toWireType:function(y,v){v instanceof ArrayBuffer&&(v=new Uint8Array(v));var C=typeof v=="string";if(!(C||v instanceof Uint8Array||v instanceof Uint8ClampedArray||v instanceof Int8Array))throw new pt("Cannot pass non-string to std::string");var O=f&&C?wn(v):v.length,B=hr(4+O+1),N=B+4;if(a()[B>>>2>>>0]=O,f&&C)zt(v,N,O+1);else if(C)for(C=0;C<O;++C){var L=v.charCodeAt(C);if(255<L)throw et(N),new pt("String has UTF-16 code units that do not fit in 8 bits");r()[N+C>>>0]=L}else for(C=0;C<O;++C)r()[N+C>>>0]=v[C];return y!==null&&y.push(et,B),B},argPackAdvance:mt,readValueFromPointer:Sn,Db(y){et(y)}})}var di=typeof TextDecoder<"u"?new TextDecoder("utf-16le"):void 0,nc=(s,c)=>{for(var f=s>>1,y=f+c/2;!(f>=y)&&o()[f>>>0];)++f;if(32<(f<<=1)-s&&di)return di.decode(r().slice(s,f));for(f="",y=0;!(y>=c/2);++y){var v=n()[s+2*y>>>1>>>0];if(v==0)break;f+=String.fromCharCode(v)}return f},oc=(s,c,f)=>{if(f??=2147483647,2>f)return 0;var y=c;f=(f-=2)<2*s.length?f/2:s.length;for(var v=0;v<f;++v){var C=s.charCodeAt(v);n()[c>>>1>>>0]=C,c+=2}return n()[c>>>1>>>0]=0,c-y},ic=s=>2*s.length,ac=(s,c)=>{for(var f=0,y="";!(f>=c/4);){var v=i()[s+4*f>>>2>>>0];if(v==0)break;++f,65536<=v?(v-=65536,y+=String.fromCharCode(55296|v>>10,56320|1023&v)):y+=String.fromCharCode(v)}return y},sc=(s,c,f)=>{if(c>>>=0,f??=2147483647,4>f)return 0;var y=c;f=y+f-4;for(var v=0;v<s.length;++v){var C=s.charCodeAt(v);if(55296<=C&&57343>=C&&(C=65536+((1023&C)<<10)|1023&s.charCodeAt(++v)),i()[c>>>2>>>0]=C,(c+=4)+4>f)break}return i()[c>>>2>>>0]=0,c-y},uc=s=>{for(var c=0,f=0;f<s.length;++f){var y=s.charCodeAt(f);55296<=y&&57343>=y&&++f,c+=4}return c};function dc(s,c,f){if(s>>>=0,c>>>=0,f=Qe(f>>>=0),c===2)var y=nc,v=oc,C=ic,O=B=>o()[B>>>1>>>0];else c===4&&(y=ac,v=sc,C=uc,O=B=>a()[B>>>2>>>0]);st(s,{name:f,fromWireType:B=>{for(var N,L=a()[B>>>2>>>0],j=B+4,me=0;me<=L;++me){var he=B+4+me*c;me!=L&&O(he)!=0||(j=y(j,he-j),N===void 0?N=j:(N+=String.fromCharCode(0),N+=j),j=he+c)}return et(B),N},toWireType:(B,N)=>{if(typeof N!="string")throw new pt(`Cannot pass non-string to C++ string type ${f}`);var L=C(N),j=hr(4+L+c);return a()[j>>>2>>>0]=L/c,v(N,j+4,L+c),B!==null&&B.push(et,j),j},argPackAdvance:mt,readValueFromPointer:Sn,Db(B){et(B)}})}function lc(s,c){st(s>>>=0,{Sb:!0,name:c=Qe(c>>>0),argPackAdvance:0,fromWireType:()=>{},toWireType:()=>{}})}var cc=()=>1;function pc(s){On(s>>>0,!g,1,!w,131072,!1),No()}var li=s=>{if(!Se)try{if(s(),!(0<bt))try{b?gr(de):bn(de)}catch(c){c instanceof hn||c=="unwind"||A(1,c)}}catch(c){c instanceof hn||c=="unwind"||A(1,c)}};function In(s){s>>>=0,typeof Atomics.jc=="function"&&(Atomics.jc(i(),s>>>2,s).value.then(sr),s+=128,Atomics.store(i(),s>>>2,1))}var sr=()=>{var s=Mt();s&&(In(s),li(Ni))};function mc(s,c){(s>>>=0)==c>>>0?setTimeout(sr):b?postMessage({targetThread:s,cmd:"checkMailbox"}):(s=Ze[s])&&s.postMessage({cmd:"checkMailbox"})}var Cn=[];function fc(s,c,f,y,v){for(c>>>=0,y/=2,Cn.length=y,f=v>>>0>>>3,v=0;v<y;v++)Cn[v]=F[f+2*v]?F[f+2*v+1]:d()[f+2*v+1>>>0];return(c?fn[c]:np[s])(...Cn)}function hc(s){s>>>=0,b?postMessage({cmd:"cleanupThread",thread:s}):Vo(Ze[s])}function gc(s){}var Tn=(s,c)=>{var f=$n[s];if(f===void 0)throw s=Ri(s),f=Qe(s),et(s),new pt(`${c} has unknown type ${f}`);return f},ci=(s,c,f)=>{var y=[];return s=s.toWireType(y,f),y.length&&(a()[c>>>2>>>0]=je(y)),s};function yc(s,c,f){return c>>>=0,f>>>=0,s=qe(s>>>0),c=Tn(c,"emval::as"),ci(c,f,s)}var ur=s=>{try{s()}catch(c){Dt(c)}},ft=0,Je=null,pi=0,dr=[],mi={},fi={},bc=0,An=null,wc=[];function hi(s){return function(c){if(!Se){if(ft===0){var f=!1,y=!1;c((v=0)=>{if(!Se&&(pi=v,f=!0,y)){ft=2,ur(()=>Fi(Je)),typeof Browser<"u"&&Browser.Jb.Qb&&Browser.Jb.resume(),v=!1;try{var C=function(){var N=i()[Je+8>>>2>>>0];return N=q[fi[N]],--bt,N()}()}catch(N){C=N,v=!0}var O=!1;if(!Je){var B=An;B&&(An=null,(v?B.reject:B.resolve)(C),O=!0)}if(v&&!O)throw C}}),y=!0,f||(ft=1,Je=function(){var v=hr(65548),C=v+12;a()[v>>>2>>>0]=C,a()[v+4>>>2>>>0]=C+65536,C=dr[0];var O=mi[C];return O===void 0&&(O=bc++,mi[C]=O,fi[O]=C),C=O,i()[v+8>>>2>>>0]=C,v}(),typeof Browser<"u"&&Browser.Jb.Qb&&Browser.Jb.pause(),ur(()=>Gi(Je)))}else ft===2?(ft=0,ur(qi),et(Je),Je=null,wc.forEach(li)):Dt(`invalid state: ${ft}`);return pi}}(c=>{s().then(c)})}function vc(s){return s>>>=0,hi(()=>(s=qe(s)).then(je))}var lr=[];function $c(s,c,f,y){return f>>>=0,y>>>=0,(s=lr[s>>>0])(null,c=qe(c>>>0),f,y)}var _c={},cr=s=>{var c=_c[s];return c===void 0?Qe(s):c};function xc(s,c,f,y,v){return f>>>=0,y>>>=0,v>>>=0,(s=lr[s>>>0])(c=qe(c>>>0),c[f=cr(f)],y,v)}var gi=()=>typeof globalThis=="object"?globalThis:Function("return this")();function Sc(s){return(s>>>=0)==0?je(gi()):(s=cr(s),je(gi()[s]))}var Ic=s=>{var c=lr.length;return lr.push(s),c},Cc=(s,c)=>{for(var f=Array(s),y=0;y<s;++y)f[y]=Tn(a()[c+4*y>>>2>>>0],"parameter "+y);return f},yi=(s,c)=>Object.defineProperty(c,"name",{value:s});function Tc(s,c,f){var y=(c=Cc(s,c>>>0)).shift();s--;var v=`return function (obj, func, destructorsRef, args) {
3804
- `,C=0,O=[];f===0&&O.push("obj");for(var B=["retType"],N=[y],L=0;L<s;++L)O.push("arg"+L),B.push("argType"+L),N.push(c[L]),v+=` var arg${L} = argType${L}.readValueFromPointer(args${C?"+"+C:""});
3805
- `,C+=c[L].argPackAdvance;return v+=` var rv = ${f===1?"new func":"func.call"}(${O.join(", ")});
3806
- `,y.Sb||(B.push("emval_returnValue"),N.push(ci),v+=` return emval_returnValue(retType, destructorsRef, rv);
3802
+ var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Object.getOwnPropertyNames;var tp=Object.prototype.hasOwnProperty;var zn=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var M=(e,t)=>()=>(e&&(t=e(e=0)),t);var Lt=(e,t)=>{for(var r in t)On(e,r,{get:t[r],enumerable:!0})},rp=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ep(t))!tp.call(e,o)&&o!==r&&On(e,o,{get:()=>t[o],enumerable:!(n=Jc(t,o))||n.enumerable});return e};var hr=e=>rp(On({},"__esModule",{value:!0}),e);var gr,bt,wt,np,yr,br=M(()=>{"use strict";gr=new Map,bt=[],wt=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let n=gr.get(e);if(n===void 0)gr.set(e,{backend:t,priority:r});else{if(n.priority>r)return;if(n.priority===r&&n.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let o=bt.indexOf(e);o!==-1&&bt.splice(o,1);for(let i=0;i<bt.length;i++)if(gr.get(bt[i]).priority<=r){bt.splice(i,0,e);return}bt.push(e)}return}throw new TypeError("not a valid backend")},np=async e=>{let t=gr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(n){return r||(t.error=`${n}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},yr=async e=>{let t=e.executionProviders||[],r=t.map(d=>typeof d=="string"?d:d.name),n=r.length===0?bt:r,o,i=[],a=new Set;for(let d of n){let p=await np(d);typeof p=="string"?i.push({name:d,err:p}):(o||(o=p),o===p&&a.add(d))}if(!o)throw new Error(`no available backend found. ERR: ${i.map(d=>`[${d.name}] ${d.err}`).join(", ")}`);for(let{name:d,err:p}of i)r.includes(d)&&console.warn(`removing requested execution provider "${d}" from session options because it is not available: ${p}`);let l=t.filter(d=>a.has(typeof d=="string"?d:d.name));return[o,new Proxy(e,{get:(d,p)=>p==="executionProviders"?l:Reflect.get(d,p)})]}});var Li=M(()=>{"use strict";br()});var Hi,Gi=M(()=>{"use strict";Hi="1.20.0-dev.20240816-b2d603abda"});var Fi,Ue,Bn=M(()=>{"use strict";Gi();Fi="warning",Ue={wasm:{},webgl:{},webgpu:{},versions:{common:Hi},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Fi=e}},get logLevel(){return Fi}};Object.defineProperty(Ue,"logLevel",{enumerable:!0})});var fe,qi=M(()=>{"use strict";Bn();fe=Ue});var Ki,ji,Yi=M(()=>{"use strict";Ki=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let n=r.getContext("2d");if(n!=null){let o,i;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[3]):(o=e.dims[3],i=e.dims[2]);let a=t?.format!==void 0?t.format:"RGB",l=t?.norm,d,p;l===void 0||l.mean===void 0?d=[255,255,255,255]:typeof l.mean=="number"?d=[l.mean,l.mean,l.mean,l.mean]:(d=[l.mean[0],l.mean[1],l.mean[2],0],l.mean[3]!==void 0&&(d[3]=l.mean[3])),l===void 0||l.bias===void 0?p=[0,0,0,0]:typeof l.bias=="number"?p=[l.bias,l.bias,l.bias,l.bias]:(p=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(p[3]=l.bias[3]));let m=i*o,u=0,h=m,w=m*2,g=-1;a==="RGBA"?(u=0,h=m,w=m*2,g=m*3):a==="RGB"?(u=0,h=m,w=m*2):a==="RBG"&&(u=0,w=m,h=m*2);for(let y=0;y<i;y++)for(let x=0;x<o;x++){let _=(e.data[u++]-p[0])*d[0],$=(e.data[h++]-p[1])*d[1],S=(e.data[w++]-p[2])*d[2],I=g===-1?255:(e.data[g++]-p[3])*d[3];n.fillStyle="rgba("+_+","+$+","+S+","+I+")",n.fillRect(x,y,1,1)}if("toDataURL"in r)return r.toDataURL();throw new Error("toDataURL is not supported")}else throw new Error("Can not access image data")},ji=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),n;if(r!=null){let o,i,a;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[1],a=e.dims[3]):(o=e.dims[3],i=e.dims[2],a=e.dims[1]);let l=t!==void 0&&t.format!==void 0?t.format:"RGB",d=t?.norm,p,m;d===void 0||d.mean===void 0?p=[255,255,255,255]:typeof d.mean=="number"?p=[d.mean,d.mean,d.mean,d.mean]:(p=[d.mean[0],d.mean[1],d.mean[2],255],d.mean[3]!==void 0&&(p[3]=d.mean[3])),d===void 0||d.bias===void 0?m=[0,0,0,0]:typeof d.bias=="number"?m=[d.bias,d.bias,d.bias,d.bias]:(m=[d.bias[0],d.bias[1],d.bias[2],0],d.bias[3]!==void 0&&(m[3]=d.bias[3]));let u=i*o;if(t!==void 0&&(t.format!==void 0&&a===4&&t.format!=="RGBA"||a===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let h=4,w=0,g=1,y=2,x=3,_=0,$=u,S=u*2,I=-1;l==="RGBA"?(_=0,$=u,S=u*2,I=u*3):l==="RGB"?(_=0,$=u,S=u*2):l==="RBG"&&(_=0,S=u,$=u*2),n=r.createImageData(o,i);for(let T=0;T<i*o;w+=h,g+=h,y+=h,x+=h,T++)n.data[w]=(e.data[_++]-m[0])*p[0],n.data[g]=(e.data[$++]-m[1])*p[1],n.data[y]=(e.data[S++]-m[2])*p[2],n.data[x]=I===-1?255:(e.data[I++]-m[3])*p[3]}else throw new Error("Can not access image data");return n}});var Dn,Xi,Zi,Qi,Ji,ea=M(()=>{"use strict";wr();Dn=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:n}=t,o=t.norm??{mean:255,bias:0},i,a;typeof o.mean=="number"?i=[o.mean,o.mean,o.mean,o.mean]:i=[o.mean[0],o.mean[1],o.mean[2],o.mean[3]??255],typeof o.bias=="number"?a=[o.bias,o.bias,o.bias,o.bias]:a=[o.bias[0],o.bias[1],o.bias[2],o.bias[3]??0];let l=t.format!==void 0?t.format:"RGBA",d=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",p=r*n,m=d==="RGBA"?new Float32Array(p*4):new Float32Array(p*3),u=4,h=0,w=1,g=2,y=3,x=0,_=p,$=p*2,S=-1;l==="RGB"&&(u=3,h=0,w=1,g=2,y=-1),d==="RGBA"?S=p*3:d==="RBG"?(x=0,$=p,_=p*2):d==="BGR"&&($=0,_=p,x=p*2);for(let T=0;T<p;T++,h+=u,g+=u,w+=u,y+=u)m[x++]=(e[h]+a[0])/i[0],m[_++]=(e[w]+a[1])/i[1],m[$++]=(e[g]+a[2])/i[2],S!==-1&&y!==-1&&(m[S++]=(e[y]+a[3])/i[3]);return d==="RGBA"?new ze("float32",m,[1,4,r,n]):new ze("float32",m,[1,3,r,n])},Xi=async(e,t)=>{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,n=typeof ImageData<"u"&&e instanceof ImageData,o=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,i=typeof e=="string",a,l=t??{},d=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},p=m=>m instanceof HTMLCanvasElement||m instanceof OffscreenCanvas?m.getContext("2d"):null;if(r){let m=d();m.width=e.width,m.height=e.height;let u=p(m);if(u!=null){let h=e.height,w=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(h=t.resizedHeight,w=t.resizedWidth),t!==void 0){if(l=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");l.tensorFormat="RGBA",l.height=h,l.width=w}else l.tensorFormat="RGBA",l.height=h,l.width=w;u.drawImage(e,0,0),a=u.getImageData(0,0,w,h).data}else throw new Error("Can not access image data")}else if(n){let m,u;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(m=t.resizedHeight,u=t.resizedWidth):(m=e.height,u=e.width),t!==void 0&&(l=t),l.format="RGBA",l.height=m,l.width=u,t!==void 0){let h=d();h.width=u,h.height=m;let w=p(h);if(w!=null)w.putImageData(e,0,0),a=w.getImageData(0,0,u,m).data;else throw new Error("Can not access image data")}else a=e.data}else if(o){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let m=d();m.width=e.width,m.height=e.height;let u=p(m);if(u!=null){let h=e.height,w=e.width;return u.drawImage(e,0,0,w,h),a=u.getImageData(0,0,w,h).data,l.height=h,l.width=w,Dn(a,l)}else throw new Error("Can not access image data")}else{if(i)return new Promise((m,u)=>{let h=d(),w=p(h);if(!e||!w)return u();let g=new Image;g.crossOrigin="Anonymous",g.src=e,g.onload=()=>{h.width=g.width,h.height=g.height,w.drawImage(g,0,0,h.width,h.height);let y=w.getImageData(0,0,h.width,h.height);l.height=h.height,l.width=h.width,m(Dn(y.data,l))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(a!==void 0)return Dn(a,l);throw new Error("Input data provided is not supported - aborted tensor creation")},Zi=(e,t)=>{let{width:r,height:n,download:o,dispose:i}=t,a=[1,n,r,4];return new ze({location:"texture",type:"float32",texture:e,dims:a,download:o,dispose:i})},Qi=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new ze({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:n,download:o,dispose:i})},Ji=(e,t,r)=>new ze({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})});var vt,Ht,ta,ra,na=M(()=>{"use strict";vt=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),Ht=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),ta=!1,ra=()=>{if(!ta){ta=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=typeof Float16Array<"u"&&Float16Array.from;e&&(vt.set("int64",BigInt64Array),Ht.set(BigInt64Array,"int64")),t&&(vt.set("uint64",BigUint64Array),Ht.set(BigUint64Array,"uint64")),r?(vt.set("float16",Float16Array),Ht.set(Float16Array,"float16")):vt.set("float16",Uint16Array)}}});var oa,ia,aa=M(()=>{"use strict";wr();oa=e=>{let t=1;for(let r=0;r<e.length;r++){let n=e[r];if(typeof n!="number"||!Number.isSafeInteger(n))throw new TypeError(`dims[${r}] must be an integer, got: ${n}`);if(n<0)throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${n}`);t*=n}return t},ia=(e,t)=>{switch(e.location){case"cpu":return new ze(e.type,e.data,t);case"cpu-pinned":return new ze({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new ze({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new ze({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var ze,wr=M(()=>{"use strict";Yi();ea();na();aa();ze=class{constructor(t,r,n){ra();let o,i;if(typeof t=="object"&&"location"in t)switch(this.dataLocation=t.location,o=t.type,i=t.dims,t.location){case"cpu-pinned":{let l=vt.get(o);if(!l)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(t.data instanceof l))throw new TypeError(`buffer should be of type ${l.name}`);this.cpuData=t.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let l,d;if(typeof t=="string")if(o=t,d=n,t==="string"){if(!Array.isArray(r))throw new TypeError("A string tensor's data must be a string array.");l=r}else{let p=vt.get(t);if(p===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if(t==="float16"&&p===Uint16Array||t==="uint4"||t==="int4")throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${p.name} as data.`);t==="uint64"||t==="int64"?l=p.from(r,BigInt):l=p.from(r)}else if(r instanceof p)l=r;else throw new TypeError(`A ${o} tensor's data must be type of ${p}`)}else if(d=r,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let p=typeof t[0];if(p==="string")o="string",l=t;else if(p==="boolean")o="bool",l=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${p}.`)}else{let p=Ht.get(t.constructor);if(p===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);o=p,l=t}if(d===void 0)d=[l.length];else if(!Array.isArray(d))throw new TypeError("A tensor's dims must be a number array");i=d,this.cpuData=l,this.dataLocation="cpu"}let a=oa(i);if(this.cpuData&&a!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(a/2)===this.cpuData.length))throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=i,this.size=a}static async fromImage(t,r){return Xi(t,r)}static fromTexture(t,r){return Zi(t,r)}static fromGpuBuffer(t,r){return Qi(t,r)}static fromPinnedBuffer(t,r,n){return Ji(t,r,n)}toDataURL(t){return Ki(this,t)}toImageData(t){return ji(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return ia(this,t)}}});var Pe,vr=M(()=>{"use strict";wr();Pe=ze});var $r,sa,Ve,De,Rn=M(()=>{"use strict";Bn();$r=(e,t)=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||console.timeStamp(`${e}::ORT::${t}`)},sa=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],n=!1;for(let o=0;o<r.length;o++){if(n&&!r[o].includes("TRACE_FUNC")){let i=`FUNC_${e}::${r[o].trim().split(" ")[1]}`;t&&(i+=`::${t}`),$r("CPU",i);return}r[o].includes("TRACE_FUNC")&&(n=!0)}},Ve=e=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||sa("BEGIN",e)},De=e=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||sa("END",e)}});var _r,ua=M(()=>{"use strict";br();vr();Rn();_r=class e{constructor(t){this.handler=t}async run(t,r,n){Ve();let o={},i={};if(typeof t!="object"||t===null||t instanceof Pe||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let a=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Pe)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");a=!1;for(let p of r){if(typeof p!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(p)===-1)throw new RangeError(`'fetches' contains invalid output name: ${p}.`);o[p]=null}if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,m=Object.getOwnPropertyNames(r);for(let u of this.outputNames)if(m.indexOf(u)!==-1){let h=r[u];(h===null||h instanceof Pe)&&(p=!0,a=!1,o[u]=h)}if(p){if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else i=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let p of this.inputNames)if(typeof t[p]>"u")throw new Error(`input '${p}' is missing in 'feeds'.`);if(a)for(let p of this.outputNames)o[p]=null;let l=await this.handler.run(t,o,i),d={};for(let p in l)if(Object.hasOwnProperty.call(l,p)){let m=l[p];m instanceof Pe?d[p]=m:d[p]=new Pe(m.type,m.data,m.dims)}return De(),d}async release(){return this.handler.dispose()}static async create(t,r,n,o){Ve();let i,a={};if(typeof t=="string"){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let m=t,u=0,h=t.byteLength;if(typeof r=="object"&&r!==null)a=r;else if(typeof r=="number"){if(u=r,!Number.isSafeInteger(u))throw new RangeError("'byteOffset' must be an integer.");if(u<0||u>=m.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${m.byteLength}).`);if(h=t.byteLength-u,typeof n=="number"){if(h=n,!Number.isSafeInteger(h))throw new RangeError("'byteLength' must be an integer.");if(h<=0||u+h>m.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${m.byteLength-u}].`);if(typeof o=="object"&&o!==null)a=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(typeof n<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");i=new Uint8Array(m,u,h)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[l,d]=await yr(a),p=await l.createInferenceSessionHandler(i,d);return De(),new e(p)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var op,da=M(()=>{"use strict";ua();op=_r});var la=M(()=>{"use strict"});var ca=M(()=>{"use strict"});var pa=M(()=>{"use strict"});var ma=M(()=>{"use strict"});var ip,xr,fa=M(()=>{"use strict";br();vr();ip="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",xr=class e{constructor(t,r,n){this.handler=t,this.hasOptimizerModel=r,this.hasEvalModel=n}get trainingInputNames(){return this.handler.inputNames}get trainingOutputNames(){return this.handler.outputNames}get evalInputNames(){if(this.hasEvalModel)return this.handler.evalInputNames;throw new Error("This training session has no evalModel loaded.")}get evalOutputNames(){if(this.hasEvalModel)return this.handler.evalOutputNames;throw new Error("This training session has no evalModel loaded.")}static async create(t,r){let n=t.evalModel||"",o=t.optimizerModel||"",i=r||{},[a,l]=await yr(i);if(a.createTrainingSessionHandler){let d=await a.createTrainingSessionHandler(t.checkpointState,t.trainModel,n,o,l);return new e(d,!!t.optimizerModel,!!t.evalModel)}else throw new Error(ip)}typeNarrowingForRunStep(t,r,n,o,i){let a={},l={};if(typeof n!="object"||n===null||n instanceof Pe||Array.isArray(n))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof o=="object"){if(o===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(o instanceof Pe)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(o)){if(o.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let p of o){if(typeof p!="string")throw new TypeError("'fetches' must be a string array or an object.");if(r.indexOf(p)===-1)throw new RangeError(`'fetches' contains invalid output name: ${p}.`);a[p]=null}if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,m=Object.getOwnPropertyNames(o);for(let u of r)if(m.indexOf(u)!==-1){let h=o[u];(h===null||h instanceof Pe)&&(p=!0,d=!1,a[u]=h)}if(p){if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else l=o}}else if(typeof o<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let p of t)if(typeof n[p]>"u")throw new Error(`input '${p}' is missing in 'feeds'.`);if(d)for(let p of r)a[p]=null;return[a,l]}convertHandlerReturnTypeToMapOfTensors(t){let r={};for(let n in t)if(Object.hasOwnProperty.call(t,n)){let o=t[n];o instanceof Pe?r[n]=o:r[n]=new Pe(o.type,o.data,o.dims)}return r}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(t,r,n){let[o,i]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,t,r,n),a=await this.handler.runTrainStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}async runOptimizerStep(t){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(t||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(t,r,n){if(this.hasEvalModel){let[o,i]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,t,r,n),a=await this.handler.runEvalStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(t=!0){return this.handler.getParametersSize(t)}async loadParametersBuffer(t,r=!0){let n=await this.getParametersSize(r);if(t.length!==4*n)throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check.");return this.handler.loadParametersBuffer(t,r)}async getContiguousParameters(t=!0){return this.handler.getContiguousParameters(t)}async release(){return this.handler.dispose()}}});var ap,ha=M(()=>{"use strict";fa();ap=xr});var Mn={};Lt(Mn,{InferenceSession:()=>op,TRACE:()=>$r,TRACE_FUNC_BEGIN:()=>Ve,TRACE_FUNC_END:()=>De,Tensor:()=>Pe,TrainingSession:()=>ap,env:()=>fe,registerBackend:()=>wt});var qe=M(()=>{"use strict";Li();qi();da();vr();la();ca();Rn();pa();ma();ha()});var Sr=M(()=>{"use strict"});var wa={};Lt(wa,{default:()=>sp});var ya,ba,sp,va=M(()=>{"use strict";Un();$t();Gt();ya="ort-wasm-proxy-worker",ba=globalThis.self?.name===ya;ba&&(self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":Ir(r.wasm).then(()=>{Cr(r).then(()=>{postMessage({type:t})},n=>{postMessage({type:t,err:n})})},n=>{postMessage({type:t,err:n})});break;case"init-ep":{let{epName:n,env:o}=r;Tr(o,n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})});break}case"copy-from":{let{buffer:n}=r,o=Ft(n);postMessage({type:t,out:o});break}case"create":{let{model:n,options:o}=r;Ar(n,o).then(i=>{postMessage({type:t,out:i})},i=>{postMessage({type:t,err:i})});break}case"release":kr(r),postMessage({type:t});break;case"run":{let{sessionId:n,inputIndices:o,inputs:i,outputIndices:a,options:l}=r;Er(n,o,i,a,new Array(a.length).fill(null),l).then(d=>{d.some(p=>p[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:d},Or([...i,...d]))},d=>{postMessage({type:t,err:d})});break}case"end-profiling":Pr(r),postMessage({type:t});break;default:}}catch(n){postMessage({type:t,err:n})}});sp=ba?null:e=>new Worker(e??Dt,{type:"module",name:ya})});var _a={};Lt(_a,{default:()=>up});var Vn,$a,up,xa=M(()=>{"use strict";$a=(Vn=import.meta.url,async function(e={}){function t(){return ie.buffer!=ne.buffer&&Se(),ne}function r(){return ie.buffer!=ne.buffer&&Se(),Z}function n(){return ie.buffer!=ne.buffer&&Se(),ee}function o(){return ie.buffer!=ne.buffer&&Se(),K}function i(){return ie.buffer!=ne.buffer&&Se(),me}function a(){return ie.buffer!=ne.buffer&&Se(),$e}function l(){return ie.buffer!=ne.buffer&&Se(),R}function d(){return ie.buffer!=ne.buffer&&Se(),Be}var p,m,u=e,h=new Promise((s,c)=>{p=s,m=c}),w=typeof window=="object",g=typeof importScripts=="function",y=g&&self.name=="em-pthread";u.mountExternalData=(s,c)=>{s.startsWith("./")&&(s=s.substring(2)),(u.Ab||(u.Ab=new Map)).set(s,c)},u.unmountExternalData=()=>{delete u.Ab};var x=globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let _=()=>{let s=(f,b,v)=>(...C)=>{let P=Ze,B=b?.();C=f(...C);let N=b?.();return B!==N&&(f=N,v(B),b=v=null),Ze!=P?new Promise((H,J)=>{Sn={resolve:H,reject:J}}):C},c=f=>async(...b)=>{try{if(u.zb)throw Error("Session already started");let v=u.zb={Ub:b[0],errors:[]},C=await f(...b);if(u.zb!==v)throw Error("Session mismatch");u.Eb?.flush();let P=v.errors;if(0<P.length){let B=await Promise.all(P);if(B=B.filter(N=>N),0<B.length)throw Error(B.join(`
3803
+ `))}return C}finally{u.zb=null}};u._OrtCreateSession=s(u._OrtCreateSession,()=>u._OrtCreateSession,f=>u._OrtCreateSession=f),u._OrtRun=c(s(u._OrtRun,()=>u._OrtRun,f=>u._OrtRun=f)),u._OrtRunWithBinding=c(s(u._OrtRunWithBinding,()=>u._OrtRunWithBinding,f=>u._OrtRunWithBinding=f)),u._OrtBindInput=s(u._OrtBindInput,()=>u._OrtBindInput,f=>u._OrtBindInput=f),_=void 0};u.jsepInit=(s,c)=>{if(_?.(),s==="webgpu"){[u.Eb,u.Lb,u.Pb,u.Fb,u.Ob,u.hb,u.Qb,u.Sb,u.Mb,u.Nb,u.Rb]=c;let f=u.Eb;u.jsepRegisterBuffer=(b,v,C,P)=>f.registerBuffer(b,v,C,P),u.jsepGetBuffer=b=>f.getBuffer(b),u.jsepCreateDownloader=(b,v,C)=>f.createDownloader(b,v,C),u.jsepOnReleaseSession=b=>{f.onReleaseSession(b)},u.jsepOnRunStart=b=>f.onRunStart(b)}};var $,S,I=Object.assign({},u),T="./this.program",A=(s,c)=>{throw c},O="";(w||g)&&(g?O=self.location.href:typeof document<"u"&&document.currentScript&&(O=document.currentScript.src),Vn&&(O=Vn),O=O.startsWith("blob:")?"":O.substr(0,O.replace(/[?#].*/,"").lastIndexOf("/")+1),g&&(S=s=>{var c=new XMLHttpRequest;return c.open("GET",s,!1),c.responseType="arraybuffer",c.send(null),new Uint8Array(c.response)}),$=s=>Ao(s)?new Promise((c,f)=>{var b=new XMLHttpRequest;b.open("GET",s,!0),b.responseType="arraybuffer",b.onload=()=>{(b.status==200||b.status==0&&b.response)&&f(b.response),c(b.status)},b.onerror=c,b.send(null)}):fetch(s,{credentials:"same-origin"}).then(c=>c.ok?c.arrayBuffer():Promise.reject(Error(c.status+" : "+c.url))));var z,W=console.log.bind(console),V=console.error.bind(console),j=W,Y=V;if(Object.assign(u,I),I=null,y){let s=function(c){try{var f=c.data,b=f.cmd;if(b==="load"){let v=[];self.onmessage=C=>v.push(C),self.startWorker=()=>{postMessage({cmd:"loaded"});for(let C of v)s(C);self.onmessage=s};for(let C of f.handlers)u[C]&&!u[C].proxy||(u[C]=(...P)=>{postMessage({Db:"callHandler",Yb:C,args:P})},C=="print"&&(j=u[C]),C=="printErr"&&(Y=u[C]));ie=f.wasmMemory,Se(),ae(f.wasmModule)}else if(b==="run"){An(f.pthread_ptr,0,0,1,0,0),$n(f.pthread_ptr),Ul(),Ro(),se||(Pi(),se=!0);try{Vl(f.start_routine,f.arg)}catch(v){if(v!="unwind")throw v}}else b==="cancel"?zt()&&mr(-1):f.target!=="setimmediate"&&(b==="checkMailbox"?se&&or():b&&(Y(`worker: received unknown command ${b}`),Y(f)))}catch(v){throw Oi(),v}};var _h=s,ae,se=!1;Y=function(...c){c=c.join(" "),console.error(c)},self.alert=function(...c){postMessage({Db:"alert",text:c.join(" "),$b:zt()})},u.instantiateWasm=(c,f)=>new Promise(b=>{ae=v=>{v=new WebAssembly.Instance(v,Po()),f(v),b()}}),self.onunhandledrejection=c=>{throw c.reason||c},self.onmessage=s}u.wasmBinary&&(z=u.wasmBinary);var ie,q,ce,ne,Z,ee,K,me,$e,R,G,ue,Be,ge=!1;function Se(){var s=ie.buffer;u.HEAP8=ne=new Int8Array(s),u.HEAP16=ee=new Int16Array(s),u.HEAPU8=Z=new Uint8Array(s),u.HEAPU16=K=new Uint16Array(s),u.HEAP32=me=new Int32Array(s),u.HEAPU32=$e=new Uint32Array(s),u.HEAPF32=R=new Float32Array(s),u.HEAPF64=Be=new Float64Array(s),u.HEAP64=G=new BigInt64Array(s),u.HEAPU64=ue=new BigUint64Array(s)}if(!y){if(!((ie=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0})).buffer instanceof x))throw Y("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),Error("bad memory");Se()}var Wt=[],Te=[],Oe=[],He=0,kt=null,ht=null;function Co(){if(--He==0&&(kt!==null&&(clearInterval(kt),kt=null),ht)){var s=ht;ht=null,s()}}function Et(s){throw Y(s="Aborted("+s+")"),ge=!0,ce=1,s=new WebAssembly.RuntimeError(s+". Build with -sASSERTIONS for more info."),m(s),s}var ln,To=s=>s.startsWith("data:application/octet-stream;base64,"),Ao=s=>s.startsWith("file://");function ko(s){if(s==ln&&z)return new Uint8Array(z);if(S)return S(s);throw"both async and sync fetching of the wasm failed"}function Eo(s,c,f){return function(b){return z?Promise.resolve().then(()=>ko(b)):$(b).then(v=>new Uint8Array(v),()=>ko(b))}(s).then(b=>WebAssembly.instantiate(b,c)).then(f,b=>{Y(`failed to asynchronously prepare wasm: ${b}`),Et(b)})}function Po(){return{a:{L:Ml,xa:Rl,b:Wl,Z:No,y:Ho,na:Go,V:qo,X:Ko,oa:jo,la:Yo,ea:Xo,ka:Zo,I:Qo,W:Jo,T:ei,ma:ti,U:ri,sa:Ll,B:Gl,O:Fl,N:Kl,A:Yl,r:Xl,p:Zl,C:Ql,x:ic,P:ac,ra:sc,ha:uc,R:dc,_:lc,E:cc,ga:$n,qa:pc,t:mc,D:gc,o:yc,m:wc,c:wn,n:vc,k:xc,ya:Sc,q:Ic,g:Cc,u:Tc,l:Ac,f:kc,i:Ec,j:Pc,h:Oc,e:zc,ba:Bc,ca:Dc,da:Rc,$:yi,aa:bi,ua:Mc,d:Uc,M:Vc,F:Nc,J:Wc,v:Lc,pa:Hc,S:Gc,s:vi,w:Fc,K:qc,Q:Kc,wa:jc,va:Yc,ia:xi,ja:Si,Y:hn,z:Ii,H:Ci,fa:Ti,G:Ai,a:ie,ta:fn}}}var cn={858772:(s,c,f,b)=>{if(u===void 0||!u.Ab)return 1;if((s=Ee(s>>>0)).startsWith("./")&&(s=s.substring(2)),!(s=u.Ab.get(s)))return 2;if(b>>>=0,(c>>>=0)+(f>>>=0)>s.byteLength)return 3;try{return r().set(s.subarray(c,c+f),b>>>0),0}catch{return 4}},859273:()=>{u.Mb()},859304:()=>{u.Nb()},859333:()=>{u.Rb()},859358:s=>u.Lb(s),859391:s=>u.Pb(s),859423:(s,c,f)=>{u.Fb(s,c,f,!0)},859462:(s,c,f)=>{u.Fb(s,c,f)},859495:()=>typeof wasmOffsetConverter<"u",859552:s=>{u.hb("Abs",s,void 0)},859603:s=>{u.hb("Neg",s,void 0)},859654:s=>{u.hb("Floor",s,void 0)},859707:s=>{u.hb("Ceil",s,void 0)},859759:s=>{u.hb("Reciprocal",s,void 0)},859817:s=>{u.hb("Sqrt",s,void 0)},859869:s=>{u.hb("Exp",s,void 0)},859920:s=>{u.hb("Erf",s,void 0)},859971:s=>{u.hb("Sigmoid",s,void 0)},860026:(s,c,f)=>{u.hb("HardSigmoid",s,{alpha:c,beta:f})},860105:s=>{u.hb("Log",s,void 0)},860156:s=>{u.hb("Sin",s,void 0)},860207:s=>{u.hb("Cos",s,void 0)},860258:s=>{u.hb("Tan",s,void 0)},860309:s=>{u.hb("Asin",s,void 0)},860361:s=>{u.hb("Acos",s,void 0)},860413:s=>{u.hb("Atan",s,void 0)},860465:s=>{u.hb("Sinh",s,void 0)},860517:s=>{u.hb("Cosh",s,void 0)},860569:s=>{u.hb("Asinh",s,void 0)},860622:s=>{u.hb("Acosh",s,void 0)},860675:s=>{u.hb("Atanh",s,void 0)},860728:s=>{u.hb("Tanh",s,void 0)},860780:s=>{u.hb("Not",s,void 0)},860831:(s,c,f)=>{u.hb("Clip",s,{min:c,max:f})},860900:s=>{u.hb("Clip",s,void 0)},860952:(s,c)=>{u.hb("Elu",s,{alpha:c})},861010:s=>{u.hb("Gelu",s,void 0)},861062:s=>{u.hb("Relu",s,void 0)},861114:(s,c)=>{u.hb("LeakyRelu",s,{alpha:c})},861178:(s,c)=>{u.hb("ThresholdedRelu",s,{alpha:c})},861248:(s,c)=>{u.hb("Cast",s,{to:c})},861306:s=>{u.hb("Add",s,void 0)},861357:s=>{u.hb("Sub",s,void 0)},861408:s=>{u.hb("Mul",s,void 0)},861459:s=>{u.hb("Div",s,void 0)},861510:s=>{u.hb("Pow",s,void 0)},861561:s=>{u.hb("Equal",s,void 0)},861614:s=>{u.hb("Greater",s,void 0)},861669:s=>{u.hb("GreaterOrEqual",s,void 0)},861731:s=>{u.hb("Less",s,void 0)},861783:s=>{u.hb("LessOrEqual",s,void 0)},861842:(s,c,f,b,v)=>{u.hb("ReduceMean",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862001:(s,c,f,b,v)=>{u.hb("ReduceMax",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862159:(s,c,f,b,v)=>{u.hb("ReduceMin",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862317:(s,c,f,b,v)=>{u.hb("ReduceProd",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862476:(s,c,f,b,v)=>{u.hb("ReduceSum",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862634:(s,c,f,b,v)=>{u.hb("ReduceL1",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862791:(s,c,f,b,v)=>{u.hb("ReduceL2",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862948:(s,c,f,b,v)=>{u.hb("ReduceLogSum",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863109:(s,c,f,b,v)=>{u.hb("ReduceSumSquare",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863273:(s,c,f,b,v)=>{u.hb("ReduceLogSumExp",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863437:s=>{u.hb("Where",s,void 0)},863490:(s,c,f)=>{u.hb("Transpose",s,{perm:c?Array.from(i().subarray(c>>>0,f>>>0)):[]})},863598:(s,c,f,b)=>{u.hb("DepthToSpace",s,{blocksize:c,mode:Ee(f),format:b?"NHWC":"NCHW"})},863731:(s,c,f,b)=>{u.hb("DepthToSpace",s,{blocksize:c,mode:Ee(f),format:b?"NHWC":"NCHW"})},863864:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt)=>{u.hb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:c,dilations:[f],group:b,kernelShape:[v],pads:[C,P],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:J?Array.from(i().subarray(J>>>0,de>>>0)):[],outputShape:ye?Array.from(i().subarray(ye>>>0,Ae>>>0)):[],activation:Ee(Bt)})},864265:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:c,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(P>>>0,2+(P>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,J>>>0)):[],outputShape:de?Array.from(i().subarray(de>>>0,ye>>>0)):[],activation:Ee(Ae)})},864830:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt)=>{u.hb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:c,dilations:[f],group:b,kernelShape:[v],pads:[C,P],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:J?Array.from(i().subarray(J>>>0,de>>>0)):[],outputShape:ye?Array.from(i().subarray(ye>>>0,Ae>>>0)):[],activation:Ee(Bt)})},865231:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:c,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(P>>>0,2+(P>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,J>>>0)):[],outputShape:de?Array.from(i().subarray(de>>>0,ye>>>0)):[],activation:Ee(Ae)})},865796:(s,c)=>{u.hb("GlobalAveragePool",s,{format:c?"NHWC":"NCHW"})},865887:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("AveragePool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},866302:(s,c)=>{u.hb("GlobalAveragePool",s,{format:c?"NHWC":"NCHW"})},866393:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("AveragePool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},866808:(s,c)=>{u.hb("GlobalMaxPool",s,{format:c?"NHWC":"NCHW"})},866895:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("MaxPool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},867306:(s,c)=>{u.hb("GlobalMaxPool",s,{format:c?"NHWC":"NCHW"})},867393:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("MaxPool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},867804:(s,c,f,b,v)=>{u.hb("Gemm",s,{alpha:c,beta:f,transA:b,transB:v})},867908:s=>{u.hb("MatMul",s,void 0)},867962:(s,c,f,b)=>{u.hb("ArgMax",s,{keepDims:!!c,selectLastIndex:!!f,axis:b})},868070:(s,c,f,b)=>{u.hb("ArgMin",s,{keepDims:!!c,selectLastIndex:!!f,axis:b})},868178:(s,c)=>{u.hb("Softmax",s,{axis:c})},868241:(s,c)=>{u.hb("Concat",s,{axis:c})},868301:(s,c,f,b,v)=>{u.hb("Split",s,{axis:c,numOutputs:f,splitSizes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},868441:s=>{u.hb("Expand",s,void 0)},868495:(s,c)=>{u.hb("Gather",s,{axis:Number(c)})},868566:(s,c)=>{u.hb("GatherElements",s,{axis:Number(c)})},868645:(s,c,f,b,v,C,P,B,N,H,J)=>{u.hb("Resize",s,{antialias:c,axes:f?Array.from(i().subarray(f>>>0,b>>>0)):[],coordinateTransformMode:Ee(v),cubicCoeffA:C,excludeOutside:P,extrapolationValue:B,keepAspectRatioPolicy:Ee(N),mode:Ee(H),nearestMode:Ee(J)})},868991:(s,c,f,b,v,C,P)=>{u.hb("Slice",s,{starts:c?Array.from(i().subarray(c>>>0,f>>>0)):[],ends:b?Array.from(i().subarray(b>>>0,v>>>0)):[],axes:C?Array.from(i().subarray(C>>>0,P>>>0)):[]})},869207:s=>{u.hb("Tile",s,void 0)},869259:(s,c,f)=>{u.hb("InstanceNormalization",s,{epsilon:c,format:f?"NHWC":"NCHW"})},869373:(s,c,f)=>{u.hb("InstanceNormalization",s,{epsilon:c,format:f?"NHWC":"NCHW"})},869487:s=>{u.hb("Range",s,void 0)},869540:(s,c)=>{u.hb("Einsum",s,{equation:Ee(c)})},869621:(s,c,f,b,v)=>{u.hb("Pad",s,{mode:c,value:f,pads:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},869748:(s,c,f,b,v,C)=>{u.hb("BatchNormalization",s,{epsilon:c,momentum:f,spatial:!!v,trainingMode:!!b,format:C?"NHWC":"NCHW"})},869917:(s,c,f,b,v,C)=>{u.hb("BatchNormalization",s,{epsilon:c,momentum:f,spatial:!!v,trainingMode:!!b,format:C?"NHWC":"NCHW"})},870086:(s,c,f)=>{u.hb("CumSum",s,{exclusive:Number(c),reverse:Number(f)})},870183:(s,c,f)=>{u.hb("DequantizeLinear",s,{axis:c,blockSize:f})},870273:(s,c,f,b,v,C,P,B,N)=>{u.hb("Attention",s,{numHeads:c,isUnidirectional:f,maskFilterValue:b,scale:v,doRotary:C,qkvHiddenSizes:P?Array.from(i().subarray(Number(B)>>>0,Number(B)+P>>>0)):[],pastPresentShareBuffer:!!N})},870545:s=>{u.hb("BiasAdd",s,void 0)},870600:s=>{u.hb("BiasSplitGelu",s,void 0)},870661:s=>{u.hb("FastGelu",s,void 0)},870717:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt,Qc)=>{u.hb("Conv",s,{format:de?"NHWC":"NCHW",auto_pad:c,dilations:f?Array.from(i().subarray(f>>>0,b>>>0)):[],group:v,kernel_shape:C?Array.from(i().subarray(C>>>0,P>>>0)):[],pads:B?Array.from(i().subarray(B>>>0,N>>>0)):[],strides:H?Array.from(i().subarray(H>>>0,J>>>0)):[],w_is_const:()=>!!t()[ye>>>0],activation:Ee(Ae),activation_params:Bt?Array.from(l().subarray(Bt>>>0,Qc>>>0)):[]})},871213:s=>{u.hb("Gelu",s,void 0)},871265:(s,c,f,b)=>{u.hb("GroupQueryAttention",s,{numHeads:c,kvNumHeads:f,scale:b})},871378:(s,c,f,b)=>{u.hb("LayerNormalization",s,{axis:c,epsilon:f,simplified:!!b})},871489:(s,c,f,b)=>{u.hb("LayerNormalization",s,{axis:c,epsilon:f,simplified:!!b})},871600:(s,c,f,b,v,C)=>{u.hb("MatMulNBits",s,{k:c,n:f,accuracyLevel:b,bits:v,blockSize:C})},871727:(s,c,f,b,v,C)=>{u.hb("MultiHeadAttention",s,{numHeads:c,isUnidirectional:f,maskFilterValue:b,scale:v,doRotary:C})},871886:(s,c)=>{u.hb("QuickGelu",s,{alpha:c})},871950:(s,c,f,b,v)=>{u.hb("RotaryEmbedding",s,{interleaved:!!c,numHeads:f,rotaryEmbeddingDim:b,scale:v})},872089:(s,c,f)=>{u.hb("SkipLayerNormalization",s,{epsilon:c,simplified:!!f})},872191:s=>{u.Qb(s)},872225:(s,c)=>u.Sb(s,c,u.zb.Ub,u.zb.errors),872337:(s,c,f)=>{u.hb("SkipLayerNormalization",s,{epsilon:c,simplified:!!f})}};function Rl(s,c,f){return ci(async()=>{await u.Ob(s,c,f)})}function Ml(){return typeof wasmOffsetConverter<"u"}function pn(s){this.name="ExitStatus",this.message=`Program terminated with exit(${s})`,this.status=s}var mn=s=>{s.terminate(),s.onmessage=()=>{}},Oo=s=>{dt.length==0&&(Uo(),Mo(dt[0]));var c=dt.pop();if(!c)return 6;yt.push(c),Ye[s.sb]=c,c.sb=s.sb;var f={cmd:"run",start_routine:s.Vb,arg:s.Hb,pthread_ptr:s.sb};return c.postMessage(f,s.Wb),0},gt=0,ve=(s,c,...f)=>{for(var b=2*f.length,v=Pn(),C=En(8*b),P=C>>>3,B=0;B<f.length;B++){var N=f[B];typeof N=="bigint"?(G[P+2*B]=1n,G[P+2*B+1]=N):(G[P+2*B]=0n,d()[P+2*B+1>>>0]=N)}return s=zi(s,0,b,C,c),fr(v),s};function fn(s){if(y)return ve(0,1,s);if(ce=s,!(0<gt)){for(var c of yt)mn(c);for(c of dt)mn(c);dt=[],yt=[],Ye=[],ge=!0}A(s,new pn(s))}function zo(s){if(y)return ve(1,0,s);hn(s)}var hn=s=>{if(ce=s,y)throw zo(s),"unwind";fn(s)},dt=[],yt=[],Bo=[],Ye={},Do=s=>{var c=s.sb;delete Ye[c],dt.push(s),yt.splice(yt.indexOf(s),1),s.sb=0,kn(c)};function Ro(){Bo.forEach(s=>s())}var Mo=s=>new Promise(c=>{s.onmessage=v=>{var C=(v=v.data).cmd;if(v.targetThread&&v.targetThread!=zt()){var P=Ye[v.targetThread];P?P.postMessage(v,v.transferList):Y(`Internal error! Worker sent a message "${C}" to target pthread ${v.targetThread}, but that thread no longer exists!`)}else C==="checkMailbox"?or():C==="spawnThread"?Oo(v):C==="cleanupThread"?Do(Ye[v.thread]):C==="killThread"?(v=v.thread,C=Ye[v],delete Ye[v],mn(C),kn(v),yt.splice(yt.indexOf(C),1),C.sb=0):C==="cancelThread"?Ye[v.thread].postMessage({cmd:"cancel"}):C==="loaded"?(s.loaded=!0,c(s)):C==="alert"?alert(`Thread ${v.threadId}: ${v.text}`):v.target==="setimmediate"?s.postMessage(v):C==="callHandler"?u[v.handler](...v.args):C&&Y(`worker sent an unknown command ${C}`)},s.onerror=v=>{throw Y(`worker sent an error! ${v.filename}:${v.lineno}: ${v.message}`),v};var f,b=[];for(f of[])u.propertyIsEnumerable(f)&&b.push(f);s.postMessage({cmd:"load",handlers:b,wasmMemory:ie,wasmModule:q})});function Uo(){var s=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});dt.push(s)}var nr=s=>{for(;0<s.length;)s.shift()(u)},Ul=()=>{var s=zt(),c=a()[s+52>>>2>>>0];s=a()[s+56>>>2>>>0],Di(c,c-s),fr(c)},Vl=(s,c)=>{gt=0,s=Ri(s,c),0<gt?ce=s:mr(s)};class Nl{constructor(c){this.Bb=c-24}}function Wl(s,c,f){var b=new Nl(s>>>=0);throw c>>>=0,f>>>=0,a()[b.Bb+16>>>2>>>0]=0,a()[b.Bb+4>>>2>>>0]=c,a()[b.Bb+8>>>2>>>0]=f,s}function Vo(s,c,f,b){return y?ve(2,1,s,c,f,b):No(s,c,f,b)}function No(s,c,f,b){if(s>>>=0,c>>>=0,f>>>=0,b>>>=0,x===void 0)return Y("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var v=[];return y&&v.length===0?Vo(s,c,f,b):(s={Vb:f,sb:s,Hb:b,Wb:v},y?(s.Db="spawnThread",postMessage(s,v),0):Oo(s))}var Wo=typeof TextDecoder<"u"?new TextDecoder:void 0,Lo=(s,c,f)=>{var b=(c>>>=0)+f;for(f=c;s[f]&&!(f>=b);)++f;if(16<f-c&&s.buffer&&Wo)return Wo.decode(s.buffer instanceof x?s.slice(c,f):s.subarray(c,f));for(b="";c<f;){var v=s[c++];if(128&v){var C=63&s[c++];if((224&v)==192)b+=String.fromCharCode((31&v)<<6|C);else{var P=63&s[c++];65536>(v=(240&v)==224?(15&v)<<12|C<<6|P:(7&v)<<18|C<<12|P<<6|63&s[c++])?b+=String.fromCharCode(v):(v-=65536,b+=String.fromCharCode(55296|v>>10,56320|1023&v))}}else b+=String.fromCharCode(v)}return b},Ee=(s,c)=>(s>>>=0)?Lo(r(),s,c):"";function Ho(s,c,f){return y?ve(3,1,s,c,f):0}function Go(s,c){if(y)return ve(4,1,s,c)}var Fo=s=>{for(var c=0,f=0;f<s.length;++f){var b=s.charCodeAt(f);127>=b?c++:2047>=b?c+=2:55296<=b&&57343>=b?(c+=4,++f):c+=3}return c},Pt=(s,c,f)=>{var b=r();if(c>>>=0,0<f){var v=c;f=c+f-1;for(var C=0;C<s.length;++C){var P=s.charCodeAt(C);if(55296<=P&&57343>=P&&(P=65536+((1023&P)<<10)|1023&s.charCodeAt(++C)),127>=P){if(c>=f)break;b[c++>>>0]=P}else{if(2047>=P){if(c+1>=f)break;b[c++>>>0]=192|P>>6}else{if(65535>=P){if(c+2>=f)break;b[c++>>>0]=224|P>>12}else{if(c+3>=f)break;b[c++>>>0]=240|P>>18,b[c++>>>0]=128|P>>12&63}b[c++>>>0]=128|P>>6&63}b[c++>>>0]=128|63&P}}b[c>>>0]=0,s=c-v}else s=0;return s};function qo(s,c){if(y)return ve(5,1,s,c)}function Ko(s,c,f){if(y)return ve(6,1,s,c,f)}function jo(s,c,f){return y?ve(7,1,s,c,f):0}function Yo(s,c){if(y)return ve(8,1,s,c)}function Xo(s,c,f){if(y)return ve(9,1,s,c,f)}function Zo(s,c,f,b){if(y)return ve(10,1,s,c,f,b)}function Qo(s,c,f,b){if(y)return ve(11,1,s,c,f,b)}function Jo(s,c,f,b){if(y)return ve(12,1,s,c,f,b)}function ei(s){if(y)return ve(13,1,s)}function ti(s,c){if(y)return ve(14,1,s,c)}function ri(s,c,f){if(y)return ve(15,1,s,c,f)}var ni,lt,Ll=()=>{Et("")},Xe=s=>{for(var c="";r()[s>>>0];)c+=ni[r()[s++>>>0]];return c},gn={},yn={},Hl={};function it(s,c,f={}){if(!("argPackAdvance"in c))throw new TypeError("registerType registeredInstance requires argPackAdvance");return function(b,v,C={}){var P=v.name;if(!b)throw new lt(`type "${P}" must have a positive integer typeid pointer`);if(yn.hasOwnProperty(b)){if(C.Jb)return;throw new lt(`Cannot register type '${P}' twice`)}yn[b]=v,delete Hl[b],gn.hasOwnProperty(b)&&(v=gn[b],delete gn[b],v.forEach(B=>B()))}(s,c,f)}var oi=(s,c,f)=>{switch(c){case 1:return f?b=>t()[b>>>0]:b=>r()[b>>>0];case 2:return f?b=>n()[b>>>1>>>0]:b=>o()[b>>>1>>>0];case 4:return f?b=>i()[b>>>2>>>0]:b=>a()[b>>>2>>>0];case 8:return f?b=>G[b>>>3]:b=>ue[b>>>3];default:throw new TypeError(`invalid integer width (${c}): ${s}`)}};function Gl(s,c,f){f>>>=0,it(s>>>=0,{name:c=Xe(c>>>0),fromWireType:b=>b,toWireType:function(b,v){if(typeof v!="bigint"&&typeof v!="number")throw v=v===null?"null":(b=typeof v)=="object"||b==="array"||b==="function"?v.toString():""+v,new TypeError(`Cannot convert "${v}" to ${this.name}`);return typeof v=="number"&&(v=BigInt(v)),v},argPackAdvance:ct,readValueFromPointer:oi(c,f,c.indexOf("u")==-1),yb:null})}var ct=8;function Fl(s,c,f,b){it(s>>>=0,{name:c=Xe(c>>>0),fromWireType:function(v){return!!v},toWireType:function(v,C){return C?f:b},argPackAdvance:ct,readValueFromPointer:function(v){return this.fromWireType(r()[v>>>0])},yb:null})}var bn=[],at=[];function wn(s){9<(s>>>=0)&&--at[s+1]==0&&(at[s]=void 0,bn.push(s))}var Ge=s=>{if(!s)throw new lt("Cannot use deleted val. handle = "+s);return at[s]},Fe=s=>{switch(s){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let c=bn.pop()||at.length;return at[c]=s,at[c+1]=1,c}};function vn(s){return this.fromWireType(a()[s>>>2>>>0])}var ql={name:"emscripten::val",fromWireType:s=>{var c=Ge(s);return wn(s),c},toWireType:(s,c)=>Fe(c),argPackAdvance:ct,readValueFromPointer:vn,yb:null};function Kl(s){return it(s>>>0,ql)}var jl=(s,c)=>{switch(c){case 4:return function(f){return this.fromWireType(l()[f>>>2>>>0])};case 8:return function(f){return this.fromWireType(d()[f>>>3>>>0])};default:throw new TypeError(`invalid float width (${c}): ${s}`)}};function Yl(s,c,f){f>>>=0,it(s>>>=0,{name:c=Xe(c>>>0),fromWireType:b=>b,toWireType:(b,v)=>v,argPackAdvance:ct,readValueFromPointer:jl(c,f),yb:null})}function Xl(s,c,f,b,v){if(s>>>=0,f>>>=0,c=Xe(c>>>0),v===-1&&(v=4294967295),v=B=>B,b===0){var C=32-8*f;v=B=>B<<C>>>C}var P=c.includes("unsigned")?function(B,N){return N>>>0}:function(B,N){return N};it(s,{name:c,fromWireType:v,toWireType:P,argPackAdvance:ct,readValueFromPointer:oi(c,f,b!==0),yb:null})}function Zl(s,c,f){function b(C){var P=a()[C>>>2>>>0];return C=a()[C+4>>>2>>>0],new v(t().buffer,C,P)}var v=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][c];it(s>>>=0,{name:f=Xe(f>>>0),fromWireType:b,argPackAdvance:ct,readValueFromPointer:b},{Jb:!0})}function Ql(s,c){s>>>=0;var f=(c=Xe(c>>>0))==="std::string";it(s,{name:c,fromWireType:function(b){var v=a()[b>>>2>>>0],C=b+4;if(f)for(var P=C,B=0;B<=v;++B){var N=C+B;if(B==v||r()[N>>>0]==0){if(P=Ee(P,N-P),H===void 0)var H=P;else H+=String.fromCharCode(0),H+=P;P=N+1}}else{for(H=Array(v),B=0;B<v;++B)H[B]=String.fromCharCode(r()[C+B>>>0]);H=H.join("")}return Qe(b),H},toWireType:function(b,v){v instanceof ArrayBuffer&&(v=new Uint8Array(v));var C=typeof v=="string";if(!(C||v instanceof Uint8Array||v instanceof Uint8ClampedArray||v instanceof Int8Array))throw new lt("Cannot pass non-string to std::string");var P=f&&C?Fo(v):v.length,B=pr(4+P+1),N=B+4;if(a()[B>>>2>>>0]=P,f&&C)Pt(v,N,P+1);else if(C)for(C=0;C<P;++C){var H=v.charCodeAt(C);if(255<H)throw Qe(N),new lt("String has UTF-16 code units that do not fit in 8 bits");r()[N+C>>>0]=H}else for(C=0;C<P;++C)r()[N+C>>>0]=v[C];return b!==null&&b.push(Qe,B),B},argPackAdvance:ct,readValueFromPointer:vn,yb(b){Qe(b)}})}var ii=typeof TextDecoder<"u"?new TextDecoder("utf-16le"):void 0,Jl=(s,c)=>{for(var f=s>>1,b=f+c/2;!(f>=b)&&o()[f>>>0];)++f;if(32<(f<<=1)-s&&ii)return ii.decode(r().slice(s,f));for(f="",b=0;!(b>=c/2);++b){var v=n()[s+2*b>>>1>>>0];if(v==0)break;f+=String.fromCharCode(v)}return f},ec=(s,c,f)=>{if(f??=2147483647,2>f)return 0;var b=c;f=(f-=2)<2*s.length?f/2:s.length;for(var v=0;v<f;++v){var C=s.charCodeAt(v);n()[c>>>1>>>0]=C,c+=2}return n()[c>>>1>>>0]=0,c-b},tc=s=>2*s.length,rc=(s,c)=>{for(var f=0,b="";!(f>=c/4);){var v=i()[s+4*f>>>2>>>0];if(v==0)break;++f,65536<=v?(v-=65536,b+=String.fromCharCode(55296|v>>10,56320|1023&v)):b+=String.fromCharCode(v)}return b},nc=(s,c,f)=>{if(c>>>=0,f??=2147483647,4>f)return 0;var b=c;f=b+f-4;for(var v=0;v<s.length;++v){var C=s.charCodeAt(v);if(55296<=C&&57343>=C&&(C=65536+((1023&C)<<10)|1023&s.charCodeAt(++v)),i()[c>>>2>>>0]=C,(c+=4)+4>f)break}return i()[c>>>2>>>0]=0,c-b},oc=s=>{for(var c=0,f=0;f<s.length;++f){var b=s.charCodeAt(f);55296<=b&&57343>=b&&++f,c+=4}return c};function ic(s,c,f){if(s>>>=0,c>>>=0,f=Xe(f>>>=0),c===2)var b=Jl,v=ec,C=tc,P=B=>o()[B>>>1>>>0];else c===4&&(b=rc,v=nc,C=oc,P=B=>a()[B>>>2>>>0]);it(s,{name:f,fromWireType:B=>{for(var N,H=a()[B>>>2>>>0],J=B+4,de=0;de<=H;++de){var ye=B+4+de*c;de!=H&&P(ye)!=0||(J=b(J,ye-J),N===void 0?N=J:(N+=String.fromCharCode(0),N+=J),J=ye+c)}return Qe(B),N},toWireType:(B,N)=>{if(typeof N!="string")throw new lt(`Cannot pass non-string to C++ string type ${f}`);var H=C(N),J=pr(4+H+c);return a()[J>>>2>>>0]=H/c,v(N,J+4,H+c),B!==null&&B.push(Qe,J),J},argPackAdvance:ct,readValueFromPointer:vn,yb(B){Qe(B)}})}function ac(s,c){it(s>>>=0,{Kb:!0,name:c=Xe(c>>>0),argPackAdvance:0,fromWireType:()=>{},toWireType:()=>{}})}var sc=()=>1;function uc(s){An(s>>>0,!g,1,!w,131072,!1),Ro()}var ai=s=>{if(!ge)try{if(s(),!(0<gt))try{y?mr(ce):hn(ce)}catch(c){c instanceof pn||c=="unwind"||A(1,c)}}catch(c){c instanceof pn||c=="unwind"||A(1,c)}};function $n(s){s>>>=0,typeof Atomics.Xb=="function"&&(Atomics.Xb(i(),s>>>2,s).value.then(or),s+=128,Atomics.store(i(),s>>>2,1))}var or=()=>{var s=zt();s&&($n(s),ai(Bi))};function dc(s,c){(s>>>=0)==c>>>0?setTimeout(or):y?postMessage({targetThread:s,cmd:"checkMailbox"}):(s=Ye[s])&&s.postMessage({cmd:"checkMailbox"})}var _n=[];function lc(s,c,f,b,v){for(c>>>=0,b/=2,_n.length=b,f=v>>>0>>>3,v=0;v<b;v++)_n[v]=G[f+2*v]?G[f+2*v+1]:d()[f+2*v+1>>>0];return(c?cn[c]:Zc[s])(..._n)}function cc(s){s>>>=0,y?postMessage({cmd:"cleanupThread",thread:s}):Do(Ye[s])}function pc(s){}var xn=(s,c)=>{var f=yn[s];if(f===void 0)throw s=Ei(s),f=Xe(s),Qe(s),new lt(`${c} has unknown type ${f}`);return f},si=(s,c,f)=>{var b=[];return s=s.toWireType(b,f),b.length&&(a()[c>>>2>>>0]=Fe(b)),s};function mc(s,c,f){return c>>>=0,f>>>=0,s=Ge(s>>>0),c=xn(c,"emval::as"),si(c,f,s)}var ir=s=>{try{s()}catch(c){Et(c)}},pt=0,Ze=null,ui=0,ar=[],di={},li={},fc=0,Sn=null,hc=[];function ci(s){return function(c){if(!ge){if(pt===0){var f=!1,b=!1;c((v=0)=>{if(!ge&&(ui=v,f=!0,b)){pt=2,ir(()=>Vi(Ze)),typeof Browser<"u"&&Browser.Cb.Ib&&Browser.Cb.resume(),v=!1;try{var C=function(){var N=i()[Ze+8>>>2>>>0];return N=F[li[N]],--gt,N()}()}catch(N){C=N,v=!0}var P=!1;if(!Ze){var B=Sn;B&&(Sn=null,(v?B.reject:B.resolve)(C),P=!0)}if(v&&!P)throw C}}),b=!0,f||(pt=1,Ze=function(){var v=pr(65548),C=v+12;a()[v>>>2>>>0]=C,a()[v+4>>>2>>>0]=C+65536,C=ar[0];var P=di[C];return P===void 0&&(P=fc++,di[C]=P,li[P]=C),C=P,i()[v+8>>>2>>>0]=C,v}(),typeof Browser<"u"&&Browser.Cb.Ib&&Browser.Cb.pause(),ir(()=>Mi(Ze)))}else pt===2?(pt=0,ir(Ni),Qe(Ze),Ze=null,hc.forEach(ai)):Et(`invalid state: ${pt}`);return ui}}(c=>{s().then(c)})}function gc(s){return s>>>=0,ci(()=>(s=Ge(s)).then(Fe))}var sr=[];function yc(s,c,f,b){return f>>>=0,b>>>=0,(s=sr[s>>>0])(null,c=Ge(c>>>0),f,b)}var bc={},ur=s=>{var c=bc[s];return c===void 0?Xe(s):c};function wc(s,c,f,b,v){return f>>>=0,b>>>=0,v>>>=0,(s=sr[s>>>0])(c=Ge(c>>>0),c[f=ur(f)],b,v)}var pi=()=>typeof globalThis=="object"?globalThis:Function("return this")();function vc(s){return(s>>>=0)==0?Fe(pi()):(s=ur(s),Fe(pi()[s]))}var $c=s=>{var c=sr.length;return sr.push(s),c},_c=(s,c)=>{for(var f=Array(s),b=0;b<s;++b)f[b]=xn(a()[c+4*b>>>2>>>0],"parameter "+b);return f},mi=(s,c)=>Object.defineProperty(c,"name",{value:s});function xc(s,c,f){var b=(c=_c(s,c>>>0)).shift();s--;var v=`return function (obj, func, destructorsRef, args) {
3804
+ `,C=0,P=[];f===0&&P.push("obj");for(var B=["retType"],N=[b],H=0;H<s;++H)P.push("arg"+H),B.push("argType"+H),N.push(c[H]),v+=` var arg${H} = argType${H}.readValueFromPointer(args${C?"+"+C:""});
3805
+ `,C+=c[H].argPackAdvance;return v+=` var rv = ${f===1?"new func":"func.call"}(${P.join(", ")});
3806
+ `,b.Kb||(B.push("emval_returnValue"),N.push(si),v+=` return emval_returnValue(retType, destructorsRef, rv);
3807
3807
  `),B.push(v+`};
3808
- `),s=function(j){var me=Function;if(!(me instanceof Function))throw new TypeError(`new_ called with constructor type ${typeof me} which is not a function`);var he=yi(me.name||"unknownFunctionName",function(){});return he.prototype=me.prototype,he=new he,(j=me.apply(he,j))instanceof Object?j:he}(B)(...N),f=`methodCaller<(${c.map(j=>j.name).join(", ")}) => ${y.name}>`,Ic(yi(f,s))}function Ac(s){return s=cr(s>>>0),je(u[s])}function Ec(s,c){return c>>>=0,s=qe(s>>>0),c=qe(c),je(s[c])}function kc(s){9<(s>>>=0)&&(ut[s+1]+=1)}function Pc(){return je([])}function Oc(s){s=qe(s>>>0);for(var c=Array(s.length),f=0;f<s.length;f++)c[f]=s[f];return je(c)}function Dc(s){return je(cr(s>>>0))}function zc(){return je({})}function Bc(s){for(var c=qe(s>>>=0);c.length;){var f=c.pop();c.pop()(f)}xn(s)}function Rc(s,c,f){c>>>=0,f>>>=0,s=qe(s>>>0),c=qe(c),f=qe(f),s[c]=f}function Mc(s,c){return c>>>=0,s=(s=Tn(s>>>0,"_emval_take_value")).readValueFromPointer(c),je(s)}function Uc(s,c){s=-9007199254740992>s||9007199254740992<s?NaN:Number(s),c>>>=0,s=new Date(1e3*s),i()[c>>>2>>>0]=s.getUTCSeconds(),i()[c+4>>>2>>>0]=s.getUTCMinutes(),i()[c+8>>>2>>>0]=s.getUTCHours(),i()[c+12>>>2>>>0]=s.getUTCDate(),i()[c+16>>>2>>>0]=s.getUTCMonth(),i()[c+20>>>2>>>0]=s.getUTCFullYear()-1900,i()[c+24>>>2>>>0]=s.getUTCDay(),s=(s.getTime()-Date.UTC(s.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,i()[c+28>>>2>>>0]=s}var Bt=s=>s%4==0&&(s%100!=0||s%400==0),bi=[0,31,60,91,121,152,182,213,244,274,305,335],wi=[0,31,59,90,120,151,181,212,243,273,304,334];function Vc(s,c){s=-9007199254740992>s||9007199254740992<s?NaN:Number(s),c>>>=0,s=new Date(1e3*s),i()[c>>>2>>>0]=s.getSeconds(),i()[c+4>>>2>>>0]=s.getMinutes(),i()[c+8>>>2>>>0]=s.getHours(),i()[c+12>>>2>>>0]=s.getDate(),i()[c+16>>>2>>>0]=s.getMonth(),i()[c+20>>>2>>>0]=s.getFullYear()-1900,i()[c+24>>>2>>>0]=s.getDay();var f=(Bt(s.getFullYear())?bi:wi)[s.getMonth()]+s.getDate()-1|0;i()[c+28>>>2>>>0]=f,i()[c+36>>>2>>>0]=-60*s.getTimezoneOffset(),f=new Date(s.getFullYear(),6,1).getTimezoneOffset();var y=new Date(s.getFullYear(),0,1).getTimezoneOffset();s=0|(f!=y&&s.getTimezoneOffset()==Math.min(y,f)),i()[c+32>>>2>>>0]=s}function Nc(s){s>>>=0;var c=new Date(i()[s+20>>>2>>>0]+1900,i()[s+16>>>2>>>0],i()[s+12>>>2>>>0],i()[s+8>>>2>>>0],i()[s+4>>>2>>>0],i()[s>>>2>>>0],0),f=i()[s+32>>>2>>>0],y=c.getTimezoneOffset(),v=new Date(c.getFullYear(),6,1).getTimezoneOffset(),C=new Date(c.getFullYear(),0,1).getTimezoneOffset(),O=Math.min(C,v);return 0>f?i()[s+32>>>2>>>0]=+(v!=C&&O==y):0<f!=(O==y)&&(v=Math.max(C,v),c.setTime(c.getTime()+6e4*((0<f?O:v)-y))),i()[s+24>>>2>>>0]=c.getDay(),f=(Bt(c.getFullYear())?bi:wi)[c.getMonth()]+c.getDate()-1|0,i()[s+28>>>2>>>0]=f,i()[s>>>2>>>0]=c.getSeconds(),i()[s+4>>>2>>>0]=c.getMinutes(),i()[s+8>>>2>>>0]=c.getHours(),i()[s+12>>>2>>>0]=c.getDate(),i()[s+16>>>2>>>0]=c.getMonth(),i()[s+20>>>2>>>0]=c.getYear(),s=c.getTime(),BigInt(isNaN(s)?-1:s/1e3)}function vi(s,c,f,y,v,C,O){return b?xe(16,1,s,c,f,y,v,C,O):-52}function $i(s,c,f,y,v,C){if(b)return xe(17,1,s,c,f,y,v,C)}function Wc(s,c,f,y){s>>>=0,c>>>=0,f>>>=0,y>>>=0;var v=new Date().getFullYear(),C=new Date(v,0,1),O=new Date(v,6,1);v=C.getTimezoneOffset();var B=O.getTimezoneOffset(),N=Math.max(v,B);a()[s>>>2>>>0]=60*N,i()[c>>>2>>>0]=+(v!=B),C=(s=L=>L.toLocaleTimeString(void 0,{hour12:!1,timeZoneName:"short"}).split(" ")[1])(C),O=s(O),B<v?(zt(C,f,17),zt(O,y,17)):(zt(C,y,17),zt(O,f,17))}var En=[],_i=(s,c)=>{En.length=0;for(var f;f=r()[s++>>>0];){var y=f!=105;c+=(y&=f!=112)&&c%8?4:0,En.push(f==112?a()[c>>>2>>>0]:f==106?F[c>>>3]:f==105?i()[c>>>2>>>0]:d()[c>>>3>>>0]),c+=y?8:4}return En};function Hc(s,c,f){return s>>>=0,c=_i(c>>>0,f>>>0),fn[s](...c)}function Gc(s,c,f){return s>>>=0,c=_i(c>>>0,f>>>0),fn[s](...c)}var Lc=()=>{},Fc=()=>Date.now();function qc(s,c){return Y(Ae(s>>>0,c>>>0))}var xi,jc=()=>{throw bt+=1,"unwind"};function Kc(){return 4294901760}xi=()=>performance.timeOrigin+performance.now();var Yc=()=>navigator.hardwareConcurrency;function Xc(){return Dt("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"),0}function Zc(s){s>>>=0;var c=r().length;if(s<=c||4294901760<s)return!1;for(var f=1;4>=f;f*=2){var y=c*(1+.2/f);y=Math.min(y,s+100663296);var v=Math;y=Math.max(s,y);e:{v=(v.min.call(v,4294901760,y+(65536-y%65536)%65536)-le.buffer.byteLength+65535)/65536;try{le.grow(v),we();var C=1;break e}catch{}C=void 0}if(C)return!0}return!1}var pr=()=>(Dt("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"),0),Rt={},Si=s=>{s.forEach(c=>{var f=pr();f&&(Rt[f]=c)})};function Qc(){var s=Error().stack.toString().split(`
3809
- `);return s[0]=="Error"&&s.shift(),Si(s),Rt.Ob=pr(),Rt.ac=s,Rt.Ob}function Jc(s,c,f){if(s>>>=0,c>>>=0,Rt.Ob==s)var y=Rt.ac;else(y=Error().stack.toString().split(`
3810
- `))[0]=="Error"&&y.shift(),Si(y);for(var v=3;y[v]&&pr()!=s;)++v;for(s=0;s<f&&y[s+v];++s)i()[c+4*s>>>2>>>0]=pr();return s}var kn,Pn={},Ii=()=>{if(!kn){var s,c={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:T||"./this.program"};for(s in Pn)Pn[s]===void 0?delete c[s]:c[s]=Pn[s];var f=[];for(s in c)f.push(`${s}=${c[s]}`);kn=f}return kn};function Ci(s,c){if(b)return xe(18,1,s,c);s>>>=0,c>>>=0;var f=0;return Ii().forEach((y,v)=>{var C=c+f;for(v=a()[s+4*v>>>2>>>0]=C,C=0;C<y.length;++C)t()[v++>>>0]=y.charCodeAt(C);t()[v>>>0]=0,f+=y.length+1}),0}function Ti(s,c){if(b)return xe(19,1,s,c);s>>>=0,c>>>=0;var f=Ii();a()[s>>>2>>>0]=f.length;var y=0;return f.forEach(v=>y+=v.length+1),a()[c>>>2>>>0]=y,0}function Ai(s){return b?xe(20,1,s):52}function Ei(s,c,f,y){return b?xe(21,1,s,c,f,y):52}function ki(s,c,f,y){return b?xe(22,1,s,c,f,y):70}var ep=[null,[],[]];function Pi(s,c,f,y){if(b)return xe(23,1,s,c,f,y);c>>>=0,f>>>=0,y>>>=0;for(var v=0,C=0;C<f;C++){var O=a()[c>>>2>>>0],B=a()[c+4>>>2>>>0];c+=8;for(var N=0;N<B;N++){var L=r()[O+N>>>0],j=ep[s];L===0||L===10?((s===1?X:Y)(qo(j,0)),j.length=0):j.push(L)}v+=B}return a()[y>>>2>>>0]=v,0}var Oi=[31,29,31,30,31,30,31,31,30,31,30,31],Di=[31,28,31,30,31,30,31,31,30,31,30,31],tp=(s,c)=>{t().set(s,c>>>0)};function zi(s,c,f,y){function v(P,ee,ye){for(P=typeof P=="number"?P.toString():P||"";P.length<ee;)P=ye[0]+P;return P}function C(P,ee){return v(P,ee,"0")}function O(P,ee){function ye(Ki){return 0>Ki?-1:0<Ki?1:0}var vt;return(vt=ye(P.getFullYear()-ee.getFullYear()))===0&&(vt=ye(P.getMonth()-ee.getMonth()))===0&&(vt=ye(P.getDate()-ee.getDate())),vt}function B(P){switch(P.getDay()){case 0:return new Date(P.getFullYear()-1,11,29);case 1:return P;case 2:return new Date(P.getFullYear(),0,3);case 3:return new Date(P.getFullYear(),0,2);case 4:return new Date(P.getFullYear(),0,1);case 5:return new Date(P.getFullYear()-1,11,31);case 6:return new Date(P.getFullYear()-1,11,30)}}function N(P){var ee=P.Bb;for(P=new Date(new Date(P.Cb+1900,0,1).getTime());0<ee;){var ye=P.getMonth(),vt=(Bt(P.getFullYear())?Oi:Di)[ye];if(!(ee>vt-P.getDate())){P.setDate(P.getDate()+ee);break}ee-=vt-P.getDate()+1,P.setDate(1),11>ye?P.setMonth(ye+1):(P.setMonth(0),P.setFullYear(P.getFullYear()+1))}return ye=new Date(P.getFullYear()+1,0,4),ee=B(new Date(P.getFullYear(),0,4)),ye=B(ye),0>=O(ee,P)?0>=O(ye,P)?P.getFullYear()+1:P.getFullYear():P.getFullYear()-1}s>>>=0,c>>>=0,f>>>=0,y>>>=0;var L=a()[y+40>>>2>>>0];for(var j in y={fc:i()[y>>>2>>>0],ec:i()[y+4>>>2>>>0],Gb:i()[y+8>>>2>>>0],Kb:i()[y+12>>>2>>>0],Hb:i()[y+16>>>2>>>0],Cb:i()[y+20>>>2>>>0],ub:i()[y+24>>>2>>>0],Bb:i()[y+28>>>2>>>0],nc:i()[y+32>>>2>>>0],dc:i()[y+36>>>2>>>0],hc:L?Ae(L):""},f=Ae(f),L={"%c":"%a %b %d %H:%M:%S %Y","%D":"%m/%d/%y","%F":"%Y-%m-%d","%h":"%b","%r":"%I:%M:%S %p","%R":"%H:%M","%T":"%H:%M:%S","%x":"%m/%d/%y","%X":"%H:%M:%S","%Ec":"%c","%EC":"%C","%Ex":"%m/%d/%y","%EX":"%H:%M:%S","%Ey":"%y","%EY":"%Y","%Od":"%d","%Oe":"%e","%OH":"%H","%OI":"%I","%Om":"%m","%OM":"%M","%OS":"%S","%Ou":"%u","%OU":"%U","%OV":"%V","%Ow":"%w","%OW":"%W","%Oy":"%y"})f=f.replace(new RegExp(j,"g"),L[j]);var me="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),he="January February March April May June July August September October November December".split(" ");for(j in L={"%a":P=>me[P.ub].substring(0,3),"%A":P=>me[P.ub],"%b":P=>he[P.Hb].substring(0,3),"%B":P=>he[P.Hb],"%C":P=>C((P.Cb+1900)/100|0,2),"%d":P=>C(P.Kb,2),"%e":P=>v(P.Kb,2," "),"%g":P=>N(P).toString().substring(2),"%G":N,"%H":P=>C(P.Gb,2),"%I":P=>((P=P.Gb)==0?P=12:12<P&&(P-=12),C(P,2)),"%j":P=>{for(var ee=0,ye=0;ye<=P.Hb-1;ee+=(Bt(P.Cb+1900)?Oi:Di)[ye++]);return C(P.Kb+ee,3)},"%m":P=>C(P.Hb+1,2),"%M":P=>C(P.ec,2),"%n":()=>`
3811
- `,"%p":P=>0<=P.Gb&&12>P.Gb?"AM":"PM","%S":P=>C(P.fc,2),"%t":()=>" ","%u":P=>P.ub||7,"%U":P=>C(Math.floor((P.Bb+7-P.ub)/7),2),"%V":P=>{var ee=Math.floor((P.Bb+7-(P.ub+6)%7)/7);if(2>=(P.ub+371-P.Bb-2)%7&&ee++,ee)ee==53&&((ye=(P.ub+371-P.Bb)%7)==4||ye==3&&Bt(P.Cb)||(ee=1));else{ee=52;var ye=(P.ub+7-P.Bb-1)%7;(ye==4||ye==5&&Bt(P.Cb%400-1))&&ee++}return C(ee,2)},"%w":P=>P.ub,"%W":P=>C(Math.floor((P.Bb+7-(P.ub+6)%7)/7),2),"%y":P=>(P.Cb+1900).toString().substring(2),"%Y":P=>P.Cb+1900,"%z":P=>{var ee=0<=(P=P.dc);return P=Math.abs(P)/60,(ee?"+":"-")+("0000"+(P/60*100+P%60)).slice(-4)},"%Z":P=>P.hc,"%%":()=>"%"},f=f.replace(/%%/g,"\0\0"),L)f.includes(j)&&(f=f.replace(new RegExp(j,"g"),L[j](y)));return j=function(P){var ee=Array(wn(P)+1);return Yo(P,ee,0,ee.length),ee}(f=f.replace(/\0\0/g,"%")),j.length>c?0:(tp(j,s),j.length-1)}function rp(s,c,f,y){return zi(s>>>0,c>>>0,f>>>0,y>>>0)}b||function(){for(var s=u.numThreads-1;s--;)Ho();Lt.unshift(()=>{Fe++,function(c){b?c():Promise.all(ct.map(Wo)).then(c)}(()=>ko())})}();for(var Bi=Array(256),mr=0;256>mr;++mr)Bi[mr]=String.fromCharCode(mr);si=Bi,pt=u.BindingError=class extends Error{constructor(s){super(s),this.name="BindingError"}},u.InternalError=class extends Error{constructor(s){super(s),this.name="InternalError"}},ut.push(0,1,void 0,1,null,1,!0,1,!1,1),u.count_emval_handles=()=>ut.length/2-5-_n.length;var np=[yn,Mo,Go,jo,Ko,Xo,Zo,Qo,Jo,ei,ti,ri,ni,oi,ii,ai,vi,$i,Ci,Ti,Ai,Ei,ki,Pi],q=function(){function s(f,y){return q=f.exports,q=function(){var v=q,C={};for(let[O,B]of Object.entries(v))C[O]=typeof B=="function"?(...N)=>{dr.push(O);try{return B(...N)}finally{Se||(dr.pop(),Je&&ft===1&&dr.length===0&&(ft=0,bt+=1,ur(Li),typeof Fibers<"u"&&Fibers.oc()))}}:B;return C}(),q=function(){var v=q,C=B=>N=>B(N)>>>0,O=B=>()=>B()>>>0;return(v=Object.assign({},v)).Ca=C(v.Ca),v.fb=O(v.fb),v.gb=C(v.gb),v.emscripten_main_runtime_thread_id=O(v.emscripten_main_runtime_thread_id),v.sb=C(v.sb),v.tb=O(v.tb),v}(),Uo.push(q.ib),Pe.unshift(q.Ba),K=y,ko(),q}var c=Bo();if(Fe++,u.instantiateWasm)try{return u.instantiateWasm(c,s)}catch(f){Y(`Module.instantiateWasm callback failed with error: ${f}`),m(f)}return mn||=u.locateFile?Po("ort-wasm-simd-threaded.jsep.wasm")?"ort-wasm-simd-threaded.jsep.wasm":u.locateFile?u.locateFile("ort-wasm-simd-threaded.jsep.wasm",D):D+"ort-wasm-simd-threaded.jsep.wasm":new URL(/* asset import */ __webpack_require__(/*! ort-wasm-simd-threaded.jsep.wasm */ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm"), __webpack_require__.b).href,function(f,y){var v=mn;return z||typeof WebAssembly.instantiateStreaming!="function"||Po(v)||Oo(v)||typeof fetch!="function"?zo(v,f,y):fetch(v,{credentials:"same-origin"}).then(C=>WebAssembly.instantiateStreaming(C,f).then(y,function(O){return Y(`wasm streaming compile failed: ${O}`),Y("falling back to ArrayBuffer instantiation"),zo(v,f,y)}))}(c,function(f){s(f.instance,f.module)}).catch(m),{}}(),Ri=s=>(Ri=q.Ca)(s),Mi=()=>(Mi=q.Da)();u._OrtInit=(s,c)=>(u._OrtInit=q.Ea)(s,c),u._OrtGetLastError=(s,c)=>(u._OrtGetLastError=q.Fa)(s,c),u._OrtCreateSessionOptions=(s,c,f,y,v,C,O,B,N,L)=>(u._OrtCreateSessionOptions=q.Ga)(s,c,f,y,v,C,O,B,N,L),u._OrtAppendExecutionProvider=(s,c)=>(u._OrtAppendExecutionProvider=q.Ha)(s,c),u._OrtAddFreeDimensionOverride=(s,c,f)=>(u._OrtAddFreeDimensionOverride=q.Ia)(s,c,f),u._OrtAddSessionConfigEntry=(s,c,f)=>(u._OrtAddSessionConfigEntry=q.Ja)(s,c,f),u._OrtReleaseSessionOptions=s=>(u._OrtReleaseSessionOptions=q.Ka)(s),u._OrtCreateSession=(s,c,f)=>(u._OrtCreateSession=q.La)(s,c,f),u._OrtReleaseSession=s=>(u._OrtReleaseSession=q.Ma)(s),u._OrtGetInputOutputCount=(s,c,f)=>(u._OrtGetInputOutputCount=q.Na)(s,c,f),u._OrtGetInputName=(s,c)=>(u._OrtGetInputName=q.Oa)(s,c),u._OrtGetOutputName=(s,c)=>(u._OrtGetOutputName=q.Pa)(s,c),u._OrtFree=s=>(u._OrtFree=q.Qa)(s),u._OrtCreateTensor=(s,c,f,y,v,C)=>(u._OrtCreateTensor=q.Ra)(s,c,f,y,v,C),u._OrtGetTensorData=(s,c,f,y,v)=>(u._OrtGetTensorData=q.Sa)(s,c,f,y,v),u._OrtReleaseTensor=s=>(u._OrtReleaseTensor=q.Ta)(s),u._OrtCreateRunOptions=(s,c,f,y)=>(u._OrtCreateRunOptions=q.Ua)(s,c,f,y),u._OrtAddRunConfigEntry=(s,c,f)=>(u._OrtAddRunConfigEntry=q.Va)(s,c,f),u._OrtReleaseRunOptions=s=>(u._OrtReleaseRunOptions=q.Wa)(s),u._OrtCreateBinding=s=>(u._OrtCreateBinding=q.Xa)(s),u._OrtBindInput=(s,c,f)=>(u._OrtBindInput=q.Ya)(s,c,f),u._OrtBindOutput=(s,c,f,y)=>(u._OrtBindOutput=q.Za)(s,c,f,y),u._OrtClearBoundOutputs=s=>(u._OrtClearBoundOutputs=q._a)(s),u._OrtReleaseBinding=s=>(u._OrtReleaseBinding=q.$a)(s),u._OrtRunWithBinding=(s,c,f,y,v)=>(u._OrtRunWithBinding=q.ab)(s,c,f,y,v),u._OrtRun=(s,c,f,y,v,C,O,B)=>(u._OrtRun=q.bb)(s,c,f,y,v,C,O,B),u._OrtEndProfiling=s=>(u._OrtEndProfiling=q.cb)(s),u._JsepOutput=(s,c,f)=>(u._JsepOutput=q.db)(s,c,f),u._JsepGetNodeName=s=>(u._JsepGetNodeName=q.eb)(s);var fr,Mt=()=>(Mt=q.fb)(),hr=u._malloc=s=>(hr=u._malloc=q.gb)(s),et=u._free=s=>(et=u._free=q.hb)(s),On=(s,c,f,y,v,C)=>(On=q.kb)(s,c,f,y,v,C),Ui=()=>(Ui=q.lb)(),Vi=(s,c,f,y,v)=>(Vi=q.mb)(s,c,f,y,v),Dn=s=>(Dn=q.nb)(s),gr=s=>(gr=q.ob)(s),Ni=()=>(Ni=q.pb)(),Wi=(s,c)=>(Wi=q.qb)(s,c),yr=s=>(yr=q.rb)(s),zn=s=>(zn=q.sb)(s),Bn=()=>(Bn=q.tb)(),Hi=u.dynCall_ii=(s,c)=>(Hi=u.dynCall_ii=q.vb)(s,c),Gi=s=>(Gi=q.wb)(s),Li=()=>(Li=q.xb)(),Fi=s=>(Fi=q.yb)(s),qi=()=>(qi=q.zb)();function ji(){0<Fe||(b?(p(u),b||ar(Pe),startWorker(u)):(ar(Lt),0<Fe||fr||(fr=!0,u.calledRun=!0,Se||(b||ar(Pe),p(u),b||ar(ze)))))}return u.___start_em_js=862461,u.___stop_em_js=862683,u.stackSave=()=>Bn(),u.stackRestore=s=>yr(s),u.stackAlloc=s=>zn(s),u.UTF8ToString=Ae,u.stringToUTF8=zt,u.lengthBytesUTF8=wn,yt=function s(){fr||ji(),fr||(yt=s)},ji(),h}),mp=Aa;globalThis.self?.name==="em-pthread"&&Aa()});var Ut,fp,hp,gp,Pa,Oa,yp,Da,jt=U(()=>{"use strict";Tr();Ut= false?0:import.meta.url??(typeof document<"u"?document.currentScript?.src:typeof self<"u"?self.location?.href:void 0),fp= false||typeof location>"u"?void 0:location.origin,hp=(e,t)=>{try{let r=t??Ut;return(r?new URL(e,r):new URL(e)).origin===fp}catch{return!1}},gp=async e=>{let r=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(r)},Pa=(Ta(),br(Ca)).default,Oa=async()=>{if(!Ut)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(hp(Ut))return[void 0,Pa()];let e=await gp(Ut);return[e,Pa(e)]},yp=(ka(),br(Ea)).default,Da=async(e,t,r)=>[void 0,yp]});var Ln,Fn,Rr,za,bp,wp,Ar,Ie,St=U(()=>{"use strict";jt();Fn=!1,Rr=!1,za=!1,bp=()=>{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}},wp=()=>{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}},Ar=async e=>{if(Fn)return Promise.resolve();if(Rr)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(za)throw new Error("previous call to 'initializeWebAssembly()' failed.");Rr=!0;let t=e.initTimeout,r=e.numThreads;if(!wp())throw new Error("WebAssembly SIMD is not supported in the current environment.");let n=bp();r>1&&!n&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", 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=r=1);let o=e.wasmPaths,i=typeof o=="string"?o:void 0,a=o?.mjs,l=a?.href??a,d=o?.wasm,p=d?.href??d,m=e.wasmBinary,[u,h]=await Da(l,i,r>1),w=!1,g=[];if(t>0&&g.push(new Promise(b=>{setTimeout(()=>{w=!0,b()},t)})),g.push(new Promise((b,x)=>{let _={numThreads:r};m?_.wasmBinary=m:(p||i)&&(_.locateFile=($,S)=>p??(i??S)+$),h(_).then($=>{Rr=!1,Fn=!0,Ln=$,b(),u&&URL.revokeObjectURL(u)},$=>{Rr=!1,za=!0,x($)})})),await Promise.race(g),w)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},Ie=()=>{if(Fn&&Ln)return Ln;throw new Error("WebAssembly is not initialized yet.")}});var Te,Yt,_e,Mr=U(()=>{"use strict";St();Te=(e,t)=>{let r=Ie(),n=r.lengthBytesUTF8(e)+1,o=r._malloc(n);return r.stringToUTF8(e,o,n),t.push(o),o},Yt=(e,t,r,n)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([o,i])=>{let a=t?t+o:o;if(typeof i=="object")Yt(i,a+".",r,n);else if(typeof i=="string"||typeof i=="number")n(a,i.toString());else if(typeof i=="boolean")n(a,i?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof i}`)})},_e=e=>{let t=Ie(),r=t.stackSave();try{let n=t.stackAlloc(8);t._OrtGetLastError(n,n+4);let o=t.HEAP32[n/4],i=t.HEAPU32[n/4+1],a=i?t.UTF8ToString(i):"";throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${a}`)}finally{t.stackRestore(r)}}});var Ba,Ra=U(()=>{"use strict";St();Mr();Ba=e=>{let t=Ie(),r=0,n=[],o=e||{};try{if(e?.logSeverityLevel===void 0)o.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)o.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(o.terminate=!1);let i=0;return e?.tag!==void 0&&(i=Te(e.tag,n)),r=t._OrtCreateRunOptions(o.logSeverityLevel,o.logVerbosityLevel,!!o.terminate,i),r===0&&_e("Can't create run options."),e?.extra!==void 0&&Yt(e.extra,"",new WeakSet,(a,l)=>{let d=Te(a,n),p=Te(l,n);t._OrtAddRunConfigEntry(r,d,p)!==0&&_e(`Can't set a run config entry: ${a} - ${l}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseRunOptions(r),n.forEach(a=>t._free(a)),i}}});var vp,$p,_p,xp,Ma,Ua=U(()=>{"use strict";St();Mr();vp=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},$p=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},_p=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},xp=(e,t,r)=>{for(let n of t){let o=typeof n=="string"?n:n.name;switch(o){case"webnn":if(o="WEBNN",typeof n!="string"){let l=n?.deviceType;if(l){let d=Te("deviceType",r),p=Te(l,r);Ie()._OrtAddSessionConfigEntry(e,d,p)!==0&&_e(`Can't set a session config entry: 'deviceType' - ${l}.`)}}break;case"webgpu":if(o="JS",typeof n!="string"){let a=n;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);let l=Te("preferredLayout",r),d=Te(a.preferredLayout,r);Ie()._OrtAddSessionConfigEntry(e,l,d)!==0&&_e(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${o}`)}let i=Te(o,r);Ie()._OrtAppendExecutionProvider(e,i)!==0&&_e(`Can't append execution provider: ${o}.`)}},Ma=e=>{let t=Ie(),r=0,n=[],o=e||{};_p(o);try{let i=vp(o.graphOptimizationLevel??"all"),a=$p(o.executionMode??"sequential"),l=typeof o.logId=="string"?Te(o.logId,n):0,d=o.logSeverityLevel??2;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log serverity level is not valid: ${d}`);let p=o.logVerbosityLevel??0;if(!Number.isInteger(p)||p<0||p>4)throw new Error(`log verbosity level is not valid: ${p}`);let m=typeof o.optimizedModelFilePath=="string"?Te(o.optimizedModelFilePath,n):0;if(r=t._OrtCreateSessionOptions(i,!!o.enableCpuMemArena,!!o.enableMemPattern,a,!!o.enableProfiling,0,l,d,p,m),r===0&&_e("Can't create session options."),o.executionProviders&&xp(r,o.executionProviders,n),o.enableGraphCapture!==void 0){if(typeof o.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`);let u=Te("enableGraphCapture",n),h=Te(o.enableGraphCapture.toString(),n);t._OrtAddSessionConfigEntry(r,u,h)!==0&&_e(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`)}if(o.freeDimensionOverrides)for(let[u,h]of Object.entries(o.freeDimensionOverrides)){if(typeof u!="string")throw new Error(`free dimension override name must be a string: ${u}`);if(typeof h!="number"||!Number.isInteger(h)||h<0)throw new Error(`free dimension override value must be a non-negative integer: ${h}`);let w=Te(u,n);t._OrtAddFreeDimensionOverride(r,w,h)!==0&&_e(`Can't set a free dimension override: ${u} - ${h}.`)}return o.extra!==void 0&&Yt(o.extra,"",new WeakSet,(u,h)=>{let w=Te(u,n),g=Te(h,n);t._OrtAddSessionConfigEntry(r,w,g)!==0&&_e(`Can't set a session config entry: ${u} - ${h}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseSessionOptions(r),n.forEach(a=>t._free(a)),i}}});var qn,ht,It,Ur,Xt,Vr,jn,Q=U(()=>{"use strict";qn=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;default:throw new Error(`unsupported data type: ${e}`)}},ht=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";default:throw new Error(`unsupported data type: ${e}`)}},It=e=>[void 0,4,1,1,2,2,4,8,void 0,1,2,8,4,8,void 0,void 0,void 0][e],Ur=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 new Error(`unsupported type: ${e}`)}},Xt=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Vr=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool",jn=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;default:throw new Error(`unsupported data location: ${e}`)}}});var Zt,Kn=U(()=>{"use strict";Tr();Zt=async e=>{if(typeof e=="string")if(false){}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),n=r?parseInt(r,10):0;if(n<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let o=t.body.getReader(),i;try{i=new ArrayBuffer(n)}catch(l){if(l instanceof RangeError){let d=Math.ceil(n/65536);i=new WebAssembly.Memory({initial:d,maximum:d}).buffer}else throw l}let a=0;for(;;){let{done:l,value:d}=await o.read();if(l)break;let p=d.byteLength;new Uint8Array(i,a,p).set(d),a+=p}return new Uint8Array(i,0,n)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var Sp,Ip,Va,Na,Wa,Cp,ve,dt=U(()=>{"use strict";Q();Sp=["V","I","W","E","F"],Ip=(e,t)=>{console.log(`[${Sp[e]},${new Date().toISOString()}]${t}`)},Wa=(e,t)=>{Va=e,Na=t},Cp=(e,t)=>{let r=Xt(e),n=Xt(Va);r>=n&&Ip(r,typeof t=="function"?t():t)},ve=(...e)=>{Na&&Cp(...e)}});var Ha,Ga=U(()=>{"use strict";Q();Ha=(e,t)=>new(Ur(t))(e)});var Nr=U(()=>{"use strict"});var La,Yn,Xn,Tp,Ap,Fa,Qn,Zn,ja,Ka=U(()=>{"use strict";dt();Nr();La=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]]),Yn=[],Xn=e=>Math.ceil(e/16)*16,Tp=e=>{for(let t=0;t<Yn.length;t++){let r=Yn[t];if(e<=r)return r}return Math.ceil(e/16)*16},Ap=1,Fa=()=>Ap++,Qn=async(e,t,r,n)=>{let o=Xn(r),i=e.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let a=e.getCommandEncoder();e.endComputePass(),a.copyBufferToBuffer(t,0,i,0,o),e.flush(),await i.mapAsync(GPUMapMode.READ);let l=i.getMappedRange();if(n){let d=n();return d.set(new Uint8Array(l,0,r)),d}else return new Uint8Array(l.slice(0,r))}finally{i.destroy()}},Zn=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersForUploadingPending=[],this.buffersPending=[],this.externalBuffers=new Map,this.capturedPendingBuffers=new Map;for(let[r]of La)Yn.push(r),this.freeBuffers.set(r,[]),this.freeUniformBuffers.set(r,[])}upload(t,r){let n=r.buffer,o=r.byteOffset,i=r.byteLength,a=Xn(i),l=this.storageCache.get(t);if(!l)throw new Error("gpu data for uploading does not exist");if(l.originalSize!==i)throw new Error(`inconsistent data size. gpu data size=${l.originalSize}, data size=${i}`);let d=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),p=d.getMappedRange();new Uint8Array(p).set(new Uint8Array(n,o,i)),d.unmap();let m=this.backend.getCommandEncoder();this.backend.endComputePass(),m.copyBufferToBuffer(d,0,l.gpuData.buffer,0,a),ve("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(d)}memcpy(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(r);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=Xn(n.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,i)}registerExternalBuffer(t,r,n){let o;if(n){if(o=this.externalBuffers.get(n),o===void 0)throw new Error("previous buffer is not registered");if(t===n)return ve("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet.
3812
- Please use the previous external buffer!`);this.externalBuffers.delete(n)}else o=Fa();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:t},originalSize:r}),this.externalBuffers.set(t,o),ve("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`),o}unregisterExternalBuffer(t){let r=this.externalBuffers.get(t);r!==void 0&&(this.storageCache.delete(r),this.externalBuffers.delete(t),ve("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${r}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=Tp(t),o,i=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||a){let p=(i?this.freeBuffers:this.freeUniformBuffers).get(n);p?p.length>0?o=p.pop():o=this.backend.device.createBuffer({size:n,usage:r}):o=this.backend.device.createBuffer({size:n,usage:r})}else o=this.backend.device.createBuffer({size:n,usage:r});let l={id:Fa(),type:0,buffer:o};return this.storageCache.set(l.id,{gpuData:l,originalSize:t}),ve("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${l.id}`),l}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=this.storageCache.get(t);if(!r)throw new Error("releasing data does not exist");return ve("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("data does not exist");await Qn(this.backend,n.gpuData.buffer,n.originalSize,r)}refreshPendingBuffers(){for(let t of this.buffersForUploadingPending)t.destroy();if(this.buffersForUploadingPending=[],this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending){let r=La.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else t.destroy()}this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(t))}},ja=(...e)=>new Zn(...e)});var Jn,te,Ce=U(()=>{"use strict";Jn=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},te=e=>new Jn(e)});var eo,tt,k,Ct,Wr,Hr,Gr,ae=U(()=>{"use strict";eo=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},tt=class{static calcShape(t,r,n=!1){let o=t.length,i=r.length;if(o===0)return r;if(i===0)return t;let a=Math.max(t.length,r.length),l=new Array(a);if(n){if(o<2||i<2)return;let d=eo.calcMatMulShape([t[o-2],t[o-1]],[r[i-2],r[i-1]]);if(d===void 0)return;[l[a-2],l[a-1]]=d}for(let d=n?3:1;d<=a;d++){let p=o-d<0?1:t[o-d],m=i-d<0?1:r[i-d];if(p!==m&&p>1&&m>1)return;let u=Math.max(p,m);if(p&&m)l[a-d]=Math.max(p,m);else{if(u>1)return;l[a-d]=0}}return l}static isValidBroadcast(t,r){let n=t.length,o=r.length;if(n>o)return!1;for(let i=1;i<=n;i++)if(t[n-i]!==1&&t[n-i]!==r[o-i])return!1;return!0}},k=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let n=t.length;if(n===0)return[];let o=new Array(n),i=n-1;for(;i>=0;){if(t[i]%r===0){o[i]=t[i]/r;break}if(r%t[i]!==0)throw new Error("cannot convert shape");o[i]=1,r/=t[i],i--}for(i--;i>=0;i--)o[i]=t[i];return o}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,n){let o=1;for(let i=r;i<n;i++){if(t[i]<0)throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them.");o*=t[i]}return o}static computeStrides(t){let r=t.length;if(r===0)return[];if(r===1)return[1];let n=new Array(r);n[r-1]=1,n[r-2]=t[r-1];for(let o=r-3;o>=0;--o)n[o]=n[o+1]*t[o+1];return n}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(n=>this.normalizeAxis(n,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(n=>t[n]):t.slice().reverse()}static padShape(t,r){let n=t.length;return t.map((o,i)=>o+r[i]+r[i+n])}static areEqual(t,r){return t.length!==r.length?!1:t.every((n,o)=>n===r[o])}},Ct=class e{static adjustPoolAttributes(t,r,n,o,i,a){if(!t&&n.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let l=0;l<r.length-2;l++)l>=n.length?n.push(r[l+2]):n[l]=r[l+2];for(let l=0;l<n.length;l++)if(l<o.length){if(o[l]<0)throw new Error("strides should be greater than or equal to 1")}else o.push(1);for(let l=0;l<n.length;l++)if(l<i.length){if(i[l]<0)throw new Error("dilations should be greater than or equal to 1")}else i.push(1);for(let l=0;l<n.length*2;l++)if(l<a.length){if(a[l]<0)throw new Error("pad should be greater than or equal to 1")}else a.push(0);for(let l=0;l<n.length;l++){if(n[l]<=0)throw new Error("kernel shapes need to be greater than 0");if(a[l]>=n[l]||a[l+n.length]>=n[l])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,n,o,i,a,l){if(l){if(i.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(o.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let d=0;d<t.length-2;d++)e.adjustPadAndReturnShape(t[d+(a?1:2)],r[d],n[d],o[d],i,d,d+t.length-2,l)}}static computePoolOutputShape(t,r,n,o,i,a,l){if(r.length<=0)throw new Error("input shape must be of size greater than 0");let d=[r[0],r[1]];return e.computeShapeHelper(t,r,d,n,o,i,a,l),d}static computeConvOutputShape(t,r,n,o,i,a,l){if(t.length<=0||r.length<=0)throw new Error("invalid input tensor dims or invalid filter tensor dims");let d=[t[0],r[0]];return e.computeShapeHelper(!1,t,d,n,o,i,a,l),d}static computeShapeHelper(t,r,n,o,i,a,l,d){if(t)for(let p=0;p<r.length-2;p++)n.push(1);else for(let p=0;p<r.length-2;p++)n.push(e.adjustPadAndReturnShape(r[p+2],o[p],i[p],a[p],l,p,p+r.length-2,d))}static adjustPadAndReturnShape(t,r,n,o,i,a,l,d){let p=n*(o-1)+1;if(d&&d!=="NOTSET")switch(d){case"VALID":return i[a]=0,i[l]=0,Math.floor((t-p)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(n!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let u=((t+r-1)/r-1)*r+o-t;return i[a]=Math.floor(d==="SAME_LOWER"?(u+1)/2:u/2),i[l]=u-i[a],Math.floor((t+u-o)/r+1)}default:throw new Error("Unsupported AutoPad type")}else return Math.floor((t+i[a]+i[l]-p)/r+1)}},Wr=class{static getShapeOfGemmResult(t,r,n,o,i){if(t.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let a,l,d;r?(a=t[1],l=t[0]):(a=t[0],l=t[1]);let p=-1;if(o?(d=n[0],p=1):(d=n[1],p=0),n[p]!==l)throw new Error("dimension mismatch");if(a<=0||d<=0||l<=0)throw new Error("invalid shape specified");if(i&&!tt.isValidBroadcast(i,[a,d]))throw new Error("gemm: invalid bias shape for broadcast");return[a,d,l]}},Hr=-34028234663852886e22,Gr=34028234663852886e22});var Tt,ro,ge,Oe,V,$e,gt,At,Xe,G,no,E,M,Lr,to,Ya,Nt,ie=U(()=>{"use strict";Q();ae();Tt=64,ro=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(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 new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4<bool>"];default:throw new Error(`Unknown data type: ${e}`)}},ge=(e,t=1)=>{let r=ro(e,t);return typeof r=="string"?r:r[0]},Oe=(e,t=1)=>{let r=ro(e,t);return typeof r=="string"?r:r[1]},V=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:k.computeStrides(r)})}),t},$e=e=>e%4===0?4:e%2===0?2:1,gt=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,At=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}<f32>(${r})`,Xe=(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,G=(e,t,r,n)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?n==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:n==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,no=(e,t,r,n,o)=>{let i=typeof r=="number",a=i?r:r.length,l=[...new Array(a).keys()],d=a<2?"u32":a<=4?`vec${a}<u32>`:`array<u32, ${a}>`,p=ro(t,o),m=typeof p=="string"?p:p[1],u=typeof p=="string"?p:p[0],h={indices:d,value:m,storage:u,tensor:t},w=R=>typeof R=="string"?R:`${R}u`,g={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},b=i?"uniforms.":"",x=`${b}${e}_shape`,_=`${b}${e}_strides`,$="";for(let R=0;R<a-1;R++)$+=`
3813
- let dim${R} = current / ${G(_,R,a)};
3814
- let rest${R} = current % ${G(_,R,a)};
3808
+ `),s=function(J){var de=Function;if(!(de instanceof Function))throw new TypeError(`new_ called with constructor type ${typeof de} which is not a function`);var ye=mi(de.name||"unknownFunctionName",function(){});return ye.prototype=de.prototype,ye=new ye,(J=de.apply(ye,J))instanceof Object?J:ye}(B)(...N),f=`methodCaller<(${c.map(J=>J.name).join(", ")}) => ${b.name}>`,$c(mi(f,s))}function Sc(s){return s=ur(s>>>0),Fe(u[s])}function Ic(s,c){return c>>>=0,s=Ge(s>>>0),c=Ge(c),Fe(s[c])}function Cc(s){9<(s>>>=0)&&(at[s+1]+=1)}function Tc(){return Fe([])}function Ac(s){s=Ge(s>>>0);for(var c=Array(s.length),f=0;f<s.length;f++)c[f]=s[f];return Fe(c)}function kc(s){return Fe(ur(s>>>0))}function Ec(){return Fe({})}function Pc(s){for(var c=Ge(s>>>=0);c.length;){var f=c.pop();c.pop()(f)}wn(s)}function Oc(s,c,f){c>>>=0,f>>>=0,s=Ge(s>>>0),c=Ge(c),f=Ge(f),s[c]=f}function zc(s,c){return c>>>=0,s=(s=xn(s>>>0,"_emval_take_value")).readValueFromPointer(c),Fe(s)}function Bc(s,c){s=-9007199254740992>s||9007199254740992<s?NaN:Number(s),c>>>=0,s=new Date(1e3*s),i()[c>>>2>>>0]=s.getUTCSeconds(),i()[c+4>>>2>>>0]=s.getUTCMinutes(),i()[c+8>>>2>>>0]=s.getUTCHours(),i()[c+12>>>2>>>0]=s.getUTCDate(),i()[c+16>>>2>>>0]=s.getUTCMonth(),i()[c+20>>>2>>>0]=s.getUTCFullYear()-1900,i()[c+24>>>2>>>0]=s.getUTCDay(),s=(s.getTime()-Date.UTC(s.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,i()[c+28>>>2>>>0]=s}var fi=s=>s%4==0&&(s%100!=0||s%400==0),hi=[0,31,60,91,121,152,182,213,244,274,305,335],gi=[0,31,59,90,120,151,181,212,243,273,304,334];function Dc(s,c){s=-9007199254740992>s||9007199254740992<s?NaN:Number(s),c>>>=0,s=new Date(1e3*s),i()[c>>>2>>>0]=s.getSeconds(),i()[c+4>>>2>>>0]=s.getMinutes(),i()[c+8>>>2>>>0]=s.getHours(),i()[c+12>>>2>>>0]=s.getDate(),i()[c+16>>>2>>>0]=s.getMonth(),i()[c+20>>>2>>>0]=s.getFullYear()-1900,i()[c+24>>>2>>>0]=s.getDay();var f=(fi(s.getFullYear())?hi:gi)[s.getMonth()]+s.getDate()-1|0;i()[c+28>>>2>>>0]=f,i()[c+36>>>2>>>0]=-60*s.getTimezoneOffset(),f=new Date(s.getFullYear(),6,1).getTimezoneOffset();var b=new Date(s.getFullYear(),0,1).getTimezoneOffset();s=0|(f!=b&&s.getTimezoneOffset()==Math.min(b,f)),i()[c+32>>>2>>>0]=s}function Rc(s){s>>>=0;var c=new Date(i()[s+20>>>2>>>0]+1900,i()[s+16>>>2>>>0],i()[s+12>>>2>>>0],i()[s+8>>>2>>>0],i()[s+4>>>2>>>0],i()[s>>>2>>>0],0),f=i()[s+32>>>2>>>0],b=c.getTimezoneOffset(),v=new Date(c.getFullYear(),6,1).getTimezoneOffset(),C=new Date(c.getFullYear(),0,1).getTimezoneOffset(),P=Math.min(C,v);return 0>f?i()[s+32>>>2>>>0]=+(v!=C&&P==b):0<f!=(P==b)&&(v=Math.max(C,v),c.setTime(c.getTime()+6e4*((0<f?P:v)-b))),i()[s+24>>>2>>>0]=c.getDay(),f=(fi(c.getFullYear())?hi:gi)[c.getMonth()]+c.getDate()-1|0,i()[s+28>>>2>>>0]=f,i()[s>>>2>>>0]=c.getSeconds(),i()[s+4>>>2>>>0]=c.getMinutes(),i()[s+8>>>2>>>0]=c.getHours(),i()[s+12>>>2>>>0]=c.getDate(),i()[s+16>>>2>>>0]=c.getMonth(),i()[s+20>>>2>>>0]=c.getYear(),s=c.getTime(),BigInt(isNaN(s)?-1:s/1e3)}function yi(s,c,f,b,v,C,P){return y?ve(16,1,s,c,f,b,v,C,P):-52}function bi(s,c,f,b,v,C){if(y)return ve(17,1,s,c,f,b,v,C)}function Mc(s,c,f,b){s>>>=0,c>>>=0,f>>>=0,b>>>=0;var v=new Date().getFullYear(),C=new Date(v,0,1),P=new Date(v,6,1);v=C.getTimezoneOffset();var B=P.getTimezoneOffset(),N=Math.max(v,B);a()[s>>>2>>>0]=60*N,i()[c>>>2>>>0]=+(v!=B),C=(s=H=>H.toLocaleTimeString(void 0,{hour12:!1,timeZoneName:"short"}).split(" ")[1])(C),P=s(P),B<v?(Pt(C,f,17),Pt(P,b,17)):(Pt(C,b,17),Pt(P,f,17))}var In=[],wi=(s,c)=>{In.length=0;for(var f;f=r()[s++>>>0];){var b=f!=105;c+=(b&=f!=112)&&c%8?4:0,In.push(f==112?a()[c>>>2>>>0]:f==106?G[c>>>3]:f==105?i()[c>>>2>>>0]:d()[c>>>3>>>0]),c+=b?8:4}return In};function Uc(s,c,f){return s>>>=0,c=wi(c>>>0,f>>>0),cn[s](...c)}function Vc(s,c,f){return s>>>=0,c=wi(c>>>0,f>>>0),cn[s](...c)}var Nc=()=>{},Wc=()=>Date.now();function Lc(s,c){return Y(Ee(s>>>0,c>>>0))}var vi,Hc=()=>{throw gt+=1,"unwind"};function Gc(){return 4294901760}vi=()=>performance.timeOrigin+performance.now();var Fc=()=>navigator.hardwareConcurrency;function qc(){return Et("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"),0}function Kc(s){s>>>=0;var c=r().length;if(s<=c||4294901760<s)return!1;for(var f=1;4>=f;f*=2){var b=c*(1+.2/f);b=Math.min(b,s+100663296);var v=Math;b=Math.max(s,b);e:{v=(v.min.call(v,4294901760,b+(65536-b%65536)%65536)-ie.buffer.byteLength+65535)/65536;try{ie.grow(v),Se();var C=1;break e}catch{}C=void 0}if(C)return!0}return!1}var dr=()=>(Et("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"),0),Ot={},$i=s=>{s.forEach(c=>{var f=dr();f&&(Ot[f]=c)})};function jc(){var s=Error().stack.toString().split(`
3809
+ `);return s[0]=="Error"&&s.shift(),$i(s),Ot.Gb=dr(),Ot.Tb=s,Ot.Gb}function Yc(s,c,f){if(s>>>=0,c>>>=0,Ot.Gb==s)var b=Ot.Tb;else(b=Error().stack.toString().split(`
3810
+ `))[0]=="Error"&&b.shift(),$i(b);for(var v=3;b[v]&&dr()!=s;)++v;for(s=0;s<f&&b[s+v];++s)i()[c+4*s>>>2>>>0]=dr();return s}var Cn,Tn={},_i=()=>{if(!Cn){var s,c={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:T||"./this.program"};for(s in Tn)Tn[s]===void 0?delete c[s]:c[s]=Tn[s];var f=[];for(s in c)f.push(`${s}=${c[s]}`);Cn=f}return Cn};function xi(s,c){if(y)return ve(18,1,s,c);s>>>=0,c>>>=0;var f=0;return _i().forEach((b,v)=>{var C=c+f;for(v=a()[s+4*v>>>2>>>0]=C,C=0;C<b.length;++C)t()[v++>>>0]=b.charCodeAt(C);t()[v>>>0]=0,f+=b.length+1}),0}function Si(s,c){if(y)return ve(19,1,s,c);s>>>=0,c>>>=0;var f=_i();a()[s>>>2>>>0]=f.length;var b=0;return f.forEach(v=>b+=v.length+1),a()[c>>>2>>>0]=b,0}function Ii(s){return y?ve(20,1,s):52}function Ci(s,c,f,b){return y?ve(21,1,s,c,f,b):52}function Ti(s,c,f,b){return y?ve(22,1,s,c,f,b):70}var Xc=[null,[],[]];function Ai(s,c,f,b){if(y)return ve(23,1,s,c,f,b);c>>>=0,f>>>=0,b>>>=0;for(var v=0,C=0;C<f;C++){var P=a()[c>>>2>>>0],B=a()[c+4>>>2>>>0];c+=8;for(var N=0;N<B;N++){var H=r()[P+N>>>0],J=Xc[s];H===0||H===10?((s===1?j:Y)(Lo(J,0)),J.length=0):J.push(H)}v+=B}return a()[b>>>2>>>0]=v,0}y||function(){for(var s=u.numThreads-1;s--;)Uo();Wt.unshift(()=>{He++,function(c){y?c():Promise.all(dt.map(Mo)).then(c)}(()=>Co())})}();for(var ki=Array(256),lr=0;256>lr;++lr)ki[lr]=String.fromCharCode(lr);ni=ki,lt=u.BindingError=class extends Error{constructor(s){super(s),this.name="BindingError"}},u.InternalError=class extends Error{constructor(s){super(s),this.name="InternalError"}},at.push(0,1,void 0,1,null,1,!0,1,!1,1),u.count_emval_handles=()=>at.length/2-5-bn.length;var Zc=[fn,zo,Vo,Ho,Go,qo,Ko,jo,Yo,Xo,Zo,Qo,Jo,ei,ti,ri,yi,bi,xi,Si,Ii,Ci,Ti,Ai],F=function(){function s(f,b){return F=f.exports,F=function(){var v=F,C={};for(let[P,B]of Object.entries(v))C[P]=typeof B=="function"?(...N)=>{ar.push(P);try{return B(...N)}finally{ge||(ar.pop(),Ze&&pt===1&&ar.length===0&&(pt=0,gt+=1,ir(Ui),typeof Fibers<"u"&&Fibers.ac()))}}:B;return C}(),F=function(){var v=F,C=B=>N=>B(N)>>>0,P=B=>()=>B()>>>0;return(v=Object.assign({},v)).Aa=C(v.Aa),v.db=P(v.db),v.eb=C(v.eb),v.emscripten_main_runtime_thread_id=P(v.emscripten_main_runtime_thread_id),v.qb=C(v.qb),v.rb=P(v.rb),v}(),Bo.push(F.gb),Te.unshift(F.za),q=b,Co(),F}var c=Po();if(He++,u.instantiateWasm)try{return u.instantiateWasm(c,s)}catch(f){Y(`Module.instantiateWasm callback failed with error: ${f}`),m(f)}return ln||=u.locateFile?To("ort-wasm-simd-threaded.jsep.wasm")?"ort-wasm-simd-threaded.jsep.wasm":u.locateFile?u.locateFile("ort-wasm-simd-threaded.jsep.wasm",O):O+"ort-wasm-simd-threaded.jsep.wasm":new URL(/* asset import */ __webpack_require__(/*! ort-wasm-simd-threaded.jsep.wasm */ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm"), __webpack_require__.b).href,function(f,b){var v=ln;return z||typeof WebAssembly.instantiateStreaming!="function"||To(v)||Ao(v)||typeof fetch!="function"?Eo(v,f,b):fetch(v,{credentials:"same-origin"}).then(C=>WebAssembly.instantiateStreaming(C,f).then(b,function(P){return Y(`wasm streaming compile failed: ${P}`),Y("falling back to ArrayBuffer instantiation"),Eo(v,f,b)}))}(c,function(f){s(f.instance,f.module)}).catch(m),{}}(),Ei=s=>(Ei=F.Aa)(s),Pi=()=>(Pi=F.Ba)();u._OrtInit=(s,c)=>(u._OrtInit=F.Ca)(s,c),u._OrtGetLastError=(s,c)=>(u._OrtGetLastError=F.Da)(s,c),u._OrtCreateSessionOptions=(s,c,f,b,v,C,P,B,N,H)=>(u._OrtCreateSessionOptions=F.Ea)(s,c,f,b,v,C,P,B,N,H),u._OrtAppendExecutionProvider=(s,c)=>(u._OrtAppendExecutionProvider=F.Fa)(s,c),u._OrtAddFreeDimensionOverride=(s,c,f)=>(u._OrtAddFreeDimensionOverride=F.Ga)(s,c,f),u._OrtAddSessionConfigEntry=(s,c,f)=>(u._OrtAddSessionConfigEntry=F.Ha)(s,c,f),u._OrtReleaseSessionOptions=s=>(u._OrtReleaseSessionOptions=F.Ia)(s),u._OrtCreateSession=(s,c,f)=>(u._OrtCreateSession=F.Ja)(s,c,f),u._OrtReleaseSession=s=>(u._OrtReleaseSession=F.Ka)(s),u._OrtGetInputOutputCount=(s,c,f)=>(u._OrtGetInputOutputCount=F.La)(s,c,f),u._OrtGetInputName=(s,c)=>(u._OrtGetInputName=F.Ma)(s,c),u._OrtGetOutputName=(s,c)=>(u._OrtGetOutputName=F.Na)(s,c),u._OrtFree=s=>(u._OrtFree=F.Oa)(s),u._OrtCreateTensor=(s,c,f,b,v,C)=>(u._OrtCreateTensor=F.Pa)(s,c,f,b,v,C),u._OrtGetTensorData=(s,c,f,b,v)=>(u._OrtGetTensorData=F.Qa)(s,c,f,b,v),u._OrtReleaseTensor=s=>(u._OrtReleaseTensor=F.Ra)(s),u._OrtCreateRunOptions=(s,c,f,b)=>(u._OrtCreateRunOptions=F.Sa)(s,c,f,b),u._OrtAddRunConfigEntry=(s,c,f)=>(u._OrtAddRunConfigEntry=F.Ta)(s,c,f),u._OrtReleaseRunOptions=s=>(u._OrtReleaseRunOptions=F.Ua)(s),u._OrtCreateBinding=s=>(u._OrtCreateBinding=F.Va)(s),u._OrtBindInput=(s,c,f)=>(u._OrtBindInput=F.Wa)(s,c,f),u._OrtBindOutput=(s,c,f,b)=>(u._OrtBindOutput=F.Xa)(s,c,f,b),u._OrtClearBoundOutputs=s=>(u._OrtClearBoundOutputs=F.Ya)(s),u._OrtReleaseBinding=s=>(u._OrtReleaseBinding=F.Za)(s),u._OrtRunWithBinding=(s,c,f,b,v)=>(u._OrtRunWithBinding=F._a)(s,c,f,b,v),u._OrtRun=(s,c,f,b,v,C,P,B)=>(u._OrtRun=F.$a)(s,c,f,b,v,C,P,B),u._OrtEndProfiling=s=>(u._OrtEndProfiling=F.ab)(s),u._JsepOutput=(s,c,f)=>(u._JsepOutput=F.bb)(s,c,f),u._JsepGetNodeName=s=>(u._JsepGetNodeName=F.cb)(s);var cr,zt=()=>(zt=F.db)(),pr=u._malloc=s=>(pr=u._malloc=F.eb)(s),Qe=u._free=s=>(Qe=u._free=F.fb)(s),An=(s,c,f,b,v,C)=>(An=F.ib)(s,c,f,b,v,C),Oi=()=>(Oi=F.jb)(),zi=(s,c,f,b,v)=>(zi=F.kb)(s,c,f,b,v),kn=s=>(kn=F.lb)(s),mr=s=>(mr=F.mb)(s),Bi=()=>(Bi=F.nb)(),Di=(s,c)=>(Di=F.ob)(s,c),fr=s=>(fr=F.pb)(s),En=s=>(En=F.qb)(s),Pn=()=>(Pn=F.rb)(),Ri=u.dynCall_ii=(s,c)=>(Ri=u.dynCall_ii=F.tb)(s,c),Mi=s=>(Mi=F.ub)(s),Ui=()=>(Ui=F.vb)(),Vi=s=>(Vi=F.wb)(s),Ni=()=>(Ni=F.xb)();function Wi(){0<He||(y?(p(u),y||nr(Te),startWorker(u)):(nr(Wt),0<He||cr||(cr=!0,u.calledRun=!0,ge||(y||nr(Te),p(u),y||nr(Oe)))))}return u.stackSave=()=>Pn(),u.stackRestore=s=>fr(s),u.stackAlloc=s=>En(s),u.UTF8ToString=Ee,u.stringToUTF8=Pt,u.lengthBytesUTF8=Fo,ht=function s(){cr||Wi(),cr||(ht=s)},Wi(),h}),up=$a;globalThis.self?.name==="em-pthread"&&$a()});var Dt,dp,lp,cp,Sa,Ia,pp,Ca,Gt=M(()=>{"use strict";Sr();Dt= false?0:import.meta.url??(typeof document<"u"?document.currentScript?.src:typeof self<"u"?self.location?.href:void 0),dp= false||typeof location>"u"?void 0:location.origin,lp=(e,t)=>{try{let r=t??Dt;return(r?new URL(e,r):new URL(e)).origin===dp}catch{return!1}},cp=async e=>{let r=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(r)},Sa=(va(),hr(wa)).default,Ia=async()=>{if(!Dt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(lp(Dt))return[void 0,Sa()];let e=await cp(Dt);return[e,Sa(e)]},pp=(xa(),hr(_a)).default,Ca=async(e,t,r)=>[void 0,pp]});var Nn,Wn,zr,Ta,mp,fp,Ir,_e,$t=M(()=>{"use strict";Gt();Wn=!1,zr=!1,Ta=!1,mp=()=>{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}},fp=()=>{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}},Ir=async e=>{if(Wn)return Promise.resolve();if(zr)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Ta)throw new Error("previous call to 'initializeWebAssembly()' failed.");zr=!0;let t=e.initTimeout,r=e.numThreads;if(!fp())throw new Error("WebAssembly SIMD is not supported in the current environment.");let n=mp();r>1&&!n&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", 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=r=1);let o=e.wasmPaths,i=typeof o=="string"?o:void 0,a=o?.mjs,l=a?.href??a,d=o?.wasm,p=d?.href??d,m=e.wasmBinary,[u,h]=await Ca(l,i,r>1),w=!1,g=[];if(t>0&&g.push(new Promise(y=>{setTimeout(()=>{w=!0,y()},t)})),g.push(new Promise((y,x)=>{let _={numThreads:r};m?_.wasmBinary=m:(p||i)&&(_.locateFile=($,S)=>p??(i??S)+$),h(_).then($=>{zr=!1,Wn=!0,Nn=$,y(),u&&URL.revokeObjectURL(u)},$=>{zr=!1,Ta=!0,x($)})})),await Promise.race(g),w)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},_e=()=>{if(Wn&&Nn)return Nn;throw new Error("WebAssembly is not initialized yet.")}});var Ie,qt,we,Br=M(()=>{"use strict";$t();Ie=(e,t)=>{let r=_e(),n=r.lengthBytesUTF8(e)+1,o=r._malloc(n);return r.stringToUTF8(e,o,n),t.push(o),o},qt=(e,t,r,n)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([o,i])=>{let a=t?t+o:o;if(typeof i=="object")qt(i,a+".",r,n);else if(typeof i=="string"||typeof i=="number")n(a,i.toString());else if(typeof i=="boolean")n(a,i?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof i}`)})},we=e=>{let t=_e(),r=t.stackSave();try{let n=t.stackAlloc(8);t._OrtGetLastError(n,n+4);let o=t.HEAP32[n/4],i=t.HEAPU32[n/4+1],a=i?t.UTF8ToString(i):"";throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${a}`)}finally{t.stackRestore(r)}}});var Aa,ka=M(()=>{"use strict";$t();Br();Aa=e=>{let t=_e(),r=0,n=[],o=e||{};try{if(e?.logSeverityLevel===void 0)o.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)o.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(o.terminate=!1);let i=0;return e?.tag!==void 0&&(i=Ie(e.tag,n)),r=t._OrtCreateRunOptions(o.logSeverityLevel,o.logVerbosityLevel,!!o.terminate,i),r===0&&we("Can't create run options."),e?.extra!==void 0&&qt(e.extra,"",new WeakSet,(a,l)=>{let d=Ie(a,n),p=Ie(l,n);t._OrtAddRunConfigEntry(r,d,p)!==0&&we(`Can't set a run config entry: ${a} - ${l}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseRunOptions(r),n.forEach(a=>t._free(a)),i}}});var hp,gp,yp,bp,Ea,Pa=M(()=>{"use strict";$t();Br();hp=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},gp=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},yp=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},bp=(e,t,r)=>{for(let n of t){let o=typeof n=="string"?n:n.name;switch(o){case"webnn":if(o="WEBNN",typeof n!="string"){let l=n?.deviceType;if(l){let d=Ie("deviceType",r),p=Ie(l,r);_e()._OrtAddSessionConfigEntry(e,d,p)!==0&&we(`Can't set a session config entry: 'deviceType' - ${l}.`)}}break;case"webgpu":if(o="JS",typeof n!="string"){let a=n;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);let l=Ie("preferredLayout",r),d=Ie(a.preferredLayout,r);_e()._OrtAddSessionConfigEntry(e,l,d)!==0&&we(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${o}`)}let i=Ie(o,r);_e()._OrtAppendExecutionProvider(e,i)!==0&&we(`Can't append execution provider: ${o}.`)}},Ea=e=>{let t=_e(),r=0,n=[],o=e||{};yp(o);try{let i=hp(o.graphOptimizationLevel??"all"),a=gp(o.executionMode??"sequential"),l=typeof o.logId=="string"?Ie(o.logId,n):0,d=o.logSeverityLevel??2;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log serverity level is not valid: ${d}`);let p=o.logVerbosityLevel??0;if(!Number.isInteger(p)||p<0||p>4)throw new Error(`log verbosity level is not valid: ${p}`);let m=typeof o.optimizedModelFilePath=="string"?Ie(o.optimizedModelFilePath,n):0;if(r=t._OrtCreateSessionOptions(i,!!o.enableCpuMemArena,!!o.enableMemPattern,a,!!o.enableProfiling,0,l,d,p,m),r===0&&we("Can't create session options."),o.executionProviders&&bp(r,o.executionProviders,n),o.enableGraphCapture!==void 0){if(typeof o.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`);let u=Ie("enableGraphCapture",n),h=Ie(o.enableGraphCapture.toString(),n);t._OrtAddSessionConfigEntry(r,u,h)!==0&&we(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`)}if(o.freeDimensionOverrides)for(let[u,h]of Object.entries(o.freeDimensionOverrides)){if(typeof u!="string")throw new Error(`free dimension override name must be a string: ${u}`);if(typeof h!="number"||!Number.isInteger(h)||h<0)throw new Error(`free dimension override value must be a non-negative integer: ${h}`);let w=Ie(u,n);t._OrtAddFreeDimensionOverride(r,w,h)!==0&&we(`Can't set a free dimension override: ${u} - ${h}.`)}return o.extra!==void 0&&qt(o.extra,"",new WeakSet,(u,h)=>{let w=Ie(u,n),g=Ie(h,n);t._OrtAddSessionConfigEntry(r,w,g)!==0&&we(`Can't set a session config entry: ${u} - ${h}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseSessionOptions(r),n.forEach(a=>t._free(a)),i}}});var Ln,mt,_t,Dr,Kt,Rr,Hn,X=M(()=>{"use strict";Ln=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},mt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},_t=(e,t)=>{let r=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],n=typeof t=="number"?t:t.reduce((o,i)=>o*i,1);return r>0?Math.ceil(n*r):void 0},Dr=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 new Error(`unsupported type: ${e}`)}},Kt=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Rr=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool",Hn=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;default:throw new Error(`unsupported data location: ${e}`)}}});var jt,Gn=M(()=>{"use strict";Sr();jt=async e=>{if(typeof e=="string")if(false){}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),n=r?parseInt(r,10):0;if(n<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let o=t.body.getReader(),i;try{i=new ArrayBuffer(n)}catch(l){if(l instanceof RangeError){let d=Math.ceil(n/65536);i=new WebAssembly.Memory({initial:d,maximum:d}).buffer}else throw l}let a=0;for(;;){let{done:l,value:d}=await o.read();if(l)break;let p=d.byteLength;new Uint8Array(i,a,p).set(d),a+=p}return new Uint8Array(i,0,n)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var wp,vp,Oa,za,Ba,$p,be,st=M(()=>{"use strict";X();wp=["V","I","W","E","F"],vp=(e,t)=>{console.log(`[${wp[e]},${new Date().toISOString()}]${t}`)},Ba=(e,t)=>{Oa=e,za=t},$p=(e,t)=>{let r=Kt(e),n=Kt(Oa);r>=n&&vp(r,typeof t=="function"?t():t)},be=(...e)=>{za&&$p(...e)}});var Da,Ra=M(()=>{"use strict";X();Da=(e,t)=>new(Dr(t))(e)});var Mr=M(()=>{"use strict"});var Ma,Fn,qn,_p,xp,Ua,jn,Kn,Na,Wa=M(()=>{"use strict";st();Mr();Ma=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]]),Fn=[],qn=e=>Math.ceil(e/16)*16,_p=e=>{for(let t=0;t<Fn.length;t++){let r=Fn[t];if(e<=r)return r}return Math.ceil(e/16)*16},xp=1,Ua=()=>xp++,jn=async(e,t,r,n)=>{let o=qn(r),i=e.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let a=e.getCommandEncoder();e.endComputePass(),a.copyBufferToBuffer(t,0,i,0,o),e.flush(),await i.mapAsync(GPUMapMode.READ);let l=i.getMappedRange();if(n){let d=n();return d.set(new Uint8Array(l,0,r)),d}else return new Uint8Array(l.slice(0,r))}finally{i.destroy()}},Kn=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersForUploadingPending=[],this.buffersPending=[],this.externalBuffers=new Map,this.capturedPendingBuffers=new Map;for(let[r]of Ma)Fn.push(r),this.freeBuffers.set(r,[]),this.freeUniformBuffers.set(r,[])}upload(t,r){let n=r.buffer,o=r.byteOffset,i=r.byteLength,a=qn(i),l=this.storageCache.get(t);if(!l)throw new Error("gpu data for uploading does not exist");if(l.originalSize!==i)throw new Error(`inconsistent data size. gpu data size=${l.originalSize}, data size=${i}`);let d=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),p=d.getMappedRange();new Uint8Array(p).set(new Uint8Array(n,o,i)),d.unmap();let m=this.backend.getCommandEncoder();this.backend.endComputePass(),m.copyBufferToBuffer(d,0,l.gpuData.buffer,0,a),be("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(d)}memcpy(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(r);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=qn(n.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,i)}registerExternalBuffer(t,r,n){let o;if(n){if(o=this.externalBuffers.get(n),o===void 0)throw new Error("previous buffer is not registered");if(t===n)return be("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet.
3811
+ Please use the previous external buffer!`);this.externalBuffers.delete(n)}else o=Ua();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:t},originalSize:r}),this.externalBuffers.set(t,o),be("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`),o}unregisterExternalBuffer(t){let r=this.externalBuffers.get(t);r!==void 0&&(this.storageCache.delete(r),this.externalBuffers.delete(t),be("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${r}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=_p(t),o,i=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||a){let p=(i?this.freeBuffers:this.freeUniformBuffers).get(n);p?p.length>0?o=p.pop():o=this.backend.device.createBuffer({size:n,usage:r}):o=this.backend.device.createBuffer({size:n,usage:r})}else o=this.backend.device.createBuffer({size:n,usage:r});let l={id:Ua(),type:0,buffer:o};return this.storageCache.set(l.id,{gpuData:l,originalSize:t}),be("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${l.id}`),l}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=this.storageCache.get(t);if(!r)throw new Error("releasing data does not exist");return be("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("data does not exist");await jn(this.backend,n.gpuData.buffer,n.originalSize,r)}refreshPendingBuffers(){for(let t of this.buffersForUploadingPending)t.destroy();if(this.buffersForUploadingPending=[],this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending){let r=Ma.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else t.destroy()}this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(t))}},Na=(...e)=>new Kn(...e)});var Yn,Q,xe=M(()=>{"use strict";Yn=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},Q=e=>new Yn(e)});var Xn,Je,E,xt,Ur,Vr,Nr,oe=M(()=>{"use strict";Xn=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},Je=class{static calcShape(t,r,n=!1){let o=t.length,i=r.length;if(o===0)return r;if(i===0)return t;let a=Math.max(t.length,r.length),l=new Array(a);if(n){if(o<2||i<2)return;let d=Xn.calcMatMulShape([t[o-2],t[o-1]],[r[i-2],r[i-1]]);if(d===void 0)return;[l[a-2],l[a-1]]=d}for(let d=n?3:1;d<=a;d++){let p=o-d<0?1:t[o-d],m=i-d<0?1:r[i-d];if(p!==m&&p>1&&m>1)return;let u=Math.max(p,m);if(p&&m)l[a-d]=Math.max(p,m);else{if(u>1)return;l[a-d]=0}}return l}static isValidBroadcast(t,r){let n=t.length,o=r.length;if(n>o)return!1;for(let i=1;i<=n;i++)if(t[n-i]!==1&&t[n-i]!==r[o-i])return!1;return!0}},E=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let n=t.length;if(n===0)return[];let o=new Array(n),i=n-1;for(;i>=0;){if(t[i]%r===0){o[i]=t[i]/r;break}if(r%t[i]!==0)throw new Error("cannot convert shape");o[i]=1,r/=t[i],i--}for(i--;i>=0;i--)o[i]=t[i];return o}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,n){let o=1;for(let i=r;i<n;i++){if(t[i]<0)throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them.");o*=t[i]}return o}static computeStrides(t){let r=t.length;if(r===0)return[];if(r===1)return[1];let n=new Array(r);n[r-1]=1,n[r-2]=t[r-1];for(let o=r-3;o>=0;--o)n[o]=n[o+1]*t[o+1];return n}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(n=>this.normalizeAxis(n,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(n=>t[n]):t.slice().reverse()}static padShape(t,r){let n=t.length;return t.map((o,i)=>o+r[i]+r[i+n])}static areEqual(t,r){return t.length!==r.length?!1:t.every((n,o)=>n===r[o])}},xt=class e{static adjustPoolAttributes(t,r,n,o,i,a){if(!t&&n.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let l=0;l<r.length-2;l++)l>=n.length?n.push(r[l+2]):n[l]=r[l+2];for(let l=0;l<n.length;l++)if(l<o.length){if(o[l]<0)throw new Error("strides should be greater than or equal to 1")}else o.push(1);for(let l=0;l<n.length;l++)if(l<i.length){if(i[l]<0)throw new Error("dilations should be greater than or equal to 1")}else i.push(1);for(let l=0;l<n.length*2;l++)if(l<a.length){if(a[l]<0)throw new Error("pad should be greater than or equal to 1")}else a.push(0);for(let l=0;l<n.length;l++){if(n[l]<=0)throw new Error("kernel shapes need to be greater than 0");if(a[l]>=n[l]||a[l+n.length]>=n[l])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,n,o,i,a,l){if(l){if(i.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(o.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let d=0;d<t.length-2;d++)e.adjustPadAndReturnShape(t[d+(a?1:2)],r[d],n[d],o[d],i,d,d+t.length-2,l)}}static computePoolOutputShape(t,r,n,o,i,a,l){if(r.length<=0)throw new Error("input shape must be of size greater than 0");let d=[r[0],r[1]];return e.computeShapeHelper(t,r,d,n,o,i,a,l),d}static computeConvOutputShape(t,r,n,o,i,a,l){if(t.length<=0||r.length<=0)throw new Error("invalid input tensor dims or invalid filter tensor dims");let d=[t[0],r[0]];return e.computeShapeHelper(!1,t,d,n,o,i,a,l),d}static computeShapeHelper(t,r,n,o,i,a,l,d){if(t)for(let p=0;p<r.length-2;p++)n.push(1);else for(let p=0;p<r.length-2;p++)n.push(e.adjustPadAndReturnShape(r[p+2],o[p],i[p],a[p],l,p,p+r.length-2,d))}static adjustPadAndReturnShape(t,r,n,o,i,a,l,d){let p=n*(o-1)+1;if(d&&d!=="NOTSET")switch(d){case"VALID":return i[a]=0,i[l]=0,Math.floor((t-p)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(n!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let u=((t+r-1)/r-1)*r+o-t;return i[a]=Math.floor(d==="SAME_LOWER"?(u+1)/2:u/2),i[l]=u-i[a],Math.floor((t+u-o)/r+1)}default:throw new Error("Unsupported AutoPad type")}else return Math.floor((t+i[a]+i[l]-p)/r+1)}},Ur=class{static getShapeOfGemmResult(t,r,n,o,i){if(t.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let a,l,d;r?(a=t[1],l=t[0]):(a=t[0],l=t[1]);let p=-1;if(o?(d=n[0],p=1):(d=n[1],p=0),n[p]!==l)throw new Error("dimension mismatch");if(a<=0||d<=0||l<=0)throw new Error("invalid shape specified");if(i&&!Je.isValidBroadcast(i,[a,d]))throw new Error("gemm: invalid bias shape for broadcast");return[a,d,l]}},Vr=-34028234663852886e22,Nr=34028234663852886e22});var St,Qn,pe,ke,U,he,ft,It,je,L,Jn,k,D,Wr,Zn,La,Mt,re=M(()=>{"use strict";X();oe();St=64,Qn=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(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 new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4<bool>"];default:throw new Error(`Unknown data type: ${e}`)}},pe=(e,t=1)=>{let r=Qn(e,t);return typeof r=="string"?r:r[0]},ke=(e,t=1)=>{let r=Qn(e,t);return typeof r=="string"?r:r[1]},U=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:E.computeStrides(r)})}),t},he=e=>e%4===0?4:e%2===0?2:1,ft=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,It=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}<f32>(${r})`,je=(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,L=(e,t,r,n)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?n==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:n==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,Jn=(e,t,r,n,o)=>{let i=typeof r=="number",a=i?r:r.length,l=[...new Array(a).keys()],d=a<2?"u32":a<=4?`vec${a}<u32>`:`array<u32, ${a}>`,p=Qn(t,o),m=typeof p=="string"?p:p[1],u=typeof p=="string"?p:p[0],h={indices:d,value:m,storage:u,tensor:t},w=R=>typeof R=="string"?R:`${R}u`,g={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},y=i?"uniforms.":"",x=`${y}${e}_shape`,_=`${y}${e}_strides`,$="";for(let R=0;R<a-1;R++)$+=`
3812
+ let dim${R} = current / ${L(_,R,a)};
3813
+ let rest${R} = current % ${L(_,R,a)};
3815
3814
  indices[${R}] = dim${R};
3816
3815
  current = rest${R};
3817
3816
  `;$+=`indices[${a-1}] = current;`;let S=a<2?"":`
@@ -3820,25 +3819,25 @@ var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Objec
3820
3819
  var current = offset;
3821
3820
  ${$}
3822
3821
  return indices;
3823
- }`,I=R=>(g.offsetToIndices=!0,a<2?R:`o2i_${e}(${R})`),T=[];if(a>=2)for(let R=a-1;R>=0;R--)T.push(`${G(_,R,a)} * (indices[${R}])`);let A=a<2?"":`
3822
+ }`,I=R=>(g.offsetToIndices=!0,a<2?R:`o2i_${e}(${R})`),T=[];if(a>=2)for(let R=a-1;R>=0;R--)T.push(`${L(_,R,a)} * (indices[${R}])`);let A=a<2?"":`
3824
3823
  fn i2o_${e}(indices: ${h.indices}) -> u32 {
3825
3824
  return ${T.join("+")};
3826
- }`,D=R=>(g.indicesToOffset=!0,a<2?R:`i2o_${e}(${R})`),z=(...R)=>a===0?"0u":`${h.indices}(${R.map(w).join(",")})`,H=(R,F)=>a<2?`${R}`:`${G(R,F,a)}`,W=(R,F,ce)=>a<2?`${R}=${ce};`:`${G(R,F,a)}=${ce};`,X={},Y=(R,F)=>{g.broadcastedIndicesToOffset=!0;let ce=`${F.name}broadcastedIndicesTo${e}Offset`;if(ce in X)return`${ce}(${R})`;let Re=[];for(let Se=a-1;Se>=0;Se--){let we=F.indicesGet("outputIndices",Se+F.rank-a);Re.push(`${H(_,Se)} * (${we} % ${H(x,Se)})`)}return X[ce]=`fn ${ce}(outputIndices: ${F.type.indices}) -> u32 {
3827
- return ${Re.length>0?Re.join("+"):"0u"};
3828
- }`,`${ce}(${R})`},pe=(R,F)=>(()=>{if(h.storage===h.value)return`${e}[${R}]=${F};`;if(h.storage==="vec2<u32>"&&h.value==="i32")return`${e}[${R}]=vec2<u32>(u32(${F}), select(0u, 0xFFFFFFFFu, ${F} < 0));`;if(h.storage==="vec2<u32>"&&h.value==="u32")return`${e}[${R}]=vec2<u32>(u32(${F}), 0u);`;if(h.storage==="u32"&&h.value==="vec4<bool>")return`${e}[${R}]=dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(${F}));`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),ue=R=>(()=>{if(h.storage===h.value)return`${e}[${R}]`;if(h.storage==="vec2<u32>"&&h.value==="i32")return`i32(${e}[${R}].x)`;if(h.storage==="vec2<u32>"&&h.value==="u32")return`u32(${e}[${R}].x)`;if(h.storage==="u32"&&h.value==="vec4<bool>")return`vec4<bool>(bool(${e}[${R}] & 0xFFu), bool(${e}[${R}] & 0xFF00u), bool(${e}[${R}] & 0xFF0000u), bool(${e}[${R}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),le=a<2?"":`
3825
+ }`,O=R=>(g.indicesToOffset=!0,a<2?R:`i2o_${e}(${R})`),z=(...R)=>a===0?"0u":`${h.indices}(${R.map(w).join(",")})`,W=(R,G)=>a<2?`${R}`:`${L(R,G,a)}`,V=(R,G,ue)=>a<2?`${R}=${ue};`:`${L(R,G,a)}=${ue};`,j={},Y=(R,G)=>{g.broadcastedIndicesToOffset=!0;let ue=`${G.name}broadcastedIndicesTo${e}Offset`;if(ue in j)return`${ue}(${R})`;let Be=[];for(let ge=a-1;ge>=0;ge--){let Se=G.indicesGet("outputIndices",ge+G.rank-a);Be.push(`${W(_,ge)} * (${Se} % ${W(x,ge)})`)}return j[ue]=`fn ${ue}(outputIndices: ${G.type.indices}) -> u32 {
3826
+ return ${Be.length>0?Be.join("+"):"0u"};
3827
+ }`,`${ue}(${R})`},ae=(R,G)=>(()=>{if(h.storage===h.value)return`${e}[${R}]=${G};`;if(h.storage==="vec2<u32>"&&h.value==="i32")return`${e}[${R}]=vec2<u32>(u32(${G}), select(0u, 0xFFFFFFFFu, ${G} < 0));`;if(h.storage==="vec2<u32>"&&h.value==="u32")return`${e}[${R}]=vec2<u32>(u32(${G}), 0u);`;if(h.storage==="u32"&&h.value==="vec4<bool>")return`${e}[${R}]=dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(${G}));`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),se=R=>(()=>{if(h.storage===h.value)return`${e}[${R}]`;if(h.storage==="vec2<u32>"&&h.value==="i32")return`i32(${e}[${R}].x)`;if(h.storage==="vec2<u32>"&&h.value==="u32")return`u32(${e}[${R}].x)`;if(h.storage==="u32"&&h.value==="vec4<bool>")return`vec4<bool>(bool(${e}[${R}] & 0xFFu), bool(${e}[${R}] & 0xFF00u), bool(${e}[${R}] & 0xFF0000u), bool(${e}[${R}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),ie=a<2?"":`
3829
3828
  fn get_${e}ByIndices(indices: ${h.indices}) -> ${m} {
3830
- return ${ue(`i2o_${e}(indices)`)};
3831
- }`,K=a<2?"":(()=>{let R=l.map(ce=>`d${ce}: u32`).join(", "),F=l.map(ce=>`d${ce}`).join(", ");return`
3829
+ return ${se(`i2o_${e}(indices)`)};
3830
+ }`,q=a<2?"":(()=>{let R=l.map(ue=>`d${ue}: u32`).join(", "),G=l.map(ue=>`d${ue}`).join(", ");return`
3832
3831
  fn get_${e}(${R}) -> ${m} {
3833
- return get_${e}ByIndices(${z(F)});
3834
- }`})(),de=(...R)=>{if(R.length!==a)throw new Error(`indices length must be ${a}`);let F=R.map(w).join(",");return a===0?ue("0u"):a===1?ue(F[0]):(g.get=!0,g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}(${F})`)},re=R=>a<2?ue(R):(g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}ByIndices(${R})`),se=a<2?"":`
3832
+ return get_${e}ByIndices(${z(G)});
3833
+ }`})(),ce=(...R)=>{if(R.length!==a)throw new Error(`indices length must be ${a}`);let G=R.map(w).join(",");return a===0?se("0u"):a===1?se(G[0]):(g.get=!0,g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}(${G})`)},ne=R=>a<2?se(R):(g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}ByIndices(${R})`),Z=a<2?"":`
3835
3834
  fn set_${e}ByIndices(indices: ${h.indices}, value: ${m}) {
3836
- ${pe(`i2o_${e}(indices)`,"value")}
3837
- }`,Z=a<2?"":(()=>{let R=l.map(ce=>`d${ce}: u32`).join(", "),F=l.map(ce=>`d${ce}`).join(", ");return`
3835
+ ${ae(`i2o_${e}(indices)`,"value")}
3836
+ }`,ee=a<2?"":(()=>{let R=l.map(ue=>`d${ue}: u32`).join(", "),G=l.map(ue=>`d${ue}`).join(", ");return`
3838
3837
  fn set_${e}(${R}, value: ${m}) {
3839
- set_${e}ByIndices(${z(F)}, value);
3840
- }`})();return{impl:()=>{let R=[],F=!1;return g.offsetToIndices&&(R.push(S),F=!0),g.indicesToOffset&&(R.push(A),F=!0),g.broadcastedIndicesToOffset&&(Object.values(X).forEach(ce=>R.push(ce)),F=!0),g.set&&(R.push(Z),F=!0),g.setByIndices&&(R.push(se),F=!0),g.get&&(R.push(K),F=!0),g.getByIndices&&(R.push(le),F=!0),!i&&F&&R.unshift(`const ${x} = ${h.indices}(${r.join(",")});`,`const ${_} = ${h.indices}(${k.computeStrides(r).join(",")});`),R.join(`
3841
- `)},type:h,offsetToIndices:I,indicesToOffset:D,broadcastedIndicesToOffset:Y,indices:z,indicesGet:H,indicesSet:W,set:(...R)=>{if(R.length!==a+1)throw new Error(`indices length must be ${a}`);let F=R[a];if(typeof F!="string")throw new Error("value must be string");let ce=R.slice(0,a).map(w).join(",");return a===0?pe("0u",F):a===1?pe(ce[0],F):(g.set=!0,g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}(${ce}, ${F})`)},setByOffset:pe,setByIndices:(R,F)=>a<2?pe(R,F):(g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}ByIndices(${R}, ${F});`),get:de,getByOffset:ue,getByIndices:re,usage:n,name:e,strides:_,shape:x,rank:a}},E=(e,t,r,n=1)=>no(e,t,r,"input",n),M=(e,t,r,n=1)=>no(e,t,r,"output",n),Lr=(e,t,r,n=1)=>no(e,t,r,"internal",n),to=class{constructor(t,r){this.normalizedDispatchGroup=t;this.limits=r;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=Tt){let r=typeof t=="number"?t:t[0],n=typeof t=="number"?1:t[1],o=typeof t=="number"?1:t[2];if(r>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(r*n*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,a=i?`@builtin(global_invocation_id) global_id : vec3<u32>,
3838
+ set_${e}ByIndices(${z(G)}, value);
3839
+ }`})();return{impl:()=>{let R=[],G=!1;return g.offsetToIndices&&(R.push(S),G=!0),g.indicesToOffset&&(R.push(A),G=!0),g.broadcastedIndicesToOffset&&(Object.values(j).forEach(ue=>R.push(ue)),G=!0),g.set&&(R.push(ee),G=!0),g.setByIndices&&(R.push(Z),G=!0),g.get&&(R.push(q),G=!0),g.getByIndices&&(R.push(ie),G=!0),!i&&G&&R.unshift(`const ${x} = ${h.indices}(${r.join(",")});`,`const ${_} = ${h.indices}(${E.computeStrides(r).join(",")});`),R.join(`
3840
+ `)},type:h,offsetToIndices:I,indicesToOffset:O,broadcastedIndicesToOffset:Y,indices:z,indicesGet:W,indicesSet:V,set:(...R)=>{if(R.length!==a+1)throw new Error(`indices length must be ${a}`);let G=R[a];if(typeof G!="string")throw new Error("value must be string");let ue=R.slice(0,a).map(w).join(",");return a===0?ae("0u",G):a===1?ae(ue[0],G):(g.set=!0,g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}(${ue}, ${G})`)},setByOffset:ae,setByIndices:(R,G)=>a<2?ae(R,G):(g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}ByIndices(${R}, ${G});`),get:ce,getByOffset:se,getByIndices:ne,usage:n,name:e,strides:_,shape:x,rank:a}},k=(e,t,r,n=1)=>Jn(e,t,r,"input",n),D=(e,t,r,n=1)=>Jn(e,t,r,"output",n),Wr=(e,t,r,n=1)=>Jn(e,t,r,"internal",n),Zn=class{constructor(t,r){this.normalizedDispatchGroup=t;this.limits=r;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=St){let r=typeof t=="number"?t:t[0],n=typeof t=="number"?1:t[1],o=typeof t=="number"?1:t[2];if(r>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(r*n*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,a=i?`@builtin(global_invocation_id) global_id : vec3<u32>,
3842
3841
  @builtin(workgroup_id) workgroup_id : vec3<u32>,
3843
3842
  @builtin(local_invocation_id) local_id : vec3<u32>`:`@builtin(global_invocation_id) global_id : vec3<u32>,
3844
3843
  @builtin(local_invocation_id) local_id : vec3<u32>,
@@ -3853,9 +3852,9 @@ var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Objec
3853
3852
  struct Uniforms { ${t.join(", ")} };
3854
3853
  @group(0) @binding(${this.variableIndex}) var<uniform> uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(t=>t.impl()).join(`
3855
3854
  `)+this.internalVariables.map(t=>t.impl()).join(`
3856
- `)}get variablesInfo(){if(this.uniforms.length===0)return;let t=r=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(r)];return this.uniforms.map(r=>[t(r.type),r.length??1])}},Ya=(e,t)=>new to(e,t),Nt=(e,t)=>{let r=e.length,n=[];for(let o=0;o<r;o++){let i=r-1-o,a=e[i]||1;(t[t.length-1-o]||1)>1&&a===1&&n.unshift(i)}return n}});var Ep,Xa,kp,Pp,Ue,Za,Qa,Et=U(()=>{"use strict";Q();ae();Ce();ie();Ep=e=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.")},Xa=(e,t)=>t&&t.length!==e?[...new Array(e).keys()].reverse():t,kp=(e,t)=>k.sortBasedOnPerm(e,Xa(e.length,t)),Pp=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} {
3855
+ `)}get variablesInfo(){if(this.uniforms.length===0)return;let t=r=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(r)];return this.uniforms.map(r=>[t(r.type),r.length??1])}},La=(e,t)=>new Zn(e,t),Mt=(e,t)=>{let r=e.length,n=[];for(let o=0;o<r;o++){let i=r-1-o,a=e[i]||1;(t[t.length-1-o]||1)>1&&a===1&&n.unshift(i)}return n}});var Sp,Ha,Ip,Cp,Re,Ga,Fa,Ct=M(()=>{"use strict";X();oe();xe();re();Sp=e=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.")},Ha=(e,t)=>t&&t.length!==e?[...new Array(e).keys()].reverse():t,Ip=(e,t)=>E.sortBasedOnPerm(e,Ha(e.length,t)),Cp=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} {
3857
3856
  var a: ${r.type.indices};`);for(let i=0;i<t;++i)o.push(r.indicesSet("a",e[i],`i[${i}]`));return o.push("return a;}"),o.join(`
3858
- `)},Ue=(e,t)=>{let r=e.dataType,n=e.dims.length,o=Xa(n,t),i=kp(e.dims,o),a=M("output",r,i.length),l=E("a",r,n),d;if(o.length===2&&o[0]===1&&o[1]===0){let p=a.type.value,m=[16,16,1];d=u=>`
3857
+ `)},Re=(e,t)=>{let r=e.dataType,n=e.dims.length,o=Ha(n,t),i=Ip(e.dims,o),a=D("output",r,i.length),l=k("a",r,n),d;if(o.length===2&&o[0]===1&&o[1]===0){let p=a.type.value,m=[16,16,1];d=u=>`
3859
3858
  ${u.registerUniform("output_size","u32").declareVariables(l,a)}
3860
3859
  var<workgroup> tile : array<array<${p}, ${m[0]+1}>, ${m[0]}>;
3861
3860
  ${u.mainStart(m)}
@@ -3875,7 +3874,7 @@ var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Objec
3875
3874
  }`}else d=p=>`
3876
3875
  ${p.registerUniform("output_size","u32").declareVariables(l,a)}
3877
3876
 
3878
- ${Pp(o,n,l,a)}
3877
+ ${Cp(o,n,l,a)}
3879
3878
 
3880
3879
  ${p.mainStart()}
3881
3880
  ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
@@ -3884,24 +3883,24 @@ var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Objec
3884
3883
  let aIndices = perm(indices);
3885
3884
 
3886
3885
  ${a.setByOffset("global_idx",l.getByIndices("aIndices"))}
3887
- }`;return{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:p=>{let m=k.size(i);return{outputs:[{dims:i,dataType:p[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...V(p[0].dims,i)]}},getShaderSource:d}},Za=(e,t)=>{Ep(e.inputs),e.compute(Ue(e.inputs[0],t.perm))},Qa=e=>te({perm:e.perm})});var Op,Dp,zp,Bp,Rp,Mp,Up,Vp,Np,Wp,rt,Ja,es,ts,rs,ns,os,is,as,ss,us,ds=U(()=>{"use strict";Q();ae();ie();Fr();Et();Op={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"},Dp={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"},zp={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Bp={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Rp=(e,t)=>{let r=[];for(let n=t-e;n<t;++n)r.push(n);return r},Mp=(e,t)=>{let r=[],n=e.length;for(let i=0;i<n;i++)t.indexOf(i)===-1&&r.push(e[i]);let o=t.map(i=>e[i]);return[r,o]},Up=(e,t)=>{let r=e.length+t.length,n=[],o=0;for(let i=0;i<r;i++)t.indexOf(i)===-1?n.push(e[o++]):n.push(1);return n},Vp=(e,t)=>{for(let r=0;r<e.length;++r)if(e[e.length-r-1]!==t-1-r)return!1;return!0},Np=(e,t)=>{let r=[];if(!Vp(e,t)){for(let n=0;n<t;++n)e.indexOf(n)===-1&&r.push(n);e.forEach(n=>r.push(n))}return r},Wp=(e,t,r,n,o,i,a)=>{let l=r[0].dims,d=k.size(i),p=k.size(a),m=E("_A",r[0].dataType,l),u=M("output",o,i),h=32,w=`
3886
+ }`;return{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:p=>{let m=E.size(i);return{outputs:[{dims:i,dataType:p[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...U(p[0].dims,i)]}},getShaderSource:d}},Ga=(e,t)=>{Sp(e.inputs),e.compute(Re(e.inputs[0],t.perm))},Fa=e=>Q({perm:e.perm})});var Tp,Ap,kp,Ep,Pp,Op,zp,Bp,Dp,Rp,et,qa,Ka,ja,Ya,Xa,Za,Qa,Ja,es,ts,rs=M(()=>{"use strict";X();oe();re();Lr();Ct();Tp={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"},Ap={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"},kp={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Ep={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Pp=(e,t)=>{let r=[];for(let n=t-e;n<t;++n)r.push(n);return r},Op=(e,t)=>{let r=[],n=e.length;for(let i=0;i<n;i++)t.indexOf(i)===-1&&r.push(e[i]);let o=t.map(i=>e[i]);return[r,o]},zp=(e,t)=>{let r=e.length+t.length,n=[],o=0;for(let i=0;i<r;i++)t.indexOf(i)===-1?n.push(e[o++]):n.push(1);return n},Bp=(e,t)=>{for(let r=0;r<e.length;++r)if(e[e.length-r-1]!==t-1-r)return!1;return!0},Dp=(e,t)=>{let r=[];if(!Bp(e,t)){for(let n=0;n<t;++n)e.indexOf(n)===-1&&r.push(n);e.forEach(n=>r.push(n))}return r},Rp=(e,t,r,n,o,i,a)=>{let l=r[0].dims,d=E.size(i),p=E.size(a),m=k("_A",r[0].dataType,l),u=D("output",o,i),h=32,w=`
3888
3887
  var<workgroup> aBestValues : array<f32, ${h}>;
3889
- `;return{name:e,shaderCache:t,getShaderSource:b=>`
3890
- ${b.registerUniform("reduceSize","u32").declareVariables(m,u)}
3888
+ `;return{name:e,shaderCache:t,getShaderSource:y=>`
3889
+ ${y.registerUniform("reduceSize","u32").declareVariables(m,u)}
3891
3890
  ${w}
3892
3891
  fn DIV_CEIL(a : u32, b : u32) -> u32 {
3893
3892
  return ((a - 1u) / b + 1u);
3894
3893
  }
3895
- ${b.mainStart(h)}
3894
+ ${y.mainStart(h)}
3896
3895
 
3897
3896
  let outputIndex = global_idx / ${h};
3898
3897
  let offset = outputIndex * uniforms.reduceSize;
3899
3898
 
3900
- var bestValue = f32(${zp[n]});
3899
+ var bestValue = f32(${kp[n]});
3901
3900
  let Length = uniforms.reduceSize;
3902
3901
  for (var k = local_idx; k < Length; k = k + ${h}) {
3903
3902
  let candidate = f32(${m.getByOffset("offset + k")});
3904
- bestValue = ${Op[n]};
3903
+ bestValue = ${Tp[n]};
3905
3904
  }
3906
3905
  aBestValues[local_idx] = bestValue;
3907
3906
  workgroupBarrier();
@@ -3912,7 +3911,7 @@ var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Objec
3912
3911
  let interval = DIV_CEIL(reduceSize, 2u);
3913
3912
  if (local_idx < currentSize) {
3914
3913
  let candidate = aBestValues[local_idx + interval];
3915
- bestValue = ${Dp[n]};
3914
+ bestValue = ${Ap[n]};
3916
3915
  aBestValues[local_idx] = bestValue;
3917
3916
  }
3918
3917
  reduceSize = interval;
@@ -3920,13 +3919,13 @@ var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Objec
3920
3919
  }
3921
3920
 
3922
3921
  if (local_idx == 0u) {
3923
- ${u.setByOffset("outputIndex",`${n==="mean"?`${u.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${u.type.storage}(${Bp[n]})`}`)};
3922
+ ${u.setByOffset("outputIndex",`${n==="mean"?`${u.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${u.type.storage}(${Ep[n]})`}`)};
3924
3923
  }
3925
- }`,getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:d},programUniforms:[{type:12,data:p}]})}},rt=(e,t,r,n)=>{let o=e.inputs.length===1?r:oo(e.inputs,r),i=o.axes;i.length===0&&!o.noopWithEmptyAxes&&(i=e.inputs[0].dims.map((w,g)=>g));let a=k.normalizeAxes(i,e.inputs[0].dims.length),l=a,d=e.inputs[0],p=Np(l,e.inputs[0].dims.length);p.length>0&&(d=e.compute(Ue(e.inputs[0],p),{inputs:[0],outputs:[-1]})[0],l=Rp(l.length,d.dims.length));let[m,u]=Mp(d.dims,l),h=m;o.keepDims&&(h=Up(m,a)),e.compute(Wp(t,{hint:o.cacheKey,inputDependencies:["type"]},[d],n,e.inputs[0].dataType,h,u),{inputs:[d]})},Ja=(e,t)=>{rt(e,"ReduceMeanShared",t,"mean")},es=(e,t)=>{rt(e,"ReduceL1Shared",t,"l1")},ts=(e,t)=>{rt(e,"ReduceL2Shared",t,"l2")},rs=(e,t)=>{rt(e,"ReduceLogSumExpShared",t,"logSumExp")},ns=(e,t)=>{rt(e,"ReduceMaxShared",t,"max")},os=(e,t)=>{rt(e,"ReduceMinShared",t,"min")},is=(e,t)=>{rt(e,"ReduceProdShared",t,"prod")},as=(e,t)=>{rt(e,"ReduceSumShared",t,"sum")},ss=(e,t)=>{rt(e,"ReduceSumSquareShared",t,"sumSquare")},us=(e,t)=>{rt(e,"ReduceLogSumShared",t,"logSum")}});var nt,Hp,qr,oo,ot,Gp,Lp,Fp,qp,jp,Kp,Yp,Xp,Zp,Qp,it,ls,cs,ps,ms,fs,hs,gs,ys,bs,ws,Fr=U(()=>{"use strict";Q();ae();Ce();ie();ds();nt=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Hp=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],qr=(e,t,r,n,o,i,a=!1,l=!1)=>{let d=[],p=r[0].dims,m=p.length,u=k.normalizeAxes(o,m),h=!l&&u.length===0;p.forEach((x,_)=>{h||u.indexOf(_)>=0?a&&d.push(1):d.push(x)});let w=d.length,g=k.size(d);return{name:e,shaderCache:t,getShaderSource:x=>{let _=[],$=E("_A",r[0].dataType,m),S=M("output",i,w),I=n($,S,u),T=I[2];for(let A=0,D=0;A<m;A++)h||u.indexOf(A)>=0?(a&&D++,T=`for(var j${A}: u32 = 0; j${A} < ${p[A]}; j${A}++) {
3924
+ }`,getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:d},programUniforms:[{type:12,data:p}]})}},et=(e,t,r,n)=>{let o=e.inputs.length===1?r:eo(e.inputs,r),i=o.axes;i.length===0&&!o.noopWithEmptyAxes&&(i=e.inputs[0].dims.map((w,g)=>g));let a=E.normalizeAxes(i,e.inputs[0].dims.length),l=a,d=e.inputs[0],p=Dp(l,e.inputs[0].dims.length);p.length>0&&(d=e.compute(Re(e.inputs[0],p),{inputs:[0],outputs:[-1]})[0],l=Pp(l.length,d.dims.length));let[m,u]=Op(d.dims,l),h=m;o.keepDims&&(h=zp(m,a)),e.compute(Rp(t,{hint:o.cacheKey,inputDependencies:["type"]},[d],n,e.inputs[0].dataType,h,u),{inputs:[d]})},qa=(e,t)=>{et(e,"ReduceMeanShared",t,"mean")},Ka=(e,t)=>{et(e,"ReduceL1Shared",t,"l1")},ja=(e,t)=>{et(e,"ReduceL2Shared",t,"l2")},Ya=(e,t)=>{et(e,"ReduceLogSumExpShared",t,"logSumExp")},Xa=(e,t)=>{et(e,"ReduceMaxShared",t,"max")},Za=(e,t)=>{et(e,"ReduceMinShared",t,"min")},Qa=(e,t)=>{et(e,"ReduceProdShared",t,"prod")},Ja=(e,t)=>{et(e,"ReduceSumShared",t,"sum")},es=(e,t)=>{et(e,"ReduceSumSquareShared",t,"sumSquare")},ts=(e,t)=>{et(e,"ReduceLogSumShared",t,"logSum")}});var tt,Mp,Hr,eo,rt,Up,Vp,Np,Wp,Lp,Hp,Gp,Fp,qp,Kp,nt,ns,os,is,as,ss,us,ds,ls,cs,ps,Lr=M(()=>{"use strict";X();oe();xe();re();rs();tt=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Mp=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Hr=(e,t,r,n,o,i,a=!1,l=!1)=>{let d=[],p=r[0].dims,m=p.length,u=E.normalizeAxes(o,m),h=!l&&u.length===0;p.forEach((x,_)=>{h||u.indexOf(_)>=0?a&&d.push(1):d.push(x)});let w=d.length,g=E.size(d);return{name:e,shaderCache:t,getShaderSource:x=>{let _=[],$=k("_A",r[0].dataType,m),S=D("output",i,w),I=n($,S,u),T=I[2];for(let A=0,O=0;A<m;A++)h||u.indexOf(A)>=0?(a&&O++,T=`for(var j${A}: u32 = 0; j${A} < ${p[A]}; j${A}++) {
3926
3925
  ${I[2].includes("last_index")?`let last_index = j${A};`:""}
3927
3926
  ${$.indicesSet("input_indices",A,`j${A}`)}
3928
3927
  ${T}
3929
- }`):(_.push(`${$.indicesSet("input_indices",A,S.indicesGet("output_indices",D))};`),D++);return`
3928
+ }`):(_.push(`${$.indicesSet("input_indices",A,S.indicesGet("output_indices",O))};`),O++);return`
3930
3929
 
3931
3930
  ${x.registerUniform("output_size","u32").declareVariables($,S)}
3932
3931
 
@@ -3943,25 +3942,25 @@ var Rn=Object.defineProperty;var op=Object.getOwnPropertyDescriptor;var ip=Objec
3943
3942
  ${I[3]}
3944
3943
  ${I.length===4?S.setByOffset("global_idx","value"):I.slice(4).join(`
3945
3944
  `)}
3946
- }`},getRunData:()=>({outputs:[{dims:d,dataType:i}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},...V(p,d)]})}},oo=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),te({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},ot=(e,t,r,n)=>{let o=e.inputs,i=o.length===1?r:oo(o,r);e.compute(qr(t,{hint:i.cacheKey,inputDependencies:["rank"]},[o[0]],i.noopWithEmptyAxes&&i.axes.length===0?Hp:n,i.axes,o[0].dataType,i.keepDims,i.noopWithEmptyAxes),{inputs:[0]})},Gp=(e,t)=>{nt(e.inputs),ot(e,"ReduceLogSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},Lp=(e,t)=>{nt(e.inputs),ot(e,"ReduceL1",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},Fp=(e,t)=>{nt(e.inputs),ot(e,"ReduceL2",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},qp=(e,t)=>{nt(e.inputs),ot(e,"ReduceLogSumExp",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},jp=(e,t)=>{nt(e.inputs),ot(e,"ReduceMax",t,(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(n.indicesSet("input_indices",l,0));return[`${a.join(`
3947
- `)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},Kp=(e,t)=>{nt(e.inputs),ot(e,"ReduceMean",t,(n,o,i)=>{let a=1;for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&(a*=e.inputs[0].dims[l]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${a});`]})},Yp=(e,t)=>{nt(e.inputs),ot(e,"ReduceMin",t,(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
3948
- `)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},Xp=(e,t)=>{nt(e.inputs),ot(e,"ReduceProd",t,(n,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},Zp=(e,t)=>{nt(e.inputs),ot(e,"ReduceSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Qp=(e,t)=>{nt(e.inputs),ot(e,"ReduceSumSquare",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},it=(e,t,r)=>{if(t.length===0)return r;let n=1,o=1;for(let i=0;i<t.length;i++)t.indexOf(i)===-1?n*=e[i]:o*=e[i];return o<32&&n>1024},ls=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Kp(e,t):Ja(e,t)},cs=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Lp(e,t):es(e,t)},ps=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Fp(e,t):ts(e,t)},ms=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?qp(e,t):rs(e,t)},fs=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?jp(e,t):ns(e,t)},hs=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Yp(e,t):os(e,t)},gs=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Xp(e,t):is(e,t)},ys=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Zp(e,t):as(e,t)},bs=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Qp(e,t):ss(e,t)},ws=(e,t)=>{it(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Gp(e,t):us(e,t)}});var vs,$s,_s,io,xs=U(()=>{"use strict";Q();Ce();Fr();vs=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},$s=(e,t)=>{vs(e.inputs);let r=(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
3945
+ }`},getRunData:()=>({outputs:[{dims:d,dataType:i}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},...U(p,d)]})}},eo=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),Q({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},rt=(e,t,r,n)=>{let o=e.inputs,i=o.length===1?r:eo(o,r);e.compute(Hr(t,{hint:i.cacheKey,inputDependencies:["rank"]},[o[0]],i.noopWithEmptyAxes&&i.axes.length===0?Mp:n,i.axes,o[0].dataType,i.keepDims,i.noopWithEmptyAxes),{inputs:[0]})},Up=(e,t)=>{tt(e.inputs),rt(e,"ReduceLogSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},Vp=(e,t)=>{tt(e.inputs),rt(e,"ReduceL1",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},Np=(e,t)=>{tt(e.inputs),rt(e,"ReduceL2",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Wp=(e,t)=>{tt(e.inputs),rt(e,"ReduceLogSumExp",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},Lp=(e,t)=>{tt(e.inputs),rt(e,"ReduceMax",t,(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(n.indicesSet("input_indices",l,0));return[`${a.join(`
3946
+ `)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},Hp=(e,t)=>{tt(e.inputs),rt(e,"ReduceMean",t,(n,o,i)=>{let a=1;for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&(a*=e.inputs[0].dims[l]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${a});`]})},Gp=(e,t)=>{tt(e.inputs),rt(e,"ReduceMin",t,(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
3947
+ `)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},Fp=(e,t)=>{tt(e.inputs),rt(e,"ReduceProd",t,(n,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},qp=(e,t)=>{tt(e.inputs),rt(e,"ReduceSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Kp=(e,t)=>{tt(e.inputs),rt(e,"ReduceSumSquare",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},nt=(e,t,r)=>{if(t.length===0)return r;let n=1,o=1;for(let i=0;i<t.length;i++)t.indexOf(i)===-1?n*=e[i]:o*=e[i];return o<32&&n>1024},ns=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Hp(e,t):qa(e,t)},os=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Vp(e,t):Ka(e,t)},is=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Np(e,t):ja(e,t)},as=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Wp(e,t):Ya(e,t)},ss=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Lp(e,t):Xa(e,t)},us=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Gp(e,t):Za(e,t)},ds=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Fp(e,t):Qa(e,t)},ls=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?qp(e,t):Ja(e,t)},cs=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Kp(e,t):es(e,t)},ps=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Up(e,t):ts(e,t)}});var ms,fs,hs,to,gs=M(()=>{"use strict";X();xe();Lr();ms=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},fs=(e,t)=>{ms(e.inputs);let r=(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
3949
3948
  `)}`,`var value = ${n.getByIndices("input_indices")};
3950
3949
  var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) {
3951
3950
  value = ${n.getByIndices("input_indices")};
3952
3951
  best_index = i32(last_index);
3953
- }`,"",o.setByOffset("global_idx","best_index")]};e.compute(qr("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},_s=(e,t)=>{vs(e.inputs);let r=(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
3952
+ }`,"",o.setByOffset("global_idx","best_index")]};e.compute(Hr("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},hs=(e,t)=>{ms(e.inputs);let r=(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
3954
3953
  `)}`,`var value = ${n.getByIndices("input_indices")};
3955
3954
  var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) {
3956
3955
  value = ${n.getByIndices("input_indices")};
3957
3956
  best_index = i32(last_index);
3958
- }`,"",o.setByOffset("global_idx","best_index")]};e.compute(qr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},io=e=>te(e)});var Jp,em,tm,rm,Wt,nm,Ss,jr=U(()=>{"use strict";Q();Nr();ie();Jp=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4],l=e[5];if(a&&l)throw new Error("Attention cannot have both past and relative_position_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let d=r.dims[0],p=r.dims[1],m=r.dims[2];if(o.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(n.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(n.dims[0]!==m)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(o.dims[0]!==n.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let u=o.dims[0]/3,h=u,w=h;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let S of t.qkvHiddenSizes)if(S%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");u=t.qkvHiddenSizes[0],h=t.qkvHiddenSizes[1],w=t.qkvHiddenSizes[2]}let g=p;if(u!==h)throw new Error("qkv_hidden_sizes first element should be same as the second");if(o.dims[0]!==u+h+w)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let b=0;if(a){if(h!==w)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(a.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(a.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(a.dims[1]!==d)throw new Error('Input "past" second dimension must be batch_size');if(a.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(a.dims[4]!==h/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(b=a.dims[3])}let x=g+b,_=-1,$=0;if(i)throw new Error("Mask not supported");if(a)throw new Error("past is not supported");return{batchSize:d,sequenceLength:p,pastSequenceLength:b,kvSequenceLength:g,totalSequenceLength:x,maxSequenceLength:_,inputHiddenSize:m,hiddenSize:u,vHiddenSize:w,headSize:Math.floor(u/t.numHeads),vHeadSize:Math.floor(w/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:$,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},em=(e,t,r,n)=>{let o=$e(n),i=64,a=n/o;a<i?i=1:a/8<64&&(i=Math.ceil(a/8));let l=Math.ceil(n/o/i),d=[{type:t.dataType,data:1/n},{type:12,data:a},{type:12,data:l}],p=ge(t.dataType,o),m=Oe(1,o),u=h=>{let w=M("x",t.dataType,t.dims,o),b=[{name:"d_inv",type:Oe(t.dataType)},{name:"d_comp",type:"u32"},{name:"elements_per_thread",type:"u32"}];return`
3957
+ }`,"",o.setByOffset("global_idx","best_index")]};e.compute(Hr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},to=e=>Q(e)});var jp,Yp,Xp,Zp,Ut,Qp,ys,Gr=M(()=>{"use strict";X();Mr();re();jp=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4],l=e[5];if(a&&l)throw new Error("Attention cannot have both past and attention_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let d=r.dims[0],p=r.dims[1],m=r.dims[2];if(o.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(n.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(n.dims[0]!==m)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(o.dims[0]!==n.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let u=o.dims[0]/3,h=u,w=h;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let S of t.qkvHiddenSizes)if(S%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");u=t.qkvHiddenSizes[0],h=t.qkvHiddenSizes[1],w=t.qkvHiddenSizes[2]}let g=p;if(u!==h)throw new Error("qkv_hidden_sizes first element should be same as the second");if(o.dims[0]!==u+h+w)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let y=0;if(a){if(h!==w)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(a.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(a.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(a.dims[1]!==d)throw new Error('Input "past" second dimension must be batch_size');if(a.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(a.dims[4]!==h/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(y=a.dims[3])}let x=g+y,_=-1,$=0;if(i)throw new Error("Mask not supported");if(a)throw new Error("past is not supported");if(l){if(l.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(l.dims[0]!==d||l.dims[1]!==t.numHeads||l.dims[2]!==p||l.dims[3]!==x)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:d,sequenceLength:p,pastSequenceLength:y,kvSequenceLength:g,totalSequenceLength:x,maxSequenceLength:_,inputHiddenSize:m,hiddenSize:u,vHiddenSize:w,headSize:Math.floor(u/t.numHeads),vHeadSize:Math.floor(w/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:$,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},Yp=(e,t,r,n)=>{let o=he(n),i=64,a=n/o;a<i&&(i=32);let l=Math.ceil(n/o/i),d=[{type:1,data:1/n},{type:12,data:a},{type:12,data:l}],p=pe(t.dataType,o),m=ke(1,o),u=["type"],h=w=>{let g=D("x",t.dataType,t.dims,o),y=ke(t.dataType),x=[{name:"d_inv",type:"f32"},{name:"d_comp",type:"u32"},{name:"elements_per_thread",type:"u32"}];return`
3959
3958
  var<workgroup> thread_max: array<f32, ${i}>;
3960
3959
  var<workgroup> thread_sum: array<f32, ${i}>;
3961
- ${h.registerUniforms(b).declareVariables(w)}
3962
- ${h.mainStart([i,1,1])}
3960
+ ${w.registerUniforms(x).declareVariables(g)}
3961
+ ${w.mainStart([i,1,1])}
3963
3962
  let local_offset = local_idx * uniforms.elements_per_thread;
3964
- let offset = workgroup_id.x * uniforms.d_comp + local_offset;
3963
+ let offset = (global_idx / ${i}) * uniforms.d_comp + local_offset;
3965
3964
 
3966
3965
  var thread_max_vector = ${m}(-3.402823e+38f);
3967
3966
  for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) {
@@ -3989,21 +3988,21 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
3989
3988
 
3990
3989
  if (sum == 0) {
3991
3990
  for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) {
3992
- x[offset + i] = ${w.type.value}(uniforms.d_inv);
3991
+ x[offset + i] = ${g.type.value}(${y}(uniforms.d_inv));
3993
3992
  }
3994
3993
  } else {
3995
3994
  for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) {
3996
3995
  var f32input = ${m}(x[offset + i]);
3997
- x[offset + i] = ${w.type.value}(exp(f32input - max_value) / sum);
3996
+ x[offset + i] = ${g.type.value}(exp(f32input - max_value) / sum);
3998
3997
  }
3999
3998
  }
4000
- }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${i};${p};${o}`},getShaderSource:u,getRunData:()=>({outputs:[],dispatchGroup:{x:r},programUniforms:d})}},tm=(e,t,r,n,o,i,a,l)=>{let d=l+i.kvSequenceLength,p=[i.batchSize,i.numHeads,i.sequenceLength,d],m=i.kvNumHeads===void 0&&e.outputCount>1,u=m?[i.batchSize,i.numHeads,d,i.headSize]:void 0,h=a.scale===0?1/Math.sqrt(i.headSize):a.scale,w=$e(i.headSize),g=i.headSize/w,b=12,x={x:Math.ceil(d/b),y:Math.ceil(i.sequenceLength/b),z:i.batchSize*i.numHeads},_=[{type:12,data:i.sequenceLength},{type:12,data:g},{type:12,data:d},{type:12,data:i.numHeads},{type:1,data:h},{type:12,data:l},{type:12,data:i.kvSequenceLength}],$=["type","type"];n&&$.push("type"),o&&$.push("type");let S=[{dims:p,dataType:t.dataType,gpuDataType:0}];m&&S.push({dims:u,dataType:t.dataType,gpuDataType:0});let I=T=>{let A=E("q",t.dataType,t.dims,w),D=E("key",r.dataType,r.dims,w),z=[A,D];if(n){let pe=E("past_key",n.dataType,n.dims,w);z.push(pe)}o&&z.push(E("relative_position_bias",o.dataType,o.dims));let H=M("output",t.dataType,p),W=[H];m&&W.push(M("present_key",t.dataType,u,w));let X=Oe(1,w),Y=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return`
4001
- const TILE_SIZE = ${b}u;
3999
+ }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${i};${p};${o}`,inputDependencies:u},getShaderSource:h,getRunData:()=>({outputs:[],dispatchGroup:{x:r},programUniforms:d})}},Xp=(e,t,r,n,o,i,a,l)=>{let d=l+i.kvSequenceLength,p=[i.batchSize,i.numHeads,i.sequenceLength,d],m=i.kvNumHeads===void 0&&e.outputCount>1,u=m?[i.batchSize,i.numHeads,d,i.headSize]:void 0,h=a.scale===0?1/Math.sqrt(i.headSize):a.scale,w=he(i.headSize),g=i.headSize/w,y=12,x={x:Math.ceil(d/y),y:Math.ceil(i.sequenceLength/y),z:i.batchSize*i.numHeads},_=[{type:12,data:i.sequenceLength},{type:12,data:g},{type:12,data:d},{type:12,data:i.numHeads},{type:1,data:h},{type:12,data:l},{type:12,data:i.kvSequenceLength}],$=["type","type"];n&&$.push("type"),o&&$.push("type");let S=[{dims:p,dataType:t.dataType,gpuDataType:0}];m&&S.push({dims:u,dataType:t.dataType,gpuDataType:0});let I=T=>{let A=k("q",t.dataType,t.dims,w),O=k("key",r.dataType,r.dims,w),z=[A,O];if(n){let ae=k("past_key",n.dataType,n.dims,w);z.push(ae)}o&&z.push(k("attention_bias",o.dataType,o.dims));let W=D("output",t.dataType,p),V=[W];m&&V.push(D("present_key",t.dataType,u,w));let j=ke(1,w),Y=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return`
4000
+ const TILE_SIZE = ${y}u;
4002
4001
 
4003
- var<workgroup> tileQ: array<${A.type.storage}, ${b*b}>;
4004
- var<workgroup> tileK: array<${A.type.storage}, ${b*b}>;
4005
- ${T.registerUniforms(Y).declareVariables(...z,...W)}
4006
- ${T.mainStart([b,b,1])}
4002
+ var<workgroup> tileQ: array<${A.type.storage}, ${y*y}>;
4003
+ var<workgroup> tileK: array<${A.type.storage}, ${y*y}>;
4004
+ ${T.registerUniforms(Y).declareVariables(...z,...V)}
4005
+ ${T.mainStart([y,y,1])}
4007
4006
  // x holds the N and y holds the M
4008
4007
  let headIdx = workgroup_id.z;
4009
4008
  let m = workgroup_id.y * TILE_SIZE;
@@ -4014,7 +4013,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4014
4013
  let pastKeyOffset = uniforms.past_sequence_length * uniforms.K * headIdx;`:`
4015
4014
  let kOffset = uniforms.N * uniforms.K * headIdx + n * uniforms.K;`)()}
4016
4015
  ${m?"let presentKeyOffset = headIdx * uniforms.N * uniforms.K;":""}
4017
- var value = ${X}(0);
4016
+ var value = ${j}(0);
4018
4017
  for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
4019
4018
  if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) {
4020
4019
  tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x];
@@ -4033,7 +4032,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4033
4032
  workgroupBarrier();
4034
4033
 
4035
4034
  for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) {
4036
- value += ${X}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]);
4035
+ value += ${j}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]);
4037
4036
  }
4038
4037
 
4039
4038
  workgroupBarrier();
@@ -4043,13 +4042,13 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4043
4042
  if (global_id.y < uniforms.M && global_id.x < uniforms.N) {
4044
4043
  let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x;
4045
4044
  var sum: f32 = ${(()=>{switch(w){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${w}`)}})()};
4046
- output[outputIdx] = ${H.type.value} (sum * uniforms.alpha) + ${o?"relative_position_bias[outputIdx]":"0.0"};
4045
+ output[outputIdx] = ${W.type.value} (sum * uniforms.alpha) + ${o?"attention_bias[outputIdx]":"0.0"};
4047
4046
  }
4048
- }`};return{name:"AttentionProbs",shaderCache:{hint:`${w};${o!==void 0};${n!==void 0};${e.outputCount}`,inputDependencies:$},getRunData:()=>({outputs:S,dispatchGroup:x,programUniforms:_}),getShaderSource:I}},rm=(e,t,r,n,o,i)=>{let a=i+o.kvSequenceLength,l=o.nReps?o.nReps:1,d=o.vHiddenSize*l,p=o.kvNumHeads==null&&e.outputCount>1,m=p?[o.batchSize,o.numHeads,a,o.headSize]:void 0,u=[o.batchSize,o.sequenceLength,d],h=12,w={x:Math.ceil(o.vHeadSize/h),y:Math.ceil(o.sequenceLength/h),z:o.batchSize*o.numHeads},g=[{type:12,data:o.sequenceLength},{type:12,data:a},{type:12,data:o.vHeadSize},{type:12,data:o.numHeads},{type:12,data:d},{type:12,data:i},{type:12,data:o.kvSequenceLength}],b=n?["type","type","type"]:["type","type"],x=[{dims:u,dataType:t.dataType,gpuDataType:0}];p&&x.push({dims:m,dataType:t.dataType,gpuDataType:0});let _=$=>{let S=E("probs",t.dataType,t.dims),I=E("v",r.dataType,r.dims),T=[S,I];n&&T.push(E("past_value",n.dataType,n.dims));let D=[M("output",t.dataType,u)];p&&D.push(M("present_value",t.dataType,m));let z=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return`
4047
+ }`};return{name:"AttentionProbs",shaderCache:{hint:`${w};${o!==void 0};${n!==void 0};${e.outputCount}`,inputDependencies:$},getRunData:()=>({outputs:S,dispatchGroup:x,programUniforms:_}),getShaderSource:I}},Zp=(e,t,r,n,o,i)=>{let a=i+o.kvSequenceLength,l=o.nReps?o.nReps:1,d=o.vHiddenSize*l,p=o.kvNumHeads==null&&e.outputCount>1,m=p?[o.batchSize,o.numHeads,a,o.headSize]:void 0,u=[o.batchSize,o.sequenceLength,d],h=12,w={x:Math.ceil(o.vHeadSize/h),y:Math.ceil(o.sequenceLength/h),z:o.batchSize*o.numHeads},g=[{type:12,data:o.sequenceLength},{type:12,data:a},{type:12,data:o.vHeadSize},{type:12,data:o.numHeads},{type:12,data:d},{type:12,data:i},{type:12,data:o.kvSequenceLength}],y=n?["type","type","type"]:["type","type"],x=[{dims:u,dataType:t.dataType,gpuDataType:0}];p&&x.push({dims:m,dataType:t.dataType,gpuDataType:0});let _=$=>{let S=k("probs",t.dataType,t.dims),I=k("v",r.dataType,r.dims),T=[S,I];n&&T.push(k("past_value",n.dataType,n.dims));let O=[D("output",t.dataType,u)];p&&O.push(D("present_value",t.dataType,m));let z=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return`
4049
4048
  const TILE_SIZE = ${h}u;
4050
4049
  var<workgroup> tileQ: array<${S.type.value}, ${h*h}>;
4051
4050
  var<workgroup> tileK: array<${S.type.value}, ${h*h}>;
4052
- ${$.registerUniforms(z).declareVariables(...T,...D)}
4051
+ ${$.registerUniforms(z).declareVariables(...T,...O)}
4053
4052
  ${$.mainStart([h,h,1])}
4054
4053
  let headIdx = workgroup_id.z;
4055
4054
  let m = global_id.y;
@@ -4096,13 +4095,13 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4096
4095
  + currentBatchHeadNumber * uniforms.N + n;
4097
4096
  output[outputIdx] = value;
4098
4097
  }
4099
- }`};return{name:"AttentionScore",shaderCache:{hint:`${n!==void 0};${e.outputCount}`,inputDependencies:b},getRunData:()=>({outputs:x,dispatchGroup:w,programUniforms:g}),getShaderSource:_}},Wt=(e,t,r,n,o,i,a,l,d,p,m)=>{let u=e.outputCount,h=p.kvNumHeads!==void 0||u>1?p.pastSequenceLength:0,w=h+p.kvSequenceLength,g=p.kvNumHeads===void 0&&u>1&&a?[t,r,a]:[t,r];d&&g.push(d);let b=e.compute(tm(e,t,r,u>1?a:void 0,d,p,m,h),{inputs:g,outputs:p.kvNumHeads===void 0&&u>1?[-1,1]:[-1]})[0];e.compute(em(e,b,p.batchSize*p.numHeads*p.sequenceLength,w),{inputs:[b],outputs:[]});let x=p.kvNumHeads===void 0&&u>1&&l?[b,n,l]:[b,n];e.compute(rm(e,b,n,u>1&&l?l:void 0,p,h),{inputs:x,outputs:p.kvNumHeads===void 0&&u>1?[0,2]:[0]})},nm=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],n=t.sequenceLength,o=t.inputHiddenSize,i=t.headSize,a=12,l={x:Math.ceil(t.headSize/a),y:Math.ceil(t.sequenceLength/a),z:t.batchSize*t.numHeads},d=[e.inputs[0],e.inputs[1],e.inputs[2]],p=[{type:12,data:n},{type:12,data:o},{type:12,data:i},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],m=u=>{let h=M("output_q",d[0].dataType,r),w=M("output_k",d[0].dataType,r),g=M("output_v",d[0].dataType,r),b=E("input",d[0].dataType,d[0].dims),x=E("weight",d[1].dataType,d[1].dims),_=E("bias",d[2].dataType,d[2].dims),$=b.type.storage,S=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return`
4098
+ }`};return{name:"AttentionScore",shaderCache:{hint:`${n!==void 0};${e.outputCount}`,inputDependencies:y},getRunData:()=>({outputs:x,dispatchGroup:w,programUniforms:g}),getShaderSource:_}},Ut=(e,t,r,n,o,i,a,l,d,p,m)=>{let u=e.outputCount,h=p.kvNumHeads!==void 0||u>1?p.pastSequenceLength:0,w=h+p.kvSequenceLength,g=p.kvNumHeads===void 0&&u>1&&a?[t,r,a]:[t,r];d&&g.push(d);let y=e.compute(Xp(e,t,r,u>1?a:void 0,d,p,m,h),{inputs:g,outputs:p.kvNumHeads===void 0&&u>1?[-1,1]:[-1]})[0];e.compute(Yp(e,y,p.batchSize*p.numHeads*p.sequenceLength,w),{inputs:[y],outputs:[]});let x=p.kvNumHeads===void 0&&u>1&&l?[y,n,l]:[y,n];e.compute(Zp(e,y,n,u>1&&l?l:void 0,p,h),{inputs:x,outputs:p.kvNumHeads===void 0&&u>1?[0,2]:[0]})},Qp=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],n=t.sequenceLength,o=t.inputHiddenSize,i=t.headSize,a=12,l={x:Math.ceil(t.headSize/a),y:Math.ceil(t.sequenceLength/a),z:t.batchSize*t.numHeads},d=[e.inputs[0],e.inputs[1],e.inputs[2]],p=[{type:12,data:n},{type:12,data:o},{type:12,data:i},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],m=u=>{let h=D("output_q",d[0].dataType,r),w=D("output_k",d[0].dataType,r),g=D("output_v",d[0].dataType,r),y=k("input",d[0].dataType,d[0].dims),x=k("weight",d[1].dataType,d[1].dims),_=k("bias",d[2].dataType,d[2].dims),$=y.type.storage,S=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return`
4100
4099
  const TILE_SIZE = ${a}u;
4101
4100
  var<workgroup> tileInput: array<${$}, ${a*a}>;
4102
4101
  var<workgroup> tileWeightQ: array<${$}, ${a*a}>;
4103
4102
  var<workgroup> tileWeightK: array<${$}, ${a*a}>;
4104
4103
  var<workgroup> tileWeightV: array<${$}, ${a*a}>;
4105
- ${u.registerUniforms(S).declareVariables(b,x,_,h,w,g)}
4104
+ ${u.registerUniforms(S).declareVariables(y,x,_,h,w,g)}
4106
4105
  ${u.mainStart([a,a,1])}
4107
4106
  let batchIndex = workgroup_id.z / uniforms.num_heads;
4108
4107
  let headNumber = workgroup_id.z % uniforms.num_heads;
@@ -4151,12 +4150,12 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4151
4150
  output_k[outputIdx] = valueK;
4152
4151
  output_v[outputIdx] = valueV;
4153
4152
  }
4154
- }`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:l,programUniforms:p}),getShaderSource:m},{inputs:d,outputs:[-1,-1,-1]})},Ss=(e,t)=>{let r=Jp(e.inputs,t),[n,o,i]=nm(e,r);return Wt(e,n,o,i,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r,t)}});var om,im,am,Is,Cs=U(()=>{"use strict";Ke();Q();ae();Ce();ie();om=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(n,o,i)=>{let a=o.length;if(a!==n.length)throw new Error(`${i}: num dimensions != ${a}`);o.forEach((l,d)=>{if(l!==n[d])throw new Error(`${i}: dim[${d}] do not match`)})};if(e[0].dims.length>1){let n=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);r(e[1].dims,n,"Invalid input scale"),r(e[2].dims,n,"Invalid input B"),r(e[3].dims,n,"Invalid input mean"),r(e[4].dims,n,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},im=(e,t)=>{let{epsilon:r,spatial:n,format:o}=t,i=e[0].dims,a=n?$e(i[i.length-1]):1,l=o==="NHWC"&&i.length>1?a:1,d=k.size(i)/a,p=n,m=p?i.length:i,u=E("x",e[0].dataType,e[0].dims,a),h=E("scale",e[1].dataType,e[1].dims,l),w=E("bias",e[2].dataType,e[2].dims,l),g=E("inputMean",e[3].dataType,e[3].dims,l),b=E("inputVar",e[4].dataType,e[4].dims,l),x=M("y",e[0].dataType,m,a),_=()=>{let S="";if(n)S=`let cOffset = ${i.length===1?"0u":o==="NHWC"?`outputIndices[${i.length-1}] / ${a}`:"outputIndices[1]"};`;else if(o==="NCHW")S=`
4153
+ }`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:l,programUniforms:p}),getShaderSource:m},{inputs:d,outputs:[-1,-1,-1]})},ys=(e,t)=>{let r=jp(e.inputs,t),[n,o,i]=Qp(e,r);return Ut(e,n,o,i,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r,t)}});var Jp,em,tm,bs,ws=M(()=>{"use strict";qe();X();oe();xe();re();Jp=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(n,o,i)=>{let a=o.length;if(a!==n.length)throw new Error(`${i}: num dimensions != ${a}`);o.forEach((l,d)=>{if(l!==n[d])throw new Error(`${i}: dim[${d}] do not match`)})};if(e[0].dims.length>1){let n=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);r(e[1].dims,n,"Invalid input scale"),r(e[2].dims,n,"Invalid input B"),r(e[3].dims,n,"Invalid input mean"),r(e[4].dims,n,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},em=(e,t)=>{let{epsilon:r,spatial:n,format:o}=t,i=e[0].dims,a=n?he(i[i.length-1]):1,l=o==="NHWC"&&i.length>1?a:1,d=E.size(i)/a,p=n,m=p?i.length:i,u=k("x",e[0].dataType,e[0].dims,a),h=k("scale",e[1].dataType,e[1].dims,l),w=k("bias",e[2].dataType,e[2].dims,l),g=k("inputMean",e[3].dataType,e[3].dims,l),y=k("inputVar",e[4].dataType,e[4].dims,l),x=D("y",e[0].dataType,m,a),_=()=>{let S="";if(n)S=`let cOffset = ${i.length===1?"0u":o==="NHWC"?`outputIndices[${i.length-1}] / ${a}`:"outputIndices[1]"};`;else if(o==="NCHW")S=`
4155
4154
  ${x.indicesSet("outputIndices","0","0")}
4156
4155
  let cOffset = ${x.indicesToOffset("outputIndices")};`;else{S=`var cIndices = ${h.type.indices}(0);
4157
4156
  cIndices[0] = outputIndices[${i.length-1}];`;for(let I=1;I<h.rank;I++)S+=`cIndices[${I}] = outputIndices[${I}];`;S+=`let cOffset = ${h.indicesToOffset("cIndices")};`}return S},$=S=>`
4158
4157
  const epsilon = ${r};
4159
- ${S.registerUniform("outputSize","u32").declareVariables(u,h,w,g,b,x)}
4158
+ ${S.registerUniform("outputSize","u32").declareVariables(u,h,w,g,y,x)}
4160
4159
  ${S.mainStart()}
4161
4160
  ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
4162
4161
  var outputIndices = ${x.offsetToIndices(`global_idx * ${a}`)};
@@ -4164,11 +4163,11 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4164
4163
  let scale = ${h.getByOffset("cOffset")};
4165
4164
  let bias = ${w.getByOffset("cOffset")};
4166
4165
  let inputMean = ${g.getByOffset("cOffset")};
4167
- let inputVar = ${b.getByOffset("cOffset")};
4166
+ let inputVar = ${y.getByOffset("cOffset")};
4168
4167
  let x = ${u.getByOffset("global_idx")};
4169
4168
  let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias;
4170
4169
  ${x.setByOffset("global_idx","value")}
4171
- }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${n}_${a}`,inputDependencies:p?["rank","type","type","type","type"]:void 0},getShaderSource:$,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p?[{type:12,data:d},...V(i)]:[{type:12,data:d}]})}},am=e=>te(e),Is=(e,t)=>{let{inputs:r,outputCount:n}=e,o=am({...t,outputCount:n});if(be.webgpu.validateInputContent&&om(r,o),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(im(r,o))}});var sm,um,Ts,As=U(()=>{"use strict";ae();ie();sm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},um=e=>{let t=e[0].dims,r=e[0].dims[2],n=k.size(t)/4,o=e[0].dataType,i=E("input",o,t,4),a=E("bias",o,[r],4),l=E("residual",o,t,4),d=M("output",o,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)}}),getShaderSource:m=>`
4170
+ }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${n}_${a}`,inputDependencies:p?["rank","type","type","type","type"]:void 0},getShaderSource:$,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p?[{type:12,data:d},...U(i)]:[{type:12,data:d}]})}},tm=e=>Q(e),bs=(e,t)=>{let{inputs:r,outputCount:n}=e,o=tm({...t,outputCount:n});if(fe.webgpu.validateInputContent&&Jp(r,o),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(em(r,o))}});var rm,nm,vs,$s=M(()=>{"use strict";oe();re();rm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},nm=e=>{let t=e[0].dims,r=e[0].dims[2],n=E.size(t)/4,o=e[0].dataType,i=k("input",o,t,4),a=k("bias",o,[r],4),l=k("residual",o,t,4),d=D("output",o,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)}}),getShaderSource:m=>`
4172
4171
  const channels = ${r}u / 4;
4173
4172
  ${m.declareVariables(i,a,l,d)}
4174
4173
 
@@ -4177,7 +4176,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4177
4176
  let value = ${i.getByOffset("global_idx")}
4178
4177
  + ${a.getByOffset("global_idx % channels")} + ${l.getByOffset("global_idx")};
4179
4178
  ${d.setByOffset("global_idx","value")}
4180
- }`}},Ts=e=>{sm(e.inputs),e.compute(um(e.inputs))}});var dm,fe,Es,ks,Ps,Os,Ds,zs,Bs,Rs,Ms,lm,Us,Vs,Ns,Ws,Qt,Hs,Kr,Gs,Ls,Fs,qs,js,Ks,Ys,Xs,Zs,Qs,Js,eu,tu,ru,nu,ou,iu,au,ao,so,su,uu,du,cm,pm,lu,Yr=U(()=>{"use strict";Q();ae();Ce();ie();dm=(e,t,r,n,o,i)=>{let a=Math.ceil(t/4),l="";typeof o=="string"?l=`${o}(a)`:l=o("a");let d=E("inputData",r,[a],4),p=M("outputData",n,[a],4);return`
4179
+ }`}},vs=e=>{rm(e.inputs),e.compute(nm(e.inputs))}});var om,le,_s,xs,Ss,Is,Cs,Ts,As,ks,Es,im,Ps,Os,zs,Bs,Yt,Ds,Fr,Rs,Ms,Us,Vs,Ns,Ws,Ls,Hs,Gs,Fs,qs,Ks,js,Ys,Xs,Zs,Qs,Js,ro,no,eu,tu,ru,am,sm,nu,qr=M(()=>{"use strict";X();oe();xe();re();om=(e,t,r,n,o,i)=>{let a=Math.ceil(t/4),l="";typeof o=="string"?l=`${o}(a)`:l=o("a");let d=k("inputData",r,[a],4),p=D("outputData",n,[a],4);return`
4181
4180
  ${e.registerUniform("vec_size","u32").declareVariables(d,p)}
4182
4181
 
4183
4182
  ${i??""}
@@ -4187,10 +4186,10 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4187
4186
 
4188
4187
  let a = ${d.getByOffset("global_idx")};
4189
4188
  ${p.setByOffset("global_idx",l)}
4190
- }`},fe=(e,t,r,n,o,i=e.dataType)=>({name:t,shaderCache:{hint:o,inputDependencies:["type"]},getShaderSource:a=>dm(a,k.size(e.dims),e.dataType,i,r,n),getRunData:a=>({outputs:[{dims:e.dims,dataType:i}],dispatchGroup:{x:Math.ceil(k.size(a[0].dims)/64/4)},programUniforms:[{type:12,data:Math.ceil(k.size(e.dims)/4)}]})}),Es=e=>{e.compute(fe(e.inputs[0],"Abs","abs"))},ks=e=>{e.compute(fe(e.inputs[0],"Acos","acos"))},Ps=e=>{e.compute(fe(e.inputs[0],"Acosh","acosh"))},Os=e=>{e.compute(fe(e.inputs[0],"Asin","asin"))},Ds=e=>{e.compute(fe(e.inputs[0],"Asinh","asinh"))},zs=e=>{e.compute(fe(e.inputs[0],"Atan","atan"))},Bs=e=>{e.compute(fe(e.inputs[0],"Atanh","atanh"))},Rs=e=>te(e),Ms=(e,t)=>{let r;switch(t.to){case 10:r="vec4<f16>";break;case 1:r="vec4<f32>";break;case 12:r="vec4<u32>";break;case 6:r="vec4<i32>";break;case 9:r="vec4<bool>";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(fe(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},lm=e=>{let t=e.length>=2&&e[1].data!==0?e[1].getFloat32Array()[0]:Hr,r=e.length>=3&&e[2].data!==0?e[2].getFloat32Array()[0]:Gr;return te({min:t,max:r})},Us=(e,t)=>{let r=e.inputs.length===1?t:lm(e.inputs),n=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Clip",o=>`clamp(${o}, clip_min_, clip_max_)`,`
4189
+ }`},le=(e,t,r,n,o,i=e.dataType)=>({name:t,shaderCache:{hint:o,inputDependencies:["type"]},getShaderSource:a=>om(a,E.size(e.dims),e.dataType,i,r,n),getRunData:a=>({outputs:[{dims:e.dims,dataType:i}],dispatchGroup:{x:Math.ceil(E.size(a[0].dims)/64/4)},programUniforms:[{type:12,data:Math.ceil(E.size(e.dims)/4)}]})}),_s=e=>{e.compute(le(e.inputs[0],"Abs","abs"))},xs=e=>{e.compute(le(e.inputs[0],"Acos","acos"))},Ss=e=>{e.compute(le(e.inputs[0],"Acosh","acosh"))},Is=e=>{e.compute(le(e.inputs[0],"Asin","asin"))},Cs=e=>{e.compute(le(e.inputs[0],"Asinh","asinh"))},Ts=e=>{e.compute(le(e.inputs[0],"Atan","atan"))},As=e=>{e.compute(le(e.inputs[0],"Atanh","atanh"))},ks=e=>Q(e),Es=(e,t)=>{let r;switch(t.to){case 10:r="vec4<f16>";break;case 1:r="vec4<f32>";break;case 12:r="vec4<u32>";break;case 6:r="vec4<i32>";break;case 9:r="vec4<bool>";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(le(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},im=e=>{let t=e.length>=2&&e[1].data!==0?e[1].getFloat32Array()[0]:Vr,r=e.length>=3&&e[2].data!==0?e[2].getFloat32Array()[0]:Nr;return Q({min:t,max:r})},Ps=(e,t)=>{let r=e.inputs.length===1?t:im(e.inputs),n=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"Clip",o=>`clamp(${o}, clip_min_, clip_max_)`,`
4191
4190
  const clip_min_: vec4<${n}> = vec4(${n}(${r.min}));
4192
4191
  const clip_max_: vec4<${n}> = vec4(${n}(${r.max}));
4193
- `,r.cacheKey),{inputs:[0]})},Vs=e=>{e.compute(fe(e.inputs[0],"Ceil","ceil"))},Ns=e=>{e.compute(fe(e.inputs[0],"Cos","cos"))},Ws=e=>{e.compute(fe(e.inputs[0],"Cosh","cosh"))},Qt=e=>te(e),Hs=(e,t)=>{let r=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Elu",n=>`elu_vf32(${n})`,`
4192
+ `,r.cacheKey),{inputs:[0]})},Os=e=>{e.compute(le(e.inputs[0],"Ceil","ceil"))},zs=e=>{e.compute(le(e.inputs[0],"Cos","cos"))},Bs=e=>{e.compute(le(e.inputs[0],"Cosh","cosh"))},Yt=e=>Q(e),Ds=(e,t)=>{let r=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"Elu",n=>`elu_vf32(${n})`,`
4194
4193
  const elu_alpha_ = ${r}(${t.alpha});
4195
4194
 
4196
4195
  fn elu_f32(a: ${r}) -> ${r} {
@@ -4199,7 +4198,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
4199
4198
 
4200
4199
  fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> {
4201
4200
  return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w));
4202
- }`,t.cacheKey))},Kr=(e="f32")=>`
4201
+ }`,t.cacheKey))},Fr=(e="f32")=>`
4203
4202
  const r0: ${e} = 0.3275911;
4204
4203
  const r1: ${e} = 0.254829592;
4205
4204
  const r2: ${e} = -0.284496736;
@@ -4211,15 +4210,15 @@ fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> {
4211
4210
  let absv = abs(v);
4212
4211
  let x = 1.0 / (1.0 + r0 * absv);
4213
4212
  return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv));
4214
- }`,Gs=e=>{let t=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,Kr(t)))},Ls=e=>{e.compute(fe(e.inputs[0],"Exp","exp"))},Fs=e=>{e.compute(fe(e.inputs[0],"Floor","floor"))},qs=e=>{let t=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,Kr(t)))},js=(e,t)=>{let r=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"LeakyRelu",n=>`select(leaky_relu_alpha_ * ${n}, ${n}, ${n} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},Ks=e=>{e.compute(fe(e.inputs[0],"Not",t=>`!${t}`))},Ys=e=>{e.compute(fe(e.inputs[0],"Neg",t=>`-${t}`))},Xs=e=>{e.compute(fe(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},Zs=e=>{let t=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},Qs=e=>{e.compute(fe(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},Js=e=>te(e),eu=(e,t)=>{let r=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"HardSigmoid",n=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${n} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},tu=e=>{e.compute(fe(e.inputs[0],"Sin","sin"))},ru=e=>{e.compute(fe(e.inputs[0],"Sinh","sinh"))},nu=e=>{e.compute(fe(e.inputs[0],"Sqrt","sqrt"))},ou=e=>{e.compute(fe(e.inputs[0],"Tan","tan"))},iu=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,au=e=>{e.compute(fe(e.inputs[0],"Tanh",iu))},ao=(e="f32")=>`
4213
+ }`,Rs=e=>{let t=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,Fr(t)))},Ms=e=>{e.compute(le(e.inputs[0],"Exp","exp"))},Us=e=>{e.compute(le(e.inputs[0],"Floor","floor"))},Vs=e=>{let t=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,Fr(t)))},Ns=(e,t)=>{let r=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"LeakyRelu",n=>`select(leaky_relu_alpha_ * ${n}, ${n}, ${n} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},Ws=e=>{e.compute(le(e.inputs[0],"Not",t=>`!${t}`))},Ls=e=>{e.compute(le(e.inputs[0],"Neg",t=>`-${t}`))},Hs=e=>{e.compute(le(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},Gs=e=>{let t=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},Fs=e=>{e.compute(le(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},qs=e=>Q(e),Ks=(e,t)=>{let r=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"HardSigmoid",n=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${n} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},js=e=>{e.compute(le(e.inputs[0],"Sin","sin"))},Ys=e=>{e.compute(le(e.inputs[0],"Sinh","sinh"))},Xs=e=>{e.compute(le(e.inputs[0],"Sqrt","sqrt"))},Zs=e=>{e.compute(le(e.inputs[0],"Tan","tan"))},Qs=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,Js=e=>{e.compute(le(e.inputs[0],"Tanh",Qs))},ro=(e="f32")=>`
4215
4214
  const fast_gelu_a: ${e} = 0.5;
4216
4215
  const fast_gelu_b: ${e} = 0.7978845608028654;
4217
4216
  const fast_gelu_c: ${e} = 0.035677408136300125;
4218
4217
 
4219
4218
  fn tanh_v(v: vec4<${e}>) -> vec4<${e}> {
4220
- return ${iu("v")};
4219
+ return ${Qs("v")};
4221
4220
  }
4222
- `,so=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,su=e=>{let t=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"FastGelu",so,ao(t),void 0,e.inputs[0].dataType))},uu=(e,t)=>{let r=Oe(e.inputs[0].dataType);return e.compute(fe(e.inputs[0],"ThresholdedRelu",n=>`select(vec4<${r}>(0.0), ${n}, ${n} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},du=e=>{e.compute(fe(e.inputs[0],"Log","log"))},cm=(e,t)=>`
4221
+ `,no=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,eu=e=>{let t=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"FastGelu",no,ro(t),void 0,e.inputs[0].dataType))},tu=(e,t)=>{let r=ke(e.inputs[0].dataType);return e.compute(le(e.inputs[0],"ThresholdedRelu",n=>`select(vec4<${r}>(0.0), ${n}, ${n} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},ru=e=>{e.compute(le(e.inputs[0],"Log","log"))},am=(e,t)=>`
4223
4222
  const alpha = vec4<${e}>(${t});
4224
4223
  const one = ${e}(1.0);
4225
4224
  const zero = ${e}(0.0);
@@ -4236,13 +4235,13 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
4236
4235
  }
4237
4236
  return x * x1;
4238
4237
  }
4239
- `,pm=e=>`quick_gelu_impl(${e})`,lu=(e,t)=>{let r=Oe(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"QuickGelu",pm,cm(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}});var mm,fm,pu,mu=U(()=>{"use strict";ae();ie();Yr();mm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},fm=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=E("input",e[0].dataType,e[0].dims,4),n=E("bias",e[0].dataType,[e[0].dims[2]],4),o=M("output",e[0].dataType,t,4),i=k.size(t)/4,a=ge(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:d=>`
4238
+ `,sm=e=>`quick_gelu_impl(${e})`,nu=(e,t)=>{let r=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"QuickGelu",sm,am(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}});var um,dm,iu,au=M(()=>{"use strict";oe();re();qr();um=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},dm=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=k("input",e[0].dataType,e[0].dims,4),n=k("bias",e[0].dataType,[e[0].dims[2]],4),o=D("output",e[0].dataType,t,4),i=E.size(t)/4,a=pe(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:d=>`
4240
4239
  const M_SQRT2 = sqrt(2.0);
4241
4240
  const halfChannels = ${e[0].dims[2]/4/2}u;
4242
4241
 
4243
4242
  ${d.declareVariables(r,n,o)}
4244
4243
 
4245
- ${Kr(a)}
4244
+ ${Fr(a)}
4246
4245
 
4247
4246
  ${d.mainStart()}
4248
4247
  ${d.guardAgainstOutOfBoundsWorkgroupSizes(i)}
@@ -4254,20 +4253,20 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
4254
4253
  let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1);
4255
4254
 
4256
4255
  ${o.setByOffset("global_idx","valueLeft * geluRight")}
4257
- }`}},pu=e=>{mm(e.inputs),e.compute(fm(e.inputs))}});var hm,gm,at,fu,hu,gu,yu,bu,wu,vu,$u,_u,xu,Su=U(()=>{"use strict";Q();ae();ie();hm=(e,t,r,n,o,i,a,l,d,p,m,u)=>{let h,w;typeof l=="string"?h=w=($,S)=>`${l}((${$}),(${S}))`:typeof l=="function"?h=w=l:(h=l.scalar,w=l.vector);let g=M("outputData",m,n.length,4),b=E("aData",d,t.length,4),x=E("bData",p,r.length,4),_;if(o)if(i){let $=k.size(t)===1,S=k.size(r)===1,I=t.length>0&&t[t.length-1]%4===0,T=r.length>0&&r[r.length-1]%4===0;$||S?_=g.setByOffset("global_idx",w($?`${b.type.value}(${b.getByOffset("0")}.x)`:b.getByOffset("global_idx"),S?`${x.type.value}(${x.getByOffset("0")}.x)`:x.getByOffset("global_idx"))):_=`
4256
+ }`}},iu=e=>{um(e.inputs),e.compute(dm(e.inputs))}});var lm,cm,ot,su,uu,du,lu,cu,pu,mu,fu,hu,gu,yu=M(()=>{"use strict";X();oe();re();lm=(e,t,r,n,o,i,a,l,d,p,m,u)=>{let h,w;typeof l=="string"?h=w=($,S)=>`${l}((${$}),(${S}))`:typeof l=="function"?h=w=l:(h=l.scalar,w=l.vector);let g=D("outputData",m,n.length,4),y=k("aData",d,t.length,4),x=k("bData",p,r.length,4),_;if(o)if(i){let $=E.size(t)===1,S=E.size(r)===1,I=t.length>0&&t[t.length-1]%4===0,T=r.length>0&&r[r.length-1]%4===0;$||S?_=g.setByOffset("global_idx",w($?`${y.type.value}(${y.getByOffset("0")}.x)`:y.getByOffset("global_idx"),S?`${x.type.value}(${x.getByOffset("0")}.x)`:x.getByOffset("global_idx"))):_=`
4258
4257
  let outputIndices = ${g.offsetToIndices("global_idx * 4u")};
4259
- let offsetA = ${b.broadcastedIndicesToOffset("outputIndices",g)};
4258
+ let offsetA = ${y.broadcastedIndicesToOffset("outputIndices",g)};
4260
4259
  let offsetB = ${x.broadcastedIndicesToOffset("outputIndices",g)};
4261
- ${g.setByOffset("global_idx",w(a||I?b.getByOffset("offsetA / 4u"):`${b.type.value}(${b.getByOffset("offsetA / 4u")}[offsetA % 4u])`,a||T?x.getByOffset("offsetB / 4u"):`${x.type.value}(${x.getByOffset("offsetB / 4u")}[offsetB % 4u])`))}
4262
- `}else _=g.setByOffset("global_idx",w(b.getByOffset("global_idx"),x.getByOffset("global_idx")));else{if(!i)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let $=(S,I,T="")=>{let A=`aData[indexA${I}][componentA${I}]`,D=`bData[indexB${I}][componentB${I}]`;return`
4260
+ ${g.setByOffset("global_idx",w(a||I?y.getByOffset("offsetA / 4u"):`${y.type.value}(${y.getByOffset("offsetA / 4u")}[offsetA % 4u])`,a||T?x.getByOffset("offsetB / 4u"):`${x.type.value}(${x.getByOffset("offsetB / 4u")}[offsetB % 4u])`))}
4261
+ `}else _=g.setByOffset("global_idx",w(y.getByOffset("global_idx"),x.getByOffset("global_idx")));else{if(!i)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let $=(S,I,T="")=>{let A=`aData[indexA${I}][componentA${I}]`,O=`bData[indexB${I}][componentB${I}]`;return`
4263
4262
  let outputIndices${I} = ${g.offsetToIndices(`global_idx * 4u + ${I}u`)};
4264
- let offsetA${I} = ${b.broadcastedIndicesToOffset(`outputIndices${I}`,g)};
4263
+ let offsetA${I} = ${y.broadcastedIndicesToOffset(`outputIndices${I}`,g)};
4265
4264
  let offsetB${I} = ${x.broadcastedIndicesToOffset(`outputIndices${I}`,g)};
4266
4265
  let indexA${I} = offsetA${I} / 4u;
4267
4266
  let indexB${I} = offsetB${I} / 4u;
4268
4267
  let componentA${I} = offsetA${I} % 4u;
4269
4268
  let componentB${I} = offsetB${I} % 4u;
4270
- ${S}[${I}] = ${T}(${h(A,D)});
4269
+ ${S}[${I}] = ${T}(${h(A,O)});
4271
4270
  `};m===9?_=`
4272
4271
  var data = vec4<u32>(0);
4273
4272
  ${$("data",0,"u32")}
@@ -4280,14 +4279,14 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
4280
4279
  ${$("outputData[global_idx]",2)}
4281
4280
  ${$("outputData[global_idx]",3)}
4282
4281
  `}return`
4283
- ${e.registerUniform("vec_size","u32").declareVariables(b,x,g)}
4282
+ ${e.registerUniform("vec_size","u32").declareVariables(y,x,g)}
4284
4283
 
4285
4284
  ${u??""}
4286
4285
 
4287
4286
  ${e.mainStart()}
4288
4287
  ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
4289
4288
  ${_}
4290
- }`},gm=(e,t,r,n,o,i,a=r.dataType)=>{let l=!k.areEqual(r.dims,n.dims),d=r.dims,p=k.size(r.dims),m=!1,u=!1,h=[l];if(l){let w=tt.calcShape(r.dims,n.dims,!1);if(!w)throw new Error("Can't perform binary op on the given tensors");d=w,p=k.size(d);let g=k.size(r.dims)===1,b=k.size(n.dims)===1,x=r.dims.length>0&&r.dims[r.dims.length-1]%4===0,_=n.dims.length>0&&n.dims[n.dims.length-1]%4===0;h.push(g),h.push(b),h.push(x),h.push(_);let $=1;for(let S=1;S<d.length;S++){let I=r.dims[r.dims.length-S]??1,T=n.dims[n.dims.length-S]??1;if(I===T)$*=I;else break}$%4===0?(u=!0,m=!0):(g||b||x||_)&&(m=!0)}else m=!0;return h.push(m),{name:e,shaderCache:{hint:t+h.map(w=>w.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:w=>hm(w,r.dims,n.dims,d,m,l,u,o,r.dataType,n.dataType,a,i),getRunData:()=>({outputs:[{dims:d,dataType:a}],dispatchGroup:{x:Math.ceil(p/64/4)},programUniforms:[{type:12,data:Math.ceil(k.size(d)/4)},...V(r.dims,n.dims,d)]})}},at=(e,t,r,n,o,i)=>{e.compute(gm(t,o??"",e.inputs[0],e.inputs[1],r,n,i))},fu=e=>{at(e,"Add",(t,r)=>`${t}+${r}`)},hu=e=>{at(e,"Div",(t,r)=>`${t}/${r}`)},gu=e=>{at(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4<u32>(${t}==${r})`},void 0,void 0,9)},yu=e=>{at(e,"Mul",(t,r)=>`${t}*${r}`)},bu=e=>{let t=E("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;at(e,"Pow",{scalar:(n,o)=>`pow_custom(${n},${o})`,vector:(n,o)=>`pow_vector_custom(${n},${o})`},`
4289
+ }`},cm=(e,t,r,n,o,i,a=r.dataType)=>{let l=!E.areEqual(r.dims,n.dims),d=r.dims,p=E.size(r.dims),m=!1,u=!1,h=[l];if(l){let w=Je.calcShape(r.dims,n.dims,!1);if(!w)throw new Error("Can't perform binary op on the given tensors");d=w,p=E.size(d);let g=E.size(r.dims)===1,y=E.size(n.dims)===1,x=r.dims.length>0&&r.dims[r.dims.length-1]%4===0,_=n.dims.length>0&&n.dims[n.dims.length-1]%4===0;h.push(g),h.push(y),h.push(x),h.push(_);let $=1;for(let S=1;S<d.length;S++){let I=r.dims[r.dims.length-S]??1,T=n.dims[n.dims.length-S]??1;if(I===T)$*=I;else break}$%4===0?(u=!0,m=!0):(g||y||x||_)&&(m=!0)}else m=!0;return h.push(m),{name:e,shaderCache:{hint:t+h.map(w=>w.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:w=>lm(w,r.dims,n.dims,d,m,l,u,o,r.dataType,n.dataType,a,i),getRunData:()=>({outputs:[{dims:d,dataType:a}],dispatchGroup:{x:Math.ceil(p/64/4)},programUniforms:[{type:12,data:Math.ceil(E.size(d)/4)},...U(r.dims,n.dims,d)]})}},ot=(e,t,r,n,o,i)=>{e.compute(cm(t,o??"",e.inputs[0],e.inputs[1],r,n,i))},su=e=>{ot(e,"Add",(t,r)=>`${t}+${r}`)},uu=e=>{ot(e,"Div",(t,r)=>`${t}/${r}`)},du=e=>{ot(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4<u32>(${t}==${r})`},void 0,void 0,9)},lu=e=>{ot(e,"Mul",(t,r)=>`${t}*${r}`)},cu=e=>{let t=k("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;ot(e,"Pow",{scalar:(n,o)=>`pow_custom(${n},${o})`,vector:(n,o)=>`pow_vector_custom(${n},${o})`},`
4291
4290
  fn pow_custom(a : ${t}, b : ${t}) -> ${t} {
4292
4291
  if (b == ${t}(0.0)) {
4293
4292
  return ${t}(1.0);
@@ -4300,7 +4299,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
4300
4299
  // TODO: implement vectorized pow
4301
4300
  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));
4302
4301
  }
4303
- `)},wu=e=>{at(e,"Sub",(t,r)=>`${t}-${r}`)},vu=e=>{at(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},$u=e=>{at(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},_u=e=>{at(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},xu=e=>{at(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}});var bm,wm,vm,$m,Iu,Cu,Tu=U(()=>{"use strict";Q();ae();Ce();ie();bm=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,n=e[r],o=n.dataType,i=n.dims.length;e.forEach((a,l)=>{if(l!==r){if(a.dataType!==o)throw new Error("input tensors should be one type");if(a.dims.length!==i)throw new Error("input tensors should have the same shape");a.dims.forEach((d,p)=>{if(p!==t&&d!==n.dims[p])throw new Error("non concat dimensions must match")})}})},wm=(e,t)=>`
4302
+ `)},pu=e=>{ot(e,"Sub",(t,r)=>`${t}-${r}`)},mu=e=>{ot(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},fu=e=>{ot(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},hu=e=>{ot(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},gu=e=>{ot(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}});var mm,fm,hm,gm,bu,wu,vu=M(()=>{"use strict";X();oe();xe();re();mm=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,n=e[r],o=n.dataType,i=n.dims.length;e.forEach((a,l)=>{if(l!==r){if(a.dataType!==o)throw new Error("input tensors should be one type");if(a.dims.length!==i)throw new Error("input tensors should have the same shape");a.dims.forEach((d,p)=>{if(p!==t&&d!==n.dims[p])throw new Error("non concat dimensions must match")})}})},fm=(e,t)=>`
4304
4303
  fn calculateInputIndex(index: u32) -> u32 {
4305
4304
  let sizeInConcatAxis = array<u32, ${e}u>(${t});
4306
4305
  for (var i: u32 = 0u; i < ${e}; i += 1u ) {
@@ -4309,15 +4308,15 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
4309
4308
  }
4310
4309
  }
4311
4310
  return ${e}u;
4312
- }`,vm=(e,t)=>{let r=e.length,n=[];for(let o=0;o<r;++o){let i=t.setByOffset("global_idx",e[o].getByIndices("indices"));r===1?n.push(i):o===0?n.push(`if (inputIndex == ${o}u) { ${i} }`):o===r-1?n.push(`else { ${i} }`):n.push(`else if (inputIndex == ${o}) { ${i} }`)}return n.join(`
4313
- `)},$m=(e,t,r,n)=>{let o=k.size(r),i=new Array(e.length),a=new Array(e.length),l=0,d=[],p=[],m=[{type:12,data:o}];for(let b=0;b<e.length;++b)l+=e[b].dims[t],i[b]=l,p.push(e[b].dims.length),a[b]=E(`input${b}`,n,p[b]),d.push("rank"),m.push({type:12,data:i[b]});for(let b=0;b<e.length;++b)m.push(...V(e[b].dims));m.push(...V(r));let u=M("output",n,r.length),h=u.indicesGet("indices",t),w=Array.from(Array(i.length).keys()).map(b=>`uniforms.sizeInConcatAxis${b}`).join(","),g=b=>`
4311
+ }`,hm=(e,t)=>{let r=e.length,n=[];for(let o=0;o<r;++o){let i=t.setByOffset("global_idx",e[o].getByIndices("indices"));r===1?n.push(i):o===0?n.push(`if (inputIndex == ${o}u) { ${i} }`):o===r-1?n.push(`else { ${i} }`):n.push(`else if (inputIndex == ${o}) { ${i} }`)}return n.join(`
4312
+ `)},gm=(e,t,r,n)=>{let o=E.size(r),i=new Array(e.length),a=new Array(e.length),l=0,d=[],p=[],m=[{type:12,data:o}];for(let y=0;y<e.length;++y)l+=e[y].dims[t],i[y]=l,p.push(e[y].dims.length),a[y]=k(`input${y}`,n,p[y]),d.push("rank"),m.push({type:12,data:i[y]});for(let y=0;y<e.length;++y)m.push(...U(e[y].dims));m.push(...U(r));let u=D("output",n,r.length),h=u.indicesGet("indices",t),w=Array.from(Array(i.length).keys()).map(y=>`uniforms.sizeInConcatAxis${y}`).join(","),g=y=>`
4314
4313
 
4315
- ${(()=>{b.registerUniform("outputSize","u32");for(let x=0;x<e.length;x++)b.registerUniform(`sizeInConcatAxis${x}`,"u32");return b.declareVariables(...a,u)})()}
4314
+ ${(()=>{y.registerUniform("outputSize","u32");for(let x=0;x<e.length;x++)y.registerUniform(`sizeInConcatAxis${x}`,"u32");return y.declareVariables(...a,u)})()}
4316
4315
 
4317
- ${wm(i.length,w)}
4316
+ ${fm(i.length,w)}
4318
4317
 
4319
- ${b.mainStart()}
4320
- ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
4318
+ ${y.mainStart()}
4319
+ ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
4321
4320
 
4322
4321
  var indices = ${u.offsetToIndices("global_idx")};
4323
4322
 
@@ -4327,12 +4326,12 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
4327
4326
  ${h} -= sizeInConcatAxis[inputIndex - 1u];
4328
4327
  }
4329
4328
 
4330
- ${vm(a,u)}
4331
- }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:r,dataType:n}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:m}),getShaderSource:g}},Iu=(e,t)=>{let r=e.inputs,n=r[0].dims,o=k.normalizeAxis(t.axis,n.length);bm(r,o);let i=n.slice();i[o]=r.reduce((l,d)=>l+(d.dims.length>o?d.dims[o]:0),0);let a=r.filter(l=>k.size(l.dims)>0);e.compute($m(a,o,i,r[0].dataType),{inputs:a})},Cu=e=>te({axis:e.axis})});var He,Ge,Le,Xr,lt=U(()=>{"use strict";Q();ae();He=(e,t,r="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}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value));
4329
+ ${hm(a,u)}
4330
+ }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:r,dataType:n}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:m}),getShaderSource:g}},bu=(e,t)=>{let r=e.inputs,n=r[0].dims,o=E.normalizeAxis(t.axis,n.length);mm(r,o);let i=n.slice();i[o]=r.reduce((l,d)=>l+(d.dims.length>o?d.dims[o]:0),0);let a=r.filter(l=>E.size(l.dims)>0);e.compute(gm(a,o,i,r[0].dataType),{inputs:a})},wu=e=>Q({axis:e.axis})});var Ne,We,Le,Kr,ut=M(()=>{"use strict";X();oe();Ne=(e,t,r="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}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value));
4332
4331
  value = sign(value) * (1.0 - e2x) / (1.0 + e2x);
4333
- `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},Ge=(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})},Le=(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"})},Xr=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,n]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:n}}else if(t==="Clip"){let[r,n]=e?.activation_params||[Hr,Gr];return{activation:t,clipMax:n,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}});var Ee,Zr,Jt=U(()=>{"use strict";Ee=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},Zr=e=>`
4332
+ `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},We=(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})},Le=(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"})},Kr=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,n]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:n}}else if(t==="Clip"){let[r,n]=e?.activation_params||[Vr,Nr];return{activation:t,clipMax:n,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}});var Ce,jr,Xt=M(()=>{"use strict";Ce=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},jr=e=>`
4334
4333
  ${e?"value = value + getBiasByOutputCoords(coords);":""}
4335
- `});var Qr,uo=U(()=>{"use strict";Qr=e=>`
4334
+ `});var Yr,oo=M(()=>{"use strict";Yr=e=>`
4336
4335
  fn getIndexFromCoords4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 {
4337
4336
  return dot(coords, vec4<i32>(
4338
4337
  shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));
@@ -4341,7 +4340,7 @@ fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
4341
4340
  return dot(coords, vec4<i32>(
4342
4341
  i32(${e}.x), i32(${e}.y), i32(${e}.z), 1));
4343
4342
  }
4344
- `});var _m,xm,er,Au,Sm,tr,Im,Jr,rr=U(()=>{"use strict";Q();ae();ie();lt();Jt();_m=(e,t)=>e?`
4343
+ `});var ym,bm,Zt,$u,wm,Qt,vm,Xr,Jt=M(()=>{"use strict";X();oe();re();ut();Xt();ym=(e,t)=>e?`
4345
4344
  mm_Asub[inputRow][inputCol] = mm_readA(batch,
4346
4345
  kStart + inputRow,
4347
4346
  globalRowStart / innerElementSize + inputCol${t?", batchIndices":""});
@@ -4349,7 +4348,7 @@ fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
4349
4348
  mm_Asub[inputRow][inputCol] = mm_readA(batch,
4350
4349
  globalRow + innerRow,
4351
4350
  kStart / innerElementSize + inputCol${t?", batchIndices":""});
4352
- `,xm=(e,t)=>e?`
4351
+ `,bm=(e,t)=>e?`
4353
4352
  let ACached0 = mm_Asub[k * innerElementSize][localRow];
4354
4353
  let ACached1 = mm_Asub[k * innerElementSize + 1][localRow];
4355
4354
  let ACached2 = mm_Asub[k * innerElementSize + 2][localRow];
@@ -4366,7 +4365,7 @@ fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
4366
4365
  acc[i] = BCached1 * ACached.y + acc[i];
4367
4366
  acc[i] = BCached2 * ACached.z + acc[i];
4368
4367
  ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"}
4369
- }`,er=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32)=>{let d=t[1]*e[1],p=t[0]*e[0],m=o?d:i,u=o?i:d,h=m/t[0],w=i/t[1];if(!((o&&h===4&&e[1]===4||!o&&(h===3||h===4))&&m%t[0]===0&&i%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${o} is true, innerElementSize ${h} and workPerThread[1] ${e[1]} must be 4.
4368
+ }`,Zt=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32)=>{let d=t[1]*e[1],p=t[0]*e[0],m=o?d:i,u=o?i:d,h=m/t[0],w=i/t[1];if(!((o&&h===4&&e[1]===4||!o&&(h===3||h===4))&&m%t[0]===0&&i%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${o} is true, innerElementSize ${h} and workPerThread[1] ${e[1]} must be 4.
4370
4369
  Otherwise, innerElementSize ${h} must be 3 or 4.
4371
4370
  tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}. tileInner ${i} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return`
4372
4371
  var<workgroup> mm_Asub: array<array<vec${h}<${r}>, ${m/h}>, ${u}>;
@@ -4403,7 +4402,7 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
4403
4402
  for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
4404
4403
  let inputRow = tileRow + innerRow;
4405
4404
  let inputCol = tileCol;
4406
- ${_m(o,n)}
4405
+ ${ym(o,n)}
4407
4406
  }
4408
4407
 
4409
4408
  // Load one tile of B into local memory.
@@ -4422,7 +4421,7 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
4422
4421
  let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol];
4423
4422
  ${h===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"}
4424
4423
 
4425
- ${xm(o,h)}
4424
+ ${bm(o,h)}
4426
4425
  }
4427
4426
 
4428
4427
  workgroupBarrier();
@@ -4431,7 +4430,7 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
4431
4430
  for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
4432
4431
  mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]);
4433
4432
  }
4434
- }`},Au=(e,t)=>e?`
4433
+ }`},$u=(e,t)=>e?`
4435
4434
  mm_Asub[inputRow][inputCol] = mm_readA(batch,
4436
4435
  kStart + inputRow,
4437
4436
  globalRowStart + inputCol${t?", batchIndices":""});
@@ -4439,7 +4438,7 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
4439
4438
  mm_Asub[inputRow][inputCol] = mm_readA(batch,
4440
4439
  globalRowStart + inputRow,
4441
4440
  kStart + inputCol${t?", batchIndices":""});
4442
- `,Sm=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",tr=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32,d=!1)=>{let p=e[1]*t[1],m=e[0]*t[0],u=o?p:i,h=o?i:p;if(!(h%t[1]===0&&u%t[0]===0&&i%t[1]===0))throw new Error(`tileAHight ${h} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${u} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`);let w=h/t[1],g=u/t[0],b=i/t[1],x=d?`
4441
+ `,wm=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",Qt=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32,d=!1)=>{let p=e[1]*t[1],m=e[0]*t[0],u=o?p:i,h=o?i:p;if(!(h%t[1]===0&&u%t[0]===0&&i%t[1]===0))throw new Error(`tileAHight ${h} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${u} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`);let w=h/t[1],g=u/t[0],y=i/t[1],x=d?`
4443
4442
  let localRow = i32(localId.y);
4444
4443
  let localCol = i32(localId.x);
4445
4444
  let globalRowStart = i32(workgroupId.y) * ${p};
@@ -4450,7 +4449,7 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
4450
4449
  // Load one tile of A into local memory.
4451
4450
  for (var inputRow = localRow; inputRow < ${h}; inputRow = inputRow + ${t[1]}) {
4452
4451
  for (var inputCol = localCol; inputCol < ${u}; inputCol = inputCol + ${t[0]}) {
4453
- ${Au(o,n)}
4452
+ ${$u(o,n)}
4454
4453
  }
4455
4454
  }
4456
4455
  // Load one tile of B into local memory.
@@ -4497,7 +4496,7 @@ let globalRowStart = i32(workgroupId.y) * ${p};
4497
4496
 
4498
4497
  let tileRowA = i32(localId.y) * ${w};
4499
4498
  let tileColA = i32(localId.x) * ${g};
4500
- let tileRowB = i32(localId.y) * ${b};
4499
+ let tileRowB = i32(localId.y) * ${y};
4501
4500
  // Loop over shared dimension.
4502
4501
  for (var t = 0; t < num_tiles; t = t + 1) {
4503
4502
  // Load one tile of A into local memory.
@@ -4505,12 +4504,12 @@ for (var t = 0; t < num_tiles; t = t + 1) {
4505
4504
  for (var innerCol = 0; innerCol < ${g}; innerCol = innerCol + 1) {
4506
4505
  let inputRow = tileRowA + innerRow;
4507
4506
  let inputCol = tileColA + innerCol;
4508
- ${Au(o,n)}
4507
+ ${$u(o,n)}
4509
4508
  }
4510
4509
  }
4511
4510
 
4512
4511
  // Load one tile of B into local memory.
4513
- for (var innerRow = 0; innerRow < ${b}; innerRow = innerRow + 1) {
4512
+ for (var innerRow = 0; innerRow < ${y}; innerRow = innerRow + 1) {
4514
4513
  for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
4515
4514
  let inputRow = tileRowB + innerRow;
4516
4515
  let inputCol = tileCol + innerCol;
@@ -4530,7 +4529,7 @@ for (var t = 0; t < num_tiles; t = t + 1) {
4530
4529
  }
4531
4530
 
4532
4531
  for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
4533
- ${Sm(o)}
4532
+ ${wm(o)}
4534
4533
  for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
4535
4534
  acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];
4536
4535
  }
@@ -4565,17 +4564,17 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
4565
4564
  var acc : array<array<${r}, colPerThread>, rowPerThread>;
4566
4565
  ${x}
4567
4566
  }
4568
- `},Im=(e,t,r,n,o,i=!1)=>{let[a,l,d]=o,[p,m,u,h]=n,w=Nt(a,d),g=Nt(l,d),b=ge(n[0].type.tensor),x=()=>{let S=m.rank,I=p.rank,T=`var aIndices: ${m.type.indices};`;for(let A=S-2-1,D=I-1;A>=0;A--,D--)T+=`
4569
- aIndices[${A}] = ${I>1?`batchIndices[${D}]`:"batchIndices"};`;return w.forEach(A=>{T+=`
4567
+ `},vm=(e,t,r,n,o,i=!1)=>{let[a,l,d]=o,[p,m,u,h]=n,w=Mt(a,d),g=Mt(l,d),y=pe(n[0].type.tensor),x=()=>{let S=m.rank,I=p.rank,T=`var aIndices: ${m.type.indices};`;for(let A=S-2-1,O=I-1;A>=0;A--,O--)T+=`
4568
+ aIndices[${A}] = ${I>1?`batchIndices[${O}]`:"batchIndices"};`;return w.forEach(A=>{T+=`
4570
4569
  aIndices[${A}] = 0;`}),T+=`
4571
4570
  aIndices[${S-2}] = u32(row);
4572
- aIndices[${S-1}] = u32(colIn);`,T},_=()=>{let S=u.rank,I=p.rank,T=`var bIndices: ${u.type.indices};`;for(let A=S-2-1,D=I-1;A>=0;A--,D--)T+=`
4573
- bIndices[${A}] = ${I>1?`batchIndices[${D}]`:"batchIndices"};`;return g.forEach(A=>{T+=`
4571
+ aIndices[${S-1}] = u32(colIn);`,T},_=()=>{let S=u.rank,I=p.rank,T=`var bIndices: ${u.type.indices};`;for(let A=S-2-1,O=I-1;A>=0;A--,O--)T+=`
4572
+ bIndices[${A}] = ${I>1?`batchIndices[${O}]`:"batchIndices"};`;return g.forEach(A=>{T+=`
4574
4573
  bIndices[${A}] = 0;`}),T+=`
4575
4574
  bIndices[${S-2}] = u32(row);
4576
4575
  bIndices[${S-1}] = u32(colIn);`,T};return`
4577
- fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${p.type.indices}) -> ${Ee(e,b)} {
4578
- var value = ${Ee(e,b)}(0.0);
4576
+ fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${p.type.indices}) -> ${Ce(e,y)} {
4577
+ var value = ${Ce(e,y)}(0.0);
4579
4578
  let col = colIn * ${e};
4580
4579
  if(row < uniforms.dim_a_outer && col < uniforms.dim_inner)
4581
4580
  {
@@ -4585,8 +4584,8 @@ bIndices[${S-2}] = u32(row);
4585
4584
  return value;
4586
4585
  }
4587
4586
 
4588
- fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${p.type.indices}) -> ${Ee(e,b)} {
4589
- var value = ${Ee(e,b)}(0.0);
4587
+ fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${p.type.indices}) -> ${Ce(e,y)} {
4588
+ var value = ${Ce(e,y)}(0.0);
4590
4589
  let col = colIn * ${e};
4591
4590
  if(row < uniforms.dim_inner && col < uniforms.dim_b_outer)
4592
4591
  {
@@ -4596,21 +4595,21 @@ bIndices[${S-2}] = u32(row);
4596
4595
  return value;
4597
4596
  }
4598
4597
 
4599
- fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Ee(e,b)}) {
4598
+ fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Ce(e,y)}) {
4600
4599
  let col = colIn * ${e};
4601
4600
  if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) {
4602
4601
  var value = valueIn;
4603
4602
  let coords = vec3<i32>(batch, row, colIn);
4604
- ${t?`value = value + ${i?"bias[colIn]":`${Ee(e,b)}(bias[row])`};`:""}
4603
+ ${t?`value = value + ${i?"bias[colIn]":`${Ce(e,y)}(bias[row])`};`:""}
4605
4604
  ${r}
4606
4605
  ${h.setByIndices("vec3<u32>(coords)","value")}
4607
4606
  }
4608
4607
  }
4609
- `},Jr=(e,t,r,n,o=!1)=>{let i=e[0].dims,a=e[1].dims,l=i.slice(0,-2),d=a.slice(0,-2),p=n?n.slice(0,-2):r.slice(0,-2),m=k.size(p),u=i[i.length-2],h=i[i.length-1],w=a[a.length-1],g=h%4===0&&w%4===0,b=u<=8?[4,1,1]:[4,4,1],x=[8,8,1],_=[Math.ceil(w/x[0]/b[0]),Math.ceil(u/x[1]/b[1]),Math.ceil(m/x[2]/b[2])],$=g?4:1,S=[...l,u,h/$],I=S.length,T=[...d,h,w/$],A=T.length,D=[m,u,w/$],z=[{type:6,data:u},{type:6,data:w},{type:6,data:h}];Ge(t,z),z.push(...V(p,S,T));let H=["rank","rank"],W=e.length>2;W&&(z.push(...V(e[2].dims)),H.push("rank")),z.push(...V(D));let X=Y=>{let pe=p.length,ue=Lr("batchDims",e[0].dataType,pe,1),le=ge(e[0].dataType),K=E("a",e[0].dataType,I,$),de=E("b",e[1].dataType,A,$),re=M("result",e[0].dataType,D.length,$),se=[K,de];if(W){let R=o?$:1;se.push(E("bias",e[2].dataType,e[2].dims.length,R))}let Z=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];Le(t,Z);let ne=ge(re.type.tensor),J=He(t,re.type.value,ne),ke=Im($,W,J,[ue,K,de,re],[l,d,p],o);return`
4610
- ${Y.registerUniforms(Z).registerInternalVariables(ue).declareVariables(...se,re)}
4611
- ${ke}
4612
- ${g?er(b,x,le,ue):tr(b,x,le,ue)}
4613
- `};return{name:"MatMul",shaderCache:{hint:`${b};${t.activation};${g};${o}`,inputDependencies:H},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:_[0],y:_[1],z:_[2]},programUniforms:z}),getShaderSource:X}}});var Cm,Eu,ku=U(()=>{"use strict";Q();dt();ie();lt();Jt();uo();rr();Cm=(e,t,r,n,o=!1,i,a=4,l=4,d=4,p="f32")=>{let m=W=>{switch(W){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${p}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${W} is not supported.`)}},u=W=>{switch(W){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${W} is not supported.`)}},h=e?`
4608
+ `},Xr=(e,t,r,n,o=!1)=>{let i=e[0].dims,a=e[1].dims,l=i.slice(0,-2),d=a.slice(0,-2),p=n?n.slice(0,-2):r.slice(0,-2),m=E.size(p),u=i[i.length-2],h=i[i.length-1],w=a[a.length-1],g=h%4===0&&w%4===0,y=u<=8?[4,1,1]:[4,4,1],x=[8,8,1],_=[Math.ceil(w/x[0]/y[0]),Math.ceil(u/x[1]/y[1]),Math.ceil(m/x[2]/y[2])],$=g?4:1,S=[...l,u,h/$],I=S.length,T=[...d,h,w/$],A=T.length,O=[m,u,w/$],z=[{type:6,data:u},{type:6,data:w},{type:6,data:h}];We(t,z),z.push(...U(p,S,T));let W=["rank","rank"],V=e.length>2;V&&(z.push(...U(e[2].dims)),W.push("rank")),z.push(...U(O));let j=Y=>{let ae=p.length,se=Wr("batchDims",e[0].dataType,ae,1),ie=pe(e[0].dataType),q=k("a",e[0].dataType,I,$),ce=k("b",e[1].dataType,A,$),ne=D("result",e[0].dataType,O.length,$),Z=[q,ce];if(V){let R=o?$:1;Z.push(k("bias",e[2].dataType,e[2].dims.length,R))}let ee=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];Le(t,ee);let K=pe(ne.type.tensor),me=Ne(t,ne.type.value,K),$e=vm($,V,me,[se,q,ce,ne],[l,d,p],o);return`
4609
+ ${Y.registerUniforms(ee).registerInternalVariables(se).declareVariables(...Z,ne)}
4610
+ ${$e}
4611
+ ${g?Zt(y,x,ie,se):Qt(y,x,ie,se)}
4612
+ `};return{name:"MatMul",shaderCache:{hint:`${y};${t.activation};${g};${o}`,inputDependencies:W},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:_[0],y:_[1],z:_[2]},programUniforms:z}),getShaderSource:j}}});var $m,_u,xu=M(()=>{"use strict";X();st();re();ut();Xt();oo();Jt();$m=(e,t,r,n,o=!1,i,a=4,l=4,d=4,p="f32")=>{let m=V=>{switch(V){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${p}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${V} is not supported.`)}},u=V=>{switch(V){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${V} is not supported.`)}},h=e?`
4614
4613
  let coord = vec4<i32>(batch, xRow, xCol, xCh);
4615
4614
  `:`
4616
4615
  let coord = vec4<i32>(batch, xCh, xRow, xCol);
@@ -4626,7 +4625,7 @@ bIndices[${S-2}] = u32(row);
4626
4625
  row,
4627
4626
  col / outWidth,
4628
4627
  col % outWidth);
4629
- `,g=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",b=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",x=e?"row":"col",_=e?"col":"row",$=`
4628
+ `,g=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",y=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",x=e?"row":"col",_=e?"col":"row",$=`
4630
4629
  let inChannels = i32(uniforms.w_shape[2]);
4631
4630
  let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
4632
4631
  let outRow = ${x} / outWidth;
@@ -4637,10 +4636,10 @@ bIndices[${S-2}] = u32(row);
4637
4636
  let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];
4638
4637
  let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];
4639
4638
  let xCh = ${_} % inChannels;
4640
- var resData = ${Ee(a,p)}(0.0);
4639
+ var resData = ${Ce(a,p)}(0.0);
4641
4640
  // The bounds checking is always needed since we use it to pad zero for
4642
4641
  // the 'same' padding type.
4643
- if (xRow >= 0 && xRow < ${g} && xCol >= 0 && xCol < ${b}) {
4642
+ if (xRow >= 0 && xRow < ${g} && xCol >= 0 && xCol < ${y}) {
4644
4643
  ${h}
4645
4644
  let xIndex = getIndexFromCoords4D(coord, vec4<i32>(uniforms.x_shape));
4646
4645
  ${m(a)}
@@ -4652,19 +4651,19 @@ bIndices[${S-2}] = u32(row);
4652
4651
  if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {
4653
4652
  ${$}
4654
4653
  }
4655
- return ${Ee(a,p)}(0.0);`:n&&r?`
4654
+ return ${Ce(a,p)}(0.0);`:n&&r?`
4656
4655
  let col = colIn * ${a};
4657
4656
  ${$}`:`
4658
4657
  let col = colIn * ${a};
4659
4658
  if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
4660
4659
  ${$}
4661
4660
  }
4662
- return ${Ee(a,p)}(0.0);`,I=`${u(l)}`,T=Ee(d,p),A=e?Ee(a,p):Ee(l,p),D=e?Ee(l,p):Ee(a,p),z=He(i,T,p);return`
4661
+ return ${Ce(a,p)}(0.0);`,I=`${u(l)}`,T=Ce(d,p),A=e?Ce(a,p):Ce(l,p),O=e?Ce(l,p):Ce(a,p),z=Ne(i,T,p);return`
4663
4662
  fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${A} {
4664
4663
  ${e?S:I}
4665
4664
  }
4666
4665
 
4667
- fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${D} {
4666
+ fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${O} {
4668
4667
  ${e?I:S}
4669
4668
  }
4670
4669
 
@@ -4675,57 +4674,57 @@ bIndices[${S-2}] = u32(row);
4675
4674
  var value = valueIn;
4676
4675
  let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
4677
4676
  ${w}
4678
- ${Zr(o)}
4677
+ ${jr(o)}
4679
4678
  ${z}
4680
4679
  setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);
4681
4680
  }
4682
- }`},Eu=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",p=d?e[0].dims[3]:e[0].dims[1],m=r[0],u=d?r[2]:r[3],h=d?r[1]:r[2],w=d?r[3]:r[1],g=d&&(p%4===0||p%3===0)&&w%4===0,b=d?w:u*h,x=d?u*h:w,_=[8,8,1],$=n<=8?[4,1,1]:[4,4,1],S=[Math.ceil(b/_[0]/$[0]),Math.ceil(x/_[1]/$[1]),Math.ceil(m/_[2]/$[2])];ve("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${S}`);let I=g?d&&p%4!==0?3:4:1,T=_[1]*$[1],A=_[0]*$[0],D=Math.max(_[0]*I,_[1]),z=n%T===0,H=o%A===0,W=i%D===0,X=g?[I,4,4]:[1,1,1],Y=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];Ge(t,Y),Y.push(...V(e[0].dims,e[1].dims));let pe=["rank","rank"];a&&(Y.push(...V(e[2].dims)),pe.push("rank")),Y.push(...V(r));let ue=le=>{let K=[{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}];Le(t,K);let de=g?4:1,re=ge(e[0].dataType),se=`
4683
- fn setOutputAtIndex(flatIndex : i32, value : ${g?`vec4<${re}>`:re}) {
4684
- result[flatIndex] = ${g?`vec4<${re}>`:re}(value);
4681
+ }`},_u=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",p=d?e[0].dims[3]:e[0].dims[1],m=r[0],u=d?r[2]:r[3],h=d?r[1]:r[2],w=d?r[3]:r[1],g=d&&(p%4===0||p%3===0)&&w%4===0,y=d?w:u*h,x=d?u*h:w,_=[8,8,1],$=n<=8?[4,1,1]:[4,4,1],S=[Math.ceil(y/_[0]/$[0]),Math.ceil(x/_[1]/$[1]),Math.ceil(m/_[2]/$[2])];be("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${S}`);let I=g?d&&p%4!==0?3:4:1,T=_[1]*$[1],A=_[0]*$[0],O=Math.max(_[0]*I,_[1]),z=n%T===0,W=o%A===0,V=i%O===0,j=g?[I,4,4]:[1,1,1],Y=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];We(t,Y),Y.push(...U(e[0].dims,e[1].dims));let ae=["rank","rank"];a&&(Y.push(...U(e[2].dims)),ae.push("rank")),Y.push(...U(r));let se=ie=>{let q=[{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}];Le(t,q);let ce=g?4:1,ne=pe(e[0].dataType),Z=`
4682
+ fn setOutputAtIndex(flatIndex : i32, value : ${g?`vec4<${ne}>`:ne}) {
4683
+ result[flatIndex] = ${g?`vec4<${ne}>`:ne}(value);
4685
4684
  }
4686
- fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${g?`vec4<${re}>`:re}) {
4685
+ fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${g?`vec4<${ne}>`:ne}) {
4687
4686
  let flatIndex = getOutputIndexFromCoords(vec4<i32>(d0, d1, d2, d3));
4688
4687
  setOutputAtIndex(flatIndex ${g?"/ 4":""}, value);
4689
- }`,Z=E("x",e[0].dataType,e[0].dims.length,I===3?1:I),ne=E("w",e[1].dataType,e[1].dims.length,de),J=[Z,ne],ke=M("result",e[0].dataType,r.length,de);if(a){let R=E("bias",e[2].dataType,e[2].dims.length,de);J.push(R),se+=`
4690
- fn getBiasByOutputCoords(coords : vec4<i32>) -> ${g?`vec4<${re}>`:re} {
4688
+ }`,ee=k("x",e[0].dataType,e[0].dims.length,I===3?1:I),K=k("w",e[1].dataType,e[1].dims.length,ce),me=[ee,K],$e=D("result",e[0].dataType,r.length,ce);if(a){let R=k("bias",e[2].dataType,e[2].dims.length,ce);me.push(R),Z+=`
4689
+ fn getBiasByOutputCoords(coords : vec4<i32>) -> ${g?`vec4<${ne}>`:ne} {
4691
4690
  return bias[coords.${d?"w":"y"}${g?"/ 4":""}];
4692
4691
  }`}return`
4693
- ${Qr("uniforms.result_strides")}
4692
+ ${Yr("uniforms.result_strides")}
4694
4693
  //struct Uniforms { xShape : vec4<i32>, wShape : vec4<i32>, outShape : vec4<i32>,
4695
4694
  // outShapeStrides: vec3<i32>, filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>,
4696
4695
  // dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32 };
4697
- ${le.registerUniforms(K).declareVariables(...J,ke)}
4698
- ${se}
4699
- ${Cm(d,z,H,W,a,t,X[0],X[1],X[2],re)}
4700
- ${g?er($,_,re,void 0,!d,D):tr($,_,re,void 0,!d,D,!1,void 0,l)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${I};${g};${z};${H};${W};${T};${A};${D}`,inputDependencies:pe},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:Y}),getShaderSource:ue}}});var Tm,Pu,en,Am,Ou,Em,Du,zu,Bu=U(()=>{"use strict";Q();dt();ae();ie();lt();Jt();Tm=e=>{let t=1;for(let r=0;r<e.length;r++)t*=e[r];return t},Pu=e=>typeof e=="number"?[e,e,e]:e,en=(e,t)=>t<=1?e:e+(e-1)*(t-1),Am=(e,t,r,n=1)=>{let o=en(t,n);return Math.floor((e[0]*(r-1)-r+o)/2)},Ou=(e,t,r,n,o)=>{o==null&&(o=Am(e,t[0],n[0]));let i=[0,0,0,r];for(let a=0;a<3;a++)e[a]+2*o>=t[a]&&(i[a]=Math.trunc((e[a]-t[a]+2*o)/n[a]+1));return i},Em=(e,t,r,n,o,i,a,l,d,p)=>{let m,u,h,w;if(e==="VALID"&&(e=0),typeof e=="number"){m={top:e,bottom:e,left:e,right:e,front:e,back:e};let g=Ou([t,r,n,1],[l,d,p],1,[o,i,a],e);u=g[0],h=g[1],w=g[2]}else if(Array.isArray(e)){if(!e.every((b,x,_)=>b===_[0]))throw Error(`Unsupported padding parameter: ${e}`);m={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let g=Ou([t,r,n,1],[l,d,p],1,[o,i,a],e[0]);u=g[0],h=g[1],w=g[2]}else if(e==="SAME_UPPER"){u=Math.ceil(t/o),h=Math.ceil(r/i),w=Math.ceil(n/a);let g=(u-1)*o+l-t,b=(h-1)*i+d-r,x=(w-1)*a+p-n,_=Math.floor(g/2),$=g-_,S=Math.floor(b/2),I=b-S,T=Math.floor(x/2),A=x-T;m={top:S,bottom:I,left:T,right:A,front:_,back:$}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:m,outDepth:u,outHeight:h,outWidth:w}},Du=(e,t,r,n,o,i=!1,a="channelsLast")=>{let l,d,p,m,u;if(a==="channelsLast")[l,d,p,m,u]=e;else if(a==="channelsFirst")[l,u,d,p,m]=e;else throw new Error(`Unknown dataFormat ${a}`);let[h,,w,g,b]=t,[x,_,$]=Pu(r),[S,I,T]=Pu(n),A=en(w,S),D=en(g,I),z=en(b,T),{padInfo:H,outDepth:W,outHeight:X,outWidth:Y}=Em(o,d,p,m,x,_,$,A,D,z),pe=i?h*u:h,ue=[0,0,0,0,0];return a==="channelsFirst"?ue=[l,pe,W,X,Y]:a==="channelsLast"&&(ue=[l,W,X,Y,pe]),{batchSize:l,dataFormat:a,inDepth:d,inHeight:p,inWidth:m,inChannels:u,outDepth:W,outHeight:X,outWidth:Y,outChannels:pe,padInfo:H,strideDepth:x,strideHeight:_,strideWidth:$,filterDepth:w,filterHeight:g,filterWidth:b,effectiveFilterDepth:A,effectiveFilterHeight:D,effectiveFilterWidth:z,dilationDepth:S,dilationHeight:I,dilationWidth:T,inShape:e,outShape:ue,filterShape:t}},zu=(e,t,r,n,o,i)=>{let a=i==="channelsLast",l=a?e[0].dims[3]:e[0].dims[1],d=!1,p=[64,1,1],m={x:r.map(($,S)=>S)},u=[Math.ceil(Tm(m.x.map($=>r[$]))/p[0]),1,1];ve("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${u}`);let h=d?a&&l%4!==0?3:4:1,w=k.size(r),g=[{type:12,data:w},{type:12,data:n},{type:12,data:o},{type:12,data:t.strides},{type:12,data:t.dilations}];Ge(t,g),g.push(...V(e[0].dims,e[1].dims));let b=["rank","rank"],x=e.length===3;x&&(g.push(...V(e[2].dims)),b.push("rank")),g.push(...V(r));let _=$=>{let S=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:n.length},{name:"pads",type:"u32",length:o.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];Le(t,S);let I=d?4:1,T=ge(e[0].dataType),A=E("x",e[0].dataType,e[0].dims.length,h===3?1:h),D=E("W",e[1].dataType,e[1].dims.length,I),z=[A,D],H=M("result",e[0].dataType,r.length,I),W="";if(x){let pe=E("bias",e[2].dataType,e[2].dims.length,I);z.push(pe),W+=`
4696
+ ${ie.registerUniforms(q).declareVariables(...me,$e)}
4697
+ ${Z}
4698
+ ${$m(d,z,W,V,a,t,j[0],j[1],j[2],ne)}
4699
+ ${g?Zt($,_,ne,void 0,!d,O):Qt($,_,ne,void 0,!d,O,!1,void 0,l)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${I};${g};${z};${W};${V};${T};${A};${O}`,inputDependencies:ae},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:Y}),getShaderSource:se}}});var _m,Su,Zr,xm,Iu,Sm,Cu,Tu,Au=M(()=>{"use strict";X();st();oe();re();ut();Xt();_m=e=>{let t=1;for(let r=0;r<e.length;r++)t*=e[r];return t},Su=e=>typeof e=="number"?[e,e,e]:e,Zr=(e,t)=>t<=1?e:e+(e-1)*(t-1),xm=(e,t,r,n=1)=>{let o=Zr(t,n);return Math.floor((e[0]*(r-1)-r+o)/2)},Iu=(e,t,r,n,o)=>{o==null&&(o=xm(e,t[0],n[0]));let i=[0,0,0,r];for(let a=0;a<3;a++)e[a]+2*o>=t[a]&&(i[a]=Math.trunc((e[a]-t[a]+2*o)/n[a]+1));return i},Sm=(e,t,r,n,o,i,a,l,d,p)=>{let m,u,h,w;if(e==="VALID"&&(e=0),typeof e=="number"){m={top:e,bottom:e,left:e,right:e,front:e,back:e};let g=Iu([t,r,n,1],[l,d,p],1,[o,i,a],e);u=g[0],h=g[1],w=g[2]}else if(Array.isArray(e)){if(!e.every((y,x,_)=>y===_[0]))throw Error(`Unsupported padding parameter: ${e}`);m={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let g=Iu([t,r,n,1],[l,d,p],1,[o,i,a],e[0]);u=g[0],h=g[1],w=g[2]}else if(e==="SAME_UPPER"){u=Math.ceil(t/o),h=Math.ceil(r/i),w=Math.ceil(n/a);let g=(u-1)*o+l-t,y=(h-1)*i+d-r,x=(w-1)*a+p-n,_=Math.floor(g/2),$=g-_,S=Math.floor(y/2),I=y-S,T=Math.floor(x/2),A=x-T;m={top:S,bottom:I,left:T,right:A,front:_,back:$}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:m,outDepth:u,outHeight:h,outWidth:w}},Cu=(e,t,r,n,o,i=!1,a="channelsLast")=>{let l,d,p,m,u;if(a==="channelsLast")[l,d,p,m,u]=e;else if(a==="channelsFirst")[l,u,d,p,m]=e;else throw new Error(`Unknown dataFormat ${a}`);let[h,,w,g,y]=t,[x,_,$]=Su(r),[S,I,T]=Su(n),A=Zr(w,S),O=Zr(g,I),z=Zr(y,T),{padInfo:W,outDepth:V,outHeight:j,outWidth:Y}=Sm(o,d,p,m,x,_,$,A,O,z),ae=i?h*u:h,se=[0,0,0,0,0];return a==="channelsFirst"?se=[l,ae,V,j,Y]:a==="channelsLast"&&(se=[l,V,j,Y,ae]),{batchSize:l,dataFormat:a,inDepth:d,inHeight:p,inWidth:m,inChannels:u,outDepth:V,outHeight:j,outWidth:Y,outChannels:ae,padInfo:W,strideDepth:x,strideHeight:_,strideWidth:$,filterDepth:w,filterHeight:g,filterWidth:y,effectiveFilterDepth:A,effectiveFilterHeight:O,effectiveFilterWidth:z,dilationDepth:S,dilationHeight:I,dilationWidth:T,inShape:e,outShape:se,filterShape:t}},Tu=(e,t,r,n,o,i)=>{let a=i==="channelsLast",l=a?e[0].dims[3]:e[0].dims[1],d=!1,p=[64,1,1],m={x:r.map(($,S)=>S)},u=[Math.ceil(_m(m.x.map($=>r[$]))/p[0]),1,1];be("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${u}`);let h=d?a&&l%4!==0?3:4:1,w=E.size(r),g=[{type:12,data:w},{type:12,data:n},{type:12,data:o},{type:12,data:t.strides},{type:12,data:t.dilations}];We(t,g),g.push(...U(e[0].dims,e[1].dims));let y=["rank","rank"],x=e.length===3;x&&(g.push(...U(e[2].dims)),y.push("rank")),g.push(...U(r));let _=$=>{let S=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:n.length},{name:"pads",type:"u32",length:o.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];Le(t,S);let I=d?4:1,T=pe(e[0].dataType),A=k("x",e[0].dataType,e[0].dims.length,h===3?1:h),O=k("W",e[1].dataType,e[1].dims.length,I),z=[A,O],W=D("result",e[0].dataType,r.length,I),V="";if(x){let ae=k("bias",e[2].dataType,e[2].dims.length,I);z.push(ae),V+=`
4701
4700
  fn getBiasByOutputCoords(coords : array<u32, 5>) -> ${d?`vec4<${T}>`:T} {
4702
- return bias[${a?G("coords",4,5):G("coords",1,5)}${d?"/ 4":""}];
4703
- }`}let X=Ee(h,T),Y=He(t,X,T);return`
4704
- ${W}
4701
+ return bias[${a?L("coords",4,5):L("coords",1,5)}${d?"/ 4":""}];
4702
+ }`}let j=Ce(h,T),Y=Ne(t,j,T);return`
4703
+ ${V}
4705
4704
  fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
4706
4705
  let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
4707
4706
  return ${A.getByIndices("aIndices")};
4708
4707
  }
4709
4708
  fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
4710
4709
  let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
4711
- return ${D.getByIndices("aIndices")};
4710
+ return ${O.getByIndices("aIndices")};
4712
4711
  }
4713
- ${$.registerUniforms(S).declareVariables(...z,H)}
4712
+ ${$.registerUniforms(S).declareVariables(...z,W)}
4714
4713
  ${$.mainStart()}
4715
4714
  ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
4716
- let coords = ${H.offsetToIndices("global_idx")};
4717
- let batch = ${G("coords",0,A.rank)};
4718
- let d2 = ${a?G("coords",A.rank-1,A.rank):G("coords",1,A.rank)};
4719
- let xFRCCorner = vec3<u32>(${a?G("coords",1,A.rank):G("coords",2,A.rank)},
4720
- ${a?G("coords",2,A.rank):G("coords",3,A.rank)},
4721
- ${a?G("coords",3,A.rank):G("coords",4,A.rank)}) * uniforms.strides - uniforms.pads;
4715
+ let coords = ${W.offsetToIndices("global_idx")};
4716
+ let batch = ${L("coords",0,A.rank)};
4717
+ let d2 = ${a?L("coords",A.rank-1,A.rank):L("coords",1,A.rank)};
4718
+ let xFRCCorner = vec3<u32>(${a?L("coords",1,A.rank):L("coords",2,A.rank)},
4719
+ ${a?L("coords",2,A.rank):L("coords",3,A.rank)},
4720
+ ${a?L("coords",3,A.rank):L("coords",4,A.rank)}) * uniforms.strides - uniforms.pads;
4722
4721
  let xFCorner = xFRCCorner.x;
4723
4722
  let xRCorner = xFRCCorner.y;
4724
4723
  let xCCorner = xFRCCorner.z;
4725
- let xShapeY = ${a?G("uniforms.x_shape",1,A.rank):G("uniforms.x_shape",2,A.rank)};
4726
- let xShapeZ = ${a?G("uniforms.x_shape",2,A.rank):G("uniforms.x_shape",3,A.rank)};
4727
- let xShapeW = ${a?G("uniforms.x_shape",3,A.rank):G("uniforms.x_shape",4,A.rank)};
4728
- let xShapeU = ${a?G("uniforms.x_shape",4,A.rank):G("uniforms.x_shape",1,A.rank)};
4724
+ let xShapeY = ${a?L("uniforms.x_shape",1,A.rank):L("uniforms.x_shape",2,A.rank)};
4725
+ let xShapeZ = ${a?L("uniforms.x_shape",2,A.rank):L("uniforms.x_shape",3,A.rank)};
4726
+ let xShapeW = ${a?L("uniforms.x_shape",3,A.rank):L("uniforms.x_shape",4,A.rank)};
4727
+ let xShapeU = ${a?L("uniforms.x_shape",4,A.rank):L("uniforms.x_shape",1,A.rank)};
4729
4728
  let inputDepthNearestVec4 = (xShapeU / 4) * 4;
4730
4729
  let inputDepthVec4Remainder = xShapeU % 4;
4731
4730
 
@@ -4805,19 +4804,19 @@ bIndices[${S-2}] = u32(row);
4805
4804
  ${x?"value = value + getBiasByOutputCoords(coords)":""};
4806
4805
  ${Y}
4807
4806
  result[global_idx] = f32(value);
4808
- }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${a};${h};${x}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:u[0],y:u[1],z:u[2]},programUniforms:g}),getShaderSource:_}}});var lo,Ru,Mu=U(()=>{"use strict";Q();ae();ie();co();lt();lo=(e,t,r)=>{let n=e.length>2,o=n?"value += b[output_channel];":"",i=e[0].dims,a=e[1].dims,l=a[0]/t.group,d=t.format==="NHWC",p=tn(i,a,t.dilations,t.pads,t.strides,d),m=k.size(p),u=[{type:12,data:m},{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:l}];Ge(t,u),u.push(...V(i,a));let h=["rank","rank"];n&&(u.push(...V(e[2].dims)),h.push("rank")),u.push(...V(p));let w=g=>{let b=M("output",e[0].dataType,p.length),x=ge(b.type.tensor),_=He(t,b.type.value,x),$=E("x",e[0].dataType,i.length),S=E("w",e[1].dataType,a.length),I=[$,S];n&&I.push(E("b",e[2].dataType,e[2].dims.length));let T=[{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"}];return Le(t,T),`
4809
- ${g.registerUniforms(T).declareVariables(...I,b)}
4807
+ }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${a};${h};${x}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:u[0],y:u[1],z:u[2]},programUniforms:g}),getShaderSource:_}}});var io,ku,Eu=M(()=>{"use strict";X();oe();re();ao();ut();io=(e,t,r)=>{let n=e.length>2,o=n?"value += b[output_channel];":"",i=e[0].dims,a=e[1].dims,l=a[0]/t.group,d=t.format==="NHWC",p=Qr(i,a,t.dilations,t.pads,t.strides,d),m=E.size(p),u=[{type:12,data:m},{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:l}];We(t,u),u.push(...U(i,a));let h=["rank","rank"];n&&(u.push(...U(e[2].dims)),h.push("rank")),u.push(...U(p));let w=g=>{let y=D("output",e[0].dataType,p.length),x=pe(y.type.tensor),_=Ne(t,y.type.value,x),$=k("x",e[0].dataType,i.length),S=k("w",e[1].dataType,a.length),I=[$,S];n&&I.push(k("b",e[2].dataType,e[2].dims.length));let T=[{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"}];return Le(t,T),`
4808
+ ${g.registerUniforms(T).declareVariables(...I,y)}
4810
4809
 
4811
4810
  ${g.mainStart()}
4812
4811
  ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
4813
4812
 
4814
- let outputIndices = ${b.offsetToIndices("global_idx")};
4813
+ let outputIndices = ${y.offsetToIndices("global_idx")};
4815
4814
  let batch: u32 = outputIndices[0];
4816
4815
  let output_channel: u32 = outputIndices[${d?3:1}];
4817
4816
  let xRCCorner: vec2<u32> = vec2<u32>(outputIndices[${d?1:2}], outputIndices[${d?2:3}]) * uniforms.strides - uniforms.pads;
4818
4817
  let group_id: u32 = output_channel / uniforms.output_channels_per_group;
4819
4818
 
4820
- var value: ${b.type.value} = ${b.type.value}(0);
4819
+ var value: ${y.type.value} = ${y.type.value}(0);
4821
4820
  for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) {
4822
4821
  let input_channel = group_id * uniforms.w_shape[1] + wInChannel;
4823
4822
  for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) {
@@ -4841,8 +4840,8 @@ bIndices[${S-2}] = u32(row);
4841
4840
  }
4842
4841
  ${o}
4843
4842
  ${_}
4844
- ${b.setByOffset("global_idx","value")}
4845
- }`};return{name:"GroupedConv",shaderCache:{hint:t.cacheKey,inputDependencies:h},getRunData:()=>({outputs:[{dims:r?r(p):p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:u}),getShaderSource:w}},Ru=(e,t,r)=>{let n=e.length>2,o=$e(r[3]),i=$e(r[2]),a=k.size(r)/o/i,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/o],d=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/o],p=[r[0],r[1],r[2],r[3]/o],m=[{type:12,data:a},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];Ge(t,m),m.push(...V(l,d,p));let u=(i-1)*t.strides[1]+d[1],h=w=>{let g=M("output",e[0].dataType,p.length,o),b=ge(g.type.tensor),x=He(t,g.type.value,b),_=E("x",e[0].dataType,l.length,o),$=E("w",e[1].dataType,d.length,o),S=[_,$];n&&S.push(E("b",e[2].dataType,e[2].dims,o));let I=n?"value += b[output_channel];":"",T=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return Le(t,T),`
4843
+ ${y.setByOffset("global_idx","value")}
4844
+ }`};return{name:"GroupedConv",shaderCache:{hint:t.cacheKey,inputDependencies:h},getRunData:()=>({outputs:[{dims:r?r(p):p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:u}),getShaderSource:w}},ku=(e,t,r)=>{let n=e.length>2,o=he(r[3]),i=he(r[2]),a=E.size(r)/o/i,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/o],d=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/o],p=[r[0],r[1],r[2],r[3]/o],m=[{type:12,data:a},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];We(t,m),m.push(...U(l,d,p));let u=(i-1)*t.strides[1]+d[1],h=w=>{let g=D("output",e[0].dataType,p.length,o),y=pe(g.type.tensor),x=Ne(t,g.type.value,y),_=k("x",e[0].dataType,l.length,o),$=k("w",e[1].dataType,d.length,o),S=[_,$];n&&S.push(k("b",e[2].dataType,e[2].dims,o));let I=n?"value += b[output_channel];":"",T=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return Le(t,T),`
4846
4845
  ${w.registerUniforms(T).declareVariables(...S,g)}
4847
4846
  ${w.mainStart()}
4848
4847
  ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
@@ -4887,14 +4886,14 @@ bIndices[${S-2}] = u32(row);
4887
4886
  ${x}
4888
4887
  ${g.set("batch","row","col + i","output_channel","value")};
4889
4888
  }
4890
- }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${o};${i};${u};${d[0]};${d[1]}`,inputDependencies:n?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:m}),getShaderSource:h}}});var po,km,Uu,mo=U(()=>{"use strict";Q();ae();rr();ie();lt();po=(e,t,r,n,o=!1)=>{let i=e[0].dims,a=e[1].dims,l=i[i.length-2],d=a[a.length-1],p=i[i.length-1],m=$e(d),u=$e(p),h=$e(l),w=k.size(r)/m/h,g=e.length>2,b=n?n.slice(0,-2):r.slice(0,-2),_=[k.size(b),l,d],$=[{type:12,data:w},{type:12,data:l},{type:12,data:d},{type:12,data:p}];Ge(t,$),$.push(...V(b,i,a)),g&&$.push(...V(e[2].dims)),$.push(...V(_));let S=I=>{let T=Lr("batch_dims",e[0].dataType,b.length),A=E("a",e[0].dataType,i.length,u),D=E("b",e[1].dataType,a.length,m),z=M("output",e[0].dataType,_.length,m),H=ge(z.type.tensor),W=He(t,z.type.value,H),X=[A,D],Y="";if(g){let Z=o?m:1;X.push(E("bias",e[2].dataType,e[2].dims.length,Z)),Y=`${o?`value += bias[col / ${Z}];`:`value += ${z.type.value}(bias[row + i]);`}`}let pe=i.slice(0,-2),ue=a.slice(0,-2),le=Nt(pe,b),K=Nt(ue,b),de=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];Le(t,de);let re=(Z,ne)=>{let J=Z.rank,ke=Z.name;if(J===2)return`var ${ke}_indices = ${Z.type.indices}(0u, 0u);`;let R=T.rank,F=`var ${ke}_indices: ${Z.type.indices};`;for(let ce=J-2-1,Re=R-1;ce>=0;ce--,Re--)F+=`
4891
- ${ke}_indices[${ce}] = ${R>1?`batch_indices[${Re}]`:"batch_indices"};`;return ne.forEach(ce=>{F+=`
4892
- ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
4893
- ${ke}_indices[${J-1}] = 0u;`,F},se=()=>{let Z=`var a_data: ${A.type.value};`;for(let ne=0;ne<u;ne++)Z+=`
4894
- let b_data${ne} = b[(b_offset + (k + ${ne}) * uniforms.N + col) / ${m}];`;for(let ne=0;ne<h;ne++){Z+=`a_data = a[(a_offset + (row + ${ne}) * uniforms.K + k) / ${u}];`;for(let J=0;J<u;J++)Z+=`
4895
- values[${ne}] = fma(${D.type.value}(a_data${u===1?"":`[${J}]`}), b_data${J}, values[${ne}]);
4896
- `}return Z};return`
4897
- ${I.registerUniforms(de).registerInternalVariables(T).declareVariables(...X,z)}
4889
+ }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${o};${i};${u};${d[0]};${d[1]}`,inputDependencies:n?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:m}),getShaderSource:h}}});var so,Im,Pu,uo=M(()=>{"use strict";X();oe();Jt();re();ut();so=(e,t,r,n,o=!1)=>{let i=e[0].dims,a=e[1].dims,l=i[i.length-2],d=a[a.length-1],p=i[i.length-1],m=he(d),u=he(p),h=he(l),w=E.size(r)/m/h,g=e.length>2,y=n?n.slice(0,-2):r.slice(0,-2),_=[E.size(y),l,d],$=[{type:12,data:w},{type:12,data:l},{type:12,data:d},{type:12,data:p}];We(t,$),$.push(...U(y,i,a)),g&&$.push(...U(e[2].dims)),$.push(...U(_));let S=I=>{let T=Wr("batch_dims",e[0].dataType,y.length),A=k("a",e[0].dataType,i.length,u),O=k("b",e[1].dataType,a.length,m),z=D("output",e[0].dataType,_.length,m),W=pe(z.type.tensor),V=Ne(t,z.type.value,W),j=[A,O],Y="";if(g){let ee=o?m:1;j.push(k("bias",e[2].dataType,e[2].dims.length,ee)),Y=`${o?`value += bias[col / ${ee}];`:`value += ${z.type.value}(bias[row + i]);`}`}let ae=i.slice(0,-2),se=a.slice(0,-2),ie=Mt(ae,y),q=Mt(se,y),ce=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];Le(t,ce);let ne=(ee,K)=>{let me=ee.rank,$e=ee.name;if(me===2)return`var ${$e}_indices = ${ee.type.indices}(0u, 0u);`;let R=T.rank,G=`var ${$e}_indices: ${ee.type.indices};`;for(let ue=me-2-1,Be=R-1;ue>=0;ue--,Be--)G+=`
4890
+ ${$e}_indices[${ue}] = ${R>1?`batch_indices[${Be}]`:"batch_indices"};`;return K.forEach(ue=>{G+=`
4891
+ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
4892
+ ${$e}_indices[${me-1}] = 0u;`,G},Z=()=>{let ee=`var a_data: ${A.type.value};`;for(let K=0;K<u;K++)ee+=`
4893
+ let b_data${K} = b[(b_offset + (k + ${K}) * uniforms.N + col) / ${m}];`;for(let K=0;K<h;K++){ee+=`a_data = a[(a_offset + (row + ${K}) * uniforms.K + k) / ${u}];`;for(let me=0;me<u;me++)ee+=`
4894
+ values[${K}] = fma(${O.type.value}(a_data${u===1?"":`[${me}]`}), b_data${me}, values[${K}]);
4895
+ `}return ee};return`
4896
+ ${I.registerUniforms(ce).registerInternalVariables(T).declareVariables(...j,z)}
4898
4897
  ${I.mainStart()}
4899
4898
  ${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
4900
4899
  let col = (global_idx % (uniforms.N / ${m})) * ${m};
@@ -4904,24 +4903,24 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
4904
4903
  let batch = index1 / stride1;
4905
4904
 
4906
4905
  ${r.length===2?"":`let batch_indices = ${T.offsetToIndices("batch")};`}
4907
- ${re(A,le)}
4906
+ ${ne(A,ie)}
4908
4907
  let a_offset = ${A.indicesToOffset("a_indices")};
4909
- ${re(D,K)}
4910
- let b_offset = ${D.indicesToOffset("b_indices")};
4908
+ ${ne(O,q)}
4909
+ let b_offset = ${O.indicesToOffset("b_indices")};
4911
4910
  var values: array<${z.type.value}, ${h}>;
4912
4911
  for (var k: u32 = 0u; k < uniforms.K; k = k + ${u}) {
4913
- ${se()}
4912
+ ${Z()}
4914
4913
  }
4915
4914
  for (var i = 0u; i < ${h}u; i++) {
4916
4915
  var value = values[i];
4917
4916
  ${Y}
4918
- ${W}
4917
+ ${V}
4919
4918
  let cur_indices = ${z.type.indices}(batch, row + i, col);
4920
4919
  let offset = ${z.indicesToOffset("cur_indices")};
4921
4920
  ${z.setByOffset(`offset / ${m}`,"value")};
4922
4921
  }
4923
4922
  }
4924
- `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${m};${u};${h};${o}`,inputDependencies:g?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:$}),getShaderSource:S}},km=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},Uu=e=>{km(e.inputs);let t=tt.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],n=e.inputs[0].dims[e.inputs[0].dims.length-1];r<8&&n<8?e.compute(po(e.inputs,{activation:""},t)):e.compute(Jr(e.inputs,{activation:""},t))}});var tn,fo,Pm,ho,go,Om,Dm,zm,yo,co=U(()=>{"use strict";ae();ku();Bu();rr();Mu();lt();mo();Et();tn=(e,t,r,n,o,i)=>{let a=e[0],l=e.slice(i?1:2,i?3:4),d=l.length,p=t[0],u=t.slice(2).map((g,b)=>g+(g-1)*(r[b]-1)),w=l.map((g,b)=>g+n[b]+n[b+d]).map((g,b)=>Math.floor((g-u[b]+o[b])/o[b]));return w.splice(0,0,a),w.splice(i?3:1,0,p),w},fo=[2,3,1,0],Pm=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[1]*t.group;if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let o=e[0].dims.length-2;if(t.dilations.length!==o)throw new Error(`dilations should be ${o}D`);if(t.strides.length!==o)throw new Error(`strides should be ${o}D`);if(t.pads.length!==o*2)throw new Error(`pads should be ${o*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},ho=(e,t)=>{let r=e.kernelShape.slice();for(let i=2;i<t[1].dims.length;++i)r[i-2]===0&&(r[i-2]=t[1].dims[i]);let n=e.pads.slice();Ct.adjustPadsBasedOnAutoPad(t[0].dims,e.strides,e.dilations,r,n,e.format==="NHWC",e.autoPad);let o=Object.assign({},e);return Object.assign(o,{kernelShape:r,pads:n}),o},go=e=>{let t=Xr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],o=e.dilations,i=e.group,a=e.kernel_shape,l=e.pads,d=e.strides,p=e.w_is_const();return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,pads:l,strides:d,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},Om=(e,t,r)=>{let n=ho(r,t),o=r.format==="NHWC";if(r.group!==1){if(!e.adapterInfo.isArchitecture("ampere")&&o&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1){let D=tn(t[0].dims,t[1].dims,r.dilations,n.pads,r.strides,o),z=e.kernelCustomData.wT??e.compute(Ue(t[1],fo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=z);let H=[t[0],z];t.length===3&&H.push(t[2]),e.compute(Ru(H,n,D),{inputs:H})}else e.compute(lo(t,n));return}let i=t.length===3,a=t[0].dims[o?1:2],l=t[0].dims[o?2:3],d=t[0].dims[o?3:1],p=t[1].dims[2],m=t[1].dims[3],u=tn(t[0].dims,t[1].dims,r.dilations,n.pads,r.strides,o),h=u[o?1:2],w=u[o?2:3],g=u[o?3:1],b=o&&p===a&&m===l&&r.pads[0]===0&&r.pads[1]===0;if(b||p===1&&m===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let A=u[0],D,z,H,W=[];if(o){let pe=e.kernelCustomData.wT??e.compute(Ue(t[1],fo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=pe),b){let ue=a*l*d;D=t[0].reshape([1,A,ue]),z=pe.reshape([1,ue,g]),H=[1,A,g]}else D=t[0].reshape([A,a*l,d]),z=pe.reshape([1,d,g]),H=[A,h*w,g];W.push(D),W.push(z)}else D=t[0].reshape([A,d,a*l]),z=t[1].reshape([1,g,d]),H=[A,g,h*w],W.push(z),W.push(D);i&&W.push(t[2]);let X=H[2],Y=W[0].dims[W[0].dims.length-1];X<8&&Y<8?e.compute(po(W,n,u,H,o),{inputs:W}):e.compute(Jr(W,n,u,H,o),{inputs:W});return}let x=!0,_=e.kernelCustomData.wT??e.compute(Ue(t[1],fo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=_);let $=[t[0],_];i&&$.push(t[2]);let S=o?h*w:g,I=o?g:h*w,T=p*m*d;e.compute(Eu($,n,u,S,I,T,i,x),{inputs:$})},Dm=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=[0,t.pads[0],0,t.pads[1]],i=[1].concat(t.strides),a=[1].concat(t.dilations),l=[1].concat(t.kernelShape),d=ho({...t,pads:o,strides:i,dilations:a,kernelShape:l},n);e.compute(lo(n,d,p=>r?[p[0],p[2],p[3]]:[]))},zm=(e,t,r)=>{let n=r.format==="NHWC"?"channelsLast":"channelsFirst",o=ho(r,t),i=r.autoPad==="NOTSET"?r.pads:r.autoPad,a=Du(t[0].dims,t[1].dims,r.strides,r.dilations,i,!1,n);e.compute(zu(t,o,a.outShape,[a.filterDepth,a.filterHeight,a.filterWidth],[a.padInfo.front,a.padInfo.top,a.padInfo.left],n))},yo=(e,t)=>{Pm(e.inputs,t),e.inputs[0].dims.length===3?Dm(e,t):e.inputs[0].dims.length===5?zm(e,e.inputs,t):Om(e,e.inputs,t)}});var Bm,Vu,Nu=U(()=>{"use strict";Q();dt();ie();lt();Jt();uo();rr();Bm=(e,t=!1,r,n,o=4)=>{let i=_=>{switch(_){case 1:return"return w[getIndexFromCoords4D(coord, vec4<i32>(uniforms.w_shape))];";case 4:return`
4923
+ `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${m};${u};${h};${o}`,inputDependencies:g?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:$}),getShaderSource:S}},Im=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},Pu=e=>{Im(e.inputs);let t=Je.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],n=e.inputs[0].dims[e.inputs[0].dims.length-1];r<8&&n<8?e.compute(so(e.inputs,{activation:""},t)):e.compute(Xr(e.inputs,{activation:""},t))}});var Qr,lo,Cm,co,po,Tm,Am,km,mo,ao=M(()=>{"use strict";oe();xu();Au();Jt();Eu();ut();uo();Ct();Qr=(e,t,r,n,o,i)=>{let a=e[0],l=e.slice(i?1:2,i?3:4),d=l.length,p=t[0],u=t.slice(2).map((g,y)=>g+(g-1)*(r[y]-1)),w=l.map((g,y)=>g+n[y]+n[y+d]).map((g,y)=>Math.floor((g-u[y]+o[y])/o[y]));return w.splice(0,0,a),w.splice(i?3:1,0,p),w},lo=[2,3,1,0],Cm=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[1]*t.group;if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let o=e[0].dims.length-2;if(t.dilations.length!==o)throw new Error(`dilations should be ${o}D`);if(t.strides.length!==o)throw new Error(`strides should be ${o}D`);if(t.pads.length!==o*2)throw new Error(`pads should be ${o*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},co=(e,t)=>{let r=e.kernelShape.slice();for(let i=2;i<t[1].dims.length;++i)r[i-2]===0&&(r[i-2]=t[1].dims[i]);let n=e.pads.slice();xt.adjustPadsBasedOnAutoPad(t[0].dims,e.strides,e.dilations,r,n,e.format==="NHWC",e.autoPad);let o=Object.assign({},e);return Object.assign(o,{kernelShape:r,pads:n}),o},po=e=>{let t=Kr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],o=e.dilations,i=e.group,a=e.kernel_shape,l=e.pads,d=e.strides,p=e.w_is_const();return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,pads:l,strides:d,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},Tm=(e,t,r)=>{let n=co(r,t),o=r.format==="NHWC";if(r.group!==1){if(!e.adapterInfo.isArchitecture("ampere")&&o&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1){let O=Qr(t[0].dims,t[1].dims,r.dilations,n.pads,r.strides,o),z=e.kernelCustomData.wT??e.compute(Re(t[1],lo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=z);let W=[t[0],z];t.length===3&&W.push(t[2]),e.compute(ku(W,n,O),{inputs:W})}else e.compute(io(t,n));return}let i=t.length===3,a=t[0].dims[o?1:2],l=t[0].dims[o?2:3],d=t[0].dims[o?3:1],p=t[1].dims[2],m=t[1].dims[3],u=Qr(t[0].dims,t[1].dims,r.dilations,n.pads,r.strides,o),h=u[o?1:2],w=u[o?2:3],g=u[o?3:1],y=o&&p===a&&m===l&&r.pads[0]===0&&r.pads[1]===0;if(y||p===1&&m===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let A=u[0],O,z,W,V=[];if(o){let ae=e.kernelCustomData.wT??e.compute(Re(t[1],lo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=ae),y){let se=a*l*d;O=t[0].reshape([1,A,se]),z=ae.reshape([1,se,g]),W=[1,A,g]}else O=t[0].reshape([A,a*l,d]),z=ae.reshape([1,d,g]),W=[A,h*w,g];V.push(O),V.push(z)}else O=t[0].reshape([A,d,a*l]),z=t[1].reshape([1,g,d]),W=[A,g,h*w],V.push(z),V.push(O);i&&V.push(t[2]);let j=W[2],Y=V[0].dims[V[0].dims.length-1];j<8&&Y<8?e.compute(so(V,n,u,W,o),{inputs:V}):e.compute(Xr(V,n,u,W,o),{inputs:V});return}let x=!0,_=e.kernelCustomData.wT??e.compute(Re(t[1],lo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=_);let $=[t[0],_];i&&$.push(t[2]);let S=o?h*w:g,I=o?g:h*w,T=p*m*d;e.compute(_u($,n,u,S,I,T,i,x),{inputs:$})},Am=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=[0,t.pads[0],0,t.pads[1]],i=[1].concat(t.strides),a=[1].concat(t.dilations),l=[1].concat(t.kernelShape),d=co({...t,pads:o,strides:i,dilations:a,kernelShape:l},n);e.compute(io(n,d,p=>r?[p[0],p[2],p[3]]:[p[0],p[1],p[3]]))},km=(e,t,r)=>{let n=r.format==="NHWC"?"channelsLast":"channelsFirst",o=co(r,t),i=r.autoPad==="NOTSET"?r.pads:r.autoPad,a=Cu(t[0].dims,t[1].dims,r.strides,r.dilations,i,!1,n);e.compute(Tu(t,o,a.outShape,[a.filterDepth,a.filterHeight,a.filterWidth],[a.padInfo.front,a.padInfo.top,a.padInfo.left],n))},mo=(e,t)=>{Cm(e.inputs,t),e.inputs[0].dims.length===3?Am(e,t):e.inputs[0].dims.length===5?km(e,e.inputs,t):Tm(e,e.inputs,t)}});var Em,Ou,zu=M(()=>{"use strict";X();st();re();ut();Xt();oo();Jt();Em=(e,t=!1,r,n,o=4)=>{let i=_=>{switch(_){case 1:return"return w[getIndexFromCoords4D(coord, vec4<i32>(uniforms.w_shape))];";case 4:return`
4925
4924
  let coord1 = vec4<i32>(coordX, coordY, col + 1, rowInner);
4926
4925
  let coord2 = vec4<i32>(coordX, coordY, col + 2, rowInner);
4927
4926
  let coord3 = vec4<i32>(coordX, coordY, col + 3, rowInner);
@@ -4987,7 +4986,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
4987
4986
  ${i(o)}
4988
4987
  }
4989
4988
  return ${n}(0.0);
4990
- `,b=He(r,n);return`
4989
+ `,y=Ne(r,n);return`
4991
4990
  fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${n} {
4992
4991
  ${e?w:g}
4993
4992
  }
@@ -5002,25 +5001,25 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5002
5001
  var value = valueInput;
5003
5002
  let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
5004
5003
  ${l}
5005
- ${Zr(t)}
5006
- ${b}
5004
+ ${jr(t)}
5005
+ ${y}
5007
5006
  result[getIndexFromCoords4D(coords, vec4<i32>(uniforms.result_shape))/${o}] = value;
5008
5007
  }
5009
- }`},Vu=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",p=d?e[0].dims[3]:e[0].dims[1],m=r[0],u=d?r[2]:r[3],h=d?r[1]:r[2],w=d?r[3]:r[1],g=d&&p%4===0&&p%3&&w%4===0,b=d?w:u*h,x=d?u*h:w,_=[8,8,1],$=n<=8?[4,1,1]:[4,4,1],S=[Math.ceil(b/_[0]/$[0]),Math.ceil(x/_[1]/$[1]),Math.ceil(m/_[2]/$[2])];ve("verbose",()=>`[conv_backprop_mm_webgpu] dispatch = ${S}`);let I=g?4:1,T=Math.max(_[0]*I,_[1]),A=g?4:1,D=[t.kernelShape[d?1:2],t.kernelShape[d?2:3]],z=[D[0]+(t.dilations[0]<=1?0:(D[0]-1)*(t.dilations[0]-1)),D[1]+(t.dilations[1]<=1?0:(D[1]-1)*(t.dilations[1]-1))],H=[z[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),z[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],W=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:t.strides},{type:6,data:t.dilations},{type:6,data:D},{type:6,data:H}];Ge(t,W),W.push(...V(e[0].dims,e[1].dims));let X=["rank","rank"];a&&(W.push(...V(e[2].dims)),X.push("rank")),W.push(...V(r));let Y=pe=>{let ue=E("x",e[0].dataType,e[0].dims.length,A),le=E("w",e[1].dataType,e[1].dims.length,1),K=M("result",e[0].dataType,r.length,A),de=[ue,le],re="";if(a){let ne=E("bias",e[2].dataType,e[2].dims.length,A);de.push(ne),re+=`
5010
- fn getBiasByOutputCoords(coords : vec4<i32>) -> ${ne.type.value} {
5008
+ }`},Ou=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",p=d?e[0].dims[3]:e[0].dims[1],m=r[0],u=d?r[2]:r[3],h=d?r[1]:r[2],w=d?r[3]:r[1],g=d&&p%4===0&&p%3&&w%4===0,y=d?w:u*h,x=d?u*h:w,_=[8,8,1],$=n<=8?[4,1,1]:[4,4,1],S=[Math.ceil(y/_[0]/$[0]),Math.ceil(x/_[1]/$[1]),Math.ceil(m/_[2]/$[2])];be("verbose",()=>`[conv_backprop_mm_webgpu] dispatch = ${S}`);let I=g?4:1,T=Math.max(_[0]*I,_[1]),A=g?4:1,O=[t.kernelShape[d?1:2],t.kernelShape[d?2:3]],z=[O[0]+(t.dilations[0]<=1?0:(O[0]-1)*(t.dilations[0]-1)),O[1]+(t.dilations[1]<=1?0:(O[1]-1)*(t.dilations[1]-1))],W=[z[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),z[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],V=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:t.strides},{type:6,data:t.dilations},{type:6,data:O},{type:6,data:W}];We(t,V),V.push(...U(e[0].dims,e[1].dims));let j=["rank","rank"];a&&(V.push(...U(e[2].dims)),j.push("rank")),V.push(...U(r));let Y=ae=>{let se=k("x",e[0].dataType,e[0].dims.length,A),ie=k("w",e[1].dataType,e[1].dims.length,1),q=D("result",e[0].dataType,r.length,A),ce=[se,ie],ne="";if(a){let K=k("bias",e[2].dataType,e[2].dims.length,A);ce.push(K),ne+=`
5009
+ fn getBiasByOutputCoords(coords : vec4<i32>) -> ${K.type.value} {
5011
5010
  return bias[coords.${d?"w":"y"}${g?"/ 4":""}];
5012
- }`}let se=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"strides",type:"i32",length:2},{name:"dilations",type:"i32",length:2},{name:"filter_dims",type:"i32",length:D.length},{name:"pads",type:"i32",length:H.length}];Le(t,se);let Z=ge(e[0].dataType,1);if(Z!=="f16"&&Z!=="f32")throw new Error(`elemType ${Z} is not supported.`);return`
5013
- ${Qr("uniforms.result_strides")}
5014
- ${pe.registerUniforms(se).declareVariables(...de,K)};
5015
- ${re}
5016
- ${Bm(d,a,t,ue.type.value,I)}
5017
- ${g?er($,_,Z,void 0,!d,T):tr($,_,Z,void 0,!d,T,!1,void 0,l)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${$};${_};${g}`,inputDependencies:X},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:W}),getShaderSource:Y}}});var Rm,bo,Wu=U(()=>{"use strict";Q();dt();ae();ie();Rm=(e,t,r,n,o,i=!1,a,l,d=!1)=>{let p=d?1:2,m=d?2:3,u=d?3:1,h=i?2:1,w=`
5011
+ }`}let Z=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"strides",type:"i32",length:2},{name:"dilations",type:"i32",length:2},{name:"filter_dims",type:"i32",length:O.length},{name:"pads",type:"i32",length:W.length}];Le(t,Z);let ee=pe(e[0].dataType,1);if(ee!=="f16"&&ee!=="f32")throw new Error(`elemType ${ee} is not supported.`);return`
5012
+ ${Yr("uniforms.result_strides")}
5013
+ ${ae.registerUniforms(Z).declareVariables(...ce,q)};
5014
+ ${ne}
5015
+ ${Em(d,a,t,se.type.value,I)}
5016
+ ${g?Zt($,_,ee,void 0,!d,T):Qt($,_,ee,void 0,!d,T,!1,void 0,l)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${$};${_};${g}`,inputDependencies:j},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:V}),getShaderSource:Y}}});var Pm,fo,Bu=M(()=>{"use strict";X();st();oe();re();Pm=(e,t,r,n,o,i=!1,a,l,d=!1)=>{let p=d?1:2,m=d?2:3,u=d?3:1,h=i?2:1,w=`
5018
5017
  fn setOutputAtIndex(flatIndex : u32, value : ${i?`vec4<${a}>`:a}) {
5019
5018
  result[flatIndex] = ${i?`vec4<${a}>`:a}(value);
5020
5019
  }`;n&&(w+=`
5021
5020
  fn getBiasByOutputCoords(coords : vec4<u32>) -> ${i?`vec4<${a}>`:a} {
5022
5021
  return bias[coords.${d?"w":"y"}${i?"/ 4":""}];
5023
- }`);let g=i?4:1,b=E("W",t[1].dataType,t[1].dims.length,g),x=E("Dy",t[0].dataType,t[0].dims.length,g),_=[x,b];n&&_.push(E("bias",t[2].dataType,[r[u]].length,g));let $=M("result",t[0].dataType,r.length,g),S=`{
5022
+ }`);let g=i?4:1,y=k("W",t[1].dataType,t[1].dims.length,g),x=k("Dy",t[0].dataType,t[0].dims.length,g),_=[x,y];n&&_.push(k("bias",t[2].dataType,[r[u]].length,g));let $=D("result",t[0].dataType,r.length,g),S=`{
5024
5023
  let batch: u32 = ${o?"global_id.z":"workgroup_id.z"} / uniforms.result_shape[1];
5025
5024
  let r = ${o?"global_id.z":"workgroup_id.z"} % uniforms.result_shape[1];
5026
5025
  let c = ${o?"global_id.y":"workgroup_id.y"} * ${h};
@@ -5066,10 +5065,10 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5066
5065
  if (bDyCVal && bDyCVal2) {
5067
5066
  let d2Length = uniforms.Dy_shape[3];
5068
5067
  for (var d2 :u32 = 0; d2 < d2Length; d2 = d2 + 4) {
5069
- let wValue0 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};
5070
- let wValue1 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};
5071
- let wValue2 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};
5072
- let wValue3 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};
5068
+ let wValue0 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};
5069
+ let wValue1 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};
5070
+ let wValue2 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};
5071
+ let wValue3 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};
5073
5072
 
5074
5073
  var xValue = ${x.get("batch","idyR","idyC","d2")};
5075
5074
  let tmpval = vec4<${a}>(dot(xValue, wValue0),
@@ -5088,10 +5087,10 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5088
5087
  } else if (bDyCVal) {
5089
5088
  let d2Length = uniforms.Dy_shape[${u}];
5090
5089
  for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) {
5091
- let wValue0 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};
5092
- let wValue1 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};
5093
- let wValue2 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};
5094
- let wValue3 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};
5090
+ let wValue0 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};
5091
+ let wValue1 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};
5092
+ let wValue2 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};
5093
+ let wValue3 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};
5095
5094
 
5096
5095
  var xValue = ${x.get("batch","idyR","idyC","d2")};
5097
5096
  let tmpval = vec4<${a}>(dot(xValue, wValue0),
@@ -5103,10 +5102,10 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5103
5102
  } else if (bDyCVal2) {
5104
5103
  let d2Length = uniforms.Dy_shape[3];
5105
5104
  for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) {
5106
- let wValue0 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};
5107
- let wValue1 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};
5108
- let wValue2 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};
5109
- let wValue3 = ${b.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};
5105
+ let wValue0 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};
5106
+ let wValue1 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};
5107
+ let wValue2 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};
5108
+ let wValue3 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};
5110
5109
 
5111
5110
  var xValue = ${x.get("batch","idyR","idyC2","d2")};
5112
5111
  let tmpval = vec4<${a}>(dot(xValue, wValue0),
@@ -5163,7 +5162,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5163
5162
  var inputChannel = groupId * uniforms.input_channels_per_group;
5164
5163
  for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group; d2 = d2 + 1) {
5165
5164
  let xValue = ${d?x.get("batch","idyR","idyC","inputChannel"):x.get("batch","inputChannel","idyR","idyC")};
5166
- let wValue = ${b.get("inputChannel","wOutChannel","u32(wRPerm)","u32(wCPerm)")};
5165
+ let wValue = ${y.get("inputChannel","wOutChannel","u32(wRPerm)","u32(wCPerm)")};
5167
5166
  dotProd = dotProd + xValue * wValue;
5168
5167
  inputChannel = inputChannel + 1;
5169
5168
  }
@@ -5177,35 +5176,35 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5177
5176
 
5178
5177
  ${e.mainStart()}
5179
5178
  ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")};
5180
- ${i?S:I}}`},bo=(e,t,r)=>{let n=e.length>2,o=t.outputShape,i=k.size(o),a=[Math.ceil(i/64),1,1];ve("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${a}`);let l=t.format==="NHWC",d=["rank","rank"],p=[t.strides[0],t.strides[1]],m=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],u=[t.dilations[0],t.dilations[1]],h=[m[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),m[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],w=[h[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),h[1]-1-Math.floor(t.pads[1]+t.pads[3])/2],g=!1,b=t.group,x=e[1].dims,_=x[0]/b,$=x[1],S=[{type:12,data:i},{type:12,data:p},{type:12,data:m},{type:12,data:u},{type:12,data:h},{type:6,data:w},{type:12,data:_},{type:12,data:$},...V(e[0].dims,e[1].dims)];n&&(S.push(...V(e[2].dims)),d.push("rank")),S.push(...V(o));let I=a[1]===1&&a[2]===1,T=A=>{let D=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:p.length},{name:"filter_dims",type:"u32",length:m.length},{name:"dilations",type:"u32",length:m.length},{name:"effective_filter_dims",type:"u32",length:h.length},{name:"pads",type:"i32",length:w.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],z=ge(e[0].dataType);return`${Rm(A,e,o,n,I,g,z,D,l)}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};`,inputDependencies:d},getRunData:()=>({dispatchGroup:{x:a[0],y:a[1],z:a[2]},outputs:[{dims:r?r(o):o,dataType:e[0].dataType}],programUniforms:S}),getShaderSource:T}}});var Mm,Um,Vm,Hu,Gu,Nm,Wm,Hm,Gm,Lu,Fu=U(()=>{"use strict";Nu();Wu();lt();Et();Mm=(e,t,r,n,o,i)=>(e-1)*t+r+(n-1)*o+1-i,Um=(e,t,r,n,o)=>{let i=Math.floor(e/2);t==="SAME_UPPER"?(r[n]=i,r[o]=e-i):t==="SAME_LOWER"&&(r[n]=e-i,r[o]=i)},Vm=(e,t,r,n,o,i,a,l,d,p)=>{let m=e.length-2,u=p.length===0;if(d.length===0)for(let g=0;g<m;++g)d.push(0);let h=e[0],w=t[l?3:1]*o;for(let g=0,b=e.length-m-(l?1:0);g<m;++g,++b){let x=e[b],_=u?x*a[g]:p[g],$=Mm(x,a[g],i[g],t[b],r[g],_);Um($,n,i,g,g+m),u&&p.push(a[g]*(x-1)+d[g]+(t[b]-1)*r[g]+1-i[g]-i[g+m])}p.splice(0,0,h),p.splice(l?3:1,0,w)},Hu=(e,t)=>{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((u,h)=>u*h,1)===0){r.length=0;for(let u=2;u<t[1].dims.length;++u)r.push(t[1].dims[u])}let n=e.format==="NHWC";r.splice(0,0,t[1].dims[0]),r.splice(n?3:1,0,t[1].dims[1]);let o=e.pads.slice(),i=e.outputShape.slice(),a=e.outputPadding.slice(),l=t[0].dims,d=e.dilations.slice();if(d.reduce((u,h)=>u+h,0)===0){let u=t[0].dims.length-2;d=new Array(u).fill(1)}let p=e.strides.slice();if(p.reduce((u,h)=>u+h,0)===0){let u=t[0].dims.length-2;p=new Array(u).fill(1)}Vm(l,r,d,e.autoPad,e.group,o,p,n,a,i);let m=Object.assign({},e);return Object.assign(m,{kernelShape:r,pads:o,outputPadding:a,outputShape:i,dilations:d,strides:p}),m},Gu=e=>{let t=Xr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],o=e.dilations,i=e.group,a=e.kernelShape,l=e.pads,d=e.strides,p=e.wIsConst(),m=e.outputPadding,u=e.outputShape;return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,outputPadding:m,outputShape:u,pads:l,strides:d,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},Nm=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[0];if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let o=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==o))throw new Error("invalid bias");let i=e[0].dims.length-2;if(t.dilations.reduce((m,u)=>m+u,0)>0&&t.dilations.length!==i)throw new Error(`dilations should be ${i}D`);if(t.strides.reduce((m,u)=>m+u,0)>0&&t.strides.length!==i)throw new Error(`strides should be ${i}D`);if(t.pads.reduce((m,u)=>m+u,0)>0&&t.pads.length!==i*2)throw new Error(`pads should be ${i*2}D`);if(t.outputPadding.length!==i&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${i}D`);if(t.kernelShape.reduce((m,u)=>m+u,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Wm=[2,3,1,0],Hm=(e,t,r)=>{let n=Hu(r,t),o=r.format==="NHWC",i=n.outputShape,a=i[o?3:1],l=t[0].dims[o?3:1];if(n.group!==1||a===1&&l===1){e.compute(bo(t,n));return}let d=i[o?1:2],p=i[o?2:3],m=t[1].dims[2],u=t[1].dims[3],h=o?d*p:a,w=o?a:d*p,g=m*u*l,b=!0,x=e.kernelCustomData.wT??e.compute(Ue(t[1],Wm),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=x);let _=[t[0],x],$=t.length===3;$&&(!o&&t[2].dims.length===1?_.push(t[2].reshape([t[2].dims[0],1,1])):_.push(t[2])),e.compute(Vu(_,n,i,h,w,g,$,b),{inputs:_})},Gm=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=t.kernelShape;(o.length===0||o[0]===0)&&(o=[e.inputs[1].dims[2]]);let i=t.dilations;(i.length===0||i[0]===0)&&(i=[1]);let a=t.strides;(a.length===0||a[0]===0)&&(a=[1]);let l=t.pads;l.length===0&&(l=[0,0]),l=[0,l[0],0,l[1]],a=[1].concat(a),i=[1].concat(i),o=[1].concat(o);let d=Hu({...t,pads:l,strides:a,dilations:i,kernelShape:o},n);e.compute(bo(n,d,p=>r?[p[0],p[2],p[3]]:[p[0],p[1],p[3]]))},Lu=(e,t)=>{Nm(e.inputs,t),e.inputs[0].dims.length===3?Gm(e,t):Hm(e,e.inputs,t)}});var Lm,qu,ju,Ku=U(()=>{"use strict";Q();ae();Ce();ie();Lm=(e,t,r,n)=>{let o=k.size(t),i=t.length,a=E("input",e,i),l=M("output",e,i),d=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),p=k.normalizeAxis(d,i),m=u=>{let h=` i32(${a.indicesGet("inputIndices","uniforms.axis")}) `,w=G("uniforms.input_shape","uniforms.axis",i),g=n.reverse?h+(n.exclusive?" + 1":""):"0",b=n.reverse?w:h+(n.exclusive?"":" + 1");return`
5179
+ ${i?S:I}}`},fo=(e,t,r)=>{let n=e.length>2,o=t.outputShape,i=E.size(o),a=[Math.ceil(i/64),1,1];be("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${a}`);let l=t.format==="NHWC",d=["rank","rank"],p=[t.strides[0],t.strides[1]],m=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],u=[t.dilations[0],t.dilations[1]],h=[m[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),m[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],w=[h[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),h[1]-1-Math.floor(t.pads[1]+t.pads[3])/2],g=!1,y=t.group,x=e[1].dims,_=x[0]/y,$=x[1],S=[{type:12,data:i},{type:12,data:p},{type:12,data:m},{type:12,data:u},{type:12,data:h},{type:6,data:w},{type:12,data:_},{type:12,data:$},...U(e[0].dims,e[1].dims)];n&&(S.push(...U(e[2].dims)),d.push("rank")),S.push(...U(o));let I=a[1]===1&&a[2]===1,T=A=>{let O=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:p.length},{name:"filter_dims",type:"u32",length:m.length},{name:"dilations",type:"u32",length:m.length},{name:"effective_filter_dims",type:"u32",length:h.length},{name:"pads",type:"i32",length:w.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],z=pe(e[0].dataType);return`${Pm(A,e,o,n,I,g,z,O,l)}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};`,inputDependencies:d},getRunData:()=>({dispatchGroup:{x:a[0],y:a[1],z:a[2]},outputs:[{dims:r?r(o):o,dataType:e[0].dataType}],programUniforms:S}),getShaderSource:T}}});var Om,zm,Bm,Du,Ru,Dm,Rm,Mm,Um,Mu,Uu=M(()=>{"use strict";zu();Bu();ut();Ct();Om=(e,t,r,n,o,i)=>(e-1)*t+r+(n-1)*o+1-i,zm=(e,t,r,n,o)=>{let i=Math.floor(e/2);t==="SAME_UPPER"?(r[n]=i,r[o]=e-i):t==="SAME_LOWER"&&(r[n]=e-i,r[o]=i)},Bm=(e,t,r,n,o,i,a,l,d,p)=>{let m=e.length-2,u=p.length===0;if(d.length===0)for(let g=0;g<m;++g)d.push(0);let h=e[0],w=t[l?3:1]*o;for(let g=0,y=e.length-m-(l?1:0);g<m;++g,++y){let x=e[y],_=u?x*a[g]:p[g],$=Om(x,a[g],i[g],t[y],r[g],_);zm($,n,i,g,g+m),u&&p.push(a[g]*(x-1)+d[g]+(t[y]-1)*r[g]+1-i[g]-i[g+m])}p.splice(0,0,h),p.splice(l?3:1,0,w)},Du=(e,t)=>{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((u,h)=>u*h,1)===0){r.length=0;for(let u=2;u<t[1].dims.length;++u)r.push(t[1].dims[u])}let n=e.format==="NHWC";r.splice(0,0,t[1].dims[0]),r.splice(n?3:1,0,t[1].dims[1]);let o=e.pads.slice(),i=e.outputShape.slice(),a=e.outputPadding.slice(),l=t[0].dims,d=e.dilations.slice();if(d.reduce((u,h)=>u+h,0)===0){let u=t[0].dims.length-2;d=new Array(u).fill(1)}let p=e.strides.slice();if(p.reduce((u,h)=>u+h,0)===0){let u=t[0].dims.length-2;p=new Array(u).fill(1)}Bm(l,r,d,e.autoPad,e.group,o,p,n,a,i);let m=Object.assign({},e);return Object.assign(m,{kernelShape:r,pads:o,outputPadding:a,outputShape:i,dilations:d,strides:p}),m},Ru=e=>{let t=Kr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],o=e.dilations,i=e.group,a=e.kernelShape,l=e.pads,d=e.strides,p=e.wIsConst(),m=e.outputPadding,u=e.outputShape;return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,outputPadding:m,outputShape:u,pads:l,strides:d,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},Dm=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[0];if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let o=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==o))throw new Error("invalid bias");let i=e[0].dims.length-2;if(t.dilations.reduce((m,u)=>m+u,0)>0&&t.dilations.length!==i)throw new Error(`dilations should be ${i}D`);if(t.strides.reduce((m,u)=>m+u,0)>0&&t.strides.length!==i)throw new Error(`strides should be ${i}D`);if(t.pads.reduce((m,u)=>m+u,0)>0&&t.pads.length!==i*2)throw new Error(`pads should be ${i*2}D`);if(t.outputPadding.length!==i&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${i}D`);if(t.kernelShape.reduce((m,u)=>m+u,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Rm=[2,3,1,0],Mm=(e,t,r)=>{let n=Du(r,t),o=r.format==="NHWC",i=n.outputShape,a=i[o?3:1],l=t[0].dims[o?3:1];if(n.group!==1||a===1&&l===1){e.compute(fo(t,n));return}let d=i[o?1:2],p=i[o?2:3],m=t[1].dims[2],u=t[1].dims[3],h=o?d*p:a,w=o?a:d*p,g=m*u*l,y=!0,x=e.kernelCustomData.wT??e.compute(Re(t[1],Rm),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=x);let _=[t[0],x],$=t.length===3;$&&(!o&&t[2].dims.length===1?_.push(t[2].reshape([t[2].dims[0],1,1])):_.push(t[2])),e.compute(Ou(_,n,i,h,w,g,$,y),{inputs:_})},Um=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=t.kernelShape;(o.length===0||o[0]===0)&&(o=[e.inputs[1].dims[2]]);let i=t.dilations;(i.length===0||i[0]===0)&&(i=[1]);let a=t.strides;(a.length===0||a[0]===0)&&(a=[1]);let l=t.pads;l.length===0&&(l=[0,0]),l=[0,l[0],0,l[1]],a=[1].concat(a),i=[1].concat(i),o=[1].concat(o);let d=Du({...t,pads:l,strides:a,dilations:i,kernelShape:o},n);e.compute(fo(n,d,p=>r?[p[0],p[2],p[3]]:[p[0],p[1],p[3]]))},Mu=(e,t)=>{Dm(e.inputs,t),e.inputs[0].dims.length===3?Um(e,t):Mm(e,e.inputs,t)}});var Vm,Vu,Nu,Wu=M(()=>{"use strict";X();oe();xe();re();Vm=(e,t,r,n)=>{let o=E.size(t),i=t.length,a=k("input",e,i),l=D("output",e,i),d=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),p=E.normalizeAxis(d,i),m=u=>{let h=` i32(${a.indicesGet("inputIndices","uniforms.axis")}) `,w=L("uniforms.input_shape","uniforms.axis",i),g=n.reverse?h+(n.exclusive?" + 1":""):"0",y=n.reverse?w:h+(n.exclusive?"":" + 1");return`
5181
5180
  ${u.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(a,l)}
5182
5181
  ${u.mainStart()}
5183
5182
  ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
5184
5183
  var inputIndices = ${l.offsetToIndices("global_idx")};
5185
5184
  var sum = ${l.type.value}(0);
5186
5185
  let first : i32 = ${g};
5187
- let last : i32 = ${b};
5186
+ let last : i32 = ${y};
5188
5187
  for (var i : i32 = first; i < last; i++) {
5189
5188
  ${a.indicesSet("inputIndices","uniforms.axis","u32(i)")};
5190
5189
  sum = sum + ${a.getByIndices("inputIndices")};
5191
5190
  }
5192
5191
  ${l.setByOffset("global_idx","sum")};
5193
- }`};return{name:"CumSum",shaderCache:{hint:n.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},{type:12,data:p},...V(t,t)]}),getShaderSource:m}},qu=(e,t)=>{let r=e.inputs[0].dims,n=e.inputs[0].dataType,o=e.inputs[1];e.compute(Lm(n,r,o,t),{inputs:[0]})},ju=e=>{let t=e.exclusive===1,r=e.reverse===1;return te({exclusive:t,reverse:r})}});var Fm,qm,jm,Yu,Xu,Zu=U(()=>{"use strict";Q();ae();Ce();ie();Fm=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},qm=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} {
5192
+ }`};return{name:"CumSum",shaderCache:{hint:n.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},{type:12,data:p},...U(t,t)]}),getShaderSource:m}},Vu=(e,t)=>{let r=e.inputs[0].dims,n=e.inputs[0].dataType,o=e.inputs[1];e.compute(Vm(n,r,o,t),{inputs:[0]})},Nu=e=>{let t=e.exclusive===1,r=e.reverse===1;return Q({exclusive:t,reverse:r})}});var Nm,Wm,Lm,Lu,Hu,Gu=M(()=>{"use strict";X();oe();xe();re();Nm=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},Wm=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} {
5194
5193
  var a: ${r.type.indices};`);for(let i=0;i<t;++i)o.push(r.indicesSet("a",e[i],`i[${i}]`));return o.push("return a;}"),o.join(`
5195
- `)},jm=(e,t)=>{let r,n,o,i,a,l,d=t.format==="NHWC",p=t.blocksize,m=t.mode==="DCR";d?([r,n,o,i]=e.dims,a=m?[r,n,o,p,p,i/p**2]:[r,n,o,i/p**2,p,p],l=m?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,n,o,i]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],a=m?[r,p,p,i/p**2,n,o]:[r,i/p**2,p,p,n,o],l=m?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let u=e.reshape(a),h=u.dims.length,w=e.dataType,g=E("a",w,h),b=M("output",w,h),x=_=>`
5196
- ${_.registerUniform("output_size","u32").declareVariables(g,b)}
5194
+ `)},Lm=(e,t)=>{let r,n,o,i,a,l,d=t.format==="NHWC",p=t.blocksize,m=t.mode==="DCR";d?([r,n,o,i]=e.dims,a=m?[r,n,o,p,p,i/p**2]:[r,n,o,i/p**2,p,p],l=m?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,n,o,i]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],a=m?[r,p,p,i/p**2,n,o]:[r,i/p**2,p,p,n,o],l=m?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let u=e.reshape(a),h=u.dims.length,w=e.dataType,g=k("a",w,h),y=D("output",w,h),x=_=>`
5195
+ ${_.registerUniform("output_size","u32").declareVariables(g,y)}
5197
5196
 
5198
- ${qm(l,h,g,b)}
5197
+ ${Wm(l,h,g,y)}
5199
5198
 
5200
5199
  ${_.mainStart()}
5201
5200
  ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
5202
5201
 
5203
- let indices = ${b.offsetToIndices("global_idx")};
5202
+ let indices = ${y.offsetToIndices("global_idx")};
5204
5203
  let aIndices = perm(indices);
5205
5204
 
5206
- ${b.setByOffset("global_idx",g.getByIndices("aIndices"))}
5207
- }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:_=>{let $=d?[r,n*p,o*p,i/p**2]:[r,i/p**2,n*p,o*p],S=k.size($),I=u.dims,T=k.sortBasedOnPerm(I,l);return{outputs:[{dims:$,dataType:_[0].dataType}],dispatchGroup:{x:Math.ceil(S/64)},programUniforms:[{type:12,data:S},...V(I,T)]}},getShaderSource:x}},Yu=(e,t)=>{Fm(e.inputs),e.compute(jm(e.inputs[0],t))},Xu=e=>te({blocksize:e.blocksize,mode:e.mode,format:e.format})});var wo,rn,Qu,Km,Ym,vo,$o,Ju,Xm,ed,td,rd=U(()=>{"use strict";Q();ae();Ce();ie();wo="[a-zA-Z]|\\.\\.\\.",rn="("+wo+")+",Qu="^"+rn+"$",Km="("+rn+",)*"+rn,Ym="^"+Km+"$",vo=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let n=this.symbolToIndices.get(t);n===void 0?n=[r]:n.push(r),this.symbolToIndices.set(t,n)}},$o=class{constructor(t,r){this.equation=r;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,o]=r.includes("->")?r.split("->",2):[r,""];if(!n.match(RegExp(Ym)))throw new Error("Invalid LHS term");if(n.split(",").forEach((l,d)=>{let p=t[d].dims.slice();if(!l.match(RegExp(Qu)))throw new Error("Invalid LHS term");let m=this.processTerm(l,!0,p,d);this.lhs.push(m)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([l,d])=>d.count===1||l==="...").map(([l])=>l).join("");else if(!o.match(RegExp(rn)))throw new Error("Invalid RHS");o.match(RegExp(wo,"g"))?.forEach(l=>{if(l==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let d=this.symbolToInfo.get(l);if(d===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(d.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(t,r,n){let o=this.symbolToInfo.get(t);if(o!==void 0){if(o.dimValue!==r&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(n)}else o={count:1,dimValue:r,inputIndices:[n]};this.symbolToInfo.set(t,o)}processTerm(t,r,n,o=-1){let i=n.length,a=!1,l=[],d=0;if(!t.match(RegExp(Qu))&&!r&&t!=="")throw new Error("Invalid LHS term");let p=t.match(RegExp(wo,"g")),m=new vo(o);return p?.forEach((u,h)=>{if(u==="..."){if(a)throw new Error("Only one ellipsis is allowed per input term");a=!0;let w=i-p.length+1;if(w<0)throw new Error("Ellipsis out of bounds");if(l=n.slice(d,d+w),this.hasEllipsis){if(this.ellipsisDims.length!==l.length||this.ellipsisDims.toString()!==l.toString())throw new Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=l;else throw new Error("Ellipsis must be specified in the LHS");for(let g=0;g<l.length;g++){let b=String.fromCharCode("0".charCodeAt(0)+g);m.addSymbol(b,h+g),this.addSymbol(b,n[d++],o)}}else m.addSymbol(u,h+(this.hasEllipsis?this.ellipsisDims.length-1:0)),this.addSymbol(u,n[d++],o)}),m}},Ju=e=>e+"_max",Xm=(e,t,r,n)=>{let i=e.map(m=>m.length).map((m,u)=>E(`input${u}`,t,m)),a=k.size(n),l=M("output",t,n.length),d=[...r.symbolToInfo.keys()].filter(m=>!r.rhs.symbolToIndices.has(m)),p=m=>{let u=[],h="var prod = 1.0;",w="var sum = 0.0;",g="sum += prod;",b=[],x=[],_=[],$=[],S=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((T,A)=>{if(r.rhs.symbolToIndices.has(A)){let D=r.rhs.symbolToIndices.get(A)?.[0];D!==void 0&&r.lhs.forEach((z,H)=>{if(T.inputIndices.includes(H)){let W=z.symbolToIndices.get(A);if(W===void 0)throw new Error("Invalid symbol error");W.forEach(X=>{u.push(`${i[H].indicesSet(`input${H}Indices`,X,l.indicesGet("outputIndices",D))}`)})}})}else r.lhs.forEach((D,z)=>{if(T.inputIndices.includes(z)){let H=D.symbolToIndices.get(A);if(H===void 0)throw new Error("Invalid symbol error");H.forEach(W=>{b.push(`${i[z].indicesSet(`input${z}Indices`,W,`${A}`)}`)}),$.push(`prod *= ${i[z].getByIndices(`input${z}Indices`)};`)}}),x.push(`for(var ${A}: u32 = 0; ${A} < uniforms.${Ju(A)}; ${A}++) {`),_.push("}")});let I=S?[...u,`let sum = ${i.map((T,A)=>T.getByIndices(`input${A}Indices`)).join(" * ")};`]:[...u,w,...x,...b,h,...$,g,..._];return`
5208
- ${m.registerUniforms(d.map(T=>({name:`${Ju(T)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...i,l)}
5205
+ ${y.setByOffset("global_idx",g.getByIndices("aIndices"))}
5206
+ }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:_=>{let $=d?[r,n*p,o*p,i/p**2]:[r,i/p**2,n*p,o*p],S=E.size($),I=u.dims,T=E.sortBasedOnPerm(I,l);return{outputs:[{dims:$,dataType:_[0].dataType}],dispatchGroup:{x:Math.ceil(S/64)},programUniforms:[{type:12,data:S},...U(I,T)]}},getShaderSource:x}},Lu=(e,t)=>{Nm(e.inputs),e.compute(Lm(e.inputs[0],t))},Hu=e=>Q({blocksize:e.blocksize,mode:e.mode,format:e.format})});var ho,Jr,Fu,Hm,Gm,go,yo,qu,Fm,Ku,ju,Yu=M(()=>{"use strict";X();oe();xe();re();ho="[a-zA-Z]|\\.\\.\\.",Jr="("+ho+")+",Fu="^"+Jr+"$",Hm="("+Jr+",)*"+Jr,Gm="^"+Hm+"$",go=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let n=this.symbolToIndices.get(t);n===void 0?n=[r]:n.push(r),this.symbolToIndices.set(t,n)}},yo=class{constructor(t,r){this.equation=r;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,o]=r.includes("->")?r.split("->",2):[r,""];if(!n.match(RegExp(Gm)))throw new Error("Invalid LHS term");if(n.split(",").forEach((l,d)=>{let p=t[d].dims.slice();if(!l.match(RegExp(Fu)))throw new Error("Invalid LHS term");let m=this.processTerm(l,!0,p,d);this.lhs.push(m)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([l,d])=>d.count===1||l==="...").map(([l])=>l).join("");else if(!o.match(RegExp(Jr)))throw new Error("Invalid RHS");o.match(RegExp(ho,"g"))?.forEach(l=>{if(l==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let d=this.symbolToInfo.get(l);if(d===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(d.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(t,r,n){let o=this.symbolToInfo.get(t);if(o!==void 0){if(o.dimValue!==r&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(n)}else o={count:1,dimValue:r,inputIndices:[n]};this.symbolToInfo.set(t,o)}processTerm(t,r,n,o=-1){let i=n.length,a=!1,l=[],d=0;if(!t.match(RegExp(Fu))&&!r&&t!=="")throw new Error("Invalid LHS term");let p=t.match(RegExp(ho,"g")),m=new go(o);return p?.forEach((u,h)=>{if(u==="..."){if(a)throw new Error("Only one ellipsis is allowed per input term");a=!0;let w=i-p.length+1;if(w<0)throw new Error("Ellipsis out of bounds");if(l=n.slice(d,d+w),this.hasEllipsis){if(this.ellipsisDims.length!==l.length||this.ellipsisDims.toString()!==l.toString())throw new Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=l;else throw new Error("Ellipsis must be specified in the LHS");for(let g=0;g<l.length;g++){let y=String.fromCharCode("0".charCodeAt(0)+g);m.addSymbol(y,h+g),this.addSymbol(y,n[d++],o)}}else m.addSymbol(u,h+(this.hasEllipsis?this.ellipsisDims.length-1:0)),this.addSymbol(u,n[d++],o)}),m}},qu=e=>e+"_max",Fm=(e,t,r,n)=>{let i=e.map(m=>m.length).map((m,u)=>k(`input${u}`,t,m)),a=E.size(n),l=D("output",t,n.length),d=[...r.symbolToInfo.keys()].filter(m=>!r.rhs.symbolToIndices.has(m)),p=m=>{let u=[],h="var prod = 1.0;",w="var sum = 0.0;",g="sum += prod;",y=[],x=[],_=[],$=[],S=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((T,A)=>{if(r.rhs.symbolToIndices.has(A)){let O=r.rhs.symbolToIndices.get(A)?.[0];O!==void 0&&r.lhs.forEach((z,W)=>{if(T.inputIndices.includes(W)){let V=z.symbolToIndices.get(A);if(V===void 0)throw new Error("Invalid symbol error");V.forEach(j=>{u.push(`${i[W].indicesSet(`input${W}Indices`,j,l.indicesGet("outputIndices",O))}`)})}})}else r.lhs.forEach((O,z)=>{if(T.inputIndices.includes(z)){let W=O.symbolToIndices.get(A);if(W===void 0)throw new Error("Invalid symbol error");W.forEach(V=>{y.push(`${i[z].indicesSet(`input${z}Indices`,V,`${A}`)}`)}),$.push(`prod *= ${i[z].getByIndices(`input${z}Indices`)};`)}}),x.push(`for(var ${A}: u32 = 0; ${A} < uniforms.${qu(A)}; ${A}++) {`),_.push("}")});let I=S?[...u,`let sum = ${i.map((T,A)=>T.getByIndices(`input${A}Indices`)).join(" * ")};`]:[...u,w,...x,...y,h,...$,g,..._];return`
5207
+ ${m.registerUniforms(d.map(T=>({name:`${qu(T)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...i,l)}
5209
5208
 
5210
5209
  ${m.mainStart()}
5211
5210
  ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
@@ -5215,12 +5214,12 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5215
5214
  ${I.join(`
5216
5215
  `)};
5217
5216
  ${l.setByOffset("global_idx","sum")};
5218
- }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let m=d.filter(h=>r.symbolToInfo.has(h)).map(h=>({type:12,data:r.symbolToInfo.get(h)?.dimValue||0}));m.push({type:12,data:a});let u=e.map((h,w)=>[...V(h)]).reduce((h,w)=>h.concat(w),m);return u.push(...V(n)),{outputs:[{dims:n,dataType:t}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:u}},getShaderSource:p}},ed=(e,t)=>{let r=new $o(e.inputs,t.equation),n=r.outputDims,o=e.inputs.map((i,a)=>i.dims);e.compute(Xm(o,e.inputs[0].dataType,r,n))},td=e=>{let t=e.equation.replace(/\s+/g,"");return te({equation:t})}});var Zm,nd,Qm,Jm,od,id=U(()=>{"use strict";Q();ae();ie();Zm=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=r.length<t.length?0:r.length-t.length,o=t.length<r.length?0:t.length-r.length;for(;n<r.length&&o<t.length;++n,++o)if(r[n]!==t[o]&&r[n]!==1&&t[o]!==1)throw new Error("Expand requires shape to be broadcastable to input")},nd=(e,t)=>{let r=e.length-t.length,n=[];for(let o=0;o<r;++o)n.push(e[o]);for(let o=0;o<t.length;++o)n.push(t[o]===1?e[o+r]:t[o]);return n},Qm=(e,t)=>e.length>t.length?nd(e,t):nd(t,e),Jm=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=Qm(t,r),o=e[0].dataType,i=o===9?4:1,a=Math.ceil(k.size(n)/i),l=p=>{let m=E("input",o,t.length,i),u=M("output",o,n.length,i),h;if(o===9){let w=(g,b,x="")=>`
5219
- let outputIndices${b} = ${u.offsetToIndices(`outputOffset + ${b}u`)};
5220
- let offset${b} = ${m.broadcastedIndicesToOffset(`outputIndices${b}`,u)};
5221
- let index${b} = offset${b} / 4u;
5222
- let component${b} = offset${b} % 4u;
5223
- ${g}[${b}] = ${x}(${m.getByOffset(`index${b}`)}[component${b}]);
5217
+ }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let m=d.filter(h=>r.symbolToInfo.has(h)).map(h=>({type:12,data:r.symbolToInfo.get(h)?.dimValue||0}));m.push({type:12,data:a});let u=e.map((h,w)=>[...U(h)]).reduce((h,w)=>h.concat(w),m);return u.push(...U(n)),{outputs:[{dims:n,dataType:t}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:u}},getShaderSource:p}},Ku=(e,t)=>{let r=new yo(e.inputs,t.equation),n=r.outputDims,o=e.inputs.map((i,a)=>i.dims);e.compute(Fm(o,e.inputs[0].dataType,r,n))},ju=e=>{let t=e.equation.replace(/\s+/g,"");return Q({equation:t})}});var qm,Xu,Km,jm,Zu,Qu=M(()=>{"use strict";X();oe();re();qm=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=r.length<t.length?0:r.length-t.length,o=t.length<r.length?0:t.length-r.length;for(;n<r.length&&o<t.length;++n,++o)if(r[n]!==t[o]&&r[n]!==1&&t[o]!==1)throw new Error("Expand requires shape to be broadcastable to input")},Xu=(e,t)=>{let r=e.length-t.length,n=[];for(let o=0;o<r;++o)n.push(e[o]);for(let o=0;o<t.length;++o)n.push(t[o]===1?e[o+r]:t[o]);return n},Km=(e,t)=>e.length>t.length?Xu(e,t):Xu(t,e),jm=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=Km(t,r),o=e[0].dataType,i=o===9?4:1,a=Math.ceil(E.size(n)/i),l=p=>{let m=k("input",o,t.length,i),u=D("output",o,n.length,i),h;if(o===9){let w=(g,y,x="")=>`
5218
+ let outputIndices${y} = ${u.offsetToIndices(`outputOffset + ${y}u`)};
5219
+ let offset${y} = ${m.broadcastedIndicesToOffset(`outputIndices${y}`,u)};
5220
+ let index${y} = offset${y} / 4u;
5221
+ let component${y} = offset${y} % 4u;
5222
+ ${g}[${y}] = ${x}(${m.getByOffset(`index${y}`)}[component${y}]);
5224
5223
  `;h=`
5225
5224
  let outputOffset = global_idx * ${i};
5226
5225
  var data = vec4<u32>(0);
@@ -5237,29 +5236,29 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5237
5236
  ${p.registerUniform("vec_size","u32").declareVariables(m,u)}
5238
5237
  ${p.mainStart()}
5239
5238
  ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
5240
- ${h}`},d=[{type:12,data:a},...V(t,n)];return{name:"Expand",shaderCache:{hint:`${n.length}`,inputDependencies:["rank"]},getShaderSource:l,getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:d})}},od=e=>{Zm(e.inputs),e.compute(Jm(e.inputs),{inputs:[0]})}});var ef,ad,sd=U(()=>{"use strict";Q();ae();ie();Yr();ef=e=>{let t=e[0].dataType,r=k.size(e[0].dims),n=k.size(e[1].dims),o=n%4===0,i=a=>{let l=E("x",t,[1],4),d=E("bias",t,[1],4),p=M("y",t,[1],4),m=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],u=w=>`
5239
+ ${h}`},d=[{type:12,data:a},...U(t,n)];return{name:"Expand",shaderCache:{hint:`${n.length}`,inputDependencies:["rank"]},getShaderSource:l,getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:d})}},Zu=e=>{qm(e.inputs),e.compute(jm(e.inputs),{inputs:[0]})}});var Ym,Ju,ed=M(()=>{"use strict";X();oe();re();qr();Ym=e=>{let t=e[0].dataType,r=E.size(e[0].dims),n=E.size(e[1].dims),o=n%4===0,i=a=>{let l=k("x",t,[1],4),d=k("bias",t,[1],4),p=D("y",t,[1],4),m=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],u=w=>`
5241
5240
  let bias${w}_offset: u32 = (global_idx * 4 + ${w}) % uniforms.bias_size;
5242
5241
  let bias${w} = ${d.getByOffset(`bias${w}_offset / 4`)}[bias${w}_offset % 4];`,h=o?`
5243
5242
  let bias = ${d.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${u(0)}${u(1)}${u(2)}${u(3)}
5244
5243
  let bias = ${l.type.value}(bias0, bias1, bias2, bias3);`;return`${a.registerUniforms(m).declareVariables(l,d,p)}
5245
5244
 
5246
- ${ao(Oe(t))}
5245
+ ${ro(ke(t))}
5247
5246
 
5248
- ${a.mainStart(Tt)}
5247
+ ${a.mainStart(St)}
5249
5248
  ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")}
5250
5249
 
5251
5250
  let x = ${l.getByOffset("global_idx")};
5252
5251
  ${h}
5253
5252
  let x_in = x + bias;
5254
- ${p.setByOffset("global_idx",so("x_in"))}
5255
- }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${o}`,inputDependencies:["type","type"]},getShaderSource:i,getRunData:a=>({outputs:[{dims:a[0].dims,dataType:a[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:n}],dispatchGroup:{x:Math.ceil(r/Tt/4)}})}},ad=e=>{e.inputs.length<2||k.size(e.inputs[1].dims)===0?su(e):e.compute(ef(e.inputs))}});var tf,rf,ud,dd,ld=U(()=>{"use strict";Q();ae();Ce();ie();tf=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},rf=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r.length,i=k.normalizeAxis(t.axis,o),a=r.slice(0);a.splice(i,1,...n);let l=r[i],d=e[0].dataType===9?4:1,p=Math.ceil(k.size(a)/d),m=[{type:12,data:p},{type:6,data:l},{type:12,data:i},...V(e[0].dims,e[1].dims,a)],u=h=>{let w=E("data",e[0].dataType,e[0].dims.length,d),g=E("inputIndices",e[1].dataType,e[1].dims.length),b=M("output",e[0].dataType,a.length,d),x=$=>{let S=n.length,I=`var indicesIndices${$} = ${g.type.indices}(0);`;for(let T=0;T<S;T++)I+=`${S>1?`indicesIndices${$}[${T}]`:`indicesIndices${$}`} = ${a.length>1?`outputIndices${$}[uniforms.axis + ${T}]`:`outputIndices${$}`};`;I+=`
5253
+ ${p.setByOffset("global_idx",no("x_in"))}
5254
+ }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${o}`,inputDependencies:["type","type"]},getShaderSource:i,getRunData:a=>({outputs:[{dims:a[0].dims,dataType:a[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:n}],dispatchGroup:{x:Math.ceil(r/St/4)}})}},Ju=e=>{e.inputs.length<2||E.size(e.inputs[1].dims)===0?eu(e):e.compute(Ym(e.inputs))}});var Xm,Zm,td,rd,nd=M(()=>{"use strict";X();oe();xe();re();Xm=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Zm=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r.length,i=E.normalizeAxis(t.axis,o),a=r.slice(0);a.splice(i,1,...n);let l=r[i],d=e[0].dataType===9?4:1,p=Math.ceil(E.size(a)/d),m=[{type:12,data:p},{type:6,data:l},{type:12,data:i},...U(e[0].dims,e[1].dims,a)],u=h=>{let w=k("data",e[0].dataType,e[0].dims.length,d),g=k("inputIndices",e[1].dataType,e[1].dims.length),y=D("output",e[0].dataType,a.length,d),x=$=>{let S=n.length,I=`var indicesIndices${$} = ${g.type.indices}(0);`;for(let T=0;T<S;T++)I+=`${S>1?`indicesIndices${$}[${T}]`:`indicesIndices${$}`} = ${a.length>1?`outputIndices${$}[uniforms.axis + ${T}]`:`outputIndices${$}`};`;I+=`
5256
5255
  var idx${$} = ${g.getByIndices(`indicesIndices${$}`)};
5257
5256
  if (idx${$} < 0) {
5258
5257
  idx${$} = idx${$} + uniforms.axisDimLimit;
5259
5258
  }
5260
5259
  var dataIndices${$} : ${w.type.indices};
5261
5260
  `;for(let T=0,A=0;T<o;T++)T===i?(I+=`${o>1?`dataIndices${$}[${T}]`:`dataIndices${$}`} = u32(idx${$});`,A+=S):(I+=`${o>1?`dataIndices${$}[${T}]`:`dataIndices${$}`} = ${a.length>1?`outputIndices${$}[${A}]`:`outputIndices${$}`};`,A++);return I},_;if(e[0].dataType===9){let $=(S,I,T="")=>`
5262
- let outputIndices${I} = ${b.offsetToIndices(`outputOffset + ${I}u`)};
5261
+ let outputIndices${I} = ${y.offsetToIndices(`outputOffset + ${I}u`)};
5263
5262
  ${x(I)};
5264
5263
  let offset${I} = ${w.indicesToOffset(`dataIndices${I}`)};
5265
5264
  let index${I} = offset${I} / 4u;
@@ -5272,19 +5271,19 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5272
5271
  ${$("value",1,"u32")}
5273
5272
  ${$("value",2,"u32")}
5274
5273
  ${$("value",3,"u32")}
5275
- ${b.setByOffset("global_idx","value")}
5274
+ ${y.setByOffset("global_idx","value")}
5276
5275
  `}else _=`
5277
- let outputIndices = ${b.offsetToIndices("global_idx")};
5276
+ let outputIndices = ${y.offsetToIndices("global_idx")};
5278
5277
  ${x("")};
5279
5278
  let value = ${w.getByIndices("dataIndices")};
5280
- ${b.setByOffset("global_idx","value")};
5279
+ ${y.setByOffset("global_idx","value")};
5281
5280
  `;return`
5282
- ${h.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(w,g,b)}
5281
+ ${h.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(w,g,y)}
5283
5282
  ${h.mainStart()}
5284
5283
  ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
5285
5284
  ${_}
5286
- }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:m}),getShaderSource:u}},ud=e=>te({axis:e.axis}),dd=(e,t)=>{let r=e.inputs;tf(r),e.compute(rf(e.inputs,t))}});var nf,of,cd,pd,md=U(()=>{"use strict";Q();ae();Ce();ie();nf=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and
5287
- indices input tensors be of same rank.`)},of=(e,t)=>{let r=e[0].dims,n=e[0].dataType,o=r.length,i=e[1].dims,a=e[1].dataType,l=k.normalizeAxis(t.axis,o),d=r[l],p=i.slice(0),m=k.size(p),u=E("input",n,o),h=E("indicesInput",a,i.length),w=M("output",n,p.length),g=[{type:12,data:m},{type:6,data:d},{type:12,data:l}];return g.push(...V(r,i,p)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:g}),getShaderSource:_=>`
5285
+ }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:m}),getShaderSource:u}},td=e=>Q({axis:e.axis}),rd=(e,t)=>{let r=e.inputs;Xm(r),e.compute(Zm(e.inputs,t))}});var Qm,Jm,od,id,ad=M(()=>{"use strict";X();oe();xe();re();Qm=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and
5286
+ indices input tensors be of same rank.`)},Jm=(e,t)=>{let r=e[0].dims,n=e[0].dataType,o=r.length,i=e[1].dims,a=e[1].dataType,l=E.normalizeAxis(t.axis,o),d=r[l],p=i.slice(0),m=E.size(p),u=k("input",n,o),h=k("indicesInput",a,i.length),w=D("output",n,p.length),g=[{type:12,data:m},{type:6,data:d},{type:12,data:l}];return g.push(...U(r,i,p)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:g}),getShaderSource:_=>`
5288
5287
  ${_.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(u,h,w)}
5289
5288
  ${_.mainStart()}
5290
5289
  ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
@@ -5300,7 +5299,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5300
5299
  let value = ${u.getByIndices("inputIndices")};
5301
5300
 
5302
5301
  ${w.setByOffset("global_idx","value")};
5303
- }`}},cd=e=>te({axis:e.axis}),pd=(e,t)=>{let r=e.inputs;nf(r),e.compute(of(e.inputs,t))}});var af,sf,fd,hd,gd=U(()=>{"use strict";Q();ae();ie();af=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},sf=(e,t)=>{let r=e[0].dims.slice(),n=e[1].dims.slice(),[o,i,a]=Wr.getShapeOfGemmResult(r,t.transA,n,t.transB,e.length===3?e[2].dims:void 0),l=[o,i];if(!l)throw new Error("Can't use gemm on the given tensors");let d=k.size(l),p=[{type:12,data:d},{type:12,data:o},{type:12,data:i},{type:12,data:a},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(p.push(...V(e[2].dims)),m.push("rank")),p.push(...V(l));let u=h=>{let w="";t.transA&&t.transB?w="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?w="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?w="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(w="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let g=t.alpha===1?"":"value *= uniforms.alpha;",b=E("a",e[0].dataType,e[0].dims),x=E("b",e[1].dataType,e[1].dims),_=b.type.value,$=null,S=[b,x];e.length===3&&($=E("c",e[2].dataType,e[2].dims.length),S.push($));let I=M("output",e[0].dataType,l.length);S.push(I);let T=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return`
5302
+ }`}},od=e=>Q({axis:e.axis}),id=(e,t)=>{let r=e.inputs;Qm(r),e.compute(Jm(e.inputs,t))}});var ef,tf,sd,ud,dd=M(()=>{"use strict";X();oe();re();ef=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},tf=(e,t)=>{let r=e[0].dims.slice(),n=e[1].dims.slice(),[o,i,a]=Ur.getShapeOfGemmResult(r,t.transA,n,t.transB,e.length===3?e[2].dims:void 0),l=[o,i];if(!l)throw new Error("Can't use gemm on the given tensors");let d=E.size(l),p=[{type:12,data:d},{type:12,data:o},{type:12,data:i},{type:12,data:a},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(p.push(...U(e[2].dims)),m.push("rank")),p.push(...U(l));let u=h=>{let w="";t.transA&&t.transB?w="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?w="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?w="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(w="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let g=t.alpha===1?"":"value *= uniforms.alpha;",y=k("a",e[0].dataType,e[0].dims),x=k("b",e[1].dataType,e[1].dims),_=y.type.value,$=null,S=[y,x];e.length===3&&($=k("c",e[2].dataType,e[2].dims.length),S.push($));let I=D("output",e[0].dataType,l.length);S.push(I);let T=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return`
5304
5303
  ${h.registerUniforms(T).declareVariables(...S)}
5305
5304
 
5306
5305
  ${h.mainStart()}
@@ -5317,14 +5316,14 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5317
5316
  ${g}
5318
5317
  ${(()=>$!=null?`let cOffset = ${$.broadcastedIndicesToOffset("vec2(m, n)",I)}; value += ${_}(uniforms.beta) * ${$.getByOffset("cOffset")};`:"")()}
5319
5318
  output[global_idx] = value;
5320
- }`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:l,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p}),getShaderSource:u}},fd=e=>{let t=e.transA,r=e.transB,n=e.alpha,o=e.beta;return{transA:t,transB:r,alpha:n,beta:o,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},hd=(e,t)=>{af(e.inputs),e.compute(sf(e.inputs,t))}});var Ve,lf,bd,yd,cf,nr,wd,_o=U(()=>{"use strict";Q();ae();Ce();Nr();jr();ie();Et();Ve=(e,t)=>e.length>t&&e[t].dims.length>0&&k.size(e[t].dims)>0?e[t]:void 0,lf=(e,t)=>{let r=e[0],n=Ve(e,1),o=Ve(e,2),i=Ve(e,3),a=Ve(e,4),l=Ve(e,5),d=Ve(e,6),p=Ve(e,7);if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let m=!1,u=r.dims[0],h=r.dims[1],w=r.dims.length===3?m?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],g=h,b=0,x=0,_=Math.floor(w/t.numHeads);if(d&&p){if(d.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims[0]!==u||d.dims[1]!==t.numHeads||d.dims[3]!==_)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(p.dims[0]!==u||p.dims[1]!==t.numHeads||p.dims[3]!==_)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[2]!==p.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(p.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');b=d.dims[2],x=d.dims[2]}else if(d||p)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $;if(n){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(n.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');$=2,g=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==_)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');$=5,g=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==_)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');$=0,g=n.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}if(i){if(i.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(o&&r.dims.length===5&&r.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let S=0;if(a){S=8;let z=a.dims;throw z.length===1?z[0]===u?S=1:z[0]===3*u+2&&(S=3):z.length===2&&z[0]===u&&z[1]===g&&(S=5),S===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, kv_sequence_length)'):new Error("Mask not supported")}let I=!1,T=w;if(o){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(g!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=o.dims[2]}else{if(g!==o.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');T=o.dims[1]*o.dims[3],I=!0}}let A=b+g,D=!1;if(a)throw new Error("Key padding mask is not supported");if(l){if(l.dims.length!==4)throw new Error('Input "relative_position_bias" is expected to have 4 dimensions');if(l.dims[0]!==u&&l.dims[0]!==1||l.dims[1]!==t.numHeads||l.dims[2]!==h||l.dims[3]!==A)throw new Error('Input "relative_position_bias" shape (batch_size, 1, sequence_length, kv_sequence_length)')}return{batchSize:u,sequenceLength:h,pastSequenceLength:b,kvSequenceLength:g,totalSequenceLength:A,maxSequenceLength:x,inputHiddenSize:0,hiddenSize:w,vHiddenSize:T,headSize:_,vHeadSize:Math.floor(T/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:S,scale:t.scale,broadcastResPosBias:D,passPastInKv:I,qkvFormat:$}},bd=e=>te({...e}),yd=te({perm:[0,2,1,3]}),cf=(e,t,r,n,o,i,a)=>{let l=[n,o,i],d=k.size(l),p=[{type:12,data:d},{type:12,data:a},{type:12,data:i}],m=u=>{let h=M("qkv_with_bias",t.dataType,l),w=E("qkv",t.dataType,l),g=E("bias",r.dataType,l),b=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return`
5321
- ${u.registerUniforms(b).declareVariables(w,g,h)}
5319
+ }`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:l,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p}),getShaderSource:u}},sd=e=>{let t=e.transA,r=e.transB,n=e.alpha,o=e.beta;return{transA:t,transB:r,alpha:n,beta:o,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},ud=(e,t)=>{ef(e.inputs),e.compute(tf(e.inputs,t))}});var Me,of,cd,ld,af,er,pd,bo=M(()=>{"use strict";X();oe();xe();Mr();Gr();re();Ct();Me=(e,t)=>e.length>t&&e[t].dims.length>0&&E.size(e[t].dims)>0?e[t]:void 0,of=(e,t)=>{let r=e[0],n=Me(e,1),o=Me(e,2),i=Me(e,3),a=Me(e,4),l=Me(e,5),d=Me(e,6),p=Me(e,7);if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let m=r.dims[0],u=r.dims[1],h=r.dims.length===3?r.dims[2]:t.numHeads*r.dims[4],w=u,g=0,y=0,x=Math.floor(h/t.numHeads);if(d&&p){if(d.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims[0]!==m||d.dims[1]!==t.numHeads||d.dims[3]!==x)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(p.dims[0]!==m||p.dims[1]!==t.numHeads||p.dims[3]!==x)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[2]!==p.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(p.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');g=d.dims[2],y=d.dims[2]}else if(d||p)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let _;if(n){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(n.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');_=2,w=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==x)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');_=5,w=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==x)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');_=0,w=n.dims[2]}}else{if(r.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(r.dims[2]!==t.numHeads||r.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');_=3}if(i){if(i.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(n&&n.dims.length===5&&n.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let $=g+w,S=0;if(a){S=8;let O=a.dims;throw O.length===1?O[0]===m?S=1:O[0]===3*m+2&&(S=3):O.length===2&&O[0]===m&&O[1]===$&&(S=5),S===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let I=!1,T=h;if(o){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(w!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=o.dims[2]}else{if(w!==o.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');T=o.dims[1]*o.dims[3],I=!0}}let A=!1;if(a)throw new Error("Key padding mask is not supported");if(l){if(l.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(l.dims[0]!==m||l.dims[1]!==t.numHeads||l.dims[2]!==u||l.dims[3]!==$)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:m,sequenceLength:u,pastSequenceLength:g,kvSequenceLength:w,totalSequenceLength:$,maxSequenceLength:y,inputHiddenSize:0,hiddenSize:h,vHiddenSize:T,headSize:x,vHeadSize:Math.floor(T/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:S,scale:t.scale,broadcastResPosBias:A,passPastInKv:I,qkvFormat:_}},cd=e=>Q({...e}),ld=Q({perm:[0,2,1,3]}),af=(e,t,r,n,o,i,a)=>{let l=[n,o,i],d=E.size(l),p=[{type:12,data:d},{type:12,data:a},{type:12,data:i}],m=u=>{let h=D("qkv_with_bias",t.dataType,l),w=k("qkv",t.dataType,l),g=k("bias",r.dataType,l),y=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return`
5320
+ ${u.registerUniforms(y).declareVariables(w,g,h)}
5322
5321
  ${u.mainStart()}
5323
5322
  ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
5324
5323
  let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset;
5325
5324
 
5326
5325
  qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx];
5327
- }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p}),getShaderSource:m},{inputs:[t,r],outputs:[-1]})[0]},nr=(e,t,r,n,o,i,a,l)=>{let d=i;if(a){if(n===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return d=cf(e,i,a,t,n,r*o,l),d=d.reshape([t,n,r,o]),e.compute(Ue(d,yd.perm),{inputs:[d],outputs:[-1]})[0]}else return i.dims.length===3&&(d=i.reshape([t,n,r,o])),e.compute(Ue(d,yd.perm),{inputs:[d],outputs:[-1]})[0]},wd=(e,t)=>{let r=lf(e.inputs,t),n=e.inputs[0],o=Ve(e.inputs,1),i=Ve(e.inputs,2),a=Ve(e.inputs,3),l=Ve(e.inputs,4),d=Ve(e.inputs,5),p=Ve(e.inputs,6),m=Ve(e.inputs,7);if(n.dims.length===5)throw new Error("Packed QKV is not implemented");if(o?.dims.length===5)throw new Error("Packed KV is not implemented");let u=o&&i&&o.dims.length===4&&i.dims.length===4,h=nr(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,n,a,0);if(u)return Wt(e,h,o,i,l,void 0,p,m,d,r,t);if(!o||!i)throw new Error("key and value must be provided");let w=nr(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,o,a,r.hiddenSize),g=nr(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,i,a,2*r.hiddenSize);Wt(e,h,w,g,l,void 0,p,m,d,r,t)}});var vd,pf,mf,xo,$d,So=U(()=>{"use strict";Q();ae();ie();vd=e=>Array.from(e.getBigInt64Array(),Number),pf=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(vd(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},mf=(e,t)=>{let r=[];for(let n=0;n<e.length;++n)r.push(e[n]*t[n]);return r},xo=(e,t)=>{let r=e[0].dims,n=t??vd(e[1]),o=mf(r,n),i=k.size(o),a=e[0].dataType,l=E("input",a,r.length),d=M("output",a,o.length),p=m=>`
5326
+ }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p}),getShaderSource:m},{inputs:[t,r],outputs:[-1]})[0]},er=(e,t,r,n,o,i,a,l)=>{let d=i;if(a){if(n===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return d=af(e,i,a,t,n,r*o,l),d=d.reshape([t,n,r,o]),e.compute(Re(d,ld.perm),{inputs:[d],outputs:[-1]})[0]}else return i.dims.length===3&&(d=i.reshape([t,n,r,o])),e.compute(Re(d,ld.perm),{inputs:[d],outputs:[-1]})[0]},pd=(e,t)=>{let r=of(e.inputs,t),n=e.inputs[0],o=Me(e.inputs,1),i=Me(e.inputs,2),a=Me(e.inputs,3),l=Me(e.inputs,4),d=Me(e.inputs,5),p=Me(e.inputs,6),m=Me(e.inputs,7);if(n.dims.length===5)throw new Error("Packed QKV is not implemented");if(o?.dims.length===5)throw new Error("Packed KV is not implemented");let u=o&&i&&o.dims.length===4&&i.dims.length===4,h=er(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,n,a,0);if(u)return Ut(e,h,o,i,l,void 0,p,m,d,r,t);if(!o||!i)throw new Error("key and value must be provided");let w=er(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,o,a,r.hiddenSize),g=er(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,i,a,2*r.hiddenSize);Ut(e,h,w,g,l,void 0,p,m,d,r,t)}});var md,sf,uf,wo,fd,vo=M(()=>{"use strict";X();oe();re();md=e=>Array.from(e.getBigInt64Array(),Number),sf=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(md(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},uf=(e,t)=>{let r=[];for(let n=0;n<e.length;++n)r.push(e[n]*t[n]);return r},wo=(e,t)=>{let r=e[0].dims,n=t??md(e[1]),o=uf(r,n),i=E.size(o),a=e[0].dataType,l=k("input",a,r.length),d=D("output",a,o.length),p=m=>`
5328
5327
  const inputShape = ${l.indices(...r)};
5329
5328
  ${m.registerUniform("output_size","u32").declareVariables(l,d)}
5330
5329
  ${m.mainStart()}
@@ -5338,7 +5337,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5338
5337
  ${l.indicesSet("input_indices","i","input_dim_value")}
5339
5338
  }
5340
5339
  ${d.setByOffset("global_idx",l.getByIndices("input_indices"))}
5341
- }`;return{name:"Tile",shaderCache:{hint:`${n}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...V(e[0].dims,o)]}),getShaderSource:p}},$d=e=>{pf(e.inputs),e.compute(xo(e.inputs),{inputs:[0]})}});var ff,_d,Sd,hf,xd,Id,Cd=U(()=>{"use strict";Q();ae();Ce();jr();ie();_o();So();Et();ff=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4];if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let l=!1,d=r.dims[0],p=r.dims[1],m=r.dims.length===3?l?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],u=p,h=0,w=0,g=Math.floor(m/t.numHeads),b=i&&i.dims.length!==0,x=a&&a.dims.length!==0,_=!0;if(b&&x){if(i.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(a.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');_?(h=i.dims[1],w=i.dims[1]):(h=i.dims[2],w=i.dims[2])}else if(b||x)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $;if(n){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(r.dims[2]%n.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');$=2,u=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==g)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');$=5,u=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==g)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');$=0,u=n.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}let S=0,I=!1,T=m;if(o){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(u!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=o.dims[2]}else{if(u!==o.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');T=o.dims[1]*o.dims[3],I=!0}}let A=h+u,D=!1;return{batchSize:d,sequenceLength:p,pastSequenceLength:h,kvSequenceLength:u,totalSequenceLength:A,maxSequenceLength:w,inputHiddenSize:0,hiddenSize:m,vHiddenSize:T,headSize:g,vHeadSize:Math.floor(T/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:S,scale:t.scale,broadcastResPosBias:D,passPastInKv:I,qkvFormat:$,isPastkvBSNH:_}},_d=(e,t,r,n)=>{let o=[n.batchSize,n.totalSequenceLength,n.kvNumHeads,n.headSize],i=4,a=k.size(o)/i,l=n.totalSequenceLength,d=M("present_kv",r,o.length,i),p=E("new_kv",e.dataType,e.dims.length,i),m=t?E("past_kv",t.dataType,t.dims.length,i):void 0,u=Math.ceil(n.headSize/i),h={x:l,y:e.dims[0],z:1},w=t?["rank","rank"]:["rank"],g=[{type:12,data:a},{type:12,data:n.pastSequenceLength},{type:12,data:n.kvSequenceLength},{type:12,data:n.totalSequenceLength}],b=[p];m?(g.push(...V(e.dims),...V(t.dims),...V(o)),b.push(m)):g.push(...V(e.dims),...V(o));let x=[{name:"output_size",type:"u32"},{name:"past_seqlen",type:"u32"},{name:"new_seqlen",type:"u32"},{name:"present_seqlen",type:"u32"}],_=` let past_batch_stride = uniforms.past_seqlen * num_heads * H;
5340
+ }`;return{name:"Tile",shaderCache:{hint:`${n}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...U(e[0].dims,o)]}),getShaderSource:p}},fd=e=>{sf(e.inputs),e.compute(wo(e.inputs),{inputs:[0]})}});var df,hd,yd,lf,gd,bd,wd=M(()=>{"use strict";X();oe();xe();Gr();re();bo();vo();Ct();df=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4];if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let l=!1,d=r.dims[0],p=r.dims[1],m=r.dims.length===3?l?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],u=p,h=0,w=0,g=Math.floor(m/t.numHeads),y=i&&i.dims.length!==0,x=a&&a.dims.length!==0,_=!0;if(y&&x){if(i.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(a.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');_?(h=i.dims[1],w=i.dims[1]):(h=i.dims[2],w=i.dims[2])}else if(y||x)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $;if(n){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(r.dims[2]%n.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');$=2,u=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==g)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');$=5,u=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==g)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');$=0,u=n.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}let S=0,I=!1,T=m;if(o){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(u!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=o.dims[2]}else{if(u!==o.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');T=o.dims[1]*o.dims[3],I=!0}}let A=h+u,O=!1;return{batchSize:d,sequenceLength:p,pastSequenceLength:h,kvSequenceLength:u,totalSequenceLength:A,maxSequenceLength:w,inputHiddenSize:0,hiddenSize:m,vHiddenSize:T,headSize:g,vHeadSize:Math.floor(T/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:S,scale:t.scale,broadcastResPosBias:O,passPastInKv:I,qkvFormat:$,isPastkvBSNH:_}},hd=(e,t,r,n)=>{let o=[n.batchSize,n.totalSequenceLength,n.kvNumHeads,n.headSize],i=4,a=E.size(o)/i,l=n.totalSequenceLength,d=D("present_kv",r,o.length,i),p=k("new_kv",e.dataType,e.dims.length,i),m=t?k("past_kv",t.dataType,t.dims.length,i):void 0,u=Math.ceil(n.headSize/i),h={x:l,y:e.dims[0],z:1},w=t?["rank","rank"]:["rank"],g=[{type:12,data:a},{type:12,data:n.pastSequenceLength},{type:12,data:n.kvSequenceLength},{type:12,data:n.totalSequenceLength}],y=[p];m?(g.push(...U(e.dims),...U(t.dims),...U(o)),y.push(m)):g.push(...U(e.dims),...U(o));let x=[{name:"output_size",type:"u32"},{name:"past_seqlen",type:"u32"},{name:"new_seqlen",type:"u32"},{name:"present_seqlen",type:"u32"}],_=` let past_batch_stride = uniforms.past_seqlen * num_heads * H;
5342
5341
  var past_head_stride = uniforms.past_seqlen * H;
5343
5342
  if (is_bsnh) {
5344
5343
  past_head_stride = H;
@@ -5356,7 +5355,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5356
5355
  ${$}
5357
5356
  }`,I=T=>`
5358
5357
 
5359
- ${T.registerUniforms(x).declareVariables(...b,d)}
5358
+ ${T.registerUniforms(x).declareVariables(...y,d)}
5360
5359
  ${T.mainStart([u,n.kvNumHeads,1])}
5361
5360
  ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
5362
5361
  var indices = ${d.offsetToIndices("global_idx")};
@@ -5384,7 +5383,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5384
5383
 
5385
5384
  let out_offset = b * present_batch_stride + s * row_stride + n * present_head_stride + h;
5386
5385
  ${S}
5387
- }`;return{name:"ConcatPastNew",shaderCache:{hint:`${n.kvNumHeads}${u}${!!t}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:o,dataType:r}],dispatchGroup:h,programUniforms:g}),getShaderSource:I}},Sd=e=>te({...e}),hf=te({perm:[0,2,1,3]}),xd=(e,t,r,n,o)=>{let i=t,a=n.kvNumHeads,l=n.nReps;return t.dims.length===3&&n.kvSequenceLength!==0&&(i=t.reshape([n.batchSize,n.kvSequenceLength,a,n.headSize])),r?i=e.compute(_d(i,r,i.dataType,n),{inputs:[i,r],outputs:[n.isPastkvBSNH?o:-1]})[0]:i=e.compute(_d(i,void 0,i.dataType,n),{inputs:[i],outputs:[n.isPastkvBSNH?o:-1]})[0],l!==1&&(i=e.compute(xo([i],[1,1,1,l]),{inputs:[i],outputs:[-1]})[0],i=i.reshape([n.batchSize,n.totalSequenceLength,a*l,n.headSize])),e.compute(Ue(i,hf.perm),{inputs:[i],outputs:[-1]})[0]},Id=(e,t)=>{let r=ff(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let n=nr(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,e.inputs[0],void 0,0),o=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,i=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,a=xd(e,e.inputs[1],o,r,1),l=xd(e,e.inputs[2],i,r,2);Wt(e,n,a,l,void 0,void 0,void 0,void 0,void 0,r,t)}});var gf,yf,bf,Td,Ad=U(()=>{"use strict";Q();ae();ie();gf=(e,t)=>{let r=e[0].dims,n=r,o=2,i=k.sizeToDimension(r,o),a=k.sizeFromDimension(r,o),l=$e(a),d=a/l,p=[r[0],r[1],d],m=["rank","type","type"],u=[{type:12,data:a},{type:12,data:d}];u.push(...V(p,p));let h=w=>{let g=E("x",e[0].dataType,p.length,l),b=E("scale",e[1].dataType,e[1].dims),x=E("bias",e[2].dataType,e[2].dims),_=M("output",e[0].dataType,p.length,l),$=[g,b,x,_],S=g.type.value,I=l===1?"f32":`vec${l}<f32>`,T=64,A=[{name:"normSize",type:"u32"},{name:"normPackedSize",type:"u32"}];return`
5386
+ }`;return{name:"ConcatPastNew",shaderCache:{hint:`${n.kvNumHeads}${u}${!!t}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:o,dataType:r}],dispatchGroup:h,programUniforms:g}),getShaderSource:I}},yd=e=>Q({...e}),lf=Q({perm:[0,2,1,3]}),gd=(e,t,r,n,o)=>{let i=t,a=n.kvNumHeads,l=n.nReps;return t.dims.length===3&&n.kvSequenceLength!==0&&(i=t.reshape([n.batchSize,n.kvSequenceLength,a,n.headSize])),r?i=e.compute(hd(i,r,i.dataType,n),{inputs:[i,r],outputs:[n.isPastkvBSNH?o:-1]})[0]:i=e.compute(hd(i,void 0,i.dataType,n),{inputs:[i],outputs:[n.isPastkvBSNH?o:-1]})[0],l!==1&&(i=e.compute(wo([i],[1,1,1,l]),{inputs:[i],outputs:[-1]})[0],i=i.reshape([n.batchSize,n.totalSequenceLength,a*l,n.headSize])),e.compute(Re(i,lf.perm),{inputs:[i],outputs:[-1]})[0]},bd=(e,t)=>{let r=df(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let n=er(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,e.inputs[0],void 0,0),o=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,i=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,a=gd(e,e.inputs[1],o,r,1),l=gd(e,e.inputs[2],i,r,2);Ut(e,n,a,l,void 0,void 0,void 0,void 0,void 0,r,t)}});var cf,pf,mf,vd,$d=M(()=>{"use strict";X();oe();re();cf=(e,t)=>{let r=e[0].dims,n=r,o=2,i=E.sizeToDimension(r,o),a=E.sizeFromDimension(r,o),l=he(a),d=a/l,p=[r[0],r[1],d],m=["rank","type","type"],u=[{type:12,data:a},{type:12,data:d}];u.push(...U(p,p));let h=w=>{let g=k("x",e[0].dataType,p.length,l),y=k("scale",e[1].dataType,e[1].dims),x=k("bias",e[2].dataType,e[2].dims),_=D("output",e[0].dataType,p.length,l),$=[g,y,x,_],S=g.type.value,I=l===1?"f32":`vec${l}<f32>`,T=64,A=[{name:"normSize",type:"u32"},{name:"normPackedSize",type:"u32"}];return`
5388
5387
  var<workgroup> meanShared : f32;
5389
5388
  var<workgroup> squaredNormShared : f32;
5390
5389
  var<workgroup> workgroupShared : array<${I}, ${T}>;
@@ -5412,7 +5411,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5412
5411
  workgroupBarrier();
5413
5412
  }
5414
5413
  if (localIndex == 0) {
5415
- meanShared = ${Xe("workgroupShared[0]",l)} / f32(uniforms.normSize);
5414
+ meanShared = ${je("workgroupShared[0]",l)} / f32(uniforms.normSize);
5416
5415
  }
5417
5416
  workgroupBarrier();
5418
5417
 
@@ -5433,19 +5432,19 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5433
5432
  workgroupBarrier();
5434
5433
  }
5435
5434
  if (localIndex == 0) {
5436
- squaredNormShared = ${Xe("workgroupShared[0]",l)};
5435
+ squaredNormShared = ${je("workgroupShared[0]",l)};
5437
5436
  }
5438
5437
  workgroupBarrier();
5439
5438
 
5440
5439
  let invStdDev = inverseSqrt(squaredNormShared / f32(uniforms.normSize) + f32(${t.epsilon}));
5441
- let channelScale = invStdDev * f32(${b.getByOffset("channel")});
5440
+ let channelScale = invStdDev * f32(${y.getByOffset("channel")});
5442
5441
  let channelShift = f32(${x.getByOffset("channel")}) - meanShared * channelScale;
5443
5442
  for (var h = localIndex; h < uniforms.normPackedSize; h += workgroupSize) {
5444
5443
  let value = ${g.get("batch","channel","h")} * ${S}(${I}(channelScale)) + ${S}(${I}(channelShift));
5445
5444
  ${_.set("batch","channel","h","value")};
5446
5445
  }
5447
- }`};return{name:"InstanceNormalization",shaderCache:{hint:`${t.epsilon};${l}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:i},programUniforms:u}),getShaderSource:h}},yf=(e,t,r,n,o,i,a,l)=>{let d=$e(a),p=64,m=d===1?"vec2f":`mat2x${d}f`,u=d===1?"f32":`vec${d}f`,h=(A,D)=>`${m}(${A}, ${D})`,w=o*a/d,g=Math.ceil(i/p),b=["type"],x=[{type:12,data:g},{type:12,data:i},{type:12,data:Math.floor(a/d)},{type:12,data:Math.floor(i*a/d)}],_=A=>{let D=E("input",t.dataType,t.dims,d);return`
5448
- ${A.declareVariables(D)}
5446
+ }`};return{name:"InstanceNormalization",shaderCache:{hint:`${t.epsilon};${l}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:i},programUniforms:u}),getShaderSource:h}},pf=(e,t,r,n,o,i,a,l)=>{let d=he(a),p=64,m=d===1?"vec2f":`mat2x${d}f`,u=d===1?"f32":`vec${d}f`,h=(A,O)=>`${m}(${A}, ${O})`,w=o*a/d,g=Math.ceil(i/p),y=["type"],x=[{type:12,data:g},{type:12,data:i},{type:12,data:Math.floor(a/d)},{type:12,data:Math.floor(i*a/d)}],_=A=>{let O=k("input",t.dataType,t.dims,d);return`
5447
+ ${A.declareVariables(O)}
5449
5448
  @group(0) @binding(1) var<storage, read_write> output : array<${m}>;
5450
5449
  struct Uniforms {wg_size:u32, H:u32, C:u32, image_size:u32};
5451
5450
  @group(0) @binding(2) var<uniform> uniforms: Uniforms;
@@ -5460,17 +5459,17 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5460
5459
  let wgMax = min(wgOffset + uniforms.wg_size, uniforms.H);
5461
5460
 
5462
5461
  let offset = currentImageNumber * uniforms.image_size + currentChannelNumber;
5463
- var sum = ${gt("f32",d)};
5464
- var squaredSum = ${gt("f32",d)};
5462
+ var sum = ${ft("f32",d)};
5463
+ var squaredSum = ${ft("f32",d)};
5465
5464
  for (var i: u32 = wgOffset; i < wgMax; i++) {
5466
5465
  let value = ${u}(input[offset + i * uniforms.C]);
5467
5466
  sum += value;
5468
5467
  squaredSum += value * value;
5469
5468
  }
5470
5469
  output[global_idx] = ${h("sum","squaredSum")};
5471
- }`},$=e.compute({name:"InstanceNormComputeMean",shaderCache:{hint:`${d}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:[o,a,p,2],dataType:1}],dispatchGroup:{x:o*a/d},programUniforms:x}),getShaderSource:_},{inputs:[t],outputs:[-1]})[0],S=[{type:12,data:w},{type:12,data:i},{type:12,data:Math.floor(a/d)},{type:12,data:Math.floor(p*a/d)}],I=["type","type","type"],T=A=>{let D=E("scale",r.dataType,r.dims,d),z=E("bias",n.dataType,n.dims,d);return`
5470
+ }`},$=e.compute({name:"InstanceNormComputeMean",shaderCache:{hint:`${d}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:[o,a,p,2],dataType:1}],dispatchGroup:{x:o*a/d},programUniforms:x}),getShaderSource:_},{inputs:[t],outputs:[-1]})[0],S=[{type:12,data:w},{type:12,data:i},{type:12,data:Math.floor(a/d)},{type:12,data:Math.floor(p*a/d)}],I=["type","type","type"],T=A=>{let O=k("scale",r.dataType,r.dims,d),z=k("bias",n.dataType,n.dims,d);return`
5472
5471
  @group(0) @binding(0) var<storage, read> input : array<${m}>;
5473
- @group(0) @binding(1) var<storage, read> scale : array<${D.type.storage}>;
5472
+ @group(0) @binding(1) var<storage, read> scale : array<${O.type.storage}>;
5474
5473
  @group(0) @binding(2) var<storage, read> bias : array<${z.type.storage}>;
5475
5474
  @group(0) @binding(3) var<storage, read_write> output : array<${m}>;
5476
5475
  struct Uniforms {units_of_work : u32, H: u32, C : u32, image_size : u32};
@@ -5482,8 +5481,8 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5482
5481
  let currentChannelNumber = global_idx % uniforms.C;
5483
5482
 
5484
5483
  let offset = currentImageNumber * uniforms.image_size;
5485
- var sum = ${gt("f32",d)};
5486
- var squaredSum = ${gt("f32",d)};
5484
+ var sum = ${ft("f32",d)};
5485
+ var squaredSum = ${ft("f32",d)};
5487
5486
  for (var i: u32 = 0; i < min(${p}, uniforms.H); i++) {
5488
5487
  let value = input[offset + i + currentChannelNumber * ${p}];
5489
5488
  sum += value[0];
@@ -5496,7 +5495,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5496
5495
  let channelShift = ${u}(bias[currentChannelNumber]) - sum * channelScale;
5497
5496
 
5498
5497
  output[global_idx] = ${h("channelScale","channelShift")};
5499
- }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${d};${l}`,inputDependencies:I},getRunData:()=>({outputs:[{dims:[o,a,2],dataType:1}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:S}),getShaderSource:T},{inputs:[$,r,n],outputs:[-1]})[0]},bf=(e,t,r)=>{let n=t[0].dims,o=n,i=n[0],a=n[n.length-1],l=k.sizeFromDimension(n,1)/a,d=$e(a),p=k.size(o)/d,m=[{type:12,data:l},{type:12,data:Math.floor(a/d)}],u=["type","type"],h=yf(e,t[0],t[1],t[2],i,l,a,r.epsilon),w=g=>{let b=ge(t[0].dataType),x=d===1?"vec2f":`mat2x${d}f`,_=d===1?b:`vec${d}<${b}>`,$=E("input",t[0].dataType,t[0].dims,d),S=M("output",t[0].dataType,o,d);return`
5498
+ }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${d};${l}`,inputDependencies:I},getRunData:()=>({outputs:[{dims:[o,a,2],dataType:1}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:S}),getShaderSource:T},{inputs:[$,r,n],outputs:[-1]})[0]},mf=(e,t,r)=>{let n=t[0].dims,o=n,i=n[0],a=n[n.length-1],l=E.sizeFromDimension(n,1)/a,d=he(a),p=E.size(o)/d,m=[{type:12,data:l},{type:12,data:Math.floor(a/d)}],u=["type","type"],h=pf(e,t[0],t[1],t[2],i,l,a,r.epsilon),w=g=>{let y=pe(t[0].dataType),x=d===1?"vec2f":`mat2x${d}f`,_=d===1?y:`vec${d}<${y}>`,$=k("input",t[0].dataType,t[0].dims,d),S=D("output",t[0].dataType,o,d);return`
5500
5499
  @group(0) @binding(0) var<storage, read> input : array<${$.type.storage}>;
5501
5500
  @group(0) @binding(1) var<storage, read> scaleInput : array<${x}>;
5502
5501
  @group(0) @binding(2) var<storage, read_write> output : array<${S.type.storage}>;
@@ -5510,77 +5509,77 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5510
5509
  let scaleOffset = currentImageNumber * uniforms.C + currentChannelNumber;
5511
5510
  let scale = scaleInput[scaleOffset];
5512
5511
  output[global_idx] = fma(input[global_idx], ${_}(scale[0]), ${_}(scale[1]));
5513
- }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${d}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:m}),getShaderSource:w},{inputs:[t[0],h]})},Td=(e,t)=>{t.format==="NHWC"?bf(e,e.inputs,t):e.compute(gf(e.inputs,t))}});var wf,vf,Ed,kd=U(()=>{"use strict";Q();ae();ie();wf=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},vf=(e,t,r)=>{let n=t.simplified,o=e[0].dims,i=e[1],a=!n&&e[2],l=o,d=k.normalizeAxis(t.axis,o.length),p=k.sizeToDimension(o,d),m=k.sizeFromDimension(o,d),u=k.size(i.dims),h=a?k.size(a.dims):0;if(u!==m||a&&h!==m)throw new Error(`Size of X.shape()[axis:] == ${m}.
5512
+ }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${d}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:m}),getShaderSource:w},{inputs:[t[0],h]})},vd=(e,t)=>{t.format==="NHWC"?mf(e,e.inputs,t):e.compute(cf(e.inputs,t))}});var ff,hf,_d,xd=M(()=>{"use strict";X();oe();re();ff=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},hf=(e,t,r)=>{let n=t.simplified,o=e[0].dims,i=e[1],a=!n&&e[2],l=o,d=E.normalizeAxis(t.axis,o.length),p=E.sizeToDimension(o,d),m=E.sizeFromDimension(o,d),u=E.size(i.dims),h=a?E.size(a.dims):0;if(u!==m||a&&h!==m)throw new Error(`Size of X.shape()[axis:] == ${m}.
5514
5513
  Size of scale and bias (if provided) must match this.
5515
- Got scale size of ${u} and bias size of ${h}`);let w=[];for(let T=0;T<o.length;++T)T<d?w.push(o[T]):w.push(1);let g=$e(m),b=["type","type"],x=[{type:12,data:p},{type:1,data:m},{type:12,data:Math.floor(m/g)},{type:1,data:t.epsilon}];a&&b.push("type");let _=r>1,$=r>2,S=T=>{let A=ge(e[0].dataType),D=[E("x",e[0].dataType,e[0].dims,g),E("scale",i.dataType,i.dims,g)];a&&D.push(E("bias",a.dataType,a.dims,g)),D.push(M("output",e[0].dataType,l,g)),_&&D.push(M("mean_data_output",1,w)),$&&D.push(M("inv_std_output",1,w));let z=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return`
5516
- ${T.registerUniforms(z).declareVariables(...D)}
5514
+ Got scale size of ${u} and bias size of ${h}`);let w=[];for(let T=0;T<o.length;++T)T<d?w.push(o[T]):w.push(1);let g=he(m),y=["type","type"],x=[{type:12,data:p},{type:1,data:m},{type:12,data:Math.floor(m/g)},{type:1,data:t.epsilon}];a&&y.push("type");let _=r>1,$=r>2,S=T=>{let A=pe(e[0].dataType),O=[k("x",e[0].dataType,e[0].dims,g),k("scale",i.dataType,i.dims,g)];a&&O.push(k("bias",a.dataType,a.dims,g)),O.push(D("output",e[0].dataType,l,g)),_&&O.push(D("mean_data_output",1,w)),$&&O.push(D("inv_std_output",1,w));let z=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return`
5515
+ ${T.registerUniforms(z).declareVariables(...O)}
5517
5516
  ${T.mainStart()}
5518
5517
  ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")}
5519
5518
  let offset = global_idx * uniforms.norm_size_vectorized;
5520
- var mean_vector = ${gt("f32",g)};
5521
- var mean_square_vector = ${gt("f32",g)};
5519
+ var mean_vector = ${ft("f32",g)};
5520
+ var mean_square_vector = ${ft("f32",g)};
5522
5521
 
5523
5522
  for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) {
5524
- let value = ${At(A,g,"x[h + offset]")};
5523
+ let value = ${It(A,g,"x[h + offset]")};
5525
5524
  mean_vector += value;
5526
5525
  mean_square_vector += value * value;
5527
5526
  }
5528
- let mean = ${Xe("mean_vector",g)} / uniforms.norm_size;
5529
- let inv_std_dev = inverseSqrt(${Xe("mean_square_vector",g)} / uniforms.norm_size ${n?"":"- mean * mean"} + uniforms.epsilon);
5527
+ let mean = ${je("mean_vector",g)} / uniforms.norm_size;
5528
+ let inv_std_dev = inverseSqrt(${je("mean_square_vector",g)} / uniforms.norm_size ${n?"":"- mean * mean"} + uniforms.epsilon);
5530
5529
 
5531
5530
  for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) {
5532
- let f32input = ${At(A,g,"x[j + offset]")};
5533
- let f32scale = ${At(A,g,"scale[j]")};
5534
- output[j + offset] = ${D[0].type.value}((f32input ${n?"":"- mean"}) * inv_std_dev * f32scale
5535
- ${a?`+ ${At(A,g,"bias[j]")}`:""}
5531
+ let f32input = ${It(A,g,"x[j + offset]")};
5532
+ let f32scale = ${It(A,g,"scale[j]")};
5533
+ output[j + offset] = ${O[0].type.value}((f32input ${n?"":"- mean"}) * inv_std_dev * f32scale
5534
+ ${a?`+ ${It(A,g,"bias[j]")}`:""}
5536
5535
  );
5537
5536
  }
5538
5537
 
5539
5538
  ${_?"mean_data_output[global_idx] = mean":""};
5540
5539
  ${$?"inv_std_output[global_idx] = inv_std_dev":""};
5541
- }`},I=[{dims:l,dataType:e[0].dataType}];return _&&I.push({dims:w,dataType:1}),$&&I.push({dims:w,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${g};${r};${n}`,inputDependencies:b},getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(p/64)},programUniforms:x}),getShaderSource:S}},Ed=(e,t)=>{wf(e.inputs),e.compute(vf(e.inputs,t,e.outputCount))}});var $f,_f,Pd,Od,Dd=U(()=>{"use strict";Q();ae();Ce();ie();$f=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],n=r.dims.length;if(r.dims[n-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let o=Math.floor((t.k+t.blockSize-1)/t.blockSize),i=t.blockSize/8*t.bits,a=e[1];if(!k.areEqual(a.dims,[t.n,o,i]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let d=e[2].dims;if(k.size(d)!==t.n*o)throw new Error("scales input size error.");if(e.length===4){let m=e[3].dims,u=t.bits>4?t.n*o:t.n*Math.floor((o+1)/2);if(k.size(m)!==u)throw new Error("zeroPoints input size error.")}},_f=(e,t,r,n)=>{let o=e[0].dims,i=o.length,a=Math.floor((t.k+t.blockSize-1)/t.blockSize),l=o[i-2],d=t.k,p=t.n,m=o.slice(0,i-2),u=k.size(m),w=t.blockSize/8*t.bits/4,g=e[0].dataType,b=$e(l),x=$e(t.k),_=$e(w),$=It(g),S=l*a*$,I=Math.floor(n/S),T=a<=r[0]&&I>0,A=!T||I>=4?$e(p):I>=2&&$e(p)>=2?2:1,D=m.concat([l,p]),z=k.size(D)/A/b,H=T?[]:[{type:12,data:z},{type:12,data:t.blockSize}],W=[u,l,d/x],X=k.convertShape(e[1].dims).slice();X.splice(-1,1,w/_),H.push(...V(W)),H.push(...V(X)),H.push(...V(e[2].dims)),e.length===4&&H.push(...V(k.convertShape(e[3].dims)));let Y=[u,l,p/A];H.push(...V(Y));let pe=ue=>{let le=W.length,K=E("a",e[0].dataType,le,x),de=E("b",12,X.length,_),re=E("scales",e[2].dataType,e[2].dims.length),se=[K,de,re],Z=e.length===4?E("zero_points",12,e[3].dims.length):void 0;Z&&se.push(Z);let ne=Y.length,J=M("output",e[0].dataType,ne,A),ke=[{name:"output_size",type:"u32"},{name:"block_size",type:"u32"}],R=ge(e[0].dataType),F=(()=>{switch(x){case 1:return`array<${R}, 8>`;case 2:return`mat4x2<${R}>`;case 4:return`mat2x4<${R}>`;default:throw new Error(`${x}-component is not supported.`)}})(),ce=`
5540
+ }`},I=[{dims:l,dataType:e[0].dataType}];return _&&I.push({dims:w,dataType:1}),$&&I.push({dims:w,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${g};${r};${n}`,inputDependencies:y},getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(p/64)},programUniforms:x}),getShaderSource:S}},_d=(e,t)=>{ff(e.inputs),e.compute(hf(e.inputs,t,e.outputCount))}});var gf,yf,Sd,Id,Cd=M(()=>{"use strict";X();oe();xe();re();gf=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],n=r.dims.length;if(r.dims[n-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let o=Math.floor((t.k+t.blockSize-1)/t.blockSize),i=t.blockSize/8*t.bits,a=e[1];if(!E.areEqual(a.dims,[t.n,o,i]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let d=e[2].dims;if(E.size(d)!==t.n*o)throw new Error("scales input size error.");if(e.length===4){let m=e[3].dims,u=t.bits>4?t.n*o:t.n*Math.floor((o+1)/2);if(E.size(m)!==u)throw new Error("zeroPoints input size error.")}},yf=(e,t,r,n)=>{let o=e[0].dims,i=o.length,a=Math.floor((t.k+t.blockSize-1)/t.blockSize),l=o[i-2],d=t.k,p=t.n,m=o.slice(0,i-2),u=E.size(m),w=t.blockSize/8*t.bits/4,g=e[0].dataType,y=he(l),x=he(t.k),_=he(w),$=_t(g,l*a),S=Math.floor(n/$),I=a<=r[0]&&S>0,T=!I||S>=4?he(p):S>=2&&he(p)>=2?2:1,A=m.concat([l,p]),O=E.size(A)/T/y,z=I?[]:[{type:12,data:O},{type:12,data:t.blockSize}],W=[u,l,d/x],V=E.convertShape(e[1].dims).slice();V.splice(-1,1,w/_),z.push(...U(W)),z.push(...U(V)),z.push(...U(e[2].dims)),e.length===4&&z.push(...U(E.convertShape(e[3].dims)));let j=[u,l,p/T];z.push(...U(j));let Y=ae=>{let se=W.length,ie=k("a",e[0].dataType,se,x),q=k("b",12,V.length,_),ce=k("scales",e[2].dataType,e[2].dims.length),ne=[ie,q,ce],Z=e.length===4?k("zero_points",12,e[3].dims.length):void 0;Z&&ne.push(Z);let ee=j.length,K=D("output",e[0].dataType,ee,T),me=[{name:"output_size",type:"u32"},{name:"block_size",type:"u32"}],$e=pe(e[0].dataType),R=(()=>{switch(x){case 1:return`array<${$e}, 8>`;case 2:return`mat4x2<${$e}>`;case 4:return`mat2x4<${$e}>`;default:throw new Error(`${x}-component is not supported.`)}})(),G=`
5542
5541
  for (var word: u32 = 0; word < ${w}; word += ${_}) {
5543
- ${de.indicesSet("b_indices","2","word")};
5544
- let b_data = ${de.getByIndices("b_indices")};
5542
+ ${q.indicesSet("b_indices","2","word")};
5543
+ let b_data = ${q.getByIndices("b_indices")};
5545
5544
  for (var i: u32 = 0; i < ${_}; i++) {
5546
5545
  let b_value: u32 = ${_===1?"b_data":"b_data[word + i]"};
5547
5546
  let b_mask: u32 = 0x0F0F0F0Fu;
5548
5547
  let b_value_lower: vec4<u32> = unpack4xU8(b_value & b_mask);
5549
5548
  let b_value_upper: vec4<u32> = unpack4xU8((b_value >> 4) & b_mask);
5550
- let b_quantized_values = ${F}(${Array.from({length:4},(Se,we)=>`${R}(b_value_lower[${we}]), ${R}(b_value_upper[${we}])`).join(", ")});
5551
- let b_dequantized_values = ${(()=>x===1?`${F}(${Array.from({length:8},(Se,we)=>`(b_quantized_values[${we}] - zero_point) * scale`).join(", ")});`:`(b_quantized_values - ${F}(${Array(8).fill("zero_point").join(",")})) * scale;`)()};
5549
+ let b_quantized_values = ${R}(${Array.from({length:4},(Be,ge)=>`${$e}(b_value_lower[${ge}]), ${$e}(b_value_upper[${ge}])`).join(", ")});
5550
+ let b_dequantized_values = ${(()=>x===1?`${R}(${Array.from({length:8},(Be,ge)=>`(b_quantized_values[${ge}] - zero_point) * scale`).join(", ")});`:`(b_quantized_values - ${R}(${Array(8).fill("zero_point").join(",")})) * scale;`)()};
5552
5551
  // Number of B elements per 32-bit word is 32/bits = 32/4 = 8
5553
- for (var m: u32 = 0; m < ${T?l:b}u; m++) {
5554
- ${K.indicesSet("a_indices",le-2,T?"m":`row * ${b} + m`)};
5555
- ${K.indicesSet("a_indices",le-1,"word_offset")};
5556
- var input_offset = ${K.indicesToOffset("a_indices")};
5557
- var a_data: ${F};
5552
+ for (var m: u32 = 0; m < ${I?l:y}u; m++) {
5553
+ ${ie.indicesSet("a_indices",se-2,I?"m":`row * ${y} + m`)};
5554
+ ${ie.indicesSet("a_indices",se-1,"word_offset")};
5555
+ var input_offset = ${ie.indicesToOffset("a_indices")};
5556
+ var a_data: ${R};
5558
5557
  for (var j: u32 = 0; j < ${8/x}; j++) {
5559
- a_data[j] = ${K.getByOffset("input_offset")};
5558
+ a_data[j] = ${ie.getByOffset("input_offset")};
5560
5559
  input_offset++;
5561
5560
  }
5562
- ${T?"workgroup_shared[workgroup_shared_offset + m]":"output_values[m]"}${A>1?"[c]":""} += ${Array.from({length:8/x},(Se,we)=>`${x===1?`a_data[${we}] * b_dequantized_values[${we}]`:`dot(a_data[${we}], b_dequantized_values[${we}])`}`).join(" + ")};
5561
+ ${I?"workgroup_shared[workgroup_shared_offset + m]":"output_values[m]"}${T>1?"[c]":""} += ${Array.from({length:8/x},(Be,ge)=>`${x===1?`a_data[${ge}] * b_dequantized_values[${ge}]`:`dot(a_data[${ge}], b_dequantized_values[${ge}])`}`).join(" + ")};
5563
5562
  }
5564
5563
  word_offset += ${8/x};
5565
5564
  }
5566
- }`,Re=Z?`
5565
+ }`,ue=Z?`
5567
5566
  zero_point_offset += 4;
5568
5567
  if (zero_point_offset == 32) {
5569
5568
  zero_point_offset = 0;
5570
5569
  zero_point_index++;
5571
5570
  zero_point_word = ${Z.getByOffset("zero_point_index")};
5572
- }`:"";return T?`
5573
- var<workgroup> workgroup_shared: array<${J.type.value}, ${l*a}>;
5574
- ${ue.declareVariables(...se,J)}
5575
- ${ue.mainStart([a,1,1])}
5576
- var a_indices: ${K.type.indices};
5571
+ }`:"";return I?`
5572
+ var<workgroup> workgroup_shared: array<${K.type.value}, ${l*a}>;
5573
+ ${ae.declareVariables(...ne,K)}
5574
+ ${ae.mainStart([a,1,1])}
5575
+ var a_indices: ${ie.type.indices};
5577
5576
  var block = local_id.x;
5578
5577
  var col = workgroup_id.y;
5579
5578
  var batch = workgroup_id.z;
5580
- ${K.indicesSet("a_indices","0","batch")};
5579
+ ${ie.indicesSet("a_indices","0","batch")};
5581
5580
  // Two zero points are packed into one byte when uniforms.bits is 4.
5582
- for (var c: u32 = 0; c < ${A}; c++) {
5583
- let col_times_components_plus_c = col * ${A} + c;
5581
+ for (var c: u32 = 0; c < ${T}; c++) {
5582
+ let col_times_components_plus_c = col * ${T} + c;
5584
5583
  ${Z?`
5585
5584
  var zero_point_bytes_per_col: u32 = (${a} + 1) / 2;
5586
5585
  var zero_point_byte_count: u32 = col_times_components_plus_c * zero_point_bytes_per_col + (block >> 0x1u);
@@ -5589,90 +5588,90 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5589
5588
  var zero_point_nibble_offset: u32 = block & 0x1u;
5590
5589
  var zero_point_bits_offset: u32 = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2);
5591
5590
  var zero_point_word: u32 = ${Z.getByOffset("zero_point_word_index")} >> zero_point_bits_offset;`:""}
5592
- var b_indices: ${de.type.indices};
5593
- ${de.indicesSet("b_indices","0","col_times_components_plus_c")};
5591
+ var b_indices: ${q.type.indices};
5592
+ ${q.indicesSet("b_indices","0","col_times_components_plus_c")};
5594
5593
  // The scale and zero points are computed per block.
5595
5594
  var scales_index = col_times_components_plus_c * ${a} + block;
5596
- let scale = ${re.getByOffset("scales_index")};
5595
+ let scale = ${ce.getByOffset("scales_index")};
5597
5596
  // The default zero point is 8 for unsigned 4-bit quantization.
5598
- let zero_point = ${R}(${Z?"(zero_point_word) & 0xFu":8});
5599
- ${de.indicesSet("b_indices","1","block")};
5597
+ let zero_point = ${$e}(${Z?"(zero_point_word) & 0xFu":8});
5598
+ ${q.indicesSet("b_indices","1","block")};
5600
5599
  var word_offset: u32 = block * ${t.blockSize/x};
5601
5600
  var workgroup_shared_offset: u32 = block * ${l};
5602
- ${ce}
5601
+ ${G}
5603
5602
  }
5604
5603
  workgroupBarrier();
5605
- var output_indices: ${J.type.indices};
5604
+ var output_indices: ${K.type.indices};
5606
5605
  var elements_per_thread: u32 = ${Math.ceil(l/a)};
5607
- ${J.indicesSet("output_indices","0","batch")};
5608
- ${J.indicesSet("output_indices",ne-1,"col")};
5609
- ${J.indicesSet("output_indices",ne-2,"local_id.x * elements_per_thread")};
5610
- var output_offset = ${J.indicesToOffset("output_indices")};
5606
+ ${K.indicesSet("output_indices","0","batch")};
5607
+ ${K.indicesSet("output_indices",ee-1,"col")};
5608
+ ${K.indicesSet("output_indices",ee-2,"local_id.x * elements_per_thread")};
5609
+ var output_offset = ${K.indicesToOffset("output_indices")};
5611
5610
  for (var m: u32 = 0u; m < elements_per_thread; m++) {
5612
5611
  var row = m + local_id.x * elements_per_thread;
5613
5612
  if (row < ${l}) {
5614
- var output_value: ${J.type.value} = ${J.type.value}(0);
5613
+ var output_value: ${K.type.value} = ${K.type.value}(0);
5615
5614
  var workgroup_shared_offset: u32 = row;
5616
5615
  for (var b: u32 = 0u; b < ${a}u; b++) {
5617
5616
  output_value += workgroup_shared[workgroup_shared_offset];
5618
5617
  workgroup_shared_offset += ${l};
5619
5618
  }
5620
- ${J.setByOffset("output_offset","output_value")};
5621
- output_offset += ${p/A};
5619
+ ${K.setByOffset("output_offset","output_value")};
5620
+ output_offset += ${p/T};
5622
5621
  }
5623
5622
  }
5624
5623
  }`:`
5625
- ${ue.registerUniforms(ke).declareVariables(...se,J)}
5626
- ${ue.mainStart()}
5627
- ${ue.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
5628
- var output_values: array<${J.type.value}, ${b}>;
5629
- var output_indices = ${J.offsetToIndices("global_idx")};
5630
- var col = ${J.indicesGet("output_indices",ne-1)};
5631
- var row = ${J.indicesGet("output_indices",ne-2)};
5632
- var a_indices: ${K.type.indices} = output_indices;
5624
+ ${ae.registerUniforms(me).declareVariables(...ne,K)}
5625
+ ${ae.mainStart()}
5626
+ ${ae.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
5627
+ var output_values: array<${K.type.value}, ${y}>;
5628
+ var output_indices = ${K.offsetToIndices("global_idx")};
5629
+ var col = ${K.indicesGet("output_indices",ee-1)};
5630
+ var row = ${K.indicesGet("output_indices",ee-2)};
5631
+ var a_indices: ${ie.type.indices} = output_indices;
5633
5632
  // Two zero points are packed into one byte because uniforms.bits <= 4.
5634
5633
  // zero_point_offset is either 0 or 4. It is bit offset within one byte.
5635
5634
  // TODO support zero_point_offset for bits > 4
5636
5635
  ${Z?`
5637
- var zero_point_abs_offset = col * ${A} * ((${a} + 1) / 2);
5636
+ var zero_point_abs_offset = col * ${T} * ((${a} + 1) / 2);
5638
5637
  var zero_point_index: u32 = zero_point_abs_offset / 4;
5639
5638
  var zero_point_word: u32 = ${Z.getByOffset("zero_point_index")};
5640
5639
  var zero_point_offset: u32 = (zero_point_abs_offset % 4) * 8;`:""}
5641
- var scale_index = col * ${a*A};
5642
- var b_indices: ${de.type.indices};
5643
- for (var c: u32 = 0; c < ${A}; c++) {
5644
- ${de.indicesSet("b_indices","0",`col * ${A} + c`)};
5640
+ var scale_index = col * ${a*T};
5641
+ var b_indices: ${q.type.indices};
5642
+ for (var c: u32 = 0; c < ${T}; c++) {
5643
+ ${q.indicesSet("b_indices","0",`col * ${T} + c`)};
5645
5644
  var block_offset: u32 = 0;
5646
5645
  for (var block: u32 = 0; block < ${a}; block++) {
5647
5646
  // The scale and zero points are computed per block.
5648
- let scale = ${re.getByOffset("scale_index")};
5647
+ let scale = ${ce.getByOffset("scale_index")};
5649
5648
  // The default zero point is 8 for unsigned 4-bit quantization.
5650
- let zero_point = ${R}(${Z?"extractBits(zero_point_word, zero_point_offset, 4)":8});
5651
- ${de.indicesSet("b_indices","1","block")};
5649
+ let zero_point = ${$e}(${Z?"extractBits(zero_point_word, zero_point_offset, 4)":8});
5650
+ ${q.indicesSet("b_indices","1","block")};
5652
5651
  var word_offset: u32 = block_offset;
5653
- ${ce}
5652
+ ${G}
5654
5653
  scale_index++;
5655
- ${Re}
5654
+ ${ue}
5656
5655
  block_offset += uniforms.block_size / ${x};
5657
5656
  }
5658
5657
  // Drop the trailing 4 bits if the zero_poit_offset is not a byte boundary to align with the next byte.
5659
5658
  ${Z?`if (zero_point_offset % 8 > 0) {
5660
- ${Re}
5659
+ ${ue}
5661
5660
  }`:""}
5662
5661
  }
5663
- for (var k: u32 = 0u; k < ${b}u; k++) {
5664
- ${J.indicesSet("output_indices",ne-2,`${b} * row + k`)};
5665
- ${J.setByIndices("output_indices","output_values[k]")}
5662
+ for (var k: u32 = 0u; k < ${y}u; k++) {
5663
+ ${K.indicesSet("output_indices",ee-2,`${y} * row + k`)};
5664
+ ${K.setByIndices("output_indices","output_values[k]")}
5666
5665
  }
5667
- }`};return{name:T?"BlockwiseMatMulNBits":"MatMulNBits",shaderCache:{hint:`${t.cacheKey};${l};${g};${e.length}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:D,dataType:g}],name:T?"BlockwiseMatMulNBits":"MatMulNBits",dispatchGroup:T?{x:1,y:Math.ceil(p/A),z:u}:{x:Math.ceil(z/64)},programUniforms:H}),getShaderSource:pe}},Pd=(e,t)=>{$f(e.inputs,t);let r=e.getMaxComputeWorkgroupSizes(),n=e.getMaxComputeWorkgroupStoragesize();e.compute(_f(e.inputs,t,r,n))},Od=e=>te(e)});var xf,Sf,If,Cf,Tf,Af,Ef,kf,zd,Bd=U(()=>{"use strict";Q();ae();ie();xf=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},Sf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5668
- k = i32(${e.indicesGet("indices",o)}) - ${G("uniforms.pads",o,r)};
5666
+ }`};return{name:I?"BlockwiseMatMulNBits":"MatMulNBits",shaderCache:{hint:`${t.cacheKey};${l};${g};${e.length}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:A,dataType:g}],name:I?"BlockwiseMatMulNBits":"MatMulNBits",dispatchGroup:I?{x:1,y:Math.ceil(p/T),z:u}:{x:Math.ceil(O/64)},programUniforms:z}),getShaderSource:Y}},Sd=(e,t)=>{gf(e.inputs,t);let r=e.getMaxComputeWorkgroupSizes(),n=e.getMaxComputeWorkgroupStoragesize();e.compute(yf(e.inputs,t,r,n))},Id=e=>Q(e)});var bf,wf,vf,$f,_f,xf,Sf,If,Td,Ad=M(()=>{"use strict";X();oe();re();bf=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},wf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5667
+ k = i32(${e.indicesGet("indices",o)}) - ${L("uniforms.pads",o,r)};
5669
5668
  if (k < 0) {
5670
5669
  break;
5671
5670
  }
5672
- if (k >= i32(${G("uniforms.x_shape",o,t)})) {
5671
+ if (k >= i32(${L("uniforms.x_shape",o,t)})) {
5673
5672
  break;
5674
5673
  }
5675
- offset += k * i32(${G("uniforms.x_strides",o,t)});
5674
+ offset += k * i32(${L("uniforms.x_strides",o,t)});
5676
5675
  `;return`
5677
5676
  value = ${e.type.value}(uniforms.constant_value);
5678
5677
  for (var i = 0; i < 1; i++) {
@@ -5681,53 +5680,53 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5681
5680
  ${n}
5682
5681
  value = x[offset];
5683
5682
  }
5684
- `},If=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5685
- k = i32(${e.indicesGet("indices",o)}) - ${G("uniforms.pads",o,r)};
5683
+ `},vf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5684
+ k = i32(${e.indicesGet("indices",o)}) - ${L("uniforms.pads",o,r)};
5686
5685
  if (k < 0) {
5687
5686
  k = -k;
5688
5687
  }
5689
5688
  {
5690
- let _2n_1 = 2 * (i32(${G("uniforms.x_shape",o,t)}) - 1);
5689
+ let _2n_1 = 2 * (i32(${L("uniforms.x_shape",o,t)}) - 1);
5691
5690
  k = k % _2n_1;
5692
- if(k >= i32(${G("uniforms.x_shape",o,t)})) {
5691
+ if(k >= i32(${L("uniforms.x_shape",o,t)})) {
5693
5692
  k = _2n_1 - k;
5694
5693
  }
5695
5694
  }
5696
- offset += k * i32(${G("uniforms.x_strides",o,t)});
5695
+ offset += k * i32(${L("uniforms.x_strides",o,t)});
5697
5696
  `;return`
5698
5697
  var offset = 0;
5699
5698
  var k = 0;
5700
5699
  ${n}
5701
5700
  value = x[offset];
5702
- `},Cf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5703
- k = i32(${e.indicesGet("indices",o)}) - ${G("uniforms.pads",o,r)};
5701
+ `},$f=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5702
+ k = i32(${e.indicesGet("indices",o)}) - ${L("uniforms.pads",o,r)};
5704
5703
  if (k < 0) {
5705
5704
  k = 0;
5706
5705
  }
5707
- if (k >= i32(${G("uniforms.x_shape",o,t)})) {
5708
- k = i32(${G("uniforms.x_shape",o,t)}) - 1;
5706
+ if (k >= i32(${L("uniforms.x_shape",o,t)})) {
5707
+ k = i32(${L("uniforms.x_shape",o,t)}) - 1;
5709
5708
  }
5710
- offset += k * i32(${G("uniforms.x_strides",o,t)});
5709
+ offset += k * i32(${L("uniforms.x_strides",o,t)});
5711
5710
  `;return`
5712
5711
  var offset = 0;
5713
5712
  var k = 0;
5714
5713
  ${n}
5715
5714
  value = x[offset];
5716
- `},Tf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5717
- k = i32(${e.indicesGet("indices",o)}) - ${G("uniforms.pads",o,r)};
5715
+ `},_f=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
5716
+ k = i32(${e.indicesGet("indices",o)}) - ${L("uniforms.pads",o,r)};
5718
5717
  if (k < 0) {
5719
- k += i32(${G("uniforms.x_shape",o,t)}]);
5718
+ k += i32(${L("uniforms.x_shape",o,t)}]);
5720
5719
  }
5721
- if (k >= i32(${G("uniforms.x_shape",o,t)})) {
5722
- k -= i32(${G("uniforms.x_shape",o,t)});
5720
+ if (k >= i32(${L("uniforms.x_shape",o,t)})) {
5721
+ k -= i32(${L("uniforms.x_shape",o,t)});
5723
5722
  }
5724
- offset += k * i32(${G("uniforms.x_strides",o,t)});
5723
+ offset += k * i32(${L("uniforms.x_strides",o,t)});
5725
5724
  `;return`
5726
5725
  var offset = 0;
5727
5726
  var k = 0;
5728
5727
  ${n}
5729
5728
  value = x[offset];
5730
- `},Af=(e,t,r)=>{switch(r.mode){case 0:return Sf(e,t,r.pads.length);case 1:return If(e,t,r.pads.length);case 2:return Cf(e,t,r.pads.length);case 3:return Tf(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Ef=(e,t)=>{let r=k.padShape(e[0].dims.slice(),t.pads),n=e[0].dims,o=k.size(r),i=[{type:12,data:o},{type:6,data:t.pads}];t.mode===0&&i.push({type:e[0].dataType,data:t.value}),i.push(...V(e[0].dims,r));let a=["rank"],l=d=>{let p=M("output",e[0].dataType,r.length),m=E("x",e[0].dataType,n.length),u=m.type.value,h=Af(p,n.length,t),w=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&w.push({name:"constant_value",type:u}),`
5729
+ `},xf=(e,t,r)=>{switch(r.mode){case 0:return wf(e,t,r.pads.length);case 1:return vf(e,t,r.pads.length);case 2:return $f(e,t,r.pads.length);case 3:return _f(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Sf=(e,t)=>{let r=E.padShape(e[0].dims.slice(),t.pads),n=e[0].dims,o=E.size(r),i=[{type:12,data:o},{type:6,data:t.pads}];t.mode===0&&i.push({type:e[0].dataType,data:t.value}),i.push(...U(e[0].dims,r));let a=["rank"],l=d=>{let p=D("output",e[0].dataType,r.length),m=k("x",e[0].dataType,n.length),u=m.type.value,h=xf(p,n.length,t),w=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&w.push({name:"constant_value",type:u}),`
5731
5730
  ${d.registerUniforms(w).declareVariables(m,p)}
5732
5731
  ${d.mainStart()}
5733
5732
  ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
@@ -5737,7 +5736,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5737
5736
  var value = ${u}(0);
5738
5737
  ${h}
5739
5738
  output[global_idx] = value;
5740
- }`};return{name:"Pad",shaderCache:{hint:`${t.mode}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(r)/64)},programUniforms:i}),getShaderSource:l}},kf=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),n=e.length>=3&&e[2].data?e[2].getFloat32Array()[0]:0,o=e[0].dims.length,i=new Int32Array(2*o).fill(0);if(e.length>=4){let l=e[3].getBigInt64Array();for(let d=0;d<l.length;d++)i[Number(l[d])]=Number(r[d]),i[Number(l[d])+o]=Number(r[d+l.length])}else r.forEach((l,d)=>i[Number(d)]=Number(l));let a=[];return i.forEach(l=>a.push(l)),{mode:t.mode,value:n,pads:a}}else return t},zd=(e,t)=>{xf(e.inputs);let r=kf(e.inputs,t);e.compute(Ef(e.inputs,r),{inputs:[0]})}});var nn,Rd,Md,Ud,Vd,Pf,Of,Nd,Wd,Hd,Gd,Ld,Fd,qd,jd,Kd,Yd,Xd,Zd,Qd=U(()=>{"use strict";Ke();Q();ae();ie();nn=e=>{if(be.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},Rd=(e,t,r)=>{let n=t.format==="NHWC",o=e.dims.slice();n&&o.splice(1,0,o.pop());let i=Object.hasOwnProperty.call(t,"dilations"),a=t.kernelShape.slice(),l=t.strides.slice(),d=i?t.dilations.slice():[],p=t.pads.slice();Ct.adjustPoolAttributes(r,o,a,l,d,p);let m=Ct.computePoolOutputShape(r,o,l,d,a,p,t.autoPad),u=Object.assign({},t);i?Object.assign(u,{kernelShape:a,strides:l,pads:p,dilations:d,cacheKey:t.cacheKey}):Object.assign(u,{kernelShape:a,strides:l,pads:p,cacheKey:t.cacheKey});let h=m.slice();return h.push(h.splice(1,1)[0]),[u,n?h:m]},Md=(e,t)=>{let r=t.format==="NHWC",n=k.size(e),o=k.size(t.kernelShape),i=[{type:12,data:n},{type:12,data:o}],a=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let l=t.kernelShape[t.kernelShape.length-1],d=t.strides[t.strides.length-1],p=t.pads[t.pads.length/2-1],m=t.pads[t.pads.length-1],u=!!(p+m);i.push({type:12,data:l},{type:12,data:d},{type:12,data:p},{type:12,data:m}),a.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let h=!1;if(t.kernelShape.length===2){let w=t.kernelShape[t.kernelShape.length-2],g=t.strides[t.strides.length-2],b=t.pads[t.pads.length/2-2],x=t.pads[t.pads.length-2];h=!!(b+x),i.push({type:12,data:w},{type:12,data:g},{type:12,data:b},{type:12,data:x}),a.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[i,a,!0,u,h]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let l=k.computeStrides(t.kernelShape);i.push({type:12,data:l},{type:12,data:t.pads},{type:12,data:t.strides}),a.push({name:"kernelStrides",type:"u32",length:l.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let d=t.pads.reduce((p,m)=>p+m);return[i,a,!!d,!1,!1]}},Ud=(e,t,r,n,o,i,a,l,d,p,m,u)=>{let h=o.format==="NHWC",w=t.type.value,g=M("output",t.type.tensor,n);if(o.kernelShape.length<=2){let b="",x="",_="",$=r-(h?2:1);if(m?b=`
5739
+ }`};return{name:"Pad",shaderCache:{hint:`${t.mode}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(E.size(r)/64)},programUniforms:i}),getShaderSource:l}},If=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),n=e.length>=3&&e[2].data?e[2].getFloat32Array()[0]:0,o=e[0].dims.length,i=new Int32Array(2*o).fill(0);if(e.length>=4){let l=e[3].getBigInt64Array();for(let d=0;d<l.length;d++)i[Number(l[d])]=Number(r[d]),i[Number(l[d])+o]=Number(r[d+l.length])}else r.forEach((l,d)=>i[Number(d)]=Number(l));let a=[];return i.forEach(l=>a.push(l)),{mode:t.mode,value:n,pads:a}}else return t},Td=(e,t)=>{bf(e.inputs);let r=If(e.inputs,t);e.compute(Sf(e.inputs,r),{inputs:[0]})}});var en,kd,Ed,Pd,Od,Cf,Tf,zd,Bd,Dd,Rd,Md,Ud,Vd,Nd,Wd,Ld,Hd,Gd,Fd=M(()=>{"use strict";qe();X();oe();re();en=e=>{if(fe.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},kd=(e,t,r)=>{let n=t.format==="NHWC",o=e.dims.slice();n&&o.splice(1,0,o.pop());let i=Object.hasOwnProperty.call(t,"dilations"),a=t.kernelShape.slice(),l=t.strides.slice(),d=i?t.dilations.slice():[],p=t.pads.slice();xt.adjustPoolAttributes(r,o,a,l,d,p);let m=xt.computePoolOutputShape(r,o,l,d,a,p,t.autoPad),u=Object.assign({},t);i?Object.assign(u,{kernelShape:a,strides:l,pads:p,dilations:d,cacheKey:t.cacheKey}):Object.assign(u,{kernelShape:a,strides:l,pads:p,cacheKey:t.cacheKey});let h=m.slice();return h.push(h.splice(1,1)[0]),[u,n?h:m]},Ed=(e,t)=>{let r=t.format==="NHWC",n=E.size(e),o=E.size(t.kernelShape),i=[{type:12,data:n},{type:12,data:o}],a=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let l=t.kernelShape[t.kernelShape.length-1],d=t.strides[t.strides.length-1],p=t.pads[t.pads.length/2-1],m=t.pads[t.pads.length-1],u=!!(p+m);i.push({type:12,data:l},{type:12,data:d},{type:12,data:p},{type:12,data:m}),a.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let h=!1;if(t.kernelShape.length===2){let w=t.kernelShape[t.kernelShape.length-2],g=t.strides[t.strides.length-2],y=t.pads[t.pads.length/2-2],x=t.pads[t.pads.length-2];h=!!(y+x),i.push({type:12,data:w},{type:12,data:g},{type:12,data:y},{type:12,data:x}),a.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[i,a,!0,u,h]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let l=E.computeStrides(t.kernelShape);i.push({type:12,data:l},{type:12,data:t.pads},{type:12,data:t.strides}),a.push({name:"kernelStrides",type:"u32",length:l.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let d=t.pads.reduce((p,m)=>p+m);return[i,a,!!d,!1,!1]}},Pd=(e,t,r,n,o,i,a,l,d,p,m,u)=>{let h=o.format==="NHWC",w=t.type.value,g=D("output",t.type.tensor,n);if(o.kernelShape.length<=2){let y="",x="",_="",$=r-(h?2:1);if(m?y=`
5741
5740
  for (var i: u32 = 0u; i < uniforms.kw; i++) {
5742
5741
  xIndices[${$}] = indices[${$}] * uniforms.sw - uniforms.pwStart + i;
5743
5742
  if (xIndices[${$}] < 0 || xIndices[${$}]
@@ -5747,7 +5746,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5747
5746
  }
5748
5747
  let x_val = x[${t.indicesToOffset("xIndices")}];
5749
5748
  ${i}
5750
- }`:b=`
5749
+ }`:y=`
5751
5750
  for (var i: u32 = 0u; i < uniforms.kw; i++) {
5752
5751
  xIndices[${$}] = indices[${$}] * uniforms.sw - uniforms.pwStart + i;
5753
5752
  let x_val = x[${t.indicesToOffset("xIndices")}];
@@ -5776,12 +5775,12 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5776
5775
  var value = ${w}(${l});
5777
5776
  var pad = 0;
5778
5777
  ${x}
5779
- ${b}
5778
+ ${y}
5780
5779
  ${_}
5781
5780
  ${a}
5782
5781
 
5783
5782
  output[global_idx] = value;
5784
- }`}else{if(h)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let b=o.kernelShape.length,x=o.pads.length,_="";return p?_=`
5783
+ }`}else{if(h)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let y=o.kernelShape.length,x=o.pads.length,_="";return p?_=`
5785
5784
  if (xIndices[j] >= uniforms.x_shape[j]) {
5786
5785
  pad++;
5787
5786
  isPad = true;
@@ -5803,7 +5802,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5803
5802
  let indices = ${g.offsetToIndices("global_idx")};
5804
5803
  var xIndices = ${g.offsetToIndices("global_idx")};
5805
5804
 
5806
- var offsets: array<u32, ${b}>;
5805
+ var offsets: array<u32, ${y}>;
5807
5806
 
5808
5807
  var value = ${w}(${l});
5809
5808
  var pad = 0;
@@ -5811,30 +5810,68 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5811
5810
 
5812
5811
  for (var i: u32 = 0u; i < uniforms.kernelSize; i++) {
5813
5812
  var offset = i;
5814
- for (var j = 0u; j < ${b-1}u; j++) {
5815
- offsets[j] = offset / ${G("uniforms.kernelStrides","j",b)};
5816
- offset -= offsets[j] * ${G("uniforms.kernelStrides","j",b)};
5813
+ for (var j = 0u; j < ${y-1}u; j++) {
5814
+ offsets[j] = offset / ${L("uniforms.kernelStrides","j",y)};
5815
+ offset -= offsets[j] * ${L("uniforms.kernelStrides","j",y)};
5817
5816
  }
5818
- offsets[${b-1}] = offset;
5817
+ offsets[${y-1}] = offset;
5819
5818
 
5820
5819
  isPad = false;
5821
- for (var j = ${r-b}u; j < ${r}u; j++) {
5822
- xIndices[j] = indices[j] * ${G("uniforms.strides",`j - ${r-b}u`,b)}
5823
- + offsets[j - ${r-b}u] - ${G("uniforms.pads","j - 2u",x)};
5820
+ for (var j = ${r-y}u; j < ${r}u; j++) {
5821
+ xIndices[j] = indices[j] * ${L("uniforms.strides",`j - ${r-y}u`,y)}
5822
+ + offsets[j - ${r-y}u] - ${L("uniforms.pads","j - 2u",x)};
5824
5823
  ${_}
5825
5824
  }
5826
5825
  ${a}
5827
5826
 
5828
5827
  output[global_idx] = value;
5829
- }`}},Vd=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Pf=e=>`${Vd(e)};${e.countIncludePad}`,Of=e=>`${Vd(e)};${e.storageOrder};${e.dilations}`,Nd=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Wd=(e,t,r,n)=>{let[o,i]=Rd(t,n,r),a=E("x",t.dataType,t.dims.length),l=a.type.value,d="value += x_val;",p="";o.countIncludePad?p+=`value /= ${l}(uniforms.kernelSize);`:p+=`value /= ${l}(i32(uniforms.kernelSize) - pad);`;let[m,u,h,w,g]=Md(i,o);m.push(...V(t.dims,i));let b=["rank"];return{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:m}),getShaderSource:x=>Ud(x,a,t.dims.length,i.length,o,d,p,0,u,h,w,g)}},Hd=e=>{let t=e.count_include_pad!==0,r=Nd(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let n={countIncludePad:t,...r,cacheKey:""};return{...n,cacheKey:Pf(n)}},Gd=(e,t)=>{nn(e.inputs),e.compute(Wd("AveragePool",e.inputs[0],!1,t))},Ld={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},Fd=e=>{let t=e.format;return{format:t,...Ld,cacheKey:t}},qd=(e,t)=>{nn(e.inputs),e.compute(Wd("GlobalAveragePool",e.inputs[0],!0,t))},jd=(e,t,r,n)=>{let[o,i]=Rd(t,n,r),a=`
5828
+ }`}},Od=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Cf=e=>`${Od(e)};${e.countIncludePad}`,Tf=e=>`${Od(e)};${e.storageOrder};${e.dilations}`,zd=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Bd=(e,t,r,n)=>{let[o,i]=kd(t,n,r),a=k("x",t.dataType,t.dims.length),l=a.type.value,d="value += x_val;",p="";o.countIncludePad?p+=`value /= ${l}(uniforms.kernelSize);`:p+=`value /= ${l}(i32(uniforms.kernelSize) - pad);`;let[m,u,h,w,g]=Ed(i,o);m.push(...U(t.dims,i));let y=["rank"];return{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(E.size(i)/64)},programUniforms:m}),getShaderSource:x=>Pd(x,a,t.dims.length,i.length,o,d,p,0,u,h,w,g)}},Dd=e=>{let t=e.count_include_pad!==0,r=zd(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let n={countIncludePad:t,...r,cacheKey:""};return{...n,cacheKey:Cf(n)}},Rd=(e,t)=>{en(e.inputs),e.compute(Bd("AveragePool",e.inputs[0],!1,t))},Md={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},Ud=e=>{let t=e.format;return{format:t,...Md,cacheKey:t}},Vd=(e,t)=>{en(e.inputs),e.compute(Bd("GlobalAveragePool",e.inputs[0],!0,t))},Nd=(e,t,r,n)=>{let[o,i]=kd(t,n,r),a=`
5830
5829
  value = max(x_val, value);
5831
- `,l="",d=E("x",t.dataType,t.dims.length),p=["rank"],[m,u,h,w,g]=Md(i,o);return m.push(...V(t.dims,i)),{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:p},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:m}),getShaderSource:b=>Ud(b,d,t.dims.length,i.length,o,a,l,t.dataType===10?-65504:-1e5,u,h,w,g)}},Kd=(e,t)=>{nn(e.inputs),e.compute(jd("MaxPool",e.inputs[0],!1,t))},Yd=e=>{let t=e.storage_order,r=e.dilations,n=Nd(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let o={storageOrder:t,dilations:r,...n,cacheKey:""};return{...o,cacheKey:Of(o)}},Xd=e=>{let t=e.format;return{format:t,...Ld,cacheKey:t}},Zd=(e,t)=>{nn(e.inputs),e.compute(jd("GlobalMaxPool",e.inputs[0],!0,t))}});var zf,Bf,Jd,el=U(()=>{"use strict";Ke();Q();ie();zf=(e,t,r)=>{let n=e===t,o=e<t&&r<0,i=e>t&&r>0;if(n||o||i)throw new Error("Range these inputs' contents are invalid.")},Bf=(e,t,r,n)=>{let o=Math.abs(Math.ceil((t-e)/r)),i=[o],a=o,l=[{type:12,data:a},{type:n,data:e},{type:n,data:r},...V(i)],d=p=>{let m=M("output",n,i.length),u=m.type.value,h=[{name:"outputSize",type:"u32"},{name:"start",type:u},{name:"delta",type:u}];return`
5830
+ `,l="",d=k("x",t.dataType,t.dims.length),p=["rank"],[m,u,h,w,g]=Ed(i,o);return m.push(...U(t.dims,i)),{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:p},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(E.size(i)/64)},programUniforms:m}),getShaderSource:y=>Pd(y,d,t.dims.length,i.length,o,a,l,t.dataType===10?-65504:-1e5,u,h,w,g)}},Wd=(e,t)=>{en(e.inputs),e.compute(Nd("MaxPool",e.inputs[0],!1,t))},Ld=e=>{let t=e.storage_order,r=e.dilations,n=zd(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let o={storageOrder:t,dilations:r,...n,cacheKey:""};return{...o,cacheKey:Tf(o)}},Hd=e=>{let t=e.format;return{format:t,...Md,cacheKey:t}},Gd=(e,t)=>{en(e.inputs),e.compute(Nd("GlobalMaxPool",e.inputs[0],!0,t))}});var kf,Ef,qd,Kd,jd=M(()=>{"use strict";X();oe();xe();re();kf=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[0].dataType===6&&e.length>2)throw new Error("In the case of dequantizing int32 there is no zero point.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((r,n)=>r===e[2].dims[n]).reduce((r,n)=>r&&n,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((o,i)=>i===t.axis||o===e[0].dims[i]).reduce((o,i)=>o&&i,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let r=e[0].dims[t.axis],n=e[1].dims[t.axis];if(t.blockSize<Math.ceil(r/n)||t.blockSize>Math.ceil(r/(n-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},Ef=(e,t)=>{let r=E.normalizeAxis(t.axis,e[0].dims.length),n=e[0].dataType,o=n===3,i=e[0].dims,a=e[1].dataType,l=E.size(i),d=n===3||n===2,p=d?[Math.ceil(E.size(e[0].dims)/4)]:e[0].dims,m=e[1].dims,u=e.length>2?e[2]:void 0,h=u?d?[Math.ceil(E.size(u.dims)/4)]:u.dims:void 0,w=m.length===0||m.length===1&&m[0]===1,g=w===!1&&m.length===1,y=he(l),x=w&&(!d||y===4),_=x?y:1,$=x&&!d?y:1,S=k("input",d?12:n,p.length,$),I=k("scale",a,m.length),T=u?k("zero_point",d?12:n,h.length):void 0,A=D("output",a,i.length,_),O=[S,I];T&&O.push(T);let z=[p,m];u&&z.push(h);let W=[{type:12,data:l/_},{type:12,data:r},{type:12,data:t.blockSize},...U(...z,i)],V=j=>{let Y=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return`
5831
+ ${j.registerUniforms(Y).declareVariables(...O,A)}
5832
+ ${j.mainStart()}
5833
+ ${j.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
5834
+ let output_indices = ${A.offsetToIndices("global_idx")};
5835
+
5836
+ // Set input x
5837
+ ${(()=>d?`
5838
+ let input = ${S.getByOffset("global_idx / 4")};
5839
+ let x_vec = ${o?"unpack4xI8(input)":"unpack4xU8(input)"};
5840
+ let x_value = ${_===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${S.getByOffset("global_idx")};`)()};
5841
+
5842
+ // Set scale input
5843
+ ${(()=>w?`let scale_value= ${I.getByOffset("0")}`:g?`
5844
+ let scale_index = ${A.indicesGet("output_indices","uniforms.axis")};
5845
+ let scale_value= ${I.getByOffset("scale_index")};`:`
5846
+ var scale_indices: ${I.type.indices} = output_indices;
5847
+ let index = ${I.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size;
5848
+ ${I.indicesSet("scale_indices","uniforms.axis","index")};
5849
+ let scale_value= ${I.getByIndices("scale_indices")};`)()};
5850
+
5851
+ // Set zero-point input
5852
+ ${(()=>T?w?d?`
5853
+ let zero_point_input = ${T.getByOffset("0")};
5854
+ let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
5855
+ let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${T.getByOffset("0")}`:g?d?`
5856
+ let zero_point_index = ${A.indicesGet("output_indices","uniforms.axis")};
5857
+ let zero_point_input = ${T.getByOffset("zero_point_index / 4")};
5858
+ let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
5859
+ let zero_point_value = zero_point_vec[zero_point_index % 4]`:`
5860
+ let zero_point_index = ${A.indicesGet("output_indices","uniforms.axis")};
5861
+ let zero_point_value = ${T.getByOffset("zero_point_index")};`:d?`
5862
+ let zero_point_offset = ${I.indicesToOffset("scale_indices")};
5863
+ let zero_point_input = ${T.getByOffset("zero_point_offset / 4")};
5864
+ let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
5865
+ let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${T.getByIndices("scale_indices")};`:`let zero_point_value = ${d?o?"i32":"u32":S.type.value}(0);`)()};
5866
+ // Compute and write output
5867
+ ${A.setByOffset("global_idx",`${A.type.value}(x_value - zero_point_value) * scale_value`)};
5868
+ }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:T?["rank","rank","rank"]:["rank","rank"]},getShaderSource:V,getRunData:()=>({outputs:[{dims:i,dataType:a}],dispatchGroup:{x:Math.ceil(l/_/64),y:1,z:1},programUniforms:W})}},qd=(e,t)=>{kf(e.inputs,t),e.compute(Ef(e.inputs,t))},Kd=e=>Q({axis:e.axis,blockSize:e.blockSize})});var Pf,Of,Yd,Xd=M(()=>{"use strict";qe();X();re();Pf=(e,t,r)=>{let n=e===t,o=e<t&&r<0,i=e>t&&r>0;if(n||o||i)throw new Error("Range these inputs' contents are invalid.")},Of=(e,t,r,n)=>{let o=Math.abs(Math.ceil((t-e)/r)),i=[o],a=o,l=[{type:12,data:a},{type:n,data:e},{type:n,data:r},...U(i)],d=p=>{let m=D("output",n,i.length),u=m.type.value,h=[{name:"outputSize",type:"u32"},{name:"start",type:u},{name:"delta",type:u}];return`
5832
5869
  ${p.registerUniforms(h).declareVariables(m)}
5833
5870
  ${p.mainStart()}
5834
5871
  ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
5835
5872
  output[global_idx] = uniforms.start + ${u}(global_idx) * uniforms.delta;
5836
- }`};return{name:"Range",shaderCache:{hint:`${n}`},getShaderSource:d,getRunData:()=>({outputs:[{dims:i,dataType:n}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:l})}},Jd=e=>{let t=0,r=0,n=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],n=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],n=e.inputs[2].getFloat32Array()[0]),be.webgpu.validateInputContent&&zf(t,r,n),e.compute(Bf(t,r,n,e.inputs[0].dataType),{inputs:[]})}});var Rf,Mf,Uf,Vf,Nf,Wf,Hf,Gf,Lf,Ff,qf,tl,jf,Kf,Yf,Xf,Zf,rl,nl,ol=U(()=>{"use strict";Q();ae();Ce();ie();Rf=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and
5837
- one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Mf=(e,t,r)=>{t.every(o=>o>=0&&o<r||(()=>{throw new Error("Resize requires axes input values to be positive and less than rank")}));let n=new Array(r).fill(1);return t.forEach((o,i)=>n[o]=e[i]),n},Uf=(e,t,r,n,o,i)=>{let[a,l,d]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],p=e[0].dims.length;if(a>0&&e.length>a&&e[a].dims.length>0)e[a].getFloat32Array().forEach(m=>i.push(m));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(l>0&&e.length>l&&e[l].dims.length>0){if(e[l].getFloat32Array().forEach(m=>n.push(m)),n.length!==0&&n.length!==p&&r>=18&&n.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");Rf(n,t),t.axes.length>0&&Mf(n,t.axes,p).forEach((m,u)=>n[u]=m)}if(d>0&&e.length>d&&(e[d].getBigInt64Array().forEach(m=>o.push(Number(m))),o.length!==p||r>=18&&o.length===t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(n.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(o.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof n<"u"&&typeof o<"u"&&n.length>0&&o.length>p)throw new Error("Resize requires only of scales or sizes to be specified")},Vf=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32,
5873
+ }`};return{name:"Range",shaderCache:{hint:`${n}`},getShaderSource:d,getRunData:()=>({outputs:[{dims:i,dataType:n}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:l})}},Yd=e=>{let t=0,r=0,n=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],n=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],n=e.inputs[2].getFloat32Array()[0]),fe.webgpu.validateInputContent&&Pf(t,r,n),e.compute(Of(t,r,n,e.inputs[0].dataType),{inputs:[]})}});var zf,Bf,Df,Rf,Mf,Uf,Vf,Nf,Wf,Lf,Hf,Zd,Gf,Ff,qf,Kf,jf,Qd,Jd,el=M(()=>{"use strict";X();oe();xe();re();zf=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and
5874
+ one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Bf=(e,t,r)=>{t.every(o=>o>=0&&o<r||(()=>{throw new Error("Resize requires axes input values to be positive and less than rank")}));let n=new Array(r).fill(1);return t.forEach((o,i)=>n[o]=e[i]),n},Df=(e,t,r,n,o,i)=>{let[a,l,d]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],p=e[0].dims.length;if(a>0&&e.length>a&&e[a].dims.length>0)e[a].getFloat32Array().forEach(m=>i.push(m));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(l>0&&e.length>l&&e[l].dims.length>0){if(e[l].getFloat32Array().forEach(m=>n.push(m)),n.length!==0&&n.length!==p&&r>=18&&n.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");zf(n,t),t.axes.length>0&&Bf(n,t.axes,p).forEach((m,u)=>n[u]=m)}if(d>0&&e.length>d&&(e[d].getBigInt64Array().forEach(m=>o.push(Number(m))),o.length!==p||r>=18&&o.length===t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(n.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(o.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof n<"u"&&typeof o<"u"&&n.length>0&&o.length>p)throw new Error("Resize requires only of scales or sizes to be specified")},Rf=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32,
5838
5875
  lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return`return ${t}(xResized) / ${t}(xScale);`;case"pytorch_half_pixel":return`if (lengthResized > 1) {
5839
5876
  return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5;
5840
5877
  } else {
@@ -5859,38 +5896,38 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5859
5896
  const adjustment = ${t}(lengthResized) / outputWidth;
5860
5897
  const center = ${t}(lengthOriginal) / 2;
5861
5898
  const offset = center * (1 - adjustment);
5862
- return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Nf=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Wf=(e,t,r)=>{let n=new Array(r).fill(0).concat(new Array(r).fill(1)),o=e.length===0?n:e.slice();return t.length>0?(t.forEach((i,a)=>{n[i]=o[a],n[a+r]=o[t.length+a]}),n):o},Hf=(e,t,r,n)=>{let o=[];if(r.length>0)if(n.length>0){if(e.forEach(i=>o.push(i)),Math.max(...n)>e.length)throw new Error("axes is out of bound");n.forEach((i,a)=>o[i]=r[a])}else r.forEach(i=>o.push(i));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");o=e.map((i,a)=>Math.round(i*t[a]))}return o},Gf=(e,t,r)=>{let n=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(i=>t[i]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(i=>t[i]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let o=e.slice();return r.axes.length>0?(r.axes.forEach(i=>t[i]=n),r.axes.forEach(i=>o[i]=Math.round(e[i]*t[i]))):(t.fill(n,0,t.length),o.forEach((i,a)=>o[a]=Math.round(i*t[a]))),o},Lf=(e,t,r,n,o)=>`
5899
+ return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Mf=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Uf=(e,t,r)=>{let n=new Array(r).fill(0).concat(new Array(r).fill(1)),o=e.length===0?n:e.slice();return t.length>0?(t.forEach((i,a)=>{n[i]=o[a],n[a+r]=o[t.length+a]}),n):o},Vf=(e,t,r,n)=>{let o=[];if(r.length>0)if(n.length>0){if(e.forEach(i=>o.push(i)),Math.max(...n)>e.length)throw new Error("axes is out of bound");n.forEach((i,a)=>o[i]=r[a])}else r.forEach(i=>o.push(i));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");o=e.map((i,a)=>Math.round(i*t[a]))}return o},Nf=(e,t,r)=>{let n=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(i=>t[i]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(i=>t[i]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let o=e.slice();return r.axes.length>0?(r.axes.forEach(i=>t[i]=n),r.axes.forEach(i=>o[i]=Math.round(e[i]*t[i]))):(t.fill(n,0,t.length),o.forEach((i,a)=>o[a]=Math.round(i*t[a]))),o},Wf=(e,t,r,n,o)=>`
5863
5900
  fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> {
5864
5901
  var original_indices: array<${e.type.value}, ${r.length}>;
5865
5902
  for (var i:u32 = 0; i < ${r.length}; i++) {
5866
5903
  var output_index = ${e.indicesGet("output_indices","i")};
5867
- var scale = ${G("uniforms.scales","i",n)};
5868
- var roi_low = ${G("uniforms.roi","i",o)};
5869
- var roi_hi = ${G("uniforms.roi",`i + ${t.length}`,o)};
5904
+ var scale = ${L("uniforms.scales","i",n)};
5905
+ var roi_low = ${L("uniforms.roi","i",o)};
5906
+ var roi_hi = ${L("uniforms.roi",`i + ${t.length}`,o)};
5870
5907
  if (scale == 1.0) {
5871
5908
  original_indices[i] = ${e.type.value}(output_index);
5872
5909
  } else {
5873
- var input_shape_i = ${G("uniforms.input_shape","i",t.length)};
5874
- var output_shape_i = ${G("uniforms.output_shape","i",r.length)};
5910
+ var input_shape_i = ${L("uniforms.input_shape","i",t.length)};
5911
+ var output_shape_i = ${L("uniforms.output_shape","i",r.length)};
5875
5912
  original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
5876
5913
  input_shape_i, roi_low, roi_hi);
5877
5914
  }
5878
5915
  }
5879
5916
  return original_indices;
5880
- }`,Ff=(e,t,r,n,o,i,a)=>`
5917
+ }`,Lf=(e,t,r,n,o,i,a)=>`
5881
5918
  fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
5882
5919
  var input_indices: ${e.type.indices};
5883
5920
  for (var i:u32 = 0; i < ${n.length}; i++) {
5884
5921
  var output_index = ${t.indicesGet("output_indices","i")};
5885
5922
  var input_index: u32;
5886
- var scale = ${G("uniforms.scales","i",o)};
5923
+ var scale = ${L("uniforms.scales","i",o)};
5887
5924
  if (scale == 1.0) {
5888
5925
  input_index = output_index;
5889
5926
  } else {
5890
- var roi_low = ${G("uniforms.roi","i",i)};
5891
- var roi_hi = ${G("uniforms.roi",`i + ${r.length}`,i)};
5892
- var input_shape_i = ${G("uniforms.input_shape","i",r.length)};
5893
- var output_shape_i = ${G("uniforms.output_shape","i",n.length)};
5927
+ var roi_low = ${L("uniforms.roi","i",i)};
5928
+ var roi_hi = ${L("uniforms.roi",`i + ${r.length}`,i)};
5929
+ var input_shape_i = ${L("uniforms.input_shape","i",r.length)};
5930
+ var output_shape_i = ${L("uniforms.output_shape","i",n.length)};
5894
5931
  var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
5895
5932
  input_shape_i, roi_low, roi_hi);
5896
5933
  if (!${a} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) {
@@ -5908,24 +5945,24 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5908
5945
  ${e.indicesSet("input_indices","i"," input_index")}
5909
5946
  }
5910
5947
  return input_indices;
5911
- }`,qf=(e,t)=>`
5948
+ }`,Hf=(e,t)=>`
5912
5949
  fn checkInputIndices(input_indices: ${e.type.indices}) -> bool {
5913
5950
  for (var i:u32 = 0; i < ${t.length}; i++) {
5914
5951
  var input_index = ${e.indicesGet("input_indices","i")};
5915
- if (input_index < 0 || input_index >= ${G("uniforms.input_shape","i",t.length)}) {
5952
+ if (input_index < 0 || input_index >= ${L("uniforms.input_shape","i",t.length)}) {
5916
5953
  return false;
5917
5954
  }
5918
5955
  }
5919
5956
  return true;
5920
- }`,tl=(e,t,r,n)=>e.rank>n?`
5957
+ }`,Zd=(e,t,r,n)=>e.rank>n?`
5921
5958
  ${e.indicesSet("input_indices",t,"channel")};
5922
5959
  ${e.indicesSet("input_indices",r,"batch")};
5923
- `:"",jf=(e,t,r,n,o)=>{let[a,l,d,p]=r.length===2?[-1,0,1,-1]:[0,2,3,1],m=e.type.value;return`
5960
+ `:"",Gf=(e,t,r,n,o)=>{let[a,l,d,p]=r.length===2?[-1,0,1,-1]:[0,2,3,1],m=e.type.value;return`
5924
5961
  fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${m} {
5925
5962
  var input_indices: ${e.type.indices};
5926
5963
  ${e.indicesSet("input_indices",l,`max(0, min(row, ${r[l]} - 1))`)};
5927
5964
  ${e.indicesSet("input_indices",d,`max(0, min(col, ${r[d]} - 1))`)};
5928
- ${tl(e,p,a,2)}
5965
+ ${Zd(e,p,a,2)}
5929
5966
  return ${e.getByIndices("input_indices")};
5930
5967
  }
5931
5968
 
@@ -5961,7 +5998,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5961
5998
  dy2 = 0.5;
5962
5999
  }
5963
6000
  return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1);
5964
- }`},Kf=(e,t,r,n,o,i,a,l,d,p)=>{let m=r.length===2,u=!0,[h,w]=m?[0,1]:u?[2,3]:[1,2],g=e.type.value,b=x=>{let _=x===h?"row":"col";return`
6001
+ }`},Ff=(e,t,r,n,o,i,a,l,d,p)=>{let m=r.length===2,u=!0,[h,w]=m?[0,1]:u?[2,3]:[1,2],g=e.type.value,y=x=>{let _=x===h?"row":"col";return`
5965
6002
  fn ${_}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${g} {
5966
6003
  var output_index = ${t.indicesGet("output_indices",x)};
5967
6004
  var originalIdx: ${g} = getOriginalCoordinateFromResizedCoordinate(output_index, ${o[x]},
@@ -5985,8 +6022,8 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
5985
6022
  }
5986
6023
  return cubicInterpolation1D(data, coefs);
5987
6024
  }`};return`
5988
- ${b(h)};
5989
- ${b(w)};
6025
+ ${y(h)};
6026
+ ${y(w)};
5990
6027
  fn getCubicInterpolationCoefs(s: ${g}) -> array<${g}, 4> {
5991
6028
  var absS = abs(s);
5992
6029
  var coeffs: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0);
@@ -6009,13 +6046,13 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6009
6046
  var input_indices: ${e.type.indices} = output_indices;
6010
6047
  return colCubicInterpolation(input_indices, output_indices);
6011
6048
  }
6012
- `},Yf=(e,t,r,n,o)=>{let[a,l,d,p,m]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],u=e.type.value;return`
6049
+ `},qf=(e,t,r,n,o)=>{let[a,l,d,p,m]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],u=e.type.value;return`
6013
6050
  fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${u} {
6014
6051
  var input_indices: ${e.type.indices};
6015
6052
  ${e.indicesSet("input_indices",l,`max(0, min(depth, ${r[l]} - 1))`)};
6016
6053
  ${e.indicesSet("input_indices",d,`max(0, min(height, ${r[d]} - 1))`)};
6017
6054
  ${e.indicesSet("input_indices",p,`max(0, min(width, ${r[p]} - 1))`)};
6018
- ${tl(e,m,a,3)}
6055
+ ${Zd(e,m,a,3)}
6019
6056
  return ${e.getByIndices("input_indices")};
6020
6057
  }
6021
6058
 
@@ -6068,18 +6105,18 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6068
6105
  }
6069
6106
  return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 +
6070
6107
  x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1);
6071
- }`},Xf=(e,t,r,n,o,i)=>{let a=e.dims,l=Wf(i,t.axes,a.length),d=Hf(a,n,o,t.axes),p=n.slice();n.length===0&&(p=a.map(($,S)=>$===0?1:d[S]/$),t.keepAspectRatioPolicy!=="stretch"&&(d=Gf(a,p,t)));let m=M("output",e.dataType,d.length),u=E("input",e.dataType,a.length),h=k.size(d),w=a.length===d.length&&a.every(($,S)=>$===d[S]),g=t.coordinateTransformMode==="tf_crop_and_resize",b=t.extrapolationValue,x=u.type.value,_=$=>`
6108
+ }`},Kf=(e,t,r,n,o,i)=>{let a=e.dims,l=Uf(i,t.axes,a.length),d=Vf(a,n,o,t.axes),p=n.slice();n.length===0&&(p=a.map(($,S)=>$===0?1:d[S]/$),t.keepAspectRatioPolicy!=="stretch"&&(d=Nf(a,p,t)));let m=D("output",e.dataType,d.length),u=k("input",e.dataType,a.length),h=E.size(d),w=a.length===d.length&&a.every(($,S)=>$===d[S]),g=t.coordinateTransformMode==="tf_crop_and_resize",y=t.extrapolationValue,x=u.type.value,_=$=>`
6072
6109
  ${w?"":`
6073
- ${Vf(t.coordinateTransformMode,x)};
6110
+ ${Rf(t.coordinateTransformMode,x)};
6074
6111
  ${(()=>{switch(t.mode){case"nearest":return`
6075
- ${qf(u,a)};
6076
- ${Nf(t.nearestMode,r,x)};
6077
- ${Ff(u,m,a,d,p.length,l.length,g)};
6112
+ ${Hf(u,a)};
6113
+ ${Mf(t.nearestMode,r,x)};
6114
+ ${Lf(u,m,a,d,p.length,l.length,g)};
6078
6115
  `;case"linear":return`
6079
- ${Lf(m,a,d,p.length,l.length)};
6080
- ${(()=>{if(a.length===2||a.length===4)return`${jf(u,m,a,g,b)}`;if(a.length===3||a.length===5)return`${Yf(u,m,a,g,b)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()};
6116
+ ${Wf(m,a,d,p.length,l.length)};
6117
+ ${(()=>{if(a.length===2||a.length===4)return`${Gf(u,m,a,g,y)}`;if(a.length===3||a.length===5)return`${qf(u,m,a,g,y)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()};
6081
6118
  `;case"cubic":return`
6082
- ${(()=>{if(a.length===2||a.length===4)return`${Kf(u,m,a,d,p,l,t.cubicCoeffA,g,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()};
6119
+ ${(()=>{if(a.length===2||a.length===4)return`${Ff(u,m,a,d,p,l,t.cubicCoeffA,g,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()};
6083
6120
  `;default:throw Error("Invalid resize mode")}})()};
6084
6121
  `}
6085
6122
  ${$.registerUniform("output_size","u32").registerUniform("scales","f32",p.length).registerUniform("roi","f32",l.length).declareVariables(u,m)}
@@ -6095,10 +6132,10 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6095
6132
  output[global_idx] = ${t.extrapolationValue};
6096
6133
  }`;case"linear":return`output[global_idx] = ${a.length===2||a.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()};
6097
6134
  `}
6098
- }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${p.length>0?p:""}|${o.length>0?o:""}|${l.length>0?l:""}|${w}|${a}`,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:[{dims:d,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:[{type:12,data:h},{type:1,data:p},{type:1,data:l},...V(a,d)]})}},Zf=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},rl=(e,t)=>{let r=[],n=[],o=[],i=Zf(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Uf(e.inputs,t,i,r,n,o),e.compute(Xf(e.inputs[0],t,i,r,n,o),{inputs:[0]})},nl=e=>{let t=e.antialias,r=e.axes,n=e.coordinateTransformMode,o=e.cubicCoeffA,i=e.excludeOutside!==0,a=e.extrapolationValue,l=e.keepAspectRatioPolicy,d=e.mode,p=e.nearestMode===""?"simple":e.nearestMode;return te({antialias:t,axes:r,coordinateTransformMode:n,cubicCoeffA:o,excludeOutside:i,extrapolationValue:a,keepAspectRatioPolicy:l,mode:d,nearestMode:p})}});var Qf,Jf,il,al=U(()=>{"use strict";Q();ae();Ce();ie();Qf=(e,t)=>{let[r,n,o,i]=e,{numHeads:a,rotaryEmbeddingDim:l}=t;if(r.dims.length!==3&&r.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`);if(!k.areEqual(n.dims,[])&&!k.areEqual(n.dims,[1])&&n.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${n.dims.length}`);if(o.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${o.dims.length}`);if(i.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${i.dims.length}`);if(!k.areEqual(o.dims,i.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(l>0&&a===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let d=r.dims[0],p=r.dims[r.dims.length-2],m=o.dims[0],u=k.sizeFromDimension(r.dims,1)/p,h=l===0?o.dims[1]*2:u/a;if(l>h)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(n.dims.length===2){if(d!==n.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${n.dims[0]}`);if(p!==n.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${n.dims[1]}`)}if(h/2!==o.dims[1]&&l/2!==o.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${o.dims[1]}`);if(p>m)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},Jf=(e,t)=>{let{interleaved:r,numHeads:n,rotaryEmbeddingDim:o,scale:i}=t,a=e[0].dims[0],l=k.sizeFromDimension(e[0].dims,1),d=e[0].dims[e[0].dims.length-2],p=l/d,m=e[2].dims[1],u=o===0?m*2:p/n,h=new Array(a,d,p/u,u-m),w=k.computeStrides(h),g=[{type:1,data:i},{type:12,data:h},{type:12,data:w},...e[0].dims.length===3?new Array({type:12,data:[l,p,u,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[l,u,d*u,1]}):[],...V(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],b=x=>{let _=E("input",e[0].dataType,e[0].dims.length),$=E("position_ids",e[1].dataType,e[1].dims.length),S=E("cos_cache",e[2].dataType,e[2].dims.length),I=E("sin_cache",e[3].dataType,e[3].dims.length),T=M("output",e[0].dataType,e[0].dims.length);return x.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:h.length},{name:"global_strides",type:"u32",length:w.length},{name:"input_output_strides",type:"u32",length:w.length}]),`
6135
+ }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${p.length>0?p:""}|${o.length>0?o:""}|${l.length>0?l:""}|${w}|${a}`,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:[{dims:d,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:[{type:12,data:h},{type:1,data:p},{type:1,data:l},...U(a,d)]})}},jf=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},Qd=(e,t)=>{let r=[],n=[],o=[],i=jf(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Df(e.inputs,t,i,r,n,o),e.compute(Kf(e.inputs[0],t,i,r,n,o),{inputs:[0]})},Jd=e=>{let t=e.antialias,r=e.axes,n=e.coordinateTransformMode,o=e.cubicCoeffA,i=e.excludeOutside!==0,a=e.extrapolationValue,l=e.keepAspectRatioPolicy,d=e.mode,p=e.nearestMode===""?"simple":e.nearestMode;return Q({antialias:t,axes:r,coordinateTransformMode:n,cubicCoeffA:o,excludeOutside:i,extrapolationValue:a,keepAspectRatioPolicy:l,mode:d,nearestMode:p})}});var Yf,Xf,tl,rl=M(()=>{"use strict";X();oe();xe();re();Yf=(e,t)=>{let[r,n,o,i]=e,{numHeads:a,rotaryEmbeddingDim:l}=t;if(r.dims.length!==3&&r.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`);if(!E.areEqual(n.dims,[])&&!E.areEqual(n.dims,[1])&&n.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${n.dims.length}`);if(o.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${o.dims.length}`);if(i.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${i.dims.length}`);if(!E.areEqual(o.dims,i.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(l>0&&a===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let d=r.dims[0],p=r.dims[r.dims.length-2],m=o.dims[0],u=E.sizeFromDimension(r.dims,1)/p,h=l===0?o.dims[1]*2:u/a;if(l>h)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(n.dims.length===2){if(d!==n.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${n.dims[0]}`);if(p!==n.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${n.dims[1]}`)}if(h/2!==o.dims[1]&&l/2!==o.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${o.dims[1]}`);if(p>m)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},Xf=(e,t)=>{let{interleaved:r,numHeads:n,rotaryEmbeddingDim:o,scale:i}=t,a=e[0].dims[0],l=E.sizeFromDimension(e[0].dims,1),d=e[0].dims[e[0].dims.length-2],p=l/d,m=e[2].dims[1],u=o===0?m*2:p/n,h=new Array(a,d,p/u,u-m),w=E.computeStrides(h),g=[{type:1,data:i},{type:12,data:h},{type:12,data:w},...e[0].dims.length===3?new Array({type:12,data:[l,p,u,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[l,u,d*u,1]}):[],...U(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],y=x=>{let _=k("input",e[0].dataType,e[0].dims.length),$=k("position_ids",e[1].dataType,e[1].dims.length),S=k("cos_cache",e[2].dataType,e[2].dims.length),I=k("sin_cache",e[3].dataType,e[3].dims.length),T=D("output",e[0].dataType,e[0].dims.length);return x.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:h.length},{name:"global_strides",type:"u32",length:w.length},{name:"input_output_strides",type:"u32",length:w.length}]),`
6099
6136
  ${x.declareVariables(_,$,S,I,T)}
6100
6137
 
6101
- ${x.mainStart(Tt)}
6138
+ ${x.mainStart(St)}
6102
6139
  let half_rotary_emb_dim = uniforms.${S.name}_shape[1];
6103
6140
  let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape;
6104
6141
  let size = uniforms.global_shape[0] * uniforms.global_strides[0];
@@ -6106,7 +6143,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6106
6143
 
6107
6144
  if (bsnh[3] < half_rotary_emb_dim) {
6108
6145
  let position_ids_idx =
6109
- ${$.broadcastedIndicesToOffset("bsnh.xy",M("",$.type.tensor,2))};
6146
+ ${$.broadcastedIndicesToOffset("bsnh.xy",D("",$.type.tensor,2))};
6110
6147
  let position_id =
6111
6148
  u32(${$.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0);
6112
6149
  let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r});
@@ -6121,11 +6158,11 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6121
6158
  let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim;
6122
6159
  ${T.setByOffset("k",_.getByOffset("k"))}
6123
6160
  }
6124
- }`};return{name:"RotaryEmbedding",shaderCache:{hint:te({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:b,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(h)/Tt)},programUniforms:g})}},il=(e,t)=>{Qf(e.inputs,t),e.compute(Jf(e.inputs,t))}});var eh,th,sl,ul=U(()=>{"use strict";Q();ae();ie();eh=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],n=e[2];if(t.dataType!==r.dataType||t.dataType!==n.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let o=t.dims[t.dims.length-1],i=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==o)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==i)throw new Error("Skip must have the same sequence length as input");if(n.dims.length!==1)throw new Error("Gamma must be 1D");if(n.dims[n.dims.length-1]!==o)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let a=e[3];if(a.dims.length!==1)throw new Error("Beta must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let a=e[4];if(a.dims.length!==1)throw new Error("Bias must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Bias must have the same hidden size as input")}},th=(e,t,r,n)=>{let o=t.simplified,i=e[0].dims,a=k.size(i),l=i,d=a,p=i.slice(-1)[0],m=n?i.slice(0,-1).concat(1):[],u=!o&&e.length>3,h=e.length>4,w=n&&r>1,g=n&&r>2,b=r>3,x=64,_=$e(p),$=[{type:12,data:d},{type:12,data:_},{type:12,data:p},{type:1,data:t.epsilon}],S=T=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],D=[E("x",e[0].dataType,e[0].dims,_),E("skip",e[1].dataType,e[1].dims,_),E("gamma",e[2].dataType,e[2].dims,_)];u&&D.push(E("beta",e[3].dataType,e[3].dims,_)),h&&D.push(E("bias",e[4].dataType,e[4].dims,_)),D.push(M("output",e[0].dataType,l,_)),w&&D.push(M("mean_output",1,m)),g&&D.push(M("inv_std_output",1,m)),b&&D.push(M("input_skip_bias_sum",e[0].dataType,l,_));let z=ge(e[0].dataType),H=ge(1,_);return`
6161
+ }`};return{name:"RotaryEmbedding",shaderCache:{hint:Q({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(E.size(h)/St)},programUniforms:g})}},tl=(e,t)=>{Yf(e.inputs,t),e.compute(Xf(e.inputs,t))}});var Zf,Qf,nl,ol=M(()=>{"use strict";X();oe();re();Zf=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],n=e[2];if(t.dataType!==r.dataType||t.dataType!==n.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let o=t.dims[t.dims.length-1],i=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==o)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==i)throw new Error("Skip must have the same sequence length as input");if(n.dims.length!==1)throw new Error("Gamma must be 1D");if(n.dims[n.dims.length-1]!==o)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let a=e[3];if(a.dims.length!==1)throw new Error("Beta must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let a=e[4];if(a.dims.length!==1)throw new Error("Bias must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Bias must have the same hidden size as input")}},Qf=(e,t,r,n)=>{let o=t.simplified,i=e[0].dims,a=E.size(i),l=i,d=a,p=i.slice(-1)[0],m=n?i.slice(0,-1).concat(1):[],u=!o&&e.length>3,h=e.length>4,w=n&&r>1,g=n&&r>2,y=r>3,x=64,_=he(p),$=[{type:12,data:d},{type:12,data:_},{type:12,data:p},{type:1,data:t.epsilon}],S=T=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],O=[k("x",e[0].dataType,e[0].dims,_),k("skip",e[1].dataType,e[1].dims,_),k("gamma",e[2].dataType,e[2].dims,_)];u&&O.push(k("beta",e[3].dataType,e[3].dims,_)),h&&O.push(k("bias",e[4].dataType,e[4].dims,_)),O.push(D("output",e[0].dataType,l,_)),w&&O.push(D("mean_output",1,m)),g&&O.push(D("inv_std_output",1,m)),y&&O.push(D("input_skip_bias_sum",e[0].dataType,l,_));let z=pe(e[0].dataType),W=pe(1,_);return`
6125
6162
 
6126
- ${T.registerUniforms(A).declareVariables(...D)}
6127
- var<workgroup> sum_shared : array<${H}, ${x}>;
6128
- var<workgroup> sum_squared_shared : array<${H}, ${x}>;
6163
+ ${T.registerUniforms(A).declareVariables(...O)}
6164
+ var<workgroup> sum_shared : array<${W}, ${x}>;
6165
+ var<workgroup> sum_squared_shared : array<${W}, ${x}>;
6129
6166
 
6130
6167
  ${T.mainStart([x,1,1])}
6131
6168
  let ix = local_id.x;
@@ -6143,9 +6180,9 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6143
6180
  let bias_value = ${h?"bias[offset1d + i]":z+"(0.0)"};
6144
6181
  let input_value = x[offset + i];
6145
6182
  let value = input_value + skip_value + bias_value;
6146
- ${b?"input_skip_bias_sum[offset + i] = value;":""}
6183
+ ${y?"input_skip_bias_sum[offset + i] = value;":""}
6147
6184
  output[offset + i] = value;
6148
- let f32_value = ${At(z,_,"value")};
6185
+ let f32_value = ${It(z,_,"value")};
6149
6186
  sum_shared[ix] += f32_value;
6150
6187
  sum_squared_shared[ix] += f32_value * f32_value;
6151
6188
  }
@@ -6163,8 +6200,8 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6163
6200
 
6164
6201
  let sum = sum_shared[0];
6165
6202
  let square_sum = sum_squared_shared[0];
6166
- let mean = ${Xe("sum",_)} / f32(uniforms.hidden_size);
6167
- let inv_std_dev = inverseSqrt(${Xe("square_sum",_)} / f32(uniforms.hidden_size) ${o?"":"- mean * mean"} + uniforms.epsilon);
6203
+ let mean = ${je("sum",_)} / f32(uniforms.hidden_size);
6204
+ let inv_std_dev = inverseSqrt(${je("square_sum",_)} / f32(uniforms.hidden_size) ${o?"":"- mean * mean"} + uniforms.epsilon);
6168
6205
  ${w?"mean_output[global_idx] = mean;":""}
6169
6206
  ${g?"inv_std_output[global_idx] = inv_std_dev;":""}
6170
6207
 
@@ -6173,14 +6210,14 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6173
6210
  ${z}(inv_std_dev) * gamma[offset1d + i]
6174
6211
  ${u?"+ beta[offset1d + i]":""};
6175
6212
  }
6176
- }`},I=[{dims:l,dataType:e[0].dataType}];return r>1&&I.push({dims:m,dataType:1}),r>2&&I.push({dims:m,dataType:1}),r>3&&I.push({dims:i,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${_};${w};${g};${b}`,inputDependencies:e.map((T,A)=>"type")},getShaderSource:S,getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(d/p)},programUniforms:$})}},sl=(e,t)=>{eh(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(th(e.inputs,t,e.outputCount,!1),{outputs:n})}});var rh,on,nh,dl,oh,ih,ll,cl,pl=U(()=>{"use strict";Q();ae();Ce();ie();rh=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,n)=>{if(e[n+1].dataType!==6&&e[n+1].dataType!==7)throw new Error(`Input ${n} must be an array of int32 or int64`)})},on=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(n=>r.push(Number(n)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(n=>r.push(Number(n)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},nh=(e,t)=>{if(e.length>1){let r=on(e,1),n=on(e,2),o=on(e,3);return o.length===0&&(o=[...Array(e[0].dims.length).keys()]),te({starts:r,ends:n,axes:o})}else return t},dl=(e,t,r,n,o)=>{let i=e;return e<0&&(i+=r[n[t]]),o[t]<0?Math.max(0,Math.min(i,r[n[t]]-1)):Math.max(0,Math.min(i,r[n[t]]))},oh=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
6213
+ }`},I=[{dims:l,dataType:e[0].dataType}];return r>1&&I.push({dims:m,dataType:1}),r>2&&I.push({dims:m,dataType:1}),r>3&&I.push({dims:i,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${_};${w};${g};${y}`,inputDependencies:e.map((T,A)=>"type")},getShaderSource:S,getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(d/p)},programUniforms:$})}},nl=(e,t)=>{Zf(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(Qf(e.inputs,t,e.outputCount,!1),{outputs:n})}});var Jf,tn,eh,il,th,rh,al,sl,ul=M(()=>{"use strict";X();oe();xe();re();Jf=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,n)=>{if(e[n+1].dataType!==6&&e[n+1].dataType!==7)throw new Error(`Input ${n} must be an array of int32 or int64`)})},tn=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(n=>r.push(Number(n)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(n=>r.push(Number(n)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},eh=(e,t)=>{if(e.length>1){let r=tn(e,1),n=tn(e,2),o=tn(e,3);return o.length===0&&(o=[...Array(e[0].dims.length).keys()]),Q({starts:r,ends:n,axes:o})}else return t},il=(e,t,r,n,o)=>{let i=e;return e<0&&(i+=r[n[t]]),o[t]<0?Math.max(0,Math.min(i,r[n[t]]-1)):Math.max(0,Math.min(i,r[n[t]]))},th=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
6177
6214
  var input_indices: ${e.type.indices};
6178
6215
  var carry = 0u;
6179
6216
  for (var i = ${r.length}; i >= 0; i--) {
6180
- let input_shape_i = ${G("uniforms.input_shape","i",r.length)};
6181
- let steps_i = ${G("uniforms.steps","i",r.length)};
6182
- let signs_i = ${G("uniforms.signs","i",r.length)};
6183
- let starts_i = ${G("uniforms.starts","i",r.length)};
6217
+ let input_shape_i = ${L("uniforms.input_shape","i",r.length)};
6218
+ let steps_i = ${L("uniforms.steps","i",r.length)};
6219
+ let signs_i = ${L("uniforms.signs","i",r.length)};
6220
+ let starts_i = ${L("uniforms.starts","i",r.length)};
6184
6221
  var output_index = ${t.indicesGet("output_indices","i")};
6185
6222
  var input_index = output_index * steps_i + starts_i + carry;
6186
6223
  carry = input_index / input_shape_i;
@@ -6191,15 +6228,15 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6191
6228
  ${e.indicesSet("input_indices","i","input_index")};
6192
6229
  }
6193
6230
  return input_indices;
6194
- }`,ih=(e,t)=>{let r=e[0].dims,n=k.size(r),o=t.axes.length>0?k.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],i=on(e,4);i.forEach(_=>_!==0||(()=>{throw new Error("step cannot be 0")})),i.length===0&&(i=Array(o.length).fill(1));let a=t.starts.map((_,$)=>dl(_,$,r,o,i)),l=t.ends.map((_,$)=>dl(_,$,r,o,i));if(o.length!==a.length||o.length!==l.length)throw new Error("start, ends and axes should have the same number of elements");if(o.length!==r.length)for(let _=0;_<r.length;++_)o.includes(_)||(a.splice(_,0,0),l.splice(_,0,r[_]),i.splice(_,0,1));let d=i.map(_=>Math.sign(_));i.forEach((_,$,S)=>{if(_<0){let I=(l[$]-a[$])/_,T=a[$],A=T+I*i[$];a[$]=A,l[$]=T,S[$]=-_}});let p=r.slice(0);o.forEach((_,$)=>{p[_]=Math.ceil((l[_]-a[_])/i[_])});let m={dims:p,dataType:e[0].dataType},u=M("output",e[0].dataType,p.length),h=E("input",e[0].dataType,e[0].dims.length),w=k.size(p),g=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:a.length},{name:"signs",type:"i32",length:d.length},{name:"steps",type:"u32",length:i.length}],b=[{type:12,data:w},{type:12,data:a},{type:6,data:d},{type:12,data:i},...V(e[0].dims,p)],x=_=>`
6231
+ }`,rh=(e,t)=>{let r=e[0].dims,n=E.size(r),o=t.axes.length>0?E.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],i=tn(e,4);i.forEach(_=>_!==0||(()=>{throw new Error("step cannot be 0")})),i.length===0&&(i=Array(o.length).fill(1));let a=t.starts.map((_,$)=>il(_,$,r,o,i)),l=t.ends.map((_,$)=>il(_,$,r,o,i));if(o.length!==a.length||o.length!==l.length)throw new Error("start, ends and axes should have the same number of elements");if(o.length!==r.length)for(let _=0;_<r.length;++_)o.includes(_)||(a.splice(_,0,0),l.splice(_,0,r[_]),i.splice(_,0,1));let d=i.map(_=>Math.sign(_));i.forEach((_,$,S)=>{if(_<0){let I=(l[$]-a[$])/_,T=a[$],A=T+I*i[$];a[$]=A,l[$]=T,S[$]=-_}});let p=r.slice(0);o.forEach((_,$)=>{p[_]=Math.ceil((l[_]-a[_])/i[_])});let m={dims:p,dataType:e[0].dataType},u=D("output",e[0].dataType,p.length),h=k("input",e[0].dataType,e[0].dims.length),w=E.size(p),g=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:a.length},{name:"signs",type:"i32",length:d.length},{name:"steps",type:"u32",length:i.length}],y=[{type:12,data:w},{type:12,data:a},{type:6,data:d},{type:12,data:i},...U(e[0].dims,p)],x=_=>`
6195
6232
  ${_.registerUniforms(g).declareVariables(h,u)}
6196
- ${oh(h,u,r)}
6233
+ ${th(h,u,r)}
6197
6234
  ${_.mainStart()}
6198
6235
  ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
6199
6236
  let output_indices = ${u.offsetToIndices("global_idx")};
6200
6237
  let input_indices = calculateInputIndices(output_indices);
6201
6238
  ${u.setByOffset("global_idx",h.getByIndices("input_indices"))}
6202
- }`;return{name:"Slice",shaderCache:{hint:`${d.length}_${a.length}_${i.length}`,inputDependencies:["rank"]},getShaderSource:x,getRunData:()=>({outputs:[m],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:b})}},ll=(e,t)=>{rh(e.inputs,t);let r=nh(e.inputs,t);e.compute(ih(e.inputs,r),{inputs:[0]})},cl=e=>{let t=e.starts,r=e.ends,n=e.axes;return te({starts:t,ends:r,axes:n})}});var ah,sh,ml,fl,hl=U(()=>{"use strict";Q();ae();Ce();ie();ah=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},sh=(e,t)=>{let r=e.dims,n=k.size(r),o=64,i=t.axis;if(i<0&&(i=r.length+i),i<r.length-1)throw new Error("softmax only supports last axis for now.");let a=r[i],l=n/a,d=$e(a),p=a/d,m=(x,_)=>_===4?`max(max(${x}.x, ${x}.y), max(${x}.z, ${x}.w))`:_===2?`max(${x}.x, ${x}.y)`:_===3?`max(max(${x}.x, ${x}.y), ${x}.z)`:x,u=E("x",e.dataType,e.dims,d),h=M("result",e.dataType,e.dims,d),w=u.type.value,g=ge(e.dataType)==="f32"?`var threadMax = ${w}(-3.402823e+38f);`:`var threadMax = ${w}(-65504.0h);`,b=x=>`
6239
+ }`;return{name:"Slice",shaderCache:{hint:`${d.length}_${a.length}_${i.length}`,inputDependencies:["rank"]},getShaderSource:x,getRunData:()=>({outputs:[m],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:y})}},al=(e,t)=>{Jf(e.inputs,t);let r=eh(e.inputs,t);e.compute(rh(e.inputs,r),{inputs:[0]})},sl=e=>{let t=e.starts,r=e.ends,n=e.axes;return Q({starts:t,ends:r,axes:n})}});var nh,oh,dl,ll,cl=M(()=>{"use strict";X();oe();xe();re();nh=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},oh=(e,t)=>{let r=e.dims,n=E.size(r),o=64,i=t.axis;if(i<0&&(i=r.length+i),i<r.length-1)throw new Error("softmax only supports last axis for now.");let a=r[i],l=n/a,d=he(a),p=a/d,m=(x,_)=>_===4?`max(max(${x}.x, ${x}.y), max(${x}.z, ${x}.w))`:_===2?`max(${x}.x, ${x}.y)`:_===3?`max(max(${x}.x, ${x}.y), ${x}.z)`:x,u=k("x",e.dataType,e.dims,d),h=D("result",e.dataType,e.dims,d),w=u.type.value,g=pe(e.dataType)==="f32"?`var threadMax = ${w}(-3.402823e+38f);`:`var threadMax = ${w}(-65504.0h);`,y=x=>`
6203
6240
  var<workgroup> rowMaxShared : ${w};
6204
6241
  var<workgroup> rowSumShared : ${w};
6205
6242
  var<workgroup> threadShared : array<${w}, ${o}>;
@@ -6262,7 +6299,7 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6262
6299
  workgroupBarrier();
6263
6300
  }
6264
6301
  if (lindex == 0) {
6265
- rowSumShared = ${w}(${Xe("threadShared[0]",d)});
6302
+ rowSumShared = ${w}(${je("threadShared[0]",d)});
6266
6303
  }
6267
6304
  workgroupBarrier();
6268
6305
 
@@ -6271,22 +6308,22 @@ ${ke}_indices[${ce}] = 0;`}),F+=`${ke}_indices[${J-2}] = 0u;
6271
6308
  let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared;
6272
6309
  setValue(row, col, row_stride, value);
6273
6310
  }
6274
- }`;return{name:"Softmax",shaderCache:{hint:`${d}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.dataType}],dispatchGroup:{x:l},programUniforms:[{type:6,data:p}]}),getShaderSource:b}},ml=(e,t)=>{ah(e.inputs),e.compute(sh(e.inputs[0],t))},fl=e=>te({axis:e.axis})});var uh,dh,lh,ch,ph,gl,yl,bl=U(()=>{"use strict";Q();ae();Ce();ie();uh=e=>{if(!e||e.length<1)throw new Error("too few inputs")},dh=(e,t)=>{let r=[],n=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(o=>r.push(Number(o))),n=r.length),te({numOutputs:n,axis:t.axis,splitSizes:r})},lh=e=>`
6311
+ }`;return{name:"Softmax",shaderCache:{hint:`${d}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.dataType}],dispatchGroup:{x:l},programUniforms:[{type:6,data:p}]}),getShaderSource:y}},dl=(e,t)=>{nh(e.inputs),e.compute(oh(e.inputs[0],t))},ll=e=>Q({axis:e.axis})});var ih,ah,sh,uh,dh,pl,ml,fl=M(()=>{"use strict";X();oe();xe();re();ih=e=>{if(!e||e.length<1)throw new Error("too few inputs")},ah=(e,t)=>{let r=[],n=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(o=>r.push(Number(o))),n=r.length),Q({numOutputs:n,axis:t.axis,splitSizes:r})},sh=e=>`
6275
6312
  fn calculateOutputIndex(index: u32) -> u32 {
6276
6313
  for (var i: u32 = 0u; i < ${e}u; i += 1u ) {
6277
- if (index < ${G("uniforms.size_in_split_axis","i",e)}) {
6314
+ if (index < ${L("uniforms.size_in_split_axis","i",e)}) {
6278
6315
  return i;
6279
6316
  }
6280
6317
  }
6281
6318
  return ${e}u;
6282
- }`,ch=e=>{let t=e.length,r=[];for(let n=0;n<t;++n){let o=e[n].setByIndices("indices","input[global_idx]");t===1?r.push(o):n===0?r.push(`if (output_number == ${n}u) { ${o} }`):n===t-1?r.push(`else { ${o} }`):r.push(`else if (output_number == ${n}) { ${o} }`)}return`
6319
+ }`,uh=e=>{let t=e.length,r=[];for(let n=0;n<t;++n){let o=e[n].setByIndices("indices","input[global_idx]");t===1?r.push(o):n===0?r.push(`if (output_number == ${n}u) { ${o} }`):n===t-1?r.push(`else { ${o} }`):r.push(`else if (output_number == ${n}) { ${o} }`)}return`
6283
6320
  fn writeBufferData(output_number: u32, indices: ${e[0].type.indices}, global_idx: u32) {
6284
6321
  ${r.join(`
6285
6322
  `)}
6286
- }`},ph=(e,t)=>{let r=e[0].dims,n=k.size(r),o=e[0].dataType,i=k.normalizeAxis(t.axis,r.length),a=new Array(t.numOutputs),l=E("input",o,r.length),d=new Array(t.numOutputs),p=[],m=[],u=0,h=[{type:12,data:n}];for(let g=0;g<t.numOutputs;g++){u+=t.splitSizes[g],d[g]=u;let b=r.slice();b[t.axis]=t.splitSizes[g],m.push(b),a[g]=M(`output${g}`,o,b.length),p.push({dims:m[g],dataType:e[0].dataType})}h.push({type:12,data:d},...V(r,...m));let w=g=>`
6323
+ }`},dh=(e,t)=>{let r=e[0].dims,n=E.size(r),o=e[0].dataType,i=E.normalizeAxis(t.axis,r.length),a=new Array(t.numOutputs),l=k("input",o,r.length),d=new Array(t.numOutputs),p=[],m=[],u=0,h=[{type:12,data:n}];for(let g=0;g<t.numOutputs;g++){u+=t.splitSizes[g],d[g]=u;let y=r.slice();y[i]=t.splitSizes[g],m.push(y),a[g]=D(`output${g}`,o,y.length),p.push({dims:m[g],dataType:e[0].dataType})}h.push({type:12,data:d},...U(r,...m));let w=g=>`
6287
6324
  ${g.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",d.length).declareVariables(l,...a)}
6288
- ${lh(d.length)}
6289
- ${ch(a)}
6325
+ ${sh(d.length)}
6326
+ ${uh(a)}
6290
6327
 
6291
6328
  ${g.mainStart()}
6292
6329
  ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")}
@@ -6295,11 +6332,11 @@ fn calculateOutputIndex(index: u32) -> u32 {
6295
6332
  var index = ${l.indicesGet("indices",i)};
6296
6333
  let output_number = calculateOutputIndex(index);
6297
6334
  if (output_number != 0) {
6298
- index -= ${G("uniforms.size_in_split_axis","output_number - 1u",d.length)};
6335
+ index -= ${L("uniforms.size_in_split_axis","output_number - 1u",d.length)};
6299
6336
  ${l.indicesSet("indices",i,"index")};
6300
6337
  }
6301
6338
  writeBufferData(output_number, indices, global_idx);
6302
- }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:p,dispatchGroup:{x:Math.ceil(n/64)},programUniforms:h})}},gl=(e,t)=>{uh(e.inputs);let r=e.inputs.length===1?t:dh(e.inputs,t);e.compute(ph(e.inputs,r),{inputs:[0]})},yl=e=>{let t=e.axis,r=e.splitSizes,n=e.numOutputs<0?r.length:e.numOutputs;if(n!==r.length)throw new Error("numOutputs and splitSizes lengh must be equal");return te({axis:t,numOutputs:n,splitSizes:r})}});var mh,fh,wl,vl=U(()=>{"use strict";Q();ae();ie();mh=(e,t,r,n,o)=>{let i=M("output_data",o,r.length,4),a=E("a_data",t[1].dataType,t[1].dims.length,4),l=E("b_data",t[2].dataType,t[2].dims.length,4),d=E("c_data",t[0].dataType,t[0].dims.length,4),p,m=(u,h,w)=>`select(${h}, ${u}, ${w})`;if(!n)p=i.setByOffset("global_idx",m(a.getByOffset("global_idx"),l.getByOffset("global_idx"),d.getByOffset("global_idx")));else{let u=(h,w,g="")=>{let b=`a_data[index_a${w}][component_a${w}]`,x=`b_data[index_b${w}][component_b${w}]`,_=`bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`;return`
6339
+ }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:p,dispatchGroup:{x:Math.ceil(n/64)},programUniforms:h})}},pl=(e,t)=>{ih(e.inputs);let r=e.inputs.length===1?t:ah(e.inputs,t);e.compute(dh(e.inputs,r),{inputs:[0]})},ml=e=>{let t=e.axis,r=e.splitSizes,n=e.numOutputs<0?r.length:e.numOutputs;if(n!==r.length)throw new Error("numOutputs and splitSizes lengh must be equal");return Q({axis:t,numOutputs:n,splitSizes:r})}});var lh,ch,hl,gl=M(()=>{"use strict";X();oe();re();lh=(e,t,r,n,o)=>{let i=D("output_data",o,r.length,4),a=k("a_data",t[1].dataType,t[1].dims.length,4),l=k("b_data",t[2].dataType,t[2].dims.length,4),d=k("c_data",t[0].dataType,t[0].dims.length,4),p,m=(u,h,w)=>`select(${h}, ${u}, ${w})`;if(!n)p=i.setByOffset("global_idx",m(a.getByOffset("global_idx"),l.getByOffset("global_idx"),d.getByOffset("global_idx")));else{let u=(h,w,g="")=>{let y=`a_data[index_a${w}][component_a${w}]`,x=`b_data[index_b${w}][component_b${w}]`,_=`bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`;return`
6303
6340
  let output_indices${w} = ${i.offsetToIndices(`global_idx * 4u + ${w}u`)};
6304
6341
  let offset_a${w} = ${a.broadcastedIndicesToOffset(`output_indices${w}`,i)};
6305
6342
  let offset_b${w} = ${l.broadcastedIndicesToOffset(`output_indices${w}`,i)};
@@ -6310,7 +6347,7 @@ fn calculateOutputIndex(index: u32) -> u32 {
6310
6347
  let component_a${w} = offset_a${w} % 4u;
6311
6348
  let component_b${w} = offset_b${w} % 4u;
6312
6349
  let component_c${w} = offset_c${w} % 4u;
6313
- ${h}[${w}] = ${g}(${m(b,x,_)});
6350
+ ${h}[${w}] = ${g}(${m(y,x,_)});
6314
6351
  `};o===9?p=`
6315
6352
  var data = vec4<u32>(0);
6316
6353
  ${u("data",0,"u32")}
@@ -6327,10 +6364,10 @@ fn calculateOutputIndex(index: u32) -> u32 {
6327
6364
  ${e.mainStart()}
6328
6365
  ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
6329
6366
  ${p}
6330
- }`},fh=e=>{let t=e[1].dims,r=e[2].dims,n=e[0].dims,o=e[1].dataType,i=!(k.areEqual(t,r)&&k.areEqual(r,n)),a=t,l=k.size(t);if(i){let p=tt.calcShape(tt.calcShape(t,r,!1),n,!1);if(!p)throw new Error("Can't perform where op on the given tensors");a=p,l=k.size(a)}let d=Math.ceil(l/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:p=>mh(p,e,a,i,o),getRunData:()=>({outputs:[{dims:a,dataType:o}],dispatchGroup:{x:Math.ceil(l/64/4)},programUniforms:[{type:12,data:d},...V(n,t,r,a)]})}},wl=e=>{e.compute(fh(e.inputs))}});var $l,_l=U(()=>{"use strict";xs();jr();Cs();As();mu();Su();Tu();co();Fu();Ku();Zu();rd();id();sd();ld();md();gd();Cd();Ad();kd();mo();Dd();_o();Bd();Qd();el();Fr();ol();al();ul();pl();hl();bl();So();Et();Yr();vl();$l=new Map([["Abs",[Es]],["Acos",[ks]],["Acosh",[Ps]],["Add",[fu]],["ArgMax",[_s,io]],["ArgMin",[$s,io]],["Asin",[Os]],["Asinh",[Ds]],["Atan",[zs]],["Atanh",[Bs]],["Attention",[Ss]],["AveragePool",[Gd,Hd]],["BatchNormalization",[Is]],["BiasAdd",[Ts]],["BiasSplitGelu",[pu]],["Cast",[Ms,Rs]],["Ceil",[Vs]],["Clip",[Us]],["Concat",[Iu,Cu]],["Conv",[yo,go]],["ConvTranspose",[Lu,Gu]],["Cos",[Ns]],["Cosh",[Ws]],["CumSum",[qu,ju]],["DepthToSpace",[Yu,Xu]],["Div",[hu]],["Einsum",[ed,td]],["Elu",[Hs,Qt]],["Equal",[gu]],["Erf",[Gs]],["Exp",[Ls]],["Expand",[od]],["FastGelu",[ad]],["Floor",[Fs]],["FusedConv",[yo,go]],["Gather",[dd,ud]],["GatherElements",[pd,cd]],["Gelu",[qs]],["Gemm",[hd,fd]],["GlobalAveragePool",[qd,Fd]],["GlobalMaxPool",[Zd,Xd]],["Greater",[vu]],["GreaterOrEqual",[_u]],["GroupQueryAttention",[Id,Sd]],["HardSigmoid",[eu,Js]],["InstanceNormalization",[Td]],["LayerNormalization",[Ed]],["LeakyRelu",[js,Qt]],["Less",[$u]],["LessOrEqual",[xu]],["Log",[du]],["MatMul",[Uu]],["MatMulNBits",[Pd,Od]],["MaxPool",[Kd,Yd]],["Mul",[yu]],["MultiHeadAttention",[wd,bd]],["Neg",[Ys]],["Not",[Ks]],["Pad",[zd]],["Pow",[bu]],["QuickGelu",[lu,Qt]],["Range",[Jd]],["Reciprocal",[Xs]],["ReduceMin",[hs]],["ReduceMean",[ls]],["ReduceMax",[fs]],["ReduceSum",[ys]],["ReduceProd",[gs]],["ReduceL1",[cs]],["ReduceL2",[ps]],["ReduceLogSum",[ws]],["ReduceLogSumExp",[ms]],["ReduceSumSquare",[bs]],["Relu",[Zs]],["Resize",[rl,nl]],["RotaryEmbedding",[il]],["Sigmoid",[Qs]],["Sin",[tu]],["Sinh",[ru]],["Slice",[ll,cl]],["SkipLayerNormalization",[sl]],["Split",[gl,yl]],["Sqrt",[nu]],["Softmax",[ml,fl]],["Sub",[wu]],["Tan",[ou]],["Tanh",[au]],["ThresholdedRelu",[uu,Qt]],["Tile",[$d]],["Transpose",[Za,Qa]],["Where",[wl]]])});var an,xl=U(()=>{"use strict";Ke();dt();ie();an=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,n,o,i){We(t.programInfo.name);let a=this.backend.device,l=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let d=[];for(let m of r)d.push({binding:d.length,resource:{buffer:m.buffer}});for(let m of n)d.push({binding:d.length,resource:{buffer:m.buffer}});i&&d.push({binding:d.length,resource:i});let p=a.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:d,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let m={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:p,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(m)}l.setPipeline(t.computePipeline),l.setBindGroup(0,p),l.dispatchWorkgroups(...o),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Me(t.programInfo.name)}dispose(){}build(t,r){We(t.name);let n=this.backend.device,o=[];n.features.has("shader-f16")&&o.push("enable f16;");let i=Ya(r,this.backend.device.limits),a=t.getShaderSource(i),l=`${o.join(`
6367
+ }`},ch=e=>{let t=e[1].dims,r=e[2].dims,n=e[0].dims,o=e[1].dataType,i=!(E.areEqual(t,r)&&E.areEqual(r,n)),a=t,l=E.size(t);if(i){let p=Je.calcShape(Je.calcShape(t,r,!1),n,!1);if(!p)throw new Error("Can't perform where op on the given tensors");a=p,l=E.size(a)}let d=Math.ceil(l/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:p=>lh(p,e,a,i,o),getRunData:()=>({outputs:[{dims:a,dataType:o}],dispatchGroup:{x:Math.ceil(l/64/4)},programUniforms:[{type:12,data:d},...U(n,t,r,a)]})}},hl=e=>{e.compute(ch(e.inputs))}});var yl,bl=M(()=>{"use strict";gs();Gr();ws();$s();au();yu();vu();ao();Uu();Wu();Gu();Yu();Qu();ed();nd();ad();dd();wd();$d();xd();uo();Cd();bo();Ad();Fd();jd();Xd();Lr();el();rl();ol();ul();cl();fl();vo();Ct();qr();gl();yl=new Map([["Abs",[_s]],["Acos",[xs]],["Acosh",[Ss]],["Add",[su]],["ArgMax",[hs,to]],["ArgMin",[fs,to]],["Asin",[Is]],["Asinh",[Cs]],["Atan",[Ts]],["Atanh",[As]],["Attention",[ys]],["AveragePool",[Rd,Dd]],["BatchNormalization",[bs]],["BiasAdd",[vs]],["BiasSplitGelu",[iu]],["Cast",[Es,ks]],["Ceil",[Os]],["Clip",[Ps]],["Concat",[bu,wu]],["Conv",[mo,po]],["ConvTranspose",[Mu,Ru]],["Cos",[zs]],["Cosh",[Bs]],["CumSum",[Vu,Nu]],["DepthToSpace",[Lu,Hu]],["DequantizeLinear",[qd,Kd]],["Div",[uu]],["Einsum",[Ku,ju]],["Elu",[Ds,Yt]],["Equal",[du]],["Erf",[Rs]],["Exp",[Ms]],["Expand",[Zu]],["FastGelu",[Ju]],["Floor",[Us]],["FusedConv",[mo,po]],["Gather",[rd,td]],["GatherElements",[id,od]],["Gelu",[Vs]],["Gemm",[ud,sd]],["GlobalAveragePool",[Vd,Ud]],["GlobalMaxPool",[Gd,Hd]],["Greater",[mu]],["GreaterOrEqual",[hu]],["GroupQueryAttention",[bd,yd]],["HardSigmoid",[Ks,qs]],["InstanceNormalization",[vd]],["LayerNormalization",[_d]],["LeakyRelu",[Ns,Yt]],["Less",[fu]],["LessOrEqual",[gu]],["Log",[ru]],["MatMul",[Pu]],["MatMulNBits",[Sd,Id]],["MaxPool",[Wd,Ld]],["Mul",[lu]],["MultiHeadAttention",[pd,cd]],["Neg",[Ls]],["Not",[Ws]],["Pad",[Td]],["Pow",[cu]],["QuickGelu",[nu,Yt]],["Range",[Yd]],["Reciprocal",[Hs]],["ReduceMin",[us]],["ReduceMean",[ns]],["ReduceMax",[ss]],["ReduceSum",[ls]],["ReduceProd",[ds]],["ReduceL1",[os]],["ReduceL2",[is]],["ReduceLogSum",[ps]],["ReduceLogSumExp",[as]],["ReduceSumSquare",[cs]],["Relu",[Gs]],["Resize",[Qd,Jd]],["RotaryEmbedding",[tl]],["Sigmoid",[Fs]],["Sin",[js]],["Sinh",[Ys]],["Slice",[al,sl]],["SkipLayerNormalization",[nl]],["Split",[pl,ml]],["Sqrt",[Xs]],["Softmax",[dl,ll]],["Sub",[pu]],["Tan",[Zs]],["Tanh",[Js]],["ThresholdedRelu",[tu,Yt]],["Tile",[fd]],["Transpose",[Ga,Fa]],["Where",[hl]]])});var rn,wl=M(()=>{"use strict";qe();st();re();rn=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,n,o,i){Ve(t.programInfo.name);let a=this.backend.device,l=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let d=[];for(let m of r)d.push({binding:d.length,resource:{buffer:m.buffer}});for(let m of n)d.push({binding:d.length,resource:{buffer:m.buffer}});i&&d.push({binding:d.length,resource:i});let p=a.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:d,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let m={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:p,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(m)}l.setPipeline(t.computePipeline),l.setBindGroup(0,p),l.dispatchWorkgroups(...o),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),De(t.programInfo.name)}dispose(){}build(t,r){Ve(t.name);let n=this.backend.device,o=[];n.features.has("shader-f16")&&o.push("enable f16;");let i=La(r,this.backend.device.limits),a=t.getShaderSource(i),l=`${o.join(`
6331
6368
  `)}
6332
6369
  ${i.additionalImplementations}
6333
- ${a}`,d=n.createShaderModule({code:l,label:t.name});ve("verbose",()=>`[WebGPU] ${t.name} shader code: ${l}`);let p=n.createComputePipeline({compute:{module:d,entryPoint:"main"},layout:"auto",label:t.name});return Me(t.name),{programInfo:t,computePipeline:p,uniformVariablesInfo:i.variablesInfo}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,n=typeof t=="number"?1:t.y||1,o=typeof t=="number"?1:t.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=i&&n<=i&&o<=i)return[r,n,o];let a=r*n*o,l=Math.ceil(Math.sqrt(a));if(l>i){if(l=Math.ceil(Math.cbrt(a)),l>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[l,l,l]}else return[l,l,1]}}});var hh,gh,Io,sn,Sl=U(()=>{"use strict";Ke();Q();dt();Ga();Ka();_l();xl();hh=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let n=0;n<e.length;++n){let o=e[n].dataType;switch(t[n]){case"none":{r.push("");break}case"type":{r.push(`${o}`);break}case"rank":{let i=e[n].dims.length;r.push(`${o};${i}`);break}case"dims":{let i=e[n].dims.join(",");r.push(`${o};${i}`);break}default:throw new Error(`unsupported input dependency: ${t[n]}`)}}return r.join("|")},gh=(e,t,r)=>{let n=e.name;return e.shaderCache?.hint&&(n+="["+e.shaderCache.hint+"]"),n+=":"+r+`:${hh(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,n},Io=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},sn=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n};r.features.has("chromium-experimental-timestamp-query-inside-passes")?n.push("chromium-experimental-timestamp-query-inside-passes"):r.features.has("timestamp-query")&&n.push("timestamp-query"),r.features.has("shader-f16")&&n.push("shader-f16"),this.device=await r.requestDevice(o),this.adapterInfo=new Io(r.info||await r.requestAdapterInfo()),this.gpuDataManager=ja(this),this.programManager=new an(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Wa(t.logLevel,!!t.debug),this.device.onuncapturederror=i=>{i.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${i.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;We(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),n=this.pendingQueries.get(t);for(let o=0;o<r.length/2;o++){let i=n[o],a=i.kernelId,l=this.kernels.get(a),d=l.kernelType,p=l.kernelName,m=i.programName,u=i.inputTensorViews,h=i.outputTensorViews,w=r[o*2],g=r[o*2+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=w);let b=Number(w-this.queryTimeBase),x=Number(g-this.queryTimeBase);if(!Number.isSafeInteger(b)||!Number.isSafeInteger(x))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:u.map(_=>({dims:_.dims,dataType:ht(_.dataType)})),outputsMetadata:h.map(_=>({dims:_.dims,dataType:ht(_.dataType)})),kernelId:a,kernelType:d,kernelName:p,programName:m,startTime:b,endTime:x});else{let _="";u.forEach((S,I)=>{_+=`input[${I}]: [${S.dims}] | ${ht(S.dataType)}, `});let $="";h.forEach((S,I)=>{$+=`output[${I}]: [${S.dims}] | ${ht(S.dataType)}, `}),console.log(`[profiling] kernel "${a}|${d}|${p}|${m}" ${_}${$}execution time: ${x-b} ns`)}Sr("GPU",`${m}::${w}::${g}`)}t.unmap(),this.pendingQueries.delete(t)}),Me()}run(t,r,n,o,i,a){We(t.name);let l=[];for(let S=0;S<r.length;++S){let I=r[S].data;if(I===0)continue;let T=this.gpuDataManager.get(I);if(!T)throw new Error(`no GPU data for input: ${I}`);l.push(T)}let{outputs:d,dispatchGroup:p,programUniforms:m}=t.getRunData(r),u=n.length===0?d.map((S,I)=>I):n;if(u.length!==d.length)throw new Error(`Output size ${u.length} must be equal to ${d.length}.`);let h=[],w=[];for(let S=0;S<d.length;++S){if(!Number.isInteger(u[S])||u[S]<-3||u[S]>=a)throw new Error(`Invalid output index: ${u[S]}`);if(u[S]===-3)continue;let I=u[S]===-1,T=u[S]===-2,A=I||T?i(d[S].dataType,d[S].dims):o(u[S],d[S].dataType,d[S].dims);if(h.push(A),A.data===0)continue;let D=this.gpuDataManager.get(A.data);if(!D)throw new Error(`no GPU data for output: ${A.data}`);if(I&&this.temporaryData.push(D),T){let z=this.kernelPersistentData.get(this.currentKernelId);z||(z=[],this.kernelPersistentData.set(this.currentKernelId,z)),z.push(D)}w.push(D)}if(l.length!==r.length||w.length!==h.length){if(w.length===0)return Me(t.name),h;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let g;if(m){let S=0,I=[];m.forEach(z=>{let H=typeof z.data=="number"?[z.data]:z.data;if(H.length===0)return;let W=z.type===10?2:4,X,Y;z.type===10?(Y=H.length>4?16:H.length>2?8:H.length*W,X=H.length>4?16:W*H.length):(Y=H.length<=2?H.length*W:16,X=16),S=Math.ceil(S/Y)*Y,I.push(S);let pe=z.type===10?8:4;S+=H.length>4?Math.ceil(H.length/pe)*X:H.length*W});let T=16;S=Math.ceil(S/T)*T;let A=new ArrayBuffer(S);m.forEach((z,H)=>{let W=I[H],X=typeof z.data=="number"?[z.data]:z.data;if(z.type===6)new Int32Array(A,W,X.length).set(X);else if(z.type===12)new Uint32Array(A,W,X.length).set(X);else if(z.type===10)new Uint16Array(A,W,X.length).set(X);else if(z.type===1)new Float32Array(A,W,X.length).set(X);else throw new Error(`Unsupported uniform type: ${ht(z.type)}`)});let D=this.gpuDataManager.create(S,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(D.buffer,0,A,0,S),this.gpuDataManager.release(D.id),g={offset:0,size:S,buffer:D.buffer}}let b=this.programManager.normalizeDispatchGroupSize(p),x=b[1]===1&&b[2]===1,_=gh(t,r,x),$=this.programManager.getArtifact(_);if($||($=this.programManager.build(t,b),this.programManager.setArtifact(_,$),ve("info",()=>`[artifact] key: ${_}, programName: ${t.name}`)),m&&$.uniformVariablesInfo){if(m.length!==$.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${$.uniformVariablesInfo.length}, got ${m.length} in program "${$.programInfo.name}".`);for(let S=0;S<m.length;S++){let I=m[S],T=I.type,A=typeof I.data=="number"?1:I.data.length,[D,z]=$.uniformVariablesInfo[S];if(T!==D||A!==z)throw new Error(`Uniform variable ${S} mismatch: expect type ${D} with size ${z}, got type ${T} with size ${A} in program "${$.programInfo.name}".`)}}if(ve("info",()=>`[ProgramManager] run "${t.name}" (key=${_}) with ${b[0]}x${b[1]}x${b[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let S={kernelId:this.currentKernelId,programName:$.programInfo.name,inputTensorViews:r,outputTensorViews:h};this.pendingKernels.push(S),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(S)}return this.programManager.run($,l,w,b,g),Me(t.name),h}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,n,o){let i=$l.get(t);if(!i)throw new Error(`kernel not implemented: ${t}`);let a={kernelType:t,kernelName:o,kernelEntry:i[0],attributes:[i[1],n]};this.kernels.set(r,a)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let n of r)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,n){let o=this.kernels.get(t);if(!o)throw new Error(`kernel not created: ${t}`);let i=o.kernelType,a=o.kernelName,l=o.kernelEntry,d=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`);this.currentKernelId=t,d[0]&&(d[1]=d[0](d[1]),d[0]=void 0),ve("info",()=>`[WebGPU] Start to run kernel "[${i}] ${a}"...`);let p=this.env.debug;this.temporaryData=[];try{return p&&this.device.pushErrorScope("validation"),l(r,d[1]),0}catch(m){return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${m}`)),1}finally{p&&n.push(this.device.popErrorScope().then(m=>m?`GPU validation error for kernel "[${i}] ${a}": ${m.message}`:null));for(let m of this.temporaryData)this.gpuDataManager.release(m.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,n,o){let i=this.sessionExternalDataMapping.get(t);i||(i=new Map,this.sessionExternalDataMapping.set(t,i));let a=i.get(r),l=this.gpuDataManager.registerExternalBuffer(n,o,a?.[1]);return i.set(r,[l,n]),l}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[1])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,n){return async()=>{let o=await Qn(this,t,r);return Ha(o.buffer,n)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){ve("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){ve("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){ve("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),n=t.length;this.pendingKernels=[];for(let o=0;o<n;o++){let i=this.getComputePassEncoder(),a=t[o];this.writeTimestamp(this.pendingDispatchNumber*2),i.setPipeline(a.computePipeline),i.setBindGroup(0,a.bindGroup),i.dispatchWorkgroups(...a.dispatchGroup),this.writeTimestamp(this.pendingDispatchNumber*2+1),this.pendingDispatchNumber++,this.queryType!=="none"&&this.pendingKernels.push(r[o]),(this.pendingDispatchNumber>=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var Il={};Ft(Il,{init:()=>yh});var or,Co,yh,Cl=U(()=>{"use strict";Q();Sl();dt();ae();or=class e{constructor(t,r,n,o){this.module=t;this.dataType=r;this.data=n;this.dims=o}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(k.size(t)!==k.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},Co=class{constructor(t,r,n){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let o=t.HEAPU32,i=n>>>2;this.opKernelContext=o[i++];let a=o[i++];this.outputCount=o[i++],this.customDataOffset=o[i++],this.customDataSize=o[i++];let l=[];for(let d=0;d<a;d++){let p=o[i++],m=o[i++],u=o[i++],h=[];for(let w=0;w<u;w++)h.push(o[i++]);l.push(new or(t,p,m,h))}this.inputs=l}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}getMaxComputeWorkgroupSizes(){return[this.backend.device.limits.maxComputeWorkgroupSizeX,this.backend.device.limits.maxComputeWorkgroupSizeY,this.backend.device.limits.maxComputeWorkgroupSizeZ]}getMaxComputeWorkgroupStoragesize(){return this.backend.device.limits.maxComputeWorkgroupStorageSize}compute(t,r){let n=r?.inputs?.map(l=>typeof l=="number"?this.inputs[l]:l)??this.inputs,o=r?.outputs??[],i=(l,d,p)=>new or(this.module,d,this.output(l,p),p),a=(l,d)=>{let p=It(l);if(!p)throw new Error(`Unsupported data type: ${l}`);let m=p*k.size(d),u=m>0?this.backend.gpuDataManager.create(m).id:0;return new or(this.module,l,u,d)};return this.backend.run(t,n,o,i,a,this.outputCount)}output(t,r){let n=this.module.stackSave();try{let o=this.module.stackAlloc((1+r.length)*4),i=o>>2;this.module.HEAPU32[i++]=r.length;for(let a=0;a<r.length;a++)this.module.HEAPU32[i++]=r[a];return this.module._JsepOutput(this.opKernelContext,t,o)}catch(o){throw new Error(`Failed to generate kernel's output[${t}] with dims [${r}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${o}`)}finally{this.module.stackRestore(n)}}},yh=async(e,t,r,n)=>{let o=t.jsepInit;if(!o)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let i=new sn;await i.initialize(r,n),o("webgpu",[i,a=>i.alloc(a),a=>i.free(a),(a,l,d,p=!1)=>{if(p)ve("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${a}, dst=${l}, size=${d}`),i.memcpy(a,l);else{ve("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${a}, gpuDataId=${l}, size=${d}`);let m=t.HEAPU8.subarray(a>>>0,(a>>>0)+d);i.upload(l,m)}},async(a,l,d)=>{ve("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${a}, dataOffset=${l}, size=${d}`),await i.download(a,()=>t.HEAPU8.subarray(l>>>0,(l>>>0)+d))},(a,l,d)=>i.createKernel(a,l,d,t.UTF8ToString(t._JsepGetNodeName(l))),a=>i.releaseKernel(a),(a,l,d,p)=>{ve("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${d}, kernel=${a}, contextDataOffset=${l}`);let m=new Co(t,i,l);return i.computeKernel(a,m,p)},()=>i.captureBegin(),()=>i.captureEnd(),()=>i.replay()])}else o("webnn")}});var bh,Er,kr,kt,wh,Kt,Pr,Or,Tl,Dr,zr,Br,Hn=U(()=>{"use strict";Ra();Ua();Q();St();Mr();Kn();bh=(e,t)=>{Ie()._OrtInit(e,t)!==0&&_e("Can't initialize onnxruntime.")},Er=async e=>{bh(e.wasm.numThreads,Xt(e.logLevel))},kr=async(e,t)=>{{let r=(Cl(),br(Il)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let n=e.webgpu.adapter;if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let i=e.webgpu.forceFallbackAdapter;if(i!==void 0&&typeof i!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:i}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await r("webgpu",Ie(),e,n)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",Ie(),e)}}},kt=new Map,wh=e=>{let t=Ie(),r=t.stackSave();try{let n=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,n,n+4)!==0&&_e("Can't get session input/output count."),[t.HEAP32[n/4],t.HEAP32[n/4+1]]}finally{t.stackRestore(r)}},Kt=e=>{let t=Ie(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},Pr=async(e,t)=>{let r,n,o=Ie();Array.isArray(e)?[r,n]=e:e.buffer===o.HEAPU8.buffer?[r,n]=[e.byteOffset,e.byteLength]:[r,n]=Kt(e);let i=0,a=0,l=0,d=[],p=[],m=[];try{if([a,d]=Ma(t),t?.externalData&&o.mountExternalData){let $=[];for(let S of t.externalData){let I=typeof S=="string"?S:S.path;$.push(Zt(typeof S=="string"?S:S.data).then(T=>{o.mountExternalData(I,T)}))}await Promise.all($)}for(let $ of t?.executionProviders??[])if((typeof $=="string"?$:$.name)==="webnn"){if(o.currentContext)throw new Error("WebNN execution provider is already set.");if(typeof $!="string"){let I=$,T=I?.context,A=I?.gpuDevice,D=I?.deviceType,z=I?.numThreads,H=I?.powerPreference;T?o.currentContext=T:A?o.currentContext=await navigator.ml.createContext(A):o.currentContext=await navigator.ml.createContext({deviceType:D,numThreads:z,powerPreference:H})}else o.currentContext=await navigator.ml.createContext();break}i=await o._OrtCreateSession(r,n,a),i===0&&_e("Can't create a session."),o.currentContext&&(o.currentContext=void 0);let[u,h]=wh(i),w=!!t?.enableGraphCapture,g=[],b=[],x=[];for(let $=0;$<u;$++){let S=o._OrtGetInputName(i,$);S===0&&_e("Can't get an input name."),p.push(S),g.push(o.UTF8ToString(S))}for(let $=0;$<h;$++){let S=o._OrtGetOutputName(i,$);S===0&&_e("Can't get an output name."),m.push(S);let I=o.UTF8ToString(S);b.push(I);{if(w&&t?.preferredOutputLocation===void 0){x.push("gpu-buffer");continue}let T=typeof t?.preferredOutputLocation=="string"?t.preferredOutputLocation:t?.preferredOutputLocation?.[I]??"cpu";if(T!=="cpu"&&T!=="cpu-pinned"&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}.`);if(w&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);x.push(T)}}let _=null;return x.some($=>$==="gpu-buffer")&&(l=o._OrtCreateBinding(i),l===0&&_e("Can't create IO binding."),_={handle:l,outputPreferredLocations:x,outputPreferredLocationsEncoded:x.map($=>jn($))}),kt.set(i,[i,p,m,_,w,!1]),[i,g,b]}catch(u){throw p.forEach(h=>o._OrtFree(h)),m.forEach(h=>o._OrtFree(h)),l!==0&&o._OrtReleaseBinding(l),i!==0&&o._OrtReleaseSession(i),u}finally{o._free(r),a!==0&&o._OrtReleaseSessionOptions(a),d.forEach(u=>o._free(u)),o.unmountExternalData?.()}},Or=e=>{let t=Ie(),r=kt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[n,o,i,a,l]=r;a&&(l&&t._OrtClearBoundOutputs(a.handle),t._OrtReleaseBinding(a.handle)),t.jsepOnReleaseSession?.(e),o.forEach(d=>t._OrtFree(d)),i.forEach(d=>t._OrtFree(d)),t._OrtReleaseSession(n),kt.delete(e)},Tl=(e,t,r,n,o,i=!1)=>{if(!e){t.push(0);return}let a=Ie(),l=e[0],d=e[1],p=e[3],m,u;if(l==="string"&&p==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");if(i&&p!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`);if(p==="gpu-buffer"){let g=e[2].gpuBuffer,b=It(qn(l));u=d.reduce((_,$)=>_*$,1)*b;let x=a.jsepRegisterBuffer;if(!x)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');m=x(n,o,g,u)}else{let g=e[2];if(Array.isArray(g)){u=4*g.length,m=a._malloc(u),r.push(m);let b=m/4;for(let x=0;x<g.length;x++){if(typeof g[x]!="string")throw new TypeError(`tensor data at index ${x} is not a string`);a.HEAPU32[b++]=Te(g[x],r)}}else u=g.byteLength,m=a._malloc(u),r.push(m),a.HEAPU8.set(new Uint8Array(g.buffer,g.byteOffset,u),m)}let h=a.stackSave(),w=a.stackAlloc(4*d.length);try{let g=w/4;d.forEach(x=>a.HEAP32[g++]=x);let b=a._OrtCreateTensor(qn(l),m,u,w,d.length,jn(p));b===0&&_e(`Can't create tensor for input/output. session=${n}, index=${o}.`),t.push(b)}finally{a.stackRestore(h)}},Dr=async(e,t,r,n,o,i)=>{let a=Ie(),l=kt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let d=l[0],p=l[1],m=l[2],u=l[3],h=l[4],w=l[5],g=t.length,b=n.length,x=0,_=[],$=[],S=[],I=[],T=a.stackSave(),A=a.stackAlloc(g*4),D=a.stackAlloc(g*4),z=a.stackAlloc(b*4),H=a.stackAlloc(b*4);try{[x,_]=Ba(i);for(let K=0;K<g;K++)Tl(r[K],$,I,e,t[K],h);for(let K=0;K<b;K++)Tl(o[K],S,I,e,g+n[K],h);let W=A/4,X=D/4,Y=z/4,pe=H/4;for(let K=0;K<g;K++)a.HEAPU32[W++]=$[K],a.HEAPU32[X++]=p[t[K]];for(let K=0;K<b;K++)a.HEAPU32[Y++]=S[K],a.HEAPU32[pe++]=m[n[K]];if(u&&!w){let{handle:K,outputPreferredLocations:de,outputPreferredLocationsEncoded:re}=u;if(p.length!==g)throw new Error(`input count from feeds (${g}) is expected to be always equal to model's input count (${p.length}).`);for(let se=0;se<g;se++){let Z=t[se];await a._OrtBindInput(K,p[Z],$[se])!==0&&_e(`Can't bind input[${se}] for session=${e}.`)}for(let se=0;se<b;se++){let Z=n[se];o[se]?.[3]?a._OrtBindOutput(K,m[Z],S[se],0)!==0&&_e(`Can't bind pre-allocated output[${se}] for session=${e}.`):a._OrtBindOutput(K,m[Z],0,re[Z])!==0&&_e(`Can't bind output[${se}] to ${de[se]} for session=${e}.`)}kt.set(e,[d,p,m,u,h,!0])}a.jsepOnRunStart?.(d);let ue;u?ue=await a._OrtRunWithBinding(d,u.handle,b,z,x):ue=await a._OrtRun(d,D,A,g,H,b,z,x),ue!==0&&_e("failed to call OrtRun().");let le=[];for(let K=0;K<b;K++){let de=a.HEAPU32[z/4+K];if(de===S[K]){le.push(o[K]);continue}let re=a.stackSave(),se=a.stackAlloc(4*4),Z=!1,ne,J=0;try{a._OrtGetTensorData(de,se,se+4,se+8,se+12)!==0&&_e(`Can't access output tensor data on index ${K}.`);let R=se/4,F=a.HEAPU32[R++];J=a.HEAPU32[R++];let ce=a.HEAPU32[R++],Re=a.HEAPU32[R++],Se=[];for(let Pe=0;Pe<Re;Pe++)Se.push(a.HEAPU32[ce/4+Pe]);a._OrtFree(ce);let we=Se.reduce((Pe,ze)=>Pe*ze,1);ne=ht(F);let Lt=u?.outputPreferredLocations[n[K]];if(ne==="string"){if(Lt==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");let Pe=[],ze=J/4;for(let Fe=0;Fe<we;Fe++){let Ot=a.HEAPU32[ze++],yt=Fe===we-1?void 0:a.HEAPU32[ze]-Ot;Pe.push(a.UTF8ToString(Ot,yt))}le.push([ne,Se,Pe,"cpu"])}else if(Lt==="gpu-buffer"&&we>0){let Pe=a.jsepGetBuffer;if(!Pe)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let ze=Pe(J),Fe=It(F);if(Fe===void 0||!Vr(ne))throw new Error(`Unsupported data type: ${ne}`);Z=!0,le.push([ne,Se,{gpuBuffer:ze,download:a.jsepCreateDownloader(ze,we*Fe,ne),dispose:()=>{a._OrtReleaseTensor(de)}},"gpu-buffer"])}else{let Pe=Ur(ne),ze=new Pe(we);new Uint8Array(ze.buffer,ze.byteOffset,ze.byteLength).set(a.HEAPU8.subarray(J,J+ze.byteLength)),le.push([ne,Se,ze,"cpu"])}}finally{a.stackRestore(re),ne==="string"&&J&&a._free(J),Z||a._OrtReleaseTensor(de)}}return u&&!h&&(a._OrtClearBoundOutputs(u.handle),kt.set(e,[d,p,m,u,h,!1])),le}finally{a.stackRestore(T),$.forEach(W=>a._OrtReleaseTensor(W)),S.forEach(W=>a._OrtReleaseTensor(W)),I.forEach(W=>a._free(W)),x!==0&&a._OrtReleaseRunOptions(x),_.forEach(W=>a._free(W))}},zr=e=>{let t=Ie(),r=kt.get(e);if(!r)throw new Error("invalid session id");let n=r[0],o=t._OrtEndProfiling(n);o===0&&_e("Can't get an profile file name."),t._OrtFree(o)},Br=e=>{let t=[];for(let r of e){let n=r[2];!Array.isArray(n)&&"buffer"in n&&t.push(n.buffer)}return t}});var Pt,Ye,ir,dn,ln,un,To,Ao,Ht,Gt,$h,Al,El,kl,Pl,Ol,Dl,zl,Eo=U(()=>{"use strict";Ke();Hn();St();jt();Pt=()=>!!be.wasm.proxy&&typeof document<"u",ir=!1,dn=!1,ln=!1,Ao=new Map,Ht=(e,t)=>{let r=Ao.get(e);r?r.push(t):Ao.set(e,[t])},Gt=()=>{if(ir||!dn||ln||!Ye)throw new Error("worker not ready")},$h=e=>{switch(e.data.type){case"init-wasm":ir=!1,e.data.err?(ln=!0,To[1](e.data.err)):(dn=!0,To[0]()),un&&(URL.revokeObjectURL(un),un=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=Ao.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},Al=async()=>{if(!dn){if(ir)throw new Error("multiple calls to 'initWasm()' detected.");if(ln)throw new Error("previous call to 'initWasm()' failed.");if(ir=!0,Pt())return new Promise((e,t)=>{Ye?.terminate(),Oa().then(([r,n])=>{try{Ye=n,Ye.onerror=i=>t(i),Ye.onmessage=$h,To=[e,t];let o={type:"init-wasm",in:be};Ye.postMessage(o),un=r}catch(o){t(o)}},t)});try{await Ar(be.wasm),await Er(be),dn=!0}catch(e){throw ln=!0,e}finally{ir=!1}}},El=async e=>{if(Pt())return Gt(),new Promise((t,r)=>{Ht("init-ep",[t,r]);let n={type:"init-ep",in:{epName:e,env:be}};Ye.postMessage(n)});await kr(be,e)},kl=async e=>Pt()?(Gt(),new Promise((t,r)=>{Ht("copy-from",[t,r]);let n={type:"copy-from",in:{buffer:e}};Ye.postMessage(n,[e.buffer])})):Kt(e),Pl=async(e,t)=>{if(Pt()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Gt(),new Promise((r,n)=>{Ht("create",[r,n]);let o={type:"create",in:{model:e,options:{...t}}},i=[];e instanceof Uint8Array&&i.push(e.buffer),Ye.postMessage(o,i)})}else return Pr(e,t)},Ol=async e=>{if(Pt())return Gt(),new Promise((t,r)=>{Ht("release",[t,r]);let n={type:"release",in:e};Ye.postMessage(n)});Or(e)},Dl=async(e,t,r,n,o,i)=>{if(Pt()){if(r.some(a=>a[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(o.some(a=>a))throw new Error("pre-allocated output tensor is not supported for proxy.");return Gt(),new Promise((a,l)=>{Ht("run",[a,l]);let d=r,p={type:"run",in:{sessionId:e,inputIndices:t,inputs:d,outputIndices:n,options:i}};Ye.postMessage(p,Br(d))})}else return Dr(e,t,r,n,o,i)},zl=async e=>{if(Pt())return Gt(),new Promise((t,r)=>{Ht("end-profiling",[t,r]);let n={type:"end-profiling",in:e};Ye.postMessage(n)});zr(e)}});var Bl,_h,cn,Rl=U(()=>{"use strict";Ke();Eo();Q();Tr();Kn();Bl=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},_h=e=>{switch(e[3]){case"cpu":return new De(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Vr(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:n,dispose:o}=e[2];return De.fromGpuBuffer(r,{dataType:t,dims:e[1],download:n,dispose:o})}default:throw new Error(`invalid data location: ${e[3]}`)}},cn=class{async fetchModelAndCopyToWasmMemory(t){return kl(await Zt(t))}async loadModel(t,r){We();let n;typeof t=="string"? false?0:n=await this.fetchModelAndCopyToWasmMemory(t):n=t,[this.sessionId,this.inputNames,this.outputNames]=await Pl(n,r),Me()}async dispose(){return Ol(this.sessionId)}async run(t,r,n){We();let o=[],i=[];Object.entries(t).forEach(h=>{let w=h[0],g=h[1],b=this.inputNames.indexOf(w);if(b===-1)throw new Error(`invalid input '${w}'`);o.push(g),i.push(b)});let a=[],l=[];Object.entries(r).forEach(h=>{let w=h[0],g=h[1],b=this.outputNames.indexOf(w);if(b===-1)throw new Error(`invalid output '${w}'`);a.push(g),l.push(b)});let d=o.map((h,w)=>Bl(h,()=>`input "${this.inputNames[i[w]]}"`)),p=a.map((h,w)=>h?Bl(h,()=>`output "${this.outputNames[l[w]]}"`):null),m=await Dl(this.sessionId,i,d,l,p,n),u={};for(let h=0;h<m.length;h++)u[this.outputNames[l[h]]]=a[h]??_h(m[h]);return Me(),u}startProfiling(){}endProfiling(){zl(this.sessionId)}}});var xh,pn,Ml=U(()=>{"use strict";Ke();Eo();Rl();jt();xh=()=>{if((typeof be.wasm.initTimeout!="number"||be.wasm.initTimeout<0)&&(be.wasm.initTimeout=0),be.wasm.simd===!1&&console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'),typeof be.wasm.proxy!="boolean"&&(be.wasm.proxy=!1),typeof be.wasm.trace!="boolean"&&(be.wasm.trace=!1),typeof be.wasm.numThreads!="number"||!Number.isInteger(be.wasm.numThreads)||be.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)be.wasm.numThreads=1;else{let e=typeof navigator>"u"?Mn("node:os").cpus().length:navigator.hardwareConcurrency;be.wasm.numThreads=Math.min(4,Math.ceil((e||1)/2))}},pn=class{async init(t){xh(),await Al(),await El(t)}async createInferenceSessionHandler(t,r){let n=new cn;return await n.loadModel(t,r),Promise.resolve(n)}}});var Ul={};Ft(Ul,{wasmBackend:()=>Sh});var Sh,Vl=U(()=>{"use strict";Ml();Sh=new pn});Ke();Ke();Ke();var xa="1.19.0";var K_=Wn;{let e=(Vl(),br(Ul)).wasmBackend;_t("webgpu",e,5),_t("webnn",e,5),_t("cpu",e,10),_t("wasm",e,10)}Object.defineProperty(be.versions,"web",{value:xa,enumerable:!0});
6370
+ ${a}`,d=n.createShaderModule({code:l,label:t.name});be("verbose",()=>`[WebGPU] ${t.name} shader code: ${l}`);let p=n.createComputePipeline({compute:{module:d,entryPoint:"main"},layout:"auto",label:t.name});return De(t.name),{programInfo:t,computePipeline:p,uniformVariablesInfo:i.variablesInfo}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,n=typeof t=="number"?1:t.y||1,o=typeof t=="number"?1:t.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=i&&n<=i&&o<=i)return[r,n,o];let a=r*n*o,l=Math.ceil(Math.sqrt(a));if(l>i){if(l=Math.ceil(Math.cbrt(a)),l>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[l,l,l]}else return[l,l,1]}}});var ph,mh,$o,nn,vl=M(()=>{"use strict";qe();X();st();Ra();Wa();bl();wl();ph=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let n=0;n<e.length;++n){let o=e[n].dataType;switch(t[n]){case"none":{r.push("");break}case"type":{r.push(`${o}`);break}case"rank":{let i=e[n].dims.length;r.push(`${o};${i}`);break}case"dims":{let i=e[n].dims.join(",");r.push(`${o};${i}`);break}default:throw new Error(`unsupported input dependency: ${t[n]}`)}}return r.join("|")},mh=(e,t,r)=>{let n=e.name;return e.shaderCache?.hint&&(n+="["+e.shaderCache.hint+"]"),n+=":"+r+`:${ph(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,n},$o=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},nn=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n};r.features.has("chromium-experimental-timestamp-query-inside-passes")?n.push("chromium-experimental-timestamp-query-inside-passes"):r.features.has("timestamp-query")&&n.push("timestamp-query"),r.features.has("shader-f16")&&n.push("shader-f16"),this.device=await r.requestDevice(o),this.adapterInfo=new $o(r.info||await r.requestAdapterInfo()),this.gpuDataManager=Na(this),this.programManager=new rn(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Ba(t.logLevel,!!t.debug),this.device.onuncapturederror=i=>{i.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${i.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Ve(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),n=this.pendingQueries.get(t);for(let o=0;o<r.length/2;o++){let i=n[o],a=i.kernelId,l=this.kernels.get(a),d=l.kernelType,p=l.kernelName,m=i.programName,u=i.inputTensorViews,h=i.outputTensorViews,w=r[o*2],g=r[o*2+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=w);let y=Number(w-this.queryTimeBase),x=Number(g-this.queryTimeBase);if(!Number.isSafeInteger(y)||!Number.isSafeInteger(x))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:u.map(_=>({dims:_.dims,dataType:mt(_.dataType)})),outputsMetadata:h.map(_=>({dims:_.dims,dataType:mt(_.dataType)})),kernelId:a,kernelType:d,kernelName:p,programName:m,startTime:y,endTime:x});else{let _="";u.forEach((S,I)=>{_+=`input[${I}]: [${S.dims}] | ${mt(S.dataType)}, `});let $="";h.forEach((S,I)=>{$+=`output[${I}]: [${S.dims}] | ${mt(S.dataType)}, `}),console.log(`[profiling] kernel "${a}|${d}|${p}|${m}" ${_}${$}execution time: ${x-y} ns`)}$r("GPU",`${m}::${w}::${g}`)}t.unmap(),this.pendingQueries.delete(t)}),De()}run(t,r,n,o,i,a){Ve(t.name);let l=[];for(let S=0;S<r.length;++S){let I=r[S].data;if(I===0)continue;let T=this.gpuDataManager.get(I);if(!T)throw new Error(`no GPU data for input: ${I}`);l.push(T)}let{outputs:d,dispatchGroup:p,programUniforms:m}=t.getRunData(r),u=n.length===0?d.map((S,I)=>I):n;if(u.length!==d.length)throw new Error(`Output size ${u.length} must be equal to ${d.length}.`);let h=[],w=[];for(let S=0;S<d.length;++S){if(!Number.isInteger(u[S])||u[S]<-3||u[S]>=a)throw new Error(`Invalid output index: ${u[S]}`);if(u[S]===-3)continue;let I=u[S]===-1,T=u[S]===-2,A=I||T?i(d[S].dataType,d[S].dims):o(u[S],d[S].dataType,d[S].dims);if(h.push(A),A.data===0)continue;let O=this.gpuDataManager.get(A.data);if(!O)throw new Error(`no GPU data for output: ${A.data}`);if(I&&this.temporaryData.push(O),T){let z=this.kernelPersistentData.get(this.currentKernelId);z||(z=[],this.kernelPersistentData.set(this.currentKernelId,z)),z.push(O)}w.push(O)}if(l.length!==r.length||w.length!==h.length){if(w.length===0)return De(t.name),h;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let g;if(m){let S=0,I=[];m.forEach(z=>{let W=typeof z.data=="number"?[z.data]:z.data;if(W.length===0)return;let V=z.type===10?2:4,j,Y;z.type===10?(Y=W.length>4?16:W.length>2?8:W.length*V,j=W.length>4?16:V*W.length):(Y=W.length<=2?W.length*V:16,j=16),S=Math.ceil(S/Y)*Y,I.push(S);let ae=z.type===10?8:4;S+=W.length>4?Math.ceil(W.length/ae)*j:W.length*V});let T=16;S=Math.ceil(S/T)*T;let A=new ArrayBuffer(S);m.forEach((z,W)=>{let V=I[W],j=typeof z.data=="number"?[z.data]:z.data;if(z.type===6)new Int32Array(A,V,j.length).set(j);else if(z.type===12)new Uint32Array(A,V,j.length).set(j);else if(z.type===10)new Uint16Array(A,V,j.length).set(j);else if(z.type===1)new Float32Array(A,V,j.length).set(j);else throw new Error(`Unsupported uniform type: ${mt(z.type)}`)});let O=this.gpuDataManager.create(S,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(O.buffer,0,A,0,S),this.gpuDataManager.release(O.id),g={offset:0,size:S,buffer:O.buffer}}let y=this.programManager.normalizeDispatchGroupSize(p),x=y[1]===1&&y[2]===1,_=mh(t,r,x),$=this.programManager.getArtifact(_);if($||($=this.programManager.build(t,y),this.programManager.setArtifact(_,$),be("info",()=>`[artifact] key: ${_}, programName: ${t.name}`)),m&&$.uniformVariablesInfo){if(m.length!==$.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${$.uniformVariablesInfo.length}, got ${m.length} in program "${$.programInfo.name}".`);for(let S=0;S<m.length;S++){let I=m[S],T=I.type,A=typeof I.data=="number"?1:I.data.length,[O,z]=$.uniformVariablesInfo[S];if(T!==O||A!==z)throw new Error(`Uniform variable ${S} mismatch: expect type ${O} with size ${z}, got type ${T} with size ${A} in program "${$.programInfo.name}".`)}}if(be("info",()=>`[ProgramManager] run "${t.name}" (key=${_}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let S={kernelId:this.currentKernelId,programName:$.programInfo.name,inputTensorViews:r,outputTensorViews:h};this.pendingKernels.push(S),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(S)}return this.programManager.run($,l,w,y,g),De(t.name),h}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,n,o){let i=yl.get(t);if(!i)throw new Error(`kernel not implemented: ${t}`);let a={kernelType:t,kernelName:o,kernelEntry:i[0],attributes:[i[1],n]};this.kernels.set(r,a)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let n of r)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,n){let o=this.kernels.get(t);if(!o)throw new Error(`kernel not created: ${t}`);let i=o.kernelType,a=o.kernelName,l=o.kernelEntry,d=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`);this.currentKernelId=t,d[0]&&(d[1]=d[0](d[1]),d[0]=void 0),be("info",()=>`[WebGPU] Start to run kernel "[${i}] ${a}"...`);let p=this.env.debug;this.temporaryData=[];try{return p&&this.device.pushErrorScope("validation"),l(r,d[1]),0}catch(m){return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${m}`)),1}finally{p&&n.push(this.device.popErrorScope().then(m=>m?`GPU validation error for kernel "[${i}] ${a}": ${m.message}`:null));for(let m of this.temporaryData)this.gpuDataManager.release(m.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,n,o){let i=this.sessionExternalDataMapping.get(t);i||(i=new Map,this.sessionExternalDataMapping.set(t,i));let a=i.get(r),l=this.gpuDataManager.registerExternalBuffer(n,o,a?.[1]);return i.set(r,[l,n]),l}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[1])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,n){return async()=>{let o=await jn(this,t,r);return Da(o.buffer,n)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){be("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){be("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){be("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),n=t.length;this.pendingKernels=[];for(let o=0;o<n;o++){let i=this.getComputePassEncoder(),a=t[o];this.writeTimestamp(this.pendingDispatchNumber*2),i.setPipeline(a.computePipeline),i.setBindGroup(0,a.bindGroup),i.dispatchWorkgroups(...a.dispatchGroup),this.writeTimestamp(this.pendingDispatchNumber*2+1),this.pendingDispatchNumber++,this.queryType!=="none"&&this.pendingKernels.push(r[o]),(this.pendingDispatchNumber>=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var $l={};Lt($l,{init:()=>fh});var tr,_o,fh,_l=M(()=>{"use strict";X();vl();st();oe();tr=class e{constructor(t,r,n,o){this.module=t;this.dataType=r;this.data=n;this.dims=o}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=E.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=E.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=E.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(E.size(t)!==E.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},_o=class{constructor(t,r,n){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let o=t.HEAPU32,i=n>>>2;this.opKernelContext=o[i++];let a=o[i++];this.outputCount=o[i++],this.customDataOffset=o[i++],this.customDataSize=o[i++];let l=[];for(let d=0;d<a;d++){let p=o[i++],m=o[i++],u=o[i++],h=[];for(let w=0;w<u;w++)h.push(o[i++]);l.push(new tr(t,p,m,h))}this.inputs=l}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}getMaxComputeWorkgroupSizes(){return[this.backend.device.limits.maxComputeWorkgroupSizeX,this.backend.device.limits.maxComputeWorkgroupSizeY,this.backend.device.limits.maxComputeWorkgroupSizeZ]}getMaxComputeWorkgroupStoragesize(){return this.backend.device.limits.maxComputeWorkgroupStorageSize}compute(t,r){let n=r?.inputs?.map(l=>typeof l=="number"?this.inputs[l]:l)??this.inputs,o=r?.outputs??[],i=(l,d,p)=>new tr(this.module,d,this.output(l,p),p),a=(l,d)=>{let p=_t(l,d);if(!p)throw new Error(`Unsupported data type: ${l}`);let m=p>0?this.backend.gpuDataManager.create(p).id:0;return new tr(this.module,l,m,d)};return this.backend.run(t,n,o,i,a,this.outputCount)}output(t,r){let n=this.module.stackSave();try{let o=this.module.stackAlloc((1+r.length)*4),i=o>>2;this.module.HEAPU32[i++]=r.length;for(let a=0;a<r.length;a++)this.module.HEAPU32[i++]=r[a];return this.module._JsepOutput(this.opKernelContext,t,o)}catch(o){throw new Error(`Failed to generate kernel's output[${t}] with dims [${r}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${o}`)}finally{this.module.stackRestore(n)}}},fh=async(e,t,r,n)=>{let o=t.jsepInit;if(!o)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let i=new nn;await i.initialize(r,n),o("webgpu",[i,a=>i.alloc(a),a=>i.free(a),(a,l,d,p=!1)=>{if(p)be("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${a}, dst=${l}, size=${d}`),i.memcpy(a,l);else{be("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${a}, gpuDataId=${l}, size=${d}`);let m=t.HEAPU8.subarray(a>>>0,(a>>>0)+d);i.upload(l,m)}},async(a,l,d)=>{be("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${a}, dataOffset=${l}, size=${d}`),await i.download(a,()=>t.HEAPU8.subarray(l>>>0,(l>>>0)+d))},(a,l,d)=>i.createKernel(a,l,d,t.UTF8ToString(t._JsepGetNodeName(l))),a=>i.releaseKernel(a),(a,l,d,p)=>{be("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${d}, kernel=${a}, contextDataOffset=${l}`);let m=new _o(t,i,l);return i.computeKernel(a,m,p)},()=>i.captureBegin(),()=>i.captureEnd(),()=>i.replay()])}else o("webnn")}});var hh,Cr,Tr,Tt,gh,Ft,Ar,kr,xl,Er,Pr,Or,Un=M(()=>{"use strict";ka();Pa();X();$t();Br();Gn();hh=(e,t)=>{_e()._OrtInit(e,t)!==0&&we("Can't initialize onnxruntime.")},Cr=async e=>{hh(e.wasm.numThreads,Kt(e.logLevel))},Tr=async(e,t)=>{{let r=(_l(),hr($l)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let n=e.webgpu.adapter;if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let i=e.webgpu.forceFallbackAdapter;if(i!==void 0&&typeof i!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:i}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await r("webgpu",_e(),e,n)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",_e(),e)}}},Tt=new Map,gh=e=>{let t=_e(),r=t.stackSave();try{let n=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,n,n+4)!==0&&we("Can't get session input/output count."),[t.HEAP32[n/4],t.HEAP32[n/4+1]]}finally{t.stackRestore(r)}},Ft=e=>{let t=_e(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},Ar=async(e,t)=>{let r,n,o=_e();Array.isArray(e)?[r,n]=e:e.buffer===o.HEAPU8.buffer?[r,n]=[e.byteOffset,e.byteLength]:[r,n]=Ft(e);let i=0,a=0,l=0,d=[],p=[],m=[];try{if([a,d]=Ea(t),t?.externalData&&o.mountExternalData){let $=[];for(let S of t.externalData){let I=typeof S=="string"?S:S.path;$.push(jt(typeof S=="string"?S:S.data).then(T=>{o.mountExternalData(I,T)}))}await Promise.all($)}for(let $ of t?.executionProviders??[])if((typeof $=="string"?$:$.name)==="webnn"){if(o.currentContext)throw new Error("WebNN execution provider is already set.");if(typeof $!="string"){let I=$,T=I?.context,A=I?.gpuDevice,O=I?.deviceType,z=I?.numThreads,W=I?.powerPreference;T?o.currentContext=T:A?o.currentContext=await navigator.ml.createContext(A):o.currentContext=await navigator.ml.createContext({deviceType:O,numThreads:z,powerPreference:W})}else o.currentContext=await navigator.ml.createContext();break}i=await o._OrtCreateSession(r,n,a),i===0&&we("Can't create a session."),o.currentContext&&(o.currentContext=void 0);let[u,h]=gh(i),w=!!t?.enableGraphCapture,g=[],y=[],x=[];for(let $=0;$<u;$++){let S=o._OrtGetInputName(i,$);S===0&&we("Can't get an input name."),p.push(S),g.push(o.UTF8ToString(S))}for(let $=0;$<h;$++){let S=o._OrtGetOutputName(i,$);S===0&&we("Can't get an output name."),m.push(S);let I=o.UTF8ToString(S);y.push(I);{if(w&&t?.preferredOutputLocation===void 0){x.push("gpu-buffer");continue}let T=typeof t?.preferredOutputLocation=="string"?t.preferredOutputLocation:t?.preferredOutputLocation?.[I]??"cpu";if(T!=="cpu"&&T!=="cpu-pinned"&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}.`);if(w&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);x.push(T)}}let _=null;return x.some($=>$==="gpu-buffer")&&(l=o._OrtCreateBinding(i),l===0&&we("Can't create IO binding."),_={handle:l,outputPreferredLocations:x,outputPreferredLocationsEncoded:x.map($=>Hn($))}),Tt.set(i,[i,p,m,_,w,!1]),[i,g,y]}catch(u){throw p.forEach(h=>o._OrtFree(h)),m.forEach(h=>o._OrtFree(h)),l!==0&&o._OrtReleaseBinding(l),i!==0&&o._OrtReleaseSession(i),u}finally{o._free(r),a!==0&&o._OrtReleaseSessionOptions(a),d.forEach(u=>o._free(u)),o.unmountExternalData?.()}},kr=e=>{let t=_e(),r=Tt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[n,o,i,a,l]=r;a&&(l&&t._OrtClearBoundOutputs(a.handle),t._OrtReleaseBinding(a.handle)),t.jsepOnReleaseSession?.(e),o.forEach(d=>t._OrtFree(d)),i.forEach(d=>t._OrtFree(d)),t._OrtReleaseSession(n),Tt.delete(e)},xl=(e,t,r,n,o,i=!1)=>{if(!e){t.push(0);return}let a=_e(),l=e[0],d=e[1],p=e[3],m,u;if(l==="string"&&p==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");if(i&&p!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`);if(p==="gpu-buffer"){let g=e[2].gpuBuffer;u=_t(Ln(l),d);let y=a.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');m=y(n,o,g,u)}else{let g=e[2];if(Array.isArray(g)){u=4*g.length,m=a._malloc(u),r.push(m);let y=m/4;for(let x=0;x<g.length;x++){if(typeof g[x]!="string")throw new TypeError(`tensor data at index ${x} is not a string`);a.HEAPU32[y++]=Ie(g[x],r)}}else u=g.byteLength,m=a._malloc(u),r.push(m),a.HEAPU8.set(new Uint8Array(g.buffer,g.byteOffset,u),m)}let h=a.stackSave(),w=a.stackAlloc(4*d.length);try{let g=w/4;d.forEach(x=>a.HEAP32[g++]=x);let y=a._OrtCreateTensor(Ln(l),m,u,w,d.length,Hn(p));y===0&&we(`Can't create tensor for input/output. session=${n}, index=${o}.`),t.push(y)}finally{a.stackRestore(h)}},Er=async(e,t,r,n,o,i)=>{let a=_e(),l=Tt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let d=l[0],p=l[1],m=l[2],u=l[3],h=l[4],w=l[5],g=t.length,y=n.length,x=0,_=[],$=[],S=[],I=[],T=a.stackSave(),A=a.stackAlloc(g*4),O=a.stackAlloc(g*4),z=a.stackAlloc(y*4),W=a.stackAlloc(y*4);try{[x,_]=Aa(i);for(let q=0;q<g;q++)xl(r[q],$,I,e,t[q],h);for(let q=0;q<y;q++)xl(o[q],S,I,e,g+n[q],h);let V=A/4,j=O/4,Y=z/4,ae=W/4;for(let q=0;q<g;q++)a.HEAPU32[V++]=$[q],a.HEAPU32[j++]=p[t[q]];for(let q=0;q<y;q++)a.HEAPU32[Y++]=S[q],a.HEAPU32[ae++]=m[n[q]];if(u&&!w){let{handle:q,outputPreferredLocations:ce,outputPreferredLocationsEncoded:ne}=u;if(p.length!==g)throw new Error(`input count from feeds (${g}) is expected to be always equal to model's input count (${p.length}).`);for(let Z=0;Z<g;Z++){let ee=t[Z];await a._OrtBindInput(q,p[ee],$[Z])!==0&&we(`Can't bind input[${Z}] for session=${e}.`)}for(let Z=0;Z<y;Z++){let ee=n[Z];o[Z]?.[3]?a._OrtBindOutput(q,m[ee],S[Z],0)!==0&&we(`Can't bind pre-allocated output[${Z}] for session=${e}.`):a._OrtBindOutput(q,m[ee],0,ne[ee])!==0&&we(`Can't bind output[${Z}] to ${ce[Z]} for session=${e}.`)}Tt.set(e,[d,p,m,u,h,!0])}a.jsepOnRunStart?.(d);let se;u?se=await a._OrtRunWithBinding(d,u.handle,y,z,x):se=await a._OrtRun(d,O,A,g,W,y,z,x),se!==0&&we("failed to call OrtRun().");let ie=[];for(let q=0;q<y;q++){let ce=a.HEAPU32[z/4+q];if(ce===S[q]){ie.push(o[q]);continue}let ne=a.stackSave(),Z=a.stackAlloc(4*4),ee=!1,K,me=0;try{a._OrtGetTensorData(ce,Z,Z+4,Z+8,Z+12)!==0&&we(`Can't access output tensor data on index ${q}.`);let R=Z/4,G=a.HEAPU32[R++];me=a.HEAPU32[R++];let ue=a.HEAPU32[R++],Be=a.HEAPU32[R++],ge=[];for(let Te=0;Te<Be;Te++)ge.push(a.HEAPU32[ue/4+Te]);a._OrtFree(ue);let Se=ge.reduce((Te,Oe)=>Te*Oe,1);K=mt(G);let Wt=u?.outputPreferredLocations[n[q]];if(K==="string"){if(Wt==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");let Te=[],Oe=me/4;for(let He=0;He<Se;He++){let kt=a.HEAPU32[Oe++],ht=He===Se-1?void 0:a.HEAPU32[Oe]-kt;Te.push(a.UTF8ToString(kt,ht))}ie.push([K,ge,Te,"cpu"])}else if(Wt==="gpu-buffer"&&Se>0){let Te=a.jsepGetBuffer;if(!Te)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let Oe=Te(me),He=_t(G,Se);if(He===void 0||!Rr(K))throw new Error(`Unsupported data type: ${K}`);ee=!0,ie.push([K,ge,{gpuBuffer:Oe,download:a.jsepCreateDownloader(Oe,He,K),dispose:()=>{a._OrtReleaseTensor(ce)}},"gpu-buffer"])}else{let Te=Dr(K),Oe=new Te(Se);new Uint8Array(Oe.buffer,Oe.byteOffset,Oe.byteLength).set(a.HEAPU8.subarray(me,me+Oe.byteLength)),ie.push([K,ge,Oe,"cpu"])}}finally{a.stackRestore(ne),K==="string"&&me&&a._free(me),ee||a._OrtReleaseTensor(ce)}}return u&&!h&&(a._OrtClearBoundOutputs(u.handle),Tt.set(e,[d,p,m,u,h,!1])),ie}finally{a.stackRestore(T),$.forEach(V=>a._OrtReleaseTensor(V)),S.forEach(V=>a._OrtReleaseTensor(V)),I.forEach(V=>a._free(V)),x!==0&&a._OrtReleaseRunOptions(x),_.forEach(V=>a._free(V))}},Pr=e=>{let t=_e(),r=Tt.get(e);if(!r)throw new Error("invalid session id");let n=r[0],o=t._OrtEndProfiling(n);o===0&&we("Can't get an profile file name."),t._OrtFree(o)},Or=e=>{let t=[];for(let r of e){let n=r[2];!Array.isArray(n)&&"buffer"in n&&t.push(n.buffer)}return t}});var At,Ke,rr,an,sn,on,xo,So,Vt,Nt,bh,Sl,Il,Cl,Tl,Al,kl,El,Io=M(()=>{"use strict";qe();Un();$t();Gt();At=()=>!!fe.wasm.proxy&&typeof document<"u",rr=!1,an=!1,sn=!1,So=new Map,Vt=(e,t)=>{let r=So.get(e);r?r.push(t):So.set(e,[t])},Nt=()=>{if(rr||!an||sn||!Ke)throw new Error("worker not ready")},bh=e=>{switch(e.data.type){case"init-wasm":rr=!1,e.data.err?(sn=!0,xo[1](e.data.err)):(an=!0,xo[0]()),on&&(URL.revokeObjectURL(on),on=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=So.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},Sl=async()=>{if(!an){if(rr)throw new Error("multiple calls to 'initWasm()' detected.");if(sn)throw new Error("previous call to 'initWasm()' failed.");if(rr=!0,At())return new Promise((e,t)=>{Ke?.terminate(),Ia().then(([r,n])=>{try{Ke=n,Ke.onerror=i=>t(i),Ke.onmessage=bh,xo=[e,t];let o={type:"init-wasm",in:fe};Ke.postMessage(o),on=r}catch(o){t(o)}},t)});try{await Ir(fe.wasm),await Cr(fe),an=!0}catch(e){throw sn=!0,e}finally{rr=!1}}},Il=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("init-ep",[t,r]);let n={type:"init-ep",in:{epName:e,env:fe}};Ke.postMessage(n)});await Tr(fe,e)},Cl=async e=>At()?(Nt(),new Promise((t,r)=>{Vt("copy-from",[t,r]);let n={type:"copy-from",in:{buffer:e}};Ke.postMessage(n,[e.buffer])})):Ft(e),Tl=async(e,t)=>{if(At()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Nt(),new Promise((r,n)=>{Vt("create",[r,n]);let o={type:"create",in:{model:e,options:{...t}}},i=[];e instanceof Uint8Array&&i.push(e.buffer),Ke.postMessage(o,i)})}else return Ar(e,t)},Al=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("release",[t,r]);let n={type:"release",in:e};Ke.postMessage(n)});kr(e)},kl=async(e,t,r,n,o,i)=>{if(At()){if(r.some(a=>a[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(o.some(a=>a))throw new Error("pre-allocated output tensor is not supported for proxy.");return Nt(),new Promise((a,l)=>{Vt("run",[a,l]);let d=r,p={type:"run",in:{sessionId:e,inputIndices:t,inputs:d,outputIndices:n,options:i}};Ke.postMessage(p,Or(d))})}else return Er(e,t,r,n,o,i)},El=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("end-profiling",[t,r]);let n={type:"end-profiling",in:e};Ke.postMessage(n)});Pr(e)}});var Pl,wh,un,Ol=M(()=>{"use strict";qe();Io();X();Sr();Gn();Pl=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},wh=e=>{switch(e[3]){case"cpu":return new Pe(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Rr(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:n,dispose:o}=e[2];return Pe.fromGpuBuffer(r,{dataType:t,dims:e[1],download:n,dispose:o})}default:throw new Error(`invalid data location: ${e[3]}`)}},un=class{async fetchModelAndCopyToWasmMemory(t){return Cl(await jt(t))}async loadModel(t,r){Ve();let n;typeof t=="string"? false?0:n=await this.fetchModelAndCopyToWasmMemory(t):n=t,[this.sessionId,this.inputNames,this.outputNames]=await Tl(n,r),De()}async dispose(){return Al(this.sessionId)}async run(t,r,n){Ve();let o=[],i=[];Object.entries(t).forEach(h=>{let w=h[0],g=h[1],y=this.inputNames.indexOf(w);if(y===-1)throw new Error(`invalid input '${w}'`);o.push(g),i.push(y)});let a=[],l=[];Object.entries(r).forEach(h=>{let w=h[0],g=h[1],y=this.outputNames.indexOf(w);if(y===-1)throw new Error(`invalid output '${w}'`);a.push(g),l.push(y)});let d=o.map((h,w)=>Pl(h,()=>`input "${this.inputNames[i[w]]}"`)),p=a.map((h,w)=>h?Pl(h,()=>`output "${this.outputNames[l[w]]}"`):null),m=await kl(this.sessionId,i,d,l,p,n),u={};for(let h=0;h<m.length;h++)u[this.outputNames[l[h]]]=a[h]??wh(m[h]);return De(),u}startProfiling(){}endProfiling(){El(this.sessionId)}}});var vh,dn,zl=M(()=>{"use strict";qe();Io();Ol();Gt();vh=()=>{if((typeof fe.wasm.initTimeout!="number"||fe.wasm.initTimeout<0)&&(fe.wasm.initTimeout=0),fe.wasm.simd===!1&&console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'),typeof fe.wasm.proxy!="boolean"&&(fe.wasm.proxy=!1),typeof fe.wasm.trace!="boolean"&&(fe.wasm.trace=!1),typeof fe.wasm.numThreads!="number"||!Number.isInteger(fe.wasm.numThreads)||fe.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)fe.wasm.numThreads=1;else{let e=typeof navigator>"u"?zn("node:os").cpus().length:navigator.hardwareConcurrency;fe.wasm.numThreads=Math.min(4,Math.ceil((e||1)/2))}},dn=class{async init(t){vh(),await Sl(),await Il(t)}async createInferenceSessionHandler(t,r){let n=new un;return await n.loadModel(t,r),Promise.resolve(n)}}});var Bl={};Lt(Bl,{wasmBackend:()=>$h});var $h,Dl=M(()=>{"use strict";zl();$h=new dn});qe();qe();qe();var ga="1.20.0-dev.20240818-417aa00406";var ex=Mn;{let e=(Dl(),hr(Bl)).wasmBackend;wt("webgpu",e,5),wt("webnn",e,5),wt("cpu",e,10),wt("wasm",e,10)}Object.defineProperty(fe.versions,"web",{value:ga,enumerable:!0});
6334
6371
  /**
6335
6372
  * @license
6336
6373
  * Copyright 2021 Google LLC. All Rights Reserved.
@@ -7036,7 +7073,7 @@ __webpack_require__.r(__webpack_exports__);
7036
7073
 
7037
7074
 
7038
7075
 
7039
- const VERSION = '3.0.0-alpha.7';
7076
+ const VERSION = '3.0.0-alpha.8';
7040
7077
 
7041
7078
  // Check if various APIs are available (depends on environment)
7042
7079
  const IS_BROWSER_ENV = typeof self !== 'undefined';