PageSourceSearch

https://provamoda.ai/_next/static/chunks/037f3a08.0d52295304d92eca.js

js provamoda.ai collected 2026-10-05 12:26:24 UTC 390,629 bytes, 2,747 lines download raw bytes

1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[2643],{94026:(t,r,i)=>{i.r(r),i.d(r,{InferenceSession:()=>B,TRACE:()=>E,TRACE_FUNC_BEGIN:()=>A,TRACE_FUNC_END:()=>O,Tensor:()=>I,default:()=>lI,env:()=>c,registerBackend:()=>s});var a,n,s,o,u,l,d,p,c,h,f,m,g,y,_,$,b,v,w,x,S,k,T,C,I,E,z,A,O,R,B,N=Object.defineProperty,M=Object.getOwnPropertyDescriptor,D=Object.getOwnPropertyNames,P=Object.prototype.hasOwnProperty,U=(t=>"u">typeof require?require:"u">typeof Proxy?new Proxy(t,{get:(t,r)=>("u">typeof require?require:t)[r]}):t)(function(t){if("u">typeof require)return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')}),W=(t,r)=>()=>(t&&(r=t(t=0)),r),q=(t,r)=>{for(var i in r)N(t,i,{get:r[i],enumerable:!0})},L=(t,r,i,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let n of D(r))P.call(t,n)||n===i||N(t,n,{get:()=>r[n],enumerable:!(a=M(r,n))||a.enumerable});return t},V=t=>L(N({},"__esModule",{value:!0}),t),G=W(()=>{a=new Map,n=[],s=(t,r,i)=>{if(r&&"function"==typeof r.init&&"function"==typeof r.createInferenceSessionHandler){let s=a.get(t);if(void 0===s)a.set(t,{backend:r,priority:i});else{if(s.priority>i)return;if(s.priority===i&&s.backend!==r)throw Error(`cannot register backend "${t}" using priority ${i}`)}if(i>=0){let r=n.indexOf(t);-1!==r&&n.splice(r,1);for(let r=0;r<n.length;r++)if(a.get(n[r]).priority<=i){n.splice(r,0,t);return}n.push(t)}return}throw TypeError("not a valid backend")},o=async t=>{let r=a.get(t);if(!r)return"backend not found.";if(r.initialized)return r.backend;if(r.aborted)return r.error;{let i=!!r.initPromise;try{return i||(r.initPromise=r.backend.init(t)),await r.initPromise,r.initialized=!0,r.backend}catch(t){return i||(r.error=`${t}`,r.aborted=!0),r.error}finally{delete r.initPromise}}},u=async t=>{let r=t.executionProviders||[],i=r.map(t=>"string"==typeof t?t:t.name),a=0===i.length?n:i,s,u=[],l=new Set;for(let t of a){let r=await o(t);"string"==typeof r?u.push({name:t,err:r}):(s||(s=r),s===r&&l.add(t))}if(!s)throw Error(`no available backend found. ERR: ${u.map(t=>`[${t.name}] ${t.err}`).join(", ")}`);for(let{name:t,err:r}of u)i.includes(t)&&console.warn(`removing requested execution provider "${t}" from session options because it is not available: ${r}`);let d=r.filter(t=>l.has("string"==typeof t?t:t.name));return[s,new Proxy(t,{get:(t,r)=>"executionProviders"===r?d:Reflect.get(t,r)})]}}),H=W(()=>{G()}),F=W(()=>{l="1.21.0"}),j=W(()=>{F(),d="warning",Object.defineProperty(p={wasm:{},webgl:{},webgpu:{},versions:{common:l},set logLevel(e){if(void 0!==e){if("string"!=typeof e||-1===["verbose","info","warning","error","fatal"].indexOf(e))throw Error(`Unsupported logging level: ${e}`);d=e}},get logLevel(){return d}},"logLevel",{enumerable:!0})}),K=W(()=>{j(),c=p}),Q=W(()=>{h=(t,r)=>{let i="u">typeof document?document.createElement("canvas"):new OffscreenCanvas(1,1);i.width=t.dims[3],i.height=t.dims[2];let a=i.getContext("2d");if(null!=a){let n,s;r?.tensorLayout!==void 0&&"NHWC"===r.tensorLayout?(n=t.dims[2],s=t.dims[3]):(n=t.dims[3],s=t.dims[2]);let o=r?.format!==void 0?r.format:"RGB",u=r?.norm,l,d;void 0===u||void 0===u.mean?l=[255,255,255,255]:"number"==typeof u.mean?l=[u.mean,u.mean,u.mean,u.mean]:(l=[u.mean[0],u.mean[1],u.mean[2],0],void 0!==u.mean[3]&&(l[3]=u.mean[3])),void 0===u||void 0===u.bias?d=[0,0,0,0]:"number"==typeof u.bias?d=[u.bias,u.bias,u.bias,u.bias]:(d=[u.bias[0],u.bias[1],u.bias[2],0],void 0!==u.bias[3]&&(d[3]=u.bias[3]));let p=s*n,c=0,h=p,f=2*p,m=-1;"RGBA"===o?(c=0,h=p,f=2*p,m=3*p):"RGB"===o?(c=0,h=p,f=2*p):"RBG"===o&&(c=0,f=p,h=2*p);for(let r=0;r<s;r++)for(let i=0;i<n;i++){let n=(t.data[c++]-d[0])*l[0],s=(t.data[h++]-d[1])*l[1],o=(t.data[f++]-d[2])*l[2],u=-1===m?255:(t.data[m++]-d[3])*l[3];a.fillStyle="rgba("+n+","+s+","+o+","+u+")",a.fillRect(i,r,1,1)}if("toDataURL"in i)return i.toDataURL();throw Error("toDataURL is not supported")}throw Error("Can not access image data")},f=(t,r)=>{let i="u">typeof document?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),a;if(null!=i){let n,s,o;r?.tensorLayout!==void 0&&"NHWC"===r.tensorLayout?(n=t.dims[2],s=t.dims[1],o=t.dims[3]):(n=t.dims[3],s=t.dims[2],o=t.dims[1]);let u=void 0!==r&&void 0!==r.format?r.format:"RGB",l=r?.norm,d,p;void 0===l||void 0===l.mean?d=[255,255,255,255]:"number"==typeof l.mean?d=[l.mean,l.mean,l.mean,l.mean]:(d=[l.mean[0],l.mean[1],l.mean[2],255],void 0!==l.mean[3]&&(d[3]=l.mean[3])),void 0===l||void 0===l.bias?p=[0,0,0,0]:"number"==typeof l.bias?p=[l.bias,l.bias,l.bias,l.bias]:(p=[l.bias[0],l.bias[1],l.bias[2],0],void 0!==l.bias[3]&&(p[3]=l.bias[3]));let c=s*n;if(void 0!==r&&(void 0!==r.format&&4===o&&"RGBA"!==r.format||3===o&&"RGB"!==r.format&&"BGR"!==r.format))throw Error("Tensor format doesn't match input tensor dims");let h=0,f=1,m=2,g=3,y=0,_=c,$=2*c,b=-1;"RGBA"===u?(y=0,_=c,$=2*c,b=3*c):"RGB"===u?(y=0,_=c,$=2*c):"RBG"===u&&(y=0,$=c,_=2*c),a=i.createImageData(n,s);for(let r=0;r<s*n;h+=4,f+=4,m+=4,g+=4,r++)a.data[h]=(t.data[y++]-p[0])*d[0],a.data[f]=(t.data[_++]-p[1])*d[1],a.data[m]=(t.data[$++]-p[2])*d[2],a.data[g]=-1===b?255:(t.data[b++]-p[3])*d[3]}else throw Error("Can not access image data");return a}}),Z=W(()=>{J(),m=(t,r)=>{if(void 0===t)throw Error("Image buffer must be defined");if(void 0===r.height||void 0===r.width)throw Error("Image height and width must be defined");if("NHWC"===r.tensorLayout)throw Error("NHWC Tensor layout is not supported yet");let{height:i,width:a}=r,n=r.norm??{mean:255,bias:0},s,o;s="number"==typeof n.mean?[n.mean,n.mean,n.mean,n.mean]:[n.mean[0],n.mean[1],n.mean[2],n.mean[3]??255],o="number"==typeof n.bias?[n.bias,n.bias,n.bias,n.bias]:[n.bias[0],n.bias[1],n.bias[2],n.bias[3]??0];let u=void 0!==r.format?r.format:"RGBA",l=void 0!==r.tensorFormat&&void 0!==r.tensorFormat?r.tensorFormat:"RGB",d=i*a,p=new Float32Array("RGBA"===l?4*d:3*d),c=4,h=0,f=1,m=2,g=3,y=0,_=d,$=2*d,b=-1;"RGB"===u&&(c=3,h=0,f=1,m=2,g=-1),"RGBA"===l?b=3*d:"RBG"===l?(y=0,$=d,_=2*d):"BGR"===l&&($=0,_=d,y=2*d);for(let r=0;r<d;r++,h+=c,m+=c,f+=c,g+=c)p[y++]=(t[h]+o[0])/s[0],p[_++]=(t[f]+o[1])/s[1],p[$++]=(t[m]+o[2])/s[2],-1!==b&&-1!==g&&(p[b++]=(t[g]+o[3])/s[3]);return"RGBA"===l?new C("float32",p,[1,4,i,a]):new C("float32",p,[1,3,i,a])},g=async(t,r)=>{let i="u">typeof HTMLImageElement&&t instanceof HTMLImageElement,a="u">typeof ImageData&&t instanceof ImageData,n="u">typeof ImageBitmap&&t instanceof ImageBitmap,s="string"==typeof t,o,u=r??{},l=()=>{if("u">typeof document)return document.createElement("canvas");if("u">typeof OffscreenCanvas)return new OffscreenCanvas(1,1);throw Error("Canvas is not supported")},d=t=>"u">typeof HTMLCanvasElement&&t instanceof HTMLCanvasElement||t instanceof OffscreenCanvas?t.getContext("2d"):null;if(i){let i=l();i.width=t.width,i.height=t.height;let a=d(i);if(null!=a){let i=t.height,n=t.width;if(void 0!==r&&void 0!==r.resizedHeight&&void 0!==r.resizedWidth&&(i=r.resizedHeight,n=r.resizedWidth),void 0!==r){if(u=r,void 0!==r.tensorFormat)throw Error("Image input config format must be RGBA for HTMLImageElement");
1u.tensorFormat="RGBA",u.height=i,u.width=n}else u.tensorFormat="RGBA",u.height=i,u.width=n;a.drawImage(t,0,0),o=a.getImageData(0,0,n,i).data}else throw Error("Can not access image data")}else if(a){let i,a;if(void 0!==r&&void 0!==r.resizedWidth&&void 0!==r.resizedHeight?(i=r.resizedHeight,a=r.resizedWidth):(i=t.height,a=t.width),void 0!==r&&(u=r),u.format="RGBA",u.height=i,u.width=a,void 0!==r){let r=l();r.width=a,r.height=i;let n=d(r);if(null!=n)n.putImageData(t,0,0),o=n.getImageData(0,0,a,i).data;else throw Error("Can not access image data")}else o=t.data}else if(n){if(void 0===r)throw Error("Please provide image config with format for Imagebitmap");let i=l();i.width=t.width,i.height=t.height;let a=d(i);if(null!=a){let r=t.height,i=t.width;return a.drawImage(t,0,0,i,r),o=a.getImageData(0,0,i,r).data,u.height=r,u.width=i,m(o,u)}throw Error("Can not access image data")}else{if(s)return new Promise((r,i)=>{let a=l(),n=d(a);if(!t||!n)return i();let s=new Image;s.crossOrigin="Anonymous",s.src=t,s.onload=()=>{a.width=s.width,a.height=s.height,n.drawImage(s,0,0,a.width,a.height);let t=n.getImageData(0,0,a.width,a.height);u.height=a.height,u.width=a.width,r(m(t.data,u))}});throw Error("Input data provided is not supported - aborted tensor creation")}if(void 0!==o)return m(o,u);throw Error("Input data provided is not supported - aborted tensor creation")},y=(t,r)=>{let{width:i,height:a,download:n,dispose:s}=r;return new C({location:"texture",type:"float32",texture:t,dims:[1,a,i,4],download:n,dispose:s})},_=(t,r)=>{let{dataType:i,dims:a,download:n,dispose:s}=r;return new C({location:"gpu-buffer",type:i??"float32",gpuBuffer:t,dims:a,download:n,dispose:s})},$=(t,r)=>{let{dataType:i,dims:a,download:n,dispose:s}=r;return new C({location:"ml-tensor",type:i??"float32",mlTensor:t,dims:a,download:n,dispose:s})},b=(t,r,i)=>new C({location:"cpu-pinned",type:t,data:r,dims:i??[r.length]})}),X=W(()=>{v=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),w=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),x=!1,S=()=>
1{if(!x){x=!0;let t="u">typeof BigInt64Array&&BigInt64Array.from,r="u">typeof BigUint64Array&&BigUint64Array.from,i=globalThis.Float16Array,a="u">typeof i&&i.from;t&&(v.set("int64",BigInt64Array),w.set(BigInt64Array,"int64")),r&&(v.set("uint64",BigUint64Array),w.set(BigUint64Array,"uint64")),a?(v.set("float16",i),w.set(i,"float16")):v.set("float16",Uint16Array)}}}),Y=W(()=>{J(),k=t=>{let r=1;for(let i=0;i<t.length;i++){let a=t[i];if("number"!=typeof a||!Number.isSafeInteger(a))throw TypeError(`dims[${i}] must be an integer, got: ${a}`);if(a<0)throw RangeError(`dims[${i}] must be a non-negative integer, got: ${a}`);r*=a}return r},T=(t,r)=>{switch(t.location){case"cpu":return new C(t.type,t.data,r);case"cpu-pinned":return new C({location:"cpu-pinned",data:t.data,type:t.type,dims:r});case"texture":return new C({location:"texture",texture:t.texture,type:t.type,dims:r});case"gpu-buffer":return new C({location:"gpu-buffer",gpuBuffer:t.gpuBuffer,type:t.type,dims:r});case"ml-tensor":return new C({location:"ml-tensor",mlTensor:t.mlTensor,type:t.type,dims:r});default:throw Error(`tensorReshape: tensor location ${t.location} is not supported`)}}}),J=W(()=>{Q(),Z(),X(),Y(),C=class{constructor(t,r,i){let a,n;if(S(),"object"==typeof t&&"location"in t)switch(this.dataLocation=t.location,a=t.type,n=t.dims,t.location){case"cpu-pinned":{let r=v.get(a);if(!r)throw TypeError(`unsupported type "${a}" to create tensor from pinned buffer`);if(!(t.data instanceof r))throw TypeError(`buffer should be of type ${r.name}`);this.cpuData=t.data;break}case"texture":if("float32"!==a)throw TypeError(`unsupported type "${a}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break;case"gpu-buffer":if("float32"!==a&&"float16"!==a&&"int32"!==a&&"int64"!==a&&"uint32"!==a&&"uint8"!==a&&"bool"!==a&&"uint4"!==a&&"int4"!==a)throw TypeError(`unsupported type "${a}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break;case"ml-tensor":if("float32"!==a&&"float16"!==a&&"int32"!==a&&"int64"!==a&&"uint32"!==a&&"uint64"!==a&&"int8"!==a&&"uint8"!==a&&"bool"!==a&&"uint4"!==a&&"int4"!==a)throw TypeError(`unsupported type "${a}" to create tensor from MLTensor`);this.mlTensorData=t.mlTensor,this.downloader=t.download,this.disposer=t.dispose;break;default:throw Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let s,o;if("string"==typeof t){if(a=t,o=i,"string"===t){if(!Array.isArray(r))throw TypeError("A string tensor's data must be a string array.");s=r}else{let i=v.get(t);if(void 0===i)throw TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if("float16"===t&&i===Uint16Array||"uint4"===t||"int4"===t)throw TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${i.name} as data.`);s="uint64"===t||"int64"===t?i.from(r,BigInt):i.from(r)}else if(r instanceof i)s=r;else if(r instanceof Uint8ClampedArray){if("uint8"===t)s=Uint8Array.from(r);else throw TypeError("A Uint8ClampedArray tensor's data must be type of uint8")}else if("float16"===t&&r instanceof Uint16Array&&i!==Uint16Array)s=new globalThis.Float16Array(r.buffer,r.byteOffset,r.length);else throw TypeError(`A ${a} tensor's data must be type of ${i}`)}}else if(o=r,Array.isArray(t)){if(0===t.length)throw TypeError("Tensor type cannot be inferred from an empty array.");let r=typeof t[0];if("string"===r)a="string",s=t;else if("boolean"===r)a="bool",s=Uint8Array.from(t);else throw TypeError(`Invalid element type of data array: ${r}.`)}else if(t instanceof Uint8ClampedArray)a="uint8",s=Uint8Array.from(t);else{let r=w.get(t.constructor);if(void 0===r)throw TypeError(`Unsupported type for tensor data: ${t.constructor}.`);a=r,s=t}if(void 0===o)o=[s.length];else if(!Array.isArray(o))throw TypeError("A tensor's dims must be a number array");n=o,this.cpuData=s,this.dataLocation="cpu"}let s=k(n);if(this.cpuData&&s!==this.cpuData.length&&!(("uint4"===a||"int4"===a)&&Math.ceil(s/2)===this.cpuData.length))throw Error(`Tensor's size(${s}) does not match data length(${this.cpuData.length}).`);this.type=a,this.dims=n,this.size=s}static async fromImage(t,r){return g(t,r)}static fromTexture(t,r){return y(t,r)}static fromGpuBuffer(t,r){return _(t,r)}static fromMLTensor(t,r){return $(t,r)}static fromPinnedBuffer(t,r,i){return b(t,r,i)}toDataURL(t){return h(this,t)}toImageData(t){return f(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":if(!this.downloader)throw Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}default:throw Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw 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("none"===this.dataLocation)throw Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw Error("Cannot reshape a tensor that owns GPU resource.");return T(this,t)}}}),ee=W(()=>{J(),I=C}),et=W(()=>{j(),E=(t,r)=>{(typeof p.trace>"u"?p.wasm.trace:p.trace)&&console.timeStamp(`${t}::ORT::${r}`)},z=(t,r)=>{let i=Error().stack?.split(/\r\n|\r|\n/g)||[],a=!1;for(let n=0;n<i.length;n++){if(a&&!i[n].includes("TRACE_FUNC")){let a=`FUNC_${t}::${i[n].trim().split(" ")[1]}`;r&&(a+=`::${r}`),E("CPU",a);return}i[n].includes("TRACE_FUNC")&&(a=!0)}},A=t=>{(typeof p.trace>"u"?p.wasm.trace:p.trace)&&z("BEGIN",t)},O=t=>{(typeof p.trace>"u"?p.wasm.trace:p.trace)&&z("END",t)}}),er=W(()=>{G(),ee(),et(),R=class t{constructor(t){this.handler=t}async run(t,r,i){A();let a={},n={};if("object"!=typeof t||null===t||t instanceof I||Array.isArray(t))throw TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let s=!0;if("object"==typeof r){if(null===r)throw TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof I)throw TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(0===r.length)throw TypeError("'fetches' cannot be an empty array.");
1for(let t of(s=!1,r)){if("string"!=typeof t)throw TypeError("'fetches' must be a string array or an object.");if(-1===this.outputNames.indexOf(t))throw RangeError(`'fetches' contains invalid output name: ${t}.`);a[t]=null}if("object"==typeof i&&null!==i)n=i;else if("u">typeof i)throw TypeError("'options' must be an object.")}else{let t=!1,o=Object.getOwnPropertyNames(r);for(let i of this.outputNames)if(-1!==o.indexOf(i)){let n=r[i];(null===n||n instanceof I)&&(t=!0,s=!1,a[i]=n)}if(t){if("object"==typeof i&&null!==i)n=i;else if("u">typeof i)throw TypeError("'options' must be an object.")}else n=r}}else if("u">typeof r)throw TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let r of this.inputNames)if(typeof t[r]>"u")throw Error(`input '${r}' is missing in 'feeds'.`);if(s)for(let t of this.outputNames)a[t]=null;let o=await this.handler.run(t,a,n),u={};for(let t in o)if(Object.hasOwnProperty.call(o,t)){let r=o[t];r instanceof I?u[t]=r:u[t]=new I(r.type,r.data,r.dims)}return O(),u}async release(){return this.handler.dispose()}static async create(r,i,a,n){A();let s,o={};if("string"==typeof r){if(s=r,"object"==typeof i&&null!==i)o=i;else if("u">typeof i)throw TypeError("'options' must be an object.")}else if(r instanceof Uint8Array){if(s=r,"object"==typeof i&&null!==i)o=i;else if("u">typeof i)throw TypeError("'options' must be an object.")}else if(r instanceof ArrayBuffer||"u">typeof SharedArrayBuffer&&r instanceof SharedArrayBuffer){let t=0,u=r.byteLength;if("object"==typeof i&&null!==i)o=i;else if("number"==typeof i){if(!Number.isSafeInteger(t=i))throw RangeError("'byteOffset' must be an integer.");if(t<0||t>=r.byteLength)throw RangeError(`'byteOffset' is out of range [0, ${r.byteLength}).`);if(u=r.byteLength-t,"number"==typeof a){if(!Number.isSafeInteger(u=a))throw RangeError("'byteLength' must be an integer.");if(u<=0||t+u>r.byteLength)throw RangeError(`'byteLength' is out of range (0, ${r.byteLength-t}].`);if("object"==typeof n&&null!==n)o=n;else if("u">typeof n)throw TypeError("'options' must be an object.")}else if("u">typeof a)throw TypeError("'byteLength' must be a number.")}else if("u">typeof i)throw TypeError("'options' must be an object.");s=new Uint8Array(r,t,u)}else throw TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[l,d]=await u(o),p=await l.createInferenceSessionHandler(s,d);return O(),new t(p)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}}),ei=W(()=>{er(),B=R}),ea=W(()=>{}),en=W(()=>{}),es=W(()=>{}),eo=W(()=>{}),eu={};q(eu,{InferenceSession:()=>B,TRACE:()=>E,TRACE_FUNC_BEGIN:()=>A,TRACE_FUNC_END:()=>O,Tensor:()=>I,env:()=>c,registerBackend:()=>s});var el=W(()=>{H(),K(),ei(),ee(),ea(),en(),et(),es(),eo()}),ed=W(()=>{}),ep={};q(ep,{default:()=>ef});var ec,eh,ef,em=W(()=>{lb(),oW(),oU(),ec="ort-wasm-proxy-worker",(eh=globalThis.self?.name===ec)&&(self.onmessage=t=>{let{type:r,in:i}=t.data;try{switch(r){case"init-wasm":eP(i.wasm).then(()=>{uX(i).then(()=>{postMessage({type:r})},t=>{postMessage({type:r,err:t})})},t=>{postMessage({type:r,err:t})});break;case"init-ep":{let{epName:t,env:a}=i;uY(a,t).then(()=>{postMessage({type:r})},t=>{postMessage({type:r,err:t})});break}case"copy-from":{let{buffer:t}=i,a=u1(t);postMessage({type:r,out:a});break}case"create":{let{model:t,options:a}=i;u2(t,a).then(t=>{postMessage({type:r,out:t})},t=>{postMessage({type:r,err:t})});break}case"release":u3(i),postMessage({type:r});break;case"run":{let{sessionId:t,inputIndices:a,inputs:n,outputIndices:s,options:o}=i;u6(t,a,n,s,Array(s.length).fill(null),o).then(t=>{t.some(t=>"cpu"!==t[3])?postMessage({type:r,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:r,out:t},u5([...n,...t]))},t=>{postMessage({type:r,err:t})});break}case"end-profiling":u8(i),postMessage({type:r})}}catch(t){postMessage({type:r,err:t})}}),ef=eh?null:t=>new Worker(t??ev,{type:"module",name:ec})}),eg={};q(eg,{default:()=>e$});var ey,e_,e$,eb,ev,ew,ex,eS,ek,eT,eC,eI,eE,ez,eA,eO,eR,eB,eN,eM,eD,eP,eU,eW,eq,eL,eV,eG,eH,eF,ej,eK,eQ,eZ,eX,eY,eJ,e0,e1,e2,e3,e4,e6,e8,e5,e9,e7,te,tt,tr,ti,ta,tn,ts,to,tu,tl,td,tp,tc,th,tf,tm,tg,ty,t_,t$,tb,tv,tw,tx,tS,tk,tT,tC,tI,tE,tz,tA,tO,tR,tB,tN,tM,tD,tP,tU,tW,tq,tL,tV,tG,tH,tF,tj,tK,tQ,tZ,tX,tY,tJ,t0,t1,t2,t3,t4,t6,t8,t5,t9,t7,re,rt,rr,ri,ra,rn,rs,ro,ru,rl,rd,rp,rc,rh,rf,rm,rg,ry,r_,r$,rb,rv,rw,rx,rS,rk,rT,rC,rI,rE,rz,rA,rO,rR,rB,rN,rM,rD,rP,rU,rW,rq,rL,rV,rG,rH,rF,rj,rK,rQ,rZ,rX,rY,rJ,r0,r1,r2,r3,r4,r6,r8,r5,r9,r7,ie,it,ir,ii,ia,is,io,iu,il,id,ip,ic,ih,im,ig,iy,i_,i$,ib,iv,iw,ix,iS,ik,iT,iC,iI,iE,iz,iA,iO,iR,iB,iN,iM,iD,iP,iU,iW,iq,iL,iV,iG,iH,iF,ij,iK,iQ,iZ,iX,iY,iJ,i0,i1,i2,i3,i4,i6,i8,i5,i9,i7,ae,at,ar,ai,aa,an,as,ao,au,al,ad,ap,ac,ah,af,am,ag,ay,a_,a$,ab,av,aw,ax,aS,ak,aT,aC,aI,aE,az,aA,aO,aR,aB,aN,aM,aD,aP,aU,aW,aq,aL,aV,aG,aH,aF,aj,aK,aQ,aZ,aX,aY,aJ,a0,a1,a2,a3,a4,a6,a8,a5,a9,a7,ne,nt,nr,ni,na,nn,ns,no,nu,nl,nd,np,nc,nh,nf,nm,ng,ny,n_,n$,nb,nv,nw,nx,nS,nk,nT,nC,nI,nE,nz,nA,nO,nR,nB,nN,nM,nD,nP,nU,nW,nq,nL,nV,nG,nH,nF,nj,nK,nQ,nZ,nX,nY,nJ,n0,n1,n2,n3,n4,n6,n8,n5,n9,n7,se,st,sr,si,sa,sn,ss,so,su,sl,sd,sp,sc,sh,sf,sm,sg,sy,s_,s$,sb,sv,sw,sx,sS,sk,sT,sC,sI,sE,sz,sA,sO,sR,s
1B,sN,sM,sD,sP,sU,sW,sq,sL,sV,sG,sH,sF,sj,sK,sQ,sZ,sX,sY,sJ,s0,s1,s2,s3,s4,s6,s8,s5,s9,s7,oe,ot,or,oi,oa,on,os,oo,ou,ol,od,op,oc,oh,of,om,og,oy,o_,o$,ob,ov,ow,ox,oS,ok,oT,oC,oI,oE,oz,oA,oO,oR,oB,oN,oM,oD,oP=W(()=>{ey="file:///vercel/path0/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs",e$=e_=async function(t={}){var r,a,n=new Promise((t,i)=>{r=t,a=i}),s="object"==typeof window,o="u">typeof WorkerGlobalScope,u=o&&self.name?.startsWith("em-pthread");t.mountExternalData=(r,i)=>{r.startsWith("./")&&(r=r.substring(2)),(t.Bd||(t.Bd=new Map)).set(r,i)},t.unmountExternalData=()=>{delete t.Bd};var l=globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let d=()=>{let r=(t,r,i)=>(...a)=>{let n=tq,s=r?.();a=t(...a);let o=r?.();return s!==o&&(t=o,i(s),r=i=null),tq!=n?new Promise((t,r)=>{tj={resolve:t,reject:r}}):a},i=r=>async(...i)=>{try{if(t.Cd)throw Error("Session already started");let a=t.Cd={be:i[0],errors:[]},n=await r(...i);if(t.Cd!==a)throw Error("Session mismatch");t.Dd?.flush();let s=a.errors;if(0<s.length){let t=await Promise.all(s);if(t=t.filter(t=>t),0<t.length)throw Error(t.join(`
2`))}return n}finally{t.Cd=null}};t._OrtCreateSession=r(t._OrtCreateSession,()=>t._OrtCreateSession,r=>t._OrtCreateSession=r),t._OrtRun=i(r(t._OrtRun,()=>t._OrtRun,r=>t._OrtRun=r)),t._OrtRunWithBinding=i(r(t._OrtRunWithBinding,()=>t._OrtRunWithBinding,r=>t._OrtRunWithBinding=r)),t._OrtBindInput=r(t._OrtBindInput,()=>t._OrtBindInput,r=>t._OrtBindInput=r),d=void 0};t.jsepInit=(r,i)=>{if(d?.(),"webgpu"===r){[t.Dd,t.Rd,t.Vd,t.Hd,t.Ud,t.hc,t.Wd,t.Zd,t.Sd,t.Td,t.Xd]=i;let r=t.Dd;t.jsepRegisterBuffer=(t,i,a,n)=>r.registerBuffer(t,i,a,n),t.jsepGetBuffer=t=>r.getBuffer(t),t.jsepCreateDownloader=(t,i,a)=>r.createDownloader(t,i,a),t.jsepOnCreateSession=t=>{r.onCreateSession(t)},t.jsepOnReleaseSession=t=>{r.onReleaseSession(t)},t.jsepOnRunStart=t=>r.onRunStart(t),t.$d=(t,i)=>{r.upload(t,i)}}else if("webnn"===r){[t.Dd,t.Yd,t.Id,t.jsepEnsureTensor,t.Jd,t.jsepDownloadTensor]=i,t.jsepReleaseTensorId=t.Id,t.jsepUploadTensor=t.Jd;let r=t.Dd;t.jsepOnRunStart=t=>r.onRunStart(t),t.jsepOnRunEnd=r.onRunEnd.bind(r),t.jsepRegisterMLContext=(t,i)=>{r.registerMLContext(t,i)},t.jsepOnReleaseSession=t=>{r.onReleaseSession(t)},t.jsepCreateMLTensorDownloader=(t,i)=>r.createMLTensorDownloader(t,i),t.jsepRegisterMLTensor=(t,i,a,n)=>r.registerMLTensor(t,i,a,n),t.jsepCreateMLContext=t=>r.createMLContext(t),t.jsepRegisterMLConstant=(i,a,n,s,o)=>r.registerMLConstant(i,a,n,s,o,t.Bd),t.jsepRegisterGraphInput=r.registerGraphInput.bind(r),t.jsepIsGraphInput=r.isGraphInput.bind(r),t.jsepCreateTemporaryTensor=r.createTemporaryTensor.bind(r)}};var p,c,h=Object.assign({},t),f=(t,r)=>{throw r},m="";(s||o)&&(o?m=self.location.href:"u">typeof document&&document.currentScript&&(m=document.currentScript.src),ey&&(m=ey),m=m.startsWith("blob:")?"":m.slice(0,m.replace(/[?#].*/,"").lastIndexOf("/")+1),o&&(c=t=>{var r=new XMLHttpRequest;return r.open("GET",t,!1),r.responseType="arraybuffer",r.send(null),new Uint8Array(r.response)}),p=async t=>{if(N(t))return new Promise((r,i)=>{var a=new XMLHttpRequest;a.open("GET",t,!0),a.responseType="arraybuffer",a.onload=()=>{200==a.status||0==a.status&&a.response?r(a.response):i(a.status)},a.onerror=i,a.send(null)});var r=await fetch(t,{credentials:"same-origin"});if(r.ok)return r.arrayBuffer();throw Error(r.status+" : "+r.url)});var g=console.log.bind(console),y=console.error.bind(console),_=g,$=y;Object.assign(t,h),h=null;var b,v,w,x,S,k,T,C,I,E,z,A,O,R=t.wasmBinary,B=!1,N=t=>t.startsWith("file://");function M(){return b.buffer!=x.buffer&&F(),x}function D(){return b.buffer!=x.buffer&&F(),S}function P(){return b.buffer!=x.buffer&&F(),k}function U(){return b.buffer!=x.buffer&&F(),T}function W(){return b.buffer!=x.buffer&&F(),C}function q(){return b.buffer!=x.buffer&&F(),I}function L(){return b.buffer!=x.buffer&&F(),E}function V(){return b.buffer!=x.buffer&&F(),O}if(u){let r=function(i){try{var a=i.data,n=a.yd;if("load"===n){let i=[];for(let n of(self.onmessage=t=>i.push(t),self.startWorker=()=>{for(let t of(postMessage({yd:"loaded"}),i))r(t);self.onmessage=r},a.Od))t[n]&&!t[n].proxy||(t[n]=(...t)=>{postMessage({yd:"callHandler",Nd:n,args:t})},"print"==n&&(_=t[n]),"printErr"==n&&($=t[n]));b=a.he,F(),G(a.ie)}else if("run"===n){ev(a.xd),r4(a.xd,0,0,1,0,0),e_(),tC(a.xd),H||(r0(),H=!0);try{ew(a.de,a.Fd)}catch(t){if("unwind"!=t)throw t}}else"setimmediate"!==a.target&&("checkMailbox"===n?H&&tI():n&&($(`worker: received unknown command ${n}`),$(a)))}catch(t){throw r6(),t}};var G,H=!1;$=function(...t){console.error(t=t.join(" "))},self.alert=function(...t){postMessage({yd:"alert",text:t.join(" "),fe:r1()})},self.onunhandledrejection=t=>{throw t.reason||t},self.onmessage=r}function F(){var r=b.buffer;t.HEAP8=x=new Int8Array(r),t.HEAP16=k=new Int16Array(r),t.HEAPU8=S=new Uint8Array(r),t.HEAPU16=T=new Uint16Array(r),t.HEAP32=C=new Int32Array(r),t.HEAPU32
vendor: 14,452 bytes, line 2
2=I=new Uint32Array(r),t.HEAPF32=E=new Float32Array(r),t.HEAPF64=O=new Float64Array(r),t.HEAP64=z=new BigInt64Array(r),t.HEAPU64=A=new BigUint64Array(r)}function j(){u?startWorker(t):rX.Bb()}u||(b=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),F());var K,Q=0,Z=null;function X(){if(0==--Q&&Z){var t=Z;Z=null,t()}}function Y(t){throw $(t="Aborted("+t+")"),B=!0,t=new WebAssembly.RuntimeError(t+". Build with -sASSERTIONS for more info."),a(t),t}function J(){return{a:{Ta:er,Va:et,W:eS,la:eT,b:ez,u:eA,R:eO,Za:eR,d:eB,pb:eM,g:eI,T:eW,Ga:eq,lb:eG,nb:eH,Ha:eF,Ea:ej,wb:eK,Da:eQ,pa:eZ,mb:eX,jb:eY,Fa:eJ,kb:e0,Ma:e3,za:te,eb:tr,cb:td,ya:tc,V:th,N:tf,db:tm,ma:tx,fb:tS,zb:tk,hb:tE,qb:tA,ab:tO,Aa:tR,yb:tC,Ja:tB,S:tD,Wa:tP,$:tZ,G:tY,E:t1,m:tn,H:t3,B:t5,X:t9,J:t7,v:re,O:rt,D:rr,t:ri,A:ra,z:rn,w:rs,r:ro,tb:ru,ub:rc,vb:rh,rb:rf,sb:rm,bb:r_,Oa:r$,La:rv,y:rS,ja:rk,Ba:rT,Ka:rb,qa:rC,Ia:rI,ib:rE,U:ry,fa:rz,Sa:rA,gb:rO,Qa:rM,Pa:rD,Ab:rq,Ca:rL,ob:ep,aa:rV,oa:rG,xb:rH,na:rj,$a:av,ia:aB,sa:aU,ga:a$,da:aC,ua:aD,p:ay,e:at,c:i7,ea:ak,f:ar,n:aa,k:ah,Y:as,ka:af,j:a_,wa:aS,Ra:aL,ca:aO,Ua:aq,P:aT,K:au,_:aA,Q:ab,Z:aN,x:ao,l:ae,va:az,i:i9,h:an,ra:aW,ta:aP,o:ai,q:al,s:ap,I:ac,C:ag,L:am,xa:ax,_a:aw,F:aR,Ya:aI,ba:aM,M:ad,Xa:aE,ha:rK,a:b,Na:el}}}var ee={1319426:()=>"u">typeof wasmOffsetConverter,1319483:(r,i,a,n,s)=>{if(void 0===t||!t.Bd)return 1;if((r=eU(Number(r>>>0))).startsWith("./")&&(r=r.substring(2)),!(r=t.Bd.get(r)))return 2;if(i=Number(i>>>0),a=Number(a>>>0),n=Number(n>>>0),i+a>r.byteLength)return 3;try{let o=r.subarray(i,i+a);switch(s){case 0:D().set(o,n>>>0);break;case 1:t.$d(n,o);break;default:return 4}return 0}catch{return 4}},1320198:(r,i,a)=>{t.Jd(r,D().subarray(i>>>0,i+a>>>0))},1320261:()=>t.Yd(),1320302:r=>{t.Id(r)},1320338:()=>{t.Sd()},1320369:()=>{t.Td()},1320398:()=>{t.Xd()},1320423:r=>t.Rd(r),1320456:r=>t.Vd(r),1320488:(r,i,a)=>{t.Hd(Number(r),Number(i),Number(a),!0)},1320551:(r,i,a)=>{t.Hd(Number(r),Number(i),Number(a))},1320608:r=>{t.hc("Abs",r,void 0)},1320659:r=>{t.hc("Neg",r,void 0)},1320710:r=>{t.hc("Floor",r,void 0)},1320763:r=>{t.hc("Ceil",r,void 0)},1320815:r=>{t.hc("Reciprocal",r,void 0)},1320873:r=>{t.hc("Sqrt",r,void 0)},1320925:r=>{t.hc("Exp",r,void 0)},1320976:r=>{t.hc("Erf",r,void 0)},1321027:r=>{t.hc("Sigmoid",r,void 0)},1321082:(r,i,a)=>{t.hc("HardSigmoid",r,{alpha:i,beta:a})},1321161:r=>{t.hc("Log",r,void 0)},1321212:r=>{t.hc("Sin",r,void 0)},1321263:r=>{t.hc("Cos",r,void 0)},1321314:r=>{t.hc("Tan",r,void 0)},1321365:r=>{t.hc("Asin",r,void 0)},1321417:r=>{t.hc("Acos",r,void 0)},1321469:r=>{t.hc("Atan",r,void 0)},1321521:r=>{t.hc("Sinh",r,void 0)},1321573:r=>{t.hc("Cosh",r,void 0)},1321625:r=>{t.hc("Asinh",r,void 0)},1321678:r=>{t.hc("Acosh",r,void 0)},1321731:r=>{t.hc("Atanh",r,void 0)},1321784:r=>{t.hc("Tanh",r,void 0)},1321836:r=>{t.hc("Not",r,void 0)},1321887:(r,i,a)=>{t.hc("Clip",r,{min:i,max:a})},1321956:r=>{t.hc("Clip",r,void 0)},1322008:(r,i)=>{t.hc("Elu",r,{alpha:i})},1322066:r=>{t.hc("Gelu",r,void 0)},1322118:r=>{t.hc("Relu",r,void 0)},1322170:(r,i)=>{t.hc("LeakyRelu",r,{alpha:i})},1322234:(r,i)=>{t.hc("ThresholdedRelu",r,{alpha:i})},1322304:(r,i)=>{t.hc("Cast",r,{to:i})},1322362:r=>{t.hc("Add",r,void 0)},1322413:r=>{t.hc("Sub",r,void 0)},1322464:r=>{t.hc("Mul",r,void 0)},1322515:r=>{t.hc("Div",r,void 0)},1322566:r=>{t.hc("Pow",r,void 0)},1322617:r=>{t.hc("Equal",r,void 0)},1322670:r=>{t.hc("Greater",r,void 0)},1322725:r=>{t.hc("GreaterOrEqual",r,void 0)},1322787:r=>{t.hc("Less",r,void 0)},1322839:r=>{t.hc("LessOrEqual",r,void 0)},1322898:(r,i,a,n,s)=>{t.hc("ReduceMean",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1323073:(r,i,a,n,s)=>{t.hc("ReduceMax",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1323247:(r,i,a,n,s)=>{t.hc("ReduceMin",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1323421:(r,i,a,n,s)=>{t.hc("ReduceProd",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1323596:(r,i,a,n,s)=>{t.hc("ReduceSum",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1323770:(r,i,a,n,s)=>{t.hc("ReduceL1",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1323943:(r,i,a,n,s)=>{t.hc("ReduceL2",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1324116:(r,i,a,n,s)=>{t.hc("ReduceLogSum",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1324293:(r,i,a,n,s)=>{t.hc("ReduceSumSquare",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1324473:(r,i,a,n,s)=>{t.hc("ReduceLogSumExp",r,{keepDims:!!i,noopWithEmptyAxes:!!a,axes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1324653:r=>{t.hc("Where",r,void 0)},1324706:(r,i,a)=>{t.hc("Transpose",r,{perm:i?Array.from(W().subarray(Number(i)>>>0,Number(a)>>>0)):[]})},1324830:(r,i,a,n)=>{t.hc("DepthToSpace",r,{blocksize:i,mode:eU(a),format:n?"NHWC":"NCHW"})},1324963:(r,i,a,n)=>{t.hc("DepthToSpace",r,{blocksize:i,mode:eU(a),format:n?"NHWC":"NCHW"})},1325096:(r,i,a,n,s,o,u,l,d,p,c,h,f,m,g)=>{t.hc("ConvTranspose",r,{format:d?"NHWC":"NCHW",autoPad:i,dilations:[a],group:n,kernelShape:[s],pads:[o,u],strides:[l],wIsConst:()=>!!M()[p>>>0],outputPadding:c?Array.from(W().subarray(Number(c)>>>0,Number(h)>>>0)):[],outputShape:f?Array.from(W().subarray(Number(f)>>>0,Number(m)>>>0)):[],activation:eU(g)})},1325529:(r,i,a,n,s,o,u,l,d,p,c,h,f,m)=>{t.hc("ConvTranspose",r,{format:l?"NHWC":"NCHW",autoPad:i,dilations:Array.from(W().subarray(Number(a)>>>0,2+(Number(a)>>>0)>>>0)),group:n,kernelShape:Array.from(W().subarray(Number(s)>>>0,2+(Number(s)>>>0)>>>0)),pads:Array.from(W().subarray(Number(o)>>>0,4+(Number(o)>>>0)>>>0)),strides:Array.from(W().subarray(Number(u)>>>0,2+(Number(u)>>>0)>>>0)),wIsConst:()=>!!M()[d>>>0],outputPadding:p?Array.from(W().subarray(Number(p)>>>0,Number(c)>>>0)):[],outputShape:h?Array.from(W().subarray(Number(h)>>>0,Number(f)>>>0)):[],activation:eU(m)})},1326190:(r,i,a,n,s,o,u,l,d,p,c,h,f,m,g)=>{t.hc("ConvTranspose",r,{format:d?"NHWC":"NCHW",autoPad:i,dilations:[a],group:n,kernelShape:[s],pads:[o,u],strides:[l],wIsConst:()=>!!M()[p>>>0],outputPadding:c?Array.from(W().subarray(Number(c)>>>0,Number(h)>>>0)):[],outputShape:f?Array.from(W().subarray(Number(f)>>>0,Number(m)>>>0)):[],activation:eU(g)})},1326623:(r,i,a,n,s,o,u,l,d,p,c,h,f,m)=>{t.hc("ConvTranspose",r,{format:l?"NHWC":"NCHW",autoPad:i,dilations:Array.from(W().subarray(Number(a)>>>0,2+(Number(a)>>>0)>>>0)),group:n,kernelShape:Array.from(W().subarray(Number(s)>>>0,2+(Number(s)>>>0)>>>0)),pads:Array.from(W().subarray(Number(o)>>>0,4+(Number(o)>>>0)>>>0)),strides:Array.from(W().subarray(Number(u)>>>0,2+(Number(u)>>>0)>>>0)),wIsConst:()=>!!M()[d>>>0],outputPadding:p?Array.from(W().subarray(Number(p)>>>0,Number(c)>>>0)):[],outputShape:h?Array.from(W().subarray(Number(h)>>>0,Number(f)>>>0)):[],activation:eU(m)})},1327284:(r,i)=>{t.hc("GlobalAveragePool",r,{format:i?"NHWC":"NCHW"})},1327375:(r,i,a,n,s,o,u,l,d,p,c,h,f,m)=>{t.hc("AveragePool",r,{format:m?"NHWC":"NCHW",auto_pad:i,ceil_mode:a,count_include_pad:n,storage_order:s,dilations:o?Array.from(W().subarray(Number(o)>>>0,Number(u)>>>0)):[],kernel_shape:l?Array.from(W().subarray(Number(l)>>>0,Number(d)>>>0)):[],pads:p?Array.from(W().subarray(Number(p)>>>0,Number(c)>>>0)):[],strides:h?Array.from(W().subarray(Number(h)>>>0,Number(f)>>>0)):[]})},1327854:(r,i)=>{t.hc("GlobalAveragePool",r,{format:i?"NHWC":"NCHW"})},1327945:(r,i,a,n,s,o,u,l,d,p,c,h,f,m)=>{t.hc("AveragePool",r,{format:m?"NHWC":"NCHW",auto_pad:i,ceil_mode:a,count_include_pad:n,storage_order:s,dilations:o?Array.from(W().subarray(Number(o)>>>0,Number(u)>>>0)):[],kernel_shape:l?Array.from(W().subarray(Number(l)>>>0,Number(d)>>>0)):[],pads:p?Array.from(W().subarray(Number(p)>>>0,Number(c)>>>0)):[],strides:h?Array.from(W().subarray(Number(h)>>>0,Number(f)>>>0)):[]})},1328424:(r,i)=>{t.hc("GlobalMaxPool",r,{format:i?"NHWC":"NCHW"})},1328511:(r,i,a,n,s,o,u,l,d,p,c,h,f,m)=>{t.hc("MaxPool",r,{format:m?"NHWC":"NCHW",auto_pad:i,ceil_mode:a,count_include_pad:n,storage_order:s,dilations:o?Array.from(W().subarray(Number(o)>>>0,Number(u)>>>0)):[],kernel_shape:l?Array.from(W().subarray(Number(l)>>>0,Number(d)>>>0)):[],pads:p?Array.from(W().subarray(Number(p)>>>0,Number(c)>>>0)):[],strides:h?Array.from(W().subarray(Number(h)>>>0,Number(f)>>>0)):[]})},1328986:(r,i)=>{t.hc("GlobalMaxPool",r,{format:i?"NHWC":"NCHW"})},1329073:(r,i,a,n,s,o,u,l,d,p,c,h,f,m)=>{t.hc("MaxPool",r,{format:m?"NHWC":"NCHW",auto_pad:i,ceil_mode:a,count_include_pad:n,storage_order:s,dilations:o?Array.from(W().subarray(Number(o)>>>0,Number(u)>>>0)):[],kernel_shape:l?Array.from(W().subarray(Number(l)>>>0,Number(d)>>>0)):[],pads:p?Array.from(W().subarray(Number(p)>>>0,Number(c)>>>0)):[],strides:h?Array.from(W().subarray(Number(h)>>>0,Number(f)>>>0)):[]})},1329548:(r,i,a,n,s)=>{t.hc("Gemm",r,{alpha:i,beta:a,transA:n,transB:s})},1329652:r=>{t.hc("MatMul",r,void 0)},1329706:(r,i,a,n)=>{t.hc("ArgMax",r,{keepDims:!!i,selectLastIndex:!!a,axis:n})},1329814:(r,i,a,n)=>{t.hc("ArgMin",r,{keepDims:!!i,selectLastIndex:!!a,axis:n})},1329922:(r,i)=>{t.hc("Softmax",r,{axis:i})},1329985:(r,i)=>{t.hc("Concat",r,{axis:i})},1330045:(r,i,a,n,s)=>{t.hc("Split",r,{axis:i,numOutputs:a,splitSizes:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1330201:r=>{t.hc("Expand",r,void 0)},1330255:(r,i)=>{t.hc("Gather",r,{axis:Number(i)})},1330326:(r,i)=>{t.hc("GatherElements",r,{axis:Number(i)})},1330405:(r,i)=>{t.hc("GatherND",r,{batch_dims:Number(i)})},1330484:(r,i,a,n,s,o,u,l,d,p,c)=>{t.hc("Resize",r,{antialias:i,axes:a?Array.from(W().subarray(Number(a)>>>0,Number(n)>>>0)):[],coordinateTransformMode:eU(s),cubicCoeffA:o,excludeOutside:u,extrapolationValue:l,keepAspectRatioPolicy:eU(d),mode:eU(p),nearestMode:eU(c)})},1330846:(r,i,a,n,s,o,u)=>{t.hc("Slice",r,{starts:i?Array.from(W().subarray(Number(i)>>>0,Number(a)>>>0)):[],ends:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[],axes:o?Array.from(W().subarray(Number(o)>>>0,Number(u)>>>0)):[]})},1331110:r=>{t.hc("Tile",r,void 0)},1331162:(r,i,a)=>{t.hc("InstanceNormalization",r,{epsilon:i,format:a?"NHWC":"NCHW"})},1331276:(r,i,a)=>{t.hc("InstanceNormalization",r,{epsilon:i,format:a?"NHWC":"NCHW"})},1331390:r=>{t.hc("Range",r,void 0)},1331443:(r,i)=>{t.hc("Einsum",r,{equation:eU(i)})},1331524:(r,i,a,n,s)=>{t.hc("Pad",r,{mode:i,value:a,pads:n?Array.from(W().subarray(Number(n)>>>0,Number(s)>>>0)):[]})},1331667:(r,i,a,n,s,o)=>{t.hc("BatchNormalization",r,{epsilon:i,momentum:a,spatial:!!s,trainingMode:!!n,format:o?"NHWC":"NCHW"})},1331836:(r,i,a,n,s,o)=>{t.hc("BatchNormalization",r,{epsilon:i,momentum:a,spatial:!!s,trainingMode:!!n,format:o?"NHWC":"NCHW"})},1332005:(r,i,a)=>{t.hc("CumSum",r,{exclusive:Number(i),reverse:Number(a)})},1332102:(r,i,a)=>{t.hc("DequantizeLinear",r,{axis:i,blockSize:a})},1332192:(r,i,a,n,s)=>{t.hc("GridSample",r,{align_corners:i,mode:eU(a),padding_mode:eU(n),format:s?"NHWC":"NCHW"})},1332362:(r,i,a,n,s)=>{t.hc("GridSample",r,{align_corners:i,mode:eU(a),padding_mode:eU(n),format:s?"NHWC":"NCHW"})},1332532:(r,i)=>{t.hc("ScatterND",r,{reduction:eU(i)})},1332617:(r,i,a,n,s,o,u,l,d)=>{t.hc("Attention",r,{numHeads:i,isUnidirectional:a,maskFilterValue:n,scale:s,doRotary:o,qkvHiddenSizes:u?Array.from(W().subarray(Number(l)>>>0,Number(l)+u>>>0)):[],pastPresentShareBuffer:!!d})},1332889:r=>{t.hc("BiasAdd",r,void 0)},1332944:r=>{t.hc("BiasSplitGelu",r,void 0)},1333005:r=>{t.hc("FastGelu",r,void 0)},1333061:(r,i,a,n,s,o,u,l,d,p,c,h,f,m,g,y)=>{t.hc("Conv",r,{format:h?"NHWC":"NCHW",auto_pad:i,dilations:a?Array.from(W().subarray(Number(a)>>>0,Number(n)>>>0)):[],group:s,kernel_shape:o?Array.from(W().subarray(Number(o)>>>0,Number(u)>>>0)):[],pads:l?Array.from(W().subarray(Number(l)>>>0,Number(d)>>>0)):[],strides:p?Array.from(W().subarray(Number(p)>>>0,Number(c)>>>0)):[],w_is_const:()=>!!M()[Number(f)>>>0],activation:eU(m),activation_params:g?Array.from(L().subarray(Number(g)>>>0,Number(y)>>>0)):[]})},1333645:r=>{t.hc("Gelu",r,void 0)},1333697:(r,i,a,n,s,o,u,l,d)=>{t.hc("GroupQueryAttention",r,{numHeads:i,kvNumHeads:a,scale:n,softcap:s,doRotary:o,rotaryInterleaved:u,smoothSoftmax:l,localWindowSize:d})},1333914:(r,i,a,n)=>{t.hc("LayerNormalization",r,{axis:i,epsilon:a,simplified:!!n})},1334025:(r,i,a,n)=>{t.hc("LayerNormalization",r,{axis:i,epsilon:a,simplified:!!n})},1334136:(r,i,a,n,s,o)=>{t.hc("MatMulNBits",r,{k:i,n:a,accuracyLevel:n,bits:s,blockSize:o})},1334263:(r,i,a,n,s,o)=>{t.hc("MultiHeadAttention",r,{numHeads:i,isUnidirectional:a,maskFilterValue:n,scale:s,doRotary:o})},1334422:(r,i)=>{t.hc("QuickGelu",r,{alpha:i})},1334486:(r,i,a,n,s)=>{t.hc("RotaryEmbedding",r,{interleaved:!!i,numHeads:a,rotaryEmbeddingDim:n,scale:s})},1334625:(r,i,a)=>{t.hc("SkipLayerNormalization",r,{epsilon:i,simplified:!!a})},1334727:(r,i,a)=>{t.hc("SkipLayerNormalization",r,{epsilon:i,simplified:!!a})},1334829:(r,i,a,n)=>{t.hc("GatherBlockQuantized",r,{gatherAxis:i,quantizeAxis:a,blockSize:n})},1334950:r=>{t.Wd(r)},1334984:(r,i)=>t.Zd(Number(r),Number(i),t.Cd.be,t.Cd.errors)};function et(r,i,a){return tQ(async()=>{await t.Ud(Number(r),Number(i),Number(a))})}function er(){return"u">typeof wasmOffsetConverter}class ei{name="ExitStatus";constructor(t){this.message=`Program terminated with exit(${t})`,this.status=t}}var ea=t=>{t.terminate(),t.onmessage=()=>{}},en=[],es=t=>{0==ec.length&&(eb(),e$(ec[0]));var r=ec.pop();if(!r)return 6;eh.push(r),em[t.xd]=r,r.xd=t.xd;var i={yd:"run",de:t.ce,Fd:t.Fd,xd:t.xd};return r.postMessage(i,t.Ld),0},eo=0,eu=(t,r,...i)=>{for(var a=2*i.length,n=io(),s=is(8*a),o=s>>>3,u=0;u<i.length;u++){var l=i[u];"bigint"==typeof l?(z[o+2*u]=1n,z[o+2*u+1]=l):(z[o+2*u]=0n,V()[o+2*u+1>>>0]=l)}return t=r8(t,0,a,s,r),ia(n),t};function el(t){if(u)return eu(0,1,t);if(w=t,!(0<eo)){for(var r of eh)ea(r);for(r of ec)ea(r);ec=[],eh=[],em={},B=!0}f(0,new ei(t))}function ed(t){if(u)return eu(1,0,t);ep(t)}var ep=t=>{if(w=t,u)throw ed(t),"unwind";el(t)},ec=[],eh=[],ef=[],em={},eg=t=>{var r=t.xd;delete em[r],ec.push(t),eh.splice(eh.indexOf(t),1),t.xd=0,r5(r)};function e_(){ef.forEach(t=>t())}var e$=r=>new Promise(i=>{r.onmessage=a=>{var n=(a=a.data).yd;if(a.Ed&&a.Ed!=r1()){var s=em[a.Ed];
2s?s.postMessage(a,a.Ld):$(`Internal error! Worker sent a message "${n}" to target pthread ${a.Ed}, but that thread no longer exists!`)}else"checkMailbox"===n?tI():"spawnThread"===n?es(a):"cleanupThread"===n?eg(em[a.ee]):"loaded"===n?(r.loaded=!0,i(r)):"alert"===n?alert(`Thread ${a.fe}: ${a.text}`):"setimmediate"===a.target?r.postMessage(a):"callHandler"===n?t[a.Nd](...a.args):n&&$(`worker sent an unknown command ${n}`)},r.onerror=t=>{throw $(`worker sent an error! ${t.filename}:${t.lineno}: ${t.message}`),t};var a,n=[];for(a of[])t.propertyIsEnumerable(a)&&n.push(a);r.postMessage({yd:"load",Od:n,he:b,ie:v})});function eb(){var t=new Worker("file:///vercel/path0/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs".startsWith("file:")?new i.U(i(42968)):new URL("file:///vercel/path0/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs"),{type:"module",workerData:"em-pthread",name:"em-pthread"});ec.push(t)}var ev=t=>{F();var r=q()[t+52>>>2>>>0];t=q()[t+56>>>2>>>0],ii(r,r-t),ia(r)},ew=(t,r)=>{eo=0,t=im(t,r),0<eo?w=t:r9(t)},ex=[];function eS(t){var r=new eC(t>>>=0);if(0==M()[r.wd+12>>>0]){var i=1;M()[r.wd+12>>>0]=i}return i=0,M()[r.wd+13>>>0]=i,ex.push(r),il(t),ip(t)}var ek=0,eT=()=>{it(0,0);var t=ex.pop();iu(t.Gd),ek=0};class eC{constructor(t){this.Gd=t,this.wd=t-24}}function eI(t){throw ek||=t>>>0}var eE=t=>{var r=ek;if(!r)return ir(0),0;var i=new eC(r);q()[i.wd+16>>>2>>>0]=r;var a=q()[i.wd+4>>>2>>>0];if(!a)return ir(0),r;for(var n of t){if(0===n||n===a)break;if(id(n,a,i.wd+16))return ir(n),r}return ir(a),r};function ez(){return eE([])}function eA(t){return eE([t>>>0])}function eO(t,r){return eE([t>>>0,r>>>0])}var eR=()=>{var t=ex.pop();t||Y("no exception to throw");var r=t.Gd;if(0==M()[t.wd+13>>>0]){ex.push(t);var i=1;M()[t.wd+13>>>0]=i,i=0,M()[t.wd+12>>>0]=i}throw ek=r};function eB(t,r,i){var a=new eC(t>>>=0);throw r>>>=0,i>>>=0,q()[a.wd+16>>>2>>>0]=0,q()[a.wd+4>>>2>>>0]=r,q()[a.wd+8>>>2>>>0]=i,ek=t}function eN(t,r,i,a){return u?eu(2,1,t,r,i,a):eM(t,r,i,a)}function eM(t,r,i,a){if(t>>>=0,i>>>=0,a>>>=0,void 0===l)return 6;var n=[];return u&&0===n.length?eN(t,r>>>=0,i,a):(t={ce:i,xd:t,Fd:a,Ld:n},u?(t.yd="spawnThread",postMessage(t,n),0):es(t))}var eD="u">typeof TextDecoder?new TextDecoder:void 0,eP=(t,r=0,i=NaN)=>{var a=(r>>>=0)+i;for(i=r;t[i]&&!(i>=a);)++i;if(16<i-r&&t.buffer&&eD)return eD.decode(t.buffer instanceof ArrayBuffer?t.subarray(r,i):t.slice(r,i));for(a="";r<i;){var n=t[r++];if(128&n){var s=63&t[r++];if((224&n)==192)a+=String.fromCharCode((31&n)<<6|s);else{var o=63&t[r++];65536>(n=(240&n)==224?(15&n)<<12|s<<6|o:(7&n)<<18|s<<12|o<<6|63&t[r++])?a+=String.fromCharCode(n):(n-=65536,a+=String.fromCharCode(55296|n>>10,56320|1023&n))}}else a+=String.fromCharCode(n)}return a},eU=(t,r)=>(t>>>=0)?eP(D(),t,r):"";function eW(t,r,i){return u?eu(3,1,t,r,i):0}function eq(t,r){if(u)return eu(4,1,t,r)}var eL=t=>{for(var r=0,i=0;i<t.length;++i){var a=t.charCodeAt(i);127>=a?r++:2047>=a?r+=2:55296<=a&&57343>=a?(r+=4,++i):r+=3}return r},eV=(t,r,i)=>{var a=D();if(r>>>=0,0<i){var n=r;i=r+i-1;for(var s=0;s<t.length;++s){var o=t.charCodeAt(s);if(55296<=o&&57343>=o&&(o=65536+((1023&o)<<10)|1023&t.charCodeAt(++s)),127>=o){if(r>=i)break;a[r++>>>0]=o}else{if(2047>=o){if(r+1>=i)break;a[r++>>>0]=192|o>>6}else{if(65535>=o){if(r+2>=i)break;a[r++>>>0]=224|o>>12}else{if(r+3>=i)break;a[r++>>>0]=240|o>>18,a[r++>>>0]=128|o>>12&63}a[r++>>>0]=128|o>>6&63}a[r++>>>0]=128|63&o}}a[r>>>0]=0,t=r-n}else t=0;return t};function eG(t,r){if(u)return eu(5,1,t,r)}function eH(t,r,i){if(u)return eu(6,1,t,r,i)}function eF(t,r,i){return u?eu(7,1,t,r,i):0}function ej(t,r){if(u)return eu(8,1,t,r)}function eK(t,r,i){if(u)return eu(9,1,t,r,i)}function eQ(t,r,i,a){if(u)return eu(10,1,t,r,i,a)}function eZ(t,r,i,a){if(u)return eu(11,1,t,r,i,a)}function eX(t,r,i,a){if(u)return eu(12,1,t,r,i,a)}function eY(t){if(u)return eu(13,1,t)}function eJ(t,r){if(u)return eu(14,1,t,r)}function e0(t,r,i){if(u)return eu(15,1,t,r,i)}var e1,e2,e3=()=>Y(""),e4=t=>{for(var r="";D()[t>>>0];)r+=e1[D()[t++>>>0]];return r},e6={},e8={},e5={};function e9(t,r,i={}){return function(t,r,i={}){var a=r.name;if(!t)throw new e2(`type "${a}" must have a positive integer typeid pointer`);if(e8.hasOwnProperty(t)){if(i.Pd)return;throw new e2(`Cannot register type '${a}' twice`)}e8[t]=r,delete e5[t],e6.hasOwnProperty(t)&&(r=e6[t],delete e6[t],r.forEach(t=>t()))}(t,r,i)}var e7=(t,r,i)=>{switch(r){case 1:return i?t=>M()[t>>>
vendor: 2,769 bytes, line 2
20]:t=>D()[t>>>0];case 2:return i?t=>P()[t>>>1>>>0]:t=>U()[t>>>1>>>0];case 4:return i?t=>W()[t>>>2>>>0]:t=>q()[t>>>2>>>0];case 8:return i?t=>z[t>>>3]:t=>A[t>>>3];default:throw TypeError(`invalid integer width (${r}): ${t}`)}};function te(t,r,i){i>>>=0,e9(t>>>=0,{name:r=e4(r>>>0),fromWireType:t=>t,toWireType:function(t,r){if("bigint"!=typeof r&&"number"!=typeof r)throw r=null===r?"null":"object"==(t=typeof r)||"array"===t||"function"===t?r.toString():""+r,TypeError(`Cannot convert "${r}" to ${this.name}`);return"number"==typeof r&&(r=BigInt(r)),r},zd:tt,readValueFromPointer:e7(r,i,-1==r.indexOf("u")),Ad:null})}var tt=8;function tr(t,r,i,a){e9(t>>>=0,{name:r=e4(r>>>0),fromWireType:function(t){return!!t},toWireType:function(t,r){return r?i:a},zd:tt,readValueFromPointer:function(t){return this.fromWireType(D()[t>>>0])},Ad:null})}var ti=[],ta=[];function tn(t){9<(t>>>=0)&&0==--ta[t+1]&&(ta[t]=void 0,ti.push(t))}var ts=t=>{if(!t)throw new e2("Cannot use deleted val. handle = "+t);return ta[t]},to=t=>{switch(t){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let r=ti.pop()||ta.length;return ta[r]=t,ta[r+1]=1,r}};function tu(t){return this.fromWireType(q()[t>>>2>>>0])}var tl={name:"emscripten::val",fromWireType:t=>{var r=ts(t);return tn(t),r},toWireType:(t,r)=>to(r),zd:8,readValueFromPointer:tu,Ad:null};function td(t){return e9(t>>>0,tl)}var tp=(t,r)=>{switch(r){case 4:return function(t){return this.fromWireType(L()[t>>>2>>>0])};case 8:return function(t){return this.fromWireType(V()[t>>>3>>>0])};default:throw TypeError(`invalid float width (${r}): ${t}`)}};function tc(t,r,i){i>>>=0,e9(t>>>=0,{name:r=e4(r>>>0),fromWireType:t=>t,toWireType:(t,r)=>r,zd:tt,readValueFromPointer:tp(r,i),Ad:null})}function th(t,r,i,a,n){if(t>>>=0,i>>>=0,r=e4(r>>>0),-1===n&&(n=0xffffffff),n=t=>t,0===a){var s=32-8*i;n=t=>t<<s>>>s}var o=r.includes("unsigned")?function(t,r){return r>>>0}:function(t,r){return r};e9(t,{name:r,fromWireType:n,toWireType:o,zd:tt,readValueFromPointer:e7(r,i,0!==a),Ad:null})}function tf(t,r,i){function a(t){var r=q()[t>>>2>>>0];return t=q()[t+4>>>2>>>0],new n(M().buffer,t,r)}var n=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][r];e9(t>>>=0,{name:i=e4(i>>>0),fromWireType:a,zd:tt,readValueFromPointer:a},{Pd:!0})}function tm(t,r){e9(t>>>=0,{name:r=e4(r>>>0),fromWireType:function(t){for(var r,i=q()[t>>>2>>>0],a=t+4,n=a,s=0;s<=i;++s){var o=a+s;s!=i&&0!=D()[o>>>0]||(n=eU(n,o-n),void 0===r?r=n:(r+="\0",r+=n),n=o+1)}return r2(t),r},toWireType:function(t,r){r instanceof ArrayBuffer&&(r=new Uint8Array(r));var i="string"==typeof r;if(!(i||r instanceof Uint8Array||r instanceof Uint8ClampedArray||r instanceof Int8Array))throw new e2("C
2annot pass non-string to std::string");var a=i?eL(r):r.length,n=r3(4+a+1),s=n+4;if(q()[n>>>2>>>0]=a,i)eV(r,s,a+1);else if(i)for(i=0;i<a;++i){var o=r.charCodeAt(i);if(255<o)throw r2(n),new e2("String has UTF-16 code units that do not fit in 8 bits");D()[s+i>>>0]=o}else for(i=0;i<a;++i)D()[s+i>>>0]=r[i];return null!==t&&t.push(r2,n),n},zd:tt,readValueFromPointer:tu,Ad(t){r2(t)}})}var tg="u">typeof TextDecoder?new TextDecoder("utf-16le"):void 0,ty=(t,r)=>{for(var i=t>>1,a=i+r/2;!(i>=a)&&U()[i>>>0];)++i;if(32<(i<<=1)-t&&tg)return tg.decode(D().slice(t,i));for(i="",a=0;!(a>=r/2);++a){var n=P()[t+2*a>>>1>>>0];if(0==n)break;i+=String.fromCharCode(n)}return i},t_=(t,r,i)=>{if(2>(i??=0x7fffffff))return 0;var a=r;i=(i-=2)<2*t.length?i/2:t.length;for(var n=0;n<i;++n){var s=t.charCodeAt(n);P()[r>>>1>>>0]=s,r+=2}return P()[r>>>1>>>0]=0,r-a},t$=t=>2*t.length,tb=(t,r)=>{for(var i=0,a="";!(i>=r/4);){var n=W()[t+4*i>>>2>>>0];if(0==n)break;++i,65536<=n?(n-=65536,a+=String.fromCharCode(55296|n>>10,56320|1023&n)):a+=String.fromCharCode(n)}return a},tv=(t,r,i)=>{if(r>>>=0,4>(i??=0x7fffffff))return 0;var a=r;i=a+i-4;for(var n=0;n<t.length;++n){var s=t.charCodeAt(n);if(55296<=s&&57343>=s&&(s=65536+((1023&s)<<10)|1023&t.charCodeAt(++n)),W()[r>>>2>>>0]=s,(r+=4)+4>i)break}return W()[r>>>2>>>0]=0,r-a},tw=t=>{for(var r=0,i=0;i<t.length;++i){var a=t.charCodeAt(i);55296<=a&&57343>=a&&++i,r+=4}return r};function tx(t,r,i){if(t>>>=0,r>>>=0,i=e4(i>>>=0),2===r)var a=ty,n=t_,s=t$,o=t=>U()[t>>>1>>>0];else 4===r&&(a=tb,n=tv,s=tw,o=t=>q()[t>>>2>>>0]);e9(t,{name:i,fromWireType:t=>{for(var i,n=q()[t>>>2>>>0],s=t+4,u=0;u<=n;++u){var l=t+4+u*r;u!=n&&0!=o(l)||(s=a(s,l-s),void 0===i?i=s:(i+="\0",i+=s),s=l+r)}return r2(t),i},toWireType:(t,a)=>{if("string"!=typeof a)throw new e2(`Cannot pass non-string to C++ string type ${i}`);var o=s(a),u=r3(4+o+r);return q()[u>>>2>>>0]=o/r,n(a,u+4,o+r),null!==t&&t.push(r2,u),u},zd:tt,readValueFromPointer:tu,Ad(t){r2(t)}})}function tS(t,r){e9(t>>>=0,{Qd:!0,name:r=e4(r>>>0),zd:0,fromWireType:()=>{},toWireType:()=>{}})}function tk(t){r4(t>>>0,!o,1,!s,131072,!1),e_()}var tT=t=>{if(!B)try{if(t(),!(0<eo))try{u?r9(w):ep(w)}catch(t){t instanceof ei||"unwind"==t||f(0,t)}}catch(t){t instanceof ei||"unwind"==t||f(0,t)}};function tC(t){t>>>=0,"function"==typeof Atomics.ge&&(Atomics.ge(W(),t>>>2,t).value.then(tI),t+=128,Atomics.store(W(),t>>>
22,1))}var tI=()=>{var t=r1();t&&(tC(t),tT(ie))};function tE(t,r){(t>>>=0)==r>>>0?setTimeout(tI):u?postMessage({Ed:t,yd:"checkMailbox"}):(t=em[t])&&t.postMessage({yd:"checkMailbox"})}var tz=[];function tA(t,r,i,a,n){for(r>>>=0,a/=2,tz.length=a,i=n>>>0>>>3,n=0;n<a;n++)tz[n]=z[i+2*n]?z[i+2*n+1]:V()[i+2*n+1>>>0];return(r?ee[r]:rY[t])(...tz)}var tO=()=>{eo=0};function tR(t){t>>>=0,u?postMessage({yd:"cleanupThread",ee:t}):eg(em[t])}function tB(t){}var tN=(t,r)=>{var i=e8[t];if(void 0===i)throw i=e4(t=rJ(t)),r2(t),new e2(`${r} has unknown type ${i}`);return i},tM=(t,r,i)=>{var a=[];return t=t.toWireType(a,i),a.length&&(q()[r>>>2>>>0]=to(a)),t};function tD(t,r,i){return r>>>=0,i>>>=0,t=ts(t>>>0),tM(r=tN(r,"emval::as"),i,t)}function tP(t,r){return r>>>=0,t=ts(t>>>0),(r=tN(r,"emval::as")).toWireType(null,t)}var tU=t=>{try{t()}catch(t){Y(t)}},tW=0,tq=null,tL=0,tV=[],tG={},tH={},tF=0,tj=null,tK=[];function tQ(t){return function(t){if(!B){if(0===tW){var r,i,a,n=!1,s=!1;t((t=0)=>{if(!B&&(tL=t,n=!0,s)){tW=2,tU(()=>i8(tq)),"u">typeof MainLoop&&MainLoop.Md&&MainLoop.resume(),t=!1;try{var r,i=(r=rX[tH[r=W()[tq+8>>>2>>>0]]],--eo,r())}catch(r){i=r,t=!0}var a=!1;if(!tq){var o=tj;o&&(tj=null,(t?o.reject:o.resolve)(i),a=!0)}if(t&&!a)throw i}}),s=!0,n||(tW=1,i=(r=r3(65548))+12,q()[r>>>2>>>0]=i,q()[r+4>>>2>>>0]=i+65536,void 0===(a=tG[i=tV[0]])&&(a=tF++,tG[i]=a,tH[a]=i),i=a,W()[r+8>>>2>>>0]=i,tq=r,"u">typeof MainLoop&&MainLoop.Md&&MainLoop.pause(),tU(()=>i4(tq)))}else 2===tW?(tW=0,tU(i5),r2(tq),tq=null,tK.forEach(tT)):Y(`invalid state: ${tW}`);return tL}}(r=>{t().then(r)})}function tZ(t){return t>>>=0,tQ(async()=>to(await ts(t)))}var tX=[];function tY(t,r,i,a){return i>>>=0,a>>>=0,(t=tX[t>>>0])(null,r=ts(r>>>0),i,a)}var tJ={},t0=t=>{var r=tJ[t];return void 0===r?e4(t):r};function t1(t,r,i,a,n){return i>>>=0,a>>>=0,n>>>=0,(t=tX[t>>>0])(r=ts(r>>>0),r[i=t0(i)],a,n)}var t2=()=>"object"==typeof globalThis?globalThis:Function("return this")();function t3(t){return 0==(t>>>=0)?to(t2()):(t=t0(t),to(t2()[t]))}var t4=t=>{var r=tX.length;return tX.push(t),r},t6=(t,r)=>{for(var i=Array(t),a=0;a<t;++a)i[a]=tN(q()[r+4*a>>>2>>>0],"parameter "+a);return i},t8=(t,r)=>Object.defineProperty(r,"name",{value:t});function t5(t,r,i){var a=(r=t6(t,r>>>0)).shift();t--;var n=`return function (obj, func, destructorsRef, args) {
3`,s=0,o=[];0===i&&o.push("obj");for(var u=["retType"],l=[a],d=0;d<t;++d)o.push("arg"+d),u.push("argType"+d),l.push(r[d]),n+=`  var arg${d} = argType${d}.readValueFromPointer(args${s?"+"+s:""});
4`,s+=r[d].zd;return n+=`  var rv = ${1===i?"new func":"func.call"}(${o.join(", ")});
5`,a.Qd||(u.push("emval_returnValue"),l.push(tM),n+=`  return emval_returnValue(retType, destructorsRef, rv);
6`),u.push(n+`};
7`),t=(function(t){var r=Function;if(!(r instanceof Function))throw TypeError(`new_ called with constructor type ${typeof r} which is not a function`);var i=t8(r.name||"unknownFunctionName",function(){});return i.prototype=r.prototype,i=new i,(t=r.apply(i,t))instanceof Object?t:i})(u)(...l),t4(t8(i=`methodCaller<(${r.map(t=>t.name).join(", ")}) => ${a.name}>`,t))}function t9(r){return to(t[r=t0(r>>>0)])}function t7(t,r){return r>>>=0,to((t=ts(t>>>0))[r=ts(r)])}function re(t){9<(t>>>=0)&&(ta[t+1]+=1)}function rt(){return to([])}function rr(t){t=ts(t>>>0);for(var r=Array(t.length),i=0;i<t.length;i++)r[i]=t[i];return to(r)}function ri(t){return to(t0(t>>>0))}function ra(){return to({})}function rn(t){for(var r=ts(t>>>=0);r.length;){var i=r.pop();r.pop()(i)}tn(t)}function rs(t,r,i){r>>>=0,i>>>=0,t=ts(t>>>0),r=ts(r),i=ts(i),t[r]=i}function ro(t,r){return r>>>=0,to(t=(t=tN(t>>>0,"_emval_take_value")).readValueFromPointer(r))}function ru(t,r){t=-0x20000000000000>t||0x20000000000000<t?NaN:Number(t),r>>>=0,t=new Date(1e3*t),W()[r>>>2>>>0]=t.getUTCSeconds(),W()[r+4>>>2>>>0]=t.getUTCMinutes(),W()[r+8>>>2>>>0]=t.getUTCHours(),W()[r+12>>>2>>>0]=t.getUTCDate(),W()[r+16>>>2>>>0]=t.getUTCMonth(),W()[r+20>>>2>>>0]=t.getUTCFullYear()-1900,W()[r+24>>>2>>>0]=t.getUTCDay(),t=(t.getTime()-Date.UTC(t.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,W()[r+28>>>2>>>0]=t}var rl=t=>t%4==0&&(t%100!=0||t%400==0),rd=[0,31,60,91,121,152,182,213,244,274,305,335],rp=[0,31,59,90,120,151,181,212,243,273,304,334];function rc(t,r){t=-0x20000000000000>t||0x20000000000000<t?NaN:Number(t),r>>>=0,t=new Date(1e3*t),W()[r>>>2>>>0]=t.getSeconds(),W()[r+4>>>2>>>0]=t.getMinutes(),W()[r+8>>>2>>>0]=t.getHours(),W()[r+12>>>2>>>0]=t.getDate(),W()[r+16>>>2>>>0]=t.getMonth(),W()[r+20>>>2>>>0]=t.getFullYear()-1900,W()[r+24>>>2>>>0]=t.getDay();var i=(rl(t.getFullYear())?rd:rp)[t.getMonth()]+t.getDate()-1|0;W()[r+28>>>2>>>0]=i,W()[r+36>>>2>>>0]=-60*t.getTimezoneOffset(),i=new Date(t.getFullYear(),6,1).getTimezoneOffset();var a=new Date(t.getFullYear(),0,1).getTimezoneOffset();t=0|(i!=a&&t.getTimezoneOffset()==Math.min(a,i)),W()[r+32>>>2>>>0]=t}function rh(t){t>>>=0;var r=new Date(W()[t+20>>>2>>>0]+1900,W()[t+16>>>2>>>0],W()[t+12>>>2>>>0],W()[t+8>>>2>>>0],W()[t+4>>>2>>>0],W()[t>>>2>>>0],0),i=W()[t+32>>>2>>>0],a=r.getTimezoneOffset(),n=new Date(r.getFullYear(),6,1).getTimezoneOffset(),s=new Date(r.getFullYear(),0,1).getTimezoneOffset(),o=Math.min(s,n);return 0>i?W()[t+32>>>2>>>0]=+(n!=s&&o==a):0<i!=(o==a)&&(n=Math.max(s,n),r.setTime(r.getTime()+6e4*((0<i?o:n)-a))),W()[t+24>>>2>>>0]=r.getDay(),i=(rl(r.getFullYear())?rd:rp)[r.getMonth()]+r.getDate()-1|0,W()[t+28>>>2>>>0]=i,W()[t>>>2>>>0]=r.getSeconds(),W()[t+4>>>2>>>0]=r.getMinutes(),W()[t+8>>>2>>>0]=r.getHours(),W()[t+12>>>2>>>0]=r.getDate(),W()[t+16>>>2>>>0]=r.getMonth(),W()[t+20>>>2>>>0]=r.getYear(),BigInt(isNaN(t=r.getTime())?-1:t/1e3)}function rf(t,r,i,a,n,s,o){return u?eu(16,1,t,r,i,a,n,s,o):-52}function rm(t,r,i,a,n,s){if(u)return eu(17,1,t,r,i,a,n,s)}var rg={},ry=()=>performance.timeOrigin+performance.now();function r_(t,r){if(u)return eu(18,1,t,r);if(rg[t]&&(clearTimeout(rg[t].id),delete rg[t]),!r)return 0;var i=setTimeout(()=>{delete rg[t],tT(()=>r7(t,performance.timeOrigin+performance.now()))},r);return rg[t]={id:i,ke:r},0}function r$(t,r,i,a){t>>>=0,r>>>=0,i>>>=0,a>>>=0;var n=new Date().getFullYear(),s=new Date(n,0,1).getTimezoneOffset(),o=Math.max(s,n=new Date(n,6,1).getTimezoneOffset());q()[t>>>2>>>0]=60*o,W()[r>>>2>>>0]=+(s!=n),t=(r=t=>{var r=Math.abs(t);return`UTC${0<=t?"-":"+"}${String(Math.floor(r/60)).padStart(2,"0")}${String(r%60).padStart(2,"0")}`})(s),r=r(n),n<s?(eV(t,i,17),eV(r,a,17)):(eV(t,a,17),eV(r,i,17))}var rb=()=>Date.now();function rv(t,r,i){return 0<=t&&3>=t?(t=0===t?Date.now():performance.timeOrigin+performance.now(),z[i>>>0>>>3]=BigInt(Math.round(1e6*t)),0):28}var rw=[],rx=(t,r)=>{rw.length=0;for(var i;i=D()[t++>>>0];){var a=105!=i;r+=(a&=112!=i)&&r%8?4:0,rw.push(112==i?q()[r>>>2>>>0]:106==i?z[r>>>3]:105==i?W()[r>>>2>>>0]:V()[r>>>3>>>
70]),r+=a?8:4}return rw};function rS(t,r,i){return t>>>=0,r=rx(r>>>0,i>>>0),ee[t](...r)}function rk(t,r,i){return t>>>=0,r=rx(r>>>0,i>>>0),ee[t](...r)}var rT=()=>{};function rC(t,r){return $(eU(t>>>0,r>>>0))}var rI=()=>{throw eo+=1,"unwind"};function rE(){return 0xffff0000}var rz=()=>navigator.hardwareConcurrency;function rA(){return Y("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"),0}function rO(t){t>>>=0;var r=D().length;if(t<=r||0xffff0000<t)return!1;for(var i=1;4>=i;i*=2){var a=r*(1+.2/i);a=Math.min(a,t+0x6000000);e:{a=(Math.min(0xffff0000,65536*Math.ceil(Math.max(t,a)/65536))-b.buffer.byteLength+65535)/65536|0;try{b.grow(a),F();var n=1;break e}catch{}n=void 0}if(n)return!0}return!1}var rR=()=>(Y("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"),0),rB={},rN=t=>{t.forEach(t=>{var r=rR();r&&(rB[r]=t)})};function rM(){var t=Error().stack.toString().split(`
8`);return"Error"==t[0]&&t.shift(),rN(t),rB.Kd=rR(),rB.ae=t,rB.Kd}function rD(t,r,i){if(t>>>=0,r>>>=0,rB.Kd==t)var a=rB.ae;else"Error"==(a=Error().stack.toString().split(`
9`))[0]&&a.shift(),rN(a);for(var n=3;a[n]&&rR()!=t;)++n;for(t=0;t<i&&a[t+n];++t)W()[r+4*t>>>2>>>0]=rR();return t}var rP,rU={},rW=()=>{if(!rP){var t,r={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:("object"==typeof navigator&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:"./this.program"};for(t in rU)void 0===rU[t]?delete r[t]:r[t]=rU[t];var i=[];for(t in r)i.push(`${t}=${r[t]}`);rP=i}return rP};function rq(t,r){if(u)return eu(19,1,t,r);t>>>=0,r>>>=0;var i=0;return rW().forEach((a,n)=>{var s=r+i;for(n=q()[t+4*n>>>2>>>0]=s,s=0;s<a.length;++s)M()[n++>>>0]=a.charCodeAt(s);M()[n>>>0]=0,i+=a.length+1}),0}function rL(t,r){if(u)return eu(20,1,t,r);t>>>=0,r>>>=0;var i=rW();q()[t>>>2>>>0]=i.length;var a=0;return i.forEach(t=>a+=t.length+1),q()[r>>>2>>>0]=a,0}function rV(t){return u?eu(21,1,t):52}function rG(t,r,i,a){return u?eu(22,1,t,r,i,a):52}function rH(t,r,i,a){return u?eu(23,1,t,r,i,a):70}var rF=[null,[],[]];function rj(t,r,i,a){if(u)return eu(24,1,t,r,i,a);r>>>=0,i>>>=0,a>>>=0;for(var n=0,s=0;s<i;s++){var o=q()[r>>>2>>>0],l=q()[r+4>>>2>>>0];r+=8;for(var d=0;d<l;d++){var p=D()[o+d>>>0],c=rF[t];0===p||10===p?((1===t?_:$)(eP(c)),c.length=0):c.push(p)}n+=l}return q()[a>>>2>>>0]=n,0}function rK(t){return t>>>0}u||function(){for(var r=t.numThreads-1;r--;)eb();en.unshift(()=>{var t;Q++,t=()=>X(),u?t():Promise.all(ec.map(e$)).then(t)})}();for(var rQ=Array(256),rZ=0;256>rZ;++rZ)rQ[rZ]=String.fromCharCode(rZ);e1=rQ,e2=t.BindingError=class extends Error{constructor(t){super(t),this.name="BindingError"}},t.InternalError=class extends Error{constructor(t){super(t),this.name="InternalError"}},ta.push(0,1,void 0,1,null,1,!0,1,!1,1),t.count_emval_handles=()=>ta.length/2-5-ti.length;var rX,rY=[el,ed,eN,eW,eq,eG,eH,eF,ej,eK,eQ,eZ,eX,eY,eJ,e0,rf,rm,r_,rq,rL,rV,rG,rH,rj];(async function(){function r(t,r){var i,a,n;return rX=t.exports,i=rX=function(){var t={};for(let[r,i]of Object.entries(rX))t[r]="function"==typeof i?(...t)=>{tV.push(r);try{return i(...t)}finally{B||(tV.pop(),tq&&1===tW&&0===tV.length&&(tW=0,eo+=1,tU(i6),"u">typeof Fibers&&Fibers.le()))}}:i;return t}(),a=t=>r=>t(r)>>>0,n=t=>()=>t()>>>0,(i=Object.assign({},i)).Cb=a(i.Cb),i.fc=n(i.fc),i.ic=a(i.ic),i.vc=a(i.vc),i.wc=n(i.wc),i.Ac=a(i.Ac),rX=i,ef.push(rX.jc),v=r,X(),rX}Q++;var n=J();if(t.instantiateWasm)return new Promise(i=>{t.instantiateWasm(n,(t,a)=>{r(t,a),i(t.exports)})});if(u)return new Promise(t=>{G=i=>{t(r(new WebAssembly.Instance(i,J()),i))}});K??=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",m):m+"ort-wasm-simd-threaded.jsep.wasm":new i.U(i(47759)).href;try{var s=await async function(t){if(!R&&"function"==typeof WebAssembly.instantiateStreaming&&!N(K))try{var r=fetch(K,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(r,t)}catch(t){$(`wasm streaming compile failed: ${t}`),$("falling back to ArrayBuffer instantiation")}return async function(t,r){try{var i=await async function(t){if(!R)try{var r=await p(t);return new Uint8Array(r)}catch{}
9if(t==K&&R)t=new Uint8Array(R);else{if(!c)throw"both async and sync fetching of the wasm failed";t=c(t)}return t}(t);return await WebAssembly.instantiate(i,r)}catch(t){$(`failed to asynchronously prepare wasm: ${t}`),Y(t)}}(K,t)}(n);r(s.instance,s.module)}catch(t){return a(t),Promise.reject(t)}})();var rJ=t=>(rJ=rX.Cb)(t),r0=()=>(r0=rX.Db)();t._OrtInit=(r,i)=>(t._OrtInit=rX.Eb)(r,i),t._OrtGetLastError=(r,i)=>(t._OrtGetLastError=rX.Fb)(r,i),t._OrtCreateSessionOptions=(r,i,a,n,s,o,u,l,d,p)=>(t._OrtCreateSessionOptions=rX.Gb)(r,i,a,n,s,o,u,l,d,p),t._OrtAppendExecutionProvider=(r,i)=>(t._OrtAppendExecutionProvider=rX.Hb)(r,i),t._OrtAddFreeDimensionOverride=(r,i,a)=>(t._OrtAddFreeDimensionOverride=rX.Ib)(r,i,a),t._OrtAddSessionConfigEntry=(r,i,a)=>(t._OrtAddSessionConfigEntry=rX.Jb)(r,i,a),t._OrtReleaseSessionOptions=r=>(t._OrtReleaseSessionOptions=rX.Kb)(r),t._OrtCreateSession=(r,i,a)=>(t._OrtCreateSession=rX.Lb)(r,i,a),t._OrtReleaseSession=r=>(t._OrtReleaseSession=rX.Mb)(r),t._OrtGetInputOutputCount=(r,i,a)=>(t._OrtGetInputOutputCount=rX.Nb)(r,i,a),t._OrtGetInputName=(r,i)=>(t._OrtGetInputName=rX.Ob)(r,i),t._OrtGetOutputName=(r,i)=>(t._OrtGetOutputName=rX.Pb)(r,i),t._OrtFree=r=>(t._OrtFree=rX.Qb)(r),t._OrtCreateTensor=(r,i,a,n,s,o)=>(t._OrtCreateTensor=rX.Rb)(r,i,a,n,s,o),t._OrtGetTensorData=(r,i,a,n,s)=>(t._OrtGetTensorData=rX.Sb)(r,i,a,n,s),t._OrtReleaseTensor=r=>(t._OrtReleaseTensor=rX.Tb)(r),t._OrtCreateRunOptions=(r,i,a,n)=>(t._OrtCreateRunOptions=rX.Ub)(r,i,a,n),t._OrtAddRunConfigEntry=(r,i,a)=>(t._OrtAddRunConfigEntry=rX.Vb)(r,i,a),t._OrtReleaseRunOptions=r=>(t._OrtReleaseRunOptions=rX.Wb)(r),t._OrtCreateBinding=r=>(t._OrtCreateBinding=rX.Xb)(r),t._OrtBindInput=(r,i,a)=>(t._OrtBindInput=rX.Yb)(r,i,a),t._OrtBindOutput=(r,i,a,n)=>(t._OrtBindOutput=rX.Zb)(r,i,a,n),t._OrtClearBoundOutputs=r=>(t._OrtClearBoundOutputs=rX._b)(r),t._OrtReleaseBinding=r=>(t._OrtReleaseBinding=rX.$b)(r),t._OrtRunWithBinding=(r,i,a,n,s)=>(t._OrtRunWithBinding=rX.ac)(r,i,a,n,s),t._OrtRun=(r,i,a,n,s,o,u,l)=>(t._OrtRun=rX.bc)(r,i,a,n,s,o,u,l),t._OrtEndProfiling=r=>(t._OrtEndProfiling=rX.cc)(r),t._JsepOutput=(r,i,a)=>(t._JsepOutput=rX.dc)(r,i,a),t._JsepGetNodeName=r=>(t._JsepGetNodeName=rX.ec)(r);var r1=()=>(r1=rX.fc)(),r2=t._free=r=>(r2=t._free=rX.gc)(r),r3=t._malloc=r=>(r3=t._malloc=rX.ic)(r),r4=(t,r,i,a,n,s)=>(r4=rX.kc)(t,r,i,a,n,s),r6=()=>(r6=rX.lc)(),r8=(t,r,i,a,n)=>(r8=rX.mc)(t,r,i,a,n),r5=t=>(r5=rX.nc)(t),r9=t=>(r9=rX.oc)(t),r7=(t,r)=>(r7=rX.pc)(t,r),ie=()=>(ie=rX.qc)(),it=(t,r)=>(it=rX.rc)(t,r),ir=t=>(ir=rX.sc)(t),ii=(t,r)=>(ii=rX.tc)(t,r),ia=t=>(ia=rX.uc)(t),is=t=>(is=rX.vc)(t),io=()=>(io=rX.wc)(),iu=t=>(iu=rX.xc)(t),il=t=>(il=rX.yc)(t),id=(t,r,i)=>(id=rX.zc)(t,r,i),ip=t=>(ip=rX.Ac)(t),ic=t.dynCall_iii=(r,i,a)=>(ic=t.dynCall_iii=rX.Bc)(r,i,a),ih=t.dynCall_vi=(r,i)=>(ih=t.dynCall_vi=rX.Cc)(r,i),im=t.dynCall_ii=(r,i)=>(im=t.dynCall_ii=rX.Dc)(r,i),ig=t.dynCall_vii=(r,i,a)=>(ig=t.dynCall_vii=rX.Ec)(r,i,a),iy=t.dynCall_iiii=(r,i,a,n)=>(iy=t.dynCall_iiii=rX.Fc)(r,i,a,n),i_=t.dynCall_viii=(r,i,a,n)=>(i_=t.dynCall_viii=rX.Gc)(r,i,a,n),i$=t.dynCall_iiiii=(r,i,a,n,s)=>(i$=t.dynCall_iiiii=rX.Hc)(r,i,a,n,s),ib=t.dynCall_viiii=(r,i,a,n,s)=>(ib=t.dynCall_viiii=rX.Ic)(r,i,a,n,s),iv=t.dynCall_viiiiii=(r,i,a,n,s,o,u)=>(iv=t.dynCall_viiiiii=rX.Jc)(r,i,a,n,s,o,u),iw=t.dynCall_viiiiiii=(r,i,a,n,s,o,u,l)=>(iw=t.dynCall_viiiiiii=rX.Kc)(r,i,a,n,s,o,u,l),ix=t.dynCall_ji=(r,i)=>(ix=t.dynCall_ji=rX.Lc)(r,i),iS=t.dynCall_v=r=>(iS=t.dynCall_v=rX.Mc)(r),ik=t.dynCall_viiiii=(r,i,a,n,s,o)=>(ik=t.dynCall_viiiii=rX.Nc)(r,i,a,n,s,o),iT=t.dynCall_i=r=>(iT=t.dynCall_i=rX.Oc)(r),iC=t.dynCall_fii=(r,i,a)=>(iC=t.dynCall_fii=rX.Pc)(r,i,a),iI=t.dynCall_viiiiiiii=(r,i,a,n,s,o,u,l,d)=>(iI=t.dynCall_viiiiiiii=rX.Qc)(r,i,a,n,s,o,u,l,d),iE=t.dynCall_viiiiiiiiii=(r,i,a,n,s,o,u,l,d,p,c)=>(iE=t.dynCall_viiiiiiiiii=rX.Rc)(r,i,a,n,s,o,u,l,d,p,c),iz=t.dynCall_jiii=(r,i,a,n)=>(iz=t.dynCall_jiii=rX.Sc)(r,i,a,n),iA=t.dynCall_dii=(r,i,a)=>(iA=t.dynCall_dii=rX.Tc)(r,i,a),iO=t.dynCall_viiiiiiiii=(r,i,a,n,s,o,u,l,d,p)=>(iO=t.dynCall_viiiiiiiii=rX.Uc)(r,i,a,n,s,o,u,l,d,p),iR=t.dynCall_viiiiiiiiiii=(r,i,a,n,s,o,u,l,d,p,c,h)=>(iR=t.dynCall_viiiiiiiiiii=rX.Vc)(r,i,a,n,s,o,u,l,d,p,c,h),iB=t.dynCall_iiiiii=(r,i,a,n,s,o)=>(iB=t.dynCall_iiiiii=rX.Wc)(r,i,a,n,s,o),iN=t.dynCall_iij=(r,i,a)=>(iN=t.dynCall_iij=rX.Xc)(r,i,a),iM=t.dynCall_iiiiiiiiii=(r,i,a,n,s,o,u,l,d,p)=>(iM=t.dynCall_iiiiiiiiii=rX.Yc)(r,i,a,n,s,o,u,l,d,p),iD=t.dynCall_iiiiiiiiiii=(r,i,a,n,s,o,u,l,d,p,c)=>(iD=t.dynCall_iiiiiiiiiii=rX.Zc)(r,i,a,n,s,o,u,l,d,p,c),iP=t.dynCall_vij=(r,i,a)=>
9(iP=t.dynCall_vij=rX._c)(r,i,a),iU=t.dynCall_iiif=(r,i,a,n)=>(iU=t.dynCall_iiif=rX.$c)(r,i,a,n),iW=t.dynCall_iiij=(r,i,a,n)=>(iW=t.dynCall_iiij=rX.ad)(r,i,a,n),iq=t.dynCall_fiii=(r,i,a,n)=>(iq=t.dynCall_fiii=rX.bd)(r,i,a,n),iL=t.dynCall_viiiiiiiiiiiii=(r,i,a,n,s,o,u,l,d,p,c,h,f,m)=>(iL=t.dynCall_viiiiiiiiiiiii=rX.cd)(r,i,a,n,s,o,u,l,d,p,c,h,f,m),iV=t.dynCall_vjiii=(r,i,a,n,s)=>(iV=t.dynCall_vjiii=rX.dd)(r,i,a,n,s),iG=t.dynCall_vif=(r,i,a)=>(iG=t.dynCall_vif=rX.ed)(r,i,a),iH=t.dynCall_iiiiiii=(r,i,a,n,s,o,u)=>(iH=t.dynCall_iiiiiii=rX.fd)(r,i,a,n,s,o,u),iF=t.dynCall_iiiij=(r,i,a,n,s)=>(iF=t.dynCall_iiiij=rX.gd)(r,i,a,n,s),ij=t.dynCall_iiiiiiii=(r,i,a,n,s,o,u,l)=>(ij=t.dynCall_iiiiiiii=rX.hd)(r,i,a,n,s,o,u,l),iK=t.dynCall_viiiiiiiiiiii=(r,i,a,n,s,o,u,l,d,p,c,h,f)=>(iK=t.dynCall_viiiiiiiiiiii=rX.id)(r,i,a,n,s,o,u,l,d,p,c,h,f),iQ=t.dynCall_diii=(r,i,a,n)=>(iQ=t.dynCall_diii=rX.jd)(r,i,a,n),iZ=t.dynCall_jiiii=(r,i,a,n,s)=>(iZ=t.dynCall_jiiii=rX.kd)(r,i,a,n,s),iX=t.dynCall_viiij=(r,i,a,n,s)=>(iX=t.dynCall_viiij=rX.ld)(r,i,a,n,s),iY=t.dynCall_fiiii=(r,i,a,n,s)=>
9(iY=t.dynCall_fiiii=rX.md)(r,i,a,n,s),iJ=t.dynCall_viiif=(r,i,a,n,s)=>(iJ=t.dynCall_viiif=rX.nd)(r,i,a,n,s),i0=t.dynCall_diiii=(r,i,a,n,s)=>(i0=t.dynCall_diiii=rX.od)(r,i,a,n,s),i1=t.dynCall_viiid=(r,i,a,n,s)=>(i1=t.dynCall_viiid=rX.pd)(r,i,a,n,s),i2=t.dynCall_iiiijii=(r,i,a,n,s,o,u)=>(i2=t.dynCall_iiiijii=rX.qd)(r,i,a,n,s,o,u),i3=t.dynCall_iiiiiij=(r,i,a,n,s,o,u)=>(i3=t.dynCall_iiiiiij=rX.rd)(r,i,a,n,s,o,u),i4=t=>(i4=rX.sd)(t),i6=()=>(i6=rX.td)(),i8=t=>(i8=rX.ud)(t),i5=()=>(i5=rX.vd)();function i9(t,r,i){var a=io();try{ig(t,r,i)}catch(t){if(ia(a),t!==t+0)throw t;it(1,0)}}function i7(t,r,i){var a=io();try{return ic(t,r,i)}catch(t){if(ia(a),t!==t+0)throw t;it(1,0)}}function ae(t,r){var i=io();try{ih(t,r)}catch(t){if(ia(i),t!==t+0)throw t;it(1,0)}}function at(t,r){var i=io();try{return im(t,r)}catch(t){if(ia(i),t!==t+0)throw t;it(1,0)}}function ar(t,r,i,a){var n=io();try{return iy(t,r,i,a)}catch(t){if(ia(n),t!==t+0)throw t;it(1,0)}}function ai(t,r,i,a,n){var s=io();try{ib(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function aa(t,r,i,a,n){var s=io();try{return i$(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function an(t,r,i,a){var n=io();try{i_(t,r,i,a)}catch(t){if(ia(n),t!==t+0)throw t;it(1,0)}}function as(t,r,i,a,n,s,o){var u=io();try{return iH(t,r,i,a,n,s,o)}catch(t){if(ia(u),t!==t+0)throw t;it(1,0)}}function ao(t){var r=io();try{iS(t)}catch(t){if(ia(r),t!==t+0)throw t;it(1,0)}}function au(t,r,i){var a=io();try{return iN(t,r,i)}catch(t){if(ia(a),t!==t+0)throw t;it(1,0)}}function al(t,r,i,a,n,s){var o=io();try{ik(t,r,i,a,n,s)}catch(t){if(ia(o),t!==t+0)throw t;it(1,0)}}function ad(t,r,i){var a=io();try{iP(t,r,i)}catch(t){if(ia(a),t!==t+0)throw t;it(1,0)}}function ap(t,r,i,a,n,s,o){var u=io();try{iv(t,r,i,a,n,s,o)}catch(t){if(ia(u),t!==t+0)throw t;it(1,0)}}function ac(t,r,i,a,n,s,o,u){var l=io();try{iw(t,r,i,a,n,s,o,u)}catch(t){if(ia(l),t!==t+0)throw t;it(1,0)}}function ah(t,r,i,a,n,s){var o=io();try{return iB(t,r,i,a,n,s)}catch(t){if(ia(o),t!==t+0)throw t;it(1,0)}}function af(t,r,i,a,n,s,o,u){var l=io();try{return ij(t,r,i,a,n,s,o,u)}catch(t){if(ia(l),t!==t+0)throw t;it(1,0)}}function am(t,r,i,a,n,s,o,u,l,d){var p=io();try{iO(t,r,i,a,n,s,o,u,l,d)}catch(t){if(ia(p),t!==t+0)throw t;it(1,0)}}function ag(t,r,i,a,n,s,o,u,l){var d=io();try{iI(t,r,i,a,n,s,o,u,l)}catch(t){if(ia(d),t!==t+0)throw t;it(1,0)}}function ay(t){var r=io();try{return iT(t)}catch(t){if(ia(r),t!==t+0)throw t;it(1,0)}}function a_(t,r,i,a,n,s,o,u,l,d){var p=io();try{return iM(t,r,i,a,n,s,o,u,l,d)}catch(t){if(ia(p),t!==t+0)throw t;it(1,0)}}function a$(t,r,i){var a=io();try{return iC(t,r,i)}catch(t){if(ia(a),t!==t+0)throw t;it(1,0)}}function ab(t,r,i,a){var n=io();try{return iz(t,r,i,a)}catch(t){if(ia(n),t!==t+0)throw t;return it(1,0),0n}}function av(t,r,i){var a=io();try{return iA(t,r,i)}catch(t){if(ia(a),t!==t+0)throw t;it(1,0)}}function aw(t,r,i,a,n,s,o,u,l,d,p,c){var h=io();try{iR(t,r,i,a,n,s,o,u,l,d,p,c)}catch(t){if(ia(h),t!==t+0)throw t;it(1,0)}}function ax(t,r,i,a,n,s,o,u,l,d,p){var c=io();try{iE(t,r,i,a,n,s,o,u,l,d,p)}catch(t){if(ia(c),t!==t+0)throw t;it(1,0)}}function aS(t,r,i,a,n,s,o,u,l,d,p){var c=io();try{return iD(t,r,i,a,n,s,o,u,l,d,p)}catch(t){if(ia(c),t!==t+0)throw t;it(1,0)}}function ak(t,r,i,a){var n=io();try{return iU(t,r,i,a)}catch(t){if(ia(n),t!==t+0)throw t;it(1,0)}}function aT(t,r,i,a){var n=io();try{return iW(t,r,i,a)}catch(t){if(ia(n),t!==t+0)throw t;it(1,0)}}function aC(t,r,i,a){var n=io();try{return iq(t,r,i,a)}catch(t){if(ia(n),t!==t+0)throw t;it(1,0)}}function aI(t,r,i,a,n,s,o,u,l,d,p,c,h,f){var m=io();try{iL(t,r,i,a,n,s,o,u,l,d,p,c,h,f)}catch(t){if(ia(m),t!==t+0)throw t;it(1,0)}}function aE(t,r,i,a,n){var s=io();try{iV(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function az(t,r,i){var a=io();try{iG(t,r,i)}catch(t){if(ia(a),t!==t+0)throw t;it(1,0)}}function aA(t,r){var i=io();try{return ix(t,r)}catch(t){if(ia(i),t!==t+0)throw t;return it(1,0),0n}}function aO(t,r,i,a,n){var s=io();try{return iF(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function aR(t,r,i,a,n,s,o,u,l,d,p,c,h){var f=io();try{iK(t,r,i,a,n,s,o,u,l,d,p,c,h)}catch(t){if(ia(f),t!==t+0)throw t;it(1,0)}}function aB(t,r,i,a){var n=io();try{return iQ(t,r,i,a)}catch(t){if(ia(n),t!==t+0)throw t;it(1,0)}}function aN(t,r,i,a,n){var s=io();try{return iZ(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;return it(1,0),0n}}function aM(t,r,i,a,n){var s=io();try{iX(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function aD(t,r,i,a,n){var s=io();try{return iY(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function aP(t,r,i,a,n){var s=io();try{iJ(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function aU(t,r,i,a,n){var s=io();try{return i0(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function aW(t,r,i,a,n){var s=io();try{i1(t,r,i,a,n)}catch(t){if(ia(s),t!==t+0)throw t;it(1,0)}}function aq(t,r,i,a,n,s,o){var u=io();try{return i2(t,r,i,a,n,s,o)}catch(t){if(ia(u),t!==t+0)throw t;it(1,0)}}function aL(t,r,i,a,n,s,o){var u=io();try{return i3(t,r,i,a,n,s,o)}catch(t){if(ia(u),t!==t+0)throw t;it(1,0)}}return t.stackSave=()=>io(),t.stackRestore=t=>ia(t),t.stackAlloc=t=>is(t),t.setValue=function(t,r,i="i8"){switch(i.endsWith("*")&&(i="*"),i){case"i1":case"i8":M()[t>>>
90]=r;break;case"i16":P()[t>>>1>>>0]=r;break;case"i32":W()[t>>>2>>>0]=r;break;case"i64":z[t>>>3]=BigInt(r);break;case"float":L()[t>>>2>>>0]=r;break;case"double":V()[t>>>3>>>0]=r;break;case"*":q()[t>>>2>>>0]=r;break;default:Y(`invalid type for setValue: ${i}`)}},t.getValue=function(t,r="i8"){switch(r.endsWith("*")&&(r="*"),r){case"i1":case"i8":return M()[t>>>0];case"i16":return P()[t>>>1>>>0];case"i32":return W()[t>>>2>>>0];case"i64":return z[t>>>3];case"float":return L()[t>>>2>>>0];case"double":return V()[t>>>3>>>0];case"*":return q()[t>>>2>>>0];default:Y(`invalid type for getValue: ${r}`)}},t.UTF8ToString=eU,t.stringToUTF8=eV,t.lengthBytesUTF8=eL,function i(){if(0<Q)Z=i;else if(u)r(t),j();else{for(;0<en.length;)en.shift()(t);0<Q?Z=i:(t.calledRun=!0,B||(j(),r(t)))}}(),t.PTR_SIZE=4,n},globalThis.self?.name?.startsWith("em-pthread")&&e_()}),oU=W(()=>{ed(),eb=typeof location>"u"?void 0:location.origin,ev="file:///vercel/path0/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs".startsWith("file:")?new URL(new i.U(i(42968)).href,eb).href:"file:///vercel/path0/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs",ew=()=>{if(ev&&!ev.startsWith("blob:"))return ev.substring(0,ev.lastIndexOf("/")+1)},ex=(t,r)=>{try{let i=r??ev;return(i?new URL(t,i):new URL(t)).origin===eb}catch{return!1}},eS=(t,r)=>{let i=r??ev;try{return(i?new URL(t,i):new URL(t)).href}catch{return}},ek=(t,r)=>`${r??"./"}${t}`,eT=async t=>{let r=await (await fetch(t,{credentials:"same-origin"})).blob();return URL.createObjectURL(r)},eC=async t=>(await import(t)).default,eI=(em(),V(ep)).default,eE=async()=>{if(!ev)throw Error("Failed to load proxy worker: cannot determine the script source URL.");if(ex(ev))return[void 0,eI()];let t=await eT(ev);return[t,eI(t)]},ez=(oP(),V(eg)).default,eA=async(t,r,i)=>{if(!t&&!r&&ez&&ev&&ex(ev))return[void 0,ez];{let a="ort-wasm-simd-threaded.jsep.mjs",n=t??eS(a,r),s=i&&n&&!ex(n,r),o=s?await eT(n):n??ek(a,r);return[s?o:void 0,await eC(o)]}}}),oW=W(()=>{oU(),eR=!1,eB=!1,eN=!1,eM=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return"u">typeof MessageChannel&&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}},eD=()=>{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}},eP=async t=>{if(eR)return Promise.resolve();if(eB)throw Error("multiple calls to 'initializeWebAssembly()' detected.");if(eN)throw Error("previous call to 'initializeWebAssembly()' failed.");eB=!0;let r=t.initTimeout,i=t.numThreads;if(!eD())throw Error("WebAssembly SIMD is not supported in the current environment.");let a=eM();i>1&&!a&&("u">typeof self&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+i+", 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."),t.numThreads=i=1);let n=t.wasmPaths,s="string"==typeof n?n:void 0,o=n?.mjs,u=o?.href??o,l=n?.wasm,d=l?.href??l,p=t.wasmBinary,[c,h]=await eA(u,s,i>1),f=!1,m=[];if(r>0&&m.push(new Promise(t=>{setTimeout(()=>{f=!0,t()},r)})),m.push(new Promise((t,r)=>{let a={numThreads:i};if(p)a.wasmBinary=p;else if(d||s)a.locateFile=t=>d??s+t;else if(u&&0!==u.indexOf("blob:"))a.locateFile=t=>new URL(t,u).href;else if(c){let t=ew();t&&(a.locateFile=r=>t+r)}h(a).then(r=>{eB=!1,eR=!0,eO=r,t(),c&&URL.revokeObjectURL(c)},t=>{eB=!1,eN=!0,r(t)})})),await Promise.race(m),f)throw Error(`WebAssembly backend initializing failed due to timeout: ${r}ms`)},eU=()=>{if(eR&&eO)return eO;throw Error("WebAssembly is not initialized yet.")}}),oq=W(()=>{oW(),eW=(t,r)=>{let i=eU(),a=i.lengthBytesUTF8(t)+1,n=i._malloc(a);return i.stringToUTF8(t,n,a),r.push(n),n},eq=(t,r,i,a)=>{if("object"==typeof t&&null!==t){if(i.has(t))throw Error("Circular reference in options");
9i.add(t)}Object.entries(t).forEach(([t,n])=>{let s=r?r+t:t;if("object"==typeof n)eq(n,s+".",i,a);else if("string"==typeof n||"number"==typeof n)a(s,n.toString());else if("boolean"==typeof n)a(s,n?"1":"0");else throw Error(`Can't handle extra config type: ${typeof n}`)})},eL=t=>{let r=eU(),i=r.stackSave();try{let i=r.PTR_SIZE,a=r.stackAlloc(2*i);r._OrtGetLastError(a,a+i);let n=Number(r.getValue(a,4===i?"i32":"i64")),s=r.getValue(a+i,"*"),o=s?r.UTF8ToString(s):"";throw Error(`${t} ERROR_CODE: ${n}, ERROR_MESSAGE: ${o}`)}finally{r.stackRestore(i)}}}),oL=W(()=>{oW(),oq(),eV=t=>{let r=eU(),i=0,a=[],n=t||{};try{if(t?.logSeverityLevel===void 0)n.logSeverityLevel=2;else if("number"!=typeof t.logSeverityLevel||!Number.isInteger(t.logSeverityLevel)||t.logSeverityLevel<0||t.logSeverityLevel>4)throw Error(`log serverity level is not valid: ${t.logSeverityLevel}`);if(t?.logVerbosityLevel===void 0)n.logVerbosityLevel=0;else if("number"!=typeof t.logVerbosityLevel||!Number.isInteger(t.logVerbosityLevel))throw Error(`log verbosity level is not valid: ${t.logVerbosityLevel}`);t?.terminate===void 0&&(n.terminate=!1);let s=0;return t?.tag!==void 0&&(s=eW(t.tag,a)),i=r._OrtCreateRunOptions(n.logSeverityLevel,n.logVerbosityLevel,!!n.terminate,s),0===i&&eL("Can't create run options."),t?.extra!==void 0&&eq(t.extra,"",new WeakSet,(t,n)=>{let s=eW(t,a),o=eW(n,a);0!==r._OrtAddRunConfigEntry(i,s,o)&&eL(`Can't set a run config entry: ${t} - ${n}.`)}),[i,a]}catch(t){throw 0!==i&&r._OrtReleaseRunOptions(i),a.forEach(t=>r._free(t)),t}}}),oV=W(()=>{oW(),oq(),eG=t=>{switch(t){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw Error(`unsupported graph optimization level: ${t}`)}},eH=t=>{switch(t){case"sequential":return 0;case"parallel":return 1;default:throw Error(`unsupported execution mode: ${t}`)}},eF=t=>{t.extra||(t.extra={}),t.extra.session||(t.extra.session={});let r=t.extra.session;r.use_ort_model_bytes_directly||(r.use_ort_model_bytes_directly="1"),t.executionProviders&&t.executionProviders.some(t=>("string"==typeof t?t:t.name)==="webgpu")&&(t.enableMemPattern=!1)},ej=(t,r,i)=>{for(let a of r){let r="string"==typeof a?a:a.name;switch(r){case"webnn":if(r="WEBNN","string"!=typeof a){let r=a?.deviceType;if(r){let a=eW("deviceType",i),n=eW(r,i);0!==eU()._OrtAddSessionConfigEntry(t,a,n)&&eL(`Can't set a session config entry: 'deviceType' - ${r}.`)}}break;case"webgpu":if(r="JS","string"!=typeof a&&a?.preferredLayout){if("NCHW"!==a.preferredLayout&&"NHWC"!==a.preferredLayout)throw Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);let r=eW("preferredLayout",i),n=eW(a.preferredLayout,i);0!==eU()._OrtAddSessionConfigEntry(t,r,n)&&eL(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`)}break;case"wasm":case"cpu":continue;default:throw Error(`not supported execution provider: ${r}`)}let n=eW(r,i);0!==eU()._OrtAppendExecutionProvider(t,n)&&eL(`Can't append execution provider: ${r}.`)}},eK=t=>{let r=eU(),i=0,a=[],n=t||{};eF(n);try{let t=eG(n.graphOptimizationLevel??"all"),s=eH(n.executionMode??"sequential"),o="string"==typeof n.logId?eW(n.logId,a):0,u=n.logSeverityLevel??2;if(!Number.isInteger(u)||u<0||u>4)throw Error(`log serverity level is not valid: ${u}`);let l=n.logVerbosityLevel??0;if(!Number.isInteger(l)||l<0||l>4)throw Error(`log verbosity level is not valid: ${l}`);let d="string"==typeof n.optimizedModelFilePath?eW(n.optimizedModelFilePath,a):0;if(i=r._OrtCreateSessionOptions(t,!!n.enableCpuMemArena,!!n.enableMemPattern,s,!!n.enableProfiling,0,o,u,l,d),0===i&&eL("Can't create session options."),n.executionProviders&&ej(i,n.executionProviders,a),void 0!==n.enableGraphCapture){if("boolean"!=typeof n.enableGraphCapture)throw Error(`enableGraphCapture must be a boolean value: ${n.enableGraphCapture}`);let t=eW("enableGraphCapture",a),s=eW(n.enableGraphCapture.toString(),a);0!==r._OrtAddSessionConfigEntry(i,t,s)&&eL(`Can't set a session config entry: 'enableGraphCapture' - ${n.enableGraphCapture}.`)}if(n.freeDimensionOverrides)for(let[t,s]of Object.entries(n.freeDimensionOverrides)){if("string"!=typeof t)throw Error(`free dimension override name must be a string: ${t}`);if("number"!=typeof s||!Number.isInteger(s)||s<0)throw Error(`free dimension override value must be a non-negative integer: ${s}`);let n=eW(t,a);0!==r._OrtAddFreeDimensionOverride(i,n,s)&&eL(`Can't set a free dimension override: ${t} - ${s}.`)}return void 0!==n.extra&&eq(n.extra,"",new WeakSet,(t,n)=>{let s=eW(t,a),o=eW(n,a);0!==r._OrtAddSessionConfigEntry(i,s,o)&&eL(`Can't set a session config entry: ${t} - ${n}.`)}),[i,a]}catch(t){throw 0!==i&&0!==r._OrtReleaseSessionOptions(i)&&eL("Can't release session options."),a.forEach(t=>r._free(t)),t}}}),oG=W(()=>{eQ=t=>{switch(t){case"int8":return 3;case"uint8":return 2;case"bool":return 9;
9case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw Error(`unsupported data type: ${t}`)}},eZ=t=>{switch(t){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw Error(`unsupported data type: ${t}`)}},eX=(t,r)=>{let i=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][t],a="number"==typeof r?r:r.reduce((t,r)=>t*r,1);return i>0?Math.ceil(a*i):void 0},eY=t=>{switch(t){case"float16":return"u">typeof Float16Array&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":case"bool":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw Error(`unsupported type: ${t}`)}},eJ=t=>{switch(t){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw Error(`unsupported logging level: ${t}`)}},e0=t=>"float32"===t||"float16"===t||"int32"===t||"int64"===t||"uint32"===t||"uint8"===t||"bool"===t||"uint4"===t||"int4"===t,e1=t=>"float32"===t||"float16"===t||"int32"===t||"int64"===t||"uint32"===t||"uint64"===t||"int8"===t||"uint8"===t||"bool"===t||"uint4"===t||"int4"===t,e2=t=>{switch(t){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw Error(`unsupported data location: ${t}`)}}}),oH=W(()=>{ed(),e3=async t=>{if("string"!=typeof t)return t instanceof Blob?new Uint8Array(await t.arrayBuffer()):t instanceof Uint8Array?t:new Uint8Array(t);{let r=await fetch(t);if(!r.ok)throw Error(`failed to load external data file: ${t}`);let i=r.headers.get("Content-Length"),a=i?parseInt(i,10):0;if(a<0x40000000)return new Uint8Array(await r.arrayBuffer());{if(!r.body)throw Error(`failed to load external data file: ${t}, no response body.`);let i=r.body.getReader(),n;try{n=new ArrayBuffer(a)}catch(t){if(t instanceof RangeError){let t=Math.ceil(a/65536);n=new WebAssembly.Memory({initial:t,maximum:t}).buffer}else throw t}let s=0;for(;;){let{done:t,value:r}=await i.read();if(t)break;let a=r.byteLength;new Uint8Array(n,s,a).set(r),s+=a}return new Uint8Array(n,0,a)}}}}),oF=W(()=>{oG(),e4=["V","I","W","E","F"],e6=(t,r)=>{console.log(`[${e4[t]},${new Date().toISOString()}]${r}`)},e9=(t,r)=>{e8=t,e5=r},e7=(t,r)=>{let i=eJ(t);i>=eJ(e8)&&e6(i,"function"==typeof r?r():r)},te=(...t)=>{e5&&e7(...t)}}),oj=W(()=>{oG(),tt=(t,r)=>new(eY(r))(t)}),oK=W(()=>{}),oQ=W(()=>{oF(),oK(),tr=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],[0xc00000,10],[0x1000000,10],[0x1900000,15],[0x2000000,22],[0x2a30000,2],[0x3840000,6],[0x4000000,6],[0x8000000,6],[0xa000000,6]]),ti=[],ta=t=>16*Math.ceil(Number(t)/16),tn=t=>{for(let r=0;r<ti.length;r++){let i=ti[r];if(t<=i)return i}return 16*Math.ceil(t/16)},ts=1,to=()=>ts++,tu=async(t,r,i,a)=>{let n=ta(i),s=t.device.createBuffer({size:n,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let o=t.getCommandEncoder();t.endComputePass(),o.copyBufferToBuffer(r,0,s,0,n),t.flush(),await s.mapAsync(GPUMapMode.READ);let u=s.getMappedRange();if(!a)return new Uint8Array(u.slice(0,i));{let t=a();return t.set(new Uint8Array(u,0,i)),t}}finally{s.destroy()}},tl=class{constructor(t){for(let[r]of(this.backend=t,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map,tr))ti.push(r),this.freeBuffers.set(r,[]),this.freeUniformBuffers.set(r,[]);this.sessionCount=0}upload(t,r){let i=r.buffer,a=r.byteOffset,n=r.byteLength,s=ta(n),o=this.storageCache.get(t);if(!o)throw Error("gpu data for uploading does not exist");if(Number(o.originalSize)!==n)throw Error(`inconsistent data size. gpu data size=${o.originalSize}, data size=${n}`);let u=this.backend.device.createBuffer({mappedAtCreation:!0,size:s,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC});new Uint8Array(u.getMappedRange()).set(new Uint8Array(i,a,n)),u.unmap();let l=this.backend.device.createCommandEncoder();l.copyBufferToBuffer(u,0,o.gpuData.buffer,0,s),this.backend.device.queue.submit([l.finish()]),u.destroy(),te("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`)}memcpy(t,r){let i=this.storageCache.get(t);if(!i)throw Error("source gpu data for memcpy does not exist");let a=this.storageCache.get(r);if(!a)throw Error("destination gpu data for memcpy does not exist");if(i.originalSize!==a.originalSize)throw Error("inconsistent source and destination gpu data size");let n=ta(i.originalSize),s=this.backend.getCommandEncoder();this.backend.endComputePass(),s.copyBufferToBuffer(i.gpuData.buffer,0,a.gpuData.buffer,0,n)}registerExternalBuffer(t,r,i){let a;if(i){if(a=i[0],t===i[1])return te("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${a}, buffer is the same, skip.`),a;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw Error(`Registering a different external buffer under graph capture mode is not supported yet.
10             Please use the previous external buffer!`)}else a=to();return this.storageCache.set(a,{gpuData:{id:a,type:0,buffer:t},originalSize:r}),te("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${a}, registered.`),a}unregisterExternalBuffer(t){void 0!==t&&(this.storageCache.delete(t),te("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${t}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let i=tn(t),a,n=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,s=(r&GPUBufferUsage.UNIFORM)===GP
10UBufferUsage.UNIFORM;if(n||s){let t=(n?this.freeBuffers:this.freeUniformBuffers).get(i);a=t&&t.length>0?t.pop():this.backend.device.createBuffer({size:i,usage:r})}else a=this.backend.device.createBuffer({size:i,usage:r});let o={id:to(),type:0,buffer:a};return this.storageCache.set(o.id,{gpuData:o,originalSize:Number(t)}),te("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${o.id}`),o}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r="bigint"==typeof t?Number(t):t,i=this.storageCache.get(r);if(!i){if(0===this.storageCache.size)return 0;throw Error("releasing data does not exist")}return te("verbose",()=>`[WebGPU] GpuDataManager.release(id=${r}), gpuDataId=${i.gpuData.id}`),this.storageCache.delete(r),this.buffersPending.push(i.gpuData.buffer),i.originalSize}async download(t,r){let i=this.storageCache.get(Number(t));if(!i)throw Error("data does not exist");await tu(this.backend,i.gpuData.buffer,i.originalSize,r)}refreshPendingBuffers(){if(0!==this.buffersPending.length){if("default"===this.backend.sessionStatus){for(let t of this.buffersPending){let r=tr.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let i=this.freeBuffers.get(t.size)||[];void 0===r||i.length>=r?t.destroy():i.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let i=this.freeUniformBuffers.get(t.size)||[];void 0===r||i.length>=r?t.destroy():i.push(t)}else t.destroy()}this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);for(let r of(t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t)),this.buffersPending))t.push(r);this.buffersPending=[]}}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(t=>{t.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.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(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(t=>{t.destroy()}),this.capturedPendingBuffers.delete(t)),this.sessionCount-=1,0===this.sessionCount&&(te("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.storageCache=new Map)}},td=(...t)=>new tl(...t)}),oZ=W(()=>{tp=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},tc=t=>new tp(t)}),oX=W(()=>{th=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},tf=class{static calcShape(t,r,i=!1){let a=t.length,n=r.length;if(0===a)return r;if(0===n)return t;let s=Math.max(t.length,r.length),o=Array(s);if(i){if(a<2||n<2)return;let i=th.calcMatMulShape([t[a-2],t[a-1]],[r[n-2],r[n-1]]);if(void 0===i)return;[o[s-2],o[s-1]]=i}for(let u=i?3:1;u<=s;u++){let i=a-u<0?1:t[a-u],l=n-u<0?1:r[n-u];if(i!==l&&i>1&&l>1)return;let d=Math.max(i,l);if(i&&l)o[s-u]=Math.max(i,l);else{if(d>1)return;o[s-u]=0}}return o}static isValidBroadcast(t,r){let i=t.length,a=r.length;if(i>a)return!1;for(let n=1;n<=i;n++)if(1!==t[i-n]&&t[i-n]!==r[a-n])return!1;return!0}},tm=class t{static size(r){return t.getSizeFromDimensionRange(r,0,r.length)}static convertShape(t,r=4){let i=t.length;if(0===i)return[];let a=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 Error("cannot convert shape");a[n]=1,r/=t[n],n--}for(n--;n>=0;n--)a[n]=t[n];return a}static sizeFromDimension(r,i){if(i<0||i>r.length)throw Error(`invalid dimension of ${i} for sizeFromDimension as Tensor has ${r.length} dimensions.`);return t.getSizeFromDimensionRange(r,i,r.length)}static sizeToDimension(r,i){if(i<0||i>r.length)throw Error(`invalid dimension of ${i} for sizeToDimension as Tensor has ${r.length} dimensions.`);return t.getSizeFromDimensionRange(r,0,i)}static getSizeFromDimensionRange(t,r,i){let a=1;for(let n=r;n<i;n++){if(t[n]<0)throw 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(0===r)return[];if(1===r)return[1];let i=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 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(r=>t[r]):t.slice().reverse()}static padShape(t,r){let i=t.length;return t.map((t,a)=>t+r[a]+r[a+i])}static areEqual(t,r){return t.length===r.length&&t.every((t,i)=>t===r[i])}},tg=class t{static adjustPoolAttributes(t,r,i,a,n,s){if(!t&&i.length!==r.length-2)throw Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let t=0;t<r.length-2;t++)t>=i.length?i.push(r[t+2]):i[t]=r[t+2];for(let t=0;t<i.length;t++)if(t<a.length){if(a[t]<0)throw Error("strides should be greater than or equal to 1")}else a.push(1);for(let t=0;t<i.length;t++)if(t<n.length){if(n[t]<0)throw Error("dilations should be greater than or equal to 1")}else n.push(1);for(let t=0;t<2*i.length;t++)if(t<s.length){if(s[t]<0)throw Error("pad should be greater than or equal to 1")}else s.push(0);for(let t=0;t<i.length;t++){if(i[t]<=0)throw Error("kernel shapes need to be greater than 0");if(s[t]>=i[t]||s[t+i.length]>=i[t])throw Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(r,i,a,n,s,o,u){if(u){if(s.length!==2*(r.length-2))throw Error("length of pads should be twice the length of data dimensions");if(i.length!==r.length-2)throw Error("length of strides should be the length of data dimensions");if(n.length!==r.length-2)throw Error("length of kernel shapes should be the length of data dimensions");for(let l=0;l<r.length-2;l++)t.adjustPadAndReturnShape(r[l+(o?1:2)],i[l],a[l],n[l],s,l,l+r.length-2,u)}}static computePoolOutputShape(r,i,a,n,s,o,u){if(i.length<=0)throw Error("input shape must be of size greater than 0");let l=[i[0],i[1]];return t.computeShapeHelper(r,i,l,a,n,s,o,u),l}static computeConvOutputShape(r,i,a,n,s,o,u){if(r.length<=0||i.length<=0)throw Error("invalid input tensor dims or invalid filter tensor dims");let l=[r[0],i[0]];return t.computeShapeHelper(!1,r,l,a,n,s,o,u),l}static computeShapeHelper(r,i,a,n,s,o,u,l){if(r)for(let t=0;t<i.length-2;t++)a.push(1);else for(let r=0;r<i.length-2;r++)a.push(t.adjustPadAndReturnShape(i[r+2],n[r],s[r],o[r],u,r,r+i.length-2,l))}static adjustPadAndReturnShape(t,r,i,a,n,s,o,u){let l=i*(a-1)+1;if(!u||"NOTSET"===u)return Math.floor((t+n[s]+n[o]-l)/r+1);switch(u){case"VALID":return n[s]=0,n[o]=0,Math.floor((t-l)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(1!==i)throw Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let i=((t+r-1)/r-1)*r+a-t;return n[s]=Math.floor("SAME_LOWER"===u?(i+1)/2:i/2),n[o]=i-n[s],Math.floor((t+i-a)/r+1)}default:throw Error("Unsupported AutoPad type")}}},ty=class{static getShapeOfGemmResult(t,r,i,a,n){let s,o,u;if(2!==t.length||2!==i.length)throw Error("shape need to be of size 2");r?(s=t[1],o=t[0]):(s=t[0],o=t[1]);let l=-1;if(a?(u=i[0],l=1):(u=i[1],l=0),i[l]!==o)throw Error("dimension mismatch");if(s<=0||u<=0||o<=0)throw Error("invalid shape specified");if(n&&!tf.isValidBroadcast(n,[s,u]))throw Error("gemm: invalid bias shape for broadcast");return[s,u,o]}},t_=-34028234663852886e22,t$=34028234663852886e22}),oY=W(()=>{oG(),oX(),tb=64,tv=(t,r)=>{if(3===r)throw Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(t)){case 10:return r>1?`vec${r}<f16>`:"f16";case 1:return r>1?`vec${r}<f32>`:"f32";case 6:return r>1?`vec${r}<i32>`:"i32";case 12:return r>1?`vec${r}<u32>`:"u32";case 7:if(r>1)throw Error("currently not supported vecX of uint64 yet");return["vec2<u32>","i32"];case 13:if(r>1)throw Error("currently not supported vecX of uint64 yet");return["vec2<u32>","u32"];case 9:if(4!==r)throw Error("bool must be vec4");return["u32","vec4<bool>"];case 22:return"i32";case 21:return"u32";default:throw Error(`Unknown data type: ${t}`)}},tw=(t,r=1)=>{let i=tv(t,r);return"string"==typeof i?i:i[0]},tx=(t,r=1)=>{let i=tv(t,r);return"string"==typeof i?i:i[1]},tS=(...t)=>{let r=[];return t.forEach(t=>{0!==t.length&&r.push({type:12,data:t},{type:12,data:tm.computeStrides(t)})}),r},tk=t=>t%4==0?4:t%2==0?2:1,tT=(t="f32",r,i="0")=>r&&1!==r?`vec${r}<${t}>(${i})`:`${t}(${i})`,tC=(t,r,i)=>"f32"===t?i:1===r?`f32(${i})`:`vec${r}<f32>(${i})`,tI=(t,r)=>4===r?`(${t}.x + ${t}.y + ${t}.z + ${t}.w)`:2===r?`(${t}.x + ${t}.y)`:3===r?`(${t}.x + ${t}.y + ${t}.z)`:t,tE=(t,r,i,a)=>t.startsWith("uniforms.")&&i>4?"string"==typeof r?"f16"===a?`${t}[(${r}) / 8][(${r}) % 8 / 4][(${r}) % 8 % 4]`:`${t}[(${r}) / 4][(${r}) % 4]`:"f16"===a?`${t}[${Math.floor(r/8)}][${Math.floor(r%8/4)}][${r%8%4}]`:`${t}[${Math.floor(r/4)}][${r%4}]`:i>1?`${t}[${r}]`:t,tz=(t,r,i,a,n)=>{let s="number"==typeof i,o=s?i:i.length,u=[...Array(o).keys()],l=o<2?"u32":o<=4?`vec${o}<u32>`:`array<u32, ${o}>`,d=tv(r,n),p="string"==typeof d?d:d[1],c={indices:l,value:p,storage:"string"==typeof d?d:d[0],tensor:r},h=t=>"string"==typeof t?t:`${t}u`,f={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},m=s?"uniforms.":"",g=`${m}${t}_shape`,y=`${m}${t}_strides`,_="";for(let t=0;t<o-1;t++)_+=`
11    let dim${t} = current / ${tE(y,t,o)};
12    let rest${t} = current % ${tE(y,t,o)};
13    indices[${t}] = dim${t};
14    current = rest${t};
15    `;_+=`indices[${o-1}] = current;`;let $=o<2?"":`
16  fn o2i_${t}(offset: u32) -> ${c.indices} {
17    var indices: ${c.indices};
18    var current = offset;
19    ${_}
20    return indices;
21  }`,b=[];if(o>=2)for(let t=o-1;t>=0;t--)b.push(`${tE(y,t,o)} * (indices[${t}])`);let v=o<2?"":`
22  fn i2o_${t}(indices: ${c.indices}) -> u32 {
23    return ${b.join("+")};
24  }`,w=(...t)=>0===o?"0u":`${c.indices}(${t.map(h).join(",")})`,x=(t,r)=>o<2?`${t}`:`${tE(t,r,o)}`,S={},k=(r,i)=>(()=>
24{if(c.storage===c.value)return`${t}[${r}]=${i};`;if("vec2<u32>"===c.storage&&"i32"===c.value)return`${t}[${r}]=vec2<u32>(u32(${i}), select(0u, 0xFFFFFFFFu, ${i} < 0));`;if("vec2<u32>"===c.storage&&"u32"===c.value)return`${t}[${r}]=vec2<u32>(u32(${i}), 0u);`;if("u32"===c.storage&&"vec4<bool>"===c.value)return`${t}[${r}]=dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(${i}));`;throw Error(`not supported combination of storage type ${c.storage} and value type ${c.value} yet`)})(),T=r=>(()=>{if(c.storage===c.value)return`${t}[${r}]`;if("vec2<u32>"===c.storage&&"i32"===c.value)return`i32(${t}[${r}].x)`;if("vec2<u32>"===c.storage&&"u32"===c.value)return`u32(${t}[${r}].x)`;if("u32"===c.storage&&"vec4<bool>"===c.value)return`vec4<bool>(bool(${t}[${r}] & 0xFFu), bool(${t}[${r}] & 0xFF00u), bool(${t}[${r}] & 0xFF0000u), bool(${t}[${r}] & 0xFF000000u))`;throw Error(`not supported combination of storage type ${c.storage} and value type ${c.value} yet`)})(),C=o<2?"":`
25  fn get_${t}ByIndices(indices: ${c.indices}) -> ${p} {
26    return ${T(`i2o_${t}(indices)`)};
27  }`,I=o<2?"":(()=>{let r=u.map(t=>`d${t}: u32`).join(", "),i=u.map(t=>`d${t}`).join(", ");return`
28  fn get_${t}(${r}) -> ${p} {
29    return get_${t}ByIndices(${w(i)});
30  }`})(),E=o<2?"":`
31  fn set_${t}ByIndices(indices: ${c.indices}, value: ${p}) {
32    ${k(`i2o_${t}(indices)`,"value")}
33  }`,z=o<2?"":(()=>{let r=u.map(t=>`d${t}: u32`).join(", "),i=u.map(t=>`d${t}`).join(", ");return`
34  fn set_${t}(${r}, value: ${p}) {
35    set_${t}ByIndices(${w(i)}, value);
36  }`})();return{impl:()=>{let t=[],r=!1;return f.offsetToIndices&&(t.push($),r=!0),f.indicesToOffset&&(t.push(v),r=!0),f.broadcastedIndicesToOffset&&(Object.values(S).forEac
36h(r=>t.push(r)),r=!0),f.set&&(t.push(z),r=!0),f.setByIndices&&(t.push(E),r=!0),f.get&&(t.push(I),r=!0),f.getByIndices&&(t.push(C),r=!0),!s&&r&&t.unshift(`const ${g} = ${c.indices}(${i.join(",")});`,`const ${y} = ${c.indices}(${tm.computeStrides(i).join(",")});`),t.join(`
37`)},type:c,offsetToIndices:r=>(f.offsetToIndices=!0,o<2?r:`o2i_${t}(${r})`),indicesToOffset:r=>(f.indicesToOffset=!0,o<2?r:`i2o_${t}(${r})`),broadcastedIndicesToOffset:(r,i)=>{f.broadcastedIndicesToOffset=!0;let a=`${i.name}broadcastedIndicesTo${t}Offset`;if(a in S)return`${a}(${r})`;let n=[];for(let t=o-1;t>=0;t--){let r=i.indicesGet("outputIndices",t+i.rank-o);n.push(`${x(y,t)} * (${r} % ${x(g,t)})`)}return S[a]=`fn ${a}(outputIndices: ${i.type.indices}) -> u32 {
38             return ${n.length>0?n.join("+"):"0u"};
39           }`,`${a}(${r})`},indices:w,indicesGet:x,indicesSet:(t,r,i)=>o<2?`${t}=${i};`:`${tE(t,r,o)}=${i};`,set:(...r)=>{if(r.length!==o+1)throw Error(`indices length must be ${o}`);let i=r[o];if("string"!=typeof i)throw Error("value must be string");let a=r.slice(0,o).map(h).join(",");return 0===o?k("0u",i):1===o?k(a[0],i):(f.set=!0,f.setByIndices=!0,f.indicesToOffset=!0,`set_${t}(${a}, ${i})`)},setByOffset:k,setByIndices:(r,i)=>o<2?k(r,i):(f.setByIndices=!0,f.indicesToOffset=!0,`set_${t}ByIndices(${r}, ${i});`),get:(...r)=>{if(r.length!==o)throw Error(`indices length must be ${o}`);let i=r.map(h).join(",");return 0===o?T("0u"):1===o?T(i[0]):(f.get=!0,f.getByIndices=!0,f.indicesToOffset=!0,`get_${t}(${i})`)},getByOffset:T,getByIndices:r=>o<2?T(r):(f.getByIndices=!0,f.indicesToOffset=!0,`get_${t}ByIndices(${r})`),usage:a,name:t,strides:y,shape:g,rank:o}},tA=(t,r,i,a=1)=>tz(t,r,i,"input",a),tO=(t,r,i,a=1)=>tz(t,r,i,"output",a),tR=(t,r,i)=>tz(t,r,i,"atomicOutput",1),tB=(t,r,i,a=1)=>tz(t,r,i,"internal",a),tN=class{constructor(t,r){this.normalizedDispatchGroup=t,this.limits=r,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${"number"==typeof t?`${t}u`:t}) { return; }`}mainStart(t=tb){let r="number"==typeof t?t:t[0],i="number"==typeof t?1:t[1],a="number"==typeof t?1:t[2];if(r>this.limits.maxComputeWorkgroupSizeX||i>this.limits.maxComputeWorkgroupSizeY||a>this.limits.maxComputeWorkgroupSizeZ)throw Error(`workgroup size [${r}, ${i}, ${a}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(r*i*a>this.limits.maxComputeInvocationsPerWorkgroup)throw Error(`workgroup size [${r}, ${i}, ${a}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let n=1===this.normalizedDispatchGroup[1]&&1===this.normalizedDispatchGroup[2],s=n?`@builtin(global_invocation_id) global_id : vec3<u32>,
40    @builtin(workgroup_id) workgroup_id : vec3<u32>,
41    @builtin(local_invocation_index) local_idx : u32,
42    @builtin(local_invocation_id) local_id : vec3<u32>`:`@builtin(global_invocation_id) global_id : vec3<u32>,
43                                             @builtin(local_invocation_id) local_id : vec3<u32>,
44    @builtin(local_invocation_index) local_idx : u32,
45    @builtin(workgroup_id) workgroup_id : vec3<u32>,
46    @builtin(num_workgroups) num_workgroups : vec3<u32>`,o=n?`let global_idx = global_id.x;
47         let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] +
48             workgroup_id.y * num_workgroups[0] + workgroup_id.x;
49         let global_idx = workgroup_index * ${r*i*a}u + local_idx;`;return`@compute @workgroup_size(${r}, ${i}, ${a})
50  fn main(${s}) {
51    ${o}
52  `}appendVariableUniforms(t){0!==t.rank&&(t.shape.startsWith("uniforms.")&&this.uniforms.push({name:t.shape.replace("uniforms.",""),type:"u32",length:t.rank}),t.strides.startsWith("uniforms.")&&this.uniforms.push({name:t.strides.replace("uniforms.",""),type:"u32",length:t.rank}))}declareVariable(t,r){if("internal"===t.usage)throw Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(t),this.appendVariableUniforms(t);let i="input"===t.usage?"read":"read_write",a="atomicOutput"===t.usage?"atomic<i32>":t.type.storage;return`@group(0) @binding(${r}) var<storage, ${i}> ${t.name}: array<${a}>;`}declareVariables(...t){return t.map(t=>this.declareVariable(t,this.variableIndex++)).join(`
53`)}registerInternalVariable(t){if("internal"!==t.usage)throw Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(t),this.appendVariableUniforms(t)}registerInternalVariables(...t){return t.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(t,r,i=1){return this.uniforms.push({name:t,type:r,length:i}),this}registerUniforms(t){return this.uniforms=this.uniforms.concat(t),this}uniformDeclaration(){if(0===this.uniforms.length)return"";let t=[];for(let{name:r,type:i,length:a}of this.uniforms)if(a&&a>4)"f16"===i?t.push(`@align(16) ${r}:array<mat2x4<${i}>, ${Math.ceil(a/8)}>`):t.push(`${r}:array<vec4<${i}>, ${Math.ceil(a/4)}>`);else{let n=null==a||1===a?i:`vec${a}<${i}>`;t.push(`${r}:${n}`)}return`
54      struct Uniforms { ${t.join(", ")} };
55      @group(0) @binding(${this.variableIndex}) var<uniform> uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(t=>t.impl()).join(`
56`)+this.internalVariables.map(t=>t.impl()).join(`
57`)}get variablesInfo(){if(0===this.uniforms.length)return;let t=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(r=>[t(r.type),r.length??1])}},tM=(t,r)=>new tN(t,r)}
57),oJ=W(()=>{oG(),oX(),oZ(),oY(),tD=(t,r)=>{if(!t||1!==t.length)throw Error("Transpose requires 1 input.");if(0!==r.length&&r.length!==t[0].dims.length)throw Error(`perm size ${r.length} does not match input rank ${t[0].dims.length}`)},tP=(t,r)=>0!==r.length?r:[...Array(t).keys()].reverse(),tU=(t,r)=>tm.sortBasedOnPerm(t,tP(t.length,r)),tW=(t,r,i,a)=>{let n=`fn perm(i: ${a.type.indices}) -> ${i.type.indices} {
58    var a: ${i.type.indices};`;for(let i=0;i<r;++i)n+=`a[${t[i]}]=i[${i}];`;return n+"return a;}"},tq=(t,r)=>{let i=[],a=[];for(let n=0;n<t.length;++n)1!==t[n]&&i.push(t[n]),1!==t[r[n]]&&a.push(r[n]);return{newShape:i,newPerm:a}},tL=(t,r)=>{let i=0;for(let a=0;a<t.length;++a)if(1!==r[t[a]]){if(t[a]<i)return!1;i=t[a]}return!0},tV=(t,r)=>{let i=t.dataType,a=t.dims.length,n=tP(a,r),s=tU(t.dims,n),o=t.dims,u=s;if(a<2||tL(n,t.dims))return{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let r=tm.size(s);return{outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(r/64/4)},programUniforms:[{type:12,data:Math.ceil(r/4)}]}},getShaderSource:t=>{let r=tA("input",i,o,4),a=tO("output",i,u,4);return`
59  ${t.registerUniform("output_size","u32").declareVariables(r,a)}
60  ${t.mainStart()}
61    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
62    output[global_idx] = input[global_idx];
63  }`}};let{newShape:l,newPerm:d}=tq(t.dims,n),p=tm.areEqual(d,[2,3,1]),c=tm.areEqual(d,[3,1,2]);return 2===l.length||p||c?(u=[(o=p?[l[0],l[1]*l[2]]:c?[l[0]*l[1],l[2]]:l)[1],o[0]],{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let r=tm.size(s);return{outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(u[1]/16),y:Math.ceil(u[0]/16)},programUniforms:[{type:12,data:r},...tS(o,u)]}},getShaderSource:t=>{let r=tA("a",i,o.length),a=tO("output",i,u.length);return`
64  ${t.registerUniform("output_size","u32").declareVariables(r,a)}
65  var<workgroup> tile : array<array<${a.type.value}, 17>, 16>;
66  ${t.mainStart([16,16,1])}
67    let stride = (uniforms.output_shape[1] - 1) / 16 + 1;
68    let workgroup_id_x = workgroup_index % stride;
69    let workgroup_id_y = workgroup_index / stride;
70    let input_col = workgroup_id_y * 16u + local_id.x;
71    let input_row = workgroup_id_x * 16u + local_id.y;
72    if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) {
73      tile[local_id.y][local_id.x] = ${r.getByIndices(`${r.type.indices}(input_row, input_col)`)};
74    }
75    workgroupBarrier();
76
77    let output_col = workgroup_id_x * 16u + local_id.x;
78    let output_row = workgroup_id_y * 16u + local_id.y;
79    if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) {
80      ${a.setByIndices(`${a.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")}
81    }
82  }`}}):{name:"Transpose",shaderCache:{hint:`${r}`,inputDependencies:["rank"]},getRunData:()=>{let r=tm.size(s);return{outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:[{type:12,data:r},...tS(o,u)]}},getShaderSource:t=>{let r=tA("a",i,o.length),s=tO("output",i,u.length);return`
83  ${t.registerUniform("output_size","u32").declareVariables(r,s)}
84
85  ${tW(n,a,r,s)}
86
87  ${t.mainStart()}
88    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
89
90    let indices = ${s.offsetToIndices("global_idx")};
91    let aIndices = perm(indices);
92
93    ${s.setByOffset("global_idx",r.getByIndices("aIndices"))}
94  }`}}},tG=(t,r)=>{tD(t.inputs,r.perm),t.compute(tV(t.inputs[0],r.perm))},tH=t=>tc({perm:t.perm})}),o0=W(()=>{oG(),oX(),oY(),o1(),oJ(),tF={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"},tj={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"},tK={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"
940",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},tQ={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},tZ=(t,r)=>{let i=[];for(let a=r-t;a<r;++a)i.push(a);return i},tX=(t,r)=>{let i=[],a=t.length;for(let n=0;n<a;n++)-1===r.indexOf(n)&&i.push(t[n]);return[i,r.map(r=>t[r])]},tY=(t,r)=>{let i=t.length+r.length,a=[],n=0;for(let s=0;s<i;s++)-1===r.indexOf(s)?a.push(t[n++]):a.push(1);return a},tJ=(t,r)=>{for(let i=0;i<t.length;++i)if(t[t.length-i-1]!==r-1-i)return!1;return!0},t0=(t,r)=>{let i=[];if(!tJ(t,r)){for(let a=0;a<r;++a)-1===t.indexOf(a)&&i.push(a);t.forEach(t=>i.push(t))}return i},t1=(t,r,i,a,n,s,o)=>{let u=i[0].dims,l=tm.size(s),d=tm.size(o),p=tA("_A",i[0].dataType,u),c=tO("output",n,s),h=64;1===l&&(h=256);let f=`
95          var<workgroup> aBestValues : array<f32, ${h}>;
96       `;return{name:t,shaderCache:{hint:`${r};${h}`,inputDependencies:["type"]},getShaderSource:t=>`
97        ${t.registerUniform("reduceSize","u32").declareVariables(p,c)}
98        ${f}
99        fn DIV_CEIL(a : u32, b : u32) -> u32 {
100          return ((a - 1u) / b + 1u);
101         }
102         ${t.mainStart(h)}
103
104          let outputIndex = global_idx / ${h};
105          let offset = outputIndex * uniforms.reduceSize;
106
107          var bestValue = f32(${tK[a]});
108          let Length = uniforms.reduceSize;
109          for (var k = local_idx; k < Length; k = k + ${h}) {
110           let candidate = f32(${p.getByOffset("offset + k")});
111           bestValue = ${tF[a]};
112          }
113          aBestValues[local_idx] = bestValue;
114          workgroupBarrier();
115
116         var reduceSize = min(Length, ${h}u);
117         for (var currentSize = reduceSize / 2u; reduceSize > 1u;
118             currentSize = reduceSize / 2u) {
119           let interval = DIV_CEIL(reduceSize, 2u);
120           if (local_idx < currentSize) {
121            let candidate = aBestValues[local_idx + interval];
122            bestValue = ${tj[a]};
123            aBestValues[local_idx] = bestValue;
124           }
125           reduceSize = interval;
126           workgroupBarrier();
127         }
128
129         if (local_idx == 0u) {
130          ${c.setByOffset("outputIndex",`${"mean"===a?`${c.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${c.type.storage}(${tQ[a]})`}`)};
131         }
132        }`,getRunData:()=>({outputs:[{dims:s,dataType:n}],dispatchGroup:{x:l},programUniforms:[{type:12,data:d}]})}},t2=(t,r,i,a)=>{let n=1===t.inputs.length?i:rs(t.inputs,i),s=n.axes;0!==s.length||n.noopWithEmptyAxes||(s=t.inputs[0].dims.map((t,r)=>r));let o=tm.normalizeAxes(s,t.inputs[0].dims.length),u=o,l=t.inputs[0],d=t0(u,t.inputs[0].dims.length);d.length>0&&(l=t.compute(tV(t.inputs[0],d),{inputs:[0],outputs:[-1]})[0],u=tZ(u.length,l.dims.length));let[p,c]=tX(l.dims,u),h=p;n.keepDims&&(h=tY(p,o)),t.compute(t1(r,n.cacheKey,[l],a,t.inputs[0].dataType,h,c),{inputs:[l]})},t3=(t,r)=>{t2(t,"ReduceMeanShared",r,"mean")},t4=(t,r)=>{t2(t,"ReduceL1Shared",r,"l1")},t6=(t,r)=>{t2(t,"ReduceL2Shared",r,"l2")},t8=(t,r)=>{t2(t,"ReduceLogSumExpShared",r,"logSumExp")},t5=(t,r)=>{t2(t,"ReduceMaxShared",r,"max")},t9=(t,r)=>{t2(t,"ReduceMinShared",r,"min")},t7=(t,r)=>{t2(t,"ReduceProdShared",r,"prod")},re=(t,r)=>{t2(t,"ReduceSumShared",r,"sum")},rt=(t,r)=>{t2(t,"ReduceSumSquareShared",r,"sumSquare")},rr=(t,r)=>{t2(t,"ReduceLogSumShared",r,"logSum")}}),o1=W(()=>{oG(),oX(),oZ(),oY(),o0(),ri=t=>{if(!t||0===t.length||t.length>2)throw Error("Reduce op requires 1 or 2 inputs.");if(2===t.length&&1!==t[1].dims.length)throw Error("Invalid axes input dims.")},ra=t=>["","",`var value = ${t.getByIndices("input_indices")};`,""],rn=(t,r,i,a,n,s,o=!1,u=!1)=>{let l=[],d=i[0].dims,p=d.length,c=tm.normalizeAxes(n,p),h=!u&&0===c.length;d.forEach((t,r)=>{h||c.indexOf(r)>=0?o&&l.push(1):l.push(t)});let f=l.length,m=tm.size(l);return{name:t,shaderCache:r,getShaderSource:t=>{let r=[],n=tA("_A",i[0].dataType,p),u=tO("output",s,f),l=a(n,u,c),m=l[2];for(let t=0,i=0;t<p;t++)h||c.indexOf(t)>=0?(o&&i++,m=`for(var j${t}: u32 = 0; j${t} < ${d[t]}; j${t}++) {
133                  ${l[2].includes("last_index")?`let last_index = j${t};`:""}
134                  ${n.indicesSet("input_indices",t,`j${t}`)}
135                  ${m}
136                }`):(r.push(`${n.indicesSet("input_indices",t,u.indicesGet("output_indices",
136i))};`),i++);return`
137
138        ${t.registerUniform("output_size","u32").declareVariables(n,u)}
139
140        ${t.mainStart()}
141          ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
142          var input_indices: ${n.type.indices};
143          let output_indices = ${u.offsetToIndices("global_idx")};
144
145          ${r.join(`
146`)}
147          ${l[0]}       // init ops for reduce max/min
148          ${l[1]}
149          ${m}
150          ${l[3]}
151          ${4===l.length?u.setByOffset("global_idx","value"):l.slice(4).join(`
152`)}
153        }`},getRunData:()=>({outputs:[{dims:l,dataType:s}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...tS(d,l)]})}},rs=(t,r)=>{let i=[];return t[1].dims[0]>0&&t[1].getBigInt64Array().forEach(t=>i.push(Number(t))),tc({axes:i,keepDims:r.keepDims,noopWithEmptyAxes:r.noopWithEmptyAxes})},ro=(t,r,i,a)=>{let n=t.inputs,s=1===n.length?i:rs(n,i);t.compute(rn(r,{hint:s.cacheKey,inputDependencies:["rank"]},[n[0]],s.noopWithEmptyAxes&&0===s.axes.length?ra:a,s.axes,n[0].dataType,s.keepDims,s.noopWithEmptyAxes),{inputs:[0]})},ru=(t,r)=>{ri(t.inputs),ro(t,"ReduceLogSum",r,(t,r)=>[`var value = ${r.type.storage}(0);`,"",`value += ${t.getByIndices("input_indices")};`,"value = log(value);"])},rl=(t,r)=>{ri(t.inputs),ro(t,"ReduceL1",r,(t,r)=>[`var value = ${r.type.storage}(0);`,"",`value += abs(${t.getByIndices("input_indices")});`,""])},rd=(t,r)=>{ri(t.inputs),ro(t,"ReduceL2",r,(t,r)=>[`var t = ${r.type.value}(0); var value = ${r.type.value}(0);`,"",`t = ${t.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},rp=(t,r)=>{ri(t.inputs),ro(t,"ReduceLogSumExp",r,(t,r)=>[`var value = ${r.type.storage}(0);`,"",`value += exp(${t.getByIndices("input_indices")});`,"value = log(value);"])},rc=(t,r)=>{ri(t.inputs),ro(t,"ReduceMax",r,(t,r,i)=>{let a=[];for(let r=0;r<t.rank;r++)(i.indexOf(r)>=0||0===i.length)&&a.push(t.indicesSet("input_indices",r,0));return[`${a.join(`
154`)}`,`var value = ${t.getByIndices("input_indices")};`,`value = max(value, ${t.getByIndices("input_indices")});`,""]})},rh=(t,r)=>{ri(t.inputs),ro(t,"ReduceMean",r,(r,i,a)=>{let n=1;for(let i=0;i<r.rank;i++)(a.indexOf(i)>=0||0===a.length)&&(n*=t.inputs[0].dims[i]);return["var sum = f32(0);","",`sum += f32(${r.getByIndices("input_indices")});`,`let value = ${i.type.value}(sum / ${n});`]})},rf=(t,r)=>{ri(t.inputs),ro(t,"ReduceMin",r,(t,r,i)=>{let a=[];for(let r=0;r<t.rank;r++)(i.indexOf(r)>=0||0===i.length)&&a.push(`input_indices[${r}] = 0;`);return[`${a.join(`
155`)}`,`var value = ${t.getByIndices("input_indices")};`,`value = min(value, ${t.getByIndices("input_indices")});`,""]})},rm=(t,r)=>{ri(t.inputs),ro(t,"ReduceProd",r,(t,r)=>[`var value = ${r.type.storage}(1);`,"",`value *= ${t.getByIndices("input_indices")};`,""])},rg=(t,r)=>{ri(t.inputs),ro(t,"ReduceSum",r,(t,r)=>[`var value = ${r.type.storage}(0);`,"",`value += ${t.getByIndices("input_indices")};`,""])},ry=(t,r)=>{ri(t.inputs),ro(t,"ReduceSumSquare",r,(t,r)=>[`var t = ${r.type.value}(0); var value = ${r.type.value}(0);`,"",`t = ${t.getByIndices("input_indices")}; value += t * t;`,""])},r_=(t,r,i)=>{if(0===r.length)return i;let a=1,n=1;for(let i=0;i<r.length;i++)-1===r.indexOf(i)?a*=t[i]:n*=t[i];return n<32&&a>1024},r$=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rh(t,r):t3(t,r)},rb=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rl(t,r):t4(t,r)},rv=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rd(t,r):t6(t,r)},rw=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rp(t,r):t8(t,r)},rx=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rc(t,r):t5(t,r)},rS=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rf(t,r):t9(t,r)},rk=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rm(t,r):t7(t,r)},rT=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?rg(t,r):re(t,r)},rC=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?ry(t,r):rt(t,r)},rI=(t,r)=>{r_(t.inputs[0].dims,r.axes,r.noopWithEmptyAxes)?ru(t,r):rr(t,r)}}),o2=W(()=>{oG(),oZ(),o1(),rE=t=>{if(!t||0===t.length||t.length>2)throw Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(1!==t[0].dataType)throw Error("Invalid input type.")},rz=(t,r)=>{rE(t.inputs),t.compute(rn("ArgMin",{hint:r.cacheKey,inputDependencies:["rank"]},[t.inputs[0]],(t,i,a)=>{let n=[];for(let r=0;r<t.rank;r++)(a.indexOf(r)>=0||0===a.length)&&n.push(`input_indices[${r}] = 0;`);return[`${n.join(`
156`)}`,`var value = ${t.getByIndices("input_indices")};
157var best_index : i32 = 0;`,`if (${t.getByIndices("input_indices")} ${r.selectLastIndex>0?"<=":"<"} value) {
158         value = ${t.getByIndices("input_indices")};
159         best_index = i32(last_index);
160       }`,"",i.setByOffset("global_idx","best_index")]},[r.axis],7,r.keepDims),{inputs:[0]})},rA=(t,r)=>{rE(t.inputs),t.compute(rn("argMax",{hint:r.cacheKey,inputDependencies:["rank"]},[t.inputs[0]],(t,i,a)=>{let n=[];for(let r=0;r<t.rank;r++)(a.indexOf(r)>=0||0===a.length)&&n.push(`input_indices[${r}] = 0;`);return[`${n.join(`
161`)}`,`var value = ${t.getByIndices("input_indices")};
162var best_index : i32 = 0;`,`if (${t.getByIndices("input_indices")} ${r.selectLastIndex>0?">=":">"} value) {
163         value = ${t.getByIndices("input_indices")};
164         best_index = i32(last_index);
165       }`,"",i.setByOffset("global_idx","best_index")]},[r.axis],7,r.keepDims),{inputs:[0]})},rO=t=>tc(t)}),o3=W(()=>{oG(),oX(),oK(),oY(),rR=(t,r)=>{let i=t[0],a=t[1],n=t[2],s=t[3],o=t[4],u=t[5];if(o&&u)throw Error("Attention cannot have both past and attention_bias");if(3!==i.dims.length)throw Error('Input "input" must have 3 dimensions');
165let l=i.dims[0],d=i.dims[1],p=i.dims[2];if(1!==n.dims.length)throw Error('Input "bias" is expected to have 1 dimensions');if(2!==a.dims.length)throw Error('Input "weights" is expected to have 2 dimensions');if(a.dims[0]!==p)throw Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(n.dims[0]!==a.dims[1])throw Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let c=n.dims[0]/3,h=c,f=h;if(r.qkvHiddenSizes.length>0){if(3!==r.qkvHiddenSizes.length)throw Error("qkv_hidden_sizes attribute should have 3 elements");for(let t of r.qkvHiddenSizes)if(t%r.numHeads!=0)throw Error("qkv_hidden_sizes should be divisible by num_heads");c=r.qkvHiddenSizes[0],h=r.qkvHiddenSizes[1],f=r.qkvHiddenSizes[2]}if(c!==h)throw Error("qkv_hidden_sizes first element should be same as the second");if(n.dims[0]!==c+h+f)throw Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let m=0;if(o){if(h!==f)throw Error('Input "past" expect k_hidden_size == v_hidden_size');if(5!==o.dims.length)throw Error('Input "past" must have 5 dimensions');if(2!==o.dims[0])throw Error('Input "past" first dimension must be 2');if(o.dims[1]!==l)throw Error('Input "past" second dimension must be batch_size');if(o.dims[2]!==r.numHeads)throw Error('Input "past" third dimension must be num_heads');if(o.dims[4]!==h/r.numHeads)throw Error('Input "past" fifth dimension must be k_hidden_size / num_heads');r.pastPresentShareBuffer||(m=o.dims[3])}let g=d+m;if(s)throw Error("Mask not supported");if(o)throw Error("past is not supported");if(u){if(4!==u.dims.length)throw Error('Input "attention_bias" must have 4 dimensions');if(u.dims[0]!==l||u.dims[1]!==r.numHeads||u.dims[2]!==d||u.dims[3]!==g)throw Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:l,sequenceLength:d,pastSequenceLength:m,kvSequenceLength:d,totalSequenceLength:g,maxSequenceLength:-1,inputHiddenSize:p,hiddenSize:c,vHiddenSize:f,headSize:Math.floor(c/r.numHeads),vHeadSize:Math.floor(f/r.numHeads),numHeads:r.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:r.maskFilterValue,maskType:0,scale:r.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},rB=(t,r,i)=>r&&t?`
166      let total_sequence_length_input = u32(${r.getByOffset("0")});
167      let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length);
168      let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input;
169      let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input;
170      total_sequence_length = u32(${t?.getByOffset("batchIdx")}) + 1;
171      var past_sequence_length: u32 = 0;
172      if (is_first_prompt == false) {
173        past_sequence_length = total_sequence_length - sequence_length;
174      }
175       `:`
176    ${i?"let past_sequence_length = uniforms.past_sequence_length":""};
177    let present_sequence_length = total_sequence_length;
178    `,rN=(t,r,i,a,n,s,o,u)=>{let l=tk(o?1:s),d=64,p=s/l;p<64&&(d=32);let c=[{type:12,data:r},{type:12,data:i},{type:12,data:a},{type:12,data:n},{type:12,data:p},{type:12,data:Math.ceil(s/l/d)}],h=tw(t.dataType,l),f=tx(1,l),m=["type"];return o&&m.push("type"),u&&m.push("type"),{name:"AttentionProbsSoftmax",shaderCache:{hint:`${d};${h};${l}`,inputDependencies:m},getShaderSource:r=>{let i=tO("x",t.dataType,t.dims,l),a=[i],n=o?tA("seq_lens",o.dataType,o.dims):void 0;n&&a.push(n);let s=u?tA("total_sequence_length_input",u.dataType,u.dims):void 0;s&&a.push(s);let p=tx(t.dataType);return`
179  var<workgroup> thread_max: array<f32, ${d}>;
180  var<workgroup> thread_sum: array<f32, ${d}>;
181  ${r.registerUniforms([{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}]).declareVariables(...a)}
182  ${r.mainStart([d,1,1])}
183    let batchIdx = workgroup_id.z / uniforms.num_heads;
184    let headIdx = workgroup_id.z % uniforms.num_heads;
185    let sequence_length = uniforms.sequence_length;
186    var total_sequence_length = uniforms.total_sequence_length;
187    ${rB(n,s,!1)}
188    let local_offset = local_idx * uniforms.elements_per_thread;
189    let offset = (global_idx / ${d}) * uniforms.total_sequence_length + local_offset;
190    let seq_causal_length = ${o?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"};
191    var thread_max_vector = ${f}(-3.402823e+38f);
192    for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
193      thread_max_vector = max(${f}(x[offset + i]), thread_max_vector);
194    }
195    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 Error(`Unsupported components: ${l}`)}})()};
196    workgroupBarrier();
197
198    var max_value =  f32(-3.402823e+38f);
199    for (var i = 0u; i < ${d}; i++) {
200      max_value = max(thread_max[i], max_value);
201    }
202
203    var sum_vector = ${f}(0);
204    for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
205      sum_vector += exp(${f}(x[offset + i]) - max_value);
206    }
207    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 Error(`Unsupported components: ${l}`)}})()};
208    workgroupBarrier();
209
210    var sum: f32 = 0;
211    for (var i = 0u; i < ${d}; i++) {
212      sum += thread_sum[i];
213    }
214
215    if (sum == 0) {
216      for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
217        x[offset + i] = ${i.type.value}(${p}(1.0) / ${p}
217(seq_causal_length));
218      }
219    } else {
220      for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
221        var f32input = ${f}(x[offset + i]);
222        x[offset + i] = ${i.type.value}(exp(f32input - max_value) / sum);
223      }
224    }
225      ${o?`
226        for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) {
227          x[offset + total_seq_id] = ${i.type.value}(${p}(0));
228        }`:""};
229  }`},getRunData:()=>({outputs:[],dispatchGroup:{x:Math.ceil(s/d),y:n,z:r*i},programUniforms:c})}},rM=(t,r,i,a,n,s,o,u,l)=>{let d=o+s.kvSequenceLength,p=[s.batchSize,s.numHeads,s.sequenceLength,d],c=t>1&&a,h=s.kvNumHeads?s.kvNumHeads:s.numHeads,f=c?[s.batchSize,h,d,s.headSize]:void 0,m=s.nReps?s.nReps:1,g=0===s.scale?1/Math.sqrt(s.headSize):s.scale,y=tk(s.headSize),_=s.headSize/y,$={x:Math.ceil(d/12),y:Math.ceil(s.sequenceLength/12),z:s.batchSize*s.numHeads},b=[{type:12,data:s.sequenceLength},{type:12,data:_},{type:12,data:d},{type:12,data:s.numHeads},{type:12,data:s.headSize},{type:1,data:g},{type:12,data:o},{type:12,data:s.kvSequenceLength},{type:12,data:m}],v=c&&a&&tm.size(a.dims)>0,w=["type","type"];v&&w.push("type"),n&&w.push("type"),u&&w.push("type"),l&&w.push("type");let x=[{dims:p,dataType:r.dataType,gpuDataType:0}];return c&&x.push({dims:f,dataType:r.dataType,gpuDataType:0}),{name:"AttentionProbs",shaderCache:{hint:`${y};${void 0!==n};${void 0!==a};${t}`,inputDependencies:w},getRunData:()=>({outputs:x,dispatchGroup:$,programUniforms:b}),getShaderSource:t=>{let s=tA("q",r.dataType,r.dims,y),o=[s,tA("key",i.dataType,i.dims,y)];if(v){let t=tA("past_key",a.dataType,a.dims,y);o.push(t)}n&&o.push(tA("attention_bias",n.dataType,n.dims));let d=u?tA("seq_lens",u.dataType,u.dims):void 0;d&&o.push(d);let h=l?tA("total_sequence_length_input",l.dataType,l.dims):void 0;h&&o.push(h);let g=tO("output",r.dataType,p),_=[g];c&&_.push(tO("present_key",r.dataType,f,y));let $=tx(1,y);return`
230  const TILE_SIZE = 12u;
231
232  var<workgroup> tileQ: array<${s.type.storage}, 144>;
233  var<workgroup> tileK: array<${s.type.storage}, 144>;
234  ${t.registerUniforms([{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}]).declareVariables(...o,..._)}
235  ${t.mainStart([12,12,1])}
236    // x holds the N and y holds the M
237    let headIdx = workgroup_id.z % uniforms.num_heads;
238    let kvHeadIdx = ${1===m?"headIdx":"headIdx / uniforms.n_reps"};
239    let kv_num_heads = ${1===m?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"};
240    let batchIdx = workgroup_id.z / uniforms.num_heads;
241    let m = workgroup_id.y * TILE_SIZE;
242    let n = workgroup_id.x * TILE_SIZE;
243    let sequence_length = uniforms.M;
244    var total_sequence_length = uniforms.N;
245    ${rB(d,h,!0)}
246    let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx;
247    let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;
248    ${v&&c?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""};
249    let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K;
250    ${c?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""}
251    var value = ${$}(0);
252    for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
253      if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) {
254        tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x];
255      }
256      if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) {
257        var idx = TILE_SIZE * local_id.y + local_id.x;
258      ${v&&c?`
259              if (n + local_id.y < past_sequence_length) {
260                tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x];
261              } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {
262                tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x];
263              }`:`
264          if (n + local_id.y < uniforms.kv_sequence_length) {
265            tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x];
266          }`}
267      ${c?`if (n + local_id.y < present_sequence_length) {
268        present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx];
269      }`:""}
270      }
271      workgroupBarrier();
272
273      for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) {
274          value += ${$}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]);
275      }
276
277      workgroupBarrier();
278    }
279
280    if (global_id.y < uniforms.M && global_id.x < total_sequence_length) {
281      let headOffset = workgroup_id.z * uniforms.M * uniforms.N;
282      let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x;
283      var sum: f32 = ${(()=>{switch(y){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw Error(`Unsupported components: ${y}`)}})()};
284        output[outputIdx] = ${g.type.value} (sum * uniforms.alpha) + ${n?"attention_bias[outputIdx]":"0.0"};
285    }
286  }`}}},rD=(t,r,i,a,n,s,o,u)=>{let l=s+n.kvSequenceLength,d=n.nReps?n.nReps:1,p=n.vHiddenSize*d,c=t>1&&a,h=n.kvNumHeads?n.kvNumHeads:n.numHeads,f=c?[n.batchSize,h,l,n.headSize]:void 0,m=[n.batchSize,n.sequenceLength,p],g={x:Math.ceil(n.vHeadSize/12),y:Math.ceil(n.sequenceLength/12),z:n.batchSize*n.numHeads},y=[{type:12,data:n.sequenceLength},{type:12,data:l},{type:12,data:n.vHeadSize},{type:12,data:n.numHeads},{type:12,data:n.headSize},{type:12,data:p},{type:12,data:s},{type:12,data:n.kvSequenceLength},{type:12,data:d}],_=c&&a&&tm.size(a.dims)>0,$=["type","type"];_&&$.push("type"),o&&$.push("type"),u&&$.push("type");let b=[{dims:m,dataType:r.dataType,gpuDataType:0}];return c&&b.push({dims:f,dataType:r.dataType,gpuDataType:0}),{name:"AttentionScore",shaderCache:{hint:`${void 0!==a};${t}`,inputDependencies:$},getRunData:()=>({outputs:b,dispatchGroup:g,programUniforms:y}),getShaderSource:t=>{let n=tA("probs",r.dataType,r.dims),s=[n,tA("v",i.dataType,i.dims)];_&&s.push(tA("past_value",a.dataType,a.dims));let l=o?tA("seq_lens",o.dataType,o.dims):void 0;o&&s.push(l);let p=u?tA("total_sequence_length_input",u.dataType,u.dims):void 0;u&&s.push(p);let h=[tO("output",r.dataType,m)];return c&&h.push(tO("present_value",r.dataType,f)),`
287  const TILE_SIZE = 12u;
288  var<workgroup> tileQ: array<${n.type.value}, 144>;
289  var<workgroup> tileV: array<${n.type.value}, 144>;
290  ${t.registerUniforms([{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}]).declareVariables(...s,...h)}
291  ${t.mainStart([12,12,1])}
292   let headIdx = workgroup_id.z % uniforms.num_heads;
293   let batchIdx = workgroup_id.z / uniforms.num_heads;
294   let kvHeadIdx = ${1===d?"headIdx":"headIdx / uniforms.n_reps"};
295   let kv_num_heads = ${1===d?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"};
296   let m = global_id.y;
297   let n = global_id.x;
298   let sequence_length = uniforms.M;
299   var total_sequence_length = uniforms.K;
300   ${rB(l,p,!0)}
301   let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;
302   let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch
303   ${_&&c?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""};
304   let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n;
305   ${c?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""}
306   var value = ${n.type.storage}(0);
307   for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
308      if (m < uniforms.M && w + local_id.x < uniforms.K) {
309        tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x];
310      }
311      if (n < uniforms.N && w + local_id.y < uniforms.K) {
312        var idx = TILE_SIZE * local_id.y + local_id.x;
313        ${_&&c?`
314        if (w + local_id.y < past_sequence_length) {
315          tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N];
316        } else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {
317          tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N];
318        }
319      `:`
320            if (w + local_id.y < uniforms.kv_sequence_length) {
321              tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N];
322            }`}
323        ${c?`
324            if (w + local_id.y < present_sequence_length) {
325          present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx];
326        }`:""}
327      }
328     workgroupBarrier();
329     for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) {
330       value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x];
331     }
332     workgroupBarrier();
333   }
334
335   // we need to transpose output from BNSH_v to BSND_v
336   if (m < uniforms.M && n < uniforms.N) {
337     let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size
338       + headIdx * uniforms.N + n;
339     output[outputIdx] = value;
340   }
341  }`}}},rP=(t,r,i,a,n,s,o,u,l,d,p,c)=>{let h=Math.min(t.outputCount,1+(o?1:0)+(u?1:0)),f=h>1?d.pastSequenceLength:0,m=f+d.kvSequenceLength,g=l&&tm.size(l.dims)>0?l:void 0,y=[r,i];h>1&&o&&tm.size(o.dims)>0&&y.push(o),g&&y.push(g),p&&y.push(p),c&&y.push(c);let _=t.compute(rM(h,r,i,o,g,d,f,p,c),{inputs:y,outputs:h>1?[-1,1]:[-1]})[0];t.compute(rN(_,d.batchSize,d.numHeads,f,d.sequenceLength,m,p,c),{inputs:p&&c?[_,p,c]:[_],outputs:[]});let $=[_,a];h>1&&u&&tm.size(u.dims)>
3410&&$.push(u),p&&$.push(p),c&&$.push(c),t.compute(rD(h,_,a,u,d,f,p,c),{inputs:$,outputs:h>1?[0,2]:[0]})},rU=(t,r)=>{let i=[r.batchSize,r.numHeads,r.sequenceLength,r.headSize],a=r.sequenceLength,n=r.inputHiddenSize,s=r.headSize,o={x:Math.ceil(r.headSize/12),y:Math.ceil(r.sequenceLength/12),z:r.batchSize*r.numHeads},u=[t.inputs[0],t.inputs[1],t.inputs[2]],l=[{type:12,data:a},{type:12,data:n},{type:12,data:s},{type:12,data:r.numHeads},{type:12,data:r.headSize},{type:12,data:r.hiddenSize},{type:12,data:r.hiddenSize+r.hiddenSize+r.vHiddenSize}];return t.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:i,dataType:t.inputs[0].dataType,gpuDataType:0},{dims:i,dataType:t.inputs[0].dataType,gpuDataType:0},{dims:i,dataType:t.inputs[0].dataType,gpuDataType:0}],dispatchGroup:o,programUniforms:l}),getShaderSource:t=>{let r=tO("output_q",u[0].dataType,i),a=tO("output_k",u[0].dataType,i),n=tO("output_v",u[0].dataType,i),s=tA("input",u[0].dataType,u[0].dims),o=tA("weight",u[1].dataType,u[1].dims),l=tA("bias",u[2].dataType,u[2].dims),d=s.type.storage;return`
342  const TILE_SIZE = 12u;
343  var<workgroup> tileInput: array<${d}, 144>;
344  var<workgroup> tileWeightQ: array<${d}, 144>;
345  var<workgroup> tileWeightK: array<${d}, 144>;
346  var<workgroup> tileWeightV: array<${d}, 144>;
347  ${t.registerUniforms([{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}]).declareVariables(s,o,l,r,a,n)}
348  ${t.mainStart([12,12,1])}
349    let batchIndex = workgroup_id.z / uniforms.num_heads;
350    let headNumber = workgroup_id.z % uniforms.num_heads;
351    let m = global_id.y;
352    let n = global_id.x;
353
354    let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K;
355    let biasOffsetQ = headNumber * uniforms.head_size;
356    let biasOffsetK = uniforms.hidden_size + biasOffsetQ;
357    let biasOffsetV = uniforms.hidden_size + biasOffsetK;
358
359    var valueQ = ${d}(0);
360    var valueK = ${d}(0);
361    var valueV = ${d}(0);
362    for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
363      if (m < uniforms.M && w + local_id.x < uniforms.K) {
364        tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x];
365      }
366      if (n < uniforms.N && w + local_id.y < uniforms.K) {
367        let offset = n + (w + local_id.y) * uniforms.ldb;
368        tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset];
369        tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset];
370        tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset];
371      }
372      workgroupBarrier();
373      for (var k: u32 = 0u; k<TILE_SIZE && w+k < uniforms.K; k++) {
374        let inputTileOffset = TILE_SIZE * local_id.y + k;
375        let weightTileOffset = TILE_SIZE * k + local_id.x;
376        valueQ += tileInput[inputTileOffset] * tileWeightQ[weightTileOffset];
377        valueK += tileInput[inputTileOffset] * tileWeightK[weightTileOffset];
378        valueV += tileInput[inputTileOffset] * tileWeightV[weightTileOffset];
379      }
380
381      workgroupBarrier();
382    }
383
384    let headOffset = (m * uniforms.N + n) % uniforms.head_size;
385    valueQ += bias[headOffset + biasOffsetQ];
386    valueK += bias[headOffset + biasOffsetK];
387    valueV += bias[headOffset + biasOffsetV];
388
389    let offset = workgroup_id.z * uniforms.M * uniforms.N;
390    if (m < uniforms.M && n < uniforms.N) {
391      let outputIdx = offset + m * uniforms.N + n;
392      output_q[outputIdx] = valueQ;
393      output_k[outputIdx] = valueK;
394      output_v[outputIdx] = valueV;
395    }
396  }`}},{inputs:u,outputs:[-1,-1,-1]})},rW=(t,r)=>{let i=rR(t.inputs,r),[a,n,s]=rU(t,i);return rP(t,a,n,s,t.inputs[4],void 0,void 0,void 0,t.inputs[5],i)}}),o4=W(()=>{el(),oG(),oX(),oZ(),oY(),rq=(t,r)=>{if(!t||5!==t.length)throw Error("BatchNormalization requires 5 inputs");let i=(t,r,i)=>{let a=r.length;if(a!==t.length)throw Error(`${i}: num dimensions != ${a}`);r.forEach((r,a)=>{if(r!==t[a])throw Error(`${i}: dim[${a}] do not match`)})};if(t[0].dims.length>1){let a="NHWC"===r.format?r.spatial?t[0].dims.slice(-1):t[0].dims.slice(-1).c
396oncat(t[0].dims.slice(1,t[0].dims.length-1)):t[0].dims.slice(1,r.spatial?2:void 0);i(t[1].dims,a,"Invalid input scale"),i(t[2].dims,a,"Invalid input B"),i(t[3].dims,a,"Invalid input mean"),i(t[4].dims,a,"Invalid input var")}else i(t[1].dims,[1],"Invalid input scale"),i(t[2].dims,[1],"Invalid input B"),i(t[3].dims,[1],"Invalid input mean"),i(t[4].dims,[1],"Invalid input var")},rL=(t,r)=>{let{epsilon:i,spatial:a,format:n}=r,s=t[0].dims,o=a?tk(s[s.length-1]):1,u="NHWC"===n&&s.length>1?o:1,l=tm.size(s)/o,d=a?s.length:s,p=tA("x",t[0].dataType,t[0].dims,o),c=tA("scale",t[1].dataType,t[1].dims,u),h=tA("bias",t[2].dataType,t[2].dims,u),f=tA("inputMean",t[3].dataType,t[3].dims,u),m=tA("inputVar",t[4].dataType,t[4].dims,u),g=tO("y",t[0].dataType,d,o),y=()=>{let t="";if(a)t=`let cOffset = ${1===s.length?"0u":"NHWC"===n?`outputIndices[${s.length-1}] / ${o}`:"outputIndices[1]"};`;else if("NCHW"===n)t=`
397            ${g.indicesSet("outputIndices","0","0")}
398            let cOffset = ${g.indicesToOffset("outputIndices")};`;else{t=`var cIndices = ${c.type.indices}(0);
399                       cIndices[0] = outputIndices[${s.length-1}];`;for(let r=1;r<c.rank;r++)t+=`cIndices[${r}] = outputIndices[${r}];`;t+=`let cOffset = ${c.indicesToOffset("cIndices")};`}return t};return{name:"BatchNormalization",shaderCache:{hint:`${r.epsilon}_${r.format}_${a}_${o}`,inputDependencies:a?["rank","type","type","type","type"]:void 0},getShaderSource:t=>`
400  const epsilon = ${i};
401  ${t.registerUniform("outputSize","u32").declareVariables(p,c,h,f,m,g)}
402  ${t.mainStart()}
403  ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
404    var outputIndices = ${g.offsetToIndices(`global_idx * ${o}`)};
405    ${y()}
406    let scale = ${c.getByOffset("cOffset")};
407    let bias = ${h.getByOffset("cOffset")};
408    let inputMean = ${f.getByOffset("cOffset")};
409    let inputVar = ${m.getByOffset("cOffset")};
410    let x = ${p.getByOffset("global_idx")};
411    let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias;
412    ${g.setByOffset("global_idx","value")}
413  }`,getRunData:()=>({outputs:[{dims:t[0].dims,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:a?[{type:12,data:l},...tS(s)]:[{type:12,data:l}]})}},rV=t=>tc(t),rG=(t,r)=>{let{inputs:i,outputCount:a}=t,n=rV({...r,outputCount:a});if(c.webgpu.validateInputContent&&rq(i,n),r.trainingMode)throw Error("BatchNormalization trainingMode is not supported yet.");t.compute(rL(i,n))}}),o6=W(()=>{oX(),oY(),rH=t=>{if(3!==t[0].dims.length)throw Error("input should have 3 dimensions");if(![320,640,1280].includes(t[0].dims[2]))throw Error("number of channels should be 320, 640 or 1280");if(1!==t[1].dims.length)throw Error("bias is expected to have 1 dimensions");if(t[0].dims[2]!==t[1].dims[0])throw Error("last dimension of input and bias are not the same")},rF=t=>{let r=t[0].dims,i=t[0].dims[2],a=tm.size(r)/4,n=t[0].dataType,s=tA("input",n,r,4),o=tA("bias",n,[i],4),u=tA("residual",n,r,4),l=tO("output",n,r,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:r,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)}}),getShaderSource:t=>`
414  const channels = ${i}u / 4;
415  ${t.declareVariables(s,o,u,l)}
416
417  ${t.mainStart()}
418    ${t.guardAgainstOutOfBoundsWorkgroupSizes(a)}
419    let value = ${s.getByOffset("global_idx")}
420      + ${o.getByOffset("global_idx % channels")} + ${u.getByOffset("global_idx")};
421    ${l.setByOffset("global_idx","value")}
422  }`}},rj=t=>{rH(t.inputs),t.compute(rF(t.inputs))}}),o8=W(()=>{oG(),oX(),oZ(),oY(),rK=(t,r,i,a,n,s,o)=>{let u=Math.ceil(r/4),l="";l="string"==typeof n?`${n}(a)`:n("a");let d=tA("inputData",i,[u],4),p=tO("outputData",a,[u],4),c=[{name:"vec_size",type:"u32"}];return o&&c.push(...o),`
423      ${t.registerUniforms(c).declareVariables(d,p)}
424
425  ${s??""}
426
427  ${t.mainStart()}
428    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
429
430    let a = ${d.getByOffset("global_idx")};
431    ${p.setByOffset("global_idx",l)}
432  }`},rQ=(t,r,i,a,n,s=t.dataType,o,u)=>{let l=[{type:12,data:Math.ceil(tm.size(t.dims)/4)}];return o&&l.push(...o),{name:r,shaderCache:{hint:n,inputDependencies:["type"]},getShaderSource:r=>rK(r,tm.size(t.dims),t.dataType,s,i,a,u),getRunData:r=>({outputs:[{dims:t.dims,dataType:s}],dispatchGroup:{x:Math.ceil(tm.size(r[0].dims)/64/4)},programUniforms:l})}},rZ=t=>{t.compute(rQ(t.inputs[0],"Abs","abs"))},rX=t=>{t.compute(rQ(t.inputs[0],"Acos","acos"))},rY=t=>{t.compute(rQ(t.inputs[0],"Acosh","acosh"))},rJ=t=>{t.compute(rQ(t.inputs[0],"Asin","asin"))},r0=t=>
432{t.compute(rQ(t.inputs[0],"Asinh","asinh"))},r1=t=>{t.compute(rQ(t.inputs[0],"Atan","atan"))},r2=t=>{t.compute(rQ(t.inputs[0],"Atanh","atanh"))},r3=t=>tc(t),r4=(t,r)=>{let i;switch(r.to){case 10:i="vec4<f16>";break;case 1:i="vec4<f32>";break;case 12:i="vec4<u32>";break;case 6:i="vec4<i32>";break;case 9:i="vec4<bool>";break;default:throw RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${r.to}`)}t.compute(rQ(t.inputs[0],"Cast",i,void 0,r.cacheKey,r.to))},r6=t=>{let r,i,a=t.length>=2&&0!==t[1].data,n=t.length>=3&&0!==t[2].data;switch(t[0].dataType){case 1:r=a?t[1].getFloat32Array()[0]:-34028234663852886e22,i=n?t[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:r=a?t[1].getUint16Array()[0]:64511,i=n?t[2].getUint16Array()[0]:31743;break;default:throw Error("Unsupport data type")}return tc({min:r,max:i})},r8=(t,r)=>{let i=r||r6(t.inputs),a=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"Clip",t=>`clamp(${t}, vec4<${a}>(uniforms.min), vec4<${a}>(uniforms.max))`,void 0,i.cacheKey,void 0,[{type:t.inputs[0].dataType,data:i.min},{type:t.inputs[0].dataType,data:i.max}],[{name:"min",type:a},{name:"max",type:a}]),{inputs:[0]})},r5=t=>{t.compute(rQ(t.inputs[0],"Ceil","ceil"))},r9=t=>{t.compute(rQ(t.inputs[0],"Cos","cos"))},r7=t=>{t.compute(rQ(t.inputs[0],"Cosh","cosh"))},ie=t=>tc(t),it=(t,r)=>{let i=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"Elu",t=>`elu_vf32(${t})`,`
433  const elu_alpha_ = ${i}(${r.alpha});
434
435  fn elu_f32(a: ${i}) -> ${i} {
436  return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0);
437  }
438
439  fn elu_vf32(v: vec4<${i}>) -> vec4<${i}> {
440  return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w));
441  }`,r.cacheKey))},ir=(t="f32")=>`
442const r0: ${t} = 0.3275911;
443const r1: ${t} = 0.254829592;
444const r2: ${t} = -0.284496736;
445const r3: ${t} = 1.421413741;
446const r4: ${t} = -1.453152027;
447const r5: ${t} = 1.061405429;
448
449fn erf_vf32(v: vec4<${t}>) -> vec4<${t}> {
450  let absv = abs(v);
451  let x = 1.0 / (1.0 + r0 * absv);
452  return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv));
453}`,ii=t=>{let r=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"Erf",t=>`erf_vf32(${t})`,ir(r)))},ia=t=>{t.compute(rQ(t.inputs[0],"Exp","exp"))},is=t=>{t.compute(rQ(t.inputs[0],"Floor","floor"))},io=t=>{let r=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"Gelu",t=>`0.5 * ${t} * (1.0 + erf_vf32(${t} * 0.7071067811865475))`,ir(r)))},iu=(t,r)=>{let i=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"LeakyRelu",t=>`select(leaky_relu_alpha_ * ${t}, ${t}, ${t} >= vec4<${i}>(0.0))`,`const leaky_relu_alpha_ = ${i}(${r.alpha});
453`,r.cacheKey))},il=t=>{t.compute(rQ(t.inputs[0],"Not",t=>`!${t}`))},id=t=>{t.compute(rQ(t.inputs[0],"Neg",t=>`-${t}`))},ip=t=>{t.compute(rQ(t.inputs[0],"Reciprocal",t=>`1.0/${t}`))},ic=t=>{let r=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"Relu",t=>`select(vec4<${r}>(0.0), ${t}, ${t} > vec4<${r}>(0.0))`))},ih=t=>{t.compute(rQ(t.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},im=t=>tc(t),ig=(t,r)=>{let i=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"HardSigmoid",t=>`max(vec4<${i}>(0.0), min(vec4<${i}>(1.0), ${r.alpha} * ${t} + vec4<${i}>(${r.beta})))`,void 0,r.cacheKey))},iy=t=>{t.compute(rQ(t.inputs[0],"Sin","sin"))},i_=t=>{t.compute(rQ(t.inputs[0],"Sinh","sinh"))},i$=t=>{t.compute(rQ(t.inputs[0],"Sqrt","sqrt"))},ib=t=>{t.compute(rQ(t.inputs[0],"Tan","tan"))},iv=t=>`sign(${t}) * (1 - exp(-2 * abs(${t}))) / (1 + exp(-2 * abs(${t})))`,iw=t=>{t.compute(rQ(t.inputs[0],"Tanh",iv))},ix=(t="f32")=>`
454const fast_gelu_a: ${t} = 0.5;
455const fast_gelu_b: ${t} = 0.7978845608028654;
456const fast_gelu_c: ${t} = 0.035677408136300125;
457
458fn tanh_v(v: vec4<${t}>) -> vec4<${t}> {
459  return ${iv("v")};
460}
461`,iS=t=>`(fast_gelu_a + fast_gelu_a * tanh_v(${t} * (fast_gelu_c * ${t} * ${t} + fast_gelu_b))) * ${t}`,ik=t=>{let r=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"FastGelu",iS,ix(r),void 0,t.inputs[0].dataType))},iT=(t,r)=>{let i=tx(t.inputs[0].dataType);return t.compute(rQ(t.inputs[0],"ThresholdedRelu",t=>`select(vec4<${i}>(0.0), ${t}, ${t} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${i}>(${r.alpha});
461`,r.cacheKey)),0},iC=t=>{t.compute(rQ(t.inputs[0],"Log","log"))},iI=(t,r)=>`
462const alpha = vec4<${t}>(${r});
463const one = ${t}(1.0);
464const zero = ${t}(0.0);
465
466fn quick_gelu_impl(x: vec4<${t}>) -> vec4<${t}> {
467  let v = x *alpha;
468  var x1 : vec4<${t}>;
469  for (var i = 0; i < 4; i = i + 1) {
470    if (v[i] >= zero) {
471      x1[i] = one / (one + exp(-v[i]));
472    } else {
473      x1[i] = one - one / (one + exp(v[i]));
474    }
475  }
476  return x * x1;
477}
478`,iE=t=>`quick_gelu_impl(${t})`,iz=(t,r)=>{let i=tx(t.inputs[0].dataType);t.compute(rQ(t.inputs[0],"QuickGelu",iE,iI(i,r.alpha),r.cacheKey,t.inputs[0].dataType))}}),o5=W(()=>{oX(),oY(),o8(),iA=t=>{if(3!==t[0].dims.length)throw Error("input should have 3 dimensions");if(![2560,5120,10240].includes(t[0].dims[2]))throw Error("hidden state should be 2560, 5120 or 10240");if(1!==t[1].dims.length)throw Error("bias is expected to have 1 dimensions");if(t[0].dims[2]!==t[1].dims[0])throw Error("last dimension of input and bias are not the same")},iO=t=>{let r=t[0].dims.slice();r[2]=r[2]/2;let i=tA("input",t[0].dataType,t[0].dims,4),a=tA("bias",t[0].dataType,[t[0].dims[2]],4),n=tO("output",t[0].dataType,r,4),s=tm.size(r)/4,o=tw(t[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:r,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)}}),getShaderSource:r=>`
479  const M_SQRT2 = sqrt(2.0);
480  const halfChannels = ${t[0].dims[2]/4/2}u;
481
482  ${r.declareVariables(i,a,n)}
483
484  ${ir(o)}
485
486  ${r.mainStart()}
487    ${r.guardAgainstOutOfBoundsWorkgroupSizes(s)}
488    let biasIdx = global_idx % halfChannels;
489    let batchIndex = global_idx / halfChannels;
490    let inputOffset = biasIdx + batchIndex * halfChannels * 2;
491    let valueLeft = input[inputOffset] + bias[biasIdx];
492    let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels];
493    let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1);
494
495    ${n.setByOffset("global_idx","valueLeft * geluRight")}
496  }`}},iR=t=>{iA(t.inputs),t.compute(iO(t.inputs))}}),o9=W(()=>{oG(),oX(),oY(),iB=(t,r,i,a,n,s,o,u,l,d,p,c)=>{let h,f;"string"==typeof u?h=f=(t,r)=>`${u}((${t}),(${r}))`:"function"==typeof u?h=f=u:(h=u.scalar,f=u.vector);let m=tO("outputData",p,a.length,4),g=tA("aData",l,r.length,4),y=tA("bData",d,i.length,4),_;if(n){if(s){let t=1===tm.size(r),a=1===tm.size(i),n=r.length>0&&r[r.length-1]%4==0,s=i.length>0&&i[i.length-1]%4==0;_=t||a?m.setByOffset("global_idx",f(t?`${g.type.value}(${g.getByOffset("0")}.x)`:g.getByOffset("global_idx"),a?`${y.type.value}(${y.getByOffset("0")}.x)`:y.getByOffset("global_idx"))):`
497            let outputIndices = ${m.offsetToIndices("global_idx * 4u")};
498            let offsetA = ${g.broadcastedIndicesToOffset("outputIndices",m)};
499            let offsetB = ${y.broadcastedIndicesToOffset("outputIndices",m)};
500            ${m.setByOffset("global_idx",f(o||n?g.getByOffset("offsetA / 4u"):`${g.type.value}(${g.getByOffset("offsetA / 4u")}[offsetA % 4u])`,o||s?y.getByOffset("offsetB / 4u"):`${y.type.value}(${y.getByOffset("offsetB / 4u")}[offsetB % 4u])`))}
501          `}else _=m.setByOffset("global_idx",f(g.getByOffset("global_idx"),y.getByOffset("global_idx")))}else{if(!s)throw Error("no necessary to use scalar implementation for element-wise binary op implementation.");let t=(t,r,i="")=>{let a=`aData[indexA${r}][componentA${r}]`,n=`bData[indexB${r}][componentB${r}]`;return`
502            let outputIndices${r} = ${m.offsetToIndices(`global_idx * 4u + ${r}u`)};
503            let offsetA${r} = ${g.broadcastedIndicesToOffset(`outputIndices${r}`,m)};
504            let offsetB${r} = ${y.broadcastedIndicesToOffset(`outputIndices${r}`,m)};
505            let indexA${r} = offsetA${r} / 4u;
506            let indexB${r} = offsetB${r} / 4u;
507            let componentA${r} = offsetA${r} % 4u;
508            let componentB${r} = offsetB${r} % 4u;
509            ${t}[${r}] = ${i}(${h(a,n)});
510          `};_=9===p?`
511            var data = vec4<u32>(0);
512            ${t("data",0,"u32")}
513            ${t("data",1,"u32")}
514            ${t("data",2,"u32")}
515            ${t("data",3,"u32")}
516            outputData[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:`
517            ${t("outputData[global_idx]",0)}
518            ${t("outputData[global_idx]",1)}
519            ${t("outputData[global_idx]",2)}
520            ${t("outputData[global_idx]",3)}
521          `}return`
522        ${t.registerUniform("vec_size","u32").declareVariables(g,y,m)}
523
524        ${c??""}
525
526        ${t.mainStart()}
527        ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
528        ${_}
529      }`},iN=(t,r,i,a,n,s,o=i.dataType)=>{let u=i.dims.map(t=>Number(t)??1),l=a.dims.map(t=>Number(t)??1),d=!tm.areEqual(u,l),p=u,c=tm.size(u),h=!1,f=!1,m=[d];
529if(d){let t=tf.calcShape(u,l,!1);if(!t)throw Error("Can't perform binary op on the given tensors");p=t.slice(),c=tm.size(p);let r=1===tm.size(u),i=1===tm.size(l),a=u.length>0&&u[u.length-1]%4==0,n=l.length>0&&l[l.length-1]%4==0;m.push(r),m.push(i),m.push(a),m.push(n);let s=1;for(let t=1;t<p.length;t++){let r=u[u.length-t];if(r===l[l.length-t])s*=r;else break}s%4==0?(f=!0,h=!0):(r||i||a||n)&&(h=!0)}else h=!0;return m.push(h),{name:t,shaderCache:{hint:r+m.map(t=>t.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:t=>iB(t,u,l,p,h,d,f,n,i.dataType,a.dataType,o,s),getRunData:()=>({outputs:[{dims:p,dataType:o}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:Math.ceil(tm.size(p)/4)},...tS(u,l,p)]})}},iM=(t,r,i,a,n,s)=>{t.compute(iN(r,n??"",t.inputs[0],t.inputs[1],i,a,s))},iD=t=>{iM(t,"Add",(t,r)=>`${t}+${r}`)},iP=t=>{iM(t,"Div",(t,r)=>`${t}/${r}`)},iU=t=>{iM(t,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4<u32>(${t}==${r})`},void 0,void 0,9)},iW=t=>{iM(t,"Mul",(t,r)=>`${t}*${r}`)},iq=t=>{let r=tA("input",t.inputs[0].dataType,t.inputs[0].dims).type.value;iM(t,"Pow",{scalar:(t,r)=>`pow_custom(${t},${r})`,vector:(t,r)=>`pow_vector_custom(${t},${r})`},`
530    fn pow_custom(a : ${r}, b : ${r}) -> ${r} {
531      if (b == ${r}(0.0)) {
532        return ${r}(1.0);
533      } else if (a < ${r}(0.0) && f32(b) != floor(f32(b))) {
534        return ${r}(pow(f32(a), f32(b))); // NaN
535      }
536      return select(sign(a), ${r}(1.0), round(f32(abs(b) % ${r}(2.0))) != 1.0) * ${r}(${"i32"===r?"round":""}(pow(f32(abs(a)), f32(b))));
537    }
538    fn pow_vector_custom(a : vec4<${r}>, b : vec4<${r}>) -> vec4<${r}> {
539      // TODO: implement vectorized pow
540      return vec4<${r}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w));
541    }
542      `)},iL=t=>{iM(t,"Sub",(t,r)=>`${t}-${r}`)},iV=t=>{iM(t,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},iG=t=>{iM(t,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},iH=t=>{iM(t,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},iF=t=>{iM(t,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}}),o7=W(()=>{oG(),oX(),oZ(),oY(),ij=(t,r)=>{if(!t||t.length<1)throw Error("too few inputs");let i=t[0],a=i.dataType,n=i.dims.length;t.forEach((t,s)=>{if(0!==s){if(t.dataType!==a)throw Error("input tensors should be one type");if(t.dims.length!==n)throw Error("input tensors should have the same shape");t.dims.forEach((t,a)=>{if(a!==r&&t!==i.dims[a])throw Error("non concat dimensions must match")})}})},iK=(t,r)=>`
543  fn calculateInputIndex(index: u32) -> u32 {
544    let sizeInConcatAxis = array<u32, ${t}u>(${r});
545    for (var i: u32 = 0u; i < ${t}; i += 1u ) {
546      if (index < sizeInConcatAxis[i]) {
547        return i;
548      }
549    }
550    return ${t}u;
551  }`,iQ=(t,r)=>{let i=t.length,a=[];for(let n=0;n<i;++n){let s=r.setByOffset("global_idx",t[n].getByIndices("indices"));1===i?a.push(s):0===n?a.push(`if (inputIndex == ${n}u) { ${s} }`):n===i-1?a.push(`else { ${s} }`):a.push(`else if (inputIndex == ${n}) { ${s} }`)}return a.join(`
552`)},iZ=(t,r,i,a)=>{let n=tm.size(i),s=Array(t.length),o=Array(t.length),u=0,l=[],d=[],p=[{type:12,data:n}];for(let i=0;i<t.length;++i)u+=t[i].dims[r],s[i]=u,d.push(t[i].dims.length),o[i]=tA(`input${i}`,a,d[i]),l.push("rank"),p.push({type:12,data:s[i]});for(let r=0;r<t.length;++r)p.push(...tS(t[r].dims));p.push(...tS(i));let c=tO("output",a,i.length),h=c.indicesGet("indices",r),f=Array.from(Array(s.length).keys()).map(t=>`uniforms.sizeInConcatAxis${t}`).join(",");return{name:"Concat",shaderCache:{hint:`${r}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:i,dataType:a}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:p}),getShaderSource:r=>`
553
554  ${(()=>{r.registerUniform("outputSize","u32");for(let i=0;i<t.length;i++)r.registerUniform(`sizeInConcatAxis${i}`,"u32");return r.declareVariables(...o,c)})()}
555
556  ${iK(s.length,f)}
557
558  ${r.mainStart()}
559    ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
560
561    var indices = ${c.offsetToIndices("global_idx")};
562
563    let inputIndex = calculateInputIndex(${h});
564    if (inputIndex != 0u) {
565      let sizeInConcatAxis = array<u32, ${s.length}u>(${f});
566      ${h} -= sizeInConcatAxis[inputIndex - 1u];
567    }
568
569    ${iQ(o,c)}
570  }`}},iX=(t,r)=>{let i=t.inputs,a=i[0].dims,n=tm.normalizeAxis(r.axis,a.length);ij(i,n);let s=a.slice();s[n]=i.reduce((t,r)=>t+(r.dims.length>n?r.dims[n]:0),0);let o=i.filter(t=>tm.size(t.dims)>0);t.compute(iZ(o,n,s,i[0].dataType),{inputs:o})},iY=t=>tc({axis:t.axis})}),ue=W(()=>{oG(),oX(),iJ=(t,r,i="f32")=>{switch(t.activation){case"Relu":return`value = max(value, ${r}(0.0));`;case"Sigmoid":return`value = (${r}(1.0) / (${r}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${r}(${i}(uniforms.clip_min)), ${r}(${i}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${r}(0.0), min(${r}(1.0), ${i}(uniforms.alpha) * value + ${i}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${i}(uniforms.alpha) * value, value, value >= ${r}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value));
571              value = sign(value) * (1.0 - e2x) / (1.0 + e2x);
572        `;case"":return"";default:throw Error(`Unsupported activation ${t.activation}`)}},i0=(t,r)=>{"Clip"===t.activation?r.push({type:1,data:t.clipMax},{type:1,data:t.clipMin}):"HardSigmoid"===t.activation?r.push({type:1,data:t.alpha},{type:1,data:t.beta}):"LeakyRelu"===t.activation&&r.push({type:1,data:t.alpha})},i1=(t,r)=>{"Clip"===t.activation?r.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):"HardSigmoid"===t.activation?r.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):"LeakyRelu"===t.activation&&r.push({name:"alpha",type:"f32"})},i2=t=>{let r=t?.activation||"";if("HardSigmoid"===r){let[i,a]=t?.activation_params||[.2,.5];return{activation:r,alpha:i,beta:a}}if("Clip"===r){let[i,a]=t?.activation_params||[t_,t$];return{activation:r,clipMax:a,clipMin:i}}if("LeakyRelu"===r){let[i]=t?.activation_params||[.01];return{activation:r,alpha:i}}return{activation:r}}}),ut=W(()=>{i3=(t,r)=>{switch(t){case 1:return r;case 2:return`vec2<${r}>`;case 3:return`vec3<${r}>`;case 4:return`vec4<${r}>`;default:throw Error(`${t}-component is not supported.`)}},i4=t=>`
573      ${t?"value = value + getBiasByOutputCoords(coords);":""}
574      `}),ur=W(()=>{i6=t=>`
575fn getIndexFromCoords4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 {
576  return dot(coords, vec4<i32>(
577      shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));
578}
579fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
580  return dot(coords, vec4<i32>(
581    i32(${t}.x), i32(${t}.y), i32(${t}.z), 1));
582}
583`}),ui=W(()=>{oG(),oX(),oY(),ue(),i8=(t,r,i,a,n)=>{let s=a-i;return`
584      ${Array.from({length:i}).map((i,o)=>`
585      if (${tE(r.shape,o,r.rank)} != 1) {
586        ${r.indicesSet(t,o,tE(n,o+s,a))}
587      } else {
588        ${r.indicesSet(t,o,0)}
589      }`).join("")}
590`},i5=(t,r,i,a,n=!1,s)=>{let o=t[0].dims,u=t[1].dims,l=o[o.length-2],d=u[u.length-1],p=o[o.length-1],c=tk(d),h=tk(p),f=tk(l),m=tm.size(i)/c/f,g=t.length>2,y=a?a.slice(0,-2):i.slice(0,-2),_=[tm.size(y),l,d],$=[{type:12,data:m},{type:12,data:l},{type:12,data:d}
590,{type:12,data:p}];return i0(r,$),$.push(...tS(y,o,u)),g&&$.push(...tS(t[2].dims)),$.push(...tS(_)),{name:"MatMulNaive",shaderCache:{hint:`${r.activation};${c};${h};${f};${n}`,inputDependencies:g?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:s?s(i):i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:$}),getShaderSource:a=>{let s=tB("batch_dims",t[0].dataType,y.length),l=tA("a",t[0].dataType,o.length,h),d=tA("b",t[1].dataType,u.length,c),p=tO("output",t[0].dataType,_.length,c),m=tw(p.type.tensor),$=iJ(r,p.type.value,m),b=[l,d],v="";if(g){let r=n?c:1;b.push(tA("bias",t[2].dataType,t[2].dims.length,r)),v=`${n?`value += bias[col / ${r}];`:`value += ${p.type.value}(bias[row + i]);`}`}let w=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];return i1(r,w),`
591  ${a.registerUniforms(w).registerInternalVariables(s).declareVariables(...b,p)}
592  ${a.mainStart()}
593    ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
594    let col = (global_idx % (uniforms.N / ${c})) * ${c};
595    var index1 = global_idx / (uniforms.N / ${c});
596    let stride1 = uniforms.M / ${f};
597    let row = (index1 % stride1) * ${f};
598    let batch = index1 / stride1;
599
600    ${2===i.length?"":`let batch_indices = ${s.offsetToIndices("batch")};`}
601
602    var a_indices: ${l.type.indices};
603    ${i8("a_indices",l,l.rank-2,s.rank,"batch_indices")}
604    ${l.indicesSet("a_indices",l.rank-2,0)}
605    ${l.indicesSet("a_indices",l.rank-1,0)}
606    let a_offset = ${l.indicesToOffset("a_indices")};
607
608    var b_indices: ${d.type.indices};
609    ${i8("b_indices",d,d.rank-2,s.rank,"batch_indices")}
610    ${d.indicesSet("b_indices",d.rank-2,0)}
611    ${d.indicesSet("b_indices",d.rank-1,0)}
612    let b_offset = ${d.indicesToOffset("b_indices")};
613    var values: array<${p.type.value}, ${f}>;
614    for (var k: u32 = 0u; k < uniforms.K; k = k + ${h}) {
615      ${(()=>{let t=`var a_data: ${l.type.value};`;for(let r=0;r<h;r++)t+=`
616              let b_data${r} = b[(b_offset + (k + ${r}) * uniforms.N + col) / ${c}];`;for(let r=0;r<f;r++){t+=`a_data = a[(a_offset + (row + ${r}) * uniforms.K + k) / ${h}];`;for(let i=0;i<h;i++)t+=`
617            values[${r}] = fma(${d.type.value}(a_data${1===h?"":`[${i}]`}), b_data${i}, values[${r}]);
618`}return t})()}
619    }
620    for (var i = 0u; i < ${f}u; i++) {
621      var value = values[i];
622      ${v}
623      ${$}
624      let cur_indices = ${p.type.indices}(batch, row + i, col);
625      let offset = ${p.indicesToOffset("cur_indices")};
626      ${p.setByOffset(`offset / ${c}`,"value")};
627    }
628  }
629  `}}}}),ua=W(()=>{oG(),oX(),oY(),ue(),ui(),ut(),i9=(t,r)=>t?`
630        mm_Asub[inputRow][inputCol] = mm_readA(batch,
631          kStart + inputRow,
632          globalRowStart / innerElementSize + inputCol${r?", batchIndices":""});
633        `:`
634        mm_Asub[inputRow][inputCol] = mm_readA(batch,
635          globalRow + innerRow,
636          kStart / innerElementSize + inputCol${r?", batchIndices":""});
637        `,i7=(t,r)=>t?`
638        let ACached0 = mm_Asub[k * innerElementSize][localRow];
639        let ACached1 = mm_Asub[k * innerElementSize + 1][localRow];
640        let ACached2 = mm_Asub[k * innerElementSize + 2][localRow];
641        ${3===r?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"}
642        for (var i = 0; i < rowPerThread; i = i + 1) {
643          acc[i] = BCached0 * ACached0[i] + acc[i];
644          acc[i] = BCached1 * ACached1[i] + acc[i];
645          acc[i] = BCached2 * ACached2[i] + acc[i];
646          ${3===r?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"}
647        }`:`
648        for (var i = 0; i < rowPerThread; i = i + 1) {
649          let ACached = mm_Asub[tileRow + i][k];
650          acc[i] = BCached0 * ACached.x + acc[i];
651          acc[i] = BCached1 * ACached.y + acc[i];
652          acc[i] = BCached2 * ACached.z + acc[i];
653          ${3===r?"":"acc[i] = BCached3 * ACached.w + acc[i];"}
654        }`,ae=(t,r,i="f32",a,n=!1,s=32,o=!1,u=32)=>{let l=r[1]*t[1],d=r[0]*t[0],p=n?l:s,c=n?s:l,h=p/r[0],f=s/r[1];if(!((n&&4===h&&4===t[1]||!n&&(3===h||4===h))&&p%r[0]==0&&s%r[1]==0&&4===t[0]))throw Error(`If transposeA ${n} is true, innerElementSize ${h} and workPerThread[1] ${t[1]} must be 4.
655      Otherwise, innerElementSize ${h} must be 3 or 4.
656  tileAWidth ${p} must be divisible by workgroupSize[0]${r[0]}. tileInner ${s} must be divisible by workgroupSize[1] ${r[1]}. colPerThread ${t[0]} must be 4.`);return`
657var<workgroup> mm_Asub: array<array<vec${h}<${i}>, ${p/h}>, ${c}>;
658var<workgroup> mm_Bsub: array<array<vec4<${i}>, ${d/t[0]}>, ${s}>;
659
660const rowPerThread = ${t[1]};
661const colPerThread = ${t[0]};
662const innerElementSize = ${h};
663const tileInner = ${s};
664
665@compute @workgroup_size(${r[0]}, ${r[1]}, ${r[2]})
666fn main(@builtin(local_invocation_id) localId : vec3<u32>,
667        @builtin(global_invocation_id) globalId : vec3<u32>,
668        @builtin(workgroup_id) workgroupId : vec3<u32>) {
669  let localRow = i32(localId.y);
670  let tileRow = localRow * rowPerThread;
671  let tileCol = i32(localId.x);
672
673  let globalRow =i32(globalId.y) * rowPerThread;
674  let globalCol = i32(globalId.x);
675  let batch = ${o?"0":"i32(globalId.z)"};
676  ${a?`let batchIndices = ${a.offsetToIndices("u32(batch)")};`:""}
677  let globalRowStart = i32(workgroupId.y) * ${l};
678
679  let num_tiles = ${o?`${Math.ceil(u/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};
680  var kStart = ${o?`i32(globalId.z) * ${u}`:"0"};
681
682  var acc: array<vec4<${i}>, rowPerThread>;
683
684  // Loop over shared dimension.
685  let tileRowB = localRow * ${f};
686  for (var t = 0; t < num_tiles; t = t + 1) {
687      // Load one tile of A into local memory.
688      for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
689          let inputRow = tileRow + innerRow;
690          let inputCol = tileCol;
691          ${i9(n,a)}
692      }
693
694      // Load one tile of B into local memory.
695      for (var innerRow = 0; innerRow < ${f}; innerRow = innerRow + 1) {
696          let inputRow = tileRowB + innerRow;
697          let inputCol = tileCol;
698          mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${a?", batchIndices":""});
699      }
700      kStart = kStart + tileInner;
701      workgroupBarrier();
702
703      // Compute acc values for a single thread.
704      for (var k = 0; k < tileInner / innerElementSize; k = k + 1) {
705          let BCached0 = mm_Bsub[k * innerElementSize][tileCol];
706          let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol];
707          let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol];
708          ${3===h?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"}
709
710          ${i7(n,h)}
711      }
712
713      workgroupBarrier();
714  }
715
716  for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
717      mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]);
718  }
719}`},at=(t,r)=>t?`
720            mm_Asub[inputRow][inputCol] = mm_readA(batch,
721              kStart + inputRow,
722              globalRowStart + inputCol${r?", batchIndices":""});
723            `:`
724            mm_Asub[inputRow][inputCol] = mm_readA(batch,
725              globalRowStart + inputRow,
726              kStart + inputCol${r?", batchIndices":""});
727            `,ar=t=>t?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",ai=(t,r,i="f32",a,n=!1,s=32,o=!1,u=32,l=!1)=>
727{let d=t[1]*r[1],p=t[0]*r[0],c=n?d:s,h=n?s:d;if(!(h%r[1]==0&&c%r[0]==0&&s%r[1]==0))throw Error(`tileAHight ${h} must be divisible by workgroupSize[1]${r[1]}, tileAWidth ${c} must be divisible by workgroupSize[0]${r[0]}, tileInner ${s} must be divisible by workgroupSize[1]${r[1]}`);let f=h/r[1],m=c/r[0],g=s/r[1],y=l?`
728    let localRow = i32(localId.y);
729    let localCol = i32(localId.x);
730    let globalRowStart = i32(workgroupId.y) * ${d};
731    let globalColStart = i32(workgroupId.x) * ${p};
732
733    // Loop over shared dimension.
734    for (var t = 0; t < num_tiles; t = t + 1) {
735      // Load one tile of A into local memory.
736      for (var inputRow = localRow; inputRow < ${h}; inputRow = inputRow + ${r[1]}) {
737        for (var inputCol = localCol; inputCol < ${c}; inputCol = inputCol + ${r[0]}) {
738          ${at(n,a)}
739        }
740      }
741      // Load one tile of B into local memory.
742      for (var inputRow = localRow; inputRow < ${s}; inputRow = inputRow + ${r[1]}) {
743            for (var inputCol = localCol; inputCol < ${p}; inputCol = inputCol + ${r[0]}) {
744          mm_Bsub[inputRow][inputCol] = mm_readB(batch,
745            kStart + inputRow,
746            globalColStart + inputCol${a?", batchIndices":""});
747        }
748      }
749      kStart = kStart + tileInner;
750      workgroupBarrier();
751
752      // Compute acc values for a single thread.
753      var BCached : array<${i}, colPerThread>;
754      for (var k = 0; k < tileInner; k = k + 1) {
755        for (var inner = 0; inner < colPerThread; inner = inner + 1) {
756          BCached[inner] = mm_Bsub[k][localCol + inner * ${r[0]}];
757        }
758        for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
759          let ACached = ${n?`mm_Asub[k][localRow + innerRow * ${r[1]}];`:`mm_Asub[localRow + innerRow * ${r[1]}][k];`}
760          for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
761            acc[innerRow][innerCol] = acc[innerRow][innerCol] +
762                ACached * BCached[innerCol];
763          }
764        }
765      }
766      workgroupBarrier();
767    }
768    for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
769      let gRow = globalRowStart + localRow + innerRow * ${r[1]};
770      for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
771        let gCol = globalColStart + localCol + innerCol * ${r[0]};
772        mm_write(batch, gRow, gCol, acc[innerRow][innerCol]);
773      }
774    }
775    `:`
776let tileRow = i32(localId.y) * rowPerThread;
777let tileCol = i32(localId.x) * colPerThread;
778
779let globalRow = i32(globalId.y) * rowPerThread;
780let globalCol = i32(globalId.x) * colPerThread;
781let globalRowStart = i32(workgroupId.y) * ${d};
782
783let tileRowA = i32(localId.y) * ${f};
784let tileColA = i32(localId.x) * ${m};
785let tileRowB = i32(localId.y) * ${g};
786// Loop over shared dimension.
787for (var t = 0; t < num_tiles; t = t + 1) {
788  // Load one tile of A into local memory.
789  for (var innerRow = 0; innerRow < ${f}; innerRow = innerRow + 1) {
790    for (var innerCol = 0; innerCol < ${m}; innerCol = innerCol + 1) {
791      let inputRow = tileRowA + innerRow;
792      let inputCol = tileColA + innerCol;
793      ${at(n,a)}
794    }
795  }
796
797  // Load one tile of B into local memory.
798  for (var innerRow = 0; innerRow < ${g}; innerRow = innerRow + 1) {
799    for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
800      let inputRow = tileRowB + innerRow;
801      let inputCol = tileCol + innerCol;
802      mm_Bsub[inputRow][inputCol] = mm_readB(batch,
803        kStart + inputRow,
804        globalCol + innerCol${a?", batchIndices":""});
805    }
806  }
807  kStart = kStart + tileInner;
808  workgroupBarrier();
809
810  // Compute acc values for a single thread.
811  var BCached : array<${i}, colPerThread>;
812  for (var k = 0; k < tileInner; k = k + 1) {
813    for (var inner = 0; inner < colPerThread; inner = inner + 1) {
814      BCached[inner] = mm_Bsub[k][tileCol + inner];
815    }
816
817    for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
818      ${ar(n)}
819      for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
820        acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];
821      }
822    }
823  }
824
825  workgroupBarrier();
826}
827
828for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
829  for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
830    mm_write(batch, globalRow + innerRow, globalCol + innerCol,
831        acc[innerRow][innerCol]);
832  }
833}
834`;return`
835  var<workgroup> mm_Asub : array<array<${i}, ${c}>, ${h}>;
836  var<workgroup> mm_Bsub : array<array<${i}, ${p}>, ${s}>;
837  const rowPerThread = ${t[1]};
838  const colPerThread = ${t[0]};
839  const tileInner = ${s};
840
841@compute @workgroup_size(${r[0]}, ${r[1]}, ${r[2]})
842fn main(@builtin(local_invocation_id) localId : vec3<u32>,
843        @builtin(global_invocation_id) globalId : vec3<u32>,
844        @builtin(workgroup_id) workgroupId : vec3<u32>) {
845    let batch = ${o?"0":"i32(globalId.z)"};
846    ${a?`let batchIndices = ${a.offsetToIndices("u32(batch)")};`:""}
847    let num_tiles = ${o?`${Math.ceil(u/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};
848    var kStart = ${o?`i32(globalId.z) * ${u}`:"0"};
849
850    var acc : array<array<${i}, colPerThread>, rowPerThread>;
851    ${y}
852  }
853`},aa=(t,r,i,a,n=!1)=>{let[s,o,u,l]=a,d=tw(a[0].type.tensor);return`
854    fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${s.type.indices}) -> ${i3(t,d)} {
855      var value = ${i3(t,d)}(0.0);
856      let col = colIn * ${t};
857      if(row < uniforms.dim_a_outer && col < uniforms.dim_inner)
858      {
859        var aIndices: ${o.type.indices};
860        ${i8("aIndices",o,o.rank-2,s.rank,"batchIndices")}
861        ${o.indicesSet("aIndices",o.rank-2,"u32(row)")}
862        ${o.indicesSet("aIndices",o.rank-1,"u32(colIn)")}
863        value = ${o.getByIndices("aIndices")};
864      }
865      return value;
866    }
867
868    fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${s.type.indices}) -> ${i3(t,d)} {
869      var value = ${i3(t,d)}(0.0);
870      let col = colIn * ${t};
871      if(row < uniforms.dim_inner && col < uniforms.dim_b_outer)
872      {
873        var bIndices: ${u.type.indices};
874        ${i8("bIndices",u,u.rank-2,s.rank,"batchIndices")}
875        ${u.indicesSet("bIndices",u.rank-2,"u32(row)")}
876        ${u.indicesSet("bIndices",u.rank-1,"u32(colIn)")}
877        value = ${u.getByIndices("bIndices")};
878      }
879      return value;
880    }
881
882    fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${i3(t,d)}) {
883      let col = colIn * ${t};
884      if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) {
885        var value = valueIn;
886        let coords = vec3<i32>(batch, row, colIn);
887        ${r?`value = value + ${n?"bias[colIn]":`${i3(t,d)}(bias[row])`};`:""}
888        ${i}
889        ${l.setByIndices("vec3<u32>(coords)","value")}
890      }
891    }
892    `},an=(t,r,i,a,n=!1,s)=>{let o=t[0].dims,u=t[1].dims,l=o.slice(0,-2),d=u.slice(0,-2),p=a?a.slice(0,-2):i.slice(0,-2),c=tm.size(p),h=o[o.length-2],f=o[o.length-1],m=u[u.length-1],g=f%4==0&&m%4==0,y=h<=8?[4,1,1]:[4,4,1],_=[8,8,1],$=[Math.ceil(m/_[0]/y[0]),Math.ceil(h/_[1]/y[1]),Math.ceil(c/_[2]/y[2])],b=g?4:1,v=[...l,h,f/b],w=v.length,x=[...d,f,m/b],S=x.length,k=[c,h,m/b],T=[{type:6,data:h},{type:6,data:m},{type:6,data:f}];i0(r,T),T.push(...tS(p,v,x));let C=["rank","rank"],I=t.length>2;return I&&(T.push(...tS(t[2].dims)),C.push("rank")),T.push(...tS(k)),{name:"MatMul",shaderCache:{hint:`${y};${r.activation};${g};${n}`,inputDependencies:C},getRunData:()=>({outputs:[{dims:s?s(i):i,dataType:t[0].dataType}],dispatchGroup:{x:$[0],y:$[1],z:$[2]},programUniforms:T}),getShaderSource:i=>{let a=p.length,s=tB("batchDims",t[0].dataType,a,1),o=tw(t[0].dataType),u=tA("a",t[0].dataType,w,b),l=tA("b",t[1].dataType,S,b),d=tO("result",t[0].dataType,k.length,b),c=[u,l];if(I){let r=n?b:1;c.push(tA("bias",t[2].dataType,t[2].dims.length,r))}let h=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];i1(r,h);let f=tw(d.type.tensor),m=aa(b,I,iJ(r,d.type.value,f),[s,u,l,d],n);return`
893  ${i.registerUniforms(h).registerInternalVariables(s).declareVariables(...c,d)}
894  ${m}
895  ${g?ae(y,_,o,s):ai(y,_,o,s)}
896                   `}}}}),un=W(()=>{oG(),oF(),oY(),ue(),ut(),ur(),ua(),as=(t,r,i,a,n=!1,s,o=4,u=4,l=4,d="f32")=>{let p=t=>{switch(t){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw Error(`innerElementSize ${t} is not supported.`)}},c=t?`
897    let coord = vec4<i32>(batch, xRow, xCol, xCh);
898    `:`
899    let coord = vec4<i32>(batch, xCh, xRow, xCol);
900    `,h=t?`
901    let coords = vec4<i32>(
902      batch,
903      row / outWidth,
904      row % outWidth,
905      col);
906    `:`
907    let coords = vec4<i32>(
908      batch,
909      row,
910      col / outWidth,
911      col % outWidth);
912    `,f=t?"row":"col",m=t?"col":"row",g=`
913    let inChannels = i32(uniforms.w_shape[2]);
914    let outWidth = ${t?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
915    let outRow = ${f} / outWidth;
916    let outCol = ${f} % outWidth;
917
918    let WRow = ${m} / (i32(uniforms.w_shape[1]) * inChannels);
919    let WCol = ${m} / inChannels % i32(uniforms.w_shape[1]);
920    let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];
921    let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];
922    let xCh = ${m} % inChannels;
923    var resData = ${i3(o,d)}(0.0);
924    // The bounds checking is always needed since we use it to pad zero for
925    // the 'same' padding type.
926    if (xRow >= 0 && xRow < ${t?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])"} && xCol >= 0 && xCol < ${t?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])"}) {
927      ${c}
928      let xIndex = getIndexFromCoords4D(coord, vec4<i32>(uniforms.x_shape));
929      ${(t=>{switch(t){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${d}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw Error(`innerElementSize ${t} is not supported.`)}})(o)}
930    }
931    return resData;`,y=t?r&&a?`
932    let col = colIn * ${o};
933    ${g}`:`
934    let col = colIn * ${o};
935    if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {
936      ${g}
937    }
938    return ${i3(o,d)}(0.0);`:a&&i?`
939    let col = colIn * ${o};
940    ${g}`:`
941    let col = colIn * ${o};
942    if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
943      ${g}
944    }
945    return ${i3(o,d)}(0.0);`,_=t?a&&i?p(u):`
946    let col = colIn * ${u};
947    if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
948      ${p(u)}
949    }
950    return ${i3(u,d)}(0.0);`:`
951    let col = colIn * ${u};
952    if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) {
953      ${p(u)}
954    }
955    return ${i3(u,d)}(0.0);`,$=i3(l,d),b=t?i3(o,d):i3(u,d),v=t?i3(u,d):i3(o,d),w=iJ(s,$,d);return`
956    fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${b} {
957      ${t?y:_}
958    }
959
960    fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${v} {
961      ${t?_:y}
962    }
963
964    fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${$}) {
965      let col = colIn * ${l};
966      if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer)
967      {
968      var value = valueIn;
969      let outWidth = ${t?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
970      ${h}
971      ${i4(n)}
972      ${w}
973      setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);
974      }
975    }`},ao=(t,r,i,a,n,s,o,u,l)=>{let d="NHWC"===r.format,p=d?t[0].dims[3]:t[0].dims[1],c=i[0],h=d?i[2]:i[3],f=d?i[1]:i[2],m=d?i[3]:i[1],g=d&&(p%4==0||p%3==0)&&m%4==0,y=d?m:h*f,_=d?h*f:m,$=[8,8,1],b=a<=8?[4,1,1]:[4,4,1],v=[Math.ceil(y/$[0]/b[0]),Math.ceil(_/$[1]/b[1]),Math.ceil(c/$[2]/b[2])];te("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${v}`);let w=g?d&&p%4!=0?3:4:1,x=$[1]*b[1],S=$[0]*b[0],k=Math.max($[0]*w,$[1]),T=a%x==0,C=n%S==0,I=s%k==0,E=g?[w,4,4]:[1,1,1],z=[{type:6,data:a},{type:6,data:n},{type:6,data:s},{type:6,data:[r.pads[0],r.pads[1]]},{type:6,data:r.strides},{type:6,data:r.dilations}];i0(r,z),z.push(...tS(t[0].dims,t[1].dims));let A=["rank","rank"];return o&&(z.push(...tS(t[2].dims)),A.push("rank")),z.push(...tS(i)),{name:"Conv2DMatMul",shaderCache:{hint:`${r.cacheKey};${w};${g};${T};${C};${I};${x};${S};${k}`,inputDependencies:A},getRunData:()=>({outputs:[{dims:l?l(i):i,dataType:t[0].dataType}],dispatchGroup:{x:v[0],y:v[1],z:v[2]},programUniforms:z}),getShaderSource:a=>{let n=[{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}];i1(r,n);let s=g?4:1,l=tw(t[0].dataType),p=`
976      fn setOutputAtIndex(flatIndex : i32, value : ${g?`vec4<${l}>`:l}) {
977        result[flatIndex] = ${g?`vec4<${l}>`:l}(value);
978      }
979      fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${g?`vec4<${l}>`:l}) {
980        let flatIndex = getOutputIndexFromCoords(vec4<i32>(d0, d1, d2, d3));
981        setOutputAtIndex(flatIndex ${g?"/ 4":""}, value);
982      }`,c=[tA("x",t[0].dataType,t[0].dims.length,3===w?1:w),tA("w",t[1].dataType,t[1].dims.length,s)],h=tO("result",t[0].dataType,i.length,s);if(o){let r=tA("bias",t[2].dataType,t[2].dims.length,s);c.push(r),p+=`
983        fn getBiasByOutputCoords(coords : vec4<i32>) -> ${g?`vec4<${l}>`:l} {
984          return bias[coords.${d?"w":"y"}${g?"/ 4":""}];
985        }`}return`
986        ${i6("uniforms.result_strides")}
987        //struct Uniforms { xShape : vec4<i32>, wShape : vec4<i32>, outShape : vec4<i32>,
988        //  outShapeStrides: vec3<i32>, filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>,
989        //  dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32 };
990        ${a.registerUniforms(n).declareVariables(...c,h)}
991        ${p}
992        ${as(d,T,C,I,o,r,E[0],E[1],E[2],l)}
993        ${g?ae(b,$,l,void 0,!d,k):ai(b,$,l,void 0,!d,k,!1,void 0,u)}`}}}}),us=W(()=>{oG(),oF(),oX(),oY(),ue(),ut(),au=t=>{let r=1;for(let i=0;i<t.length;i++)r*=t[i];return r},al=t=>"number"==typeof t?[t,t,t]:t,ad=(t,r)=>r<=1?t:t+(t-1)*(r-1),ap=(t,r,i,a=1)=>{let n=ad(r,a);return Math.floor((t[0]*(i-1)-i+n)/2)},ac=(t,r,i,a,n)=>{null==n&&(n=ap(t,r[0],a[0]));let s=[0,0,0,i];for(let i=0;i<3;i++)t[i]+2*n>=r[i]&&(s[i]=Math.trunc((t[i]-r[i]+2*n)/a[i]+1));return s},ah=(t,r,i,a,n,s,o,u,l,d)=>{let p,c,h,f;if("VALID"===t&&(t=0),"number"==typeof t){p={top:t,bottom:t,left:t,right:t,front:t,back:t};let m=ac([r,i,a,1],[u,l,d],1,[n,s,o],t);c=m[0],h=m[1],f=m[2]}else if(Array.isArray(t)){if(!t.every((t,r,i)=>t===i[0]))throw Error(`Unsupported padding parameter: ${t}`);p={top:t[0],bottom:t[1],left:t[2],right:t[3],front:t[4],back:t[5]};let m=ac([r,i,a,1],[u,l,d],1,[n,s,o],t[0]);c=m[0],h=m[1],f=m[2]}else if("SAME_UPPER"===t){c=Math.ceil(r/n),h=Math.ceil(i/s),f=Math.ceil(a/o);
993let t=(c-1)*n+u-r,m=(h-1)*s+l-i,g=(f-1)*o+d-a,y=Math.floor(t/2),_=Math.floor(m/2),$=Math.floor(g/2);p={top:_,bottom:m-_,left:$,right:g-$,front:y,back:t-y}}else throw Error(`Unknown padding parameter: ${t}`);return{padInfo:p,outDepth:c,outHeight:h,outWidth:f}},af=(t,r,i,a,n,s=!1,o="channelsLast")=>{let u,l,d,p,c;if("channelsLast"===o)[u,l,d,p,c]=t;else if("channelsFirst"===o)[u,c,l,d,p]=t;else throw Error(`Unknown dataFormat ${o}`);let[h,,f,m,g]=r,[y,_,$]=al(i),[b,v,w]=al(a),x=ad(f,b),S=ad(m,v),k=ad(g,w),{padInfo:T,outDepth:C,outHeight:I,outWidth:E}=ah(n,l,d,p,y,_,$,x,S,k),z=s?h*c:h,A=[0,0,0,0,0];return"channelsFirst"===o?A=[u,z,C,I,E]:"channelsLast"===o&&(A=[u,C,I,E,z]),{batchSize:u,dataFormat:o,inDepth:l,inHeight:d,inWidth:p,inChannels:c,outDepth:C,outHeight:I,outWidth:E,outChannels:z,padInfo:T,strideDepth:y,strideHeight:_,strideWidth:$,filterDepth:f,filterHeight:m,filterWidth:g,effectiveFilterDepth:x,effectiveFilterHeight:S,effectiveFilterWidth:k,dilationDepth:b,dilationHeight:v,dilationWidth:w,inShape:t,outShape:A,filterShape:r}},am=(t,r,i,a,n,s)=>{let o="channelsLast"===s,u=(o?t[0].dims[3]:t[0].dims[1],[Math.ceil(au(({x:i.map((t,r)=>r)}).x.map(t=>i[t]))/64),1,1]);te("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${u}`);let l=[{type:12,data:tm.size(i)},{type:12,data:a},{type:12,data:n},{type:12,data:r.strides},{type:12,data:r.dilations}];i0(r,l),l.push(...tS(t[0].dims,t[1].dims));let d=["rank","rank"],p=3===t.length;return p&&(l.push(...tS(t[2].dims)),d.push("rank")),l.push(...tS(i)),{name:"Conv3DNaive",shaderCache:{hint:`${r.cacheKey};${o};1;${p}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:i,dataType:t[0].dataType}],dispatchGroup:{x:u[0],y:u[1],z:u[2]},programUniforms:l}),getShaderSource:s=>{let u=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:a.length},{name:"pads",type:"u32",length:n.length},{name:"strides",type:"u32",length:r.strides.length},{name:"dilations",type:"u32",length:r.dilations.length}];i1(r,u);let l=tw(t[0].dataType),d=tA("x",t[0].dataType,t[0].dims.length,1),c=tA("W",t[1].dataType,t[1].dims.length,1),h=[d,c],f=tO("result",t[0].dataType,i.length,1),m="";if(p){let r=tA("bias",t[2].dataType,t[2].dims.length,1);h.push(r),m+=`
994        fn getBiasByOutputCoords(coords : array<u32, 5>) -> ${l} {
995          return bias[${o?tE("coords",4,5):tE("coords",1,5)}];
996        }`}let g=i3(1,l),y=iJ(r,g,l);return`
997            ${m}
998            fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
999              let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
1000              return ${d.getByIndices("aIndices")};
1001            }
1002            fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
1003              let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
1004              return ${c.getByIndices("aIndices")};
1005            }
1006          ${s.registerUniforms(u).declareVariables(...h,f)}
1007          ${s.mainStart()}
1008          ${s.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1009              let coords = ${f.offsetToIndices("global_idx")};
1010              let batch = ${tE("coords",0,d.rank)};
1011              let d2 = ${o?tE("coords",d.rank-1,d.rank):tE("coords",1,d.rank)};
1012              let xFRCCorner = vec3<u32>(${o?tE("coords",1,d.rank):tE("coords",2,d.rank)},
1013              ${o?tE("coords",2,d.rank):tE("coords",3,d.rank)},
1014              ${o?tE("coords",3,d.rank):tE("coords",4,d.rank)}) * uniforms.strides - uniforms.pads;
1015              let xFCorner = xFRCCorner.x;
1016              let xRCorner = xFRCCorner.y;
1017              let xCCorner = xFRCCorner.z;
1018              let xShapeY = ${o?tE("uniforms.x_shape",1,d.rank):tE("uniforms.x_shape",2,d.rank)};
1019              let xShapeZ = ${o?tE("uniforms.x_shape",2,d.rank):tE("uniforms.x_shape",3,d.rank)};
1020              let xShapeW = ${o?tE("uniforms.x_shape",3,d.rank):tE("uniforms.x_shape",4,d.rank)};
1021              let xShapeU = ${o?tE("uniforms.x_shape",4,d.rank):tE("uniforms.x_shape",1,d.rank)};
1022              let inputDepthNearestVec4 = (xShapeU / 4) * 4;
1023              let inputDepthVec4Remainder = xShapeU % 4;
1024
1025              var value = 0.0;
1026              for (var wF = 0u;
1026 wF < uniforms.filter_dims[0]; wF++) {
1027                let xF = xFCorner + wF * uniforms.dilations[0];
1028                if (xF < 0 || xF >= xShapeY) {
1029                  continue;
1030                }
1031
1032                for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) {
1033                  let xR = xRCorner + wR * uniforms.dilations[1];
1034                  if (xR < 0 || xR >= xShapeZ) {
1035                    continue;
1036                  }
1037
1038                  for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) {
1039                    let xC = xCCorner + wC * uniforms.dilations[2];
1040                    if (xC < 0 || xC >= xShapeW) {
1041                      continue;
1042                    }
1043
1044                    for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) {
1045                      ${o?`let xValues = vec4<f32>(
1046                               getX(batch, xF, xR, xC, d1),
1047                               getX(batch, xF, xR, xC, d1 + 1),
1048                               getX(batch, xF, xR, xC, d1 + 2),
1049                               getX(batch, xF, xR, xC, d1 + 3));
1050                            `:`let xValues = vec4<f32>(
1051                               getX(batch, d1, xF, xR, xC),
1052                               getX(batch, d1 + 1, xF, xR, xC),
1053                               getX(batch, d1 + 2, xF, xR, xC),
1054                               getX(batch, d1 + 3, xF, xR, xC));
1055                            `}
1056                            let wValues = vec4<f32>(
1057                              getW(d2, d1, wF, wR, wC),
1058                              getW(d2, d1 + 1, wF, wR, wC),
1059                              getW(d2, d1 + 2, wF, wR, wC),
1060                              getW(d2, d1 + 3, wF, wR, wC));
1061                      value += dot(xValues, wValues);
1062                    }
1063                    if (inputDepthVec4Remainder == 1) {
1064                        ${o?`value += getX(batch, xF, xR, xC, inputDepthNearestVec4)
1065                          * getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC)
1066                          * getW(d2, inputDepthNearestVec4, wF, wR, wC);`}
1067                    } else if (inputDepthVec4Remainder == 2) {
1068                      ${o?`let xValues = vec2<f32>(
1069                        getX(batch, xF, xR, xC, inputDepthNearestVec4),
1070                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1));
1071                      `:`let xValues = vec2<f32>(
1072                        getX(batch, inputDepthNearestVec4, xF, xR, xC),
1073                        getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC));
1074                    `}
1075                    let wValues = vec2<f32>(
1076                      getW(d2, inputDepthNearestVec4, wF, wR, wC),
1077                      getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC));
1078                      value += dot(xValues, wValues);
1079                    } else if (inputDepthVec4Remainder == 3) {
1080                      ${o?`let xValues = vec3<f32>(
1081                        getX(batch, xF, xR, xC, inputDepthNearestVec4),
1082                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1),
1083                        getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2));
1084                      `:`let xValues = vec3<f32>(
1085                        getX(batch, inputDepthNearestVec4, xF, xR, xC),
1086                        getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC),
1087                        getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC));
1088                    `}
1089                    let wValues = vec3<f32>(
1090                      getW(d2, inputDepthNearestVec4, wF, wR, wC),
1091                      getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC),
1092                      getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC));
1093                      value += dot(xValues, wValues);
1094                    }
1095                  }
1096                }
1097              }
1098              ${p?"value = value + getBiasByOutputCoords(coords)":""};
1099              ${y}
1100              result[global_idx] = f32(value);
1101          }`}}}}),uo=W(()=>{oG(),oX(),oY(),ue(),ag=(t,r,i,a)=>{let n=t.length>2,s=n?"value += b[output_channel];":"",o=t[0].dims,u=t[1].dims,l="NHWC"===r.format,d=l?i[3]:i[1],p=d/r.group,c=l&&p>=4?tk(d):1,h=tm.size(i)/c,f=[{type:12,data:h},{type:12,data:r.dilations},{type:12,data:[r.strides[0],r.strides[1]]},{type:12,data:[r.pads[0],r.pads[1]]},{type:12,data:p}];
vendor: 4,595 bytes, lines 1101-1170
1101return i0(r,f),f.push(...tS(o,[u[0],u[1],u[2],u[3]/c])),f.push(...tS([i[0],i[1],i[2],i[3]/c])),{name:"GroupedConv",shaderCache:{hint:`${r.cacheKey}_${c}`,inputDependencies:n?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:a?a(i):i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:f}),getShaderSource:a=>{let d=tO("output",t[0].dataType,i.length,c),p=tw(d.type.tensor),h=iJ(r,d.type.value,p),f=tA("x",t[0].dataType,o.length),m=tA("w",t[1].dataType,u.length,c),g=[f,m];n&&g.push(tA("b",t[2].dataType,t[2].dims,c));let y=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:r.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];i1(r,y);let _=l?`
1102      for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) {
1103        let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];
1104
1105        if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) {
1106          continue;
1107        }
1108
1109        for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) {
1110          let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];
1111          if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) {
1112            continue;
1113          }
1114
1115          for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) {
1116            let input_channel = in_channel_offset + wInChannel;
1117            let xVal = ${f.get("batch","xHeight","xWidth","input_channel")};
1118            let wVal = ${m.get("wHeight","wWidth","wInChannel","output_channel")};
1119            value += xVal * wVal;
1120          }
1121        }
1122      }
1123      `:`
1124      for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) {
1125        let input_channel = in_channel_offset + wInChannel;
1126        for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) {
1127          let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];
1128
1129          if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) {
1130            continue;
1131          }
1132
1133          for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) {
1134            let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];
1135            if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) {
1136              continue;
1137            }
1138
1139            let xVal = ${f.get("batch","input_channel","xHeight","xWidth")};
1140            let wVal = ${m.get("output_channel","wInChannel","wHeight","wWidth")};
1141            value += xVal * wVal;
1142          }
1143        }
1144      }
1145      `;return`
1146  ${a.registerUniforms(y).declareVariables(...g,d)}
1147
1148  ${a.mainStart()}
1149    ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1150
1151    let outputIndices = ${d.offsetToIndices("global_idx")};
1152    let batch: u32 = outputIndices[0];
1153    let output_channel: u32 = outputIndices[${l?3:1}];
1154    let xRCCorner: vec2<u32> = vec2<u32>(outputIndices[${l?1:2}], outputIndices[${l?2:3}]) * uniforms.strides - uniforms.pads;
1155    let group_id: u32 = output_channel * ${c} / uniforms.output_channels_per_group;
1156    var in_channel_offset = group_id * uniforms.w_shape[${l?2:1}];
1157
1158    var value: ${d.type.value} = ${d.type.value}(0);
1159    ${_}
1160    ${s}
1161    ${h}
1162    ${d.setByOffset("global_idx","value")}
1163  }`}}},ay=(t,r,i,a)=>{let n=t.length>2,s=tk(i[3]),o=tk(i[2]),u=tm.size(i)/s/o,l=[t[0].dims[0],t[0].dims[1],t[0].dims[2],t[0].dims[3]/s],d=[t[1].dims[0],t[1].dims[1],t[1].dims[2],t[1].dims[3]/s],p=[i[0],i[1],i[2],i[3]/s],c=[{type:12,data:u},{type:6,data:[r.strides[0],r.strides[1]]},{type:6,data:[r.pads[0],r.pads[1]]}];i0(r,c),c.push(...tS(l,d,p));let h=(o-1)*r.strides[1]+d[1];return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${r.cacheKey};${s};${o};${h};${d[0]};${d[1]}`,inputDependencies:n?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:a?a(i):i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:c}),getShaderSource:i=>{let a=tO("output",t[0].dataType,p.length,s),u=tw(a.type.tensor),c=iJ(r,a.type.value,u),f=tA("x",t[0].dataType,l.length,s),m=tA("w",t[1].dataType,d.length,s),g=[f,m];n&&g.push(tA("b",t[2].dataType,t[2].dims,s));let y=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return i1(r,y),`
1164  ${i.registerUniforms(y).declareVariables(...g,a)}
1165  ${i.mainStart()}
1166    ${i.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1167    let width0 = uniforms.output_shape[3];
1168    let output_channel = global_idx % width0;
1169    var index1 = global_idx / width0;
1170    let width1 = uniforms.output_shape[2] / ${o}u;
1171    let col = (index1 % width1) * ${o}u;
1172    index1 = index1 / width1;
1173    let row = index1 % uniforms.output_shape[1];
1174    let batch = index1 / uniforms.output_shape[1];
1175
1176    let x_corner = vec2<i32>(i32(row), i32(col)) * uniforms.strides - uniforms.pads;
1177
1178    var x_vals: array<${f.type.value}, ${h}>;
1179    var values: array<${a.type.value}, ${o}>;
1180    let input_channel = output_channel;
1181    // Use constant instead of uniform can give better performance for w's height/width.
1182    for (var w_height: u32 = 0u; w_height < ${d[0]}; w_height++) {
1183      let x_height = x_corner.x + i32(w_height);
1184      if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) {
1185        for (var i = 0; i < ${h}; i++) {
1186          let x_width = x_corner.y + i;
1187          if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) {
1188            x_vals[i] = ${f.get("batch","u32(x_height)","u32(x_width)","input_channel")};
1189          } else {
1190            x_vals[i] = ${f.type.value}(0);
1191          }
1192        }
1193        for (var w_width: u32 = 0u; w_width < ${d[1]}; w_width++) {
1194          let w_val = ${m.get("w_height","w_width","0","output_channel")};
1195          for (var i = 0u; i < ${o}u; i++) {
1196            values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]);
1197          }
1198        }
1199      }
1200    }
1201
1202    for (var i = 0u; i < ${o}u; i++) {
1203      var value = values[i];
1204      ${n?"value += b[output_channel];":""}
1205      ${c}
1206      ${a.set("batch","row","col + i","output_channel","value")};
1207    }
1208  }`}}}}),uu=W(()=>{oX(),un(),us(),ua(),uo(),ue(),ui(),oJ(),a_=(t,r,i,a,n,s)=>{let o=t[0],u=t.slice(s?1:2,s?3:4),l=u.length,d=r[0],p=r.slice(2).map((t,r)=>t+(t-1)*(i[r]-1)),c=u.map((t,r)=>t+a[r]+a[r+l]).map((t,r)=>Math.floor((t-p[r]+n[r])/n[r]));return c.splice(0,0,o),c.splice(s?3:1,0,d),c},a$=[2,3,1,0],ab=(t,r)=>{if(!t||2!==t.length&&3!==t.length)throw Error("Conv requires 2 or 3 inputs");if(t[0].dims.length>5)throw Error("greater than 5D is not supported");if(t[0].dims.length!==t[1].dims.length)throw Error("filter does not have same dimension as input");if(t[0].dims["NHWC"===r.format?t[0].dims.length-1:1]!==t[1].dims[1]*r.group)throw Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(3===t.length&&(1!==t[2].dims.length||t[1].dims[0]!==t[2].dims[0]))throw Error("invalid bias");let i=t[0].dims.length-2;if(r.dilations.length!==i)throw Error(`dilations should be ${i}D`);if(r.strides.length!==i)throw Error(`strides should be ${i}D`);if(r.pads.length!==2*i)throw Error(`pads should be ${2*i}D`);if(0!==r.kernelShape.length&&r.kernelShape.length!==t[1].dims.length-2)throw Error("invalid kernel shape")},av=(t,r)=>{let i=t.kernelShape.slice();i.length<r[1].dims.length-2&&i.push(...Array(r[1].dims.length-2-i.length).fill(0));for(let t=2;t<r[1].dims.length;++t)0===i[t-2]&&(i[t-2]=r[1].dims[t]);let a=t.pads.slice();tg.adjustPadsBasedOnAutoPad(r[0].dims,t.strides,t.dilations,i,a,"NHWC"===t.format,t.autoPad);let n=Object.assign({},t);return Object.assign(n,{kernelShape:i,pads:a}),n},aw=t=>{let r=i2(t),i=t.format;return{autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][t.auto_pad],format:i,dilations:t.dilations,group:t.group,kernelShape:t.kernel_shape,pads:t.pads,strides:t.strides,wIsConst:t.w_is_const(),...r,cacheKey:`${t.format};${r.activation};`}},ax=(t,r,i,a)=>{let n="NHWC"===i.format,s=a_(r[0].dims,r[1].dims,i.dilations,i.pads,i.strides,n);if(1!==i.group){let o=[r[0]];if(n){let a=t.kernelCustomData.wT??t.compute(tV(r[1],a$),{inputs:[1],outputs:[i.wIsConst?-2:-1]})[0];i.wIsConst&&!t.kernelCustomData.wT&&(t.kernelCustomData.wT=a),o.push(a)}else o.push(r[1]);3===r.length&&o.push(r[2]),!t.adapterInfo.isArchitecture("ampere")&&n&&r[1].dims[0]===i.group&&1===r[1].dims[1]&&1===i.dilations[0]&&1===i.dilations[1]?t.compute(ay(o,i,s,a),{inputs:o}):t.compute(ag(o,i,s,a),{inputs:o});return}let o=3===r.length,u=r[0].dims[n?1:2],l=r[0].dims[n?2:3],d=r[0].dims[n?3:1],p=r[1].dims[2],c=r[1].dims[3],h=s[n?1:2],f=s[n?2:3],m=s[n?3:1],g=n&&p===u&&c===l&&0===i.pads[0]&&0===i.pads[1];if(g||1===p&&1===c&&1===i.dilations[0]&&1===i.dilations[1]&&1===i.strides[0]&&1===i.strides[1]&&0===i.pads[0]&&0===i.pads[1]){let p=s[0],c,y,_,$=[];if(n){let a=t.kernelCustomData.wT??t.compute(tV(r[1],a$),{inputs:[1],outputs:[i.wIsConst?-2:-1]})[0];if(i.wIsConst&&!t.kernelCustomData.wT&&(t.kernelCustomData.wT=a),g){let t=u*l*d;c=r[0].reshape([1,p,t]),y=a.reshape([1,t,m]),_=[1,p,m]}else c=r[0].reshape([p,u*l,d]),y=a.reshape([1,d,m]),_=[p,h*f,m];$.push(c),$.push(y)}else c=r[0].reshape([p,d,u*l]),y=r[1].reshape([1,m,d]),_=[p,m,h*f],$.pu
vendor: 5,551 bytes, lines 1208-1250
1208sh(y),$.push(c);o&&$.push(r[2]);let b=_[2],v=$[0].dims[$[0].dims.length-1];b<8&&v<8?t.compute(i5($,i,s,_,n,a),{inputs:$}):t.compute(an($,i,s,_,n,a),{inputs:$});return}let y=t.kernelCustomData.wT??t.compute(tV(r[1],a$),{inputs:[1],outputs:[i.wIsConst?-2:-1]})[0];i.wIsConst&&!t.kernelCustomData.wT&&(t.kernelCustomData.wT=y);let _=[r[0],y];o&&_.push(r[2]);let $=n?h*f:m,b=n?m:h*f,v=p*c*d;t.compute(ao(_,i,s,$,b,v,o,!0,a),{inputs:_})},aS=(t,r)=>{let i="NHWC"===r.format,a=[t.inputs[0].reshape(i?[t.inputs[0].dims[0],1,t.inputs[0].dims[1],t.inputs[0].dims[2]]:[t.inputs[0].dims[0],t.inputs[0].dims[1],1,t.inputs[0].dims[2]]),t.inputs[1].reshape([t.inputs[1].dims[0],t.inputs[1].dims[1],1,t.inputs[1].dims[2]])];3===t.inputs.length&&a.push(t.inputs[2]);let n=[0,r.pads[0],0,r.pads[1]],s=[1].concat(r.strides),o=[1].concat(r.dilations),u=[1].concat(r.kernelShape),l=av({...r,pads:n,strides:s,dilations:o,kernelShape:u},a);ax(t,a,l,t=>i?[t[0],t[2],t[3]]:[t[0],t[1],t[3]])},ak=(t,r,i)=>{let a="NHWC"===i.format?"channelsLast":"channelsFirst",n=av(i,r),s="NOTSET"===i.autoPad?i.pads:i.autoPad,o=af(r[0].dims,r[1].dims,i.strides,i.dilations,s,!1,a);t.compute(am(r,n,o.outShape,[o.filterDepth,o.filterHeight,o.filterWidth],[o.padInfo.front,o.padInfo.top,o.padInfo.left],a))},aT=(t,r)=>{if(ab(t.inputs,r),3===t.inputs[0].dims.length)aS(t,r);else if(5===t.inputs[0].dims.length)ak(t,t.inputs,r);else{let i=av(r,t.inputs);ax(t,t.inputs,i)}}}),ul=W(()=>{oG(),oF(),oX(),oY(),aC=(t,r,i)=>{let a=t.length>2,n=r.outputShape,s="NHWC"===r.format,o=r.group,u=t[1].dims,l=u[2]/o,d=u[3],p=s?tk(l):1,c=s?tk(d):1,h=s?1===d?p:c:1,f=tm.size(n)/c,m=[Math.ceil(f/64),1,1];te("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${m}`);let g=["rank","rank"],y=[r.strides[0],r.strides[1]],_=[r.kernelShape[s?1:2],r.kernelShape[s?2:3]],$=[r.dilations[0],r.dilations[1]],b=[_[0]+(r.dilations[0]<=1?0:(r.kernelShape[s?1:2]-1)*(r.dilations[0]-1)),_[1]+(r.dilations[1]<=1?0:(r.kernelShape[s?2:3]-1)*(r.dilations[1]-1))],v=[b[0]-1-Math.floor((r.pads[0]+r.pads[2])/2),b[1]-1-Math.floor((r.pads[1]+r.pads[3])/2)],w=[{type:12,data:f},{type:12,data:y},{type:12,data:_},{type:12,data:$},{type:12,data:b},{type:6,data:v},{type:12,data:l},{type:12,data:d},...tS(t[0].dims,t[1].dims)];return a&&(w.push(...tS(t[2].dims)),g.push("rank")),w.push(...tS(n)),{name:"ConvTranspose2D",shaderCache:{hint:`${r.cacheKey};${p}${h}${c}${1===d}`,inputDependencies:g},getRunData:()=>({dispatchGroup:{x:m[0],y:m[1],z:m[2]},outputs:[{dims:i?i(n):n,dataType:t[0].dataType}],programUniforms:w}),getShaderSource:r=>{let i=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:y.length},{name:"filter_dims",type:"u32",length:_.length},{name:"dilations",type:"u32",length:_.length},{name:"effective_filter_dims",type:"u32",length:b.length},{name:"pads",type:"i32",length:v.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],o=tw(t[0].dataType),u=s?1:2,l=s?2:3,f=s?3:1,m=tA("W",t[1].dataType,t[1].dims.length,h),g=tA("Dy",t[0].dataType,t[0].dims.length,p),$=[g,m];a&&$.push(tA("bias",t[2].dataType,[n[f]].length,c));let w=tO("result",t[0].dataType,n.length,c),x=`
1209            let outputIndices = ${w.offsetToIndices(`global_idx * ${c}`)};
1210            let batch = ${w.indicesGet("outputIndices",0)};
1211            let d1 = ${w.indicesGet("outputIndices",f)};
1212            let r = ${w.indicesGet("outputIndices",u)};
1213            let c = ${w.indicesGet("outputIndices",l)};
1214            let dyCorner = vec2<i32>(i32(r), i32(c)) - uniforms.pads;
1215            let dyRCorner = dyCorner.x;
1216            let dyCCorner = dyCorner.y;
1217            let groupId = d1 / uniforms.output_channels_per_group;
1218            let wOutChannel = d1 - groupId * uniforms.output_channels_per_group;
1219            // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
1220            // ? = to be determined. : = across all values in that axis.
1221            var dotProd = ${w.type.value}(0.0);
1222            var wR: u32 = 0;
1223            if (uniforms.dilations.x == 1) {
1224              // Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0
1225              wR = u32(((dyRCorner + i32(uniforms.strides.x) - 1) / i32(uniforms.strides.x)) * i32(uniforms.strides.x) - dyRCorner);
1226            }
1227            for (; wR < uniforms.effective_filter_dims.x; wR = wR + 1) {
1228              if (wR % uniforms.dilations.x != 0) {
1229                continue;
1230              }
1231              let dyR = (${o}(dyRCorner) + ${o}(wR)) / ${o}(uniforms.strides[0]);
1232              let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x;
1233              if (dyR < 0.0 || dyR >= ${o}(uniforms.Dy_shape[${u}]) || fract(dyR) > 0.0 ||
1234                  wRPerm < 0) {
1235                continue;
1236              }
1237              let idyR: u32 = u32(dyR);
1238              var wC: u32 = 0;
1239              if (uniforms.dilations.y == 1) {
1240                // Minimum wC >= 0 that satisfies (dyCCorner + wC) % (uniforms.strides.y) == 0
1241                wC = u32(((dyCCorner + i32(uniforms.strides.y) - 1) / i32(uniforms.strides.y)) * i32(uniforms.strides.y) - dyCCorner);
1242              }
1243
1244              for (; wC < uniforms.effective_filter_dims.y; wC = wC + 1) {
1245                if (wC % uniforms.dilations.y != 0) {
1246                  continue;
1247                }
1248                let dyC = (${o}(dyCCorner) + ${o}(wC)) / ${o}(uniforms.strides.y);
1249                let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y;
1250                if (dyC < 0.0 || dyC >= ${o}(uniforms.Dy_shape[${l}
1250]) ||
1251                    fract(dyC) > 0.0 || wCPerm < 0) {
1252                  continue;
1253                }
1254                let idyC: u32 = u32(dyC);
1255                var inputChannel = groupId * uniforms.input_channels_per_group;
1256                for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group; d2 = d2 + ${p}) {
1257                  let xValue = ${s?g.getByOffset(`${g.indicesToOffset(`${g.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${p}`):g.get("batch","inputChannel","idyR","idyC")};
1258                  ${(()=>{let t="";if(1===p)t+=`
1259        let w_offset = ${m.indicesToOffset(`${m.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)};
1260        let wValue = ${m.getByOffset(`w_offset / ${h}`)};
1261        dotProd = dotProd + xValue * wValue;`;else if(1===d)t+=`
1262          let wValue = ${m.getByOffset(`${m.indicesToOffset(`${m.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)} / ${h}`)};
1263          dotProd = dotProd + dot(xValue, wValue);`;else for(let r=0;r<p;r++)t+=`
1264            let wValue${r} = ${m.getByOffset(`${m.indicesToOffset(`${m.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel + ${r}, wOutChannel)`)} / ${h}`)};
1265            dotProd = dotProd + xValue[${r}] * wValue${r};`;return t})()}
1266                  inputChannel = inputChannel + ${p};
1267                }
1268                wC = wC + uniforms.strides.y - 1;
1269              }
1270              wR = wR + uniforms.strides[0] - 1;
1271            }
1272            let value = dotProd${a?` + bias[d1 / ${c}]`:""};
1273            ${w.setByOffset("global_idx","value")};
1274          `;return`
1275    ${r.registerUniforms(i).declareVariables(...$,w)}
1276      ${r.mainStart()}
1277      ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")};
1278    ${x}}`}}}}),ud=W(()=>{ul(),ue(),oJ(),aI=(t,r,i,a,n,s)=>(t-1)*r+i+(a-1)*n+1-s,aE=(t,r,i,a,n)=>{let s=Math.floor(t/2);"SAME_UPPER"===r?(i[a]=s,i[n]=t-s):"SAME_LOWER"===r&&(i[a]=t-s,i[n]=s)},az=(t,r,i,a,n,s,o,u,l,d)=>{let p=t.length-2,c=0===d.length;l.length<p&&l.push(...Array(p-l.length).fill(0));let h=t[0],f=r[u?3:1]*n;for(let n=0,h=t.length-p-(u?1:0);n<p;++n,++h){let u=t[h],f=c?u*o[n]:d[n];aE(aI(u,o[n],s[n],r[h],i[n],f),a,s,n,n+p),c&&d.push(o[n]*(u-1)+l[n]+(r[h]-1)*i[n]+1-s[n]-s[n+p])}d.splice(0,0,h),d.splice(u?3:1,0,f)},aA=(t,r)=>{let i=t.kernelShape.slice();if(0===t.kernelShape.length||0===t.kernelShape.reduce((t,r)=>t*r,1)){i.length=0;for(let t=2;t<r[1].dims.length;++t)i.push(r[1].dims[t])}let a="NHWC"===t.format;i.splice(0,0,r[1].dims[0]),i.splice(a?3:1,0,r[1].dims[1]);let n=t.pads.slice(),s=t.outputShape.slice(),o=t.outputPadding.slice(),u=r[0].dims,l=t.dilations.slice();0===l.reduce((t,r)=>t+r,0)&&(l=Array(r[0].dims.length-2).fill(1));let d=t.strides.slice();0===d.reduce((t,r)=>t+r,0)&&(d=Array(r[0].dims.length-2).fill(1)),az(u,i,l,t.autoPad,t.group,n,d,a,o,s);let p=Object.assign({},t);return Object.assign(p,{kernelShape:i,pads:n,outputPadding:o,outputShape:s,dilations:l,strides:d}),p},aO=t=>{let r=i2(t),i=t.format,a=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof t.autoPad>"u"?0:t.autoPad],n=t.dilations,s=t.group,o=t.kernelShape,u=t.pads,l=t.strides,d=t.wIsConst();return{autoPad:a,format:i,dilations:n,group:s,kernelShape:o,outputPadding:t.outputPadding,outputShape:t.outputShape,pads:u,strides:l,wIsConst:d,...r,cacheKey:`${t.format};${r.activation};`}},aR=(t,r)=>{if(!t||2!==t.length&&3!==t.length)throw Error("Conv requires 2 or 3 inputs");if(4!==t[0].dims.length&&3!==t[0].dims.length)throw Error("currently only support 2-dimensional conv");if(t[0].dims.length!==t[1].dims.length)throw Error("filter does not have same dimension as input");if(t[0].dims["NHWC"===r.format?t[0].dims.length-1:1]!==t[1].dims[0])throw Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let i=t[1].dims[1]*r.group;if(3===t.length&&(1!==t[2].dims.length||t[2].dims[0]!==i))throw Error("invalid bias");let a=t[0].dims.length-2;if(r.dilations.reduce((t,r)=>t+r,0)>0&&r.dilations.length!==a)throw Error(`dilations should be ${a}D`);if(r.strides.reduce((t,r)=>t+r,0)>0&&r.strides.length!==a)throw Error(`strides should be ${a}D`);
vendor: 4,638 bytes, lines 1278-1305
1278if(r.pads.reduce((t,r)=>t+r,0)>0&&r.pads.length!==2*a)throw Error(`pads should be ${2*a}D`);if(r.outputPadding.length!==a&&0!==r.outputPadding.length)throw Error(`output_padding should be ${a}D`);if(r.kernelShape.reduce((t,r)=>t+r,0)>0&&0!==r.kernelShape.length&&r.kernelShape.length!==t[1].dims.length-2)throw Error("invalid kernel shape");if(0!==r.outputShape.length&&r.outputShape.length!==t[0].dims.length-2)throw Error("invalid output shape")},aB=(t,r,i,a)=>{let n=t.kernelCustomData.wT??t.compute(tV(r[1],[2,3,0,1]),{inputs:[1],outputs:[i.wIsConst?-2:-1]})[0];i.wIsConst&&!t.kernelCustomData.wT&&(t.kernelCustomData.wT=n);let s=[r[0],n];3===r.length&&s.push(r[2]),t.compute(aC(s,i,a),{inputs:s})},aN=(t,r)=>{let i="NHWC"===r.format,a=[t.inputs[0].reshape(i?[t.inputs[0].dims[0],1,t.inputs[0].dims[1],t.inputs[0].dims[2]]:[t.inputs[0].dims[0],t.inputs[0].dims[1],1,t.inputs[0].dims[2]]),t.inputs[1].reshape([t.inputs[1].dims[0],t.inputs[1].dims[1],1,t.inputs[1].dims[2]])];3===t.inputs.length&&a.push(t.inputs[2]);let n=r.kernelShape;(0===n.length||0===n[0])&&(n=[t.inputs[1].dims[2]]);let s=r.dilations;(0===s.length||0===s[0])&&(s=[1]);let o=r.strides;(0===o.length||0===o[0])&&(o=[1]);let u=r.pads;0===u.length&&(u=[0,0]),u=[0,u[0],0,u[1]],o=[1].concat(o),s=[1].concat(s),n=[1].concat(n);let l=r.outputPadding;l=[0].concat(l);let d=aA({...r,pads:u,strides:o,dilations:s,kernelShape:n,outputPadding:l},a);aB(t,a,d,t=>i?[t[0],t[2],t[3]]:[t[0],t[1],t[3]])},aM=(t,r)=>{if(aR(t.inputs,r),3===t.inputs[0].dims.length)aN(t,r);else{let i=aA(r,t.inputs);aB(t,t.inputs,i)}}}),up=W(()=>{oG(),oX(),oZ(),oY(),aD=(t,r,i,a)=>{let n=tm.size(r),s=r.length,o=tA("input",t,s),u=tO("output",t,s),l=6===i.dataType?i.getInt32Array()[0]:Number(i.getBigInt64Array()[0]),d=tm.normalizeAxis(l,s);return{name:"CumSum",shaderCache:{hint:a.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:r,dataType:t}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:[{type:12,data:n},{type:12,data:d},...tS(r,r)]}),getShaderSource:t=>{let r=` i32(${o.indicesGet("inputIndices","uniforms.axis")}) `,i=tE("uniforms.input_shape","uniforms.axis",s),n=a.reverse?r+(a.exclusive?" + 1":""):"0",l=a.reverse?i:r+(a.exclusive?"":" + 1");return`
1279                ${t.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(o,u)}
1280                ${t.mainStart()}
1281                  ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1282                  var inputIndices = ${u.offsetToIndices("global_idx")};
1283                  var sum = ${u.type.value}(0);
1284                  let first : i32 = ${n};
1285                  let last : i32 = ${l};
1286                  for (var i : i32 = first; i < last; i++) {
1287                    ${o.indicesSet("inputIndices","uniforms.axis","u32(i)")};
1288                    sum = sum + ${o.getByIndices("inputIndices")};
1289                  }
1290                  ${u.setByOffset("global_idx","sum")};
1291                }`}}},aP=(t,r)=>{let i=t.inputs[0].dims,a=t.inputs[0].dataType,n=t.inputs[1];t.compute(aD(a,i,n,r),{inputs:[0]})},aU=t=>{let r=1===t.exclusive,i=1===t.reverse;return tc({exclusive:r,reverse:i})}}),uc=W(()=>{oG(),oX(),oZ(),oY(),aW=t=>{if(!t||1!==t.length)throw Error("DepthToSpace requires 1 input.");if(4!==t[0].dims.length)throw Error("DepthToSpace requires 4D input.")},aq=(t,r,i,a)=>{let n=[];n.push(`fn perm(i: ${a.type.indices}) -> ${i.type.indices} {
1292    var a: ${i.type.indices};`);for(let a=0;a<r;++a)n.push(i.indicesSet("a",t[a],`i[${a}]`));return n.push("return a;}"),n.join(`
1293`)},aL=(t,r)=>{let i,a,n,s,o,u,l="NHWC"===r.format,d=r.blocksize,p="DCR"===r.mode;l?([i,a,n,s]=t.dims,o=p?[i,a,n,d,d,s/d**2]:[i,a,n,s/d**2,d,d],u=p?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([i,a,n,s]=[t.dims[0],t.dims[2],t.dims[3],t.dims[1]],o=p?[i,d,d,s/d**2,a,n]:[i,s/d**2,d,d,a,n],u=p?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let c=t.reshape(o),h=c.dims.length,f=t.dataType,m=tA("a",f,h),g=tO("output",f,h);return{name:"DepthToSpace",shaderCache:{hint:`${t.dims};${r.blocksize};${r.mode}`,inputDependencies:["rank"]},getRunData:t=>{let r=l?[i,a*d,n*d,s/d**2]:[i,s/d**2,a*d,n*d],o=tm.size(r),p=c.dims,h=tm.sortBasedOnPerm(p,u);return{outputs:[{dims:r,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},...tS(p,h)]}},getShaderSource:t=>`
1294  ${t.registerUniform("output_size","u32").declareVariables(m,g)}
1295
1296  ${aq(u,h,m,g)}
1297
1298  ${t.mainStart()}
1299    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1300
1301    let indices = ${g.offsetToIndices("global_idx")};
1302    let aIndices = perm(indices);
1303
1304    ${g.setByOffset("global_idx",m.getByIndices("aIndices"))}
1305  }`}},aV=(t,r)=>
1305{aW(t.inputs),t.compute(aL(t.inputs[0],r))},aG=t=>tc({blocksize:t.blocksize,mode:t.mode,format:t.format})}),uh=W(()=>{oG(),oX(),oZ(),oY(),aj="^"+(aF="("+(aH="[a-zA-Z]|\\.\\.\\.")+")+")+"$",aK="^("+aF+",)*"+aF+"$",aQ=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let i=this.symbolToIndices.get(t);void 0===i?i=[r]:i.push(r),this.symbolToIndices.set(t,i)}},aZ=class{constructor(t,r){this.equation=r,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=[],this.outputDims=[];let[i,a]=r.includes("->")?r.split("->",2):[r,""];if(!i.match(RegExp(aK)))throw Error("Invalid LHS term");if(i.split(",").forEach((r,i)=>{let a=t[i].dims.slice();if(!r.match(RegExp(aj)))throw Error("Invalid LHS term");let n=this.processTerm(r,!0,a,i);this.lhs.push(n)}),""===a)a+=[...this.symbolToInfo.entries()].filter(([t,r])=>1===r.count||"..."===t).map(([t])=>t).join("");else if(!a.match(RegExp(aF)))throw Error("Invalid RHS");a.match(RegExp(aH,"g"))?.forEach(t=>{if("..."===t)this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let r=this.symbolToInfo.get(t);if(void 0===r)throw Error("Invalid RHS symbol");this.outputDims.push(r.dimValue)}}),this.rhs=this.processTerm(a,!1,this.outputDims)}addSymbol(t,r,i){let a=this.symbolToInfo.get(t);if(void 0!==a){if(a.dimValue!==r&&1!==a.count)throw Error("Dimension mismatch");a.count++,a.inputIndices.push(i)}else a={count:1,dimValue:r,inputIndices:[i]};this.symbolToInfo.set(t,a)}processTerm(t,r,i,a=-1){let n=i.length,s=!1,o=[],u=0;if(!t.match(RegExp(aj))&&!r&&""!==t)throw Error("Invalid LHS term");let l=t.match(RegExp(aH,"g")),d=new aQ(a);return l?.forEach((t,p)=>{if("..."===t){if(s)throw Error("Only one ellipsis is allowed per input term");s=!0;let t=n-l.length+1;if(t<0)throw Error("Ellipsis out of bounds");if(o=i.slice(u,u+t),this.hasEllipsis){if(this.ellipsisDims.length!==o.length||this.ellipsisDims.toString()!==o.toString())throw Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=o;else throw Error("Ellipsis must be specified in the LHS");for(let t=0;t<o.length;t++){let r=String.fromCharCode(48+t);d.addSymbol(r,p+t),this.addSymbol(r,i[u++],a)}}else d.addSymbol(t,p+(this.hasEllipsis?this.ellipsisDims.length-1:0)),this.addSymbol(t,i[u++],a)}),d}},aX=t=>t+"_max",aY=(t,r,i,a)=>{let n=t.map(t=>t.length).map((t,i)=>tA(`input${i}`,r,t)),s=tm.size(a),o=tO("output",r,a.length),u=[...i.symbolToInfo.keys()].filter(t=>!i.rhs.symbolToIndices.has(t));return{name:"Einsum",shaderCache:{hint:i.equation,inputDependencies:t.map(()=>"rank")},getRunData:()=>{let n=u.filter(t=>i.symbolToInfo.has(t)).map(t=>({type:12,data:i.symbolToInfo.get(t)?.dimValue||0}));n.push({type:12,data:s});let o=t.map((t,r)=>[...tS(t)]).reduce((t,r)=>t.concat(r),n);return o.push(...tS(a)),{outputs:[{dims:a,dataType:r}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:o}},getShaderSource:t=>{let r=[],a=[],s=[],l=[],d=[],p=i.symbolToInfo.size===i.rhs.symbolToIndices.size;i.symbolToInfo.forEach((t,u)=>{if(i.rhs.symbolToIndices.has(u)){let a=i.rhs.symbolToIndices.get(u)?.[0];void 0!==a&&i.lhs.forEach((i,s)=>{if(t.inputIndices.includes(s)){let t=i.symbolToIndices.get(u);if(void 0===t)throw Error("Invalid symbol error");t.forEach(t=>{r.push(`${n[s].indicesSet(`input${s}Indices`,t,o.indicesGet("outputIndices",a))}`)})}})}else i.lhs.forEach((r,i)=>{if(t.inputIndices.includes(i)){let t=r.symbolToIndices.get(u);if(void 0===t)throw Error("Invalid symbol error");t.forEach(t=>{a.push(`${n[i].indicesSet(`input${i}Indices`,t,`${u}`)}`)}),d.push(`prod *= ${n[i].getByIndices(`input${i}Indices`)};`)}}),s.push(`for(var ${u}: u32 = 0; ${u} < uniforms.${aX(u)}; ${u}++) {`),l.push("}")});let c=p?[...r,`let sum = ${n.map((t,r)=>t.getByIndices(`input${r}Indices`)).join(" * ")};`]:[...r,"var sum = 0.0;",...s,...a,"var prod = 1.0;",...d,"sum += prod;",...l];return`
1306            ${t.registerUniforms(u.map(t=>({name:`${aX(t)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...n,o)}
1307
1308            ${t.mainStart()}
1309            ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1310            var outputIndices = ${o.offsetToIndices("global_idx")};
1311            ${n.map((t,r)=>`var input${r}Indices: ${n[r].type.indices};`).join(`
1312`)}
1313            ${c.join(`
1314`)};
1315            ${o.setByOffset("global_idx","sum")};
1316          }`}}},aJ=(t,r)=>{let i=new aZ(t.inputs,r.equation),a=i.outputDims,n=t.inputs.map((t,r)=>t.dims);
vendor: 4,794 bytes, lines 1316-1360
1316t.compute(aY(n,t.inputs[0].dataType,i,a))},a0=t=>{let r=t.equation.replace(/\s+/g,"");return tc({equation:r})}}),uf=W(()=>{oG(),oX(),oY(),a1=t=>{if(!t||2!==t.length)throw Error("Expand requires 2 input.");let r=t[0].dims,i=Array.from(t[1].getBigInt64Array(),Number),a=i.length<r.length?0:i.length-r.length,n=r.length<i.length?0:r.length-i.length;for(;a<i.length&&n<r.length;++a,++n)if(i[a]!==r[n]&&1!==i[a]&&1!==r[n])throw Error("Expand requires shape to be broadcastable to input")},a2=(t,r)=>{let i=t.length-r.length,a=[];for(let r=0;r<i;++r)a.push(t[r]);for(let n=0;n<r.length;++n)a.push(1===r[n]?t[n+i]:r[n]);return a},a3=(t,r)=>t.length>r.length?a2(t,r):a2(r,t),a4=t=>{let r=t[0].dims,i=a3(r,Array.from(t[1].getBigInt64Array(),Number)),a=t[0].dataType,n=9===a||1===tm.size(r),s=9===a||r.length>0&&r[r.length-1]%4==0?4:1,o=n||i.length>0&&i[i.length-1]%4==0?4:1,u=Math.ceil(tm.size(i)/o),l=[{type:12,data:u},...tS(r,i)];return{name:"Expand",shaderCache:{hint:`${i.length};${s}${o}`,inputDependencies:["rank"]},getShaderSource:t=>{let n=tA("input",a,r.length,s),u=tO("output",a,i.length,o),l;if(9===a){let t=(t,r,i="")=>`
1317          let outputIndices${r} = ${u.offsetToIndices(`outputOffset + ${r}u`)};
1318          let offset${r} = ${n.broadcastedIndicesToOffset(`outputIndices${r}`,u)};
1319          let index${r} = offset${r} / 4u;
1320          let component${r} = offset${r} % 4u;
1321          ${t}[${r}] = ${i}(${n.getByOffset(`index${r}`)}[component${r}]);
1322        `;l=`
1323        let outputOffset = global_idx * ${o};
1324        var data = vec4<u32>(0);
1325        ${t("data",0,"u32")}
1326        ${t("data",1,"u32")}
1327        ${t("data",2,"u32")}
1328        ${t("data",3,"u32")}
1329        ${u.setByOffset("global_idx","data")}
1330      }`}else l=`
1331        let outputIndices = ${u.offsetToIndices(`global_idx * ${o}`)};
1332        let inputOffset = ${n.broadcastedIndicesToOffset("outputIndices",u)};
1333        let data = ${u.type.value}(${n.getByOffset(`inputOffset / ${s}`)});
1334        ${u.setByOffset("global_idx","data")}
1335      }`;return`
1336    ${t.registerUniform("vec_size","u32").declareVariables(n,u)}
1337    ${t.mainStart()}
1338    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
1339    ${l}`},getRunData:()=>({outputs:[{dims:i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:l})}},a6=t=>{a1(t.inputs),t.compute(a4(t.inputs),{inputs:[0]})}}),um=W(()=>{oG(),oX(),oY(),o8(),a8=t=>{let r=t[0].dataType,i=tm.size(t[0].dims),a=tm.size(t[1].dims),n=a%4==0;return{name:"FastGeluWithBias",shaderCache:{hint:`${n}`,inputDependencies:["type","type"]},getShaderSource:t=>{let i=tA("x",r,[1],4),a=tA("bias",r,[1],4),s=tO("y",r,[1],4),o=t=>`
1340      let bias${t}_offset: u32 = (global_idx * 4 + ${t}) % uniforms.bias_size;
1341      let bias${t} = ${a.getByOffset(`bias${t}_offset / 4`)}[bias${t}_offset % 4];`,u=n?`
1342      let bias = ${a.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${o(0)}${o(1)}${o(2)}${o(3)}
1343      let bias = ${i.type.value}(bias0, bias1, bias2, bias3);`;return`${t.registerUniforms([{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}]).declareVariables(i,a,s)}
1344
1345    ${ix(tx(r))}
1346
1347    ${t.mainStart(tb)}
1348      ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")}
1349
1350      let x = ${i.getByOffset("global_idx")};
1351      ${u}
1352      let x_in = x + bias;
1353      ${s.setByOffset("global_idx",iS("x_in"))}
1354    }`},getRunData:t=>({outputs:[{dims:t[0].dims,dataType:t[0].dataType}],programUniforms:[{type:12,data:Math.ceil(i/4)},{type:12,data:a}],dispatchGroup:{x:Math.ceil(i/tb/4)}})}},a5=t=>{t.inputs.length<2||0===tm.size(t.inputs[1].dims)?ik(t):t.compute(a8(t.inputs))}}),ug=W(()=>{oG(),oX(),oZ(),oY(),a9=t=>{if(!t||2!==t.length)throw Error("Gather requires 2 inputs.")},a7=(t,r)=>{let i=t[0].dims,a=t[1].dims,n=i.length,s=tm.normalizeAxis(r.axis,n),o=i.slice(0);o.splice(s,1,...a);let u=i[s],l=9===t[0].dataType?4:1,d=Math.ceil(tm.size(o)/l),p=[{type:12,data:d},{type:6,data:u},{type:12,data:s},...tS(t[0].dims,t[1].dims,o)];return{name:"Gather",shaderCache:{hint:r.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p}),getShaderSource:r=>{let i=tA("data",t[0].dataType,t[0].dims.length,l),u=tA("inputIndices",t[1].dataType,t[1].dims.length),d=tO("output",t[0].dataType,o.length,l),p=t=>{let r=a.length,l=`var indicesIndices${t}  = ${u.type.indices}(0);`;for(let i=0;i<r;i++)l+=`${r>1?`indicesIndices${t}[${i}]`:`indicesIndices${t}`} = ${o.length>1?`outputIndices${t}[uniforms.axis + ${i}]`:`outputIndices${t}`};`;l+=`
1355          var idx${t} = ${u.getByIndices(`indicesIndices${t}`)};
1356          if (idx${t} < 0) {
1357            idx${t} = idx${t} + uniforms.axisDimLimit;
1358          }
1359          var dataIndices${t} : ${i.type.indices};
1360        `;
1360for(let i=0,a=0;i<n;i++)i===s?(l+=`${n>1?`dataIndices${t}[${i}]`:`dataIndices${t}`} = u32(idx${t});`,a+=r):(l+=`${n>1?`dataIndices${t}[${i}]`:`dataIndices${t}`} = ${o.length>1?`outputIndices${t}[${a}]`:`outputIndices${t}`};`,a++);return l},c;if(9===t[0].dataType){let t=(t,r,a="")=>`
1361          let outputIndices${r} = ${d.offsetToIndices(`outputOffset + ${r}u`)};
1362          ${p(r)};
1363          let offset${r} = ${i.indicesToOffset(`dataIndices${r}`)};
1364          let index${r} = offset${r} / 4u;
1365          let component${r} = offset${r} % 4u;
1366          ${t}[${r}] = ${a}(${i.getByOffset(`index${r}`)}[component${r}]);
1367        `;c=`
1368        let outputOffset = global_idx * ${l};
1369        var value = vec4<u32>(0);
1370        ${t("value",0,"u32")}
1371        ${t("value",1,"u32")}
1372        ${t("value",2,"u32")}
1373        ${t("value",3,"u32")}
1374        ${d.setByOffset("global_idx","value")}
1375      `}else c=`
1376      let outputIndices = ${d.offsetToIndices("global_idx")};
1377      ${p("")};
1378      let value = ${i.getByIndices("dataIndices")};
1379      ${d.setByOffset("global_idx","value")};
1380      `;return`
1381      ${r.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(i,u,d)}
1382      ${r.mainStart()}
1383        ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1384        ${c}
1385      }`}}},ne=t=>tc({axis:t.axis}),nt=(t,r)=>{a9(t.inputs),t.compute(a7(t.inputs,r))}}),uy=W(()=>{oG(),oX(),oY(),nr=(t,r,i,a,n,s,o,u,l)=>{let d=[{type:12,data:s},{type:12,data:a},{type:12,data:n},{type:12,data:i},{type:12,data:o},{type:12,data:u},{type:12,data:l}],p=[s];return d.push(...tS(r.dims,p)),t.compute({name:"computeSliceOffsets",shaderCache:{hint:`${n.length}_${i.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:p,dataType:t.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:d}),getShaderSource:t=>{let a=tA("indices_data",r.dataType,r.dims.length),s=tO("input_slice_offsets_data",12,1,1),o=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:n.length},{name:"sizes_from_slice_dims_data",type:"u32",length:i.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return`
1386  ${t.registerUniforms(o).declareVariables(a,s)}
1387  ${t.mainStart()}
1388    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1389    let batch_idx = global_idx / uniforms.num_slices_per_batch;
1390    let base_offset = batch_idx * uniforms.input_batch_stride;
1391
1392    let slice_indices_base_offset = global_idx * uniforms.num_slice_dims;
1393    var relative_slice_offset = 0;
1394    for (var dim_idx = 0u; dim_idx < uniforms.num_slice_dims; dim_idx ++) {
1395      var index = i32(indices_data[dim_idx + slice_indices_base_offset].x);
1396      let input_dim_idx = uniforms.batch_dims + dim_idx;
1397      if (index < 0) {
1398        ${1===n.length?"index += i32(uniforms.input_dims);":"index += i32(uniforms.input_dims[input_dim_idx]);"}
1399      }
1400      ${1===i.length?"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data);":"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data[dim_idx]);"}
1401    }
1402
1403    input_slice_offsets_data[global_idx] =  base_offset + u32(relative_slice_offset);
1404  }`}},{inputs:[r],outputs:[-1]})[0]},ni=(t,r)=>{let i=t.inputs,a=i[0].dims,n=i[0].dataType,s=i[1].dims,o=s[s.length-1],u=tm.sizeToDimension(s,s.length-1),l=tm.sizeFromDimension(a,r.batchDims+o),d=tm.sizeToDimension(a,r.batchDims),p=tm.sizeFromDimension(a,r.batchDims),c=Array(o),h=l;for(let t=0;t<o;++t)c[o-1-t]=h,h*=a[r.batchDims+o-1-t];let f=nr(t,i[1],c,r.batchDims,a,u,u/d,p,o),m=r.batchDims+o;if(m>a.length)throw Error("last dimension of indices must not be larger than rank of input tensor");let g=s.slice(0,-1).concat(a.slice(m)),y=tm.size(g),_=[{type:12,data:y},{type:12,data:l},...tS(i[0].dims,f.dims,g)];t.compute({name:"GatherND",shaderCache:{hint:r.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:g,dataType:n}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:_}),getShaderSource:t=>{let r=tA("data",i[0].dataType,i[0].dims.length),a=tA("slice_offsets",12,f.dims.length),n=tO("output",i[0].dataType,g.length);return`
1405          ${t.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(r,a,n)}
1406            ${t.mainStart()}
1407            ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1408          let slice_offset = slice_offsets[global_idx / uniforms.slice_size];
1409          output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size];
1410        }`}},{inputs:[i[0],f]})},na=t=>({batchDims:t.batch_dims,cacheKey:""})}),u_=W(()=>{oG(),oX(),oZ(),oY(),nn=(t,r)=>{if(t.length<3||t.length>4)throw Error("GatherBlockQuantized requires 3 or 4 inputs.");let i=tm.normalizeAxis(r.quantizeAxis,t[0].dims.length),a=r.blockSize,n=t[0],s=t[2],o=4===t.length?t[3]:void 0;if(s.dims.length!==n.dims.length||!n.dims.map((t,r)=>r===i?Math.ceil(t/a)===s.dims[r]:t===s.dims[r]).reduce((t,r)=>t&&r,!0))throw Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(o){if(o.dataType!==n.dataType)throw Error("Zero point must have the same data type as the input tensor.");if(o.dims.length!==s.dims.length||!o.dims.map((t,r)=>t===s.dims[r]).reduce((t,r)=>t&&r,!0))throw Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},ns=(t,r)=>{let i=t[0].dims,a=t[1].dims,n=i.length,s=tm.normalizeAxis(r.gatherAxis,n),o=tm.normalizeAxis(r.quantizeAxis,n),u=i.slice(0);u.splice(s,1,...a);let l=tm.size(u),d=t[2].dataType,p=22===t[0].dataType,c=[{type:12,data:l},{type:12,data:o},{type:12,data:s},{type:12,data:r.blockSize},...tS(...t.map((t,r)=>t.dims),u)];return{name:"GatherBlockQuantized",shaderCache:{hint:`${r.cacheKey};${t.filter((t,r)=>1!==r).map(t=>t.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:t.length},(t,r)=>"rank")},getRunData:()=>({outputs:[{dims:u,dataType:d}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:c}),getShaderSource:r=>{let n=tA("data",t[0].dataType,t[0].dims.length),o=tA("inputIndices",t[1].dataType,t[1].dims.length),l=tA("scales",t[2].dataType,t[2].dims.length),c=t.length>3?tA("zeroPoint",t[3].dataType,t[3].dims.length):void 0,h=tO("output",d,u.length),f=[n,o,l];return c&&f.push(c),`
1411        ${r.registerUniforms([{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}]).declareVariables(...f,h)}
1412        ${r.mainStart()}
1413        let output_indices = ${h.offsetToIndices("global_idx")};
1414        var indices_indices = ${o.type.indices}(0);
1415        ${a.length>1?`
1416          for (var i: u32 = 0; i < ${a.length}; i++) {
1417            let index = ${h.indicesGet("output_indices","uniforms.gather_axis + i")};
1418            ${o.indicesSet("indices_indices","i","index")};
1419          }`:`indices_indices = ${h.indicesGet("output_indices","uniforms.gather_axis")};`};
1420        var data_indices = ${n.type.indices}(0);
1421        for (var i: u32 = 0; i < uniforms.gather_axis; i++) {
1422          let index = ${h.indicesGet("output_indices","i")};
1423          ${n.indicesSet("data_indices","i","index")};
1424        }
1425        var index_from_indices = ${o.getByIndices("indices_indices")};
1426        if (index_from_indices < 0) {
1427          index_from_indices += ${i[s]};
1428        }
1429        ${n.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")};
1430        for (var i = uniforms.gather_axis + 1; i < ${u.length}; i++) {
1431          let index = ${h.indicesGet("output_indices",`i + ${a.length} - 1`)};
1432          ${n.indicesSet("data_indices","i","index")};
1433        }
1434        let data_offset = ${n.indicesToOffset("data_indices")};
1435        let data_index = data_offset % 8;
1436        // Convert 4-bit packed data to 8-bit packed data.
1437        let packed_4bit_quantized_data = ${n.getByOffset("data_offset / 8")};
1438        let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f;
1439        let quantized_data_vec = ${p?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data));
1440        let quantized_data = quantized_data_vec[data_index / 2];
1441        var scale_indices = data_indices;
1442        let quantize_axis_index = ${l.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size;
1443        ${l.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")};
1444        var scale = ${l.getByIndices("scale_indices")};
1445        ${c?`
1446              let zero_point_indices = scale_indices;
1447              let zero_point_offset = ${c.indicesToOffset("zero_point_indices")};
1448              let zero_point_index = zero_point_offset % 8;
1449              let packed_4bit_zero_points = ${c.getByOffset("zero_point_offset / 8")};
1450              let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f;
1451              let zero_point_vec = ${p?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points));
1452              let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"};
1453        let dequantized_data = ${tx(d)}(quantized_data - zero_point) * scale;
1454        ${h.setByOffset("global_idx","dequantized_data")};
1455    }`}}},no=(t,r)=>{nn(t.inputs,r),t.compute(ns(t.inputs,r))},nu=t=>tc({blockSize:t.blockSize,gatherAxis:t.gatherAxis,quantizeAxis:t.quantizeAxis})}),u$=W(()=>{oG(),oX(),oZ(),oY(),nl=t=>{if(!t||2!==t.length)throw Error("GatherElements requires 2 inputs.");if(t[0].dims.length<1)throw Error("GatherElements requires that the data input be rank >= 1.");if(t[0].dims.length!==t[1].dims.length)throw Error(`GatherElements requires that the data input and
1456                     indices input tensors be of same rank.`)},nd=(t,r)=>{let i=t[0].dims,a=t[0].dataType,n=i.length,s=t[1].dims,o=t[1].dataType,u=tm.normalizeAxis(r.axis,n),l=i[u],d=s.slice(0),p=tm.size(d),c=tA("input",a,n),h=tA("indicesInput",o,s.length),f=tO("output",a,d.length),m=[{type:12,data:p},{type:6,data:l},{type:12,data:u}];return m.push(...tS(i,s,d)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:m}),getShaderSource:t=>`
1457      ${t.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(c,h,f)}
1458      ${t.mainStart()}
1459      ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
1460
1461      let outputIndices = ${f.offsetToIndices("global_idx")};
1462
1463      var idx = ${h.getByOffset("global_idx")};
1464      if (idx < 0) {
1465        idx = idx + uniforms.axisDimLimit;
1466      }
1467      var inputIndices = ${c.type.indices}(outputIndices);
1468      ${c.indicesSet("inputIndices","uniforms.axis","u32(idx)")};
1469      let value = ${c.getByIndices("inputIndices")};
1470
1471      ${f.setByOffset("global_idx","value")};
1472  }`}},np=t=>tc({axis:t.axis}),nc=(t,r)=>{nl(t.inputs),t.compute(nd(t.inputs,r))}}),ub=W(()=>{oG(),oX(),oY(),nh=t=>{if(!t)throw Error("Input is missing");if(t.length<2||t.length>3)throw Error("Invaid input number.");if(3===t.length&&t[2].dims.length>2)throw Error("Invalid input shape of C");if(t[0].dataType!==t[1].dataType||3===t.length&&t[0].dataType!==t[2].dataType)throw Error("Input types are mismatched")},nf=(t,r)=>{let i=t[0].dims.slice(),a=t[1].dims.slice(),[n,s,o]=ty.getShapeOfGemmResult(i,r.transA,a,r.transB,3===t.length?t[2].dims:void 0),u=[n,s];if(!u)throw Error("Can't use gemm on the given tensors");let l=Math.ceil(s/16),d=Math.ceil(n/16),p=(tm.size(u),[{type:12,data:l},{type:12,data:n},{type:12,data:s},{type:12,data:o},{type:1,data:r.alpha},{type:1,data:r.beta}]),c=["type","type"];return 3===t.length&&(p.push(...tS(t[2].dims)),c.push("rank")),p.push(...tS(u)),{name:"GemmShared",shaderCache:{hint:`${r.cacheKey}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:u,dataType:t[0].dataType}],dispatchGroup:{x:l*d},programUniforms:p}),getShaderSource:i=>{let a=tA("a",t[0].dataType,t[0].dims),n=tA("b",t[1].dataType,t[1].dims),s=null,o=[a,n];3===t.length&&(s=tA("c",t[2].dataType,t[2].dims.length),o.push(s));let l=tO("output",t[0].dataType,u.length);o.push(l);let d="",p="";r.transA&&r.transB?(p=`
1473      var col = tile_row_start + local_id.x;
1474      var row = k_start + local_id.y;
1475      if (col < uniforms.M && row < uniforms.K) {
1476        tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col];
1477      } else {
1478        tile_a[local_id.y][local_id.x] = ${a.type.value}(0);
1479      }
1480
1481      col = k_start + local_id.x;
1482      row = tile_col_start + local_id.y;
1483      if (col < uniforms.K && row < uniforms.N) {
1484        tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col];
1485      } else {
1486        tile_b[local_id.y][local_id.x] = ${n.type.value}(0);
1487      }
1488      `,d="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):r.transA&&!r.transB?(p=`
1489      var col = tile_row_start + local_id.x;
1490      var row = k_start + local_id.y;
1491      if (col < uniforms.M && row < uniforms.K) {
1492        tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col];
1493      } else {
1494        tile_a[local_id.y][local_id.x] = ${a.type.value}(0);
1495      }
1496
1497      col = tile_col_start + local_id.x;
1498      row = k_start + local_id.y;
1499      if (col < uniforms.N && row < uniforms.K) {
1500        tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col];
1501      } else {
1502        tile_b[local_id.y][local_id.x] = ${n.type.value}(0);
1503      }
1504      `,d="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!r.transA&&r.transB?(p=`
1505      var col = k_start + local_id.x;
1506      var row = tile_row_start + local_id.y;
1507      if (col < uniforms.K && row < uniforms.M) {
1508        tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col];
1509      } else {
1510        tile_a[local_id.y][local_id.x] = ${a.type.value}(0);
1511      }
1512
1513      col = k_start + local_id.x;
1514      row = tile_col_start + local_id.y;
1515      if (col < uniforms.K && row < uniforms.N) {
1516        tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col];
1517      } else {
1518        tile_b[local_id.y][local_id.x] = ${n.type.value}(0);
1519      }
1520      `,d="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):r.transA||r.transB||(p=`
1521      var col = k_start + local_id.x;
1522      var row = tile_row_start + local_id.y;
1523      if (col < uniforms.K && row < uniforms.M) {
1524        tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col];
1525      } else {
1526        tile_a[local_id.y][local_id.x] = ${a.type.value}(0);
1527      }
1528
1529      col = tile_col_start + local_id.x;
1530      row = k_start + local_id.y;
1531      if (col < uniforms.N && row < uniforms.K) {
1532        tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col];
1533      } else {
1534        tile_b[local_id.y][local_id.x] = ${n.type.value}(0);
1535      }
1536      `,d="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let c=1===r.alpha?"":"value *= uniforms.alpha;";return`
1537  ${i.registerUniforms([{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"}]).declareVariables(...o)}
1538  var<workgroup> tile_a: array<array<${a.type.storage}, 16>, 16>;
1539  var<workgroup> tile_b: array<array<${n.type.storage}, 16>, 16>;
1540  ${i.mainStart([16,16,1])}
1541    let tile_col_start = (workgroup_index % uniforms.num_tile_n) * 16;
1542    let tile_row_start = (workgroup_index / uniforms.num_tile_n) * 16;
1543    let num_tiles = (uniforms.K - 1) / 16 + 1;
1544    var k_start = 0u;
1545    var value = ${l.type.value}(0);
1546    for (var t: u32 = 0u; t < num_tiles; t++) {
1547      ${p}
1548      k_start = k_start + 16;
1549      workgroupBarrier();
1550
1551      for (var k: u32 = 0u; k < 16; k++) {
1552        ${d}
1553      }
1554      workgroupBarrier();
1555    }
1556
1557    ${c}
1558    let m = tile_row_start + local_id.y;
1559    let n = tile_col_start + local_id.x;
1560    ${null!=s?`let cOffset = ${s.broadcastedIndicesToOffset("vec2(m, n)",l)}; value += ${l.type.value}(uniforms.beta) * ${s.getByOffset("cOffset")};`:""}
1561    if (m < uniforms.M && n < uniforms.N) {
1562      output[m * uniforms.N + n] = value;
1563    }
1564  }`}}},nm=t=>({transA:t.transA,transB:t.transB,alpha:t.alpha,beta:t.beta,cacheKey:`${t.transA};${t.transB};${1===t.alpha}`}),ng=(t,r)=>{nh(t.inputs),t.compute(nf(t.inputs,r))}}),uv=W(()=>{oG(),oX(),oZ(),oY(),[ny,n_,n$,nb]=[0,1,2,3],nv=t=>{if(4!==t[0].dims.length)throw Error("only 4-D tensor is supported.");if(t[0].dims.length!==t[1].dims.length)throw Error("input dimensions must be equal to grid dimensions");if(t[0].dims.length-2!==t[1].dims[t[1].dims.length-1])throw Error(`last dimension of grid must be equal to ${t[0].dims.length-2}`);if(t[0].dims[0]!==t[1].dims[0])throw Error("grid batch size must match input batch size")},nw=`
1565  fn gs_get_cubic_coeffs(x: f32) -> vec4<f32> {
1566    let cubic_alpha = -0.75f;
1567    let x_abs = abs(x);
1568    var coeffs: vec4<f32>;
1569    coeffs[0] = (((cubic_alpha * (x_abs + 1) - 5 * cubic_alpha) * (x_abs + 1) + 8 * cubic_alpha) * (x_abs + 1) - 4 * cubic_alpha);
1570    coeffs[1] = (((cubic_alpha + 2) * x_abs - (cubic_alpha + 3)) * x_abs * x_abs + 1);
1571    coeffs[2] = (((cubic_alpha + 2) * (1 - x_abs) - (cubic_alpha + 3)) * (1 - x_abs) * (1 - x_abs) + 1);
1572    coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha);
1573    return coeffs;
1574  }
1575`,nx=t=>`
1576  fn gs_bicubic_interpolate(p: mat4x4<${t}>, x: f32, y: f32) -> ${t} {
1577    var v: vec4<f32>;
1578    var coeffs = gs_get_cubic_coeffs(x);
1579    for (var i = 0; i < 4; i++) {
1580      v[i] = coeffs[0] * p[i][0] + coeffs[1] * p[i][1] + coeffs[2] * p[i][2] + coeffs[3] * p[i][3];
1581    }
1582    coeffs = gs_get_cubic_coeffs(y);
1583    let pixel = ${t}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]);
1584    return pixel;
1585  }
1586`,nS=t=>`
1587  fn gs_denormalize(n: f32, length: i32) -> f32 {
1588    ${0===t.alignCorners?`
1589    // alignCorners: false => [-1, 1] to [-0.5, length - 0.5]
1590    return ((n + 1.0) * f32(length) - 1.0) / 2.0;
1591    `:`
1592    // alignCorners: true => [-1, 1] to [0, length - 1]
1593    return (n + 1.0) / 2.0 * (f32(length - 1));
1594    `}
1595  }
1596`,nk=t=>`
1597  ${"reflection"===t.paddingMode?`
1598      fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 {
1599        var dx = 0.0;
1600        var fx = f32(x);
1601        let range = x_max - x_min;
1602        if (fx < x_min) {
1603          dx = x_min - fx;
1604          let n = u32(dx / range);
1605          let r = dx - f32(n) * range;
1606          if (n % 2 == 0) {
1607            fx = x_min + r;
1608          } else {
1609            fx = x_max - r;
1610          }
1611        } else if (fx > x_max) {
1612          dx = fx - x_max;
1613          let n = u32(dx / range);
1614          let r = dx - f32(n) * range;
1615          if (n % 2 == 0) {
1616            fx = x_max - r;
1617          } else {
1618            fx = x_min + r;
1619          }
1620        }
1621        return u32(fx);
1622      }`:""}
1623`,nT=(t,r,i)=>`
1624  fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4<f32>) -> ${r} {
1625     var pixel = ${r}(0);
1626     var indices = vec4<u32>(0);
1627     indices[${ny}] = batch;
1628     indices[${n_}] = channel;`+(()=>{switch(i.paddingMode){case"zeros":return`
1629          if (r >= 0 && r < H && c >=0 && c < W) {
1630            indices[${n$}] = u32(r);
1631            indices[${nb}] = u32(c);
1632          }
1633        `;case"border":return`
1634          indices[${n$}] = u32(clamp(r, 0, H - 1));
1635          indices[${nb}] = u32(clamp(c, 0, W - 1));
1636        `;case"reflection":return`
1637          indices[${n$}] = gs_reflect(r, border[1], border[3]);
1638          indices[${nb}] = gs_reflect(c, border[0], border[2]);
1639        `;default:throw Error(`padding mode ${i.paddingMode} is not supported`)}})()+`
1640    return ${t.getByIndices("indices")};
1641  }
1642`,nC=(t,r,i)=>(()=>{switch(i.mode){case"nearest":return`
1643          let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${ny}], indices[${n_}], border);
1644        `;case"bilinear":return`
1645          let x1 = i32(floor(x));
1646          let y1 = i32(floor(y));
1647          let x2 = x1 + 1;
1648          let y2 = y1 + 1;
1649
1650          let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${ny}], indices[${n_}], border);
1651          let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${ny}], indices[${n_}], border);
1652          let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${ny}], indices[${n_}], border);
1653          let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${ny}], indices[${n_}], border);
1654
1655          let dx2 = ${r}(f32(x2) - x);
1656          let dx1 = ${r}(x - f32(x1));
1657          let dy2 = ${r}(f32(y2) - y);
1658          let dy1 = ${r}(y - f32(y1));
1659          let result = dy2 * (dx2 * p11 + dx1 * p12) + dy1 * (dx2 * p21 + dx1 * p22);
1660        `;case"bicubic":return`
1661          let x0 = i32(floor(x)) - 1;
1662          let y0 = i32(floor(y)) - 1;
1663          var p: mat4x4<${r}>;
1664          for (var h = 0; h < 4; h++) {
1665            for (var w = 0; w < 4; w++) {
1666              p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${ny}], indices[${n_}], border);
1667            }
1668          }
1669
1670          let dx = x - f32(x0 + 1);
1671          let dy = y - f32(y0 + 1);
1672          let result = gs_bicubic_interpolate(p, dx, dy);
1673        `;default:throw Error(`mode ${i.mode} is not supported`)}})()+`${t.setByOffset("global_idx","result")}`,nI=(t,r)=>{let i=tA("x",t[0].dataType,t[0].dims.length),a=[t[1].dims[0],t[1].dims[1],t[1].dims[2]],n=tA("grid",t[1].dataType,a.length,2),s=[t[0].dims[0],t[0].dims[1],t[1].dims[1],t[1].dims[2]];"NHWC"===r.format&&(s=[t[0].dims[0],t[1].dims[1],t[1].dims[2],t[0].dims[3]],[ny,n_,n$,nb]=[0,3,1,2]);let o=tO("output",t[0].dataType,s.length),u=i.type.value,l=[{type:12,data:tm.size(s)},...tS(t[0].dims,a,s)];return{name:"GridSample",shaderCache:{hint:`${r.cacheKey}`,inputDependencies:["type","type"]},getRunData:t=>{let r=tm.size(s);return{outputs:[{dims:s,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:l}},getShaderSource:t=>`
1674  ${t.registerUniform("output_size","u32").declareVariables(i,n,o)}
1675  ${nw}
1676  ${nx(u)}
1677  ${nS(r)}
1678  ${nk(r)}
1679  ${nT(i,u,r)}
1680
1681  ${t.mainStart()}
1682    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1683      let H_in = i32(uniforms.x_shape[${n$}]);
1684      let W_in = i32(uniforms.x_shape[${nb}]);
1685
1686      ${0===r.alignCorners?`
1687      let x_min = -0.5;
1688      let x_max = f32(W_in) - 0.5;
1689      let y_min = -0.5;
1690      let y_max = f32(H_in) - 0.5;
1691      `:`
1692      let x_min = 0.0;
1693      let x_max = f32(W_in) - 1.0;
1694      let y_min = 0.0;
1695      let y_max = f32(H_in) - 1.0;
1696      `};
1697      let border = vec4<f32>(x_min, y_min, x_max, y_max);
1698
1699      let indices = ${o.offsetToIndices("global_idx")};
1700      var grid_indices = vec3<u32>(indices[${ny}], indices[${n$}], indices[${nb}]);
1701      let nxy = ${n.getByIndices("grid_indices")};
1702      var x = gs_denormalize(f32(nxy[0]), W_in);
1703      var y = gs_denormalize(f32(nxy[1]), H_in);
1704
1705      ${nC(o,u,r)}
1706  }`}},nE=(t,r)=>{nv(t.inputs),t.compute(nI(t.inputs,r))},nz=t=>tc({alignCorners:t.align_corners,mode:t.mode,paddingMode:t.padding_mode,format:t.format})}),uw=W(()=>{oG(),oX(),oZ(),oK(),o3(),oY(),oJ(),nA=(t,r)=>t.length>r&&t[r].dims.length>0?t[r]:void 0,nO=(t,r)=>{let i,a=t[0],n=nA(t,1),s=nA(t,2),o=nA(t,3),u=nA(t,4),l=nA(t,5),d=nA(t,6),p=nA(t,7);if(3!==a.dims.length&&5!==a.dims.length)throw Error("Input query is expected to have 3 or 5 dimensions");let c=a.dims[0],h=a.dims[1],f=3===a.dims.length?a.dims[2]:r.numHeads*a.dims[4],m=h,g=0,y=0,_=Math.floor(f/r.numHeads);if(d&&p&&tm.size(d.dims)&&tm.size(p.dims)){if(4!==d.dims.length)throw Error('Input "past_key" is expected to have 4 dimensions');if(d.dims[0]!==c||d.dims[1]!==r.numHeads||d.dims[3]!==_)throw Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(p.dims[0]!==c||p.dims[1]!==r.numHeads||p.dims[3]!==_)throw Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[2]!==p.dims[2])throw Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(4!==p.dims.length)throw Error('Input "past_value" is expected to have 4 dimensions');g=d.dims[2],y=d.dims[2]}else if(d&&tm.size(d.dims)||p&&tm.size(p.dims))throw Error('Input "past_key" and "past_value" shall be both present or both absent');if(n&&tm.size(n.dims)>0){if(3!==a.dims.length)throw Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(a.dims[0]!==n.dims[0])throw Error('Input "query" and "key" shall have same dim 0 (batch size)');if(3===n.dims.length){if(n.dims[2]!==a.dims[2])throw Error('Input "query" and "key" shall have same dim 2 (hidden_size)');i=2,m=n.dims[1]}else if(5===n.dims.length){if(n.dims[2]!==r.numHeads||2!==n.dims[3]||n.dims[4]!==_)throw Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(s)throw Error('Expect "value" be none when "key" has packed kv format.');i=5,m=n.dims[1]}else{if(n.dims[1]!==r.numHeads||n.dims[3]!==_)throw Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');i=0,m=n.dims[2]}}else{if(5!==a.dims.length)throw Error('Input "query" is expected to have 5 dimensions when key is empty');if(a.dims[2]!==r.numHeads||3!==a.dims[3])throw Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');i=3}
1706if(o&&tm.size(o.dims)>0){if(1!==o.dims.length)throw Error('Input "bias" is expected to have 1 dimension');if(n&&5===n.dims.length&&2===n.dims[3])throw Error("bias is not allowed for packed kv.")}let $=g+m,b=0;if(u&&tm.size(u.dims)>0){b=8;let t=u.dims;throw 1===t.length?t[0]===c?b=1:t[0]===3*c+2&&(b=3):2===t.length&&t[0]===c&&t[1]===$&&(b=5),8===b?Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):Error("Mask not supported")}let v=!1,w=f;if(s&&tm.size(s.dims)>0){if(3!==s.dims.length&&4!==s.dims.length)throw Error('Input "value" is expected to have 3 or 4 dimensions');if(a.dims[0]!==s.dims[0])throw Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(3===s.dims.length){if(m!==s.dims[1])throw Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');w=s.dims[2]}else{if(m!==s.dims[2])throw Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');w=s.dims[1]*s.dims[3],v=!0}}if(u&&tm.size(u.dims)>0)throw Error("Key padding mask is not supported");if(l&&tm.size(l.dims)>0){if(4!==l.dims.length)throw Error('Input "attention_bias" is expected to have 4 dimensions');if(l.dims[0]!==c||l.dims[1]!==r.numHeads||l.dims[2]!==h||l.dims[3]!==$)throw Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:c,sequenceLength:h,pastSequenceLength:g,kvSequenceLength:m,totalSequenceLength:$,maxSequenceLength:y,inputHiddenSize:0,hiddenSize:f,vHiddenSize:w,headSize:_,vHeadSize:Math.floor(w/r.numHeads),numHeads:r.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:r.maskFilterValue,maskType:b,scale:r.scale,broadcastResPosBias:!1,passPastInKv:v,qkvFormat:i}},nR=t=>tc({...t}),nB=tc({perm:[0,2,1,3]}),nN=(t,r,i,a,n,s,o)=>{let u=[a,n,s],l=tm.size(u),d=[{type:12,data:l},{type:12,data:o},{type:12,data:s}];return t.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:u,dataType:r.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:d}),getShaderSource:t=>{let a=tO("qkv_with_bias",r.dataType,u),n=tA("qkv",r.dataType,u),s=tA("bias",i.dataType,u);return`
1707  ${t.registerUniforms([{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}]).declareVariables(n,s,a)}
1708  ${t.mainStart()}
1709    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1710    let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset;
1711
1712    qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx];
1713  }`}},{inputs:[r,i],outputs:[-1]})[0]},nM=(t,r,i,a,n,s,o,u)=>{let l=s;if(!(o&&tm.size(o.dims)>0))return 3===s.dims.length&&(l=s.reshape([r,a,i,n])),1===i||1===a?l:t.compute(tV(l,nB.perm),{inputs:[l],outputs:[-1]})[0];if(1===a)throw Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return l=(l=nN(t,s,o,r,a,i*n,u)).reshape([r,a,i,n]),1===i||1===a?l:t.compute(tV(l,nB.perm),{inputs:[l],outputs:[-1]})[0]},nD=(t,r)=>{let i=nO(t.inputs,r),a=t.inputs[0],n=nA(t.inputs,1),s=nA(t.inputs,2),o=nA(t.inputs,3),u=nA(t.inputs,4),l=nA(t.inputs,5),d=nA(t.inputs,6),p=nA(t.inputs,7);if(5===a.dims.length)throw Error("Packed QKV is not implemented");if(n?.dims.length===5)throw Error("Packed KV is not implemented");let c=n&&s&&4===n.dims.length&&4===s.dims.length,h=nM(t,i.batchSize,i.numHeads,i.sequenceLength,i.headSize,a,o,0);if(c)return rP(t,h,n,s,u,void 0,d,p,l,i);if(!n||!s)throw Error("key and value must be provided");let f=nM(t,i.batchSize,i.numHeads,i.kvSequenceLength,i.headSize,n,o,i.hiddenSize),m=nM(t,i.batchSize,i.numHeads,i.kvSequenceLength,i.vHeadSize,s,o,2*i.hiddenSize);rP(t,h,f,m,u,void 0,d,p,l,i)}}),ux=W(()=>{oG(),oX(),oZ(),oY(),nP=t=>{if(!t||t.length<1)throw Error("too few inputs")},nU=(t,r)=>{let i=[],a=r.numOutputs;return t[1].dims[0]>0&&(t[1].getBigInt64Array().forEach(t=>i.push(Number(t))),a=i.length),tc({numOutputs:a,axis:r.axis,splitSizes:i})},nW=t=>`
1714fn calculateOutputIndex(index: u32) -> u32 {
1715    for (var i: u32 = 0u; i < ${t}u; i += 1u ) {
1716    if (index < ${tE("uniforms.size_in_split_axis","i",t)}) {
1717        return i;
1718    }
1719    }
1720    return ${t}u;
1721}`,nq=t=>{let r=t.length,i=[];for(let a=0;a<r;++a){let n=t[a].setByIndices("indices","input[global_idx]");1===r?i.push(n):0===a?i.push(`if (output_number == ${a}u) { ${n} }`):a===r-1?i.push(`else { ${n} }`):i.push(`else if (output_number == ${a}) { ${n} }`)}return`
1722      fn writeBufferData(output_number: u32, indices: ${t[0].type.indices}, global_idx: u32) {
1723        ${i.join(`
1724`)}
1725      }`},nL=(t,r)=>{let i=t[0].dims,a=tm.size(i),n=t[0].dataType,s=tm.normalizeAxis(r.axis,i.length),o=Array(r.numOutputs),u=tA("input",n,i.length),l=Array(r.numOutputs),d=[],p=[],c=0,h=[{type:12,data:a}];for(let a=0;a<r.numOutputs;
1725a++){c+=r.splitSizes[a],l[a]=c;let u=i.slice();u[s]=r.splitSizes[a],p.push(u),o[a]=tO(`output${a}`,n,u.length),d.push({dims:p[a],dataType:t[0].dataType})}return h.push({type:12,data:l},...tS(i,...p)),{name:"Split",shaderCache:{hint:r.cacheKey,inputDependencies:["rank"]},getShaderSource:t=>`
1726  ${t.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",l.length).declareVariables(u,...o)}
1727  ${nW(l.length)}
1728  ${nq(o)}
1729
1730  ${t.mainStart()}
1731    ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")}
1732
1733    var indices = ${u.offsetToIndices("global_idx")};
1734    var index = ${u.indicesGet("indices",s)};
1735    let output_number = calculateOutputIndex(index);
1736    if (output_number != 0) {
1737      index -= ${tE("uniforms.size_in_split_axis","output_number - 1u",l.length)};
1738      ${u.indicesSet("indices",s,"index")};
1739    }
1740    writeBufferData(output_number, indices, global_idx);
1741  }`,getRunData:()=>({outputs:d,dispatchGroup:{x:Math.ceil(a/64)},programUniforms:h})}},nV=(t,r)=>{nP(t.inputs);let i=1===t.inputs.length?r:nU(t.inputs,r);t.compute(nL(t.inputs,i),{inputs:[0]})},nG=t=>{let r=t.axis,i=t.splitSizes,a=t.numOutputs<0?i.length:t.numOutputs;if(a!==i.length)throw Error("numOutputs and splitSizes lengh must be equal");return tc({axis:r,numOutputs:a,splitSizes:i})}}),uS=W(()=>{oZ(),o3(),uw(),ux(),oJ(),nH=(t,r)=>{if(r.doRotary)throw Error("GroupQuerryAttention do_rotary attribute is not supported");if(r.doRotary&&t.length<=7)throw Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let i=t[0],a=t[1],n=t[2],s=t[3],o=t[4];if(-1!==r.localWindowSize)throw Error("Local attention is not supported");if(0!==r.softcap)throw Error("Softcap is not supported");if(0!==r.rotaryInterleaved)throw Error("Rotary interleaved is not supported");if(r.smoothSoftmax)throw Error("Smooth softmax is not supported");if(3!==i.dims.length&&5!==i.dims.length)throw Error("Input query is expected to have 3 or 5 dimensions");let u=i.dims[0],l=i.dims[1],d=3===i.dims.length?i.dims[2]:r.numHeads*i.dims[4],p=l,c=0,h=!a||0===a.dims.length,f=Math.floor(h?d/(r.numHeads+2*r.kvNumHeads):d/r.numHeads);h&&(d=f*r.numHeads);let m=s&&0!==s.dims.length,g=o&&0!==o.dims.length;if(m&&4===s.dims.length&&s.dims[0]===u&&s.dims[1]!==r.kvNumHeads&&s.dims[2]===r.kvNumHeads&&s.dims[3]===f)throw Error("BSNH pastKey/pastValue is not supported");if(m&&g){if(4!==s.dims.length)throw Error('Input "past_key" is expected to have 4 dimensions');if(4!==o.dims.length)throw Error('Input "past_value" is expected to have 4 dimensions');c=s.dims[2]}else if(m||g)throw Error('Input "past_key" and "past_value" shall be both present or both absent');let y=1;if(a&&a.dims.length>0){if(3!==i.dims.length)throw Error('Input "query" is expected to have 3 dimensions when key is given');if(a.dims.length<3||a.dims.length>5)throw Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(i.dims[0]!==a.dims[0])throw Error('Input "query" and "key" shall have same dim 0 (batch size)');if(3===a.dims.length){if(i.dims[2]%a.dims[2]!=0)throw Error('Dimension 2 of "query" should be a multiple of "key"');p=a.dims[1]}else if(5===a.dims.length){if(a.dims[2]!==r.numHeads||2!==a.dims[3]||a.dims[4]!==f)throw Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(n)throw Error('Expect "value" be none when "key" has packed kv format.');p=a.dims[1]}else{if(a.dims[1]!==r.numHeads||a.dims[3]!==f)throw Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');p=a.dims[2]}}else{if(3!==i.dims.length&&5!==i.dims.length)throw Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(5===i.dims.length&&(i.dims[2]!==r.numHeads||3!==i.dims[3]))throw Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');y=3}let _=!1,$=r.kvNumHeads?f*r.kvNumHeads:d;if(n&&n.dims.length>0){if(3!==n.dims.length&&4!==n.dims.length)throw Error('Input "value" is expected to have 3 or 4 dimensions');if(i.dims[0]!==n.dims[0])throw Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(3===n.dims.length){if(p!==n.dims[1])throw Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');$=n.dims[2]}else{if(p!==n.dims[2])throw Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');$=n.dims[1]*n.dims[3],_=!0}}let b=t.length>4?t[5]:void 0;if(b&&1!==b.dims.length&&b.dims[0]!==u)throw Error('Input "seqlens" is expected to have 1 dimension and the same dim 0 as batch_size');return{batchSize:u,sequenceLength:l,pastSequenceLength:c,kvSequenceLength:p,totalSequenceLength:-1,maxSequenceLength:-1,inputHiddenSize:0,hiddenSize:d,vHiddenSize:$,headSize:f,vHeadSize:Math.floor($/r.kvNumHeads),numHeads:r.numHeads,kvNumHeads:r.kvNumHeads,nReps:r.numHeads/r.kvNumHeads,pastPresentShareBuffer:!1,maskType:0,scale:r.scale,broadcastResPosBias:!1,passPastInKv:_,qkvFormat:y}},nF=tc({perm:[0,2,1,3]}),nj=(t,r,i)=>{let a=r,n=i.kvNumHeads;return 3===r.dims.length&&0!==i.kvSequenceLength&&(a=r.reshape([i.batchSize,i.kvSequenceLength,n,i.headSize]),a=t.compute(tV(a,nF.perm),{inputs:[a],outputs:[-1]})[0]),a},nK=(t,r)=>{let i=nH(t.inputs,r);if(5===t.inputs[0].dims.length)throw Error("Packed QKV is not implemented");if(t.inputs[1]?.dims.length===5)throw Error("Packed KV is not implemented");
1741let a=t.inputs[0],n=t.inputs[1]&&t.inputs[1].dims.length>0?t.inputs[1]:void 0,s=t.inputs[2]&&t.inputs[2].dims.length>0?t.inputs[2]:void 0,o=t.inputs[3]&&0!==t.inputs[3].dims.length?t.inputs[3]:void 0,u=t.inputs[4]&&0!==t.inputs[4].dims.length?t.inputs[4]:void 0,l=t.inputs.length>4?t.inputs[5]:void 0,d=t.inputs.length>5?t.inputs[6]:void 0,p=i.kvNumHeads?i.kvNumHeads:i.numHeads,c=tc({axis:2,numOutputs:3,splitSizes:[i.numHeads*i.headSize,p*i.headSize,p*i.headSize]}),[h,f,m]=n||s?[a,n,s]:t.compute(nL([a],c),{inputs:[a],outputs:[-1,-1,-1]}),g=nM(t,i.batchSize,i.numHeads,i.sequenceLength,i.headSize,h,void 0,0);rP(t,g,nj(t,f,i),nj(t,m,i),void 0,void 0,o,u,void 0,i,l,d)}}),uk=W(()=>{oG(),oX(),oJ(),oY(),nQ=(t,r,i,a,n,s,o,u)=>{let l=tk(s),d=1===l?"f32":`vec${l}f`,p=1===l?"vec2f":`mat2x${l}f`,c=n*o,h=64;1===c&&(h=256);let f=[n,o,s/l],m=[n,o,2],g=[];return g.push(...tS(f,m)),t.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${l};${u};${h}`,inputDependencies:["rank","type","type"]},getRunData:()=>({outputs:[{dims:m,dataType:1}],dispatchGroup:{x:c},programUniforms:g}),getShaderSource:t=>{let n=tA("x",r.dataType,3,l),s=[n,tA("scale",i.dataType,i.dims),tA("bias",a.dataType,a.dims),tO("output",1,3,2)];return`
1742  var<workgroup> workgroup_shared : array<${p}, ${h}>;
1743  const workgroup_size = ${h}u;
1744  ${t.declareVariables(...s)}
1745  ${t.mainStart(h)}
1746    let batch = workgroup_index / uniforms.x_shape[1];
1747    let channel = workgroup_index % uniforms.x_shape[1];
1748    let hight = uniforms.x_shape[2];
1749    // initialize workgroup memory
1750    var sum = ${d}(0);
1751    var squared_sum = ${d}(0);
1752    for (var h = local_idx; h < hight; h += workgroup_size) {
1753      let value = ${d}(${n.get("batch","channel","h")});
1754      sum += value;
1755      squared_sum += value * value;
1756    }
1757    workgroup_shared[local_idx] = ${p}(sum, squared_sum);
1758    workgroupBarrier();
1759
1760    for (var currSize = workgroup_size >> 1;  currSize > 0; currSize = currSize >> 1) {
1761      if (local_idx < currSize) {
1762        workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize];
1763      }
1764      workgroupBarrier();
1765    }
1766    if (local_idx == 0) {
1767      let sum_final = ${tI("workgroup_shared[0][0]",l)} / f32(hight * ${l});
1768      let squared_sum_final = ${tI("workgroup_shared[0][1]",l)} / f32(hight * ${l});
1769
1770      let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${u}));
1771      let channel_scale = inv_std_dev * f32(scale[channel]);
1772      let channel_shift = f32(bias[channel]) - sum_final * channel_scale;
1773      output[workgroup_index] = vec2f(channel_scale, channel_shift);
1774    }
1775  }`}},{inputs:[r,i,a],outputs:[-1]})[0]},nZ=(t,r,i)=>{let a=r[0].dims,n=a[0],s=a[1],o=tm.sizeFromDimension(a,2),u=tk(o),l=tm.size(a)/u,d=nQ(t,r[0],r[1],r[2],n,o,s,i.epsilon),p=[n,s,o/u],c=[n,s];t.compute({name:"InstanceNormalization",shaderCache:{hint:`${u}`,inputDependencies:["type","none"]},getRunData:()=>({outputs:[{dims:a,dataType:r[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:[{type:12,data:l},...tS(p,c,p)]}),getShaderSource:t=>{let i=tA("x",r[0].dataType,p.length,u),a=tA("scale_shift",1,c.length,2),n=tO("output",r[0].dataType,p.length,u),s=[i,a,n];return`
1776  ${t.registerUniform("output_size","u32").declareVariables(...s)}
1777  ${t.mainStart()}
1778  ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
1779      let outputIndices = ${n.offsetToIndices("global_idx")};
1780      let batch = outputIndices[0];
1781      let channel = outputIndices[1];
1782      let scale_shift = ${a.getByIndices("vec2<u32>(batch, channel)")};
1783      let value = ${i.getByOffset("global_idx")} * ${n.type.value}(scale_shift.x) + ${n.type.value}(scale_shift.y);
1784      ${n.setByOffset("global_idx","value")};
1785  }`}},{inputs:[r[0],d]})},nX=(t,r,i)=>{let a=r[0].dims,n=a[0],s=a[a.length-1],o=tm.sizeFromDimension(a,1)/s,u=tk(s),l=tm.size(a)/u,d=[{type:12,data:o},{type:12,data:Math.floor(s/u)}],p=!1,c=[0,a.length-1];for(let t=0;t<a.length-2;t++)p=p||1!==a[t+1],c.push(t+1);let h=(p=p&&1!==a[a.length-1])?t.compute(tV(t.inputs[0],c),{inputs:[t.inputs[0]],outputs:[-1]})[0]:t.inputs[0].reshape(Array.from({length:a.length},(t,r)=>a[c[r]])),f=nQ(t,h,r[1],r[2],n,o,s,i.epsilon);t.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${u}`,inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:a,dataType:r[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:d}),getShaderSource:t=>{let i=tw(r[0].dataType),n=1===u?"vec2f":`mat${u}x2f`,s=t=>{let r=0===t?"x":"y",a=1===u?"f32":`vec${u}f`;switch(u){case 1:return`${i}(${a}(scale.${r}))`;case 2:return`vec2<${i}>(${a}(scale[0].${r}, scale[1].${r}))`;case 4:return`vec4<${i}>(${a}(scale[0].${r}, scale[1].${r}, scale[2].${r}, scale[3].${r}))`;default:throw Error(`Not supported compoents ${u}`)}},o=tA("input",r[0].dataType,r[0].dims,u),l=tO("output",r[0].dataType,a,u);return`
1786  @group(0) @binding(0) var<storage, read> input : array<${o.type.storage}>;
1787  @group(0) @binding(1) var<storage, read> scale_input : array<${n}>;
1788  @group(0) @binding(2) var<storage, read_write> output : array<${l.type.storage}>;
1789  struct Uniforms {H: u32, C : u32};
1790  @group(0) @binding(3) var<uniform> uniforms: Uniforms;
1791
1792  ${t.mainStart()}
1793    let current_image_number = global_idx / (uniforms.C * uniforms.H);
1794    let current_channel_number = global_idx % uniforms.C;
1795
1796    let scale_offset = current_image_number * uniforms.C + current_channel_number;
1797    let scale = scale_input[scale_offset];
1798    output[global_idx] = fma(input[global_idx], ${s(0)}, ${s(1)});
1799  }`}},{inputs:[r[0],f]})},nY=(t,r)=>{"NHWC"===r.format?nX(t,t.inputs,r):nZ(t,t.inputs,r)}}),uT=W(()=>{oG(),oX(),oY(),nJ=t=>{if(!t||t.length<2)throw Error("layerNorm requires at least 2 inputs.")},n0=(t,r,i)=>{let a=r.simplified,n=t[0].dims,s=t[1],o=!a&&t[2],u=tm.normalizeAxis(r.axis,n.length),l=tm.sizeToDimension(n,u),d=tm.sizeFromDimension(n,u),p=tm.size(s.dims),c=o?tm.size(o.dims):0;if(p!==d||o&&c!==d)throw Error(`Size of X.shape()[axis:] == ${d}.
1800       Size of scale and bias (if provided) must match this.
1801       Got scale size of ${p} and bias size of ${c}`);let h=[];for(let t=0;t<n.length;++t)t<u?h.push(n[t]):h.push(1);let f=tk(d),m=["type","type"],g=[{type:12,data:l},{type:1,data:d},{type:12,data:Math.floor(d/f)},{type:1,data:r.epsilon}];o&&m.push("type");let y=i>1,_=i>2,$=[{dims:n,dataType:t[0].dataType}];return y&&$.push({dims:h,dataType:1}),_&&$.push({dims:h,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${f};${i};${a}`,inputDependencies:m},getRunData:()=>({outputs:$,dispatchGroup:{x:Math.ceil(l/64)},programUniforms:g}),getShaderSource:r=>{let i=tw(t[0].dataType),u=[tA("x",t[0].dataType,t[0].dims,f),tA("scale",s.dataType,s.dims,f)];return o&&u.push(tA("bias",o.dataType,o.dims,f)),u.push(tO("output",t[0].dataType,n,f)),y&&u.push(tO("mean_data_output",1,h)),_&&u.push(tO("inv_std_output",1,h)),`
1802  ${r.registerUniforms([{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}]).declareVariables(...u)}
1803  ${r.mainStart()}
1804    ${r.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")}
1805    let offset = global_idx * uniforms.norm_size_vectorized;
1806    var mean_vector = ${tT("f32",f)};
1807    var mean_square_vector = ${tT("f32",f)};
1808
1809    for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) {
1810      let value = ${tC(i,f,"x[h + offset]")};
1811      mean_vector += value;
1812      mean_square_vector += value * value;
1813    }
1814    let mean = ${tI("mean_vector",f)} / uniforms.norm_size;
1815    let inv_std_dev = inverseSqrt(${tI("mean_square_vector",f)} / uniforms.norm_size ${a?"":"- mean * mean"} + uniforms.epsilon);
1816
1817    for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) {
1818      let f32input = ${tC(i,f,"x[j + offset]")};
1819      let f32scale = ${tC(i,f,"scale[j]")};
1820      output[j + offset] = ${u[0].type.value}((f32input ${a?"":"- mean"}) * inv_std_dev * f32scale
1821        ${o?`+ ${tC(i,f,"bias[j]")}`:""}
1822      );
1823    }
1824
1825    ${y?"mean_data_output[global_idx] = mean":""};
1826    ${_?"inv_std_output[global_idx] = inv_std_dev":""};
1827  }`}}},n1=(t,r)=>{nJ(t.inputs),t.compute(n0(t.inputs,r,t.outputCount))}}),uC=W(()=>{oX(),ui(),ua(),n2=t=>{if(!t||2!==t.length)throw Error("MatMul requires 2 inputs.");if(t[0].dims[t[0].dims.length-1]!==t[1].dims[t[1].dims.length-2])throw Error("shared dimension does not match.")},n3=t=>{n2(t.inputs);let r=tf.calcShape(t.inputs[0].dims,t.inputs[1].dims,!0);if(!r)throw Error("Can't use matmul on the given tensors");let i=r[r.length-1],a=t.inputs[0].dims[t.inputs[0].dims.length-1];if(i<8&&a<8)t.compute(i5(t.inputs,{activation:""},r));else{let n=r[r.length-2],s=tm.size(t.inputs[0].dims.slice(0,-2)),o=tm.size(t.inputs[1].dims.slice(0,-2));if(1!==s&&1===n&&1===o){let n=t.inputs[0].reshape([1,s,a]),o=t.inputs[1].reshape([1,a,i]),u=[1,s,i],l=[n,o];t.compute(an(l,{activation:""},r,u),{inputs:l})}else t.compute(an(t.inputs,{activation:""},r))}}}),uI=W(()=>{oG(),oX(),oZ(),oY(),n4=(t,r)=>{if(t.length<3||t.length>4)throw Error("MatMulNBits requires 3 or 4 inputs");let i=t[0],a=i.dims.length;if(i.dims[a-1]!==r.k)throw Error("The last dim of input shape does not match the k value");let n=Math.floor((r.k+r.blockSize-1)/r.blockSize),s=r.blockSize/8*r.bits,o=t[1];if(!tm.areEqual(o.dims,[r.n,n,s]))throw Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let u=t[2].dims;if(tm.size(u)!==r.n*n)throw Error("scales input size error.");if(4===t.length){let i=t[3].dims,a=r.bits>4?r.n*n:r.n*Math.floor((n+1)/2);if(tm.size(i)!==a)throw Error("zeroPoints input size error.")}},n6=(t,r)=>{let i=t[0].dims,a=i.length,n=i[a-2],s=r.k,o=r.n,u=i.slice(0,a-2),l=tm.size(u),d=t[1].dims[2]/4,p=t[0].dataType,c=tk(r.k),h=tk(d),f=tk(o),m=u.concat([n,o]),g=n>1&&o/f%2==0?2:1,y=tm.size(m)/f/g,_=[],$=[l,n,s/c],b=tm.convertShape(t[1].dims).slice();b.splice(-1,1,d/h),_.push(...tS($)),_.push(...tS(b)),_.push(...tS(t[2].dims)),4===t.length&&_.push(...tS(tm.convertShape(t[3].dims)));let v=[l,n,o/f];return _.push(...tS(v)),{name:"MatMulNBits",shaderCache:{hint:`${r.blockSize};${r.bits};${c};${h};${f};${g};64`,inputDependencies:Array(t.length).fill("rank")},getRunData:()=>({outputs:[{dims:m,dataType:p}],dispatchGroup:{x:y},programUniforms:_}),getShaderSource:i=>{let a=$.length,n=tA("a",t[0].dataType,a,c),s=tA("b",12,b.length,h),o=tA("scales",t[2].dataType,t[2].dims.length),u=[n,s,o],l=4===t.length?tA("zero_points",12,t[3].dims.length):void 0;l&&u.push(l);let p=v.length,m=tO("output",t[0].dataType,p,f),y=tw(t[0].dataType),_=(()=>{switch(c){case 1:return`array<${y}, 8>`;case 2:return`mat4x2<${y}>`;case 4:return`mat2x4<${y}>`;default:throw Error(`${c}-component is not supported.`)}})();return`
1828        var<workgroup> workgroup_shared: array<${m.type.value}, ${64*g}>;
1829        ${i.declareVariables(...u,m)}
1830        ${i.mainStart([64,1,1])}
1831          let output_indices = ${m.offsetToIndices(`(global_idx / 64) * ${g}`)};
1832          let col = output_indices[2];
1833          let row = output_indices[1];
1834          let batch = output_indices[0];
1835          let nBlocksPerCol = uniforms.b_shape[1];
1836
1837          for (var block = local_id.x; block < nBlocksPerCol; block += 64) {
1838            //process one block
1839            var word_offset: u32 = block * ${r.blockSize/c};
1840            ${(()=>{let t=`
1841            var col_index = col * ${f};
1842            ${l?`
1843            let zero_point_bytes_per_col = (nBlocksPerCol + 1) / 2;
1844            var zero_point_byte_count: u32;
1845            var zero_point_word_index: u32;
1846            var zero_point_byte_offset: u32;
1847            let zero_point_nibble_offset: u32 = block & 0x1u;
1848            var zero_point_bits_offset: u32;
1849            var zero_point_word: u32;`:`
1850            // The default zero point is 8 for unsigned 4-bit quantization.
1851            let zero_point = ${y}(8);`}
1852            `;for(let r=0;r<f*g;r++)t+=`
1853            let scale${r} = ${o.getByOffset("col_index * nBlocksPerCol + block")};
1854            ${l?`
1855            zero_point_byte_count = col_index * zero_point_bytes_per_col + (block >> 0x1u);
1856            zero_point_word_index = zero_point_byte_count >> 0x2u;
1857            zero_point_byte_offset = zero_point_byte_count & 0x3u;
1858            zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2);
1859            zero_point_word = ${l.getByOffset("zero_point_word_index")} >> zero_point_bits_offset;
1860            let zero_point${r} = ${y}((zero_point_word) & 0xFu);`:""}
1861            col_index += 1;`;return t})()}
1862            for (var word: u32 = 0; word < ${d}; word += ${h}) {
1863              ${(()=>{let t=`col_index = col * ${f};`;for(let r=0;r<f*g;r++)t+=`
1864            let b${r}_data = ${s.getByIndices(`${s.type.indices}(col_index, block, word)`)};
1865            col_index += 1;`;return t+`
1866            var b_value: u32;
1867            let b_mask: u32 = 0x0F0F0F0Fu;
1868            var b_value_lower: vec4<u32>;
1869            var b_value_upper: vec4<u32>;
1870            var b_quantized_values: ${_};
1871            var b_dequantized_values: ${_};`})()}
1872              for (var i: u32 = 0; i < ${h}; i++) {
1873                ${(()=>{let t=`
1874          // reuse a data
1875            var input_offset = ${n.indicesToOffset(`${n.type.indices}(batch, row, word_offset)`)};
1876            var a_data: ${_};
1877            for (var j: u32 = 0; j < ${8/c}; j++) {
1878              a_data[j] = ${n.getByOffset("input_offset")};
1879              input_offset++;
1880            }
1881          `;for(let r=0;r<f*g;r++)t+=`
1882            b_value = ${1===h?`b${r}_data`:`b${r}_data[i]`};
1883            b_value_lower = unpack4xU8(b_value & b_mask);
1884            b_value_upper = unpack4xU8((b_value >> 4) & b_mask);
1885            b_quantized_values = ${_}(${Array.from({length:4},(t,r)=>`${y}(b_value_lower[${r}]), ${y}(b_value_upper[${r}])`).join(", ")});
1886            b_dequantized_values = ${1===c?`${_}(${Array.from({length:8},(t,i)=>`(b_quantized_values[${i}] - ${l?`zero_point${r}`:"zero_point"}) * scale${r}`).join(", ")});`:`(b_quantized_values - ${_}(${Array(8).fill(`${l?`zero_point${r}`:"zero_point"}`).join(",")})) * scale${r};`};
1887            workgroup_shared[local_id.x * ${g} + ${Math.floor(r/f)}]${f>1?`[${r%f}]`:""} += ${Array.from({length:8/c},(t,r)=>`${1===c?`a_data[${r}] * b_dequantized_values[${r}]`:`dot(a_data[${r}], b_dequantized_values[${r}])`}`).join(" + ")};
1888          `;return t})()}
1889                word_offset += ${8/c};
1890              }
1891            }
1892          }
1893          workgroupBarrier();
1894
1895          if (local_id.x < ${g}) {
1896            var output_value: ${m.type.value} = ${m.type.value}(0);
1897            var workgroup_shared_offset: u32 = local_id.x;
1898            for (var b: u32 = 0u; b < 64u; b++) {
1899              output_value += workgroup_shared[workgroup_shared_offset];
1900              workgroup_shared_offset += ${g};
1901            }
1902            ${m.setByIndices(`${m.type.indices}(batch, row, col + local_id.x)`,"output_value")};
1903          }
1904        }`}}},n8=(t,r)=>{let i=t[0].dims,a=i.length,n=i[a-2],s=r.k,o=r.n,u=i.slice(0,a-2),l=tm.size(u),d=t[1].dims[2]/4,p=t[0].dataType,c=tk(r.k),h=tk(d),f=u.concat([n,o]),m=o%8==0?8:o%4==0?4:1,g=128/m,y=g*h*8,_=y/c,$=y/r.blockSize,b
vendor: 4,566 bytes, lines 1904-1969
1904=tm.size(f)/m,v=[],w=[l,n,s/c],x=tm.convertShape(t[1].dims).slice();x.splice(-1,1,d/h),v.push(...tS(w)),v.push(...tS(x)),v.push(...tS(t[2].dims)),4===t.length&&v.push(...tS(tm.convertShape(t[3].dims)));let S=[l,n,o];return v.push(...tS(S)),{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${r.blockSize};${c};${h};${g};${m}`,inputDependencies:Array(t.length).fill("rank")},getRunData:()=>({outputs:[{dims:f,dataType:p}],dispatchGroup:{x:b},programUniforms:v}),getShaderSource:i=>{let a=w.length,n=tA("a",t[0].dataType,a,c),s=tA("b",12,x.length,h),o=tA("scales",t[2].dataType,t[2].dims.length),u=[n,s,o],l=4===t.length?tA("zero_points",12,t[3].dims.length):void 0;l&&u.push(l);let d=S.length,p=tO("output",t[0].dataType,d),f=tw(t[0].dataType);return`
1905        var<workgroup> sub_a: array<${n.type.value}, ${_}>;
1906        var<workgroup> inter_results: array<array<${p.type.value}, ${g}>, ${m}>;
1907        ${i.declareVariables(...u,p)}
1908        ${i.mainStart([g,m,1])}
1909          let output_indices = ${p.offsetToIndices(`workgroup_index * ${m}`)};
1910          let col = output_indices[2];
1911          let row = output_indices[1];
1912          let batch = output_indices[0];
1913          let n_blocks_per_col = uniforms.b_shape[1];
1914          let num_tiles =  (n_blocks_per_col - 1) / ${$} + 1;
1915
1916          // Loop over shared dimension.
1917          for (var tile: u32 = 0; tile < num_tiles; tile += 1) {
1918            let a_col_start = tile * ${_};
1919            // load one tile A data into shared memory.
1920            for (var a_offset = local_idx; a_offset < ${_}; a_offset += 128)
1921            {
1922              let a_col = a_col_start + a_offset;
1923              if (a_col < uniforms.a_shape[2])
1924              {
1925                sub_a[a_offset] = ${n.getByIndices(`${n.type.indices}(batch, row, a_col)`)};
1926              } else {
1927                sub_a[a_offset] = ${n.type.value}(0);
1928              }
1929            }
1930            workgroupBarrier();
1931
1932            // each thread process one block
1933            let b_row = col + local_id.y;
1934            let block = tile * ${$} + local_id.x;
1935            ${l?`
1936            let zero_point_bytes_per_col = (n_blocks_per_col + 1) / 2;
1937            let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block >> 0x1u);
1938            let zero_point_word_index = zero_point_byte_count >> 0x2u;
1939            let zero_point_byte_offset = zero_point_byte_count & 0x3u;
1940            let zero_point_nibble_offset: u32 = block & 0x1u;
1941            let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2);
1942            let zero_point_word = ${l.getByOffset("zero_point_word_index")} >> zero_point_bits_offset;
1943            let zero_point = ${f}((zero_point_word) & 0xFu);`:`
1944            // The default zero point is 8 for unsigned 4-bit quantization.
1945            let zero_point = ${f}(8);`}
1946            let scale = ${o.getByOffset("b_row * n_blocks_per_col + block")};
1947            let b_data = ${s.getByIndices(`${s.type.indices}(b_row, block, 0)`)};
1948            var word_offset = local_id.x * ${r.blockSize/c};
1949            for (var i: u32 = 0; i < ${h}; i++) {
1950              ${(()=>{switch(c){case 1:return`
1951          let a_data0 = vec4<${f}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]);
1952          let a_data1 = vec4<${f}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return`
1953          let a_data0 = vec4<${f}>(sub_a[word_offset], sub_a[word_offset + 1]);
1954          let a_data1 = vec4<${f}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return`
1955          let a_data0 = sub_a[word_offset];
1956          let a_data1 = sub_a[word_offset + 1];`;default:throw Error(`${c}-component is not supported.`)}})()}
1957              let b_value = ${1===h?"b_data":"b_data[i]"};
1958              let b_value_lower = unpack4xU8(b_value & 0x0F0F0F0Fu);
1959              let b_value_upper = unpack4xU8((b_value >> 4) & 0x0F0F0F0Fu);
1960              let b_quantized_values = mat2x4<${f}>(${Array.from({length:4},(t,r)=>`${f}(b_value_lower[${r}]), ${f}(b_value_upper[${r}])`).join(", ")});
1961              let b_dequantized_values = (b_quantized_values - mat2x4<${f}>(${Array(8).fill("zero_point").join(",")})) * scale;
1962              inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(t,r)=>`dot(a_data${r}, b_dequantized_values[${r}])`).join(" + ")};
1963              word_offset += ${8/c};
1964            }
1965            workgroupBarrier();
1966          }
1967
1968          if (local_idx < ${m}) {
1969            var output_value: ${p.type.value}
1969 = ${p.type.value}(0);
1970            for (var b = 0u; b < ${g}; b++) {
1971              output_value += inter_results[local_idx][b];
1972            }
1973            if (col + local_idx < uniforms.output_shape[2])
1974            {
1975              ${p.setByIndices(`${p.type.indices}(batch, row, col + local_idx)`,"output_value")}
1976            }
1977          }
1978        }`}}},n5=(t,r)=>{n4(t.inputs,r),32===r.blockSize&&t.adapterInfo.isVendor("intel")&&t.adapterInfo.isArchitecture("gen-12lp")?t.compute(n8(t.inputs,r)):t.compute(n6(t.inputs,r))},n9=t=>tc(t)}),uE=W(()=>{oG(),oX(),oY(),n7=t=>{if(!t||t.length<1)throw Error("Too few inputs");if(1!==t[0].dataType&&10!==t[0].dataType)throw Error("Input type must be float or float16.");if(t.length>=2){let r=2*t[0].dims.length===t[1].dims[0];if(4===t.length&&(r=2*t[3].dims[0]===t[1].dims[0]),!r)throw Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},se=(t,r,i)=>{let a="";for(let n=r-1;n>=0;--n)a+=`
1979            k = i32(${t.indicesGet("indices",n)}) - ${tE("uniforms.pads",n,i)};
1980            if (k < 0) {
1981              break;
1982            }
1983            if (k >= i32(${tE("uniforms.x_shape",n,r)})) {
1984              break;
1985            }
1986            offset += k * i32(${tE("uniforms.x_strides",n,r)});
1987        `;return`
1988          value = ${t.type.value}(uniforms.constant_value);
1989          for (var i = 0; i < 1; i++) {
1990            var offset = 0;
1991            var k = 0;
1992            ${a}
1993            value = x[offset];
1994          }
1995      `},st=(t,r,i)=>{let a="";for(let n=r-1;n>=0;--n)a+=`
1996                k = i32(${t.indicesGet("indices",n)}) - ${tE("uniforms.pads",n,i)};
1997                if (k < 0) {
1998                  k = -k;
1999                }
2000                {
2001                  let _2n_1 = 2 * (i32(${tE("uniforms.x_shape",n,r)}) - 1);
2002                  k = k % _2n_1;
2003                  if(k >= i32(${tE("uniforms.x_shape",n,r)})) {
2004                    k = _2n_1 - k;
2005                  }
2006                }
2007                offset += k * i32(${tE("uniforms.x_strides",n,r)});
2008            `;return`
2009              var offset = 0;
2010              var k = 0;
2011              ${a}
2012              value = x[offset];
2013          `},sr=(t,r,i)=>{let a="";for(let n=r-1;n>=0;--n)a+=`
2014                k = i32(${t.indicesGet("indices",n)}) - ${tE("uniforms.pads",n,i)};
2015                if (k < 0) {
2016                  k = 0;
2017                }
2018                if (k >= i32(${tE("uniforms.x_shape",n,r)})) {
2019                  k = i32(${tE("uniforms.x_shape",n,r)}) - 1;
2020                }
2021                offset += k * i32(${tE("uniforms.x_strides",n,r)});
2022            `;return`
2023              var offset = 0;
2024              var k = 0;
2025              ${a}
2026              value = x[offset];
2027          `},si=(t,r,i)=>{let a="";for(let n=r-1;n>=0;--n)a+=`
2028                k = i32(${t.indicesGet("indices",n)}) - ${tE("uniforms.pads",n,i)};
2029                if (k < 0)  {
2030                  k += i32(${tE("uniforms.x_shape",n,r)}]);
2031                }
2032                if (k >= i32(${tE("uniforms.x_shape",n,r)})) {
2033                  k -= i32(${tE("uniforms.x_shape",n,r)});
2034                }
2035                offset += k * i32(${tE("uniforms.x_strides",n,r)});
2036            `;return`
2037              var offset = 0;
2038              var k = 0;
2039              ${a}
2040              value = x[offset];
2041          `},sa=(t,r,i)=>{switch(i.mode){case 0:return se(t,r,i.pads.length);case 1:return st(t,r,i.pads.length);case 2:return sr(t,r,i.pads.length);case 3:return si(t,r,i.pads.length);default:throw Error("Invalid mode")}},sn=(t,r)=>{let i=tm.padShape(t[0].dims.slice(),r.pads),a=t[0].dims,n=[{type:12,data:tm.size(i)},{type:6,data:r.pads}],s=t.length>=3&&t[2].data;return 0===r.mode&&n.push({type:s?t[2].dataType:1,data:r.value}),n.push(...tS(t[0].dims,i)),{name:"Pad",shaderCache:{hint:`${r.mode}${s}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(tm.size(i)/64)},programUniforms:n}),getShaderSource:n=>{let o=tO("output",t[0].dataType,i.length),u=tA("x",t[0].dataType,a.length),l=u.type.value,d=sa(o,a.length,r),p=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:r.pads.length}];return 0===r.mode&&p.push({name:"constant_value",type:s?l:"f32"}),`
2042            ${n.registerUniforms(p).declareVariables(u,o)}
2043            ${n.mainStart()}
2044            ${n.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2045
2046            let indices = ${o.offsetToIndices("global_idx")};
2047
2048            var value = ${l}(0);
2049            ${d}
2050            output[global_idx] = value;
2051        }`}}},ss=(t,r)=>{if(!(t.length>1))return r;{let i=t[1].getBigInt64Array(),a=t.length>=3&&t[2].data?10===t[2].dataType?t[2].getUint16Array()[0]:t[2].getFloat32Array()[0]:0,n=t[0].dims.length,s=new Int32Array(2*n).fill(0);if(t.length>=4){let r=t[3].getBigInt64Array();for(let t=0;t<r.length;t++)s[Number(r[t])]=Number(i[t]),s[Number(r[t])+n]=Number(i[t+r.length])}
2051else i.forEach((t,r)=>s[Number(r)]=Number(t));let o=[];return s.forEach(t=>o.push(t)),{mode:r.mode,value:a,pads:o}}},so=(t,r)=>{n7(t.inputs);let i=ss(t.inputs,r);t.compute(sn(t.inputs,i),{inputs:[0]})}}),uz=W(()=>{el(),oG(),oX(),oY(),su=t=>{if(c.webgpu.validateInputContent&&(!t||1!==t.length))throw Error("Pool ops requires 1 input.")},sl=(t,r,i)=>{let a="NHWC"===r.format,n=t.dims.slice();a&&n.splice(1,0,n.pop());let s=Object.hasOwnProperty.call(r,"dilations"),o=r.kernelShape.slice(),u=r.strides.slice(),l=s?r.dilations.slice():[],d=r.pads.slice();tg.adjustPoolAttributes(i,n,o,u,l,d);let p=tg.computePoolOutputShape(i,n,u,l,o,d,r.autoPad),c=Object.assign({},r);s?Object.assign(c,{kernelShape:o,strides:u,pads:d,dilations:l,cacheKey:r.cacheKey}):Object.assign(c,{kernelShape:o,strides:u,pads:d,cacheKey:r.cacheKey});let h=p.slice();return h.push(h.splice(1,1)[0]),[c,a?h:p]},sd=(t,r)=>{let i="NHWC"===r.format,a=[{type:12,data:tm.size(t)},{type:12,data:tm.size(r.kernelShape)}],n=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(r.kernelShape.length<=2){let t=r.kernelShape[r.kernelShape.length-1],i=r.strides[r.strides.length-1],s=r.pads[r.pads.length/2-1],o=r.pads[r.pads.length-1],u=!!(s+o);a.push({type:12,data:t},{type:12,data:i},{type:12,data:s},{type:12,data:o}),n.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let l=!1;if(2===r.kernelShape.length){let t=r.kernelShape[r.kernelShape.length-2],i=r.strides[r.strides.length-2],s=r.pads[r.pads.length/2-2],o=r.pads[r.pads.length-2];l=!!(s+o),a.push({type:12,data:t},{type:12,data:i},{type:12,data:s},{type:12,data:o}),n.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[a,n,!0,u,l]}{if(i)throw Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let t=tm.computeStrides(r.kernelShape);return a.push({type:12,data:t},{type:12,data:r.pads},{type:12,data:r.strides}),n.push({name:"kernelStrides",type:"u32",length:t.length},{name:"pads",type:"u32",length:r.pads.length},{name:"strides",type:"u32",length:r.strides.length}),[a,n,!!r.pads.reduce((t,r)=>t+r),!1,!1]}},sp=(t,r,i,a,n,s,o,u,l,d,p,c)=>{let h="NHWC"===n.format,f=r.type.value,m=tO("output",r.type.tensor,a);if(n.kernelShape.length<=2){let a="",d="",g="",y=i-(h?2:1);if(a=p?`
2052                for (var i: u32 = 0u; i < uniforms.kw; i++) {
2053                  xIndices[${y}] = indices[${y}] * uniforms.sw - uniforms.pwStart + i;
2054                  if (xIndices[${y}] < 0 || xIndices[${y}]
2055                      >= uniforms.x_shape[${y}]) {
2056                    pad++;
2057                    continue;
2058                  }
2059                  let x_val = x[${r.indicesToOffset("xIndices")}];
2060                  ${s}
2061                }`:`
2062                for (var i: u32 = 0u; i < uniforms.kw; i++) {
2063                  xIndices[${y}] = indices[${y}] * uniforms.sw - uniforms.pwStart + i;
2064                  let x_val = x[${r.indicesToOffset("xIndices")}];
2065                  ${s}
2066                }`,2===n.kernelShape.length){let t=i-(h?3:2);d=c?`
2067                for (var j: u32 = 0u; j < uniforms.kh; j++) {
2068                  xIndices[${t}] = indices[${t}] * uniforms.sh - uniforms.phStart + j;
2069                  if (xIndices[${t}] < 0 || xIndices[${t}] >= uniforms.x_shape[${t}]) {
2070                    pad += i32(uniforms.kw);
2071                    continue;
2072                  }
2073              `:`
2074                for (var j: u32 = 0u; j < uniforms.kh; j++) {
2075                  xIndices[${t}] = indices[${t}] * uniforms.sh - uniforms.phStart + j;
2076                `,g=`
2077              }
2078            `}return`
2079            ${t.registerUniforms(l).declareVariables(r,m)}
2080
2081            ${t.mainStart()}
2082              ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2083
2084              let indices = ${m.offsetToIndices("global_idx")};
2085              var xIndices = ${m.offsetToIndices("global_idx")};
2086
2087              var value = ${f}(${u});
2088              var pad = 0;
2089              ${d}
2090              ${a}
2091              ${g}
2092              ${o}
2093
2094              output[global_idx] = value;
2095            }`}{if(h)throw Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let a=n.kernelShape.length,p=n.pads.length,c="";return c=d?`
2096                if (xIndices[j] >= uniforms.x_shape[j]) {
2097                  pad++;
2098                  isPad = true;
2099                  break;
2100                }
2101              }
2102              if (!isPad) {
2103                let x_val = x[${r.indicesToOffset("xIndices")}];
2104                ${s}
2105              }`:`
2106              }
2107              let x_val = x[${r.indicesToOffset("xIndices")}];
2108              ${s}
2109            `,`
2110            ${t.registerUniforms(l).declareVariables(r,m)}
2111
2112            ${t.mainStart()}
2113              ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2114              let indices = ${m.offsetToIndices("global_idx")};
2115              var xIndices = ${m.offsetToIndices("global_idx")};
2116
2117              var offsets: array<u32, ${a}>;
2118
2119              var value = ${f}(${u});
2120              var pad = 0;
2121              var isPad = false;
2122
2123              for (var i: u32 = 0u; i < uniforms.kernelSize; i++) {
2124                var offset = i;
2125                for (var j = 0u; j < ${a-1}u; j++) {
2126                  offsets[j] = offset / ${tE("uniforms.kernelStrides","j",a)};
2127                  offset -= offsets[j] * ${tE("uniforms.kernelStrides","j",a)};
2128                }
2129                offsets[${a-1}] = offset;
2130
2131                isPad = false;
2132                for (var j = ${i-a}u; j < ${i}u; j++) {
2133                  xIndices[j] = indices[j] * ${tE("uniforms.strides",`j - ${i-a}u`,a)}
2134                    + offsets[j - ${i-a}u] - ${tE("uniforms.pads","j - 2u",p)};
2135                  ${c}
2136              }
2137              ${o}
2138
2139              output[global_idx] = value;
2140            }`}},sc=t=>`${t.format};${t.ceilMode};${t.autoPad};${t.kernelShape.length}`,sh=t=>`${sc(t)};${t.countIncludePad}`,sf=t=>`${sc(t)};${t.storageOrder};${t.dilations}`,sm=t=>({format:t.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][t.auto_pad],ceilMode:t.ceil_mode,kernelShape:t.kernel_shape,strides:t.strides,pads:t.pads}),sg=(t,r,i,a)=>{let[n,s]=sl(r,a,i),o=tA("x",r.dataType,r.dims.length),u=o.type.value,l="";n.countIncludePad?l+=`value /= ${u}(uniforms.kernelSize);`:l+=`value /= ${u}(i32(uniforms.kernelSize) - pad);`;let[d,p,c,h,f]=sd(s,n);return d.push(...tS(r.dims,s)),{name:t,shaderCache:{hint:`${a.cacheKey};${c};${h};${f}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:s,dataType:r.dataType}],dispatchGroup:{x:Math.ceil(tm.size(s)/64)},programUniforms:d}),getShaderSource:t=>sp(t,o,r.dims.length,s.length,n,"value += x_val;",l,0,p,c,h,f)}},sy=t=>{let r=0!==t.count_include_pad,i=sm(t);if(0!==i.ceilMode)throw Error("using ceil() in shape computation is not yet supported for AveragePool");let a={countIncludePad:r,...i,cacheKey:""};return{...a,cacheKey:sh(a)}},s_=(t,r)=>{su(t.inputs),t.compute(sg("AveragePool",t.inputs[0],!1,r))},s$={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},sb=t=>{let r=t.format;return{format:r,...s$,cacheKey:r}},sv=(t,r)=>{su(t.inputs),t.compute(sg("GlobalAveragePool",t.inputs[0],!0,r))},sw=(t,r,i,a)=>{let[n,s]=sl(r,a,i),o=`
2141      value = max(x_val, value);
2142    `,u=tA("x",r.dataType,r.dims.length),[l,d,p,c,h]=sd(s,n);return l.push(...tS(r.dims,s)),{name:t,shaderCache:{hint:`${a.cacheKey};${p};${c};${h}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:s,dataType:r.dataType}],dispatchGroup:{x:Math.ceil(tm.size(s)/64)},programUniforms:l}),getShaderSource:t=>sp(t,u,r.dims.length,s.length,n,o,"",10===r.dataType?-65504:-1e5,d,p,c,h)}},sx=(t,r)=>{su(t.inputs),t.compute(sw("MaxPool",t.inputs[0],!1,r))},sS=t=>{let r=t.storage_order,i=t.dilations,a=sm(t);if(0!==r)throw Error("column major storage order is not yet supported for MaxPool");if(0!==a.ceilMode)throw Error("using ceil() in shape computation is not yet supported for MaxPool");let n={storageOrder:r,dilations:i,...a,cacheKey:""};return{...n,cacheKey:sf(n)}},sk=t=>{let r=t.format;return{format:r,...s$,cacheKey:r}},sT=(t,r)=>{su(t.inputs),t.compute(sw("GlobalMaxPool",t.inputs[0],!0,r))}}),uA=W(()=>{oG(),oX(),oZ(),oY(),sC=(t,r)=>{if(t.length<2||t.length>3)throw Error("DequantizeLinear requires 2 or 3 inputs.");if(3===t.length&&t[1].dims===t[2].dims)throw Error("x-scale and x-zero-point must have the same shape.");if(3===t.length&&t[0].dataType!==t[2].dataType)throw Error("x and x-zero-point must have the same data type.");if(6===t[0].dataType&&t.length>2)throw Error("In the case of dequantizing int32 there is no zero point.");if(0!==t[1].dims.length&&1!==t[1].dims.length&&t[1].dims.length!==t[0].dims.length)throw Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(t.length>2){if(t[0].dataType!==t[2].dataType)throw Error("x and x-zero-point must have the same data type.");if(t[1].dims.length!==t[2].dims.length)throw Error("scale and zero-point inputs must have the same rank.");if(!t[1].dims.map((r,i)=>r===t[2].dims[i]).reduce((t,r)=>t&&r,!0))throw Error("scale and zero-point inputs must have the same shape.")}if(r.blockSize>0){if(0===t[1].dims.length||1===t[1].dims.length&&1===t[1].dims[0])throw Error("blockSize must be set only for block quantization.");
2142if(!t[1].dims.map((i,a)=>a===r.axis||i===t[0].dims[a]).reduce((t,r)=>t&&r,!0))throw Error("For block qunatization, scale input shape to match the input shape except for the axis");if(t[1].dims.length!==t[0].dims.length)throw Error("For block qunatization the scale input rank must be the same as the x rank.");let i=t[0].dims[r.axis],a=t[1].dims[r.axis];if(r.blockSize<Math.ceil(i/a)||r.blockSize>Math.ceil(i/(a-1)-1))throw Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},sI=(t,r)=>{let i=tm.normalizeAxis(r.axis,t[0].dims.length),a=t[0].dataType,n=3===a,s=t[0].dims,o=t[1].dataType,u=tm.size(s),l=3===a||2===a,d=l?[Math.ceil(tm.size(t[0].dims)/4)]:t[0].dims,p=t[1].dims,c=t.length>2?t[2]:void 0,h=c?l?[Math.ceil(tm.size(c.dims)/4)]:c.dims:void 0,f=0===p.length||1===p.length&&1===p[0],m=!1===f&&1===p.length,g=tk(u),y=f&&(!l||4===g),_=y?g:1,$=tA("input",l?12:a,d.length,y&&!l?g:1),b=tA("scale",o,p.length),v=c?tA("zero_point",l?12:a,h.length):void 0,w=tO("output",o,s.length,_),x=[$,b];v&&x.push(v);let S=[d,p];c&&S.push(h);let k=[{type:12,data:u/_},{type:12,data:i},{type:12,data:r.blockSize},...tS(...S,s)];return{name:"DequantizeLinear",shaderCache:{hint:r.cacheKey,inputDependencies:v?["rank","rank","rank"]:["rank","rank"]},getShaderSource:t=>`
2143      ${t.registerUniforms([{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}]).declareVariables(...x,w)}
2144      ${t.mainStart()}
2145          ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2146          let output_indices = ${w.offsetToIndices("global_idx")};
2147
2148          // Set input x
2149          ${l?`
2150            let input = ${$.getByOffset("global_idx / 4")};
2151            let x_vec = ${n?"unpack4xI8(input)":"unpack4xU8(input)"};
2152            let x_value = ${1===_?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${$.getByOffset("global_idx")};`};
2153
2154          // Set scale input
2155          ${f?`let scale_value= ${b.getByOffset("0")}`:m?`
2156            let scale_index = ${w.indicesGet("output_indices","uniforms.axis")};
2157            let scale_value= ${b.getByOffset("scale_index")};`:`
2158            var scale_indices: ${b.type.indices} = output_indices;
2159            let index = ${b.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size;
2160            ${b.indicesSet("scale_indices","uniforms.axis","index")};
2161            let scale_value= ${b.getByIndices("scale_indices")};`};
2162
2163          // Set zero-point input
2164          ${v?f?l?`
2165                let zero_point_input = ${v.getByOffset("0")};
2166                let zero_point_vec =  ${n?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
2167                let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${v.getByOffset("0")}`:m?l?`
2168                let zero_point_index = ${w.indicesGet("output_indices","uniforms.axis")};
2169                let zero_point_input = ${v.getByOffset("zero_point_index / 4")};
2170                let zero_point_vec =  ${n?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
2171                let zero_point_value = zero_point_vec[zero_point_index % 4]`:`
2172                let zero_point_index = ${w.indicesGet("output_indices","uniforms.axis")};
2173                let zero_point_value = ${v.getByOffset("zero_point_index")};`:l?`
2174                let zero_point_offset = ${b.indicesToOffset("scale_indices")};
2175                let zero_point_input = ${v.getByOffset("zero_point_offset / 4")};
2176                let zero_point_vec = ${n?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
2177                let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${v.getByIndices("scale_indices")};`:`let zero_point_value = ${l?n?"i32":"u32":$.type.value}(0);`};
2178      // Compute and write output
2179      ${w.setByOffset("global_idx",`${w.type.value}(x_value - zero_point_value) * scale_value`)};
2180      }`,getRunData:()=>({outputs:[{dims:s,dataType:o}],dispatchGroup:{x:Math.ceil(u/_/64),y:1,z:1},programUniforms:k})}},sE=(t,r)=>{sC(t.inputs,r),t.compute(sI(t.inputs,r))},sz=t=>tc({axis:t.axis,blockSize:t.blockSize})}),uO=W(()=>{el(),oG(),oY(),sA=(t,r,i)=>{if(t===r||t<r&&i<0||t>r&&i>0)throw Error("Range these inputs' contents are invalid.")},sO=(t,r,i,a)=>{let n=Math.abs(Math.ceil((r-t)/i)),s=[n],o=[{type:12,data:n},{type:a,data:t},{type:a,data:i},...tS(s)];return{name:"Range",shaderCache:{hint:`${a}`}
2180,getShaderSource:t=>{let r=tO("output",a,s.length),i=r.type.value;return`
2181        ${t.registerUniforms([{name:"outputSize",type:"u32"},{name:"start",type:i},{name:"delta",type:i}]).declareVariables(r)}
2182        ${t.mainStart()}
2183        ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2184        output[global_idx] = uniforms.start + ${i}(global_idx) * uniforms.delta;
2185      }`},getRunData:()=>({outputs:[{dims:s,dataType:a}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:o})}},sR=t=>{let r=0,i=0,a=0;6===t.inputs[0].dataType?(r=t.inputs[0].getInt32Array()[0],i=t.inputs[1].getInt32Array()[0],a=t.inputs[2].getInt32Array()[0]):1===t.inputs[0].dataType&&(r=t.inputs[0].getFloat32Array()[0],i=t.inputs[1].getFloat32Array()[0],a=t.inputs[2].getFloat32Array()[0]),c.webgpu.validateInputContent&&sA(r,i,a),t.compute(sO(r,i,a,t.inputs[0].dataType),{inputs:[]})}}),uR=W(()=>{oG(),oX(),oZ(),oY(),sB=(t,r,i,a)=>{if("none"!==t&&"i32"!==a&&"u32"!==a&&"f32"!==a)throw Error(`Input ${a} is not supported with reduction ${t}.`);let n=`{
2186                var oldValue = 0;
2187                loop {
2188                  let newValueF32 =`,s=`;
2189                  let newValue = bitcast<i32>(newValueF32);
2190                  let res = atomicCompareExchangeWeak(&${r}, oldValue, newValue);
2191                  if res.exchanged {
2192                    break;
2193                  }
2194                  oldValue = res.old_value;
2195                }
2196              }`;switch(t){case"none":return`${r}=${i};`;case"add":return"i32"===a||"u32"===a?`atomicAdd(&${r}, bitcast<${a}>(${i}));`:`
2197              ${n}bitcast<${a}>(oldValue) + (${i})${s}`;case"max":return"i32"===a||"u32"===a?`atomicMax(&${r}, bitcast<${a}>(${i}));`:`
2198                ${n}max(bitcast<f32>(oldValue), (${i}))${s}`;case"min":return"i32"===a||"u32"===a?`atomicMin(&${r}, bitcast<${a}>(${i}));`:`${n}min(bitcast<${a}>(oldValue), (${i}))${s}`;case"mul":return`${n}(bitcast<${a}>(oldValue) * (${i}))${s}`;default:throw Error(`Reduction ${t} is not supported.`)}},sN=(t,r)=>{let i=t[0].dims,a=t[1].dims,n=Math.ceil(tm.size(a)/1),s=a[a.length-1],o=tm.sizeFromDimension(i,s),u=[{type:12,data:n},{type:12,data:s},{type:12,data:o},...tS(t[1].dims,t[2].dims,i)];return{name:"ScatterND",shaderCache:{hint:`${r.cacheKey}_${r.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:u}),getShaderSource:n=>{let s=tA("indices",t[1].dataType,t[1].dims.length),o=tA("updates",t[2].dataType,t[2].dims.length,1),u="none"!==r.reduction&&""!==r.reduction?tR("output",t[0].dataType,i.length):tO("output",t[0].dataType,i.length,1);return`
2199      ${n.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(s,o,u)}
2200      ${n.mainStart()}
2201        ${n.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2202  var hasDuplicates = false;
2203  if (${"none"===r.reduction}) {
2204    let n = ${tm.size(a)};
2205    for (var i = 0; i < n; i = i + 1) {
2206      for (var j = i + 1; j < n; j = j + 1) {
2207        var index_i = i32(indices[i].x);
2208        var index_j = i32(indices[j].x);
2209        if (index_i == index_j) {
2210          hasDuplicates = true;
2211          break;
2212        }
2213      }
2214      if (hasDuplicates) {
2215        break;
2216      }
2217    }
2218  }
2219
2220  var data_offset = 0u;
2221  var indices_start = uniforms.last_index_dimension * global_idx;
2222  if (${"none"===r.reduction} && hasDuplicates) {
2223    if (global_idx != 0u) {
2224      return;
2225    }
2226    indices_start = 0u;
2227  }
2228  let indices_end = indices_start + uniforms.last_index_dimension;
2229  for (var i = indices_start; i < indices_end; i++) {
2230    var index = i32(indices[i].x);
2231    ${1===t[0].dims.length?`
2232    let element_count_dim = uniforms.output_strides;
2233    let dim_value = uniforms.output_shape;`:`
2234    let element_count_dim = uniforms.output_strides[i - indices_start];
2235    let dim_value = uniforms.output_shape[i - indices_start + uniforms.last_index_dimension];`}
2236    if (index >= 0) {
2237      if (index >= i32(dim_value)) {
2238        index = i32(dim_value - 1);
2239      }
2240    } else {
2241      if (index < -i32(dim_value)) {
2242        index = 0;
2243      } else {
2244        index += i32(dim_value);
2245      }
2246    }
2247    data_offset += u32((u32(index) * element_count_dim));
2248  }
2249
2250  for (var i = 0u; i < uniforms.num_updates_elements; i++) {
2251    let value = updates[uniforms.num_updates_elements * global_idx + i];
2252    ${sB(r.reduction,"output[data_offset + i]","value",u.type.value)}
2253  }
2254
2255      }`}}},sM=t=>tc({reduction:t.reduction}),sD=(t,r)=>{t.compute(sN(t.inputs,r),{inputs:[t.inputs[1],t.inputs[2]],outputs:[]})}}),uB=W(()=>{oG(),oX(),oZ(),oY(),sP=(t,r)=>{if(t.every(t=>t>0||(()=>{throw Error("Resize requires scales input values to be positive")})),t.length>0){if("linear"===r.mode){if(!(2===t.length||3===t.length||4===t.length&&1===t[0]&&1===t[1]||4===t.length&&1===t[0]&&1===t[3]||5===t.length&&1===t[0]&&1===t[1]))throw Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and
2256            one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if("cubic"===r.mode&&!(2===t.length||4===t.length&&1===t[0]&&1===t[1]||4===t.length&&1===t[0]&&1===t[3]))throw Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},sU=(t,r,i)=>{r.every(t=>t>=0&&t<i||(()=>{throw Error("Resize requires axes input values to be positive and less than rank")}));let a=Array(i).fill(1);return r.forEach((r,i)=>a[r]=t[i]),a},sW=(t,r,i,a,n,s)=>{let[o,u,l]=i>10?[1,2,3]:[-1,t.length>1?1:-1,-1],d=t[0].dims.length;if(o>0&&t.length>o&&t[o].dims.length>0)t[o].getFloat32Array().forEach(t=>s.push(t));else if("tf_crop_and_resize"===r.coordinateTransformMode)throw Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(u>0&&t.length>u&&1===t[u].dims.length&&t[u].dims[0]>0){if(t[u].getFloat32Array().forEach(t=>a.push(t)),0!==a.length&&a.length!==d&&i>=18&&a.length!==r.axes.length)throw Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");sP(a,r),r.axes.length>0&&sU(a,r.axes,d).forEach((t,r)=>a[r]=t)}if(l>0&&t.length>l&&1===t[l].dims.length&&t[l].dims[0]>0&&(t[l].getBigInt64Array().forEach(t=>n.push(Number(t))),0!==n.length&&n.length!==d&&i>=18&&n.length!==r.axes.length))throw Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(r.axes.length>0){if(0!==a.length&&a.length!==r.axes.length)throw Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(0!==n.length&&n.length!==r.axes.length)throw Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if("u">typeof a&&"u">typeof n&&a.length>0&&n.length>d)throw Error("Resize requires only of scales or sizes to be specified")},sq=(t,r,i,a)=>`
2257  // The whole part and the fractional part are calculated separately due to inaccuracy of floating
2258  // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an
2259  // offset-by-one error later in floor().
2260  let big = (${t}) * (${r});
2261  let whole = ${a}(big / (${i}));
2262  let fract = ${a}(big % (${i})) / ${a}(${i});
2263  return whole + fract;
2264`,sL=(t,r)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32,
2265     lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${r} { `+(()=>{switch(t){case"asymmetric":return`
2266          if (xScale < 1.0 || floor(xScale) != xScale) {
2267            return ${r}(xResized) / ${r}(xScale);
2268          } else {
2269            ${sq("xResized","lengthOriginal","lengthResized",r)}
2270          }
2271        `;case"pytorch_half_pixel":return`if (lengthResized > 1) {
2272                    return (${r}(xResized) + 0.5) / ${r}(xScale) - 0.5;
2273                  } else {
2274                    return 0.0;
2275                  }`;case"tf_half_pixel_for_nn":return`return (${r}(xResized) + 0.5) / ${r}(xScale);`;case"align_corners":return`if (lengthResized == 1) {
2276                    return 0.0;
2277                  } else {
2278                    ${sq("xResized","lengthOriginal - 1","lengthResized - 1",r)}
2279                  }`;case"tf_crop_and_resize":return`if (lengthResized > 1) {
2280                    return ${r}(roiStart) * ${r}(lengthOriginal - 1) +
2281                        (${r}(xResized) * ${r}(roiEnd - roiStart) * ${r}(lengthOriginal - 1)) /
2282                        ${r}(lengthResized - 1);
2283                  } else {
2284                    return 0.5 * ${r}(roiStart + roiEnd) * ${r}(lengthOriginal - 1);
2285                  }`;case"half_pixel_symmetric":return`const outputWidth = ${r}xScale * ${r}(lengthResized);
2286                  const adjustment = ${r}(lengthResized) / outputWidth;
2287                  const center = ${r}(lengthOriginal) / 2;
2288                  const offset = center * (1 - adjustment);
2289                  return offset + ((${r}(xResized) + 0.5) / ${r}(xScale)) - 0.5;`;case"half_pixel":return`return ((${r}(xResized) + 0.5) / ${r}(xScale)) - 0.5;`;default:throw Error(`Coordinate transform mode ${t} is not supported`)}})()+"}",sV=(t,r,i)=>`fn getNearestPixelFromOriginal(xOriginal: ${i}, isDownSample: bool) -> ${i} {`+(()=>{switch(t){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) {             return ceil(xOriginal);           } else {             return round(xOriginal);           }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) {                     return floor(xOriginal);                   } else {                     return round(xOriginal);                   }";default:if(r<11)return"if (isDownSample)                     {                       return ceil(xOriginal);                     } else {                       return xOriginal;                     }";throw Error(`Nearest mode ${t} is not supported`)}})()+"}",sG=(t,r,i)=>{let a=Array(i).fill(0).concat(Array(i).fill(1)),n=0===t.length?a:t.slice();return r.length>0?(r.forEach((t,s)=>{a[t]=n[s],a[s+i]=n[r.length+s]}),a):n},sH=(t,r,i,a)=>{let n=[];if(i.length>0){if(a.length>0){if(t.forEach(t=>n.push(t)),Math.max(...a)>t.length)throw Error("axes is out of bound");a.forEach((t,r)=>n[t]=i[r])}
2289else i.forEach(t=>n.push(t))}else{if(0===r.length)throw Error("Resize requires either scales or sizes.");n=t.map((t,i)=>Math.round(t*r[i]))}return n},sF=(t,r,i)=>{let a=(()=>{switch(i.keepAspectRatioPolicy){case"not_larger":return i.axes.length>0?Math.min(...i.axes.map(t=>r[t]),Number.MAX_VALUE):Math.min(...r,Number.MAX_VALUE);case"not_smaller":return i.axes.length>0?Math.max(...i.axes.map(t=>r[t]),Number.MIN_VALUE):Math.max(...r,Number.MIN_VALUE);default:throw Error(`Keep aspect ratio policy ${i.keepAspectRatioPolicy} is not supported`)}})();r.fill(1,0,r.length);let n=t.slice();return i.axes.length>0?(i.axes.forEach(t=>r[t]=a),i.axes.forEach(i=>n[i]=Math.round(t[i]*r[i]))):(r.fill(a,0,r.length),n.forEach((t,i)=>n[i]=Math.round(t*r[i]))),n},sj=(t,r,i,a,n)=>`
2290    fn calculateOriginalIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> array<${t.type.value}, ${i.length}> {
2291      var original_indices: array<${t.type.value}, ${i.length}>;
2292      for (var i:u32 = 0; i < ${i.length}; i++) {
2293        var output_index = ${t.indicesGet("output_indices","i")};
2294        var scale = ${tE("uniforms.scales","i",a)};
2295        var roi_low = ${tE("uniforms.roi","i",n)};
2296        var roi_hi = ${tE("uniforms.roi",`i + ${r.length}`,n)};
2297        if (scale == 1.0) {
2298          original_indices[i] = ${t.type.value}(output_index);
2299        } else {
2300          var input_shape_i = ${tE("uniforms.input_shape","i",r.length)};
2301          var output_shape_i = ${tE("uniforms.output_shape","i",i.length)};
2302          original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
2303                                                                           input_shape_i, roi_low, roi_hi);
2304        }
2305      }
2306      return original_indices;
2307    }`,sK=(t,r,i,a,n,s,o)=>`
2308    fn calculateInputIndicesFromOutputIndices(output_indices: ${r.type.indices}) -> ${t.type.indices} {
2309      var input_indices: ${t.type.indices};
2310      for (var i:u32 = 0; i < ${a.length}; i++) {
2311        var output_index = ${r.indicesGet("output_indices","i")};
2312        var input_index: u32;
2313        var scale = ${tE("uniforms.scales","i",n)};
2314        if (scale == 1.0) {
2315          input_index = output_index;
2316        } else {
2317          var roi_low = ${tE("uniforms.roi","i",s)};
2318          var roi_hi = ${tE("uniforms.roi",`i + ${i.length}`,s)};
2319          var input_shape_i = ${tE("uniforms.input_shape","i",i.length)};
2320          var output_shape_i = ${tE("uniforms.output_shape","i",a.length)};
2321          var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
2322                                                                        input_shape_i, roi_low, roi_hi);
2323          if (!${o} || (original_idx >= 0 && original_idx < ${r.type.value}(input_shape_i))) {
2324            if (original_idx < 0) {
2325              input_index = 0;
2326            } else if (original_idx > ${r.type.value}(input_shape_i - 1)) {
2327              input_index = input_shape_i - 1;
2328            } else {
2329              input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1));
2330            }
2331          } else {
2332            input_index = u32(original_idx);
2333          }
2334        }
2335        ${t.indicesSet("input_indices","i","input_index")}
2336      }
2337      return input_indices;
2338    }`,sQ=(t,r)=>`
2339    fn checkInputIndices(input_indices: ${t.type.indices}) -> bool {
2340      for (var i:u32 = 0; i < ${r.length}; i++) {
2341        var input_index = ${t.indicesGet("input_indices","i")};
2342        if (input_index < 0 || input_index >= ${tE("uniforms.input_shape","i",r.length)}) {
2343          return false;
2344        }
2345      }
2346      return true;
2347    }`,sZ=(t,r,i,a)=>t.rank>a?`
2348    ${t.indicesSet("input_indices",r,"channel")};
2349    ${t.indicesSet("input_indices",i,"batch")};
2350`:"",sX=(t,r,i,a,n)=>{let[s,o,u,l]=2===i.length?[-1,0,1,-1]:[0,2,3,1],d=t.type.value;return`
2351    fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${d} {
2352      var input_indices: ${t.type.indices};
2353      ${t.indicesSet("input_indices",o,`max(0, min(row, ${i[o]} - 1))`)};
2354      ${t.indicesSet("input_indices",u,`max(0, min(col, ${i[u]} - 1))`)};
2355      ${sZ(t,l,s,2)}
2356      return ${t.getByIndices("input_indices")};
2357    }
2358
2359    fn bilinearInterpolation(output_indices: ${r.type.indices}) -> ${d} {
2360      var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);
2361      var row:${d} = originalIndices[${o}];
2362      var col:${d} = originalIndices[${u}];
2363      ${a?`if (row < 0 || row > (${i[o]}
2363 - 1) || col < 0 || col > (${i[u]} - 1)) {
2364        return ${n};
2365      }`:""};
2366      row = max(0, min(row, ${i[o]} - 1));
2367      col = max(0, min(col, ${i[u]} - 1));
2368      var row1: u32 = u32(row);
2369      var col1: u32 = u32(col);
2370      var row2: u32 = u32(row + 1);
2371      var col2: u32 = u32(col + 1);
2372      var channel: u32 = ${i.length>2?`u32(originalIndices[${l}])`:"0"};
2373      var batch: u32 =  ${i.length>2?`u32(originalIndices[${s}])`:"0"};
2374      var x11: ${d} = getInputValue(batch, channel, row1, col1);
2375      var x12: ${d} = getInputValue(batch, channel, row1, col2);
2376      var x21: ${d} = getInputValue(batch, channel, row2, col1);
2377      var x22: ${d} = getInputValue(batch, channel, row2, col2);
2378      var dx1: ${d} = abs(row - ${d}(row1));
2379      var dx2: ${d} = abs(${d}(row2) - row);
2380      var dy1: ${d} = abs(col - ${d}(col1));
2381      var dy2: ${d} = abs(${d}(col2) - col);
2382      if (row1 == row2) {
2383        dx1 = 0.5;
2384        dx2 = 0.5;
2385      }
2386      if (col1 == col2) {
2387        dy1 = 0.5;
2388        dy2 = 0.5;
2389      }
2390      return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1);
2391    }`},sY=(t,r,i,a,n,s,o,u,l,d)=>{let[p,c]=2===i.length?[0,1]:[2,3],h=t.type.value,f=o=>{let c=o===p?"row":"col";return`
2392      fn ${c}CubicInterpolation(input_indices: ${t.type.indices}, output_indices: ${r.type.indices}) -> ${h} {
2393        var output_index = ${r.indicesGet("output_indices",o)};
2394        var originalIdx: ${h} = getOriginalCoordinateFromResizedCoordinate(output_index, ${n[o]},
2395        ${a[o]}, ${i[o]}, ${s[o]}, ${s[o]} + ${i.length});
2396        var fractOriginalIdx: ${h} = originalIdx - floor(originalIdx);
2397        var coefs = getCubicInterpolationCoefs(fractOriginalIdx);
2398
2399        if (${u} && (originalIdx < 0 || originalIdx > (${i[o]} - 1))) {
2400          return ${l};
2401        }
2402        var data: array<${h}, 4> = array<${h}, 4>(0.0, 0.0, 0.0, 0.0);
2403        for (var i: i32 = -1; i < 3; i++) {
2404          var ${c}: ${h} = originalIdx + ${h}(i);
2405          if (${c} < 0 || ${c} >= ${i[o]}) {
2406            ${d?`coefs[i + 1] = 0.0;
2407                        continue;`:u?`return ${l};`:`${c} = max(0, min(${c}, ${i[o]} - 1));`};
2408          }
2409        var input_indices_copy: ${t.type.indices} = input_indices;
2410          ${t.indicesSet("input_indices_copy",o,`u32(${c})`)};
2411          data[i + 1] = ${o===p?t.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"};
2412        }
2413        return cubicInterpolation1D(data, coefs);
2414      }`};return`
2415    ${f(p)};
2416    ${f(c)};
2417  fn getCubicInterpolationCoefs(s: ${h}) -> array<${h}, 4> {
2418    var absS = abs(s);
2419    var coeffs: array<${h}, 4> = array<${h}, 4>(0.0, 0.0, 0.0, 0.0);
2420    var oneMinusAbsS: ${h} = 1.0 - absS;
2421    var twoMinusAbsS: ${h} = 2.0 - absS;
2422    var onePlusAbsS: ${h} = 1.0 + absS;
2423    coeffs[0] = ((${o} * onePlusAbsS - 5 * ${o}) * onePlusAbsS + 8 * ${o}) * onePlusAbsS - 4 * ${o};
2424    coeffs[1] = ((${o} + 2) * absS - (${o} + 3)) * absS * absS + 1;
2425    coeffs[2] = ((${o} + 2) * oneMinusAbsS - (${o} + 3)) * oneMinusAbsS * oneMinusAbsS + 1;
2426    coeffs[3] = ((${o} * twoMinusAbsS - 5 * ${o}) * twoMinusAbsS + 8 * ${o}) * twoMinusAbsS - 4 * ${o};
2427    return coeffs;
2428  }
2429
2430  fn cubicInterpolation1D(x: array<${h}, 4>, coefs: array<${h}, 4>) -> ${h} {
2431    var coefsSum: ${h} = coefs[0] + coefs[1] + coefs[2] + coefs[3];
2432    return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum;
2433  }
2434
2435  fn bicubicInterpolation(output_indices: ${r.type.indices}) -> ${h} {
2436    var input_indices: ${t.type.indices} = output_indices;
2437    return colCubicInterpolation(input_indices, output_indices);
2438  }
2439    `},sJ=(t,r,i,a,n)=>{let[s,o,u,l,d]=3===i.length?[-1,0,1,2,-1]:[0,2,3,4,1],p=t.type.value;return`
2440    fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${p} {
2441      var input_indices: ${t.type.indices};
2442      ${t.indicesSet("input_indices",o,`max(0, min(depth, ${i[o]} - 1))`)};
2443      ${t.indicesSet("input_indices",u,`max(0, min(height, ${i[u]} - 1))`)};
2444      ${t.indicesSet("input_indices",l,`max(0, min(width, ${i[l]} - 1))`)};
2445      ${sZ(t,d,s,3)}
2446      return ${t.getByIndices("input_indices")};
2447    }
2448
2449    fn trilinearInterpolation(output_indices: ${r.type.indices}) -> ${p} {
2450      var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);
2451      var depth:${p} = originalIndices[${o}];
2452      var height:${p} = originalIndices[${u}];
2453      var width:${p} = originalIndices[${l}];
2454      ${a?`if (depth < 0 || depth > (${i[o]} - 1) || height < 0 || height > (${i[u]} - 1) || width < 0 || (width > ${i[l]} - 1)) {
2455      return ${n};
2456        }`:""};
2457
2458    depth = max(0, min(depth, ${i[o]} - 1));
2459      height = max(0, min(height, ${i[u]} - 1));
2460      width = max(0, min(width, ${i[l]} - 1));
2461      var depth1: u32 = u32(depth);
2462      var height1: u32 = u32(height);
2463      var width1: u32 = u32(width);
2464      var depth2: u32 = u32(depth + 1);
2465      var height2: u32 = u32(height + 1);
2466      var width2: u32 = u32(width + 1);
2467      var channel: u32 = ${i.length>3?`u32(originalIndices[${d}])`:"0"};
2468      var batch: u32 =  ${i.length>3?`u32(originalIndices[${s}])`:"0"};
2469
2470      var x111: ${p} = getInputValue(batch, channel, depth1, height1, width1);
2471      var x112: ${p} = getInputValue(batch, channel, depth1, height1, width2);
2472      var x121: ${p} = getInputValue(batch, channel, depth1, height2, width1);
2473      var x122: ${p} = getInputValue(batch, channel, depth1, height2, width2);
2474      var x211: ${p} = getInputValue(batch, channel, depth2, height1, width1);
2475      var x212: ${p} = getInputValue(batch, channel, depth2, height1, width2);
2476      var x221: ${p} = getInputValue(batch, channel, depth2, height2, width1);
2477      var x222: ${p} = getInputValue(batch, channel, depth2, height2, width2);
2478      var dx1: ${p} = abs(depth - ${p}(depth1));
2479      var dx2: ${p} = abs(${p}(depth2) - depth);
2480      var dy1: ${p} = abs(height - ${p}(height1));
2481      var dy2: ${p} = abs(${p}(height2) - height);
2482      var dz1: ${p} = abs(width - ${p}(width1));
2483      var dz2: ${p} = abs(${p}(width2) - width);
2484      if (depth1 == depth2) {
2485        dx1 = 0.5;
2486        dx2 = 0.5;
2487      }
2488      if (height1 == height2) {
2489        dy1 = 0.5;
2490        dy2 = 0.5;
2491      }
2492      if (width1 == width2) {
2493        dz1 = 0.5;
2494        dz2 = 0.5;
2495      }
2496      return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 +
2497              x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1);
2498    }`},s0=(t,r,i,a,n,s)=>{let o=t.dims,u=sG(s,r.axes,o.length),l=sH(o,a,n,r.axes),d=a.slice();0===a.length&&(d=o.map((t,r)=>0===t?1:l[r]/t),"stretch"!==r.keepAspectRatioPolicy&&(l=sF(o,d,r)));let p=tO("output",t.dataType,l.length),c=tA("input",t.dataType,o.length),h=tm.size(l),f=o.length===l.length&&o.every((t,r)=>t===l[r]),m="tf_crop_and_resize"===r.coordinateTransformMode,g=r.extrapolationValue,y=c.type.value;return{name:"Resize",shaderCache:{hint:`${r.cacheKey}|${i}|${d.length>0?"cubic"===r.mode?d:d.length:""}|${n.length>0?n:""}|${u.length>0?u:""}|${f}|${"nearest"===r.mode?o.length:o}`,inputDependencies:["rank"]},getShaderSource:t=>`
2499      ${f?"":`
2500      ${sL(r.coordinateTransformMode,y)};
2501      ${(()=>{switch(r.mode){case"nearest":return`
2502              ${sQ(c,o)};
2503              ${sV(r.nearestMode,i,y)};
2504              ${sK(c,p,o,l,d.length,u.length,m)};
2505              `;case"linear":return`
2506              ${sj(p,o,l,d.length,u.length)};
2507              ${(()=>{if(2===o.length||4===o.length)return`${sX(c,p,o,m,g)}`;if(3===o.length||5===o.length)return`${sJ(c,p,o,m,g)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()};
2508            `;case"cubic":return`
2509            ${(()=>{if(2===o.length||4===o.length)return`${sY(c,p,o,l,d,u,r.cubicCoeffA,m,r.extrapolationValue,r.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()};
2510            `;default:throw Error("Invalid resize mode")}})()};
2511      `}
2512      ${t.registerUniform("output_size","u32").registerUniform("scales","f32",d.length).registerUniform("roi","f32",u.length).declareVariables(c,p)}
2513      ${t.mainStart()}
2514        ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2515        ${f?"output[global_idx] = input[global_idx];":`
2516        let output_indices = ${p.offsetToIndices("global_idx")};
2517        var input_indices: ${c.type.indices};
2518        ${(()=>{switch(r.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices);
2519                if (checkInputIndices(input_indices)) {
2520                  output[global_idx] = ${c.getByIndices("input_indices")};
2521                } else {
2522                  output[global_idx] = ${r.extrapolationValue};
2523                }`;case"linear":return`output[global_idx] = ${2===o.length||4===o.length?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${r.mode}`)}})()};
2524`}
2525      }`,getRunData:()=>({outputs:[{dims:l,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:[{type:12,data:h},{type:1,data:d}
2525,{type:1,data:u},...tS(o,l)]})}},s1=t=>{let r=t.customDataBuffer;return new Uint32Array(r,r.byteOffset,1)[0]},s2=(t,r)=>{let i=[],a=[],n=[],s=s1(t);if(0!==r.antialias)throw Error("Only default value (0) for Antialias attribute is supported");sW(t.inputs,r,s,i,a,n),t.compute(s0(t.inputs[0],r,s,i,a,n),{inputs:[0]})},s3=t=>{let r=t.antialias,i=t.axes,a=t.coordinateTransformMode,n=t.cubicCoeffA,s=0!==t.excludeOutside,o=t.extrapolationValue,u=t.keepAspectRatioPolicy,l=t.mode,d=""===t.nearestMode?"simple":t.nearestMode;return tc({antialias:r,axes:i,coordinateTransformMode:a,cubicCoeffA:n,excludeOutside:s,extrapolationValue:o,keepAspectRatioPolicy:u,mode:l,nearestMode:d})}}),uN=W(()=>{oG(),oX(),oZ(),oY(),s4=(t,r)=>{let[i,a,n,s]=t,{numHeads:o,rotaryEmbeddingDim:u}=r;if(3!==i.dims.length&&4!==i.dims.length)throw Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${i.dims.length}`);if(!tm.areEqual(a.dims,[])&&!tm.areEqual(a.dims,[1])&&2!==a.dims.length)throw Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${a.dims.length}`);if(2!==n.dims.length)throw Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${n.dims.length}`);if(2!==s.dims.length)throw Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${s.dims.length}`);if(!tm.areEqual(n.dims,s.dims))throw Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(u>0&&0===o)throw Error("num_heads must be provided if rotary_embedding_dim is specified");let l=i.dims[0],d=i.dims[i.dims.length-2],p=n.dims[0],c=tm.sizeFromDimension(i.dims,1)/d,h=0===u?2*n.dims[1]:c/o;if(u>h)throw Error("rotary_embedding_dim must be less than or equal to head_size");if(2===a.dims.length){if(l!==a.dims[0])throw Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${a.dims[0]}`);if(d!==a.dims[1])throw Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${a.dims[1]}`)}if(h/2!==n.dims[1]&&u/2!==n.dims[1])throw Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${n.dims[1]}`);if(d>p)throw Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},s6=(t,r)=>{let{interleaved:i,numHeads:a,rotaryEmbeddingDim:n,scale:s}=r,o=t[0].dims[0],u=tm.sizeFromDimension(t[0].dims,1),l=t[0].dims[t[0].dims.length-2],d=u/l,p=t[2].dims[1],c=0===n?2*p:d/a,h=[o,l,d/c,c-p],f=tm.computeStrides(h),m=[{type:1,data:s},{type:12,data:h},{type:12,data:f},...3===t[0].dims.length?Array({type:12,data:[u,d,c,1]}):[],...4===t[0].dims.length?Array({type:12,data:[u,c,l*c,1]}):[],...tS(t[0].dims,t[1].dims,t[2].dims,t[3].dims,t[0].dims)];return{name:"RotaryEmbedding",shaderCache:{hint:tc({interleaved:i}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:r=>{let a=tA("input",t[0].dataType,t[0].dims.length),n=tA("position_ids",t[1].dataType,t[1].dims.length),s=tA("cos_cache",t[2].dataType,t[2].dims.length),o=tA("sin_cache",t[3].dataType,t[3].dims.length),u=tO("output",t[0].dataType,t[0].dims.length);return r.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:h.length},{name:"global_strides",type:"u32",length:f.length},{name:"input_output_strides",type:"u32",length:f.length}]),`
2526        ${r.declareVariables(a,n,s,o,u)}
2527
2528        ${r.mainStart(tb)}
2529          let half_rotary_emb_dim = uniforms.${s.name}_shape[1];
2530          let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape;
2531          let size = uniforms.global_shape[0] * uniforms.global_strides[0];
2532          ${r.guardAgainstOutOfBoundsWorkgroupSizes("size")}
2533
2534          if (bsnh[3] < half_rotary_emb_dim) {
2535            let position_ids_idx =
2536                ${n.broadcastedIndicesToOffset("bsnh.xy",tO("",n.type.tensor,2))};
2537            let position_id =
2538                u32(${n.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0);
2539            let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${i});
2540            let j = i + select(half_rotary_emb_dim, 1, ${i});
2541            let re = ${a.getByOffset("i")}
2541 * ${s.get("position_id","bsnh[3]")} -
2542                ${a.getByOffset("j")} * ${o.get("position_id","bsnh[3]")};
2543            ${u.setByOffset("i","re")}
2544            let im = ${a.getByOffset("i")} * ${o.get("position_id","bsnh[3]")} +
2545                ${a.getByOffset("j")} * ${s.get("position_id","bsnh[3]")};
2546            ${u.setByOffset("j","im")}
2547          } else {
2548            let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim;
2549            ${u.setByOffset("k",a.getByOffset("k"))}
2550          }
2551        }`},getRunData:()=>({outputs:[{dims:t[0].dims,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(tm.size(h)/tb)},programUniforms:m})}},s8=(t,r)=>{s4(t.inputs,r),t.compute(s6(t.inputs,r))}}),uM=W(()=>{oG(),oX(),oY(),s5=t=>{if(!t||t.length<3)throw Error("layerNorm requires at least 3 inputs.");let r=t[0],i=t[1],a=t[2];if(r.dataType!==i.dataType||r.dataType!==a.dataType)throw Error("All inputs must have the same data type");if(3!==r.dims.length&&2!==r.dims.length)throw Error("Input must be 2D or 3D");if(3!==i.dims.length&&2!==i.dims.length)throw Error("Skip must be 2D or 3D");let n=r.dims[r.dims.length-1],s=r.dims[r.dims.length-2];if(i.dims[i.dims.length-1]!==n)throw Error("Skip must have the same hidden size as input");if(i.dims[i.dims.length-2]!==s)throw Error("Skip must have the same sequence length as input");if(1!==a.dims.length)throw Error("Gamma must be 1D");if(a.dims[a.dims.length-1]!==n)throw Error("Gamma must have the same hidden size as input");if(t.length>3){let r=t[3];if(1!==r.dims.length)throw Error("Beta must be 1D");if(r.dims[r.dims.length-1]!==n)throw Error("Beta must have the same hidden size as input")}if(t.length>4){let r=t[4];if(1!==r.dims.length)throw Error("Bias must be 1D");if(r.dims[r.dims.length-1]!==n)throw Error("Bias must have the same hidden size as input")}},s9=(t,r,i,a)=>{let n=r.simplified,s=t[0].dims,o=tm.size(s),u=s.slice(-1)[0],l=a?s.slice(0,-1).concat(1):[],d=!n&&t.length>3,p=t.length>4,c=a&&i>1,h=a&&i>2,f=i>3,m=tk(u),g=[{type:12,data:o},{type:12,data:m},{type:12,data:u},{type:1,data:r.epsilon}],y=[{dims:s,dataType:t[0].dataType}];return i>1&&y.push({dims:l,dataType:1}),i>2&&y.push({dims:l,dataType:1}),i>3&&y.push({dims:s,dataType:t[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${m};${c};${h};${f}`,inputDependencies:t.map((t,r)=>"type")},getShaderSource:r=>{let i=[tA("x",t[0].dataType,t[0].dims,m),tA("skip",t[1].dataType,t[1].dims,m),tA("gamma",t[2].dataType,t[2].dims,m)];d&&i.push(tA("beta",t[3].dataType,t[3].dims,m)),p&&i.push(tA("bias",t[4].dataType,t[4].dims,m)),i.push(tO("output",t[0].dataType,s,m)),c&&i.push(tO("mean_output",1,l)),h&&i.push(tO("inv_std_output",1,l)),f&&i.push(tO("input_skip_bias_sum",t[0].dataType,s,m));let a=tw(t[0].dataType),o=tw(1,m);return`
2552
2553      ${r.registerUniforms([{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}]).declareVariables(...i)}
2554      var<workgroup> sum_shared : array<${o}, 64>;
2555      var<workgroup> sum_squared_shared : array<${o}, 64>;
2556
2557      ${r.mainStart([64,1,1])}
2558        let ix = local_id.x;
2559        let iy = global_id.x / 64;
2560
2561        let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components;
2562        var stride = hidden_size_vectorized / 64;
2563        let offset = ix * stride + iy * hidden_size_vectorized;
2564        let offset1d = stride * ix;
2565        if (ix == 63) {
2566          stride = hidden_size_vectorized - stride * ix;
2567        }
2568        for (var i: u32 = 0; i < stride; i++) {
2569          let skip_value = skip[offset + i];
2570          let bias_value = ${p?"bias[offset1d + i]":a+"(0.0)"};
2571          let input_value = x[offset + i];
2572          let value = input_value + skip_value + bias_value;
2573          ${f?"input_skip_bias_sum[offset + i] = value;":""}
2574          output[offset + i] = value;
2575          let f32_value = ${tC(a,m,"value")};
2576          sum_shared[ix] += f32_value;
2577          sum_squared_shared[ix] += f32_value * f32_value;
2578        }
2579        workgroupBarrier();
2580
2581        var reduce_size : u32 = 64;
2582        for (var curr_size = reduce_size >> 1;  curr_size > 0; curr_size = reduce_size >> 1) {
2583          reduce_size = curr_size + (reduce_size & 1);
2584          if (ix < curr_size) {
2585            sum_shared[ix] += sum_shared[ix + reduce_size];
2586            sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size];
2587          }
2588          workgroupBarrier();
2589        }
2590
2591        let sum = sum_shared[0];
2592        let square_sum = sum_squared_shared[0];
2593        let mean = ${tI("sum",m)} / f32(uniforms.hidden_size);
2594        let inv_std_dev = inverseSqrt(${tI("square_sum",m)} / f32(uniforms.hidden_size) ${n?"":"- mean * mean"} + uniforms.epsilon);
2595        ${c?"mean_output[global_idx] = mean;":""}
2596        ${h?"inv_std_output[global_idx] = inv_std_dev;":""}
2597
2598        for (var i: u32 = 0; i < stride; i++) {
2599          output[offset + i] = (output[offset + i] ${n?"":`- ${a}(mean)`}) *
2600            ${a}(inv_std_dev) * gamma[offset1d + i]
2601            ${d?"+ beta[offset1d + i]":""};
2602        }
2603      }`},getRunData:()=>({outputs:y,dispatchGroup:{x:Math.ceil(o/u)},programUniforms:g})}},s7=(t,r)=>{s5(t.inputs);let i=[0];t.outputCount>1&&i.push(-3),t.outputCount>2&&i.push(-3),t.outputCount>3&&i.push(3),t.compute(s9(t.inputs,r,t.outputCount,!1),{outputs:i})}}),uD=W(()=>{oG(),oX(),oZ(),oY(),oe=(t,r)=>{if(!t||t.length<1)throw Error("too few inputs");if(0!==r.axes.length){if(r.axes.length!==r.starts.length||r.axes.length!==r.ends.length)throw Error("axes, starts and ends must have the same length")}else if(r.starts.length!==r.ends.length)throw Error("starts and ends must have the same length");
2603t.slice(1).forEach((r,i)=>{if(6!==t[i+1].dataType&&7!==t[i+1].dataType)throw Error(`Input ${i} must be an array of int32 or int64`)})},ot=(t,r)=>{let i=[];if(t.length>r){if(7===t[r].dataType)t[r].getBigInt64Array().forEach(t=>i.push(Number(t)));else if(6===t[r].dataType)t[r].getInt32Array().forEach(t=>i.push(Number(t)));else throw Error(`Input ${r} must be an array of int32 or int64`)}return i},or=(t,r)=>{if(!(t.length>1))return r;{let r=ot(t,1),i=ot(t,2),a=ot(t,3);return 0===a.length&&(a=[...Array(t[0].dims.length).keys()]),tc({starts:r,ends:i,axes:a})}},oi=(t,r,i,a,n)=>{let s=t;return t<0&&(s+=i[a[r]]),n[r]<0?Math.max(0,Math.min(s,i[a[r]]-1)):Math.max(0,Math.min(s,i[a[r]]))},oa=(t,r,i)=>`fn calculateInputIndices(output_indices: ${r.type.indices}) -> ${t.type.indices} {
2604          var input_indices: ${t.type.indices};
2605          var carry = 0u;
2606          for (var i = ${i.length}; i >= 0; i--) {
2607            let input_shape_i = ${tE("uniforms.input_shape","i",i.length)};
2608            let steps_i = ${tE("uniforms.steps","i",i.length)};
2609            let signs_i = ${tE("uniforms.signs","i",i.length)};
2610            let starts_i = ${tE("uniforms.starts","i",i.length)};
2611            var output_index = ${r.indicesGet("output_indices","i")};
2612            var input_index = output_index * steps_i + starts_i + carry;
2613            carry = input_index / input_shape_i;
2614            input_index = input_index % input_shape_i;
2615            if (signs_i < 0) {
2616              input_index = input_shape_i - input_index - 1u + starts_i;
2617            }
2618            ${t.indicesSet("input_indices","i","input_index")};
2619          }
2620          return input_indices;
2621      }`,on=(t,r)=>{let i=t[0].dims,a=tm.size(i),n=r.axes.length>0?tm.normalizeAxes(r.axes,i.length):[...Array(i.length).keys()],s=ot(t,4);s.forEach(t=>0!==t||(()=>{throw Error("step cannot be 0")})),0===s.length&&(s=Array(n.length).fill(1));let o=r.starts.map((t,r)=>oi(t,r,i,n,s)),u=r.ends.map((t,r)=>oi(t,r,i,n,s));if(n.length!==o.length||n.length!==u.length)throw Error("start, ends and axes should have the same number of elements");if(n.length!==i.length)for(let t=0;t<i.length;++t)n.includes(t)||(o.splice(t,0,0),u.splice(t,0,i[t]),s.splice(t,0,1));let l=s.map(t=>Math.sign(t));s.forEach((t,r,i)=>{if(t<0){let a=(u[r]-o[r])/t,n=o[r],l=n+a*s[r];o[r]=l,u[r]=n,i[r]=-t}});let d=i.slice(0);n.forEach((t,r)=>{d[t]=Math.ceil((u[t]-o[t])/s[t])});let p={dims:d,dataType:t[0].dataType},c=tO("output",t[0].dataType,d.length),h=tA("input",t[0].dataType,t[0].dims.length),f=tm.size(d),m=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:o.length},{name:"signs",type:"i32",length:l.length},{name:"steps",type:"u32",length:s.length}],g=[{type:12,data:f},{type:12,data:o},{type:6,data:l},{type:12,data:s},...tS(t[0].dims,d)];return{name:"Slice",shaderCache:{hint:`${l.length}_${o.length}_${s.length}`,inputDependencies:["rank"]},getShaderSource:t=>`
2622      ${t.registerUniforms(m).declareVariables(h,c)}
2623        ${oa(h,c,i)}
2624        ${t.mainStart()}
2625          ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
2626          let output_indices = ${c.offsetToIndices("global_idx")};
2627          let input_indices = calculateInputIndices(output_indices);
2628          ${c.setByOffset("global_idx",h.getByIndices("input_indices"))}
2629      }`,getRunData:()=>({outputs:[p],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:g})}},os=(t,r)=>{oe(t.inputs,r);let i=or(t.inputs,r);t.compute(on(t.inputs,i),{inputs:[0]})},oo=t=>{let r=t.starts,i=t.ends,a=t.axes;return tc({starts:r,ends:i,axes:a})}}),uP=W(()=>{oG(),oX(),oZ(),oJ(),oY(),ou=t=>{if(!t||1!==t.length)throw Error("Softmax op requires 1 input.")},ol=(t,r)=>{let i=t.inputs[0],a=i.dims,n=tm.size(a),s=a.length,o=tm.normalizeAxis(r.axis,s),u=o<a.length-1,l,d=[];u?((d=Array.from({length:s},(t,r)=>r))[o]=s-1,d[s-1]=o,l=t.compute(tV(i,d),{inputs:[i],outputs:[-1]})[0]):l=i;let p=l.dims,c=p[s-1],h=n/c,f=tk(c),m=c/f,g=64;1===h&&(g=256);let y=(t,r)=>4===r?`max(max(${t}.x, ${t}.y), max(${t}.z, ${t}.w))`:2===r?`max(${t}.x, ${t}.y)`:3===r?`max(max(${t}.x, ${t}.y), ${t}.z)`:t,_=tA("x",l.dataType,l.dims,f),$=tO("result",l.dataType,l.dims,f),b=_.type.value,v="f32"===tw(l.dataType)?`var threadMax = ${b}(-3.402823e+38f);`:`var threadMax = ${b}(-65504.0h);`,w=t.compute({name:"Softmax",shaderCache:{hint:`${f};${g}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:p,dataType:l.dataType}],dispatchGroup:{x:h},programUniforms:[{type:6,data:m}]}),getShaderSource:t=>`
2630      var<workgroup> rowMaxShared : ${b};
2631      var<workgroup> rowSumShared : ${b};
2632      var<workgroup> threadShared : array<${b}, ${g}>;
2633
2634      fn getValue(row: i32, col: i32, row_stride: i32) -> ${b} {
2635        let index = row * row_stride + col;
2636        return x[index];
2637      }
2638
2639      fn setValue(row: i32, col: i32, row_stride: i32, value: ${b}) {
2640        let index = row * row_stride + col;
2641        result[index] = value;
2642      }
2643      ${t.registerUniform("packedCols","i32").declareVariables(_,$)}
2644      ${t.mainStart(g)}
2645        let gindex = i32(global_idx);
2646        let lindex = i32(local_idx);
2647        const wg = ${g};
2648        let row = gindex / wg;
2649        let cols = uniforms.packedCols;
2650        let row_stride : i32 = uniforms.packedCols;
2651
2652        // find the rows max
2653        ${v}
2654        for (var col = lindex; col < cols; col += wg) {
2655          let value = getValue(row, col, row_stride);
2656          threadMax = max(threadMax, value);
2657        }
2658        if (lindex < cols) {
2659          threadShared[lindex] = threadMax;
2660        }
2661        workgroupBarrier();
2662
2663        var reduceSize = min(cols, wg);
2664        for (var currSize = reduceSize >> 1;  currSize > 0; currSize = reduceSize >> 1) {
2665          reduceSize = currSize + (reduceSize & 1);
2666          if (lindex < currSize) {
2667            threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]);
2668          }
2669          workgroupBarrier();
2670        }
2671        if (lindex == 0) {
2672          rowMaxShared = ${b}(${y("threadShared[0]",f)});
2673        }
2674        workgroupBarrier();
2675
2676        // find the rows sum
2677        var threadSum = ${b}(0.0);
2678        for (var col = lindex; col < cols; col += wg) {
2679          let subExp = exp(getValue(row, col, row_stride) - rowMaxShared);
2680          threadSum += subExp;
2681        }
2682        threadShared[lindex] = threadSum;
2683        workgroupBarrier();
2684
2685        for (var currSize = wg >> 1;  currSize > 0; currSize = currSize >> 1) {
2686          if (lindex < currSize) {
2687            threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize];
2688          }
2689          workgroupBarrier();
2690        }
2691        if (lindex == 0) {
2692          rowSumShared = ${b}(${tI("threadShared[0]",f)});
2693        }
2694        workgroupBarrier();
2695
2696        // calculate final value for each element in the row
2697        for (var col = lindex; col < cols; col += wg) {
2698          let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared;
2699          setValue(row, col, row_stride, value);
2700        }
2701      }`},{inputs:[l],outputs:[u?-1:0]})[0];u&&t.compute(tV(w,d),{inputs:[w]})},od=(t,r)=>{ou(t.inputs),ol(t,r)},op=t=>tc({axis:t.axis})}),uU=W(()=>{oG(),oX(),oY(),oc=t=>Array.from(t.getBigInt64Array(),Number),oh=t=>{if(!t||2!==t.length)throw Error("Tile requires 2 inputs.");if(1!==t[0].dataType&&10!==t[0].dataType&&6!==t[0].dataType&&12!==t[0].dataType)throw Error("Tile only support float, float16, int32, and uint32 data types");if(7!==t[1].dataType)throw Error("Tile `repeats` input should be of int64 data type");if(1!==t[1].dims.length)throw Error("Tile `repeats` input should be 1-D");if(oc(t[1]).length!==t[0].dims.length)throw Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},of=(t,r)=>{let i=[];for(let a=0;a<t.length;++a)i.push(t[a]*r[a]);return i},om=(t,r)=>{let i=t[0].dims,a=r??oc(t[1]),n=of(i,a),s=tm.size(n),o=t[0].dataType,u=tA("input",o,i.length),l=tO("output",o,n.length);return{name:"Tile",shaderCache:{hint:`${a}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:n,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:[{type:12,data:s},...tS(t[0].dims,n)]}),getShaderSource:t=>`
2702      const inputShape = ${u.indices(...i)};
2703      ${t.registerUniform("output_size","u32").declareVariables(u,l)}
2704      ${t.mainStart()}
2705      ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
2706      let output_indices = ${l.offsetToIndices("global_idx")};
2707      var input_indices: ${u.type.indices};
2708      for (var i = 0; i < ${i.length}; i++) {
2709        let input_dim_i = ${u.indicesGet("uniforms.input_shape","i")};
2710        let input_dim_value = ${l.indicesGet("output_indices","i")}  % input_dim_i;
2711
2712        ${u.indicesSet("input_indices","i","input_dim_value")}
2713      }
2714      ${l.setByOffset("global_idx",u.getByIndices("input_indices"))}
2715    }`}},og=t=>{oh(t.inputs),t.compute(om(t.inputs),{inputs:[0]})}}),uW=W(()=>{oG(),oX(),oY(),oy=(t,r,i,a,n)=>{let s=tO("output_data",n,i.length,4),o=tA("a_data",r[1].dataType,r[1].dims.length,4),u=tA("b_data",r[2].dataType,r[2].dims.length,4),l=tA("c_data",r[0].dataType,r[0].dims.length,4),d,p=(t,r,i)=>`select(${r}, ${t}, ${i})`;if(a){let t=(t,r,i="")=>{let a=`a_data[index_a${r}][component_a${r}]`,n=`b_data[index_b${r}][component_b${r}]`,d=`bool(c_data[index_c${r}] & (0xffu << (component_c${r} * 8)))`;return`
2716            let output_indices${r} = ${s.offsetToIndices(`global_idx * 4u + ${r}u`)};
2717            let offset_a${r} = ${o.broadcastedIndicesToOffset(`output_indices${r}`,s)};
2718            let offset_b${r} = ${u.broadcastedIndicesToOffset(`output_indices${r}`,s)};
2719            let offset_c${r} = ${l.broadcastedIndicesToOffset(`output_indices${r}`,s)};
2720            let index_a${r} = offset_a${r} / 4u;
2721            let index_b${r} = offset_b${r} / 4u;
2722            let index_c${r} = offset_c${r} / 4u;
2723            let component_a${r} = offset_a${r} % 4u;
2724            let component_b${r} = offset_b${r} % 4u;
2725            let component_c${r} = offset_c${r} % 4u;
2726            ${t}[${r}] = ${i}(${p(a,n,d)});
2727          `};d=9===n?`
2728            var data = vec4<u32>(0);
2729            ${t("data",0,"u32")}
2730            ${t("data",1,"u32")}
2731            ${t("data",2,"u32")}
2732            ${t("data",3,"u32")}
2733            output_data[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:`
2734            ${t("output_data[global_idx]",0)}
2735            ${t("output_data[global_idx]",1)}
2736            ${t("output_data[global_idx]",2)}
2737            ${t("output_data[global_idx]",3)}
2738          `}else d=s.setByOffset("global_idx",p(o.getByOffset("global_idx"),u.getByOffset("global_idx"),l.getByOffset("global_idx")));return`
2739        ${t.registerUniform("vec_size","u32").declareVariables(l,o,u,s)}
2740        ${t.mainStart()}
2741        ${t.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
2742        ${d}
2743      }`},o_=t=>{let r=t[1].dims,i=t[2].dims,a=t[0].dims,n=t[1].dataType,s=!(tm.areEqual(r,i)&&tm.areEqual(i,a)),o=r,u=tm.size(r);if(s){let t=tf.calcShape(tf.calcShape(r,i,!1),a,!1);if(!t)throw Error("Can't perform where op on the given tensors");o=t,u=tm.size(o)}let l=Math.ceil(u/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:r=>oy(r,t,o,s,n),getRunData:()=>({outputs:[{dims:o,dataType:n}],dispatchGroup:{x:Math.ceil(u/64/4)},programUniforms:[{type:12,data:l},...tS(a,r,i,o)]})}},o$=t=>{t.compute(o_(t.inputs))}}),uq=W(()=>{o2(),o3(),o4(),o6(),o5(),o9(),o7(),uu(),ud(),up(),uc(),uh(),uf(),um(),ug(),uy(),u_(),u$(),ub(),uv(),uS(),uk(),uT(),uC(),uI(),uw(),uE(),uz(),uA(),uO(),uR(),o1(),uB(),uN(),uM(),uD(),uP(),ux(),uU(),oJ(),o8(),uW(),ob=new Map([["Abs",[rZ]],["Acos",[rX]],["Acosh",[rY]],["Add",[iD]],["ArgMax",[rA,rO]],["ArgMin",[rz,rO]],["Asin",[rJ]],["Asinh",[r0]],["Atan",[r1]],["Atanh",[r2]],["Attention",[rW]],["AveragePool",[s_,sy]],["BatchNormalization",[rG]],["BiasAdd",[rj]],["BiasSplitGelu",[iR]],["Cast",[r4,r3]],["Ceil",[r5]],["Clip",[r8]],["Concat",[iX,iY]],["Conv",[aT,aw]],["ConvTranspose",[aM,aO]],["Cos",[r9]],["Cosh",[r7]],["CumSum",[aP,aU]],["DepthToSpace",[aV,aG]],["DequantizeLinear",[sE,sz]],["Div",[iP]],["Einsum",[aJ,a0]],["Elu",[it,ie]],["Equal",[iU]],["Erf",[ii]],["Exp",[ia]],["Expand",[a6]],["FastGelu",[a5]],["Floor",[is]],["FusedConv",[aT,aw]],["Gather",[nt,ne]],["GatherElements",[nc,np]],["GatherBlockQuantized",[no,nu]],["GatherND",[ni,na]],["Gelu",[io]],["Gemm",[ng,nm]],["GlobalAveragePool",[sv,sb]],["GlobalMaxPool",[sT,sk]],["Greater",[iV]],["GreaterOrEqual",[iH]],["GridSample",[nE,nz]],["GroupQueryAttention",[nK]],["HardSigmoid",[ig,im]],["InstanceNormalization",[nY]],["LayerNormalization",[n1]],["LeakyRelu",[iu,ie]],["Less",[iG]],["LessOrEqual",[iF]],["Log",[iC]],["MatMul",[n3]],["MatMulNBits",[n5,n9]],["MaxPool",[sx,sS]],["Mul",[iW]],["MultiHeadAttention",[nD,nR]],["Neg",[id]],["Not",[il]],["Pad",[so]],["Pow",[iq]],["QuickGelu",[iz,ie]],["Range",[sR]],["Reciprocal",[ip]],["ReduceMin",[rS]],["ReduceMean",[r$]],["ReduceMax",[rx]],["ReduceSum",[rT]],["ReduceProd",[rk]],["ReduceL1",[rb]],["ReduceL2",[rv]],["ReduceLogSum",[rI]],["ReduceLogSumExp",[rw]],["ReduceSumSquare",[rC]],["Relu",[ic]],["Resize",[s2,s3]],["RotaryEmbedding",[s8]],["ScatterND",[sD,sM]],["Sigmoid",[ih]],["Sin",[iy]],["Sinh",[i_]],["Slice",[os,oo]],["SkipLayerNormalization",[s7]],["Split",[nV,nG]],["Sqrt",[i$]],["Softmax",[od,op]],["Sub",[iL]],["Tan",[ib]],["Tanh",[iw]],["ThresholdedRelu",[iT,ie]],["Tile",[og]],["Transpose",[tG,tH]],["Where",[o$]]])}),uL=W(()=>{el(),oF(),oY(),ov=class{constructor(t){this.backend=t,this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,i,a,n){A(t.programInfo.name);let s=this.backend.device,o=this.backend.getComputePassEncoder();this.backend.writeTimestamp(2*this.backend.pendingDispatchNumber);let u=[];for(let t of r)u.push({binding:u.length,resource:{buffer:t.buffer}});for(let t of i)u.push({binding:u.length,resource:{buffer:t.buffer}});n&&u.push({binding:u.length,resource:n});let l=s.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:u,label:t.programInfo.name});if("capturing"===this.backend.sessionStatus){let r={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:l,dispatchGroup:a};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(r)}o.setPipeline(t.computePipeline),o.setBindGroup(0,l),o.dispatchWorkgroups(...a),this.backend.writeTimestamp(2*this.backend.pendingDispatchNumber+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>
2743=this.backend.maxDispatchNumber||"at-passes"===this.backend.queryType)&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),O(t.programInfo.name)}dispose(){}build(t,r){A(t.name);let i=this.backend.device,a=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"},{feature:"subgroups-f16",extension:"subgroups_f16"}].forEach(t=>{i.features.has(t.feature)&&a.push(`enable ${t.extension};`)});let n=tM(r,this.backend.device.limits),s=t.getShaderSource(n),o=`${a.join(`
2744`)}
2745${n.additionalImplementations}
2746${s}`,u=i.createShaderModule({code:o,label:t.name});te("verbose",()=>`[WebGPU] ${t.name} shader code: ${o}`);let l=i.createComputePipeline({compute:{module:u,entryPoint:"main"},layout:"auto",label:t.name});return O(t.name),{programInfo:t,computePipeline:l,uniformVariablesInfo:n.variablesInfo}}normalizeDispatchGroupSize(t){let r="number"==typeof t?t:t.x,i="number"==typeof t?1:t.y||1,a="number"==typeof t?1:t.z||1,n=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=n&&i<=n&&a<=n)return[r,i,a];let s=r*i*a,o=Math.ceil(Math.sqrt(s));if(!(o>n))return[o,o,1];if((o=Math.ceil(Math.cbrt(s)))>n)throw Error("Total dispatch size exceeds WebGPU maximum.");return[o,o,o]}}}),uV=W(()=>{el(),oG(),oF(),oj(),oQ(),uq(),uL(),ow=(t,r)=>{if(r.length!==t.length)throw Error(`inputDependencies length ${r.length} is not equal to inputTensors length ${t.length}.`);let i=[];for(let a=0;a<t.length;++a){let n=t[a].dataType;switch(r[a]){case"none":i.push("");break;case"type":i.push(`${n}`);break;case"rank":{let r=t[a].dims.length;i.push(`${n};${r}`);break}case"dims":{let r=t[a].dims.join(",");i.push(`${n};${r}`);break}default:throw Error(`unsupported input dependency: ${r[a]}`)}}return i.join("|")},ox=(t,r,i)=>{let a=t.name;return t.shaderCache?.hint&&(a+="["+t.shaderCache.hint+"]"),a+=":"+i+`:${ow(r,t.shaderCache?.inputDependencies??Array(r.length).fill("dims"))}`},oS=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},ok=class{constructor(t){this.subgroupsSupported=t.features.has("subgroups"),this.subgroupsF16Supported=t.features.has("subgroups");let r=t.limits;this.subgroupsSupported&&r.minSubgroupSize&&r.maxSubgroupSize?this.subgroupSizeRange=[r.minSubgroupSize,r.maxSubgroupSize]:this.subgroupSizeRange=void 0}},oT=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(null===this.currentKernelId)throw Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let i=[],a={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:i},n=t=>r.features.has(t)&&i.push(t)&&!0;n("chromium-experimental-timestamp-query-inside-passes")||n("timestamp-query"),n("shader-f16"),n("subgroups")&&n("subgroups-f16"),this.device=await r.requestDevice(a),this.deviceInfo=new ok(this.device),this.adapterInfo=new oS(r.info||await r.requestAdapterInfo()),this.gpuDataManager=td(this),this.programManager=new ov(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,e9(t.logLevel,!!t.debug),this.device.onuncapturederror=t=>
2746{t.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${t.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){"u">typeof this.querySet&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};"at-passes"===this.queryType&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:2*this.pendingDispatchNumber,endOfPassWriteIndex:2*this.pendingDispatchNumber+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){let t;this.commandEncoder&&(A(),this.endComputePass(),"none"!==this.queryType&&(this.commandEncoder.resolveQuerySet(this.querySet,0,2*this.pendingDispatchNumber,this.queryResolveBuffer,0),t=this.device.createBuffer({size:16*this.pendingDispatchNumber,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,16*this.pendingDispatchNumber)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,"none"!==this.queryType&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),i=this.pendingQueries.get(t);for(let t=0;t<r.length/2;t++){let a=i[t],n=a.kernelId,s=this.kernels.get(n),o=s.kernelType,u=s.kernelName,l=a.programName,d=a.inputTensorViews,p=a.outputTensorViews,c=r[2*t],h=r[2*t+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=c);let f=Number(c-this.queryTimeBase),m=Number(h-this.queryTimeBase);if(!Number.isSafeInteger(f)||!Number.isSafeInteger(m))throw RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:d.map(t=>({dims:t.dims,dataType:eZ(t.dataType)})),outputsMetadata:p.map(t=>({dims:t.dims,dataType:eZ(t.dataType)})),kernelId:n,kernelType:o,kernelName:u,programName:l,startTime:f,endTime:m});else{let t="";d.forEach((r,i)=>{t+=`input[${i}]: [${r.dims}] | ${eZ(r.dataType)}, `});let r="";p.forEach((t,i)=>{r+=`output[${i}]: [${t.dims}] | ${eZ(t.dataType)}, `}),console.log(`[profiling] kernel "${n}|${o}|${u}|${l}" ${t}${r}execution time: ${m-f} ns`)}E("GPU",`${l}::${c}::${h}`)}t.unmap(),this.pendingQueries.delete(t)}),O())}run(t,r,i,a,n,s){let o;A(t.name);let u=[];for(let t=0;t<r.length;++t){let i=r[t].data;if(0===i)continue;let a=this.gpuDataManager.get(i);
2746if(!a)throw Error(`no GPU data for input: ${i}`);u.push(a)}let{outputs:l,dispatchGroup:d,programUniforms:p}=t.getRunData(r),c=0===i.length?l.map((t,r)=>r):i;if(c.length!==l.length)throw Error(`Output size ${c.length} must be equal to ${l.length}.`);let h=[],f=[];for(let t=0;t<l.length;++t){if(!Number.isInteger(c[t])||c[t]<-3||c[t]>=s)throw Error(`Invalid output index: ${c[t]}`);if(-3===c[t])continue;let r=-1===c[t],i=-2===c[t],o=r||i?n(l[t].dataType,l[t].dims):a(c[t],l[t].dataType,l[t].dims);if(h.push(o),0===o.data)continue;let u=this.gpuDataManager.get(o.data);if(!u)throw Error(`no GPU data for output: ${o.data}`);if(r&&this.temporaryData.push(u),i){let t=this.kernelPersistentData.get(this.currentKernelId);t||(t=[],this.kernelPersistentData.set(this.currentKernelId,t)),t.push(u)}f.push(u)}if(u.length!==r.length||f.length!==h.length){if(0===f.length)return O(t.name),h;throw Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}if(p){let t=0,r=[];p.forEach(i=>{let a="number"==typeof i.data?[i.data]:i.data;if(0===a.length)return;let n=10===i.type?2:4,s,o;10===i.type?(o=a.length>4?16:a.length>2?8:a.length*n,s=a.length>4?16:n*a.length):(o=a.length<=2?a.length*n:16,s=16),t=Math.ceil(t/o)*o,r.push(t);let u=10===i.type?8:4;t+=a.length>4?Math.ceil(a.length/u)*s:a.length*n});let i=new ArrayBuffer(t=16*Math.ceil(t/16));p.forEach((t,a)=>{let n=r[a],s="number"==typeof t.data?[t.data]:t.data;if(6===t.type)new Int32Array(i,n,s.length).set(s);else if(12===t.type)new Uint32Array(i,n,s.length).set(s);else if(10===t.type)new Uint16Array(i,n,s.length).set(s);else if(1===t.type)new Float32Array(i,n,s.length).set(s);else throw Error(`Unsupported uniform type: ${eZ(t.type)}`)});let a=this.gpuDataManager.create(t,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(a.buffer,0,i,0,t),this.gpuDataManager.release(a.id),o={offset:0,size:t,buffer:a.buffer}}let m=this.programManager.normalizeDispatchGroupSize(d),g=ox(t,r,1===m[1]&&1===m[2]),y=this.programManager.getArtifact(g);if(y||(y=this.programManager.build(t,m),this.programManager.setArtifact(g,y),te("info",()=>`[artifact] key: ${g}, programName: ${t.name}`)),p&&y.uniformVariablesInfo){if(p.length!==y.uniformVariablesInfo.length)throw Error(`Uniform variables count mismatch: expect ${y.uniformVariablesInfo.length}, got ${p.length} in program "${y.programInfo.name}".`);for(let t=0;t<p.length;t++){let r=p[t],i=r.type,a="number"==typeof r.data?1:r.data.length,[n,s]=y.uniformVariablesInfo[t];if(i!==n||a!==s)throw Error(`Uniform variable ${t} mismatch: expect type ${n} with size ${s}, got type ${i} with size ${a} in program "${y.programInfo.name}".`)}}if(te("info",()=>`[ProgramManager] run "${t.name}" (key=${g}) with ${m[0]}x${m[1]}x${m[2]}`),"none"!==this.queryType||"capturing"===this.sessionStatus){let t={kernelId:this.currentKernelId,programName:y.programInfo.name,inputTensorViews:r,outputTensorViews:h};this.pendingKernels.push(t),"capturing"===this.sessionStatus&&this.capturedPendingKernels.get(this.currentSessionId).push(t)}return this.programManager.run(y,u,f,m,o),O(t.name),h}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,i,a){let n=ob.get(t);if(!n)throw Error(`kernel not implemented: ${t}`);let s={kernelType:t,kernelName:a,kernelEntry:n[0],attributes:[n[1],i]};this.kernels.set(r,s)}releaseKernel(t){let r=this.kernelPersistentData.get(t);
2746if(r){for(let t of r)this.gpuDataManager.release(t.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,i){let a=this.kernels.get(t);if(!a)throw Error(`kernel not created: ${t}`);let n=a.kernelType,s=a.kernelName,o=a.kernelEntry,u=a.attributes;if(null!==this.currentKernelId)throw Error(`kernel "[${n}] ${s}" is not allowed to be called recursively`);this.currentKernelId=t,u[0]&&(u[1]=u[0](u[1]),u[0]=void 0),te("info",()=>`[WebGPU] Start to run kernel "[${n}] ${s}"...`);let l=this.env.debug;this.temporaryData=[];try{return l&&this.device.pushErrorScope("validation"),o(r,u[1]),0}catch(t){return i.push(Promise.resolve(`[WebGPU] Kernel "[${n}] ${s}" failed. ${t}`)),1}finally{for(let t of(l&&i.push(this.device.popErrorScope().then(t=>t?`GPU validation error for kernel "[${n}] ${s}": ${t.message}`:null)),this.temporaryData))this.gpuDataManager.release(t.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,i,a){let n=this.sessionExternalDataMapping.get(t);n||(n=new Map,this.sessionExternalDataMapping.set(t,n));let s=n.get(r),o=this.gpuDataManager.registerExternalBuffer(i,a,s);return n.set(r,[o,i]),o}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(t=>this.gpuDataManager.unregisterExternalBuffer(t[0])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,i){return async()=>{let a=await tu(this,t,r);return tt(a.buffer,i)}}writeTimestamp(t){"inside-passes"===this.queryType&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),"none"!==this.queryType&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:2*this.maxDispatchNumber}),this.queryResolveBuffer=this.device.createBuffer({size:16*this.maxDispatchNumber,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){te("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(){te("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){te("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),i=t.length;this.pendingKernels=[];for(let a=0;a<i;a++){let i=this.getComputePassEncoder(),n=t[a];this.writeTimestamp(2*this.pendingDispatchNumber),i.setPipeline(n.computePipeline),i.setBindGroup(0,n.bindGroup),i.dispatchWorkgroups(...n.dispatchGroup),this.writeTimestamp(2*this.pendingDispatchNumber+1),this.pendingDispatchNumber++,"none"!==this.queryType&&this.pendingKernels.push(r[a]),(this.pendingDispatchNumber>=this.maxDispatchNumber||"at-passes"===this.queryType)&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}}),uG=W(()=>{oF(),oC=1,oI=()=>oC++,oE=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),oz=(t,r)=>{let i=oE.get(t);if(!i)throw Error("Unsupported data type.");return r.length>0?Math.ceil(r.reduce((t,r)=>t*r)*i/8):0},oA=class{constructor(t){this.sessionId=t.sessionId,this.mlContext=t.context,this.mlTensor=t.tensor,this.dataType=t.dataType,this.tensorShape=t.shape}get tensor(){return this.mlTensor}get type(){return this.dataType}get shape(){return this.tensorShape}get byteLength(){return oz(this.dataType,this.tensorShape)}destroy(){te("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(t){this.mlContext.writeTensor(this.mlTensor,t)}async read(t){return t?this.mlContext.readTensor(this.mlTensor,t):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(t,r,i){return this.mlContext===t&&this.dataType===r&&this.tensorShape.length===i.length&&this.tensorShape.every((t,r)=>t===i[r])}},oO=class{constructor(t,r){this.tensorManager=t,this.wrapper=r}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(t,r,i,a){let n=this.tensorManager.getMLContext(t);if(this.wrapper){if(this.wrapper.canReuseTensor(n,r,i))return this.wrapper.tensor;if(a){if(this.wrapper.byteLength!==oz(r,i))throw Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let s=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(t,r,i,s,!0,!0),a&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(t){if(this.wrapper){if(t.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}te("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(t){if(this.activeUpload)return t?(t instanceof ArrayBuffer?new Uint8Array(t).set(this.activeUpload):new Uint8Array(t.buffer,t.byteOffset,t.byteLength).set(this.activeUpload),void 0):this.activeUpload.buffer;if(!this.wrapper)throw Error("Tensor has not been created.");return t?this.wrapper.read(t):this.wrapper.read()}},oR=class{constructor(t){this.backend=t,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(t){let r=this.backend.getMLContext(t);if(!r)throw Error("MLContext not found for session.");return r}
2746reserveTensorId(){let t=oI();return this.tensorTrackersById.set(t,new oO(this)),t}releaseTensorId(t){let r=this.tensorTrackersById.get(t);r&&(this.tensorTrackersById.delete(t),r.tensorWrapper&&this.releaseTensor(r.tensorWrapper))}async ensureTensor(t,r,i,a,n){te("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${r}, dataType: ${i}, shape: ${a}, copyOld: ${n}}`);let s=this.tensorTrackersById.get(r);if(!s)throw Error("Tensor not found.");return s.ensureTensor(t,i,a,n)}upload(t,r){let i=this.tensorTrackersById.get(t);if(!i)throw Error("Tensor not found.");i.upload(r)}async download(t,r){te("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${t}, dstBuffer: ${r?.byteLength}}`);let i=this.tensorTrackersById.get(t);if(!i)throw Error("Tensor not found.");return i.download(r)}releaseTensorsForSession(t){for(let r of this.freeTensors)r.sessionId===t&&r.destroy();this.freeTensors=this.freeTensors.filter(r=>r.sessionId!==t)}registerTensor(t,r,i,a){let n=this.getMLContext(t),s=oI(),o=new oA({sessionId:t,context:n,tensor:r,dataType:i,shape:a});return this.tensorTrackersById.set(s,new oO(this,o)),this.externalTensors.add(o),s}async getCachedTensor(t,r,i,a,n,s){let o=this.getMLContext(t);for(let[a,n]of this.freeTensors.entries())if(n.canReuseTensor(o,r,i)){te("verbose",()=>`[WebNN] Reusing tensor {dataType: ${r}, shape: ${i}}`);let n=this.freeTensors.splice(a,1)[0];return n.sessionId=t,n}te("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${r}, shape: ${i}}`);let u=await o.createTensor({dataType:r,shape:i,dimensions:i,usage:a,writable:n,readable:s});return new oA({sessionId:t,context:o,tensor:u,dataType:r,shape:i})}releaseTensor(t){this.externalTensors.has(t)&&this.externalTensors.delete(t),this.freeTensors.push(t)}},oB=(...t)=>new oR(...t)}),uH=W(()=>{oG(),oW(),oj(),uG(),oF(),oN=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),oM=(t,r)=>{if(t===r)return!0;if(void 0===t||void 0===r)return!1;let i=Object.keys(t).sort(),a=Object.keys(r).sort();return i.length===a.length&&i.every((i,n)=>i===a[n]&&t[i]===r[i])},oD=class{constructor(t){this.tensorManager=oB(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.temporaryGraphInputs=[],this.temporarySessionTensorIds=new Map,e9(t.logLevel,!!t.debug)}get currentSessionId(){if(void 0===this.activeSessionId)throw Error("No active session");return this.activeSessionId}onRunStart(t){te("verbose",()=>`[WebNN] onRunStart {sessionId: ${t}}`),this.activeSessionId=t}onRunEnd(t){te("verbose",()=>`[WebNN] onRunEnd {sessionId: ${t}}`);let r=this.temporarySessionTensorIds.get(t);if(r){for(let t of r)te("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${t}}`),this.tensorManager.releaseTensorId(t);this.temporarySessionTensorIds.delete(t),this.activeSessionId=void 0}}async createMLContext(t){if(t instanceof GPUDevice){let r=this.mlContextCache.findIndex(r=>r.gpuDevice===t);if(-1!==r)return this.mlContextCache[r].mlContext;{let r=await navigator.ml.createContext(t);return this.mlContextCache.push({gpuDevice:t,mlContext:r}),r}}if(void 0===t){let t=this.mlContextCache.findIndex(t=>void 0===t.options&&void 0===t.gpuDevice);if(-1!==t)return this.mlContextCache[t].mlContext;{let t=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:t}),t}}let r=this.mlContextCache.findIndex(r=>oM(r.options,t));if(-1!==r)return this.mlContextCache[r].mlContext;{let r=await navigator.ml.createContext(t);return this.mlContextCache.push({options:t,mlContext:r}),r}}registerMLContext(t,r){this.mlContextBySessionId.set(t,r);let i=this.sessionIdsByMLContext.get(r);i||(i=new Set,this.sessionIdsByMLContext.set(r,i)),i.add(t),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(t,this.temporaryGraphInputs),this.temporaryGraphInputs=[])}onReleaseSession(t){this.sessionGraphInputs.delete(t);let r=this.mlContextBySessionId.get(t);if(!r)return;this.tensorManager.releaseTensorsForSession(t),this.mlContextBySessionId.delete(t);let i=this.sessionIdsByMLContext.get(r);if(i.delete(t),0===i.size){this.sessionIdsByMLContext.delete(r);let t=this.mlContextCache.findIndex(t=>t.mlContext===r);-1!==t&&this.mlContextCache.splice(t,1)}}getMLContext(t){return this.mlContextBySessionId.get(t)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(t){te("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${t}}`),this.tensorManager.releaseTensorId(t)}async ensureTensor(t,r,i,a,n){let s=oN.get(i);if(!s)throw Error(`Unsupported ONNX data type: ${i}`);return this.tensorManager.ensureTensor(t??this.currentSessionId,r,s,a,n)}async createTemporaryTensor(t,r,i){te("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${r}, shape: ${i}}`);let a=oN.get(r);if(!a)throw Error(`Unsupported ONNX data type: ${r}`);let n=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(t,n,a,i,!1);let s=this.temporarySessionTensorIds.get(t);return s?s.push(n):this.temporarySessionTensorIds.set(t,[n]),n}uploadTensor(t,r){if(!eU().shouldTransferToMLTensor)throw Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");te("verbose",()=>`[WebNN] uploadTensor {tensorId: ${t}, data: ${r.byteLength}}`),this.tensorManager.upload(t,r)}async downloadTensor(t,r){return this.tensorManager.download(t,r)}createMLTensorDownloader(t,r){return async()=>{let i=await this.tensorManager.download(t);return tt(i,r)}}registerMLTensor(t,r,i,a){let n=oN.get(i);if(!n)throw Error(`Unsupported ONNX data type: ${i}`);let s=this.tensorManager.registerTensor(t,r,n,a);return te("verbose",()=>`[WebNN] registerMLTensor {tensor: ${r}, dataType: ${n}, dimensions: ${a}} -> {tensorId: ${s}}`),s}registerMLConstant(t,r,i,a,n,s){if(!s)throw Error("External mounted files are not available.");let o=t;t.startsWith("./")&&(o=t.substring(2));let u=s.get(o);if(!u)throw Error(`File with name ${o} not found in preloaded files.`);if(r+i>u.byteLength)throw Error("Out of bounds: data offset and length exceed the external file data size.");let l=u.slice(r,r+i).buffer,d;switch(n.dataType){case"float32":d=new Float32Array(l);break;case"float16":d=new Uint16Array(l);break;case"int32":d=new Int32Array(l);break;
2746case"uint32":d=new Uint32Array(l);break;case"int64":d=new BigInt64Array(l);break;case"uint64":d=new BigUint64Array(l);break;case"int8":d=new Int8Array(l);break;case"int4":case"uint4":case"uint8":d=new Uint8Array(l);break;default:throw Error(`Unsupported data type: ${n.dataType} in creating WebNN Constant from external data.`)}return te("verbose",()=>`[WebNN] registerMLConstant {dataType: ${n.dataType}, shape: ${n.shape}}}`),a.constant(n,d)}registerGraphInput(t){this.temporaryGraphInputs.push(t)}isGraphInput(t,r){let i=this.sessionGraphInputs.get(t);return!!i&&i.includes(r)}flush(){}}}),uF={};q(uF,{init:()=>uQ});var uj,uK,uQ,uZ,uX,uY,uJ,u0,u1,u2,u3,u4,u6,u8,u5,u9,u7,le,lt,lr,li,la,ln,ls,lo,lu,ll,ld,lp,lc,lh,lf,lm,lg,ly,l_,l$=W(()=>{oG(),uV(),oF(),oX(),uH(),uj=class t{constructor(t,r,i,a){this.module=t,this.dataType=r,this.data=i,this.dims=a}getFloat32Array(){if(1!==this.dataType)throw Error("Invalid data type");let t=tm.size(this.dims);return 0===t?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(7!==this.dataType)throw Error("Invalid data type");let t=tm.size(this.dims);return 0===t?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(6!==this.dataType)throw Error("Invalid data type");let t=tm.size(this.dims);return 0===t?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(10!==this.dataType&&4!==this.dataType)throw Error("Invalid data type");let t=tm.size(this.dims);return 0===t?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(r){if(tm.size(r)!==tm.size(this.dims))throw Error("Invalid new shape");return new t(this.module,this.dataType,this.data,r)}},uK=class{constructor(t,r,i){this.module=t,this.backend=r,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=r.adapterInfo,this.deviceInfo=r.deviceInfo;let a=t.PTR_SIZE,n=i/t.PTR_SIZE,s=4===a?"i32":"i64";this.opKernelContext=Number(t.getValue(a*n++,s));let o=Number(t.getValue(a*n++,s));this.outputCount=Number(t.getValue(a*n++,s)),this.customDataOffset=Number(t.getValue(a*n++,"*")),this.customDataSize=Number(t.getValue(a*n++,s));let u=[];for(let r=0;r<o;r++){let r=Number(t.getValue(a*n++,s)),i=Number(t.getValue(a*n++,"*")),o=Number(t.getValue(a*n++,s)),l=[];for(let r=0;r<o;r++)l.push(Number(t.getValue(a*n++,s)));u.push(new uj(t,r,i,l))}this.inputs=u}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}compute(t,r){let i=r?.inputs?.map(t=>"number"==typeof t?this.inputs[t]:t)??this.inputs,a=r?.outputs??[];return this.backend.run(t,i,a,(t,r,i)=>new uj(this.module,r,this.output(t,i),i),(t,r)=>{let i=eX(t,r);if(!i)throw Error(`Unsupported data type: ${t}`);let a=i>0?this.backend.gpuDataManager.create(i).id:0;return new uj(this.module,t,a,r)},this.outputCount)}output(t,r){let i=this.module.stackSave();try{let i=this.module.PTR_SIZE,a=4===i?"i32":"i64",n=this.module.stackAlloc((1+r.length)*i);this.module.setValue(n,r.length,a);for(let t=0;t<r.length;t++)this.module.setValue(n+i*(t+1),r[t],a);return this.module._JsepOutput(this.opKernelContext,t,n)}catch(i){throw Error(`Failed to generate kernel's output[${t}] with dims [${r}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${i}`)}finally{this.module.stackRestore(i)}}},uQ=async(t,r,i,a)=>{let n=r.jsepInit;if(!n)throw Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if("webgpu"===t){let t=new oT;await t.initialize(i,a),n("webgpu",[t,r=>t.alloc(Number(r)),r=>t.free(r),(i,a,n,s=!1)=>{if(s)te("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(i)}, dst=${Number(a)}, size=${Number(n)}`),t.memcpy(Number(i),Number(a));else{te("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(i)}, gpuDataId=${Number(a)}, size=${Number(n)}`);let s=r.HEAPU8.subarray(Number(i>>>0),Number(i>>>0)+Number(n));t.upload(Number(a),s)}},async(i,a,n)=>{te("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${i}, dataOffset=${a}, size=${n}`),await t.download(Number(i),()=>r.HEAPU8.subarray(Number(a)>>>0,Number(a+n)>>>0))},(i,a,n)=>t.createKernel(i,Number(a),n,r.UTF8ToString(r._JsepGetNodeName(Number(a)))),r=>t.releaseKernel(r),(i,a,n,s)=>{te("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${n}, kernel=${i}, contextDataOffset=${a}`);let o=new uK(r,t,Number(a));return t.computeKernel(Number(i),o,s)},()=>t.captureBegin(),()=>t.captureEnd(),()=>t.replay()])}else{let t=new oD(i);n("webnn",[t,()=>t.reserveTensorId(),r=>t.releaseTensorId(r),async(r,i,a,n,s)=>t.ensureTensor(r,i,a,n,s),(r,i)=>{t.uploadTensor(r,i)},async(r,i)=>t.downloadTensor(r,i)])}}}),lb=W(()=>{oL(),oV(),oG(),oW(),oq(),oH(),uZ=(t,r)=>{0!==eU()._OrtInit(t,r)&&eL("Can't initialize onnxruntime.")},uX=async t=>{uZ(t.wasm.numThreads,eJ(t.logLevel))},uY=async(t,r)=>{{let i=(l$(),V(uF)).init;if("webgpu"===r){if(typeof navigator>"u"||!navigator.gpu)throw Error("WebGPU is not supported in current environment");let r=t.webgpu.adapter;if(r){if("object"!=typeof r.limits||"object"!=typeof r.features||"function"!=typeof r.requestDevice)throw Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let i=t.webgpu.powerPreference;if(void 0!==i&&"low-power"!==i&&"high-performance"!==i)throw Error(`Invalid powerPreference setting: "${i}"`);let a=t.webgpu.forceFallbackAdapter;if(void 0!==a&&"boolean"!=typeof a)throw Error(`Invalid forceFallbackAdapter setting: "${a}"`);if(!(r=await navigator.gpu.requestAdapter({powerPreference:i,forceFallbackAdapter:a})))throw Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await i("webgpu",eU(),t,r)}if("webnn"===r){if(typeof navigator>"u"||!navigator.ml)throw Error("WebNN is not supported in current environment");await i("webnn",eU(),t)}}},uJ=new Map,u0=t=>{let r=eU(),i=r.stackSave();try{let i=r.PTR_SIZE,a=r.stackAlloc(2*i);0!==r._OrtGetInputOutputCount(t,a,a+i)&&eL("Can't get session input/output count.");
2746let n=4===i?"i32":"i64";return[Number(r.getValue(a,n)),Number(r.getValue(a+i,n))]}finally{r.stackRestore(i)}},u1=t=>{let r=eU(),i=r._malloc(t.byteLength);if(0===i)throw Error(`Can't create a session. failed to allocate a buffer of size ${t.byteLength}.`);return r.HEAPU8.set(t,i),[i,t.byteLength]},u2=async(t,r)=>{let i,a,n=eU();Array.isArray(t)?[i,a]=t:t.buffer===n.HEAPU8.buffer?[i,a]=[t.byteOffset,t.byteLength]:[i,a]=u1(t);let s=0,o=0,u=0,l=[],d=[],p=[];try{if([o,l]=eK(r),r?.externalData&&n.mountExternalData){let t=[];for(let i of r.externalData){let r="string"==typeof i?i:i.path;t.push(e3("string"==typeof i?i:i.data).then(t=>{n.mountExternalData(r,t)}))}await Promise.all(t)}for(let t of r?.executionProviders??[])if(("string"==typeof t?t:t.name)==="webnn"){if(n.shouldTransferToMLTensor=!1,"string"!=typeof t){let r=t?.context,i=t?.gpuDevice,a=t?.deviceType,s=t?.powerPreference;r?n.currentContext=r:i?n.currentContext=await n.jsepCreateMLContext(i):n.currentContext=await n.jsepCreateMLContext({deviceType:a,powerPreference:s})}else n.currentContext=await n.jsepCreateMLContext();break}s=await n._OrtCreateSession(i,a,o),0===s&&eL("Can't create a session."),n.jsepOnCreateSession?.(),n.currentContext&&(n.jsepRegisterMLContext(s,n.currentContext),n.currentContext=void 0,n.shouldTransferToMLTensor=!0);let[t,c]=u0(s),h=!!r?.enableGraphCapture,f=[],m=[],g=[];for(let r=0;r<t;r++){let t=n._OrtGetInputName(s,r);0===t&&eL("Can't get an input name."),d.push(t),f.push(n.UTF8ToString(t))}for(let t=0;t<c;t++){let i=n._OrtGetOutputName(s,t);0===i&&eL("Can't get an output name."),p.push(i);let a=n.UTF8ToString(i);m.push(a);{if(h&&r?.preferredOutputLocation===void 0){g.push("gpu-buffer");continue}let t="string"==typeof r?.preferredOutputLocation?r.preferredOutputLocation:r?.preferredOutputLocation?.[a]??"cpu";if("cpu"!==t&&"cpu-pinned"!==t&&"gpu-buffer"!==t&&"ml-tensor"!==t)throw Error(`Not supported preferred output location: ${t}.`);if(h&&"gpu-buffer"!==t)throw Error(`Not supported preferred output location: ${t}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);g.push(t)}}let y=null;return g.some(t=>"gpu-buffer"===t||"ml-tensor"===t)&&(u=n._OrtCreateBinding(s),0===u&&eL("Can't create IO binding."),y={handle:u,outputPreferredLocations:g,outputPreferredLocationsEncoded:g.map(t=>e2(t))}),uJ.set(s,[s,d,p,y,h,!1]),[s,f,m]}catch(t){throw d.forEach(t=>n._OrtFree(t)),p.forEach(t=>n._OrtFree(t)),0!==u&&0!==n._OrtReleaseBinding(u)&&eL("Can't release IO binding."),0!==s&&0!==n._OrtReleaseSession(s)&&eL("Can't release session."),t}finally{n._free(i),0!==o&&0!==n._OrtReleaseSessionOptions(o)&&eL("Can't release session options."),l.forEach(t=>n._free(t)),n.unmountExternalData?.()}},u3=t=>{let r=eU(),i=uJ.get(t);if(!i)throw Error(`cannot release session. invalid session id: ${t}`);let[a,n,s,o,u]=i;o&&(u&&0!==r._OrtClearBoundOutputs(o.handle)&&eL("Can't clear bound outputs."),0!==r._OrtReleaseBinding(o.handle)&&eL("Can't release IO binding.")),r.jsepOnReleaseSession?.(t),n.forEach(t=>r._OrtFree(t)),s.forEach(t=>r._OrtFree(t)),0!==r._OrtReleaseSession(a)&&eL("Can't release session."),uJ.delete(t)},u4=async(t,r,i,a,n,s=!1)=>{if(!t){r.push(0);return}let o=eU(),u=o.PTR_SIZE,l=t[0],d=t[1],p=t[3],c=p,h,f;if("string"===l&&("gpu-buffer"===p||"ml-tensor"===p))throw Error("String tensor is not supported on GPU.");if(s&&"gpu-buffer"!==p)throw Error(`External buffer must be provided for input/output index ${n} when enableGraphCapture is true.`);if("gpu-buffer"===p){let r=t[2].gpuBuffer;f=eX(eQ(l),d);let i=o.jsepRegisterBuffer;if(!i)throw Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');h=i(a,n,r,f)}else if("ml-tensor"===p){let r=t[2].mlTensor;f=eX(eQ(l),d);let i=o.jsepRegisterMLTensor;if(!i)throw Error('Tensor location "ml-tensor" is not supported without using WebNN.');h=i(a,r,eQ(l),d)}else{let r=t[2];if(Array.isArray(r)){f=u*r.length,h=o._malloc(f),i.push(h);for(let t=0;t<r.length;t++){if("string"!=typeof r[t])throw TypeError(`tensor data at index ${t} is not a string`);o.setValue(h+t*u,eW(r[t],i),"*")}}else{let t=o.jsepIsGraphInput;if("string"!==l&&t){let s=o._OrtGetInputName(a,n);if(t(a,o.UTF8ToString(s))){let t=eQ(l);f=eX(t,d),c="ml-tensor";let i=o.jsepCreateTemporaryTensor,n=o.jsepUploadTensor;if(!i||!n)throw Error('Tensor location "ml-tensor" is not supported without using WebNN.');let s=await i(a,t,d);n(s,new Uint8Array(r.buffer,r.byteOffset,r.byteLength)),h=s}else f=r.byteLength,h=o._malloc(f),i.push(h),o.HEAPU8.set(new Uint8Array(r.buffer,r.byteOffset,f),h)}else f=r.byteLength,h=o._malloc(f),i.push(h),o.HEAPU8.set(new Uint8Array(r.buffer,r.byteOffset,f),h)}}let m=o.stackSave(),g=o.stackAlloc(4*d.length);try{d.forEach((t,r)=>o.setValue(g+r*u,t,4===u?"i32":"i64"));let t=o._OrtCreateTensor(eQ(l),h,f,g,d.length,e2(c));0===t&&eL(`Can't create tensor for input/output. session=${a}, index=${n}.`),r.push(t)}finally{o.stackRestore(m)}},u6=async(t,r,i,a,n,s)=>{let o=eU(),u=o.PTR_SIZE,l=uJ.get(t);if(!l)throw Error(`cannot run inference. invalid session id: ${t}`);let d=l[0],p=l[1],c=l[2],h=l[3],f=l[4],m=l[5],g=r.length,y=a.length,_=0,$=[],b=[],v=[],w=[],x=o.stackSave(),S=o.stackAlloc(g*u),k=o.stackAlloc(g*u),T=o.stackAlloc(y*u),C=o.stackAlloc(y*u);try{let l;[_,$]=eV(s);for(let a=0;a<g;a++)await u4(i[a],b,w,t,r[a],f);for(let r=0;r<y;r++)await u4(n[r],v,w,t,g+a[r],f);for(let t=0;t<g;t++)o.setValue(S+t*u,b[t],"*"),o.setValue(k+t*u,p[r[t]],"*");for(let t=0;t<y;t++)o.setValue(T+t*u,v[t],"*"),o.setValue(C+t*u,c[a[t]],"*");if(h&&!m){let{handle:i,outputPreferredLocations:s,outputPreferredLocationsEncoded:u}=h;if(p.length!==g)throw Error(`input count from feeds (${g}
2746) is expected to be always equal to model's input count (${p.length}).`);for(let a=0;a<g;a++){let n=r[a];await o._OrtBindInput(i,p[n],b[a])!==0&&eL(`Can't bind input[${a}] for session=${t}.`)}for(let r=0;r<y;r++){let l=a[r];n[r]?.[3]?0!==o._OrtBindOutput(i,c[l],v[r],0)&&eL(`Can't bind pre-allocated output[${r}] for session=${t}.`):0!==o._OrtBindOutput(i,c[l],0,u[l])&&eL(`Can't bind output[${r}] to ${s[r]} for session=${t}.`)}uJ.set(t,[d,p,c,h,f,!0])}o.jsepOnRunStart?.(d),l=h?await o._OrtRunWithBinding(d,h.handle,y,T,_):await o._OrtRun(d,k,S,g,C,y,T,_),0!==l&&eL("failed to call OrtRun().");let x=[];for(let r=0;r<y;r++){let i=Number(o.getValue(T+r*u,"*"));if(i===v[r]){x.push(n[r]);continue}let s=o.stackSave(),l=o.stackAlloc(4*u),p=!1,c,f=0;try{0!==o._OrtGetTensorData(i,l,l+u,l+2*u,l+3*u)&&eL(`Can't access output tensor data on index ${r}.`);let n=4===u?"i32":"i64",s=Number(o.getValue(l,n));f=o.getValue(l+u,"*");let d=o.getValue(l+2*u,"*"),m=Number(o.getValue(l+3*u,n)),g=[];for(let t=0;t<m;t++)g.push(Number(o.getValue(d+t*u,n)));0!==o._OrtFree(d)&&eL("Can't free memory for tensor dims.");let y=g.reduce((t,r)=>t*r,1);c=eZ(s);let _=h?.outputPreferredLocations[a[r]];if("string"===c){if("gpu-buffer"===_||"ml-tensor"===_)throw Error("String tensor is not supported on GPU.");let t=[];for(let r=0;r<y;r++){let i=o.getValue(f+r*u,"*"),a=o.getValue(f+(r+1)*u,"*"),n=r===y-1?void 0:a-i;t.push(o.UTF8ToString(i,n))}x.push([c,g,t,"cpu"])}else if("gpu-buffer"===_&&y>0){let t=o.jsepGetBuffer;if(!t)throw Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let r=t(f),a=eX(s,y);if(void 0===a||!e0(c))throw Error(`Unsupported data type: ${c}`);p=!0,x.push([c,g,{gpuBuffer:r,download:o.jsepCreateDownloader(r,a,c),dispose:()=>{0!==o._OrtReleaseTensor(i)&&eL("Can't release tensor.")}},"gpu-buffer"])}else if("ml-tensor"===_&&y>0){let r=o.jsepEnsureTensor;if(!r)throw Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(void 0===eX(s,y)||!e1(c))throw Error(`Unsupported data type: ${c}`);let a=await r(t,f,s,g,!1);p=!0,x.push([c,g,{mlTensor:a,download:o.jsepCreateMLTensorDownloader(f,c),dispose:()=>{o.jsepReleaseTensorId(f),o._OrtReleaseTensor(i)}},"ml-tensor"])}else{let t=new(eY(c))(y);new Uint8Array(t.buffer,t.byteOffset,t.byteLength).set(o.HEAPU8.subarray(f,f+t.byteLength)),x.push([c,g,t,"cpu"])}}finally{o.stackRestore(s),"string"===c&&f&&o._free(f),p||o._OrtReleaseTensor(i),o.jsepOnRunEnd?.(d)}}return h&&!f&&(0!==o._OrtClearBoundOutputs(h.handle)&&eL("Can't clear bound outputs."),uJ.set(t,[d,p,c,h,f,!1])),x}finally{o.stackRestore(x),b.forEach(t=>o._OrtReleaseTensor(t)),v.forEach(t=>o._OrtReleaseTensor(t)),w.forEach(t=>o._free(t)),0!==_&&o._OrtReleaseRunOptions(_),$.forEach(t=>o._free(t))}},u8=t=>{let r=eU(),i=uJ.get(t);if(!i)throw Error("invalid session id");let a=i[0],n=r._OrtEndProfiling(a);0===n&&eL("Can't get an profile file name."),r._OrtFree(n)},u5=t=>{let r=[];for(let i of t){let t=i[2];!Array.isArray(t)&&"buffer"in t&&r.push(t.buffer)}return r}}),lv=W(()=>{el(),lb(),oW(),oU(),u9=()=>!!c.wasm.proxy&&"u">typeof document,le=!1,lt=!1,lr=!1,ln=new Map,ls=(t,r)=>{let i=ln.get(t);i?i.push(r):ln.set(t,[r])},lo=()=>{if(le||!lt||lr||!u7)throw Error("worker not ready")},lu=t=>{switch(t.data.type){case"init-wasm":le=!1,t.data.err?(lr=!0,la[1](t.data.err)):(lt=!0,la[0]()),li&&(URL.revokeObjectURL(li),li=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let r=ln.get(t.data.type);t.data.err?r.shift()[1](t.data.err):r.shift()[0](t.data.out)}}},ll=async()=>{if(!lt){if(le)throw Error("multiple calls to 'initWasm()' detected.");if(lr)throw Error("previous call to 'initWasm()' failed.");if(le=!0,u9())return new Promise((t,r)=>{u7?.terminate(),eE().then(([a,n])=>{try{(u7=n).onerror=t=>r(t),u7.onmessage=lu,la=[t,r];let s={type:"init-wasm",in:c};!s.in.wasm.wasmPaths&&(a||"file:///vercel/path0/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs".startsWith("file:"))&&(s.in.wasm.wasmPaths={wasm:new i.U(i(47759)).href}),u7.postMessage(s),li=a}catch(t){r(t)}},r)});try{await eP(c.wasm),await uX(c),lt=!0}catch(t){throw lr=!0,t}finally{le=!1}}},ld=async t=>{if(u9())return lo(),new Promise((r,i)=>{ls("init-ep",[r,i]);let a={type:"init-ep",in:{epName:t,env:c}};
2746u7.postMessage(a)});await uY(c,t)},lp=async t=>u9()?(lo(),new Promise((r,i)=>{ls("copy-from",[r,i]),u7.postMessage({type:"copy-from",in:{buffer:t}},[t.buffer])})):u1(t),lc=async(t,r)=>{if(!u9())return u2(t,r);if(r?.preferredOutputLocation)throw Error('session option "preferredOutputLocation" is not supported for proxy.');return lo(),new Promise((i,a)=>{ls("create",[i,a]);let n={type:"create",in:{model:t,options:{...r}}},s=[];t instanceof Uint8Array&&s.push(t.buffer),u7.postMessage(n,s)})},lh=async t=>{if(u9())return lo(),new Promise((r,i)=>{ls("release",[r,i]),u7.postMessage({type:"release",in:t})});u3(t)},lf=async(t,r,i,a,n,s)=>{if(!u9())return u6(t,r,i,a,n,s);if(i.some(t=>"cpu"!==t[3]))throw Error("input tensor on GPU is not supported for proxy.");if(n.some(t=>t))throw Error("pre-allocated output tensor is not supported for proxy.");return lo(),new Promise((n,o)=>{ls("run",[n,o]),u7.postMessage({type:"run",in:{sessionId:t,inputIndices:r,inputs:i,outputIndices:a,options:s}},u5(i))})},lm=async t=>{if(u9())return lo(),new Promise((r,i)=>{ls("end-profiling",[r,i]),u7.postMessage({type:"end-profiling",in:t})});u8(t)}}),lw=W(()=>{el(),lv(),oG(),ed(),oH(),lg=(t,r)=>{switch(t.location){case"cpu":return[t.type,t.dims,t.data,"cpu"];case"gpu-buffer":return[t.type,t.dims,{gpuBuffer:t.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[t.type,t.dims,{mlTensor:t.mlTensor},"ml-tensor"];default:throw Error(`invalid data location: ${t.location} for ${r()}`)}},ly=t=>{switch(t[3]){case"cpu":return new I(t[0],t[2],t[1]);case"gpu-buffer":{let r=t[0];if(!e0(r))throw Error(`not supported data type: ${r} for deserializing GPU tensor`);let{gpuBuffer:i,download:a,dispose:n}=t[2];return I.fromGpuBuffer(i,{dataType:r,dims:t[1],download:a,dispose:n})}case"ml-tensor":{let r=t[0];if(!e1(r))throw Error(`not supported data type: ${r} for deserializing MLTensor tensor`);let{mlTensor:i,download:a,dispose:n}=t[2];return I.fromMLTensor(i,{dataType:r,dims:t[1],download:a,dispose:n})}default:throw Error(`invalid data location: ${t[3]}`)}},l_=class{async fetchModelAndCopyToWasmMemory(t){return lp(await e3(t))}async loadModel(t,r){let i;A(),i="string"==typeof t?await this.fetchModelAndCopyToWasmMemory(t):t,[this.sessionId,this.inputNames,this.outputNames]=await lc(i,r),O()}async dispose(){return lh(this.sessionId)}async run(t,r,i){A();let a=[],n=[];Object.entries(t).forEach(t=>{let r=t[0],i=t[1],s=this.inputNames.indexOf(r);if(-1===s)throw Error(`invalid input '${r}'`);a.push(i),n.push(s)});let s=[],o=[];Object.entries(r).forEach(t=>{let r=t[0],i=t[1],a=this.outputNames.indexOf(r);if(-1===a)throw Error(`invalid output '${r}'`);s.push(i),o.push(a)});let u=a.map((t,r)=>lg(t,()=>`input "${this.inputNames[n[r]]}"`)),l=s.map((t,r)=>t?lg(t,()=>`output "${this.outputNames[o[r]]}"`):null),d=await lf(this.sessionId,n,u,o,l,i),p={};for(let t=0;t<d.length;t++)p[this.outputNames[o[t]]]=s[t]??ly(d[t]);return O(),p}startProfiling(){}endProfiling(){lm(this.sessionId)}}}),lx={};q(lx,{OnnxruntimeWebAssemblyBackend:()=>lk,initializeFlags:()=>lS,wasmBackend:()=>lT});var lS,lk,lT,lC=W(()=>{el(),lv(),lw(),lS=()=>{if(("number"!=typeof c.wasm.initTimeout||c.wasm.initTimeout<0)&&(c.wasm.initTimeout=0),!1===c.wasm.simd&&console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'),"boolean"!=typeof c.wasm.proxy&&(c.wasm.proxy=!1),"boolean"!=typeof c.wasm.trace&&(c.wasm.trace=!1),"number"!=typeof c.wasm.numThreads||!Number.isInteger(c.wasm.numThreads)||c.wasm.numThreads<=0){if("u">typeof self&&!self.crossOriginIsolated)c.wasm.numThreads=1;else{let t=typeof navigator>"u"?U("node:os").cpus().length:navigator.hardwareConcurrency;c.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}}},lT=new(lk=class{async init(t){lS(),await ll(),await ld(t)}async createInferenceSessionHandler(t,r){let i=new l_;return await i.loadModel(t,r),Promise.resolve(i)}})});el(),el(),el();var lI=eu;{let t=(lC(),V(lx)).wasmBackend;s("webgpu",t,5),s("webnn",t,5),s("cpu",t,10),s("wasm",t,10)}Object.defineProperty(c.versions,"web",{value:"1.21.0",enumerable:!0})}}]);
2747//# sourceMappingURL=037f3a08.0d52295304d92eca.js.map

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.