PageSourceSearch

https://hacca.ai/app/_app/immutable/chunks/Cyo71q8q.js

js hacca.ai collected 2026-10-05 13:34:13 UTC 414,151 bytes, 2,876 lines download raw bytes

1var kg=Object.defineProperty;var Tg=(e,t,r)=>t in e?kg(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var Js=(e,t,r)=>Tg(e,typeof t!="symbol"?t+"":t,r);/*!
2 * ONNX Runtime Web v1.26.0
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License.
5 */var Aa=Object.defineProperty,Ig=Object.getOwnPropertyDescriptor,Eg=Object.getOwnPropertyNames,zg=Object.prototype.hasOwnProperty,Cg=(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')}),U=(e,t)=>()=>(e&&(t=e(e=0)),t),Ht=(e,t)=>{for(var r in t)Aa(e,r,{get:t[r],enumerable:!0})},Ag=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of Eg(t))!zg.call(e,a)&&a!==r&&Aa(e,a,{get:()=>t[a],enumerable:!(i=Ig(t,a))||i.enumerable});return e},cr=e=>Ag(Aa({},"__esModule",{value:!0}),e),Xt,ft,qt,eo,Bd,Nd=U(()=>{Xt=new Map,ft=[],qt=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let i=Xt.get(e);if(i===void 0)Xt.set(e,{backend:t,priority:r});else{if(i.priority>r)return;if(i.priority===r&&i.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let a=ft.indexOf(e);a!==-1&&ft.splice(a,1);for(let n=0;n<ft.length;n++)if(Xt.get(ft[n]).priority<=r){ft.splice(n,0,e);return}ft.push(e)}return}throw new TypeError("not a valid backend")},eo=async e=>{let t=Xt.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(i){return r||(t.error=`${i}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},Bd=async e=>{let t=e.executionProviders||[],r=t.map(l=>typeof l=="string"?l:l.name),i=r.length===0?ft:r,a,n=[],s=new Set;for(let l of i){let p=await eo(l);typeof p=="string"?n.push({name:l,err:p}):(a||(a=p),a===p&&s.add(l))}if(!a)throw new Error(`no available backend found. ERR: ${n.map(l=>`[${l.name}] ${l.err}`).join(", ")}`);for(let{name:l,err:p}of n)r.includes(l)&&console.warn(`removing requested execution provider "${l}" from session options because it is not available: ${p}`);let u=t.filter(l=>s.has(typeof l=="string"?l:l.name));return[a,new Proxy(e,{get:(l,p)=>p==="executionProviders"?u:Reflect.get(l,p)})]}}),Og=U(()=>{Nd()}),Md,Rg=U(()=>{Md="1.26.0"}),bi,Ie,Dd=U(()=>{Rg(),bi="warning",Ie={wasm:{},webgl:{},webgpu:{},versions:{common:Md},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}`);bi=e}},get logLevel(){return bi}},Object.defineProperty(Ie,"logLevel",{enumerable:!0})}),$e,Bg=U(()=>{Dd(),$e=Ie}),Ud,Pd,Ng=U(()=>{Ud=(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 i=r.getContext("2d");if(i!=null){let a,n;(t==null?void 0:t.tensorLayout)!==void 0&&t.tensorLayout==="NHWC"?(a=e.dims[2],n=e.dims[3]):(a=e.dims[3],n=e.dims[2]);let s=(t==null?void 0:t.format)!==void 0?t.format:"RGB",u=t==null?void 0:t.norm,l,p;u===void 0||u.mean===void 0?l=[255,255,255,255]:typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:(l=[u.mean[0],u.mean[1],u.mean[2],0],u.mean[3]!==void 0&&(l[3]=u.mean[3])),u===void 0||u.bias===void 0?p=[0,0,0,0]:typeof u.bias=="number"?p=[u.bias,u.bias,u.bias,u.bias]:(p=[u.bias[0],u.bias[1],u.bias[2],0],u.bias[3]!==void 0&&(p[3]=u.bias[3]));let f=n*a,h=0,g=f,_=f*2,y=-1;s==="RGBA"?(h=0,g=f,_=f*2,y=f*3):s==="RGB"?(h=0,g=f,_=f*2):s==="RBG"&&(h=0,_=f,g=f*2);for(let b=0;b<n;b++)for(let S=0;S<a;S++){let x=(e.data[h++]-p[0])*l[0],w=(e.data[g++]-p[1])*l[1],I=(e.data[_++]-p[2])*l[2],k=y===-1?255:(e.data[y++]-p[3])*l[3];i.fillStyle="rgba("+x+","+w+","+I+","+k+")",i.fillRect(S,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")},Pd=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),i;if(r!=null){let a,n,s;(t==null?void 0:t.tensorLayout)!==void 0&&t.tensorLayout==="NHWC"?(a=e.dims[2],n=e.dims[1],s=e.dims[3]):(a=e.dims[3],n=e.dims[2],s=e.dims[1]);let u=t!==void 0&&t.format!==void 0?t.format:"RGB",l=t==null?void 0:t.norm,p,f;l===void 0||l.mean===void 0?p=[255,255,255,255]:typeof l.mean=="number"?p=[l.mean,l.mean,l.mean,l.mean]:(p=[l.mean[0],l.mean[1],l.mean[2],255],l.mean[3]!==void 0&&(p[3]=l.mean[3])),l===void 0||l.bias===void 0?f=[0,0,0,0]:typeof l.bias=="number"?f=[l.bias,l.bias,l.bias,l.bias]:(f=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(f[3]=l.bias[3]));let h=n*a;if(t!==void 0&&(t.format!==void 0&&s===4&&t.format!=="RGBA"||s===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let g=4,_=0,y=1,b=2,S=3,x=0,w=h,I=h*2,k=-1;u==="RGBA"?(x=0,w=h,I=h*2,k=h*3):u==="RGB"?(x=0,w=h,I=h*2):u==="RBG"&&(x=0,I=h,w=h*2),i=r.createImageData(a,n);for(let E=0;E<n*a;_+=g,y+=g,b+=g,S+=g,E++)i.data[_]=(e.data[x++]-f[0])*p[0],i.data[y]=(e.data[w++]-f[1])*p[1],i.data[b]=(e.data[I++]-f[2])*p[2],i.data[S]=k===-1?255:(e.data[k++]-f[3])*p[3]}else throw new Error("Can not access image data");return i}}),Ir,qd,Ld,Wd,Vd,Gd,Mg=U(()=>{Oa(),Ir=(e,t)=>
vendor: 12,319 bytes, line 5
5{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:i}=t,a=t.norm??{mean:255,bias:0},n,s;typeof a.mean=="number"?n=[a.mean,a.mean,a.mean,a.mean]:n=[a.mean[0],a.mean[1],a.mean[2],a.mean[3]??255],typeof a.bias=="number"?s=[a.bias,a.bias,a.bias,a.bias]:s=[a.bias[0],a.bias[1],a.bias[2],a.bias[3]??0];let u=t.format!==void 0?t.format:"RGBA",l=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",p=r*i,f=l==="RGBA"?new Float32Array(p*4):new Float32Array(p*3),h=4,g=0,_=1,y=2,b=3,S=0,x=p,w=p*2,I=-1;u==="RGB"&&(h=3,g=0,_=1,y=2,b=-1),l==="RGBA"?I=p*3:l==="RBG"?(S=0,w=p,x=p*2):l==="BGR"&&(w=0,x=p,S=p*2);for(let k=0;k<p;k++,g+=h,y+=h,_+=h,b+=h)f[S++]=(e[g]+s[0])/n[0],f[x++]=(e[_]+s[1])/n[1],f[w++]=(e[y]+s[2])/n[2],I!==-1&&b!==-1&&(f[I++]=(e[b]+s[3])/n[3]);return l==="RGBA"?new Pe("float32",f,[1,4,r,i]):new Pe("float32",f,[1,3,r,i])},qd=async(e,t)=>{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,i=typeof ImageData<"u"&&e instanceof ImageData,a=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,n=typeof e=="string",s,u=t??{},l=()=>{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=f=>typeof HTMLCanvasElement<"u"&&f instanceof HTMLCanvasElement||f instanceof OffscreenCanvas?f.getContext("2d"):null;if(r){let f=l();f.width=e.width,f.height=e.height;let h=p(f);if(h!=null){let g=e.height,_=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(g=t.resizedHeight,_=t.resizedWidth),t!==void 0){if(u=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");u.tensorFormat="RGBA",u.height=g,u.width=_}else u.tensorFormat="RGBA",u.height=g,u.width=_;h.drawImage(e,0,0),s=h.getImageData(0,0,_,g).data}else throw new Error("Can not access image data")}else if(i){let f,h;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(f=t.resizedHeight,h=t.resizedWidth):(f=e.height,h=e.width),t!==void 0&&(u=t),u.format="RGBA",u.height=f,u.width=h,t!==void 0){let g=l();g.width=h,g.height=f;let _=p(g);if(_!=null)_.putImageData(e,0,0),s=_.getImageData(0,0,h,f).data;else throw new Error("Can not access image data")}else s=e.data}else if(a){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let f=l();f.width=e.width,f.height=e.height;let h=p(f);if(h!=null){let g=e.height,_=e.width;return h.drawImage(e,0,0,_,g),s=h.getImageData(0,0,_,g).data,u.height=g,u.width=_,Ir(s,u)}else throw new Error("Can not access image data")}else{if(n)return new Promise((f,h)=>{let g=l(),_=p(g);if(!e||!_)return h();let y=new Image;y.crossOrigin="Anonymous",y.src=e,y.onload=()=>{g.width=y.width,g.height=y.height,_.drawImage(y,0,0,g.width,g.height);let b=_.getImageData(0,0,g.width,g.height);u.height=g.height,u.width=g.width,f(Ir(b.data,u))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(s!==void 0)return Ir(s,u);throw new Error("Input data provided is not supported - aborted tensor creation")},Ld=(e,t)=>{let{width:r,height:i,download:a,dispose:n}=t,s=[1,i,r,4];return new Pe({location:"texture",type:"float32",texture:e,dims:s,download:a,dispose:n})},Wd=(e,t)=>{let{dataType:r,dims:i,download:a,dispose:n}=t;return new Pe({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:i,download:a,dispose:n})},Vd=(e,t)=>{let{dataType:r,dims:i,download:a,dispose:n}=t;return new Pe({location:"ml-tensor",type:r??"float32",mlTensor:e,dims:i,download:a,dispose:n})},Gd=(e,t,r)=>new Pe({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})}),It,or,wi,Hd,Dg=U(()=>{It=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),or=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),wi=!1,Hd=()=>{if(!wi){wi=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=globalThis.Float16Array,i=typeof r<"u"&&r.from;e&&(It.set("int64",BigInt64Array),or.set(BigInt64Array,"int64")),t&&(It.set("uint64",BigUint64Array),or.set(BigUint64Array,"uint64")),i?(It.set("float16",r),or.set(r,"float16")):It.set("float16",Uint16Array)}}}),Fd,jd,Ug=U(()=>{Oa(),Fd=e=>{let t=1;for(let r=0;r<e.length;r++){let i=e[r];if(typeof i!="number"||!Number.isSafeInteger(i))throw new TypeError(`dims[${r}] must be an integer, got: ${i}`);if(i<0)throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${i}`);t*=i}return t},jd=(e,t)=>{switch(e.location){case"cpu":return new Pe(e.type,e.data,t);case"cpu-pinned":return new Pe({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new Pe({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new Pe({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new Pe({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),Pe,Oa=U(()=>{Ng(),Mg(),Dg(),Ug(),Pe=class{constructor(e,t,r){Hd();let i,a;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,i=e.type,a=e.dims,e.location){case"cpu-pinned":{let s=It.get(i);if(!s)throw new TypeError(`unsupported type "${i}" to create tensor from pinned buffer`);if(!(e.data instanceof s))throw new TypeError(`buffer should be of type ${s.name}`);this.cpuData=e.data;break}case"texture":{if(i!=="float32")throw new TypeError(`unsupported type "${i}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint64"&&i!=="int8"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let s,u;if(typeof e=="string")if(i=e,u=r,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");s=t}else{let l=It.get(e);if(l===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&l===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${l.name} as data.`);e==="uint64"||e==="int64"?s=l.from(t,BigInt):s=l.from(t)}else if(t instanceof l)s=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")s=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&l!==Uint16Array)s=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${i} tensor's data must be type of ${l}`)}else if(u=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let l=typeof e[0];if(l==="string")i="string",s=e;else if(l==="boolean")i="bool",s=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${l}.`)}else if(e instanceof Uint8ClampedArray)i="uint8",s=Uint8Array.from(e);else{let l=or.get(e.constructor);if(l===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);i=l,s=e}if(u===void 0)u=[s.length];else if(!Array.isArray(u))throw new TypeError("A tensor's dims must be a number array");a=u,this.cpuData=s,this.dataLocation="cpu"}let n=Fd(a);if(this.cpuData&&n!==this.cpuData.length&&!((i==="uint4"||i==="int4")&&Math.ceil(n/2)===this.cpuData.length))throw new Error(`Tensor's size(${n}) does not match data length(${this.cpuData.length}).`);this.type=i,this.dims=a,this.size=n}static async fromImage(e,t){return qd(e,t)}static fromTexture(e,t){return Ld(e,t)}static fromGpuBuffer(e,t){return Wd(e,t)}static fromMLTensor(e,t){return Vd(e,t)}static fromPinnedBuffer(e,t,r){return Gd(e,t,r)}toDataURL(e){return Ud(this,e)}toImageData(e){return Pd(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return jd(this,e)}}}),ze,Kd=U(()=>{Oa(),ze=Pe}),Wr,$i,rt,Xe,Ct,At,Zd=U(()=>{Dd(),Wr=(e,t)=>{(typeof Ie.trace>"u"?!Ie.wasm.trace:!Ie.trace)||console.timeStamp(`${e}::ORT::${t}`)},$i=(e,t)=>{var a;let r=((a=new Error().stack)==null?void 0:a.split(/\r\n|\r|\n/g))||[],i=!1;for(let n=0;n<r.length;n++){if(i&&!r[n].includes("TRACE_FUNC")){let s=`FUNC_${e}::${r[n].trim().split(" ")[1]}`;t&&(s+=`::${t}`),Wr("CPU",s);return}r[n].includes("TRACE_FUNC")&&(i=!0)}},rt=e=>{(typeof Ie.trace>"u"?!Ie.wasm.trace:!Ie.trace)||$i("BEGIN",e)},Xe=e=>{(typeof Ie.trace>"u"?!Ie.wasm.trace:!Ie.trace)||$i("END",e)},Ct=e=>{(typeof Ie.trace>"u"?!Ie.wasm.trace:!Ie.trace)||console.time(`ORT::${e}`)},At=e=>{(typeof Ie.trace>"u"?!Ie.wasm.trace:!Ie.trace)||console.timeEnd(`ORT::${e}`)}}),Qd,Pg=U(()=>{Nd(),Kd(),Zd(),Qd=class Xd{constructor(t){this.handler=t}async run(t,r,i){rt(),Ct("InferenceSession.run");let a={},n={};if(typeof t!="object"||t===null||t instanceof ze||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let s=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof ze)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.");s=!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 invali
5d output name: ${p}.`);a[p]=null}if(typeof i=="object"&&i!==null)n=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,f=Object.getOwnPropertyNames(r);for(let h of this.outputNames)if(f.indexOf(h)!==-1){let g=r[h];(g===null||g instanceof ze)&&(p=!0,s=!1,a[h]=g)}if(p){if(typeof i=="object"&&i!==null)n=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else n=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(s)for(let p of this.outputNames)a[p]=null;let u=await this.handler.run(t,a,n),l={};for(let p in u)if(Object.hasOwnProperty.call(u,p)){let f=u[p];f instanceof ze?l[p]=f:l[p]=new ze(f.type,f.data,f.dims)}return At("InferenceSession.run"),Xe(),l}async release(){return this.handler.dispose()}static async create(t,r,i,a){rt(),Ct("InferenceSession.create");let n,s={};if(typeof t=="string"){if(n=t,typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(n=t,typeof r=="object"&&r!==null)s=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 f=t,h=0,g=t.byteLength;if(typeof r=="object"&&r!==null)s=r;else if(typeof r=="number"){if(h=r,!Number.isSafeInteger(h))throw new RangeError("'byteOffset' must be an integer.");if(h<0||h>=f.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${f.byteLength}).`);if(g=t.byteLength-h,typeof i=="number"){if(g=i,!Number.isSafeInteger(g))throw new RangeError("'byteLength' must be an integer.");if(g<=0||h+g>f.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${f.byteLength-h}].`);if(typeof a=="object"&&a!==null)s=a;else if(typeof a<"u")throw new TypeError("'options' must be an object.")}else if(typeof i<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");n=new Uint8Array(f,h,g)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[u,l]=await Bd(s),p=await u.createInferenceSessionHandler(n,l);return At("InferenceSession.create"),Xe(),new Xd(p)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),Lt,qg=U(()=>{Pg(),Lt=Qd}),Lg=U(()=>{}),Wg=U(()=>{}),Vg=U(()=>{}),Gg=U(()=>{}),Hg={};Ht(Hg,{InferenceSession:()=>Lt,TRACE:()=>Wr,TRACE_EVENT_BEGIN:()=>Ct,TRACE_EVENT_END:()=>At,TRACE_FUNC_BEGIN:()=>rt,TRACE_FUNC_END:()=>Xe,Tensor:()=>ze,env:()=>$e,registerBackend:()=>qt});var Ve=U(()=>{Og(),Bg(),qg(),Kd(),Lg(),Wg(),Zd(),Vg(),Gg()}),Ra=U(()=>{}),Yd={};Ht(Yd,{default:()=>Jd});var vi,xi,Jd,Fg=U(()=>{var e;nf(),Nt(),Ba(),vi="ort-wasm-proxy-worker",xi=((e=globalThis.self)==null?void 0:e.name)===vi,xi&&(self.onmessage=t=>{let{type:r,in:i}=t.data;try{switch(r){case"init-wasm":Na(i.wasm).then(()=>{Ya(i).then(()=>{postMessage({type:r})},a=>{postMessage({type:r,err:a})})},a=>{postMessage({type:r,err:a})});break;case"init-ep":{let{epName:a,env:n}=i;Ja(n,a).then(()=>{postMessage({type:r})},s=>{postMessage({type:r,err:s})});break}case"copy-from":{let{buffer:a}=i,n=Zr(a);postMessage({type:r,out:n});break}case"create":{let{model:a,options:n}=i;en(a,n).then(s=>{postMessage({type:r,out:s})},s=>{postMessage({type:r,err:s})});break}case"release":tn(i),postMessage({type:r});break;case"run":{let{sessionId:a,inputIndices:n,inputs:s,outputIndices:u,options:l}=i;rn(a,n,s,u,new Array(u.length).fill(null),l).then(p=>{p.some(f=>f[3]!=="cpu")?postMessage({type:r,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:r,out:p},nn([...s,...p]))},p=>{postMessage({type:r,err:p})});break}case"end-profiling":an(i),postMessage({type:r});break;default:}}catch(a){postMessage({type:r,err:a})}}),Jd=xi?null:t=>new Worker(t??Ue,{type:"module",name:vi})}),ep={};Ht(ep,{default:()=>tp});async function to(e={}){var Xs,Ys;var t=e,r=!!globalThis.window,i=!!globalThis.WorkerGlobalScope,a=i&&((Xs=self.name)==null?void 0:Xs.startsWith("em-pthread"));t.mountExternalData=(o,d)=>{o.startsWith("./")&&(o=o.substring(2)),(t.Xc||(t.Xc=new Map)).set(o,d)},t.unmountExternalData=()=>{delete t.Xc},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let n=o=>async(...d)=>{var m;try{if(t.Yc)throw Error("Session already started");let c=t.Yc={Kd:d[0],errors:[]},$=await o(...d);if(t.Yc!==c)throw Error("Session mismatch");(m=t.dd)==null||m.flush();let T=c.errors;if(0<T.length){let z=await Promise.all(T);if(z=z.filter(B=>
vendor: 29,350 bytes, lines 5-10
5B),0<z.length)throw Error(z.join(`
6`))}return $}finally{t.Yc=null}};t.jsepInit=(o,d)=>{if(o==="webgpu"){[t.dd,t.Ad,t.Ed,t.ed,t.Dd,t.$b,t.Fd,t.Hd,t.Bd,t.Cd,t.Gd]=d;let m=t.dd;t.jsepRegisterBuffer=(c,$,T,z)=>m.registerBuffer(c,$,T,z),t.jsepGetBuffer=c=>m.getBuffer(c),t.jsepCreateDownloader=(c,$,T)=>m.createDownloader(c,$,T),t.jsepOnCreateSession=c=>{m.onCreateSession(c)},t.jsepOnReleaseSession=c=>{m.onReleaseSession(c)},t.jsepOnRunStart=c=>m.onRunStart(c),t.Id=(c,$)=>{m.upload(c,$)}}else if(o==="webnn"){let m=d[0];[t.Wd,t.sd,t.webnnEnsureTensor,t.td,t.webnnDownloadTensor,t.Rd,t.webnnEnableTraceEvent]=d.slice(1),t.webnnReleaseTensorId=t.sd,t.webnnUploadTensor=t.td,t.webnnRegisterMLContext=t.Rd,t.webnnOnRunStart=c=>m.onRunStart(c),t.webnnOnRunEnd=m.onRunEnd.bind(m),t.webnnOnReleaseSession=c=>{m.onReleaseSession(c)},t.webnnCreateMLTensorDownloader=(c,$)=>m.createMLTensorDownloader(c,$),t.webnnRegisterMLTensor=(c,$,T,z)=>m.registerMLTensor(c,$,T,z),t.webnnCreateMLContext=c=>m.createMLContext(c),t.webnnRegisterMLConstant=(c,$,T,z,B,L)=>m.registerMLConstant(c,$,T,z,B,t.Xc,L),t.webnnRegisterGraphInput=m.registerGraphInput.bind(m),t.webnnIsGraphInput=m.isGraphInput.bind(m),t.webnnRegisterGraphOutput=m.registerGraphOutput.bind(m),t.webnnIsGraphOutput=m.isGraphOutput.bind(m),t.webnnCreateTemporaryTensor=m.createTemporaryTensor.bind(m),t.webnnIsGraphInputOutputTypeSupported=m.isGraphInputOutputTypeSupported.bind(m)}};let s=()=>{let o=d=>(...m)=>{let c=Je;return m=d(...m),Je!=c?new Promise(($,T)=>{si={resolve:$,reject:T}}):m};(()=>{for(let d of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[d]=o(t[d])})(),n!==void 0&&(t._OrtRun=n(t._OrtRun),t._OrtRunWithBinding=n(t._OrtRunWithBinding)),s=void 0};t.asyncInit=()=>{s==null||s()};var u,l,p=(o,d)=>{throw d},f=import.meta.url,h="";if(r||i){try{h=new URL(".",f).href}catch{}i&&(l=o=>{var d=new XMLHttpRequest;return d.open("GET",o,!1),d.responseType="arraybuffer",d.send(null),new Uint8Array(d.response)}),u=async o=>{if(A(o))return new Promise((m,c)=>{var $=new XMLHttpRequest;$.open("GET",o,!0),$.responseType="arraybuffer",$.onload=()=>{$.status==200||$.status==0&&$.response?m($.response):c($.status)},$.onerror=c,$.send(null)});var d=await fetch(o,{credentials:"same-origin"});if(d.ok)return d.arrayBuffer();throw Error(d.status+" : "+d.url)}}var g,_,y,b,S,x,w=console.log.bind(console),I=console.error.bind(console),k=w,E=I,C=!1,A=o=>o.startsWith("file://");function v(){dt.buffer!=D.buffer&&V()}if(a){let o=function(d){try{var m=d.data,c=m.Sc;if(c==="load"){let $=[];self.onmessage=T=>$.push(T),x=()=>{postMessage({Sc:"loaded"});for(let T of $)o(T);self.onmessage=o};for(let T of m.xd)t[T]&&!t[T].proxy||(t[T]=(...z)=>{postMessage({Sc:"callHandler",wd:T,args:z})},T=="print"&&(k=t[T]),T=="printErr"&&(E=t[T]));dt=m.Od,V(),_=m.Pd,Le(),Tr()}else if(c==="run"){(function($){var T=(v(),K)[$+52>>>2>>>0];$=(v(),K)[$+56>>>2>>>0],os(T,T-$),se(T)})(m.Rc),pi(m.Rc,0,0,1,0,0),un(),ii(m.Rc),M||(ts(),M=!0);try{_f(m.Md,m.bd)}catch($){if($!="unwind")throw $}}else m.target!=="setimmediate"&&(c==="checkMailbox"?M&&br():c&&(E(`worker: received unknown command ${c}`),E(m)))}catch($){throw rs(),$}};var M=!1;self.onunhandledrejection=d=>{throw d.reason||d},self.onmessage=o}var D,H,F,j,R,K,Z,ee,he,W,ue,P=!1;function V(){var o=dt.buffer;t.HEAP8=D=new Int8Array(o),F=new Int16Array(o),t.HEAPU8=H=new Uint8Array(o),j=new Uint16Array(o),t.HEAP32=R=new Int32Array(o),t.HEAPU32=K=new Uint32Array(o),Z=new Float32Array(o),ee=new Float64Array(o),he=new BigInt64Array(o),W=new BigUint64Array(o)}function Q(){P=!0,a?x():at.sb()}function q(o){throw E(o="Aborted("+o+")"),C=!0,o=new WebAssembly.RuntimeError(o+". Build with -sASSERTIONS for more info."),S==null||S(o),o}function me(){return{a:{ma:qm,gb:Pm,g:yf,J:bf,f:wf,o:$f,h:vf,ha:xf,b:Sf,T:kf,Ha:fn,n:Tf,$:yn,Xa:bn,Da:wn,Fa:$n,Ya:vn,Va:xn,Oa:Sn,Ua:kn,ka:Tn,Ea:In,Ba:En,Wa:zn,Ca:Cn,bb:If,ea:Ef,wa:zf,ua:Af,da:Rf,O:Bf,H:Nf,va:Mf,_:Vf,xa:Gf,Ra:Hf,za:jf,Ia:Kf,sa:Zf,fa:Qf,Qa:ii,_a:Xf,R:tm,r:sm,c:ti,hb:om,y:um,M:lm,D:dm,l:pm,s:Un,ib:cm,I:hm,S:fm,j:mm,u:gm,q:_m,k:ym,La:bm,Ma:wm,Na:$m,Ja:Wn,Ka:Vn,ta:Gn,db:xm,ab:km,v:Tm,aa:Im,ga:Em,$a:Sm,W:zm,Za:Cm,Aa:Am,F:vm,U:Om,la:Sr,ya:Bm,fb:Rm,eb:Nm,Sa:Kn,Ta:Zn,Ga:Ft,V:Qn,ja:Xn,Pa:Yn,ia:Jn,kb:vg,na:_g,lb:$g,oa:gg,G:og,d:Gm,t:Wm,w:Lm,A:eg,mb:hg,K:ag,x:jm,pa:fg,Y:yg,ba:cg,nb:pg,ob:dg,P:tg,qa:lg,pb:ug,N:ng,Z:mg,e:Vm,B:Fm,m:Hm,jb:xg,p:Zm,z:Qm,C:Km,E:Xm,L:rg,qb:sg,Q:bg,ca:ig,X:wg,rb:Jm,ra:Ym,i:Dm,a:dt,cb:De}}}async function Le(){function o(c,$){var T=at=c.exports;c={};for(let[z,B]of Object.entries(T))typeof B=="function"?(T=Yf(B),c[z]=T):c[z]=B;return at=c,at=(function(){var z=at,B=G=>ne=>G(ne)>>>0,L=G=>()=>G()>>>0;return(z=Object.assign({},z)).tb=B(z.tb),z.Xb=L(z.Xb),z.Zb=B(z.Zb),z.lc=B(z.lc),z.mc=L(z.mc),z.qc=B(z.qc),z})(),sn.push(at._b),es=(c=at).tb,ts=c.ub,t._OrtInit=c.vb,t._OrtGetLastError=c.wb,t._OrtCreateSessionOptions=c.xb,t._OrtAppendExecutionProvider=c.yb,t._OrtAddFreeDimensionOverride=c.zb,t._OrtAddSessionConfigEntry=c.Ab,t._OrtReleaseSessionOptions=c.Bb,t._OrtCreateSession=c.Cb,t._OrtReleaseSession=c.Db,t._OrtGetInputOutputCount=c.Eb,t._OrtGetInputOutputMetadata=c.Fb,t._OrtFree=c.Gb,t._OrtCreateTensor=c.Hb,t._OrtGetTensorData=c.Ib,t._OrtReleaseTensor=c.Jb,t._OrtCreateRunOptions=c.Kb,t._OrtAddRunConfigEntry=c.Lb,t._OrtReleaseRunOptions=c.Mb,t._OrtCreateBinding=c.Nb,t._OrtBindInput=c.Ob,t._OrtBindOutput=c.Pb,t._OrtClearBoundOutputs=c.Qb,t._OrtReleaseBinding=c.Rb,t._OrtRunWithBinding=c.Sb,t._OrtRun=c.Tb,t._OrtEndProfiling=c.Ub,t._JsepOutput=c.Vb,t._JsepGetNodeName=c.Wb,kr=c.Xb,et=t._free=c.Yb,Zt=t._malloc=c.Zb,pi=c.ac,rs=c.bc,is=c.cc,as=c.dc,ci=c.ec,ns=c.fc,ss=c.gc,le=c.hc,Qt=c.ic,os=c.jc,se=c.kc,hi=c.lc,oe=c.mc,us=c.nc,fi=c.oc,ls=c.pc,ds=c.qc,ps=c.rc,mi=c.sc,cs=c.tc,hs=c.uc,fs=c.vc,ms=c.wc,gs=c.xc,_s=c.yc,ys=c.zc,bs=c.Ac,ws=c.Bc,$s=c.Cc,vs=c.Dc,xs=c.Ec,Ss=c.Fc,ks=c.Gc,Ts=c.Hc,Is=c.Ic,Es=c.Jc,zs=c.Kc,Cs=c.Lc,As=c.Mc,Os=c.Nc,Rs=c.Pc,Bs=c.Qc,Ns=c.$c,Ms=c.ad,Ds=c.fd,Us=c.jd,Ps=c.kd,qs=c.ld,Ls=c.md,Ws=c.nd,Vs=c.od,Gs=c.pd,Hs=c.qd,Fs=c.vd,js=c.Sd,Ks=c.Td,Zs=c.Ud,Qs=c.Vd,_=$,at}var d,m=me();return t.instantiateWasm?new Promise(c=>{t.instantiateWasm(m,($,T)=>{c(o($,T))})}):a?o(new WebAssembly.Instance(_,me()),_):(ue??(ue=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",h):h+"ort-wasm-simd-threaded.jsep.wasm":new URL(""+new URL("../assets/ort-wasm-simd-threaded.jsep.CyqnNavA.wasm",import.meta.url).href,import.meta.url).href),d=await(async function(c){var $=ue;if(!g&&!A($))try{var T=fetch($,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(T,c)}catch(z){E(`wasm streaming compile failed: ${z}`),E("falling back to ArrayBuffer instantiation")}return(async function(z,B){try{var L=await(async function(G){if(!g)try{var ne=await u(G);return new Uint8Array(ne)}catch{}if(G==ue&&g)G=new Uint8Array(g);else{if(!l)throw"both async and sync fetching of the wasm failed";G=l(G)}return G})(z);return await WebAssembly.instantiate(L,B)}catch(G){E(`failed to asynchronously prepare wasm: ${G}`),q(G)}})($,c)})(m),o(d.instance,d.module))}class Se{constructor(d){Js(this,"name","ExitStatus");this.message=`Program terminated with exit(${d})`,this.status=d}}var Oe=o=>{o.terminate(),o.onmessage=()=>{}},Re=[],Me=0,Be=null,ut=o=>{lt.length==0&&(dn(),ln(lt[0]));var d=lt.pop();if(!d)return 6;jt.push(d),bt[o.Rc]=d,d.Rc=o.Rc;var m={Sc:"run",Md:o.Ld,bd:o.bd,Rc:o.Rc};return d.postMessage(m,o.rd),0},be=0,re=(o,d,...m)=>{var c,$=16*m.length,T=oe(),z=hi($),B=z>>>3;for(c of m)typeof c=="bigint"?((v(),he)[B++>>>0]=1n,(v(),he)[B++>>>0]=c):((v(),he)[B++>>>0]=0n,(v(),ee)[B++>>>0]=c);return o=is(o,0,$,z,d),se(T),o};function De(o){if(a)return re(0,1,o);if(y=o,!(0<be)){for(var d of jt)Oe(d);for(d of lt)Oe(d);lt=[],jt=[],bt={},C=!0}p(0,new Se(o))}function fr(o){if(a)return re(1,0,o);Ft(o)}var Ft=o=>{if(y=o,a)throw fr(o),"unwind";De(o)},lt=[],jt=[],sn=[],bt={},on=o=>{var d=o.Rc;delete bt[d],lt.push(o),jt.splice(jt.indexOf(o),1),o.Rc=0,as(d)};function un(){sn.forEach(o=>o())}var ln=o=>new Promise(d=>{o.onmessage=$=>{var T=$.data;if($=T.Sc,T.Zc&&T.Zc!=kr()){var z=bt[T.Zc];z?z.postMessage(T,T.rd):E(`Internal error! Worker sent a message "${$}" to target pthread ${T.Zc}, but that thread no longer exists!`)}else $==="checkMailbox"?br():$==="spawnThread"?ut(T):$==="cleanupThread"?yr(()=>{on(bt[T.Nd])}):$==="loaded"?(o.loaded=!0,d(o)):T.target==="setimmediate"?o.postMessage(T):$==="uncaughtException"?o.onerror(T.error):$==="callHandler"?t[T.wd](...T.args):$&&E(`worker sent an unknown command ${$}`)},o.onerror=$=>{throw E(`worker sent an error! ${$.filename}:${$.lineno}: ${$.message}`),$};var m,c=[];for(m of[])t.propertyIsEnumerable(m)&&c.push(m);o.postMessage({Sc:"load",xd:c,Od:dt,Pd:_})});function dn(){var o=new Worker((()=>{let d=URL;return import.meta.url>"file:"&&import.meta.url<"file;"?new d("ort.bundle.min.mjs",import.meta.url):new URL(import.meta.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});lt.push(o)}var dt,_f=(o,d)=>{be=0,o=mi(o,d),0<be?y=o:ci(o)},mr=[],gr=0;function yf(o){var d=new Xr(o>>>=0);return(v(),D)[d.Tc+12>>>0]==0&&(pn(d,!0),gr--),cn(d,!1),mr.push(d),ds(o)}var Dt=0,bf=()=>{le(0,0);var o=mr.pop();us(o.cd),Dt=0};function pn(o,d){d=d?1:0,(v(),D)[o.Tc+12>>>0]=d}function cn(o,d){d=d?1:0,(v(),D)[o.Tc+13>>>0]=d}class Xr{constructor(d){this.cd=d,this.Tc=d-24}}var Yr=o=>{var d=Dt;if(!d)return Qt(0),0;var m=new Xr(d);(v(),K)[m.Tc+16>>>2>>>0]=d;var c=(v(),K)[m.Tc+4>>>2>>>0];if(!c)return Qt(0),d;for(var $ of o){if($===0||$===c)break;if(ls($,c,m.Tc+16))return Qt($),d}return Qt(c),d};function wf(){return Yr([])}function $f(o){return Yr([o>>>0])}function vf(o,d,m,c){return Yr([o>>>0,d>>>0,m>>>0,c>>>0])}var xf=()=>{var o=mr.pop();o||q("no exception to throw");var d=o.cd;throw(v(),D)[o.Tc+13>>>0]==0&&(mr.push(o),cn(o,!0),pn(o,!1),gr++),fi(d),Dt=d};function Sf(o,d,m){var c=new Xr(o>>>=0);throw d>>>=0,m>>>=0,(v(),K)[c.Tc+16>>>2>>>0]=0,(v(),K)[c.Tc+4>>>2>>>0]=d,(v(),K)[c.Tc+8>>>2>>>0]=m,fi(o),gr++,Dt=o}var kf=()=>gr;function hn(o,d,m,c){return a?re(2,1,o,d,m,c):fn(o,d,m,c)}function fn(o,d,m,c){if(o>>>=0,d>>>=0,m>>>=0,c>>>=0,!globalThis.SharedArrayBuffer)return 6;var $=[];return a&&$.length===0?hn(o,d,m,c):(o={Ld:m,Rc:o,bd:c,rd:$},a?(o.Sc="spawnThread",postMessage(o,$),0):ut(o))}function Tf(o){throw Dt||(Dt=o>>>0),Dt}var mn=globalThis.TextDecoder&&new TextDecoder,gn=(o,d,m,c)=>{if(m=d+m,c)return m;for(;o[d]&&!(d>=m);)++d;return d},_n=(o,d=0,m,c)=>{if(16<(m=gn(o,d>>>=0,m,c))-d&&o.buffer&&mn)return mn.decode(o.buffer instanceof ArrayBuffer?o.subarray(d,m):o.slice(d,m));for(c="";d<m;){var $=o[d++];if(128&$){var T=63&o[d++];if((224&$)==192)c+=String.fromCharCode((31&$)<<6|T);else{var z=63&o[d++];65536>($=(240&$)==224?(15&$)<<12|T<<6|z:(7&$)<<18|T<<12|z<<6|63&o[d++])?c+=String.fromCharCode($):($-=65536,c+=String.fromCharCode(55296|$>>10,56320|1023&$))}}else c+=String.fromCharCode($)}return c},ke=(o,d,m)=>(o>>>=0)?_n((v(),H),o,d,m):"";function yn(o,d,m){return a?re(3,1,o,d,m):0}function bn(o,d){if(a)return re(4,1,o,d)}function wn(o,d){if(a)return re(5,1,o,d)}function $n(o,d,m){if(a)return re(6,1,o,d,m)}function vn(o,d,m){return a?re(7,1,o,d,m):0}function xn(o,d){if(a)return re(8,1,o,d)}function Sn(o,d,m){if(a)return re(9,1,o,d,m)}function kn(o,d,m,c){if(a)return re(10,1,o,d,m,c)}function Tn(o,d,m,c){if(a)return re(11,1,o,d,m,c)}function In(o,d,m,c){if(a)return re(12,1,o,d,m,c)}function En(o){if(a)return re(13,1,o)}function zn(o,d){if(a)return re(14,1,o,d)}function Cn(o,d,m){if(a)return re(15,1,o,d,m)}var If=()=>q(""),Ye=o=>{o>>>=0;for(var d="";;){var m=(v(),H)[o++>>>0];if(!m)return d;d+=String.fromCharCode(m)}},Jr={},ei={},Ut=class extends Error{constructor(o){super(o),this.name="BindingError"}};function it(o,d,m={}){return(function(c,$,T={}){var z=$.name;if(!c)throw new Ut(`type "${z}" must have a positive integer typeid pointer`);if(ei.hasOwnProperty(c)){if(T.yd)return;throw new Ut(`Cannot register type '${z}' twice`)}ei[c]=$,Jr.hasOwnProperty(c)&&($=Jr[c],delete Jr[c],$.forEach(B=>B()))})(o,d,m)}var An=(o,d,m)=>{switch(d){case 1:return m?c=>(v(),D)[c>>>0]:c=>(v(),H)[c>>>0];case 2:return m?c=>(v(),F)[c>>>1>>>0]:c=>(v(),j)[c>>>1>>>0];case 4:return m?c=>(v(),R)[c>>>2>>>0]:c=>(v(),K)[c>>>2>>>0];case 8:return m?c=>(v(),he)[c>>>3>>>0]:c=>(v(),W)[c>>>3>>>0];default:throw new TypeError(`invalid integer width (${d}): ${o}`)}};function Ef(o,d,m,c,$){o>>>=0,m>>>=0,d=Ye(d>>>0);let T=z=>z;if(c=c===0n){let z=8*m;T=B=>BigInt.asUintN(z,B),$=T($)}it(o,{name:d,Oc:T,Vc:(z,B)=>(typeof B=="number"&&(B=BigInt(B)),B),Uc:An(d,m,!c),Wc:null})}function zf(o,d,m,c){it(o>>>=0,{name:d=Ye(d>>>0),Oc:function($){return!!$},Vc:function($,T){return T?m:c},Uc:function($){return this.Oc((v(),H)[$>>>0])},Wc:null})}var On=[],wt=[0,1,,1,null,1,!0,1,!1,1];function ti(o){9<(o>>>=0)&&--wt[o+1]==0&&(wt[o]=void 0,On.push(o))}var We=o=>{if(!o)throw new Ut(`Cannot use deleted val. handle = ${o}`);return wt[o]},Ge=o=>{switch(o){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let d=On.pop()||wt.length;return wt[d]=o,wt[d+1]=1,d}};function ri(o){return this.Oc((v(),K)[o>>>2>>>0])}var Cf={name:"emscripten::val",Oc:o=>{var d=We(o);return ti(o),d},Vc:(o,d)=>Ge(d),Uc:ri,Wc:null};function Af(o){return it(o>>>0,Cf)}var Of=(o,d)=>{switch(d){case 4:return function(m){return this.Oc((v(),Z)[m>>>2>>>0])};case 8:return function(m){return this.Oc((v(),ee)[m>>>3>>>0])};default:throw new TypeError(`invalid float width (${d}): ${o}`)}};function Rf(o,d,m){m>>>=0,it(o>>>=0,{name:d=Ye(d>>>0),Oc:c=>c,Vc:(c,$)=>$,Uc:Of(d,m),Wc:null})}function Bf(o,d,m,c,$){o>>>=0,m>>>=0,d=Ye(d>>>0);let T=B=>B;if(c===0){var z=32-8*m;T=B=>B<<z>>>z,$=T($)}it(o,{name:d,Oc:T,Vc:(B,L)=>L,Uc:An(d,m,c!==0),Wc:null})}function Nf(o,d,m){function c(T){var z=(v(),K)[T>>>2>>>0];return T=(v(),K)[T+4>>>2>>>0],new $((v(),D).buffer,T,z)}var $=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][d];it(o>>>=0,{name:m=Ye(m>>>0),Oc:c,Uc:c},{yd:!0})}var pt=(o,d,m)=>{var c=(v(),H);if(d>>>=0,0<m){var $=d;m=d+m-1;for(var T=0;T<o.length;++T){var z=o.codePointAt(T);if(127>=z){if(d>=m)break;c[d++>>>0]=z}else if(2047>=z){if(d+1>=m)break;c[d++>>>0]=192|z>>6,c[d++>>>0]=128|63&z}else if(65535>=z){if(d+2>=m)break;c[d++>>>0]=224|z>>12,c[d++>>>0]=128|z>>6&63,c[d++>>>0]=128|63&z}else{if(d+3>=m)break;c[d++>>>0]=240|z>>18,c[d++>>>0]=128|z>>12&63,c[d++>>>0]=128|z>>6&63,c[d++>>>0]=128|63&z,T++}}c[d>>>0]=0,o=d-$}else o=0;return o},_r=o=>{for(var d=0,m=0;m<o.length;++m){var c=o.charCodeAt(m);127>=c?d++:2047>=c?d+=2:55296<=c&&57343>=c?(d+=4,++m):d+=3}return d};function Mf(o,d){it(o>>>=0,{name:d=Ye(d>>>0),Oc(m){var c=(v(),K)[m>>>2>>>0];return c=ke(m+4,c,!0),et(m),c},Vc(m,c){c instanceof ArrayBuffer&&(c=new Uint8Array(c));var $=typeof c=="string";if(!($||ArrayBuffer.isView(c)&&c.BYTES_PER_ELEMENT==1))throw new Ut("Cannot pass non-string to std::string");var T=$?_r(c):c.length,z=Zt(4+T+1),B=z+4;return(v(),K)[z>>>2>>>0]=T,$?pt(c,B,T+1):(v(),H).set(c,B>>>0),m!==null&&m.push(et,z),z},Uc:ri,Wc(m){et(m)}})}var Rn=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,Df=(o,d,m)=>{if(o>>>=1,16<(d=gn((v(),j),o,d/2,m))-o&&Rn)return Rn.decode((v(),j).slice(o,d));for(m="";o<d;++o){var c=(v(),j)[o>>>0];m+=String.fromCharCode(c)}return m},Uf=(o,d,m)=>{if(m??(m=2147483647),2>m)return 0;var c=d;m=(m-=2)<2*o.length?m/2:o.length;for(var $=0;$<m;++$){var T=o.charCodeAt($);(v(),F)[d>>>1>>>0]=T,d+=2}return(v(),F)[d>>>1>>>0]=0,d-c},Pf=o=>2*o.length,qf=(o,d,m)=>{var c="";o>>>=2;for(var $=0;!($>=d/4);$++){var T=(v(),K)[o+$>>>0];if(!T&&!m)break;c+=String.fromCodePoint(T)}return c},Lf=(o,d,m)=>{if(d>>>=0,m??(m=2147483647),4>m)return 0;var c=d;m=c+m-4;for(var $=0;$<o.length;++$){var T=o.codePointAt($);if(65535<T&&$++,(v(),R)[d>>>2>>>0]=T,(d+=4)+4>m)break}return(v(),R)[d>>>2>>>0]=0,d-c},Wf=o=>{for(var d=0,m=0;m<o.length;++m)65535<o.codePointAt(m)&&m++,d+=4;return d};function Vf(o,d,m){if(o>>>=0,d>>>=0,m=Ye(m>>>=0),d===2)var c=Df,$=Uf,T=Pf;else c=qf,$=Lf,T=Wf;it(o,{name:m,Oc:z=>{var B=(v(),K)[z>>>2>>>0];return B=c(z+4,B*d,!0),et(z),B},Vc:(z,B)=>{if(typeof B!="string")throw new Ut(`Cannot pass non-string to C++ string type ${m}`);var L=T(B),G=Zt(4+L+d);return(v(),K)[G>>>2>>>0]=L/d,$(B,G+4,L+d),z!==null&&z.push(et,G),G},Uc:ri,Wc(z){et(z)}})}function Gf(o,d){it(o>>>=0,{zd:!0,name:d=Ye(d>>>0),Oc:()=>{},Vc:()=>{}})}function Hf(o){pi(o>>>0,!i,1,!r,131072,!1),un()}var yr=o=>{if(!C)try{if(o(),!(0<be))try{a?kr()&&ci(y):Ft(y)}catch(d){d instanceof Se||d=="unwind"||p(0,d)}}catch(d){d instanceof Se||d=="unwind"||p(0,d)}},Ff=!Atomics.waitAsync||((Ys=globalThis.navigator)==null?void 0:Ys.userAgent)&&91>Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function ii(o){o>>>=0,Ff||(Atomics.waitAsync((v(),R),o>>>2,o).value.then(br),o+=128,Atomics.store((v(),R),o>>>2,1))}var br=()=>yr(()=>{var o=kr();o&&(ii(o),ss())});function jf(o,d){(o>>>=0)==d>>>0?setTimeout(br):a?postMessage({Zc:o,Sc:"checkMailbox"}):(o=bt[o])&&o.postMessage({Sc:"checkMailbox"})}var ai=[];function Kf(o,d,m,c,$){for(d>>>=0,$>>>=0,ai.length=0,m=$>>>3,c=$+c>>>3;m<c;){var T;T=(v(),he)[m++>>>0]?(v(),he)[m++>>>0]:(v(),ee)[m++>>>0],ai.push(T)}return(d?gi[d]:Um[o])(...ai)}var Zf=()=>{be=0};function Qf(o){o>>>=0,a?postMessage({Sc:"cleanupThread",Nd:o}):on(bt[o])}function Xf(o){}var wr=o=>{try{o()}catch(d){q(d)}};function Yf(o){var d=(...m)=>{$r.push(o);try{return o(...m)}finally{C||($r.pop(),Je&&ct===1&&$r.length===0&&(ct=0,be+=1,wr(Ks),typeof Fibers<"u"&&Fibers.Zd()))}};return Mn.set(o,d),d}var ct=0,Je=null,Bn=0,$r=[],ni=new Map,Nn=new Map,Mn=new Map,Jf=0,si=null,em=[],Dn=o=>(function(d){if(!C){if(ct===0){var m=!1,c=!1;d(($=0)=>{if(!C&&(Bn=$,m=!0,c)){ct=2,wr(()=>Zs(Je)),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.resume(),$=!1;try{var T=(function(){var L=(v(),R)[Je+8>>>2>>>0];return L=Nn.get(L),L=Mn.get(L),--be,L()})()}catch(L){T=L,$=!0}var z=!1;if(!Je){var B=si;B&&(si=null,($?B.reject:B.resolve)(T),z=!0)}if($&&!z)throw T}}),c=!0,m||(ct=1,Je=(function(){var $=Zt(65548),T=$+12;if((v(),K)[$>>>2>>>0]=T,(v(),K)[$+4>>>2>>>0]=T+65536,T=$r[0],!ni.has(T)){var z=Jf++;ni.set(T,z),Nn.set(z,T)}return T=ni.get(T),(v(),R)[$+8>>>2>>>0]=T,$})(),typeof MainLoop<"u"&&MainLoop.ud&&MainLoop.pause(),wr(()=>js(Je)))}else ct===2?(ct=0,wr(Qs),et(Je),Je=null,em.forEach(yr)):q(`invalid state: ${ct}`);return Bn}})(d=>{o().then(d)});function tm(o){return o>>>=0,Dn(async()=>{var d=await We(o);return Ge(d)})}var oi=[],rm=o=>{var d=oi.length;return oi.push(o),d},im=(o,d)=>{for(var m=Array(o),c=0;c<o;++c){var $=c,T=(v(),K)[d+4*c>>>2>>>0],z=ei[T];if(z===void 0)throw o=`parameter ${c}`,T=es(T),d=Ye(T),et(T),new Ut(`${o} has unknown type ${d}`);m[$]=z}return m},am=(o,d,m)=>{var c=[];return o=o(c,m),c.length&&((v(),K)[d>>>2>>>0]=Ge(c)),o},nm={},vr=o=>{var d=nm[o];return d===void 0?Ye(o):d};function sm(o,d,m){var[c,...$]=im(o,d>>>0);d=c.Vc.bind(c);var T=$.map(L=>L.Uc.bind(L));o--;var z={toValue:We};switch(o=T.map((L,G)=>{var ne=`argFromPtr${G}`;return z[ne]=L,`${ne}(args${G?"+"+8*G:""})`}),m){case 0:var B="toValue(handle)";break;case 2:B="new (toValue(handle))";break;case 3:B="";break;case 1:z.getStringOrSymbol=vr,B="toValue(handle)[getStringOrSymbol(methodName)]"}return B+=`(${o})`,c.zd||(z.toReturnWire=d,z.emval_returnValue=am,B=`return emval_returnValue(toReturnWire, destructorsRef, ${B})`),B=`return function (handle, methodName, destructorsRef, args) {
7  ${B}
8  }`,m=new Function(Object.keys(z),B)(...Object.values(z)),B=`methodCaller<(${$.map(L=>L.name)}) => ${c.name}>`,rm(Object.defineProperty(m,"name",{value:B}))}function om(o,d){return d>>>=0,(o=We(o>>>0))==We(d)}function um(o){return(o>>>=0)?(o=vr(o),Ge(globalThis[o])):Ge(globalThis)}function lm(o){return o=vr(o>>>0),Ge(t[o])}function dm(o,d){return d>>>=0,o=We(o>>>0),d=We(d),Ge(o[d])}function pm(o){9<(o>>>=0)&&(wt[o+1]+=1)}function Un(o,d,m,c,$){return oi[o>>>0](d>>>0,m>>>0,c>>>0,$>>>0)}function cm(o,d,m,c,$){return Un(o>>>0,d>>>0,m>>>0,c>>>0,$>>>0)}function hm(){return Ge([])}function fm(o){o=We(o>>>0);for(var d=Array(o.length),m=0;m<o.length;m++)d[m]=o[m];return Ge(d)}function mm(o){return Ge(vr(o>>>0))}function gm(){return Ge({})}function _m(o){for(var d=We(o>>>=0);d.length;){var m=d.pop();d.pop()(m)}ti(o)}function ym(o,d,m){d>>>=0,m>>>=0,o=We(o>>>0),d=We(d),m=We(m),o[d]=m}function bm(o,d){o=-9007199254740992>o||9007199254740992<o?NaN:Number(o),d>>>=0,o=new Date(1e3*o),(v(),R)[d>>>2>>>0]=o.getUTCSeconds(),(v(),R)[d+4>>>2>>>0]=o.getUTCMinutes(),(v(),R)[d+8>>>2>>>0]=o.getUTCHours(),(v(),R)[d+12>>>2>>>0]=o.getUTCDate(),(v(),R)[d+16>>>2>>>0]=o.getUTCMonth(),(v(),R)[d+20>>>2>>>0]=o.getUTCFullYear()-1900,(v(),R)[d+24>>>2>>>0]=o.getUTCDay(),o=(o.getTime()-Date.UTC(o.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(v(),R)[d+28>>>2>>>0]=o}var Pn=o=>o%4==0&&(o%100!=0||o%400==0),qn=[0,31,60,91,121,152,182,213,244,274,305,335],Ln=[0,31,59,90,120,151,181,212,243,273,304,334];function wm(o,d){o=-9007199254740992>o||9007199254740992<o?NaN:Number(o),d>>>=0,o=new Date(1e3*o),(v(),R)[d>>>2>>>0]=o.getSeconds(),(v(),R)[d+4>>>2>>>0]=o.getMinutes(),(v(),R)[d+8>>>2>>>0]=o.getHours(),(v(),R)[d+12>>>2>>>0]=o.getDate(),(v(),R)[d+16>>>2>>>0]=o.getMonth(),(v(),R)[d+20>>>2>>>0]=o.getFullYear()-1900,(v(),R)[d+24>>>2>>>0]=o.getDay();var m=(Pn(o.getFullYear())?qn:Ln)[o.getMonth()]+o.getDate()-1|0;(v(),R)[d+28>>>2>>>0]=m,(v(),R)[d+36>>>2>>>0]=-60*o.getTimezoneOffset(),m=new Date(o.getFullYear(),6,1).getTimezoneOffset();var c=new Date(o.getFullYear(),0,1).getTimezoneOffset();o=0|(m!=c&&o.getTimezoneOffset()==Math.min(c,m)),(v(),R)[d+32>>>2>>>0]=o}function $m(o){o>>>=0;var d=new Date((v(),R)[o+20>>>2>>>0]+1900,(v(),R)[o+16>>>2>>>0],(v(),R)[o+12>>>2>>>0],(v(),R)[o+8>>>2>>>0],(v(),R)[o+4>>>2>>>0],(v(),R)[o>>>2>>>0],0),m=(v(),R)[o+32>>>2>>>0],c=d.getTimezoneOffset(),$=new Date(d.getFullYear(),6,1).getTimezoneOffset(),T=new Date(d.getFullYear(),0,1).getTimezoneOffset(),z=Math.min(T,$);return 0>m?(v(),R)[o+32>>>2>>>0]=+($!=T&&z==c):0<m!=(z==c)&&($=Math.max(T,$),d.setTime(d.getTime()+6e4*((0<m?z:$)-c))),(v(),R)[o+24>>>2>>>0]=d.getDay(),m=(Pn(d.getFullYear())?qn:Ln)[d.getMonth()]+d.getDate()-1|0,(v(),R)[o+28>>>2>>>0]=m,(v(),R)[o>>>2>>>0]=d.getSeconds(),(v(),R)[o+4>>>2>>>0]=d.getMinutes(),(v(),R)[o+8>>>2>>>0]=d.getHours(),(v(),R)[o+12>>>2>>>0]=d.getDate(),(v(),R)[o+16>>>2>>>0]=d.getMonth(),(v(),R)[o+20>>>2>>>0]=d.getYear(),o=d.getTime(),BigInt(isNaN(o)?-1:o/1e3)}function Wn(o,d,m,c,$,T,z){return a?re(16,1,o,d,m,c,$,T,z):-52}function Vn(o,d,m,c,$,T){if(a)return re(17,1,o,d,m,c,$,T)}var Kt={},vm=()=>performance.timeOrigin+performance.now();function Gn(o,d){if(a)return re(18,1,o,d);if(Kt[o]&&(clearTimeout(Kt[o].id),delete Kt[o]),!d)return 0;var m=setTimeout(()=>{delete Kt[o],yr(()=>ns(o,performance.timeOrigin+performance.now()))},d);return Kt[o]={id:m,Yd:d},0}function xm(o,d,m,c){o>>>=0,d>>>=0,m>>>=0,c>>>=0;var $=new Date().getFullYear(),T=new Date($,0,1).getTimezoneOffset();$=new Date($,6,1).getTimezoneOffset();var z=Math.max(T,$);(v(),K)[o>>>2>>>0]=60*z,(v(),R)[d>>>2>>>0]=+(T!=$),o=(d=B=>{var L=Math.abs(B);return`UTC${0<=B?"-":"+"}${String(Math.floor(L/60)).padStart(2,"0")}${String(L%60).padStart(2,"0")}`})(T),d=d($),$<T?(pt(o,m,17),pt(d,c,17)):(pt(o,c,17),pt(d,m,17))}var Sm=()=>Date.now();function km(o,d,m){return m>>>=0,0<=o&&3>=o?(o===0?o=Date.now():o=performance.timeOrigin+performance.now(),o=Math.round(1e6*o),(v(),he)[m>>>3>>>0]=BigInt(o),0):28}var ui=[],Hn=(o,d)=>{ui.length=0;for(var m;m=(v(),H)[o++>>>0];){var c=m!=105;d+=(c&=m!=112)&&d%8?4:0,ui.push(m==112?(v(),K)[d>>>2>>>0]:m==106?(v(),he)[d>>>3>>>0]:m==105?(v(),R)[d>>>2>>>0]:(v(),ee)[d>>>3>>>0]),d+=c?8:4}return ui};function Tm(o,d,m){return o>>>=0,d=Hn(d>>>0,m>>>0),gi[o](...d)}function Im(o,d,m){return o>>>=0,d=Hn(d>>>0,m>>>0),gi[o](...d)}var Em=()=>{};function zm(o,d){return E(ke(o>>>0,d>>>0))}var Cm=()=>{throw be+=1,"unwind"};function Am(){return 4294901760}var Om=()=>navigator.hardwareConcurrency,$t={},xr=o=>{var d;return(d=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(o))?+d[1]:(d=/:(\d+):\d+(?:\)|$)/.exec(o))?2147483648|+d[1]:0},Fn=o=>{for(var d of o)(o=xr(d))&&($t[o]=d)};function Rm(){var o=Error().stack.toString().split(`
9`);return o[0]=="Error"&&o.shift(),Fn(o),$t.gd=xr(o[3]),$t.Jd=o,$t.gd}function Sr(o){if(!(o=$t[o>>>0]))return 0;var d;if(d=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(o))o=d[1];else if(d=/^\s+at (.*) \(.*\)$/.exec(o))o=d[1];else{if(!(d=/^(.+?)@/.exec(o)))return 0;o=d[1]}et(Sr.hd??0),d=_r(o)+1;var m=Zt(d);return m&&pt(o,m,d),Sr.hd=m,Sr.hd}function Bm(o){o>>>=0;var d=(v(),H).length;if(o<=d||4294901760<o)return!1;for(var m=1;4>=m;m*=2){var c=d*(1+.2/m);c=Math.min(c,o+100663296);e:{c=(Math.min(4294901760,65536*Math.ceil(Math.max(o,c)/65536))-dt.buffer.byteLength+65535)/65536|0;try{dt.grow(c),V();var $=1;break e}catch{}$=void 0}if($)return!0}return!1}function Nm(o,d,m){if(o>>>=0,d>>>=0,$t.gd==o)var c=$t.Jd;else(c=Error().stack.toString().split(`
10`))[0]=="Error"&&c.shift(),Fn(c);for(var $=3;c[$]&&xr(c[$])!=o;)++$;for(o=0;o<m&&c[o+$];++o)(v(),R)[d+4*o>>>2>>>0]=xr(c[o+$]);return o}var li,di={},jn=()=>{var c;if(!li){var o,d={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(((c=globalThis.navigator)==null?void 0:c.language)??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(o in di)di[o]===void 0?delete d[o]:d[o]=di[o];var m=[];for(o in d)m.push(`${o}=${d[o]}`);li=m}return li};function Kn(o,d){if(a)return re(19,1,o,d);o>>>=0,d>>>=0;var m,c=0,$=0;for(m of jn()){var T=d+c;(v(),K)[o+$>>>2>>>0]=T,c+=pt(m,T,1/0)+1,$+=4}return 0}function Zn(o,d){if(a)return re(20,1,o,d);o>>>=0,d>>>=0;var m=jn();for(var c of((v(),K)[o>>>2>>>0]=m.length,o=0,m))o+=_r(c)+1;return(v(),K)[d>>>2>>>0]=o,0}function Qn(o){return a?re(21,1,o):52}function Xn(o,d,m,c){return a?re(22,1,o,d,m,c):52}function Yn(o,d,m,c){return a?re(23,1,o,d,m,c):70}var Mm=[null,[],[]];function Jn(o,d,m,c){if(a)return re(24,1,o,d,m,c);d>>>=0,m>>>=0,c>>>=0;for(var $=0,T=0;T<m;T++){var z=(v(),K)[d>>>2>>>0],B=(v(),K)[d+4>>>2>>>0];d+=8;for(var L=0;L<B;L++){var G=o,ne=(v(),H)[z+L>>>0],pe=Mm[G];ne===0||ne===10?((G===1?k:E)(_n(pe)),pe.length=0):pe.push(ne)}$+=B}return(v(),K)[c>>>2>>>0]=$,0}function Dm(o){return o>>>0}a||(function(){for(var o=t.numThreads-1;o--;)dn();Re.push(async()=>{var d=(async function(){if(!a)return Promise.all(lt.map(ln))})();Me++,await d,--Me==0&&Be&&(d=Be,Be=null,d())})})(),a||(dt=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),V()),t.wasmBinary&&(g=t.wasmBinary),t.stackSave=()=>oe(),t.stackRestore=o=>se(o),t.stackAlloc=o=>hi(o),t.setValue=function(o,d,m="i8"){switch(m.endsWith("*")&&(m="*"),m){case"i1":case"i8":(v(),D)[o>>>0]=d;break;case"i16":(v(),F)[o>>>1>>>0]=d;break;case"i32":(v(),R)[o>>>2>>>0]=d;break;case"i64":(v(),he)[o>>>3>>>0]=BigInt(d);break;case"float":(v(),Z)[o>>>2>>>0]=d;break;case"double":(v(),ee)[o>>>3>>>0]=d;break;case"*":(v(),K)[o>>>2>>>0]=d;break;default:q(`invalid type for setValue: ${m}`)}},t.getValue=function(o,d="i8"){switch(d.endsWith("*")&&(d="*"),d){case"i1":case"i8":return(v(),D)[o>>>0];case"i16":return(v(),F)[o>>>1>>>0];case"i32":return(v(),R)[o>>>2>>>0];case"i64":return(v(),he)[o>>>3>>>0];case"float":return(v(),Z)[o>>>2>>>0];case"double":return(v(),ee)[o>>>3>>>0];case"*":return(v(),K)[o>>>2>>>0];default:q(`invalid type for getValue: ${d}`)}},t.UTF8ToString=ke,t.stringToUTF8=pt,t.lengthBytesUTF8=_r;var es,ts,kr,et,Zt,pi,rs,is,as,ci,ns,ss,le,Qt,os,se,hi,oe,us,fi,ls,ds,ps,mi,cs,hs,fs,ms,gs,_s,ys,bs,ws,$s,vs,xs,Ss,ks,Ts,Is,Es,zs,Cs,As,Os,Rs,Bs,Ns,Ms,Ds,Us,Ps,qs,Ls,Ws,Vs,Gs,Hs,Fs,js,Ks,Zs,Qs,at,Um=[De,fr,hn,yn,bn,wn,$n,vn,xn,Sn,kn,Tn,In,En,zn,Cn,Wn,Vn,Gn,Kn,Zn,Qn,Xn,Yn,Jn],gi={973212:(o,d,m,c,$)=>{if(t===void 0||!t.Xc)return 1;if((o=ke(Number(o>>>0))).startsWith("./")&&(o=o.substring(2)),!(o=t.Xc.get(o)))return 2;if(d=Number(d>>>0),m=Number(m>>>0),c=Number(c>>>0),d+m>o.byteLength)return 3;try{let T=o.subarray(d,d+m);switch($){case 0:(v(),H).set(T,c>>>0);break;case 1:t.Qd?t.Qd(c,T):t.Id(c,T);break;default:return 4}return 0}catch{return 4}},974036:(o,d,m)=>{t.td(o,(v(),H).subarray(d>>>0,d+m>>>0))},974100:()=>t.Wd(),974142:o=>{t.sd(o)},974179:()=>{t.Bd()},974210:()=>{t.Cd()},974239:()=>{t.Gd()},974264:o=>t.Ad(o),974297:o=>t.Ed(o),974329:(o,d,m)=>{t.ed(Number(o),Number(d),Number(m),!0)},974392:(o,d,m)=>{t.ed(Number(o),Number(d),Number(m))},974449:()=>typeof wasmOffsetConverter<"u",974506:o=>{t.$b("Abs",o,void 0)},974557:o=>{t.$b("Neg",o,void 0)},974608:o=>{t.$b("Floor",o,void 0)},974661:o=>{t.$b("Ceil",o,void 0)},974713:o=>{t.$b("Reciprocal",o,void 0)},974771:o=>{t.$b("Sqrt",o,void 0)},974823:o=>{t.$b("Exp",o,void 0)},974874:o=>{t.$b("Erf",o,void 0)},974925:o=>{t.$b("Sigmoid",o,void 0)},974980:(o,d,m)=>{t.$b("HardSigmoid",o,{alpha:d,beta:m})},975059:o=>{t.$b("Log",o,void 0)},975110:o=>{t.$b("Sin",o,void 0)},975161:o=>{t.$b("Cos",o,void 0)},975212:o=>{t.$b("Tan",o,void 0)},975263:o=>{t.$b("Asin",o,void 0)},975315:o=>{t.$b("Acos",o,void 0)},975367:o=>{t.$b("Atan",o,void 0)},975419:o=>{t.$b("Sinh",o,void 0)},975471:o=>{t.$b("Cosh",o,void 0)},975523:o=>{t.$b("Asinh",o,void 0)},975576:o=>{t.$b("Acosh",o,void 0)},975629:o=>{t.$b("Atanh",o,void 0)},975682:o=>{t.$b("Tanh",o,void 0)},975734:o=>{t.$b("Not",o,void 0)},975785:(o,d,m)=>{t.$b("Clip",o,{min:d,max:m})},975854:o=>{t.$b("Clip",o,void 0)},975906:(o,d)=>{t.$b("Elu",o,{alpha:d})},975964:o=>{t.$b("Gelu",o,void 0)},976016:o=>{t.$b("Relu",o,void 0)}
vendor: 8,258 bytes, line 10
10,976068:(o,d)=>{t.$b("LeakyRelu",o,{alpha:d})},976132:(o,d)=>{t.$b("ThresholdedRelu",o,{alpha:d})},976202:(o,d)=>{t.$b("Cast",o,{to:d})},976260:o=>{t.$b("Add",o,void 0)},976311:o=>{t.$b("Sub",o,void 0)},976362:o=>{t.$b("Mul",o,void 0)},976413:o=>{t.$b("Div",o,void 0)},976464:o=>{t.$b("Pow",o,void 0)},976515:o=>{t.$b("Equal",o,void 0)},976568:o=>{t.$b("Greater",o,void 0)},976623:o=>{t.$b("GreaterOrEqual",o,void 0)},976685:o=>{t.$b("Less",o,void 0)},976737:o=>{t.$b("LessOrEqual",o,void 0)},976796:(o,d,m,c,$)=>{t.$b("ReduceMean",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},976971:(o,d,m,c,$)=>{t.$b("ReduceMax",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},977145:(o,d,m,c,$)=>{t.$b("ReduceMin",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},977319:(o,d,m,c,$)=>{t.$b("ReduceProd",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},977494:(o,d,m,c,$)=>{t.$b("ReduceSum",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},977668:(o,d,m,c,$)=>{t.$b("ReduceL1",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},977841:(o,d,m,c,$)=>{t.$b("ReduceL2",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},978014:(o,d,m,c,$)=>{t.$b("ReduceLogSum",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},978191:(o,d,m,c,$)=>{t.$b("ReduceSumSquare",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},978371:(o,d,m,c,$)=>{t.$b("ReduceLogSumExp",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},978551:o=>{t.$b("Where",o,void 0)},978604:(o,d,m)=>{t.$b("Transpose",o,{perm:d?Array.from((v(),R).subarray(Number(d)>>>0,Number(m)>>>0)):[]})},978728:(o,d,m,c)=>{t.$b("DepthToSpace",o,{blocksize:d,mode:ke(m),format:c?"NHWC":"NCHW"})},978861:(o,d,m,c)=>{t.$b("DepthToSpace",o,{blocksize:d,mode:ke(m),format:c?"NHWC":"NCHW"})},978994:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we,ht)=>{t.$b("ConvTranspose",o,{format:L?"NHWC":"NCHW",autoPad:d,dilations:[m],group:c,kernelShape:[$],pads:[T,z],strides:[B],wIsConst:()=>!!(v(),D)[G>>>0],outputPadding:ne?Array.from((v(),R).subarray(Number(ne)>>>0,Number(pe)>>>0)):[],outputShape:_e?Array.from((v(),R).subarray(Number(_e)>>>0,Number(we)>>>0)):[],activation:ke(ht)})},979427:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we)=>{t.$b("ConvTranspose",o,{format:B?"NHWC":"NCHW",autoPad:d,dilations:Array.from((v(),R).subarray(Number(m)>>>0,2+(Number(m)>>>0)>>>0)),group:c,kernelShape:Array.from((v(),R).subarray(Number($)>>>0,2+(Number($)>>>0)>>>0)),pads:Array.from((v(),R).subarray(Number(T)>>>0,4+(Number(T)>>>0)>>>0)),strides:Array.from((v(),R).subarray(Number(z)>>>0,2+(Number(z)>>>0)>>>0)),wIsConst:()=>!!(v(),D)[L>>>0],outputPadding:G?Array.from((v(),R).subarray(Number(G)>>>0,Number(ne)>>>0)):[],outputShape:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(_e)>>>0)):[],activation:ke(we)})},980088:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we,ht)=>{t.$b("ConvTranspose",o,{format:L?"NHWC":"NCHW",autoPad:d,dilations:[m],group:c,kernelShape:[$],pads:[T,z],strides:[B],wIsConst:()=>!!(v(),D)[G>>>0],outputPadding:ne?Array.from((v(),R).subarray(Number(ne)>>>0,Number(pe)>>>0)):[],outputShape:_e?Array.from((v(),R).subarray(Number(_e)>>>0,Number(we)>>>0)):[],activation:ke(ht)})},980521:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we)=>{t.$b("ConvTranspose",o,{format:B?"NHWC":"NCHW",autoPad:d,dilations:Array.from((v(),R).subarray(Number(m)>>>0,2+(Number(m)>>>0)>>>0)),group:c,kernelShape:Array.from((v(),R).subarray(Number($)>>>0,2+(Number($)>>>0)>>>0)),pads:Array.from((v(),R).subarray(Number(T)>>>0,4+(Number(T)>>>0)>>>0)),strides:Array.from((v(),R).subarray(Number(z)>>>0,2+(Number(z)>>>0)>>>0)),wIsConst:()=>!!(v(),D)[L>>>0],outputPadding:G?Array.from((v(),R).subarray(Number(G)>>>0,Number(ne)>>>0)):[],outputShape:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(_e)>>>0)):[],activation:ke(we)})},981182:(o,d)=>{t.$b("GlobalAveragePool",o,{format:d?"NHWC":"NCHW"})},981273:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we)=>{t.$b("AveragePool",o,{format:we?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:T?Array.from((v(),R).subarray(Number(T)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:G?Array.from((v(),R).subarray(Number(G)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(_e)>>>0)):[]})},981752:(o,d)=>{t.$b("GlobalAveragePool",o,{format:d?"NHWC":"NCHW"})},981843:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we)=>{t.$b("AveragePool",o,{format:we?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:T?Array.from((v(),R).subarray(Number(T)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:G?Array.from((v(),R).subarray(Number(G)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(_e)>>>0)):[]})},982322:(o,d)=>{t.$b("GlobalMaxPool",o,{format:d?"NHWC":"NCHW"})},982409:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we)=>{t.$b("MaxPool",o,{format:we?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:T?Array.from((v(),R).subarray(Number(T)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:G?Array.from((v(),R).subarray(Number(G)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(_e)>>>0)):[]})},982884:(o,d)=>{t.$b("GlobalMaxPool",o,{format:d?"NHWC":"NCHW"})},982971:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we)=>{t.$b("MaxPool",o,{format:we?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:T?Array.from((v(),R).subarray(Number(T)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:G?Array.from((v(),R).subarray(Number(G)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(_e)>>>0)):[]})},983446:(o,d,m,c,$)=>{t.$b("Gemm",o,{alpha:d,beta:m,transA:c,transB:$})},983550:o=>{t.$b("MatMul",o,void 0)},983604:(o,d,m,c)=>{t.$b("ArgMax",o,{keepDims:!!d,selectLastIndex:!!m,axis:c})},983712:(o,d,m,c)=>{t.$b("ArgMin",o,{keepDims:!!d,selectLastIndex:!!m,axis:c})},983820:(o,d)=>{t.$b("Softmax",o,{axis:d})},983883:(o,d)=>{t.$b("Concat",o,{axis:d})},983943:(o,d,m,c,$)=>{t.$b("Split",o,{axis:d,numOutputs:m,splitSizes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},984099:o=>{t.$b("Expand",o,void 0)},984153:(o,d)=>{t.$b("Gather",o,{axis:Number(d)})},984224:(o,d)=>{t.$b("GatherElements",o,{axis:Number(d)})},984303:(o,d)=>{t.$b("GatherND",o,{batch_dims:Number(d)})},984382:(o,d,m,c,$,T,z,B,L,G,ne)=>{t.$b("Resize",o,{antialias:d,axes:m?Array.from((v(),R).subarray(Number(m)>>>0,Number(c)>>>0)):[],coordinateTransformMode:ke($),cubicCoeffA:T,excludeOutside:z,extrapolationValue:B,keepAspectRatioPolicy:ke(L),mode:ke(G),nearestMode:ke(ne)})},984744:(o,d,m,c,$,T,z)=>{t.$b("Slice",o,{starts:d?Array.from((v(),R).subarray(Number(d)>>>0,Number(m)>>>0)):[],ends:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[],axes:T?Array.from((v(),R).subarray(Number(T)>>>0,Number(z)>>>0)):[]})},985008:o=>{t.$b("Tile",o,void 0)},985060:(o,d,m)=>{t.$b("InstanceNormalization",o,{epsilon:d,format:m?"NHWC":"NCHW"})},985174:(o,d,m)=>{t.$b("InstanceNormalization",o,{epsilon:d,format:m?"NHWC":"NCHW"})},985288:o=>{t.$b("Range",o,void 0)},985341:(o,d)=>{t.$b("Einsum",o,{equation:ke(d)})},985422:(o,d,m,c,$)=>{t.$b("Pad",o,{mode:d,value:m,pads:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},985565:(o,d,m,c,$,T)=>{t.$b("BatchNormalization",o,{epsilon:d,momentum:m,spatial:!!$,trainingMode:!!c,format:T?"NHWC":"NCHW"})},985734:(o,d,m,c,$,T)=>{t.$b("BatchNormalization",o,{epsilon:d,momentum:m,spatial:!!$,trainingMode:!!c,format:T?"NHWC":"NCHW"})},985903:(o,d,m)=>{t.$b("CumSum",o,{exclusive:Number(d),reverse:Number(m)}
vendor: 4,173 bytes, line 10
10)},986e3:(o,d,m)=>{t.$b("DequantizeLinear",o,{axis:d,blockSize:m})},986090:(o,d,m,c,$)=>{t.$b("GridSample",o,{align_corners:d,mode:ke(m),padding_mode:ke(c),format:$?"NHWC":"NCHW"})},986260:(o,d,m,c,$)=>{t.$b("GridSample",o,{align_corners:d,mode:ke(m),padding_mode:ke(c),format:$?"NHWC":"NCHW"})},986430:(o,d)=>{t.$b("ScatterND",o,{reduction:ke(d)})},986515:(o,d,m,c,$,T,z,B,L)=>{t.$b("Attention",o,{numHeads:d,isUnidirectional:m,maskFilterValue:c,scale:$,doRotary:T,qkvHiddenSizes:z?Array.from((v(),R).subarray(Number(B)>>>0,Number(B)+z>>>0)):[],pastPresentShareBuffer:!!L})},986787:o=>{t.$b("BiasAdd",o,void 0)},986842:o=>{t.$b("BiasSplitGelu",o,void 0)},986903:o=>{t.$b("FastGelu",o,void 0)},986959:(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we,ht,_i)=>{t.$b("Conv",o,{format:pe?"NHWC":"NCHW",auto_pad:d,dilations:m?Array.from((v(),R).subarray(Number(m)>>>0,Number(c)>>>0)):[],group:$,kernel_shape:T?Array.from((v(),R).subarray(Number(T)>>>0,Number(z)>>>0)):[],pads:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],strides:G?Array.from((v(),R).subarray(Number(G)>>>0,Number(ne)>>>0)):[],w_is_const:()=>!!(v(),D)[Number(_e)>>>0],activation:ke(we),activation_params:ht?Array.from((v(),Z).subarray(Number(ht)>>>0,Number(_i)>>>0)):[]})},987543:o=>{t.$b("Gelu",o,void 0)},987595:(o,d,m,c,$,T,z,B,L)=>{t.$b("GroupQueryAttention",o,{numHeads:d,kvNumHeads:m,scale:c,softcap:$,doRotary:T,rotaryInterleaved:z,smoothSoftmax:B,localWindowSize:L})},987812:(o,d,m,c)=>{t.$b("LayerNormalization",o,{axis:d,epsilon:m,simplified:!!c})},987923:(o,d,m,c)=>{t.$b("LayerNormalization",o,{axis:d,epsilon:m,simplified:!!c})},988034:(o,d,m,c,$,T)=>{t.$b("MatMulNBits",o,{k:d,n:m,accuracyLevel:c,bits:$,blockSize:T})},988161:(o,d,m,c,$,T)=>{t.$b("MultiHeadAttention",o,{numHeads:d,isUnidirectional:m,maskFilterValue:c,scale:$,doRotary:T})},988320:(o,d)=>{t.$b("QuickGelu",o,{alpha:d})},988384:(o,d,m,c,$)=>{t.$b("RotaryEmbedding",o,{interleaved:!!d,numHeads:m,rotaryEmbeddingDim:c,scale:$})},988523:(o,d,m)=>{t.$b("SkipLayerNormalization",o,{epsilon:d,simplified:!!m})},988625:(o,d,m)=>{t.$b("SkipLayerNormalization",o,{epsilon:d,simplified:!!m})},988727:(o,d,m,c)=>{t.$b("GatherBlockQuantized",o,{gatherAxis:d,quantizeAxis:m,blockSize:c})},988848:o=>{t.Fd(o)},988882:(o,d)=>t.Hd(Number(o),Number(d),t.Yc.Kd,t.Yc.errors)};function Pm(o,d,m){return Dn(async()=>{await t.Dd(Number(o),Number(d),Number(m))})}function qm(){return typeof wasmOffsetConverter<"u"}function Lm(o,d,m,c){var $=oe();try{return bs(o,d,m,c)}catch(T){if(se($),T!==T+0)throw T;le(1,0)}}function Wm(o,d,m){var c=oe();try{return ms(o,d,m)}catch($){if(se(c),$!==$+0)throw $;le(1,0)}}function Vm(o){var d=oe();try{cs(o)}catch(m){if(se(d),m!==m+0)throw m;le(1,0)}}function Gm(o,d){var m=oe();try{return mi(o,d)}catch(c){if(se(m),c!==c+0)throw c;le(1,0)}}function Hm(o,d,m){var c=oe();try{ps(o,d,m)}catch($){if(se(c),$!==$+0)throw $;le(1,0)}}function Fm(o,d){var m=oe();try{ws(o,d)}catch(c){if(se(m),c!==c+0)throw c;le(1,0)}}function jm(o,d,m,c,$,T,z){var B=oe();try{return _s(o,d,m,c,$,T,z)}catch(L){if(se(B),L!==L+0)throw L;le(1,0)}}function Km(o,d,m,c,$,T){var z=oe();try{hs(o,d,m,c,$,T)}catch(B){if(se(z),B!==B+0)throw B;le(1,0)}}function Zm(o,d,m,c){var $=oe();try{ys(o,d,m,c)}catch(T){if(se($),T!==T+0)throw T;le(1,0)}}function Qm(o,d,m,c,$){var T=oe();try{fs(o,d,m,c,$)}catch(z){if(se(T),z!==z+0)throw z;le(1,0)}}function Xm(o,d,m,c,$,T,z){var B=oe();try{vs(o,d,m,c,$,T,z)}catch(L){if(se(B),L!==L+0)throw L;le(1,0)}}function Ym(o,d,m,c,$,T,z){var B=oe();try{xs(o,d,m,c,$,T,z)}catch(L){if(se(B),L!==L+0)throw L;le(1,0)}}function Jm(o,d,m,c,$,T,z,B){var L=oe();try{Is(o,d,m,c,$,T,z,B)}catch(G){if(se(L),G!==G+0)throw G;le(1,0)}}function eg(o,d,m,c,$){var T=oe();try{return $s(o,d,m,c,$)}catch(z){if(se(T),z!==z+0)throw z;le(1,0)}}function tg(o,d,m){var c=oe();try{return Es(o,d,m)}catch($){if(se(c),$!==$+0)throw $;le(1,0)}}function rg(o,d,m,c,$,T,z,B){var L=oe();try{zs(o,d,m,c,$,T,z,B)}catch(G){if(se(L),G!==G+0)throw G;le(1,0)}}function ig(o,d,m,c,$,T,z,B,L,G,ne,pe){var _e=oe();try{Ss(o,d,m,c,$,T,z,B,L,G,ne,pe)}catch(we){if(se(_e),we!==we+0)throw we;le(1,0)}}function ag(o,d,m,c,$,T){var z=oe();try{return ks(o,d,m,c,$,T)}
10catch(B){if(se(z),B!==B+0)throw B;le(1,0)}}function ng(o,d,m){var c=oe();try{return Cs(o,d,m)}catch($){if(se(c),$!==$+0)throw $;return le(1,0),0n}}function sg(o,d,m,c,$,T,z,B,L){var G=oe();try{gs(o,d,m,c,$,T,z,B,L)}catch(ne){if(se(G),ne!==ne+0)throw ne;le(1,0)}}function og(o){var d=oe();try{return As(o)}catch(m){if(se(d),m!==m+0)throw m;le(1,0)}}function ug(o,d){var m=oe();try{return Fs(o,d)}catch(c){if(se(m),c!==c+0)throw c;return le(1,0),0n}}function lg(o){var d=oe();try{return Os(o)}catch(m){if(se(d),m!==m+0)throw m;return le(1,0),0n}}function dg(o,d,m,c){var $=oe();try{return Us(o,d,m,c)}catch(T){if(se($),T!==T+0)throw T;le(1,0)}}function pg(o,d,m,c,$){var T=oe();try{return Ps(o,d,m,c,$)}catch(z){if(se(T),z!==z+0)throw z;le(1,0)}}function cg(o,d,m,c,$,T){var z=oe();try{return qs(o,d,m,c,$,T)}catch(B){if(se(z),B!==B+0)throw B;le(1,0)}}function hg(o,d,m,c,$,T){var z=oe();try{return Ls(o,d,m,c,$,T)}catch(B){if(se(z),B!==B+0)throw B;le(1,0)}}function fg(o,d,m,c,$,T,z,B){var L=oe();try{return Ts(o,d,m,c,$,T,z,B)}catch(G){if(se(L),G!==G+0)throw G;le(1,0)}}function mg(o,d,m,c,$){var T=oe();try{return Ws(o,d,m,c,$)}catch(z){if(se(T),z!==z+0)throw z;return le(1,0),0n}}function gg(o,d,m,c){var $=oe();try{return Vs(o,d,m,c)}catch(T){if(se($),T!==T+0)throw T;le(1,0)}}function _g(o,d,m,c){var $=oe();try{return Gs(o,d,m,c)}catch(T){if(se($),T!==T+0)throw T;le(1,0)}}function yg(o,d,m,c,$,T,z,B,L,G,ne,pe){var _e=oe();try{return Hs(o,d,m,c,$,T,z,B,L,G,ne,pe)}catch(we){if(se(_e),we!==we+0)throw we;le(1,0)}}function bg(o,d,m,c,$,T,z,B,L,G,ne){var pe=oe();try{Ms(o,d,m,c,$,T,z,B,L,G,ne)}catch(_e){if(se(pe),_e!==_e+0)throw _e;le(1,0)}}function wg(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we,ht,_i){var Sg=oe();try{Ds(o,d,m,c,$,T,z,B,L,G,ne,pe,_e,we,ht,_i)}catch(yi){if(se(Sg),yi!==yi+0)throw yi;le(1,0)}}function $g(o,d,m){var c=oe();try{return Rs(o,d,m)}catch($){if(se(c),$!==$+0)throw $;le(1,0)}}function vg(o,d,m){var c=oe();try{return Bs(o,d,m)}catch($){if(se(c),$!==$+0)throw $;le(1,0)}}function xg(o,d,m,c){var $=oe();try{Ns(o,d,m,c)}catch(T){if(se($),T!==T+0)throw T;le(1,0)}}function Tr(){if(0<Me)Be=Tr;else if(a)b==null||b(t),Q();else{for(var o=Re;0<o.length;)o.shift()(t);0<Me?Be=Tr:(t.calledRun=!0,C||(Q(),b==null||b(t)))}}return a||(at=await Le(),Tr()),t.PTR_SIZE=4,P?t:new Promise((o,d)=>{b=o,S=d})}var tp,ro,jg=U(()=>{var e,t;tp=to,ro=(t=(e=globalThis.self)==null?void 0:e.name)==null?void 0:t.startsWith("em-pthread"),ro&&to()}),Si,ma,io,Ue,rp,Er,ao,no,ki,so,Ti,ip,Ii,ap,Ba=U(()=>{Ra(),Si=typeof location>"u"?void 0:location.origin,ma=import.meta.url>"file:"&&import.meta.url<"file;",io=()=>{{if(ma){let e=URL;return new URL(new e("ort.bundle.min.mjs",import.meta.url).href,Si).href}return import.meta.url}},Ue=io(),rp=()=>{if(Ue&&!Ue.startsWith("blob:"))return Ue.substring(0,Ue.lastIndexOf("/")+1)},Er=(e,t)=>
vendor: 4,112 bytes, line 10
10{try{let r=t??Ue;return(r?new URL(e,r):new URL(e)).origin===Si}catch{return!1}},ao=(e,t)=>{let r=t??Ue;try{return(r?new URL(e,r):new URL(e)).href}catch{return}},no=(e,t)=>`${t??"./"}${e}`,ki=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},so=async e=>(await import(e)).default,Ti=(Fg(),cr(Yd)).default,ip=async()=>{if(!Ue)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(Er(Ue))return[void 0,Ti()];let e=await ki(Ue);return[e,Ti(e)]},Ii=(jg(),cr(ep)).default,ap=async(e,t,r,i)=>{let a=Ii&&!(e||t);if(a)if(Ue)a=Er(Ue)||i&&!r;else if(i&&!r)a=!0;else throw new Error("cannot determine the script source URL.");if(a)return[void 0,Ii];{let n="ort-wasm-simd-threaded.jsep.mjs",s=e??ao(n,t),u=r&&s&&!Er(s,t),l=u?await ki(s):s??no(n,t);return[u?l:void 0,await so(l)]}}}),Ei,zr,Yt,zi,oo,uo,lo,Na,ye,Nt=U(()=>{Ba(),zr=!1,Yt=!1,zi=!1,oo=()=>{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}},uo=()=>{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}},lo=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},Na=async e=>{if(zr)return Promise.resolve();if(Yt)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(zi)throw new Error("previous call to 'initializeWebAssembly()' failed.");Yt=!0;let t=e.initTimeout,r=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!lo())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!uo())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let i=oo();r>1&&!i&&(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 a=e.wasmPaths,n=typeof a=="string"?a:void 0,s=a==null?void 0:a.mjs,u=(s==null?void 0:s.href)??s,l=a==null?void 0:a.wasm,p=(l==null?void 0:l.href)??l,f=e.wasmBinary,[h,g]=await ap(u,n,r>1,!!f||!!p),_=!1,y=[];if(t>0&&y.push(new Promise(b=>{setTimeout(()=>{_=!0,b()},t)})),y.push(new Promise((b,S)=>{let x={numThreads:r};if(f)x.wasmBinary=f,x.locateFile=w=>w;else if(p||n)x.locateFile=w=>p??n+w;else if(u&&u.indexOf("blob:")!==0)x.locateFile=w=>new URL(w,u).href;else if(h){let w=rp();w&&(x.locateFile=I=>w+I)}g(x).then(w=>{Yt=!1,zr=!0,Ei=w,b(),h&&URL.revokeObjectURL(h)},w=>{Yt=!1,zi=!0,S(w)})})),await Promise.race(y),_)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},ye=()=>{if(zr&&Ei)return Ei;throw new Error("WebAssembly is not initialized yet.")}}),Qe,Vr,ge,Ma=U(()=>{Nt(),Qe=(e,t)=>{let r=ye(),i=r.lengthBytesUTF8(e)+1,a=r._malloc(i);return r.stringToUTF8(e,a,i),t.push(a),a},Vr=(e,t,r,i)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([a,n])=>{let s=t?t+a:a;if(typeof n=="object")Vr(n,s+".",r,i);else if(typeof n=="string"||typeof n=="number")i(s,n.toString());else if(typeof n=="boolean")i(s,n?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof n}`)})},ge=e=>{let t=ye(),r=t.stackSave();try{let i=t.PTR_SIZE,a=t.stackAlloc(2*i);t._OrtGetLastError(a,a+i);let n=Number(t.getValue(a,i===4?"i32":"i64")),s=t.getValue(a+i,"*"),u=s?t.UTF8ToString(s):"";throw new Error(`${e} ERROR_CODE: ${n}, ERROR_MESSAGE: ${u}`)}finally{t.stackRestore(r)}}}),np,Kg=U(()=>{Nt(),Ma(),np=e=>{let t=ye(),r=0,i=[],a=e||{};
10try{if((e==null?void 0:e.logSeverityLevel)===void 0)a.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if((e==null?void 0:e.logVerbosityLevel)===void 0)a.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);(e==null?void 0:e.terminate)===void 0&&(a.terminate=!1);let n=0;return(e==null?void 0:e.tag)!==void 0&&(n=Qe(e.tag,i)),r=t._OrtCreateRunOptions(a.logSeverityLevel,a.logVerbosityLevel,!!a.terminate,n),r===0&&ge("Can't create run options."),(e==null?void 0:e.extra)!==void 0&&Vr(e.extra,"",new WeakSet,(s,u)=>{let l=Qe(s,i),p=Qe(u,i);t._OrtAddRunConfigEntry(r,l,p)!==0&&ge(`Can't set a run config entry: ${s} - ${u}.`)}),[r,i]}catch(n){throw r!==0&&t._OrtReleaseRunOptions(r),i.forEach(s=>t._free(s)),n}}}),po,co,ho,vt,fo,sp,Zg=U(()=>{Nt(),Ma(),po=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},co=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},ho=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)},vt=(e,t,r,i)=>{let a=Qe(t,i),n=Qe(r,i);ye()._OrtAddSessionConfigEntry(e,a,n)!==0&&ge(`Can't set a session config entry: ${t} - ${r}.`)},fo=async(e,t,r)=>{let i=t.executionProviders;for(let a of i){let n=typeof a=="string"?a:a.name,s=[];switch(n){case"webnn":if(n="WEBNN",vt(e,"session.disable_quant_qdq","1",r),vt(e,"session.disable_qdq_constant_folding","1",r),typeof a!="string"){let h=a==null?void 0:a.deviceType;h&&vt(e,"deviceType",h,r)}break;case"webgpu":if(n="JS",typeof a!="string"){let h=a;if(h!=null&&h.preferredLayout){if(h.preferredLayout!=="NCHW"&&h.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${h.preferredLayout}`);vt(e,"preferredLayout",h.preferredLayout,r)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${n}`)}let u=Qe(n,r),l=s.length,p=0,f=0;if(l>0){p=ye()._malloc(l*ye().PTR_SIZE),r.push(p),f=ye()._malloc(l*ye().PTR_SIZE),r.push(f);for(let h=0;h<l;h++)ye().setValue(p+h*ye().PTR_SIZE,s[h][0],"*"),ye().setValue(f+h*ye().PTR_SIZE,s[h][1],"*")}await ye()._OrtAppendExecutionProvider(e,u,p,f,l)!==0&&ge(`Can't append execution provider: ${n}.`)}},sp=async e=>{let t=ye(),r=0,i=[],a=e||{};ho(a);try{let n=po(a.graphOptimizationLevel??"all"),s=co(a.executionMode??"sequential"),u=typeof a.logId=="string"?Qe(a.logId,i):0,l=a.logSeverityLevel??2;if(!Number.isInteger(l)||l<0||l>4)throw new Error(`log severity level is not valid: ${l}`);let p=a.logVerbosityLevel??0;if(!Number.isInteger(p)||p<0||p>4)throw new Error(`log verbosity level is not valid: ${p}`);let f=typeof a.optimizedModelFilePath=="string"?Qe(a.optimizedModelFilePath,i):0;if(r=t._OrtCreateSessionOptions(n,!!a.enableCpuMemArena,!!a.enableMemPattern,s,!!a.enableProfiling,0,u,l,p,f),r===0&&ge("Can't create session options."),a.executionProviders&&await fo(r,a,i),a.enableGraphCapture!==void 0){if(typeof a.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${a.enableGraphCapture}`);vt(r,"enableGraphCapture",a.enableGraphCapture.toString(),i)}if(a.freeDimensionOverrides)for(let[h,g]of Object.entries(a.freeDimensionOverrides)){if(typeof h!="string")throw new Error(`free dimension override name must be a string: ${h}`);if(typeof g!="number"||!Number.isInteger(g)||g<0)throw new Error(`free dimension override value must be a non-negative integer: ${g}`);let _=Qe(h,i);t._OrtAddFreeDimensionOverride(r,_,g)!==0&&ge(`Can't set a free dimension override: ${h} - ${g}.`)}return a.extra!==void 0&&Vr(a.extra,"",new WeakSet,(h,g)=>{vt(r,h,g,i)}),[r,i]}catch(n){throw r!==0&&t._OrtReleaseSessionOptions(r)!==0&&ge("Can't release session options."),i.forEac
vendor: 4,843 bytes, line 10
10h(s=>t._free(s)),n}}}),Et,st,zt,Qr,Gr,Da,Ua,ga,te=U(()=>{Et=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}`)}},st=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},zt=(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],i=typeof t=="number"?t:t.reduce((a,n)=>a*n,1);return r>0?Math.ceil(i*r):void 0},Qr=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}`)}},Gr=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}`)}},Da=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Ua=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",ga=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),Pa,op=U(()=>{Ra(),Pa=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),i=r?parseInt(r,10):0;if(i<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let a=t.body.getReader(),n;try{n=new ArrayBuffer(i)}catch(u){if(u instanceof RangeError){let l=Math.ceil(i/65536);n=new WebAssembly.Memory({initial:l,maximum:l}).buffer}else throw u}let s=0;for(;;){let{done:u,value:l}=await a.read();if(u)break;let p=l.byteLength;new Uint8Array(n,s,p).set(l),s+=p}return new Uint8Array(n,0,i)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),mo,go,_o,yo,qa,bo,de,ot=U(()=>{te(),mo=["V","I","W","E","F"],go=(e,t)=>{console.log(`[${mo[e]},${new Date().toISOString()}]${t}`)},qa=(e,t)=>{_o=e,yo=t},bo=(e,t)=>{let r=Gr(e),i=Gr(_o);r>=i&&go(r,typeof t=="function"?t():t)},de=(...e)=>{yo&&bo(...e)}}),wo,Vt,O,Hr,up,lp,dp,ie=U(()=>{wo=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},Vt=class{static calcShape(e,t,r=!1){let i=e.length,a=t.length;if(i===0)return t;if(a===0)return e;let n=Math.max(e.length,t.length),s=new Array(n);if(r){if(i<2||a<2)return;let u=wo.calcMatMulShape([e[i-2],e[i-1]],[t[a-2],t[a-1]]);if(u===void 0)return;[s[n-2],s[n-1]]=u}for(let u=r?3:1;u<=n;u++){let l=i-u<0?1:e[i-u],p=a-u<0?1:t[a-u];if(l!==p&&l>1&&p>1)return;let f=Math.max(l,p);if(l&&p)s[n-u]=Math.max(l,p);else{if(f>1)return;s[n-u]=0}}return s}static isValidBroadcast(e,t){let r=e.length,i=t.length;if(r>i)return!1;for(let a=1;a<=r;a++)if(e[r-a]!==1&&e[r-a]!==t[i-a])return!1;return!0}},O=class qr{static size(t){return qr.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let i=t.length;if(i===0)return[];let a=new Array(i),n=i-1;for(;n>=0;){if(t[n]%r===0){a[n]=t[n]/r;break}if(r%t[n]!==0)throw new Error("cannot convert shape");a[n]=1,r/=t[n],n--}for(n--;n>=0;n--)a[n]=t[n];return a}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 qr.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 qr.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,i){let a=1;for(let n=r;n<i;n++){if(t[n]<0)throw new Error("cannot get valid size from specified dimension range. Most likely the range contains 
10negative values in them.");a*=Number(t[n])}return a}static computeStrides(t){let r=t.length;if(r===0)return[];if(r===1)return[1];let i=new Array(r);i[r-1]=1,i[r-2]=t[r-1];for(let a=r-3;a>=0;--a)i[a]=i[a+1]*t[a+1];return i}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(i=>this.normalizeAxis(i,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(i=>t[i]):t.slice().reverse()}static padShape(t,r){let i=t.length;return t.map((a,n)=>a+r[n]+r[n+i])}static areEqual(t,r){return t.length!==r.length?!1:t.every((i,a)=>i===r[a])}},Hr=class ur{static adjustPoolAttributes(t,r,i,a,n,s){if(!t&&i.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 u=0;u<r.length-2;u++)u>=i.length?i.push(r[u+2]):i[u]=r[u+2];for(let u=0;u<i.length;u++)if(u<a.length){if(a[u]<0)throw new Error("strides should be greater than or equal to 1")}else a.push(1);for(let u=0;u<i.length;u++)if(u<n.length){if(n[u]<0)throw new Error("dilations should be greater than or equal to 1")}else n.push(1);for(let u=0;u<i.length*2;u++)if(u<s.length){if(s[u]<0)throw new Error("pad should be greater than or equal to 1")}else s.push(0);for(let u=0;u<i.length;u++){if(i[u]<=0)throw new Error("kernel shapes need to be greater than 0");if(s[u]>=i[u]||s[u+i.length]>=i[u])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,i,a,n,s,u){if(u){if(n.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(a.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let l=0;l<t.length-2;l++)ur.adjustPadAndReturnShape(t[l+(s?1:2)],r[l],i[l],a[l],n,l,l+t.length-2,u)}}static computePoolOutputShape(t,r,i,a,n,s,u){if(r.length<=0)throw new Error("input shape must be of size greater than 0");let l=[r[0],r[1]];return ur.computeShapeHelper(t,r,l,i,a,n,s,u),l}static computeConvOutputShape(t,r,i,a,n,s,u){if(t.length<=0||r.length<=0)throw new Error("invalid input tensor dims or invalid filter tensor dims");let l=[t[0],r[0]];return ur.computeShapeHelper(!1,t,l,i,a,n,s,u),l}static computeShapeHelper(t,r,i,a,n,s,u,l){if(t)for(let p=0;p<r.length-2;p++)i.push(1);else for(let p=0;p<r.length-2;p++)i.push(ur.adjustPadAndReturnShape(r[p+2],a[p],n[p],s[p],u,p,p+r.length-2,l))}static adjustPadAndReturnShape(t,r,i,a,n,s,u,l){let p=i*(a-1)+1;if(l&&l!=="NOTSET")switch(l){case"VALID":return n[s]=0,n[u]=0,Math.floor((t-p)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(i!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let f=((t+r-1)/r-1)*r+a-t;return n[s]=Math.floor(l==="SAME_LOWER"?(f+1)/2:f/2),n[u]=f-n[s],Math.floor((t+f-a)/r+1)}default:throw new Error("Unsupported AutoPad type")}else return Math.floor((t+n[s]+n[u]-p)/r+1)}},up=class{static getShapeOfGemmResult(e,t,r,i,a){if(e.length!==2||r.length!==2)throw new Error("shape need to be of size 2");let n,s,u;t?(n=e[1],s=e[0]):(n=e[0],s=e[1]);let l=-1;if(i?(u=r[0],l=1):(u=r[1],l=0),r[l]!==s)throw new Error("dimension mismatch");if(n<=0||u<=0||s<=0)throw new Error("invalid shape specified");if(a&&!Vt.isValidBroadcast(a,[n,u]))throw new Error("gemm: invalid bias shape for broadcast");return[n,u,s]}},lp=-34028234663852886e22,dp=34028234663852886e22}),La,pp=U(()=>{te(),La=(e,t)=>new(Qr(t))(e)}),Ci,_a,Ai,$o,Oi,vo,Ri,Bi,Ni,xo,cp,Qg=U(()=>{te(),ot(),Ci=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),_a=(e,t)=>{if(t==="int32")return e;let r=Ci.get(t);if(!r)throw new Error(`WebNN backend does not support data type: ${t}`);let i=r/8;if(e.byteLength%i!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${i}.`);let a=e.byteLength/i,n=new(Qr(t))(e.buffer,e.byteOffset,a);switch(t){case"int64":case"uint64":{let s=new Int32Array(a);for(let u=0;u<a;u++){let l=n[u];if(l>2147483647n||l<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");s[u]=Number(l)}return new Uint8Array(s.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&n.some(u=>u>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let s=Int32Array.from(n,Number);return new Uint8Array(s.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},Ai=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let r=e.byteLength/4,i=new Int32Array(e.buffer,e.byteOffset,r);switch(t){case"int64":{let a=BigInt64Array.from(i,BigInt);return new Uint8Array(a.buffer)}case"uint64":{if(i.some(n=>n<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let a=BigUint64Array.from(i,BigInt);return new Uint8Array(a.buffer)}
10case"int8":{if(i.some(n=>n<-128||n>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let a=Int8Array.from(i,Number);return new Uint8Array(a.buffer)}case"uint8":{if(i.some(a=>a<0||a>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(i,Number)}case"uint32":{if(i.some(n=>n<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let a=Uint32Array.from(i,Number);return new Uint8Array(a.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},$o=1,Oi=()=>$o++,vo=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),Ri=(e,t)=>{let r=Ci.get(e);if(!r)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((i,a)=>i*a)*r/8):0},Bi=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:r,tensor:i,dataType:a,shape:n,fallbackDataType:s}=e;this.sessionId=t,this.mlContext=r,this.mlTensor=i,this.dataType=a,this.tensorShape=n,this.fallbackDataType=s}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return Ri(this.dataType,this.tensorShape)}destroy(){de("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),r=Ai(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(r);return}else return r.buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,r){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===r.length&&this.tensorShape.every((i,a)=>i===r[a])}setIsDataConverted(e){this.isDataConverted=e}},Ni=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(e,t,r,i){let a=this.tensorManager.getMLContext(e),n=this.tensorManager.getMLOpSupportLimits(e),s;if(!(n!=null&&n.input.dataTypes.includes(t))){if(s=vo.get(t),!s||(n==null?void 0:n.input.dataTypes.includes(s)))throw new Error(`WebNN backend does not support data type: ${t}`);de("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${s}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(a,t,r))return this.wrapper.tensor;if(i){if(this.wrapper.byteLength!==Ri(t,r))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let u=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,r,u,!0,!0,s),i&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=_a(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else de("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){var t,r;if(this.activeUpload){let i=(t=this.wrapper)!=null&&t.isDataConverted?Ai(this.activeUpload,(r=this.wrapper)==null?void 0:r.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(i):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(i);return}else return i.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},xo=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=Oi();return this.tensorTrackersById.set(e,new Ni(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,r,i,a){de("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${r}, shape: ${i}, copyOld: ${a}}`);let n=this.tensorTrackersById.get(t);if(!n)throw new Error("Tensor not found.");return n.ensureTensor(e,r,i,a)}upload(e,t){let r=this.tensorTrackersById.get(e);if(!r)throw new Error("Tensor not found.");r.upload(t)}async download(e,t){de("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t==null?void 0:t.byteLength}}`);let r=this.tensorTrackersById.get(e);if(!r)throw new Error("Tensor not found.");return r.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}
vendor: 5,214 bytes, line 10
10registerTensor(e,t,r,i){let a=this.getMLContext(e),n=Oi(),s=new Bi({sessionId:e,context:a,tensor:t,dataType:r,shape:i});return this.tensorTrackersById.set(n,new Ni(this,s)),this.externalTensors.add(s),n}async getCachedTensor(e,t,r,i,a,n,s){let u=this.getMLContext(e);for(let[p,f]of this.freeTensors.entries())if(f.canReuseTensor(u,t,r)){de("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${s?`fallbackDataType: ${s},`:""} shape: ${r}`);let h=this.freeTensors.splice(p,1)[0];return h.sessionId=e,h}de("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${s?`fallbackDataType: ${s},`:""} shape: ${r}}`);let l=await u.createTensor({dataType:s??t,shape:r,dimensions:r,usage:i,writable:a,readable:n});return new Bi({sessionId:e,context:u,tensor:l,dataType:t,shape:r,fallbackDataType:s})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},cp=(...e)=>new xo(...e)}),Jt,So,hp,Xg=U(()=>{te(),Nt(),pp(),Qg(),ot(),Jt=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),So=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let r=Object.keys(e).sort(),i=Object.keys(t).sort();return r.length===i.length&&r.every((a,n)=>a===i[n]&&e[a]===t[a])},hp=class{constructor(e){this.tensorManager=cp(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,qa(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){de("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){de("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let r of t)de("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${r}}`),this.tensorManager.releaseTensorId(r);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let r=this.mlContextCache.findIndex(i=>i.gpuDevice===e);if(r!==-1)return this.mlContextCache[r].mlContext;{let i=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:i}),i}}else if(e===void 0){let r=this.mlContextCache.findIndex(i=>i.options===void 0&&i.gpuDevice===void 0);if(r!==-1)return this.mlContextCache[r].mlContext;{let i=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:i}),i}}let t=this.mlContextCache.findIndex(r=>So(r.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let r=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:r}),r}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let r=this.sessionIdsByMLContext.get(t);r||(r=new Set,this.sessionIdsByMLContext.set(t,r)),r.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let r=this.sessionIdsByMLContext.get(t);if(r.delete(e),r.size===0){this.sessionIdsByMLContext.delete(t);let i=this.mlContextCache.findIndex(a=>a.mlContext===t);i!==-1&&this.mlContextCache.splice(i,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){de("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,r,i,a){let n=Jt.get(r);if(!n)throw new Error(`Unsupported ONNX data type: ${r}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,n,i,a)}async createTemporaryTensor(e,t,r){de("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${r}}`);let i=Jt.get(t);if(!i)throw new Error(`Unsupported ONNX data type: ${t}`);let a=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,a,i,r,!1);let n=this.temporarySessionTensorIds.get(e);return n?n.push(a):this.temporarySessionTensorIds.set(e,[a]),a}uploadTensor(e,t){if(!ye().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");de("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let r=await this.tensorManager.download(e);return La(r,t)}}
10registerMLTensor(e,t,r,i){let a=Jt.get(r);if(!a)throw new Error(`Unsupported ONNX data type: ${r}`);let n=this.tensorManager.registerTensor(e,t,a,i);return de("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${a}, dimensions: ${i}} -> {tensorId: ${n}}`),n}registerMLConstant(e,t,r,i,a,n,s=!1){if(!n)throw new Error("External mounted files are not available.");let u=e;e.startsWith("./")&&(u=e.substring(2));let l=n.get(u);if(!l)throw new Error(`File with name ${u} not found in preloaded files.`);if(t+r>l.byteLength)throw new Error("Out of bounds: data offset and length exceed the external file data size.");let p=l.slice(t,t+r).buffer,f;switch(a.dataType){case"float32":f=new Float32Array(p);break;case"float16":f=typeof Float16Array<"u"&&Float16Array.from?new Float16Array(p):new Uint16Array(p);break;case"int32":f=new Int32Array(p);break;case"uint32":f=new Uint32Array(p);break;case"int64":if(s){let h=_a(new Uint8Array(p),"int64");f=new Int32Array(h.buffer),a.dataType="int32"}else f=new BigInt64Array(p);break;case"uint64":f=new BigUint64Array(p);break;case"int8":f=new Int8Array(p);break;case"int4":case"uint4":case"uint8":f=new Uint8Array(p);break;default:throw new Error(`Unsupported data type: ${a.dataType} in creating WebNN Constant from external data.`)}return de("verbose",()=>`[WebNN] registerMLConstant {dataType: ${a.dataType}, shape: ${a.shape}}} ${s?"(Note: it was int64 data type and registered to int32 as workaround)":""}`),i.constant(a,f)}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let r=this.sessionGraphInputs.get(e);return r?r.includes(t):!1}isGraphOutput(e,t){let r=this.sessionGraphOutputs.get(e);return r?r.includes(t):!1}isGraphInputOutputTypeSupported(e,t,r=!0){let i=Jt.get(Et(t)),a=this.mlOpSupportLimitsBySessionId.get(e);return typeof i>"u"?!1:r?!!(a!=null&&a.input.dataTypes.includes(i)):!!(a!=null&&a.output.dataTypes.includes(i))}flush(){}}}),Wa=U(()=>{}),Mi,Cr,Ar,ko,To,Di,ya,Io,fp,Yg=U(()=>{ot(),Wa(),Mi=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]]),Cr=[],Ar=e=>Math.ceil(Number(e)/16)*16,ko=e=>{for(let t=0;t<Cr.length;t++){let r=Cr[t];if(e<=r)return r}return Math.ceil(e/16)*16},To=1,Di=()=>To++,ya=async(e,t,r,i)=>{let a=Ar(r),n=e.device.createBuffer({size:a,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let s=e.getCommandEncoder();e.endComputePass(),s.copyBufferToBuffer(t,0,n,0,a),e.flush(),await n.mapAsync(GPUMapMode.READ);let u=n.getMappedRange();if(i){let l=i();return l.set(new Uint8Array(u,0,r)),l}else return new Uint8Array(u.slice(0,r))}finally{n.destroy()}},Io=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of Mi)Cr.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let r=t.buffer,i=t.byteOffset,a=t.byteLength,n=Ar(a),s=this.storageCache.get(e);if(!s)throw new Error("gpu data for uploading does not exist");if(Number(s.originalSize)!==a)throw new Error(`inconsistent data size. gpu data size=${s.originalSize}, data size=${a}`);let u=this.backend.device.createBuffer({mappedAtCreation:!0,size:n,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),l=u.getMappedRange();new Uint8Array(l).set(new Uint8Array(r,i,a)),u.unmap();let p=this.backend.device.createCommandEncoder();p.copyBufferToBuffer(u,0,s.gpuData.buffer,0,n),this.backend.device.queue.submit([p.finish()]),u.destroy(),de("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let r=this.storageCache.get(e);if(!r)throw new Error("source gpu data for memcpy does not exist");let i=this.storageCache.get(t);if(!i)throw new Error("destination gpu data for memcpy does not exist");if(r.originalSize!==i.originalSize)throw new Error("inconsistent source and destination gpu data size");let a=Ar(r.originalSize),n=this.backend.getCommandEncoder();this.backend.endComputePass(),n.copyBufferToBuffer(r.gpuData.buffer,0,i.gpuData.buffer,0,a)}registerExternalBuffer(e,t,r){let i;if(r){if(i=r[0],e===r[1])return de("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, buffer is the same, skip.`),i;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet.
11             Please use the previous external buffer!`)}else i=Di();return this.storageCache.set(i,{gpuData:{id:i,type:0,buffer:e},originalSize:t}),de("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, registered.`),i}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),de("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let r=ko(e),i,a=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,n=(t&GPUBufferUsage.UNIFORM)===GP
vendor: 40,350 bytes, lines 11-289
11UBufferUsage.UNIFORM;if(a||n){let u=(a?this.freeBuffers:this.freeUniformBuffers).get(r);u?u.length>0?i=u.pop():i=this.backend.device.createBuffer({size:r,usage:t}):i=this.backend.device.createBuffer({size:r,usage:t})}else i=this.backend.device.createBuffer({size:r,usage:t});let s={id:Di(),type:0,buffer:i};return this.storageCache.set(s.id,{gpuData:s,originalSize:Number(e)}),de("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${s.id}`),s}get(e){var t;return(t=this.storageCache.get(e))==null?void 0:t.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,r=this.storageCache.get(t);if(!r){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return de("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(e,t){let r=this.storageCache.get(Number(e));if(!r)throw new Error("data does not exist");await ya(this.backend,r.gpuData.buffer,r.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=Mi.get(e.size);if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let r=this.freeBuffers.get(e.size)||[];t===void 0||r.length>=t?e.destroy():r.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let r=this.freeUniformBuffers.get(e.size)||[];t===void 0||r.length>=t?e.destroy():r.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(r=>{r.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(de("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(r=>{r.gpuData.buffer.destroy()}),this.storageCache=new Map)}},fp=(...e)=>new Io(...e)}),Eo,fe,xe=U(()=>{Eo=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},fe=e=>new Eo(e)}),Gt,Or,Te,Ce,J,ve,ba,Wt,_t,Y,er,N,X,mp,Va,zo,gp,ae=U(()=>{te(),ie(),Gt=64,Or=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}<f16>`:"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>"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},Te=(e,t=1)=>{let r=Or(e,t);return typeof r=="string"?r:r[0]},Ce=(e,t=1)=>{let r=Or(e,t);return typeof r=="string"?r:r[1]},J=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:O.computeStrides(r)})}),t},ve=e=>e%4===0?4:e%2===0?2:1,ba=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,Wt=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}<f32>(${r})`,_t=(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,Y=(e,t,r,i)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?i==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:i==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,er=(e,t,r,i,a)=>{let n=typeof r=="number",s=n?r:r.length,u=[...new Array(s).keys()],l=s<2?"u32":s<=4?`vec${s}<u32>`:`array<u32, ${s}>`,p=Or(t,a),f=typeof p=="string"?p:p[1],h=typeof p=="string"?p:p[0],g={indices:l,value:f,storage:h,tensor:t},_=P=>typeof P=="string"?P:`${P}u`,y={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},b=n?"uniforms.":"",S=`${b}${e}_shape`,x=`${b}${e}_strides`,w="";for(let P=0;P<s-1;P++)w+=`
12    let dim${P} = current / ${Y(x,P,s)};
13    let rest${P} = current % ${Y(x,P,s)};
14    indices[${P}] = dim${P};
15    current = rest${P};
16    `;w+=`indices[${s-1}] = current;`;let I=s<2?"":`
17  fn o2i_${e}(offset: u32) -> ${g.indices} {
18    var indices: ${g.indices};
19    var current = offset;
20    ${w}
21    return indices;
22  }`,k=P=>(y.offsetToIndices=!0,s<2?P:`o2i_${e}(${P})`),E=[];if(s>=2)for(let P=s-1;P>=0;P--)E.push(`${Y(x,P,s)} * (indices[${P}])`);let C=s<2?"":`
23  fn i2o_${e}(indices: ${g.indices}) -> u32 {
24    return ${E.join("+")};
25  }`,A=P=>(y.indicesToOffset=!0,s<2?P:`i2o_${e}(${P})`),v=(...P)=>s===0?"0u":`${g.indices}(${P.map(_).join(",")})`,M=(P,V)=>s<2?`${P}`:`${Y(P,V,s)}`,D=(P,V,Q)=>s<2?`${P}=${Q};`:`${Y(P,V,s)}=${Q};`,H={},F=(P,V)=>{y.broadcastedIndicesToOffset=!0;let Q=`${V.name}broadcastedIndicesTo${e}Offset`;if(Q in H)return`${Q}(${P})`;let q=[];for(let me=s-1;me>=0;me--){let Le=V.indicesGet("outputIndices",me+V.rank-s);q.push(`${M(x,me)} * (${Le} % ${M(S,me)})`)}return H[Q]=`fn ${Q}(outputIndices: ${V.type.indices}) -> u32 {
26             return ${q.length>0?q.join("+"):"0u"};
27           }`,`${Q}(${P})`},j=(P,V)=>(()=>{if(g.storage===g.value)return`${e}[${P}]=${V};`;if(g.storage==="vec2<u32>"&&g.value==="i32")return`${e}[${P}]=vec2<u32>(u32(${V}), select(0u, 0xFFFFFFFFu, ${V} < 0));`;if(g.storage==="vec2<u32>"&&g.value==="u32")return`${e}[${P}]=vec2<u32>(u32(${V}), 0u);`;if(g.storage==="u32"&&g.value==="vec4<bool>")return`${e}[${P}]=dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(${V}));`;throw new Error(`not supported combination of storage type ${g.storage} and value type ${g.value} yet`)})(),R=P=>(()=>{if(g.storage===g.value)return`${e}[${P}]`;if(g.storage==="vec2<u32>"&&g.value==="i32")return`i32(${e}[${P}].x)`;if(g.storage==="vec2<u32>"&&g.value==="u32")return`u32(${e}[${P}].x)`;if(g.storage==="u32"&&g.value==="vec4<bool>")return`vec4<bool>(bool(${e}[${P}] & 0xFFu), bool(${e}[${P}] & 0xFF00u), bool(${e}[${P}] & 0xFF0000u), bool(${e}[${P}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${g.storage} and value type ${g.value} yet`)})(),K=s<2?"":`
28  fn get_${e}ByIndices(indices: ${g.indices}) -> ${f} {
29    return ${R(`i2o_${e}(indices)`)};
30  }`,Z=s<2?"":(()=>{let P=u.map(Q=>`d${Q}: u32`).join(", "),V=u.map(Q=>`d${Q}`).join(", ");return`
31  fn get_${e}(${P}) -> ${f} {
32    return get_${e}ByIndices(${v(V)});
33  }`})(),ee=(...P)=>{if(P.length!==s)throw new Error(`indices length must be ${s}`);let V=P.map(_).join(",");return s===0?R("0u"):s===1?R(V[0]):(y.get=!0,y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}(${V})`)},he=P=>s<2?R(P):(y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}ByIndices(${P})`),W=s<2?"":`
34  fn set_${e}ByIndices(indices: ${g.indices}, value: ${f}) {
35    ${j(`i2o_${e}(indices)`,"value")}
36  }`,ue=s<2?"":(()=>{let P=u.map(Q=>`d${Q}: u32`).join(", "),V=u.map(Q=>`d${Q}`).join(", ");return`
37  fn set_${e}(${P}, value: ${f}) {
38    set_${e}ByIndices(${v(V)}, value);
39  }`})();return{impl:()=>{let P=[],V=!1;return y.offsetToIndices&&(P.push(I),V=!0),y.indicesToOffset&&(P.push(C),V=!0),y.broadcastedIndicesToOffset&&(Object.values(H).forEach(Q=>P.push(Q)),V=!0),y.set&&(P.push(ue),V=!0),y.setByIndices&&(P.push(W),V=!0),y.get&&(P.push(Z),V=!0),y.getByIndices&&(P.push(K),V=!0),!n&&V&&P.unshift(`const ${S} = ${g.indices}(${r.join(",")});`,`const ${x} = ${g.indices}(${O.computeStrides(r).join(",")});`),P.join(`
40`)},type:g,offsetToIndices:k,indicesToOffset:A,broadcastedIndicesToOffset:F,indices:v,indicesGet:M,indicesSet:D,set:(...P)=>{if(P.length!==s+1)throw new Error(`indices length must be ${s}`);let V=P[s];if(typeof V!="string")throw new Error("value must be string");let Q=P.slice(0,s).map(_).join(",");return s===0?j("0u",V):s===1?j(Q[0],V):(y.set=!0,y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}(${Q}, ${V})`)},setByOffset:j,setByIndices:(P,V)=>s<2?j(P,V):(y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}ByIndices(${P}, ${V});`),get:ee,getByOffset:R,getByIndices:he,usage:i,name:e,strides:x,shape:S,rank:s}},N=(e,t,r,i=1)=>er(e,t,r,"input",i),X=(e,t,r,i=1)=>er(e,t,r,"output",i),mp=(e,t,r)=>er(e,t,r,"atomicOutput",1),Va=(e,t,r,i=1)=>er(e,t,r,"internal",i),zo=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=Gt){let t=typeof e=="number"?e:e[0],r=typeof e=="number"?1:e[1],i=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||r>this.limits.maxComputeWorkgroupSizeY||i>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${r}, ${i}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*r*i>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${r}, ${i}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let a=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,n=a?`@builtin(global_invocation_id) global_id : vec3<u32>,
41    @builtin(workgroup_id) workgroup_id : vec3<u32>,
42    @builtin(local_invocation_index) local_idx : u32,
43    @builtin(local_invocation_id) local_id : vec3<u32>`:`@builtin(global_invocation_id) global_id : vec3<u32>,
44                                             @builtin(local_invocation_id) local_id : vec3<u32>,
45    @builtin(local_invocation_index) local_idx : u32,
46    @builtin(workgroup_id) workgroup_id : vec3<u32>,
47    @builtin(num_workgroups) num_workgroups : vec3<u32>`,s=a?`let global_idx = global_id.x;
48         let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] +
49             workgroup_id.y * num_workgroups[0] + workgroup_id.x;
50         let global_idx = workgroup_index * ${t*r*i}u + local_idx;`;return`@compute @workgroup_size(${t}, ${r}, ${i})
51  fn main(${n}) {
52    ${s}
53  `}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let r=e.usage==="input"?"read":"read_write",i=e.usage==="atomicOutput"?"atomic<i32>":e.type.storage;return`@group(0) @binding(${t}) var<storage, ${r}> ${e.name}: array<${i}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(`
54`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,r=1){return this.uniforms.push({name:e,type:t,length:r}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:r,length:i}of this.uniforms)if(i&&i>4)r==="f16"?e.push(`@align(16) ${t}:array<mat2x4<${r}>, ${Math.ceil(i/8)}>`):e.push(`${t}:array<vec4<${r}>, ${Math.ceil(i/4)}>`);else{let a=i==null||i===1?r:`vec${i}<${r}>`;e.push(`${t}:${a}`)}return`
55      struct Uniforms { ${e.join(", ")} };
56      @group(0) @binding(${this.variableIndex}) var<uniform> uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(e=>e.impl()).join(`
57`)+this.internalVariables.map(e=>e.impl()).join(`
58`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},gp=(e,t)=>new zo(e,t)}),Co,Ui,Ao,Oo,Ro,Bo,qe,_p,yp,yt=U(()=>{te(),ie(),xe(),ae(),Co=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},Ui=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),Ao=(e,t)=>O.sortBasedOnPerm(e,Ui(e.length,t)),Oo=(e,t,r,i)=>{let a=`fn perm(i: ${i.type.indices}) -> ${r.type.indices} {
59    var a: ${r.type.indices};`;for(let n=0;n<t;++n)a+=`a[${e[n]}]=i[${n}];`;return a+="return a;}"},Ro=(e,t)=>{let r=[],i=[];for(let a=0;a<e.length;++a)e[a]!==1&&r.push(e[a]),e[t[a]]!==1&&i.push(t[a]);return{newShape:r,newPerm:i}},Bo=(e,t)=>{let r=0;for(let i=0;i<e.length;++i)if(t[e[i]]!==1){if(e[i]<r)return!1;r=e[i]}return!0},qe=(e,t)=>{let r=e.dataType,i=e.dims.length,a=Ui(i,t),n=Ao(e.dims,a),s=e.dims,u=n,l=i<2||Bo(a,e.dims),p;if(l)return p=y=>{let b=N("input",r,s,4),S=X("output",r,u,4);return`
60  ${y.registerUniform("output_size","u32").declareVariables(b,S)}
61  ${y.mainStart()}
62    ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
63    output[global_idx] = input[global_idx];
64  }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let y=O.size(n);return{outputs:[{dims:n,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(y/64/4)},programUniforms:[{type:12,data:Math.ceil(y/4)}]}},getShaderSource:p};let{newShape:f,newPerm:h}=Ro(e.dims,a),g=O.areEqual(h,[2,3,1]),_=O.areEqual(h,[3,1,2]);if(f.length===2||g||_){s=g?[f[0],f[1]*f[2]]:_?[f[0]*f[1],f[2]]:f,u=[s[1],s[0]];let y=16;return p=b=>{let S=N("a",r,s.length),x=X("output",r,u.length);return`
65  ${b.registerUniform("output_size","u32").declareVariables(S,x)}
66  var<workgroup> tile : array<array<${x.type.value}, ${y+1}>, ${y}>;
67  ${b.mainStart([y,y,1])}
68    let stride = (uniforms.output_shape[1] - 1) / ${y} + 1;
69    let workgroup_id_x = workgroup_index % stride;
70    let workgroup_id_y = workgroup_index / stride;
71    let input_col = workgroup_id_y * ${y}u + local_id.x;
72    let input_row = workgroup_id_x * ${y}u + local_id.y;
73    if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) {
74      tile[local_id.y][local_id.x] = ${S.getByIndices(`${S.type.indices}(input_row, input_col)`)};
75    }
76    workgroupBarrier();
77
78    let output_col = workgroup_id_x * ${y}u + local_id.x;
79    let output_row = workgroup_id_y * ${y}u + local_id.y;
80    if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) {
81      ${x.setByIndices(`${x.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")}
82    }
83  }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let b=O.size(n);return{outputs:[{dims:n,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(u[1]/y),y:Math.ceil(u[0]/y)},programUniforms:[{type:12,data:b},...J(s,u)]}},getShaderSource:p}}return p=y=>{let b=N("a",r,s.length),S=X("output",r,u.length);return`
84  ${y.registerUniform("output_size","u32").declareVariables(b,S)}
85
86  ${Oo(a,i,b,S)}
87
88  ${y.mainStart()}
89    ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
90
91    let indices = ${S.offsetToIndices("global_idx")};
92    let aIndices = perm(indices);
93
94    ${S.setByOffset("global_idx",b.getByIndices("aIndices"))}
95  }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let y=O.size(n);return{outputs:[{dims:n,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},...J(s,u)]}},getShaderSource:p}},_p=(e,t)=>{Co(e.inputs,t.perm),e.compute(qe(e.inputs[0],t.perm))},yp=e=>fe({perm:e.perm})}),No,Mo,Do,Uo,Po,qo,Lo,Wo,Vo,Go,He,bp,wp,$p,vp,xp,Sp,kp,Tp,Ip,Ep,Jg=U(()=>{te(),ie(),ae(),Ga(),yt(),No={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"},Mo={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"},Do={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Uo={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Po=(e,t)=>{let r=[];for(let i=t-e;i<t;++i)r.push(i);return r},qo=(e,t)=>{let r=[],i=e.length;for(let n=0;n<i;n++)t.indexOf(n)===-1&&r.push(e[n]);let a=t.map(n=>e[n]);return[r,a]},Lo=(e,t)=>{let r=e.length+t.length,i=[],a=0;for(let n=0;n<r;n++)t.indexOf(n)===-1?i.push(e[a++]):i.push(1);return i},Wo=(e,t)=>{for(let r=0;r<e.length;++r)if(e[e.length-r-1]!==t-1-r)return!1;return!0},Vo=(e,t)=>{let r=[];if(!Wo(e,t)){for(let i=0;i<t;++i)e.indexOf(i)===-1&&r.push(i);e.forEach(i=>r.push(i))}return r},Go=(e,t,r,i,a,n,s)=>{let u=r[0].dims,l=O.size(n),p=O.size(s),f=N("_A",r[0].dataType,u),h=X("output",a,n),g=64;l===1&&(g=256);let _=`
96          var<workgroup> aBestValues : array<f32, ${g}>;
97       `,y=b=>`
98        ${b.registerUniform("reduceSize","u32").declareVariables(f,h)}
99        ${_}
100        fn DIV_CEIL(a : u32, b : u32) -> u32 {
101          return ((a - 1u) / b + 1u);
102         }
103         ${b.mainStart(g)}
104
105          let outputIndex = global_idx / ${g};
106          let offset = outputIndex * uniforms.reduceSize;
107
108          var bestValue = f32(${Do[i]});
109          let Length = uniforms.reduceSize;
110          for (var k = local_idx; k < Length; k = k + ${g}) {
111           let candidate = f32(${f.getByOffset("offset + k")});
112           bestValue = ${No[i]};
113          }
114          aBestValues[local_idx] = bestValue;
115          workgroupBarrier();
116
117         var reduceSize = min(Length, ${g}u);
118         for (var currentSize = reduceSize / 2u; reduceSize > 1u;
119             currentSize = reduceSize / 2u) {
120           let interval = DIV_CEIL(reduceSize, 2u);
121           if (local_idx < currentSize) {
122            let candidate = aBestValues[local_idx + interval];
123            bestValue = ${Mo[i]};
124            aBestValues[local_idx] = bestValue;
125           }
126           reduceSize = interval;
127           workgroupBarrier();
128         }
129
130         if (local_idx == 0u) {
131          ${h.setByOffset("outputIndex",`${i==="mean"?`${h.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${h.type.storage}(${Uo[i]})`}`)};
132         }
133        }`;return{name:e,shaderCache:{hint:`${t};${g}`,inputDependencies:["type"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:n,dataType:a}],dispatchGroup:{x:l},programUniforms:[{type:12,data:p}]})}},He=(e,t,r,i)=>{let a=e.inputs.length===1?r:wa(e.inputs,r),n=a.axes;n.length===0&&!a.noopWithEmptyAxes&&(n=e.inputs[0].dims.map((_,y)=>y));let s=O.normalizeAxes(n,e.inputs[0].dims.length),u=s,l=e.inputs[0],p=Vo(u,e.inputs[0].dims.length);p.length>0&&(l=e.compute(qe(e.inputs[0],p),{inputs:[0],outputs:[-1]})[0],u=Po(u.length,l.dims.length));let[f,h]=qo(l.dims,u),g=f;a.keepDims&&(g=Lo(f,s)),e.compute(Go(t,a.cacheKey,[l],i,e.inputs[0].dataType,g,h),{inputs:[l]})},bp=(e,t)=>{He(e,"ReduceMeanShared",t,"mean")},wp=(e,t)=>{He(e,"ReduceL1Shared",t,"l1")},$p=(e,t)=>{He(e,"ReduceL2Shared",t,"l2")},vp=(e,t)=>{He(e,"ReduceLogSumExpShared",t,"logSumExp")},xp=(e,t)=>{He(e,"ReduceMaxShared",t,"max")},Sp=(e,t)=>{He(e,"ReduceMinShared",t,"min")},kp=(e,t)=>{He(e,"ReduceProdShared",t,"prod")},Tp=(e,t)=>{He(e,"ReduceSumShared",t,"sum")},Ip=(e,t)=>{He(e,"ReduceSumSquareShared",t,"sumSquare")},Ep=(e,t)=>{He(e,"ReduceLogSumShared",t,"logSum")}}),Fe,Ho,Fr,wa,je,Fo,jo,Ko,Zo,Qo,Xo,Yo,Jo,eu,tu,Ke,zp,Cp,Ap,Op,Rp,Bp,Np,Mp,Dp,Up,Ga=U(()=>{te(),ie(),xe(),ae(),Jg(),Fe=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.")},Ho=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Fr=(e,t,r,i,a,n,s=!1,u=!1)=>{let l=[],p=r[0].dims,f=p.length,h=O.normalizeAxes(a,f),g=!u&&h.length===0;p.forEach((b,S)=>{g||h.indexOf(S)>=0?s&&l.push(1):l.push(b)});let _=l.length,y=O.size(l);return{name:e,shaderCache:t,getShaderSource:b=>{let S=[],x=N("_A",r[0].dataType,f),w=X("output",n,_),I=i(x,w,h),k=I[2];for(let E=0,C=0;E<f;E++)g||h.indexOf(E)>=0?(s&&C++,k=`for(var j${E}: u32 = 0; j${E} < ${p[E]}; j${E}++) {
134                  ${I[2].includes("last_index")?`let last_index = j${E};`:""}
135                  ${x.indicesSet("input_indices",E,`j${E}`)}
136                  ${k}
137                }`):(S.push(`${x.indicesSet("input_indices",E,w.indicesGet("output_indices",C))};`),C++);return`
138
139        ${b.registerUniform("output_size","u32").declareVariables(x,w)}
140
141        ${b.mainStart()}
142          ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
143          var input_indices: ${x.type.indices};
144          let output_indices = ${w.offsetToIndices("global_idx")};
145
146          ${S.join(`
147`)}
148          ${I[0]}       // init ops for reduce max/min
149          ${I[1]}
150          ${k}
151          ${I[3]}
152          ${I.length===4?w.setByOffset("global_idx","value"):I.slice(4).join(`
153`)}
154        }`},getRunData:()=>({outputs:[{dims:l,dataType:n}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},...J(p,l)]})}},wa=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(i=>r.push(Number(i))),fe({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},je=(e,t,r,i)=>{let a=e.inputs,n=a.length===1?r:wa(a,r);e.compute(Fr(t,{hint:n.cacheKey,inputDependencies:["rank"]},[a[0]],n.noopWithEmptyAxes&&n.axes.length===0?Ho:i,n.axes,a[0].dataType,n.keepDims,n.noopWithEmptyAxes),{inputs:[0]})},Fo=(e,t)=>{Fe(e.inputs),je(e,"ReduceLogSum",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,"value = log(value);"])},jo=(e,t)=>{Fe(e.inputs),je(e,"ReduceL1",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += abs(${r.getByIndices("input_indices")});`,""])},Ko=(e,t)=>{Fe(e.inputs),je(e,"ReduceL2",t,(r,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Zo=(e,t)=>{Fe(e.inputs),je(e,"ReduceLogSumExp",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += exp(${r.getByIndices("input_indices")});`,"value = log(value);"])},Qo=(e,t)=>{Fe(e.inputs),je(e,"ReduceMax",t,(r,i,a)=>{let n=[];for(let s=0;s<r.rank;s++)(a.indexOf(s)>=0||a.length===0)&&n.push(r.indicesSet("input_indices",s,0));return[`${n.join(`
155`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = max(value, ${r.getByIndices("input_indices")});`,""]})},Xo=(e,t)=>{Fe(e.inputs),je(e,"ReduceMean",t,(r,i,a)=>{let n=1;for(let s=0;s<r.rank;s++)(a.indexOf(s)>=0||a.length===0)&&(n*=e.inputs[0].dims[s]);return["var sum = f32(0);","",`sum += f32(${r.getByIndices("input_indices")});`,`let value = ${i.type.value}(sum / ${n});`]})},Yo=(e,t)=>{Fe(e.inputs),je(e,"ReduceMin",t,(r,i,a)=>{let n=[];for(let s=0;s<r.rank;s++)(a.indexOf(s)>=0||a.length===0)&&n.push(`input_indices[${s}] = 0;`);return[`${n.join(`
156`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = min(value, ${r.getByIndices("input_indices")});`,""]})},Jo=(e,t)=>{Fe(e.inputs),je(e,"ReduceProd",t,(r,i)=>[`var value = ${i.type.storage}(1);`,"",`value *= ${r.getByIndices("input_indices")};`,""])},eu=(e,t)=>{Fe(e.inputs),je(e,"ReduceSum",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,""])},tu=(e,t)=>{Fe(e.inputs),je(e,"ReduceSumSquare",t,(r,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += t * t;`,""])},Ke=(e,t,r)=>{if(t.length===0)return r;let i=1,a=1;for(let n=0;n<t.length;n++)t.indexOf(n)===-1?i*=e[n]:a*=e[n];return a<32&&i>1024},zp=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Xo(e,t):bp(e,t)},Cp=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?jo(e,t):wp(e,t)},Ap=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Ko(e,t):$p(e,t)},Op=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Zo(e,t):vp(e,t)},Rp=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Qo(e,t):xp(e,t)},Bp=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Yo(e,t):Sp(e,t)},Np=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Jo(e,t):kp(e,t)},Mp=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?eu(e,t):Tp(e,t)},Dp=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?tu(e,t):Ip(e,t)},Up=(e,t)=>{Ke(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Fo(e,t):Ep(e,t)}}),Pi,Pp,qp,$a,e_=U(()=>{te(),xe(),Ga(),Pi=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.")},Pp=(e,t)=>{Pi(e.inputs);let r=(i,a,n)=>{let s=[];for(let u=0;u<i.rank;u++)(n.indexOf(u)>=0||n.length===0)&&s.push(`input_indices[${u}] = 0;`);return[`${s.join(`
157`)}`,`var value = ${i.getByIndices("input_indices")};
158var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) {
159         value = ${i.getByIndices("input_indices")};
160         best_index = i32(last_index);
161       }`,"",a.setByOffset("global_idx","best_index")]};e.compute(Fr("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},qp=(e,t)=>{Pi(e.inputs);let r=(i,a,n)=>{let s=[];for(let u=0;u<i.rank;u++)(n.indexOf(u)>=0||n.length===0)&&s.push(`input_indices[${u}] = 0;`);return[`${s.join(`
162`)}`,`var value = ${i.getByIndices("input_indices")};
163var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) {
164         value = ${i.getByIndices("input_indices")};
165         best_index = i32(last_index);
166       }`,"",a.setByOffset("global_idx","best_index")]};e.compute(Fr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},$a=e=>fe(e)}),ru,Rr,iu,au,nu,hr,su,Lp,Ha=U(()=>{te(),ie(),Wa(),ae(),ru=(e,t)=>{let r=e[0],i=e[1],a=e[2],n=e[3],s=e[4],u=e[5];if(s&&u)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 l=r.dims[0],p=r.dims[1],f=r.dims[2];if(a.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(i.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(i.dims[0]!==f)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(a.dims[0]!==i.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let h=a.dims[0]/3,g=h,_=g;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let I of t.qkvHiddenSizes)if(I%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");h=t.qkvHiddenSizes[0],g=t.qkvHiddenSizes[1],_=t.qkvHiddenSizes[2]}let y=p;if(h!==g)throw new Error("qkv_hidden_sizes first element should be same as the second");if(a.dims[0]!==h+g+_)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let b=0;if(s){if(g!==_)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(s.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(s.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(s.dims[1]!==l)throw new Error('Input "past" second dimension must be batch_size');if(s.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(s.dims[4]!==g/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(b=s.dims[3])}let S=y+b,x=-1,w=0;if(n)throw new Error("Mask not supported");if(s)throw new Error("past is not supported");if(u){if(u.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(u.dims[0]!==l||u.dims[1]!==t.numHeads||u.dims[2]!==p||u.dims[3]!==S)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:l,sequenceLength:p,pastSequenceLength:b,kvSequenceLength:y,totalSequenceLength:S,maxSequenceLength:x,inputHiddenSize:f,hiddenSize:h,vHiddenSize:_,headSize:Math.floor(h/t.numHeads),vHeadSize:Math.floor(_/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:w,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},Rr=(e,t,r)=>t&&e?`
167      let total_sequence_length_input = u32(${t.getByOffset("0")});
168      let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length);
169      let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input;
170      let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input;
171      total_sequence_length = u32(${e==null?void 0:e.getByOffset("batchIdx")}) + 1;
172      var past_sequence_length: u32 = 0;
173      if (is_first_prompt == false) {
174        past_sequence_length = total_sequence_length - sequence_length;
175      }
176       `:`
177    ${r?"let past_sequence_length = uniforms.past_sequence_length":""};
178    let present_sequence_length = total_sequence_length;
179    `,iu=(e,t,r,i,a,n,s,u)=>{let l=ve(s?1:n),p=64,f=n/l;f<p&&(p=32);let h=Math.ceil(n/l/p),g=[{type:12,data:t},{type:12,data:r},{type:12,data:i},{type:12,data:a},{type:12,data:f},{type:12,data:h}],_=Te(e.dataType,l),y=Ce(1,l),b=["type"];s&&b.push("type"),u&&b.push("type");let S=x=>{let w=X("x",e.dataType,e.dims,l),I=[w],k=s?N("seq_lens",s.dataType,s.dims):void 0;k&&I.push(k);let E=u?N("total_sequence_length_input",u.dataType,u.dims):void 0;E&&I.push(E);let C=Ce(e.dataType),A=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return`
180  var<workgroup> thread_max: array<f32, ${p}>;
181  var<workgroup> thread_sum: array<f32, ${p}>;
182  ${x.registerUniforms(A).declareVariables(...I)}
183  ${x.mainStart([p,1,1])}
184    let batchIdx = workgroup_id.z / uniforms.num_heads;
185    let headIdx = workgroup_id.z % uniforms.num_heads;
186    let sequence_length = uniforms.sequence_length;
187    var total_sequence_length = uniforms.total_sequence_length;
188    ${Rr(k,E,!1)}
189    let local_offset = local_idx * uniforms.elements_per_thread;
190    let offset = (global_idx / ${p}) * uniforms.total_sequence_length + local_offset;
191    let seq_causal_length = ${s?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"};
192    var thread_max_vector = ${y}(-3.4028234663852886e+38f);
193    for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
194      thread_max_vector = max(${y}(x[offset + i]), thread_max_vector);
195    }
196    thread_max[local_idx] = ${(()=>{switch(l){case 1:return"thread_max_vector";case 2:return"max(thread_max_vector.x, thread_max_vector.y)";case 4:return"max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))";default:throw new Error(`Unsupported components: ${l}`)}})()};
197    workgroupBarrier();
198
199    var max_value =  f32(-3.4028234663852886e+38f);
200    for (var i = 0u; i < ${p}; i++) {
201      max_value = max(thread_max[i], max_value);
202    }
203
204    var sum_vector = ${y}(0);
205    for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
206      sum_vector += exp(${y}(x[offset + i]) - max_value);
207    }
208    thread_sum[local_idx] = ${(()=>{switch(l){case 1:return"sum_vector";case 2:return"sum_vector.x + sum_vector.y";case 4:return"sum_vector.x + sum_vector.y + sum_vector.z + sum_vector.w";default:throw new Error(`Unsupported components: ${l}`)}})()};
209    workgroupBarrier();
210
211    var sum: f32 = 0;
212    for (var i = 0u; i < ${p}; i++) {
213      sum += thread_sum[i];
214    }
215
216    if (sum == 0) {
217      for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
218        x[offset + i] = ${w.type.value}(${C}(1.0) / ${C}(seq_causal_length));
219      }
220    } else {
221      for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
222        var f32input = ${y}(x[offset + i]);
223        x[offset + i] = ${w.type.value}(exp(f32input - max_value) / sum);
224      }
225    }
226      ${s?`
227        for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) {
228          x[offset + total_seq_id] = ${w.type.value}(${C}(0));
229        }`:""};
230  }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${p};${_};${l}`,inputDependencies:b},getShaderSource:S,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:a,z:t*r},programUniforms:g})}},au=(e,t,r,i,a,n,s,u,l)=>{let p=s+n.kvSequenceLength,f=[n.batchSize,n.numHeads,n.sequenceLength,p],h=e>1&&i,g=n.kvNumHeads?n.kvNumHeads:n.numHeads,_=h?[n.batchSize,g,p,n.headSize]:void 0,y=n.nReps?n.nReps:1,b=n.scale===0?1/Math.sqrt(n.headSize):n.scale,S=ve(n.headSize),x=n.headSize/S,w=12,I={x:Math.ceil(p/w),y:Math.ceil(n.sequenceLength/w),z:n.batchSize*n.numHeads},k=[{type:12,data:n.sequenceLength},{type:12,data:x},{type:12,data:p},{type:12,data:n.numHeads},{type:12,data:n.headSize},{type:1,data:b},{type:12,data:s},{type:12,data:n.kvSequenceLength},{type:12,data:y}],E=h&&i&&O.size(i.dims)>0,C=["type","type"];E&&C.push("type"),a&&C.push("type"),u&&C.push("type"),l&&C.push("type");let A=[{dims:f,dataType:t.dataType,gpuDataType:0}];h&&A.push({dims:_,dataType:t.dataType,gpuDataType:0});let v=M=>{let D=N("q",t.dataType,t.dims,S),H=N("key",r.dataType,r.dims,S),F=[D,H];if(E){let W=N("past_key",i.dataType,i.dims,S);F.push(W)}a&&F.push(N("attention_bias",a.dataType,a.dims));let j=u?N("seq_lens",u.dataType,u.dims):void 0;j&&F.push(j);let R=l?N("total_sequence_length_input",l.dataType,l.dims):void 0;R&&F.push(R);let K=X("output",t.dataType,f),Z=[K];h&&Z.push(X("present_key",t.dataType,_,S));let ee=Ce(1,S),he=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return`
231  const TILE_SIZE = ${w}u;
232
233  var<workgroup> tileQ: array<${D.type.storage}, ${w*w}>;
234  var<workgroup> tileK: array<${D.type.storage}, ${w*w}>;
235  ${M.registerUniforms(he).declareVariables(...F,...Z)}
236  ${M.mainStart([w,w,1])}
237    // x holds the N and y holds the M
238    let headIdx = workgroup_id.z % uniforms.num_heads;
239    let kvHeadIdx = ${y===1?"headIdx":"headIdx / uniforms.n_reps"};
240    let kv_num_heads = ${y===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"};
241    let batchIdx = workgroup_id.z / uniforms.num_heads;
242    let m = workgroup_id.y * TILE_SIZE;
243    let n = workgroup_id.x * TILE_SIZE;
244    let sequence_length = uniforms.M;
245    var total_sequence_length = uniforms.N;
246    ${Rr(j,R,!0)}
247    let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx;
248    let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;
249    ${E&&h?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""};
250    let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K;
251    ${h?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""}
252    var value = ${ee}(0);
253    for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
254      if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) {
255        tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x];
256      }
257      if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) {
258        var idx = TILE_SIZE * local_id.y + local_id.x;
259      ${E&&h?`
260              if (n + local_id.y < past_sequence_length) {
261                tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x];
262              } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {
263                tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x];
264              }`:`
265          if (n + local_id.y < uniforms.kv_sequence_length) {
266            tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x];
267          }`}
268      ${h?`if (n + local_id.y < present_sequence_length) {
269        present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx];
270      }`:""}
271      }
272      workgroupBarrier();
273
274      for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) {
275          value += ${ee}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]);
276      }
277
278      workgroupBarrier();
279    }
280
281    if (global_id.y < uniforms.M && global_id.x < total_sequence_length) {
282      let headOffset = workgroup_id.z * uniforms.M * uniforms.N;
283      let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x;
284      var sum: f32 = ${(()=>{switch(S){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: ${S}`)}})()};
285        output[outputIdx] = ${K.type.value} (sum * uniforms.alpha) + ${a?"attention_bias[outputIdx]":"0.0"};
286    }
287  }`};return{name:"AttentionProbs",shaderCache:{hint:`${S};${a!==void 0};${i!==void 0};${e}`,inputDependencies:C},getRunData:()=>({outputs:A,dispatchGroup:I,programUniforms:k}),getShaderSource:v}},nu=(e,t,r,i,a,n,s=void 0,u=void 0)=>{let l=n+a.kvSequenceLength,p=a.nReps?a.nReps:1,f=a.vHiddenSize*p,h=e>1&&i,g=a.kvNumHeads?a.kvNumHeads:a.numHeads,_=h?[a.batchSize,g,l,a.headSize]:void 0,y=[a.batchSize,a.sequenceLength,f],b=12,S={x:Math.ceil(a.vHeadSize/b),y:Math.ceil(a.sequenceLength/b),z:a.batchSize*a.numHeads},x=[{type:12,data:a.sequenceLength},{type:12,data:l},{type:12,data:a.vHeadSize},{type:12,data:a.numHeads},{type:12,data:a.headSize},{type:12,data:f},{type:12,data:n},{type:12,data:a.kvSequenceLength},{type:12,data:p}],w=h&&i&&O.size(i.dims)>0,I=["type","type"];w&&I.push("type"),s&&I.push("type"),u&&I.push("type");let k=[{dims:y,dataType:t.dataType,gpuDataType:0}];h&&k.push({dims:_,dataType:t.dataType,gpuDataType:0});let E=C=>{let A=N("probs",t.dataType,t.dims),v=N("v",r.dataType,r.dims),M=[A,v];w&&M.push(N("past_value",i.dataType,i.dims));let D=s?N("seq_lens",s.dataType,s.dims):void 0;s&&M.push(D);let H=u?N("total_sequence_length_input",u.dataType,u.dims):void 0;u&&M.push(H);let F=[X("output",t.dataType,y)];h&&F.push(X("present_value",t.dataType,_));let j=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return`
288  const TILE_SIZE = ${b}u;
289  var<workgroup> tileQ: array<${A.type.value}, ${b*b}>;
290  var<workgroup> tileV: array<${A.type.value}, ${b*b}>;
291  ${C.registerUniforms(j).declareVariables(...M,...F)}
292  ${C.mainStart([b,b,1])}
293   let headIdx = workgroup_id.z % uniforms.num_heads;
294   let batchIdx = workgroup_id.z / uniforms.num_heads;
295   let kvHeadIdx = ${p===1?"headIdx":"headIdx / uniforms.n_reps"};
296   let kv_num_heads = ${p===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"};
297   let m = global_id.y;
298   let n = global_id.x;
299   let sequence_length = uniforms.M;
300   var total_sequence_length = uniforms.K;
301   ${Rr(D,H,!0)}
302   let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;
303   let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch
304   ${w&&h?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""};
305   let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n;
306   ${h?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""}
307   var value = ${A.type.storage}(0);
308   for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
309      if (m < uniforms.M && w + local_id.x < uniforms.K) {
310        tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x];
311      }
312      if (n < uniforms.N && w + local_id.y < uniforms.K) {
313        var idx = TILE_SIZE * local_id.y + local_id.x;
314        ${w&&h?`
315        if (w + local_id.y < past_sequence_length) {
316          tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N];
317        } else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {
318          tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N];
319        }
320      `:`
321            if (w + local_id.y < uniforms.kv_sequence_length) {
322              tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N];
323            }`}
324        ${h?`
325            if (w + local_id.y < present_sequence_length) {
326          present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx];
327        }`:""}
328      }
329     workgroupBarrier();
330     for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) {
331       value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x];
332     }
333     workgroupBarrier();
334   }
335
336   // we need to transpose output from BNSH_v to BSND_v
337   if (m < uniforms.M && n < uniforms.N) {
338     let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size
339       + headIdx * uniforms.N + n;
340     output[outputIdx] = value;
341   }
342  }`};return{name:"AttentionScore",shaderCache:{hint:`${i!==void 0};${e}`,inputDependencies:I},getRunData:()=>({outputs:k,dispatchGroup:S,programUniforms:x}),getShaderSource:E}},hr=(e,t,r,i,a,n,s,u,l,p,f=void 0,h=void 0)=>{let g=Math.min(e.outputCount,1+(s?1:0)+(u?1:0)),_=g>1?s:void 0,y=g>1?u:void 0,b=g>1?p.pastSequenceLength:0,S=b+p.kvSequenceLength,x=l&&O.size(l.dims)>0?l:void 0,w=[t,r];_&&O.size(_.dims)>0&&w.push(_),x&&w.push(x),f&&w.push(f),h&&w.push(h);let I=e.compute(au(g,t,r,_,x,p,b,f,h),{inputs:w,outputs:g>1?[-1,1]:[-1]})[0];e.compute(iu(I,p.batchSize,p.numHeads,b,p.sequenceLength,S,f,h),{inputs:f&&h?[I,f,h]:[I],outputs:[]});let k=[I,i];y&&O.size(y.dims)>0&&k.push(y),f&&k.push(f),h&&k.push(h),e.compute(nu(g,I,i,y,p,b,f,h),{inputs:k,outputs:g>1?[0,2]:[0]})},su=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],i=t.sequenceLength,a=t.inputHiddenSize,n=t.headSize,s=12,u={x:Math.ceil(t.headSize/s),y:Math.ceil(t.sequenceLength/s),z:t.batchSize*t.numHeads},l=[e.inputs[0],e.inputs[1],e.inputs[2]],p=[{type:12,data:i},{type:12,data:a},{type:12,data:n},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],f=h=>{let g=X("output_q",l[0].dataType,r),_=X("output_k",l[0].dataType,r),y=X("output_v",l[0].dataType,r),b=N("input",l[0].dataType,l[0].dims),S=N("weight",l[1].dataType,l[1].dims),x=N("bias",l[2].dataType,l[2].dims),w=b.type.storage,I=[{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`
343  const TILE_SIZE = ${s}u;
344  var<workgroup> tileInput: array<${w}, ${s*s}>;
345  var<workgroup> tileWeightQ: array<${w}, ${s*s}>;
346  var<workgroup> tileWeightK: array<${w}, ${s*s}>;
347  var<workgroup> tileWeightV: array<${w}, ${s*s}>;
348  ${h.registerUniforms(I).declareVariables(b,S,x,g,_,y)}
349  ${h.mainStart([s,s,1])}
350    let batchIndex = workgroup_id.z / uniforms.num_heads;
351    let headNumber = workgroup_id.z % uniforms.num_heads;
352    let m = global_id.y;
353    let n = global_id.x;
354
355    let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K;
356    let biasOffsetQ = headNumber * uniforms.head_size;
357    let biasOffsetK = uniforms.hidden_size + biasOffsetQ;
358    let biasOffsetV = uniforms.hidden_size + biasOffsetK;
359
360    var valueQ = ${w}(0);
361    var valueK = ${w}(0);
362    var valueV = ${w}(0);
363    for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
364      if (m < uniforms.M && w + local_id.x < uniforms.K) {
365        tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x];
366      }
367      if (n < uniforms.N && w + local_id.y < uniforms.K) {
368        let offset = n + (w + local_id.y) * uniforms.ldb;
369        tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset];
370        tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset];
371        tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset];
372      }
373      workgroupBarrier();
374      for (var k: u32 = 0u; k<TILE_SIZE && w+k < uniforms.K; k++) {
375        let inputTileOffset = TILE_SIZE * local_id.y + k;
376        let weightTileOffset = TILE_SIZE * k + local_id.x;
377        valueQ += tileInput[inputTileOffset] * tileWeightQ[weightTileOffset];
378        valueK += tileInput[inputTileOffset] * tileWeightK[weightTileOffset];
379        valueV += tileInput[inputTileOffset] * tileWeightV[weightTileOffset];
380      }
381
382      workgroupBarrier();
383    }
384
385    let headOffset = (m * uniforms.N + n) % uniforms.head_size;
386    valueQ += bias[headOffset + biasOffsetQ];
387    valueK += bias[headOffset + biasOffsetK];
388    valueV += bias[headOffset + biasOffsetV];
389
390    let offset = workgroup_id.z * uniforms.M * uniforms.N;
391    if (m < uniforms.M && n < uniforms.N) {
392      let outputIdx = offset + m * uniforms.N + n;
393      output_q[outputIdx] = valueQ;
394      output_k[outputIdx] = valueK;
395      output_v[outputIdx] = valueV;
396    }
397  }`};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:u,programUniforms:p}),getShaderSource:f},{inputs:l,outputs:[-1,-1,-1]})},Lp=(e,t)=>{let r=ru(e.inputs,t),[i,a,n]=su(e,r);return hr(e,i,a,n,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r)}}),ou,uu,lu,Wp,t_=U(()=>{Ve(),te(),ie(),xe(),ae(),ou=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(i,a,n)=>{let s=a.length;if(s!==i.length)throw new Error(`${n}: num dimensions != ${s}`);a.forEach((u,l)=>{if(u!==i[l])throw new Error(`${n}: dim[${l}] do not match`)})};if(e[0].dims.length>1){let i=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,i,"Invalid input scale"),r(e[2].dims,i,"Invalid input B"),r(e[3].dims,i,"Invalid input mean"),r(e[4].dims,i,"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")},uu=(e,t)=>{let{epsilon:r,spatial:i,format:a}=t,n=e[0].dims,s=i?ve(n[n.length-1]):1,u=a==="NHWC"&&n.length>1?s:1,l=O.size(n)/s,p=i,f=p?n.length:n,h=N("x",e[0].dataType,e[0].dims,s),g=N("scale",e[1].dataType,e[1].dims,u),_=N("bias",e[2].dataType,e[2].dims,u),y=N("inputMean",e[3].dataType,e[3].dims,u),b=N("inputVar",e[4].dataType,e[4].dims,u),S=X("y",e[0].dataType,f,s),x=()=>{let I="";if(i)I=`let cOffset = ${n.length===1?"0u":a==="NHWC"?`outputIndices[${n.length-1}] / ${s}`:"outputIndices[1]"};`;else if(a==="NCHW")I=`
398            ${S.indicesSet("outputIndices","0","0")}
399            let cOffset = ${S.indicesToOffset("outputIndices")};`;else{I=`var cIndices = ${g.type.indices}(0);
400                       cIndices[0] = outputIndices[${n.length-1}];`;for(let k=1;k<g.rank;k++)I+=`cIndices[${k}] = outputIndices[${k}];`;I+=`let cOffset = ${g.indicesToOffset("cIndices")};`}return I},w=I=>`
401  const epsilon = ${r};
402  ${I.registerUniform("outputSize","u32").declareVariables(h,g,_,y,b,S)}
403  ${I.mainStart()}
404  ${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
405    var outputIndices = ${S.offsetToIndices(`global_idx * ${s}`)};
406    ${x()}
407    let scale = ${g.getByOffset("cOffset")};
408    let bias = ${_.getByOffset("cOffset")};
409    let inputMean = ${y.getByOffset("cOffset")};
410    let inputVar = ${b.getByOffset("cOffset")};
411    let x = ${h.getByOffset("global_idx")};
412    let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias;
413    ${S.setByOffset("global_idx","value")}
414  }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${i}_${s}`,inputDependencies:p?["rank","type","type","type","type"]:void 0},getShaderSource:w,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:p?[{type:12,data:l},...J(n)]:[{type:12,data:l}]})}},lu=e=>fe(e),Wp=(e,t)=>{let{inputs:r,outputCount:i}=e,a=lu({...t,outputCount:i});if($e.webgpu.validateInputContent&&ou(r,a),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(uu(r,a))}}),du,pu,Vp,r_=U(()=>{ie(),ae(),du=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")},pu=e=>{let t=e[0].dims,r=e[0].dims[2],i=O.size(t)/4,a=e[0].dataType,n=N("input",a,t,4),s=N("bias",a,[r],4),u=N("residual",a,t,4),l=X("output",a,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:p=>`
415  const channels = ${r}u / 4;
416  ${p.declareVariables(n,s,u,l)}
417
418  ${p.mainStart()}
419    ${p.guardAgainstOutOfBoundsWorkgroupSizes(i)}
420    let value = ${n.getByOffset("global_idx")}
421      + ${s.getByOffset("global_idx % channels")} + ${u.getByOffset("global_idx")};
422    ${l.setByOffset("global_idx","value")}
423  }`}},Vp=e=>{du(e.inputs),e.compute(pu(e.inputs))}}),cu,ce,Gp,Hp,Fp,jp,Kp,Zp,Qp,Xp,Yp,hu,Jp,ec,tc,rc,lr,ic,Lr,ac,nc,sc,oc,uc,lc,dc,pc,cc,hc,fc,mc,gc,_c,yc,bc,qi,wc,va,xa,$c,vc,xc,fu,mu,Sc,Fa=U(()=>{te(),ie(),xe(),ae(),cu=(e,t,r,i,a,n,s)=>{let u=Math.ceil(t/4),l="";typeof a=="string"?l=`${a}(a)`:l=a("a");let p=N("inputData",r,[u],4),f=X("outputData",i,[u],4),h=[{name:"vec_size",type:"u32"}];return s&&h.push(...s),`
424      ${e.registerUniforms(h).declareVariables(p,f)}
425
426  ${n??""}
427
428  ${e.mainStart()}
429    ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
430
431    let a = ${p.getByOffset("global_idx")};
432    ${f.setByOffset("global_idx",l)}
433  }`},ce=(e,t,r,i,a,n=e.dataType,s,u)=>{let l=[{type:12,data:Math.ceil(O.size(e.dims)/4)}];return s&&l.push(...s),{name:t,shaderCache:{hint:a,inputDependencies:["type"]},getShaderSource:p=>cu(p,O.size(e.dims),e.dataType,n,r,i,u),getRunData:p=>({outputs:[{dims:e.dims,dataType:n}],dispatchGroup:{x:Math.ceil(O.size(p[0].dims)/64/4)},programUniforms:l})}},Gp=e=>{e.compute(ce(e.inputs[0],"Abs","abs"))},Hp=e=>{e.compute(ce(e.inputs[0],"Acos","acos"))},Fp=e=>{e.compute(ce(e.inputs[0],"Acosh","acosh"))},jp=e=>{e.compute(ce(e.inputs[0],"Asin","asin"))},Kp=e=>{e.compute(ce(e.inputs[0],"Asinh","asinh"))},Zp=e=>{e.compute(ce(e.inputs[0],"Atan","atan"))},Qp=e=>{e.compute(ce(e.inputs[0],"Atanh","atanh"))},Xp=e=>fe(e),Yp=(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(ce(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},hu=e=>{let t,r,i=e.length>=2&&e[1].data!==0,a=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=i?e[1].getFloat32Array()[0]:-34028234663852886e22,r=a?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=i?e[1].getUint16Array()[0]:64511,r=a?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return fe({min:t,max:r})},Jp=(e,t)=>
vendor: 4,733 bytes, lines 433-482
433{let r=t||hu(e.inputs),i=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Clip",a=>`clamp(${a}, vec4<${i}>(uniforms.min), vec4<${i}>(uniforms.max))`,void 0,r.cacheKey,void 0,[{type:e.inputs[0].dataType,data:r.min},{type:e.inputs[0].dataType,data:r.max}],[{name:"min",type:i},{name:"max",type:i}]),{inputs:[0]})},ec=e=>{e.compute(ce(e.inputs[0],"Ceil","ceil"))},tc=e=>{e.compute(ce(e.inputs[0],"Cos","cos"))},rc=e=>{e.compute(ce(e.inputs[0],"Cosh","cosh"))},lr=e=>fe(e),ic=(e,t)=>{let r=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Elu",i=>`elu_vf32(${i})`,`
434  const elu_alpha_ = ${r}(${t.alpha});
435
436  fn elu_f32(a: ${r}) -> ${r} {
437  return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0);
438  }
439
440  fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> {
441  return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w));
442  }`,t.cacheKey))},Lr=(e="f32")=>`
443const r0: ${e} = 0.3275911;
444const r1: ${e} = 0.254829592;
445const r2: ${e} = -0.284496736;
446const r3: ${e} = 1.421413741;
447const r4: ${e} = -1.453152027;
448const r5: ${e} = 1.061405429;
449
450fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> {
451  let absv = abs(v);
452  let x = 1.0 / (1.0 + r0 * absv);
453  return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv));
454}`,ac=e=>{let t=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,Lr(t)))},nc=e=>{e.compute(ce(e.inputs[0],"Exp","exp"))},sc=e=>{e.compute(ce(e.inputs[0],"Floor","floor"))},oc=e=>{let t=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,Lr(t)))},uc=(e,t)=>{let r=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"LeakyRelu",i=>`select(leaky_relu_alpha_ * ${i}, ${i}, ${i} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},lc=e=>{e.compute(ce(e.inputs[0],"Not",t=>`!${t}`))},dc=e=>{e.compute(ce(e.inputs[0],"Neg",t=>`-${t}`))},pc=e=>{e.compute(ce(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},cc=e=>{let t=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},hc=e=>{e.compute(ce(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},fc=e=>fe(e),mc=(e,t)=>{let r=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"HardSigmoid",i=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${i} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},gc=e=>{e.compute(ce(e.inputs[0],"Sin","sin"))},_c=e=>{e.compute(ce(e.inputs[0],"Sinh","sinh"))},yc=e=>{e.compute(ce(e.inputs[0],"Sqrt","sqrt"))},bc=e=>{e.compute(ce(e.inputs[0],"Tan","tan"))},qi=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,wc=e=>{e.compute(ce(e.inputs[0],"Tanh",qi))},va=(e="f32")=>`
455const fast_gelu_a: ${e} = 0.5;
456const fast_gelu_b: ${e} = 0.7978845608028654;
457const fast_gelu_c: ${e} = 0.035677408136300125;
458
459fn tanh_v(v: vec4<${e}>) -> vec4<${e}> {
460  return ${qi("v")};
461}
462`,xa=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,$c=e=>{let t=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"FastGelu",xa,va(t),void 0,e.inputs[0].dataType))},vc=(e,t)=>{let r=Ce(e.inputs[0].dataType);return e.compute(ce(e.inputs[0],"ThresholdedRelu",i=>`select(vec4<${r}>(0.0), ${i}, ${i} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},xc=e=>{e.compute(ce(e.inputs[0],"Log","log"))},fu=(e,t)=>`
463const alpha = vec4<${e}>(${t});
464const one = ${e}(1.0);
465const zero = ${e}(0.0);
466
467fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
468  let v = x *alpha;
469  var x1 : vec4<${e}>;
470  for (var i = 0; i < 4; i = i + 1) {
471    if (v[i] >= zero) {
472      x1[i] = one / (one + exp(-v[i]));
473    } else {
474      x1[i] = one - one / (one + exp(v[i]));
475    }
476  }
477  return x * x1;
478}
479`,mu=e=>`quick_gelu_impl(${e})`,Sc=(e,t)=>{let r=Ce(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"QuickGelu",mu,fu(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),gu,_u,kc,i_=U(()=>{ie(),ae(),Fa(),gu=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")},_u=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=N("input",e[0].dataType,e[0].dims,4),i=N("bias",e[0].dataType,[e[0].dims[2]],4),a=X("output",e[0].dataType,t,4),n=O.size(t)/4,s=Te(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)}}),getShaderSource:u=>`
480  const M_SQRT2 = sqrt(2.0);
481  const halfChannels = ${e[0].dims[2]/4/2}u;
482
483  ${u.declareVariables(r,i,a)}
484
485  ${Lr(s)}
486
487  ${u.mainStart()}
488    ${u.guardAgainstOutOfBoundsWorkgroupSizes(n)}
489    let biasIdx = global_idx % halfChannels;
490    let batchIndex = global_idx / halfChannels;
491    let inputOffset = biasIdx + batchIndex * halfChannels * 2;
492    let valueLeft = input[inputOffset] + bias[biasIdx];
493    let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels];
494    let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1);
495
496    ${a.setByOffset("global_idx","valueLeft * geluRight")}
497  }`}},kc=e=>{gu(e.inputs),e.compute(_u(e.inputs))}}),yu,bu,Ze,Tc,Ic,Ec,zc,Cc,Ac,Oc,Rc,Bc,Nc,a_=U(()=>{te(),ie(),ae(),yu=(e,t,r,i,a,n,s,u,l,p,f,h)=>{let g,_;typeof u=="string"?g=_=(w,I)=>`${u}((${w}),(${I}))`:typeof u=="function"?g=_=u:(g=u.scalar,_=u.vector);let y=X("outputData",f,i.length,4),b=N("aData",l,t.length,4),S=N("bData",p,r.length,4),x;if(a)if(n){let w=O.size(t)===1,I=O.size(r)===1,k=t.length>0&&t[t.length-1]%4===0,E=r.length>0&&r[r.length-1]%4===0;w||I?x=y.setByOffset("global_idx",_(w?`${b.type.value}(${b.getByOffset("0")}.x)`:b.getByOffset("global_idx"),I?`${S.type.value}(${S.getByOffset("0")}.x)`:S.getByOffset("global_idx"))):x=`
498            let outputIndices = ${y.offsetToIndices("global_idx * 4u")};
499            let offsetA = ${b.broadcastedIndicesToOffset("outputIndices",y)};
500            let offsetB = ${S.broadcastedIndicesToOffset("outputIndices",y)};
501            ${y.setByOffset("global_idx",_(s||k?b.getByOffset("offsetA / 4u"):`${b.type.value}(${b.getByOffset("offsetA / 4u")}[offsetA % 4u])`,s||E?S.getByOffset("offsetB / 4u"):`${S.type.value}(${S.getByOffset("offsetB / 4u")}[offsetB % 4u])`))}
502          `}else x=y.setByOffset("global_idx",_(b.getByOffset("global_idx"),S.getByOffset("global_idx")));else{if(!n)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let w=(I,k,E="")=>{let C=`aData[indexA${k}][componentA${k}]`,A=`bData[indexB${k}][componentB${k}]`;return`
503            let outputIndices${k} = ${y.offsetToIndices(`global_idx * 4u + ${k}u`)};
504            let offsetA${k} = ${b.broadcastedIndicesToOffset(`outputIndices${k}`,y)};
505            let offsetB${k} = ${S.broadcastedIndicesToOffset(`outputIndices${k}`,y)};
506            let indexA${k} = offsetA${k} / 4u;
507            let indexB${k} = offsetB${k} / 4u;
508            let componentA${k} = offsetA${k} % 4u;
509            let componentB${k} = offsetB${k} % 4u;
510            ${I}[${k}] = ${E}(${g(C,A)});
511          `};f===9?x=`
512            var data = vec4<u32>(0);
513            ${w("data",0,"u32")}
514            ${w("data",1,"u32")}
515            ${w("data",2,"u32")}
516            ${w("data",3,"u32")}
517            outputData[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:x=`
518            ${w("outputData[global_idx]",0)}
519            ${w("outputData[global_idx]",1)}
520            ${w("outputData[global_idx]",2)}
521            ${w("outputData[global_idx]",3)}
522          `}return`
523        ${e.registerUniform("vec_size","u32").declareVariables(b,S,y)}
524
525        ${h??""}
526
527        ${e.mainStart()}
528        ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
529        ${x}
530      }`},bu=(e,t,r,i,a,n,s=r.dataType)=>{let u=r.dims.map(Number),l=i.dims.map(Number),p=!O.areEqual(u,l),f=u,h=O.size(u),g=!1,_=!1,y=[p];if(p){let b=Vt.calcShape(u,l,!1);if(!b)throw new Error("Can't perform binary op on the given tensors");f=b.slice(),h=O.size(f);let S=O.size(u)===1,x=O.size(l)===1,w=u.length>0&&u[u.length-1]%4===0,I=l.length>0&&l[l.length-1]%4===0;y.push(S),y.push(x),y.push(w),y.push(I);let k=1;for(let E=1;E<f.length;E++){let C=u[u.length-E],A=l[l.length-E];if(C===A)k*=C;else break}k%4===0?(_=!0,g=!0):(S||x||w||I)&&(g=!0)}else g=!0;return y.push(g),{name:e,shaderCache:{hint:t+y.map(b=>b.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:b=>yu(b,u,l,f,g,p,_,a,r.dataType,i.dataType,s,n),getRunData:()=>({outputs:[{dims:f,dataType:s}],dispatchGroup:{x:Math.ceil(h/64/4)},programUniforms:[{type:12,data:Math.ceil(O.size(f)/4)},...J(u,l,f)]})}},Ze=(e,t,r,i,a,n)=>{e.compute(bu(t,a??"",e.inputs[0],e.inputs[1],r,i,n))},Tc=e=>{Ze(e,"Add",(t,r)=>`${t}+${r}`)},Ic=e=>{Ze(e,"Div",(t,r)=>`${t}/${r}`)},Ec=e=>{Ze(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4<u32>(${t}==${r})`},void 0,void 0,9)},zc=e=>{Ze(e,"Mul",(t,r)=>`${t}*${r}`)},Cc=e=>{let t=N("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;Ze(e,"Pow",{scalar:(r,i)=>`pow_custom(${r},${i})`,vector:(r,i)=>`pow_vector_custom(${r},${i})`},`
531    fn pow_custom(a : ${t}, b : ${t}) -> ${t} {
532      if (b == ${t}(0.0)) {
533        return ${t}(1.0);
534      } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) {
535        return ${t}(pow(f32(a), f32(b))); // NaN
536      }
537      return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b))));
538    }
539    fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>
539) -> vec4<${t}> {
540      // TODO: implement vectorized pow
541      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));
542    }
543      `)},Ac=e=>{Ze(e,"Sub",(t,r)=>`${t}-${r}`)},Oc=e=>{Ze(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},Rc=e=>{Ze(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},Bc=e=>{Ze(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},Nc=e=>{Ze(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}}),wu,$u,vu,xu,Mc,Dc,n_=U(()=>{te(),ie(),xe(),ae(),wu=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,i=e[r],a=i.dataType,n=i.dims.length;e.forEach((s,u)=>{if(u!==r){if(s.dataType!==a)throw new Error("input tensors should be one type");if(s.dims.length!==n)throw new Error("input tensors should have the same shape");s.dims.forEach((l,p)=>{if(p!==t&&l!==i.dims[p])throw new Error("non concat dimensions must match")})}})},$u=(e,t)=>`
544  fn calculateInputIndex(index: u32) -> u32 {
545    let sizeInConcatAxis = array<u32, ${e}u>(${t});
546    for (var i: u32 = 0u; i < ${e}; i += 1u ) {
547      if (index < sizeInConcatAxis[i]) {
548        return i;
549      }
550    }
551    return ${e}u;
552  }`,vu=(e,t)=>{let r=e.length,i=[];for(let a=0;a<r;++a){let n=t.setByOffset("global_idx",e[a].getByIndices("indices"));r===1?i.push(n):a===0?i.push(`if (inputIndex == ${a}u) { ${n} }`):a===r-1?i.push(`else { ${n} }`):i.push(`else if (inputIndex == ${a}) { ${n} }`)}return i.join(`
553`)},xu=(e,t,r,i)=>{let a=O.size(r),n=new Array(e.length),s=new Array(e.length),u=0,l=[],p=[],f=[{type:12,data:a}];for(let b=0;b<e.length;++b)u+=e[b].dims[t],n[b]=u,p.push(e[b].dims.length),s[b]=N(`input${b}`,i,p[b]),l.push("rank"),f.push({type:12,data:n[b]});for(let b=0;b<e.length;++b)f.push(...J(e[b].dims));f.push(...J(r));let h=X("output",i,r.length),g=h.indicesGet("indices",t),_=Array.from(Array(n.length).keys()).map(b=>`uniforms.sizeInConcatAxis${b}`).join(","),y=b=>`
554
555  ${(()=>{b.registerUniform("outputSize","u32");for(let S=0;S<e.length;S++)b.registerUniform(`sizeInConcatAxis${S}`,"u32");return b.declareVariables(...s,h)})()}
556
557  ${$u(n.length,_)}
558
559  ${b.mainStart()}
560    ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
561
562    var indices = ${h.offsetToIndices("global_idx")};
563
564    let inputIndex = calculateInputIndex(${g});
565    if (inputIndex != 0u) {
566      let sizeInConcatAxis = array<u32, ${n.length}u>(${_});
567      ${g} -= sizeInConcatAxis[inputIndex - 1u];
568    }
569
570    ${vu(s,h)}
571  }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:r,dataType:i}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:f}),getShaderSource:y}},Mc=(e,t)=>{let r=e.inputs,i=r[0].dims,a=O.normalizeAxis(t.axis,i.length);wu(r,a);let n=i.slice();n[a]=r.reduce((u,l)=>u+(l.dims.length>a?l.dims[a]:0),0);let s=r.filter(u=>O.size(u.dims)>0);e.compute(xu(s,a,n,r[0].dataType),{inputs:s})},Dc=e=>fe({axis:e.axis})}),Ot,Rt,Bt,ja,Mt=U(()=>{te(),ie(),Ot=(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));
572              value = sign(value) * (1.0 - e2x) / (1.0 + e2x);
573        `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},Rt=(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})},Bt=(e,t)=>
vendor: 13,556 bytes, lines 573-835
573{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"})},ja=e=>{let t=(e==null?void 0:e.activation)||"";if(t==="HardSigmoid"){let[r,i]=(e==null?void 0:e.activation_params)||[.2,.5];return{activation:t,alpha:r,beta:i}}else if(t==="Clip"){let[r,i]=(e==null?void 0:e.activation_params)||[lp,dp];return{activation:t,clipMax:i,clipMin:r}}else if(t==="LeakyRelu"){let[r]=(e==null?void 0:e.activation_params)||[.01];return{activation:t,alpha:r}}return{activation:t}}}),Ee,Uc,Ka=U(()=>{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.`)}},Uc=e=>`
574      ${e?"value = value + getBiasByOutputCoords(coords);":""}
575      `}),Pc,s_=U(()=>{Pc=e=>`
576fn getIndexFromCoords4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 {
577  return dot(coords, vec4<i32>(
578      shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));
579}
580fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
581  return dot(coords, vec4<i32>(
582    i32(${e}.x), i32(${e}.y), i32(${e}.z), 1));
583}
584`}),pr,Za,Qa=U(()=>{te(),ie(),ae(),Mt(),pr=(e,t,r,i,a)=>{let n=i-r;return`
585      ${Array.from({length:r}).map((s,u)=>`
586      if (${Y(t.shape,u,t.rank)} != 1) {
587        ${t.indicesSet(e,u,Y(a,u+n,i))}
588      } else {
589        ${t.indicesSet(e,u,0)}
590      }`).join("")}
591`},Za=(e,t,r,i,a=!1,n)=>{let s=e[0].dims,u=e[1].dims,l=s[s.length-2],p=u[u.length-1],f=s[s.length-1],h=ve(p),g=ve(f),_=ve(l),y=O.size(r)/h/_,b=e.length>2,S=i?i.slice(0,-2):r.slice(0,-2),x=[O.size(S),l,p],w=[{type:12,data:y},{type:12,data:l},{type:12,data:p},{type:12,data:f}];Rt(t,w),w.push(...J(S,s,u)),b&&w.push(...J(e[2].dims)),w.push(...J(x));let I=k=>{let E=Va("batch_dims",e[0].dataType,S.length),C=N("a",e[0].dataType,s.length,g),A=N("b",e[1].dataType,u.length,h),v=X("output",e[0].dataType,x.length,h),M=Te(v.type.tensor),D=Ot(t,v.type.value,M),H=[C,A],F="";if(b){let K=a?h:1;H.push(N("bias",e[2].dataType,e[2].dims.length,K)),F=`${a?`value += bias[col / ${K}];`:`value += ${v.type.value}(bias[row + i]);`}`}let j=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];Bt(t,j);let R=()=>{let K=`var a_data: ${C.type.value};`;for(let Z=0;Z<g;Z++)K+=`
592              let b_data${Z} = b[(b_offset + (k + ${Z}) * uniforms.N + col) / ${h}];`;for(let Z=0;Z<_;Z++){K+=`a_data = a[(a_offset + (row + ${Z}) * uniforms.K + k) / ${g}];`;for(let ee=0;ee<g;ee++)K+=`
593            values[${Z}] = fma(${A.type.value}(a_data${g===1?"":`[${ee}]`}), b_data${ee}, values[${Z}]);
594`}return K};return`
595  ${k.registerUniforms(j).registerInternalVariables(E).declareVariables(...H,v)}
596  ${k.mainStart()}
597    ${k.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
598    let col = (global_idx % (uniforms.N / ${h})) * ${h};
599    var index1 = global_idx / (uniforms.N / ${h});
600    let stride1 = uniforms.M / ${_};
601    let row = (index1 % stride1) * ${_};
602    let batch = index1 / stride1;
603
604    ${r.length===2?"":`let batch_indices = ${E.offsetToIndices("batch")};`}
605
606    var a_indices: ${C.type.indices};
607    ${pr("a_indices",C,C.rank-2,E.rank,"batch_indices")}
608    ${C.indicesSet("a_indices",C.rank-2,0)}
609    ${C.indicesSet("a_indices",C.rank-1,0)}
610    let a_offset = ${C.indicesToOffset("a_indices")};
611
612    var b_indices: ${A.type.indices};
613    ${pr("b_indices",A,A.rank-2,E.rank,"batch_indices")}
614    ${A.indicesSet("b_indices",A.rank-2,0)}
615    ${A.indicesSet("b_indices",A.rank-1,0)}
616    let b_offset = ${A.indicesToOffset("b_indices")};
617    var values: array<${v.type.value}, ${_}>;
618    for (var k: u32 = 0u; k < uniforms.K; k = k + ${g}) {
619      ${R()}
620    }
621    for (var i = 0u; i < ${_}u; i++) {
622      var value = values[i];
623      ${F}
624      ${D}
625      let cur_indices = ${v.type.indices}(batch, row + i, col);
626      let offset = ${v.indicesToOffset("cur_indices")};
627      ${v.setByOffset(`offset / ${h}`,"value")};
628    }
629  }
630  `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${h};${g};${_};${a}`,inputDependencies:b?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:n?n(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:w}),getShaderSource:I}}}),Su,ku,Sa,Li,Tu,ka,Iu,jr,Xa=U(()=>{te(),ie(),ae(),Mt(),Qa(),Ka(),Su=(e,t)=>e?`
631        mm_Asub[inputRow][inputCol] = mm_readA(batch,
632          kStart + inputRow,
633          globalRowStart / innerElementSize + inputCol${t?", batchIndices":""});
634        `:`
635        mm_Asub[inputRow][inputCol] = mm_readA(batch,
636          globalRow + innerRow,
637          kStart / innerElementSize + inputCol${t?", batchIndices":""});
638        `,ku=(e,t)=>e?`
639        let ACached0 = mm_Asub[k * innerElementSize][localRow];
640        let ACached1 = mm_Asub[k * innerElementSize + 1][localRow];
641        let ACached2 = mm_Asub[k * innerElementSize + 2][localRow];
642        ${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"}
643        for (var i = 0; i < rowPerThread; i = i + 1) {
644          acc[i] = BCached0 * ACached0[i] + acc[i];
645          acc[i] = BCached1 * ACached1[i] + acc[i];
646          acc[i] = BCached2 * ACached2[i] + acc[i];
647          ${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"}
648        }`:`
649        for (var i = 0; i < rowPerThread; i = i + 1) {
650          let ACached = mm_Asub[tileRow + i][k];
651          acc[i] = BCached0 * ACached.x + acc[i];
652          acc[i] = BCached1 * ACached.y + acc[i];
653          acc[i] = BCached2 * ACached.z + acc[i];
654          ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"}
655        }`,Sa=(e,t,r="f32",i,a=!1,n=32,s=!1,u=32)=>{let l=t[1]*e[1],p=t[0]*e[0],f=a?l:n,h=a?n:l,g=f/t[0],_=n/t[1];if(!((a&&g===4&&e[1]===4||!a&&(g===3||g===4))&&f%t[0]===0&&n%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${a} is true, innerElementSize ${g} and workPerThread[1] ${e[1]} must be 4.
656      Otherwise, innerElementSize ${g} must be 3 or 4.
657  tileAWidth ${f} must be divisible by workgroupSize[0]${t[0]}. tileInner ${n} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return`
658var<workgroup> mm_Asub: array<array<vec${g}<${r}>, ${f/g}>, ${h}>;
659var<workgroup> mm_Bsub: array<array<vec4<${r}>, ${p/e[0]}>, ${n}>;
660
661const rowPerThread = ${e[1]};
662const colPerThread = ${e[0]};
663const innerElementSize = ${g};
664const tileInner = ${n};
665
666@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})
667fn main(@builtin(local_invocation_id) localId : vec3<u32>,
668        @builtin(global_invocation_id) globalId : vec3<u32>,
669        @builtin(workgroup_id) workgroupId : vec3<u32>) {
670  let localRow = i32(localId.y);
671  let tileRow = localRow * rowPerThread;
672  let tileCol = i32(localId.x);
673
674  let globalRow =i32(globalId.y) * rowPerThread;
675  let globalCol = i32(globalId.x);
676  let batch = ${s?"0":"i32(globalId.z)"};
677  ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""}
678  let globalRowStart = i32(workgroupId.y) * ${l};
679
680  let num_tiles = ${s?`${Math.ceil(u/n)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};
681  var kStart = ${s?`i32(globalId.z) * ${u}`:"0"};
682
683  var acc: array<vec4<${r}>, rowPerThread>;
684
685  // Loop over shared dimension.
686  let tileRowB = localRow * ${_};
687  for (var t = 0; t < num_tiles; t = t + 1) {
688      // Load one tile of A into local memory.
689      for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
690          let inputRow = tileRow + innerRow;
691          let inputCol = tileCol;
692          ${Su(a,i)}
693      }
694
695      // Load one tile of B into local memory.
696      for (var innerRow = 0; innerRow < ${_}; innerRow = innerRow + 1) {
697          let inputRow = tileRowB + innerRow;
698          let inputCol = tileCol;
699          mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${i?", batchIndices":""});
700      }
701      kStart = kStart + tileInner;
702      workgroupBarrier();
703
704      // Compute acc values for a single thread.
705      for (var k = 0; k < tileInner / innerElementSize; k = k + 1) {
706          let BCached0 = mm_Bsub[k * innerElementSize][tileCol];
707          let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol];
708          let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol];
709          ${g===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"}
710
711          ${ku(a,g)}
712      }
713
714      workgroupBarrier();
715  }
716
717  for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
718      mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]);
719  }
720}`},Li=(e,t)=>e?`
721            mm_Asub[inputRow][inputCol] = mm_readA(batch,
722              kStart + inputRow,
723              globalRowStart + inputCol${t?", batchIndices":""});
724            `:`
725            mm_Asub[inputRow][inputCol] = mm_readA(batch,
726              globalRowStart + inputRow,
727              kStart + inputCol${t?", batchIndices":""});
728            `,Tu=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",ka=(e,t,r="f32",i,a=!1,n=32,s=!1,u=32,l=!1)=>{let p=e[1]*t[1],f=e[0]*t[0],h=a?p:n,g=a?n:p;if(!(g%t[1]===0&&h%t[0]===0&&n%t[1]===0))throw new Error(`tileAHight ${g} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${h} must be divisible by workgroupSize[0]${t[0]}, tileInner ${n} must be divisible by workgroupSize[1]${t[1]}`);let _=g/t[1],y=h/t[0],b=n/t[1],S=l?`
729    let localRow = i32(localId.y);
730    let localCol = i32(localId.x);
731    let globalRowStart = i32(workgroupId.y) * ${p};
732    let globalColStart = i32(workgroupId.x) * ${f};
733
734    // Loop over shared dimension.
735    for (var t = 0; t < num_tiles; t = t + 1) {
736      // Load one tile of A into local memory.
737      for (var inputRow = localRow; inputRow < ${g}; inputRow = inputRow + ${t[1]}) {
738        for (var inputCol = localCol; inputCol < ${h}; inputCol = inputCol + ${t[0]}) {
739          ${Li(a,i)}
740        }
741      }
742      // Load one tile of B into local memory.
743      for (var inputRow = localRow; inputRow < ${n}; inputRow = inputRow + ${t[1]}) {
744            for (var inputCol = localCol; inputCol < ${f}; inputCol = inputCol + ${t[0]}) {
745          mm_Bsub[inputRow][inputCol] = mm_readB(batch,
746            kStart + inputRow,
747            globalColStart + inputCol${i?", batchIndices":""});
748        }
749      }
750      kStart = kStart + tileInner;
751      workgroupBarrier();
752
753      // Compute acc values for a single thread.
754      var BCached : array<${r}, colPerThread>;
755      for (var k = 0; k < tileInner; k = k + 1) {
756        for (var inner = 0; inner < colPerThread; inner = inner + 1) {
757          BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}];
758        }
759        for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
760          let ACached = ${a?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`}
761          for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
762            acc[innerRow][innerCol] = acc[innerRow][innerCol] +
763                ACached * BCached[innerCol];
764          }
765        }
766      }
767      workgroupBarrier();
768    }
769    for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
770      let gRow = globalRowStart + localRow + innerRow * ${t[1]};
771      for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
772        let gCol = globalColStart + localCol + innerCol * ${t[0]};
773        mm_write(batch, gRow, gCol, acc[innerRow][innerCol]);
774      }
775    }
776    `:`
777let tileRow = i32(localId.y) * rowPerThread;
778let tileCol = i32(localId.x) * colPerThread;
779
780let globalRow = i32(globalId.y) * rowPerThread;
781let globalCol = i32(globalId.x) * colPerThread;
782let globalRowStart = i32(workgroupId.y) * ${p};
783
784let tileRowA = i32(localId.y) * ${_};
785let tileColA = i32(localId.x) * ${y};
786let tileRowB = i32(localId.y) * ${b};
787// Loop over shared dimension.
788for (var t = 0; t < num_tiles; t = t + 1) {
789  // Load one tile of A into local memory.
790  for (var innerRow = 0; innerRow < ${_}; innerRow = innerRow + 1) {
791    for (var innerCol = 0; innerCol < ${y}; innerCol = innerCol + 1) {
792      let inputRow = tileRowA + innerRow;
793      let inputCol = tileColA + innerCol;
794      ${Li(a,i)}
795    }
796  }
797
798  // Load one tile of B into local memory.
799  for (var innerRow = 0; innerRow < ${b}; innerRow = innerRow + 1) {
800    for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
801      let inputRow = tileRowB + innerRow;
802      let inputCol = tileCol + innerCol;
803      mm_Bsub[inputRow][inputCol] = mm_readB(batch,
804        kStart + inputRow,
805        globalCol + innerCol${i?", batchIndices":""});
806    }
807  }
808  kStart = kStart + tileInner;
809  workgroupBarrier();
810
811  // Compute acc values for a single thread.
812  var BCached : array<${r}, colPerThread>;
813  for (var k = 0; k < tileInner; k = k + 1) {
814    for (var inner = 0; inner < colPerThread; inner = inner + 1) {
815      BCached[inner] = mm_Bsub[k][tileCol + inner];
816    }
817
818    for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
819      ${Tu(a)}
820      for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
821        acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];
822      }
823    }
824  }
825
826  workgroupBarrier();
827}
828
829for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
830  for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
831    mm_write(batch, globalRow + innerRow, globalCol + innerCol,
832        acc[innerRow][innerCol]);
833  }
834}
835`;return`
836  var<workgroup> mm_Asub : array<array<${r}, ${h}>, ${g}>;
837  var<workgroup> mm_Bsub : array<array<${r}, ${f}>, ${n}>;
838  const rowPerThread = ${e[1]};
839  const colPerThread = ${e[0]};
840  const tileInner = ${n};
841
842@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})
843fn main(@builtin(local_invocation_id) localId : vec3<u32>,
844        @builtin(global_invocation_id) globalId : vec3<u32>,
845        @builtin(workgroup_id) workgroupId : vec3<u32>) {
846    let batch = ${s?"0":"i32(globalId.z)"};
847    ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""}
848    let num_tiles = ${s?`${Math.ceil(u/n)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};
849    var kStart = ${s?`i32(globalId.z) * ${u}`:"0"};
850
851    var acc : array<array<${r}, colPerThread>, rowPerThread>;
852    ${S}
853  }
854`},Iu=(e,t,r,i,a=!1)=>{let[n,s,u,l]=i,p=Te(i[0].type.tensor);return`
855    fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${n.type.indices}) -> ${Ee(e,p)} {
856      var value = ${Ee(e,p)}(0.0);
857      let col = colIn * ${e};
858      if(row < uniforms.dim_a_outer && col < uniforms.dim_inner)
859      {
860        var aIndices: ${s.type.indices};
861        ${pr("aIndices",s,s.rank-2,n.rank,"batchIndices")}
862        ${s.indicesSet("aIndices",s.rank-2,"u32(row)")}
863        ${s.indicesSet("aIndices",s.rank-1,"u32(colIn)")}
864        value = ${s.getByIndices("aIndices")};
865      }
866      return value;
867    }
868
869    fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${n.type.indices}) -> ${Ee(e,p)} {
870      var value = ${Ee(e,p)}(0.0);
871      let col = colIn * ${e};
872      if(row < uniforms.dim_inner && col < uniforms.dim_b_outer)
873      {
874        var bIndices: ${u.type.indices};
875        ${pr("bIndices",u,u.rank-2,n.rank,"batchIndices")}
876        ${u.indicesSet("bIndices",u.rank-2,"u32(row)")}
877        ${u.indicesSet("bIndices",u.rank-1,"u32(colIn)")}
878        value = ${u.getByIndices("bIndices")};
879      }
880      return value;
881    }
882
883    fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Ee(e,p)}) {
884      let col = colIn * ${e};
885      if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) {
886        var value = valueIn;
887        let coords = vec3<i32>(batch, row, colIn);
888        ${t?`value = value + ${a?"bias[colIn]":`${Ee(e,p)}(bias[row])`};`:""}
889        ${r}
890        ${l.setByIndices("vec3<u32>(coords)","value")}
891      }
892    }
893    `},jr=(e,t,r,i,a=!1,n)=>{let s=e[0].dims,u=e[1].dims,l=s.slice(0,-2),p=u.slice(0,-2),f=i?i.slice(0,-2):r.slice(0,-2),h=O.size(f),g=s[s.length-2],_=s[s.length-1],y=u[u.length-1],b=_%4===0&&y%4===0,S=g<=8?[4,1,1]:[4,4,1],x=[8,8,1],w=[Math.ceil(y/x[0]/S[0]),Math.ceil(g/x[1]/S[1]),Math.ceil(h/x[2]/S[2])],I=b?4:1,k=[...l,g,_/I],E=k.length,C=[...p,_,y/I],A=C.length,v=[h,g,y/I],M=[{type:6,data:g},{type:6,data:y},{type:6,data:_}];Rt(t,M),M.push(...J(f,k,C));let D=["rank","rank"],H=e.length>2;H&&(M.push(...J(e[2].dims)),D.push("rank")),M.push(...J(v));let F=j=>{let R=f.length,K=Va("batchDims",e[0].dataType,R,1),Z=Te(e[0].dataType),ee=N("a",e[0].dataType,E,I),he=N("b",e[1].dataType,A,I),W=X("result",e[0].dataType,v.length,I),ue=[ee,he];if(H){let me=a?I:1;ue.push(N("bias",e[2].dataType,e[2].dims.length,me))}let P=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];Bt(t,P);let V=Te(W.type.tensor),Q=Ot(t,W.type.value,V),q=Iu(I,H,Q,[K,ee,he,W],a);return`
894  ${j.registerUniforms(P).registerInternalVariables(K).declareVariables(...ue,W)}
895  ${q}
896  ${b?Sa(S,x,Z,K):ka(S,x,Z,K)}
897                   `};return{name:"MatMul",shaderCache:{hint:`${S};${t.activation};${b};${a}`,inputDependencies:D},getRunData:()=>({outputs:[{dims:n?n(r):r,dataType:e[0].dataType}],dispatchGroup:{x:w[0],y:w[1],z:w[2]},programUniforms:M}),getShaderSource:F}}}),Eu,qc,o_=U(()=>{te(),ot(),ae(),Mt(),Ka(),s_(),Xa(),Eu=(e,t,r,i,a=!1,n,s=4,u=4,l=4,p="f32")=>{let f=M=>{switch(M){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 ${M} is not supported.`)}},h=M=>{switch(M){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 ${M} is not supported.`)}},g=e?`
898    let coord = vec4<i32>(batch, xRow, xCol, xCh);
899    `:`
900    let coord = vec4<i32>(batch, xCh, xRow, xCol);
901    `,_=e?`
902    let coords = vec4<i32>(
903      batch,
904      row / outWidth,
905      row % outWidth,
906      col);
907    `:`
908    let coords = vec4<i32>(
909      batch,
910      row,
911      col / outWidth,
912      col % outWidth);
913    `,y=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",b=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",S=e?"row":"col",x=e?"col":"row",w=`
914    let inChannels = i32(uniforms.w_shape[2]);
915    let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
916    let outRow = ${S} / outWidth;
917    let outCol = ${S} % outWidth;
918
919    let WRow = ${x} / (i32(uniforms.w_shape[1]) * inChannels);
920    let WCol = ${x} / inChannels % i32(uniforms.w_shape[1]);
921    let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];
922    let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];
923    let xCh = ${x} % inChannels;
924    var resData = ${Ee(s,p)}(0.0);
925    // The bounds checking is always needed since we use it to pad zero for
926    // the 'same' padding type.
927    if (xRow >= 0 && xRow < ${y} && xCol >= 0 && xCol < ${b}) {
928      ${g}
929      let xIndex = getIndexFromCoords4D(coord, vec4<i32>(uniforms.x_shape));
930      ${f(s)}
931    }
932    return resData;`,I=e?t&&i?`
933    let col = colIn * ${s};
934    ${w}`:`
935    let col = colIn * ${s};
936    if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {
937      ${w}
938    }
939    return ${Ee(s,p)}(0.0);`:i&&r?`
940    let col = colIn * ${s};
941    ${w}`:`
942    let col = colIn * ${s};
943    if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
944      ${w}
945    }
946    return ${Ee(s,p)}(0.0);`,k=e?i&&r?h(u):`
947    let col = colIn * ${u};
948    if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
949      ${h(u)}
950    }
951    return ${Ee(u,p)}(0.0);`:`
952    let col = colIn * ${u};
953    if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) {
954      ${h(u)}
955    }
956    return ${Ee(u,p)}(0.0);`,E=Ee(l,p),C=Ee(e?s:u,p),A=Ee(e?u:s,p),v=Ot(n,E,p);return`
957    fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${C} {
958      ${e?I:k}
959    }
960
961    fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${A} {
962      ${e?k:I}
963    }
964
965    fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${E}) {
966      let col = colIn * ${l};
967      if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer)
968      {
969      var value = valueIn;
970      let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
971      ${_}
972      ${Uc(a)}
973      ${v}
974      setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);
975      }
976    }`},qc=(e,t,r,i,a,n,s,u,l)=>{let p=t.format==="NHWC",f=p?e[0].dims[3]:e[0].dims[1],h=r[0],g=p?r[2]:r[3],_=p?r[1]:r[2],y=p?r[3]:r[1],b=p&&(f%4===0||f%3===0)&&y%4===0,S=p?y:g*_,x=p?g*_:y,w=[8,8,1],I=i<=8?[4,1,1]:[4,4,1],k=[Math.ceil(S/w[0]/I[0]),Math.ceil(x/w[1]/I[1]),Math.ceil(h/w[2]/I[2])];de("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${k}`);let E=b?p&&f%4!==0?3:4:1,C=w[1]*I[1],A=w[0]*I[0],v=Math.max(w[0]*E,w[1]),M=i%C===0,D=a%A===0,H=n%v===0,F=b?[E,4,4]:[1,1,1],j=[{type:6,data:i},{type:6,data:a},{type:6,data:n},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];Rt(t,j),j.push(...J(e[0].dims,e[1].dims));let R=["rank","rank"];s&&(j.push(...J(e[2].dims)),R.push("rank")),j.push(...J(r));let K=Z=>{let ee=[{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}];Bt(t,ee);let he=b?4:1,W=Te(e[0].dataType),ue=`
977      fn setOutputAtIndex(flatIndex : i32, value : ${b?`vec4<${W}>`:W}) {
978        result[flatIndex] = ${b?`vec4<${W}>`:W}(value);
979      }
980      fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${b?`vec4<${W}>`:W}) {
981        let flatIndex = getOutputIndexFromCoords(vec4<i32>(d0, d1, d2, d3));
982        setOutputAtIndex(flatIndex ${b?"/ 4":""}, value);
983      }`,P=N("x",e[0].dataType,e[0].dims.length,E===3?1:E),V=N("w",e[1].dataType,e[1].dims.length,he),Q=[P,V],q=X("result",e[0].dataType,r.length,he);if(s){let me=N("bias",e[2].dataType,e[2].dims.length,he);Q.push(me),ue+=`
984        fn getBiasByOutputCoords(coords : vec4<i32>) -> ${b?`vec4<${W}>`:W} {
985          return bias[coords.${p?"w":"y"}${b?"/ 4":""}];
986        }`}return`
987        ${Pc("uniforms.result_strides")}
988        //struct Uniforms { xShape : vec4<i32>, wShape : vec4<i32>, outShape : vec4<i32>,
989        //  outShapeStrides: vec3<i32>, filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>,
990        //  dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32 };
991        ${Z.registerUniforms(ee).declareVariables(...Q,q)}
992        ${ue}
993        ${Eu(p,M,D,H,s,t,F[0],F[1],F[2],W)}
994        ${b?Sa(I,w,W,void 0,!p,v):ka(I,w,W,void 0,!p,v,!1,void 0,u)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${E};${b};${M};${D};${H};${C};${A};${v}`,inputDependencies:R},getRunData:()=>({outputs:[{dims:l?l(r):r,dataType:e[0].dataType}],dispatchGroup:{x:k[0],y:k[1],z:k[2]},programUniforms:j}),getShaderSource:K}}}),zu,Wi,tr,Cu,Vi,Au,Lc,Wc,u_=U(()=>{te(),ot(),ie(),ae(),Mt(),Ka(),zu=e=>{let t=1;for(let r=0;r<e.length;r++)t*=e[r];return t},Wi=e=>typeof e=="number"?[e,e,e]:e,tr=(e,t)=>t<=1?e:e+(e-1)*(t-1),Cu=(e,t,r,i=1)=>{let a=tr(t,i);return Math.floor((e[0]*(r-1)-r+a)/2)},Vi=(e,t,r,i,a)=>{a==null&&(a=Cu(e,t[0],i[0]));let n=[0,0,0,r];for(let s=0;s<3;s++)e[s]+2*a>=t[s]&&(n[s]=Math.trunc((e[s]-t[s]+2*a)/i[s]+1));return n},Au=(e,t,r,i,a,n,s,u,l,p)=>{let f,h,g,_;if(e==="VALID"&&(e=0),typeof e=="number"){f={top:e,bottom:e,left:e,right:e,front:e,back:e};let y=Vi([t,r,i,1],[u,l,p],1,[a,n,s],e);h=y[0],g=y[1],_=y[2]}else if(Array.isArray(e)){if(!e.every((b,S,x)=>b===x[0]))throw Error(`Unsupported padding parameter: ${e}`);f={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let y=Vi([t,r,i,1],[u,l,p],1,[a,n,s],e[0]);h=y[0],g=y[1],_=y[2]}else if(e==="SAME_UPPER"){h=Math.ceil(t/a),g=Math.ceil(r/n),_=Math.ceil(i/s);let y=(h-1)*a+u-t,b=(g-1)*n+l-r,S=(_-1)*s+p-i,x=Math.floor(y/2),w=y-x,I=Math.floor(b/2),k=b-I,E=Math.floor(S/2),C=S-E;f={top:I,bottom:k,left:E,right:C,front:x,back:w}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:f,outDepth:h,outHeight:g,outWidth:_}},Lc=(e,t,r,i,a,n=!1,s="channelsLast")=>{let u,l,p,f,h;if(s==="channelsLast")[u,l,p,f,h]=e;else if(s==="channelsFirst")[u,h,l,p,f]=e;else throw new Error(`Unknown dataFormat ${s}`);let[g,,_,y,b]=t,[S,x,w]=Wi(r),[I,k,E]=Wi(i),C=tr(_,I),A=tr(y,k),v=tr(b,E),{padInfo:M,outDepth:D,outHeight:H,outWidth:F}=Au(a,l,p,f,S,x,w,C,A,v),j=n?g*h:g,R=[0,0,0,0,0];return s==="channelsFirst"?R=[u,j,D,H,F]:s==="channelsLast"&&(R=[u,D,H,F,j]),{batchSize:u,dataFormat:s,inDepth:l,inHeight:p,inWidth:f,inChannels:h,outDepth:D,outHeight:H,outWidth:F,outChannels:j,padInfo:M,str
vendor: 5,019 bytes, lines 994-1065
994ideDepth:S,strideHeight:x,strideWidth:w,filterDepth:_,filterHeight:y,filterWidth:b,effectiveFilterDepth:C,effectiveFilterHeight:A,effectiveFilterWidth:v,dilationDepth:I,dilationHeight:k,dilationWidth:E,inShape:e,outShape:R,filterShape:t}},Wc=(e,t,r,i,a,n)=>{let s=n==="channelsLast";s?e[0].dims[3]:e[0].dims[1];let u=[64,1,1],l={x:r.map((S,x)=>x)},p=[Math.ceil(zu(l.x.map(S=>r[S]))/u[0]),1,1];de("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${p}`);let f=1,h=O.size(r),g=[{type:12,data:h},{type:12,data:i},{type:12,data:a},{type:12,data:t.strides},{type:12,data:t.dilations}];Rt(t,g),g.push(...J(e[0].dims,e[1].dims));let _=["rank","rank"],y=e.length===3;y&&(g.push(...J(e[2].dims)),_.push("rank")),g.push(...J(r));let b=S=>{let x=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:i.length},{name:"pads",type:"u32",length:a.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];Bt(t,x);let w=1,I=Te(e[0].dataType),k=N("x",e[0].dataType,e[0].dims.length,f),E=N("W",e[1].dataType,e[1].dims.length,w),C=[k,E],A=X("result",e[0].dataType,r.length,w),v="";if(y){let H=N("bias",e[2].dataType,e[2].dims.length,w);C.push(H),v+=`
995        fn getBiasByOutputCoords(coords : array<u32, 5>) -> ${I} {
996          return bias[${s?Y("coords",4,5):Y("coords",1,5)}];
997        }`}let M=Ee(f,I),D=Ot(t,M,I);return`
998            ${v}
999            fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
1000              let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
1001              return ${k.getByIndices("aIndices")};
1002            }
1003            fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
1004              let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
1005              return ${E.getByIndices("aIndices")};
1006            }
1007          ${S.registerUniforms(x).declareVariables(...C,A)}
1008          ${S.mainStart()}
1009          ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1010              let coords = ${A.offsetToIndices("global_idx")};
1011              let batch = ${Y("coords",0,k.rank)};
1012              let d2 = ${s?Y("coords",k.rank-1,k.rank):Y("coords",1,k.rank)};
1013              let xFRCCorner = vec3<u32>(${s?Y("coords",1,k.rank):Y("coords",2,k.rank)},
1014              ${s?Y("coords",2,k.rank):Y("coords",3,k.rank)},
1015              ${s?Y("coords",3,k.rank):Y("coords",4,k.rank)}) * uniforms.strides - uniforms.pads;
1016              let xFCorner = xFRCCorner.x;
1017              let xRCorner = xFRCCorner.y;
1018              let xCCorner = xFRCCorner.z;
1019              let xShapeY = ${s?Y("uniforms.x_shape",1,k.rank):Y("uniforms.x_shape",2,k.rank)};
1020              let xShapeZ = ${s?Y("uniforms.x_shape",2,k.rank):Y("uniforms.x_shape",3,k.rank)};
1021              let xShapeW = ${s?Y("uniforms.x_shape",3,k.rank):Y("uniforms.x_shape",4,k.rank)};
1022              let xShapeU = ${s?Y("uniforms.x_shape",4,k.rank):Y("uniforms.x_shape",1,k.rank)};
1023              let inputDepthNearestVec4 = (xShapeU / 4) * 4;
1024              let inputDepthVec4Remainder = xShapeU % 4;
1025
1026              var value = 0.0;
1027              for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) {
1028                let xF = xFCorner + wF * uniforms.dilations[0];
1029                if (xF < 0 || xF >= xShapeY) {
1030                  continue;
1031                }
1032
1033                for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) {
1034                  let xR = xRCorner + wR * uniforms.dilations[1];
1035                  if (xR < 0 || xR >= xShapeZ) {
1036                    continue;
1037                  }
1038
1039                  for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) {
1040                    let xC = xCCorner + wC * uniforms.dilations[2];
1041                    if (xC < 0 || xC >= xShapeW) {
1042                      continue;
1043                    }
1044
1045                    for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) {
1046                      ${s?`let xValues = vec4<f32>(
1047                               getX(batch, xF, xR, xC, d1),
1048                               getX(batch, xF, xR, xC, d1 + 1),
1049                               getX(batch, xF, xR, xC, d1 + 2),
1050                               getX(batch, xF, xR, xC, d1 + 3));
1051                            `:`let xValues = vec4<f32>(
1052                               getX(batch, d1, xF, xR, xC),
1053                               getX(batch, d1 + 1, xF, xR, xC),
1054                               getX(batch, d1 + 2, xF, xR, xC),
1055                               getX(batch, d1 + 3, xF, xR, xC));
1056                            `}
1057                            let wValues = vec4<f32>(
1058                              getW(d2, d1, wF, wR, wC),
1059                              getW(d2, d1 + 1, wF, wR, wC),
1060                              getW(d2, d1 + 2, wF, wR, wC),
1061                              getW(d2, d1 + 3, wF, wR, wC));
1062                      value += dot(xValues, wValues);
1063                    }
1064                    if (inputDepthVec4Remainder == 1) {
1065                        ${s?`value += getX(batch, xF, xR, xC, inputDepthNearest
1065Vec4)
1066                          * getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC)
1067                          * getW(d2, inputDepthNearestVec4, wF, wR, wC);`}
1068                    } else if (inputDepthVec4Remainder == 2) {
1069                      ${s?`let xValues = vec2<f32>(
1070                        getX(batch, xF, xR, xC, inputDepthNearestVec4),
1071                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1));
1072                      `:`let xValues = vec2<f32>(
1073                        getX(batch, inputDepthNearestVec4, xF, xR, xC),
1074                        getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC));
1075                    `}
1076                    let wValues = vec2<f32>(
1077                      getW(d2, inputDepthNearestVec4, wF, wR, wC),
1078                      getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC));
1079                      value += dot(xValues, wValues);
1080                    } else if (inputDepthVec4Remainder == 3) {
1081                      ${s?`let xValues = vec3<f32>(
1082                        getX(batch, xF, xR, xC, inputDepthNearestVec4),
1083                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1),
1084                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2));
1085                      `:`let xValues = vec3<f32>(
1086                        getX(batch, inputDepthNearestVec4, xF, xR, xC),
1087                        getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC),
1088                        getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC));
1089                    `}
1090                    let wValues = vec3<f32>(
1091                      getW(d2, inputDepthNearestVec4, wF, wR, wC),
1092                      getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC),
1093                      getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC));
1094                      value += dot(xValues, wValues);
1095                    }
1096                  }
1097                }
1098              }
1099              ${y?"value = value + getBiasByOutputCoords(coords)":""};
1100              ${D}
1101              result[global_idx] = f32(value);
1102          }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${s};${f};${y}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:p[0],y:p[1],z:p[2]},programUniforms:g}),getShaderSource:b}}}),Vc,Gc,l_=U(()=>{te(),ie(),ae(),Mt(),Vc=(e,t,r,i)=>{let a=e.length>2,n=a?"value += b[output_channel];":"",s=e[0].dims,u=e[1].dims,l=t.format==="NHWC",p=l?r[3]:r[1],f=p/t.group,h=l&&f>=4?ve(p):1,g=O.size(r)/h,_=[{type:12,data:g},{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:f}];Rt(t,_),_.push(...J(s,[u[0],u[1],u[2],u[3]/h]));let y=a?["rank","rank","rank"]:["rank","rank"];_.push(...J([r[0],r[1],r[2],r[3]/h]));let b=S=>{let x=X("output",e[0].dataType,r.length,h),w=Te(x.type.tensor),I=Ot(t,x.type.value,w),k=N("x",e[0].dataType,s.length),E=N("w",e[1].dataType,u.length,h),C=[k,E];a&&C.push(N("b",e[2].dataType,e[2].dims,h));let A=[{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"}];Bt(t,A);let v=l?`
1103      for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) {
1104        let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];
1105
1106        if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) {
1107          continue;
1108        }
1109
1110        for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) {
1111          let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];
1112          if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) {
1113            continue;
1114          }
1115
1116          for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) {
1117            let input_channel = in_channel_offset + wInChannel;
1118            let xVal = ${k.get("batch","xHeight","xWidth","input_channel")};
1119            let wVal = ${E.get("wHeight","wWidth","wInChannel","output_channel")};
1120            value += xVal * wVal;
1121          }
1122        }
1123      }
1124      `:`
1125      for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) {
1126        let input_channel = in_channel_offset + wInChannel;
1127        for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) {
1128          let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];
1129
1130          if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) {
1131            continue;
1132          }
1133
1134          for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) {
1135            let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];
1136            if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) {
1137              continue;
1138            }
1139
1140            let xVal = ${k.get("batch","input_channel","xHeight","xWidth")};
1141            let wVal = ${E.get("output_channel","wInChannel","wHeight","wWidth")};
1142            value += xVal * wVal;
1143          }
1144        }
1145      }
1146      `;return`
1147  ${S.registerUniforms(A).declareVariables(...C,x)}
1148
1149  ${S.mainStart()}
1150    ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1151
1152    let outputIndices = ${x.offsetToIndices("global_idx")};
1153    let batch: u32 = outputIndices[0];
1154    let output_channel: u32 = outputIndices[${l?3:1}];
1155    let xRCCorner: vec2<u32> = vec2<u32>(outputIndices[${l?1:2}
1155], outputIndices[${l?2:3}]) * uniforms.strides - uniforms.pads;
1156    let group_id: u32 = output_channel * ${h} / uniforms.output_channels_per_group;
1157    var in_channel_offset = group_id * uniforms.w_shape[${l?2:1}];
1158
1159    var value: ${x.type.value} = ${x.type.value}(0);
1160    ${v}
1161    ${n}
1162    ${I}
1163    ${x.setByOffset("global_idx","value")}
1164  }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${h}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:_}),getShaderSource:b}},Gc=(e,t,r,i)=>{let a=e.length>2,n=ve(r[3]),s=ve(r[2]),u=O.size(r)/n/s,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/n],p=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/n],f=[r[0],r[1],r[2],r[3]/n],h=[{type:12,data:u},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];Rt(t,h),h.push(...J(l,p,f));let g=(s-1)*t.strides[1]+p[1],_=y=>{let b=X("output",e[0].dataType,f.length,n),S=Te(b.type.tensor),x=Ot(t,b.type.value,S),w=N("x",e[0].dataType,l.length,n),I=N("w",e[1].dataType,p.length,n),k=[w,I];a&&k.push(N("b",e[2].dataType,e[2].dims,n));let E=a?"value += b[output_channel];":"",C=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return Bt(t,C),`
1165  ${y.registerUniforms(C).declareVariables(...k,b)}
1166  ${y.mainStart()}
1167    ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1168    let width0 = uniforms.output_shape[3];
1169    let output_channel = global_idx % width0;
1170    var index1 = global_idx / width0;
1171    let width1 = uniforms.output_shape[2] / ${s}u;
1172    let col = (index1 % width1) * ${s}u;
1173    index1 = index1 / width1;
1174    let row = index1 % uniforms.output_shape[1];
1175    let batch = index1 / uniforms.output_shape[1];
1176
1177    let x_corner = vec2<i32>(i32(row), i32(col)) * uniforms.strides - uniforms.pads;
1178
1179    var x_vals: array<${w.type.value}, ${g}>;
1180    var values: array<${b.type.value}, ${s}>;
1181    let input_channel = output_channel;
1182    // Use constant instead of uniform can give better performance for w's height/width.
1183    for (var w_height: u32 = 0u; w_height < ${p[0]};
1183 w_height++) {
1184      let x_height = x_corner.x + i32(w_height);
1185      if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) {
1186        for (var i = 0; i < ${g}; i++) {
1187          let x_width = x_corner.y + i;
1188          if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) {
1189            x_vals[i] = ${w.get("batch","u32(x_height)","u32(x_width)","input_channel")};
1190          } else {
1191            x_vals[i] = ${w.type.value}(0);
1192          }
1193        }
1194        for (var w_width: u32 = 0u; w_width < ${p[1]}; w_width++) {
1195          let w_val = ${I.get("w_height","w_width","0","output_channel")};
1196          for (var i = 0u; i < ${s}u; i++) {
1197            values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]);
1198          }
1199        }
1200      }
1201    }
1202
1203    for (var i = 0u; i < ${s}u; i++) {
1204      var value = values[i];
1205      ${E}
1206      ${x}
1207      ${b.set("batch","row","col + i","output_channel","value")};
1208    }
1209  }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${n};${s};${g};${p[0]};${p[1]}`,inputDependencies:a?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:h}),getShaderSource:_}}
vendor: 37,791 bytes, lines 1209-1474
1209}),Ou,Br,Ru,Nr,Ta,Gi,Bu,Nu,Ia,d_=U(()=>{ie(),o_(),u_(),Xa(),l_(),Mt(),Qa(),yt(),Ou=(e,t,r,i,a,n)=>{let s=e[0],u=e.slice(n?1:2,n?3:4),l=u.length,p=t[0],f=t.slice(2).map((g,_)=>g+(g-1)*(r[_]-1)),h=u.map((g,_)=>g+i[_]+i[_+l]).map((g,_)=>Math.floor((g-f[_]+a[_])/a[_]));return h.splice(0,0,s),h.splice(n?3:1,0,p),h},Br=[2,3,1,0],Ru=(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],i=e[1].dims[1]*t.group;if(r!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let a=e[0].dims.length-2;if(t.dilations.length!==a)throw new Error(`dilations should be ${a}D`);if(t.strides.length!==a)throw new Error(`strides should be ${a}D`);if(t.pads.length!==a*2)throw new Error(`pads should be ${a*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},Nr=(e,t)=>{let r=e.kernelShape.slice();r.length<t[1].dims.length-2&&r.push(...Array(t[1].dims.length-2-r.length).fill(0));for(let n=2;n<t[1].dims.length;++n)r[n-2]===0&&(r[n-2]=t[1].dims[n]);let i=e.pads.slice();Hr.adjustPadsBasedOnAutoPad(t[0].dims,e.strides,e.dilations,r,i,e.format==="NHWC",e.autoPad);let a=Object.assign({},e);return Object.assign(a,{kernelShape:r,pads:i}),a},Ta=e=>{let t=ja(e),r=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],a=e.dilations,n=e.group,s=e.kernel_shape,u=e.pads,l=e.strides,p=e.w_is_const();return{autoPad:i,format:r,dilations:a,group:n,kernelShape:s,pads:u,strides:l,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},Gi=(e,t,r,i)=>{let a=r.format==="NHWC",n=Ou(t[0].dims,t[1].dims,r.dilations,r.pads,r.strides,a);if(r.group!==1){let C=[t[0]];if(a){let A=e.kernelCustomData.wT??e.compute(qe(t[1],Br),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=A),C.push(A)}else C.push(t[1]);t.length===3&&C.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&a&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1?e.compute(Gc(C,r,n,i),{inputs:C}):e.compute(Vc(C,r,n,i),{inputs:C});return}let s=t.length===3,u=t[0].dims[a?1:2],l=t[0].dims[a?2:3],p=t[0].dims[a?3:1],f=t[1].dims[2],h=t[1].dims[3],g=n[a?1:2],_=n[a?2:3],y=n[a?3:1],b=a&&f===u&&h===l&&r.pads[0]===0&&r.pads[1]===0;if(b||f===1&&h===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 C=n[0],A,v,M,D=[];if(a){let j=e.kernelCustomData.wT??e.compute(qe(t[1],Br),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=j),b){let R=u*l*p;A=t[0].reshape([1,C,R]),v=j.reshape([1,R,y]),M=[1,C,y]}else A=t[0].reshape([C,u*l,p]),v=j.reshape([1,p,y]),M=[C,g*_,y];D.push(A),D.push(v)}else A=t[0].reshape([C,p,u*l]),v=t[1].reshape([1,y,p]),M=[C,y,g*_],D.push(v),D.push(A);s&&D.push(t[2]);let H=M[2],F=D[0].dims[D[0].dims.length-1];H<8&&F<8?e.compute(Za(D,r,n,M,a,i),{inputs:D}):e.compute(jr(D,r,n,M,a,i),{inputs:D});return}let S=!0,x=e.kernelCustomData.wT??e.compute(qe(t[1],Br),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=x);let w=[t[0],x];s&&w.push(t[2]);let I=a?g*_:y,k=a?y:g*_,E=f*h*p;e.compute(qc(w,r,n,I,k,E,s,S,i),{inputs:w})},Bu=(e,t)=>{let r=t.format==="NHWC",i=[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&&i.push(e.inputs[2]);let a=[0,t.pads[0],0,t.pads[1]],n=[1].concat(t.strides),s=[1].concat(t.dilations),u=[1].concat(t.kernelShape),l=Nr({...t,pads:a,strides:n,dilations:s,kernelShape:u},i);Gi(e,i,l,p=>r?[p[0],p[2],p[3]]:[p[0],p[1],p[3]])},Nu=(e,t,r)=>{let i=r.format==="NHWC"?"channelsLast":"channelsFirst",a=Nr(r,t),n=r.autoPad==="NOTSET"?r.pads:r.autoPad,s=Lc(t[0].dims,t[1].dims,r.strides,r.dilations,n,!1,i);e.compute(Wc(t,a,s.outShape,[s.filterDepth,s.filterHeight,s.filterWidth],[s.padInfo.front,s.padInfo.top,s.padInfo.left],i))},Ia=(e,t)=>{if(Ru(e.inputs,t),e.inputs[0].dims.length===3)Bu(e,t);else if(e.inputs[0].dims.length===5)Nu(e,e.inputs,t);else{let r=Nr(t,e.inputs);Gi(e,e.inputs,r)}}}),Hc,p_=U(()=>{te(),ot(),ie(),ae(),Hc=(e,t,r)=>{let i=e.length>2,a=t.outputShape,n=t.format==="NHWC",s=t.group,u=e[1].dims,l=u[2]/s,p=u[3],f=n?ve(l):1,h=n&&p===1&&l>=4,g=h?Math.floor(l/4)*4:Math.floor(l/f)*f,_=l-g,y=n?ve(p):1,b=n?p===1?f:y:1,S=O.size(a)/y,x=[Math.ceil(S/64),1,1];de("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${x}`);let w=["rank","rank"],I=[t.strides[0],t.strides[1]],k=[t.kernelShape[n?1:2],t.kernelShape[n?2:3]],E=[t.dilations[0],t.dilations[1]],C=[k[0]+(t.dilations[0]<=1?0:(t.kernelShape[n?1:2]-1)*(t.dilations[0]-1)),k[1]+(t.dilations[1]<=1?0:(t.kernelShape[n?2:3]-1)*(t.dilations[1]-1))],A=[C[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),C[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],v=[{type:12,data:S},{type:12,data:I},{type:12,data:k},{type:12,data:E},{type:12,data:C},{type:6,data:A},{type:12,data:g},{type:12,data:l},{type:12,data:p},...J(e[0].dims,e[1].dims)];i&&(v.push(...J(e[2].dims)),w.push("rank")),v.push(...J(a));let M=D=>{let H=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:I.length},{name:"filter_dims",type:"u32",length:k.length},{name:"dilations",type:"u32",length:k.length},{name:"effective_filter_dims",type:"u32",length:C.length},{name:"pads",type:"i32",length:A.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],F=Te(e[0].dataType),j=n?1:2,R=n?2:3,K=n?3:1,Z=N("W",e[1].dataType,e[1].dims.length,b),ee=N("Dy",e[0].dataType,e[0].dims.length,f),he=[ee,Z];i&&he.push(N("bias",e[2].dataType,[a[K]].length,y));let W=X("result",e[0].dataType,a.length,y),ue=()=>{let Q="";if(h)f===4?Q+=`
1210        let xValue = ${ee.getByOffset("x_offset")};
1211        let wValue = ${Z.getByOffset("w_offset")};
1212        dotProd = dotProd + dot(xValue, wValue);
1213        x_offset += 1u;
1214        w_offset += 1u;`:f===2?Q+=`
1215          dotProd = dotProd + dot(vec4<${F}>(${ee.getByOffset("x_offset")}, ${ee.getByOffset("x_offset + 1u")}), vec4<${F}>(${Z.getByOffset("w_offset")}, ${Z.getByOffset("w_offset + 1u")}));
1216          x_offset += 2u;
1217          w_offset += 2u;`:f===1&&(Q+=`
1218          dotProd = dotProd + dot(vec4<${F}>(${ee.getByOffset("x_offset")}, ${ee.getByOffset("x_offset + 1u")}, ${ee.getByOffset("x_offset + 2u")}, ${ee.getByOffset("x_offset + 3u")}), vec4<${F}>(${Z.getByOffset("w_offset")}, ${Z.getByOffset("w_offset + 1u")}, ${Z.getByOffset("w_offset + 2u")}, ${Z.getByOffset("w_offset + 3u")}));
1219          x_offset += 4u;
1220          w_offset += 4u;`);else if(Q+=`
1221                  let xValue = ${n?ee.getByOffset(`${ee.indicesToOffset(`${ee.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${f}`):ee.get("batch","inputChannel","idyR","idyC")};
1222        `,f===1)Q+=`
1223          let w_offset = ${Z.indicesToOffset(`${Z.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)};
1224          let wValue = ${Z.getByOffset(`w_offset / ${b}`)};
1225          dotProd = dotProd + xValue * wValue;`;else for(let q=0;q<f;q++)Q+=`
1226            let wValue${q} = ${Z.getByOffset(`${Z.indicesToOffset(`${Z.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel + ${q}, wOutChannel)`)} / ${b}`)};
1227            dotProd = dotProd + xValue[${q}] * wValue${q};`;return Q},P=()=>{if(_===0)return"";if(!h)throw new Error(`packInputAs4 ${h} is not true.`);let Q="";if(f===1){Q+="dotProd = dotProd";for(let q=0;q<_;q++)Q+=`
1228            + ${ee.getByOffset(`x_offset + ${q}`)} * ${Z.getByOffset(`w_offset + ${q}`)}`;Q+=";"}else if(f===2){if(_!==2)throw new Error(`Invalid inputChannelsRemainder ${_}.`);Q+=`
1229          let xValue = ${ee.getByOffset("x_offset")};
1230          let wValue = ${Z.getByOffset("w_offset")};
1231          dotProd = dotProd + dot(xValue, wValue);`}return Q},V=`
1232            let outputIndices = ${W.offsetToIndices(`global_idx * ${y}`)};
1233            let batch = ${W.indicesGet("outputIndices",0)};
1234            let d1 = ${W.indicesGet("outputIndices",K)};
1235            let r = ${W.indicesGet("outputIndices",j)};
1236            let c = ${W.indicesGet("outputIndices",R)};
1237            let dyCorner = vec2<i32>(i32(r), i32(c)) - uniforms.pads;
1238            let dyRCorner = dyCorner.x;
1239            let dyCCorner = dyCorner.y;
1240            let groupId = d1 / uniforms.output_channels_per_group;
1241            let wOutChannel = d1 - groupId * uniforms.output_channels_per_group;
1242            // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
1243            // ? = to be determined. : = across all values in that axis.
1244            var dotProd = ${W.type.value}(0.0);
1245            var wR: u32 = 0;
1246            if (uniforms.dilations.x == 1) {
1247              // Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0
1248              wR = u32(((dyRCorner + i32(uniforms.strides.x) - 1) / i32(uniforms.strides.x)) * i32(uniforms.strides.x) - dyRCorner);
1249            }
1250            for (; wR < uniforms.effective_filter_dims.x; wR = wR + 1) {
1251              if (wR % uniforms.dilations.x != 0) {
1252                continue;
1253              }
1254              let dyR = (${F}(dyRCorner) + ${F}(wR)) / ${F}(uniforms.strides[0]);
1255              let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x;
1256              if (dyR < 0.0 || dyR >= ${F}(uniforms.Dy_shape[${j}]) || fract(dyR) > 0.0 ||
1257                  wRPerm < 0) {
1258                continue;
1259              }
1260              let idyR: u32 = u32(dyR);
1261              var wC: u32 = 0;
1262              if (uniforms.dilations.y == 1) {
1263                // Minimum wC >= 0 that satisfies (dyCCorner + wC) % (uniforms.strides.y) == 0
1264                wC = u32(((dyCCorner + i32(uniforms.strides.y) - 1) / i32(uniforms.strides.y)) * i32(uniforms.strides.y) - dyCCorner);
1265              }
1266              for (; wC < uniforms.effective_filter_dims.y; wC = wC + 1) {
1267                if (wC % uniforms.dilations.y != 0) {
1268                  continue;
1269                }
1270                let dyC = (${F}(dyCCorner) + ${F}(wC)) / ${F}(uniforms.strides.y);
1271                let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y;
1272                if (dyC < 0.0 || dyC >= ${F}(uniforms.Dy_shape[${R}]) ||
1273                    fract(dyC) > 0.0 || wCPerm < 0) {
1274                  continue;
1275                }
1276                let idyC: u32 = u32(dyC);
1277                var inputChannel = groupId * uniforms.input_channels_per_group;
1278                ${h?`
1279                var x_offset = ${ee.indicesToOffset(`${ee.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${f};
1280                var w_offset = ${Z.indicesToOffset(`${Z.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${b};
1281                  `:""}
1282                for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${h?4:f}) {
1283                  ${ue()}
1284                  inputChannel = inputChannel + ${h?4:f};
1285                }
1286                ${P()}
1287                wC = wC + uniforms.strides.y - 1;
1288              }
1289              wR = wR + uniforms.strides[0] - 1;
1290            }
1291            let value = dotProd${i?` + bias[d1 / ${y}]`:""};
1292            ${W.setByOffset("global_idx","value")};
1293          `;return`
1294    ${D.registerUniforms(H).declareVariables(...he,W)}
1295      ${D.mainStart()}
1296      ${D.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")};
1297    ${V}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${f}${b}${y}${h}${_}`,inputDependencies:w},getRunData:()=>({dispatchGroup:{x:x[0],y:x[1],z:x[2]},outputs:[{dims:r?r(a):a,dataType:e[0].dataType}],programUniforms:v}),getShaderSource:M}}}),Mu,Du,Uu,Hi,Fc,Pu,Fi,qu,jc,c_=U(()=>{p_(),Mt(),yt(),Mu=(e,t,r,i,a,n)=>(e-1)*t+r+(i-1)*a+1-n,Du=(e,t,r,i,a)=>{let n=Math.floor(e/2);t==="SAME_UPPER"?(r[i]=n,r[a]=e-n):t==="SAME_LOWER"&&(r[i]=e-n,r[a]=n)},Uu=(e,t,r,i,a,n,s,u,l,p)=>{let f=e.length-2,h=p.length===0;l.length<f&&l.push(...Array(f-l.length).fill(0));let g=e[0],_=t[u?3:1]*a;for(let y=0,b=e.length-f-(u?1:0);y<f;++y,++b){let S=e[b],x=h?S*s[y]:p[y],w=Mu(S,s[y],n[y],t[b],r[y],x);Du(w,i,n,y,y+f),h&&p.push(s[y]*(S-1)+l[y]+(t[b]-1)*r[y]+1-n[y]-n[y+f])}p.splice(0,0,g),p.splice(u?3:1,0,_)},Hi=(e,t)=>{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((h,g)=>h*g,1)===0){r.length=0;for(let h=2;h<t[1].dims.length;++h)r.push(t[1].dims[h])}let i=e.format==="NHWC";r.splice(0,0,t[1].dims[0]),r.splice(i?3:1,0,t[1].dims[1]);let a=e.pads.slice(),n=e.outputShape.slice(),s=e.outputPadding.slice(),u=t[0].dims,l=e.dilations.slice();if(l.reduce((h,g)=>h+g,0)===0){let h=t[0].dims.length-2;l=new Array(h).fill(1)}let p=e.strides.slice();if(p.reduce((h,g)=>h+g,0)===0){let h=t[0].dims.length-2;p=new Array(h).fill(1)}Uu(u,r,l,e.autoPad,e.group,a,p,i,s,n);let f=Object.assign({},e);return Object.assign(f,{kernelShape:r,pads:a,outputPadding:s,outputShape:n,dilations:l,strides:p}),f},Fc=e=>{let t=ja(e),r=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],a=e.dilations,n=e.group??1,s=e.kernelShape,u=e.pads,l=e.strides,p=e.wIsConst(),f=e.outputPadding,h=e.outputShape;return{autoPad:i,format:r,dilations:a,group:n,kernelShape:s,outputPadding:f,outputShape:h,pads:u,strides:l,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},Pu=(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],i=e[1].dims[0];if(r!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let a=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==a))throw new Error("invalid bias");let n=e[0].dims.length-2;if(t.dilations.reduce((s,u)=>s+u,0)>0&&t.dilations.length!==n)throw new Error(`dilations should be ${n}D`);if(t.strides.reduce((s,u)=>s+u,0)>0&&t.strides.length!==n)throw new Error(`strides should be ${n}D`);if(t.pads.reduce((s,u)=>s+u,0)>0&&t.pads.length!==n*2)throw new Error(`pads should be ${n*2}D`);if(t.outputPadding.length!==n&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${n}D`);if(t.kernelShape.reduce((s,u)=>s+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")},Fi=(e,t,r,i)=>{let a=e.kernelCustomData.wT??e.compute(qe(t[1],[2,3,0,1]),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=a);let n=[t[0],a];t.length===3&&n.push(t[2]),e.compute(Hc(n,r,i),{inputs:n})},qu=(e,t)=>{let r=t.format==="NHWC",i=[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&&i.push(e.inputs[2]);let a=t.kernelShape;(a.length===0||a[0]===0)&&(a=[e.inputs[1].dims[2]]);let n=t.dilations;(n.length===0||n[0]===0)&&(n=[1]);let s=t.strides;(s.length===0||s[0]===0)&&(s=[1]);let u=t.pads;u.length===0&&(u=[0,0]),u=[0,u[0],0,u[1]],s=[1].concat(s),n=[1].concat(n),a=[1].concat(a);let l=t.outputPadding;l=[0].concat(l);let p=Hi({...t,pads:u,strides:s,dilations:n,kernelShape:a,outputPadding:l},i);Fi(e,i,p,f=>r?[f[0],f[2],f[3]]:[f[0],f[1],f[3]])},jc=(e,t)=>{if(Pu(e.inputs,t),e.inputs[0].dims.length===3)qu(e,t);else{let r=Hi(t,e.inputs);Fi(e,e.inputs,r)}}}),Lu,Kc,Zc,h_=U(()=>{te(),ie(),xe(),ae(),Lu=(e,t,r,i)=>{let a=O.size(t),n=t.length,s=N("input",e,n),u=X("output",e,n),l=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),p=O.normalizeAxis(l,n),f=h=>{let g=` i32(${s.indicesGet("inputIndices","uniforms.axis")}) `,_=Y("uniforms.input_shape","uniforms.axis",n),y=i.reverse?g+(i.exclusive?" + 1":""):"0",b=i.reverse?_:g+(i.exclusive?"":" + 1");return`
1298                ${h.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(s,u)}
1299                ${h.mainStart()}
1300                  ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1301                  var inputIndices = ${u.offsetToIndices("global_idx")};
1302                  var sum = ${u.type.value}(0);
1303                  let first : i32 = ${y};
1304                  let last : i32 = ${b};
1305                  for (var i : i32 = first; i < last; i++) {
1306                    ${s.indicesSet("inputIndices","uniforms.axis","u32(i)")};
1307                    sum = sum + ${s.getByIndices("inputIndices")};
1308                  }
1309                  ${u.setByOffset("global_idx","sum")};
1310                }`};return{name:"CumSum",shaderCache:{hint:i.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:[{type:12,data:a},{type:12,data:p},...J(t,t)]}),getShaderSource:f}},Kc=(e,t)=>{let r=e.inputs[0].dims,i=e.inputs[0].dataType,a=e.inputs[1];e.compute(Lu(i,r,a,t),{inputs:[0]})},Zc=e=>{let t=e.exclusive===1,r=e.reverse===1;return fe({exclusive:t,reverse:r})}}),Wu,Vu,Gu,Qc,Xc,f_=U(()=>{te(),ie(),xe(),ae(),Wu=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.")},Vu=(e,t,r,i)=>{let a=[];a.push(`fn perm(i: ${i.type.indices}) -> ${r.type.indices} {
1311    var a: ${r.type.indices};`);for(let n=0;n<t;++n)a.push(r.indicesSet("a",e[n],`i[${n}]`));return a.push("return a;}"),a.join(`
1312`)},Gu=(e,t)=>{let r,i,a,n,s,u,l=t.format==="NHWC",p=t.blocksize,f=t.mode==="DCR";l?([r,i,a,n]=e.dims,s=f?[r,i,a,p,p,n/p**2]:[r,i,a,n/p**2,p,p],u=f?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,i,a,n]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],s=f?[r,p,p,n/p**2,i,a]:[r,n/p**2,p,p,i,a],u=f?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let h=e.reshape(s),g=h.dims.length,_=e.dataType,y=N("a",_,g),b=X("output",_,g),S=x=>`
1313  ${x.registerUniform("output_size","u32").declareVariables(y,b)}
1314
1315  ${Vu(u,g,y,b)}
1316
1317  ${x.mainStart()}
1318    ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1319
1320    let indices = ${b.offsetToIndices("global_idx")};
1321    let aIndices = perm(indices);
1322
1323    ${b.setByOffset("global_idx",y.getByIndices("aIndices"))}
1324  }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:x=>{let w=l?[r,i*p,a*p,n/p**2]:[r,n/p**2,i*p,a*p],I=O.size(w),k=h.dims,E=O.sortBasedOnPerm(k,u);return{outputs:[{dims:w,dataType:x[0].dataType}],dispatchGroup:{x:Math.ceil(I/64)},programUniforms:[{type:12,data:I},...J(k,E)]}},getShaderSource:S}},Qc=(e,t)=>{Wu(e.inputs),e.compute(Gu(e.inputs[0],t))},Xc=e=>fe({blocksize:e.blocksize,mode:e.mode,format:e.format})}),Mr,rr,ji,Hu,Fu,ju,Ku,Ki,Zu,Yc,Jc,m_=U(()=>{te(),ie(),xe(),ae(),Mr="[a-zA-Z]|\\.\\.\\.",rr="("+Mr+")+",ji="^"+rr+"$",Hu="("+rr+",)*"+rr,Fu="^"+Hu+"$",ju=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let r=this.symbolToIndices.get(e);r===void 0?r=[t]:r.push(t),this.symbolToIndices.set(e,r)}},Ku=class{constructor(e,t){var a;this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[r,i]=t.includes("->")?t.split("->",2):[t,""];if(!r.match(RegExp(Fu)))throw new Error("Invalid LHS term");if(r.split(",").forEach((n,s)=>{let u=e[s].dims.slice();if(!n.match(RegExp(ji)))throw new Error("Invalid LHS term");let l=this.processTerm(n,!0,u,s);this.lhs.push(l)}),i==="")i+=[...this.symbolToInfo.entries()].filter(([n,s])=>s.count===1||n==="...").map(([n])=>n).join("");else if(!i.match(RegExp(rr)))throw new Error("Invalid RHS");(a=i.match(RegExp(Mr,"g")))==null||a.forEach(n=>{if(n==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let s=this.symbolToInfo.get(n);if(s===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(s.dimValue)}}),this.rhs=this.processTerm(i,!1,this.outputDims)}addSymbol(e,t,r){let i=this.symbolToInfo.get(e);if(i!==void 0){if(i.dimValue!==t&&i.count!==1)throw new Error("Dimension mismatch");i.count++,i.inputIndices.push(r)}else i={count:1,dimValue:t,inputIndices:[r]};this.symbolToInfo.set(e,i)}processTerm(e,t,r,i=-1){let a=r.length,n=!1,s=[],u=0;if(!e.match(RegExp(ji))&&!t&&e!=="")throw new Error("Invalid LHS term");let l=e.match(RegExp(Mr,"g")),p=new ju(i);return l==null||l.forEach((f,h)=>{if(f==="..."){if(n)throw new Error("Only one ellipsis is allowed per input term");n=!0;let g=a-l.length+1;if(g<0)throw new Error("Ellipsis out of bounds");if(s=r.slice(u,u+g),this.hasEllipsis){if(this.ellipsisDims.length!==s.length||this.ellipsisDims.toString()!==s.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=s;else throw new Error("Ellipsis must be specified in the LHS");for(let _=0;_<s.length;_++){let y=String.fromCharCode(48+_);p.addSymbol(y,h+_),this.addSymbol(y,r[u++],i)}}else p.addSymbol(f,h+(this.hasEllipsis?this.ellipsisDims.length-1:0)),this.addSymbol(f,r[u++],i)}),p}},Ki=e=>e+"_max",Zu=(e,t,r,i)=>{let a=e.map(p=>p.length).map((p,f)=>N(`input${f}`,t,p)),n=O.size(i),s=X("output",t,i.length),u=[...r.symbolToInfo.keys()].filter(p=>!r.rhs.symbolToIndices.has(p)),l=p=>{let f=[],h="var prod = 1.0;",g="var sum = 0.0;",_="sum += prod;",y=[],b=[],S=[],x=[],w=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((k,E)=>{var C;if(r.rhs.symbolToIndices.has(E)){let A=(C=r.rhs.symbolToIndices.get(E))==null?void 0:C[0];A!==void 0&&r.lhs.forEach((v,M)=>{if(k.inputIndices.includes(M)){let D=v.symbolToIndices.get(E);if(D===void 0)throw new Error("Invalid symbol error");D.forEach(H=>{f.push(`${a[M].indicesSet(`input${M}Indices`,H,s.indicesGet("outputIndices",A))}`)})}})}else r.lhs.forEach((A,v)=>{if(k.inputIndices.includes(v)){let M=A.symbolToIndices.get(E);if(M===void 0)throw new Error("Invalid symbol error");M.forEach(D=>{y.push(`${a[v].indicesSet(`input${v}Indices`,D,`${E}`)}`)}),x.push(`prod *= ${a[v].getByIndices(`input${v}Indices`)};`)}}),b.push(`for(var ${E}: u32 = 0; ${E} < uniforms.${Ki(E)}; ${E}++) {`),S.push("}")});let I=w?[...f,`let sum = ${a.map((k,E)=>k.getByIndices(`input${E}Indices`)).join(" * ")};`]:[...f,g,...b,...y,h,...x,_,...S];return`
1325            ${p.registerUniforms(u.map(k=>({name:`${Ki(k)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...a,s)}
1326
1327            ${p.mainStart()}
1328            ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1329            var outputIndices = ${s.offsetToIndices("global_idx")};
1330            ${a.map((k,E)=>`var input${E}Indices: ${a[E].type.indices};`).join(`
1331`)}
1332            ${I.join(`
1333`)};
1334            ${s.setByOffset("global_idx","sum")};
1335          }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let p=u.filter(h=>r.symbolToInfo.has(h)).map(h=>{var g;return{type:12,data:((g=r.symbolToInfo.get(h))==null?void 0:g.dimValue)||0}});p.push({type:12,data:n});let f=e.map((h,g)=>[...J(h)]).reduce((h,g)=>h.concat(g),p);return f.push(...J(i)),{outputs:[{dims:i,dataType:t}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:f}},getShaderSource:l}},Yc=(e,t)=>{let r=new Ku(e.inputs,t.equation),i=r.outputDims,a=e.inputs.map((n,s)=>n.dims);e.compute(Zu(a,e.inputs[0].dataType,r,i))},Jc=e=>{let t=e.equation.replace(/\s+/g,"");return fe({equation:t})}}),Qu,Zi,Xu,Yu,eh,g_=U(()=>{te(),ie(),ae(),Qu=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),i=r.length<t.length?0:r.length-t.length,a=t.length<r.length?0:t.length-r.length;for(;i<r.length&&a<t.length;++i,++a)if(r[i]!==t[a]&&r[i]!==1&&t[a]!==1)throw new Error("Expand requires shape to be broadcastable to input")},Zi=(e,t)=>{let r=e.length-t.length,i=[];for(let a=0;a<r;++a)i.push(e[a]);for(let a=0;a<t.length;++a)i.push(t[a]===1?e[a+r]:t[a]);return i},Xu=(e,t)=>e.length>t.length?Zi(e,t):Zi(t,e),Yu=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),i=Xu(t,r),a=e[0].dataType,n=a===9||O.size(t)===1,s=a===9||t.length>0&&t[t.length-1]%4===0?4:1,u=n||i.length>0&&i[i.length-1]%4===0?4:1,l=Math.ceil(O.size(i)/u),p=h=>{let g=N("input",a,t.length,s),_=X("output",a,i.length,u),y;if(a===9){let b=(S,x,w="")=>`
1336          let outputIndices${x} = ${_.offsetToIndices(`outputOffset + ${x}u`)};
1337          let offset${x} = ${g.broadcastedIndicesToOffset(`outputIndices${x}`,_)};
1338          let index${x} = offset${x} / 4u;
1339          let component${x} = offset${x} % 4u;
1340          ${S}[${x}] = ${w}(${g.getByOffset(`index${x}`)}[component${x}]);
1341        `;y=`
1342        let outputOffset = global_idx * ${u};
1343        var data = vec4<u32>(0);
1344        ${b("data",0,"u32")}
1345        ${b("data",1,"u32")}
1346        ${b("data",2,"u32")}
1347        ${b("data",3,"u32")}
1348        ${_.setByOffset("global_idx","data")}
1349      }`}else y=`
1350        let outputIndices = ${_.offsetToIndices(`global_idx * ${u}`)};
1351        let inputOffset = ${g.broadcastedIndicesToOffset("outputIndices",_)};
1352        let data = ${_.type.value}(${g.getByOffset(`inputOffset / ${s}`)});
1353        ${_.setByOffset("global_idx","data")}
1354      }`;return`
1355    ${h.registerUniform("vec_size","u32").declareVariables(g,_)}
1356    ${h.mainStart()}
1357    ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
1358    ${y}`},f=[{type:12,data:l},...J(t,i)];return{name:"Expand",shaderCache:{hint:`${i.length};${s}${u}`,inputDependencies:["rank"]},getShaderSource:p,getRunData:()=>({outputs:[{dims:i,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:f})}},eh=e=>{Qu(e.inputs),e.compute(Yu(e.inputs),{inputs:[0]})}}),Ju,th,__=U(()=>{te(),ie(),ae(),Fa(),Ju=e=>{let t=e[0].dataType,r=O.size(e[0].dims),i=O.size(e[1].dims),a=i%4===0,n=s=>{let u=N("x",t,[1],4),l=N("bias",t,[1],4),p=X("y",t,[1],4),f=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],h=_=>`
1359      let bias${_}_offset: u32 = (global_idx * 4 + ${_}) % uniforms.bias_size;
1360      let bias${_} = ${l.getByOffset(`bias${_}_offset / 4`)}[bias${_}_offset % 4];`,g=a?`
1361      let bias = ${l.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${h(0)}${h(1)}${h(2)}${h(3)}
1362      let bias = ${u.type.value}(bias0, bias1, bias2, bias3);`;return`${s.registerUniforms(f).declareVariables(u,l,p)}
1363
1364    ${va(Ce(t))}
1365
1366    ${s.mainStart(Gt)}
1367      ${s.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")}
1368
1369      let x = ${u.getByOffset("global_idx")};
1370      ${g}
1371      let x_in = x + bias;
1372      ${p.setByOffset("global_idx",xa("x_in"))}
1373    }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${a}`,inputDependencies:["type","type"]},getShaderSource:n,getRunData:s=>({outputs:[{dims:s[0].dims,dataType:s[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:i}],dispatchGroup:{x:Math.ceil(r/Gt/4)}})}},th=e=>{e.inputs.length<2||O.size(e.inputs[1].dims)===0?$c(e):e.compute(Ju(e.inputs))}}),el,tl,rh,ih,y_=U(()=>{te(),ie(),xe(),ae(),el=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},tl=(e,t)=>{let r=e[0].dims,i=e[1].dims,a=r.length,n=O.normalizeAxis(t.axis,a),s=r.slice(0);s.splice(n,1,...i);let u=r[n],l=e[0].dataType===9?4:1,p=Math.ceil(O.size(s)/l),f=[{type:12,data:p},{type:6,data:u},{type:12,data:n},...J(e[0].dims,e[1].dims,s)],h=g=>{let _=N("data",e[0].dataType,e[0].dims.length,l),y=N("inputIndices",e[1].dataType,e[1].dims.length),b=X("output",e[0].dataType,s.length,l),S=w=>{let I=i.length,k=`var indicesIndices${w}  = ${y.type.indices}(0);`;for(let E=0;E<I;E++)k+=`${I>1?`indicesIndices${w}[${E}]`:`indicesIndices${w}`} = ${s.length>1?`outputIndices${w}[uniforms.axis + ${E}]`:`outputIndices${w}`};`;k+=`
1374          var idx${w} = ${y.getByIndices(`indicesIndices${w}`)};
1375          if (idx${w} < 0) {
1376            idx${w} = idx${w} + uniforms.axisDimLimit;
1377          }
1378          var dataIndices${w} : ${_.type.indices};
1379        `;for(let E=0,C=0;E<a;E++)E===n?(k+=`${a>1?`dataIndices${w}[${E}]`:`dataIndices${w}`} = u32(idx${w});`,C+=I):(k+=`${a>1?`dataIndices${w}[${E}]`:`dataIndices${w}`} = ${s.length>1?`outputIndices${w}[${C}]`:`outputIndices${w}`};`,C++);return k},x;if(e[0].dataType===9){let w=(I,k,E="")=>`
1380          let outputIndices${k} = ${b.offsetToIndices(`outputOffset + ${k}u`)};
1381          ${S(k)};
1382          let offset${k} = ${_.indicesToOffset(`dataIndices${k}`)};
1383          let index${k} = offset${k} / 4u;
1384          let component${k} = offset${k} % 4u;
1385          ${I}[${k}] = ${E}(${_.getByOffset(`index${k}`)}[component${k}]);
1386        `;x=`
1387        let outputOffset = global_idx * ${l};
1388        var value = vec4<u32>(0);
1389        ${w("value",0,"u32")}
1390        ${w("value",1,"u32")}
1391        ${w("value",2,"u32")}
1392        ${w("value",3,"u32")}
1393        ${b.setByOffset("global_idx","value")}
1394      `}else x=`
1395      let outputIndices = ${b.offsetToIndices("global_idx")};
1396      ${S("")};
1397      let value = ${_.getByIndices("dataIndices")};
1398      ${b.setByOffset("global_idx","value")};
1399      `;return`
1400      ${g.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(_,y,b)}
1401      ${g.mainStart()}
1402        ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1403        ${x}
1404      }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:s,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:f}),getShaderSource:h}},rh=e=>fe({axis:e.axis}),ih=(e,t)=>{let r=e.inputs;el(r),e.compute(tl(e.inputs,t))}}),rl,ah,nh,b_=U(()=>{te(),ie(),ae(),rl=(e,t,r,i,a,n,s,u,l)=>{let p=[{type:12,data:n},{type:12,data:i},{type:12,data:a},{type:12,data:r},{type:12,data:s},{type:12,data:u},{type:12,data:l}],f=[n];p.push(...J(t.dims,f));let h=g=>{let _=N("indices_data",t.dataType,t.dims.length),y=X("input_slice_offsets_data",12,1,1),b=[_,y],S=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:a.length},{name:"sizes_from_slice_dims_data",type:"u32",length:r.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return`
1405  ${g.registerUniforms(S).declareVariables(...b)}
1406  ${g.mainStart()}
1407    ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1408    let batch_idx = global_idx / uniforms.num_slices_per_batch;
1409    let base_offset = batch_idx * uniforms.input_batch_stride;
1410
1411    let slice_indices_base_offset = global_idx * uniforms.num_slice_dims;
1412    var relative_slice_offset = 0;
1413    for (var dim_idx = 0u; dim_idx < uniforms.num_slice_dims; dim_idx ++) {
1414      var index = i32(indices_data[dim_idx + slice_indices_base_offset].x);
1415      let input_dim_idx = uniforms.batch_dims + dim_idx;
1416      if (index < 0) {
1417        ${a.length===1?"index += i32(uniforms.input_dims);":"index += i32(uniforms.input_dims[input_dim_idx]);"}
1418      }
1419      ${r.length===1?"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data);":"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data[dim_idx]);"}
1420    }
1421
1422    input_slice_offsets_data[global_idx] =  base_offset + u32(relative_slice_offset);
1423  }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${a.length}_${r.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:f,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:p}),getShaderSource:h},{inputs:[t],outputs:[-1]})[0]},ah=(e,t)=>{let r=e.inputs,i=r[0].dims,a=r[0].dataType,n=r[1].dims,s=n[n.length-1],u=O.sizeToDimension(n,n.length-1),l=O.sizeFromDimension(i,t.batchDims+s),p=O.sizeToDimension(i,t.batchDims),f=O.sizeFromDimension(i,t.batchDims),h=u/p,g=new Array(s),_=l;for(let k=0;k<s;++k)g[s-1-k]=_,_*=i[t.batchDims+s-1-k];let y=rl(e,r[1],g,t.batchDims,i,u,h,f,s),b=t.batchDims+s;if(b>i.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let S=n.slice(0,-1).concat(i.slice(b)),x=O.size(S),w=[{type:12,data:x},{type:12,data:l},...J(r[0].dims,y.dims,S)],I=k=>{let E=N("data",r[0].dataType,r[0].dims.length),C=N("slice_offsets",12,y.dims.length),A=X("output",r[0].dataType,S.length);return`
1424          ${k.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(E,C,A)}
1425            ${k.mainStart()}
1426            ${k.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1427          let slice_offset = slice_offsets[global_idx / uniforms.slice_size];
1428          output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size];
1429        }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:S,dataType:a}],dispatchGroup:{x:Math.ceil(x/64)},programUniforms:w}),getShaderSource:I},{inputs:[r[0],y]})},nh=e=>({batchDims:e.batch_dims,cacheKey:""})}),il,al,sh,oh,w_=U(()=>{te(),ie(),xe(),ae(),il=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let r=O.normalizeAxis(t.quantizeAxis,e[0].dims.length),i=t.blockSize,a=e[0],n=e[2],s=e.length===4?e[3]:void 0;if(n.dims.length!==a.dims.length||!a.dims.map((u,l)=>l===r?Math.ceil(u/i)===n.dims[l]:u===n.dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(s){if(s.dataType!==a.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(s.dims.length!==n.dims.length||!s.dims.map((u,l)=>u===n.dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},al=(e,t)=>{let r=e[0].dims,i=e[1].dims,a=r.length,n=O.normalizeAxis(t.gatherAxis,a),s=O.normalizeAxis(t.quantizeAxis,a),u=r.slice(0);u.splice(n,1,...i);let l=O.size(u),p=e[2].dataType,f=e[0].dataType===22,h=[{type:12,data:l},{type:12,data:s},{type:12,data:n},{type:12,data:t.blockSize},...J(...e.map((_,y)=>_.dims),u)],g=_=>{let y=N("data",e[0].dataType,e[0].dims.length),b=N("inputIndices",e[1].dataType,e[1].dims.length),S=N("scales",e[2].dataType,e[2].dims.length),x=e.length>3?N("zeroPoint",e[3].dataType,e[3].dims.length):void 0,w=X("output",p,u.length),I=[y,b,S];x&&I.push(x);let k=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return`
1430        ${_.registerUniforms(k).declareVariables(...I,w)}
1431        ${_.mainStart()}
1432        let output_indices = ${w.offsetToIndices("global_idx")};
1433        var indices_indices = ${b.type.indices}(0);
1434        ${i.length>1?`
1435          for (var i: u32 = 0; i < ${i.length}; i++) {
1436            let index = ${w.indicesGet("output_indices","uniforms.gather_axis + i")};
1437            ${b.indicesSet("indices_indices","i","index")};
1438          }`:`indices_indices = ${w.indicesGet("output_indices","uniforms.gather_axis")};`};
1439        var data_indices = ${y.type.indices}(0);
1440        for (var i: u32 = 0; i < uniforms.gather_axis; i++) {
1441          let index = ${w.indicesGet("output_indices","i")};
1442          ${y.indicesSet("data_indices","i","index")};
1443        }
1444        var index_from_indices = ${b.getByIndices("indices_indices")};
1445        if (index_from_indices < 0) {
1446          index_from_indices += ${r[n]};
1447        }
1448        ${y.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")};
1449        for (var i = uniforms.gather_axis + 1; i < ${u.length}; i++) {
1450          let index = ${w.indicesGet("output_indices",`i + ${i.length} - 1`)};
1451          ${y.indicesSet("data_indices","i","index")};
1452        }
1453        let data_offset = ${y.indicesToOffset("data_indices")};
1454        let data_index = data_offset % 8;
1455        // Convert 4-bit packed data to 8-bit packed data.
1456        let packed_4bit_quantized_data = ${y.getByOffset("data_offset / 8")};
1457        let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f;
1458        let quantized_data_vec = ${f?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data));
1459        let quantized_data = quantized_data_vec[data_index / 2];
1460        var scale_indices = data_indices;
1461        let quantize_axis_index = ${S.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size;
1462        ${S.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")};
1463        var scale = ${S.getByIndices("scale_indices")};
1464        ${x?`
1465              let zero_point_indices = scale_indices;
1466              let zero_point_offset = ${x.indicesToOffset("zero_point_indices")};
1467              let zero_point_index = zero_point_offset % 8;
1468              let packed_4bit_zero_points = ${x.getByOffset("zero_point_offset / 8")};
1469              let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f;
1470              let zero_point_vec = ${f?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points));
1471              let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"};
1472        let dequantized_data = ${Ce(p)}(quantized_data - zero_point) * scale;
1473        ${w.setByOffset("global_idx","dequantized_data")};
1474    }`};
1474return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((_,y)=>y!==1).map(_=>_.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(_,y)=>"rank")},getRunData:()=>({outputs:[{dims:u,dataType:p}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:h}),getShaderSource:g}},sh=(e,t)=>{let r=e.inputs;il(r,t),e.compute(al(e.inputs,t))},oh=e=>fe({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),nl,sl,uh,lh,$_=U(()=>{te(),ie(),xe(),ae(),nl=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
1475                     indices input tensors be of same rank.`)},sl=(e,t)=>{let r=e[0].dims,i=e[0].dataType,a=r.length,n=e[1].dims,s=e[1].dataType,u=O.normalizeAxis(t.axis,a),l=r[u],p=n.slice(0),f=O.size(p),h=N("input",i,a),g=N("indicesInput",s,n.length),_=X("output",i,p.length),y=[{type:12,data:f},{type:6,data:l},{type:12,data:u}];return y.push(...J(r,n,p)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:y}),getShaderSource:b=>`
1476      ${b.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(h,g,_)}
1477      ${b.mainStart()}
1478      ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1479
1480      let outputIndices = ${_.offsetToIndices("global_idx")};
1481
1482      var idx = ${g.getByOffset("global_idx")};
1483      if (idx < 0) {
1484        idx = idx + uniforms.axisDimLimit;
1485      }
1486      var inputIndices = ${h.type.indices}(outputIndices);
1487      ${h.indicesSet("inputIndices","uniforms.axis","u32(idx)")};
1488      let value = ${h.getByIndices("inputIndices")};
1489
1490      ${_.setByOffset("global_idx","value")};
1491  }`}},uh=e=>fe({axis:e.axis}),lh=(e,t)=>{let r=e.inputs;nl(r),e.compute(sl(e.inputs,t))}}),ol,ul,dh,ph,v_=U(()=>{te(),ie(),ae(),ol=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")},ul=(e,t)=>
vendor: 4,793 bytes, lines 1491-1572
1491{let r=e[0].dims.slice(),i=e[1].dims.slice(),[a,n,s]=up.getShapeOfGemmResult(r,t.transA,i,t.transB,e.length===3?e[2].dims:void 0),u=[a,n];if(!u)throw new Error("Can't use gemm on the given tensors");let l=16,p=Math.ceil(n/l),f=Math.ceil(a/l),h=!0,g=O.size(u),_=[{type:12,data:h?p:g},{type:12,data:a},{type:12,data:n},{type:12,data:s},{type:1,data:t.alpha},{type:1,data:t.beta}],y=["type","type"];e.length===3&&(_.push(...J(e[2].dims)),y.push("rank")),_.push(...J(u));let b=x=>{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 I=t.alpha===1?"":"value *= uniforms.alpha;",k=N("a",e[0].dataType,e[0].dims),E=N("b",e[1].dataType,e[1].dims),C=k.type.value,A=null,v=[k,E];e.length===3&&(A=N("c",e[2].dataType,e[2].dims.length),v.push(A));let M=X("output",e[0].dataType,u.length);v.push(M);let D=[{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`
1492  ${x.registerUniforms(D).declareVariables(...v)}
1493
1494  ${x.mainStart()}
1495    ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1496
1497    let m = global_idx / uniforms.N;
1498    let n = global_idx % uniforms.N;
1499
1500    var value = ${C}(0);
1501    for (var k: u32 = 0u; k < uniforms.K; k++) {
1502      ${w}
1503    }
1504
1505    ${I}
1506    ${A!=null?`let cOffset = ${A.broadcastedIndicesToOffset("vec2(m, n)",M)}; value += ${C}(uniforms.beta) * ${A.getByOffset("cOffset")};`:""}
1507    output[global_idx] = value;
1508  }`},S=x=>{let w=N("a",e[0].dataType,e[0].dims),I=N("b",e[1].dataType,e[1].dims),k=null,E=[w,I];e.length===3&&(k=N("c",e[2].dataType,e[2].dims.length),E.push(k));let C=X("output",e[0].dataType,u.length);E.push(C);let A=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],v="",M="";t.transA&&t.transB?(M=`
1509      var col = tile_row_start + local_id.x;
1510      var row = k_start + local_id.y;
1511      if (col < uniforms.M && row < uniforms.K) {
1512        tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col];
1513      } else {
1514        tile_a[local_id.y][local_id.x] = ${w.type.value}(0);
1515      }
1516
1517      col = k_start + local_id.x;
1518      row = tile_col_start + local_id.y;
1519      if (col < uniforms.K && row < uniforms.N) {
1520        tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col];
1521      } else {
1522        tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
1523      }
1524      `,v="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(M=`
1525      var col = tile_row_start + local_id.x;
1526      var row = k_start + local_id.y;
1527      if (col < uniforms.M && row < uniforms.K) {
1528        tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col];
1529      } else {
1530        tile_a[local_id.y][local_id.x] = ${w.type.value}(0);
1531      }
1532
1533      col = tile_col_start + local_id.x;
1534      row = k_start + local_id.y;
1535      if (col < uniforms.N && row < uniforms.K) {
1536        tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col];
1537      } else {
1538        tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
1539      }
1540      `,v="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(M=`
1541      var col = k_start + local_id.x;
1542      var row = tile_row_start + local_id.y;
1543      if (col < uniforms.K && row < uniforms.M) {
1544        tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col];
1545      } else {
1546        tile_a[local_id.y][local_id.x] = ${w.type.value}(0);
1547      }
1548
1549      col = k_start + local_id.x;
1550      row = tile_col_start + local_id.y;
1551      if (col < uniforms.K && row < uniforms.N) {
1552        tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col];
1553      } else {
1554        tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
1555      }
1556      `,v="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(M=`
1557      var col = k_start + local_id.x;
1558      var row = tile_row_start + local_id.y;
1559      if (col < uniforms.K && row < uniforms.M) {
1560        tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col];
1561      } else {
1562        tile_a[local_id.y][local_id.x] = ${w.type.value}(0);
1563      }
1564
1565      col = tile_col_start + local_id.x;
1566      row = k_start + local_id.y;
1567      if (col < uniforms.N && row < uniforms.K) {
1568        tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col];
1569      } else {
1570        tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
1571      }
1572      `,v="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let D=t.alpha===1?"":"value *= uniforms.alpha;";return`
1573  ${x.registerUniforms(A).declareVariables(...E)}
1574  var<workgroup> tile_a: array<array<${w.type.storage}, ${l}>, ${l}>;
1575  var<workgroup> tile_b: array<array<${I.type.storage}, ${l}>, ${l}>;
1576  ${x.mainStart([l,l,1])}
1577    let tile_col_start = (workgroup_index % uniforms.num_tile_n) * ${l};
1578    let tile_row_start = (workgroup_index / uniforms.num_tile_n) * ${l};
1579    let num_tiles = (uniforms.K - 1) / ${l} + 1;
1580    var k_start = 0u;
1581    var value = ${C.type.value}(0);
1582    for (var t: u32 = 0u; t < num_tiles; t++) {
1583      ${M}
1584      k_start = k_start + ${l};
1585      workgroupBarrier();
1586
1587      for (var k: u32 = 0u; k < ${l}; k++) {
1588        ${v}
1589      }
1590      workgroupBarrier();
1591    }
1592
1593    ${D}
1594    let m = tile_row_start + local_id.y;
1595    let n = tile_col_start + local_id.x;
1596    ${k!=null?`let cOffset = ${k.broadcastedIndicesToOffset("vec2(m, n)",C)}; value += ${C.type.value}(uniforms.beta) * ${k.getByOffset("cOffset")};`:""}
1597    if (m < uniforms.M && n < uniforms.N) {
1598      output[m * uniforms.N + n] = value;
1599    }
1600  }`};return h?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:p*f},programUniforms:_}),getShaderSource:S}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:_}),getShaderSource:b}},dh=e=>{let t=e.transA,r=e.transB,i=e.alpha,a=e.beta;return{transA:t,transB:r,alpha:i,beta:a,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},ph=(e,t)=>{ol(e.inputs),e.compute(ul(e.inputs,t))}}),tt,nt,xt,St,ll,dl,pl,cl,hl,fl,ml,gl,ch,hh,x_=U(()=>{te(),ie(),xe(),ae(),[tt,nt,xt,St]=[0,1,2,3],ll=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},dl=`
1601  fn gs_get_cubic_coeffs(x: f32) -> vec4<f32> {
1602    let cubic_alpha = -0.75f;
1603    let x_abs = abs(x);
1604    var coeffs: vec4<f32>;
1605    coeffs[0] = (((cubic_alpha * (x_abs + 1) - 5 * cubic_alpha) * (x_abs + 1) + 8 * cubic_alpha) * (x_abs + 1) - 4 * cubic_alpha);
1606    coeffs[1] = (((cubic_alpha + 2) * x_abs - (cubic_alpha + 3)) * x_abs * x_abs + 1);
1607    coeffs[2] = (((cubic_alpha + 2) * (1 - x_abs) - (cubic_alpha + 3)) * (1 - x_abs) * (1 - x_abs) + 1);
1608    coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha);
1609    return coeffs;
1610  }
1611`,pl=e=>`
1612  fn gs_bicubic_interpolate(p: mat4x4<${e}>, x: f32, y: f32) -> ${e} {
1613    var v: vec4<f32>;
1614    var coeffs = gs_get_cubic_coeffs(x);
1615    for (var i = 0; i < 4; i++) {
1616      v[i] = coeffs[0] * p[i][0] + coeffs[1] * p[i][1] + coeffs[2] * p[i][2] + coeffs[3] * p[i][3];
1617    }
1618    coeffs = gs_get_cubic_coeffs(y);
1619    let pixel = ${e}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]);
1620    return pixel;
1621  }
1622`,cl=e=>`
1623  fn gs_denormalize(n: f32, length: i32) -> f32 {
1624    ${e.alignCorners===0?`
1625    // alignCorners: false => [-1, 1] to [-0.5, length - 0.5]
1626    return ((n + 1.0) * f32(length) - 1.0) / 2.0;
1627    `:`
1628    // alignCorners: true => [-1, 1] to [0, length - 1]
1629    return (n + 1.0) / 2.0 * (f32(length - 1));
1630    `}
1631  }
1632`,hl=e=>`
1633  ${e.paddingMode==="reflection"?`
1634      fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 {
1635        var dx = 0.0;
1636        var fx = f32(x);
1637        let range = x_max - x_min;
1638        if (fx < x_min) {
1639          dx = x_min - fx;
1640          let n = u32(dx / range);
1641          let r = dx - f32(n) * range;
1642          if (n % 2 == 0) {
1643            fx = x_min + r;
1644          } else {
1645            fx = x_max - r;
1646          }
1647        } else if (fx > x_max) {
1648          dx = fx - x_max;
1649          let n = u32(dx / range);
1650          let r = dx - f32(n) * range;
1651          if (n % 2 == 0) {
1652            fx = x_max - r;
1653          } else {
1654            fx = x_min + r;
1655          }
1656        }
1657        return u32(fx);
1658      }`:""}
1659`,fl=(e,t,r)=>`
1660  fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4<f32>) -> ${t} {
1661     var pixel = ${t}(0);
1662     var indices = vec4<u32>(0);
1663     indices[${tt}] = batch;
1664     indices[${nt}] = channel;`+(()=>{switch(r.paddingMode){case"zeros":return`
1665          if (r >= 0 && r < H && c >=0 && c < W) {
1666            indices[${xt}] = u32(r);
1667            indices[${St}] = u32(c);
1668          } else {
1669            return ${t}(0);
1670          }
1671        `;case"border":return`
1672          indices[${xt}] = u32(clamp(r, 0, H - 1));
1673          indices[${St}] = u32(clamp(c, 0, W - 1));
1674        `;case"reflection":return`
1675          indices[${xt}] = gs_reflect(r, border[1], border[3]);
1676          indices[${St}] = gs_reflect(c, border[0], border[2]);
1677        `;default:throw new Error(`padding mode ${r.paddingMode} is not supported`)}})()+`
1678    return ${e.getByIndices("indices")};
1679  }
1680`,ml=(e,t,r)=>(()=>{switch(r.mode){case"nearest":return`
1681          let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${tt}], indices[${nt}], border);
1682        `;case"bilinear":return`
1683          let x1 = i32(floor(x));
1684          let y1 = i32(floor(y));
1685          let x2 = x1 + 1;
1686          let y2 = y1 + 1;
1687
1688          let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${tt}], indices[${nt}], border);
1689          let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${tt}], indices[${nt}], border);
1690          let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${tt}], indices[${nt}], border);
1691          let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${tt}], indices[${nt}], border);
1692
1693          let dx2 = ${t}(f32(x2) - x);
1694          let dx1 = ${t}(x - f32(x1));
1695          let dy2 = ${t}(f32(y2) - y);
1696          let dy1 = ${t}(y - f32(y1));
1697          let result = dy2 * (dx2 * p11 + dx1 * p12) + dy1 * (dx2 * p21 + dx1 * p22);
1698        `;case"bicubic":return`
1699          let x0 = i32(floor(x)) - 1;
1700          let y0 = i32(floor(y)) - 1;
1701          var p: mat4x4<${t}>;
1702          for (var h = 0; h < 4; h++) {
1703            for (var w = 0; w < 4; w++) {
1704              p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${tt}], indices[${nt}], border);
1705            }
1706          }
1707
1708          let dx = x - f32(x0 + 1);
1709          let dy = y - f32(y0 + 1);
1710          let result = gs_bicubic_interpolate(p, dx, dy);
1711        `;default:throw new Error(`mode ${r.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,gl=(e,t)=>{let r=N("x",e[0].dataType,e[0].dims.length),i=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],a=N("grid",e[1].dataType,i.length,2),n=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(n=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[tt,nt,xt,St]=[0,3,1,2]);let s=X("output",e[0].dataType,n.length),u=r.type.value,l=O.size(n),p=[{type:12,data:l},...J(e[0].dims,i,n)],f=h=>`
1712  ${h.registerUniform("output_size","u32").declareVariables(r,a,s)}
1713  ${dl}
1714  ${pl(u)}
1715  ${cl(t)}
1716  ${hl(t)}
1717  ${fl(r,u,t)}
1718
1719  ${h.mainStart()}
1720    ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1721      let H_in = i32(uniforms.x_shape[${xt}]);
1722      let W_in = i32(uniforms.x_shape[${St}]);
1723
1724      ${t.alignCorners===0?`
1725      let x_min = -0.5;
1726      let x_max = f32(W_in) - 0.5;
1727      let y_min = -0.5;
1728      let y_max = f32(H_in) - 0.5;
1729      `:`
1730      let x_min = 0.0;
1731      let x_max = f32(W_in) - 1.0;
1732      let y_min = 0.0;
1733      let y_max = f32(H_in) - 1.0;
1734      `};
1735      let border = vec4<f32>(x_min, y_min, x_max, y_max);
1736
1737      let indices = ${s.offsetToIndices("global_idx")};
1738      var grid_indices = vec3<u32>(indices[${tt}], indices[${xt}], indices[${St}]);
1739      let nxy = ${a.getByIndices("grid_indices")};
1740      var x = gs_denormalize(f32(nxy[0]), W_in);
1741      var y = gs_denormalize(f32(nxy[1]), H_in);
1742
1743      ${ml(s,u,t)}
1744  }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:h=>{let g=O.size(n);return{outputs:[{dims:n,dataType:h[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:p}},getShaderSource:f}},ch=(e,t)=>{ll(e.inputs),e.compute(gl(e.inputs,t))},hh=e=>fe({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),Ae,_l,fh,Qi,yl,dr,mh,gh=U(()=>{te(),ie(),xe(),Wa(),Ha(),ae(),yt(),Ae=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,_l=(e,t)=>{let r=e[0],i=Ae(e,1),a=Ae(e,2),n=Ae(e,3),s=Ae(e,4),u=Ae(e,5),l=Ae(e,6),p=Ae(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 f=r.dims[0],h=r.dims[1],g=r.dims.length===3?r.dims[2]:t.numHeads*r.dims[4],_=h,y=0,b=0,S=Math.floor(g/t.numHeads);if(l&&p&&O.size(l.dims)&&O.size(p.dims)){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(l.dims[0]!==f||l.dims[1]!==t.numHeads||l.dims[3]!==S)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(p.dims[0]!==f||p.dims[1]!==t.numHeads||p.dims[3]!==S)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(l.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');y=l.dims[2],b=l.dims[2]}else if(l&&O.size(l.dims)||p&&O.size(p.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let x;if(i&&O.size(i.dims)>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(i.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');x=2,_=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==S)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(a)throw new Error('Expect "value" be none when "key" has packed kv format.');x=5,_=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==S)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');x=0,_=i.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');x=3}if(n&&O.size(n.dims)>0){if(n.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');
vendor: 9,346 bytes, lines 1744-1792
1744if(i&&i.dims.length===5&&i.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let w=y+_,I=0;if(s&&O.size(s.dims)>0){I=8;let A=s.dims;throw A.length===1?A[0]===f?I=1:A[0]===3*f+2&&(I=3):A.length===2&&A[0]===f&&A[1]===w&&(I=5),I===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let k=!1,E=g;if(a&&O.size(a.dims)>0){if(a.dims.length!==3&&a.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==a.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(a.dims.length===3){if(_!==a.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');E=a.dims[2]}else{if(_!==a.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');E=a.dims[1]*a.dims[3],k=!0}}let C=!1;if(s&&O.size(s.dims)>0)throw new Error("Key padding mask is not supported");if(u&&O.size(u.dims)>0){if(u.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(u.dims[0]!==f||u.dims[1]!==t.numHeads||u.dims[2]!==h||u.dims[3]!==w)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:f,sequenceLength:h,pastSequenceLength:y,kvSequenceLength:_,totalSequenceLength:w,maxSequenceLength:b,inputHiddenSize:0,hiddenSize:g,vHiddenSize:E,headSize:S,vHeadSize:Math.floor(E/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:I,scale:t.scale,broadcastResPosBias:C,passPastInKv:k,qkvFormat:x}},fh=e=>fe({...e}),Qi=fe({perm:[0,2,1,3]}),yl=(e,t,r,i,a,n,s)=>{let u=[i,a,n],l=O.size(u),p=[{type:12,data:l},{type:12,data:s},{type:12,data:n}],f=h=>{let g=X("qkv_with_bias",t.dataType,u),_=N("qkv",t.dataType,u),y=N("bias",r.dataType,u),b=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return`
1745  ${h.registerUniforms(b).declareVariables(_,y,g)}
1746  ${h.mainStart()}
1747    ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1748    let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset;
1749
1750    qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx];
1751  }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:u,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:p}),getShaderSource:f},{inputs:[t,r],outputs:[-1]})[0]},dr=(e,t,r,i,a,n,s,u)=>{let l=n;if(s&&O.size(s.dims)>0){if(i===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return l=yl(e,n,s,t,i,r*a,u),l=l.reshape([t,i,r,a]),r===1||i===1?l:e.compute(qe(l,Qi.perm),{inputs:[l],outputs:[-1]})[0]}else return n.dims.length===3&&(l=n.reshape([t,i,r,a])),r===1||i===1?l:e.compute(qe(l,Qi.perm),{inputs:[l],outputs:[-1]})[0]},mh=(e,t)=>{let r=_l(e.inputs,t),i=e.inputs[0],a=Ae(e.inputs,1),n=Ae(e.inputs,2),s=Ae(e.inputs,3),u=Ae(e.inputs,4),l=Ae(e.inputs,5),p=Ae(e.inputs,6),f=Ae(e.inputs,7);if(i.dims.length===5)throw new Error("Packed QKV is not implemented");if((a==null?void 0:a.dims.length)===5)throw new Error("Packed KV is not implemented");let h=a&&n&&a.dims.length===4&&n.dims.length===4,g=dr(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,i,s,0);if(h)return hr(e,g,a,n,u,void 0,p,f,l,r);if(!a||!n)throw new Error("key and value must be provided");let _=dr(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,a,s,r.hiddenSize),y=dr(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,n,s,2*r.hiddenSize);hr(e,g,_,y,u,void 0,p,f,l,r)}}),bl,wl,$l,vl,Ea,_h,yh,bh=U(()=>{te(),ie(),xe(),ae(),bl=e=>{if(!e||e.length<1)throw new Error("too few inputs")},wl=(e,t)=>{let r=[],i=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(a=>r.push(Number(a))),i=r.length),fe({numOutputs:i,axis:t.axis,splitSizes:r})},$l=e=>`
1752fn calculateOutputIndex(index: u32) -> u32 {
1753    for (var i: u32 = 0u; i < ${e}u; i += 1u ) {
1754    if (index < ${Y("uniforms.size_in_split_axis","i",e)}) {
1755        return i;
1756    }
1757    }
1758    return ${e}u;
1759}`,vl=e=>{let t=e.length,r=[];for(let i=0;i<t;++i){let a=e[i].setByIndices("indices","input[global_idx]");t===1?r.push(a):i===0?r.push(`if (output_number == ${i}u) { ${a} }`):i===t-1?r.push(`else { ${a} }`):r.push(`else if (output_number == ${i}) { ${a} }`)}return`
1760      fn writeBufferData(output_number: u32, indices: ${e[0].type.indices}, global_idx: u32) {
1761        ${r.join(`
1762`)}
1763      }`},Ea=(e,t)=>{let r=e[0].dims,i=O.size(r),a=e[0].dataType,n=O.normalizeAxis(t.axis,r.length),s=new Array(t.numOutputs),u=N("input",a,r.length),l=new Array(t.numOutputs),p=[],f=[],h=0,g=[{type:12,data:i}];for(let y=0;y<t.numOutputs;y++){h+=t.splitSizes[y],l[y]=h;let b=r.slice();b[n]=t.splitSizes[y],f.push(b),s[y]=X(`output${y}`,a,b.length),p.push({dims:f[y],dataType:e[0].dataType})}g.push({type:12,data:l},...J(r,...f));let _=y=>`
1764  ${y.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",l.length).declareVariables(u,...s)}
1765  ${$l(l.length)}
1766  ${vl(s)}
1767
1768  ${y.mainStart()}
1769    ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")}
1770
1771    var indices = ${u.offsetToIndices("global_idx")};
1772    var index = ${u.indicesGet("indices",n)};
1773    let output_number = calculateOutputIndex(index);
1774    if (output_number != 0) {
1775      index -= ${Y("uniforms.size_in_split_axis","output_number - 1u",l.length)};
1776      ${u.indicesSet("indices",n,"index")};
1777    }
1778    writeBufferData(output_number, indices, global_idx);
1779  }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:p,dispatchGroup:{x:Math.ceil(i/64)},programUniforms:g})}},_h=(e,t)=>{bl(e.inputs);let r=e.inputs.length===1?t:wl(e.inputs,t);e.compute(Ea(e.inputs,r),{inputs:[0]})},yh=e=>{let t=e.axis,r=e.splitSizes,i=e.numOutputs<0?r.length:e.numOutputs;if(i!==r.length)throw new Error("numOutputs and splitSizes length must be equal");return fe({axis:t,numOutputs:i,splitSizes:r})}}),xl,Kr,wh,$h=U(()=>{te(),ie(),xe(),ae(),xl=(e,t)=>{let[r,i,a,n]=e,{numHeads:s,rotaryEmbeddingDim:u}=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(!O.areEqual(i.dims,[])&&!O.areEqual(i.dims,[1])&&i.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${i.dims.length}`);if(a.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${a.dims.length}`);if(n.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${n.dims.length}`);if(!O.areEqual(a.dims,n.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(u>0&&s===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let l=r.dims[0],p=r.dims[r.dims.length-2],f=a.dims[0],h=O.sizeFromDimension(r.dims,1)/p,g=u===0?a.dims[1]*2:h/s;if(u>g)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(i.dims.length===2){if(l!==i.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${i.dims[0]}`);if(p!==i.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${i.dims[1]}`)}if(p>f)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported");if(g/2!==a.dims[1]&&u/2!==a.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${a.dims[1]}`)},Kr=(e,t)=>{let{interleaved:r,numHeads:i,rotaryEmbeddingDim:a,scale:n}=t,s=e[0].dims[0],u=O.sizeFromDimension(e[0].dims,1),l=e[0].dims[e[0].dims.length-2],p=u/l,f=e[2].dims[1],h=a===0?f*2:p/i,g=new Array(s,l,p/h,h-f),_=O.computeStrides(g),y=[{type:1,data:n},{type:12,data:g},{type:12,data:_},...e[0].dims.length===3?new Array({type:12,data:[u,p,h,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[u,h,l*h,1]}):[],...J(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],b=S=>{let x=N("input",e[0].dataType,e[0].dims.length),w=N("position_ids",e[1].dataType,e[1].dims.length),I=N("cos_cache",e[2].dataType,e[2].dims.length),k=N("sin_cache",e[3].dataType,e[3].dims.length),E=X("output",e[0].dataType,e[0].dims.length);return S.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:g.length},{name:"global_strides",type:"u32",length:_.length},{name:"input_output_strides",type:"u32",length:_.length}]),`
1780        ${S.declareVariables(x,w,I,k,E)}
1781
1782        ${S.mainStart(Gt)}
1783          let half_rotary_emb_dim = uniforms.${I.name}_shape[1];
1784          let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape;
1785          let size = uniforms.global_shape[0] * uniforms.global_strides[0];
1786          ${S.guardAgainstOutOfBoundsWorkgroupSizes("size")}
1787
1788          if (bsnh[3] < half_rotary_emb_dim) {
1789            let position_ids_idx =
1790                ${w.broadcastedIndicesToOffset("bsnh.xy",X("",w.type.tensor,2))};
1791            let position_id =
1792                u32(${w.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0);
1793            let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r});
1794            let j = i + select(half_rotary_emb_dim, 1, ${r});
1795            let re = ${x.getByOffset("i")} * ${I.get("position_id","bsnh[3]")} -
1796                ${x.getByOffset("j")} * ${k.get("position_id","bsnh[3]")};
1797            ${E.setByOffset("i","re")}
1798            let im = ${x.getByOffset("i")} * ${k.get("position_id","bsnh[3]")} +
1799                ${x.getByOffset("j")} * ${I.get("position_id","bsnh[3]")};
1800            ${E.setByOffset("j","im")}
1801          } else {
1802            let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim;
1803            ${E.setByOffset("k",x.getByOffset("k"))}
1804          }
1805        }`};return{name:"RotaryEmbedding",shaderCache:{hint:fe({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:b,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(O.size(g)/Gt)},programUniforms:y})}},wh=(e,t)=>{xl(e.inputs,t),e.compute(Kr(e.inputs,t))}}),Sl,kl,Xi,Tl,vh,S_=U(()=>{xe(),te(),Ha(),gh(),bh(),yt(),$h(),ae(),Sl=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let r=e[0],i=e[1],a=e[2],n=e[3],s=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let u=!1,l=r.dims[0],p=r.dims[1],f=r.dims.length===3?u?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],h=p,g=0,_=!i||i.dims.length===0,y=Math.floor(_?f/(t.numHeads+2*t.kvNumHeads):f/t.numHeads);_&&(f=y*t.numHeads);let b=n&&n.dims.length!==0,S=s&&s.dims.length!==0;if(b&&n.dims.length===4&&n.dims[0]===l&&n.dims[1]!==t.kvNumHeads&&n.dims[2]===t.kvNumHeads&&n.dims[3]===y)throw new Error("BSNH pastKey/pastValue is not supported");if(b&&S){if(n.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(s.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');g=n.dims[2]}else if(b||S)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let x=1;if(i&&i.dims.length>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(r.dims[2]%i.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');h=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==y)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(a)throw new Error('Expect "value" be none when "key" has packed kv format.');h=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==y)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');h=i.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');x=3}let w=0,I=!1,k=t.kvNumHeads?y*t.kvNumHeads:f;if(a&&a.dims.length>0){if(a.dims.length!==3&&a.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==a.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(a.dims.length===3){if(h!==a.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');k=a.dims[2]}else{if(h!==a.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');k=a.dims[1]*a.dims[3],I=!0}}let E=e.length>4?e[5]:void 0;if(E){if(E.dims.length===0)throw new Error("seqlens_k must be at least 1D, got scalar.");let C=E.dims.reduce((A,v)=>A*v,1);if(C!==l)throw new Error(`seqlens_k must have batch_size (${l}) elements, got ${C}.`);for(let A=0;A<E.dims.length;A++)if(E.dims[A]!==1&&E.dims[A]!==l)throw new Error(`seqlens_k has unexpected shape. Each dimension must be 1 or batch_size (${l}), got dims[${A}] = ${E.dims[A]}.`)}return{batchSize:l,sequenceLength:p,pastSequenceLength:g,kvSequenceLength:h,totalSequenceLength:-1,maxSequenceLength:-1,inputHiddenSize:0,hiddenSize:f,vHiddenSize:k,headSize:y,vHeadSize:Math.floor(k/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:w,scale:t.scale,broadcastResPosBias:!1,passPastInKv:I,qkvFormat:x}},kl=fe({perm:[0,2,1,3]}),Xi=(e,t,r)=>{let i=t,a=r.kvNumHeads;return t.dims.length===3&&r.kvSequenceLength!==0&&(i=t.reshape([r.batchSize,r.kvSequenceLength,a,r.headSize]),i=e.compute(qe(i,kl.perm),{inputs:[i],outputs:[-1]})[0]),i},Tl=(e,t,r,i)=>{let a=7,n=["type","type"],s=[e*t],u=e*t,l=[{type:12,data:u},{type:12,data:t},{type:12,data:e}],p=f=>{let h=N("seq_lens",r.dataType,r.dims),g=N("total_seq_lens",i.dataType,i.dims),_=X("pos_ids",a,s),y=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return`
1806  ${f.registerUniforms(y).declareVariables(h,g,_)}
1807  ${f.mainStart()}
1808    ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1809    let total_sequence_length = u32(${g.getByOffset("0")});
1810    let is_subsequent_prompt = uniforms.sequence_length > 1 && uniforms.sequence_length != total_sequence_length;
1811    let is_first_prompt = !is_subsequent_prompt && uniforms.sequence_length == total_sequence_length;
1812    let batch_idx = global_idx / uniforms.sequence_length;
1813    let sequence_idx = i32(global_idx % uniforms.sequence_length);
1814    var pos_id: i32 = 0;
1815    let seqlen = ${h.getByOffset("batch_idx")};
1816    let total_seqlen = seqlen + 1;
1817    if (is_first_prompt) {
1818      if (sequence_idx < total_seqlen) {
1819        pos_id = sequence_idx;
1820      } else {
1821        pos_id = 1;
1822      }
1823      ${_.setByOffset("global_idx","pos_id")}
1824    } else if (is_subsequent_prompt) {
1825      let past_seqlen = total_seqlen - i32(uniforms.sequence_length);
1826      if (past_seqlen + sequence_idx < total_seqlen) {
1827        pos_id = past_seqlen + sequence_idx;
1828      } else {
1829        pos_id = 1;
1830      }
1831      ${_.setByOffset("global_idx","pos_id")}
1832    } else if (global_idx < uniforms.batch_size) {
1833      ${_.setByOffset("global_idx","seqlen")}
1834    };
1835  }
1836  `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:n},getRunData:()=>({outputs:[{dims:s,dataType:a}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:l}),getShaderSource:p}},vh=(e,t)=>{var k;
vendor: 18,622 bytes, lines 1836-2020
1836let r=Sl(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(((k=e.inputs[1])==null?void 0:k.dims.length)===5)throw new Error("Packed KV is not implemented");let i=e.inputs[0],a=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,n=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,s=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,u=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,l=e.inputs.length>4?e.inputs[5]:void 0,p=e.inputs.length>5?e.inputs[6]:void 0,f=r.kvNumHeads?r.kvNumHeads:r.numHeads,h=fe({axis:2,numOutputs:3,splitSizes:[r.numHeads*r.headSize,f*r.headSize,f*r.headSize]}),[g,_,y]=!a&&!n?e.compute(Ea([i],h),{inputs:[i],outputs:[-1,-1,-1]}):[i,a,n],b,S;if(t.doRotary){let E=e.compute(Tl(r.batchSize,r.sequenceLength,l,p),{inputs:[l,p],outputs:[-1]})[0],C=e.inputs[7],A=e.inputs[8],v=fe({interleaved:t.rotaryInterleaved!==0,numHeads:r.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),M=[g,E,C,A],D=[-1];b=e.compute(Kr(M,v),{inputs:M,outputs:D})[0],M.splice(0,1,_);let H=fe({interleaved:t.rotaryInterleaved!==0,numHeads:r.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});S=e.compute(Kr(M,H),{inputs:M,outputs:D})[0]}let x=dr(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,t.doRotary?b:g,void 0,0),w=Xi(e,t.doRotary?S:_,r),I=Xi(e,y,r);hr(e,x,w,I,void 0,void 0,s,u,void 0,r,l,p)}}),Yi,Il,El,xh,k_=U(()=>{te(),ie(),yt(),ae(),Yi=(e,t,r,i,a,n,s,u)=>{let l=ve(n),p=l===1?"f32":`vec${l}f`,f=l===1?"vec2f":`mat2x${l}f`,h=a*s,g=64;h===1&&(g=256);let _=[a,s,n/l],y=[a,s,2],b=["rank","type","type"],S=[];S.push(...J(_,y));let x=w=>{let I=N("x",t.dataType,3,l),k=N("scale",r.dataType,r.dims),E=N("bias",i.dataType,i.dims),C=X("output",1,3,2),A=[I,k,E,C];return`
1837  var<workgroup> workgroup_shared : array<${f}, ${g}>;
1838  const workgroup_size = ${g}u;
1839  ${w.declareVariables(...A)}
1840  ${w.mainStart(g)}
1841    let batch = workgroup_index / uniforms.x_shape[1];
1842    let channel = workgroup_index % uniforms.x_shape[1];
1843    let hight = uniforms.x_shape[2];
1844    // initialize workgroup memory
1845    var sum = ${p}(0);
1846    var squared_sum = ${p}(0);
1847    for (var h = local_idx; h < hight; h += workgroup_size) {
1848      let value = ${p}(${I.get("batch","channel","h")});
1849      sum += value;
1850      squared_sum += value * value;
1851    }
1852    workgroup_shared[local_idx] = ${f}(sum, squared_sum);
1853    workgroupBarrier();
1854
1855    for (var currSize = workgroup_size >> 1;  currSize > 0; currSize = currSize >> 1) {
1856      if (local_idx < currSize) {
1857        workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize];
1858      }
1859      workgroupBarrier();
1860    }
1861    if (local_idx == 0) {
1862      let sum_final = ${_t("workgroup_shared[0][0]",l)} / f32(hight * ${l});
1863      let squared_sum_final = ${_t("workgroup_shared[0][1]",l)} / f32(hight * ${l});
1864
1865      let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${u}));
1866      let channel_scale = inv_std_dev * f32(scale[channel]);
1867      let channel_shift = f32(bias[channel]) - sum_final * channel_scale;
1868      output[workgroup_index] = vec2f(channel_scale, channel_shift);
1869    }
1870  }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${l};${u};${g}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:y,dataType:1}],dispatchGroup:{x:h},programUniforms:S}),getShaderSource:x},{inputs:[t,r,i],outputs:[-1]})[0]},Il=(e,t,r)=>{let i=t[0].dims,a=i,n=2,s=i[0],u=i[1],l=O.sizeFromDimension(i,n),p=ve(l),f=O.size(a)/p,h=Yi(e,t[0],t[1],t[2],s,l,u,r.epsilon),g=[s,u,l/p],_=[s,u],y=["type","none"],b=S=>{let x=N("x",t[0].dataType,g.length,p),w=N("scale_shift",1,_.length,2),I=X("output",t[0].dataType,g.length,p),k=[x,w,I];return`
1871  ${S.registerUniform("output_size","u32").declareVariables(...k)}
1872  ${S.mainStart()}
1873  ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1874      let outputIndices = ${I.offsetToIndices("global_idx")};
1875      let batch = outputIndices[0];
1876      let channel = outputIndices[1];
1877      let scale_shift = ${w.getByIndices("vec2<u32>(batch, channel)")};
1878      let value = ${x.getByOffset("global_idx")} * ${I.type.value}(scale_shift.x) + ${I.type.value}(scale_shift.y);
1879      ${I.setByOffset("global_idx","value")};
1880  }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${p}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:a,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:[{type:12,data:f},...J(g,_,g)]}),getShaderSource:b},{inputs:[t[0],h]})},El=(e,t,r)=>{let i=t[0].dims,a=i,n=i[0],s=i[i.length-1],u=O.sizeFromDimension(i,1)/s,l=ve(s),p=O.size(a)/l,f=[{type:12,data:u},{type:12,data:Math.floor(s/l)}],h=["type","type"],g=!1,_=[0,i.length-1];for(let x=0;x<i.length-2;x++)g=g||i[x+1]!==1,_.push(x+1);g=g&&i[i.length-1]!==1;let y=g?e.compute(qe(e.inputs[0],_),{inputs:[e.inputs[0]],outputs:[-1]})[0]:e.inputs[0].reshape(Array.from({length:i.length},(x,w)=>i[_[w]])),b=Yi(e,y,t[1],t[2],n,u,s,r.epsilon),S=x=>{let w=Te(t[0].dataType),I=l===1?"vec2f":`mat${l}x2f`,k=A=>{let v=A===0?"x":"y",M=l===1?"f32":`vec${l}f`;switch(l){case 1:return`${w}(${M}(scale.${v}))`;case 2:return`vec2<${w}>(${M}(scale[0].${v}, scale[1].${v}))`;case 4:return`vec4<${w}>(${M}(scale[0].${v}, scale[1].${v}, scale[2].${v}, scale[3].${v}))`;default:throw new Error(`Not supported compoents ${l}`)}},E=N("input",t[0].dataType,t[0].dims,l),C=X("output",t[0].dataType,a,l);return`
1881  @group(0) @binding(0) var<storage, read> input : array<${E.type.storage}>;
1882  @group(0) @binding(1) var<storage, read> scale_input : array<${I}>;
1883  @group(0) @binding(2) var<storage, read_write> output : array<${C.type.storage}>;
1884  struct Uniforms {H: u32, C : u32};
1885  @group(0) @binding(3) var<uniform> uniforms: Uniforms;
1886
1887  ${x.mainStart()}
1888    let current_image_number = global_idx / (uniforms.C * uniforms.H);
1889    let current_channel_number = global_idx % uniforms.C;
1890
1891    let scale_offset = current_image_number * uniforms.C + current_channel_number;
1892    let scale = scale_input[scale_offset];
1893    output[global_idx] = fma(input[global_idx], ${k(0)}, ${k(1)});
1894  }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${l}`,inputDependencies:h},getRunData:()=>({outputs:[{dims:a,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:f}),getShaderSource:S},{inputs:[t[0],b]})},xh=(e,t)=>{t.format==="NHWC"?El(e,e.inputs,t):Il(e,e.inputs,t)}}),zl,Cl,Sh,T_=U(()=>{te(),ie(),ae(),zl=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},Cl=(e,t,r)=>{let i=t.simplified,a=e[0].dims,n=e[1],s=!i&&e[2],u=a,l=O.normalizeAxis(t.axis,a.length),p=O.sizeToDimension(a,l),f=O.sizeFromDimension(a,l),h=O.size(n.dims),g=s?O.size(s.dims):0;if(h!==f||s&&g!==f)throw new Error(`Size of X.shape()[axis:] == ${f}.
1895       Size of scale and bias (if provided) must match this.
1896       Got scale size of ${h} and bias size of ${g}`);let _=[];for(let E=0;E<a.length;++E)E<l?_.push(a[E]):_.push(1);let y=ve(f),b=["type","type"],S=[{type:12,data:p},{type:1,data:f},{type:12,data:Math.floor(f/y)},{type:1,data:t.epsilon}];s&&b.push("type");let x=r>1,w=r>2,I=E=>{let C=Te(e[0].dataType),A=[N("x",e[0].dataType,e[0].dims,y),N("scale",n.dataType,n.dims,y)];s&&A.push(N("bias",s.dataType,s.dims,y)),A.push(X("output",e[0].dataType,u,y)),x&&A.push(X("mean_data_output",1,_)),w&&A.push(X("inv_std_output",1,_));let v=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return`
1897  ${E.registerUniforms(v).declareVariables(...A)}
1898  ${E.mainStart()}
1899    ${E.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")}
1900    let offset = global_idx * uniforms.norm_size_vectorized;
1901    var mean_vector = ${ba("f32",y)};
1902    var mean_square_vector = ${ba("f32",y)};
1903
1904    for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) {
1905      let value = ${Wt(C,y,"x[h + offset]")};
1906      mean_vector += value;
1907      mean_square_vector += value * value;
1908    }
1909    let mean = ${_t("mean_vector",y)} / uniforms.norm_size;
1910    let inv_std_dev = inverseSqrt(${_t("mean_square_vector",y)} / uniforms.norm_size ${i?"":"- mean * mean"} + uniforms.epsilon);
1911
1912    for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) {
1913      let f32input = ${Wt(C,y,"x[j + offset]")};
1914      let f32scale = ${Wt(C,y,"scale[j]")};
1915      output[j + offset] = ${A[0].type.value}((f32input ${i?"":"- mean"}) * inv_std_dev * f32scale
1916        ${s?`+ ${Wt(C,y,"bias[j]")}`:""}
1917      );
1918    }
1919
1920    ${x?"mean_data_output[global_idx] = mean":""};
1921    ${w?"inv_std_output[global_idx] = inv_std_dev":""};
1922  }`},k=[{dims:u,dataType:e[0].dataType}];return x&&k.push({dims:_,dataType:1}),w&&k.push({dims:_,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${y};${r};${i}`,inputDependencies:b},getRunData:()=>({outputs:k,dispatchGroup:{x:Math.ceil(p/64)},programUniforms:S}),getShaderSource:I}},Sh=(e,t)=>{zl(e.inputs),e.compute(Cl(e.inputs,t,e.outputCount))}}),Al,kh,I_=U(()=>{ie(),Qa(),Xa(),Al=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.")},kh=e=>{Al(e.inputs);let t=Vt.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],i=e.inputs[0].dims[e.inputs[0].dims.length-1];if(r<8&&i<8)e.compute(Za(e.inputs,{activation:""},t));else{let a=t[t.length-2],n=O.size(e.inputs[0].dims.slice(0,-2)),s=O.size(e.inputs[1].dims.slice(0,-2));if(n!==1&&a===1&&s===1){let u=e.inputs[0].reshape([1,n,i]),l=e.inputs[1].reshape([1,i,r]),p=[1,n,r],f=[u,l];e.compute(jr(f,{activation:""},t,p),{inputs:f})}else e.compute(jr(e.inputs,{activation:""},t))}}}),Ol,Rl,Bl,Th,Ih,E_=U(()=>{te(),ie(),xe(),ae(),Ol=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],i=r.dims.length;if(r.dims[i-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let a=Math.floor((t.k+t.blockSize-1)/t.blockSize),n=t.blockSize/8*t.bits,s=e[1];if(!O.areEqual(s.dims,[t.n,a,n]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let u=e[2].dims;if(O.size(u)!==t.n*a)throw new Error("scales input size error.");if(e.length===4){let l=e[3].dims,p=t.n*(t.bits===8?a:Math.floor((a*t.bits+7)/8));if(O.size(l)!==p)throw new Error("zeroPoints input size error.")}},Rl=(e,t)=>{let r=e[0].dims,i=r.length,a=r[i-2],n=t.k,s=t.n,u=r.slice(0,i-2),l=O.size(u),p=e[1].dims[2]/4,f=e[0].dataType,h=ve(t.k),g=ve(p),_=ve(s),y=u.concat([a,s]),b=a>1&&s/_%2===0?2:1,S=O.size(y)/_/b,x=64,w=[],I=[l,a,n/h],k=O.convertShape(e[1].dims).slice();k.splice(-1,1,p/g),w.push(...J(I)),w.push(...J(k)),w.push(...J(e[2].dims)),e.length===4&&w.push(...J(O.convertShape(e[3].dims)));let E=[l,a,s/_];w.push(...J(E));let C=A=>{let v=I.length,M=N("a",e[0].dataType,v,h),D=N("b",12,k.length,g),H=N("scales",e[2].dataType,e[2].dims.length),F=[M,D,H],j=e.length===4?N("zero_points",12,e[3].dims.length):void 0;j&&F.push(j);let R=E.length,K=X("output",e[0].dataType,R,_),Z=Te(e[0].dataType),ee=(()=>{switch(h){case 1:return`array<${Z}, 8>`;case 2:return`mat4x2<${Z}>`;case 4:return`mat2x4<${Z}>`;default:throw new Error(`${h}-component is not supported.`)}})(),he=Math.floor(32/t.bits),W=Math.floor(he/8),ue=()=>{let Q="";for(let q=0;q<W;q++){let me=q*t.bits*4,Le=me+t.bits;Q+=`
1923          // reuse a data (pass ${q})
1924            var input_offset${q>0?q:""} = ${q===0?M.indicesToOffset(`${M.type.indices}(batch, row, word_offset)`):"input_offset"};
1925            var a_data${q>0?q:""}: ${ee};
1926            for (var j${q>0?q:""}: u32 = 0; j${q>0?q:""} < ${8/h}; j${q>0?q:""}++) {
1927              a_data${q>0?q:""}[j${q>0?q:""}] = ${M.getByOffset(`input_offset${q>0?q:""}`)};
1928              input_offset${q>0?q:""}++;
1929            }
1930          `;for(let Se=0;Se<_*b;Se++)Q+=`
1931            b_value = ${g===1?`b${Se}_data`:`b${Se}_data[i]`};
1932            ${t.bits===2?`{
1933              let half_word = b_value >> ${q*16}u;
1934              let byte_lo = half_word & 0xFFu;
1935              let byte_hi = (half_word >> 8u) & 0xFFu;
1936              let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u);
1937              b_value_lower = unpack4xU8(spread_word & b_mask);
1938              b_value_upper = unpack4xU8((spread_word >> 2u) & b_mask);
1939            }`:`b_value_lower = unpack4xU8((b_value >> ${me}u) & b_mask);
1940            b_value_upper = unpack4xU8((b_value >> ${Le}u) & b_mask);`}
1941            b_quantized_values = ${ee}(${Array.from({length:4},(Oe,Re)=>`${Z}(b_value_lower[${Re}]), ${Z}(b_value_upper[${Re}])`).join(", ")});
1942            b_dequantized_values = ${h===1?`${ee}(${Array.from({length:8},(Oe,Re)=>`(b_quantized_values[${Re}] - ${j?`zero_point${Se}`:"zero_point"}) * scale${Se}`).join(", ")});`:`(b_quantized_values - ${ee}(${Array(8).fill(`${j?`zero_point${Se}`:"zero_point"}`).join(",")})) * scale${Se};`};
1943            workgroup_shared[local_id.x * ${b} + ${Math.floor(Se/_)}]${_>1?`[${Se%_}]`:""} += ${Array.from({length:8/h},(Oe,Re)=>`${h===1?`a_data${q>0?q:""}[${Re}] * b_dequantized_values[${Re}]`:`dot(a_data${q>0?q:""}[${Re}], b_dequantized_values[${Re}])`}`).join(" + ")};
1944          `}return Q},P=()=>{let Q=`
1945            var col_index = col * ${_};
1946            ${j?`
1947            let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u;
1948            let zero_point_bytes_per_col = (nBlocksPerCol + zero_point_values_per_byte - 1u) / zero_point_values_per_byte;
1949            var zero_point_byte_count: u32;
1950            var zero_point_word_index: u32;
1951            var zero_point_byte_offset: u32;
1952            let zero_point_sub_offset: u32 = block % zero_point_values_per_byte;
1953            var zero_point_bits_offset: u32;
1954            var zero_point_word: u32;`:`
1955            // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization.
1956            let zero_point = ${Z}(${Math.pow(2,t.bits-1).toFixed(1)});`}
1957            `;for(let q=0;q<_*b;q++)Q+=`
1958            let scale${q} = ${H.getByOffset("col_index * nBlocksPerCol + block")};
1959            ${j?`
1960            zero_point_byte_count = col_index * zero_point_bytes_per_col + (block / zero_point_values_per_byte);
1961            zero_point_word_index = zero_point_byte_count >> 0x2u;
1962            zero_point_byte_offset = zero_point_byte_count & 0x3u;
1963            zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u);
1964            zero_point_word = ${j.getByOffset("zero_point_word_index")} >> zero_point_bits_offset;
1965            let zero_point${q} = ${Z}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:""}
1966            col_index += 1;`;return Q},V=()=>{let Q=`col_index = col * ${_};`;for(let q=0;q<_*b;q++)Q+=`
1967            let b${q}_data = ${D.getByIndices(`${D.type.indices}(col_index, block, word)`)};
1968            col_index += 1;`;return Q+=`
1969            var b_value: u32;
1970            let b_mask: u32 = ${t.bits===2?"0x03030303u":"0x0F0F0F0Fu"};
1971            var b_value_lower: vec4<u32>;
1972            var b_value_upper: vec4<u32>;
1973            var b_quantized_values: ${ee};
1974            var b_dequantized_values: ${ee};`,Q};return`
1975        var<workgroup> workgroup_shared: array<${K.type.value}, ${b*x}>;
1976        ${A.declareVariables(...F,K)}
1977        ${A.mainStart([x,1,1])}
1978          let output_indices = ${K.offsetToIndices(`(global_idx / ${x}) * ${b}`)};
1979          let col = output_indices[2];
1980          let row = output_indices[1];
1981          let batch = output_indices[0];
1982          let nBlocksPerCol = uniforms.b_shape[1];
1983
1984          for (var block = local_id.x; block < nBlocksPerCol; block += ${x}) {
1985            //process one block
1986            var word_offset: u32 = block * ${t.blockSize/h};
1987            ${P()}
1988            for (var word: u32 = 0; word < ${p}; word += ${g}) {
1989              ${V()}
1990              for (var i: u32 = 0; i < ${g}; i++) {
1991                ${ue()}
1992                word_offset += ${he/h};
1993              }
1994            }
1995          }
1996          workgroupBarrier();
1997
1998          if (local_id.x < ${b}) {
1999            var output_value: ${K.type.value} = ${K.type.value}(0);
2000            var workgroup_shared_offset: u32 = local_id.x;
2001            for (var b: u32 = 0u; b < ${x}u; b++) {
2002              output_value += workgroup_shared[workgroup_shared_offset];
2003              workgroup_shared_offset += ${b};
2004            }
2005            ${K.setByIndices(`${K.type.indices}(batch, row, col + local_id.x)`,"output_value")};
2006          }
2007        }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${h};${g};${_};${b};${x}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:y,dataType:f}],dispatchGroup:{x:S},programUniforms:w}),getShaderSource:C}},Bl=(e,t)=>{let r=e[0].dims,i=r.length,a=r[i-2],n=t.k,s=t.n,u=r.slice(0,i-2),l=O.size(u),p=e[1].dims[2]/4,f=e[0].dataType,h=ve(t.k),g=ve(p),_=u.concat([a,s]),y=128,b=s%8===0?8:s%4===0?4:1,S=y/b,x=Math.floor(32/t.bits),w=S*g*x,I=w/h,k=w/t.blockSize,E=O.size(_)/b,C=[],A=[l,a,n/h],v=O.convertShape(e[1].dims).slice();v.splice(-1,1,p/g),C.push(...J(A)),C.push(...J(v)),C.push(...J(e[2].dims)),e.length===4&&C.push(...J(O.convertShape(e[3].dims)));let M=[l,a,s];C.push(...J(M));let D=H=>{let F=A.length,j=N("a",e[0].dataType,F,h),R=N("b",12,v.length,g),K=N("scales",e[2].dataType,e[2].dims.length),Z=[j,R,K],ee=e.length===4?N("zero_points",12,e[3].dims.length):void 0;ee&&Z.push(ee);let he=M.length,W=X("output",e[0].dataType,he),ue=Te(e[0].dataType),P=()=>{switch(h){case 1:return`
2008          let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]);
2009          let a_data1 = vec4<${ue}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return`
2010          let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1]);
2011          let a_data1 = vec4<${ue}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return`
2012          let a_data0 = sub_a[word_offset];
2013          let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${h}-component is not supported.`)}};return`
2014        var<workgroup> sub_a: array<${j.type.value}, ${I}>;
2015        var<workgroup> inter_results: array<array<${W.type.value}, ${S}>, ${b}>;
2016        ${H.declareVariables(...Z,W)}
2017        ${H.mainStart([S,b,1])}
2018          let output_indices = ${W.offsetToIndices(`workgroup_index * ${b}`)};
2019          let col = output_indices[2];
2020          let row = output_indices[1];
2021          let batch = output_indices[0];
2022          let n_blocks_per_col = uniforms.b_shape[1];
2023          let num_tiles =  (n_blocks_per_col - 1) / ${k} + 1;
2024
2025          // Loop over shared dimension.
2026          for (var tile: u32 = 0; tile < num_tiles; tile += 1) {
2027            let a_col_start = tile * ${I};
2028            // load one tile A data into shared memory.
2029            for (var a_offset = local_idx; a_offset < ${I}; a_offset += ${y})
2030            {
2031              let a_col = a_col_start + a_offset;
2032              if (a_col < uniforms.a_shape[2])
2033              {
2034                sub_a[a_offset] = ${j.getByIndices(`${j.type.indices}(batch, row, a_col)`)};
2035              } else {
2036                sub_a[a_offset] = ${j.type.value}(0);
2037              }
2038            }
2039            workgroupBarrier();
2040
2041            // each thread process one block
2042            let b_row = col + local_id.y;
2043            let block = tile * ${k} + local_id.x;
2044            ${ee?`
2045            let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u;
2046            let zero_point_bytes_per_col = (n_blocks_per_col + zero_point_values_per_byte - 1u) / zero_point_values_per_byte;
2047            let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block / zero_point_values_per_byte);
2048            let zero_point_word_index = zero_point_byte_count >> 0x2u;
2049            let zero_point_byte_offset = zero_point_byte_count & 0x3u;
2050            let zero_point_sub_offset: u32 = block % zero_point_values_per_byte;
2051            let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u);
2052            let zero_point_word = ${ee.getByOffset("zero_point_word_index")} >> zero_point_bits_offset;
2053            let zero_point = ${ue}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:`
2054            // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization.
2055            let zero_point = ${ue}(${Math.pow(2,t.bits-1).toFixed(1)});`}
2056            let scale = ${K.getByOffset("b_row * n_blocks_per_col + block")};
2057            let b_data = ${R.getByIndices(`${R.type.indices}(b_row, block, 0)`)};
2058            var word_offset = local_id.x * ${t.blockSize/h};
2059            for (var i: u32 = 0; i < ${g}; i++) {
2060              let b_value = ${g===1?"b_data":"b_data[i]"};
2061              ${(()=>{let V=Math.floor(x/8),Q="";for(let q=0;q<V;q++){let me=q*t.bits*4,Le=me+t.bits;Q+=`
2062              ${P()}
2063              {${t.bits===2?`
2064                let half_word = b_value >> ${q*16}u;
2065                let byte_lo = half_word & 0xFFu;
2066                let byte_hi = (half_word >> 8u) & 0xFFu;
2067                let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u);
2068                let b_value_lower = unpack4xU8(spread_word & 0x03030303u);
2069                let b_value_upper = unpack4xU8((spread_word >> 2u) & 0x03030303u);`:`
2070                let b_value_lower = unpack4xU8((b_value >> ${me}u) & 0x0F0F0F0Fu);
2071                let b_value_upper = unpack4xU8((b_value >> ${Le}u) & 0x0F0F0F0Fu);`}
2072                let b_quantized_values = mat2x4<${ue}>(${Array.from({length:4},(Se,Oe)=>`${ue}(b_value_lower[${Oe}]), ${ue}(b_value_upper[${Oe}])`).join(", ")});
2073                let b_dequantized_values = (b_quantized_values - mat2x4<${ue}>(${Array(8).fill("zero_point").join(",")})) * scale;
2074                inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(Se,Oe)=>`${`dot(a_data${Oe}, b_dequantized_values[${Oe}])`}`).join(" + ")};
2075              }
2076              word_offset += ${8/h};`}return Q})()}
2077            }
2078            workgroupBarrier();
2079          }
2080
2081          if (local_idx < ${b}) {
2082            var output_value: ${W.type.value} = ${W.type.value}(0);
2083            for (var b = 0u; b < ${S}; b++) {
2084              output_value += inter_results[local_idx][b];
2085            }
2086            if (col + local_idx < uniforms.output_shape[2])
2087            {
2088              ${W.setByIndices(`${W.type.indices}(batch, row, col + local_idx)`,"output_value")}
2089            }
2090          }
2091        }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${h};${g};${S};${b}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:_,dataType:f}],dispatchGroup:{x:E},programUniforms:C}),getShaderSource:D}},Th=(e,t)=>
vendor: 9,896 bytes, lines 2091-2234
2091{Ol(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Bl(e.inputs,t)):e.compute(Rl(e.inputs,t))},Ih=e=>fe(e)}),Nl,Ml,Dl,Ul,Pl,ql,Ll,Wl,Eh,z_=U(()=>{te(),ie(),ae(),Nl=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].")}},Ml=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=`
2092            k = i32(${e.indicesGet("indices",a)}) - ${Y("uniforms.pads",a,r)};
2093            if (k < 0) {
2094              break;
2095            }
2096            if (k >= i32(${Y("uniforms.x_shape",a,t)})) {
2097              break;
2098            }
2099            offset += k * i32(${Y("uniforms.x_strides",a,t)});
2100        `;return`
2101          value = ${e.type.value}(uniforms.constant_value);
2102          for (var i = 0; i < 1; i++) {
2103            var offset = 0;
2104            var k = 0;
2105            ${i}
2106            value = x[offset];
2107          }
2108      `},Dl=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=`
2109                k = i32(${e.indicesGet("indices",a)}) - ${Y("uniforms.pads",a,r)};
2110                if (k < 0) {
2111                  k = -k;
2112                }
2113                {
2114                  let _2n_1 = 2 * (i32(${Y("uniforms.x_shape",a,t)}) - 1);
2115                  k = k % _2n_1;
2116                  if(k >= i32(${Y("uniforms.x_shape",a,t)})) {
2117                    k = _2n_1 - k;
2118                  }
2119                }
2120                offset += k * i32(${Y("uniforms.x_strides",a,t)});
2121            `;return`
2122              var offset = 0;
2123              var k = 0;
2124              ${i}
2125              value = x[offset];
2126          `},Ul=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=`
2127                k = i32(${e.indicesGet("indices",a)}) - ${Y("uniforms.pads",a,r)};
2128                if (k < 0) {
2129                  k = 0;
2130                }
2131                if (k >= i32(${Y("uniforms.x_shape",a,t)})) {
2132                  k = i32(${Y("uniforms.x_shape",a,t)}) - 1;
2133                }
2134                offset += k * i32(${Y("uniforms.x_strides",a,t)});
2135            `;return`
2136              var offset = 0;
2137              var k = 0;
2138              ${i}
2139              value = x[offset];
2140          `},Pl=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=`
2141                k = i32(${e.indicesGet("indices",a)}) - ${Y("uniforms.pads",a,r)};
2142                if (k < 0)  {
2143                  k += i32(${Y("uniforms.x_shape",a,t)}]);
2144                }
2145                if (k >= i32(${Y("uniforms.x_shape",a,t)})) {
2146                  k -= i32(${Y("uniforms.x_shape",a,t)});
2147                }
2148                offset += k * i32(${Y("uniforms.x_strides",a,t)});
2149            `;return`
2150              var offset = 0;
2151              var k = 0;
2152              ${i}
2153              value = x[offset];
2154          `},ql=(e,t,r)=>{switch(r.mode){case 0:return Ml(e,t,r.pads.length);case 1:return Dl(e,t,r.pads.length);case 2:return Ul(e,t,r.pads.length);case 3:return Pl(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Ll=(e,t)=>{let r=O.padShape(e[0].dims.slice(),t.pads),i=e[0].dims,a=O.size(r),n=[{type:12,data:a},{type:6,data:t.pads}],s=e.length>=3&&e[2].data;t.mode===0&&n.push({type:s?e[2].dataType:1,data:t.value}),n.push(...J(e[0].dims,r));let u=["rank"],l=p=>{let f=X("output",e[0].dataType,r.length),h=N("x",e[0].dataType,i.length),g=h.type.value,_=ql(f,i.length,t),y=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&y.push({name:"constant_value",type:s?g:"f32"}),`
2155            ${p.registerUniforms(y).declareVariables(h,f)}
2156            ${p.mainStart()}
2157            ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2158
2159            let indices = ${f.offsetToIndices("global_idx")};
2160
2161            var value = ${g}(0);
2162            ${_}
2163            output[global_idx] = value;
2164        }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${s}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(O.size(r)/64)},programUniforms:n}),getShaderSource:l}},Wl=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),i=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,a=e[0].dims.length,n=new Int32Array(2*a).fill(0);if(e.length>=4){let u=e[3].getBigInt64Array();for(let l=0;l<u.length;l++)n[Number(u[l])]=Number(r[l]),n[Number(u[l])+a]=Number(r[l+u.length])}else r.forEach((u,l)=>n[Number(l)]=Number(u));let s=[];return n.forEach(u=>s.push(u)),{mode:t.mode,value:i,pads:s}}else return t},Eh=(e,t)=>{Nl(e.inputs);let r=Wl(e.inputs,t);e.compute(Ll(e.inputs,r),{inputs:[0]})}}),ir,Ji,ea,ta,ra,Vl,Gl,ia,aa,zh,Ch,na,Ah,Oh,sa,Rh,Bh,Nh,Mh,C_=U(()=>{Ve(),te(),ie(),ae(),ir=e=>{if($e.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},Ji=(e,t,r)=>{let i=t.format==="NHWC",a=e.dims.slice();i&&a.splice(1,0,a.pop());let n=Object.hasOwnProperty.call(t,"dilations"),s=t.kernelShape.slice(),u=t.strides.slice(),l=n?t.dilations.slice():[],p=t.pads.slice();Hr.adjustPoolAttributes(r,a,s,u,l,p);let f=Hr.computePoolOutputShape(r,a,u,l,s,p,t.autoPad),h=Object.assign({},t);n?Object.assign(h,{kernelShape:s,strides:u,pads:p,dilations:l,cacheKey:t.cacheKey}):Object.assign(h,{kernelShape:s,strides:u,pads:p,cacheKey:t.cacheKey});let g=f.slice();return g.push(g.splice(1,1)[0]),[h,i?g:f]},ea=(e,t)=>{let r=t.format==="NHWC",i=O.size(e),a=O.size(t.kernelShape),n=[{type:12,data:i},{type:12,data:a}],s=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let u=t.kernelShape[t.kernelShape.length-1],l=t.strides[t.strides.length-1],p=t.pads[t.pads.length/2-1],f=t.pads[t.pads.length-1],h=!!(p+f);n.push({type:12,data:u},{type:12,data:l},{type:12,data:p},{type:12,data:f}),s.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let g=!1;if(t.kernelShape.length===2){let _=t.kernelShape[t.kernelShape.length-2],y=t.strides[t.strides.length-2],b=t.pads[t.pads.length/2-2],S=t.pads[t.pads.length-2];g=!!(b+S),n.push({type:12,data:_},{type:12,data:y},{type:12,data:b},{type:12,data:S}),s.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[n,s,!0,h,g]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let u=O.computeStrides(t.kernelShape);n.push({type:12,data:u},{type:12,data:t.pads},{type:12,data:t.strides}),s.push({name:"kernelStrides",type:"u32",length:u.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let l=t.pads.reduce((p,f)=>p+f);return[n,s,!!l,!1,!1]}},ta=(e,t,r,i,a,n,s,u,l,p,f,h)=>{let g=a.format==="NHWC",_=t.type.value,y=X("output",t.type.tensor,i);if(a.kernelShape.length<=2){let b="",S="",x="",w=r-(g?2:1);if(f?b=`
2165                for (var i: u32 = 0u; i < uniforms.kw; i++) {
2166                  xIndices[${w}] = indices[${w}] * uniforms.sw - uniforms.pwStart + i;
2167                  if (xIndices[${w}] < 0 || xIndices[${w}]
2168                      >= uniforms.x_shape[${w}]) {
2169                    pad++;
2170                    continue;
2171                  }
2172                  let x_val = x[${t.indicesToOffset("xIndices")}];
2173                  ${n}
2174                }`:b=`
2175                for (var i: u32 = 0u; i < uniforms.kw; i++) {
2176                  xIndices[${w}] = indices[${w}] * uniforms.sw - uniforms.pwStart + i;
2177                  let x_val = x[${t.indicesToOffset("xIndices")}];
2178                  ${n}
2179                }`,a.kernelShape.length===2){let I=r-(g?3:2);h?S=`
2180                for (var j: u32 = 0u; j < uniforms.kh; j++) {
2181                  xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j;
2182                  if (xIndices[${I}] < 0 || xIndices[${I}] >= uniforms.x_shape[${I}]) {
2183                    pad += i32(uniforms.kw);
2184                    continue;
2185                  }
2186              `:S=`
2187                for (var j: u32 = 0u; j < uniforms.kh; j++) {
2188                  xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j;
2189                `,x=`
2190              }
2191            `}return`
2192            ${e.registerUniforms(l).declareVariables(t,y)}
2193
2194            ${e.mainStart()}
2195              ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2196
2197              let indices = ${y.offsetToIndices("global_idx")};
2198              var xIndices = ${y.offsetToIndices("global_idx")};
2199
2200              var value = ${_}(${u});
2201              var pad = 0;
2202              ${S}
2203              ${b}
2204              ${x}
2205              ${s}
2206
2207              output[global_idx] = value;
2208            }`}else{if(g)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let b=a.kernelShape.length,S=a.pads.length,x="";return p?x=`
2209                if (xIndices[j] >= uniforms.x_shape[j]) {
2210                  pad++;
2211                  isPad = true;
2212                  break;
2213                }
2214              }
2215              if (!isPad) {
2216                let x_val = x[${t.indicesToOffset("xIndices")}];
2217                ${n}
2218              }`:x=`
2219              }
2220              let x_val = x[${t.indicesToOffset("xIndices")}];
2221              ${n}
2222            `,`
2223            ${e.registerUniforms(l).declareVariables(t,y)}
2224
2225            ${e.mainStart()}
2226              ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2227              let indices = ${y.offsetToIndices("global_idx")};
2228              var xIndices = ${y.offsetToIndices("global_idx")};
2229
2230              var offsets: array<u32, ${b}>;
2231
2232              var value = ${_}(${u});
2233              var pad = 0;
2234              var isPad = false;
2235
2236              for (var i: u32 = 0u; i < uniforms.kernelSize; i++) {
2237                var offset = i;
2238                for (var j = 0u; j < ${b-1}u; j++) {
2239                  offsets[j] = offset / ${Y("uniforms.kernelStrides","j",b)};
2240                  offset -= offsets[j] * ${Y("uniforms.kernelStrides","j",b)};
2241                }
2242                offsets[${b-1}] = offset;
2243
2244                isPad = false;
2245                for (var j = ${r-b}u; j < ${r}u; j++) {
2246                  xIndices[j] = indices[j] * ${Y("uniforms.strides",`j - ${r-b}u`,b)}
2247                    + offsets[j - ${r-b}u] - ${Y("uniforms.pads","j - 2u",S)};
2248                  ${x}
2249              }
2250              ${s}
2251
2252              output[global_idx] = value;
2253            }`}},ra=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Vl=e=>`${ra(e)};${e.countIncludePad}`,Gl=e=>`${ra(e)};${e.storageOrder};${e.dilations}`,ia=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}),aa=(e,t,r,i)=>{let[a,n]=Ji(t,i,r),s=N("x",t.dataType,t.dims.length),u=s.type.value,l="value += x_val;",p="";a.countIncludePad?p+=`value /= ${u}(uniforms.kernelSize);`:p+=`value /= ${u}(i32(uniforms.kernelSize) - pad);`;let[f,h,g,_,y]=ea(n,a);f.push(...J(t.dims,n));let b=["rank"];return{name:e,shaderCache:{hint:`${i.cacheKey};${g};${_};${y}`,inputDependencies:b},getRunData:()=>({outputs:[{dims:n,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(O.size(n)/64)},programUniforms:f}),getShaderSource:S=>ta(S,s,t.dims.length,n.length,a,l,p,0,h,g,_,y)}},zh=e=>{let t=e.count_include_pad!==0,r=ia(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let i={countIncludePad:t,...r,cacheKey:""};return{...i,cacheKey:Vl(i)}},Ch=(e,t)=>{ir(e.inputs),e.compute(aa("AveragePool",e.inputs[0],!1,t))},na={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},Ah=e=>{let t=e.format;return{format:t,...na,cacheKey:t}},Oh=(e,t)=>{ir(e.inputs),e.compute(aa("GlobalAveragePool",e.inputs[0],!0,t))},sa=(e,t,r,i)=>{let[a,n]=Ji(t,i,r),s=`
2254      value = max(x_val, value);
2255    `,u="",l=N("x",t.dataType,t.dims.length),p=["rank"],[f,h,g,_,y]=ea(n,a);return f.push(...J(t.dims,n)),{name:e,shaderCache:{hint:`${i.cacheKey};${g};${_};${y}`,inputDependencies:p},getRunData:()=>({outputs:[{dims:n,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(O.size(n)/64)},programUniforms:f}),getShaderSource:b=>ta(b,l,t.dims.length,n.length,a,s,u,t.dataType===10?-65504:-1e5,h,g,_,y)}},Rh=(e,t)=>{ir(e.inputs),e.compute(sa("MaxPool",e.inputs[0],!1,t))},Bh=e=>{let t=e.storage_order,r=e.dilations,i=ia(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(i.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let a={storageOrder:t,dilations:r,...i,cacheKey:""};return{...a,cacheKey:Gl(a)}},Nh=e=>{let t=e.format;return{format:t,...na,cacheKey:t}},Mh=(e,t)=>{ir(e.inputs),e.compute(sa("GlobalMaxPool",e.inputs[0],!0,t))}}),Hl,Fl,Dh,Uh,A_=U(()=>{te(),ie(),xe(),ae(),Hl=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((r,i)=>r===e[2].dims[i]).reduce((r,i)=>r&&i,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");
vendor: 18,936 bytes, lines 2255-2452
2255if(!e[1].dims.map((a,n)=>n===t.axis||a===e[0].dims[n]).reduce((a,n)=>a&&n,!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],i=e[1].dims[t.axis];if(t.blockSize<Math.ceil(r/i)||t.blockSize>Math.ceil(r/(i-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},Fl=(e,t)=>{let r=O.normalizeAxis(t.axis,e[0].dims.length),i=e[0].dataType,a=i===3,n=e[0].dims,s=e[1].dataType,u=O.size(n),l=i===3||i===2,p=l?[Math.ceil(O.size(e[0].dims)/4)]:e[0].dims,f=e[1].dims,h=e.length>2?e[2]:void 0,g=h?l?[Math.ceil(O.size(h.dims)/4)]:h.dims:void 0,_=f.length===0||f.length===1&&f[0]===1,y=_===!1&&f.length===1,b=ve(u),S=_&&(!l||b===4),x=S?b:1,w=S&&!l?b:1,I=N("input",l?12:i,p.length,w),k=N("scale",s,f.length),E=h?N("zero_point",l?12:i,g.length):void 0,C=X("output",s,n.length,x),A=[I,k];E&&A.push(E);let v=[p,f];h&&v.push(g);let M=[{type:12,data:u/x},{type:12,data:r},{type:12,data:t.blockSize},...J(...v,n)],D=H=>{let F=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return`
2256      ${H.registerUniforms(F).declareVariables(...A,C)}
2257      ${H.mainStart()}
2258          ${H.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2259          let output_indices = ${C.offsetToIndices("global_idx")};
2260
2261          // Set input x
2262          ${l?`
2263            let input = ${I.getByOffset("global_idx / 4")};
2264            let x_vec = ${a?"unpack4xI8(input)":"unpack4xU8(input)"};
2265            let x_value = ${x===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${I.getByOffset("global_idx")};`};
2266
2267          // Set scale input
2268          ${_?`let scale_value= ${k.getByOffset("0")}`:y?`
2269            let scale_index = ${C.indicesGet("output_indices","uniforms.axis")};
2270            let scale_value= ${k.getByOffset("scale_index")};`:`
2271            var scale_indices: ${k.type.indices} = output_indices;
2272            let index = ${k.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size;
2273            ${k.indicesSet("scale_indices","uniforms.axis","index")};
2274            let scale_value= ${k.getByIndices("scale_indices")};`};
2275
2276          // Set zero-point input
2277          ${E?_?l?`
2278                let zero_point_input = ${E.getByOffset("0")};
2279                let zero_point_vec =  ${a?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
2280                let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${E.getByOffset("0")}`:y?l?`
2281                let zero_point_index = ${C.indicesGet("output_indices","uniforms.axis")};
2282                let zero_point_input = ${E.getByOffset("zero_point_index / 4")};
2283                let zero_point_vec =  ${a?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
2284                let zero_point_value = zero_point_vec[zero_point_index % 4]`:`
2285                let zero_point_index = ${C.indicesGet("output_indices","uniforms.axis")};
2286                let zero_point_value = ${E.getByOffset("zero_point_index")};`:l?`
2287                let zero_point_offset = ${k.indicesToOffset("scale_indices")};
2288                let zero_point_input = ${E.getByOffset("zero_point_offset / 4")};
2289                let zero_point_vec = ${a?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
2290                let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${E.getByIndices("scale_indices")};`:`let zero_point_value = ${l?a?"i32":"u32":I.type.value}(0);`};
2291      // Compute and write output
2292      ${C.setByOffset("global_idx",`${C.type.value}(x_value - zero_point_value) * scale_value`)};
2293      }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:E?["rank","rank","rank"]:["rank","rank"]},getShaderSource:D,getRunData:()=>({outputs:[{dims:n,dataType:s}],dispatchGroup:{x:Math.ceil(u/x/64),y:1,z:1},programUniforms:M})}},Dh=(e,t)=>{Hl(e.inputs,t),e.compute(Fl(e.inputs,t))},Uh=e=>fe({axis:e.axis,blockSize:e.blockSize})}),jl,Kl,Ph,O_=U(()=>{Ve(),te(),ae(),jl=(e,t,r)=>{let i=e===t,a=e<t&&r<0,n=e>t&&r>0;if(i||a||n)throw new Error("Range these inputs' contents are invalid.")},Kl=(e,t,r,i)=>{let a=Math.abs(Math.ceil((t-e)/r)),n=[a],s=a,u=[{type:12,data:s},{type:i,data:e},{type:i,data:r},...J(n)],l=p=>{let f=X("output",i,n.length),h=f.type.value,g=[{name:"outputSize",type:"u32"},{name:"start",type:h},{name:"delta",type:h}];return`
2294        ${p.registerUniforms(g).declareVariables(f)}
2295        ${p.mainStart()}
2296        ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2297        output[global_idx] = uniforms.start + ${h}(global_idx) * uniforms.delta;
2298      }`};return{name:"Range",shaderCache:{hint:`${i}`},getShaderSource:l,getRunData:()=>({outputs:[{dims:n,dataType:i}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:u})}},Ph=e=>{let t=0,r=0,i=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],i=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],i=e.inputs[2].getFloat32Array()[0]),$e.webgpu.validateInputContent&&jl(t,r,i),e.compute(Kl(t,r,i,e.inputs[0].dataType),{inputs:[]})}}),Zl,Ql,qh,Lh,R_=U(()=>{te(),ie(),xe(),ae(),Zl=(e,t,r,i)=>{if(e!=="none"&&i!=="i32"&&i!=="u32"&&i!=="f32")throw new Error(`Input ${i} is not supported with reduction ${e}.`);let a=`{
2299                var oldValue = 0;
2300                loop {
2301                  let newValueF32 =`,n=`;
2302                  let newValue = bitcast<i32>(newValueF32);
2303                  let res = atomicCompareExchangeWeak(&${t}, oldValue, newValue);
2304                  if res.exchanged {
2305                    break;
2306                  }
2307                  oldValue = res.old_value;
2308                }
2309              }`;switch(e){case"none":return`${t}=${r};`;case"add":return i==="i32"||i==="u32"?`atomicAdd(&${t}, bitcast<${i}>(${r}));`:`
2310              ${a}bitcast<${i}>(oldValue) + (${r})${n}`;case"max":return i==="i32"||i==="u32"?`atomicMax(&${t}, bitcast<${i}>(${r}));`:`
2311                ${a}max(bitcast<f32>(oldValue), (${r}))${n}`;case"min":return i==="i32"||i==="u32"?`atomicMin(&${t}, bitcast<${i}>(${r}));`:`${a}min(bitcast<${i}>(oldValue), (${r}))${n}`;case"mul":return`${a}(bitcast<${i}>(oldValue) * (${r}))${n}`;default:throw new Error(`Reduction ${e} is not supported.`)}},Ql=(e,t)=>{let r=e[0].dims,i=e[1].dims,a=r,n=1,s=Math.ceil(O.sizeToDimension(i,i.length-1)/n),u=i[i.length-1],l=O.sizeFromDimension(r,u),p=[{type:12,data:s},{type:12,data:u},{type:12,data:l},...J(e[1].dims,e[2].dims,a)],f=h=>{let g=N("indices",e[1].dataType,e[1].dims.length),_=N("updates",e[2].dataType,e[2].dims.length,n),y=t.reduction!=="none"&&t.reduction!==""?mp("output",e[0].dataType,a.length):X("output",e[0].dataType,a.length,n);return`
2312      ${h.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(g,_,y)}
2313      ${h.mainStart()}
2314        ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2315  var data_offset = 0u;
2316  let indices_start = uniforms.last_index_dimension * global_idx;
2317  let indices_end = indices_start + uniforms.last_index_dimension;
2318  for (var i = indices_start; i < indices_end; i++) {
2319    var index = i32(indices[i].x);
2320    ${e[0].dims.length===1?`
2321    let element_count_dim = uniforms.output_strides;
2322    let dim_value = uniforms.output_shape;`:`
2323    let element_count_dim = uniforms.output_strides[i - indices_start];
2324    let dim_value = uniforms.output_shape[i - indices_start];`}
2325    if (index >= 0) {
2326      if (index >= i32(dim_value)) {
2327        index = i32(dim_value - 1);
2328      }
2329    } else {
2330      if (index < -i32(dim_value)) {
2331        index = 0;
2332      } else {
2333        index += i32(dim_value);
2334      }
2335    }
2336    data_offset += u32((u32(index) * element_count_dim));
2337  }
2338
2339  for (var i = 0u; i < uniforms.num_updates_elements; i++) {
2340    let value = updates[uniforms.num_updates_elements * global_idx + i];
2341    ${Zl(t.reduction,"output[data_offset + i]","value",y.type.value)}
2342  }
2343
2344      }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:p}),getShaderSource:f}},qh=e=>fe({reduction:e.reduction}),Lh=(e,t)=>{e.compute(Ql(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),Xl,Yl,Jl,oa,ed,td,rd,id,ad,nd,sd,od,ua,ud,ld,dd,pd,cd,Wh,Vh,B_=U(()=>{te(),ie(),xe(),ae(),Xl=(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
2345            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")}},Yl=(e,t,r)=>{t.every(a=>a>=0&&a<r||(()=>{throw new Error("Resize requires axes input values to be positive and less than rank")}));let i=new Array(r).fill(1);return t.forEach((a,n)=>i[a]=e[n]),i},Jl=(e,t,r,i,a,n)=>{let[s,u,l]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],p=e[0].dims.length;if(s>0&&e.length>s&&e[s].dims.length>0)e[s].getFloat32Array().forEach(f=>n.push(f));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(u>0&&e.length>u&&e[u].dims.length===1&&e[u].dims[0]>0){if(e[u].getFloat32Array().forEach(f=>i.push(f)),i.length!==0&&i.length!==p&&r>=18&&i.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");Xl(i,t),t.axes.length>0&&Yl(i,t.axes,p).forEach((f,h)=>i[h]=f)}if(l>0&&e.length>l&&e[l].dims.length===1&&e[l].dims[0]>0&&(e[l].getBigInt64Array().forEach(f=>a.push(Number(f))),a.length!==0&&a.length!==p&&r>=18&&a.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(i.length!==0&&i.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(a.length!==0&&a.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof i<"u"&&typeof a<"u"&&i.length>0&&a.length>p)throw new Error("Resize requires only of scales or sizes to be specified")},oa=(e,t,r,i)=>`
2346  // The whole part and the fractional part are calculated separately due to inaccuracy of floating
2347  // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an
2348  // offset-by-one error later in floor().
2349  let big = (${e}) * (${t});
2350  let whole = ${i}(big / (${r}));
2351  let fract = ${i}(big % (${r})) / ${i}(${r});
2352  return whole + fract;
2353`,ed=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32,
2354     lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return`
2355          if (xScale < 1.0 || floor(xScale) != xScale) {
2356            return ${t}(xResized) / ${t}(xScale);
2357          } else {
2358            ${oa("xResized","lengthOriginal","lengthResized",t)}
2359          }
2360        `;case"pytorch_half_pixel":return`if (lengthResized > 1) {
2361                    return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5;
2362                  } else {
2363                    return 0.0;
2364                  }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) {
2365                    return 0.0;
2366                  } else {
2367                    ${oa("xResized","lengthOriginal - 1","lengthResized - 1",t)}
2368                  }`;case"tf_crop_and_resize":return`if (lengthResized > 1) {
2369                    return ${t}(roiStart) * ${t}(lengthOriginal - 1) +
2370                        (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) /
2371                        ${t}(lengthResized - 1);
2372                  } else {
2373                    return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1);
2374                  }`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized);
2375                  const adjustment = ${t}(lengthResized) / outputWidth;
2376                  const center = ${t}(lengthOriginal) / 2;
2377                  const offset = center * (1 - adjustment);
2378                  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`)}})()+"}",td=(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`)}})()+"}",rd=(e,t,r)=>{let i=new Array(r).fill(0).concat(new Array(r).fill(1)),a=e.length===0?i:e.slice();return t.length>0?(t.forEach((n,s)=>{i[n]=a[s],i[s+r]=a[t.length+s]}),i):a},id=(e,t,r,i)=>{let a=[];if(r.length>0)if(i.length>0){if(e.forEach(n=>a.push(n)),Math.max(...i)>e.length)throw new Error("axes is out of bound");i.forEach((n,s)=>a[n]=r[s])}else r.forEach(n=>a.push(n));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");a=e.map((n,s)=>Math.round(n*t[s]))}return a},ad=(e,t,r)=>{let i=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(n=>t[n]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(n=>t[n]),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 a=e.slice();return r.axes.length>0?(r.axes.forEach(n=>t[n]=i),r.axes.forEach(n=>a[n]=Math.round(e[n]*t[n]))):(t.fill(i,0,t.length),a.forEach((n,s)=>a[s]=Math.round(n*t[s]))),a},nd=(e,t,r,i,a)=>`
2379    fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> {
2380      var original_indices: array<${e.type.value}, ${r.length}>;
2381      for (var i:u32 = 0; i < ${r.length}; i++) {
2382        var output_index = ${e.indicesGet("output_indices","i")};
2383        var scale = ${Y("uniforms.scales","i",i)};
2384        var roi_low = ${Y("uniforms.roi","i",a)};
2385        var roi_hi = ${Y("uniforms.roi",`i + ${t.length}`,a)};
2386        if (scale == 1.0) {
2387          original_indices[i] = ${e.type.value}(output_index);
2388        } else {
2389          var input_shape_i = ${Y("uniforms.input_shape","i",t.length)};
2390          var output_shape_i = ${Y("uniforms.output_shape","i",r.length)};
2391          original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
2392                                                                           input_shape_i, roi_low, roi_hi);
2393        }
2394      }
2395      return original_indices;
2396    }`,sd=(e,t,r,i,a,n,s)=>`
2397    fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
2398      var input_indices: ${e.type.indices};
2399      for (var i:u32 = 0; i < ${i.length}; i++) {
2400        var output_index = ${t.indicesGet("output_indices","i")};
2401        var input_index: u32;
2402        var scale = ${Y("uniforms.scales","i",a)};
2403        if (scale == 1.0) {
2404          input_index = output_index;
2405        } else {
2406          var roi_low = ${Y("uniforms.roi","i",n)};
2407          var roi_hi = ${Y("uniforms.roi",`i + ${r.length}`,n)};
2408          var input_shape_i = ${Y("uniforms.input_shape","i",r.length)};
2409          var output_shape_i = ${Y("uniforms.output_shape","i",i.length)};
2410          var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
2411                                                                        input_shape_i, roi_low, roi_hi);
2412          if (!${s} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) {
2413            if (original_idx < 0) {
2414              input_index = 0;
2415            } else if (original_idx > ${t.type.value}(input_shape_i - 1)) {
2416              input_index = input_shape_i - 1;
2417            } else {
2418              input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1));
2419            }
2420          } else {
2421            input_index = u32(original_idx);
2422          }
2423        }
2424        ${e.indicesSet("input_indices","i","input_index")}
2425      }
2426      return input_indices;
2427    }`,od=(e,t)=>`
2428    fn checkInputIndices(input_indices: ${e.type.indices}) -> bool {
2429      for (var i:u32 = 0; i < ${t.length}; i++) {
2430        var input_index = ${e.indicesGet("input_indices","i")};
2431        if (input_index < 0 || input_index >= ${Y("uniforms.input_shape","i",t.length)}) {
2432          return false;
2433        }
2434      }
2435      return true;
2436    }`,ua=(e,t,r,i)=>e.rank>i?`
2437    ${e.indicesSet("input_indices",t,"channel")};
2438    ${e.indicesSet("input_indices",r,"batch")};
2439`:"",ud=(e,t,r,i,a)=>{let[n,s,u,l]=r.length===2?[-1,0,1,-1]:[0,2,3,1],p=e.type.value;return`
2440    fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${p} {
2441      var input_indices: ${e.type.indices};
2442      ${e.indicesSet("input_indices",s,`max(0, min(row, ${r[s]} - 1))`)};
2443      ${e.indicesSet("input_indices",u,`max(0, min(col, ${r[u]} - 1))`)};
2444      ${ua(e,l,n,2)}
2445      return ${e.getByIndices("input_indices")};
2446    }
2447
2448    fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${p} {
2449      var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);
2450      var row:${p} = originalIndices[${s}];
2451      var col:${p} = originalIndices[${u}];
2452      ${i?`if (row < 0 || row > (${r[s]}
vendor: 8,925 bytes, lines 2452-2614
2452 - 1) || col < 0 || col > (${r[u]} - 1)) {
2453        return ${a};
2454      }`:""};
2455      row = max(0, min(row, ${r[s]} - 1));
2456      col = max(0, min(col, ${r[u]} - 1));
2457      var row1: u32 = u32(row);
2458      var col1: u32 = u32(col);
2459      var row2: u32 = u32(row + 1);
2460      var col2: u32 = u32(col + 1);
2461      var channel: u32 = ${r.length>2?`u32(originalIndices[${l}])`:"0"};
2462      var batch: u32 =  ${r.length>2?`u32(originalIndices[${n}])`:"0"};
2463      var x11: ${p} = getInputValue(batch, channel, row1, col1);
2464      var x12: ${p} = getInputValue(batch, channel, row1, col2);
2465      var x21: ${p} = getInputValue(batch, channel, row2, col1);
2466      var x22: ${p} = getInputValue(batch, channel, row2, col2);
2467      var dx1: ${p} = abs(row - ${p}(row1));
2468      var dx2: ${p} = abs(${p}(row2) - row);
2469      var dy1: ${p} = abs(col - ${p}(col1));
2470      var dy2: ${p} = abs(${p}(col2) - col);
2471      if (row1 == row2) {
2472        dx1 = 0.5;
2473        dx2 = 0.5;
2474      }
2475      if (col1 == col2) {
2476        dy1 = 0.5;
2477        dy2 = 0.5;
2478      }
2479      return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1);
2480    }`},ld=(e,t,r,i,a,n,s,u,l,p)=>{let f=r.length===2,[h,g]=f?[0,1]:[2,3],_=e.type.value,y=b=>{let S=b===h?"row":"col";return`
2481      fn ${S}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${_} {
2482        var output_index = ${t.indicesGet("output_indices",b)};
2483        var originalIdx: ${_} = getOriginalCoordinateFromResizedCoordinate(output_index, ${a[b]},
2484        ${i[b]}, ${r[b]}, ${n[b]}, ${n[b]} + ${r.length});
2485        var fractOriginalIdx: ${_} = originalIdx - floor(originalIdx);
2486        var coefs = getCubicInterpolationCoefs(fractOriginalIdx);
2487
2488        if (${u} && (originalIdx < 0 || originalIdx > (${r[b]} - 1))) {
2489          return ${l};
2490        }
2491        var data: array<${_}, 4> = array<${_}, 4>(0.0, 0.0, 0.0, 0.0);
2492        for (var i: i32 = -1; i < 3; i++) {
2493          var ${S}: ${_} = originalIdx + ${_}(i);
2494          if (${S} < 0 || ${S} >= ${r[b]}) {
2495            ${p?`coefs[i + 1] = 0.0;
2496                        continue;`:u?`return ${l};`:`${S} = max(0, min(${S}, ${r[b]} - 1));`};
2497          }
2498        var input_indices_copy: ${e.type.indices} = input_indices;
2499          ${e.indicesSet("input_indices_copy",b,`u32(${S})`)};
2500          data[i + 1] = ${b===h?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"};
2501        }
2502        return cubicInterpolation1D(data, coefs);
2503      }`};return`
2504    ${y(h)};
2505    ${y(g)};
2506  fn getCubicInterpolationCoefs(s: ${_}) -> array<${_}, 4> {
2507    var absS = abs(s);
2508    var coeffs: array<${_}, 4> = array<${_}, 4>(0.0, 0.0, 0.0, 0.0);
2509    var oneMinusAbsS: ${_} = 1.0 - absS;
2510    var twoMinusAbsS: ${_} = 2.0 - absS;
2511    var onePlusAbsS: ${_} = 1.0 + absS;
2512    coeffs[0] = ((${s} * onePlusAbsS - 5 * ${s}) * onePlusAbsS + 8 * ${s}) * onePlusAbsS - 4 * ${s};
2513    coeffs[1] = ((${s} + 2) * absS - (${s} + 3)) * absS * absS + 1;
2514    coeffs[2] = ((${s} + 2) * oneMinusAbsS - (${s} + 3)) * oneMinusAbsS * oneMinusAbsS + 1;
2515    coeffs[3] = ((${s} * twoMinusAbsS - 5 * ${s}) * twoMinusAbsS + 8 * ${s}) * twoMinusAbsS - 4 * ${s};
2516    return coeffs;
2517  }
2518
2519  fn cubicInterpolation1D(x: array<${_}, 4>, coefs: array<${_}, 4>) -> ${_} {
2520    var coefsSum: ${_} = coefs[0] + coefs[1] + coefs[2] + coefs[3];
2521    return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum;
2522  }
2523
2524  fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${_} {
2525    var input_indices: ${e.type.indices} = output_indices;
2526    return colCubicInterpolation(input_indices, output_indices);
2527  }
2528    `},dd=(e,t,r,i,a)=>{let[n,s,u,l,p]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],f=e.type.value;return`
2529    fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${f} {
2530      var input_indices: ${e.type.indices};
2531      ${e.indicesSet("input_indices",s,`max(0, min(depth, ${r[s]} - 1))`)};
2532      ${e.indicesSet("input_indices",u,`max(0, min(height, ${r[u]} - 1))`)};
2533      ${e.indicesSet("input_indices",l,`max(0, min(width, ${r[l]} - 1))`)};
2534      ${ua(e,p,n,3)}
2535      return ${e.getByIndices("input_indices")};
2536    }
2537
2538    fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${f} {
2539      var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);
2540      var depth:${f} = originalIndices[${s}];
2541      var height:${f} = originalIndices[${u}];
2542      var width:${f} = originalIndices[${l}];
2543      ${i?`if (depth < 0 || depth > (${r[s]} - 1) || height < 0 || height > (${r[u]} - 1) || width < 0 || (width > ${r[l]} - 1)) {
2544      return ${a};
2545        }`:""};
2546
2547    depth = max(0, min(depth, ${r[s]} - 1));
2548      height = max(0, min(height, ${r[u]} - 1));
2549      width = max(0, min(width, ${r[l]} - 1));
2550      var depth1: u32 = u32(depth);
2551      var height1: u32 = u32(height);
2552      var width1: u32 = u32(width);
2553      var depth2: u32 = u32(depth + 1);
2554      var height2: u32 = u32(height + 1);
2555      var width2: u32 = u32(width + 1);
2556      var channel: u32 = ${r.length>3?`u32(originalIndices[${p}])`:"0"};
2557      var batch: u32 =  ${r.length>3?`u32(originalIndices[${n}])`:"0"};
2558
2559      var x111: ${f} = getInputValue(batch, channel, depth1, height1, width1);
2560      var x112: ${f} = getInputValue(batch, channel, depth1, height1, width2);
2561      var x121: ${f} = getInputValue(batch, channel, depth1, height2, width1);
2562      var x122: ${f} = getInputValue(batch, channel, depth1, height2, width2);
2563      var x211: ${f} = getInputValue(batch, channel, depth2, height1, width1);
2564      var x212: ${f} = getInputValue(batch, channel, depth2, height1, width2);
2565      var x221: ${f} = getInputValue(batch, channel, depth2, height2, width1);
2566      var x222: ${f} = getInputValue(batch, channel, depth2, height2, width2);
2567      var dx1: ${f} = abs(depth - ${f}(depth1));
2568      var dx2: ${f} = abs(${f}(depth2) - depth);
2569      var dy1: ${f} = abs(height - ${f}(height1));
2570      var dy2: ${f} = abs(${f}(height2) - height);
2571      var dz1: ${f} = abs(width - ${f}(width1));
2572      var dz2: ${f} = abs(${f}(width2) - width);
2573      if (depth1 == depth2) {
2574        dx1 = 0.5;
2575        dx2 = 0.5;
2576      }
2577      if (height1 == height2) {
2578        dy1 = 0.5;
2579        dy2 = 0.5;
2580      }
2581      if (width1 == width2) {
2582        dz1 = 0.5;
2583        dz2 = 0.5;
2584      }
2585      return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 +
2586              x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1);
2587    }`},pd=(e,t,r,i,a,n)=>{let s=e.dims,u=rd(n,t.axes,s.length),l=id(s,i,a,t.axes),p=i.slice();i.length===0&&(p=s.map((w,I)=>w===0?1:l[I]/w),t.keepAspectRatioPolicy!=="stretch"&&(l=ad(s,p,t)));let f=X("output",e.dataType,l.length),h=N("input",e.dataType,s.length),g=O.size(l),_=s.length===l.length&&s.every((w,I)=>w===l[I]),y=t.coordinateTransformMode==="tf_crop_and_resize",b=t.extrapolationValue,S=h.type.value,x=w=>`
2588      ${_?"":`
2589      ${ed(t.coordinateTransformMode,S)};
2590      ${(()=>{switch(t.mode){case"nearest":return`
2591              ${od(h,s)};
2592              ${td(t.nearestMode,r,S)};
2593              ${sd(h,f,s,l,p.length,u.length,y)};
2594              `;case"linear":return`
2595              ${nd(f,s,l,p.length,u.length)};
2596              ${(()=>{if(s.length===2||s.length===4)return`${ud(h,f,s,y,b)}`;if(s.length===3||s.length===5)return`${dd(h,f,s,y,b)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()};
2597            `;case"cubic":return`
2598            ${(()=>{if(s.length===2||s.length===4)return`${ld(h,f,s,l,p,u,t.cubicCoeffA,y,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()};
2599            `;default:throw Error("Invalid resize mode")}})()};
2600      `}
2601      ${w.registerUniform("output_size","u32").registerUniform("scales","f32",p.length).registerUniform("roi","f32",u.length).declareVariables(h,f)}
2602      ${w.mainStart()}
2603        ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2604        ${_?"output[global_idx] = input[global_idx];":`
2605        let output_indices = ${f.offsetToIndices("global_idx")};
2606        var input_indices: ${h.type.indices};
2607        ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices);
2608                if (checkInputIndices(input_indices)) {
2609                  output[global_idx] = ${h.getByIndices("input_indices")};
2610                } else {
2611                  output[global_idx] = ${t.extrapolationValue};
2612                }`;case"linear":return`output[global_idx] = ${s.length===2||s.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()};
2613`}
2614      }`;
vendor: 15,670 bytes, lines 2614-2780
2614return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${p.length>0?t.mode==="cubic"?p:p.length:""}|${a.length>0?a:""}|${u.length>0?u:""}|${_}|${t.mode==="nearest"?s.length:s}`,inputDependencies:["rank"]},getShaderSource:x,getRunData:()=>({outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},{type:1,data:p},{type:1,data:u},...J(s,l)]})}},cd=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},Wh=(e,t)=>{let r=[],i=[],a=[],n=cd(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Jl(e.inputs,t,n,r,i,a),e.compute(pd(e.inputs[0],t,n,r,i,a),{inputs:[0]})},Vh=e=>{let t=e.antialias,r=e.axes,i=e.coordinateTransformMode,a=e.cubicCoeffA,n=e.excludeOutside!==0,s=e.extrapolationValue,u=e.keepAspectRatioPolicy,l=e.mode,p=e.nearestMode===""?"simple":e.nearestMode;return fe({antialias:t,axes:r,coordinateTransformMode:i,cubicCoeffA:a,excludeOutside:n,extrapolationValue:s,keepAspectRatioPolicy:u,mode:l,nearestMode:p})}}),hd,fd,Gh,N_=U(()=>{te(),ie(),ae(),hd=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],i=e[2];if(t.dataType!==r.dataType||t.dataType!==i.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let a=t.dims[t.dims.length-1],n=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==a)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==n)throw new Error("Skip must have the same sequence length as input");if(i.dims.length!==1)throw new Error("Gamma must be 1D");if(i.dims[i.dims.length-1]!==a)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let s=e[3];if(s.dims.length!==1)throw new Error("Beta must be 1D");if(s.dims[s.dims.length-1]!==a)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let s=e[4];if(s.dims.length!==1)throw new Error("Bias must be 1D");if(s.dims[s.dims.length-1]!==a)throw new Error("Bias must have the same hidden size as input")}},fd=(e,t,r,i)=>{let a=t.simplified,n=e[0].dims,s=O.size(n),u=n,l=s,p=n.slice(-1)[0],f=i?n.slice(0,-1).concat(1):[],h=!a&&e.length>3,g=e.length>4,_=i&&r>1,y=i&&r>2,b=r>3,S=64,x=ve(p),w=[{type:12,data:l},{type:12,data:x},{type:12,data:p},{type:1,data:t.epsilon}],I=E=>{let C=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],A=[N("x",e[0].dataType,e[0].dims,x),N("skip",e[1].dataType,e[1].dims,x),N("gamma",e[2].dataType,e[2].dims,x)];h&&A.push(N("beta",e[3].dataType,e[3].dims,x)),g&&A.push(N("bias",e[4].dataType,e[4].dims,x)),A.push(X("output",e[0].dataType,u,x)),_&&A.push(X("mean_output",1,f)),y&&A.push(X("inv_std_output",1,f)),b&&A.push(X("input_skip_bias_sum",e[0].dataType,u,x));let v=Te(e[0].dataType),M=Te(1,x);return`
2615
2616      ${E.registerUniforms(C).declareVariables(...A)}
2617      var<workgroup> sum_shared : array<${M}, ${S}>;
2618      var<workgroup> sum_squared_shared : array<${M}, ${S}>;
2619
2620      ${E.mainStart([S,1,1])}
2621        let ix = local_id.x;
2622        let iy = global_id.x / ${S};
2623
2624        let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components;
2625        var stride = hidden_size_vectorized / ${S};
2626        let offset = ix * stride + iy * hidden_size_vectorized;
2627        let offset1d = stride * ix;
2628        if (ix == ${S-1}) {
2629          stride = hidden_size_vectorized - stride * ix;
2630        }
2631        for (var i: u32 = 0; i < stride; i++) {
2632          let skip_value = skip[offset + i];
2633          let bias_value = ${g?"bias[offset1d + i]":v+"(0.0)"};
2634          let input_value = x[offset + i];
2635          let value = input_value + skip_value + bias_value;
2636          ${b?"input_skip_bias_sum[offset + i] = value;":""}
2637          output[offset + i] = value;
2638          let f32_value = ${Wt(v,x,"value")};
2639          sum_shared[ix] += f32_value;
2640          sum_squared_shared[ix] += f32_value * f32_value;
2641        }
2642        workgroupBarrier();
2643
2644        var reduce_size : u32 = ${S};
2645        for (var curr_size = reduce_size >> 1;  curr_size > 0; curr_size = reduce_size >> 1) {
2646          reduce_size = curr_size + (reduce_size & 1);
2647          if (ix < curr_size) {
2648            sum_shared[ix] += sum_shared[ix + reduce_size];
2649            sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size];
2650          }
2651          workgroupBarrier();
2652        }
2653
2654        let sum = sum_shared[0];
2655        let square_sum = sum_squared_shared[0];
2656        let mean = ${_t("sum",x)} / f32(uniforms.hidden_size);
2657        let inv_std_dev = inverseSqrt(${_t("square_sum",x)} / f32(uniforms.hidden_size) ${a?"":"- mean * mean"} + uniforms.epsilon);
2658        ${_?"mean_output[global_idx] = mean;":""}
2659        ${y?"inv_std_output[global_idx] = inv_std_dev;":""}
2660
2661        for (var i: u32 = 0; i < stride; i++) {
2662          output[offset + i] = (output[offset + i] ${a?"":`- ${v}(mean)`}) *
2663            ${v}(inv_std_dev) * gamma[offset1d + i]
2664            ${h?"+ beta[offset1d + i]":""};
2665        }
2666      }`},k=[{dims:u,dataType:e[0].dataType}];return r>1&&k.push({dims:f,dataType:1}),r>2&&k.push({dims:f,dataType:1}),r>3&&k.push({dims:n,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${x};${_};${y};${b}`,inputDependencies:e.map((E,C)=>"type")},getShaderSource:I,getRunData:()=>({outputs:k,dispatchGroup:{x:Math.ceil(l/p)},programUniforms:w})}},Gh=(e,t)=>{hd(e.inputs);let r=[0];e.outputCount>1&&r.push(-3),e.outputCount>2&&r.push(-3),e.outputCount>3&&r.push(3),e.compute(fd(e.inputs,t,e.outputCount,!1),{outputs:r})}}),md,ar,gd,la,_d,yd,Hh,Fh,M_=U(()=>{te(),ie(),xe(),ae(),md=(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,i)=>{if(e[i+1].dataType!==6&&e[i+1].dataType!==7)throw new Error(`Input ${i} must be an array of int32 or int64`)})},ar=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(i=>r.push(Number(i)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(i=>r.push(Number(i)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},gd=(e,t)=>{if(e.length>1){let r=ar(e,1),i=ar(e,2),a=ar(e,3);return a.length===0&&(a=[...Array(e[0].dims.length).keys()]),fe({starts:r,ends:i,axes:a})}else return t},la=(e,t,r,i,a)=>{let n=e;return e<0&&(n+=r[i[t]]),a[t]<0?Math.max(0,Math.min(n,r[i[t]]-1)):Math.max(0,Math.min(n,r[i[t]]))},_d=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
2667          var input_indices: ${e.type.indices};
2668          var carry = 0u;
2669          for (var i = ${r.length-1}; i >= 0; i--) {
2670            let input_shape_i = ${Y("uniforms.input_shape","i",r.length)};
2671            let steps_i = ${Y("uniforms.steps","i",r.length)};
2672            let signs_i = ${Y("uniforms.signs","i",r.length)};
2673            let starts_i = ${Y("uniforms.starts","i",r.length)};
2674            var output_index = ${t.indicesGet("output_indices","i")};
2675            var input_index = output_index * steps_i + starts_i + carry;
2676            carry = input_index / input_shape_i;
2677            input_index = input_index % input_shape_i;
2678            if (signs_i < 0) {
2679              input_index = input_shape_i - input_index - 1u + starts_i;
2680            }
2681            ${e.indicesSet("input_indices","i","input_index")};
2682          }
2683          return input_indices;
2684      }`,yd=(e,t)=>{let r=e[0].dims,i=O.size(r),a=t.axes.length>0?O.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],n=ar(e,4);n.forEach(x=>x!==0||(()=>{throw new Error("step cannot be 0")})),n.length===0&&(n=Array(a.length).fill(1));let s=t.starts.map((x,w)=>la(x,w,r,a,n)),u=t.ends.map((x,w)=>la(x,w,r,a,n));if(a.length!==s.length||a.length!==u.length)throw new Error("start, ends and axes should have the same number of elements");if(a.length!==r.length)for(let x=0;x<r.length;++x)a.includes(x)||(s.splice(x,0,0),u.splice(x,0,r[x]),n.splice(x,0,1));let l=n.map(x=>Math.sign(x));n.forEach((x,w,I)=>{if(x<0){let k=(u[w]-s[w])/x,E=s[w],C=E+k*n[w];s[w]=C,u[w]=E,I[w]=-x}});let p=r.slice(0);a.forEach((x,w)=>{p[x]=Math.ceil((u[x]-s[x])/n[x])});let f={dims:p,dataType:e[0].dataType},h=X("output",e[0].dataType,p.length),g=N("input",e[0].dataType,e[0].dims.length),_=O.size(p),y=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:s.length},{name:"signs",type:"i32",length:l.length},{name:"steps",type:"u32",length:n.length}],b=[{type:12,data:_},{type:12,data:s},{type:6,data:l},{type:12,data:n},...J(e[0].dims,p)],S=x=>`
2685      ${x.registerUniforms(y).declareVariables(g,h)}
2686        ${_d(g,h,r)}
2687        ${x.mainStart()}
2688          ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2689          let output_indices = ${h.offsetToIndices("global_idx")};
2690          let input_indices = calculateInputIndices(output_indices);
2691          ${h.setByOffset("global_idx",g.getByIndices("input_indices"))}
2692      }`;return{name:"Slice",shaderCache:{hint:`${l.length}_${s.length}_${n.length}`,inputDependencies:["rank"]},getShaderSource:S,getRunData:()=>({outputs:[f],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:b})}},Hh=(e,t)=>{md(e.inputs,t);let r=gd(e.inputs,t);e.compute(yd(e.inputs,r),{inputs:[0]})},Fh=e=>{let t=e.starts,r=e.ends,i=e.axes;return fe({starts:t,ends:r,axes:i})}}),bd,wd,jh,Kh,D_=U(()=>{te(),ie(),xe(),yt(),ae(),bd=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},wd=(e,t)=>{let r=e.inputs[0],i=r.dims,a=O.size(i),n=i.length,s=O.normalizeAxis(t.axis,n),u=s<i.length-1,l,p=[];u?(p=Array.from({length:n},(A,v)=>v),p[s]=n-1,p[n-1]=s,l=e.compute(qe(r,p),{inputs:[r],outputs:[-1]})[0]):l=r;let f=l.dims,h=f[n-1],g=a/h,_=ve(h),y=h/_,b=64;g===1&&(b=256);let S=(A,v)=>v===4?`max(max(${A}.x, ${A}.y), max(${A}.z, ${A}.w))`:v===2?`max(${A}.x, ${A}.y)`:v===3?`max(max(${A}.x, ${A}.y), ${A}.z)`:A,x=N("x",l.dataType,l.dims,_),w=X("result",l.dataType,l.dims,_),I=x.type.value,k=Te(l.dataType)==="f32"?`var threadMax = ${I}(-3.4028234663852886e+38f);`:`var threadMax = ${I}(-65504.0h);`,E=A=>`
2693      var<workgroup> rowMaxShared : ${I};
2694      var<workgroup> rowSumShared : ${I};
2695      var<workgroup> threadShared : array<${I}, ${b}>;
2696
2697      fn getValue(row: i32, col: i32, row_stride: i32) -> ${I} {
2698        let index = row * row_stride + col;
2699        return x[index];
2700      }
2701
2702      fn setValue(row: i32, col: i32, row_stride: i32, value: ${I}) {
2703        let index = row * row_stride + col;
2704        result[index] = value;
2705      }
2706      ${A.registerUniform("packedCols","i32").declareVariables(x,w)}
2707      ${A.mainStart(b)}
2708        let gindex = i32(global_idx);
2709        let lindex = i32(local_idx);
2710        const wg = ${b};
2711        let row = gindex / wg;
2712        let cols = uniforms.packedCols;
2713        let row_stride : i32 = uniforms.packedCols;
2714
2715        // find the rows max
2716        ${k}
2717        for (var col = lindex; col < cols; col += wg) {
2718          let value = getValue(row, col, row_stride);
2719          threadMax = max(threadMax, value);
2720        }
2721        if (lindex < cols) {
2722          threadShared[lindex] = threadMax;
2723        }
2724        workgroupBarrier();
2725
2726        var reduceSize = min(cols, wg);
2727        for (var currSize = reduceSize >> 1;  currSize > 0; currSize = reduceSize >> 1) {
2728          reduceSize = currSize + (reduceSize & 1);
2729          if (lindex < currSize) {
2730            threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]);
2731          }
2732          workgroupBarrier();
2733        }
2734        if (lindex == 0) {
2735          rowMaxShared = ${I}(${S("threadShared[0]",_)});
2736        }
2737        workgroupBarrier();
2738
2739        // find the rows sum
2740        var threadSum = ${I}(0.0);
2741        for (var col = lindex; col < cols; col += wg) {
2742          let subExp = exp(getValue(row, col, row_stride) - rowMaxShared);
2743          threadSum += subExp;
2744        }
2745        threadShared[lindex] = threadSum;
2746        workgroupBarrier();
2747
2748        for (var currSize = wg >> 1;  currSize > 0; currSize = currSize >> 1) {
2749          if (lindex < currSize) {
2750            threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize];
2751          }
2752          workgroupBarrier();
2753        }
2754        if (lindex == 0) {
2755          rowSumShared = ${I}(${_t("threadShared[0]",_)});
2756        }
2757        workgroupBarrier();
2758
2759        // calculate final value for each element in the row
2760        for (var col = lindex; col < cols; col += wg) {
2761          var value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared;
2762          // max operation protects against NaN since all values should be >=0
2763          value = max(value, ${I}(0.0));
2764          setValue(row, col, row_stride, value);
2765        }
2766      }`,C=e.compute({name:"Softmax",shaderCache:{hint:`${_};${b}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:f,dataType:l.dataType}],dispatchGroup:{x:g},programUniforms:[{type:6,data:y}]}),getShaderSource:E},{inputs:[l],outputs:[u?-1:0]})[0];u&&e.compute(qe(C,p),{inputs:[C]})},jh=(e,t)=>{bd(e.inputs),wd(e,t)},Kh=e=>fe({axis:e.axis})}),da,$d,vd,xd,Zh,U_=U(()=>{te(),ie(),ae(),da=e=>Array.from(e.getBigInt64Array(),Number),$d=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(da(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")},vd=(e,t)=>{let r=[];for(let i=0;i<e.length;++i)r.push(e[i]*t[i]);return r},xd=(e,t)=>{let r=e[0].dims,i=t??da(e[1]),a=vd(r,i),n=O.size(a),s=e[0].dataType,u=N("input",s,r.length),l=X("output",s,a.length),p=f=>`
2767      const inputShape = ${u.indices(...r)};
2768      ${f.registerUniform("output_size","u32").declareVariables(u,l)}
2769      ${f.mainStart()}
2770      ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2771      let output_indices = ${l.offsetToIndices("global_idx")};
2772      var input_indices: ${u.type.indices};
2773      for (var i = 0; i < ${r.length}; i++) {
2774        let input_dim_i = ${u.indicesGet("uniforms.input_shape","i")};
2775        let input_dim_value = ${l.indicesGet("output_indices","i")}  % input_dim_i;
2776
2777        ${u.indicesSet("input_indices","i","input_dim_value")}
2778      }
2779      ${l.setByOffset("global_idx",u.getByIndices("input_indices"))}
2780    }`;return{name:"Tile",shaderCache:{hint:`${i}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:[{type:12,data:n},...J(e[0].dims,a)]}),getShaderSource:p}},Zh=e=>{$d(e.inputs),e.compute(xd(e.inputs),{inputs:[0]})}}),Sd,kd,Qh,P_=U(()=>{te(),ie(),ae(),Sd=(e,t,r,i,a)=>{let n=X("output_data",a,r.length,4),s=N("a_data",t[1].dataType,t[1].dims.length,4),u=N("b_data",t[2].dataType,t[2].dims.length,4),l=N("c_data",t[0].dataType,t[0].dims.length,4),p,f=(h,g,_)=>`select(${g}, ${h}, ${_})`;if(!i)p=n.setByOffset("global_idx",f(s.getByOffset("global_idx"),u.getByOffset("global_idx"),l.getByOffset("global_idx")));else{let h=(g,_,y="")=>{let b=`a_data[index_a${_}][component_a${_}
2780]`,S=`b_data[index_b${_}][component_b${_}]`,x=`bool(c_data[index_c${_}] & (0xffu << (component_c${_} * 8)))`;return`
2781            let output_indices${_} = ${n.offsetToIndices(`global_idx * 4u + ${_}u`)};
2782            let offset_a${_} = ${s.broadcastedIndicesToOffset(`output_indices${_}`,n)};
2783            let offset_b${_} = ${u.broadcastedIndicesToOffset(`output_indices${_}`,n)};
2784            let offset_c${_} = ${l.broadcastedIndicesToOffset(`output_indices${_}`,n)};
2785            let index_a${_} = offset_a${_} / 4u;
2786            let index_b${_} = offset_b${_} / 4u;
2787            let index_c${_} = offset_c${_} / 4u;
2788            let component_a${_} = offset_a${_} % 4u;
2789            let component_b${_} = offset_b${_} % 4u;
2790            let component_c${_} = offset_c${_} % 4u;
2791            ${g}[${_}] = ${y}(${f(b,S,x)});
2792          `};a===9?p=`
2793            var data = vec4<u32>(0);
2794            ${h("data",0,"u32")}
2795            ${h("data",1,"u32")}
2796            ${h("data",2,"u32")}
2797            ${h("data",3,"u32")}
2798            output_data[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:p=`
2799            ${h("output_data[global_idx]",0)}
2800            ${h("output_data[global_idx]",1)}
2801            ${h("output_data[global_idx]",2)}
2802            ${h("output_data[global_idx]",3)}
2803          `}return`
2804        ${e.registerUniform("vec_size","u32").declareVariables(l,s,u,n)}
2805        ${e.mainStart()}
2806        ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
2807        ${p}
2808      }`},kd=e=>{let t=e[1].dims,r=e[2].dims,i=e[0].dims,a=e[1].dataType,n=!(O.areEqual(t,r)&&O.areEqual(r,i)),s=t,u=O.size(t);if(n){let p=Vt.calcShape(Vt.calcShape(t,r,!1),i,!1);if(!p)throw new Error("Can't perform where op on the given tensors");s=p,u=O.size(s)}let l=Math.ceil(u/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:p=>Sd(p,e,s,n,a),getRunData:()=>({outputs:[{dims:s,dataType:a}],dispatchGroup:{x:Math.ceil(u/64/4)},programUniforms:[{type:12,data:l},...J(i,t,r,s)]})}},Qh=e=>{e.compute(kd(e.inputs))}}),Xh,q_=U(()=>{e_(),Ha(),t_(),r_(),i_(),a_(),n_(),d_(),c_(),h_(),f_(),m_(),g_(),__(),y_(),b_(),w_(),$_(),v_(),x_(),S_(),k_(),T_(),I_(),E_(),gh(),z_(),C_(),A_(),O_(),R_(),Ga(),B_(),$h(),N_(),M_(),D_(),bh(),U_(),yt(),Fa(),P_(),Xh=new Map([["Abs",[Gp]],["Acos",[Hp]],["Acosh",[Fp]],["Add",[Tc]],["ArgMax",[qp,$a]],["ArgMin",[Pp,$a]],["Asin",[jp]],["Asinh",[Kp]],["Atan",[Zp]],["Atanh",[Qp]],["Attention",[Lp]],["AveragePool",[Ch,zh]],["BatchNormalization",[Wp]],["BiasAdd",[Vp]],["BiasSplitGelu",[kc]],["Cast",[Yp,Xp]],["Ceil",[ec]],["Clip",[Jp]],["Concat",[Mc,Dc]],["Conv",[Ia,Ta]],["ConvTranspose",[jc,Fc]],["Cos",[tc]],["Cosh",[rc]],["CumSum",[Kc,Zc]],["DepthToSpace",[Qc,Xc]],["DequantizeLinear",[Dh,Uh]],["Div",[Ic]],["Einsum",[Yc,Jc]],["Elu",[ic,lr]],["Equal",[Ec]],["Erf",[ac]],["Exp",[nc]],["Expand",[eh]],["FastGelu",[th]],["Floor",[sc]],["FusedConv",[Ia,Ta]],["Gather",[ih,rh]],["GatherElements",[lh,uh]],["GatherBlockQuantized",[sh,oh]],["GatherND",[ah,nh]],["Gelu",[oc]],["Gemm",[ph,dh]],["GlobalAveragePool",[Oh,Ah]],["GlobalMaxPool",[Mh,Nh]],["Greater",[Oc]],["GreaterOrEqual",[Bc]],["GridSample",[ch,hh]],["GroupQueryAttention",[vh]],["HardSigmoid",[mc,fc]],["InstanceNormalization",[xh]],["LayerNormalization",[Sh]],["LeakyRelu",[uc,lr]],["Less",[Rc]],["LessOrEqual",[Nc]],["Log",[xc]],["MatMul",[kh]],["MatMulNBits",[Th,Ih]],["MaxPool",[Rh,Bh]],["Mul",[zc]],["MultiHeadAttention",[mh,fh]],["Neg",[dc]],["Not",[lc]],["Pad",[Eh]],["Pow",[Cc]],["QuickGelu",[Sc,lr]],["Range",[Ph]],["Reciprocal",[pc]],["ReduceMin",[Bp]],["ReduceMean",[zp]],["ReduceMax",[Rp]],["ReduceSum",[Mp]],["ReduceProd",[Np]],["ReduceL1",[Cp]],["ReduceL2",[Ap]],["ReduceLogSum",[Up]],["ReduceLogSumExp",[Op]],["ReduceSumSquare",[Dp]],["Relu",[cc]],["Resize",[Wh,Vh]],["RotaryEmbedding",[wh]],["ScatterND",[Lh,qh]],["Sigmoid",[hc]],["Sin",[gc]],["Sinh",[_c]],["Slice",[Hh,Fh]],["SkipLayerNormalization",[Gh]],["Split",[_h,yh]],["Sqrt",[yc]],["Softmax",[jh,Kh]],["Sub",[Ac]],["Tan",[bc]],["Tanh",[wc]],["ThresholdedRelu",[vc,lr]],["Tile",[Zh]],["Transpose",[_p,yp]],["Where",[Qh]]])}),Yh,L_=U(()=>{Ve(),ot(),ae(),Yh=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,r,i,a){rt(e.programInfo.name);let n=this.backend.device,s=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let u=[];for(let p of t)u.push({binding:u.length,resource:{buffer:p.buffer}});for(let p of r)u.push({binding:u.length,resource:{buffer:p.buffer}});a&&u.push({binding:u.length,resource:a});let l=n.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:u,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let p={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:l,dispatchGroup:i};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(p)}s.setPipeline(e.computePipeline),s.setBindGroup(0,l),s.dispatchWorkgroups(...i),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>
2808=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Xe(e.programInfo.name)}dispose(){}build(e,t){rt(e.name);let r=this.backend.device,i=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(p=>{r.features.has(p.feature)&&i.push(`enable ${p.extension};`)});let a=gp(t,this.backend.device.limits),n=e.getShaderSource(a),s=`${i.join(`
2809`)}
2810${a.additionalImplementations}
2811${n}`,u=r.createShaderModule({code:s,label:e.name});de("verbose",()=>`[WebGPU] ${e.name} shader code: ${s}`);let l=r.createComputePipeline({compute:{module:u,entryPoint:"main"},layout:"auto",label:e.name});return Xe(e.name),{programInfo:e,computePipeline:l,uniformVariablesInfo:a.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,r=typeof e=="number"?1:e.y||1,i=typeof e=="number"?1:e.z||1,a=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=a&&r<=a&&i<=a)return[t,r,i];let n=t*r*i,s=Math.ceil(Math.sqrt(n));if(s>a){if(s=Math.ceil(Math.cbrt(n)),s>a)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[s,s,s]}else return[s,s,1]}}}),Jh={};Ht(Jh,{WebGpuBackend:()=>ef});var Td,Id,Ed,ef,W_=U(()=>{Ve(),te(),ot(),pp(),Yg(),q_(),L_(),Td=(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 i=0;i<e.length;++i){let a=e[i].dataType;switch(t[i]){case"none":{r.push("");break}case"type":{r.push(`${a}`);break}case"rank":{let n=e[i].dims.length;r.push(`${a};${n}`);break}case"dims":{let n=e[i].dims.join(",");r.push(`${a};${n}`);break}default:throw new Error(`unsupported input dependency: ${t[i]}`)}}return r.join("|")},Id=(e,t,r)=>{var a,n;let i=e.name;return(a=e.shaderCache)!=null&&a.hint&&(i+="["+e.shaderCache.hint+"]"),i+=":"+r+`:${Td(t,((n=e.shaderCache)==null?void 0:n.inputDependencies)??new Array(t.length).fill("dims"))}`,i},Ed=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vendor)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},ef=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let r=[],i={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:r},a=n=>t.features.has(n)&&r.push(n)&&!0;a("chromium-experimental-timestamp-query-inside-passes")||a("timestamp-query"),a("shader-f16"),a("subgroups"),this.device=await t.requestDevice(i),this.adapterInfo=new Ed(t.info||await t.requestAdapterInfo()),this.gpuDataManager=fp(this),this.programManager=new Yh(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,qa(e.logLevel,!!e.debug),this.device.onuncapturederror=n=>
2811{n.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${n.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!0}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){var e;typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose(),this.device&&((e=this.env)!=null&&e.webgpu)&&this.device.lost.then(()=>{delete this.env.webgpu.device})}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;rt(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{var i;let t=new BigUint64Array(e.getMappedRange()),r=this.pendingQueries.get(e);for(let a=0;a<t.length/2;a++){let n=r[a],s=n.kernelId,u=this.kernels.get(s),l=u.kernelType,p=u.kernelName,f=n.programName,h=n.inputTensorViews,g=n.outputTensorViews,_=t[a*2],y=t[a*2+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=_);let b=Number(_-this.queryTimeBase),S=Number(y-this.queryTimeBase);if(!Number.isSafeInteger(b)||!Number.isSafeInteger(S))throw new RangeError("incorrect timestamp range");if((i=this.env.webgpu.profiling)!=null&&i.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:h.map(x=>({dims:x.dims,dataType:st(x.dataType)})),outputsMetadata:g.map(x=>({dims:x.dims,dataType:st(x.dataType)})),kernelId:s,kernelType:l,kernelName:p,programName:f,startTime:b,endTime:S});else{let x="";h.forEach((I,k)=>{x+=`input[${k}]: [${I.dims}] | ${st(I.dataType)}, `});let w="";g.forEach((I,k)=>{w+=`output[${k}]: [${I.dims}] | ${st(I.dataType)}, `}),console.log(`[profiling] kernel "${s}|${l}|${p}|${f}" ${x}${w}start time: ${b} ns, execution time: ${S-b} ns`)}Wr("GPU",`${f}::${_}::${y}`)}e.unmap(),this.pendingQueries.delete(e)}),Xe()}run(e,t,r,i,a,n){rt(e.name);let s=[];for(let w=0;w<t.length;++w){let I=t[w].data;if(I===0)continue;let k=this.gpuDataManager.get(I);
vendor: 10,632 bytes, line 2811
2811if(!k)throw new Error(`no GPU data for input: ${I}`);s.push(k)}let{outputs:u,dispatchGroup:l,programUniforms:p}=e.getRunData(t),f=r.length===0?u.map((w,I)=>I):r;if(f.length!==u.length)throw new Error(`Output size ${f.length} must be equal to ${u.length}.`);let h=[],g=[];for(let w=0;w<u.length;++w){if(!Number.isInteger(f[w])||f[w]<-3||f[w]>=n)throw new Error(`Invalid output index: ${f[w]}`);if(f[w]===-3)continue;let I=f[w]===-1,k=f[w]===-2,E=I||k?a(u[w].dataType,u[w].dims):i(f[w],u[w].dataType,u[w].dims);if(h.push(E),E.data===0)continue;let C=this.gpuDataManager.get(E.data);if(!C)throw new Error(`no GPU data for output: ${E.data}`);if(I&&this.temporaryData.push(C),k){let A=this.kernelPersistentData.get(this.currentKernelId);A||(A=[],this.kernelPersistentData.set(this.currentKernelId,A)),A.push(C)}g.push(C)}if(s.length!==t.length||g.length!==h.length){if(g.length===0)return Xe(e.name),h;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let _;if(p){let w=0,I=[];p.forEach(A=>{let v=typeof A.data=="number"?[A.data]:A.data;if(v.length===0)return;let M=A.type===10?2:4,D,H;A.type===10?(H=v.length>4?16:v.length>2?8:v.length*M,D=v.length>4?16:M*v.length):(H=v.length<=2?v.length*M:16,D=16),w=Math.ceil(w/H)*H,I.push(w);let F=A.type===10?8:4;w+=v.length>4?Math.ceil(v.length/F)*D:v.length*M});let k=16;w=Math.ceil(w/k)*k;let E=new ArrayBuffer(w);p.forEach((A,v)=>{let M=I[v],D=typeof A.data=="number"?[A.data]:A.data;if(A.type===6)new Int32Array(E,M,D.length).set(D);else if(A.type===12)new Uint32Array(E,M,D.length).set(D);else if(A.type===10)new Uint16Array(E,M,D.length).set(D);else if(A.type===1)new Float32Array(E,M,D.length).set(D);else throw new Error(`Unsupported uniform type: ${st(A.type)}`)});let C=this.gpuDataManager.create(w,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(C.buffer,0,E,0,w),this.gpuDataManager.release(C.id),_={offset:0,size:w,buffer:C.buffer}}let y=this.programManager.normalizeDispatchGroupSize(l),b=y[1]===1&&y[2]===1,S=Id(e,t,b),x=this.programManager.getArtifact(S);if(x||(x=this.programManager.build(e,y),this.programManager.setArtifact(S,x),de("info",()=>`[artifact] key: ${S}, programName: ${e.name}`)),p&&x.uniformVariablesInfo){if(p.length!==x.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${x.uniformVariablesInfo.length}, got ${p.length} in program "${x.programInfo.name}".`);for(let w=0;w<p.length;w++){let I=p[w],k=I.type,E=typeof I.data=="number"?1:I.data.length,[C,A]=x.uniformVariablesInfo[w];if(k!==C||E!==A)throw new Error(`Uniform variable ${w} mismatch: expect type ${C} with size ${A}, got type ${k} with size ${E} in program "${x.programInfo.name}".`)}}if(de("info",()=>`[ProgramManager] run "${e.name}" (key=${S}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let w={kernelId:this.currentKernelId,programName:x.programInfo.name,inputTensorViews:t,outputTensorViews:h};this.pendingKernels.push(w),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(w)}return this.programManager.run(x,s,g,y,_),Xe(e.name),h}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,r,i){let a=Xh.get(e);if(!a)throw new Error(`kernel not implemented: ${e}`);let n={kernelType:e,kernelName:i,kernelEntry:a[0],attributes:[a[1],r]};this.kernels.set(t,n)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let r of t)this.gpuDataManager.release(r.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,r){let i=this.kernels.get(e);if(!i)throw new Error(`kernel not created: ${e}`);let a=i.kernelType,n=i.kernelName,s=i.kernelEntry,u=i.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${a}] ${n}" is not allowed to be called recursively`);this.currentKernelId=e,u[0]&&(u[1]=u[0](u[1]),u[0]=void 0),de("info",()=>`[WebGPU] Start to run kernel "[${a}] ${n}"...`);let l=this.env.debug;this.temporaryData=[];try{return l&&this.device.pushErrorScope("validation"),s(t,u[1]),0}catch(p){return r.push(Promise.resolve(`[WebGPU] Kernel "[${a}] ${n}" failed. ${p}`)),1}finally{l&&r.push(this.device.popErrorScope().then(p=>p?`GPU validation error for kernel "[${a}] ${n}": ${p.message}`:null));for(let p of this.temporaryData)this.gpuDataManager.release(p.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,r,i){let a=this.sessionExternalDataMapping.get(e);a||(a=new Map,this.sessionExternalDataMapping.set(e,a));let n=a.get(t),s=this.gpuDataManager.registerExternalBuffer(r,i,n);return a.set(t,[s,r]),s}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(r=>this.gpuDataManager.unregisterExternalBuffer(r[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,r){return async()=>{let i=await ya(this,e,t);return La(i.buffer,r)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){var e;this.queryType="none",(((e=this.env.webgpu.profiling)==null?void 0:e.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(){de("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(){de("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){de("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),r=e.length;this.pendingKernels=[];for(let i=0;i<r;i++){let a=this.getComputePassEncoder(),n=e[i];this.writeTimestamp(this.pendingDispatchNumber*2),a.setPipeline(n.computePipeline),a.setBindGroup(0,n.bindGroup),a.dispatchWorkgroups(...n.dispatchGroup),this.writeTimestamp(this.pendingDispatchNumber*2+1),this.pendingDispatchNumber++,this.queryType!=="none"&&this.pendingKernels.push(t[i]),(this.pendingDispatchNumber>=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),tf={};Ht(tf,{init:()=>rf});var Dr,zd,rf,V_=U(()=>{te(),ot(),ie(),Xg(),Dr=class af{constructor(t,r,i,a){this.module=t,this.dataType=r,this.data=i,this.dims=a}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=O.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=O.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=O.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=O.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(O.size(t)!==O.size(this.dims))throw new Error("Invalid new shape");return new af(this.module,this.dataType,this.data,t)}},zd=class{constructor(e,t,r){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let i=e.PTR_SIZE,a=r/e.PTR_SIZE,n=i===4?"i32":"i64";this.opKernelContext=Number(e.getValue(i*a++,n));let s=Number(e.getValue(i*a++,n));this.outputCount=Number(e.getValue(i*a++,n)),this.customDataOffset=Number(e.getValue(i*a++,"*")),this.customDataSize=Number(e.getValue(i*a++,n));let u=[];for(let l=0;l<s;l++){let p=Number(e.getValue(i*a++,n)),f=Number(e.getValue(i*a++,"*")),h=Number(e.getValue(i*a++,n)),g=[];for(let _=0;_<h;_++)g.push(Number(e.getValue(i*a++,n)));u.push(new Dr(e,p,f,g))}this.inputs=u}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}compute(e,t){var s;let r=((s=t==null?void 0:t.inputs)==null?void 0:s.map(u=>typeof u=="number"?this.inputs[u]:u))??this.inputs,i=(t==null?void 0:t.outputs)??[],a=(u,l,p)=>new Dr(this.module,l,this.output(u,p),p),n=(u,l)=>{let p=zt(u,l);if(!p)throw new Error(`Unsupported data type: ${u}`);let f=p>0?this.backend.gpuDataManager.create(p).id:0;return new Dr(this.module,u,f,l)};return this.backend.run(e,r,i,a,n,this.outputCount)}output(e,t){let r=this.module.stackSave();try{let i=this.module.PTR_SIZE,a=i===4?"i32":"i64",n=this.module.stackAlloc((1+t.length)*i);this.module.setValue(n,t.length,a);for(let s=0;s<t.length;s++)this.module.setValue(n+i*(s+1),t[s],a);return this.module._JsepOutput(this.opKernelContext,e,n)}catch(i){throw new Error(`Failed to generate kernel's output[${e}] with dims [${t}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${i}`)}finally{this.module.stackRestore(r)}}},rf=async(e,t,r,i)=>{let a=t.jsepInit;if(!a)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let n=(W_(),cr(Jh)).WebGpuBackend,s=new n;await s.initialize(r,i),a("webgpu",[s,u=>s.alloc(Number(u)),u=>s.free(u),(u,l,p,f=!1)=>
2811{if(f)de("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(u)}, dst=${Number(l)}, size=${Number(p)}`),s.memcpy(Number(u),Number(l));else{de("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(u)}, gpuDataId=${Number(l)}, size=${Number(p)}`);let h=t.HEAPU8.subarray(Number(u>>>0),Number(u>>>0)+Number(p));s.upload(Number(l),h)}},async(u,l,p)=>{de("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${u}, dataOffset=${l}, size=${p}`),await s.download(Number(u),()=>t.HEAPU8.subarray(Number(l)>>>0,Number(l+p)>>>0))},(u,l,p)=>s.createKernel(u,Number(l),p,t.UTF8ToString(t._JsepGetNodeName(Number(l)))),u=>s.releaseKernel(u),(u,l,p,f)=>{de("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${p}, kernel=${u}, contextDataOffset=${l}`);let h=new zd(t,s,Number(l));return s.computeKernel(Number(u),h,f)},()=>s.captureBegin(),()=>s.captureEnd(),()=>s.replay()])}else{let n=new hp(r);a("webnn",[n,()=>n.reserveTensorId(),s=>n.releaseTensorId(s),async(s,u,l,p,f)=>n.ensureTensor(s,u,l,p,f),(s,u)=>{n.uploadTensor(s,u)},async(s,u)=>n.downloadTensor(s,u),(s,u)=>n.registerMLContext(s,u),!!r.trace])}}}),Cd,Ya,Ja,mt,Ad,pa,Zr,en,tn,ca,rn,an,nn,nf=U(()=>{Ve(),Kg(),Zg(),te(),Nt(),Ma(),op(),Cd=(e,t)=>{ye()._OrtInit(e,t)!==0&&ge("Can't initialize onnxruntime.")},Ya=async e=>{Cd(e.wasm.numThreads,Gr(e.logLevel))},Ja=async(e,t)=>{var i,a;(a=(i=ye()).asyncInit)==null||a.call(i);let r=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(r){if(typeof r.limits!="object"||typeof r.features!="object"||typeof r.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let n=e.webgpu.powerPreference;if(n!==void 0&&n!=="low-power"&&n!=="high-performance")throw new Error(`Invalid powerPreference setting: "${n}"`);let s=e.webgpu.forceFallbackAdapter;if(s!==void 0&&typeof s!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${s}"`);if(r=await navigator.gpu.requestAdapter({powerPreference:n,forceFallbackAdapter:s}),!r)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let n=(V_(),cr(tf)).init;t==="webgpu"&&await n("webgpu",ye(),e,r),t==="webnn"&&await n("webnn",ye(),e)}},mt=new Map,Ad=e=>{let t=ye(),r=t.stackSave();try{let i=t.PTR_SIZE,a=t.stackAlloc(2*i);t._OrtGetInputOutputCount(e,a,a+i)!==0&&ge("Can't get session input/output count.");let n=i===4?"i32":"i64";return[Number(t.getValue(a,n)),Number(t.getValue(a+i,n))]}finally{t.stackRestore(r)}},pa=(e,t)=>{let r=ye(),i=r.stackSave(),a=0;try{let n=r.PTR_SIZE,s=r.stackAlloc(2*n);r._OrtGetInputOutputMetadata(e,t,s,s+n)!==0&&ge("Can't get session input/output metadata.");let u=Number(r.getValue(s,"*"));a=Number(r.getValue(s+n,"*"));let l=r.HEAP32[a/4];if(l===0)return[u,0];let p=r.HEAPU32[a/4+1],f=[];for(let h=0;h<p;h++){let g=Number(r.getValue(a+8+h*n,"*"));f.push(g!==0?r.UTF8ToString(g):Number(r.getValue(a+8+(h+p)*n,"*")))}return[u,l,f]}finally{r.stackRestore(i),a!==0&&r._OrtFree(a)}},Zr=e=>{let t=ye(),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]},en=async(e,t)=>{var h,g,_,y;let r,i,a=ye();Array.isArray(e)?[r,i]=e:e.buffer===a.HEAPU8.buffer?[r,i]=[e.byteOffset,e.byteLength]:[r,i]=Zr(e);let n=0,s=0,u=0,l=[],p=[],f=[];try{if([s,l]=await sp(t),(t==null?void 0:t.externalData)&&a.mountExternalData){let v=[];for(let M of t.externalData){let D=typeof M=="string"?M:M.path;v.push(Pa(typeof M=="string"?M:M.data).then(H=>{a.mountExternalData(D,H)}))}await Promise.all(v)}for(let v of(t==null?void 0:t.executionProviders)??[])if((typeof v=="string"?v:v.name)==="webnn"){if(a.shouldTransferToMLTensor=!1,typeof v!="string"){let M=v,D=M==null?void 0:M.context,H=M==null?void 0:M.gpuDevice,F=M==null?void 0:M.deviceType,j=M==null?void 0:M.powerPreference;D?a.currentContext=D:H?a.currentContext=await a.webnnCreateMLContext(H):a.currentContext=await a.webnnCreateMLContext({deviceType:F,powerPreference:j})}else a.currentContext=await a.webnnCreateMLContext();break}n=await a._OrtCreateSession(r,i,s),(h=a.webgpuOnCreateSession)==null||h.call(a,n),n===0&&ge("Can't create a session."),(g=a.jsepOnCreateSession)==null||g.call(a),a.currentContext&&(a.webnnRegisterMLContext(n,a.currentContext),a.currentContext=void 0,a.shouldTransferToMLTensor=!0);let[b,S]=Ad(n),x=!!(t!=null&&t.enableGraphCapture),w=[],I=[],k=[],E=[],C=[];for(let v=0;v<b;v++){let[M,D,H]=pa(n,v);M===0&&ge("Can't get an input name."),p.push(M);let F=a.UTF8ToString(M);w.push(F),k.push(D===0?{name:F,isTensor:!1}:{name:F,isTensor:!0,type:st(D),shape:H})}for(let v=0;v<S;v++){let[M,D,H]=pa(n,v+b);M===0&&ge("Can't get an output name."),f.push(M);let F=a.UTF8ToString(M);I.push(F),E.push(D===0?{name:F,isTensor:!1}:{name:F,isTensor:!0,type:st(D),shape:H});{if(x&&(t==null?void 0:t.preferredOutputLocation)===void 0){C.push("gpu-buffer");continue}let j=typeof(t==null?void 0:t.preferredOutputLocation)=="string"?t.preferredOutputLocation:((_=t==null?void 0:t.preferredOutputLocation)==null?void 0:_[F])??"cpu",R=a.webnnIsGraphOutput;if(j==="cpu"&&R&&R(n,F)){C.push("ml-tensor-cpu-output");continue}if(j!=="cpu"&&j!=="cpu-pinned"&&j!=="gpu-buffer"&&j!=="ml-tensor")throw new Error(`Not supported preferred output location: ${j}.`);if(x&&j!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${j}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);C.push(j)}}let A=null;return C.some(v=>v==="gpu-buffer"||v==="ml-tensor"||v==="ml-tensor-cpu-output")&&(u=a._OrtCreateBinding(n),u===0&&ge("Can't create IO binding."),A={handle:u,outputPreferredLocations:C,outputPreferredLocationsEncoded:C.map(v
vendor: 6,967 bytes, line 2811
2811=>v==="ml-tensor-cpu-output"?"ml-tensor":v).map(v=>ga(v))}),mt.set(n,[n,p,f,A,x,!1]),[n,w,I,k,E]}catch(b){throw p.forEach(S=>a._OrtFree(S)),f.forEach(S=>a._OrtFree(S)),u!==0&&a._OrtReleaseBinding(u)!==0&&ge("Can't release IO binding."),n!==0&&a._OrtReleaseSession(n)!==0&&ge("Can't release session."),b}finally{a._free(r),s!==0&&a._OrtReleaseSessionOptions(s)!==0&&ge("Can't release session options."),l.forEach(b=>a._free(b)),(y=a.unmountExternalData)==null||y.call(a)}},tn=e=>{var l,p,f;let t=ye(),r=mt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[i,a,n,s,u]=r;s&&(u&&t._OrtClearBoundOutputs(s.handle)!==0&&ge("Can't clear bound outputs."),t._OrtReleaseBinding(s.handle)!==0&&ge("Can't release IO binding.")),(l=t.jsepOnReleaseSession)==null||l.call(t,e),(p=t.webnnOnReleaseSession)==null||p.call(t,e),(f=t.webgpuOnReleaseSession)==null||f.call(t,e),a.forEach(h=>t._OrtFree(h)),n.forEach(h=>t._OrtFree(h)),t._OrtReleaseSession(i)!==0&&ge("Can't release session."),mt.delete(e)},ca=async(e,t,r,i,a,n,s=!1)=>{if(!e){t.push(0);return}let u=ye(),l=u.PTR_SIZE,p=e[0],f=e[1],h=e[3],g=h,_,y;if(p==="string"&&(h==="gpu-buffer"||h==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(s&&h!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${n} when enableGraphCapture is true.`);if(h==="gpu-buffer"){let x=e[2].gpuBuffer;y=zt(Et(p),f);{let w=u.jsepRegisterBuffer;if(!w)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');_=w(i,n,x,y)}}else if(h==="ml-tensor"){let x=e[2].mlTensor;y=zt(Et(p),f);let w=u.webnnRegisterMLTensor;if(!w)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');_=w(i,x,Et(p),f)}else{let x=e[2];if(Array.isArray(x)){y=l*x.length,_=u._malloc(y),r.push(_);for(let w=0;w<x.length;w++){if(typeof x[w]!="string")throw new TypeError(`tensor data at index ${w} is not a string`);u.setValue(_+w*l,Qe(x[w],r),"*")}}else{let w=u.webnnIsGraphInput,I=u.webnnIsGraphOutput;if(p!=="string"&&w&&I){let k=u.UTF8ToString(a);if(w(i,k)||I(i,k)){let E=Et(p);y=zt(E,f),g="ml-tensor";let C=u.webnnCreateTemporaryTensor,A=u.webnnUploadTensor;if(!C||!A)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');let v=await C(i,E,f);A(v,new Uint8Array(x.buffer,x.byteOffset,x.byteLength)),_=v}else y=x.byteLength,_=u._malloc(y),r.push(_),u.HEAPU8.set(new Uint8Array(x.buffer,x.byteOffset,y),_)}else y=x.byteLength,_=u._malloc(y),r.push(_),u.HEAPU8.set(new Uint8Array(x.buffer,x.byteOffset,y),_)}}let b=u.stackSave(),S=u.stackAlloc(4*f.length);try{f.forEach((w,I)=>u.setValue(S+I*l,w,l===4?"i32":"i64"));let x=u._OrtCreateTensor(Et(p),_,y,S,f.length,ga(g));x===0&&ge(`Can't create tensor for input/output. session=${i}, index=${n}.`),t.push(x)}finally{u.stackRestore(b)}},rn=async(e,t,r,i,a,n)=>{var F,j,R,K;let s=ye(),u=s.PTR_SIZE,l=mt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let p=l[0],f=l[1],h=l[2],g=l[3],_=l[4],y=l[5],b=t.length,S=i.length,x=0,w=[],I=[],k=[],E=[],C=[],A=s.stackSave(),v=s.stackAlloc(b*u),M=s.stackAlloc(b*u),D=s.stackAlloc(S*u),H=s.stackAlloc(S*u);try{[x,w]=np(n),Ct("wasm prepareInputOutputTensor");for(let W=0;W<b;W++)await ca(r[W],I,E,e,f[t[W]],t[W],_);for(let W=0;W<S;W++)await ca(a[W],k,E,e,h[i[W]],b+i[W],_);At("wasm prepareInputOutputTensor");for(let W=0;W<b;W++)s.setValue(v+W*u,I[W],"*"),s.setValue(M+W*u,f[t[W]],"*");for(let W=0;W<S;W++)s.setValue(D+W*u,k[W],"*"),s.setValue(H+W*u,h[i[W]],"*");if(g&&!y){let{handle:W,outputPreferredLocations:ue,outputPreferredLocationsEncoded:P}=g;if(f.length!==b)throw new Error(`input count from feeds (${b}) is expected to be always equal to model's input count (${f.length}).`);Ct("wasm bindInputsOutputs");for(let V=0;V<b;V++){let Q=t[V];await s._OrtBindInput(W,f[Q],I[V])!==0&&ge(`Can't bind input[${V}] for session=${e}.`)}for(let V=0;V<S;V++){let Q=i[V];(F=a[V])!=null&&F[3]?(C.push(k[V]),s._OrtBindOutput(W,h[Q],k[V],0)!==0&&ge(`Can't bind pre-allocated output[${V}] for session=${e}.`)):s._OrtBindOutput(W,h[Q],0,P[Q])!==0&&ge(`Can't bind output[${V}] to ${ue[V]} for session=${e}.`)}At("wasm bindInputsOutputs"),mt.set(e,[p,f,h,g,_,!0])}(j=s.jsepOnRunStart)==null||j.call(s,p),(R=s.webnnOnRunStart)==null||R.call(s,p);let Z;g?Z=await s._OrtRunWithBinding(p,g.handle,S,D,x):Z=await s._OrtRun(p,M,v,b,H,S,D,x),Z!==0&&ge("failed to call OrtRun().");let ee=[],he=[];Ct("wasm ProcessOutputTensor");for(let W=0;W<S;W++){let ue=Number(s.getValue(D+W*u,"*"));if(ue===k[W]||C.includes(k[W])){ee.push(a[W]),ue!==k[W]&&s._OrtReleaseTensor(ue)!==0&&ge("Can't release tensor.");continue}let P=s.stackSave(),V=s.stackAlloc(4*u),Q=!1,q,me=0;try{s._OrtGetTensorData(ue,V,V+u,V+2*u,V+3*u)!==0&&ge(`Can't access output tensor data on index ${W}.`);let Le=u===4?"i32":"i64",Se=Number(s.getValue(V,Le));me=s.getValue(V+u,"*");let Oe=s.getValue(V+u*2,"*"),Re=Number(s.getValue(V+u*3,Le)),Me=[];for(let be=0;be<Re;be++)Me.push(Number(s.getValue(Oe+be*u,Le)));s._OrtFree(Oe)!==0&&ge("Can't free memory for tensor dims.");let Be=Me.reduce((be,re)=>be*re,1);q=st(Se);let ut=g==null?void 0:g.outputPreferredLocations[i[W]];if(q==="string"){if(ut==="gpu-buffer"||ut==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let be=[];for(let re=0;re<Be;re++){let De=s.getValue(me+re*u,"*"),fr=s.getValue(me+(re+1)*u,"*"),Ft=re===Be-1?void 0:fr-De;be.push(s.UTF8ToString(De,Ft))}ee.push([q,Me,be,"cpu"])}else if(ut==="gpu-buffer"&&Be>0){let be=s.jsepGetBuffer;if(!be)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let re=be(me),De=zt(Se,Be);if(De===void 0||!Da(q))throw new Error(`Unsupported data type: ${q}`);Q=!0,ee.push([q,Me,{gpuBuffer:re,download:s.jsepCreateDownloader(re,De,q),dispose:()=>{s._OrtReleaseTensor(ue)!==0&&ge("Can't release tensor.")}},"gpu-buffer"])}else if(ut==="ml-tensor"&&Be>0){let be=s.webnnEnsureTensor,re=s.webnnIsGraphInputOutputTypeSupported;if(!be||!re)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(zt(Se,Be)===void 0||!Ua(q))throw new Error(`Unsupported data type: ${q}`);if(!re(e,q,!1))throw new Error(`preferredLocation "ml-tensor" for ${q} output is not supported by current WebNN Context.`);let De=await be(e,me,Se,Me,!1);Q=!0,ee.push([q,Me,{mlTensor:De,download:s.webnnCreateMLTensorDownloader(me,q),dispose:()=>{s.webnnReleaseTensorId(me),s._OrtReleaseTensor(ue)}},"ml-tensor"])}else if(ut==="ml-tensor-cpu-output"&&Be>0){let be=s.webnnCreateMLTensorDownloader(me,q)(),re=ee.length;Q=!0,he.push((async()=>{let De=[re,await be];return s.webnnReleaseTensorId(me),s._OrtReleaseTensor(ue),De})()),ee.push([q,Me,[],"cpu"])}else{let be=Qr(q),re=new be(Be);new Uint8Array(re.buffer,re.byteOffset,re.byteLength).set(s.HEAPU8.subarray(me,me+re.byteLength)),ee.push([q,Me,re,"cpu"])}}finally{s.stackRestore(P),q==="string"&&me&&s._free(me),Q||s._OrtReleaseTensor(ue)}
vendor: 6,505 bytes, lines 2811-2814
2811}g&&!_&&(s._OrtClearBoundOutputs(g.handle)!==0&&ge("Can't clear bound outputs."),mt.set(e,[p,f,h,g,_,!1]));for(let[W,ue]of await Promise.all(he))ee[W][2]=ue;return At("wasm ProcessOutputTensor"),ee}finally{(K=s.webnnOnRunEnd)==null||K.call(s,p),s.stackRestore(A),I.forEach(Z=>s._OrtReleaseTensor(Z)),k.forEach(Z=>s._OrtReleaseTensor(Z)),E.forEach(Z=>s._free(Z)),x!==0&&s._OrtReleaseRunOptions(x),w.forEach(Z=>s._free(Z))}},an=e=>{let t=ye(),r=mt.get(e);if(!r)throw new Error("invalid session id");let i=r[0],a=t._OrtEndProfiling(i);a===0&&ge("Can't get an profile file name."),t._OrtFree(a)},nn=e=>{let t=[];for(let r of e){let i=r[2];!Array.isArray(i)&&"buffer"in i&&t.push(i.buffer)}return t}}),gt,Ne,Pt,nr,sr,Ur,ha,Pr,kt,Tt,Od,sf,of,uf,lf,df,pf,cf,hf=U(()=>{Ve(),nf(),Nt(),Ba(),gt=()=>!!$e.wasm.proxy&&typeof document<"u",Pt=!1,nr=!1,sr=!1,Pr=new Map,kt=(e,t)=>{let r=Pr.get(e);r?r.push(t):Pr.set(e,[t])},Tt=()=>{if(Pt||!nr||sr||!Ne)throw new Error("worker not ready")},Od=e=>{switch(e.data.type){case"init-wasm":Pt=!1,e.data.err?(sr=!0,ha[1](e.data.err)):(nr=!0,ha[0]()),Ur&&(URL.revokeObjectURL(Ur),Ur=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=Pr.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}}},sf=async()=>{if(!nr){if(Pt)throw new Error("multiple calls to 'initWasm()' detected.");if(sr)throw new Error("previous call to 'initWasm()' failed.");if(Pt=!0,gt())return new Promise((e,t)=>{Ne==null||Ne.terminate(),ip().then(([r,i])=>{try{Ne=i,Ne.onerror=n=>t(n),Ne.onmessage=Od,ha=[e,t];let a={type:"init-wasm",in:$e};!a.in.wasm.wasmPaths&&(r||ma)&&(a.in.wasm.wasmPaths={wasm:new URL(""+new URL("../assets/ort-wasm-simd-threaded.jsep.CyqnNavA.wasm",import.meta.url).href,import.meta.url).href}),Ne.postMessage(a),Ur=r}catch(a){t(a)}},t)});try{await Na($e.wasm),await Ya($e),nr=!0}catch(e){throw sr=!0,e}finally{Pt=!1}}},of=async e=>{if(gt())return Tt(),new Promise((t,r)=>{kt("init-ep",[t,r]);let i={type:"init-ep",in:{epName:e,env:$e}};Ne.postMessage(i)});await Ja($e,e)},uf=async e=>gt()?(Tt(),new Promise((t,r)=>{kt("copy-from",[t,r]);let i={type:"copy-from",in:{buffer:e}};Ne.postMessage(i,[e.buffer])})):Zr(e),lf=async(e,t)=>{if(gt()){if(t!=null&&t.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Tt(),new Promise((r,i)=>{kt("create",[r,i]);let a={type:"create",in:{model:e,options:{...t}}},n=[];e instanceof Uint8Array&&n.push(e.buffer),Ne.postMessage(a,n)})}else return en(e,t)},df=async e=>{if(gt())return Tt(),new Promise((t,r)=>{kt("release",[t,r]);let i={type:"release",in:e};Ne.postMessage(i)});tn(e)},pf=async(e,t,r,i,a,n)=>{if(gt()){if(r.some(s=>s[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(a.some(s=>s))throw new Error("pre-allocated output tensor is not supported for proxy.");return Tt(),new Promise((s,u)=>{kt("run",[s,u]);let l=r,p={type:"run",in:{sessionId:e,inputIndices:t,inputs:l,outputIndices:i,options:n}};Ne.postMessage(p,nn(l))})}else return rn(e,t,r,i,a,n)},cf=async e=>{if(gt())return Tt(),new Promise((t,r)=>{kt("end-profiling",[t,r]);let i={type:"end-profiling",in:e};Ne.postMessage(i)});an(e)}}),fa,Rd,ff,G_=U(()=>{Ve(),hf(),te(),Ra(),op(),fa=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},Rd=e=>{switch(e[3]){case"cpu":return new ze(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Da(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:i,dispose:a}=e[2];return ze.fromGpuBuffer(r,{dataType:t,dims:e[1],download:i,dispose:a})}case"ml-tensor":{let t=e[0];if(!Ua(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:r,download:i,dispose:a}=e[2];return ze.fromMLTensor(r,{dataType:t,dims:e[1],download:i,dispose:a})}default:throw new Error(`invalid data location: ${e[3]}`)}},ff=class{async fetchModelAndCopyToWasmMemory(e){return uf(await Pa(e))}async loadModel(e,t){rt();let r;typeof e=="string"?r=await this.fetchModelAndCopyToWasmMemory(e):r=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await lf(r,t),Xe()}async dispose(){return df(this.sessionId)}async run(e,t,r){rt();let i=[],a=[];Object.entries(e).forEach(h=>{let g=h[0],_=h[1],y=this.inputNames.indexOf(g);if(y===-1)throw new Error(`invalid input '${g}'`);i.push(_),a.push(y)});let n=[],s=[];Object.entries(t).forEach(h=>{let g=h[0],_=h[1],y=this.outputNames.indexOf(g);if(y===-1)throw new Error(`invalid output '${g}'`);n.push(_),s.push(y)});let u=i.map((h,g)=>fa(h,()=>`input "${this.inputNames[a[g]]}"`)),l=n.map((h,g)=>h?fa(h,()=>`output "${this.outputNames[s[g]]}"`):null),p=await pf(this.sessionId,a,u,s,l,r),f={};for(let h=0;h<p.length;h++)f[this.outputNames[s[h]]]=n[h]??Rd(p[h]);return Xe(),f}startProfiling(){}endProfiling(){cf(this.sessionId)}}}),mf={};Ht(mf,{OnnxruntimeWebAssemblyBackend:()=>Ca,initializeFlags:()=>za,wasmBackend:()=>gf});var za,Ca,gf,H_=U(()=>{Ve(),hf(),G_(),za=()=>{(typeof $e.wasm.initTimeout!="number"||$e.wasm.initTimeout<0)&&($e.wasm.initTimeout=0);let e=$e.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),$e.wasm.simd=!1),typeof $e.wasm.proxy!="boolean"&&($e.wasm.proxy=!1),typeof $e.wasm.trace!="boolean"&&($e.wasm.trace=!1),typeof $e.wasm.numThreads!="number"||!Number.isInteger($e.wasm.numThreads)||$e.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)$e.wasm.numThreads=1;else{let t=typeof navigator>"u"?Cg("node:os").cpus().length:navigator.hardwareConcurrency;$e.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},Ca=class{async init(e){za(),await sf(),await of(e)}async createInferenceSessionHandler(e,t){let r=new ff;return await r.loadModel(e,t),r}},gf=new Ca});Ve();Ve();Ve();var F_="1.26.0";{let e=(H_(),cr(mf)).wasmBackend;qt("webgpu",e,5),qt("webnn",e,5),qt("cpu",e,10),qt("wasm",e,10)}Object.defineProperty($e.versions,"web",{value:F_,enumerable:!0});/**
2812* @license
2813* Copyright 2021 Google LLC. All Rights Reserved.
2814* Licensed under the Apache License, Version 2.0 (the "License");
2815* you may not use this file except in compliance with the License.
2816* You may obtain a copy of the License at
2817*
2818* http://www.apache.org/licenses/LICENSE-2.0
2819*
2820* Unless required by applicable law or agreed to in writing, software
2821* distributed under the License is distributed on an "AS IS" BASIS,
2822* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2823* See the License for the specific language governing permissions and
2824* limitations under the License.
2825* =============================================================================
2826*//**
2827 * @license
2828 * Copyright 2020 Google LLC. All Rights Reserved.
2829 * Licensed under the Apache License, Version 2.0 (the "License");
2830 * you may not use this file except in compliance with the License.
2831 * You may obtain a copy of the License at
2832 *
2833 * http://www.apache.org/licenses/LICENSE-2.0
2834 *
2835 * Unless required by applicable law or agreed to in writing, software
2836 * distributed under the License is distributed on an "AS IS" BASIS,
2837 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2838 * See the License for the specific language governing permissions and
2839 * limitations under the License.
2840 * =============================================================================
2841 *//**
2842 * @license
2843 * Copyright 2019 Google LLC. All Rights Reserved.
2844 * Licensed under the Apache License, Version 2.0 (the "License");
2845 * you may not use this file except in compliance with the License.
2846 * You may obtain a copy of the License at
2847 *
2848 * http://www.apache.org/licenses/LICENSE-2.0
2849 *
2850 * Unless required by applicable law or agreed to in writing, software
2851 * distributed under the License is distributed on an "AS IS" BASIS,
2852 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2853 * See the License for the specific language governing permissions and
2854 * limitations under the License.
2855 * =============================================================================
2856 */const j_={alexa:"alexa_v0.1.onnx",hey_mycroft:"hey_mycroft_v0.1.onnx",hey_jarvis:"hey_jarvis_v0.1.onnx",hey_rhasspy:"hey_rhasspy_v0.1.onnx",timer:"timer_v0.1.onnx",weather:"weather_v0.1.onnx"},K_=`
2857class AudioProcessor extends AudioWorkletProcessor {
2858    bufferSize = 1280;
2859    _buffer = new Float32Array(this.bufferSize);
2860    _pos = 0;
2861    process(inputs) {
2862        const input = inputs[0][0];
2863        if (input) {
2864            for (let i = 0; i < input.length; i++) {
2865                this._buffer[this._pos++] = input[i];
2866                if (this._pos === this.bufferSize) {
2867                    this.port.postMessage(this._buffer);
2868                    this._pos = 0;
2869                }
2870            }
2871        }
2872        return true;
2873    }
2874}
2875registerProcessor('audio-processor', AudioProcessor);
2876`,Z_=()=>{const e=new Map;return{on(t,r){return e.has(t)||e.set(t,new Set),e.get(t).add(r),()=>this.off(t,r)},off(t,r){const i=e.get(t);i&&i.delete(r)},emit(t,r){const i=e.get(t);if(i)for(const a of Array.from(i))a(r)}}};class X_{constructor({keywords:t=["hey_jarvis"],modelFiles:r=j_,baseAssetUrl:i="/models",ortWasmPath:a,frameSize:n=1280,sampleRate:s=16e3,vadHangoverFrames:u=12,detectionThreshold:l=.5,cooldownMs:p=2e3,executionProviders:f=["wasm"],embeddingWindowSize:h=16,debug:g=!1}={}){this.config={keywords:t,modelFiles:r,baseAssetUrl:i,frameSize:n,sampleRate:s,vadHangoverFrames:u,detectionThreshold:l,cooldownMs:p,executionProviders:f,embeddingWindowSize:h,debug:g},this._setOrtPath(a),this._emitter=Z_(),this._melBuffer=[],this._embeddingWindowSize=h,this._activeKeywords=new Set(t),this._vadState={h:null,c:null},this._isSpeechActive=!1,this._vadHangover=0,this._mediaStream=null,this._audioContext=null,this._workletNode=null,this._gainNode=null,this._processingQueue=Promise.resolve(),this._isDetectionCoolingDown=!1,this._loaded=!1}on(t,r){return this._emitter.on(t,r)}off(t,r){this._emitter.off(t,r)}async load(){if(this._loaded)return;const t={executionProviders:this.config.executionProviders},r=a=>`${this.config.baseAssetUrl.replace(/\/+$/,"")}/${a}`;this._debug("Loading core models with options",t),this._melspecModel=await Lt.create(r("melspectrogram.onnx"),t),this._embeddingModel=await Lt.create(r("embedding_model.onnx"),t),this._vadModel=await Lt.create(r("silero_vad.onnx"),t),this._keywordModels={};let i=this.config.embeddingWindowSize;for(const a of this.config.keywords){const n=this.config.modelFiles[a];if(!n)throw new Error(`No model file configured for keyword "${a}"`);const s=await Lt.create(r(n),t),u=this._inferKeywordWindowSize(s)??this.config.embeddingWindowSize;i=Math.max(i,u);const l=[];for(let p=0;p<u;p++)l.push(new Float32Array(96).fill(0));this._keywordModels[a]={session:s,scores:new Array(50).fill(0),windowSize:u,history:l},this._debug("Loaded keyword model",{keyword:a,file:n,windowSize:u})}this._embeddingWindowSize=i,this._debug("Embedding window size resolved",this._embeddingWindowSize),this._resetState(),this._loaded=!0,this._emitter.emit("ready")}async start({deviceId:t,gain:r=1}={}){if(!this._loaded)throw new Error("Call load() before start()");if(this._workletNode)return;this._resetState(),this._mediaStream=await navigator.mediaDevices.getUserMedia({audio:t?{deviceId:{exact:t}}:!0}),this._audioContext=new AudioContext({sampleRate:this.config.sampleRate});const i=this._audioContext.createMediaStreamSource(this._mediaStream);this._gainNode=this._audioContext.createGain(),this._gainNode.gain.value=r;const a=new Blob([K_],{type:"application/javascript"}),n=URL.createObjectURL(a);await this._audioContext.audioWorklet.addModule(n),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-processor"),this._workletNode.port.onmessage=s=>{const u=s.data;u&&(this._processingQueue=this._processingQueue.then(()=>this._processChunk(u)).catch(l=>{this._emitter.emit("error",l)}))},i.connect(this._gainNode),this._gainNode.connect(this._workletNode),this._workletNode.connect(this._audioContext.destination),this._debug("Microphone stream started",{deviceId:t??"default",gain:r})}async stop(){this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._gainNode&&(this._gainNode.disconnect(),this._gainNode=null),this._audioContext&&this._audioContext.state!=="closed"&&await this._audioContext.close(),this._audioContext=null,this._mediaStream&&(this._mediaStream.getTracks().forEac
2876h(t=>t.stop()),this._mediaStream=null),this._isDetectionCoolingDown=!1,this._debug("Engine stopped and media stream closed")}setGain(t){this._gainNode&&(this._gainNode.gain.value=t)}async runWav(t){if(!this._loaded)throw new Error("Call load() before runWav()");this._resetState();const i=await new(window.AudioContext||window.webkitAudioContext)().decodeAudioData(t.slice(0)),a=new OfflineAudioContext(1,Math.ceil(i.length*this.config.sampleRate/i.sampleRate),this.config.sampleRate),n=a.createBufferSource();n.buffer=i,n.connect(a.destination),n.start();const u=(await a.startRendering()).getChannelData(0);this._debug("Running offline WAV",{samples:u.length});const l=this._embeddingWindowSize*this.config.frameSize;let p=u;if(p.length<l){const h=new Float32Array(l-p.length),g=new Float32Array(l);g.set(p,0),g.set(h,p.length),p=g}let f=0;for(let h=0;h<Math.floor(p.length/this.config.frameSize);h++){const g=p.subarray(h*this.config.frameSize,(h+1)*this.config.frameSize);await this._processChunk(g,{emitEvents:!1});for(const _ of Object.keys(this._keywordModels)){const y=this._keywordModels[_].scores.slice(-1)[0];y>f&&(f=y)}}return f}_resetState(){this._melBuffer=[];const t=[2,1,64];if(this._vadState.h?(this._vadState.h.data.fill(0),this._vadState.c.data.fill(0)):(this._vadState.h=new ze("float32",new Float32Array(128).fill(0),t),this._vadState.c=new ze("float32",new Float32Array(128).fill(0),t)),this._isSpeechActive=!1,this._vadHangover=0,this._isDetectionCoolingDown=!1,this._keywordModels)for(const r of Object.keys(this._keywordModels)){this._keywordModels[r].scores.fill(0);const i=this._keywordModels[r].history;if(i)for(let a=0;a<i.length;a++)i[a].fill(0)}this._debug("Internal buffers reset")}async _processChunk(t,{emitEvents:r=!0}={}){if(this.config.debug){let a=0,n=0;for(let u=0;u<t.length;u++){const l=t[u];n+=l*l;const p=Math.abs(l);p>a&&(a=p)}const s=Math.sqrt(n/t.length);this._debug("Chunk received",{rms:Number(s.toFixed(4)),peak:Number(a.toFixed(4))})}await this._runVad(t)?(!this._isSpeechActive&&r&&this._emitter.emit("speech-start"),this._isSpeechActive=!0,this._vadHangover=this.config.vadHangoverFrames):this._isSpeechActive&&(this._vadHangover-=1,this._vadHangover<=0&&(this._isSpeechActive=!1,r&&this._emitter.emit("speech-end"))),await this._runInference(t,this._isSpeechActive,r)}async _runVad(t){try{const r=new ze("float32",t,[1,t.length]),i=new ze("int64",[BigInt(this.config.sampleRate)],[]),a=await this._vadModel.run({input:r,sr:i,h:this._vadState.h,c:this._vadState.c});this._vadState.h=a.hn,this._vadState.c=a.cn;const n=a.output.data[0];return this._debug("VAD result",{confidence:Number(n.toFixed(3))}),n>.5}catch(r){return this._emitter.emit("error",r),!1}}async _runInference(t,r,i){const a=new ze("float32",t,[1,this.config.frameSize]),s=(await this._melspecModel.run({[this._melspecModel.inputNames[0]]:a}))[this._melspecModel.outputNames[0]].data;for(let u=0;u<s.length;u++)s[u]=s[u]/10+2;for(let u=0;u<5;u++)this._melBuffer.push(new Float32Array(s.subarray(u*32,(u+1)*32)));for(;this._melBuffer.length>=76;){const u=this._melBuffer.slice(0,76),l=new Float32Array(2432);for(let _=0;_<u.length;_++)l.set(u[_],_*32);const p={[this._embeddingModel.inputNames[0]]:new ze("float32",l,[1,76,32,1])},h=(await this._embeddingModel.run(p))[this._embeddingModel.outputNames[0]].data,g=new Float32Array(h);for(const _ of Object.keys(this._keywordModels)){const y=this._keywordModels[_];y.history.shift(),y.history.push(g);const b=new Float32Array(y.windowSize*96);for(let k=0;k<y.history.length;k++)b.set(y.history[k],k*96);const S=new ze("float32",b,[1,y.windowSize,96]),w=(await y.session.run({[y.session.inputNames[0]]:S}))[y.session.outputNames[0]].data[0];y.scores.shift(),y.scores.push(w),this._debug("Keyword score",{keyword:_,score:Number(w.toFixed(3)),windowSize:y.windowSize});const I=this._activeKeywords.has(_);i&&I&&w>this.config.detectionThreshold&&r&&!this._isDetectionCoolingDown?(this._isDetectionCoolingDown=!0,this._debug("Detection emitted",{keyword:_,score:w}),this._emitter.emit("detect",{keyword:_,score:w,at:performance.now()}),setTimeout(()=>{this._isDetectionCoolingDown=!1},this.config.cooldownMs)):i&&!I&&this._debug("Detection suppressed (inactive keyword)",{keyword:_,score:w})}this._melBuffer.splice(0,8)}}_setOrtPath(t){t&&($e.wasm.wasmPaths=t)}_inferKeywordWindowSize(t){var s;if(!t)return;const r=t.inputMetadata,i=(s=t.inputNames)==null?void 0:s[0];if(!r||!i)return;let a;if(Array.isArray(r)?a=r.find(u=>(u==null?void 0:u.name)===i)||r[0]:a=r[i],!a||!a.isTensor||!Array.isArray(a.shape))return;const n=a.shape[1];return typeof n=="number"&&Number.isFinite(n)?n:void 0}_debug(...t){this.config.debug&&console.debug("[WakeWordEngine]",...t)}setActiveKeywords(t){const r=Array.isArray(t)&&t.length?t:this.config.keywords;this._activeKeywords=new Set(r),this._debug("Active keywords updated",Array.from(this._activeKeywords))}}export{j_ as MODEL_FILE_MAP,X_ as WakeWordEngine,X_ as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.