@huggingface/transformers 3.0.0-alpha.8 → 3.0.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/ort-wasm-simd-threaded.jsep.wasm +0 -0
- package/dist/transformers.cjs +1 -1
- package/dist/transformers.cjs.map +1 -1
- package/dist/transformers.js +187 -187
- package/dist/transformers.js.map +1 -1
- package/dist/transformers.min.cjs +1 -1
- package/dist/transformers.min.cjs.map +1 -1
- package/dist/transformers.min.js +3 -3
- package/dist/transformers.min.js.map +1 -1
- package/dist/transformers.min.mjs +1 -1
- package/dist/transformers.min.mjs.map +1 -1
- package/dist/transformers.mjs +1 -1
- package/dist/transformers.mjs.map +1 -1
- package/package.json +2 -2
- package/src/env.js +1 -1
package/dist/transformers.js
CHANGED
|
@@ -3790,25 +3790,25 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
3790
3790
|
/* harmony export */ "TRACE_FUNC_END": () => (/* binding */ De),
|
|
3791
3791
|
/* harmony export */ "Tensor": () => (/* binding */ Pe),
|
|
3792
3792
|
/* harmony export */ "TrainingSession": () => (/* binding */ ap),
|
|
3793
|
-
/* harmony export */ "default": () => (/* binding */
|
|
3793
|
+
/* harmony export */ "default": () => (/* binding */ tx),
|
|
3794
3794
|
/* harmony export */ "env": () => (/* binding */ fe),
|
|
3795
3795
|
/* harmony export */ "registerBackend": () => (/* binding */ wt)
|
|
3796
3796
|
/* harmony export */ });
|
|
3797
3797
|
/*!
|
|
3798
|
-
* ONNX Runtime Web v1.20.0-dev.
|
|
3798
|
+
* ONNX Runtime Web v1.20.0-dev.20240821-009209e016
|
|
3799
3799
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3800
3800
|
* Licensed under the MIT License.
|
|
3801
3801
|
*/
|
|
3802
|
-
var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Object.getOwnPropertyNames;var tp=Object.prototype.hasOwnProperty;var zn=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var M=(e,t)=>()=>(e&&(t=e(e=0)),t);var Lt=(e,t)=>{for(var r in t)On(e,r,{get:t[r],enumerable:!0})},rp=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ep(t))!tp.call(e,o)&&o!==r&&On(e,o,{get:()=>t[o],enumerable:!(n=Jc(t,o))||n.enumerable});return e};var hr=e=>rp(On({},"__esModule",{value:!0}),e);var gr,bt,wt,np,yr,br=M(()=>{"use strict";gr=new Map,bt=[],wt=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let n=gr.get(e);if(n===void 0)gr.set(e,{backend:t,priority:r});else{if(n.priority>r)return;if(n.priority===r&&n.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let o=bt.indexOf(e);o!==-1&&bt.splice(o,1);for(let i=0;i<bt.length;i++)if(gr.get(bt[i]).priority<=r){bt.splice(i,0,e);return}bt.push(e)}return}throw new TypeError("not a valid backend")},np=async e=>{let t=gr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(n){return r||(t.error=`${n}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},yr=async e=>{let t=e.executionProviders||[],r=t.map(d=>typeof d=="string"?d:d.name),n=r.length===0?bt:r,o,i=[],a=new Set;for(let d of n){let p=await np(d);typeof p=="string"?i.push({name:d,err:p}):(o||(o=p),o===p&&a.add(d))}if(!o)throw new Error(`no available backend found. ERR: ${i.map(d=>`[${d.name}] ${d.err}`).join(", ")}`);for(let{name:d,err:p}of i)r.includes(d)&&console.warn(`removing requested execution provider "${d}" from session options because it is not available: ${p}`);let l=t.filter(d=>a.has(typeof d=="string"?d:d.name));return[o,new Proxy(e,{get:(d,p)=>p==="executionProviders"?l:Reflect.get(d,p)})]}});var Li=M(()=>{"use strict";br()});var Hi,Gi=M(()=>{"use strict";Hi="1.20.0-dev.20240816-b2d603abda"});var Fi,Ue,Bn=M(()=>{"use strict";Gi();Fi="warning",Ue={wasm:{},webgl:{},webgpu:{},versions:{common:Hi},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Fi=e}},get logLevel(){return Fi}};Object.defineProperty(Ue,"logLevel",{enumerable:!0})});var fe,qi=M(()=>{"use strict";Bn();fe=Ue});var Ki,ji,Yi=M(()=>{"use strict";Ki=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let n=r.getContext("2d");if(n!=null){let o,i;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[3]):(o=e.dims[3],i=e.dims[2]);let a=t?.format!==void 0?t.format:"RGB",l=t?.norm,d,p;l===void 0||l.mean===void 0?d=[255,255,255,255]:typeof l.mean=="number"?d=[l.mean,l.mean,l.mean,l.mean]:(d=[l.mean[0],l.mean[1],l.mean[2],0],l.mean[3]!==void 0&&(d[3]=l.mean[3])),l===void 0||l.bias===void 0?p=[0,0,0,0]:typeof l.bias=="number"?p=[l.bias,l.bias,l.bias,l.bias]:(p=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(p[3]=l.bias[3]));let m=i*o,u=0,h=m,w=m*2,g=-1;a==="RGBA"?(u=0,h=m,w=m*2,g=m*3):a==="RGB"?(u=0,h=m,w=m*2):a==="RBG"&&(u=0,w=m,h=m*2);for(let y=0;y<i;y++)for(let x=0;x<o;x++){let _=(e.data[u++]-p[0])*d[0],$=(e.data[h++]-p[1])*d[1],S=(e.data[w++]-p[2])*d[2],I=g===-1?255:(e.data[g++]-p[3])*d[3];n.fillStyle="rgba("+_+","+$+","+S+","+I+")",n.fillRect(x,y,1,1)}if("toDataURL"in r)return r.toDataURL();throw new Error("toDataURL is not supported")}else throw new Error("Can not access image data")},ji=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),n;if(r!=null){let o,i,a;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[1],a=e.dims[3]):(o=e.dims[3],i=e.dims[2],a=e.dims[1]);let l=t!==void 0&&t.format!==void 0?t.format:"RGB",d=t?.norm,p,m;d===void 0||d.mean===void 0?p=[255,255,255,255]:typeof d.mean=="number"?p=[d.mean,d.mean,d.mean,d.mean]:(p=[d.mean[0],d.mean[1],d.mean[2],255],d.mean[3]!==void 0&&(p[3]=d.mean[3])),d===void 0||d.bias===void 0?m=[0,0,0,0]:typeof d.bias=="number"?m=[d.bias,d.bias,d.bias,d.bias]:(m=[d.bias[0],d.bias[1],d.bias[2],0],d.bias[3]!==void 0&&(m[3]=d.bias[3]));let u=i*o;if(t!==void 0&&(t.format!==void 0&&a===4&&t.format!=="RGBA"||a===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let h=4,w=0,g=1,y=2,x=3,_=0,$=u,S=u*2,I=-1;l==="RGBA"?(_=0,$=u,S=u*2,I=u*3):l==="RGB"?(_=0,$=u,S=u*2):l==="RBG"&&(_=0,S=u,$=u*2),n=r.createImageData(o,i);for(let T=0;T<i*o;w+=h,g+=h,y+=h,x+=h,T++)n.data[w]=(e.data[_++]-m[0])*p[0],n.data[g]=(e.data[$++]-m[1])*p[1],n.data[y]=(e.data[S++]-m[2])*p[2],n.data[x]=I===-1?255:(e.data[I++]-m[3])*p[3]}else throw new Error("Can not access image data");return n}});var Dn,Xi,Zi,Qi,Ji,ea=M(()=>{"use strict";wr();Dn=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:n}=t,o=t.norm??{mean:255,bias:0},i,a;typeof o.mean=="number"?i=[o.mean,o.mean,o.mean,o.mean]:i=[o.mean[0],o.mean[1],o.mean[2],o.mean[3]??255],typeof o.bias=="number"?a=[o.bias,o.bias,o.bias,o.bias]:a=[o.bias[0],o.bias[1],o.bias[2],o.bias[3]??0];let l=t.format!==void 0?t.format:"RGBA",d=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",p=r*n,m=d==="RGBA"?new Float32Array(p*4):new Float32Array(p*3),u=4,h=0,w=1,g=2,y=3,x=0,_=p,$=p*2,S=-1;l==="RGB"&&(u=3,h=0,w=1,g=2,y=-1),d==="RGBA"?S=p*3:d==="RBG"?(x=0,$=p,_=p*2):d==="BGR"&&($=0,_=p,x=p*2);for(let T=0;T<p;T++,h+=u,g+=u,w+=u,y+=u)m[x++]=(e[h]+a[0])/i[0],m[_++]=(e[w]+a[1])/i[1],m[$++]=(e[g]+a[2])/i[2],S!==-1&&y!==-1&&(m[S++]=(e[y]+a[3])/i[3]);return d==="RGBA"?new ze("float32",m,[1,4,r,n]):new ze("float32",m,[1,3,r,n])},Xi=async(e,t)=>{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,n=typeof ImageData<"u"&&e instanceof ImageData,o=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,i=typeof e=="string",a,l=t??{},d=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},p=m=>m instanceof HTMLCanvasElement||m instanceof OffscreenCanvas?m.getContext("2d"):null;if(r){let m=d();m.width=e.width,m.height=e.height;let u=p(m);if(u!=null){let h=e.height,w=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(h=t.resizedHeight,w=t.resizedWidth),t!==void 0){if(l=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");l.tensorFormat="RGBA",l.height=h,l.width=w}else l.tensorFormat="RGBA",l.height=h,l.width=w;u.drawImage(e,0,0),a=u.getImageData(0,0,w,h).data}else throw new Error("Can not access image data")}else if(n){let m,u;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(m=t.resizedHeight,u=t.resizedWidth):(m=e.height,u=e.width),t!==void 0&&(l=t),l.format="RGBA",l.height=m,l.width=u,t!==void 0){let h=d();h.width=u,h.height=m;let w=p(h);if(w!=null)w.putImageData(e,0,0),a=w.getImageData(0,0,u,m).data;else throw new Error("Can not access image data")}else a=e.data}else if(o){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let m=d();m.width=e.width,m.height=e.height;let u=p(m);if(u!=null){let h=e.height,w=e.width;return u.drawImage(e,0,0,w,h),a=u.getImageData(0,0,w,h).data,l.height=h,l.width=w,Dn(a,l)}else throw new Error("Can not access image data")}else{if(i)return new Promise((m,u)=>{let h=d(),w=p(h);if(!e||!w)return u();let g=new Image;g.crossOrigin="Anonymous",g.src=e,g.onload=()=>{h.width=g.width,h.height=g.height,w.drawImage(g,0,0,h.width,h.height);let y=w.getImageData(0,0,h.width,h.height);l.height=h.height,l.width=h.width,m(Dn(y.data,l))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(a!==void 0)return Dn(a,l);throw new Error("Input data provided is not supported - aborted tensor creation")},Zi=(e,t)=>{let{width:r,height:n,download:o,dispose:i}=t,a=[1,n,r,4];return new ze({location:"texture",type:"float32",texture:e,dims:a,download:o,dispose:i})},Qi=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new ze({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:n,download:o,dispose:i})},Ji=(e,t,r)=>new ze({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})});var vt,Ht,ta,ra,na=M(()=>{"use strict";vt=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),Ht=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),ta=!1,ra=()=>{if(!ta){ta=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=typeof Float16Array<"u"&&Float16Array.from;e&&(vt.set("int64",BigInt64Array),Ht.set(BigInt64Array,"int64")),t&&(vt.set("uint64",BigUint64Array),Ht.set(BigUint64Array,"uint64")),r?(vt.set("float16",Float16Array),Ht.set(Float16Array,"float16")):vt.set("float16",Uint16Array)}}});var oa,ia,aa=M(()=>{"use strict";wr();oa=e=>{let t=1;for(let r=0;r<e.length;r++){let n=e[r];if(typeof n!="number"||!Number.isSafeInteger(n))throw new TypeError(`dims[${r}] must be an integer, got: ${n}`);if(n<0)throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${n}`);t*=n}return t},ia=(e,t)=>{switch(e.location){case"cpu":return new ze(e.type,e.data,t);case"cpu-pinned":return new ze({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new ze({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new ze({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var ze,wr=M(()=>{"use strict";Yi();ea();na();aa();ze=class{constructor(t,r,n){ra();let o,i;if(typeof t=="object"&&"location"in t)switch(this.dataLocation=t.location,o=t.type,i=t.dims,t.location){case"cpu-pinned":{let l=vt.get(o);if(!l)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(t.data instanceof l))throw new TypeError(`buffer should be of type ${l.name}`);this.cpuData=t.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let l,d;if(typeof t=="string")if(o=t,d=n,t==="string"){if(!Array.isArray(r))throw new TypeError("A string tensor's data must be a string array.");l=r}else{let p=vt.get(t);if(p===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if(t==="float16"&&p===Uint16Array||t==="uint4"||t==="int4")throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${p.name} as data.`);t==="uint64"||t==="int64"?l=p.from(r,BigInt):l=p.from(r)}else if(r instanceof p)l=r;else throw new TypeError(`A ${o} tensor's data must be type of ${p}`)}else if(d=r,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let p=typeof t[0];if(p==="string")o="string",l=t;else if(p==="boolean")o="bool",l=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${p}.`)}else{let p=Ht.get(t.constructor);if(p===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);o=p,l=t}if(d===void 0)d=[l.length];else if(!Array.isArray(d))throw new TypeError("A tensor's dims must be a number array");i=d,this.cpuData=l,this.dataLocation="cpu"}let a=oa(i);if(this.cpuData&&a!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(a/2)===this.cpuData.length))throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=i,this.size=a}static async fromImage(t,r){return Xi(t,r)}static fromTexture(t,r){return Zi(t,r)}static fromGpuBuffer(t,r){return Qi(t,r)}static fromPinnedBuffer(t,r,n){return Ji(t,r,n)}toDataURL(t){return Ki(this,t)}toImageData(t){return ji(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return ia(this,t)}}});var Pe,vr=M(()=>{"use strict";wr();Pe=ze});var $r,sa,Ve,De,Rn=M(()=>{"use strict";Bn();$r=(e,t)=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||console.timeStamp(`${e}::ORT::${t}`)},sa=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],n=!1;for(let o=0;o<r.length;o++){if(n&&!r[o].includes("TRACE_FUNC")){let i=`FUNC_${e}::${r[o].trim().split(" ")[1]}`;t&&(i+=`::${t}`),$r("CPU",i);return}r[o].includes("TRACE_FUNC")&&(n=!0)}},Ve=e=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||sa("BEGIN",e)},De=e=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||sa("END",e)}});var _r,ua=M(()=>{"use strict";br();vr();Rn();_r=class e{constructor(t){this.handler=t}async run(t,r,n){Ve();let o={},i={};if(typeof t!="object"||t===null||t instanceof Pe||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let a=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Pe)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");a=!1;for(let p of r){if(typeof p!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(p)===-1)throw new RangeError(`'fetches' contains invalid output name: ${p}.`);o[p]=null}if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,m=Object.getOwnPropertyNames(r);for(let u of this.outputNames)if(m.indexOf(u)!==-1){let h=r[u];(h===null||h instanceof Pe)&&(p=!0,a=!1,o[u]=h)}if(p){if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else i=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let p of this.inputNames)if(typeof t[p]>"u")throw new Error(`input '${p}' is missing in 'feeds'.`);if(a)for(let p of this.outputNames)o[p]=null;let l=await this.handler.run(t,o,i),d={};for(let p in l)if(Object.hasOwnProperty.call(l,p)){let m=l[p];m instanceof Pe?d[p]=m:d[p]=new Pe(m.type,m.data,m.dims)}return De(),d}async release(){return this.handler.dispose()}static async create(t,r,n,o){Ve();let i,a={};if(typeof t=="string"){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let m=t,u=0,h=t.byteLength;if(typeof r=="object"&&r!==null)a=r;else if(typeof r=="number"){if(u=r,!Number.isSafeInteger(u))throw new RangeError("'byteOffset' must be an integer.");if(u<0||u>=m.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${m.byteLength}).`);if(h=t.byteLength-u,typeof n=="number"){if(h=n,!Number.isSafeInteger(h))throw new RangeError("'byteLength' must be an integer.");if(h<=0||u+h>m.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${m.byteLength-u}].`);if(typeof o=="object"&&o!==null)a=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(typeof n<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");i=new Uint8Array(m,u,h)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[l,d]=await yr(a),p=await l.createInferenceSessionHandler(i,d);return De(),new e(p)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var op,da=M(()=>{"use strict";ua();op=_r});var la=M(()=>{"use strict"});var ca=M(()=>{"use strict"});var pa=M(()=>{"use strict"});var ma=M(()=>{"use strict"});var ip,xr,fa=M(()=>{"use strict";br();vr();ip="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",xr=class e{constructor(t,r,n){this.handler=t,this.hasOptimizerModel=r,this.hasEvalModel=n}get trainingInputNames(){return this.handler.inputNames}get trainingOutputNames(){return this.handler.outputNames}get evalInputNames(){if(this.hasEvalModel)return this.handler.evalInputNames;throw new Error("This training session has no evalModel loaded.")}get evalOutputNames(){if(this.hasEvalModel)return this.handler.evalOutputNames;throw new Error("This training session has no evalModel loaded.")}static async create(t,r){let n=t.evalModel||"",o=t.optimizerModel||"",i=r||{},[a,l]=await yr(i);if(a.createTrainingSessionHandler){let d=await a.createTrainingSessionHandler(t.checkpointState,t.trainModel,n,o,l);return new e(d,!!t.optimizerModel,!!t.evalModel)}else throw new Error(ip)}typeNarrowingForRunStep(t,r,n,o,i){let a={},l={};if(typeof n!="object"||n===null||n instanceof Pe||Array.isArray(n))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof o=="object"){if(o===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(o instanceof Pe)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(o)){if(o.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let p of o){if(typeof p!="string")throw new TypeError("'fetches' must be a string array or an object.");if(r.indexOf(p)===-1)throw new RangeError(`'fetches' contains invalid output name: ${p}.`);a[p]=null}if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let p=!1,m=Object.getOwnPropertyNames(o);for(let u of r)if(m.indexOf(u)!==-1){let h=o[u];(h===null||h instanceof Pe)&&(p=!0,d=!1,a[u]=h)}if(p){if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else l=o}}else if(typeof o<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let p of t)if(typeof n[p]>"u")throw new Error(`input '${p}' is missing in 'feeds'.`);if(d)for(let p of r)a[p]=null;return[a,l]}convertHandlerReturnTypeToMapOfTensors(t){let r={};for(let n in t)if(Object.hasOwnProperty.call(t,n)){let o=t[n];o instanceof Pe?r[n]=o:r[n]=new Pe(o.type,o.data,o.dims)}return r}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(t,r,n){let[o,i]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,t,r,n),a=await this.handler.runTrainStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}async runOptimizerStep(t){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(t||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(t,r,n){if(this.hasEvalModel){let[o,i]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,t,r,n),a=await this.handler.runEvalStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(t=!0){return this.handler.getParametersSize(t)}async loadParametersBuffer(t,r=!0){let n=await this.getParametersSize(r);if(t.length!==4*n)throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check.");return this.handler.loadParametersBuffer(t,r)}async getContiguousParameters(t=!0){return this.handler.getContiguousParameters(t)}async release(){return this.handler.dispose()}}});var ap,ha=M(()=>{"use strict";fa();ap=xr});var Mn={};Lt(Mn,{InferenceSession:()=>op,TRACE:()=>$r,TRACE_FUNC_BEGIN:()=>Ve,TRACE_FUNC_END:()=>De,Tensor:()=>Pe,TrainingSession:()=>ap,env:()=>fe,registerBackend:()=>wt});var qe=M(()=>{"use strict";Li();qi();da();vr();la();ca();Rn();pa();ma();ha()});var Sr=M(()=>{"use strict"});var wa={};Lt(wa,{default:()=>sp});var ya,ba,sp,va=M(()=>{"use strict";Un();$t();Gt();ya="ort-wasm-proxy-worker",ba=globalThis.self?.name===ya;ba&&(self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":Ir(r.wasm).then(()=>{Cr(r).then(()=>{postMessage({type:t})},n=>{postMessage({type:t,err:n})})},n=>{postMessage({type:t,err:n})});break;case"init-ep":{let{epName:n,env:o}=r;Tr(o,n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})});break}case"copy-from":{let{buffer:n}=r,o=Ft(n);postMessage({type:t,out:o});break}case"create":{let{model:n,options:o}=r;Ar(n,o).then(i=>{postMessage({type:t,out:i})},i=>{postMessage({type:t,err:i})});break}case"release":kr(r),postMessage({type:t});break;case"run":{let{sessionId:n,inputIndices:o,inputs:i,outputIndices:a,options:l}=r;Er(n,o,i,a,new Array(a.length).fill(null),l).then(d=>{d.some(p=>p[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:d},Or([...i,...d]))},d=>{postMessage({type:t,err:d})});break}case"end-profiling":Pr(r),postMessage({type:t});break;default:}}catch(n){postMessage({type:t,err:n})}});sp=ba?null:e=>new Worker(e??Dt,{type:"module",name:ya})});var _a={};Lt(_a,{default:()=>up});var Vn,$a,up,xa=M(()=>{"use strict";$a=(Vn=import.meta.url,async function(e={}){function t(){return ie.buffer!=ne.buffer&&Se(),ne}function r(){return ie.buffer!=ne.buffer&&Se(),Z}function n(){return ie.buffer!=ne.buffer&&Se(),ee}function o(){return ie.buffer!=ne.buffer&&Se(),K}function i(){return ie.buffer!=ne.buffer&&Se(),me}function a(){return ie.buffer!=ne.buffer&&Se(),$e}function l(){return ie.buffer!=ne.buffer&&Se(),R}function d(){return ie.buffer!=ne.buffer&&Se(),Be}var p,m,u=e,h=new Promise((s,c)=>{p=s,m=c}),w=typeof window=="object",g=typeof importScripts=="function",y=g&&self.name=="em-pthread";u.mountExternalData=(s,c)=>{s.startsWith("./")&&(s=s.substring(2)),(u.Ab||(u.Ab=new Map)).set(s,c)},u.unmountExternalData=()=>{delete u.Ab};var x=globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let _=()=>{let s=(f,b,v)=>(...C)=>{let P=Ze,B=b?.();C=f(...C);let N=b?.();return B!==N&&(f=N,v(B),b=v=null),Ze!=P?new Promise((H,J)=>{Sn={resolve:H,reject:J}}):C},c=f=>async(...b)=>{try{if(u.zb)throw Error("Session already started");let v=u.zb={Ub:b[0],errors:[]},C=await f(...b);if(u.zb!==v)throw Error("Session mismatch");u.Eb?.flush();let P=v.errors;if(0<P.length){let B=await Promise.all(P);if(B=B.filter(N=>N),0<B.length)throw Error(B.join(`
|
|
3803
|
-
`))}return C}finally{u.zb=null}};u._OrtCreateSession=s(u._OrtCreateSession,()=>u._OrtCreateSession,f=>u._OrtCreateSession=f),u._OrtRun=c(s(u._OrtRun,()=>u._OrtRun,f=>u._OrtRun=f)),u._OrtRunWithBinding=c(s(u._OrtRunWithBinding,()=>u._OrtRunWithBinding,f=>u._OrtRunWithBinding=f)),u._OrtBindInput=s(u._OrtBindInput,()=>u._OrtBindInput,f=>u._OrtBindInput=f),_=void 0};u.jsepInit=(s,c)=>{if(_?.(),s==="webgpu"){[u.Eb,u.Lb,u.Pb,u.Fb,u.Ob,u.hb,u.Qb,u.Sb,u.Mb,u.Nb,u.Rb]=c;let f=u.Eb;u.jsepRegisterBuffer=(b,v,C,P)=>f.registerBuffer(b,v,C,P),u.jsepGetBuffer=b=>f.getBuffer(b),u.jsepCreateDownloader=(b,v,C)=>f.createDownloader(b,v,C),u.jsepOnReleaseSession=b=>{f.onReleaseSession(b)},u.jsepOnRunStart=b=>f.onRunStart(b)}};var $,S,I=Object.assign({},u),T="./this.program",A=(s,c)=>{throw c},O="";(w||g)&&(g?O=self.location.href:typeof document<"u"&&document.currentScript&&(O=document.currentScript.src),Vn&&(O=Vn),O=O.startsWith("blob:")?"":O.substr(0,O.replace(/[?#].*/,"").lastIndexOf("/")+1),g&&(S=s=>{var c=new XMLHttpRequest;return c.open("GET",s,!1),c.responseType="arraybuffer",c.send(null),new Uint8Array(c.response)}),$=s=>Ao(s)?new Promise((c,f)=>{var b=new XMLHttpRequest;b.open("GET",s,!0),b.responseType="arraybuffer",b.onload=()=>{(b.status==200||b.status==0&&b.response)&&f(b.response),c(b.status)},b.onerror=c,b.send(null)}):fetch(s,{credentials:"same-origin"}).then(c=>c.ok?c.arrayBuffer():Promise.reject(Error(c.status+" : "+c.url))));var z,W=console.log.bind(console),V=console.error.bind(console),j=W,Y=V;if(Object.assign(u,I),I=null,y){let s=function(c){try{var f=c.data,b=f.cmd;if(b==="load"){let v=[];self.onmessage=C=>v.push(C),self.startWorker=()=>{postMessage({cmd:"loaded"});for(let C of v)s(C);self.onmessage=s};for(let C of f.handlers)u[C]&&!u[C].proxy||(u[C]=(...P)=>{postMessage({Db:"callHandler",Yb:C,args:P})},C=="print"&&(j=u[C]),C=="printErr"&&(Y=u[C]));ie=f.wasmMemory,Se(),ae(f.wasmModule)}else if(b==="run"){An(f.pthread_ptr,0,0,1,0,0),$n(f.pthread_ptr),Ul(),Ro(),se||(Pi(),se=!0);try{Vl(f.start_routine,f.arg)}catch(v){if(v!="unwind")throw v}}else b==="cancel"?zt()&&mr(-1):f.target!=="setimmediate"&&(b==="checkMailbox"?se&&or():b&&(Y(`worker: received unknown command ${b}`),Y(f)))}catch(v){throw Oi(),v}};var _h=s,ae,se=!1;Y=function(...c){c=c.join(" "),console.error(c)},self.alert=function(...c){postMessage({Db:"alert",text:c.join(" "),$b:zt()})},u.instantiateWasm=(c,f)=>new Promise(b=>{ae=v=>{v=new WebAssembly.Instance(v,Po()),f(v),b()}}),self.onunhandledrejection=c=>{throw c.reason||c},self.onmessage=s}u.wasmBinary&&(z=u.wasmBinary);var ie,q,ce,ne,Z,ee,K,me,$e,R,G,ue,Be,ge=!1;function Se(){var s=ie.buffer;u.HEAP8=ne=new Int8Array(s),u.HEAP16=ee=new Int16Array(s),u.HEAPU8=Z=new Uint8Array(s),u.HEAPU16=K=new Uint16Array(s),u.HEAP32=me=new Int32Array(s),u.HEAPU32=$e=new Uint32Array(s),u.HEAPF32=R=new Float32Array(s),u.HEAPF64=Be=new Float64Array(s),u.HEAP64=G=new BigInt64Array(s),u.HEAPU64=ue=new BigUint64Array(s)}if(!y){if(!((ie=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0})).buffer instanceof x))throw Y("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),Error("bad memory");Se()}var Wt=[],Te=[],Oe=[],He=0,kt=null,ht=null;function Co(){if(--He==0&&(kt!==null&&(clearInterval(kt),kt=null),ht)){var s=ht;ht=null,s()}}function Et(s){throw Y(s="Aborted("+s+")"),ge=!0,ce=1,s=new WebAssembly.RuntimeError(s+". Build with -sASSERTIONS for more info."),m(s),s}var ln,To=s=>s.startsWith("data:application/octet-stream;base64,"),Ao=s=>s.startsWith("file://");function ko(s){if(s==ln&&z)return new Uint8Array(z);if(S)return S(s);throw"both async and sync fetching of the wasm failed"}function Eo(s,c,f){return function(b){return z?Promise.resolve().then(()=>ko(b)):$(b).then(v=>new Uint8Array(v),()=>ko(b))}(s).then(b=>WebAssembly.instantiate(b,c)).then(f,b=>{Y(`failed to asynchronously prepare wasm: ${b}`),Et(b)})}function Po(){return{a:{L:Ml,xa:Rl,b:Wl,Z:No,y:Ho,na:Go,V:qo,X:Ko,oa:jo,la:Yo,ea:Xo,ka:Zo,I:Qo,W:Jo,T:ei,ma:ti,U:ri,sa:Ll,B:Gl,O:Fl,N:Kl,A:Yl,r:Xl,p:Zl,C:Ql,x:ic,P:ac,ra:sc,ha:uc,R:dc,_:lc,E:cc,ga:$n,qa:pc,t:mc,D:gc,o:yc,m:wc,c:wn,n:vc,k:xc,ya:Sc,q:Ic,g:Cc,u:Tc,l:Ac,f:kc,i:Ec,j:Pc,h:Oc,e:zc,ba:Bc,ca:Dc,da:Rc,$:yi,aa:bi,ua:Mc,d:Uc,M:Vc,F:Nc,J:Wc,v:Lc,pa:Hc,S:Gc,s:vi,w:Fc,K:qc,Q:Kc,wa:jc,va:Yc,ia:xi,ja:Si,Y:hn,z:Ii,H:Ci,fa:Ti,G:Ai,a:ie,ta:fn}}}var cn={858772:(s,c,f,b)=>{if(u===void 0||!u.Ab)return 1;if((s=Ee(s>>>0)).startsWith("./")&&(s=s.substring(2)),!(s=u.Ab.get(s)))return 2;if(b>>>=0,(c>>>=0)+(f>>>=0)>s.byteLength)return 3;try{return r().set(s.subarray(c,c+f),b>>>0),0}catch{return 4}},859273:()=>{u.Mb()},859304:()=>{u.Nb()},859333:()=>{u.Rb()},859358:s=>u.Lb(s),859391:s=>u.Pb(s),859423:(s,c,f)=>{u.Fb(s,c,f,!0)},859462:(s,c,f)=>{u.Fb(s,c,f)},859495:()=>typeof wasmOffsetConverter<"u",859552:s=>{u.hb("Abs",s,void 0)},859603:s=>{u.hb("Neg",s,void 0)},859654:s=>{u.hb("Floor",s,void 0)},859707:s=>{u.hb("Ceil",s,void 0)},859759:s=>{u.hb("Reciprocal",s,void 0)},859817:s=>{u.hb("Sqrt",s,void 0)},859869:s=>{u.hb("Exp",s,void 0)},859920:s=>{u.hb("Erf",s,void 0)},859971:s=>{u.hb("Sigmoid",s,void 0)},860026:(s,c,f)=>{u.hb("HardSigmoid",s,{alpha:c,beta:f})},860105:s=>{u.hb("Log",s,void 0)},860156:s=>{u.hb("Sin",s,void 0)},860207:s=>{u.hb("Cos",s,void 0)},860258:s=>{u.hb("Tan",s,void 0)},860309:s=>{u.hb("Asin",s,void 0)},860361:s=>{u.hb("Acos",s,void 0)},860413:s=>{u.hb("Atan",s,void 0)},860465:s=>{u.hb("Sinh",s,void 0)},860517:s=>{u.hb("Cosh",s,void 0)},860569:s=>{u.hb("Asinh",s,void 0)},860622:s=>{u.hb("Acosh",s,void 0)},860675:s=>{u.hb("Atanh",s,void 0)},860728:s=>{u.hb("Tanh",s,void 0)},860780:s=>{u.hb("Not",s,void 0)},860831:(s,c,f)=>{u.hb("Clip",s,{min:c,max:f})},860900:s=>{u.hb("Clip",s,void 0)},860952:(s,c)=>{u.hb("Elu",s,{alpha:c})},861010:s=>{u.hb("Gelu",s,void 0)},861062:s=>{u.hb("Relu",s,void 0)},861114:(s,c)=>{u.hb("LeakyRelu",s,{alpha:c})},861178:(s,c)=>{u.hb("ThresholdedRelu",s,{alpha:c})},861248:(s,c)=>{u.hb("Cast",s,{to:c})},861306:s=>{u.hb("Add",s,void 0)},861357:s=>{u.hb("Sub",s,void 0)},861408:s=>{u.hb("Mul",s,void 0)},861459:s=>{u.hb("Div",s,void 0)},861510:s=>{u.hb("Pow",s,void 0)},861561:s=>{u.hb("Equal",s,void 0)},861614:s=>{u.hb("Greater",s,void 0)},861669:s=>{u.hb("GreaterOrEqual",s,void 0)},861731:s=>{u.hb("Less",s,void 0)},861783:s=>{u.hb("LessOrEqual",s,void 0)},861842:(s,c,f,b,v)=>{u.hb("ReduceMean",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862001:(s,c,f,b,v)=>{u.hb("ReduceMax",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862159:(s,c,f,b,v)=>{u.hb("ReduceMin",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862317:(s,c,f,b,v)=>{u.hb("ReduceProd",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862476:(s,c,f,b,v)=>{u.hb("ReduceSum",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862634:(s,c,f,b,v)=>{u.hb("ReduceL1",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862791:(s,c,f,b,v)=>{u.hb("ReduceL2",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862948:(s,c,f,b,v)=>{u.hb("ReduceLogSum",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863109:(s,c,f,b,v)=>{u.hb("ReduceSumSquare",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863273:(s,c,f,b,v)=>{u.hb("ReduceLogSumExp",s,{keepDims:!!c,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863437:s=>{u.hb("Where",s,void 0)},863490:(s,c,f)=>{u.hb("Transpose",s,{perm:c?Array.from(i().subarray(c>>>0,f>>>0)):[]})},863598:(s,c,f,b)=>{u.hb("DepthToSpace",s,{blocksize:c,mode:Ee(f),format:b?"NHWC":"NCHW"})},863731:(s,c,f,b)=>{u.hb("DepthToSpace",s,{blocksize:c,mode:Ee(f),format:b?"NHWC":"NCHW"})},863864:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt)=>{u.hb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:c,dilations:[f],group:b,kernelShape:[v],pads:[C,P],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:J?Array.from(i().subarray(J>>>0,de>>>0)):[],outputShape:ye?Array.from(i().subarray(ye>>>0,Ae>>>0)):[],activation:Ee(Bt)})},864265:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:c,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(P>>>0,2+(P>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,J>>>0)):[],outputShape:de?Array.from(i().subarray(de>>>0,ye>>>0)):[],activation:Ee(Ae)})},864830:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt)=>{u.hb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:c,dilations:[f],group:b,kernelShape:[v],pads:[C,P],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:J?Array.from(i().subarray(J>>>0,de>>>0)):[],outputShape:ye?Array.from(i().subarray(ye>>>0,Ae>>>0)):[],activation:Ee(Bt)})},865231:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:c,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(P>>>0,2+(P>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,J>>>0)):[],outputShape:de?Array.from(i().subarray(de>>>0,ye>>>0)):[],activation:Ee(Ae)})},865796:(s,c)=>{u.hb("GlobalAveragePool",s,{format:c?"NHWC":"NCHW"})},865887:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("AveragePool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},866302:(s,c)=>{u.hb("GlobalAveragePool",s,{format:c?"NHWC":"NCHW"})},866393:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("AveragePool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},866808:(s,c)=>{u.hb("GlobalMaxPool",s,{format:c?"NHWC":"NCHW"})},866895:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("MaxPool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},867306:(s,c)=>{u.hb("GlobalMaxPool",s,{format:c?"NHWC":"NCHW"})},867393:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("MaxPool",s,{format:Ae?"NHWC":"NCHW",auto_pad:c,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},867804:(s,c,f,b,v)=>{u.hb("Gemm",s,{alpha:c,beta:f,transA:b,transB:v})},867908:s=>{u.hb("MatMul",s,void 0)},867962:(s,c,f,b)=>{u.hb("ArgMax",s,{keepDims:!!c,selectLastIndex:!!f,axis:b})},868070:(s,c,f,b)=>{u.hb("ArgMin",s,{keepDims:!!c,selectLastIndex:!!f,axis:b})},868178:(s,c)=>{u.hb("Softmax",s,{axis:c})},868241:(s,c)=>{u.hb("Concat",s,{axis:c})},868301:(s,c,f,b,v)=>{u.hb("Split",s,{axis:c,numOutputs:f,splitSizes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},868441:s=>{u.hb("Expand",s,void 0)},868495:(s,c)=>{u.hb("Gather",s,{axis:Number(c)})},868566:(s,c)=>{u.hb("GatherElements",s,{axis:Number(c)})},868645:(s,c,f,b,v,C,P,B,N,H,J)=>{u.hb("Resize",s,{antialias:c,axes:f?Array.from(i().subarray(f>>>0,b>>>0)):[],coordinateTransformMode:Ee(v),cubicCoeffA:C,excludeOutside:P,extrapolationValue:B,keepAspectRatioPolicy:Ee(N),mode:Ee(H),nearestMode:Ee(J)})},868991:(s,c,f,b,v,C,P)=>{u.hb("Slice",s,{starts:c?Array.from(i().subarray(c>>>0,f>>>0)):[],ends:b?Array.from(i().subarray(b>>>0,v>>>0)):[],axes:C?Array.from(i().subarray(C>>>0,P>>>0)):[]})},869207:s=>{u.hb("Tile",s,void 0)},869259:(s,c,f)=>{u.hb("InstanceNormalization",s,{epsilon:c,format:f?"NHWC":"NCHW"})},869373:(s,c,f)=>{u.hb("InstanceNormalization",s,{epsilon:c,format:f?"NHWC":"NCHW"})},869487:s=>{u.hb("Range",s,void 0)},869540:(s,c)=>{u.hb("Einsum",s,{equation:Ee(c)})},869621:(s,c,f,b,v)=>{u.hb("Pad",s,{mode:c,value:f,pads:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},869748:(s,c,f,b,v,C)=>{u.hb("BatchNormalization",s,{epsilon:c,momentum:f,spatial:!!v,trainingMode:!!b,format:C?"NHWC":"NCHW"})},869917:(s,c,f,b,v,C)=>{u.hb("BatchNormalization",s,{epsilon:c,momentum:f,spatial:!!v,trainingMode:!!b,format:C?"NHWC":"NCHW"})},870086:(s,c,f)=>{u.hb("CumSum",s,{exclusive:Number(c),reverse:Number(f)})},870183:(s,c,f)=>{u.hb("DequantizeLinear",s,{axis:c,blockSize:f})},870273:(s,c,f,b,v,C,P,B,N)=>{u.hb("Attention",s,{numHeads:c,isUnidirectional:f,maskFilterValue:b,scale:v,doRotary:C,qkvHiddenSizes:P?Array.from(i().subarray(Number(B)>>>0,Number(B)+P>>>0)):[],pastPresentShareBuffer:!!N})},870545:s=>{u.hb("BiasAdd",s,void 0)},870600:s=>{u.hb("BiasSplitGelu",s,void 0)},870661:s=>{u.hb("FastGelu",s,void 0)},870717:(s,c,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt,Qc)=>{u.hb("Conv",s,{format:de?"NHWC":"NCHW",auto_pad:c,dilations:f?Array.from(i().subarray(f>>>0,b>>>0)):[],group:v,kernel_shape:C?Array.from(i().subarray(C>>>0,P>>>0)):[],pads:B?Array.from(i().subarray(B>>>0,N>>>0)):[],strides:H?Array.from(i().subarray(H>>>0,J>>>0)):[],w_is_const:()=>!!t()[ye>>>0],activation:Ee(Ae),activation_params:Bt?Array.from(l().subarray(Bt>>>0,Qc>>>0)):[]})},871213:s=>{u.hb("Gelu",s,void 0)},871265:(s,c,f,b)=>{u.hb("GroupQueryAttention",s,{numHeads:c,kvNumHeads:f,scale:b})},871378:(s,c,f,b)=>{u.hb("LayerNormalization",s,{axis:c,epsilon:f,simplified:!!b})},871489:(s,c,f,b)=>{u.hb("LayerNormalization",s,{axis:c,epsilon:f,simplified:!!b})},871600:(s,c,f,b,v,C)=>{u.hb("MatMulNBits",s,{k:c,n:f,accuracyLevel:b,bits:v,blockSize:C})},871727:(s,c,f,b,v,C)=>{u.hb("MultiHeadAttention",s,{numHeads:c,isUnidirectional:f,maskFilterValue:b,scale:v,doRotary:C})},871886:(s,c)=>{u.hb("QuickGelu",s,{alpha:c})},871950:(s,c,f,b,v)=>{u.hb("RotaryEmbedding",s,{interleaved:!!c,numHeads:f,rotaryEmbeddingDim:b,scale:v})},872089:(s,c,f)=>{u.hb("SkipLayerNormalization",s,{epsilon:c,simplified:!!f})},872191:s=>{u.Qb(s)},872225:(s,c)=>u.Sb(s,c,u.zb.Ub,u.zb.errors),872337:(s,c,f)=>{u.hb("SkipLayerNormalization",s,{epsilon:c,simplified:!!f})}};function Rl(s,c,f){return ci(async()=>{await u.Ob(s,c,f)})}function Ml(){return typeof wasmOffsetConverter<"u"}function pn(s){this.name="ExitStatus",this.message=`Program terminated with exit(${s})`,this.status=s}var mn=s=>{s.terminate(),s.onmessage=()=>{}},Oo=s=>{dt.length==0&&(Uo(),Mo(dt[0]));var c=dt.pop();if(!c)return 6;yt.push(c),Ye[s.sb]=c,c.sb=s.sb;var f={cmd:"run",start_routine:s.Vb,arg:s.Hb,pthread_ptr:s.sb};return c.postMessage(f,s.Wb),0},gt=0,ve=(s,c,...f)=>{for(var b=2*f.length,v=Pn(),C=En(8*b),P=C>>>3,B=0;B<f.length;B++){var N=f[B];typeof N=="bigint"?(G[P+2*B]=1n,G[P+2*B+1]=N):(G[P+2*B]=0n,d()[P+2*B+1>>>0]=N)}return s=zi(s,0,b,C,c),fr(v),s};function fn(s){if(y)return ve(0,1,s);if(ce=s,!(0<gt)){for(var c of yt)mn(c);for(c of dt)mn(c);dt=[],yt=[],Ye=[],ge=!0}A(s,new pn(s))}function zo(s){if(y)return ve(1,0,s);hn(s)}var hn=s=>{if(ce=s,y)throw zo(s),"unwind";fn(s)},dt=[],yt=[],Bo=[],Ye={},Do=s=>{var c=s.sb;delete Ye[c],dt.push(s),yt.splice(yt.indexOf(s),1),s.sb=0,kn(c)};function Ro(){Bo.forEach(s=>s())}var Mo=s=>new Promise(c=>{s.onmessage=v=>{var C=(v=v.data).cmd;if(v.targetThread&&v.targetThread!=zt()){var P=Ye[v.targetThread];P?P.postMessage(v,v.transferList):Y(`Internal error! Worker sent a message "${C}" to target pthread ${v.targetThread}, but that thread no longer exists!`)}else C==="checkMailbox"?or():C==="spawnThread"?Oo(v):C==="cleanupThread"?Do(Ye[v.thread]):C==="killThread"?(v=v.thread,C=Ye[v],delete Ye[v],mn(C),kn(v),yt.splice(yt.indexOf(C),1),C.sb=0):C==="cancelThread"?Ye[v.thread].postMessage({cmd:"cancel"}):C==="loaded"?(s.loaded=!0,c(s)):C==="alert"?alert(`Thread ${v.threadId}: ${v.text}`):v.target==="setimmediate"?s.postMessage(v):C==="callHandler"?u[v.handler](...v.args):C&&Y(`worker sent an unknown command ${C}`)},s.onerror=v=>{throw Y(`worker sent an error! ${v.filename}:${v.lineno}: ${v.message}`),v};var f,b=[];for(f of[])u.propertyIsEnumerable(f)&&b.push(f);s.postMessage({cmd:"load",handlers:b,wasmMemory:ie,wasmModule:q})});function Uo(){var s=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});dt.push(s)}var nr=s=>{for(;0<s.length;)s.shift()(u)},Ul=()=>{var s=zt(),c=a()[s+52>>>2>>>0];s=a()[s+56>>>2>>>0],Di(c,c-s),fr(c)},Vl=(s,c)=>{gt=0,s=Ri(s,c),0<gt?ce=s:mr(s)};class Nl{constructor(c){this.Bb=c-24}}function Wl(s,c,f){var b=new Nl(s>>>=0);throw c>>>=0,f>>>=0,a()[b.Bb+16>>>2>>>0]=0,a()[b.Bb+4>>>2>>>0]=c,a()[b.Bb+8>>>2>>>0]=f,s}function Vo(s,c,f,b){return y?ve(2,1,s,c,f,b):No(s,c,f,b)}function No(s,c,f,b){if(s>>>=0,c>>>=0,f>>>=0,b>>>=0,x===void 0)return Y("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var v=[];return y&&v.length===0?Vo(s,c,f,b):(s={Vb:f,sb:s,Hb:b,Wb:v},y?(s.Db="spawnThread",postMessage(s,v),0):Oo(s))}var Wo=typeof TextDecoder<"u"?new TextDecoder:void 0,Lo=(s,c,f)=>{var b=(c>>>=0)+f;for(f=c;s[f]&&!(f>=b);)++f;if(16<f-c&&s.buffer&&Wo)return Wo.decode(s.buffer instanceof x?s.slice(c,f):s.subarray(c,f));for(b="";c<f;){var v=s[c++];if(128&v){var C=63&s[c++];if((224&v)==192)b+=String.fromCharCode((31&v)<<6|C);else{var P=63&s[c++];65536>(v=(240&v)==224?(15&v)<<12|C<<6|P:(7&v)<<18|C<<12|P<<6|63&s[c++])?b+=String.fromCharCode(v):(v-=65536,b+=String.fromCharCode(55296|v>>10,56320|1023&v))}}else b+=String.fromCharCode(v)}return b},Ee=(s,c)=>(s>>>=0)?Lo(r(),s,c):"";function Ho(s,c,f){return y?ve(3,1,s,c,f):0}function Go(s,c){if(y)return ve(4,1,s,c)}var Fo=s=>{for(var c=0,f=0;f<s.length;++f){var b=s.charCodeAt(f);127>=b?c++:2047>=b?c+=2:55296<=b&&57343>=b?(c+=4,++f):c+=3}return c},Pt=(s,c,f)=>{var b=r();if(c>>>=0,0<f){var v=c;f=c+f-1;for(var C=0;C<s.length;++C){var P=s.charCodeAt(C);if(55296<=P&&57343>=P&&(P=65536+((1023&P)<<10)|1023&s.charCodeAt(++C)),127>=P){if(c>=f)break;b[c++>>>0]=P}else{if(2047>=P){if(c+1>=f)break;b[c++>>>0]=192|P>>6}else{if(65535>=P){if(c+2>=f)break;b[c++>>>0]=224|P>>12}else{if(c+3>=f)break;b[c++>>>0]=240|P>>18,b[c++>>>0]=128|P>>12&63}b[c++>>>0]=128|P>>6&63}b[c++>>>0]=128|63&P}}b[c>>>0]=0,s=c-v}else s=0;return s};function qo(s,c){if(y)return ve(5,1,s,c)}function Ko(s,c,f){if(y)return ve(6,1,s,c,f)}function jo(s,c,f){return y?ve(7,1,s,c,f):0}function Yo(s,c){if(y)return ve(8,1,s,c)}function Xo(s,c,f){if(y)return ve(9,1,s,c,f)}function Zo(s,c,f,b){if(y)return ve(10,1,s,c,f,b)}function Qo(s,c,f,b){if(y)return ve(11,1,s,c,f,b)}function Jo(s,c,f,b){if(y)return ve(12,1,s,c,f,b)}function ei(s){if(y)return ve(13,1,s)}function ti(s,c){if(y)return ve(14,1,s,c)}function ri(s,c,f){if(y)return ve(15,1,s,c,f)}var ni,lt,Ll=()=>{Et("")},Xe=s=>{for(var c="";r()[s>>>0];)c+=ni[r()[s++>>>0]];return c},gn={},yn={},Hl={};function it(s,c,f={}){if(!("argPackAdvance"in c))throw new TypeError("registerType registeredInstance requires argPackAdvance");return function(b,v,C={}){var P=v.name;if(!b)throw new lt(`type "${P}" must have a positive integer typeid pointer`);if(yn.hasOwnProperty(b)){if(C.Jb)return;throw new lt(`Cannot register type '${P}' twice`)}yn[b]=v,delete Hl[b],gn.hasOwnProperty(b)&&(v=gn[b],delete gn[b],v.forEach(B=>B()))}(s,c,f)}var oi=(s,c,f)=>{switch(c){case 1:return f?b=>t()[b>>>0]:b=>r()[b>>>0];case 2:return f?b=>n()[b>>>1>>>0]:b=>o()[b>>>1>>>0];case 4:return f?b=>i()[b>>>2>>>0]:b=>a()[b>>>2>>>0];case 8:return f?b=>G[b>>>3]:b=>ue[b>>>3];default:throw new TypeError(`invalid integer width (${c}): ${s}`)}};function Gl(s,c,f){f>>>=0,it(s>>>=0,{name:c=Xe(c>>>0),fromWireType:b=>b,toWireType:function(b,v){if(typeof v!="bigint"&&typeof v!="number")throw v=v===null?"null":(b=typeof v)=="object"||b==="array"||b==="function"?v.toString():""+v,new TypeError(`Cannot convert "${v}" to ${this.name}`);return typeof v=="number"&&(v=BigInt(v)),v},argPackAdvance:ct,readValueFromPointer:oi(c,f,c.indexOf("u")==-1),yb:null})}var ct=8;function Fl(s,c,f,b){it(s>>>=0,{name:c=Xe(c>>>0),fromWireType:function(v){return!!v},toWireType:function(v,C){return C?f:b},argPackAdvance:ct,readValueFromPointer:function(v){return this.fromWireType(r()[v>>>0])},yb:null})}var bn=[],at=[];function wn(s){9<(s>>>=0)&&--at[s+1]==0&&(at[s]=void 0,bn.push(s))}var Ge=s=>{if(!s)throw new lt("Cannot use deleted val. handle = "+s);return at[s]},Fe=s=>{switch(s){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let c=bn.pop()||at.length;return at[c]=s,at[c+1]=1,c}};function vn(s){return this.fromWireType(a()[s>>>2>>>0])}var ql={name:"emscripten::val",fromWireType:s=>{var c=Ge(s);return wn(s),c},toWireType:(s,c)=>Fe(c),argPackAdvance:ct,readValueFromPointer:vn,yb:null};function Kl(s){return it(s>>>0,ql)}var jl=(s,c)=>{switch(c){case 4:return function(f){return this.fromWireType(l()[f>>>2>>>0])};case 8:return function(f){return this.fromWireType(d()[f>>>3>>>0])};default:throw new TypeError(`invalid float width (${c}): ${s}`)}};function Yl(s,c,f){f>>>=0,it(s>>>=0,{name:c=Xe(c>>>0),fromWireType:b=>b,toWireType:(b,v)=>v,argPackAdvance:ct,readValueFromPointer:jl(c,f),yb:null})}function Xl(s,c,f,b,v){if(s>>>=0,f>>>=0,c=Xe(c>>>0),v===-1&&(v=4294967295),v=B=>B,b===0){var C=32-8*f;v=B=>B<<C>>>C}var P=c.includes("unsigned")?function(B,N){return N>>>0}:function(B,N){return N};it(s,{name:c,fromWireType:v,toWireType:P,argPackAdvance:ct,readValueFromPointer:oi(c,f,b!==0),yb:null})}function Zl(s,c,f){function b(C){var P=a()[C>>>2>>>0];return C=a()[C+4>>>2>>>0],new v(t().buffer,C,P)}var v=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][c];it(s>>>=0,{name:f=Xe(f>>>0),fromWireType:b,argPackAdvance:ct,readValueFromPointer:b},{Jb:!0})}function Ql(s,c){s>>>=0;var f=(c=Xe(c>>>0))==="std::string";it(s,{name:c,fromWireType:function(b){var v=a()[b>>>2>>>0],C=b+4;if(f)for(var P=C,B=0;B<=v;++B){var N=C+B;if(B==v||r()[N>>>0]==0){if(P=Ee(P,N-P),H===void 0)var H=P;else H+=String.fromCharCode(0),H+=P;P=N+1}}else{for(H=Array(v),B=0;B<v;++B)H[B]=String.fromCharCode(r()[C+B>>>0]);H=H.join("")}return Qe(b),H},toWireType:function(b,v){v instanceof ArrayBuffer&&(v=new Uint8Array(v));var C=typeof v=="string";if(!(C||v instanceof Uint8Array||v instanceof Uint8ClampedArray||v instanceof Int8Array))throw new lt("Cannot pass non-string to std::string");var P=f&&C?Fo(v):v.length,B=pr(4+P+1),N=B+4;if(a()[B>>>2>>>0]=P,f&&C)Pt(v,N,P+1);else if(C)for(C=0;C<P;++C){var H=v.charCodeAt(C);if(255<H)throw Qe(N),new lt("String has UTF-16 code units that do not fit in 8 bits");r()[N+C>>>0]=H}else for(C=0;C<P;++C)r()[N+C>>>0]=v[C];return b!==null&&b.push(Qe,B),B},argPackAdvance:ct,readValueFromPointer:vn,yb(b){Qe(b)}})}var ii=typeof TextDecoder<"u"?new TextDecoder("utf-16le"):void 0,Jl=(s,c)=>{for(var f=s>>1,b=f+c/2;!(f>=b)&&o()[f>>>0];)++f;if(32<(f<<=1)-s&&ii)return ii.decode(r().slice(s,f));for(f="",b=0;!(b>=c/2);++b){var v=n()[s+2*b>>>1>>>0];if(v==0)break;f+=String.fromCharCode(v)}return f},ec=(s,c,f)=>{if(f??=2147483647,2>f)return 0;var b=c;f=(f-=2)<2*s.length?f/2:s.length;for(var v=0;v<f;++v){var C=s.charCodeAt(v);n()[c>>>1>>>0]=C,c+=2}return n()[c>>>1>>>0]=0,c-b},tc=s=>2*s.length,rc=(s,c)=>{for(var f=0,b="";!(f>=c/4);){var v=i()[s+4*f>>>2>>>0];if(v==0)break;++f,65536<=v?(v-=65536,b+=String.fromCharCode(55296|v>>10,56320|1023&v)):b+=String.fromCharCode(v)}return b},nc=(s,c,f)=>{if(c>>>=0,f??=2147483647,4>f)return 0;var b=c;f=b+f-4;for(var v=0;v<s.length;++v){var C=s.charCodeAt(v);if(55296<=C&&57343>=C&&(C=65536+((1023&C)<<10)|1023&s.charCodeAt(++v)),i()[c>>>2>>>0]=C,(c+=4)+4>f)break}return i()[c>>>2>>>0]=0,c-b},oc=s=>{for(var c=0,f=0;f<s.length;++f){var b=s.charCodeAt(f);55296<=b&&57343>=b&&++f,c+=4}return c};function ic(s,c,f){if(s>>>=0,c>>>=0,f=Xe(f>>>=0),c===2)var b=Jl,v=ec,C=tc,P=B=>o()[B>>>1>>>0];else c===4&&(b=rc,v=nc,C=oc,P=B=>a()[B>>>2>>>0]);it(s,{name:f,fromWireType:B=>{for(var N,H=a()[B>>>2>>>0],J=B+4,de=0;de<=H;++de){var ye=B+4+de*c;de!=H&&P(ye)!=0||(J=b(J,ye-J),N===void 0?N=J:(N+=String.fromCharCode(0),N+=J),J=ye+c)}return Qe(B),N},toWireType:(B,N)=>{if(typeof N!="string")throw new lt(`Cannot pass non-string to C++ string type ${f}`);var H=C(N),J=pr(4+H+c);return a()[J>>>2>>>0]=H/c,v(N,J+4,H+c),B!==null&&B.push(Qe,J),J},argPackAdvance:ct,readValueFromPointer:vn,yb(B){Qe(B)}})}function ac(s,c){it(s>>>=0,{Kb:!0,name:c=Xe(c>>>0),argPackAdvance:0,fromWireType:()=>{},toWireType:()=>{}})}var sc=()=>1;function uc(s){An(s>>>0,!g,1,!w,131072,!1),Ro()}var ai=s=>{if(!ge)try{if(s(),!(0<gt))try{y?mr(ce):hn(ce)}catch(c){c instanceof pn||c=="unwind"||A(1,c)}}catch(c){c instanceof pn||c=="unwind"||A(1,c)}};function $n(s){s>>>=0,typeof Atomics.Xb=="function"&&(Atomics.Xb(i(),s>>>2,s).value.then(or),s+=128,Atomics.store(i(),s>>>2,1))}var or=()=>{var s=zt();s&&($n(s),ai(Bi))};function dc(s,c){(s>>>=0)==c>>>0?setTimeout(or):y?postMessage({targetThread:s,cmd:"checkMailbox"}):(s=Ye[s])&&s.postMessage({cmd:"checkMailbox"})}var _n=[];function lc(s,c,f,b,v){for(c>>>=0,b/=2,_n.length=b,f=v>>>0>>>3,v=0;v<b;v++)_n[v]=G[f+2*v]?G[f+2*v+1]:d()[f+2*v+1>>>0];return(c?cn[c]:Zc[s])(..._n)}function cc(s){s>>>=0,y?postMessage({cmd:"cleanupThread",thread:s}):Do(Ye[s])}function pc(s){}var xn=(s,c)=>{var f=yn[s];if(f===void 0)throw s=Ei(s),f=Xe(s),Qe(s),new lt(`${c} has unknown type ${f}`);return f},si=(s,c,f)=>{var b=[];return s=s.toWireType(b,f),b.length&&(a()[c>>>2>>>0]=Fe(b)),s};function mc(s,c,f){return c>>>=0,f>>>=0,s=Ge(s>>>0),c=xn(c,"emval::as"),si(c,f,s)}var ir=s=>{try{s()}catch(c){Et(c)}},pt=0,Ze=null,ui=0,ar=[],di={},li={},fc=0,Sn=null,hc=[];function ci(s){return function(c){if(!ge){if(pt===0){var f=!1,b=!1;c((v=0)=>{if(!ge&&(ui=v,f=!0,b)){pt=2,ir(()=>Vi(Ze)),typeof Browser<"u"&&Browser.Cb.Ib&&Browser.Cb.resume(),v=!1;try{var C=function(){var N=i()[Ze+8>>>2>>>0];return N=F[li[N]],--gt,N()}()}catch(N){C=N,v=!0}var P=!1;if(!Ze){var B=Sn;B&&(Sn=null,(v?B.reject:B.resolve)(C),P=!0)}if(v&&!P)throw C}}),b=!0,f||(pt=1,Ze=function(){var v=pr(65548),C=v+12;a()[v>>>2>>>0]=C,a()[v+4>>>2>>>0]=C+65536,C=ar[0];var P=di[C];return P===void 0&&(P=fc++,di[C]=P,li[P]=C),C=P,i()[v+8>>>2>>>0]=C,v}(),typeof Browser<"u"&&Browser.Cb.Ib&&Browser.Cb.pause(),ir(()=>Mi(Ze)))}else pt===2?(pt=0,ir(Ni),Qe(Ze),Ze=null,hc.forEach(ai)):Et(`invalid state: ${pt}`);return ui}}(c=>{s().then(c)})}function gc(s){return s>>>=0,ci(()=>(s=Ge(s)).then(Fe))}var sr=[];function yc(s,c,f,b){return f>>>=0,b>>>=0,(s=sr[s>>>0])(null,c=Ge(c>>>0),f,b)}var bc={},ur=s=>{var c=bc[s];return c===void 0?Xe(s):c};function wc(s,c,f,b,v){return f>>>=0,b>>>=0,v>>>=0,(s=sr[s>>>0])(c=Ge(c>>>0),c[f=ur(f)],b,v)}var pi=()=>typeof globalThis=="object"?globalThis:Function("return this")();function vc(s){return(s>>>=0)==0?Fe(pi()):(s=ur(s),Fe(pi()[s]))}var $c=s=>{var c=sr.length;return sr.push(s),c},_c=(s,c)=>{for(var f=Array(s),b=0;b<s;++b)f[b]=xn(a()[c+4*b>>>2>>>0],"parameter "+b);return f},mi=(s,c)=>Object.defineProperty(c,"name",{value:s});function xc(s,c,f){var b=(c=_c(s,c>>>0)).shift();s--;var v=`return function (obj, func, destructorsRef, args) {
|
|
3804
|
-
`,C=0,P=[];f===0&&P.push("obj");for(var B=["retType"],N=[b],H=0;H<s;++H)P.push("arg"+H),B.push("argType"+H),N.push(
|
|
3805
|
-
`,C+=
|
|
3802
|
+
var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Object.getOwnPropertyNames;var tp=Object.prototype.hasOwnProperty;var zn=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var M=(e,t)=>()=>(e&&(t=e(e=0)),t);var Lt=(e,t)=>{for(var r in t)On(e,r,{get:t[r],enumerable:!0})},rp=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ep(t))!tp.call(e,o)&&o!==r&&On(e,o,{get:()=>t[o],enumerable:!(n=Jc(t,o))||n.enumerable});return e};var hr=e=>rp(On({},"__esModule",{value:!0}),e);var gr,bt,wt,np,yr,br=M(()=>{"use strict";gr=new Map,bt=[],wt=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let n=gr.get(e);if(n===void 0)gr.set(e,{backend:t,priority:r});else{if(n.priority>r)return;if(n.priority===r&&n.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let o=bt.indexOf(e);o!==-1&&bt.splice(o,1);for(let i=0;i<bt.length;i++)if(gr.get(bt[i]).priority<=r){bt.splice(i,0,e);return}bt.push(e)}return}throw new TypeError("not a valid backend")},np=async e=>{let t=gr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(n){return r||(t.error=`${n}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},yr=async e=>{let t=e.executionProviders||[],r=t.map(d=>typeof d=="string"?d:d.name),n=r.length===0?bt:r,o,i=[],a=new Set;for(let d of n){let c=await np(d);typeof c=="string"?i.push({name:d,err:c}):(o||(o=c),o===c&&a.add(d))}if(!o)throw new Error(`no available backend found. ERR: ${i.map(d=>`[${d.name}] ${d.err}`).join(", ")}`);for(let{name:d,err:c}of i)r.includes(d)&&console.warn(`removing requested execution provider "${d}" from session options because it is not available: ${c}`);let l=t.filter(d=>a.has(typeof d=="string"?d:d.name));return[o,new Proxy(e,{get:(d,c)=>c==="executionProviders"?l:Reflect.get(d,c)})]}});var Li=M(()=>{"use strict";br()});var Hi,Gi=M(()=>{"use strict";Hi="1.20.0-dev.20240816-b2d603abda"});var Fi,Ue,Bn=M(()=>{"use strict";Gi();Fi="warning",Ue={wasm:{},webgl:{},webgpu:{},versions:{common:Hi},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Fi=e}},get logLevel(){return Fi}};Object.defineProperty(Ue,"logLevel",{enumerable:!0})});var fe,qi=M(()=>{"use strict";Bn();fe=Ue});var Ki,ji,Yi=M(()=>{"use strict";Ki=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let n=r.getContext("2d");if(n!=null){let o,i;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[3]):(o=e.dims[3],i=e.dims[2]);let a=t?.format!==void 0?t.format:"RGB",l=t?.norm,d,c;l===void 0||l.mean===void 0?d=[255,255,255,255]:typeof l.mean=="number"?d=[l.mean,l.mean,l.mean,l.mean]:(d=[l.mean[0],l.mean[1],l.mean[2],0],l.mean[3]!==void 0&&(d[3]=l.mean[3])),l===void 0||l.bias===void 0?c=[0,0,0,0]:typeof l.bias=="number"?c=[l.bias,l.bias,l.bias,l.bias]:(c=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(c[3]=l.bias[3]));let m=i*o,u=0,h=m,w=m*2,g=-1;a==="RGBA"?(u=0,h=m,w=m*2,g=m*3):a==="RGB"?(u=0,h=m,w=m*2):a==="RBG"&&(u=0,w=m,h=m*2);for(let y=0;y<i;y++)for(let x=0;x<o;x++){let _=(e.data[u++]-c[0])*d[0],$=(e.data[h++]-c[1])*d[1],S=(e.data[w++]-c[2])*d[2],I=g===-1?255:(e.data[g++]-c[3])*d[3];n.fillStyle="rgba("+_+","+$+","+S+","+I+")",n.fillRect(x,y,1,1)}if("toDataURL"in r)return r.toDataURL();throw new Error("toDataURL is not supported")}else throw new Error("Can not access image data")},ji=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),n;if(r!=null){let o,i,a;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[1],a=e.dims[3]):(o=e.dims[3],i=e.dims[2],a=e.dims[1]);let l=t!==void 0&&t.format!==void 0?t.format:"RGB",d=t?.norm,c,m;d===void 0||d.mean===void 0?c=[255,255,255,255]:typeof d.mean=="number"?c=[d.mean,d.mean,d.mean,d.mean]:(c=[d.mean[0],d.mean[1],d.mean[2],255],d.mean[3]!==void 0&&(c[3]=d.mean[3])),d===void 0||d.bias===void 0?m=[0,0,0,0]:typeof d.bias=="number"?m=[d.bias,d.bias,d.bias,d.bias]:(m=[d.bias[0],d.bias[1],d.bias[2],0],d.bias[3]!==void 0&&(m[3]=d.bias[3]));let u=i*o;if(t!==void 0&&(t.format!==void 0&&a===4&&t.format!=="RGBA"||a===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let h=4,w=0,g=1,y=2,x=3,_=0,$=u,S=u*2,I=-1;l==="RGBA"?(_=0,$=u,S=u*2,I=u*3):l==="RGB"?(_=0,$=u,S=u*2):l==="RBG"&&(_=0,S=u,$=u*2),n=r.createImageData(o,i);for(let T=0;T<i*o;w+=h,g+=h,y+=h,x+=h,T++)n.data[w]=(e.data[_++]-m[0])*c[0],n.data[g]=(e.data[$++]-m[1])*c[1],n.data[y]=(e.data[S++]-m[2])*c[2],n.data[x]=I===-1?255:(e.data[I++]-m[3])*c[3]}else throw new Error("Can not access image data");return n}});var Dn,Xi,Zi,Qi,Ji,ea=M(()=>{"use strict";wr();Dn=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:n}=t,o=t.norm??{mean:255,bias:0},i,a;typeof o.mean=="number"?i=[o.mean,o.mean,o.mean,o.mean]:i=[o.mean[0],o.mean[1],o.mean[2],o.mean[3]??255],typeof o.bias=="number"?a=[o.bias,o.bias,o.bias,o.bias]:a=[o.bias[0],o.bias[1],o.bias[2],o.bias[3]??0];let l=t.format!==void 0?t.format:"RGBA",d=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",c=r*n,m=d==="RGBA"?new Float32Array(c*4):new Float32Array(c*3),u=4,h=0,w=1,g=2,y=3,x=0,_=c,$=c*2,S=-1;l==="RGB"&&(u=3,h=0,w=1,g=2,y=-1),d==="RGBA"?S=c*3:d==="RBG"?(x=0,$=c,_=c*2):d==="BGR"&&($=0,_=c,x=c*2);for(let T=0;T<c;T++,h+=u,g+=u,w+=u,y+=u)m[x++]=(e[h]+a[0])/i[0],m[_++]=(e[w]+a[1])/i[1],m[$++]=(e[g]+a[2])/i[2],S!==-1&&y!==-1&&(m[S++]=(e[y]+a[3])/i[3]);return d==="RGBA"?new ze("float32",m,[1,4,r,n]):new ze("float32",m,[1,3,r,n])},Xi=async(e,t)=>{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,n=typeof ImageData<"u"&&e instanceof ImageData,o=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,i=typeof e=="string",a,l=t??{},d=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},c=m=>m instanceof HTMLCanvasElement||m instanceof OffscreenCanvas?m.getContext("2d"):null;if(r){let m=d();m.width=e.width,m.height=e.height;let u=c(m);if(u!=null){let h=e.height,w=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(h=t.resizedHeight,w=t.resizedWidth),t!==void 0){if(l=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");l.tensorFormat="RGBA",l.height=h,l.width=w}else l.tensorFormat="RGBA",l.height=h,l.width=w;u.drawImage(e,0,0),a=u.getImageData(0,0,w,h).data}else throw new Error("Can not access image data")}else if(n){let m,u;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(m=t.resizedHeight,u=t.resizedWidth):(m=e.height,u=e.width),t!==void 0&&(l=t),l.format="RGBA",l.height=m,l.width=u,t!==void 0){let h=d();h.width=u,h.height=m;let w=c(h);if(w!=null)w.putImageData(e,0,0),a=w.getImageData(0,0,u,m).data;else throw new Error("Can not access image data")}else a=e.data}else if(o){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let m=d();m.width=e.width,m.height=e.height;let u=c(m);if(u!=null){let h=e.height,w=e.width;return u.drawImage(e,0,0,w,h),a=u.getImageData(0,0,w,h).data,l.height=h,l.width=w,Dn(a,l)}else throw new Error("Can not access image data")}else{if(i)return new Promise((m,u)=>{let h=d(),w=c(h);if(!e||!w)return u();let g=new Image;g.crossOrigin="Anonymous",g.src=e,g.onload=()=>{h.width=g.width,h.height=g.height,w.drawImage(g,0,0,h.width,h.height);let y=w.getImageData(0,0,h.width,h.height);l.height=h.height,l.width=h.width,m(Dn(y.data,l))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(a!==void 0)return Dn(a,l);throw new Error("Input data provided is not supported - aborted tensor creation")},Zi=(e,t)=>{let{width:r,height:n,download:o,dispose:i}=t,a=[1,n,r,4];return new ze({location:"texture",type:"float32",texture:e,dims:a,download:o,dispose:i})},Qi=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new ze({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:n,download:o,dispose:i})},Ji=(e,t,r)=>new ze({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})});var vt,Ht,ta,ra,na=M(()=>{"use strict";vt=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),Ht=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),ta=!1,ra=()=>{if(!ta){ta=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=typeof Float16Array<"u"&&Float16Array.from;e&&(vt.set("int64",BigInt64Array),Ht.set(BigInt64Array,"int64")),t&&(vt.set("uint64",BigUint64Array),Ht.set(BigUint64Array,"uint64")),r?(vt.set("float16",Float16Array),Ht.set(Float16Array,"float16")):vt.set("float16",Uint16Array)}}});var oa,ia,aa=M(()=>{"use strict";wr();oa=e=>{let t=1;for(let r=0;r<e.length;r++){let n=e[r];if(typeof n!="number"||!Number.isSafeInteger(n))throw new TypeError(`dims[${r}] must be an integer, got: ${n}`);if(n<0)throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${n}`);t*=n}return t},ia=(e,t)=>{switch(e.location){case"cpu":return new ze(e.type,e.data,t);case"cpu-pinned":return new ze({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new ze({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new ze({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var ze,wr=M(()=>{"use strict";Yi();ea();na();aa();ze=class{constructor(t,r,n){ra();let o,i;if(typeof t=="object"&&"location"in t)switch(this.dataLocation=t.location,o=t.type,i=t.dims,t.location){case"cpu-pinned":{let l=vt.get(o);if(!l)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(t.data instanceof l))throw new TypeError(`buffer should be of type ${l.name}`);this.cpuData=t.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let l,d;if(typeof t=="string")if(o=t,d=n,t==="string"){if(!Array.isArray(r))throw new TypeError("A string tensor's data must be a string array.");l=r}else{let c=vt.get(t);if(c===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if(t==="float16"&&c===Uint16Array||t==="uint4"||t==="int4")throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${c.name} as data.`);t==="uint64"||t==="int64"?l=c.from(r,BigInt):l=c.from(r)}else if(r instanceof c)l=r;else throw new TypeError(`A ${o} tensor's data must be type of ${c}`)}else if(d=r,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let c=typeof t[0];if(c==="string")o="string",l=t;else if(c==="boolean")o="bool",l=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${c}.`)}else{let c=Ht.get(t.constructor);if(c===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);o=c,l=t}if(d===void 0)d=[l.length];else if(!Array.isArray(d))throw new TypeError("A tensor's dims must be a number array");i=d,this.cpuData=l,this.dataLocation="cpu"}let a=oa(i);if(this.cpuData&&a!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(a/2)===this.cpuData.length))throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=i,this.size=a}static async fromImage(t,r){return Xi(t,r)}static fromTexture(t,r){return Zi(t,r)}static fromGpuBuffer(t,r){return Qi(t,r)}static fromPinnedBuffer(t,r,n){return Ji(t,r,n)}toDataURL(t){return Ki(this,t)}toImageData(t){return ji(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return ia(this,t)}}});var Pe,vr=M(()=>{"use strict";wr();Pe=ze});var $r,sa,Ve,De,Rn=M(()=>{"use strict";Bn();$r=(e,t)=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||console.timeStamp(`${e}::ORT::${t}`)},sa=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],n=!1;for(let o=0;o<r.length;o++){if(n&&!r[o].includes("TRACE_FUNC")){let i=`FUNC_${e}::${r[o].trim().split(" ")[1]}`;t&&(i+=`::${t}`),$r("CPU",i);return}r[o].includes("TRACE_FUNC")&&(n=!0)}},Ve=e=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||sa("BEGIN",e)},De=e=>{(typeof Ue.trace>"u"?!Ue.wasm.trace:!Ue.trace)||sa("END",e)}});var _r,ua=M(()=>{"use strict";br();vr();Rn();_r=class e{constructor(t){this.handler=t}async run(t,r,n){Ve();let o={},i={};if(typeof t!="object"||t===null||t instanceof Pe||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let a=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Pe)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");a=!1;for(let c of r){if(typeof c!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(c)===-1)throw new RangeError(`'fetches' contains invalid output name: ${c}.`);o[c]=null}if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else{let c=!1,m=Object.getOwnPropertyNames(r);for(let u of this.outputNames)if(m.indexOf(u)!==-1){let h=r[u];(h===null||h instanceof Pe)&&(c=!0,a=!1,o[u]=h)}if(c){if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else i=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let c of this.inputNames)if(typeof t[c]>"u")throw new Error(`input '${c}' is missing in 'feeds'.`);if(a)for(let c of this.outputNames)o[c]=null;let l=await this.handler.run(t,o,i),d={};for(let c in l)if(Object.hasOwnProperty.call(l,c)){let m=l[c];m instanceof Pe?d[c]=m:d[c]=new Pe(m.type,m.data,m.dims)}return De(),d}async release(){return this.handler.dispose()}static async create(t,r,n,o){Ve();let i,a={};if(typeof t=="string"){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let m=t,u=0,h=t.byteLength;if(typeof r=="object"&&r!==null)a=r;else if(typeof r=="number"){if(u=r,!Number.isSafeInteger(u))throw new RangeError("'byteOffset' must be an integer.");if(u<0||u>=m.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${m.byteLength}).`);if(h=t.byteLength-u,typeof n=="number"){if(h=n,!Number.isSafeInteger(h))throw new RangeError("'byteLength' must be an integer.");if(h<=0||u+h>m.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${m.byteLength-u}].`);if(typeof o=="object"&&o!==null)a=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(typeof n<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");i=new Uint8Array(m,u,h)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[l,d]=await yr(a),c=await l.createInferenceSessionHandler(i,d);return De(),new e(c)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var op,da=M(()=>{"use strict";ua();op=_r});var la=M(()=>{"use strict"});var ca=M(()=>{"use strict"});var pa=M(()=>{"use strict"});var ma=M(()=>{"use strict"});var ip,xr,fa=M(()=>{"use strict";br();vr();ip="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",xr=class e{constructor(t,r,n){this.handler=t,this.hasOptimizerModel=r,this.hasEvalModel=n}get trainingInputNames(){return this.handler.inputNames}get trainingOutputNames(){return this.handler.outputNames}get evalInputNames(){if(this.hasEvalModel)return this.handler.evalInputNames;throw new Error("This training session has no evalModel loaded.")}get evalOutputNames(){if(this.hasEvalModel)return this.handler.evalOutputNames;throw new Error("This training session has no evalModel loaded.")}static async create(t,r){let n=t.evalModel||"",o=t.optimizerModel||"",i=r||{},[a,l]=await yr(i);if(a.createTrainingSessionHandler){let d=await a.createTrainingSessionHandler(t.checkpointState,t.trainModel,n,o,l);return new e(d,!!t.optimizerModel,!!t.evalModel)}else throw new Error(ip)}typeNarrowingForRunStep(t,r,n,o,i){let a={},l={};if(typeof n!="object"||n===null||n instanceof Pe||Array.isArray(n))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let d=!0;if(typeof o=="object"){if(o===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(o instanceof Pe)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(o)){if(o.length===0)throw new TypeError("'fetches' cannot be an empty array.");d=!1;for(let c of o){if(typeof c!="string")throw new TypeError("'fetches' must be a string array or an object.");if(r.indexOf(c)===-1)throw new RangeError(`'fetches' contains invalid output name: ${c}.`);a[c]=null}if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let c=!1,m=Object.getOwnPropertyNames(o);for(let u of r)if(m.indexOf(u)!==-1){let h=o[u];(h===null||h instanceof Pe)&&(c=!0,d=!1,a[u]=h)}if(c){if(typeof i=="object"&&i!==null)l=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else l=o}}else if(typeof o<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let c of t)if(typeof n[c]>"u")throw new Error(`input '${c}' is missing in 'feeds'.`);if(d)for(let c of r)a[c]=null;return[a,l]}convertHandlerReturnTypeToMapOfTensors(t){let r={};for(let n in t)if(Object.hasOwnProperty.call(t,n)){let o=t[n];o instanceof Pe?r[n]=o:r[n]=new Pe(o.type,o.data,o.dims)}return r}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(t,r,n){let[o,i]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,t,r,n),a=await this.handler.runTrainStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}async runOptimizerStep(t){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(t||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(t,r,n){if(this.hasEvalModel){let[o,i]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,t,r,n),a=await this.handler.runEvalStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(t=!0){return this.handler.getParametersSize(t)}async loadParametersBuffer(t,r=!0){let n=await this.getParametersSize(r);if(t.length!==4*n)throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check.");return this.handler.loadParametersBuffer(t,r)}async getContiguousParameters(t=!0){return this.handler.getContiguousParameters(t)}async release(){return this.handler.dispose()}}});var ap,ha=M(()=>{"use strict";fa();ap=xr});var Mn={};Lt(Mn,{InferenceSession:()=>op,TRACE:()=>$r,TRACE_FUNC_BEGIN:()=>Ve,TRACE_FUNC_END:()=>De,Tensor:()=>Pe,TrainingSession:()=>ap,env:()=>fe,registerBackend:()=>wt});var qe=M(()=>{"use strict";Li();qi();da();vr();la();ca();Rn();pa();ma();ha()});var Sr=M(()=>{"use strict"});var wa={};Lt(wa,{default:()=>sp});var ya,ba,sp,va=M(()=>{"use strict";Un();$t();Gt();ya="ort-wasm-proxy-worker",ba=globalThis.self?.name===ya;ba&&(self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":Ir(r.wasm).then(()=>{Cr(r).then(()=>{postMessage({type:t})},n=>{postMessage({type:t,err:n})})},n=>{postMessage({type:t,err:n})});break;case"init-ep":{let{epName:n,env:o}=r;Tr(o,n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})});break}case"copy-from":{let{buffer:n}=r,o=Ft(n);postMessage({type:t,out:o});break}case"create":{let{model:n,options:o}=r;Ar(n,o).then(i=>{postMessage({type:t,out:i})},i=>{postMessage({type:t,err:i})});break}case"release":kr(r),postMessage({type:t});break;case"run":{let{sessionId:n,inputIndices:o,inputs:i,outputIndices:a,options:l}=r;Er(n,o,i,a,new Array(a.length).fill(null),l).then(d=>{d.some(c=>c[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:d},Or([...i,...d]))},d=>{postMessage({type:t,err:d})});break}case"end-profiling":Pr(r),postMessage({type:t});break;default:}}catch(n){postMessage({type:t,err:n})}});sp=ba?null:e=>new Worker(e??Dt,{type:"module",name:ya})});var _a={};Lt(_a,{default:()=>up});var Vn,$a,up,xa=M(()=>{"use strict";$a=(Vn=import.meta.url,async function(e={}){function t(){return ie.buffer!=oe.buffer&&Se(),oe}function r(){return ie.buffer!=oe.buffer&&Se(),Z}function n(){return ie.buffer!=oe.buffer&&Se(),ee}function o(){return ie.buffer!=oe.buffer&&Se(),K}function i(){return ie.buffer!=oe.buffer&&Se(),me}function a(){return ie.buffer!=oe.buffer&&Se(),$e}function l(){return ie.buffer!=oe.buffer&&Se(),R}function d(){return ie.buffer!=oe.buffer&&Se(),Be}var c,m,u=e,h=new Promise((s,p)=>{c=s,m=p}),w=typeof window=="object",g=typeof importScripts=="function",y=g&&self.name=="em-pthread";u.mountExternalData=(s,p)=>{s.startsWith("./")&&(s=s.substring(2)),(u.Ab||(u.Ab=new Map)).set(s,p)},u.unmountExternalData=()=>{delete u.Ab};var x=globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let _=()=>{let s=(f,b,v)=>(...C)=>{let P=Ze,B=b?.();C=f(...C);let N=b?.();return B!==N&&(f=N,v(B),b=v=null),Ze!=P?new Promise((H,J)=>{Sn={resolve:H,reject:J}}):C},p=f=>async(...b)=>{try{if(u.zb)throw Error("Session already started");let v=u.zb={Ub:b[0],errors:[]},C=await f(...b);if(u.zb!==v)throw Error("Session mismatch");u.Eb?.flush();let P=v.errors;if(0<P.length){let B=await Promise.all(P);if(B=B.filter(N=>N),0<B.length)throw Error(B.join(`
|
|
3803
|
+
`))}return C}finally{u.zb=null}};u._OrtCreateSession=s(u._OrtCreateSession,()=>u._OrtCreateSession,f=>u._OrtCreateSession=f),u._OrtRun=p(s(u._OrtRun,()=>u._OrtRun,f=>u._OrtRun=f)),u._OrtRunWithBinding=p(s(u._OrtRunWithBinding,()=>u._OrtRunWithBinding,f=>u._OrtRunWithBinding=f)),u._OrtBindInput=s(u._OrtBindInput,()=>u._OrtBindInput,f=>u._OrtBindInput=f),_=void 0};u.jsepInit=(s,p)=>{if(_?.(),s==="webgpu"){[u.Eb,u.Lb,u.Pb,u.Fb,u.Ob,u.hb,u.Qb,u.Sb,u.Mb,u.Nb,u.Rb]=p;let f=u.Eb;u.jsepRegisterBuffer=(b,v,C,P)=>f.registerBuffer(b,v,C,P),u.jsepGetBuffer=b=>f.getBuffer(b),u.jsepCreateDownloader=(b,v,C)=>f.createDownloader(b,v,C),u.jsepOnReleaseSession=b=>{f.onReleaseSession(b)},u.jsepOnRunStart=b=>f.onRunStart(b)}};var $,S,I=Object.assign({},u),T="./this.program",A=(s,p)=>{throw p},O="";(w||g)&&(g?O=self.location.href:typeof document<"u"&&document.currentScript&&(O=document.currentScript.src),Vn&&(O=Vn),O=O.startsWith("blob:")?"":O.substr(0,O.replace(/[?#].*/,"").lastIndexOf("/")+1),g&&(S=s=>{var p=new XMLHttpRequest;return p.open("GET",s,!1),p.responseType="arraybuffer",p.send(null),new Uint8Array(p.response)}),$=s=>Ao(s)?new Promise((p,f)=>{var b=new XMLHttpRequest;b.open("GET",s,!0),b.responseType="arraybuffer",b.onload=()=>{(b.status==200||b.status==0&&b.response)&&f(b.response),p(b.status)},b.onerror=p,b.send(null)}):fetch(s,{credentials:"same-origin"}).then(p=>p.ok?p.arrayBuffer():Promise.reject(Error(p.status+" : "+p.url))));var z,W=console.log.bind(console),V=console.error.bind(console),j=W,Y=V;if(Object.assign(u,I),I=null,y){let s=function(p){try{var f=p.data,b=f.cmd;if(b==="load"){let v=[];self.onmessage=C=>v.push(C),self.startWorker=()=>{postMessage({cmd:"loaded"});for(let C of v)s(C);self.onmessage=s};for(let C of f.handlers)u[C]&&!u[C].proxy||(u[C]=(...P)=>{postMessage({Db:"callHandler",Yb:C,args:P})},C=="print"&&(j=u[C]),C=="printErr"&&(Y=u[C]));ie=f.wasmMemory,Se(),ae(f.wasmModule)}else if(b==="run"){An(f.pthread_ptr,0,0,1,0,0),$n(f.pthread_ptr),Ul(),Ro(),se||(Pi(),se=!0);try{Vl(f.start_routine,f.arg)}catch(v){if(v!="unwind")throw v}}else b==="cancel"?zt()&&mr(-1):f.target!=="setimmediate"&&(b==="checkMailbox"?se&&or():b&&(Y(`worker: received unknown command ${b}`),Y(f)))}catch(v){throw Oi(),v}};var _h=s,ae,se=!1;Y=function(...p){p=p.join(" "),console.error(p)},self.alert=function(...p){postMessage({Db:"alert",text:p.join(" "),$b:zt()})},u.instantiateWasm=(p,f)=>new Promise(b=>{ae=v=>{v=new WebAssembly.Instance(v,Po()),f(v),b()}}),self.onunhandledrejection=p=>{throw p.reason||p},self.onmessage=s}u.wasmBinary&&(z=u.wasmBinary);var ie,q,ce,oe,Z,ee,K,me,$e,R,G,ue,Be,ge=!1;function Se(){var s=ie.buffer;u.HEAP8=oe=new Int8Array(s),u.HEAP16=ee=new Int16Array(s),u.HEAPU8=Z=new Uint8Array(s),u.HEAPU16=K=new Uint16Array(s),u.HEAP32=me=new Int32Array(s),u.HEAPU32=$e=new Uint32Array(s),u.HEAPF32=R=new Float32Array(s),u.HEAPF64=Be=new Float64Array(s),u.HEAP64=G=new BigInt64Array(s),u.HEAPU64=ue=new BigUint64Array(s)}if(!y){if(!((ie=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0})).buffer instanceof x))throw Y("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),Error("bad memory");Se()}var Wt=[],Te=[],Oe=[],He=0,kt=null,ht=null;function Co(){if(--He==0&&(kt!==null&&(clearInterval(kt),kt=null),ht)){var s=ht;ht=null,s()}}function Et(s){throw Y(s="Aborted("+s+")"),ge=!0,ce=1,s=new WebAssembly.RuntimeError(s+". Build with -sASSERTIONS for more info."),m(s),s}var ln,To=s=>s.startsWith("data:application/octet-stream;base64,"),Ao=s=>s.startsWith("file://");function ko(s){if(s==ln&&z)return new Uint8Array(z);if(S)return S(s);throw"both async and sync fetching of the wasm failed"}function Eo(s,p,f){return function(b){return z?Promise.resolve().then(()=>ko(b)):$(b).then(v=>new Uint8Array(v),()=>ko(b))}(s).then(b=>WebAssembly.instantiate(b,p)).then(f,b=>{Y(`failed to asynchronously prepare wasm: ${b}`),Et(b)})}function Po(){return{a:{L:Ml,xa:Rl,b:Wl,Z:No,y:Ho,na:Go,V:qo,X:Ko,oa:jo,la:Yo,ea:Xo,ka:Zo,I:Qo,W:Jo,T:ei,ma:ti,U:ri,sa:Ll,B:Gl,O:Fl,N:Kl,A:Yl,r:Xl,p:Zl,C:Ql,x:ic,P:ac,ra:sc,ha:uc,R:dc,_:lc,E:cc,ga:$n,qa:pc,t:mc,D:gc,o:yc,m:wc,c:wn,n:vc,k:xc,ya:Sc,q:Ic,g:Cc,u:Tc,l:Ac,f:kc,i:Ec,j:Pc,h:Oc,e:zc,ba:Bc,ca:Dc,da:Rc,$:yi,aa:bi,ua:Mc,d:Uc,M:Vc,F:Nc,J:Wc,v:Lc,pa:Hc,S:Gc,s:vi,w:Fc,K:qc,Q:Kc,wa:jc,va:Yc,ia:xi,ja:Si,Y:hn,z:Ii,H:Ci,fa:Ti,G:Ai,a:ie,ta:fn}}}var cn={858772:(s,p,f,b)=>{if(u===void 0||!u.Ab)return 1;if((s=Ee(s>>>0)).startsWith("./")&&(s=s.substring(2)),!(s=u.Ab.get(s)))return 2;if(b>>>=0,(p>>>=0)+(f>>>=0)>s.byteLength)return 3;try{return r().set(s.subarray(p,p+f),b>>>0),0}catch{return 4}},859273:()=>{u.Mb()},859304:()=>{u.Nb()},859333:()=>{u.Rb()},859358:s=>u.Lb(s),859391:s=>u.Pb(s),859423:(s,p,f)=>{u.Fb(s,p,f,!0)},859462:(s,p,f)=>{u.Fb(s,p,f)},859495:()=>typeof wasmOffsetConverter<"u",859552:s=>{u.hb("Abs",s,void 0)},859603:s=>{u.hb("Neg",s,void 0)},859654:s=>{u.hb("Floor",s,void 0)},859707:s=>{u.hb("Ceil",s,void 0)},859759:s=>{u.hb("Reciprocal",s,void 0)},859817:s=>{u.hb("Sqrt",s,void 0)},859869:s=>{u.hb("Exp",s,void 0)},859920:s=>{u.hb("Erf",s,void 0)},859971:s=>{u.hb("Sigmoid",s,void 0)},860026:(s,p,f)=>{u.hb("HardSigmoid",s,{alpha:p,beta:f})},860105:s=>{u.hb("Log",s,void 0)},860156:s=>{u.hb("Sin",s,void 0)},860207:s=>{u.hb("Cos",s,void 0)},860258:s=>{u.hb("Tan",s,void 0)},860309:s=>{u.hb("Asin",s,void 0)},860361:s=>{u.hb("Acos",s,void 0)},860413:s=>{u.hb("Atan",s,void 0)},860465:s=>{u.hb("Sinh",s,void 0)},860517:s=>{u.hb("Cosh",s,void 0)},860569:s=>{u.hb("Asinh",s,void 0)},860622:s=>{u.hb("Acosh",s,void 0)},860675:s=>{u.hb("Atanh",s,void 0)},860728:s=>{u.hb("Tanh",s,void 0)},860780:s=>{u.hb("Not",s,void 0)},860831:(s,p,f)=>{u.hb("Clip",s,{min:p,max:f})},860900:s=>{u.hb("Clip",s,void 0)},860952:(s,p)=>{u.hb("Elu",s,{alpha:p})},861010:s=>{u.hb("Gelu",s,void 0)},861062:s=>{u.hb("Relu",s,void 0)},861114:(s,p)=>{u.hb("LeakyRelu",s,{alpha:p})},861178:(s,p)=>{u.hb("ThresholdedRelu",s,{alpha:p})},861248:(s,p)=>{u.hb("Cast",s,{to:p})},861306:s=>{u.hb("Add",s,void 0)},861357:s=>{u.hb("Sub",s,void 0)},861408:s=>{u.hb("Mul",s,void 0)},861459:s=>{u.hb("Div",s,void 0)},861510:s=>{u.hb("Pow",s,void 0)},861561:s=>{u.hb("Equal",s,void 0)},861614:s=>{u.hb("Greater",s,void 0)},861669:s=>{u.hb("GreaterOrEqual",s,void 0)},861731:s=>{u.hb("Less",s,void 0)},861783:s=>{u.hb("LessOrEqual",s,void 0)},861842:(s,p,f,b,v)=>{u.hb("ReduceMean",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862001:(s,p,f,b,v)=>{u.hb("ReduceMax",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862159:(s,p,f,b,v)=>{u.hb("ReduceMin",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862317:(s,p,f,b,v)=>{u.hb("ReduceProd",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862476:(s,p,f,b,v)=>{u.hb("ReduceSum",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862634:(s,p,f,b,v)=>{u.hb("ReduceL1",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862791:(s,p,f,b,v)=>{u.hb("ReduceL2",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},862948:(s,p,f,b,v)=>{u.hb("ReduceLogSum",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863109:(s,p,f,b,v)=>{u.hb("ReduceSumSquare",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863273:(s,p,f,b,v)=>{u.hb("ReduceLogSumExp",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},863437:s=>{u.hb("Where",s,void 0)},863490:(s,p,f)=>{u.hb("Transpose",s,{perm:p?Array.from(i().subarray(p>>>0,f>>>0)):[]})},863598:(s,p,f,b)=>{u.hb("DepthToSpace",s,{blocksize:p,mode:Ee(f),format:b?"NHWC":"NCHW"})},863731:(s,p,f,b)=>{u.hb("DepthToSpace",s,{blocksize:p,mode:Ee(f),format:b?"NHWC":"NCHW"})},863864:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt)=>{u.hb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:p,dilations:[f],group:b,kernelShape:[v],pads:[C,P],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:J?Array.from(i().subarray(J>>>0,de>>>0)):[],outputShape:ye?Array.from(i().subarray(ye>>>0,Ae>>>0)):[],activation:Ee(Bt)})},864265:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:p,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(P>>>0,2+(P>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,J>>>0)):[],outputShape:de?Array.from(i().subarray(de>>>0,ye>>>0)):[],activation:Ee(Ae)})},864830:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt)=>{u.hb("ConvTranspose",s,{format:N?"NHWC":"NCHW",autoPad:p,dilations:[f],group:b,kernelShape:[v],pads:[C,P],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:J?Array.from(i().subarray(J>>>0,de>>>0)):[],outputShape:ye?Array.from(i().subarray(ye>>>0,Ae>>>0)):[],activation:Ee(Bt)})},865231:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:p,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(v>>>0,2+(v>>>0)>>>0)),pads:Array.from(i().subarray(C>>>0,4+(C>>>0)>>>0)),strides:Array.from(i().subarray(P>>>0,2+(P>>>0)>>>0)),wIsConst:()=>!!t()[N>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,J>>>0)):[],outputShape:de?Array.from(i().subarray(de>>>0,ye>>>0)):[],activation:Ee(Ae)})},865796:(s,p)=>{u.hb("GlobalAveragePool",s,{format:p?"NHWC":"NCHW"})},865887:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("AveragePool",s,{format:Ae?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},866302:(s,p)=>{u.hb("GlobalAveragePool",s,{format:p?"NHWC":"NCHW"})},866393:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("AveragePool",s,{format:Ae?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},866808:(s,p)=>{u.hb("GlobalMaxPool",s,{format:p?"NHWC":"NCHW"})},866895:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("MaxPool",s,{format:Ae?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},867306:(s,p)=>{u.hb("GlobalMaxPool",s,{format:p?"NHWC":"NCHW"})},867393:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae)=>{u.hb("MaxPool",s,{format:Ae?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:v,dilations:C?Array.from(i().subarray(C>>>0,P>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,N>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,J>>>0)):[],strides:de?Array.from(i().subarray(de>>>0,ye>>>0)):[]})},867804:(s,p,f,b,v)=>{u.hb("Gemm",s,{alpha:p,beta:f,transA:b,transB:v})},867908:s=>{u.hb("MatMul",s,void 0)},867962:(s,p,f,b)=>{u.hb("ArgMax",s,{keepDims:!!p,selectLastIndex:!!f,axis:b})},868070:(s,p,f,b)=>{u.hb("ArgMin",s,{keepDims:!!p,selectLastIndex:!!f,axis:b})},868178:(s,p)=>{u.hb("Softmax",s,{axis:p})},868241:(s,p)=>{u.hb("Concat",s,{axis:p})},868301:(s,p,f,b,v)=>{u.hb("Split",s,{axis:p,numOutputs:f,splitSizes:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},868441:s=>{u.hb("Expand",s,void 0)},868495:(s,p)=>{u.hb("Gather",s,{axis:Number(p)})},868566:(s,p)=>{u.hb("GatherElements",s,{axis:Number(p)})},868645:(s,p,f,b,v,C,P,B,N,H,J)=>{u.hb("Resize",s,{antialias:p,axes:f?Array.from(i().subarray(f>>>0,b>>>0)):[],coordinateTransformMode:Ee(v),cubicCoeffA:C,excludeOutside:P,extrapolationValue:B,keepAspectRatioPolicy:Ee(N),mode:Ee(H),nearestMode:Ee(J)})},868991:(s,p,f,b,v,C,P)=>{u.hb("Slice",s,{starts:p?Array.from(i().subarray(p>>>0,f>>>0)):[],ends:b?Array.from(i().subarray(b>>>0,v>>>0)):[],axes:C?Array.from(i().subarray(C>>>0,P>>>0)):[]})},869207:s=>{u.hb("Tile",s,void 0)},869259:(s,p,f)=>{u.hb("InstanceNormalization",s,{epsilon:p,format:f?"NHWC":"NCHW"})},869373:(s,p,f)=>{u.hb("InstanceNormalization",s,{epsilon:p,format:f?"NHWC":"NCHW"})},869487:s=>{u.hb("Range",s,void 0)},869540:(s,p)=>{u.hb("Einsum",s,{equation:Ee(p)})},869621:(s,p,f,b,v)=>{u.hb("Pad",s,{mode:p,value:f,pads:b?Array.from(i().subarray(b>>>0,v>>>0)):[]})},869748:(s,p,f,b,v,C)=>{u.hb("BatchNormalization",s,{epsilon:p,momentum:f,spatial:!!v,trainingMode:!!b,format:C?"NHWC":"NCHW"})},869917:(s,p,f,b,v,C)=>{u.hb("BatchNormalization",s,{epsilon:p,momentum:f,spatial:!!v,trainingMode:!!b,format:C?"NHWC":"NCHW"})},870086:(s,p,f)=>{u.hb("CumSum",s,{exclusive:Number(p),reverse:Number(f)})},870183:(s,p,f)=>{u.hb("DequantizeLinear",s,{axis:p,blockSize:f})},870273:(s,p,f,b,v,C,P,B,N)=>{u.hb("Attention",s,{numHeads:p,isUnidirectional:f,maskFilterValue:b,scale:v,doRotary:C,qkvHiddenSizes:P?Array.from(i().subarray(Number(B)>>>0,Number(B)+P>>>0)):[],pastPresentShareBuffer:!!N})},870545:s=>{u.hb("BiasAdd",s,void 0)},870600:s=>{u.hb("BiasSplitGelu",s,void 0)},870661:s=>{u.hb("FastGelu",s,void 0)},870717:(s,p,f,b,v,C,P,B,N,H,J,de,ye,Ae,Bt,Qc)=>{u.hb("Conv",s,{format:de?"NHWC":"NCHW",auto_pad:p,dilations:f?Array.from(i().subarray(f>>>0,b>>>0)):[],group:v,kernel_shape:C?Array.from(i().subarray(C>>>0,P>>>0)):[],pads:B?Array.from(i().subarray(B>>>0,N>>>0)):[],strides:H?Array.from(i().subarray(H>>>0,J>>>0)):[],w_is_const:()=>!!t()[ye>>>0],activation:Ee(Ae),activation_params:Bt?Array.from(l().subarray(Bt>>>0,Qc>>>0)):[]})},871213:s=>{u.hb("Gelu",s,void 0)},871265:(s,p,f,b)=>{u.hb("GroupQueryAttention",s,{numHeads:p,kvNumHeads:f,scale:b})},871378:(s,p,f,b)=>{u.hb("LayerNormalization",s,{axis:p,epsilon:f,simplified:!!b})},871489:(s,p,f,b)=>{u.hb("LayerNormalization",s,{axis:p,epsilon:f,simplified:!!b})},871600:(s,p,f,b,v,C)=>{u.hb("MatMulNBits",s,{k:p,n:f,accuracyLevel:b,bits:v,blockSize:C})},871727:(s,p,f,b,v,C)=>{u.hb("MultiHeadAttention",s,{numHeads:p,isUnidirectional:f,maskFilterValue:b,scale:v,doRotary:C})},871886:(s,p)=>{u.hb("QuickGelu",s,{alpha:p})},871950:(s,p,f,b,v)=>{u.hb("RotaryEmbedding",s,{interleaved:!!p,numHeads:f,rotaryEmbeddingDim:b,scale:v})},872089:(s,p,f)=>{u.hb("SkipLayerNormalization",s,{epsilon:p,simplified:!!f})},872191:s=>{u.Qb(s)},872225:(s,p)=>u.Sb(s,p,u.zb.Ub,u.zb.errors),872337:(s,p,f)=>{u.hb("SkipLayerNormalization",s,{epsilon:p,simplified:!!f})}};function Rl(s,p,f){return ci(async()=>{await u.Ob(s,p,f)})}function Ml(){return typeof wasmOffsetConverter<"u"}function pn(s){this.name="ExitStatus",this.message=`Program terminated with exit(${s})`,this.status=s}var mn=s=>{s.terminate(),s.onmessage=()=>{}},Oo=s=>{dt.length==0&&(Uo(),Mo(dt[0]));var p=dt.pop();if(!p)return 6;yt.push(p),Ye[s.sb]=p,p.sb=s.sb;var f={cmd:"run",start_routine:s.Vb,arg:s.Hb,pthread_ptr:s.sb};return p.postMessage(f,s.Wb),0},gt=0,ve=(s,p,...f)=>{for(var b=2*f.length,v=Pn(),C=En(8*b),P=C>>>3,B=0;B<f.length;B++){var N=f[B];typeof N=="bigint"?(G[P+2*B]=1n,G[P+2*B+1]=N):(G[P+2*B]=0n,d()[P+2*B+1>>>0]=N)}return s=zi(s,0,b,C,p),fr(v),s};function fn(s){if(y)return ve(0,1,s);if(ce=s,!(0<gt)){for(var p of yt)mn(p);for(p of dt)mn(p);dt=[],yt=[],Ye=[],ge=!0}A(s,new pn(s))}function zo(s){if(y)return ve(1,0,s);hn(s)}var hn=s=>{if(ce=s,y)throw zo(s),"unwind";fn(s)},dt=[],yt=[],Bo=[],Ye={},Do=s=>{var p=s.sb;delete Ye[p],dt.push(s),yt.splice(yt.indexOf(s),1),s.sb=0,kn(p)};function Ro(){Bo.forEach(s=>s())}var Mo=s=>new Promise(p=>{s.onmessage=v=>{var C=(v=v.data).cmd;if(v.targetThread&&v.targetThread!=zt()){var P=Ye[v.targetThread];P?P.postMessage(v,v.transferList):Y(`Internal error! Worker sent a message "${C}" to target pthread ${v.targetThread}, but that thread no longer exists!`)}else C==="checkMailbox"?or():C==="spawnThread"?Oo(v):C==="cleanupThread"?Do(Ye[v.thread]):C==="killThread"?(v=v.thread,C=Ye[v],delete Ye[v],mn(C),kn(v),yt.splice(yt.indexOf(C),1),C.sb=0):C==="cancelThread"?Ye[v.thread].postMessage({cmd:"cancel"}):C==="loaded"?(s.loaded=!0,p(s)):C==="alert"?alert(`Thread ${v.threadId}: ${v.text}`):v.target==="setimmediate"?s.postMessage(v):C==="callHandler"?u[v.handler](...v.args):C&&Y(`worker sent an unknown command ${C}`)},s.onerror=v=>{throw Y(`worker sent an error! ${v.filename}:${v.lineno}: ${v.message}`),v};var f,b=[];for(f of[])u.propertyIsEnumerable(f)&&b.push(f);s.postMessage({cmd:"load",handlers:b,wasmMemory:ie,wasmModule:q})});function Uo(){var s=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});dt.push(s)}var nr=s=>{for(;0<s.length;)s.shift()(u)},Ul=()=>{var s=zt(),p=a()[s+52>>>2>>>0];s=a()[s+56>>>2>>>0],Di(p,p-s),fr(p)},Vl=(s,p)=>{gt=0,s=Ri(s,p),0<gt?ce=s:mr(s)};class Nl{constructor(p){this.Bb=p-24}}function Wl(s,p,f){var b=new Nl(s>>>=0);throw p>>>=0,f>>>=0,a()[b.Bb+16>>>2>>>0]=0,a()[b.Bb+4>>>2>>>0]=p,a()[b.Bb+8>>>2>>>0]=f,s}function Vo(s,p,f,b){return y?ve(2,1,s,p,f,b):No(s,p,f,b)}function No(s,p,f,b){if(s>>>=0,p>>>=0,f>>>=0,b>>>=0,x===void 0)return Y("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var v=[];return y&&v.length===0?Vo(s,p,f,b):(s={Vb:f,sb:s,Hb:b,Wb:v},y?(s.Db="spawnThread",postMessage(s,v),0):Oo(s))}var Wo=typeof TextDecoder<"u"?new TextDecoder:void 0,Lo=(s,p,f)=>{var b=(p>>>=0)+f;for(f=p;s[f]&&!(f>=b);)++f;if(16<f-p&&s.buffer&&Wo)return Wo.decode(s.buffer instanceof x?s.slice(p,f):s.subarray(p,f));for(b="";p<f;){var v=s[p++];if(128&v){var C=63&s[p++];if((224&v)==192)b+=String.fromCharCode((31&v)<<6|C);else{var P=63&s[p++];65536>(v=(240&v)==224?(15&v)<<12|C<<6|P:(7&v)<<18|C<<12|P<<6|63&s[p++])?b+=String.fromCharCode(v):(v-=65536,b+=String.fromCharCode(55296|v>>10,56320|1023&v))}}else b+=String.fromCharCode(v)}return b},Ee=(s,p)=>(s>>>=0)?Lo(r(),s,p):"";function Ho(s,p,f){return y?ve(3,1,s,p,f):0}function Go(s,p){if(y)return ve(4,1,s,p)}var Fo=s=>{for(var p=0,f=0;f<s.length;++f){var b=s.charCodeAt(f);127>=b?p++:2047>=b?p+=2:55296<=b&&57343>=b?(p+=4,++f):p+=3}return p},Pt=(s,p,f)=>{var b=r();if(p>>>=0,0<f){var v=p;f=p+f-1;for(var C=0;C<s.length;++C){var P=s.charCodeAt(C);if(55296<=P&&57343>=P&&(P=65536+((1023&P)<<10)|1023&s.charCodeAt(++C)),127>=P){if(p>=f)break;b[p++>>>0]=P}else{if(2047>=P){if(p+1>=f)break;b[p++>>>0]=192|P>>6}else{if(65535>=P){if(p+2>=f)break;b[p++>>>0]=224|P>>12}else{if(p+3>=f)break;b[p++>>>0]=240|P>>18,b[p++>>>0]=128|P>>12&63}b[p++>>>0]=128|P>>6&63}b[p++>>>0]=128|63&P}}b[p>>>0]=0,s=p-v}else s=0;return s};function qo(s,p){if(y)return ve(5,1,s,p)}function Ko(s,p,f){if(y)return ve(6,1,s,p,f)}function jo(s,p,f){return y?ve(7,1,s,p,f):0}function Yo(s,p){if(y)return ve(8,1,s,p)}function Xo(s,p,f){if(y)return ve(9,1,s,p,f)}function Zo(s,p,f,b){if(y)return ve(10,1,s,p,f,b)}function Qo(s,p,f,b){if(y)return ve(11,1,s,p,f,b)}function Jo(s,p,f,b){if(y)return ve(12,1,s,p,f,b)}function ei(s){if(y)return ve(13,1,s)}function ti(s,p){if(y)return ve(14,1,s,p)}function ri(s,p,f){if(y)return ve(15,1,s,p,f)}var ni,lt,Ll=()=>{Et("")},Xe=s=>{for(var p="";r()[s>>>0];)p+=ni[r()[s++>>>0]];return p},gn={},yn={},Hl={};function it(s,p,f={}){if(!("argPackAdvance"in p))throw new TypeError("registerType registeredInstance requires argPackAdvance");return function(b,v,C={}){var P=v.name;if(!b)throw new lt(`type "${P}" must have a positive integer typeid pointer`);if(yn.hasOwnProperty(b)){if(C.Jb)return;throw new lt(`Cannot register type '${P}' twice`)}yn[b]=v,delete Hl[b],gn.hasOwnProperty(b)&&(v=gn[b],delete gn[b],v.forEach(B=>B()))}(s,p,f)}var oi=(s,p,f)=>{switch(p){case 1:return f?b=>t()[b>>>0]:b=>r()[b>>>0];case 2:return f?b=>n()[b>>>1>>>0]:b=>o()[b>>>1>>>0];case 4:return f?b=>i()[b>>>2>>>0]:b=>a()[b>>>2>>>0];case 8:return f?b=>G[b>>>3]:b=>ue[b>>>3];default:throw new TypeError(`invalid integer width (${p}): ${s}`)}};function Gl(s,p,f){f>>>=0,it(s>>>=0,{name:p=Xe(p>>>0),fromWireType:b=>b,toWireType:function(b,v){if(typeof v!="bigint"&&typeof v!="number")throw v=v===null?"null":(b=typeof v)=="object"||b==="array"||b==="function"?v.toString():""+v,new TypeError(`Cannot convert "${v}" to ${this.name}`);return typeof v=="number"&&(v=BigInt(v)),v},argPackAdvance:ct,readValueFromPointer:oi(p,f,p.indexOf("u")==-1),yb:null})}var ct=8;function Fl(s,p,f,b){it(s>>>=0,{name:p=Xe(p>>>0),fromWireType:function(v){return!!v},toWireType:function(v,C){return C?f:b},argPackAdvance:ct,readValueFromPointer:function(v){return this.fromWireType(r()[v>>>0])},yb:null})}var bn=[],at=[];function wn(s){9<(s>>>=0)&&--at[s+1]==0&&(at[s]=void 0,bn.push(s))}var Ge=s=>{if(!s)throw new lt("Cannot use deleted val. handle = "+s);return at[s]},Fe=s=>{switch(s){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let p=bn.pop()||at.length;return at[p]=s,at[p+1]=1,p}};function vn(s){return this.fromWireType(a()[s>>>2>>>0])}var ql={name:"emscripten::val",fromWireType:s=>{var p=Ge(s);return wn(s),p},toWireType:(s,p)=>Fe(p),argPackAdvance:ct,readValueFromPointer:vn,yb:null};function Kl(s){return it(s>>>0,ql)}var jl=(s,p)=>{switch(p){case 4:return function(f){return this.fromWireType(l()[f>>>2>>>0])};case 8:return function(f){return this.fromWireType(d()[f>>>3>>>0])};default:throw new TypeError(`invalid float width (${p}): ${s}`)}};function Yl(s,p,f){f>>>=0,it(s>>>=0,{name:p=Xe(p>>>0),fromWireType:b=>b,toWireType:(b,v)=>v,argPackAdvance:ct,readValueFromPointer:jl(p,f),yb:null})}function Xl(s,p,f,b,v){if(s>>>=0,f>>>=0,p=Xe(p>>>0),v===-1&&(v=4294967295),v=B=>B,b===0){var C=32-8*f;v=B=>B<<C>>>C}var P=p.includes("unsigned")?function(B,N){return N>>>0}:function(B,N){return N};it(s,{name:p,fromWireType:v,toWireType:P,argPackAdvance:ct,readValueFromPointer:oi(p,f,b!==0),yb:null})}function Zl(s,p,f){function b(C){var P=a()[C>>>2>>>0];return C=a()[C+4>>>2>>>0],new v(t().buffer,C,P)}var v=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][p];it(s>>>=0,{name:f=Xe(f>>>0),fromWireType:b,argPackAdvance:ct,readValueFromPointer:b},{Jb:!0})}function Ql(s,p){s>>>=0;var f=(p=Xe(p>>>0))==="std::string";it(s,{name:p,fromWireType:function(b){var v=a()[b>>>2>>>0],C=b+4;if(f)for(var P=C,B=0;B<=v;++B){var N=C+B;if(B==v||r()[N>>>0]==0){if(P=Ee(P,N-P),H===void 0)var H=P;else H+=String.fromCharCode(0),H+=P;P=N+1}}else{for(H=Array(v),B=0;B<v;++B)H[B]=String.fromCharCode(r()[C+B>>>0]);H=H.join("")}return Qe(b),H},toWireType:function(b,v){v instanceof ArrayBuffer&&(v=new Uint8Array(v));var C=typeof v=="string";if(!(C||v instanceof Uint8Array||v instanceof Uint8ClampedArray||v instanceof Int8Array))throw new lt("Cannot pass non-string to std::string");var P=f&&C?Fo(v):v.length,B=pr(4+P+1),N=B+4;if(a()[B>>>2>>>0]=P,f&&C)Pt(v,N,P+1);else if(C)for(C=0;C<P;++C){var H=v.charCodeAt(C);if(255<H)throw Qe(N),new lt("String has UTF-16 code units that do not fit in 8 bits");r()[N+C>>>0]=H}else for(C=0;C<P;++C)r()[N+C>>>0]=v[C];return b!==null&&b.push(Qe,B),B},argPackAdvance:ct,readValueFromPointer:vn,yb(b){Qe(b)}})}var ii=typeof TextDecoder<"u"?new TextDecoder("utf-16le"):void 0,Jl=(s,p)=>{for(var f=s>>1,b=f+p/2;!(f>=b)&&o()[f>>>0];)++f;if(32<(f<<=1)-s&&ii)return ii.decode(r().slice(s,f));for(f="",b=0;!(b>=p/2);++b){var v=n()[s+2*b>>>1>>>0];if(v==0)break;f+=String.fromCharCode(v)}return f},ec=(s,p,f)=>{if(f??=2147483647,2>f)return 0;var b=p;f=(f-=2)<2*s.length?f/2:s.length;for(var v=0;v<f;++v){var C=s.charCodeAt(v);n()[p>>>1>>>0]=C,p+=2}return n()[p>>>1>>>0]=0,p-b},tc=s=>2*s.length,rc=(s,p)=>{for(var f=0,b="";!(f>=p/4);){var v=i()[s+4*f>>>2>>>0];if(v==0)break;++f,65536<=v?(v-=65536,b+=String.fromCharCode(55296|v>>10,56320|1023&v)):b+=String.fromCharCode(v)}return b},nc=(s,p,f)=>{if(p>>>=0,f??=2147483647,4>f)return 0;var b=p;f=b+f-4;for(var v=0;v<s.length;++v){var C=s.charCodeAt(v);if(55296<=C&&57343>=C&&(C=65536+((1023&C)<<10)|1023&s.charCodeAt(++v)),i()[p>>>2>>>0]=C,(p+=4)+4>f)break}return i()[p>>>2>>>0]=0,p-b},oc=s=>{for(var p=0,f=0;f<s.length;++f){var b=s.charCodeAt(f);55296<=b&&57343>=b&&++f,p+=4}return p};function ic(s,p,f){if(s>>>=0,p>>>=0,f=Xe(f>>>=0),p===2)var b=Jl,v=ec,C=tc,P=B=>o()[B>>>1>>>0];else p===4&&(b=rc,v=nc,C=oc,P=B=>a()[B>>>2>>>0]);it(s,{name:f,fromWireType:B=>{for(var N,H=a()[B>>>2>>>0],J=B+4,de=0;de<=H;++de){var ye=B+4+de*p;de!=H&&P(ye)!=0||(J=b(J,ye-J),N===void 0?N=J:(N+=String.fromCharCode(0),N+=J),J=ye+p)}return Qe(B),N},toWireType:(B,N)=>{if(typeof N!="string")throw new lt(`Cannot pass non-string to C++ string type ${f}`);var H=C(N),J=pr(4+H+p);return a()[J>>>2>>>0]=H/p,v(N,J+4,H+p),B!==null&&B.push(Qe,J),J},argPackAdvance:ct,readValueFromPointer:vn,yb(B){Qe(B)}})}function ac(s,p){it(s>>>=0,{Kb:!0,name:p=Xe(p>>>0),argPackAdvance:0,fromWireType:()=>{},toWireType:()=>{}})}var sc=()=>1;function uc(s){An(s>>>0,!g,1,!w,131072,!1),Ro()}var ai=s=>{if(!ge)try{if(s(),!(0<gt))try{y?mr(ce):hn(ce)}catch(p){p instanceof pn||p=="unwind"||A(1,p)}}catch(p){p instanceof pn||p=="unwind"||A(1,p)}};function $n(s){s>>>=0,typeof Atomics.Xb=="function"&&(Atomics.Xb(i(),s>>>2,s).value.then(or),s+=128,Atomics.store(i(),s>>>2,1))}var or=()=>{var s=zt();s&&($n(s),ai(Bi))};function dc(s,p){(s>>>=0)==p>>>0?setTimeout(or):y?postMessage({targetThread:s,cmd:"checkMailbox"}):(s=Ye[s])&&s.postMessage({cmd:"checkMailbox"})}var _n=[];function lc(s,p,f,b,v){for(p>>>=0,b/=2,_n.length=b,f=v>>>0>>>3,v=0;v<b;v++)_n[v]=G[f+2*v]?G[f+2*v+1]:d()[f+2*v+1>>>0];return(p?cn[p]:Zc[s])(..._n)}function cc(s){s>>>=0,y?postMessage({cmd:"cleanupThread",thread:s}):Do(Ye[s])}function pc(s){}var xn=(s,p)=>{var f=yn[s];if(f===void 0)throw s=Ei(s),f=Xe(s),Qe(s),new lt(`${p} has unknown type ${f}`);return f},si=(s,p,f)=>{var b=[];return s=s.toWireType(b,f),b.length&&(a()[p>>>2>>>0]=Fe(b)),s};function mc(s,p,f){return p>>>=0,f>>>=0,s=Ge(s>>>0),p=xn(p,"emval::as"),si(p,f,s)}var ir=s=>{try{s()}catch(p){Et(p)}},pt=0,Ze=null,ui=0,ar=[],di={},li={},fc=0,Sn=null,hc=[];function ci(s){return function(p){if(!ge){if(pt===0){var f=!1,b=!1;p((v=0)=>{if(!ge&&(ui=v,f=!0,b)){pt=2,ir(()=>Vi(Ze)),typeof Browser<"u"&&Browser.Cb.Ib&&Browser.Cb.resume(),v=!1;try{var C=function(){var N=i()[Ze+8>>>2>>>0];return N=F[li[N]],--gt,N()}()}catch(N){C=N,v=!0}var P=!1;if(!Ze){var B=Sn;B&&(Sn=null,(v?B.reject:B.resolve)(C),P=!0)}if(v&&!P)throw C}}),b=!0,f||(pt=1,Ze=function(){var v=pr(65548),C=v+12;a()[v>>>2>>>0]=C,a()[v+4>>>2>>>0]=C+65536,C=ar[0];var P=di[C];return P===void 0&&(P=fc++,di[C]=P,li[P]=C),C=P,i()[v+8>>>2>>>0]=C,v}(),typeof Browser<"u"&&Browser.Cb.Ib&&Browser.Cb.pause(),ir(()=>Mi(Ze)))}else pt===2?(pt=0,ir(Ni),Qe(Ze),Ze=null,hc.forEach(ai)):Et(`invalid state: ${pt}`);return ui}}(p=>{s().then(p)})}function gc(s){return s>>>=0,ci(()=>(s=Ge(s)).then(Fe))}var sr=[];function yc(s,p,f,b){return f>>>=0,b>>>=0,(s=sr[s>>>0])(null,p=Ge(p>>>0),f,b)}var bc={},ur=s=>{var p=bc[s];return p===void 0?Xe(s):p};function wc(s,p,f,b,v){return f>>>=0,b>>>=0,v>>>=0,(s=sr[s>>>0])(p=Ge(p>>>0),p[f=ur(f)],b,v)}var pi=()=>typeof globalThis=="object"?globalThis:Function("return this")();function vc(s){return(s>>>=0)==0?Fe(pi()):(s=ur(s),Fe(pi()[s]))}var $c=s=>{var p=sr.length;return sr.push(s),p},_c=(s,p)=>{for(var f=Array(s),b=0;b<s;++b)f[b]=xn(a()[p+4*b>>>2>>>0],"parameter "+b);return f},mi=(s,p)=>Object.defineProperty(p,"name",{value:s});function xc(s,p,f){var b=(p=_c(s,p>>>0)).shift();s--;var v=`return function (obj, func, destructorsRef, args) {
|
|
3804
|
+
`,C=0,P=[];f===0&&P.push("obj");for(var B=["retType"],N=[b],H=0;H<s;++H)P.push("arg"+H),B.push("argType"+H),N.push(p[H]),v+=` var arg${H} = argType${H}.readValueFromPointer(args${C?"+"+C:""});
|
|
3805
|
+
`,C+=p[H].argPackAdvance;return v+=` var rv = ${f===1?"new func":"func.call"}(${P.join(", ")});
|
|
3806
3806
|
`,b.Kb||(B.push("emval_returnValue"),N.push(si),v+=` return emval_returnValue(retType, destructorsRef, rv);
|
|
3807
3807
|
`),B.push(v+`};
|
|
3808
|
-
`),s=function(J){var de=Function;if(!(de instanceof Function))throw new TypeError(`new_ called with constructor type ${typeof de} which is not a function`);var ye=mi(de.name||"unknownFunctionName",function(){});return ye.prototype=de.prototype,ye=new ye,(J=de.apply(ye,J))instanceof Object?J:ye}(B)(...N),f=`methodCaller<(${
|
|
3809
|
-
`);return s[0]=="Error"&&s.shift(),$i(s),Ot.Gb=dr(),Ot.Tb=s,Ot.Gb}function Yc(s,
|
|
3810
|
-
`))[0]=="Error"&&b.shift(),$i(b);for(var v=3;b[v]&&dr()!=s;)++v;for(s=0;s<f&&b[s+v];++s)i()[c+4*s>>>2>>>0]=dr();return s}var Cn,Tn={},_i=()=>{if(!Cn){var s,c={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:T||"./this.program"};for(s in Tn)Tn[s]===void 0?delete c[s]:c[s]=Tn[s];var f=[];for(s in c)f.push(`${s}=${c[s]}`);Cn=f}return Cn};function xi(s,c){if(y)return ve(18,1,s,c);s>>>=0,c>>>=0;var f=0;return _i().forEach((b,v)=>{var C=c+f;for(v=a()[s+4*v>>>2>>>0]=C,C=0;C<b.length;++C)t()[v++>>>0]=b.charCodeAt(C);t()[v>>>0]=0,f+=b.length+1}),0}function Si(s,c){if(y)return ve(19,1,s,c);s>>>=0,c>>>=0;var f=_i();a()[s>>>2>>>0]=f.length;var b=0;return f.forEach(v=>b+=v.length+1),a()[c>>>2>>>0]=b,0}function Ii(s){return y?ve(20,1,s):52}function Ci(s,c,f,b){return y?ve(21,1,s,c,f,b):52}function Ti(s,c,f,b){return y?ve(22,1,s,c,f,b):70}var Xc=[null,[],[]];function Ai(s,c,f,b){if(y)return ve(23,1,s,c,f,b);c>>>=0,f>>>=0,b>>>=0;for(var v=0,C=0;C<f;C++){var P=a()[c>>>2>>>0],B=a()[c+4>>>2>>>0];c+=8;for(var N=0;N<B;N++){var H=r()[P+N>>>0],J=Xc[s];H===0||H===10?((s===1?j:Y)(Lo(J,0)),J.length=0):J.push(H)}v+=B}return a()[b>>>2>>>0]=v,0}y||function(){for(var s=u.numThreads-1;s--;)Uo();Wt.unshift(()=>{He++,function(c){y?c():Promise.all(dt.map(Mo)).then(c)}(()=>Co())})}();for(var ki=Array(256),lr=0;256>lr;++lr)ki[lr]=String.fromCharCode(lr);ni=ki,lt=u.BindingError=class extends Error{constructor(s){super(s),this.name="BindingError"}},u.InternalError=class extends Error{constructor(s){super(s),this.name="InternalError"}},at.push(0,1,void 0,1,null,1,!0,1,!1,1),u.count_emval_handles=()=>at.length/2-5-bn.length;var Zc=[fn,zo,Vo,Ho,Go,qo,Ko,jo,Yo,Xo,Zo,Qo,Jo,ei,ti,ri,yi,bi,xi,Si,Ii,Ci,Ti,Ai],F=function(){function s(f,b){return F=f.exports,F=function(){var v=F,C={};for(let[P,B]of Object.entries(v))C[P]=typeof B=="function"?(...N)=>{ar.push(P);try{return B(...N)}finally{ge||(ar.pop(),Ze&&pt===1&&ar.length===0&&(pt=0,gt+=1,ir(Ui),typeof Fibers<"u"&&Fibers.ac()))}}:B;return C}(),F=function(){var v=F,C=B=>N=>B(N)>>>0,P=B=>()=>B()>>>0;return(v=Object.assign({},v)).Aa=C(v.Aa),v.db=P(v.db),v.eb=C(v.eb),v.emscripten_main_runtime_thread_id=P(v.emscripten_main_runtime_thread_id),v.qb=C(v.qb),v.rb=P(v.rb),v}(),Bo.push(F.gb),Te.unshift(F.za),q=b,Co(),F}var c=Po();if(He++,u.instantiateWasm)try{return u.instantiateWasm(c,s)}catch(f){Y(`Module.instantiateWasm callback failed with error: ${f}`),m(f)}return ln||=u.locateFile?To("ort-wasm-simd-threaded.jsep.wasm")?"ort-wasm-simd-threaded.jsep.wasm":u.locateFile?u.locateFile("ort-wasm-simd-threaded.jsep.wasm",O):O+"ort-wasm-simd-threaded.jsep.wasm":new URL(/* asset import */ __webpack_require__(/*! ort-wasm-simd-threaded.jsep.wasm */ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm"), __webpack_require__.b).href,function(f,b){var v=ln;return z||typeof WebAssembly.instantiateStreaming!="function"||To(v)||Ao(v)||typeof fetch!="function"?Eo(v,f,b):fetch(v,{credentials:"same-origin"}).then(C=>WebAssembly.instantiateStreaming(C,f).then(b,function(P){return Y(`wasm streaming compile failed: ${P}`),Y("falling back to ArrayBuffer instantiation"),Eo(v,f,b)}))}(c,function(f){s(f.instance,f.module)}).catch(m),{}}(),Ei=s=>(Ei=F.Aa)(s),Pi=()=>(Pi=F.Ba)();u._OrtInit=(s,c)=>(u._OrtInit=F.Ca)(s,c),u._OrtGetLastError=(s,c)=>(u._OrtGetLastError=F.Da)(s,c),u._OrtCreateSessionOptions=(s,c,f,b,v,C,P,B,N,H)=>(u._OrtCreateSessionOptions=F.Ea)(s,c,f,b,v,C,P,B,N,H),u._OrtAppendExecutionProvider=(s,c)=>(u._OrtAppendExecutionProvider=F.Fa)(s,c),u._OrtAddFreeDimensionOverride=(s,c,f)=>(u._OrtAddFreeDimensionOverride=F.Ga)(s,c,f),u._OrtAddSessionConfigEntry=(s,c,f)=>(u._OrtAddSessionConfigEntry=F.Ha)(s,c,f),u._OrtReleaseSessionOptions=s=>(u._OrtReleaseSessionOptions=F.Ia)(s),u._OrtCreateSession=(s,c,f)=>(u._OrtCreateSession=F.Ja)(s,c,f),u._OrtReleaseSession=s=>(u._OrtReleaseSession=F.Ka)(s),u._OrtGetInputOutputCount=(s,c,f)=>(u._OrtGetInputOutputCount=F.La)(s,c,f),u._OrtGetInputName=(s,c)=>(u._OrtGetInputName=F.Ma)(s,c),u._OrtGetOutputName=(s,c)=>(u._OrtGetOutputName=F.Na)(s,c),u._OrtFree=s=>(u._OrtFree=F.Oa)(s),u._OrtCreateTensor=(s,c,f,b,v,C)=>(u._OrtCreateTensor=F.Pa)(s,c,f,b,v,C),u._OrtGetTensorData=(s,c,f,b,v)=>(u._OrtGetTensorData=F.Qa)(s,c,f,b,v),u._OrtReleaseTensor=s=>(u._OrtReleaseTensor=F.Ra)(s),u._OrtCreateRunOptions=(s,c,f,b)=>(u._OrtCreateRunOptions=F.Sa)(s,c,f,b),u._OrtAddRunConfigEntry=(s,c,f)=>(u._OrtAddRunConfigEntry=F.Ta)(s,c,f),u._OrtReleaseRunOptions=s=>(u._OrtReleaseRunOptions=F.Ua)(s),u._OrtCreateBinding=s=>(u._OrtCreateBinding=F.Va)(s),u._OrtBindInput=(s,c,f)=>(u._OrtBindInput=F.Wa)(s,c,f),u._OrtBindOutput=(s,c,f,b)=>(u._OrtBindOutput=F.Xa)(s,c,f,b),u._OrtClearBoundOutputs=s=>(u._OrtClearBoundOutputs=F.Ya)(s),u._OrtReleaseBinding=s=>(u._OrtReleaseBinding=F.Za)(s),u._OrtRunWithBinding=(s,c,f,b,v)=>(u._OrtRunWithBinding=F._a)(s,c,f,b,v),u._OrtRun=(s,c,f,b,v,C,P,B)=>(u._OrtRun=F.$a)(s,c,f,b,v,C,P,B),u._OrtEndProfiling=s=>(u._OrtEndProfiling=F.ab)(s),u._JsepOutput=(s,c,f)=>(u._JsepOutput=F.bb)(s,c,f),u._JsepGetNodeName=s=>(u._JsepGetNodeName=F.cb)(s);var cr,zt=()=>(zt=F.db)(),pr=u._malloc=s=>(pr=u._malloc=F.eb)(s),Qe=u._free=s=>(Qe=u._free=F.fb)(s),An=(s,c,f,b,v,C)=>(An=F.ib)(s,c,f,b,v,C),Oi=()=>(Oi=F.jb)(),zi=(s,c,f,b,v)=>(zi=F.kb)(s,c,f,b,v),kn=s=>(kn=F.lb)(s),mr=s=>(mr=F.mb)(s),Bi=()=>(Bi=F.nb)(),Di=(s,c)=>(Di=F.ob)(s,c),fr=s=>(fr=F.pb)(s),En=s=>(En=F.qb)(s),Pn=()=>(Pn=F.rb)(),Ri=u.dynCall_ii=(s,c)=>(Ri=u.dynCall_ii=F.tb)(s,c),Mi=s=>(Mi=F.ub)(s),Ui=()=>(Ui=F.vb)(),Vi=s=>(Vi=F.wb)(s),Ni=()=>(Ni=F.xb)();function Wi(){0<He||(y?(p(u),y||nr(Te),startWorker(u)):(nr(Wt),0<He||cr||(cr=!0,u.calledRun=!0,ge||(y||nr(Te),p(u),y||nr(Oe)))))}return u.stackSave=()=>Pn(),u.stackRestore=s=>fr(s),u.stackAlloc=s=>En(s),u.UTF8ToString=Ee,u.stringToUTF8=Pt,u.lengthBytesUTF8=Fo,ht=function s(){cr||Wi(),cr||(ht=s)},Wi(),h}),up=$a;globalThis.self?.name==="em-pthread"&&$a()});var Dt,dp,lp,cp,Sa,Ia,pp,Ca,Gt=M(()=>{"use strict";Sr();Dt= false?0:import.meta.url??(typeof document<"u"?document.currentScript?.src:typeof self<"u"?self.location?.href:void 0),dp= false||typeof location>"u"?void 0:location.origin,lp=(e,t)=>{try{let r=t??Dt;return(r?new URL(e,r):new URL(e)).origin===dp}catch{return!1}},cp=async e=>{let r=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(r)},Sa=(va(),hr(wa)).default,Ia=async()=>{if(!Dt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(lp(Dt))return[void 0,Sa()];let e=await cp(Dt);return[e,Sa(e)]},pp=(xa(),hr(_a)).default,Ca=async(e,t,r)=>[void 0,pp]});var Nn,Wn,zr,Ta,mp,fp,Ir,_e,$t=M(()=>{"use strict";Gt();Wn=!1,zr=!1,Ta=!1,mp=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},fp=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Ir=async e=>{if(Wn)return Promise.resolve();if(zr)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Ta)throw new Error("previous call to 'initializeWebAssembly()' failed.");zr=!0;let t=e.initTimeout,r=e.numThreads;if(!fp())throw new Error("WebAssembly SIMD is not supported in the current environment.");let n=mp();r>1&&!n&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=r=1);let o=e.wasmPaths,i=typeof o=="string"?o:void 0,a=o?.mjs,l=a?.href??a,d=o?.wasm,p=d?.href??d,m=e.wasmBinary,[u,h]=await Ca(l,i,r>1),w=!1,g=[];if(t>0&&g.push(new Promise(y=>{setTimeout(()=>{w=!0,y()},t)})),g.push(new Promise((y,x)=>{let _={numThreads:r};m?_.wasmBinary=m:(p||i)&&(_.locateFile=($,S)=>p??(i??S)+$),h(_).then($=>{zr=!1,Wn=!0,Nn=$,y(),u&&URL.revokeObjectURL(u)},$=>{zr=!1,Ta=!0,x($)})})),await Promise.race(g),w)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},_e=()=>{if(Wn&&Nn)return Nn;throw new Error("WebAssembly is not initialized yet.")}});var Ie,qt,we,Br=M(()=>{"use strict";$t();Ie=(e,t)=>{let r=_e(),n=r.lengthBytesUTF8(e)+1,o=r._malloc(n);return r.stringToUTF8(e,o,n),t.push(o),o},qt=(e,t,r,n)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([o,i])=>{let a=t?t+o:o;if(typeof i=="object")qt(i,a+".",r,n);else if(typeof i=="string"||typeof i=="number")n(a,i.toString());else if(typeof i=="boolean")n(a,i?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof i}`)})},we=e=>{let t=_e(),r=t.stackSave();try{let n=t.stackAlloc(8);t._OrtGetLastError(n,n+4);let o=t.HEAP32[n/4],i=t.HEAPU32[n/4+1],a=i?t.UTF8ToString(i):"";throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${a}`)}finally{t.stackRestore(r)}}});var Aa,ka=M(()=>{"use strict";$t();Br();Aa=e=>{let t=_e(),r=0,n=[],o=e||{};try{if(e?.logSeverityLevel===void 0)o.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)o.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(o.terminate=!1);let i=0;return e?.tag!==void 0&&(i=Ie(e.tag,n)),r=t._OrtCreateRunOptions(o.logSeverityLevel,o.logVerbosityLevel,!!o.terminate,i),r===0&&we("Can't create run options."),e?.extra!==void 0&&qt(e.extra,"",new WeakSet,(a,l)=>{let d=Ie(a,n),p=Ie(l,n);t._OrtAddRunConfigEntry(r,d,p)!==0&&we(`Can't set a run config entry: ${a} - ${l}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseRunOptions(r),n.forEach(a=>t._free(a)),i}}});var hp,gp,yp,bp,Ea,Pa=M(()=>{"use strict";$t();Br();hp=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},gp=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},yp=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},bp=(e,t,r)=>{for(let n of t){let o=typeof n=="string"?n:n.name;switch(o){case"webnn":if(o="WEBNN",typeof n!="string"){let l=n?.deviceType;if(l){let d=Ie("deviceType",r),p=Ie(l,r);_e()._OrtAddSessionConfigEntry(e,d,p)!==0&&we(`Can't set a session config entry: 'deviceType' - ${l}.`)}}break;case"webgpu":if(o="JS",typeof n!="string"){let a=n;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);let l=Ie("preferredLayout",r),d=Ie(a.preferredLayout,r);_e()._OrtAddSessionConfigEntry(e,l,d)!==0&&we(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${o}`)}let i=Ie(o,r);_e()._OrtAppendExecutionProvider(e,i)!==0&&we(`Can't append execution provider: ${o}.`)}},Ea=e=>{let t=_e(),r=0,n=[],o=e||{};yp(o);try{let i=hp(o.graphOptimizationLevel??"all"),a=gp(o.executionMode??"sequential"),l=typeof o.logId=="string"?Ie(o.logId,n):0,d=o.logSeverityLevel??2;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log serverity level is not valid: ${d}`);let p=o.logVerbosityLevel??0;if(!Number.isInteger(p)||p<0||p>4)throw new Error(`log verbosity level is not valid: ${p}`);let m=typeof o.optimizedModelFilePath=="string"?Ie(o.optimizedModelFilePath,n):0;if(r=t._OrtCreateSessionOptions(i,!!o.enableCpuMemArena,!!o.enableMemPattern,a,!!o.enableProfiling,0,l,d,p,m),r===0&&we("Can't create session options."),o.executionProviders&&bp(r,o.executionProviders,n),o.enableGraphCapture!==void 0){if(typeof o.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`);let u=Ie("enableGraphCapture",n),h=Ie(o.enableGraphCapture.toString(),n);t._OrtAddSessionConfigEntry(r,u,h)!==0&&we(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`)}if(o.freeDimensionOverrides)for(let[u,h]of Object.entries(o.freeDimensionOverrides)){if(typeof u!="string")throw new Error(`free dimension override name must be a string: ${u}`);if(typeof h!="number"||!Number.isInteger(h)||h<0)throw new Error(`free dimension override value must be a non-negative integer: ${h}`);let w=Ie(u,n);t._OrtAddFreeDimensionOverride(r,w,h)!==0&&we(`Can't set a free dimension override: ${u} - ${h}.`)}return o.extra!==void 0&&qt(o.extra,"",new WeakSet,(u,h)=>{let w=Ie(u,n),g=Ie(h,n);t._OrtAddSessionConfigEntry(r,w,g)!==0&&we(`Can't set a session config entry: ${u} - ${h}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseSessionOptions(r),n.forEach(a=>t._free(a)),i}}});var Ln,mt,_t,Dr,Kt,Rr,Hn,X=M(()=>{"use strict";Ln=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},mt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},_t=(e,t)=>{let r=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],n=typeof t=="number"?t:t.reduce((o,i)=>o*i,1);return r>0?Math.ceil(n*r):void 0},Dr=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Kt=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Rr=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool",Hn=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;default:throw new Error(`unsupported data location: ${e}`)}}});var jt,Gn=M(()=>{"use strict";Sr();jt=async e=>{if(typeof e=="string")if(false){}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),n=r?parseInt(r,10):0;if(n<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let o=t.body.getReader(),i;try{i=new ArrayBuffer(n)}catch(l){if(l instanceof RangeError){let d=Math.ceil(n/65536);i=new WebAssembly.Memory({initial:d,maximum:d}).buffer}else throw l}let a=0;for(;;){let{done:l,value:d}=await o.read();if(l)break;let p=d.byteLength;new Uint8Array(i,a,p).set(d),a+=p}return new Uint8Array(i,0,n)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var wp,vp,Oa,za,Ba,$p,be,st=M(()=>{"use strict";X();wp=["V","I","W","E","F"],vp=(e,t)=>{console.log(`[${wp[e]},${new Date().toISOString()}]${t}`)},Ba=(e,t)=>{Oa=e,za=t},$p=(e,t)=>{let r=Kt(e),n=Kt(Oa);r>=n&&vp(r,typeof t=="function"?t():t)},be=(...e)=>{za&&$p(...e)}});var Da,Ra=M(()=>{"use strict";X();Da=(e,t)=>new(Dr(t))(e)});var Mr=M(()=>{"use strict"});var Ma,Fn,qn,_p,xp,Ua,jn,Kn,Na,Wa=M(()=>{"use strict";st();Mr();Ma=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),Fn=[],qn=e=>Math.ceil(e/16)*16,_p=e=>{for(let t=0;t<Fn.length;t++){let r=Fn[t];if(e<=r)return r}return Math.ceil(e/16)*16},xp=1,Ua=()=>xp++,jn=async(e,t,r,n)=>{let o=qn(r),i=e.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let a=e.getCommandEncoder();e.endComputePass(),a.copyBufferToBuffer(t,0,i,0,o),e.flush(),await i.mapAsync(GPUMapMode.READ);let l=i.getMappedRange();if(n){let d=n();return d.set(new Uint8Array(l,0,r)),d}else return new Uint8Array(l.slice(0,r))}finally{i.destroy()}},Kn=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersForUploadingPending=[],this.buffersPending=[],this.externalBuffers=new Map,this.capturedPendingBuffers=new Map;for(let[r]of Ma)Fn.push(r),this.freeBuffers.set(r,[]),this.freeUniformBuffers.set(r,[])}upload(t,r){let n=r.buffer,o=r.byteOffset,i=r.byteLength,a=qn(i),l=this.storageCache.get(t);if(!l)throw new Error("gpu data for uploading does not exist");if(l.originalSize!==i)throw new Error(`inconsistent data size. gpu data size=${l.originalSize}, data size=${i}`);let d=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),p=d.getMappedRange();new Uint8Array(p).set(new Uint8Array(n,o,i)),d.unmap();let m=this.backend.getCommandEncoder();this.backend.endComputePass(),m.copyBufferToBuffer(d,0,l.gpuData.buffer,0,a),be("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(d)}memcpy(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(r);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=qn(n.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,i)}registerExternalBuffer(t,r,n){let o;if(n){if(o=this.externalBuffers.get(n),o===void 0)throw new Error("previous buffer is not registered");if(t===n)return be("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet.
|
|
3811
|
-
Please use the previous external buffer!`);this.externalBuffers.delete(n)}else o=Ua();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:t},originalSize:r}),this.externalBuffers.set(t,o),be("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`),o}unregisterExternalBuffer(t){let r=this.externalBuffers.get(t);r!==void 0&&(this.storageCache.delete(r),this.externalBuffers.delete(t),be("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${r}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=_p(t),o,i=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||a){let p=(i?this.freeBuffers:this.freeUniformBuffers).get(n);p?p.length>0?o=p.pop():o=this.backend.device.createBuffer({size:n,usage:r}):o=this.backend.device.createBuffer({size:n,usage:r})}else o=this.backend.device.createBuffer({size:n,usage:r});let l={id:Ua(),type:0,buffer:o};return this.storageCache.set(l.id,{gpuData:l,originalSize:t}),be("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${l.id}`),l}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=this.storageCache.get(t);if(!r)throw new Error("releasing data does not exist");return be("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("data does not exist");await jn(this.backend,n.gpuData.buffer,n.originalSize,r)}refreshPendingBuffers(){for(let t of this.buffersForUploadingPending)t.destroy();if(this.buffersForUploadingPending=[],this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending){let r=Ma.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else t.destroy()}this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(t))}},Na=(...e)=>new Kn(...e)});var Yn,Q,xe=M(()=>{"use strict";Yn=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},Q=e=>new Yn(e)});var Xn,Je,E,xt,Ur,Vr,Nr,oe=M(()=>{"use strict";Xn=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},Je=class{static calcShape(t,r,n=!1){let o=t.length,i=r.length;if(o===0)return r;if(i===0)return t;let a=Math.max(t.length,r.length),l=new Array(a);if(n){if(o<2||i<2)return;let d=Xn.calcMatMulShape([t[o-2],t[o-1]],[r[i-2],r[i-1]]);if(d===void 0)return;[l[a-2],l[a-1]]=d}for(let d=n?3:1;d<=a;d++){let p=o-d<0?1:t[o-d],m=i-d<0?1:r[i-d];if(p!==m&&p>1&&m>1)return;let u=Math.max(p,m);if(p&&m)l[a-d]=Math.max(p,m);else{if(u>1)return;l[a-d]=0}}return l}static isValidBroadcast(t,r){let n=t.length,o=r.length;if(n>o)return!1;for(let i=1;i<=n;i++)if(t[n-i]!==1&&t[n-i]!==r[o-i])return!1;return!0}},E=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let n=t.length;if(n===0)return[];let o=new Array(n),i=n-1;for(;i>=0;){if(t[i]%r===0){o[i]=t[i]/r;break}if(r%t[i]!==0)throw new Error("cannot convert shape");o[i]=1,r/=t[i],i--}for(i--;i>=0;i--)o[i]=t[i];return o}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,n){let o=1;for(let i=r;i<n;i++){if(t[i]<0)throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them.");o*=t[i]}return o}static computeStrides(t){let r=t.length;if(r===0)return[];if(r===1)return[1];let n=new Array(r);n[r-1]=1,n[r-2]=t[r-1];for(let o=r-3;o>=0;--o)n[o]=n[o+1]*t[o+1];return n}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(n=>this.normalizeAxis(n,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(n=>t[n]):t.slice().reverse()}static padShape(t,r){let n=t.length;return t.map((o,i)=>o+r[i]+r[i+n])}static areEqual(t,r){return t.length!==r.length?!1:t.every((n,o)=>n===r[o])}},xt=class e{static adjustPoolAttributes(t,r,n,o,i,a){if(!t&&n.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let l=0;l<r.length-2;l++)l>=n.length?n.push(r[l+2]):n[l]=r[l+2];for(let l=0;l<n.length;l++)if(l<o.length){if(o[l]<0)throw new Error("strides should be greater than or equal to 1")}else o.push(1);for(let l=0;l<n.length;l++)if(l<i.length){if(i[l]<0)throw new Error("dilations should be greater than or equal to 1")}else i.push(1);for(let l=0;l<n.length*2;l++)if(l<a.length){if(a[l]<0)throw new Error("pad should be greater than or equal to 1")}else a.push(0);for(let l=0;l<n.length;l++){if(n[l]<=0)throw new Error("kernel shapes need to be greater than 0");if(a[l]>=n[l]||a[l+n.length]>=n[l])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,n,o,i,a,l){if(l){if(i.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(o.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let d=0;d<t.length-2;d++)e.adjustPadAndReturnShape(t[d+(a?1:2)],r[d],n[d],o[d],i,d,d+t.length-2,l)}}static computePoolOutputShape(t,r,n,o,i,a,l){if(r.length<=0)throw new Error("input shape must be of size greater than 0");let d=[r[0],r[1]];return e.computeShapeHelper(t,r,d,n,o,i,a,l),d}static computeConvOutputShape(t,r,n,o,i,a,l){if(t.length<=0||r.length<=0)throw new Error("invalid input tensor dims or invalid filter tensor dims");let d=[t[0],r[0]];return e.computeShapeHelper(!1,t,d,n,o,i,a,l),d}static computeShapeHelper(t,r,n,o,i,a,l,d){if(t)for(let p=0;p<r.length-2;p++)n.push(1);else for(let p=0;p<r.length-2;p++)n.push(e.adjustPadAndReturnShape(r[p+2],o[p],i[p],a[p],l,p,p+r.length-2,d))}static adjustPadAndReturnShape(t,r,n,o,i,a,l,d){let p=n*(o-1)+1;if(d&&d!=="NOTSET")switch(d){case"VALID":return i[a]=0,i[l]=0,Math.floor((t-p)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(n!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let u=((t+r-1)/r-1)*r+o-t;return i[a]=Math.floor(d==="SAME_LOWER"?(u+1)/2:u/2),i[l]=u-i[a],Math.floor((t+u-o)/r+1)}default:throw new Error("Unsupported AutoPad type")}else return Math.floor((t+i[a]+i[l]-p)/r+1)}},Ur=class{static getShapeOfGemmResult(t,r,n,o,i){if(t.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let a,l,d;r?(a=t[1],l=t[0]):(a=t[0],l=t[1]);let p=-1;if(o?(d=n[0],p=1):(d=n[1],p=0),n[p]!==l)throw new Error("dimension mismatch");if(a<=0||d<=0||l<=0)throw new Error("invalid shape specified");if(i&&!Je.isValidBroadcast(i,[a,d]))throw new Error("gemm: invalid bias shape for broadcast");return[a,d,l]}},Vr=-34028234663852886e22,Nr=34028234663852886e22});var St,Qn,pe,ke,U,he,ft,It,je,L,Jn,k,D,Wr,Zn,La,Mt,re=M(()=>{"use strict";X();oe();St=64,Qn=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(e){case 10:return t>1?`vec${t}<f16>`:"f16";case 1:return t>1?`vec${t}<f32>`:"f32";case 6:return t>1?`vec${t}<i32>`:"i32";case 12:return t>1?`vec${t}<u32>`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4<bool>"];default:throw new Error(`Unknown data type: ${e}`)}},pe=(e,t=1)=>{let r=Qn(e,t);return typeof r=="string"?r:r[0]},ke=(e,t=1)=>{let r=Qn(e,t);return typeof r=="string"?r:r[1]},U=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:E.computeStrides(r)})}),t},he=e=>e%4===0?4:e%2===0?2:1,ft=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,It=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}<f32>(${r})`,je=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,L=(e,t,r,n)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?n==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:n==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,Jn=(e,t,r,n,o)=>{let i=typeof r=="number",a=i?r:r.length,l=[...new Array(a).keys()],d=a<2?"u32":a<=4?`vec${a}<u32>`:`array<u32, ${a}>`,p=Qn(t,o),m=typeof p=="string"?p:p[1],u=typeof p=="string"?p:p[0],h={indices:d,value:m,storage:u,tensor:t},w=R=>typeof R=="string"?R:`${R}u`,g={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},y=i?"uniforms.":"",x=`${y}${e}_shape`,_=`${y}${e}_strides`,$="";for(let R=0;R<a-1;R++)$+=`
|
|
3808
|
+
`),s=function(J){var de=Function;if(!(de instanceof Function))throw new TypeError(`new_ called with constructor type ${typeof de} which is not a function`);var ye=mi(de.name||"unknownFunctionName",function(){});return ye.prototype=de.prototype,ye=new ye,(J=de.apply(ye,J))instanceof Object?J:ye}(B)(...N),f=`methodCaller<(${p.map(J=>J.name).join(", ")}) => ${b.name}>`,$c(mi(f,s))}function Sc(s){return s=ur(s>>>0),Fe(u[s])}function Ic(s,p){return p>>>=0,s=Ge(s>>>0),p=Ge(p),Fe(s[p])}function Cc(s){9<(s>>>=0)&&(at[s+1]+=1)}function Tc(){return Fe([])}function Ac(s){s=Ge(s>>>0);for(var p=Array(s.length),f=0;f<s.length;f++)p[f]=s[f];return Fe(p)}function kc(s){return Fe(ur(s>>>0))}function Ec(){return Fe({})}function Pc(s){for(var p=Ge(s>>>=0);p.length;){var f=p.pop();p.pop()(f)}wn(s)}function Oc(s,p,f){p>>>=0,f>>>=0,s=Ge(s>>>0),p=Ge(p),f=Ge(f),s[p]=f}function zc(s,p){return p>>>=0,s=(s=xn(s>>>0,"_emval_take_value")).readValueFromPointer(p),Fe(s)}function Bc(s,p){s=-9007199254740992>s||9007199254740992<s?NaN:Number(s),p>>>=0,s=new Date(1e3*s),i()[p>>>2>>>0]=s.getUTCSeconds(),i()[p+4>>>2>>>0]=s.getUTCMinutes(),i()[p+8>>>2>>>0]=s.getUTCHours(),i()[p+12>>>2>>>0]=s.getUTCDate(),i()[p+16>>>2>>>0]=s.getUTCMonth(),i()[p+20>>>2>>>0]=s.getUTCFullYear()-1900,i()[p+24>>>2>>>0]=s.getUTCDay(),s=(s.getTime()-Date.UTC(s.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,i()[p+28>>>2>>>0]=s}var fi=s=>s%4==0&&(s%100!=0||s%400==0),hi=[0,31,60,91,121,152,182,213,244,274,305,335],gi=[0,31,59,90,120,151,181,212,243,273,304,334];function Dc(s,p){s=-9007199254740992>s||9007199254740992<s?NaN:Number(s),p>>>=0,s=new Date(1e3*s),i()[p>>>2>>>0]=s.getSeconds(),i()[p+4>>>2>>>0]=s.getMinutes(),i()[p+8>>>2>>>0]=s.getHours(),i()[p+12>>>2>>>0]=s.getDate(),i()[p+16>>>2>>>0]=s.getMonth(),i()[p+20>>>2>>>0]=s.getFullYear()-1900,i()[p+24>>>2>>>0]=s.getDay();var f=(fi(s.getFullYear())?hi:gi)[s.getMonth()]+s.getDate()-1|0;i()[p+28>>>2>>>0]=f,i()[p+36>>>2>>>0]=-60*s.getTimezoneOffset(),f=new Date(s.getFullYear(),6,1).getTimezoneOffset();var b=new Date(s.getFullYear(),0,1).getTimezoneOffset();s=0|(f!=b&&s.getTimezoneOffset()==Math.min(b,f)),i()[p+32>>>2>>>0]=s}function Rc(s){s>>>=0;var p=new Date(i()[s+20>>>2>>>0]+1900,i()[s+16>>>2>>>0],i()[s+12>>>2>>>0],i()[s+8>>>2>>>0],i()[s+4>>>2>>>0],i()[s>>>2>>>0],0),f=i()[s+32>>>2>>>0],b=p.getTimezoneOffset(),v=new Date(p.getFullYear(),6,1).getTimezoneOffset(),C=new Date(p.getFullYear(),0,1).getTimezoneOffset(),P=Math.min(C,v);return 0>f?i()[s+32>>>2>>>0]=+(v!=C&&P==b):0<f!=(P==b)&&(v=Math.max(C,v),p.setTime(p.getTime()+6e4*((0<f?P:v)-b))),i()[s+24>>>2>>>0]=p.getDay(),f=(fi(p.getFullYear())?hi:gi)[p.getMonth()]+p.getDate()-1|0,i()[s+28>>>2>>>0]=f,i()[s>>>2>>>0]=p.getSeconds(),i()[s+4>>>2>>>0]=p.getMinutes(),i()[s+8>>>2>>>0]=p.getHours(),i()[s+12>>>2>>>0]=p.getDate(),i()[s+16>>>2>>>0]=p.getMonth(),i()[s+20>>>2>>>0]=p.getYear(),s=p.getTime(),BigInt(isNaN(s)?-1:s/1e3)}function yi(s,p,f,b,v,C,P){return y?ve(16,1,s,p,f,b,v,C,P):-52}function bi(s,p,f,b,v,C){if(y)return ve(17,1,s,p,f,b,v,C)}function Mc(s,p,f,b){s>>>=0,p>>>=0,f>>>=0,b>>>=0;var v=new Date().getFullYear(),C=new Date(v,0,1),P=new Date(v,6,1);v=C.getTimezoneOffset();var B=P.getTimezoneOffset(),N=Math.max(v,B);a()[s>>>2>>>0]=60*N,i()[p>>>2>>>0]=+(v!=B),C=(s=H=>H.toLocaleTimeString(void 0,{hour12:!1,timeZoneName:"short"}).split(" ")[1])(C),P=s(P),B<v?(Pt(C,f,17),Pt(P,b,17)):(Pt(C,b,17),Pt(P,f,17))}var In=[],wi=(s,p)=>{In.length=0;for(var f;f=r()[s++>>>0];){var b=f!=105;p+=(b&=f!=112)&&p%8?4:0,In.push(f==112?a()[p>>>2>>>0]:f==106?G[p>>>3]:f==105?i()[p>>>2>>>0]:d()[p>>>3>>>0]),p+=b?8:4}return In};function Uc(s,p,f){return s>>>=0,p=wi(p>>>0,f>>>0),cn[s](...p)}function Vc(s,p,f){return s>>>=0,p=wi(p>>>0,f>>>0),cn[s](...p)}var Nc=()=>{},Wc=()=>Date.now();function Lc(s,p){return Y(Ee(s>>>0,p>>>0))}var vi,Hc=()=>{throw gt+=1,"unwind"};function Gc(){return 4294901760}vi=()=>performance.timeOrigin+performance.now();var Fc=()=>navigator.hardwareConcurrency;function qc(){return Et("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"),0}function Kc(s){s>>>=0;var p=r().length;if(s<=p||4294901760<s)return!1;for(var f=1;4>=f;f*=2){var b=p*(1+.2/f);b=Math.min(b,s+100663296);var v=Math;b=Math.max(s,b);e:{v=(v.min.call(v,4294901760,b+(65536-b%65536)%65536)-ie.buffer.byteLength+65535)/65536;try{ie.grow(v),Se();var C=1;break e}catch{}C=void 0}if(C)return!0}return!1}var dr=()=>(Et("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"),0),Ot={},$i=s=>{s.forEach(p=>{var f=dr();f&&(Ot[f]=p)})};function jc(){var s=Error().stack.toString().split(`
|
|
3809
|
+
`);return s[0]=="Error"&&s.shift(),$i(s),Ot.Gb=dr(),Ot.Tb=s,Ot.Gb}function Yc(s,p,f){if(s>>>=0,p>>>=0,Ot.Gb==s)var b=Ot.Tb;else(b=Error().stack.toString().split(`
|
|
3810
|
+
`))[0]=="Error"&&b.shift(),$i(b);for(var v=3;b[v]&&dr()!=s;)++v;for(s=0;s<f&&b[s+v];++s)i()[p+4*s>>>2>>>0]=dr();return s}var Cn,Tn={},_i=()=>{if(!Cn){var s,p={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:T||"./this.program"};for(s in Tn)Tn[s]===void 0?delete p[s]:p[s]=Tn[s];var f=[];for(s in p)f.push(`${s}=${p[s]}`);Cn=f}return Cn};function xi(s,p){if(y)return ve(18,1,s,p);s>>>=0,p>>>=0;var f=0;return _i().forEach((b,v)=>{var C=p+f;for(v=a()[s+4*v>>>2>>>0]=C,C=0;C<b.length;++C)t()[v++>>>0]=b.charCodeAt(C);t()[v>>>0]=0,f+=b.length+1}),0}function Si(s,p){if(y)return ve(19,1,s,p);s>>>=0,p>>>=0;var f=_i();a()[s>>>2>>>0]=f.length;var b=0;return f.forEach(v=>b+=v.length+1),a()[p>>>2>>>0]=b,0}function Ii(s){return y?ve(20,1,s):52}function Ci(s,p,f,b){return y?ve(21,1,s,p,f,b):52}function Ti(s,p,f,b){return y?ve(22,1,s,p,f,b):70}var Xc=[null,[],[]];function Ai(s,p,f,b){if(y)return ve(23,1,s,p,f,b);p>>>=0,f>>>=0,b>>>=0;for(var v=0,C=0;C<f;C++){var P=a()[p>>>2>>>0],B=a()[p+4>>>2>>>0];p+=8;for(var N=0;N<B;N++){var H=r()[P+N>>>0],J=Xc[s];H===0||H===10?((s===1?j:Y)(Lo(J,0)),J.length=0):J.push(H)}v+=B}return a()[b>>>2>>>0]=v,0}y||function(){for(var s=u.numThreads-1;s--;)Uo();Wt.unshift(()=>{He++,function(p){y?p():Promise.all(dt.map(Mo)).then(p)}(()=>Co())})}();for(var ki=Array(256),lr=0;256>lr;++lr)ki[lr]=String.fromCharCode(lr);ni=ki,lt=u.BindingError=class extends Error{constructor(s){super(s),this.name="BindingError"}},u.InternalError=class extends Error{constructor(s){super(s),this.name="InternalError"}},at.push(0,1,void 0,1,null,1,!0,1,!1,1),u.count_emval_handles=()=>at.length/2-5-bn.length;var Zc=[fn,zo,Vo,Ho,Go,qo,Ko,jo,Yo,Xo,Zo,Qo,Jo,ei,ti,ri,yi,bi,xi,Si,Ii,Ci,Ti,Ai],F=function(){function s(f,b){return F=f.exports,F=function(){var v=F,C={};for(let[P,B]of Object.entries(v))C[P]=typeof B=="function"?(...N)=>{ar.push(P);try{return B(...N)}finally{ge||(ar.pop(),Ze&&pt===1&&ar.length===0&&(pt=0,gt+=1,ir(Ui),typeof Fibers<"u"&&Fibers.ac()))}}:B;return C}(),F=function(){var v=F,C=B=>N=>B(N)>>>0,P=B=>()=>B()>>>0;return(v=Object.assign({},v)).Aa=C(v.Aa),v.db=P(v.db),v.eb=C(v.eb),v.emscripten_main_runtime_thread_id=P(v.emscripten_main_runtime_thread_id),v.qb=C(v.qb),v.rb=P(v.rb),v}(),Bo.push(F.gb),Te.unshift(F.za),q=b,Co(),F}var p=Po();if(He++,u.instantiateWasm)try{return u.instantiateWasm(p,s)}catch(f){Y(`Module.instantiateWasm callback failed with error: ${f}`),m(f)}return ln||=u.locateFile?To("ort-wasm-simd-threaded.jsep.wasm")?"ort-wasm-simd-threaded.jsep.wasm":u.locateFile?u.locateFile("ort-wasm-simd-threaded.jsep.wasm",O):O+"ort-wasm-simd-threaded.jsep.wasm":new URL(/* asset import */ __webpack_require__(/*! ort-wasm-simd-threaded.jsep.wasm */ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm"), __webpack_require__.b).href,function(f,b){var v=ln;return z||typeof WebAssembly.instantiateStreaming!="function"||To(v)||Ao(v)||typeof fetch!="function"?Eo(v,f,b):fetch(v,{credentials:"same-origin"}).then(C=>WebAssembly.instantiateStreaming(C,f).then(b,function(P){return Y(`wasm streaming compile failed: ${P}`),Y("falling back to ArrayBuffer instantiation"),Eo(v,f,b)}))}(p,function(f){s(f.instance,f.module)}).catch(m),{}}(),Ei=s=>(Ei=F.Aa)(s),Pi=()=>(Pi=F.Ba)();u._OrtInit=(s,p)=>(u._OrtInit=F.Ca)(s,p),u._OrtGetLastError=(s,p)=>(u._OrtGetLastError=F.Da)(s,p),u._OrtCreateSessionOptions=(s,p,f,b,v,C,P,B,N,H)=>(u._OrtCreateSessionOptions=F.Ea)(s,p,f,b,v,C,P,B,N,H),u._OrtAppendExecutionProvider=(s,p)=>(u._OrtAppendExecutionProvider=F.Fa)(s,p),u._OrtAddFreeDimensionOverride=(s,p,f)=>(u._OrtAddFreeDimensionOverride=F.Ga)(s,p,f),u._OrtAddSessionConfigEntry=(s,p,f)=>(u._OrtAddSessionConfigEntry=F.Ha)(s,p,f),u._OrtReleaseSessionOptions=s=>(u._OrtReleaseSessionOptions=F.Ia)(s),u._OrtCreateSession=(s,p,f)=>(u._OrtCreateSession=F.Ja)(s,p,f),u._OrtReleaseSession=s=>(u._OrtReleaseSession=F.Ka)(s),u._OrtGetInputOutputCount=(s,p,f)=>(u._OrtGetInputOutputCount=F.La)(s,p,f),u._OrtGetInputName=(s,p)=>(u._OrtGetInputName=F.Ma)(s,p),u._OrtGetOutputName=(s,p)=>(u._OrtGetOutputName=F.Na)(s,p),u._OrtFree=s=>(u._OrtFree=F.Oa)(s),u._OrtCreateTensor=(s,p,f,b,v,C)=>(u._OrtCreateTensor=F.Pa)(s,p,f,b,v,C),u._OrtGetTensorData=(s,p,f,b,v)=>(u._OrtGetTensorData=F.Qa)(s,p,f,b,v),u._OrtReleaseTensor=s=>(u._OrtReleaseTensor=F.Ra)(s),u._OrtCreateRunOptions=(s,p,f,b)=>(u._OrtCreateRunOptions=F.Sa)(s,p,f,b),u._OrtAddRunConfigEntry=(s,p,f)=>(u._OrtAddRunConfigEntry=F.Ta)(s,p,f),u._OrtReleaseRunOptions=s=>(u._OrtReleaseRunOptions=F.Ua)(s),u._OrtCreateBinding=s=>(u._OrtCreateBinding=F.Va)(s),u._OrtBindInput=(s,p,f)=>(u._OrtBindInput=F.Wa)(s,p,f),u._OrtBindOutput=(s,p,f,b)=>(u._OrtBindOutput=F.Xa)(s,p,f,b),u._OrtClearBoundOutputs=s=>(u._OrtClearBoundOutputs=F.Ya)(s),u._OrtReleaseBinding=s=>(u._OrtReleaseBinding=F.Za)(s),u._OrtRunWithBinding=(s,p,f,b,v)=>(u._OrtRunWithBinding=F._a)(s,p,f,b,v),u._OrtRun=(s,p,f,b,v,C,P,B)=>(u._OrtRun=F.$a)(s,p,f,b,v,C,P,B),u._OrtEndProfiling=s=>(u._OrtEndProfiling=F.ab)(s),u._JsepOutput=(s,p,f)=>(u._JsepOutput=F.bb)(s,p,f),u._JsepGetNodeName=s=>(u._JsepGetNodeName=F.cb)(s);var cr,zt=()=>(zt=F.db)(),pr=u._malloc=s=>(pr=u._malloc=F.eb)(s),Qe=u._free=s=>(Qe=u._free=F.fb)(s),An=(s,p,f,b,v,C)=>(An=F.ib)(s,p,f,b,v,C),Oi=()=>(Oi=F.jb)(),zi=(s,p,f,b,v)=>(zi=F.kb)(s,p,f,b,v),kn=s=>(kn=F.lb)(s),mr=s=>(mr=F.mb)(s),Bi=()=>(Bi=F.nb)(),Di=(s,p)=>(Di=F.ob)(s,p),fr=s=>(fr=F.pb)(s),En=s=>(En=F.qb)(s),Pn=()=>(Pn=F.rb)(),Ri=u.dynCall_ii=(s,p)=>(Ri=u.dynCall_ii=F.tb)(s,p),Mi=s=>(Mi=F.ub)(s),Ui=()=>(Ui=F.vb)(),Vi=s=>(Vi=F.wb)(s),Ni=()=>(Ni=F.xb)();function Wi(){0<He||(y?(c(u),y||nr(Te),startWorker(u)):(nr(Wt),0<He||cr||(cr=!0,u.calledRun=!0,ge||(y||nr(Te),c(u),y||nr(Oe)))))}return u.stackSave=()=>Pn(),u.stackRestore=s=>fr(s),u.stackAlloc=s=>En(s),u.UTF8ToString=Ee,u.stringToUTF8=Pt,u.lengthBytesUTF8=Fo,ht=function s(){cr||Wi(),cr||(ht=s)},Wi(),h}),up=$a;globalThis.self?.name==="em-pthread"&&$a()});var Dt,dp,lp,cp,Sa,Ia,pp,Ca,Gt=M(()=>{"use strict";Sr();Dt= false?0:import.meta.url??(typeof document<"u"?document.currentScript?.src:typeof self<"u"?self.location?.href:void 0),dp= false||typeof location>"u"?void 0:location.origin,lp=(e,t)=>{try{let r=t??Dt;return(r?new URL(e,r):new URL(e)).origin===dp}catch{return!1}},cp=async e=>{let r=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(r)},Sa=(va(),hr(wa)).default,Ia=async()=>{if(!Dt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(lp(Dt))return[void 0,Sa()];let e=await cp(Dt);return[e,Sa(e)]},pp=(xa(),hr(_a)).default,Ca=async(e,t,r)=>[void 0,pp]});var Nn,Wn,zr,Ta,mp,fp,Ir,_e,$t=M(()=>{"use strict";Gt();Wn=!1,zr=!1,Ta=!1,mp=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},fp=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Ir=async e=>{if(Wn)return Promise.resolve();if(zr)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Ta)throw new Error("previous call to 'initializeWebAssembly()' failed.");zr=!0;let t=e.initTimeout,r=e.numThreads;if(!fp())throw new Error("WebAssembly SIMD is not supported in the current environment.");let n=mp();r>1&&!n&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=r=1);let o=e.wasmPaths,i=typeof o=="string"?o:void 0,a=o?.mjs,l=a?.href??a,d=o?.wasm,c=d?.href??d,m=e.wasmBinary,[u,h]=await Ca(l,i,r>1),w=!1,g=[];if(t>0&&g.push(new Promise(y=>{setTimeout(()=>{w=!0,y()},t)})),g.push(new Promise((y,x)=>{let _={numThreads:r};m?_.wasmBinary=m:(c||i)&&(_.locateFile=($,S)=>c??(i??S)+$),h(_).then($=>{zr=!1,Wn=!0,Nn=$,y(),u&&URL.revokeObjectURL(u)},$=>{zr=!1,Ta=!0,x($)})})),await Promise.race(g),w)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},_e=()=>{if(Wn&&Nn)return Nn;throw new Error("WebAssembly is not initialized yet.")}});var Ie,qt,we,Br=M(()=>{"use strict";$t();Ie=(e,t)=>{let r=_e(),n=r.lengthBytesUTF8(e)+1,o=r._malloc(n);return r.stringToUTF8(e,o,n),t.push(o),o},qt=(e,t,r,n)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([o,i])=>{let a=t?t+o:o;if(typeof i=="object")qt(i,a+".",r,n);else if(typeof i=="string"||typeof i=="number")n(a,i.toString());else if(typeof i=="boolean")n(a,i?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof i}`)})},we=e=>{let t=_e(),r=t.stackSave();try{let n=t.stackAlloc(8);t._OrtGetLastError(n,n+4);let o=t.HEAP32[n/4],i=t.HEAPU32[n/4+1],a=i?t.UTF8ToString(i):"";throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${a}`)}finally{t.stackRestore(r)}}});var Aa,ka=M(()=>{"use strict";$t();Br();Aa=e=>{let t=_e(),r=0,n=[],o=e||{};try{if(e?.logSeverityLevel===void 0)o.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)o.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(o.terminate=!1);let i=0;return e?.tag!==void 0&&(i=Ie(e.tag,n)),r=t._OrtCreateRunOptions(o.logSeverityLevel,o.logVerbosityLevel,!!o.terminate,i),r===0&&we("Can't create run options."),e?.extra!==void 0&&qt(e.extra,"",new WeakSet,(a,l)=>{let d=Ie(a,n),c=Ie(l,n);t._OrtAddRunConfigEntry(r,d,c)!==0&&we(`Can't set a run config entry: ${a} - ${l}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseRunOptions(r),n.forEach(a=>t._free(a)),i}}});var hp,gp,yp,bp,Ea,Pa=M(()=>{"use strict";$t();Br();hp=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},gp=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},yp=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},bp=(e,t,r)=>{for(let n of t){let o=typeof n=="string"?n:n.name;switch(o){case"webnn":if(o="WEBNN",typeof n!="string"){let l=n?.deviceType;if(l){let d=Ie("deviceType",r),c=Ie(l,r);_e()._OrtAddSessionConfigEntry(e,d,c)!==0&&we(`Can't set a session config entry: 'deviceType' - ${l}.`)}}break;case"webgpu":if(o="JS",typeof n!="string"){let a=n;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);let l=Ie("preferredLayout",r),d=Ie(a.preferredLayout,r);_e()._OrtAddSessionConfigEntry(e,l,d)!==0&&we(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${o}`)}let i=Ie(o,r);_e()._OrtAppendExecutionProvider(e,i)!==0&&we(`Can't append execution provider: ${o}.`)}},Ea=e=>{let t=_e(),r=0,n=[],o=e||{};yp(o);try{let i=hp(o.graphOptimizationLevel??"all"),a=gp(o.executionMode??"sequential"),l=typeof o.logId=="string"?Ie(o.logId,n):0,d=o.logSeverityLevel??2;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log serverity level is not valid: ${d}`);let c=o.logVerbosityLevel??0;if(!Number.isInteger(c)||c<0||c>4)throw new Error(`log verbosity level is not valid: ${c}`);let m=typeof o.optimizedModelFilePath=="string"?Ie(o.optimizedModelFilePath,n):0;if(r=t._OrtCreateSessionOptions(i,!!o.enableCpuMemArena,!!o.enableMemPattern,a,!!o.enableProfiling,0,l,d,c,m),r===0&&we("Can't create session options."),o.executionProviders&&bp(r,o.executionProviders,n),o.enableGraphCapture!==void 0){if(typeof o.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`);let u=Ie("enableGraphCapture",n),h=Ie(o.enableGraphCapture.toString(),n);t._OrtAddSessionConfigEntry(r,u,h)!==0&&we(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`)}if(o.freeDimensionOverrides)for(let[u,h]of Object.entries(o.freeDimensionOverrides)){if(typeof u!="string")throw new Error(`free dimension override name must be a string: ${u}`);if(typeof h!="number"||!Number.isInteger(h)||h<0)throw new Error(`free dimension override value must be a non-negative integer: ${h}`);let w=Ie(u,n);t._OrtAddFreeDimensionOverride(r,w,h)!==0&&we(`Can't set a free dimension override: ${u} - ${h}.`)}return o.extra!==void 0&&qt(o.extra,"",new WeakSet,(u,h)=>{let w=Ie(u,n),g=Ie(h,n);t._OrtAddSessionConfigEntry(r,w,g)!==0&&we(`Can't set a session config entry: ${u} - ${h}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseSessionOptions(r),n.forEach(a=>t._free(a)),i}}});var Ln,mt,_t,Dr,Kt,Rr,Hn,X=M(()=>{"use strict";Ln=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},mt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},_t=(e,t)=>{let r=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],n=typeof t=="number"?t:t.reduce((o,i)=>o*i,1);return r>0?Math.ceil(n*r):void 0},Dr=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Kt=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Rr=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool",Hn=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;default:throw new Error(`unsupported data location: ${e}`)}}});var jt,Gn=M(()=>{"use strict";Sr();jt=async e=>{if(typeof e=="string")if(false){}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),n=r?parseInt(r,10):0;if(n<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let o=t.body.getReader(),i;try{i=new ArrayBuffer(n)}catch(l){if(l instanceof RangeError){let d=Math.ceil(n/65536);i=new WebAssembly.Memory({initial:d,maximum:d}).buffer}else throw l}let a=0;for(;;){let{done:l,value:d}=await o.read();if(l)break;let c=d.byteLength;new Uint8Array(i,a,c).set(d),a+=c}return new Uint8Array(i,0,n)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var wp,vp,Oa,za,Ba,$p,be,st=M(()=>{"use strict";X();wp=["V","I","W","E","F"],vp=(e,t)=>{console.log(`[${wp[e]},${new Date().toISOString()}]${t}`)},Ba=(e,t)=>{Oa=e,za=t},$p=(e,t)=>{let r=Kt(e),n=Kt(Oa);r>=n&&vp(r,typeof t=="function"?t():t)},be=(...e)=>{za&&$p(...e)}});var Da,Ra=M(()=>{"use strict";X();Da=(e,t)=>new(Dr(t))(e)});var Mr=M(()=>{"use strict"});var Ma,Fn,qn,_p,xp,Ua,jn,Kn,Na,Wa=M(()=>{"use strict";st();Mr();Ma=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),Fn=[],qn=e=>Math.ceil(e/16)*16,_p=e=>{for(let t=0;t<Fn.length;t++){let r=Fn[t];if(e<=r)return r}return Math.ceil(e/16)*16},xp=1,Ua=()=>xp++,jn=async(e,t,r,n)=>{let o=qn(r),i=e.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let a=e.getCommandEncoder();e.endComputePass(),a.copyBufferToBuffer(t,0,i,0,o),e.flush(),await i.mapAsync(GPUMapMode.READ);let l=i.getMappedRange();if(n){let d=n();return d.set(new Uint8Array(l,0,r)),d}else return new Uint8Array(l.slice(0,r))}finally{i.destroy()}},Kn=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersForUploadingPending=[],this.buffersPending=[],this.externalBuffers=new Map,this.capturedPendingBuffers=new Map;for(let[r]of Ma)Fn.push(r),this.freeBuffers.set(r,[]),this.freeUniformBuffers.set(r,[])}upload(t,r){let n=r.buffer,o=r.byteOffset,i=r.byteLength,a=qn(i),l=this.storageCache.get(t);if(!l)throw new Error("gpu data for uploading does not exist");if(l.originalSize!==i)throw new Error(`inconsistent data size. gpu data size=${l.originalSize}, data size=${i}`);let d=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),c=d.getMappedRange();new Uint8Array(c).set(new Uint8Array(n,o,i)),d.unmap();let m=this.backend.getCommandEncoder();this.backend.endComputePass(),m.copyBufferToBuffer(d,0,l.gpuData.buffer,0,a),be("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(d)}memcpy(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(r);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=qn(n.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,i)}registerExternalBuffer(t,r,n){let o;if(n){if(o=this.externalBuffers.get(n),o===void 0)throw new Error("previous buffer is not registered");if(t===n)return be("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet.
|
|
3811
|
+
Please use the previous external buffer!`);this.externalBuffers.delete(n)}else o=Ua();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:t},originalSize:r}),this.externalBuffers.set(t,o),be("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`),o}unregisterExternalBuffer(t){let r=this.externalBuffers.get(t);r!==void 0&&(this.storageCache.delete(r),this.externalBuffers.delete(t),be("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${r}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=_p(t),o,i=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||a){let c=(i?this.freeBuffers:this.freeUniformBuffers).get(n);c?c.length>0?o=c.pop():o=this.backend.device.createBuffer({size:n,usage:r}):o=this.backend.device.createBuffer({size:n,usage:r})}else o=this.backend.device.createBuffer({size:n,usage:r});let l={id:Ua(),type:0,buffer:o};return this.storageCache.set(l.id,{gpuData:l,originalSize:t}),be("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${l.id}`),l}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=this.storageCache.get(t);if(!r)throw new Error("releasing data does not exist");return be("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("data does not exist");await jn(this.backend,n.gpuData.buffer,n.originalSize,r)}refreshPendingBuffers(){for(let t of this.buffersForUploadingPending)t.destroy();if(this.buffersForUploadingPending=[],this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending){let r=Ma.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else t.destroy()}this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(t))}},Na=(...e)=>new Kn(...e)});var Yn,Q,xe=M(()=>{"use strict";Yn=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},Q=e=>new Yn(e)});var Xn,Je,k,xt,Ur,Vr,Nr,te=M(()=>{"use strict";Xn=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},Je=class{static calcShape(t,r,n=!1){let o=t.length,i=r.length;if(o===0)return r;if(i===0)return t;let a=Math.max(t.length,r.length),l=new Array(a);if(n){if(o<2||i<2)return;let d=Xn.calcMatMulShape([t[o-2],t[o-1]],[r[i-2],r[i-1]]);if(d===void 0)return;[l[a-2],l[a-1]]=d}for(let d=n?3:1;d<=a;d++){let c=o-d<0?1:t[o-d],m=i-d<0?1:r[i-d];if(c!==m&&c>1&&m>1)return;let u=Math.max(c,m);if(c&&m)l[a-d]=Math.max(c,m);else{if(u>1)return;l[a-d]=0}}return l}static isValidBroadcast(t,r){let n=t.length,o=r.length;if(n>o)return!1;for(let i=1;i<=n;i++)if(t[n-i]!==1&&t[n-i]!==r[o-i])return!1;return!0}},k=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let n=t.length;if(n===0)return[];let o=new Array(n),i=n-1;for(;i>=0;){if(t[i]%r===0){o[i]=t[i]/r;break}if(r%t[i]!==0)throw new Error("cannot convert shape");o[i]=1,r/=t[i],i--}for(i--;i>=0;i--)o[i]=t[i];return o}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,n){let o=1;for(let i=r;i<n;i++){if(t[i]<0)throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them.");o*=t[i]}return o}static computeStrides(t){let r=t.length;if(r===0)return[];if(r===1)return[1];let n=new Array(r);n[r-1]=1,n[r-2]=t[r-1];for(let o=r-3;o>=0;--o)n[o]=n[o+1]*t[o+1];return n}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(n=>this.normalizeAxis(n,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(n=>t[n]):t.slice().reverse()}static padShape(t,r){let n=t.length;return t.map((o,i)=>o+r[i]+r[i+n])}static areEqual(t,r){return t.length!==r.length?!1:t.every((n,o)=>n===r[o])}},xt=class e{static adjustPoolAttributes(t,r,n,o,i,a){if(!t&&n.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let l=0;l<r.length-2;l++)l>=n.length?n.push(r[l+2]):n[l]=r[l+2];for(let l=0;l<n.length;l++)if(l<o.length){if(o[l]<0)throw new Error("strides should be greater than or equal to 1")}else o.push(1);for(let l=0;l<n.length;l++)if(l<i.length){if(i[l]<0)throw new Error("dilations should be greater than or equal to 1")}else i.push(1);for(let l=0;l<n.length*2;l++)if(l<a.length){if(a[l]<0)throw new Error("pad should be greater than or equal to 1")}else a.push(0);for(let l=0;l<n.length;l++){if(n[l]<=0)throw new Error("kernel shapes need to be greater than 0");if(a[l]>=n[l]||a[l+n.length]>=n[l])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,n,o,i,a,l){if(l){if(i.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(o.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let d=0;d<t.length-2;d++)e.adjustPadAndReturnShape(t[d+(a?1:2)],r[d],n[d],o[d],i,d,d+t.length-2,l)}}static computePoolOutputShape(t,r,n,o,i,a,l){if(r.length<=0)throw new Error("input shape must be of size greater than 0");let d=[r[0],r[1]];return e.computeShapeHelper(t,r,d,n,o,i,a,l),d}static computeConvOutputShape(t,r,n,o,i,a,l){if(t.length<=0||r.length<=0)throw new Error("invalid input tensor dims or invalid filter tensor dims");let d=[t[0],r[0]];return e.computeShapeHelper(!1,t,d,n,o,i,a,l),d}static computeShapeHelper(t,r,n,o,i,a,l,d){if(t)for(let c=0;c<r.length-2;c++)n.push(1);else for(let c=0;c<r.length-2;c++)n.push(e.adjustPadAndReturnShape(r[c+2],o[c],i[c],a[c],l,c,c+r.length-2,d))}static adjustPadAndReturnShape(t,r,n,o,i,a,l,d){let c=n*(o-1)+1;if(d&&d!=="NOTSET")switch(d){case"VALID":return i[a]=0,i[l]=0,Math.floor((t-c)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(n!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let u=((t+r-1)/r-1)*r+o-t;return i[a]=Math.floor(d==="SAME_LOWER"?(u+1)/2:u/2),i[l]=u-i[a],Math.floor((t+u-o)/r+1)}default:throw new Error("Unsupported AutoPad type")}else return Math.floor((t+i[a]+i[l]-c)/r+1)}},Ur=class{static getShapeOfGemmResult(t,r,n,o,i){if(t.length!==2||n.length!==2)throw new Error("shape need to be of size 2");let a,l,d;r?(a=t[1],l=t[0]):(a=t[0],l=t[1]);let c=-1;if(o?(d=n[0],c=1):(d=n[1],c=0),n[c]!==l)throw new Error("dimension mismatch");if(a<=0||d<=0||l<=0)throw new Error("invalid shape specified");if(i&&!Je.isValidBroadcast(i,[a,d]))throw new Error("gemm: invalid bias shape for broadcast");return[a,d,l]}},Vr=-34028234663852886e22,Nr=34028234663852886e22});var St,Qn,pe,ke,U,he,ft,It,je,L,Jn,E,D,Wr,Zn,La,Mt,ne=M(()=>{"use strict";X();te();St=64,Qn=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(e){case 10:return t>1?`vec${t}<f16>`:"f16";case 1:return t>1?`vec${t}<f32>`:"f32";case 6:return t>1?`vec${t}<i32>`:"i32";case 12:return t>1?`vec${t}<u32>`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4<bool>"];default:throw new Error(`Unknown data type: ${e}`)}},pe=(e,t=1)=>{let r=Qn(e,t);return typeof r=="string"?r:r[0]},ke=(e,t=1)=>{let r=Qn(e,t);return typeof r=="string"?r:r[1]},U=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:k.computeStrides(r)})}),t},he=e=>e%4===0?4:e%2===0?2:1,ft=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,It=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}<f32>(${r})`,je=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,L=(e,t,r,n)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?n==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:n==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,Jn=(e,t,r,n,o)=>{let i=typeof r=="number",a=i?r:r.length,l=[...new Array(a).keys()],d=a<2?"u32":a<=4?`vec${a}<u32>`:`array<u32, ${a}>`,c=Qn(t,o),m=typeof c=="string"?c:c[1],u=typeof c=="string"?c:c[0],h={indices:d,value:m,storage:u,tensor:t},w=R=>typeof R=="string"?R:`${R}u`,g={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},y=i?"uniforms.":"",x=`${y}${e}_shape`,_=`${y}${e}_strides`,$="";for(let R=0;R<a-1;R++)$+=`
|
|
3812
3812
|
let dim${R} = current / ${L(_,R,a)};
|
|
3813
3813
|
let rest${R} = current % ${L(_,R,a)};
|
|
3814
3814
|
indices[${R}] = dim${R};
|
|
@@ -3830,14 +3830,14 @@ var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Objec
|
|
|
3830
3830
|
}`,q=a<2?"":(()=>{let R=l.map(ue=>`d${ue}: u32`).join(", "),G=l.map(ue=>`d${ue}`).join(", ");return`
|
|
3831
3831
|
fn get_${e}(${R}) -> ${m} {
|
|
3832
3832
|
return get_${e}ByIndices(${z(G)});
|
|
3833
|
-
}`})(),ce=(...R)=>{if(R.length!==a)throw new Error(`indices length must be ${a}`);let G=R.map(w).join(",");return a===0?se("0u"):a===1?se(G[0]):(g.get=!0,g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}(${G})`)},
|
|
3833
|
+
}`})(),ce=(...R)=>{if(R.length!==a)throw new Error(`indices length must be ${a}`);let G=R.map(w).join(",");return a===0?se("0u"):a===1?se(G[0]):(g.get=!0,g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}(${G})`)},oe=R=>a<2?se(R):(g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}ByIndices(${R})`),Z=a<2?"":`
|
|
3834
3834
|
fn set_${e}ByIndices(indices: ${h.indices}, value: ${m}) {
|
|
3835
3835
|
${ae(`i2o_${e}(indices)`,"value")}
|
|
3836
3836
|
}`,ee=a<2?"":(()=>{let R=l.map(ue=>`d${ue}: u32`).join(", "),G=l.map(ue=>`d${ue}`).join(", ");return`
|
|
3837
3837
|
fn set_${e}(${R}, value: ${m}) {
|
|
3838
3838
|
set_${e}ByIndices(${z(G)}, value);
|
|
3839
|
-
}`})();return{impl:()=>{let R=[],G=!1;return g.offsetToIndices&&(R.push(S),G=!0),g.indicesToOffset&&(R.push(A),G=!0),g.broadcastedIndicesToOffset&&(Object.values(j).forEach(ue=>R.push(ue)),G=!0),g.set&&(R.push(ee),G=!0),g.setByIndices&&(R.push(Z),G=!0),g.get&&(R.push(q),G=!0),g.getByIndices&&(R.push(ie),G=!0),!i&&G&&R.unshift(`const ${x} = ${h.indices}(${r.join(",")});`,`const ${_} = ${h.indices}(${
|
|
3840
|
-
`)},type:h,offsetToIndices:I,indicesToOffset:O,broadcastedIndicesToOffset:Y,indices:z,indicesGet:W,indicesSet:V,set:(...R)=>{if(R.length!==a+1)throw new Error(`indices length must be ${a}`);let G=R[a];if(typeof G!="string")throw new Error("value must be string");let ue=R.slice(0,a).map(w).join(",");return a===0?ae("0u",G):a===1?ae(ue[0],G):(g.set=!0,g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}(${ue}, ${G})`)},setByOffset:ae,setByIndices:(R,G)=>a<2?ae(R,G):(g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}ByIndices(${R}, ${G});`),get:ce,getByOffset:se,getByIndices:
|
|
3839
|
+
}`})();return{impl:()=>{let R=[],G=!1;return g.offsetToIndices&&(R.push(S),G=!0),g.indicesToOffset&&(R.push(A),G=!0),g.broadcastedIndicesToOffset&&(Object.values(j).forEach(ue=>R.push(ue)),G=!0),g.set&&(R.push(ee),G=!0),g.setByIndices&&(R.push(Z),G=!0),g.get&&(R.push(q),G=!0),g.getByIndices&&(R.push(ie),G=!0),!i&&G&&R.unshift(`const ${x} = ${h.indices}(${r.join(",")});`,`const ${_} = ${h.indices}(${k.computeStrides(r).join(",")});`),R.join(`
|
|
3840
|
+
`)},type:h,offsetToIndices:I,indicesToOffset:O,broadcastedIndicesToOffset:Y,indices:z,indicesGet:W,indicesSet:V,set:(...R)=>{if(R.length!==a+1)throw new Error(`indices length must be ${a}`);let G=R[a];if(typeof G!="string")throw new Error("value must be string");let ue=R.slice(0,a).map(w).join(",");return a===0?ae("0u",G):a===1?ae(ue[0],G):(g.set=!0,g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}(${ue}, ${G})`)},setByOffset:ae,setByIndices:(R,G)=>a<2?ae(R,G):(g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}ByIndices(${R}, ${G});`),get:ce,getByOffset:se,getByIndices:oe,usage:n,name:e,strides:_,shape:x,rank:a}},E=(e,t,r,n=1)=>Jn(e,t,r,"input",n),D=(e,t,r,n=1)=>Jn(e,t,r,"output",n),Wr=(e,t,r,n=1)=>Jn(e,t,r,"internal",n),Zn=class{constructor(t,r){this.normalizedDispatchGroup=t;this.limits=r;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=St){let r=typeof t=="number"?t:t[0],n=typeof t=="number"?1:t[1],o=typeof t=="number"?1:t[2];if(r>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(r*n*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,a=i?`@builtin(global_invocation_id) global_id : vec3<u32>,
|
|
3841
3841
|
@builtin(workgroup_id) workgroup_id : vec3<u32>,
|
|
3842
3842
|
@builtin(local_invocation_id) local_id : vec3<u32>`:`@builtin(global_invocation_id) global_id : vec3<u32>,
|
|
3843
3843
|
@builtin(local_invocation_id) local_id : vec3<u32>,
|
|
@@ -3852,11 +3852,11 @@ var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Objec
|
|
|
3852
3852
|
struct Uniforms { ${t.join(", ")} };
|
|
3853
3853
|
@group(0) @binding(${this.variableIndex}) var<uniform> uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(t=>t.impl()).join(`
|
|
3854
3854
|
`)+this.internalVariables.map(t=>t.impl()).join(`
|
|
3855
|
-
`)}get variablesInfo(){if(this.uniforms.length===0)return;let t=r=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(r)];return this.uniforms.map(r=>[t(r.type),r.length??1])}},La=(e,t)=>new Zn(e,t),Mt=(e,t)=>{let r=e.length,n=[];for(let o=0;o<r;o++){let i=r-1-o,a=e[i]||1;(t[t.length-1-o]||1)>1&&a===1&&n.unshift(i)}return n}});var Sp,Ha,Ip,Cp,Re,Ga,Fa,Ct=M(()=>{"use strict";X();
|
|
3855
|
+
`)}get variablesInfo(){if(this.uniforms.length===0)return;let t=r=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(r)];return this.uniforms.map(r=>[t(r.type),r.length??1])}},La=(e,t)=>new Zn(e,t),Mt=(e,t)=>{let r=e.length,n=[];for(let o=0;o<r;o++){let i=r-1-o,a=e[i]||1;(t[t.length-1-o]||1)>1&&a===1&&n.unshift(i)}return n}});var Sp,Ha,Ip,Cp,Re,Ga,Fa,Ct=M(()=>{"use strict";X();te();xe();ne();Sp=e=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.")},Ha=(e,t)=>t&&t.length!==e?[...new Array(e).keys()].reverse():t,Ip=(e,t)=>k.sortBasedOnPerm(e,Ha(e.length,t)),Cp=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} {
|
|
3856
3856
|
var a: ${r.type.indices};`);for(let i=0;i<t;++i)o.push(r.indicesSet("a",e[i],`i[${i}]`));return o.push("return a;}"),o.join(`
|
|
3857
|
-
`)},Re=(e,t)=>{let r=e.dataType,n=e.dims.length,o=Ha(n,t),i=Ip(e.dims,o),a=D("output",r,i.length),l=
|
|
3857
|
+
`)},Re=(e,t)=>{let r=e.dataType,n=e.dims.length,o=Ha(n,t),i=Ip(e.dims,o),a=D("output",r,i.length),l=E("a",r,n),d;if(o.length===2&&o[0]===1&&o[1]===0){let c=a.type.value,m=[16,16,1];d=u=>`
|
|
3858
3858
|
${u.registerUniform("output_size","u32").declareVariables(l,a)}
|
|
3859
|
-
var<workgroup> tile : array<array<${
|
|
3859
|
+
var<workgroup> tile : array<array<${c}, ${m[0]+1}>, ${m[0]}>;
|
|
3860
3860
|
${u.mainStart(m)}
|
|
3861
3861
|
var x = workgroup_id.x * ${m[0]}u + local_id.x;
|
|
3862
3862
|
var y = workgroup_id.y * ${m[0]}u + local_id.y;
|
|
@@ -3871,19 +3871,19 @@ var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Objec
|
|
|
3871
3871
|
if (x < height && y < width) {
|
|
3872
3872
|
${a.setByOffset("y * height + x","tile[local_id.x][local_id.y]")}
|
|
3873
3873
|
}
|
|
3874
|
-
}`}else d=
|
|
3875
|
-
${
|
|
3874
|
+
}`}else d=c=>`
|
|
3875
|
+
${c.registerUniform("output_size","u32").declareVariables(l,a)}
|
|
3876
3876
|
|
|
3877
3877
|
${Cp(o,n,l,a)}
|
|
3878
3878
|
|
|
3879
|
-
${
|
|
3880
|
-
${
|
|
3879
|
+
${c.mainStart()}
|
|
3880
|
+
${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
3881
3881
|
|
|
3882
3882
|
let indices = ${a.offsetToIndices("global_idx")};
|
|
3883
3883
|
let aIndices = perm(indices);
|
|
3884
3884
|
|
|
3885
3885
|
${a.setByOffset("global_idx",l.getByIndices("aIndices"))}
|
|
3886
|
-
}`;return{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:
|
|
3886
|
+
}`;return{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:c=>{let m=k.size(i);return{outputs:[{dims:i,dataType:c[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...U(c[0].dims,i)]}},getShaderSource:d}},Ga=(e,t)=>{Sp(e.inputs),e.compute(Re(e.inputs[0],t.perm))},Fa=e=>Q({perm:e.perm})});var Tp,Ap,kp,Ep,Pp,Op,zp,Bp,Dp,Rp,et,qa,Ka,ja,Ya,Xa,Za,Qa,Ja,es,ts,rs=M(()=>{"use strict";X();te();ne();Lr();Ct();Tp={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},Ap={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},kp={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Ep={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Pp=(e,t)=>{let r=[];for(let n=t-e;n<t;++n)r.push(n);return r},Op=(e,t)=>{let r=[],n=e.length;for(let i=0;i<n;i++)t.indexOf(i)===-1&&r.push(e[i]);let o=t.map(i=>e[i]);return[r,o]},zp=(e,t)=>{let r=e.length+t.length,n=[],o=0;for(let i=0;i<r;i++)t.indexOf(i)===-1?n.push(e[o++]):n.push(1);return n},Bp=(e,t)=>{for(let r=0;r<e.length;++r)if(e[e.length-r-1]!==t-1-r)return!1;return!0},Dp=(e,t)=>{let r=[];if(!Bp(e,t)){for(let n=0;n<t;++n)e.indexOf(n)===-1&&r.push(n);e.forEach(n=>r.push(n))}return r},Rp=(e,t,r,n,o,i,a)=>{let l=r[0].dims,d=k.size(i),c=k.size(a),m=E("_A",r[0].dataType,l),u=D("output",o,i),h=32,w=`
|
|
3887
3887
|
var<workgroup> aBestValues : array<f32, ${h}>;
|
|
3888
3888
|
`;return{name:e,shaderCache:t,getShaderSource:y=>`
|
|
3889
3889
|
${y.registerUniform("reduceSize","u32").declareVariables(m,u)}
|
|
@@ -3921,7 +3921,7 @@ var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Objec
|
|
|
3921
3921
|
if (local_idx == 0u) {
|
|
3922
3922
|
${u.setByOffset("outputIndex",`${n==="mean"?`${u.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${u.type.storage}(${Ep[n]})`}`)};
|
|
3923
3923
|
}
|
|
3924
|
-
}`,getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:d},programUniforms:[{type:12,data:
|
|
3924
|
+
}`,getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:d},programUniforms:[{type:12,data:c}]})}},et=(e,t,r,n)=>{let o=e.inputs.length===1?r:eo(e.inputs,r),i=o.axes;i.length===0&&!o.noopWithEmptyAxes&&(i=e.inputs[0].dims.map((w,g)=>g));let a=k.normalizeAxes(i,e.inputs[0].dims.length),l=a,d=e.inputs[0],c=Dp(l,e.inputs[0].dims.length);c.length>0&&(d=e.compute(Re(e.inputs[0],c),{inputs:[0],outputs:[-1]})[0],l=Pp(l.length,d.dims.length));let[m,u]=Op(d.dims,l),h=m;o.keepDims&&(h=zp(m,a)),e.compute(Rp(t,{hint:o.cacheKey,inputDependencies:["type"]},[d],n,e.inputs[0].dataType,h,u),{inputs:[d]})},qa=(e,t)=>{et(e,"ReduceMeanShared",t,"mean")},Ka=(e,t)=>{et(e,"ReduceL1Shared",t,"l1")},ja=(e,t)=>{et(e,"ReduceL2Shared",t,"l2")},Ya=(e,t)=>{et(e,"ReduceLogSumExpShared",t,"logSumExp")},Xa=(e,t)=>{et(e,"ReduceMaxShared",t,"max")},Za=(e,t)=>{et(e,"ReduceMinShared",t,"min")},Qa=(e,t)=>{et(e,"ReduceProdShared",t,"prod")},Ja=(e,t)=>{et(e,"ReduceSumShared",t,"sum")},es=(e,t)=>{et(e,"ReduceSumSquareShared",t,"sumSquare")},ts=(e,t)=>{et(e,"ReduceLogSumShared",t,"logSum")}});var tt,Mp,Hr,eo,rt,Up,Vp,Np,Wp,Lp,Hp,Gp,Fp,qp,Kp,nt,ns,os,is,as,ss,us,ds,ls,cs,ps,Lr=M(()=>{"use strict";X();te();xe();ne();rs();tt=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Mp=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Hr=(e,t,r,n,o,i,a=!1,l=!1)=>{let d=[],c=r[0].dims,m=c.length,u=k.normalizeAxes(o,m),h=!l&&u.length===0;c.forEach((x,_)=>{h||u.indexOf(_)>=0?a&&d.push(1):d.push(x)});let w=d.length,g=k.size(d);return{name:e,shaderCache:t,getShaderSource:x=>{let _=[],$=E("_A",r[0].dataType,m),S=D("output",i,w),I=n($,S,u),T=I[2];for(let A=0,O=0;A<m;A++)h||u.indexOf(A)>=0?(a&&O++,T=`for(var j${A}: u32 = 0; j${A} < ${c[A]}; j${A}++) {
|
|
3925
3925
|
${I[2].includes("last_index")?`let last_index = j${A};`:""}
|
|
3926
3926
|
${$.indicesSet("input_indices",A,`j${A}`)}
|
|
3927
3927
|
${T}
|
|
@@ -3942,7 +3942,7 @@ var On=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var ep=Objec
|
|
|
3942
3942
|
${I[3]}
|
|
3943
3943
|
${I.length===4?S.setByOffset("global_idx","value"):I.slice(4).join(`
|
|
3944
3944
|
`)}
|
|
3945
|
-
}`},getRunData:()=>({outputs:[{dims:d,dataType:i}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},...U(
|
|
3945
|
+
}`},getRunData:()=>({outputs:[{dims:d,dataType:i}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},...U(c,d)]})}},eo=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),Q({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},rt=(e,t,r,n)=>{let o=e.inputs,i=o.length===1?r:eo(o,r);e.compute(Hr(t,{hint:i.cacheKey,inputDependencies:["rank"]},[o[0]],i.noopWithEmptyAxes&&i.axes.length===0?Mp:n,i.axes,o[0].dataType,i.keepDims,i.noopWithEmptyAxes),{inputs:[0]})},Up=(e,t)=>{tt(e.inputs),rt(e,"ReduceLogSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},Vp=(e,t)=>{tt(e.inputs),rt(e,"ReduceL1",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},Np=(e,t)=>{tt(e.inputs),rt(e,"ReduceL2",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Wp=(e,t)=>{tt(e.inputs),rt(e,"ReduceLogSumExp",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},Lp=(e,t)=>{tt(e.inputs),rt(e,"ReduceMax",t,(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(n.indicesSet("input_indices",l,0));return[`${a.join(`
|
|
3946
3946
|
`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},Hp=(e,t)=>{tt(e.inputs),rt(e,"ReduceMean",t,(n,o,i)=>{let a=1;for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&(a*=e.inputs[0].dims[l]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${a});`]})},Gp=(e,t)=>{tt(e.inputs),rt(e,"ReduceMin",t,(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
|
|
3947
3947
|
`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},Fp=(e,t)=>{tt(e.inputs),rt(e,"ReduceProd",t,(n,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},qp=(e,t)=>{tt(e.inputs),rt(e,"ReduceSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Kp=(e,t)=>{tt(e.inputs),rt(e,"ReduceSumSquare",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},nt=(e,t,r)=>{if(t.length===0)return r;let n=1,o=1;for(let i=0;i<t.length;i++)t.indexOf(i)===-1?n*=e[i]:o*=e[i];return o<32&&n>1024},ns=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Hp(e,t):qa(e,t)},os=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Vp(e,t):Ka(e,t)},is=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Np(e,t):ja(e,t)},as=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Wp(e,t):Ya(e,t)},ss=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Lp(e,t):Xa(e,t)},us=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Gp(e,t):Za(e,t)},ds=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Fp(e,t):Qa(e,t)},ls=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?qp(e,t):Ja(e,t)},cs=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Kp(e,t):es(e,t)},ps=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Up(e,t):ts(e,t)}});var ms,fs,hs,to,gs=M(()=>{"use strict";X();xe();Lr();ms=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},fs=(e,t)=>{ms(e.inputs);let r=(n,o,i)=>{let a=[];for(let l=0;l<n.rank;l++)(i.indexOf(l)>=0||i.length===0)&&a.push(`input_indices[${l}] = 0;`);return[`${a.join(`
|
|
3948
3948
|
`)}`,`var value = ${n.getByIndices("input_indices")};
|
|
@@ -3954,73 +3954,73 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
3954
3954
|
var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) {
|
|
3955
3955
|
value = ${n.getByIndices("input_indices")};
|
|
3956
3956
|
best_index = i32(last_index);
|
|
3957
|
-
}`,"",o.setByOffset("global_idx","best_index")]};e.compute(Hr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},to=e=>Q(e)});var jp,Yp,Xp,Zp,Ut,Qp,ys,Gr=M(()=>{"use strict";X();Mr();
|
|
3958
|
-
var<workgroup> thread_max: array<f32, ${
|
|
3959
|
-
var<workgroup> thread_sum: array<f32, ${
|
|
3960
|
-
${
|
|
3961
|
-
${
|
|
3957
|
+
}`,"",o.setByOffset("global_idx","best_index")]};e.compute(Hr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},to=e=>Q(e)});var jp,Yp,Xp,Zp,Ut,Qp,ys,Gr=M(()=>{"use strict";X();te();Mr();ne();jp=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4],l=e[5];if(a&&l)throw new Error("Attention cannot have both past and attention_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let d=r.dims[0],c=r.dims[1],m=r.dims[2];if(o.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(n.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(n.dims[0]!==m)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(o.dims[0]!==n.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let u=o.dims[0]/3,h=u,w=h;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let S of t.qkvHiddenSizes)if(S%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");u=t.qkvHiddenSizes[0],h=t.qkvHiddenSizes[1],w=t.qkvHiddenSizes[2]}let g=c;if(u!==h)throw new Error("qkv_hidden_sizes first element should be same as the second");if(o.dims[0]!==u+h+w)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let y=0;if(a){if(h!==w)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(a.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(a.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(a.dims[1]!==d)throw new Error('Input "past" second dimension must be batch_size');if(a.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(a.dims[4]!==h/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(y=a.dims[3])}let x=g+y,_=-1,$=0;if(i)throw new Error("Mask not supported");if(a)throw new Error("past is not supported");if(l){if(l.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(l.dims[0]!==d||l.dims[1]!==t.numHeads||l.dims[2]!==c||l.dims[3]!==x)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:d,sequenceLength:c,pastSequenceLength:y,kvSequenceLength:g,totalSequenceLength:x,maxSequenceLength:_,inputHiddenSize:m,hiddenSize:u,vHiddenSize:w,headSize:Math.floor(u/t.numHeads),vHeadSize:Math.floor(w/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:$,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},Yp=(e,t,r)=>{let n=he(r),o=64,i=r/n;i<o&&(o=32);let a=Math.ceil(r/n/o),l=[{type:1,data:1/r},{type:12,data:i},{type:12,data:a}],d=pe(e.dataType,n),c=ke(1,n),m=["type"],u=h=>{let w=D("x",e.dataType,e.dims,n),g=ke(e.dataType),y=[{name:"d_inv",type:"f32"},{name:"d_comp",type:"u32"},{name:"elements_per_thread",type:"u32"}];return`
|
|
3958
|
+
var<workgroup> thread_max: array<f32, ${o}>;
|
|
3959
|
+
var<workgroup> thread_sum: array<f32, ${o}>;
|
|
3960
|
+
${h.registerUniforms(y).declareVariables(w)}
|
|
3961
|
+
${h.mainStart([o,1,1])}
|
|
3962
3962
|
let local_offset = local_idx * uniforms.elements_per_thread;
|
|
3963
|
-
let offset = (global_idx / ${
|
|
3963
|
+
let offset = (global_idx / ${o}) * uniforms.d_comp + local_offset;
|
|
3964
3964
|
|
|
3965
|
-
var thread_max_vector = ${
|
|
3965
|
+
var thread_max_vector = ${c}(-3.402823e+38f);
|
|
3966
3966
|
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) {
|
|
3967
|
-
thread_max_vector = max(${
|
|
3967
|
+
thread_max_vector = max(${c}(x[offset + i]), thread_max_vector);
|
|
3968
3968
|
}
|
|
3969
|
-
thread_max[local_idx] = ${(()=>{switch(
|
|
3969
|
+
thread_max[local_idx] = ${(()=>{switch(n){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: ${n}`)}})()};
|
|
3970
3970
|
workgroupBarrier();
|
|
3971
3971
|
|
|
3972
3972
|
var max_value = f32(-3.402823e+38f);
|
|
3973
|
-
for (var i = 0u; i < ${
|
|
3973
|
+
for (var i = 0u; i < ${o}; i++) {
|
|
3974
3974
|
max_value = max(thread_max[i], max_value);
|
|
3975
3975
|
}
|
|
3976
3976
|
|
|
3977
|
-
var sum_vector = ${
|
|
3977
|
+
var sum_vector = ${c}(0);
|
|
3978
3978
|
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) {
|
|
3979
|
-
sum_vector += exp(${
|
|
3979
|
+
sum_vector += exp(${c}(x[offset + i]) - max_value);
|
|
3980
3980
|
}
|
|
3981
|
-
thread_sum[local_idx] = ${(()=>{switch(
|
|
3981
|
+
thread_sum[local_idx] = ${(()=>{switch(n){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: ${n}`)}})()};
|
|
3982
3982
|
workgroupBarrier();
|
|
3983
3983
|
|
|
3984
3984
|
var sum: f32 = 0;
|
|
3985
|
-
for (var i = 0u; i < ${
|
|
3985
|
+
for (var i = 0u; i < ${o}; i++) {
|
|
3986
3986
|
sum += thread_sum[i];
|
|
3987
3987
|
}
|
|
3988
3988
|
|
|
3989
3989
|
if (sum == 0) {
|
|
3990
3990
|
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) {
|
|
3991
|
-
x[offset + i] = ${
|
|
3991
|
+
x[offset + i] = ${w.type.value}(${g}(uniforms.d_inv));
|
|
3992
3992
|
}
|
|
3993
3993
|
} else {
|
|
3994
3994
|
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) {
|
|
3995
|
-
var f32input = ${
|
|
3996
|
-
x[offset + i] = ${
|
|
3995
|
+
var f32input = ${c}(x[offset + i]);
|
|
3996
|
+
x[offset + i] = ${w.type.value}(exp(f32input - max_value) / sum);
|
|
3997
3997
|
}
|
|
3998
3998
|
}
|
|
3999
|
-
}`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${
|
|
3999
|
+
}`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${o};${d};${n}`,inputDependencies:m},getShaderSource:u,getRunData:()=>({outputs:[],dispatchGroup:{x:t},programUniforms:l})}},Xp=(e,t,r,n,o,i,a,l)=>{let d=l+i.kvSequenceLength,c=[i.batchSize,i.numHeads,i.sequenceLength,d],m=i.kvNumHeads===void 0&&e>1&&n,u=m?[i.batchSize,i.numHeads,d,i.headSize]:void 0,h=a.scale===0?1/Math.sqrt(i.headSize):a.scale,w=he(i.headSize),g=i.headSize/w,y=12,x={x:Math.ceil(d/y),y:Math.ceil(i.sequenceLength/y),z:i.batchSize*i.numHeads},_=[{type:12,data:i.sequenceLength},{type:12,data:g},{type:12,data:d},{type:12,data:i.numHeads},{type:1,data:h},{type:12,data:l},{type:12,data:i.kvSequenceLength}],$=m&&n&&k.size(n.dims)>0,S=["type","type"];$&&S.push("type"),o&&S.push("type");let I=[{dims:c,dataType:t.dataType,gpuDataType:0}];m&&I.push({dims:u,dataType:t.dataType,gpuDataType:0});let T=A=>{let O=E("q",t.dataType,t.dims,w),z=E("key",r.dataType,r.dims,w),W=[O,z];if($){let se=E("past_key",n.dataType,n.dims,w);W.push(se)}o&&W.push(E("attention_bias",o.dataType,o.dims));let V=D("output",t.dataType,c),j=[V];m&&j.push(D("present_key",t.dataType,u,w));let Y=ke(1,w),ae=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return`
|
|
4000
4000
|
const TILE_SIZE = ${y}u;
|
|
4001
4001
|
|
|
4002
|
-
var<workgroup> tileQ: array<${
|
|
4003
|
-
var<workgroup> tileK: array<${
|
|
4004
|
-
${
|
|
4005
|
-
${
|
|
4002
|
+
var<workgroup> tileQ: array<${O.type.storage}, ${y*y}>;
|
|
4003
|
+
var<workgroup> tileK: array<${O.type.storage}, ${y*y}>;
|
|
4004
|
+
${A.registerUniforms(ae).declareVariables(...W,...j)}
|
|
4005
|
+
${A.mainStart([y,y,1])}
|
|
4006
4006
|
// x holds the N and y holds the M
|
|
4007
4007
|
let headIdx = workgroup_id.z;
|
|
4008
4008
|
let m = workgroup_id.y * TILE_SIZE;
|
|
4009
4009
|
let n = workgroup_id.x * TILE_SIZE;
|
|
4010
4010
|
let qOffset = uniforms.M * uniforms.K * headIdx + m * uniforms.K;
|
|
4011
|
-
${(()
|
|
4011
|
+
${(()=>$&&m?`
|
|
4012
4012
|
let kOffset = uniforms.kv_sequence_length * uniforms.K * headIdx;
|
|
4013
4013
|
let pastKeyOffset = uniforms.past_sequence_length * uniforms.K * headIdx;`:`
|
|
4014
4014
|
let kOffset = uniforms.N * uniforms.K * headIdx + n * uniforms.K;`)()}
|
|
4015
4015
|
${m?"let presentKeyOffset = headIdx * uniforms.N * uniforms.K;":""}
|
|
4016
|
-
var value = ${
|
|
4016
|
+
var value = ${Y}(0);
|
|
4017
4017
|
for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
|
|
4018
4018
|
if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) {
|
|
4019
4019
|
tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x];
|
|
4020
4020
|
}
|
|
4021
4021
|
if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) {
|
|
4022
4022
|
var idx = TILE_SIZE * local_id.y + local_id.x;
|
|
4023
|
-
${(()
|
|
4023
|
+
${(()=>$&&m?`
|
|
4024
4024
|
if (n + local_id.y < uniforms.past_sequence_length) {
|
|
4025
4025
|
tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x];
|
|
4026
4026
|
} else {
|
|
@@ -4032,7 +4032,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4032
4032
|
workgroupBarrier();
|
|
4033
4033
|
|
|
4034
4034
|
for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) {
|
|
4035
|
-
value += ${
|
|
4035
|
+
value += ${Y}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]);
|
|
4036
4036
|
}
|
|
4037
4037
|
|
|
4038
4038
|
workgroupBarrier();
|
|
@@ -4042,34 +4042,34 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4042
4042
|
if (global_id.y < uniforms.M && global_id.x < uniforms.N) {
|
|
4043
4043
|
let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x;
|
|
4044
4044
|
var sum: f32 = ${(()=>{switch(w){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${w}`)}})()};
|
|
4045
|
-
output[outputIdx] = ${
|
|
4045
|
+
output[outputIdx] = ${V.type.value} (sum * uniforms.alpha) + ${o?"attention_bias[outputIdx]":"0.0"};
|
|
4046
4046
|
}
|
|
4047
|
-
}`};return{name:"AttentionProbs",shaderCache:{hint:`${w};${o!==void 0};${n!==void 0};${e
|
|
4047
|
+
}`};return{name:"AttentionProbs",shaderCache:{hint:`${w};${o!==void 0};${n!==void 0};${e}`,inputDependencies:S},getRunData:()=>({outputs:I,dispatchGroup:x,programUniforms:_}),getShaderSource:T}},Zp=(e,t,r,n,o,i)=>{let a=i+o.kvSequenceLength,l=o.nReps?o.nReps:1,d=o.vHiddenSize*l,c=o.kvNumHeads==null&&e>1&&n,m=c?[o.batchSize,o.numHeads,a,o.headSize]:void 0,u=[o.batchSize,o.sequenceLength,d],h=12,w={x:Math.ceil(o.vHeadSize/h),y:Math.ceil(o.sequenceLength/h),z:o.batchSize*o.numHeads},g=[{type:12,data:o.sequenceLength},{type:12,data:a},{type:12,data:o.vHeadSize},{type:12,data:o.numHeads},{type:12,data:d},{type:12,data:i},{type:12,data:o.kvSequenceLength}],y=c&&n&&k.size(n.dims)>0,x=["type","type"];y&&x.push("type");let _=[{dims:u,dataType:t.dataType,gpuDataType:0}];c&&_.push({dims:m,dataType:t.dataType,gpuDataType:0});let $=S=>{let I=E("probs",t.dataType,t.dims),T=E("v",r.dataType,r.dims),A=[I,T];y&&A.push(E("past_value",n.dataType,n.dims));let z=[D("output",t.dataType,u)];c&&z.push(D("present_value",t.dataType,m));let W=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return`
|
|
4048
4048
|
const TILE_SIZE = ${h}u;
|
|
4049
|
-
var<workgroup> tileQ: array<${
|
|
4050
|
-
var<workgroup> tileK: array<${
|
|
4051
|
-
${
|
|
4052
|
-
${
|
|
4049
|
+
var<workgroup> tileQ: array<${I.type.value}, ${h*h}>;
|
|
4050
|
+
var<workgroup> tileK: array<${I.type.value}, ${h*h}>;
|
|
4051
|
+
${S.registerUniforms(W).declareVariables(...A,...z)}
|
|
4052
|
+
${S.mainStart([h,h,1])}
|
|
4053
4053
|
let headIdx = workgroup_id.z;
|
|
4054
4054
|
let m = global_id.y;
|
|
4055
4055
|
let n = global_id.x;
|
|
4056
4056
|
|
|
4057
4057
|
let offsetA = headIdx * (uniforms.M * uniforms.K) + m * uniforms.K;
|
|
4058
|
-
${(()=>
|
|
4058
|
+
${(()=>y&&c?`
|
|
4059
4059
|
let pastValueOffset = headIdx * uniforms.N * uniforms.past_sequence_length + n;
|
|
4060
4060
|
let vOffset = headIdx * uniforms.N * uniforms.kv_sequence_length + n;
|
|
4061
4061
|
`:`
|
|
4062
4062
|
let offsetB = headIdx * uniforms.N * uniforms.K + n;
|
|
4063
4063
|
`)()}
|
|
4064
|
-
${
|
|
4065
|
-
var value = ${
|
|
4064
|
+
${c?"let presentValueOffset = headIdx * uniforms.N * uniforms.K + n;":""}
|
|
4065
|
+
var value = ${I.type.storage}(0);
|
|
4066
4066
|
for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
|
|
4067
4067
|
if (m < uniforms.M && w + local_id.x < uniforms.K) {
|
|
4068
4068
|
tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x];
|
|
4069
4069
|
}
|
|
4070
4070
|
if (n < uniforms.N && w + local_id.y < uniforms.K) {
|
|
4071
4071
|
var idx = TILE_SIZE * local_id.y + local_id.x;
|
|
4072
|
-
${(()=>
|
|
4072
|
+
${(()=>y&&c?`
|
|
4073
4073
|
if (w + local_id.y < uniforms.past_sequence_length) {
|
|
4074
4074
|
tileK[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N];
|
|
4075
4075
|
} else {
|
|
@@ -4078,7 +4078,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4078
4078
|
`:`
|
|
4079
4079
|
tileK[idx] = v[offsetB + (w + local_id.y) * uniforms.N];
|
|
4080
4080
|
`)()}
|
|
4081
|
-
${
|
|
4081
|
+
${c?"present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileK[idx];":""}
|
|
4082
4082
|
}
|
|
4083
4083
|
workgroupBarrier();
|
|
4084
4084
|
for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) {
|
|
@@ -4095,7 +4095,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4095
4095
|
+ currentBatchHeadNumber * uniforms.N + n;
|
|
4096
4096
|
output[outputIdx] = value;
|
|
4097
4097
|
}
|
|
4098
|
-
}`};return{name:"AttentionScore",shaderCache:{hint:`${n!==void 0};${e
|
|
4098
|
+
}`};return{name:"AttentionScore",shaderCache:{hint:`${n!==void 0};${e}`,inputDependencies:x},getRunData:()=>({outputs:_,dispatchGroup:w,programUniforms:g}),getShaderSource:$}},Ut=(e,t,r,n,o,i,a,l,d,c,m)=>{let u=Math.min(e.outputCount,1+(a?1:0)+(l?1:0)),h=c.kvNumHeads!==void 0||u>1?c.pastSequenceLength:0,w=h+c.kvSequenceLength,g=d&&k.size(d.dims)>0?d:void 0,y=[t,r];c.kvNumHeads===void 0&&u>1&&a&&k.size(a.dims)>0&&y.push(a),g&&y.push(g);let x=e.compute(Xp(u,t,r,a,g,c,m,h),{inputs:y,outputs:c.kvNumHeads===void 0&&u>1?[-1,1]:[-1]})[0];e.compute(Yp(x,c.batchSize*c.numHeads*c.sequenceLength,w),{inputs:[x],outputs:[]});let _=[x,n];c.kvNumHeads===void 0&&u>1&&l&&k.size(l.dims)>0&&_.push(l),e.compute(Zp(u,x,n,l,c,h),{inputs:_,outputs:c.kvNumHeads===void 0&&u>1?[0,2]:[0]})},Qp=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],n=t.sequenceLength,o=t.inputHiddenSize,i=t.headSize,a=12,l={x:Math.ceil(t.headSize/a),y:Math.ceil(t.sequenceLength/a),z:t.batchSize*t.numHeads},d=[e.inputs[0],e.inputs[1],e.inputs[2]],c=[{type:12,data:n},{type:12,data:o},{type:12,data:i},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],m=u=>{let h=D("output_q",d[0].dataType,r),w=D("output_k",d[0].dataType,r),g=D("output_v",d[0].dataType,r),y=E("input",d[0].dataType,d[0].dims),x=E("weight",d[1].dataType,d[1].dims),_=E("bias",d[2].dataType,d[2].dims),$=y.type.storage,S=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return`
|
|
4099
4099
|
const TILE_SIZE = ${a}u;
|
|
4100
4100
|
var<workgroup> tileInput: array<${$}, ${a*a}>;
|
|
4101
4101
|
var<workgroup> tileWeightQ: array<${$}, ${a*a}>;
|
|
@@ -4150,7 +4150,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4150
4150
|
output_k[outputIdx] = valueK;
|
|
4151
4151
|
output_v[outputIdx] = valueV;
|
|
4152
4152
|
}
|
|
4153
|
-
}`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:l,programUniforms:
|
|
4153
|
+
}`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:l,programUniforms:c}),getShaderSource:m},{inputs:d,outputs:[-1,-1,-1]})},ys=(e,t)=>{let r=jp(e.inputs,t),[n,o,i]=Qp(e,r);return Ut(e,n,o,i,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r,t)}});var Jp,em,tm,bs,ws=M(()=>{"use strict";qe();X();te();xe();ne();Jp=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(n,o,i)=>{let a=o.length;if(a!==n.length)throw new Error(`${i}: num dimensions != ${a}`);o.forEach((l,d)=>{if(l!==n[d])throw new Error(`${i}: dim[${d}] do not match`)})};if(e[0].dims.length>1){let n=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,n,"Invalid input scale"),r(e[2].dims,n,"Invalid input B"),r(e[3].dims,n,"Invalid input mean"),r(e[4].dims,n,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},em=(e,t)=>{let{epsilon:r,spatial:n,format:o}=t,i=e[0].dims,a=n?he(i[i.length-1]):1,l=o==="NHWC"&&i.length>1?a:1,d=k.size(i)/a,c=n,m=c?i.length:i,u=E("x",e[0].dataType,e[0].dims,a),h=E("scale",e[1].dataType,e[1].dims,l),w=E("bias",e[2].dataType,e[2].dims,l),g=E("inputMean",e[3].dataType,e[3].dims,l),y=E("inputVar",e[4].dataType,e[4].dims,l),x=D("y",e[0].dataType,m,a),_=()=>{let S="";if(n)S=`let cOffset = ${i.length===1?"0u":o==="NHWC"?`outputIndices[${i.length-1}] / ${a}`:"outputIndices[1]"};`;else if(o==="NCHW")S=`
|
|
4154
4154
|
${x.indicesSet("outputIndices","0","0")}
|
|
4155
4155
|
let cOffset = ${x.indicesToOffset("outputIndices")};`;else{S=`var cIndices = ${h.type.indices}(0);
|
|
4156
4156
|
cIndices[0] = outputIndices[${i.length-1}];`;for(let I=1;I<h.rank;I++)S+=`cIndices[${I}] = outputIndices[${I}];`;S+=`let cOffset = ${h.indicesToOffset("cIndices")};`}return S},$=S=>`
|
|
@@ -4167,7 +4167,7 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4167
4167
|
let x = ${u.getByOffset("global_idx")};
|
|
4168
4168
|
let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias;
|
|
4169
4169
|
${x.setByOffset("global_idx","value")}
|
|
4170
|
-
}`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${n}_${a}`,inputDependencies:
|
|
4170
|
+
}`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${n}_${a}`,inputDependencies:c?["rank","type","type","type","type"]:void 0},getShaderSource:$,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c?[{type:12,data:d},...U(i)]:[{type:12,data:d}]})}},tm=e=>Q(e),bs=(e,t)=>{let{inputs:r,outputCount:n}=e,o=tm({...t,outputCount:n});if(fe.webgpu.validateInputContent&&Jp(r,o),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(em(r,o))}});var rm,nm,vs,$s=M(()=>{"use strict";te();ne();rm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},nm=e=>{let t=e[0].dims,r=e[0].dims[2],n=k.size(t)/4,o=e[0].dataType,i=E("input",o,t,4),a=E("bias",o,[r],4),l=E("residual",o,t,4),d=D("output",o,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)}}),getShaderSource:m=>`
|
|
4171
4171
|
const channels = ${r}u / 4;
|
|
4172
4172
|
${m.declareVariables(i,a,l,d)}
|
|
4173
4173
|
|
|
@@ -4176,8 +4176,8 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4176
4176
|
let value = ${i.getByOffset("global_idx")}
|
|
4177
4177
|
+ ${a.getByOffset("global_idx % channels")} + ${l.getByOffset("global_idx")};
|
|
4178
4178
|
${d.setByOffset("global_idx","value")}
|
|
4179
|
-
}`}},vs=e=>{rm(e.inputs),e.compute(nm(e.inputs))}});var om,le,_s,xs,Ss,Is,Cs,Ts,As,ks,Es,im,Ps,Os,zs,Bs,Yt,Ds,Fr,Rs,Ms,Us,Vs,Ns,Ws,Ls,Hs,Gs,Fs,qs,Ks,js,Ys,Xs,Zs,Qs,Js,ro,no,eu,tu,ru,am,sm,nu,qr=M(()=>{"use strict";X();
|
|
4180
|
-
${e.registerUniform("vec_size","u32").declareVariables(d,
|
|
4179
|
+
}`}},vs=e=>{rm(e.inputs),e.compute(nm(e.inputs))}});var om,le,_s,xs,Ss,Is,Cs,Ts,As,ks,Es,im,Ps,Os,zs,Bs,Yt,Ds,Fr,Rs,Ms,Us,Vs,Ns,Ws,Ls,Hs,Gs,Fs,qs,Ks,js,Ys,Xs,Zs,Qs,Js,ro,no,eu,tu,ru,am,sm,nu,qr=M(()=>{"use strict";X();te();xe();ne();om=(e,t,r,n,o,i)=>{let a=Math.ceil(t/4),l="";typeof o=="string"?l=`${o}(a)`:l=o("a");let d=E("inputData",r,[a],4),c=D("outputData",n,[a],4);return`
|
|
4180
|
+
${e.registerUniform("vec_size","u32").declareVariables(d,c)}
|
|
4181
4181
|
|
|
4182
4182
|
${i??""}
|
|
4183
4183
|
|
|
@@ -4185,8 +4185,8 @@ var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLas
|
|
|
4185
4185
|
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
4186
4186
|
|
|
4187
4187
|
let a = ${d.getByOffset("global_idx")};
|
|
4188
|
-
${
|
|
4189
|
-
}`},le=(e,t,r,n,o,i=e.dataType)=>({name:t,shaderCache:{hint:o,inputDependencies:["type"]},getShaderSource:a=>om(a,
|
|
4188
|
+
${c.setByOffset("global_idx",l)}
|
|
4189
|
+
}`},le=(e,t,r,n,o,i=e.dataType)=>({name:t,shaderCache:{hint:o,inputDependencies:["type"]},getShaderSource:a=>om(a,k.size(e.dims),e.dataType,i,r,n),getRunData:a=>({outputs:[{dims:e.dims,dataType:i}],dispatchGroup:{x:Math.ceil(k.size(a[0].dims)/64/4)},programUniforms:[{type:12,data:Math.ceil(k.size(e.dims)/4)}]})}),_s=e=>{e.compute(le(e.inputs[0],"Abs","abs"))},xs=e=>{e.compute(le(e.inputs[0],"Acos","acos"))},Ss=e=>{e.compute(le(e.inputs[0],"Acosh","acosh"))},Is=e=>{e.compute(le(e.inputs[0],"Asin","asin"))},Cs=e=>{e.compute(le(e.inputs[0],"Asinh","asinh"))},Ts=e=>{e.compute(le(e.inputs[0],"Atan","atan"))},As=e=>{e.compute(le(e.inputs[0],"Atanh","atanh"))},ks=e=>Q(e),Es=(e,t)=>{let r;switch(t.to){case 10:r="vec4<f16>";break;case 1:r="vec4<f32>";break;case 12:r="vec4<u32>";break;case 6:r="vec4<i32>";break;case 9:r="vec4<bool>";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(le(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},im=e=>{let t=e.length>=2&&e[1].data!==0?e[1].getFloat32Array()[0]:Vr,r=e.length>=3&&e[2].data!==0?e[2].getFloat32Array()[0]:Nr;return Q({min:t,max:r})},Ps=(e,t)=>{let r=e.inputs.length===1?t:im(e.inputs),n=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"Clip",o=>`clamp(${o}, clip_min_, clip_max_)`,`
|
|
4190
4190
|
const clip_min_: vec4<${n}> = vec4(${n}(${r.min}));
|
|
4191
4191
|
const clip_max_: vec4<${n}> = vec4(${n}(${r.max}));
|
|
4192
4192
|
`,r.cacheKey),{inputs:[0]})},Os=e=>{e.compute(le(e.inputs[0],"Ceil","ceil"))},zs=e=>{e.compute(le(e.inputs[0],"Cos","cos"))},Bs=e=>{e.compute(le(e.inputs[0],"Cosh","cosh"))},Yt=e=>Q(e),Ds=(e,t)=>{let r=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"Elu",n=>`elu_vf32(${n})`,`
|
|
@@ -4235,7 +4235,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
|
|
|
4235
4235
|
}
|
|
4236
4236
|
return x * x1;
|
|
4237
4237
|
}
|
|
4238
|
-
`,sm=e=>`quick_gelu_impl(${e})`,nu=(e,t)=>{let r=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"QuickGelu",sm,am(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}});var um,dm,iu,au=M(()=>{"use strict";
|
|
4238
|
+
`,sm=e=>`quick_gelu_impl(${e})`,nu=(e,t)=>{let r=ke(e.inputs[0].dataType);e.compute(le(e.inputs[0],"QuickGelu",sm,am(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}});var um,dm,iu,au=M(()=>{"use strict";te();ne();qr();um=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},dm=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=E("input",e[0].dataType,e[0].dims,4),n=E("bias",e[0].dataType,[e[0].dims[2]],4),o=D("output",e[0].dataType,t,4),i=k.size(t)/4,a=pe(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:d=>`
|
|
4239
4239
|
const M_SQRT2 = sqrt(2.0);
|
|
4240
4240
|
const halfChannels = ${e[0].dims[2]/4/2}u;
|
|
4241
4241
|
|
|
@@ -4253,7 +4253,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
|
|
|
4253
4253
|
let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1);
|
|
4254
4254
|
|
|
4255
4255
|
${o.setByOffset("global_idx","valueLeft * geluRight")}
|
|
4256
|
-
}`}},iu=e=>{um(e.inputs),e.compute(dm(e.inputs))}});var lm,cm,ot,su,uu,du,lu,cu,pu,mu,fu,hu,gu,yu=M(()=>{"use strict";X();
|
|
4256
|
+
}`}},iu=e=>{um(e.inputs),e.compute(dm(e.inputs))}});var lm,cm,ot,su,uu,du,lu,cu,pu,mu,fu,hu,gu,yu=M(()=>{"use strict";X();te();ne();lm=(e,t,r,n,o,i,a,l,d,c,m,u)=>{let h,w;typeof l=="string"?h=w=($,S)=>`${l}((${$}),(${S}))`:typeof l=="function"?h=w=l:(h=l.scalar,w=l.vector);let g=D("outputData",m,n.length,4),y=E("aData",d,t.length,4),x=E("bData",c,r.length,4),_;if(o)if(i){let $=k.size(t)===1,S=k.size(r)===1,I=t.length>0&&t[t.length-1]%4===0,T=r.length>0&&r[r.length-1]%4===0;$||S?_=g.setByOffset("global_idx",w($?`${y.type.value}(${y.getByOffset("0")}.x)`:y.getByOffset("global_idx"),S?`${x.type.value}(${x.getByOffset("0")}.x)`:x.getByOffset("global_idx"))):_=`
|
|
4257
4257
|
let outputIndices = ${g.offsetToIndices("global_idx * 4u")};
|
|
4258
4258
|
let offsetA = ${y.broadcastedIndicesToOffset("outputIndices",g)};
|
|
4259
4259
|
let offsetB = ${x.broadcastedIndicesToOffset("outputIndices",g)};
|
|
@@ -4286,7 +4286,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
|
|
|
4286
4286
|
${e.mainStart()}
|
|
4287
4287
|
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
4288
4288
|
${_}
|
|
4289
|
-
}`},cm=(e,t,r,n,o,i,a=r.dataType)=>{let l=!
|
|
4289
|
+
}`},cm=(e,t,r,n,o,i,a=r.dataType)=>{let l=!k.areEqual(r.dims,n.dims),d=r.dims,c=k.size(r.dims),m=!1,u=!1,h=[l];if(l){let w=Je.calcShape(r.dims,n.dims,!1);if(!w)throw new Error("Can't perform binary op on the given tensors");d=w,c=k.size(d);let g=k.size(r.dims)===1,y=k.size(n.dims)===1,x=r.dims.length>0&&r.dims[r.dims.length-1]%4===0,_=n.dims.length>0&&n.dims[n.dims.length-1]%4===0;h.push(g),h.push(y),h.push(x),h.push(_);let $=1;for(let S=1;S<d.length;S++){let I=r.dims[r.dims.length-S]??1,T=n.dims[n.dims.length-S]??1;if(I===T)$*=I;else break}$%4===0?(u=!0,m=!0):(g||y||x||_)&&(m=!0)}else m=!0;return h.push(m),{name:e,shaderCache:{hint:t+h.map(w=>w.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:w=>lm(w,r.dims,n.dims,d,m,l,u,o,r.dataType,n.dataType,a,i),getRunData:()=>({outputs:[{dims:d,dataType:a}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:Math.ceil(k.size(d)/4)},...U(r.dims,n.dims,d)]})}},ot=(e,t,r,n,o,i)=>{e.compute(cm(t,o??"",e.inputs[0],e.inputs[1],r,n,i))},su=e=>{ot(e,"Add",(t,r)=>`${t}+${r}`)},uu=e=>{ot(e,"Div",(t,r)=>`${t}/${r}`)},du=e=>{ot(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4<u32>(${t}==${r})`},void 0,void 0,9)},lu=e=>{ot(e,"Mul",(t,r)=>`${t}*${r}`)},cu=e=>{let t=E("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;ot(e,"Pow",{scalar:(n,o)=>`pow_custom(${n},${o})`,vector:(n,o)=>`pow_vector_custom(${n},${o})`},`
|
|
4290
4290
|
fn pow_custom(a : ${t}, b : ${t}) -> ${t} {
|
|
4291
4291
|
if (b == ${t}(0.0)) {
|
|
4292
4292
|
return ${t}(1.0);
|
|
@@ -4299,7 +4299,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
|
|
|
4299
4299
|
// TODO: implement vectorized pow
|
|
4300
4300
|
return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w));
|
|
4301
4301
|
}
|
|
4302
|
-
`)},pu=e=>{ot(e,"Sub",(t,r)=>`${t}-${r}`)},mu=e=>{ot(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},fu=e=>{ot(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},hu=e=>{ot(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},gu=e=>{ot(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}});var mm,fm,hm,gm,bu,wu,vu=M(()=>{"use strict";X();
|
|
4302
|
+
`)},pu=e=>{ot(e,"Sub",(t,r)=>`${t}-${r}`)},mu=e=>{ot(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},fu=e=>{ot(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},hu=e=>{ot(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},gu=e=>{ot(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}});var mm,fm,hm,gm,bu,wu,vu=M(()=>{"use strict";X();te();xe();ne();mm=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,n=e[r],o=n.dataType,i=n.dims.length;e.forEach((a,l)=>{if(l!==r){if(a.dataType!==o)throw new Error("input tensors should be one type");if(a.dims.length!==i)throw new Error("input tensors should have the same shape");a.dims.forEach((d,c)=>{if(c!==t&&d!==n.dims[c])throw new Error("non concat dimensions must match")})}})},fm=(e,t)=>`
|
|
4303
4303
|
fn calculateInputIndex(index: u32) -> u32 {
|
|
4304
4304
|
let sizeInConcatAxis = array<u32, ${e}u>(${t});
|
|
4305
4305
|
for (var i: u32 = 0u; i < ${e}; i += 1u ) {
|
|
@@ -4309,7 +4309,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
|
|
|
4309
4309
|
}
|
|
4310
4310
|
return ${e}u;
|
|
4311
4311
|
}`,hm=(e,t)=>{let r=e.length,n=[];for(let o=0;o<r;++o){let i=t.setByOffset("global_idx",e[o].getByIndices("indices"));r===1?n.push(i):o===0?n.push(`if (inputIndex == ${o}u) { ${i} }`):o===r-1?n.push(`else { ${i} }`):n.push(`else if (inputIndex == ${o}) { ${i} }`)}return n.join(`
|
|
4312
|
-
`)},gm=(e,t,r,n)=>{let o=
|
|
4312
|
+
`)},gm=(e,t,r,n)=>{let o=k.size(r),i=new Array(e.length),a=new Array(e.length),l=0,d=[],c=[],m=[{type:12,data:o}];for(let y=0;y<e.length;++y)l+=e[y].dims[t],i[y]=l,c.push(e[y].dims.length),a[y]=E(`input${y}`,n,c[y]),d.push("rank"),m.push({type:12,data:i[y]});for(let y=0;y<e.length;++y)m.push(...U(e[y].dims));m.push(...U(r));let u=D("output",n,r.length),h=u.indicesGet("indices",t),w=Array.from(Array(i.length).keys()).map(y=>`uniforms.sizeInConcatAxis${y}`).join(","),g=y=>`
|
|
4313
4313
|
|
|
4314
4314
|
${(()=>{y.registerUniform("outputSize","u32");for(let x=0;x<e.length;x++)y.registerUniform(`sizeInConcatAxis${x}`,"u32");return y.declareVariables(...a,u)})()}
|
|
4315
4315
|
|
|
@@ -4327,7 +4327,7 @@ fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
|
|
|
4327
4327
|
}
|
|
4328
4328
|
|
|
4329
4329
|
${hm(a,u)}
|
|
4330
|
-
}`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:r,dataType:n}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:m}),getShaderSource:g}},bu=(e,t)=>{let r=e.inputs,n=r[0].dims,o=
|
|
4330
|
+
}`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:r,dataType:n}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:m}),getShaderSource:g}},bu=(e,t)=>{let r=e.inputs,n=r[0].dims,o=k.normalizeAxis(t.axis,n.length);mm(r,o);let i=n.slice();i[o]=r.reduce((l,d)=>l+(d.dims.length>o?d.dims[o]:0),0);let a=r.filter(l=>k.size(l.dims)>0);e.compute(gm(a,o,i,r[0].dataType),{inputs:a})},wu=e=>Q({axis:e.axis})});var Ne,We,Le,Kr,ut=M(()=>{"use strict";X();te();Ne=(e,t,r="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value));
|
|
4331
4331
|
value = sign(value) * (1.0 - e2x) / (1.0 + e2x);
|
|
4332
4332
|
`;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},We=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},Le=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},Kr=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,n]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:n}}else if(t==="Clip"){let[r,n]=e?.activation_params||[Vr,Nr];return{activation:t,clipMax:n,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}});var Ce,jr,Xt=M(()=>{"use strict";Ce=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},jr=e=>`
|
|
4333
4333
|
${e?"value = value + getBiasByOutputCoords(coords);":""}
|
|
@@ -4340,7 +4340,7 @@ fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
|
|
|
4340
4340
|
return dot(coords, vec4<i32>(
|
|
4341
4341
|
i32(${e}.x), i32(${e}.y), i32(${e}.z), 1));
|
|
4342
4342
|
}
|
|
4343
|
-
`});var ym,bm,Zt,$u,wm,Qt,vm,Xr,Jt=M(()=>{"use strict";X();
|
|
4343
|
+
`});var ym,bm,Zt,$u,wm,Qt,vm,Xr,Jt=M(()=>{"use strict";X();te();ne();ut();Xt();ym=(e,t)=>e?`
|
|
4344
4344
|
mm_Asub[inputRow][inputCol] = mm_readA(batch,
|
|
4345
4345
|
kStart + inputRow,
|
|
4346
4346
|
globalRowStart / innerElementSize + inputCol${t?", batchIndices":""});
|
|
@@ -4365,11 +4365,11 @@ fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
|
|
|
4365
4365
|
acc[i] = BCached1 * ACached.y + acc[i];
|
|
4366
4366
|
acc[i] = BCached2 * ACached.z + acc[i];
|
|
4367
4367
|
${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"}
|
|
4368
|
-
}`,Zt=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32)=>{let d=t[1]*e[1],
|
|
4368
|
+
}`,Zt=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32)=>{let d=t[1]*e[1],c=t[0]*e[0],m=o?d:i,u=o?i:d,h=m/t[0],w=i/t[1];if(!((o&&h===4&&e[1]===4||!o&&(h===3||h===4))&&m%t[0]===0&&i%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${o} is true, innerElementSize ${h} and workPerThread[1] ${e[1]} must be 4.
|
|
4369
4369
|
Otherwise, innerElementSize ${h} must be 3 or 4.
|
|
4370
4370
|
tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}. tileInner ${i} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return`
|
|
4371
4371
|
var<workgroup> mm_Asub: array<array<vec${h}<${r}>, ${m/h}>, ${u}>;
|
|
4372
|
-
var<workgroup> mm_Bsub: array<array<vec4<${r}>, ${
|
|
4372
|
+
var<workgroup> mm_Bsub: array<array<vec4<${r}>, ${c/e[0]}>, ${i}>;
|
|
4373
4373
|
|
|
4374
4374
|
const rowPerThread = ${e[1]};
|
|
4375
4375
|
const colPerThread = ${e[0]};
|
|
@@ -4438,10 +4438,10 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
|
|
|
4438
4438
|
mm_Asub[inputRow][inputCol] = mm_readA(batch,
|
|
4439
4439
|
globalRowStart + inputRow,
|
|
4440
4440
|
kStart + inputCol${t?", batchIndices":""});
|
|
4441
|
-
`,wm=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",Qt=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32,d=!1)=>{let
|
|
4441
|
+
`,wm=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",Qt=(e,t,r="f32",n,o=!1,i=32,a=!1,l=32,d=!1)=>{let c=e[1]*t[1],m=e[0]*t[0],u=o?c:i,h=o?i:c;if(!(h%t[1]===0&&u%t[0]===0&&i%t[1]===0))throw new Error(`tileAHight ${h} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${u} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`);let w=h/t[1],g=u/t[0],y=i/t[1],x=d?`
|
|
4442
4442
|
let localRow = i32(localId.y);
|
|
4443
4443
|
let localCol = i32(localId.x);
|
|
4444
|
-
let globalRowStart = i32(workgroupId.y) * ${
|
|
4444
|
+
let globalRowStart = i32(workgroupId.y) * ${c};
|
|
4445
4445
|
let globalColStart = i32(workgroupId.x) * ${m};
|
|
4446
4446
|
|
|
4447
4447
|
// Loop over shared dimension.
|
|
@@ -4492,7 +4492,7 @@ let tileCol = i32(localId.x) * colPerThread;
|
|
|
4492
4492
|
|
|
4493
4493
|
let globalRow = i32(globalId.y) * rowPerThread;
|
|
4494
4494
|
let globalCol = i32(globalId.x) * colPerThread;
|
|
4495
|
-
let globalRowStart = i32(workgroupId.y) * ${
|
|
4495
|
+
let globalRowStart = i32(workgroupId.y) * ${c};
|
|
4496
4496
|
|
|
4497
4497
|
let tileRowA = i32(localId.y) * ${w};
|
|
4498
4498
|
let tileColA = i32(localId.x) * ${g};
|
|
@@ -4564,16 +4564,16 @@ fn main(@builtin(local_invocation_id) localId : vec3<u32>,
|
|
|
4564
4564
|
var acc : array<array<${r}, colPerThread>, rowPerThread>;
|
|
4565
4565
|
${x}
|
|
4566
4566
|
}
|
|
4567
|
-
`},vm=(e,t,r,n,o,i=!1)=>{let[a,l,d]=o,[
|
|
4567
|
+
`},vm=(e,t,r,n,o,i=!1)=>{let[a,l,d]=o,[c,m,u,h]=n,w=Mt(a,d),g=Mt(l,d),y=pe(n[0].type.tensor),x=()=>{let S=m.rank,I=c.rank,T=`var aIndices: ${m.type.indices};`;for(let A=S-2-1,O=I-1;A>=0;A--,O--)T+=`
|
|
4568
4568
|
aIndices[${A}] = ${I>1?`batchIndices[${O}]`:"batchIndices"};`;return w.forEach(A=>{T+=`
|
|
4569
4569
|
aIndices[${A}] = 0;`}),T+=`
|
|
4570
4570
|
aIndices[${S-2}] = u32(row);
|
|
4571
|
-
aIndices[${S-1}] = u32(colIn);`,T},_=()=>{let S=u.rank,I=
|
|
4571
|
+
aIndices[${S-1}] = u32(colIn);`,T},_=()=>{let S=u.rank,I=c.rank,T=`var bIndices: ${u.type.indices};`;for(let A=S-2-1,O=I-1;A>=0;A--,O--)T+=`
|
|
4572
4572
|
bIndices[${A}] = ${I>1?`batchIndices[${O}]`:"batchIndices"};`;return g.forEach(A=>{T+=`
|
|
4573
4573
|
bIndices[${A}] = 0;`}),T+=`
|
|
4574
4574
|
bIndices[${S-2}] = u32(row);
|
|
4575
4575
|
bIndices[${S-1}] = u32(colIn);`,T};return`
|
|
4576
|
-
fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${
|
|
4576
|
+
fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Ce(e,y)} {
|
|
4577
4577
|
var value = ${Ce(e,y)}(0.0);
|
|
4578
4578
|
let col = colIn * ${e};
|
|
4579
4579
|
if(row < uniforms.dim_a_outer && col < uniforms.dim_inner)
|
|
@@ -4584,7 +4584,7 @@ bIndices[${S-2}] = u32(row);
|
|
|
4584
4584
|
return value;
|
|
4585
4585
|
}
|
|
4586
4586
|
|
|
4587
|
-
fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${
|
|
4587
|
+
fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Ce(e,y)} {
|
|
4588
4588
|
var value = ${Ce(e,y)}(0.0);
|
|
4589
4589
|
let col = colIn * ${e};
|
|
4590
4590
|
if(row < uniforms.dim_inner && col < uniforms.dim_b_outer)
|
|
@@ -4605,11 +4605,11 @@ bIndices[${S-2}] = u32(row);
|
|
|
4605
4605
|
${h.setByIndices("vec3<u32>(coords)","value")}
|
|
4606
4606
|
}
|
|
4607
4607
|
}
|
|
4608
|
-
`},Xr=(e,t,r,n,o=!1)=>{let i=e[0].dims,a=e[1].dims,l=i.slice(0,-2),d=a.slice(0,-2),
|
|
4609
|
-
${Y.registerUniforms(ee).registerInternalVariables(se).declareVariables(...Z,
|
|
4608
|
+
`},Xr=(e,t,r,n,o=!1)=>{let i=e[0].dims,a=e[1].dims,l=i.slice(0,-2),d=a.slice(0,-2),c=n?n.slice(0,-2):r.slice(0,-2),m=k.size(c),u=i[i.length-2],h=i[i.length-1],w=a[a.length-1],g=h%4===0&&w%4===0,y=u<=8?[4,1,1]:[4,4,1],x=[8,8,1],_=[Math.ceil(w/x[0]/y[0]),Math.ceil(u/x[1]/y[1]),Math.ceil(m/x[2]/y[2])],$=g?4:1,S=[...l,u,h/$],I=S.length,T=[...d,h,w/$],A=T.length,O=[m,u,w/$],z=[{type:6,data:u},{type:6,data:w},{type:6,data:h}];We(t,z),z.push(...U(c,S,T));let W=["rank","rank"],V=e.length>2;V&&(z.push(...U(e[2].dims)),W.push("rank")),z.push(...U(O));let j=Y=>{let ae=c.length,se=Wr("batchDims",e[0].dataType,ae,1),ie=pe(e[0].dataType),q=E("a",e[0].dataType,I,$),ce=E("b",e[1].dataType,A,$),oe=D("result",e[0].dataType,O.length,$),Z=[q,ce];if(V){let R=o?$:1;Z.push(E("bias",e[2].dataType,e[2].dims.length,R))}let ee=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];Le(t,ee);let K=pe(oe.type.tensor),me=Ne(t,oe.type.value,K),$e=vm($,V,me,[se,q,ce,oe],[l,d,c],o);return`
|
|
4609
|
+
${Y.registerUniforms(ee).registerInternalVariables(se).declareVariables(...Z,oe)}
|
|
4610
4610
|
${$e}
|
|
4611
4611
|
${g?Zt(y,x,ie,se):Qt(y,x,ie,se)}
|
|
4612
|
-
`};return{name:"MatMul",shaderCache:{hint:`${y};${t.activation};${g};${o}`,inputDependencies:W},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:_[0],y:_[1],z:_[2]},programUniforms:z}),getShaderSource:j}}});var $m,_u,xu=M(()=>{"use strict";X();st();
|
|
4612
|
+
`};return{name:"MatMul",shaderCache:{hint:`${y};${t.activation};${g};${o}`,inputDependencies:W},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:_[0],y:_[1],z:_[2]},programUniforms:z}),getShaderSource:j}}});var $m,_u,xu=M(()=>{"use strict";X();st();ne();ut();Xt();oo();Jt();$m=(e,t,r,n,o=!1,i,a=4,l=4,d=4,c="f32")=>{let m=V=>{switch(V){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${c}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${V} is not supported.`)}},u=V=>{switch(V){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${V} is not supported.`)}},h=e?`
|
|
4613
4613
|
let coord = vec4<i32>(batch, xRow, xCol, xCh);
|
|
4614
4614
|
`:`
|
|
4615
4615
|
let coord = vec4<i32>(batch, xCh, xRow, xCol);
|
|
@@ -4636,7 +4636,7 @@ bIndices[${S-2}] = u32(row);
|
|
|
4636
4636
|
let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];
|
|
4637
4637
|
let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];
|
|
4638
4638
|
let xCh = ${_} % inChannels;
|
|
4639
|
-
var resData = ${Ce(a,
|
|
4639
|
+
var resData = ${Ce(a,c)}(0.0);
|
|
4640
4640
|
// The bounds checking is always needed since we use it to pad zero for
|
|
4641
4641
|
// the 'same' padding type.
|
|
4642
4642
|
if (xRow >= 0 && xRow < ${g} && xCol >= 0 && xCol < ${y}) {
|
|
@@ -4651,14 +4651,14 @@ bIndices[${S-2}] = u32(row);
|
|
|
4651
4651
|
if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {
|
|
4652
4652
|
${$}
|
|
4653
4653
|
}
|
|
4654
|
-
return ${Ce(a,
|
|
4654
|
+
return ${Ce(a,c)}(0.0);`:n&&r?`
|
|
4655
4655
|
let col = colIn * ${a};
|
|
4656
4656
|
${$}`:`
|
|
4657
4657
|
let col = colIn * ${a};
|
|
4658
4658
|
if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
|
|
4659
4659
|
${$}
|
|
4660
4660
|
}
|
|
4661
|
-
return ${Ce(a,
|
|
4661
|
+
return ${Ce(a,c)}(0.0);`,I=`${u(l)}`,T=Ce(d,c),A=e?Ce(a,c):Ce(l,c),O=e?Ce(l,c):Ce(a,c),z=Ne(i,T,c);return`
|
|
4662
4662
|
fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${A} {
|
|
4663
4663
|
${e?S:I}
|
|
4664
4664
|
}
|
|
@@ -4678,15 +4678,15 @@ bIndices[${S-2}] = u32(row);
|
|
|
4678
4678
|
${z}
|
|
4679
4679
|
setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);
|
|
4680
4680
|
}
|
|
4681
|
-
}`},_u=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",
|
|
4682
|
-
fn setOutputAtIndex(flatIndex : i32, value : ${g?`vec4<${
|
|
4683
|
-
result[flatIndex] = ${g?`vec4<${
|
|
4681
|
+
}`},_u=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",c=d?e[0].dims[3]:e[0].dims[1],m=r[0],u=d?r[2]:r[3],h=d?r[1]:r[2],w=d?r[3]:r[1],g=d&&(c%4===0||c%3===0)&&w%4===0,y=d?w:u*h,x=d?u*h:w,_=[8,8,1],$=n<=8?[4,1,1]:[4,4,1],S=[Math.ceil(y/_[0]/$[0]),Math.ceil(x/_[1]/$[1]),Math.ceil(m/_[2]/$[2])];be("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${S}`);let I=g?d&&c%4!==0?3:4:1,T=_[1]*$[1],A=_[0]*$[0],O=Math.max(_[0]*I,_[1]),z=n%T===0,W=o%A===0,V=i%O===0,j=g?[I,4,4]:[1,1,1],Y=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];We(t,Y),Y.push(...U(e[0].dims,e[1].dims));let ae=["rank","rank"];a&&(Y.push(...U(e[2].dims)),ae.push("rank")),Y.push(...U(r));let se=ie=>{let q=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];Le(t,q);let ce=g?4:1,oe=pe(e[0].dataType),Z=`
|
|
4682
|
+
fn setOutputAtIndex(flatIndex : i32, value : ${g?`vec4<${oe}>`:oe}) {
|
|
4683
|
+
result[flatIndex] = ${g?`vec4<${oe}>`:oe}(value);
|
|
4684
4684
|
}
|
|
4685
|
-
fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${g?`vec4<${
|
|
4685
|
+
fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${g?`vec4<${oe}>`:oe}) {
|
|
4686
4686
|
let flatIndex = getOutputIndexFromCoords(vec4<i32>(d0, d1, d2, d3));
|
|
4687
4687
|
setOutputAtIndex(flatIndex ${g?"/ 4":""}, value);
|
|
4688
|
-
}`,ee=
|
|
4689
|
-
fn getBiasByOutputCoords(coords : vec4<i32>) -> ${g?`vec4<${
|
|
4688
|
+
}`,ee=E("x",e[0].dataType,e[0].dims.length,I===3?1:I),K=E("w",e[1].dataType,e[1].dims.length,ce),me=[ee,K],$e=D("result",e[0].dataType,r.length,ce);if(a){let R=E("bias",e[2].dataType,e[2].dims.length,ce);me.push(R),Z+=`
|
|
4689
|
+
fn getBiasByOutputCoords(coords : vec4<i32>) -> ${g?`vec4<${oe}>`:oe} {
|
|
4690
4690
|
return bias[coords.${d?"w":"y"}${g?"/ 4":""}];
|
|
4691
4691
|
}`}return`
|
|
4692
4692
|
${Yr("uniforms.result_strides")}
|
|
@@ -4695,8 +4695,8 @@ bIndices[${S-2}] = u32(row);
|
|
|
4695
4695
|
// dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32 };
|
|
4696
4696
|
${ie.registerUniforms(q).declareVariables(...me,$e)}
|
|
4697
4697
|
${Z}
|
|
4698
|
-
${$m(d,z,W,V,a,t,j[0],j[1],j[2],
|
|
4699
|
-
${g?Zt($,_,
|
|
4698
|
+
${$m(d,z,W,V,a,t,j[0],j[1],j[2],oe)}
|
|
4699
|
+
${g?Zt($,_,oe,void 0,!d,O):Qt($,_,oe,void 0,!d,O,!1,void 0,l)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${I};${g};${z};${W};${V};${T};${A};${O}`,inputDependencies:ae},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:Y}),getShaderSource:se}}});var _m,Su,Zr,xm,Iu,Sm,Cu,Tu,Au=M(()=>{"use strict";X();st();te();ne();ut();Xt();_m=e=>{let t=1;for(let r=0;r<e.length;r++)t*=e[r];return t},Su=e=>typeof e=="number"?[e,e,e]:e,Zr=(e,t)=>t<=1?e:e+(e-1)*(t-1),xm=(e,t,r,n=1)=>{let o=Zr(t,n);return Math.floor((e[0]*(r-1)-r+o)/2)},Iu=(e,t,r,n,o)=>{o==null&&(o=xm(e,t[0],n[0]));let i=[0,0,0,r];for(let a=0;a<3;a++)e[a]+2*o>=t[a]&&(i[a]=Math.trunc((e[a]-t[a]+2*o)/n[a]+1));return i},Sm=(e,t,r,n,o,i,a,l,d,c)=>{let m,u,h,w;if(e==="VALID"&&(e=0),typeof e=="number"){m={top:e,bottom:e,left:e,right:e,front:e,back:e};let g=Iu([t,r,n,1],[l,d,c],1,[o,i,a],e);u=g[0],h=g[1],w=g[2]}else if(Array.isArray(e)){if(!e.every((y,x,_)=>y===_[0]))throw Error(`Unsupported padding parameter: ${e}`);m={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let g=Iu([t,r,n,1],[l,d,c],1,[o,i,a],e[0]);u=g[0],h=g[1],w=g[2]}else if(e==="SAME_UPPER"){u=Math.ceil(t/o),h=Math.ceil(r/i),w=Math.ceil(n/a);let g=(u-1)*o+l-t,y=(h-1)*i+d-r,x=(w-1)*a+c-n,_=Math.floor(g/2),$=g-_,S=Math.floor(y/2),I=y-S,T=Math.floor(x/2),A=x-T;m={top:S,bottom:I,left:T,right:A,front:_,back:$}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:m,outDepth:u,outHeight:h,outWidth:w}},Cu=(e,t,r,n,o,i=!1,a="channelsLast")=>{let l,d,c,m,u;if(a==="channelsLast")[l,d,c,m,u]=e;else if(a==="channelsFirst")[l,u,d,c,m]=e;else throw new Error(`Unknown dataFormat ${a}`);let[h,,w,g,y]=t,[x,_,$]=Su(r),[S,I,T]=Su(n),A=Zr(w,S),O=Zr(g,I),z=Zr(y,T),{padInfo:W,outDepth:V,outHeight:j,outWidth:Y}=Sm(o,d,c,m,x,_,$,A,O,z),ae=i?h*u:h,se=[0,0,0,0,0];return a==="channelsFirst"?se=[l,ae,V,j,Y]:a==="channelsLast"&&(se=[l,V,j,Y,ae]),{batchSize:l,dataFormat:a,inDepth:d,inHeight:c,inWidth:m,inChannels:u,outDepth:V,outHeight:j,outWidth:Y,outChannels:ae,padInfo:W,strideDepth:x,strideHeight:_,strideWidth:$,filterDepth:w,filterHeight:g,filterWidth:y,effectiveFilterDepth:A,effectiveFilterHeight:O,effectiveFilterWidth:z,dilationDepth:S,dilationHeight:I,dilationWidth:T,inShape:e,outShape:se,filterShape:t}},Tu=(e,t,r,n,o,i)=>{let a=i==="channelsLast",l=a?e[0].dims[3]:e[0].dims[1],d=!1,c=[64,1,1],m={x:r.map(($,S)=>S)},u=[Math.ceil(_m(m.x.map($=>r[$]))/c[0]),1,1];be("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${u}`);let h=d?a&&l%4!==0?3:4:1,w=k.size(r),g=[{type:12,data:w},{type:12,data:n},{type:12,data:o},{type:12,data:t.strides},{type:12,data:t.dilations}];We(t,g),g.push(...U(e[0].dims,e[1].dims));let y=["rank","rank"],x=e.length===3;x&&(g.push(...U(e[2].dims)),y.push("rank")),g.push(...U(r));let _=$=>{let S=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:n.length},{name:"pads",type:"u32",length:o.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];Le(t,S);let I=d?4:1,T=pe(e[0].dataType),A=E("x",e[0].dataType,e[0].dims.length,h===3?1:h),O=E("W",e[1].dataType,e[1].dims.length,I),z=[A,O],W=D("result",e[0].dataType,r.length,I),V="";if(x){let ae=E("bias",e[2].dataType,e[2].dims.length,I);z.push(ae),V+=`
|
|
4700
4700
|
fn getBiasByOutputCoords(coords : array<u32, 5>) -> ${d?`vec4<${T}>`:T} {
|
|
4701
4701
|
return bias[${a?L("coords",4,5):L("coords",1,5)}${d?"/ 4":""}];
|
|
4702
4702
|
}`}let j=Ce(h,T),Y=Ne(t,j,T);return`
|
|
@@ -4804,7 +4804,7 @@ bIndices[${S-2}] = u32(row);
|
|
|
4804
4804
|
${x?"value = value + getBiasByOutputCoords(coords)":""};
|
|
4805
4805
|
${Y}
|
|
4806
4806
|
result[global_idx] = f32(value);
|
|
4807
|
-
}`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${a};${h};${x}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:u[0],y:u[1],z:u[2]},programUniforms:g}),getShaderSource:_}}});var io,ku,Eu=M(()=>{"use strict";X();
|
|
4807
|
+
}`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${a};${h};${x}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:u[0],y:u[1],z:u[2]},programUniforms:g}),getShaderSource:_}}});var io,ku,Eu=M(()=>{"use strict";X();te();ne();ao();ut();io=(e,t,r)=>{let n=e.length>2,o=n?"value += b[output_channel];":"",i=e[0].dims,a=e[1].dims,l=a[0]/t.group,d=t.format==="NHWC",c=Qr(i,a,t.dilations,t.pads,t.strides,d),m=k.size(c),u=[{type:12,data:m},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:l}];We(t,u),u.push(...U(i,a));let h=["rank","rank"];n&&(u.push(...U(e[2].dims)),h.push("rank")),u.push(...U(c));let w=g=>{let y=D("output",e[0].dataType,c.length),x=pe(y.type.tensor),_=Ne(t,y.type.value,x),$=E("x",e[0].dataType,i.length),S=E("w",e[1].dataType,a.length),I=[$,S];n&&I.push(E("b",e[2].dataType,e[2].dims.length));let T=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];return Le(t,T),`
|
|
4808
4808
|
${g.registerUniforms(T).declareVariables(...I,y)}
|
|
4809
4809
|
|
|
4810
4810
|
${g.mainStart()}
|
|
@@ -4841,7 +4841,7 @@ bIndices[${S-2}] = u32(row);
|
|
|
4841
4841
|
${o}
|
|
4842
4842
|
${_}
|
|
4843
4843
|
${y.setByOffset("global_idx","value")}
|
|
4844
|
-
}`};return{name:"GroupedConv",shaderCache:{hint:t.cacheKey,inputDependencies:h},getRunData:()=>({outputs:[{dims:r?r(
|
|
4844
|
+
}`};return{name:"GroupedConv",shaderCache:{hint:t.cacheKey,inputDependencies:h},getRunData:()=>({outputs:[{dims:r?r(c):c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:u}),getShaderSource:w}},ku=(e,t,r)=>{let n=e.length>2,o=he(r[3]),i=he(r[2]),a=k.size(r)/o/i,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/o],d=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/o],c=[r[0],r[1],r[2],r[3]/o],m=[{type:12,data:a},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];We(t,m),m.push(...U(l,d,c));let u=(i-1)*t.strides[1]+d[1],h=w=>{let g=D("output",e[0].dataType,c.length,o),y=pe(g.type.tensor),x=Ne(t,g.type.value,y),_=E("x",e[0].dataType,l.length,o),$=E("w",e[1].dataType,d.length,o),S=[_,$];n&&S.push(E("b",e[2].dataType,e[2].dims,o));let I=n?"value += b[output_channel];":"",T=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return Le(t,T),`
|
|
4845
4845
|
${w.registerUniforms(T).declareVariables(...S,g)}
|
|
4846
4846
|
${w.mainStart()}
|
|
4847
4847
|
${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
@@ -4886,7 +4886,7 @@ bIndices[${S-2}] = u32(row);
|
|
|
4886
4886
|
${x}
|
|
4887
4887
|
${g.set("batch","row","col + i","output_channel","value")};
|
|
4888
4888
|
}
|
|
4889
|
-
}`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${o};${i};${u};${d[0]};${d[1]}`,inputDependencies:n?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:m}),getShaderSource:h}}});var so,Im,Pu,uo=M(()=>{"use strict";X();
|
|
4889
|
+
}`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${o};${i};${u};${d[0]};${d[1]}`,inputDependencies:n?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:m}),getShaderSource:h}}});var so,Im,Pu,uo=M(()=>{"use strict";X();te();Jt();ne();ut();so=(e,t,r,n,o=!1)=>{let i=e[0].dims,a=e[1].dims,l=i[i.length-2],d=a[a.length-1],c=i[i.length-1],m=he(d),u=he(c),h=he(l),w=k.size(r)/m/h,g=e.length>2,y=n?n.slice(0,-2):r.slice(0,-2),_=[k.size(y),l,d],$=[{type:12,data:w},{type:12,data:l},{type:12,data:d},{type:12,data:c}];We(t,$),$.push(...U(y,i,a)),g&&$.push(...U(e[2].dims)),$.push(...U(_));let S=I=>{let T=Wr("batch_dims",e[0].dataType,y.length),A=E("a",e[0].dataType,i.length,u),O=E("b",e[1].dataType,a.length,m),z=D("output",e[0].dataType,_.length,m),W=pe(z.type.tensor),V=Ne(t,z.type.value,W),j=[A,O],Y="";if(g){let ee=o?m:1;j.push(E("bias",e[2].dataType,e[2].dims.length,ee)),Y=`${o?`value += bias[col / ${ee}];`:`value += ${z.type.value}(bias[row + i]);`}`}let ae=i.slice(0,-2),se=a.slice(0,-2),ie=Mt(ae,y),q=Mt(se,y),ce=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];Le(t,ce);let oe=(ee,K)=>{let me=ee.rank,$e=ee.name;if(me===2)return`var ${$e}_indices = ${ee.type.indices}(0u, 0u);`;let R=T.rank,G=`var ${$e}_indices: ${ee.type.indices};`;for(let ue=me-2-1,Be=R-1;ue>=0;ue--,Be--)G+=`
|
|
4890
4890
|
${$e}_indices[${ue}] = ${R>1?`batch_indices[${Be}]`:"batch_indices"};`;return K.forEach(ue=>{G+=`
|
|
4891
4891
|
${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
4892
4892
|
${$e}_indices[${me-1}] = 0u;`,G},Z=()=>{let ee=`var a_data: ${A.type.value};`;for(let K=0;K<u;K++)ee+=`
|
|
@@ -4903,9 +4903,9 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
4903
4903
|
let batch = index1 / stride1;
|
|
4904
4904
|
|
|
4905
4905
|
${r.length===2?"":`let batch_indices = ${T.offsetToIndices("batch")};`}
|
|
4906
|
-
${
|
|
4906
|
+
${oe(A,ie)}
|
|
4907
4907
|
let a_offset = ${A.indicesToOffset("a_indices")};
|
|
4908
|
-
${
|
|
4908
|
+
${oe(O,q)}
|
|
4909
4909
|
let b_offset = ${O.indicesToOffset("b_indices")};
|
|
4910
4910
|
var values: array<${z.type.value}, ${h}>;
|
|
4911
4911
|
for (var k: u32 = 0u; k < uniforms.K; k = k + ${u}) {
|
|
@@ -4920,7 +4920,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
4920
4920
|
${z.setByOffset(`offset / ${m}`,"value")};
|
|
4921
4921
|
}
|
|
4922
4922
|
}
|
|
4923
|
-
`};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${m};${u};${h};${o}`,inputDependencies:g?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:$}),getShaderSource:S}},Im=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},Pu=e=>{Im(e.inputs);let t=Je.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],n=e.inputs[0].dims[e.inputs[0].dims.length-1];r<8&&n<8?e.compute(so(e.inputs,{activation:""},t)):e.compute(Xr(e.inputs,{activation:""},t))}});var Qr,lo,Cm,co,po,Tm,Am,km,mo,ao=M(()=>{"use strict";
|
|
4923
|
+
`};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${m};${u};${h};${o}`,inputDependencies:g?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:$}),getShaderSource:S}},Im=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},Pu=e=>{Im(e.inputs);let t=Je.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],n=e.inputs[0].dims[e.inputs[0].dims.length-1];r<8&&n<8?e.compute(so(e.inputs,{activation:""},t)):e.compute(Xr(e.inputs,{activation:""},t))}});var Qr,lo,Cm,co,po,Tm,Am,km,mo,ao=M(()=>{"use strict";te();xu();Au();Jt();Eu();ut();uo();Ct();Qr=(e,t,r,n,o,i)=>{let a=e[0],l=e.slice(i?1:2,i?3:4),d=l.length,c=t[0],u=t.slice(2).map((g,y)=>g+(g-1)*(r[y]-1)),w=l.map((g,y)=>g+n[y]+n[y+d]).map((g,y)=>Math.floor((g-u[y]+o[y])/o[y]));return w.splice(0,0,a),w.splice(i?3:1,0,c),w},lo=[2,3,1,0],Cm=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[1]*t.group;if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let o=e[0].dims.length-2;if(t.dilations.length!==o)throw new Error(`dilations should be ${o}D`);if(t.strides.length!==o)throw new Error(`strides should be ${o}D`);if(t.pads.length!==o*2)throw new Error(`pads should be ${o*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},co=(e,t)=>{let r=e.kernelShape.slice();for(let i=2;i<t[1].dims.length;++i)r[i-2]===0&&(r[i-2]=t[1].dims[i]);let n=e.pads.slice();xt.adjustPadsBasedOnAutoPad(t[0].dims,e.strides,e.dilations,r,n,e.format==="NHWC",e.autoPad);let o=Object.assign({},e);return Object.assign(o,{kernelShape:r,pads:n}),o},po=e=>{let t=Kr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],o=e.dilations,i=e.group,a=e.kernel_shape,l=e.pads,d=e.strides,c=e.w_is_const();return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,pads:l,strides:d,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},Tm=(e,t,r)=>{let n=co(r,t),o=r.format==="NHWC";if(r.group!==1){if(!e.adapterInfo.isArchitecture("ampere")&&o&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1){let O=Qr(t[0].dims,t[1].dims,r.dilations,n.pads,r.strides,o),z=e.kernelCustomData.wT??e.compute(Re(t[1],lo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=z);let W=[t[0],z];t.length===3&&W.push(t[2]),e.compute(ku(W,n,O),{inputs:W})}else e.compute(io(t,n));return}let i=t.length===3,a=t[0].dims[o?1:2],l=t[0].dims[o?2:3],d=t[0].dims[o?3:1],c=t[1].dims[2],m=t[1].dims[3],u=Qr(t[0].dims,t[1].dims,r.dilations,n.pads,r.strides,o),h=u[o?1:2],w=u[o?2:3],g=u[o?3:1],y=o&&c===a&&m===l&&r.pads[0]===0&&r.pads[1]===0;if(y||c===1&&m===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let A=u[0],O,z,W,V=[];if(o){let ae=e.kernelCustomData.wT??e.compute(Re(t[1],lo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=ae),y){let se=a*l*d;O=t[0].reshape([1,A,se]),z=ae.reshape([1,se,g]),W=[1,A,g]}else O=t[0].reshape([A,a*l,d]),z=ae.reshape([1,d,g]),W=[A,h*w,g];V.push(O),V.push(z)}else O=t[0].reshape([A,d,a*l]),z=t[1].reshape([1,g,d]),W=[A,g,h*w],V.push(z),V.push(O);i&&V.push(t[2]);let j=W[2],Y=V[0].dims[V[0].dims.length-1];j<8&&Y<8?e.compute(so(V,n,u,W,o),{inputs:V}):e.compute(Xr(V,n,u,W,o),{inputs:V});return}let x=!0,_=e.kernelCustomData.wT??e.compute(Re(t[1],lo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=_);let $=[t[0],_];i&&$.push(t[2]);let S=o?h*w:g,I=o?g:h*w,T=c*m*d;e.compute(_u($,n,u,S,I,T,i,x),{inputs:$})},Am=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=[0,t.pads[0],0,t.pads[1]],i=[1].concat(t.strides),a=[1].concat(t.dilations),l=[1].concat(t.kernelShape),d=co({...t,pads:o,strides:i,dilations:a,kernelShape:l},n);e.compute(io(n,d,c=>r?[c[0],c[2],c[3]]:[c[0],c[1],c[3]]))},km=(e,t,r)=>{let n=r.format==="NHWC"?"channelsLast":"channelsFirst",o=co(r,t),i=r.autoPad==="NOTSET"?r.pads:r.autoPad,a=Cu(t[0].dims,t[1].dims,r.strides,r.dilations,i,!1,n);e.compute(Tu(t,o,a.outShape,[a.filterDepth,a.filterHeight,a.filterWidth],[a.padInfo.front,a.padInfo.top,a.padInfo.left],n))},mo=(e,t)=>{Cm(e.inputs,t),e.inputs[0].dims.length===3?Am(e,t):e.inputs[0].dims.length===5?km(e,e.inputs,t):Tm(e,e.inputs,t)}});var Em,Ou,zu=M(()=>{"use strict";X();st();ne();ut();Xt();oo();Jt();Em=(e,t=!1,r,n,o=4)=>{let i=_=>{switch(_){case 1:return"return w[getIndexFromCoords4D(coord, vec4<i32>(uniforms.w_shape))];";case 4:return`
|
|
4924
4924
|
let coord1 = vec4<i32>(coordX, coordY, col + 1, rowInner);
|
|
4925
4925
|
let coord2 = vec4<i32>(coordX, coordY, col + 2, rowInner);
|
|
4926
4926
|
let coord3 = vec4<i32>(coordX, coordY, col + 3, rowInner);
|
|
@@ -4945,7 +4945,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
4945
4945
|
row,
|
|
4946
4946
|
col / outWidth,
|
|
4947
4947
|
col % outWidth);
|
|
4948
|
-
`,d=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",
|
|
4948
|
+
`,d=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",c=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",m=e?"row":"col",u=e?"col":"row",h=`
|
|
4949
4949
|
let inChannels = ${e?"i32(uniforms.x_shape[3])":"i32(uniforms.x_shape[1])"};
|
|
4950
4950
|
let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
|
|
4951
4951
|
let outRow = ${m} / outWidth;
|
|
@@ -4958,7 +4958,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
4958
4958
|
if (xR < 0.0 || xR >= f32(${d}) || fract(xR) > 0.0) {
|
|
4959
4959
|
return ${n}(0.0);
|
|
4960
4960
|
}
|
|
4961
|
-
if (xC < 0.0 || xC >= f32(${
|
|
4961
|
+
if (xC < 0.0 || xC >= f32(${c}) || fract(xC) > 0.0) {
|
|
4962
4962
|
return ${n}(0.0);
|
|
4963
4963
|
}
|
|
4964
4964
|
let iXR = i32(xR);
|
|
@@ -5005,21 +5005,21 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5005
5005
|
${y}
|
|
5006
5006
|
result[getIndexFromCoords4D(coords, vec4<i32>(uniforms.result_shape))/${o}] = value;
|
|
5007
5007
|
}
|
|
5008
|
-
}`},Ou=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",
|
|
5008
|
+
}`},Ou=(e,t,r,n,o,i,a,l)=>{let d=t.format==="NHWC",c=d?e[0].dims[3]:e[0].dims[1],m=r[0],u=d?r[2]:r[3],h=d?r[1]:r[2],w=d?r[3]:r[1],g=d&&c%4===0&&c%3&&w%4===0,y=d?w:u*h,x=d?u*h:w,_=[8,8,1],$=n<=8?[4,1,1]:[4,4,1],S=[Math.ceil(y/_[0]/$[0]),Math.ceil(x/_[1]/$[1]),Math.ceil(m/_[2]/$[2])];be("verbose",()=>`[conv_backprop_mm_webgpu] dispatch = ${S}`);let I=g?4:1,T=Math.max(_[0]*I,_[1]),A=g?4:1,O=[t.kernelShape[d?1:2],t.kernelShape[d?2:3]],z=[O[0]+(t.dilations[0]<=1?0:(O[0]-1)*(t.dilations[0]-1)),O[1]+(t.dilations[1]<=1?0:(O[1]-1)*(t.dilations[1]-1))],W=[z[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),z[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],V=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:t.strides},{type:6,data:t.dilations},{type:6,data:O},{type:6,data:W}];We(t,V),V.push(...U(e[0].dims,e[1].dims));let j=["rank","rank"];a&&(V.push(...U(e[2].dims)),j.push("rank")),V.push(...U(r));let Y=ae=>{let se=E("x",e[0].dataType,e[0].dims.length,A),ie=E("w",e[1].dataType,e[1].dims.length,1),q=D("result",e[0].dataType,r.length,A),ce=[se,ie],oe="";if(a){let K=E("bias",e[2].dataType,e[2].dims.length,A);ce.push(K),oe+=`
|
|
5009
5009
|
fn getBiasByOutputCoords(coords : vec4<i32>) -> ${K.type.value} {
|
|
5010
5010
|
return bias[coords.${d?"w":"y"}${g?"/ 4":""}];
|
|
5011
5011
|
}`}let Z=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"strides",type:"i32",length:2},{name:"dilations",type:"i32",length:2},{name:"filter_dims",type:"i32",length:O.length},{name:"pads",type:"i32",length:W.length}];Le(t,Z);let ee=pe(e[0].dataType,1);if(ee!=="f16"&&ee!=="f32")throw new Error(`elemType ${ee} is not supported.`);return`
|
|
5012
5012
|
${Yr("uniforms.result_strides")}
|
|
5013
5013
|
${ae.registerUniforms(Z).declareVariables(...ce,q)};
|
|
5014
|
-
${
|
|
5014
|
+
${oe}
|
|
5015
5015
|
${Em(d,a,t,se.type.value,I)}
|
|
5016
|
-
${g?Zt($,_,ee,void 0,!d,T):Qt($,_,ee,void 0,!d,T,!1,void 0,l)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${$};${_};${g}`,inputDependencies:j},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:V}),getShaderSource:Y}}});var Pm,fo,Bu=M(()=>{"use strict";X();st();
|
|
5016
|
+
${g?Zt($,_,ee,void 0,!d,T):Qt($,_,ee,void 0,!d,T,!1,void 0,l)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${$};${_};${g}`,inputDependencies:j},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:V}),getShaderSource:Y}}});var Pm,fo,Bu=M(()=>{"use strict";X();st();te();ne();Pm=(e,t,r,n,o,i=!1,a,l,d=!1)=>{let c=d?1:2,m=d?2:3,u=d?3:1,h=i?2:1,w=`
|
|
5017
5017
|
fn setOutputAtIndex(flatIndex : u32, value : ${i?`vec4<${a}>`:a}) {
|
|
5018
5018
|
result[flatIndex] = ${i?`vec4<${a}>`:a}(value);
|
|
5019
5019
|
}`;n&&(w+=`
|
|
5020
5020
|
fn getBiasByOutputCoords(coords : vec4<u32>) -> ${i?`vec4<${a}>`:a} {
|
|
5021
5021
|
return bias[coords.${d?"w":"y"}${i?"/ 4":""}];
|
|
5022
|
-
}`);let g=i?4:1,y=
|
|
5022
|
+
}`);let g=i?4:1,y=E("W",t[1].dataType,t[1].dims.length,g),x=E("Dy",t[0].dataType,t[0].dims.length,g),_=[x,y];n&&_.push(E("bias",t[2].dataType,[r[u]].length,g));let $=D("result",t[0].dataType,r.length,g),S=`{
|
|
5023
5023
|
let batch: u32 = ${o?"global_id.z":"workgroup_id.z"} / uniforms.result_shape[1];
|
|
5024
5024
|
let r = ${o?"global_id.z":"workgroup_id.z"} % uniforms.result_shape[1];
|
|
5025
5025
|
let c = ${o?"global_id.y":"workgroup_id.y"} * ${h};
|
|
@@ -5126,7 +5126,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5126
5126
|
let outputIndices = ${$.offsetToIndices("global_idx")};
|
|
5127
5127
|
let batch = ${$.indicesGet("outputIndices",0)};
|
|
5128
5128
|
let d1 = ${$.indicesGet("outputIndices",u)};
|
|
5129
|
-
let r = ${$.indicesGet("outputIndices",
|
|
5129
|
+
let r = ${$.indicesGet("outputIndices",c)};
|
|
5130
5130
|
let c = ${$.indicesGet("outputIndices",m)};
|
|
5131
5131
|
let dyCorner = vec2<i32>(i32(r), i32(c)) - uniforms.pads;
|
|
5132
5132
|
let dyRCorner = dyCorner.x;
|
|
@@ -5142,7 +5142,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5142
5142
|
}
|
|
5143
5143
|
let dyR = (${a}(dyRCorner) + ${a}(wR)) / ${a}(uniforms.strides[0]);
|
|
5144
5144
|
let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x;
|
|
5145
|
-
if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[${
|
|
5145
|
+
if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[${c}]) || fract(dyR) > 0.0 ||
|
|
5146
5146
|
wRPerm < 0) {
|
|
5147
5147
|
continue;
|
|
5148
5148
|
}
|
|
@@ -5176,7 +5176,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5176
5176
|
|
|
5177
5177
|
${e.mainStart()}
|
|
5178
5178
|
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")};
|
|
5179
|
-
${i?S:I}}`},fo=(e,t,r)=>{let n=e.length>2,o=t.outputShape,i=
|
|
5179
|
+
${i?S:I}}`},fo=(e,t,r)=>{let n=e.length>2,o=t.outputShape,i=k.size(o),a=[Math.ceil(i/64),1,1];be("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${a}`);let l=t.format==="NHWC",d=["rank","rank"],c=[t.strides[0],t.strides[1]],m=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],u=[t.dilations[0],t.dilations[1]],h=[m[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),m[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],w=[h[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),h[1]-1-Math.floor(t.pads[1]+t.pads[3])/2],g=!1,y=t.group,x=e[1].dims,_=x[0]/y,$=x[1],S=[{type:12,data:i},{type:12,data:c},{type:12,data:m},{type:12,data:u},{type:12,data:h},{type:6,data:w},{type:12,data:_},{type:12,data:$},...U(e[0].dims,e[1].dims)];n&&(S.push(...U(e[2].dims)),d.push("rank")),S.push(...U(o));let I=a[1]===1&&a[2]===1,T=A=>{let O=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:c.length},{name:"filter_dims",type:"u32",length:m.length},{name:"dilations",type:"u32",length:m.length},{name:"effective_filter_dims",type:"u32",length:h.length},{name:"pads",type:"i32",length:w.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],z=pe(e[0].dataType);return`${Pm(A,e,o,n,I,g,z,O,l)}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};`,inputDependencies:d},getRunData:()=>({dispatchGroup:{x:a[0],y:a[1],z:a[2]},outputs:[{dims:r?r(o):o,dataType:e[0].dataType}],programUniforms:S}),getShaderSource:T}}});var Om,zm,Bm,Du,Ru,Dm,Rm,Mm,Um,Mu,Uu=M(()=>{"use strict";zu();Bu();ut();Ct();Om=(e,t,r,n,o,i)=>(e-1)*t+r+(n-1)*o+1-i,zm=(e,t,r,n,o)=>{let i=Math.floor(e/2);t==="SAME_UPPER"?(r[n]=i,r[o]=e-i):t==="SAME_LOWER"&&(r[n]=e-i,r[o]=i)},Bm=(e,t,r,n,o,i,a,l,d,c)=>{let m=e.length-2,u=c.length===0;if(d.length===0)for(let g=0;g<m;++g)d.push(0);let h=e[0],w=t[l?3:1]*o;for(let g=0,y=e.length-m-(l?1:0);g<m;++g,++y){let x=e[y],_=u?x*a[g]:c[g],$=Om(x,a[g],i[g],t[y],r[g],_);zm($,n,i,g,g+m),u&&c.push(a[g]*(x-1)+d[g]+(t[y]-1)*r[g]+1-i[g]-i[g+m])}c.splice(0,0,h),c.splice(l?3:1,0,w)},Du=(e,t)=>{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((u,h)=>u*h,1)===0){r.length=0;for(let u=2;u<t[1].dims.length;++u)r.push(t[1].dims[u])}let n=e.format==="NHWC";r.splice(0,0,t[1].dims[0]),r.splice(n?3:1,0,t[1].dims[1]);let o=e.pads.slice(),i=e.outputShape.slice(),a=e.outputPadding.slice(),l=t[0].dims,d=e.dilations.slice();if(d.reduce((u,h)=>u+h,0)===0){let u=t[0].dims.length-2;d=new Array(u).fill(1)}let c=e.strides.slice();if(c.reduce((u,h)=>u+h,0)===0){let u=t[0].dims.length-2;c=new Array(u).fill(1)}Bm(l,r,d,e.autoPad,e.group,o,c,n,a,i);let m=Object.assign({},e);return Object.assign(m,{kernelShape:r,pads:o,outputPadding:a,outputShape:i,dilations:d,strides:c}),m},Ru=e=>{let t=Kr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],o=e.dilations,i=e.group,a=e.kernelShape,l=e.pads,d=e.strides,c=e.wIsConst(),m=e.outputPadding,u=e.outputShape;return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,outputPadding:m,outputShape:u,pads:l,strides:d,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},Dm=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[0];if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let o=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==o))throw new Error("invalid bias");let i=e[0].dims.length-2;if(t.dilations.reduce((m,u)=>m+u,0)>0&&t.dilations.length!==i)throw new Error(`dilations should be ${i}D`);if(t.strides.reduce((m,u)=>m+u,0)>0&&t.strides.length!==i)throw new Error(`strides should be ${i}D`);if(t.pads.reduce((m,u)=>m+u,0)>0&&t.pads.length!==i*2)throw new Error(`pads should be ${i*2}D`);if(t.outputPadding.length!==i&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${i}D`);if(t.kernelShape.reduce((m,u)=>m+u,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Rm=[2,3,1,0],Mm=(e,t,r)=>{let n=Du(r,t),o=r.format==="NHWC",i=n.outputShape,a=i[o?3:1],l=t[0].dims[o?3:1];if(n.group!==1||a===1&&l===1){e.compute(fo(t,n));return}let d=i[o?1:2],c=i[o?2:3],m=t[1].dims[2],u=t[1].dims[3],h=o?d*c:a,w=o?a:d*c,g=m*u*l,y=!0,x=e.kernelCustomData.wT??e.compute(Re(t[1],Rm),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=x);let _=[t[0],x],$=t.length===3;$&&(!o&&t[2].dims.length===1?_.push(t[2].reshape([t[2].dims[0],1,1])):_.push(t[2])),e.compute(Ou(_,n,i,h,w,g,$,y),{inputs:_})},Um=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=t.kernelShape;(o.length===0||o[0]===0)&&(o=[e.inputs[1].dims[2]]);let i=t.dilations;(i.length===0||i[0]===0)&&(i=[1]);let a=t.strides;(a.length===0||a[0]===0)&&(a=[1]);let l=t.pads;l.length===0&&(l=[0,0]),l=[0,l[0],0,l[1]],a=[1].concat(a),i=[1].concat(i),o=[1].concat(o);let d=Du({...t,pads:l,strides:a,dilations:i,kernelShape:o},n);e.compute(fo(n,d,c=>r?[c[0],c[2],c[3]]:[c[0],c[1],c[3]]))},Mu=(e,t)=>{Dm(e.inputs,t),e.inputs[0].dims.length===3?Um(e,t):Mm(e,e.inputs,t)}});var Vm,Vu,Nu,Wu=M(()=>{"use strict";X();te();xe();ne();Vm=(e,t,r,n)=>{let o=k.size(t),i=t.length,a=E("input",e,i),l=D("output",e,i),d=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),c=k.normalizeAxis(d,i),m=u=>{let h=` i32(${a.indicesGet("inputIndices","uniforms.axis")}) `,w=L("uniforms.input_shape","uniforms.axis",i),g=n.reverse?h+(n.exclusive?" + 1":""):"0",y=n.reverse?w:h+(n.exclusive?"":" + 1");return`
|
|
5180
5180
|
${u.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(a,l)}
|
|
5181
5181
|
${u.mainStart()}
|
|
5182
5182
|
${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
@@ -5189,9 +5189,9 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5189
5189
|
sum = sum + ${a.getByIndices("inputIndices")};
|
|
5190
5190
|
}
|
|
5191
5191
|
${l.setByOffset("global_idx","sum")};
|
|
5192
|
-
}`};return{name:"CumSum",shaderCache:{hint:n.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},{type:12,data:
|
|
5192
|
+
}`};return{name:"CumSum",shaderCache:{hint:n.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},{type:12,data:c},...U(t,t)]}),getShaderSource:m}},Vu=(e,t)=>{let r=e.inputs[0].dims,n=e.inputs[0].dataType,o=e.inputs[1];e.compute(Vm(n,r,o,t),{inputs:[0]})},Nu=e=>{let t=e.exclusive===1,r=e.reverse===1;return Q({exclusive:t,reverse:r})}});var Nm,Wm,Lm,Lu,Hu,Gu=M(()=>{"use strict";X();te();xe();ne();Nm=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},Wm=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} {
|
|
5193
5193
|
var a: ${r.type.indices};`);for(let i=0;i<t;++i)o.push(r.indicesSet("a",e[i],`i[${i}]`));return o.push("return a;}"),o.join(`
|
|
5194
|
-
`)},Lm=(e,t)=>{let r,n,o,i,a,l,d=t.format==="NHWC",
|
|
5194
|
+
`)},Lm=(e,t)=>{let r,n,o,i,a,l,d=t.format==="NHWC",c=t.blocksize,m=t.mode==="DCR";d?([r,n,o,i]=e.dims,a=m?[r,n,o,c,c,i/c**2]:[r,n,o,i/c**2,c,c],l=m?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,n,o,i]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],a=m?[r,c,c,i/c**2,n,o]:[r,i/c**2,c,c,n,o],l=m?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let u=e.reshape(a),h=u.dims.length,w=e.dataType,g=E("a",w,h),y=D("output",w,h),x=_=>`
|
|
5195
5195
|
${_.registerUniform("output_size","u32").declareVariables(g,y)}
|
|
5196
5196
|
|
|
5197
5197
|
${Wm(l,h,g,y)}
|
|
@@ -5203,7 +5203,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5203
5203
|
let aIndices = perm(indices);
|
|
5204
5204
|
|
|
5205
5205
|
${y.setByOffset("global_idx",g.getByIndices("aIndices"))}
|
|
5206
|
-
}`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:_=>{let $=d?[r,n*
|
|
5206
|
+
}`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:_=>{let $=d?[r,n*c,o*c,i/c**2]:[r,i/c**2,n*c,o*c],S=k.size($),I=u.dims,T=k.sortBasedOnPerm(I,l);return{outputs:[{dims:$,dataType:_[0].dataType}],dispatchGroup:{x:Math.ceil(S/64)},programUniforms:[{type:12,data:S},...U(I,T)]}},getShaderSource:x}},Lu=(e,t)=>{Nm(e.inputs),e.compute(Lm(e.inputs[0],t))},Hu=e=>Q({blocksize:e.blocksize,mode:e.mode,format:e.format})});var ho,Jr,Fu,Hm,Gm,go,yo,qu,Fm,Ku,ju,Yu=M(()=>{"use strict";X();te();xe();ne();ho="[a-zA-Z]|\\.\\.\\.",Jr="("+ho+")+",Fu="^"+Jr+"$",Hm="("+Jr+",)*"+Jr,Gm="^"+Hm+"$",go=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let n=this.symbolToIndices.get(t);n===void 0?n=[r]:n.push(r),this.symbolToIndices.set(t,n)}},yo=class{constructor(t,r){this.equation=r;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,o]=r.includes("->")?r.split("->",2):[r,""];if(!n.match(RegExp(Gm)))throw new Error("Invalid LHS term");if(n.split(",").forEach((l,d)=>{let c=t[d].dims.slice();if(!l.match(RegExp(Fu)))throw new Error("Invalid LHS term");let m=this.processTerm(l,!0,c,d);this.lhs.push(m)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([l,d])=>d.count===1||l==="...").map(([l])=>l).join("");else if(!o.match(RegExp(Jr)))throw new Error("Invalid RHS");o.match(RegExp(ho,"g"))?.forEach(l=>{if(l==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let d=this.symbolToInfo.get(l);if(d===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(d.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(t,r,n){let o=this.symbolToInfo.get(t);if(o!==void 0){if(o.dimValue!==r&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(n)}else o={count:1,dimValue:r,inputIndices:[n]};this.symbolToInfo.set(t,o)}processTerm(t,r,n,o=-1){let i=n.length,a=!1,l=[],d=0;if(!t.match(RegExp(Fu))&&!r&&t!=="")throw new Error("Invalid LHS term");let c=t.match(RegExp(ho,"g")),m=new go(o);return c?.forEach((u,h)=>{if(u==="..."){if(a)throw new Error("Only one ellipsis is allowed per input term");a=!0;let w=i-c.length+1;if(w<0)throw new Error("Ellipsis out of bounds");if(l=n.slice(d,d+w),this.hasEllipsis){if(this.ellipsisDims.length!==l.length||this.ellipsisDims.toString()!==l.toString())throw new Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=l;else throw new Error("Ellipsis must be specified in the LHS");for(let g=0;g<l.length;g++){let y=String.fromCharCode("0".charCodeAt(0)+g);m.addSymbol(y,h+g),this.addSymbol(y,n[d++],o)}}else m.addSymbol(u,h+(this.hasEllipsis?this.ellipsisDims.length-1:0)),this.addSymbol(u,n[d++],o)}),m}},qu=e=>e+"_max",Fm=(e,t,r,n)=>{let i=e.map(m=>m.length).map((m,u)=>E(`input${u}`,t,m)),a=k.size(n),l=D("output",t,n.length),d=[...r.symbolToInfo.keys()].filter(m=>!r.rhs.symbolToIndices.has(m)),c=m=>{let u=[],h="var prod = 1.0;",w="var sum = 0.0;",g="sum += prod;",y=[],x=[],_=[],$=[],S=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((T,A)=>{if(r.rhs.symbolToIndices.has(A)){let O=r.rhs.symbolToIndices.get(A)?.[0];O!==void 0&&r.lhs.forEach((z,W)=>{if(T.inputIndices.includes(W)){let V=z.symbolToIndices.get(A);if(V===void 0)throw new Error("Invalid symbol error");V.forEach(j=>{u.push(`${i[W].indicesSet(`input${W}Indices`,j,l.indicesGet("outputIndices",O))}`)})}})}else r.lhs.forEach((O,z)=>{if(T.inputIndices.includes(z)){let W=O.symbolToIndices.get(A);if(W===void 0)throw new Error("Invalid symbol error");W.forEach(V=>{y.push(`${i[z].indicesSet(`input${z}Indices`,V,`${A}`)}`)}),$.push(`prod *= ${i[z].getByIndices(`input${z}Indices`)};`)}}),x.push(`for(var ${A}: u32 = 0; ${A} < uniforms.${qu(A)}; ${A}++) {`),_.push("}")});let I=S?[...u,`let sum = ${i.map((T,A)=>T.getByIndices(`input${A}Indices`)).join(" * ")};`]:[...u,w,...x,...y,h,...$,g,..._];return`
|
|
5207
5207
|
${m.registerUniforms(d.map(T=>({name:`${qu(T)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...i,l)}
|
|
5208
5208
|
|
|
5209
5209
|
${m.mainStart()}
|
|
@@ -5214,7 +5214,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5214
5214
|
${I.join(`
|
|
5215
5215
|
`)};
|
|
5216
5216
|
${l.setByOffset("global_idx","sum")};
|
|
5217
|
-
}`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let m=d.filter(h=>r.symbolToInfo.has(h)).map(h=>({type:12,data:r.symbolToInfo.get(h)?.dimValue||0}));m.push({type:12,data:a});let u=e.map((h,w)=>[...U(h)]).reduce((h,w)=>h.concat(w),m);return u.push(...U(n)),{outputs:[{dims:n,dataType:t}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:u}},getShaderSource:
|
|
5217
|
+
}`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let m=d.filter(h=>r.symbolToInfo.has(h)).map(h=>({type:12,data:r.symbolToInfo.get(h)?.dimValue||0}));m.push({type:12,data:a});let u=e.map((h,w)=>[...U(h)]).reduce((h,w)=>h.concat(w),m);return u.push(...U(n)),{outputs:[{dims:n,dataType:t}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:u}},getShaderSource:c}},Ku=(e,t)=>{let r=new yo(e.inputs,t.equation),n=r.outputDims,o=e.inputs.map((i,a)=>i.dims);e.compute(Fm(o,e.inputs[0].dataType,r,n))},ju=e=>{let t=e.equation.replace(/\s+/g,"");return Q({equation:t})}});var qm,Xu,Km,jm,Zu,Qu=M(()=>{"use strict";X();te();ne();qm=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=r.length<t.length?0:r.length-t.length,o=t.length<r.length?0:t.length-r.length;for(;n<r.length&&o<t.length;++n,++o)if(r[n]!==t[o]&&r[n]!==1&&t[o]!==1)throw new Error("Expand requires shape to be broadcastable to input")},Xu=(e,t)=>{let r=e.length-t.length,n=[];for(let o=0;o<r;++o)n.push(e[o]);for(let o=0;o<t.length;++o)n.push(t[o]===1?e[o+r]:t[o]);return n},Km=(e,t)=>e.length>t.length?Xu(e,t):Xu(t,e),jm=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=Km(t,r),o=e[0].dataType,i=o===9?4:1,a=Math.ceil(k.size(n)/i),l=c=>{let m=E("input",o,t.length,i),u=D("output",o,n.length,i),h;if(o===9){let w=(g,y,x="")=>`
|
|
5218
5218
|
let outputIndices${y} = ${u.offsetToIndices(`outputOffset + ${y}u`)};
|
|
5219
5219
|
let offset${y} = ${m.broadcastedIndicesToOffset(`outputIndices${y}`,u)};
|
|
5220
5220
|
let index${y} = offset${y} / 4u;
|
|
@@ -5233,14 +5233,14 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5233
5233
|
let inputOffset = ${m.broadcastedIndicesToOffset("outputIndices",u)};
|
|
5234
5234
|
${u.setByOffset("global_idx",m.getByOffset("inputOffset"))}
|
|
5235
5235
|
}`;return`
|
|
5236
|
-
${
|
|
5237
|
-
${
|
|
5238
|
-
${
|
|
5239
|
-
${h}`},d=[{type:12,data:a},...U(t,n)];return{name:"Expand",shaderCache:{hint:`${n.length}`,inputDependencies:["rank"]},getShaderSource:l,getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:d})}},Zu=e=>{qm(e.inputs),e.compute(jm(e.inputs),{inputs:[0]})}});var Ym,Ju,ed=M(()=>{"use strict";X();
|
|
5236
|
+
${c.registerUniform("vec_size","u32").declareVariables(m,u)}
|
|
5237
|
+
${c.mainStart()}
|
|
5238
|
+
${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
5239
|
+
${h}`},d=[{type:12,data:a},...U(t,n)];return{name:"Expand",shaderCache:{hint:`${n.length}`,inputDependencies:["rank"]},getShaderSource:l,getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:d})}},Zu=e=>{qm(e.inputs),e.compute(jm(e.inputs),{inputs:[0]})}});var Ym,Ju,ed=M(()=>{"use strict";X();te();ne();qr();Ym=e=>{let t=e[0].dataType,r=k.size(e[0].dims),n=k.size(e[1].dims),o=n%4===0,i=a=>{let l=E("x",t,[1],4),d=E("bias",t,[1],4),c=D("y",t,[1],4),m=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],u=w=>`
|
|
5240
5240
|
let bias${w}_offset: u32 = (global_idx * 4 + ${w}) % uniforms.bias_size;
|
|
5241
5241
|
let bias${w} = ${d.getByOffset(`bias${w}_offset / 4`)}[bias${w}_offset % 4];`,h=o?`
|
|
5242
5242
|
let bias = ${d.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${u(0)}${u(1)}${u(2)}${u(3)}
|
|
5243
|
-
let bias = ${l.type.value}(bias0, bias1, bias2, bias3);`;return`${a.registerUniforms(m).declareVariables(l,d,
|
|
5243
|
+
let bias = ${l.type.value}(bias0, bias1, bias2, bias3);`;return`${a.registerUniforms(m).declareVariables(l,d,c)}
|
|
5244
5244
|
|
|
5245
5245
|
${ro(ke(t))}
|
|
5246
5246
|
|
|
@@ -5250,8 +5250,8 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5250
5250
|
let x = ${l.getByOffset("global_idx")};
|
|
5251
5251
|
${h}
|
|
5252
5252
|
let x_in = x + bias;
|
|
5253
|
-
${
|
|
5254
|
-
}`};return{name:"FastGeluWithBias",shaderCache:{hint:`${o}`,inputDependencies:["type","type"]},getShaderSource:i,getRunData:a=>({outputs:[{dims:a[0].dims,dataType:a[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:n}],dispatchGroup:{x:Math.ceil(r/St/4)}})}},Ju=e=>{e.inputs.length<2||
|
|
5253
|
+
${c.setByOffset("global_idx",no("x_in"))}
|
|
5254
|
+
}`};return{name:"FastGeluWithBias",shaderCache:{hint:`${o}`,inputDependencies:["type","type"]},getShaderSource:i,getRunData:a=>({outputs:[{dims:a[0].dims,dataType:a[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:n}],dispatchGroup:{x:Math.ceil(r/St/4)}})}},Ju=e=>{e.inputs.length<2||k.size(e.inputs[1].dims)===0?eu(e):e.compute(Ym(e.inputs))}});var Xm,Zm,td,rd,nd=M(()=>{"use strict";X();te();xe();ne();Xm=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Zm=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r.length,i=k.normalizeAxis(t.axis,o),a=r.slice(0);a.splice(i,1,...n);let l=r[i],d=e[0].dataType===9?4:1,c=Math.ceil(k.size(a)/d),m=[{type:12,data:c},{type:6,data:l},{type:12,data:i},...U(e[0].dims,e[1].dims,a)],u=h=>{let w=E("data",e[0].dataType,e[0].dims.length,d),g=E("inputIndices",e[1].dataType,e[1].dims.length),y=D("output",e[0].dataType,a.length,d),x=$=>{let S=n.length,I=`var indicesIndices${$} = ${g.type.indices}(0);`;for(let T=0;T<S;T++)I+=`${S>1?`indicesIndices${$}[${T}]`:`indicesIndices${$}`} = ${a.length>1?`outputIndices${$}[uniforms.axis + ${T}]`:`outputIndices${$}`};`;I+=`
|
|
5255
5255
|
var idx${$} = ${g.getByIndices(`indicesIndices${$}`)};
|
|
5256
5256
|
if (idx${$} < 0) {
|
|
5257
5257
|
idx${$} = idx${$} + uniforms.axisDimLimit;
|
|
@@ -5282,8 +5282,8 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5282
5282
|
${h.mainStart()}
|
|
5283
5283
|
${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
5284
5284
|
${_}
|
|
5285
|
-
}`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(
|
|
5286
|
-
indices input tensors be of same rank.`)},Jm=(e,t)=>{let r=e[0].dims,n=e[0].dataType,o=r.length,i=e[1].dims,a=e[1].dataType,l=
|
|
5285
|
+
}`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:m}),getShaderSource:u}},td=e=>Q({axis:e.axis}),rd=(e,t)=>{let r=e.inputs;Xm(r),e.compute(Zm(e.inputs,t))}});var Qm,Jm,od,id,ad=M(()=>{"use strict";X();te();xe();ne();Qm=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and
|
|
5286
|
+
indices input tensors be of same rank.`)},Jm=(e,t)=>{let r=e[0].dims,n=e[0].dataType,o=r.length,i=e[1].dims,a=e[1].dataType,l=k.normalizeAxis(t.axis,o),d=r[l],c=i.slice(0),m=k.size(c),u=E("input",n,o),h=E("indicesInput",a,i.length),w=D("output",n,c.length),g=[{type:12,data:m},{type:6,data:d},{type:12,data:l}];return g.push(...U(r,i,c)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:g}),getShaderSource:_=>`
|
|
5287
5287
|
${_.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(u,h,w)}
|
|
5288
5288
|
${_.mainStart()}
|
|
5289
5289
|
${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
@@ -5299,7 +5299,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5299
5299
|
let value = ${u.getByIndices("inputIndices")};
|
|
5300
5300
|
|
|
5301
5301
|
${w.setByOffset("global_idx","value")};
|
|
5302
|
-
}`}},od=e=>Q({axis:e.axis}),id=(e,t)=>{let r=e.inputs;Qm(r),e.compute(Jm(e.inputs,t))}});var ef,tf,sd,ud,dd=M(()=>{"use strict";X();
|
|
5302
|
+
}`}},od=e=>Q({axis:e.axis}),id=(e,t)=>{let r=e.inputs;Qm(r),e.compute(Jm(e.inputs,t))}});var ef,tf,sd,ud,dd=M(()=>{"use strict";X();te();ne();ef=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},tf=(e,t)=>{let r=e[0].dims.slice(),n=e[1].dims.slice(),[o,i,a]=Ur.getShapeOfGemmResult(r,t.transA,n,t.transB,e.length===3?e[2].dims:void 0),l=[o,i];if(!l)throw new Error("Can't use gemm on the given tensors");let d=k.size(l),c=[{type:12,data:d},{type:12,data:o},{type:12,data:i},{type:12,data:a},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(c.push(...U(e[2].dims)),m.push("rank")),c.push(...U(l));let u=h=>{let w="";t.transA&&t.transB?w="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?w="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?w="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(w="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let g=t.alpha===1?"":"value *= uniforms.alpha;",y=E("a",e[0].dataType,e[0].dims),x=E("b",e[1].dataType,e[1].dims),_=y.type.value,$=null,S=[y,x];e.length===3&&($=E("c",e[2].dataType,e[2].dims.length),S.push($));let I=D("output",e[0].dataType,l.length);S.push(I);let T=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return`
|
|
5303
5303
|
${h.registerUniforms(T).declareVariables(...S)}
|
|
5304
5304
|
|
|
5305
5305
|
${h.mainStart()}
|
|
@@ -5316,14 +5316,14 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5316
5316
|
${g}
|
|
5317
5317
|
${(()=>$!=null?`let cOffset = ${$.broadcastedIndicesToOffset("vec2(m, n)",I)}; value += ${_}(uniforms.beta) * ${$.getByOffset("cOffset")};`:"")()}
|
|
5318
5318
|
output[global_idx] = value;
|
|
5319
|
-
}`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:l,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:
|
|
5319
|
+
}`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:l,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c}),getShaderSource:u}},sd=e=>{let t=e.transA,r=e.transB,n=e.alpha,o=e.beta;return{transA:t,transB:r,alpha:n,beta:o,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},ud=(e,t)=>{ef(e.inputs),e.compute(tf(e.inputs,t))}});var Me,of,cd,ld,af,er,pd,bo=M(()=>{"use strict";X();te();xe();Mr();Gr();ne();Ct();Me=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,of=(e,t)=>{let r=e[0],n=Me(e,1),o=Me(e,2),i=Me(e,3),a=Me(e,4),l=Me(e,5),d=Me(e,6),c=Me(e,7);if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let m=r.dims[0],u=r.dims[1],h=r.dims.length===3?r.dims[2]:t.numHeads*r.dims[4],w=u,g=0,y=0,x=Math.floor(h/t.numHeads);if(d&&c){if(d.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims[0]!==m||d.dims[1]!==t.numHeads||d.dims[3]!==x)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(c.dims[0]!==m||c.dims[1]!==t.numHeads||c.dims[3]!==x)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[2]!==c.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(c.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');g=d.dims[2],y=d.dims[2]}else if(d||c)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let _;if(n){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(n.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');_=2,w=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==x)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');_=5,w=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==x)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');_=0,w=n.dims[2]}}else{if(r.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(r.dims[2]!==t.numHeads||r.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');_=3}if(i){if(i.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(n&&n.dims.length===5&&n.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let $=g+w,S=0;if(a){S=8;let O=a.dims;throw O.length===1?O[0]===m?S=1:O[0]===3*m+2&&(S=3):O.length===2&&O[0]===m&&O[1]===$&&(S=5),S===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let I=!1,T=h;if(o){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(w!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=o.dims[2]}else{if(w!==o.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');T=o.dims[1]*o.dims[3],I=!0}}let A=!1;if(a)throw new Error("Key padding mask is not supported");if(l){if(l.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(l.dims[0]!==m||l.dims[1]!==t.numHeads||l.dims[2]!==u||l.dims[3]!==$)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:m,sequenceLength:u,pastSequenceLength:g,kvSequenceLength:w,totalSequenceLength:$,maxSequenceLength:y,inputHiddenSize:0,hiddenSize:h,vHiddenSize:T,headSize:x,vHeadSize:Math.floor(T/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:S,scale:t.scale,broadcastResPosBias:A,passPastInKv:I,qkvFormat:_}},cd=e=>Q({...e}),ld=Q({perm:[0,2,1,3]}),af=(e,t,r,n,o,i,a)=>{let l=[n,o,i],d=k.size(l),c=[{type:12,data:d},{type:12,data:a},{type:12,data:i}],m=u=>{let h=D("qkv_with_bias",t.dataType,l),w=E("qkv",t.dataType,l),g=E("bias",r.dataType,l),y=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return`
|
|
5320
5320
|
${u.registerUniforms(y).declareVariables(w,g,h)}
|
|
5321
5321
|
${u.mainStart()}
|
|
5322
5322
|
${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
5323
5323
|
let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset;
|
|
5324
5324
|
|
|
5325
5325
|
qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx];
|
|
5326
|
-
}`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:
|
|
5326
|
+
}`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c}),getShaderSource:m},{inputs:[t,r],outputs:[-1]})[0]},er=(e,t,r,n,o,i,a,l)=>{let d=i;if(a){if(n===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return d=af(e,i,a,t,n,r*o,l),d=d.reshape([t,n,r,o]),e.compute(Re(d,ld.perm),{inputs:[d],outputs:[-1]})[0]}else return i.dims.length===3&&(d=i.reshape([t,n,r,o])),e.compute(Re(d,ld.perm),{inputs:[d],outputs:[-1]})[0]},pd=(e,t)=>{let r=of(e.inputs,t),n=e.inputs[0],o=Me(e.inputs,1),i=Me(e.inputs,2),a=Me(e.inputs,3),l=Me(e.inputs,4),d=Me(e.inputs,5),c=Me(e.inputs,6),m=Me(e.inputs,7);if(n.dims.length===5)throw new Error("Packed QKV is not implemented");if(o?.dims.length===5)throw new Error("Packed KV is not implemented");let u=o&&i&&o.dims.length===4&&i.dims.length===4,h=er(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,n,a,0);if(u)return Ut(e,h,o,i,l,void 0,c,m,d,r,t);if(!o||!i)throw new Error("key and value must be provided");let w=er(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,o,a,r.hiddenSize),g=er(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,i,a,2*r.hiddenSize);Ut(e,h,w,g,l,void 0,c,m,d,r,t)}});var md,sf,uf,wo,fd,vo=M(()=>{"use strict";X();te();ne();md=e=>Array.from(e.getBigInt64Array(),Number),sf=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(md(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},uf=(e,t)=>{let r=[];for(let n=0;n<e.length;++n)r.push(e[n]*t[n]);return r},wo=(e,t)=>{let r=e[0].dims,n=t??md(e[1]),o=uf(r,n),i=k.size(o),a=e[0].dataType,l=E("input",a,r.length),d=D("output",a,o.length),c=m=>`
|
|
5327
5327
|
const inputShape = ${l.indices(...r)};
|
|
5328
5328
|
${m.registerUniform("output_size","u32").declareVariables(l,d)}
|
|
5329
5329
|
${m.mainStart()}
|
|
@@ -5337,7 +5337,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5337
5337
|
${l.indicesSet("input_indices","i","input_dim_value")}
|
|
5338
5338
|
}
|
|
5339
5339
|
${d.setByOffset("global_idx",l.getByIndices("input_indices"))}
|
|
5340
|
-
}`;return{name:"Tile",shaderCache:{hint:`${n}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...U(e[0].dims,o)]}),getShaderSource:
|
|
5340
|
+
}`;return{name:"Tile",shaderCache:{hint:`${n}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...U(e[0].dims,o)]}),getShaderSource:c}},fd=e=>{sf(e.inputs),e.compute(wo(e.inputs),{inputs:[0]})}});var df,hd,yd,lf,gd,bd,wd=M(()=>{"use strict";X();te();xe();Gr();ne();bo();vo();Ct();df=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4];if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let l=!1,d=r.dims[0],c=r.dims[1],m=r.dims.length===3?l?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],u=c,h=0,w=0,g=Math.floor(m/t.numHeads),y=i&&i.dims.length!==0,x=a&&a.dims.length!==0,_=!0;if(y&&x){if(i.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(a.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');_?(h=i.dims[1],w=i.dims[1]):(h=i.dims[2],w=i.dims[2])}else if(y||x)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $;if(n){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(r.dims[2]%n.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');$=2,u=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==g)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');$=5,u=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==g)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');$=0,u=n.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}let S=0,I=!1,T=m;if(o){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(u!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');T=o.dims[2]}else{if(u!==o.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');T=o.dims[1]*o.dims[3],I=!0}}let A=h+u,O=!1;return{batchSize:d,sequenceLength:c,pastSequenceLength:h,kvSequenceLength:u,totalSequenceLength:A,maxSequenceLength:w,inputHiddenSize:0,hiddenSize:m,vHiddenSize:T,headSize:g,vHeadSize:Math.floor(T/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:S,scale:t.scale,broadcastResPosBias:O,passPastInKv:I,qkvFormat:$,isPastkvBSNH:_}},hd=(e,t,r,n)=>{let o=[n.batchSize,n.totalSequenceLength,n.kvNumHeads,n.headSize],i=4,a=k.size(o)/i,l=n.totalSequenceLength,d=D("present_kv",r,o.length,i),c=E("new_kv",e.dataType,e.dims.length,i),m=t?E("past_kv",t.dataType,t.dims.length,i):void 0,u=Math.ceil(n.headSize/i),h={x:l,y:e.dims[0],z:1},w=t?["rank","rank"]:["rank"],g=[{type:12,data:a},{type:12,data:n.pastSequenceLength},{type:12,data:n.kvSequenceLength},{type:12,data:n.totalSequenceLength}],y=[c];m?(g.push(...U(e.dims),...U(t.dims),...U(o)),y.push(m)):g.push(...U(e.dims),...U(o));let x=[{name:"output_size",type:"u32"},{name:"past_seqlen",type:"u32"},{name:"new_seqlen",type:"u32"},{name:"present_seqlen",type:"u32"}],_=` let past_batch_stride = uniforms.past_seqlen * num_heads * H;
|
|
5341
5341
|
var past_head_stride = uniforms.past_seqlen * H;
|
|
5342
5342
|
if (is_bsnh) {
|
|
5343
5343
|
past_head_stride = H;
|
|
@@ -5383,7 +5383,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5383
5383
|
|
|
5384
5384
|
let out_offset = b * present_batch_stride + s * row_stride + n * present_head_stride + h;
|
|
5385
5385
|
${S}
|
|
5386
|
-
}`;return{name:"ConcatPastNew",shaderCache:{hint:`${n.kvNumHeads}${u}${!!t}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:o,dataType:r}],dispatchGroup:h,programUniforms:g}),getShaderSource:I}},yd=e=>Q({...e}),lf=Q({perm:[0,2,1,3]}),gd=(e,t,r,n,o)=>{let i=t,a=n.kvNumHeads,l=n.nReps;return t.dims.length===3&&n.kvSequenceLength!==0&&(i=t.reshape([n.batchSize,n.kvSequenceLength,a,n.headSize])),r?i=e.compute(hd(i,r,i.dataType,n),{inputs:[i,r],outputs:[n.isPastkvBSNH?o:-1]})[0]:i=e.compute(hd(i,void 0,i.dataType,n),{inputs:[i],outputs:[n.isPastkvBSNH?o:-1]})[0],l!==1&&(i=e.compute(wo([i],[1,1,1,l]),{inputs:[i],outputs:[-1]})[0],i=i.reshape([n.batchSize,n.totalSequenceLength,a*l,n.headSize])),e.compute(Re(i,lf.perm),{inputs:[i],outputs:[-1]})[0]},bd=(e,t)=>{let r=df(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let n=er(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,e.inputs[0],void 0,0),o=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,i=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,a=gd(e,e.inputs[1],o,r,1),l=gd(e,e.inputs[2],i,r,2);Ut(e,n,a,l,void 0,void 0,void 0,void 0,void 0,r,t)}});var cf,pf,mf,vd,$d=M(()=>{"use strict";X();
|
|
5386
|
+
}`;return{name:"ConcatPastNew",shaderCache:{hint:`${n.kvNumHeads}${u}${!!t}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:o,dataType:r}],dispatchGroup:h,programUniforms:g}),getShaderSource:I}},yd=e=>Q({...e}),lf=Q({perm:[0,2,1,3]}),gd=(e,t,r,n,o)=>{let i=t,a=n.kvNumHeads,l=n.nReps;return t.dims.length===3&&n.kvSequenceLength!==0&&(i=t.reshape([n.batchSize,n.kvSequenceLength,a,n.headSize])),r?i=e.compute(hd(i,r,i.dataType,n),{inputs:[i,r],outputs:[n.isPastkvBSNH?o:-1]})[0]:i=e.compute(hd(i,void 0,i.dataType,n),{inputs:[i],outputs:[n.isPastkvBSNH?o:-1]})[0],l!==1&&(i=e.compute(wo([i],[1,1,1,l]),{inputs:[i],outputs:[-1]})[0],i=i.reshape([n.batchSize,n.totalSequenceLength,a*l,n.headSize])),e.compute(Re(i,lf.perm),{inputs:[i],outputs:[-1]})[0]},bd=(e,t)=>{let r=df(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let n=er(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,e.inputs[0],void 0,0),o=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,i=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,a=gd(e,e.inputs[1],o,r,1),l=gd(e,e.inputs[2],i,r,2);Ut(e,n,a,l,void 0,void 0,void 0,void 0,void 0,r,t)}});var cf,pf,mf,vd,$d=M(()=>{"use strict";X();te();ne();cf=(e,t)=>{let r=e[0].dims,n=r,o=2,i=k.sizeToDimension(r,o),a=k.sizeFromDimension(r,o),l=he(a),d=a/l,c=[r[0],r[1],d],m=["rank","type","type"],u=[{type:12,data:a},{type:12,data:d}];u.push(...U(c,c));let h=w=>{let g=E("x",e[0].dataType,c.length,l),y=E("scale",e[1].dataType,e[1].dims),x=E("bias",e[2].dataType,e[2].dims),_=D("output",e[0].dataType,c.length,l),$=[g,y,x,_],S=g.type.value,I=l===1?"f32":`vec${l}<f32>`,T=64,A=[{name:"normSize",type:"u32"},{name:"normPackedSize",type:"u32"}];return`
|
|
5387
5387
|
var<workgroup> meanShared : f32;
|
|
5388
5388
|
var<workgroup> squaredNormShared : f32;
|
|
5389
5389
|
var<workgroup> workgroupShared : array<${I}, ${T}>;
|
|
@@ -5443,15 +5443,15 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5443
5443
|
let value = ${g.get("batch","channel","h")} * ${S}(${I}(channelScale)) + ${S}(${I}(channelShift));
|
|
5444
5444
|
${_.set("batch","channel","h","value")};
|
|
5445
5445
|
}
|
|
5446
|
-
}`};return{name:"InstanceNormalization",shaderCache:{hint:`${t.epsilon};${l}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:i},programUniforms:u}),getShaderSource:h}},pf=(e,t,r,n,o,i,a,l)=>{let d=he(a),
|
|
5446
|
+
}`};return{name:"InstanceNormalization",shaderCache:{hint:`${t.epsilon};${l}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:i},programUniforms:u}),getShaderSource:h}},pf=(e,t,r,n,o,i,a,l)=>{let d=he(a),c=64,m=d===1?"vec2f":`mat2x${d}f`,u=d===1?"f32":`vec${d}f`,h=(A,O)=>`${m}(${A}, ${O})`,w=o*a/d,g=Math.ceil(i/c),y=["type"],x=[{type:12,data:g},{type:12,data:i},{type:12,data:Math.floor(a/d)},{type:12,data:Math.floor(i*a/d)}],_=A=>{let O=E("input",t.dataType,t.dims,d);return`
|
|
5447
5447
|
${A.declareVariables(O)}
|
|
5448
5448
|
@group(0) @binding(1) var<storage, read_write> output : array<${m}>;
|
|
5449
5449
|
struct Uniforms {wg_size:u32, H:u32, C:u32, image_size:u32};
|
|
5450
5450
|
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
|
|
5451
5451
|
|
|
5452
|
-
${A.mainStart(
|
|
5453
|
-
let currentImageNumber = global_idx / ${
|
|
5454
|
-
let currentChannelNumber = (global_idx / ${
|
|
5452
|
+
${A.mainStart(c)}
|
|
5453
|
+
let currentImageNumber = global_idx / ${c} / uniforms.C;
|
|
5454
|
+
let currentChannelNumber = (global_idx / ${c}) % uniforms.C;
|
|
5455
5455
|
let wgOffset = local_id.x * uniforms.wg_size;
|
|
5456
5456
|
if (wgOffset >= uniforms.H) {
|
|
5457
5457
|
return;
|
|
@@ -5467,7 +5467,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5467
5467
|
squaredSum += value * value;
|
|
5468
5468
|
}
|
|
5469
5469
|
output[global_idx] = ${h("sum","squaredSum")};
|
|
5470
|
-
}`},$=e.compute({name:"InstanceNormComputeMean",shaderCache:{hint:`${d}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:[o,a,
|
|
5470
|
+
}`},$=e.compute({name:"InstanceNormComputeMean",shaderCache:{hint:`${d}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:[o,a,c,2],dataType:1}],dispatchGroup:{x:o*a/d},programUniforms:x}),getShaderSource:_},{inputs:[t],outputs:[-1]})[0],S=[{type:12,data:w},{type:12,data:i},{type:12,data:Math.floor(a/d)},{type:12,data:Math.floor(c*a/d)}],I=["type","type","type"],T=A=>{let O=E("scale",r.dataType,r.dims,d),z=E("bias",n.dataType,n.dims,d);return`
|
|
5471
5471
|
@group(0) @binding(0) var<storage, read> input : array<${m}>;
|
|
5472
5472
|
@group(0) @binding(1) var<storage, read> scale : array<${O.type.storage}>;
|
|
5473
5473
|
@group(0) @binding(2) var<storage, read> bias : array<${z.type.storage}>;
|
|
@@ -5483,8 +5483,8 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5483
5483
|
let offset = currentImageNumber * uniforms.image_size;
|
|
5484
5484
|
var sum = ${ft("f32",d)};
|
|
5485
5485
|
var squaredSum = ${ft("f32",d)};
|
|
5486
|
-
for (var i: u32 = 0; i < min(${
|
|
5487
|
-
let value = input[offset + i + currentChannelNumber * ${
|
|
5486
|
+
for (var i: u32 = 0; i < min(${c}, uniforms.H); i++) {
|
|
5487
|
+
let value = input[offset + i + currentChannelNumber * ${c}];
|
|
5488
5488
|
sum += value[0];
|
|
5489
5489
|
squaredSum += value[1];
|
|
5490
5490
|
}
|
|
@@ -5495,7 +5495,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5495
5495
|
let channelShift = ${u}(bias[currentChannelNumber]) - sum * channelScale;
|
|
5496
5496
|
|
|
5497
5497
|
output[global_idx] = ${h("channelScale","channelShift")};
|
|
5498
|
-
}`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${d};${l}`,inputDependencies:I},getRunData:()=>({outputs:[{dims:[o,a,2],dataType:1}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:S}),getShaderSource:T},{inputs:[$,r,n],outputs:[-1]})[0]},mf=(e,t,r)=>{let n=t[0].dims,o=n,i=n[0],a=n[n.length-1],l=
|
|
5498
|
+
}`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${d};${l}`,inputDependencies:I},getRunData:()=>({outputs:[{dims:[o,a,2],dataType:1}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:S}),getShaderSource:T},{inputs:[$,r,n],outputs:[-1]})[0]},mf=(e,t,r)=>{let n=t[0].dims,o=n,i=n[0],a=n[n.length-1],l=k.sizeFromDimension(n,1)/a,d=he(a),c=k.size(o)/d,m=[{type:12,data:l},{type:12,data:Math.floor(a/d)}],u=["type","type"],h=pf(e,t[0],t[1],t[2],i,l,a,r.epsilon),w=g=>{let y=pe(t[0].dataType),x=d===1?"vec2f":`mat2x${d}f`,_=d===1?y:`vec${d}<${y}>`,$=E("input",t[0].dataType,t[0].dims,d),S=D("output",t[0].dataType,o,d);return`
|
|
5499
5499
|
@group(0) @binding(0) var<storage, read> input : array<${$.type.storage}>;
|
|
5500
5500
|
@group(0) @binding(1) var<storage, read> scaleInput : array<${x}>;
|
|
5501
5501
|
@group(0) @binding(2) var<storage, read_write> output : array<${S.type.storage}>;
|
|
@@ -5509,9 +5509,9 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5509
5509
|
let scaleOffset = currentImageNumber * uniforms.C + currentChannelNumber;
|
|
5510
5510
|
let scale = scaleInput[scaleOffset];
|
|
5511
5511
|
output[global_idx] = fma(input[global_idx], ${_}(scale[0]), ${_}(scale[1]));
|
|
5512
|
-
}`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${d}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(
|
|
5512
|
+
}`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${d}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:m}),getShaderSource:w},{inputs:[t[0],h]})},vd=(e,t)=>{t.format==="NHWC"?mf(e,e.inputs,t):e.compute(cf(e.inputs,t))}});var ff,hf,_d,xd=M(()=>{"use strict";X();te();ne();ff=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},hf=(e,t,r)=>{let n=t.simplified,o=e[0].dims,i=e[1],a=!n&&e[2],l=o,d=k.normalizeAxis(t.axis,o.length),c=k.sizeToDimension(o,d),m=k.sizeFromDimension(o,d),u=k.size(i.dims),h=a?k.size(a.dims):0;if(u!==m||a&&h!==m)throw new Error(`Size of X.shape()[axis:] == ${m}.
|
|
5513
5513
|
Size of scale and bias (if provided) must match this.
|
|
5514
|
-
Got scale size of ${u} and bias size of ${h}`);let w=[];for(let T=0;T<o.length;++T)T<d?w.push(o[T]):w.push(1);let g=he(m),y=["type","type"],x=[{type:12,data:
|
|
5514
|
+
Got scale size of ${u} and bias size of ${h}`);let w=[];for(let T=0;T<o.length;++T)T<d?w.push(o[T]):w.push(1);let g=he(m),y=["type","type"],x=[{type:12,data:c},{type:1,data:m},{type:12,data:Math.floor(m/g)},{type:1,data:t.epsilon}];a&&y.push("type");let _=r>1,$=r>2,S=T=>{let A=pe(e[0].dataType),O=[E("x",e[0].dataType,e[0].dims,g),E("scale",i.dataType,i.dims,g)];a&&O.push(E("bias",a.dataType,a.dims,g)),O.push(D("output",e[0].dataType,l,g)),_&&O.push(D("mean_data_output",1,w)),$&&O.push(D("inv_std_output",1,w));let z=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return`
|
|
5515
5515
|
${T.registerUniforms(z).declareVariables(...O)}
|
|
5516
5516
|
${T.mainStart()}
|
|
5517
5517
|
${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")}
|
|
@@ -5537,7 +5537,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5537
5537
|
|
|
5538
5538
|
${_?"mean_data_output[global_idx] = mean":""};
|
|
5539
5539
|
${$?"inv_std_output[global_idx] = inv_std_dev":""};
|
|
5540
|
-
}`},I=[{dims:l,dataType:e[0].dataType}];return _&&I.push({dims:w,dataType:1}),$&&I.push({dims:w,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${g};${r};${n}`,inputDependencies:y},getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(
|
|
5540
|
+
}`},I=[{dims:l,dataType:e[0].dataType}];return _&&I.push({dims:w,dataType:1}),$&&I.push({dims:w,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${g};${r};${n}`,inputDependencies:y},getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(c/64)},programUniforms:x}),getShaderSource:S}},_d=(e,t)=>{ff(e.inputs),e.compute(hf(e.inputs,t,e.outputCount))}});var gf,yf,Sd,Id,Cd=M(()=>{"use strict";X();te();xe();ne();gf=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],n=r.dims.length;if(r.dims[n-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let o=Math.floor((t.k+t.blockSize-1)/t.blockSize),i=t.blockSize/8*t.bits,a=e[1];if(!k.areEqual(a.dims,[t.n,o,i]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let d=e[2].dims;if(k.size(d)!==t.n*o)throw new Error("scales input size error.");if(e.length===4){let m=e[3].dims,u=t.bits>4?t.n*o:t.n*Math.floor((o+1)/2);if(k.size(m)!==u)throw new Error("zeroPoints input size error.")}},yf=(e,t,r,n)=>{let o=e[0].dims,i=o.length,a=Math.floor((t.k+t.blockSize-1)/t.blockSize),l=o[i-2],d=t.k,c=t.n,m=o.slice(0,i-2),u=k.size(m),w=t.blockSize/8*t.bits/4,g=e[0].dataType,y=he(l),x=he(t.k),_=he(w),$=_t(g,l*a),S=Math.floor(n/$),I=a<=r[0]&&S>0,T=!I||S>=4?he(c):S>=2&&he(c)>=2?2:1,A=m.concat([l,c]),O=k.size(A)/T/y,z=I?[]:[{type:12,data:O},{type:12,data:t.blockSize}],W=[u,l,d/x],V=k.convertShape(e[1].dims).slice();V.splice(-1,1,w/_),z.push(...U(W)),z.push(...U(V)),z.push(...U(e[2].dims)),e.length===4&&z.push(...U(k.convertShape(e[3].dims)));let j=[u,l,c/T];z.push(...U(j));let Y=ae=>{let se=W.length,ie=E("a",e[0].dataType,se,x),q=E("b",12,V.length,_),ce=E("scales",e[2].dataType,e[2].dims.length),oe=[ie,q,ce],Z=e.length===4?E("zero_points",12,e[3].dims.length):void 0;Z&&oe.push(Z);let ee=j.length,K=D("output",e[0].dataType,ee,T),me=[{name:"output_size",type:"u32"},{name:"block_size",type:"u32"}],$e=pe(e[0].dataType),R=(()=>{switch(x){case 1:return`array<${$e}, 8>`;case 2:return`mat4x2<${$e}>`;case 4:return`mat2x4<${$e}>`;default:throw new Error(`${x}-component is not supported.`)}})(),G=`
|
|
5541
5541
|
for (var word: u32 = 0; word < ${w}; word += ${_}) {
|
|
5542
5542
|
${q.indicesSet("b_indices","2","word")};
|
|
5543
5543
|
let b_data = ${q.getByIndices("b_indices")};
|
|
@@ -5570,7 +5570,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5570
5570
|
zero_point_word = ${Z.getByOffset("zero_point_index")};
|
|
5571
5571
|
}`:"";return I?`
|
|
5572
5572
|
var<workgroup> workgroup_shared: array<${K.type.value}, ${l*a}>;
|
|
5573
|
-
${ae.declareVariables(...
|
|
5573
|
+
${ae.declareVariables(...oe,K)}
|
|
5574
5574
|
${ae.mainStart([a,1,1])}
|
|
5575
5575
|
var a_indices: ${ie.type.indices};
|
|
5576
5576
|
var block = local_id.x;
|
|
@@ -5617,11 +5617,11 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5617
5617
|
workgroup_shared_offset += ${l};
|
|
5618
5618
|
}
|
|
5619
5619
|
${K.setByOffset("output_offset","output_value")};
|
|
5620
|
-
output_offset += ${
|
|
5620
|
+
output_offset += ${c/T};
|
|
5621
5621
|
}
|
|
5622
5622
|
}
|
|
5623
5623
|
}`:`
|
|
5624
|
-
${ae.registerUniforms(me).declareVariables(...
|
|
5624
|
+
${ae.registerUniforms(me).declareVariables(...oe,K)}
|
|
5625
5625
|
${ae.mainStart()}
|
|
5626
5626
|
${ae.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
5627
5627
|
var output_values: array<${K.type.value}, ${y}>;
|
|
@@ -5663,7 +5663,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5663
5663
|
${K.indicesSet("output_indices",ee-2,`${y} * row + k`)};
|
|
5664
5664
|
${K.setByIndices("output_indices","output_values[k]")}
|
|
5665
5665
|
}
|
|
5666
|
-
}`};return{name:I?"BlockwiseMatMulNBits":"MatMulNBits",shaderCache:{hint:`${t.cacheKey};${l};${g};${e.length}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:A,dataType:g}],name:I?"BlockwiseMatMulNBits":"MatMulNBits",dispatchGroup:I?{x:1,y:Math.ceil(
|
|
5666
|
+
}`};return{name:I?"BlockwiseMatMulNBits":"MatMulNBits",shaderCache:{hint:`${t.cacheKey};${l};${g};${e.length}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:A,dataType:g}],name:I?"BlockwiseMatMulNBits":"MatMulNBits",dispatchGroup:I?{x:1,y:Math.ceil(c/T),z:u}:{x:Math.ceil(O/64)},programUniforms:z}),getShaderSource:Y}},Sd=(e,t)=>{gf(e.inputs,t);let r=e.getMaxComputeWorkgroupSizes(),n=e.getMaxComputeWorkgroupStoragesize();e.compute(yf(e.inputs,t,r,n))},Id=e=>Q(e)});var bf,wf,vf,$f,_f,xf,Sf,If,Td,Ad=M(()=>{"use strict";X();te();ne();bf=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},wf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=`
|
|
5667
5667
|
k = i32(${e.indicesGet("indices",o)}) - ${L("uniforms.pads",o,r)};
|
|
5668
5668
|
if (k < 0) {
|
|
5669
5669
|
break;
|
|
@@ -5726,17 +5726,17 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5726
5726
|
var k = 0;
|
|
5727
5727
|
${n}
|
|
5728
5728
|
value = x[offset];
|
|
5729
|
-
`},xf=(e,t,r)=>{switch(r.mode){case 0:return wf(e,t,r.pads.length);case 1:return vf(e,t,r.pads.length);case 2:return $f(e,t,r.pads.length);case 3:return _f(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Sf=(e,t)=>{let r=
|
|
5730
|
-
${d.registerUniforms(w).declareVariables(m,
|
|
5729
|
+
`},xf=(e,t,r)=>{switch(r.mode){case 0:return wf(e,t,r.pads.length);case 1:return vf(e,t,r.pads.length);case 2:return $f(e,t,r.pads.length);case 3:return _f(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Sf=(e,t)=>{let r=k.padShape(e[0].dims.slice(),t.pads),n=e[0].dims,o=k.size(r),i=[{type:12,data:o},{type:6,data:t.pads}];t.mode===0&&i.push({type:e[0].dataType,data:t.value}),i.push(...U(e[0].dims,r));let a=["rank"],l=d=>{let c=D("output",e[0].dataType,r.length),m=E("x",e[0].dataType,n.length),u=m.type.value,h=xf(c,n.length,t),w=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&w.push({name:"constant_value",type:u}),`
|
|
5730
|
+
${d.registerUniforms(w).declareVariables(m,c)}
|
|
5731
5731
|
${d.mainStart()}
|
|
5732
5732
|
${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
5733
5733
|
|
|
5734
|
-
let indices = ${
|
|
5734
|
+
let indices = ${c.offsetToIndices("global_idx")};
|
|
5735
5735
|
|
|
5736
5736
|
var value = ${u}(0);
|
|
5737
5737
|
${h}
|
|
5738
5738
|
output[global_idx] = value;
|
|
5739
|
-
}`};return{name:"Pad",shaderCache:{hint:`${t.mode}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(
|
|
5739
|
+
}`};return{name:"Pad",shaderCache:{hint:`${t.mode}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(r)/64)},programUniforms:i}),getShaderSource:l}},If=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),n=e.length>=3&&e[2].data?e[2].getFloat32Array()[0]:0,o=e[0].dims.length,i=new Int32Array(2*o).fill(0);if(e.length>=4){let l=e[3].getBigInt64Array();for(let d=0;d<l.length;d++)i[Number(l[d])]=Number(r[d]),i[Number(l[d])+o]=Number(r[d+l.length])}else r.forEach((l,d)=>i[Number(d)]=Number(l));let a=[];return i.forEach(l=>a.push(l)),{mode:t.mode,value:n,pads:a}}else return t},Td=(e,t)=>{bf(e.inputs);let r=If(e.inputs,t);e.compute(Sf(e.inputs,r),{inputs:[0]})}});var en,kd,Ed,Pd,Od,Cf,Tf,zd,Bd,Dd,Rd,Md,Ud,Vd,Nd,Wd,Ld,Hd,Gd,Fd=M(()=>{"use strict";qe();X();te();ne();en=e=>{if(fe.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},kd=(e,t,r)=>{let n=t.format==="NHWC",o=e.dims.slice();n&&o.splice(1,0,o.pop());let i=Object.hasOwnProperty.call(t,"dilations"),a=t.kernelShape.slice(),l=t.strides.slice(),d=i?t.dilations.slice():[],c=t.pads.slice();xt.adjustPoolAttributes(r,o,a,l,d,c);let m=xt.computePoolOutputShape(r,o,l,d,a,c,t.autoPad),u=Object.assign({},t);i?Object.assign(u,{kernelShape:a,strides:l,pads:c,dilations:d,cacheKey:t.cacheKey}):Object.assign(u,{kernelShape:a,strides:l,pads:c,cacheKey:t.cacheKey});let h=m.slice();return h.push(h.splice(1,1)[0]),[u,n?h:m]},Ed=(e,t)=>{let r=t.format==="NHWC",n=k.size(e),o=k.size(t.kernelShape),i=[{type:12,data:n},{type:12,data:o}],a=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let l=t.kernelShape[t.kernelShape.length-1],d=t.strides[t.strides.length-1],c=t.pads[t.pads.length/2-1],m=t.pads[t.pads.length-1],u=!!(c+m);i.push({type:12,data:l},{type:12,data:d},{type:12,data:c},{type:12,data:m}),a.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let h=!1;if(t.kernelShape.length===2){let w=t.kernelShape[t.kernelShape.length-2],g=t.strides[t.strides.length-2],y=t.pads[t.pads.length/2-2],x=t.pads[t.pads.length-2];h=!!(y+x),i.push({type:12,data:w},{type:12,data:g},{type:12,data:y},{type:12,data:x}),a.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[i,a,!0,u,h]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let l=k.computeStrides(t.kernelShape);i.push({type:12,data:l},{type:12,data:t.pads},{type:12,data:t.strides}),a.push({name:"kernelStrides",type:"u32",length:l.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let d=t.pads.reduce((c,m)=>c+m);return[i,a,!!d,!1,!1]}},Pd=(e,t,r,n,o,i,a,l,d,c,m,u)=>{let h=o.format==="NHWC",w=t.type.value,g=D("output",t.type.tensor,n);if(o.kernelShape.length<=2){let y="",x="",_="",$=r-(h?2:1);if(m?y=`
|
|
5740
5740
|
for (var i: u32 = 0u; i < uniforms.kw; i++) {
|
|
5741
5741
|
xIndices[${$}] = indices[${$}] * uniforms.sw - uniforms.pwStart + i;
|
|
5742
5742
|
if (xIndices[${$}] < 0 || xIndices[${$}]
|
|
@@ -5780,7 +5780,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5780
5780
|
${a}
|
|
5781
5781
|
|
|
5782
5782
|
output[global_idx] = value;
|
|
5783
|
-
}`}else{if(h)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let y=o.kernelShape.length,x=o.pads.length,_="";return
|
|
5783
|
+
}`}else{if(h)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let y=o.kernelShape.length,x=o.pads.length,_="";return c?_=`
|
|
5784
5784
|
if (xIndices[j] >= uniforms.x_shape[j]) {
|
|
5785
5785
|
pad++;
|
|
5786
5786
|
isPad = true;
|
|
@@ -5825,9 +5825,9 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5825
5825
|
${a}
|
|
5826
5826
|
|
|
5827
5827
|
output[global_idx] = value;
|
|
5828
|
-
}`}},Od=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Cf=e=>`${Od(e)};${e.countIncludePad}`,Tf=e=>`${Od(e)};${e.storageOrder};${e.dilations}`,zd=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Bd=(e,t,r,n)=>{let[o,i]=kd(t,n,r),a=
|
|
5828
|
+
}`}},Od=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Cf=e=>`${Od(e)};${e.countIncludePad}`,Tf=e=>`${Od(e)};${e.storageOrder};${e.dilations}`,zd=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Bd=(e,t,r,n)=>{let[o,i]=kd(t,n,r),a=E("x",t.dataType,t.dims.length),l=a.type.value,d="value += x_val;",c="";o.countIncludePad?c+=`value /= ${l}(uniforms.kernelSize);`:c+=`value /= ${l}(i32(uniforms.kernelSize) - pad);`;let[m,u,h,w,g]=Ed(i,o);m.push(...U(t.dims,i));let y=["rank"];return{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:m}),getShaderSource:x=>Pd(x,a,t.dims.length,i.length,o,d,c,0,u,h,w,g)}},Dd=e=>{let t=e.count_include_pad!==0,r=zd(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let n={countIncludePad:t,...r,cacheKey:""};return{...n,cacheKey:Cf(n)}},Rd=(e,t)=>{en(e.inputs),e.compute(Bd("AveragePool",e.inputs[0],!1,t))},Md={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},Ud=e=>{let t=e.format;return{format:t,...Md,cacheKey:t}},Vd=(e,t)=>{en(e.inputs),e.compute(Bd("GlobalAveragePool",e.inputs[0],!0,t))},Nd=(e,t,r,n)=>{let[o,i]=kd(t,n,r),a=`
|
|
5829
5829
|
value = max(x_val, value);
|
|
5830
|
-
`,l="",d=
|
|
5830
|
+
`,l="",d=E("x",t.dataType,t.dims.length),c=["rank"],[m,u,h,w,g]=Ed(i,o);return m.push(...U(t.dims,i)),{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:m}),getShaderSource:y=>Pd(y,d,t.dims.length,i.length,o,a,l,t.dataType===10?-65504:-1e5,u,h,w,g)}},Wd=(e,t)=>{en(e.inputs),e.compute(Nd("MaxPool",e.inputs[0],!1,t))},Ld=e=>{let t=e.storage_order,r=e.dilations,n=zd(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let o={storageOrder:t,dilations:r,...n,cacheKey:""};return{...o,cacheKey:Tf(o)}},Hd=e=>{let t=e.format;return{format:t,...Md,cacheKey:t}},Gd=(e,t)=>{en(e.inputs),e.compute(Nd("GlobalMaxPool",e.inputs[0],!0,t))}});var kf,Ef,qd,Kd,jd=M(()=>{"use strict";X();te();xe();ne();kf=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[0].dataType===6&&e.length>2)throw new Error("In the case of dequantizing int32 there is no zero point.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((r,n)=>r===e[2].dims[n]).reduce((r,n)=>r&&n,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((o,i)=>i===t.axis||o===e[0].dims[i]).reduce((o,i)=>o&&i,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let r=e[0].dims[t.axis],n=e[1].dims[t.axis];if(t.blockSize<Math.ceil(r/n)||t.blockSize>Math.ceil(r/(n-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},Ef=(e,t)=>{let r=k.normalizeAxis(t.axis,e[0].dims.length),n=e[0].dataType,o=n===3,i=e[0].dims,a=e[1].dataType,l=k.size(i),d=n===3||n===2,c=d?[Math.ceil(k.size(e[0].dims)/4)]:e[0].dims,m=e[1].dims,u=e.length>2?e[2]:void 0,h=u?d?[Math.ceil(k.size(u.dims)/4)]:u.dims:void 0,w=m.length===0||m.length===1&&m[0]===1,g=w===!1&&m.length===1,y=he(l),x=w&&(!d||y===4),_=x?y:1,$=x&&!d?y:1,S=E("input",d?12:n,c.length,$),I=E("scale",a,m.length),T=u?E("zero_point",d?12:n,h.length):void 0,A=D("output",a,i.length,_),O=[S,I];T&&O.push(T);let z=[c,m];u&&z.push(h);let W=[{type:12,data:l/_},{type:12,data:r},{type:12,data:t.blockSize},...U(...z,i)],V=j=>{let Y=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return`
|
|
5831
5831
|
${j.registerUniforms(Y).declareVariables(...O,A)}
|
|
5832
5832
|
${j.mainStart()}
|
|
5833
5833
|
${j.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
@@ -5865,13 +5865,13 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5865
5865
|
let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${T.getByIndices("scale_indices")};`:`let zero_point_value = ${d?o?"i32":"u32":S.type.value}(0);`)()};
|
|
5866
5866
|
// Compute and write output
|
|
5867
5867
|
${A.setByOffset("global_idx",`${A.type.value}(x_value - zero_point_value) * scale_value`)};
|
|
5868
|
-
}`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:T?["rank","rank","rank"]:["rank","rank"]},getShaderSource:V,getRunData:()=>({outputs:[{dims:i,dataType:a}],dispatchGroup:{x:Math.ceil(l/_/64),y:1,z:1},programUniforms:W})}},qd=(e,t)=>{kf(e.inputs,t),e.compute(Ef(e.inputs,t))},Kd=e=>Q({axis:e.axis,blockSize:e.blockSize})});var Pf,Of,Yd,Xd=M(()=>{"use strict";qe();X();
|
|
5869
|
-
${
|
|
5870
|
-
${
|
|
5871
|
-
${
|
|
5868
|
+
}`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:T?["rank","rank","rank"]:["rank","rank"]},getShaderSource:V,getRunData:()=>({outputs:[{dims:i,dataType:a}],dispatchGroup:{x:Math.ceil(l/_/64),y:1,z:1},programUniforms:W})}},qd=(e,t)=>{kf(e.inputs,t),e.compute(Ef(e.inputs,t))},Kd=e=>Q({axis:e.axis,blockSize:e.blockSize})});var Pf,Of,Yd,Xd=M(()=>{"use strict";qe();X();ne();Pf=(e,t,r)=>{let n=e===t,o=e<t&&r<0,i=e>t&&r>0;if(n||o||i)throw new Error("Range these inputs' contents are invalid.")},Of=(e,t,r,n)=>{let o=Math.abs(Math.ceil((t-e)/r)),i=[o],a=o,l=[{type:12,data:a},{type:n,data:e},{type:n,data:r},...U(i)],d=c=>{let m=D("output",n,i.length),u=m.type.value,h=[{name:"outputSize",type:"u32"},{name:"start",type:u},{name:"delta",type:u}];return`
|
|
5869
|
+
${c.registerUniforms(h).declareVariables(m)}
|
|
5870
|
+
${c.mainStart()}
|
|
5871
|
+
${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
5872
5872
|
output[global_idx] = uniforms.start + ${u}(global_idx) * uniforms.delta;
|
|
5873
|
-
}`};return{name:"Range",shaderCache:{hint:`${n}`},getShaderSource:d,getRunData:()=>({outputs:[{dims:i,dataType:n}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:l})}},Yd=e=>{let t=0,r=0,n=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],n=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],n=e.inputs[2].getFloat32Array()[0]),fe.webgpu.validateInputContent&&Pf(t,r,n),e.compute(Of(t,r,n,e.inputs[0].dataType),{inputs:[]})}});var zf,Bf,Df,Rf,Mf,Uf,Vf,Nf,Wf,Lf,Hf,Zd,Gf,Ff,qf,Kf,jf,Qd,Jd,el=M(()=>{"use strict";X();
|
|
5874
|
-
one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Bf=(e,t,r)=>{t.every(o=>o>=0&&o<r||(()=>{throw new Error("Resize requires axes input values to be positive and less than rank")}));let n=new Array(r).fill(1);return t.forEach((o,i)=>n[o]=e[i]),n},Df=(e,t,r,n,o,i)=>{let[a,l,d]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],
|
|
5873
|
+
}`};return{name:"Range",shaderCache:{hint:`${n}`},getShaderSource:d,getRunData:()=>({outputs:[{dims:i,dataType:n}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:l})}},Yd=e=>{let t=0,r=0,n=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],n=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],n=e.inputs[2].getFloat32Array()[0]),fe.webgpu.validateInputContent&&Pf(t,r,n),e.compute(Of(t,r,n,e.inputs[0].dataType),{inputs:[]})}});var zf,Bf,Df,Rf,Mf,Uf,Vf,Nf,Wf,Lf,Hf,Zd,Gf,Ff,qf,Kf,jf,Qd,Jd,el=M(()=>{"use strict";X();te();xe();ne();zf=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and
|
|
5874
|
+
one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Bf=(e,t,r)=>{t.every(o=>o>=0&&o<r||(()=>{throw new Error("Resize requires axes input values to be positive and less than rank")}));let n=new Array(r).fill(1);return t.forEach((o,i)=>n[o]=e[i]),n},Df=(e,t,r,n,o,i)=>{let[a,l,d]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],c=e[0].dims.length;if(a>0&&e.length>a&&e[a].dims.length>0)e[a].getFloat32Array().forEach(m=>i.push(m));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(l>0&&e.length>l&&e[l].dims.length>0){if(e[l].getFloat32Array().forEach(m=>n.push(m)),n.length!==0&&n.length!==c&&r>=18&&n.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");zf(n,t),t.axes.length>0&&Bf(n,t.axes,c).forEach((m,u)=>n[u]=m)}if(d>0&&e.length>d&&(e[d].getBigInt64Array().forEach(m=>o.push(Number(m))),o.length!==c||r>=18&&o.length===t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(n.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(o.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof n<"u"&&typeof o<"u"&&n.length>0&&o.length>c)throw new Error("Resize requires only of scales or sizes to be specified")},Rf=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32,
|
|
5875
5875
|
lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return`return ${t}(xResized) / ${t}(xScale);`;case"pytorch_half_pixel":return`if (lengthResized > 1) {
|
|
5876
5876
|
return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5;
|
|
5877
5877
|
} else {
|
|
@@ -5957,12 +5957,12 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5957
5957
|
}`,Zd=(e,t,r,n)=>e.rank>n?`
|
|
5958
5958
|
${e.indicesSet("input_indices",t,"channel")};
|
|
5959
5959
|
${e.indicesSet("input_indices",r,"batch")};
|
|
5960
|
-
`:"",Gf=(e,t,r,n,o)=>{let[a,l,d,
|
|
5960
|
+
`:"",Gf=(e,t,r,n,o)=>{let[a,l,d,c]=r.length===2?[-1,0,1,-1]:[0,2,3,1],m=e.type.value;return`
|
|
5961
5961
|
fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${m} {
|
|
5962
5962
|
var input_indices: ${e.type.indices};
|
|
5963
5963
|
${e.indicesSet("input_indices",l,`max(0, min(row, ${r[l]} - 1))`)};
|
|
5964
5964
|
${e.indicesSet("input_indices",d,`max(0, min(col, ${r[d]} - 1))`)};
|
|
5965
|
-
${Zd(e,
|
|
5965
|
+
${Zd(e,c,a,2)}
|
|
5966
5966
|
return ${e.getByIndices("input_indices")};
|
|
5967
5967
|
}
|
|
5968
5968
|
|
|
@@ -5979,7 +5979,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5979
5979
|
var col1: u32 = u32(col);
|
|
5980
5980
|
var row2: u32 = u32(row + 1);
|
|
5981
5981
|
var col2: u32 = u32(col + 1);
|
|
5982
|
-
var channel: u32 = ${r.length>2?`u32(originalIndices[${
|
|
5982
|
+
var channel: u32 = ${r.length>2?`u32(originalIndices[${c}])`:"0"};
|
|
5983
5983
|
var batch: u32 = ${r.length>2?`u32(originalIndices[${a}])`:"0"};
|
|
5984
5984
|
var x11: ${m} = getInputValue(batch, channel, row1, col1);
|
|
5985
5985
|
var x12: ${m} = getInputValue(batch, channel, row1, col2);
|
|
@@ -5998,7 +5998,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
5998
5998
|
dy2 = 0.5;
|
|
5999
5999
|
}
|
|
6000
6000
|
return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1);
|
|
6001
|
-
}`},Ff=(e,t,r,n,o,i,a,l,d,
|
|
6001
|
+
}`},Ff=(e,t,r,n,o,i,a,l,d,c)=>{let m=r.length===2,u=!0,[h,w]=m?[0,1]:u?[2,3]:[1,2],g=e.type.value,y=x=>{let _=x===h?"row":"col";return`
|
|
6002
6002
|
fn ${_}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${g} {
|
|
6003
6003
|
var output_index = ${t.indicesGet("output_indices",x)};
|
|
6004
6004
|
var originalIdx: ${g} = getOriginalCoordinateFromResizedCoordinate(output_index, ${o[x]},
|
|
@@ -6013,7 +6013,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6013
6013
|
for (var i: i32 = -1; i < 3; i++) {
|
|
6014
6014
|
var ${_}: ${g} = originalIdx + ${g}(i);
|
|
6015
6015
|
if (${_} < 0 || ${_} >= ${r[x]}) {
|
|
6016
|
-
${(()=>
|
|
6016
|
+
${(()=>c?`coefs[i + 1] = 0.0;
|
|
6017
6017
|
continue;`:l?`return ${d};`:`${_} = max(0, min(${_}, ${r[x]} - 1));`)()};
|
|
6018
6018
|
}
|
|
6019
6019
|
var input_indices_copy: ${e.type.indices} = input_indices;
|
|
@@ -6046,12 +6046,12 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6046
6046
|
var input_indices: ${e.type.indices} = output_indices;
|
|
6047
6047
|
return colCubicInterpolation(input_indices, output_indices);
|
|
6048
6048
|
}
|
|
6049
|
-
`},qf=(e,t,r,n,o)=>{let[a,l,d,
|
|
6049
|
+
`},qf=(e,t,r,n,o)=>{let[a,l,d,c,m]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],u=e.type.value;return`
|
|
6050
6050
|
fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${u} {
|
|
6051
6051
|
var input_indices: ${e.type.indices};
|
|
6052
6052
|
${e.indicesSet("input_indices",l,`max(0, min(depth, ${r[l]} - 1))`)};
|
|
6053
6053
|
${e.indicesSet("input_indices",d,`max(0, min(height, ${r[d]} - 1))`)};
|
|
6054
|
-
${e.indicesSet("input_indices",
|
|
6054
|
+
${e.indicesSet("input_indices",c,`max(0, min(width, ${r[c]} - 1))`)};
|
|
6055
6055
|
${Zd(e,m,a,3)}
|
|
6056
6056
|
return ${e.getByIndices("input_indices")};
|
|
6057
6057
|
}
|
|
@@ -6060,14 +6060,14 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6060
6060
|
var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);
|
|
6061
6061
|
var depth:${u} = originalIndices[${l}];
|
|
6062
6062
|
var height:${u} = originalIndices[${d}];
|
|
6063
|
-
var width:${u} = originalIndices[${
|
|
6064
|
-
${n?`if (depth < 0 || depth > (${r[l]} - 1) || height < 0 || height > (${r[d]} - 1) || width < 0 || (width > ${r[
|
|
6063
|
+
var width:${u} = originalIndices[${c}];
|
|
6064
|
+
${n?`if (depth < 0 || depth > (${r[l]} - 1) || height < 0 || height > (${r[d]} - 1) || width < 0 || (width > ${r[c]} - 1)) {
|
|
6065
6065
|
return ${o};
|
|
6066
6066
|
}`:""};
|
|
6067
6067
|
|
|
6068
6068
|
depth = max(0, min(depth, ${r[l]} - 1));
|
|
6069
6069
|
height = max(0, min(height, ${r[d]} - 1));
|
|
6070
|
-
width = max(0, min(width, ${r[
|
|
6070
|
+
width = max(0, min(width, ${r[c]} - 1));
|
|
6071
6071
|
var depth1: u32 = u32(depth);
|
|
6072
6072
|
var height1: u32 = u32(height);
|
|
6073
6073
|
var width1: u32 = u32(width);
|
|
@@ -6105,21 +6105,21 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6105
6105
|
}
|
|
6106
6106
|
return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 +
|
|
6107
6107
|
x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1);
|
|
6108
|
-
}`},Kf=(e,t,r,n,o,i)=>{let a=e.dims,l=Uf(i,t.axes,a.length),d=Vf(a,n,o,t.axes),
|
|
6108
|
+
}`},Kf=(e,t,r,n,o,i)=>{let a=e.dims,l=Uf(i,t.axes,a.length),d=Vf(a,n,o,t.axes),c=n.slice();n.length===0&&(c=a.map(($,S)=>$===0?1:d[S]/$),t.keepAspectRatioPolicy!=="stretch"&&(d=Nf(a,c,t)));let m=D("output",e.dataType,d.length),u=E("input",e.dataType,a.length),h=k.size(d),w=a.length===d.length&&a.every(($,S)=>$===d[S]),g=t.coordinateTransformMode==="tf_crop_and_resize",y=t.extrapolationValue,x=u.type.value,_=$=>`
|
|
6109
6109
|
${w?"":`
|
|
6110
6110
|
${Rf(t.coordinateTransformMode,x)};
|
|
6111
6111
|
${(()=>{switch(t.mode){case"nearest":return`
|
|
6112
6112
|
${Hf(u,a)};
|
|
6113
6113
|
${Mf(t.nearestMode,r,x)};
|
|
6114
|
-
${Lf(u,m,a,d,
|
|
6114
|
+
${Lf(u,m,a,d,c.length,l.length,g)};
|
|
6115
6115
|
`;case"linear":return`
|
|
6116
|
-
${Wf(m,a,d,
|
|
6116
|
+
${Wf(m,a,d,c.length,l.length)};
|
|
6117
6117
|
${(()=>{if(a.length===2||a.length===4)return`${Gf(u,m,a,g,y)}`;if(a.length===3||a.length===5)return`${qf(u,m,a,g,y)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()};
|
|
6118
6118
|
`;case"cubic":return`
|
|
6119
|
-
${(()=>{if(a.length===2||a.length===4)return`${Ff(u,m,a,d,
|
|
6119
|
+
${(()=>{if(a.length===2||a.length===4)return`${Ff(u,m,a,d,c,l,t.cubicCoeffA,g,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()};
|
|
6120
6120
|
`;default:throw Error("Invalid resize mode")}})()};
|
|
6121
6121
|
`}
|
|
6122
|
-
${$.registerUniform("output_size","u32").registerUniform("scales","f32",
|
|
6122
|
+
${$.registerUniform("output_size","u32").registerUniform("scales","f32",c.length).registerUniform("roi","f32",l.length).declareVariables(u,m)}
|
|
6123
6123
|
${$.mainStart()}
|
|
6124
6124
|
${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
6125
6125
|
${w?"output[global_idx] = input[global_idx];":`
|
|
@@ -6132,7 +6132,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6132
6132
|
output[global_idx] = ${t.extrapolationValue};
|
|
6133
6133
|
}`;case"linear":return`output[global_idx] = ${a.length===2||a.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()};
|
|
6134
6134
|
`}
|
|
6135
|
-
}`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${
|
|
6135
|
+
}`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${c.length>0?c:""}|${o.length>0?o:""}|${l.length>0?l:""}|${w}|${a}`,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:[{dims:d,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:[{type:12,data:h},{type:1,data:c},{type:1,data:l},...U(a,d)]})}},jf=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},Qd=(e,t)=>{let r=[],n=[],o=[],i=jf(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Df(e.inputs,t,i,r,n,o),e.compute(Kf(e.inputs[0],t,i,r,n,o),{inputs:[0]})},Jd=e=>{let t=e.antialias,r=e.axes,n=e.coordinateTransformMode,o=e.cubicCoeffA,i=e.excludeOutside!==0,a=e.extrapolationValue,l=e.keepAspectRatioPolicy,d=e.mode,c=e.nearestMode===""?"simple":e.nearestMode;return Q({antialias:t,axes:r,coordinateTransformMode:n,cubicCoeffA:o,excludeOutside:i,extrapolationValue:a,keepAspectRatioPolicy:l,mode:d,nearestMode:c})}});var Yf,Xf,tl,rl=M(()=>{"use strict";X();te();xe();ne();Yf=(e,t)=>{let[r,n,o,i]=e,{numHeads:a,rotaryEmbeddingDim:l}=t;if(r.dims.length!==3&&r.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`);if(!k.areEqual(n.dims,[])&&!k.areEqual(n.dims,[1])&&n.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${n.dims.length}`);if(o.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${o.dims.length}`);if(i.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${i.dims.length}`);if(!k.areEqual(o.dims,i.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(l>0&&a===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let d=r.dims[0],c=r.dims[r.dims.length-2],m=o.dims[0],u=k.sizeFromDimension(r.dims,1)/c,h=l===0?o.dims[1]*2:u/a;if(l>h)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(n.dims.length===2){if(d!==n.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${n.dims[0]}`);if(c!==n.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${n.dims[1]}`)}if(h/2!==o.dims[1]&&l/2!==o.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${o.dims[1]}`);if(c>m)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},Xf=(e,t)=>{let{interleaved:r,numHeads:n,rotaryEmbeddingDim:o,scale:i}=t,a=e[0].dims[0],l=k.sizeFromDimension(e[0].dims,1),d=e[0].dims[e[0].dims.length-2],c=l/d,m=e[2].dims[1],u=o===0?m*2:c/n,h=new Array(a,d,c/u,u-m),w=k.computeStrides(h),g=[{type:1,data:i},{type:12,data:h},{type:12,data:w},...e[0].dims.length===3?new Array({type:12,data:[l,c,u,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[l,u,d*u,1]}):[],...U(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],y=x=>{let _=E("input",e[0].dataType,e[0].dims.length),$=E("position_ids",e[1].dataType,e[1].dims.length),S=E("cos_cache",e[2].dataType,e[2].dims.length),I=E("sin_cache",e[3].dataType,e[3].dims.length),T=D("output",e[0].dataType,e[0].dims.length);return x.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:h.length},{name:"global_strides",type:"u32",length:w.length},{name:"input_output_strides",type:"u32",length:w.length}]),`
|
|
6136
6136
|
${x.declareVariables(_,$,S,I,T)}
|
|
6137
6137
|
|
|
6138
6138
|
${x.mainStart(St)}
|
|
@@ -6158,7 +6158,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6158
6158
|
let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim;
|
|
6159
6159
|
${T.setByOffset("k",_.getByOffset("k"))}
|
|
6160
6160
|
}
|
|
6161
|
-
}`};return{name:"RotaryEmbedding",shaderCache:{hint:Q({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(
|
|
6161
|
+
}`};return{name:"RotaryEmbedding",shaderCache:{hint:Q({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(h)/St)},programUniforms:g})}},tl=(e,t)=>{Yf(e.inputs,t),e.compute(Xf(e.inputs,t))}});var Zf,Qf,nl,ol=M(()=>{"use strict";X();te();ne();Zf=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],n=e[2];if(t.dataType!==r.dataType||t.dataType!==n.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let o=t.dims[t.dims.length-1],i=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==o)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==i)throw new Error("Skip must have the same sequence length as input");if(n.dims.length!==1)throw new Error("Gamma must be 1D");if(n.dims[n.dims.length-1]!==o)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let a=e[3];if(a.dims.length!==1)throw new Error("Beta must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let a=e[4];if(a.dims.length!==1)throw new Error("Bias must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Bias must have the same hidden size as input")}},Qf=(e,t,r,n)=>{let o=t.simplified,i=e[0].dims,a=k.size(i),l=i,d=a,c=i.slice(-1)[0],m=n?i.slice(0,-1).concat(1):[],u=!o&&e.length>3,h=e.length>4,w=n&&r>1,g=n&&r>2,y=r>3,x=64,_=he(c),$=[{type:12,data:d},{type:12,data:_},{type:12,data:c},{type:1,data:t.epsilon}],S=T=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],O=[E("x",e[0].dataType,e[0].dims,_),E("skip",e[1].dataType,e[1].dims,_),E("gamma",e[2].dataType,e[2].dims,_)];u&&O.push(E("beta",e[3].dataType,e[3].dims,_)),h&&O.push(E("bias",e[4].dataType,e[4].dims,_)),O.push(D("output",e[0].dataType,l,_)),w&&O.push(D("mean_output",1,m)),g&&O.push(D("inv_std_output",1,m)),y&&O.push(D("input_skip_bias_sum",e[0].dataType,l,_));let z=pe(e[0].dataType),W=pe(1,_);return`
|
|
6162
6162
|
|
|
6163
6163
|
${T.registerUniforms(A).declareVariables(...O)}
|
|
6164
6164
|
var<workgroup> sum_shared : array<${W}, ${x}>;
|
|
@@ -6210,7 +6210,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6210
6210
|
${z}(inv_std_dev) * gamma[offset1d + i]
|
|
6211
6211
|
${u?"+ beta[offset1d + i]":""};
|
|
6212
6212
|
}
|
|
6213
|
-
}`},I=[{dims:l,dataType:e[0].dataType}];return r>1&&I.push({dims:m,dataType:1}),r>2&&I.push({dims:m,dataType:1}),r>3&&I.push({dims:i,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${_};${w};${g};${y}`,inputDependencies:e.map((T,A)=>"type")},getShaderSource:S,getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(d/
|
|
6213
|
+
}`},I=[{dims:l,dataType:e[0].dataType}];return r>1&&I.push({dims:m,dataType:1}),r>2&&I.push({dims:m,dataType:1}),r>3&&I.push({dims:i,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${_};${w};${g};${y}`,inputDependencies:e.map((T,A)=>"type")},getShaderSource:S,getRunData:()=>({outputs:I,dispatchGroup:{x:Math.ceil(d/c)},programUniforms:$})}},nl=(e,t)=>{Zf(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(Qf(e.inputs,t,e.outputCount,!1),{outputs:n})}});var Jf,tn,eh,il,th,rh,al,sl,ul=M(()=>{"use strict";X();te();xe();ne();Jf=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,n)=>{if(e[n+1].dataType!==6&&e[n+1].dataType!==7)throw new Error(`Input ${n} must be an array of int32 or int64`)})},tn=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(n=>r.push(Number(n)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(n=>r.push(Number(n)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},eh=(e,t)=>{if(e.length>1){let r=tn(e,1),n=tn(e,2),o=tn(e,3);return o.length===0&&(o=[...Array(e[0].dims.length).keys()]),Q({starts:r,ends:n,axes:o})}else return t},il=(e,t,r,n,o)=>{let i=e;return e<0&&(i+=r[n[t]]),o[t]<0?Math.max(0,Math.min(i,r[n[t]]-1)):Math.max(0,Math.min(i,r[n[t]]))},th=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
|
|
6214
6214
|
var input_indices: ${e.type.indices};
|
|
6215
6215
|
var carry = 0u;
|
|
6216
6216
|
for (var i = ${r.length}; i >= 0; i--) {
|
|
@@ -6228,7 +6228,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6228
6228
|
${e.indicesSet("input_indices","i","input_index")};
|
|
6229
6229
|
}
|
|
6230
6230
|
return input_indices;
|
|
6231
|
-
}`,rh=(e,t)=>{let r=e[0].dims,n=
|
|
6231
|
+
}`,rh=(e,t)=>{let r=e[0].dims,n=k.size(r),o=t.axes.length>0?k.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],i=tn(e,4);i.forEach(_=>_!==0||(()=>{throw new Error("step cannot be 0")})),i.length===0&&(i=Array(o.length).fill(1));let a=t.starts.map((_,$)=>il(_,$,r,o,i)),l=t.ends.map((_,$)=>il(_,$,r,o,i));if(o.length!==a.length||o.length!==l.length)throw new Error("start, ends and axes should have the same number of elements");if(o.length!==r.length)for(let _=0;_<r.length;++_)o.includes(_)||(a.splice(_,0,0),l.splice(_,0,r[_]),i.splice(_,0,1));let d=i.map(_=>Math.sign(_));i.forEach((_,$,S)=>{if(_<0){let I=(l[$]-a[$])/_,T=a[$],A=T+I*i[$];a[$]=A,l[$]=T,S[$]=-_}});let c=r.slice(0);o.forEach((_,$)=>{c[_]=Math.ceil((l[_]-a[_])/i[_])});let m={dims:c,dataType:e[0].dataType},u=D("output",e[0].dataType,c.length),h=E("input",e[0].dataType,e[0].dims.length),w=k.size(c),g=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:a.length},{name:"signs",type:"i32",length:d.length},{name:"steps",type:"u32",length:i.length}],y=[{type:12,data:w},{type:12,data:a},{type:6,data:d},{type:12,data:i},...U(e[0].dims,c)],x=_=>`
|
|
6232
6232
|
${_.registerUniforms(g).declareVariables(h,u)}
|
|
6233
6233
|
${th(h,u,r)}
|
|
6234
6234
|
${_.mainStart()}
|
|
@@ -6236,7 +6236,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6236
6236
|
let output_indices = ${u.offsetToIndices("global_idx")};
|
|
6237
6237
|
let input_indices = calculateInputIndices(output_indices);
|
|
6238
6238
|
${u.setByOffset("global_idx",h.getByIndices("input_indices"))}
|
|
6239
|
-
}`;return{name:"Slice",shaderCache:{hint:`${d.length}_${a.length}_${i.length}`,inputDependencies:["rank"]},getShaderSource:x,getRunData:()=>({outputs:[m],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:y})}},al=(e,t)=>{Jf(e.inputs,t);let r=eh(e.inputs,t);e.compute(rh(e.inputs,r),{inputs:[0]})},sl=e=>{let t=e.starts,r=e.ends,n=e.axes;return Q({starts:t,ends:r,axes:n})}});var nh,oh,dl,ll,cl=M(()=>{"use strict";X();
|
|
6239
|
+
}`;return{name:"Slice",shaderCache:{hint:`${d.length}_${a.length}_${i.length}`,inputDependencies:["rank"]},getShaderSource:x,getRunData:()=>({outputs:[m],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:y})}},al=(e,t)=>{Jf(e.inputs,t);let r=eh(e.inputs,t);e.compute(rh(e.inputs,r),{inputs:[0]})},sl=e=>{let t=e.starts,r=e.ends,n=e.axes;return Q({starts:t,ends:r,axes:n})}});var nh,oh,dl,ll,cl=M(()=>{"use strict";X();te();xe();ne();nh=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},oh=(e,t)=>{let r=e.dims,n=k.size(r),o=64,i=t.axis;if(i<0&&(i=r.length+i),i<r.length-1)throw new Error("softmax only supports last axis for now.");let a=r[i],l=n/a,d=he(a),c=a/d,m=(x,_)=>_===4?`max(max(${x}.x, ${x}.y), max(${x}.z, ${x}.w))`:_===2?`max(${x}.x, ${x}.y)`:_===3?`max(max(${x}.x, ${x}.y), ${x}.z)`:x,u=E("x",e.dataType,e.dims,d),h=D("result",e.dataType,e.dims,d),w=u.type.value,g=pe(e.dataType)==="f32"?`var threadMax = ${w}(-3.402823e+38f);`:`var threadMax = ${w}(-65504.0h);`,y=x=>`
|
|
6240
6240
|
var<workgroup> rowMaxShared : ${w};
|
|
6241
6241
|
var<workgroup> rowSumShared : ${w};
|
|
6242
6242
|
var<workgroup> threadShared : array<${w}, ${o}>;
|
|
@@ -6308,7 +6308,7 @@ ${$e}_indices[${ue}] = 0;`}),G+=`${$e}_indices[${me-2}] = 0u;
|
|
|
6308
6308
|
let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared;
|
|
6309
6309
|
setValue(row, col, row_stride, value);
|
|
6310
6310
|
}
|
|
6311
|
-
}`;return{name:"Softmax",shaderCache:{hint:`${d}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.dataType}],dispatchGroup:{x:l},programUniforms:[{type:6,data:
|
|
6311
|
+
}`;return{name:"Softmax",shaderCache:{hint:`${d}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.dataType}],dispatchGroup:{x:l},programUniforms:[{type:6,data:c}]}),getShaderSource:y}},dl=(e,t)=>{nh(e.inputs),e.compute(oh(e.inputs[0],t))},ll=e=>Q({axis:e.axis})});var ih,ah,sh,uh,dh,pl,ml,fl=M(()=>{"use strict";X();te();xe();ne();ih=e=>{if(!e||e.length<1)throw new Error("too few inputs")},ah=(e,t)=>{let r=[],n=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(o=>r.push(Number(o))),n=r.length),Q({numOutputs:n,axis:t.axis,splitSizes:r})},sh=e=>`
|
|
6312
6312
|
fn calculateOutputIndex(index: u32) -> u32 {
|
|
6313
6313
|
for (var i: u32 = 0u; i < ${e}u; i += 1u ) {
|
|
6314
6314
|
if (index < ${L("uniforms.size_in_split_axis","i",e)}) {
|
|
@@ -6320,7 +6320,7 @@ fn calculateOutputIndex(index: u32) -> u32 {
|
|
|
6320
6320
|
fn writeBufferData(output_number: u32, indices: ${e[0].type.indices}, global_idx: u32) {
|
|
6321
6321
|
${r.join(`
|
|
6322
6322
|
`)}
|
|
6323
|
-
}`},dh=(e,t)=>{let r=e[0].dims,n=
|
|
6323
|
+
}`},dh=(e,t)=>{let r=e[0].dims,n=k.size(r),o=e[0].dataType,i=k.normalizeAxis(t.axis,r.length),a=new Array(t.numOutputs),l=E("input",o,r.length),d=new Array(t.numOutputs),c=[],m=[],u=0,h=[{type:12,data:n}];for(let g=0;g<t.numOutputs;g++){u+=t.splitSizes[g],d[g]=u;let y=r.slice();y[i]=t.splitSizes[g],m.push(y),a[g]=D(`output${g}`,o,y.length),c.push({dims:m[g],dataType:e[0].dataType})}h.push({type:12,data:d},...U(r,...m));let w=g=>`
|
|
6324
6324
|
${g.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",d.length).declareVariables(l,...a)}
|
|
6325
6325
|
${sh(d.length)}
|
|
6326
6326
|
${uh(a)}
|
|
@@ -6336,7 +6336,7 @@ fn calculateOutputIndex(index: u32) -> u32 {
|
|
|
6336
6336
|
${l.indicesSet("indices",i,"index")};
|
|
6337
6337
|
}
|
|
6338
6338
|
writeBufferData(output_number, indices, global_idx);
|
|
6339
|
-
}`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:
|
|
6339
|
+
}`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:c,dispatchGroup:{x:Math.ceil(n/64)},programUniforms:h})}},pl=(e,t)=>{ih(e.inputs);let r=e.inputs.length===1?t:ah(e.inputs,t);e.compute(dh(e.inputs,r),{inputs:[0]})},ml=e=>{let t=e.axis,r=e.splitSizes,n=e.numOutputs<0?r.length:e.numOutputs;if(n!==r.length)throw new Error("numOutputs and splitSizes lengh must be equal");return Q({axis:t,numOutputs:n,splitSizes:r})}});var lh,ch,hl,gl=M(()=>{"use strict";X();te();ne();lh=(e,t,r,n,o)=>{let i=D("output_data",o,r.length,4),a=E("a_data",t[1].dataType,t[1].dims.length,4),l=E("b_data",t[2].dataType,t[2].dims.length,4),d=E("c_data",t[0].dataType,t[0].dims.length,4),c,m=(u,h,w)=>`select(${h}, ${u}, ${w})`;if(!n)c=i.setByOffset("global_idx",m(a.getByOffset("global_idx"),l.getByOffset("global_idx"),d.getByOffset("global_idx")));else{let u=(h,w,g="")=>{let y=`a_data[index_a${w}][component_a${w}]`,x=`b_data[index_b${w}][component_b${w}]`,_=`bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`;return`
|
|
6340
6340
|
let output_indices${w} = ${i.offsetToIndices(`global_idx * 4u + ${w}u`)};
|
|
6341
6341
|
let offset_a${w} = ${a.broadcastedIndicesToOffset(`output_indices${w}`,i)};
|
|
6342
6342
|
let offset_b${w} = ${l.broadcastedIndicesToOffset(`output_indices${w}`,i)};
|
|
@@ -6348,13 +6348,13 @@ fn calculateOutputIndex(index: u32) -> u32 {
|
|
|
6348
6348
|
let component_b${w} = offset_b${w} % 4u;
|
|
6349
6349
|
let component_c${w} = offset_c${w} % 4u;
|
|
6350
6350
|
${h}[${w}] = ${g}(${m(y,x,_)});
|
|
6351
|
-
`};o===9?
|
|
6351
|
+
`};o===9?c=`
|
|
6352
6352
|
var data = vec4<u32>(0);
|
|
6353
6353
|
${u("data",0,"u32")}
|
|
6354
6354
|
${u("data",1,"u32")}
|
|
6355
6355
|
${u("data",2,"u32")}
|
|
6356
6356
|
${u("data",3,"u32")}
|
|
6357
|
-
output_data[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:
|
|
6357
|
+
output_data[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:c=`
|
|
6358
6358
|
${u("output_data[global_idx]",0)}
|
|
6359
6359
|
${u("output_data[global_idx]",1)}
|
|
6360
6360
|
${u("output_data[global_idx]",2)}
|
|
@@ -6363,11 +6363,11 @@ fn calculateOutputIndex(index: u32) -> u32 {
|
|
|
6363
6363
|
${e.registerUniform("vec_size","u32").declareVariables(d,a,l,i)}
|
|
6364
6364
|
${e.mainStart()}
|
|
6365
6365
|
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
6366
|
-
${
|
|
6367
|
-
}`},ch=e=>{let t=e[1].dims,r=e[2].dims,n=e[0].dims,o=e[1].dataType,i=!(
|
|
6366
|
+
${c}
|
|
6367
|
+
}`},ch=e=>{let t=e[1].dims,r=e[2].dims,n=e[0].dims,o=e[1].dataType,i=!(k.areEqual(t,r)&&k.areEqual(r,n)),a=t,l=k.size(t);if(i){let c=Je.calcShape(Je.calcShape(t,r,!1),n,!1);if(!c)throw new Error("Can't perform where op on the given tensors");a=c,l=k.size(a)}let d=Math.ceil(l/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:c=>lh(c,e,a,i,o),getRunData:()=>({outputs:[{dims:a,dataType:o}],dispatchGroup:{x:Math.ceil(l/64/4)},programUniforms:[{type:12,data:d},...U(n,t,r,a)]})}},hl=e=>{e.compute(ch(e.inputs))}});var yl,bl=M(()=>{"use strict";gs();Gr();ws();$s();au();yu();vu();ao();Uu();Wu();Gu();Yu();Qu();ed();nd();ad();dd();wd();$d();xd();uo();Cd();bo();Ad();Fd();jd();Xd();Lr();el();rl();ol();ul();cl();fl();vo();Ct();qr();gl();yl=new Map([["Abs",[_s]],["Acos",[xs]],["Acosh",[Ss]],["Add",[su]],["ArgMax",[hs,to]],["ArgMin",[fs,to]],["Asin",[Is]],["Asinh",[Cs]],["Atan",[Ts]],["Atanh",[As]],["Attention",[ys]],["AveragePool",[Rd,Dd]],["BatchNormalization",[bs]],["BiasAdd",[vs]],["BiasSplitGelu",[iu]],["Cast",[Es,ks]],["Ceil",[Os]],["Clip",[Ps]],["Concat",[bu,wu]],["Conv",[mo,po]],["ConvTranspose",[Mu,Ru]],["Cos",[zs]],["Cosh",[Bs]],["CumSum",[Vu,Nu]],["DepthToSpace",[Lu,Hu]],["DequantizeLinear",[qd,Kd]],["Div",[uu]],["Einsum",[Ku,ju]],["Elu",[Ds,Yt]],["Equal",[du]],["Erf",[Rs]],["Exp",[Ms]],["Expand",[Zu]],["FastGelu",[Ju]],["Floor",[Us]],["FusedConv",[mo,po]],["Gather",[rd,td]],["GatherElements",[id,od]],["Gelu",[Vs]],["Gemm",[ud,sd]],["GlobalAveragePool",[Vd,Ud]],["GlobalMaxPool",[Gd,Hd]],["Greater",[mu]],["GreaterOrEqual",[hu]],["GroupQueryAttention",[bd,yd]],["HardSigmoid",[Ks,qs]],["InstanceNormalization",[vd]],["LayerNormalization",[_d]],["LeakyRelu",[Ns,Yt]],["Less",[fu]],["LessOrEqual",[gu]],["Log",[ru]],["MatMul",[Pu]],["MatMulNBits",[Sd,Id]],["MaxPool",[Wd,Ld]],["Mul",[lu]],["MultiHeadAttention",[pd,cd]],["Neg",[Ls]],["Not",[Ws]],["Pad",[Td]],["Pow",[cu]],["QuickGelu",[nu,Yt]],["Range",[Yd]],["Reciprocal",[Hs]],["ReduceMin",[us]],["ReduceMean",[ns]],["ReduceMax",[ss]],["ReduceSum",[ls]],["ReduceProd",[ds]],["ReduceL1",[os]],["ReduceL2",[is]],["ReduceLogSum",[ps]],["ReduceLogSumExp",[as]],["ReduceSumSquare",[cs]],["Relu",[Gs]],["Resize",[Qd,Jd]],["RotaryEmbedding",[tl]],["Sigmoid",[Fs]],["Sin",[js]],["Sinh",[Ys]],["Slice",[al,sl]],["SkipLayerNormalization",[nl]],["Split",[pl,ml]],["Sqrt",[Xs]],["Softmax",[dl,ll]],["Sub",[pu]],["Tan",[Zs]],["Tanh",[Js]],["ThresholdedRelu",[tu,Yt]],["Tile",[fd]],["Transpose",[Ga,Fa]],["Where",[hl]]])});var rn,wl=M(()=>{"use strict";qe();st();ne();rn=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,n,o,i){Ve(t.programInfo.name);let a=this.backend.device,l=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let d=[];for(let m of r)d.push({binding:d.length,resource:{buffer:m.buffer}});for(let m of n)d.push({binding:d.length,resource:{buffer:m.buffer}});i&&d.push({binding:d.length,resource:i});let c=a.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:d,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let m={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:c,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(m)}l.setPipeline(t.computePipeline),l.setBindGroup(0,c),l.dispatchWorkgroups(...o),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),De(t.programInfo.name)}dispose(){}build(t,r){Ve(t.name);let n=this.backend.device,o=[];n.features.has("shader-f16")&&o.push("enable f16;");let i=La(r,this.backend.device.limits),a=t.getShaderSource(i),l=`${o.join(`
|
|
6368
6368
|
`)}
|
|
6369
6369
|
${i.additionalImplementations}
|
|
6370
|
-
${a}`,d=n.createShaderModule({code:l,label:t.name});be("verbose",()=>`[WebGPU] ${t.name} shader code: ${l}`);let p=n.createComputePipeline({compute:{module:d,entryPoint:"main"},layout:"auto",label:t.name});return De(t.name),{programInfo:t,computePipeline:p,uniformVariablesInfo:i.variablesInfo}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,n=typeof t=="number"?1:t.y||1,o=typeof t=="number"?1:t.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=i&&n<=i&&o<=i)return[r,n,o];let a=r*n*o,l=Math.ceil(Math.sqrt(a));if(l>i){if(l=Math.ceil(Math.cbrt(a)),l>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[l,l,l]}else return[l,l,1]}}});var ph,mh,$o,nn,vl=M(()=>{"use strict";qe();X();st();Ra();Wa();bl();wl();ph=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let n=0;n<e.length;++n){let o=e[n].dataType;switch(t[n]){case"none":{r.push("");break}case"type":{r.push(`${o}`);break}case"rank":{let i=e[n].dims.length;r.push(`${o};${i}`);break}case"dims":{let i=e[n].dims.join(",");r.push(`${o};${i}`);break}default:throw new Error(`unsupported input dependency: ${t[n]}`)}}return r.join("|")},mh=(e,t,r)=>{let n=e.name;return e.shaderCache?.hint&&(n+="["+e.shaderCache.hint+"]"),n+=":"+r+`:${ph(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,n},$o=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},nn=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n};r.features.has("chromium-experimental-timestamp-query-inside-passes")?n.push("chromium-experimental-timestamp-query-inside-passes"):r.features.has("timestamp-query")&&n.push("timestamp-query"),r.features.has("shader-f16")&&n.push("shader-f16"),this.device=await r.requestDevice(o),this.adapterInfo=new $o(r.info||await r.requestAdapterInfo()),this.gpuDataManager=Na(this),this.programManager=new rn(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Ba(t.logLevel,!!t.debug),this.device.onuncapturederror=i=>{i.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${i.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Ve(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),n=this.pendingQueries.get(t);for(let o=0;o<r.length/2;o++){let i=n[o],a=i.kernelId,l=this.kernels.get(a),d=l.kernelType,p=l.kernelName,m=i.programName,u=i.inputTensorViews,h=i.outputTensorViews,w=r[o*2],g=r[o*2+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=w);let y=Number(w-this.queryTimeBase),x=Number(g-this.queryTimeBase);if(!Number.isSafeInteger(y)||!Number.isSafeInteger(x))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:u.map(_=>({dims:_.dims,dataType:mt(_.dataType)})),outputsMetadata:h.map(_=>({dims:_.dims,dataType:mt(_.dataType)})),kernelId:a,kernelType:d,kernelName:p,programName:m,startTime:y,endTime:x});else{let _="";u.forEach((S,I)=>{_+=`input[${I}]: [${S.dims}] | ${mt(S.dataType)}, `});let $="";h.forEach((S,I)=>{$+=`output[${I}]: [${S.dims}] | ${mt(S.dataType)}, `}),console.log(`[profiling] kernel "${a}|${d}|${p}|${m}" ${_}${$}execution time: ${x-y} ns`)}$r("GPU",`${m}::${w}::${g}`)}t.unmap(),this.pendingQueries.delete(t)}),De()}run(t,r,n,o,i,a){Ve(t.name);let l=[];for(let S=0;S<r.length;++S){let I=r[S].data;if(I===0)continue;let T=this.gpuDataManager.get(I);if(!T)throw new Error(`no GPU data for input: ${I}`);l.push(T)}let{outputs:d,dispatchGroup:p,programUniforms:m}=t.getRunData(r),u=n.length===0?d.map((S,I)=>I):n;if(u.length!==d.length)throw new Error(`Output size ${u.length} must be equal to ${d.length}.`);let h=[],w=[];for(let S=0;S<d.length;++S){if(!Number.isInteger(u[S])||u[S]<-3||u[S]>=a)throw new Error(`Invalid output index: ${u[S]}`);if(u[S]===-3)continue;let I=u[S]===-1,T=u[S]===-2,A=I||T?i(d[S].dataType,d[S].dims):o(u[S],d[S].dataType,d[S].dims);if(h.push(A),A.data===0)continue;let O=this.gpuDataManager.get(A.data);if(!O)throw new Error(`no GPU data for output: ${A.data}`);if(I&&this.temporaryData.push(O),T){let z=this.kernelPersistentData.get(this.currentKernelId);z||(z=[],this.kernelPersistentData.set(this.currentKernelId,z)),z.push(O)}w.push(O)}if(l.length!==r.length||w.length!==h.length){if(w.length===0)return De(t.name),h;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let g;if(m){let S=0,I=[];m.forEach(z=>{let W=typeof z.data=="number"?[z.data]:z.data;if(W.length===0)return;let V=z.type===10?2:4,j,Y;z.type===10?(Y=W.length>4?16:W.length>2?8:W.length*V,j=W.length>4?16:V*W.length):(Y=W.length<=2?W.length*V:16,j=16),S=Math.ceil(S/Y)*Y,I.push(S);let ae=z.type===10?8:4;S+=W.length>4?Math.ceil(W.length/ae)*j:W.length*V});let T=16;S=Math.ceil(S/T)*T;let A=new ArrayBuffer(S);m.forEach((z,W)=>{let V=I[W],j=typeof z.data=="number"?[z.data]:z.data;if(z.type===6)new Int32Array(A,V,j.length).set(j);else if(z.type===12)new Uint32Array(A,V,j.length).set(j);else if(z.type===10)new Uint16Array(A,V,j.length).set(j);else if(z.type===1)new Float32Array(A,V,j.length).set(j);else throw new Error(`Unsupported uniform type: ${mt(z.type)}`)});let O=this.gpuDataManager.create(S,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(O.buffer,0,A,0,S),this.gpuDataManager.release(O.id),g={offset:0,size:S,buffer:O.buffer}}let y=this.programManager.normalizeDispatchGroupSize(p),x=y[1]===1&&y[2]===1,_=mh(t,r,x),$=this.programManager.getArtifact(_);if($||($=this.programManager.build(t,y),this.programManager.setArtifact(_,$),be("info",()=>`[artifact] key: ${_}, programName: ${t.name}`)),m&&$.uniformVariablesInfo){if(m.length!==$.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${$.uniformVariablesInfo.length}, got ${m.length} in program "${$.programInfo.name}".`);for(let S=0;S<m.length;S++){let I=m[S],T=I.type,A=typeof I.data=="number"?1:I.data.length,[O,z]=$.uniformVariablesInfo[S];if(T!==O||A!==z)throw new Error(`Uniform variable ${S} mismatch: expect type ${O} with size ${z}, got type ${T} with size ${A} in program "${$.programInfo.name}".`)}}if(be("info",()=>`[ProgramManager] run "${t.name}" (key=${_}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let S={kernelId:this.currentKernelId,programName:$.programInfo.name,inputTensorViews:r,outputTensorViews:h};this.pendingKernels.push(S),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(S)}return this.programManager.run($,l,w,y,g),De(t.name),h}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,n,o){let i=yl.get(t);if(!i)throw new Error(`kernel not implemented: ${t}`);let a={kernelType:t,kernelName:o,kernelEntry:i[0],attributes:[i[1],n]};this.kernels.set(r,a)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let n of r)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,n){let o=this.kernels.get(t);if(!o)throw new Error(`kernel not created: ${t}`);let i=o.kernelType,a=o.kernelName,l=o.kernelEntry,d=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`);this.currentKernelId=t,d[0]&&(d[1]=d[0](d[1]),d[0]=void 0),be("info",()=>`[WebGPU] Start to run kernel "[${i}] ${a}"...`);let p=this.env.debug;this.temporaryData=[];try{return p&&this.device.pushErrorScope("validation"),l(r,d[1]),0}catch(m){return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${m}`)),1}finally{p&&n.push(this.device.popErrorScope().then(m=>m?`GPU validation error for kernel "[${i}] ${a}": ${m.message}`:null));for(let m of this.temporaryData)this.gpuDataManager.release(m.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,n,o){let i=this.sessionExternalDataMapping.get(t);i||(i=new Map,this.sessionExternalDataMapping.set(t,i));let a=i.get(r),l=this.gpuDataManager.registerExternalBuffer(n,o,a?.[1]);return i.set(r,[l,n]),l}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[1])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,n){return async()=>{let o=await jn(this,t,r);return Da(o.buffer,n)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){be("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){be("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){be("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),n=t.length;this.pendingKernels=[];for(let o=0;o<n;o++){let i=this.getComputePassEncoder(),a=t[o];this.writeTimestamp(this.pendingDispatchNumber*2),i.setPipeline(a.computePipeline),i.setBindGroup(0,a.bindGroup),i.dispatchWorkgroups(...a.dispatchGroup),this.writeTimestamp(this.pendingDispatchNumber*2+1),this.pendingDispatchNumber++,this.queryType!=="none"&&this.pendingKernels.push(r[o]),(this.pendingDispatchNumber>=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var $l={};Lt($l,{init:()=>fh});var tr,_o,fh,_l=M(()=>{"use strict";X();vl();st();oe();tr=class e{constructor(t,r,n,o){this.module=t;this.dataType=r;this.data=n;this.dims=o}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=E.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=E.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=E.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(E.size(t)!==E.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},_o=class{constructor(t,r,n){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let o=t.HEAPU32,i=n>>>2;this.opKernelContext=o[i++];let a=o[i++];this.outputCount=o[i++],this.customDataOffset=o[i++],this.customDataSize=o[i++];let l=[];for(let d=0;d<a;d++){let p=o[i++],m=o[i++],u=o[i++],h=[];for(let w=0;w<u;w++)h.push(o[i++]);l.push(new tr(t,p,m,h))}this.inputs=l}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}getMaxComputeWorkgroupSizes(){return[this.backend.device.limits.maxComputeWorkgroupSizeX,this.backend.device.limits.maxComputeWorkgroupSizeY,this.backend.device.limits.maxComputeWorkgroupSizeZ]}getMaxComputeWorkgroupStoragesize(){return this.backend.device.limits.maxComputeWorkgroupStorageSize}compute(t,r){let n=r?.inputs?.map(l=>typeof l=="number"?this.inputs[l]:l)??this.inputs,o=r?.outputs??[],i=(l,d,p)=>new tr(this.module,d,this.output(l,p),p),a=(l,d)=>{let p=_t(l,d);if(!p)throw new Error(`Unsupported data type: ${l}`);let m=p>0?this.backend.gpuDataManager.create(p).id:0;return new tr(this.module,l,m,d)};return this.backend.run(t,n,o,i,a,this.outputCount)}output(t,r){let n=this.module.stackSave();try{let o=this.module.stackAlloc((1+r.length)*4),i=o>>2;this.module.HEAPU32[i++]=r.length;for(let a=0;a<r.length;a++)this.module.HEAPU32[i++]=r[a];return this.module._JsepOutput(this.opKernelContext,t,o)}catch(o){throw new Error(`Failed to generate kernel's output[${t}] with dims [${r}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${o}`)}finally{this.module.stackRestore(n)}}},fh=async(e,t,r,n)=>{let o=t.jsepInit;if(!o)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let i=new nn;await i.initialize(r,n),o("webgpu",[i,a=>i.alloc(a),a=>i.free(a),(a,l,d,p=!1)=>{if(p)be("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${a}, dst=${l}, size=${d}`),i.memcpy(a,l);else{be("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${a}, gpuDataId=${l}, size=${d}`);let m=t.HEAPU8.subarray(a>>>0,(a>>>0)+d);i.upload(l,m)}},async(a,l,d)=>{be("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${a}, dataOffset=${l}, size=${d}`),await i.download(a,()=>t.HEAPU8.subarray(l>>>0,(l>>>0)+d))},(a,l,d)=>i.createKernel(a,l,d,t.UTF8ToString(t._JsepGetNodeName(l))),a=>i.releaseKernel(a),(a,l,d,p)=>{be("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${d}, kernel=${a}, contextDataOffset=${l}`);let m=new _o(t,i,l);return i.computeKernel(a,m,p)},()=>i.captureBegin(),()=>i.captureEnd(),()=>i.replay()])}else o("webnn")}});var hh,Cr,Tr,Tt,gh,Ft,Ar,kr,xl,Er,Pr,Or,Un=M(()=>{"use strict";ka();Pa();X();$t();Br();Gn();hh=(e,t)=>{_e()._OrtInit(e,t)!==0&&we("Can't initialize onnxruntime.")},Cr=async e=>{hh(e.wasm.numThreads,Kt(e.logLevel))},Tr=async(e,t)=>{{let r=(_l(),hr($l)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let n=e.webgpu.adapter;if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let i=e.webgpu.forceFallbackAdapter;if(i!==void 0&&typeof i!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:i}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await r("webgpu",_e(),e,n)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",_e(),e)}}},Tt=new Map,gh=e=>{let t=_e(),r=t.stackSave();try{let n=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,n,n+4)!==0&&we("Can't get session input/output count."),[t.HEAP32[n/4],t.HEAP32[n/4+1]]}finally{t.stackRestore(r)}},Ft=e=>{let t=_e(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},Ar=async(e,t)=>{let r,n,o=_e();Array.isArray(e)?[r,n]=e:e.buffer===o.HEAPU8.buffer?[r,n]=[e.byteOffset,e.byteLength]:[r,n]=Ft(e);let i=0,a=0,l=0,d=[],p=[],m=[];try{if([a,d]=Ea(t),t?.externalData&&o.mountExternalData){let $=[];for(let S of t.externalData){let I=typeof S=="string"?S:S.path;$.push(jt(typeof S=="string"?S:S.data).then(T=>{o.mountExternalData(I,T)}))}await Promise.all($)}for(let $ of t?.executionProviders??[])if((typeof $=="string"?$:$.name)==="webnn"){if(o.currentContext)throw new Error("WebNN execution provider is already set.");if(typeof $!="string"){let I=$,T=I?.context,A=I?.gpuDevice,O=I?.deviceType,z=I?.numThreads,W=I?.powerPreference;T?o.currentContext=T:A?o.currentContext=await navigator.ml.createContext(A):o.currentContext=await navigator.ml.createContext({deviceType:O,numThreads:z,powerPreference:W})}else o.currentContext=await navigator.ml.createContext();break}i=await o._OrtCreateSession(r,n,a),i===0&&we("Can't create a session."),o.currentContext&&(o.currentContext=void 0);let[u,h]=gh(i),w=!!t?.enableGraphCapture,g=[],y=[],x=[];for(let $=0;$<u;$++){let S=o._OrtGetInputName(i,$);S===0&&we("Can't get an input name."),p.push(S),g.push(o.UTF8ToString(S))}for(let $=0;$<h;$++){let S=o._OrtGetOutputName(i,$);S===0&&we("Can't get an output name."),m.push(S);let I=o.UTF8ToString(S);y.push(I);{if(w&&t?.preferredOutputLocation===void 0){x.push("gpu-buffer");continue}let T=typeof t?.preferredOutputLocation=="string"?t.preferredOutputLocation:t?.preferredOutputLocation?.[I]??"cpu";if(T!=="cpu"&&T!=="cpu-pinned"&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}.`);if(w&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);x.push(T)}}let _=null;return x.some($=>$==="gpu-buffer")&&(l=o._OrtCreateBinding(i),l===0&&we("Can't create IO binding."),_={handle:l,outputPreferredLocations:x,outputPreferredLocationsEncoded:x.map($=>Hn($))}),Tt.set(i,[i,p,m,_,w,!1]),[i,g,y]}catch(u){throw p.forEach(h=>o._OrtFree(h)),m.forEach(h=>o._OrtFree(h)),l!==0&&o._OrtReleaseBinding(l),i!==0&&o._OrtReleaseSession(i),u}finally{o._free(r),a!==0&&o._OrtReleaseSessionOptions(a),d.forEach(u=>o._free(u)),o.unmountExternalData?.()}},kr=e=>{let t=_e(),r=Tt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[n,o,i,a,l]=r;a&&(l&&t._OrtClearBoundOutputs(a.handle),t._OrtReleaseBinding(a.handle)),t.jsepOnReleaseSession?.(e),o.forEach(d=>t._OrtFree(d)),i.forEach(d=>t._OrtFree(d)),t._OrtReleaseSession(n),Tt.delete(e)},xl=(e,t,r,n,o,i=!1)=>{if(!e){t.push(0);return}let a=_e(),l=e[0],d=e[1],p=e[3],m,u;if(l==="string"&&p==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");if(i&&p!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`);if(p==="gpu-buffer"){let g=e[2].gpuBuffer;u=_t(Ln(l),d);let y=a.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');m=y(n,o,g,u)}else{let g=e[2];if(Array.isArray(g)){u=4*g.length,m=a._malloc(u),r.push(m);let y=m/4;for(let x=0;x<g.length;x++){if(typeof g[x]!="string")throw new TypeError(`tensor data at index ${x} is not a string`);a.HEAPU32[y++]=Ie(g[x],r)}}else u=g.byteLength,m=a._malloc(u),r.push(m),a.HEAPU8.set(new Uint8Array(g.buffer,g.byteOffset,u),m)}let h=a.stackSave(),w=a.stackAlloc(4*d.length);try{let g=w/4;d.forEach(x=>a.HEAP32[g++]=x);let y=a._OrtCreateTensor(Ln(l),m,u,w,d.length,Hn(p));y===0&&we(`Can't create tensor for input/output. session=${n}, index=${o}.`),t.push(y)}finally{a.stackRestore(h)}},Er=async(e,t,r,n,o,i)=>{let a=_e(),l=Tt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let d=l[0],p=l[1],m=l[2],u=l[3],h=l[4],w=l[5],g=t.length,y=n.length,x=0,_=[],$=[],S=[],I=[],T=a.stackSave(),A=a.stackAlloc(g*4),O=a.stackAlloc(g*4),z=a.stackAlloc(y*4),W=a.stackAlloc(y*4);try{[x,_]=Aa(i);for(let q=0;q<g;q++)xl(r[q],$,I,e,t[q],h);for(let q=0;q<y;q++)xl(o[q],S,I,e,g+n[q],h);let V=A/4,j=O/4,Y=z/4,ae=W/4;for(let q=0;q<g;q++)a.HEAPU32[V++]=$[q],a.HEAPU32[j++]=p[t[q]];for(let q=0;q<y;q++)a.HEAPU32[Y++]=S[q],a.HEAPU32[ae++]=m[n[q]];if(u&&!w){let{handle:q,outputPreferredLocations:ce,outputPreferredLocationsEncoded:ne}=u;if(p.length!==g)throw new Error(`input count from feeds (${g}) is expected to be always equal to model's input count (${p.length}).`);for(let Z=0;Z<g;Z++){let ee=t[Z];await a._OrtBindInput(q,p[ee],$[Z])!==0&&we(`Can't bind input[${Z}] for session=${e}.`)}for(let Z=0;Z<y;Z++){let ee=n[Z];o[Z]?.[3]?a._OrtBindOutput(q,m[ee],S[Z],0)!==0&&we(`Can't bind pre-allocated output[${Z}] for session=${e}.`):a._OrtBindOutput(q,m[ee],0,ne[ee])!==0&&we(`Can't bind output[${Z}] to ${ce[Z]} for session=${e}.`)}Tt.set(e,[d,p,m,u,h,!0])}a.jsepOnRunStart?.(d);let se;u?se=await a._OrtRunWithBinding(d,u.handle,y,z,x):se=await a._OrtRun(d,O,A,g,W,y,z,x),se!==0&&we("failed to call OrtRun().");let ie=[];for(let q=0;q<y;q++){let ce=a.HEAPU32[z/4+q];if(ce===S[q]){ie.push(o[q]);continue}let ne=a.stackSave(),Z=a.stackAlloc(4*4),ee=!1,K,me=0;try{a._OrtGetTensorData(ce,Z,Z+4,Z+8,Z+12)!==0&&we(`Can't access output tensor data on index ${q}.`);let R=Z/4,G=a.HEAPU32[R++];me=a.HEAPU32[R++];let ue=a.HEAPU32[R++],Be=a.HEAPU32[R++],ge=[];for(let Te=0;Te<Be;Te++)ge.push(a.HEAPU32[ue/4+Te]);a._OrtFree(ue);let Se=ge.reduce((Te,Oe)=>Te*Oe,1);K=mt(G);let Wt=u?.outputPreferredLocations[n[q]];if(K==="string"){if(Wt==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");let Te=[],Oe=me/4;for(let He=0;He<Se;He++){let kt=a.HEAPU32[Oe++],ht=He===Se-1?void 0:a.HEAPU32[Oe]-kt;Te.push(a.UTF8ToString(kt,ht))}ie.push([K,ge,Te,"cpu"])}else if(Wt==="gpu-buffer"&&Se>0){let Te=a.jsepGetBuffer;if(!Te)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let Oe=Te(me),He=_t(G,Se);if(He===void 0||!Rr(K))throw new Error(`Unsupported data type: ${K}`);ee=!0,ie.push([K,ge,{gpuBuffer:Oe,download:a.jsepCreateDownloader(Oe,He,K),dispose:()=>{a._OrtReleaseTensor(ce)}},"gpu-buffer"])}else{let Te=Dr(K),Oe=new Te(Se);new Uint8Array(Oe.buffer,Oe.byteOffset,Oe.byteLength).set(a.HEAPU8.subarray(me,me+Oe.byteLength)),ie.push([K,ge,Oe,"cpu"])}}finally{a.stackRestore(ne),K==="string"&&me&&a._free(me),ee||a._OrtReleaseTensor(ce)}}return u&&!h&&(a._OrtClearBoundOutputs(u.handle),Tt.set(e,[d,p,m,u,h,!1])),ie}finally{a.stackRestore(T),$.forEach(V=>a._OrtReleaseTensor(V)),S.forEach(V=>a._OrtReleaseTensor(V)),I.forEach(V=>a._free(V)),x!==0&&a._OrtReleaseRunOptions(x),_.forEach(V=>a._free(V))}},Pr=e=>{let t=_e(),r=Tt.get(e);if(!r)throw new Error("invalid session id");let n=r[0],o=t._OrtEndProfiling(n);o===0&&we("Can't get an profile file name."),t._OrtFree(o)},Or=e=>{let t=[];for(let r of e){let n=r[2];!Array.isArray(n)&&"buffer"in n&&t.push(n.buffer)}return t}});var At,Ke,rr,an,sn,on,xo,So,Vt,Nt,bh,Sl,Il,Cl,Tl,Al,kl,El,Io=M(()=>{"use strict";qe();Un();$t();Gt();At=()=>!!fe.wasm.proxy&&typeof document<"u",rr=!1,an=!1,sn=!1,So=new Map,Vt=(e,t)=>{let r=So.get(e);r?r.push(t):So.set(e,[t])},Nt=()=>{if(rr||!an||sn||!Ke)throw new Error("worker not ready")},bh=e=>{switch(e.data.type){case"init-wasm":rr=!1,e.data.err?(sn=!0,xo[1](e.data.err)):(an=!0,xo[0]()),on&&(URL.revokeObjectURL(on),on=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=So.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},Sl=async()=>{if(!an){if(rr)throw new Error("multiple calls to 'initWasm()' detected.");if(sn)throw new Error("previous call to 'initWasm()' failed.");if(rr=!0,At())return new Promise((e,t)=>{Ke?.terminate(),Ia().then(([r,n])=>{try{Ke=n,Ke.onerror=i=>t(i),Ke.onmessage=bh,xo=[e,t];let o={type:"init-wasm",in:fe};Ke.postMessage(o),on=r}catch(o){t(o)}},t)});try{await Ir(fe.wasm),await Cr(fe),an=!0}catch(e){throw sn=!0,e}finally{rr=!1}}},Il=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("init-ep",[t,r]);let n={type:"init-ep",in:{epName:e,env:fe}};Ke.postMessage(n)});await Tr(fe,e)},Cl=async e=>At()?(Nt(),new Promise((t,r)=>{Vt("copy-from",[t,r]);let n={type:"copy-from",in:{buffer:e}};Ke.postMessage(n,[e.buffer])})):Ft(e),Tl=async(e,t)=>{if(At()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Nt(),new Promise((r,n)=>{Vt("create",[r,n]);let o={type:"create",in:{model:e,options:{...t}}},i=[];e instanceof Uint8Array&&i.push(e.buffer),Ke.postMessage(o,i)})}else return Ar(e,t)},Al=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("release",[t,r]);let n={type:"release",in:e};Ke.postMessage(n)});kr(e)},kl=async(e,t,r,n,o,i)=>{if(At()){if(r.some(a=>a[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(o.some(a=>a))throw new Error("pre-allocated output tensor is not supported for proxy.");return Nt(),new Promise((a,l)=>{Vt("run",[a,l]);let d=r,p={type:"run",in:{sessionId:e,inputIndices:t,inputs:d,outputIndices:n,options:i}};Ke.postMessage(p,Or(d))})}else return Er(e,t,r,n,o,i)},El=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("end-profiling",[t,r]);let n={type:"end-profiling",in:e};Ke.postMessage(n)});Pr(e)}});var Pl,wh,un,Ol=M(()=>{"use strict";qe();Io();X();Sr();Gn();Pl=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},wh=e=>{switch(e[3]){case"cpu":return new Pe(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Rr(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:n,dispose:o}=e[2];return Pe.fromGpuBuffer(r,{dataType:t,dims:e[1],download:n,dispose:o})}default:throw new Error(`invalid data location: ${e[3]}`)}},un=class{async fetchModelAndCopyToWasmMemory(t){return Cl(await jt(t))}async loadModel(t,r){Ve();let n;typeof t=="string"? false?0:n=await this.fetchModelAndCopyToWasmMemory(t):n=t,[this.sessionId,this.inputNames,this.outputNames]=await Tl(n,r),De()}async dispose(){return Al(this.sessionId)}async run(t,r,n){Ve();let o=[],i=[];Object.entries(t).forEach(h=>{let w=h[0],g=h[1],y=this.inputNames.indexOf(w);if(y===-1)throw new Error(`invalid input '${w}'`);o.push(g),i.push(y)});let a=[],l=[];Object.entries(r).forEach(h=>{let w=h[0],g=h[1],y=this.outputNames.indexOf(w);if(y===-1)throw new Error(`invalid output '${w}'`);a.push(g),l.push(y)});let d=o.map((h,w)=>Pl(h,()=>`input "${this.inputNames[i[w]]}"`)),p=a.map((h,w)=>h?Pl(h,()=>`output "${this.outputNames[l[w]]}"`):null),m=await kl(this.sessionId,i,d,l,p,n),u={};for(let h=0;h<m.length;h++)u[this.outputNames[l[h]]]=a[h]??wh(m[h]);return De(),u}startProfiling(){}endProfiling(){El(this.sessionId)}}});var vh,dn,zl=M(()=>{"use strict";qe();Io();Ol();Gt();vh=()=>{if((typeof fe.wasm.initTimeout!="number"||fe.wasm.initTimeout<0)&&(fe.wasm.initTimeout=0),fe.wasm.simd===!1&&console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'),typeof fe.wasm.proxy!="boolean"&&(fe.wasm.proxy=!1),typeof fe.wasm.trace!="boolean"&&(fe.wasm.trace=!1),typeof fe.wasm.numThreads!="number"||!Number.isInteger(fe.wasm.numThreads)||fe.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)fe.wasm.numThreads=1;else{let e=typeof navigator>"u"?zn("node:os").cpus().length:navigator.hardwareConcurrency;fe.wasm.numThreads=Math.min(4,Math.ceil((e||1)/2))}},dn=class{async init(t){vh(),await Sl(),await Il(t)}async createInferenceSessionHandler(t,r){let n=new un;return await n.loadModel(t,r),Promise.resolve(n)}}});var Bl={};Lt(Bl,{wasmBackend:()=>$h});var $h,Dl=M(()=>{"use strict";zl();$h=new dn});qe();qe();qe();var ga="1.20.0-dev.20240818-417aa00406";var ex=Mn;{let e=(Dl(),hr(Bl)).wasmBackend;wt("webgpu",e,5),wt("webnn",e,5),wt("cpu",e,10),wt("wasm",e,10)}Object.defineProperty(fe.versions,"web",{value:ga,enumerable:!0});
|
|
6370
|
+
${a}`,d=n.createShaderModule({code:l,label:t.name});be("verbose",()=>`[WebGPU] ${t.name} shader code: ${l}`);let c=n.createComputePipeline({compute:{module:d,entryPoint:"main"},layout:"auto",label:t.name});return De(t.name),{programInfo:t,computePipeline:c,uniformVariablesInfo:i.variablesInfo}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,n=typeof t=="number"?1:t.y||1,o=typeof t=="number"?1:t.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=i&&n<=i&&o<=i)return[r,n,o];let a=r*n*o,l=Math.ceil(Math.sqrt(a));if(l>i){if(l=Math.ceil(Math.cbrt(a)),l>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[l,l,l]}else return[l,l,1]}}});var ph,mh,$o,nn,vl=M(()=>{"use strict";qe();X();st();Ra();Wa();bl();wl();ph=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let n=0;n<e.length;++n){let o=e[n].dataType;switch(t[n]){case"none":{r.push("");break}case"type":{r.push(`${o}`);break}case"rank":{let i=e[n].dims.length;r.push(`${o};${i}`);break}case"dims":{let i=e[n].dims.join(",");r.push(`${o};${i}`);break}default:throw new Error(`unsupported input dependency: ${t[n]}`)}}return r.join("|")},mh=(e,t,r)=>{let n=e.name;return e.shaderCache?.hint&&(n+="["+e.shaderCache.hint+"]"),n+=":"+r+`:${ph(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,n},$o=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},nn=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n};r.features.has("chromium-experimental-timestamp-query-inside-passes")?n.push("chromium-experimental-timestamp-query-inside-passes"):r.features.has("timestamp-query")&&n.push("timestamp-query"),r.features.has("shader-f16")&&n.push("shader-f16"),this.device=await r.requestDevice(o),this.adapterInfo=new $o(r.info||await r.requestAdapterInfo()),this.gpuDataManager=Na(this),this.programManager=new rn(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Ba(t.logLevel,!!t.debug),this.device.onuncapturederror=i=>{i.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${i.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Ve(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),n=this.pendingQueries.get(t);for(let o=0;o<r.length/2;o++){let i=n[o],a=i.kernelId,l=this.kernels.get(a),d=l.kernelType,c=l.kernelName,m=i.programName,u=i.inputTensorViews,h=i.outputTensorViews,w=r[o*2],g=r[o*2+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=w);let y=Number(w-this.queryTimeBase),x=Number(g-this.queryTimeBase);if(!Number.isSafeInteger(y)||!Number.isSafeInteger(x))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:u.map(_=>({dims:_.dims,dataType:mt(_.dataType)})),outputsMetadata:h.map(_=>({dims:_.dims,dataType:mt(_.dataType)})),kernelId:a,kernelType:d,kernelName:c,programName:m,startTime:y,endTime:x});else{let _="";u.forEach((S,I)=>{_+=`input[${I}]: [${S.dims}] | ${mt(S.dataType)}, `});let $="";h.forEach((S,I)=>{$+=`output[${I}]: [${S.dims}] | ${mt(S.dataType)}, `}),console.log(`[profiling] kernel "${a}|${d}|${c}|${m}" ${_}${$}execution time: ${x-y} ns`)}$r("GPU",`${m}::${w}::${g}`)}t.unmap(),this.pendingQueries.delete(t)}),De()}run(t,r,n,o,i,a){Ve(t.name);let l=[];for(let S=0;S<r.length;++S){let I=r[S].data;if(I===0)continue;let T=this.gpuDataManager.get(I);if(!T)throw new Error(`no GPU data for input: ${I}`);l.push(T)}let{outputs:d,dispatchGroup:c,programUniforms:m}=t.getRunData(r),u=n.length===0?d.map((S,I)=>I):n;if(u.length!==d.length)throw new Error(`Output size ${u.length} must be equal to ${d.length}.`);let h=[],w=[];for(let S=0;S<d.length;++S){if(!Number.isInteger(u[S])||u[S]<-3||u[S]>=a)throw new Error(`Invalid output index: ${u[S]}`);if(u[S]===-3)continue;let I=u[S]===-1,T=u[S]===-2,A=I||T?i(d[S].dataType,d[S].dims):o(u[S],d[S].dataType,d[S].dims);if(h.push(A),A.data===0)continue;let O=this.gpuDataManager.get(A.data);if(!O)throw new Error(`no GPU data for output: ${A.data}`);if(I&&this.temporaryData.push(O),T){let z=this.kernelPersistentData.get(this.currentKernelId);z||(z=[],this.kernelPersistentData.set(this.currentKernelId,z)),z.push(O)}w.push(O)}if(l.length!==r.length||w.length!==h.length){if(w.length===0)return De(t.name),h;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let g;if(m){let S=0,I=[];m.forEach(z=>{let W=typeof z.data=="number"?[z.data]:z.data;if(W.length===0)return;let V=z.type===10?2:4,j,Y;z.type===10?(Y=W.length>4?16:W.length>2?8:W.length*V,j=W.length>4?16:V*W.length):(Y=W.length<=2?W.length*V:16,j=16),S=Math.ceil(S/Y)*Y,I.push(S);let ae=z.type===10?8:4;S+=W.length>4?Math.ceil(W.length/ae)*j:W.length*V});let T=16;S=Math.ceil(S/T)*T;let A=new ArrayBuffer(S);m.forEach((z,W)=>{let V=I[W],j=typeof z.data=="number"?[z.data]:z.data;if(z.type===6)new Int32Array(A,V,j.length).set(j);else if(z.type===12)new Uint32Array(A,V,j.length).set(j);else if(z.type===10)new Uint16Array(A,V,j.length).set(j);else if(z.type===1)new Float32Array(A,V,j.length).set(j);else throw new Error(`Unsupported uniform type: ${mt(z.type)}`)});let O=this.gpuDataManager.create(S,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(O.buffer,0,A,0,S),this.gpuDataManager.release(O.id),g={offset:0,size:S,buffer:O.buffer}}let y=this.programManager.normalizeDispatchGroupSize(c),x=y[1]===1&&y[2]===1,_=mh(t,r,x),$=this.programManager.getArtifact(_);if($||($=this.programManager.build(t,y),this.programManager.setArtifact(_,$),be("info",()=>`[artifact] key: ${_}, programName: ${t.name}`)),m&&$.uniformVariablesInfo){if(m.length!==$.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${$.uniformVariablesInfo.length}, got ${m.length} in program "${$.programInfo.name}".`);for(let S=0;S<m.length;S++){let I=m[S],T=I.type,A=typeof I.data=="number"?1:I.data.length,[O,z]=$.uniformVariablesInfo[S];if(T!==O||A!==z)throw new Error(`Uniform variable ${S} mismatch: expect type ${O} with size ${z}, got type ${T} with size ${A} in program "${$.programInfo.name}".`)}}if(be("info",()=>`[ProgramManager] run "${t.name}" (key=${_}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let S={kernelId:this.currentKernelId,programName:$.programInfo.name,inputTensorViews:r,outputTensorViews:h};this.pendingKernels.push(S),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(S)}return this.programManager.run($,l,w,y,g),De(t.name),h}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,n,o){let i=yl.get(t);if(!i)throw new Error(`kernel not implemented: ${t}`);let a={kernelType:t,kernelName:o,kernelEntry:i[0],attributes:[i[1],n]};this.kernels.set(r,a)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let n of r)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,n){let o=this.kernels.get(t);if(!o)throw new Error(`kernel not created: ${t}`);let i=o.kernelType,a=o.kernelName,l=o.kernelEntry,d=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`);this.currentKernelId=t,d[0]&&(d[1]=d[0](d[1]),d[0]=void 0),be("info",()=>`[WebGPU] Start to run kernel "[${i}] ${a}"...`);let c=this.env.debug;this.temporaryData=[];try{return c&&this.device.pushErrorScope("validation"),l(r,d[1]),0}catch(m){return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${m}`)),1}finally{c&&n.push(this.device.popErrorScope().then(m=>m?`GPU validation error for kernel "[${i}] ${a}": ${m.message}`:null));for(let m of this.temporaryData)this.gpuDataManager.release(m.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,n,o){let i=this.sessionExternalDataMapping.get(t);i||(i=new Map,this.sessionExternalDataMapping.set(t,i));let a=i.get(r),l=this.gpuDataManager.registerExternalBuffer(n,o,a?.[1]);return i.set(r,[l,n]),l}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[1])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,n){return async()=>{let o=await jn(this,t,r);return Da(o.buffer,n)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){be("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){be("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){be("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),n=t.length;this.pendingKernels=[];for(let o=0;o<n;o++){let i=this.getComputePassEncoder(),a=t[o];this.writeTimestamp(this.pendingDispatchNumber*2),i.setPipeline(a.computePipeline),i.setBindGroup(0,a.bindGroup),i.dispatchWorkgroups(...a.dispatchGroup),this.writeTimestamp(this.pendingDispatchNumber*2+1),this.pendingDispatchNumber++,this.queryType!=="none"&&this.pendingKernels.push(r[o]),(this.pendingDispatchNumber>=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var $l={};Lt($l,{init:()=>fh});var tr,_o,fh,_l=M(()=>{"use strict";X();vl();st();te();tr=class e{constructor(t,r,n,o){this.module=t;this.dataType=r;this.data=n;this.dims=o}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(k.size(t)!==k.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},_o=class{constructor(t,r,n){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let o=t.HEAPU32,i=n>>>2;this.opKernelContext=o[i++];let a=o[i++];this.outputCount=o[i++],this.customDataOffset=o[i++],this.customDataSize=o[i++];let l=[];for(let d=0;d<a;d++){let c=o[i++],m=o[i++],u=o[i++],h=[];for(let w=0;w<u;w++)h.push(o[i++]);l.push(new tr(t,c,m,h))}this.inputs=l}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}getMaxComputeWorkgroupSizes(){return[this.backend.device.limits.maxComputeWorkgroupSizeX,this.backend.device.limits.maxComputeWorkgroupSizeY,this.backend.device.limits.maxComputeWorkgroupSizeZ]}getMaxComputeWorkgroupStoragesize(){return this.backend.device.limits.maxComputeWorkgroupStorageSize}compute(t,r){let n=r?.inputs?.map(l=>typeof l=="number"?this.inputs[l]:l)??this.inputs,o=r?.outputs??[],i=(l,d,c)=>new tr(this.module,d,this.output(l,c),c),a=(l,d)=>{let c=_t(l,d);if(!c)throw new Error(`Unsupported data type: ${l}`);let m=c>0?this.backend.gpuDataManager.create(c).id:0;return new tr(this.module,l,m,d)};return this.backend.run(t,n,o,i,a,this.outputCount)}output(t,r){let n=this.module.stackSave();try{let o=this.module.stackAlloc((1+r.length)*4),i=o>>2;this.module.HEAPU32[i++]=r.length;for(let a=0;a<r.length;a++)this.module.HEAPU32[i++]=r[a];return this.module._JsepOutput(this.opKernelContext,t,o)}catch(o){throw new Error(`Failed to generate kernel's output[${t}] with dims [${r}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${o}`)}finally{this.module.stackRestore(n)}}},fh=async(e,t,r,n)=>{let o=t.jsepInit;if(!o)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let i=new nn;await i.initialize(r,n),o("webgpu",[i,a=>i.alloc(a),a=>i.free(a),(a,l,d,c=!1)=>{if(c)be("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${a}, dst=${l}, size=${d}`),i.memcpy(a,l);else{be("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${a}, gpuDataId=${l}, size=${d}`);let m=t.HEAPU8.subarray(a>>>0,(a>>>0)+d);i.upload(l,m)}},async(a,l,d)=>{be("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${a}, dataOffset=${l}, size=${d}`),await i.download(a,()=>t.HEAPU8.subarray(l>>>0,(l>>>0)+d))},(a,l,d)=>i.createKernel(a,l,d,t.UTF8ToString(t._JsepGetNodeName(l))),a=>i.releaseKernel(a),(a,l,d,c)=>{be("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${d}, kernel=${a}, contextDataOffset=${l}`);let m=new _o(t,i,l);return i.computeKernel(a,m,c)},()=>i.captureBegin(),()=>i.captureEnd(),()=>i.replay()])}else o("webnn")}});var hh,Cr,Tr,Tt,gh,Ft,Ar,kr,xl,Er,Pr,Or,Un=M(()=>{"use strict";ka();Pa();X();$t();Br();Gn();hh=(e,t)=>{_e()._OrtInit(e,t)!==0&&we("Can't initialize onnxruntime.")},Cr=async e=>{hh(e.wasm.numThreads,Kt(e.logLevel))},Tr=async(e,t)=>{{let r=(_l(),hr($l)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let n=e.webgpu.adapter;if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let i=e.webgpu.forceFallbackAdapter;if(i!==void 0&&typeof i!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:i}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await r("webgpu",_e(),e,n)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",_e(),e)}}},Tt=new Map,gh=e=>{let t=_e(),r=t.stackSave();try{let n=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,n,n+4)!==0&&we("Can't get session input/output count."),[t.HEAP32[n/4],t.HEAP32[n/4+1]]}finally{t.stackRestore(r)}},Ft=e=>{let t=_e(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},Ar=async(e,t)=>{let r,n,o=_e();Array.isArray(e)?[r,n]=e:e.buffer===o.HEAPU8.buffer?[r,n]=[e.byteOffset,e.byteLength]:[r,n]=Ft(e);let i=0,a=0,l=0,d=[],c=[],m=[];try{if([a,d]=Ea(t),t?.externalData&&o.mountExternalData){let $=[];for(let S of t.externalData){let I=typeof S=="string"?S:S.path;$.push(jt(typeof S=="string"?S:S.data).then(T=>{o.mountExternalData(I,T)}))}await Promise.all($)}for(let $ of t?.executionProviders??[])if((typeof $=="string"?$:$.name)==="webnn"){if(o.currentContext)throw new Error("WebNN execution provider is already set.");if(typeof $!="string"){let I=$,T=I?.context,A=I?.gpuDevice,O=I?.deviceType,z=I?.numThreads,W=I?.powerPreference;T?o.currentContext=T:A?o.currentContext=await navigator.ml.createContext(A):o.currentContext=await navigator.ml.createContext({deviceType:O,numThreads:z,powerPreference:W})}else o.currentContext=await navigator.ml.createContext();break}i=await o._OrtCreateSession(r,n,a),i===0&&we("Can't create a session."),o.currentContext&&(o.currentContext=void 0);let[u,h]=gh(i),w=!!t?.enableGraphCapture,g=[],y=[],x=[];for(let $=0;$<u;$++){let S=o._OrtGetInputName(i,$);S===0&&we("Can't get an input name."),c.push(S),g.push(o.UTF8ToString(S))}for(let $=0;$<h;$++){let S=o._OrtGetOutputName(i,$);S===0&&we("Can't get an output name."),m.push(S);let I=o.UTF8ToString(S);y.push(I);{if(w&&t?.preferredOutputLocation===void 0){x.push("gpu-buffer");continue}let T=typeof t?.preferredOutputLocation=="string"?t.preferredOutputLocation:t?.preferredOutputLocation?.[I]??"cpu";if(T!=="cpu"&&T!=="cpu-pinned"&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}.`);if(w&&T!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${T}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);x.push(T)}}let _=null;return x.some($=>$==="gpu-buffer")&&(l=o._OrtCreateBinding(i),l===0&&we("Can't create IO binding."),_={handle:l,outputPreferredLocations:x,outputPreferredLocationsEncoded:x.map($=>Hn($))}),Tt.set(i,[i,c,m,_,w,!1]),[i,g,y]}catch(u){throw c.forEach(h=>o._OrtFree(h)),m.forEach(h=>o._OrtFree(h)),l!==0&&o._OrtReleaseBinding(l),i!==0&&o._OrtReleaseSession(i),u}finally{o._free(r),a!==0&&o._OrtReleaseSessionOptions(a),d.forEach(u=>o._free(u)),o.unmountExternalData?.()}},kr=e=>{let t=_e(),r=Tt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[n,o,i,a,l]=r;a&&(l&&t._OrtClearBoundOutputs(a.handle),t._OrtReleaseBinding(a.handle)),t.jsepOnReleaseSession?.(e),o.forEach(d=>t._OrtFree(d)),i.forEach(d=>t._OrtFree(d)),t._OrtReleaseSession(n),Tt.delete(e)},xl=(e,t,r,n,o,i=!1)=>{if(!e){t.push(0);return}let a=_e(),l=e[0],d=e[1],c=e[3],m,u;if(l==="string"&&c==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");if(i&&c!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`);if(c==="gpu-buffer"){let g=e[2].gpuBuffer;u=_t(Ln(l),d);let y=a.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');m=y(n,o,g,u)}else{let g=e[2];if(Array.isArray(g)){u=4*g.length,m=a._malloc(u),r.push(m);let y=m/4;for(let x=0;x<g.length;x++){if(typeof g[x]!="string")throw new TypeError(`tensor data at index ${x} is not a string`);a.HEAPU32[y++]=Ie(g[x],r)}}else u=g.byteLength,m=a._malloc(u),r.push(m),a.HEAPU8.set(new Uint8Array(g.buffer,g.byteOffset,u),m)}let h=a.stackSave(),w=a.stackAlloc(4*d.length);try{let g=w/4;d.forEach(x=>a.HEAP32[g++]=x);let y=a._OrtCreateTensor(Ln(l),m,u,w,d.length,Hn(c));y===0&&we(`Can't create tensor for input/output. session=${n}, index=${o}.`),t.push(y)}finally{a.stackRestore(h)}},Er=async(e,t,r,n,o,i)=>{let a=_e(),l=Tt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let d=l[0],c=l[1],m=l[2],u=l[3],h=l[4],w=l[5],g=t.length,y=n.length,x=0,_=[],$=[],S=[],I=[],T=a.stackSave(),A=a.stackAlloc(g*4),O=a.stackAlloc(g*4),z=a.stackAlloc(y*4),W=a.stackAlloc(y*4);try{[x,_]=Aa(i);for(let q=0;q<g;q++)xl(r[q],$,I,e,t[q],h);for(let q=0;q<y;q++)xl(o[q],S,I,e,g+n[q],h);let V=A/4,j=O/4,Y=z/4,ae=W/4;for(let q=0;q<g;q++)a.HEAPU32[V++]=$[q],a.HEAPU32[j++]=c[t[q]];for(let q=0;q<y;q++)a.HEAPU32[Y++]=S[q],a.HEAPU32[ae++]=m[n[q]];if(u&&!w){let{handle:q,outputPreferredLocations:ce,outputPreferredLocationsEncoded:oe}=u;if(c.length!==g)throw new Error(`input count from feeds (${g}) is expected to be always equal to model's input count (${c.length}).`);for(let Z=0;Z<g;Z++){let ee=t[Z];await a._OrtBindInput(q,c[ee],$[Z])!==0&&we(`Can't bind input[${Z}] for session=${e}.`)}for(let Z=0;Z<y;Z++){let ee=n[Z];o[Z]?.[3]?a._OrtBindOutput(q,m[ee],S[Z],0)!==0&&we(`Can't bind pre-allocated output[${Z}] for session=${e}.`):a._OrtBindOutput(q,m[ee],0,oe[ee])!==0&&we(`Can't bind output[${Z}] to ${ce[Z]} for session=${e}.`)}Tt.set(e,[d,c,m,u,h,!0])}a.jsepOnRunStart?.(d);let se;u?se=await a._OrtRunWithBinding(d,u.handle,y,z,x):se=await a._OrtRun(d,O,A,g,W,y,z,x),se!==0&&we("failed to call OrtRun().");let ie=[];for(let q=0;q<y;q++){let ce=a.HEAPU32[z/4+q];if(ce===S[q]){ie.push(o[q]);continue}let oe=a.stackSave(),Z=a.stackAlloc(4*4),ee=!1,K,me=0;try{a._OrtGetTensorData(ce,Z,Z+4,Z+8,Z+12)!==0&&we(`Can't access output tensor data on index ${q}.`);let R=Z/4,G=a.HEAPU32[R++];me=a.HEAPU32[R++];let ue=a.HEAPU32[R++],Be=a.HEAPU32[R++],ge=[];for(let Te=0;Te<Be;Te++)ge.push(a.HEAPU32[ue/4+Te]);a._OrtFree(ue);let Se=ge.reduce((Te,Oe)=>Te*Oe,1);K=mt(G);let Wt=u?.outputPreferredLocations[n[q]];if(K==="string"){if(Wt==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");let Te=[],Oe=me/4;for(let He=0;He<Se;He++){let kt=a.HEAPU32[Oe++],ht=He===Se-1?void 0:a.HEAPU32[Oe]-kt;Te.push(a.UTF8ToString(kt,ht))}ie.push([K,ge,Te,"cpu"])}else if(Wt==="gpu-buffer"&&Se>0){let Te=a.jsepGetBuffer;if(!Te)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let Oe=Te(me),He=_t(G,Se);if(He===void 0||!Rr(K))throw new Error(`Unsupported data type: ${K}`);ee=!0,ie.push([K,ge,{gpuBuffer:Oe,download:a.jsepCreateDownloader(Oe,He,K),dispose:()=>{a._OrtReleaseTensor(ce)}},"gpu-buffer"])}else{let Te=Dr(K),Oe=new Te(Se);new Uint8Array(Oe.buffer,Oe.byteOffset,Oe.byteLength).set(a.HEAPU8.subarray(me,me+Oe.byteLength)),ie.push([K,ge,Oe,"cpu"])}}finally{a.stackRestore(oe),K==="string"&&me&&a._free(me),ee||a._OrtReleaseTensor(ce)}}return u&&!h&&(a._OrtClearBoundOutputs(u.handle),Tt.set(e,[d,c,m,u,h,!1])),ie}finally{a.stackRestore(T),$.forEach(V=>a._OrtReleaseTensor(V)),S.forEach(V=>a._OrtReleaseTensor(V)),I.forEach(V=>a._free(V)),x!==0&&a._OrtReleaseRunOptions(x),_.forEach(V=>a._free(V))}},Pr=e=>{let t=_e(),r=Tt.get(e);if(!r)throw new Error("invalid session id");let n=r[0],o=t._OrtEndProfiling(n);o===0&&we("Can't get an profile file name."),t._OrtFree(o)},Or=e=>{let t=[];for(let r of e){let n=r[2];!Array.isArray(n)&&"buffer"in n&&t.push(n.buffer)}return t}});var At,Ke,rr,an,sn,on,xo,So,Vt,Nt,bh,Sl,Il,Cl,Tl,Al,kl,El,Io=M(()=>{"use strict";qe();Un();$t();Gt();At=()=>!!fe.wasm.proxy&&typeof document<"u",rr=!1,an=!1,sn=!1,So=new Map,Vt=(e,t)=>{let r=So.get(e);r?r.push(t):So.set(e,[t])},Nt=()=>{if(rr||!an||sn||!Ke)throw new Error("worker not ready")},bh=e=>{switch(e.data.type){case"init-wasm":rr=!1,e.data.err?(sn=!0,xo[1](e.data.err)):(an=!0,xo[0]()),on&&(URL.revokeObjectURL(on),on=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=So.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},Sl=async()=>{if(!an){if(rr)throw new Error("multiple calls to 'initWasm()' detected.");if(sn)throw new Error("previous call to 'initWasm()' failed.");if(rr=!0,At())return new Promise((e,t)=>{Ke?.terminate(),Ia().then(([r,n])=>{try{Ke=n,Ke.onerror=i=>t(i),Ke.onmessage=bh,xo=[e,t];let o={type:"init-wasm",in:fe};Ke.postMessage(o),on=r}catch(o){t(o)}},t)});try{await Ir(fe.wasm),await Cr(fe),an=!0}catch(e){throw sn=!0,e}finally{rr=!1}}},Il=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("init-ep",[t,r]);let n={type:"init-ep",in:{epName:e,env:fe}};Ke.postMessage(n)});await Tr(fe,e)},Cl=async e=>At()?(Nt(),new Promise((t,r)=>{Vt("copy-from",[t,r]);let n={type:"copy-from",in:{buffer:e}};Ke.postMessage(n,[e.buffer])})):Ft(e),Tl=async(e,t)=>{if(At()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Nt(),new Promise((r,n)=>{Vt("create",[r,n]);let o={type:"create",in:{model:e,options:{...t}}},i=[];e instanceof Uint8Array&&i.push(e.buffer),Ke.postMessage(o,i)})}else return Ar(e,t)},Al=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("release",[t,r]);let n={type:"release",in:e};Ke.postMessage(n)});kr(e)},kl=async(e,t,r,n,o,i)=>{if(At()){if(r.some(a=>a[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(o.some(a=>a))throw new Error("pre-allocated output tensor is not supported for proxy.");return Nt(),new Promise((a,l)=>{Vt("run",[a,l]);let d=r,c={type:"run",in:{sessionId:e,inputIndices:t,inputs:d,outputIndices:n,options:i}};Ke.postMessage(c,Or(d))})}else return Er(e,t,r,n,o,i)},El=async e=>{if(At())return Nt(),new Promise((t,r)=>{Vt("end-profiling",[t,r]);let n={type:"end-profiling",in:e};Ke.postMessage(n)});Pr(e)}});var Pl,wh,un,Ol=M(()=>{"use strict";qe();Io();X();Sr();Gn();Pl=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},wh=e=>{switch(e[3]){case"cpu":return new Pe(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Rr(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:n,dispose:o}=e[2];return Pe.fromGpuBuffer(r,{dataType:t,dims:e[1],download:n,dispose:o})}default:throw new Error(`invalid data location: ${e[3]}`)}},un=class{async fetchModelAndCopyToWasmMemory(t){return Cl(await jt(t))}async loadModel(t,r){Ve();let n;typeof t=="string"? false?0:n=await this.fetchModelAndCopyToWasmMemory(t):n=t,[this.sessionId,this.inputNames,this.outputNames]=await Tl(n,r),De()}async dispose(){return Al(this.sessionId)}async run(t,r,n){Ve();let o=[],i=[];Object.entries(t).forEach(h=>{let w=h[0],g=h[1],y=this.inputNames.indexOf(w);if(y===-1)throw new Error(`invalid input '${w}'`);o.push(g),i.push(y)});let a=[],l=[];Object.entries(r).forEach(h=>{let w=h[0],g=h[1],y=this.outputNames.indexOf(w);if(y===-1)throw new Error(`invalid output '${w}'`);a.push(g),l.push(y)});let d=o.map((h,w)=>Pl(h,()=>`input "${this.inputNames[i[w]]}"`)),c=a.map((h,w)=>h?Pl(h,()=>`output "${this.outputNames[l[w]]}"`):null),m=await kl(this.sessionId,i,d,l,c,n),u={};for(let h=0;h<m.length;h++)u[this.outputNames[l[h]]]=a[h]??wh(m[h]);return De(),u}startProfiling(){}endProfiling(){El(this.sessionId)}}});var vh,dn,zl=M(()=>{"use strict";qe();Io();Ol();Gt();vh=()=>{if((typeof fe.wasm.initTimeout!="number"||fe.wasm.initTimeout<0)&&(fe.wasm.initTimeout=0),fe.wasm.simd===!1&&console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'),typeof fe.wasm.proxy!="boolean"&&(fe.wasm.proxy=!1),typeof fe.wasm.trace!="boolean"&&(fe.wasm.trace=!1),typeof fe.wasm.numThreads!="number"||!Number.isInteger(fe.wasm.numThreads)||fe.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)fe.wasm.numThreads=1;else{let e=typeof navigator>"u"?zn("node:os").cpus().length:navigator.hardwareConcurrency;fe.wasm.numThreads=Math.min(4,Math.ceil((e||1)/2))}},dn=class{async init(t){vh(),await Sl(),await Il(t)}async createInferenceSessionHandler(t,r){let n=new un;return await n.loadModel(t,r),Promise.resolve(n)}}});var Bl={};Lt(Bl,{wasmBackend:()=>$h});var $h,Dl=M(()=>{"use strict";zl();$h=new dn});qe();qe();qe();var ga="1.20.0-dev.20240821-009209e016";var tx=Mn;{let e=(Dl(),hr(Bl)).wasmBackend;wt("webgpu",e,5),wt("webnn",e,5),wt("cpu",e,10),wt("wasm",e,10)}Object.defineProperty(fe.versions,"web",{value:ga,enumerable:!0});
|
|
6371
6371
|
/**
|
|
6372
6372
|
* @license
|
|
6373
6373
|
* Copyright 2021 Google LLC. All Rights Reserved.
|
|
@@ -7073,7 +7073,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
7073
7073
|
|
|
7074
7074
|
|
|
7075
7075
|
|
|
7076
|
-
const VERSION = '3.0.0-alpha.
|
|
7076
|
+
const VERSION = '3.0.0-alpha.9';
|
|
7077
7077
|
|
|
7078
7078
|
// Check if various APIs are available (depends on environment)
|
|
7079
7079
|
const IS_BROWSER_ENV = typeof self !== 'undefined';
|