vendor: 16,356 bytes, lines 1-5
1var Xy=Object.defineProperty;var Jy=(Je,et,Nt)=>et in Je?Xy(Je,et,{enumerable:!0,configurable:!0,writable:!0,value:Nt}):Je[et]=Nt;var wf=(Je,et,Nt)=>Jy(Je,typeof et!="symbol"?et+"":et,Nt);(function(){"use strict";/*! 2 * ONNX Runtime Web v1.24.3 3 * Copyright (c) Microsoft Corporation. All rights reserved. 4 * Licensed under the MIT License. 5 */var Je=Object.defineProperty,et=Object.getOwnPropertyDescriptor,Nt=Object.getOwnPropertyNames,vf=Object.prototype.hasOwnProperty,xf=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')}),q=(e,t)=>()=>(e&&(t=e(e=0)),t),Dt=(e,t)=>{for(var r in t)Je(e,r,{get:t[r],enumerable:!0})},Sf=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of Nt(t))!vf.call(e,a)&&a!==r&&Je(e,a,{get:()=>t[a],enumerable:!(i=et(t,a))||i.enumerable});return e},Ht=e=>Sf(Je({},"__esModule",{value:!0}),e),Ft,st,Pt,dn,pn,cn=q(()=>{Ft=new Map,st=[],Pt=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let i=Ft.get(e);if(i===void 0)Ft.set(e,{backend:t,priority:r});else{if(i.priority>r)return;if(i.priority===r&&i.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let a=st.indexOf(e);a!==-1&&st.splice(a,1);for(let s=0;s<st.length;s++)if(Ft.get(st[s]).priority<=r){st.splice(s,0,e);return}st.push(e)}return}throw new TypeError("not a valid backend")},dn=async e=>{let t=Ft.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(i){return r||(t.error=`${i}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},pn=async e=>{let t=e.executionProviders||[],r=t.map(l=>typeof l=="string"?l:l.name),i=r.length===0?st:r,a,s=[],n=new Set;for(let l of i){let p=await dn(l);typeof p=="string"?s.push({name:l,err:p}):(a||(a=p),a===p&&n.add(l))}if(!a)throw new Error(`no available backend found. ERR: ${s.map(l=>`[${l.name}] ${l.err}`).join(", ")}`);for(let{name:l,err:p}of s)r.includes(l)&&console.warn(`removing requested execution provider "${l}" from session options because it is not available: ${p}`);let u=t.filter(l=>n.has(typeof l=="string"?l:l.name));return[a,new Proxy(e,{get:(l,p)=>p==="executionProviders"?u:Reflect.get(l,p)})]}}),kf=q(()=>{cn()}),hn,Tf=q(()=>{hn="1.24.3"}),ti,Te,fn=q(()=>{Tf(),ti="warning",Te={wasm:{},webgl:{},webgpu:{},versions:{common:hn},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);ti=e}},get logLevel(){return ti}},Object.defineProperty(Te,"logLevel",{enumerable:!0})}),ge,If=q(()=>{fn(),ge=Te}),mn,gn,Ef=q(()=>{mn=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let i=r.getContext("2d");if(i!=null){let a,s;(t==null?void 0:t.tensorLayout)!==void 0&&t.tensorLayout==="NHWC"?(a=e.dims[2],s=e.dims[3]):(a=e.dims[3],s=e.dims[2]);let n=(t==null?void 0:t.format)!==void 0?t.format:"RGB",u=t==null?void 0:t.norm,l,p;u===void 0||u.mean===void 0?l=[255,255,255,255]:typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:(l=[u.mean[0],u.mean[1],u.mean[2],0],u.mean[3]!==void 0&&(l[3]=u.mean[3])),u===void 0||u.bias===void 0?p=[0,0,0,0]:typeof u.bias=="number"?p=[u.bias,u.bias,u.bias,u.bias]:(p=[u.bias[0],u.bias[1],u.bias[2],0],u.bias[3]!==void 0&&(p[3]=u.bias[3]));let h=s*a,f=0,g=h,y=h*2,_=-1;n==="RGBA"?(f=0,g=h,y=h*2,_=h*3):n==="RGB"?(f=0,g=h,y=h*2):n==="RBG"&&(f=0,y=h,g=h*2);for(let w=0;w<s;w++)for(let S=0;S<a;S++){let x=(e.data[f++]-p[0])*l[0],b=(e.data[g++]-p[1])*l[1],I=(e.data[y++]-p[2])*l[2],T=_===-1?255:(e.data[_++]-p[3])*l[3];i.fillStyle="rgba("+x+","+b+","+I+","+T+")",i.fillRect(S,w,1,1)}if("toDataURL"in r)return r.toDataURL();throw new Error("toDataURL is not supported")}else throw new Error("Can not access image data")},gn=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),i;if(r!=null){let a,s,n;(t==null?void 0:t.tensorLayout)!==void 0&&t.tensorLayout==="NHWC"?(a=e.dims[2],s=e.dims[1],n=e.dims[3]):(a=e.dims[3],s=e.dims[2],n=e.dims[1]);let u=t!==void 0&&t.format!==void 0?t.format:"RGB",l=t==null?void 0:t.norm,p,h;l===void 0||l.mean===void 0?p=[255,255,255,255]:typeof l.mean=="number"?p=[l.mean,l.mean,l.mean,l.mean]:(p=[l.mean[0],l.mean[1],l.mean[2],255],l.mean[3]!==void 0&&(p[3]=l.mean[3])),l===void 0||l.bias===void 0?h=[0,0,0,0]:typeof l.bias=="number"?h=[l.bias,l.bias,l.bias,l.bias]:(h=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(h[3]=l.bias[3]));let f=s*a;if(t!==void 0&&(t.format!==void 0&&n===4&&t.format!=="RGBA"||n===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let g=4,y=0,_=1,w=2,S=3,x=0,b=f,I=f*2,T=-1;u==="RGBA"?(x=0,b=f,I=f*2,T=f*3):u==="RGB"?(x=0,b=f,I=f*2):u==="RBG"&&(x=0,I=f,b=f*2),i=r.createImageData(a,s);for(let E=0;E<s*a;y+=g,_+=g,w+=g,S+=g,E++)i.data[y]=(e.data[x++]-h[0])*p[0],i.data[_]=(e.data[b++]-h[1])*p[1],i.data[w]=(e.data[I++]-h[2])*p[2],i.data[S]=T===-1?255:(e.data[T++]-h[3])*p[3]}else throw new Error("Can not access image data");return i}}),gr,yn,_n,wn,bn,$n,zf=q(()=>{ii(),gr=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:i}=t,a=t.norm??{mean:255,bias:0},s,n;typeof a.mean=="number"?s=[a.mean,a.mean,a.mean,a.mean]:s=[a.mean[0],a.mean[1],a.mean[2],a.mean[3]??255],typeof a.bias=="number"?n=[a.bias,a.bias,a.bias,a.bias]:n=[a.bias[0],a.bias[1],a.bias[2],a.bias[3]??0];let u=t.format!==void 0?t.format:"RGBA",l=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",p=r*i,h=l==="RGBA"?new Float32Array(p*4):new Float32Array(p*3),f=4,g=0,y=1,_=2,w=3,S=0,x=p,b=p*2,I=-1;u==="RGB"&&(f=3,g=0,y=1,_=2,w=-1),l==="RGBA"?I=p*3:l==="RBG"?(S=0,b=p,x=p*2):l==="BGR"&&(b=0,x=p,S=p*2);for(let T=0;T<p;T++,g+=f,_+=f,y+=f,w+=f)h[S++]=(e[g]+n[0])/s[0],h[x++]=(e[y]+n[1])/s[1],h[b++]=(e[_]+n[2])/s[2],I!==-1&&w!==-1&&(h[I++]=(e[w]+n[3])/s[3]);return l==="RGBA"?new Oe("float32",h,[1,4,r,i]):new Oe("float32",h,[1,3,r,i])},yn=async(e,t)=>{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,i=typeof ImageData<"u"&&e instanceof ImageData,a=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,s=typeof e=="string",n,u=t??{},l=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},p=h=>typeof HTMLCanvasElement<"u"&&h instanceof HTMLCanvasElement||h instanceof OffscreenCanvas?h.getContext("2d"):null;if(r){let h=l();h.width=e.width,h.height=e.height;let f=p(h);if(f!=null){let g=e.height,y=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(g=t.resizedHeight,y=t.resizedWidth),t!==void 0){if(u=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");u.tensorFormat="RGBA",u.height=g,u.width=y}else u.tensorFormat="RGBA",u.height=g,u.width=y;f.drawImage(e,0,0),n=f.getImageData(0,0,y,g).data}else throw new Error("Can not access image data")}else if(i){let h,f;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(h=t.resizedHeight,f=t.resizedWidth):(h=e.height,f=e.width),t!==void 0&&(u=t),u.format="RGBA",u.height=h,u.width=f,t!==void 0){let g=l();g.width=f,g.height=h;let y=p(g);if(y!=null)y.putImageData(e,0,0),n=y.getImageData(0,0,f,h).data;else throw new Error("Can not access image data")}else n=e.data}else if(a){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let h=l();h.width=e.width,h.height=e.height;let f=p(h);if(f!=null){let g=e.height,y=e.width;return f.drawImage(e,0,0,y,g),n=f.getImageData(0,0,y,g).data,u.height=g,u.width=y,gr(n,u)}else throw new Error("Can not access image data")}else{if(s)return new Promise((h,f)=>{let g=l(),y=p(g);if(!e||!y)return f();let _=new Image;_.crossOrigin="Anonymous",_.src=e,_.onload=()=>{g.width=_.width,g.height=_.height,y.drawImage(_,0,0,g.width,g.height);let w=y.getImageData(0,0,g.width,g.height);u.height=g.height,u.width=g.width,h(gr(w.data,u))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(n!==void 0)return gr(n,u);throw new Error("Input data provided is not supported - aborted tensor creation")},_n=(e,t)=>{let{width:r,height:i,download:a,dispose:s}=t,n=[1,i,r,4];return new Oe({location:"texture",type:"float32",texture:e,dims:n,download:a,dispose:s})},wn=(e,t)=>{let{dataType:r,dims:i,download:a,dispose:s}=t;return new Oe({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:i,download:a,dispose:s})},bn=(e,t)=>{let{dataType:r,dims:i,download:a,dispose:s}=t;return new Oe({location:"ml-tensor",type:r??"float32",mlTensor:e,dims:i,download:a,dispose:s})},$n=(e,t,r)=>new Oe({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})}),wt,jt,ri,vn,Cf=q(()=>{wt=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),jt=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),ri=!1,vn=()=>{if(!ri){ri=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=globalThis.Float16Array,i=typeof r<"u"&&r.from;e&&(wt.set("int64",BigInt64Array),jt.set(BigInt64Array,"int64")),t&&(wt.set("uint64",BigUint64Array),jt.set(BigUint64Array,"uint64")),i?(wt.set("float16",r),jt.set(r,"float16")):wt.set("float16",Uint16Array)}}}),xn,Sn,Af=q(()=>{ii(),xn=e=>{let t=1;for(let r=0;r<e.length;r++){let i=e[r];if(typeof i!="number"||!Number.isSafeInteger(i))throw new TypeError(`dims[${r}] must be an integer, got: ${i}`);if(i<0)throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${i}`);t*=i}return t},Sn=(e,t)=>{switch(e.location){case"cpu":return new Oe(e.type,e.data,t);case"cpu-pinned":return new Oe({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new Oe({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new Oe({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new Oe({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),Oe,ii=q(()=>{Ef(),zf(),Cf(),Af(),Oe=class{constructor(e,t,r){vn();let i,a;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,i=e.type,a=e.dims,e.location){case"cpu-pinned":{let n=wt.get(i);if(!n)throw new TypeError(`unsupported type "${i}" to create tensor from pinned buffer`);if(!(e.data instanceof n))throw new TypeError(`buffer should be of type ${n.name}`);this.cpuData=e.data;break}case"texture":{if(i!=="float32")throw new TypeError(`unsupported type "${i}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint64"&&i!=="int8"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let n,u;if(typeof e=="string")if(i=e,u=r,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");n=t}else{let l=wt.get(e);if(l===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&l===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${l.name} as data.`);e==="uint64"||e==="int64"?n=l.from(t,BigInt):n=l.from(t)}else if(t instanceof l)n=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")n=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&l!==Uint16Array)n=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${i} tensor's data must be type of ${l}`)}else if(u=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let l=typeof e[0];if(l==="string")i="string",n=e;else if(l==="boolean")i="bool",n=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${l}.`)}else if(e instanceof Uint8ClampedArray)i="uint8",n=Uint8Array.from(e);else{let l=jt.get(e.constructor);if(l===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);i=l,n=e}if(u===void 0)u=[n.length];else if(!Array.isArray(u))throw new TypeError("A tensor's dims must be a number array");a=u,this.cpuData=n,this.dataLocation="cpu"}let s=xn(a);if(this.cpuData&&s!==this.cpuData.length&&!((i==="uint4"||i==="int4")&&Math.ceil(s/2)===this.cpuData.length))throw new Error(`Tensor's size(${s}) does not match data length(${this.cpuData.length}).`);this.type=i,this.dims=a,this.size=s}static async fromImage(e,t){return yn(e,t)}static fromTexture(e,t){return _n(e,t)}static fromGpuBuffer(e,t){return wn(e,t)}static fromMLTensor(e,t){return bn(e,t)}static fromPinnedBuffer(e,t,r){return $n(e,t,r)}toDataURL(e){return mn(this,e)}toImageData(e){return gn(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return Sn(this,e)}}}),Re,kn=q(()=>{ii(),Re=Oe}),yr,ai,Ke,qe,bt,$t,Tn=q(()=>{fn(),yr=(e,t)=>
5{(typeof Te.trace>"u"?!Te.wasm.trace:!Te.trace)||console.timeStamp(`${e}::ORT::${t}`)},ai=(e,t)=>{var a;let r=((a=new Error().stack)==null?void 0:a.split(/\r\n|\r|\n/g))||[],i=!1;for(let s=0;s<r.length;s++){if(i&&!r[s].includes("TRACE_FUNC")){let n=`FUNC_${e}::${r[s].trim().split(" ")[1]}`;t&&(n+=`::${t}`),yr("CPU",n);return}r[s].includes("TRACE_FUNC")&&(i=!0)}},Ke=e=>{(typeof Te.trace>"u"?!Te.wasm.trace:!Te.trace)||ai("BEGIN",e)},qe=e=>{(typeof Te.trace>"u"?!Te.wasm.trace:!Te.trace)||ai("END",e)},bt=e=>{(typeof Te.trace>"u"?!Te.wasm.trace:!Te.trace)||console.time(`ORT::${e}`)},$t=e=>{(typeof Te.trace>"u"?!Te.wasm.trace:!Te.trace)||console.timeEnd(`ORT::${e}`)}}),In,Of=q(()=>{cn(),kn(),Tn(),In=class bf{constructor(t){this.handler=t}async run(t,r,i){Ke(),bt("InferenceSession.run");let a={},s={};if(typeof t!="object"||t===null||t instanceof Re||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let n=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Re)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");n=!1;for(let p of r){if(typeof p!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(p)===-1)throw new RangeError(`'fetches' contains invalid output name: ${p}.`);a[p]=null}if(typeof i=="object"&&i!==null)s=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,h=Object.getOwnPropertyNames(r);for(let f of this.outputNames)if(h.indexOf(f)!==-1){let g=r[f];(g===null||g instanceof Re)&&(p=!0,n=!1,a[f]=g)}if(p){if(typeof i=="object"&&i!==null)s=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else s=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let p of this.inputNames)if(typeof t[p]>"u")throw new Error(`input '${p}' is missing in 'feeds'.`);if(n)for(let p of this.outputNames)a[p]=null;let u=await this.handler.run(t,a,s),l={};for(let p in u)if(Object.hasOwnProperty.call(u,p)){let h=u[p];h instanceof Re?l[p]=h:l[p]=new Re(h.type,h.data,h.dims)}return $t("InferenceSession.run"),qe(),l}async release(){return this.handler.dispose()}static async create(t,r,i,a){Ke(),bt("InferenceSession.create");let s,n={};if(typeof t=="string"){if(s=t,typeof r=="object"&&r!==null)n=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(s=t,typeof r=="object"&&r!==null)n=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let h=t,f=0,g=t.byteLength;if(typeof r=="object"&&r!==null)n=r;else if(typeof r=="number"){if(f=r,!Number.isSafeInteger(f))throw new RangeError("'byteOffset' must be an integer.");if(f<0||f>=h.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${h.byteLength}).`);if(g=t.byteLength-f,typeof i=="number"){if(g=i,!Number.isSafeInteger(g))throw new RangeError("'byteLength' must be an integer.");if(g<=0||f+g>h.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${h.byteLength-f}].`);if(typeof a=="object"&&a!==null)n=a;else if(typeof a<"u")throw new TypeError("'options' must be an object.")}else if(typeof i<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");s=new Uint8Array(h,f,g)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[u,l]=await pn(n),p=await u.createInferenceSessionHandler(s,l);return $t("InferenceSession.create"),qe(),new bf(p)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),ni,Rf=q(()=>{Of(),ni=In}),Bf=q(()=>{}),Mf=q(()=>{}),Nf=q(()=>{}),Df=q(()=>{}),Pf={};Dt(Pf,{InferenceSession:()=>ni,TRACE:()=>yr,TRACE_EVENT_BEGIN:()=>bt,TRACE_EVENT_END:()=>$t,TRACE_FUNC_BEGIN:()=>Ke,TRACE_FUNC_END:()=>qe,Tensor:()=>Re,env:()=>ge,registerBackend:()=>Pt});var Pe=q(()=>{kf(),If(),Rf(),kn(),Bf(),Mf(),Tn(),Nf(),Df()}),si=q(()=>{}),En={};Dt(En,{default:()=>zn});var oi,ui,zn,Uf=q(()=>{var e;dc(),vt(),fi(),oi="ort-wasm-proxy-worker",ui=((e=globalThis.self)==null?void 0:e.name)===oi,ui&&(self.onmessage=t=>{let{type:r,in:i}=t.data;
vendor: 5,193 bytes, lines 5-6
5try{switch(r){case"init-wasm":yi(i.wasm).then(()=>{Ea(i).then(()=>{postMessage({type:r})},a=>{postMessage({type:r,err:a})})},a=>{postMessage({type:r,err:a})});break;case"init-ep":{let{epName:a,env:s}=i;za(s,a).then(()=>{postMessage({type:r})},n=>{postMessage({type:r,err:n})});break}case"copy-from":{let{buffer:a}=i,s=Nr(a);postMessage({type:r,out:s});break}case"create":{let{model:a,options:s}=i;Aa(a,s).then(n=>{postMessage({type:r,out:n})},n=>{postMessage({type:r,err:n})});break}case"release":Oa(i),postMessage({type:r});break;case"run":{let{sessionId:a,inputIndices:s,inputs:n,outputIndices:u,options:l}=i;Ba(a,s,n,u,new Array(u.length).fill(null),l).then(p=>{p.some(h=>h[3]!=="cpu")?postMessage({type:r,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:r,out:p},Na([...n,...p]))},p=>{postMessage({type:r,err:p})});break}case"end-profiling":Ma(i),postMessage({type:r});break;default:}}catch(a){postMessage({type:r,err:a})}}),zn=ui?null:t=>new Worker(t??Be,{type:"module",name:oi})}),Cn={};Dt(Cn,{default:()=>On});async function An(e={}){var yf,_f;var t=e,r=!!globalThis.window,i=!!globalThis.WorkerGlobalScope,a=i&&((yf=self.name)==null?void 0:yf.startsWith("em-pthread"));t.mountExternalData=(o,d)=>{o.startsWith("./")&&(o=o.substring(2)),(t.Zc||(t.Zc=new Map)).set(o,d)},t.unmountExternalData=()=>{delete t.Zc},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,ae:!0}).buffer.constructor;let s=o=>async(...d)=>{var m;try{if(t.$c)throw Error("Session already started");let c=t.$c={Nd:d[0],errors:[]},$=await o(...d);if(t.$c!==c)throw Error("Session mismatch");(m=t.gd)==null||m.flush();let k=c.errors;if(0<k.length){let z=await Promise.all(k);if(z=z.filter(B=>B),0<z.length)throw Error(z.join(` 6`))}return $}finally{t.$c=null}};t.jsepInit=(o,d)=>{if(o==="webgpu"){[t.gd,t.Dd,t.Hd,t.jd,t.Gd,t.ac,t.Id,t.Kd,t.Ed,t.Fd,t.Jd]=d;let m=t.gd;t.jsepRegisterBuffer=(c,$,k,z)=>m.registerBuffer(c,$,k,z),t.jsepGetBuffer=c=>m.getBuffer(c),t.jsepCreateDownloader=(c,$,k)=>m.createDownloader(c,$,k),t.jsepOnCreateSession=c=>{m.onCreateSession(c)},t.jsepOnReleaseSession=c=>{m.onReleaseSession(c)},t.jsepOnRunStart=c=>m.onRunStart(c),t.Ld=(c,$)=>{m.upload(c,$)}}else if(o==="webnn"){let m=d[0];[t.Zd,t.vd,t.webnnEnsureTensor,t.xd,t.webnnDownloadTensor,t.Yd,t.webnnEnableTraceEvent]=d.slice(1),t.webnnReleaseTensorId=t.vd,t.webnnUploadTensor=t.xd,t.webnnRegisterMLContext=t.Yd,t.webnnOnRunStart=c=>m.onRunStart(c),t.webnnOnRunEnd=m.onRunEnd.bind(m),t.webnnOnReleaseSession=c=>{m.onReleaseSession(c)},t.webnnCreateMLTensorDownloader=(c,$)=>m.createMLTensorDownloader(c,$),t.webnnRegisterMLTensor=(c,$,k,z)=>m.registerMLTensor(c,$,k,z),t.webnnCreateMLContext=c=>m.createMLContext(c),t.webnnRegisterMLConstant=(c,$,k,z,B,L)=>m.registerMLConstant(c,$,k,z,B,t.Zc,L),t.webnnRegisterGraphInput=m.registerGraphInput.bind(m),t.webnnIsGraphInput=m.isGraphInput.bind(m),t.webnnRegisterGraphOutput=m.registerGraphOutput.bind(m),t.webnnIsGraphOutput=m.isGraphOutput.bind(m),t.webnnCreateTemporaryTensor=m.createTemporaryTensor.bind(m),t.webnnIsGraphInputOutputTypeSupported=m.isGraphInputOutputTypeSupported.bind(m)}};let n=()=>{let o=d=>(...m)=>{let c=Ye;return m=d(...m),Ye!=c?new Promise(($,k)=>{Za={resolve:$,reject:k}}):m};(()=>{for(let d of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[d]=o(t[d])})(),s!==void 0&&(t._OrtRun=s(t._OrtRun),t._OrtRunWithBinding=s(t._OrtRunWithBinding)),n=void 0};t.asyncInit=()=>{n==null||n()};var u,l,p=(o,d)=>{throw d},h=self.location.href,f="";if(r||i){try{f=new URL(".",h).href}catch{}i&&(l=o=>{var d=new XMLHttpRequest;return d.open("GET",o,!1),d.responseType="arraybuffer",d.send(null),new Uint8Array(d.response)}),u=async o=>{if(A(o))return new Promise((m,c)=>{var $=new XMLHttpRequest;$.open("GET",o,!0),$.responseType="arraybuffer",$.onload=()=>{$.status==200||$.status==0&&$.response?m($.response):c($.status)},$.onerror=c,$.send(null)});var d=await fetch(o,{credentials:"same-origin"});if(d.ok)return d.arrayBuffer();throw Error(d.status+" : "+d.url)}}var g,y,_,w,S,x,b=console.log.bind(console),I=console.error.bind(console),T=b,E=I,C=!1,A=o=>o.startsWith("file://");function v(){mt.buffer!=D.buffer&&U()}if(a){let o=function(d){try{var m=d.data,c=m.Uc;if(c==="load"){let $=[];self.onmessage=k=>$.push(k),x=()=>{postMessage({Uc:"loaded"});for(let k of $)o(k);self.onmessage=o};for(let k of m.Ad)t[k]&&!t[k].proxy||(t[k]=(...z)=>{postMessage({Uc:"callHandler",zd:k,args:z})},k=="print"&&(T=t[k]),k=="printErr"&&(E=t[k]));mt=m.Vd,U(),y=m.Wd,pt(),Jr()}else if(c==="run"){(function($){var k=(v(),j)[$+52>>>2>>>0];$=(v(),j)[$+56>>>2>>>0],kh(k,k-$),se(k)})(m.Tc),tn(m.Tc,0,0,1,0,0),Tc(),ja(m.Tc),N||(wh(),N=!0);try{Vm(m.Pd,m.dd)}catch($){if($!="unwind")throw $}}else m.target!=="setimmediate"&&(c==="checkMailbox"?N&&Fr():c&&(E(`worker: received unknown command ${c}`),E(m)))}catch($){throw bh(),$}};var N=!1;self.onunhandledrejection=d=>{throw d.reason||d},self.onmessage=o}var D,H,G,Q,R,j,F,X,le,W,me,P=!1;function U(){var o=mt.buffer;t.HEAP8=D=new Int8Array(o),G=new Int16Array(o),t.HEAPU8=H=new Uint8Array(o),Q=new Uint16Array(o),t.HEAP32=R=new Int32Array(o),t.HEAPU32
6=j=new Uint32Array(o),F=new Float32Array(o),X=new Float64Array(o),le=new BigInt64Array(o),W=new BigUint64Array(o)}function J(){P=!0,a?x():nt.tb()}function ee(o){throw E(o="Aborted("+o+")"),C=!0,o=new WebAssembly.RuntimeError(o+". Build with -sASSERTIONS for more info."),S==null||S(o),o}function $e(){return{a:{ma:cy,hb:py,g:Gm,J:Hm,f:Fm,o:jm,h:Km,ha:Qm,b:Zm,T:Ym,Ia:Oc,n:Xm,_:Nc,Ya:Dc,Ea:Pc,Ga:Uc,Za:qc,Wa:Wc,Pa:Lc,Va:Vc,ka:Gc,Fa:Hc,Ca:Fc,Xa:jc,Da:Kc,cb:Jm,ea:eg,xa:tg,va:ig,da:ng,O:sg,H:og,wa:ug,Z:mg,ya:gg,Sa:yg,Aa:wg,Ja:bg,ta:$g,fa:vg,Ra:ja,$a:xg,R:Ig,s:Og,c:Ha,ib:Rg,y:Bg,M:Mg,D:Ng,m:Dg,t:rh,jb:Pg,I:Ug,S:qg,j:Wg,v:Lg,r:Vg,l:Gg,Ma:Hg,Na:Fg,Oa:jg,Ka:sh,La:oh,ua:uh,eb:Qg,bb:Yg,u:Xg,aa:Jg,ga:ey,ab:Zg,V:ty,_a:ry,Ba:iy,F:Kg,U:ay,la:Yr,za:sy,gb:ny,fb:oy,Ta:ch,Ua:hh,Ha:dr,$:fh,ja:mh,Qa:gh,ia:yh,lb:Qy,na:Ly,mb:Ky,oa:Wy,G:Oy,d:gy,q:fy,w:hy,B:Ty,pb:Py,K:zy,x:_y,pa:Uy,X:Vy,ba:Dy,nb:jy,ob:Fy,ra:Ry,qa:Ny,qb:By,N:Cy,Y:qy,e:yy,A:wy,k:my,kb:Zy,p:$y,z:vy,C:by,E:xy,L:Iy,rb:Ay,Q:Gy,ca:Ey,W:Hy,sb:ky,sa:Sy,P:My,i:ly,a:mt,db:De}}}async function pt(){function o(c,$){var k=nt=c.exports;c={};for(let[z,B]of Object.entries(k))typeof B=="function"?(k=Sg(B),c[z]=k):c[z]=B;return nt=c,nt=(function(){var z=nt,B=V=>ne=>V(ne)>>>0,L=V=>()=>V()>>>0;return(z=Object.assign({},z)).ub=B(z.ub),z.Yb=L(z.Yb),z._b=B(z._b),z.mc=B(z.mc),z.nc=L(z.nc),z.rc=B(z.rc),z})(),Sc.push(nt.$b),_h=(c=nt).ub,wh=c.vb,t._OrtInit=c.wb,t._OrtGetLastError=c.xb,t._OrtCreateSessionOptions=c.yb,t._OrtAppendExecutionProvider=c.zb,t._OrtAddFreeDimensionOverride=c.Ab,t._OrtAddSessionConfigEntry=c.Bb,t._OrtReleaseSessionOptions=c.Cb,t._OrtCreateSession=c.Db,t._OrtReleaseSession=c.Eb,t._OrtGetInputOutputCount=c.Fb,t._OrtGetInputOutputMetadata=c.Gb,t._OrtFree=c.Hb,t._OrtCreateTensor=c.Ib,t._OrtGetTensorData=c.Jb,t._OrtReleaseTensor=c.Kb,t._OrtCreateRunOptions=c.Lb,t._OrtAddRunConfigEntry=c.Mb,t._OrtReleaseRunOptions=c.Nb,t._OrtCreateBinding=c.Ob,t._OrtBindInput=c.Pb,t._OrtBindOutput=c.Qb,t._OrtClearBoundOutputs=c.Rb,t._OrtReleaseBinding=c.Sb,t._OrtRunWithBinding=c.Tb,t._OrtRun=c.Ub,t._OrtEndProfiling=c.Vb,t._JsepOutput=c.Wb,t._JsepGetNodeName=c.Xb,Xr=c.Yb,Xe=t._free=c.Zb,hr=t._malloc=c._b,tn=c.bc,bh=c.cc,$h=c.dc,vh=c.ec,rn=c.fc,xh=c.gc,Sh=c.hc,ue=c.ic,fr=c.jc,kh=c.kc,se=c.lc,an=c.mc,oe=c.nc,Th=c.oc,nn=c.pc,Ih=c.qc,Eh=c.rc,zh=c.sc,sn=c.tc,Ch=c.uc,Ah=c.vc,Oh=c.wc,Rh=c.xc,Bh=c.yc,Mh=c.zc,Nh=c.Ac,Dh=c.Bc,Ph=c.Cc,Uh=c.Dc,qh=c.Ec,Wh=c.Fc,Lh=c.Gc,Vh=c.Hc,Gh=c.Ic,Hh=c.Jc,Fh=c.Kc,jh=c.Lc,Kh=c.Mc,Qh=c.Nc,Zh=c.Oc,Yh=c.Pc,Xh=c.Rc,Jh=c.Sc,ef=c.bd,tf=c.cd,rf=c.hd,af=c.kd,nf=c.ld,sf=c.md,of=c.nd,uf=c.od,lf=c.pd,df=c.qd,pf=c.rd,cf=c.wd,hf=c.Rd,ff=c.Sd,mf=c.Td,gf=c.Ud,y=$,nt}var d,m=$e();return t.instantiateWasm?new Promise(c=>{t.instantiateWasm(m,($,k)=>{c(o($,k))})}):a?o(new WebAssembly.Instance(y,$e()),y):(me??(me=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",f):f+"ort-wasm-simd-threaded.jsep.wasm":new URL("/assets/ort-wasm-simd-threaded.jsep-C887KxcQ.wasm",self.location.href).href),d=await(async function(c){var $=me;if(!g&&!A($))try{var k=fetch($,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(k,c)}catch(z){E(`wasm streaming compile failed: ${z}`),E("falling back to ArrayBuffer instantiation")}return(async function(z,B){try{var L=await(async function(V){if(!g)try{var ne=await u(V);return new Uint8Array(ne)}catch{}if(V==me&&g)V=new Uint8Array(g);else{if(!l)throw"both async and sync fetching of the wasm failed";V=l(V)}return V})(z);return await WebAssembly.instantiate(L,B)}catch(V){E(`failed to asynchronously prepare wasm: ${V}`),ee(V)}})($,c)})(m),o(d.instance,d.module))}class ct{constructor(d){wf(this,"name","ExitStatus");this.message=`Program terminated with exit(${d})`,this.status=d}}var lr=o=>{o.terminate(),o.onmessage=()=>{}},qr=[],Ne=0,Ae=null,ht=o=>{ft.length==0&&(Ec(),Ic(ft[0]));var d=ft.pop();if(!d)return 6;pr.push(d),Rt[o.Tc]=d,d.Tc=o.Tc;var m={Uc:"run",Pd:o.Od,dd:o.dd,Tc:o.Tc};return d.postMessage(m,o.ud),0},we=0,ie=(o,d,...m)=>{var c,$=16*m.length,k=oe(),z=an($),B=z>>>3;for(c of m)typeof c=="bigint"?((v(),le)[B++>>>0]=1n,(v(),le)[B++>>>0]=c):((v(),le)[B++>>>
vendor: 22,221 bytes, lines 6-10
60]=0n,(v(),X)[B++>>>0]=c);return o=$h(o,0,$,z,d),se(k),o};function De(o){if(a)return ie(0,1,o);if(_=o,!(0<we)){for(var d of pr)lr(d);for(d of ft)lr(d);ft=[],pr=[],Rt={},C=!0}p(0,new ct(o))}function Wr(o){if(a)return ie(1,0,o);dr(o)}var dr=o=>{if(_=o,a)throw Wr(o),"unwind";De(o)},ft=[],pr=[],Sc=[],Rt={},kc=o=>{var d=o.Tc;delete Rt[d],ft.push(o),pr.splice(pr.indexOf(o),1),o.Tc=0,vh(d)};function Tc(){Sc.forEach(o=>o())}var Ic=o=>new Promise(d=>{o.onmessage=$=>{var k=$.data;if($=k.Uc,k.ad&&k.ad!=Xr()){var z=Rt[k.ad];z?z.postMessage(k,k.ud):E(`Internal error! Worker sent a message "${$}" to target pthread ${k.ad}, but that thread no longer exists!`)}else $==="checkMailbox"?Fr():$==="spawnThread"?ht(k):$==="cleanupThread"?Hr(()=>{kc(Rt[k.Qd])}):$==="loaded"?(o.loaded=!0,d(o)):k.target==="setimmediate"?o.postMessage(k):$==="uncaughtException"?o.onerror(k.error):$==="callHandler"?t[k.zd](...k.args):$&&E(`worker sent an unknown command ${$}`)},o.onerror=$=>{throw E(`worker sent an error! ${$.filename}:${$.lineno}: ${$.message}`),$};var m,c=[];for(m of[])t.propertyIsEnumerable(m)&&c.push(m);o.postMessage({Uc:"load",Ad:c,Vd:mt,Wd:y})});function Ec(){var o=new Worker((()=>{let d=URL;return self.location.href>"file:"&&self.location.href<"file;"?new d("ort.bundle.min.mjs",self.location.href):new URL(self.location.href)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});ft.push(o)}var mt,Vm=(o,d)=>{we=0,o=sn(o,d),0<we?_=o:rn(o)},Lr=[],Vr=0;function Gm(o){var d=new Wa(o>>>=0);return(v(),D)[d.Vc+12>>>0]==0&&(zc(d,!0),Vr--),Cc(d,!1),Lr.push(d),Eh(o)}var Vt=0,Hm=()=>{ue(0,0);var o=Lr.pop();Th(o.ed),Vt=0};function zc(o,d){d=d?1:0,(v(),D)[o.Vc+12>>>0]=d}function Cc(o,d){d=d?1:0,(v(),D)[o.Vc+13>>>0]=d}class Wa{constructor(d){this.ed=d,this.Vc=d-24}}var La=o=>{var d=Vt;if(!d)return fr(0),0;var m=new Wa(d);(v(),j)[m.Vc+16>>>2>>>0]=d;var c=(v(),j)[m.Vc+4>>>2>>>0];if(!c)return fr(0),d;for(var $ of o){if($===0||$===c)break;if(Ih($,c,m.Vc+16))return fr($),d}return fr(c),d};function Fm(){return La([])}function jm(o){return La([o>>>0])}function Km(o,d,m,c){return La([o>>>0,d>>>0,m>>>0,c>>>0])}var Qm=()=>{var o=Lr.pop();o||ee("no exception to throw");var d=o.ed;throw(v(),D)[o.Vc+13>>>0]==0&&(Lr.push(o),Cc(o,!0),zc(o,!1),Vr++),nn(d),Vt=d};function Zm(o,d,m){var c=new Wa(o>>>=0);throw d>>>=0,m>>>=0,(v(),j)[c.Vc+16>>>2>>>0]=0,(v(),j)[c.Vc+4>>>2>>>0]=d,(v(),j)[c.Vc+8>>>2>>>0]=m,nn(o),Vr++,Vt=o}var Ym=()=>Vr;function Ac(o,d,m,c){return a?ie(2,1,o,d,m,c):Oc(o,d,m,c)}function Oc(o,d,m,c){if(o>>>=0,d>>>=0,m>>>=0,c>>>=0,!globalThis.SharedArrayBuffer)return 6;var $=[];return a&&$.length===0?Ac(o,d,m,c):(o={Od:m,Tc:o,dd:c,ud:$},a?(o.Uc="spawnThread",postMessage(o,$),0):ht(o))}function Xm(o){throw Vt||(Vt=o>>>0),Vt}var Rc=globalThis.TextDecoder&&new TextDecoder,Bc=(o,d,m,c)=>{if(m=d+m,c)return m;for(;o[d]&&!(d>=m);)++d;return d},Mc=(o,d=0,m,c)=>{if(16<(m=Bc(o,d>>>=0,m,c))-d&&o.buffer&&Rc)return Rc.decode(o.buffer instanceof ArrayBuffer?o.subarray(d,m):o.slice(d,m));for(c="";d<m;){var $=o[d++];if(128&$){var k=63&o[d++];if((224&$)==192)c+=String.fromCharCode((31&$)<<6|k);else{var z=63&o[d++];65536>($=(240&$)==224?(15&$)<<12|k<<6|z:(7&$)<<18|k<<12|z<<6|63&o[d++])?c+=String.fromCharCode($):($-=65536,c+=String.fromCharCode(55296|$>>10,56320|1023&$))}}else c+=String.fromCharCode($)}return c},Se=(o,d,m)=>(o>>>=0)?Mc((v(),H),o,d,m):"";function Nc(o,d,m){return a?ie(3,1,o,d,m):0}function Dc(o,d){if(a)return ie(4,1,o,d)}function Pc(o,d){if(a)return ie(5,1,o,d)}function Uc(o,d,m){if(a)return ie(6,1,o,d,m)}function qc(o,d,m){return a?ie(7,1,o,d,m):0}function Wc(o,d){if(a)return ie(8,1,o,d)}function Lc(o,d,m){if(a)return ie(9,1,o,d,m)}function Vc(o,d,m,c){if(a)return ie(10,1,o,d,m,c)}function Gc(o,d,m,c){if(a)return ie(11,1,o,d,m,c)}function Hc(o,d,m,c){if(a)return ie(12,1,o,d,m,c)}function Fc(o){if(a)return ie(13,1,o)}function jc(o,d){if(a)return ie(14,1,o,d)}function Kc(o,d,m){if(a)return ie(15,1,o,d,m)}var Jm=()=>ee(""),Ze=o=>{o>>>=0;for(var d="";;){var m=(v(),H)[o++>>>0];if(!m)return d;d+=String.fromCharCode(m)}},Va={},Ga={},Gt=class extends Error{constructor(o){super(o),this.name="BindingError"}};function at(o,d,m={}){return(function(c,$,k={}){var z=$.name;if(!c)throw new Gt(`type "${z}" must have a positive integer typeid pointer`);if(Ga.hasOwnProperty(c)){if(k.Bd)return;throw new Gt(`Cannot register type '${z}' twice`)}Ga[c]=$,Va.hasOwnProperty(c)&&($=Va[c],delete Va[c],$.forEach(B=>B()))})(o,d,m)}var Qc=(o,d,m)=>{switch(d){case 1:return m?c=>(v(),D)[c>>>0]:c=>(v(),H)[c>>>0];case 2:return m?c=>(v(),G)[c>>>1>>>0]:c=>(v(),Q)[c>>>1>>>0];case 4:return m?c=>(v(),R)[c>>>2>>>0]:c=>(v(),j)[c>>>2>>>0];case 8:return m?c=>(v(),le)[c>>>3>>>0]:c=>(v(),W)[c>>>3>>>0];default:throw new TypeError(`invalid integer width (${d}): ${o}`)}};function eg(o,d,m,c,$){o>>>=0,m>>>=0,d=Ze(d>>>0);let k=z=>z;if(c=c===0n){let z=8*m;k=B=>BigInt.asUintN(z,B),$=k($)}at(o,{name:d,Qc:k,Xc:(z,B)=>(typeof B=="number"&&(B=BigInt(B)),B),Wc:Qc(d,m,!c),Yc:null})}function tg(o,d,m,c){at(o>>>=0,{name:d=Ze(d>>>0),Qc:function($){return!!$},Xc:function($,k){return k?m:c},Wc:function($){return this.Qc((v(),H)[$>>>0])},Yc:null})}var Zc=[],Bt=[0,1,,1,null,1,!0,1,!1,1];function Ha(o){9<(o>>>=0)&&--Bt[o+1]==0&&(Bt[o]=void 0,Zc.push(o))}var Ue=o=>{if(!o)throw new Gt(`Cannot use deleted val. handle = ${o}`);return Bt[o]},je=o=>{switch(o){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let d=Zc.pop()||Bt.length;return Bt[d]=o,Bt[d+1]=1,d}};function Fa(o){return this.Qc((v(),j)[o>>>2>>>0])}var rg={name:"emscripten::val",Qc:o=>{var d=Ue(o);return Ha(o),d},Xc:(o,d)=>je(d),Wc:Fa,Yc:null};function ig(o){return at(o>>>0,rg)}var ag=(o,d)=>{switch(d){case 4:return function(m){return this.Qc((v(),F)[m>>>2>>>0])};case 8:return function(m){return this.Qc((v(),X)[m>>>3>>>0])};default:throw new TypeError(`invalid float width (${d}): ${o}`)}};function ng(o,d,m){m>>>=0,at(o>>>=0,{name:d=Ze(d>>>0),Qc:c=>c,Xc:(c,$)=>$,Wc:ag(d,m),Yc:null})}function sg(o,d,m,c,$){o>>>=0,m>>>=0,d=Ze(d>>>0);let k=B=>B;if(c===0){var z=32-8*m;k=B=>B<<z>>>z,$=k($)}at(o,{name:d,Qc:k,Xc:(B,L)=>L,Wc:Qc(d,m,c!==0),Yc:null})}function og(o,d,m){function c(k){var z=(v(),j)[k>>>2>>>0];return k=(v(),j)[k+4>>>2>>>0],new $((v(),D).buffer,k,z)}var $=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][d];at(o>>>=0,{name:m=Ze(m>>>0),Qc:c,Wc:c},{Bd:!0})}var gt=(o,d,m)=>{var c=(v(),H);if(d>>>=0,0<m){var $=d;m=d+m-1;for(var k=0;k<o.length;++k){var z=o.codePointAt(k);if(127>=z){if(d>=m)break;c[d++>>>0]=z}else if(2047>=z){if(d+1>=m)break;c[d++>>>0]=192|z>>6,c[d++>>>0]=128|63&z}else if(65535>=z){if(d+2>=m)break;c[d++>>>0]=224|z>>12,c[d++>>>0]=128|z>>6&63,c[d++>>>0]=128|63&z}else{if(d+3>=m)break;c[d++>>>0]=240|z>>18,c[d++>>>0]=128|z>>12&63,c[d++>>>0]=128|z>>6&63,c[d++>>>0]=128|63&z,k++}}c[d>>>0]=0,o=d-$}else o=0;return o},Gr=o=>{for(var d=0,m=0;m<o.length;++m){var c=o.charCodeAt(m);127>=c?d++:2047>=c?d+=2:55296<=c&&57343>=c?(d+=4,++m):d+=3}return d};function ug(o,d){at(o>>>=0,{name:d=Ze(d>>>0),Qc(m){var c=(v(),j)[m>>>2>>>0];return c=Se(m+4,c,!0),Xe(m),c},Xc(m,c){c instanceof ArrayBuffer&&(c=new Uint8Array(c));var $=typeof c=="string";if(!($||ArrayBuffer.isView(c)&&c.BYTES_PER_ELEMENT==1))throw new Gt("Cannot pass non-string to std::string");var k=$?Gr(c):c.length,z=hr(4+k+1),B=z+4;return(v(),j)[z>>>2>>>0]=k,$?gt(c,B,k+1):(v(),H).set(c,B>>>0),m!==null&&m.push(Xe,z),z},Wc:Fa,Yc(m){Xe(m)}})}var Yc=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,lg=(o,d,m)=>{if(o>>>=1,16<(d=Bc((v(),Q),o,d/2,m))-o&&Yc)return Yc.decode((v(),Q).slice(o,d));for(m="";o<d;++o){var c=(v(),Q)[o>>>0];m+=String.fromCharCode(c)}return m},dg=(o,d,m)=>{if(m??(m=2147483647),2>m)return 0;var c=d;m=(m-=2)<2*o.length?m/2:o.length;for(var $=0;$<m;++$){var k=o.charCodeAt($);(v(),G)[d>>>1>>>0]=k,d+=2}return(v(),G)[d>>>1>>>0]=0,d-c},pg=o=>2*o.length,cg=(o,d,m)=>{var c="";o>>>=2;for(var $=0;!($>=d/4);$++){var k=(v(),j)[o+$>>>0];if(!k&&!m)break;c+=String.fromCodePoint(k)}return c},hg=(o,d,m)=>{if(d>>>=0,m??(m=2147483647),4>m)return 0;var c=d;m=c+m-4;for(var $=0;$<o.length;++$){var k=o.codePointAt($);if(65535<k&&$++,(v(),R)[d>>>2>>>0]=k,(d+=4)+4>m)break}return(v(),R)[d>>>2>>>0]=0,d-c},fg=o=>{for(var d=0,m=0;m<o.length;++m)65535<o.codePointAt(m)&&m++,d+=4;return d};function mg(o,d,m){if(o>>>=0,d>>>=0,m=Ze(m>>>=0),d===2)var c=lg,$=dg,k=pg;else c=cg,$=hg,k=fg;at(o,{name:m,Qc:z=>{var B=(v(),j)[z>>>2>>>0];return B=c(z+4,B*d,!0),Xe(z),B},Xc:(z,B)=>{if(typeof B!="string")throw new Gt(`Cannot pass non-string to C++ string type ${m}`);var L=k(B),V=hr(4+L+d);return(v(),j)[V>>>2>>>0]=L/d,$(B,V+4,L+d),z!==null&&z.push(Xe,V),V},Wc:Fa,Yc(z){Xe(z)}})}function gg(o,d){at(o>>>=0,{Cd:!0,name:d=Ze(d>>>0),Qc:()=>{},Xc:()=>{}})}function yg(o){tn(o>>>0,!i,1,!r,131072,!1),Tc()}var Hr=o=>{if(!C)try{if(o(),!(0<we))try{a?Xr()&&rn(_):dr(_)}catch(d){d instanceof ct||d=="unwind"||p(0,d)}}catch(d){d instanceof ct||d=="unwind"||p(0,d)}},_g=!Atomics.waitAsync||((_f=globalThis.navigator)==null?void 0:_f.userAgent)&&91>Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function ja(o){o>>>=0,_g||(Atomics.waitAsync((v(),R),o>>>2,o).value.then(Fr),o+=128,Atomics.store((v(),R),o>>>2,1))}var Fr=()=>Hr(()=>{var o=Xr();o&&(ja(o),Sh())});function wg(o,d){(o>>>=0)==d>>>0?setTimeout(Fr):a?postMessage({ad:o,Uc:"checkMailbox"}):(o=Rt[o])&&o.postMessage({Uc:"checkMailbox"})}var Ka=[];function bg(o,d,m,c,$){for(d>>>=0,$>>>=0,Ka.length=0,m=$>>>3,c=$+c>>>3;m<c;){var k;k=(v(),le)[m++>>>0]?(v(),le)[m++>>>0]:(v(),X)[m++>>>0],Ka.push(k)}return(d?on[d]:dy[o])(...Ka)}var $g=()=>{we=0};function vg(o){o>>>=0,a?postMessage({Uc:"cleanupThread",Qd:o}):kc(Rt[o])}function xg(o){}var jr=o=>{try{o()}catch(d){ee(d)}};function Sg(o){var d=(...m)=>{Kr.push(o);try{return o(...m)}finally{C||(Kr.pop(),Ye&&yt===1&&Kr.length===0&&(yt=0,we+=1,jr(ff),typeof Fibers<"u"&&Fibers.ce()))}};return eh.set(o,d),d}var yt=0,Ye=null,Xc=0,Kr=[],Qa=new Map,Jc=new Map,eh=new Map,kg=0,Za=null,Tg=[],th=o=>(function(d){if(!C){if(yt===0){var m=!1,c=!1;d(($=0)=>{if(!C&&(Xc=$,m=!0,c)){yt=2,jr(()=>mf(Ye)),typeof MainLoop<"u"&&MainLoop.yd&&MainLoop.resume(),$=!1;try{var k=(function(){var L=(v(),R)[Ye+8>>>2>>>0];return L=Jc.get(L),L=eh.get(L),--we,L()})()}catch(L){k=L,$=!0}var z=!1;if(!Ye){var B=Za;B&&(Za=null,($?B.reject:B.resolve)(k),z=!0)}if($&&!z)throw k}}),c=!0,m||(yt=1,Ye=(function(){var $=hr(65548),k=$+12;if((v(),j)[$>>>2>>>0]=k,(v(),j)[$+4>>>2>>>0]=k+65536,k=Kr[0],!Qa.has(k)){var z=kg++;Qa.set(k,z),Jc.set(z,k)}return k=Qa.get(k),(v(),R)[$+8>>>2>>>0]=k,$})(),typeof MainLoop<"u"&&MainLoop.yd&&MainLoop.pause(),jr(()=>hf(Ye)))}else yt===2?(yt=0,jr(gf),Xe(Ye),Ye=null,Tg.forEach(Hr)):ee(`invalid state: ${yt}`);return Xc}})(d=>{o().then(d)});function Ig(o){return o>>>=0,th(async()=>{var d=await Ue(o);return je(d)})}var Ya=[],Eg=o=>{var d=Ya.length;return Ya.push(o),d},zg=(o,d)=>{for(var m=Array(o),c=0;c<o;++c){var $=c,k=(v(),j)[d+4*c>>>2>>>0],z=Ga[k];if(z===void 0)throw o=`parameter ${c}`,k=_h(k),d=Ze(k),Xe(k),new Gt(`${o} has unknown type ${d}`);m[$]=z}return m},Cg=(o,d,m)=>{var c=[];return o=o(c,m),c.length&&((v(),j)[d>>>2>>>0]=je(c)),o},Ag={},Qr=o=>{var d=Ag[o];return d===void 0?Ze(o):d};function Og(o,d,m){var[c,...$]=zg(o,d>>>0);d=c.Xc.bind(c);var k=$.map(L=>L.Wc.bind(L));o--;var z={toValue:Ue};switch(o=k.map((L,V)=>{var ne=`argFromPtr${V}`;return z[ne]=L,`${ne}(args${V?"+"+8*V:""})`}),m){case 0:var B="toValue(handle)";break;case 2:B="new (toValue(handle))";break;case 3:B="";break;case 1:z.getStringOrSymbol=Qr,B="toValue(handle)[getStringOrSymbol(methodName)]"}return B+=`(${o})`,c.Cd||(z.toReturnWire=d,z.emval_returnValue=Cg,B=`return emval_returnValue(toReturnWire, destructorsRef, ${B})`),B=`return function (handle, methodName, destructorsRef, args) { 7 ${B} 8 }`,m=new Function(Object.keys(z),B)(...Object.values(z)),B=`methodCaller<(${$.map(L=>L.name)}) => ${c.name}>`,Eg(Object.defineProperty(m,"name",{value:B}))}function Rg(o,d){return d>>>=0,(o=Ue(o>>>0))==Ue(d)}function Bg(o){return(o>>>=0)?(o=Qr(o),je(globalThis[o])):je(globalThis)}function Mg(o){return o=Qr(o>>>0),je(t[o])}function Ng(o,d){return d>>>=0,o=Ue(o>>>0),d=Ue(d),je(o[d])}function Dg(o){9<(o>>>=0)&&(Bt[o+1]+=1)}function rh(o,d,m,c,$){return Ya[o>>>0](d>>>0,m>>>0,c>>>0,$>>>0)}function Pg(o,d,m,c,$){return rh(o>>>0,d>>>0,m>>>0,c>>>0,$>>>0)}function Ug(){return je([])}function qg(o){o=Ue(o>>>0);for(var d=Array(o.length),m=0;m<o.length;m++)d[m]=o[m];return je(d)}function Wg(o){return je(Qr(o>>>0))}function Lg(){return je({})}function Vg(o){for(var d=Ue(o>>>=0);d.length;){var m=d.pop();d.pop()(m)}Ha(o)}function Gg(o,d,m){d>>>=0,m>>>=0,o=Ue(o>>>0),d=Ue(d),m=Ue(m),o[d]=m}function Hg(o,d){o=-9007199254740992>o||9007199254740992<o?NaN:Number(o),d>>>=0,o=new Date(1e3*o),(v(),R)[d>>>2>>>0]=o.getUTCSeconds(),(v(),R)[d+4>>>2>>>0]=o.getUTCMinutes(),(v(),R)[d+8>>>2>>>0]=o.getUTCHours(),(v(),R)[d+12>>>2>>>0]=o.getUTCDate(),(v(),R)[d+16>>>2>>>0]=o.getUTCMonth(),(v(),R)[d+20>>>2>>>0]=o.getUTCFullYear()-1900,(v(),R)[d+24>>>2>>>0]=o.getUTCDay(),o=(o.getTime()-Date.UTC(o.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(v(),R)[d+28>>>2>>>0]=o}var ih=o=>o%4==0&&(o%100!=0||o%400==0),ah=[0,31,60,91,121,152,182,213,244,274,305,335],nh=[0,31,59,90,120,151,181,212,243,273,304,334];function Fg(o,d){o=-9007199254740992>o||9007199254740992<o?NaN:Number(o),d>>>=0,o=new Date(1e3*o),(v(),R)[d>>>2>>>0]=o.getSeconds(),(v(),R)[d+4>>>2>>>0]=o.getMinutes(),(v(),R)[d+8>>>2>>>0]=o.getHours(),(v(),R)[d+12>>>2>>>0]=o.getDate(),(v(),R)[d+16>>>2>>>0]=o.getMonth(),(v(),R)[d+20>>>2>>>0]=o.getFullYear()-1900,(v(),R)[d+24>>>2>>>0]=o.getDay();var m=(ih(o.getFullYear())?ah:nh)[o.getMonth()]+o.getDate()-1|0;(v(),R)[d+28>>>2>>>0]=m,(v(),R)[d+36>>>2>>>0]=-60*o.getTimezoneOffset(),m=new Date(o.getFullYear(),6,1).getTimezoneOffset();var c=new Date(o.getFullYear(),0,1).getTimezoneOffset();o=0|(m!=c&&o.getTimezoneOffset()==Math.min(c,m)),(v(),R)[d+32>>>2>>>0]=o}function jg(o){o>>>=0;var d=new Date((v(),R)[o+20>>>2>>>0]+1900,(v(),R)[o+16>>>2>>>0],(v(),R)[o+12>>>2>>>0],(v(),R)[o+8>>>2>>>0],(v(),R)[o+4>>>2>>>0],(v(),R)[o>>>2>>>0],0),m=(v(),R)[o+32>>>2>>>0],c=d.getTimezoneOffset(),$=new Date(d.getFullYear(),6,1).getTimezoneOffset(),k=new Date(d.getFullYear(),0,1).getTimezoneOffset(),z=Math.min(k,$);return 0>m?(v(),R)[o+32>>>2>>>0]=+($!=k&&z==c):0<m!=(z==c)&&($=Math.max(k,$),d.setTime(d.getTime()+6e4*((0<m?z:$)-c))),(v(),R)[o+24>>>2>>>0]=d.getDay(),m=(ih(d.getFullYear())?ah:nh)[d.getMonth()]+d.getDate()-1|0,(v(),R)[o+28>>>2>>>0]=m,(v(),R)[o>>>2>>>0]=d.getSeconds(),(v(),R)[o+4>>>2>>>0]=d.getMinutes(),(v(),R)[o+8>>>2>>>0]=d.getHours(),(v(),R)[o+12>>>2>>>0]=d.getDate(),(v(),R)[o+16>>>2>>>0]=d.getMonth(),(v(),R)[o+20>>>2>>>0]=d.getYear(),o=d.getTime(),BigInt(isNaN(o)?-1:o/1e3)}function sh(o,d,m,c,$,k,z){return a?ie(16,1,o,d,m,c,$,k,z):-52}function oh(o,d,m,c,$,k){if(a)return ie(17,1,o,d,m,c,$,k)}var cr={},Kg=()=>performance.timeOrigin+performance.now();function uh(o,d){if(a)return ie(18,1,o,d);if(cr[o]&&(clearTimeout(cr[o].id),delete cr[o]),!d)return 0;var m=setTimeout(()=>{delete cr[o],Hr(()=>xh(o,performance.timeOrigin+performance.now()))},d);return cr[o]={id:m,be:d},0}function Qg(o,d,m,c){o>>>=0,d>>>=0,m>>>=0,c>>>=0;var $=new Date().getFullYear(),k=new Date($,0,1).getTimezoneOffset();$=new Date($,6,1).getTimezoneOffset();var z=Math.max(k,$);(v(),j)[o>>>2>>>0]=60*z,(v(),R)[d>>>2>>>0]=+(k!=$),o=(d=B=>{var L=Math.abs(B);return`UTC${0<=B?"-":"+"}${String(Math.floor(L/60)).padStart(2,"0")}${String(L%60).padStart(2,"0")}`})(k),d=d($),$<k?(gt(o,m,17),gt(d,c,17)):(gt(o,c,17),gt(d,m,17))}var Zg=()=>Date.now();function Yg(o,d,m){return m>>>=0,0<=o&&3>=o?(o===0?o=Date.now():o=performance.timeOrigin+performance.now(),o=Math.round(1e6*o),(v(),le)[m>>>3>>>0]=BigInt(o),0):28}var Xa=[],lh=(o,d)=>{Xa.length=0;for(var m;m=(v(),H)[o++>>>0];){var c=m!=105;d+=(c&=m!=112)&&d%8?4:0,Xa.push(m==112?(v(),j)[d>>>2>>>0]:m==106?(v(),le)[d>>>3>>>0]:m==105?(v(),R)[d>>>2>>>0]:(v(),X)[d>>>3>>>0]),d+=c?8:4}return Xa};function Xg(o,d,m){return o>>>=0,d=lh(d>>>0,m>>>0),on[o](...d)}function Jg(o,d,m){return o>>>=0,d=lh(d>>>0,m>>>0),on[o](...d)}var ey=()=>{};function ty(o,d){return E(Se(o>>>0,d>>>0))}var ry=()=>{throw we+=1,"unwind"};function iy(){return 4294901760}var ay=()=>navigator.hardwareConcurrency,Mt={},Zr=o=>{var d;return(d=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(o))?+d[1]:(d=/:(\d+):\d+(?:\)|$)/.exec(o))?2147483648|+d[1]:0},dh=o=>{for(var d of o)(o=Zr(d))&&(Mt[o]=d)};function ny(){var o=Error().stack.toString().split(` 9`);return o[0]=="Error"&&o.shift(),dh(o),Mt.sd=Zr(o[3]),Mt.Md=o,Mt.sd}function Yr(o){if(!(o=Mt[o>>>0]))return 0;var d;if(d=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(o))o=d[1];else if(d=/^\s+at (.*) \(.*\)$/.exec(o))o=d[1];else{if(!(d=/^(.+?)@/.exec(o)))return 0;o=d[1]}Xe(Yr.td??0),d=Gr(o)+1;var m=hr(d);return m&>(o,m,d),Yr.td=m,Yr.td}function sy(o){o>>>=0;var d=(v(),H).length;if(o<=d||4294901760<o)return!1;for(var m=1;4>=m;m*=2){var c=d*(1+.2/m);c=Math.min(c,o+100663296);e:{c=(Math.min(4294901760,65536*Math.ceil(Math.max(o,c)/65536))-mt.buffer.byteLength+65535)/65536|0;try{mt.grow(c),U();var $=1;break e}catch{}$=void 0}if($)return!0}return!1}function oy(o,d,m){if(o>>>=0,d>>>=0,Mt.sd==o)var c=Mt.Md;else(c=Error().stack.toString().split(` 10`))[0]=="Error"&&c.shift(),dh(c);for(var $=3;c[$]&&Zr(c[$])!=o;)++$;for(o=0;o<m&&c[o+$];++o)(v(),R)[d+4*o>>>2>>>0]=Zr(c[o+$]);return o}var Ja,en={},ph=()=>{var c;if(!Ja){var o,d={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(((c=globalThis.navigator)==null?void 0:c.language)??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(o in en)en[o]===void 0?delete d[o]:d[o]=en[o];var m=[];for(o in d)m.push(`${o}=${d[o]}`);Ja=m}return Ja};function ch(o,d){if(a)return ie(19,1,o,d);o>>>=0,d>>>=0;var m,c=0,$=0;for(m of ph()){var k=d+c;(v(),j)[o+$>>>2>>>0]=k,c+=gt(m,k,1/0)+1,$+=4}return 0}function hh(o,d){if(a)return ie(20,1,o,d);o>>>=0,d>>>=0;var m=ph();for(var c of((v(),j)[o>>>2>>>0]=m.length,o=0,m))o+=Gr(c)+1;return(v(),j)[d>>>2>>>0]=o,0}function fh(o){return a?ie(21,1,o):52}function mh(o,d,m,c){return a?ie(22,1,o,d,m,c):52}function gh(o,d,m,c){return a?ie(23,1,o,d,m,c):70}var uy=[null,[],[]];function yh(o,d,m,c){if(a)return ie(24,1,o,d,m,c);d>>>=0,m>>>=0,c>>>=0;for(var $=0,k=0;k<m;k++){var z=(v(),j)[d>>>2>>>0],B=(v(),j)[d+4>>>2>>>0];d+=8;for(var L=0;L<B;L++){var V=o,ne=(v(),H)[z+L>>>0],pe=uy[V];ne===0||ne===10?((V===1?T:E)(Mc(pe)),pe.length=0):pe.push(ne)}$+=B}return(v(),j)[c>>>2>>>0]=$,0}function ly(o){return o>>>0}a||(function(){for(var o=t.numThreads-1;o--;)Ec();qr.push(async()=>{var d=(async function(){if(!a)return Promise.all(ft.map(Ic))})();Ne++,await d,--Ne==0&&Ae&&(d=Ae,Ae=null,d())})})(),a||(mt=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),U()),t.wasmBinary&&(g=t.wasmBinary),t.stackSave=()=>oe(),t.stackRestore=o=>se(o),t.stackAlloc=o=>an(o),t.setValue=function(o,d,m="i8"){switch(m.endsWith("*")&&(m="*"),m){case"i1":case"i8":(v(),D)[o>>>0]=d;break;case"i16":(v(),G)[o>>>1>>>0]=d;break;case"i32":(v(),R)[o>>>2>>>0]=d;break;case"i64":(v(),le)[o>>>3>>>0]=BigInt(d);break;case"float":(v(),F)[o>>>2>>>0]=d;break;case"double":(v(),X)[o>>>3>>>0]=d;break;case"*":(v(),j)[o>>>2>>>0]=d;break;default:ee(`invalid type for setValue: ${m}`)}},t.getValue=function(o,d="i8"){switch(d.endsWith("*")&&(d="*"),d){case"i1":case"i8":return(v(),D)[o>>>0];case"i16":return(v(),G)[o>>>1>>>0];case"i32":return(v(),R)[o>>>2>>>0];case"i64":return(v(),le)[o>>>3>>>0];case"float":return(v(),F)[o>>>2>>>0];case"double":return(v(),X)[o>>>3>>>0];case"*":return(v(),j)[o>>>2>>>0];default:ee(`invalid type for getValue: ${d}`)}},t.UTF8ToString=Se,t.stringToUTF8=gt,t.lengthBytesUTF8=Gr;var _h,wh,Xr,Xe,hr,tn,bh,$h,vh,rn,xh,Sh,ue,fr,kh,se,an,oe,Th,nn,Ih,Eh,zh,sn,Ch,Ah,Oh,Rh,Bh,Mh,Nh,Dh,Ph,Uh,qh,Wh,Lh,Vh,Gh,Hh,Fh,jh,Kh,Qh,Zh,Yh,Xh,Jh,ef,tf,rf,af,nf,sf,of,uf,lf,df,pf,cf,hf,ff,mf,gf,nt,dy=[De,Wr,Ac,Nc,Dc,Pc,Uc,qc,Wc,Lc,Vc,Gc,Hc,Fc,jc,Kc,sh,oh,uh,ch,hh,fh,mh,gh,yh],on={929356:(o,d,m,c,$)=>{if(t===void 0||!t.Zc)return 1;if((o=Se(Number(o>>>0))).startsWith("./")&&(o=o.substring(2)),!(o=t.Zc.get(o)))return 2;if(d=Number(d>>>0),m=Number(m>>>0),c=Number(c>>>0),d+m>o.byteLength)return 3;try{let k=o.subarray(d,d+m);switch($){case 0:(v(),H).set(k,c>>>0);break;case 1:t.Xd?t.Xd(c,k):t.Ld(c,k);break;default:return 4}return 0}catch{return 4}},930180:(o,d,m)=>{t.xd(o,(v(),H).subarray(d>>>0,d+m>>>0))},930244:()=>t.Zd(),930286:o=>{t.vd(o)},930323:()=>{t.Ed()},930354:()=>{t.Fd()},930383:()=>{t.Jd()},930408:o=>t.Dd(o),930441:o=>t.Hd(o),930473:(o,d,m)=>{t.jd(Number(o),Number(d),Number(m),!0)},930536:(o,d,m)=>{t.jd(Number(o),Number(d),Number(m))},930593:()=>typeof wasmOffsetConverter<"u",930650:o=>{t.ac("Abs",o,void 0)},930701:o=>{t.ac("Neg",o,void 0)},930752:o=>{t.ac("Floor",o,void 0)},930805:o=>{t.ac("Ceil",o,void 0)},930857:o=>{t.ac("Reciprocal",o,void 0)},930915:o=>{t.ac("Sqrt",o,void 0)},930967:o=>{t.ac("Exp",o,void 0)},931018:o=>{t.ac("Erf",o,void 0)},931069:o=>{t.ac("Sigmoid",o,void 0)},931124:(o,d,m)=>{t.ac("HardSigmoid",o,{alpha:d,beta:m})},931203:o=>{t.ac("Log",o,void 0)},931254:o=>{t.ac("Sin",o,void 0)},931305:o=>{t.ac("Cos",o,void 0)},931356:o=>{t.ac("Tan",o,void 0)},931407:o=>{t.ac("Asin",o,void 0)},931459:o=>{t.ac("Acos",o,void 0)},931511:o=>{t.ac("Atan",o,void 0)},931563:o=>{t.ac("Sinh",o,void 0)},931615:o=>{t.ac("Cosh",o,void 0)},931667:o=>{t.ac("Asinh",o,void 0)},931720:o=>{t.ac("Acosh",o,void 0)},931773:o=>{t.ac("Atanh",o,void 0)},931826:o=>{t.ac("Tanh",o,void 0)},931878:o=>{t.ac("Not",o,void 0)},931929:(o,d,m)=>{t.ac("Clip",o,{min:d,max:m})},931998:o=>{t.ac("Clip",o,void 0)},932050:(o,d)=>{t.ac("Elu",o,{alpha:d})},932108:o=>{t.ac("Gelu",o,void 0)},932160:o=>{t.ac("Relu",o,void 0)},932212:(o,d)=>{t.ac("LeakyRelu",o,{alpha:d})},932276:(o,d)=>{t.ac("ThresholdedRelu",o,{alpha:d})},932346:(o,d)=>{t.ac("Cast",o,{to:d})},932404:o=>{t.ac("Add",o,void 0)},932455:o=>{t.ac("Sub",o,void 0)},932506:o=>{t.ac("Mul",o,void 0)},932557:o=>{t.ac("Div",o,void 0)},932608:o=>{t.ac("Pow",o,void 0)},932659:o=>{t.ac("Equal",o,void 0)},932712:o=>{t.ac("Greater",o,void 0)},932767:o=>{t.ac("GreaterOrEqual",o,void 0)},932829:o=>{t.ac("Less",o,void 0)},932881:o=>
vendor: 5,973 bytes, line 10
10{t.ac("LessOrEqual",o,void 0)},932940:(o,d,m,c,$)=>{t.ac("ReduceMean",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},933115:(o,d,m,c,$)=>{t.ac("ReduceMax",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},933289:(o,d,m,c,$)=>{t.ac("ReduceMin",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},933463:(o,d,m,c,$)=>{t.ac("ReduceProd",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},933638:(o,d,m,c,$)=>{t.ac("ReduceSum",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},933812:(o,d,m,c,$)=>{t.ac("ReduceL1",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},933985:(o,d,m,c,$)=>{t.ac("ReduceL2",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},934158:(o,d,m,c,$)=>{t.ac("ReduceLogSum",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},934335:(o,d,m,c,$)=>{t.ac("ReduceSumSquare",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},934515:(o,d,m,c,$)=>{t.ac("ReduceLogSumExp",o,{keepDims:!!d,noopWithEmptyAxes:!!m,axes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},934695:o=>{t.ac("Where",o,void 0)},934748:(o,d,m)=>{t.ac("Transpose",o,{perm:d?Array.from((v(),R).subarray(Number(d)>>>0,Number(m)>>>0)):[]})},934872:(o,d,m,c)=>{t.ac("DepthToSpace",o,{blocksize:d,mode:Se(m),format:c?"NHWC":"NCHW"})},935005:(o,d,m,c)=>{t.ac("DepthToSpace",o,{blocksize:d,mode:Se(m),format:c?"NHWC":"NCHW"})},935138:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be,_t)=>{t.ac("ConvTranspose",o,{format:L?"NHWC":"NCHW",autoPad:d,dilations:[m],group:c,kernelShape:[$],pads:[k,z],strides:[B],wIsConst:()=>!!(v(),D)[V>>>0],outputPadding:ne?Array.from((v(),R).subarray(Number(ne)>>>0,Number(pe)>>>0)):[],outputShape:ye?Array.from((v(),R).subarray(Number(ye)>>>0,Number(be)>>>0)):[],activation:Se(_t)})},935571:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be)=>{t.ac("ConvTranspose",o,{format:B?"NHWC":"NCHW",autoPad:d,dilations:Array.from((v(),R).subarray(Number(m)>>>0,2+(Number(m)>>>0)>>>0)),group:c,kernelShape:Array.from((v(),R).subarray(Number($)>>>0,2+(Number($)>>>0)>>>0)),pads:Array.from((v(),R).subarray(Number(k)>>>0,4+(Number(k)>>>0)>>>0)),strides:Array.from((v(),R).subarray(Number(z)>>>0,2+(Number(z)>>>0)>>>0)),wIsConst:()=>!!(v(),D)[L>>>0],outputPadding:V?Array.from((v(),R).subarray(Number(V)>>>0,Number(ne)>>>0)):[],outputShape:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(ye)>>>0)):[],activation:Se(be)})},936232:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be,_t)=>{t.ac("ConvTranspose",o,{format:L?"NHWC":"NCHW",autoPad:d,dilations:[m],group:c,kernelShape:[$],pads:[k,z],strides:[B],wIsConst:()=>!!(v(),D)[V>>>0],outputPadding:ne?Array.from((v(),R).subarray(Number(ne)>>>0,Number(pe)>>>0)):[],outputShape:ye?Array.from((v(),R).subarray(Number(ye)>>>0,Number(be)>>>0)):[],activation:Se(_t)})},936665:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be)=>{t.ac("ConvTranspose",o,{format:B?"NHWC":"NCHW",autoPad:d,dilations:Array.from((v(),R).subarray(Number(m)>>>0,2+(Number(m)>>>0)>>>0)),group:c,kernelShape:Array.from((v(),R).subarray(Number($)>>>0,2+(Number($)>>>0)>>>0)),pads:Array.from((v(),R).subarray(Number(k)>>>0,4+(Number(k)>>>0)>>>0)),strides:Array.from((v(),R).subarray(Number(z)>>>0,2+(Number(z)>>>0)>>>0)),wIsConst:()=>!!(v(),D)[L>>>0],outputPadding:V?Array.from((v(),R).subarray(Number(V)>>>0,Number(ne)>>>0)):[],outputShape:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(ye)>>>0)):[],activation:Se(be)})},937326:(o,d)=>{t.ac("GlobalAveragePool",o,{format:d?"NHWC":"NCHW"})},937417:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be)=>{t.ac("AveragePool",o,{format:be?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:k?Array.from((v(),R).subarray(Number(k)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:V?Array.from((v(),R).subarray(Number(V)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(ye)>>>0)):[]})},937896:(o,d)=>{t.ac("GlobalAveragePool",o,{format:d?"NHWC":"NCHW"})},937987:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be)=>{t.ac("AveragePool",o,{format:be?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:k?Array.from((v(),R).subarray(Number(k)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:V?Array.from((v(),R).subarray(Number(V)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(ye)>>>0)):[]})},938466:(o,d)=>{t.ac("GlobalMaxPool",o,{format:d?"NHWC":"NCHW"})},938553:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be)=>{t.ac("MaxPool",o,{format:be?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:k?Array.from((v(),R).subarray(Number(k)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:V?Array.from((v(),R).subarray(Number(V)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(ye)>>>0)):[]})},939028:(o,d)=>{t.ac("GlobalMaxPool",o,{format:d?"NHWC":"NCHW"})},939115:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be)=>{t.ac("MaxPool",o,{format:be?"NHWC":"NCHW",auto_pad:d,ceil_mode:m,count_include_pad:c,storage_order:$,dilations:k?Array.from((v(),R).subarray(Number(k)>>>0,Number(z)>>>0)):[],kernel_shape:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],pads:V?Array.from((v(),R).subarray(Number(V)>>>0,Number(ne)>>>0)):[],strides:pe?Array.from((v(),R).subarray(Number(pe)>>>0,Number(ye)>>>0)):[]})},939590:(o,d,m,c,$)=>{t.ac("Gemm",o,{alpha:d,beta:m,transA:c,transB:$})},939694:o=>{t.ac("MatMul",o,void 0)},939748:(o,d,m,c)=>
vendor: 4,956 bytes, line 10
10{t.ac("ArgMax",o,{keepDims:!!d,selectLastIndex:!!m,axis:c})},939856:(o,d,m,c)=>{t.ac("ArgMin",o,{keepDims:!!d,selectLastIndex:!!m,axis:c})},939964:(o,d)=>{t.ac("Softmax",o,{axis:d})},940027:(o,d)=>{t.ac("Concat",o,{axis:d})},940087:(o,d,m,c,$)=>{t.ac("Split",o,{axis:d,numOutputs:m,splitSizes:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},940243:o=>{t.ac("Expand",o,void 0)},940297:(o,d)=>{t.ac("Gather",o,{axis:Number(d)})},940368:(o,d)=>{t.ac("GatherElements",o,{axis:Number(d)})},940447:(o,d)=>{t.ac("GatherND",o,{batch_dims:Number(d)})},940526:(o,d,m,c,$,k,z,B,L,V,ne)=>{t.ac("Resize",o,{antialias:d,axes:m?Array.from((v(),R).subarray(Number(m)>>>0,Number(c)>>>0)):[],coordinateTransformMode:Se($),cubicCoeffA:k,excludeOutside:z,extrapolationValue:B,keepAspectRatioPolicy:Se(L),mode:Se(V),nearestMode:Se(ne)})},940888:(o,d,m,c,$,k,z)=>{t.ac("Slice",o,{starts:d?Array.from((v(),R).subarray(Number(d)>>>0,Number(m)>>>0)):[],ends:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[],axes:k?Array.from((v(),R).subarray(Number(k)>>>0,Number(z)>>>0)):[]})},941152:o=>{t.ac("Tile",o,void 0)},941204:(o,d,m)=>{t.ac("InstanceNormalization",o,{epsilon:d,format:m?"NHWC":"NCHW"})},941318:(o,d,m)=>{t.ac("InstanceNormalization",o,{epsilon:d,format:m?"NHWC":"NCHW"})},941432:o=>{t.ac("Range",o,void 0)},941485:(o,d)=>{t.ac("Einsum",o,{equation:Se(d)})},941566:(o,d,m,c,$)=>{t.ac("Pad",o,{mode:d,value:m,pads:c?Array.from((v(),R).subarray(Number(c)>>>0,Number($)>>>0)):[]})},941709:(o,d,m,c,$,k)=>{t.ac("BatchNormalization",o,{epsilon:d,momentum:m,spatial:!!$,trainingMode:!!c,format:k?"NHWC":"NCHW"})},941878:(o,d,m,c,$,k)=>{t.ac("BatchNormalization",o,{epsilon:d,momentum:m,spatial:!!$,trainingMode:!!c,format:k?"NHWC":"NCHW"})},942047:(o,d,m)=>{t.ac("CumSum",o,{exclusive:Number(d),reverse:Number(m)})},942144:(o,d,m)=>{t.ac("DequantizeLinear",o,{axis:d,blockSize:m})},942234:(o,d,m,c,$)=>{t.ac("GridSample",o,{align_corners:d,mode:Se(m),padding_mode:Se(c),format:$?"NHWC":"NCHW"})},942404:(o,d,m,c,$)=>{t.ac("GridSample",o,{align_corners:d,mode:Se(m),padding_mode:Se(c),format:$?"NHWC":"NCHW"})},942574:(o,d)=>{t.ac("ScatterND",o,{reduction:Se(d)})},942659:(o,d,m,c,$,k,z,B,L)=>{t.ac("Attention",o,{numHeads:d,isUnidirectional:m,maskFilterValue:c,scale:$,doRotary:k,qkvHiddenSizes:z?Array.from((v(),R).subarray(Number(B)>>>0,Number(B)+z>>>0)):[],pastPresentShareBuffer:!!L})},942931:o=>{t.ac("BiasAdd",o,void 0)},942986:o=>{t.ac("BiasSplitGelu",o,void 0)},943047:o=>{t.ac("FastGelu",o,void 0)},943103:(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be,_t,un)=>{t.ac("Conv",o,{format:pe?"NHWC":"NCHW",auto_pad:d,dilations:m?Array.from((v(),R).subarray(Number(m)>>>0,Number(c)>>>0)):[],group:$,kernel_shape:k?Array.from((v(),R).subarray(Number(k)>>>0,Number(z)>>>0)):[],pads:B?Array.from((v(),R).subarray(Number(B)>>>0,Number(L)>>>0)):[],strides:V?Array.from((v(),R).subarray(Number(V)>>>0,Number(ne)>>>0)):[],w_is_const:()=>!!(v(),D)[Number(ye)>>>0],activation:Se(be),activation_params:_t?Array.from((v(),F).subarray(Number(_t)>>>0,Number(un)>>>0)):[]})},943687:o=>{t.ac("Gelu",o,void 0)},943739:(o,d,m,c,$,k,z,B,L)=>{t.ac("GroupQueryAttention",o,{numHeads:d,kvNumHeads:m,scale:c,softcap:$,doRotary:k,rotaryInterleaved:z,smoothSoftmax:B,localWindowSize:L})},943956:(o,d,m,c)=>{t.ac("LayerNormalization",o,{axis:d,epsilon:m,simplified:!!c})},944067:(o,d,m,c)=>{t.ac("LayerNormalization",o,{axis:d,epsilon:m,simplified:!!c})},944178:(o,d,m,c,$,k)=>{t.ac("MatMulNBits",o,{k:d,n:m,accuracyLevel:c,bits:$,blockSize:k})},944305:(o,d,m,c,$,k)=>{t.ac("MultiHeadAttention",o,{numHeads:d,isUnidirectional:m,maskFilterValue:c,scale:$,doRotary:k})},944464:(o,d)=>{t.ac("QuickGelu",o,{alpha:d})},944528:(o,d,m,c,$)=>{t.ac("RotaryEmbedding",o,{interleaved:!!d,numHeads:m,rotaryEmbeddingDim:c,scale:$})},944667:(o,d,m)=>{t.ac("SkipLayerNormalization",o,{epsilon:d,simplified:!!m})},944769:(o,d,m)=>{t.ac("SkipLayerNormalization",o,{epsilon:d,simplified:!!m})},944871:(o,d,m,c)=>{t.ac("GatherBlockQuantized",o,{gatherAxis:d,quantizeAxis:m,blockSize:c})},944992:o=>{t.Id(o)},945026:(o,d)=>t.Kd(Number(o),Number(d),t.$c.Nd,t.$c.errors)};function py(o,d,m){return th(async()=>{await t.Gd(Number(o),Number(d),Number(m))})}function cy(){return typeof wasmOffsetConverter<"u"}function hy(o,d,m,c){var $=oe();try{return Dh(o,d,m,c)}catch(k){if(se($),k!==k+0)throw k;ue(1,0)}}function fy(o,d,m){var c=oe();try{return Rh(o,d,m)}catch($){if(se(c),$!==$+0)throw $;ue(1,0)}}function my(o,d,m){var c=oe();try{zh(o,d,m)}catch($){if(se(c),$!==$+0)throw $;ue(1,0)}}function gy(o,d){var m=oe();try{return sn(o,d)}catch(c){if(se(m),c!==c+0)throw c;ue(1,0)}}function yy(o){var d=oe();try{Ch(o)}catch(m){if(se(d),m!==m+0)throw m;ue(1,0)}}function _y(o,d,m,c,$,k,z){var B=oe();try{return Mh(o,d,m,c,$,k,z)}catch(L){if(se(B),L!==L+0)throw L;ue(1,0)}}function wy(o,d){var m=oe();try{Ph(o,d)}catch(c){if(se(m),c!==c+0)throw c;ue(1,0)}}function by(o,d,m,c,$,k){var z=oe();try{Ah(o,d,m,c,$,k)}
10catch(B){if(se(z),B!==B+0)throw B;ue(1,0)}}function $y(o,d,m,c){var $=oe();try{Nh(o,d,m,c)}catch(k){if(se($),k!==k+0)throw k;ue(1,0)}}function vy(o,d,m,c,$){var k=oe();try{Oh(o,d,m,c,$)}catch(z){if(se(k),z!==z+0)throw z;ue(1,0)}}function xy(o,d,m,c,$,k,z){var B=oe();try{qh(o,d,m,c,$,k,z)}catch(L){if(se(B),L!==L+0)throw L;ue(1,0)}}function Sy(o,d,m,c,$,k,z){var B=oe();try{Wh(o,d,m,c,$,k,z)}catch(L){if(se(B),L!==L+0)throw L;ue(1,0)}}function ky(o,d,m,c,$,k,z,B){var L=oe();try{Hh(o,d,m,c,$,k,z,B)}catch(V){if(se(L),V!==V+0)throw V;ue(1,0)}}function Ty(o,d,m,c,$){var k=oe();try{return Uh(o,d,m,c,$)}catch(z){if(se(k),z!==z+0)throw z;ue(1,0)}}function Iy(o,d,m,c,$,k,z,B){var L=oe();try{Fh(o,d,m,c,$,k,z,B)}catch(V){if(se(L),V!==V+0)throw V;ue(1,0)}}function Ey(o,d,m,c,$,k,z,B,L,V,ne,pe){var ye=oe();try{Lh(o,d,m,c,$,k,z,B,L,V,ne,pe)}catch(be){if(se(ye),be!==be+0)throw be;ue(1,0)}}function zy(o,d,m,c,$,k){var z=oe();try{return Vh(o,d,m,c,$,k)}catch(B){if(se(z),B!==B+0)throw B;ue(1,0)}}function Cy(o,d,m){var c=oe();try{return jh(o,d,m)}catch($){if(se(c),$!==$+0)throw $;return ue(1,0),0n}}function Ay(o,d,m,c,$,k,z,B,L){var V=oe();try{Bh(o,d,m,c,$,k,z,B,L)}catch(ne){if(se(V),ne!==ne+0)throw ne;ue(1,0)}}function Oy(o){var d=oe();try{return Kh(o)}catch(m){if(se(d),m!==m+0)throw m;ue(1,0)}}function Ry(o,d,m){var c=oe();try{return Qh(o,d,m)}catch($){if(se(c),$!==$+0)throw $;ue(1,0)}}function By(o,d){var m=oe();try{return cf(o,d)}catch(c){if(se(m),c!==c+0)throw c;return ue(1,0),0n}}function My(o,d,m,c,$){var k=oe();try{Zh(o,d,m,c,$)}catch(z){if(se(k),z!==z+0)throw z;ue(1,0)}}function Ny(o){var d=oe();try{return Yh(o)}catch(m){if(se(d),m!==m+0)throw m;return ue(1,0),0n}}function Dy(o,d,m,c,$,k){var z=oe();try{return af(o,d,m,c,$,k)}catch(B){if(se(z),B!==B+0)throw B;ue(1,0)}}function Py(o,d,m,c,$,k){var z=oe();try{return nf(o,d,m,c,$,k)}catch(B){if(se(z),B!==B+0)throw B;ue(1,0)}}function Uy(o,d,m,c,$,k,z,B){var L=oe();try{return Gh(o,d,m,c,$,k,z,B)}catch(V){if(se(L),V!==V+0)throw V;ue(1,0)}}function qy(o,d,m,c,$){var k=oe();try{return sf(o,d,m,c,$)}catch(z){if(se(k),z!==z+0)throw z;return ue(1,0),0n}}function Wy(o,d,m,c){var $=oe();try{return of(o,d,m,c)}catch(k){if(se($),k!==k+0)throw k;ue(1,0)}}function Ly(o,d,m,c){var $=oe();try{return uf(o,d,m,c)}catch(k){if(se($),k!==k+0)throw k;ue(1,0)}}function Vy(o,d,m,c,$,k,z,B,L,V,ne,pe){var ye=oe();try{return lf(o,d,m,c,$,k,z,B,L,V,ne,pe)}catch(be){if(se(ye),be!==be+0)throw be;ue(1,0)}}function Gy(o,d,m,c,$,k,z,B,L,V,ne){var pe=oe();try{tf(o,d,m,c,$,k,z,B,L,V,ne)}catch(ye){if(se(pe),ye!==ye+0)throw ye;ue(1,0)}}function Hy(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be,_t,un){var Yy=oe();try{rf(o,d,m,c,$,k,z,B,L,V,ne,pe,ye,be,_t,un)}catch(ln){if(se(Yy),ln!==ln+0)throw ln;ue(1,0)}}function Fy(o,d,m,c){var $=oe();try{return df(o,d,m,c)}catch(k){if(se($),k!==k+0)throw k;ue(1,0)}}function jy(o,d,m,c,$){var k=oe();try{return pf(o,d,m,c,$)}catch(z){if(se(k),z!==z+0)throw z;ue(1,0)}}function Ky(o,d,m){var c=oe();try{return Xh(o,d,m)}catch($){if(se(c),$!==$+0)throw $;ue(1,0)}}function Qy(o,d,m){var c=oe();try{return Jh(o,d,m)}catch($){if(se(c),$!==$+0)throw $;ue(1,0)}}function Zy(o,d,m,c){var $=oe();try{ef(o,d,m,c)}catch(k){if(se($),k!==k+0)throw k;ue(1,0)}}function Jr(){if(0<Ne)Ae=Jr;else if(a)w==null||w(t),J();else{for(var o=qr;0<o.length;)o.shift()(t);0<Ne?Ae=Jr:(t.calledRun=!0,C||(J(),w==null||w(t)))}}return a||(nt=await pt(),Jr()),t.PTR_SIZE=4,P?t:new Promise((o,d)=>{w=o,S=d})}var On,Rn,qf=q(()=>{var e,t;On=An,Rn=(t=(e=globalThis.self)==null?void 0:e.name)==null?void 0:t.startsWith("em-pthread"),Rn&&An()}),li,di,Bn,Be,Mn,_r,Nn,Dn,pi,Pn,ci,Un,hi,qn,fi=q(()=>{si(),li=typeof location>"u"?void 0:location.origin,di=self.location.href>"file:"&&self.location.href<"file;",Bn=()=>{{if(di){let e=URL;return new URL(new e("ort.bundle.min.mjs",self.location.href).href,li).href}return self.location.href}},Be=Bn(),Mn=()=>{if(Be&&!Be.startsWith("blob:"))return Be.substring(0,Be.lastIndexOf("/")+1)},_r=(e,t)=>{try{let r=t??Be;return(r?new URL(e,r):new URL(e)).origin===li}catch{return!1}},Nn=(e,t)=>{let r=t??Be;try{return(r?new URL(e,r):new URL(e)).href}catch{return}},Dn=(e,t)=>`${t??"./"}${e}`,pi=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},Pn=async e=>(await import(e)).default,ci=(Uf(),Ht(En)).default,Un=async()=>
vendor: 4,219 bytes, line 10
10{if(!Be)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(_r(Be))return[void 0,ci()];let e=await pi(Be);return[e,ci(e)]},hi=(qf(),Ht(Cn)).default,qn=async(e,t,r,i)=>{let a=hi&&!(e||t);if(a)if(Be)a=_r(Be)||i&&!r;else if(i&&!r)a=!0;else throw new Error("cannot determine the script source URL.");if(a)return[void 0,hi];{let s="ort-wasm-simd-threaded.jsep.mjs",n=e??Nn(s,t),u=r&&n&&!_r(n,t),l=u?await pi(n):n??Dn(s,t);return[u?l:void 0,await Pn(l)]}}}),mi,wr,Kt,gi,Wn,Ln,Vn,yi,_e,vt=q(()=>{fi(),wr=!1,Kt=!1,gi=!1,Wn=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Ln=()=>{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}},Vn=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},yi=async e=>{if(wr)return Promise.resolve();if(Kt)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(gi)throw new Error("previous call to 'initializeWebAssembly()' failed.");Kt=!0;let t=e.initTimeout,r=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!Vn())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!Ln())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let i=Wn();r>1&&!i&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=r=1);let a=e.wasmPaths,s=typeof a=="string"?a:void 0,n=a==null?void 0:a.mjs,u=(n==null?void 0:n.href)??n,l=a==null?void 0:a.wasm,p=(l==null?void 0:l.href)??l,h=e.wasmBinary,[f,g]=await qn(u,s,r>1,!!h||!!p),y=!1,_=[];if(t>0&&_.push(new Promise(w=>{setTimeout(()=>{y=!0,w()},t)})),_.push(new Promise((w,S)=>{let x={numThreads:r};if(h)x.wasmBinary=h,x.locateFile=b=>b;else if(p||s)x.locateFile=b=>p??s+b;else if(u&&u.indexOf("blob:")!==0)x.locateFile=b=>new URL(b,u).href;else if(f){let b=Mn();b&&(x.locateFile=I=>b+I)}g(x).then(b=>{Kt=!1,wr=!0,mi=b,w(),f&&URL.revokeObjectURL(f)},b=>{Kt=!1,gi=!0,S(b)})})),await Promise.race(_),y)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},_e=()=>{if(wr&&mi)return mi;throw new Error("WebAssembly is not initialized yet.")}}),We,br,fe,_i=q(()=>{vt(),We=(e,t)=>{let r=_e(),i=r.lengthBytesUTF8(e)+1,a=r._malloc(i);return r.stringToUTF8(e,a,i),t.push(a),a},br=(e,t,r,i)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([a,s])=>{let n=t?t+a:a;if(typeof s=="object")br(s,n+".",r,i);else if(typeof s=="string"||typeof s=="number")i(n,s.toString());else if(typeof s=="boolean")i(n,s?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof s}`)})},fe=e=>{let t=_e(),r=t.stackSave();try{let i=t.PTR_SIZE,a=t.stackAlloc(2*i);t._OrtGetLastError(a,a+i);let s=Number(t.getValue(a,i===4?"i32":"i64")),n=t.getValue(a+i,"*"),u=n?t.UTF8ToString(n):"";throw new Error(`${e} ERROR_CODE: ${s}, ERROR_MESSAGE: ${u}`)}finally{t.stackRestore(r)}}}),Gn,Wf=q(()=>{vt(),_i(),Gn=e=>{let t=_e(),r=0,i=[],a=e||{};try{if((e==null?void 0:e.logSeverityLevel)===void 0)a.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if((e==null?void 0:e.logVerbosityLevel)===void 0)a.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not val
10id: ${e.logVerbosityLevel}`);(e==null?void 0:e.terminate)===void 0&&(a.terminate=!1);let s=0;return(e==null?void 0:e.tag)!==void 0&&(s=We(e.tag,i)),r=t._OrtCreateRunOptions(a.logSeverityLevel,a.logVerbosityLevel,!!a.terminate,s),r===0&&fe("Can't create run options."),(e==null?void 0:e.extra)!==void 0&&br(e.extra,"",new WeakSet,(n,u)=>{let l=We(n,i),p=We(u,i);t._OrtAddRunConfigEntry(r,l,p)!==0&&fe(`Can't set a run config entry: ${n} - ${u}.`)}),[r,i]}catch(s){throw r!==0&&t._OrtReleaseRunOptions(r),i.forEach(n=>t._free(n)),s}}}),Hn,Fn,jn,Qt,Kn,Qn,Lf=q(()=>{vt(),_i(),Hn=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},Fn=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},jn=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},Qt=(e,t,r,i)=>{let a=We(t,i),s=We(r,i);_e()._OrtAddSessionConfigEntry(e,a,s)!==0&&fe(`Can't set a session config entry: ${t} - ${r}.`)},Kn=async(e,t,r)=>{let i=t.executionProviders;for(let a of i){let s=typeof a=="string"?a:a.name,n=[];switch(s){case"webnn":if(s="WEBNN",typeof a!="string"){let f=a==null?void 0:a.deviceType;f&&Qt(e,"deviceType",f,r)}break;case"webgpu":if(s="JS",typeof a!="string"){let f=a;if(f!=null&&f.preferredLayout){if(f.preferredLayout!=="NCHW"&&f.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${f.preferredLayout}`);Qt(e,"preferredLayout",f.preferredLayout,r)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${s}`)}let u=We(s,r),l=n.length,p=0,h=0;if(l>0){p=_e()._malloc(l*_e().PTR_SIZE),r.push(p),h=_e()._malloc(l*_e().PTR_SIZE),r.push(h);for(let f=0;f<l;f++)_e().setValue(p+f*_e().PTR_SIZE,n[f][0],"*"),_e().setValue(h+f*_e().PTR_SIZE,n[f][1],"*")}await _e()._OrtAppendExecutionProvider(e,u,p,h,l)!==0&&fe(`Can't append execution provider: ${s}.`)}},Qn=async e=>{let t=_e(),r=0,i=[],a=e||{};jn(a);try{let s=Hn(a.graphOptimizationLevel??"all"),n=Fn(a.executionMode??"sequential"),u=typeof a.logId=="string"?We(a.logId,i):0,l=a.logSeverityLevel??2;if(!Number.isInteger(l)||l<0||l>4)throw new Error(`log severity level is not valid: ${l}`);let p=a.logVerbosityLevel??0;if(!Number.isInteger(p)||p<0||p>4)throw new Error(`log verbosity level is not valid: ${p}`);let h=typeof a.optimizedModelFilePath=="string"?We(a.optimizedModelFilePath,i):0;if(r=t._OrtCreateSessionOptions(s,!!a.enableCpuMemArena,!!a.enableMemPattern,n,!!a.enableProfiling,0,u,l,p,h),r===0&&fe("Can't create session options."),a.executionProviders&&await Kn(r,a,i),a.enableGraphCapture!==void 0){if(typeof a.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${a.enableGraphCapture}`);Qt(r,"enableGraphCapture",a.enableGraphCapture.toString(),i)}if(a.freeDimensionOverrides)for(let[f,g]of Object.entries(a.freeDimensionOverrides)){if(typeof f!="string")throw new Error(`free dimension override name must be a string: ${f}`);if(typeof g!="number"||!Number.isInteger(g)||g<0)throw new Error(`free dimension override value must be a non-negative integer: ${g}`);let y=We(f,i);t._OrtAddFreeDimensionOverride(r,y,g)!==0&&fe(`Can't set a free dimension override: ${f} - ${g}.`)}return a.extra!==void 0&&br(a.extra,"",new WeakSet,(f,g)=>{Qt(r,f,g,i)}),[r,i]}catch(s){throw r!==0&&t._OrtReleaseSessionOptions(r)!==0&&fe("Can't release session options."),i.forEach(n=>t._free(n)),s}}}),xt,tt,St,$r,vr,wi,bi,$i,te=q(()=>{xt=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},tt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},St=(e,t)=>
vendor: 5,605 bytes, line 10
10{let r=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],i=typeof t=="number"?t:t.reduce((a,s)=>a*s,1);return r>0?Math.ceil(i*r):void 0},$r=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},vr=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},wi=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",bi=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",$i=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),vi,Zn=q(()=>{si(),vi=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),i=r?parseInt(r,10):0;if(i<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let a=t.body.getReader(),s;try{s=new ArrayBuffer(i)}catch(u){if(u instanceof RangeError){let l=Math.ceil(i/65536);s=new WebAssembly.Memory({initial:l,maximum:l}).buffer}else throw u}let n=0;for(;;){let{done:u,value:l}=await a.read();if(u)break;let p=l.byteLength;new Uint8Array(s,n,p).set(l),n+=p}return new Uint8Array(s,0,i)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),Yn,Xn,Jn,es,xi,ts,de,rt=q(()=>{te(),Yn=["V","I","W","E","F"],Xn=(e,t)=>{console.log(`[${Yn[e]},${new Date().toISOString()}]${t}`)},xi=(e,t)=>{Jn=e,es=t},ts=(e,t)=>{let r=vr(e),i=vr(Jn);r>=i&&Xn(r,typeof t=="function"?t():t)},de=(...e)=>{es&&ts(...e)}}),rs,Ut,O,xr,is,as,ns,re=q(()=>{rs=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},Ut=class{static calcShape(e,t,r=!1){let i=e.length,a=t.length;if(i===0)return t;if(a===0)return e;let s=Math.max(e.length,t.length),n=new Array(s);if(r){if(i<2||a<2)return;let u=rs.calcMatMulShape([e[i-2],e[i-1]],[t[a-2],t[a-1]]);if(u===void 0)return;[n[s-2],n[s-1]]=u}for(let u=r?3:1;u<=s;u++){let l=i-u<0?1:e[i-u],p=a-u<0?1:t[a-u];if(l!==p&&l>1&&p>1)return;let h=Math.max(l,p);if(l&&p)n[s-u]=Math.max(l,p);else{if(h>1)return;n[s-u]=0}}return n}static isValidBroadcast(e,t){let r=e.length,i=t.length;if(r>i)return!1;for(let a=1;a<=r;a++)if(e[r-a]!==1&&e[r-a]!==t[i-a])return!1;return!0}},O=class ei{static size(t){return ei.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let i=t.length;if(i===0)return[];let a=new Array(i),s=i-1;for(;s>=0;){if(t[s]%r===0){a[s]=t[s]/r;break}if(r%t[s]!==0)throw new Error("cannot convert shape");a[s]=1,r/=t[s],s--}for(s--;s>=0;s--)a[s]=t[s];return a}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return ei.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return ei.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,i){let a=1;for(let s=r;s<i;s++){if(t[s]<0)throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them.");a*=Number(t[s])}return a}static computeStrides(t){let r=t.length;if(r===0)return[];if(r===1)return[1];let i=new Array(r);i[r-1]=1,i[r-2]=t[r-1];for(let a=r-3;a>=0;--a)i[a]=i[a+1]*t[a+1];return i}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(i=>this.normalizeAxis(i,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(i=>t[i]):t.slice().reverse()}static padShape(t,r){let i=t.length;return t.map((a,s)=>a+r[s]+r[s+i])}static areEqual(t,r){return t.length!==r.length?!1:t.every((i,a)=>i===r[a])}},xr=class mr{static adjustPoolAttributes(t,r,i,a,s,n){if(!t&&i.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let u=0;u<r.length-2;u++)u>=i.length?i.push(r[u+2]):i[u]=r[u+2];for(let u=0;u<i.length;u++)if(u<a.length){if(a[u]<0)throw new Error("strides should be greater than or equal to 1")}else a.push(1);for(let u=0;u<i.length;u++)if(u<s.length){if(s[u]<0)throw new Error("dilations should be greater than or equal to 1")}else s.push(1);for(let u=0;u<i.length*2;u++)if(u<n.length){if(n[u]<0)throw new Error("pad should be greater than or equal to 1")}else n.push(0);for(let u=0;u<i.length;u++){if(i[u]<=0)throw new Error("kernel shapes need to be greater than 0");if(n[u]>=i[u]||n[u+i.length]>=i[u])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,i,a,s,n,u){if(u){if(s.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");
10if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(a.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let l=0;l<t.length-2;l++)mr.adjustPadAndReturnShape(t[l+(n?1:2)],r[l],i[l],a[l],s,l,l+t.length-2,u)}}static computePoolOutputShape(t,r,i,a,s,n,u){if(r.length<=0)throw new Error("input shape must be of size greater than 0");let l=[r[0],r[1]];return mr.computeShapeHelper(t,r,l,i,a,s,n,u),l}static computeConvOutputShape(t,r,i,a,s,n,u){if(t.length<=0||r.length<=0)throw new Error("invalid input tensor dims or invalid filter tensor dims");let l=[t[0],r[0]];return mr.computeShapeHelper(!1,t,l,i,a,s,n,u),l}static computeShapeHelper(t,r,i,a,s,n,u,l){if(t)for(let p=0;p<r.length-2;p++)i.push(1);else for(let p=0;p<r.length-2;p++)i.push(mr.adjustPadAndReturnShape(r[p+2],a[p],s[p],n[p],u,p,p+r.length-2,l))}static adjustPadAndReturnShape(t,r,i,a,s,n,u,l){let p=i*(a-1)+1;if(l&&l!=="NOTSET")switch(l){case"VALID":return s[n]=0,s[u]=0,Math.floor((t-p)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(i!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let h=((t+r-1)/r-1)*r+a-t;return s[n]=Math.floor(l==="SAME_LOWER"?(h+1)/2:h/2),s[u]=h-s[n],Math.floor((t+h-a)/r+1)}default:throw new Error("Unsupported AutoPad type")}else return Math.floor((t+s[n]+s[u]-p)/r+1)}},is=class{static getShapeOfGemmResult(e,t,r,i,a){if(e.length!==2||r.length!==2)throw new Error("shape need to be of size 2");let s,n,u;t?(s=e[1],n=e[0]):(s=e[0],n=e[1]);let l=-1;if(i?(u=r[0],l=1):(u=r[1],l=0),r[l]!==n)throw new Error("dimension mismatch");if(s<=0||u<=0||n<=0)throw new Error("invalid shape specified");if(a&&!Ut.isValidBroadcast(a,[s,u]))throw new Error("gemm: invalid bias shape for broadcast");return[s,u,n]}},as=-34028234663852886e22,ns=34028234663852886e22}),Si,ss=q(()=>{te(),Si=(e,t)=>new($r(t))(e)}),ki,Ti,Ii,os,Ei,us,zi,Ci,Ai,ls,ds,Vf=q(()=>{te(),rt(),ki=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),Ti=(e,t)=>{if(t==="int32")return e;let r=ki.get(t);if(!r)throw new Error(`WebNN backend does not support data type: ${t}`);let i=r/8;if(e.byteLength%i!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${i}.`);let a=e.byteLength/i,s=new($r(t))(e.buffer,e.byteOffset,a);switch(t){case"int64":case"uint64":{let n=new Int32Array(a);for(let u=0;u<a;u++){let l=s[u];if(l>2147483647n||l<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");n[u]=Number(l)}return new Uint8Array(n.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&s.some(u=>u>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let n=Int32Array.from(s,Number);return new Uint8Array(n.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},Ii=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let r=e.byteLength/4,i=new Int32Array(e.buffer,e.byteOffset,r);switch(t){case"int64":{let a=BigInt64Array.from(i,BigInt);return new Uint8Array(a.buffer)}case"uint64":{if(i.some(s=>s<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let a=BigUint64Array.from(i,BigInt);return new Uint8Array(a.buffer)}case"int8":{if(i.some(s=>s<-128||s>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let a=Int8Array.from(i,Number);return new Uint8Array(a.buffer)}case"uint8":{if(i.some(a=>a<0||a>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(i,Number)}case"uint32":{if(i.some(s=>s<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let a=Uint32Array.from(i,Number);return new Uint8Array(a.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},os=1,Ei=()=>os++,us=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),zi=(e,t)=>{let r=ki.get(e);if(!r)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((i,a)=>i*a)*r/8):0},Ci=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:r,tensor:i,dataType:a,shape:s,fallbackDataType:n}=e;this.sessionId=t,this.mlContext=r,this.mlTensor=i,this.dataType=a,this.tensorShape=s,this.fallbackDataType=n}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return zi(this.dataType,this.tensorShape)}destroy(){de("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),r=Ii(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(r);return}else return r.buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,r){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===r.length&&this.tensorShape.every((i,a)=>i===r[a])}setIsDataConverted(e){this.isDataConverted=e}},Ai=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}
10async ensureTensor(e,t,r,i){let a=this.tensorManager.getMLContext(e),s=this.tensorManager.getMLOpSupportLimits(e),n;if(!(s!=null&&s.input.dataTypes.includes(t))){if(n=us.get(t),!n||(s==null?void 0:s.input.dataTypes.includes(n)))throw new Error(`WebNN backend does not support data type: ${t}`);de("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${n}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(a,t,r))return this.wrapper.tensor;if(i){if(this.wrapper.byteLength!==zi(t,r))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let u=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,r,u,!0,!0,n),i&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=Ti(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else de("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){var t,r;if(this.activeUpload){let i=(t=this.wrapper)!=null&&t.isDataConverted?Ii(this.activeUpload,(r=this.wrapper)==null?void 0:r.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(i):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(i);return}else return i.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},ls=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=Ei();return this.tensorTrackersById.set(e,new Ai(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,r,i,a){de("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${r}, shape: ${i}, copyOld: ${a}}`);let s=this.tensorTrackersById.get(t);if(!s)throw new Error("Tensor not found.");return s.ensureTensor(e,r,i,a)}upload(e,t){let r=this.tensorTrackersById.get(e);if(!r)throw new Error("Tensor not found.");r.upload(t)}async download(e,t){de("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t==null?void 0:t.byteLength}}`);let r=this.tensorTrackersById.get(e);if(!r)throw new Error("Tensor not found.");return r.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}registerTensor(e,t,r,i){let a=this.getMLContext(e),s=Ei(),n=new Ci({sessionId:e,context:a,tensor:t,dataType:r,shape:i});return this.tensorTrackersById.set(s,new Ai(this,n)),this.externalTensors.add(n),s}async getCachedTensor(e,t,r,i,a,s,n){let u=this.getMLContext(e);for(let[p,h]of this.freeTensors.entries())if(h.canReuseTensor(u,t,r)){de("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${n?`fallbackDataType: ${n},`:""} shape: ${r}`);let f=this.freeTensors.splice(p,1)[0];return f.sessionId=e,f}de("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${n?`fallbackDataType: ${n},`:""} shape: ${r}}`);let l=await u.createTensor({dataType:n??t,shape:r,dimensions:r,usage:i,writable:a,readable:s});return new Ci({sessionId:e,context:u,tensor:l,dataType:t,shape:r,fallbackDataType:n})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},ds=(...e)=>new ls(...e)}),Zt,ps,cs,Gf=q(()=>{te(),vt(),ss(),Vf(),rt(),Zt=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),ps=(e,t)=>{if(e===t)return!0;
10if(e===void 0||t===void 0)return!1;let r=Object.keys(e).sort(),i=Object.keys(t).sort();return r.length===i.length&&r.every((a,s)=>a===i[s]&&e[a]===t[a])},cs=class{constructor(e){this.tensorManager=ds(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,xi(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){de("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){de("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let r of t)de("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${r}}`),this.tensorManager.releaseTensorId(r);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let r=this.mlContextCache.findIndex(i=>i.gpuDevice===e);if(r!==-1)return this.mlContextCache[r].mlContext;{let i=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:i}),i}}else if(e===void 0){let r=this.mlContextCache.findIndex(i=>i.options===void 0&&i.gpuDevice===void 0);if(r!==-1)return this.mlContextCache[r].mlContext;{let i=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:i}),i}}let t=this.mlContextCache.findIndex(r=>ps(r.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let r=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:r}),r}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let r=this.sessionIdsByMLContext.get(t);r||(r=new Set,this.sessionIdsByMLContext.set(t,r)),r.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let r=this.sessionIdsByMLContext.get(t);if(r.delete(e),r.size===0){this.sessionIdsByMLContext.delete(t);let i=this.mlContextCache.findIndex(a=>a.mlContext===t);i!==-1&&this.mlContextCache.splice(i,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){de("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,r,i,a){let s=Zt.get(r);if(!s)throw new Error(`Unsupported ONNX data type: ${r}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,s,i,a)}async createTemporaryTensor(e,t,r){de("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${r}}`);let i=Zt.get(t);if(!i)throw new Error(`Unsupported ONNX data type: ${t}`);let a=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,a,i,r,!1);let s=this.temporarySessionTensorIds.get(e);return s?s.push(a):this.temporarySessionTensorIds.set(e,[a]),a}uploadTensor(e,t){if(!_e().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");de("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let r=await this.tensorManager.download(e);return Si(r,t)}}registerMLTensor(e,t,r,i){let a=Zt.get(r);if(!a)throw new Error(`Unsupported ONNX data type: ${r}`);let s=this.tensorManager.registerTensor(e,t,a,i);return de("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${a}, dimensions: ${i}} -> {tensorId: ${s}}`),s}registerMLConstant(e,t,r,i,a,s,n=!1){if(!s)throw new Error("External mounted files are not available.");let u=e;e.startsWith("./")&&(u=e.substring(2));let l=s.get(u);if(!l)throw new Error(`File with name ${u} not found in preloaded files.`);if(t+r>l.byteLength)throw new Error("Out of bounds: data offset and length exceed the external file data size.");let p=l.slice(t,t+r).buffer,h;switch(a.dataType){case"float32":h=new Float32Array(p);break;case"float16":h=typeof Float16Array<"u"&&Float16Array.from?new Float16Array(p):new Uint16Array(p);break;case"int32":h=new Int32Array(p);break;case"uint32":h=new Uint32Array(p);break;case"int64":if(n){let f=Ti(new Uint8Array(p),"int64");h=new Int32Array(f.buffer),a.dataType="int32"}else h=new BigInt64Array(p);break;case"uint64":h=new BigUint64Array(p);break;case"int8":h=new Int8Array(p);break;case"int4":case"uint4":case"uint8":h=new Uint8Array(p);break;default:throw new Error(`Unsupported data type: ${a.dataType} in creating WebNN Constant from external data.`)}return de("verbose",()=>`[WebNN] registerMLConstant {dataType: ${a.dataType}, shape: ${a.shape}}} ${n?"(Note: it was int64 data type and registered to int32 as workaround)":""}`),i.constant(a,h)}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let r=this.sessionGraphInputs.get(e);return r?r.includes(t):!1}isGraphOutput(e,t){let r=this.sessionGraphOutputs.get(e);return r?r.includes(t):!1}isGraphInputOutputTypeSupported(e,t,r=!0){let i=Zt.get(xt(t)),a=this.mlOpSupportLimitsBySessionId.get(e);return typeof i>"u"?!1:r?!!(a!=null&&a.input.dataTypes.includes(i)):!!(a!=null&&a.output.dataTypes.includes(i))}flush(){}}}),Oi=q(()=>{}),Ri,Sr,kr,hs,fs,Bi,Mi,ms,gs,Hf=q(()=>{rt(),Oi(),Ri=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200
10],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),Sr=[],kr=e=>Math.ceil(Number(e)/16)*16,hs=e=>{for(let t=0;t<Sr.length;t++){let r=Sr[t];if(e<=r)return r}return Math.ceil(e/16)*16},fs=1,Bi=()=>fs++,Mi=async(e,t,r,i)=>{let a=kr(r),s=e.device.createBuffer({size:a,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let n=e.getCommandEncoder();e.endComputePass(),n.copyBufferToBuffer(t,0,s,0,a),e.flush(),await s.mapAsync(GPUMapMode.READ);let u=s.getMappedRange();if(i){let l=i();return l.set(new Uint8Array(u,0,r)),l}else return new Uint8Array(u.slice(0,r))}finally{s.destroy()}},ms=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of Ri)Sr.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let r=t.buffer,i=t.byteOffset,a=t.byteLength,s=kr(a),n=this.storageCache.get(e);if(!n)throw new Error("gpu data for uploading does not exist");if(Number(n.originalSize)!==a)throw new Error(`inconsistent data size. gpu data size=${n.originalSize}, data size=${a}`);let u=this.backend.device.createBuffer({mappedAtCreation:!0,size:s,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),l=u.getMappedRange();new Uint8Array(l).set(new Uint8Array(r,i,a)),u.unmap();let p=this.backend.device.createCommandEncoder();p.copyBufferToBuffer(u,0,n.gpuData.buffer,0,s),this.backend.device.queue.submit([p.finish()]),u.destroy(),de("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let r=this.storageCache.get(e);if(!r)throw new Error("source gpu data for memcpy does not exist");let i=this.storageCache.get(t);if(!i)throw new Error("destination gpu data for memcpy does not exist");if(r.originalSize!==i.originalSize)throw new Error("inconsistent source and destination gpu data size");let a=kr(r.originalSize),s=this.backend.getCommandEncoder();this.backend.endComputePass(),s.copyBufferToBuffer(r.gpuData.buffer,0,i.gpuData.buffer,0,a)}registerExternalBuffer(e,t,r){let i;if(r){if(i=r[0],e===r[1])return de("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, buffer is the same, skip.`),i;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. 11 Please use the previous external buffer!`)}else i=Bi();return this.storageCache.set(i,{gpuData:{id:i,type:0,buffer:e},originalSize:t}),de("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, registered.`),i}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),de("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let r=hs(e),i,a=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,s=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(a||s){let u=(a?this.freeBuffers:this.freeUniformBuffers).get(r);u?u.length>0?i=u.pop():i=this.backend.device.createBuffer({size:r,usage:t}):i=this.backend.device.createBuffer({size:r,usage:t})}else i=this.backend.device.createBuffer({size:r,usage:t});let n={id:Bi(),type:0,buffer:i};return this.storageCache.set(n.id,{gpuData:n,originalSize:Number(e)}),de("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${n.id}`),n}get(e){var t;return(t=this.storageCache.get(e))==null?void 0:t.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,r=this.storageCache.get(t);if(!r){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return de("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(e,t){let r=this.storageCache.get(Number(e));if(!r)throw new Error("data does not exist");await Mi(this.backend,r.gpuData.buffer,r.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=Ri.get(e.size);
vendor: 17,882 bytes, lines 11-118
11if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let r=this.freeBuffers.get(e.size)||[];t===void 0||r.length>=t?e.destroy():r.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let r=this.freeUniformBuffers.get(e.size)||[];t===void 0||r.length>=t?e.destroy():r.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(r=>{r.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(de("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(r=>{r.gpuData.buffer.destroy()}),this.storageCache=new Map)}},gs=(...e)=>new ms(...e)}),ys,he,xe=q(()=>{ys=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},he=e=>new ys(e)}),qt,Tr,ke,Ee,Y,ve,Ni,Wt,ot,Z,Yt,M,K,_s,Di,ws,bs,ae=q(()=>{te(),re(),qt=64,Tr=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}<f16>`:"f16";case 1:return t>1?`vec${t}<f32>`:"f32";case 6:return t>1?`vec${t}<i32>`:"i32";case 12:return t>1?`vec${t}<u32>`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4<bool>"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},ke=(e,t=1)=>{let r=Tr(e,t);return typeof r=="string"?r:r[0]},Ee=(e,t=1)=>{let r=Tr(e,t);return typeof r=="string"?r:r[1]},Y=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:O.computeStrides(r)})}),t},ve=e=>e%4===0?4:e%2===0?2:1,Ni=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,Wt=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}<f32>(${r})`,ot=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,Z=(e,t,r,i)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?i==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:i==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,Yt=(e,t,r,i,a)=>{let s=typeof r=="number",n=s?r:r.length,u=[...new Array(n).keys()],l=n<2?"u32":n<=4?`vec${n}<u32>`:`array<u32, ${n}>`,p=Tr(t,a),h=typeof p=="string"?p:p[1],f=typeof p=="string"?p:p[0],g={indices:l,value:h,storage:f,tensor:t},y=P=>typeof P=="string"?P:`${P}u`,_={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},w=s?"uniforms.":"",S=`${w}${e}_shape`,x=`${w}${e}_strides`,b="";for(let P=0;P<n-1;P++)b+=` 12 let dim${P} = current / ${Z(x,P,n)}; 13 let rest${P} = current % ${Z(x,P,n)}; 14 indices[${P}] = dim${P}; 15 current = rest${P}; 16 `;b+=`indices[${n-1}] = current;`;let I=n<2?"":` 17 fn o2i_${e}(offset: u32) -> ${g.indices} { 18 var indices: ${g.indices}; 19 var current = offset; 20 ${b} 21 return indices; 22 }`,T=P=>(_.offsetToIndices=!0,n<2?P:`o2i_${e}(${P})`),E=[];if(n>=2)for(let P=n-1;P>=0;P--)E.push(`${Z(x,P,n)} * (indices[${P}])`);let C=n<2?"":` 23 fn i2o_${e}(indices: ${g.indices}) -> u32 { 24 return ${E.join("+")}; 25 }`,A=P=>(_.indicesToOffset=!0,n<2?P:`i2o_${e}(${P})`),v=(...P)=>n===0?"0u":`${g.indices}(${P.map(y).join(",")})`,N=(P,U)=>n<2?`${P}`:`${Z(P,U,n)}`,D=(P,U,J)=>n<2?`${P}=${J};`:`${Z(P,U,n)}=${J};`,H={},G=(P,U)=>{_.broadcastedIndicesToOffset=!0;let J=`${U.name}broadcastedIndicesTo${e}Offset`;if(J in H)return`${J}(${P})`;let ee=[];for(let $e=n-1;$e>=0;$e--){let pt=U.indicesGet("outputIndices",$e+U.rank-n);ee.push(`${N(x,$e)} * (${pt} % ${N(S,$e)})`)}return H[J]=`fn ${J}(outputIndices: ${U.type.indices}) -> u32 { 26 return ${ee.length>0?ee.join("+"):"0u"}; 27 }`,`${J}(${P})`},Q=(P,U)=>(()=>{if(g.storage===g.value)return`${e}[${P}]=${U};`;if(g.storage==="vec2<u32>"&&g.value==="i32")return`${e}[${P}]=vec2<u32>(u32(${U}), select(0u, 0xFFFFFFFFu, ${U} < 0));`;if(g.storage==="vec2<u32>"&&g.value==="u32")return`${e}[${P}]=vec2<u32>(u32(${U}), 0u);`;if(g.storage==="u32"&&g.value==="vec4<bool>")return`${e}[${P}]=dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(${U}));`;throw new Error(`not supported combination of storage type ${g.storage} and value type ${g.value} yet`)})(),R=P=>(()=>{if(g.storage===g.value)return`${e}[${P}]`;if(g.storage==="vec2<u32>"&&g.value==="i32")return`i32(${e}[${P}].x)`;if(g.storage==="vec2<u32>"&&g.value==="u32")return`u32(${e}[${P}].x)`;if(g.storage==="u32"&&g.value==="vec4<bool>")return`vec4<bool>(bool(${e}[${P}] & 0xFFu), bool(${e}[${P}] & 0xFF00u), bool(${e}[${P}] & 0xFF0000u), bool(${e}[${P}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${g.storage} and value type ${g.value} yet`)})(),j=n<2?"":` 28 fn get_${e}ByIndices(indices: ${g.indices}) -> ${h} { 29 return ${R(`i2o_${e}(indices)`)}; 30 }`,F=n<2?"":(()=>{let P=u.map(J=>`d${J}: u32`).join(", "),U=u.map(J=>`d${J}`).join(", ");return` 31 fn get_${e}(${P}) -> ${h} { 32 return get_${e}ByIndices(${v(U)}); 33 }`})(),X=(...P)=>{if(P.length!==n)throw new Error(`indices length must be ${n}`);let U=P.map(y).join(",");return n===0?R("0u"):n===1?R(U[0]):(_.get=!0,_.getByIndices=!0,_.indicesToOffset=!0,`get_${e}(${U})`)},le=P=>n<2?R(P):(_.getByIndices=!0,_.indicesToOffset=!0,`get_${e}ByIndices(${P})`),W=n<2?"":` 34 fn set_${e}ByIndices(indices: ${g.indices}, value: ${h}) { 35 ${Q(`i2o_${e}(indices)`,"value")} 36 }`,me=n<2?"":(()=>{let P=u.map(J=>`d${J}: u32`).join(", "),U=u.map(J=>`d${J}`).join(", ");return` 37 fn set_${e}(${P}, value: ${h}) { 38 set_${e}ByIndices(${v(U)}, value); 39 }`})();return{impl:()=>{let P=[],U=!1;return _.offsetToIndices&&(P.push(I),U=!0),_.indicesToOffset&&(P.push(C),U=!0),_.broadcastedIndicesToOffset&&(Object.values(H).forEach(J=>P.push(J)),U=!0),_.set&&(P.push(me),U=!0),_.setByIndices&&(P.push(W),U=!0),_.get&&(P.push(F),U=!0),_.getByIndices&&(P.push(j),U=!0),!s&&U&&P.unshift(`const ${S} = ${g.indices}(${r.join(",")});`,`const ${x} = ${g.indices}(${O.computeStrides(r).join(",")});`),P.join(` 40`)},type:g,offsetToIndices:T,indicesToOffset:A,broadcastedIndicesToOffset:G,indices:v,indicesGet:N,indicesSet:D,set:(...P)=>{if(P.length!==n+1)throw new Error(`indices length must be ${n}`);let U=P[n];if(typeof U!="string")throw new Error("value must be string");let J=P.slice(0,n).map(y).join(",");return n===0?Q("0u",U):n===1?Q(J[0],U):(_.set=!0,_.setByIndices=!0,_.indicesToOffset=!0,`set_${e}(${J}, ${U})`)},setByOffset:Q,setByIndices:(P,U)=>n<2?Q(P,U):(_.setByIndices=!0,_.indicesToOffset=!0,`set_${e}ByIndices(${P}, ${U});`),get:X,getByOffset:R,getByIndices:le,usage:i,name:e,strides:x,shape:S,rank:n}},M=(e,t,r,i=1)=>Yt(e,t,r,"input",i),K=(e,t,r,i=1)=>Yt(e,t,r,"output",i),_s=(e,t,r)=>Yt(e,t,r,"atomicOutput",1),Di=(e,t,r,i=1)=>Yt(e,t,r,"internal",i),ws=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=qt){let t=typeof e=="number"?e:e[0],r=typeof e=="number"?1:e[1],i=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||r>this.limits.maxComputeWorkgroupSizeY||i>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${r}, ${i}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*r*i>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${r}, ${i}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let a=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,s=a?`@builtin(global_invocation_id) global_id : vec3<u32>, 41 @builtin(workgroup_id) workgroup_id : vec3<u32>, 42 @builtin(local_invocation_index) local_idx : u32, 43 @builtin(local_invocation_id) local_id : vec3<u32>`:`@builtin(global_invocation_id) global_id : vec3<u32>, 44 @builtin(local_invocation_id) local_id : vec3<u32>, 45 @builtin(local_invocation_index) local_idx : u32, 46 @builtin(workgroup_id) workgroup_id : vec3<u32>, 47 @builtin(num_workgroups) num_workgroups : vec3<u32>`,n=a?`let global_idx = global_id.x; 48 let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + 49 workgroup_id.y * num_workgroups[0] + workgroup_id.x; 50 let global_idx = workgroup_index * ${t*r*i}u + local_idx;`;return`@compute @workgroup_size(${t}, ${r}, ${i}) 51 fn main(${s}) { 52 ${n} 53 `}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let r=e.usage==="input"?"read":"read_write",i=e.usage==="atomicOutput"?"atomic<i32>":e.type.storage;return`@group(0) @binding(${t}) var<storage, ${r}> ${e.name}: array<${i}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(` 54`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,r=1){return this.uniforms.push({name:e,type:t,length:r}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:r,length:i}of this.uniforms)if(i&&i>4)r==="f16"?e.push(`@align(16) ${t}:array<mat2x4<${r}>, ${Math.ceil(i/8)}>`):e.push(`${t}:array<vec4<${r}>, ${Math.ceil(i/4)}>`);else{let a=i==null||i===1?r:`vec${i}<${r}>`;e.push(`${t}:${a}`)}return` 55 struct Uniforms { ${e.join(", ")} }; 56 @group(0) @binding(${this.variableIndex}) var<uniform> uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(e=>e.impl()).join(` 57`)+this.internalVariables.map(e=>e.impl()).join(` 58`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},bs=(e,t)=>new ws(e,t)}),$s,Pi,vs,xs,Ss,ks,Me,Ts,Is,ut=q(()=>{te(),re(),xe(),ae(),$s=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},Pi=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),vs=(e,t)=>O.sortBasedOnPerm(e,Pi(e.length,t)),xs=(e,t,r,i)=>{let a=`fn perm(i: ${i.type.indices}) -> ${r.type.indices} { 59 var a: ${r.type.indices};`;for(let s=0;s<t;++s)a+=`a[${e[s]}]=i[${s}];`;return a+="return a;}"},Ss=(e,t)=>{let r=[],i=[];for(let a=0;a<e.length;++a)e[a]!==1&&r.push(e[a]),e[t[a]]!==1&&i.push(t[a]);return{newShape:r,newPerm:i}},ks=(e,t)=>{let r=0;for(let i=0;i<e.length;++i)if(t[e[i]]!==1){if(e[i]<r)return!1;r=e[i]}return!0},Me=(e,t)=>{let r=e.dataType,i=e.dims.length,a=Pi(i,t),s=vs(e.dims,a),n=e.dims,u=s,l=i<2||ks(a,e.dims),p;if(l)return p=_=>{let w=M("input",r,n,4),S=K("output",r,u,4);return` 60 ${_.registerUniform("output_size","u32").declareVariables(w,S)} 61 ${_.mainStart()} 62 ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 63 output[global_idx] = input[global_idx]; 64 }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let _=O.size(s);return{outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(_/64/4)},programUniforms:[{type:12,data:Math.ceil(_/4)}]}},getShaderSource:p};let{newShape:h,newPerm:f}=Ss(e.dims,a),g=O.areEqual(f,[2,3,1]),y=O.areEqual(f,[3,1,2]);if(h.length===2||g||y){n=g?[h[0],h[1]*h[2]]:y?[h[0]*h[1],h[2]]:h,u=[n[1],n[0]];let _=16;return p=w=>{let S=M("a",r,n.length),x=K("output",r,u.length);return` 65 ${w.registerUniform("output_size","u32").declareVariables(S,x)} 66 var<workgroup> tile : array<array<${x.type.value}, ${_+1}>, ${_}>; 67 ${w.mainStart([_,_,1])} 68 let stride = (uniforms.output_shape[1] - 1) / ${_} + 1; 69 let workgroup_id_x = workgroup_index % stride; 70 let workgroup_id_y = workgroup_index / stride; 71 let input_col = workgroup_id_y * ${_}u + local_id.x; 72 let input_row = workgroup_id_x * ${_}u + local_id.y; 73 if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { 74 tile[local_id.y][local_id.x] = ${S.getByIndices(`${S.type.indices}(input_row, input_col)`)}; 75 } 76 workgroupBarrier(); 77 78 let output_col = workgroup_id_x * ${_}u + local_id.x; 79 let output_row = workgroup_id_y * ${_}u + local_id.y; 80 if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { 81 ${x.setByIndices(`${x.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} 82 } 83 }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let w=O.size(s);return{outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(u[1]/_),y:Math.ceil(u[0]/_)},programUniforms:[{type:12,data:w},...Y(n,u)]}},getShaderSource:p}}return p=_=>{let w=M("a",r,n.length),S=K("output",r,u.length);return` 84 ${_.registerUniform("output_size","u32").declareVariables(w,S)} 85 86 ${xs(a,i,w,S)} 87 88 ${_.mainStart()} 89 ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 90 91 let indices = ${S.offsetToIndices("global_idx")}; 92 let aIndices = perm(indices); 93 94 ${S.setByOffset("global_idx",w.getByIndices("aIndices"))} 95 }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let _=O.size(s);return{outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:[{type:12,data:_},...Y(n,u)]}},getShaderSource:p}},Ts=(e,t)=>{$s(e.inputs,t.perm),e.compute(Me(e.inputs[0],t.perm))},Is=e=>he({perm:e.perm})}),Es,zs,Cs,As,Os,Rs,Bs,Ms,Ns,Ds,Le,Ps,Us,qs,Ws,Ls,Vs,Gs,Hs,Fs,js,Ff=q(()=>{te(),re(),ae(),qi(),ut(),Es={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"},zs={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"},Cs={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},As={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Os=(e,t)=>{let r=[];for(let i=t-e;i<t;++i)r.push(i);return r},Rs=(e,t)=>{let r=[],i=e.length;for(let s=0;s<i;s++)t.indexOf(s)===-1&&r.push(e[s]);let a=t.map(s=>e[s]);return[r,a]},Bs=(e,t)=>{let r=e.length+t.length,i=[],a=0;for(let s=0;s<r;s++)t.indexOf(s)===-1?i.push(e[a++]):i.push(1);return i},Ms=(e,t)=>{for(let r=0;r<e.length;++r)if(e[e.length-r-1]!==t-1-r)return!1;return!0},Ns=(e,t)=>{let r=[];if(!Ms(e,t)){for(let i=0;i<t;++i)e.indexOf(i)===-1&&r.push(i);e.forEach(i=>r.push(i))}return r},Ds=(e,t,r,i,a,s,n)=>{let u=r[0].dims,l=O.size(s),p=O.size(n),h=M("_A",r[0].dataType,u),f=K("output",a,s),g=64;l===1&&(g=256);let y=` 96 var<workgroup> aBestValues : array<f32, ${g}>; 97 `,_=w=>` 98 ${w.registerUniform("reduceSize","u32").declareVariables(h,f)} 99 ${y} 100 fn DIV_CEIL(a : u32, b : u32) -> u32 { 101 return ((a - 1u) / b + 1u); 102 } 103 ${w.mainStart(g)} 104 105 let outputIndex = global_idx / ${g}; 106 let offset = outputIndex * uniforms.reduceSize; 107 108 var bestValue = f32(${Cs[i]}); 109 let Length = uniforms.reduceSize; 110 for (var k = local_idx; k < Length; k = k + ${g}) { 111 let candidate = f32(${h.getByOffset("offset + k")}); 112 bestValue = ${Es[i]}; 113 } 114 aBestValues[local_idx] = bestValue; 115 workgroupBarrier(); 116 117 var reduceSize = min(Length, ${g}u); 118 for (var currentSize = reduceSize / 2u; reduceSize > 1u;
119 currentSize = reduceSize / 2u) { 120 let interval = DIV_CEIL(reduceSize, 2u); 121 if (local_idx < currentSize) { 122 let candidate = aBestValues[local_idx + interval]; 123 bestValue = ${zs[i]}; 124 aBestValues[local_idx] = bestValue; 125 } 126 reduceSize = interval; 127 workgroupBarrier(); 128 } 129 130 if (local_idx == 0u) { 131 ${f.setByOffset("outputIndex",`${i==="mean"?`${f.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${f.type.storage}(${As[i]})`}`)}; 132 } 133 }`;return{name:e,shaderCache:{hint:`${t};${g}`,inputDependencies:["type"]},getShaderSource:_,getRunData:()=>({outputs:[{dims:s,dataType:a}],dispatchGroup:{x:l},programUniforms:[{type:12,data:p}]})}},Le=(e,t,r,i)=>{let a=e.inputs.length===1?r:Ui(e.inputs,r),s=a.axes;s.length===0&&!a.noopWithEmptyAxes&&(s=e.inputs[0].dims.map((y,_)=>_));let n=O.normalizeAxes(s,e.inputs[0].dims.length),u=n,l=e.inputs[0],p=Ns(u,e.inputs[0].dims.length);p.length>0&&(l=e.compute(Me(e.inputs[0],p),{inputs:[0],outputs:[-1]})[0],u=Os(u.length,l.dims.length));let[h,f]=Rs(l.dims,u),g=h;a.keepDims&&(g=Bs(h,n)),e.compute(Ds(t,a.cacheKey,[l],i,e.inputs[0].dataType,g,f),{inputs:[l]})},Ps=(e,t)=>{Le(e,"ReduceMeanShared",t,"mean")},Us=(e,t)=>{Le(e,"ReduceL1Shared",t,"l1")},qs=(e,t)=>{Le(e,"ReduceL2Shared",t,"l2")},Ws=(e,t)=>{Le(e,"ReduceLogSumExpShared",t,"logSumExp")},Ls=(e,t)=>{Le(e,"ReduceMaxShared",t,"max")},Vs=(e,t)=>{Le(e,"ReduceMinShared",t,"min")},Gs=(e,t)=>{Le(e,"ReduceProdShared",t,"prod")},Hs=(e,t)=>{Le(e,"ReduceSumShared",t,"sum")},Fs=(e,t)=>{Le(e,"ReduceSumSquareShared",t,"sumSquare")},js=(e,t)=>{Le(e,"ReduceLogSumShared",t,"logSum")}}),Ve,Ks,Ir,Ui,Ge,Qs,Zs,Ys,Xs,Js,eo,to,ro,io,ao,He,no,so,oo,uo,lo,po,co,ho,fo,mo,qi=q(()=>{te(),re(),xe(),ae(),Ff(),Ve=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Ks=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Ir=(e,t,r,i,a,s,n=!1,u=!1)=>{let l=[],p=r[0].dims,h=p.length,f=O.normalizeAxes(a,h),g=!u&&f.length===0;p.forEach((w,S)=>{g||f.indexOf(S)>=0?n&&l.push(1):l.push(w)});let y=l.length,_=O.size(l);return{name:e,shaderCache:t,getShaderSource:w=>{let S=[],x=M("_A",r[0].dataType,h),b=K("output",s,y),I=i(x,b,f),T=I[2];for(let E=0,C=0;E<h;E++)g||f.indexOf(E)>=0?(n&&C++,T=`for(var j${E}: u32 = 0; j${E} < ${p[E]}; j${E}++) { 134 ${I[2].includes("last_index")?`let last_index = j${E};`:""} 135 ${x.indicesSet("input_indices",E,`j${E}`)} 136 ${T} 137 }`):(S.push(`${x.indicesSet("input_indices",E,b.indicesGet("output_indices",C))};`),C++);return` 138 139 ${w.registerUniform("output_size","u32").declareVariables(x,b)} 140 141 ${w.mainStart()} 142 ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 143 var input_indices: ${x.type.indices}; 144 let output_indices = ${b.offsetToIndices("global_idx")}; 145 146 ${S.join(` 147`)} 148 ${I[0]} // init ops for reduce max/min 149 ${I[1]} 150 ${T} 151 ${I[3]} 152 ${I.length===4?b.setByOffset("global_idx","value"):I.slice(4).join(` 153`)} 154 }`},getRunData:()=>({outputs:[{dims:l,dataType:s}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:[{type:12,data:_},...Y(p,l)]})}},Ui=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(i=>r.push(Number(i))),he({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},Ge=(e,t,r,i)=>{let a=e.inputs,s=a.length===1?r:Ui(a,r);e.compute(Ir(t,{hint:s.cacheKey,inputDependencies:["rank"]},[a[0]],s.noopWithEmptyAxes&&s.axes.length===0?Ks:i,s.axes,a[0].dataType,s.keepDims,s.noopWithEmptyAxes),{inputs:[0]})},Qs=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceLogSum",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,"value = log(value);"])},Zs=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceL1",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += abs(${r.getByIndices("input_indices")});`,""])},Ys=(e,t)=>
154{Ve(e.inputs),Ge(e,"ReduceL2",t,(r,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Xs=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceLogSumExp",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += exp(${r.getByIndices("input_indices")});`,"value = log(value);"])},Js=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceMax",t,(r,i,a)=>{let s=[];for(let n=0;n<r.rank;n++)(a.indexOf(n)>=0||a.length===0)&&s.push(r.indicesSet("input_indices",n,0));return[`${s.join(` 155`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = max(value, ${r.getByIndices("input_indices")});`,""]})},eo=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceMean",t,(r,i,a)=>{let s=1;for(let n=0;n<r.rank;n++)(a.indexOf(n)>=0||a.length===0)&&(s*=e.inputs[0].dims[n]);return["var sum = f32(0);","",`sum += f32(${r.getByIndices("input_indices")});`,`let value = ${i.type.value}(sum / ${s});`]})},to=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceMin",t,(r,i,a)=>{let s=[];for(let n=0;n<r.rank;n++)(a.indexOf(n)>=0||a.length===0)&&s.push(`input_indices[${n}] = 0;`);return[`${s.join(` 156`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = min(value, ${r.getByIndices("input_indices")});`,""]})},ro=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceProd",t,(r,i)=>[`var value = ${i.type.storage}(1);`,"",`value *= ${r.getByIndices("input_indices")};`,""])},io=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceSum",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,""])},ao=(e,t)=>{Ve(e.inputs),Ge(e,"ReduceSumSquare",t,(r,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += t * t;`,""])},He=(e,t,r)=>{if(t.length===0)return r;let i=1,a=1;for(let s=0;s<t.length;s++)t.indexOf(s)===-1?i*=e[s]:a*=e[s];return a<32&&i>1024},no=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?eo(e,t):Ps(e,t)},so=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Zs(e,t):Us(e,t)},oo=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Ys(e,t):qs(e,t)},uo=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Xs(e,t):Ws(e,t)},lo=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Js(e,t):Ls(e,t)},po=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?to(e,t):Vs(e,t)},co=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?ro(e,t):Gs(e,t)},ho=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?io(e,t):Hs(e,t)},fo=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?ao(e,t):Fs(e,t)},mo=(e,t)=>{He(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Qs(e,t):js(e,t)}}),Wi,go,yo,Li,jf=q(()=>{te(),xe(),qi(),Wi=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},go=(e,t)=>{Wi(e.inputs);let r=(i,a,s)=>{let n=[];for(let u=0;u<i.rank;u++)(s.indexOf(u)>=0||s.length===0)&&n.push(`input_indices[${u}] = 0;`);return[`${n.join(` 157`)}`,`var value = ${i.getByIndices("input_indices")}; 158var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { 159 value = ${i.getByIndices("input_indices")}; 160 best_index = i32(last_index); 161 }`,"",a.setByOffset("global_idx","best_index")]};e.compute(Ir("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},yo=(e,t)=>{Wi(e.inputs);let r=(i,a,s)=>{let n=[];for(let u=0;u<i.rank;u++)(s.indexOf(u)>=0||s.length===0)&&n.push(`input_indices[${u}] = 0;`);return[`${n.join(` 162`)}`,`var value = ${i.getByIndices("input_indices")}; 163var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { 164 value = ${i.getByIndices("input_indices")}; 165 best_index = i32(last_index); 166 }`,"",a.setByOffset("global_idx","best_index")]};e.compute(Ir("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},Li=e=>he(e)}),_o,Er,wo,bo,$o,Xt,vo,xo,Vi=q(()=>{te(),re(),Oi(),ae(),_o=(e,t)=>{let r=e[0],i=e[1],a=e[2],s=e[3],n=e[4],u=e[5];if(n&&u)throw new Error("Attention cannot have both past and attention_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let l=r.dims[0],p=r.dims[1],h=r.dims[2];if(a.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(i.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');
vendor: 12,674 bytes, lines 166-289
166if(i.dims[0]!==h)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(a.dims[0]!==i.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let f=a.dims[0]/3,g=f,y=g;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let I of t.qkvHiddenSizes)if(I%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");f=t.qkvHiddenSizes[0],g=t.qkvHiddenSizes[1],y=t.qkvHiddenSizes[2]}let _=p;if(f!==g)throw new Error("qkv_hidden_sizes first element should be same as the second");if(a.dims[0]!==f+g+y)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let w=0;if(n){if(g!==y)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(n.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(n.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(n.dims[1]!==l)throw new Error('Input "past" second dimension must be batch_size');if(n.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(n.dims[4]!==g/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(w=n.dims[3])}let S=_+w,x=-1,b=0;if(s)throw new Error("Mask not supported");if(n)throw new Error("past is not supported");if(u){if(u.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(u.dims[0]!==l||u.dims[1]!==t.numHeads||u.dims[2]!==p||u.dims[3]!==S)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:l,sequenceLength:p,pastSequenceLength:w,kvSequenceLength:_,totalSequenceLength:S,maxSequenceLength:x,inputHiddenSize:h,hiddenSize:f,vHiddenSize:y,headSize:Math.floor(f/t.numHeads),vHeadSize:Math.floor(y/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:b,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},Er=(e,t,r)=>t&&e?` 167 let total_sequence_length_input = u32(${t.getByOffset("0")}); 168 let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length); 169 let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input; 170 let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input; 171 total_sequence_length = u32(${e==null?void 0:e.getByOffset("batchIdx")}) + 1; 172 var past_sequence_length: u32 = 0; 173 if (is_first_prompt == false) { 174 past_sequence_length = total_sequence_length - sequence_length; 175 } 176 `:` 177 ${r?"let past_sequence_length = uniforms.past_sequence_length":""}; 178 let present_sequence_length = total_sequence_length; 179 `,wo=(e,t,r,i,a,s,n,u)=>{let l=ve(n?1:s),p=64,h=s/l;h<p&&(p=32);let f=Math.ceil(s/l/p),g=[{type:12,data:t},{type:12,data:r},{type:12,data:i},{type:12,data:a},{type:12,data:h},{type:12,data:f}],y=ke(e.dataType,l),_=Ee(1,l),w=["type"];n&&w.push("type"),u&&w.push("type");let S=x=>{let b=K("x",e.dataType,e.dims,l),I=[b],T=n?M("seq_lens",n.dataType,n.dims):void 0;T&&I.push(T);let E=u?M("total_sequence_length_input",u.dataType,u.dims):void 0;E&&I.push(E);let C=Ee(e.dataType),A=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` 180 var<workgroup> thread_max: array<f32, ${p}>; 181 var<workgroup> thread_sum: array<f32, ${p}>; 182 ${x.registerUniforms(A).declareVariables(...I)} 183 ${x.mainStart([p,1,1])} 184 let batchIdx = workgroup_id.z / uniforms.num_heads; 185 let headIdx = workgroup_id.z % uniforms.num_heads; 186 let sequence_length = uniforms.sequence_length; 187 var total_sequence_length = uniforms.total_sequence_length; 188 ${Er(T,E,!1)} 189 let local_offset = local_idx * uniforms.elements_per_thread; 190 let offset = (global_idx / ${p}) * uniforms.total_sequence_length + local_offset; 191 let seq_causal_length = ${n?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"}; 192 var thread_max_vector = ${_}(-3.4028234663852886e+38f); 193 for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { 194 thread_max_vector = max(${_}(x[offset + i]), thread_max_vector); 195 } 196 thread_max[local_idx] = ${(()=>{switch(l){case 1:return"thread_max_vector";case 2:return"max(thread_max_vector.x, thread_max_vector.y)";case 4:return"max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))";default:throw new Error(`Unsupported components: ${l}`)}})()}; 197 workgroupBarrier(); 198 199 var max_value = f32(-3.4028234663852886e+38f); 200 for (var i = 0u; i < ${p}; i++) { 201 max_value = max(thread_max[i], max_value); 202 } 203 204 var sum_vector = ${_}(0); 205 for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { 206 sum_vector += exp(${_}(x[offset + i]) - max_value); 207 } 208 thread_sum[local_idx] = ${(()=>{switch(l){case 1:return"sum_vector";case 2:return"sum_vector.x + sum_vector.y";case 4:return"sum_vector.x + sum_vector.y + sum_vector.z + sum_vector.w";default:throw new Error(`Unsupported components: ${l}`)}})()}; 209 workgroupBarrier(); 210 211 var sum: f32 = 0; 212 for (var i = 0u; i < ${p}; i++) { 213 sum += thread_sum[i]; 214 } 215 216 if (sum == 0) { 217 for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { 218 x[offset + i] = ${b.type.value}(${C}(1.0) / ${C}(seq_causal_length)); 219 } 220 } else { 221 for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { 222 var f32input = ${_}(x[offset + i]); 223 x[offset + i] = ${b.type.value}(exp(f32input - max_value) / sum); 224 } 225 } 226 ${n?` 227 for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) { 228 x[offset + total_seq_id] = ${b.type.value}(${C}(0)); 229 }`:""}; 230 }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${p};${y};${l}`,inputDependencies:w},getShaderSource:S,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:a,z:t*r},programUniforms:g})}},bo=(e,t,r,i,a,s,n,u,l)=>{let p=n+s.kvSequenceLength,h=[s.batchSize,s.numHeads,s.sequenceLength,p],f=e>1&&i,g=s.kvNumHeads?s.kvNumHeads:s.numHeads,y=f?[s.batchSize,g,p,s.headSize]:void 0,_=s.nReps?s.nReps:1,w=s.scale===0?1/Math.sqrt(s.headSize):s.scale,S=ve(s.headSize),x=s.headSize/S,b=12,I={x:Math.ceil(p/b),y:Math.ceil(s.sequenceLength/b),z:s.batchSize*s.numHeads},T=[{type:12,data:s.sequenceLength},{type:12,data:x},{type:12,data:p},{type:12,data:s.numHeads},{type:12,data:s.headSize},{type:1,data:w},{type:12,data:n},{type:12,data:s.kvSequenceLength},{type:12,data:_}],E=f&&i&&O.size(i.dims)>0,C=["type","type"];E&&C.push("type"),a&&C.push("type"),u&&C.push("type"),l&&C.push("type");let A=[{dims:h,dataType:t.dataType,gpuDataType:0}];f&&A.push({dims:y,dataType:t.dataType,gpuDataType:0});let v=N=>{let D=M("q",t.dataType,t.dims,S),H=M("key",r.dataType,r.dims,S),G=[D,H];if(E){let W=M("past_key",i.dataType,i.dims,S);G.push(W)}a&&G.push(M("attention_bias",a.dataType,a.dims));let Q=u?M("seq_lens",u.dataType,u.dims):void 0;Q&&G.push(Q);let R=l?M("total_sequence_length_input",l.dataType,l.dims):void 0;R&&G.push(R);let j=K("output",t.dataType,h),F=[j];f&&F.push(K("present_key",t.dataType,y,S));let X=Ee(1,S),le=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` 231 const TILE_SIZE = ${b}u; 232 233 var<workgroup> tileQ: array<${D.type.storage}, ${b*b}>; 234 var<workgroup> tileK: array<${D.type.storage}, ${b*b}>; 235 ${N.registerUniforms(le).declareVariables(...G,...F)} 236 ${N.mainStart([b,b,1])} 237 // x holds the N and y holds the M 238 let headIdx = workgroup_id.z % uniforms.num_heads; 239 let kvHeadIdx = ${_===1?"headIdx":"headIdx / uniforms.n_reps"}; 240 let kv_num_heads = ${_===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; 241 let batchIdx = workgroup_id.z / uniforms.num_heads; 242 let m = workgroup_id.y * TILE_SIZE; 243 let n = workgroup_id.x * TILE_SIZE; 244 let sequence_length = uniforms.M; 245 var total_sequence_length = uniforms.N; 246 ${Er(Q,R,!0)} 247 let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; 248 let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; 249 ${E&&f?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""}; 250 let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K; 251 ${f?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""} 252 var value = ${X}(0); 253 for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { 254 if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) { 255 tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x]; 256 } 257 if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) { 258 var idx = TILE_SIZE * local_id.y + local_id.x; 259 ${E&&f?` 260 if (n + local_id.y < past_sequence_length) { 261 tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; 262 } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { 263 tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x]; 264 }`:` 265 if (n + local_id.y < uniforms.kv_sequence_length) { 266 tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; 267 }`} 268 ${f?`if (n + local_id.y < present_sequence_length) { 269 present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx]; 270 }`:""} 271 } 272 workgroupBarrier(); 273 274 for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) { 275 value += ${X}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); 276 } 277 278 workgroupBarrier(); 279 } 280 281 if (global_id.y < uniforms.M && global_id.x < total_sequence_length) { 282 let headOffset = workgroup_id.z * uniforms.M * uniforms.N; 283 let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x; 284 var sum: f32 = ${(()=>{switch(S){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${S}`)}})()}; 285 output[outputIdx] = ${j.type.value} (sum * uniforms.alpha) + ${a?"attention_bias[outputIdx]":"0.0"}; 286 } 287 }`};return{name:"AttentionProbs",shaderCache:{hint:`${S};${a!==void 0};${i!==void 0};${e}`,inputDependencies:C},getRunData:()=>({outputs:A,dispatchGroup:I,programUniforms:T}),getShaderSource:v}},$o=(e,t,r,i,a,s,n=void 0,u=void 0)=>{let l=s+a.kvSequenceLength,p=a.nReps?a.nReps:1,h=a.vHiddenSize*p,f=e>1&&i,g=a.kvNumHeads?a.kvNumHeads:a.numHeads,y=f?[a.batchSize,g,l,a.headSize]:void 0,_=[a.batchSize,a.sequenceLength,h],w=12,S={x:Math.ceil(a.vHeadSize/w),y:Math.ceil(a.sequenceLength/w),z:a.batchSize*a.numHeads},x=[{type:12,data:a.sequenceLength},{type:12,data:l},{type:12,data:a.vHeadSize},{type:12,data:a.numHeads},{type:12,data:a.headSize},{type:12,data:h},{type:12,data:s},{type:12,data:a.kvSequenceLength},{type:12,data:p}],b=f&&i&&O.size(i.dims)>0,I=["type","type"];b&&I.push("type"),n&&I.push("type"),u&&I.push("type");let T=[{dims:_,dataType:t.dataType,gpuDataType:0}];f&&T.push({dims:y,dataType:t.dataType,gpuDataType:0});let E=C=>{let A=M("probs",t.dataType,t.dims),v=M("v",r.dataType,r.dims),N=[A,v];b&&N.push(M("past_value",i.dataType,i.dims));let D=n?M("seq_lens",n.dataType,n.dims):void 0;n&&N.push(D);let H=u?M("total_sequence_length_input",u.dataType,u.dims):void 0;u&&N.push(H);let G=[K("output",t.dataType,_)];f&&G.push(K("present_value",t.dataType,y));let Q=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` 288 const TILE_SIZE = ${w}u; 289 var<workgroup> tileQ: array<${A.type.value}, ${w*w}>;
290 var<workgroup> tileV: array<${A.type.value}, ${w*w}>; 291 ${C.registerUniforms(Q).declareVariables(...N,...G)} 292 ${C.mainStart([w,w,1])} 293 let headIdx = workgroup_id.z % uniforms.num_heads; 294 let batchIdx = workgroup_id.z / uniforms.num_heads; 295 let kvHeadIdx = ${p===1?"headIdx":"headIdx / uniforms.n_reps"}; 296 let kv_num_heads = ${p===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; 297 let m = global_id.y; 298 let n = global_id.x; 299 let sequence_length = uniforms.M; 300 var total_sequence_length = uniforms.K; 301 ${Er(D,H,!0)} 302 let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; 303 let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch 304 ${b&&f?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""}; 305 let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n; 306 ${f?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""} 307 var value = ${A.type.storage}(0); 308 for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { 309 if (m < uniforms.M && w + local_id.x < uniforms.K) { 310 tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x]; 311 } 312 if (n < uniforms.N && w + local_id.y < uniforms.K) { 313 var idx = TILE_SIZE * local_id.y + local_id.x; 314 ${b&&f?` 315 if (w + local_id.y < past_sequence_length) { 316 tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N]; 317 } else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { 318 tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N]; 319 } 320 `:` 321 if (w + local_id.y < uniforms.kv_sequence_length) { 322 tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N]; 323 }`} 324 ${f?` 325 if (w + local_id.y < present_sequence_length) { 326 present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx]; 327 }`:""} 328 } 329 workgroupBarrier(); 330 for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) { 331 value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x]; 332 } 333 workgroupBarrier(); 334 } 335 336 // we need to transpose output from BNSH_v to BSND_v 337 if (m < uniforms.M && n < uniforms.N) { 338 let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size 339 + headIdx * uniforms.N + n; 340 output[outputIdx] = value; 341 } 342 }`};return{name:"AttentionScore",shaderCache:{hint:`${i!==void 0};${e}`,inputDependencies:I},getRunData:()=>({outputs:T,dispatchGroup:S,programUniforms:x}),getShaderSource:E}},Xt=(e,t,r,i,a,s,n,u,l,p,h=void 0,f=void 0)=>{let g=Math.min(e.outputCount,1+(n?1:0)+(u?1:0)),y=g>1?p.pastSequenceLength:0,_=y+p.kvSequenceLength,w=l&&O.size(l.dims)>0?l:void 0,S=[t,r];g>1&&n&&O.size(n.dims)>0&&S.push(n),w&&S.push(w),h&&S.push(h),f&&S.push(f);let x=e.compute(bo(g,t,r,n,w,p,y,h,f),{inputs:S,outputs:g>1?[-1,1]:[-1]})[0];e.compute(wo(x,p.batchSize,p.numHeads,y,p.sequenceLength,_,h,f),{inputs:h&&f?[x,h,f]:[x],outputs:[]});let b=[x,i];g>1&&u&&O.size(u.dims)>0&&b.push(u),h&&b.push(h),f&&b.push(f),e.compute($o(g,x,i,u,p,y,h,f),{inputs:b,outputs:g>1?[0,2]:[0]})},vo=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],i=t.sequenceLength,a=t.inputHiddenSize,s=t.headSize,n=12,u={x:Math.ceil(t.headSize/n),y:Math.ceil(t.sequenceLength/n),z:t.batchSize*t.numHeads},l=[e.inputs[0],e.inputs[1],e.inputs[2]],p=[{type:12,data:i},{type:12,data:a},{type:12,data:s},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],h=f=>{let g=K("output_q",l[0].dataType,r),y=K("output_k",l[0].dataType,r),_=K("output_v",l[0].dataType,r),w=M("input",l[0].dataType,l[0].dims),S=M("weight",l[1].dataType,l[1].dims),x=M("bias",l[2].dataType,l[2].dims),b=w.type.storage,I=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` 343 const TILE_SIZE = ${n}u; 344 var<workgroup> tileInput: array<${b}, ${n*n}>;
345 var<workgroup> tileWeightQ: array<${b}, ${n*n}>; 346 var<workgroup> tileWeightK: array<${b}, ${n*n}>; 347 var<workgroup> tileWeightV: array<${b}, ${n*n}>; 348 ${f.registerUniforms(I).declareVariables(w,S,x,g,y,_)} 349 ${f.mainStart([n,n,1])} 350 let batchIndex = workgroup_id.z / uniforms.num_heads; 351 let headNumber = workgroup_id.z % uniforms.num_heads; 352 let m = global_id.y; 353 let n = global_id.x; 354 355 let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K; 356 let biasOffsetQ = headNumber * uniforms.head_size; 357 let biasOffsetK = uniforms.hidden_size + biasOffsetQ; 358 let biasOffsetV = uniforms.hidden_size + biasOffsetK; 359 360 var valueQ = ${b}(0); 361 var valueK = ${b}(0); 362 var valueV = ${b}(0); 363 for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { 364 if (m < uniforms.M && w + local_id.x < uniforms.K) { 365 tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x]; 366 } 367 if (n < uniforms.N && w + local_id.y < uniforms.K) { 368 let offset = n + (w + local_id.y) * uniforms.ldb; 369 tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset]; 370 tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset]; 371 tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset]; 372 } 373 workgroupBarrier(); 374 for (var k: u32 = 0u; k<TILE_SIZE && w+k < uniforms.K; k++) { 375 let inputTileOffset = TILE_SIZE * local_id.y + k; 376 let weightTileOffset = TILE_SIZE * k + local_id.x; 377 valueQ += tileInput[inputTileOffset] * tileWeightQ[weightTileOffset]; 378 valueK += tileInput[inputTileOffset] * tileWeightK[weightTileOffset]; 379 valueV += tileInput[inputTileOffset] * tileWeightV[weightTileOffset]; 380 } 381 382 workgroupBarrier(); 383 } 384 385 let headOffset = (m * uniforms.N + n) % uniforms.head_size; 386 valueQ += bias[headOffset + biasOffsetQ]; 387 valueK += bias[headOffset + biasOffsetK]; 388 valueV += bias[headOffset + biasOffsetV]; 389 390 let offset = workgroup_id.z * uniforms.M * uniforms.N; 391 if (m < uniforms.M && n < uniforms.N) { 392 let outputIdx = offset + m * uniforms.N + n; 393 output_q[outputIdx] = valueQ; 394 output_k[outputIdx] = valueK; 395 output_v[outputIdx] = valueV; 396 } 397 }`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:u,programUniforms:p}),getShaderSource:h},{inputs:l,outputs:[-1,-1,-1]})},xo=(e,t)=>{let r=_o(e.inputs,t),[i,a,s]=vo(e,r);return Xt(e,i,a,s,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r)}}),So,ko,To,Io,Kf=q(()=>{Pe(),te(),re(),xe(),ae(),So=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(i,a,s)=>{let n=a.length;if(n!==i.length)throw new Error(`${s}: num dimensions != ${n}`);a.forEach((u,l)=>{if(u!==i[l])throw new Error(`${s}: dim[${l}] do not match`)})};if(e[0].dims.length>1){let i=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,i,"Invalid input scale"),r(e[2].dims,i,"Invalid input B"),r(e[3].dims,i,"Invalid input mean"),r(e[4].dims,i,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},ko=(e,t)=>{let{epsilon:r,spatial:i,format:a}=t,s=e[0].dims,n=i?ve(s[s.length-1]):1,u=a==="NHWC"&&s.length>1?n:1,l=O.size(s)/n,p=i,h=p?s.length:s,f=M("x",e[0].dataType,e[0].dims,n),g=M("scale",e[1].dataType,e[1].dims,u),y=M("bias",e[2].dataType,e[2].dims,u),_=M("inputMean",e[3].dataType,e[3].dims,u),w=M("inputVar",e[4].dataType,e[4].dims,u),S=K("y",e[0].dataType,h,n),x=()=>{let I="";if(i)I=`let cOffset = ${s.length===1?"0u":a==="NHWC"?`outputIndices[${s.length-1}] / ${n}`:"outputIndices[1]"};`;else if(a==="NCHW")I=` 398 ${S.indicesSet("outputIndices","0","0")} 399 let cOffset = ${S.indicesToOffset("outputIndices")};`;else{I=`var cIndices = ${g.type.indices}(0);
400 cIndices[0] = outputIndices[${s.length-1}];`;for(let T=1;T<g.rank;T++)I+=`cIndices[${T}] = outputIndices[${T}];`;I+=`let cOffset = ${g.indicesToOffset("cIndices")};`}return I},b=I=>` 401 const epsilon = ${r}; 402 ${I.registerUniform("outputSize","u32").declareVariables(f,g,y,_,w,S)} 403 ${I.mainStart()} 404 ${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 405 var outputIndices = ${S.offsetToIndices(`global_idx * ${n}`)}; 406 ${x()} 407 let scale = ${g.getByOffset("cOffset")}; 408 let bias = ${y.getByOffset("cOffset")}; 409 let inputMean = ${_.getByOffset("cOffset")}; 410 let inputVar = ${w.getByOffset("cOffset")}; 411 let x = ${f.getByOffset("global_idx")}; 412 let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; 413 ${S.setByOffset("global_idx","value")} 414 }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${i}_${n}`,inputDependencies:p?["rank","type","type","type","type"]:void 0},getShaderSource:b,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:p?[{type:12,data:l},...Y(s)]:[{type:12,data:l}]})}},To=e=>he(e),Io=(e,t)=>{let{inputs:r,outputCount:i}=e,a=To({...t,outputCount:i});if(ge.webgpu.validateInputContent&&So(r,a),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(ko(r,a))}}),Eo,zo,Co,Qf=q(()=>{re(),ae(),Eo=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},zo=e=>{let t=e[0].dims,r=e[0].dims[2],i=O.size(t)/4,a=e[0].dataType,s=M("input",a,t,4),n=M("bias",a,[r],4),u=M("residual",a,t,4),l=K("output",a,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:p=>` 415 const channels = ${r}u / 4; 416 ${p.declareVariables(s,n,u,l)} 417 418 ${p.mainStart()} 419 ${p.guardAgainstOutOfBoundsWorkgroupSizes(i)} 420 let value = ${s.getByOffset("global_idx")} 421 + ${n.getByOffset("global_idx % channels")} + ${u.getByOffset("global_idx")}; 422 ${l.setByOffset("global_idx","value")} 423 }`}},Co=e=>{Eo(e.inputs),e.compute(zo(e.inputs))}}),Ao,ce,Oo,Ro,Bo,Mo,No,Do,Po,Uo,qo,Wo,Lo,Vo,Go,Ho,Jt,Fo,zr,jo,Ko,Qo,Zo,Yo,Xo,Jo,eu,tu,ru,iu,au,nu,su,ou,uu,Gi,lu,Hi,Fi,du,pu,cu,hu,fu,mu,ji=q(()=>{te(),re(),xe(),ae(),Ao=(e,t,r,i,a,s,n)=>{let u=Math.ceil(t/4),l="";typeof a=="string"?l=`${a}(a)`:l=a("a");let p=M("inputData",r,[u],4),h=K("outputData",i,[u],4),f=[{name:"vec_size",type:"u32"}];return n&&f.push(...n),` 424 ${e.registerUniforms(f).declareVariables(p,h)} 425 426 ${s??""} 427 428 ${e.mainStart()} 429 ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} 430 431 let a = ${p.getByOffset("global_idx")}; 432 ${h.setByOffset("global_idx",l)} 433 }`},ce=(e,t,r,i,a,s=e.dataType,n,u)=>{let l=[{type:12,data:Math.ceil(O.size(e.dims)/4)}];return n&&l.push(...n),{name:t,shaderCache:{hint:a,inputDependencies:["type"]},getShaderSource:p=>Ao(p,O.size(e.dims),e.dataType,s,r,i,u),getRunData:p=>({outputs:[{dims:e.dims,dataType:s}],dispatchGroup:{x:Math.ceil(O.size(p[0].dims)/64/4)},programUniforms:l})}},Oo=e=>{e.compute(ce(e.inputs[0],"Abs","abs"))},Ro=e=>{e.compute(ce(e.inputs[0],"Acos","acos"))},Bo=e=>{e.compute(ce(e.inputs[0],"Acosh","acosh"))},Mo=e=>{e.compute(ce(e.inputs[0],"Asin","asin"))},No=e=>{e.compute(ce(e.inputs[0],"Asinh","asinh"))},Do=e=>{e.compute(ce(e.inputs[0],"Atan","atan"))},Po=e=>{e.compute(ce(e.inputs[0],"Atanh","atanh"))},Uo=e=>he(e),qo=(e,t)=>{let r;switch(t.to){case 10:r="vec4<f16>";break;case 1:r="vec4<f32>";break;case 12:r="vec4<u32>";break;case 6:r="vec4<i32>";break;case 9:r="vec4<bool>";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(ce(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},Wo=e=>{let t,r,i=e.length>=2&&e[1].data!==0,a=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=i?e[1].getFloat32Array()[0]:-34028234663852886e22,r=a?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=i?e[1].getUint16Array()[0]:64511,r=a?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return he({min:t,max:r})},Lo=(e,t)=>
vendor: 9,714 bytes, lines 433-539
433{let r=t||Wo(e.inputs),i=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Clip",a=>`clamp(${a}, vec4<${i}>(uniforms.min), vec4<${i}>(uniforms.max))`,void 0,r.cacheKey,void 0,[{type:e.inputs[0].dataType,data:r.min},{type:e.inputs[0].dataType,data:r.max}],[{name:"min",type:i},{name:"max",type:i}]),{inputs:[0]})},Vo=e=>{e.compute(ce(e.inputs[0],"Ceil","ceil"))},Go=e=>{e.compute(ce(e.inputs[0],"Cos","cos"))},Ho=e=>{e.compute(ce(e.inputs[0],"Cosh","cosh"))},Jt=e=>he(e),Fo=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Elu",i=>`elu_vf32(${i})`,` 434 const elu_alpha_ = ${r}(${t.alpha}); 435 436 fn elu_f32(a: ${r}) -> ${r} { 437 return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); 438 } 439 440 fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> { 441 return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); 442 }`,t.cacheKey))},zr=(e="f32")=>` 443const r0: ${e} = 0.3275911; 444const r1: ${e} = 0.254829592; 445const r2: ${e} = -0.284496736; 446const r3: ${e} = 1.421413741; 447const r4: ${e} = -1.453152027; 448const r5: ${e} = 1.061405429; 449 450fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { 451 let absv = abs(v); 452 let x = 1.0 / (1.0 + r0 * absv); 453 return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); 454}`,jo=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,zr(t)))},Ko=e=>{e.compute(ce(e.inputs[0],"Exp","exp"))},Qo=e=>{e.compute(ce(e.inputs[0],"Floor","floor"))},Zo=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,zr(t)))},Yo=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"LeakyRelu",i=>`select(leaky_relu_alpha_ * ${i}, ${i}, ${i} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},Xo=e=>{e.compute(ce(e.inputs[0],"Not",t=>`!${t}`))},Jo=e=>{e.compute(ce(e.inputs[0],"Neg",t=>`-${t}`))},eu=e=>{e.compute(ce(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},tu=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},ru=e=>{e.compute(ce(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},iu=e=>he(e),au=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"HardSigmoid",i=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${i} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},nu=e=>{e.compute(ce(e.inputs[0],"Sin","sin"))},su=e=>{e.compute(ce(e.inputs[0],"Sinh","sinh"))},ou=e=>{e.compute(ce(e.inputs[0],"Sqrt","sqrt"))},uu=e=>{e.compute(ce(e.inputs[0],"Tan","tan"))},Gi=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,lu=e=>{e.compute(ce(e.inputs[0],"Tanh",Gi))},Hi=(e="f32")=>` 455const fast_gelu_a: ${e} = 0.5; 456const fast_gelu_b: ${e} = 0.7978845608028654; 457const fast_gelu_c: ${e} = 0.035677408136300125; 458 459fn tanh_v(v: vec4<${e}>) -> vec4<${e}> { 460 return ${Gi("v")}; 461} 462`,Fi=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,du=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"FastGelu",Fi,Hi(t),void 0,e.inputs[0].dataType))},pu=(e,t)=>{let r=Ee(e.inputs[0].dataType);return e.compute(ce(e.inputs[0],"ThresholdedRelu",i=>`select(vec4<${r}>(0.0), ${i}, ${i} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},cu=e=>{e.compute(ce(e.inputs[0],"Log","log"))},hu=(e,t)=>` 463const alpha = vec4<${e}>(${t}); 464const one = ${e}(1.0); 465const zero = ${e}(0.0); 466 467fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { 468 let v = x *alpha; 469 var x1 : vec4<${e}>; 470 for (var i = 0; i < 4; i = i + 1) { 471 if (v[i] >= zero) { 472 x1[i] = one / (one + exp(-v[i])); 473 } else { 474 x1[i] = one - one / (one + exp(v[i])); 475 } 476 } 477 return x * x1; 478} 479`,fu=e=>`quick_gelu_impl(${e})`,mu=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ce(e.inputs[0],"QuickGelu",fu,hu(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),gu,yu,_u,Zf=q(()=>{re(),ae(),ji(),gu=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},yu=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=M("input",e[0].dataType,e[0].dims,4),i=M("bias",e[0].dataType,[e[0].dims[2]],4),a=K("output",e[0].dataType,t,4),s=O.size(t)/4,n=ke(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)}}),getShaderSource:u=>` 480 const M_SQRT2 = sqrt(2.0); 481 const halfChannels = ${e[0].dims[2]/4/2}u; 482 483 ${u.declareVariables(r,i,a)} 484 485 ${zr(n)} 486 487 ${u.mainStart()} 488 ${u.guardAgainstOutOfBoundsWorkgroupSizes(s)} 489 let biasIdx = global_idx % halfChannels; 490 let batchIndex = global_idx / halfChannels; 491 let inputOffset = biasIdx + batchIndex * halfChannels * 2; 492 let valueLeft = input[inputOffset] + bias[biasIdx]; 493 let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels]; 494 let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); 495 496 ${a.setByOffset("global_idx","valueLeft * geluRight")} 497 }`}},_u=e=>{gu(e.inputs),e.compute(yu(e.inputs))}}),wu,bu,Fe,$u,vu,xu,Su,ku,Tu,Iu,Eu,zu,Cu,Yf=q(()=>{te(),re(),ae(),wu=(e,t,r,i,a,s,n,u,l,p,h,f)=>{let g,y;typeof u=="string"?g=y=(b,I)=>`${u}((${b}),(${I}))`:typeof u=="function"?g=y=u:(g=u.scalar,y=u.vector);let _=K("outputData",h,i.length,4),w=M("aData",l,t.length,4),S=M("bData",p,r.length,4),x;if(a)if(s){let b=O.size(t)===1,I=O.size(r)===1,T=t.length>0&&t[t.length-1]%4===0,E=r.length>0&&r[r.length-1]%4===0;b||I?x=_.setByOffset("global_idx",y(b?`${w.type.value}(${w.getByOffset("0")}.x)`:w.getByOffset("global_idx"),I?`${S.type.value}(${S.getByOffset("0")}.x)`:S.getByOffset("global_idx"))):x=` 498 let outputIndices = ${_.offsetToIndices("global_idx * 4u")}; 499 let offsetA = ${w.broadcastedIndicesToOffset("outputIndices",_)}; 500 let offsetB = ${S.broadcastedIndicesToOffset("outputIndices",_)}; 501 ${_.setByOffset("global_idx",y(n||T?w.getByOffset("offsetA / 4u"):`${w.type.value}(${w.getByOffset("offsetA / 4u")}[offsetA % 4u])`,n||E?S.getByOffset("offsetB / 4u"):`${S.type.value}(${S.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} 502 `}else x=_.setByOffset("global_idx",y(w.getByOffset("global_idx"),S.getByOffset("global_idx")));else{if(!s)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let b=(I,T,E="")=>{let C=`aData[indexA${T}][componentA${T}]`,A=`bData[indexB${T}][componentB${T}]`;return` 503 let outputIndices${T} = ${_.offsetToIndices(`global_idx * 4u + ${T}u`)}; 504 let offsetA${T} = ${w.broadcastedIndicesToOffset(`outputIndices${T}`,_)}; 505 let offsetB${T} = ${S.broadcastedIndicesToOffset(`outputIndices${T}`,_)}; 506 let indexA${T} = offsetA${T} / 4u; 507 let indexB${T} = offsetB${T} / 4u; 508 let componentA${T} = offsetA${T} % 4u; 509 let componentB${T} = offsetB${T} % 4u; 510 ${I}[${T}] = ${E}(${g(C,A)}); 511 `};h===9?x=` 512 var data = vec4<u32>(0); 513 ${b("data",0,"u32")} 514 ${b("data",1,"u32")} 515 ${b("data",2,"u32")} 516 ${b("data",3,"u32")} 517 outputData[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:x=` 518 ${b("outputData[global_idx]",0)} 519 ${b("outputData[global_idx]",1)} 520 ${b("outputData[global_idx]",2)} 521 ${b("outputData[global_idx]",3)} 522 `}return` 523 ${e.registerUniform("vec_size","u32").declareVariables(w,S,_)} 524 525 ${f??""} 526 527 ${e.mainStart()} 528 ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} 529 ${x} 530 }`},bu=(e,t,r,i,a,s,n=r.dataType)=>{let u=r.dims.map(Number),l=i.dims.map(Number),p=!O.areEqual(u,l),h=u,f=O.size(u),g=!1,y=!1,_=[p];if(p){let w=Ut.calcShape(u,l,!1);if(!w)throw new Error("Can't perform binary op on the given tensors");h=w.slice(),f=O.size(h);let S=O.size(u)===1,x=O.size(l)===1,b=u.length>0&&u[u.length-1]%4===0,I=l.length>0&&l[l.length-1]%4===0;_.push(S),_.push(x),_.push(b),_.push(I);let T=1;for(let E=1;E<h.length;E++){let C=u[u.length-E],A=l[l.length-E];if(C===A)T*=C;else break}T%4===0?(y=!0,g=!0):(S||x||b||I)&&(g=!0)}else g=!0;return _.push(g),{name:e,shaderCache:{hint:t+_.map(w=>w.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:w=>wu(w,u,l,h,g,p,y,a,r.dataType,i.dataType,n,s),getRunData:()=>({outputs:[{dims:h,dataType:n}],dispatchGroup:{x:Math.ceil(f/64/4)},programUniforms:[{type:12,data:Math.ceil(O.size(h)/4)},...Y(u,l,h)]})}},Fe=(e,t,r,i,a,s)=>{e.compute(bu(t,a??"",e.inputs[0],e.inputs[1],r,i,s))},$u=e=>{Fe(e,"Add",(t,r)=>`${t}+${r}`)},vu=e=>{Fe(e,"Div",(t,r)=>`${t}/${r}`)},xu=e=>{Fe(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4<u32>(${t}==${r})`},void 0,void 0,9)},Su=e=>{Fe(e,"Mul",(t,r)=>`${t}*${r}`)},ku=e=>{let t=M("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;Fe(e,"Pow",{scalar:(r,i)=>`pow_custom(${r},${i})`,vector:(r,i)=>`pow_vector_custom(${r},${i})`},` 531 fn pow_custom(a : ${t}, b : ${t}) -> ${t} { 532 if (b == ${t}(0.0)) { 533 return ${t}(1.0); 534 } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) { 535 return ${t}(pow(f32(a), f32(b))); // NaN 536 } 537 return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b)))); 538 } 539 fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>
539) -> vec4<${t}> { 540 // TODO: implement vectorized pow 541 return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); 542 } 543 `)},Tu=e=>{Fe(e,"Sub",(t,r)=>`${t}-${r}`)},Iu=e=>{Fe(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},Eu=e=>{Fe(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},zu=e=>{Fe(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},Cu=e=>{Fe(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}}),Au,Ou,Ru,Bu,Mu,Nu,Xf=q(()=>{te(),re(),xe(),ae(),Au=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,i=e[r],a=i.dataType,s=i.dims.length;e.forEach((n,u)=>{if(u!==r){if(n.dataType!==a)throw new Error("input tensors should be one type");if(n.dims.length!==s)throw new Error("input tensors should have the same shape");n.dims.forEach((l,p)=>{if(p!==t&&l!==i.dims[p])throw new Error("non concat dimensions must match")})}})},Ou=(e,t)=>` 544 fn calculateInputIndex(index: u32) -> u32 { 545 let sizeInConcatAxis = array<u32, ${e}u>(${t}); 546 for (var i: u32 = 0u; i < ${e}; i += 1u ) { 547 if (index < sizeInConcatAxis[i]) { 548 return i; 549 } 550 } 551 return ${e}u; 552 }`,Ru=(e,t)=>{let r=e.length,i=[];for(let a=0;a<r;++a){let s=t.setByOffset("global_idx",e[a].getByIndices("indices"));r===1?i.push(s):a===0?i.push(`if (inputIndex == ${a}u) { ${s} }`):a===r-1?i.push(`else { ${s} }`):i.push(`else if (inputIndex == ${a}) { ${s} }`)}return i.join(` 553`)},Bu=(e,t,r,i)=>{let a=O.size(r),s=new Array(e.length),n=new Array(e.length),u=0,l=[],p=[],h=[{type:12,data:a}];for(let w=0;w<e.length;++w)u+=e[w].dims[t],s[w]=u,p.push(e[w].dims.length),n[w]=M(`input${w}`,i,p[w]),l.push("rank"),h.push({type:12,data:s[w]});for(let w=0;w<e.length;++w)h.push(...Y(e[w].dims));h.push(...Y(r));let f=K("output",i,r.length),g=f.indicesGet("indices",t),y=Array.from(Array(s.length).keys()).map(w=>`uniforms.sizeInConcatAxis${w}`).join(","),_=w=>` 554 555 ${(()=>{w.registerUniform("outputSize","u32");for(let S=0;S<e.length;S++)w.registerUniform(`sizeInConcatAxis${S}`,"u32");return w.declareVariables(...n,f)})()} 556 557 ${Ou(s.length,y)} 558 559 ${w.mainStart()} 560 ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 561 562 var indices = ${f.offsetToIndices("global_idx")}; 563 564 let inputIndex = calculateInputIndex(${g}); 565 if (inputIndex != 0u) { 566 let sizeInConcatAxis = array<u32, ${s.length}u>(${y}); 567 ${g} -= sizeInConcatAxis[inputIndex - 1u]; 568 } 569 570 ${Ru(n,f)} 571 }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:r,dataType:i}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:h}),getShaderSource:_}}
571,Mu=(e,t)=>{let r=e.inputs,i=r[0].dims,a=O.normalizeAxis(t.axis,i.length);Au(r,a);let s=i.slice();s[a]=r.reduce((u,l)=>u+(l.dims.length>a?l.dims[a]:0),0);let n=r.filter(u=>O.size(u.dims)>0);e.compute(Bu(n,a,s,r[0].dataType),{inputs:n})},Nu=e=>he({axis:e.axis})}),kt,Tt,It,Ki,Et=q(()=>{te(),re(),kt=(e,t,r="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); 572 value = sign(value) * (1.0 - e2x) / (1.0 + e2x); 573 `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},Tt=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},It=(e,t)=>
573{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},Ki=e=>{let t=(e==null?void 0:e.activation)||"";if(t==="HardSigmoid"){let[r,i]=(e==null?void 0:e.activation_params)||[.2,.5];return{activation:t,alpha:r,beta:i}}else if(t==="Clip"){let[r,i]=(e==null?void 0:e.activation_params)||[as,ns];return{activation:t,clipMax:i,clipMin:r}}else if(t==="LeakyRelu"){let[r]=(e==null?void 0:e.activation_params)||[.01];return{activation:t,alpha:r}}return{activation:t}}}),Ie,Du,Qi=q(()=>{Ie=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},Du=e=>` 574 ${e?"value = value + getBiasByOutputCoords(coords);":""} 575 `}),Pu,Jf=q(()=>{Pu=e=>` 576fn getIndexFromCoords4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 { 577 return dot(coords, vec4<i32>( 578 shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); 579} 580fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 { 581 return dot(coords, vec4<i32>( 582 i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); 583} 584`}),er,Zi,Yi=q(()=>{te(),re(),ae(),Et(),er=(e,t,r,i,a)=>{let s=i-r;return` 585 ${Array.from({length:r}).map((n,u)=>` 586 if (${Z(t.shape,u,t.rank)} != 1) { 587 ${t.indicesSet(e,u,Z(a,u+s,i))} 588 } else { 589 ${t.indicesSet(e,u,0)} 590 }`).join("")} 591`},Zi=(e,t,r,i,a=!1,s)=>{let n=e[0].dims,u=e[1].dims,l=n[n.length-2],p=u[u.length-1],h=n[n.length-1],f=ve(p),g=ve(h),y=ve(l),_=O.size(r)/f/y,w=e.length>2,S=i?i.slice(0,-2):r.slice(0,-2),x=[O.size(S),l,p],b=[{type:12,data:_},{type:12,data:l},{type:12,data:p},{type:12,data:h}];Tt(t,b),b.push(...Y(S,n,u)),w&&b.push(...Y(e[2].dims)),b.push(...Y(x));let I=T=>{let E=Di("batch_dims",e[0].dataType,S.length),C=M("a",e[0].dataType,n.length,g),A=M("b",e[1].dataType,u.length,f),v=K("output",e[0].dataType,x.length,f),N=ke(v.type.tensor),D=kt(t,v.type.value,N),H=[C,A],G="";if(w){let j=a?f:1;H.push(M("bias",e[2].dataType,e[2].dims.length,j)),G=`${a?`value += bias[col / ${j}];`:`value += ${v.type.value}(bias[row + i]);`}`}let Q=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];It(t,Q);let R=()=>{let j=`var a_data: ${C.type.value};`;for(let F=0;F<g;F++)j+=` 592 let b_data${F} = b[(b_offset + (k + ${F}) * uniforms.N + col) / ${f}];`;for(let F=0;F<y;F++){j+=`a_data = a[(a_offset + (row + ${F}) * uniforms.K + k) / ${g}];`;for(let X=0;X<g;X++)j+=` 593 values[${F}] = fma(${A.type.value}(a_data${g===1?"":`[${X}]`}), b_data${X}, values[${F}]); 594`}return j};return` 595 ${T.registerUniforms(Q).registerInternalVariables(E).declareVariables(...H,v)} 596 ${T.mainStart()} 597 ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 598 let col = (global_idx % (uniforms.N / ${f})) * ${f}; 599 var index1 = global_idx / (uniforms.N / ${f}); 600 let stride1 = uniforms.M / ${y}; 601 let row = (index1 % stride1) * ${y}; 602 let batch = index1 / stride1; 603 604 ${r.length===2?"":`let batch_indices = ${E.offsetToIndices("batch")};`} 605 606 var a_indices: ${C.type.indices}; 607 ${er("a_indices",C,C.rank-2,E.rank,"batch_indices")} 608 ${C.indicesSet("a_indices",C.rank-2,0)} 609 ${C.indicesSet("a_indices",C.rank-1,0)} 610 let a_offset = ${C.indicesToOffset("a_indices")}; 611 612 var b_indices: ${A.type.indices}; 613 ${er("b_indices",A,A.rank-2,E.rank,"batch_indices")} 614 ${A.indicesSet("b_indices",A.rank-2,0)} 615 ${A.indicesSet("b_indices",A.rank-1,0)} 616 let b_offset = ${A.indicesToOffset("b_indices")}; 617 var values: array<${v.type.value}, ${y}>; 618 for (var k: u32 = 0u; k < uniforms.K; k = k + ${g}) { 619 ${R()} 620 } 621 for (var i = 0u; i < ${y}u; i++) { 622 var value = values[i]; 623 ${G} 624 ${D} 625 let cur_indices = ${v.type.indices}(batch, row + i, col); 626 let offset = ${v.indicesToOffset("cur_indices")}; 627 ${v.setByOffset(`offset / ${f}`,"value")}; 628 } 629 } 630 `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${f};${g};${y};${a}`,inputDependencies:w?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:s?s(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:b}),getShaderSource:I}}}),Uu,qu,Xi,Ji,Wu,ea,Lu,Cr,ta=q(()=>{te(),re(),ae(),Et(),Yi(),Qi(),Uu=(e,t)=>e?` 631 mm_Asub[inputRow][inputCol] = mm_readA(batch, 632 kStart + inputRow, 633 globalRowStart / innerElementSize + inputCol${t?", batchIndices":""}); 634 `:` 635 mm_Asub[inputRow][inputCol] = mm_readA(batch, 636 globalRow + innerRow, 637 kStart / innerElementSize + inputCol${t?", batchIndices":""}); 638 `,qu=(e,t)=>
vendor: 8,748 bytes, lines 638-835
638e?` 639 let ACached0 = mm_Asub[k * innerElementSize][localRow]; 640 let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; 641 let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; 642 ${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"} 643 for (var i = 0; i < rowPerThread; i = i + 1) { 644 acc[i] = BCached0 * ACached0[i] + acc[i]; 645 acc[i] = BCached1 * ACached1[i] + acc[i]; 646 acc[i] = BCached2 * ACached2[i] + acc[i]; 647 ${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"} 648 }`:` 649 for (var i = 0; i < rowPerThread; i = i + 1) { 650 let ACached = mm_Asub[tileRow + i][k]; 651 acc[i] = BCached0 * ACached.x + acc[i]; 652 acc[i] = BCached1 * ACached.y + acc[i]; 653 acc[i] = BCached2 * ACached.z + acc[i]; 654 ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"} 655 }`,Xi=(e,t,r="f32",i,a=!1,s=32,n=!1,u=32)=>{let l=t[1]*e[1],p=t[0]*e[0],h=a?l:s,f=a?s:l,g=h/t[0],y=s/t[1];if(!((a&&g===4&&e[1]===4||!a&&(g===3||g===4))&&h%t[0]===0&&s%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${a} is true, innerElementSize ${g} and workPerThread[1] ${e[1]} must be 4. 656 Otherwise, innerElementSize ${g} must be 3 or 4. 657 tileAWidth ${h} must be divisible by workgroupSize[0]${t[0]}. tileInner ${s} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` 658var<workgroup> mm_Asub: array<array<vec${g}<${r}>, ${h/g}>, ${f}>; 659var<workgroup> mm_Bsub: array<array<vec4<${r}>, ${p/e[0]}>, ${s}>; 660 661const rowPerThread = ${e[1]}; 662const colPerThread = ${e[0]}; 663const innerElementSize = ${g}; 664const tileInner = ${s}; 665 666@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) 667fn main(@builtin(local_invocation_id) localId : vec3<u32>, 668 @builtin(global_invocation_id) globalId : vec3<u32>, 669 @builtin(workgroup_id) workgroupId : vec3<u32>) { 670 let localRow = i32(localId.y); 671 let tileRow = localRow * rowPerThread; 672 let tileCol = i32(localId.x); 673 674 let globalRow =i32(globalId.y) * rowPerThread; 675 let globalCol = i32(globalId.x); 676 let batch = ${n?"0":"i32(globalId.z)"}; 677 ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""} 678 let globalRowStart = i32(workgroupId.y) * ${l}; 679 680 let num_tiles = ${n?`${Math.ceil(u/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; 681 var kStart = ${n?`i32(globalId.z) * ${u}`:"0"}; 682 683 var acc: array<vec4<${r}>, rowPerThread>; 684 685 // Loop over shared dimension. 686 let tileRowB = localRow * ${y}; 687 for (var t = 0; t < num_tiles; t = t + 1) { 688 // Load one tile of A into local memory. 689 for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { 690 let inputRow = tileRow + innerRow; 691 let inputCol = tileCol; 692 ${Uu(a,i)} 693 } 694 695 // Load one tile of B into local memory. 696 for (var innerRow = 0; innerRow < ${y}; innerRow = innerRow + 1) { 697 let inputRow = tileRowB + innerRow; 698 let inputCol = tileCol; 699 mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${i?", batchIndices":""}); 700 } 701 kStart = kStart + tileInner; 702 workgroupBarrier(); 703 704 // Compute acc values for a single thread. 705 for (var k = 0; k < tileInner / innerElementSize; k = k + 1) { 706 let BCached0 = mm_Bsub[k * innerElementSize][tileCol]; 707 let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol]; 708 let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; 709 ${g===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} 710 711 ${qu(a,g)} 712 } 713 714 workgroupBarrier(); 715 } 716 717 for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { 718 mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); 719 } 720}`},Ji=(e,t)=>e?` 721 mm_Asub[inputRow][inputCol] = mm_readA(batch, 722 kStart + inputRow, 723 globalRowStart + inputCol${t?", batchIndices":""}); 724 `:` 725 mm_Asub[inputRow][inputCol] = mm_readA(batch, 726 globalRowStart + inputRow, 727 kStart + inputCol${t?", batchIndices":""}); 728 `,Wu=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",ea=(e,t,r="f32",i,a=!1,s=32,n=!1,u=32,l=!1)=>{let p=e[1]*t[1],h=e[0]*t[0],f=a?p:s,g=a?s:p;if(!(g%t[1]===0&&f%t[0]===0&&s%t[1]===0))throw new Error(`tileAHight ${g} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${f} must be divisible by workgroupSize[0]${t[0]}, tileInner ${s} must be divisible by workgroupSize[1]${t[1]}`);let y=g/t[1],_=f/t[0],w=s/t[1],S=l?` 729 let localRow = i32(localId.y); 730 let localCol = i32(localId.x); 731 let globalRowStart = i32(workgroupId.y) * ${p}; 732 let globalColStart = i32(workgroupId.x) * ${h}; 733 734 // Loop over shared dimension. 735 for (var t = 0; t < num_tiles; t = t + 1) { 736 // Load one tile of A into local memory. 737 for (var inputRow = localRow; inputRow < ${g}; inputRow = inputRow + ${t[1]}) { 738 for (var inputCol = localCol; inputCol < ${f}; inputCol = inputCol + ${t[0]}) { 739 ${Ji(a,i)} 740 } 741 } 742 // Load one tile of B into local memory. 743 for (var inputRow = localRow; inputRow < ${s}; inputRow = inputRow + ${t[1]}) { 744 for (var inputCol = localCol; inputCol < ${h}; inputCol = inputCol + ${t[0]}) { 745 mm_Bsub[inputRow][inputCol] = mm_readB(batch, 746 kStart + inputRow, 747 globalColStart + inputCol${i?", batchIndices":""}); 748 } 749 } 750 kStart = kStart + tileInner; 751 workgroupBarrier(); 752 753 // Compute acc values for a single thread. 754 var BCached : array<${r}, colPerThread>; 755 for (var k = 0; k < tileInner; k = k + 1) { 756 for (var inner = 0; inner < colPerThread; inner = inner + 1) { 757 BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}]; 758 } 759 for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { 760 let ACached = ${a?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`} 761 for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { 762 acc[innerRow][innerCol] = acc[innerRow][innerCol] + 763 ACached * BCached[innerCol]; 764 } 765 } 766 } 767 workgroupBarrier(); 768 } 769 for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { 770 let gRow = globalRowStart + localRow + innerRow * ${t[1]}; 771 for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { 772 let gCol = globalColStart + localCol + innerCol * ${t[0]}; 773 mm_write(batch, gRow, gCol, acc[innerRow][innerCol]); 774 } 775 } 776 `:` 777let tileRow = i32(localId.y) * rowPerThread; 778let tileCol = i32(localId.x) * colPerThread; 779 780let globalRow = i32(globalId.y) * rowPerThread; 781let globalCol = i32(globalId.x) * colPerThread; 782let globalRowStart = i32(workgroupId.y) * ${p}; 783 784let tileRowA = i32(localId.y) * ${y}; 785let tileColA = i32(localId.x) * ${_}; 786let tileRowB = i32(localId.y) * ${w}; 787// Loop over shared dimension. 788for (var t = 0; t < num_tiles; t = t + 1) { 789 // Load one tile of A into local memory. 790 for (var innerRow = 0; innerRow < ${y}; innerRow = innerRow + 1) { 791 for (var innerCol = 0; innerCol < ${_}; innerCol = innerCol + 1) { 792 let inputRow = tileRowA + innerRow; 793 let inputCol = tileColA + innerCol; 794 ${Ji(a,i)} 795 } 796 } 797 798 // Load one tile of B into local memory. 799 for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { 800 for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { 801 let inputRow = tileRowB + innerRow; 802 let inputCol = tileCol + innerCol; 803 mm_Bsub[inputRow][inputCol] = mm_readB(batch, 804 kStart + inputRow, 805 globalCol + innerCol${i?", batchIndices":""}); 806 } 807 } 808 kStart = kStart + tileInner; 809 workgroupBarrier(); 810 811 // Compute acc values for a single thread. 812 var BCached : array<${r}, colPerThread>; 813 for (var k = 0; k < tileInner; k = k + 1) { 814 for (var inner = 0; inner < colPerThread; inner = inner + 1) { 815 BCached[inner] = mm_Bsub[k][tileCol + inner]; 816 } 817 818 for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { 819 ${Wu(a)} 820 for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { 821 acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; 822 } 823 } 824 } 825 826 workgroupBarrier(); 827} 828 829for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { 830 for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { 831 mm_write(batch, globalRow + innerRow, globalCol + innerCol, 832 acc[innerRow][innerCol]); 833 } 834} 835`;return`
836 var<workgroup> mm_Asub : array<array<${r}, ${f}>, ${g}>; 837 var<workgroup> mm_Bsub : array<array<${r}, ${h}>, ${s}>; 838 const rowPerThread = ${e[1]}; 839 const colPerThread = ${e[0]}; 840 const tileInner = ${s}; 841 842@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) 843fn main(@builtin(local_invocation_id) localId : vec3<u32>, 844 @builtin(global_invocation_id) globalId : vec3<u32>, 845 @builtin(workgroup_id) workgroupId : vec3<u32>) { 846 let batch = ${n?"0":"i32(globalId.z)"}; 847 ${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""} 848 let num_tiles = ${n?`${Math.ceil(u/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; 849 var kStart = ${n?`i32(globalId.z) * ${u}`:"0"}; 850 851 var acc : array<array<${r}, colPerThread>, rowPerThread>; 852 ${S} 853 } 854`},Lu=(e,t,r,i,a=!1)=>{let[s,n,u,l]=i,p=ke(i[0].type.tensor);return` 855 fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${s.type.indices}) -> ${Ie(e,p)} { 856 var value = ${Ie(e,p)}(0.0); 857 let col = colIn * ${e}; 858 if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) 859 { 860 var aIndices: ${n.type.indices}; 861 ${er("aIndices",n,n.rank-2,s.rank,"batchIndices")} 862 ${n.indicesSet("aIndices",n.rank-2,"u32(row)")} 863 ${n.indicesSet("aIndices",n.rank-1,"u32(colIn)")} 864 value = ${n.getByIndices("aIndices")}; 865 } 866 return value; 867 } 868 869 fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${s.type.indices}) -> ${Ie(e,p)} { 870 var value = ${Ie(e,p)}(0.0); 871 let col = colIn * ${e}; 872 if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) 873 { 874 var bIndices: ${u.type.indices}; 875 ${er("bIndices",u,u.rank-2,s.rank,"batchIndices")} 876 ${u.indicesSet("bIndices",u.rank-2,"u32(row)")} 877 ${u.indicesSet("bIndices",u.rank-1,"u32(colIn)")} 878 value = ${u.getByIndices("bIndices")}; 879 } 880 return value; 881 } 882 883 fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Ie(e,p)}) { 884 let col = colIn * ${e}; 885 if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { 886 var value = valueIn; 887 let coords = vec3<i32>(batch, row, colIn); 888 ${t?`value = value + ${a?"bias[colIn]":`${Ie(e,p)}(bias[row])`};`:""} 889 ${r} 890 ${l.setByIndices("vec3<u32>(coords)","value")} 891 } 892 } 893 `},Cr=(e,t,r,i,a=!1,s)=>{let n=e[0].dims,u=e[1].dims,l=n.slice(0,-2),p=u.slice(0,-2),h=i?i.slice(0,-2):r.slice(0,-2),f=O.size(h),g=n[n.length-2],y=n[n.length-1],_=u[u.length-1],w=y%4===0&&_%4===0,S=g<=8?[4,1,1]:[4,4,1],x=[8,8,1],b=[Math.ceil(_/x[0]/S[0]),Math.ceil(g/x[1]/S[1]),Math.ceil(f/x[2]/S[2])],I=w?4:1,T=[...l,g,y/I],E=T.length,C=[...p,y,_/I],A=C.length,v=[f,g,_/I],N=[{type:6,data:g},{type:6,data:_},{type:6,data:y}];Tt(t,N),N.push(...Y(h,T,C));let D=["rank","rank"],H=e.length>2;H&&(N.push(...Y(e[2].dims)),D.push("rank")),N.push(...Y(v));let G=Q=>{let R=h.length,j=Di("batchDims",e[0].dataType,R,1),F=ke(e[0].dataType),X=M("a",e[0].dataType,E,I),le=M("b",e[1].dataType,A,I),W=K("result",e[0].dataType,v.length,I),me=[X,le];if(H){let $e=a?I:1;me.push(M("bias",e[2].dataType,e[2].dims.length,$e))}let P=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];It(t,P);let U=ke(W.type.tensor),J=kt(t,W.type.value,U),ee=Lu(I,H,J,[j,X,le,W],a);return` 894 ${Q.registerUniforms(P).registerInternalVariables(j).declareVariables(...me,W)} 895 ${ee} 896 ${w?Xi(S,x,F,j):ea(S,x,F,j)} 897 `};return{name:"MatMul",shaderCache:{hint:`${S};${t.activation};${w};${a}`,inputDependencies:D},getRunData:()=>({outputs:[{dims:s?s(r):r,dataType:e[0].dataType}],dispatchGroup:{x:b[0],y:b[1],z:b[2]},programUniforms:N}),getShaderSource:G}}}),Vu,Gu,em=q(()=>{te(),rt(),ae(),Et(),Qi(),Jf(),ta(),Vu=(e,t,r,i,a=!1,s,n=4,u=4,l=4,p="f32")=>{let h=N=>{switch(N){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${p}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${N} is not supported.`)}},f=N=>{switch(N){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${N} is not supported.`)}},g=e?`
898 let coord = vec4<i32>(batch, xRow, xCol, xCh); 899 `:` 900 let coord = vec4<i32>(batch, xCh, xRow, xCol); 901 `,y=e?` 902 let coords = vec4<i32>( 903 batch, 904 row / outWidth, 905 row % outWidth, 906 col); 907 `:` 908 let coords = vec4<i32>( 909 batch, 910 row, 911 col / outWidth, 912 col % outWidth); 913 `,_=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",w=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",S=e?"row":"col",x=e?"col":"row",b=` 914 let inChannels = i32(uniforms.w_shape[2]); 915 let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; 916 let outRow = ${S} / outWidth; 917 let outCol = ${S} % outWidth; 918 919 let WRow = ${x} / (i32(uniforms.w_shape[1]) * inChannels); 920 let WCol = ${x} / inChannels % i32(uniforms.w_shape[1]); 921 let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; 922 let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; 923 let xCh = ${x} % inChannels; 924 var resData = ${Ie(n,p)}(0.0); 925 // The bounds checking is always needed since we use it to pad zero for 926 // the 'same' padding type. 927 if (xRow >= 0 && xRow < ${_} && xCol >= 0 && xCol < ${w}) { 928 ${g} 929 let xIndex = getIndexFromCoords4D(coord, vec4<i32>(uniforms.x_shape)); 930 ${h(n)} 931 } 932 return resData;`,I=e?t&&i?` 933 let col = colIn * ${n}; 934 ${b}`:` 935 let col = colIn * ${n}; 936 if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { 937 ${b} 938 } 939 return ${Ie(n,p)}(0.0);`:i&&r?`
940 let col = colIn * ${n}; 941 ${b}`:` 942 let col = colIn * ${n}; 943 if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { 944 ${b} 945 } 946 return ${Ie(n,p)}(0.0);`,T=e?i&&r?f(u):` 947 let col = colIn * ${u}; 948 if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { 949 ${f(u)} 950 } 951 return ${Ie(u,p)}(0.0);`:` 952 let col = colIn * ${u}; 953 if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) { 954 ${f(u)} 955 } 956 return ${Ie(u,p)}(0.0);`,E=Ie(l,p),C=Ie(e?n:u,p),A=Ie(e?u:n,p),v=kt(s,E,p);return` 957 fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${C} { 958 ${e?I:T} 959 } 960 961 fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${A} { 962 ${e?T:I} 963 } 964 965 fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${E}) { 966 let col = colIn * ${l}; 967 if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) 968 { 969 var value = valueIn; 970 let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; 971 ${y} 972 ${Du(a)} 973 ${v} 974 setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); 975 } 976 }`},Gu=(e,t,r,i,a,s,n,u,l)=>{let p=t.format==="NHWC",h=p?e[0].dims[3]:e[0].dims[1],f=r[0],g=p?r[2]:r[3],y=p?r[1]:r[2],_=p?r[3]:r[1],w=p&&(h%4===0||h%3===0)&&_%4===0,S=p?_:g*y,x=p?g*y:_,b=[8,8,1],I=i<=8?[4,1,1]:[4,4,1],T=[Math.ceil(S/b[0]/I[0]),Math.ceil(x/b[1]/I[1]),Math.ceil(f/b[2]/I[2])];de("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${T}`);let E=w?p&&h%4!==0?3:4:1,C=b[1]*I[1],A=b[0]*I[0],v=Math.max(b[0]*E,b[1]),N=i%C===0,D=a%A===0,H=s%v===0,G=w?[E,4,4]:[1,1,1],Q=[{type:6,data:i},{type:6,data:a},{type:6,data:s},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];Tt(t,Q),Q.push(...Y(e[0].dims,e[1].dims));let R=["rank","rank"];n&&(Q.push(...Y(e[2].dims)),R.push("rank")),Q.push(...Y(r));let j=F=>{let X=[{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}];It(t,X);let le=w?4:1,W=ke(e[0].dataType),me=` 977 fn setOutputAtIndex(flatIndex : i32, value : ${w?`vec4<${W}>`:W}) { 978 result[flatIndex] = ${w?`vec4<${W}>`:W}(value); 979 } 980 fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${w?`vec4<${W}>`:W}) { 981 let flatIndex = getOutputIndexFromCoords(vec4<i32>(d0, d1, d2, d3)); 982 setOutputAtIndex(flatIndex ${w?"/ 4":""}, value); 983 }`,P=M("x",e[0].dataType,e[0].dims.length,E===3?1:E),U=M("w",e[1].dataType,e[1].dims.length,le),J=[P,U],ee=K("result",e[0].dataType,r.length,le);if(n){let $e=M("bias",e[2].dataType,e[2].dims.length,le);J.push($e),me+=` 984 fn getBiasByOutputCoords(coords : vec4<i32>) -> ${w?`vec4<${W}>`:W} { 985 return bias[coords.${p?"w":"y"}${w?"/ 4":""}]; 986 }`}return` 987 ${Pu("uniforms.result_strides")} 988 //struct Uniforms { xShape : vec4<i32>, wShape : vec4<i32>, outShape : vec4<i32>, 989 // outShapeStrides: vec3<i32>, filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>, 990 // dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; 991 ${F.registerUniforms(X).declareVariables(...J,ee)} 992 ${me} 993 ${Vu(p,N,D,H,n,t,G[0],G[1],G[2],W)} 994 ${w?Xi(I,b,W,void 0,!p,v):ea(I,b,W,void 0,!p,v,!1,void 0,u)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${E};${w};${N};${D};${H};${C};${A};${v}`,inputDependencies:R},getRunData:()=>({outputs:[{dims:l?l(r):r,dataType:e[0].dataType}],dispatchGroup:{x:T[0],y:T[1],z:T[2]},programUniforms:Q}),getShaderSource:j}}}),Hu,ra,tr,Fu,ia,ju,Ku,Qu,tm=q(()=>{te(),rt(),re(),ae(),Et(),Qi(),Hu=e=>{let t=1;for(let r=0;r<e.length;r++)t*=e[r];return t},ra=e=>typeof e=="number"?[e,e,e]:e,tr=(e,t)=>t<=1?e:e+(e-1)*(t-1),Fu=(e,t,r,i=1)=>{let a=tr(t,i);return Math.floor((e[0]*(r-1)-r+a)/2)},ia=(e,t,r,i,a)=>{a==null&&(a=Fu(e,t[0],i[0]));let s=[0,0,0,r];for(let n=0;n<3;n++)e[n]+2*a>=t[n]&&(s[n]=Math.trunc((e[n]-t[n]+2*a)/i[n]+1));return s},ju=(e,t,r,i,a,s,n,u,l,p)=>{let h,f,g,y;if(e==="VALID"&&(e=0),typeof e=="number"){h={top:e,bottom:e,left:e,right:e,front:e,back:e};let _=ia([t,r,i,1],[u,l,p],1,[a,s,n],e);f=_[0],g=_[1],y=_[2]}else if(Array.isArray(e)){if(!e.every((w,S,x)=>w===x[0]))throw Error(`Unsupported padding parameter: ${e}`);h={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let _=ia([t,r,i,1],[u,l,p],1,[a,s,n],e[0]);f=_[0],g=_[1],y=_[2]}else if(e==="SAME_UPPER"){f=Math.ceil(t/a),g=Math.ceil(r/s),y=Math.ceil(i/n);let _=(f-1)*a+u-t,w=(g-1)*s+l-r,S=(y-1)*n+p-i,x=Math.floor(_/2),b=_-x,I=Math.floor(w/2),T=w-I,E=Math.floor(S/2),C=S-E;h={top:I,bottom:T,left:E,right:C,front:x,back:b}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:h,outDepth:f,outHeight:g,outWidth:y}},Ku=(e,t,r,i,a,s=!1,n="channelsLast")=>{let u,l,p,h,f;if(n==="channelsLast")[u,l,p,h,f]=e;else if(n==="channelsFirst")[u,f,l,p,h]=e;else throw new Error(`Unknown dataFormat ${n}`);let[g,,y,_,w]=t,[S,x,b]=ra(r),[I,T,E]=ra(i),C=tr(y,I),A=tr(_,T),v=tr(w,E),{padInfo:N,outDepth:D,outHeight:H,outWidth:G}=ju(a,l,p,h,S,x,b,C,A,v),Q=s?g*f:g,R=[0,0,0,0,0];return n==="channelsFirst"?R=[u,Q,D,H,G]:n==="channelsLast"&&(R=[u,D,H,G,Q]),{batchSize:u,dataFormat:n,inDepth:l,inHeight:p,inWidth:h,inChannels:f,outDepth:D,outHeight:H,outWidth:G,outChannels:Q,padInfo:N,strideDepth:S,strideHeight:x,strideWidth:b,filterDepth:y,filterHeight:_,filterWidth:w,effectiveFilterDepth:C,effectiveFilterHeight:A,effectiveFilterWidth:v,dilationDepth:I,dilationHeight:T,dilationWidth:E,inShape:e,outShape:R,filterShape:t}},Qu=(e,t,r,i,a,s)=>{let n=s==="channelsLast";n?e[0].dims[3]:e[0].dims[1];let u=[64,1,1],l={x:r.map((S,x)=>x)},p=[Math.ceil(Hu(l.x.map(S=>r[S]))/u[0]),1,1];de("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${p}`);let h=1,f=O.size(r),g=[{type:12,data:f},{type:12,data:i},{type:12,data:a},{type:12,data:t.strides},{type:12,data:t.dilations}];Tt(t,g),g.push(...Y(e[0].dims,e[1].dims));let y=["rank","rank"],_=e.length===3;_&&(g.push(...Y(e[2].dims)),y.push("rank")),g.push(...Y(r));let w=S=>{let x=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:i.length},{name:"pads",type:"u32",length:a.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];It(t,x);let b=1,I=ke(e[0].dataType),T=M("x",e[0].dataType,e[0].dims.length,h),E=M("W",e[1].dataType,e[1].dims.length,b),C=[T,E],A=K("result",e[0].dataType,r.length,b),v="";if(_){let H=M("bias",e[2].dataType,e[2].dims.length,b);C.push(H),v+=` 995 fn getBiasByOutputCoords(coords : array<u32, 5>) -> ${I} { 996 return bias[${n?Z("coords",4,5):Z("coords",1,5)}]; 997 }`}let N=Ie(h,I),D=kt(t,N,I);return` 998 ${v} 999 fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { 1000 let aIndices = array<u32, 5>(d0, d1, d2, d3, d4); 1001 return ${T.getByIndices("aIndices")}; 1002 } 1003 fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { 1004 let aIndices = array<u32, 5>(d0, d1, d2, d3, d4); 1005 return ${E.getByIndices("aIndices")}; 1006 } 1007 ${S.registerUniforms(x).declareVariables(...C,A)} 1008 ${S.mainStart()} 1009 ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1010 let coords = ${A.offsetToIndices("global_idx")}; 1011 let batch = ${Z("coords",0,T.rank)}; 1012 let d2 = ${n?Z("coords",T.rank-1,T.rank):Z("coords",1,T.rank)}; 1013 let xFRCCorner = vec3<u32>(${n?Z("coords",1,T.rank):Z("coords",2,T.rank)}, 1014 ${n?Z("coords",2,T.rank):Z("coords",3,T.rank)}, 1015 ${n?Z("coords",3,T.rank):Z("coords",4,T.rank)}) * uniforms.strides - uniforms.pads; 1016 let xFCorner = xFRCCorner.x; 1017 let xRCorner = xFRCCorner.y; 1018 let xCCorner = xFRCCorner.z; 1019 let xShapeY = ${n?Z("uniforms.x_shape",1,T.rank):Z("uniforms.x_shape",2,T.rank)}; 1020 let xShapeZ = ${n?Z("uniforms.x_shape",2,T.rank):Z("uniforms.x_shape",3,T.rank)}; 1021 let xShapeW = ${n?Z("uniforms.x_shape",3,T.rank):Z("uniforms.x_shape",4,T.rank)}; 1022 let xShapeU = ${n?Z("uniforms.x_shape",4,T.rank):Z("uniforms.x_shape",1,T.rank)};
1023 let inputDepthNearestVec4 = (xShapeU / 4) * 4; 1024 let inputDepthVec4Remainder = xShapeU % 4; 1025 1026 var value = 0.0; 1027 for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) { 1028 let xF = xFCorner + wF * uniforms.dilations[0]; 1029 if (xF < 0 || xF >= xShapeY) { 1030 continue; 1031 } 1032 1033 for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) { 1034 let xR = xRCorner + wR * uniforms.dilations[1]; 1035 if (xR < 0 || xR >= xShapeZ) { 1036 continue; 1037 } 1038 1039 for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) { 1040 let xC = xCCorner + wC * uniforms.dilations[2]; 1041 if (xC < 0 || xC >= xShapeW) { 1042 continue; 1043 } 1044 1045 for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) { 1046 ${n?`let xValues = vec4<f32>( 1047 getX(batch, xF, xR, xC, d1), 1048 getX(batch, xF, xR, xC, d1 + 1), 1049 getX(batch, xF, xR, xC, d1 + 2), 1050 getX(batch, xF, xR, xC, d1 + 3)); 1051 `:`let xValues = vec4<f32>( 1052 getX(batch, d1, xF, xR, xC), 1053 getX(batch, d1 + 1, xF, xR, xC), 1054 getX(batch, d1 + 2, xF, xR, xC), 1055 getX(batch, d1 + 3, xF, xR, xC)); 1056 `} 1057 let wValues = vec4<f32>( 1058 getW(d2, d1, wF, wR, wC), 1059 getW(d2, d1 + 1, wF, wR, wC), 1060 getW(d2, d1 + 2, wF, wR, wC), 1061 getW(d2, d1 + 3, wF, wR, wC)); 1062 value += dot(xValues, wValues); 1063 } 1064 if (inputDepthVec4Remainder == 1) { 1065 ${n?`value += getX(batch, xF, xR, xC, inputDepthNearestVec4) 1066 * getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC) 1067 * getW(d2, inputDepthNearestVec4, wF, wR, wC);`} 1068 } else if (inputDepthVec4Remainder == 2) { 1069 ${n?`let xValues = vec2<f32>( 1070 getX(batch, xF, xR, xC, inputDepthNearestVec4), 1071 getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1)); 1072 `:`let xValues = vec2<f32>( 1073 getX(batch, inputDepthNearestVec4, xF, xR, xC), 1074 getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC)); 1075 `} 1076 let wValues = vec2<f32>( 1077 getW(d2, inputDepthNearestVec4, wF, wR, wC), 1078 getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC)); 1079 value += dot(xValues, wValues); 1080 } else if (inputDepthVec4Remainder == 3) { 1081 ${n?`let xValues = vec3<f32>( 1082 getX(batch, xF, xR, xC, inputDepthNearestVec4), 1083 getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1), 1084 getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2)); 1085 `:`let xValues = vec3<f32>( 1086 getX(batch, inputDepthNearestVec4, xF, xR, xC), 1087 getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC), 1088 getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC)); 1089 `} 1090 let wValues = vec3<f32>( 1091 getW(d2, inputDepthNearestVec4, wF, wR, wC), 1092 getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC), 1093 getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC)); 1094 value += dot(xValues, wValues); 1095 } 1096 } 1097 } 1098 } 1099 ${_?"value = value + getBiasByOutputCoords(coords)":""}; 1100 ${D} 1101 result[global_idx] = f32(value); 1102 }`};
vendor: 4,926 bytes, lines 1102-1171
1102return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${n};${h};${_}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:p[0],y:p[1],z:p[2]},programUniforms:g}),getShaderSource:w}}}),Zu,Yu,rm=q(()=>{te(),re(),ae(),Et(),Zu=(e,t,r,i)=>{let a=e.length>2,s=a?"value += b[output_channel];":"",n=e[0].dims,u=e[1].dims,l=t.format==="NHWC",p=l?r[3]:r[1],h=p/t.group,f=l&&h>=4?ve(p):1,g=O.size(r)/f,y=[{type:12,data:g},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:h}];Tt(t,y),y.push(...Y(n,[u[0],u[1],u[2],u[3]/f]));let _=a?["rank","rank","rank"]:["rank","rank"];y.push(...Y([r[0],r[1],r[2],r[3]/f]));let w=S=>{let x=K("output",e[0].dataType,r.length,f),b=ke(x.type.tensor),I=kt(t,x.type.value,b),T=M("x",e[0].dataType,n.length),E=M("w",e[1].dataType,u.length,f),C=[T,E];a&&C.push(M("b",e[2].dataType,e[2].dims,f));let A=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];It(t,A);let v=l?` 1103 for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { 1104 let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; 1105 1106 if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) { 1107 continue; 1108 } 1109 1110 for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) { 1111 let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; 1112 if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) { 1113 continue; 1114 } 1115 1116 for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) { 1117 let input_channel = in_channel_offset + wInChannel; 1118 let xVal = ${T.get("batch","xHeight","xWidth","input_channel")}; 1119 let wVal = ${E.get("wHeight","wWidth","wInChannel","output_channel")}; 1120 value += xVal * wVal; 1121 } 1122 } 1123 } 1124 `:` 1125 for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) { 1126 let input_channel = in_channel_offset + wInChannel; 1127 for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) { 1128 let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; 1129 1130 if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) { 1131 continue; 1132 } 1133 1134 for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) { 1135 let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; 1136 if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) { 1137 continue; 1138 } 1139 1140 let xVal = ${T.get("batch","input_channel","xHeight","xWidth")}; 1141 let wVal = ${E.get("output_channel","wInChannel","wHeight","wWidth")}; 1142 value += xVal * wVal; 1143 } 1144 } 1145 } 1146 `;return` 1147 ${S.registerUniforms(A).declareVariables(...C,x)} 1148 1149 ${S.mainStart()} 1150 ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1151 1152 let outputIndices = ${x.offsetToIndices("global_idx")}; 1153 let batch: u32 = outputIndices[0]; 1154 let output_channel: u32 = outputIndices[${l?3:1}]; 1155 let xRCCorner: vec2<u32> = vec2<u32>(outputIndices[${l?1:2}], outputIndices[${l?2:3}]) * uniforms.strides - uniforms.pads; 1156 let group_id: u32 = output_channel * ${f} / uniforms.output_channels_per_group; 1157 var in_channel_offset = group_id * uniforms.w_shape[${l?2:1}]; 1158 1159 var value: ${x.type.value} = ${x.type.value}(0); 1160 ${v} 1161 ${s} 1162 ${I} 1163 ${x.setByOffset("global_idx","value")} 1164 }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${f}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:y}),getShaderSource:w}},Yu=(e,t,r,i)=>{let a=e.length>2,s=ve(r[3]),n=ve(r[2]),u=O.size(r)/s/n,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/s],p=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/s],h=[r[0],r[1],r[2],r[3]/s],f=[{type:12,data:u},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];Tt(t,f),f.push(...Y(l,p,h));let g=(n-1)*t.strides[1]+p[1],y=_=>{let w=K("output",e[0].dataType,h.length,s),S=ke(w.type.tensor),x=kt(t,w.type.value,S),b=M("x",e[0].dataType,l.length,s),I=M("w",e[1].dataType,p.length,s),T=[b,I];a&&T.push(M("b",e[2].dataType,e[2].dims,s));let E=a?"value += b[output_channel];":"",C=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return It(t,C),` 1165 ${_.registerUniforms(C).declareVariables(...T,w)} 1166 ${_.mainStart()} 1167 ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1168 let width0 = uniforms.output_shape[3]; 1169 let output_channel = global_idx % width0; 1170 var index1 = global_idx / width0; 1171 let width1 = uniforms.output_shape[2] / ${n}u;
1172 let col = (index1 % width1) * ${n}u; 1173 index1 = index1 / width1; 1174 let row = index1 % uniforms.output_shape[1]; 1175 let batch = index1 / uniforms.output_shape[1]; 1176 1177 let x_corner = vec2<i32>(i32(row), i32(col)) * uniforms.strides - uniforms.pads; 1178 1179 var x_vals: array<${b.type.value}, ${g}>; 1180 var values: array<${w.type.value}, ${n}>; 1181 let input_channel = output_channel; 1182 // Use constant instead of uniform can give better performance for w's height/width. 1183 for (var w_height: u32 = 0u; w_height < ${p[0]}; w_height++) { 1184 let x_height = x_corner.x + i32(w_height); 1185 if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) { 1186 for (var i = 0; i < ${g}; i++) { 1187 let x_width = x_corner.y + i; 1188 if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { 1189 x_vals[i] = ${b.get("batch","u32(x_height)","u32(x_width)","input_channel")}; 1190 } else { 1191 x_vals[i] = ${b.type.value}(0); 1192 } 1193 } 1194 for (var w_width: u32 = 0u; w_width < ${p[1]}; w_width++) { 1195 let w_val = ${I.get("w_height","w_width","0","output_channel")}; 1196 for (var i = 0u; i < ${n}u; i++) { 1197 values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); 1198 } 1199 } 1200 } 1201 } 1202 1203 for (var i = 0u; i < ${n}u; i++) { 1204 var value = values[i]; 1205 ${E} 1206 ${x} 1207 ${w.set("batch","row","col + i","output_channel","value")}; 1208 } 1209 }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${s};${n};${g};${p[0]};${p[1]}`,inputDependencies:a?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:f}),getShaderSource:y}}}),Xu,Ar,Ju,Or,aa,na,el,tl,sa,im=q(()=>{re(),em(),tm(),ta(),rm(),Et(),Yi(),ut(),Xu=(e,t,r,i,a,s)=>{let n=e[0],u=e.slice(s?1:2,s?3:4),l=u.length,p=t[0],h=t.slice(2).map((g,y)=>g+(g-1)*(r[y]-1)),f=u.map((g,y)=>g+i[y]+i[y+l]).map((g,y)=>Math.floor((g-h[y]+a[y])/a[y]));return f.splice(0,0,n),f.splice(s?3:1,0,p),f},Ar=[2,3,1,0],Ju=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[1]*t.group;if(r!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let a=e[0].dims.length-2;if(t.dilations.length!==a)throw new Error(`dilations should be ${a}D`);if(t.strides.length!==a)throw new Error(`strides should be ${a}D`);if(t.pads.length!==a*2)throw new Error(`pads should be ${a*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},Or=(e,t)=>{let r=e.kernelShape.slice();r.length<t[1].dims.length-2&&r.push(...Array(t[1].dims.length-2-r.length).fill(0));for(let s=2;s<t[1].dims.length;++s)r[s-2]===0&&(r[s-2]=t[1].dims[s]);let i=e.pads.slice();xr.adjustPadsBasedOnAutoPad(t[0].dims,e.strides,e.dilations,r,i,e.format==="NHWC",e.autoPad);let a=Object.assign({},e);return Object.assign(a,{kernelShape:r,pads:i}),a},aa=e=>{let t=Ki(e),r=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],a=e.dilations,s=e.group,n=e.kernel_shape,u=e.pads,l=e.strides,p=e.w_is_const();return{autoPad:i,format:r,dilations:a,group:s,kernelShape:n,pads:u,strides:l,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},na=(e,t,r,i)=>{let a=r.format==="NHWC",s=Xu(t[0].dims,t[1].dims,r.dilations,r.pads,r.strides,a);if(r.group!==1){let C=[t[0]];if(a){let A=e.kernelCustomData.wT??e.compute(Me(t[1],Ar),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=A),C.push(A)}else C.push(t[1]);t.length===3&&C.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&a&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1?e.compute(Yu(C,r,s,i),{inputs:C}):e.compute(Zu(C,r,s,i),{inputs:C});return}let n=t.length===3,u=t[0].dims[a?1:2],l=t[0].dims[a?2:3],p=t[0].dims[a?3:1],h=t[1].dims[2],f=t[1].dims[3],g=s[a?1:2],y=s[a?2:3],_=s[a?3:1],w=a&&h===u&&f===l&&r.pads[0]===0&&r.pads[1]===0;if(w||h===1&&f===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let C=s[0],A,v,N,D=[];if(a){let Q=e.kernelCustomData.wT??e.compute(Me(t[1],Ar),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=Q),w){let R=u*l*p;A=t[0].reshape([1,C,R]),v=Q.reshape([1,R,_]),N=[1,C,_]}else A=t[0].reshape([C,u*l,p]),v=Q.reshape([1,p,_]),N=[C,g*y,_];D.push(A),D.push(v)}else A=t[0].reshape([C,p,u*l]),v=t[1].reshape([1,_,p]),N=[C,_,g*y],D.push(v),D.push(A);n&&D.push(t[2]);let H=N[2],G=D[0].dims[D[0].dims.length-1];H<8&&G<8?e.compute(Zi(D,r,s,N,a,i),{inputs:D}):e.compute(Cr(D,r,s,N,a,i),{inputs:D});return}let S=!0,x=e.kernelCustomData.wT??e.compute(Me(t[1],Ar),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=x);let b=[t[0],x];n&&b.push(t[2]);let I=a?g*y:_,T=a?_:g*y,E=h*f*p;e.compute(Gu(b,r,s,I,T,E,n,S,i),{inputs:b})},el=(e,t)=>
vendor: 23,066 bytes, lines 1209-1360
1209{let r=t.format==="NHWC",i=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let a=[0,t.pads[0],0,t.pads[1]],s=[1].concat(t.strides),n=[1].concat(t.dilations),u=[1].concat(t.kernelShape),l=Or({...t,pads:a,strides:s,dilations:n,kernelShape:u},i);na(e,i,l,p=>r?[p[0],p[2],p[3]]:[p[0],p[1],p[3]])},tl=(e,t,r)=>{let i=r.format==="NHWC"?"channelsLast":"channelsFirst",a=Or(r,t),s=r.autoPad==="NOTSET"?r.pads:r.autoPad,n=Ku(t[0].dims,t[1].dims,r.strides,r.dilations,s,!1,i);e.compute(Qu(t,a,n.outShape,[n.filterDepth,n.filterHeight,n.filterWidth],[n.padInfo.front,n.padInfo.top,n.padInfo.left],i))},sa=(e,t)=>{if(Ju(e.inputs,t),e.inputs[0].dims.length===3)el(e,t);else if(e.inputs[0].dims.length===5)tl(e,e.inputs,t);else{let r=Or(t,e.inputs);na(e,e.inputs,r)}}}),rl,am=q(()=>{te(),rt(),re(),ae(),rl=(e,t,r)=>{let i=e.length>2,a=t.outputShape,s=t.format==="NHWC",n=t.group,u=e[1].dims,l=u[2]/n,p=u[3],h=s?ve(l):1,f=s&&p===1&&l>=4,g=f?Math.floor(l/4)*4:Math.floor(l/h)*h,y=l-g,_=s?ve(p):1,w=s?p===1?h:_:1,S=O.size(a)/_,x=[Math.ceil(S/64),1,1];de("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${x}`);let b=["rank","rank"],I=[t.strides[0],t.strides[1]],T=[t.kernelShape[s?1:2],t.kernelShape[s?2:3]],E=[t.dilations[0],t.dilations[1]],C=[T[0]+(t.dilations[0]<=1?0:(t.kernelShape[s?1:2]-1)*(t.dilations[0]-1)),T[1]+(t.dilations[1]<=1?0:(t.kernelShape[s?2:3]-1)*(t.dilations[1]-1))],A=[C[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),C[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],v=[{type:12,data:S},{type:12,data:I},{type:12,data:T},{type:12,data:E},{type:12,data:C},{type:6,data:A},{type:12,data:g},{type:12,data:l},{type:12,data:p},...Y(e[0].dims,e[1].dims)];i&&(v.push(...Y(e[2].dims)),b.push("rank")),v.push(...Y(a));let N=D=>{let H=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:I.length},{name:"filter_dims",type:"u32",length:T.length},{name:"dilations",type:"u32",length:T.length},{name:"effective_filter_dims",type:"u32",length:C.length},{name:"pads",type:"i32",length:A.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],G=ke(e[0].dataType),Q=s?1:2,R=s?2:3,j=s?3:1,F=M("W",e[1].dataType,e[1].dims.length,w),X=M("Dy",e[0].dataType,e[0].dims.length,h),le=[X,F];i&&le.push(M("bias",e[2].dataType,[a[j]].length,_));let W=K("result",e[0].dataType,a.length,_),me=()=>{let J="";if(f)h===4?J+=` 1210 let xValue = ${X.getByOffset("x_offset")}; 1211 let wValue = ${F.getByOffset("w_offset")}; 1212 dotProd = dotProd + dot(xValue, wValue); 1213 x_offset += 1u; 1214 w_offset += 1u;`:h===2?J+=` 1215 dotProd = dotProd + dot(vec4<${G}>(${X.getByOffset("x_offset")}, ${X.getByOffset("x_offset + 1u")}), vec4<${G}>(${F.getByOffset("w_offset")}, ${F.getByOffset("w_offset + 1u")})); 1216 x_offset += 2u; 1217 w_offset += 2u;`:h===1&&(J+=` 1218 dotProd = dotProd + dot(vec4<${G}>(${X.getByOffset("x_offset")}, ${X.getByOffset("x_offset + 1u")}, ${X.getByOffset("x_offset + 2u")}, ${X.getByOffset("x_offset + 3u")}), vec4<${G}>(${F.getByOffset("w_offset")}, ${F.getByOffset("w_offset + 1u")}, ${F.getByOffset("w_offset + 2u")}, ${F.getByOffset("w_offset + 3u")})); 1219 x_offset += 4u; 1220 w_offset += 4u;`);else if(J+=` 1221 let xValue = ${s?X.getByOffset(`${X.indicesToOffset(`${X.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${h}`):X.get("batch","inputChannel","idyR","idyC")}; 1222 `,h===1)J+=` 1223 let w_offset = ${F.indicesToOffset(`${F.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)}; 1224 let wValue = ${F.getByOffset(`w_offset / ${w}`)}; 1225 dotProd = dotProd + xValue * wValue;`;else for(let ee=0;ee<h;ee++)J+=` 1226 let wValue${ee} = ${F.getByOffset(`${F.indicesToOffset(`${F.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel + ${ee}, wOutChannel)`)} / ${w}`)}; 1227 dotProd = dotProd + xValue[${ee}] * wValue${ee};`;return J},P=()=>{if(y===0)return"";if(!f)throw new Error(`packInputAs4 ${f} is not true.`);let J="";if(h===1){J+="dotProd = dotProd";for(let ee=0;ee<y;ee++)J+=` 1228 + ${X.getByOffset(`x_offset + ${ee}`)} * ${F.getByOffset(`w_offset + ${ee}`)}`;J+=";"}else if(h===2){if(y!==2)throw new Error(`Invalid inputChannelsRemainder ${y}.`);J+=` 1229 let xValue = ${X.getByOffset("x_offset")}; 1230 let wValue = ${F.getByOffset("w_offset")}; 1231 dotProd = dotProd + dot(xValue, wValue);`}return J},U=` 1232 let outputIndices = ${W.offsetToIndices(`global_idx * ${_}`)}; 1233 let batch = ${W.indicesGet("outputIndices",0)}; 1234 let d1 = ${W.indicesGet("outputIndices",j)}; 1235 let r = ${W.indicesGet("outputIndices",Q)}; 1236 let c = ${W.indicesGet("outputIndices",R)}; 1237 let dyCorner = vec2<i32>(i32(r), i32(c)) - uniforms.pads; 1238 let dyRCorner = dyCorner.x; 1239 let dyCCorner = dyCorner.y; 1240 let groupId = d1 / uniforms.output_channels_per_group; 1241 let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; 1242 // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). 1243 // ? = to be determined. : = across all values in that axis. 1244 var dotProd = ${W.type.value}(0.0); 1245 var wR: u32 = 0; 1246 if (uniforms.dilations.x == 1) { 1247 // Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0 1248 wR = u32(((dyRCorner + i32(uniforms.strides.x) - 1) / i32(uniforms.strides.x)) * i32(uniforms.strides.x) - dyRCorner); 1249 } 1250 for (; wR < uniforms.effective_filter_dims.x; wR = wR + 1) { 1251 if (wR % uniforms.dilations.x != 0) { 1252 continue; 1253 } 1254 let dyR = (${G}(dyRCorner) + ${G}(wR)) / ${G}(uniforms.strides[0]); 1255 let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; 1256 if (dyR < 0.0 || dyR >= ${G}(uniforms.Dy_shape[${Q}]) || fract(dyR) > 0.0 || 1257 wRPerm < 0) { 1258 continue; 1259 } 1260 let idyR: u32 = u32(dyR); 1261 var wC: u32 = 0; 1262 if (uniforms.dilations.y == 1) { 1263 // Minimum wC >= 0 that satisfies (dyCCorner + wC) % (uniforms.strides.y) == 0 1264 wC = u32(((dyCCorner + i32(uniforms.strides.y) - 1) / i32(uniforms.strides.y)) * i32(uniforms.strides.y) - dyCCorner); 1265 } 1266 for (; wC < uniforms.effective_filter_dims.y; wC = wC + 1) { 1267 if (wC % uniforms.dilations.y != 0) { 1268 continue; 1269 } 1270 let dyC = (${G}(dyCCorner) + ${G}(wC)) / ${G}(uniforms.strides.y); 1271 let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; 1272 if (dyC < 0.0 || dyC >= ${G}(uniforms.Dy_shape[${R}]) || 1273 fract(dyC) > 0.0 || wCPerm < 0) { 1274 continue; 1275 } 1276 let idyC: u32 = u32(dyC); 1277 var inputChannel = groupId * uniforms.input_channels_per_group; 1278 ${f?` 1279 var x_offset = ${X.indicesToOffset(`${X.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${h}; 1280 var w_offset = ${F.indicesToOffset(`${F.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${w}; 1281 `:""} 1282 for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${f?4:h}) { 1283 ${me()} 1284 inputChannel = inputChannel + ${f?4:h}; 1285 } 1286 ${P()} 1287 wC = wC + uniforms.strides.y - 1; 1288 } 1289 wR = wR + uniforms.strides[0] - 1; 1290 } 1291 let value = dotProd${i?` + bias[d1 / ${_}]`:""}; 1292 ${W.setByOffset("global_idx","value")}; 1293 `;return` 1294 ${D.registerUniforms(H).declareVariables(...le,W)} 1295 ${D.mainStart()} 1296 ${D.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; 1297 ${U}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${h}${w}${_}${f}${y}`,inputDependencies:b},getRunData:()=>({dispatchGroup:{x:x[0],y:x[1],z:x[2]},outputs:[{dims:r?r(a):a,dataType:e[0].dataType}],programUniforms:v}),getShaderSource:N}}}),il,al,nl,oa,sl,ol,ua,ul,ll,nm=q(()=>{am(),Et(),ut(),il=(e,t,r,i,a,s)=>(e-1)*t+r+(i-1)*a+1-s,al=(e,t,r,i,a)=>{let s=Math.floor(e/2);t==="SAME_UPPER"?(r[i]=s,r[a]=e-s):t==="SAME_LOWER"&&(r[i]=e-s,r[a]=s)},nl=(e,t,r,i,a,s,n,u,l,p)=>{let h=e.length-2,f=p.length===0;l.length<h&&l.push(...Array(h-l.length).fill(0));let g=e[0],y=t[u?3:1]*a;for(let _=0,w=e.length-h-(u?1:0);_<h;++_,++w){let S=e[w],x=f?S*n[_]:p[_],b=il(S,n[_],s[_],t[w],r[_],x);al(b,i,s,_,_+h),f&&p.push(n[_]*(S-1)+l[_]+(t[w]-1)*r[_]+1-s[_]-s[_+h])}p.splice(0,0,g),p.splice(u?3:1,0,y)},oa=(e,t)=>{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((f,g)=>f*g,1)===0){r.length=0;for(let f=2;f<t[1].dims.length;++f)r.push(t[1].dims[f])}let i=e.format==="NHWC";r.splice(0,0,t[1].dims[0]),r.splice(i?3:1,0,t[1].dims[1]);let a=e.pads.slice(),s=e.outputShape.slice(),n=e.outputPadding.slice(),u=t[0].dims,l=e.dilations.slice();if(l.reduce((f,g)=>f+g,0)===0){let f=t[0].dims.length-2;l=new Array(f).fill(1)}let p=e.strides.slice();if(p.reduce((f,g)=>f+g,0)===0){let f=t[0].dims.length-2;p=new Array(f).fill(1)}nl(u,r,l,e.autoPad,e.group,a,p,i,n,s);let h=Object.assign({},e);return Object.assign(h,{kernelShape:r,pads:a,outputPadding:n,outputShape:s,dilations:l,strides:p}),h},sl=e=>{let t=Ki(e),r=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],a=e.dilations,s=e.group??1,n=e.kernelShape,u=e.pads,l=e.strides,p=e.wIsConst(),h=e.outputPadding,f=e.outputShape;return{autoPad:i,format:r,dilations:a,group:s,kernelShape:n,outputPadding:h,outputShape:f,pads:u,strides:l,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},ol=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[0];if(r!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let a=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==a))throw new Error("invalid bias");let s=e[0].dims.length-2;if(t.dilations.reduce((n,u)=>n+u,0)>0&&t.dilations.length!==s)throw new Error(`dilations should be ${s}D`);if(t.strides.reduce((n,u)=>n+u,0)>0&&t.strides.length!==s)throw new Error(`strides should be ${s}D`);if(t.pads.reduce((n,u)=>n+u,0)>0&&t.pads.length!==s*2)throw new Error(`pads should be ${s*2}D`);if(t.outputPadding.length!==s&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${s}D`);if(t.kernelShape.reduce((n,u)=>n+u,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},ua=(e,t,r,i)=>{let a=e.kernelCustomData.wT??e.compute(Me(t[1],[2,3,0,1]),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=a);let s=[t[0],a];t.length===3&&s.push(t[2]),e.compute(rl(s,r,i),{inputs:s})},ul=(e,t)=>{let r=t.format==="NHWC",i=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let a=t.kernelShape;(a.length===0||a[0]===0)&&(a=[e.inputs[1].dims[2]]);let s=t.dilations;(s.length===0||s[0]===0)&&(s=[1]);let n=t.strides;(n.length===0||n[0]===0)&&(n=[1]);let u=t.pads;u.length===0&&(u=[0,0]),u=[0,u[0],0,u[1]],n=[1].concat(n),s=[1].concat(s),a=[1].concat(a);let l=t.outputPadding;l=[0].concat(l);let p=oa({...t,pads:u,strides:n,dilations:s,kernelShape:a,outputPadding:l},i);ua(e,i,p,h=>r?[h[0],h[2],h[3]]:[h[0],h[1],h[3]])},ll=(e,t)=>{if(ol(e.inputs,t),e.inputs[0].dims.length===3)ul(e,t);else{let r=oa(t,e.inputs);ua(e,e.inputs,r)}}}),dl,pl,cl,sm=q(()=>{te(),re(),xe(),ae(),dl=(e,t,r,i)=>{let a=O.size(t),s=t.length,n=M("input",e,s),u=K("output",e,s),l=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),p=O.normalizeAxis(l,s),h=f=>{let g=` i32(${n.indicesGet("inputIndices","uniforms.axis")}) `,y=Z("uniforms.input_shape","uniforms.axis",s),_=i.reverse?g+(i.exclusive?" + 1":""):"0",w=i.reverse?y:g+(i.exclusive?"":" + 1");return` 1298 ${f.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(n,u)} 1299 ${f.mainStart()} 1300 ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 1301 var inputIndices = ${u.offsetToIndices("global_idx")}; 1302 var sum = ${u.type.value}(0); 1303 let first : i32 = ${_}; 1304 let last : i32 = ${w}; 1305 for (var i : i32 = first; i < last; i++) { 1306 ${n.indicesSet("inputIndices","uniforms.axis","u32(i)")}; 1307 sum = sum + ${n.getByIndices("inputIndices")}; 1308 } 1309 ${u.setByOffset("global_idx","sum")}; 1310 }`};return{name:"CumSum",shaderCache:{hint:i.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:[{type:12,data:a},{type:12,data:p},...Y(t,t)]}),getShaderSource:h}},pl=(e,t)=>{let r=e.inputs[0].dims,i=e.inputs[0].dataType,a=e.inputs[1];e.compute(dl(i,r,a,t),{inputs:[0]})},cl=e=>{let t=e.exclusive===1,r=e.reverse===1;return he({exclusive:t,reverse:r})}}),hl,fl,ml,gl,yl,om=q(()=>{te(),re(),xe(),ae(),hl=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},fl=(e,t,r,i)=>{let a=[];a.push(`fn perm(i: ${i.type.indices}) -> ${r.type.indices} { 1311 var a: ${r.type.indices};`);for(let s=0;s<t;++s)a.push(r.indicesSet("a",e[s],`i[${s}]`));return a.push("return a;}"),a.join(` 1312`)},ml=(e,t)=>{let r,i,a,s,n,u,l=t.format==="NHWC",p=t.blocksize,h=t.mode==="DCR";l?([r,i,a,s]=e.dims,n=h?[r,i,a,p,p,s/p**2]:[r,i,a,s/p**2,p,p],u=h?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,i,a,s]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],n=h?[r,p,p,s/p**2,i,a]:[r,s/p**2,p,p,i,a],u=h?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let f=e.reshape(n),g=f.dims.length,y=e.dataType,_=M("a",y,g),w=K("output",y,g),S=x=>` 1313 ${x.registerUniform("output_size","u32").declareVariables(_,w)} 1314 1315 ${fl(u,g,_,w)} 1316 1317 ${x.mainStart()} 1318 ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1319 1320 let indices = ${w.offsetToIndices("global_idx")}; 1321 let aIndices = perm(indices); 1322 1323 ${w.setByOffset("global_idx",_.getByIndices("aIndices"))} 1324 }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:x=>{let b=l?[r,i*p,a*p,s/p**2]:[r,s/p**2,i*p,a*p],I=O.size(b),T=f.dims,E=O.sortBasedOnPerm(T,u);return{outputs:[{dims:b,dataType:x[0].dataType}],dispatchGroup:{x:Math.ceil(I/64)},programUniforms:[{type:12,data:I},...Y(T,E)]}},getShaderSource:S}},gl=(e,t)=>{hl(e.inputs),e.compute(ml(e.inputs[0],t))},yl=e=>he({blocksize:e.blocksize,mode:e.mode,format:e.format})}),Rr,rr,la,_l,wl,bl,$l,da,vl,xl,Sl,um=q(()=>{te(),re(),xe(),ae(),Rr="[a-zA-Z]|\\.\\.\\.",rr="("+Rr+")+",la="^"+rr+"$",_l="("+rr+",)*"+rr,wl="^"+_l+"$",bl=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let r=this.symbolToIndices.get(e);r===void 0?r=[t]:r.push(t),this.symbolToIndices.set(e,r)}},$l=class{constructor(e,t){var a;this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[r,i]=t.includes("->")?t.split("->",2):[t,""];if(!r.match(RegExp(wl)))throw new Error("Invalid LHS term");if(r.split(",").forEach((s,n)=>{let u=e[n].dims.slice();if(!s.match(RegExp(la)))throw new Error("Invalid LHS term");let l=this.processTerm(s,!0,u,n);this.lhs.push(l)}),i==="")i+=[...this.symbolToInfo.entries()].filter(([s,n])=>n.count===1||s==="...").map(([s])=>s).join("");else if(!i.match(RegExp(rr)))throw new Error("Invalid RHS");(a=i.match(RegExp(Rr,"g")))==null||a.forEach(s=>{if(s==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let n=this.symbolToInfo.get(s);if(n===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(n.dimValue)}}),this.rhs=this.processTerm(i,!1,this.outputDims)}addSymbol(e,t,r){let i=this.symbolToInfo.get(e);if(i!==void 0){if(i.dimValue!==t&&i.count!==1)throw new Error("Dimension mismatch");i.count++,i.inputIndices.push(r)}else i={count:1,dimValue:t,inputIndices:[r]};this.symbolToInfo.set(e,i)}processTerm(e,t,r,i=-1){let a=r.length,s=!1,n=[],u=0;if(!e.match(RegExp(la))&&!t&&e!=="")throw new Error("Invalid LHS term");let l=e.match(RegExp(Rr,"g")),p=new bl(i);return l==null||l.forEach((h,f)=>{if(h==="..."){if(s)throw new Error("Only one ellipsis is allowed per input term");s=!0;let g=a-l.length+1;if(g<0)throw new Error("Ellipsis out of bounds");if(n=r.slice(u,u+g),this.hasEllipsis){if(this.ellipsisDims.length!==n.length||this.ellipsisDims.toString()!==n.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=n;else throw new Error("Ellipsis must be specified in the LHS");for(let y=0;y<n.length;y++){let _=String.fromCharCode(48+y);p.addSymbol(_,f+y),this.addSymbol(_,r[u++],i)}}else p.addSymbol(h,f+(this.hasEllipsis?this.ellipsisDims.length-1:0)),this.addSymbol(h,r[u++],i)}),p}},da=e=>e+"_max",vl=(e,t,r,i)=>{let a=e.map(p=>p.length).map((p,h)=>M(`input${h}`,t,p)),s=O.size(i),n=K("output",t,i.length),u=[...r.symbolToInfo.keys()].filter(p=>!r.rhs.symbolToIndices.has(p)),l=p=>{let h=[],f="var prod = 1.0;",g="var sum = 0.0;",y="sum += prod;",_=[],w=[],S=[],x=[],b=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((T,E)=>{var C;if(r.rhs.symbolToIndices.has(E)){let A=(C=r.rhs.symbolToIndices.get(E))==null?void 0:C[0];A!==void 0&&r.lhs.forEach((v,N)=>{if(T.inputIndices.includes(N)){let D=v.symbolToIndices.get(E);if(D===void 0)throw new Error("Invalid symbol error");D.forEach(H=>{h.push(`${a[N].indicesSet(`input${N}Indices`,H,n.indicesGet("outputIndices",A))}`)})}})}else r.lhs.forEach((A,v)=>{if(T.inputIndices.includes(v)){let N=A.symbolToIndices.get(E);if(N===void 0)throw new Error("Invalid symbol error");N.forEach(D=>{_.push(`${a[v].indicesSet(`input${v}Indices`,D,`${E}`)}`)}),x.push(`prod *= ${a[v].getByIndices(`input${v}Indices`)};`)}}),w.push(`for(var ${E}: u32 = 0; ${E} < uniforms.${da(E)}; ${E}++) {`),S.push("}")});let I=b?[...h,`let sum = ${a.map((T,E)=>T.getByIndices(`input${E}Indices`)).join(" * ")};`]:[...h,g,...w,..._,f,...x,y,...S];return` 1325 ${p.registerUniforms(u.map(T=>({name:`${da(T)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...a,n)} 1326 1327 ${p.mainStart()} 1328 ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 1329 var outputIndices = ${n.offsetToIndices("global_idx")}; 1330 ${a.map((T,E)=>`var input${E}Indices: ${a[E].type.indices};`).join(` 1331`)} 1332 ${I.join(` 1333`)}; 1334 ${n.setByOffset("global_idx","sum")}; 1335 }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let p=u.filter(f=>r.symbolToInfo.has(f)).map(f=>{var g;return{type:12,data:((g=r.symbolToInfo.get(f))==null?void 0:g.dimValue)||0}});p.push({type:12,data:s});let h=e.map((f,g)=>[...Y(f)]).reduce((f,g)=>f.concat(g),p);return h.push(...Y(i)),{outputs:[{dims:i,dataType:t}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:h}},getShaderSource:l}},xl=(e,t)=>{let r=new $l(e.inputs,t.equation),i=r.outputDims,a=e.inputs.map((s,n)=>s.dims);e.compute(vl(a,e.inputs[0].dataType,r,i))},Sl=e=>{let t=e.equation.replace(/\s+/g,"");return he({equation:t})}}),kl,pa,Tl,Il,El,lm=q(()=>{te(),re(),ae(),kl=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),i=r.length<t.length?0:r.length-t.length,a=t.length<r.length?0:t.length-r.length;for(;i<r.length&&a<t.length;++i,++a)if(r[i]!==t[a]&&r[i]!==1&&t[a]!==1)throw new Error("Expand requires shape to be broadcastable to input")},pa=(e,t)=>{let r=e.length-t.length,i=[];for(let a=0;a<r;++a)i.push(e[a]);for(let a=0;a<t.length;++a)i.push(t[a]===1?e[a+r]:t[a]);return i},Tl=(e,t)=>e.length>t.length?pa(e,t):pa(t,e),Il=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),i=Tl(t,r),a=e[0].dataType,s=a===9||O.size(t)===1,n=a===9||t.length>0&&t[t.length-1]%4===0?4:1,u=s||i.length>0&&i[i.length-1]%4===0?4:1,l=Math.ceil(O.size(i)/u),p=f=>{let g=M("input",a,t.length,n),y=K("output",a,i.length,u),_;if(a===9){let w=(S,x,b="")=>` 1336 let outputIndices${x} = ${y.offsetToIndices(`outputOffset + ${x}u`)}; 1337 let offset${x} = ${g.broadcastedIndicesToOffset(`outputIndices${x}`,y)}; 1338 let index${x} = offset${x} / 4u; 1339 let component${x} = offset${x} % 4u; 1340 ${S}[${x}] = ${b}(${g.getByOffset(`index${x}`)}[component${x}]); 1341 `;_=` 1342 let outputOffset = global_idx * ${u}; 1343 var data = vec4<u32>(0); 1344 ${w("data",0,"u32")} 1345 ${w("data",1,"u32")} 1346 ${w("data",2,"u32")} 1347 ${w("data",3,"u32")} 1348 ${y.setByOffset("global_idx","data")} 1349 }`}else _=` 1350 let outputIndices = ${y.offsetToIndices(`global_idx * ${u}`)}; 1351 let inputOffset = ${g.broadcastedIndicesToOffset("outputIndices",y)}; 1352 let data = ${y.type.value}(${g.getByOffset(`inputOffset / ${n}`)}); 1353 ${y.setByOffset("global_idx","data")} 1354 }`;return` 1355 ${f.registerUniform("vec_size","u32").declareVariables(g,y)} 1356 ${f.mainStart()} 1357 ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} 1358 ${_}`},h=[{type:12,data:l},...Y(t,i)];return{name:"Expand",shaderCache:{hint:`${i.length};${n}${u}`,inputDependencies:["rank"]},getShaderSource:p,getRunData:()=>({outputs:[{dims:i,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:h})}},El=e=>{kl(e.inputs),e.compute(Il(e.inputs),{inputs:[0]})}}),zl,Cl,dm=q(()=>{te(),re(),ae(),ji(),zl=e=>{let t=e[0].dataType,r=O.size(e[0].dims),i=O.size(e[1].dims),a=i%4===0,s=n=>{let u=M("x",t,[1],4),l=M("bias",t,[1],4),p=K("y",t,[1],4),h=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],f=y=>` 1359 let bias${y}_offset: u32 = (global_idx * 4 + ${y}) % uniforms.bias_size; 1360 let bias${y} = ${l.getByOffset(`bias${y}_offset / 4`)}[bias${y}_offset % 4];`,g=a?`
1361 let bias = ${l.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${f(0)}${f(1)}${f(2)}${f(3)} 1362 let bias = ${u.type.value}(bias0, bias1, bias2, bias3);`;return`${n.registerUniforms(h).declareVariables(u,l,p)} 1363 1364 ${Hi(Ee(t))} 1365 1366 ${n.mainStart(qt)} 1367 ${n.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} 1368 1369 let x = ${u.getByOffset("global_idx")}; 1370 ${g} 1371 let x_in = x + bias; 1372 ${p.setByOffset("global_idx",Fi("x_in"))} 1373 }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${a}`,inputDependencies:["type","type"]},getShaderSource:s,getRunData:n=>({outputs:[{dims:n[0].dims,dataType:n[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:i}],dispatchGroup:{x:Math.ceil(r/qt/4)}})}},Cl=e=>{e.inputs.length<2||O.size(e.inputs[1].dims)===0?du(e):e.compute(zl(e.inputs))}}),Al,Ol,Rl,Bl,pm=q(()=>{te(),re(),xe(),ae(),Al=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Ol=(e,t)=>{let r=e[0].dims,i=e[1].dims,a=r.length,s=O.normalizeAxis(t.axis,a),n=r.slice(0);n.splice(s,1,...i);let u=r[s],l=e[0].dataType===9?4:1,p=Math.ceil(O.size(n)/l),h=[{type:12,data:p},{type:6,data:u},{type:12,data:s},...Y(e[0].dims,e[1].dims,n)],f=g=>{let y=M("data",e[0].dataType,e[0].dims.length,l),_=M("inputIndices",e[1].dataType,e[1].dims.length),w=K("output",e[0].dataType,n.length,l),S=b=>{let I=i.length,T=`var indicesIndices${b} = ${_.type.indices}(0);`;for(let E=0;E<I;E++)T+=`${I>1?`indicesIndices${b}[${E}]`:`indicesIndices${b}`} = ${n.length>1?`outputIndices${b}[uniforms.axis + ${E}]`:`outputIndices${b}`};`;T+=` 1374 var idx${b} = ${_.getByIndices(`indicesIndices${b}`)}; 1375 if (idx${b} < 0) { 1376 idx${b} = idx${b} + uniforms.axisDimLimit; 1377 } 1378 var dataIndices${b} : ${y.type.indices}; 1379 `;for(let E=0,C=0;E<a;E++)E===s?(T+=`${a>1?`dataIndices${b}[${E}]`:`dataIndices${b}`} = u32(idx${b});`,C+=I):(T+=`${a>1?`dataIndices${b}[${E}]`:`dataIndices${b}`} = ${n.length>1?`outputIndices${b}[${C}]`:`outputIndices${b}`};`,C++);return T},x;if(e[0].dataType===9){let b=(I,T,E="")=>` 1380 let outputIndices${T} = ${w.offsetToIndices(`outputOffset + ${T}u`)}; 1381 ${S(T)}; 1382 let offset${T} = ${y.indicesToOffset(`dataIndices${T}`)}; 1383 let index${T} = offset${T} / 4u; 1384 let component${T} = offset${T} % 4u; 1385 ${I}[${T}] = ${E}(${y.getByOffset(`index${T}`)}[component${T}]); 1386 `;x=` 1387 let outputOffset = global_idx * ${l}; 1388 var value = vec4<u32>(0); 1389 ${b("value",0,"u32")} 1390 ${b("value",1,"u32")} 1391 ${b("value",2,"u32")} 1392 ${b("value",3,"u32")} 1393 ${w.setByOffset("global_idx","value")} 1394 `}else x=` 1395 let outputIndices = ${w.offsetToIndices("global_idx")}; 1396 ${S("")}; 1397 let value = ${y.getByIndices("dataIndices")}; 1398 ${w.setByOffset("global_idx","value")}; 1399 `;return` 1400 ${g.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(y,_,w)} 1401 ${g.mainStart()} 1402 ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 1403 ${x} 1404 }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:f}},Rl=e=>he({axis:e.axis}),Bl=(e,t)=>{let r=e.inputs;Al(r),e.compute(Ol(e.inputs,t))}}),Ml,Nl,Dl,cm=q(()=>{te(),re(),ae(),Ml=(e,t,r,i,a,s,n,u,l)=>{let p=[{type:12,data:s},{type:12,data:i},{type:12,data:a},{type:12,data:r},{type:12,data:n},{type:12,data:u},{type:12,data:l}],h=[s];p.push(...Y(t.dims,h));let f=g=>{let y=M("indices_data",t.dataType,t.dims.length),_=K("input_slice_offsets_data",12,1,1),w=[y,_],S=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:a.length},{name:"sizes_from_slice_dims_data",type:"u32",length:r.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return` 1405 ${g.registerUniforms(S).declareVariables(...w)} 1406 ${g.mainStart()} 1407 ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1408 let batch_idx = global_idx / uniforms.num_slices_per_batch; 1409 let base_offset = batch_idx * uniforms.input_batch_stride; 1410 1411 let slice_indices_base_offset = global_idx * uniforms.num_slice_dims; 1412 var relative_slice_offset = 0; 1413 for (var dim_idx = 0u; dim_idx < uniforms.num_slice_dims; dim_idx ++) { 1414 var index = i32(indices_data[dim_idx + slice_indices_base_offset].x); 1415 let input_dim_idx = uniforms.batch_dims + dim_idx; 1416 if (index < 0) { 1417 ${a.length===1?"index += i32(uniforms.input_dims);":"index += i32(uniforms.input_dims[input_dim_idx]);"} 1418 } 1419 ${r.length===1?"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data);":"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data[dim_idx]);"} 1420 } 1421 1422 input_slice_offsets_data[global_idx] = base_offset + u32(relative_slice_offset); 1423 }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${a.length}_${r.length}`,inputDependencies:["rank"]},getRunData:()=>
vendor: 4,258 bytes, lines 1423-1450
1423({outputs:[{dims:h,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:p}),getShaderSource:f},{inputs:[t],outputs:[-1]})[0]},Nl=(e,t)=>{let r=e.inputs,i=r[0].dims,a=r[0].dataType,s=r[1].dims,n=s[s.length-1],u=O.sizeToDimension(s,s.length-1),l=O.sizeFromDimension(i,t.batchDims+n),p=O.sizeToDimension(i,t.batchDims),h=O.sizeFromDimension(i,t.batchDims),f=u/p,g=new Array(n),y=l;for(let T=0;T<n;++T)g[n-1-T]=y,y*=i[t.batchDims+n-1-T];let _=Ml(e,r[1],g,t.batchDims,i,u,f,h,n),w=t.batchDims+n;if(w>i.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let S=s.slice(0,-1).concat(i.slice(w)),x=O.size(S),b=[{type:12,data:x},{type:12,data:l},...Y(r[0].dims,_.dims,S)],I=T=>{let E=M("data",r[0].dataType,r[0].dims.length),C=M("slice_offsets",12,_.dims.length),A=K("output",r[0].dataType,S.length);return` 1424 ${T.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(E,C,A)} 1425 ${T.mainStart()} 1426 ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1427 let slice_offset = slice_offsets[global_idx / uniforms.slice_size]; 1428 output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size]; 1429 }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:S,dataType:a}],dispatchGroup:{x:Math.ceil(x/64)},programUniforms:b}),getShaderSource:I},{inputs:[r[0],_]})},Dl=e=>({batchDims:e.batch_dims,cacheKey:""})}),Pl,Ul,ql,Wl,hm=q(()=>{te(),re(),xe(),ae(),Pl=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let r=O.normalizeAxis(t.quantizeAxis,e[0].dims.length),i=t.blockSize,a=e[0],s=e[2],n=e.length===4?e[3]:void 0;if(s.dims.length!==a.dims.length||!a.dims.map((u,l)=>l===r?Math.ceil(u/i)===s.dims[l]:u===s.dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(n){if(n.dataType!==a.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(n.dims.length!==s.dims.length||!n.dims.map((u,l)=>u===s.dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},Ul=(e,t)=>{let r=e[0].dims,i=e[1].dims,a=r.length,s=O.normalizeAxis(t.gatherAxis,a),n=O.normalizeAxis(t.quantizeAxis,a),u=r.slice(0);u.splice(s,1,...i);let l=O.size(u),p=e[2].dataType,h=e[0].dataType===22,f=[{type:12,data:l},{type:12,data:n},{type:12,data:s},{type:12,data:t.blockSize},...Y(...e.map((y,_)=>y.dims),u)],g=y=>{let _=M("data",e[0].dataType,e[0].dims.length),w=M("inputIndices",e[1].dataType,e[1].dims.length),S=M("scales",e[2].dataType,e[2].dims.length),x=e.length>3?M("zeroPoint",e[3].dataType,e[3].dims.length):void 0,b=K("output",p,u.length),I=[_,w,S];x&&I.push(x);let T=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` 1430 ${y.registerUniforms(T).declareVariables(...I,b)} 1431 ${y.mainStart()} 1432 let output_indices = ${b.offsetToIndices("global_idx")}; 1433 var indices_indices = ${w.type.indices}(0); 1434 ${i.length>1?` 1435 for (var i: u32 = 0; i < ${i.length}; i++) { 1436 let index = ${b.indicesGet("output_indices","uniforms.gather_axis + i")}; 1437 ${w.indicesSet("indices_indices","i","index")}; 1438 }`:`indices_indices = ${b.indicesGet("output_indices","uniforms.gather_axis")};`}; 1439 var data_indices = ${_.type.indices}(0); 1440 for (var i: u32 = 0; i < uniforms.gather_axis; i++) { 1441 let index = ${b.indicesGet("output_indices","i")}; 1442 ${_.indicesSet("data_indices","i","index")}; 1443 } 1444 var index_from_indices = ${w.getByIndices("indices_indices")}; 1445 if (index_from_indices < 0) { 1446 index_from_indices += ${r[s]}; 1447 } 1448 ${_.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")}; 1449 for (var i = uniforms.gather_axis + 1; i < ${u.length}; i++) { 1450 let index = ${b.indicesGet("output_indices",`i + ${i.length} - 1`)};
1451 ${_.indicesSet("data_indices","i","index")}; 1452 } 1453 let data_offset = ${_.indicesToOffset("data_indices")}; 1454 let data_index = data_offset % 8; 1455 // Convert 4-bit packed data to 8-bit packed data. 1456 let packed_4bit_quantized_data = ${_.getByOffset("data_offset / 8")}; 1457 let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; 1458 let quantized_data_vec = ${h?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); 1459 let quantized_data = quantized_data_vec[data_index / 2]; 1460 var scale_indices = data_indices; 1461 let quantize_axis_index = ${S.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; 1462 ${S.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; 1463 var scale = ${S.getByIndices("scale_indices")}; 1464 ${x?` 1465 let zero_point_indices = scale_indices; 1466 let zero_point_offset = ${x.indicesToOffset("zero_point_indices")}; 1467 let zero_point_index = zero_point_offset % 8; 1468 let packed_4bit_zero_points = ${x.getByOffset("zero_point_offset / 8")}; 1469 let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; 1470 let zero_point_vec = ${h?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); 1471 let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"}; 1472 let dequantized_data = ${Ee(p)}(quantized_data - zero_point) * scale; 1473 ${b.setByOffset("global_idx","dequantized_data")}; 1474 }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((y,_)=>_!==1).map(y=>y.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(y,_)=>"rank")},getRunData:()=>({outputs:[{dims:u,dataType:p}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:f}),getShaderSource:g}},ql=(e,t)=>{let r=e.inputs;
vendor: 6,907 bytes, lines 1474-1572
1474Pl(r,t),e.compute(Ul(e.inputs,t))},Wl=e=>he({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),Ll,Vl,Gl,Hl,fm=q(()=>{te(),re(),xe(),ae(),Ll=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and 1475 indices input tensors be of same rank.`)},Vl=(e,t)=>{let r=e[0].dims,i=e[0].dataType,a=r.length,s=e[1].dims,n=e[1].dataType,u=O.normalizeAxis(t.axis,a),l=r[u],p=s.slice(0),h=O.size(p),f=M("input",i,a),g=M("indicesInput",n,s.length),y=K("output",i,p.length),_=[{type:12,data:h},{type:6,data:l},{type:12,data:u}];return _.push(...Y(r,s,p)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:_}),getShaderSource:w=>` 1476 ${w.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(f,g,y)} 1477 ${w.mainStart()} 1478 ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 1479 1480 let outputIndices = ${y.offsetToIndices("global_idx")}; 1481 1482 var idx = ${g.getByOffset("global_idx")}; 1483 if (idx < 0) { 1484 idx = idx + uniforms.axisDimLimit; 1485 } 1486 var inputIndices = ${f.type.indices}(outputIndices); 1487 ${f.indicesSet("inputIndices","uniforms.axis","u32(idx)")}; 1488 let value = ${f.getByIndices("inputIndices")}; 1489 1490 ${y.setByOffset("global_idx","value")}; 1491 }`}},Gl=e=>he({axis:e.axis}),Hl=(e,t)=>{let r=e.inputs;Ll(r),e.compute(Vl(e.inputs,t))}}),Fl,jl,Kl,Ql,mm=q(()=>{te(),re(),ae(),Fl=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},jl=(e,t)=>{let r=e[0].dims.slice(),i=e[1].dims.slice(),[a,s,n]=is.getShapeOfGemmResult(r,t.transA,i,t.transB,e.length===3?e[2].dims:void 0),u=[a,s];if(!u)throw new Error("Can't use gemm on the given tensors");let l=16,p=Math.ceil(s/l),h=Math.ceil(a/l),f=!0,g=O.size(u),y=[{type:12,data:f?p:g},{type:12,data:a},{type:12,data:s},{type:12,data:n},{type:1,data:t.alpha},{type:1,data:t.beta}],_=["type","type"];e.length===3&&(y.push(...Y(e[2].dims)),_.push("rank")),y.push(...Y(u));let w=x=>{let b="";t.transA&&t.transB?b="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?b="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?b="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(b="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let I=t.alpha===1?"":"value *= uniforms.alpha;",T=M("a",e[0].dataType,e[0].dims),E=M("b",e[1].dataType,e[1].dims),C=T.type.value,A=null,v=[T,E];e.length===3&&(A=M("c",e[2].dataType,e[2].dims.length),v.push(A));let N=K("output",e[0].dataType,u.length);v.push(N);let D=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` 1492 ${x.registerUniforms(D).declareVariables(...v)} 1493 1494 ${x.mainStart()} 1495 ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1496 1497 let m = global_idx / uniforms.N; 1498 let n = global_idx % uniforms.N; 1499 1500 var value = ${C}(0); 1501 for (var k: u32 = 0u; k < uniforms.K; k++) { 1502 ${b} 1503 } 1504 1505 ${I} 1506 ${A!=null?`let cOffset = ${A.broadcastedIndicesToOffset("vec2(m, n)",N)}; value += ${C}(uniforms.beta) * ${A.getByOffset("cOffset")};`:""} 1507 output[global_idx] = value; 1508 }`},S=x=>{let b=M("a",e[0].dataType,e[0].dims),I=M("b",e[1].dataType,e[1].dims),T=null,E=[b,I];e.length===3&&(T=M("c",e[2].dataType,e[2].dims.length),E.push(T));let C=K("output",e[0].dataType,u.length);E.push(C);let A=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],v="",N="";t.transA&&t.transB?(N=` 1509 var col = tile_row_start + local_id.x; 1510 var row = k_start + local_id.y; 1511 if (col < uniforms.M && row < uniforms.K) { 1512 tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col]; 1513 } else { 1514 tile_a[local_id.y][local_id.x] = ${b.type.value}(0); 1515 } 1516 1517 col = k_start + local_id.x; 1518 row = tile_col_start + local_id.y; 1519 if (col < uniforms.K && row < uniforms.N) { 1520 tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; 1521 } else { 1522 tile_b[local_id.y][local_id.x] = ${I.type.value}(0); 1523 } 1524 `,v="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(N=` 1525 var col = tile_row_start + local_id.x; 1526 var row = k_start + local_id.y; 1527 if (col < uniforms.M && row < uniforms.K) { 1528 tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col]; 1529 } else { 1530 tile_a[local_id.y][local_id.x] = ${b.type.value}(0); 1531 } 1532 1533 col = tile_col_start + local_id.x; 1534 row = k_start + local_id.y; 1535 if (col < uniforms.N && row < uniforms.K) { 1536 tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; 1537 } else { 1538 tile_b[local_id.y][local_id.x] = ${I.type.value}(0); 1539 } 1540 `,v="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(N=` 1541 var col = k_start + local_id.x; 1542 var row = tile_row_start + local_id.y; 1543 if (col < uniforms.K && row < uniforms.M) { 1544 tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col]; 1545 } else { 1546 tile_a[local_id.y][local_id.x] = ${b.type.value}(0); 1547 } 1548 1549 col = k_start + local_id.x; 1550 row = tile_col_start + local_id.y; 1551 if (col < uniforms.K && row < uniforms.N) { 1552 tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; 1553 } else { 1554 tile_b[local_id.y][local_id.x] = ${I.type.value}(0); 1555 } 1556 `,v="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(N=` 1557 var col = k_start + local_id.x; 1558 var row = tile_row_start + local_id.y; 1559 if (col < uniforms.K && row < uniforms.M) { 1560 tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col]; 1561 } else { 1562 tile_a[local_id.y][local_id.x] = ${b.type.value}(0); 1563 } 1564 1565 col = tile_col_start + local_id.x; 1566 row = k_start + local_id.y; 1567 if (col < uniforms.N && row < uniforms.K) { 1568 tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; 1569 } else { 1570 tile_b[local_id.y][local_id.x] = ${I.type.value}(0); 1571 } 1572 `,v="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let D=t.alpha===1?"":"value *= uniforms.alpha;";return`
1573 ${x.registerUniforms(A).declareVariables(...E)} 1574 var<workgroup> tile_a: array<array<${b.type.storage}, ${l}>, ${l}>; 1575 var<workgroup> tile_b: array<array<${I.type.storage}, ${l}>, ${l}>; 1576 ${x.mainStart([l,l,1])} 1577 let tile_col_start = (workgroup_index % uniforms.num_tile_n) * ${l}; 1578 let tile_row_start = (workgroup_index / uniforms.num_tile_n) * ${l}; 1579 let num_tiles = (uniforms.K - 1) / ${l} + 1; 1580 var k_start = 0u; 1581 var value = ${C.type.value}(0); 1582 for (var t: u32 = 0u; t < num_tiles; t++) { 1583 ${N} 1584 k_start = k_start + ${l}; 1585 workgroupBarrier(); 1586 1587 for (var k: u32 = 0u; k < ${l}; k++) { 1588 ${v} 1589 } 1590 workgroupBarrier(); 1591 } 1592 1593 ${D} 1594 let m = tile_row_start + local_id.y; 1595 let n = tile_col_start + local_id.x; 1596 ${T!=null?`let cOffset = ${T.broadcastedIndicesToOffset("vec2(m, n)",C)}; value += ${C.type.value}(uniforms.beta) * ${T.getByOffset("cOffset")};`:""} 1597 if (m < uniforms.M && n < uniforms.N) { 1598 output[m * uniforms.N + n] = value; 1599 } 1600 }`};return f?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:p*h},programUniforms:y}),getShaderSource:S}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:y}),getShaderSource:w}},Kl=e=>{let t=e.transA,r=e.transB,i=e.alpha,a=e.beta;return{transA:t,transB:r,alpha:i,beta:a,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},Ql=(e,t)=>
vendor: 4,741 bytes, lines 1600-1703
1600{Fl(e.inputs),e.compute(jl(e.inputs,t))}}),Qe,it,zt,Ct,Zl,Yl,Xl,Jl,ed,td,rd,id,ad,nd,gm=q(()=>{te(),re(),xe(),ae(),[Qe,it,zt,Ct]=[0,1,2,3],Zl=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},Yl=` 1601 fn gs_get_cubic_coeffs(x: f32) -> vec4<f32> { 1602 let cubic_alpha = -0.75f; 1603 let x_abs = abs(x); 1604 var coeffs: vec4<f32>; 1605 coeffs[0] = (((cubic_alpha * (x_abs + 1) - 5 * cubic_alpha) * (x_abs + 1) + 8 * cubic_alpha) * (x_abs + 1) - 4 * cubic_alpha); 1606 coeffs[1] = (((cubic_alpha + 2) * x_abs - (cubic_alpha + 3)) * x_abs * x_abs + 1); 1607 coeffs[2] = (((cubic_alpha + 2) * (1 - x_abs) - (cubic_alpha + 3)) * (1 - x_abs) * (1 - x_abs) + 1); 1608 coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha); 1609 return coeffs; 1610 } 1611`,Xl=e=>` 1612 fn gs_bicubic_interpolate(p: mat4x4<${e}>, x: f32, y: f32) -> ${e} { 1613 var v: vec4<f32>; 1614 var coeffs = gs_get_cubic_coeffs(x); 1615 for (var i = 0; i < 4; i++) { 1616 v[i] = coeffs[0] * p[i][0] + coeffs[1] * p[i][1] + coeffs[2] * p[i][2] + coeffs[3] * p[i][3]; 1617 } 1618 coeffs = gs_get_cubic_coeffs(y); 1619 let pixel = ${e}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]); 1620 return pixel; 1621 } 1622`,Jl=e=>` 1623 fn gs_denormalize(n: f32, length: i32) -> f32 { 1624 ${e.alignCorners===0?` 1625 // alignCorners: false => [-1, 1] to [-0.5, length - 0.5] 1626 return ((n + 1.0) * f32(length) - 1.0) / 2.0; 1627 `:` 1628 // alignCorners: true => [-1, 1] to [0, length - 1] 1629 return (n + 1.0) / 2.0 * (f32(length - 1)); 1630 `} 1631 } 1632`,ed=e=>` 1633 ${e.paddingMode==="reflection"?` 1634 fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 { 1635 var dx = 0.0; 1636 var fx = f32(x); 1637 let range = x_max - x_min; 1638 if (fx < x_min) { 1639 dx = x_min - fx; 1640 let n = u32(dx / range); 1641 let r = dx - f32(n) * range; 1642 if (n % 2 == 0) { 1643 fx = x_min + r; 1644 } else { 1645 fx = x_max - r; 1646 } 1647 } else if (fx > x_max) { 1648 dx = fx - x_max; 1649 let n = u32(dx / range); 1650 let r = dx - f32(n) * range; 1651 if (n % 2 == 0) { 1652 fx = x_max - r; 1653 } else { 1654 fx = x_min + r; 1655 } 1656 } 1657 return u32(fx); 1658 }`:""} 1659`,td=(e,t,r)=>` 1660 fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4<f32>) -> ${t} { 1661 var pixel = ${t}(0); 1662 var indices = vec4<u32>(0); 1663 indices[${Qe}] = batch; 1664 indices[${it}] = channel;`+(()=>{switch(r.paddingMode){case"zeros":return` 1665 if (r >= 0 && r < H && c >=0 && c < W) { 1666 indices[${zt}] = u32(r); 1667 indices[${Ct}] = u32(c); 1668 } else { 1669 return ${t}(0); 1670 } 1671 `;case"border":return` 1672 indices[${zt}] = u32(clamp(r, 0, H - 1)); 1673 indices[${Ct}] = u32(clamp(c, 0, W - 1)); 1674 `;case"reflection":return` 1675 indices[${zt}] = gs_reflect(r, border[1], border[3]); 1676 indices[${Ct}] = gs_reflect(c, border[0], border[2]); 1677 `;default:throw new Error(`padding mode ${r.paddingMode} is not supported`)}})()+` 1678 return ${e.getByIndices("indices")}; 1679 } 1680`,rd=(e,t,r)=>(()=>{switch(r.mode){case"nearest":return` 1681 let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${Qe}], indices[${it}], border); 1682 `;case"bilinear":return` 1683 let x1 = i32(floor(x)); 1684 let y1 = i32(floor(y)); 1685 let x2 = x1 + 1; 1686 let y2 = y1 + 1; 1687 1688 let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${Qe}], indices[${it}], border); 1689 let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${Qe}], indices[${it}], border); 1690 let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${Qe}], indices[${it}], border); 1691 let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${Qe}], indices[${it}], border); 1692 1693 let dx2 = ${t}(f32(x2) - x); 1694 let dx1 = ${t}(x - f32(x1)); 1695 let dy2 = ${t}(f32(y2) - y); 1696 let dy1 = ${t}(y - f32(y1)); 1697 let result = dy2 * (dx2 * p11 + dx1 * p12) + dy1 * (dx2 * p21 + dx1 * p22); 1698 `;case"bicubic":return` 1699 let x0 = i32(floor(x)) - 1; 1700 let y0 = i32(floor(y)) - 1; 1701 var p: mat4x4<${t}>; 1702 for (var h = 0; h < 4; h++) { 1703 for (var w = 0; w < 4; w++) {
1704 p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${Qe}], indices[${it}], border); 1705 } 1706 } 1707 1708 let dx = x - f32(x0 + 1); 1709 let dy = y - f32(y0 + 1); 1710 let result = gs_bicubic_interpolate(p, dx, dy); 1711 `;default:throw new Error(`mode ${r.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,id=(e,t)=>{let r=M("x",e[0].dataType,e[0].dims.length),i=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],a=M("grid",e[1].dataType,i.length,2),s=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(s=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[Qe,it,zt,Ct]=[0,3,1,2]);let n=K("output",e[0].dataType,s.length),u=r.type.value,l=O.size(s),p=[{type:12,data:l},...Y(e[0].dims,i,s)],h=f=>` 1712 ${f.registerUniform("output_size","u32").declareVariables(r,a,n)} 1713 ${Yl} 1714 ${Xl(u)} 1715 ${Jl(t)} 1716 ${ed(t)} 1717 ${td(r,u,t)} 1718 1719 ${f.mainStart()} 1720 ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1721 let H_in = i32(uniforms.x_shape[${zt}]); 1722 let W_in = i32(uniforms.x_shape[${Ct}]); 1723 1724 ${t.alignCorners===0?` 1725 let x_min = -0.5; 1726 let x_max = f32(W_in) - 0.5; 1727 let y_min = -0.5; 1728 let y_max = f32(H_in) - 0.5; 1729 `:` 1730 let x_min = 0.0; 1731 let x_max = f32(W_in) - 1.0; 1732 let y_min = 0.0; 1733 let y_max = f32(H_in) - 1.0; 1734 `}; 1735 let border = vec4<f32>(x_min, y_min, x_max, y_max); 1736 1737 let indices = ${n.offsetToIndices("global_idx")}; 1738 var grid_indices = vec3<u32>(indices[${Qe}], indices[${zt}], indices[${Ct}]); 1739 let nxy = ${a.getByIndices("grid_indices")}; 1740 var x = gs_denormalize(f32(nxy[0]), W_in); 1741 var y = gs_denormalize(f32(nxy[1]), H_in); 1742 1743 ${rd(n,u,t)} 1744 }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:f=>{let g=O.size(s);return{outputs:[{dims:s,dataType:f[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:p}},getShaderSource:h}},ad=(e,t)=>{Zl(e.inputs),e.compute(id(e.inputs,t))},nd=e=>he({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),ze,sd,od,ca,ud,ir,ld,dd=q(()=>{te(),re(),xe(),Oi(),Vi(),ae(),ut(),ze=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,sd=(e,t)=>{let r=e[0],i=ze(e,1),a=ze(e,2),s=ze(e,3),n=ze(e,4),u=ze(e,5),l=ze(e,6),p=ze(e,7);if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let h=r.dims[0],f=r.dims[1],g=r.dims.length===3?r.dims[2]:t.numHeads*r.dims[4],y=f,_=0,w=0,S=Math.floor(g/t.numHeads);if(l&&p&&O.size(l.dims)&&O.size(p.dims)){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(l.dims[0]!==h||l.dims[1]!==t.numHeads||l.dims[3]!==S)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(p.dims[0]!==h||p.dims[1]!==t.numHeads||p.dims[3]!==S)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(l.dims[2]!==p.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(p.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');_=l.dims[2],w=l.dims[2]}else if(l&&O.size(l.dims)||p&&O.size(p.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let x;if(i&&O.size(i.dims)>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(i.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');x=2,y=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==S)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(a)throw new Error('Expect "value" be none when "key" has packed kv format.');x=5,y=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==S)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');x=0,y=i.dims[2]}}else{if(r.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(r.dims[2]!==t.numHeads||r.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');x=3}if(s&&O.size(s.dims)>0){if(s.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');
vendor: 30,817 bytes, lines 1744-1972
1744if(i&&i.dims.length===5&&i.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let b=_+y,I=0;if(n&&O.size(n.dims)>0){I=8;let A=n.dims;throw A.length===1?A[0]===h?I=1:A[0]===3*h+2&&(I=3):A.length===2&&A[0]===h&&A[1]===b&&(I=5),I===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let T=!1,E=g;if(a&&O.size(a.dims)>0){if(a.dims.length!==3&&a.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==a.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(a.dims.length===3){if(y!==a.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');E=a.dims[2]}else{if(y!==a.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');E=a.dims[1]*a.dims[3],T=!0}}let C=!1;if(n&&O.size(n.dims)>0)throw new Error("Key padding mask is not supported");if(u&&O.size(u.dims)>0){if(u.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(u.dims[0]!==h||u.dims[1]!==t.numHeads||u.dims[2]!==f||u.dims[3]!==b)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:h,sequenceLength:f,pastSequenceLength:_,kvSequenceLength:y,totalSequenceLength:b,maxSequenceLength:w,inputHiddenSize:0,hiddenSize:g,vHiddenSize:E,headSize:S,vHeadSize:Math.floor(E/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:I,scale:t.scale,broadcastResPosBias:C,passPastInKv:T,qkvFormat:x}},od=e=>he({...e}),ca=he({perm:[0,2,1,3]}),ud=(e,t,r,i,a,s,n)=>{let u=[i,a,s],l=O.size(u),p=[{type:12,data:l},{type:12,data:n},{type:12,data:s}],h=f=>{let g=K("qkv_with_bias",t.dataType,u),y=M("qkv",t.dataType,u),_=M("bias",r.dataType,u),w=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` 1745 ${f.registerUniforms(w).declareVariables(y,_,g)} 1746 ${f.mainStart()} 1747 ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1748 let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; 1749 1750 qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; 1751 }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:u,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:p}),getShaderSource:h},{inputs:[t,r],outputs:[-1]})[0]},ir=(e,t,r,i,a,s,n,u)=>{let l=s;if(n&&O.size(n.dims)>0){if(i===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return l=ud(e,s,n,t,i,r*a,u),l=l.reshape([t,i,r,a]),r===1||i===1?l:e.compute(Me(l,ca.perm),{inputs:[l],outputs:[-1]})[0]}else return s.dims.length===3&&(l=s.reshape([t,i,r,a])),r===1||i===1?l:e.compute(Me(l,ca.perm),{inputs:[l],outputs:[-1]})[0]},ld=(e,t)=>{let r=sd(e.inputs,t),i=e.inputs[0],a=ze(e.inputs,1),s=ze(e.inputs,2),n=ze(e.inputs,3),u=ze(e.inputs,4),l=ze(e.inputs,5),p=ze(e.inputs,6),h=ze(e.inputs,7);if(i.dims.length===5)throw new Error("Packed QKV is not implemented");if((a==null?void 0:a.dims.length)===5)throw new Error("Packed KV is not implemented");let f=a&&s&&a.dims.length===4&&s.dims.length===4,g=ir(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,i,n,0);if(f)return Xt(e,g,a,s,u,void 0,p,h,l,r);if(!a||!s)throw new Error("key and value must be provided");let y=ir(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,a,n,r.hiddenSize),_=ir(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,s,n,2*r.hiddenSize);Xt(e,g,y,_,u,void 0,p,h,l,r)}}),pd,cd,hd,fd,ha,md,gd,yd=q(()=>{te(),re(),xe(),ae(),pd=e=>{if(!e||e.length<1)throw new Error("too few inputs")},cd=(e,t)=>{let r=[],i=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(a=>r.push(Number(a))),i=r.length),he({numOutputs:i,axis:t.axis,splitSizes:r})},hd=e=>` 1752fn calculateOutputIndex(index: u32) -> u32 { 1753 for (var i: u32 = 0u; i < ${e}u; i += 1u ) { 1754 if (index < ${Z("uniforms.size_in_split_axis","i",e)}) { 1755 return i; 1756 } 1757 } 1758 return ${e}u; 1759}`,fd=e=>{let t=e.length,r=[];for(let i=0;i<t;++i){let a=e[i].setByIndices("indices","input[global_idx]");t===1?r.push(a):i===0?r.push(`if (output_number == ${i}u) { ${a} }`):i===t-1?r.push(`else { ${a} }`):r.push(`else if (output_number == ${i}) { ${a} }`)}return` 1760 fn writeBufferData(output_number: u32, indices: ${e[0].type.indices}, global_idx: u32) { 1761 ${r.join(` 1762`)} 1763 }`},ha=(e,t)=>{let r=e[0].dims,i=O.size(r),a=e[0].dataType,s=O.normalizeAxis(t.axis,r.length),n=new Array(t.numOutputs),u=M("input",a,r.length),l=new Array(t.numOutputs),p=[],h=[],f=0,g=[{type:12,data:i}];for(let _=0;_<t.numOutputs;_++){f+=t.splitSizes[_],l[_]=f;let w=r.slice();w[s]=t.splitSizes[_],h.push(w),n[_]=K(`output${_}`,a,w.length),p.push({dims:h[_],dataType:e[0].dataType})}g.push({type:12,data:l},...Y(r,...h));let y=_=>` 1764 ${_.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",l.length).declareVariables(u,...n)} 1765 ${hd(l.length)} 1766 ${fd(n)} 1767 1768 ${_.mainStart()} 1769 ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} 1770 1771 var indices = ${u.offsetToIndices("global_idx")}; 1772 var index = ${u.indicesGet("indices",s)}; 1773 let output_number = calculateOutputIndex(index); 1774 if (output_number != 0) { 1775 index -= ${Z("uniforms.size_in_split_axis","output_number - 1u",l.length)}; 1776 ${u.indicesSet("indices",s,"index")}; 1777 } 1778 writeBufferData(output_number, indices, global_idx); 1779 }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:y,getRunData:()=>({outputs:p,dispatchGroup:{x:Math.ceil(i/64)},programUniforms:g})}},md=(e,t)=>{pd(e.inputs);let r=e.inputs.length===1?t:cd(e.inputs,t);e.compute(ha(e.inputs,r),{inputs:[0]})},gd=e=>{let t=e.axis,r=e.splitSizes,i=e.numOutputs<0?r.length:e.numOutputs;if(i!==r.length)throw new Error("numOutputs and splitSizes length must be equal");return he({axis:t,numOutputs:i,splitSizes:r})}}),_d,Br,wd,bd=q(()=>{te(),re(),xe(),ae(),_d=(e,t)=>{let[r,i,a,s]=e,{numHeads:n,rotaryEmbeddingDim:u}=t;if(r.dims.length!==3&&r.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`);if(!O.areEqual(i.dims,[])&&!O.areEqual(i.dims,[1])&&i.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${i.dims.length}`);if(a.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${a.dims.length}`);if(s.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${s.dims.length}`);if(!O.areEqual(a.dims,s.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(u>0&&n===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let l=r.dims[0],p=r.dims[r.dims.length-2],h=a.dims[0],f=O.sizeFromDimension(r.dims,1)/p,g=u===0?a.dims[1]*2:f/n;if(u>g)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(i.dims.length===2){if(l!==i.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${i.dims[0]}`);if(p!==i.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${i.dims[1]}`)}if(g/2!==a.dims[1]&&u/2!==a.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${a.dims[1]}`);if(p>h)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},Br=(e,t)=>{let{interleaved:r,numHeads:i,rotaryEmbeddingDim:a,scale:s}=t,n=e[0].dims[0],u=O.sizeFromDimension(e[0].dims,1),l=e[0].dims[e[0].dims.length-2],p=u/l,h=e[2].dims[1],f=a===0?h*2:p/i,g=new Array(n,l,p/f,f-h),y=O.computeStrides(g),_=[{type:1,data:s},{type:12,data:g},{type:12,data:y},...e[0].dims.length===3?new Array({type:12,data:[u,p,f,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[u,f,l*f,1]}):[],...Y(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],w=S=>{let x=M("input",e[0].dataType,e[0].dims.length),b=M("position_ids",e[1].dataType,e[1].dims.length),I=M("cos_cache",e[2].dataType,e[2].dims.length),T=M("sin_cache",e[3].dataType,e[3].dims.length),E=K("output",e[0].dataType,e[0].dims.length);return S.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:g.length},{name:"global_strides",type:"u32",length:y.length},{name:"input_output_strides",type:"u32",length:y.length}]),` 1780 ${S.declareVariables(x,b,I,T,E)} 1781 1782 ${S.mainStart(qt)} 1783 let half_rotary_emb_dim = uniforms.${I.name}_shape[1]; 1784 let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; 1785 let size = uniforms.global_shape[0] * uniforms.global_strides[0]; 1786 ${S.guardAgainstOutOfBoundsWorkgroupSizes("size")} 1787 1788 if (bsnh[3] < half_rotary_emb_dim) { 1789 let position_ids_idx = 1790 ${b.broadcastedIndicesToOffset("bsnh.xy",K("",b.type.tensor,2))}; 1791 let position_id = 1792 u32(${b.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); 1793 let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r}); 1794 let j = i + select(half_rotary_emb_dim, 1, ${r}); 1795 let re = ${x.getByOffset("i")} * ${I.get("position_id","bsnh[3]")} - 1796 ${x.getByOffset("j")} * ${T.get("position_id","bsnh[3]")}; 1797 ${E.setByOffset("i","re")} 1798 let im = ${x.getByOffset("i")} * ${T.get("position_id","bsnh[3]")} + 1799 ${x.getByOffset("j")} * ${I.get("position_id","bsnh[3]")}; 1800 ${E.setByOffset("j","im")} 1801 } else { 1802 let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; 1803 ${E.setByOffset("k",x.getByOffset("k"))} 1804 } 1805 }`};return{name:"RotaryEmbedding",shaderCache:{hint:he({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:w,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(O.size(g)/qt)},programUniforms:_})}},wd=(e,t)=>{_d(e.inputs,t),e.compute(Br(e.inputs,t))}}),$d,vd,fa,xd,Sd,ym=q(()=>{xe(),te(),Vi(),dd(),yd(),ut(),bd(),ae(),$d=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let r=e[0],i=e[1],a=e[2],s=e[3],n=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let u=!1,l=r.dims[0],p=r.dims[1],h=r.dims.length===3?u?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],f=p,g=0,y=!i||i.dims.length===0,_=Math.floor(y?h/(t.numHeads+2*t.kvNumHeads):h/t.numHeads);y&&(h=_*t.numHeads);let w=s&&s.dims.length!==0,S=n&&n.dims.length!==0;if(w&&s.dims.length===4&&s.dims[0]===l&&s.dims[1]!==t.kvNumHeads&&s.dims[2]===t.kvNumHeads&&s.dims[3]===_)throw new Error("BSNH pastKey/pastValue is not supported");if(w&&S){if(s.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(n.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');g=s.dims[2]}else if(w||S)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let x=1;if(i&&i.dims.length>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(r.dims[2]%i.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');f=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==_)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(a)throw new Error('Expect "value" be none when "key" has packed kv format.');f=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==_)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');f=i.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');x=3}let b=0,I=!1,T=t.kvNumHeads?_*t.kvNumHeads:h;if(a&&a.dims.length>0){if(a.dims.length!==3&&a.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==a.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(a.dims.length===3){if(f!==a.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=a.dims[2]}else{if(f!==a.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');T=a.dims[1]*a.dims[3],I=!0}}let E=e.length>4?e[5]:void 0;if(E&&E.dims.length!==1&&E.dims[0]!==l)throw new Error('Input "seqlens" is expected to have 1 dimension and the same dim 0 as batch_size');return{batchSize:l,sequenceLength:p,pastSequenceLength:g,kvSequenceLength:f,totalSequenceLength:-1,maxSequenceLength:-1,inputHiddenSize:0,hiddenSize:h,vHiddenSize:T,headSize:_,vHeadSize:Math.floor(T/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:b,scale:t.scale,broadcastResPosBias:!1,passPastInKv:I,qkvFormat:x}},vd=he({perm:[0,2,1,3]}),fa=(e,t,r)=>{let i=t,a=r.kvNumHeads;return t.dims.length===3&&r.kvSequenceLength!==0&&(i=t.reshape([r.batchSize,r.kvSequenceLength,a,r.headSize]),i=e.compute(Me(i,vd.perm),{inputs:[i],outputs:[-1]})[0]),i},xd=(e,t,r,i)=>{let a=7,s=["type","type"],n=[e*t],u=e*t,l=[{type:12,data:u},{type:12,data:t},{type:12,data:e}],p=h=>{let f=M("seq_lens",r.dataType,r.dims),g=M("total_seq_lens",i.dataType,i.dims),y=K("pos_ids",a,n),_=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return` 1806 ${h.registerUniforms(_).declareVariables(f,g,y)} 1807 ${h.mainStart()} 1808 ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1809 let total_sequence_length = u32(${g.getByOffset("0")}); 1810 let is_subsequent_prompt = uniforms.sequence_length > 1 && uniforms.sequence_length != total_sequence_length; 1811 let is_first_prompt = !is_subsequent_prompt && uniforms.sequence_length == total_sequence_length; 1812 let batch_idx = global_idx / uniforms.sequence_length; 1813 let sequence_idx = i32(global_idx % uniforms.sequence_length); 1814 var pos_id: i32 = 0; 1815 let seqlen = ${f.getByOffset("batch_idx")}; 1816 let total_seqlen = seqlen + 1; 1817 if (is_first_prompt) { 1818 if (sequence_idx < total_seqlen) { 1819 pos_id = sequence_idx; 1820 } else { 1821 pos_id = 1; 1822 } 1823 ${y.setByOffset("global_idx","pos_id")} 1824 } else if (is_subsequent_prompt) { 1825 let past_seqlen = total_seqlen - i32(uniforms.sequence_length); 1826 if (past_seqlen + sequence_idx < total_seqlen) { 1827 pos_id = past_seqlen + sequence_idx; 1828 } else { 1829 pos_id = 1; 1830 } 1831 ${y.setByOffset("global_idx","pos_id")} 1832 } else if (global_idx < uniforms.batch_size) { 1833 ${y.setByOffset("global_idx","seqlen")} 1834 }; 1835 } 1836 `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:s},getRunData:()=>({outputs:[{dims:n,dataType:a}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:l}),getShaderSource:p}},Sd=(e,t)=>{var T;let r=$d(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(((T=e.inputs[1])==null?void 0:T.dims.length)===5)throw new Error("Packed KV is not implemented");let i=e.inputs[0],a=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,s=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,n=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,u=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,l=e.inputs.length>4?e.inputs[5]:void 0,p=e.inputs.length>5?e.inputs[6]:void 0,h=r.kvNumHeads?r.kvNumHeads:r.numHeads,f=he({axis:2,numOutputs:3,splitSizes:[r.numHeads*r.headSize,h*r.headSize,h*r.headSize]}),[g,y,_]=!a&&!s?e.compute(ha([i],f),{inputs:[i],outputs:[-1,-1,-1]}):[i,a,s],w,S;if(t.doRotary){let E=e.compute(xd(r.batchSize,r.sequenceLength,l,p),{inputs:[l,p],outputs:[-1]})[0],C=e.inputs[7],A=e.inputs[8],v=he({interleaved:t.rotaryInterleaved!==0,numHeads:r.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),N=[g,E,C,A],D=[-1];w=e.compute(Br(N,v),{inputs:N,outputs:D})[0],N.splice(0,1,y);let H=he({interleaved:t.rotaryInterleaved!==0,numHeads:r.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});S=e.compute(Br(N,H),{inputs:N,outputs:D})[0]}let x=ir(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,t.doRotary?w:g,void 0,0),b=fa(e,t.doRotary?S:y,r),I=fa(e,_,r);Xt(e,x,b,I,void 0,void 0,n,u,void 0,r,l,p)}}),ma,kd,Td,Id,_m=q(()=>{te(),re(),ut(),ae(),ma=(e,t,r,i,a,s,n,u)=>{let l=ve(s),p=l===1?"f32":`vec${l}f`,h=l===1?"vec2f":`mat2x${l}f`,f=a*n,g=64;f===1&&(g=256);let y=[a,n,s/l],_=[a,n,2],w=["rank","type","type"],S=[];S.push(...Y(y,_));let x=b=>{let I=M("x",t.dataType,3,l),T=M("scale",r.dataType,r.dims),E=M("bias",i.dataType,i.dims),C=K("output",1,3,2),A=[I,T,E,C];return` 1837 var<workgroup> workgroup_shared : array<${h}, ${g}>; 1838 const workgroup_size = ${g}u; 1839 ${b.declareVariables(...A)} 1840 ${b.mainStart(g)} 1841 let batch = workgroup_index / uniforms.x_shape[1]; 1842 let channel = workgroup_index % uniforms.x_shape[1]; 1843 let hight = uniforms.x_shape[2]; 1844 // initialize workgroup memory 1845 var sum = ${p}(0); 1846 var squared_sum = ${p}(0); 1847 for (var h = local_idx; h < hight; h += workgroup_size) { 1848 let value = ${p}(${I.get("batch","channel","h")}); 1849 sum += value; 1850 squared_sum += value * value; 1851 } 1852 workgroup_shared[local_idx] = ${h}(sum, squared_sum); 1853 workgroupBarrier(); 1854 1855 for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { 1856 if (local_idx < currSize) { 1857 workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize]; 1858 } 1859 workgroupBarrier(); 1860 } 1861 if (local_idx == 0) { 1862 let sum_final = ${ot("workgroup_shared[0][0]",l)} / f32(hight * ${l}); 1863 let squared_sum_final = ${ot("workgroup_shared[0][1]",l)} / f32(hight * ${l}); 1864 1865 let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${u})); 1866 let channel_scale = inv_std_dev * f32(scale[channel]); 1867 let channel_shift = f32(bias[channel]) - sum_final * channel_scale; 1868 output[workgroup_index] = vec2f(channel_scale, channel_shift); 1869 } 1870 }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${l};${u};${g}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:_,dataType:1}],dispatchGroup:{x:f},programUniforms:S}),getShaderSource:x},{inputs:[t,r,i],outputs:[-1]})[0]},kd=(e,t,r)=>{let i=t[0].dims,a=i,s=2,n=i[0],u=i[1],l=O.sizeFromDimension(i,s),p=ve(l),h=O.size(a)/p,f=ma(e,t[0],t[1],t[2],n,l,u,r.epsilon),g=[n,u,l/p],y=[n,u],_=["type","none"],w=S=>{let x=M("x",t[0].dataType,g.length,p),b=M("scale_shift",1,y.length,2),I=K("output",t[0].dataType,g.length,p),T=[x,b,I];return` 1871 ${S.registerUniform("output_size","u32").declareVariables(...T)} 1872 ${S.mainStart()} 1873 ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 1874 let outputIndices = ${I.offsetToIndices("global_idx")}; 1875 let batch = outputIndices[0]; 1876 let channel = outputIndices[1]; 1877 let scale_shift = ${b.getByIndices("vec2<u32>(batch, channel)")}; 1878 let value = ${x.getByOffset("global_idx")} * ${I.type.value}(scale_shift.x) + ${I.type.value}(scale_shift.y); 1879 ${I.setByOffset("global_idx","value")}; 1880 }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${p}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:a,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:[{type:12,data:h},...Y(g,y,g)]}),getShaderSource:w},{inputs:[t[0],f]})},Td=(e,t,r)=>{let i=t[0].dims,a=i,s=i[0],n=i[i.length-1],u=O.sizeFromDimension(i,1)/n,l=ve(n),p=O.size(a)/l,h=[{type:12,data:u},{type:12,data:Math.floor(n/l)}],f=["type","type"],g=!1,y=[0,i.length-1];for(let x=0;x<i.length-2;x++)g=g||i[x+1]!==1,y.push(x+1);g=g&&i[i.length-1]!==1;let _=g?e.compute(Me(e.inputs[0],y),{inputs:[e.inputs[0]],outputs:[-1]})[0]:e.inputs[0].reshape(Array.from({length:i.length},(x,b)=>i[y[b]])),w=ma(e,_,t[1],t[2],s,u,n,r.epsilon),S=x=>{let b=ke(t[0].dataType),I=l===1?"vec2f":`mat${l}x2f`,T=A=>{let v=A===0?"x":"y",N=l===1?"f32":`vec${l}f`;switch(l){case 1:return`${b}(${N}(scale.${v}))`;case 2:return`vec2<${b}>(${N}(scale[0].${v}, scale[1].${v}))`;case 4:return`vec4<${b}>(${N}(scale[0].${v}, scale[1].${v}, scale[2].${v}, scale[3].${v}))`;default:throw new Error(`Not supported compoents ${l}`)}},E=M("input",t[0].dataType,t[0].dims,l),C=K("output",t[0].dataType,a,l);return` 1881 @group(0) @binding(0) var<storage, read> input : array<${E.type.storage}>; 1882 @group(0) @binding(1) var<storage, read> scale_input : array<${I}>; 1883 @group(0) @binding(2) var<storage, read_write> output : array<${C.type.storage}>; 1884 struct Uniforms {H: u32, C : u32}; 1885 @group(0) @binding(3) var<uniform> uniforms: Uniforms; 1886 1887 ${x.mainStart()} 1888 let current_image_number = global_idx / (uniforms.C * uniforms.H); 1889 let current_channel_number = global_idx % uniforms.C; 1890 1891 let scale_offset = current_image_number * uniforms.C + current_channel_number; 1892 let scale = scale_input[scale_offset]; 1893 output[global_idx] = fma(input[global_idx], ${T(0)}, ${T(1)}); 1894 }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${l}`,inputDependencies:f},getRunData:()=>({outputs:[{dims:a,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:S},{inputs:[t[0],w]})},Id=(e,t)=>{t.format==="NHWC"?Td(e,e.inputs,t):kd(e,e.inputs,t)}}),Ed,zd,Cd,wm=q(()=>{te(),re(),ae(),Ed=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},zd=(e,t,r)=>{let i=t.simplified,a=e[0].dims,s=e[1],n=!i&&e[2],u=a,l=O.normalizeAxis(t.axis,a.length),p=O.sizeToDimension(a,l),h=O.sizeFromDimension(a,l),f=O.size(s.dims),g=n?O.size(n.dims):0;if(f!==h||n&&g!==h)throw new Error(`Size of X.shape()[axis:] == ${h}. 1895 Size of scale and bias (if provided) must match this. 1896 Got scale size of ${f} and bias size of ${g}`);let y=[];for(let E=0;E<a.length;++E)E<l?y.push(a[E]):y.push(1);let _=ve(h),w=["type","type"],S=[{type:12,data:p},{type:1,data:h},{type:12,data:Math.floor(h/_)},{type:1,data:t.epsilon}];n&&w.push("type");let x=r>1,b=r>2,I=E=>{let C=ke(e[0].dataType),A=[M("x",e[0].dataType,e[0].dims,_),M("scale",s.dataType,s.dims,_)];n&&A.push(M("bias",n.dataType,n.dims,_)),A.push(K("output",e[0].dataType,u,_)),x&&A.push(K("mean_data_output",1,y)),b&&A.push(K("inv_std_output",1,y));let v=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` 1897 ${E.registerUniforms(v).declareVariables(...A)} 1898 ${E.mainStart()} 1899 ${E.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} 1900 let offset = global_idx * uniforms.norm_size_vectorized; 1901 var mean_vector = ${Ni("f32",_)}; 1902 var mean_square_vector = ${Ni("f32",_)}; 1903 1904 for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { 1905 let value = ${Wt(C,_,"x[h + offset]")}; 1906 mean_vector += value; 1907 mean_square_vector += value * value; 1908 } 1909 let mean = ${ot("mean_vector",_)} / uniforms.norm_size; 1910 let inv_std_dev = inverseSqrt(${ot("mean_square_vector",_)} / uniforms.norm_size ${i?"":"- mean * mean"} + uniforms.epsilon); 1911 1912 for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { 1913 let f32input = ${Wt(C,_,"x[j + offset]")}; 1914 let f32scale = ${Wt(C,_,"scale[j]")}; 1915 output[j + offset] = ${A[0].type.value}((f32input ${i?"":"- mean"}) * inv_std_dev * f32scale 1916 ${n?`+ ${Wt(C,_,"bias[j]")}`:""} 1917 ); 1918 } 1919 1920 ${x?"mean_data_output[global_idx] = mean":""}; 1921 ${b?"inv_std_output[global_idx] = inv_std_dev":""}; 1922 }`},T=[{dims:u,dataType:e[0].dataType}];return x&&T.push({dims:y,dataType:1}),b&&T.push({dims:y,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${_};${r};${i}`,inputDependencies:w},getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(p/64)},programUniforms:S}),getShaderSource:I}},Cd=(e,t)=>{Ed(e.inputs),e.compute(zd(e.inputs,t,e.outputCount))}}),Ad,Od,bm=q(()=>{re(),Yi(),ta(),Ad=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},Od=e=>{Ad(e.inputs);let t=Ut.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],i=e.inputs[0].dims[e.inputs[0].dims.length-1];if(r<8&&i<8)e.compute(Zi(e.inputs,{activation:""},t));else{let a=t[t.length-2],s=O.size(e.inputs[0].dims.slice(0,-2)),n=O.size(e.inputs[1].dims.slice(0,-2));if(s!==1&&a===1&&n===1){let u=e.inputs[0].reshape([1,s,i]),l=e.inputs[1].reshape([1,i,r]),p=[1,s,r],h=[u,l];e.compute(Cr(h,{activation:""},t,p),{inputs:h})}else e.compute(Cr(e.inputs,{activation:""},t))}}}),Rd,Bd,Md,Nd,Dd,$m=q(()=>{te(),re(),xe(),ae(),Rd=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],i=r.dims.length;if(r.dims[i-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let a=Math.floor((t.k+t.blockSize-1)/t.blockSize),s=t.blockSize/8*t.bits,n=e[1];if(!O.areEqual(n.dims,[t.n,a,s]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let u=e[2].dims;if(O.size(u)!==t.n*a)throw new Error("scales input size error.");if(e.length===4){let l=e[3].dims,p=t.n*(t.bits===8?a:Math.floor((a*t.bits+7)/8));if(O.size(l)!==p)throw new Error("zeroPoints input size error.")}},Bd=(e,t)=>{let r=e[0].dims,i=r.length,a=r[i-2],s=t.k,n=t.n,u=r.slice(0,i-2),l=O.size(u),p=e[1].dims[2]/4,h=e[0].dataType,f=ve(t.k),g=ve(p),y=ve(n),_=u.concat([a,n]),w=a>1&&n/y%2===0?2:1,S=O.size(_)/y/w,x=64,b=[],I=[l,a,s/f],T=O.convertShape(e[1].dims).slice();T.splice(-1,1,p/g),b.push(...Y(I)),b.push(...Y(T)),b.push(...Y(e[2].dims)),e.length===4&&b.push(...Y(O.convertShape(e[3].dims)));let E=[l,a,n/y];b.push(...Y(E));let C=A=>{let v=I.length,N=M("a",e[0].dataType,v,f),D=M("b",12,T.length,g),H=M("scales",e[2].dataType,e[2].dims.length),G=[N,D,H],Q=e.length===4?M("zero_points",12,e[3].dims.length):void 0;Q&&G.push(Q);let R=E.length,j=K("output",e[0].dataType,R,y),F=ke(e[0].dataType),X=(()=>{switch(f){case 1:return`array<${F}, 8>`;case 2:return`mat4x2<${F}>`;case 4:return`mat2x4<${F}>`;default:throw new Error(`${f}-component is not supported.`)}})(),le=()=>{let P=` 1923 // reuse a data 1924 var input_offset = ${N.indicesToOffset(`${N.type.indices}(batch, row, word_offset)`)}; 1925 var a_data: ${X}; 1926 for (var j: u32 = 0; j < ${8/f}; j++) { 1927 a_data[j] = ${N.getByOffset("input_offset")}; 1928 input_offset++; 1929 } 1930 `;for(let U=0;U<y*w;U++)P+=` 1931 b_value = ${g===1?`b${U}_data`:`b${U}_data[i]`}; 1932 b_value_lower = unpack4xU8(b_value & b_mask); 1933 b_value_upper = unpack4xU8((b_value >> 4) & b_mask); 1934 b_quantized_values = ${X}(${Array.from({length:4},(J,ee)=>`${F}(b_value_lower[${ee}]), ${F}(b_value_upper[${ee}])`).join(", ")}); 1935 b_dequantized_values = ${f===1?`${X}(${Array.from({length:8},(J,ee)=>`(b_quantized_values[${ee}] - ${Q?`zero_point${U}`:"zero_point"}) * scale${U}`).join(", ")});`:`(b_quantized_values - ${X}(${Array(8).fill(`${Q?`zero_point${U}`:"zero_point"}`).join(",")})) * scale${U};`}; 1936 workgroup_shared[local_id.x * ${w} + ${Math.floor(U/y)}]${y>1?`[${U%y}]`:""} += ${Array.from({length:8/f},(J,ee)=>`${f===1?`a_data[${ee}] * b_dequantized_values[${ee}]`:`dot(a_data[${ee}], b_dequantized_values[${ee}])`}`).join(" + ")}; 1937 `;return P},W=()=>{let P=` 1938 var col_index = col * ${y}; 1939 ${Q?` 1940 let zero_point_bytes_per_col = (nBlocksPerCol + 1) / 2; 1941 var zero_point_byte_count: u32; 1942 var zero_point_word_index: u32; 1943 var zero_point_byte_offset: u32; 1944 let zero_point_nibble_offset: u32 = block & 0x1u; 1945 var zero_point_bits_offset: u32; 1946 var zero_point_word: u32;`:` 1947 // The default zero point is 8 for unsigned 4-bit quantization. 1948 let zero_point = ${F}(8);`} 1949 `;for(let U=0;U<y*w;U++)P+=` 1950 let scale${U} = ${H.getByOffset("col_index * nBlocksPerCol + block")}; 1951 ${Q?` 1952 zero_point_byte_count = col_index * zero_point_bytes_per_col + (block >> 0x1u); 1953 zero_point_word_index = zero_point_byte_count >> 0x2u; 1954 zero_point_byte_offset = zero_point_byte_count & 0x3u; 1955 zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2); 1956 zero_point_word = ${Q.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; 1957 let zero_point${U} = ${F}((zero_point_word) & 0xFu);`:""} 1958 col_index += 1;`;return P},me=()=>{let P=`col_index = col * ${y};`;for(let U=0;U<y*w;U++)P+=` 1959 let b${U}_data = ${D.getByIndices(`${D.type.indices}(col_index, block, word)`)}; 1960 col_index += 1;`;return P+=` 1961 var b_value: u32; 1962 let b_mask: u32 = 0x0F0F0F0Fu; 1963 var b_value_lower: vec4<u32>; 1964 var b_value_upper: vec4<u32>; 1965 var b_quantized_values: ${X}; 1966 var b_dequantized_values: ${X};`,P};return` 1967 var<workgroup> workgroup_shared: array<${j.type.value}, ${w*x}>; 1968 ${A.declareVariables(...G,j)} 1969 ${A.mainStart([x,1,1])} 1970 let output_indices = ${j.offsetToIndices(`(global_idx / ${x}) * ${w}`)}; 1971 let col = output_indices[2]; 1972 let row = output_indices[1];
1973 let batch = output_indices[0]; 1974 let nBlocksPerCol = uniforms.b_shape[1]; 1975 1976 for (var block = local_id.x; block < nBlocksPerCol; block += ${x}) { 1977 //process one block 1978 var word_offset: u32 = block * ${t.blockSize/f}; 1979 ${W()} 1980 for (var word: u32 = 0; word < ${p}; word += ${g}) { 1981 ${me()} 1982 for (var i: u32 = 0; i < ${g}; i++) { 1983 ${le()} 1984 word_offset += ${8/f}; 1985 } 1986 } 1987 } 1988 workgroupBarrier(); 1989 1990 if (local_id.x < ${w}) { 1991 var output_value: ${j.type.value} = ${j.type.value}(0); 1992 var workgroup_shared_offset: u32 = local_id.x; 1993 for (var b: u32 = 0u; b < ${x}u; b++) { 1994 output_value += workgroup_shared[workgroup_shared_offset]; 1995 workgroup_shared_offset += ${w}; 1996 } 1997 ${j.setByIndices(`${j.type.indices}(batch, row, col + local_id.x)`,"output_value")}; 1998 } 1999 }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${f};${g};${y};${w};${x}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:_,dataType:h}],dispatchGroup:{x:S},programUniforms:b}),getShaderSource:C}},Md=(e,t)=>{let r=e[0].dims,i=r.length,a=r[i-2],s=t.k,n=t.n,u=r.slice(0,i-2),l=O.size(u),p=e[1].dims[2]/4,h=e[0].dataType,f=ve(t.k),g=ve(p),y=u.concat([a,n]),_=128,w=n%8===0?8:n%4===0?4:1,S=_/w,x=S*g*8,b=x/f,I=x/t.blockSize,T=O.size(y)/w,E=[],C=[l,a,s/f],A=O.convertShape(e[1].dims).slice();A.splice(-1,1,p/g),E.push(...Y(C)),E.push(...Y(A)),E.push(...Y(e[2].dims)),e.length===4&&E.push(...Y(O.convertShape(e[3].dims)));let v=[l,a,n];E.push(...Y(v));let N=D=>{let H=C.length,G=M("a",e[0].dataType,H,f),Q=M("b",12,A.length,g),R=M("scales",e[2].dataType,e[2].dims.length),j=[G,Q,R],F=e.length===4?M("zero_points",12,e[3].dims.length):void 0;F&&j.push(F);let X=v.length,le=K("output",e[0].dataType,X),W=ke(e[0].dataType),me=()=>{switch(f){case 1:return` 2000 let a_data0 = vec4<${W}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); 2001 let a_data1 = vec4<${W}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` 2002 let a_data0 = vec4<${W}>(sub_a[word_offset], sub_a[word_offset + 1]); 2003 let a_data1 = vec4<${W}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` 2004 let a_data0 = sub_a[word_offset]; 2005 let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${f}-component is not supported.`)}};return` 2006 var<workgroup> sub_a: array<${G.type.value}, ${b}>; 2007 var<workgroup> inter_results: array<array<${le.type.value}, ${S}>, ${w}>; 2008 ${D.declareVariables(...j,le)} 2009 ${D.mainStart([S,w,1])} 2010 let output_indices = ${le.offsetToIndices(`workgroup_index * ${w}`)}; 2011 let col = output_indices[2]; 2012 let row = output_indices[1]; 2013 let batch = output_indices[0]; 2014 let n_blocks_per_col = uniforms.b_shape[1]; 2015 let num_tiles = (n_blocks_per_col - 1) / ${I} + 1; 2016 2017 // Loop over shared dimension. 2018 for (var tile: u32 = 0; tile < num_tiles; tile += 1) { 2019 let a_col_start = tile * ${b}; 2020 // load one tile A data into shared memory. 2021 for (var a_offset = local_idx; a_offset < ${b}; a_offset += ${_}) 2022 { 2023 let a_col = a_col_start + a_offset; 2024 if (a_col < uniforms.a_shape[2]) 2025 { 2026 sub_a[a_offset] = ${G.getByIndices(`${G.type.indices}(batch, row, a_col)`)}; 2027 } else { 2028 sub_a[a_offset] = ${G.type.value}(0); 2029 } 2030 } 2031 workgroupBarrier(); 2032 2033 // each thread process one block 2034 let b_row = col + local_id.y; 2035 let block = tile * ${I} + local_id.x; 2036 ${F?` 2037 let zero_point_bytes_per_col = (n_blocks_per_col + 1) / 2; 2038 let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block >> 0x1u); 2039 let zero_point_word_index = zero_point_byte_count >> 0x2u; 2040 let zero_point_byte_offset = zero_point_byte_count & 0x3u; 2041 let zero_point_nibble_offset: u32 = block & 0x1u; 2042 let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2); 2043 let zero_point_word = ${F.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; 2044 let zero_point = ${W}((zero_point_word) & 0xFu);`:` 2045 // The default zero point is 8 for unsigned 4-bit quantization. 2046 let zero_point = ${W}(8);`} 2047 let scale = ${R.getByOffset("b_row * n_blocks_per_col + block")}; 2048 let b_data = ${Q.getByIndices(`${Q.type.indices}(b_row, block, 0)`)}; 2049 var word_offset = local_id.x * ${t.blockSize/f}; 2050 for (var i: u32 = 0; i < ${g}; i++) { 2051 ${me()} 2052 let b_value = ${g===1?"b_data":"b_data[i]"}; 2053 let b_value_lower = unpack4xU8(b_value & 0x0F0F0F0Fu); 2054 let b_value_upper = unpack4xU8((b_value >> 4) & 0x0F0F0F0Fu); 2055 let b_quantized_values = mat2x4<${W}>(${Array.from({length:4},(P,U)=>`${W}(b_value_lower[${U}]), ${W}(b_value_upper[${U}])`).join(", ")}); 2056 let b_dequantized_values = (b_quantized_values - mat2x4<${W}>(${Array(8).fill("zero_point").join(",")})) * scale; 2057 inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(P,U)=>`${`dot(a_data${U}, b_dequantized_values[${U}])`}`).join(" + ")}; 2058 word_offset += ${8/f}; 2059 } 2060 workgroupBarrier(); 2061 } 2062 2063 if (local_idx < ${w}) { 2064 var output_value: ${le.type.value} = ${le.type.value}(0); 2065 for (var b = 0u; b < ${S}; b++) { 2066 output_value += inter_results[local_idx][b]; 2067 } 2068 if (col + local_idx < uniforms.output_shape[2]) 2069 { 2070 ${le.setByIndices(`${le.type.indices}
vendor: 10,239 bytes, lines 2070-2216
2070(batch, row, col + local_idx)`,"output_value")} 2071 } 2072 } 2073 }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${f};${g};${S};${w}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:y,dataType:h}],dispatchGroup:{x:T},programUniforms:E}),getShaderSource:N}},Nd=(e,t)=>{Rd(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Md(e.inputs,t)):e.compute(Bd(e.inputs,t))},Dd=e=>he(e)}),Pd,Ud,qd,Wd,Ld,Vd,Gd,Hd,Fd,vm=q(()=>{te(),re(),ae(),Pd=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},Ud=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=` 2074 k = i32(${e.indicesGet("indices",a)}) - ${Z("uniforms.pads",a,r)}; 2075 if (k < 0) { 2076 break; 2077 } 2078 if (k >= i32(${Z("uniforms.x_shape",a,t)})) { 2079 break; 2080 } 2081 offset += k * i32(${Z("uniforms.x_strides",a,t)}); 2082 `;return` 2083 value = ${e.type.value}(uniforms.constant_value); 2084 for (var i = 0; i < 1; i++) { 2085 var offset = 0; 2086 var k = 0; 2087 ${i} 2088 value = x[offset]; 2089 } 2090 `},qd=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=` 2091 k = i32(${e.indicesGet("indices",a)}) - ${Z("uniforms.pads",a,r)}; 2092 if (k < 0) { 2093 k = -k; 2094 } 2095 { 2096 let _2n_1 = 2 * (i32(${Z("uniforms.x_shape",a,t)}) - 1); 2097 k = k % _2n_1; 2098 if(k >= i32(${Z("uniforms.x_shape",a,t)})) { 2099 k = _2n_1 - k; 2100 } 2101 } 2102 offset += k * i32(${Z("uniforms.x_strides",a,t)}); 2103 `;return` 2104 var offset = 0; 2105 var k = 0; 2106 ${i} 2107 value = x[offset]; 2108 `},Wd=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=` 2109 k = i32(${e.indicesGet("indices",a)}) - ${Z("uniforms.pads",a,r)}; 2110 if (k < 0) { 2111 k = 0; 2112 } 2113 if (k >= i32(${Z("uniforms.x_shape",a,t)})) { 2114 k = i32(${Z("uniforms.x_shape",a,t)}) - 1; 2115 } 2116 offset += k * i32(${Z("uniforms.x_strides",a,t)}); 2117 `;return` 2118 var offset = 0; 2119 var k = 0; 2120 ${i} 2121 value = x[offset]; 2122 `},Ld=(e,t,r)=>{let i="";for(let a=t-1;a>=0;--a)i+=` 2123 k = i32(${e.indicesGet("indices",a)}) - ${Z("uniforms.pads",a,r)}; 2124 if (k < 0) { 2125 k += i32(${Z("uniforms.x_shape",a,t)}]); 2126 } 2127 if (k >= i32(${Z("uniforms.x_shape",a,t)})) { 2128 k -= i32(${Z("uniforms.x_shape",a,t)}); 2129 } 2130 offset += k * i32(${Z("uniforms.x_strides",a,t)}); 2131 `;return` 2132 var offset = 0; 2133 var k = 0; 2134 ${i} 2135 value = x[offset]; 2136 `},Vd=(e,t,r)=>{switch(r.mode){case 0:return Ud(e,t,r.pads.length);case 1:return qd(e,t,r.pads.length);case 2:return Wd(e,t,r.pads.length);case 3:return Ld(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Gd=(e,t)=>{let r=O.padShape(e[0].dims.slice(),t.pads),i=e[0].dims,a=O.size(r),s=[{type:12,data:a},{type:6,data:t.pads}],n=e.length>=3&&e[2].data;t.mode===0&&s.push({type:n?e[2].dataType:1,data:t.value}),s.push(...Y(e[0].dims,r));let u=["rank"],l=p=>{let h=K("output",e[0].dataType,r.length),f=M("x",e[0].dataType,i.length),g=f.type.value,y=Vd(h,i.length,t),_=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&_.push({name:"constant_value",type:n?g:"f32"}),` 2137 ${p.registerUniforms(_).declareVariables(f,h)} 2138 ${p.mainStart()} 2139 ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 2140 2141 let indices = ${h.offsetToIndices("global_idx")}; 2142 2143 var value = ${g}(0); 2144 ${y} 2145 output[global_idx] = value; 2146 }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${n}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(O.size(r)/64)},programUniforms:s}),getShaderSource:l}},Hd=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),i=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,a=e[0].dims.length,s=new Int32Array(2*a).fill(0);if(e.length>=4){let u=e[3].getBigInt64Array();for(let l=0;l<u.length;l++)s[Number(u[l])]=Number(r[l]),s[Number(u[l])+a]=Number(r[l+u.length])}else r.forEach((u,l)=>s[Number(l)]=Number(u));let n=[];return s.forEach(u=>n.push(u)),{mode:t.mode,value:i,pads:n}}else return t},Fd=(e,t)=>{Pd(e.inputs);let r=Hd(e.inputs,t);e.compute(Gd(e.inputs,r),{inputs:[0]})}}),ar,ga,ya,_a,wa,jd,Kd,ba,$a,Qd,Zd,va,Yd,Xd,xa,Jd,ep,tp,rp,xm=q(()=>{Pe(),te(),re(),ae(),ar=e=>{if(ge.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},ga=(e,t,r)=>{let i=t.format==="NHWC",a=e.dims.slice();i&&a.splice(1,0,a.pop());let s=Object.hasOwnProperty.call(t,"dilations"),n=t.kernelShape.slice(),u=t.strides.slice(),l=s?t.dilations.slice():[],p=t.pads.slice();xr.adjustPoolAttributes(r,a,n,u,l,p);let h=xr.computePoolOutputShape(r,a,u,l,n,p,t.autoPad),f=Object.assign({},t);s?Object.assign(f,{kernelShape:n,strides:u,pads:p,dilations:l,cacheKey:t.cacheKey}):Object.assign(f,{kernelShape:n,strides:u,pads:p,cacheKey:t.cacheKey});let g=h.slice();return g.push(g.splice(1,1)[0]),[f,i?g:h]},ya=(e,t)=>{let r=t.format==="NHWC",i=O.size(e),a=O.size(t.kernelShape),s=[{type:12,data:i},{type:12,data:a}],n=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let u=t.kernelShape[t.kernelShape.length-1],l=t.strides[t.strides.length-1],p=t.pads[t.pads.length/2-1],h=t.pads[t.pads.length-1],f=!!(p+h);s.push({type:12,data:u},{type:12,data:l},{type:12,data:p},{type:12,data:h}),n.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let g=!1;if(t.kernelShape.length===2){let y=t.kernelShape[t.kernelShape.length-2],_=t.strides[t.strides.length-2],w=t.pads[t.pads.length/2-2],S=t.pads[t.pads.length-2];g=!!(w+S),s.push({type:12,data:y},{type:12,data:_},{type:12,data:w},{type:12,data:S}),n.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[s,n,!0,f,g]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let u=O.computeStrides(t.kernelShape);s.push({type:12,data:u},{type:12,data:t.pads},{type:12,data:t.strides}),n.push({name:"kernelStrides",type:"u32",length:u.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let l=t.pads.reduce((p,h)=>p+h);return[s,n,!!l,!1,!1]}},_a=(e,t,r,i,a,s,n,u,l,p,h,f)=>{let g=a.format==="NHWC",y=t.type.value,_=K("output",t.type.tensor,i);if(a.kernelShape.length<=2){let w="",S="",x="",b=r-(g?2:1);if(h?w=` 2147 for (var i: u32 = 0u; i < uniforms.kw; i++) { 2148 xIndices[${b}] = indices[${b}] * uniforms.sw - uniforms.pwStart + i; 2149 if (xIndices[${b}] < 0 || xIndices[${b}] 2150 >= uniforms.x_shape[${b}]) { 2151 pad++; 2152 continue; 2153 } 2154 let x_val = x[${t.indicesToOffset("xIndices")}]; 2155 ${s} 2156 }`:w=` 2157 for (var i: u32 = 0u; i < uniforms.kw; i++) { 2158 xIndices[${b}] = indices[${b}] * uniforms.sw - uniforms.pwStart + i; 2159 let x_val = x[${t.indicesToOffset("xIndices")}]; 2160 ${s} 2161 }`,a.kernelShape.length===2){let I=r-(g?3:2);f?S=` 2162 for (var j: u32 = 0u; j < uniforms.kh; j++) { 2163 xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j; 2164 if (xIndices[${I}] < 0 || xIndices[${I}] >= uniforms.x_shape[${I}]) { 2165 pad += i32(uniforms.kw); 2166 continue; 2167 } 2168 `:S=` 2169 for (var j: u32 = 0u; j < uniforms.kh; j++) { 2170 xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j; 2171 `,x=` 2172 } 2173 `}return` 2174 ${e.registerUniforms(l).declareVariables(t,_)} 2175 2176 ${e.mainStart()} 2177 ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 2178 2179 let indices = ${_.offsetToIndices("global_idx")}; 2180 var xIndices = ${_.offsetToIndices("global_idx")}; 2181 2182 var value = ${y}(${u}); 2183 var pad = 0; 2184 ${S} 2185 ${w} 2186 ${x} 2187 ${n} 2188 2189 output[global_idx] = value; 2190 }`}else{if(g)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let w=a.kernelShape.length,S=a.pads.length,x="";return p?x=` 2191 if (xIndices[j] >= uniforms.x_shape[j]) { 2192 pad++; 2193 isPad = true; 2194 break; 2195 } 2196 } 2197 if (!isPad) { 2198 let x_val = x[${t.indicesToOffset("xIndices")}]; 2199 ${s} 2200 }`:x=` 2201 } 2202 let x_val = x[${t.indicesToOffset("xIndices")}]; 2203 ${s} 2204 `,` 2205 ${e.registerUniforms(l).declareVariables(t,_)} 2206 2207 ${e.mainStart()} 2208 ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 2209 let indices = ${_.offsetToIndices("global_idx")}; 2210 var xIndices = ${_.offsetToIndices("global_idx")}; 2211 2212 var offsets: array<u32, ${w}>; 2213 2214 var value = ${y}(${u}); 2215 var pad = 0; 2216 var isPad = false;
2217 2218 for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { 2219 var offset = i; 2220 for (var j = 0u; j < ${w-1}u; j++) { 2221 offsets[j] = offset / ${Z("uniforms.kernelStrides","j",w)}; 2222 offset -= offsets[j] * ${Z("uniforms.kernelStrides","j",w)}; 2223 } 2224 offsets[${w-1}] = offset; 2225 2226 isPad = false; 2227 for (var j = ${r-w}u; j < ${r}u; j++) { 2228 xIndices[j] = indices[j] * ${Z("uniforms.strides",`j - ${r-w}u`,w)} 2229 + offsets[j - ${r-w}u] - ${Z("uniforms.pads","j - 2u",S)}; 2230 ${x} 2231 } 2232 ${n} 2233 2234 output[global_idx] = value; 2235 }`}},wa=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,jd=e=>`${wa(e)};${e.countIncludePad}`,Kd=e=>`${wa(e)};${e.storageOrder};${e.dilations}`,ba=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),$a=(e,t,r,i)=>{let[a,s]=ga(t,i,r),n=M("x",t.dataType,t.dims.length),u=n.type.value,l="value += x_val;",p="";a.countIncludePad?p+=`value /= ${u}(uniforms.kernelSize);`:p+=`value /= ${u}(i32(uniforms.kernelSize) - pad);`;let[h,f,g,y,_]=ya(s,a);h.push(...Y(t.dims,s));let w=["rank"];return{name:e,shaderCache:{hint:`${i.cacheKey};${g};${y};${_}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(O.size(s)/64)},programUniforms:h}),getShaderSource:S=>_a(S,n,t.dims.length,s.length,a,l,p,0,f,g,y,_)}},Qd=e=>{let t=e.count_include_pad!==0,r=ba(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let i={countIncludePad:t,...r,cacheKey:""};return{...i,cacheKey:jd(i)}},Zd=(e,t)=>{ar(e.inputs),e.compute($a("AveragePool",e.inputs[0],!1,t))},va={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},Yd=e=>{let t=e.format;return{format:t,...va,cacheKey:t}},Xd=(e,t)=>{ar(e.inputs),e.compute($a("GlobalAveragePool",e.inputs[0],!0,t))},xa=(e,t,r,i)=>{let[a,s]=ga(t,i,r),n=` 2236 value = max(x_val, value); 2237 `,u="",l=M("x",t.dataType,t.dims.length),p=["rank"],[h,f,g,y,_]=ya(s,a);return h.push(...Y(t.dims,s)),{name:e,shaderCache:{hint:`${i.cacheKey};${g};${y};${_}`,inputDependencies:p},getRunData:()=>({outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(O.size(s)/64)},programUniforms:h}),getShaderSource:w=>_a(w,l,t.dims.length,s.length,a,n,u,t.dataType===10?-65504:-1e5,f,g,y,_)}},Jd=(e,t)=>{ar(e.inputs),e.compute(xa("MaxPool",e.inputs[0],!1,t))},ep=e=>{let t=e.storage_order,r=e.dilations,i=ba(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(i.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let a={storageOrder:t,dilations:r,...i,cacheKey:""};return{...a,cacheKey:Kd(a)}},tp=e=>{let t=e.format;return{format:t,...va,cacheKey:t}},rp=(e,t)=>{ar(e.inputs),e.compute(xa("GlobalMaxPool",e.inputs[0],!0,t))}}),ip,ap,np,sp,Sm=q(()=>{te(),re(),xe(),ae(),ip=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[0].dataType===6&&e.length>2)throw new Error("In the case of dequantizing int32 there is no zero point.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((r,i)=>r===e[2].dims[i]).reduce((r,i)=>r&&i,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");
vendor: 18,936 bytes, lines 2237-2434
2237if(!e[1].dims.map((a,s)=>s===t.axis||a===e[0].dims[s]).reduce((a,s)=>a&&s,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let r=e[0].dims[t.axis],i=e[1].dims[t.axis];if(t.blockSize<Math.ceil(r/i)||t.blockSize>Math.ceil(r/(i-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},ap=(e,t)=>{let r=O.normalizeAxis(t.axis,e[0].dims.length),i=e[0].dataType,a=i===3,s=e[0].dims,n=e[1].dataType,u=O.size(s),l=i===3||i===2,p=l?[Math.ceil(O.size(e[0].dims)/4)]:e[0].dims,h=e[1].dims,f=e.length>2?e[2]:void 0,g=f?l?[Math.ceil(O.size(f.dims)/4)]:f.dims:void 0,y=h.length===0||h.length===1&&h[0]===1,_=y===!1&&h.length===1,w=ve(u),S=y&&(!l||w===4),x=S?w:1,b=S&&!l?w:1,I=M("input",l?12:i,p.length,b),T=M("scale",n,h.length),E=f?M("zero_point",l?12:i,g.length):void 0,C=K("output",n,s.length,x),A=[I,T];E&&A.push(E);let v=[p,h];f&&v.push(g);let N=[{type:12,data:u/x},{type:12,data:r},{type:12,data:t.blockSize},...Y(...v,s)],D=H=>{let G=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` 2238 ${H.registerUniforms(G).declareVariables(...A,C)} 2239 ${H.mainStart()} 2240 ${H.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 2241 let output_indices = ${C.offsetToIndices("global_idx")}; 2242 2243 // Set input x 2244 ${l?` 2245 let input = ${I.getByOffset("global_idx / 4")}; 2246 let x_vec = ${a?"unpack4xI8(input)":"unpack4xU8(input)"}; 2247 let x_value = ${x===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${I.getByOffset("global_idx")};`}; 2248 2249 // Set scale input 2250 ${y?`let scale_value= ${T.getByOffset("0")}`:_?` 2251 let scale_index = ${C.indicesGet("output_indices","uniforms.axis")}; 2252 let scale_value= ${T.getByOffset("scale_index")};`:` 2253 var scale_indices: ${T.type.indices} = output_indices; 2254 let index = ${T.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size; 2255 ${T.indicesSet("scale_indices","uniforms.axis","index")}; 2256 let scale_value= ${T.getByIndices("scale_indices")};`}; 2257 2258 // Set zero-point input 2259 ${E?y?l?` 2260 let zero_point_input = ${E.getByOffset("0")}; 2261 let zero_point_vec = ${a?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; 2262 let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${E.getByOffset("0")}`:_?l?` 2263 let zero_point_index = ${C.indicesGet("output_indices","uniforms.axis")}; 2264 let zero_point_input = ${E.getByOffset("zero_point_index / 4")}; 2265 let zero_point_vec = ${a?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; 2266 let zero_point_value = zero_point_vec[zero_point_index % 4]`:` 2267 let zero_point_index = ${C.indicesGet("output_indices","uniforms.axis")}; 2268 let zero_point_value = ${E.getByOffset("zero_point_index")};`:l?` 2269 let zero_point_offset = ${T.indicesToOffset("scale_indices")}; 2270 let zero_point_input = ${E.getByOffset("zero_point_offset / 4")}; 2271 let zero_point_vec = ${a?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; 2272 let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${E.getByIndices("scale_indices")};`:`let zero_point_value = ${l?a?"i32":"u32":I.type.value}(0);`}; 2273 // Compute and write output 2274 ${C.setByOffset("global_idx",`${C.type.value}(x_value - zero_point_value) * scale_value`)}; 2275 }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:E?["rank","rank","rank"]:["rank","rank"]},getShaderSource:D,getRunData:()=>({outputs:[{dims:s,dataType:n}],dispatchGroup:{x:Math.ceil(u/x/64),y:1,z:1},programUniforms:N})}},np=(e,t)=>{ip(e.inputs,t),e.compute(ap(e.inputs,t))},sp=e=>he({axis:e.axis,blockSize:e.blockSize})}),op,up,lp,km=q(()=>{Pe(),te(),ae(),op=(e,t,r)=>{let i=e===t,a=e<t&&r<0,s=e>t&&r>0;if(i||a||s)throw new Error("Range these inputs' contents are invalid.")},up=(e,t,r,i)=>{let a=Math.abs(Math.ceil((t-e)/r)),s=[a],n=a,u=[{type:12,data:n},{type:i,data:e},{type:i,data:r},...Y(s)],l=p=>{let h=K("output",i,s.length),f=h.type.value,g=[{name:"outputSize",type:"u32"},{name:"start",type:f},{name:"delta",type:f}];return` 2276 ${p.registerUniforms(g).declareVariables(h)} 2277 ${p.mainStart()} 2278 ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 2279 output[global_idx] = uniforms.start + ${f}(global_idx) * uniforms.delta; 2280 }`};return{name:"Range",shaderCache:{hint:`${i}`},getShaderSource:l,getRunData:()=>({outputs:[{dims:s,dataType:i}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:u})}},lp=e=>{let t=0,r=0,i=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],i=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],i=e.inputs[2].getFloat32Array()[0]),ge.webgpu.validateInputContent&&op(t,r,i),e.compute(up(t,r,i,e.inputs[0].dataType),{inputs:[]})}}),dp,pp,cp,hp,Tm=q(()=>{te(),re(),xe(),ae(),dp=(e,t,r,i)=>{if(e!=="none"&&i!=="i32"&&i!=="u32"&&i!=="f32")throw new Error(`Input ${i} is not supported with reduction ${e}.`);let a=`{ 2281 var oldValue = 0; 2282 loop { 2283 let newValueF32 =`,s=`; 2284 let newValue = bitcast<i32>(newValueF32); 2285 let res = atomicCompareExchangeWeak(&${t}, oldValue, newValue); 2286 if res.exchanged { 2287 break; 2288 } 2289 oldValue = res.old_value; 2290 } 2291 }`;switch(e){case"none":return`${t}=${r};`;case"add":return i==="i32"||i==="u32"?`atomicAdd(&${t}, bitcast<${i}>(${r}));`:` 2292 ${a}bitcast<${i}>(oldValue) + (${r})${s}`;case"max":return i==="i32"||i==="u32"?`atomicMax(&${t}, bitcast<${i}>(${r}));`:` 2293 ${a}max(bitcast<f32>(oldValue), (${r}))${s}`;case"min":return i==="i32"||i==="u32"?`atomicMin(&${t}, bitcast<${i}>(${r}));`:`${a}min(bitcast<${i}>(oldValue), (${r}))${s}`;case"mul":return`${a}(bitcast<${i}>(oldValue) * (${r}))${s}`;default:throw new Error(`Reduction ${e} is not supported.`)}},pp=(e,t)=>{let r=e[0].dims,i=e[1].dims,a=r,s=1,n=Math.ceil(O.sizeToDimension(i,i.length-1)/s),u=i[i.length-1],l=O.sizeFromDimension(r,u),p=[{type:12,data:n},{type:12,data:u},{type:12,data:l},...Y(e[1].dims,e[2].dims,a)],h=f=>{let g=M("indices",e[1].dataType,e[1].dims.length),y=M("updates",e[2].dataType,e[2].dims.length,s),_=t.reduction!=="none"&&t.reduction!==""?_s("output",e[0].dataType,a.length):K("output",e[0].dataType,a.length,s);return` 2294 ${f.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(g,y,_)} 2295 ${f.mainStart()} 2296 ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 2297 var data_offset = 0u; 2298 let indices_start = uniforms.last_index_dimension * global_idx; 2299 let indices_end = indices_start + uniforms.last_index_dimension; 2300 for (var i = indices_start; i < indices_end; i++) { 2301 var index = i32(indices[i].x); 2302 ${e[0].dims.length===1?` 2303 let element_count_dim = uniforms.output_strides; 2304 let dim_value = uniforms.output_shape;`:` 2305 let element_count_dim = uniforms.output_strides[i - indices_start]; 2306 let dim_value = uniforms.output_shape[i - indices_start];`} 2307 if (index >= 0) { 2308 if (index >= i32(dim_value)) { 2309 index = i32(dim_value - 1); 2310 } 2311 } else { 2312 if (index < -i32(dim_value)) { 2313 index = 0; 2314 } else { 2315 index += i32(dim_value); 2316 } 2317 } 2318 data_offset += u32((u32(index) * element_count_dim)); 2319 } 2320 2321 for (var i = 0u; i < uniforms.num_updates_elements; i++) { 2322 let value = updates[uniforms.num_updates_elements * global_idx + i]; 2323 ${dp(t.reduction,"output[data_offset + i]","value",_.type.value)} 2324 } 2325 2326 }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:p}),getShaderSource:h}},cp=e=>he({reduction:e.reduction}),hp=(e,t)=>{e.compute(pp(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),fp,mp,gp,Sa,yp,_p,wp,bp,$p,vp,xp,Sp,ka,kp,Tp,Ip,Ep,zp,Cp,Ap,Im=q(()=>{te(),re(),xe(),ae(),fp=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and 2327 one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},mp=(e,t,r)=>{t.every(a=>a>=0&&a<r||(()=>{throw new Error("Resize requires axes input values to be positive and less than rank")}));let i=new Array(r).fill(1);return t.forEach((a,s)=>i[a]=e[s]),i},gp=(e,t,r,i,a,s)=>{let[n,u,l]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],p=e[0].dims.length;if(n>0&&e.length>n&&e[n].dims.length>0)e[n].getFloat32Array().forEach(h=>s.push(h));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(u>0&&e.length>u&&e[u].dims.length===1&&e[u].dims[0]>0){if(e[u].getFloat32Array().forEach(h=>i.push(h)),i.length!==0&&i.length!==p&&r>=18&&i.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");fp(i,t),t.axes.length>0&&mp(i,t.axes,p).forEach((h,f)=>i[f]=h)}if(l>0&&e.length>l&&e[l].dims.length===1&&e[l].dims[0]>0&&(e[l].getBigInt64Array().forEach(h=>a.push(Number(h))),a.length!==0&&a.length!==p&&r>=18&&a.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(i.length!==0&&i.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(a.length!==0&&a.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof i<"u"&&typeof a<"u"&&i.length>0&&a.length>p)throw new Error("Resize requires only of scales or sizes to be specified")},Sa=(e,t,r,i)=>` 2328 // The whole part and the fractional part are calculated separately due to inaccuracy of floating 2329 // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an 2330 // offset-by-one error later in floor(). 2331 let big = (${e}) * (${t}); 2332 let whole = ${i}(big / (${r})); 2333 let fract = ${i}(big % (${r})) / ${i}(${r}); 2334 return whole + fract; 2335`,yp=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, 2336 lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return` 2337 if (xScale < 1.0 || floor(xScale) != xScale) { 2338 return ${t}(xResized) / ${t}(xScale); 2339 } else { 2340 ${Sa("xResized","lengthOriginal","lengthResized",t)} 2341 } 2342 `;case"pytorch_half_pixel":return`if (lengthResized > 1) { 2343 return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5; 2344 } else { 2345 return 0.0; 2346 }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) { 2347 return 0.0; 2348 } else { 2349 ${Sa("xResized","lengthOriginal - 1","lengthResized - 1",t)} 2350 }`;case"tf_crop_and_resize":return`if (lengthResized > 1) { 2351 return ${t}(roiStart) * ${t}(lengthOriginal - 1) + 2352 (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) / 2353 ${t}(lengthResized - 1); 2354 } else { 2355 return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1); 2356 }`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized); 2357 const adjustment = ${t}(lengthResized) / outputWidth; 2358 const center = ${t}(lengthOriginal) / 2; 2359 const offset = center * (1 - adjustment); 2360 return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",_p=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",wp=(e,t,r)=>{let i=new Array(r).fill(0).concat(new Array(r).fill(1)),a=e.length===0?i:e.slice();return t.length>0?(t.forEach((s,n)=>{i[s]=a[n],i[n+r]=a[t.length+n]}),i):a},bp=(e,t,r,i)=>{let a=[];if(r.length>0)if(i.length>0){if(e.forEach(s=>a.push(s)),Math.max(...i)>e.length)throw new Error("axes is out of bound");i.forEach((s,n)=>a[s]=r[n])}else r.forEach(s=>a.push(s));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");a=e.map((s,n)=>Math.round(s*t[n]))}return a},$p=(e,t,r)=>{let i=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(s=>t[s]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(s=>t[s]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let a=e.slice();return r.axes.length>0?(r.axes.forEach(s=>t[s]=i),r.axes.forEach(s=>a[s]=Math.round(e[s]*t[s]))):(t.fill(i,0,t.length),a.forEach((s,n)=>a[n]=Math.round(s*t[n]))),a},vp=(e,t,r,i,a)=>` 2361 fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> { 2362 var original_indices: array<${e.type.value}, ${r.length}>; 2363 for (var i:u32 = 0; i < ${r.length}; i++) { 2364 var output_index = ${e.indicesGet("output_indices","i")}; 2365 var scale = ${Z("uniforms.scales","i",i)}; 2366 var roi_low = ${Z("uniforms.roi","i",a)}; 2367 var roi_hi = ${Z("uniforms.roi",`i + ${t.length}`,a)}; 2368 if (scale == 1.0) { 2369 original_indices[i] = ${e.type.value}(output_index); 2370 } else { 2371 var input_shape_i = ${Z("uniforms.input_shape","i",t.length)}; 2372 var output_shape_i = ${Z("uniforms.output_shape","i",r.length)}; 2373 original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, 2374 input_shape_i, roi_low, roi_hi); 2375 } 2376 } 2377 return original_indices; 2378 }`,xp=(e,t,r,i,a,s,n)=>` 2379 fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { 2380 var input_indices: ${e.type.indices}; 2381 for (var i:u32 = 0; i < ${i.length}; i++) { 2382 var output_index = ${t.indicesGet("output_indices","i")}; 2383 var input_index: u32; 2384 var scale = ${Z("uniforms.scales","i",a)}; 2385 if (scale == 1.0) { 2386 input_index = output_index; 2387 } else { 2388 var roi_low = ${Z("uniforms.roi","i",s)}; 2389 var roi_hi = ${Z("uniforms.roi",`i + ${r.length}`,s)}; 2390 var input_shape_i = ${Z("uniforms.input_shape","i",r.length)}; 2391 var output_shape_i = ${Z("uniforms.output_shape","i",i.length)}; 2392 var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, 2393 input_shape_i, roi_low, roi_hi); 2394 if (!${n} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { 2395 if (original_idx < 0) { 2396 input_index = 0; 2397 } else if (original_idx > ${t.type.value}(input_shape_i - 1)) { 2398 input_index = input_shape_i - 1; 2399 } else { 2400 input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1)); 2401 } 2402 } else { 2403 input_index = u32(original_idx); 2404 } 2405 } 2406 ${e.indicesSet("input_indices","i","input_index")} 2407 } 2408 return input_indices; 2409 }`,Sp=(e,t)=>` 2410 fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { 2411 for (var i:u32 = 0; i < ${t.length}; i++) { 2412 var input_index = ${e.indicesGet("input_indices","i")}; 2413 if (input_index < 0 || input_index >= ${Z("uniforms.input_shape","i",t.length)}) { 2414 return false; 2415 } 2416 } 2417 return true; 2418 }`,ka=(e,t,r,i)=>e.rank>i?` 2419 ${e.indicesSet("input_indices",t,"channel")}; 2420 ${e.indicesSet("input_indices",r,"batch")}; 2421`:"",kp=(e,t,r,i,a)=>{let[s,n,u,l]=r.length===2?[-1,0,1,-1]:[0,2,3,1],p=e.type.value;return` 2422 fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${p} { 2423 var input_indices: ${e.type.indices}; 2424 ${e.indicesSet("input_indices",n,`max(0, min(row, ${r[n]} - 1))`)}; 2425 ${e.indicesSet("input_indices",u,`max(0, min(col, ${r[u]} - 1))`)}; 2426 ${ka(e,l,s,2)} 2427 return ${e.getByIndices("input_indices")}; 2428 } 2429 2430 fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${p} { 2431 var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); 2432 var row:${p} = originalIndices[${n}]; 2433 var col:${p} = originalIndices[${u}]; 2434 ${i?`if (row < 0 || row > (${r[n]}
vendor: 8,925 bytes, lines 2434-2596
2434 - 1) || col < 0 || col > (${r[u]} - 1)) { 2435 return ${a}; 2436 }`:""}; 2437 row = max(0, min(row, ${r[n]} - 1)); 2438 col = max(0, min(col, ${r[u]} - 1)); 2439 var row1: u32 = u32(row); 2440 var col1: u32 = u32(col); 2441 var row2: u32 = u32(row + 1); 2442 var col2: u32 = u32(col + 1); 2443 var channel: u32 = ${r.length>2?`u32(originalIndices[${l}])`:"0"}; 2444 var batch: u32 = ${r.length>2?`u32(originalIndices[${s}])`:"0"}; 2445 var x11: ${p} = getInputValue(batch, channel, row1, col1); 2446 var x12: ${p} = getInputValue(batch, channel, row1, col2); 2447 var x21: ${p} = getInputValue(batch, channel, row2, col1); 2448 var x22: ${p} = getInputValue(batch, channel, row2, col2); 2449 var dx1: ${p} = abs(row - ${p}(row1)); 2450 var dx2: ${p} = abs(${p}(row2) - row); 2451 var dy1: ${p} = abs(col - ${p}(col1)); 2452 var dy2: ${p} = abs(${p}(col2) - col); 2453 if (row1 == row2) { 2454 dx1 = 0.5; 2455 dx2 = 0.5; 2456 } 2457 if (col1 == col2) { 2458 dy1 = 0.5; 2459 dy2 = 0.5; 2460 } 2461 return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); 2462 }`},Tp=(e,t,r,i,a,s,n,u,l,p)=>{let h=r.length===2,[f,g]=h?[0,1]:[2,3],y=e.type.value,_=w=>{let S=w===f?"row":"col";return` 2463 fn ${S}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${y} { 2464 var output_index = ${t.indicesGet("output_indices",w)}; 2465 var originalIdx: ${y} = getOriginalCoordinateFromResizedCoordinate(output_index, ${a[w]}, 2466 ${i[w]}, ${r[w]}, ${s[w]}, ${s[w]} + ${r.length}); 2467 var fractOriginalIdx: ${y} = originalIdx - floor(originalIdx); 2468 var coefs = getCubicInterpolationCoefs(fractOriginalIdx); 2469 2470 if (${u} && (originalIdx < 0 || originalIdx > (${r[w]} - 1))) { 2471 return ${l}; 2472 } 2473 var data: array<${y}, 4> = array<${y}, 4>(0.0, 0.0, 0.0, 0.0); 2474 for (var i: i32 = -1; i < 3; i++) { 2475 var ${S}: ${y} = originalIdx + ${y}(i); 2476 if (${S} < 0 || ${S} >= ${r[w]}) { 2477 ${p?`coefs[i + 1] = 0.0; 2478 continue;`:u?`return ${l};`:`${S} = max(0, min(${S}, ${r[w]} - 1));`}; 2479 } 2480 var input_indices_copy: ${e.type.indices} = input_indices; 2481 ${e.indicesSet("input_indices_copy",w,`u32(${S})`)}; 2482 data[i + 1] = ${w===f?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; 2483 } 2484 return cubicInterpolation1D(data, coefs); 2485 }`};return` 2486 ${_(f)}; 2487 ${_(g)}; 2488 fn getCubicInterpolationCoefs(s: ${y}) -> array<${y}, 4> { 2489 var absS = abs(s); 2490 var coeffs: array<${y}, 4> = array<${y}, 4>(0.0, 0.0, 0.0, 0.0); 2491 var oneMinusAbsS: ${y} = 1.0 - absS; 2492 var twoMinusAbsS: ${y} = 2.0 - absS; 2493 var onePlusAbsS: ${y} = 1.0 + absS; 2494 coeffs[0] = ((${n} * onePlusAbsS - 5 * ${n}) * onePlusAbsS + 8 * ${n}) * onePlusAbsS - 4 * ${n}; 2495 coeffs[1] = ((${n} + 2) * absS - (${n} + 3)) * absS * absS + 1; 2496 coeffs[2] = ((${n} + 2) * oneMinusAbsS - (${n} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; 2497 coeffs[3] = ((${n} * twoMinusAbsS - 5 * ${n}) * twoMinusAbsS + 8 * ${n}) * twoMinusAbsS - 4 * ${n}; 2498 return coeffs; 2499 } 2500 2501 fn cubicInterpolation1D(x: array<${y}, 4>, coefs: array<${y}, 4>) -> ${y} { 2502 var coefsSum: ${y} = coefs[0] + coefs[1] + coefs[2] + coefs[3]; 2503 return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum; 2504 } 2505 2506 fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${y} { 2507 var input_indices: ${e.type.indices} = output_indices; 2508 return colCubicInterpolation(input_indices, output_indices); 2509 } 2510 `},Ip=(e,t,r,i,a)=>{let[s,n,u,l,p]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],h=e.type.value;return` 2511 fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${h} { 2512 var input_indices: ${e.type.indices}; 2513 ${e.indicesSet("input_indices",n,`max(0, min(depth, ${r[n]} - 1))`)}; 2514 ${e.indicesSet("input_indices",u,`max(0, min(height, ${r[u]} - 1))`)}; 2515 ${e.indicesSet("input_indices",l,`max(0, min(width, ${r[l]} - 1))`)}; 2516 ${ka(e,p,s,3)} 2517 return ${e.getByIndices("input_indices")}; 2518 } 2519 2520 fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${h} { 2521 var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); 2522 var depth:${h} = originalIndices[${n}]; 2523 var height:${h} = originalIndices[${u}]; 2524 var width:${h} = originalIndices[${l}]; 2525 ${i?`if (depth < 0 || depth > (${r[n]} - 1) || height < 0 || height > (${r[u]} - 1) || width < 0 || (width > ${r[l]} - 1)) { 2526 return ${a}; 2527 }`:""}; 2528 2529 depth = max(0, min(depth, ${r[n]} - 1)); 2530 height = max(0, min(height, ${r[u]} - 1)); 2531 width = max(0, min(width, ${r[l]} - 1)); 2532 var depth1: u32 = u32(depth); 2533 var height1: u32 = u32(height); 2534 var width1: u32 = u32(width); 2535 var depth2: u32 = u32(depth + 1); 2536 var height2: u32 = u32(height + 1); 2537 var width2: u32 = u32(width + 1); 2538 var channel: u32 = ${r.length>3?`u32(originalIndices[${p}])`:"0"}; 2539 var batch: u32 = ${r.length>3?`u32(originalIndices[${s}])`:"0"}; 2540 2541 var x111: ${h} = getInputValue(batch, channel, depth1, height1, width1); 2542 var x112: ${h} = getInputValue(batch, channel, depth1, height1, width2); 2543 var x121: ${h} = getInputValue(batch, channel, depth1, height2, width1); 2544 var x122: ${h} = getInputValue(batch, channel, depth1, height2, width2); 2545 var x211: ${h} = getInputValue(batch, channel, depth2, height1, width1); 2546 var x212: ${h} = getInputValue(batch, channel, depth2, height1, width2); 2547 var x221: ${h} = getInputValue(batch, channel, depth2, height2, width1); 2548 var x222: ${h} = getInputValue(batch, channel, depth2, height2, width2); 2549 var dx1: ${h} = abs(depth - ${h}(depth1)); 2550 var dx2: ${h} = abs(${h}(depth2) - depth); 2551 var dy1: ${h} = abs(height - ${h}(height1)); 2552 var dy2: ${h} = abs(${h}(height2) - height); 2553 var dz1: ${h} = abs(width - ${h}(width1)); 2554 var dz2: ${h} = abs(${h}(width2) - width); 2555 if (depth1 == depth2) { 2556 dx1 = 0.5; 2557 dx2 = 0.5; 2558 } 2559 if (height1 == height2) { 2560 dy1 = 0.5; 2561 dy2 = 0.5; 2562 } 2563 if (width1 == width2) { 2564 dz1 = 0.5; 2565 dz2 = 0.5; 2566 } 2567 return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + 2568 x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); 2569 }`},Ep=(e,t,r,i,a,s)=>{let n=e.dims,u=wp(s,t.axes,n.length),l=bp(n,i,a,t.axes),p=i.slice();i.length===0&&(p=n.map((b,I)=>b===0?1:l[I]/b),t.keepAspectRatioPolicy!=="stretch"&&(l=$p(n,p,t)));let h=K("output",e.dataType,l.length),f=M("input",e.dataType,n.length),g=O.size(l),y=n.length===l.length&&n.every((b,I)=>b===l[I]),_=t.coordinateTransformMode==="tf_crop_and_resize",w=t.extrapolationValue,S=f.type.value,x=b=>` 2570 ${y?"":` 2571 ${yp(t.coordinateTransformMode,S)}; 2572 ${(()=>{switch(t.mode){case"nearest":return` 2573 ${Sp(f,n)}; 2574 ${_p(t.nearestMode,r,S)}; 2575 ${xp(f,h,n,l,p.length,u.length,_)}; 2576 `;case"linear":return` 2577 ${vp(h,n,l,p.length,u.length)}; 2578 ${(()=>{if(n.length===2||n.length===4)return`${kp(f,h,n,_,w)}`;if(n.length===3||n.length===5)return`${Ip(f,h,n,_,w)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; 2579 `;case"cubic":return` 2580 ${(()=>{if(n.length===2||n.length===4)return`${Tp(f,h,n,l,p,u,t.cubicCoeffA,_,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; 2581 `;default:throw Error("Invalid resize mode")}})()}; 2582 `} 2583 ${b.registerUniform("output_size","u32").registerUniform("scales","f32",p.length).registerUniform("roi","f32",u.length).declareVariables(f,h)} 2584 ${b.mainStart()} 2585 ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 2586 ${y?"output[global_idx] = input[global_idx];":` 2587 let output_indices = ${h.offsetToIndices("global_idx")}; 2588 var input_indices: ${f.type.indices}; 2589 ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); 2590 if (checkInputIndices(input_indices)) { 2591 output[global_idx] = ${f.getByIndices("input_indices")}; 2592 } else { 2593 output[global_idx] = ${t.extrapolationValue}; 2594 }`;case"linear":return`output[global_idx] = ${n.length===2||n.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()}; 2595`} 2596 }`;
vendor: 16,679 bytes, lines 2596-2779
2596return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${p.length>0?t.mode==="cubic"?p:p.length:""}|${a.length>0?a:""}|${u.length>0?u:""}|${y}|${t.mode==="nearest"?n.length:n}`,inputDependencies:["rank"]},getShaderSource:x,getRunData:()=>({outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},{type:1,data:p},{type:1,data:u},...Y(n,l)]})}},zp=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},Cp=(e,t)=>{let r=[],i=[],a=[],s=zp(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");gp(e.inputs,t,s,r,i,a),e.compute(Ep(e.inputs[0],t,s,r,i,a),{inputs:[0]})},Ap=e=>{let t=e.antialias,r=e.axes,i=e.coordinateTransformMode,a=e.cubicCoeffA,s=e.excludeOutside!==0,n=e.extrapolationValue,u=e.keepAspectRatioPolicy,l=e.mode,p=e.nearestMode===""?"simple":e.nearestMode;return he({antialias:t,axes:r,coordinateTransformMode:i,cubicCoeffA:a,excludeOutside:s,extrapolationValue:n,keepAspectRatioPolicy:u,mode:l,nearestMode:p})}}),Op,Rp,Bp,Em=q(()=>{te(),re(),ae(),Op=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],i=e[2];if(t.dataType!==r.dataType||t.dataType!==i.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let a=t.dims[t.dims.length-1],s=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==a)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==s)throw new Error("Skip must have the same sequence length as input");if(i.dims.length!==1)throw new Error("Gamma must be 1D");if(i.dims[i.dims.length-1]!==a)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let n=e[3];if(n.dims.length!==1)throw new Error("Beta must be 1D");if(n.dims[n.dims.length-1]!==a)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let n=e[4];if(n.dims.length!==1)throw new Error("Bias must be 1D");if(n.dims[n.dims.length-1]!==a)throw new Error("Bias must have the same hidden size as input")}},Rp=(e,t,r,i)=>{let a=t.simplified,s=e[0].dims,n=O.size(s),u=s,l=n,p=s.slice(-1)[0],h=i?s.slice(0,-1).concat(1):[],f=!a&&e.length>3,g=e.length>4,y=i&&r>1,_=i&&r>2,w=r>3,S=64,x=ve(p),b=[{type:12,data:l},{type:12,data:x},{type:12,data:p},{type:1,data:t.epsilon}],I=E=>{let C=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],A=[M("x",e[0].dataType,e[0].dims,x),M("skip",e[1].dataType,e[1].dims,x),M("gamma",e[2].dataType,e[2].dims,x)];f&&A.push(M("beta",e[3].dataType,e[3].dims,x)),g&&A.push(M("bias",e[4].dataType,e[4].dims,x)),A.push(K("output",e[0].dataType,u,x)),y&&A.push(K("mean_output",1,h)),_&&A.push(K("inv_std_output",1,h)),w&&A.push(K("input_skip_bias_sum",e[0].dataType,u,x));let v=ke(e[0].dataType),N=ke(1,x);return` 2597 2598 ${E.registerUniforms(C).declareVariables(...A)} 2599 var<workgroup> sum_shared : array<${N}, ${S}>; 2600 var<workgroup> sum_squared_shared : array<${N}, ${S}>; 2601 2602 ${E.mainStart([S,1,1])} 2603 let ix = local_id.x; 2604 let iy = global_id.x / ${S}; 2605 2606 let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; 2607 var stride = hidden_size_vectorized / ${S}; 2608 let offset = ix * stride + iy * hidden_size_vectorized; 2609 let offset1d = stride * ix; 2610 if (ix == ${S-1}) { 2611 stride = hidden_size_vectorized - stride * ix; 2612 } 2613 for (var i: u32 = 0; i < stride; i++) { 2614 let skip_value = skip[offset + i]; 2615 let bias_value = ${g?"bias[offset1d + i]":v+"(0.0)"}; 2616 let input_value = x[offset + i]; 2617 let value = input_value + skip_value + bias_value; 2618 ${w?"input_skip_bias_sum[offset + i] = value;":""} 2619 output[offset + i] = value; 2620 let f32_value = ${Wt(v,x,"value")}; 2621 sum_shared[ix] += f32_value; 2622 sum_squared_shared[ix] += f32_value * f32_value; 2623 } 2624 workgroupBarrier(); 2625 2626 var reduce_size : u32 = ${S}; 2627 for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { 2628 reduce_size = curr_size + (reduce_size & 1); 2629 if (ix < curr_size) { 2630 sum_shared[ix] += sum_shared[ix + reduce_size]; 2631 sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size]; 2632 } 2633 workgroupBarrier(); 2634 } 2635 2636 let sum = sum_shared[0]; 2637 let square_sum = sum_squared_shared[0]; 2638 let mean = ${ot("sum",x)} / f32(uniforms.hidden_size); 2639 let inv_std_dev = inverseSqrt(${ot("square_sum",x)} / f32(uniforms.hidden_size) ${a?"":"- mean * mean"} + uniforms.epsilon); 2640 ${y?"mean_output[global_idx] = mean;":""} 2641 ${_?"inv_std_output[global_idx] = inv_std_dev;":""} 2642 2643 for (var i: u32 = 0; i < stride; i++) { 2644 output[offset + i] = (output[offset + i] ${a?"":`- ${v}(mean)`}) * 2645 ${v}(inv_std_dev) * gamma[offset1d + i] 2646 ${f?"+ beta[offset1d + i]":""}; 2647 } 2648 }`},T=[{dims:u,dataType:e[0].dataType}];return r>1&&T.push({dims:h,dataType:1}),r>2&&T.push({dims:h,dataType:1}),r>3&&T.push({dims:s,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${x};${y};${_};${w}`,inputDependencies:e.map((E,C)=>"type")},getShaderSource:I,getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(l/p)},programUniforms:b})}},Bp=(e,t)=>{Op(e.inputs);let r=[0];e.outputCount>1&&r.push(-3),e.outputCount>2&&r.push(-3),e.outputCount>3&&r.push(3),e.compute(Rp(e.inputs,t,e.outputCount,!1),{outputs:r})}}),Mp,nr,Np,Ta,Dp,Pp,Up,qp,zm=q(()=>{te(),re(),xe(),ae(),Mp=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,i)=>{if(e[i+1].dataType!==6&&e[i+1].dataType!==7)throw new Error(`Input ${i} must be an array of int32 or int64`)})},nr=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(i=>r.push(Number(i)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(i=>r.push(Number(i)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},Np=(e,t)=>{if(e.length>1){let r=nr(e,1),i=nr(e,2),a=nr(e,3);return a.length===0&&(a=[...Array(e[0].dims.length).keys()]),he({starts:r,ends:i,axes:a})}else return t},Ta=(e,t,r,i,a)=>{let s=e;return e<0&&(s+=r[i[t]]),a[t]<0?Math.max(0,Math.min(s,r[i[t]]-1)):Math.max(0,Math.min(s,r[i[t]]))},Dp=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { 2649 var input_indices: ${e.type.indices}; 2650 var carry = 0u; 2651 for (var i = ${r.length-1}; i >= 0; i--) { 2652 let input_shape_i = ${Z("uniforms.input_shape","i",r.length)}; 2653 let steps_i = ${Z("uniforms.steps","i",r.length)}; 2654 let signs_i = ${Z("uniforms.signs","i",r.length)}; 2655 let starts_i = ${Z("uniforms.starts","i",r.length)}; 2656 var output_index = ${t.indicesGet("output_indices","i")}; 2657 var input_index = output_index * steps_i + starts_i + carry; 2658 carry = input_index / input_shape_i; 2659 input_index = input_index % input_shape_i; 2660 if (signs_i < 0) { 2661 input_index = input_shape_i - input_index - 1u + starts_i; 2662 } 2663 ${e.indicesSet("input_indices","i","input_index")}; 2664 } 2665 return input_indices; 2666 }`,Pp=(e,t)=>{let r=e[0].dims,i=O.size(r),a=t.axes.length>0?O.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],s=nr(e,4);s.forEach(x=>x!==0||(()=>{throw new Error("step cannot be 0")})),s.length===0&&(s=Array(a.length).fill(1));let n=t.starts.map((x,b)=>Ta(x,b,r,a,s)),u=t.ends.map((x,b)=>Ta(x,b,r,a,s));if(a.length!==n.length||a.length!==u.length)throw new Error("start, ends and axes should have the same number of elements");if(a.length!==r.length)for(let x=0;x<r.length;++x)a.includes(x)||(n.splice(x,0,0),u.splice(x,0,r[x]),s.splice(x,0,1));let l=s.map(x=>Math.sign(x));s.forEach((x,b,I)=>{if(x<0){let T=(u[b]-n[b])/x,E=n[b],C=E+T*s[b];n[b]=C,u[b]=E,I[b]=-x}});let p=r.slice(0);a.forEach((x,b)=>{p[x]=Math.ceil((u[x]-n[x])/s[x])});let h={dims:p,dataType:e[0].dataType},f=K("output",e[0].dataType,p.length),g=M("input",e[0].dataType,e[0].dims.length),y=O.size(p),_=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:n.length},{name:"signs",type:"i32",length:l.length},{name:"steps",type:"u32",length:s.length}],w=[{type:12,data:y},{type:12,data:n},{type:6,data:l},{type:12,data:s},...Y(e[0].dims,p)],S=x=>` 2667 ${x.registerUniforms(_).declareVariables(g,f)} 2668 ${Dp(g,f,r)} 2669 ${x.mainStart()} 2670 ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} 2671 let output_indices = ${f.offsetToIndices("global_idx")}; 2672 let input_indices = calculateInputIndices(output_indices); 2673 ${f.setByOffset("global_idx",g.getByIndices("input_indices"))} 2674 }`;return{name:"Slice",shaderCache:{hint:`${l.length}_${n.length}_${s.length}`,inputDependencies:["rank"]},getShaderSource:S,getRunData:()=>({outputs:[h],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:w})}},Up=(e,t)=>{Mp(e.inputs,t);let r=Np(e.inputs,t);e.compute(Pp(e.inputs,r),{inputs:[0]})},qp=e=>{let t=e.starts,r=e.ends,i=e.axes;return he({starts:t,ends:r,axes:i})}}),Wp,Lp,Vp,Gp,Cm=q(()=>{te(),re(),xe(),ut(),ae(),Wp=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},Lp=(e,t)=>{let r=e.inputs[0],i=r.dims,a=O.size(i),s=i.length,n=O.normalizeAxis(t.axis,s),u=n<i.length-1,l,p=[];u?(p=Array.from({length:s},(A,v)=>v),p[n]=s-1,p[s-1]=n,l=e.compute(Me(r,p),{inputs:[r],outputs:[-1]})[0]):l=r;let h=l.dims,f=h[s-1],g=a/f,y=ve(f),_=f/y,w=64;g===1&&(w=256);let S=(A,v)=>v===4?`max(max(${A}.x, ${A}.y), max(${A}.z, ${A}.w))`:v===2?`max(${A}.x, ${A}.y)`:v===3?`max(max(${A}.x, ${A}.y), ${A}.z)`:A,x=M("x",l.dataType,l.dims,y),b=K("result",l.dataType,l.dims,y),I=x.type.value,T=ke(l.dataType)==="f32"?`var threadMax = ${I}(-3.4028234663852886e+38f);`:`var threadMax = ${I}(-65504.0h);`,E=A=>` 2675 var<workgroup> rowMaxShared : ${I}; 2676 var<workgroup> rowSumShared : ${I}; 2677 var<workgroup> threadShared : array<${I}, ${w}>; 2678 2679 fn getValue(row: i32, col: i32, row_stride: i32) -> ${I} { 2680 let index = row * row_stride + col; 2681 return x[index]; 2682 } 2683 2684 fn setValue(row: i32, col: i32, row_stride: i32, value: ${I}) { 2685 let index = row * row_stride + col; 2686 result[index] = value; 2687 } 2688 ${A.registerUniform("packedCols","i32").declareVariables(x,b)} 2689 ${A.mainStart(w)} 2690 let gindex = i32(global_idx); 2691 let lindex = i32(local_idx); 2692 const wg = ${w}; 2693 let row = gindex / wg; 2694 let cols = uniforms.packedCols; 2695 let row_stride : i32 = uniforms.packedCols; 2696 2697 // find the rows max 2698 ${T} 2699 for (var col = lindex; col < cols; col += wg) { 2700 let value = getValue(row, col, row_stride); 2701 threadMax = max(threadMax, value); 2702 } 2703 if (lindex < cols) { 2704 threadShared[lindex] = threadMax; 2705 } 2706 workgroupBarrier(); 2707 2708 var reduceSize = min(cols, wg); 2709 for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) { 2710 reduceSize = currSize + (reduceSize & 1); 2711 if (lindex < currSize) { 2712 threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]); 2713 } 2714 workgroupBarrier(); 2715 } 2716 if (lindex == 0) { 2717 rowMaxShared = ${I}(${S("threadShared[0]",y)}); 2718 } 2719 workgroupBarrier(); 2720 2721 // find the rows sum 2722 var threadSum = ${I}(0.0); 2723 for (var col = lindex; col < cols; col += wg) { 2724 let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); 2725 threadSum += subExp; 2726 } 2727 threadShared[lindex] = threadSum; 2728 workgroupBarrier(); 2729 2730 for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) { 2731 if (lindex < currSize) { 2732 threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize]; 2733 } 2734 workgroupBarrier(); 2735 } 2736 if (lindex == 0) { 2737 rowSumShared = ${I}(${ot("threadShared[0]",y)}); 2738 } 2739 workgroupBarrier(); 2740 2741 // calculate final value for each element in the row 2742 for (var col = lindex; col < cols; col += wg) { 2743 var value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; 2744 // max operation protects against NaN since all values should be >=0 2745 value = max(value, ${I}(0.0)); 2746 setValue(row, col, row_stride, value); 2747 } 2748 }`,C=e.compute({name:"Softmax",shaderCache:{hint:`${y};${w}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:h,dataType:l.dataType}],dispatchGroup:{x:g},programUniforms:[{type:6,data:_}]}),getShaderSource:E},{inputs:[l],outputs:[u?-1:0]})[0];u&&e.compute(Me(C,p),{inputs:[C]})},Vp=(e,t)=>{Wp(e.inputs),Lp(e,t)},Gp=e=>he({axis:e.axis})}),Ia,Hp,Fp,jp,Kp,Am=q(()=>{te(),re(),ae(),Ia=e=>Array.from(e.getBigInt64Array(),Number),Hp=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(Ia(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},Fp=(e,t)=>{let r=[];for(let i=0;i<e.length;++i)r.push(e[i]*t[i]);return r},jp=(e,t)=>{let r=e[0].dims,i=t??Ia(e[1]),a=Fp(r,i),s=O.size(a),n=e[0].dataType,u=M("input",n,r.length),l=K("output",n,a.length),p=h=>` 2749 const inputShape = ${u.indices(...r)}; 2750 ${h.registerUniform("output_size","u32").declareVariables(u,l)} 2751 ${h.mainStart()} 2752 ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} 2753 let output_indices = ${l.offsetToIndices("global_idx")}; 2754 var input_indices: ${u.type.indices}; 2755 for (var i = 0; i < ${r.length}; i++) { 2756 let input_dim_i = ${u.indicesGet("uniforms.input_shape","i")}; 2757 let input_dim_value = ${l.indicesGet("output_indices","i")} % input_dim_i; 2758 2759 ${u.indicesSet("input_indices","i","input_dim_value")} 2760 } 2761 ${l.setByOffset("global_idx",u.getByIndices("input_indices"))} 2762 }`;return{name:"Tile",shaderCache:{hint:`${i}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:[{type:12,data:s},...Y(e[0].dims,a)]}),getShaderSource:p}},Kp=e=>{Hp(e.inputs),e.compute(jp(e.inputs),{inputs:[0]})}}),Qp,Zp,Yp,Om=q(()=>{te(),re(),ae(),Qp=(e,t,r,i,a)=>{let s=K("output_data",a,r.length,4),n=M("a_data",t[1].dataType,t[1].dims.length,4),u=M("b_data",t[2].dataType,t[2].dims.length,4),l=M("c_data",t[0].dataType,t[0].dims.length,4),p,h=(f,g,y)=>`select(${g}, ${f}, ${y})`;if(!i)p=s.setByOffset("global_idx",h(n.getByOffset("global_idx"),u.getByOffset("global_idx"),l.getByOffset("global_idx")));else{let f=(g,y,_="")=>{let w=`a_data[index_a${y}][component_a${y}]`,S=`b_data[index_b${y}][component_b${y}]`,x=`bool(c_data[index_c${y}] & (0xffu << (component_c${y} * 8)))`;return` 2763 let output_indices${y} = ${s.offsetToIndices(`global_idx * 4u + ${y}u`)}; 2764 let offset_a${y} = ${n.broadcastedIndicesToOffset(`output_indices${y}`,s)}; 2765 let offset_b${y} = ${u.broadcastedIndicesToOffset(`output_indices${y}`,s)}; 2766 let offset_c${y} = ${l.broadcastedIndicesToOffset(`output_indices${y}`,s)}; 2767 let index_a${y} = offset_a${y} / 4u; 2768 let index_b${y} = offset_b${y} / 4u; 2769 let index_c${y} = offset_c${y} / 4u; 2770 let component_a${y} = offset_a${y} % 4u; 2771 let component_b${y} = offset_b${y} % 4u; 2772 let component_c${y} = offset_c${y} % 4u; 2773 ${g}[${y}] = ${_}(${h(w,S,x)}); 2774 `};a===9?p=` 2775 var data = vec4<u32>(0); 2776 ${f("data",0,"u32")} 2777 ${f("data",1,"u32")} 2778 ${f("data",2,"u32")} 2779 ${f("data",3,"u32")}
2780 output_data[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:p=` 2781 ${f("output_data[global_idx]",0)} 2782 ${f("output_data[global_idx]",1)} 2783 ${f("output_data[global_idx]",2)} 2784 ${f("output_data[global_idx]",3)} 2785 `}return` 2786 ${e.registerUniform("vec_size","u32").declareVariables(l,n,u,s)} 2787 ${e.mainStart()} 2788 ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} 2789 ${p} 2790 }`},Zp=e=>{let t=e[1].dims,r=e[2].dims,i=e[0].dims,a=e[1].dataType,s=!(O.areEqual(t,r)&&O.areEqual(r,i)),n=t,u=O.size(t);if(s){let p=Ut.calcShape(Ut.calcShape(t,r,!1),i,!1);if(!p)throw new Error("Can't perform where op on the given tensors");n=p,u=O.size(n)}let l=Math.ceil(u/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:p=>Qp(p,e,n,s,a),getRunData:()=>({outputs:[{dims:n,dataType:a}],dispatchGroup:{x:Math.ceil(u/64/4)},programUniforms:[{type:12,data:l},...Y(i,t,r,n)]})}},Yp=e=>{e.compute(Zp(e.inputs))}}),Xp,Rm=q(()=>{jf(),Vi(),Kf(),Qf(),Zf(),Yf(),Xf(),im(),nm(),sm(),om(),um(),lm(),dm(),pm(),cm(),hm(),fm(),mm(),gm(),ym(),_m(),wm(),bm(),$m(),dd(),vm(),xm(),Sm(),km(),Tm(),qi(),Im(),bd(),Em(),zm(),Cm(),yd(),Am(),ut(),ji(),Om(),Xp=new Map([["Abs",[Oo]],["Acos",[Ro]],["Acosh",[Bo]],["Add",[$u]],["ArgMax",[yo,Li]],["ArgMin",[go,Li]],["Asin",[Mo]],["Asinh",[No]],["Atan",[Do]],["Atanh",[Po]],["Attention",[xo]],["AveragePool",[Zd,Qd]],["BatchNormalization",[Io]],["BiasAdd",[Co]],["BiasSplitGelu",[_u]],["Cast",[qo,Uo]],["Ceil",[Vo]],["Clip",[Lo]],["Concat",[Mu,Nu]],["Conv",[sa,aa]],["ConvTranspose",[ll,sl]],["Cos",[Go]],["Cosh",[Ho]],["CumSum",[pl,cl]],["DepthToSpace",[gl,yl]],["DequantizeLinear",[np,sp]],["Div",[vu]],["Einsum",[xl,Sl]],["Elu",[Fo,Jt]],["Equal",[xu]],["
2790Erf",[jo]],["Exp",[Ko]],["Expand",[El]],["FastGelu",[Cl]],["Floor",[Qo]],["FusedConv",[sa,aa]],["Gather",[Bl,Rl]],["GatherElements",[Hl,Gl]],["GatherBlockQuantized",[ql,Wl]],["GatherND",[Nl,Dl]],["Gelu",[Zo]],["Gemm",[Ql,Kl]],["GlobalAveragePool",[Xd,Yd]],["GlobalMaxPool",[rp,tp]],["Greater",[Iu]],["GreaterOrEqual",[zu]],["GridSample",[ad,nd]],["GroupQueryAttention",[Sd]],["HardSigmoid",[au,iu]],["InstanceNormalization",[Id]],["LayerNormalization",[Cd]],["LeakyRelu",[Yo,Jt]],["Less",[Eu]],["LessOrEqual",[Cu]],["Log",[cu]],["MatMul",[Od]],["MatMulNBits",[Nd,Dd]],["MaxPool",[Jd,ep]],["Mul",[Su]],["MultiHeadAttention",[ld,od]],["Neg",[Jo]],["Not",[Xo]],["Pad",[Fd]],["Pow",[ku]],["QuickGelu",[mu,Jt]],["Range",[lp]],["Reciprocal",[eu]],["ReduceMin",[po]],["ReduceMean",[no]],["ReduceMax",[lo]],["ReduceSum",[ho]],["ReduceProd",[co]],["ReduceL1",[so]],["ReduceL2",[oo]],["ReduceLogSum",[mo]],["ReduceLogSumExp",[uo]],["ReduceSumSquare",[fo]],["Relu",[tu]],["Resize",[Cp,Ap]],["RotaryEmbedding",[wd]],["ScatterND",[hp,cp]],["Sigmoid",[ru]],["Sin",[nu]],["Sinh",[su]],["Slice",[Up,qp]],["SkipLayerNormalization",[Bp]],["Split",[md,gd]],["Sqrt",[ou]],["Softmax",[Vp,Gp]],["Sub",[Tu]],["Tan",[uu]],["Tanh",[lu]],["ThresholdedRelu",[pu,Jt]],["Tile",[Kp]],["Transpose",[Ts,Is]],["Where",[Yp]]])}),Jp,Bm=q(()=>{Pe(),rt(),ae(),Jp=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,r,i,a){Ke(e.programInfo.name);let s=this.backend.device,n=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let u=[];for(let p of t)u.push({binding:u.length,resource:{buffer:p.buffer}});for(let p of r)u.push({binding:u.length,resource:{buffer:p.buffer}});a&&u.push({binding:u.length,resource:a});let l=s.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:u,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let p={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:l,dispatchGroup:i};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(p)}n.setPipeline(e.computePipeline),n.setBindGroup(0,l),n.dispatchWorkgroups(...i),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),qe(e.programInfo.name)}dispose(){}build(e,t){Ke(e.name);let r=this.backend.device,i=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(p=>{r.features.has(p.feature)&&i.push(`enable ${p.extension};`)});let a=bs(t,this.backend.device.limits),s=e.getShaderSource(a),n=`${i.join(` 2791`)} 2792${a.additionalImplementations} 2793${s}`,u=r.createShaderModule({code:n,label:e.name});de("verbose",()=>`[WebGPU] ${e.name} shader code: ${n}`);let l=r.createComputePipeline({compute:{module:u,entryPoint:"main"},layout:"auto",label:e.name});return qe(e.name),{programInfo:e,computePipeline:l,uniformVariablesInfo:a.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,r=typeof e=="number"?1:e.y||1,i=typeof e=="number"?1:e.z||1,a=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=a&&r<=a&&i<=a)return[t,r,i];let s=t*r*i,n=Math.ceil(Math.sqrt(s));if(n>a){if(n=Math.ceil(Math.cbrt(s)),n>a)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[n,n,n]}else return[n,n,1]}}}),ec={};Dt(ec,{WebGpuBackend:()=>ac});var tc,rc,ic,ac,Mm=q(()=>{Pe(),te(),rt(),ss(),Hf(),Rm(),Bm(),tc=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let i=0;i<e.length;++i){let a=e[i].dataType;switch(t[i]){case"none":{r.push("");break}case"type":{r.push(`${a}`);break}case"rank":{let s=e[i].dims.length;r.push(`${a};${s}`);break}case"dims":{let s=e[i].dims.join(",");r.push(`${a};${s}`);break}default:throw new Error(`unsupported input dependency: ${t[i]}`)}}return r.join("|")},rc=(e,t,r)=>{var a,s;let i=e.name;return(a=e.shaderCache)!=null&&a.hint&&(i+="["+e.shaderCache.hint+"]"),i+=":"+r+`:${tc(t,((s=e.shaderCache)==null?void 0:s.inputDependencies)??new Array(t.length).fill("dims"))}`,i},ic=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vend
vendor: 1,773 bytes, line 2793
2793or)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},ac=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let r=[],i={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:r},a=s=>t.features.has(s)&&r.push(s)&&!0;a("chromium-experimental-timestamp-query-inside-passes")||a("timestamp-query"),a("shader-f16"),a("subgroups"),this.device=await t.requestDevice(i),this.adapterInfo=new ic(t.info||await t.requestAdapterInfo()),this.gpuDataManager=gs(this),this.programManager=new Jp(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,xi(e.logLevel,!!e.debug),this.device.onuncapturederror=s=>
2793{s.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${s.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Ke(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{var i;let t=new BigUint64Array(e.getMappedRange()),r=this.pendingQueries.get(e);for(let a=0;a<t.length/2;a++){let s=r[a],n=s.kernelId,u=this.kernels.get(n),l=u.kernelType,p=u.kernelName,h=s.programName,f=s.inputTensorViews,g=s.outputTensorViews,y=t[a*2],_=t[a*2+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=y);let w=Number(y-this.queryTimeBase),S=Number(_-this.queryTimeBase);if(!Number.isSafeInteger(w)||!Number.isSafeInteger(S))throw new RangeError("incorrect timestamp range");if((i=this.env.webgpu.profiling)!=null&&i.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:f.map(x=>({dims:x.dims,dataType:tt(x.dataType)})),outputsMetadata:g.map(x=>({dims:x.dims,dataType:tt(x.dataType)})),kernelId:n,kernelType:l,kernelName:p,programName:h,startTime:w,endTime:S});else{let x="";f.forEach((I,T)=>{x+=`input[${T}]: [${I.dims}] | ${tt(I.dataType)}, `});let b="";g.forEach((I,T)=>{b+=`output[${T}]: [${I.dims}] | ${tt(I.dataType)}, `}),console.log(`[profiling] kernel "${n}|${l}|${p}|${h}" ${x}${b}start time: ${w} ns, execution time: ${S-w} ns`)}yr("GPU",`${h}::${y}::${_}`)}e.unmap(),this.pendingQueries.delete(e)}),qe()}run(e,t,r,i,a,s){Ke(e.name);let n=[];for(let b=0;b<t.length;++b){let I=t[b].data;if(I===0)continue;let T=this.gpuDataManager.get(I);
vendor: 10,632 bytes, line 2793
2793if(!T)throw new Error(`no GPU data for input: ${I}`);n.push(T)}let{outputs:u,dispatchGroup:l,programUniforms:p}=e.getRunData(t),h=r.length===0?u.map((b,I)=>I):r;if(h.length!==u.length)throw new Error(`Output size ${h.length} must be equal to ${u.length}.`);let f=[],g=[];for(let b=0;b<u.length;++b){if(!Number.isInteger(h[b])||h[b]<-3||h[b]>=s)throw new Error(`Invalid output index: ${h[b]}`);if(h[b]===-3)continue;let I=h[b]===-1,T=h[b]===-2,E=I||T?a(u[b].dataType,u[b].dims):i(h[b],u[b].dataType,u[b].dims);if(f.push(E),E.data===0)continue;let C=this.gpuDataManager.get(E.data);if(!C)throw new Error(`no GPU data for output: ${E.data}`);if(I&&this.temporaryData.push(C),T){let A=this.kernelPersistentData.get(this.currentKernelId);A||(A=[],this.kernelPersistentData.set(this.currentKernelId,A)),A.push(C)}g.push(C)}if(n.length!==t.length||g.length!==f.length){if(g.length===0)return qe(e.name),f;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let y;if(p){let b=0,I=[];p.forEach(A=>{let v=typeof A.data=="number"?[A.data]:A.data;if(v.length===0)return;let N=A.type===10?2:4,D,H;A.type===10?(H=v.length>4?16:v.length>2?8:v.length*N,D=v.length>4?16:N*v.length):(H=v.length<=2?v.length*N:16,D=16),b=Math.ceil(b/H)*H,I.push(b);let G=A.type===10?8:4;b+=v.length>4?Math.ceil(v.length/G)*D:v.length*N});let T=16;b=Math.ceil(b/T)*T;let E=new ArrayBuffer(b);p.forEach((A,v)=>{let N=I[v],D=typeof A.data=="number"?[A.data]:A.data;if(A.type===6)new Int32Array(E,N,D.length).set(D);else if(A.type===12)new Uint32Array(E,N,D.length).set(D);else if(A.type===10)new Uint16Array(E,N,D.length).set(D);else if(A.type===1)new Float32Array(E,N,D.length).set(D);else throw new Error(`Unsupported uniform type: ${tt(A.type)}`)});let C=this.gpuDataManager.create(b,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(C.buffer,0,E,0,b),this.gpuDataManager.release(C.id),y={offset:0,size:b,buffer:C.buffer}}let _=this.programManager.normalizeDispatchGroupSize(l),w=_[1]===1&&_[2]===1,S=rc(e,t,w),x=this.programManager.getArtifact(S);if(x||(x=this.programManager.build(e,_),this.programManager.setArtifact(S,x),de("info",()=>`[artifact] key: ${S}, programName: ${e.name}`)),p&&x.uniformVariablesInfo){if(p.length!==x.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${x.uniformVariablesInfo.length}, got ${p.length} in program "${x.programInfo.name}".`);for(let b=0;b<p.length;b++){let I=p[b],T=I.type,E=typeof I.data=="number"?1:I.data.length,[C,A]=x.uniformVariablesInfo[b];if(T!==C||E!==A)throw new Error(`Uniform variable ${b} mismatch: expect type ${C} with size ${A}, got type ${T} with size ${E} in program "${x.programInfo.name}".`)}}if(de("info",()=>`[ProgramManager] run "${e.name}" (key=${S}) with ${_[0]}x${_[1]}x${_[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let b={kernelId:this.currentKernelId,programName:x.programInfo.name,inputTensorViews:t,outputTensorViews:f};this.pendingKernels.push(b),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(b)}return this.programManager.run(x,n,g,_,y),qe(e.name),f}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,r,i){let a=Xp.get(e);if(!a)throw new Error(`kernel not implemented: ${e}`);let s={kernelType:e,kernelName:i,kernelEntry:a[0],attributes:[a[1],r]};this.kernels.set(t,s)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let r of t)this.gpuDataManager.release(r.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,r){let i=this.kernels.get(e);if(!i)throw new Error(`kernel not created: ${e}`);let a=i.kernelType,s=i.kernelName,n=i.kernelEntry,u=i.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${a}] ${s}" is not allowed to be called recursively`);this.currentKernelId=e,u[0]&&(u[1]=u[0](u[1]),u[0]=void 0),de("info",()=>`[WebGPU] Start to run kernel "[${a}] ${s}"...`);let l=this.env.debug;this.temporaryData=[];try{return l&&this.device.pushErrorScope("validation"),n(t,u[1]),0}catch(p){return r.push(Promise.resolve(`[WebGPU] Kernel "[${a}] ${s}" failed. ${p}`)),1}finally{l&&r.push(this.device.popErrorScope().then(p=>p?`GPU validation error for kernel "[${a}] ${s}": ${p.message}`:null));for(let p of this.temporaryData)this.gpuDataManager.release(p.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,r,i){let a=this.sessionExternalDataMapping.get(e);a||(a=new Map,this.sessionExternalDataMapping.set(e,a));let s=a.get(t),n=this.gpuDataManager.registerExternalBuffer(r,i,s);return a.set(t,[n,r]),n}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(r=>this.gpuDataManager.unregisterExternalBuffer(r[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,r){return async()=>{let i=await Mi(this,e,t);return Si(i.buffer,r)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){var e;this.queryType="none",(((e=this.env.webgpu.profiling)==null?void 0:e.mode)==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){de("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){de("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){de("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),r=e.length;this.pendingKernels=[];for(let i=0;i<r;i++){let a=this.getComputePassEncoder(),s=e[i];this.writeTimestamp(this.pendingDispatchNumber*2),a.setPipeline(s.computePipeline),a.setBindGroup(0,s.bindGroup),a.dispatchWorkgroups(...s.dispatchGroup),this.writeTimestamp(this.pendingDispatchNumber*2+1),this.pendingDispatchNumber++,this.queryType!=="none"&&this.pendingKernels.push(t[i]),(this.pendingDispatchNumber>=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),nc={};Dt(nc,{init:()=>oc});var Mr,sc,oc,Nm=q(()=>{te(),rt(),re(),Gf(),Mr=class $f{constructor(t,r,i,a){this.module=t,this.dataType=r,this.data=i,this.dims=a}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=O.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=O.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=O.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=O.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(O.size(t)!==O.size(this.dims))throw new Error("Invalid new shape");return new $f(this.module,this.dataType,this.data,t)}},sc=class{constructor(e,t,r){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let i=e.PTR_SIZE,a=r/e.PTR_SIZE,s=i===4?"i32":"i64";this.opKernelContext=Number(e.getValue(i*a++,s));let n=Number(e.getValue(i*a++,s));this.outputCount=Number(e.getValue(i*a++,s)),this.customDataOffset=Number(e.getValue(i*a++,"*")),this.customDataSize=Number(e.getValue(i*a++,s));let u=[];for(let l=0;l<n;l++){let p=Number(e.getValue(i*a++,s)),h=Number(e.getValue(i*a++,"*")),f=Number(e.getValue(i*a++,s)),g=[];for(let y=0;y<f;y++)g.push(Number(e.getValue(i*a++,s)));u.push(new Mr(e,p,h,g))}this.inputs=u}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}compute(e,t){var n;let r=((n=t==null?void 0:t.inputs)==null?void 0:n.map(u=>typeof u=="number"?this.inputs[u]:u))??this.inputs,i=(t==null?void 0:t.outputs)??[],a=(u,l,p)=>new Mr(this.module,l,this.output(u,p),p),s=(u,l)=>{let p=St(u,l);if(!p)throw new Error(`Unsupported data type: ${u}`);let h=p>0?this.backend.gpuDataManager.create(p).id:0;return new Mr(this.module,u,h,l)};return this.backend.run(e,r,i,a,s,this.outputCount)}output(e,t){let r=this.module.stackSave();try{let i=this.module.PTR_SIZE,a=i===4?"i32":"i64",s=this.module.stackAlloc((1+t.length)*i);this.module.setValue(s,t.length,a);for(let n=0;n<t.length;n++)this.module.setValue(s+i*(n+1),t[n],a);return this.module._JsepOutput(this.opKernelContext,e,s)}catch(i){throw new Error(`Failed to generate kernel's output[${e}] with dims [${t}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${i}`)}finally{this.module.stackRestore(r)}}},oc=async(e,t,r,i)=>{let a=t.jsepInit;if(!a)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let s=(Mm(),Ht(ec)).WebGpuBackend,n=new s;await n.initialize(r,i),a("webgpu",[n,u=>n.alloc(Number(u)),u=>n.free(u),(u,l,p,h=!1)=>
2793{if(h)de("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(u)}, dst=${Number(l)}, size=${Number(p)}`),n.memcpy(Number(u),Number(l));else{de("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(u)}, gpuDataId=${Number(l)}, size=${Number(p)}`);let f=t.HEAPU8.subarray(Number(u>>>0),Number(u>>>0)+Number(p));n.upload(Number(l),f)}},async(u,l,p)=>{de("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${u}, dataOffset=${l}, size=${p}`),await n.download(Number(u),()=>t.HEAPU8.subarray(Number(l)>>>0,Number(l+p)>>>0))},(u,l,p)=>n.createKernel(u,Number(l),p,t.UTF8ToString(t._JsepGetNodeName(Number(l)))),u=>n.releaseKernel(u),(u,l,p,h)=>{de("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${p}, kernel=${u}, contextDataOffset=${l}`);let f=new sc(t,n,Number(l));return n.computeKernel(Number(u),f,h)},()=>n.captureBegin(),()=>n.captureEnd(),()=>n.replay()])}else{let s=new cs(r);a("webnn",[s,()=>s.reserveTensorId(),n=>s.releaseTensorId(n),async(n,u,l,p,h)=>s.ensureTensor(n,u,l,p,h),(n,u)=>{s.uploadTensor(n,u)},async(n,u)=>s.downloadTensor(n,u),(n,u)=>s.registerMLContext(n,u),!!r.trace])}}}),uc,Ea,za,lt,lc,Ca,Nr,Aa,Oa,Ra,Ba,Ma,Na,dc=q(()=>{Pe(),Wf(),Lf(),te(),vt(),_i(),Zn(),uc=(e,t)=>{_e()._OrtInit(e,t)!==0&&fe("Can't initialize onnxruntime.")},Ea=async e=>{uc(e.wasm.numThreads,vr(e.logLevel))},za=async(e,t)=>{var i,a;(a=(i=_e()).asyncInit)==null||a.call(i);let r=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(r){if(typeof r.limits!="object"||typeof r.features!="object"||typeof r.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let s=e.webgpu.powerPreference;if(s!==void 0&&s!=="low-power"&&s!=="high-performance")throw new Error(`Invalid powerPreference setting: "${s}"`);let n=e.webgpu.forceFallbackAdapter;if(n!==void 0&&typeof n!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${n}"`);if(r=await navigator.gpu.requestAdapter({powerPreference:s,forceFallbackAdapter:n}),!r)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let s=(Nm(),Ht(nc)).init;t==="webgpu"&&await s("webgpu",_e(),e,r),t==="webnn"&&await s("webnn",_e(),e)}},lt=new Map,lc=e=>{let t=_e(),r=t.stackSave();try{let i=t.PTR_SIZE,a=t.stackAlloc(2*i);t._OrtGetInputOutputCount(e,a,a+i)!==0&&fe("Can't get session input/output count.");let s=i===4?"i32":"i64";return[Number(t.getValue(a,s)),Number(t.getValue(a+i,s))]}finally{t.stackRestore(r)}},Ca=(e,t)=>{let r=_e(),i=r.stackSave(),a=0;try{let s=r.PTR_SIZE,n=r.stackAlloc(2*s);r._OrtGetInputOutputMetadata(e,t,n,n+s)!==0&&fe("Can't get session input/output metadata.");let u=Number(r.getValue(n,"*"));a=Number(r.getValue(n+s,"*"));let l=r.HEAP32[a/4];if(l===0)return[u,0];let p=r.HEAPU32[a/4+1],h=[];for(let f=0;f<p;f++){let g=Number(r.getValue(a+8+f*s,"*"));h.push(g!==0?r.UTF8ToString(g):Number(r.getValue(a+8+(f+p)*s,"*")))}return[u,l,h]}finally{r.stackRestore(i),a!==0&&r._OrtFree(a)}},Nr=e=>{let t=_e(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},Aa=async(e,t)=>{var f,g,y,_;let r,i,a=_e();Array.isArray(e)?[r,i]=e:e.buffer===a.HEAPU8.buffer?[r,i]=[e.byteOffset,e.byteLength]:[r,i]=Nr(e);let s=0,n=0,u=0,l=[],p=[],h=[];try{if([n,l]=await Qn(t),(t==null?void 0:t.externalData)&&a.mountExternalData){let v=[];for(let N of t.externalData){let D=typeof N=="string"?N:N.path;v.push(vi(typeof N=="string"?N:N.data).then(H=>{a.mountExternalData(D,H)}))}await Promise.all(v)}for(let v of(t==null?void 0:t.executionProviders)??[])if((typeof v=="string"?v:v.name)==="webnn"){if(a.shouldTransferToMLTensor=!1,typeof v!="string"){let N=v,D=N==null?void 0:N.context,H=N==null?void 0:N.gpuDevice,G=N==null?void 0:N.deviceType,Q=N==null?void 0:N.powerPreference;D?a.currentContext=D:H?a.currentContext=await a.webnnCreateMLContext(H):a.currentContext=await a.webnnCreateMLContext({deviceType:G,powerPreference:Q})}else a.currentContext=await a.webnnCreateMLContext();break}s=await a._OrtCreateSession(r,i,n),(f=a.webgpuOnCreateSession)==null||f.call(a,s),s===0&&fe("Can't create a session."),(g=a.jsepOnCreateSession)==null||g.call(a),a.currentContext&&(a.webnnRegisterMLContext(s,a.currentContext),a.currentContext=void 0,a.shouldTransferToMLTensor=!0);let[w,S]=lc(s),x=!!(t!=null&&t.enableGraphCapture),b=[],I=[],T=[],E=[],C=[];for(let v=0;v<w;v++){let[N,D,H]=Ca(s,v);N===0&&fe("Can't get an input name."),p.push(N);let G=a.UTF8ToString(N);b.push(G),T.push(D===0?{name:G,isTensor:!1}:{name:G,isTensor:!0,type:tt(D),shape:H})}for(let v=0;v<S;v++){let[N,D,H]=Ca(s,v+w);N===0&&fe("Can't get an output name."),h.push(N);let G=a.UTF8ToString(N);I.push(G),E.push(D===0?{name:G,isTensor:!1}:{name:G,isTensor:!0,type:tt(D),shape:H});{if(x&&(t==null?void 0:t.preferredOutputLocation)===void 0){C.push("gpu-buffer");continue}let Q=typeof(t==null?void 0:t.preferredOutputLocation)=="string"?t.preferredOutputLocation:((y=t==null?void 0:t.preferredOutputLocation)==null?void 0:y[G])??"cpu",R=a.webnnIsGraphOutput;if(Q==="cpu"&&R&&R(s,G)){C.push("ml-tensor-cpu-output");continue}if(Q!=="cpu"&&Q!=="cpu-pinned"&&Q!=="gpu-buffer"&&Q!=="ml-tensor")throw new Error(`Not supported preferred output location: ${Q}.`);
vendor: 7,696 bytes, line 2793
2793if(x&&Q!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${Q}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);C.push(Q)}}let A=null;return C.some(v=>v==="gpu-buffer"||v==="ml-tensor"||v==="ml-tensor-cpu-output")&&(u=a._OrtCreateBinding(s),u===0&&fe("Can't create IO binding."),A={handle:u,outputPreferredLocations:C,outputPreferredLocationsEncoded:C.map(v=>v==="ml-tensor-cpu-output"?"ml-tensor":v).map(v=>$i(v))}),lt.set(s,[s,p,h,A,x,!1]),[s,b,I,T,E]}catch(w){throw p.forEach(S=>a._OrtFree(S)),h.forEach(S=>a._OrtFree(S)),u!==0&&a._OrtReleaseBinding(u)!==0&&fe("Can't release IO binding."),s!==0&&a._OrtReleaseSession(s)!==0&&fe("Can't release session."),w}finally{a._free(r),n!==0&&a._OrtReleaseSessionOptions(n)!==0&&fe("Can't release session options."),l.forEach(w=>a._free(w)),(_=a.unmountExternalData)==null||_.call(a)}},Oa=e=>{var l,p,h;let t=_e(),r=lt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[i,a,s,n,u]=r;n&&(u&&t._OrtClearBoundOutputs(n.handle)!==0&&fe("Can't clear bound outputs."),t._OrtReleaseBinding(n.handle)!==0&&fe("Can't release IO binding.")),(l=t.jsepOnReleaseSession)==null||l.call(t,e),(p=t.webnnOnReleaseSession)==null||p.call(t,e),(h=t.webgpuOnReleaseSession)==null||h.call(t,e),a.forEach(f=>t._OrtFree(f)),s.forEach(f=>t._OrtFree(f)),t._OrtReleaseSession(i)!==0&&fe("Can't release session."),lt.delete(e)},Ra=async(e,t,r,i,a,s,n=!1)=>{if(!e){t.push(0);return}let u=_e(),l=u.PTR_SIZE,p=e[0],h=e[1],f=e[3],g=f,y,_;if(p==="string"&&(f==="gpu-buffer"||f==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(n&&f!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${s} when enableGraphCapture is true.`);if(f==="gpu-buffer"){let x=e[2].gpuBuffer;_=St(xt(p),h);{let b=u.jsepRegisterBuffer;if(!b)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');y=b(i,s,x,_)}}else if(f==="ml-tensor"){let x=e[2].mlTensor;_=St(xt(p),h);let b=u.webnnRegisterMLTensor;if(!b)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');y=b(i,x,xt(p),h)}else{let x=e[2];if(Array.isArray(x)){_=l*x.length,y=u._malloc(_),r.push(y);for(let b=0;b<x.length;b++){if(typeof x[b]!="string")throw new TypeError(`tensor data at index ${b} is not a string`);u.setValue(y+b*l,We(x[b],r),"*")}}else{let b=u.webnnIsGraphInput,I=u.webnnIsGraphOutput;if(p!=="string"&&b&&I){let T=u.UTF8ToString(a);if(b(i,T)||I(i,T)){let E=xt(p);_=St(E,h),g="ml-tensor";let C=u.webnnCreateTemporaryTensor,A=u.webnnUploadTensor;if(!C||!A)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');let v=await C(i,E,h);A(v,new Uint8Array(x.buffer,x.byteOffset,x.byteLength)),y=v}else _=x.byteLength,y=u._malloc(_),r.push(y),u.HEAPU8.set(new Uint8Array(x.buffer,x.byteOffset,_),y)}else _=x.byteLength,y=u._malloc(_),r.push(y),u.HEAPU8.set(new Uint8Array(x.buffer,x.byteOffset,_),y)}}let w=u.stackSave(),S=u.stackAlloc(4*h.length);try{h.forEach((b,I)=>u.setValue(S+I*l,b,l===4?"i32":"i64"));let x=u._OrtCreateTensor(xt(p),y,_,S,h.length,$i(g));x===0&&fe(`Can't create tensor for input/output. session=${i}, index=${s}.`),t.push(x)}finally{u.stackRestore(w)}},Ba=async(e,t,r,i,a,s)=>{var G,Q,R,j;let n=_e(),u=n.PTR_SIZE,l=lt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let p=l[0],h=l[1],f=l[2],g=l[3],y=l[4],_=l[5],w=t.length,S=i.length,x=0,b=[],I=[],T=[],E=[],C=[],A=n.stackSave(),v=n.stackAlloc(w*u),N=n.stackAlloc(w*u),D=n.stackAlloc(S*u),H=n.stackAlloc(S*u);try{[x,b]=Gn(s),bt("wasm prepareInputOutputTensor");for(let W=0;W<w;W++)await Ra(r[W],I,E,e,h[t[W]],t[W],y);for(let W=0;W<S;W++)await Ra(a[W],T,E,e,f[i[W]],w+i[W],y);$t("wasm prepareInputOutputTensor");for(let W=0;W<w;W++)n.setValue(v+W*u,I[W],"*"),n.setValue(N+W*u,h[t[W]],"*");for(let W=0;W<S;W++)n.setValue(D+W*u,T[W],"*"),n.setValue(H+W*u,f[i[W]],"*");if(g&&!_){let{handle:W,outputPreferredLocations:me,outputPreferredLocationsEncoded:P}=g;if(h.length!==w)throw new Error(`input count from feeds (${w}) is expected to be always equal to model's input count (${h.length}).`);bt("wasm bindInputsOutputs");for(let U=0;U<w;U++){let J=t[U];await n._OrtBindInput(W,h[J],I[U])!==0&&fe(`Can't bind input[${U}] for session=${e}.`)}for(let U=0;U<S;U++){let J=i[U];(G=a[U])!=null&&G[3]?(C.push(T[U]),n._OrtBindOutput(W,f[J],T[U],0)!==0&&fe(`Can't bind pre-allocated output[${U}] for session=${e}.`)):n._OrtBindOutput(W,f[J],0,P[J])!==0&&fe(`Can't bind output[${U}] to ${me[U]} for session=${e}.`)}$t("wasm bindInputsOutputs"),lt.set(e,[p,h,f,g,y,!0])}(Q=n.jsepOnRunStart)==null||Q.call(n,p),(R=n.webnnOnRunStart)==null||R.call(n,p);let F;g?F=await n._OrtRunWithBinding(p,g.handle,S,D,x):F=await n._OrtRun(p,N,v,w,H,S,D,x),F!==0&&fe("failed to call OrtRun().");let X=[],le=[];bt("wasm ProcessOutputTensor");for(let W=0;W<S;W++){let me=Number(n.getValue(D+W*u,"*"));if(me===T[W]||C.includes(T[W])){X.push(a[W]),me!==T[W]&&n._OrtReleaseTensor(me)!==0&&fe("Can't release tensor.");continue}let P=n.stackSave(),U=n.stackAlloc(4*u),J=!1,ee,$e=0;try{n._OrtGetTensorData(me,U,U+u,U+2*u,U+3*u)!==0&&fe(`Can't access output tensor data on index ${W}.`);let pt=u===4?"i32":"i64",ct=Number(n.getValue(U,pt));$e=n.getValue(U+u,"*");let lr=n.getValue(U+u*2,"*"),qr=Number(n.getValue(U+u*3,pt)),Ne=[];for(let we=0;we<qr;we++)Ne.push(Number(n.getValue(lr+we*u,pt)));n._OrtFree(lr)!==0&&fe("Can't free memory for tensor dims.");let Ae=Ne.reduce((we,ie)=>we*ie,1);ee=tt(ct);let ht=g==null?void 0:g.outputPreferredLocations[i[W]];if(ee==="string"){if(ht==="gpu-buffer"||ht==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let we=[];for(let ie=0;ie<Ae;ie++){let De=n.getValue($e+ie*u,"*"),Wr=n.getValue($e+(ie+1)*u,"*"),dr=ie===Ae-1?void 0:Wr-De;we.push(n.UTF8ToString(De,dr))}X.push([ee,Ne,we,"cpu"])}else if(ht==="gpu-buffer"&&Ae>0){let we=n.jsepGetBuffer;if(!we)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let ie=we($e),De=St(ct,Ae);if(De===void 0||!wi(ee))throw new Error(`Unsupported data type: ${ee}`);J=!0,X.push([ee,Ne,{gpuBuffer:ie,download:n.jsepCreateDownloader(ie,De,ee),dispose:()=>{n._OrtReleaseTensor(me)!==0&&fe("Can't release tensor.")}},"gpu-buffer"])}else if(ht==="ml-tensor"&&Ae>0){let we=n.webnnEnsureTensor,ie=n.webnnIsGraphInputOutputTypeSupported;if(!we||!ie)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(St(ct,Ae)===void 0||!bi(ee))throw new Error(`Unsupported data type: ${ee}`);if(!ie(e,ee,!1))throw new Error(`preferredLocation "ml-tensor" for ${ee} output is not supported by current WebNN Context.`);let De=await we(e,$e,ct,Ne,!1);J=!0,X.push([ee,Ne,{mlTensor:De,download:n.webnnCreateMLTensorDownloader($e,ee),dispose:()=>{n.webnnReleaseTensorId($e),n._OrtReleaseTensor(me)}},"ml-tensor"])}else if(ht==="ml-tensor-cpu-output"&&Ae>0){let we=n.webnnCreateMLTensorDownloader($e,ee)(),ie=X.length;J=!0,le.push((async()=>{let De=[ie,await we];return n.webnnReleaseTensorId($e),n._OrtReleaseTensor(me),De})()),X.push([ee,Ne,[],"cpu"])}else{let we=$r(ee),ie=new we(Ae);new Uint8Array(ie.buffer,ie.byteOffset,ie.byteLength).set(n.HEAPU8.subarray($e,$e+ie.byteLength)),X.push([ee,Ne,ie,"cpu"])}}finally{n.stackRestore(P),ee==="string"&&$e&&n._free($e),J||n._OrtReleaseTensor(me)}}g&&!y&&(n._OrtClearBoundOutputs(g.handle)!==0&&fe("Can't clear bound outputs."),lt.set(e,[p,h,f,g,y,!1]));for(let[W,me]of await Promise.all(le))X[W][2]=me;return $t("wasm ProcessOutputTensor"),X}finally{(j=n.webnnOnRunEnd)==null||j.call(n,p),n.stackRestore(A),I.forEach(F=>n._OrtReleaseTensor(F)),T.forEach(F=>
vendor: 5,835 bytes, line 2793
2793n._OrtReleaseTensor(F)),E.forEach(F=>n._free(F)),x!==0&&n._OrtReleaseRunOptions(x),b.forEach(F=>n._free(F))}},Ma=e=>{let t=_e(),r=lt.get(e);if(!r)throw new Error("invalid session id");let i=r[0],a=t._OrtEndProfiling(i);a===0&&fe("Can't get an profile file name."),t._OrtFree(a)},Na=e=>{let t=[];for(let r of e){let i=r[2];!Array.isArray(i)&&"buffer"in i&&t.push(i.buffer)}return t}}),dt,Ce,Lt,sr,or,Dr,Da,Pr,At,Ot,pc,cc,hc,fc,mc,gc,yc,_c,wc=q(()=>{Pe(),dc(),vt(),fi(),dt=()=>!!ge.wasm.proxy&&typeof document<"u",Lt=!1,sr=!1,or=!1,Pr=new Map,At=(e,t)=>{let r=Pr.get(e);r?r.push(t):Pr.set(e,[t])},Ot=()=>{if(Lt||!sr||or||!Ce)throw new Error("worker not ready")},pc=e=>{switch(e.data.type){case"init-wasm":Lt=!1,e.data.err?(or=!0,Da[1](e.data.err)):(sr=!0,Da[0]()),Dr&&(URL.revokeObjectURL(Dr),Dr=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=Pr.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}}},cc=async()=>{if(!sr){if(Lt)throw new Error("multiple calls to 'initWasm()' detected.");if(or)throw new Error("previous call to 'initWasm()' failed.");if(Lt=!0,dt())return new Promise((e,t)=>{Ce==null||Ce.terminate(),Un().then(([r,i])=>{try{Ce=i,Ce.onerror=s=>t(s),Ce.onmessage=pc,Da=[e,t];let a={type:"init-wasm",in:ge};!a.in.wasm.wasmPaths&&(r||di)&&(a.in.wasm.wasmPaths={wasm:new URL("/assets/ort-wasm-simd-threaded.jsep-C887KxcQ.wasm",self.location.href).href}),Ce.postMessage(a),Dr=r}catch(a){t(a)}},t)});try{await yi(ge.wasm),await Ea(ge),sr=!0}catch(e){throw or=!0,e}finally{Lt=!1}}},hc=async e=>{if(dt())return Ot(),new Promise((t,r)=>{At("init-ep",[t,r]);let i={type:"init-ep",in:{epName:e,env:ge}};Ce.postMessage(i)});await za(ge,e)},fc=async e=>dt()?(Ot(),new Promise((t,r)=>{At("copy-from",[t,r]);let i={type:"copy-from",in:{buffer:e}};Ce.postMessage(i,[e.buffer])})):Nr(e),mc=async(e,t)=>{if(dt()){if(t!=null&&t.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Ot(),new Promise((r,i)=>{At("create",[r,i]);let a={type:"create",in:{model:e,options:{...t}}},s=[];e instanceof Uint8Array&&s.push(e.buffer),Ce.postMessage(a,s)})}else return Aa(e,t)},gc=async e=>{if(dt())return Ot(),new Promise((t,r)=>{At("release",[t,r]);let i={type:"release",in:e};Ce.postMessage(i)});Oa(e)},yc=async(e,t,r,i,a,s)=>{if(dt()){if(r.some(n=>n[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(a.some(n=>n))throw new Error("pre-allocated output tensor is not supported for proxy.");return Ot(),new Promise((n,u)=>{At("run",[n,u]);let l=r,p={type:"run",in:{sessionId:e,inputIndices:t,inputs:l,outputIndices:i,options:s}};Ce.postMessage(p,Na(l))})}else return Ba(e,t,r,i,a,s)},_c=async e=>{if(dt())return Ot(),new Promise((t,r)=>{At("end-profiling",[t,r]);let i={type:"end-profiling",in:e};Ce.postMessage(i)});Ma(e)}}),Pa,bc,$c,Dm=q(()=>{Pe(),wc(),te(),si(),Zn(),Pa=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},bc=e=>{switch(e[3]){case"cpu":return new Re(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!wi(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:i,dispose:a}=e[2];return Re.fromGpuBuffer(r,{dataType:t,dims:e[1],download:i,dispose:a})}case"ml-tensor":{let t=e[0];if(!bi(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:r,download:i,dispose:a}=e[2];return Re.fromMLTensor(r,{dataType:t,dims:e[1],download:i,dispose:a})}default:throw new Error(`invalid data location: ${e[3]}`)}},$c=class{async fetchModelAndCopyToWasmMemory(e){return fc(await vi(e))}async loadModel(e,t){Ke();let r;typeof e=="string"?r=await this.fetchModelAndCopyToWasmMemory(e):r=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await mc(r,t),qe()}async dispose(){return gc(this.sessionId)}async run(e,t,r){Ke();let i=[],a=[];Object.entries(e).forEach(f=>{let g=f[0],y=f[1],_=this.inputNames.indexOf(g);if(_===-1)throw new Error(`invalid input '${g}'`);i.push(y),a.push(_)});let s=[],n=[];Object.entries(t).forEach(f=>{let g=f[0],y=f[1],_=this.outputNames.indexOf(g);if(_===-1)throw new Error(`invalid output '${g}'`);s.push(y),n.push(_)});let u=i.map((f,g)=>Pa(f,()=>`input "${this.inputNames[a[g]]}"`)),l=s.map((f,g)=>f?Pa(f,()=>`output "${this.outputNames[n[g]]}"`):null),p=await yc(this.sessionId,a,u,n,l,r),h={};for(let f=0;f<p.length;f++)h[this.outputNames[n[f]]]=s[f]??bc(p[f]);return qe(),h}startProfiling(){}endProfiling(){_c(this.sessionId)}}}),vc={};Dt(vc,{OnnxruntimeWebAssemblyBackend:()=>qa,initializeFlags:()=>Ua,wasmBackend:()=>xc});var Ua,qa,xc,Pm=q(()=>{Pe(),wc(),Dm(),Ua=()=>{(typeof ge.wasm.initTimeout!="number"||ge.wasm.initTimeout<0)&&(ge.wasm.initTimeout=0);let e=ge.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),ge.wasm.simd=!1),typeof ge.wasm.proxy!="boolean"&&(ge.wasm.proxy=!1),typeof ge.wasm.trace!="boolean"&&(ge.wasm.trace=!1),typeof ge.wasm.numThreads!="number"||!Number.isInteger(ge.wasm.numThreads)||ge.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)ge.wasm.numThreads=1;else{let t=typeof navigator>"u"?xf("node:os").cpus().length:navigator.hardwareConcurrency;ge.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},qa=class{async init(e){Ua(),await cc(),await hc(e)}async createInferenceSessionHandler(e,t){let r=new $c;return await r.loadModel(e,t),r}},xc=new qa});
2793Pe(),Pe(),Pe();var Um="1.24.3";{let e=(Pm(),Ht(vc)).wasmBackend;Pt("webgpu",e,5),Pt("webnn",e,5),Pt("cpu",e,10),Pt("wasm",e,10)}Object.defineProperty(ge.versions,"web",{value:Um,enumerable:!0});/** 2794* @license 2795* Copyright 2021 Google LLC. All Rights Reserved. 2796* Licensed under the Apache License, Version 2.0 (the "License"); 2797* you may not use this file except in compliance with the License. 2798* You may obtain a copy of the License at 2799* 2800* http://www.apache.org/licenses/LICENSE-2.0 2801* 2802* Unless required by applicable law or agreed to in writing, software 2803* distributed under the License is distributed on an "AS IS" BASIS, 2804* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2805* See the License for the specific language governing permissions and 2806* limitations under the License. 2807* ============================================================================= 2808*//** 2809 * @license 2810 * Copyright 2020 Google LLC. All Rights Reserved. 2811 * Licensed under the Apache License, Version 2.0 (the "License"); 2812 * you may not use this file except in compliance with the License. 2813 * You may obtain a copy of the License at 2814 * 2815 * http://www.apache.org/licenses/LICENSE-2.0 2816 * 2817 * Unless required by applicable law or agreed to in writing, software 2818 * distributed under the License is distributed on an "AS IS" BASIS, 2819 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2820 * See the License for the specific language governing permissions and 2821 * limitations under the License. 2822 * ============================================================================= 2823 *//** 2824 * @license 2825 * Copyright 2019 Google LLC. All Rights Reserved. 2826 * Licensed under the Apache License, Version 2.0 (the "License"); 2827 * you may not use this file except in compliance with the License. 2828 * You may obtain a copy of the License at 2829 * 2830 * http://www.apache.org/licenses/LICENSE-2.0 2831 * 2832 * Unless required by applicable law or agreed to in writing, software 2833 * distributed under the License is distributed on an "AS IS" BASIS, 2834 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2835 * See the License for the specific language governing permissions and 2836 * limitations under the License. 2837 * ============================================================================= 2838 */ge.wasm.wasmPaths="/ort/";const qm=typeof self<"u"&&self.crossOriginIsolated===!0,Wm=typeof self<"u"&&self.navigator?self.navigator.hardwareConcurrency:void 0;ge.wasm.numThreads=1;const Lm=self.postMessage.bind(self);let ur=null,Ur=Promise.resolve();self.onmessage=async e=>{const{type:t,id:r}=e.data;if(console.debug("[maiaWorker] received message",{type:t,id:r}),t==="load"){try{const i=e.data.performanceMode==="lite"?"lite":"full";ge.wasm.numThreads=qm&&i==="full"?Math.max(1,Math.min(Wm??4,4)):1,console.debug("[maiaWorker] creating session (wasm)"),ur=await ni.create(e.data.modelBuffer,{executionProviders:["wasm"]}),console.debug("[maiaWorker] session created"),self.postMessage({type:"loaded",id:r})}catch(i){console.error("[maiaWorker] load error",i),self.postMessage({type:"error",id:r,error:String(i)})}return}if(t==="release"){const i=Ur.then(async()=>{const a=ur;ur=null,await(a==null?void 0:a.release())});Ur=i.then(()=>{},()=>{});try{await i,self.postMessage({type:"released",id:r})}catch(a){self.postMessage({type:"error",id:r,error:String(a)})}return}if(t==="inference"){if(console.debug("[maiaWorker] inference request",{id:r}),!ur){console.error("[maiaWorker] inference requested but session not loaded"),self.postMessage({type:"error",id:r,error:"Session not loaded"});return}const{tokens:i,eloSelfs:a,eloOppos:s,batchSize:n}=e.data,u=Ur.then(async()=>{var g,y;const l={tokens:new Re("float32",new Float32Array(i),[n,64,12]),elo_self:new Re("float32",new Float32Array(a),[n]),elo_oppo:new Re("float32",new Float32Array(s),[n])},p=await ur.run(l),h=p.logits_move.data.slice(),f=p.logits_value.data.slice();for(const _ of Object.keys(p))(y=(g=p[_]).dispose)==null||y.call(g);return{logitsMove:h,logitsValue:f}});Ur=u.then(()=>{},()=>{});try{const{logitsMove:l,logitsValue:p}=await u;console.debug("[maiaWorker] inference done, posting result",{id:r,batchSize:n}),Lm({type:"result",id:r,logitsMove:l.buffer,logitsValue:p.buffer,batchSize:n},[l.buffer,p.buffer])}catch(l){console.error("[maiaWorker] inference error",l),self.postMessage({type:"error",id:r,error:String(l)}
2838)}}}})();
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.